2 * Driver for CLPS711x serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
28 #include <linux/module.h>
29 #include <linux/ioport.h>
30 #include <linux/init.h>
31 #include <linux/console.h>
32 #include <linux/sysrq.h>
33 #include <linux/spinlock.h>
34 #include <linux/device.h>
35 #include <linux/tty.h>
36 #include <linux/tty_flip.h>
37 #include <linux/serial_core.h>
38 #include <linux/serial.h>
40 #include <linux/platform_device.h>
42 #include <mach/hardware.h>
45 #define UART_CLPS711X_NAME "uart-clps711x"
49 #define SERIAL_CLPS711X_MAJOR 204
50 #define SERIAL_CLPS711X_MINOR 40
51 #define SERIAL_CLPS711X_NR UART_NR
54 * We use the relevant SYSCON register as a base address for these ports.
56 #define UBRLCR(port) ((port)->iobase + UBRLCR1 - SYSCON1)
57 #define UARTDR(port) ((port)->iobase + UARTDR1 - SYSCON1)
58 #define SYSFLG(port) ((port)->iobase + SYSFLG1 - SYSCON1)
59 #define SYSCON(port) ((port)->iobase + SYSCON1 - SYSCON1)
61 #define TX_IRQ(port) ((port)->irq)
62 #define RX_IRQ(port) ((port)->irq + 1)
64 #define UART_ANY_ERR (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR)
66 #define tx_enabled(port) ((port)->unused[0])
68 static void clps711xuart_stop_tx(struct uart_port *port)
70 if (tx_enabled(port)) {
71 disable_irq(TX_IRQ(port));
76 static void clps711xuart_start_tx(struct uart_port *port)
78 if (!tx_enabled(port)) {
79 enable_irq(TX_IRQ(port));
84 static void clps711xuart_stop_rx(struct uart_port *port)
86 disable_irq(RX_IRQ(port));
89 static void clps711xuart_enable_ms(struct uart_port *port)
93 static irqreturn_t clps711xuart_int_rx(int irq, void *dev_id)
95 struct uart_port *port = dev_id;
96 struct tty_struct *tty = port->state->port.tty;
97 unsigned int status, ch, flg;
99 status = clps_readl(SYSFLG(port));
100 while (!(status & SYSFLG_URXFE)) {
101 ch = clps_readl(UARTDR(port));
108 * Note that the error handling code is
109 * out of the main execution path
111 if (unlikely(ch & UART_ANY_ERR)) {
112 if (ch & UARTDR_PARERR)
113 port->icount.parity++;
114 else if (ch & UARTDR_FRMERR)
115 port->icount.frame++;
116 if (ch & UARTDR_OVERR)
117 port->icount.overrun++;
119 ch &= port->read_status_mask;
121 if (ch & UARTDR_PARERR)
123 else if (ch & UARTDR_FRMERR)
131 if (uart_handle_sysrq_char(port, ch))
135 * CHECK: does overrun affect the current character?
136 * ASSUMPTION: it does not.
138 uart_insert_char(port, ch, UARTDR_OVERR, ch, flg);
141 status = clps_readl(SYSFLG(port));
143 tty_flip_buffer_push(tty);
147 static irqreturn_t clps711xuart_int_tx(int irq, void *dev_id)
149 struct uart_port *port = dev_id;
150 struct circ_buf *xmit = &port->state->xmit;
154 clps_writel(port->x_char, UARTDR(port));
160 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
163 count = port->fifosize >> 1;
165 clps_writel(xmit->buf[xmit->tail], UARTDR(port));
166 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
168 if (uart_circ_empty(xmit))
170 } while (--count > 0);
172 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
173 uart_write_wakeup(port);
175 if (uart_circ_empty(xmit)) {
177 disable_irq_nosync(TX_IRQ(port));
178 tx_enabled(port) = 0;
184 static unsigned int clps711xuart_tx_empty(struct uart_port *port)
186 unsigned int status = clps_readl(SYSFLG(port));
187 return status & SYSFLG_UBUSY ? 0 : TIOCSER_TEMT;
190 static unsigned int clps711xuart_get_mctrl(struct uart_port *port)
192 unsigned int port_addr;
193 unsigned int result = 0;
196 port_addr = SYSFLG(port);
197 if (port_addr == SYSFLG1) {
198 status = clps_readl(SYSFLG1);
199 if (status & SYSFLG1_DCD)
201 if (status & SYSFLG1_DSR)
203 if (status & SYSFLG1_CTS)
211 clps711xuart_set_mctrl_null(struct uart_port *port, unsigned int mctrl)
215 static void clps711xuart_break_ctl(struct uart_port *port, int break_state)
220 spin_lock_irqsave(&port->lock, flags);
221 ubrlcr = clps_readl(UBRLCR(port));
222 if (break_state == -1)
223 ubrlcr |= UBRLCR_BREAK;
225 ubrlcr &= ~UBRLCR_BREAK;
226 clps_writel(ubrlcr, UBRLCR(port));
227 spin_unlock_irqrestore(&port->lock, flags);
230 static int clps711xuart_startup(struct uart_port *port)
235 tx_enabled(port) = 1;
240 retval = request_irq(TX_IRQ(port), clps711xuart_int_tx, 0,
241 "clps711xuart_tx", port);
245 retval = request_irq(RX_IRQ(port), clps711xuart_int_rx, 0,
246 "clps711xuart_rx", port);
248 free_irq(TX_IRQ(port), port);
255 syscon = clps_readl(SYSCON(port));
256 syscon |= SYSCON_UARTEN;
257 clps_writel(syscon, SYSCON(port));
262 static void clps711xuart_shutdown(struct uart_port *port)
264 unsigned int ubrlcr, syscon;
269 free_irq(TX_IRQ(port), port); /* TX interrupt */
270 free_irq(RX_IRQ(port), port); /* RX interrupt */
275 syscon = clps_readl(SYSCON(port));
276 syscon &= ~SYSCON_UARTEN;
277 clps_writel(syscon, SYSCON(port));
280 * disable break condition and fifos
282 ubrlcr = clps_readl(UBRLCR(port));
283 ubrlcr &= ~(UBRLCR_FIFOEN | UBRLCR_BREAK);
284 clps_writel(ubrlcr, UBRLCR(port));
288 clps711xuart_set_termios(struct uart_port *port, struct ktermios *termios,
289 struct ktermios *old)
291 unsigned int ubrlcr, baud, quot;
295 * We don't implement CREAD.
297 termios->c_cflag |= CREAD;
300 * Ask the core to calculate the divisor for us.
302 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
303 quot = uart_get_divisor(port, baud);
305 switch (termios->c_cflag & CSIZE) {
307 ubrlcr = UBRLCR_WRDLEN5;
310 ubrlcr = UBRLCR_WRDLEN6;
313 ubrlcr = UBRLCR_WRDLEN7;
316 ubrlcr = UBRLCR_WRDLEN8;
319 if (termios->c_cflag & CSTOPB)
320 ubrlcr |= UBRLCR_XSTOP;
321 if (termios->c_cflag & PARENB) {
322 ubrlcr |= UBRLCR_PRTEN;
323 if (!(termios->c_cflag & PARODD))
324 ubrlcr |= UBRLCR_EVENPRT;
326 if (port->fifosize > 1)
327 ubrlcr |= UBRLCR_FIFOEN;
329 spin_lock_irqsave(&port->lock, flags);
332 * Update the per-port timeout.
334 uart_update_timeout(port, termios->c_cflag, baud);
336 port->read_status_mask = UARTDR_OVERR;
337 if (termios->c_iflag & INPCK)
338 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
341 * Characters to ignore
343 port->ignore_status_mask = 0;
344 if (termios->c_iflag & IGNPAR)
345 port->ignore_status_mask |= UARTDR_FRMERR | UARTDR_PARERR;
346 if (termios->c_iflag & IGNBRK) {
348 * If we're ignoring parity and break indicators,
349 * ignore overruns to (for real raw support).
351 if (termios->c_iflag & IGNPAR)
352 port->ignore_status_mask |= UARTDR_OVERR;
357 clps_writel(ubrlcr | quot, UBRLCR(port));
359 spin_unlock_irqrestore(&port->lock, flags);
362 static const char *clps711xuart_type(struct uart_port *port)
364 return port->type == PORT_CLPS711X ? "CLPS711x" : NULL;
368 * Configure/autoconfigure the port.
370 static void clps711xuart_config_port(struct uart_port *port, int flags)
372 if (flags & UART_CONFIG_TYPE)
373 port->type = PORT_CLPS711X;
376 static void clps711xuart_release_port(struct uart_port *port)
380 static int clps711xuart_request_port(struct uart_port *port)
385 static struct uart_ops clps711x_pops = {
386 .tx_empty = clps711xuart_tx_empty,
387 .set_mctrl = clps711xuart_set_mctrl_null,
388 .get_mctrl = clps711xuart_get_mctrl,
389 .stop_tx = clps711xuart_stop_tx,
390 .start_tx = clps711xuart_start_tx,
391 .stop_rx = clps711xuart_stop_rx,
392 .enable_ms = clps711xuart_enable_ms,
393 .break_ctl = clps711xuart_break_ctl,
394 .startup = clps711xuart_startup,
395 .shutdown = clps711xuart_shutdown,
396 .set_termios = clps711xuart_set_termios,
397 .type = clps711xuart_type,
398 .config_port = clps711xuart_config_port,
399 .release_port = clps711xuart_release_port,
400 .request_port = clps711xuart_request_port,
403 static struct uart_port clps711x_ports[UART_NR] = {
406 .irq = IRQ_UTXINT1, /* IRQ_URXINT1, IRQ_UMSINT */
409 .ops = &clps711x_pops,
411 .flags = UPF_BOOT_AUTOCONF,
415 .irq = IRQ_UTXINT2, /* IRQ_URXINT2 */
418 .ops = &clps711x_pops,
420 .flags = UPF_BOOT_AUTOCONF,
424 #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
425 static void clps711xuart_console_putchar(struct uart_port *port, int ch)
427 while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF)
429 clps_writel(ch, UARTDR(port));
433 * Print a string to the serial port trying not to disturb
434 * any possible real use of the port...
436 * The console_lock must be held when we get here.
438 * Note that this is called with interrupts already disabled
441 clps711xuart_console_write(struct console *co, const char *s,
444 struct uart_port *port = clps711x_ports + co->index;
445 unsigned int status, syscon;
448 * Ensure that the port is enabled.
450 syscon = clps_readl(SYSCON(port));
451 clps_writel(syscon | SYSCON_UARTEN, SYSCON(port));
453 uart_console_write(port, s, count, clps711xuart_console_putchar);
456 * Finally, wait for transmitter to become empty
457 * and restore the uart state.
460 status = clps_readl(SYSFLG(port));
461 } while (status & SYSFLG_UBUSY);
463 clps_writel(syscon, SYSCON(port));
467 clps711xuart_console_get_options(struct uart_port *port, int *baud,
468 int *parity, int *bits)
470 if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) {
471 unsigned int ubrlcr, quot;
473 ubrlcr = clps_readl(UBRLCR(port));
476 if (ubrlcr & UBRLCR_PRTEN) {
477 if (ubrlcr & UBRLCR_EVENPRT)
483 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
488 quot = ubrlcr & UBRLCR_BAUD_MASK;
489 *baud = port->uartclk / (16 * (quot + 1));
493 static int __init clps711xuart_console_setup(struct console *co, char *options)
495 struct uart_port *port;
502 * Check whether an invalid uart number has been specified, and
503 * if so, search for the first available port that does have
506 port = uart_get_console(clps711x_ports, UART_NR, co);
509 uart_parse_options(options, &baud, &parity, &bits, &flow);
511 clps711xuart_console_get_options(port, &baud, &parity, &bits);
513 return uart_set_options(port, co, baud, parity, bits, flow);
516 static struct uart_driver clps711x_reg;
517 static struct console clps711x_console = {
519 .write = clps711xuart_console_write,
520 .device = uart_console_device,
521 .setup = clps711xuart_console_setup,
522 .flags = CON_PRINTBUFFER,
524 .data = &clps711x_reg,
527 static int __init clps711xuart_console_init(void)
529 register_console(&clps711x_console);
532 console_initcall(clps711xuart_console_init);
534 #define CLPS711X_CONSOLE &clps711x_console
536 #define CLPS711X_CONSOLE NULL
539 static struct uart_driver clps711x_reg = {
540 .driver_name = "ttyCL",
542 .major = SERIAL_CLPS711X_MAJOR,
543 .minor = SERIAL_CLPS711X_MINOR,
546 .cons = CLPS711X_CONSOLE,
549 static int __devinit uart_clps711x_probe(struct platform_device *pdev)
553 printk(KERN_INFO "Serial: CLPS711x driver\n");
555 ret = uart_register_driver(&clps711x_reg);
559 for (i = 0; i < UART_NR; i++)
560 uart_add_one_port(&clps711x_reg, &clps711x_ports[i]);
565 static int __devexit uart_clps711x_remove(struct platform_device *pdev)
569 for (i = 0; i < UART_NR; i++)
570 uart_remove_one_port(&clps711x_reg, &clps711x_ports[i]);
572 uart_unregister_driver(&clps711x_reg);
577 static struct platform_driver clps711x_uart_driver = {
579 .name = UART_CLPS711X_NAME,
580 .owner = THIS_MODULE,
582 .probe = uart_clps711x_probe,
583 .remove = __devexit_p(uart_clps711x_remove),
585 module_platform_driver(clps711x_uart_driver);
587 static struct platform_device clps711x_uart_device = {
588 .name = UART_CLPS711X_NAME,
591 static int __init uart_clps711x_init(void)
593 return platform_device_register(&clps711x_uart_device);
595 module_init(uart_clps711x_init);
597 static void __exit uart_clps711x_exit(void)
599 platform_device_unregister(&clps711x_uart_device);
601 module_exit(uart_clps711x_exit);
603 MODULE_AUTHOR("Deep Blue Solutions Ltd");
604 MODULE_DESCRIPTION("CLPS711X serial driver");
605 MODULE_LICENSE("GPL");