2 * drivers/serial/netx-serial.c
4 * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #if defined(CONFIG_SERIAL_NETX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
24 #include <linux/device.h>
25 #include <linux/module.h>
26 #include <linux/ioport.h>
27 #include <linux/init.h>
28 #include <linux/console.h>
29 #include <linux/sysrq.h>
30 #include <linux/platform_device.h>
31 #include <linux/tty.h>
32 #include <linux/tty_flip.h>
33 #include <linux/serial_core.h>
34 #include <linux/serial.h>
38 #include <asm/hardware.h>
39 #include <asm/arch/netx-regs.h>
41 /* We've been assigned a range on the "Low-density serial ports" major */
42 #define SERIAL_NX_MAJOR 204
43 #define MINOR_START 170
45 #ifdef CONFIG_SERIAL_NETX_CONSOLE
51 UART_BAUDDIV_MSB = 0x0c,
52 UART_BAUDDIV_LSB = 0x10,
59 UART_RTS_TRAIL = 0x2c,
60 UART_DRV_ENABLE = 0x30,
62 UART_RXFIFO_IRQLEVEL = 0x38,
63 UART_TXFIFO_IRQLEVEL = 0x3c,
71 #define LINE_CR_BRK (1<<0)
72 #define LINE_CR_PEN (1<<1)
73 #define LINE_CR_EPS (1<<2)
74 #define LINE_CR_STP2 (1<<3)
75 #define LINE_CR_FEN (1<<4)
76 #define LINE_CR_5BIT (0<<5)
77 #define LINE_CR_6BIT (1<<5)
78 #define LINE_CR_7BIT (2<<5)
79 #define LINE_CR_8BIT (3<<5)
80 #define LINE_CR_BITS_MASK (3<<5)
82 #define CR_UART_EN (1<<0)
83 #define CR_SIREN (1<<1)
84 #define CR_SIRLP (1<<2)
85 #define CR_MSIE (1<<3)
88 #define CR_RTIE (1<<6)
94 #define FR_BUSY (1<<3)
95 #define FR_RXFE (1<<4)
96 #define FR_TXFF (1<<5)
97 #define FR_RXFF (1<<6)
98 #define FR_TXFE (1<<7)
100 #define IIR_MIS (1<<0)
101 #define IIR_RIS (1<<1)
102 #define IIR_TIS (1<<2)
103 #define IIR_RTIS (1<<3)
106 #define RTS_CR_AUTO (1<<0)
107 #define RTS_CR_RTS (1<<1)
108 #define RTS_CR_COUNT (1<<2)
109 #define RTS_CR_MOD2 (1<<3)
110 #define RTS_CR_RTS_POL (1<<4)
111 #define RTS_CR_CTS_CTR (1<<5)
112 #define RTS_CR_CTS_POL (1<<6)
113 #define RTS_CR_STICK (1<<7)
115 #define UART_PORT_SIZE 0x40
116 #define DRIVER_NAME "netx-uart"
119 struct uart_port port;
122 static void netx_stop_tx(struct uart_port *port)
125 val = readl(port->membase + UART_CR);
126 writel(val & ~CR_TIE, port->membase + UART_CR);
129 static void netx_stop_rx(struct uart_port *port)
132 val = readl(port->membase + UART_CR);
133 writel(val & ~CR_RIE, port->membase + UART_CR);
136 static void netx_enable_ms(struct uart_port *port)
139 val = readl(port->membase + UART_CR);
140 writel(val | CR_MSIE, port->membase + UART_CR);
143 static inline void netx_transmit_buffer(struct uart_port *port)
145 struct circ_buf *xmit = &port->info->xmit;
148 writel(port->x_char, port->membase + UART_DR);
154 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
160 /* send xmit->buf[xmit->tail]
161 * out the port here */
162 writel(xmit->buf[xmit->tail], port->membase + UART_DR);
163 xmit->tail = (xmit->tail + 1) &
164 (UART_XMIT_SIZE - 1);
166 if (uart_circ_empty(xmit))
168 } while (!(readl(port->membase + UART_FR) & FR_TXFF));
170 if (uart_circ_empty(xmit))
174 static void netx_start_tx(struct uart_port *port)
177 readl(port->membase + UART_CR) | CR_TIE, port->membase + UART_CR);
179 if (!(readl(port->membase + UART_FR) & FR_TXFF))
180 netx_transmit_buffer(port);
183 static unsigned int netx_tx_empty(struct uart_port *port)
185 return readl(port->membase + UART_FR) & FR_BUSY ? 0 : TIOCSER_TEMT;
188 static void netx_txint(struct uart_port *port)
190 struct circ_buf *xmit = &port->info->xmit;
192 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
197 netx_transmit_buffer(port);
199 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
200 uart_write_wakeup(port);
203 static void netx_rxint(struct uart_port *port)
205 unsigned char rx, flg, status;
206 struct tty_struct *tty = port->info->port.tty;
208 while (!(readl(port->membase + UART_FR) & FR_RXFE)) {
209 rx = readl(port->membase + UART_DR);
212 status = readl(port->membase + UART_SR);
213 if (status & SR_BE) {
214 writel(0, port->membase + UART_SR);
215 if (uart_handle_break(port))
219 if (unlikely(status & (SR_FE | SR_PE | SR_OE))) {
222 port->icount.parity++;
223 else if (status & SR_FE)
224 port->icount.frame++;
226 port->icount.overrun++;
228 status &= port->read_status_mask;
232 else if (status & SR_PE)
234 else if (status & SR_FE)
238 if (uart_handle_sysrq_char(port, rx))
241 uart_insert_char(port, status, SR_OE, rx, flg);
244 tty_flip_buffer_push(tty);
248 static irqreturn_t netx_int(int irq, void *dev_id)
250 struct uart_port *port = dev_id;
252 unsigned char status;
254 spin_lock_irqsave(&port->lock,flags);
256 status = readl(port->membase + UART_IIR) & IIR_MASK;
258 if (status & IIR_RIS)
260 if (status & IIR_TIS)
262 if (status & IIR_MIS) {
263 if (readl(port->membase + UART_FR) & FR_CTS)
264 uart_handle_cts_change(port, 1);
266 uart_handle_cts_change(port, 0);
268 writel(0, port->membase + UART_IIR);
269 status = readl(port->membase + UART_IIR) & IIR_MASK;
272 spin_unlock_irqrestore(&port->lock,flags);
276 static unsigned int netx_get_mctrl(struct uart_port *port)
278 unsigned int ret = TIOCM_DSR | TIOCM_CAR;
280 if (readl(port->membase + UART_FR) & FR_CTS)
286 static void netx_set_mctrl(struct uart_port *port, unsigned int mctrl)
290 /* FIXME: Locking needed ? */
291 if (mctrl & TIOCM_RTS) {
292 val = readl(port->membase + UART_RTS_CR);
293 writel(val | RTS_CR_RTS, port->membase + UART_RTS_CR);
297 static void netx_break_ctl(struct uart_port *port, int break_state)
299 unsigned int line_cr;
300 spin_lock_irq(&port->lock);
302 line_cr = readl(port->membase + UART_LINE_CR);
303 if (break_state != 0)
304 line_cr |= LINE_CR_BRK;
306 line_cr &= ~LINE_CR_BRK;
307 writel(line_cr, port->membase + UART_LINE_CR);
309 spin_unlock_irq(&port->lock);
312 static int netx_startup(struct uart_port *port)
316 ret = request_irq(port->irq, netx_int, 0,
319 dev_err(port->dev, "unable to grab irq%d\n",port->irq);
323 writel(readl(port->membase + UART_LINE_CR) | LINE_CR_FEN,
324 port->membase + UART_LINE_CR);
326 writel(CR_MSIE | CR_RIE | CR_TIE | CR_RTIE | CR_UART_EN,
327 port->membase + UART_CR);
333 static void netx_shutdown(struct uart_port *port)
335 writel(0, port->membase + UART_CR) ;
337 free_irq(port->irq, port);
341 netx_set_termios(struct uart_port *port, struct ktermios *termios,
342 struct ktermios *old)
344 unsigned int baud, quot;
345 unsigned char old_cr;
346 unsigned char line_cr = LINE_CR_FEN;
347 unsigned char rts_cr = 0;
349 switch (termios->c_cflag & CSIZE) {
351 line_cr |= LINE_CR_5BIT;
354 line_cr |= LINE_CR_6BIT;
357 line_cr |= LINE_CR_7BIT;
360 line_cr |= LINE_CR_8BIT;
364 if (termios->c_cflag & CSTOPB)
365 line_cr |= LINE_CR_STP2;
367 if (termios->c_cflag & PARENB) {
368 line_cr |= LINE_CR_PEN;
369 if (!(termios->c_cflag & PARODD))
370 line_cr |= LINE_CR_EPS;
373 if (termios->c_cflag & CRTSCTS)
374 rts_cr = RTS_CR_AUTO | RTS_CR_CTS_CTR | RTS_CR_RTS_POL;
376 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
382 spin_lock_irq(&port->lock);
384 uart_update_timeout(port, termios->c_cflag, baud);
386 old_cr = readl(port->membase + UART_CR);
388 /* disable interrupts */
389 writel(old_cr & ~(CR_MSIE | CR_RIE | CR_TIE | CR_RTIE),
390 port->membase + UART_CR);
392 /* drain transmitter */
393 while (readl(port->membase + UART_FR) & FR_BUSY);
396 writel(old_cr & ~CR_UART_EN, port->membase + UART_CR);
398 /* modem status interrupts */
400 if (UART_ENABLE_MS(port, termios->c_cflag))
403 writel((quot>>8) & 0xff, port->membase + UART_BAUDDIV_MSB);
404 writel(quot & 0xff, port->membase + UART_BAUDDIV_LSB);
405 writel(line_cr, port->membase + UART_LINE_CR);
407 writel(rts_cr, port->membase + UART_RTS_CR);
410 * Characters to ignore
412 port->ignore_status_mask = 0;
413 if (termios->c_iflag & IGNPAR)
414 port->ignore_status_mask |= SR_PE;
415 if (termios->c_iflag & IGNBRK) {
416 port->ignore_status_mask |= SR_BE;
418 * If we're ignoring parity and break indicators,
419 * ignore overruns too (for real raw support).
421 if (termios->c_iflag & IGNPAR)
422 port->ignore_status_mask |= SR_PE;
425 port->read_status_mask = 0;
426 if (termios->c_iflag & (BRKINT | PARMRK))
427 port->read_status_mask |= SR_BE;
428 if (termios->c_iflag & INPCK)
429 port->read_status_mask |= SR_PE | SR_FE;
431 writel(old_cr, port->membase + UART_CR);
433 spin_unlock_irq(&port->lock);
436 static const char *netx_type(struct uart_port *port)
438 return port->type == PORT_NETX ? "NETX" : NULL;
441 static void netx_release_port(struct uart_port *port)
443 release_mem_region(port->mapbase, UART_PORT_SIZE);
446 static int netx_request_port(struct uart_port *port)
448 return request_mem_region(port->mapbase, UART_PORT_SIZE,
449 DRIVER_NAME) != NULL ? 0 : -EBUSY;
452 static void netx_config_port(struct uart_port *port, int flags)
454 if (flags & UART_CONFIG_TYPE && netx_request_port(port) == 0)
455 port->type = PORT_NETX;
459 netx_verify_port(struct uart_port *port, struct serial_struct *ser)
463 if (ser->type != PORT_UNKNOWN && ser->type != PORT_NETX)
469 static struct uart_ops netx_pops = {
470 .tx_empty = netx_tx_empty,
471 .set_mctrl = netx_set_mctrl,
472 .get_mctrl = netx_get_mctrl,
473 .stop_tx = netx_stop_tx,
474 .start_tx = netx_start_tx,
475 .stop_rx = netx_stop_rx,
476 .enable_ms = netx_enable_ms,
477 .break_ctl = netx_break_ctl,
478 .startup = netx_startup,
479 .shutdown = netx_shutdown,
480 .set_termios = netx_set_termios,
482 .release_port = netx_release_port,
483 .request_port = netx_request_port,
484 .config_port = netx_config_port,
485 .verify_port = netx_verify_port,
488 static struct netx_port netx_ports[] = {
493 .membase = (char __iomem *)io_p2v(NETX_PA_UART0),
494 .mapbase = NETX_PA_UART0,
495 .irq = NETX_IRQ_UART0,
496 .uartclk = 100000000,
498 .flags = UPF_BOOT_AUTOCONF,
506 .membase = (char __iomem *)io_p2v(NETX_PA_UART1),
507 .mapbase = NETX_PA_UART1,
508 .irq = NETX_IRQ_UART1,
509 .uartclk = 100000000,
511 .flags = UPF_BOOT_AUTOCONF,
519 .membase = (char __iomem *)io_p2v(NETX_PA_UART2),
520 .mapbase = NETX_PA_UART2,
521 .irq = NETX_IRQ_UART2,
522 .uartclk = 100000000,
524 .flags = UPF_BOOT_AUTOCONF,
531 static void netx_console_putchar(struct uart_port *port, int ch)
533 while (readl(port->membase + UART_FR) & FR_BUSY);
534 writel(ch, port->membase + UART_DR);
538 netx_console_write(struct console *co, const char *s, unsigned int count)
540 struct uart_port *port = &netx_ports[co->index].port;
541 unsigned char cr_save;
543 cr_save = readl(port->membase + UART_CR);
544 writel(cr_save | CR_UART_EN, port->membase + UART_CR);
546 uart_console_write(port, s, count, netx_console_putchar);
548 while (readl(port->membase + UART_FR) & FR_BUSY);
549 writel(cr_save, port->membase + UART_CR);
553 netx_console_get_options(struct uart_port *port, int *baud,
554 int *parity, int *bits, int *flow)
556 unsigned char line_cr;
558 *baud = (readl(port->membase + UART_BAUDDIV_MSB) << 8) |
559 readl(port->membase + UART_BAUDDIV_LSB);
566 line_cr = readl(port->membase + UART_LINE_CR);
568 if (line_cr & LINE_CR_PEN) {
569 if (line_cr & LINE_CR_EPS)
575 switch (line_cr & LINE_CR_BITS_MASK) {
590 if (readl(port->membase + UART_RTS_CR) & RTS_CR_AUTO)
595 netx_console_setup(struct console *co, char *options)
597 struct netx_port *sport;
604 * Check whether an invalid uart number has been specified, and
605 * if so, search for the first available port that does have
608 if (co->index == -1 || co->index >= ARRAY_SIZE(netx_ports))
610 sport = &netx_ports[co->index];
613 uart_parse_options(options, &baud, &parity, &bits, &flow);
615 /* if the UART is enabled, assume it has been correctly setup
616 * by the bootloader and get the options
618 if (readl(sport->port.membase + UART_CR) & CR_UART_EN) {
619 netx_console_get_options(&sport->port, &baud,
620 &parity, &bits, &flow);
625 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
628 static struct uart_driver netx_reg;
629 static struct console netx_console = {
631 .write = netx_console_write,
632 .device = uart_console_device,
633 .setup = netx_console_setup,
634 .flags = CON_PRINTBUFFER,
639 static int __init netx_console_init(void)
641 register_console(&netx_console);
644 console_initcall(netx_console_init);
646 #define NETX_CONSOLE &netx_console
648 #define NETX_CONSOLE NULL
651 static struct uart_driver netx_reg = {
652 .owner = THIS_MODULE,
653 .driver_name = DRIVER_NAME,
655 .major = SERIAL_NX_MAJOR,
656 .minor = MINOR_START,
657 .nr = ARRAY_SIZE(netx_ports),
658 .cons = NETX_CONSOLE,
661 static int serial_netx_suspend(struct platform_device *pdev, pm_message_t state)
663 struct netx_port *sport = platform_get_drvdata(pdev);
666 uart_suspend_port(&netx_reg, &sport->port);
671 static int serial_netx_resume(struct platform_device *pdev)
673 struct netx_port *sport = platform_get_drvdata(pdev);
676 uart_resume_port(&netx_reg, &sport->port);
681 static int serial_netx_probe(struct platform_device *pdev)
683 struct uart_port *port = &netx_ports[pdev->id].port;
685 dev_info(&pdev->dev, "initialising\n");
687 port->dev = &pdev->dev;
689 writel(1, port->membase + UART_RXFIFO_IRQLEVEL);
690 uart_add_one_port(&netx_reg, &netx_ports[pdev->id].port);
691 platform_set_drvdata(pdev, &netx_ports[pdev->id]);
696 static int serial_netx_remove(struct platform_device *pdev)
698 struct netx_port *sport = platform_get_drvdata(pdev);
700 platform_set_drvdata(pdev, NULL);
703 uart_remove_one_port(&netx_reg, &sport->port);
708 static struct platform_driver serial_netx_driver = {
709 .probe = serial_netx_probe,
710 .remove = serial_netx_remove,
712 .suspend = serial_netx_suspend,
713 .resume = serial_netx_resume,
717 .owner = THIS_MODULE,
721 static int __init netx_serial_init(void)
725 printk(KERN_INFO "Serial: NetX driver\n");
727 ret = uart_register_driver(&netx_reg);
731 ret = platform_driver_register(&serial_netx_driver);
733 uart_unregister_driver(&netx_reg);
738 static void __exit netx_serial_exit(void)
740 platform_driver_unregister(&serial_netx_driver);
741 uart_unregister_driver(&netx_reg);
744 module_init(netx_serial_init);
745 module_exit(netx_serial_exit);
747 MODULE_AUTHOR("Sascha Hauer");
748 MODULE_DESCRIPTION("NetX serial port driver");
749 MODULE_LICENSE("GPL");
750 MODULE_ALIAS("platform:" DRIVER_NAME);