7f4112423f3dfe354a8ce9fe05d764b7610b4df7
[firefly-linux-kernel-4.4.55.git] / drivers / tty / serial / vt8500_serial.c
1 /*
2  * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
3  *
4  * Based on msm_serial.c, which is:
5  * Copyright (C) 2007 Google, Inc.
6  * Author: Robert Love <rlove@google.com>
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #if defined(CONFIG_SERIAL_VT8500_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19 # define SUPPORT_SYSRQ
20 #endif
21
22 #include <linux/hrtimer.h>
23 #include <linux/delay.h>
24 #include <linux/module.h>
25 #include <linux/io.h>
26 #include <linux/ioport.h>
27 #include <linux/irq.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/serial_core.h>
33 #include <linux/serial.h>
34 #include <linux/slab.h>
35 #include <linux/clk.h>
36 #include <linux/platform_device.h>
37 #include <linux/of.h>
38
39 /*
40  * UART Register offsets
41  */
42
43 #define VT8500_URTDR            0x0000  /* Transmit data */
44 #define VT8500_URRDR            0x0004  /* Receive data */
45 #define VT8500_URDIV            0x0008  /* Clock/Baud rate divisor */
46 #define VT8500_URLCR            0x000C  /* Line control */
47 #define VT8500_URICR            0x0010  /* IrDA control */
48 #define VT8500_URIER            0x0014  /* Interrupt enable */
49 #define VT8500_URISR            0x0018  /* Interrupt status */
50 #define VT8500_URUSR            0x001c  /* UART status */
51 #define VT8500_URFCR            0x0020  /* FIFO control */
52 #define VT8500_URFIDX           0x0024  /* FIFO index */
53 #define VT8500_URBKR            0x0028  /* Break signal count */
54 #define VT8500_URTOD            0x002c  /* Time out divisor */
55 #define VT8500_TXFIFO           0x1000  /* Transmit FIFO (16x8) */
56 #define VT8500_RXFIFO           0x1020  /* Receive FIFO (16x10) */
57
58 /*
59  * Interrupt enable and status bits
60  */
61
62 #define TXDE    (1 << 0)        /* Tx Data empty */
63 #define RXDF    (1 << 1)        /* Rx Data full */
64 #define TXFAE   (1 << 2)        /* Tx FIFO almost empty */
65 #define TXFE    (1 << 3)        /* Tx FIFO empty */
66 #define RXFAF   (1 << 4)        /* Rx FIFO almost full */
67 #define RXFF    (1 << 5)        /* Rx FIFO full */
68 #define TXUDR   (1 << 6)        /* Tx underrun */
69 #define RXOVER  (1 << 7)        /* Rx overrun */
70 #define PER     (1 << 8)        /* Parity error */
71 #define FER     (1 << 9)        /* Frame error */
72 #define TCTS    (1 << 10)       /* Toggle of CTS */
73 #define RXTOUT  (1 << 11)       /* Rx timeout */
74 #define BKDONE  (1 << 12)       /* Break signal done */
75 #define ERR     (1 << 13)       /* AHB error response */
76
77 #define RX_FIFO_INTS    (RXFAF | RXFF | RXOVER | PER | FER | RXTOUT)
78 #define TX_FIFO_INTS    (TXFAE | TXFE | TXUDR)
79
80 #define VT8500_MAX_PORTS        6
81
82 struct vt8500_port {
83         struct uart_port        uart;
84         char                    name[16];
85         struct clk              *clk;
86         unsigned int            ier;
87 };
88
89 /*
90  * we use this variable to keep track of which ports
91  * have been allocated as we can't use pdev->id in
92  * devicetree
93  */
94 static unsigned long vt8500_ports_in_use;
95
96 static inline void vt8500_write(struct uart_port *port, unsigned int val,
97                              unsigned int off)
98 {
99         writel(val, port->membase + off);
100 }
101
102 static inline unsigned int vt8500_read(struct uart_port *port, unsigned int off)
103 {
104         return readl(port->membase + off);
105 }
106
107 static void vt8500_stop_tx(struct uart_port *port)
108 {
109         struct vt8500_port *vt8500_port = container_of(port,
110                                                        struct vt8500_port,
111                                                        uart);
112
113         vt8500_port->ier &= ~TX_FIFO_INTS;
114         vt8500_write(port, vt8500_port->ier, VT8500_URIER);
115 }
116
117 static void vt8500_stop_rx(struct uart_port *port)
118 {
119         struct vt8500_port *vt8500_port = container_of(port,
120                                                        struct vt8500_port,
121                                                        uart);
122
123         vt8500_port->ier &= ~RX_FIFO_INTS;
124         vt8500_write(port, vt8500_port->ier, VT8500_URIER);
125 }
126
127 static void vt8500_enable_ms(struct uart_port *port)
128 {
129         struct vt8500_port *vt8500_port = container_of(port,
130                                                        struct vt8500_port,
131                                                        uart);
132
133         vt8500_port->ier |= TCTS;
134         vt8500_write(port, vt8500_port->ier, VT8500_URIER);
135 }
136
137 static void handle_rx(struct uart_port *port)
138 {
139         struct tty_port *tport = &port->state->port;
140         struct tty_struct *tty = tty_port_tty_get(tport);
141         if (!tty) {
142                 /* Discard data: no tty available */
143                 int count = (vt8500_read(port, VT8500_URFIDX) & 0x1f00) >> 8;
144                 u16 ch;
145                 while (count--)
146                         ch = readw(port->membase + VT8500_RXFIFO);
147                 return;
148         }
149
150         /*
151          * Handle overrun
152          */
153         if ((vt8500_read(port, VT8500_URISR) & RXOVER)) {
154                 port->icount.overrun++;
155                 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
156         }
157
158         /* and now the main RX loop */
159         while (vt8500_read(port, VT8500_URFIDX) & 0x1f00) {
160                 unsigned int c;
161                 char flag = TTY_NORMAL;
162
163                 c = readw(port->membase + VT8500_RXFIFO) & 0x3ff;
164
165                 /* Mask conditions we're ignorning. */
166                 c &= ~port->read_status_mask;
167
168                 if (c & FER) {
169                         port->icount.frame++;
170                         flag = TTY_FRAME;
171                 } else if (c & PER) {
172                         port->icount.parity++;
173                         flag = TTY_PARITY;
174                 }
175                 port->icount.rx++;
176
177                 if (!uart_handle_sysrq_char(port, c))
178                         tty_insert_flip_char(tport, c, flag);
179         }
180
181         tty_flip_buffer_push(tty);
182         tty_kref_put(tty);
183 }
184
185 static void handle_tx(struct uart_port *port)
186 {
187         struct circ_buf *xmit = &port->state->xmit;
188
189         if (port->x_char) {
190                 writeb(port->x_char, port->membase + VT8500_TXFIFO);
191                 port->icount.tx++;
192                 port->x_char = 0;
193         }
194         if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
195                 vt8500_stop_tx(port);
196                 return;
197         }
198
199         while ((vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16) {
200                 if (uart_circ_empty(xmit))
201                         break;
202
203                 writeb(xmit->buf[xmit->tail], port->membase + VT8500_TXFIFO);
204
205                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
206                 port->icount.tx++;
207         }
208
209         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
210                 uart_write_wakeup(port);
211
212         if (uart_circ_empty(xmit))
213                 vt8500_stop_tx(port);
214 }
215
216 static void vt8500_start_tx(struct uart_port *port)
217 {
218         struct vt8500_port *vt8500_port = container_of(port,
219                                                        struct vt8500_port,
220                                                        uart);
221
222         vt8500_port->ier &= ~TX_FIFO_INTS;
223         vt8500_write(port, vt8500_port->ier, VT8500_URIER);
224         handle_tx(port);
225         vt8500_port->ier |= TX_FIFO_INTS;
226         vt8500_write(port, vt8500_port->ier, VT8500_URIER);
227 }
228
229 static void handle_delta_cts(struct uart_port *port)
230 {
231         port->icount.cts++;
232         wake_up_interruptible(&port->state->port.delta_msr_wait);
233 }
234
235 static irqreturn_t vt8500_irq(int irq, void *dev_id)
236 {
237         struct uart_port *port = dev_id;
238         unsigned long isr;
239
240         spin_lock(&port->lock);
241         isr = vt8500_read(port, VT8500_URISR);
242
243         /* Acknowledge active status bits */
244         vt8500_write(port, isr, VT8500_URISR);
245
246         if (isr & RX_FIFO_INTS)
247                 handle_rx(port);
248         if (isr & TX_FIFO_INTS)
249                 handle_tx(port);
250         if (isr & TCTS)
251                 handle_delta_cts(port);
252
253         spin_unlock(&port->lock);
254
255         return IRQ_HANDLED;
256 }
257
258 static unsigned int vt8500_tx_empty(struct uart_port *port)
259 {
260         return (vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16 ?
261                                                 TIOCSER_TEMT : 0;
262 }
263
264 static unsigned int vt8500_get_mctrl(struct uart_port *port)
265 {
266         unsigned int usr;
267
268         usr = vt8500_read(port, VT8500_URUSR);
269         if (usr & (1 << 4))
270                 return TIOCM_CTS;
271         else
272                 return 0;
273 }
274
275 static void vt8500_set_mctrl(struct uart_port *port, unsigned int mctrl)
276 {
277 }
278
279 static void vt8500_break_ctl(struct uart_port *port, int break_ctl)
280 {
281         if (break_ctl)
282                 vt8500_write(port, vt8500_read(port, VT8500_URLCR) | (1 << 9),
283                              VT8500_URLCR);
284 }
285
286 static int vt8500_set_baud_rate(struct uart_port *port, unsigned int baud)
287 {
288         unsigned long div;
289         unsigned int loops = 1000;
290
291         div = vt8500_read(port, VT8500_URDIV) & ~(0x3ff);
292
293         if (unlikely((baud < 900) || (baud > 921600)))
294                 div |= 7;
295         else
296                 div |= (921600 / baud) - 1;
297
298         while ((vt8500_read(port, VT8500_URUSR) & (1 << 5)) && --loops)
299                 cpu_relax();
300         vt8500_write(port, div, VT8500_URDIV);
301
302         return baud;
303 }
304
305 static int vt8500_startup(struct uart_port *port)
306 {
307         struct vt8500_port *vt8500_port =
308                         container_of(port, struct vt8500_port, uart);
309         int ret;
310
311         snprintf(vt8500_port->name, sizeof(vt8500_port->name),
312                  "vt8500_serial%d", port->line);
313
314         ret = request_irq(port->irq, vt8500_irq, IRQF_TRIGGER_HIGH,
315                           vt8500_port->name, port);
316         if (unlikely(ret))
317                 return ret;
318
319         vt8500_write(port, 0x03, VT8500_URLCR); /* enable TX & RX */
320
321         return 0;
322 }
323
324 static void vt8500_shutdown(struct uart_port *port)
325 {
326         struct vt8500_port *vt8500_port =
327                         container_of(port, struct vt8500_port, uart);
328
329         vt8500_port->ier = 0;
330
331         /* disable interrupts and FIFOs */
332         vt8500_write(&vt8500_port->uart, 0, VT8500_URIER);
333         vt8500_write(&vt8500_port->uart, 0x880, VT8500_URFCR);
334         free_irq(port->irq, port);
335 }
336
337 static void vt8500_set_termios(struct uart_port *port,
338                                struct ktermios *termios,
339                                struct ktermios *old)
340 {
341         struct vt8500_port *vt8500_port =
342                         container_of(port, struct vt8500_port, uart);
343         unsigned long flags;
344         unsigned int baud, lcr;
345         unsigned int loops = 1000;
346
347         spin_lock_irqsave(&port->lock, flags);
348
349         /* calculate and set baud rate */
350         baud = uart_get_baud_rate(port, termios, old, 900, 921600);
351         baud = vt8500_set_baud_rate(port, baud);
352         if (tty_termios_baud_rate(termios))
353                 tty_termios_encode_baud_rate(termios, baud, baud);
354
355         /* calculate parity */
356         lcr = vt8500_read(&vt8500_port->uart, VT8500_URLCR);
357         lcr &= ~((1 << 5) | (1 << 4));
358         if (termios->c_cflag & PARENB) {
359                 lcr |= (1 << 4);
360                 termios->c_cflag &= ~CMSPAR;
361                 if (termios->c_cflag & PARODD)
362                         lcr |= (1 << 5);
363         }
364
365         /* calculate bits per char */
366         lcr &= ~(1 << 2);
367         switch (termios->c_cflag & CSIZE) {
368         case CS7:
369                 break;
370         case CS8:
371         default:
372                 lcr |= (1 << 2);
373                 termios->c_cflag &= ~CSIZE;
374                 termios->c_cflag |= CS8;
375                 break;
376         }
377
378         /* calculate stop bits */
379         lcr &= ~(1 << 3);
380         if (termios->c_cflag & CSTOPB)
381                 lcr |= (1 << 3);
382
383         /* set parity, bits per char, and stop bit */
384         vt8500_write(&vt8500_port->uart, lcr, VT8500_URLCR);
385
386         /* Configure status bits to ignore based on termio flags. */
387         port->read_status_mask = 0;
388         if (termios->c_iflag & IGNPAR)
389                 port->read_status_mask = FER | PER;
390
391         uart_update_timeout(port, termios->c_cflag, baud);
392
393         /* Reset FIFOs */
394         vt8500_write(&vt8500_port->uart, 0x88c, VT8500_URFCR);
395         while ((vt8500_read(&vt8500_port->uart, VT8500_URFCR) & 0xc)
396                                                         && --loops)
397                 cpu_relax();
398
399         /* Every possible FIFO-related interrupt */
400         vt8500_port->ier = RX_FIFO_INTS | TX_FIFO_INTS;
401
402         /*
403          * CTS flow control
404          */
405         if (UART_ENABLE_MS(&vt8500_port->uart, termios->c_cflag))
406                 vt8500_port->ier |= TCTS;
407
408         vt8500_write(&vt8500_port->uart, 0x881, VT8500_URFCR);
409         vt8500_write(&vt8500_port->uart, vt8500_port->ier, VT8500_URIER);
410
411         spin_unlock_irqrestore(&port->lock, flags);
412 }
413
414 static const char *vt8500_type(struct uart_port *port)
415 {
416         struct vt8500_port *vt8500_port =
417                         container_of(port, struct vt8500_port, uart);
418         return vt8500_port->name;
419 }
420
421 static void vt8500_release_port(struct uart_port *port)
422 {
423 }
424
425 static int vt8500_request_port(struct uart_port *port)
426 {
427         return 0;
428 }
429
430 static void vt8500_config_port(struct uart_port *port, int flags)
431 {
432         port->type = PORT_VT8500;
433 }
434
435 static int vt8500_verify_port(struct uart_port *port,
436                               struct serial_struct *ser)
437 {
438         if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_VT8500))
439                 return -EINVAL;
440         if (unlikely(port->irq != ser->irq))
441                 return -EINVAL;
442         return 0;
443 }
444
445 static struct vt8500_port *vt8500_uart_ports[VT8500_MAX_PORTS];
446 static struct uart_driver vt8500_uart_driver;
447
448 #ifdef CONFIG_SERIAL_VT8500_CONSOLE
449
450 static inline void wait_for_xmitr(struct uart_port *port)
451 {
452         unsigned int status, tmout = 10000;
453
454         /* Wait up to 10ms for the character(s) to be sent. */
455         do {
456                 status = vt8500_read(port, VT8500_URFIDX);
457
458                 if (--tmout == 0)
459                         break;
460                 udelay(1);
461         } while (status & 0x10);
462 }
463
464 static void vt8500_console_putchar(struct uart_port *port, int c)
465 {
466         wait_for_xmitr(port);
467         writeb(c, port->membase + VT8500_TXFIFO);
468 }
469
470 static void vt8500_console_write(struct console *co, const char *s,
471                               unsigned int count)
472 {
473         struct vt8500_port *vt8500_port = vt8500_uart_ports[co->index];
474         unsigned long ier;
475
476         BUG_ON(co->index < 0 || co->index >= vt8500_uart_driver.nr);
477
478         ier = vt8500_read(&vt8500_port->uart, VT8500_URIER);
479         vt8500_write(&vt8500_port->uart, VT8500_URIER, 0);
480
481         uart_console_write(&vt8500_port->uart, s, count,
482                            vt8500_console_putchar);
483
484         /*
485          *      Finally, wait for transmitter to become empty
486          *      and switch back to FIFO
487          */
488         wait_for_xmitr(&vt8500_port->uart);
489         vt8500_write(&vt8500_port->uart, VT8500_URIER, ier);
490 }
491
492 static int __init vt8500_console_setup(struct console *co, char *options)
493 {
494         struct vt8500_port *vt8500_port;
495         int baud = 9600;
496         int bits = 8;
497         int parity = 'n';
498         int flow = 'n';
499
500         if (unlikely(co->index >= vt8500_uart_driver.nr || co->index < 0))
501                 return -ENXIO;
502
503         vt8500_port = vt8500_uart_ports[co->index];
504
505         if (!vt8500_port)
506                 return -ENODEV;
507
508         if (options)
509                 uart_parse_options(options, &baud, &parity, &bits, &flow);
510
511         return uart_set_options(&vt8500_port->uart,
512                                  co, baud, parity, bits, flow);
513 }
514
515 static struct console vt8500_console = {
516         .name = "ttyWMT",
517         .write = vt8500_console_write,
518         .device = uart_console_device,
519         .setup = vt8500_console_setup,
520         .flags = CON_PRINTBUFFER,
521         .index = -1,
522         .data = &vt8500_uart_driver,
523 };
524
525 #define VT8500_CONSOLE  (&vt8500_console)
526
527 #else
528 #define VT8500_CONSOLE  NULL
529 #endif
530
531 static struct uart_ops vt8500_uart_pops = {
532         .tx_empty       = vt8500_tx_empty,
533         .set_mctrl      = vt8500_set_mctrl,
534         .get_mctrl      = vt8500_get_mctrl,
535         .stop_tx        = vt8500_stop_tx,
536         .start_tx       = vt8500_start_tx,
537         .stop_rx        = vt8500_stop_rx,
538         .enable_ms      = vt8500_enable_ms,
539         .break_ctl      = vt8500_break_ctl,
540         .startup        = vt8500_startup,
541         .shutdown       = vt8500_shutdown,
542         .set_termios    = vt8500_set_termios,
543         .type           = vt8500_type,
544         .release_port   = vt8500_release_port,
545         .request_port   = vt8500_request_port,
546         .config_port    = vt8500_config_port,
547         .verify_port    = vt8500_verify_port,
548 };
549
550 static struct uart_driver vt8500_uart_driver = {
551         .owner          = THIS_MODULE,
552         .driver_name    = "vt8500_serial",
553         .dev_name       = "ttyWMT",
554         .nr             = 6,
555         .cons           = VT8500_CONSOLE,
556 };
557
558 static int vt8500_serial_probe(struct platform_device *pdev)
559 {
560         struct vt8500_port *vt8500_port;
561         struct resource *mmres, *irqres;
562         struct device_node *np = pdev->dev.of_node;
563         int ret;
564         int port;
565
566         mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
567         irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
568         if (!mmres || !irqres)
569                 return -ENODEV;
570
571         if (np)
572                 port = of_alias_get_id(np, "serial");
573                 if (port > VT8500_MAX_PORTS)
574                         port = -1;
575         else
576                 port = -1;
577
578         if (port < 0) {
579                 /* calculate the port id */
580                 port = find_first_zero_bit(&vt8500_ports_in_use,
581                                         sizeof(vt8500_ports_in_use));
582         }
583
584         if (port > VT8500_MAX_PORTS)
585                 return -ENODEV;
586
587         /* reserve the port id */
588         if (test_and_set_bit(port, &vt8500_ports_in_use)) {
589                 /* port already in use - shouldn't really happen */
590                 return -EBUSY;
591         }
592
593         vt8500_port = kzalloc(sizeof(struct vt8500_port), GFP_KERNEL);
594         if (!vt8500_port)
595                 return -ENOMEM;
596
597         vt8500_port->uart.type = PORT_VT8500;
598         vt8500_port->uart.iotype = UPIO_MEM;
599         vt8500_port->uart.mapbase = mmres->start;
600         vt8500_port->uart.irq = irqres->start;
601         vt8500_port->uart.fifosize = 16;
602         vt8500_port->uart.ops = &vt8500_uart_pops;
603         vt8500_port->uart.line = port;
604         vt8500_port->uart.dev = &pdev->dev;
605         vt8500_port->uart.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
606
607         vt8500_port->clk = of_clk_get(pdev->dev.of_node, 0);
608         if (vt8500_port->clk) {
609                 vt8500_port->uart.uartclk = clk_get_rate(vt8500_port->clk);
610         } else {
611                 /* use the default of 24Mhz if not specified and warn */
612                 pr_warn("%s: serial clock source not specified\n", __func__);
613                 vt8500_port->uart.uartclk = 24000000;
614         }
615
616         snprintf(vt8500_port->name, sizeof(vt8500_port->name),
617                  "VT8500 UART%d", pdev->id);
618
619         vt8500_port->uart.membase = ioremap(mmres->start, resource_size(mmres));
620         if (!vt8500_port->uart.membase) {
621                 ret = -ENOMEM;
622                 goto err;
623         }
624
625         vt8500_uart_ports[port] = vt8500_port;
626
627         uart_add_one_port(&vt8500_uart_driver, &vt8500_port->uart);
628
629         platform_set_drvdata(pdev, vt8500_port);
630
631         return 0;
632
633 err:
634         kfree(vt8500_port);
635         return ret;
636 }
637
638 static int vt8500_serial_remove(struct platform_device *pdev)
639 {
640         struct vt8500_port *vt8500_port = platform_get_drvdata(pdev);
641
642         platform_set_drvdata(pdev, NULL);
643         uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart);
644         kfree(vt8500_port);
645
646         return 0;
647 }
648
649 static const struct of_device_id wmt_dt_ids[] = {
650         { .compatible = "via,vt8500-uart", },
651         {}
652 };
653
654 static struct platform_driver vt8500_platform_driver = {
655         .probe  = vt8500_serial_probe,
656         .remove = vt8500_serial_remove,
657         .driver = {
658                 .name = "vt8500_serial",
659                 .owner = THIS_MODULE,
660                 .of_match_table = of_match_ptr(wmt_dt_ids),
661         },
662 };
663
664 static int __init vt8500_serial_init(void)
665 {
666         int ret;
667
668         ret = uart_register_driver(&vt8500_uart_driver);
669         if (unlikely(ret))
670                 return ret;
671
672         ret = platform_driver_register(&vt8500_platform_driver);
673
674         if (unlikely(ret))
675                 uart_unregister_driver(&vt8500_uart_driver);
676
677         return ret;
678 }
679
680 static void __exit vt8500_serial_exit(void)
681 {
682 #ifdef CONFIG_SERIAL_VT8500_CONSOLE
683         unregister_console(&vt8500_console);
684 #endif
685         platform_driver_unregister(&vt8500_platform_driver);
686         uart_unregister_driver(&vt8500_uart_driver);
687 }
688
689 module_init(vt8500_serial_init);
690 module_exit(vt8500_serial_exit);
691
692 MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
693 MODULE_DESCRIPTION("Driver for vt8500 serial device");
694 MODULE_LICENSE("GPL v2");