1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for KS8695 serial ports
5 * Based on drivers/serial/serial_amba.c, by Kam Lee.
7 * Copyright 2002-2005 Micrel Inc.
9 #include <linux/module.h>
10 #include <linux/tty.h>
11 #include <linux/tty_flip.h>
12 #include <linux/ioport.h>
13 #include <linux/init.h>
14 #include <linux/serial.h>
15 #include <linux/console.h>
16 #include <linux/sysrq.h>
17 #include <linux/device.h>
21 #include <asm/mach/irq.h>
23 #include <mach/regs-uart.h>
24 #include <mach/regs-irq.h>
26 #if defined(CONFIG_SERIAL_KS8695_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
30 #include <linux/serial_core.h>
33 #define SERIAL_KS8695_MAJOR 204
34 #define SERIAL_KS8695_MINOR 16
35 #define SERIAL_KS8695_DEVNAME "ttyAM"
37 #define SERIAL_KS8695_NR 1
40 * Access macros for the KS8695 UART
42 #define UART_GET_CHAR(p) (__raw_readl((p)->membase + KS8695_URRB) & 0xFF)
43 #define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + KS8695_URTH)
44 #define UART_GET_FCR(p) __raw_readl((p)->membase + KS8695_URFC)
45 #define UART_PUT_FCR(p, c) __raw_writel((c), (p)->membase + KS8695_URFC)
46 #define UART_GET_MSR(p) __raw_readl((p)->membase + KS8695_URMS)
47 #define UART_GET_LSR(p) __raw_readl((p)->membase + KS8695_URLS)
48 #define UART_GET_LCR(p) __raw_readl((p)->membase + KS8695_URLC)
49 #define UART_PUT_LCR(p, c) __raw_writel((c), (p)->membase + KS8695_URLC)
50 #define UART_GET_MCR(p) __raw_readl((p)->membase + KS8695_URMC)
51 #define UART_PUT_MCR(p, c) __raw_writel((c), (p)->membase + KS8695_URMC)
52 #define UART_GET_BRDR(p) __raw_readl((p)->membase + KS8695_URBD)
53 #define UART_PUT_BRDR(p, c) __raw_writel((c), (p)->membase + KS8695_URBD)
55 #define KS8695_CLR_TX_INT() __raw_writel(1 << KS8695_IRQ_UART_TX, KS8695_IRQ_VA + KS8695_INTST)
57 #define UART_DUMMY_LSR_RX 0x100
58 #define UART_PORT_SIZE (KS8695_USR - KS8695_URRB + 4)
60 static inline int tx_enabled(struct uart_port *port)
62 return port->unused[0] & 1;
65 static inline int rx_enabled(struct uart_port *port)
67 return port->unused[0] & 2;
70 static inline int ms_enabled(struct uart_port *port)
72 return port->unused[0] & 4;
75 static inline void ms_enable(struct uart_port *port, int enabled)
80 port->unused[0] &= ~4;
83 static inline void rx_enable(struct uart_port *port, int enabled)
88 port->unused[0] &= ~2;
91 static inline void tx_enable(struct uart_port *port, int enabled)
96 port->unused[0] &= ~1;
101 static struct console ks8695_console;
104 static void ks8695uart_stop_tx(struct uart_port *port)
106 if (tx_enabled(port)) {
107 /* use disable_irq_nosync() and not disable_irq() to avoid self
108 * imposed deadlock by not waiting for irq handler to end,
109 * since this ks8695uart_stop_tx() is called from interrupt context.
111 disable_irq_nosync(KS8695_IRQ_UART_TX);
116 static void ks8695uart_start_tx(struct uart_port *port)
118 if (!tx_enabled(port)) {
119 enable_irq(KS8695_IRQ_UART_TX);
124 static void ks8695uart_stop_rx(struct uart_port *port)
126 if (rx_enabled(port)) {
127 disable_irq(KS8695_IRQ_UART_RX);
132 static void ks8695uart_enable_ms(struct uart_port *port)
134 if (!ms_enabled(port)) {
135 enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
140 static void ks8695uart_disable_ms(struct uart_port *port)
142 if (ms_enabled(port)) {
143 disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
148 static irqreturn_t ks8695uart_rx_chars(int irq, void *dev_id)
150 struct uart_port *port = dev_id;
151 unsigned int status, ch, lsr, flg, max_count = 256;
153 status = UART_GET_LSR(port); /* clears pending LSR interrupts */
154 while ((status & URLS_URDR) && max_count--) {
155 ch = UART_GET_CHAR(port);
161 * Note that the error handling code is
162 * out of the main execution path
164 lsr = UART_GET_LSR(port) | UART_DUMMY_LSR_RX;
165 if (unlikely(lsr & (URLS_URBI | URLS_URPE | URLS_URFE | URLS_URROE))) {
166 if (lsr & URLS_URBI) {
167 lsr &= ~(URLS_URFE | URLS_URPE);
169 if (uart_handle_break(port))
173 port->icount.parity++;
175 port->icount.frame++;
176 if (lsr & URLS_URROE)
177 port->icount.overrun++;
179 lsr &= port->read_status_mask;
183 else if (lsr & URLS_URPE)
185 else if (lsr & URLS_URFE)
189 if (uart_handle_sysrq_char(port, ch))
192 uart_insert_char(port, lsr, URLS_URROE, ch, flg);
195 status = UART_GET_LSR(port);
197 tty_flip_buffer_push(&port->state->port);
203 static irqreturn_t ks8695uart_tx_chars(int irq, void *dev_id)
205 struct uart_port *port = dev_id;
206 struct circ_buf *xmit = &port->state->xmit;
211 UART_PUT_CHAR(port, port->x_char);
217 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
218 ks8695uart_stop_tx(port);
222 count = 16; /* fifo size */
223 while (!uart_circ_empty(xmit) && (count-- > 0)) {
225 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
227 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
231 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
232 uart_write_wakeup(port);
234 if (uart_circ_empty(xmit))
235 ks8695uart_stop_tx(port);
240 static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
242 struct uart_port *port = dev_id;
246 * clear modem interrupt by reading MSR
248 status = UART_GET_MSR(port);
250 if (status & URMS_URDDCD)
251 uart_handle_dcd_change(port, status & URMS_URDDCD);
253 if (status & URMS_URDDST)
256 if (status & URMS_URDCTS)
257 uart_handle_cts_change(port, status & URMS_URDCTS);
259 if (status & URMS_URTERI)
262 wake_up_interruptible(&port->state->port.delta_msr_wait);
267 static unsigned int ks8695uart_tx_empty(struct uart_port *port)
269 return (UART_GET_LSR(port) & URLS_URTE) ? TIOCSER_TEMT : 0;
272 static unsigned int ks8695uart_get_mctrl(struct uart_port *port)
274 unsigned int result = 0;
277 status = UART_GET_MSR(port);
278 if (status & URMS_URDCD)
280 if (status & URMS_URDSR)
282 if (status & URMS_URCTS)
284 if (status & URMS_URRI)
290 static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
294 mcr = UART_GET_MCR(port);
295 if (mctrl & TIOCM_RTS)
300 if (mctrl & TIOCM_DTR)
305 UART_PUT_MCR(port, mcr);
308 static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
312 lcr = UART_GET_LCR(port);
314 if (break_state == -1)
319 UART_PUT_LCR(port, lcr);
322 static int ks8695uart_startup(struct uart_port *port)
326 irq_modify_status(KS8695_IRQ_UART_TX, IRQ_NOREQUEST, IRQ_NOAUTOEN);
334 retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, 0, "UART TX", port);
338 retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, 0, "UART RX", port);
342 retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, 0, "UART LineStatus", port);
346 retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, 0, "UART ModemStatus", port);
353 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
355 free_irq(KS8695_IRQ_UART_RX, port);
357 free_irq(KS8695_IRQ_UART_TX, port);
362 static void ks8695uart_shutdown(struct uart_port *port)
367 free_irq(KS8695_IRQ_UART_RX, port);
368 free_irq(KS8695_IRQ_UART_TX, port);
369 free_irq(KS8695_IRQ_UART_MODEM_STATUS, port);
370 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
372 /* disable break condition and fifos */
373 UART_PUT_LCR(port, UART_GET_LCR(port) & ~URLC_URSBC);
374 UART_PUT_FCR(port, UART_GET_FCR(port) & ~URFC_URFE);
377 static void ks8695uart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
379 unsigned int lcr, fcr = 0;
381 unsigned int baud, quot;
384 * Ask the core to calculate the divisor for us.
386 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
387 quot = uart_get_divisor(port, baud);
389 switch (termios->c_cflag & CSIZE) {
405 if (termios->c_cflag & CSTOPB)
409 if (termios->c_cflag & PARENB) {
410 if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
411 if (termios->c_cflag & PARODD)
416 else if (termios->c_cflag & PARODD)
422 if (port->fifosize > 1)
423 fcr = URFC_URFRT_8 | URFC_URTFR | URFC_URRFR | URFC_URFE;
425 spin_lock_irqsave(&port->lock, flags);
428 * Update the per-port timeout.
430 uart_update_timeout(port, termios->c_cflag, baud);
432 port->read_status_mask = URLS_URROE;
433 if (termios->c_iflag & INPCK)
434 port->read_status_mask |= (URLS_URFE | URLS_URPE);
435 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
436 port->read_status_mask |= URLS_URBI;
439 * Characters to ignore
441 port->ignore_status_mask = 0;
442 if (termios->c_iflag & IGNPAR)
443 port->ignore_status_mask |= (URLS_URFE | URLS_URPE);
444 if (termios->c_iflag & IGNBRK) {
445 port->ignore_status_mask |= URLS_URBI;
447 * If we're ignoring parity and break indicators,
448 * ignore overruns too (for real raw support).
450 if (termios->c_iflag & IGNPAR)
451 port->ignore_status_mask |= URLS_URROE;
455 * Ignore all characters if CREAD is not set.
457 if ((termios->c_cflag & CREAD) == 0)
458 port->ignore_status_mask |= UART_DUMMY_LSR_RX;
460 /* first, disable everything */
461 if (UART_ENABLE_MS(port, termios->c_cflag))
462 ks8695uart_enable_ms(port);
464 ks8695uart_disable_ms(port);
467 UART_PUT_BRDR(port, quot);
469 UART_PUT_LCR(port, lcr);
470 UART_PUT_FCR(port, fcr);
472 spin_unlock_irqrestore(&port->lock, flags);
475 static const char *ks8695uart_type(struct uart_port *port)
477 return port->type == PORT_KS8695 ? "KS8695" : NULL;
481 * Release the memory region(s) being used by 'port'
483 static void ks8695uart_release_port(struct uart_port *port)
485 release_mem_region(port->mapbase, UART_PORT_SIZE);
489 * Request the memory region(s) being used by 'port'
491 static int ks8695uart_request_port(struct uart_port *port)
493 return request_mem_region(port->mapbase, UART_PORT_SIZE,
494 "serial_ks8695") != NULL ? 0 : -EBUSY;
498 * Configure/autoconfigure the port.
500 static void ks8695uart_config_port(struct uart_port *port, int flags)
502 if (flags & UART_CONFIG_TYPE) {
503 port->type = PORT_KS8695;
504 ks8695uart_request_port(port);
509 * verify the new serial_struct (for TIOCSSERIAL).
511 static int ks8695uart_verify_port(struct uart_port *port, struct serial_struct *ser)
515 if (ser->type != PORT_UNKNOWN && ser->type != PORT_KS8695)
517 if (ser->irq != port->irq)
519 if (ser->baud_base < 9600)
524 static struct uart_ops ks8695uart_pops = {
525 .tx_empty = ks8695uart_tx_empty,
526 .set_mctrl = ks8695uart_set_mctrl,
527 .get_mctrl = ks8695uart_get_mctrl,
528 .stop_tx = ks8695uart_stop_tx,
529 .start_tx = ks8695uart_start_tx,
530 .stop_rx = ks8695uart_stop_rx,
531 .enable_ms = ks8695uart_enable_ms,
532 .break_ctl = ks8695uart_break_ctl,
533 .startup = ks8695uart_startup,
534 .shutdown = ks8695uart_shutdown,
535 .set_termios = ks8695uart_set_termios,
536 .type = ks8695uart_type,
537 .release_port = ks8695uart_release_port,
538 .request_port = ks8695uart_request_port,
539 .config_port = ks8695uart_config_port,
540 .verify_port = ks8695uart_verify_port,
543 static struct uart_port ks8695uart_ports[SERIAL_KS8695_NR] = {
545 .membase = KS8695_UART_VA,
546 .mapbase = KS8695_UART_PA,
547 .iotype = SERIAL_IO_MEM,
548 .irq = KS8695_IRQ_UART_TX,
549 .uartclk = KS8695_CLOCK_RATE * 16,
551 .ops = &ks8695uart_pops,
552 .flags = UPF_BOOT_AUTOCONF,
557 #ifdef CONFIG_SERIAL_KS8695_CONSOLE
558 static void ks8695_console_putchar(struct uart_port *port, int ch)
560 while (!(UART_GET_LSR(port) & URLS_URTHRE))
563 UART_PUT_CHAR(port, ch);
566 static void ks8695_console_write(struct console *co, const char *s, u_int count)
568 struct uart_port *port = ks8695uart_ports + co->index;
570 uart_console_write(port, s, count, ks8695_console_putchar);
573 static void __init ks8695_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
577 lcr = UART_GET_LCR(port);
579 switch (lcr & URLC_PARITY) {
590 switch (lcr & URLC_URCL) {
604 *baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
609 static int __init ks8695_console_setup(struct console *co, char *options)
611 struct uart_port *port;
618 * Check whether an invalid uart number has been specified, and
619 * if so, search for the first available port that does have
622 port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
625 uart_parse_options(options, &baud, &parity, &bits, &flow);
627 ks8695_console_get_options(port, &baud, &parity, &bits);
629 return uart_set_options(port, co, baud, parity, bits, flow);
632 static struct uart_driver ks8695_reg;
634 static struct console ks8695_console = {
635 .name = SERIAL_KS8695_DEVNAME,
636 .write = ks8695_console_write,
637 .device = uart_console_device,
638 .setup = ks8695_console_setup,
639 .flags = CON_PRINTBUFFER,
644 static int __init ks8695_console_init(void)
646 add_preferred_console(SERIAL_KS8695_DEVNAME, 0, NULL);
647 register_console(&ks8695_console);
651 console_initcall(ks8695_console_init);
653 #define KS8695_CONSOLE &ks8695_console
655 #define KS8695_CONSOLE NULL
658 static struct uart_driver ks8695_reg = {
659 .owner = THIS_MODULE,
660 .driver_name = "serial_ks8695",
661 .dev_name = SERIAL_KS8695_DEVNAME,
662 .major = SERIAL_KS8695_MAJOR,
663 .minor = SERIAL_KS8695_MINOR,
664 .nr = SERIAL_KS8695_NR,
665 .cons = KS8695_CONSOLE,
668 static int __init ks8695uart_init(void)
672 printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
674 ret = uart_register_driver(&ks8695_reg);
678 for (i = 0; i < SERIAL_KS8695_NR; i++)
679 uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
684 static void __exit ks8695uart_exit(void)
688 for (i = 0; i < SERIAL_KS8695_NR; i++)
689 uart_remove_one_port(&ks8695_reg, &ks8695uart_ports[0]);
690 uart_unregister_driver(&ks8695_reg);
693 module_init(ks8695uart_init);
694 module_exit(ks8695uart_exit);
696 MODULE_DESCRIPTION("KS8695 serial port driver");
697 MODULE_AUTHOR("Micrel Inc.");
698 MODULE_LICENSE("GPL");