]> err.no Git - linux-2.6/blob - drivers/serial/sunhv.c
[SPARC64] sunhv: Support SYSRQ properly.
[linux-2.6] / drivers / serial / sunhv.c
1 /* 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>
23 #include <asm/vdev.h>
24 #include <asm/oplib.h>
25 #include <asm/irq.h>
26
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
38 static 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
57 static 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
74 static int hung_up = 0;
75
76 static 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                         if (uart_handle_break(port))
95                                 continue;
96                         saw_console_brk = 1;
97                         c = 0;
98                 }
99
100                 if (c == CON_HUP) {
101                         hung_up = 1;
102                         uart_handle_dcd_change(port, 0);
103                 } else if (hung_up) {
104                         hung_up = 0;
105                         uart_handle_dcd_change(port, 1);
106                 }
107
108                 if (tty == NULL) {
109                         uart_handle_sysrq_char(port, c, regs);
110                         continue;
111                 }
112
113                 flag = TTY_NORMAL;
114                 port->icount.rx++;
115                 if (c == CON_BREAK) {
116                         port->icount.brk++;
117                         if (uart_handle_break(port))
118                                 continue;
119                         flag = TTY_BREAK;
120                 }
121
122                 if (uart_handle_sysrq_char(port, c, regs))
123                         continue;
124
125                 if ((port->ignore_status_mask & IGNORE_ALL) ||
126                     ((port->ignore_status_mask & IGNORE_BREAK) &&
127                      (c == CON_BREAK)))
128                         continue;
129
130                 tty_insert_flip_char(tty, c, flag);
131         }
132
133         if (saw_console_brk)
134                 sun_do_break();
135
136         return tty;
137 }
138
139 static void transmit_chars(struct uart_port *port)
140 {
141         struct circ_buf *xmit;
142
143         if (!port->info)
144                 return;
145
146         xmit = &port->info->xmit;
147         if (uart_circ_empty(xmit) || uart_tx_stopped(port))
148                 return;
149
150         while (!uart_circ_empty(xmit)) {
151                 long status = hypervisor_con_putchar(xmit->buf[xmit->tail]);
152
153                 if (status != HV_EOK)
154                         break;
155
156                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
157                 port->icount.tx++;
158         }
159
160         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
161                 uart_write_wakeup(port);
162 }
163
164 static irqreturn_t sunhv_interrupt(int irq, void *dev_id, struct pt_regs *regs)
165 {
166         struct uart_port *port = dev_id;
167         struct tty_struct *tty;
168         unsigned long flags;
169
170         spin_lock_irqsave(&port->lock, flags);
171         tty = receive_chars(port, regs);
172         transmit_chars(port);
173         spin_unlock_irqrestore(&port->lock, flags);
174
175         if (tty)
176                 tty_flip_buffer_push(tty);
177
178         return IRQ_HANDLED;
179 }
180
181 /* port->lock is not held.  */
182 static unsigned int sunhv_tx_empty(struct uart_port *port)
183 {
184         /* Transmitter is always empty for us.  If the circ buffer
185          * is non-empty or there is an x_char pending, our caller
186          * will do the right thing and ignore what we return here.
187          */
188         return TIOCSER_TEMT;
189 }
190
191 /* port->lock held by caller.  */
192 static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
193 {
194         return;
195 }
196
197 /* port->lock is held by caller and interrupts are disabled.  */
198 static unsigned int sunhv_get_mctrl(struct uart_port *port)
199 {
200         return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
201 }
202
203 /* port->lock held by caller.  */
204 static void sunhv_stop_tx(struct uart_port *port)
205 {
206         return;
207 }
208
209 /* port->lock held by caller.  */
210 static void sunhv_start_tx(struct uart_port *port)
211 {
212         struct circ_buf *xmit = &port->info->xmit;
213         unsigned long flags;
214
215         spin_lock_irqsave(&port->lock, flags);
216
217         while (!uart_circ_empty(xmit)) {
218                 long status = hypervisor_con_putchar(xmit->buf[xmit->tail]);
219
220                 if (status != HV_EOK)
221                         break;
222
223                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
224                 port->icount.tx++;
225         }
226
227         spin_unlock_irqrestore(&port->lock, flags);
228 }
229
230 /* port->lock is not held.  */
231 static void sunhv_send_xchar(struct uart_port *port, char ch)
232 {
233         unsigned long flags;
234         int limit = 10000;
235
236         spin_lock_irqsave(&port->lock, flags);
237
238         while (limit-- > 0) {
239                 long status = hypervisor_con_putchar(ch);
240                 if (status == HV_EOK)
241                         break;
242         }
243
244         spin_unlock_irqrestore(&port->lock, flags);
245 }
246
247 /* port->lock held by caller.  */
248 static void sunhv_stop_rx(struct uart_port *port)
249 {
250 }
251
252 /* port->lock held by caller.  */
253 static void sunhv_enable_ms(struct uart_port *port)
254 {
255 }
256
257 /* port->lock is not held.  */
258 static void sunhv_break_ctl(struct uart_port *port, int break_state)
259 {
260         if (break_state) {
261                 unsigned long flags;
262                 int limit = 10000;
263
264                 spin_lock_irqsave(&port->lock, flags);
265
266                 while (limit-- > 0) {
267                         long status = hypervisor_con_putchar(CON_BREAK);
268                         if (status == HV_EOK)
269                                 break;
270                 }
271
272                 spin_unlock_irqrestore(&port->lock, flags);
273         }
274 }
275
276 /* port->lock is not held.  */
277 static int sunhv_startup(struct uart_port *port)
278 {
279         return 0;
280 }
281
282 /* port->lock is not held.  */
283 static void sunhv_shutdown(struct uart_port *port)
284 {
285 }
286
287 /* port->lock is not held.  */
288 static void sunhv_set_termios(struct uart_port *port, struct termios *termios,
289                               struct termios *old)
290 {
291         unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
292         unsigned int quot = uart_get_divisor(port, baud);
293         unsigned int iflag, cflag;
294         unsigned long flags;
295
296         spin_lock_irqsave(&port->lock, flags);
297
298         iflag = termios->c_iflag;
299         cflag = termios->c_cflag;
300
301         port->ignore_status_mask = 0;
302         if (iflag & IGNBRK)
303                 port->ignore_status_mask |= IGNORE_BREAK;
304         if ((cflag & CREAD) == 0)
305                 port->ignore_status_mask |= IGNORE_ALL;
306
307         /* XXX */
308         uart_update_timeout(port, cflag,
309                             (port->uartclk / (16 * quot)));
310
311         spin_unlock_irqrestore(&port->lock, flags);
312 }
313
314 static const char *sunhv_type(struct uart_port *port)
315 {
316         return "SUN4V HCONS";
317 }
318
319 static void sunhv_release_port(struct uart_port *port)
320 {
321 }
322
323 static int sunhv_request_port(struct uart_port *port)
324 {
325         return 0;
326 }
327
328 static void sunhv_config_port(struct uart_port *port, int flags)
329 {
330 }
331
332 static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
333 {
334         return -EINVAL;
335 }
336
337 static struct uart_ops sunhv_pops = {
338         .tx_empty       = sunhv_tx_empty,
339         .set_mctrl      = sunhv_set_mctrl,
340         .get_mctrl      = sunhv_get_mctrl,
341         .stop_tx        = sunhv_stop_tx,
342         .start_tx       = sunhv_start_tx,
343         .send_xchar     = sunhv_send_xchar,
344         .stop_rx        = sunhv_stop_rx,
345         .enable_ms      = sunhv_enable_ms,
346         .break_ctl      = sunhv_break_ctl,
347         .startup        = sunhv_startup,
348         .shutdown       = sunhv_shutdown,
349         .set_termios    = sunhv_set_termios,
350         .type           = sunhv_type,
351         .release_port   = sunhv_release_port,
352         .request_port   = sunhv_request_port,
353         .config_port    = sunhv_config_port,
354         .verify_port    = sunhv_verify_port,
355 };
356
357 static struct uart_driver sunhv_reg = {
358         .owner                  = THIS_MODULE,
359         .driver_name            = "serial",
360         .devfs_name             = "tts/",
361         .dev_name               = "ttyS",
362         .major                  = TTY_MAJOR,
363 };
364
365 static struct uart_port *sunhv_port;
366
367 static inline void sunhv_console_putchar(struct uart_port *port, char c)
368 {
369         unsigned long flags;
370         int limit = 1000000;
371
372         spin_lock_irqsave(&port->lock, flags);
373
374         while (limit-- > 0) {
375                 long status = hypervisor_con_putchar(c);
376                 if (status == HV_EOK)
377                         break;
378                 udelay(2);
379         }
380
381         spin_unlock_irqrestore(&port->lock, flags);
382 }
383
384 static void sunhv_console_write(struct console *con, const char *s, unsigned n)
385 {
386         struct uart_port *port = sunhv_port;
387         int i;
388
389         for (i = 0; i < n; i++) {
390                 if (*s == '\n')
391                         sunhv_console_putchar(port, '\r');
392                 sunhv_console_putchar(port, *s++);
393         }
394 }
395
396 static struct console sunhv_console = {
397         .name   =       "ttyHV",
398         .write  =       sunhv_console_write,
399         .device =       uart_console_device,
400         .flags  =       CON_PRINTBUFFER,
401         .index  =       -1,
402         .data   =       &sunhv_reg,
403 };
404
405 static inline struct console *SUNHV_CONSOLE(void)
406 {
407         if (con_is_present())
408                 return NULL;
409
410         sunhv_console.index = 0;
411
412         return &sunhv_console;
413 }
414
415 static int __init hv_console_compatible(char *buf, int len)
416 {
417         while (len) {
418                 int this_len;
419
420                 if (!strcmp(buf, "qcn"))
421                         return 1;
422
423                 this_len = strlen(buf) + 1;
424
425                 buf += this_len;
426                 len -= this_len;
427         }
428
429         return 0;
430 }
431
432 static unsigned int __init get_interrupt(void)
433 {
434         const char *cons_str = "console";
435         const char *compat_str = "compatible";
436         int node = prom_getchild(sun4v_vdev_root);
437         char buf[64];
438         int err, len;
439
440         node = prom_searchsiblings(node, cons_str);
441         if (!node)
442                 return 0;
443
444         len = prom_getproplen(node, compat_str);
445         if (len == 0 || len == -1)
446                 return 0;
447
448         err = prom_getproperty(node, compat_str, buf, 64);
449         if (err == -1)
450                 return 0;
451
452         if (!hv_console_compatible(buf, len))
453                 return 0;
454
455         /* Ok, the this is the OBP node for the sun4v hypervisor
456          * console device.  Decode the interrupt.
457          */
458         return sun4v_vdev_device_interrupt(node);
459 }
460
461 static int __init sunhv_init(void)
462 {
463         struct uart_port *port;
464         int ret;
465
466         if (tlb_type != hypervisor)
467                 return -ENODEV;
468
469         port = kmalloc(sizeof(struct uart_port), GFP_KERNEL);
470         if (unlikely(!port))
471                 return -ENOMEM;
472
473         memset(port, 0, sizeof(struct uart_port));
474
475         port->line = 0;
476         port->ops = &sunhv_pops;
477         port->type = PORT_SUNHV;
478         port->uartclk = ( 29491200 / 16 ); /* arbitrary */
479
480         /* Set this just to make uart_configure_port() happy.  */
481         port->membase = (unsigned char __iomem *) __pa(port);
482
483         port->irq = get_interrupt();
484         if (!port->irq) {
485                 kfree(port);
486                 return -ENODEV;
487         }
488
489         sunhv_reg.minor = sunserial_current_minor;
490         sunhv_reg.nr = 1;
491
492         ret = uart_register_driver(&sunhv_reg);
493         if (ret < 0) {
494                 printk(KERN_ERR "SUNHV: uart_register_driver() failed %d\n",
495                        ret);
496                 kfree(port);
497
498                 return ret;
499         }
500
501         sunserial_current_minor += 1;
502
503         sunhv_reg.cons = SUNHV_CONSOLE();
504
505         sunhv_port = port;
506
507         ret = uart_add_one_port(&sunhv_reg, port);
508         if (ret < 0) {
509                 printk(KERN_ERR "SUNHV: uart_add_one_port() failed %d\n", ret);
510                 sunserial_current_minor -= 1;
511                 uart_unregister_driver(&sunhv_reg);
512                 kfree(port);
513                 sunhv_port = NULL;
514                 return -ENODEV;
515         }
516
517         if (request_irq(port->irq, sunhv_interrupt,
518                         SA_SHIRQ, "serial(sunhv)", port)) {
519                 printk(KERN_ERR "sunhv: Cannot register IRQ\n");
520                 uart_remove_one_port(&sunhv_reg, port);
521                 sunserial_current_minor -= 1;
522                 uart_unregister_driver(&sunhv_reg);
523                 kfree(port);
524                 sunhv_port = NULL;
525                 return -ENODEV;
526         }
527
528         return 0;
529 }
530
531 static void __exit sunhv_exit(void)
532 {
533         struct uart_port *port = sunhv_port;
534
535         BUG_ON(!port);
536
537         free_irq(port->irq, port);
538
539         uart_remove_one_port(&sunhv_reg, port);
540         sunserial_current_minor -= 1;
541
542         uart_unregister_driver(&sunhv_reg);
543
544         kfree(sunhv_port);
545         sunhv_port = NULL;
546 }
547
548 module_init(sunhv_init);
549 module_exit(sunhv_exit);
550
551 MODULE_AUTHOR("David S. Miller");
552 MODULE_DESCRIPTION("SUN4V Hypervisor console driver")
553 MODULE_LICENSE("GPL");