1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 230530791SWilson Ding /* 330530791SWilson Ding * *************************************************************************** 489ebc274SPaul Gortmaker * Marvell Armada-3700 Serial Driver 589ebc274SPaul Gortmaker * Author: Wilson Ding <dingwei@marvell.com> 630530791SWilson Ding * Copyright (C) 2015 Marvell International Ltd. 730530791SWilson Ding * *************************************************************************** 830530791SWilson Ding */ 930530791SWilson Ding 1030530791SWilson Ding #include <linux/clk.h> 11b7e2b536SPali Rohár #include <linux/clk-provider.h> 1230530791SWilson Ding #include <linux/console.h> 1330530791SWilson Ding #include <linux/delay.h> 1430530791SWilson Ding #include <linux/device.h> 1530530791SWilson Ding #include <linux/init.h> 1630530791SWilson Ding #include <linux/io.h> 1730530791SWilson Ding #include <linux/iopoll.h> 18b7e2b536SPali Rohár #include <linux/math64.h> 1930530791SWilson Ding #include <linux/of.h> 2030530791SWilson Ding #include <linux/of_address.h> 2130530791SWilson Ding #include <linux/of_device.h> 2230530791SWilson Ding #include <linux/of_irq.h> 2330530791SWilson Ding #include <linux/of_platform.h> 2430530791SWilson Ding #include <linux/platform_device.h> 2530530791SWilson Ding #include <linux/serial.h> 2630530791SWilson Ding #include <linux/serial_core.h> 2730530791SWilson Ding #include <linux/slab.h> 2830530791SWilson Ding #include <linux/tty.h> 2930530791SWilson Ding #include <linux/tty_flip.h> 3030530791SWilson Ding 3130530791SWilson Ding /* Register Map */ 325218d769SMiquel Raynal #define UART_STD_RBR 0x00 3353501e02SMiquel Raynal #define UART_EXT_RBR 0x18 3430530791SWilson Ding 355218d769SMiquel Raynal #define UART_STD_TSH 0x04 3653501e02SMiquel Raynal #define UART_EXT_TSH 0x1C 3730530791SWilson Ding 385218d769SMiquel Raynal #define UART_STD_CTRL1 0x08 3953501e02SMiquel Raynal #define UART_EXT_CTRL1 0x04 4030530791SWilson Ding #define CTRL_SOFT_RST BIT(31) 4130530791SWilson Ding #define CTRL_TXFIFO_RST BIT(15) 4230530791SWilson Ding #define CTRL_RXFIFO_RST BIT(14) 4330530791SWilson Ding #define CTRL_SND_BRK_SEQ BIT(11) 4430530791SWilson Ding #define CTRL_BRK_DET_INT BIT(3) 4530530791SWilson Ding #define CTRL_FRM_ERR_INT BIT(2) 4630530791SWilson Ding #define CTRL_PAR_ERR_INT BIT(1) 4730530791SWilson Ding #define CTRL_OVR_ERR_INT BIT(0) 485218d769SMiquel Raynal #define CTRL_BRK_INT (CTRL_BRK_DET_INT | CTRL_FRM_ERR_INT | \ 495218d769SMiquel Raynal CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT) 5030530791SWilson Ding 515218d769SMiquel Raynal #define UART_STD_CTRL2 UART_STD_CTRL1 5253501e02SMiquel Raynal #define UART_EXT_CTRL2 0x20 535218d769SMiquel Raynal #define CTRL_STD_TX_RDY_INT BIT(5) 5453501e02SMiquel Raynal #define CTRL_EXT_TX_RDY_INT BIT(6) 555218d769SMiquel Raynal #define CTRL_STD_RX_RDY_INT BIT(4) 5653501e02SMiquel Raynal #define CTRL_EXT_RX_RDY_INT BIT(5) 575218d769SMiquel Raynal 585218d769SMiquel Raynal #define UART_STAT 0x0C 5930530791SWilson Ding #define STAT_TX_FIFO_EMP BIT(13) 6030530791SWilson Ding #define STAT_TX_FIFO_FUL BIT(11) 6130530791SWilson Ding #define STAT_TX_EMP BIT(6) 625218d769SMiquel Raynal #define STAT_STD_TX_RDY BIT(5) 6353501e02SMiquel Raynal #define STAT_EXT_TX_RDY BIT(15) 645218d769SMiquel Raynal #define STAT_STD_RX_RDY BIT(4) 6553501e02SMiquel Raynal #define STAT_EXT_RX_RDY BIT(14) 6630530791SWilson Ding #define STAT_BRK_DET BIT(3) 6730530791SWilson Ding #define STAT_FRM_ERR BIT(2) 6830530791SWilson Ding #define STAT_PAR_ERR BIT(1) 6930530791SWilson Ding #define STAT_OVR_ERR BIT(0) 700ef5a6e0SColin Ian King #define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR \ 7130530791SWilson Ding | STAT_PAR_ERR | STAT_OVR_ERR) 7230530791SWilson Ding 73b7e2b536SPali Rohár /* 74b7e2b536SPali Rohár * Marvell Armada 3700 Functional Specifications describes that bit 21 of UART 75b7e2b536SPali Rohár * Clock Control register controls UART1 and bit 20 controls UART2. But in 76b7e2b536SPali Rohár * reality bit 21 controls UART2 and bit 20 controls UART1. This seems to be an 77b7e2b536SPali Rohár * error in Marvell's documentation. Hence following CLK_DIS macros are swapped. 78b7e2b536SPali Rohár */ 79b7e2b536SPali Rohár 8030530791SWilson Ding #define UART_BRDV 0x10 81b7e2b536SPali Rohár /* These bits are located in UART1 address space and control UART2 */ 82b7e2b536SPali Rohár #define UART2_CLK_DIS BIT(21) 83b7e2b536SPali Rohár /* These bits are located in UART1 address space and control UART1 */ 84b7e2b536SPali Rohár #define UART1_CLK_DIS BIT(20) 85b7e2b536SPali Rohár /* These bits are located in UART1 address space and control both UARTs */ 86b7e2b536SPali Rohár #define CLK_NO_XTAL BIT(19) 87b7e2b536SPali Rohár #define CLK_TBG_DIV1_SHIFT 15 88b7e2b536SPali Rohár #define CLK_TBG_DIV1_MASK 0x7 89b7e2b536SPali Rohár #define CLK_TBG_DIV1_MAX 6 90b7e2b536SPali Rohár #define CLK_TBG_DIV2_SHIFT 12 91b7e2b536SPali Rohár #define CLK_TBG_DIV2_MASK 0x7 92b7e2b536SPali Rohár #define CLK_TBG_DIV2_MAX 6 93b7e2b536SPali Rohár #define CLK_TBG_SEL_SHIFT 10 94b7e2b536SPali Rohár #define CLK_TBG_SEL_MASK 0x3 95b7e2b536SPali Rohár /* These bits are located in both UARTs address space */ 9668a0db1dSAllen Yan #define BRDV_BAUD_MASK 0x3FF 97b7e2b536SPali Rohár #define BRDV_BAUD_MAX BRDV_BAUD_MASK 9830530791SWilson Ding 99394e8351SMiquel Raynal #define UART_OSAMP 0x14 1000e4cf69eSMiquel Raynal #define OSAMP_DEFAULT_DIVISOR 16 10135d7a58aSMiquel Raynal #define OSAMP_DIVISORS_MASK 0x3F3F3F3F 102694b7112SPali Rohár #define OSAMP_MAX_DIVISOR 63 103394e8351SMiquel Raynal 1043a75e91bSMiquel Raynal #define MVEBU_NR_UARTS 2 10530530791SWilson Ding 10630530791SWilson Ding #define MVEBU_UART_TYPE "mvebu-uart" 10702c33330SYehuda Yitschak #define DRIVER_NAME "mvebu_serial" 10830530791SWilson Ding 10995f78768SMiquel Raynal enum { 11095f78768SMiquel Raynal /* Either there is only one summed IRQ... */ 11195f78768SMiquel Raynal UART_IRQ_SUM = 0, 11295f78768SMiquel Raynal /* ...or there are two separate IRQ for RX and TX */ 11395f78768SMiquel Raynal UART_RX_IRQ = 0, 11495f78768SMiquel Raynal UART_TX_IRQ, 11595f78768SMiquel Raynal UART_IRQ_COUNT 11695f78768SMiquel Raynal }; 11795f78768SMiquel Raynal 11895f78768SMiquel Raynal /* Diverging register offsets */ 1195218d769SMiquel Raynal struct uart_regs_layout { 1205218d769SMiquel Raynal unsigned int rbr; 1215218d769SMiquel Raynal unsigned int tsh; 1225218d769SMiquel Raynal unsigned int ctrl; 1235218d769SMiquel Raynal unsigned int intr; 1245218d769SMiquel Raynal }; 12530530791SWilson Ding 1265218d769SMiquel Raynal /* Diverging flags */ 1275218d769SMiquel Raynal struct uart_flags { 1285218d769SMiquel Raynal unsigned int ctrl_tx_rdy_int; 1295218d769SMiquel Raynal unsigned int ctrl_rx_rdy_int; 1305218d769SMiquel Raynal unsigned int stat_tx_rdy; 1315218d769SMiquel Raynal unsigned int stat_rx_rdy; 1325218d769SMiquel Raynal }; 1335218d769SMiquel Raynal 1345218d769SMiquel Raynal /* Driver data, a structure for each UART port */ 1355218d769SMiquel Raynal struct mvebu_uart_driver_data { 1365218d769SMiquel Raynal bool is_ext; 1375218d769SMiquel Raynal struct uart_regs_layout regs; 1385218d769SMiquel Raynal struct uart_flags flags; 1395218d769SMiquel Raynal }; 1405218d769SMiquel Raynal 141394e8351SMiquel Raynal /* Saved registers during suspend */ 142394e8351SMiquel Raynal struct mvebu_uart_pm_regs { 143394e8351SMiquel Raynal unsigned int rbr; 144394e8351SMiquel Raynal unsigned int tsh; 145394e8351SMiquel Raynal unsigned int ctrl; 146394e8351SMiquel Raynal unsigned int intr; 147394e8351SMiquel Raynal unsigned int stat; 148394e8351SMiquel Raynal unsigned int brdv; 149394e8351SMiquel Raynal unsigned int osamp; 150394e8351SMiquel Raynal }; 151394e8351SMiquel Raynal 1525218d769SMiquel Raynal /* MVEBU UART driver structure */ 1535218d769SMiquel Raynal struct mvebu_uart { 15430530791SWilson Ding struct uart_port *port; 15530530791SWilson Ding struct clk *clk; 15695f78768SMiquel Raynal int irq[UART_IRQ_COUNT]; 1575218d769SMiquel Raynal struct mvebu_uart_driver_data *data; 158394e8351SMiquel Raynal #if defined(CONFIG_PM) 159394e8351SMiquel Raynal struct mvebu_uart_pm_regs pm_regs; 160394e8351SMiquel Raynal #endif /* CONFIG_PM */ 16130530791SWilson Ding }; 16230530791SWilson Ding 1635218d769SMiquel Raynal static struct mvebu_uart *to_mvuart(struct uart_port *port) 1645218d769SMiquel Raynal { 1655218d769SMiquel Raynal return (struct mvebu_uart *)port->private_data; 1665218d769SMiquel Raynal } 1675218d769SMiquel Raynal 1685218d769SMiquel Raynal #define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext) 1695218d769SMiquel Raynal 1705218d769SMiquel Raynal #define UART_RBR(port) (to_mvuart(port)->data->regs.rbr) 1715218d769SMiquel Raynal #define UART_TSH(port) (to_mvuart(port)->data->regs.tsh) 1725218d769SMiquel Raynal #define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl) 1735218d769SMiquel Raynal #define UART_INTR(port) (to_mvuart(port)->data->regs.intr) 1745218d769SMiquel Raynal 1755218d769SMiquel Raynal #define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int) 1765218d769SMiquel Raynal #define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int) 1775218d769SMiquel Raynal #define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy) 1785218d769SMiquel Raynal #define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy) 1795218d769SMiquel Raynal 1805218d769SMiquel Raynal static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS]; 1815218d769SMiquel Raynal 182b7e2b536SPali Rohár static DEFINE_SPINLOCK(mvebu_uart_lock); 183b7e2b536SPali Rohár 18430530791SWilson Ding /* Core UART Driver Operations */ 18530530791SWilson Ding static unsigned int mvebu_uart_tx_empty(struct uart_port *port) 18630530791SWilson Ding { 18730530791SWilson Ding unsigned long flags; 18830530791SWilson Ding unsigned int st; 18930530791SWilson Ding 19030530791SWilson Ding spin_lock_irqsave(&port->lock, flags); 19130530791SWilson Ding st = readl(port->membase + UART_STAT); 19230530791SWilson Ding spin_unlock_irqrestore(&port->lock, flags); 19330530791SWilson Ding 19474e1eb3bSPali Rohár return (st & STAT_TX_EMP) ? TIOCSER_TEMT : 0; 19530530791SWilson Ding } 19630530791SWilson Ding 19730530791SWilson Ding static unsigned int mvebu_uart_get_mctrl(struct uart_port *port) 19830530791SWilson Ding { 19930530791SWilson Ding return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR; 20030530791SWilson Ding } 20130530791SWilson Ding 20230530791SWilson Ding static void mvebu_uart_set_mctrl(struct uart_port *port, 20330530791SWilson Ding unsigned int mctrl) 20430530791SWilson Ding { 20530530791SWilson Ding /* 20630530791SWilson Ding * Even if we do not support configuring the modem control lines, this 20730530791SWilson Ding * function must be proided to the serial core 20830530791SWilson Ding */ 20930530791SWilson Ding } 21030530791SWilson Ding 21130530791SWilson Ding static void mvebu_uart_stop_tx(struct uart_port *port) 21230530791SWilson Ding { 2135218d769SMiquel Raynal unsigned int ctl = readl(port->membase + UART_INTR(port)); 21430530791SWilson Ding 2155218d769SMiquel Raynal ctl &= ~CTRL_TX_RDY_INT(port); 2165218d769SMiquel Raynal writel(ctl, port->membase + UART_INTR(port)); 21730530791SWilson Ding } 21830530791SWilson Ding 21930530791SWilson Ding static void mvebu_uart_start_tx(struct uart_port *port) 22030530791SWilson Ding { 22130434b07SAllen Yan unsigned int ctl; 22230434b07SAllen Yan struct circ_buf *xmit = &port->state->xmit; 22330530791SWilson Ding 22430434b07SAllen Yan if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) { 22530434b07SAllen Yan writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port)); 22630434b07SAllen Yan xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 22730434b07SAllen Yan port->icount.tx++; 22830434b07SAllen Yan } 22930434b07SAllen Yan 23030434b07SAllen Yan ctl = readl(port->membase + UART_INTR(port)); 2315218d769SMiquel Raynal ctl |= CTRL_TX_RDY_INT(port); 2325218d769SMiquel Raynal writel(ctl, port->membase + UART_INTR(port)); 23330530791SWilson Ding } 23430530791SWilson Ding 23530530791SWilson Ding static void mvebu_uart_stop_rx(struct uart_port *port) 23630530791SWilson Ding { 2375218d769SMiquel Raynal unsigned int ctl; 23830530791SWilson Ding 2395218d769SMiquel Raynal ctl = readl(port->membase + UART_CTRL(port)); 2405218d769SMiquel Raynal ctl &= ~CTRL_BRK_INT; 2415218d769SMiquel Raynal writel(ctl, port->membase + UART_CTRL(port)); 2425218d769SMiquel Raynal 2435218d769SMiquel Raynal ctl = readl(port->membase + UART_INTR(port)); 2445218d769SMiquel Raynal ctl &= ~CTRL_RX_RDY_INT(port); 2455218d769SMiquel Raynal writel(ctl, port->membase + UART_INTR(port)); 24630530791SWilson Ding } 24730530791SWilson Ding 24830530791SWilson Ding static void mvebu_uart_break_ctl(struct uart_port *port, int brk) 24930530791SWilson Ding { 25030530791SWilson Ding unsigned int ctl; 25130530791SWilson Ding unsigned long flags; 25230530791SWilson Ding 25330530791SWilson Ding spin_lock_irqsave(&port->lock, flags); 2545218d769SMiquel Raynal ctl = readl(port->membase + UART_CTRL(port)); 25530530791SWilson Ding if (brk == -1) 25630530791SWilson Ding ctl |= CTRL_SND_BRK_SEQ; 25730530791SWilson Ding else 25830530791SWilson Ding ctl &= ~CTRL_SND_BRK_SEQ; 2595218d769SMiquel Raynal writel(ctl, port->membase + UART_CTRL(port)); 26030530791SWilson Ding spin_unlock_irqrestore(&port->lock, flags); 26130530791SWilson Ding } 26230530791SWilson Ding 26330530791SWilson Ding static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status) 26430530791SWilson Ding { 26530530791SWilson Ding struct tty_port *tport = &port->state->port; 26630530791SWilson Ding unsigned char ch = 0; 26730530791SWilson Ding char flag = 0; 26830530791SWilson Ding 26930530791SWilson Ding do { 2705218d769SMiquel Raynal if (status & STAT_RX_RDY(port)) { 2715218d769SMiquel Raynal ch = readl(port->membase + UART_RBR(port)); 27230530791SWilson Ding ch &= 0xff; 27330530791SWilson Ding flag = TTY_NORMAL; 27430530791SWilson Ding port->icount.rx++; 27530530791SWilson Ding 27630530791SWilson Ding if (status & STAT_PAR_ERR) 27730530791SWilson Ding port->icount.parity++; 27830530791SWilson Ding } 27930530791SWilson Ding 28030530791SWilson Ding if (status & STAT_BRK_DET) { 28130530791SWilson Ding port->icount.brk++; 28230530791SWilson Ding status &= ~(STAT_FRM_ERR | STAT_PAR_ERR); 28330530791SWilson Ding if (uart_handle_break(port)) 28430530791SWilson Ding goto ignore_char; 28530530791SWilson Ding } 28630530791SWilson Ding 28730530791SWilson Ding if (status & STAT_OVR_ERR) 28830530791SWilson Ding port->icount.overrun++; 28930530791SWilson Ding 29030530791SWilson Ding if (status & STAT_FRM_ERR) 29130530791SWilson Ding port->icount.frame++; 29230530791SWilson Ding 29330530791SWilson Ding if (uart_handle_sysrq_char(port, ch)) 29430530791SWilson Ding goto ignore_char; 29530530791SWilson Ding 29630530791SWilson Ding if (status & port->ignore_status_mask & STAT_PAR_ERR) 2975218d769SMiquel Raynal status &= ~STAT_RX_RDY(port); 29830530791SWilson Ding 29930530791SWilson Ding status &= port->read_status_mask; 30030530791SWilson Ding 30130530791SWilson Ding if (status & STAT_PAR_ERR) 30230530791SWilson Ding flag = TTY_PARITY; 30330530791SWilson Ding 30430530791SWilson Ding status &= ~port->ignore_status_mask; 30530530791SWilson Ding 3065218d769SMiquel Raynal if (status & STAT_RX_RDY(port)) 30730530791SWilson Ding tty_insert_flip_char(tport, ch, flag); 30830530791SWilson Ding 30930530791SWilson Ding if (status & STAT_BRK_DET) 31030530791SWilson Ding tty_insert_flip_char(tport, 0, TTY_BREAK); 31130530791SWilson Ding 31230530791SWilson Ding if (status & STAT_FRM_ERR) 31330530791SWilson Ding tty_insert_flip_char(tport, 0, TTY_FRAME); 31430530791SWilson Ding 31530530791SWilson Ding if (status & STAT_OVR_ERR) 31630530791SWilson Ding tty_insert_flip_char(tport, 0, TTY_OVERRUN); 31730530791SWilson Ding 31830530791SWilson Ding ignore_char: 31930530791SWilson Ding status = readl(port->membase + UART_STAT); 3205218d769SMiquel Raynal } while (status & (STAT_RX_RDY(port) | STAT_BRK_DET)); 32130530791SWilson Ding 32230530791SWilson Ding tty_flip_buffer_push(tport); 32330530791SWilson Ding } 32430530791SWilson Ding 32530530791SWilson Ding static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status) 32630530791SWilson Ding { 32730530791SWilson Ding struct circ_buf *xmit = &port->state->xmit; 32830530791SWilson Ding unsigned int count; 32930530791SWilson Ding unsigned int st; 33030530791SWilson Ding 33130530791SWilson Ding if (port->x_char) { 3325218d769SMiquel Raynal writel(port->x_char, port->membase + UART_TSH(port)); 33330530791SWilson Ding port->icount.tx++; 33430530791SWilson Ding port->x_char = 0; 33530530791SWilson Ding return; 33630530791SWilson Ding } 33730530791SWilson Ding 33830530791SWilson Ding if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 33930530791SWilson Ding mvebu_uart_stop_tx(port); 34030530791SWilson Ding return; 34130530791SWilson Ding } 34230530791SWilson Ding 34330530791SWilson Ding for (count = 0; count < port->fifosize; count++) { 3445218d769SMiquel Raynal writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port)); 34530530791SWilson Ding xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 34630530791SWilson Ding port->icount.tx++; 34730530791SWilson Ding 34830530791SWilson Ding if (uart_circ_empty(xmit)) 34930530791SWilson Ding break; 35030530791SWilson Ding 35130530791SWilson Ding st = readl(port->membase + UART_STAT); 35230530791SWilson Ding if (st & STAT_TX_FIFO_FUL) 35330530791SWilson Ding break; 35430530791SWilson Ding } 35530530791SWilson Ding 35630530791SWilson Ding if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 35730530791SWilson Ding uart_write_wakeup(port); 35830530791SWilson Ding 35930530791SWilson Ding if (uart_circ_empty(xmit)) 36030530791SWilson Ding mvebu_uart_stop_tx(port); 36130530791SWilson Ding } 36230530791SWilson Ding 36330530791SWilson Ding static irqreturn_t mvebu_uart_isr(int irq, void *dev_id) 36430530791SWilson Ding { 36530530791SWilson Ding struct uart_port *port = (struct uart_port *)dev_id; 36630530791SWilson Ding unsigned int st = readl(port->membase + UART_STAT); 36730530791SWilson Ding 3685218d769SMiquel Raynal if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR | 3695218d769SMiquel Raynal STAT_BRK_DET)) 37030530791SWilson Ding mvebu_uart_rx_chars(port, st); 37130530791SWilson Ding 3725218d769SMiquel Raynal if (st & STAT_TX_RDY(port)) 37330530791SWilson Ding mvebu_uart_tx_chars(port, st); 37430530791SWilson Ding 37530530791SWilson Ding return IRQ_HANDLED; 37630530791SWilson Ding } 37730530791SWilson Ding 37895f78768SMiquel Raynal static irqreturn_t mvebu_uart_rx_isr(int irq, void *dev_id) 37995f78768SMiquel Raynal { 38095f78768SMiquel Raynal struct uart_port *port = (struct uart_port *)dev_id; 38195f78768SMiquel Raynal unsigned int st = readl(port->membase + UART_STAT); 38295f78768SMiquel Raynal 38395f78768SMiquel Raynal if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR | 38495f78768SMiquel Raynal STAT_BRK_DET)) 38595f78768SMiquel Raynal mvebu_uart_rx_chars(port, st); 38695f78768SMiquel Raynal 38795f78768SMiquel Raynal return IRQ_HANDLED; 38895f78768SMiquel Raynal } 38995f78768SMiquel Raynal 39095f78768SMiquel Raynal static irqreturn_t mvebu_uart_tx_isr(int irq, void *dev_id) 39195f78768SMiquel Raynal { 39295f78768SMiquel Raynal struct uart_port *port = (struct uart_port *)dev_id; 39395f78768SMiquel Raynal unsigned int st = readl(port->membase + UART_STAT); 39495f78768SMiquel Raynal 39595f78768SMiquel Raynal if (st & STAT_TX_RDY(port)) 39695f78768SMiquel Raynal mvebu_uart_tx_chars(port, st); 39795f78768SMiquel Raynal 39895f78768SMiquel Raynal return IRQ_HANDLED; 39995f78768SMiquel Raynal } 40095f78768SMiquel Raynal 40130530791SWilson Ding static int mvebu_uart_startup(struct uart_port *port) 40230530791SWilson Ding { 40395f78768SMiquel Raynal struct mvebu_uart *mvuart = to_mvuart(port); 4045218d769SMiquel Raynal unsigned int ctl; 40530530791SWilson Ding int ret; 40630530791SWilson Ding 40730530791SWilson Ding writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST, 4085218d769SMiquel Raynal port->membase + UART_CTRL(port)); 40930530791SWilson Ding udelay(1); 4102ff23c48SAllen Yan 4112ff23c48SAllen Yan /* Clear the error bits of state register before IRQ request */ 4122ff23c48SAllen Yan ret = readl(port->membase + UART_STAT); 4132ff23c48SAllen Yan ret |= STAT_BRK_ERR; 4142ff23c48SAllen Yan writel(ret, port->membase + UART_STAT); 4152ff23c48SAllen Yan 4165218d769SMiquel Raynal writel(CTRL_BRK_INT, port->membase + UART_CTRL(port)); 4175218d769SMiquel Raynal 4185218d769SMiquel Raynal ctl = readl(port->membase + UART_INTR(port)); 4195218d769SMiquel Raynal ctl |= CTRL_RX_RDY_INT(port); 4205218d769SMiquel Raynal writel(ctl, port->membase + UART_INTR(port)); 42130530791SWilson Ding 42295f78768SMiquel Raynal if (!mvuart->irq[UART_TX_IRQ]) { 42395f78768SMiquel Raynal /* Old bindings with just one interrupt (UART0 only) */ 42495f78768SMiquel Raynal ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM], 42595f78768SMiquel Raynal mvebu_uart_isr, port->irqflags, 42695f78768SMiquel Raynal dev_name(port->dev), port); 42730530791SWilson Ding if (ret) { 42895f78768SMiquel Raynal dev_err(port->dev, "unable to request IRQ %d\n", 42995f78768SMiquel Raynal mvuart->irq[UART_IRQ_SUM]); 43030530791SWilson Ding return ret; 43130530791SWilson Ding } 43295f78768SMiquel Raynal } else { 43395f78768SMiquel Raynal /* New bindings with an IRQ for RX and TX (both UART) */ 43495f78768SMiquel Raynal ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ], 43595f78768SMiquel Raynal mvebu_uart_rx_isr, port->irqflags, 43695f78768SMiquel Raynal dev_name(port->dev), port); 43795f78768SMiquel Raynal if (ret) { 43895f78768SMiquel Raynal dev_err(port->dev, "unable to request IRQ %d\n", 43995f78768SMiquel Raynal mvuart->irq[UART_RX_IRQ]); 44095f78768SMiquel Raynal return ret; 44195f78768SMiquel Raynal } 44295f78768SMiquel Raynal 44395f78768SMiquel Raynal ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ], 44495f78768SMiquel Raynal mvebu_uart_tx_isr, port->irqflags, 44595f78768SMiquel Raynal dev_name(port->dev), 44695f78768SMiquel Raynal port); 44795f78768SMiquel Raynal if (ret) { 44895f78768SMiquel Raynal dev_err(port->dev, "unable to request IRQ %d\n", 44995f78768SMiquel Raynal mvuart->irq[UART_TX_IRQ]); 45095f78768SMiquel Raynal devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], 45195f78768SMiquel Raynal port); 45295f78768SMiquel Raynal return ret; 45395f78768SMiquel Raynal } 45495f78768SMiquel Raynal } 45530530791SWilson Ding 45630530791SWilson Ding return 0; 45730530791SWilson Ding } 45830530791SWilson Ding 45930530791SWilson Ding static void mvebu_uart_shutdown(struct uart_port *port) 46030530791SWilson Ding { 46195f78768SMiquel Raynal struct mvebu_uart *mvuart = to_mvuart(port); 46295f78768SMiquel Raynal 4635218d769SMiquel Raynal writel(0, port->membase + UART_INTR(port)); 464c2c1659bSThomas Petazzoni 46595f78768SMiquel Raynal if (!mvuart->irq[UART_TX_IRQ]) { 46695f78768SMiquel Raynal devm_free_irq(port->dev, mvuart->irq[UART_IRQ_SUM], port); 46795f78768SMiquel Raynal } else { 46895f78768SMiquel Raynal devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], port); 46995f78768SMiquel Raynal devm_free_irq(port->dev, mvuart->irq[UART_TX_IRQ], port); 47095f78768SMiquel Raynal } 47130530791SWilson Ding } 47230530791SWilson Ding 47368a0db1dSAllen Yan static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud) 47468a0db1dSAllen Yan { 4750e4cf69eSMiquel Raynal unsigned int d_divisor, m_divisor; 476b7e2b536SPali Rohár unsigned long flags; 47735d7a58aSMiquel Raynal u32 brdv, osamp; 47868a0db1dSAllen Yan 479ecd6b010SPali Rohár if (!port->uartclk) 480ecd6b010SPali Rohár return -EOPNOTSUPP; 48168a0db1dSAllen Yan 48268a0db1dSAllen Yan /* 483694b7112SPali Rohár * The baudrate is derived from the UART clock thanks to divisors: 484694b7112SPali Rohár * > d1 * d2 ("TBG divisors"): can divide only TBG clock from 1 to 6 485694b7112SPali Rohár * > D ("baud generator"): can divide the clock from 1 to 1023 486694b7112SPali Rohár * > M ("fractional divisor"): allows a better accuracy (from 1 to 63) 4870e4cf69eSMiquel Raynal * 488694b7112SPali Rohár * Exact formulas for calculating baudrate: 489694b7112SPali Rohár * 490694b7112SPali Rohár * with default x16 scheme: 491694b7112SPali Rohár * baudrate = xtal / (d * 16) 492694b7112SPali Rohár * baudrate = tbg / (d1 * d2 * d * 16) 493694b7112SPali Rohár * 494694b7112SPali Rohár * with fractional divisor: 495694b7112SPali Rohár * baudrate = 10 * xtal / (d * (3 * (m1 + m2) + 2 * (m3 + m4))) 496694b7112SPali Rohár * baudrate = 10 * tbg / (d1*d2 * d * (3 * (m1 + m2) + 2 * (m3 + m4))) 497694b7112SPali Rohár * 498694b7112SPali Rohár * Oversampling value: 499694b7112SPali Rohár * osamp = (m1 << 0) | (m2 << 8) | (m3 << 16) | (m4 << 24); 500694b7112SPali Rohár * 501694b7112SPali Rohár * Where m1 controls number of clock cycles per bit for bits 1,2,3; 502694b7112SPali Rohár * m2 for bits 4,5,6; m3 for bits 7,8 and m4 for bits 9,10. 503694b7112SPali Rohár * 504694b7112SPali Rohár * To simplify baudrate setup set all the M prescalers to the same 505694b7112SPali Rohár * value. For baudrates 9600 Bd and higher, it is enough to use the 506694b7112SPali Rohár * default (x16) divisor or fractional divisor with M = 63, so there 507694b7112SPali Rohár * is no need to use real fractional support (where the M prescalers 508694b7112SPali Rohár * are not equal). 509694b7112SPali Rohár * 510694b7112SPali Rohár * When all the M prescalers are zeroed then default (x16) divisor is 511694b7112SPali Rohár * used. Default x16 scheme is more stable than M (fractional divisor), 512694b7112SPali Rohár * so use M only when D divisor is not enough to derive baudrate. 513694b7112SPali Rohár * 514694b7112SPali Rohár * Member port->uartclk is either xtal clock rate or TBG clock rate 515694b7112SPali Rohár * divided by (d1 * d2). So d1 and d2 are already set by the UART clock 516694b7112SPali Rohár * driver (and UART driver itself cannot change them). Moreover they are 517694b7112SPali Rohár * shared between both UARTs. 51868a0db1dSAllen Yan */ 519694b7112SPali Rohár 5200e4cf69eSMiquel Raynal m_divisor = OSAMP_DEFAULT_DIVISOR; 5219078204cSPali Rohár d_divisor = DIV_ROUND_CLOSEST(port->uartclk, baud * m_divisor); 5220e4cf69eSMiquel Raynal 523694b7112SPali Rohár if (d_divisor > BRDV_BAUD_MAX) { 524694b7112SPali Rohár /* 525694b7112SPali Rohár * Experiments show that small M divisors are unstable. 526694b7112SPali Rohár * Use maximal possible M = 63 and calculate D divisor. 527694b7112SPali Rohár */ 528694b7112SPali Rohár m_divisor = OSAMP_MAX_DIVISOR; 529694b7112SPali Rohár d_divisor = DIV_ROUND_CLOSEST(port->uartclk, baud * m_divisor); 530694b7112SPali Rohár } 531694b7112SPali Rohár 532694b7112SPali Rohár if (d_divisor < 1) 533694b7112SPali Rohár d_divisor = 1; 534694b7112SPali Rohár else if (d_divisor > BRDV_BAUD_MAX) 535694b7112SPali Rohár d_divisor = BRDV_BAUD_MAX; 536694b7112SPali Rohár 537b7e2b536SPali Rohár spin_lock_irqsave(&mvebu_uart_lock, flags); 53868a0db1dSAllen Yan brdv = readl(port->membase + UART_BRDV); 53968a0db1dSAllen Yan brdv &= ~BRDV_BAUD_MASK; 5400e4cf69eSMiquel Raynal brdv |= d_divisor; 54168a0db1dSAllen Yan writel(brdv, port->membase + UART_BRDV); 542b7e2b536SPali Rohár spin_unlock_irqrestore(&mvebu_uart_lock, flags); 54368a0db1dSAllen Yan 54435d7a58aSMiquel Raynal osamp = readl(port->membase + UART_OSAMP); 54535d7a58aSMiquel Raynal osamp &= ~OSAMP_DIVISORS_MASK; 546694b7112SPali Rohár if (m_divisor != OSAMP_DEFAULT_DIVISOR) 547694b7112SPali Rohár osamp |= (m_divisor << 0) | (m_divisor << 8) | 548694b7112SPali Rohár (m_divisor << 16) | (m_divisor << 24); 54935d7a58aSMiquel Raynal writel(osamp, port->membase + UART_OSAMP); 55035d7a58aSMiquel Raynal 55168a0db1dSAllen Yan return 0; 55268a0db1dSAllen Yan } 55368a0db1dSAllen Yan 55430530791SWilson Ding static void mvebu_uart_set_termios(struct uart_port *port, 55530530791SWilson Ding struct ktermios *termios, 55630530791SWilson Ding struct ktermios *old) 55730530791SWilson Ding { 55830530791SWilson Ding unsigned long flags; 559deeaf963SPali Rohár unsigned int baud, min_baud, max_baud; 56030530791SWilson Ding 56130530791SWilson Ding spin_lock_irqsave(&port->lock, flags); 56230530791SWilson Ding 5635218d769SMiquel Raynal port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR | 5645218d769SMiquel Raynal STAT_TX_RDY(port) | STAT_TX_FIFO_FUL; 56530530791SWilson Ding 56630530791SWilson Ding if (termios->c_iflag & INPCK) 56730530791SWilson Ding port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR; 56830530791SWilson Ding 56930530791SWilson Ding port->ignore_status_mask = 0; 57030530791SWilson Ding if (termios->c_iflag & IGNPAR) 57130530791SWilson Ding port->ignore_status_mask |= 57230530791SWilson Ding STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR; 57330530791SWilson Ding 57430530791SWilson Ding if ((termios->c_cflag & CREAD) == 0) 5755218d769SMiquel Raynal port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR; 57630530791SWilson Ding 57768a0db1dSAllen Yan /* 578694b7112SPali Rohár * Maximal divisor is 1023 and maximal fractional divisor is 63. And 579694b7112SPali Rohár * experiments show that baudrates above 1/80 of parent clock rate are 580694b7112SPali Rohár * not stable. So disallow baudrates above 1/80 of the parent clock 581694b7112SPali Rohár * rate. If port->uartclk is not available, then 582694b7112SPali Rohár * mvebu_uart_baud_rate_set() fails, so values min_baud and max_baud 583694b7112SPali Rohár * in this case do not matter. 58468a0db1dSAllen Yan */ 585694b7112SPali Rohár min_baud = DIV_ROUND_UP(port->uartclk, BRDV_BAUD_MAX * 586694b7112SPali Rohár OSAMP_MAX_DIVISOR); 587694b7112SPali Rohár max_baud = port->uartclk / 80; 588deeaf963SPali Rohár 589deeaf963SPali Rohár baud = uart_get_baud_rate(port, termios, old, min_baud, max_baud); 59068a0db1dSAllen Yan if (mvebu_uart_baud_rate_set(port, baud)) { 59168a0db1dSAllen Yan /* No clock available, baudrate cannot be changed */ 59230530791SWilson Ding if (old) 593deeaf963SPali Rohár baud = uart_get_baud_rate(port, old, NULL, 594deeaf963SPali Rohár min_baud, max_baud); 59568a0db1dSAllen Yan } else { 59668a0db1dSAllen Yan tty_termios_encode_baud_rate(termios, baud, baud); 59730530791SWilson Ding uart_update_timeout(port, termios->c_cflag, baud); 59868a0db1dSAllen Yan } 59968a0db1dSAllen Yan 60068a0db1dSAllen Yan /* Only the following flag changes are supported */ 60168a0db1dSAllen Yan if (old) { 60268a0db1dSAllen Yan termios->c_iflag &= INPCK | IGNPAR; 60368a0db1dSAllen Yan termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR); 60468a0db1dSAllen Yan termios->c_cflag &= CREAD | CBAUD; 60568a0db1dSAllen Yan termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD); 606e0bf2d49SJan Kiszka termios->c_cflag |= CS8; 60768a0db1dSAllen Yan } 60830530791SWilson Ding 60930530791SWilson Ding spin_unlock_irqrestore(&port->lock, flags); 61030530791SWilson Ding } 61130530791SWilson Ding 61230530791SWilson Ding static const char *mvebu_uart_type(struct uart_port *port) 61330530791SWilson Ding { 61430530791SWilson Ding return MVEBU_UART_TYPE; 61530530791SWilson Ding } 61630530791SWilson Ding 61730530791SWilson Ding static void mvebu_uart_release_port(struct uart_port *port) 61830530791SWilson Ding { 61930530791SWilson Ding /* Nothing to do here */ 62030530791SWilson Ding } 62130530791SWilson Ding 62230530791SWilson Ding static int mvebu_uart_request_port(struct uart_port *port) 62330530791SWilson Ding { 62430530791SWilson Ding return 0; 62530530791SWilson Ding } 62630530791SWilson Ding 62730530791SWilson Ding #ifdef CONFIG_CONSOLE_POLL 62830530791SWilson Ding static int mvebu_uart_get_poll_char(struct uart_port *port) 62930530791SWilson Ding { 63030530791SWilson Ding unsigned int st = readl(port->membase + UART_STAT); 63130530791SWilson Ding 6325218d769SMiquel Raynal if (!(st & STAT_RX_RDY(port))) 63330530791SWilson Ding return NO_POLL_CHAR; 63430530791SWilson Ding 6355218d769SMiquel Raynal return readl(port->membase + UART_RBR(port)); 63630530791SWilson Ding } 63730530791SWilson Ding 63830530791SWilson Ding static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c) 63930530791SWilson Ding { 64030530791SWilson Ding unsigned int st; 64130530791SWilson Ding 64230530791SWilson Ding for (;;) { 64330530791SWilson Ding st = readl(port->membase + UART_STAT); 64430530791SWilson Ding 64530530791SWilson Ding if (!(st & STAT_TX_FIFO_FUL)) 64630530791SWilson Ding break; 64730530791SWilson Ding 64830530791SWilson Ding udelay(1); 64930530791SWilson Ding } 65030530791SWilson Ding 6515218d769SMiquel Raynal writel(c, port->membase + UART_TSH(port)); 65230530791SWilson Ding } 65330530791SWilson Ding #endif 65430530791SWilson Ding 65530530791SWilson Ding static const struct uart_ops mvebu_uart_ops = { 65630530791SWilson Ding .tx_empty = mvebu_uart_tx_empty, 65730530791SWilson Ding .set_mctrl = mvebu_uart_set_mctrl, 65830530791SWilson Ding .get_mctrl = mvebu_uart_get_mctrl, 65930530791SWilson Ding .stop_tx = mvebu_uart_stop_tx, 66030530791SWilson Ding .start_tx = mvebu_uart_start_tx, 66130530791SWilson Ding .stop_rx = mvebu_uart_stop_rx, 66230530791SWilson Ding .break_ctl = mvebu_uart_break_ctl, 66330530791SWilson Ding .startup = mvebu_uart_startup, 66430530791SWilson Ding .shutdown = mvebu_uart_shutdown, 66530530791SWilson Ding .set_termios = mvebu_uart_set_termios, 66630530791SWilson Ding .type = mvebu_uart_type, 66730530791SWilson Ding .release_port = mvebu_uart_release_port, 66830530791SWilson Ding .request_port = mvebu_uart_request_port, 66930530791SWilson Ding #ifdef CONFIG_CONSOLE_POLL 67030530791SWilson Ding .poll_get_char = mvebu_uart_get_poll_char, 67130530791SWilson Ding .poll_put_char = mvebu_uart_put_poll_char, 67230530791SWilson Ding #endif 67330530791SWilson Ding }; 67430530791SWilson Ding 67530530791SWilson Ding /* Console Driver Operations */ 67630530791SWilson Ding 67730530791SWilson Ding #ifdef CONFIG_SERIAL_MVEBU_CONSOLE 67830530791SWilson Ding /* Early Console */ 679*3f8bab17SJiri Slaby static void mvebu_uart_putc(struct uart_port *port, unsigned char c) 68030530791SWilson Ding { 68130530791SWilson Ding unsigned int st; 68230530791SWilson Ding 68330530791SWilson Ding for (;;) { 68430530791SWilson Ding st = readl(port->membase + UART_STAT); 68530530791SWilson Ding if (!(st & STAT_TX_FIFO_FUL)) 68630530791SWilson Ding break; 68730530791SWilson Ding } 68830530791SWilson Ding 6895218d769SMiquel Raynal /* At early stage, DT is not parsed yet, only use UART0 */ 6905218d769SMiquel Raynal writel(c, port->membase + UART_STD_TSH); 69130530791SWilson Ding 69230530791SWilson Ding for (;;) { 69330530791SWilson Ding st = readl(port->membase + UART_STAT); 69430530791SWilson Ding if (st & STAT_TX_FIFO_EMP) 69530530791SWilson Ding break; 69630530791SWilson Ding } 69730530791SWilson Ding } 69830530791SWilson Ding 69930530791SWilson Ding static void mvebu_uart_putc_early_write(struct console *con, 70030530791SWilson Ding const char *s, 7015607fa6cSJinchao Wang unsigned int n) 70230530791SWilson Ding { 70330530791SWilson Ding struct earlycon_device *dev = con->data; 70430530791SWilson Ding 70530530791SWilson Ding uart_console_write(&dev->port, s, n, mvebu_uart_putc); 70630530791SWilson Ding } 70730530791SWilson Ding 70830530791SWilson Ding static int __init 70930530791SWilson Ding mvebu_uart_early_console_setup(struct earlycon_device *device, 71030530791SWilson Ding const char *opt) 71130530791SWilson Ding { 71230530791SWilson Ding if (!device->port.membase) 71330530791SWilson Ding return -ENODEV; 71430530791SWilson Ding 71530530791SWilson Ding device->con->write = mvebu_uart_putc_early_write; 71630530791SWilson Ding 71730530791SWilson Ding return 0; 71830530791SWilson Ding } 71930530791SWilson Ding 72030530791SWilson Ding EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup); 72130530791SWilson Ding OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart", 72230530791SWilson Ding mvebu_uart_early_console_setup); 72330530791SWilson Ding 72430530791SWilson Ding static void wait_for_xmitr(struct uart_port *port) 72530530791SWilson Ding { 72630530791SWilson Ding u32 val; 72730530791SWilson Ding 72830530791SWilson Ding readl_poll_timeout_atomic(port->membase + UART_STAT, val, 729c685af11SGabriel Matni (val & STAT_TX_RDY(port)), 1, 10000); 73030530791SWilson Ding } 73130530791SWilson Ding 73254ca955bSPali Rohár static void wait_for_xmite(struct uart_port *port) 73354ca955bSPali Rohár { 73454ca955bSPali Rohár u32 val; 73554ca955bSPali Rohár 73654ca955bSPali Rohár readl_poll_timeout_atomic(port->membase + UART_STAT, val, 73754ca955bSPali Rohár (val & STAT_TX_EMP), 1, 10000); 73854ca955bSPali Rohár } 73954ca955bSPali Rohár 740*3f8bab17SJiri Slaby static void mvebu_uart_console_putchar(struct uart_port *port, unsigned char ch) 74130530791SWilson Ding { 74230530791SWilson Ding wait_for_xmitr(port); 7435218d769SMiquel Raynal writel(ch, port->membase + UART_TSH(port)); 74430530791SWilson Ding } 74530530791SWilson Ding 74630530791SWilson Ding static void mvebu_uart_console_write(struct console *co, const char *s, 74730530791SWilson Ding unsigned int count) 74830530791SWilson Ding { 74930530791SWilson Ding struct uart_port *port = &mvebu_uart_ports[co->index]; 75030530791SWilson Ding unsigned long flags; 7515218d769SMiquel Raynal unsigned int ier, intr, ctl; 75230530791SWilson Ding int locked = 1; 75330530791SWilson Ding 75430530791SWilson Ding if (oops_in_progress) 75530530791SWilson Ding locked = spin_trylock_irqsave(&port->lock, flags); 75630530791SWilson Ding else 75730530791SWilson Ding spin_lock_irqsave(&port->lock, flags); 75830530791SWilson Ding 7595218d769SMiquel Raynal ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT; 7605218d769SMiquel Raynal intr = readl(port->membase + UART_INTR(port)) & 7615218d769SMiquel Raynal (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port)); 7625218d769SMiquel Raynal writel(0, port->membase + UART_CTRL(port)); 7635218d769SMiquel Raynal writel(0, port->membase + UART_INTR(port)); 76430530791SWilson Ding 76530530791SWilson Ding uart_console_write(port, s, count, mvebu_uart_console_putchar); 76630530791SWilson Ding 76754ca955bSPali Rohár wait_for_xmite(port); 76830530791SWilson Ding 76930530791SWilson Ding if (ier) 7705218d769SMiquel Raynal writel(ier, port->membase + UART_CTRL(port)); 7715218d769SMiquel Raynal 7725218d769SMiquel Raynal if (intr) { 7735218d769SMiquel Raynal ctl = intr | readl(port->membase + UART_INTR(port)); 7745218d769SMiquel Raynal writel(ctl, port->membase + UART_INTR(port)); 7755218d769SMiquel Raynal } 77630530791SWilson Ding 77730530791SWilson Ding if (locked) 77830530791SWilson Ding spin_unlock_irqrestore(&port->lock, flags); 77930530791SWilson Ding } 78030530791SWilson Ding 78130530791SWilson Ding static int mvebu_uart_console_setup(struct console *co, char *options) 78230530791SWilson Ding { 78330530791SWilson Ding struct uart_port *port; 78430530791SWilson Ding int baud = 9600; 78530530791SWilson Ding int bits = 8; 78630530791SWilson Ding int parity = 'n'; 78730530791SWilson Ding int flow = 'n'; 78830530791SWilson Ding 78930530791SWilson Ding if (co->index < 0 || co->index >= MVEBU_NR_UARTS) 79030530791SWilson Ding return -EINVAL; 79130530791SWilson Ding 79230530791SWilson Ding port = &mvebu_uart_ports[co->index]; 79330530791SWilson Ding 79430530791SWilson Ding if (!port->mapbase || !port->membase) { 79530530791SWilson Ding pr_debug("console on ttyMV%i not present\n", co->index); 79630530791SWilson Ding return -ENODEV; 79730530791SWilson Ding } 79830530791SWilson Ding 79930530791SWilson Ding if (options) 80030530791SWilson Ding uart_parse_options(options, &baud, &parity, &bits, &flow); 80130530791SWilson Ding 80230530791SWilson Ding return uart_set_options(port, co, baud, parity, bits, flow); 80330530791SWilson Ding } 80430530791SWilson Ding 80530530791SWilson Ding static struct uart_driver mvebu_uart_driver; 80630530791SWilson Ding 80730530791SWilson Ding static struct console mvebu_uart_console = { 80830530791SWilson Ding .name = "ttyMV", 80930530791SWilson Ding .write = mvebu_uart_console_write, 81030530791SWilson Ding .device = uart_console_device, 81130530791SWilson Ding .setup = mvebu_uart_console_setup, 81230530791SWilson Ding .flags = CON_PRINTBUFFER, 81330530791SWilson Ding .index = -1, 81430530791SWilson Ding .data = &mvebu_uart_driver, 81530530791SWilson Ding }; 81630530791SWilson Ding 81730530791SWilson Ding static int __init mvebu_uart_console_init(void) 81830530791SWilson Ding { 81930530791SWilson Ding register_console(&mvebu_uart_console); 82030530791SWilson Ding return 0; 82130530791SWilson Ding } 82230530791SWilson Ding 82330530791SWilson Ding console_initcall(mvebu_uart_console_init); 82430530791SWilson Ding 82530530791SWilson Ding 82630530791SWilson Ding #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */ 82730530791SWilson Ding 82830530791SWilson Ding static struct uart_driver mvebu_uart_driver = { 82930530791SWilson Ding .owner = THIS_MODULE, 83002c33330SYehuda Yitschak .driver_name = DRIVER_NAME, 83130530791SWilson Ding .dev_name = "ttyMV", 83230530791SWilson Ding .nr = MVEBU_NR_UARTS, 83330530791SWilson Ding #ifdef CONFIG_SERIAL_MVEBU_CONSOLE 83430530791SWilson Ding .cons = &mvebu_uart_console, 83530530791SWilson Ding #endif 83630530791SWilson Ding }; 83730530791SWilson Ding 838394e8351SMiquel Raynal #if defined(CONFIG_PM) 839394e8351SMiquel Raynal static int mvebu_uart_suspend(struct device *dev) 840394e8351SMiquel Raynal { 841394e8351SMiquel Raynal struct mvebu_uart *mvuart = dev_get_drvdata(dev); 842394e8351SMiquel Raynal struct uart_port *port = mvuart->port; 843b7e2b536SPali Rohár unsigned long flags; 844394e8351SMiquel Raynal 845394e8351SMiquel Raynal uart_suspend_port(&mvebu_uart_driver, port); 846394e8351SMiquel Raynal 847394e8351SMiquel Raynal mvuart->pm_regs.rbr = readl(port->membase + UART_RBR(port)); 848394e8351SMiquel Raynal mvuart->pm_regs.tsh = readl(port->membase + UART_TSH(port)); 849394e8351SMiquel Raynal mvuart->pm_regs.ctrl = readl(port->membase + UART_CTRL(port)); 850394e8351SMiquel Raynal mvuart->pm_regs.intr = readl(port->membase + UART_INTR(port)); 851394e8351SMiquel Raynal mvuart->pm_regs.stat = readl(port->membase + UART_STAT); 852b7e2b536SPali Rohár spin_lock_irqsave(&mvebu_uart_lock, flags); 853394e8351SMiquel Raynal mvuart->pm_regs.brdv = readl(port->membase + UART_BRDV); 854b7e2b536SPali Rohár spin_unlock_irqrestore(&mvebu_uart_lock, flags); 855394e8351SMiquel Raynal mvuart->pm_regs.osamp = readl(port->membase + UART_OSAMP); 856394e8351SMiquel Raynal 857394e8351SMiquel Raynal device_set_wakeup_enable(dev, true); 858394e8351SMiquel Raynal 859394e8351SMiquel Raynal return 0; 860394e8351SMiquel Raynal } 861394e8351SMiquel Raynal 862394e8351SMiquel Raynal static int mvebu_uart_resume(struct device *dev) 863394e8351SMiquel Raynal { 864394e8351SMiquel Raynal struct mvebu_uart *mvuart = dev_get_drvdata(dev); 865394e8351SMiquel Raynal struct uart_port *port = mvuart->port; 866b7e2b536SPali Rohár unsigned long flags; 867394e8351SMiquel Raynal 868394e8351SMiquel Raynal writel(mvuart->pm_regs.rbr, port->membase + UART_RBR(port)); 869394e8351SMiquel Raynal writel(mvuart->pm_regs.tsh, port->membase + UART_TSH(port)); 870394e8351SMiquel Raynal writel(mvuart->pm_regs.ctrl, port->membase + UART_CTRL(port)); 871394e8351SMiquel Raynal writel(mvuart->pm_regs.intr, port->membase + UART_INTR(port)); 872394e8351SMiquel Raynal writel(mvuart->pm_regs.stat, port->membase + UART_STAT); 873b7e2b536SPali Rohár spin_lock_irqsave(&mvebu_uart_lock, flags); 874394e8351SMiquel Raynal writel(mvuart->pm_regs.brdv, port->membase + UART_BRDV); 875b7e2b536SPali Rohár spin_unlock_irqrestore(&mvebu_uart_lock, flags); 876394e8351SMiquel Raynal writel(mvuart->pm_regs.osamp, port->membase + UART_OSAMP); 877394e8351SMiquel Raynal 878394e8351SMiquel Raynal uart_resume_port(&mvebu_uart_driver, port); 879394e8351SMiquel Raynal 880394e8351SMiquel Raynal return 0; 881394e8351SMiquel Raynal } 882394e8351SMiquel Raynal 883394e8351SMiquel Raynal static const struct dev_pm_ops mvebu_uart_pm_ops = { 884394e8351SMiquel Raynal .suspend = mvebu_uart_suspend, 885394e8351SMiquel Raynal .resume = mvebu_uart_resume, 886394e8351SMiquel Raynal }; 887394e8351SMiquel Raynal #endif /* CONFIG_PM */ 888394e8351SMiquel Raynal 8895218d769SMiquel Raynal static const struct of_device_id mvebu_uart_of_match[]; 8905218d769SMiquel Raynal 89194228f95SAllen Yan /* Counter to keep track of each UART port id when not using CONFIG_OF */ 89294228f95SAllen Yan static int uart_num_counter; 89394228f95SAllen Yan 89430530791SWilson Ding static int mvebu_uart_probe(struct platform_device *pdev) 89530530791SWilson Ding { 89630530791SWilson Ding struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0); 8975218d769SMiquel Raynal const struct of_device_id *match = of_match_device(mvebu_uart_of_match, 8985218d769SMiquel Raynal &pdev->dev); 89930530791SWilson Ding struct uart_port *port; 9005218d769SMiquel Raynal struct mvebu_uart *mvuart; 90158e49346SQinglang Miao int id, irq; 90230530791SWilson Ding 90395f78768SMiquel Raynal if (!reg) { 90495f78768SMiquel Raynal dev_err(&pdev->dev, "no registers defined\n"); 90530530791SWilson Ding return -EINVAL; 90630530791SWilson Ding } 90730530791SWilson Ding 90894228f95SAllen Yan /* Assume that all UART ports have a DT alias or none has */ 90994228f95SAllen Yan id = of_alias_get_id(pdev->dev.of_node, "serial"); 91094228f95SAllen Yan if (!pdev->dev.of_node || id < 0) 91194228f95SAllen Yan pdev->id = uart_num_counter++; 91294228f95SAllen Yan else 91394228f95SAllen Yan pdev->id = id; 91494228f95SAllen Yan 91594228f95SAllen Yan if (pdev->id >= MVEBU_NR_UARTS) { 91694228f95SAllen Yan dev_err(&pdev->dev, "cannot have more than %d UART ports\n", 91794228f95SAllen Yan MVEBU_NR_UARTS); 91894228f95SAllen Yan return -EINVAL; 91994228f95SAllen Yan } 92094228f95SAllen Yan 92194228f95SAllen Yan port = &mvebu_uart_ports[pdev->id]; 92230530791SWilson Ding 92330530791SWilson Ding spin_lock_init(&port->lock); 92430530791SWilson Ding 92530530791SWilson Ding port->dev = &pdev->dev; 92630530791SWilson Ding port->type = PORT_MVEBU; 92730530791SWilson Ding port->ops = &mvebu_uart_ops; 92830530791SWilson Ding port->regshift = 0; 92930530791SWilson Ding 93030530791SWilson Ding port->fifosize = 32; 93130530791SWilson Ding port->iotype = UPIO_MEM32; 93230530791SWilson Ding port->flags = UPF_FIXED_PORT; 93394228f95SAllen Yan port->line = pdev->id; 93430530791SWilson Ding 93595f78768SMiquel Raynal /* 93695f78768SMiquel Raynal * IRQ number is not stored in this structure because we may have two of 93795f78768SMiquel Raynal * them per port (RX and TX). Instead, use the driver UART structure 93895f78768SMiquel Raynal * array so called ->irq[]. 93995f78768SMiquel Raynal */ 94095f78768SMiquel Raynal port->irq = 0; 94130530791SWilson Ding port->irqflags = 0; 94230530791SWilson Ding port->mapbase = reg->start; 94330530791SWilson Ding 94430530791SWilson Ding port->membase = devm_ioremap_resource(&pdev->dev, reg); 94530530791SWilson Ding if (IS_ERR(port->membase)) 9464a3e2084Stangbin return PTR_ERR(port->membase); 94730530791SWilson Ding 9485218d769SMiquel Raynal mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart), 94930530791SWilson Ding GFP_KERNEL); 9505218d769SMiquel Raynal if (!mvuart) 95130530791SWilson Ding return -ENOMEM; 95230530791SWilson Ding 95368a0db1dSAllen Yan /* Get controller data depending on the compatible string */ 9545218d769SMiquel Raynal mvuart->data = (struct mvebu_uart_driver_data *)match->data; 9555218d769SMiquel Raynal mvuart->port = port; 95630530791SWilson Ding 9575218d769SMiquel Raynal port->private_data = mvuart; 9585218d769SMiquel Raynal platform_set_drvdata(pdev, mvuart); 95930530791SWilson Ding 96068a0db1dSAllen Yan /* Get fixed clock frequency */ 96168a0db1dSAllen Yan mvuart->clk = devm_clk_get(&pdev->dev, NULL); 96268a0db1dSAllen Yan if (IS_ERR(mvuart->clk)) { 96368a0db1dSAllen Yan if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER) 96468a0db1dSAllen Yan return PTR_ERR(mvuart->clk); 96568a0db1dSAllen Yan 96668a0db1dSAllen Yan if (IS_EXTENDED(port)) { 96768a0db1dSAllen Yan dev_err(&pdev->dev, "unable to get UART clock\n"); 96868a0db1dSAllen Yan return PTR_ERR(mvuart->clk); 96968a0db1dSAllen Yan } 97068a0db1dSAllen Yan } else { 97168a0db1dSAllen Yan if (!clk_prepare_enable(mvuart->clk)) 97268a0db1dSAllen Yan port->uartclk = clk_get_rate(mvuart->clk); 97368a0db1dSAllen Yan } 97468a0db1dSAllen Yan 97595f78768SMiquel Raynal /* Manage interrupts */ 97695f78768SMiquel Raynal if (platform_irq_count(pdev) == 1) { 97795f78768SMiquel Raynal /* Old bindings: no name on the single unamed UART0 IRQ */ 97895f78768SMiquel Raynal irq = platform_get_irq(pdev, 0); 9791df21786SStephen Boyd if (irq < 0) 98095f78768SMiquel Raynal return irq; 98195f78768SMiquel Raynal 98295f78768SMiquel Raynal mvuart->irq[UART_IRQ_SUM] = irq; 98395f78768SMiquel Raynal } else { 98495f78768SMiquel Raynal /* 98595f78768SMiquel Raynal * New bindings: named interrupts (RX, TX) for both UARTS, 98695f78768SMiquel Raynal * only make use of uart-rx and uart-tx interrupts, do not use 98795f78768SMiquel Raynal * uart-sum of UART0 port. 98895f78768SMiquel Raynal */ 98995f78768SMiquel Raynal irq = platform_get_irq_byname(pdev, "uart-rx"); 9901df21786SStephen Boyd if (irq < 0) 99195f78768SMiquel Raynal return irq; 99295f78768SMiquel Raynal 99395f78768SMiquel Raynal mvuart->irq[UART_RX_IRQ] = irq; 99495f78768SMiquel Raynal 99595f78768SMiquel Raynal irq = platform_get_irq_byname(pdev, "uart-tx"); 9961df21786SStephen Boyd if (irq < 0) 99795f78768SMiquel Raynal return irq; 99895f78768SMiquel Raynal 99995f78768SMiquel Raynal mvuart->irq[UART_TX_IRQ] = irq; 100095f78768SMiquel Raynal } 100195f78768SMiquel Raynal 10029c3d3ee1SAllen Yan /* UART Soft Reset*/ 10039c3d3ee1SAllen Yan writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port)); 10049c3d3ee1SAllen Yan udelay(1); 10059c3d3ee1SAllen Yan writel(0, port->membase + UART_CTRL(port)); 10069c3d3ee1SAllen Yan 1007b6353702SQinglang Miao return uart_add_one_port(&mvebu_uart_driver, port); 100830530791SWilson Ding } 100930530791SWilson Ding 10105218d769SMiquel Raynal static struct mvebu_uart_driver_data uart_std_driver_data = { 10115218d769SMiquel Raynal .is_ext = false, 10125218d769SMiquel Raynal .regs.rbr = UART_STD_RBR, 10135218d769SMiquel Raynal .regs.tsh = UART_STD_TSH, 10145218d769SMiquel Raynal .regs.ctrl = UART_STD_CTRL1, 10155218d769SMiquel Raynal .regs.intr = UART_STD_CTRL2, 10165218d769SMiquel Raynal .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT, 10175218d769SMiquel Raynal .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT, 10185218d769SMiquel Raynal .flags.stat_tx_rdy = STAT_STD_TX_RDY, 10195218d769SMiquel Raynal .flags.stat_rx_rdy = STAT_STD_RX_RDY, 10205218d769SMiquel Raynal }; 10215218d769SMiquel Raynal 102253501e02SMiquel Raynal static struct mvebu_uart_driver_data uart_ext_driver_data = { 102353501e02SMiquel Raynal .is_ext = true, 102453501e02SMiquel Raynal .regs.rbr = UART_EXT_RBR, 102553501e02SMiquel Raynal .regs.tsh = UART_EXT_TSH, 102653501e02SMiquel Raynal .regs.ctrl = UART_EXT_CTRL1, 102753501e02SMiquel Raynal .regs.intr = UART_EXT_CTRL2, 102853501e02SMiquel Raynal .flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT, 102953501e02SMiquel Raynal .flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT, 103053501e02SMiquel Raynal .flags.stat_tx_rdy = STAT_EXT_TX_RDY, 103153501e02SMiquel Raynal .flags.stat_rx_rdy = STAT_EXT_RX_RDY, 103253501e02SMiquel Raynal }; 103353501e02SMiquel Raynal 103430530791SWilson Ding /* Match table for of_platform binding */ 103530530791SWilson Ding static const struct of_device_id mvebu_uart_of_match[] = { 10365218d769SMiquel Raynal { 10375218d769SMiquel Raynal .compatible = "marvell,armada-3700-uart", 10385218d769SMiquel Raynal .data = (void *)&uart_std_driver_data, 10395218d769SMiquel Raynal }, 104053501e02SMiquel Raynal { 104153501e02SMiquel Raynal .compatible = "marvell,armada-3700-uart-ext", 104253501e02SMiquel Raynal .data = (void *)&uart_ext_driver_data, 104353501e02SMiquel Raynal }, 104430530791SWilson Ding {} 104530530791SWilson Ding }; 104630530791SWilson Ding 104730530791SWilson Ding static struct platform_driver mvebu_uart_platform_driver = { 104830530791SWilson Ding .probe = mvebu_uart_probe, 104930530791SWilson Ding .driver = { 105030530791SWilson Ding .name = "mvebu-uart", 105130530791SWilson Ding .of_match_table = of_match_ptr(mvebu_uart_of_match), 105289ebc274SPaul Gortmaker .suppress_bind_attrs = true, 1053394e8351SMiquel Raynal #if defined(CONFIG_PM) 1054394e8351SMiquel Raynal .pm = &mvebu_uart_pm_ops, 1055394e8351SMiquel Raynal #endif /* CONFIG_PM */ 105630530791SWilson Ding }, 105730530791SWilson Ding }; 105830530791SWilson Ding 1059b7e2b536SPali Rohár /* This code is based on clk-fixed-factor.c driver and modified. */ 1060b7e2b536SPali Rohár 1061b7e2b536SPali Rohár struct mvebu_uart_clock { 1062b7e2b536SPali Rohár struct clk_hw clk_hw; 1063b7e2b536SPali Rohár int clock_idx; 1064b7e2b536SPali Rohár u32 pm_context_reg1; 1065b7e2b536SPali Rohár u32 pm_context_reg2; 1066b7e2b536SPali Rohár }; 1067b7e2b536SPali Rohár 1068b7e2b536SPali Rohár struct mvebu_uart_clock_base { 1069b7e2b536SPali Rohár struct mvebu_uart_clock clocks[2]; 1070b7e2b536SPali Rohár unsigned int parent_rates[5]; 1071b7e2b536SPali Rohár int parent_idx; 1072b7e2b536SPali Rohár unsigned int div; 1073b7e2b536SPali Rohár void __iomem *reg1; 1074b7e2b536SPali Rohár void __iomem *reg2; 1075b7e2b536SPali Rohár bool configured; 1076b7e2b536SPali Rohár }; 1077b7e2b536SPali Rohár 1078b7e2b536SPali Rohár #define PARENT_CLOCK_XTAL 4 1079b7e2b536SPali Rohár 1080b7e2b536SPali Rohár #define to_uart_clock(hw) container_of(hw, struct mvebu_uart_clock, clk_hw) 1081b7e2b536SPali Rohár #define to_uart_clock_base(uart_clock) container_of(uart_clock, \ 1082b7e2b536SPali Rohár struct mvebu_uart_clock_base, clocks[uart_clock->clock_idx]) 1083b7e2b536SPali Rohár 1084b7e2b536SPali Rohár static int mvebu_uart_clock_prepare(struct clk_hw *hw) 1085b7e2b536SPali Rohár { 1086b7e2b536SPali Rohár struct mvebu_uart_clock *uart_clock = to_uart_clock(hw); 1087b7e2b536SPali Rohár struct mvebu_uart_clock_base *uart_clock_base = 1088b7e2b536SPali Rohár to_uart_clock_base(uart_clock); 1089b7e2b536SPali Rohár unsigned int prev_clock_idx, prev_clock_rate, prev_d1d2; 1090b7e2b536SPali Rohár unsigned int parent_clock_idx, parent_clock_rate; 1091b7e2b536SPali Rohár unsigned long flags; 1092b7e2b536SPali Rohár unsigned int d1, d2; 1093b7e2b536SPali Rohár u64 divisor; 1094b7e2b536SPali Rohár u32 val; 1095b7e2b536SPali Rohár 1096b7e2b536SPali Rohár /* 1097b7e2b536SPali Rohár * This function just reconfigures UART Clock Control register (located 1098b7e2b536SPali Rohár * in UART1 address space which controls both UART1 and UART2) to 1099b7e2b536SPali Rohár * selected UART base clock and recalculates current UART1/UART2 1100b7e2b536SPali Rohár * divisors in their address spaces, so that final baudrate will not be 1101b7e2b536SPali Rohár * changed by switching UART parent clock. This is required for 1102b7e2b536SPali Rohár * otherwise kernel's boot log stops working - we need to ensure that 1103b7e2b536SPali Rohár * UART baudrate does not change during this setup. It is a one time 1104b7e2b536SPali Rohár * operation, it will execute only once and set `configured` to true, 1105b7e2b536SPali Rohár * and be skipped on subsequent calls. Because this UART Clock Control 1106b7e2b536SPali Rohár * register (UART_BRDV) is shared between UART1 baudrate function, 1107b7e2b536SPali Rohár * UART1 clock selector and UART2 clock selector, every access to 1108b7e2b536SPali Rohár * UART_BRDV (reg1) needs to be protected by a lock. 1109b7e2b536SPali Rohár */ 1110b7e2b536SPali Rohár 1111b7e2b536SPali Rohár spin_lock_irqsave(&mvebu_uart_lock, flags); 1112b7e2b536SPali Rohár 1113b7e2b536SPali Rohár if (uart_clock_base->configured) { 1114b7e2b536SPali Rohár spin_unlock_irqrestore(&mvebu_uart_lock, flags); 1115b7e2b536SPali Rohár return 0; 1116b7e2b536SPali Rohár } 1117b7e2b536SPali Rohár 1118b7e2b536SPali Rohár parent_clock_idx = uart_clock_base->parent_idx; 1119b7e2b536SPali Rohár parent_clock_rate = uart_clock_base->parent_rates[parent_clock_idx]; 1120b7e2b536SPali Rohár 1121b7e2b536SPali Rohár val = readl(uart_clock_base->reg1); 1122b7e2b536SPali Rohár 1123b7e2b536SPali Rohár if (uart_clock_base->div > CLK_TBG_DIV1_MAX) { 1124b7e2b536SPali Rohár d1 = CLK_TBG_DIV1_MAX; 1125b7e2b536SPali Rohár d2 = uart_clock_base->div / CLK_TBG_DIV1_MAX; 1126b7e2b536SPali Rohár } else { 1127b7e2b536SPali Rohár d1 = uart_clock_base->div; 1128b7e2b536SPali Rohár d2 = 1; 1129b7e2b536SPali Rohár } 1130b7e2b536SPali Rohár 1131b7e2b536SPali Rohár if (val & CLK_NO_XTAL) { 1132b7e2b536SPali Rohár prev_clock_idx = (val >> CLK_TBG_SEL_SHIFT) & CLK_TBG_SEL_MASK; 1133b7e2b536SPali Rohár prev_d1d2 = ((val >> CLK_TBG_DIV1_SHIFT) & CLK_TBG_DIV1_MASK) * 1134b7e2b536SPali Rohár ((val >> CLK_TBG_DIV2_SHIFT) & CLK_TBG_DIV2_MASK); 1135b7e2b536SPali Rohár } else { 1136b7e2b536SPali Rohár prev_clock_idx = PARENT_CLOCK_XTAL; 1137b7e2b536SPali Rohár prev_d1d2 = 1; 1138b7e2b536SPali Rohár } 1139b7e2b536SPali Rohár 1140b7e2b536SPali Rohár /* Note that uart_clock_base->parent_rates[i] may not be available */ 1141b7e2b536SPali Rohár prev_clock_rate = uart_clock_base->parent_rates[prev_clock_idx]; 1142b7e2b536SPali Rohár 1143b7e2b536SPali Rohár /* Recalculate UART1 divisor so UART1 baudrate does not change */ 1144b7e2b536SPali Rohár if (prev_clock_rate) { 1145b7e2b536SPali Rohár divisor = DIV_U64_ROUND_CLOSEST((u64)(val & BRDV_BAUD_MASK) * 1146b7e2b536SPali Rohár parent_clock_rate * prev_d1d2, 1147b7e2b536SPali Rohár prev_clock_rate * d1 * d2); 1148b7e2b536SPali Rohár if (divisor < 1) 1149b7e2b536SPali Rohár divisor = 1; 1150b7e2b536SPali Rohár else if (divisor > BRDV_BAUD_MAX) 1151b7e2b536SPali Rohár divisor = BRDV_BAUD_MAX; 1152b7e2b536SPali Rohár val = (val & ~BRDV_BAUD_MASK) | divisor; 1153b7e2b536SPali Rohár } 1154b7e2b536SPali Rohár 1155b7e2b536SPali Rohár if (parent_clock_idx != PARENT_CLOCK_XTAL) { 1156b7e2b536SPali Rohár /* Do not use XTAL, select TBG clock and TBG d1 * d2 divisors */ 1157b7e2b536SPali Rohár val |= CLK_NO_XTAL; 1158b7e2b536SPali Rohár val &= ~(CLK_TBG_DIV1_MASK << CLK_TBG_DIV1_SHIFT); 1159b7e2b536SPali Rohár val |= d1 << CLK_TBG_DIV1_SHIFT; 1160b7e2b536SPali Rohár val &= ~(CLK_TBG_DIV2_MASK << CLK_TBG_DIV2_SHIFT); 1161b7e2b536SPali Rohár val |= d2 << CLK_TBG_DIV2_SHIFT; 1162b7e2b536SPali Rohár val &= ~(CLK_TBG_SEL_MASK << CLK_TBG_SEL_SHIFT); 1163b7e2b536SPali Rohár val |= parent_clock_idx << CLK_TBG_SEL_SHIFT; 1164b7e2b536SPali Rohár } else { 1165b7e2b536SPali Rohár /* Use XTAL, TBG bits are then ignored */ 1166b7e2b536SPali Rohár val &= ~CLK_NO_XTAL; 1167b7e2b536SPali Rohár } 1168b7e2b536SPali Rohár 1169b7e2b536SPali Rohár writel(val, uart_clock_base->reg1); 1170b7e2b536SPali Rohár 1171b7e2b536SPali Rohár /* Recalculate UART2 divisor so UART2 baudrate does not change */ 1172b7e2b536SPali Rohár if (prev_clock_rate) { 1173b7e2b536SPali Rohár val = readl(uart_clock_base->reg2); 1174b7e2b536SPali Rohár divisor = DIV_U64_ROUND_CLOSEST((u64)(val & BRDV_BAUD_MASK) * 1175b7e2b536SPali Rohár parent_clock_rate * prev_d1d2, 1176b7e2b536SPali Rohár prev_clock_rate * d1 * d2); 1177b7e2b536SPali Rohár if (divisor < 1) 1178b7e2b536SPali Rohár divisor = 1; 1179b7e2b536SPali Rohár else if (divisor > BRDV_BAUD_MAX) 1180b7e2b536SPali Rohár divisor = BRDV_BAUD_MAX; 1181b7e2b536SPali Rohár val = (val & ~BRDV_BAUD_MASK) | divisor; 1182b7e2b536SPali Rohár writel(val, uart_clock_base->reg2); 1183b7e2b536SPali Rohár } 1184b7e2b536SPali Rohár 1185b7e2b536SPali Rohár uart_clock_base->configured = true; 1186b7e2b536SPali Rohár 1187b7e2b536SPali Rohár spin_unlock_irqrestore(&mvebu_uart_lock, flags); 1188b7e2b536SPali Rohár 1189b7e2b536SPali Rohár return 0; 1190b7e2b536SPali Rohár } 1191b7e2b536SPali Rohár 1192b7e2b536SPali Rohár static int mvebu_uart_clock_enable(struct clk_hw *hw) 1193b7e2b536SPali Rohár { 1194b7e2b536SPali Rohár struct mvebu_uart_clock *uart_clock = to_uart_clock(hw); 1195b7e2b536SPali Rohár struct mvebu_uart_clock_base *uart_clock_base = 1196b7e2b536SPali Rohár to_uart_clock_base(uart_clock); 1197b7e2b536SPali Rohár unsigned long flags; 1198b7e2b536SPali Rohár u32 val; 1199b7e2b536SPali Rohár 1200b7e2b536SPali Rohár spin_lock_irqsave(&mvebu_uart_lock, flags); 1201b7e2b536SPali Rohár 1202b7e2b536SPali Rohár val = readl(uart_clock_base->reg1); 1203b7e2b536SPali Rohár 1204b7e2b536SPali Rohár if (uart_clock->clock_idx == 0) 1205b7e2b536SPali Rohár val &= ~UART1_CLK_DIS; 1206b7e2b536SPali Rohár else 1207b7e2b536SPali Rohár val &= ~UART2_CLK_DIS; 1208b7e2b536SPali Rohár 1209b7e2b536SPali Rohár writel(val, uart_clock_base->reg1); 1210b7e2b536SPali Rohár 1211b7e2b536SPali Rohár spin_unlock_irqrestore(&mvebu_uart_lock, flags); 1212b7e2b536SPali Rohár 1213b7e2b536SPali Rohár return 0; 1214b7e2b536SPali Rohár } 1215b7e2b536SPali Rohár 1216b7e2b536SPali Rohár static void mvebu_uart_clock_disable(struct clk_hw *hw) 1217b7e2b536SPali Rohár { 1218b7e2b536SPali Rohár struct mvebu_uart_clock *uart_clock = to_uart_clock(hw); 1219b7e2b536SPali Rohár struct mvebu_uart_clock_base *uart_clock_base = 1220b7e2b536SPali Rohár to_uart_clock_base(uart_clock); 1221b7e2b536SPali Rohár unsigned long flags; 1222b7e2b536SPali Rohár u32 val; 1223b7e2b536SPali Rohár 1224b7e2b536SPali Rohár spin_lock_irqsave(&mvebu_uart_lock, flags); 1225b7e2b536SPali Rohár 1226b7e2b536SPali Rohár val = readl(uart_clock_base->reg1); 1227b7e2b536SPali Rohár 1228b7e2b536SPali Rohár if (uart_clock->clock_idx == 0) 1229b7e2b536SPali Rohár val |= UART1_CLK_DIS; 1230b7e2b536SPali Rohár else 1231b7e2b536SPali Rohár val |= UART2_CLK_DIS; 1232b7e2b536SPali Rohár 1233b7e2b536SPali Rohár writel(val, uart_clock_base->reg1); 1234b7e2b536SPali Rohár 1235b7e2b536SPali Rohár spin_unlock_irqrestore(&mvebu_uart_lock, flags); 1236b7e2b536SPali Rohár } 1237b7e2b536SPali Rohár 1238b7e2b536SPali Rohár static int mvebu_uart_clock_is_enabled(struct clk_hw *hw) 1239b7e2b536SPali Rohár { 1240b7e2b536SPali Rohár struct mvebu_uart_clock *uart_clock = to_uart_clock(hw); 1241b7e2b536SPali Rohár struct mvebu_uart_clock_base *uart_clock_base = 1242b7e2b536SPali Rohár to_uart_clock_base(uart_clock); 1243b7e2b536SPali Rohár u32 val; 1244b7e2b536SPali Rohár 1245b7e2b536SPali Rohár val = readl(uart_clock_base->reg1); 1246b7e2b536SPali Rohár 1247b7e2b536SPali Rohár if (uart_clock->clock_idx == 0) 1248b7e2b536SPali Rohár return !(val & UART1_CLK_DIS); 1249b7e2b536SPali Rohár else 1250b7e2b536SPali Rohár return !(val & UART2_CLK_DIS); 1251b7e2b536SPali Rohár } 1252b7e2b536SPali Rohár 1253b7e2b536SPali Rohár static int mvebu_uart_clock_save_context(struct clk_hw *hw) 1254b7e2b536SPali Rohár { 1255b7e2b536SPali Rohár struct mvebu_uart_clock *uart_clock = to_uart_clock(hw); 1256b7e2b536SPali Rohár struct mvebu_uart_clock_base *uart_clock_base = 1257b7e2b536SPali Rohár to_uart_clock_base(uart_clock); 1258b7e2b536SPali Rohár unsigned long flags; 1259b7e2b536SPali Rohár 1260b7e2b536SPali Rohár spin_lock_irqsave(&mvebu_uart_lock, flags); 1261b7e2b536SPali Rohár uart_clock->pm_context_reg1 = readl(uart_clock_base->reg1); 1262b7e2b536SPali Rohár uart_clock->pm_context_reg2 = readl(uart_clock_base->reg2); 1263b7e2b536SPali Rohár spin_unlock_irqrestore(&mvebu_uart_lock, flags); 1264b7e2b536SPali Rohár 1265b7e2b536SPali Rohár return 0; 1266b7e2b536SPali Rohár } 1267b7e2b536SPali Rohár 1268b7e2b536SPali Rohár static void mvebu_uart_clock_restore_context(struct clk_hw *hw) 1269b7e2b536SPali Rohár { 1270b7e2b536SPali Rohár struct mvebu_uart_clock *uart_clock = to_uart_clock(hw); 1271b7e2b536SPali Rohár struct mvebu_uart_clock_base *uart_clock_base = 1272b7e2b536SPali Rohár to_uart_clock_base(uart_clock); 1273b7e2b536SPali Rohár unsigned long flags; 1274b7e2b536SPali Rohár 1275b7e2b536SPali Rohár spin_lock_irqsave(&mvebu_uart_lock, flags); 1276b7e2b536SPali Rohár writel(uart_clock->pm_context_reg1, uart_clock_base->reg1); 1277b7e2b536SPali Rohár writel(uart_clock->pm_context_reg2, uart_clock_base->reg2); 1278b7e2b536SPali Rohár spin_unlock_irqrestore(&mvebu_uart_lock, flags); 1279b7e2b536SPali Rohár } 1280b7e2b536SPali Rohár 1281b7e2b536SPali Rohár static unsigned long mvebu_uart_clock_recalc_rate(struct clk_hw *hw, 1282b7e2b536SPali Rohár unsigned long parent_rate) 1283b7e2b536SPali Rohár { 1284b7e2b536SPali Rohár struct mvebu_uart_clock *uart_clock = to_uart_clock(hw); 1285b7e2b536SPali Rohár struct mvebu_uart_clock_base *uart_clock_base = 1286b7e2b536SPali Rohár to_uart_clock_base(uart_clock); 1287b7e2b536SPali Rohár 1288b7e2b536SPali Rohár return parent_rate / uart_clock_base->div; 1289b7e2b536SPali Rohár } 1290b7e2b536SPali Rohár 1291b7e2b536SPali Rohár static long mvebu_uart_clock_round_rate(struct clk_hw *hw, unsigned long rate, 1292b7e2b536SPali Rohár unsigned long *parent_rate) 1293b7e2b536SPali Rohár { 1294b7e2b536SPali Rohár struct mvebu_uart_clock *uart_clock = to_uart_clock(hw); 1295b7e2b536SPali Rohár struct mvebu_uart_clock_base *uart_clock_base = 1296b7e2b536SPali Rohár to_uart_clock_base(uart_clock); 1297b7e2b536SPali Rohár 1298b7e2b536SPali Rohár return *parent_rate / uart_clock_base->div; 1299b7e2b536SPali Rohár } 1300b7e2b536SPali Rohár 1301b7e2b536SPali Rohár static int mvebu_uart_clock_set_rate(struct clk_hw *hw, unsigned long rate, 1302b7e2b536SPali Rohár unsigned long parent_rate) 1303b7e2b536SPali Rohár { 1304b7e2b536SPali Rohár /* 1305b7e2b536SPali Rohár * We must report success but we can do so unconditionally because 1306b7e2b536SPali Rohár * mvebu_uart_clock_round_rate returns values that ensure this call is a 1307b7e2b536SPali Rohár * nop. 1308b7e2b536SPali Rohár */ 1309b7e2b536SPali Rohár 1310b7e2b536SPali Rohár return 0; 1311b7e2b536SPali Rohár } 1312b7e2b536SPali Rohár 1313b7e2b536SPali Rohár static const struct clk_ops mvebu_uart_clock_ops = { 1314b7e2b536SPali Rohár .prepare = mvebu_uart_clock_prepare, 1315b7e2b536SPali Rohár .enable = mvebu_uart_clock_enable, 1316b7e2b536SPali Rohár .disable = mvebu_uart_clock_disable, 1317b7e2b536SPali Rohár .is_enabled = mvebu_uart_clock_is_enabled, 1318b7e2b536SPali Rohár .save_context = mvebu_uart_clock_save_context, 1319b7e2b536SPali Rohár .restore_context = mvebu_uart_clock_restore_context, 1320b7e2b536SPali Rohár .round_rate = mvebu_uart_clock_round_rate, 1321b7e2b536SPali Rohár .set_rate = mvebu_uart_clock_set_rate, 1322b7e2b536SPali Rohár .recalc_rate = mvebu_uart_clock_recalc_rate, 1323b7e2b536SPali Rohár }; 1324b7e2b536SPali Rohár 1325b7e2b536SPali Rohár static int mvebu_uart_clock_register(struct device *dev, 1326b7e2b536SPali Rohár struct mvebu_uart_clock *uart_clock, 1327b7e2b536SPali Rohár const char *name, 1328b7e2b536SPali Rohár const char *parent_name) 1329b7e2b536SPali Rohár { 1330b7e2b536SPali Rohár struct clk_init_data init = { }; 1331b7e2b536SPali Rohár 1332b7e2b536SPali Rohár uart_clock->clk_hw.init = &init; 1333b7e2b536SPali Rohár 1334b7e2b536SPali Rohár init.name = name; 1335b7e2b536SPali Rohár init.ops = &mvebu_uart_clock_ops; 1336b7e2b536SPali Rohár init.flags = 0; 1337b7e2b536SPali Rohár init.num_parents = 1; 1338b7e2b536SPali Rohár init.parent_names = &parent_name; 1339b7e2b536SPali Rohár 1340b7e2b536SPali Rohár return devm_clk_hw_register(dev, &uart_clock->clk_hw); 1341b7e2b536SPali Rohár } 1342b7e2b536SPali Rohár 1343b7e2b536SPali Rohár static int mvebu_uart_clock_probe(struct platform_device *pdev) 1344b7e2b536SPali Rohár { 1345b7e2b536SPali Rohár static const char *const uart_clk_names[] = { "uart_1", "uart_2" }; 1346b7e2b536SPali Rohár static const char *const parent_clk_names[] = { "TBG-A-P", "TBG-B-P", 1347b7e2b536SPali Rohár "TBG-A-S", "TBG-B-S", 1348b7e2b536SPali Rohár "xtal" }; 1349b7e2b536SPali Rohár struct clk *parent_clks[ARRAY_SIZE(parent_clk_names)]; 1350b7e2b536SPali Rohár struct mvebu_uart_clock_base *uart_clock_base; 1351b7e2b536SPali Rohár struct clk_hw_onecell_data *hw_clk_data; 1352b7e2b536SPali Rohár struct device *dev = &pdev->dev; 1353b7e2b536SPali Rohár int i, parent_clk_idx, ret; 1354b7e2b536SPali Rohár unsigned long div, rate; 1355b7e2b536SPali Rohár struct resource *res; 1356b7e2b536SPali Rohár unsigned int d1, d2; 1357b7e2b536SPali Rohár 1358b7e2b536SPali Rohár BUILD_BUG_ON(ARRAY_SIZE(uart_clk_names) != 1359b7e2b536SPali Rohár ARRAY_SIZE(uart_clock_base->clocks)); 1360b7e2b536SPali Rohár BUILD_BUG_ON(ARRAY_SIZE(parent_clk_names) != 1361b7e2b536SPali Rohár ARRAY_SIZE(uart_clock_base->parent_rates)); 1362b7e2b536SPali Rohár 1363b7e2b536SPali Rohár uart_clock_base = devm_kzalloc(dev, 1364b7e2b536SPali Rohár sizeof(*uart_clock_base), 1365b7e2b536SPali Rohár GFP_KERNEL); 1366b7e2b536SPali Rohár if (!uart_clock_base) 1367b7e2b536SPali Rohár return -ENOMEM; 1368b7e2b536SPali Rohár 1369b7e2b536SPali Rohár res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1370b7e2b536SPali Rohár if (!res) { 1371b7e2b536SPali Rohár dev_err(dev, "Couldn't get first register\n"); 1372b7e2b536SPali Rohár return -ENOENT; 1373b7e2b536SPali Rohár } 1374b7e2b536SPali Rohár 1375b7e2b536SPali Rohár /* 1376b7e2b536SPali Rohár * UART Clock Control register (reg1 / UART_BRDV) is in the address 1377b7e2b536SPali Rohár * space of UART1 (standard UART variant), controls parent clock and 1378b7e2b536SPali Rohár * dividers for both UART1 and UART2 and is supplied via DT as the first 1379b7e2b536SPali Rohár * resource. Therefore use ioremap() rather than ioremap_resource() to 1380b7e2b536SPali Rohár * avoid conflicts with UART1 driver. Access to UART_BRDV is protected 1381b7e2b536SPali Rohár * by a lock shared between clock and UART driver. 1382b7e2b536SPali Rohár */ 1383b7e2b536SPali Rohár uart_clock_base->reg1 = devm_ioremap(dev, res->start, 1384b7e2b536SPali Rohár resource_size(res)); 138547b95e8aSWei Yongjun if (!uart_clock_base->reg1) 138647b95e8aSWei Yongjun return -ENOMEM; 1387b7e2b536SPali Rohár 1388b7e2b536SPali Rohár res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1389b7e2b536SPali Rohár if (!res) { 1390b7e2b536SPali Rohár dev_err(dev, "Couldn't get second register\n"); 1391b7e2b536SPali Rohár return -ENOENT; 1392b7e2b536SPali Rohár } 1393b7e2b536SPali Rohár 1394b7e2b536SPali Rohár /* 1395b7e2b536SPali Rohár * UART 2 Baud Rate Divisor register (reg2 / UART_BRDV) is in address 1396b7e2b536SPali Rohár * space of UART2 (extended UART variant), controls only one UART2 1397b7e2b536SPali Rohár * specific divider and is supplied via DT as second resource. 1398b7e2b536SPali Rohár * Therefore use ioremap() rather than ioremap_resource() to avoid 1399b7e2b536SPali Rohár * conflicts with UART2 driver. Access to UART_BRDV is protected by a 1400b7e2b536SPali Rohár * by lock shared between clock and UART driver. 1401b7e2b536SPali Rohár */ 1402b7e2b536SPali Rohár uart_clock_base->reg2 = devm_ioremap(dev, res->start, 1403b7e2b536SPali Rohár resource_size(res)); 140447b95e8aSWei Yongjun if (!uart_clock_base->reg2) 140547b95e8aSWei Yongjun return -ENOMEM; 1406b7e2b536SPali Rohár 1407b7e2b536SPali Rohár hw_clk_data = devm_kzalloc(dev, 1408b7e2b536SPali Rohár struct_size(hw_clk_data, hws, 1409b7e2b536SPali Rohár ARRAY_SIZE(uart_clk_names)), 1410b7e2b536SPali Rohár GFP_KERNEL); 1411b7e2b536SPali Rohár if (!hw_clk_data) 1412b7e2b536SPali Rohár return -ENOMEM; 1413b7e2b536SPali Rohár 1414b7e2b536SPali Rohár hw_clk_data->num = ARRAY_SIZE(uart_clk_names); 1415b7e2b536SPali Rohár for (i = 0; i < ARRAY_SIZE(uart_clk_names); i++) { 1416b7e2b536SPali Rohár hw_clk_data->hws[i] = &uart_clock_base->clocks[i].clk_hw; 1417b7e2b536SPali Rohár uart_clock_base->clocks[i].clock_idx = i; 1418b7e2b536SPali Rohár } 1419b7e2b536SPali Rohár 1420b7e2b536SPali Rohár parent_clk_idx = -1; 1421b7e2b536SPali Rohár 1422b7e2b536SPali Rohár for (i = 0; i < ARRAY_SIZE(parent_clk_names); i++) { 1423b7e2b536SPali Rohár parent_clks[i] = devm_clk_get(dev, parent_clk_names[i]); 1424b7e2b536SPali Rohár if (IS_ERR(parent_clks[i])) { 1425b7e2b536SPali Rohár if (PTR_ERR(parent_clks[i]) == -EPROBE_DEFER) 1426b7e2b536SPali Rohár return -EPROBE_DEFER; 1427b7e2b536SPali Rohár dev_warn(dev, "Couldn't get the parent clock %s: %ld\n", 1428b7e2b536SPali Rohár parent_clk_names[i], PTR_ERR(parent_clks[i])); 1429b7e2b536SPali Rohár continue; 1430b7e2b536SPali Rohár } 1431b7e2b536SPali Rohár 1432b7e2b536SPali Rohár ret = clk_prepare_enable(parent_clks[i]); 1433b7e2b536SPali Rohár if (ret) { 1434b7e2b536SPali Rohár dev_warn(dev, "Couldn't enable parent clock %s: %d\n", 1435b7e2b536SPali Rohár parent_clk_names[i], ret); 1436b7e2b536SPali Rohár continue; 1437b7e2b536SPali Rohár } 1438b7e2b536SPali Rohár rate = clk_get_rate(parent_clks[i]); 1439b7e2b536SPali Rohár uart_clock_base->parent_rates[i] = rate; 1440b7e2b536SPali Rohár 1441b7e2b536SPali Rohár if (i != PARENT_CLOCK_XTAL) { 1442b7e2b536SPali Rohár /* 1443b7e2b536SPali Rohár * Calculate the smallest TBG d1 and d2 divisors that 1444b7e2b536SPali Rohár * still can provide 9600 baudrate. 1445b7e2b536SPali Rohár */ 1446694b7112SPali Rohár d1 = DIV_ROUND_UP(rate, 9600 * OSAMP_MAX_DIVISOR * 1447b7e2b536SPali Rohár BRDV_BAUD_MAX); 1448b7e2b536SPali Rohár if (d1 < 1) 1449b7e2b536SPali Rohár d1 = 1; 1450b7e2b536SPali Rohár else if (d1 > CLK_TBG_DIV1_MAX) 1451b7e2b536SPali Rohár d1 = CLK_TBG_DIV1_MAX; 1452b7e2b536SPali Rohár 1453694b7112SPali Rohár d2 = DIV_ROUND_UP(rate, 9600 * OSAMP_MAX_DIVISOR * 1454b7e2b536SPali Rohár BRDV_BAUD_MAX * d1); 1455b7e2b536SPali Rohár if (d2 < 1) 1456b7e2b536SPali Rohár d2 = 1; 1457b7e2b536SPali Rohár else if (d2 > CLK_TBG_DIV2_MAX) 1458b7e2b536SPali Rohár d2 = CLK_TBG_DIV2_MAX; 1459b7e2b536SPali Rohár } else { 1460b7e2b536SPali Rohár /* 1461b7e2b536SPali Rohár * When UART clock uses XTAL clock as a source then it 1462b7e2b536SPali Rohár * is not possible to use d1 and d2 divisors. 1463b7e2b536SPali Rohár */ 1464b7e2b536SPali Rohár d1 = d2 = 1; 1465b7e2b536SPali Rohár } 1466b7e2b536SPali Rohár 1467b7e2b536SPali Rohár /* Skip clock source which cannot provide 9600 baudrate */ 1468694b7112SPali Rohár if (rate > 9600 * OSAMP_MAX_DIVISOR * BRDV_BAUD_MAX * d1 * d2) 1469b7e2b536SPali Rohár continue; 1470b7e2b536SPali Rohár 1471b7e2b536SPali Rohár /* 1472b7e2b536SPali Rohár * Choose TBG clock source with the smallest divisors. Use XTAL 1473b7e2b536SPali Rohár * clock source only in case TBG is not available as XTAL cannot 1474b7e2b536SPali Rohár * be used for baudrates higher than 230400. 1475b7e2b536SPali Rohár */ 1476b7e2b536SPali Rohár if (parent_clk_idx == -1 || 1477b7e2b536SPali Rohár (i != PARENT_CLOCK_XTAL && div > d1 * d2)) { 1478b7e2b536SPali Rohár parent_clk_idx = i; 1479b7e2b536SPali Rohár div = d1 * d2; 1480b7e2b536SPali Rohár } 1481b7e2b536SPali Rohár } 1482b7e2b536SPali Rohár 1483b7e2b536SPali Rohár for (i = 0; i < ARRAY_SIZE(parent_clk_names); i++) { 1484b7e2b536SPali Rohár if (i == parent_clk_idx || IS_ERR(parent_clks[i])) 1485b7e2b536SPali Rohár continue; 1486b7e2b536SPali Rohár clk_disable_unprepare(parent_clks[i]); 1487b7e2b536SPali Rohár devm_clk_put(dev, parent_clks[i]); 1488b7e2b536SPali Rohár } 1489b7e2b536SPali Rohár 1490b7e2b536SPali Rohár if (parent_clk_idx == -1) { 1491b7e2b536SPali Rohár dev_err(dev, "No usable parent clock\n"); 1492b7e2b536SPali Rohár return -ENOENT; 1493b7e2b536SPali Rohár } 1494b7e2b536SPali Rohár 1495b7e2b536SPali Rohár uart_clock_base->parent_idx = parent_clk_idx; 1496b7e2b536SPali Rohár uart_clock_base->div = div; 1497b7e2b536SPali Rohár 1498b7e2b536SPali Rohár dev_notice(dev, "Using parent clock %s as base UART clock\n", 1499b7e2b536SPali Rohár __clk_get_name(parent_clks[parent_clk_idx])); 1500b7e2b536SPali Rohár 1501b7e2b536SPali Rohár for (i = 0; i < ARRAY_SIZE(uart_clk_names); i++) { 1502b7e2b536SPali Rohár ret = mvebu_uart_clock_register(dev, 1503b7e2b536SPali Rohár &uart_clock_base->clocks[i], 1504b7e2b536SPali Rohár uart_clk_names[i], 1505b7e2b536SPali Rohár __clk_get_name(parent_clks[parent_clk_idx])); 1506b7e2b536SPali Rohár if (ret) { 1507b7e2b536SPali Rohár dev_err(dev, "Can't register UART clock %d: %d\n", 1508b7e2b536SPali Rohár i, ret); 1509b7e2b536SPali Rohár return ret; 1510b7e2b536SPali Rohár } 1511b7e2b536SPali Rohár } 1512b7e2b536SPali Rohár 1513b7e2b536SPali Rohár return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, 1514b7e2b536SPali Rohár hw_clk_data); 1515b7e2b536SPali Rohár } 1516b7e2b536SPali Rohár 1517b7e2b536SPali Rohár static const struct of_device_id mvebu_uart_clock_of_match[] = { 1518b7e2b536SPali Rohár { .compatible = "marvell,armada-3700-uart-clock", }, 1519b7e2b536SPali Rohár { } 1520b7e2b536SPali Rohár }; 1521b7e2b536SPali Rohár 1522b7e2b536SPali Rohár static struct platform_driver mvebu_uart_clock_platform_driver = { 1523b7e2b536SPali Rohár .probe = mvebu_uart_clock_probe, 1524b7e2b536SPali Rohár .driver = { 1525b7e2b536SPali Rohár .name = "mvebu-uart-clock", 1526b7e2b536SPali Rohár .of_match_table = mvebu_uart_clock_of_match, 1527b7e2b536SPali Rohár }, 1528b7e2b536SPali Rohár }; 1529b7e2b536SPali Rohár 153030530791SWilson Ding static int __init mvebu_uart_init(void) 153130530791SWilson Ding { 153230530791SWilson Ding int ret; 153330530791SWilson Ding 153430530791SWilson Ding ret = uart_register_driver(&mvebu_uart_driver); 153530530791SWilson Ding if (ret) 153630530791SWilson Ding return ret; 153730530791SWilson Ding 1538b7e2b536SPali Rohár ret = platform_driver_register(&mvebu_uart_clock_platform_driver); 1539b7e2b536SPali Rohár if (ret) { 154030530791SWilson Ding uart_unregister_driver(&mvebu_uart_driver); 154130530791SWilson Ding return ret; 154230530791SWilson Ding } 1543b7e2b536SPali Rohár 1544b7e2b536SPali Rohár ret = platform_driver_register(&mvebu_uart_platform_driver); 1545b7e2b536SPali Rohár if (ret) { 1546b7e2b536SPali Rohár platform_driver_unregister(&mvebu_uart_clock_platform_driver); 1547b7e2b536SPali Rohár uart_unregister_driver(&mvebu_uart_driver); 1548b7e2b536SPali Rohár return ret; 1549b7e2b536SPali Rohár } 1550b7e2b536SPali Rohár 1551b7e2b536SPali Rohár return 0; 1552b7e2b536SPali Rohár } 155330530791SWilson Ding arch_initcall(mvebu_uart_init); 1554