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