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