2 * Driver for KS8695 serial ports
4 * Based on drivers/serial/serial_amba.c, by Kam Lee.
6 * Copyright 2002-2005 Micrel Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/ioport.h>
18 #include <linux/init.h>
19 #include <linux/serial.h>
20 #include <linux/console.h>
21 #include <linux/sysrq.h>
22 #include <linux/device.h>
26 #include <asm/mach/irq.h>
28 #include <mach/regs-uart.h>
29 #include <mach/regs-irq.h>
31 #if defined(CONFIG_SERIAL_KS8695_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
35 #include <linux/serial_core.h>
38 #define SERIAL_KS8695_MAJOR 204
39 #define SERIAL_KS8695_MINOR 16
40 #define SERIAL_KS8695_DEVNAME "ttyAM"
42 #define SERIAL_KS8695_NR 1
45 * Access macros for the KS8695 UART
47 #define UART_GET_CHAR(p) (__raw_readl((p)->membase + KS8695_URRB) & 0xFF)
48 #define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + KS8695_URTH)
49 #define UART_GET_FCR(p) __raw_readl((p)->membase + KS8695_URFC)
50 #define UART_PUT_FCR(p, c) __raw_writel((c), (p)->membase + KS8695_URFC)
51 #define UART_GET_MSR(p) __raw_readl((p)->membase + KS8695_URMS)
52 #define UART_GET_LSR(p) __raw_readl((p)->membase + KS8695_URLS)
53 #define UART_GET_LCR(p) __raw_readl((p)->membase + KS8695_URLC)
54 #define UART_PUT_LCR(p, c) __raw_writel((c), (p)->membase + KS8695_URLC)
55 #define UART_GET_MCR(p) __raw_readl((p)->membase + KS8695_URMC)
56 #define UART_PUT_MCR(p, c) __raw_writel((c), (p)->membase + KS8695_URMC)
57 #define UART_GET_BRDR(p) __raw_readl((p)->membase + KS8695_URBD)
58 #define UART_PUT_BRDR(p, c) __raw_writel((c), (p)->membase + KS8695_URBD)
60 #define KS8695_CLR_TX_INT() __raw_writel(1 << KS8695_IRQ_UART_TX, KS8695_IRQ_VA + KS8695_INTST)
62 #define UART_DUMMY_LSR_RX 0x100
63 #define UART_PORT_SIZE (KS8695_USR - KS8695_URRB + 4)
65 static inline int tx_enabled(struct uart_port
*port
)
67 return port
->unused
[0] & 1;
70 static inline int rx_enabled(struct uart_port
*port
)
72 return port
->unused
[0] & 2;
75 static inline int ms_enabled(struct uart_port
*port
)
77 return port
->unused
[0] & 4;
80 static inline void ms_enable(struct uart_port
*port
, int enabled
)
85 port
->unused
[0] &= ~4;
88 static inline void rx_enable(struct uart_port
*port
, int enabled
)
93 port
->unused
[0] &= ~2;
96 static inline void tx_enable(struct uart_port
*port
, int enabled
)
101 port
->unused
[0] &= ~1;
106 static struct console ks8695_console
;
109 static void ks8695uart_stop_tx(struct uart_port
*port
)
111 if (tx_enabled(port
)) {
112 /* use disable_irq_nosync() and not disable_irq() to avoid self
113 * imposed deadlock by not waiting for irq handler to end,
114 * since this ks8695uart_stop_tx() is called from interrupt context.
116 disable_irq_nosync(KS8695_IRQ_UART_TX
);
121 static void ks8695uart_start_tx(struct uart_port
*port
)
123 if (!tx_enabled(port
)) {
124 enable_irq(KS8695_IRQ_UART_TX
);
129 static void ks8695uart_stop_rx(struct uart_port
*port
)
131 if (rx_enabled(port
)) {
132 disable_irq(KS8695_IRQ_UART_RX
);
137 static void ks8695uart_enable_ms(struct uart_port
*port
)
139 if (!ms_enabled(port
)) {
140 enable_irq(KS8695_IRQ_UART_MODEM_STATUS
);
145 static void ks8695uart_disable_ms(struct uart_port
*port
)
147 if (ms_enabled(port
)) {
148 disable_irq(KS8695_IRQ_UART_MODEM_STATUS
);
153 static irqreturn_t
ks8695uart_rx_chars(int irq
, void *dev_id
)
155 struct uart_port
*port
= dev_id
;
156 unsigned int status
, ch
, lsr
, flg
, max_count
= 256;
158 status
= UART_GET_LSR(port
); /* clears pending LSR interrupts */
159 while ((status
& URLS_URDR
) && max_count
--) {
160 ch
= UART_GET_CHAR(port
);
166 * Note that the error handling code is
167 * out of the main execution path
169 lsr
= UART_GET_LSR(port
) | UART_DUMMY_LSR_RX
;
170 if (unlikely(lsr
& (URLS_URBI
| URLS_URPE
| URLS_URFE
| URLS_URROE
))) {
171 if (lsr
& URLS_URBI
) {
172 lsr
&= ~(URLS_URFE
| URLS_URPE
);
174 if (uart_handle_break(port
))
178 port
->icount
.parity
++;
180 port
->icount
.frame
++;
181 if (lsr
& URLS_URROE
)
182 port
->icount
.overrun
++;
184 lsr
&= port
->read_status_mask
;
188 else if (lsr
& URLS_URPE
)
190 else if (lsr
& URLS_URFE
)
194 if (uart_handle_sysrq_char(port
, ch
))
197 uart_insert_char(port
, lsr
, URLS_URROE
, ch
, flg
);
200 status
= UART_GET_LSR(port
);
202 tty_flip_buffer_push(&port
->state
->port
);
208 static irqreturn_t
ks8695uart_tx_chars(int irq
, void *dev_id
)
210 struct uart_port
*port
= dev_id
;
211 struct circ_buf
*xmit
= &port
->state
->xmit
;
216 UART_PUT_CHAR(port
, port
->x_char
);
222 if (uart_tx_stopped(port
) || uart_circ_empty(xmit
)) {
223 ks8695uart_stop_tx(port
);
227 count
= 16; /* fifo size */
228 while (!uart_circ_empty(xmit
) && (count
-- > 0)) {
230 UART_PUT_CHAR(port
, xmit
->buf
[xmit
->tail
]);
232 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
236 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
237 uart_write_wakeup(port
);
239 if (uart_circ_empty(xmit
))
240 ks8695uart_stop_tx(port
);
245 static irqreturn_t
ks8695uart_modem_status(int irq
, void *dev_id
)
247 struct uart_port
*port
= dev_id
;
251 * clear modem interrupt by reading MSR
253 status
= UART_GET_MSR(port
);
255 if (status
& URMS_URDDCD
)
256 uart_handle_dcd_change(port
, status
& URMS_URDDCD
);
258 if (status
& URMS_URDDST
)
261 if (status
& URMS_URDCTS
)
262 uart_handle_cts_change(port
, status
& URMS_URDCTS
);
264 if (status
& URMS_URTERI
)
267 wake_up_interruptible(&port
->state
->port
.delta_msr_wait
);
272 static unsigned int ks8695uart_tx_empty(struct uart_port
*port
)
274 return (UART_GET_LSR(port
) & URLS_URTE
) ? TIOCSER_TEMT
: 0;
277 static unsigned int ks8695uart_get_mctrl(struct uart_port
*port
)
279 unsigned int result
= 0;
282 status
= UART_GET_MSR(port
);
283 if (status
& URMS_URDCD
)
285 if (status
& URMS_URDSR
)
287 if (status
& URMS_URCTS
)
289 if (status
& URMS_URRI
)
295 static void ks8695uart_set_mctrl(struct uart_port
*port
, u_int mctrl
)
299 mcr
= UART_GET_MCR(port
);
300 if (mctrl
& TIOCM_RTS
)
305 if (mctrl
& TIOCM_DTR
)
310 UART_PUT_MCR(port
, mcr
);
313 static void ks8695uart_break_ctl(struct uart_port
*port
, int break_state
)
317 lcr
= UART_GET_LCR(port
);
319 if (break_state
== -1)
324 UART_PUT_LCR(port
, lcr
);
327 static int ks8695uart_startup(struct uart_port
*port
)
331 set_irq_flags(KS8695_IRQ_UART_TX
, IRQF_VALID
| IRQF_NOAUTOEN
);
339 retval
= request_irq(KS8695_IRQ_UART_TX
, ks8695uart_tx_chars
, 0, "UART TX", port
);
343 retval
= request_irq(KS8695_IRQ_UART_RX
, ks8695uart_rx_chars
, 0, "UART RX", port
);
347 retval
= request_irq(KS8695_IRQ_UART_LINE_STATUS
, ks8695uart_rx_chars
, 0, "UART LineStatus", port
);
351 retval
= request_irq(KS8695_IRQ_UART_MODEM_STATUS
, ks8695uart_modem_status
, 0, "UART ModemStatus", port
);
358 free_irq(KS8695_IRQ_UART_LINE_STATUS
, port
);
360 free_irq(KS8695_IRQ_UART_RX
, port
);
362 free_irq(KS8695_IRQ_UART_TX
, port
);
367 static void ks8695uart_shutdown(struct uart_port
*port
)
372 free_irq(KS8695_IRQ_UART_RX
, port
);
373 free_irq(KS8695_IRQ_UART_TX
, port
);
374 free_irq(KS8695_IRQ_UART_MODEM_STATUS
, port
);
375 free_irq(KS8695_IRQ_UART_LINE_STATUS
, port
);
377 /* disable break condition and fifos */
378 UART_PUT_LCR(port
, UART_GET_LCR(port
) & ~URLC_URSBC
);
379 UART_PUT_FCR(port
, UART_GET_FCR(port
) & ~URFC_URFE
);
382 static void ks8695uart_set_termios(struct uart_port
*port
, struct ktermios
*termios
, struct ktermios
*old
)
384 unsigned int lcr
, fcr
= 0;
386 unsigned int baud
, quot
;
389 * Ask the core to calculate the divisor for us.
391 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
392 quot
= uart_get_divisor(port
, baud
);
394 switch (termios
->c_cflag
& CSIZE
) {
410 if (termios
->c_cflag
& CSTOPB
)
414 if (termios
->c_cflag
& PARENB
) {
415 if (termios
->c_cflag
& CMSPAR
) { /* Mark or Space parity */
416 if (termios
->c_cflag
& PARODD
)
421 else if (termios
->c_cflag
& PARODD
)
427 if (port
->fifosize
> 1)
428 fcr
= URFC_URFRT_8
| URFC_URTFR
| URFC_URRFR
| URFC_URFE
;
430 spin_lock_irqsave(&port
->lock
, flags
);
433 * Update the per-port timeout.
435 uart_update_timeout(port
, termios
->c_cflag
, baud
);
437 port
->read_status_mask
= URLS_URROE
;
438 if (termios
->c_iflag
& INPCK
)
439 port
->read_status_mask
|= (URLS_URFE
| URLS_URPE
);
440 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
441 port
->read_status_mask
|= URLS_URBI
;
444 * Characters to ignore
446 port
->ignore_status_mask
= 0;
447 if (termios
->c_iflag
& IGNPAR
)
448 port
->ignore_status_mask
|= (URLS_URFE
| URLS_URPE
);
449 if (termios
->c_iflag
& IGNBRK
) {
450 port
->ignore_status_mask
|= URLS_URBI
;
452 * If we're ignoring parity and break indicators,
453 * ignore overruns too (for real raw support).
455 if (termios
->c_iflag
& IGNPAR
)
456 port
->ignore_status_mask
|= URLS_URROE
;
460 * Ignore all characters if CREAD is not set.
462 if ((termios
->c_cflag
& CREAD
) == 0)
463 port
->ignore_status_mask
|= UART_DUMMY_LSR_RX
;
465 /* first, disable everything */
466 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
467 ks8695uart_enable_ms(port
);
469 ks8695uart_disable_ms(port
);
472 UART_PUT_BRDR(port
, quot
);
474 UART_PUT_LCR(port
, lcr
);
475 UART_PUT_FCR(port
, fcr
);
477 spin_unlock_irqrestore(&port
->lock
, flags
);
480 static const char *ks8695uart_type(struct uart_port
*port
)
482 return port
->type
== PORT_KS8695
? "KS8695" : NULL
;
486 * Release the memory region(s) being used by 'port'
488 static void ks8695uart_release_port(struct uart_port
*port
)
490 release_mem_region(port
->mapbase
, UART_PORT_SIZE
);
494 * Request the memory region(s) being used by 'port'
496 static int ks8695uart_request_port(struct uart_port
*port
)
498 return request_mem_region(port
->mapbase
, UART_PORT_SIZE
,
499 "serial_ks8695") != NULL
? 0 : -EBUSY
;
503 * Configure/autoconfigure the port.
505 static void ks8695uart_config_port(struct uart_port
*port
, int flags
)
507 if (flags
& UART_CONFIG_TYPE
) {
508 port
->type
= PORT_KS8695
;
509 ks8695uart_request_port(port
);
514 * verify the new serial_struct (for TIOCSSERIAL).
516 static int ks8695uart_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
520 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_KS8695
)
522 if (ser
->irq
!= port
->irq
)
524 if (ser
->baud_base
< 9600)
529 static struct uart_ops ks8695uart_pops
= {
530 .tx_empty
= ks8695uart_tx_empty
,
531 .set_mctrl
= ks8695uart_set_mctrl
,
532 .get_mctrl
= ks8695uart_get_mctrl
,
533 .stop_tx
= ks8695uart_stop_tx
,
534 .start_tx
= ks8695uart_start_tx
,
535 .stop_rx
= ks8695uart_stop_rx
,
536 .enable_ms
= ks8695uart_enable_ms
,
537 .break_ctl
= ks8695uart_break_ctl
,
538 .startup
= ks8695uart_startup
,
539 .shutdown
= ks8695uart_shutdown
,
540 .set_termios
= ks8695uart_set_termios
,
541 .type
= ks8695uart_type
,
542 .release_port
= ks8695uart_release_port
,
543 .request_port
= ks8695uart_request_port
,
544 .config_port
= ks8695uart_config_port
,
545 .verify_port
= ks8695uart_verify_port
,
548 static struct uart_port ks8695uart_ports
[SERIAL_KS8695_NR
] = {
550 .membase
= KS8695_UART_VA
,
551 .mapbase
= KS8695_UART_PA
,
552 .iotype
= SERIAL_IO_MEM
,
553 .irq
= KS8695_IRQ_UART_TX
,
554 .uartclk
= KS8695_CLOCK_RATE
* 16,
556 .ops
= &ks8695uart_pops
,
557 .flags
= ASYNC_BOOT_AUTOCONF
,
562 #ifdef CONFIG_SERIAL_KS8695_CONSOLE
563 static void ks8695_console_putchar(struct uart_port
*port
, int ch
)
565 while (!(UART_GET_LSR(port
) & URLS_URTHRE
))
568 UART_PUT_CHAR(port
, ch
);
571 static void ks8695_console_write(struct console
*co
, const char *s
, u_int count
)
573 struct uart_port
*port
= ks8695uart_ports
+ co
->index
;
575 uart_console_write(port
, s
, count
, ks8695_console_putchar
);
578 static void __init
ks8695_console_get_options(struct uart_port
*port
, int *baud
, int *parity
, int *bits
)
582 lcr
= UART_GET_LCR(port
);
584 switch (lcr
& URLC_PARITY
) {
595 switch (lcr
& URLC_URCL
) {
609 *baud
= port
->uartclk
/ (UART_GET_BRDR(port
) & 0x0FFF);
614 static int __init
ks8695_console_setup(struct console
*co
, char *options
)
616 struct uart_port
*port
;
623 * Check whether an invalid uart number has been specified, and
624 * if so, search for the first available port that does have
627 port
= uart_get_console(ks8695uart_ports
, SERIAL_KS8695_NR
, co
);
630 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
632 ks8695_console_get_options(port
, &baud
, &parity
, &bits
);
634 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
637 static struct uart_driver ks8695_reg
;
639 static struct console ks8695_console
= {
640 .name
= SERIAL_KS8695_DEVNAME
,
641 .write
= ks8695_console_write
,
642 .device
= uart_console_device
,
643 .setup
= ks8695_console_setup
,
644 .flags
= CON_PRINTBUFFER
,
649 static int __init
ks8695_console_init(void)
651 add_preferred_console(SERIAL_KS8695_DEVNAME
, 0, NULL
);
652 register_console(&ks8695_console
);
656 console_initcall(ks8695_console_init
);
658 #define KS8695_CONSOLE &ks8695_console
660 #define KS8695_CONSOLE NULL
663 static struct uart_driver ks8695_reg
= {
664 .owner
= THIS_MODULE
,
665 .driver_name
= "serial_ks8695",
666 .dev_name
= SERIAL_KS8695_DEVNAME
,
667 .major
= SERIAL_KS8695_MAJOR
,
668 .minor
= SERIAL_KS8695_MINOR
,
669 .nr
= SERIAL_KS8695_NR
,
670 .cons
= KS8695_CONSOLE
,
673 static int __init
ks8695uart_init(void)
677 printk(KERN_INFO
"Serial: Micrel KS8695 UART driver\n");
679 ret
= uart_register_driver(&ks8695_reg
);
683 for (i
= 0; i
< SERIAL_KS8695_NR
; i
++)
684 uart_add_one_port(&ks8695_reg
, &ks8695uart_ports
[0]);
689 static void __exit
ks8695uart_exit(void)
693 for (i
= 0; i
< SERIAL_KS8695_NR
; i
++)
694 uart_remove_one_port(&ks8695_reg
, &ks8695uart_ports
[0]);
695 uart_unregister_driver(&ks8695_reg
);
698 module_init(ks8695uart_init
);
699 module_exit(ks8695uart_exit
);
701 MODULE_DESCRIPTION("KS8695 serial port driver");
702 MODULE_AUTHOR("Micrel Inc.");
703 MODULE_LICENSE("GPL");