/* * Marvell Armada 3700 Functional Specifications describes that bit 21 of UART * Clock Control register controls UART1 and bit 20 controls UART2. But in * reality bit 21 controls UART2 and bit 20 controls UART1. This seems to be an * error in Marvell's documentation. Hence following CLK_DIS macros are swapped.
*/
#define UART_BRDV 0x10 /* These bits are located in UART1 address space and control UART2 */ #define UART2_CLK_DIS BIT(21) /* These bits are located in UART1 address space and control UART1 */ #define UART1_CLK_DIS BIT(20) /* These bits are located in UART1 address space and control both UARTs */ #define CLK_NO_XTAL BIT(19) #define CLK_TBG_DIV1_SHIFT 15 #define CLK_TBG_DIV1_MASK 0x7 #define CLK_TBG_DIV1_MAX 6 #define CLK_TBG_DIV2_SHIFT 12 #define CLK_TBG_DIV2_MASK 0x7 #define CLK_TBG_DIV2_MAX 6 #define CLK_TBG_SEL_SHIFT 10 #define CLK_TBG_SEL_MASK 0x3 /* These bits are located in both UARTs address space */ #define BRDV_BAUD_MASK 0x3FF #define BRDV_BAUD_MAX BRDV_BAUD_MASK
enum { /* Either there is only one summed IRQ... */
UART_IRQ_SUM = 0, /* ...or there are two separate IRQ for RX and TX */
UART_RX_IRQ = 0,
UART_TX_IRQ,
UART_IRQ_COUNT
};
staticvoid mvebu_uart_set_mctrl(struct uart_port *port, unsignedint mctrl)
{ /* * Even if we do not support configuring the modem control lines, this * function must be proided to the serial core
*/
}
do { if (status & STAT_RX_RDY(port)) {
ch = readl(port->membase + UART_RBR(port));
ch &= 0xff;
flag = TTY_NORMAL;
port->icount.rx++;
if (status & STAT_PAR_ERR)
port->icount.parity++;
}
/* * For UART2, error bits are not cleared on buffer read. * This causes interrupt loop and system hang.
*/ if (IS_EXTENDED(port) && (status & STAT_BRK_ERR)) {
ret = readl(port->membase + UART_STAT);
ret |= STAT_BRK_ERR;
writel(ret, port->membase + UART_STAT);
}
if (status & STAT_BRK_DET) {
port->icount.brk++;
status &= ~(STAT_FRM_ERR | STAT_PAR_ERR); if (uart_handle_break(port)) goto ignore_char;
}
if (status & STAT_OVR_ERR)
port->icount.overrun++;
if (status & STAT_FRM_ERR)
port->icount.frame++;
if (uart_handle_sysrq_char(port, ch)) goto ignore_char;
if (status & port->ignore_status_mask & STAT_PAR_ERR)
status &= ~STAT_RX_RDY(port);
status &= port->read_status_mask;
if (status & STAT_PAR_ERR)
flag = TTY_PARITY;
status &= ~port->ignore_status_mask;
if (status & STAT_RX_RDY(port))
tty_insert_flip_char(tport, ch, flag);
if (status & STAT_BRK_DET)
tty_insert_flip_char(tport, 0, TTY_BREAK);
if (status & STAT_FRM_ERR)
tty_insert_flip_char(tport, 0, TTY_FRAME);
if (status & STAT_OVR_ERR)
tty_insert_flip_char(tport, 0, TTY_OVERRUN);
ignore_char:
status = readl(port->membase + UART_STAT);
} while (status & (STAT_RX_RDY(port) | STAT_BRK_DET));
/* Clear the error bits of state register before IRQ request */
ret = readl(port->membase + UART_STAT);
ret |= STAT_BRK_ERR;
writel(ret, port->membase + UART_STAT);
if (!mvuart->irq[UART_TX_IRQ]) { /* Old bindings with just one interrupt (UART0 only) */
ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM],
mvebu_uart_isr, port->irqflags,
dev_name(port->dev), port); if (ret) {
dev_err(port->dev, "unable to request IRQ %d\n",
mvuart->irq[UART_IRQ_SUM]); return ret;
}
} else { /* New bindings with an IRQ for RX and TX (both UART) */
ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ],
mvebu_uart_rx_isr, port->irqflags,
dev_name(port->dev), port); if (ret) {
dev_err(port->dev, "unable to request IRQ %d\n",
mvuart->irq[UART_RX_IRQ]); return ret;
}
ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ],
mvebu_uart_tx_isr, port->irqflags,
dev_name(port->dev),
port); if (ret) {
dev_err(port->dev, "unable to request IRQ %d\n",
mvuart->irq[UART_TX_IRQ]);
devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ],
port); return ret;
}
}
/* * The baudrate is derived from the UART clock thanks to divisors: * > d1 * d2 ("TBG divisors"): can divide only TBG clock from 1 to 6 * > D ("baud generator"): can divide the clock from 1 to 1023 * > M ("fractional divisor"): allows a better accuracy (from 1 to 63) * * Exact formulas for calculating baudrate: * * with default x16 scheme: * baudrate = xtal / (d * 16) * baudrate = tbg / (d1 * d2 * d * 16) * * with fractional divisor: * baudrate = 10 * xtal / (d * (3 * (m1 + m2) + 2 * (m3 + m4))) * baudrate = 10 * tbg / (d1*d2 * d * (3 * (m1 + m2) + 2 * (m3 + m4))) * * Oversampling value: * osamp = (m1 << 0) | (m2 << 8) | (m3 << 16) | (m4 << 24); * * Where m1 controls number of clock cycles per bit for bits 1,2,3; * m2 for bits 4,5,6; m3 for bits 7,8 and m4 for bits 9,10. * * To simplify baudrate setup set all the M prescalers to the same * value. For baudrates 9600 Bd and higher, it is enough to use the * default (x16) divisor or fractional divisor with M = 63, so there * is no need to use real fractional support (where the M prescalers * are not equal). * * When all the M prescalers are zeroed then default (x16) divisor is * used. Default x16 scheme is more stable than M (fractional divisor), * so use M only when D divisor is not enough to derive baudrate. * * Member port->uartclk is either xtal clock rate or TBG clock rate * divided by (d1 * d2). So d1 and d2 are already set by the UART clock * driver (and UART driver itself cannot change them). Moreover they are * shared between both UARTs.
*/
if (d_divisor > BRDV_BAUD_MAX) { /* * Experiments show that small M divisors are unstable. * Use maximal possible M = 63 and calculate D divisor.
*/
m_divisor = OSAMP_MAX_DIVISOR;
d_divisor = DIV_ROUND_CLOSEST(port->uartclk, baud * m_divisor);
}
/* * Maximal divisor is 1023 and maximal fractional divisor is 63. And * experiments show that baudrates above 1/80 of parent clock rate are * not stable. So disallow baudrates above 1/80 of the parent clock * rate. If port->uartclk is not available, then * mvebu_uart_baud_rate_set() fails, so values min_baud and max_baud * in this case do not matter.
*/
min_baud = DIV_ROUND_UP(port->uartclk, BRDV_BAUD_MAX *
OSAMP_MAX_DIVISOR);
max_baud = port->uartclk / 80;
/* Assume that all UART ports have a DT alias or none has */
id = of_alias_get_id(pdev->dev.of_node, "serial"); if (!pdev->dev.of_node || id < 0)
pdev->id = uart_num_counter++; else
pdev->id = id;
if (pdev->id >= MVEBU_NR_UARTS) {
dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
MVEBU_NR_UARTS); return -EINVAL;
}
/* * IRQ number is not stored in this structure because we may have two of * them per port (RX and TX). Instead, use the driver UART structure * array so called ->irq[].
*/
port->irq = 0;
port->irqflags = 0;
/* Get fixed clock frequency */
mvuart->clk = devm_clk_get(&pdev->dev, NULL); if (IS_ERR(mvuart->clk)) { if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER) return PTR_ERR(mvuart->clk);
if (IS_EXTENDED(port)) {
dev_err(&pdev->dev, "unable to get UART clock\n"); return PTR_ERR(mvuart->clk);
}
} else { if (!clk_prepare_enable(mvuart->clk))
port->uartclk = clk_get_rate(mvuart->clk);
}
/* Manage interrupts */ if (platform_irq_count(pdev) == 1) { /* Old bindings: no name on the single unamed UART0 IRQ */
irq = platform_get_irq(pdev, 0); if (irq < 0) return irq;
mvuart->irq[UART_IRQ_SUM] = irq;
} else { /* * New bindings: named interrupts (RX, TX) for both UARTS, * only make use of uart-rx and uart-tx interrupts, do not use * uart-sum of UART0 port.
*/
irq = platform_get_irq_byname(pdev, "uart-rx"); if (irq < 0) return irq;
mvuart->irq[UART_RX_IRQ] = irq;
irq = platform_get_irq_byname(pdev, "uart-tx"); if (irq < 0) return irq;
/* * This function just reconfigures UART Clock Control register (located * in UART1 address space which controls both UART1 and UART2) to * selected UART base clock and recalculates current UART1/UART2 * divisors in their address spaces, so that final baudrate will not be * changed by switching UART parent clock. This is required for * otherwise kernel's boot log stops working - we need to ensure that * UART baudrate does not change during this setup. It is a one time * operation, it will execute only once and set `configured` to true, * and be skipped on subsequent calls. Because this UART Clock Control * register (UART_BRDV) is shared between UART1 baudrate function, * UART1 clock selector and UART2 clock selector, every access to * UART_BRDV (reg1) needs to be protected by a lock.
*/
spin_lock_irqsave(&mvebu_uart_lock, flags);
if (uart_clock_base->configured) {
spin_unlock_irqrestore(&mvebu_uart_lock, flags); return 0;
}
/* Note that uart_clock_base->parent_rates[i] may not be available */
prev_clock_rate = uart_clock_base->parent_rates[prev_clock_idx];
/* Recalculate UART1 divisor so UART1 baudrate does not change */ if (prev_clock_rate) {
divisor = DIV_U64_ROUND_CLOSEST((u64)(val & BRDV_BAUD_MASK) *
parent_clock_rate * prev_d1d2,
prev_clock_rate * d1 * d2); if (divisor < 1)
divisor = 1; elseif (divisor > BRDV_BAUD_MAX)
divisor = BRDV_BAUD_MAX;
val = (val & ~BRDV_BAUD_MASK) | divisor;
}
if (parent_clock_idx != PARENT_CLOCK_XTAL) { /* Do not use XTAL, select TBG clock and TBG d1 * d2 divisors */
val |= CLK_NO_XTAL;
val &= ~(CLK_TBG_DIV1_MASK << CLK_TBG_DIV1_SHIFT);
val |= d1 << CLK_TBG_DIV1_SHIFT;
val &= ~(CLK_TBG_DIV2_MASK << CLK_TBG_DIV2_SHIFT);
val |= d2 << CLK_TBG_DIV2_SHIFT;
val &= ~(CLK_TBG_SEL_MASK << CLK_TBG_SEL_SHIFT);
val |= parent_clock_idx << CLK_TBG_SEL_SHIFT;
} else { /* Use XTAL, TBG bits are then ignored */
val &= ~CLK_NO_XTAL;
}
writel(val, uart_clock_base->reg1);
/* Recalculate UART2 divisor so UART2 baudrate does not change */ if (prev_clock_rate) {
val = readl(uart_clock_base->reg2);
divisor = DIV_U64_ROUND_CLOSEST((u64)(val & BRDV_BAUD_MASK) *
parent_clock_rate * prev_d1d2,
prev_clock_rate * d1 * d2); if (divisor < 1)
divisor = 1; elseif (divisor > BRDV_BAUD_MAX)
divisor = BRDV_BAUD_MAX;
val = (val & ~BRDV_BAUD_MASK) | divisor;
writel(val, uart_clock_base->reg2);
}
staticint mvebu_uart_clock_set_rate(struct clk_hw *hw, unsignedlong rate, unsignedlong parent_rate)
{ /* * We must report success but we can do so unconditionally because * mvebu_uart_clock_round_rate returns values that ensure this call is a * nop.
*/
uart_clock_base = devm_kzalloc(dev, sizeof(*uart_clock_base),
GFP_KERNEL); if (!uart_clock_base) return -ENOMEM;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!res) {
dev_err(dev, "Couldn't get first register\n"); return -ENOENT;
}
/* * UART Clock Control register (reg1 / UART_BRDV) is in the address * space of UART1 (standard UART variant), controls parent clock and * dividers for both UART1 and UART2 and is supplied via DT as the first * resource. Therefore use ioremap() rather than ioremap_resource() to * avoid conflicts with UART1 driver. Access to UART_BRDV is protected * by a lock shared between clock and UART driver.
*/
uart_clock_base->reg1 = devm_ioremap(dev, res->start,
resource_size(res)); if (!uart_clock_base->reg1) return -ENOMEM;
res = platform_get_resource(pdev, IORESOURCE_MEM, 1); if (!res) {
dev_err(dev, "Couldn't get second register\n"); return -ENOENT;
}
/* * UART 2 Baud Rate Divisor register (reg2 / UART_BRDV) is in address * space of UART2 (extended UART variant), controls only one UART2 * specific divider and is supplied via DT as second resource. * Therefore use ioremap() rather than ioremap_resource() to avoid * conflicts with UART2 driver. Access to UART_BRDV is protected by a * by lock shared between clock and UART driver.
*/
uart_clock_base->reg2 = devm_ioremap(dev, res->start,
resource_size(res)); if (!uart_clock_base->reg2) return -ENOMEM;
hw_clk_data->num = ARRAY_SIZE(uart_clk_names); for (i = 0; i < ARRAY_SIZE(uart_clk_names); i++) {
hw_clk_data->hws[i] = &uart_clock_base->clocks[i].clk_hw;
uart_clock_base->clocks[i].clock_idx = i;
}
parent_clk_idx = -1;
for (i = 0; i < ARRAY_SIZE(parent_clk_names); i++) {
parent_clks[i] = devm_clk_get(dev, parent_clk_names[i]); if (IS_ERR(parent_clks[i])) { if (PTR_ERR(parent_clks[i]) == -EPROBE_DEFER) return -EPROBE_DEFER;
dev_warn(dev, "Couldn't get the parent clock %s: %ld\n",
parent_clk_names[i], PTR_ERR(parent_clks[i])); continue;
}
if (i != PARENT_CLOCK_XTAL) { /* * Calculate the smallest TBG d1 and d2 divisors that * still can provide 9600 baudrate.
*/
d1 = DIV_ROUND_UP(rate, 9600 * OSAMP_MAX_DIVISOR *
BRDV_BAUD_MAX); if (d1 < 1)
d1 = 1; elseif (d1 > CLK_TBG_DIV1_MAX)
d1 = CLK_TBG_DIV1_MAX;
d2 = DIV_ROUND_UP(rate, 9600 * OSAMP_MAX_DIVISOR *
BRDV_BAUD_MAX * d1); if (d2 < 1)
d2 = 1; elseif (d2 > CLK_TBG_DIV2_MAX)
d2 = CLK_TBG_DIV2_MAX;
} else { /* * When UART clock uses XTAL clock as a source then it * is not possible to use d1 and d2 divisors.
*/
d1 = d2 = 1;
}
/* Skip clock source which cannot provide 9600 baudrate */ if (rate > 9600 * OSAMP_MAX_DIVISOR * BRDV_BAUD_MAX * d1 * d2) continue;
/* * Choose TBG clock source with the smallest divisors. Use XTAL * clock source only in case TBG is not available as XTAL cannot * be used for baudrates higher than 230400.
*/ if (parent_clk_idx == -1 ||
(i != PARENT_CLOCK_XTAL && div > d1 * d2)) {
parent_clk_idx = i;
div = d1 * d2;
}
}
for (i = 0; i < ARRAY_SIZE(parent_clk_names); i++) { if (i == parent_clk_idx || IS_ERR(parent_clks[i])) continue;
clk_disable_unprepare(parent_clks[i]);
devm_clk_put(dev, parent_clks[i]);
}
if (parent_clk_idx == -1) {
dev_err(dev, "No usable parent clock\n"); return -ENOENT;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.