2 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Copyright (C) 2004 Infineon IFAP DC COM CPE
19 * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
20 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/signal.h>
26 #include <linux/sched.h>
27 #include <linux/interrupt.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/major.h>
31 #include <linux/string.h>
32 #include <linux/fcntl.h>
33 #include <linux/ptrace.h>
34 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/init.h>
38 #include <linux/circ_buf.h>
39 #include <linux/serial.h>
40 #include <linux/serial_core.h>
41 #include <linux/console.h>
42 #include <linux/sysrq.h>
43 #include <linux/irq.h>
44 #include <linux/platform_device.h>
46 #include <linux/uaccess.h>
47 #include <linux/bitops.h>
49 #include <asm/system.h>
51 #include <asm/ifxmips/ifxmips.h>
52 #include <asm/ifxmips/ifxmips_irq.h>
54 #define PORT_IFXMIPSASC 111
56 #include <linux/serial_core.h>
58 #define UART_DUMMY_UER_RX 1
60 static void ifxmipsasc_tx_chars(struct uart_port
*port
);
61 extern void prom_printf(const char *fmt
, ...);
62 static struct uart_port ifxmipsasc_port
[2];
63 static struct uart_driver ifxmipsasc_reg
;
64 extern unsigned int ifxmips_get_fpi_hz(void);
66 static void ifxmipsasc_stop_tx(struct uart_port
*port
)
71 static void ifxmipsasc_start_tx(struct uart_port
*port
)
74 local_irq_save(flags
);
75 ifxmipsasc_tx_chars(port
);
76 local_irq_restore(flags
);
80 static void ifxmipsasc_stop_rx(struct uart_port
*port
)
82 ifxmips_w32(ASCWHBSTATE_CLRREN
, port
->membase
+ IFXMIPS_ASC_WHBSTATE
);
85 static void ifxmipsasc_enable_ms(struct uart_port
*port
)
89 #include <linux/version.h>
91 static void ifxmipsasc_rx_chars(struct uart_port
*port
)
93 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 26))
94 struct tty_struct
*tty
= port
->info
->port
.tty
;
96 struct tty_struct
*tty
= port
->info
->tty
;
98 unsigned int ch
= 0, rsr
= 0, fifocnt
;
100 fifocnt
= ifxmips_r32(port
->membase
+ IFXMIPS_ASC_FSTAT
) & ASCFSTAT_RXFFLMASK
;
102 u8 flag
= TTY_NORMAL
;
103 ch
= ifxmips_r32(port
->membase
+ IFXMIPS_ASC_RBUF
);
104 rsr
= (ifxmips_r32(port
->membase
+ IFXMIPS_ASC_STATE
) & ASCSTATE_ANY
) | UART_DUMMY_UER_RX
;
105 tty_flip_buffer_push(tty
);
109 * Note that the error handling code is
110 * out of the main execution path
112 if (rsr
& ASCSTATE_ANY
) {
113 if (rsr
& ASCSTATE_PE
) {
114 port
->icount
.parity
++;
115 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_WHBSTATE
) | ASCWHBSTATE_CLRPE
, port
->membase
+ IFXMIPS_ASC_WHBSTATE
);
116 } else if (rsr
& ASCSTATE_FE
) {
117 port
->icount
.frame
++;
118 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_WHBSTATE
) | ASCWHBSTATE_CLRFE
, port
->membase
+ IFXMIPS_ASC_WHBSTATE
);
120 if (rsr
& ASCSTATE_ROE
) {
121 port
->icount
.overrun
++;
122 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_WHBSTATE
) | ASCWHBSTATE_CLRROE
, port
->membase
+ IFXMIPS_ASC_WHBSTATE
);
125 rsr
&= port
->read_status_mask
;
127 if (rsr
& ASCSTATE_PE
)
129 else if (rsr
& ASCSTATE_FE
)
133 if ((rsr
& port
->ignore_status_mask
) == 0)
134 tty_insert_flip_char(tty
, ch
, flag
);
136 if (rsr
& ASCSTATE_ROE
)
138 * Overrun is special, since it's reported
139 * immediately, and doesn't affect the current
142 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
145 tty_flip_buffer_push(tty
);
150 static void ifxmipsasc_tx_chars(struct uart_port
*port
)
152 struct circ_buf
*xmit
= &port
->info
->xmit
;
153 if (uart_tx_stopped(port
)) {
154 ifxmipsasc_stop_tx(port
);
158 while (((ifxmips_r32(port
->membase
+ IFXMIPS_ASC_FSTAT
) & ASCFSTAT_TXFFLMASK
)
159 >> ASCFSTAT_TXFFLOFF
) != TXFIFO_FULL
) {
161 ifxmips_w32(port
->x_char
, port
->membase
+ IFXMIPS_ASC_TBUF
);
167 if (uart_circ_empty(xmit
))
170 ifxmips_w32(port
->info
->xmit
.buf
[port
->info
->xmit
.tail
], port
->membase
+ IFXMIPS_ASC_TBUF
);
171 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
175 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
176 uart_write_wakeup(port
);
179 static irqreturn_t
ifxmipsasc_tx_int(int irq
, void *_port
)
181 struct uart_port
*port
= (struct uart_port
*)_port
;
182 ifxmips_w32(ASC_IRNCR_TIR
, port
->membase
+ IFXMIPS_ASC_IRNCR
);
183 ifxmipsasc_start_tx(port
);
184 ifxmips_mask_and_ack_irq(irq
);
188 static irqreturn_t
ifxmipsasc_er_int(int irq
, void *_port
)
190 struct uart_port
*port
= (struct uart_port
*)_port
;
191 /* clear any pending interrupts */
192 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_WHBSTATE
) | ASCWHBSTATE_CLRPE
|
193 ASCWHBSTATE_CLRFE
| ASCWHBSTATE_CLRROE
, port
->membase
+ IFXMIPS_ASC_WHBSTATE
);
197 static irqreturn_t
ifxmipsasc_rx_int(int irq
, void *_port
)
199 struct uart_port
*port
= (struct uart_port
*)_port
;
200 ifxmips_w32(ASC_IRNCR_RIR
, port
->membase
+ IFXMIPS_ASC_IRNCR
);
201 ifxmipsasc_rx_chars((struct uart_port
*)port
);
202 ifxmips_mask_and_ack_irq(irq
);
206 static unsigned int ifxmipsasc_tx_empty(struct uart_port
*port
)
209 status
= ifxmips_r32(port
->membase
+ IFXMIPS_ASC_FSTAT
) & ASCFSTAT_TXFFLMASK
;
210 return status
? 0 : TIOCSER_TEMT
;
213 static unsigned int ifxmipsasc_get_mctrl(struct uart_port
*port
)
215 return TIOCM_CTS
| TIOCM_CAR
| TIOCM_DSR
;
218 static void ifxmipsasc_set_mctrl(struct uart_port
*port
, u_int mctrl
)
222 static void ifxmipsasc_break_ctl(struct uart_port
*port
, int break_state
)
226 static int ifxmipsasc_startup(struct uart_port
*port
)
231 port
->uartclk
= ifxmips_get_fpi_hz();
233 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_CLC
) & ~IFXMIPS_ASC_CLC_DISS
, port
->membase
+ IFXMIPS_ASC_CLC
);
234 ifxmips_w32(((ifxmips_r32(port
->membase
+ IFXMIPS_ASC_CLC
) & ~ASCCLC_RMCMASK
)) | (1 << ASCCLC_RMCOFFSET
), port
->membase
+ IFXMIPS_ASC_CLC
);
235 ifxmips_w32(0, port
->membase
+ IFXMIPS_ASC_PISEL
);
236 ifxmips_w32(((TXFIFO_FL
<< ASCTXFCON_TXFITLOFF
) & ASCTXFCON_TXFITLMASK
) | ASCTXFCON_TXFEN
| ASCTXFCON_TXFFLU
, port
->membase
+ IFXMIPS_ASC_TXFCON
);
237 ifxmips_w32(((RXFIFO_FL
<< ASCRXFCON_RXFITLOFF
) & ASCRXFCON_RXFITLMASK
) | ASCRXFCON_RXFEN
| ASCRXFCON_RXFFLU
, port
->membase
+ IFXMIPS_ASC_RXFCON
);
239 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_CON
) | ASCCON_M_8ASYNC
| ASCCON_FEN
| ASCCON_TOEN
| ASCCON_ROEN
, port
->membase
+ IFXMIPS_ASC_CON
);
241 local_irq_save(flags
);
243 retval
= request_irq(port
->irq
, ifxmipsasc_tx_int
, IRQF_DISABLED
, "asc_tx", port
);
245 printk(KERN_ERR
"failed to request ifxmipsasc_tx_int\n");
249 retval
= request_irq(port
->irq
+ 2, ifxmipsasc_rx_int
, IRQF_DISABLED
, "asc_rx", port
);
251 printk(KERN_ERR
"failed to request ifxmipsasc_rx_int\n");
255 retval
= request_irq(port
->irq
+ 3, ifxmipsasc_er_int
, IRQF_DISABLED
, "asc_er", port
);
257 printk(KERN_ERR
"failed to request ifxmipsasc_er_int\n");
261 ifxmips_w32(ASC_IRNREN_RX_BUF
| ASC_IRNREN_TX_BUF
| ASC_IRNREN_ERR
| ASC_IRNREN_TX
, port
->membase
+ IFXMIPS_ASC_IRNREN
);
263 local_irq_restore(flags
);
267 free_irq(port
->irq
+ 2, port
);
269 free_irq(port
->irq
, port
);
270 local_irq_restore(flags
);
274 static void ifxmipsasc_shutdown(struct uart_port
*port
)
276 free_irq(port
->irq
, port
);
277 free_irq(port
->irq
+ 2, port
);
278 free_irq(port
->irq
+ 3, port
);
280 ifxmips_w32(0, port
->membase
+ IFXMIPS_ASC_CON
);
281 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_RXFCON
) | ASCRXFCON_RXFFLU
, port
->membase
+ IFXMIPS_ASC_RXFCON
);
282 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_RXFCON
) & ~ASCRXFCON_RXFEN
, port
->membase
+ IFXMIPS_ASC_RXFCON
);
283 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_TXFCON
) | ASCTXFCON_TXFFLU
, port
->membase
+ IFXMIPS_ASC_TXFCON
);
284 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_TXFCON
) & ~ASCTXFCON_TXFEN
, port
->membase
+ IFXMIPS_ASC_TXFCON
);
287 static void ifxmipsasc_set_termios(struct uart_port
*port
, struct ktermios
*new, struct ktermios
*old
)
293 unsigned int con
= 0;
296 cflag
= new->c_cflag
;
297 iflag
= new->c_iflag
;
299 switch (cflag
& CSIZE
) {
301 con
= ASCCON_M_7ASYNC
;
307 con
= ASCCON_M_8ASYNC
;
314 if (cflag
& PARENB
) {
315 if (!(cflag
& PARODD
))
321 port
->read_status_mask
= ASCSTATE_ROE
;
323 port
->read_status_mask
|= ASCSTATE_FE
| ASCSTATE_PE
;
325 port
->ignore_status_mask
= 0;
327 port
->ignore_status_mask
|= ASCSTATE_FE
| ASCSTATE_PE
;
329 if (iflag
& IGNBRK
) {
331 * If we're ignoring parity and break indicators,
332 * ignore overruns too (for real raw support).
335 port
->ignore_status_mask
|= ASCSTATE_ROE
;
338 if ((cflag
& CREAD
) == 0)
339 port
->ignore_status_mask
|= UART_DUMMY_UER_RX
;
341 /* set error signals - framing, parity and overrun, enable receiver */
342 con
|= ASCCON_FEN
| ASCCON_TOEN
| ASCCON_ROEN
;
344 local_irq_save(flags
);
347 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_CON
) | con
, port
->membase
+ IFXMIPS_ASC_CON
);
349 /* Set baud rate - take a divider of 2 into account */
350 baud
= uart_get_baud_rate(port
, new, old
, 0, port
->uartclk
/ 16);
351 quot
= uart_get_divisor(port
, baud
);
354 /* disable the baudrate generator */
355 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_CON
) & ~ASCCON_R
, port
->membase
+ IFXMIPS_ASC_CON
);
357 /* make sure the fractional divider is off */
358 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_CON
) & ~ASCCON_FDE
, port
->membase
+ IFXMIPS_ASC_CON
);
360 /* set up to use divisor of 2 */
361 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_CON
) & ~ASCCON_BRS
, port
->membase
+ IFXMIPS_ASC_CON
);
363 /* now we can write the new baudrate into the register */
364 ifxmips_w32(quot
, port
->membase
+ IFXMIPS_ASC_BG
);
366 /* turn the baudrate generator back on */
367 ifxmips_w32(ifxmips_r32(port
->membase
+ IFXMIPS_ASC_CON
) | ASCCON_R
, port
->membase
+ IFXMIPS_ASC_CON
);
370 ifxmips_w32(ASCWHBSTATE_SETREN
, port
->membase
+ IFXMIPS_ASC_WHBSTATE
);
372 local_irq_restore(flags
);
375 static const char *ifxmipsasc_type(struct uart_port
*port
)
377 if (port
->type
== PORT_IFXMIPSASC
) {
378 if (port
->membase
== (void *)IFXMIPS_ASC_BASE_ADDR
)
387 static void ifxmipsasc_release_port(struct uart_port
*port
)
391 static int ifxmipsasc_request_port(struct uart_port
*port
)
396 static void ifxmipsasc_config_port(struct uart_port
*port
, int flags
)
398 if (flags
& UART_CONFIG_TYPE
) {
399 port
->type
= PORT_IFXMIPSASC
;
400 ifxmipsasc_request_port(port
);
404 static int ifxmipsasc_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
407 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_IFXMIPSASC
)
409 if (ser
->irq
< 0 || ser
->irq
>= NR_IRQS
)
411 if (ser
->baud_base
< 9600)
416 static struct uart_ops ifxmipsasc_pops
= {
417 .tx_empty
= ifxmipsasc_tx_empty
,
418 .set_mctrl
= ifxmipsasc_set_mctrl
,
419 .get_mctrl
= ifxmipsasc_get_mctrl
,
420 .stop_tx
= ifxmipsasc_stop_tx
,
421 .start_tx
= ifxmipsasc_start_tx
,
422 .stop_rx
= ifxmipsasc_stop_rx
,
423 .enable_ms
= ifxmipsasc_enable_ms
,
424 .break_ctl
= ifxmipsasc_break_ctl
,
425 .startup
= ifxmipsasc_startup
,
426 .shutdown
= ifxmipsasc_shutdown
,
427 .set_termios
= ifxmipsasc_set_termios
,
428 .type
= ifxmipsasc_type
,
429 .release_port
= ifxmipsasc_release_port
,
430 .request_port
= ifxmipsasc_request_port
,
431 .config_port
= ifxmipsasc_config_port
,
432 .verify_port
= ifxmipsasc_verify_port
,
435 static struct uart_port ifxmipsasc_port
[2] = {
437 .membase
= (void *)IFXMIPS_ASC_BASE_ADDR
,
438 .mapbase
= IFXMIPS_ASC_BASE_ADDR
,
439 .iotype
= SERIAL_IO_MEM
,
440 .irq
= IFXMIPSASC_TIR(0),
443 .type
= PORT_IFXMIPSASC
,
444 .ops
= &ifxmipsasc_pops
,
445 .flags
= ASYNC_BOOT_AUTOCONF
,
448 .membase
= (void *)(IFXMIPS_ASC_BASE_ADDR
+ IFXMIPS_ASC_BASE_DIFF
),
449 .mapbase
= IFXMIPS_ASC_BASE_ADDR
+ IFXMIPS_ASC_BASE_DIFF
,
450 .iotype
= SERIAL_IO_MEM
,
451 .irq
= IFXMIPSASC_TIR(1),
454 .type
= PORT_IFXMIPSASC
,
455 .ops
= &ifxmipsasc_pops
,
456 .flags
= ASYNC_BOOT_AUTOCONF
,
461 static void ifxmipsasc_console_write(struct console
*co
, const char *s
, u_int count
)
463 int port
= co
->index
;
466 local_irq_save(flags
);
467 for (i
= 0; i
< count
; i
++) {
469 fifocnt
= (ifxmips_r32((u32
*)(IFXMIPS_ASC_BASE_ADDR
+ (port
* IFXMIPS_ASC_BASE_DIFF
) + IFXMIPS_ASC_FSTAT
)) & ASCFSTAT_TXFFLMASK
)
470 >> ASCFSTAT_TXFFLOFF
;
471 } while (fifocnt
== TXFIFO_FULL
);
477 ifxmips_w32('\r', (u32
*)(IFXMIPS_ASC_BASE_ADDR
+ (port
* IFXMIPS_ASC_BASE_DIFF
) + IFXMIPS_ASC_TBUF
));
479 fifocnt
= (ifxmips_r32((u32
*)(IFXMIPS_ASC_BASE_ADDR
+ (port
* IFXMIPS_ASC_BASE_DIFF
) + IFXMIPS_ASC_FSTAT
)) & ASCFSTAT_TXFFLMASK
)
480 >> ASCFSTAT_TXFFLOFF
;
481 } while (fifocnt
== TXFIFO_FULL
);
483 ifxmips_w32(s
[i
], (u32
*)(IFXMIPS_ASC_BASE_ADDR
+ (port
* IFXMIPS_ASC_BASE_DIFF
) + IFXMIPS_ASC_TBUF
));
486 local_irq_restore(flags
);
489 static int __init
ifxmipsasc_console_setup(struct console
*co
, char *options
)
491 int port
= co
->index
;
496 ifxmipsasc_port
[port
].uartclk
= ifxmips_get_fpi_hz();
497 ifxmipsasc_port
[port
].type
= PORT_IFXMIPSASC
;
499 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
500 return uart_set_options(&ifxmipsasc_port
[port
], co
, baud
, parity
, bits
, flow
);
503 static struct console ifxmipsasc_console
[2] =
507 .write
= ifxmipsasc_console_write
,
508 .device
= uart_console_device
,
509 .setup
= ifxmipsasc_console_setup
,
510 .flags
= CON_PRINTBUFFER
,
512 .data
= &ifxmipsasc_reg
,
515 .write
= ifxmipsasc_console_write
,
516 .device
= uart_console_device
,
517 .setup
= ifxmipsasc_console_setup
,
518 .flags
= CON_PRINTBUFFER
,
520 .data
= &ifxmipsasc_reg
,
524 static int __init
ifxmipsasc_console_init(void)
526 register_console(&ifxmipsasc_console
[0]);
527 register_console(&ifxmipsasc_console
[1]);
530 console_initcall(ifxmipsasc_console_init
);
532 static struct uart_driver ifxmipsasc_reg
= {
533 .owner
= THIS_MODULE
,
534 .driver_name
= "serial",
539 .cons
= &ifxmipsasc_console
[1],
542 int __init
ifxmipsasc_init(void)
545 uart_register_driver(&ifxmipsasc_reg
);
546 ret
= uart_add_one_port(&ifxmipsasc_reg
, &ifxmipsasc_port
[0]);
547 ret
= uart_add_one_port(&ifxmipsasc_reg
, &ifxmipsasc_port
[1]);
551 void __exit
ifxmipsasc_exit(void)
553 uart_unregister_driver(&ifxmipsasc_reg
);
556 module_init(ifxmipsasc_init
);
557 module_exit(ifxmipsasc_exit
);
559 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
560 MODULE_DESCRIPTION("MIPS IFXMips serial port driver");
561 MODULE_LICENSE("GPL");