blob: d4462512605b5cbc58d053a3183d332db9a8d9ee [file] [log] [blame]
Gabor Juhosd57f3412011-06-20 19:26:11 +02001/*
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 Juhosdd910d92013-08-29 08:44:01 +020020#include <linux/of.h>
21#include <linux/of_platform.h>
Gabor Juhosd57f3412011-06-20 19:26:11 +020022#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 Juhos15ef17f2013-08-28 10:09:28 +020029#include <linux/clk.h>
Gabor Juhosd57f3412011-06-20 19:26:11 +020030
Gabor Juhos2dff8ad2012-11-14 10:38:13 +010031#include <asm/div64.h>
32
Gabor Juhosd57f3412011-06-20 19:26:11 +020033#include <asm/mach-ath79/ar933x_uart.h>
Gabor Juhosd57f3412011-06-20 19:26:11 +020034
35#define DRIVER_NAME "ar933x-uart"
36
Gabor Juhos2dff8ad2012-11-14 10:38:13 +010037#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 Juhosd57f3412011-06-20 19:26:11 +020043#define AR933X_DUMMY_STATUS_RD 0x01
44
45static struct uart_driver ar933x_uart_driver;
46
47struct ar933x_uart_port {
48 struct uart_port port;
49 unsigned int ier; /* shadow Interrupt Enable Register */
Gabor Juhos2dff8ad2012-11-14 10:38:13 +010050 unsigned int min_baud;
51 unsigned int max_baud;
Gabor Juhos15ef17f2013-08-28 10:09:28 +020052 struct clk *clk;
Gabor Juhosd57f3412011-06-20 19:26:11 +020053};
54
55static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up,
56 int offset)
57{
58 return readl(up->port.membase + offset);
59}
60
61static 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
67static 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
80static 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
87static 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
94static 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
100static 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
106static 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
115static unsigned int ar933x_uart_tx_empty(struct uart_port *port)
116{
Fabian Frederickf9c1b282014-10-05 19:19:49 +0200117 struct ar933x_uart_port *up =
118 container_of(port, struct ar933x_uart_port, port);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200119 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
129static unsigned int ar933x_uart_get_mctrl(struct uart_port *port)
130{
131 return TIOCM_CAR;
132}
133
134static void ar933x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
135{
136}
137
138static void ar933x_uart_start_tx(struct uart_port *port)
139{
Fabian Frederickf9c1b282014-10-05 19:19:49 +0200140 struct ar933x_uart_port *up =
141 container_of(port, struct ar933x_uart_port, port);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200142
143 ar933x_uart_start_tx_interrupt(up);
144}
145
146static void ar933x_uart_stop_tx(struct uart_port *port)
147{
Fabian Frederickf9c1b282014-10-05 19:19:49 +0200148 struct ar933x_uart_port *up =
149 container_of(port, struct ar933x_uart_port, port);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200150
151 ar933x_uart_stop_tx_interrupt(up);
152}
153
154static void ar933x_uart_stop_rx(struct uart_port *port)
155{
Fabian Frederickf9c1b282014-10-05 19:19:49 +0200156 struct ar933x_uart_port *up =
157 container_of(port, struct ar933x_uart_port, port);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200158
159 up->ier &= ~AR933X_UART_INT_RX_VALID;
160 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
161}
162
163static void ar933x_uart_break_ctl(struct uart_port *port, int break_state)
164{
Fabian Frederickf9c1b282014-10-05 19:19:49 +0200165 struct ar933x_uart_port *up =
166 container_of(port, struct ar933x_uart_port, port);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200167 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 Juhos2dff8ad2012-11-14 10:38:13 +0100179/*
180 * baudrate = (clk / (scale + 1)) * (step * (1 / 2^17))
181 */
182static 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
198static 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 Juhosd57f3412011-06-20 19:26:11 +0200230static void ar933x_uart_set_termios(struct uart_port *port,
231 struct ktermios *new,
232 struct ktermios *old)
233{
Fabian Frederickf9c1b282014-10-05 19:19:49 +0200234 struct ar933x_uart_port *up =
235 container_of(port, struct ar933x_uart_port, port);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200236 unsigned int cs;
237 unsigned long flags;
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100238 unsigned int baud, scale, step;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200239
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 Juhos2dff8ad2012-11-14 10:38:13 +0100260 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 Juhosd57f3412011-06-20 19:26:11 +0200262
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 Juhos2dff8ad2012-11-14 10:38:13 +0100269 /* 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 Juhosd57f3412011-06-20 19:26:11 +0200273 /* 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 Juhos2dff8ad2012-11-14 10:38:13 +0100283 scale << AR933X_UART_CLOCK_SCALE_S | step);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200284
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
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100292 /* reenable the UART */
293 ar933x_uart_rmw(up, AR933X_UART_CS_REG,
294 AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S,
295 AR933X_UART_CS_IF_MODE_DCE << AR933X_UART_CS_IF_MODE_S);
296
Gabor Juhosd57f3412011-06-20 19:26:11 +0200297 spin_unlock_irqrestore(&up->port.lock, flags);
298
299 if (tty_termios_baud_rate(new))
300 tty_termios_encode_baud_rate(new, baud, baud);
301}
302
303static void ar933x_uart_rx_chars(struct ar933x_uart_port *up)
304{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100305 struct tty_port *port = &up->port.state->port;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200306 int max_count = 256;
307
Gabor Juhosd57f3412011-06-20 19:26:11 +0200308 do {
309 unsigned int rdata;
310 unsigned char ch;
311
312 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
313 if ((rdata & AR933X_UART_DATA_RX_CSR) == 0)
314 break;
315
316 /* remove the character from the FIFO */
317 ar933x_uart_write(up, AR933X_UART_DATA_REG,
318 AR933X_UART_DATA_RX_CSR);
319
Gabor Juhosd57f3412011-06-20 19:26:11 +0200320 up->port.icount.rx++;
321 ch = rdata & AR933X_UART_DATA_TX_RX_MASK;
322
323 if (uart_handle_sysrq_char(&up->port, ch))
324 continue;
325
326 if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100327 tty_insert_flip_char(port, ch, TTY_NORMAL);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200328 } while (max_count-- > 0);
329
Viresh Kumarb16c8e32013-08-19 20:14:08 +0530330 spin_unlock(&up->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100331 tty_flip_buffer_push(port);
Viresh Kumarb16c8e32013-08-19 20:14:08 +0530332 spin_lock(&up->port.lock);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200333}
334
335static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
336{
337 struct circ_buf *xmit = &up->port.state->xmit;
338 int count;
339
340 if (uart_tx_stopped(&up->port))
341 return;
342
343 count = up->port.fifosize;
344 do {
345 unsigned int rdata;
346
347 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
348 if ((rdata & AR933X_UART_DATA_TX_CSR) == 0)
349 break;
350
351 if (up->port.x_char) {
352 ar933x_uart_putc(up, up->port.x_char);
353 up->port.icount.tx++;
354 up->port.x_char = 0;
355 continue;
356 }
357
358 if (uart_circ_empty(xmit))
359 break;
360
361 ar933x_uart_putc(up, xmit->buf[xmit->tail]);
362
363 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
364 up->port.icount.tx++;
365 } while (--count > 0);
366
367 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
368 uart_write_wakeup(&up->port);
369
370 if (!uart_circ_empty(xmit))
371 ar933x_uart_start_tx_interrupt(up);
372}
373
374static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
375{
376 struct ar933x_uart_port *up = dev_id;
377 unsigned int status;
378
379 status = ar933x_uart_read(up, AR933X_UART_CS_REG);
380 if ((status & AR933X_UART_CS_HOST_INT) == 0)
381 return IRQ_NONE;
382
383 spin_lock(&up->port.lock);
384
385 status = ar933x_uart_read(up, AR933X_UART_INT_REG);
386 status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
387
388 if (status & AR933X_UART_INT_RX_VALID) {
389 ar933x_uart_write(up, AR933X_UART_INT_REG,
390 AR933X_UART_INT_RX_VALID);
391 ar933x_uart_rx_chars(up);
392 }
393
394 if (status & AR933X_UART_INT_TX_EMPTY) {
395 ar933x_uart_write(up, AR933X_UART_INT_REG,
396 AR933X_UART_INT_TX_EMPTY);
397 ar933x_uart_stop_tx_interrupt(up);
398 ar933x_uart_tx_chars(up);
399 }
400
401 spin_unlock(&up->port.lock);
402
403 return IRQ_HANDLED;
404}
405
406static int ar933x_uart_startup(struct uart_port *port)
407{
Fabian Frederickf9c1b282014-10-05 19:19:49 +0200408 struct ar933x_uart_port *up =
409 container_of(port, struct ar933x_uart_port, port);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200410 unsigned long flags;
411 int ret;
412
413 ret = request_irq(up->port.irq, ar933x_uart_interrupt,
414 up->port.irqflags, dev_name(up->port.dev), up);
415 if (ret)
416 return ret;
417
418 spin_lock_irqsave(&up->port.lock, flags);
419
420 /* Enable HOST interrupts */
421 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
422 AR933X_UART_CS_HOST_INT_EN);
423
424 /* Enable RX interrupts */
425 up->ier = AR933X_UART_INT_RX_VALID;
426 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
427
428 spin_unlock_irqrestore(&up->port.lock, flags);
429
430 return 0;
431}
432
433static void ar933x_uart_shutdown(struct uart_port *port)
434{
Fabian Frederickf9c1b282014-10-05 19:19:49 +0200435 struct ar933x_uart_port *up =
436 container_of(port, struct ar933x_uart_port, port);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200437
438 /* Disable all interrupts */
439 up->ier = 0;
440 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
441
442 /* Disable break condition */
443 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
444 AR933X_UART_CS_TX_BREAK);
445
446 free_irq(up->port.irq, up);
447}
448
449static const char *ar933x_uart_type(struct uart_port *port)
450{
451 return (port->type == PORT_AR933X) ? "AR933X UART" : NULL;
452}
453
454static void ar933x_uart_release_port(struct uart_port *port)
455{
456 /* Nothing to release ... */
457}
458
459static int ar933x_uart_request_port(struct uart_port *port)
460{
461 /* UARTs always present */
462 return 0;
463}
464
465static void ar933x_uart_config_port(struct uart_port *port, int flags)
466{
467 if (flags & UART_CONFIG_TYPE)
468 port->type = PORT_AR933X;
469}
470
471static int ar933x_uart_verify_port(struct uart_port *port,
472 struct serial_struct *ser)
473{
Fabian Frederickf9c1b282014-10-05 19:19:49 +0200474 struct ar933x_uart_port *up =
475 container_of(port, struct ar933x_uart_port, port);
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100476
Gabor Juhosd57f3412011-06-20 19:26:11 +0200477 if (ser->type != PORT_UNKNOWN &&
478 ser->type != PORT_AR933X)
479 return -EINVAL;
480
481 if (ser->irq < 0 || ser->irq >= NR_IRQS)
482 return -EINVAL;
483
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100484 if (ser->baud_base < up->min_baud ||
485 ser->baud_base > up->max_baud)
Gabor Juhosd57f3412011-06-20 19:26:11 +0200486 return -EINVAL;
487
488 return 0;
489}
490
491static struct uart_ops ar933x_uart_ops = {
492 .tx_empty = ar933x_uart_tx_empty,
493 .set_mctrl = ar933x_uart_set_mctrl,
494 .get_mctrl = ar933x_uart_get_mctrl,
495 .stop_tx = ar933x_uart_stop_tx,
496 .start_tx = ar933x_uart_start_tx,
497 .stop_rx = ar933x_uart_stop_rx,
Gabor Juhosd57f3412011-06-20 19:26:11 +0200498 .break_ctl = ar933x_uart_break_ctl,
499 .startup = ar933x_uart_startup,
500 .shutdown = ar933x_uart_shutdown,
501 .set_termios = ar933x_uart_set_termios,
502 .type = ar933x_uart_type,
503 .release_port = ar933x_uart_release_port,
504 .request_port = ar933x_uart_request_port,
505 .config_port = ar933x_uart_config_port,
506 .verify_port = ar933x_uart_verify_port,
507};
508
Petr Štetiar21cc1fc2019-03-06 17:54:03 +0100509#ifdef CONFIG_SERIAL_AR933X_CONSOLE
Gabor Juhosd57f3412011-06-20 19:26:11 +0200510static struct ar933x_uart_port *
511ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
512
513static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up)
514{
515 unsigned int status;
516 unsigned int timeout = 60000;
517
518 /* Wait up to 60ms for the character(s) to be sent. */
519 do {
520 status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
521 if (--timeout == 0)
522 break;
523 udelay(1);
524 } while ((status & AR933X_UART_DATA_TX_CSR) == 0);
525}
526
527static void ar933x_uart_console_putchar(struct uart_port *port, int ch)
528{
Fabian Frederickf9c1b282014-10-05 19:19:49 +0200529 struct ar933x_uart_port *up =
530 container_of(port, struct ar933x_uart_port, port);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200531
532 ar933x_uart_wait_xmitr(up);
533 ar933x_uart_putc(up, ch);
534}
535
536static void ar933x_uart_console_write(struct console *co, const char *s,
537 unsigned int count)
538{
539 struct ar933x_uart_port *up = ar933x_console_ports[co->index];
540 unsigned long flags;
541 unsigned int int_en;
542 int locked = 1;
543
544 local_irq_save(flags);
545
546 if (up->port.sysrq)
547 locked = 0;
548 else if (oops_in_progress)
549 locked = spin_trylock(&up->port.lock);
550 else
551 spin_lock(&up->port.lock);
552
553 /*
554 * First save the IER then disable the interrupts
555 */
556 int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
557 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
558
559 uart_console_write(&up->port, s, count, ar933x_uart_console_putchar);
560
561 /*
562 * Finally, wait for transmitter to become empty
563 * and restore the IER
564 */
565 ar933x_uart_wait_xmitr(up);
566 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en);
567
568 ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS);
569
570 if (locked)
571 spin_unlock(&up->port.lock);
572
573 local_irq_restore(flags);
574}
575
576static int ar933x_uart_console_setup(struct console *co, char *options)
577{
578 struct ar933x_uart_port *up;
579 int baud = 115200;
580 int bits = 8;
581 int parity = 'n';
582 int flow = 'n';
583
584 if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS)
585 return -EINVAL;
586
587 up = ar933x_console_ports[co->index];
588 if (!up)
589 return -ENODEV;
590
591 if (options)
592 uart_parse_options(options, &baud, &parity, &bits, &flow);
593
594 return uart_set_options(&up->port, co, baud, parity, bits, flow);
595}
596
597static struct console ar933x_uart_console = {
598 .name = "ttyATH",
599 .write = ar933x_uart_console_write,
600 .device = uart_console_device,
601 .setup = ar933x_uart_console_setup,
602 .flags = CON_PRINTBUFFER,
603 .index = -1,
604 .data = &ar933x_uart_driver,
605};
Petr Štetiar21cc1fc2019-03-06 17:54:03 +0100606#endif /* CONFIG_SERIAL_AR933X_CONSOLE */
Gabor Juhosd57f3412011-06-20 19:26:11 +0200607
Gabor Juhosd57f3412011-06-20 19:26:11 +0200608static struct uart_driver ar933x_uart_driver = {
609 .owner = THIS_MODULE,
610 .driver_name = DRIVER_NAME,
611 .dev_name = "ttyATH",
612 .nr = CONFIG_SERIAL_AR933X_NR_UARTS,
Gabor Juhos12415532013-08-28 17:08:39 +0200613 .cons = NULL, /* filled in runtime */
Gabor Juhosd57f3412011-06-20 19:26:11 +0200614};
615
Bill Pemberton9671f092012-11-19 13:21:50 -0500616static int ar933x_uart_probe(struct platform_device *pdev)
Gabor Juhosd57f3412011-06-20 19:26:11 +0200617{
Gabor Juhosd57f3412011-06-20 19:26:11 +0200618 struct ar933x_uart_port *up;
619 struct uart_port *port;
620 struct resource *mem_res;
621 struct resource *irq_res;
Gabor Juhosdd910d92013-08-29 08:44:01 +0200622 struct device_node *np;
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100623 unsigned int baud;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200624 int id;
625 int ret;
626
Gabor Juhosdd910d92013-08-29 08:44:01 +0200627 np = pdev->dev.of_node;
Masahiro Yamada97f26452016-08-03 13:45:50 -0700628 if (IS_ENABLED(CONFIG_OF) && np) {
Gabor Juhosdd910d92013-08-29 08:44:01 +0200629 id = of_alias_get_id(np, "serial");
630 if (id < 0) {
631 dev_err(&pdev->dev, "unable to get alias id, err=%d\n",
632 id);
633 return id;
634 }
635 } else {
636 id = pdev->id;
637 if (id == -1)
638 id = 0;
639 }
Gabor Juhosd57f3412011-06-20 19:26:11 +0200640
Axel Linbb8478d2015-02-23 11:34:30 +0800641 if (id >= CONFIG_SERIAL_AR933X_NR_UARTS)
Gabor Juhosd57f3412011-06-20 19:26:11 +0200642 return -EINVAL;
643
Gabor Juhosd57f3412011-06-20 19:26:11 +0200644 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
645 if (!irq_res) {
646 dev_err(&pdev->dev, "no IRQ resource\n");
647 return -EINVAL;
648 }
649
Gabor Juhosa324e4d2013-08-10 09:32:15 +0200650 up = devm_kzalloc(&pdev->dev, sizeof(struct ar933x_uart_port),
651 GFP_KERNEL);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200652 if (!up)
653 return -ENOMEM;
654
Gabor Juhos15ef17f2013-08-28 10:09:28 +0200655 up->clk = devm_clk_get(&pdev->dev, "uart");
656 if (IS_ERR(up->clk)) {
657 dev_err(&pdev->dev, "unable to get UART clock\n");
658 return PTR_ERR(up->clk);
659 }
660
Gabor Juhosd57f3412011-06-20 19:26:11 +0200661 port = &up->port;
Gabor Juhosa324e4d2013-08-10 09:32:15 +0200662
Julia Lawall071eb0f2013-08-14 11:11:22 +0200663 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Gabor Juhosa324e4d2013-08-10 09:32:15 +0200664 port->membase = devm_ioremap_resource(&pdev->dev, mem_res);
665 if (IS_ERR(port->membase))
666 return PTR_ERR(port->membase);
667
Gabor Juhos15ef17f2013-08-28 10:09:28 +0200668 ret = clk_prepare_enable(up->clk);
669 if (ret)
670 return ret;
671
672 port->uartclk = clk_get_rate(up->clk);
673 if (!port->uartclk) {
674 ret = -EINVAL;
675 goto err_disable_clk;
676 }
677
Gabor Juhosd57f3412011-06-20 19:26:11 +0200678 port->mapbase = mem_res->start;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200679 port->line = id;
680 port->irq = irq_res->start;
681 port->dev = &pdev->dev;
682 port->type = PORT_AR933X;
683 port->iotype = UPIO_MEM32;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200684
685 port->regshift = 2;
686 port->fifosize = AR933X_UART_FIFO_SIZE;
687 port->ops = &ar933x_uart_ops;
688
Gabor Juhos2dff8ad2012-11-14 10:38:13 +0100689 baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1);
690 up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD);
691
692 baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP);
693 up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD);
694
Petr Štetiar21cc1fc2019-03-06 17:54:03 +0100695#ifdef CONFIG_SERIAL_AR933X_CONSOLE
696 ar933x_console_ports[up->port.line] = up;
697#endif
Gabor Juhosd57f3412011-06-20 19:26:11 +0200698
699 ret = uart_add_one_port(&ar933x_uart_driver, &up->port);
700 if (ret)
Gabor Juhos15ef17f2013-08-28 10:09:28 +0200701 goto err_disable_clk;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200702
703 platform_set_drvdata(pdev, up);
704 return 0;
Gabor Juhos15ef17f2013-08-28 10:09:28 +0200705
706err_disable_clk:
707 clk_disable_unprepare(up->clk);
708 return ret;
Gabor Juhosd57f3412011-06-20 19:26:11 +0200709}
710
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500711static int ar933x_uart_remove(struct platform_device *pdev)
Gabor Juhosd57f3412011-06-20 19:26:11 +0200712{
713 struct ar933x_uart_port *up;
714
715 up = platform_get_drvdata(pdev);
Gabor Juhosd57f3412011-06-20 19:26:11 +0200716
Gabor Juhos15ef17f2013-08-28 10:09:28 +0200717 if (up) {
Gabor Juhosd57f3412011-06-20 19:26:11 +0200718 uart_remove_one_port(&ar933x_uart_driver, &up->port);
Gabor Juhos15ef17f2013-08-28 10:09:28 +0200719 clk_disable_unprepare(up->clk);
720 }
Gabor Juhosd57f3412011-06-20 19:26:11 +0200721
722 return 0;
723}
724
Gabor Juhosdd910d92013-08-29 08:44:01 +0200725#ifdef CONFIG_OF
726static const struct of_device_id ar933x_uart_of_ids[] = {
727 { .compatible = "qca,ar9330-uart" },
728 {},
729};
730MODULE_DEVICE_TABLE(of, ar933x_uart_of_ids);
731#endif
732
Gabor Juhosd57f3412011-06-20 19:26:11 +0200733static struct platform_driver ar933x_uart_platform_driver = {
734 .probe = ar933x_uart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500735 .remove = ar933x_uart_remove,
Gabor Juhosd57f3412011-06-20 19:26:11 +0200736 .driver = {
737 .name = DRIVER_NAME,
Gabor Juhosdd910d92013-08-29 08:44:01 +0200738 .of_match_table = of_match_ptr(ar933x_uart_of_ids),
Gabor Juhosd57f3412011-06-20 19:26:11 +0200739 },
740};
741
742static int __init ar933x_uart_init(void)
743{
744 int ret;
745
Petr Štetiar21cc1fc2019-03-06 17:54:03 +0100746#ifdef CONFIG_SERIAL_AR933X_CONSOLE
747 ar933x_uart_driver.cons = &ar933x_uart_console;
748#endif
Gabor Juhos12415532013-08-28 17:08:39 +0200749
Gabor Juhosd57f3412011-06-20 19:26:11 +0200750 ret = uart_register_driver(&ar933x_uart_driver);
751 if (ret)
752 goto err_out;
753
754 ret = platform_driver_register(&ar933x_uart_platform_driver);
755 if (ret)
756 goto err_unregister_uart_driver;
757
758 return 0;
759
760err_unregister_uart_driver:
761 uart_unregister_driver(&ar933x_uart_driver);
762err_out:
763 return ret;
764}
765
766static void __exit ar933x_uart_exit(void)
767{
768 platform_driver_unregister(&ar933x_uart_platform_driver);
769 uart_unregister_driver(&ar933x_uart_driver);
770}
771
772module_init(ar933x_uart_init);
773module_exit(ar933x_uart_exit);
774
775MODULE_DESCRIPTION("Atheros AR933X UART driver");
776MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
777MODULE_LICENSE("GPL v2");
778MODULE_ALIAS("platform:" DRIVER_NAME);