blob: 5f9dec38db819d557bb6e121b95bd7c64210d4c2 [file] [log] [blame]
Kristoffer Glembod4ac42a2009-11-04 08:39:46 -08001/*
2 * Driver for GRLIB serial ports (APBUART)
3 *
4 * Based on linux/drivers/serial/amba.c
5 *
6 * Copyright (C) 2000 Deep Blue Solutions Ltd.
7 * Copyright (C) 2003 Konrad Eisele <eiselekd@web.de>
8 * Copyright (C) 2006 Daniel Hellstrom <daniel@gaisler.com>, Aeroflex Gaisler AB
9 * Copyright (C) 2008 Gilead Kutnick <kutnickg@zin-tech.com>
10 * Copyright (C) 2009 Kristoffer Glembo <kristoffer@gaisler.com>, Aeroflex Gaisler AB
11 */
12
13#if defined(CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
14#define SUPPORT_SYSRQ
15#endif
16
17#include <linux/module.h>
18#include <linux/tty.h>
19#include <linux/ioport.h>
20#include <linux/init.h>
21#include <linux/serial.h>
22#include <linux/console.h>
23#include <linux/sysrq.h>
24#include <linux/kthread.h>
25#include <linux/device.h>
26#include <linux/of.h>
27#include <linux/of_device.h>
28#include <linux/platform_device.h>
29#include <linux/io.h>
30#include <linux/serial_core.h>
31#include <asm/irq.h>
Kristoffer Glembod4ac42a2009-11-04 08:39:46 -080032
33#include "apbuart.h"
34
35#define SERIAL_APBUART_MAJOR TTY_MAJOR
36#define SERIAL_APBUART_MINOR 64
37#define UART_DUMMY_RSR_RX 0x8000 /* for ignore all read */
38
39static void apbuart_tx_chars(struct uart_port *port);
40
41static void apbuart_stop_tx(struct uart_port *port)
42{
43 unsigned int cr;
44
45 cr = UART_GET_CTRL(port);
46 cr &= ~UART_CTRL_TI;
47 UART_PUT_CTRL(port, cr);
48}
49
50static void apbuart_start_tx(struct uart_port *port)
51{
52 unsigned int cr;
53
54 cr = UART_GET_CTRL(port);
55 cr |= UART_CTRL_TI;
56 UART_PUT_CTRL(port, cr);
57
58 if (UART_GET_STATUS(port) & UART_STATUS_THE)
59 apbuart_tx_chars(port);
60}
61
62static void apbuart_stop_rx(struct uart_port *port)
63{
64 unsigned int cr;
65
66 cr = UART_GET_CTRL(port);
67 cr &= ~(UART_CTRL_RI);
68 UART_PUT_CTRL(port, cr);
69}
70
71static void apbuart_enable_ms(struct uart_port *port)
72{
73 /* No modem status change interrupts for APBUART */
74}
75
76static void apbuart_rx_chars(struct uart_port *port)
77{
78 struct tty_struct *tty = port->state->port.tty;
79 unsigned int status, ch, rsr, flag;
80 unsigned int max_chars = port->fifosize;
81
82 status = UART_GET_STATUS(port);
83
84 while (UART_RX_DATA(status) && (max_chars--)) {
85
86 ch = UART_GET_CHAR(port);
87 flag = TTY_NORMAL;
88
89 port->icount.rx++;
90
91 rsr = UART_GET_STATUS(port) | UART_DUMMY_RSR_RX;
92 UART_PUT_STATUS(port, 0);
93 if (rsr & UART_STATUS_ERR) {
94
95 if (rsr & UART_STATUS_BR) {
96 rsr &= ~(UART_STATUS_FE | UART_STATUS_PE);
97 port->icount.brk++;
98 if (uart_handle_break(port))
99 goto ignore_char;
100 } else if (rsr & UART_STATUS_PE) {
101 port->icount.parity++;
102 } else if (rsr & UART_STATUS_FE) {
103 port->icount.frame++;
104 }
105 if (rsr & UART_STATUS_OE)
106 port->icount.overrun++;
107
108 rsr &= port->read_status_mask;
109
110 if (rsr & UART_STATUS_PE)
111 flag = TTY_PARITY;
112 else if (rsr & UART_STATUS_FE)
113 flag = TTY_FRAME;
114 }
115
116 if (uart_handle_sysrq_char(port, ch))
117 goto ignore_char;
118
119 uart_insert_char(port, rsr, UART_STATUS_OE, ch, flag);
120
121
122 ignore_char:
123 status = UART_GET_STATUS(port);
124 }
125
126 tty_flip_buffer_push(tty);
127}
128
129static void apbuart_tx_chars(struct uart_port *port)
130{
131 struct circ_buf *xmit = &port->state->xmit;
132 int count;
133
134 if (port->x_char) {
135 UART_PUT_CHAR(port, port->x_char);
136 port->icount.tx++;
137 port->x_char = 0;
138 return;
139 }
140
141 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
142 apbuart_stop_tx(port);
143 return;
144 }
145
146 /* amba: fill FIFO */
147 count = port->fifosize >> 1;
148 do {
149 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
150 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
151 port->icount.tx++;
152 if (uart_circ_empty(xmit))
153 break;
154 } while (--count > 0);
155
156 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
157 uart_write_wakeup(port);
158
159 if (uart_circ_empty(xmit))
160 apbuart_stop_tx(port);
161}
162
163static irqreturn_t apbuart_int(int irq, void *dev_id)
164{
165 struct uart_port *port = dev_id;
166 unsigned int status;
167
168 spin_lock(&port->lock);
169
170 status = UART_GET_STATUS(port);
171 if (status & UART_STATUS_DR)
172 apbuart_rx_chars(port);
173 if (status & UART_STATUS_THE)
174 apbuart_tx_chars(port);
175
176 spin_unlock(&port->lock);
177
178 return IRQ_HANDLED;
179}
180
181static unsigned int apbuart_tx_empty(struct uart_port *port)
182{
183 unsigned int status = UART_GET_STATUS(port);
184 return status & UART_STATUS_THE ? TIOCSER_TEMT : 0;
185}
186
187static unsigned int apbuart_get_mctrl(struct uart_port *port)
188{
189 /* The GRLIB APBUART handles flow control in hardware */
190 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
191}
192
193static void apbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
194{
195 /* The GRLIB APBUART handles flow control in hardware */
196}
197
198static void apbuart_break_ctl(struct uart_port *port, int break_state)
199{
200 /* We don't support sending break */
201}
202
203static int apbuart_startup(struct uart_port *port)
204{
205 int retval;
206 unsigned int cr;
207
208 /* Allocate the IRQ */
209 retval = request_irq(port->irq, apbuart_int, 0, "apbuart", port);
210 if (retval)
211 return retval;
212
213 /* Finally, enable interrupts */
214 cr = UART_GET_CTRL(port);
215 UART_PUT_CTRL(port,
216 cr | UART_CTRL_RE | UART_CTRL_TE |
217 UART_CTRL_RI | UART_CTRL_TI);
218
219 return 0;
220}
221
222static void apbuart_shutdown(struct uart_port *port)
223{
224 unsigned int cr;
225
226 /* disable all interrupts, disable the port */
227 cr = UART_GET_CTRL(port);
228 UART_PUT_CTRL(port,
229 cr & ~(UART_CTRL_RE | UART_CTRL_TE |
230 UART_CTRL_RI | UART_CTRL_TI));
231
232 /* Free the interrupt */
233 free_irq(port->irq, port);
234}
235
236static void apbuart_set_termios(struct uart_port *port,
237 struct ktermios *termios, struct ktermios *old)
238{
239 unsigned int cr;
240 unsigned long flags;
241 unsigned int baud, quot;
242
243 /* Ask the core to calculate the divisor for us. */
244 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
245 if (baud == 0)
246 panic("invalid baudrate %i\n", port->uartclk / 16);
247
248 /* uart_get_divisor calc a *16 uart freq, apbuart is *8 */
249 quot = (uart_get_divisor(port, baud)) * 2;
250 cr = UART_GET_CTRL(port);
251 cr &= ~(UART_CTRL_PE | UART_CTRL_PS);
252
253 if (termios->c_cflag & PARENB) {
254 cr |= UART_CTRL_PE;
255 if ((termios->c_cflag & PARODD))
256 cr |= UART_CTRL_PS;
257 }
258
259 /* Enable flow control. */
260 if (termios->c_cflag & CRTSCTS)
261 cr |= UART_CTRL_FL;
262
263 spin_lock_irqsave(&port->lock, flags);
264
265 /* Update the per-port timeout. */
266 uart_update_timeout(port, termios->c_cflag, baud);
267
268 port->read_status_mask = UART_STATUS_OE;
269 if (termios->c_iflag & INPCK)
270 port->read_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
271
272 /* Characters to ignore */
273 port->ignore_status_mask = 0;
274 if (termios->c_iflag & IGNPAR)
275 port->ignore_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
276
277 /* Ignore all characters if CREAD is not set. */
278 if ((termios->c_cflag & CREAD) == 0)
279 port->ignore_status_mask |= UART_DUMMY_RSR_RX;
280
281 /* Set baud rate */
282 quot -= 1;
283 UART_PUT_SCAL(port, quot);
284 UART_PUT_CTRL(port, cr);
285
286 spin_unlock_irqrestore(&port->lock, flags);
287}
288
289static const char *apbuart_type(struct uart_port *port)
290{
291 return port->type == PORT_APBUART ? "GRLIB/APBUART" : NULL;
292}
293
294static void apbuart_release_port(struct uart_port *port)
295{
296 release_mem_region(port->mapbase, 0x100);
297}
298
299static int apbuart_request_port(struct uart_port *port)
300{
301 return request_mem_region(port->mapbase, 0x100, "grlib-apbuart")
302 != NULL ? 0 : -EBUSY;
303 return 0;
304}
305
306/* Configure/autoconfigure the port */
307static void apbuart_config_port(struct uart_port *port, int flags)
308{
309 if (flags & UART_CONFIG_TYPE) {
310 port->type = PORT_APBUART;
311 apbuart_request_port(port);
312 }
313}
314
315/* Verify the new serial_struct (for TIOCSSERIAL) */
316static int apbuart_verify_port(struct uart_port *port,
317 struct serial_struct *ser)
318{
319 int ret = 0;
320 if (ser->type != PORT_UNKNOWN && ser->type != PORT_APBUART)
321 ret = -EINVAL;
322 if (ser->irq < 0 || ser->irq >= NR_IRQS)
323 ret = -EINVAL;
324 if (ser->baud_base < 9600)
325 ret = -EINVAL;
326 return ret;
327}
328
329static struct uart_ops grlib_apbuart_ops = {
330 .tx_empty = apbuart_tx_empty,
331 .set_mctrl = apbuart_set_mctrl,
332 .get_mctrl = apbuart_get_mctrl,
333 .stop_tx = apbuart_stop_tx,
334 .start_tx = apbuart_start_tx,
335 .stop_rx = apbuart_stop_rx,
336 .enable_ms = apbuart_enable_ms,
337 .break_ctl = apbuart_break_ctl,
338 .startup = apbuart_startup,
339 .shutdown = apbuart_shutdown,
340 .set_termios = apbuart_set_termios,
341 .type = apbuart_type,
342 .release_port = apbuart_release_port,
343 .request_port = apbuart_request_port,
344 .config_port = apbuart_config_port,
345 .verify_port = apbuart_verify_port,
346};
347
348static struct uart_port grlib_apbuart_ports[UART_NR];
349static struct device_node *grlib_apbuart_nodes[UART_NR];
350
351static int apbuart_scan_fifo_size(struct uart_port *port, int portnumber)
352{
353 int ctrl, loop = 0;
354 int status;
355 int fifosize;
356 unsigned long flags;
357
358 ctrl = UART_GET_CTRL(port);
359
360 /*
361 * Enable the transceiver and wait for it to be ready to send data.
362 * Clear interrupts so that this process will not be externally
363 * interrupted in the middle (which can cause the transceiver to
364 * drain prematurely).
365 */
366
367 local_irq_save(flags);
368
369 UART_PUT_CTRL(port, ctrl | UART_CTRL_TE);
370
371 while (!UART_TX_READY(UART_GET_STATUS(port)))
372 loop++;
373
374 /*
375 * Disable the transceiver so data isn't actually sent during the
376 * actual test.
377 */
378
379 UART_PUT_CTRL(port, ctrl & ~(UART_CTRL_TE));
380
381 fifosize = 1;
382 UART_PUT_CHAR(port, 0);
383
384 /*
385 * So long as transmitting a character increments the tranceivier FIFO
386 * length the FIFO must be at least that big. These bytes will
387 * automatically drain off of the FIFO.
388 */
389
390 status = UART_GET_STATUS(port);
391 while (((status >> 20) & 0x3F) == fifosize) {
392 fifosize++;
393 UART_PUT_CHAR(port, 0);
394 status = UART_GET_STATUS(port);
395 }
396
397 fifosize--;
398
399 UART_PUT_CTRL(port, ctrl);
400 local_irq_restore(flags);
401
402 if (fifosize == 0)
403 fifosize = 1;
404
405 return fifosize;
406}
407
408static void apbuart_flush_fifo(struct uart_port *port)
409{
410 int i;
411
412 for (i = 0; i < port->fifosize; i++)
413 UART_GET_CHAR(port);
414}
415
416
417/* ======================================================================== */
418/* Console driver, if enabled */
419/* ======================================================================== */
420
421#ifdef CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
422
423static void apbuart_console_putchar(struct uart_port *port, int ch)
424{
425 unsigned int status;
426 do {
427 status = UART_GET_STATUS(port);
428 } while (!UART_TX_READY(status));
429 UART_PUT_CHAR(port, ch);
430}
431
432static void
433apbuart_console_write(struct console *co, const char *s, unsigned int count)
434{
435 struct uart_port *port = &grlib_apbuart_ports[co->index];
436 unsigned int status, old_cr, new_cr;
437
438 /* First save the CR then disable the interrupts */
439 old_cr = UART_GET_CTRL(port);
440 new_cr = old_cr & ~(UART_CTRL_RI | UART_CTRL_TI);
441 UART_PUT_CTRL(port, new_cr);
442
443 uart_console_write(port, s, count, apbuart_console_putchar);
444
445 /*
446 * Finally, wait for transmitter to become empty
447 * and restore the TCR
448 */
449 do {
450 status = UART_GET_STATUS(port);
451 } while (!UART_TX_READY(status));
452 UART_PUT_CTRL(port, old_cr);
453}
454
455static void __init
456apbuart_console_get_options(struct uart_port *port, int *baud,
457 int *parity, int *bits)
458{
459 if (UART_GET_CTRL(port) & (UART_CTRL_RE | UART_CTRL_TE)) {
460
461 unsigned int quot, status;
462 status = UART_GET_STATUS(port);
463
464 *parity = 'n';
465 if (status & UART_CTRL_PE) {
466 if ((status & UART_CTRL_PS) == 0)
467 *parity = 'e';
468 else
469 *parity = 'o';
470 }
471
472 *bits = 8;
473 quot = UART_GET_SCAL(port) / 8;
474 *baud = port->uartclk / (16 * (quot + 1));
475 }
476}
477
478static int __init apbuart_console_setup(struct console *co, char *options)
479{
480 struct uart_port *port;
481 int baud = 38400;
482 int bits = 8;
483 int parity = 'n';
484 int flow = 'n';
485
486 pr_debug("apbuart_console_setup co=%p, co->index=%i, options=%s\n",
487 co, co->index, options);
488
489 /*
490 * Check whether an invalid uart number has been specified, and
491 * if so, search for the first available port that does have
492 * console support.
493 */
494 if (co->index >= grlib_apbuart_port_nr)
495 co->index = 0;
496
497 port = &grlib_apbuart_ports[co->index];
498
499 spin_lock_init(&port->lock);
500
501 if (options)
502 uart_parse_options(options, &baud, &parity, &bits, &flow);
503 else
504 apbuart_console_get_options(port, &baud, &parity, &bits);
505
506 return uart_set_options(port, co, baud, parity, bits, flow);
507}
508
509static struct uart_driver grlib_apbuart_driver;
510
511static struct console grlib_apbuart_console = {
512 .name = "ttyS",
513 .write = apbuart_console_write,
514 .device = uart_console_device,
515 .setup = apbuart_console_setup,
516 .flags = CON_PRINTBUFFER,
517 .index = -1,
518 .data = &grlib_apbuart_driver,
519};
520
521
522static void grlib_apbuart_configure(void);
523
524static int __init apbuart_console_init(void)
525{
526 grlib_apbuart_configure();
527 register_console(&grlib_apbuart_console);
528 return 0;
529}
530
531console_initcall(apbuart_console_init);
532
533#define APBUART_CONSOLE (&grlib_apbuart_console)
534#else
535#define APBUART_CONSOLE NULL
536#endif
537
538static struct uart_driver grlib_apbuart_driver = {
539 .owner = THIS_MODULE,
540 .driver_name = "serial",
541 .dev_name = "ttyS",
542 .major = SERIAL_APBUART_MAJOR,
543 .minor = SERIAL_APBUART_MINOR,
544 .nr = UART_NR,
545 .cons = APBUART_CONSOLE,
546};
547
548
549/* ======================================================================== */
550/* OF Platform Driver */
551/* ======================================================================== */
552
553static int __devinit apbuart_probe(struct of_device *op,
554 const struct of_device_id *match)
555{
556 int i = -1;
557 struct uart_port *port = NULL;
558
559 i = 0;
560 for (i = 0; i < grlib_apbuart_port_nr; i++) {
561 if (op->node == grlib_apbuart_nodes[i])
562 break;
563 }
564
565 port = &grlib_apbuart_ports[i];
566 port->dev = &op->dev;
567
568 uart_add_one_port(&grlib_apbuart_driver, (struct uart_port *) port);
569
570 apbuart_flush_fifo((struct uart_port *) port);
571
David S. Miller384a17b2009-11-04 08:47:03 -0800572 printk(KERN_INFO "grlib-apbuart at 0x%llx, irq %d\n",
573 (unsigned long long) port->mapbase, port->irq);
Kristoffer Glembod4ac42a2009-11-04 08:39:46 -0800574 return 0;
575
576}
577
578static struct of_device_id __initdata apbuart_match[] = {
579 {
580 .name = "GAISLER_APBUART",
581 },
582 {},
583};
584
585static struct of_platform_driver grlib_apbuart_of_driver = {
586 .match_table = apbuart_match,
587 .probe = apbuart_probe,
588 .driver = {
589 .owner = THIS_MODULE,
590 .name = "grlib-apbuart",
591 },
592};
593
594
595static void grlib_apbuart_configure(void)
596{
597 static int enum_done;
David S. Millerd1350092009-11-04 23:59:18 -0800598 struct device_node *np, *rp;
Kristoffer Glembod4ac42a2009-11-04 08:39:46 -0800599 struct uart_port *port = NULL;
David S. Millerd1350092009-11-04 23:59:18 -0800600 const u32 *prop;
Kristoffer Glembod4ac42a2009-11-04 08:39:46 -0800601 int freq_khz;
602 int v = 0, d = 0;
603 unsigned int addr;
604 int irq, line;
605 struct amba_prom_registers *regs;
606
607 if (enum_done)
608 return;
609
610 /* Get bus frequency */
David S. Millerd1350092009-11-04 23:59:18 -0800611 rp = of_find_node_by_name(NULL, "/");
612 rp = of_get_next_child(rp, NULL);
613 prop = of_get_property(rp, "clock-frequency", NULL);
614 freq_khz = *prop;
Kristoffer Glembod4ac42a2009-11-04 08:39:46 -0800615
616 line = 0;
617 for_each_matching_node(np, apbuart_match) {
618
619 int *vendor = (int *) of_get_property(np, "vendor", NULL);
620 int *device = (int *) of_get_property(np, "device", NULL);
621 int *irqs = (int *) of_get_property(np, "interrupts", NULL);
622 regs = (struct amba_prom_registers *)
623 of_get_property(np, "reg", NULL);
624
625 if (vendor)
626 v = *vendor;
627 if (device)
628 d = *device;
629
630 if (!irqs || !regs)
631 return;
632
633 grlib_apbuart_nodes[line] = np;
634
635 addr = regs->phys_addr;
636 irq = *irqs;
637
638 port = &grlib_apbuart_ports[line];
639
640 port->mapbase = addr;
641 port->membase = ioremap(addr, sizeof(struct grlib_apbuart_regs_map));
642 port->irq = irq;
643 port->iotype = UPIO_MEM;
644 port->ops = &grlib_apbuart_ops;
645 port->flags = UPF_BOOT_AUTOCONF;
646 port->line = line;
647 port->uartclk = freq_khz * 1000;
648 port->fifosize = apbuart_scan_fifo_size((struct uart_port *) port, line);
649 line++;
650
651 /* We support maximum UART_NR uarts ... */
652 if (line == UART_NR)
653 break;
654
655 }
656
657 enum_done = 1;
658
659 grlib_apbuart_driver.nr = grlib_apbuart_port_nr = line;
660}
661
662static int __init grlib_apbuart_init(void)
663{
664 int ret;
665
666 /* Find all APBUARTS in device the tree and initialize their ports */
667 grlib_apbuart_configure();
668
669 printk(KERN_INFO "Serial: GRLIB APBUART driver\n");
670
671 ret = uart_register_driver(&grlib_apbuart_driver);
672
673 if (ret) {
674 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
675 __FILE__, ret);
676 return ret;
677 }
678
679 ret = of_register_driver(&grlib_apbuart_of_driver, &of_platform_bus_type);
680
681 if (ret) {
682 printk(KERN_ERR
683 "%s: of_register_platform_driver failed (%i)\n",
684 __FILE__, ret);
685 uart_unregister_driver(&grlib_apbuart_driver);
686 return ret;
687 }
688
689 return ret;
690}
691
692static void __exit grlib_apbuart_exit(void)
693{
694 int i;
695
696 for (i = 0; i < grlib_apbuart_port_nr; i++)
697 uart_remove_one_port(&grlib_apbuart_driver,
698 &grlib_apbuart_ports[i]);
699
700 uart_unregister_driver(&grlib_apbuart_driver);
701
702}
703
704module_init(grlib_apbuart_init);
705module_exit(grlib_apbuart_exit);
706
707MODULE_AUTHOR("Aeroflex Gaisler AB");
708MODULE_DESCRIPTION("GRLIB APBUART serial driver");
709MODULE_VERSION("2.1");
710MODULE_LICENSE("GPL");