Linux 3.4.102
[linux/fpc-iii.git] / drivers / tty / serial / serial_ks8695.c
blob7c13639c597ef878527e94208bd9ffc2a5cb8e4d
1 /*
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>
24 #include <asm/io.h>
25 #include <asm/irq.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)
32 #define SUPPORT_SYSRQ
33 #endif
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)
82 if(enabled)
83 port->unused[0] |= 4;
84 else
85 port->unused[0] &= ~4;
88 static inline void rx_enable(struct uart_port *port, int enabled)
90 if(enabled)
91 port->unused[0] |= 2;
92 else
93 port->unused[0] &= ~2;
96 static inline void tx_enable(struct uart_port *port, int enabled)
98 if(enabled)
99 port->unused[0] |= 1;
100 else
101 port->unused[0] &= ~1;
105 #ifdef SUPPORT_SYSRQ
106 static struct console ks8695_console;
107 #endif
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);
117 tx_enable(port, 0);
121 static void ks8695uart_start_tx(struct uart_port *port)
123 if (!tx_enabled(port)) {
124 enable_irq(KS8695_IRQ_UART_TX);
125 tx_enable(port, 1);
129 static void ks8695uart_stop_rx(struct uart_port *port)
131 if (rx_enabled(port)) {
132 disable_irq(KS8695_IRQ_UART_RX);
133 rx_enable(port, 0);
137 static void ks8695uart_enable_ms(struct uart_port *port)
139 if (!ms_enabled(port)) {
140 enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
141 ms_enable(port,1);
145 static void ks8695uart_disable_ms(struct uart_port *port)
147 if (ms_enabled(port)) {
148 disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
149 ms_enable(port,0);
153 static irqreturn_t ks8695uart_rx_chars(int irq, void *dev_id)
155 struct uart_port *port = dev_id;
156 struct tty_struct *tty = port->state->port.tty;
157 unsigned int status, ch, lsr, flg, max_count = 256;
159 status = UART_GET_LSR(port); /* clears pending LSR interrupts */
160 while ((status & URLS_URDR) && max_count--) {
161 ch = UART_GET_CHAR(port);
162 flg = TTY_NORMAL;
164 port->icount.rx++;
167 * Note that the error handling code is
168 * out of the main execution path
170 lsr = UART_GET_LSR(port) | UART_DUMMY_LSR_RX;
171 if (unlikely(lsr & (URLS_URBI | URLS_URPE | URLS_URFE | URLS_URROE))) {
172 if (lsr & URLS_URBI) {
173 lsr &= ~(URLS_URFE | URLS_URPE);
174 port->icount.brk++;
175 if (uart_handle_break(port))
176 goto ignore_char;
178 if (lsr & URLS_URPE)
179 port->icount.parity++;
180 if (lsr & URLS_URFE)
181 port->icount.frame++;
182 if (lsr & URLS_URROE)
183 port->icount.overrun++;
185 lsr &= port->read_status_mask;
187 if (lsr & URLS_URBI)
188 flg = TTY_BREAK;
189 else if (lsr & URLS_URPE)
190 flg = TTY_PARITY;
191 else if (lsr & URLS_URFE)
192 flg = TTY_FRAME;
195 if (uart_handle_sysrq_char(port, ch))
196 goto ignore_char;
198 uart_insert_char(port, lsr, URLS_URROE, ch, flg);
200 ignore_char:
201 status = UART_GET_LSR(port);
203 tty_flip_buffer_push(tty);
205 return IRQ_HANDLED;
209 static irqreturn_t ks8695uart_tx_chars(int irq, void *dev_id)
211 struct uart_port *port = dev_id;
212 struct circ_buf *xmit = &port->state->xmit;
213 unsigned int count;
215 if (port->x_char) {
216 KS8695_CLR_TX_INT();
217 UART_PUT_CHAR(port, port->x_char);
218 port->icount.tx++;
219 port->x_char = 0;
220 return IRQ_HANDLED;
223 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
224 ks8695uart_stop_tx(port);
225 return IRQ_HANDLED;
228 count = 16; /* fifo size */
229 while (!uart_circ_empty(xmit) && (count-- > 0)) {
230 KS8695_CLR_TX_INT();
231 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
233 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
234 port->icount.tx++;
237 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
238 uart_write_wakeup(port);
240 if (uart_circ_empty(xmit))
241 ks8695uart_stop_tx(port);
243 return IRQ_HANDLED;
246 static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
248 struct uart_port *port = dev_id;
249 unsigned int status;
252 * clear modem interrupt by reading MSR
254 status = UART_GET_MSR(port);
256 if (status & URMS_URDDCD)
257 uart_handle_dcd_change(port, status & URMS_URDDCD);
259 if (status & URMS_URDDST)
260 port->icount.dsr++;
262 if (status & URMS_URDCTS)
263 uart_handle_cts_change(port, status & URMS_URDCTS);
265 if (status & URMS_URTERI)
266 port->icount.rng++;
268 wake_up_interruptible(&port->state->port.delta_msr_wait);
270 return IRQ_HANDLED;
273 static unsigned int ks8695uart_tx_empty(struct uart_port *port)
275 return (UART_GET_LSR(port) & URLS_URTE) ? TIOCSER_TEMT : 0;
278 static unsigned int ks8695uart_get_mctrl(struct uart_port *port)
280 unsigned int result = 0;
281 unsigned int status;
283 status = UART_GET_MSR(port);
284 if (status & URMS_URDCD)
285 result |= TIOCM_CAR;
286 if (status & URMS_URDSR)
287 result |= TIOCM_DSR;
288 if (status & URMS_URCTS)
289 result |= TIOCM_CTS;
290 if (status & URMS_URRI)
291 result |= TIOCM_RI;
293 return result;
296 static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
298 unsigned int mcr;
300 mcr = UART_GET_MCR(port);
301 if (mctrl & TIOCM_RTS)
302 mcr |= URMC_URRTS;
303 else
304 mcr &= ~URMC_URRTS;
306 if (mctrl & TIOCM_DTR)
307 mcr |= URMC_URDTR;
308 else
309 mcr &= ~URMC_URDTR;
311 UART_PUT_MCR(port, mcr);
314 static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
316 unsigned int lcr;
318 lcr = UART_GET_LCR(port);
320 if (break_state == -1)
321 lcr |= URLC_URSBC;
322 else
323 lcr &= ~URLC_URSBC;
325 UART_PUT_LCR(port, lcr);
328 static int ks8695uart_startup(struct uart_port *port)
330 int retval;
332 set_irq_flags(KS8695_IRQ_UART_TX, IRQF_VALID | IRQF_NOAUTOEN);
333 tx_enable(port, 0);
334 rx_enable(port, 1);
335 ms_enable(port, 1);
338 * Allocate the IRQ
340 retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, 0, "UART TX", port);
341 if (retval)
342 goto err_tx;
344 retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, 0, "UART RX", port);
345 if (retval)
346 goto err_rx;
348 retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, 0, "UART LineStatus", port);
349 if (retval)
350 goto err_ls;
352 retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, 0, "UART ModemStatus", port);
353 if (retval)
354 goto err_ms;
356 return 0;
358 err_ms:
359 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
360 err_ls:
361 free_irq(KS8695_IRQ_UART_RX, port);
362 err_rx:
363 free_irq(KS8695_IRQ_UART_TX, port);
364 err_tx:
365 return retval;
368 static void ks8695uart_shutdown(struct uart_port *port)
371 * Free the interrupt
373 free_irq(KS8695_IRQ_UART_RX, port);
374 free_irq(KS8695_IRQ_UART_TX, port);
375 free_irq(KS8695_IRQ_UART_MODEM_STATUS, port);
376 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
378 /* disable break condition and fifos */
379 UART_PUT_LCR(port, UART_GET_LCR(port) & ~URLC_URSBC);
380 UART_PUT_FCR(port, UART_GET_FCR(port) & ~URFC_URFE);
383 static void ks8695uart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
385 unsigned int lcr, fcr = 0;
386 unsigned long flags;
387 unsigned int baud, quot;
390 * Ask the core to calculate the divisor for us.
392 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
393 quot = uart_get_divisor(port, baud);
395 switch (termios->c_cflag & CSIZE) {
396 case CS5:
397 lcr = URCL_5;
398 break;
399 case CS6:
400 lcr = URCL_6;
401 break;
402 case CS7:
403 lcr = URCL_7;
404 break;
405 default:
406 lcr = URCL_8;
407 break;
410 /* stop bits */
411 if (termios->c_cflag & CSTOPB)
412 lcr |= URLC_URSB;
414 /* parity */
415 if (termios->c_cflag & PARENB) {
416 if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
417 if (termios->c_cflag & PARODD)
418 lcr |= URPE_MARK;
419 else
420 lcr |= URPE_SPACE;
422 else if (termios->c_cflag & PARODD)
423 lcr |= URPE_ODD;
424 else
425 lcr |= URPE_EVEN;
428 if (port->fifosize > 1)
429 fcr = URFC_URFRT_8 | URFC_URTFR | URFC_URRFR | URFC_URFE;
431 spin_lock_irqsave(&port->lock, flags);
434 * Update the per-port timeout.
436 uart_update_timeout(port, termios->c_cflag, baud);
438 port->read_status_mask = URLS_URROE;
439 if (termios->c_iflag & INPCK)
440 port->read_status_mask |= (URLS_URFE | URLS_URPE);
441 if (termios->c_iflag & (BRKINT | PARMRK))
442 port->read_status_mask |= URLS_URBI;
445 * Characters to ignore
447 port->ignore_status_mask = 0;
448 if (termios->c_iflag & IGNPAR)
449 port->ignore_status_mask |= (URLS_URFE | URLS_URPE);
450 if (termios->c_iflag & IGNBRK) {
451 port->ignore_status_mask |= URLS_URBI;
453 * If we're ignoring parity and break indicators,
454 * ignore overruns too (for real raw support).
456 if (termios->c_iflag & IGNPAR)
457 port->ignore_status_mask |= URLS_URROE;
461 * Ignore all characters if CREAD is not set.
463 if ((termios->c_cflag & CREAD) == 0)
464 port->ignore_status_mask |= UART_DUMMY_LSR_RX;
466 /* first, disable everything */
467 if (UART_ENABLE_MS(port, termios->c_cflag))
468 ks8695uart_enable_ms(port);
469 else
470 ks8695uart_disable_ms(port);
472 /* Set baud rate */
473 UART_PUT_BRDR(port, quot);
475 UART_PUT_LCR(port, lcr);
476 UART_PUT_FCR(port, fcr);
478 spin_unlock_irqrestore(&port->lock, flags);
481 static const char *ks8695uart_type(struct uart_port *port)
483 return port->type == PORT_KS8695 ? "KS8695" : NULL;
487 * Release the memory region(s) being used by 'port'
489 static void ks8695uart_release_port(struct uart_port *port)
491 release_mem_region(port->mapbase, UART_PORT_SIZE);
495 * Request the memory region(s) being used by 'port'
497 static int ks8695uart_request_port(struct uart_port *port)
499 return request_mem_region(port->mapbase, UART_PORT_SIZE,
500 "serial_ks8695") != NULL ? 0 : -EBUSY;
504 * Configure/autoconfigure the port.
506 static void ks8695uart_config_port(struct uart_port *port, int flags)
508 if (flags & UART_CONFIG_TYPE) {
509 port->type = PORT_KS8695;
510 ks8695uart_request_port(port);
515 * verify the new serial_struct (for TIOCSSERIAL).
517 static int ks8695uart_verify_port(struct uart_port *port, struct serial_struct *ser)
519 int ret = 0;
521 if (ser->type != PORT_UNKNOWN && ser->type != PORT_KS8695)
522 ret = -EINVAL;
523 if (ser->irq != port->irq)
524 ret = -EINVAL;
525 if (ser->baud_base < 9600)
526 ret = -EINVAL;
527 return ret;
530 static struct uart_ops ks8695uart_pops = {
531 .tx_empty = ks8695uart_tx_empty,
532 .set_mctrl = ks8695uart_set_mctrl,
533 .get_mctrl = ks8695uart_get_mctrl,
534 .stop_tx = ks8695uart_stop_tx,
535 .start_tx = ks8695uart_start_tx,
536 .stop_rx = ks8695uart_stop_rx,
537 .enable_ms = ks8695uart_enable_ms,
538 .break_ctl = ks8695uart_break_ctl,
539 .startup = ks8695uart_startup,
540 .shutdown = ks8695uart_shutdown,
541 .set_termios = ks8695uart_set_termios,
542 .type = ks8695uart_type,
543 .release_port = ks8695uart_release_port,
544 .request_port = ks8695uart_request_port,
545 .config_port = ks8695uart_config_port,
546 .verify_port = ks8695uart_verify_port,
549 static struct uart_port ks8695uart_ports[SERIAL_KS8695_NR] = {
551 .membase = (void *) KS8695_UART_VA,
552 .mapbase = KS8695_UART_VA,
553 .iotype = SERIAL_IO_MEM,
554 .irq = KS8695_IRQ_UART_TX,
555 .uartclk = KS8695_CLOCK_RATE * 16,
556 .fifosize = 16,
557 .ops = &ks8695uart_pops,
558 .flags = ASYNC_BOOT_AUTOCONF,
559 .line = 0,
563 #ifdef CONFIG_SERIAL_KS8695_CONSOLE
564 static void ks8695_console_putchar(struct uart_port *port, int ch)
566 while (!(UART_GET_LSR(port) & URLS_URTHRE))
567 barrier();
569 UART_PUT_CHAR(port, ch);
572 static void ks8695_console_write(struct console *co, const char *s, u_int count)
574 struct uart_port *port = ks8695uart_ports + co->index;
576 uart_console_write(port, s, count, ks8695_console_putchar);
579 static void __init ks8695_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
581 unsigned int lcr;
583 lcr = UART_GET_LCR(port);
585 switch (lcr & URLC_PARITY) {
586 case URPE_ODD:
587 *parity = 'o';
588 break;
589 case URPE_EVEN:
590 *parity = 'e';
591 break;
592 default:
593 *parity = 'n';
596 switch (lcr & URLC_URCL) {
597 case URCL_5:
598 *bits = 5;
599 break;
600 case URCL_6:
601 *bits = 6;
602 break;
603 case URCL_7:
604 *bits = 7;
605 break;
606 default:
607 *bits = 8;
610 *baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
611 *baud /= 16;
612 *baud &= 0xFFFFFFF0;
615 static int __init ks8695_console_setup(struct console *co, char *options)
617 struct uart_port *port;
618 int baud = 115200;
619 int bits = 8;
620 int parity = 'n';
621 int flow = 'n';
624 * Check whether an invalid uart number has been specified, and
625 * if so, search for the first available port that does have
626 * console support.
628 port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
630 if (options)
631 uart_parse_options(options, &baud, &parity, &bits, &flow);
632 else
633 ks8695_console_get_options(port, &baud, &parity, &bits);
635 return uart_set_options(port, co, baud, parity, bits, flow);
638 static struct uart_driver ks8695_reg;
640 static struct console ks8695_console = {
641 .name = SERIAL_KS8695_DEVNAME,
642 .write = ks8695_console_write,
643 .device = uart_console_device,
644 .setup = ks8695_console_setup,
645 .flags = CON_PRINTBUFFER,
646 .index = -1,
647 .data = &ks8695_reg,
650 static int __init ks8695_console_init(void)
652 add_preferred_console(SERIAL_KS8695_DEVNAME, 0, NULL);
653 register_console(&ks8695_console);
654 return 0;
657 console_initcall(ks8695_console_init);
659 #define KS8695_CONSOLE &ks8695_console
660 #else
661 #define KS8695_CONSOLE NULL
662 #endif
664 static struct uart_driver ks8695_reg = {
665 .owner = THIS_MODULE,
666 .driver_name = "serial_ks8695",
667 .dev_name = SERIAL_KS8695_DEVNAME,
668 .major = SERIAL_KS8695_MAJOR,
669 .minor = SERIAL_KS8695_MINOR,
670 .nr = SERIAL_KS8695_NR,
671 .cons = KS8695_CONSOLE,
674 static int __init ks8695uart_init(void)
676 int i, ret;
678 printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
680 ret = uart_register_driver(&ks8695_reg);
681 if (ret)
682 return ret;
684 for (i = 0; i < SERIAL_KS8695_NR; i++)
685 uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
687 return 0;
690 static void __exit ks8695uart_exit(void)
692 int i;
694 for (i = 0; i < SERIAL_KS8695_NR; i++)
695 uart_remove_one_port(&ks8695_reg, &ks8695uart_ports[0]);
696 uart_unregister_driver(&ks8695_reg);
699 module_init(ks8695uart_init);
700 module_exit(ks8695uart_exit);
702 MODULE_DESCRIPTION("KS8695 serial port driver");
703 MODULE_AUTHOR("Micrel Inc.");
704 MODULE_LICENSE("GPL");