1/* sunhv.c: Serial driver for SUN4V hypervisor console.
2 *
3 * Copyright (C) 2006, 2007 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/prom.h>
24#include <asm/of_device.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#define IGNORE_BREAK	0x1
39#define IGNORE_ALL	0x2
40
41static char *con_write_page;
42static char *con_read_page;
43
44static int hung_up = 0;
45
46static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit)
47{
48	while (!uart_circ_empty(xmit)) {
49		long status = sun4v_con_putchar(xmit->buf[xmit->tail]);
50
51		if (status != HV_EOK)
52			break;
53
54		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
55		port->icount.tx++;
56	}
57}
58
59static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit)
60{
61	while (!uart_circ_empty(xmit)) {
62		unsigned long ra = __pa(xmit->buf + xmit->tail);
63		unsigned long len, status, sent;
64
65		len = CIRC_CNT_TO_END(xmit->head, xmit->tail,
66				      UART_XMIT_SIZE);
67		status = sun4v_con_write(ra, len, &sent);
68		if (status != HV_EOK)
69			break;
70		xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1);
71		port->icount.tx += sent;
72	}
73}
74
75static int receive_chars_getchar(struct uart_port *port, struct tty_struct *tty)
76{
77	int saw_console_brk = 0;
78	int limit = 10000;
79
80	while (limit-- > 0) {
81		long status;
82		long c = sun4v_con_getchar(&status);
83
84		if (status == HV_EWOULDBLOCK)
85			break;
86
87		if (c == CON_BREAK) {
88			if (uart_handle_break(port))
89				continue;
90			saw_console_brk = 1;
91			c = 0;
92		}
93
94		if (c == CON_HUP) {
95			hung_up = 1;
96			uart_handle_dcd_change(port, 0);
97		} else if (hung_up) {
98			hung_up = 0;
99			uart_handle_dcd_change(port, 1);
100		}
101
102		if (tty == NULL) {
103			uart_handle_sysrq_char(port, c);
104			continue;
105		}
106
107		port->icount.rx++;
108
109		if (uart_handle_sysrq_char(port, c))
110			continue;
111
112		tty_insert_flip_char(tty, c, TTY_NORMAL);
113	}
114
115	return saw_console_brk;
116}
117
118static int receive_chars_read(struct uart_port *port, struct tty_struct *tty)
119{
120	int saw_console_brk = 0;
121	int limit = 10000;
122
123	while (limit-- > 0) {
124		unsigned long ra = __pa(con_read_page);
125		unsigned long bytes_read, i;
126		long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read);
127
128		if (stat != HV_EOK) {
129			bytes_read = 0;
130
131			if (stat == CON_BREAK) {
132				if (uart_handle_break(port))
133					continue;
134				saw_console_brk = 1;
135				*con_read_page = 0;
136				bytes_read = 1;
137			} else if (stat == CON_HUP) {
138				hung_up = 1;
139				uart_handle_dcd_change(port, 0);
140				continue;
141			} else {
142				/* HV_EWOULDBLOCK, etc.  */
143				break;
144			}
145		}
146
147		if (hung_up) {
148			hung_up = 0;
149			uart_handle_dcd_change(port, 1);
150		}
151
152		for (i = 0; i < bytes_read; i++)
153			uart_handle_sysrq_char(port, con_read_page[i]);
154
155		if (tty == NULL)
156			continue;
157
158		port->icount.rx += bytes_read;
159
160		tty_insert_flip_string(tty, con_read_page, bytes_read);
161	}
162
163	return saw_console_brk;
164}
165
166struct sunhv_ops {
167	void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit);
168	int (*receive_chars)(struct uart_port *port, struct tty_struct *tty);
169};
170
171static struct sunhv_ops bychar_ops = {
172	.transmit_chars = transmit_chars_putchar,
173	.receive_chars = receive_chars_getchar,
174};
175
176static struct sunhv_ops bywrite_ops = {
177	.transmit_chars = transmit_chars_write,
178	.receive_chars = receive_chars_read,
179};
180
181static struct sunhv_ops *sunhv_ops = &bychar_ops;
182
183static struct tty_struct *receive_chars(struct uart_port *port)
184{
185	struct tty_struct *tty = NULL;
186
187	if (port->info != NULL)		/* Unopened serial console */
188		tty = port->info->tty;
189
190	if (sunhv_ops->receive_chars(port, tty))
191		sun_do_break();
192
193	return tty;
194}
195
196static void transmit_chars(struct uart_port *port)
197{
198	struct circ_buf *xmit;
199
200	if (!port->info)
201		return;
202
203	xmit = &port->info->xmit;
204	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
205		return;
206
207	sunhv_ops->transmit_chars(port, xmit);
208
209	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
210		uart_write_wakeup(port);
211}
212
213static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
214{
215	struct uart_port *port = dev_id;
216	struct tty_struct *tty;
217	unsigned long flags;
218
219	spin_lock_irqsave(&port->lock, flags);
220	tty = receive_chars(port);
221	transmit_chars(port);
222	spin_unlock_irqrestore(&port->lock, flags);
223
224	if (tty)
225		tty_flip_buffer_push(tty);
226
227	return IRQ_HANDLED;
228}
229
230/* port->lock is not held.  */
231static unsigned int sunhv_tx_empty(struct uart_port *port)
232{
233	/* Transmitter is always empty for us.  If the circ buffer
234	 * is non-empty or there is an x_char pending, our caller
235	 * will do the right thing and ignore what we return here.
236	 */
237	return TIOCSER_TEMT;
238}
239
240/* port->lock held by caller.  */
241static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
242{
243	return;
244}
245
246/* port->lock is held by caller and interrupts are disabled.  */
247static unsigned int sunhv_get_mctrl(struct uart_port *port)
248{
249	return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
250}
251
252/* port->lock held by caller.  */
253static void sunhv_stop_tx(struct uart_port *port)
254{
255	return;
256}
257
258/* port->lock held by caller.  */
259static void sunhv_start_tx(struct uart_port *port)
260{
261	struct circ_buf *xmit = &port->info->xmit;
262
263	while (!uart_circ_empty(xmit)) {
264		long status = sun4v_con_putchar(xmit->buf[xmit->tail]);
265
266		if (status != HV_EOK)
267			break;
268
269		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
270		port->icount.tx++;
271	}
272}
273
274/* port->lock is not held.  */
275static void sunhv_send_xchar(struct uart_port *port, char ch)
276{
277	unsigned long flags;
278	int limit = 10000;
279
280	spin_lock_irqsave(&port->lock, flags);
281
282	while (limit-- > 0) {
283		long status = sun4v_con_putchar(ch);
284		if (status == HV_EOK)
285			break;
286		udelay(1);
287	}
288
289	spin_unlock_irqrestore(&port->lock, flags);
290}
291
292/* port->lock held by caller.  */
293static void sunhv_stop_rx(struct uart_port *port)
294{
295}
296
297/* port->lock held by caller.  */
298static void sunhv_enable_ms(struct uart_port *port)
299{
300}
301
302/* port->lock is not held.  */
303static void sunhv_break_ctl(struct uart_port *port, int break_state)
304{
305	if (break_state) {
306		unsigned long flags;
307		int limit = 10000;
308
309		spin_lock_irqsave(&port->lock, flags);
310
311		while (limit-- > 0) {
312			long status = sun4v_con_putchar(CON_BREAK);
313			if (status == HV_EOK)
314				break;
315			udelay(1);
316		}
317
318		spin_unlock_irqrestore(&port->lock, flags);
319	}
320}
321
322/* port->lock is not held.  */
323static int sunhv_startup(struct uart_port *port)
324{
325	return 0;
326}
327
328/* port->lock is not held.  */
329static void sunhv_shutdown(struct uart_port *port)
330{
331}
332
333/* port->lock is not held.  */
334static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
335			      struct ktermios *old)
336{
337	unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
338	unsigned int quot = uart_get_divisor(port, baud);
339	unsigned int iflag, cflag;
340	unsigned long flags;
341
342	spin_lock_irqsave(&port->lock, flags);
343
344	iflag = termios->c_iflag;
345	cflag = termios->c_cflag;
346
347	port->ignore_status_mask = 0;
348	if (iflag & IGNBRK)
349		port->ignore_status_mask |= IGNORE_BREAK;
350	if ((cflag & CREAD) == 0)
351		port->ignore_status_mask |= IGNORE_ALL;
352
353	uart_update_timeout(port, cflag,
354			    (port->uartclk / (16 * quot)));
355
356	spin_unlock_irqrestore(&port->lock, flags);
357}
358
359static const char *sunhv_type(struct uart_port *port)
360{
361	return "SUN4V HCONS";
362}
363
364static void sunhv_release_port(struct uart_port *port)
365{
366}
367
368static int sunhv_request_port(struct uart_port *port)
369{
370	return 0;
371}
372
373static void sunhv_config_port(struct uart_port *port, int flags)
374{
375}
376
377static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
378{
379	return -EINVAL;
380}
381
382static struct uart_ops sunhv_pops = {
383	.tx_empty	= sunhv_tx_empty,
384	.set_mctrl	= sunhv_set_mctrl,
385	.get_mctrl	= sunhv_get_mctrl,
386	.stop_tx	= sunhv_stop_tx,
387	.start_tx	= sunhv_start_tx,
388	.send_xchar	= sunhv_send_xchar,
389	.stop_rx	= sunhv_stop_rx,
390	.enable_ms	= sunhv_enable_ms,
391	.break_ctl	= sunhv_break_ctl,
392	.startup	= sunhv_startup,
393	.shutdown	= sunhv_shutdown,
394	.set_termios	= sunhv_set_termios,
395	.type		= sunhv_type,
396	.release_port	= sunhv_release_port,
397	.request_port	= sunhv_request_port,
398	.config_port	= sunhv_config_port,
399	.verify_port	= sunhv_verify_port,
400};
401
402static struct uart_driver sunhv_reg = {
403	.owner			= THIS_MODULE,
404	.driver_name		= "serial",
405	.dev_name		= "ttyS",
406	.major			= TTY_MAJOR,
407};
408
409static struct uart_port *sunhv_port;
410
411/* Copy 's' into the con_write_page, decoding "\n" into
412 * "\r\n" along the way.  We have to return two lengths
413 * because the caller needs to know how much to advance
414 * 's' and also how many bytes to output via con_write_page.
415 */
416static int fill_con_write_page(const char *s, unsigned int n,
417			       unsigned long *page_bytes)
418{
419	const char *orig_s = s;
420	char *p = con_write_page;
421	int left = PAGE_SIZE;
422
423	while (n--) {
424		if (*s == '\n') {
425			if (left < 2)
426				break;
427			*p++ = '\r';
428			left--;
429		} else if (left < 1)
430			break;
431		*p++ = *s++;
432		left--;
433	}
434	*page_bytes = p - con_write_page;
435	return s - orig_s;
436}
437
438static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n)
439{
440	struct uart_port *port = sunhv_port;
441	unsigned long flags;
442
443	spin_lock_irqsave(&port->lock, flags);
444	while (n > 0) {
445		unsigned long ra = __pa(con_write_page);
446		unsigned long page_bytes;
447		unsigned int cpy = fill_con_write_page(s, n,
448						       &page_bytes);
449
450		n -= cpy;
451		s += cpy;
452		while (page_bytes > 0) {
453			unsigned long written;
454			int limit = 1000000;
455
456			while (limit--) {
457				unsigned long stat;
458
459				stat = sun4v_con_write(ra, page_bytes,
460						       &written);
461				if (stat == HV_EOK)
462					break;
463				udelay(1);
464			}
465			if (limit <= 0)
466				break;
467			page_bytes -= written;
468			ra += written;
469		}
470	}
471	spin_unlock_irqrestore(&port->lock, flags);
472}
473
474static inline void sunhv_console_putchar(struct uart_port *port, char c)
475{
476	int limit = 1000000;
477
478	while (limit-- > 0) {
479		long status = sun4v_con_putchar(c);
480		if (status == HV_EOK)
481			break;
482		udelay(1);
483	}
484}
485
486static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n)
487{
488	struct uart_port *port = sunhv_port;
489	unsigned long flags;
490	int i;
491
492	spin_lock_irqsave(&port->lock, flags);
493	for (i = 0; i < n; i++) {
494		if (*s == '\n')
495			sunhv_console_putchar(port, '\r');
496		sunhv_console_putchar(port, *s++);
497	}
498	spin_unlock_irqrestore(&port->lock, flags);
499}
500
501static struct console sunhv_console = {
502	.name	=	"ttyHV",
503	.write	=	sunhv_console_write_bychar,
504	.device	=	uart_console_device,
505	.flags	=	CON_PRINTBUFFER,
506	.index	=	-1,
507	.data	=	&sunhv_reg,
508};
509
510static inline struct console *SUNHV_CONSOLE(void)
511{
512	if (con_is_present())
513		return NULL;
514
515	sunhv_console.index = 0;
516
517	return &sunhv_console;
518}
519
520static int __devinit hv_probe(struct of_device *op, const struct of_device_id *match)
521{
522	struct uart_port *port;
523	unsigned long minor;
524	int err;
525
526	if (op->irqs[0] == 0xffffffff)
527		return -ENODEV;
528
529	port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
530	if (unlikely(!port))
531		return -ENOMEM;
532
533	minor = 1;
534	if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
535	    minor >= 1) {
536		err = -ENOMEM;
537		con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
538		if (!con_write_page)
539			goto out_free_port;
540
541		con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
542		if (!con_read_page)
543			goto out_free_con_write_page;
544
545		sunhv_console.write = sunhv_console_write_paged;
546		sunhv_ops = &bywrite_ops;
547	}
548
549	sunhv_port = port;
550
551	port->line = 0;
552	port->ops = &sunhv_pops;
553	port->type = PORT_SUNHV;
554	port->uartclk = ( 29491200 / 16 ); /* arbitrary */
555
556	port->membase = (unsigned char __iomem *) __pa(port);
557
558	port->irq = op->irqs[0];
559
560	port->dev = &op->dev;
561
562	sunhv_reg.minor = sunserial_current_minor;
563	sunhv_reg.nr = 1;
564
565	err = uart_register_driver(&sunhv_reg);
566	if (err)
567		goto out_free_con_read_page;
568
569	sunhv_reg.tty_driver->name_base = sunhv_reg.minor - 64;
570	sunserial_current_minor += 1;
571
572	sunhv_reg.cons = SUNHV_CONSOLE();
573
574	err = uart_add_one_port(&sunhv_reg, port);
575	if (err)
576		goto out_unregister_driver;
577
578	err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
579	if (err)
580		goto out_remove_port;
581
582	dev_set_drvdata(&op->dev, port);
583
584	return 0;
585
586out_remove_port:
587	uart_remove_one_port(&sunhv_reg, port);
588
589out_unregister_driver:
590	sunserial_current_minor -= 1;
591	uart_unregister_driver(&sunhv_reg);
592
593out_free_con_read_page:
594	kfree(con_read_page);
595
596out_free_con_write_page:
597	kfree(con_write_page);
598
599out_free_port:
600	kfree(port);
601	sunhv_port = NULL;
602	return err;
603}
604
605static int __devexit hv_remove(struct of_device *dev)
606{
607	struct uart_port *port = dev_get_drvdata(&dev->dev);
608
609	free_irq(port->irq, port);
610
611	uart_remove_one_port(&sunhv_reg, port);
612
613	sunserial_current_minor -= 1;
614	uart_unregister_driver(&sunhv_reg);
615
616	kfree(port);
617	sunhv_port = NULL;
618
619	dev_set_drvdata(&dev->dev, NULL);
620
621	return 0;
622}
623
624static struct of_device_id hv_match[] = {
625	{
626		.name = "console",
627		.compatible = "qcn",
628	},
629	{
630		.name = "console",
631		.compatible = "SUNW,sun4v-console",
632	},
633	{},
634};
635MODULE_DEVICE_TABLE(of, hv_match);
636
637static struct of_platform_driver hv_driver = {
638	.name		= "hv",
639	.match_table	= hv_match,
640	.probe		= hv_probe,
641	.remove		= __devexit_p(hv_remove),
642};
643
644static int __init sunhv_init(void)
645{
646	if (tlb_type != hypervisor)
647		return -ENODEV;
648
649	return of_register_driver(&hv_driver, &of_bus_type);
650}
651
652static void __exit sunhv_exit(void)
653{
654	of_unregister_driver(&hv_driver);
655}
656
657module_init(sunhv_init);
658module_exit(sunhv_exit);
659
660MODULE_AUTHOR("David S. Miller");
661MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
662MODULE_VERSION("2.0");
663MODULE_LICENSE("GPL");
664