Linux 4.16.11
[linux/fpc-iii.git] / drivers / tty / serial / serial_ks8695.c
blobb461d791188cb5e8be99c2e9de126fc535ec0b5d
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Driver for KS8695 serial ports
5 * Based on drivers/serial/serial_amba.c, by Kam Lee.
7 * Copyright 2002-2005 Micrel Inc.
8 */
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>
19 #include <asm/io.h>
20 #include <asm/irq.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)
27 #define SUPPORT_SYSRQ
28 #endif
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)
77 if(enabled)
78 port->unused[0] |= 4;
79 else
80 port->unused[0] &= ~4;
83 static inline void rx_enable(struct uart_port *port, int enabled)
85 if(enabled)
86 port->unused[0] |= 2;
87 else
88 port->unused[0] &= ~2;
91 static inline void tx_enable(struct uart_port *port, int enabled)
93 if(enabled)
94 port->unused[0] |= 1;
95 else
96 port->unused[0] &= ~1;
100 #ifdef SUPPORT_SYSRQ
101 static struct console ks8695_console;
102 #endif
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);
112 tx_enable(port, 0);
116 static void ks8695uart_start_tx(struct uart_port *port)
118 if (!tx_enabled(port)) {
119 enable_irq(KS8695_IRQ_UART_TX);
120 tx_enable(port, 1);
124 static void ks8695uart_stop_rx(struct uart_port *port)
126 if (rx_enabled(port)) {
127 disable_irq(KS8695_IRQ_UART_RX);
128 rx_enable(port, 0);
132 static void ks8695uart_enable_ms(struct uart_port *port)
134 if (!ms_enabled(port)) {
135 enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
136 ms_enable(port,1);
140 static void ks8695uart_disable_ms(struct uart_port *port)
142 if (ms_enabled(port)) {
143 disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
144 ms_enable(port,0);
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);
156 flg = TTY_NORMAL;
158 port->icount.rx++;
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);
168 port->icount.brk++;
169 if (uart_handle_break(port))
170 goto ignore_char;
172 if (lsr & URLS_URPE)
173 port->icount.parity++;
174 if (lsr & URLS_URFE)
175 port->icount.frame++;
176 if (lsr & URLS_URROE)
177 port->icount.overrun++;
179 lsr &= port->read_status_mask;
181 if (lsr & URLS_URBI)
182 flg = TTY_BREAK;
183 else if (lsr & URLS_URPE)
184 flg = TTY_PARITY;
185 else if (lsr & URLS_URFE)
186 flg = TTY_FRAME;
189 if (uart_handle_sysrq_char(port, ch))
190 goto ignore_char;
192 uart_insert_char(port, lsr, URLS_URROE, ch, flg);
194 ignore_char:
195 status = UART_GET_LSR(port);
197 tty_flip_buffer_push(&port->state->port);
199 return IRQ_HANDLED;
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;
207 unsigned int count;
209 if (port->x_char) {
210 KS8695_CLR_TX_INT();
211 UART_PUT_CHAR(port, port->x_char);
212 port->icount.tx++;
213 port->x_char = 0;
214 return IRQ_HANDLED;
217 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
218 ks8695uart_stop_tx(port);
219 return IRQ_HANDLED;
222 count = 16; /* fifo size */
223 while (!uart_circ_empty(xmit) && (count-- > 0)) {
224 KS8695_CLR_TX_INT();
225 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
227 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
228 port->icount.tx++;
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);
237 return IRQ_HANDLED;
240 static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
242 struct uart_port *port = dev_id;
243 unsigned int status;
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)
254 port->icount.dsr++;
256 if (status & URMS_URDCTS)
257 uart_handle_cts_change(port, status & URMS_URDCTS);
259 if (status & URMS_URTERI)
260 port->icount.rng++;
262 wake_up_interruptible(&port->state->port.delta_msr_wait);
264 return IRQ_HANDLED;
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;
275 unsigned int status;
277 status = UART_GET_MSR(port);
278 if (status & URMS_URDCD)
279 result |= TIOCM_CAR;
280 if (status & URMS_URDSR)
281 result |= TIOCM_DSR;
282 if (status & URMS_URCTS)
283 result |= TIOCM_CTS;
284 if (status & URMS_URRI)
285 result |= TIOCM_RI;
287 return result;
290 static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
292 unsigned int mcr;
294 mcr = UART_GET_MCR(port);
295 if (mctrl & TIOCM_RTS)
296 mcr |= URMC_URRTS;
297 else
298 mcr &= ~URMC_URRTS;
300 if (mctrl & TIOCM_DTR)
301 mcr |= URMC_URDTR;
302 else
303 mcr &= ~URMC_URDTR;
305 UART_PUT_MCR(port, mcr);
308 static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
310 unsigned int lcr;
312 lcr = UART_GET_LCR(port);
314 if (break_state == -1)
315 lcr |= URLC_URSBC;
316 else
317 lcr &= ~URLC_URSBC;
319 UART_PUT_LCR(port, lcr);
322 static int ks8695uart_startup(struct uart_port *port)
324 int retval;
326 irq_modify_status(KS8695_IRQ_UART_TX, IRQ_NOREQUEST, IRQ_NOAUTOEN);
327 tx_enable(port, 0);
328 rx_enable(port, 1);
329 ms_enable(port, 1);
332 * Allocate the IRQ
334 retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, 0, "UART TX", port);
335 if (retval)
336 goto err_tx;
338 retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, 0, "UART RX", port);
339 if (retval)
340 goto err_rx;
342 retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, 0, "UART LineStatus", port);
343 if (retval)
344 goto err_ls;
346 retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, 0, "UART ModemStatus", port);
347 if (retval)
348 goto err_ms;
350 return 0;
352 err_ms:
353 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
354 err_ls:
355 free_irq(KS8695_IRQ_UART_RX, port);
356 err_rx:
357 free_irq(KS8695_IRQ_UART_TX, port);
358 err_tx:
359 return retval;
362 static void ks8695uart_shutdown(struct uart_port *port)
365 * Free the interrupt
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;
380 unsigned long flags;
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) {
390 case CS5:
391 lcr = URCL_5;
392 break;
393 case CS6:
394 lcr = URCL_6;
395 break;
396 case CS7:
397 lcr = URCL_7;
398 break;
399 default:
400 lcr = URCL_8;
401 break;
404 /* stop bits */
405 if (termios->c_cflag & CSTOPB)
406 lcr |= URLC_URSB;
408 /* parity */
409 if (termios->c_cflag & PARENB) {
410 if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
411 if (termios->c_cflag & PARODD)
412 lcr |= URPE_MARK;
413 else
414 lcr |= URPE_SPACE;
416 else if (termios->c_cflag & PARODD)
417 lcr |= URPE_ODD;
418 else
419 lcr |= URPE_EVEN;
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);
463 else
464 ks8695uart_disable_ms(port);
466 /* Set baud rate */
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)
513 int ret = 0;
515 if (ser->type != PORT_UNKNOWN && ser->type != PORT_KS8695)
516 ret = -EINVAL;
517 if (ser->irq != port->irq)
518 ret = -EINVAL;
519 if (ser->baud_base < 9600)
520 ret = -EINVAL;
521 return ret;
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,
550 .fifosize = 16,
551 .ops = &ks8695uart_pops,
552 .flags = UPF_BOOT_AUTOCONF,
553 .line = 0,
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))
561 barrier();
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)
575 unsigned int lcr;
577 lcr = UART_GET_LCR(port);
579 switch (lcr & URLC_PARITY) {
580 case URPE_ODD:
581 *parity = 'o';
582 break;
583 case URPE_EVEN:
584 *parity = 'e';
585 break;
586 default:
587 *parity = 'n';
590 switch (lcr & URLC_URCL) {
591 case URCL_5:
592 *bits = 5;
593 break;
594 case URCL_6:
595 *bits = 6;
596 break;
597 case URCL_7:
598 *bits = 7;
599 break;
600 default:
601 *bits = 8;
604 *baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
605 *baud /= 16;
606 *baud &= 0xFFFFFFF0;
609 static int __init ks8695_console_setup(struct console *co, char *options)
611 struct uart_port *port;
612 int baud = 115200;
613 int bits = 8;
614 int parity = 'n';
615 int flow = 'n';
618 * Check whether an invalid uart number has been specified, and
619 * if so, search for the first available port that does have
620 * console support.
622 port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
624 if (options)
625 uart_parse_options(options, &baud, &parity, &bits, &flow);
626 else
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,
640 .index = -1,
641 .data = &ks8695_reg,
644 static int __init ks8695_console_init(void)
646 add_preferred_console(SERIAL_KS8695_DEVNAME, 0, NULL);
647 register_console(&ks8695_console);
648 return 0;
651 console_initcall(ks8695_console_init);
653 #define KS8695_CONSOLE &ks8695_console
654 #else
655 #define KS8695_CONSOLE NULL
656 #endif
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)
670 int i, ret;
672 printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
674 ret = uart_register_driver(&ks8695_reg);
675 if (ret)
676 return ret;
678 for (i = 0; i < SERIAL_KS8695_NR; i++)
679 uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
681 return 0;
684 static void __exit ks8695uart_exit(void)
686 int i;
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");