More meth updates.
[linux-2.6/linux-mips.git] / drivers / serial / 21285.c
blob1ed0bfcfa1e48a23fd8a2d719f4e2cf08006fb26
1 /*
2 * linux/drivers/char/21285.c
4 * Driver for the serial port on the 21285 StrongArm-110 core logic chip.
6 * Based on drivers/char/serial.c
8 * $Id: 21285.c,v 1.37 2002/07/28 10:03:27 rmk Exp $
9 */
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/tty.h>
13 #include <linux/ioport.h>
14 #include <linux/init.h>
15 #include <linux/console.h>
16 #include <linux/serial_core.h>
17 #include <linux/serial.h>
19 #include <asm/io.h>
20 #include <asm/irq.h>
21 #include <asm/hardware/dec21285.h>
22 #include <asm/hardware.h>
24 #define BAUD_BASE (mem_fclk_21285/64)
26 #define SERIAL_21285_NAME "ttyFB"
27 #define SERIAL_21285_MAJOR 204
28 #define SERIAL_21285_MINOR 4
30 #define RXSTAT_DUMMY_READ 0x80000000
31 #define RXSTAT_FRAME (1 << 0)
32 #define RXSTAT_PARITY (1 << 1)
33 #define RXSTAT_OVERRUN (1 << 2)
34 #define RXSTAT_ANYERR (RXSTAT_FRAME|RXSTAT_PARITY|RXSTAT_OVERRUN)
36 #define H_UBRLCR_BREAK (1 << 0)
37 #define H_UBRLCR_PARENB (1 << 1)
38 #define H_UBRLCR_PAREVN (1 << 2)
39 #define H_UBRLCR_STOPB (1 << 3)
40 #define H_UBRLCR_FIFO (1 << 4)
42 static const char serial21285_name[] = "Footbridge UART";
44 #define tx_enabled(port) ((port)->unused[0])
45 #define rx_enabled(port) ((port)->unused[1])
48 * The documented expression for selecting the divisor is:
49 * BAUD_BASE / baud - 1
50 * However, typically BAUD_BASE is not divisible by baud, so
51 * we want to select the divisor that gives us the minimum
52 * error. Therefore, we want:
53 * int(BAUD_BASE / baud - 0.5) ->
54 * int(BAUD_BASE / baud - (baud >> 1) / baud) ->
55 * int((BAUD_BASE - (baud >> 1)) / baud)
58 static void
59 serial21285_stop_tx(struct uart_port *port, unsigned int tty_stop)
61 if (tx_enabled(port)) {
62 disable_irq(IRQ_CONTX);
63 tx_enabled(port) = 0;
67 static void
68 serial21285_start_tx(struct uart_port *port, unsigned int tty_start)
70 if (!tx_enabled(port)) {
71 enable_irq(IRQ_CONTX);
72 tx_enabled(port) = 1;
76 static void serial21285_stop_rx(struct uart_port *port)
78 if (rx_enabled(port)) {
79 disable_irq(IRQ_CONRX);
80 rx_enabled(port) = 0;
84 static void serial21285_enable_ms(struct uart_port *port)
88 static irqreturn_t serial21285_rx_chars(int irq, void *dev_id, struct pt_regs *regs)
90 struct uart_port *port = dev_id;
91 struct tty_struct *tty = port->info->tty;
92 unsigned int status, ch, rxs, max_count = 256;
94 status = *CSR_UARTFLG;
95 while (!(status & 0x10) && max_count--) {
96 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
97 tty->flip.work.func((void *)tty);
98 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
99 printk(KERN_WARNING "TTY_DONT_FLIP set\n");
100 goto out;
104 ch = *CSR_UARTDR;
106 *tty->flip.char_buf_ptr = ch;
107 *tty->flip.flag_buf_ptr = TTY_NORMAL;
108 port->icount.rx++;
110 rxs = *CSR_RXSTAT | RXSTAT_DUMMY_READ;
111 if (rxs & RXSTAT_ANYERR) {
112 if (rxs & RXSTAT_PARITY)
113 port->icount.parity++;
114 else if (rxs & RXSTAT_FRAME)
115 port->icount.frame++;
116 if (rxs & RXSTAT_OVERRUN)
117 port->icount.overrun++;
119 rxs &= port->read_status_mask;
121 if (rxs & RXSTAT_PARITY)
122 *tty->flip.flag_buf_ptr = TTY_PARITY;
123 else if (rxs & RXSTAT_FRAME)
124 *tty->flip.flag_buf_ptr = TTY_FRAME;
127 if ((rxs & port->ignore_status_mask) == 0) {
128 tty->flip.flag_buf_ptr++;
129 tty->flip.char_buf_ptr++;
130 tty->flip.count++;
132 if ((rxs & RXSTAT_OVERRUN) &&
133 tty->flip.count < TTY_FLIPBUF_SIZE) {
135 * Overrun is special, since it's reported
136 * immediately, and doesn't affect the current
137 * character.
139 *tty->flip.char_buf_ptr++ = 0;
140 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
141 tty->flip.count++;
143 status = *CSR_UARTFLG;
145 tty_flip_buffer_push(tty);
147 out:
148 return IRQ_HANDLED;
151 static irqreturn_t serial21285_tx_chars(int irq, void *dev_id, struct pt_regs *regs)
153 struct uart_port *port = dev_id;
154 struct circ_buf *xmit = &port->info->xmit;
155 int count = 256;
157 if (port->x_char) {
158 *CSR_UARTDR = port->x_char;
159 port->icount.tx++;
160 port->x_char = 0;
161 goto out;
163 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
164 serial21285_stop_tx(port, 0);
165 goto out;
168 do {
169 *CSR_UARTDR = xmit->buf[xmit->tail];
170 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
171 port->icount.tx++;
172 if (uart_circ_empty(xmit))
173 break;
174 } while (--count > 0 && !(*CSR_UARTFLG & 0x20));
176 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
177 uart_write_wakeup(port);
179 if (uart_circ_empty(xmit))
180 serial21285_stop_tx(port, 0);
182 out:
183 return IRQ_HANDLED;
186 static unsigned int serial21285_tx_empty(struct uart_port *port)
188 return (*CSR_UARTFLG & 8) ? 0 : TIOCSER_TEMT;
191 /* no modem control lines */
192 static unsigned int serial21285_get_mctrl(struct uart_port *port)
194 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
197 static void serial21285_set_mctrl(struct uart_port *port, unsigned int mctrl)
201 static void serial21285_break_ctl(struct uart_port *port, int break_state)
203 unsigned long flags;
204 unsigned int h_lcr;
206 spin_lock_irqsave(&port->lock, flags);
207 h_lcr = *CSR_H_UBRLCR;
208 if (break_state)
209 h_lcr |= H_UBRLCR_BREAK;
210 else
211 h_lcr &= ~H_UBRLCR_BREAK;
212 *CSR_H_UBRLCR = h_lcr;
213 spin_unlock_irqrestore(&port->lock, flags);
216 static int serial21285_startup(struct uart_port *port)
218 int ret;
220 tx_enabled(port) = 1;
221 rx_enabled(port) = 1;
223 ret = request_irq(IRQ_CONRX, serial21285_rx_chars, 0,
224 serial21285_name, port);
225 if (ret == 0) {
226 ret = request_irq(IRQ_CONTX, serial21285_tx_chars, 0,
227 serial21285_name, port);
228 if (ret)
229 free_irq(IRQ_CONRX, port);
232 return ret;
235 static void serial21285_shutdown(struct uart_port *port)
237 free_irq(IRQ_CONTX, port);
238 free_irq(IRQ_CONRX, port);
241 static void
242 serial21285_set_termios(struct uart_port *port, struct termios *termios,
243 struct termios *old)
245 unsigned long flags;
246 unsigned int baud, quot, h_lcr;
249 * We don't support modem control lines.
251 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
252 termios->c_cflag |= CLOCAL;
255 * We don't support BREAK character recognition.
257 termios->c_iflag &= ~(IGNBRK | BRKINT);
260 * Ask the core to calculate the divisor for us.
262 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
263 quot = uart_get_divisor(port, baud);
265 switch (termios->c_cflag & CSIZE) {
266 case CS5:
267 h_lcr = 0x00;
268 break;
269 case CS6:
270 h_lcr = 0x20;
271 break;
272 case CS7:
273 h_lcr = 0x40;
274 break;
275 default: /* CS8 */
276 h_lcr = 0x60;
277 break;
280 if (termios->c_cflag & CSTOPB)
281 h_lcr |= H_UBRLCR_STOPB;
282 if (termios->c_cflag & PARENB) {
283 h_lcr |= H_UBRLCR_PARENB;
284 if (!(termios->c_cflag & PARODD))
285 h_lcr |= H_UBRLCR_PAREVN;
288 if (port->fifosize)
289 h_lcr |= H_UBRLCR_FIFO;
291 spin_lock_irqsave(&port->lock, flags);
294 * Update the per-port timeout.
296 uart_update_timeout(port, termios->c_cflag, baud);
299 * Which character status flags are we interested in?
301 port->read_status_mask = RXSTAT_OVERRUN;
302 if (termios->c_iflag & INPCK)
303 port->read_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
306 * Which character status flags should we ignore?
308 port->ignore_status_mask = 0;
309 if (termios->c_iflag & IGNPAR)
310 port->ignore_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
311 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
312 port->ignore_status_mask |= RXSTAT_OVERRUN;
315 * Ignore all characters if CREAD is not set.
317 if ((termios->c_cflag & CREAD) == 0)
318 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
320 quot -= 1;
322 *CSR_UARTCON = 0;
323 *CSR_L_UBRLCR = quot & 0xff;
324 *CSR_M_UBRLCR = (quot >> 8) & 0x0f;
325 *CSR_H_UBRLCR = h_lcr;
326 *CSR_UARTCON = 1;
328 spin_unlock_irqrestore(&port->lock, flags);
331 static const char *serial21285_type(struct uart_port *port)
333 return port->type == PORT_21285 ? "DC21285" : NULL;
336 static void serial21285_release_port(struct uart_port *port)
338 release_mem_region(port->mapbase, 32);
341 static int serial21285_request_port(struct uart_port *port)
343 return request_mem_region(port->mapbase, 32, serial21285_name)
344 != NULL ? 0 : -EBUSY;
347 static void serial21285_config_port(struct uart_port *port, int flags)
349 if (flags & UART_CONFIG_TYPE && serial21285_request_port(port) == 0)
350 port->type = PORT_21285;
354 * verify the new serial_struct (for TIOCSSERIAL).
356 static int serial21285_verify_port(struct uart_port *port, struct serial_struct *ser)
358 int ret = 0;
359 if (ser->type != PORT_UNKNOWN && ser->type != PORT_21285)
360 ret = -EINVAL;
361 if (ser->irq != NO_IRQ)
362 ret = -EINVAL;
363 if (ser->baud_base != port->uartclk / 16)
364 ret = -EINVAL;
365 return ret;
368 static struct uart_ops serial21285_ops = {
369 .tx_empty = serial21285_tx_empty,
370 .get_mctrl = serial21285_get_mctrl,
371 .set_mctrl = serial21285_set_mctrl,
372 .stop_tx = serial21285_stop_tx,
373 .start_tx = serial21285_start_tx,
374 .stop_rx = serial21285_stop_rx,
375 .enable_ms = serial21285_enable_ms,
376 .break_ctl = serial21285_break_ctl,
377 .startup = serial21285_startup,
378 .shutdown = serial21285_shutdown,
379 .set_termios = serial21285_set_termios,
380 .type = serial21285_type,
381 .release_port = serial21285_release_port,
382 .request_port = serial21285_request_port,
383 .config_port = serial21285_config_port,
384 .verify_port = serial21285_verify_port,
387 static struct uart_port serial21285_port = {
388 .membase = 0,
389 .mapbase = 0x42000160,
390 .iotype = SERIAL_IO_MEM,
391 .irq = NO_IRQ,
392 .uartclk = 0,
393 .fifosize = 16,
394 .ops = &serial21285_ops,
395 .flags = ASYNC_BOOT_AUTOCONF,
398 static void serial21285_setup_ports(void)
400 serial21285_port.uartclk = mem_fclk_21285 / 4;
403 #ifdef CONFIG_SERIAL_21285_CONSOLE
405 static void
406 serial21285_console_write(struct console *co, const char *s,
407 unsigned int count)
409 int i;
411 for (i = 0; i < count; i++) {
412 while (*CSR_UARTFLG & 0x20)
413 barrier();
414 *CSR_UARTDR = s[i];
415 if (s[i] == '\n') {
416 while (*CSR_UARTFLG & 0x20)
417 barrier();
418 *CSR_UARTDR = '\r';
423 static void __init
424 serial21285_get_options(struct uart_port *port, int *baud,
425 int *parity, int *bits)
427 if (*CSR_UARTCON == 1) {
428 unsigned int tmp;
430 tmp = *CSR_H_UBRLCR;
431 switch (tmp & 0x60) {
432 case 0x00:
433 *bits = 5;
434 break;
435 case 0x20:
436 *bits = 6;
437 break;
438 case 0x40:
439 *bits = 7;
440 break;
441 default:
442 case 0x60:
443 *bits = 8;
444 break;
447 if (tmp & H_UBRLCR_PARENB) {
448 *parity = 'o';
449 if (tmp & H_UBRLCR_PAREVN)
450 *parity = 'e';
453 tmp = *CSR_L_UBRLCR | (*CSR_M_UBRLCR << 8);
455 *baud = port->uartclk / (16 * (tmp + 1));
459 static int __init serial21285_console_setup(struct console *co, char *options)
461 struct uart_port *port = &serial21285_port;
462 int baud = 9600;
463 int bits = 8;
464 int parity = 'n';
465 int flow = 'n';
467 if (machine_is_personal_server())
468 baud = 57600;
471 * Check whether an invalid uart number has been specified, and
472 * if so, search for the first available port that does have
473 * console support.
475 if (options)
476 uart_parse_options(options, &baud, &parity, &bits, &flow);
477 else
478 serial21285_get_options(port, &baud, &parity, &bits);
480 return uart_set_options(port, co, baud, parity, bits, flow);
483 extern struct uart_driver serial21285_reg;
484 #ifdef CONFIG_SERIAL_21285_OLD
485 static struct console serial21285_old_cons =
487 .name = SERIAL_21285_OLD_NAME,
488 .write = serial21285_console_write,
489 .device = uart_console_device,
490 .setup = serial21285_console_setup,
491 .flags = CON_PRINTBUFFER,
492 .index = -1,
493 .data = &serial21285_reg,
495 #endif
497 static struct console serial21285_console =
499 .name = SERIAL_21285_NAME,
500 .write = serial21285_console_write,
501 .device = uart_console_device,
502 .setup = serial21285_console_setup,
503 .flags = CON_PRINTBUFFER,
504 .index = -1,
505 .data = &serial21285_reg,
508 static int __init rs285_console_init(void)
510 serial21285_setup_ports();
511 register_console(&serial21285_console);
512 return 0;
514 console_initcall(rs285_console_init);
516 #define SERIAL_21285_CONSOLE &serial21285_console
517 #else
518 #define SERIAL_21285_CONSOLE NULL
519 #endif
521 static struct uart_driver serial21285_reg = {
522 .owner = THIS_MODULE,
523 .driver_name = "ttyFB",
524 .dev_name = "ttyFB",
525 .major = SERIAL_21285_MAJOR,
526 .minor = SERIAL_21285_MINOR,
527 .nr = 1,
528 .cons = SERIAL_21285_CONSOLE,
531 static int __init serial21285_init(void)
533 int ret;
535 printk(KERN_INFO "Serial: 21285 driver $Revision: 1.37 $\n");
537 serial21285_setup_ports();
539 ret = uart_register_driver(&serial21285_reg);
540 if (ret == 0)
541 uart_add_one_port(&serial21285_reg, &serial21285_port);
543 return ret;
546 static void __exit serial21285_exit(void)
548 uart_remove_one_port(&serial21285_reg, &serial21285_port);
549 uart_unregister_driver(&serial21285_reg);
552 module_init(serial21285_init);
553 module_exit(serial21285_exit);
555 MODULE_LICENSE("GPL");
556 MODULE_DESCRIPTION("Intel Footbridge (21285) serial driver $Revision: 1.37 $");