1 /* sunhv.c: Serial driver for SUN4V hypervisor console.
3 * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net)
6 #include <linux/kernel.h>
7 #include <linux/errno.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>
19 #include <linux/of_device.h>
21 #include <asm/hypervisor.h>
22 #include <asm/spitfire.h>
25 #include <asm/setup.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)
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);
101 if (port->state == NULL) {
102 uart_handle_sysrq_char(port, c);
108 if (uart_handle_sysrq_char(port, c))
111 tty_insert_flip_char(&port->state->port, c, TTY_NORMAL);
114 return saw_console_brk;
117 static int receive_chars_read(struct uart_port *port)
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]);
154 if (port->state == NULL)
157 port->icount.rx += bytes_read;
159 tty_insert_flip_string(&port->state->port, con_read_page,
163 return saw_console_brk;
167 void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit);
168 int (*receive_chars)(struct uart_port *port);
171 static struct sunhv_ops bychar_ops = {
172 .transmit_chars = transmit_chars_putchar,
173 .receive_chars = receive_chars_getchar,
176 static struct sunhv_ops bywrite_ops = {
177 .transmit_chars = transmit_chars_write,
178 .receive_chars = receive_chars_read,
181 static struct sunhv_ops *sunhv_ops = &bychar_ops;
183 static struct tty_port *receive_chars(struct uart_port *port)
185 struct tty_port *tport = NULL;
187 if (port->state != NULL) /* Unopened serial console */
188 tport = &port->state->port;
190 if (sunhv_ops->receive_chars(port))
196 static void transmit_chars(struct uart_port *port)
198 struct circ_buf *xmit;
203 xmit = &port->state->xmit;
204 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
207 sunhv_ops->transmit_chars(port, xmit);
209 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
210 uart_write_wakeup(port);
213 static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
215 struct uart_port *port = dev_id;
216 struct tty_port *tport;
219 spin_lock_irqsave(&port->lock, flags);
220 tport = receive_chars(port);
221 transmit_chars(port);
222 spin_unlock_irqrestore(&port->lock, flags);
225 tty_flip_buffer_push(tport);
230 /* port->lock is not held. */
231 static unsigned int sunhv_tx_empty(struct uart_port *port)
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.
240 /* port->lock held by caller. */
241 static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
246 /* port->lock is held by caller and interrupts are disabled. */
247 static unsigned int sunhv_get_mctrl(struct uart_port *port)
249 return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
252 /* port->lock held by caller. */
253 static void sunhv_stop_tx(struct uart_port *port)
258 /* port->lock held by caller. */
259 static void sunhv_start_tx(struct uart_port *port)
261 transmit_chars(port);
264 /* port->lock is not held. */
265 static void sunhv_send_xchar(struct uart_port *port, char ch)
270 if (ch == __DISABLED_CHAR)
273 spin_lock_irqsave(&port->lock, flags);
275 while (limit-- > 0) {
276 long status = sun4v_con_putchar(ch);
277 if (status == HV_EOK)
282 spin_unlock_irqrestore(&port->lock, flags);
285 /* port->lock held by caller. */
286 static void sunhv_stop_rx(struct uart_port *port)
290 /* port->lock is not held. */
291 static void sunhv_break_ctl(struct uart_port *port, int break_state)
297 spin_lock_irqsave(&port->lock, flags);
299 while (limit-- > 0) {
300 long status = sun4v_con_putchar(CON_BREAK);
301 if (status == HV_EOK)
306 spin_unlock_irqrestore(&port->lock, flags);
310 /* port->lock is not held. */
311 static int sunhv_startup(struct uart_port *port)
316 /* port->lock is not held. */
317 static void sunhv_shutdown(struct uart_port *port)
321 /* port->lock is not held. */
322 static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
323 struct ktermios *old)
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;
330 spin_lock_irqsave(&port->lock, flags);
332 iflag = termios->c_iflag;
333 cflag = termios->c_cflag;
335 port->ignore_status_mask = 0;
337 port->ignore_status_mask |= IGNORE_BREAK;
338 if ((cflag & CREAD) == 0)
339 port->ignore_status_mask |= IGNORE_ALL;
342 uart_update_timeout(port, cflag,
343 (port->uartclk / (16 * quot)));
345 spin_unlock_irqrestore(&port->lock, flags);
348 static const char *sunhv_type(struct uart_port *port)
350 return "SUN4V HCONS";
353 static void sunhv_release_port(struct uart_port *port)
357 static int sunhv_request_port(struct uart_port *port)
362 static void sunhv_config_port(struct uart_port *port, int flags)
366 static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
371 static 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,
379 .break_ctl = sunhv_break_ctl,
380 .startup = sunhv_startup,
381 .shutdown = sunhv_shutdown,
382 .set_termios = sunhv_set_termios,
384 .release_port = sunhv_release_port,
385 .request_port = sunhv_request_port,
386 .config_port = sunhv_config_port,
387 .verify_port = sunhv_verify_port,
390 static struct uart_driver sunhv_reg = {
391 .owner = THIS_MODULE,
392 .driver_name = "sunhv",
397 static struct uart_port *sunhv_port;
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.
404 static int fill_con_write_page(const char *s, unsigned int n,
405 unsigned long *page_bytes)
407 const char *orig_s = s;
408 char *p = con_write_page;
409 int left = PAGE_SIZE;
422 *page_bytes = p - con_write_page;
426 static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n)
428 struct uart_port *port = sunhv_port;
432 if (port->sysrq || oops_in_progress)
433 locked = spin_trylock_irqsave(&port->lock, flags);
435 spin_lock_irqsave(&port->lock, flags);
438 unsigned long ra = __pa(con_write_page);
439 unsigned long page_bytes;
440 unsigned int cpy = fill_con_write_page(s, n,
445 while (page_bytes > 0) {
446 unsigned long written;
452 stat = sun4v_con_write(ra, page_bytes,
460 page_bytes -= written;
466 spin_unlock_irqrestore(&port->lock, flags);
469 static inline void sunhv_console_putchar(struct uart_port *port, char c)
473 while (limit-- > 0) {
474 long status = sun4v_con_putchar(c);
475 if (status == HV_EOK)
481 static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n)
483 struct uart_port *port = sunhv_port;
487 if (port->sysrq || oops_in_progress)
488 locked = spin_trylock_irqsave(&port->lock, flags);
490 spin_lock_irqsave(&port->lock, flags);
493 } else if (oops_in_progress) {
494 locked = spin_trylock(&port->lock);
496 spin_lock(&port->lock);
498 for (i = 0; i < n; i++) {
500 sunhv_console_putchar(port, '\r');
501 sunhv_console_putchar(port, *s++);
505 spin_unlock_irqrestore(&port->lock, flags);
508 static struct console sunhv_console = {
510 .write = sunhv_console_write_bychar,
511 .device = uart_console_device,
512 .flags = CON_PRINTBUFFER,
517 static int hv_probe(struct platform_device *op)
519 struct uart_port *port;
523 if (op->archdata.irqs[0] == 0xffffffff)
526 port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
531 if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
534 con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
538 con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
540 goto out_free_con_write_page;
542 sunhv_console.write = sunhv_console_write_paged;
543 sunhv_ops = &bywrite_ops;
549 port->ops = &sunhv_pops;
550 port->type = PORT_SUNHV;
551 port->uartclk = ( 29491200 / 16 ); /* arbitrary */
553 port->membase = (unsigned char __iomem *) __pa(port);
555 port->irq = op->archdata.irqs[0];
557 port->dev = &op->dev;
559 err = sunserial_register_minors(&sunhv_reg, 1);
561 goto out_free_con_read_page;
563 sunserial_console_match(&sunhv_console, op->dev.of_node,
564 &sunhv_reg, port->line, false);
566 err = uart_add_one_port(&sunhv_reg, port);
568 goto out_unregister_driver;
570 err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
572 goto out_remove_port;
574 platform_set_drvdata(op, port);
579 uart_remove_one_port(&sunhv_reg, port);
581 out_unregister_driver:
582 sunserial_unregister_minors(&sunhv_reg, 1);
584 out_free_con_read_page:
585 kfree(con_read_page);
587 out_free_con_write_page:
588 kfree(con_write_page);
596 static int hv_remove(struct platform_device *dev)
598 struct uart_port *port = platform_get_drvdata(dev);
600 free_irq(port->irq, port);
602 uart_remove_one_port(&sunhv_reg, port);
604 sunserial_unregister_minors(&sunhv_reg, 1);
612 static const struct of_device_id hv_match[] = {
619 .compatible = "SUNW,sun4v-console",
624 static struct platform_driver hv_driver = {
627 .of_match_table = hv_match,
633 static int __init sunhv_init(void)
635 if (tlb_type != hypervisor)
638 return platform_driver_register(&hv_driver);
640 device_initcall(sunhv_init);
642 #if 0 /* ...def MODULE ; never supported as such */
643 MODULE_AUTHOR("David S. Miller");
644 MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
645 MODULE_VERSION("2.0");
646 MODULE_LICENSE("GPL");