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