2 * linux/drivers/char/amba.c
4 * Driver for AMBA serial ports
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000 Deep Blue Solutions Ltd.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * $Id: amba.c,v 1.41 2002/07/28 10:03:27 rmk Exp $
27 * This is a generic driver for ARM AMBA-type serial ports. They
28 * have a lot of 16550-like features, but are not register compatible.
29 * Note that although they do have CTS, DCD and DSR inputs, they do
30 * not have an RI input, nor do they have DTR or RTS outputs. If
31 * required, these have to be supplied via some other means (eg, GPIO)
32 * and hooked into this driver.
35 #if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
39 #include <linux/module.h>
40 #include <linux/ioport.h>
41 #include <linux/init.h>
42 #include <linux/console.h>
43 #include <linux/sysrq.h>
44 #include <linux/device.h>
45 #include <linux/tty.h>
46 #include <linux/tty_flip.h>
47 #include <linux/serial_core.h>
48 #include <linux/serial.h>
49 #include <linux/amba/bus.h>
50 #include <linux/amba/serial.h>
51 #include <linux/clk.h>
54 #include <asm/sizes.h>
58 #define SERIAL_AMBA_MAJOR 204
59 #define SERIAL_AMBA_MINOR 64
60 #define SERIAL_AMBA_NR UART_NR
62 #define AMBA_ISR_PASS_LIMIT 256
64 #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
65 #define UART_DUMMY_DR_RX (1 << 16)
68 * We wrap our port structure around the generic uart_port.
70 struct uart_amba_port {
71 struct uart_port port;
73 unsigned int im; /* interrupt mask */
74 unsigned int old_status;
77 static void pl011_stop_tx(struct uart_port *port)
79 struct uart_amba_port *uap = (struct uart_amba_port *)port;
81 uap->im &= ~UART011_TXIM;
82 writew(uap->im, uap->port.membase + UART011_IMSC);
85 static void pl011_start_tx(struct uart_port *port)
87 struct uart_amba_port *uap = (struct uart_amba_port *)port;
89 uap->im |= UART011_TXIM;
90 writew(uap->im, uap->port.membase + UART011_IMSC);
93 static void pl011_stop_rx(struct uart_port *port)
95 struct uart_amba_port *uap = (struct uart_amba_port *)port;
97 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
98 UART011_PEIM|UART011_BEIM|UART011_OEIM);
99 writew(uap->im, uap->port.membase + UART011_IMSC);
102 static void pl011_enable_ms(struct uart_port *port)
104 struct uart_amba_port *uap = (struct uart_amba_port *)port;
106 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
107 writew(uap->im, uap->port.membase + UART011_IMSC);
110 static void pl011_rx_chars(struct uart_amba_port *uap)
112 struct tty_struct *tty = uap->port.info->tty;
113 unsigned int status, ch, flag, max_count = 256;
115 status = readw(uap->port.membase + UART01x_FR);
116 while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
117 ch = readw(uap->port.membase + UART01x_DR) | UART_DUMMY_DR_RX;
119 uap->port.icount.rx++;
122 * Note that the error handling code is
123 * out of the main execution path
125 if (unlikely(ch & UART_DR_ERROR)) {
126 if (ch & UART011_DR_BE) {
127 ch &= ~(UART011_DR_FE | UART011_DR_PE);
128 uap->port.icount.brk++;
129 if (uart_handle_break(&uap->port))
131 } else if (ch & UART011_DR_PE)
132 uap->port.icount.parity++;
133 else if (ch & UART011_DR_FE)
134 uap->port.icount.frame++;
135 if (ch & UART011_DR_OE)
136 uap->port.icount.overrun++;
138 ch &= uap->port.read_status_mask;
140 if (ch & UART011_DR_BE)
142 else if (ch & UART011_DR_PE)
144 else if (ch & UART011_DR_FE)
148 if (uart_handle_sysrq_char(&uap->port, ch & 255))
151 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
154 status = readw(uap->port.membase + UART01x_FR);
156 tty_flip_buffer_push(tty);
160 static void pl011_tx_chars(struct uart_amba_port *uap)
162 struct circ_buf *xmit = &uap->port.info->xmit;
165 if (uap->port.x_char) {
166 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
167 uap->port.icount.tx++;
168 uap->port.x_char = 0;
171 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
172 pl011_stop_tx(&uap->port);
176 count = uap->port.fifosize >> 1;
178 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
179 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
180 uap->port.icount.tx++;
181 if (uart_circ_empty(xmit))
183 } while (--count > 0);
185 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
186 uart_write_wakeup(&uap->port);
188 if (uart_circ_empty(xmit))
189 pl011_stop_tx(&uap->port);
192 static void pl011_modem_status(struct uart_amba_port *uap)
194 unsigned int status, delta;
196 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
198 delta = status ^ uap->old_status;
199 uap->old_status = status;
204 if (delta & UART01x_FR_DCD)
205 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
207 if (delta & UART01x_FR_DSR)
208 uap->port.icount.dsr++;
210 if (delta & UART01x_FR_CTS)
211 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
213 wake_up_interruptible(&uap->port.info->delta_msr_wait);
216 static irqreturn_t pl011_int(int irq, void *dev_id)
218 struct uart_amba_port *uap = dev_id;
219 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
222 spin_lock(&uap->port.lock);
224 status = readw(uap->port.membase + UART011_MIS);
227 writew(status & ~(UART011_TXIS|UART011_RTIS|
229 uap->port.membase + UART011_ICR);
231 if (status & (UART011_RTIS|UART011_RXIS))
233 if (status & (UART011_DSRMIS|UART011_DCDMIS|
234 UART011_CTSMIS|UART011_RIMIS))
235 pl011_modem_status(uap);
236 if (status & UART011_TXIS)
239 if (pass_counter-- == 0)
242 status = readw(uap->port.membase + UART011_MIS);
243 } while (status != 0);
247 spin_unlock(&uap->port.lock);
249 return IRQ_RETVAL(handled);
252 static unsigned int pl01x_tx_empty(struct uart_port *port)
254 struct uart_amba_port *uap = (struct uart_amba_port *)port;
255 unsigned int status = readw(uap->port.membase + UART01x_FR);
256 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
259 static unsigned int pl01x_get_mctrl(struct uart_port *port)
261 struct uart_amba_port *uap = (struct uart_amba_port *)port;
262 unsigned int result = 0;
263 unsigned int status = readw(uap->port.membase + UART01x_FR);
265 #define BIT(uartbit, tiocmbit) \
266 if (status & uartbit) \
269 BIT(UART01x_FR_DCD, TIOCM_CAR);
270 BIT(UART01x_FR_DSR, TIOCM_DSR);
271 BIT(UART01x_FR_CTS, TIOCM_CTS);
272 BIT(UART011_FR_RI, TIOCM_RNG);
277 static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
279 struct uart_amba_port *uap = (struct uart_amba_port *)port;
282 cr = readw(uap->port.membase + UART011_CR);
284 #define BIT(tiocmbit, uartbit) \
285 if (mctrl & tiocmbit) \
290 BIT(TIOCM_RTS, UART011_CR_RTS);
291 BIT(TIOCM_DTR, UART011_CR_DTR);
292 BIT(TIOCM_OUT1, UART011_CR_OUT1);
293 BIT(TIOCM_OUT2, UART011_CR_OUT2);
294 BIT(TIOCM_LOOP, UART011_CR_LBE);
297 writew(cr, uap->port.membase + UART011_CR);
300 static void pl011_break_ctl(struct uart_port *port, int break_state)
302 struct uart_amba_port *uap = (struct uart_amba_port *)port;
306 spin_lock_irqsave(&uap->port.lock, flags);
307 lcr_h = readw(uap->port.membase + UART011_LCRH);
308 if (break_state == -1)
309 lcr_h |= UART01x_LCRH_BRK;
311 lcr_h &= ~UART01x_LCRH_BRK;
312 writew(lcr_h, uap->port.membase + UART011_LCRH);
313 spin_unlock_irqrestore(&uap->port.lock, flags);
316 static int pl011_startup(struct uart_port *port)
318 struct uart_amba_port *uap = (struct uart_amba_port *)port;
323 * Try to enable the clock producer.
325 retval = clk_enable(uap->clk);
329 uap->port.uartclk = clk_get_rate(uap->clk);
334 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
338 writew(UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
339 uap->port.membase + UART011_IFLS);
342 * Provoke TX FIFO interrupt into asserting.
344 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
345 writew(cr, uap->port.membase + UART011_CR);
346 writew(0, uap->port.membase + UART011_FBRD);
347 writew(1, uap->port.membase + UART011_IBRD);
348 writew(0, uap->port.membase + UART011_LCRH);
349 writew(0, uap->port.membase + UART01x_DR);
350 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
353 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
354 writew(cr, uap->port.membase + UART011_CR);
357 * initialise the old status of the modem signals
359 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
362 * Finally, enable interrupts
364 spin_lock_irq(&uap->port.lock);
365 uap->im = UART011_RXIM | UART011_RTIM;
366 writew(uap->im, uap->port.membase + UART011_IMSC);
367 spin_unlock_irq(&uap->port.lock);
372 clk_disable(uap->clk);
377 static void pl011_shutdown(struct uart_port *port)
379 struct uart_amba_port *uap = (struct uart_amba_port *)port;
383 * disable all interrupts
385 spin_lock_irq(&uap->port.lock);
387 writew(uap->im, uap->port.membase + UART011_IMSC);
388 writew(0xffff, uap->port.membase + UART011_ICR);
389 spin_unlock_irq(&uap->port.lock);
394 free_irq(uap->port.irq, uap);
399 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
402 * disable break condition and fifos
404 val = readw(uap->port.membase + UART011_LCRH);
405 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
406 writew(val, uap->port.membase + UART011_LCRH);
409 * Shut down the clock producer
411 clk_disable(uap->clk);
415 pl011_set_termios(struct uart_port *port, struct ktermios *termios,
416 struct ktermios *old)
418 unsigned int lcr_h, old_cr;
420 unsigned int baud, quot;
423 * Ask the core to calculate the divisor for us.
425 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
426 quot = port->uartclk * 4 / baud;
428 switch (termios->c_cflag & CSIZE) {
430 lcr_h = UART01x_LCRH_WLEN_5;
433 lcr_h = UART01x_LCRH_WLEN_6;
436 lcr_h = UART01x_LCRH_WLEN_7;
439 lcr_h = UART01x_LCRH_WLEN_8;
442 if (termios->c_cflag & CSTOPB)
443 lcr_h |= UART01x_LCRH_STP2;
444 if (termios->c_cflag & PARENB) {
445 lcr_h |= UART01x_LCRH_PEN;
446 if (!(termios->c_cflag & PARODD))
447 lcr_h |= UART01x_LCRH_EPS;
449 if (port->fifosize > 1)
450 lcr_h |= UART01x_LCRH_FEN;
452 spin_lock_irqsave(&port->lock, flags);
455 * Update the per-port timeout.
457 uart_update_timeout(port, termios->c_cflag, baud);
459 port->read_status_mask = UART011_DR_OE | 255;
460 if (termios->c_iflag & INPCK)
461 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
462 if (termios->c_iflag & (BRKINT | PARMRK))
463 port->read_status_mask |= UART011_DR_BE;
466 * Characters to ignore
468 port->ignore_status_mask = 0;
469 if (termios->c_iflag & IGNPAR)
470 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
471 if (termios->c_iflag & IGNBRK) {
472 port->ignore_status_mask |= UART011_DR_BE;
474 * If we're ignoring parity and break indicators,
475 * ignore overruns too (for real raw support).
477 if (termios->c_iflag & IGNPAR)
478 port->ignore_status_mask |= UART011_DR_OE;
482 * Ignore all characters if CREAD is not set.
484 if ((termios->c_cflag & CREAD) == 0)
485 port->ignore_status_mask |= UART_DUMMY_DR_RX;
487 if (UART_ENABLE_MS(port, termios->c_cflag))
488 pl011_enable_ms(port);
490 /* first, disable everything */
491 old_cr = readw(port->membase + UART011_CR);
492 writew(0, port->membase + UART011_CR);
495 writew(quot & 0x3f, port->membase + UART011_FBRD);
496 writew(quot >> 6, port->membase + UART011_IBRD);
499 * ----------v----------v----------v----------v-----
500 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
501 * ----------^----------^----------^----------^-----
503 writew(lcr_h, port->membase + UART011_LCRH);
504 writew(old_cr, port->membase + UART011_CR);
506 spin_unlock_irqrestore(&port->lock, flags);
509 static const char *pl011_type(struct uart_port *port)
511 return port->type == PORT_AMBA ? "AMBA/PL011" : NULL;
515 * Release the memory region(s) being used by 'port'
517 static void pl010_release_port(struct uart_port *port)
519 release_mem_region(port->mapbase, SZ_4K);
523 * Request the memory region(s) being used by 'port'
525 static int pl010_request_port(struct uart_port *port)
527 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
528 != NULL ? 0 : -EBUSY;
532 * Configure/autoconfigure the port.
534 static void pl010_config_port(struct uart_port *port, int flags)
536 if (flags & UART_CONFIG_TYPE) {
537 port->type = PORT_AMBA;
538 pl010_request_port(port);
543 * verify the new serial_struct (for TIOCSSERIAL).
545 static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
548 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
550 if (ser->irq < 0 || ser->irq >= NR_IRQS)
552 if (ser->baud_base < 9600)
557 static struct uart_ops amba_pl011_pops = {
558 .tx_empty = pl01x_tx_empty,
559 .set_mctrl = pl011_set_mctrl,
560 .get_mctrl = pl01x_get_mctrl,
561 .stop_tx = pl011_stop_tx,
562 .start_tx = pl011_start_tx,
563 .stop_rx = pl011_stop_rx,
564 .enable_ms = pl011_enable_ms,
565 .break_ctl = pl011_break_ctl,
566 .startup = pl011_startup,
567 .shutdown = pl011_shutdown,
568 .set_termios = pl011_set_termios,
570 .release_port = pl010_release_port,
571 .request_port = pl010_request_port,
572 .config_port = pl010_config_port,
573 .verify_port = pl010_verify_port,
576 static struct uart_amba_port *amba_ports[UART_NR];
578 #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
580 static void pl011_console_putchar(struct uart_port *port, int ch)
582 struct uart_amba_port *uap = (struct uart_amba_port *)port;
584 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
586 writew(ch, uap->port.membase + UART01x_DR);
590 pl011_console_write(struct console *co, const char *s, unsigned int count)
592 struct uart_amba_port *uap = amba_ports[co->index];
593 unsigned int status, old_cr, new_cr;
595 clk_enable(uap->clk);
598 * First save the CR then disable the interrupts
600 old_cr = readw(uap->port.membase + UART011_CR);
601 new_cr = old_cr & ~UART011_CR_CTSEN;
602 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
603 writew(new_cr, uap->port.membase + UART011_CR);
605 uart_console_write(&uap->port, s, count, pl011_console_putchar);
608 * Finally, wait for transmitter to become empty
609 * and restore the TCR
612 status = readw(uap->port.membase + UART01x_FR);
613 } while (status & UART01x_FR_BUSY);
614 writew(old_cr, uap->port.membase + UART011_CR);
616 clk_disable(uap->clk);
620 pl011_console_get_options(struct uart_amba_port *uap, int *baud,
621 int *parity, int *bits)
623 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
624 unsigned int lcr_h, ibrd, fbrd;
626 lcr_h = readw(uap->port.membase + UART011_LCRH);
629 if (lcr_h & UART01x_LCRH_PEN) {
630 if (lcr_h & UART01x_LCRH_EPS)
636 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
641 ibrd = readw(uap->port.membase + UART011_IBRD);
642 fbrd = readw(uap->port.membase + UART011_FBRD);
644 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
648 static int __init pl011_console_setup(struct console *co, char *options)
650 struct uart_amba_port *uap;
657 * Check whether an invalid uart number has been specified, and
658 * if so, search for the first available port that does have
661 if (co->index >= UART_NR)
663 uap = amba_ports[co->index];
667 uap->port.uartclk = clk_get_rate(uap->clk);
670 uart_parse_options(options, &baud, &parity, &bits, &flow);
672 pl011_console_get_options(uap, &baud, &parity, &bits);
674 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
677 static struct uart_driver amba_reg;
678 static struct console amba_console = {
680 .write = pl011_console_write,
681 .device = uart_console_device,
682 .setup = pl011_console_setup,
683 .flags = CON_PRINTBUFFER,
688 #define AMBA_CONSOLE (&amba_console)
690 #define AMBA_CONSOLE NULL
693 static struct uart_driver amba_reg = {
694 .owner = THIS_MODULE,
695 .driver_name = "ttyAMA",
696 .dev_name = "ttyAMA",
697 .major = SERIAL_AMBA_MAJOR,
698 .minor = SERIAL_AMBA_MINOR,
700 .cons = AMBA_CONSOLE,
703 static int pl011_probe(struct amba_device *dev, void *id)
705 struct uart_amba_port *uap;
709 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
710 if (amba_ports[i] == NULL)
713 if (i == ARRAY_SIZE(amba_ports)) {
718 uap = kmalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
724 base = ioremap(dev->res.start, PAGE_SIZE);
730 memset(uap, 0, sizeof(struct uart_amba_port));
731 uap->clk = clk_get(&dev->dev, "UARTCLK");
732 if (IS_ERR(uap->clk)) {
733 ret = PTR_ERR(uap->clk);
737 uap->port.dev = &dev->dev;
738 uap->port.mapbase = dev->res.start;
739 uap->port.membase = base;
740 uap->port.iotype = UPIO_MEM;
741 uap->port.irq = dev->irq[0];
742 uap->port.fifosize = 16;
743 uap->port.ops = &amba_pl011_pops;
744 uap->port.flags = UPF_BOOT_AUTOCONF;
749 amba_set_drvdata(dev, uap);
750 ret = uart_add_one_port(&amba_reg, &uap->port);
752 amba_set_drvdata(dev, NULL);
753 amba_ports[i] = NULL;
764 static int pl011_remove(struct amba_device *dev)
766 struct uart_amba_port *uap = amba_get_drvdata(dev);
769 amba_set_drvdata(dev, NULL);
771 uart_remove_one_port(&amba_reg, &uap->port);
773 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
774 if (amba_ports[i] == uap)
775 amba_ports[i] = NULL;
777 iounmap(uap->port.membase);
783 static struct amba_id pl011_ids[] __initdata = {
791 static struct amba_driver pl011_driver = {
793 .name = "uart-pl011",
795 .id_table = pl011_ids,
796 .probe = pl011_probe,
797 .remove = pl011_remove,
800 static int __init pl011_init(void)
803 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
805 ret = uart_register_driver(&amba_reg);
807 ret = amba_driver_register(&pl011_driver);
809 uart_unregister_driver(&amba_reg);
814 static void __exit pl011_exit(void)
816 amba_driver_unregister(&pl011_driver);
817 uart_unregister_driver(&amba_reg);
820 module_init(pl011_init);
821 module_exit(pl011_exit);
823 MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
824 MODULE_DESCRIPTION("ARM AMBA serial port driver");
825 MODULE_LICENSE("GPL");