GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / tty / serial / mvebu-uart.c
1 /*
2 * ***************************************************************************
3 * Marvell Armada-3700 Serial Driver
4 * Author: Wilson Ding <dingwei@marvell.com>
5 * Copyright (C) 2015 Marvell International Ltd.
6 * ***************************************************************************
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 2 of the License, or any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 * ***************************************************************************
19 */
20
21 #include <linux/clk.h>
22 #include <linux/console.h>
23 #include <linux/delay.h>
24 #include <linux/device.h>
25 #include <linux/init.h>
26 #include <linux/io.h>
27 #include <linux/iopoll.h>
28 #include <linux/of.h>
29 #include <linux/of_address.h>
30 #include <linux/of_device.h>
31 #include <linux/of_irq.h>
32 #include <linux/of_platform.h>
33 #include <linux/platform_device.h>
34 #include <linux/serial.h>
35 #include <linux/serial_core.h>
36 #include <linux/slab.h>
37 #include <linux/tty.h>
38 #include <linux/tty_flip.h>
39
40 /* Register Map */
41 #define UART_RBR                0x00
42 #define  RBR_BRK_DET            BIT(15)
43 #define  RBR_FRM_ERR_DET        BIT(14)
44 #define  RBR_PAR_ERR_DET        BIT(13)
45 #define  RBR_OVR_ERR_DET        BIT(12)
46
47 #define UART_TSH                0x04
48
49 #define UART_CTRL               0x08
50 #define  CTRL_SOFT_RST          BIT(31)
51 #define  CTRL_TXFIFO_RST        BIT(15)
52 #define  CTRL_RXFIFO_RST        BIT(14)
53 #define  CTRL_ST_MIRR_EN        BIT(13)
54 #define  CTRL_LPBK_EN           BIT(12)
55 #define  CTRL_SND_BRK_SEQ       BIT(11)
56 #define  CTRL_PAR_EN            BIT(10)
57 #define  CTRL_TWO_STOP          BIT(9)
58 #define  CTRL_TX_HFL_INT        BIT(8)
59 #define  CTRL_RX_HFL_INT        BIT(7)
60 #define  CTRL_TX_EMP_INT        BIT(6)
61 #define  CTRL_TX_RDY_INT        BIT(5)
62 #define  CTRL_RX_RDY_INT        BIT(4)
63 #define  CTRL_BRK_DET_INT       BIT(3)
64 #define  CTRL_FRM_ERR_INT       BIT(2)
65 #define  CTRL_PAR_ERR_INT       BIT(1)
66 #define  CTRL_OVR_ERR_INT       BIT(0)
67 #define  CTRL_RX_INT                    (CTRL_RX_RDY_INT | CTRL_BRK_DET_INT |\
68         CTRL_FRM_ERR_INT | CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
69
70 #define UART_STAT               0x0c
71 #define  STAT_TX_FIFO_EMP       BIT(13)
72 #define  STAT_RX_FIFO_EMP       BIT(12)
73 #define  STAT_TX_FIFO_FUL       BIT(11)
74 #define  STAT_TX_FIFO_HFL       BIT(10)
75 #define  STAT_RX_TOGL           BIT(9)
76 #define  STAT_RX_FIFO_FUL       BIT(8)
77 #define  STAT_RX_FIFO_HFL       BIT(7)
78 #define  STAT_TX_EMP            BIT(6)
79 #define  STAT_TX_RDY            BIT(5)
80 #define  STAT_RX_RDY            BIT(4)
81 #define  STAT_BRK_DET           BIT(3)
82 #define  STAT_FRM_ERR           BIT(2)
83 #define  STAT_PAR_ERR           BIT(1)
84 #define  STAT_OVR_ERR           BIT(0)
85 #define  STAT_BRK_ERR           (STAT_BRK_DET | STAT_FRM_ERR | STAT_FRM_ERR\
86                                  | STAT_PAR_ERR | STAT_OVR_ERR)
87
88 #define UART_BRDV               0x10
89
90 #define MVEBU_NR_UARTS          1
91
92 #define MVEBU_UART_TYPE         "mvebu-uart"
93
94 static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
95
96 struct mvebu_uart_data {
97         struct uart_port *port;
98         struct clk       *clk;
99 };
100
101 /* Core UART Driver Operations */
102 static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
103 {
104         unsigned long flags;
105         unsigned int st;
106
107         spin_lock_irqsave(&port->lock, flags);
108         st = readl(port->membase + UART_STAT);
109         spin_unlock_irqrestore(&port->lock, flags);
110
111         return (st & STAT_TX_EMP) ? TIOCSER_TEMT : 0;
112 }
113
114 static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
115 {
116         return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
117 }
118
119 static void mvebu_uart_set_mctrl(struct uart_port *port,
120                                  unsigned int mctrl)
121 {
122 /*
123  * Even if we do not support configuring the modem control lines, this
124  * function must be proided to the serial core
125  */
126 }
127
128 static void mvebu_uart_stop_tx(struct uart_port *port)
129 {
130         unsigned int ctl = readl(port->membase + UART_CTRL);
131
132         ctl &= ~CTRL_TX_RDY_INT;
133         writel(ctl, port->membase + UART_CTRL);
134 }
135
136 static void mvebu_uart_start_tx(struct uart_port *port)
137 {
138         unsigned int ctl = readl(port->membase + UART_CTRL);
139
140         ctl |= CTRL_TX_RDY_INT;
141         writel(ctl, port->membase + UART_CTRL);
142 }
143
144 static void mvebu_uart_stop_rx(struct uart_port *port)
145 {
146         unsigned int ctl = readl(port->membase + UART_CTRL);
147
148         ctl &= ~CTRL_RX_INT;
149         writel(ctl, port->membase + UART_CTRL);
150 }
151
152 static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
153 {
154         unsigned int ctl;
155         unsigned long flags;
156
157         spin_lock_irqsave(&port->lock, flags);
158         ctl = readl(port->membase + UART_CTRL);
159         if (brk == -1)
160                 ctl |= CTRL_SND_BRK_SEQ;
161         else
162                 ctl &= ~CTRL_SND_BRK_SEQ;
163         writel(ctl, port->membase + UART_CTRL);
164         spin_unlock_irqrestore(&port->lock, flags);
165 }
166
167 static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
168 {
169         struct tty_port *tport = &port->state->port;
170         unsigned char ch = 0;
171         char flag = 0;
172
173         do {
174                 if (status & STAT_RX_RDY) {
175                         ch = readl(port->membase + UART_RBR);
176                         ch &= 0xff;
177                         flag = TTY_NORMAL;
178                         port->icount.rx++;
179
180                         if (status & STAT_PAR_ERR)
181                                 port->icount.parity++;
182                 }
183
184                 if (status & STAT_BRK_DET) {
185                         port->icount.brk++;
186                         status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
187                         if (uart_handle_break(port))
188                                 goto ignore_char;
189                 }
190
191                 if (status & STAT_OVR_ERR)
192                         port->icount.overrun++;
193
194                 if (status & STAT_FRM_ERR)
195                         port->icount.frame++;
196
197                 if (uart_handle_sysrq_char(port, ch))
198                         goto ignore_char;
199
200                 if (status & port->ignore_status_mask & STAT_PAR_ERR)
201                         status &= ~STAT_RX_RDY;
202
203                 status &= port->read_status_mask;
204
205                 if (status & STAT_PAR_ERR)
206                         flag = TTY_PARITY;
207
208                 status &= ~port->ignore_status_mask;
209
210                 if (status & STAT_RX_RDY)
211                         tty_insert_flip_char(tport, ch, flag);
212
213                 if (status & STAT_BRK_DET)
214                         tty_insert_flip_char(tport, 0, TTY_BREAK);
215
216                 if (status & STAT_FRM_ERR)
217                         tty_insert_flip_char(tport, 0, TTY_FRAME);
218
219                 if (status & STAT_OVR_ERR)
220                         tty_insert_flip_char(tport, 0, TTY_OVERRUN);
221
222 ignore_char:
223                 status = readl(port->membase + UART_STAT);
224         } while (status & (STAT_RX_RDY | STAT_BRK_DET));
225
226         tty_flip_buffer_push(tport);
227 }
228
229 static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
230 {
231         struct circ_buf *xmit = &port->state->xmit;
232         unsigned int count;
233         unsigned int st;
234
235         if (port->x_char) {
236                 writel(port->x_char, port->membase + UART_TSH);
237                 port->icount.tx++;
238                 port->x_char = 0;
239                 return;
240         }
241
242         if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
243                 mvebu_uart_stop_tx(port);
244                 return;
245         }
246
247         for (count = 0; count < port->fifosize; count++) {
248                 writel(xmit->buf[xmit->tail], port->membase + UART_TSH);
249                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
250                 port->icount.tx++;
251
252                 if (uart_circ_empty(xmit))
253                         break;
254
255                 st = readl(port->membase + UART_STAT);
256                 if (st & STAT_TX_FIFO_FUL)
257                         break;
258         }
259
260         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
261                 uart_write_wakeup(port);
262
263         if (uart_circ_empty(xmit))
264                 mvebu_uart_stop_tx(port);
265 }
266
267 static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
268 {
269         struct uart_port *port = (struct uart_port *)dev_id;
270         unsigned int st = readl(port->membase + UART_STAT);
271
272         if (st & (STAT_RX_RDY | STAT_OVR_ERR | STAT_FRM_ERR | STAT_BRK_DET))
273                 mvebu_uart_rx_chars(port, st);
274
275         if (st & STAT_TX_RDY)
276                 mvebu_uart_tx_chars(port, st);
277
278         return IRQ_HANDLED;
279 }
280
281 static int mvebu_uart_startup(struct uart_port *port)
282 {
283         int ret;
284
285         writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
286                port->membase + UART_CTRL);
287         udelay(1);
288         writel(CTRL_RX_INT, port->membase + UART_CTRL);
289
290         ret = request_irq(port->irq, mvebu_uart_isr, port->irqflags, "serial",
291                           port);
292         if (ret) {
293                 dev_err(port->dev, "failed to request irq\n");
294                 return ret;
295         }
296
297         return 0;
298 }
299
300 static void mvebu_uart_shutdown(struct uart_port *port)
301 {
302         writel(0, port->membase + UART_CTRL);
303
304         free_irq(port->irq, port);
305 }
306
307 static void mvebu_uart_set_termios(struct uart_port *port,
308                                    struct ktermios *termios,
309                                    struct ktermios *old)
310 {
311         unsigned long flags;
312         unsigned int baud;
313
314         spin_lock_irqsave(&port->lock, flags);
315
316         port->read_status_mask = STAT_RX_RDY | STAT_OVR_ERR |
317                 STAT_TX_RDY | STAT_TX_FIFO_FUL;
318
319         if (termios->c_iflag & INPCK)
320                 port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
321
322         port->ignore_status_mask = 0;
323         if (termios->c_iflag & IGNPAR)
324                 port->ignore_status_mask |=
325                         STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
326
327         if ((termios->c_cflag & CREAD) == 0)
328                 port->ignore_status_mask |= STAT_RX_RDY | STAT_BRK_ERR;
329
330         if (old) {
331                 tty_termios_copy_hw(termios, old);
332                 termios->c_cflag |= CS8;
333         }
334
335         baud = uart_get_baud_rate(port, termios, old, 0, 460800);
336         uart_update_timeout(port, termios->c_cflag, baud);
337
338         spin_unlock_irqrestore(&port->lock, flags);
339 }
340
341 static const char *mvebu_uart_type(struct uart_port *port)
342 {
343         return MVEBU_UART_TYPE;
344 }
345
346 static void mvebu_uart_release_port(struct uart_port *port)
347 {
348         /* Nothing to do here */
349 }
350
351 static int mvebu_uart_request_port(struct uart_port *port)
352 {
353         return 0;
354 }
355
356 #ifdef CONFIG_CONSOLE_POLL
357 static int mvebu_uart_get_poll_char(struct uart_port *port)
358 {
359         unsigned int st = readl(port->membase + UART_STAT);
360
361         if (!(st & STAT_RX_RDY))
362                 return NO_POLL_CHAR;
363
364         return readl(port->membase + UART_RBR);
365 }
366
367 static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
368 {
369         unsigned int st;
370
371         for (;;) {
372                 st = readl(port->membase + UART_STAT);
373
374                 if (!(st & STAT_TX_FIFO_FUL))
375                         break;
376
377                 udelay(1);
378         }
379
380         writel(c, port->membase + UART_TSH);
381 }
382 #endif
383
384 static const struct uart_ops mvebu_uart_ops = {
385         .tx_empty       = mvebu_uart_tx_empty,
386         .set_mctrl      = mvebu_uart_set_mctrl,
387         .get_mctrl      = mvebu_uart_get_mctrl,
388         .stop_tx        = mvebu_uart_stop_tx,
389         .start_tx       = mvebu_uart_start_tx,
390         .stop_rx        = mvebu_uart_stop_rx,
391         .break_ctl      = mvebu_uart_break_ctl,
392         .startup        = mvebu_uart_startup,
393         .shutdown       = mvebu_uart_shutdown,
394         .set_termios    = mvebu_uart_set_termios,
395         .type           = mvebu_uart_type,
396         .release_port   = mvebu_uart_release_port,
397         .request_port   = mvebu_uart_request_port,
398 #ifdef CONFIG_CONSOLE_POLL
399         .poll_get_char  = mvebu_uart_get_poll_char,
400         .poll_put_char  = mvebu_uart_put_poll_char,
401 #endif
402 };
403
404 /* Console Driver Operations  */
405
406 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
407 /* Early Console */
408 static void mvebu_uart_putc(struct uart_port *port, int c)
409 {
410         unsigned int st;
411
412         for (;;) {
413                 st = readl(port->membase + UART_STAT);
414                 if (!(st & STAT_TX_FIFO_FUL))
415                         break;
416         }
417
418         writel(c, port->membase + UART_TSH);
419
420         for (;;) {
421                 st = readl(port->membase + UART_STAT);
422                 if (st & STAT_TX_FIFO_EMP)
423                         break;
424         }
425 }
426
427 static void mvebu_uart_putc_early_write(struct console *con,
428                                         const char *s,
429                                         unsigned n)
430 {
431         struct earlycon_device *dev = con->data;
432
433         uart_console_write(&dev->port, s, n, mvebu_uart_putc);
434 }
435
436 static int __init
437 mvebu_uart_early_console_setup(struct earlycon_device *device,
438                                const char *opt)
439 {
440         if (!device->port.membase)
441                 return -ENODEV;
442
443         device->con->write = mvebu_uart_putc_early_write;
444
445         return 0;
446 }
447
448 EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
449 OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
450                     mvebu_uart_early_console_setup);
451
452 static void wait_for_xmitr(struct uart_port *port)
453 {
454         u32 val;
455
456         readl_poll_timeout_atomic(port->membase + UART_STAT, val,
457                                   (val & STAT_TX_EMP), 1, 10000);
458 }
459
460 static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
461 {
462         wait_for_xmitr(port);
463         writel(ch, port->membase + UART_TSH);
464 }
465
466 static void mvebu_uart_console_write(struct console *co, const char *s,
467                                      unsigned int count)
468 {
469         struct uart_port *port = &mvebu_uart_ports[co->index];
470         unsigned long flags;
471         unsigned int ier;
472         int locked = 1;
473
474         if (oops_in_progress)
475                 locked = spin_trylock_irqsave(&port->lock, flags);
476         else
477                 spin_lock_irqsave(&port->lock, flags);
478
479         ier = readl(port->membase + UART_CTRL) &
480                 (CTRL_RX_INT | CTRL_TX_RDY_INT);
481         writel(0, port->membase + UART_CTRL);
482
483         uart_console_write(port, s, count, mvebu_uart_console_putchar);
484
485         wait_for_xmitr(port);
486
487         if (ier)
488                 writel(ier, port->membase + UART_CTRL);
489
490         if (locked)
491                 spin_unlock_irqrestore(&port->lock, flags);
492 }
493
494 static int mvebu_uart_console_setup(struct console *co, char *options)
495 {
496         struct uart_port *port;
497         int baud = 9600;
498         int bits = 8;
499         int parity = 'n';
500         int flow = 'n';
501
502         if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
503                 return -EINVAL;
504
505         port = &mvebu_uart_ports[co->index];
506
507         if (!port->mapbase || !port->membase) {
508                 pr_debug("console on ttyMV%i not present\n", co->index);
509                 return -ENODEV;
510         }
511
512         if (options)
513                 uart_parse_options(options, &baud, &parity, &bits, &flow);
514
515         return uart_set_options(port, co, baud, parity, bits, flow);
516 }
517
518 static struct uart_driver mvebu_uart_driver;
519
520 static struct console mvebu_uart_console = {
521         .name   = "ttyMV",
522         .write  = mvebu_uart_console_write,
523         .device = uart_console_device,
524         .setup  = mvebu_uart_console_setup,
525         .flags  = CON_PRINTBUFFER,
526         .index  = -1,
527         .data   = &mvebu_uart_driver,
528 };
529
530 static int __init mvebu_uart_console_init(void)
531 {
532         register_console(&mvebu_uart_console);
533         return 0;
534 }
535
536 console_initcall(mvebu_uart_console_init);
537
538
539 #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
540
541 static struct uart_driver mvebu_uart_driver = {
542         .owner                  = THIS_MODULE,
543         .driver_name            = "mvebu_serial",
544         .dev_name               = "ttyMV",
545         .nr                     = MVEBU_NR_UARTS,
546 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
547         .cons                   = &mvebu_uart_console,
548 #endif
549 };
550
551 static int mvebu_uart_probe(struct platform_device *pdev)
552 {
553         struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
554         struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
555         struct uart_port *port;
556         struct mvebu_uart_data *data;
557         int ret;
558
559         if (!reg || !irq) {
560                 dev_err(&pdev->dev, "no registers/irq defined\n");
561                 return -EINVAL;
562         }
563
564         port = &mvebu_uart_ports[0];
565
566         spin_lock_init(&port->lock);
567
568         port->dev        = &pdev->dev;
569         port->type       = PORT_MVEBU;
570         port->ops        = &mvebu_uart_ops;
571         port->regshift   = 0;
572
573         port->fifosize   = 32;
574         port->iotype     = UPIO_MEM32;
575         port->flags      = UPF_FIXED_PORT;
576         port->line       = 0; /* single port: force line number to  0 */
577
578         port->irq        = irq->start;
579         port->irqflags   = 0;
580         port->mapbase    = reg->start;
581
582         port->membase = devm_ioremap_resource(&pdev->dev, reg);
583         if (IS_ERR(port->membase))
584                 return PTR_ERR(port->membase);
585
586         data = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart_data),
587                             GFP_KERNEL);
588         if (!data)
589                 return -ENOMEM;
590
591         data->port = port;
592
593         port->private_data = data;
594         platform_set_drvdata(pdev, data);
595
596         ret = uart_add_one_port(&mvebu_uart_driver, port);
597         if (ret)
598                 return ret;
599         return 0;
600 }
601
602 /* Match table for of_platform binding */
603 static const struct of_device_id mvebu_uart_of_match[] = {
604         { .compatible = "marvell,armada-3700-uart", },
605         {}
606 };
607
608 static struct platform_driver mvebu_uart_platform_driver = {
609         .probe  = mvebu_uart_probe,
610         .driver = {
611                 .name  = "mvebu-uart",
612                 .of_match_table = of_match_ptr(mvebu_uart_of_match),
613                 .suppress_bind_attrs = true,
614         },
615 };
616
617 static int __init mvebu_uart_init(void)
618 {
619         int ret;
620
621         ret = uart_register_driver(&mvebu_uart_driver);
622         if (ret)
623                 return ret;
624
625         ret = platform_driver_register(&mvebu_uart_platform_driver);
626         if (ret)
627                 uart_unregister_driver(&mvebu_uart_driver);
628
629         return ret;
630 }
631 arch_initcall(mvebu_uart_init);