1 /* drivers/serial/serial_lh7a40x.c
3 * Copyright (C) 2004 Coastal Environmental Systems
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
11 /* Driver for Sharp LH7A40X embedded serial ports
13 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
14 * Based on drivers/serial/amba.c, by Deep Blue Solutions Ltd.
18 * This driver supports the embedded UARTs of the Sharp LH7A40X series
19 * CPUs. While similar to the 16550 and other UART chips, there is
20 * nothing close to register compatibility. Moreover, some of the
21 * modem control lines are not available, either in the chip or they
22 * are lacking in the board-level implementation.
25 * For simplicity, we disable the IR functions of any UART whenever
30 #include <linux/config.h>
32 #if defined(CONFIG_SERIAL_LH7A40X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
36 #include <linux/module.h>
37 #include <linux/ioport.h>
38 #include <linux/init.h>
39 #include <linux/console.h>
40 #include <linux/sysrq.h>
41 #include <linux/tty.h>
42 #include <linux/tty_flip.h>
43 #include <linux/serial_core.h>
44 #include <linux/serial.h>
53 #define ISR_LOOP_LIMIT 256
55 #define UR(p,o) _UR ((p)->membase, o)
56 #define _UR(b,o) (*((volatile unsigned int*)(((unsigned char*) b) + (o))))
57 #define BIT_CLR(p,o,m) UR(p,o) = UR(p,o) & (~(unsigned int)m)
58 #define BIT_SET(p,o,m) UR(p,o) = UR(p,o) | ( (unsigned int)m)
60 #define UART_REG_SIZE 32
62 #define UART_R_DATA (0x00)
63 #define UART_R_FCON (0x04)
64 #define UART_R_BRCON (0x08)
65 #define UART_R_CON (0x0c)
66 #define UART_R_STATUS (0x10)
67 #define UART_R_RAWISR (0x14)
68 #define UART_R_INTEN (0x18)
69 #define UART_R_ISR (0x1c)
71 #define UARTEN (0x01) /* UART enable */
72 #define SIRDIS (0x02) /* Serial IR disable (UART1 only) */
74 #define RxEmpty (0x10)
75 #define TxEmpty (0x80)
77 #define nRxRdy RxEmpty
81 #define RxBreak (0x0800)
82 #define RxOverrunError (0x0400)
83 #define RxParityError (0x0200)
84 #define RxFramingError (0x0100)
85 #define RxError (RxBreak | RxOverrunError | RxParityError | RxFramingError)
93 #define ModemInt (0x04)
94 #define RxTimeoutInt (0x08)
100 #define WLEN_6 (0x20)
101 #define WLEN_5 (0x00)
102 #define WLEN (0x60) /* Mask for all word-length bits */
104 #define PEN (0x02) /* Parity Enable */
105 #define EPS (0x04) /* Even Parity Set */
106 #define FEN (0x10) /* FIFO Enable */
107 #define BRK (0x01) /* Send Break */
110 struct uart_port_lh7a40x
{
111 struct uart_port port
;
112 unsigned int statusPrev
; /* Most recently read modem status */
115 static void lh7a40xuart_stop_tx (struct uart_port
* port
, unsigned int tty_stop
)
117 BIT_CLR (port
, UART_R_INTEN
, TxInt
);
120 static void lh7a40xuart_start_tx (struct uart_port
* port
,
121 unsigned int tty_start
)
123 BIT_SET (port
, UART_R_INTEN
, TxInt
);
125 /* *** FIXME: do I need to check for startup of the
126 transmitter? The old driver did, but AMBA
130 static void lh7a40xuart_stop_rx (struct uart_port
* port
)
132 BIT_SET (port
, UART_R_INTEN
, RxTimeoutInt
| RxInt
);
135 static void lh7a40xuart_enable_ms (struct uart_port
* port
)
137 BIT_SET (port
, UART_R_INTEN
, ModemInt
);
142 lh7a40xuart_rx_chars (struct uart_port
* port
, struct pt_regs
* regs
)
144 lh7a40xuart_rx_chars (struct uart_port
* port
)
147 struct tty_struct
* tty
= port
->info
->tty
;
148 int cbRxMax
= 256; /* (Gross) limit on receive */
149 unsigned int data
, flag
;/* Received data and status */
151 while (!(UR (port
, UART_R_STATUS
) & nRxRdy
) && --cbRxMax
) {
152 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
) {
153 if (tty
->low_latency
)
154 tty_flip_buffer_push(tty
);
156 * If this failed then we will throw away the
157 * bytes but must do so to clear interrupts
161 data
= UR (port
, UART_R_DATA
);
165 if (unlikely(data
& RxError
)) { /* Quick check, short-circuit */
166 if (data
& RxBreak
) {
167 data
&= ~(RxFramingError
| RxParityError
);
169 if (uart_handle_break (port
))
172 else if (data
& RxParityError
)
173 ++port
->icount
.parity
;
174 else if (data
& RxFramingError
)
175 ++port
->icount
.frame
;
176 if (data
& RxOverrunError
)
177 ++port
->icount
.overrun
;
179 /* Mask by termios, leave Rx'd byte */
180 data
&= port
->read_status_mask
| 0xff;
184 else if (data
& RxParityError
)
186 else if (data
& RxFramingError
)
190 if (uart_handle_sysrq_char (port
, (unsigned char) data
, regs
))
193 uart_insert_char(port
, data
, RxOverrunError
, data
, flag
);
195 tty_flip_buffer_push (tty
);
199 static void lh7a40xuart_tx_chars (struct uart_port
* port
)
201 struct circ_buf
* xmit
= &port
->info
->xmit
;
202 int cbTxMax
= port
->fifosize
;
205 UR (port
, UART_R_DATA
) = port
->x_char
;
210 if (uart_circ_empty (xmit
) || uart_tx_stopped (port
)) {
211 lh7a40xuart_stop_tx (port
, 0);
215 /* Unlike the AMBA UART, the lh7a40x UART does not guarantee
216 that at least half of the FIFO is empty. Instead, we check
217 status for every character. Using the AMBA method causes
218 the transmitter to drop characters. */
221 UR (port
, UART_R_DATA
) = xmit
->buf
[xmit
->tail
];
222 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
224 if (uart_circ_empty(xmit
))
226 } while (!(UR (port
, UART_R_STATUS
) & nTxRdy
)
229 if (uart_circ_chars_pending (xmit
) < WAKEUP_CHARS
)
230 uart_write_wakeup (port
);
232 if (uart_circ_empty (xmit
))
233 lh7a40xuart_stop_tx (port
, 0);
236 static void lh7a40xuart_modem_status (struct uart_port
* port
)
238 unsigned int status
= UR (port
, UART_R_STATUS
);
240 = status
^ ((struct uart_port_lh7a40x
*) port
)->statusPrev
;
242 BIT_SET (port
, UART_R_RAWISR
, MSEOI
); /* Clear modem status intr */
244 if (!delta
) /* Only happens if we missed 2 transitions */
247 ((struct uart_port_lh7a40x
*) port
)->statusPrev
= status
;
250 uart_handle_dcd_change (port
, status
& DCD
);
256 uart_handle_cts_change (port
, status
& CTS
);
258 wake_up_interruptible (&port
->info
->delta_msr_wait
);
261 static irqreturn_t
lh7a40xuart_int (int irq
, void* dev_id
,
262 struct pt_regs
* regs
)
264 struct uart_port
* port
= dev_id
;
265 unsigned int cLoopLimit
= ISR_LOOP_LIMIT
;
266 unsigned int isr
= UR (port
, UART_R_ISR
);
270 if (isr
& (RxInt
| RxTimeoutInt
))
272 lh7a40xuart_rx_chars(port
, regs
);
274 lh7a40xuart_rx_chars(port
);
277 lh7a40xuart_modem_status (port
);
279 lh7a40xuart_tx_chars (port
);
281 if (--cLoopLimit
== 0)
284 isr
= UR (port
, UART_R_ISR
);
285 } while (isr
& (RxInt
| TxInt
| RxTimeoutInt
));
290 static unsigned int lh7a40xuart_tx_empty (struct uart_port
* port
)
292 return (UR (port
, UART_R_STATUS
) & TxEmpty
) ? TIOCSER_TEMT
: 0;
295 static unsigned int lh7a40xuart_get_mctrl (struct uart_port
* port
)
297 unsigned int result
= 0;
298 unsigned int status
= UR (port
, UART_R_STATUS
);
310 static void lh7a40xuart_set_mctrl (struct uart_port
* port
, unsigned int mctrl
)
312 /* None of the ports supports DTR. UART1 supports RTS through GPIO. */
313 /* Note, kernel appears to be setting DTR and RTS on console. */
315 /* *** FIXME: this deserves more work. There's some work in
316 tracing all of the IO pins. */
318 if( port
->mapbase
== UART1_PHYS
) {
319 gpioRegs_t
*gpio
= (gpioRegs_t
*)IO_ADDRESS(GPIO_PHYS
);
321 if (mctrl
& TIOCM_RTS
)
322 gpio
->pbdr
&= ~GPIOB_UART1_RTS
;
324 gpio
->pbdr
|= GPIOB_UART1_RTS
;
329 static void lh7a40xuart_break_ctl (struct uart_port
* port
, int break_state
)
333 spin_lock_irqsave(&port
->lock
, flags
);
334 if (break_state
== -1)
335 BIT_SET (port
, UART_R_FCON
, BRK
); /* Assert break */
337 BIT_CLR (port
, UART_R_FCON
, BRK
); /* Deassert break */
338 spin_unlock_irqrestore(&port
->lock
, flags
);
341 static int lh7a40xuart_startup (struct uart_port
* port
)
345 retval
= request_irq (port
->irq
, lh7a40xuart_int
, 0,
346 "serial_lh7a40x", port
);
350 /* Initial modem control-line settings */
351 ((struct uart_port_lh7a40x
*) port
)->statusPrev
352 = UR (port
, UART_R_STATUS
);
354 /* There is presently no configuration option to enable IR.
355 Thus, we always disable it. */
357 BIT_SET (port
, UART_R_CON
, UARTEN
| SIRDIS
);
358 BIT_SET (port
, UART_R_INTEN
, RxTimeoutInt
| RxInt
);
363 static void lh7a40xuart_shutdown (struct uart_port
* port
)
365 free_irq (port
->irq
, port
);
366 BIT_CLR (port
, UART_R_FCON
, BRK
| FEN
);
367 BIT_CLR (port
, UART_R_CON
, UARTEN
);
370 static void lh7a40xuart_set_termios (struct uart_port
* port
,
371 struct termios
* termios
,
381 baud
= uart_get_baud_rate (port
, termios
, old
, 8, port
->uartclk
/16);
382 quot
= uart_get_divisor (port
, baud
); /* -1 performed elsewhere */
384 switch (termios
->c_cflag
& CSIZE
) {
399 if (termios
->c_cflag
& CSTOPB
)
401 if (termios
->c_cflag
& PARENB
) {
403 if (!(termios
->c_cflag
& PARODD
))
406 if (port
->fifosize
> 1)
409 spin_lock_irqsave (&port
->lock
, flags
);
411 uart_update_timeout (port
, termios
->c_cflag
, baud
);
413 port
->read_status_mask
= RxOverrunError
;
414 if (termios
->c_iflag
& INPCK
)
415 port
->read_status_mask
|= RxFramingError
| RxParityError
;
416 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
417 port
->read_status_mask
|= RxBreak
;
419 /* Figure mask for status we ignore */
420 port
->ignore_status_mask
= 0;
421 if (termios
->c_iflag
& IGNPAR
)
422 port
->ignore_status_mask
|= RxFramingError
| RxParityError
;
423 if (termios
->c_iflag
& IGNBRK
) {
424 port
->ignore_status_mask
|= RxBreak
;
425 /* Ignore overrun when ignorning parity */
426 /* *** FIXME: is this in the right place? */
427 if (termios
->c_iflag
& IGNPAR
)
428 port
->ignore_status_mask
|= RxOverrunError
;
431 /* Ignore all receive errors when receive disabled */
432 if ((termios
->c_cflag
& CREAD
) == 0)
433 port
->ignore_status_mask
|= RxError
;
435 con
= UR (port
, UART_R_CON
);
436 inten
= (UR (port
, UART_R_INTEN
) & ~ModemInt
);
438 if (UART_ENABLE_MS (port
, termios
->c_cflag
))
441 BIT_CLR (port
, UART_R_CON
, UARTEN
); /* Disable UART */
442 UR (port
, UART_R_INTEN
) = 0; /* Disable interrupts */
443 UR (port
, UART_R_BRCON
) = quot
- 1; /* Set baud rate divisor */
444 UR (port
, UART_R_FCON
) = fcon
; /* Set FIFO and frame ctrl */
445 UR (port
, UART_R_INTEN
) = inten
; /* Enable interrupts */
446 UR (port
, UART_R_CON
) = con
; /* Restore UART mode */
448 spin_unlock_irqrestore(&port
->lock
, flags
);
451 static const char* lh7a40xuart_type (struct uart_port
* port
)
453 return port
->type
== PORT_LH7A40X
? "LH7A40X" : NULL
;
456 static void lh7a40xuart_release_port (struct uart_port
* port
)
458 release_mem_region (port
->mapbase
, UART_REG_SIZE
);
461 static int lh7a40xuart_request_port (struct uart_port
* port
)
463 return request_mem_region (port
->mapbase
, UART_REG_SIZE
,
464 "serial_lh7a40x") != NULL
468 static void lh7a40xuart_config_port (struct uart_port
* port
, int flags
)
470 if (flags
& UART_CONFIG_TYPE
) {
471 port
->type
= PORT_LH7A40X
;
472 lh7a40xuart_request_port (port
);
476 static int lh7a40xuart_verify_port (struct uart_port
* port
,
477 struct serial_struct
* ser
)
481 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_LH7A40X
)
483 if (ser
->irq
< 0 || ser
->irq
>= NR_IRQS
)
485 if (ser
->baud_base
< 9600) /* *** FIXME: is this true? */
490 static struct uart_ops lh7a40x_uart_ops
= {
491 .tx_empty
= lh7a40xuart_tx_empty
,
492 .set_mctrl
= lh7a40xuart_set_mctrl
,
493 .get_mctrl
= lh7a40xuart_get_mctrl
,
494 .stop_tx
= lh7a40xuart_stop_tx
,
495 .start_tx
= lh7a40xuart_start_tx
,
496 .stop_rx
= lh7a40xuart_stop_rx
,
497 .enable_ms
= lh7a40xuart_enable_ms
,
498 .break_ctl
= lh7a40xuart_break_ctl
,
499 .startup
= lh7a40xuart_startup
,
500 .shutdown
= lh7a40xuart_shutdown
,
501 .set_termios
= lh7a40xuart_set_termios
,
502 .type
= lh7a40xuart_type
,
503 .release_port
= lh7a40xuart_release_port
,
504 .request_port
= lh7a40xuart_request_port
,
505 .config_port
= lh7a40xuart_config_port
,
506 .verify_port
= lh7a40xuart_verify_port
,
509 static struct uart_port_lh7a40x lh7a40x_ports
[DEV_NR
] = {
512 .membase
= (void*) io_p2v (UART1_PHYS
),
513 .mapbase
= UART1_PHYS
,
514 .iotype
= SERIAL_IO_MEM
,
515 .irq
= IRQ_UART1INTR
,
516 .uartclk
= 14745600/2,
518 .ops
= &lh7a40x_uart_ops
,
519 .flags
= ASYNC_BOOT_AUTOCONF
,
525 .membase
= (void*) io_p2v (UART2_PHYS
),
526 .mapbase
= UART2_PHYS
,
527 .iotype
= SERIAL_IO_MEM
,
528 .irq
= IRQ_UART2INTR
,
529 .uartclk
= 14745600/2,
531 .ops
= &lh7a40x_uart_ops
,
532 .flags
= ASYNC_BOOT_AUTOCONF
,
538 .membase
= (void*) io_p2v (UART3_PHYS
),
539 .mapbase
= UART3_PHYS
,
540 .iotype
= SERIAL_IO_MEM
,
541 .irq
= IRQ_UART3INTR
,
542 .uartclk
= 14745600/2,
544 .ops
= &lh7a40x_uart_ops
,
545 .flags
= ASYNC_BOOT_AUTOCONF
,
551 #ifndef CONFIG_SERIAL_LH7A40X_CONSOLE
552 # define LH7A40X_CONSOLE NULL
554 # define LH7A40X_CONSOLE &lh7a40x_console
557 static void lh7a40xuart_console_write (struct console
* co
,
561 struct uart_port
* port
= &lh7a40x_ports
[co
->index
].port
;
562 unsigned int con
= UR (port
, UART_R_CON
);
563 unsigned int inten
= UR (port
, UART_R_INTEN
);
566 UR (port
, UART_R_INTEN
) = 0; /* Disable all interrupts */
567 BIT_SET (port
, UART_R_CON
, UARTEN
| SIRDIS
); /* Enable UART */
569 for (; count
-- > 0; ++s
) {
570 while (UR (port
, UART_R_STATUS
) & nTxRdy
)
572 UR (port
, UART_R_DATA
) = *s
;
574 while ((UR (port
, UART_R_STATUS
) & TxBusy
))
576 UR (port
, UART_R_DATA
) = '\r';
580 /* Wait until all characters are sent */
581 while (UR (port
, UART_R_STATUS
) & TxBusy
)
584 /* Restore control and interrupt mask */
585 UR (port
, UART_R_CON
) = con
;
586 UR (port
, UART_R_INTEN
) = inten
;
589 static void __init
lh7a40xuart_console_get_options (struct uart_port
* port
,
594 if (UR (port
, UART_R_CON
) & UARTEN
) {
595 unsigned int fcon
= UR (port
, UART_R_FCON
);
596 unsigned int quot
= UR (port
, UART_R_BRCON
) + 1;
598 switch (fcon
& (PEN
| EPS
)) {
599 default: *parity
= 'n'; break;
600 case PEN
: *parity
= 'o'; break;
601 case PEN
| EPS
: *parity
= 'e'; break;
604 switch (fcon
& WLEN
) {
606 case WLEN_8
: *bits
= 8; break;
607 case WLEN_7
: *bits
= 7; break;
608 case WLEN_6
: *bits
= 6; break;
609 case WLEN_5
: *bits
= 5; break;
612 *baud
= port
->uartclk
/(16*quot
);
616 static int __init
lh7a40xuart_console_setup (struct console
* co
, char* options
)
618 struct uart_port
* port
;
624 if (co
->index
>= DEV_NR
) /* Bounds check on device number */
626 port
= &lh7a40x_ports
[co
->index
].port
;
629 uart_parse_options (options
, &baud
, &parity
, &bits
, &flow
);
631 lh7a40xuart_console_get_options (port
, &baud
, &parity
, &bits
);
633 return uart_set_options (port
, co
, baud
, parity
, bits
, flow
);
636 extern struct uart_driver lh7a40x_reg
;
637 static struct console lh7a40x_console
= {
639 .write
= lh7a40xuart_console_write
,
640 .device
= uart_console_device
,
641 .setup
= lh7a40xuart_console_setup
,
642 .flags
= CON_PRINTBUFFER
,
644 .data
= &lh7a40x_reg
,
647 static int __init
lh7a40xuart_console_init(void)
649 register_console (&lh7a40x_console
);
653 console_initcall (lh7a40xuart_console_init
);
657 static struct uart_driver lh7a40x_reg
= {
658 .owner
= THIS_MODULE
,
659 .driver_name
= "ttyAM",
664 .cons
= LH7A40X_CONSOLE
,
667 static int __init
lh7a40xuart_init(void)
671 printk (KERN_INFO
"serial: LH7A40X serial driver\n");
673 ret
= uart_register_driver (&lh7a40x_reg
);
678 for (i
= 0; i
< DEV_NR
; i
++)
679 uart_add_one_port (&lh7a40x_reg
,
680 &lh7a40x_ports
[i
].port
);
685 static void __exit
lh7a40xuart_exit(void)
689 for (i
= 0; i
< DEV_NR
; i
++)
690 uart_remove_one_port (&lh7a40x_reg
, &lh7a40x_ports
[i
].port
);
692 uart_unregister_driver (&lh7a40x_reg
);
695 module_init (lh7a40xuart_init
);
696 module_exit (lh7a40xuart_exit
);
698 MODULE_AUTHOR ("Marc Singer");
699 MODULE_DESCRIPTION ("Sharp LH7A40X serial port driver");
700 MODULE_LICENSE ("GPL");