1 /* sunhv.c: Serial driver for SUN4V hypervisor console.
3 * Copyright (C) 2006 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>
21 #include <asm/hypervisor.h>
22 #include <asm/spitfire.h>
24 #include <asm/oplib.h>
27 #if defined(CONFIG_MAGIC_SYSRQ)
31 #include <linux/serial_core.h>
35 #define CON_BREAK ((long)-1)
36 #define CON_HUP ((long)-2)
38 static inline long hypervisor_con_getchar(long *status)
40 register unsigned long func asm("%o5");
41 register unsigned long arg0 asm("%o0");
42 register unsigned long arg1 asm("%o1");
44 func = HV_FAST_CONS_GETCHAR;
47 __asm__ __volatile__("ta %6"
48 : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
49 : "0" (func), "1" (arg0), "2" (arg1),
57 static inline long hypervisor_con_putchar(long ch)
59 register unsigned long func asm("%o5");
60 register unsigned long arg0 asm("%o0");
62 func = HV_FAST_CONS_PUTCHAR;
64 __asm__ __volatile__("ta %4"
65 : "=&r" (func), "=&r" (arg0)
66 : "0" (func), "1" (arg0), "i" (HV_FAST_TRAP));
71 #define IGNORE_BREAK 0x1
72 #define IGNORE_ALL 0x2
74 static int hung_up = 0;
76 static struct tty_struct *receive_chars(struct uart_port *port, struct pt_regs *regs)
78 struct tty_struct *tty = NULL;
79 int saw_console_brk = 0;
82 if (port->info != NULL) /* Unopened serial console */
83 tty = port->info->tty;
87 long c = hypervisor_con_getchar(&status);
90 if (status == HV_EWOULDBLOCK)
100 uart_handle_dcd_change(port, 0);
101 } else if (hung_up) {
103 uart_handle_dcd_change(port, 1);
107 uart_handle_sysrq_char(port, c, regs);
113 if (c == CON_BREAK) {
115 if (uart_handle_break(port))
120 if (uart_handle_sysrq_char(port, c, regs))
123 if ((port->ignore_status_mask & IGNORE_ALL) ||
124 ((port->ignore_status_mask & IGNORE_BREAK) &&
128 tty_insert_flip_char(tty, c, flag);
137 static void transmit_chars(struct uart_port *port)
139 struct circ_buf *xmit;
144 xmit = &port->info->xmit;
145 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
148 while (!uart_circ_empty(xmit)) {
149 long status = hypervisor_con_putchar(xmit->buf[xmit->tail]);
151 if (status != HV_EOK)
154 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
158 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
159 uart_write_wakeup(port);
162 static irqreturn_t sunhv_interrupt(int irq, void *dev_id, struct pt_regs *regs)
164 struct uart_port *port = dev_id;
165 struct tty_struct *tty;
168 spin_lock_irqsave(&port->lock, flags);
169 tty = receive_chars(port, regs);
170 transmit_chars(port);
171 spin_unlock_irqrestore(&port->lock, flags);
174 tty_flip_buffer_push(tty);
179 /* port->lock is not held. */
180 static unsigned int sunhv_tx_empty(struct uart_port *port)
182 /* Transmitter is always empty for us. If the circ buffer
183 * is non-empty or there is an x_char pending, our caller
184 * will do the right thing and ignore what we return here.
189 /* port->lock held by caller. */
190 static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
195 /* port->lock is held by caller and interrupts are disabled. */
196 static unsigned int sunhv_get_mctrl(struct uart_port *port)
198 return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
201 /* port->lock held by caller. */
202 static void sunhv_stop_tx(struct uart_port *port)
207 /* port->lock held by caller. */
208 static void sunhv_start_tx(struct uart_port *port)
210 struct circ_buf *xmit = &port->info->xmit;
213 spin_lock_irqsave(&port->lock, flags);
215 while (!uart_circ_empty(xmit)) {
216 long status = hypervisor_con_putchar(xmit->buf[xmit->tail]);
218 if (status != HV_EOK)
221 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
225 spin_unlock_irqrestore(&port->lock, flags);
228 /* port->lock is not held. */
229 static void sunhv_send_xchar(struct uart_port *port, char ch)
234 spin_lock_irqsave(&port->lock, flags);
236 while (limit-- > 0) {
237 long status = hypervisor_con_putchar(ch);
238 if (status == HV_EOK)
242 spin_unlock_irqrestore(&port->lock, flags);
245 /* port->lock held by caller. */
246 static void sunhv_stop_rx(struct uart_port *port)
250 /* port->lock held by caller. */
251 static void sunhv_enable_ms(struct uart_port *port)
255 /* port->lock is not held. */
256 static void sunhv_break_ctl(struct uart_port *port, int break_state)
262 spin_lock_irqsave(&port->lock, flags);
264 while (limit-- > 0) {
265 long status = hypervisor_con_putchar(CON_BREAK);
266 if (status == HV_EOK)
270 spin_unlock_irqrestore(&port->lock, flags);
274 /* port->lock is not held. */
275 static int sunhv_startup(struct uart_port *port)
280 /* port->lock is not held. */
281 static void sunhv_shutdown(struct uart_port *port)
285 /* port->lock is not held. */
286 static void sunhv_set_termios(struct uart_port *port, struct termios *termios,
289 unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
290 unsigned int quot = uart_get_divisor(port, baud);
291 unsigned int iflag, cflag;
294 spin_lock_irqsave(&port->lock, flags);
296 iflag = termios->c_iflag;
297 cflag = termios->c_cflag;
299 port->ignore_status_mask = 0;
301 port->ignore_status_mask |= IGNORE_BREAK;
302 if ((cflag & CREAD) == 0)
303 port->ignore_status_mask |= IGNORE_ALL;
306 uart_update_timeout(port, cflag,
307 (port->uartclk / (16 * quot)));
309 spin_unlock_irqrestore(&port->lock, flags);
312 static const char *sunhv_type(struct uart_port *port)
314 return "SUN4V HCONS";
317 static void sunhv_release_port(struct uart_port *port)
321 static int sunhv_request_port(struct uart_port *port)
326 static void sunhv_config_port(struct uart_port *port, int flags)
330 static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
335 static struct uart_ops sunhv_pops = {
336 .tx_empty = sunhv_tx_empty,
337 .set_mctrl = sunhv_set_mctrl,
338 .get_mctrl = sunhv_get_mctrl,
339 .stop_tx = sunhv_stop_tx,
340 .start_tx = sunhv_start_tx,
341 .send_xchar = sunhv_send_xchar,
342 .stop_rx = sunhv_stop_rx,
343 .enable_ms = sunhv_enable_ms,
344 .break_ctl = sunhv_break_ctl,
345 .startup = sunhv_startup,
346 .shutdown = sunhv_shutdown,
347 .set_termios = sunhv_set_termios,
349 .release_port = sunhv_release_port,
350 .request_port = sunhv_request_port,
351 .config_port = sunhv_config_port,
352 .verify_port = sunhv_verify_port,
355 static struct uart_driver sunhv_reg = {
356 .owner = THIS_MODULE,
357 .driver_name = "serial",
358 .devfs_name = "tts/",
363 static struct uart_port *sunhv_port;
365 static inline void sunhv_console_putchar(struct uart_port *port, char c)
370 spin_lock_irqsave(&port->lock, flags);
372 while (limit-- > 0) {
373 long status = hypervisor_con_putchar(c);
374 if (status == HV_EOK)
379 spin_unlock_irqrestore(&port->lock, flags);
382 static void sunhv_console_write(struct console *con, const char *s, unsigned n)
384 struct uart_port *port = sunhv_port;
387 for (i = 0; i < n; i++) {
389 sunhv_console_putchar(port, '\r');
390 sunhv_console_putchar(port, *s++);
394 static struct console sunhv_console = {
396 .write = sunhv_console_write,
397 .device = uart_console_device,
398 .flags = CON_PRINTBUFFER,
403 static inline struct console *SUNHV_CONSOLE(void)
405 if (con_is_present())
408 sunhv_console.index = 0;
410 return &sunhv_console;
413 static int __init hv_console_compatible(char *buf, int len)
418 if (!strcmp(buf, "qcn"))
421 this_len = strlen(buf) + 1;
430 static unsigned int __init get_interrupt(void)
432 const char *cons_str = "console";
433 const char *compat_str = "compatible";
434 int node = prom_getchild(sun4v_vdev_root);
438 node = prom_searchsiblings(node, cons_str);
442 len = prom_getproplen(node, compat_str);
443 if (len == 0 || len == -1)
446 err = prom_getproperty(node, compat_str, buf, 64);
450 if (!hv_console_compatible(buf, len))
453 /* Ok, the this is the OBP node for the sun4v hypervisor
454 * console device. Decode the interrupt.
456 return sun4v_vdev_device_interrupt(node);
459 static int __init sunhv_init(void)
461 struct uart_port *port;
464 if (tlb_type != hypervisor)
467 port = kmalloc(sizeof(struct uart_port), GFP_KERNEL);
471 memset(port, 0, sizeof(struct uart_port));
474 port->ops = &sunhv_pops;
475 port->type = PORT_SUNHV;
476 port->uartclk = ( 29491200 / 16 ); /* arbitrary */
478 /* Set this just to make uart_configure_port() happy. */
479 port->membase = (unsigned char __iomem *) __pa(port);
481 port->irq = get_interrupt();
487 sunhv_reg.minor = sunserial_current_minor;
490 ret = uart_register_driver(&sunhv_reg);
492 printk(KERN_ERR "SUNHV: uart_register_driver() failed %d\n",
499 sunserial_current_minor += 1;
501 sunhv_reg.cons = SUNHV_CONSOLE();
505 ret = uart_add_one_port(&sunhv_reg, port);
507 printk(KERN_ERR "SUNHV: uart_add_one_port() failed %d\n", ret);
508 sunserial_current_minor -= 1;
509 uart_unregister_driver(&sunhv_reg);
515 if (request_irq(port->irq, sunhv_interrupt,
516 SA_SHIRQ, "serial(sunhv)", port)) {
517 printk(KERN_ERR "sunhv: Cannot register IRQ\n");
518 uart_remove_one_port(&sunhv_reg, port);
519 sunserial_current_minor -= 1;
520 uart_unregister_driver(&sunhv_reg);
529 static void __exit sunhv_exit(void)
531 struct uart_port *port = sunhv_port;
535 free_irq(port->irq, port);
537 uart_remove_one_port(&sunhv_reg, port);
538 sunserial_current_minor -= 1;
540 uart_unregister_driver(&sunhv_reg);
546 module_init(sunhv_init);
547 module_exit(sunhv_exit);
549 MODULE_AUTHOR("David S. Miller");
550 MODULE_DESCRIPTION("SUN4V Hypervisor console driver")
551 MODULE_LICENSE("GPL");