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