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 #if defined(CONFIG_MAGIC_SYSRQ)
28 #include <linux/serial_core.h>
32 #define CON_BREAK ((long)-1)
33 #define CON_HUP ((long)-2)
35 static inline long hypervisor_con_getchar(long *status)
37 register unsigned long func asm("%o5");
38 register unsigned long arg0 asm("%o0");
39 register unsigned long arg1 asm("%o1");
41 func = HV_FAST_CONS_GETCHAR;
44 __asm__ __volatile__("ta %6"
45 : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
46 : "0" (func), "1" (arg0), "2" (arg1),
54 static inline long hypervisor_con_putchar(long ch)
56 register unsigned long func asm("%o5");
57 register unsigned long arg0 asm("%o0");
59 func = HV_FAST_CONS_PUTCHAR;
61 __asm__ __volatile__("ta %4"
62 : "=&r" (func), "=&r" (arg0)
63 : "0" (func), "1" (arg0), "i" (HV_FAST_TRAP));
68 #define IGNORE_BREAK 0x1
69 #define IGNORE_ALL 0x2
71 static int hung_up = 0;
73 static struct tty_struct *receive_chars(struct uart_port *port, struct pt_regs *regs)
75 struct tty_struct *tty = NULL;
76 int saw_console_brk = 0;
79 if (port->info != NULL) /* Unopened serial console */
80 tty = port->info->tty;
84 long c = hypervisor_con_getchar(&status);
87 if (status == HV_EWOULDBLOCK)
97 uart_handle_dcd_change(port, 0);
100 uart_handle_dcd_change(port, 1);
104 uart_handle_sysrq_char(port, c, regs);
110 if (c == CON_BREAK) {
112 if (uart_handle_break(port))
117 if (uart_handle_sysrq_char(port, c, regs))
120 if ((port->ignore_status_mask & IGNORE_ALL) ||
121 ((port->ignore_status_mask & IGNORE_BREAK) &&
125 tty_insert_flip_char(tty, c, flag);
134 static void transmit_chars(struct uart_port *port)
136 struct circ_buf *xmit = &port->info->xmit;
138 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
141 while (!uart_circ_empty(xmit)) {
142 long status = hypervisor_con_putchar(xmit->buf[xmit->tail]);
144 if (status != HV_EOK)
147 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
151 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
152 uart_write_wakeup(port);
155 static irqreturn_t sunhv_interrupt(int irq, void *dev_id, struct pt_regs *regs)
157 struct uart_port *port = dev_id;
158 struct tty_struct *tty;
161 spin_lock_irqsave(&port->lock, flags);
162 tty = receive_chars(port, regs);
163 transmit_chars(port);
164 spin_unlock_irqrestore(&port->lock, flags);
167 tty_flip_buffer_push(tty);
172 /* port->lock is not held. */
173 static unsigned int sunhv_tx_empty(struct uart_port *port)
175 /* Transmitter is always empty for us. If the circ buffer
176 * is non-empty or there is an x_char pending, our caller
177 * will do the right thing and ignore what we return here.
182 /* port->lock held by caller. */
183 static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
188 /* port->lock is held by caller and interrupts are disabled. */
189 static unsigned int sunhv_get_mctrl(struct uart_port *port)
191 return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
194 /* port->lock held by caller. */
195 static void sunhv_stop_tx(struct uart_port *port)
200 /* port->lock held by caller. */
201 static void sunhv_start_tx(struct uart_port *port)
203 struct circ_buf *xmit = &port->info->xmit;
206 spin_lock_irqsave(&port->lock, flags);
208 while (!uart_circ_empty(xmit)) {
209 long status = hypervisor_con_putchar(xmit->buf[xmit->tail]);
211 if (status != HV_EOK)
214 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
218 spin_unlock_irqrestore(&port->lock, flags);
221 /* port->lock is not held. */
222 static void sunhv_send_xchar(struct uart_port *port, char ch)
227 spin_lock_irqsave(&port->lock, flags);
229 while (limit-- > 0) {
230 long status = hypervisor_con_putchar(ch);
231 if (status == HV_EOK)
235 spin_unlock_irqrestore(&port->lock, flags);
238 /* port->lock held by caller. */
239 static void sunhv_stop_rx(struct uart_port *port)
243 /* port->lock held by caller. */
244 static void sunhv_enable_ms(struct uart_port *port)
248 /* port->lock is not held. */
249 static void sunhv_break_ctl(struct uart_port *port, int break_state)
255 spin_lock_irqsave(&port->lock, flags);
257 while (limit-- > 0) {
258 long status = hypervisor_con_putchar(CON_BREAK);
259 if (status == HV_EOK)
263 spin_unlock_irqrestore(&port->lock, flags);
267 /* port->lock is not held. */
268 static int sunhv_startup(struct uart_port *port)
273 /* port->lock is not held. */
274 static void sunhv_shutdown(struct uart_port *port)
278 /* port->lock is not held. */
279 static void sunhv_set_termios(struct uart_port *port, struct termios *termios,
282 unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
283 unsigned int quot = uart_get_divisor(port, baud);
284 unsigned int iflag, cflag;
287 spin_lock_irqsave(&port->lock, flags);
289 iflag = termios->c_iflag;
290 cflag = termios->c_cflag;
292 port->ignore_status_mask = 0;
294 port->ignore_status_mask |= IGNORE_BREAK;
295 if ((cflag & CREAD) == 0)
296 port->ignore_status_mask |= IGNORE_ALL;
299 uart_update_timeout(port, cflag,
300 (port->uartclk / (16 * quot)));
302 spin_unlock_irqrestore(&port->lock, flags);
305 static const char *sunhv_type(struct uart_port *port)
307 return "SUN4V HCONS";
310 static void sunhv_release_port(struct uart_port *port)
314 static int sunhv_request_port(struct uart_port *port)
319 static void sunhv_config_port(struct uart_port *port, int flags)
323 static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
328 static struct uart_ops sunhv_pops = {
329 .tx_empty = sunhv_tx_empty,
330 .set_mctrl = sunhv_set_mctrl,
331 .get_mctrl = sunhv_get_mctrl,
332 .stop_tx = sunhv_stop_tx,
333 .start_tx = sunhv_start_tx,
334 .send_xchar = sunhv_send_xchar,
335 .stop_rx = sunhv_stop_rx,
336 .enable_ms = sunhv_enable_ms,
337 .break_ctl = sunhv_break_ctl,
338 .startup = sunhv_startup,
339 .shutdown = sunhv_shutdown,
340 .set_termios = sunhv_set_termios,
342 .release_port = sunhv_release_port,
343 .request_port = sunhv_request_port,
344 .config_port = sunhv_config_port,
345 .verify_port = sunhv_verify_port,
348 static struct uart_driver sunhv_reg = {
349 .owner = THIS_MODULE,
350 .driver_name = "serial",
351 .devfs_name = "tts/",
356 static struct uart_port *sunhv_port;
358 static inline void sunhv_console_putchar(struct uart_port *port, char c)
363 spin_lock_irqsave(&port->lock, flags);
365 while (limit-- > 0) {
366 long status = hypervisor_con_putchar(c);
367 if (status == HV_EOK)
371 spin_unlock_irqrestore(&port->lock, flags);
374 static void sunhv_console_write(struct console *con, const char *s, unsigned n)
376 struct uart_port *port = sunhv_port;
379 for (i = 0; i < n; i++) {
381 sunhv_console_putchar(port, '\r');
382 sunhv_console_putchar(port, *s++);
386 static int sunhv_console_setup(struct console *con, char *options)
391 static struct console sunhv_console = {
393 .write = sunhv_console_write,
394 .device = uart_console_device,
395 .setup = sunhv_console_setup,
396 .flags = CON_PRINTBUFFER,
401 static void __init sunhv_console_init(void)
403 if (con_is_present())
406 sunhv_console.index = 0;
407 register_console(&sunhv_console);
410 static int __init sunhv_init(void)
412 struct uart_port *port;
415 if (tlb_type != hypervisor)
418 port = kmalloc(sizeof(struct uart_port), GFP_KERNEL);
423 port->ops = &sunhv_pops;
424 port->type = PORT_SUNHV;
425 port->uartclk = ( 29491200 / 16 ); /* arbitrary */
427 /* XXX Get interrupt. XXX */
428 if (request_irq(0 /* XXX */, sunhv_interrupt,
429 SA_SHIRQ, "serial(sunhv)", port)) {
430 printk("sunhv: Cannot get IRQ %x\n",
436 sunhv_reg.minor = sunserial_current_minor;
438 sunhv_reg.cons = &sunhv_console;
440 ret = uart_register_driver(&sunhv_reg);
442 free_irq(0 /* XXX */, up);
450 sunserial_current_minor += 1;
452 sunhv_console_init();
454 uart_add_one_port(&sunhv_reg, port);
459 static void __exit sunhv_exit(void)
461 struct uart_port *port = sunhv_port;
465 uart_remove_one_port(&sunhv_reg, port);
466 free_irq(0 /* XXX */, port);
468 sunserial_current_minor -= 1;
470 uart_unregister_driver(&sunhv_reg);
476 module_init(sunhv_init);
477 module_exit(sunhv_exit);
479 MODULE_AUTHOR("David S. Miller");
480 MODULE_DESCRIPTION("SUN4V Hypervisor console driver")
481 MODULE_LICENSE("GPL");