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 $
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/device.h>
17 #include <linux/tty_flip.h>
18 #include <linux/serial_core.h>
19 #include <linux/serial.h>
23 #include <asm/mach-types.h>
24 #include <asm/hardware/dec21285.h>
25 #include <asm/hardware.h>
27 #define BAUD_BASE (mem_fclk_21285/64)
29 #define SERIAL_21285_NAME "ttyFB"
30 #define SERIAL_21285_MAJOR 204
31 #define SERIAL_21285_MINOR 4
33 #define RXSTAT_DUMMY_READ 0x80000000
34 #define RXSTAT_FRAME (1 << 0)
35 #define RXSTAT_PARITY (1 << 1)
36 #define RXSTAT_OVERRUN (1 << 2)
37 #define RXSTAT_ANYERR (RXSTAT_FRAME|RXSTAT_PARITY|RXSTAT_OVERRUN)
39 #define H_UBRLCR_BREAK (1 << 0)
40 #define H_UBRLCR_PARENB (1 << 1)
41 #define H_UBRLCR_PAREVN (1 << 2)
42 #define H_UBRLCR_STOPB (1 << 3)
43 #define H_UBRLCR_FIFO (1 << 4)
45 static const char serial21285_name[] = "Footbridge UART";
47 #define tx_enabled(port) ((port)->unused[0])
48 #define rx_enabled(port) ((port)->unused[1])
51 * The documented expression for selecting the divisor is:
52 * BAUD_BASE / baud - 1
53 * However, typically BAUD_BASE is not divisible by baud, so
54 * we want to select the divisor that gives us the minimum
55 * error. Therefore, we want:
56 * int(BAUD_BASE / baud - 0.5) ->
57 * int(BAUD_BASE / baud - (baud >> 1) / baud) ->
58 * int((BAUD_BASE - (baud >> 1)) / baud)
62 serial21285_stop_tx(struct uart_port *port, unsigned int tty_stop)
64 if (tx_enabled(port)) {
65 disable_irq(IRQ_CONTX);
71 serial21285_start_tx(struct uart_port *port, unsigned int tty_start)
73 if (!tx_enabled(port)) {
74 enable_irq(IRQ_CONTX);
79 static void serial21285_stop_rx(struct uart_port *port)
81 if (rx_enabled(port)) {
82 disable_irq(IRQ_CONRX);
87 static void serial21285_enable_ms(struct uart_port *port)
91 static irqreturn_t serial21285_rx_chars(int irq, void *dev_id, struct pt_regs *regs)
93 struct uart_port *port = dev_id;
94 struct tty_struct *tty = port->info->tty;
95 unsigned int status, ch, flag, rxs, max_count = 256;
97 status = *CSR_UARTFLG;
98 while (!(status & 0x10) && max_count--) {
99 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
100 if (tty->low_latency)
101 tty_flip_buffer_push(tty);
103 * If this failed then we will throw away the
104 * bytes but must do so to clear interrupts
112 rxs = *CSR_RXSTAT | RXSTAT_DUMMY_READ;
113 if (unlikely(rxs & RXSTAT_ANYERR)) {
114 if (rxs & RXSTAT_PARITY)
115 port->icount.parity++;
116 else if (rxs & RXSTAT_FRAME)
117 port->icount.frame++;
118 if (rxs & RXSTAT_OVERRUN)
119 port->icount.overrun++;
121 rxs &= port->read_status_mask;
123 if (rxs & RXSTAT_PARITY)
125 else if (rxs & RXSTAT_FRAME)
129 uart_insert_char(port, rxs, RXSTAT_OVERRUN, ch, flag);
131 status = *CSR_UARTFLG;
133 tty_flip_buffer_push(tty);
138 static irqreturn_t serial21285_tx_chars(int irq, void *dev_id, struct pt_regs *regs)
140 struct uart_port *port = dev_id;
141 struct circ_buf *xmit = &port->info->xmit;
145 *CSR_UARTDR = port->x_char;
150 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
151 serial21285_stop_tx(port, 0);
156 *CSR_UARTDR = xmit->buf[xmit->tail];
157 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
159 if (uart_circ_empty(xmit))
161 } while (--count > 0 && !(*CSR_UARTFLG & 0x20));
163 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
164 uart_write_wakeup(port);
166 if (uart_circ_empty(xmit))
167 serial21285_stop_tx(port, 0);
173 static unsigned int serial21285_tx_empty(struct uart_port *port)
175 return (*CSR_UARTFLG & 8) ? 0 : TIOCSER_TEMT;
178 /* no modem control lines */
179 static unsigned int serial21285_get_mctrl(struct uart_port *port)
181 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
184 static void serial21285_set_mctrl(struct uart_port *port, unsigned int mctrl)
188 static void serial21285_break_ctl(struct uart_port *port, int break_state)
193 spin_lock_irqsave(&port->lock, flags);
194 h_lcr = *CSR_H_UBRLCR;
196 h_lcr |= H_UBRLCR_BREAK;
198 h_lcr &= ~H_UBRLCR_BREAK;
199 *CSR_H_UBRLCR = h_lcr;
200 spin_unlock_irqrestore(&port->lock, flags);
203 static int serial21285_startup(struct uart_port *port)
207 tx_enabled(port) = 1;
208 rx_enabled(port) = 1;
210 ret = request_irq(IRQ_CONRX, serial21285_rx_chars, 0,
211 serial21285_name, port);
213 ret = request_irq(IRQ_CONTX, serial21285_tx_chars, 0,
214 serial21285_name, port);
216 free_irq(IRQ_CONRX, port);
222 static void serial21285_shutdown(struct uart_port *port)
224 free_irq(IRQ_CONTX, port);
225 free_irq(IRQ_CONRX, port);
229 serial21285_set_termios(struct uart_port *port, struct termios *termios,
233 unsigned int baud, quot, h_lcr;
236 * We don't support modem control lines.
238 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
239 termios->c_cflag |= CLOCAL;
242 * We don't support BREAK character recognition.
244 termios->c_iflag &= ~(IGNBRK | BRKINT);
247 * Ask the core to calculate the divisor for us.
249 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
250 quot = uart_get_divisor(port, baud);
252 switch (termios->c_cflag & CSIZE) {
267 if (termios->c_cflag & CSTOPB)
268 h_lcr |= H_UBRLCR_STOPB;
269 if (termios->c_cflag & PARENB) {
270 h_lcr |= H_UBRLCR_PARENB;
271 if (!(termios->c_cflag & PARODD))
272 h_lcr |= H_UBRLCR_PAREVN;
276 h_lcr |= H_UBRLCR_FIFO;
278 spin_lock_irqsave(&port->lock, flags);
281 * Update the per-port timeout.
283 uart_update_timeout(port, termios->c_cflag, baud);
286 * Which character status flags are we interested in?
288 port->read_status_mask = RXSTAT_OVERRUN;
289 if (termios->c_iflag & INPCK)
290 port->read_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
293 * Which character status flags should we ignore?
295 port->ignore_status_mask = 0;
296 if (termios->c_iflag & IGNPAR)
297 port->ignore_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
298 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
299 port->ignore_status_mask |= RXSTAT_OVERRUN;
302 * Ignore all characters if CREAD is not set.
304 if ((termios->c_cflag & CREAD) == 0)
305 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
310 *CSR_L_UBRLCR = quot & 0xff;
311 *CSR_M_UBRLCR = (quot >> 8) & 0x0f;
312 *CSR_H_UBRLCR = h_lcr;
315 spin_unlock_irqrestore(&port->lock, flags);
318 static const char *serial21285_type(struct uart_port *port)
320 return port->type == PORT_21285 ? "DC21285" : NULL;
323 static void serial21285_release_port(struct uart_port *port)
325 release_mem_region(port->mapbase, 32);
328 static int serial21285_request_port(struct uart_port *port)
330 return request_mem_region(port->mapbase, 32, serial21285_name)
331 != NULL ? 0 : -EBUSY;
334 static void serial21285_config_port(struct uart_port *port, int flags)
336 if (flags & UART_CONFIG_TYPE && serial21285_request_port(port) == 0)
337 port->type = PORT_21285;
341 * verify the new serial_struct (for TIOCSSERIAL).
343 static int serial21285_verify_port(struct uart_port *port, struct serial_struct *ser)
346 if (ser->type != PORT_UNKNOWN && ser->type != PORT_21285)
348 if (ser->irq != NO_IRQ)
350 if (ser->baud_base != port->uartclk / 16)
355 static struct uart_ops serial21285_ops = {
356 .tx_empty = serial21285_tx_empty,
357 .get_mctrl = serial21285_get_mctrl,
358 .set_mctrl = serial21285_set_mctrl,
359 .stop_tx = serial21285_stop_tx,
360 .start_tx = serial21285_start_tx,
361 .stop_rx = serial21285_stop_rx,
362 .enable_ms = serial21285_enable_ms,
363 .break_ctl = serial21285_break_ctl,
364 .startup = serial21285_startup,
365 .shutdown = serial21285_shutdown,
366 .set_termios = serial21285_set_termios,
367 .type = serial21285_type,
368 .release_port = serial21285_release_port,
369 .request_port = serial21285_request_port,
370 .config_port = serial21285_config_port,
371 .verify_port = serial21285_verify_port,
374 static struct uart_port serial21285_port = {
375 .mapbase = 0x42000160,
376 .iotype = SERIAL_IO_MEM,
379 .ops = &serial21285_ops,
380 .flags = ASYNC_BOOT_AUTOCONF,
383 static void serial21285_setup_ports(void)
385 serial21285_port.uartclk = mem_fclk_21285 / 4;
388 #ifdef CONFIG_SERIAL_21285_CONSOLE
391 serial21285_console_write(struct console *co, const char *s,
396 for (i = 0; i < count; i++) {
397 while (*CSR_UARTFLG & 0x20)
401 while (*CSR_UARTFLG & 0x20)
409 serial21285_get_options(struct uart_port *port, int *baud,
410 int *parity, int *bits)
412 if (*CSR_UARTCON == 1) {
416 switch (tmp & 0x60) {
432 if (tmp & H_UBRLCR_PARENB) {
434 if (tmp & H_UBRLCR_PAREVN)
438 tmp = *CSR_L_UBRLCR | (*CSR_M_UBRLCR << 8);
440 *baud = port->uartclk / (16 * (tmp + 1));
444 static int __init serial21285_console_setup(struct console *co, char *options)
446 struct uart_port *port = &serial21285_port;
452 if (machine_is_personal_server())
456 * Check whether an invalid uart number has been specified, and
457 * if so, search for the first available port that does have
461 uart_parse_options(options, &baud, &parity, &bits, &flow);
463 serial21285_get_options(port, &baud, &parity, &bits);
465 return uart_set_options(port, co, baud, parity, bits, flow);
468 extern struct uart_driver serial21285_reg;
470 static struct console serial21285_console =
472 .name = SERIAL_21285_NAME,
473 .write = serial21285_console_write,
474 .device = uart_console_device,
475 .setup = serial21285_console_setup,
476 .flags = CON_PRINTBUFFER,
478 .data = &serial21285_reg,
481 static int __init rs285_console_init(void)
483 serial21285_setup_ports();
484 register_console(&serial21285_console);
487 console_initcall(rs285_console_init);
489 #define SERIAL_21285_CONSOLE &serial21285_console
491 #define SERIAL_21285_CONSOLE NULL
494 static struct uart_driver serial21285_reg = {
495 .owner = THIS_MODULE,
496 .driver_name = "ttyFB",
498 .devfs_name = "ttyFB",
499 .major = SERIAL_21285_MAJOR,
500 .minor = SERIAL_21285_MINOR,
502 .cons = SERIAL_21285_CONSOLE,
505 static int __init serial21285_init(void)
509 printk(KERN_INFO "Serial: 21285 driver $Revision: 1.37 $\n");
511 serial21285_setup_ports();
513 ret = uart_register_driver(&serial21285_reg);
515 uart_add_one_port(&serial21285_reg, &serial21285_port);
520 static void __exit serial21285_exit(void)
522 uart_remove_one_port(&serial21285_reg, &serial21285_port);
523 uart_unregister_driver(&serial21285_reg);
526 module_init(serial21285_init);
527 module_exit(serial21285_exit);
529 MODULE_LICENSE("GPL");
530 MODULE_DESCRIPTION("Intel Footbridge (21285) serial driver $Revision: 1.37 $");
531 MODULE_ALIAS_CHARDEV(SERIAL_21285_MAJOR, SERIAL_21285_MINOR);