blob: 40d48566215c07a9c3523a864e0be027872e962f [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. Miller93872ba2006-06-30 00:00:22 -070023#include <asm/prom.h>
24#include <asm/of_device.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
David Howells7d12e782006-10-05 14:55:46 +010076static struct tty_struct *receive_chars(struct uart_port *port)
David S. Miller02fd4732006-02-11 02:25:21 -080077{
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) {
David S. Miller135066a2006-02-16 00:42:21 -080094 if (uart_handle_break(port))
95 continue;
David S. Miller02fd4732006-02-11 02:25:21 -080096 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) {
David Howells7d12e782006-10-05 14:55:46 +0100109 uart_handle_sysrq_char(port, c);
David S. Miller02fd4732006-02-11 02:25:21 -0800110 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
David Howells7d12e782006-10-05 14:55:46 +0100122 if (uart_handle_sysrq_char(port, c))
David S. Miller02fd4732006-02-11 02:25:21 -0800123 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
139static void transmit_chars(struct uart_port *port)
140{
David S. Millerdb33f9b2006-02-15 21:56:49 -0800141 struct circ_buf *xmit;
David S. Miller02fd4732006-02-11 02:25:21 -0800142
David S. Millerdb33f9b2006-02-15 21:56:49 -0800143 if (!port->info)
144 return;
145
146 xmit = &port->info->xmit;
David S. Miller02fd4732006-02-11 02:25:21 -0800147 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
David Howells7d12e782006-10-05 14:55:46 +0100164static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
David S. Miller02fd4732006-02-11 02:25:21 -0800165{
166 struct uart_port *port = dev_id;
167 struct tty_struct *tty;
168 unsigned long flags;
169
170 spin_lock_irqsave(&port->lock, flags);
David Howells7d12e782006-10-05 14:55:46 +0100171 tty = receive_chars(port);
David S. Miller02fd4732006-02-11 02:25:21 -0800172 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. */
182static 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. */
192static 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. */
198static 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. */
204static void sunhv_stop_tx(struct uart_port *port)
205{
206 return;
207}
208
209/* port->lock held by caller. */
210static void sunhv_start_tx(struct uart_port *port)
211{
212 struct circ_buf *xmit = &port->info->xmit;
David S. Miller02fd4732006-02-11 02:25:21 -0800213
214 while (!uart_circ_empty(xmit)) {
215 long status = hypervisor_con_putchar(xmit->buf[xmit->tail]);
216
217 if (status != HV_EOK)
218 break;
219
220 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
221 port->icount.tx++;
222 }
David S. Miller02fd4732006-02-11 02:25:21 -0800223}
224
225/* port->lock is not held. */
226static void sunhv_send_xchar(struct uart_port *port, char ch)
227{
228 unsigned long flags;
229 int limit = 10000;
230
231 spin_lock_irqsave(&port->lock, flags);
232
233 while (limit-- > 0) {
234 long status = hypervisor_con_putchar(ch);
235 if (status == HV_EOK)
236 break;
237 }
238
239 spin_unlock_irqrestore(&port->lock, flags);
240}
241
242/* port->lock held by caller. */
243static void sunhv_stop_rx(struct uart_port *port)
244{
245}
246
247/* port->lock held by caller. */
248static void sunhv_enable_ms(struct uart_port *port)
249{
250}
251
252/* port->lock is not held. */
253static void sunhv_break_ctl(struct uart_port *port, int break_state)
254{
255 if (break_state) {
256 unsigned long flags;
David S. Miller843dfb42006-02-18 16:32:22 -0800257 int limit = 1000000;
David S. Miller02fd4732006-02-11 02:25:21 -0800258
259 spin_lock_irqsave(&port->lock, flags);
260
261 while (limit-- > 0) {
262 long status = hypervisor_con_putchar(CON_BREAK);
263 if (status == HV_EOK)
264 break;
David S. Miller843dfb42006-02-18 16:32:22 -0800265 udelay(2);
David S. Miller02fd4732006-02-11 02:25:21 -0800266 }
267
268 spin_unlock_irqrestore(&port->lock, flags);
269 }
270}
271
272/* port->lock is not held. */
273static int sunhv_startup(struct uart_port *port)
274{
275 return 0;
276}
277
278/* port->lock is not held. */
279static void sunhv_shutdown(struct uart_port *port)
280{
281}
282
283/* port->lock is not held. */
Alan Cox606d0992006-12-08 02:38:45 -0800284static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
285 struct ktermios *old)
David S. Miller02fd4732006-02-11 02:25:21 -0800286{
287 unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
288 unsigned int quot = uart_get_divisor(port, baud);
289 unsigned int iflag, cflag;
290 unsigned long flags;
291
292 spin_lock_irqsave(&port->lock, flags);
293
294 iflag = termios->c_iflag;
295 cflag = termios->c_cflag;
296
297 port->ignore_status_mask = 0;
298 if (iflag & IGNBRK)
299 port->ignore_status_mask |= IGNORE_BREAK;
300 if ((cflag & CREAD) == 0)
301 port->ignore_status_mask |= IGNORE_ALL;
302
303 /* XXX */
304 uart_update_timeout(port, cflag,
305 (port->uartclk / (16 * quot)));
306
307 spin_unlock_irqrestore(&port->lock, flags);
308}
309
310static const char *sunhv_type(struct uart_port *port)
311{
312 return "SUN4V HCONS";
313}
314
315static void sunhv_release_port(struct uart_port *port)
316{
317}
318
319static int sunhv_request_port(struct uart_port *port)
320{
321 return 0;
322}
323
324static void sunhv_config_port(struct uart_port *port, int flags)
325{
326}
327
328static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
329{
330 return -EINVAL;
331}
332
333static struct uart_ops sunhv_pops = {
334 .tx_empty = sunhv_tx_empty,
335 .set_mctrl = sunhv_set_mctrl,
336 .get_mctrl = sunhv_get_mctrl,
337 .stop_tx = sunhv_stop_tx,
338 .start_tx = sunhv_start_tx,
339 .send_xchar = sunhv_send_xchar,
340 .stop_rx = sunhv_stop_rx,
341 .enable_ms = sunhv_enable_ms,
342 .break_ctl = sunhv_break_ctl,
343 .startup = sunhv_startup,
344 .shutdown = sunhv_shutdown,
345 .set_termios = sunhv_set_termios,
346 .type = sunhv_type,
347 .release_port = sunhv_release_port,
348 .request_port = sunhv_request_port,
349 .config_port = sunhv_config_port,
350 .verify_port = sunhv_verify_port,
351};
352
353static struct uart_driver sunhv_reg = {
354 .owner = THIS_MODULE,
355 .driver_name = "serial",
David S. Miller02fd4732006-02-11 02:25:21 -0800356 .dev_name = "ttyS",
357 .major = TTY_MAJOR,
358};
359
360static struct uart_port *sunhv_port;
361
362static inline void sunhv_console_putchar(struct uart_port *port, char c)
363{
364 unsigned long flags;
David S. Millerd5a2aa22006-02-13 21:28:40 -0800365 int limit = 1000000;
David S. Miller02fd4732006-02-11 02:25:21 -0800366
367 spin_lock_irqsave(&port->lock, flags);
368
369 while (limit-- > 0) {
370 long status = hypervisor_con_putchar(c);
371 if (status == HV_EOK)
372 break;
David S. Millerd5a2aa22006-02-13 21:28:40 -0800373 udelay(2);
David S. Miller02fd4732006-02-11 02:25:21 -0800374 }
375
376 spin_unlock_irqrestore(&port->lock, flags);
377}
378
379static void sunhv_console_write(struct console *con, const char *s, unsigned n)
380{
381 struct uart_port *port = sunhv_port;
382 int i;
383
384 for (i = 0; i < n; i++) {
385 if (*s == '\n')
386 sunhv_console_putchar(port, '\r');
387 sunhv_console_putchar(port, *s++);
388 }
389}
390
David S. Miller02fd4732006-02-11 02:25:21 -0800391static struct console sunhv_console = {
David S. Millerd5a2aa22006-02-13 21:28:40 -0800392 .name = "ttyHV",
David S. Miller02fd4732006-02-11 02:25:21 -0800393 .write = sunhv_console_write,
394 .device = uart_console_device,
David S. Miller02fd4732006-02-11 02:25:21 -0800395 .flags = CON_PRINTBUFFER,
396 .index = -1,
397 .data = &sunhv_reg,
398};
399
David S. Millerd5a2aa22006-02-13 21:28:40 -0800400static inline struct console *SUNHV_CONSOLE(void)
David S. Miller02fd4732006-02-11 02:25:21 -0800401{
402 if (con_is_present())
David S. Millerd5a2aa22006-02-13 21:28:40 -0800403 return NULL;
David S. Miller02fd4732006-02-11 02:25:21 -0800404
405 sunhv_console.index = 0;
David S. Millerd5a2aa22006-02-13 21:28:40 -0800406
407 return &sunhv_console;
David S. Miller02fd4732006-02-11 02:25:21 -0800408}
409
David S. Miller93872ba2006-06-30 00:00:22 -0700410static int __devinit hv_probe(struct of_device *op, const struct of_device_id *match)
David S. Miller02fd4732006-02-11 02:25:21 -0800411{
412 struct uart_port *port;
David S. Miller93872ba2006-06-30 00:00:22 -0700413 int err;
David S. Miller02fd4732006-02-11 02:25:21 -0800414
David S. Miller93872ba2006-06-30 00:00:22 -0700415 if (op->irqs[0] == 0xffffffff)
David S. Miller02fd4732006-02-11 02:25:21 -0800416 return -ENODEV;
417
David S. Miller93872ba2006-06-30 00:00:22 -0700418 port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
David S. Miller02fd4732006-02-11 02:25:21 -0800419 if (unlikely(!port))
420 return -ENOMEM;
421
David S. Miller93872ba2006-06-30 00:00:22 -0700422 sunhv_port = port;
David S. Millerdb33f9b2006-02-15 21:56:49 -0800423
David S. Miller02fd4732006-02-11 02:25:21 -0800424 port->line = 0;
425 port->ops = &sunhv_pops;
426 port->type = PORT_SUNHV;
427 port->uartclk = ( 29491200 / 16 ); /* arbitrary */
428
David S. Millerdb33f9b2006-02-15 21:56:49 -0800429 port->membase = (unsigned char __iomem *) __pa(port);
430
David S. Miller93872ba2006-06-30 00:00:22 -0700431 port->irq = op->irqs[0];
432
433 port->dev = &op->dev;
David S. Miller02fd4732006-02-11 02:25:21 -0800434
435 sunhv_reg.minor = sunserial_current_minor;
436 sunhv_reg.nr = 1;
David S. Miller02fd4732006-02-11 02:25:21 -0800437
David S. Miller93872ba2006-06-30 00:00:22 -0700438 err = uart_register_driver(&sunhv_reg);
439 if (err)
440 goto out_free_port;
David S. Miller02fd4732006-02-11 02:25:21 -0800441
David S. Millerf5deb802006-02-20 14:39:16 -0800442 sunhv_reg.tty_driver->name_base = sunhv_reg.minor - 64;
David S. Miller02fd4732006-02-11 02:25:21 -0800443 sunserial_current_minor += 1;
444
David S. Millerd5a2aa22006-02-13 21:28:40 -0800445 sunhv_reg.cons = SUNHV_CONSOLE();
446
David S. Miller93872ba2006-06-30 00:00:22 -0700447 err = uart_add_one_port(&sunhv_reg, port);
448 if (err)
449 goto out_unregister_driver;
David S. Miller02fd4732006-02-11 02:25:21 -0800450
David S. Miller93872ba2006-06-30 00:00:22 -0700451 err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
452 if (err)
453 goto out_remove_port;
David S. Millerdb33f9b2006-02-15 21:56:49 -0800454
David S. Miller93872ba2006-06-30 00:00:22 -0700455 dev_set_drvdata(&op->dev, port);
David S. Miller02fd4732006-02-11 02:25:21 -0800456
457 return 0;
David S. Miller93872ba2006-06-30 00:00:22 -0700458
459out_remove_port:
460 uart_remove_one_port(&sunhv_reg, port);
461
462out_unregister_driver:
463 sunserial_current_minor -= 1;
464 uart_unregister_driver(&sunhv_reg);
465
466out_free_port:
467 kfree(port);
468 sunhv_port = NULL;
469 return err;
David S. Miller02fd4732006-02-11 02:25:21 -0800470}
471
David S. Miller93872ba2006-06-30 00:00:22 -0700472static int __devexit hv_remove(struct of_device *dev)
David S. Miller02fd4732006-02-11 02:25:21 -0800473{
David S. Miller93872ba2006-06-30 00:00:22 -0700474 struct uart_port *port = dev_get_drvdata(&dev->dev);
David S. Miller02fd4732006-02-11 02:25:21 -0800475
David S. Millerdb33f9b2006-02-15 21:56:49 -0800476 free_irq(port->irq, port);
477
David S. Miller02fd4732006-02-11 02:25:21 -0800478 uart_remove_one_port(&sunhv_reg, port);
David S. Miller02fd4732006-02-11 02:25:21 -0800479
David S. Miller93872ba2006-06-30 00:00:22 -0700480 sunserial_current_minor -= 1;
David S. Miller02fd4732006-02-11 02:25:21 -0800481 uart_unregister_driver(&sunhv_reg);
482
David S. Miller93872ba2006-06-30 00:00:22 -0700483 kfree(port);
David S. Miller02fd4732006-02-11 02:25:21 -0800484 sunhv_port = NULL;
David S. Miller93872ba2006-06-30 00:00:22 -0700485
486 dev_set_drvdata(&dev->dev, NULL);
487
488 return 0;
489}
490
491static struct of_device_id hv_match[] = {
492 {
493 .name = "console",
494 .compatible = "qcn",
495 },
496 {},
497};
498MODULE_DEVICE_TABLE(of, hv_match);
499
500static struct of_platform_driver hv_driver = {
501 .name = "hv",
502 .match_table = hv_match,
503 .probe = hv_probe,
504 .remove = __devexit_p(hv_remove),
505};
506
507static int __init sunhv_init(void)
508{
509 if (tlb_type != hypervisor)
510 return -ENODEV;
511
512 return of_register_driver(&hv_driver, &of_bus_type);
513}
514
515static void __exit sunhv_exit(void)
516{
517 of_unregister_driver(&hv_driver);
David S. Miller02fd4732006-02-11 02:25:21 -0800518}
519
520module_init(sunhv_init);
521module_exit(sunhv_exit);
522
523MODULE_AUTHOR("David S. Miller");
David S. Miller93872ba2006-06-30 00:00:22 -0700524MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
525MODULE_VERSION("2.0");
David S. Miller02fd4732006-02-11 02:25:21 -0800526MODULE_LICENSE("GPL");