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
)
117 BIT_CLR (port
, UART_R_INTEN
, TxInt
);
120 static void lh7a40xuart_start_tx (struct uart_port
* port
)
122 BIT_SET (port
, UART_R_INTEN
, TxInt
);
124 /* *** FIXME: do I need to check for startup of the
125 transmitter? The old driver did, but AMBA
129 static void lh7a40xuart_stop_rx (struct uart_port
* port
)
131 BIT_SET (port
, UART_R_INTEN
, RxTimeoutInt
| RxInt
);
134 static void lh7a40xuart_enable_ms (struct uart_port
* port
)
136 BIT_SET (port
, UART_R_INTEN
, ModemInt
);
141 lh7a40xuart_rx_chars (struct uart_port
* port
, struct pt_regs
* regs
)
143 lh7a40xuart_rx_chars (struct uart_port
* port
)
146 struct tty_struct
* tty
= port
->info
->tty
;
147 int cbRxMax
= 256; /* (Gross) limit on receive */
148 unsigned int data
; /* Received data and status */
151 while (!(UR (port
, UART_R_STATUS
) & nRxRdy
) && --cbRxMax
) {
152 data
= UR (port
, UART_R_DATA
);
156 if (unlikely(data
& RxError
)) {
157 if (data
& RxBreak
) {
158 data
&= ~(RxFramingError
| RxParityError
);
160 if (uart_handle_break (port
))
163 else if (data
& RxParityError
)
164 ++port
->icount
.parity
;
165 else if (data
& RxFramingError
)
166 ++port
->icount
.frame
;
167 if (data
& RxOverrunError
)
168 ++port
->icount
.overrun
;
170 /* Mask by termios, leave Rx'd byte */
171 data
&= port
->read_status_mask
| 0xff;
175 else if (data
& RxParityError
)
177 else if (data
& RxFramingError
)
181 if (uart_handle_sysrq_char (port
, (unsigned char) data
, regs
))
184 uart_insert_char(port
, data
, RxOverrunError
, data
, flag
);
186 tty_flip_buffer_push (tty
);
190 static void lh7a40xuart_tx_chars (struct uart_port
* port
)
192 struct circ_buf
* xmit
= &port
->info
->xmit
;
193 int cbTxMax
= port
->fifosize
;
196 UR (port
, UART_R_DATA
) = port
->x_char
;
201 if (uart_circ_empty (xmit
) || uart_tx_stopped (port
)) {
202 lh7a40xuart_stop_tx (port
);
206 /* Unlike the AMBA UART, the lh7a40x UART does not guarantee
207 that at least half of the FIFO is empty. Instead, we check
208 status for every character. Using the AMBA method causes
209 the transmitter to drop characters. */
212 UR (port
, UART_R_DATA
) = xmit
->buf
[xmit
->tail
];
213 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
215 if (uart_circ_empty(xmit
))
217 } while (!(UR (port
, UART_R_STATUS
) & nTxRdy
)
220 if (uart_circ_chars_pending (xmit
) < WAKEUP_CHARS
)
221 uart_write_wakeup (port
);
223 if (uart_circ_empty (xmit
))
224 lh7a40xuart_stop_tx (port
);
227 static void lh7a40xuart_modem_status (struct uart_port
* port
)
229 unsigned int status
= UR (port
, UART_R_STATUS
);
231 = status
^ ((struct uart_port_lh7a40x
*) port
)->statusPrev
;
233 BIT_SET (port
, UART_R_RAWISR
, MSEOI
); /* Clear modem status intr */
235 if (!delta
) /* Only happens if we missed 2 transitions */
238 ((struct uart_port_lh7a40x
*) port
)->statusPrev
= status
;
241 uart_handle_dcd_change (port
, status
& DCD
);
247 uart_handle_cts_change (port
, status
& CTS
);
249 wake_up_interruptible (&port
->info
->delta_msr_wait
);
252 static irqreturn_t
lh7a40xuart_int (int irq
, void* dev_id
,
253 struct pt_regs
* regs
)
255 struct uart_port
* port
= dev_id
;
256 unsigned int cLoopLimit
= ISR_LOOP_LIMIT
;
257 unsigned int isr
= UR (port
, UART_R_ISR
);
261 if (isr
& (RxInt
| RxTimeoutInt
))
263 lh7a40xuart_rx_chars(port
, regs
);
265 lh7a40xuart_rx_chars(port
);
268 lh7a40xuart_modem_status (port
);
270 lh7a40xuart_tx_chars (port
);
272 if (--cLoopLimit
== 0)
275 isr
= UR (port
, UART_R_ISR
);
276 } while (isr
& (RxInt
| TxInt
| RxTimeoutInt
));
281 static unsigned int lh7a40xuart_tx_empty (struct uart_port
* port
)
283 return (UR (port
, UART_R_STATUS
) & TxEmpty
) ? TIOCSER_TEMT
: 0;
286 static unsigned int lh7a40xuart_get_mctrl (struct uart_port
* port
)
288 unsigned int result
= 0;
289 unsigned int status
= UR (port
, UART_R_STATUS
);
301 static void lh7a40xuart_set_mctrl (struct uart_port
* port
, unsigned int mctrl
)
303 /* None of the ports supports DTR. UART1 supports RTS through GPIO. */
304 /* Note, kernel appears to be setting DTR and RTS on console. */
306 /* *** FIXME: this deserves more work. There's some work in
307 tracing all of the IO pins. */
309 if( port
->mapbase
== UART1_PHYS
) {
310 gpioRegs_t
*gpio
= (gpioRegs_t
*)IO_ADDRESS(GPIO_PHYS
);
312 if (mctrl
& TIOCM_RTS
)
313 gpio
->pbdr
&= ~GPIOB_UART1_RTS
;
315 gpio
->pbdr
|= GPIOB_UART1_RTS
;
320 static void lh7a40xuart_break_ctl (struct uart_port
* port
, int break_state
)
324 spin_lock_irqsave(&port
->lock
, flags
);
325 if (break_state
== -1)
326 BIT_SET (port
, UART_R_FCON
, BRK
); /* Assert break */
328 BIT_CLR (port
, UART_R_FCON
, BRK
); /* Deassert break */
329 spin_unlock_irqrestore(&port
->lock
, flags
);
332 static int lh7a40xuart_startup (struct uart_port
* port
)
336 retval
= request_irq (port
->irq
, lh7a40xuart_int
, 0,
337 "serial_lh7a40x", port
);
341 /* Initial modem control-line settings */
342 ((struct uart_port_lh7a40x
*) port
)->statusPrev
343 = UR (port
, UART_R_STATUS
);
345 /* There is presently no configuration option to enable IR.
346 Thus, we always disable it. */
348 BIT_SET (port
, UART_R_CON
, UARTEN
| SIRDIS
);
349 BIT_SET (port
, UART_R_INTEN
, RxTimeoutInt
| RxInt
);
354 static void lh7a40xuart_shutdown (struct uart_port
* port
)
356 free_irq (port
->irq
, port
);
357 BIT_CLR (port
, UART_R_FCON
, BRK
| FEN
);
358 BIT_CLR (port
, UART_R_CON
, UARTEN
);
361 static void lh7a40xuart_set_termios (struct uart_port
* port
,
362 struct termios
* termios
,
372 baud
= uart_get_baud_rate (port
, termios
, old
, 8, port
->uartclk
/16);
373 quot
= uart_get_divisor (port
, baud
); /* -1 performed elsewhere */
375 switch (termios
->c_cflag
& CSIZE
) {
390 if (termios
->c_cflag
& CSTOPB
)
392 if (termios
->c_cflag
& PARENB
) {
394 if (!(termios
->c_cflag
& PARODD
))
397 if (port
->fifosize
> 1)
400 spin_lock_irqsave (&port
->lock
, flags
);
402 uart_update_timeout (port
, termios
->c_cflag
, baud
);
404 port
->read_status_mask
= RxOverrunError
;
405 if (termios
->c_iflag
& INPCK
)
406 port
->read_status_mask
|= RxFramingError
| RxParityError
;
407 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
408 port
->read_status_mask
|= RxBreak
;
410 /* Figure mask for status we ignore */
411 port
->ignore_status_mask
= 0;
412 if (termios
->c_iflag
& IGNPAR
)
413 port
->ignore_status_mask
|= RxFramingError
| RxParityError
;
414 if (termios
->c_iflag
& IGNBRK
) {
415 port
->ignore_status_mask
|= RxBreak
;
416 /* Ignore overrun when ignorning parity */
417 /* *** FIXME: is this in the right place? */
418 if (termios
->c_iflag
& IGNPAR
)
419 port
->ignore_status_mask
|= RxOverrunError
;
422 /* Ignore all receive errors when receive disabled */
423 if ((termios
->c_cflag
& CREAD
) == 0)
424 port
->ignore_status_mask
|= RxError
;
426 con
= UR (port
, UART_R_CON
);
427 inten
= (UR (port
, UART_R_INTEN
) & ~ModemInt
);
429 if (UART_ENABLE_MS (port
, termios
->c_cflag
))
432 BIT_CLR (port
, UART_R_CON
, UARTEN
); /* Disable UART */
433 UR (port
, UART_R_INTEN
) = 0; /* Disable interrupts */
434 UR (port
, UART_R_BRCON
) = quot
- 1; /* Set baud rate divisor */
435 UR (port
, UART_R_FCON
) = fcon
; /* Set FIFO and frame ctrl */
436 UR (port
, UART_R_INTEN
) = inten
; /* Enable interrupts */
437 UR (port
, UART_R_CON
) = con
; /* Restore UART mode */
439 spin_unlock_irqrestore(&port
->lock
, flags
);
442 static const char* lh7a40xuart_type (struct uart_port
* port
)
444 return port
->type
== PORT_LH7A40X
? "LH7A40X" : NULL
;
447 static void lh7a40xuart_release_port (struct uart_port
* port
)
449 release_mem_region (port
->mapbase
, UART_REG_SIZE
);
452 static int lh7a40xuart_request_port (struct uart_port
* port
)
454 return request_mem_region (port
->mapbase
, UART_REG_SIZE
,
455 "serial_lh7a40x") != NULL
459 static void lh7a40xuart_config_port (struct uart_port
* port
, int flags
)
461 if (flags
& UART_CONFIG_TYPE
) {
462 port
->type
= PORT_LH7A40X
;
463 lh7a40xuart_request_port (port
);
467 static int lh7a40xuart_verify_port (struct uart_port
* port
,
468 struct serial_struct
* ser
)
472 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_LH7A40X
)
474 if (ser
->irq
< 0 || ser
->irq
>= NR_IRQS
)
476 if (ser
->baud_base
< 9600) /* *** FIXME: is this true? */
481 static struct uart_ops lh7a40x_uart_ops
= {
482 .tx_empty
= lh7a40xuart_tx_empty
,
483 .set_mctrl
= lh7a40xuart_set_mctrl
,
484 .get_mctrl
= lh7a40xuart_get_mctrl
,
485 .stop_tx
= lh7a40xuart_stop_tx
,
486 .start_tx
= lh7a40xuart_start_tx
,
487 .stop_rx
= lh7a40xuart_stop_rx
,
488 .enable_ms
= lh7a40xuart_enable_ms
,
489 .break_ctl
= lh7a40xuart_break_ctl
,
490 .startup
= lh7a40xuart_startup
,
491 .shutdown
= lh7a40xuart_shutdown
,
492 .set_termios
= lh7a40xuart_set_termios
,
493 .type
= lh7a40xuart_type
,
494 .release_port
= lh7a40xuart_release_port
,
495 .request_port
= lh7a40xuart_request_port
,
496 .config_port
= lh7a40xuart_config_port
,
497 .verify_port
= lh7a40xuart_verify_port
,
500 static struct uart_port_lh7a40x lh7a40x_ports
[DEV_NR
] = {
503 .membase
= (void*) io_p2v (UART1_PHYS
),
504 .mapbase
= UART1_PHYS
,
506 .irq
= IRQ_UART1INTR
,
507 .uartclk
= 14745600/2,
509 .ops
= &lh7a40x_uart_ops
,
510 .flags
= UPF_BOOT_AUTOCONF
,
516 .membase
= (void*) io_p2v (UART2_PHYS
),
517 .mapbase
= UART2_PHYS
,
519 .irq
= IRQ_UART2INTR
,
520 .uartclk
= 14745600/2,
522 .ops
= &lh7a40x_uart_ops
,
523 .flags
= UPF_BOOT_AUTOCONF
,
529 .membase
= (void*) io_p2v (UART3_PHYS
),
530 .mapbase
= UART3_PHYS
,
532 .irq
= IRQ_UART3INTR
,
533 .uartclk
= 14745600/2,
535 .ops
= &lh7a40x_uart_ops
,
536 .flags
= UPF_BOOT_AUTOCONF
,
542 #ifndef CONFIG_SERIAL_LH7A40X_CONSOLE
543 # define LH7A40X_CONSOLE NULL
545 # define LH7A40X_CONSOLE &lh7a40x_console
547 static void lh7a40xuart_console_putchar(struct uart_port
*port
, int ch
)
549 while (UR(port
, UART_R_STATUS
) & nTxRdy
)
551 UR(port
, UART_R_DATA
) = ch
;
554 static void lh7a40xuart_console_write (struct console
* co
,
558 struct uart_port
* port
= &lh7a40x_ports
[co
->index
].port
;
559 unsigned int con
= UR (port
, UART_R_CON
);
560 unsigned int inten
= UR (port
, UART_R_INTEN
);
563 UR (port
, UART_R_INTEN
) = 0; /* Disable all interrupts */
564 BIT_SET (port
, UART_R_CON
, UARTEN
| SIRDIS
); /* Enable UART */
566 uart_console_write(port
, s
, count
, lh7a40xuart_console_putchar
);
568 /* Wait until all characters are sent */
569 while (UR (port
, UART_R_STATUS
) & TxBusy
)
572 /* Restore control and interrupt mask */
573 UR (port
, UART_R_CON
) = con
;
574 UR (port
, UART_R_INTEN
) = inten
;
577 static void __init
lh7a40xuart_console_get_options (struct uart_port
* port
,
582 if (UR (port
, UART_R_CON
) & UARTEN
) {
583 unsigned int fcon
= UR (port
, UART_R_FCON
);
584 unsigned int quot
= UR (port
, UART_R_BRCON
) + 1;
586 switch (fcon
& (PEN
| EPS
)) {
587 default: *parity
= 'n'; break;
588 case PEN
: *parity
= 'o'; break;
589 case PEN
| EPS
: *parity
= 'e'; break;
592 switch (fcon
& WLEN
) {
594 case WLEN_8
: *bits
= 8; break;
595 case WLEN_7
: *bits
= 7; break;
596 case WLEN_6
: *bits
= 6; break;
597 case WLEN_5
: *bits
= 5; break;
600 *baud
= port
->uartclk
/(16*quot
);
604 static int __init
lh7a40xuart_console_setup (struct console
* co
, char* options
)
606 struct uart_port
* port
;
612 if (co
->index
>= DEV_NR
) /* Bounds check on device number */
614 port
= &lh7a40x_ports
[co
->index
].port
;
617 uart_parse_options (options
, &baud
, &parity
, &bits
, &flow
);
619 lh7a40xuart_console_get_options (port
, &baud
, &parity
, &bits
);
621 return uart_set_options (port
, co
, baud
, parity
, bits
, flow
);
624 static struct uart_driver lh7a40x_reg
;
625 static struct console lh7a40x_console
= {
627 .write
= lh7a40xuart_console_write
,
628 .device
= uart_console_device
,
629 .setup
= lh7a40xuart_console_setup
,
630 .flags
= CON_PRINTBUFFER
,
632 .data
= &lh7a40x_reg
,
635 static int __init
lh7a40xuart_console_init(void)
637 register_console (&lh7a40x_console
);
641 console_initcall (lh7a40xuart_console_init
);
645 static struct uart_driver lh7a40x_reg
= {
646 .owner
= THIS_MODULE
,
647 .driver_name
= "ttyAM",
652 .cons
= LH7A40X_CONSOLE
,
655 static int __init
lh7a40xuart_init(void)
659 printk (KERN_INFO
"serial: LH7A40X serial driver\n");
661 ret
= uart_register_driver (&lh7a40x_reg
);
666 for (i
= 0; i
< DEV_NR
; i
++) {
667 /* UART3, when used, requires GPIO pin reallocation */
668 if (lh7a40x_ports
[i
].port
.mapbase
== UART3_PHYS
)
670 uart_add_one_port (&lh7a40x_reg
,
671 &lh7a40x_ports
[i
].port
);
677 static void __exit
lh7a40xuart_exit(void)
681 for (i
= 0; i
< DEV_NR
; i
++)
682 uart_remove_one_port (&lh7a40x_reg
, &lh7a40x_ports
[i
].port
);
684 uart_unregister_driver (&lh7a40x_reg
);
687 module_init (lh7a40xuart_init
);
688 module_exit (lh7a40xuart_exit
);
690 MODULE_AUTHOR ("Marc Singer");
691 MODULE_DESCRIPTION ("Sharp LH7A40X serial port driver");
692 MODULE_LICENSE ("GPL");