1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for AMBA serial ports
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Copyright 1999 ARM Limited
8 * Copyright (C) 2000 Deep Blue Solutions Ltd.
10 * This is a generic driver for ARM AMBA-type serial ports. They
11 * have a lot of 16550-like features, but are not register compatible.
12 * Note that although they do have CTS, DCD and DSR inputs, they do
13 * not have an RI input, nor do they have DTR or RTS outputs. If
14 * required, these have to be supplied via some other means (eg, GPIO)
15 * and hooked into this driver.
18 #include <linux/module.h>
19 #include <linux/ioport.h>
20 #include <linux/init.h>
21 #include <linux/console.h>
22 #include <linux/sysrq.h>
23 #include <linux/device.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/serial_core.h>
27 #include <linux/serial.h>
28 #include <linux/amba/bus.h>
29 #include <linux/amba/serial.h>
30 #include <linux/clk.h>
31 #include <linux/slab.h>
36 #define SERIAL_AMBA_MAJOR 204
37 #define SERIAL_AMBA_MINOR 16
38 #define SERIAL_AMBA_NR UART_NR
40 #define AMBA_ISR_PASS_LIMIT 256
42 #define UART_RX_DATA(s) (((s) & UART01x_FR_RXFE) == 0)
43 #define UART_TX_READY(s) (((s) & UART01x_FR_TXFF) == 0)
45 #define UART_DUMMY_RSR_RX 256
46 #define UART_PORT_SIZE 64
49 * We wrap our port structure around the generic uart_port.
51 struct uart_amba_port {
52 struct uart_port port;
54 struct amba_device *dev;
55 struct amba_pl010_data *data;
56 unsigned int old_status;
59 static void pl010_stop_tx(struct uart_port *port)
61 struct uart_amba_port *uap =
62 container_of(port, struct uart_amba_port, port);
65 cr = readb(uap->port.membase + UART010_CR);
66 cr &= ~UART010_CR_TIE;
67 writel(cr, uap->port.membase + UART010_CR);
70 static void pl010_start_tx(struct uart_port *port)
72 struct uart_amba_port *uap =
73 container_of(port, struct uart_amba_port, port);
76 cr = readb(uap->port.membase + UART010_CR);
78 writel(cr, uap->port.membase + UART010_CR);
81 static void pl010_stop_rx(struct uart_port *port)
83 struct uart_amba_port *uap =
84 container_of(port, struct uart_amba_port, port);
87 cr = readb(uap->port.membase + UART010_CR);
88 cr &= ~(UART010_CR_RIE | UART010_CR_RTIE);
89 writel(cr, uap->port.membase + UART010_CR);
92 static void pl010_disable_ms(struct uart_port *port)
94 struct uart_amba_port *uap = (struct uart_amba_port *)port;
97 cr = readb(uap->port.membase + UART010_CR);
98 cr &= ~UART010_CR_MSIE;
99 writel(cr, uap->port.membase + UART010_CR);
102 static void pl010_enable_ms(struct uart_port *port)
104 struct uart_amba_port *uap =
105 container_of(port, struct uart_amba_port, port);
108 cr = readb(uap->port.membase + UART010_CR);
109 cr |= UART010_CR_MSIE;
110 writel(cr, uap->port.membase + UART010_CR);
113 static void pl010_rx_chars(struct uart_port *port)
115 unsigned int status, ch, flag, rsr, max_count = 256;
117 status = readb(port->membase + UART01x_FR);
118 while (UART_RX_DATA(status) && max_count--) {
119 ch = readb(port->membase + UART01x_DR);
125 * Note that the error handling code is
126 * out of the main execution path
128 rsr = readb(port->membase + UART01x_RSR) | UART_DUMMY_RSR_RX;
129 if (unlikely(rsr & UART01x_RSR_ANY)) {
130 writel(0, port->membase + UART01x_ECR);
132 if (rsr & UART01x_RSR_BE) {
133 rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE);
135 if (uart_handle_break(port))
137 } else if (rsr & UART01x_RSR_PE)
138 port->icount.parity++;
139 else if (rsr & UART01x_RSR_FE)
140 port->icount.frame++;
141 if (rsr & UART01x_RSR_OE)
142 port->icount.overrun++;
144 rsr &= port->read_status_mask;
146 if (rsr & UART01x_RSR_BE)
148 else if (rsr & UART01x_RSR_PE)
150 else if (rsr & UART01x_RSR_FE)
154 if (uart_handle_sysrq_char(port, ch))
157 uart_insert_char(port, rsr, UART01x_RSR_OE, ch, flag);
160 status = readb(port->membase + UART01x_FR);
162 tty_flip_buffer_push(&port->state->port);
165 static void pl010_tx_chars(struct uart_port *port)
169 uart_port_tx_limited(port, ch, port->fifosize >> 1,
171 writel(ch, port->membase + UART01x_DR),
175 static void pl010_modem_status(struct uart_amba_port *uap)
177 struct uart_port *port = &uap->port;
178 unsigned int status, delta;
180 writel(0, port->membase + UART010_ICR);
182 status = readb(port->membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
184 delta = status ^ uap->old_status;
185 uap->old_status = status;
190 if (delta & UART01x_FR_DCD)
191 uart_handle_dcd_change(port, status & UART01x_FR_DCD);
193 if (delta & UART01x_FR_DSR)
196 if (delta & UART01x_FR_CTS)
197 uart_handle_cts_change(port, status & UART01x_FR_CTS);
199 wake_up_interruptible(&port->state->port.delta_msr_wait);
202 static irqreturn_t pl010_int(int irq, void *dev_id)
204 struct uart_amba_port *uap = dev_id;
205 struct uart_port *port = &uap->port;
206 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
209 spin_lock(&port->lock);
211 status = readb(port->membase + UART010_IIR);
214 if (status & (UART010_IIR_RTIS | UART010_IIR_RIS))
215 pl010_rx_chars(port);
216 if (status & UART010_IIR_MIS)
217 pl010_modem_status(uap);
218 if (status & UART010_IIR_TIS)
219 pl010_tx_chars(port);
221 if (pass_counter-- == 0)
224 status = readb(port->membase + UART010_IIR);
225 } while (status & (UART010_IIR_RTIS | UART010_IIR_RIS |
230 spin_unlock(&port->lock);
232 return IRQ_RETVAL(handled);
235 static unsigned int pl010_tx_empty(struct uart_port *port)
237 unsigned int status = readb(port->membase + UART01x_FR);
239 return status & UART01x_FR_BUSY ? 0 : TIOCSER_TEMT;
242 static unsigned int pl010_get_mctrl(struct uart_port *port)
244 unsigned int result = 0;
247 status = readb(port->membase + UART01x_FR);
248 if (status & UART01x_FR_DCD)
250 if (status & UART01x_FR_DSR)
252 if (status & UART01x_FR_CTS)
258 static void pl010_set_mctrl(struct uart_port *port, unsigned int mctrl)
260 struct uart_amba_port *uap =
261 container_of(port, struct uart_amba_port, port);
264 uap->data->set_mctrl(uap->dev, port->membase, mctrl);
267 static void pl010_break_ctl(struct uart_port *port, int break_state)
272 spin_lock_irqsave(&port->lock, flags);
273 lcr_h = readb(port->membase + UART010_LCRH);
274 if (break_state == -1)
275 lcr_h |= UART01x_LCRH_BRK;
277 lcr_h &= ~UART01x_LCRH_BRK;
278 writel(lcr_h, port->membase + UART010_LCRH);
279 spin_unlock_irqrestore(&port->lock, flags);
282 static int pl010_startup(struct uart_port *port)
284 struct uart_amba_port *uap =
285 container_of(port, struct uart_amba_port, port);
289 * Try to enable the clock producer.
291 retval = clk_prepare_enable(uap->clk);
295 port->uartclk = clk_get_rate(uap->clk);
300 retval = request_irq(port->irq, pl010_int, 0, "uart-pl010", uap);
305 * initialise the old status of the modem signals
307 uap->old_status = readb(port->membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
310 * Finally, enable interrupts
312 writel(UART01x_CR_UARTEN | UART010_CR_RIE | UART010_CR_RTIE,
313 port->membase + UART010_CR);
318 clk_disable_unprepare(uap->clk);
323 static void pl010_shutdown(struct uart_port *port)
325 struct uart_amba_port *uap =
326 container_of(port, struct uart_amba_port, port);
331 free_irq(port->irq, uap);
334 * disable all interrupts, disable the port
336 writel(0, port->membase + UART010_CR);
338 /* disable break condition and fifos */
339 writel(readb(port->membase + UART010_LCRH) &
340 ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN),
341 port->membase + UART010_LCRH);
344 * Shut down the clock producer
346 clk_disable_unprepare(uap->clk);
350 pl010_set_termios(struct uart_port *port, struct ktermios *termios,
351 const struct ktermios *old)
353 unsigned int lcr_h, old_cr;
355 unsigned int baud, quot;
358 * Ask the core to calculate the divisor for us.
360 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
361 quot = uart_get_divisor(port, baud);
363 switch (termios->c_cflag & CSIZE) {
365 lcr_h = UART01x_LCRH_WLEN_5;
368 lcr_h = UART01x_LCRH_WLEN_6;
371 lcr_h = UART01x_LCRH_WLEN_7;
374 lcr_h = UART01x_LCRH_WLEN_8;
377 if (termios->c_cflag & CSTOPB)
378 lcr_h |= UART01x_LCRH_STP2;
379 if (termios->c_cflag & PARENB) {
380 lcr_h |= UART01x_LCRH_PEN;
381 if (!(termios->c_cflag & PARODD))
382 lcr_h |= UART01x_LCRH_EPS;
384 if (port->fifosize > 1)
385 lcr_h |= UART01x_LCRH_FEN;
387 spin_lock_irqsave(&port->lock, flags);
390 * Update the per-port timeout.
392 uart_update_timeout(port, termios->c_cflag, baud);
394 port->read_status_mask = UART01x_RSR_OE;
395 if (termios->c_iflag & INPCK)
396 port->read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
397 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
398 port->read_status_mask |= UART01x_RSR_BE;
401 * Characters to ignore
403 port->ignore_status_mask = 0;
404 if (termios->c_iflag & IGNPAR)
405 port->ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
406 if (termios->c_iflag & IGNBRK) {
407 port->ignore_status_mask |= UART01x_RSR_BE;
409 * If we're ignoring parity and break indicators,
410 * ignore overruns too (for real raw support).
412 if (termios->c_iflag & IGNPAR)
413 port->ignore_status_mask |= UART01x_RSR_OE;
417 * Ignore all characters if CREAD is not set.
419 if ((termios->c_cflag & CREAD) == 0)
420 port->ignore_status_mask |= UART_DUMMY_RSR_RX;
422 old_cr = readb(port->membase + UART010_CR) & ~UART010_CR_MSIE;
424 if (UART_ENABLE_MS(port, termios->c_cflag))
425 old_cr |= UART010_CR_MSIE;
429 writel((quot & 0xf00) >> 8, port->membase + UART010_LCRM);
430 writel(quot & 0xff, port->membase + UART010_LCRL);
433 * ----------v----------v----------v----------v-----
434 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
435 * ----------^----------^----------^----------^-----
437 writel(lcr_h, port->membase + UART010_LCRH);
438 writel(old_cr, port->membase + UART010_CR);
440 spin_unlock_irqrestore(&port->lock, flags);
443 static void pl010_set_ldisc(struct uart_port *port, struct ktermios *termios)
445 if (termios->c_line == N_PPS) {
446 port->flags |= UPF_HARDPPS_CD;
447 spin_lock_irq(&port->lock);
448 pl010_enable_ms(port);
449 spin_unlock_irq(&port->lock);
451 port->flags &= ~UPF_HARDPPS_CD;
452 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
453 spin_lock_irq(&port->lock);
454 pl010_disable_ms(port);
455 spin_unlock_irq(&port->lock);
460 static const char *pl010_type(struct uart_port *port)
462 return port->type == PORT_AMBA ? "AMBA" : NULL;
466 * Release the memory region(s) being used by 'port'
468 static void pl010_release_port(struct uart_port *port)
470 release_mem_region(port->mapbase, UART_PORT_SIZE);
474 * Request the memory region(s) being used by 'port'
476 static int pl010_request_port(struct uart_port *port)
478 return request_mem_region(port->mapbase, UART_PORT_SIZE, "uart-pl010")
479 != NULL ? 0 : -EBUSY;
483 * Configure/autoconfigure the port.
485 static void pl010_config_port(struct uart_port *port, int flags)
487 if (flags & UART_CONFIG_TYPE) {
488 port->type = PORT_AMBA;
489 pl010_request_port(port);
494 * verify the new serial_struct (for TIOCSSERIAL).
496 static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
499 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
501 if (ser->irq < 0 || ser->irq >= nr_irqs)
503 if (ser->baud_base < 9600)
508 static const struct uart_ops amba_pl010_pops = {
509 .tx_empty = pl010_tx_empty,
510 .set_mctrl = pl010_set_mctrl,
511 .get_mctrl = pl010_get_mctrl,
512 .stop_tx = pl010_stop_tx,
513 .start_tx = pl010_start_tx,
514 .stop_rx = pl010_stop_rx,
515 .enable_ms = pl010_enable_ms,
516 .break_ctl = pl010_break_ctl,
517 .startup = pl010_startup,
518 .shutdown = pl010_shutdown,
519 .set_termios = pl010_set_termios,
520 .set_ldisc = pl010_set_ldisc,
522 .release_port = pl010_release_port,
523 .request_port = pl010_request_port,
524 .config_port = pl010_config_port,
525 .verify_port = pl010_verify_port,
528 static struct uart_amba_port *amba_ports[UART_NR];
530 #ifdef CONFIG_SERIAL_AMBA_PL010_CONSOLE
532 static void pl010_console_putchar(struct uart_port *port, unsigned char ch)
537 status = readb(port->membase + UART01x_FR);
539 } while (!UART_TX_READY(status));
540 writel(ch, port->membase + UART01x_DR);
544 pl010_console_write(struct console *co, const char *s, unsigned int count)
546 struct uart_amba_port *uap = amba_ports[co->index];
547 struct uart_port *port = &uap->port;
548 unsigned int status, old_cr;
550 clk_enable(uap->clk);
553 * First save the CR then disable the interrupts
555 old_cr = readb(port->membase + UART010_CR);
556 writel(UART01x_CR_UARTEN, port->membase + UART010_CR);
558 uart_console_write(port, s, count, pl010_console_putchar);
561 * Finally, wait for transmitter to become empty
562 * and restore the TCR
565 status = readb(port->membase + UART01x_FR);
567 } while (status & UART01x_FR_BUSY);
568 writel(old_cr, port->membase + UART010_CR);
570 clk_disable(uap->clk);
574 pl010_console_get_options(struct uart_amba_port *uap, int *baud,
575 int *parity, int *bits)
577 if (readb(uap->port.membase + UART010_CR) & UART01x_CR_UARTEN) {
578 unsigned int lcr_h, quot;
579 lcr_h = readb(uap->port.membase + UART010_LCRH);
582 if (lcr_h & UART01x_LCRH_PEN) {
583 if (lcr_h & UART01x_LCRH_EPS)
589 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
594 quot = readb(uap->port.membase + UART010_LCRL) |
595 readb(uap->port.membase + UART010_LCRM) << 8;
596 *baud = uap->port.uartclk / (16 * (quot + 1));
600 static int __init pl010_console_setup(struct console *co, char *options)
602 struct uart_amba_port *uap;
610 * Check whether an invalid uart number has been specified, and
611 * if so, search for the first available port that does have
614 if (co->index >= UART_NR)
616 uap = amba_ports[co->index];
620 ret = clk_prepare(uap->clk);
624 uap->port.uartclk = clk_get_rate(uap->clk);
627 uart_parse_options(options, &baud, &parity, &bits, &flow);
629 pl010_console_get_options(uap, &baud, &parity, &bits);
631 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
634 static struct uart_driver amba_reg;
635 static struct console amba_console = {
637 .write = pl010_console_write,
638 .device = uart_console_device,
639 .setup = pl010_console_setup,
640 .flags = CON_PRINTBUFFER,
645 #define AMBA_CONSOLE &amba_console
647 #define AMBA_CONSOLE NULL
650 static DEFINE_MUTEX(amba_reg_lock);
651 static struct uart_driver amba_reg = {
652 .owner = THIS_MODULE,
653 .driver_name = "ttyAM",
655 .major = SERIAL_AMBA_MAJOR,
656 .minor = SERIAL_AMBA_MINOR,
658 .cons = AMBA_CONSOLE,
661 static int pl010_probe(struct amba_device *dev, const struct amba_id *id)
663 struct uart_amba_port *uap;
667 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
668 if (amba_ports[i] == NULL)
671 if (i == ARRAY_SIZE(amba_ports))
674 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
679 base = devm_ioremap(&dev->dev, dev->res.start,
680 resource_size(&dev->res));
684 uap->clk = devm_clk_get(&dev->dev, NULL);
685 if (IS_ERR(uap->clk))
686 return PTR_ERR(uap->clk);
688 uap->port.dev = &dev->dev;
689 uap->port.mapbase = dev->res.start;
690 uap->port.membase = base;
691 uap->port.iotype = UPIO_MEM;
692 uap->port.irq = dev->irq[0];
693 uap->port.fifosize = 16;
694 uap->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_AMBA_PL010_CONSOLE);
695 uap->port.ops = &amba_pl010_pops;
696 uap->port.flags = UPF_BOOT_AUTOCONF;
699 uap->data = dev_get_platdata(&dev->dev);
703 amba_set_drvdata(dev, uap);
705 mutex_lock(&amba_reg_lock);
706 if (!amba_reg.state) {
707 ret = uart_register_driver(&amba_reg);
709 mutex_unlock(&amba_reg_lock);
710 dev_err(uap->port.dev,
711 "Failed to register AMBA-PL010 driver\n");
715 mutex_unlock(&amba_reg_lock);
717 ret = uart_add_one_port(&amba_reg, &uap->port);
719 amba_ports[i] = NULL;
724 static void pl010_remove(struct amba_device *dev)
726 struct uart_amba_port *uap = amba_get_drvdata(dev);
730 uart_remove_one_port(&amba_reg, &uap->port);
732 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
733 if (amba_ports[i] == uap)
734 amba_ports[i] = NULL;
735 else if (amba_ports[i])
739 uart_unregister_driver(&amba_reg);
742 #ifdef CONFIG_PM_SLEEP
743 static int pl010_suspend(struct device *dev)
745 struct uart_amba_port *uap = dev_get_drvdata(dev);
748 uart_suspend_port(&amba_reg, &uap->port);
753 static int pl010_resume(struct device *dev)
755 struct uart_amba_port *uap = dev_get_drvdata(dev);
758 uart_resume_port(&amba_reg, &uap->port);
764 static SIMPLE_DEV_PM_OPS(pl010_dev_pm_ops, pl010_suspend, pl010_resume);
766 static const struct amba_id pl010_ids[] = {
774 MODULE_DEVICE_TABLE(amba, pl010_ids);
776 static struct amba_driver pl010_driver = {
778 .name = "uart-pl010",
779 .pm = &pl010_dev_pm_ops,
781 .id_table = pl010_ids,
782 .probe = pl010_probe,
783 .remove = pl010_remove,
786 static int __init pl010_init(void)
788 printk(KERN_INFO "Serial: AMBA driver\n");
790 return amba_driver_register(&pl010_driver);
793 static void __exit pl010_exit(void)
795 amba_driver_unregister(&pl010_driver);
798 module_init(pl010_init);
799 module_exit(pl010_exit);
801 MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
802 MODULE_DESCRIPTION("ARM AMBA serial port driver");
803 MODULE_LICENSE("GPL");