1 /* sunhv.c: Serial driver for SUN4V hypervisor console.
3 * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net)
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/errno.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>
22 #include <asm/hypervisor.h>
23 #include <asm/spitfire.h>
27 #if defined(CONFIG_MAGIC_SYSRQ)
31 #include <linux/serial_core.h>
32 #include <linux/sunserialcore.h>
34 #define CON_BREAK ((long)-1)
35 #define CON_HUP ((long)-2)
37 #define IGNORE_BREAK 0x1
38 #define IGNORE_ALL 0x2
40 static char *con_write_page;
41 static char *con_read_page;
43 static int hung_up = 0;
45 static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit)
47 while (!uart_circ_empty(xmit)) {
48 long status = sun4v_con_putchar(xmit->buf[xmit->tail]);
53 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
58 static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit)
60 while (!uart_circ_empty(xmit)) {
61 unsigned long ra = __pa(xmit->buf + xmit->tail);
62 unsigned long len, status, sent;
64 len = CIRC_CNT_TO_END(xmit->head, xmit->tail,
66 status = sun4v_con_write(ra, len, &sent);
69 xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1);
70 port->icount.tx += sent;
74 static int receive_chars_getchar(struct uart_port *port, struct tty_struct *tty)
76 int saw_console_brk = 0;
81 long c = sun4v_con_getchar(&status);
83 if (status == HV_EWOULDBLOCK)
87 if (uart_handle_break(port))
95 uart_handle_dcd_change(port, 0);
98 uart_handle_dcd_change(port, 1);
102 uart_handle_sysrq_char(port, c);
108 if (uart_handle_sysrq_char(port, c))
111 tty_insert_flip_char(tty, c, TTY_NORMAL);
114 return saw_console_brk;
117 static int receive_chars_read(struct uart_port *port, struct tty_struct *tty)
119 int saw_console_brk = 0;
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);
127 if (stat != HV_EOK) {
130 if (stat == CON_BREAK) {
131 if (uart_handle_break(port))
136 } else if (stat == CON_HUP) {
138 uart_handle_dcd_change(port, 0);
141 /* HV_EWOULDBLOCK, etc. */
148 uart_handle_dcd_change(port, 1);
151 for (i = 0; i < bytes_read; i++)
152 uart_handle_sysrq_char(port, con_read_page[i]);
157 port->icount.rx += bytes_read;
159 tty_insert_flip_string(tty, con_read_page, bytes_read);
162 return saw_console_brk;
166 void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit);
167 int (*receive_chars)(struct uart_port *port, struct tty_struct *tty);
170 static struct sunhv_ops bychar_ops = {
171 .transmit_chars = transmit_chars_putchar,
172 .receive_chars = receive_chars_getchar,
175 static struct sunhv_ops bywrite_ops = {
176 .transmit_chars = transmit_chars_write,
177 .receive_chars = receive_chars_read,
180 static struct sunhv_ops *sunhv_ops = &bychar_ops;
182 static struct tty_struct *receive_chars(struct uart_port *port)
184 struct tty_struct *tty = NULL;
186 if (port->state != NULL) /* Unopened serial console */
187 tty = port->state->port.tty;
189 if (sunhv_ops->receive_chars(port, tty))
195 static void transmit_chars(struct uart_port *port)
197 struct circ_buf *xmit;
202 xmit = &port->state->xmit;
203 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
206 sunhv_ops->transmit_chars(port, xmit);
208 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
209 uart_write_wakeup(port);
212 static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
214 struct uart_port *port = dev_id;
215 struct tty_struct *tty;
218 spin_lock_irqsave(&port->lock, flags);
219 tty = receive_chars(port);
220 transmit_chars(port);
221 spin_unlock_irqrestore(&port->lock, flags);
224 tty_flip_buffer_push(tty);
229 /* port->lock is not held. */
230 static unsigned int sunhv_tx_empty(struct uart_port *port)
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.
239 /* port->lock held by caller. */
240 static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
245 /* port->lock is held by caller and interrupts are disabled. */
246 static unsigned int sunhv_get_mctrl(struct uart_port *port)
248 return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
251 /* port->lock held by caller. */
252 static void sunhv_stop_tx(struct uart_port *port)
257 /* port->lock held by caller. */
258 static void sunhv_start_tx(struct uart_port *port)
260 transmit_chars(port);
263 /* port->lock is not held. */
264 static void sunhv_send_xchar(struct uart_port *port, char ch)
269 spin_lock_irqsave(&port->lock, flags);
271 while (limit-- > 0) {
272 long status = sun4v_con_putchar(ch);
273 if (status == HV_EOK)
278 spin_unlock_irqrestore(&port->lock, flags);
281 /* port->lock held by caller. */
282 static void sunhv_stop_rx(struct uart_port *port)
286 /* port->lock held by caller. */
287 static void sunhv_enable_ms(struct uart_port *port)
291 /* port->lock is not held. */
292 static void sunhv_break_ctl(struct uart_port *port, int break_state)
298 spin_lock_irqsave(&port->lock, flags);
300 while (limit-- > 0) {
301 long status = sun4v_con_putchar(CON_BREAK);
302 if (status == HV_EOK)
307 spin_unlock_irqrestore(&port->lock, flags);
311 /* port->lock is not held. */
312 static int sunhv_startup(struct uart_port *port)
317 /* port->lock is not held. */
318 static void sunhv_shutdown(struct uart_port *port)
322 /* port->lock is not held. */
323 static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
324 struct ktermios *old)
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;
331 spin_lock_irqsave(&port->lock, flags);
333 iflag = termios->c_iflag;
334 cflag = termios->c_cflag;
336 port->ignore_status_mask = 0;
338 port->ignore_status_mask |= IGNORE_BREAK;
339 if ((cflag & CREAD) == 0)
340 port->ignore_status_mask |= IGNORE_ALL;
343 uart_update_timeout(port, cflag,
344 (port->uartclk / (16 * quot)));
346 spin_unlock_irqrestore(&port->lock, flags);
349 static const char *sunhv_type(struct uart_port *port)
351 return "SUN4V HCONS";
354 static void sunhv_release_port(struct uart_port *port)
358 static int sunhv_request_port(struct uart_port *port)
363 static void sunhv_config_port(struct uart_port *port, int flags)
367 static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
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,
386 .release_port = sunhv_release_port,
387 .request_port = sunhv_request_port,
388 .config_port = sunhv_config_port,
389 .verify_port = sunhv_verify_port,
392 static struct uart_driver sunhv_reg = {
393 .owner = THIS_MODULE,
394 .driver_name = "sunhv",
399 static struct uart_port *sunhv_port;
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.
406 static int fill_con_write_page(const char *s, unsigned int n,
407 unsigned long *page_bytes)
409 const char *orig_s = s;
410 char *p = con_write_page;
411 int left = PAGE_SIZE;
424 *page_bytes = p - con_write_page;
428 static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n)
430 struct uart_port *port = sunhv_port;
434 local_irq_save(flags);
437 } else if (oops_in_progress) {
438 locked = spin_trylock(&port->lock);
440 spin_lock(&port->lock);
443 unsigned long ra = __pa(con_write_page);
444 unsigned long page_bytes;
445 unsigned int cpy = fill_con_write_page(s, n,
450 while (page_bytes > 0) {
451 unsigned long written;
457 stat = sun4v_con_write(ra, page_bytes,
465 page_bytes -= written;
471 spin_unlock(&port->lock);
472 local_irq_restore(flags);
475 static inline void sunhv_console_putchar(struct uart_port *port, char c)
479 while (limit-- > 0) {
480 long status = sun4v_con_putchar(c);
481 if (status == HV_EOK)
487 static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n)
489 struct uart_port *port = sunhv_port;
493 local_irq_save(flags);
496 } else if (oops_in_progress) {
497 locked = spin_trylock(&port->lock);
499 spin_lock(&port->lock);
501 for (i = 0; i < n; i++) {
503 sunhv_console_putchar(port, '\r');
504 sunhv_console_putchar(port, *s++);
508 spin_unlock(&port->lock);
509 local_irq_restore(flags);
512 static struct console sunhv_console = {
514 .write = sunhv_console_write_bychar,
515 .device = uart_console_device,
516 .flags = CON_PRINTBUFFER,
521 static int __devinit hv_probe(struct platform_device *op)
523 struct uart_port *port;
527 if (op->archdata.irqs[0] == 0xffffffff)
530 port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
535 if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
538 con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
542 con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
544 goto out_free_con_write_page;
546 sunhv_console.write = sunhv_console_write_paged;
547 sunhv_ops = &bywrite_ops;
553 port->ops = &sunhv_pops;
554 port->type = PORT_SUNHV;
555 port->uartclk = ( 29491200 / 16 ); /* arbitrary */
557 port->membase = (unsigned char __iomem *) __pa(port);
559 port->irq = op->archdata.irqs[0];
561 port->dev = &op->dev;
563 err = sunserial_register_minors(&sunhv_reg, 1);
565 goto out_free_con_read_page;
567 sunserial_console_match(&sunhv_console, op->dev.of_node,
568 &sunhv_reg, port->line, false);
570 err = uart_add_one_port(&sunhv_reg, port);
572 goto out_unregister_driver;
574 err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
576 goto out_remove_port;
578 dev_set_drvdata(&op->dev, port);
583 uart_remove_one_port(&sunhv_reg, port);
585 out_unregister_driver:
586 sunserial_unregister_minors(&sunhv_reg, 1);
588 out_free_con_read_page:
589 kfree(con_read_page);
591 out_free_con_write_page:
592 kfree(con_write_page);
600 static int __devexit hv_remove(struct platform_device *dev)
602 struct uart_port *port = dev_get_drvdata(&dev->dev);
604 free_irq(port->irq, port);
606 uart_remove_one_port(&sunhv_reg, port);
608 sunserial_unregister_minors(&sunhv_reg, 1);
613 dev_set_drvdata(&dev->dev, NULL);
618 static const struct of_device_id hv_match[] = {
625 .compatible = "SUNW,sun4v-console",
629 MODULE_DEVICE_TABLE(of, hv_match);
631 static struct platform_driver hv_driver = {
634 .owner = THIS_MODULE,
635 .of_match_table = hv_match,
638 .remove = __devexit_p(hv_remove),
641 static int __init sunhv_init(void)
643 if (tlb_type != hypervisor)
646 return platform_driver_register(&hv_driver);
649 static void __exit sunhv_exit(void)
651 platform_driver_unregister(&hv_driver);
654 module_init(sunhv_init);
655 module_exit(sunhv_exit);
657 MODULE_AUTHOR("David S. Miller");
658 MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
659 MODULE_VERSION("2.0");
660 MODULE_LICENSE("GPL");