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/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>
27 #include <linux/of_device.h>
28 #include <linux/of_platform.h>
29 #include <linux/of_irq.h>
30 #include <linux/platform_device.h>
32 #include <linux/serial_core.h>
37 #define SERIAL_APBUART_MAJOR TTY_MAJOR
38 #define SERIAL_APBUART_MINOR 64
39 #define UART_DUMMY_RSR_RX 0x8000 /* for ignore all read */
41 static void apbuart_tx_chars(struct uart_port
*port
);
43 static void apbuart_stop_tx(struct uart_port
*port
)
47 cr
= UART_GET_CTRL(port
);
49 UART_PUT_CTRL(port
, cr
);
52 static void apbuart_start_tx(struct uart_port
*port
)
56 cr
= UART_GET_CTRL(port
);
58 UART_PUT_CTRL(port
, cr
);
60 if (UART_GET_STATUS(port
) & UART_STATUS_THE
)
61 apbuart_tx_chars(port
);
64 static void apbuart_stop_rx(struct uart_port
*port
)
68 cr
= UART_GET_CTRL(port
);
69 cr
&= ~(UART_CTRL_RI
);
70 UART_PUT_CTRL(port
, cr
);
73 static void apbuart_enable_ms(struct uart_port
*port
)
75 /* No modem status change interrupts for APBUART */
78 static void apbuart_rx_chars(struct uart_port
*port
)
80 struct tty_struct
*tty
= port
->state
->port
.tty
;
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 tty_flip_buffer_push(tty
);
131 static void apbuart_tx_chars(struct uart_port
*port
)
133 struct circ_buf
*xmit
= &port
->state
->xmit
;
137 UART_PUT_CHAR(port
, port
->x_char
);
143 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
144 apbuart_stop_tx(port
);
148 /* amba: fill FIFO */
149 count
= port
->fifosize
>> 1;
151 UART_PUT_CHAR(port
, xmit
->buf
[xmit
->tail
]);
152 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
154 if (uart_circ_empty(xmit
))
156 } while (--count
> 0);
158 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
159 uart_write_wakeup(port
);
161 if (uart_circ_empty(xmit
))
162 apbuart_stop_tx(port
);
165 static irqreturn_t
apbuart_int(int irq
, void *dev_id
)
167 struct uart_port
*port
= dev_id
;
170 spin_lock(&port
->lock
);
172 status
= UART_GET_STATUS(port
);
173 if (status
& UART_STATUS_DR
)
174 apbuart_rx_chars(port
);
175 if (status
& UART_STATUS_THE
)
176 apbuart_tx_chars(port
);
178 spin_unlock(&port
->lock
);
183 static unsigned int apbuart_tx_empty(struct uart_port
*port
)
185 unsigned int status
= UART_GET_STATUS(port
);
186 return status
& UART_STATUS_THE
? TIOCSER_TEMT
: 0;
189 static unsigned int apbuart_get_mctrl(struct uart_port
*port
)
191 /* The GRLIB APBUART handles flow control in hardware */
192 return TIOCM_CAR
| TIOCM_DSR
| TIOCM_CTS
;
195 static void apbuart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
197 /* The GRLIB APBUART handles flow control in hardware */
200 static void apbuart_break_ctl(struct uart_port
*port
, int break_state
)
202 /* We don't support sending break */
205 static int apbuart_startup(struct uart_port
*port
)
210 /* Allocate the IRQ */
211 retval
= request_irq(port
->irq
, apbuart_int
, 0, "apbuart", port
);
215 /* Finally, enable interrupts */
216 cr
= UART_GET_CTRL(port
);
218 cr
| UART_CTRL_RE
| UART_CTRL_TE
|
219 UART_CTRL_RI
| UART_CTRL_TI
);
224 static void apbuart_shutdown(struct uart_port
*port
)
228 /* disable all interrupts, disable the port */
229 cr
= UART_GET_CTRL(port
);
231 cr
& ~(UART_CTRL_RE
| UART_CTRL_TE
|
232 UART_CTRL_RI
| UART_CTRL_TI
));
234 /* Free the interrupt */
235 free_irq(port
->irq
, port
);
238 static void apbuart_set_termios(struct uart_port
*port
,
239 struct ktermios
*termios
, struct ktermios
*old
)
243 unsigned int baud
, quot
;
245 /* Ask the core to calculate the divisor for us. */
246 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/ 16);
248 panic("invalid baudrate %i\n", port
->uartclk
/ 16);
250 /* uart_get_divisor calc a *16 uart freq, apbuart is *8 */
251 quot
= (uart_get_divisor(port
, baud
)) * 2;
252 cr
= UART_GET_CTRL(port
);
253 cr
&= ~(UART_CTRL_PE
| UART_CTRL_PS
);
255 if (termios
->c_cflag
& PARENB
) {
257 if ((termios
->c_cflag
& PARODD
))
261 /* Enable flow control. */
262 if (termios
->c_cflag
& CRTSCTS
)
265 spin_lock_irqsave(&port
->lock
, flags
);
267 /* Update the per-port timeout. */
268 uart_update_timeout(port
, termios
->c_cflag
, baud
);
270 port
->read_status_mask
= UART_STATUS_OE
;
271 if (termios
->c_iflag
& INPCK
)
272 port
->read_status_mask
|= UART_STATUS_FE
| UART_STATUS_PE
;
274 /* Characters to ignore */
275 port
->ignore_status_mask
= 0;
276 if (termios
->c_iflag
& IGNPAR
)
277 port
->ignore_status_mask
|= UART_STATUS_FE
| UART_STATUS_PE
;
279 /* Ignore all characters if CREAD is not set. */
280 if ((termios
->c_cflag
& CREAD
) == 0)
281 port
->ignore_status_mask
|= UART_DUMMY_RSR_RX
;
285 UART_PUT_SCAL(port
, quot
);
286 UART_PUT_CTRL(port
, cr
);
288 spin_unlock_irqrestore(&port
->lock
, flags
);
291 static const char *apbuart_type(struct uart_port
*port
)
293 return port
->type
== PORT_APBUART
? "GRLIB/APBUART" : NULL
;
296 static void apbuart_release_port(struct uart_port
*port
)
298 release_mem_region(port
->mapbase
, 0x100);
301 static int apbuart_request_port(struct uart_port
*port
)
303 return request_mem_region(port
->mapbase
, 0x100, "grlib-apbuart")
304 != NULL
? 0 : -EBUSY
;
308 /* Configure/autoconfigure the port */
309 static void apbuart_config_port(struct uart_port
*port
, int flags
)
311 if (flags
& UART_CONFIG_TYPE
) {
312 port
->type
= PORT_APBUART
;
313 apbuart_request_port(port
);
317 /* Verify the new serial_struct (for TIOCSSERIAL) */
318 static int apbuart_verify_port(struct uart_port
*port
,
319 struct serial_struct
*ser
)
322 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_APBUART
)
324 if (ser
->irq
< 0 || ser
->irq
>= NR_IRQS
)
326 if (ser
->baud_base
< 9600)
331 static struct uart_ops grlib_apbuart_ops
= {
332 .tx_empty
= apbuart_tx_empty
,
333 .set_mctrl
= apbuart_set_mctrl
,
334 .get_mctrl
= apbuart_get_mctrl
,
335 .stop_tx
= apbuart_stop_tx
,
336 .start_tx
= apbuart_start_tx
,
337 .stop_rx
= apbuart_stop_rx
,
338 .enable_ms
= apbuart_enable_ms
,
339 .break_ctl
= apbuart_break_ctl
,
340 .startup
= apbuart_startup
,
341 .shutdown
= apbuart_shutdown
,
342 .set_termios
= apbuart_set_termios
,
343 .type
= apbuart_type
,
344 .release_port
= apbuart_release_port
,
345 .request_port
= apbuart_request_port
,
346 .config_port
= apbuart_config_port
,
347 .verify_port
= apbuart_verify_port
,
350 static struct uart_port grlib_apbuart_ports
[UART_NR
];
351 static struct device_node
*grlib_apbuart_nodes
[UART_NR
];
353 static int apbuart_scan_fifo_size(struct uart_port
*port
, int portnumber
)
360 ctrl
= UART_GET_CTRL(port
);
363 * Enable the transceiver and wait for it to be ready to send data.
364 * Clear interrupts so that this process will not be externally
365 * interrupted in the middle (which can cause the transceiver to
366 * drain prematurely).
369 local_irq_save(flags
);
371 UART_PUT_CTRL(port
, ctrl
| UART_CTRL_TE
);
373 while (!UART_TX_READY(UART_GET_STATUS(port
)))
377 * Disable the transceiver so data isn't actually sent during the
381 UART_PUT_CTRL(port
, ctrl
& ~(UART_CTRL_TE
));
384 UART_PUT_CHAR(port
, 0);
387 * So long as transmitting a character increments the tranceivier FIFO
388 * length the FIFO must be at least that big. These bytes will
389 * automatically drain off of the FIFO.
392 status
= UART_GET_STATUS(port
);
393 while (((status
>> 20) & 0x3F) == fifosize
) {
395 UART_PUT_CHAR(port
, 0);
396 status
= UART_GET_STATUS(port
);
401 UART_PUT_CTRL(port
, ctrl
);
402 local_irq_restore(flags
);
410 static void apbuart_flush_fifo(struct uart_port
*port
)
414 for (i
= 0; i
< port
->fifosize
; i
++)
419 /* ======================================================================== */
420 /* Console driver, if enabled */
421 /* ======================================================================== */
423 #ifdef CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
425 static void apbuart_console_putchar(struct uart_port
*port
, int ch
)
429 status
= UART_GET_STATUS(port
);
430 } while (!UART_TX_READY(status
));
431 UART_PUT_CHAR(port
, ch
);
435 apbuart_console_write(struct console
*co
, const char *s
, unsigned int count
)
437 struct uart_port
*port
= &grlib_apbuart_ports
[co
->index
];
438 unsigned int status
, old_cr
, new_cr
;
440 /* First save the CR then disable the interrupts */
441 old_cr
= UART_GET_CTRL(port
);
442 new_cr
= old_cr
& ~(UART_CTRL_RI
| UART_CTRL_TI
);
443 UART_PUT_CTRL(port
, new_cr
);
445 uart_console_write(port
, s
, count
, apbuart_console_putchar
);
448 * Finally, wait for transmitter to become empty
449 * and restore the TCR
452 status
= UART_GET_STATUS(port
);
453 } while (!UART_TX_READY(status
));
454 UART_PUT_CTRL(port
, old_cr
);
458 apbuart_console_get_options(struct uart_port
*port
, int *baud
,
459 int *parity
, int *bits
)
461 if (UART_GET_CTRL(port
) & (UART_CTRL_RE
| UART_CTRL_TE
)) {
463 unsigned int quot
, status
;
464 status
= UART_GET_STATUS(port
);
467 if (status
& UART_CTRL_PE
) {
468 if ((status
& UART_CTRL_PS
) == 0)
475 quot
= UART_GET_SCAL(port
) / 8;
476 *baud
= port
->uartclk
/ (16 * (quot
+ 1));
480 static int __init
apbuart_console_setup(struct console
*co
, char *options
)
482 struct uart_port
*port
;
488 pr_debug("apbuart_console_setup co=%p, co->index=%i, options=%s\n",
489 co
, co
->index
, options
);
492 * Check whether an invalid uart number has been specified, and
493 * if so, search for the first available port that does have
496 if (co
->index
>= grlib_apbuart_port_nr
)
499 port
= &grlib_apbuart_ports
[co
->index
];
501 spin_lock_init(&port
->lock
);
504 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
506 apbuart_console_get_options(port
, &baud
, &parity
, &bits
);
508 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
511 static struct uart_driver grlib_apbuart_driver
;
513 static struct console grlib_apbuart_console
= {
515 .write
= apbuart_console_write
,
516 .device
= uart_console_device
,
517 .setup
= apbuart_console_setup
,
518 .flags
= CON_PRINTBUFFER
,
520 .data
= &grlib_apbuart_driver
,
524 static int grlib_apbuart_configure(void);
526 static int __init
apbuart_console_init(void)
528 if (grlib_apbuart_configure())
530 register_console(&grlib_apbuart_console
);
534 console_initcall(apbuart_console_init
);
536 #define APBUART_CONSOLE (&grlib_apbuart_console)
538 #define APBUART_CONSOLE NULL
541 static struct uart_driver grlib_apbuart_driver
= {
542 .owner
= THIS_MODULE
,
543 .driver_name
= "serial",
545 .major
= SERIAL_APBUART_MAJOR
,
546 .minor
= SERIAL_APBUART_MINOR
,
548 .cons
= APBUART_CONSOLE
,
552 /* ======================================================================== */
553 /* OF Platform Driver */
554 /* ======================================================================== */
556 static int __devinit
apbuart_probe(struct platform_device
*op
)
559 struct uart_port
*port
= NULL
;
561 for (i
= 0; i
< grlib_apbuart_port_nr
; i
++) {
562 if (op
->dev
.of_node
== grlib_apbuart_nodes
[i
])
566 port
= &grlib_apbuart_ports
[i
];
567 port
->dev
= &op
->dev
;
568 port
->irq
= op
->archdata
.irqs
[0];
570 uart_add_one_port(&grlib_apbuart_driver
, (struct uart_port
*) port
);
572 apbuart_flush_fifo((struct uart_port
*) port
);
574 printk(KERN_INFO
"grlib-apbuart at 0x%llx, irq %d\n",
575 (unsigned long long) port
->mapbase
, port
->irq
);
579 static struct of_device_id __initdata apbuart_match
[] = {
581 .name
= "GAISLER_APBUART",
589 static struct platform_driver grlib_apbuart_of_driver
= {
590 .probe
= apbuart_probe
,
592 .owner
= THIS_MODULE
,
593 .name
= "grlib-apbuart",
594 .of_match_table
= apbuart_match
,
599 static int grlib_apbuart_configure(void)
601 struct device_node
*np
;
604 for_each_matching_node(np
, apbuart_match
) {
607 const struct amba_prom_registers
*regs
;
608 struct uart_port
*port
;
611 ampopts
= of_get_property(np
, "ampopts", NULL
);
612 if (ampopts
&& (*ampopts
== 0))
613 continue; /* Ignore if used by another OS instance */
614 regs
= of_get_property(np
, "reg", NULL
);
615 /* Frequency of APB Bus is frequency of UART */
616 freq_hz
= of_get_property(np
, "freq", NULL
);
618 if (!regs
|| !freq_hz
|| (*freq_hz
== 0))
621 grlib_apbuart_nodes
[line
] = np
;
623 addr
= regs
->phys_addr
;
625 port
= &grlib_apbuart_ports
[line
];
627 port
->mapbase
= addr
;
628 port
->membase
= ioremap(addr
, sizeof(struct grlib_apbuart_regs_map
));
630 port
->iotype
= UPIO_MEM
;
631 port
->ops
= &grlib_apbuart_ops
;
632 port
->flags
= UPF_BOOT_AUTOCONF
;
634 port
->uartclk
= *freq_hz
;
635 port
->fifosize
= apbuart_scan_fifo_size((struct uart_port
*) port
, line
);
638 /* We support maximum UART_NR uarts ... */
643 grlib_apbuart_driver
.nr
= grlib_apbuart_port_nr
= line
;
644 return line
? 0 : -ENODEV
;
647 static int __init
grlib_apbuart_init(void)
651 /* Find all APBUARTS in device the tree and initialize their ports */
652 ret
= grlib_apbuart_configure();
656 printk(KERN_INFO
"Serial: GRLIB APBUART driver\n");
658 ret
= uart_register_driver(&grlib_apbuart_driver
);
661 printk(KERN_ERR
"%s: uart_register_driver failed (%i)\n",
666 ret
= platform_driver_register(&grlib_apbuart_of_driver
);
669 "%s: platform_driver_register failed (%i)\n",
671 uart_unregister_driver(&grlib_apbuart_driver
);
678 static void __exit
grlib_apbuart_exit(void)
682 for (i
= 0; i
< grlib_apbuart_port_nr
; i
++)
683 uart_remove_one_port(&grlib_apbuart_driver
,
684 &grlib_apbuart_ports
[i
]);
686 uart_unregister_driver(&grlib_apbuart_driver
);
687 platform_driver_unregister(&grlib_apbuart_of_driver
);
690 module_init(grlib_apbuart_init
);
691 module_exit(grlib_apbuart_exit
);
693 MODULE_AUTHOR("Aeroflex Gaisler AB");
694 MODULE_DESCRIPTION("GRLIB APBUART serial driver");
695 MODULE_VERSION("2.1");
696 MODULE_LICENSE("GPL");