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 */
106 amazonasc_rx_chars(struct uart_port
*port
)
108 struct tty_struct
*tty
= port
->info
->tty
;
109 unsigned int ch
= 0, rsr
= 0, fifocnt
;
111 fifocnt
= amazon_readl(AMAZON_ASC_FSTAT
) & ASCFSTAT_RXFFLMASK
;
114 u8 flag
= TTY_NORMAL
;
115 ch
= amazon_readl(AMAZON_ASC_RBUF
);
116 rsr
= (amazon_readl(AMAZON_ASC_CON
) & ASCCON_ANY
) | UART_DUMMY_UER_RX
;
117 tty_flip_buffer_push(tty
);
121 * Note that the error handling code is
122 * out of the main execution path
124 if (rsr
& ASCCON_ANY
) {
125 if (rsr
& ASCCON_PE
) {
126 port
->icount
.parity
++;
127 amazon_writel_masked(AMAZON_ASC_WHBCON
, ASCWHBCON_CLRPE
, ASCWHBCON_CLRPE
);
128 } else if (rsr
& ASCCON_FE
) {
129 port
->icount
.frame
++;
130 amazon_writel_masked(AMAZON_ASC_WHBCON
, ASCWHBCON_CLRFE
, ASCWHBCON_CLRFE
);
132 if (rsr
& ASCCON_OE
) {
133 port
->icount
.overrun
++;
134 amazon_writel_masked(AMAZON_ASC_WHBCON
, ASCWHBCON_CLROE
, ASCWHBCON_CLROE
);
137 rsr
&= port
->read_status_mask
;
141 else if (rsr
& ASCCON_FE
)
145 if ((rsr
& port
->ignore_status_mask
) == 0)
146 tty_insert_flip_char(tty
, ch
, flag
);
150 * Overrun is special, since it's reported
151 * immediately, and doesn't affect the current
154 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
157 tty_flip_buffer_push(tty
);
163 static void amazonasc_tx_chars(struct uart_port
*port
)
165 struct circ_buf
*xmit
= &port
->info
->xmit
;
167 if (uart_tx_stopped(port
)) {
168 amazonasc_stop_tx(port
);
172 while (((amazon_readl(AMAZON_ASC_FSTAT
) & ASCFSTAT_TXFFLMASK
)
173 >> ASCFSTAT_TXFFLOFF
) != AMAZONASC_TXFIFO_FULL
)
176 amazon_writel(port
->x_char
, AMAZON_ASC_TBUF
);
182 if (uart_circ_empty(xmit
))
185 amazon_writel(xmit
->buf
[xmit
->tail
], AMAZON_ASC_TBUF
);
186 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
190 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
191 uart_write_wakeup(port
);
194 static irqreturn_t
amazonasc_tx_int(int irq
, void *port
)
196 amazon_writel(ASC_IRNCR_TIR
, AMAZON_ASC_IRNCR1
);
197 amazonasc_start_tx(port
);
199 /* clear any pending interrupts */
200 amazon_writel_masked(AMAZON_ASC_WHBCON
,
201 (ASCWHBCON_CLRPE
| ASCWHBCON_CLRFE
| ASCWHBCON_CLROE
),
202 (ASCWHBCON_CLRPE
| ASCWHBCON_CLRFE
| ASCWHBCON_CLROE
));
207 static irqreturn_t
amazonasc_er_int(int irq
, void *port
)
209 /* clear any pending interrupts */
210 amazon_writel_masked(AMAZON_ASC_WHBCON
,
211 (ASCWHBCON_CLRPE
| ASCWHBCON_CLRFE
| ASCWHBCON_CLROE
),
212 (ASCWHBCON_CLRPE
| ASCWHBCON_CLRFE
| ASCWHBCON_CLROE
));
217 static irqreturn_t
amazonasc_rx_int(int irq
, void *port
)
219 amazon_writel(ASC_IRNCR_RIR
, AMAZON_ASC_IRNCR1
);
220 amazonasc_rx_chars((struct uart_port
*) port
);
224 static u_int
amazonasc_tx_empty(struct uart_port
*port
)
229 * FSTAT tells exactly how many bytes are in the FIFO.
230 * The question is whether we really need to wait for all
231 * 16 bytes to be transmitted before reporting that the
232 * transmitter is empty.
234 status
= amazon_readl(AMAZON_ASC_FSTAT
) & ASCFSTAT_TXFFLMASK
;
235 return status
? 0 : TIOCSER_TEMT
;
238 static u_int
amazonasc_get_mctrl(struct uart_port
*port
)
240 /* no modem control signals - the readme says to pretend all are set */
241 return TIOCM_CTS
|TIOCM_CAR
|TIOCM_DSR
;
244 static void amazonasc_set_mctrl(struct uart_port
*port
, u_int mctrl
)
246 /* no modem control - just return */
250 static void amazonasc_break_ctl(struct uart_port
*port
, int break_state
)
252 /* no way to send a break */
256 static int amazonasc_startup(struct uart_port
*port
)
258 unsigned int con
= 0;
262 /* this assumes: CON.BRS = CON.FDE = 0 */
264 uartclk
= amazon_get_fpi_hz();
266 amazonasc_ports
[0].uartclk
= uartclk
;
268 local_irq_save(flags
);
270 /* this setup was probably already done in u-boot */
271 /* ASC and GPIO Port 1 bits 3 and 4 share the same pins
272 * P1.3 (RX) in, Alternate 10
273 * P1.4 (TX) in, Alternate 10
275 amazon_writel_masked(AMAZON_GPIO_P1_DIR
, 0x18, 0x10); //P1.4 output, P1.3 input
276 amazon_writel_masked(AMAZON_GPIO_P1_ALTSEL0
, 0x18, 0x18); //ALTSETL0 11
277 amazon_writel_masked(AMAZON_GPIO_P1_ALTSEL1
, 0x18, 0); //ALTSETL1 00
278 amazon_writel_masked(AMAZON_GPIO_P1_OD
, 0x18, 0x10);
281 amazon_writel_masked(AMAZON_ASC_CLC
, AMAZON_ASC_CLC_DISS
, 0);
282 amazon_writel_masked(AMAZON_ASC_CLC
, ASCCLC_RMCMASK
, 1 << ASCCLC_RMCOFFSET
);
284 /* asynchronous mode */
285 con
= ASCCON_M_8ASYNC
| ASCCON_FEN
| ASCCON_OEN
| ASCCON_PEN
;
287 /* choose the line - there's only one */
288 amazon_writel(0, AMAZON_ASC_PISEL
);
289 amazon_writel(((AMAZONASC_TXFIFO_FL
<< ASCTXFCON_TXFITLOFF
) & ASCTXFCON_TXFITLMASK
) | ASCTXFCON_TXFEN
| ASCTXFCON_TXFFLU
,
291 amazon_writel(((AMAZONASC_RXFIFO_FL
<< ASCRXFCON_RXFITLOFF
) & ASCRXFCON_RXFITLMASK
) | ASCRXFCON_RXFEN
| ASCRXFCON_RXFFLU
,
295 amazon_writel_masked(AMAZON_ASC_CON
, con
, con
);
297 retval
= request_irq(AMAZONASC_RIR
, amazonasc_rx_int
, 0, "asc_rx", port
);
299 printk("failed to request amazonasc_rx_int\n");
302 retval
= request_irq(AMAZONASC_TIR
, amazonasc_tx_int
, 0, "asc_tx", port
);
304 printk("failed to request amazonasc_tx_int\n");
308 retval
= request_irq(AMAZONASC_EIR
, amazonasc_er_int
, 0, "asc_er", port
);
310 printk("failed to request amazonasc_er_int\n");
314 local_irq_restore(flags
);
318 free_irq(AMAZONASC_TIR
, port
);
321 free_irq(AMAZONASC_RIR
, port
);
322 local_irq_restore(flags
);
326 static void amazonasc_shutdown(struct uart_port
*port
)
328 free_irq(AMAZONASC_RIR
, port
);
329 free_irq(AMAZONASC_TIR
, port
);
330 free_irq(AMAZONASC_EIR
, port
);
332 * disable the baudrate generator to disable the ASC
334 amazon_writel(0, AMAZON_ASC_CON
);
336 /* flush and then disable the fifos */
337 amazon_writel_masked(AMAZON_ASC_RXFCON
, ASCRXFCON_RXFFLU
, ASCRXFCON_RXFFLU
);
338 amazon_writel_masked(AMAZON_ASC_RXFCON
, ASCRXFCON_RXFEN
, 0);
339 amazon_writel_masked(AMAZON_ASC_TXFCON
, ASCTXFCON_TXFFLU
, ASCTXFCON_TXFFLU
);
340 amazon_writel_masked(AMAZON_ASC_TXFCON
, ASCTXFCON_TXFEN
, 0);
343 static void amazonasc_set_termios(struct uart_port
*port
, struct ktermios
*new, struct ktermios
*old
)
347 unsigned int baud
, quot
;
348 unsigned int con
= 0;
351 cflag
= new->c_cflag
;
352 iflag
= new->c_iflag
;
354 /* byte size and parity */
355 switch (cflag
& CSIZE
) {
356 /* 7 bits are always with parity */
357 case CS7
: con
= ASCCON_M_7ASYNCPAR
; break;
358 /* the ASC only suports 7 and 8 bits */
363 con
= ASCCON_M_8ASYNCPAR
;
365 con
= ASCCON_M_8ASYNC
;
370 if (cflag
& PARENB
) {
371 if (!(cflag
& PARODD
))
377 port
->read_status_mask
= ASCCON_OE
;
379 port
->read_status_mask
|= ASCCON_FE
| ASCCON_PE
;
381 port
->ignore_status_mask
= 0;
383 port
->ignore_status_mask
|= ASCCON_FE
| ASCCON_PE
;
385 if (iflag
& IGNBRK
) {
387 * If we're ignoring parity and break indicators,
388 * ignore overruns too (for real raw support).
391 port
->ignore_status_mask
|= ASCCON_OE
;
395 * Ignore all characters if CREAD is not set.
397 if ((cflag
& CREAD
) == 0)
398 port
->ignore_status_mask
|= UART_DUMMY_UER_RX
;
400 /* set error signals - framing, parity and overrun */
404 /* enable the receiver */
408 local_irq_save(flags
);
411 amazon_writel(con
, AMAZON_ASC_CON
);
413 /* Set baud rate - take a divider of 2 into account */
414 baud
= uart_get_baud_rate(port
, new, old
, 0, port
->uartclk
/16);
415 quot
= uart_get_divisor(port
, baud
);
418 /* the next 3 probably already happened when we set CON above */
419 /* disable the baudrate generator */
420 amazon_writel_masked(AMAZON_ASC_CON
, ASCCON_R
, 0);
421 /* make sure the fractional divider is off */
422 amazon_writel_masked(AMAZON_ASC_CON
, ASCCON_FDE
, 0);
423 /* set up to use divisor of 2 */
424 amazon_writel_masked(AMAZON_ASC_CON
, ASCCON_BRS
, 0);
425 /* now we can write the new baudrate into the register */
426 amazon_writel(quot
, AMAZON_ASC_BTR
);
427 /* turn the baudrate generator back on */
428 amazon_writel_masked(AMAZON_ASC_CON
, ASCCON_R
, ASCCON_R
);
430 local_irq_restore(flags
);
433 static const char *amazonasc_type(struct uart_port
*port
)
435 return port
->type
== PORT_AMAZONASC
? "AMAZONASC" : NULL
;
439 * Release the memory region(s) being used by 'port'
441 static void amazonasc_release_port(struct uart_port
*port
)
447 * Request the memory region(s) being used by 'port'
449 static int amazonasc_request_port(struct uart_port
*port
)
455 * Configure/autoconfigure the port.
457 static void amazonasc_config_port(struct uart_port
*port
, int flags
)
459 if (flags
& UART_CONFIG_TYPE
) {
460 port
->type
= PORT_AMAZONASC
;
461 amazonasc_request_port(port
);
466 * verify the new serial_struct (for TIOCSSERIAL).
468 static int amazonasc_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
471 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_AMAZONASC
)
473 if (ser
->irq
< 0 || ser
->irq
>= NR_IRQS
)
475 if (ser
->baud_base
< 9600)
480 static struct uart_ops amazonasc_pops
= {
481 .tx_empty
= amazonasc_tx_empty
,
482 .set_mctrl
= amazonasc_set_mctrl
,
483 .get_mctrl
= amazonasc_get_mctrl
,
484 .stop_tx
= amazonasc_stop_tx
,
485 .start_tx
= amazonasc_start_tx
,
486 .stop_rx
= amazonasc_stop_rx
,
487 .enable_ms
= amazonasc_enable_ms
,
488 .break_ctl
= amazonasc_break_ctl
,
489 .startup
= amazonasc_startup
,
490 .shutdown
= amazonasc_shutdown
,
491 .set_termios
= amazonasc_set_termios
,
492 .type
= amazonasc_type
,
493 .release_port
= amazonasc_release_port
,
494 .request_port
= amazonasc_request_port
,
495 .config_port
= amazonasc_config_port
,
496 .verify_port
= amazonasc_verify_port
,
499 static struct uart_port amazonasc_ports
[UART_NR
] = {
501 membase
: (void *)AMAZON_ASC
,
503 iotype
: SERIAL_IO_MEM
,
504 irq
: AMAZONASC_RIR
, /* RIR */
505 uartclk
: 0, /* filled in dynamically */
507 unused
: { AMAZONASC_TIR
, AMAZONASC_EIR
}, /* xmit/error/xmit-buffer-empty IRQ */
508 type
: PORT_AMAZONASC
,
509 ops
: &amazonasc_pops
,
510 flags
: ASYNC_BOOT_AUTOCONF
,
514 static void amazonasc_console_write(struct console
*co
, const char *s
, u_int count
)
518 local_irq_save(flags
);
519 for (i
= 0; i
< count
;)
521 /* wait until the FIFO is not full */
524 fifocnt
= (amazon_readl(AMAZON_ASC_FSTAT
) & ASCFSTAT_TXFFLMASK
)
525 >> ASCFSTAT_TXFFLOFF
;
526 } while (fifocnt
== AMAZONASC_TXFIFO_FULL
);
533 amazon_writel('\r', AMAZON_ASC_TBUF
);
536 fifocnt
= (amazon_readl(AMAZON_ASC_FSTAT
) &
537 ASCFSTAT_TXFFLMASK
) >> ASCFSTAT_TXFFLOFF
;
538 } while (fifocnt
== AMAZONASC_TXFIFO_FULL
);
540 amazon_writel(s
[i
], AMAZON_ASC_TBUF
);
544 local_irq_restore(flags
);
548 amazonasc_console_get_options(struct uart_port
*port
, int *baud
, int *parity
, int *bits
)
552 lcr_h
= amazon_readl(AMAZON_ASC_CON
);
553 /* do this only if the ASC is turned on */
554 if (lcr_h
& ASCCON_R
) {
555 u_int quot
, div
, fdiv
, frac
;
558 if ((lcr_h
& ASCCON_MODEMASK
) == ASCCON_M_7ASYNCPAR
||
559 (lcr_h
& ASCCON_MODEMASK
) == ASCCON_M_8ASYNCPAR
) {
560 if (lcr_h
& ASCCON_ODD
)
566 if ((lcr_h
& ASCCON_MODEMASK
) == ASCCON_M_7ASYNCPAR
)
571 quot
= amazon_readl(AMAZON_ASC_BTR
) + 1;
573 /* this gets hairy if the fractional divider is used */
574 if (lcr_h
& ASCCON_FDE
)
577 fdiv
= amazon_readl(AMAZON_ASC_FDV
);
584 div
= lcr_h
& ASCCON_BRS
? 3 : 2;
588 * This doesn't work exactly because we use integer
589 * math to calculate baud which results in rounding
590 * errors when we try to go from quot -> baud !!
591 * Try to make this work for both the fractional divider
592 * and the simple divider. Also try to avoid rounding
593 * errors using integer math.
596 *baud
= frac
* (port
->uartclk
/ (div
* 512 * 16 * quot
));
597 if (*baud
> 1100 && *baud
< 2400)
599 if (*baud
> 2300 && *baud
< 4800)
601 if (*baud
> 4700 && *baud
< 9600)
603 if (*baud
> 9500 && *baud
< 19200)
605 if (*baud
> 19000 && *baud
< 38400)
607 if (*baud
> 38400 && *baud
< 57600)
609 if (*baud
> 57600 && *baud
< 115200)
611 if (*baud
> 115200 && *baud
< 230400)
616 static int __init
amazonasc_console_setup(struct console
*co
, char *options
)
618 struct uart_port
*port
;
624 /* this assumes: CON.BRS = CON.FDE = 0 */
626 uartclk
= amazon_get_fpi_hz();
628 port
= &amazonasc_ports
[0];
629 amazonasc_ports
[0].uartclk
= uartclk
;
630 amazonasc_ports
[0].type
= PORT_AMAZONASC
;
633 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
636 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
639 static struct uart_driver amazonasc_reg
;
640 static struct console amazonasc_console
= {
642 write
: amazonasc_console_write
,
643 device
: uart_console_device
,
644 setup
: amazonasc_console_setup
,
645 flags
: CON_PRINTBUFFER
,
647 data
: &amazonasc_reg
,
650 static int __init
amazonasc_console_init(void)
652 register_console(&amazonasc_console
);
655 console_initcall(amazonasc_console_init
);
657 static struct uart_driver amazonasc_reg
= {
658 .owner
= THIS_MODULE
,
659 .driver_name
= "serial",
664 .cons
= &amazonasc_console
,
667 static int __init
amazonasc_init(void)
670 uart_register_driver(&amazonasc_reg
);
671 res
= uart_add_one_port(&amazonasc_reg
, &amazonasc_ports
[0]);
675 static void __exit
amazonasc_exit(void)
677 uart_unregister_driver(&amazonasc_reg
);
680 module_init(amazonasc_init
);
681 module_exit(amazonasc_exit
);
683 MODULE_AUTHOR("Gary Jennejohn, Felix Fietkau, John Crispin");
684 MODULE_DESCRIPTION("MIPS AMAZONASC serial port driver");
685 MODULE_LICENSE("GPL");