2 * linux/drivers/serial/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/module.h>
11 #include <linux/tty.h>
12 #include <linux/ioport.h>
13 #include <linux/init.h>
14 #include <linux/console.h>
15 #include <linux/device.h>
16 #include <linux/tty_flip.h>
17 #include <linux/serial_core.h>
18 #include <linux/serial.h>
22 #include <asm/mach-types.h>
23 #include <asm/hardware/dec21285.h>
24 #include <asm/hardware.h>
26 #define BAUD_BASE (mem_fclk_21285/64)
28 #define SERIAL_21285_NAME "ttyFB"
29 #define SERIAL_21285_MAJOR 204
30 #define SERIAL_21285_MINOR 4
32 #define RXSTAT_DUMMY_READ 0x80000000
33 #define RXSTAT_FRAME (1 << 0)
34 #define RXSTAT_PARITY (1 << 1)
35 #define RXSTAT_OVERRUN (1 << 2)
36 #define RXSTAT_ANYERR (RXSTAT_FRAME|RXSTAT_PARITY|RXSTAT_OVERRUN)
38 #define H_UBRLCR_BREAK (1 << 0)
39 #define H_UBRLCR_PARENB (1 << 1)
40 #define H_UBRLCR_PAREVN (1 << 2)
41 #define H_UBRLCR_STOPB (1 << 3)
42 #define H_UBRLCR_FIFO (1 << 4)
44 static const char serial21285_name[] = "Footbridge UART";
46 #define tx_enabled(port) ((port)->unused[0])
47 #define rx_enabled(port) ((port)->unused[1])
50 * The documented expression for selecting the divisor is:
51 * BAUD_BASE / baud - 1
52 * However, typically BAUD_BASE is not divisible by baud, so
53 * we want to select the divisor that gives us the minimum
54 * error. Therefore, we want:
55 * int(BAUD_BASE / baud - 0.5) ->
56 * int(BAUD_BASE / baud - (baud >> 1) / baud) ->
57 * int((BAUD_BASE - (baud >> 1)) / baud)
60 static void serial21285_stop_tx(struct uart_port *port)
62 if (tx_enabled(port)) {
63 disable_irq(IRQ_CONTX);
68 static void serial21285_start_tx(struct uart_port *port)
70 if (!tx_enabled(port)) {
71 enable_irq(IRQ_CONTX);
76 static void serial21285_stop_rx(struct uart_port *port)
78 if (rx_enabled(port)) {
79 disable_irq(IRQ_CONRX);
84 static void serial21285_enable_ms(struct uart_port *port)
88 static irqreturn_t serial21285_rx_chars(int irq, void *dev_id)
90 struct uart_port *port = dev_id;
91 struct tty_struct *tty = port->info->tty;
92 unsigned int status, ch, flag, rxs, max_count = 256;
94 status = *CSR_UARTFLG;
95 while (!(status & 0x10) && max_count--) {
100 rxs = *CSR_RXSTAT | RXSTAT_DUMMY_READ;
101 if (unlikely(rxs & RXSTAT_ANYERR)) {
102 if (rxs & RXSTAT_PARITY)
103 port->icount.parity++;
104 else if (rxs & RXSTAT_FRAME)
105 port->icount.frame++;
106 if (rxs & RXSTAT_OVERRUN)
107 port->icount.overrun++;
109 rxs &= port->read_status_mask;
111 if (rxs & RXSTAT_PARITY)
113 else if (rxs & RXSTAT_FRAME)
117 uart_insert_char(port, rxs, RXSTAT_OVERRUN, ch, flag);
119 status = *CSR_UARTFLG;
121 tty_flip_buffer_push(tty);
126 static irqreturn_t serial21285_tx_chars(int irq, void *dev_id)
128 struct uart_port *port = dev_id;
129 struct circ_buf *xmit = &port->info->xmit;
133 *CSR_UARTDR = port->x_char;
138 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
139 serial21285_stop_tx(port);
144 *CSR_UARTDR = xmit->buf[xmit->tail];
145 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
147 if (uart_circ_empty(xmit))
149 } while (--count > 0 && !(*CSR_UARTFLG & 0x20));
151 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
152 uart_write_wakeup(port);
154 if (uart_circ_empty(xmit))
155 serial21285_stop_tx(port);
161 static unsigned int serial21285_tx_empty(struct uart_port *port)
163 return (*CSR_UARTFLG & 8) ? 0 : TIOCSER_TEMT;
166 /* no modem control lines */
167 static unsigned int serial21285_get_mctrl(struct uart_port *port)
169 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
172 static void serial21285_set_mctrl(struct uart_port *port, unsigned int mctrl)
176 static void serial21285_break_ctl(struct uart_port *port, int break_state)
181 spin_lock_irqsave(&port->lock, flags);
182 h_lcr = *CSR_H_UBRLCR;
184 h_lcr |= H_UBRLCR_BREAK;
186 h_lcr &= ~H_UBRLCR_BREAK;
187 *CSR_H_UBRLCR = h_lcr;
188 spin_unlock_irqrestore(&port->lock, flags);
191 static int serial21285_startup(struct uart_port *port)
195 tx_enabled(port) = 1;
196 rx_enabled(port) = 1;
198 ret = request_irq(IRQ_CONRX, serial21285_rx_chars, 0,
199 serial21285_name, port);
201 ret = request_irq(IRQ_CONTX, serial21285_tx_chars, 0,
202 serial21285_name, port);
204 free_irq(IRQ_CONRX, port);
210 static void serial21285_shutdown(struct uart_port *port)
212 free_irq(IRQ_CONTX, port);
213 free_irq(IRQ_CONRX, port);
217 serial21285_set_termios(struct uart_port *port, struct ktermios *termios,
218 struct ktermios *old)
221 unsigned int baud, quot, h_lcr;
224 * We don't support modem control lines.
226 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
227 termios->c_cflag |= CLOCAL;
230 * We don't support BREAK character recognition.
232 termios->c_iflag &= ~(IGNBRK | BRKINT);
235 * Ask the core to calculate the divisor for us.
237 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
238 quot = uart_get_divisor(port, baud);
240 switch (termios->c_cflag & CSIZE) {
255 if (termios->c_cflag & CSTOPB)
256 h_lcr |= H_UBRLCR_STOPB;
257 if (termios->c_cflag & PARENB) {
258 h_lcr |= H_UBRLCR_PARENB;
259 if (!(termios->c_cflag & PARODD))
260 h_lcr |= H_UBRLCR_PAREVN;
264 h_lcr |= H_UBRLCR_FIFO;
266 spin_lock_irqsave(&port->lock, flags);
269 * Update the per-port timeout.
271 uart_update_timeout(port, termios->c_cflag, baud);
274 * Which character status flags are we interested in?
276 port->read_status_mask = RXSTAT_OVERRUN;
277 if (termios->c_iflag & INPCK)
278 port->read_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
280 tty_encode_baud_rate(tty, baud, baud);
283 * Which character status flags should we ignore?
285 port->ignore_status_mask = 0;
286 if (termios->c_iflag & IGNPAR)
287 port->ignore_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
288 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
289 port->ignore_status_mask |= RXSTAT_OVERRUN;
292 * Ignore all characters if CREAD is not set.
294 if ((termios->c_cflag & CREAD) == 0)
295 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
300 *CSR_L_UBRLCR = quot & 0xff;
301 *CSR_M_UBRLCR = (quot >> 8) & 0x0f;
302 *CSR_H_UBRLCR = h_lcr;
305 spin_unlock_irqrestore(&port->lock, flags);
308 static const char *serial21285_type(struct uart_port *port)
310 return port->type == PORT_21285 ? "DC21285" : NULL;
313 static void serial21285_release_port(struct uart_port *port)
315 release_mem_region(port->mapbase, 32);
318 static int serial21285_request_port(struct uart_port *port)
320 return request_mem_region(port->mapbase, 32, serial21285_name)
321 != NULL ? 0 : -EBUSY;
324 static void serial21285_config_port(struct uart_port *port, int flags)
326 if (flags & UART_CONFIG_TYPE && serial21285_request_port(port) == 0)
327 port->type = PORT_21285;
331 * verify the new serial_struct (for TIOCSSERIAL).
333 static int serial21285_verify_port(struct uart_port *port, struct serial_struct *ser)
336 if (ser->type != PORT_UNKNOWN && ser->type != PORT_21285)
338 if (ser->irq != NO_IRQ)
340 if (ser->baud_base != port->uartclk / 16)
345 static struct uart_ops serial21285_ops = {
346 .tx_empty = serial21285_tx_empty,
347 .get_mctrl = serial21285_get_mctrl,
348 .set_mctrl = serial21285_set_mctrl,
349 .stop_tx = serial21285_stop_tx,
350 .start_tx = serial21285_start_tx,
351 .stop_rx = serial21285_stop_rx,
352 .enable_ms = serial21285_enable_ms,
353 .break_ctl = serial21285_break_ctl,
354 .startup = serial21285_startup,
355 .shutdown = serial21285_shutdown,
356 .set_termios = serial21285_set_termios,
357 .type = serial21285_type,
358 .release_port = serial21285_release_port,
359 .request_port = serial21285_request_port,
360 .config_port = serial21285_config_port,
361 .verify_port = serial21285_verify_port,
364 static struct uart_port serial21285_port = {
365 .mapbase = 0x42000160,
369 .ops = &serial21285_ops,
370 .flags = UPF_BOOT_AUTOCONF,
373 static void serial21285_setup_ports(void)
375 serial21285_port.uartclk = mem_fclk_21285 / 4;
378 #ifdef CONFIG_SERIAL_21285_CONSOLE
379 static void serial21285_console_putchar(struct uart_port *port, int ch)
381 while (*CSR_UARTFLG & 0x20)
387 serial21285_console_write(struct console *co, const char *s,
390 uart_console_write(&serial21285_port, s, count, serial21285_console_putchar);
394 serial21285_get_options(struct uart_port *port, int *baud,
395 int *parity, int *bits)
397 if (*CSR_UARTCON == 1) {
401 switch (tmp & 0x60) {
417 if (tmp & H_UBRLCR_PARENB) {
419 if (tmp & H_UBRLCR_PAREVN)
423 tmp = *CSR_L_UBRLCR | (*CSR_M_UBRLCR << 8);
425 *baud = port->uartclk / (16 * (tmp + 1));
429 static int __init serial21285_console_setup(struct console *co, char *options)
431 struct uart_port *port = &serial21285_port;
437 if (machine_is_personal_server())
441 * Check whether an invalid uart number has been specified, and
442 * if so, search for the first available port that does have
446 uart_parse_options(options, &baud, &parity, &bits, &flow);
448 serial21285_get_options(port, &baud, &parity, &bits);
450 return uart_set_options(port, co, baud, parity, bits, flow);
453 static struct uart_driver serial21285_reg;
455 static struct console serial21285_console =
457 .name = SERIAL_21285_NAME,
458 .write = serial21285_console_write,
459 .device = uart_console_device,
460 .setup = serial21285_console_setup,
461 .flags = CON_PRINTBUFFER,
463 .data = &serial21285_reg,
466 static int __init rs285_console_init(void)
468 serial21285_setup_ports();
469 register_console(&serial21285_console);
472 console_initcall(rs285_console_init);
474 #define SERIAL_21285_CONSOLE &serial21285_console
476 #define SERIAL_21285_CONSOLE NULL
479 static struct uart_driver serial21285_reg = {
480 .owner = THIS_MODULE,
481 .driver_name = "ttyFB",
483 .major = SERIAL_21285_MAJOR,
484 .minor = SERIAL_21285_MINOR,
486 .cons = SERIAL_21285_CONSOLE,
489 static int __init serial21285_init(void)
493 printk(KERN_INFO "Serial: 21285 driver $Revision: 1.37 $\n");
495 serial21285_setup_ports();
497 ret = uart_register_driver(&serial21285_reg);
499 uart_add_one_port(&serial21285_reg, &serial21285_port);
504 static void __exit serial21285_exit(void)
506 uart_remove_one_port(&serial21285_reg, &serial21285_port);
507 uart_unregister_driver(&serial21285_reg);
510 module_init(serial21285_init);
511 module_exit(serial21285_exit);
513 MODULE_LICENSE("GPL");
514 MODULE_DESCRIPTION("Intel Footbridge (21285) serial driver $Revision: 1.37 $");
515 MODULE_ALIAS_CHARDEV(SERIAL_21285_MAJOR, SERIAL_21285_MINOR);