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 <asm/arch/regs-uart.h>
30 #include <asm/arch/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 #define tx_enabled(port) ((port)->unused[0])
67 #define rx_enabled(port) ((port)->unused[1])
71 static struct console ks8695_console
;
74 static void ks8695uart_stop_tx(struct uart_port
*port
)
76 if (tx_enabled(port
)) {
77 disable_irq(KS8695_IRQ_UART_TX
);
82 static void ks8695uart_start_tx(struct uart_port
*port
)
84 if (!tx_enabled(port
)) {
85 enable_irq(KS8695_IRQ_UART_TX
);
90 static void ks8695uart_stop_rx(struct uart_port
*port
)
92 if (rx_enabled(port
)) {
93 disable_irq(KS8695_IRQ_UART_RX
);
98 static void ks8695uart_enable_ms(struct uart_port
*port
)
100 enable_irq(KS8695_IRQ_UART_MODEM_STATUS
);
103 static void ks8695uart_disable_ms(struct uart_port
*port
)
105 disable_irq(KS8695_IRQ_UART_MODEM_STATUS
);
108 static irqreturn_t
ks8695uart_rx_chars(int irq
, void *dev_id
)
110 struct uart_port
*port
= dev_id
;
111 struct tty_struct
*tty
= port
->info
->tty
;
112 unsigned int status
, ch
, lsr
, flg
, max_count
= 256;
114 status
= UART_GET_LSR(port
); /* clears pending LSR interrupts */
115 while ((status
& URLS_URDR
) && max_count
--) {
116 ch
= UART_GET_CHAR(port
);
122 * Note that the error handling code is
123 * out of the main execution path
125 lsr
= UART_GET_LSR(port
) | UART_DUMMY_LSR_RX
;
126 if (unlikely(lsr
& (URLS_URBI
| URLS_URPE
| URLS_URFE
| URLS_URROE
))) {
127 if (lsr
& URLS_URBI
) {
128 lsr
&= ~(URLS_URFE
| URLS_URPE
);
130 if (uart_handle_break(port
))
134 port
->icount
.parity
++;
136 port
->icount
.frame
++;
137 if (lsr
& URLS_URROE
)
138 port
->icount
.overrun
++;
140 lsr
&= port
->read_status_mask
;
144 else if (lsr
& URLS_URPE
)
146 else if (lsr
& URLS_URFE
)
150 if (uart_handle_sysrq_char(port
, ch
))
153 uart_insert_char(port
, lsr
, URLS_URROE
, ch
, flg
);
156 status
= UART_GET_LSR(port
);
158 tty_flip_buffer_push(tty
);
164 static irqreturn_t
ks8695uart_tx_chars(int irq
, void *dev_id
)
166 struct uart_port
*port
= dev_id
;
167 struct circ_buf
*xmit
= &port
->info
->xmit
;
172 UART_PUT_CHAR(port
, port
->x_char
);
178 if (uart_tx_stopped(port
) || uart_circ_empty(xmit
)) {
179 ks8695uart_stop_tx(port
);
183 count
= 16; /* fifo size */
184 while (!uart_circ_empty(xmit
) && (count
-- > 0)) {
186 UART_PUT_CHAR(port
, xmit
->buf
[xmit
->tail
]);
188 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
192 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
193 uart_write_wakeup(port
);
195 if (uart_circ_empty(xmit
))
196 ks8695uart_stop_tx(port
);
201 static irqreturn_t
ks8695uart_modem_status(int irq
, void *dev_id
)
203 struct uart_port
*port
= dev_id
;
207 * clear modem interrupt by reading MSR
209 status
= UART_GET_MSR(port
);
211 if (status
& URMS_URDDCD
)
212 uart_handle_dcd_change(port
, status
& URMS_URDDCD
);
214 if (status
& URMS_URDDST
)
217 if (status
& URMS_URDCTS
)
218 uart_handle_cts_change(port
, status
& URMS_URDCTS
);
220 if (status
& URMS_URTERI
)
223 wake_up_interruptible(&port
->info
->delta_msr_wait
);
228 static unsigned int ks8695uart_tx_empty(struct uart_port
*port
)
230 return (UART_GET_LSR(port
) & URLS_URTE
) ? TIOCSER_TEMT
: 0;
233 static unsigned int ks8695uart_get_mctrl(struct uart_port
*port
)
235 unsigned int result
= 0;
238 status
= UART_GET_MSR(port
);
239 if (status
& URMS_URDCD
)
241 if (status
& URMS_URDSR
)
243 if (status
& URMS_URCTS
)
245 if (status
& URMS_URRI
)
251 static void ks8695uart_set_mctrl(struct uart_port
*port
, u_int mctrl
)
255 mcr
= UART_GET_MCR(port
);
256 if (mctrl
& TIOCM_RTS
)
261 if (mctrl
& TIOCM_DTR
)
266 UART_PUT_MCR(port
, mcr
);
269 static void ks8695uart_break_ctl(struct uart_port
*port
, int break_state
)
273 lcr
= UART_GET_LCR(port
);
275 if (break_state
== -1)
280 UART_PUT_LCR(port
, lcr
);
283 static int ks8695uart_startup(struct uart_port
*port
)
287 set_irq_flags(KS8695_IRQ_UART_TX
, IRQF_VALID
| IRQF_NOAUTOEN
);
288 tx_enabled(port
) = 0;
289 rx_enabled(port
) = 1;
294 retval
= request_irq(KS8695_IRQ_UART_TX
, ks8695uart_tx_chars
, IRQF_DISABLED
, "UART TX", port
);
298 retval
= request_irq(KS8695_IRQ_UART_RX
, ks8695uart_rx_chars
, IRQF_DISABLED
, "UART RX", port
);
302 retval
= request_irq(KS8695_IRQ_UART_LINE_STATUS
, ks8695uart_rx_chars
, IRQF_DISABLED
, "UART LineStatus", port
);
306 retval
= request_irq(KS8695_IRQ_UART_MODEM_STATUS
, ks8695uart_modem_status
, IRQF_DISABLED
, "UART ModemStatus", port
);
313 free_irq(KS8695_IRQ_UART_LINE_STATUS
, port
);
315 free_irq(KS8695_IRQ_UART_RX
, port
);
317 free_irq(KS8695_IRQ_UART_TX
, port
);
322 static void ks8695uart_shutdown(struct uart_port
*port
)
327 free_irq(KS8695_IRQ_UART_RX
, port
);
328 free_irq(KS8695_IRQ_UART_TX
, port
);
329 free_irq(KS8695_IRQ_UART_MODEM_STATUS
, port
);
330 free_irq(KS8695_IRQ_UART_LINE_STATUS
, port
);
332 /* disable break condition and fifos */
333 UART_PUT_LCR(port
, UART_GET_LCR(port
) & ~URLC_URSBC
);
334 UART_PUT_FCR(port
, UART_GET_FCR(port
) & ~URFC_URFE
);
337 static void ks8695uart_set_termios(struct uart_port
*port
, struct ktermios
*termios
, struct ktermios
*old
)
339 unsigned int lcr
, fcr
= 0;
341 unsigned int baud
, quot
;
344 * Ask the core to calculate the divisor for us.
346 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
347 quot
= uart_get_divisor(port
, baud
);
349 switch (termios
->c_cflag
& CSIZE
) {
365 if (termios
->c_cflag
& CSTOPB
)
369 if (termios
->c_cflag
& PARENB
) {
370 if (termios
->c_cflag
& CMSPAR
) { /* Mark or Space parity */
371 if (termios
->c_cflag
& PARODD
)
376 else if (termios
->c_cflag
& PARODD
)
382 if (port
->fifosize
> 1)
383 fcr
= URFC_URFRT_8
| URFC_URTFR
| URFC_URRFR
| URFC_URFE
;
385 spin_lock_irqsave(&port
->lock
, flags
);
388 * Update the per-port timeout.
390 uart_update_timeout(port
, termios
->c_cflag
, baud
);
392 port
->read_status_mask
= URLS_URROE
;
393 if (termios
->c_iflag
& INPCK
)
394 port
->read_status_mask
|= (URLS_URFE
| URLS_URPE
);
395 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
396 port
->read_status_mask
|= URLS_URBI
;
399 * Characters to ignore
401 port
->ignore_status_mask
= 0;
402 if (termios
->c_iflag
& IGNPAR
)
403 port
->ignore_status_mask
|= (URLS_URFE
| URLS_URPE
);
404 if (termios
->c_iflag
& IGNBRK
) {
405 port
->ignore_status_mask
|= URLS_URBI
;
407 * If we're ignoring parity and break indicators,
408 * ignore overruns too (for real raw support).
410 if (termios
->c_iflag
& IGNPAR
)
411 port
->ignore_status_mask
|= URLS_URROE
;
415 * Ignore all characters if CREAD is not set.
417 if ((termios
->c_cflag
& CREAD
) == 0)
418 port
->ignore_status_mask
|= UART_DUMMY_LSR_RX
;
420 /* first, disable everything */
421 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
422 ks8695uart_enable_ms(port
);
424 ks8695uart_disable_ms(port
);
427 UART_PUT_BRDR(port
, quot
);
429 UART_PUT_LCR(port
, lcr
);
430 UART_PUT_FCR(port
, fcr
);
432 spin_unlock_irqrestore(&port
->lock
, flags
);
435 static const char *ks8695uart_type(struct uart_port
*port
)
437 return port
->type
== PORT_KS8695
? "KS8695" : NULL
;
441 * Release the memory region(s) being used by 'port'
443 static void ks8695uart_release_port(struct uart_port
*port
)
445 release_mem_region(port
->mapbase
, UART_PORT_SIZE
);
449 * Request the memory region(s) being used by 'port'
451 static int ks8695uart_request_port(struct uart_port
*port
)
453 return request_mem_region(port
->mapbase
, UART_PORT_SIZE
,
454 "serial_ks8695") != NULL
? 0 : -EBUSY
;
458 * Configure/autoconfigure the port.
460 static void ks8695uart_config_port(struct uart_port
*port
, int flags
)
462 if (flags
& UART_CONFIG_TYPE
) {
463 port
->type
= PORT_KS8695
;
464 ks8695uart_request_port(port
);
469 * verify the new serial_struct (for TIOCSSERIAL).
471 static int ks8695uart_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
475 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_KS8695
)
477 if (ser
->irq
!= port
->irq
)
479 if (ser
->baud_base
< 9600)
484 static struct uart_ops ks8695uart_pops
= {
485 .tx_empty
= ks8695uart_tx_empty
,
486 .set_mctrl
= ks8695uart_set_mctrl
,
487 .get_mctrl
= ks8695uart_get_mctrl
,
488 .stop_tx
= ks8695uart_stop_tx
,
489 .start_tx
= ks8695uart_start_tx
,
490 .stop_rx
= ks8695uart_stop_rx
,
491 .enable_ms
= ks8695uart_enable_ms
,
492 .break_ctl
= ks8695uart_break_ctl
,
493 .startup
= ks8695uart_startup
,
494 .shutdown
= ks8695uart_shutdown
,
495 .set_termios
= ks8695uart_set_termios
,
496 .type
= ks8695uart_type
,
497 .release_port
= ks8695uart_release_port
,
498 .request_port
= ks8695uart_request_port
,
499 .config_port
= ks8695uart_config_port
,
500 .verify_port
= ks8695uart_verify_port
,
503 static struct uart_port ks8695uart_ports
[SERIAL_KS8695_NR
] = {
505 .membase
= (void *) KS8695_UART_VA
,
506 .mapbase
= KS8695_UART_VA
,
507 .iotype
= SERIAL_IO_MEM
,
508 .irq
= KS8695_IRQ_UART_TX
,
509 .uartclk
= CLOCK_TICK_RATE
* 16,
511 .ops
= &ks8695uart_pops
,
512 .flags
= ASYNC_BOOT_AUTOCONF
,
517 #ifdef CONFIG_SERIAL_KS8695_CONSOLE
518 static void ks8695_console_putchar(struct uart_port
*port
, int ch
)
520 while (!(UART_GET_LSR(port
) & URLS_URTHRE
))
523 UART_PUT_CHAR(port
, ch
);
526 static void ks8695_console_write(struct console
*co
, const char *s
, u_int count
)
528 struct uart_port
*port
= ks8695uart_ports
+ co
->index
;
530 uart_console_write(port
, s
, count
, ks8695_console_putchar
);
533 static void __init
ks8695_console_get_options(struct uart_port
*port
, int *baud
, int *parity
, int *bits
)
537 lcr
= UART_GET_LCR(port
);
539 switch (lcr
& URLC_PARITY
) {
550 switch (lcr
& URLC_URCL
) {
564 *baud
= port
->uartclk
/ (UART_GET_BRDR(port
) & 0x0FFF);
569 static int __init
ks8695_console_setup(struct console
*co
, char *options
)
571 struct uart_port
*port
;
578 * Check whether an invalid uart number has been specified, and
579 * if so, search for the first available port that does have
582 port
= uart_get_console(ks8695uart_ports
, SERIAL_KS8695_NR
, co
);
585 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
587 ks8695_console_get_options(port
, &baud
, &parity
, &bits
);
589 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
592 static struct uart_driver ks8695_reg
;
594 static struct console ks8695_console
= {
595 .name
= SERIAL_KS8695_DEVNAME
,
596 .write
= ks8695_console_write
,
597 .device
= uart_console_device
,
598 .setup
= ks8695_console_setup
,
599 .flags
= CON_PRINTBUFFER
,
604 static int __init
ks8695_console_init(void)
606 register_console(&ks8695_console
);
610 console_initcall(ks8695_console_init
);
612 #define KS8695_CONSOLE &ks8695_console
614 #define KS8695_CONSOLE NULL
617 static struct uart_driver ks8695_reg
= {
618 .owner
= THIS_MODULE
,
619 .driver_name
= "serial_ks8695",
620 .dev_name
= SERIAL_KS8695_DEVNAME
,
621 .major
= SERIAL_KS8695_MAJOR
,
622 .minor
= SERIAL_KS8695_MINOR
,
623 .nr
= SERIAL_KS8695_NR
,
624 .cons
= KS8695_CONSOLE
,
627 static int __init
ks8695uart_init(void)
631 printk(KERN_INFO
"Serial: Micrel KS8695 UART driver\n");
633 ret
= uart_register_driver(&ks8695_reg
);
637 for (i
= 0; i
< SERIAL_KS8695_NR
; i
++)
638 uart_add_one_port(&ks8695_reg
, &ks8695uart_ports
[0]);
643 static void __exit
ks8695uart_exit(void)
647 for (i
= 0; i
< SERIAL_KS8695_NR
; i
++)
648 uart_remove_one_port(&ks8695_reg
, &ks8695uart_ports
[0]);
649 uart_unregister_driver(&ks8695_reg
);
652 module_init(ks8695uart_init
);
653 module_exit(ks8695uart_exit
);
655 MODULE_DESCRIPTION("KS8695 serial port driver");
656 MODULE_AUTHOR("Micrel Inc.");
657 MODULE_LICENSE("GPL");