1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for GRLIB serial ports (APBUART)
5 * Based on linux/drivers/serial/amba.c
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
14 #include <linux/module.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/ioport.h>
18 #include <linux/init.h>
19 #include <linux/serial.h>
20 #include <linux/console.h>
21 #include <linux/sysrq.h>
22 #include <linux/kthread.h>
23 #include <linux/device.h>
25 #include <linux/of_device.h>
26 #include <linux/of_platform.h>
27 #include <linux/of_irq.h>
28 #include <linux/platform_device.h>
30 #include <linux/serial_core.h>
35 #define SERIAL_APBUART_MAJOR TTY_MAJOR
36 #define SERIAL_APBUART_MINOR 64
37 #define UART_DUMMY_RSR_RX 0x8000 /* for ignore all read */
39 static void apbuart_tx_chars(struct uart_port *port);
41 static void apbuart_stop_tx(struct uart_port *port)
45 cr = UART_GET_CTRL(port);
47 UART_PUT_CTRL(port, cr);
50 static void apbuart_start_tx(struct uart_port *port)
54 cr = UART_GET_CTRL(port);
56 UART_PUT_CTRL(port, cr);
58 if (UART_GET_STATUS(port) & UART_STATUS_THE)
59 apbuart_tx_chars(port);
62 static void apbuart_stop_rx(struct uart_port *port)
66 cr = UART_GET_CTRL(port);
67 cr &= ~(UART_CTRL_RI);
68 UART_PUT_CTRL(port, cr);
71 static void apbuart_rx_chars(struct uart_port *port)
73 unsigned int status, ch, rsr, flag;
74 unsigned int max_chars = port->fifosize;
76 status = UART_GET_STATUS(port);
78 while (UART_RX_DATA(status) && (max_chars--)) {
80 ch = UART_GET_CHAR(port);
85 rsr = UART_GET_STATUS(port) | UART_DUMMY_RSR_RX;
86 UART_PUT_STATUS(port, 0);
87 if (rsr & UART_STATUS_ERR) {
89 if (rsr & UART_STATUS_BR) {
90 rsr &= ~(UART_STATUS_FE | UART_STATUS_PE);
92 if (uart_handle_break(port))
94 } else if (rsr & UART_STATUS_PE) {
95 port->icount.parity++;
96 } else if (rsr & UART_STATUS_FE) {
99 if (rsr & UART_STATUS_OE)
100 port->icount.overrun++;
102 rsr &= port->read_status_mask;
104 if (rsr & UART_STATUS_PE)
106 else if (rsr & UART_STATUS_FE)
110 if (uart_handle_sysrq_char(port, ch))
113 uart_insert_char(port, rsr, UART_STATUS_OE, ch, flag);
117 status = UART_GET_STATUS(port);
120 tty_flip_buffer_push(&port->state->port);
123 static void apbuart_tx_chars(struct uart_port *port)
125 struct circ_buf *xmit = &port->state->xmit;
129 UART_PUT_CHAR(port, port->x_char);
135 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
136 apbuart_stop_tx(port);
140 /* amba: fill FIFO */
141 count = port->fifosize >> 1;
143 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
144 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
146 if (uart_circ_empty(xmit))
148 } while (--count > 0);
150 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
151 uart_write_wakeup(port);
153 if (uart_circ_empty(xmit))
154 apbuart_stop_tx(port);
157 static irqreturn_t apbuart_int(int irq, void *dev_id)
159 struct uart_port *port = dev_id;
162 spin_lock(&port->lock);
164 status = UART_GET_STATUS(port);
165 if (status & UART_STATUS_DR)
166 apbuart_rx_chars(port);
167 if (status & UART_STATUS_THE)
168 apbuart_tx_chars(port);
170 spin_unlock(&port->lock);
175 static unsigned int apbuart_tx_empty(struct uart_port *port)
177 unsigned int status = UART_GET_STATUS(port);
178 return status & UART_STATUS_THE ? TIOCSER_TEMT : 0;
181 static unsigned int apbuart_get_mctrl(struct uart_port *port)
183 /* The GRLIB APBUART handles flow control in hardware */
184 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
187 static void apbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
189 /* The GRLIB APBUART handles flow control in hardware */
192 static void apbuart_break_ctl(struct uart_port *port, int break_state)
194 /* We don't support sending break */
197 static int apbuart_startup(struct uart_port *port)
202 /* Allocate the IRQ */
203 retval = request_irq(port->irq, apbuart_int, 0, "apbuart", port);
207 /* Finally, enable interrupts */
208 cr = UART_GET_CTRL(port);
210 cr | UART_CTRL_RE | UART_CTRL_TE |
211 UART_CTRL_RI | UART_CTRL_TI);
216 static void apbuart_shutdown(struct uart_port *port)
220 /* disable all interrupts, disable the port */
221 cr = UART_GET_CTRL(port);
223 cr & ~(UART_CTRL_RE | UART_CTRL_TE |
224 UART_CTRL_RI | UART_CTRL_TI));
226 /* Free the interrupt */
227 free_irq(port->irq, port);
230 static void apbuart_set_termios(struct uart_port *port,
231 struct ktermios *termios, struct ktermios *old)
235 unsigned int baud, quot;
237 /* Ask the core to calculate the divisor for us. */
238 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
240 panic("invalid baudrate %i\n", port->uartclk / 16);
242 /* uart_get_divisor calc a *16 uart freq, apbuart is *8 */
243 quot = (uart_get_divisor(port, baud)) * 2;
244 cr = UART_GET_CTRL(port);
245 cr &= ~(UART_CTRL_PE | UART_CTRL_PS);
247 if (termios->c_cflag & PARENB) {
249 if ((termios->c_cflag & PARODD))
253 /* Enable flow control. */
254 if (termios->c_cflag & CRTSCTS)
257 spin_lock_irqsave(&port->lock, flags);
259 /* Update the per-port timeout. */
260 uart_update_timeout(port, termios->c_cflag, baud);
262 port->read_status_mask = UART_STATUS_OE;
263 if (termios->c_iflag & INPCK)
264 port->read_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
266 /* Characters to ignore */
267 port->ignore_status_mask = 0;
268 if (termios->c_iflag & IGNPAR)
269 port->ignore_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
271 /* Ignore all characters if CREAD is not set. */
272 if ((termios->c_cflag & CREAD) == 0)
273 port->ignore_status_mask |= UART_DUMMY_RSR_RX;
277 UART_PUT_SCAL(port, quot);
278 UART_PUT_CTRL(port, cr);
280 spin_unlock_irqrestore(&port->lock, flags);
283 static const char *apbuart_type(struct uart_port *port)
285 return port->type == PORT_APBUART ? "GRLIB/APBUART" : NULL;
288 static void apbuart_release_port(struct uart_port *port)
290 release_mem_region(port->mapbase, 0x100);
293 static int apbuart_request_port(struct uart_port *port)
295 return request_mem_region(port->mapbase, 0x100, "grlib-apbuart")
296 != NULL ? 0 : -EBUSY;
300 /* Configure/autoconfigure the port */
301 static void apbuart_config_port(struct uart_port *port, int flags)
303 if (flags & UART_CONFIG_TYPE) {
304 port->type = PORT_APBUART;
305 apbuart_request_port(port);
309 /* Verify the new serial_struct (for TIOCSSERIAL) */
310 static int apbuart_verify_port(struct uart_port *port,
311 struct serial_struct *ser)
314 if (ser->type != PORT_UNKNOWN && ser->type != PORT_APBUART)
316 if (ser->irq < 0 || ser->irq >= NR_IRQS)
318 if (ser->baud_base < 9600)
323 static const struct uart_ops grlib_apbuart_ops = {
324 .tx_empty = apbuart_tx_empty,
325 .set_mctrl = apbuart_set_mctrl,
326 .get_mctrl = apbuart_get_mctrl,
327 .stop_tx = apbuart_stop_tx,
328 .start_tx = apbuart_start_tx,
329 .stop_rx = apbuart_stop_rx,
330 .break_ctl = apbuart_break_ctl,
331 .startup = apbuart_startup,
332 .shutdown = apbuart_shutdown,
333 .set_termios = apbuart_set_termios,
334 .type = apbuart_type,
335 .release_port = apbuart_release_port,
336 .request_port = apbuart_request_port,
337 .config_port = apbuart_config_port,
338 .verify_port = apbuart_verify_port,
341 static struct uart_port grlib_apbuart_ports[UART_NR];
342 static struct device_node *grlib_apbuart_nodes[UART_NR];
344 static int apbuart_scan_fifo_size(struct uart_port *port, int portnumber)
351 ctrl = UART_GET_CTRL(port);
354 * Enable the transceiver and wait for it to be ready to send data.
355 * Clear interrupts so that this process will not be externally
356 * interrupted in the middle (which can cause the transceiver to
357 * drain prematurely).
360 local_irq_save(flags);
362 UART_PUT_CTRL(port, ctrl | UART_CTRL_TE);
364 while (!UART_TX_READY(UART_GET_STATUS(port)))
368 * Disable the transceiver so data isn't actually sent during the
372 UART_PUT_CTRL(port, ctrl & ~(UART_CTRL_TE));
375 UART_PUT_CHAR(port, 0);
378 * So long as transmitting a character increments the tranceivier FIFO
379 * length the FIFO must be at least that big. These bytes will
380 * automatically drain off of the FIFO.
383 status = UART_GET_STATUS(port);
384 while (((status >> 20) & 0x3F) == fifosize) {
386 UART_PUT_CHAR(port, 0);
387 status = UART_GET_STATUS(port);
392 UART_PUT_CTRL(port, ctrl);
393 local_irq_restore(flags);
401 static void apbuart_flush_fifo(struct uart_port *port)
405 for (i = 0; i < port->fifosize; i++)
410 /* ======================================================================== */
411 /* Console driver, if enabled */
412 /* ======================================================================== */
414 #ifdef CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
416 static void apbuart_console_putchar(struct uart_port *port, int ch)
420 status = UART_GET_STATUS(port);
421 } while (!UART_TX_READY(status));
422 UART_PUT_CHAR(port, ch);
426 apbuart_console_write(struct console *co, const char *s, unsigned int count)
428 struct uart_port *port = &grlib_apbuart_ports[co->index];
429 unsigned int status, old_cr, new_cr;
431 /* First save the CR then disable the interrupts */
432 old_cr = UART_GET_CTRL(port);
433 new_cr = old_cr & ~(UART_CTRL_RI | UART_CTRL_TI);
434 UART_PUT_CTRL(port, new_cr);
436 uart_console_write(port, s, count, apbuart_console_putchar);
439 * Finally, wait for transmitter to become empty
440 * and restore the TCR
443 status = UART_GET_STATUS(port);
444 } while (!UART_TX_READY(status));
445 UART_PUT_CTRL(port, old_cr);
449 apbuart_console_get_options(struct uart_port *port, int *baud,
450 int *parity, int *bits)
452 if (UART_GET_CTRL(port) & (UART_CTRL_RE | UART_CTRL_TE)) {
454 unsigned int quot, status;
455 status = UART_GET_STATUS(port);
458 if (status & UART_CTRL_PE) {
459 if ((status & UART_CTRL_PS) == 0)
466 quot = UART_GET_SCAL(port) / 8;
467 *baud = port->uartclk / (16 * (quot + 1));
471 static int __init apbuart_console_setup(struct console *co, char *options)
473 struct uart_port *port;
479 pr_debug("apbuart_console_setup co=%p, co->index=%i, options=%s\n",
480 co, co->index, options);
483 * Check whether an invalid uart number has been specified, and
484 * if so, search for the first available port that does have
487 if (co->index >= grlib_apbuart_port_nr)
490 port = &grlib_apbuart_ports[co->index];
492 spin_lock_init(&port->lock);
495 uart_parse_options(options, &baud, &parity, &bits, &flow);
497 apbuart_console_get_options(port, &baud, &parity, &bits);
499 return uart_set_options(port, co, baud, parity, bits, flow);
502 static struct uart_driver grlib_apbuart_driver;
504 static struct console grlib_apbuart_console = {
506 .write = apbuart_console_write,
507 .device = uart_console_device,
508 .setup = apbuart_console_setup,
509 .flags = CON_PRINTBUFFER,
511 .data = &grlib_apbuart_driver,
515 static int grlib_apbuart_configure(void);
517 static int __init apbuart_console_init(void)
519 if (grlib_apbuart_configure())
521 register_console(&grlib_apbuart_console);
525 console_initcall(apbuart_console_init);
527 #define APBUART_CONSOLE (&grlib_apbuart_console)
529 #define APBUART_CONSOLE NULL
532 static struct uart_driver grlib_apbuart_driver = {
533 .owner = THIS_MODULE,
534 .driver_name = "serial",
536 .major = SERIAL_APBUART_MAJOR,
537 .minor = SERIAL_APBUART_MINOR,
539 .cons = APBUART_CONSOLE,
543 /* ======================================================================== */
544 /* OF Platform Driver */
545 /* ======================================================================== */
547 static int apbuart_probe(struct platform_device *op)
550 struct uart_port *port = NULL;
552 for (i = 0; i < grlib_apbuart_port_nr; i++) {
553 if (op->dev.of_node == grlib_apbuart_nodes[i])
557 port = &grlib_apbuart_ports[i];
558 port->dev = &op->dev;
559 port->irq = op->archdata.irqs[0];
561 uart_add_one_port(&grlib_apbuart_driver, (struct uart_port *) port);
563 apbuart_flush_fifo((struct uart_port *) port);
565 printk(KERN_INFO "grlib-apbuart at 0x%llx, irq %d\n",
566 (unsigned long long) port->mapbase, port->irq);
570 static const struct of_device_id apbuart_match[] = {
572 .name = "GAISLER_APBUART",
579 MODULE_DEVICE_TABLE(of, apbuart_match);
581 static struct platform_driver grlib_apbuart_of_driver = {
582 .probe = apbuart_probe,
584 .name = "grlib-apbuart",
585 .of_match_table = apbuart_match,
590 static int __init grlib_apbuart_configure(void)
592 struct device_node *np;
595 for_each_matching_node(np, apbuart_match) {
598 const struct amba_prom_registers *regs;
599 struct uart_port *port;
602 ampopts = of_get_property(np, "ampopts", NULL);
603 if (ampopts && (*ampopts == 0))
604 continue; /* Ignore if used by another OS instance */
605 regs = of_get_property(np, "reg", NULL);
606 /* Frequency of APB Bus is frequency of UART */
607 freq_hz = of_get_property(np, "freq", NULL);
609 if (!regs || !freq_hz || (*freq_hz == 0))
612 grlib_apbuart_nodes[line] = np;
614 addr = regs->phys_addr;
616 port = &grlib_apbuart_ports[line];
618 port->mapbase = addr;
619 port->membase = ioremap(addr, sizeof(struct grlib_apbuart_regs_map));
621 port->iotype = UPIO_MEM;
622 port->ops = &grlib_apbuart_ops;
623 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE);
624 port->flags = UPF_BOOT_AUTOCONF;
626 port->uartclk = *freq_hz;
627 port->fifosize = apbuart_scan_fifo_size((struct uart_port *) port, line);
630 /* We support maximum UART_NR uarts ... */
635 grlib_apbuart_driver.nr = grlib_apbuart_port_nr = line;
636 return line ? 0 : -ENODEV;
639 static int __init grlib_apbuart_init(void)
643 /* Find all APBUARTS in device the tree and initialize their ports */
644 ret = grlib_apbuart_configure();
648 printk(KERN_INFO "Serial: GRLIB APBUART driver\n");
650 ret = uart_register_driver(&grlib_apbuart_driver);
653 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
658 ret = platform_driver_register(&grlib_apbuart_of_driver);
661 "%s: platform_driver_register failed (%i)\n",
663 uart_unregister_driver(&grlib_apbuart_driver);
670 static void __exit grlib_apbuart_exit(void)
674 for (i = 0; i < grlib_apbuart_port_nr; i++)
675 uart_remove_one_port(&grlib_apbuart_driver,
676 &grlib_apbuart_ports[i]);
678 uart_unregister_driver(&grlib_apbuart_driver);
679 platform_driver_unregister(&grlib_apbuart_of_driver);
682 module_init(grlib_apbuart_init);
683 module_exit(grlib_apbuart_exit);
685 MODULE_AUTHOR("Aeroflex Gaisler AB");
686 MODULE_DESCRIPTION("GRLIB APBUART serial driver");
687 MODULE_VERSION("2.1");
688 MODULE_LICENSE("GPL");