MAINTAINERS: update KMEMTRACE pattern after file rename
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / serial_ks8695.c
blob998e89dc5aaf2db56eda9f18c1060ea8c9189555
1 /*
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>
25 #include <asm/io.h>
26 #include <asm/irq.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)
33 #define SUPPORT_SYSRQ
34 #endif
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)
83 if(enabled)
84 port->unused[0] |= 4;
85 else
86 port->unused[0] &= ~4;
89 static inline void rx_enable(struct uart_port *port, int enabled)
91 if(enabled)
92 port->unused[0] |= 2;
93 else
94 port->unused[0] &= ~2;
97 static inline void tx_enable(struct uart_port *port, int enabled)
99 if(enabled)
100 port->unused[0] |= 1;
101 else
102 port->unused[0] &= ~1;
106 #ifdef SUPPORT_SYSRQ
107 static struct console ks8695_console;
108 #endif
110 static void ks8695uart_stop_tx(struct uart_port *port)
112 if (tx_enabled(port)) {
113 disable_irq(KS8695_IRQ_UART_TX);
114 tx_enable(port, 0);
118 static void ks8695uart_start_tx(struct uart_port *port)
120 if (!tx_enabled(port)) {
121 enable_irq(KS8695_IRQ_UART_TX);
122 tx_enable(port, 1);
126 static void ks8695uart_stop_rx(struct uart_port *port)
128 if (rx_enabled(port)) {
129 disable_irq(KS8695_IRQ_UART_RX);
130 rx_enable(port, 0);
134 static void ks8695uart_enable_ms(struct uart_port *port)
136 if (!ms_enabled(port)) {
137 enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
138 ms_enable(port,1);
142 static void ks8695uart_disable_ms(struct uart_port *port)
144 if (ms_enabled(port)) {
145 disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
146 ms_enable(port,0);
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);
159 flg = TTY_NORMAL;
161 port->icount.rx++;
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);
171 port->icount.brk++;
172 if (uart_handle_break(port))
173 goto ignore_char;
175 if (lsr & URLS_URPE)
176 port->icount.parity++;
177 if (lsr & URLS_URFE)
178 port->icount.frame++;
179 if (lsr & URLS_URROE)
180 port->icount.overrun++;
182 lsr &= port->read_status_mask;
184 if (lsr & URLS_URBI)
185 flg = TTY_BREAK;
186 else if (lsr & URLS_URPE)
187 flg = TTY_PARITY;
188 else if (lsr & URLS_URFE)
189 flg = TTY_FRAME;
192 if (uart_handle_sysrq_char(port, ch))
193 goto ignore_char;
195 uart_insert_char(port, lsr, URLS_URROE, ch, flg);
197 ignore_char:
198 status = UART_GET_LSR(port);
200 tty_flip_buffer_push(tty);
202 return IRQ_HANDLED;
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;
210 unsigned int count;
212 if (port->x_char) {
213 KS8695_CLR_TX_INT();
214 UART_PUT_CHAR(port, port->x_char);
215 port->icount.tx++;
216 port->x_char = 0;
217 return IRQ_HANDLED;
220 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
221 ks8695uart_stop_tx(port);
222 return IRQ_HANDLED;
225 count = 16; /* fifo size */
226 while (!uart_circ_empty(xmit) && (count-- > 0)) {
227 KS8695_CLR_TX_INT();
228 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
230 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
231 port->icount.tx++;
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);
240 return IRQ_HANDLED;
243 static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
245 struct uart_port *port = dev_id;
246 unsigned int status;
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)
257 port->icount.dsr++;
259 if (status & URMS_URDCTS)
260 uart_handle_cts_change(port, status & URMS_URDCTS);
262 if (status & URMS_URTERI)
263 port->icount.rng++;
265 wake_up_interruptible(&port->info->delta_msr_wait);
267 return IRQ_HANDLED;
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;
278 unsigned int status;
280 status = UART_GET_MSR(port);
281 if (status & URMS_URDCD)
282 result |= TIOCM_CAR;
283 if (status & URMS_URDSR)
284 result |= TIOCM_DSR;
285 if (status & URMS_URCTS)
286 result |= TIOCM_CTS;
287 if (status & URMS_URRI)
288 result |= TIOCM_RI;
290 return result;
293 static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
295 unsigned int mcr;
297 mcr = UART_GET_MCR(port);
298 if (mctrl & TIOCM_RTS)
299 mcr |= URMC_URRTS;
300 else
301 mcr &= ~URMC_URRTS;
303 if (mctrl & TIOCM_DTR)
304 mcr |= URMC_URDTR;
305 else
306 mcr &= ~URMC_URDTR;
308 UART_PUT_MCR(port, mcr);
311 static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
313 unsigned int lcr;
315 lcr = UART_GET_LCR(port);
317 if (break_state == -1)
318 lcr |= URLC_URSBC;
319 else
320 lcr &= ~URLC_URSBC;
322 UART_PUT_LCR(port, lcr);
325 static int ks8695uart_startup(struct uart_port *port)
327 int retval;
329 set_irq_flags(KS8695_IRQ_UART_TX, IRQF_VALID | IRQF_NOAUTOEN);
330 tx_enable(port, 0);
331 rx_enable(port, 1);
332 ms_enable(port, 1);
335 * Allocate the IRQ
337 retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, IRQF_DISABLED, "UART TX", port);
338 if (retval)
339 goto err_tx;
341 retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, IRQF_DISABLED, "UART RX", port);
342 if (retval)
343 goto err_rx;
345 retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, IRQF_DISABLED, "UART LineStatus", port);
346 if (retval)
347 goto err_ls;
349 retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, IRQF_DISABLED, "UART ModemStatus", port);
350 if (retval)
351 goto err_ms;
353 return 0;
355 err_ms:
356 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
357 err_ls:
358 free_irq(KS8695_IRQ_UART_RX, port);
359 err_rx:
360 free_irq(KS8695_IRQ_UART_TX, port);
361 err_tx:
362 return retval;
365 static void ks8695uart_shutdown(struct uart_port *port)
368 * Free the interrupt
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;
383 unsigned long flags;
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) {
393 case CS5:
394 lcr = URCL_5;
395 break;
396 case CS6:
397 lcr = URCL_6;
398 break;
399 case CS7:
400 lcr = URCL_7;
401 break;
402 default:
403 lcr = URCL_8;
404 break;
407 /* stop bits */
408 if (termios->c_cflag & CSTOPB)
409 lcr |= URLC_URSB;
411 /* parity */
412 if (termios->c_cflag & PARENB) {
413 if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
414 if (termios->c_cflag & PARODD)
415 lcr |= URPE_MARK;
416 else
417 lcr |= URPE_SPACE;
419 else if (termios->c_cflag & PARODD)
420 lcr |= URPE_ODD;
421 else
422 lcr |= URPE_EVEN;
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);
466 else
467 ks8695uart_disable_ms(port);
469 /* Set baud rate */
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)
516 int ret = 0;
518 if (ser->type != PORT_UNKNOWN && ser->type != PORT_KS8695)
519 ret = -EINVAL;
520 if (ser->irq != port->irq)
521 ret = -EINVAL;
522 if (ser->baud_base < 9600)
523 ret = -EINVAL;
524 return ret;
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,
553 .fifosize = 16,
554 .ops = &ks8695uart_pops,
555 .flags = ASYNC_BOOT_AUTOCONF,
556 .line = 0,
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))
564 barrier();
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)
578 unsigned int lcr;
580 lcr = UART_GET_LCR(port);
582 switch (lcr & URLC_PARITY) {
583 case URPE_ODD:
584 *parity = 'o';
585 break;
586 case URPE_EVEN:
587 *parity = 'e';
588 break;
589 default:
590 *parity = 'n';
593 switch (lcr & URLC_URCL) {
594 case URCL_5:
595 *bits = 5;
596 break;
597 case URCL_6:
598 *bits = 6;
599 break;
600 case URCL_7:
601 *bits = 7;
602 break;
603 default:
604 *bits = 8;
607 *baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
608 *baud /= 16;
609 *baud &= 0xFFFFFFF0;
612 static int __init ks8695_console_setup(struct console *co, char *options)
614 struct uart_port *port;
615 int baud = 115200;
616 int bits = 8;
617 int parity = 'n';
618 int flow = 'n';
621 * Check whether an invalid uart number has been specified, and
622 * if so, search for the first available port that does have
623 * console support.
625 port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
627 if (options)
628 uart_parse_options(options, &baud, &parity, &bits, &flow);
629 else
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,
643 .index = -1,
644 .data = &ks8695_reg,
647 static int __init ks8695_console_init(void)
649 register_console(&ks8695_console);
650 return 0;
653 console_initcall(ks8695_console_init);
655 #define KS8695_CONSOLE &ks8695_console
656 #else
657 #define KS8695_CONSOLE NULL
658 #endif
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)
672 int i, ret;
674 printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
676 ret = uart_register_driver(&ks8695_reg);
677 if (ret)
678 return ret;
680 for (i = 0; i < SERIAL_KS8695_NR; i++)
681 uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
683 return 0;
686 static void __exit ks8695uart_exit(void)
688 int i;
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");