2 * drivers/serial/serial_ks8695.c
4 * Driver for KS8695 serial ports
6 * Based on drivers/serial/serial_amba.c, by Kam Lee.
8 * Copyright 2002-2005 Micrel Inc.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <linux/module.h>
17 #include <linux/tty.h>
18 #include <linux/ioport.h>
19 #include <linux/init.h>
20 #include <linux/serial.h>
21 #include <linux/console.h>
22 #include <linux/sysrq.h>
23 #include <linux/device.h>
27 #include <asm/mach/irq.h>
29 #include <mach/regs-uart.h>
30 #include <mach/regs-irq.h>
32 #if defined(CONFIG_SERIAL_KS8695_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
36 #include <linux/serial_core.h>
39 #define SERIAL_KS8695_MAJOR 204
40 #define SERIAL_KS8695_MINOR 16
41 #define SERIAL_KS8695_DEVNAME "ttyAM"
43 #define SERIAL_KS8695_NR 1
46 * Access macros for the KS8695 UART
48 #define UART_GET_CHAR(p) (__raw_readl((p)->membase + KS8695_URRB) & 0xFF)
49 #define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + KS8695_URTH)
50 #define UART_GET_FCR(p) __raw_readl((p)->membase + KS8695_URFC)
51 #define UART_PUT_FCR(p, c) __raw_writel((c), (p)->membase + KS8695_URFC)
52 #define UART_GET_MSR(p) __raw_readl((p)->membase + KS8695_URMS)
53 #define UART_GET_LSR(p) __raw_readl((p)->membase + KS8695_URLS)
54 #define UART_GET_LCR(p) __raw_readl((p)->membase + KS8695_URLC)
55 #define UART_PUT_LCR(p, c) __raw_writel((c), (p)->membase + KS8695_URLC)
56 #define UART_GET_MCR(p) __raw_readl((p)->membase + KS8695_URMC)
57 #define UART_PUT_MCR(p, c) __raw_writel((c), (p)->membase + KS8695_URMC)
58 #define UART_GET_BRDR(p) __raw_readl((p)->membase + KS8695_URBD)
59 #define UART_PUT_BRDR(p, c) __raw_writel((c), (p)->membase + KS8695_URBD)
61 #define KS8695_CLR_TX_INT() __raw_writel(1 << KS8695_IRQ_UART_TX, KS8695_IRQ_VA + KS8695_INTST)
63 #define UART_DUMMY_LSR_RX 0x100
64 #define UART_PORT_SIZE (KS8695_USR - KS8695_URRB + 4)
66 static inline int tx_enabled(struct uart_port
*port
)
68 return port
->unused
[0] & 1;
71 static inline int rx_enabled(struct uart_port
*port
)
73 return port
->unused
[0] & 2;
76 static inline int ms_enabled(struct uart_port
*port
)
78 return port
->unused
[0] & 4;
81 static inline void ms_enable(struct uart_port
*port
, int enabled
)
86 port
->unused
[0] &= ~4;
89 static inline void rx_enable(struct uart_port
*port
, int enabled
)
94 port
->unused
[0] &= ~2;
97 static inline void tx_enable(struct uart_port
*port
, int enabled
)
100 port
->unused
[0] |= 1;
102 port
->unused
[0] &= ~1;
107 static struct console ks8695_console
;
110 static void ks8695uart_stop_tx(struct uart_port
*port
)
112 if (tx_enabled(port
)) {
113 /* use disable_irq_nosync() and not disable_irq() to avoid self
114 * imposed deadlock by not waiting for irq handler to end,
115 * since this ks8695uart_stop_tx() is called from interrupt context.
117 disable_irq_nosync(KS8695_IRQ_UART_TX
);
122 static void ks8695uart_start_tx(struct uart_port
*port
)
124 if (!tx_enabled(port
)) {
125 enable_irq(KS8695_IRQ_UART_TX
);
130 static void ks8695uart_stop_rx(struct uart_port
*port
)
132 if (rx_enabled(port
)) {
133 disable_irq(KS8695_IRQ_UART_RX
);
138 static void ks8695uart_enable_ms(struct uart_port
*port
)
140 if (!ms_enabled(port
)) {
141 enable_irq(KS8695_IRQ_UART_MODEM_STATUS
);
146 static void ks8695uart_disable_ms(struct uart_port
*port
)
148 if (ms_enabled(port
)) {
149 disable_irq(KS8695_IRQ_UART_MODEM_STATUS
);
154 static irqreturn_t
ks8695uart_rx_chars(int irq
, void *dev_id
)
156 struct uart_port
*port
= dev_id
;
157 struct tty_struct
*tty
= port
->state
->port
.tty
;
158 unsigned int status
, ch
, lsr
, flg
, max_count
= 256;
160 status
= UART_GET_LSR(port
); /* clears pending LSR interrupts */
161 while ((status
& URLS_URDR
) && max_count
--) {
162 ch
= UART_GET_CHAR(port
);
168 * Note that the error handling code is
169 * out of the main execution path
171 lsr
= UART_GET_LSR(port
) | UART_DUMMY_LSR_RX
;
172 if (unlikely(lsr
& (URLS_URBI
| URLS_URPE
| URLS_URFE
| URLS_URROE
))) {
173 if (lsr
& URLS_URBI
) {
174 lsr
&= ~(URLS_URFE
| URLS_URPE
);
176 if (uart_handle_break(port
))
180 port
->icount
.parity
++;
182 port
->icount
.frame
++;
183 if (lsr
& URLS_URROE
)
184 port
->icount
.overrun
++;
186 lsr
&= port
->read_status_mask
;
190 else if (lsr
& URLS_URPE
)
192 else if (lsr
& URLS_URFE
)
196 if (uart_handle_sysrq_char(port
, ch
))
199 uart_insert_char(port
, lsr
, URLS_URROE
, ch
, flg
);
202 status
= UART_GET_LSR(port
);
204 tty_flip_buffer_push(tty
);
210 static irqreturn_t
ks8695uart_tx_chars(int irq
, void *dev_id
)
212 struct uart_port
*port
= dev_id
;
213 struct circ_buf
*xmit
= &port
->state
->xmit
;
218 UART_PUT_CHAR(port
, port
->x_char
);
224 if (uart_tx_stopped(port
) || uart_circ_empty(xmit
)) {
225 ks8695uart_stop_tx(port
);
229 count
= 16; /* fifo size */
230 while (!uart_circ_empty(xmit
) && (count
-- > 0)) {
232 UART_PUT_CHAR(port
, xmit
->buf
[xmit
->tail
]);
234 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
238 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
239 uart_write_wakeup(port
);
241 if (uart_circ_empty(xmit
))
242 ks8695uart_stop_tx(port
);
247 static irqreturn_t
ks8695uart_modem_status(int irq
, void *dev_id
)
249 struct uart_port
*port
= dev_id
;
253 * clear modem interrupt by reading MSR
255 status
= UART_GET_MSR(port
);
257 if (status
& URMS_URDDCD
)
258 uart_handle_dcd_change(port
, status
& URMS_URDDCD
);
260 if (status
& URMS_URDDST
)
263 if (status
& URMS_URDCTS
)
264 uart_handle_cts_change(port
, status
& URMS_URDCTS
);
266 if (status
& URMS_URTERI
)
269 wake_up_interruptible(&port
->state
->port
.delta_msr_wait
);
274 static unsigned int ks8695uart_tx_empty(struct uart_port
*port
)
276 return (UART_GET_LSR(port
) & URLS_URTE
) ? TIOCSER_TEMT
: 0;
279 static unsigned int ks8695uart_get_mctrl(struct uart_port
*port
)
281 unsigned int result
= 0;
284 status
= UART_GET_MSR(port
);
285 if (status
& URMS_URDCD
)
287 if (status
& URMS_URDSR
)
289 if (status
& URMS_URCTS
)
291 if (status
& URMS_URRI
)
297 static void ks8695uart_set_mctrl(struct uart_port
*port
, u_int mctrl
)
301 mcr
= UART_GET_MCR(port
);
302 if (mctrl
& TIOCM_RTS
)
307 if (mctrl
& TIOCM_DTR
)
312 UART_PUT_MCR(port
, mcr
);
315 static void ks8695uart_break_ctl(struct uart_port
*port
, int break_state
)
319 lcr
= UART_GET_LCR(port
);
321 if (break_state
== -1)
326 UART_PUT_LCR(port
, lcr
);
329 static int ks8695uart_startup(struct uart_port
*port
)
333 set_irq_flags(KS8695_IRQ_UART_TX
, IRQF_VALID
| IRQF_NOAUTOEN
);
341 retval
= request_irq(KS8695_IRQ_UART_TX
, ks8695uart_tx_chars
, IRQF_DISABLED
, "UART TX", port
);
345 retval
= request_irq(KS8695_IRQ_UART_RX
, ks8695uart_rx_chars
, IRQF_DISABLED
, "UART RX", port
);
349 retval
= request_irq(KS8695_IRQ_UART_LINE_STATUS
, ks8695uart_rx_chars
, IRQF_DISABLED
, "UART LineStatus", port
);
353 retval
= request_irq(KS8695_IRQ_UART_MODEM_STATUS
, ks8695uart_modem_status
, IRQF_DISABLED
, "UART ModemStatus", port
);
360 free_irq(KS8695_IRQ_UART_LINE_STATUS
, port
);
362 free_irq(KS8695_IRQ_UART_RX
, port
);
364 free_irq(KS8695_IRQ_UART_TX
, port
);
369 static void ks8695uart_shutdown(struct uart_port
*port
)
374 free_irq(KS8695_IRQ_UART_RX
, port
);
375 free_irq(KS8695_IRQ_UART_TX
, port
);
376 free_irq(KS8695_IRQ_UART_MODEM_STATUS
, port
);
377 free_irq(KS8695_IRQ_UART_LINE_STATUS
, port
);
379 /* disable break condition and fifos */
380 UART_PUT_LCR(port
, UART_GET_LCR(port
) & ~URLC_URSBC
);
381 UART_PUT_FCR(port
, UART_GET_FCR(port
) & ~URFC_URFE
);
384 static void ks8695uart_set_termios(struct uart_port
*port
, struct ktermios
*termios
, struct ktermios
*old
)
386 unsigned int lcr
, fcr
= 0;
388 unsigned int baud
, quot
;
391 * Ask the core to calculate the divisor for us.
393 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
394 quot
= uart_get_divisor(port
, baud
);
396 switch (termios
->c_cflag
& CSIZE
) {
412 if (termios
->c_cflag
& CSTOPB
)
416 if (termios
->c_cflag
& PARENB
) {
417 if (termios
->c_cflag
& CMSPAR
) { /* Mark or Space parity */
418 if (termios
->c_cflag
& PARODD
)
423 else if (termios
->c_cflag
& PARODD
)
429 if (port
->fifosize
> 1)
430 fcr
= URFC_URFRT_8
| URFC_URTFR
| URFC_URRFR
| URFC_URFE
;
432 spin_lock_irqsave(&port
->lock
, flags
);
435 * Update the per-port timeout.
437 uart_update_timeout(port
, termios
->c_cflag
, baud
);
439 port
->read_status_mask
= URLS_URROE
;
440 if (termios
->c_iflag
& INPCK
)
441 port
->read_status_mask
|= (URLS_URFE
| URLS_URPE
);
442 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
443 port
->read_status_mask
|= URLS_URBI
;
446 * Characters to ignore
448 port
->ignore_status_mask
= 0;
449 if (termios
->c_iflag
& IGNPAR
)
450 port
->ignore_status_mask
|= (URLS_URFE
| URLS_URPE
);
451 if (termios
->c_iflag
& IGNBRK
) {
452 port
->ignore_status_mask
|= URLS_URBI
;
454 * If we're ignoring parity and break indicators,
455 * ignore overruns too (for real raw support).
457 if (termios
->c_iflag
& IGNPAR
)
458 port
->ignore_status_mask
|= URLS_URROE
;
462 * Ignore all characters if CREAD is not set.
464 if ((termios
->c_cflag
& CREAD
) == 0)
465 port
->ignore_status_mask
|= UART_DUMMY_LSR_RX
;
467 /* first, disable everything */
468 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
469 ks8695uart_enable_ms(port
);
471 ks8695uart_disable_ms(port
);
474 UART_PUT_BRDR(port
, quot
);
476 UART_PUT_LCR(port
, lcr
);
477 UART_PUT_FCR(port
, fcr
);
479 spin_unlock_irqrestore(&port
->lock
, flags
);
482 static const char *ks8695uart_type(struct uart_port
*port
)
484 return port
->type
== PORT_KS8695
? "KS8695" : NULL
;
488 * Release the memory region(s) being used by 'port'
490 static void ks8695uart_release_port(struct uart_port
*port
)
492 release_mem_region(port
->mapbase
, UART_PORT_SIZE
);
496 * Request the memory region(s) being used by 'port'
498 static int ks8695uart_request_port(struct uart_port
*port
)
500 return request_mem_region(port
->mapbase
, UART_PORT_SIZE
,
501 "serial_ks8695") != NULL
? 0 : -EBUSY
;
505 * Configure/autoconfigure the port.
507 static void ks8695uart_config_port(struct uart_port
*port
, int flags
)
509 if (flags
& UART_CONFIG_TYPE
) {
510 port
->type
= PORT_KS8695
;
511 ks8695uart_request_port(port
);
516 * verify the new serial_struct (for TIOCSSERIAL).
518 static int ks8695uart_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
522 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_KS8695
)
524 if (ser
->irq
!= port
->irq
)
526 if (ser
->baud_base
< 9600)
531 static struct uart_ops ks8695uart_pops
= {
532 .tx_empty
= ks8695uart_tx_empty
,
533 .set_mctrl
= ks8695uart_set_mctrl
,
534 .get_mctrl
= ks8695uart_get_mctrl
,
535 .stop_tx
= ks8695uart_stop_tx
,
536 .start_tx
= ks8695uart_start_tx
,
537 .stop_rx
= ks8695uart_stop_rx
,
538 .enable_ms
= ks8695uart_enable_ms
,
539 .break_ctl
= ks8695uart_break_ctl
,
540 .startup
= ks8695uart_startup
,
541 .shutdown
= ks8695uart_shutdown
,
542 .set_termios
= ks8695uart_set_termios
,
543 .type
= ks8695uart_type
,
544 .release_port
= ks8695uart_release_port
,
545 .request_port
= ks8695uart_request_port
,
546 .config_port
= ks8695uart_config_port
,
547 .verify_port
= ks8695uart_verify_port
,
550 static struct uart_port ks8695uart_ports
[SERIAL_KS8695_NR
] = {
552 .membase
= (void *) KS8695_UART_VA
,
553 .mapbase
= KS8695_UART_VA
,
554 .iotype
= SERIAL_IO_MEM
,
555 .irq
= KS8695_IRQ_UART_TX
,
556 .uartclk
= KS8695_CLOCK_RATE
* 16,
558 .ops
= &ks8695uart_pops
,
559 .flags
= ASYNC_BOOT_AUTOCONF
,
564 #ifdef CONFIG_SERIAL_KS8695_CONSOLE
565 static void ks8695_console_putchar(struct uart_port
*port
, int ch
)
567 while (!(UART_GET_LSR(port
) & URLS_URTHRE
))
570 UART_PUT_CHAR(port
, ch
);
573 static void ks8695_console_write(struct console
*co
, const char *s
, u_int count
)
575 struct uart_port
*port
= ks8695uart_ports
+ co
->index
;
577 uart_console_write(port
, s
, count
, ks8695_console_putchar
);
580 static void __init
ks8695_console_get_options(struct uart_port
*port
, int *baud
, int *parity
, int *bits
)
584 lcr
= UART_GET_LCR(port
);
586 switch (lcr
& URLC_PARITY
) {
597 switch (lcr
& URLC_URCL
) {
611 *baud
= port
->uartclk
/ (UART_GET_BRDR(port
) & 0x0FFF);
616 static int __init
ks8695_console_setup(struct console
*co
, char *options
)
618 struct uart_port
*port
;
625 * Check whether an invalid uart number has been specified, and
626 * if so, search for the first available port that does have
629 port
= uart_get_console(ks8695uart_ports
, SERIAL_KS8695_NR
, co
);
632 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
634 ks8695_console_get_options(port
, &baud
, &parity
, &bits
);
636 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
639 static struct uart_driver ks8695_reg
;
641 static struct console ks8695_console
= {
642 .name
= SERIAL_KS8695_DEVNAME
,
643 .write
= ks8695_console_write
,
644 .device
= uart_console_device
,
645 .setup
= ks8695_console_setup
,
646 .flags
= CON_PRINTBUFFER
,
651 static int __init
ks8695_console_init(void)
653 add_preferred_console(SERIAL_KS8695_DEVNAME
, 0, NULL
);
654 register_console(&ks8695_console
);
658 console_initcall(ks8695_console_init
);
660 #define KS8695_CONSOLE &ks8695_console
662 #define KS8695_CONSOLE NULL
665 static struct uart_driver ks8695_reg
= {
666 .owner
= THIS_MODULE
,
667 .driver_name
= "serial_ks8695",
668 .dev_name
= SERIAL_KS8695_DEVNAME
,
669 .major
= SERIAL_KS8695_MAJOR
,
670 .minor
= SERIAL_KS8695_MINOR
,
671 .nr
= SERIAL_KS8695_NR
,
672 .cons
= KS8695_CONSOLE
,
675 static int __init
ks8695uart_init(void)
679 printk(KERN_INFO
"Serial: Micrel KS8695 UART driver\n");
681 ret
= uart_register_driver(&ks8695_reg
);
685 for (i
= 0; i
< SERIAL_KS8695_NR
; i
++)
686 uart_add_one_port(&ks8695_reg
, &ks8695uart_ports
[0]);
691 static void __exit
ks8695uart_exit(void)
695 for (i
= 0; i
< SERIAL_KS8695_NR
; i
++)
696 uart_remove_one_port(&ks8695_reg
, &ks8695uart_ports
[0]);
697 uart_unregister_driver(&ks8695_reg
);
700 module_init(ks8695uart_init
);
701 module_exit(ks8695uart_exit
);
703 MODULE_DESCRIPTION("KS8695 serial port driver");
704 MODULE_AUTHOR("Micrel Inc.");
705 MODULE_LICENSE("GPL");