2 * Driver for AMAZONASC serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 * Based on drivers/serial/serial_s3c2400.c
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Copyright (C) 2004 Infineon IFAP DC COM CPE
22 * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
23 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
26 #include <linux/module.h>
27 #include <linux/errno.h>
28 #include <linux/signal.h>
29 #include <linux/sched.h>
30 #include <linux/interrupt.h>
31 #include <linux/tty.h>
32 #include <linux/tty_flip.h>
33 #include <linux/major.h>
34 #include <linux/string.h>
35 #include <linux/fcntl.h>
36 #include <linux/ptrace.h>
37 #include <linux/ioport.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/circ_buf.h>
42 #include <linux/serial.h>
43 #include <linux/serial_core.h>
44 #include <linux/console.h>
45 #include <linux/sysrq.h>
46 #include <linux/irq.h>
48 #include <asm/system.h>
50 #include <asm/uaccess.h>
51 #include <asm/bitops.h>
52 #include <asm/amazon/amazon.h>
53 #include <asm/amazon/irq.h>
54 #include <asm/amazon/serial.h>
56 #define PORT_AMAZONASC 111
58 #include <linux/serial_core.h>
62 #define UART_DUMMY_UER_RX 1
64 #define SERIAL_AMAZONASC_MAJOR TTY_MAJOR
65 #define CALLOUT_AMAZONASC_MAJOR TTYAUX_MAJOR
66 #define SERIAL_AMAZONASC_MINOR 64
67 #define SERIAL_AMAZONASC_NR UART_NR
69 static void amazonasc_tx_chars(struct uart_port
*port
);
70 extern void prom_printf(const char * fmt
, ...);
71 static struct uart_port amazonasc_ports
[UART_NR
];
72 static struct uart_driver amazonasc_reg
;
73 static unsigned int uartclk
= 0;
74 extern unsigned int amazon_get_fpi_hz(void);
76 static void amazonasc_stop_tx(struct uart_port
*port
)
78 /* fifo underrun shuts up after firing once */
82 static void amazonasc_start_tx(struct uart_port
*port
)
86 local_irq_save(flags
);
87 amazonasc_tx_chars(port
);
88 local_irq_restore(flags
);
93 static void amazonasc_stop_rx(struct uart_port
*port
)
95 /* clear the RX enable bit */
96 amazon_writel(ASCWHBCON_CLRREN
, AMAZON_ASC_WHBCON
);
99 static void amazonasc_enable_ms(struct uart_port
*port
)
101 /* no modem signals */
105 #include <linux/version.h>
108 amazonasc_rx_chars(struct uart_port
*port
)
110 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 26))
111 struct tty_struct
*tty
= port
->info
->port
.tty
;
113 struct tty_struct
*tty
= port
->info
->tty
;
115 unsigned int ch
= 0, rsr
= 0, fifocnt
;
117 fifocnt
= amazon_readl(AMAZON_ASC_FSTAT
) & ASCFSTAT_RXFFLMASK
;
120 u8 flag
= TTY_NORMAL
;
121 ch
= amazon_readl(AMAZON_ASC_RBUF
);
122 rsr
= (amazon_readl(AMAZON_ASC_CON
) & ASCCON_ANY
) | UART_DUMMY_UER_RX
;
123 tty_flip_buffer_push(tty
);
127 * Note that the error handling code is
128 * out of the main execution path
130 if (rsr
& ASCCON_ANY
) {
131 if (rsr
& ASCCON_PE
) {
132 port
->icount
.parity
++;
133 amazon_writel_masked(AMAZON_ASC_WHBCON
, ASCWHBCON_CLRPE
, ASCWHBCON_CLRPE
);
134 } else if (rsr
& ASCCON_FE
) {
135 port
->icount
.frame
++;
136 amazon_writel_masked(AMAZON_ASC_WHBCON
, ASCWHBCON_CLRFE
, ASCWHBCON_CLRFE
);
138 if (rsr
& ASCCON_OE
) {
139 port
->icount
.overrun
++;
140 amazon_writel_masked(AMAZON_ASC_WHBCON
, ASCWHBCON_CLROE
, ASCWHBCON_CLROE
);
143 rsr
&= port
->read_status_mask
;
147 else if (rsr
& ASCCON_FE
)
151 if ((rsr
& port
->ignore_status_mask
) == 0)
152 tty_insert_flip_char(tty
, ch
, flag
);
156 * Overrun is special, since it's reported
157 * immediately, and doesn't affect the current
160 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
163 tty_flip_buffer_push(tty
);
169 static void amazonasc_tx_chars(struct uart_port
*port
)
171 struct circ_buf
*xmit
= &port
->info
->xmit
;
173 if (uart_tx_stopped(port
)) {
174 amazonasc_stop_tx(port
);
178 while (((amazon_readl(AMAZON_ASC_FSTAT
) & ASCFSTAT_TXFFLMASK
)
179 >> ASCFSTAT_TXFFLOFF
) != AMAZONASC_TXFIFO_FULL
)
182 amazon_writel(port
->x_char
, AMAZON_ASC_TBUF
);
188 if (uart_circ_empty(xmit
))
191 amazon_writel(xmit
->buf
[xmit
->tail
], AMAZON_ASC_TBUF
);
192 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
196 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
197 uart_write_wakeup(port
);
200 static irqreturn_t
amazonasc_tx_int(int irq
, void *port
)
202 amazon_writel(ASC_IRNCR_TIR
, AMAZON_ASC_IRNCR1
);
203 amazonasc_start_tx(port
);
205 /* clear any pending interrupts */
206 amazon_writel_masked(AMAZON_ASC_WHBCON
,
207 (ASCWHBCON_CLRPE
| ASCWHBCON_CLRFE
| ASCWHBCON_CLROE
),
208 (ASCWHBCON_CLRPE
| ASCWHBCON_CLRFE
| ASCWHBCON_CLROE
));
213 static irqreturn_t
amazonasc_er_int(int irq
, void *port
)
215 /* clear any pending interrupts */
216 amazon_writel_masked(AMAZON_ASC_WHBCON
,
217 (ASCWHBCON_CLRPE
| ASCWHBCON_CLRFE
| ASCWHBCON_CLROE
),
218 (ASCWHBCON_CLRPE
| ASCWHBCON_CLRFE
| ASCWHBCON_CLROE
));
223 static irqreturn_t
amazonasc_rx_int(int irq
, void *port
)
225 amazon_writel(ASC_IRNCR_RIR
, AMAZON_ASC_IRNCR1
);
226 amazonasc_rx_chars((struct uart_port
*) port
);
230 static u_int
amazonasc_tx_empty(struct uart_port
*port
)
235 * FSTAT tells exactly how many bytes are in the FIFO.
236 * The question is whether we really need to wait for all
237 * 16 bytes to be transmitted before reporting that the
238 * transmitter is empty.
240 status
= amazon_readl(AMAZON_ASC_FSTAT
) & ASCFSTAT_TXFFLMASK
;
241 return status
? 0 : TIOCSER_TEMT
;
244 static u_int
amazonasc_get_mctrl(struct uart_port
*port
)
246 /* no modem control signals - the readme says to pretend all are set */
247 return TIOCM_CTS
|TIOCM_CAR
|TIOCM_DSR
;
250 static void amazonasc_set_mctrl(struct uart_port
*port
, u_int mctrl
)
252 /* no modem control - just return */
256 static void amazonasc_break_ctl(struct uart_port
*port
, int break_state
)
258 /* no way to send a break */
262 static int amazonasc_startup(struct uart_port
*port
)
264 unsigned int con
= 0;
268 /* this assumes: CON.BRS = CON.FDE = 0 */
270 uartclk
= amazon_get_fpi_hz();
272 amazonasc_ports
[0].uartclk
= uartclk
;
274 local_irq_save(flags
);
276 /* this setup was probably already done in u-boot */
277 /* ASC and GPIO Port 1 bits 3 and 4 share the same pins
278 * P1.3 (RX) in, Alternate 10
279 * P1.4 (TX) in, Alternate 10
281 amazon_writel_masked(AMAZON_GPIO_P1_DIR
, 0x18, 0x10); //P1.4 output, P1.3 input
282 amazon_writel_masked(AMAZON_GPIO_P1_ALTSEL0
, 0x18, 0x18); //ALTSETL0 11
283 amazon_writel_masked(AMAZON_GPIO_P1_ALTSEL1
, 0x18, 0); //ALTSETL1 00
284 amazon_writel_masked(AMAZON_GPIO_P1_OD
, 0x18, 0x10);
287 amazon_writel_masked(AMAZON_ASC_CLC
, AMAZON_ASC_CLC_DISS
, 0);
288 amazon_writel_masked(AMAZON_ASC_CLC
, ASCCLC_RMCMASK
, 1 << ASCCLC_RMCOFFSET
);
290 /* asynchronous mode */
291 con
= ASCCON_M_8ASYNC
| ASCCON_FEN
| ASCCON_OEN
| ASCCON_PEN
;
293 /* choose the line - there's only one */
294 amazon_writel(0, AMAZON_ASC_PISEL
);
295 amazon_writel(((AMAZONASC_TXFIFO_FL
<< ASCTXFCON_TXFITLOFF
) & ASCTXFCON_TXFITLMASK
) | ASCTXFCON_TXFEN
| ASCTXFCON_TXFFLU
,
297 amazon_writel(((AMAZONASC_RXFIFO_FL
<< ASCRXFCON_RXFITLOFF
) & ASCRXFCON_RXFITLMASK
) | ASCRXFCON_RXFEN
| ASCRXFCON_RXFFLU
,
301 amazon_writel_masked(AMAZON_ASC_CON
, con
, con
);
303 retval
= request_irq(AMAZONASC_RIR
, amazonasc_rx_int
, 0, "asc_rx", port
);
305 printk("failed to request amazonasc_rx_int\n");
308 retval
= request_irq(AMAZONASC_TIR
, amazonasc_tx_int
, 0, "asc_tx", port
);
310 printk("failed to request amazonasc_tx_int\n");
314 retval
= request_irq(AMAZONASC_EIR
, amazonasc_er_int
, 0, "asc_er", port
);
316 printk("failed to request amazonasc_er_int\n");
320 local_irq_restore(flags
);
324 free_irq(AMAZONASC_TIR
, port
);
327 free_irq(AMAZONASC_RIR
, port
);
328 local_irq_restore(flags
);
332 static void amazonasc_shutdown(struct uart_port
*port
)
334 free_irq(AMAZONASC_RIR
, port
);
335 free_irq(AMAZONASC_TIR
, port
);
336 free_irq(AMAZONASC_EIR
, port
);
338 * disable the baudrate generator to disable the ASC
340 amazon_writel(0, AMAZON_ASC_CON
);
342 /* flush and then disable the fifos */
343 amazon_writel_masked(AMAZON_ASC_RXFCON
, ASCRXFCON_RXFFLU
, ASCRXFCON_RXFFLU
);
344 amazon_writel_masked(AMAZON_ASC_RXFCON
, ASCRXFCON_RXFEN
, 0);
345 amazon_writel_masked(AMAZON_ASC_TXFCON
, ASCTXFCON_TXFFLU
, ASCTXFCON_TXFFLU
);
346 amazon_writel_masked(AMAZON_ASC_TXFCON
, ASCTXFCON_TXFEN
, 0);
349 static void amazonasc_set_termios(struct uart_port
*port
, struct ktermios
*new, struct ktermios
*old
)
353 unsigned int baud
, quot
;
354 unsigned int con
= 0;
357 cflag
= new->c_cflag
;
358 iflag
= new->c_iflag
;
360 /* byte size and parity */
361 switch (cflag
& CSIZE
) {
362 /* 7 bits are always with parity */
363 case CS7
: con
= ASCCON_M_7ASYNCPAR
; break;
364 /* the ASC only suports 7 and 8 bits */
369 con
= ASCCON_M_8ASYNCPAR
;
371 con
= ASCCON_M_8ASYNC
;
376 if (cflag
& PARENB
) {
377 if (!(cflag
& PARODD
))
383 port
->read_status_mask
= ASCCON_OE
;
385 port
->read_status_mask
|= ASCCON_FE
| ASCCON_PE
;
387 port
->ignore_status_mask
= 0;
389 port
->ignore_status_mask
|= ASCCON_FE
| ASCCON_PE
;
391 if (iflag
& IGNBRK
) {
393 * If we're ignoring parity and break indicators,
394 * ignore overruns too (for real raw support).
397 port
->ignore_status_mask
|= ASCCON_OE
;
401 * Ignore all characters if CREAD is not set.
403 if ((cflag
& CREAD
) == 0)
404 port
->ignore_status_mask
|= UART_DUMMY_UER_RX
;
406 /* set error signals - framing, parity and overrun */
410 /* enable the receiver */
414 local_irq_save(flags
);
417 amazon_writel(con
, AMAZON_ASC_CON
);
419 /* Set baud rate - take a divider of 2 into account */
420 baud
= uart_get_baud_rate(port
, new, old
, 0, port
->uartclk
/16);
421 quot
= uart_get_divisor(port
, baud
);
424 /* the next 3 probably already happened when we set CON above */
425 /* disable the baudrate generator */
426 amazon_writel_masked(AMAZON_ASC_CON
, ASCCON_R
, 0);
427 /* make sure the fractional divider is off */
428 amazon_writel_masked(AMAZON_ASC_CON
, ASCCON_FDE
, 0);
429 /* set up to use divisor of 2 */
430 amazon_writel_masked(AMAZON_ASC_CON
, ASCCON_BRS
, 0);
431 /* now we can write the new baudrate into the register */
432 amazon_writel(quot
, AMAZON_ASC_BTR
);
433 /* turn the baudrate generator back on */
434 amazon_writel_masked(AMAZON_ASC_CON
, ASCCON_R
, ASCCON_R
);
436 local_irq_restore(flags
);
439 static const char *amazonasc_type(struct uart_port
*port
)
441 return port
->type
== PORT_AMAZONASC
? "AMAZONASC" : NULL
;
445 * Release the memory region(s) being used by 'port'
447 static void amazonasc_release_port(struct uart_port
*port
)
453 * Request the memory region(s) being used by 'port'
455 static int amazonasc_request_port(struct uart_port
*port
)
461 * Configure/autoconfigure the port.
463 static void amazonasc_config_port(struct uart_port
*port
, int flags
)
465 if (flags
& UART_CONFIG_TYPE
) {
466 port
->type
= PORT_AMAZONASC
;
467 amazonasc_request_port(port
);
472 * verify the new serial_struct (for TIOCSSERIAL).
474 static int amazonasc_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
477 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_AMAZONASC
)
479 if (ser
->irq
< 0 || ser
->irq
>= NR_IRQS
)
481 if (ser
->baud_base
< 9600)
486 static struct uart_ops amazonasc_pops
= {
487 .tx_empty
= amazonasc_tx_empty
,
488 .set_mctrl
= amazonasc_set_mctrl
,
489 .get_mctrl
= amazonasc_get_mctrl
,
490 .stop_tx
= amazonasc_stop_tx
,
491 .start_tx
= amazonasc_start_tx
,
492 .stop_rx
= amazonasc_stop_rx
,
493 .enable_ms
= amazonasc_enable_ms
,
494 .break_ctl
= amazonasc_break_ctl
,
495 .startup
= amazonasc_startup
,
496 .shutdown
= amazonasc_shutdown
,
497 .set_termios
= amazonasc_set_termios
,
498 .type
= amazonasc_type
,
499 .release_port
= amazonasc_release_port
,
500 .request_port
= amazonasc_request_port
,
501 .config_port
= amazonasc_config_port
,
502 .verify_port
= amazonasc_verify_port
,
505 static struct uart_port amazonasc_ports
[UART_NR
] = {
507 membase
: (void *)AMAZON_ASC
,
509 iotype
: SERIAL_IO_MEM
,
510 irq
: AMAZONASC_RIR
, /* RIR */
511 uartclk
: 0, /* filled in dynamically */
513 unused
: { AMAZONASC_TIR
, AMAZONASC_EIR
}, /* xmit/error/xmit-buffer-empty IRQ */
514 type
: PORT_AMAZONASC
,
515 ops
: &amazonasc_pops
,
516 flags
: ASYNC_BOOT_AUTOCONF
,
520 static void amazonasc_console_write(struct console
*co
, const char *s
, u_int count
)
524 local_irq_save(flags
);
525 for (i
= 0; i
< count
;)
527 /* wait until the FIFO is not full */
530 fifocnt
= (amazon_readl(AMAZON_ASC_FSTAT
) & ASCFSTAT_TXFFLMASK
)
531 >> ASCFSTAT_TXFFLOFF
;
532 } while (fifocnt
== AMAZONASC_TXFIFO_FULL
);
539 amazon_writel('\r', AMAZON_ASC_TBUF
);
542 fifocnt
= (amazon_readl(AMAZON_ASC_FSTAT
) &
543 ASCFSTAT_TXFFLMASK
) >> ASCFSTAT_TXFFLOFF
;
544 } while (fifocnt
== AMAZONASC_TXFIFO_FULL
);
546 amazon_writel(s
[i
], AMAZON_ASC_TBUF
);
550 local_irq_restore(flags
);
554 amazonasc_console_get_options(struct uart_port
*port
, int *baud
, int *parity
, int *bits
)
558 lcr_h
= amazon_readl(AMAZON_ASC_CON
);
559 /* do this only if the ASC is turned on */
560 if (lcr_h
& ASCCON_R
) {
561 u_int quot
, div
, fdiv
, frac
;
564 if ((lcr_h
& ASCCON_MODEMASK
) == ASCCON_M_7ASYNCPAR
||
565 (lcr_h
& ASCCON_MODEMASK
) == ASCCON_M_8ASYNCPAR
) {
566 if (lcr_h
& ASCCON_ODD
)
572 if ((lcr_h
& ASCCON_MODEMASK
) == ASCCON_M_7ASYNCPAR
)
577 quot
= amazon_readl(AMAZON_ASC_BTR
) + 1;
579 /* this gets hairy if the fractional divider is used */
580 if (lcr_h
& ASCCON_FDE
)
583 fdiv
= amazon_readl(AMAZON_ASC_FDV
);
590 div
= lcr_h
& ASCCON_BRS
? 3 : 2;
594 * This doesn't work exactly because we use integer
595 * math to calculate baud which results in rounding
596 * errors when we try to go from quot -> baud !!
597 * Try to make this work for both the fractional divider
598 * and the simple divider. Also try to avoid rounding
599 * errors using integer math.
602 *baud
= frac
* (port
->uartclk
/ (div
* 512 * 16 * quot
));
603 if (*baud
> 1100 && *baud
< 2400)
605 if (*baud
> 2300 && *baud
< 4800)
607 if (*baud
> 4700 && *baud
< 9600)
609 if (*baud
> 9500 && *baud
< 19200)
611 if (*baud
> 19000 && *baud
< 38400)
613 if (*baud
> 38400 && *baud
< 57600)
615 if (*baud
> 57600 && *baud
< 115200)
617 if (*baud
> 115200 && *baud
< 230400)
622 static int __init
amazonasc_console_setup(struct console
*co
, char *options
)
624 struct uart_port
*port
;
630 /* this assumes: CON.BRS = CON.FDE = 0 */
632 uartclk
= amazon_get_fpi_hz();
634 port
= &amazonasc_ports
[0];
635 amazonasc_ports
[0].uartclk
= uartclk
;
636 amazonasc_ports
[0].type
= PORT_AMAZONASC
;
639 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
642 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
645 static struct uart_driver amazonasc_reg
;
646 static struct console amazonasc_console
= {
648 write
: amazonasc_console_write
,
649 device
: uart_console_device
,
650 setup
: amazonasc_console_setup
,
651 flags
: CON_PRINTBUFFER
,
653 data
: &amazonasc_reg
,
656 static int __init
amazonasc_console_init(void)
658 register_console(&amazonasc_console
);
661 console_initcall(amazonasc_console_init
);
663 static struct uart_driver amazonasc_reg
= {
664 .owner
= THIS_MODULE
,
665 .driver_name
= "serial",
670 .cons
= &amazonasc_console
,
673 static int __init
amazonasc_init(void)
676 uart_register_driver(&amazonasc_reg
);
677 res
= uart_add_one_port(&amazonasc_reg
, &amazonasc_ports
[0]);
681 static void __exit
amazonasc_exit(void)
683 uart_unregister_driver(&amazonasc_reg
);
686 module_init(amazonasc_init
);
687 module_exit(amazonasc_exit
);
689 MODULE_AUTHOR("Gary Jennejohn, Felix Fietkau, John Crispin");
690 MODULE_DESCRIPTION("MIPS AMAZONASC serial port driver");
691 MODULE_LICENSE("GPL");