Merge remote branch 'wireless-next/master' into ath6kl-next
[firefly-linux-kernel-4.4.55.git] / drivers / tty / serial / sunhv.c
1 /* sunhv.c: Serial driver for SUN4V hypervisor console.
2  *
3  * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net)
4  */
5
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
9 #include <linux/tty.h>
10 #include <linux/tty_flip.h>
11 #include <linux/major.h>
12 #include <linux/circ_buf.h>
13 #include <linux/serial.h>
14 #include <linux/sysrq.h>
15 #include <linux/console.h>
16 #include <linux/spinlock.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/of_device.h>
21
22 #include <asm/hypervisor.h>
23 #include <asm/spitfire.h>
24 #include <asm/prom.h>
25 #include <asm/irq.h>
26
27 #if defined(CONFIG_MAGIC_SYSRQ)
28 #define SUPPORT_SYSRQ
29 #endif
30
31 #include <linux/serial_core.h>
32 #include <linux/sunserialcore.h>
33
34 #define CON_BREAK       ((long)-1)
35 #define CON_HUP         ((long)-2)
36
37 #define IGNORE_BREAK    0x1
38 #define IGNORE_ALL      0x2
39
40 static char *con_write_page;
41 static char *con_read_page;
42
43 static int hung_up = 0;
44
45 static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit)
46 {
47         while (!uart_circ_empty(xmit)) {
48                 long status = sun4v_con_putchar(xmit->buf[xmit->tail]);
49
50                 if (status != HV_EOK)
51                         break;
52
53                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
54                 port->icount.tx++;
55         }
56 }
57
58 static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit)
59 {
60         while (!uart_circ_empty(xmit)) {
61                 unsigned long ra = __pa(xmit->buf + xmit->tail);
62                 unsigned long len, status, sent;
63
64                 len = CIRC_CNT_TO_END(xmit->head, xmit->tail,
65                                       UART_XMIT_SIZE);
66                 status = sun4v_con_write(ra, len, &sent);
67                 if (status != HV_EOK)
68                         break;
69                 xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1);
70                 port->icount.tx += sent;
71         }
72 }
73
74 static int receive_chars_getchar(struct uart_port *port, struct tty_struct *tty)
75 {
76         int saw_console_brk = 0;
77         int limit = 10000;
78
79         while (limit-- > 0) {
80                 long status;
81                 long c = sun4v_con_getchar(&status);
82
83                 if (status == HV_EWOULDBLOCK)
84                         break;
85
86                 if (c == CON_BREAK) {
87                         if (uart_handle_break(port))
88                                 continue;
89                         saw_console_brk = 1;
90                         c = 0;
91                 }
92
93                 if (c == CON_HUP) {
94                         hung_up = 1;
95                         uart_handle_dcd_change(port, 0);
96                 } else if (hung_up) {
97                         hung_up = 0;
98                         uart_handle_dcd_change(port, 1);
99                 }
100
101                 if (tty == NULL) {
102                         uart_handle_sysrq_char(port, c);
103                         continue;
104                 }
105
106                 port->icount.rx++;
107
108                 if (uart_handle_sysrq_char(port, c))
109                         continue;
110
111                 tty_insert_flip_char(tty, c, TTY_NORMAL);
112         }
113
114         return saw_console_brk;
115 }
116
117 static int receive_chars_read(struct uart_port *port, struct tty_struct *tty)
118 {
119         int saw_console_brk = 0;
120         int limit = 10000;
121
122         while (limit-- > 0) {
123                 unsigned long ra = __pa(con_read_page);
124                 unsigned long bytes_read, i;
125                 long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read);
126
127                 if (stat != HV_EOK) {
128                         bytes_read = 0;
129
130                         if (stat == CON_BREAK) {
131                                 if (uart_handle_break(port))
132                                         continue;
133                                 saw_console_brk = 1;
134                                 *con_read_page = 0;
135                                 bytes_read = 1;
136                         } else if (stat == CON_HUP) {
137                                 hung_up = 1;
138                                 uart_handle_dcd_change(port, 0);
139                                 continue;
140                         } else {
141                                 /* HV_EWOULDBLOCK, etc.  */
142                                 break;
143                         }
144                 }
145
146                 if (hung_up) {
147                         hung_up = 0;
148                         uart_handle_dcd_change(port, 1);
149                 }
150
151                 for (i = 0; i < bytes_read; i++)
152                         uart_handle_sysrq_char(port, con_read_page[i]);
153
154                 if (tty == NULL)
155                         continue;
156
157                 port->icount.rx += bytes_read;
158
159                 tty_insert_flip_string(tty, con_read_page, bytes_read);
160         }
161
162         return saw_console_brk;
163 }
164
165 struct sunhv_ops {
166         void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit);
167         int (*receive_chars)(struct uart_port *port, struct tty_struct *tty);
168 };
169
170 static struct sunhv_ops bychar_ops = {
171         .transmit_chars = transmit_chars_putchar,
172         .receive_chars = receive_chars_getchar,
173 };
174
175 static struct sunhv_ops bywrite_ops = {
176         .transmit_chars = transmit_chars_write,
177         .receive_chars = receive_chars_read,
178 };
179
180 static struct sunhv_ops *sunhv_ops = &bychar_ops;
181
182 static struct tty_struct *receive_chars(struct uart_port *port)
183 {
184         struct tty_struct *tty = NULL;
185
186         if (port->state != NULL)                /* Unopened serial console */
187                 tty = port->state->port.tty;
188
189         if (sunhv_ops->receive_chars(port, tty))
190                 sun_do_break();
191
192         return tty;
193 }
194
195 static void transmit_chars(struct uart_port *port)
196 {
197         struct circ_buf *xmit;
198
199         if (!port->state)
200                 return;
201
202         xmit = &port->state->xmit;
203         if (uart_circ_empty(xmit) || uart_tx_stopped(port))
204                 return;
205
206         sunhv_ops->transmit_chars(port, xmit);
207
208         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
209                 uart_write_wakeup(port);
210 }
211
212 static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
213 {
214         struct uart_port *port = dev_id;
215         struct tty_struct *tty;
216         unsigned long flags;
217
218         spin_lock_irqsave(&port->lock, flags);
219         tty = receive_chars(port);
220         transmit_chars(port);
221         spin_unlock_irqrestore(&port->lock, flags);
222
223         if (tty)
224                 tty_flip_buffer_push(tty);
225
226         return IRQ_HANDLED;
227 }
228
229 /* port->lock is not held.  */
230 static unsigned int sunhv_tx_empty(struct uart_port *port)
231 {
232         /* Transmitter is always empty for us.  If the circ buffer
233          * is non-empty or there is an x_char pending, our caller
234          * will do the right thing and ignore what we return here.
235          */
236         return TIOCSER_TEMT;
237 }
238
239 /* port->lock held by caller.  */
240 static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
241 {
242         return;
243 }
244
245 /* port->lock is held by caller and interrupts are disabled.  */
246 static unsigned int sunhv_get_mctrl(struct uart_port *port)
247 {
248         return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
249 }
250
251 /* port->lock held by caller.  */
252 static void sunhv_stop_tx(struct uart_port *port)
253 {
254         return;
255 }
256
257 /* port->lock held by caller.  */
258 static void sunhv_start_tx(struct uart_port *port)
259 {
260         transmit_chars(port);
261 }
262
263 /* port->lock is not held.  */
264 static void sunhv_send_xchar(struct uart_port *port, char ch)
265 {
266         unsigned long flags;
267         int limit = 10000;
268
269         spin_lock_irqsave(&port->lock, flags);
270
271         while (limit-- > 0) {
272                 long status = sun4v_con_putchar(ch);
273                 if (status == HV_EOK)
274                         break;
275                 udelay(1);
276         }
277
278         spin_unlock_irqrestore(&port->lock, flags);
279 }
280
281 /* port->lock held by caller.  */
282 static void sunhv_stop_rx(struct uart_port *port)
283 {
284 }
285
286 /* port->lock held by caller.  */
287 static void sunhv_enable_ms(struct uart_port *port)
288 {
289 }
290
291 /* port->lock is not held.  */
292 static void sunhv_break_ctl(struct uart_port *port, int break_state)
293 {
294         if (break_state) {
295                 unsigned long flags;
296                 int limit = 10000;
297
298                 spin_lock_irqsave(&port->lock, flags);
299
300                 while (limit-- > 0) {
301                         long status = sun4v_con_putchar(CON_BREAK);
302                         if (status == HV_EOK)
303                                 break;
304                         udelay(1);
305                 }
306
307                 spin_unlock_irqrestore(&port->lock, flags);
308         }
309 }
310
311 /* port->lock is not held.  */
312 static int sunhv_startup(struct uart_port *port)
313 {
314         return 0;
315 }
316
317 /* port->lock is not held.  */
318 static void sunhv_shutdown(struct uart_port *port)
319 {
320 }
321
322 /* port->lock is not held.  */
323 static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
324                               struct ktermios *old)
325 {
326         unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
327         unsigned int quot = uart_get_divisor(port, baud);
328         unsigned int iflag, cflag;
329         unsigned long flags;
330
331         spin_lock_irqsave(&port->lock, flags);
332
333         iflag = termios->c_iflag;
334         cflag = termios->c_cflag;
335
336         port->ignore_status_mask = 0;
337         if (iflag & IGNBRK)
338                 port->ignore_status_mask |= IGNORE_BREAK;
339         if ((cflag & CREAD) == 0)
340                 port->ignore_status_mask |= IGNORE_ALL;
341
342         /* XXX */
343         uart_update_timeout(port, cflag,
344                             (port->uartclk / (16 * quot)));
345
346         spin_unlock_irqrestore(&port->lock, flags);
347 }
348
349 static const char *sunhv_type(struct uart_port *port)
350 {
351         return "SUN4V HCONS";
352 }
353
354 static void sunhv_release_port(struct uart_port *port)
355 {
356 }
357
358 static int sunhv_request_port(struct uart_port *port)
359 {
360         return 0;
361 }
362
363 static void sunhv_config_port(struct uart_port *port, int flags)
364 {
365 }
366
367 static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
368 {
369         return -EINVAL;
370 }
371
372 static struct uart_ops sunhv_pops = {
373         .tx_empty       = sunhv_tx_empty,
374         .set_mctrl      = sunhv_set_mctrl,
375         .get_mctrl      = sunhv_get_mctrl,
376         .stop_tx        = sunhv_stop_tx,
377         .start_tx       = sunhv_start_tx,
378         .send_xchar     = sunhv_send_xchar,
379         .stop_rx        = sunhv_stop_rx,
380         .enable_ms      = sunhv_enable_ms,
381         .break_ctl      = sunhv_break_ctl,
382         .startup        = sunhv_startup,
383         .shutdown       = sunhv_shutdown,
384         .set_termios    = sunhv_set_termios,
385         .type           = sunhv_type,
386         .release_port   = sunhv_release_port,
387         .request_port   = sunhv_request_port,
388         .config_port    = sunhv_config_port,
389         .verify_port    = sunhv_verify_port,
390 };
391
392 static struct uart_driver sunhv_reg = {
393         .owner                  = THIS_MODULE,
394         .driver_name            = "sunhv",
395         .dev_name               = "ttyS",
396         .major                  = TTY_MAJOR,
397 };
398
399 static struct uart_port *sunhv_port;
400
401 /* Copy 's' into the con_write_page, decoding "\n" into
402  * "\r\n" along the way.  We have to return two lengths
403  * because the caller needs to know how much to advance
404  * 's' and also how many bytes to output via con_write_page.
405  */
406 static int fill_con_write_page(const char *s, unsigned int n,
407                                unsigned long *page_bytes)
408 {
409         const char *orig_s = s;
410         char *p = con_write_page;
411         int left = PAGE_SIZE;
412
413         while (n--) {
414                 if (*s == '\n') {
415                         if (left < 2)
416                                 break;
417                         *p++ = '\r';
418                         left--;
419                 } else if (left < 1)
420                         break;
421                 *p++ = *s++;
422                 left--;
423         }
424         *page_bytes = p - con_write_page;
425         return s - orig_s;
426 }
427
428 static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n)
429 {
430         struct uart_port *port = sunhv_port;
431         unsigned long flags;
432         int locked = 1;
433
434         local_irq_save(flags);
435         if (port->sysrq) {
436                 locked = 0;
437         } else if (oops_in_progress) {
438                 locked = spin_trylock(&port->lock);
439         } else
440                 spin_lock(&port->lock);
441
442         while (n > 0) {
443                 unsigned long ra = __pa(con_write_page);
444                 unsigned long page_bytes;
445                 unsigned int cpy = fill_con_write_page(s, n,
446                                                        &page_bytes);
447
448                 n -= cpy;
449                 s += cpy;
450                 while (page_bytes > 0) {
451                         unsigned long written;
452                         int limit = 1000000;
453
454                         while (limit--) {
455                                 unsigned long stat;
456
457                                 stat = sun4v_con_write(ra, page_bytes,
458                                                        &written);
459                                 if (stat == HV_EOK)
460                                         break;
461                                 udelay(1);
462                         }
463                         if (limit < 0)
464                                 break;
465                         page_bytes -= written;
466                         ra += written;
467                 }
468         }
469
470         if (locked)
471                 spin_unlock(&port->lock);
472         local_irq_restore(flags);
473 }
474
475 static inline void sunhv_console_putchar(struct uart_port *port, char c)
476 {
477         int limit = 1000000;
478
479         while (limit-- > 0) {
480                 long status = sun4v_con_putchar(c);
481                 if (status == HV_EOK)
482                         break;
483                 udelay(1);
484         }
485 }
486
487 static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n)
488 {
489         struct uart_port *port = sunhv_port;
490         unsigned long flags;
491         int i, locked = 1;
492
493         local_irq_save(flags);
494         if (port->sysrq) {
495                 locked = 0;
496         } else if (oops_in_progress) {
497                 locked = spin_trylock(&port->lock);
498         } else
499                 spin_lock(&port->lock);
500
501         for (i = 0; i < n; i++) {
502                 if (*s == '\n')
503                         sunhv_console_putchar(port, '\r');
504                 sunhv_console_putchar(port, *s++);
505         }
506
507         if (locked)
508                 spin_unlock(&port->lock);
509         local_irq_restore(flags);
510 }
511
512 static struct console sunhv_console = {
513         .name   =       "ttyHV",
514         .write  =       sunhv_console_write_bychar,
515         .device =       uart_console_device,
516         .flags  =       CON_PRINTBUFFER,
517         .index  =       -1,
518         .data   =       &sunhv_reg,
519 };
520
521 static int __devinit hv_probe(struct platform_device *op)
522 {
523         struct uart_port *port;
524         unsigned long minor;
525         int err;
526
527         if (op->archdata.irqs[0] == 0xffffffff)
528                 return -ENODEV;
529
530         port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
531         if (unlikely(!port))
532                 return -ENOMEM;
533
534         minor = 1;
535         if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
536             minor >= 1) {
537                 err = -ENOMEM;
538                 con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
539                 if (!con_write_page)
540                         goto out_free_port;
541
542                 con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
543                 if (!con_read_page)
544                         goto out_free_con_write_page;
545
546                 sunhv_console.write = sunhv_console_write_paged;
547                 sunhv_ops = &bywrite_ops;
548         }
549
550         sunhv_port = port;
551
552         port->line = 0;
553         port->ops = &sunhv_pops;
554         port->type = PORT_SUNHV;
555         port->uartclk = ( 29491200 / 16 ); /* arbitrary */
556
557         port->membase = (unsigned char __iomem *) __pa(port);
558
559         port->irq = op->archdata.irqs[0];
560
561         port->dev = &op->dev;
562
563         err = sunserial_register_minors(&sunhv_reg, 1);
564         if (err)
565                 goto out_free_con_read_page;
566
567         sunserial_console_match(&sunhv_console, op->dev.of_node,
568                                 &sunhv_reg, port->line, false);
569
570         err = uart_add_one_port(&sunhv_reg, port);
571         if (err)
572                 goto out_unregister_driver;
573
574         err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
575         if (err)
576                 goto out_remove_port;
577
578         dev_set_drvdata(&op->dev, port);
579
580         return 0;
581
582 out_remove_port:
583         uart_remove_one_port(&sunhv_reg, port);
584
585 out_unregister_driver:
586         sunserial_unregister_minors(&sunhv_reg, 1);
587
588 out_free_con_read_page:
589         kfree(con_read_page);
590
591 out_free_con_write_page:
592         kfree(con_write_page);
593
594 out_free_port:
595         kfree(port);
596         sunhv_port = NULL;
597         return err;
598 }
599
600 static int __devexit hv_remove(struct platform_device *dev)
601 {
602         struct uart_port *port = dev_get_drvdata(&dev->dev);
603
604         free_irq(port->irq, port);
605
606         uart_remove_one_port(&sunhv_reg, port);
607
608         sunserial_unregister_minors(&sunhv_reg, 1);
609
610         kfree(port);
611         sunhv_port = NULL;
612
613         dev_set_drvdata(&dev->dev, NULL);
614
615         return 0;
616 }
617
618 static const struct of_device_id hv_match[] = {
619         {
620                 .name = "console",
621                 .compatible = "qcn",
622         },
623         {
624                 .name = "console",
625                 .compatible = "SUNW,sun4v-console",
626         },
627         {},
628 };
629 MODULE_DEVICE_TABLE(of, hv_match);
630
631 static struct platform_driver hv_driver = {
632         .driver = {
633                 .name = "hv",
634                 .owner = THIS_MODULE,
635                 .of_match_table = hv_match,
636         },
637         .probe          = hv_probe,
638         .remove         = __devexit_p(hv_remove),
639 };
640
641 static int __init sunhv_init(void)
642 {
643         if (tlb_type != hypervisor)
644                 return -ENODEV;
645
646         return platform_driver_register(&hv_driver);
647 }
648
649 static void __exit sunhv_exit(void)
650 {
651         platform_driver_unregister(&hv_driver);
652 }
653
654 module_init(sunhv_init);
655 module_exit(sunhv_exit);
656
657 MODULE_AUTHOR("David S. Miller");
658 MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
659 MODULE_VERSION("2.0");
660 MODULE_LICENSE("GPL");