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 disable_irq(KS8695_IRQ_UART_TX);
118 static void ks8695uart_start_tx(struct uart_port *port)
120 if (!tx_enabled(port)) {
121 enable_irq(KS8695_IRQ_UART_TX);
126 static void ks8695uart_stop_rx(struct uart_port *port)
128 if (rx_enabled(port)) {
129 disable_irq(KS8695_IRQ_UART_RX);
134 static void ks8695uart_enable_ms(struct uart_port *port)
136 if (!ms_enabled(port)) {
137 enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
142 static void ks8695uart_disable_ms(struct uart_port *port)
144 if (ms_enabled(port)) {
145 disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
150 static irqreturn_t ks8695uart_rx_chars(int irq, void *dev_id)
152 struct uart_port *port = dev_id;
153 struct tty_struct *tty = port->info->port.tty;
154 unsigned int status, ch, lsr, flg, max_count = 256;
156 status = UART_GET_LSR(port); /* clears pending LSR interrupts */
157 while ((status & URLS_URDR) && max_count--) {
158 ch = UART_GET_CHAR(port);
164 * Note that the error handling code is
165 * out of the main execution path
167 lsr = UART_GET_LSR(port) | UART_DUMMY_LSR_RX;
168 if (unlikely(lsr & (URLS_URBI | URLS_URPE | URLS_URFE | URLS_URROE))) {
169 if (lsr & URLS_URBI) {
170 lsr &= ~(URLS_URFE | URLS_URPE);
172 if (uart_handle_break(port))
176 port->icount.parity++;
178 port->icount.frame++;
179 if (lsr & URLS_URROE)
180 port->icount.overrun++;
182 lsr &= port->read_status_mask;
186 else if (lsr & URLS_URPE)
188 else if (lsr & URLS_URFE)
192 if (uart_handle_sysrq_char(port, ch))
195 uart_insert_char(port, lsr, URLS_URROE, ch, flg);
198 status = UART_GET_LSR(port);
200 tty_flip_buffer_push(tty);
206 static irqreturn_t ks8695uart_tx_chars(int irq, void *dev_id)
208 struct uart_port *port = dev_id;
209 struct circ_buf *xmit = &port->info->xmit;
214 UART_PUT_CHAR(port, port->x_char);
220 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
221 ks8695uart_stop_tx(port);
225 count = 16; /* fifo size */
226 while (!uart_circ_empty(xmit) && (count-- > 0)) {
228 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
230 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
234 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
235 uart_write_wakeup(port);
237 if (uart_circ_empty(xmit))
238 ks8695uart_stop_tx(port);
243 static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
245 struct uart_port *port = dev_id;
249 * clear modem interrupt by reading MSR
251 status = UART_GET_MSR(port);
253 if (status & URMS_URDDCD)
254 uart_handle_dcd_change(port, status & URMS_URDDCD);
256 if (status & URMS_URDDST)
259 if (status & URMS_URDCTS)
260 uart_handle_cts_change(port, status & URMS_URDCTS);
262 if (status & URMS_URTERI)
265 wake_up_interruptible(&port->info->delta_msr_wait);
270 static unsigned int ks8695uart_tx_empty(struct uart_port *port)
272 return (UART_GET_LSR(port) & URLS_URTE) ? TIOCSER_TEMT : 0;
275 static unsigned int ks8695uart_get_mctrl(struct uart_port *port)
277 unsigned int result = 0;
280 status = UART_GET_MSR(port);
281 if (status & URMS_URDCD)
283 if (status & URMS_URDSR)
285 if (status & URMS_URCTS)
287 if (status & URMS_URRI)
293 static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
297 mcr = UART_GET_MCR(port);
298 if (mctrl & TIOCM_RTS)
303 if (mctrl & TIOCM_DTR)
308 UART_PUT_MCR(port, mcr);
311 static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
315 lcr = UART_GET_LCR(port);
317 if (break_state == -1)
322 UART_PUT_LCR(port, lcr);
325 static int ks8695uart_startup(struct uart_port *port)
329 set_irq_flags(KS8695_IRQ_UART_TX, IRQF_VALID | IRQF_NOAUTOEN);
337 retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, IRQF_DISABLED, "UART TX", port);
341 retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, IRQF_DISABLED, "UART RX", port);
345 retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, IRQF_DISABLED, "UART LineStatus", port);
349 retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, IRQF_DISABLED, "UART ModemStatus", port);
356 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
358 free_irq(KS8695_IRQ_UART_RX, port);
360 free_irq(KS8695_IRQ_UART_TX, port);
365 static void ks8695uart_shutdown(struct uart_port *port)
370 free_irq(KS8695_IRQ_UART_RX, port);
371 free_irq(KS8695_IRQ_UART_TX, port);
372 free_irq(KS8695_IRQ_UART_MODEM_STATUS, port);
373 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
375 /* disable break condition and fifos */
376 UART_PUT_LCR(port, UART_GET_LCR(port) & ~URLC_URSBC);
377 UART_PUT_FCR(port, UART_GET_FCR(port) & ~URFC_URFE);
380 static void ks8695uart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
382 unsigned int lcr, fcr = 0;
384 unsigned int baud, quot;
387 * Ask the core to calculate the divisor for us.
389 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
390 quot = uart_get_divisor(port, baud);
392 switch (termios->c_cflag & CSIZE) {
408 if (termios->c_cflag & CSTOPB)
412 if (termios->c_cflag & PARENB) {
413 if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
414 if (termios->c_cflag & PARODD)
419 else if (termios->c_cflag & PARODD)
425 if (port->fifosize > 1)
426 fcr = URFC_URFRT_8 | URFC_URTFR | URFC_URRFR | URFC_URFE;
428 spin_lock_irqsave(&port->lock, flags);
431 * Update the per-port timeout.
433 uart_update_timeout(port, termios->c_cflag, baud);
435 port->read_status_mask = URLS_URROE;
436 if (termios->c_iflag & INPCK)
437 port->read_status_mask |= (URLS_URFE | URLS_URPE);
438 if (termios->c_iflag & (BRKINT | PARMRK))
439 port->read_status_mask |= URLS_URBI;
442 * Characters to ignore
444 port->ignore_status_mask = 0;
445 if (termios->c_iflag & IGNPAR)
446 port->ignore_status_mask |= (URLS_URFE | URLS_URPE);
447 if (termios->c_iflag & IGNBRK) {
448 port->ignore_status_mask |= URLS_URBI;
450 * If we're ignoring parity and break indicators,
451 * ignore overruns too (for real raw support).
453 if (termios->c_iflag & IGNPAR)
454 port->ignore_status_mask |= URLS_URROE;
458 * Ignore all characters if CREAD is not set.
460 if ((termios->c_cflag & CREAD) == 0)
461 port->ignore_status_mask |= UART_DUMMY_LSR_RX;
463 /* first, disable everything */
464 if (UART_ENABLE_MS(port, termios->c_cflag))
465 ks8695uart_enable_ms(port);
467 ks8695uart_disable_ms(port);
470 UART_PUT_BRDR(port, quot);
472 UART_PUT_LCR(port, lcr);
473 UART_PUT_FCR(port, fcr);
475 spin_unlock_irqrestore(&port->lock, flags);
478 static const char *ks8695uart_type(struct uart_port *port)
480 return port->type == PORT_KS8695 ? "KS8695" : NULL;
484 * Release the memory region(s) being used by 'port'
486 static void ks8695uart_release_port(struct uart_port *port)
488 release_mem_region(port->mapbase, UART_PORT_SIZE);
492 * Request the memory region(s) being used by 'port'
494 static int ks8695uart_request_port(struct uart_port *port)
496 return request_mem_region(port->mapbase, UART_PORT_SIZE,
497 "serial_ks8695") != NULL ? 0 : -EBUSY;
501 * Configure/autoconfigure the port.
503 static void ks8695uart_config_port(struct uart_port *port, int flags)
505 if (flags & UART_CONFIG_TYPE) {
506 port->type = PORT_KS8695;
507 ks8695uart_request_port(port);
512 * verify the new serial_struct (for TIOCSSERIAL).
514 static int ks8695uart_verify_port(struct uart_port *port, struct serial_struct *ser)
518 if (ser->type != PORT_UNKNOWN && ser->type != PORT_KS8695)
520 if (ser->irq != port->irq)
522 if (ser->baud_base < 9600)
527 static struct uart_ops ks8695uart_pops = {
528 .tx_empty = ks8695uart_tx_empty,
529 .set_mctrl = ks8695uart_set_mctrl,
530 .get_mctrl = ks8695uart_get_mctrl,
531 .stop_tx = ks8695uart_stop_tx,
532 .start_tx = ks8695uart_start_tx,
533 .stop_rx = ks8695uart_stop_rx,
534 .enable_ms = ks8695uart_enable_ms,
535 .break_ctl = ks8695uart_break_ctl,
536 .startup = ks8695uart_startup,
537 .shutdown = ks8695uart_shutdown,
538 .set_termios = ks8695uart_set_termios,
539 .type = ks8695uart_type,
540 .release_port = ks8695uart_release_port,
541 .request_port = ks8695uart_request_port,
542 .config_port = ks8695uart_config_port,
543 .verify_port = ks8695uart_verify_port,
546 static struct uart_port ks8695uart_ports[SERIAL_KS8695_NR] = {
548 .membase = (void *) KS8695_UART_VA,
549 .mapbase = KS8695_UART_VA,
550 .iotype = SERIAL_IO_MEM,
551 .irq = KS8695_IRQ_UART_TX,
552 .uartclk = CLOCK_TICK_RATE * 16,
554 .ops = &ks8695uart_pops,
555 .flags = ASYNC_BOOT_AUTOCONF,
560 #ifdef CONFIG_SERIAL_KS8695_CONSOLE
561 static void ks8695_console_putchar(struct uart_port *port, int ch)
563 while (!(UART_GET_LSR(port) & URLS_URTHRE))
566 UART_PUT_CHAR(port, ch);
569 static void ks8695_console_write(struct console *co, const char *s, u_int count)
571 struct uart_port *port = ks8695uart_ports + co->index;
573 uart_console_write(port, s, count, ks8695_console_putchar);
576 static void __init ks8695_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
580 lcr = UART_GET_LCR(port);
582 switch (lcr & URLC_PARITY) {
593 switch (lcr & URLC_URCL) {
607 *baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
612 static int __init ks8695_console_setup(struct console *co, char *options)
614 struct uart_port *port;
621 * Check whether an invalid uart number has been specified, and
622 * if so, search for the first available port that does have
625 port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
628 uart_parse_options(options, &baud, &parity, &bits, &flow);
630 ks8695_console_get_options(port, &baud, &parity, &bits);
632 return uart_set_options(port, co, baud, parity, bits, flow);
635 static struct uart_driver ks8695_reg;
637 static struct console ks8695_console = {
638 .name = SERIAL_KS8695_DEVNAME,
639 .write = ks8695_console_write,
640 .device = uart_console_device,
641 .setup = ks8695_console_setup,
642 .flags = CON_PRINTBUFFER,
647 static int __init ks8695_console_init(void)
649 register_console(&ks8695_console);
653 console_initcall(ks8695_console_init);
655 #define KS8695_CONSOLE &ks8695_console
657 #define KS8695_CONSOLE NULL
660 static struct uart_driver ks8695_reg = {
661 .owner = THIS_MODULE,
662 .driver_name = "serial_ks8695",
663 .dev_name = SERIAL_KS8695_DEVNAME,
664 .major = SERIAL_KS8695_MAJOR,
665 .minor = SERIAL_KS8695_MINOR,
666 .nr = SERIAL_KS8695_NR,
667 .cons = KS8695_CONSOLE,
670 static int __init ks8695uart_init(void)
674 printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
676 ret = uart_register_driver(&ks8695_reg);
680 for (i = 0; i < SERIAL_KS8695_NR; i++)
681 uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
686 static void __exit ks8695uart_exit(void)
690 for (i = 0; i < SERIAL_KS8695_NR; i++)
691 uart_remove_one_port(&ks8695_reg, &ks8695uart_ports[0]);
692 uart_unregister_driver(&ks8695_reg);
695 module_init(ks8695uart_init);
696 module_exit(ks8695uart_exit);
698 MODULE_DESCRIPTION("KS8695 serial port driver");
699 MODULE_AUTHOR("Micrel Inc.");
700 MODULE_LICENSE("GPL");