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>
41 #include <mach/hardware.h>
46 #define SERIAL_CLPS711X_MAJOR 204
47 #define SERIAL_CLPS711X_MINOR 40
48 #define SERIAL_CLPS711X_NR UART_NR
51 * We use the relevant SYSCON register as a base address for these ports.
53 #define UBRLCR(port) ((port)->iobase + UBRLCR1 - SYSCON1)
54 #define UARTDR(port) ((port)->iobase + UARTDR1 - SYSCON1)
55 #define SYSFLG(port) ((port)->iobase + SYSFLG1 - SYSCON1)
56 #define SYSCON(port) ((port)->iobase + SYSCON1 - SYSCON1)
58 #define TX_IRQ(port) ((port)->irq)
59 #define RX_IRQ(port) ((port)->irq + 1)
61 #define UART_ANY_ERR (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR)
63 #define tx_enabled(port) ((port)->unused[0])
65 static void clps711xuart_stop_tx(struct uart_port *port)
67 if (tx_enabled(port)) {
68 disable_irq(TX_IRQ(port));
73 static void clps711xuart_start_tx(struct uart_port *port)
75 if (!tx_enabled(port)) {
76 enable_irq(TX_IRQ(port));
81 static void clps711xuart_stop_rx(struct uart_port *port)
83 disable_irq(RX_IRQ(port));
86 static void clps711xuart_enable_ms(struct uart_port *port)
90 static irqreturn_t clps711xuart_int_rx(int irq, void *dev_id)
92 struct uart_port *port = dev_id;
93 struct tty_struct *tty = port->state->port.tty;
94 unsigned int status, ch, flg;
96 status = clps_readl(SYSFLG(port));
97 while (!(status & SYSFLG_URXFE)) {
98 ch = clps_readl(UARTDR(port));
105 * Note that the error handling code is
106 * out of the main execution path
108 if (unlikely(ch & UART_ANY_ERR)) {
109 if (ch & UARTDR_PARERR)
110 port->icount.parity++;
111 else if (ch & UARTDR_FRMERR)
112 port->icount.frame++;
113 if (ch & UARTDR_OVERR)
114 port->icount.overrun++;
116 ch &= port->read_status_mask;
118 if (ch & UARTDR_PARERR)
120 else if (ch & UARTDR_FRMERR)
128 if (uart_handle_sysrq_char(port, ch))
132 * CHECK: does overrun affect the current character?
133 * ASSUMPTION: it does not.
135 uart_insert_char(port, ch, UARTDR_OVERR, ch, flg);
138 status = clps_readl(SYSFLG(port));
140 tty_flip_buffer_push(tty);
144 static irqreturn_t clps711xuart_int_tx(int irq, void *dev_id)
146 struct uart_port *port = dev_id;
147 struct circ_buf *xmit = &port->state->xmit;
151 clps_writel(port->x_char, UARTDR(port));
157 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
160 count = port->fifosize >> 1;
162 clps_writel(xmit->buf[xmit->tail], UARTDR(port));
163 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
165 if (uart_circ_empty(xmit))
167 } while (--count > 0);
169 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
170 uart_write_wakeup(port);
172 if (uart_circ_empty(xmit)) {
174 disable_irq_nosync(TX_IRQ(port));
175 tx_enabled(port) = 0;
181 static unsigned int clps711xuart_tx_empty(struct uart_port *port)
183 unsigned int status = clps_readl(SYSFLG(port));
184 return status & SYSFLG_UBUSY ? 0 : TIOCSER_TEMT;
187 static unsigned int clps711xuart_get_mctrl(struct uart_port *port)
189 unsigned int port_addr;
190 unsigned int result = 0;
193 port_addr = SYSFLG(port);
194 if (port_addr == SYSFLG1) {
195 status = clps_readl(SYSFLG1);
196 if (status & SYSFLG1_DCD)
198 if (status & SYSFLG1_DSR)
200 if (status & SYSFLG1_CTS)
208 clps711xuart_set_mctrl_null(struct uart_port *port, unsigned int mctrl)
212 static void clps711xuart_break_ctl(struct uart_port *port, int break_state)
217 spin_lock_irqsave(&port->lock, flags);
218 ubrlcr = clps_readl(UBRLCR(port));
219 if (break_state == -1)
220 ubrlcr |= UBRLCR_BREAK;
222 ubrlcr &= ~UBRLCR_BREAK;
223 clps_writel(ubrlcr, UBRLCR(port));
224 spin_unlock_irqrestore(&port->lock, flags);
227 static int clps711xuart_startup(struct uart_port *port)
232 tx_enabled(port) = 1;
237 retval = request_irq(TX_IRQ(port), clps711xuart_int_tx, 0,
238 "clps711xuart_tx", port);
242 retval = request_irq(RX_IRQ(port), clps711xuart_int_rx, 0,
243 "clps711xuart_rx", port);
245 free_irq(TX_IRQ(port), port);
252 syscon = clps_readl(SYSCON(port));
253 syscon |= SYSCON_UARTEN;
254 clps_writel(syscon, SYSCON(port));
259 static void clps711xuart_shutdown(struct uart_port *port)
261 unsigned int ubrlcr, syscon;
266 free_irq(TX_IRQ(port), port); /* TX interrupt */
267 free_irq(RX_IRQ(port), port); /* RX interrupt */
272 syscon = clps_readl(SYSCON(port));
273 syscon &= ~SYSCON_UARTEN;
274 clps_writel(syscon, SYSCON(port));
277 * disable break condition and fifos
279 ubrlcr = clps_readl(UBRLCR(port));
280 ubrlcr &= ~(UBRLCR_FIFOEN | UBRLCR_BREAK);
281 clps_writel(ubrlcr, UBRLCR(port));
285 clps711xuart_set_termios(struct uart_port *port, struct ktermios *termios,
286 struct ktermios *old)
288 unsigned int ubrlcr, baud, quot;
292 * We don't implement CREAD.
294 termios->c_cflag |= CREAD;
297 * Ask the core to calculate the divisor for us.
299 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
300 quot = uart_get_divisor(port, baud);
302 switch (termios->c_cflag & CSIZE) {
304 ubrlcr = UBRLCR_WRDLEN5;
307 ubrlcr = UBRLCR_WRDLEN6;
310 ubrlcr = UBRLCR_WRDLEN7;
313 ubrlcr = UBRLCR_WRDLEN8;
316 if (termios->c_cflag & CSTOPB)
317 ubrlcr |= UBRLCR_XSTOP;
318 if (termios->c_cflag & PARENB) {
319 ubrlcr |= UBRLCR_PRTEN;
320 if (!(termios->c_cflag & PARODD))
321 ubrlcr |= UBRLCR_EVENPRT;
323 if (port->fifosize > 1)
324 ubrlcr |= UBRLCR_FIFOEN;
326 spin_lock_irqsave(&port->lock, flags);
329 * Update the per-port timeout.
331 uart_update_timeout(port, termios->c_cflag, baud);
333 port->read_status_mask = UARTDR_OVERR;
334 if (termios->c_iflag & INPCK)
335 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
338 * Characters to ignore
340 port->ignore_status_mask = 0;
341 if (termios->c_iflag & IGNPAR)
342 port->ignore_status_mask |= UARTDR_FRMERR | UARTDR_PARERR;
343 if (termios->c_iflag & IGNBRK) {
345 * If we're ignoring parity and break indicators,
346 * ignore overruns to (for real raw support).
348 if (termios->c_iflag & IGNPAR)
349 port->ignore_status_mask |= UARTDR_OVERR;
354 clps_writel(ubrlcr | quot, UBRLCR(port));
356 spin_unlock_irqrestore(&port->lock, flags);
359 static const char *clps711xuart_type(struct uart_port *port)
361 return port->type == PORT_CLPS711X ? "CLPS711x" : NULL;
365 * Configure/autoconfigure the port.
367 static void clps711xuart_config_port(struct uart_port *port, int flags)
369 if (flags & UART_CONFIG_TYPE)
370 port->type = PORT_CLPS711X;
373 static void clps711xuart_release_port(struct uart_port *port)
377 static int clps711xuart_request_port(struct uart_port *port)
382 static struct uart_ops clps711x_pops = {
383 .tx_empty = clps711xuart_tx_empty,
384 .set_mctrl = clps711xuart_set_mctrl_null,
385 .get_mctrl = clps711xuart_get_mctrl,
386 .stop_tx = clps711xuart_stop_tx,
387 .start_tx = clps711xuart_start_tx,
388 .stop_rx = clps711xuart_stop_rx,
389 .enable_ms = clps711xuart_enable_ms,
390 .break_ctl = clps711xuart_break_ctl,
391 .startup = clps711xuart_startup,
392 .shutdown = clps711xuart_shutdown,
393 .set_termios = clps711xuart_set_termios,
394 .type = clps711xuart_type,
395 .config_port = clps711xuart_config_port,
396 .release_port = clps711xuart_release_port,
397 .request_port = clps711xuart_request_port,
400 static struct uart_port clps711x_ports[UART_NR] = {
403 .irq = IRQ_UTXINT1, /* IRQ_URXINT1, IRQ_UMSINT */
406 .ops = &clps711x_pops,
408 .flags = UPF_BOOT_AUTOCONF,
412 .irq = IRQ_UTXINT2, /* IRQ_URXINT2 */
415 .ops = &clps711x_pops,
417 .flags = UPF_BOOT_AUTOCONF,
421 #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
422 static void clps711xuart_console_putchar(struct uart_port *port, int ch)
424 while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF)
426 clps_writel(ch, UARTDR(port));
430 * Print a string to the serial port trying not to disturb
431 * any possible real use of the port...
433 * The console_lock must be held when we get here.
435 * Note that this is called with interrupts already disabled
438 clps711xuart_console_write(struct console *co, const char *s,
441 struct uart_port *port = clps711x_ports + co->index;
442 unsigned int status, syscon;
445 * Ensure that the port is enabled.
447 syscon = clps_readl(SYSCON(port));
448 clps_writel(syscon | SYSCON_UARTEN, SYSCON(port));
450 uart_console_write(port, s, count, clps711xuart_console_putchar);
453 * Finally, wait for transmitter to become empty
454 * and restore the uart state.
457 status = clps_readl(SYSFLG(port));
458 } while (status & SYSFLG_UBUSY);
460 clps_writel(syscon, SYSCON(port));
464 clps711xuart_console_get_options(struct uart_port *port, int *baud,
465 int *parity, int *bits)
467 if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) {
468 unsigned int ubrlcr, quot;
470 ubrlcr = clps_readl(UBRLCR(port));
473 if (ubrlcr & UBRLCR_PRTEN) {
474 if (ubrlcr & UBRLCR_EVENPRT)
480 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
485 quot = ubrlcr & UBRLCR_BAUD_MASK;
486 *baud = port->uartclk / (16 * (quot + 1));
490 static int __init clps711xuart_console_setup(struct console *co, char *options)
492 struct uart_port *port;
499 * Check whether an invalid uart number has been specified, and
500 * if so, search for the first available port that does have
503 port = uart_get_console(clps711x_ports, UART_NR, co);
506 uart_parse_options(options, &baud, &parity, &bits, &flow);
508 clps711xuart_console_get_options(port, &baud, &parity, &bits);
510 return uart_set_options(port, co, baud, parity, bits, flow);
513 static struct uart_driver clps711x_reg;
514 static struct console clps711x_console = {
516 .write = clps711xuart_console_write,
517 .device = uart_console_device,
518 .setup = clps711xuart_console_setup,
519 .flags = CON_PRINTBUFFER,
521 .data = &clps711x_reg,
524 static int __init clps711xuart_console_init(void)
526 register_console(&clps711x_console);
529 console_initcall(clps711xuart_console_init);
531 #define CLPS711X_CONSOLE &clps711x_console
533 #define CLPS711X_CONSOLE NULL
536 static struct uart_driver clps711x_reg = {
537 .driver_name = "ttyCL",
539 .major = SERIAL_CLPS711X_MAJOR,
540 .minor = SERIAL_CLPS711X_MINOR,
543 .cons = CLPS711X_CONSOLE,
546 static int __init clps711xuart_init(void)
550 printk(KERN_INFO "Serial: CLPS711x driver\n");
552 ret = uart_register_driver(&clps711x_reg);
556 for (i = 0; i < UART_NR; i++)
557 uart_add_one_port(&clps711x_reg, &clps711x_ports[i]);
562 static void __exit clps711xuart_exit(void)
566 for (i = 0; i < UART_NR; i++)
567 uart_remove_one_port(&clps711x_reg, &clps711x_ports[i]);
569 uart_unregister_driver(&clps711x_reg);
572 module_init(clps711xuart_init);
573 module_exit(clps711xuart_exit);
575 MODULE_AUTHOR("Deep Blue Solutions Ltd");
576 MODULE_DESCRIPTION("CLPS-711x generic serial driver");
577 MODULE_LICENSE("GPL");
578 MODULE_ALIAS_CHARDEV(SERIAL_CLPS711X_MAJOR, SERIAL_CLPS711X_MINOR);