2 * Driver for IFXMIPSASC serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright (C) 2004 Infineon IFAP DC COM CPE
21 * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
22 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/signal.h>
28 #include <linux/sched.h>
29 #include <linux/interrupt.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/major.h>
33 #include <linux/string.h>
34 #include <linux/fcntl.h>
35 #include <linux/ptrace.h>
36 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/init.h>
40 #include <linux/circ_buf.h>
41 #include <linux/serial.h>
42 #include <linux/serial_core.h>
43 #include <linux/console.h>
44 #include <linux/sysrq.h>
45 #include <linux/irq.h>
47 #include <asm/system.h>
49 #include <asm/uaccess.h>
50 #include <asm/bitops.h>
51 #include <asm/ifxmips/ifxmips.h>
52 #include <asm/ifxmips/ifxmips_irq.h>
53 #include <asm/ifxmips/ifxmips_serial.h>
55 #define PORT_IFXMIPSASC 111
57 #include <linux/serial_core.h>
59 #define UART_DUMMY_UER_RX 1
61 static void ifxmipsasc_tx_chars(struct uart_port *port);
62 extern void prom_printf(const char * fmt, ...);
63 static struct uart_port ifxmipsasc_port;
64 static struct uart_driver ifxmipsasc_reg;
65 static unsigned int uartclk = 0;
66 extern unsigned int ifxmips_get_fpi_hz(void);
69 ifxmipsasc_stop_tx (struct uart_port *port)
71 /* fifo underrun shuts up after firing once */
76 ifxmipsasc_start_tx (struct uart_port *port)
80 local_irq_save(flags);
81 ifxmipsasc_tx_chars(port);
82 local_irq_restore(flags);
88 ifxmipsasc_stop_rx (struct uart_port *port)
90 /* clear the RX enable bit */
91 ifxmips_w32(ASCWHBSTATE_CLRREN, IFXMIPS_ASC1_WHBSTATE);
95 ifxmipsasc_enable_ms (struct uart_port *port)
97 /* no modem signals */
102 ifxmipsasc_rx_chars (struct uart_port *port)
104 struct tty_struct *tty = port->info->tty;
105 unsigned int ch = 0, rsr = 0, fifocnt;
107 fifocnt = ifxmips_r32(IFXMIPS_ASC1_FSTAT) & ASCFSTAT_RXFFLMASK;
110 u8 flag = TTY_NORMAL;
111 ch = ifxmips_r32(IFXMIPS_ASC1_RBUF);
112 rsr = (ifxmips_r32(IFXMIPS_ASC1_STATE) & ASCSTATE_ANY) | UART_DUMMY_UER_RX;
113 tty_flip_buffer_push(tty);
117 * Note that the error handling code is
118 * out of the main execution path
120 if (rsr & ASCSTATE_ANY) {
121 if (rsr & ASCSTATE_PE) {
122 port->icount.parity++;
123 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_WHBSTATE) | ASCWHBSTATE_CLRPE, IFXMIPS_ASC1_WHBSTATE);
124 } else if (rsr & ASCSTATE_FE) {
125 port->icount.frame++;
126 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_WHBSTATE) | ASCWHBSTATE_CLRFE, IFXMIPS_ASC1_WHBSTATE);
128 if (rsr & ASCSTATE_ROE) {
129 port->icount.overrun++;
130 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_WHBSTATE) | ASCWHBSTATE_CLRROE, IFXMIPS_ASC1_WHBSTATE);
133 rsr &= port->read_status_mask;
135 if (rsr & ASCSTATE_PE)
137 else if (rsr & ASCSTATE_FE)
141 if ((rsr & port->ignore_status_mask) == 0)
142 tty_insert_flip_char(tty, ch, flag);
144 if (rsr & ASCSTATE_ROE)
146 * Overrun is special, since it's reported
147 * immediately, and doesn't affect the current
150 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
153 tty_flip_buffer_push(tty);
160 ifxmipsasc_tx_chars (struct uart_port *port)
162 struct circ_buf *xmit = &port->info->xmit;
164 if (uart_tx_stopped(port)) {
165 ifxmipsasc_stop_tx(port);
169 while(((ifxmips_r32(IFXMIPS_ASC1_FSTAT) & ASCFSTAT_TXFFLMASK)
170 >> ASCFSTAT_TXFFLOFF) != IFXMIPSASC_TXFIFO_FULL)
173 ifxmips_w32(port->x_char, IFXMIPS_ASC1_TBUF);
179 if (uart_circ_empty(xmit))
182 ifxmips_w32(port->info->xmit.buf[port->info->xmit.tail], IFXMIPS_ASC1_TBUF);
183 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
187 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
188 uart_write_wakeup(port);
192 ifxmipsasc_tx_int (int irq, void *port)
194 ifxmips_w32(ASC_IRNCR_TIR, IFXMIPS_ASC1_IRNCR);
195 ifxmipsasc_start_tx(port);
196 ifxmips_mask_and_ack_irq(irq);
202 ifxmipsasc_er_int (int irq, void *port)
204 /* clear any pending interrupts */
205 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_WHBSTATE) | ASCWHBSTATE_CLRPE |
206 ASCWHBSTATE_CLRFE | ASCWHBSTATE_CLRROE, IFXMIPS_ASC1_WHBSTATE);
212 ifxmipsasc_rx_int (int irq, void *port)
214 ifxmips_w32(ASC_IRNCR_RIR, IFXMIPS_ASC1_IRNCR);
215 ifxmipsasc_rx_chars((struct uart_port *) port);
216 ifxmips_mask_and_ack_irq(irq);
222 ifxmipsasc_tx_empty (struct uart_port *port)
226 status = ifxmips_r32(IFXMIPS_ASC1_FSTAT) & ASCFSTAT_TXFFLMASK;
228 return status ? 0 : TIOCSER_TEMT;
232 ifxmipsasc_get_mctrl (struct uart_port *port)
234 return TIOCM_CTS | TIOCM_CAR | TIOCM_DSR;
238 ifxmipsasc_set_mctrl (struct uart_port *port, u_int mctrl)
244 ifxmipsasc_break_ctl (struct uart_port *port, int break_state)
250 ifxmipsasc1_hw_init (void)
252 /* this setup was probably already done in ROM/u-boot but we do it again*/
253 /* TODO: GPIO pins are multifunction */
254 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_CLC) & ~IFXMIPS_ASC1_CLC_DISS, IFXMIPS_ASC1_CLC);
255 ifxmips_w32((ifxmips_r32(IFXMIPS_ASC1_CLC) & ~ASCCLC_RMCMASK) | (1 << ASCCLC_RMCOFFSET), IFXMIPS_ASC1_CLC);
256 ifxmips_w32(0, IFXMIPS_ASC1_PISEL);
257 ifxmips_w32(((IFXMIPSASC_TXFIFO_FL << ASCTXFCON_TXFITLOFF) &
258 ASCTXFCON_TXFITLMASK) | ASCTXFCON_TXFEN | ASCTXFCON_TXFFLU, IFXMIPS_ASC1_TXFCON);
259 ifxmips_w32(((IFXMIPSASC_RXFIFO_FL << ASCRXFCON_RXFITLOFF) &
260 ASCRXFCON_RXFITLMASK) | ASCRXFCON_RXFEN | ASCRXFCON_RXFFLU, IFXMIPS_ASC1_RXFCON);
263 /*framing, overrun, enable */
264 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_CON) | ASCCON_M_8ASYNC | ASCCON_FEN | ASCCON_TOEN | ASCCON_ROEN,
269 ifxmipsasc_startup (struct uart_port *port)
274 /* this assumes: CON.BRS = CON.FDE = 0 */
276 uartclk = ifxmips_get_fpi_hz();
278 ifxmipsasc_port.uartclk = uartclk;
280 ifxmipsasc1_hw_init();
282 local_irq_save(flags);
284 retval = request_irq(IFXMIPSASC1_RIR, ifxmipsasc_rx_int, IRQF_DISABLED, "asc_rx", port);
286 printk("failed to request ifxmipsasc_rx_int\n");
290 retval = request_irq(IFXMIPSASC1_TIR, ifxmipsasc_tx_int, IRQF_DISABLED, "asc_tx", port);
292 printk("failed to request ifxmipsasc_tx_int\n");
296 retval = request_irq(IFXMIPSASC1_EIR, ifxmipsasc_er_int, IRQF_DISABLED, "asc_er", port);
298 printk("failed to request ifxmipsasc_er_int\n");
302 ifxmips_w32(ASC_IRNREN_RX_BUF | ASC_IRNREN_TX_BUF | ASC_IRNREN_ERR | ASC_IRNREN_TX,
303 IFXMIPS_ASC1_IRNREN);
305 local_irq_restore(flags);
310 free_irq(IFXMIPSASC1_TIR, port);
313 free_irq(IFXMIPSASC1_RIR, port);
314 local_irq_restore(flags);
320 ifxmipsasc_shutdown (struct uart_port *port)
322 free_irq(IFXMIPSASC1_RIR, port);
323 free_irq(IFXMIPSASC1_TIR, port);
324 free_irq(IFXMIPSASC1_EIR, port);
326 * disable the baudrate generator to disable the ASC
328 ifxmips_w32(0, IFXMIPS_ASC1_CON);
330 /* flush and then disable the fifos */
331 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_RXFCON) | ASCRXFCON_RXFFLU, IFXMIPS_ASC1_RXFCON);
332 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_RXFCON) & ~ASCRXFCON_RXFEN, IFXMIPS_ASC1_RXFCON);
333 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_TXFCON) | ASCTXFCON_TXFFLU, IFXMIPS_ASC1_TXFCON);
334 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_TXFCON) & ~ASCTXFCON_TXFEN, IFXMIPS_ASC1_TXFCON);
337 static void ifxmipsasc_set_termios(struct uart_port *port, struct ktermios *new, struct ktermios *old)
343 unsigned int con = 0;
346 cflag = new->c_cflag;
347 iflag = new->c_iflag;
349 /* byte size and parity */
350 switch (cflag & CSIZE) {
352 con = ASCCON_M_7ASYNC;
358 con = ASCCON_M_8ASYNC;
365 if (cflag & PARENB) {
366 if (!(cflag & PARODD))
372 port->read_status_mask = ASCSTATE_ROE;
374 port->read_status_mask |= ASCSTATE_FE | ASCSTATE_PE;
376 port->ignore_status_mask = 0;
378 port->ignore_status_mask |= ASCSTATE_FE | ASCSTATE_PE;
380 if (iflag & IGNBRK) {
382 * If we're ignoring parity and break indicators,
383 * ignore overruns too (for real raw support).
386 port->ignore_status_mask |= ASCSTATE_ROE;
389 if ((cflag & CREAD) == 0)
390 port->ignore_status_mask |= UART_DUMMY_UER_RX;
392 /* set error signals - framing, parity and overrun, enable receiver */
393 con |= ASCCON_FEN | ASCCON_TOEN | ASCCON_ROEN;
395 local_irq_save(flags);
398 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_CON) | con, IFXMIPS_ASC1_CON);
400 /* Set baud rate - take a divider of 2 into account */
401 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
402 quot = uart_get_divisor(port, baud);
405 /* disable the baudrate generator */
406 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_CON) & ~ASCCON_R, IFXMIPS_ASC1_CON);
408 /* make sure the fractional divider is off */
409 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_CON) & ~ASCCON_FDE, IFXMIPS_ASC1_CON);
411 /* set up to use divisor of 2 */
412 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_CON) & ~ASCCON_BRS, IFXMIPS_ASC1_CON);
414 /* now we can write the new baudrate into the register */
415 ifxmips_w32(quot, IFXMIPS_ASC1_BG);
417 /* turn the baudrate generator back on */
418 ifxmips_w32(ifxmips_r32(IFXMIPS_ASC1_CON) | ASCCON_R, IFXMIPS_ASC1_CON);
421 ifxmips_w32(ASCWHBSTATE_SETREN, IFXMIPS_ASC1_WHBSTATE);
423 local_irq_restore(flags);
427 ifxmipsasc_type (struct uart_port *port)
429 return port->type == PORT_IFXMIPSASC ? "IFXMIPSASC" : NULL;
433 ifxmipsasc_release_port (struct uart_port *port)
439 ifxmipsasc_request_port (struct uart_port *port)
445 ifxmipsasc_config_port (struct uart_port *port, int flags)
447 if (flags & UART_CONFIG_TYPE) {
448 port->type = PORT_IFXMIPSASC;
449 ifxmipsasc_request_port(port);
454 ifxmipsasc_verify_port (struct uart_port *port, struct serial_struct *ser)
457 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IFXMIPSASC)
459 if (ser->irq < 0 || ser->irq >= NR_IRQS)
461 if (ser->baud_base < 9600)
466 static struct uart_ops ifxmipsasc_pops = {
467 .tx_empty = ifxmipsasc_tx_empty,
468 .set_mctrl = ifxmipsasc_set_mctrl,
469 .get_mctrl = ifxmipsasc_get_mctrl,
470 .stop_tx = ifxmipsasc_stop_tx,
471 .start_tx = ifxmipsasc_start_tx,
472 .stop_rx = ifxmipsasc_stop_rx,
473 .enable_ms = ifxmipsasc_enable_ms,
474 .break_ctl = ifxmipsasc_break_ctl,
475 .startup = ifxmipsasc_startup,
476 .shutdown = ifxmipsasc_shutdown,
477 .set_termios = ifxmipsasc_set_termios,
478 .type = ifxmipsasc_type,
479 .release_port = ifxmipsasc_release_port,
480 .request_port = ifxmipsasc_request_port,
481 .config_port = ifxmipsasc_config_port,
482 .verify_port = ifxmipsasc_verify_port,
485 static struct uart_port ifxmipsasc_port = {
486 membase: (void *)IFXMIPS_ASC1_BASE_ADDR,
487 mapbase: IFXMIPS_ASC1_BASE_ADDR,
488 iotype: SERIAL_IO_MEM,
489 irq: IFXMIPSASC1_RIR,
492 unused: {IFXMIPSASC1_TIR, IFXMIPSASC1_EIR},
493 type: PORT_IFXMIPSASC,
494 ops: &ifxmipsasc_pops,
495 flags: ASYNC_BOOT_AUTOCONF,
499 ifxmipsasc_console_write (struct console *co, const char *s, u_int count)
504 local_irq_save(flags);
505 for (i = 0; i < count; i++)
507 /* wait until the FIFO is not full */
510 fifocnt = (ifxmips_r32(IFXMIPS_ASC1_FSTAT) & ASCFSTAT_TXFFLMASK)
511 >> ASCFSTAT_TXFFLOFF;
512 } while (fifocnt == IFXMIPSASC_TXFIFO_FULL);
521 ifxmips_w32('\r', IFXMIPS_ASC1_TBUF);
524 fifocnt = (ifxmips_r32(IFXMIPS_ASC1_FSTAT) & ASCFSTAT_TXFFLMASK)
525 >> ASCFSTAT_TXFFLOFF;
526 } while (fifocnt == IFXMIPSASC_TXFIFO_FULL);
528 ifxmips_w32(s[i], IFXMIPS_ASC1_TBUF);
531 local_irq_restore(flags);
535 ifxmipsasc_console_setup (struct console *co, char *options)
537 struct uart_port *port;
544 uartclk = ifxmips_get_fpi_hz();
546 port = &ifxmipsasc_port;
547 ifxmipsasc_port.uartclk = uartclk;
548 ifxmipsasc_port.type = PORT_IFXMIPSASC;
551 uart_parse_options(options, &baud, &parity, &bits, &flow);
554 return uart_set_options(port, co, baud, parity, bits, flow);
557 static struct uart_driver ifxmipsasc_reg;
558 static struct console ifxmipsasc_console = {
560 write: ifxmipsasc_console_write,
561 device: uart_console_device,
562 setup: ifxmipsasc_console_setup,
563 flags: CON_PRINTBUFFER,
565 data: &ifxmipsasc_reg,
569 ifxmipsasc_console_init (void)
571 register_console(&ifxmipsasc_console);
574 console_initcall(ifxmipsasc_console_init);
576 static struct uart_driver ifxmipsasc_reg = {
577 .owner = THIS_MODULE,
578 .driver_name = "serial",
583 .cons = &ifxmipsasc_console,
587 ifxmipsasc_init (void)
591 uart_register_driver(&ifxmipsasc_reg);
592 res = uart_add_one_port(&ifxmipsasc_reg, &ifxmipsasc_port);
598 ifxmipsasc_exit (void)
600 uart_unregister_driver(&ifxmipsasc_reg);
603 module_init(ifxmipsasc_init);
604 module_exit(ifxmipsasc_exit);
606 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
607 MODULE_DESCRIPTION("MIPS IFXMips serial port driver");
608 MODULE_LICENSE("GPL");