2 * Driver for GRLIB serial ports (APBUART)
4 * Based on linux/drivers/serial/amba.c
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
13 #if defined(CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
17 #include <linux/module.h>
18 #include <linux/tty.h>
19 #include <linux/tty_flip.h>
20 #include <linux/ioport.h>
21 #include <linux/init.h>
22 #include <linux/serial.h>
23 #include <linux/console.h>
24 #include <linux/sysrq.h>
25 #include <linux/kthread.h>
26 #include <linux/device.h>
28 #include <linux/of_device.h>
29 #include <linux/of_platform.h>
30 #include <linux/of_irq.h>
31 #include <linux/platform_device.h>
33 #include <linux/serial_core.h>
38 #define SERIAL_APBUART_MAJOR TTY_MAJOR
39 #define SERIAL_APBUART_MINOR 64
40 #define UART_DUMMY_RSR_RX 0x8000 /* for ignore all read */
42 static void apbuart_tx_chars(struct uart_port
*port
);
44 static void apbuart_stop_tx(struct uart_port
*port
)
48 cr
= UART_GET_CTRL(port
);
50 UART_PUT_CTRL(port
, cr
);
53 static void apbuart_start_tx(struct uart_port
*port
)
57 cr
= UART_GET_CTRL(port
);
59 UART_PUT_CTRL(port
, cr
);
61 if (UART_GET_STATUS(port
) & UART_STATUS_THE
)
62 apbuart_tx_chars(port
);
65 static void apbuart_stop_rx(struct uart_port
*port
)
69 cr
= UART_GET_CTRL(port
);
70 cr
&= ~(UART_CTRL_RI
);
71 UART_PUT_CTRL(port
, cr
);
74 static void apbuart_enable_ms(struct uart_port
*port
)
76 /* No modem status change interrupts for APBUART */
79 static void apbuart_rx_chars(struct uart_port
*port
)
81 unsigned int status
, ch
, rsr
, flag
;
82 unsigned int max_chars
= port
->fifosize
;
84 status
= UART_GET_STATUS(port
);
86 while (UART_RX_DATA(status
) && (max_chars
--)) {
88 ch
= UART_GET_CHAR(port
);
93 rsr
= UART_GET_STATUS(port
) | UART_DUMMY_RSR_RX
;
94 UART_PUT_STATUS(port
, 0);
95 if (rsr
& UART_STATUS_ERR
) {
97 if (rsr
& UART_STATUS_BR
) {
98 rsr
&= ~(UART_STATUS_FE
| UART_STATUS_PE
);
100 if (uart_handle_break(port
))
102 } else if (rsr
& UART_STATUS_PE
) {
103 port
->icount
.parity
++;
104 } else if (rsr
& UART_STATUS_FE
) {
105 port
->icount
.frame
++;
107 if (rsr
& UART_STATUS_OE
)
108 port
->icount
.overrun
++;
110 rsr
&= port
->read_status_mask
;
112 if (rsr
& UART_STATUS_PE
)
114 else if (rsr
& UART_STATUS_FE
)
118 if (uart_handle_sysrq_char(port
, ch
))
121 uart_insert_char(port
, rsr
, UART_STATUS_OE
, ch
, flag
);
125 status
= UART_GET_STATUS(port
);
128 spin_unlock(&port
->lock
);
129 tty_flip_buffer_push(&port
->state
->port
);
130 spin_lock(&port
->lock
);
133 static void apbuart_tx_chars(struct uart_port
*port
)
135 struct circ_buf
*xmit
= &port
->state
->xmit
;
139 UART_PUT_CHAR(port
, port
->x_char
);
145 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
146 apbuart_stop_tx(port
);
150 /* amba: fill FIFO */
151 count
= port
->fifosize
>> 1;
153 UART_PUT_CHAR(port
, xmit
->buf
[xmit
->tail
]);
154 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
156 if (uart_circ_empty(xmit
))
158 } while (--count
> 0);
160 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
161 uart_write_wakeup(port
);
163 if (uart_circ_empty(xmit
))
164 apbuart_stop_tx(port
);
167 static irqreturn_t
apbuart_int(int irq
, void *dev_id
)
169 struct uart_port
*port
= dev_id
;
172 spin_lock(&port
->lock
);
174 status
= UART_GET_STATUS(port
);
175 if (status
& UART_STATUS_DR
)
176 apbuart_rx_chars(port
);
177 if (status
& UART_STATUS_THE
)
178 apbuart_tx_chars(port
);
180 spin_unlock(&port
->lock
);
185 static unsigned int apbuart_tx_empty(struct uart_port
*port
)
187 unsigned int status
= UART_GET_STATUS(port
);
188 return status
& UART_STATUS_THE
? TIOCSER_TEMT
: 0;
191 static unsigned int apbuart_get_mctrl(struct uart_port
*port
)
193 /* The GRLIB APBUART handles flow control in hardware */
194 return TIOCM_CAR
| TIOCM_DSR
| TIOCM_CTS
;
197 static void apbuart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
199 /* The GRLIB APBUART handles flow control in hardware */
202 static void apbuart_break_ctl(struct uart_port
*port
, int break_state
)
204 /* We don't support sending break */
207 static int apbuart_startup(struct uart_port
*port
)
212 /* Allocate the IRQ */
213 retval
= request_irq(port
->irq
, apbuart_int
, 0, "apbuart", port
);
217 /* Finally, enable interrupts */
218 cr
= UART_GET_CTRL(port
);
220 cr
| UART_CTRL_RE
| UART_CTRL_TE
|
221 UART_CTRL_RI
| UART_CTRL_TI
);
226 static void apbuart_shutdown(struct uart_port
*port
)
230 /* disable all interrupts, disable the port */
231 cr
= UART_GET_CTRL(port
);
233 cr
& ~(UART_CTRL_RE
| UART_CTRL_TE
|
234 UART_CTRL_RI
| UART_CTRL_TI
));
236 /* Free the interrupt */
237 free_irq(port
->irq
, port
);
240 static void apbuart_set_termios(struct uart_port
*port
,
241 struct ktermios
*termios
, struct ktermios
*old
)
245 unsigned int baud
, quot
;
247 /* Ask the core to calculate the divisor for us. */
248 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/ 16);
250 panic("invalid baudrate %i\n", port
->uartclk
/ 16);
252 /* uart_get_divisor calc a *16 uart freq, apbuart is *8 */
253 quot
= (uart_get_divisor(port
, baud
)) * 2;
254 cr
= UART_GET_CTRL(port
);
255 cr
&= ~(UART_CTRL_PE
| UART_CTRL_PS
);
257 if (termios
->c_cflag
& PARENB
) {
259 if ((termios
->c_cflag
& PARODD
))
263 /* Enable flow control. */
264 if (termios
->c_cflag
& CRTSCTS
)
267 spin_lock_irqsave(&port
->lock
, flags
);
269 /* Update the per-port timeout. */
270 uart_update_timeout(port
, termios
->c_cflag
, baud
);
272 port
->read_status_mask
= UART_STATUS_OE
;
273 if (termios
->c_iflag
& INPCK
)
274 port
->read_status_mask
|= UART_STATUS_FE
| UART_STATUS_PE
;
276 /* Characters to ignore */
277 port
->ignore_status_mask
= 0;
278 if (termios
->c_iflag
& IGNPAR
)
279 port
->ignore_status_mask
|= UART_STATUS_FE
| UART_STATUS_PE
;
281 /* Ignore all characters if CREAD is not set. */
282 if ((termios
->c_cflag
& CREAD
) == 0)
283 port
->ignore_status_mask
|= UART_DUMMY_RSR_RX
;
287 UART_PUT_SCAL(port
, quot
);
288 UART_PUT_CTRL(port
, cr
);
290 spin_unlock_irqrestore(&port
->lock
, flags
);
293 static const char *apbuart_type(struct uart_port
*port
)
295 return port
->type
== PORT_APBUART
? "GRLIB/APBUART" : NULL
;
298 static void apbuart_release_port(struct uart_port
*port
)
300 release_mem_region(port
->mapbase
, 0x100);
303 static int apbuart_request_port(struct uart_port
*port
)
305 return request_mem_region(port
->mapbase
, 0x100, "grlib-apbuart")
306 != NULL
? 0 : -EBUSY
;
310 /* Configure/autoconfigure the port */
311 static void apbuart_config_port(struct uart_port
*port
, int flags
)
313 if (flags
& UART_CONFIG_TYPE
) {
314 port
->type
= PORT_APBUART
;
315 apbuart_request_port(port
);
319 /* Verify the new serial_struct (for TIOCSSERIAL) */
320 static int apbuart_verify_port(struct uart_port
*port
,
321 struct serial_struct
*ser
)
324 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_APBUART
)
326 if (ser
->irq
< 0 || ser
->irq
>= NR_IRQS
)
328 if (ser
->baud_base
< 9600)
333 static struct uart_ops grlib_apbuart_ops
= {
334 .tx_empty
= apbuart_tx_empty
,
335 .set_mctrl
= apbuart_set_mctrl
,
336 .get_mctrl
= apbuart_get_mctrl
,
337 .stop_tx
= apbuart_stop_tx
,
338 .start_tx
= apbuart_start_tx
,
339 .stop_rx
= apbuart_stop_rx
,
340 .enable_ms
= apbuart_enable_ms
,
341 .break_ctl
= apbuart_break_ctl
,
342 .startup
= apbuart_startup
,
343 .shutdown
= apbuart_shutdown
,
344 .set_termios
= apbuart_set_termios
,
345 .type
= apbuart_type
,
346 .release_port
= apbuart_release_port
,
347 .request_port
= apbuart_request_port
,
348 .config_port
= apbuart_config_port
,
349 .verify_port
= apbuart_verify_port
,
352 static struct uart_port grlib_apbuart_ports
[UART_NR
];
353 static struct device_node
*grlib_apbuart_nodes
[UART_NR
];
355 static int apbuart_scan_fifo_size(struct uart_port
*port
, int portnumber
)
362 ctrl
= UART_GET_CTRL(port
);
365 * Enable the transceiver and wait for it to be ready to send data.
366 * Clear interrupts so that this process will not be externally
367 * interrupted in the middle (which can cause the transceiver to
368 * drain prematurely).
371 local_irq_save(flags
);
373 UART_PUT_CTRL(port
, ctrl
| UART_CTRL_TE
);
375 while (!UART_TX_READY(UART_GET_STATUS(port
)))
379 * Disable the transceiver so data isn't actually sent during the
383 UART_PUT_CTRL(port
, ctrl
& ~(UART_CTRL_TE
));
386 UART_PUT_CHAR(port
, 0);
389 * So long as transmitting a character increments the tranceivier FIFO
390 * length the FIFO must be at least that big. These bytes will
391 * automatically drain off of the FIFO.
394 status
= UART_GET_STATUS(port
);
395 while (((status
>> 20) & 0x3F) == fifosize
) {
397 UART_PUT_CHAR(port
, 0);
398 status
= UART_GET_STATUS(port
);
403 UART_PUT_CTRL(port
, ctrl
);
404 local_irq_restore(flags
);
412 static void apbuart_flush_fifo(struct uart_port
*port
)
416 for (i
= 0; i
< port
->fifosize
; i
++)
421 /* ======================================================================== */
422 /* Console driver, if enabled */
423 /* ======================================================================== */
425 #ifdef CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
427 static void apbuart_console_putchar(struct uart_port
*port
, int ch
)
431 status
= UART_GET_STATUS(port
);
432 } while (!UART_TX_READY(status
));
433 UART_PUT_CHAR(port
, ch
);
437 apbuart_console_write(struct console
*co
, const char *s
, unsigned int count
)
439 struct uart_port
*port
= &grlib_apbuart_ports
[co
->index
];
440 unsigned int status
, old_cr
, new_cr
;
442 /* First save the CR then disable the interrupts */
443 old_cr
= UART_GET_CTRL(port
);
444 new_cr
= old_cr
& ~(UART_CTRL_RI
| UART_CTRL_TI
);
445 UART_PUT_CTRL(port
, new_cr
);
447 uart_console_write(port
, s
, count
, apbuart_console_putchar
);
450 * Finally, wait for transmitter to become empty
451 * and restore the TCR
454 status
= UART_GET_STATUS(port
);
455 } while (!UART_TX_READY(status
));
456 UART_PUT_CTRL(port
, old_cr
);
460 apbuart_console_get_options(struct uart_port
*port
, int *baud
,
461 int *parity
, int *bits
)
463 if (UART_GET_CTRL(port
) & (UART_CTRL_RE
| UART_CTRL_TE
)) {
465 unsigned int quot
, status
;
466 status
= UART_GET_STATUS(port
);
469 if (status
& UART_CTRL_PE
) {
470 if ((status
& UART_CTRL_PS
) == 0)
477 quot
= UART_GET_SCAL(port
) / 8;
478 *baud
= port
->uartclk
/ (16 * (quot
+ 1));
482 static int __init
apbuart_console_setup(struct console
*co
, char *options
)
484 struct uart_port
*port
;
490 pr_debug("apbuart_console_setup co=%p, co->index=%i, options=%s\n",
491 co
, co
->index
, options
);
494 * Check whether an invalid uart number has been specified, and
495 * if so, search for the first available port that does have
498 if (co
->index
>= grlib_apbuart_port_nr
)
501 port
= &grlib_apbuart_ports
[co
->index
];
503 spin_lock_init(&port
->lock
);
506 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
508 apbuart_console_get_options(port
, &baud
, &parity
, &bits
);
510 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
513 static struct uart_driver grlib_apbuart_driver
;
515 static struct console grlib_apbuart_console
= {
517 .write
= apbuart_console_write
,
518 .device
= uart_console_device
,
519 .setup
= apbuart_console_setup
,
520 .flags
= CON_PRINTBUFFER
,
522 .data
= &grlib_apbuart_driver
,
526 static int grlib_apbuart_configure(void);
528 static int __init
apbuart_console_init(void)
530 if (grlib_apbuart_configure())
532 register_console(&grlib_apbuart_console
);
536 console_initcall(apbuart_console_init
);
538 #define APBUART_CONSOLE (&grlib_apbuart_console)
540 #define APBUART_CONSOLE NULL
543 static struct uart_driver grlib_apbuart_driver
= {
544 .owner
= THIS_MODULE
,
545 .driver_name
= "serial",
547 .major
= SERIAL_APBUART_MAJOR
,
548 .minor
= SERIAL_APBUART_MINOR
,
550 .cons
= APBUART_CONSOLE
,
554 /* ======================================================================== */
555 /* OF Platform Driver */
556 /* ======================================================================== */
558 static int apbuart_probe(struct platform_device
*op
)
561 struct uart_port
*port
= NULL
;
563 for (i
= 0; i
< grlib_apbuart_port_nr
; i
++) {
564 if (op
->dev
.of_node
== grlib_apbuart_nodes
[i
])
568 port
= &grlib_apbuart_ports
[i
];
569 port
->dev
= &op
->dev
;
570 port
->irq
= op
->archdata
.irqs
[0];
572 uart_add_one_port(&grlib_apbuart_driver
, (struct uart_port
*) port
);
574 apbuart_flush_fifo((struct uart_port
*) port
);
576 printk(KERN_INFO
"grlib-apbuart at 0x%llx, irq %d\n",
577 (unsigned long long) port
->mapbase
, port
->irq
);
581 static struct of_device_id apbuart_match
[] = {
583 .name
= "GAISLER_APBUART",
591 static struct platform_driver grlib_apbuart_of_driver
= {
592 .probe
= apbuart_probe
,
594 .owner
= THIS_MODULE
,
595 .name
= "grlib-apbuart",
596 .of_match_table
= apbuart_match
,
601 static int __init
grlib_apbuart_configure(void)
603 struct device_node
*np
;
606 for_each_matching_node(np
, apbuart_match
) {
609 const struct amba_prom_registers
*regs
;
610 struct uart_port
*port
;
613 ampopts
= of_get_property(np
, "ampopts", NULL
);
614 if (ampopts
&& (*ampopts
== 0))
615 continue; /* Ignore if used by another OS instance */
616 regs
= of_get_property(np
, "reg", NULL
);
617 /* Frequency of APB Bus is frequency of UART */
618 freq_hz
= of_get_property(np
, "freq", NULL
);
620 if (!regs
|| !freq_hz
|| (*freq_hz
== 0))
623 grlib_apbuart_nodes
[line
] = np
;
625 addr
= regs
->phys_addr
;
627 port
= &grlib_apbuart_ports
[line
];
629 port
->mapbase
= addr
;
630 port
->membase
= ioremap(addr
, sizeof(struct grlib_apbuart_regs_map
));
632 port
->iotype
= UPIO_MEM
;
633 port
->ops
= &grlib_apbuart_ops
;
634 port
->flags
= UPF_BOOT_AUTOCONF
;
636 port
->uartclk
= *freq_hz
;
637 port
->fifosize
= apbuart_scan_fifo_size((struct uart_port
*) port
, line
);
640 /* We support maximum UART_NR uarts ... */
645 grlib_apbuart_driver
.nr
= grlib_apbuart_port_nr
= line
;
646 return line
? 0 : -ENODEV
;
649 static int __init
grlib_apbuart_init(void)
653 /* Find all APBUARTS in device the tree and initialize their ports */
654 ret
= grlib_apbuart_configure();
658 printk(KERN_INFO
"Serial: GRLIB APBUART driver\n");
660 ret
= uart_register_driver(&grlib_apbuart_driver
);
663 printk(KERN_ERR
"%s: uart_register_driver failed (%i)\n",
668 ret
= platform_driver_register(&grlib_apbuart_of_driver
);
671 "%s: platform_driver_register failed (%i)\n",
673 uart_unregister_driver(&grlib_apbuart_driver
);
680 static void __exit
grlib_apbuart_exit(void)
684 for (i
= 0; i
< grlib_apbuart_port_nr
; i
++)
685 uart_remove_one_port(&grlib_apbuart_driver
,
686 &grlib_apbuart_ports
[i
]);
688 uart_unregister_driver(&grlib_apbuart_driver
);
689 platform_driver_unregister(&grlib_apbuart_of_driver
);
692 module_init(grlib_apbuart_init
);
693 module_exit(grlib_apbuart_exit
);
695 MODULE_AUTHOR("Aeroflex Gaisler AB");
696 MODULE_DESCRIPTION("GRLIB APBUART serial driver");
697 MODULE_VERSION("2.1");
698 MODULE_LICENSE("GPL");