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