Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Atheros AR933X SoC built-in UART driver |
| 3 | * |
| 4 | * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org> |
| 5 | * |
| 6 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. |
| 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 version 2 as published |
| 10 | * by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/ioport.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/console.h> |
| 17 | #include <linux/sysrq.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/platform_device.h> |
Gabor Juhos | dd910d9 | 2013-08-29 08:44:01 +0200 | [diff] [blame] | 20 | #include <linux/of.h> |
| 21 | #include <linux/of_platform.h> |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 22 | #include <linux/tty.h> |
| 23 | #include <linux/tty_flip.h> |
| 24 | #include <linux/serial_core.h> |
| 25 | #include <linux/serial.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/irq.h> |
Gabor Juhos | 15ef17f | 2013-08-28 10:09:28 +0200 | [diff] [blame] | 29 | #include <linux/clk.h> |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 30 | |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 31 | #include <asm/div64.h> |
| 32 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 33 | #include <asm/mach-ath79/ar933x_uart.h> |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 34 | |
| 35 | #define DRIVER_NAME "ar933x-uart" |
| 36 | |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 37 | #define AR933X_UART_MAX_SCALE 0xff |
| 38 | #define AR933X_UART_MAX_STEP 0xffff |
| 39 | |
| 40 | #define AR933X_UART_MIN_BAUD 300 |
| 41 | #define AR933X_UART_MAX_BAUD 3000000 |
| 42 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 43 | #define AR933X_DUMMY_STATUS_RD 0x01 |
| 44 | |
| 45 | static struct uart_driver ar933x_uart_driver; |
| 46 | |
| 47 | struct ar933x_uart_port { |
| 48 | struct uart_port port; |
| 49 | unsigned int ier; /* shadow Interrupt Enable Register */ |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 50 | unsigned int min_baud; |
| 51 | unsigned int max_baud; |
Gabor Juhos | 15ef17f | 2013-08-28 10:09:28 +0200 | [diff] [blame] | 52 | struct clk *clk; |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up, |
| 56 | int offset) |
| 57 | { |
| 58 | return readl(up->port.membase + offset); |
| 59 | } |
| 60 | |
| 61 | static inline void ar933x_uart_write(struct ar933x_uart_port *up, |
| 62 | int offset, unsigned int value) |
| 63 | { |
| 64 | writel(value, up->port.membase + offset); |
| 65 | } |
| 66 | |
| 67 | static inline void ar933x_uart_rmw(struct ar933x_uart_port *up, |
| 68 | unsigned int offset, |
| 69 | unsigned int mask, |
| 70 | unsigned int val) |
| 71 | { |
| 72 | unsigned int t; |
| 73 | |
| 74 | t = ar933x_uart_read(up, offset); |
| 75 | t &= ~mask; |
| 76 | t |= val; |
| 77 | ar933x_uart_write(up, offset, t); |
| 78 | } |
| 79 | |
| 80 | static inline void ar933x_uart_rmw_set(struct ar933x_uart_port *up, |
| 81 | unsigned int offset, |
| 82 | unsigned int val) |
| 83 | { |
| 84 | ar933x_uart_rmw(up, offset, 0, val); |
| 85 | } |
| 86 | |
| 87 | static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port *up, |
| 88 | unsigned int offset, |
| 89 | unsigned int val) |
| 90 | { |
| 91 | ar933x_uart_rmw(up, offset, val, 0); |
| 92 | } |
| 93 | |
| 94 | static inline void ar933x_uart_start_tx_interrupt(struct ar933x_uart_port *up) |
| 95 | { |
| 96 | up->ier |= AR933X_UART_INT_TX_EMPTY; |
| 97 | ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); |
| 98 | } |
| 99 | |
| 100 | static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port *up) |
| 101 | { |
| 102 | up->ier &= ~AR933X_UART_INT_TX_EMPTY; |
| 103 | ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); |
| 104 | } |
| 105 | |
| 106 | static inline void ar933x_uart_putc(struct ar933x_uart_port *up, int ch) |
| 107 | { |
| 108 | unsigned int rdata; |
| 109 | |
| 110 | rdata = ch & AR933X_UART_DATA_TX_RX_MASK; |
| 111 | rdata |= AR933X_UART_DATA_TX_CSR; |
| 112 | ar933x_uart_write(up, AR933X_UART_DATA_REG, rdata); |
| 113 | } |
| 114 | |
| 115 | static unsigned int ar933x_uart_tx_empty(struct uart_port *port) |
| 116 | { |
Fabian Frederick | f9c1b28 | 2014-10-05 19:19:49 +0200 | [diff] [blame] | 117 | struct ar933x_uart_port *up = |
| 118 | container_of(port, struct ar933x_uart_port, port); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 119 | unsigned long flags; |
| 120 | unsigned int rdata; |
| 121 | |
| 122 | spin_lock_irqsave(&up->port.lock, flags); |
| 123 | rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG); |
| 124 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 125 | |
| 126 | return (rdata & AR933X_UART_DATA_TX_CSR) ? 0 : TIOCSER_TEMT; |
| 127 | } |
| 128 | |
| 129 | static unsigned int ar933x_uart_get_mctrl(struct uart_port *port) |
| 130 | { |
| 131 | return TIOCM_CAR; |
| 132 | } |
| 133 | |
| 134 | static void ar933x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | static void ar933x_uart_start_tx(struct uart_port *port) |
| 139 | { |
Fabian Frederick | f9c1b28 | 2014-10-05 19:19:49 +0200 | [diff] [blame] | 140 | struct ar933x_uart_port *up = |
| 141 | container_of(port, struct ar933x_uart_port, port); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 142 | |
| 143 | ar933x_uart_start_tx_interrupt(up); |
| 144 | } |
| 145 | |
| 146 | static void ar933x_uart_stop_tx(struct uart_port *port) |
| 147 | { |
Fabian Frederick | f9c1b28 | 2014-10-05 19:19:49 +0200 | [diff] [blame] | 148 | struct ar933x_uart_port *up = |
| 149 | container_of(port, struct ar933x_uart_port, port); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 150 | |
| 151 | ar933x_uart_stop_tx_interrupt(up); |
| 152 | } |
| 153 | |
| 154 | static void ar933x_uart_stop_rx(struct uart_port *port) |
| 155 | { |
Fabian Frederick | f9c1b28 | 2014-10-05 19:19:49 +0200 | [diff] [blame] | 156 | struct ar933x_uart_port *up = |
| 157 | container_of(port, struct ar933x_uart_port, port); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 158 | |
| 159 | up->ier &= ~AR933X_UART_INT_RX_VALID; |
| 160 | ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); |
| 161 | } |
| 162 | |
| 163 | static void ar933x_uart_break_ctl(struct uart_port *port, int break_state) |
| 164 | { |
Fabian Frederick | f9c1b28 | 2014-10-05 19:19:49 +0200 | [diff] [blame] | 165 | struct ar933x_uart_port *up = |
| 166 | container_of(port, struct ar933x_uart_port, port); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 167 | unsigned long flags; |
| 168 | |
| 169 | spin_lock_irqsave(&up->port.lock, flags); |
| 170 | if (break_state == -1) |
| 171 | ar933x_uart_rmw_set(up, AR933X_UART_CS_REG, |
| 172 | AR933X_UART_CS_TX_BREAK); |
| 173 | else |
| 174 | ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG, |
| 175 | AR933X_UART_CS_TX_BREAK); |
| 176 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 177 | } |
| 178 | |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 179 | /* |
| 180 | * baudrate = (clk / (scale + 1)) * (step * (1 / 2^17)) |
| 181 | */ |
| 182 | static unsigned long ar933x_uart_get_baud(unsigned int clk, |
| 183 | unsigned int scale, |
| 184 | unsigned int step) |
| 185 | { |
| 186 | u64 t; |
| 187 | u32 div; |
| 188 | |
| 189 | div = (2 << 16) * (scale + 1); |
| 190 | t = clk; |
| 191 | t *= step; |
| 192 | t += (div / 2); |
| 193 | do_div(t, div); |
| 194 | |
| 195 | return t; |
| 196 | } |
| 197 | |
| 198 | static void ar933x_uart_get_scale_step(unsigned int clk, |
| 199 | unsigned int baud, |
| 200 | unsigned int *scale, |
| 201 | unsigned int *step) |
| 202 | { |
| 203 | unsigned int tscale; |
| 204 | long min_diff; |
| 205 | |
| 206 | *scale = 0; |
| 207 | *step = 0; |
| 208 | |
| 209 | min_diff = baud; |
| 210 | for (tscale = 0; tscale < AR933X_UART_MAX_SCALE; tscale++) { |
| 211 | u64 tstep; |
| 212 | int diff; |
| 213 | |
| 214 | tstep = baud * (tscale + 1); |
| 215 | tstep *= (2 << 16); |
| 216 | do_div(tstep, clk); |
| 217 | |
| 218 | if (tstep > AR933X_UART_MAX_STEP) |
| 219 | break; |
| 220 | |
| 221 | diff = abs(ar933x_uart_get_baud(clk, tscale, tstep) - baud); |
| 222 | if (diff < min_diff) { |
| 223 | min_diff = diff; |
| 224 | *scale = tscale; |
| 225 | *step = tstep; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 230 | static void ar933x_uart_set_termios(struct uart_port *port, |
| 231 | struct ktermios *new, |
| 232 | struct ktermios *old) |
| 233 | { |
Fabian Frederick | f9c1b28 | 2014-10-05 19:19:49 +0200 | [diff] [blame] | 234 | struct ar933x_uart_port *up = |
| 235 | container_of(port, struct ar933x_uart_port, port); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 236 | unsigned int cs; |
| 237 | unsigned long flags; |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 238 | unsigned int baud, scale, step; |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 239 | |
| 240 | /* Only CS8 is supported */ |
| 241 | new->c_cflag &= ~CSIZE; |
| 242 | new->c_cflag |= CS8; |
| 243 | |
| 244 | /* Only one stop bit is supported */ |
| 245 | new->c_cflag &= ~CSTOPB; |
| 246 | |
| 247 | cs = 0; |
| 248 | if (new->c_cflag & PARENB) { |
| 249 | if (!(new->c_cflag & PARODD)) |
| 250 | cs |= AR933X_UART_CS_PARITY_EVEN; |
| 251 | else |
| 252 | cs |= AR933X_UART_CS_PARITY_ODD; |
| 253 | } else { |
| 254 | cs |= AR933X_UART_CS_PARITY_NONE; |
| 255 | } |
| 256 | |
| 257 | /* Mark/space parity is not supported */ |
| 258 | new->c_cflag &= ~CMSPAR; |
| 259 | |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 260 | baud = uart_get_baud_rate(port, new, old, up->min_baud, up->max_baud); |
| 261 | ar933x_uart_get_scale_step(port->uartclk, baud, &scale, &step); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * Ok, we're now changing the port state. Do it with |
| 265 | * interrupts disabled. |
| 266 | */ |
| 267 | spin_lock_irqsave(&up->port.lock, flags); |
| 268 | |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 269 | /* disable the UART */ |
| 270 | ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG, |
| 271 | AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S); |
| 272 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 273 | /* Update the per-port timeout. */ |
| 274 | uart_update_timeout(port, new->c_cflag, baud); |
| 275 | |
| 276 | up->port.ignore_status_mask = 0; |
| 277 | |
| 278 | /* ignore all characters if CREAD is not set */ |
| 279 | if ((new->c_cflag & CREAD) == 0) |
| 280 | up->port.ignore_status_mask |= AR933X_DUMMY_STATUS_RD; |
| 281 | |
| 282 | ar933x_uart_write(up, AR933X_UART_CLOCK_REG, |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 283 | scale << AR933X_UART_CLOCK_SCALE_S | step); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 284 | |
| 285 | /* setup configuration register */ |
| 286 | ar933x_uart_rmw(up, AR933X_UART_CS_REG, AR933X_UART_CS_PARITY_M, cs); |
| 287 | |
| 288 | /* enable host interrupt */ |
| 289 | ar933x_uart_rmw_set(up, AR933X_UART_CS_REG, |
| 290 | AR933X_UART_CS_HOST_INT_EN); |
| 291 | |
Daniel Golle | ab52eba | 2020-02-07 11:53:35 +0200 | [diff] [blame^] | 292 | /* enable RX and TX ready overide */ |
| 293 | ar933x_uart_rmw_set(up, AR933X_UART_CS_REG, |
| 294 | AR933X_UART_CS_TX_READY_ORIDE | AR933X_UART_CS_RX_READY_ORIDE); |
| 295 | |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 296 | /* reenable the UART */ |
| 297 | ar933x_uart_rmw(up, AR933X_UART_CS_REG, |
| 298 | AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S, |
| 299 | AR933X_UART_CS_IF_MODE_DCE << AR933X_UART_CS_IF_MODE_S); |
| 300 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 301 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 302 | |
| 303 | if (tty_termios_baud_rate(new)) |
| 304 | tty_termios_encode_baud_rate(new, baud, baud); |
| 305 | } |
| 306 | |
| 307 | static void ar933x_uart_rx_chars(struct ar933x_uart_port *up) |
| 308 | { |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 309 | struct tty_port *port = &up->port.state->port; |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 310 | int max_count = 256; |
| 311 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 312 | do { |
| 313 | unsigned int rdata; |
| 314 | unsigned char ch; |
| 315 | |
| 316 | rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG); |
| 317 | if ((rdata & AR933X_UART_DATA_RX_CSR) == 0) |
| 318 | break; |
| 319 | |
| 320 | /* remove the character from the FIFO */ |
| 321 | ar933x_uart_write(up, AR933X_UART_DATA_REG, |
| 322 | AR933X_UART_DATA_RX_CSR); |
| 323 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 324 | up->port.icount.rx++; |
| 325 | ch = rdata & AR933X_UART_DATA_TX_RX_MASK; |
| 326 | |
| 327 | if (uart_handle_sysrq_char(&up->port, ch)) |
| 328 | continue; |
| 329 | |
| 330 | if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0) |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 331 | tty_insert_flip_char(port, ch, TTY_NORMAL); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 332 | } while (max_count-- > 0); |
| 333 | |
Viresh Kumar | b16c8e3 | 2013-08-19 20:14:08 +0530 | [diff] [blame] | 334 | spin_unlock(&up->port.lock); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 335 | tty_flip_buffer_push(port); |
Viresh Kumar | b16c8e3 | 2013-08-19 20:14:08 +0530 | [diff] [blame] | 336 | spin_lock(&up->port.lock); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static void ar933x_uart_tx_chars(struct ar933x_uart_port *up) |
| 340 | { |
| 341 | struct circ_buf *xmit = &up->port.state->xmit; |
| 342 | int count; |
| 343 | |
| 344 | if (uart_tx_stopped(&up->port)) |
| 345 | return; |
| 346 | |
| 347 | count = up->port.fifosize; |
| 348 | do { |
| 349 | unsigned int rdata; |
| 350 | |
| 351 | rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG); |
| 352 | if ((rdata & AR933X_UART_DATA_TX_CSR) == 0) |
| 353 | break; |
| 354 | |
| 355 | if (up->port.x_char) { |
| 356 | ar933x_uart_putc(up, up->port.x_char); |
| 357 | up->port.icount.tx++; |
| 358 | up->port.x_char = 0; |
| 359 | continue; |
| 360 | } |
| 361 | |
| 362 | if (uart_circ_empty(xmit)) |
| 363 | break; |
| 364 | |
| 365 | ar933x_uart_putc(up, xmit->buf[xmit->tail]); |
| 366 | |
| 367 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 368 | up->port.icount.tx++; |
| 369 | } while (--count > 0); |
| 370 | |
| 371 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 372 | uart_write_wakeup(&up->port); |
| 373 | |
| 374 | if (!uart_circ_empty(xmit)) |
| 375 | ar933x_uart_start_tx_interrupt(up); |
| 376 | } |
| 377 | |
| 378 | static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id) |
| 379 | { |
| 380 | struct ar933x_uart_port *up = dev_id; |
| 381 | unsigned int status; |
| 382 | |
| 383 | status = ar933x_uart_read(up, AR933X_UART_CS_REG); |
| 384 | if ((status & AR933X_UART_CS_HOST_INT) == 0) |
| 385 | return IRQ_NONE; |
| 386 | |
| 387 | spin_lock(&up->port.lock); |
| 388 | |
| 389 | status = ar933x_uart_read(up, AR933X_UART_INT_REG); |
| 390 | status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG); |
| 391 | |
| 392 | if (status & AR933X_UART_INT_RX_VALID) { |
| 393 | ar933x_uart_write(up, AR933X_UART_INT_REG, |
| 394 | AR933X_UART_INT_RX_VALID); |
| 395 | ar933x_uart_rx_chars(up); |
| 396 | } |
| 397 | |
| 398 | if (status & AR933X_UART_INT_TX_EMPTY) { |
| 399 | ar933x_uart_write(up, AR933X_UART_INT_REG, |
| 400 | AR933X_UART_INT_TX_EMPTY); |
| 401 | ar933x_uart_stop_tx_interrupt(up); |
| 402 | ar933x_uart_tx_chars(up); |
| 403 | } |
| 404 | |
| 405 | spin_unlock(&up->port.lock); |
| 406 | |
| 407 | return IRQ_HANDLED; |
| 408 | } |
| 409 | |
| 410 | static int ar933x_uart_startup(struct uart_port *port) |
| 411 | { |
Fabian Frederick | f9c1b28 | 2014-10-05 19:19:49 +0200 | [diff] [blame] | 412 | struct ar933x_uart_port *up = |
| 413 | container_of(port, struct ar933x_uart_port, port); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 414 | unsigned long flags; |
| 415 | int ret; |
| 416 | |
| 417 | ret = request_irq(up->port.irq, ar933x_uart_interrupt, |
| 418 | up->port.irqflags, dev_name(up->port.dev), up); |
| 419 | if (ret) |
| 420 | return ret; |
| 421 | |
| 422 | spin_lock_irqsave(&up->port.lock, flags); |
| 423 | |
| 424 | /* Enable HOST interrupts */ |
| 425 | ar933x_uart_rmw_set(up, AR933X_UART_CS_REG, |
| 426 | AR933X_UART_CS_HOST_INT_EN); |
| 427 | |
Daniel Golle | ab52eba | 2020-02-07 11:53:35 +0200 | [diff] [blame^] | 428 | /* enable RX and TX ready overide */ |
| 429 | ar933x_uart_rmw_set(up, AR933X_UART_CS_REG, |
| 430 | AR933X_UART_CS_TX_READY_ORIDE | AR933X_UART_CS_RX_READY_ORIDE); |
| 431 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 432 | /* Enable RX interrupts */ |
| 433 | up->ier = AR933X_UART_INT_RX_VALID; |
| 434 | ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); |
| 435 | |
| 436 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static void ar933x_uart_shutdown(struct uart_port *port) |
| 442 | { |
Fabian Frederick | f9c1b28 | 2014-10-05 19:19:49 +0200 | [diff] [blame] | 443 | struct ar933x_uart_port *up = |
| 444 | container_of(port, struct ar933x_uart_port, port); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 445 | |
| 446 | /* Disable all interrupts */ |
| 447 | up->ier = 0; |
| 448 | ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); |
| 449 | |
| 450 | /* Disable break condition */ |
| 451 | ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG, |
| 452 | AR933X_UART_CS_TX_BREAK); |
| 453 | |
| 454 | free_irq(up->port.irq, up); |
| 455 | } |
| 456 | |
| 457 | static const char *ar933x_uart_type(struct uart_port *port) |
| 458 | { |
| 459 | return (port->type == PORT_AR933X) ? "AR933X UART" : NULL; |
| 460 | } |
| 461 | |
| 462 | static void ar933x_uart_release_port(struct uart_port *port) |
| 463 | { |
| 464 | /* Nothing to release ... */ |
| 465 | } |
| 466 | |
| 467 | static int ar933x_uart_request_port(struct uart_port *port) |
| 468 | { |
| 469 | /* UARTs always present */ |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static void ar933x_uart_config_port(struct uart_port *port, int flags) |
| 474 | { |
| 475 | if (flags & UART_CONFIG_TYPE) |
| 476 | port->type = PORT_AR933X; |
| 477 | } |
| 478 | |
| 479 | static int ar933x_uart_verify_port(struct uart_port *port, |
| 480 | struct serial_struct *ser) |
| 481 | { |
Fabian Frederick | f9c1b28 | 2014-10-05 19:19:49 +0200 | [diff] [blame] | 482 | struct ar933x_uart_port *up = |
| 483 | container_of(port, struct ar933x_uart_port, port); |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 484 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 485 | if (ser->type != PORT_UNKNOWN && |
| 486 | ser->type != PORT_AR933X) |
| 487 | return -EINVAL; |
| 488 | |
| 489 | if (ser->irq < 0 || ser->irq >= NR_IRQS) |
| 490 | return -EINVAL; |
| 491 | |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 492 | if (ser->baud_base < up->min_baud || |
| 493 | ser->baud_base > up->max_baud) |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 494 | return -EINVAL; |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static struct uart_ops ar933x_uart_ops = { |
| 500 | .tx_empty = ar933x_uart_tx_empty, |
| 501 | .set_mctrl = ar933x_uart_set_mctrl, |
| 502 | .get_mctrl = ar933x_uart_get_mctrl, |
| 503 | .stop_tx = ar933x_uart_stop_tx, |
| 504 | .start_tx = ar933x_uart_start_tx, |
| 505 | .stop_rx = ar933x_uart_stop_rx, |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 506 | .break_ctl = ar933x_uart_break_ctl, |
| 507 | .startup = ar933x_uart_startup, |
| 508 | .shutdown = ar933x_uart_shutdown, |
| 509 | .set_termios = ar933x_uart_set_termios, |
| 510 | .type = ar933x_uart_type, |
| 511 | .release_port = ar933x_uart_release_port, |
| 512 | .request_port = ar933x_uart_request_port, |
| 513 | .config_port = ar933x_uart_config_port, |
| 514 | .verify_port = ar933x_uart_verify_port, |
| 515 | }; |
| 516 | |
Petr Štetiar | 21cc1fc | 2019-03-06 17:54:03 +0100 | [diff] [blame] | 517 | #ifdef CONFIG_SERIAL_AR933X_CONSOLE |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 518 | static struct ar933x_uart_port * |
| 519 | ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS]; |
| 520 | |
| 521 | static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up) |
| 522 | { |
| 523 | unsigned int status; |
| 524 | unsigned int timeout = 60000; |
| 525 | |
| 526 | /* Wait up to 60ms for the character(s) to be sent. */ |
| 527 | do { |
| 528 | status = ar933x_uart_read(up, AR933X_UART_DATA_REG); |
| 529 | if (--timeout == 0) |
| 530 | break; |
| 531 | udelay(1); |
| 532 | } while ((status & AR933X_UART_DATA_TX_CSR) == 0); |
| 533 | } |
| 534 | |
| 535 | static void ar933x_uart_console_putchar(struct uart_port *port, int ch) |
| 536 | { |
Fabian Frederick | f9c1b28 | 2014-10-05 19:19:49 +0200 | [diff] [blame] | 537 | struct ar933x_uart_port *up = |
| 538 | container_of(port, struct ar933x_uart_port, port); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 539 | |
| 540 | ar933x_uart_wait_xmitr(up); |
| 541 | ar933x_uart_putc(up, ch); |
| 542 | } |
| 543 | |
| 544 | static void ar933x_uart_console_write(struct console *co, const char *s, |
| 545 | unsigned int count) |
| 546 | { |
| 547 | struct ar933x_uart_port *up = ar933x_console_ports[co->index]; |
| 548 | unsigned long flags; |
| 549 | unsigned int int_en; |
| 550 | int locked = 1; |
| 551 | |
| 552 | local_irq_save(flags); |
| 553 | |
| 554 | if (up->port.sysrq) |
| 555 | locked = 0; |
| 556 | else if (oops_in_progress) |
| 557 | locked = spin_trylock(&up->port.lock); |
| 558 | else |
| 559 | spin_lock(&up->port.lock); |
| 560 | |
| 561 | /* |
| 562 | * First save the IER then disable the interrupts |
| 563 | */ |
| 564 | int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG); |
| 565 | ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0); |
| 566 | |
| 567 | uart_console_write(&up->port, s, count, ar933x_uart_console_putchar); |
| 568 | |
| 569 | /* |
| 570 | * Finally, wait for transmitter to become empty |
| 571 | * and restore the IER |
| 572 | */ |
| 573 | ar933x_uart_wait_xmitr(up); |
| 574 | ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en); |
| 575 | |
| 576 | ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS); |
| 577 | |
| 578 | if (locked) |
| 579 | spin_unlock(&up->port.lock); |
| 580 | |
| 581 | local_irq_restore(flags); |
| 582 | } |
| 583 | |
| 584 | static int ar933x_uart_console_setup(struct console *co, char *options) |
| 585 | { |
| 586 | struct ar933x_uart_port *up; |
| 587 | int baud = 115200; |
| 588 | int bits = 8; |
| 589 | int parity = 'n'; |
| 590 | int flow = 'n'; |
| 591 | |
| 592 | if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS) |
| 593 | return -EINVAL; |
| 594 | |
| 595 | up = ar933x_console_ports[co->index]; |
| 596 | if (!up) |
| 597 | return -ENODEV; |
| 598 | |
| 599 | if (options) |
| 600 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 601 | |
| 602 | return uart_set_options(&up->port, co, baud, parity, bits, flow); |
| 603 | } |
| 604 | |
| 605 | static struct console ar933x_uart_console = { |
| 606 | .name = "ttyATH", |
| 607 | .write = ar933x_uart_console_write, |
| 608 | .device = uart_console_device, |
| 609 | .setup = ar933x_uart_console_setup, |
| 610 | .flags = CON_PRINTBUFFER, |
| 611 | .index = -1, |
| 612 | .data = &ar933x_uart_driver, |
| 613 | }; |
Petr Štetiar | 21cc1fc | 2019-03-06 17:54:03 +0100 | [diff] [blame] | 614 | #endif /* CONFIG_SERIAL_AR933X_CONSOLE */ |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 615 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 616 | static struct uart_driver ar933x_uart_driver = { |
| 617 | .owner = THIS_MODULE, |
| 618 | .driver_name = DRIVER_NAME, |
| 619 | .dev_name = "ttyATH", |
| 620 | .nr = CONFIG_SERIAL_AR933X_NR_UARTS, |
Gabor Juhos | 1241553 | 2013-08-28 17:08:39 +0200 | [diff] [blame] | 621 | .cons = NULL, /* filled in runtime */ |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 622 | }; |
| 623 | |
Bill Pemberton | 9671f09 | 2012-11-19 13:21:50 -0500 | [diff] [blame] | 624 | static int ar933x_uart_probe(struct platform_device *pdev) |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 625 | { |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 626 | struct ar933x_uart_port *up; |
| 627 | struct uart_port *port; |
| 628 | struct resource *mem_res; |
| 629 | struct resource *irq_res; |
Gabor Juhos | dd910d9 | 2013-08-29 08:44:01 +0200 | [diff] [blame] | 630 | struct device_node *np; |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 631 | unsigned int baud; |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 632 | int id; |
| 633 | int ret; |
| 634 | |
Gabor Juhos | dd910d9 | 2013-08-29 08:44:01 +0200 | [diff] [blame] | 635 | np = pdev->dev.of_node; |
Masahiro Yamada | 97f2645 | 2016-08-03 13:45:50 -0700 | [diff] [blame] | 636 | if (IS_ENABLED(CONFIG_OF) && np) { |
Gabor Juhos | dd910d9 | 2013-08-29 08:44:01 +0200 | [diff] [blame] | 637 | id = of_alias_get_id(np, "serial"); |
| 638 | if (id < 0) { |
| 639 | dev_err(&pdev->dev, "unable to get alias id, err=%d\n", |
| 640 | id); |
| 641 | return id; |
| 642 | } |
| 643 | } else { |
| 644 | id = pdev->id; |
| 645 | if (id == -1) |
| 646 | id = 0; |
| 647 | } |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 648 | |
Axel Lin | bb8478d | 2015-02-23 11:34:30 +0800 | [diff] [blame] | 649 | if (id >= CONFIG_SERIAL_AR933X_NR_UARTS) |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 650 | return -EINVAL; |
| 651 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 652 | irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 653 | if (!irq_res) { |
| 654 | dev_err(&pdev->dev, "no IRQ resource\n"); |
| 655 | return -EINVAL; |
| 656 | } |
| 657 | |
Gabor Juhos | a324e4d | 2013-08-10 09:32:15 +0200 | [diff] [blame] | 658 | up = devm_kzalloc(&pdev->dev, sizeof(struct ar933x_uart_port), |
| 659 | GFP_KERNEL); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 660 | if (!up) |
| 661 | return -ENOMEM; |
| 662 | |
Gabor Juhos | 15ef17f | 2013-08-28 10:09:28 +0200 | [diff] [blame] | 663 | up->clk = devm_clk_get(&pdev->dev, "uart"); |
| 664 | if (IS_ERR(up->clk)) { |
| 665 | dev_err(&pdev->dev, "unable to get UART clock\n"); |
| 666 | return PTR_ERR(up->clk); |
| 667 | } |
| 668 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 669 | port = &up->port; |
Gabor Juhos | a324e4d | 2013-08-10 09:32:15 +0200 | [diff] [blame] | 670 | |
Julia Lawall | 071eb0f | 2013-08-14 11:11:22 +0200 | [diff] [blame] | 671 | mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Gabor Juhos | a324e4d | 2013-08-10 09:32:15 +0200 | [diff] [blame] | 672 | port->membase = devm_ioremap_resource(&pdev->dev, mem_res); |
| 673 | if (IS_ERR(port->membase)) |
| 674 | return PTR_ERR(port->membase); |
| 675 | |
Gabor Juhos | 15ef17f | 2013-08-28 10:09:28 +0200 | [diff] [blame] | 676 | ret = clk_prepare_enable(up->clk); |
| 677 | if (ret) |
| 678 | return ret; |
| 679 | |
| 680 | port->uartclk = clk_get_rate(up->clk); |
| 681 | if (!port->uartclk) { |
| 682 | ret = -EINVAL; |
| 683 | goto err_disable_clk; |
| 684 | } |
| 685 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 686 | port->mapbase = mem_res->start; |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 687 | port->line = id; |
| 688 | port->irq = irq_res->start; |
| 689 | port->dev = &pdev->dev; |
| 690 | port->type = PORT_AR933X; |
| 691 | port->iotype = UPIO_MEM32; |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 692 | |
| 693 | port->regshift = 2; |
| 694 | port->fifosize = AR933X_UART_FIFO_SIZE; |
| 695 | port->ops = &ar933x_uart_ops; |
| 696 | |
Gabor Juhos | 2dff8ad | 2012-11-14 10:38:13 +0100 | [diff] [blame] | 697 | baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1); |
| 698 | up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD); |
| 699 | |
| 700 | baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP); |
| 701 | up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD); |
| 702 | |
Petr Štetiar | 21cc1fc | 2019-03-06 17:54:03 +0100 | [diff] [blame] | 703 | #ifdef CONFIG_SERIAL_AR933X_CONSOLE |
| 704 | ar933x_console_ports[up->port.line] = up; |
| 705 | #endif |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 706 | |
| 707 | ret = uart_add_one_port(&ar933x_uart_driver, &up->port); |
| 708 | if (ret) |
Gabor Juhos | 15ef17f | 2013-08-28 10:09:28 +0200 | [diff] [blame] | 709 | goto err_disable_clk; |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 710 | |
| 711 | platform_set_drvdata(pdev, up); |
| 712 | return 0; |
Gabor Juhos | 15ef17f | 2013-08-28 10:09:28 +0200 | [diff] [blame] | 713 | |
| 714 | err_disable_clk: |
| 715 | clk_disable_unprepare(up->clk); |
| 716 | return ret; |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Bill Pemberton | ae8d8a1 | 2012-11-19 13:26:18 -0500 | [diff] [blame] | 719 | static int ar933x_uart_remove(struct platform_device *pdev) |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 720 | { |
| 721 | struct ar933x_uart_port *up; |
| 722 | |
| 723 | up = platform_get_drvdata(pdev); |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 724 | |
Gabor Juhos | 15ef17f | 2013-08-28 10:09:28 +0200 | [diff] [blame] | 725 | if (up) { |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 726 | uart_remove_one_port(&ar933x_uart_driver, &up->port); |
Gabor Juhos | 15ef17f | 2013-08-28 10:09:28 +0200 | [diff] [blame] | 727 | clk_disable_unprepare(up->clk); |
| 728 | } |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
Gabor Juhos | dd910d9 | 2013-08-29 08:44:01 +0200 | [diff] [blame] | 733 | #ifdef CONFIG_OF |
| 734 | static const struct of_device_id ar933x_uart_of_ids[] = { |
| 735 | { .compatible = "qca,ar9330-uart" }, |
| 736 | {}, |
| 737 | }; |
| 738 | MODULE_DEVICE_TABLE(of, ar933x_uart_of_ids); |
| 739 | #endif |
| 740 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 741 | static struct platform_driver ar933x_uart_platform_driver = { |
| 742 | .probe = ar933x_uart_probe, |
Bill Pemberton | 2d47b71 | 2012-11-19 13:21:34 -0500 | [diff] [blame] | 743 | .remove = ar933x_uart_remove, |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 744 | .driver = { |
| 745 | .name = DRIVER_NAME, |
Gabor Juhos | dd910d9 | 2013-08-29 08:44:01 +0200 | [diff] [blame] | 746 | .of_match_table = of_match_ptr(ar933x_uart_of_ids), |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 747 | }, |
| 748 | }; |
| 749 | |
| 750 | static int __init ar933x_uart_init(void) |
| 751 | { |
| 752 | int ret; |
| 753 | |
Petr Štetiar | 21cc1fc | 2019-03-06 17:54:03 +0100 | [diff] [blame] | 754 | #ifdef CONFIG_SERIAL_AR933X_CONSOLE |
| 755 | ar933x_uart_driver.cons = &ar933x_uart_console; |
| 756 | #endif |
Gabor Juhos | 1241553 | 2013-08-28 17:08:39 +0200 | [diff] [blame] | 757 | |
Gabor Juhos | d57f341 | 2011-06-20 19:26:11 +0200 | [diff] [blame] | 758 | ret = uart_register_driver(&ar933x_uart_driver); |
| 759 | if (ret) |
| 760 | goto err_out; |
| 761 | |
| 762 | ret = platform_driver_register(&ar933x_uart_platform_driver); |
| 763 | if (ret) |
| 764 | goto err_unregister_uart_driver; |
| 765 | |
| 766 | return 0; |
| 767 | |
| 768 | err_unregister_uart_driver: |
| 769 | uart_unregister_driver(&ar933x_uart_driver); |
| 770 | err_out: |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | static void __exit ar933x_uart_exit(void) |
| 775 | { |
| 776 | platform_driver_unregister(&ar933x_uart_platform_driver); |
| 777 | uart_unregister_driver(&ar933x_uart_driver); |
| 778 | } |
| 779 | |
| 780 | module_init(ar933x_uart_init); |
| 781 | module_exit(ar933x_uart_exit); |
| 782 | |
| 783 | MODULE_DESCRIPTION("Atheros AR933X UART driver"); |
| 784 | MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); |
| 785 | MODULE_LICENSE("GPL v2"); |
| 786 | MODULE_ALIAS("platform:" DRIVER_NAME); |