Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 2 | /* |
| 3 | * *************************************************************************** |
Paul Gortmaker | 89ebc27 | 2016-03-13 19:48:52 -0400 | [diff] [blame] | 4 | * Marvell Armada-3700 Serial Driver |
| 5 | * Author: Wilson Ding <dingwei@marvell.com> |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 6 | * Copyright (C) 2015 Marvell International Ltd. |
| 7 | * *************************************************************************** |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/clk.h> |
| 11 | #include <linux/console.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/iopoll.h> |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 17 | #include <linux/of.h> |
| 18 | #include <linux/of_address.h> |
| 19 | #include <linux/of_device.h> |
| 20 | #include <linux/of_irq.h> |
| 21 | #include <linux/of_platform.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/serial.h> |
| 24 | #include <linux/serial_core.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/tty.h> |
| 27 | #include <linux/tty_flip.h> |
| 28 | |
| 29 | /* Register Map */ |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 30 | #define UART_STD_RBR 0x00 |
Miquel Raynal | 53501e0 | 2017-10-13 11:01:56 +0200 | [diff] [blame] | 31 | #define UART_EXT_RBR 0x18 |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 32 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 33 | #define UART_STD_TSH 0x04 |
Miquel Raynal | 53501e0 | 2017-10-13 11:01:56 +0200 | [diff] [blame] | 34 | #define UART_EXT_TSH 0x1C |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 35 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 36 | #define UART_STD_CTRL1 0x08 |
Miquel Raynal | 53501e0 | 2017-10-13 11:01:56 +0200 | [diff] [blame] | 37 | #define UART_EXT_CTRL1 0x04 |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 38 | #define CTRL_SOFT_RST BIT(31) |
| 39 | #define CTRL_TXFIFO_RST BIT(15) |
| 40 | #define CTRL_RXFIFO_RST BIT(14) |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 41 | #define CTRL_SND_BRK_SEQ BIT(11) |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 42 | #define CTRL_BRK_DET_INT BIT(3) |
| 43 | #define CTRL_FRM_ERR_INT BIT(2) |
| 44 | #define CTRL_PAR_ERR_INT BIT(1) |
| 45 | #define CTRL_OVR_ERR_INT BIT(0) |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 46 | #define CTRL_BRK_INT (CTRL_BRK_DET_INT | CTRL_FRM_ERR_INT | \ |
| 47 | CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT) |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 48 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 49 | #define UART_STD_CTRL2 UART_STD_CTRL1 |
Miquel Raynal | 53501e0 | 2017-10-13 11:01:56 +0200 | [diff] [blame] | 50 | #define UART_EXT_CTRL2 0x20 |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 51 | #define CTRL_STD_TX_RDY_INT BIT(5) |
Miquel Raynal | 53501e0 | 2017-10-13 11:01:56 +0200 | [diff] [blame] | 52 | #define CTRL_EXT_TX_RDY_INT BIT(6) |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 53 | #define CTRL_STD_RX_RDY_INT BIT(4) |
Miquel Raynal | 53501e0 | 2017-10-13 11:01:56 +0200 | [diff] [blame] | 54 | #define CTRL_EXT_RX_RDY_INT BIT(5) |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 55 | |
| 56 | #define UART_STAT 0x0C |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 57 | #define STAT_TX_FIFO_EMP BIT(13) |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 58 | #define STAT_TX_FIFO_FUL BIT(11) |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 59 | #define STAT_TX_EMP BIT(6) |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 60 | #define STAT_STD_TX_RDY BIT(5) |
Miquel Raynal | 53501e0 | 2017-10-13 11:01:56 +0200 | [diff] [blame] | 61 | #define STAT_EXT_TX_RDY BIT(15) |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 62 | #define STAT_STD_RX_RDY BIT(4) |
Miquel Raynal | 53501e0 | 2017-10-13 11:01:56 +0200 | [diff] [blame] | 63 | #define STAT_EXT_RX_RDY BIT(14) |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 64 | #define STAT_BRK_DET BIT(3) |
| 65 | #define STAT_FRM_ERR BIT(2) |
| 66 | #define STAT_PAR_ERR BIT(1) |
| 67 | #define STAT_OVR_ERR BIT(0) |
Colin Ian King | 0ef5a6e | 2018-02-23 14:14:51 +0000 | [diff] [blame] | 68 | #define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR \ |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 69 | | STAT_PAR_ERR | STAT_OVR_ERR) |
| 70 | |
| 71 | #define UART_BRDV 0x10 |
Allen Yan | 68a0db1 | 2017-10-13 11:01:51 +0200 | [diff] [blame] | 72 | #define BRDV_BAUD_MASK 0x3FF |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 73 | |
Miquel Raynal | 394e835 | 2018-04-21 16:07:33 +0200 | [diff] [blame] | 74 | #define UART_OSAMP 0x14 |
| 75 | |
Miquel Raynal | 3a75e91 | 2017-10-13 11:01:55 +0200 | [diff] [blame] | 76 | #define MVEBU_NR_UARTS 2 |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 77 | |
| 78 | #define MVEBU_UART_TYPE "mvebu-uart" |
Yehuda Yitschak | 02c3333 | 2017-10-13 11:01:47 +0200 | [diff] [blame] | 79 | #define DRIVER_NAME "mvebu_serial" |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 80 | |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 81 | enum { |
| 82 | /* Either there is only one summed IRQ... */ |
| 83 | UART_IRQ_SUM = 0, |
| 84 | /* ...or there are two separate IRQ for RX and TX */ |
| 85 | UART_RX_IRQ = 0, |
| 86 | UART_TX_IRQ, |
| 87 | UART_IRQ_COUNT |
| 88 | }; |
| 89 | |
| 90 | /* Diverging register offsets */ |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 91 | struct uart_regs_layout { |
| 92 | unsigned int rbr; |
| 93 | unsigned int tsh; |
| 94 | unsigned int ctrl; |
| 95 | unsigned int intr; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 96 | }; |
| 97 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 98 | /* Diverging flags */ |
| 99 | struct uart_flags { |
| 100 | unsigned int ctrl_tx_rdy_int; |
| 101 | unsigned int ctrl_rx_rdy_int; |
| 102 | unsigned int stat_tx_rdy; |
| 103 | unsigned int stat_rx_rdy; |
| 104 | }; |
| 105 | |
| 106 | /* Driver data, a structure for each UART port */ |
| 107 | struct mvebu_uart_driver_data { |
| 108 | bool is_ext; |
| 109 | struct uart_regs_layout regs; |
| 110 | struct uart_flags flags; |
| 111 | }; |
| 112 | |
Miquel Raynal | 394e835 | 2018-04-21 16:07:33 +0200 | [diff] [blame] | 113 | /* Saved registers during suspend */ |
| 114 | struct mvebu_uart_pm_regs { |
| 115 | unsigned int rbr; |
| 116 | unsigned int tsh; |
| 117 | unsigned int ctrl; |
| 118 | unsigned int intr; |
| 119 | unsigned int stat; |
| 120 | unsigned int brdv; |
| 121 | unsigned int osamp; |
| 122 | }; |
| 123 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 124 | /* MVEBU UART driver structure */ |
| 125 | struct mvebu_uart { |
| 126 | struct uart_port *port; |
| 127 | struct clk *clk; |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 128 | int irq[UART_IRQ_COUNT]; |
| 129 | unsigned char __iomem *nb; |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 130 | struct mvebu_uart_driver_data *data; |
Miquel Raynal | 394e835 | 2018-04-21 16:07:33 +0200 | [diff] [blame] | 131 | #if defined(CONFIG_PM) |
| 132 | struct mvebu_uart_pm_regs pm_regs; |
| 133 | #endif /* CONFIG_PM */ |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | static struct mvebu_uart *to_mvuart(struct uart_port *port) |
| 137 | { |
| 138 | return (struct mvebu_uart *)port->private_data; |
| 139 | } |
| 140 | |
| 141 | #define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext) |
| 142 | |
| 143 | #define UART_RBR(port) (to_mvuart(port)->data->regs.rbr) |
| 144 | #define UART_TSH(port) (to_mvuart(port)->data->regs.tsh) |
| 145 | #define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl) |
| 146 | #define UART_INTR(port) (to_mvuart(port)->data->regs.intr) |
| 147 | |
| 148 | #define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int) |
| 149 | #define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int) |
| 150 | #define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy) |
| 151 | #define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy) |
| 152 | |
| 153 | static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS]; |
| 154 | |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 155 | /* Core UART Driver Operations */ |
| 156 | static unsigned int mvebu_uart_tx_empty(struct uart_port *port) |
| 157 | { |
| 158 | unsigned long flags; |
| 159 | unsigned int st; |
| 160 | |
| 161 | spin_lock_irqsave(&port->lock, flags); |
| 162 | st = readl(port->membase + UART_STAT); |
| 163 | spin_unlock_irqrestore(&port->lock, flags); |
| 164 | |
| 165 | return (st & STAT_TX_FIFO_EMP) ? TIOCSER_TEMT : 0; |
| 166 | } |
| 167 | |
| 168 | static unsigned int mvebu_uart_get_mctrl(struct uart_port *port) |
| 169 | { |
| 170 | return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR; |
| 171 | } |
| 172 | |
| 173 | static void mvebu_uart_set_mctrl(struct uart_port *port, |
| 174 | unsigned int mctrl) |
| 175 | { |
| 176 | /* |
| 177 | * Even if we do not support configuring the modem control lines, this |
| 178 | * function must be proided to the serial core |
| 179 | */ |
| 180 | } |
| 181 | |
| 182 | static void mvebu_uart_stop_tx(struct uart_port *port) |
| 183 | { |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 184 | unsigned int ctl = readl(port->membase + UART_INTR(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 185 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 186 | ctl &= ~CTRL_TX_RDY_INT(port); |
| 187 | writel(ctl, port->membase + UART_INTR(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void mvebu_uart_start_tx(struct uart_port *port) |
| 191 | { |
Allen Yan | 30434b0 | 2017-10-13 11:01:53 +0200 | [diff] [blame] | 192 | unsigned int ctl; |
| 193 | struct circ_buf *xmit = &port->state->xmit; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 194 | |
Allen Yan | 30434b0 | 2017-10-13 11:01:53 +0200 | [diff] [blame] | 195 | if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) { |
| 196 | writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port)); |
| 197 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 198 | port->icount.tx++; |
| 199 | } |
| 200 | |
| 201 | ctl = readl(port->membase + UART_INTR(port)); |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 202 | ctl |= CTRL_TX_RDY_INT(port); |
| 203 | writel(ctl, port->membase + UART_INTR(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void mvebu_uart_stop_rx(struct uart_port *port) |
| 207 | { |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 208 | unsigned int ctl; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 209 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 210 | ctl = readl(port->membase + UART_CTRL(port)); |
| 211 | ctl &= ~CTRL_BRK_INT; |
| 212 | writel(ctl, port->membase + UART_CTRL(port)); |
| 213 | |
| 214 | ctl = readl(port->membase + UART_INTR(port)); |
| 215 | ctl &= ~CTRL_RX_RDY_INT(port); |
| 216 | writel(ctl, port->membase + UART_INTR(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void mvebu_uart_break_ctl(struct uart_port *port, int brk) |
| 220 | { |
| 221 | unsigned int ctl; |
| 222 | unsigned long flags; |
| 223 | |
| 224 | spin_lock_irqsave(&port->lock, flags); |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 225 | ctl = readl(port->membase + UART_CTRL(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 226 | if (brk == -1) |
| 227 | ctl |= CTRL_SND_BRK_SEQ; |
| 228 | else |
| 229 | ctl &= ~CTRL_SND_BRK_SEQ; |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 230 | writel(ctl, port->membase + UART_CTRL(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 231 | spin_unlock_irqrestore(&port->lock, flags); |
| 232 | } |
| 233 | |
| 234 | static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status) |
| 235 | { |
| 236 | struct tty_port *tport = &port->state->port; |
| 237 | unsigned char ch = 0; |
| 238 | char flag = 0; |
| 239 | |
| 240 | do { |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 241 | if (status & STAT_RX_RDY(port)) { |
| 242 | ch = readl(port->membase + UART_RBR(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 243 | ch &= 0xff; |
| 244 | flag = TTY_NORMAL; |
| 245 | port->icount.rx++; |
| 246 | |
| 247 | if (status & STAT_PAR_ERR) |
| 248 | port->icount.parity++; |
| 249 | } |
| 250 | |
| 251 | if (status & STAT_BRK_DET) { |
| 252 | port->icount.brk++; |
| 253 | status &= ~(STAT_FRM_ERR | STAT_PAR_ERR); |
| 254 | if (uart_handle_break(port)) |
| 255 | goto ignore_char; |
| 256 | } |
| 257 | |
| 258 | if (status & STAT_OVR_ERR) |
| 259 | port->icount.overrun++; |
| 260 | |
| 261 | if (status & STAT_FRM_ERR) |
| 262 | port->icount.frame++; |
| 263 | |
| 264 | if (uart_handle_sysrq_char(port, ch)) |
| 265 | goto ignore_char; |
| 266 | |
| 267 | if (status & port->ignore_status_mask & STAT_PAR_ERR) |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 268 | status &= ~STAT_RX_RDY(port); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 269 | |
| 270 | status &= port->read_status_mask; |
| 271 | |
| 272 | if (status & STAT_PAR_ERR) |
| 273 | flag = TTY_PARITY; |
| 274 | |
| 275 | status &= ~port->ignore_status_mask; |
| 276 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 277 | if (status & STAT_RX_RDY(port)) |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 278 | tty_insert_flip_char(tport, ch, flag); |
| 279 | |
| 280 | if (status & STAT_BRK_DET) |
| 281 | tty_insert_flip_char(tport, 0, TTY_BREAK); |
| 282 | |
| 283 | if (status & STAT_FRM_ERR) |
| 284 | tty_insert_flip_char(tport, 0, TTY_FRAME); |
| 285 | |
| 286 | if (status & STAT_OVR_ERR) |
| 287 | tty_insert_flip_char(tport, 0, TTY_OVERRUN); |
| 288 | |
| 289 | ignore_char: |
| 290 | status = readl(port->membase + UART_STAT); |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 291 | } while (status & (STAT_RX_RDY(port) | STAT_BRK_DET)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 292 | |
| 293 | tty_flip_buffer_push(tport); |
| 294 | } |
| 295 | |
| 296 | static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status) |
| 297 | { |
| 298 | struct circ_buf *xmit = &port->state->xmit; |
| 299 | unsigned int count; |
| 300 | unsigned int st; |
| 301 | |
| 302 | if (port->x_char) { |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 303 | writel(port->x_char, port->membase + UART_TSH(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 304 | port->icount.tx++; |
| 305 | port->x_char = 0; |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
| 310 | mvebu_uart_stop_tx(port); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | for (count = 0; count < port->fifosize; count++) { |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 315 | writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 316 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 317 | port->icount.tx++; |
| 318 | |
| 319 | if (uart_circ_empty(xmit)) |
| 320 | break; |
| 321 | |
| 322 | st = readl(port->membase + UART_STAT); |
| 323 | if (st & STAT_TX_FIFO_FUL) |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 328 | uart_write_wakeup(port); |
| 329 | |
| 330 | if (uart_circ_empty(xmit)) |
| 331 | mvebu_uart_stop_tx(port); |
| 332 | } |
| 333 | |
| 334 | static irqreturn_t mvebu_uart_isr(int irq, void *dev_id) |
| 335 | { |
| 336 | struct uart_port *port = (struct uart_port *)dev_id; |
| 337 | unsigned int st = readl(port->membase + UART_STAT); |
| 338 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 339 | if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR | |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 340 | STAT_BRK_DET)) |
| 341 | mvebu_uart_rx_chars(port, st); |
| 342 | |
| 343 | if (st & STAT_TX_RDY(port)) |
| 344 | mvebu_uart_tx_chars(port, st); |
| 345 | |
| 346 | return IRQ_HANDLED; |
| 347 | } |
| 348 | |
| 349 | static irqreturn_t mvebu_uart_rx_isr(int irq, void *dev_id) |
| 350 | { |
| 351 | struct uart_port *port = (struct uart_port *)dev_id; |
| 352 | unsigned int st = readl(port->membase + UART_STAT); |
| 353 | |
| 354 | if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 355 | STAT_BRK_DET)) |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 356 | mvebu_uart_rx_chars(port, st); |
| 357 | |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 358 | return IRQ_HANDLED; |
| 359 | } |
| 360 | |
| 361 | static irqreturn_t mvebu_uart_tx_isr(int irq, void *dev_id) |
| 362 | { |
| 363 | struct uart_port *port = (struct uart_port *)dev_id; |
| 364 | unsigned int st = readl(port->membase + UART_STAT); |
| 365 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 366 | if (st & STAT_TX_RDY(port)) |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 367 | mvebu_uart_tx_chars(port, st); |
| 368 | |
| 369 | return IRQ_HANDLED; |
| 370 | } |
| 371 | |
| 372 | static int mvebu_uart_startup(struct uart_port *port) |
| 373 | { |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 374 | struct mvebu_uart *mvuart = to_mvuart(port); |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 375 | unsigned int ctl; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 376 | int ret; |
| 377 | |
| 378 | writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST, |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 379 | port->membase + UART_CTRL(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 380 | udelay(1); |
Allen Yan | 2ff23c4 | 2017-10-13 11:01:52 +0200 | [diff] [blame] | 381 | |
| 382 | /* Clear the error bits of state register before IRQ request */ |
| 383 | ret = readl(port->membase + UART_STAT); |
| 384 | ret |= STAT_BRK_ERR; |
| 385 | writel(ret, port->membase + UART_STAT); |
| 386 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 387 | writel(CTRL_BRK_INT, port->membase + UART_CTRL(port)); |
| 388 | |
| 389 | ctl = readl(port->membase + UART_INTR(port)); |
| 390 | ctl |= CTRL_RX_RDY_INT(port); |
| 391 | writel(ctl, port->membase + UART_INTR(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 392 | |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 393 | if (!mvuart->irq[UART_TX_IRQ]) { |
| 394 | /* Old bindings with just one interrupt (UART0 only) */ |
| 395 | ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM], |
| 396 | mvebu_uart_isr, port->irqflags, |
| 397 | dev_name(port->dev), port); |
| 398 | if (ret) { |
| 399 | dev_err(port->dev, "unable to request IRQ %d\n", |
| 400 | mvuart->irq[UART_IRQ_SUM]); |
| 401 | return ret; |
| 402 | } |
| 403 | } else { |
| 404 | /* New bindings with an IRQ for RX and TX (both UART) */ |
| 405 | ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ], |
| 406 | mvebu_uart_rx_isr, port->irqflags, |
| 407 | dev_name(port->dev), port); |
| 408 | if (ret) { |
| 409 | dev_err(port->dev, "unable to request IRQ %d\n", |
| 410 | mvuart->irq[UART_RX_IRQ]); |
| 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ], |
| 415 | mvebu_uart_tx_isr, port->irqflags, |
| 416 | dev_name(port->dev), |
| 417 | port); |
| 418 | if (ret) { |
| 419 | dev_err(port->dev, "unable to request IRQ %d\n", |
| 420 | mvuart->irq[UART_TX_IRQ]); |
| 421 | devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], |
| 422 | port); |
| 423 | return ret; |
| 424 | } |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static void mvebu_uart_shutdown(struct uart_port *port) |
| 431 | { |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 432 | struct mvebu_uart *mvuart = to_mvuart(port); |
| 433 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 434 | writel(0, port->membase + UART_INTR(port)); |
Thomas Petazzoni | c2c1659 | 2016-06-16 16:48:52 +0200 | [diff] [blame] | 435 | |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 436 | if (!mvuart->irq[UART_TX_IRQ]) { |
| 437 | devm_free_irq(port->dev, mvuart->irq[UART_IRQ_SUM], port); |
| 438 | } else { |
| 439 | devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], port); |
| 440 | devm_free_irq(port->dev, mvuart->irq[UART_TX_IRQ], port); |
| 441 | } |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Allen Yan | 68a0db1 | 2017-10-13 11:01:51 +0200 | [diff] [blame] | 444 | static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud) |
| 445 | { |
| 446 | struct mvebu_uart *mvuart = to_mvuart(port); |
| 447 | unsigned int baud_rate_div; |
| 448 | u32 brdv; |
| 449 | |
| 450 | if (IS_ERR(mvuart->clk)) |
| 451 | return -PTR_ERR(mvuart->clk); |
| 452 | |
| 453 | /* |
| 454 | * The UART clock is divided by the value of the divisor to generate |
| 455 | * UCLK_OUT clock, which is 16 times faster than the baudrate. |
| 456 | * This prescaler can achieve all standard baudrates until 230400. |
| 457 | * Higher baudrates could be achieved for the extended UART by using the |
| 458 | * programmable oversampling stack (also called fractional divisor). |
| 459 | */ |
| 460 | baud_rate_div = DIV_ROUND_UP(port->uartclk, baud * 16); |
| 461 | brdv = readl(port->membase + UART_BRDV); |
| 462 | brdv &= ~BRDV_BAUD_MASK; |
| 463 | brdv |= baud_rate_div; |
| 464 | writel(brdv, port->membase + UART_BRDV); |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 469 | static void mvebu_uart_set_termios(struct uart_port *port, |
| 470 | struct ktermios *termios, |
| 471 | struct ktermios *old) |
| 472 | { |
| 473 | unsigned long flags; |
| 474 | unsigned int baud; |
| 475 | |
| 476 | spin_lock_irqsave(&port->lock, flags); |
| 477 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 478 | port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR | |
| 479 | STAT_TX_RDY(port) | STAT_TX_FIFO_FUL; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 480 | |
| 481 | if (termios->c_iflag & INPCK) |
| 482 | port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR; |
| 483 | |
| 484 | port->ignore_status_mask = 0; |
| 485 | if (termios->c_iflag & IGNPAR) |
| 486 | port->ignore_status_mask |= |
| 487 | STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR; |
| 488 | |
| 489 | if ((termios->c_cflag & CREAD) == 0) |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 490 | port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 491 | |
Allen Yan | 68a0db1 | 2017-10-13 11:01:51 +0200 | [diff] [blame] | 492 | /* |
| 493 | * Maximum achievable frequency with simple baudrate divisor is 230400. |
| 494 | * Since the error per bit frame would be of more than 15%, achieving |
| 495 | * higher frequencies would require to implement the fractional divisor |
| 496 | * feature. |
| 497 | */ |
| 498 | baud = uart_get_baud_rate(port, termios, old, 0, 230400); |
| 499 | if (mvebu_uart_baud_rate_set(port, baud)) { |
| 500 | /* No clock available, baudrate cannot be changed */ |
| 501 | if (old) |
| 502 | baud = uart_get_baud_rate(port, old, NULL, 0, 230400); |
| 503 | } else { |
| 504 | tty_termios_encode_baud_rate(termios, baud, baud); |
| 505 | uart_update_timeout(port, termios->c_cflag, baud); |
| 506 | } |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 507 | |
Allen Yan | 68a0db1 | 2017-10-13 11:01:51 +0200 | [diff] [blame] | 508 | /* Only the following flag changes are supported */ |
| 509 | if (old) { |
| 510 | termios->c_iflag &= INPCK | IGNPAR; |
| 511 | termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR); |
| 512 | termios->c_cflag &= CREAD | CBAUD; |
| 513 | termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD); |
Jan Kiszka | e0bf2d498 | 2018-08-26 19:49:32 +0200 | [diff] [blame^] | 514 | termios->c_cflag |= CS8; |
Allen Yan | 68a0db1 | 2017-10-13 11:01:51 +0200 | [diff] [blame] | 515 | } |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 516 | |
| 517 | spin_unlock_irqrestore(&port->lock, flags); |
| 518 | } |
| 519 | |
| 520 | static const char *mvebu_uart_type(struct uart_port *port) |
| 521 | { |
| 522 | return MVEBU_UART_TYPE; |
| 523 | } |
| 524 | |
| 525 | static void mvebu_uart_release_port(struct uart_port *port) |
| 526 | { |
| 527 | /* Nothing to do here */ |
| 528 | } |
| 529 | |
| 530 | static int mvebu_uart_request_port(struct uart_port *port) |
| 531 | { |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | #ifdef CONFIG_CONSOLE_POLL |
| 536 | static int mvebu_uart_get_poll_char(struct uart_port *port) |
| 537 | { |
| 538 | unsigned int st = readl(port->membase + UART_STAT); |
| 539 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 540 | if (!(st & STAT_RX_RDY(port))) |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 541 | return NO_POLL_CHAR; |
| 542 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 543 | return readl(port->membase + UART_RBR(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c) |
| 547 | { |
| 548 | unsigned int st; |
| 549 | |
| 550 | for (;;) { |
| 551 | st = readl(port->membase + UART_STAT); |
| 552 | |
| 553 | if (!(st & STAT_TX_FIFO_FUL)) |
| 554 | break; |
| 555 | |
| 556 | udelay(1); |
| 557 | } |
| 558 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 559 | writel(c, port->membase + UART_TSH(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 560 | } |
| 561 | #endif |
| 562 | |
| 563 | static const struct uart_ops mvebu_uart_ops = { |
| 564 | .tx_empty = mvebu_uart_tx_empty, |
| 565 | .set_mctrl = mvebu_uart_set_mctrl, |
| 566 | .get_mctrl = mvebu_uart_get_mctrl, |
| 567 | .stop_tx = mvebu_uart_stop_tx, |
| 568 | .start_tx = mvebu_uart_start_tx, |
| 569 | .stop_rx = mvebu_uart_stop_rx, |
| 570 | .break_ctl = mvebu_uart_break_ctl, |
| 571 | .startup = mvebu_uart_startup, |
| 572 | .shutdown = mvebu_uart_shutdown, |
| 573 | .set_termios = mvebu_uart_set_termios, |
| 574 | .type = mvebu_uart_type, |
| 575 | .release_port = mvebu_uart_release_port, |
| 576 | .request_port = mvebu_uart_request_port, |
| 577 | #ifdef CONFIG_CONSOLE_POLL |
| 578 | .poll_get_char = mvebu_uart_get_poll_char, |
| 579 | .poll_put_char = mvebu_uart_put_poll_char, |
| 580 | #endif |
| 581 | }; |
| 582 | |
| 583 | /* Console Driver Operations */ |
| 584 | |
| 585 | #ifdef CONFIG_SERIAL_MVEBU_CONSOLE |
| 586 | /* Early Console */ |
| 587 | static void mvebu_uart_putc(struct uart_port *port, int c) |
| 588 | { |
| 589 | unsigned int st; |
| 590 | |
| 591 | for (;;) { |
| 592 | st = readl(port->membase + UART_STAT); |
| 593 | if (!(st & STAT_TX_FIFO_FUL)) |
| 594 | break; |
| 595 | } |
| 596 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 597 | /* At early stage, DT is not parsed yet, only use UART0 */ |
| 598 | writel(c, port->membase + UART_STD_TSH); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 599 | |
| 600 | for (;;) { |
| 601 | st = readl(port->membase + UART_STAT); |
| 602 | if (st & STAT_TX_FIFO_EMP) |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | static void mvebu_uart_putc_early_write(struct console *con, |
| 608 | const char *s, |
| 609 | unsigned n) |
| 610 | { |
| 611 | struct earlycon_device *dev = con->data; |
| 612 | |
| 613 | uart_console_write(&dev->port, s, n, mvebu_uart_putc); |
| 614 | } |
| 615 | |
| 616 | static int __init |
| 617 | mvebu_uart_early_console_setup(struct earlycon_device *device, |
| 618 | const char *opt) |
| 619 | { |
| 620 | if (!device->port.membase) |
| 621 | return -ENODEV; |
| 622 | |
| 623 | device->con->write = mvebu_uart_putc_early_write; |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup); |
| 629 | OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart", |
| 630 | mvebu_uart_early_console_setup); |
| 631 | |
| 632 | static void wait_for_xmitr(struct uart_port *port) |
| 633 | { |
| 634 | u32 val; |
| 635 | |
| 636 | readl_poll_timeout_atomic(port->membase + UART_STAT, val, |
Gabriel Matni | c685af1 | 2018-03-22 19:15:12 +0000 | [diff] [blame] | 637 | (val & STAT_TX_RDY(port)), 1, 10000); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static void mvebu_uart_console_putchar(struct uart_port *port, int ch) |
| 641 | { |
| 642 | wait_for_xmitr(port); |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 643 | writel(ch, port->membase + UART_TSH(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static void mvebu_uart_console_write(struct console *co, const char *s, |
| 647 | unsigned int count) |
| 648 | { |
| 649 | struct uart_port *port = &mvebu_uart_ports[co->index]; |
| 650 | unsigned long flags; |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 651 | unsigned int ier, intr, ctl; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 652 | int locked = 1; |
| 653 | |
| 654 | if (oops_in_progress) |
| 655 | locked = spin_trylock_irqsave(&port->lock, flags); |
| 656 | else |
| 657 | spin_lock_irqsave(&port->lock, flags); |
| 658 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 659 | ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT; |
| 660 | intr = readl(port->membase + UART_INTR(port)) & |
| 661 | (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port)); |
| 662 | writel(0, port->membase + UART_CTRL(port)); |
| 663 | writel(0, port->membase + UART_INTR(port)); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 664 | |
| 665 | uart_console_write(port, s, count, mvebu_uart_console_putchar); |
| 666 | |
| 667 | wait_for_xmitr(port); |
| 668 | |
| 669 | if (ier) |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 670 | writel(ier, port->membase + UART_CTRL(port)); |
| 671 | |
| 672 | if (intr) { |
| 673 | ctl = intr | readl(port->membase + UART_INTR(port)); |
| 674 | writel(ctl, port->membase + UART_INTR(port)); |
| 675 | } |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 676 | |
| 677 | if (locked) |
| 678 | spin_unlock_irqrestore(&port->lock, flags); |
| 679 | } |
| 680 | |
| 681 | static int mvebu_uart_console_setup(struct console *co, char *options) |
| 682 | { |
| 683 | struct uart_port *port; |
| 684 | int baud = 9600; |
| 685 | int bits = 8; |
| 686 | int parity = 'n'; |
| 687 | int flow = 'n'; |
| 688 | |
| 689 | if (co->index < 0 || co->index >= MVEBU_NR_UARTS) |
| 690 | return -EINVAL; |
| 691 | |
| 692 | port = &mvebu_uart_ports[co->index]; |
| 693 | |
| 694 | if (!port->mapbase || !port->membase) { |
| 695 | pr_debug("console on ttyMV%i not present\n", co->index); |
| 696 | return -ENODEV; |
| 697 | } |
| 698 | |
| 699 | if (options) |
| 700 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 701 | |
| 702 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 703 | } |
| 704 | |
| 705 | static struct uart_driver mvebu_uart_driver; |
| 706 | |
| 707 | static struct console mvebu_uart_console = { |
| 708 | .name = "ttyMV", |
| 709 | .write = mvebu_uart_console_write, |
| 710 | .device = uart_console_device, |
| 711 | .setup = mvebu_uart_console_setup, |
| 712 | .flags = CON_PRINTBUFFER, |
| 713 | .index = -1, |
| 714 | .data = &mvebu_uart_driver, |
| 715 | }; |
| 716 | |
| 717 | static int __init mvebu_uart_console_init(void) |
| 718 | { |
| 719 | register_console(&mvebu_uart_console); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | console_initcall(mvebu_uart_console_init); |
| 724 | |
| 725 | |
| 726 | #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */ |
| 727 | |
| 728 | static struct uart_driver mvebu_uart_driver = { |
| 729 | .owner = THIS_MODULE, |
Yehuda Yitschak | 02c3333 | 2017-10-13 11:01:47 +0200 | [diff] [blame] | 730 | .driver_name = DRIVER_NAME, |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 731 | .dev_name = "ttyMV", |
| 732 | .nr = MVEBU_NR_UARTS, |
| 733 | #ifdef CONFIG_SERIAL_MVEBU_CONSOLE |
| 734 | .cons = &mvebu_uart_console, |
| 735 | #endif |
| 736 | }; |
| 737 | |
Miquel Raynal | 394e835 | 2018-04-21 16:07:33 +0200 | [diff] [blame] | 738 | #if defined(CONFIG_PM) |
| 739 | static int mvebu_uart_suspend(struct device *dev) |
| 740 | { |
| 741 | struct mvebu_uart *mvuart = dev_get_drvdata(dev); |
| 742 | struct uart_port *port = mvuart->port; |
| 743 | |
| 744 | uart_suspend_port(&mvebu_uart_driver, port); |
| 745 | |
| 746 | mvuart->pm_regs.rbr = readl(port->membase + UART_RBR(port)); |
| 747 | mvuart->pm_regs.tsh = readl(port->membase + UART_TSH(port)); |
| 748 | mvuart->pm_regs.ctrl = readl(port->membase + UART_CTRL(port)); |
| 749 | mvuart->pm_regs.intr = readl(port->membase + UART_INTR(port)); |
| 750 | mvuart->pm_regs.stat = readl(port->membase + UART_STAT); |
| 751 | mvuart->pm_regs.brdv = readl(port->membase + UART_BRDV); |
| 752 | mvuart->pm_regs.osamp = readl(port->membase + UART_OSAMP); |
| 753 | |
| 754 | device_set_wakeup_enable(dev, true); |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static int mvebu_uart_resume(struct device *dev) |
| 760 | { |
| 761 | struct mvebu_uart *mvuart = dev_get_drvdata(dev); |
| 762 | struct uart_port *port = mvuart->port; |
| 763 | |
| 764 | writel(mvuart->pm_regs.rbr, port->membase + UART_RBR(port)); |
| 765 | writel(mvuart->pm_regs.tsh, port->membase + UART_TSH(port)); |
| 766 | writel(mvuart->pm_regs.ctrl, port->membase + UART_CTRL(port)); |
| 767 | writel(mvuart->pm_regs.intr, port->membase + UART_INTR(port)); |
| 768 | writel(mvuart->pm_regs.stat, port->membase + UART_STAT); |
| 769 | writel(mvuart->pm_regs.brdv, port->membase + UART_BRDV); |
| 770 | writel(mvuart->pm_regs.osamp, port->membase + UART_OSAMP); |
| 771 | |
| 772 | uart_resume_port(&mvebu_uart_driver, port); |
| 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | static const struct dev_pm_ops mvebu_uart_pm_ops = { |
| 778 | .suspend = mvebu_uart_suspend, |
| 779 | .resume = mvebu_uart_resume, |
| 780 | }; |
| 781 | #endif /* CONFIG_PM */ |
| 782 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 783 | static const struct of_device_id mvebu_uart_of_match[]; |
| 784 | |
Allen Yan | 94228f9 | 2017-10-13 11:01:48 +0200 | [diff] [blame] | 785 | /* Counter to keep track of each UART port id when not using CONFIG_OF */ |
| 786 | static int uart_num_counter; |
| 787 | |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 788 | static int mvebu_uart_probe(struct platform_device *pdev) |
| 789 | { |
| 790 | struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 791 | const struct of_device_id *match = of_match_device(mvebu_uart_of_match, |
| 792 | &pdev->dev); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 793 | struct uart_port *port; |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 794 | struct mvebu_uart *mvuart; |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 795 | int ret, id, irq; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 796 | |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 797 | if (!reg) { |
| 798 | dev_err(&pdev->dev, "no registers defined\n"); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 799 | return -EINVAL; |
| 800 | } |
| 801 | |
Allen Yan | 94228f9 | 2017-10-13 11:01:48 +0200 | [diff] [blame] | 802 | /* Assume that all UART ports have a DT alias or none has */ |
| 803 | id = of_alias_get_id(pdev->dev.of_node, "serial"); |
| 804 | if (!pdev->dev.of_node || id < 0) |
| 805 | pdev->id = uart_num_counter++; |
| 806 | else |
| 807 | pdev->id = id; |
| 808 | |
| 809 | if (pdev->id >= MVEBU_NR_UARTS) { |
| 810 | dev_err(&pdev->dev, "cannot have more than %d UART ports\n", |
| 811 | MVEBU_NR_UARTS); |
| 812 | return -EINVAL; |
| 813 | } |
| 814 | |
| 815 | port = &mvebu_uart_ports[pdev->id]; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 816 | |
| 817 | spin_lock_init(&port->lock); |
| 818 | |
| 819 | port->dev = &pdev->dev; |
| 820 | port->type = PORT_MVEBU; |
| 821 | port->ops = &mvebu_uart_ops; |
| 822 | port->regshift = 0; |
| 823 | |
| 824 | port->fifosize = 32; |
| 825 | port->iotype = UPIO_MEM32; |
| 826 | port->flags = UPF_FIXED_PORT; |
Allen Yan | 94228f9 | 2017-10-13 11:01:48 +0200 | [diff] [blame] | 827 | port->line = pdev->id; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 828 | |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 829 | /* |
| 830 | * IRQ number is not stored in this structure because we may have two of |
| 831 | * them per port (RX and TX). Instead, use the driver UART structure |
| 832 | * array so called ->irq[]. |
| 833 | */ |
| 834 | port->irq = 0; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 835 | port->irqflags = 0; |
| 836 | port->mapbase = reg->start; |
| 837 | |
| 838 | port->membase = devm_ioremap_resource(&pdev->dev, reg); |
| 839 | if (IS_ERR(port->membase)) |
| 840 | return -PTR_ERR(port->membase); |
| 841 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 842 | mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart), |
| 843 | GFP_KERNEL); |
| 844 | if (!mvuart) |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 845 | return -ENOMEM; |
| 846 | |
Allen Yan | 68a0db1 | 2017-10-13 11:01:51 +0200 | [diff] [blame] | 847 | /* Get controller data depending on the compatible string */ |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 848 | mvuart->data = (struct mvebu_uart_driver_data *)match->data; |
| 849 | mvuart->port = port; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 850 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 851 | port->private_data = mvuart; |
| 852 | platform_set_drvdata(pdev, mvuart); |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 853 | |
Allen Yan | 68a0db1 | 2017-10-13 11:01:51 +0200 | [diff] [blame] | 854 | /* Get fixed clock frequency */ |
| 855 | mvuart->clk = devm_clk_get(&pdev->dev, NULL); |
| 856 | if (IS_ERR(mvuart->clk)) { |
| 857 | if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER) |
| 858 | return PTR_ERR(mvuart->clk); |
| 859 | |
| 860 | if (IS_EXTENDED(port)) { |
| 861 | dev_err(&pdev->dev, "unable to get UART clock\n"); |
| 862 | return PTR_ERR(mvuart->clk); |
| 863 | } |
| 864 | } else { |
| 865 | if (!clk_prepare_enable(mvuart->clk)) |
| 866 | port->uartclk = clk_get_rate(mvuart->clk); |
| 867 | } |
| 868 | |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 869 | /* Manage interrupts */ |
Miquel Raynal | 95f7876 | 2017-10-13 11:01:54 +0200 | [diff] [blame] | 870 | if (platform_irq_count(pdev) == 1) { |
| 871 | /* Old bindings: no name on the single unamed UART0 IRQ */ |
| 872 | irq = platform_get_irq(pdev, 0); |
| 873 | if (irq < 0) { |
| 874 | dev_err(&pdev->dev, "unable to get UART IRQ\n"); |
| 875 | return irq; |
| 876 | } |
| 877 | |
| 878 | mvuart->irq[UART_IRQ_SUM] = irq; |
| 879 | } else { |
| 880 | /* |
| 881 | * New bindings: named interrupts (RX, TX) for both UARTS, |
| 882 | * only make use of uart-rx and uart-tx interrupts, do not use |
| 883 | * uart-sum of UART0 port. |
| 884 | */ |
| 885 | irq = platform_get_irq_byname(pdev, "uart-rx"); |
| 886 | if (irq < 0) { |
| 887 | dev_err(&pdev->dev, "unable to get 'uart-rx' IRQ\n"); |
| 888 | return irq; |
| 889 | } |
| 890 | |
| 891 | mvuart->irq[UART_RX_IRQ] = irq; |
| 892 | |
| 893 | irq = platform_get_irq_byname(pdev, "uart-tx"); |
| 894 | if (irq < 0) { |
| 895 | dev_err(&pdev->dev, "unable to get 'uart-tx' IRQ\n"); |
| 896 | return irq; |
| 897 | } |
| 898 | |
| 899 | mvuart->irq[UART_TX_IRQ] = irq; |
| 900 | } |
| 901 | |
Allen Yan | 9c3d3ee | 2017-10-13 11:01:50 +0200 | [diff] [blame] | 902 | /* UART Soft Reset*/ |
| 903 | writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port)); |
| 904 | udelay(1); |
| 905 | writel(0, port->membase + UART_CTRL(port)); |
| 906 | |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 907 | ret = uart_add_one_port(&mvebu_uart_driver, port); |
| 908 | if (ret) |
| 909 | return ret; |
| 910 | return 0; |
| 911 | } |
| 912 | |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 913 | static struct mvebu_uart_driver_data uart_std_driver_data = { |
| 914 | .is_ext = false, |
| 915 | .regs.rbr = UART_STD_RBR, |
| 916 | .regs.tsh = UART_STD_TSH, |
| 917 | .regs.ctrl = UART_STD_CTRL1, |
| 918 | .regs.intr = UART_STD_CTRL2, |
| 919 | .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT, |
| 920 | .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT, |
| 921 | .flags.stat_tx_rdy = STAT_STD_TX_RDY, |
| 922 | .flags.stat_rx_rdy = STAT_STD_RX_RDY, |
| 923 | }; |
| 924 | |
Miquel Raynal | 53501e0 | 2017-10-13 11:01:56 +0200 | [diff] [blame] | 925 | static struct mvebu_uart_driver_data uart_ext_driver_data = { |
| 926 | .is_ext = true, |
| 927 | .regs.rbr = UART_EXT_RBR, |
| 928 | .regs.tsh = UART_EXT_TSH, |
| 929 | .regs.ctrl = UART_EXT_CTRL1, |
| 930 | .regs.intr = UART_EXT_CTRL2, |
| 931 | .flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT, |
| 932 | .flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT, |
| 933 | .flags.stat_tx_rdy = STAT_EXT_TX_RDY, |
| 934 | .flags.stat_rx_rdy = STAT_EXT_RX_RDY, |
| 935 | }; |
| 936 | |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 937 | /* Match table for of_platform binding */ |
| 938 | static const struct of_device_id mvebu_uart_of_match[] = { |
Miquel Raynal | 5218d76 | 2017-10-13 11:01:49 +0200 | [diff] [blame] | 939 | { |
| 940 | .compatible = "marvell,armada-3700-uart", |
| 941 | .data = (void *)&uart_std_driver_data, |
| 942 | }, |
Miquel Raynal | 53501e0 | 2017-10-13 11:01:56 +0200 | [diff] [blame] | 943 | { |
| 944 | .compatible = "marvell,armada-3700-uart-ext", |
| 945 | .data = (void *)&uart_ext_driver_data, |
| 946 | }, |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 947 | {} |
| 948 | }; |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 949 | |
| 950 | static struct platform_driver mvebu_uart_platform_driver = { |
| 951 | .probe = mvebu_uart_probe, |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 952 | .driver = { |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 953 | .name = "mvebu-uart", |
| 954 | .of_match_table = of_match_ptr(mvebu_uart_of_match), |
Paul Gortmaker | 89ebc27 | 2016-03-13 19:48:52 -0400 | [diff] [blame] | 955 | .suppress_bind_attrs = true, |
Miquel Raynal | 394e835 | 2018-04-21 16:07:33 +0200 | [diff] [blame] | 956 | #if defined(CONFIG_PM) |
| 957 | .pm = &mvebu_uart_pm_ops, |
| 958 | #endif /* CONFIG_PM */ |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 959 | }, |
| 960 | }; |
| 961 | |
| 962 | static int __init mvebu_uart_init(void) |
| 963 | { |
| 964 | int ret; |
| 965 | |
| 966 | ret = uart_register_driver(&mvebu_uart_driver); |
| 967 | if (ret) |
| 968 | return ret; |
| 969 | |
| 970 | ret = platform_driver_register(&mvebu_uart_platform_driver); |
| 971 | if (ret) |
| 972 | uart_unregister_driver(&mvebu_uart_driver); |
| 973 | |
| 974 | return ret; |
| 975 | } |
Wilson Ding | 3053079 | 2016-02-16 19:14:53 +0100 | [diff] [blame] | 976 | arch_initcall(mvebu_uart_init); |