blob: 7f73907db7b6f581c8a6bc5d98087d1d306f1b47 [file] [log] [blame]
David S. Miller02fd4732006-02-11 02:25:21 -08001/* sunhv.c: Serial driver for SUN4V hypervisor console.
2 *
3 * Copyright (C) 2006 David S. Miller (davem@davemloft.net)
4 */
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/errno.h>
9#include <linux/tty.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
21#include <asm/hypervisor.h>
22#include <asm/spitfire.h>
David S. Millerf4266ab2006-02-13 20:43:02 -080023#include <asm/vdev.h>
David S. Miller9d29a3f2006-02-15 19:48:54 -080024#include <asm/oplib.h>
David S. Millerf4266ab2006-02-13 20:43:02 -080025#include <asm/irq.h>
David S. Miller02fd4732006-02-11 02:25:21 -080026
27#if defined(CONFIG_MAGIC_SYSRQ)
28#define SUPPORT_SYSRQ
29#endif
30
31#include <linux/serial_core.h>
32
33#include "suncore.h"
34
35#define CON_BREAK ((long)-1)
36#define CON_HUP ((long)-2)
37
38static inline long hypervisor_con_getchar(long *status)
39{
40 register unsigned long func asm("%o5");
41 register unsigned long arg0 asm("%o0");
42 register unsigned long arg1 asm("%o1");
43
44 func = HV_FAST_CONS_GETCHAR;
45 arg0 = 0;
46 arg1 = 0;
47 __asm__ __volatile__("ta %6"
48 : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
49 : "0" (func), "1" (arg0), "2" (arg1),
50 "i" (HV_FAST_TRAP));
51
52 *status = arg0;
53
54 return (long) arg1;
55}
56
57static inline long hypervisor_con_putchar(long ch)
58{
59 register unsigned long func asm("%o5");
60 register unsigned long arg0 asm("%o0");
61
62 func = HV_FAST_CONS_PUTCHAR;
63 arg0 = ch;
64 __asm__ __volatile__("ta %4"
65 : "=&r" (func), "=&r" (arg0)
66 : "0" (func), "1" (arg0), "i" (HV_FAST_TRAP));
67
68 return (long) arg0;
69}
70
71#define IGNORE_BREAK 0x1
72#define IGNORE_ALL 0x2
73
74static int hung_up = 0;
75
76static struct tty_struct *receive_chars(struct uart_port *port, struct pt_regs *regs)
77{
78 struct tty_struct *tty = NULL;
79 int saw_console_brk = 0;
80 int limit = 10000;
81
82 if (port->info != NULL) /* Unopened serial console */
83 tty = port->info->tty;
84
85 while (limit-- > 0) {
86 long status;
87 long c = hypervisor_con_getchar(&status);
88 unsigned char flag;
89
90 if (status == HV_EWOULDBLOCK)
91 break;
92
93 if (c == CON_BREAK) {
94 saw_console_brk = 1;
95 c = 0;
96 }
97
98 if (c == CON_HUP) {
99 hung_up = 1;
100 uart_handle_dcd_change(port, 0);
101 } else if (hung_up) {
102 hung_up = 0;
103 uart_handle_dcd_change(port, 1);
104 }
105
106 if (tty == NULL) {
107 uart_handle_sysrq_char(port, c, regs);
108 continue;
109 }
110
111 flag = TTY_NORMAL;
112 port->icount.rx++;
113 if (c == CON_BREAK) {
114 port->icount.brk++;
115 if (uart_handle_break(port))
116 continue;
117 flag = TTY_BREAK;
118 }
119
120 if (uart_handle_sysrq_char(port, c, regs))
121 continue;
122
123 if ((port->ignore_status_mask & IGNORE_ALL) ||
124 ((port->ignore_status_mask & IGNORE_BREAK) &&
125 (c == CON_BREAK)))
126 continue;
127
128 tty_insert_flip_char(tty, c, flag);
129 }
130
131 if (saw_console_brk)
132 sun_do_break();
133
134 return tty;
135}
136
137static void transmit_chars(struct uart_port *port)
138{
David S. Millerdb33f9b2006-02-15 21:56:49 -0800139 struct circ_buf *xmit;
David S. Miller02fd4732006-02-11 02:25:21 -0800140
David S. Millerdb33f9b2006-02-15 21:56:49 -0800141 if (!port->info)
142 return;
143
144 xmit = &port->info->xmit;
David S. Miller02fd4732006-02-11 02:25:21 -0800145 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
146 return;
147
148 while (!uart_circ_empty(xmit)) {
149 long status = hypervisor_con_putchar(xmit->buf[xmit->tail]);
150
151 if (status != HV_EOK)
152 break;
153
154 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
155 port->icount.tx++;
156 }
157
158 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
159 uart_write_wakeup(port);
160}
161
162static irqreturn_t sunhv_interrupt(int irq, void *dev_id, struct pt_regs *regs)
163{
164 struct uart_port *port = dev_id;
165 struct tty_struct *tty;
166 unsigned long flags;
167
168 spin_lock_irqsave(&port->lock, flags);
169 tty = receive_chars(port, regs);
170 transmit_chars(port);
171 spin_unlock_irqrestore(&port->lock, flags);
172
173 if (tty)
174 tty_flip_buffer_push(tty);
175
176 return IRQ_HANDLED;
177}
178
179/* port->lock is not held. */
180static unsigned int sunhv_tx_empty(struct uart_port *port)
181{
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.
185 */
186 return TIOCSER_TEMT;
187}
188
189/* port->lock held by caller. */
190static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
191{
192 return;
193}
194
195/* port->lock is held by caller and interrupts are disabled. */
196static unsigned int sunhv_get_mctrl(struct uart_port *port)
197{
198 return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
199}
200
201/* port->lock held by caller. */
202static void sunhv_stop_tx(struct uart_port *port)
203{
204 return;
205}
206
207/* port->lock held by caller. */
208static void sunhv_start_tx(struct uart_port *port)
209{
210 struct circ_buf *xmit = &port->info->xmit;
211 unsigned long flags;
212
213 spin_lock_irqsave(&port->lock, flags);
214
215 while (!uart_circ_empty(xmit)) {
216 long status = hypervisor_con_putchar(xmit->buf[xmit->tail]);
217
218 if (status != HV_EOK)
219 break;
220
221 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
222 port->icount.tx++;
223 }
224
225 spin_unlock_irqrestore(&port->lock, flags);
226}
227
228/* port->lock is not held. */
229static void sunhv_send_xchar(struct uart_port *port, char ch)
230{
231 unsigned long flags;
232 int limit = 10000;
233
234 spin_lock_irqsave(&port->lock, flags);
235
236 while (limit-- > 0) {
237 long status = hypervisor_con_putchar(ch);
238 if (status == HV_EOK)
239 break;
240 }
241
242 spin_unlock_irqrestore(&port->lock, flags);
243}
244
245/* port->lock held by caller. */
246static void sunhv_stop_rx(struct uart_port *port)
247{
248}
249
250/* port->lock held by caller. */
251static void sunhv_enable_ms(struct uart_port *port)
252{
253}
254
255/* port->lock is not held. */
256static void sunhv_break_ctl(struct uart_port *port, int break_state)
257{
258 if (break_state) {
259 unsigned long flags;
260 int limit = 10000;
261
262 spin_lock_irqsave(&port->lock, flags);
263
264 while (limit-- > 0) {
265 long status = hypervisor_con_putchar(CON_BREAK);
266 if (status == HV_EOK)
267 break;
268 }
269
270 spin_unlock_irqrestore(&port->lock, flags);
271 }
272}
273
274/* port->lock is not held. */
275static int sunhv_startup(struct uart_port *port)
276{
277 return 0;
278}
279
280/* port->lock is not held. */
281static void sunhv_shutdown(struct uart_port *port)
282{
283}
284
285/* port->lock is not held. */
286static void sunhv_set_termios(struct uart_port *port, struct termios *termios,
287 struct termios *old)
288{
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;
292 unsigned long flags;
293
294 spin_lock_irqsave(&port->lock, flags);
295
296 iflag = termios->c_iflag;
297 cflag = termios->c_cflag;
298
299 port->ignore_status_mask = 0;
300 if (iflag & IGNBRK)
301 port->ignore_status_mask |= IGNORE_BREAK;
302 if ((cflag & CREAD) == 0)
303 port->ignore_status_mask |= IGNORE_ALL;
304
305 /* XXX */
306 uart_update_timeout(port, cflag,
307 (port->uartclk / (16 * quot)));
308
309 spin_unlock_irqrestore(&port->lock, flags);
310}
311
312static const char *sunhv_type(struct uart_port *port)
313{
314 return "SUN4V HCONS";
315}
316
317static void sunhv_release_port(struct uart_port *port)
318{
319}
320
321static int sunhv_request_port(struct uart_port *port)
322{
323 return 0;
324}
325
326static void sunhv_config_port(struct uart_port *port, int flags)
327{
328}
329
330static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
331{
332 return -EINVAL;
333}
334
335static 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,
348 .type = sunhv_type,
349 .release_port = sunhv_release_port,
350 .request_port = sunhv_request_port,
351 .config_port = sunhv_config_port,
352 .verify_port = sunhv_verify_port,
353};
354
355static struct uart_driver sunhv_reg = {
356 .owner = THIS_MODULE,
357 .driver_name = "serial",
358 .devfs_name = "tts/",
359 .dev_name = "ttyS",
360 .major = TTY_MAJOR,
361};
362
363static struct uart_port *sunhv_port;
364
365static inline void sunhv_console_putchar(struct uart_port *port, char c)
366{
367 unsigned long flags;
David S. Millerd5a2aa22006-02-13 21:28:40 -0800368 int limit = 1000000;
David S. Miller02fd4732006-02-11 02:25:21 -0800369
370 spin_lock_irqsave(&port->lock, flags);
371
372 while (limit-- > 0) {
373 long status = hypervisor_con_putchar(c);
374 if (status == HV_EOK)
375 break;
David S. Millerd5a2aa22006-02-13 21:28:40 -0800376 udelay(2);
David S. Miller02fd4732006-02-11 02:25:21 -0800377 }
378
379 spin_unlock_irqrestore(&port->lock, flags);
380}
381
382static void sunhv_console_write(struct console *con, const char *s, unsigned n)
383{
384 struct uart_port *port = sunhv_port;
385 int i;
386
387 for (i = 0; i < n; i++) {
388 if (*s == '\n')
389 sunhv_console_putchar(port, '\r');
390 sunhv_console_putchar(port, *s++);
391 }
392}
393
David S. Miller02fd4732006-02-11 02:25:21 -0800394static struct console sunhv_console = {
David S. Millerd5a2aa22006-02-13 21:28:40 -0800395 .name = "ttyHV",
David S. Miller02fd4732006-02-11 02:25:21 -0800396 .write = sunhv_console_write,
397 .device = uart_console_device,
David S. Miller02fd4732006-02-11 02:25:21 -0800398 .flags = CON_PRINTBUFFER,
399 .index = -1,
400 .data = &sunhv_reg,
401};
402
David S. Millerd5a2aa22006-02-13 21:28:40 -0800403static inline struct console *SUNHV_CONSOLE(void)
David S. Miller02fd4732006-02-11 02:25:21 -0800404{
405 if (con_is_present())
David S. Millerd5a2aa22006-02-13 21:28:40 -0800406 return NULL;
David S. Miller02fd4732006-02-11 02:25:21 -0800407
408 sunhv_console.index = 0;
David S. Millerd5a2aa22006-02-13 21:28:40 -0800409
410 return &sunhv_console;
David S. Miller02fd4732006-02-11 02:25:21 -0800411}
412
David S. Millerf4266ab2006-02-13 20:43:02 -0800413static int __init hv_console_compatible(char *buf, int len)
414{
415 while (len) {
416 int this_len;
417
418 if (!strcmp(buf, "qcn"))
419 return 1;
420
421 this_len = strlen(buf) + 1;
422
423 buf += this_len;
424 len -= this_len;
425 }
426
427 return 0;
428}
429
430static unsigned int __init get_interrupt(void)
431{
432 const char *cons_str = "console";
433 const char *compat_str = "compatible";
434 int node = prom_getchild(sun4v_vdev_root);
David S. Millerf4266ab2006-02-13 20:43:02 -0800435 char buf[64];
436 int err, len;
437
438 node = prom_searchsiblings(node, cons_str);
439 if (!node)
440 return 0;
441
442 len = prom_getproplen(node, compat_str);
443 if (len == 0 || len == -1)
444 return 0;
445
446 err = prom_getproperty(node, compat_str, buf, 64);
447 if (err == -1)
448 return 0;
449
450 if (!hv_console_compatible(buf, len))
451 return 0;
452
453 /* Ok, the this is the OBP node for the sun4v hypervisor
454 * console device. Decode the interrupt.
455 */
David S. Miller9d29a3f2006-02-15 19:48:54 -0800456 return sun4v_vdev_device_interrupt(node);
David S. Millerf4266ab2006-02-13 20:43:02 -0800457}
458
David S. Miller02fd4732006-02-11 02:25:21 -0800459static int __init sunhv_init(void)
460{
461 struct uart_port *port;
462 int ret;
463
464 if (tlb_type != hypervisor)
465 return -ENODEV;
466
467 port = kmalloc(sizeof(struct uart_port), GFP_KERNEL);
468 if (unlikely(!port))
469 return -ENOMEM;
470
David S. Millerdb33f9b2006-02-15 21:56:49 -0800471 memset(port, 0, sizeof(struct uart_port));
472
David S. Miller02fd4732006-02-11 02:25:21 -0800473 port->line = 0;
474 port->ops = &sunhv_pops;
475 port->type = PORT_SUNHV;
476 port->uartclk = ( 29491200 / 16 ); /* arbitrary */
477
David S. Millerdb33f9b2006-02-15 21:56:49 -0800478 /* Set this just to make uart_configure_port() happy. */
479 port->membase = (unsigned char __iomem *) __pa(port);
480
481 port->irq = get_interrupt();
482 if (!port->irq) {
David S. Miller02fd4732006-02-11 02:25:21 -0800483 kfree(port);
484 return -ENODEV;
485 }
486
487 sunhv_reg.minor = sunserial_current_minor;
488 sunhv_reg.nr = 1;
David S. Miller02fd4732006-02-11 02:25:21 -0800489
490 ret = uart_register_driver(&sunhv_reg);
491 if (ret < 0) {
David S. Millerdb33f9b2006-02-15 21:56:49 -0800492 printk(KERN_ERR "SUNHV: uart_register_driver() failed %d\n",
493 ret);
David S. Miller02fd4732006-02-11 02:25:21 -0800494 kfree(port);
495
496 return ret;
497 }
498
David S. Miller02fd4732006-02-11 02:25:21 -0800499 sunserial_current_minor += 1;
500
David S. Millerd5a2aa22006-02-13 21:28:40 -0800501 sunhv_reg.cons = SUNHV_CONSOLE();
502
503 sunhv_port = port;
David S. Miller02fd4732006-02-11 02:25:21 -0800504
David S. Millerdb33f9b2006-02-15 21:56:49 -0800505 ret = uart_add_one_port(&sunhv_reg, port);
506 if (ret < 0) {
507 printk(KERN_ERR "SUNHV: uart_add_one_port() failed %d\n", ret);
508 sunserial_current_minor -= 1;
509 uart_unregister_driver(&sunhv_reg);
510 kfree(port);
511 sunhv_port = NULL;
512 return -ENODEV;
513 }
514
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);
521 kfree(port);
522 sunhv_port = NULL;
523 return -ENODEV;
524 }
David S. Miller02fd4732006-02-11 02:25:21 -0800525
526 return 0;
527}
528
529static void __exit sunhv_exit(void)
530{
531 struct uart_port *port = sunhv_port;
532
533 BUG_ON(!port);
534
David S. Millerdb33f9b2006-02-15 21:56:49 -0800535 free_irq(port->irq, port);
536
David S. Miller02fd4732006-02-11 02:25:21 -0800537 uart_remove_one_port(&sunhv_reg, port);
David S. Miller02fd4732006-02-11 02:25:21 -0800538 sunserial_current_minor -= 1;
539
540 uart_unregister_driver(&sunhv_reg);
541
542 kfree(sunhv_port);
543 sunhv_port = NULL;
544}
545
546module_init(sunhv_init);
547module_exit(sunhv_exit);
548
549MODULE_AUTHOR("David S. Miller");
550MODULE_DESCRIPTION("SUN4V Hypervisor console driver")
551MODULE_LICENSE("GPL");