More meth updates.
[linux-2.6/linux-mips.git] / drivers / serial / clps711x.c
bloba32dae486b77b3e733997396e92163c4ef979781
1 /*
2 * linux/drivers/char/clps711x.c
4 * Driver for CLPS711x 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: clps711x.c,v 1.42 2002/07/28 10:03:28 rmk Exp $
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/tty.h>
31 #include <linux/ioport.h>
32 #include <linux/init.h>
33 #include <linux/serial.h>
34 #include <linux/console.h>
35 #include <linux/sysrq.h>
36 #include <linux/spinlock.h>
38 #include <asm/hardware.h>
39 #include <asm/io.h>
40 #include <asm/irq.h>
42 #if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
43 #define SUPPORT_SYSRQ
44 #endif
46 #include <linux/serial_core.h>
48 #include <asm/hardware/clps7111.h>
50 #define UART_NR 2
52 #ifndef CONFIG_SERIAL_CLPS711X_OLD_NAME
53 #define SERIAL_CLPS711X_MAJOR 204
54 #define SERIAL_CLPS711X_MINOR 40
55 #define SERIAL_CLPS711X_NR UART_NR
57 #else
58 #warning The old names/device number for this driver if compatabity is needed
59 #define SERIAL_CLPS711X_MAJOR 204
60 #define SERIAL_CLPS711X_MINOR 16
61 #define SERIAL_CLPS711X_NR UART_NR
63 #endif
66 * We use the relevant SYSCON register as a base address for these ports.
68 #define UBRLCR(port) ((port)->iobase + UBRLCR1 - SYSCON1)
69 #define UARTDR(port) ((port)->iobase + UARTDR1 - SYSCON1)
70 #define SYSFLG(port) ((port)->iobase + SYSFLG1 - SYSCON1)
71 #define SYSCON(port) ((port)->iobase + SYSCON1 - SYSCON1)
73 #define TX_IRQ(port) ((port)->irq)
74 #define RX_IRQ(port) ((port)->irq + 1)
76 #define UART_ANY_ERR (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR)
78 #define tx_enabled(port) ((port)->unused[0])
80 static void
81 clps711xuart_stop_tx(struct uart_port *port, unsigned int tty_stop)
83 if (tx_enabled(port)) {
84 disable_irq(TX_IRQ(port));
85 tx_enabled(port) = 0;
89 static void
90 clps711xuart_start_tx(struct uart_port *port, unsigned int tty_start)
92 if (!tx_enabled(port)) {
93 enable_irq(TX_IRQ(port));
94 tx_enabled(port) = 1;
98 static void clps711xuart_stop_rx(struct uart_port *port)
100 disable_irq(RX_IRQ(port));
103 static void clps711xuart_enable_ms(struct uart_port *port)
107 static void clps711xuart_int_rx(int irq, void *dev_id, struct pt_regs *regs)
109 struct uart_port *port = dev_id;
110 struct tty_struct *tty = port->info->tty;
111 unsigned int status, ch, flg, ignored = 0;
113 status = clps_readl(SYSFLG(port));
114 while (!(status & SYSFLG_URXFE)) {
115 ch = clps_readl(UARTDR(port));
117 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
118 goto ignore_char;
119 port->icount.rx++;
121 flg = TTY_NORMAL;
124 * Note that the error handling code is
125 * out of the main execution path
127 if (ch & UART_ANY_ERR)
128 goto handle_error;
130 if (uart_handle_sysrq_char(port, ch, regs))
131 goto ignore_char;
133 error_return:
134 *tty->flip.flag_buf_ptr++ = flg;
135 *tty->flip.char_buf_ptr++ = ch;
136 tty->flip.count++;
137 ignore_char:
138 status = clps_readl(SYSFLG(port));
140 out:
141 tty_flip_buffer_push(tty);
142 return;
144 handle_error:
145 if (ch & UARTDR_PARERR)
146 port->icount.parity++;
147 else if (ch & UARTDR_FRMERR)
148 port->icount.frame++;
149 if (ch & UARTDR_OVERR)
150 port->icount.overrun++;
152 if (ch & port->ignore_status_mask) {
153 if (++ignored > 100)
154 goto out;
155 goto ignore_char;
157 ch &= port->read_status_mask;
159 if (ch & UARTDR_PARERR)
160 flg = TTY_PARITY;
161 else if (ch & UARTDR_FRMERR)
162 flg = TTY_FRAME;
164 if (ch & UARTDR_OVERR) {
166 * CHECK: does overrun affect the current character?
167 * ASSUMPTION: it does not.
169 *tty->flip.flag_buf_ptr++ = flg;
170 *tty->flip.char_buf_ptr++ = ch;
171 tty->flip.count++;
172 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
173 goto ignore_char;
174 ch = 0;
175 flg = TTY_OVERRUN;
177 #ifdef SUPPORT_SYSRQ
178 port->sysrq = 0;
179 #endif
180 goto error_return;
183 static void clps711xuart_int_tx(int irq, void *dev_id, struct pt_regs *regs)
185 struct uart_port *port = dev_id;
186 struct circ_buf *xmit = &port->info->xmit;
187 int count;
189 if (port->x_char) {
190 clps_writel(port->x_char, UARTDR(port));
191 port->icount.tx++;
192 port->x_char = 0;
193 return;
195 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
196 clps711xuart_stop_tx(port, 0);
197 return;
200 count = port->fifosize >> 1;
201 do {
202 clps_writel(xmit->buf[xmit->tail], UARTDR(port));
203 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
204 port->icount.tx++;
205 if (uart_circ_empty(xmit))
206 break;
207 } while (--count > 0);
209 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
210 uart_write_wakeup(port);
212 if (uart_circ_empty(xmit))
213 clps711xuart_stop_tx(port, 0);
216 static unsigned int clps711xuart_tx_empty(struct uart_port *port)
218 unsigned int status = clps_readl(SYSFLG(port));
219 return status & SYSFLG_UBUSY ? 0 : TIOCSER_TEMT;
222 static unsigned int clps711xuart_get_mctrl(struct uart_port *port)
224 unsigned int port_addr;
225 unsigned int result = 0;
226 unsigned int status;
228 port_addr = SYSFLG(port);
229 if (port_addr == SYSFLG1) {
230 status = clps_readl(SYSFLG1);
231 if (status & SYSFLG1_DCD)
232 result |= TIOCM_CAR;
233 if (status & SYSFLG1_DSR)
234 result |= TIOCM_DSR;
235 if (status & SYSFLG1_CTS)
236 result |= TIOCM_CTS;
239 return result;
242 static void
243 clps711xuart_set_mctrl_null(struct uart_port *port, unsigned int mctrl)
247 static void clps711xuart_break_ctl(struct uart_port *port, int break_state)
249 unsigned long flags;
250 unsigned int ubrlcr;
252 spin_lock_irqsave(&port->lock, flags);
253 ubrlcr = clps_readl(UBRLCR(port));
254 if (break_state == -1)
255 ubrlcr |= UBRLCR_BREAK;
256 else
257 ubrlcr &= ~UBRLCR_BREAK;
258 clps_writel(ubrlcr, UBRLCR(port));
259 spin_unlock_irqrestore(&port->lock, flags);
262 static int clps711xuart_startup(struct uart_port *port)
264 unsigned int syscon;
265 int retval;
267 tx_enabled(port) = 1;
270 * Allocate the IRQs
272 retval = request_irq(TX_IRQ(port), clps711xuart_int_tx, 0,
273 "clps711xuart_tx", port);
274 if (retval)
275 return retval;
277 retval = request_irq(RX_IRQ(port), clps711xuart_int_rx, 0,
278 "clps711xuart_rx", port);
279 if (retval) {
280 free_irq(TX_IRQ(port), port);
281 return retval;
285 * enable the port
287 syscon = clps_readl(SYSCON(port));
288 syscon |= SYSCON_UARTEN;
289 clps_writel(syscon, SYSCON(port));
291 return 0;
294 static void clps711xuart_shutdown(struct uart_port *port)
296 unsigned int ubrlcr, syscon;
299 * Free the interrupt
301 free_irq(TX_IRQ(port), port); /* TX interrupt */
302 free_irq(RX_IRQ(port), port); /* RX interrupt */
305 * disable the port
307 syscon = clps_readl(SYSCON(port));
308 syscon &= ~SYSCON_UARTEN;
309 clps_writel(syscon, SYSCON(port));
312 * disable break condition and fifos
314 ubrlcr = clps_readl(UBRLCR(port));
315 ubrlcr &= ~(UBRLCR_FIFOEN | UBRLCR_BREAK);
316 clps_writel(ubrlcr, UBRLCR(port));
319 static void
320 clps711xuart_set_termios(struct uart_port *port, struct termios *termios,
321 struct termios *old)
323 unsigned int ubrlcr, baud, quot;
324 unsigned long flags;
327 * We don't implement CREAD.
329 termios->c_cflag |= CREAD;
332 * Ask the core to calculate the divisor for us.
334 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
335 quot = uart_get_divisor(port, baud);
337 switch (termios->c_cflag & CSIZE) {
338 case CS5:
339 ubrlcr = UBRLCR_WRDLEN5;
340 break;
341 case CS6:
342 ubrlcr = UBRLCR_WRDLEN6;
343 break;
344 case CS7:
345 ubrlcr = UBRLCR_WRDLEN7;
346 break;
347 default: // CS8
348 ubrlcr = UBRLCR_WRDLEN8;
349 break;
351 if (termios->c_cflag & CSTOPB)
352 ubrlcr |= UBRLCR_XSTOP;
353 if (termios->c_cflag & PARENB) {
354 ubrlcr |= UBRLCR_PRTEN;
355 if (!(termios->c_cflag & PARODD))
356 ubrlcr |= UBRLCR_EVENPRT;
358 if (port->fifosize > 1)
359 ubrlcr |= UBRLCR_FIFOEN;
361 spin_lock_irqsave(&port->lock, flags);
364 * Update the per-port timeout.
366 uart_update_timeout(port, termios->c_cflag, baud);
368 port->read_status_mask = UARTDR_OVERR;
369 if (termios->c_iflag & INPCK)
370 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
373 * Characters to ignore
375 port->ignore_status_mask = 0;
376 if (termios->c_iflag & IGNPAR)
377 port->ignore_status_mask |= UARTDR_FRMERR | UARTDR_PARERR;
378 if (termios->c_iflag & IGNBRK) {
380 * If we're ignoring parity and break indicators,
381 * ignore overruns to (for real raw support).
383 if (termios->c_iflag & IGNPAR)
384 port->ignore_status_mask |= UARTDR_OVERR;
387 quot -= 1;
389 clps_writel(ubrlcr | quot, UBRLCR(port));
391 spin_unlock_irqrestore(&port->lock, flags);
394 static const char *clps711xuart_type(struct uart_port *port)
396 return port->type == PORT_CLPS711X ? "CLPS711x" : NULL;
400 * Configure/autoconfigure the port.
402 static void clps711xuart_config_port(struct uart_port *port, int flags)
404 if (flags & UART_CONFIG_TYPE)
405 port->type = PORT_CLPS711X;
408 static void clps711xuart_release_port(struct uart_port *port)
412 static int clps711xuart_request_port(struct uart_port *port)
414 return 0;
417 static struct uart_ops clps711x_pops = {
418 .tx_empty = clps711xuart_tx_empty,
419 .set_mctrl = clps711xuart_set_mctrl_null,
420 .get_mctrl = clps711xuart_get_mctrl,
421 .stop_tx = clps711xuart_stop_tx,
422 .start_tx = clps711xuart_start_tx,
423 .stop_rx = clps711xuart_stop_rx,
424 .enable_ms = clps711xuart_enable_ms,
425 .break_ctl = clps711xuart_break_ctl,
426 .startup = clps711xuart_startup,
427 .shutdown = clps711xuart_shutdown,
428 .set_termios = clps711xuart_set_termios,
429 .type = clps711xuart_type,
430 .config_port = clps711xuart_config_port,
431 .release_port = clps711xuart_release_port,
432 .request_port = clps711xuart_request_port,
435 static struct uart_port clps711x_ports[UART_NR] = {
437 .iobase = SYSCON1,
438 .irq = IRQ_UTXINT1, /* IRQ_URXINT1, IRQ_UMSINT */
439 .uartclk = 3686400,
440 .fifosize = 16,
441 .ops = &clps711x_pops,
442 .line = 0,
443 .flags = ASYNC_BOOT_AUTOCONF,
446 .iobase = SYSCON2,
447 .irq = IRQ_UTXINT2, /* IRQ_URXINT2 */
448 .uartclk = 3686400,
449 .fifosize = 16,
450 .ops = &clps711x_pops,
451 .line = 1,
452 .flags = ASYNC_BOOT_AUTOCONF,
456 #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
458 * Print a string to the serial port trying not to disturb
459 * any possible real use of the port...
461 * The console_lock must be held when we get here.
463 * Note that this is called with interrupts already disabled
465 static void
466 clps711xuart_console_write(struct console *co, const char *s,
467 unsigned int count)
469 struct uart_port *port = clps711x_ports + co->index;
470 unsigned int status, syscon;
471 int i;
474 * Ensure that the port is enabled.
476 syscon = clps_readl(SYSCON(port));
477 clps_writel(syscon | SYSCON_UARTEN, SYSCON(port));
480 * Now, do each character
482 for (i = 0; i < count; i++) {
483 do {
484 status = clps_readl(SYSFLG(port));
485 } while (status & SYSFLG_UTXFF);
486 clps_writel(s[i], UARTDR(port));
487 if (s[i] == '\n') {
488 do {
489 status = clps_readl(SYSFLG(port));
490 } while (status & SYSFLG_UTXFF);
491 clps_writel('\r', UARTDR(port));
496 * Finally, wait for transmitter to become empty
497 * and restore the uart state.
499 do {
500 status = clps_readl(SYSFLG(port));
501 } while (status & SYSFLG_UBUSY);
503 clps_writel(syscon, SYSCON(port));
506 static void __init
507 clps711xuart_console_get_options(struct uart_port *port, int *baud,
508 int *parity, int *bits)
510 if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) {
511 unsigned int ubrlcr, quot;
513 ubrlcr = clps_readl(UBRLCR(port));
515 *parity = 'n';
516 if (ubrlcr & UBRLCR_PRTEN) {
517 if (ubrlcr & UBRLCR_EVENPRT)
518 *parity = 'e';
519 else
520 *parity = 'o';
523 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
524 *bits = 7;
525 else
526 *bits = 8;
528 quot = ubrlcr & UBRLCR_BAUD_MASK;
529 *baud = port->uartclk / (16 * (quot + 1));
533 static int __init clps711xuart_console_setup(struct console *co, char *options)
535 struct uart_port *port;
536 int baud = 38400;
537 int bits = 8;
538 int parity = 'n';
539 int flow = 'n';
542 * Check whether an invalid uart number has been specified, and
543 * if so, search for the first available port that does have
544 * console support.
546 port = uart_get_console(clps711x_ports, UART_NR, co);
548 if (options)
549 uart_parse_options(options, &baud, &parity, &bits, &flow);
550 else
551 clps711xuart_console_get_options(port, &baud, &parity, &bits);
553 return uart_set_options(port, co, baud, parity, bits, flow);
556 extern struct uart_driver clps711x_reg;
557 static struct console clps711x_console = {
558 .name = "ttyCL",
559 .write = clps711xuart_console_write,
560 .device = uart_console_device,
561 .setup = clps711xuart_console_setup,
562 .flags = CON_PRINTBUFFER,
563 .index = -1,
564 .data = &clps711x_reg,
567 static int __init clps711xuart_console_init(void)
569 register_console(&clps711x_console);
570 return 0;
572 console_initcall(clps711xuart_console_init);
574 #define CLPS711X_CONSOLE &clps711x_console
575 #else
576 #define CLPS711X_CONSOLE NULL
577 #endif
579 static struct uart_driver clps711x_reg = {
580 .driver_name = "ttyCL",
581 .dev_name = "ttyCL",
582 .major = SERIAL_CLPS711X_MAJOR,
583 .minor = SERIAL_CLPS711X_MINOR,
584 .nr = UART_NR,
586 .cons = CLPS711X_CONSOLE,
589 static int __init clps711xuart_init(void)
591 int ret, i;
593 printk(KERN_INFO "Serial: CLPS711x driver $Revision: 1.42 $\n");
595 ret = uart_register_driver(&clps711x_reg);
596 if (ret)
597 return ret;
599 for (i = 0; i < UART_NR; i++)
600 uart_add_one_port(&clps711x_reg, &clps711x_ports[i]);
602 return 0;
605 static void __exit clps711xuart_exit(void)
607 int i;
609 for (i = 0; i < UART_NR; i++)
610 uart_remove_one_port(&clps711x_reg, &clps711x_ports[i]);
612 uart_unregister_driver(&clps711x_reg);
615 module_init(clps711xuart_init);
616 module_exit(clps711xuart_exit);
618 MODULE_AUTHOR("Deep Blue Solutions Ltd");
619 MODULE_DESCRIPTION("CLPS-711x generic serial driver $Revision: 1.42 $");
620 MODULE_LICENSE("GPL");