Loading...
1/*
2 * Driver for KS8695 serial ports
3 *
4 * Based on drivers/serial/serial_amba.c, by Kam Lee.
5 *
6 * Copyright 2002-2005 Micrel Inc.
7 *
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.
12 *
13 */
14#include <linux/module.h>
15#include <linux/tty.h>
16#include <linux/ioport.h>
17#include <linux/init.h>
18#include <linux/serial.h>
19#include <linux/console.h>
20#include <linux/sysrq.h>
21#include <linux/device.h>
22
23#include <asm/io.h>
24#include <asm/irq.h>
25#include <asm/mach/irq.h>
26
27#include <mach/regs-uart.h>
28#include <mach/regs-irq.h>
29
30#if defined(CONFIG_SERIAL_KS8695_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
31#define SUPPORT_SYSRQ
32#endif
33
34#include <linux/serial_core.h>
35
36
37#define SERIAL_KS8695_MAJOR 204
38#define SERIAL_KS8695_MINOR 16
39#define SERIAL_KS8695_DEVNAME "ttyAM"
40
41#define SERIAL_KS8695_NR 1
42
43/*
44 * Access macros for the KS8695 UART
45 */
46#define UART_GET_CHAR(p) (__raw_readl((p)->membase + KS8695_URRB) & 0xFF)
47#define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + KS8695_URTH)
48#define UART_GET_FCR(p) __raw_readl((p)->membase + KS8695_URFC)
49#define UART_PUT_FCR(p, c) __raw_writel((c), (p)->membase + KS8695_URFC)
50#define UART_GET_MSR(p) __raw_readl((p)->membase + KS8695_URMS)
51#define UART_GET_LSR(p) __raw_readl((p)->membase + KS8695_URLS)
52#define UART_GET_LCR(p) __raw_readl((p)->membase + KS8695_URLC)
53#define UART_PUT_LCR(p, c) __raw_writel((c), (p)->membase + KS8695_URLC)
54#define UART_GET_MCR(p) __raw_readl((p)->membase + KS8695_URMC)
55#define UART_PUT_MCR(p, c) __raw_writel((c), (p)->membase + KS8695_URMC)
56#define UART_GET_BRDR(p) __raw_readl((p)->membase + KS8695_URBD)
57#define UART_PUT_BRDR(p, c) __raw_writel((c), (p)->membase + KS8695_URBD)
58
59#define KS8695_CLR_TX_INT() __raw_writel(1 << KS8695_IRQ_UART_TX, KS8695_IRQ_VA + KS8695_INTST)
60
61#define UART_DUMMY_LSR_RX 0x100
62#define UART_PORT_SIZE (KS8695_USR - KS8695_URRB + 4)
63
64static inline int tx_enabled(struct uart_port *port)
65{
66 return port->unused[0] & 1;
67}
68
69static inline int rx_enabled(struct uart_port *port)
70{
71 return port->unused[0] & 2;
72}
73
74static inline int ms_enabled(struct uart_port *port)
75{
76 return port->unused[0] & 4;
77}
78
79static inline void ms_enable(struct uart_port *port, int enabled)
80{
81 if(enabled)
82 port->unused[0] |= 4;
83 else
84 port->unused[0] &= ~4;
85}
86
87static inline void rx_enable(struct uart_port *port, int enabled)
88{
89 if(enabled)
90 port->unused[0] |= 2;
91 else
92 port->unused[0] &= ~2;
93}
94
95static inline void tx_enable(struct uart_port *port, int enabled)
96{
97 if(enabled)
98 port->unused[0] |= 1;
99 else
100 port->unused[0] &= ~1;
101}
102
103
104#ifdef SUPPORT_SYSRQ
105static struct console ks8695_console;
106#endif
107
108static void ks8695uart_stop_tx(struct uart_port *port)
109{
110 if (tx_enabled(port)) {
111 /* use disable_irq_nosync() and not disable_irq() to avoid self
112 * imposed deadlock by not waiting for irq handler to end,
113 * since this ks8695uart_stop_tx() is called from interrupt context.
114 */
115 disable_irq_nosync(KS8695_IRQ_UART_TX);
116 tx_enable(port, 0);
117 }
118}
119
120static void ks8695uart_start_tx(struct uart_port *port)
121{
122 if (!tx_enabled(port)) {
123 enable_irq(KS8695_IRQ_UART_TX);
124 tx_enable(port, 1);
125 }
126}
127
128static void ks8695uart_stop_rx(struct uart_port *port)
129{
130 if (rx_enabled(port)) {
131 disable_irq(KS8695_IRQ_UART_RX);
132 rx_enable(port, 0);
133 }
134}
135
136static void ks8695uart_enable_ms(struct uart_port *port)
137{
138 if (!ms_enabled(port)) {
139 enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
140 ms_enable(port,1);
141 }
142}
143
144static void ks8695uart_disable_ms(struct uart_port *port)
145{
146 if (ms_enabled(port)) {
147 disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
148 ms_enable(port,0);
149 }
150}
151
152static irqreturn_t ks8695uart_rx_chars(int irq, void *dev_id)
153{
154 struct uart_port *port = dev_id;
155 struct tty_struct *tty = port->state->port.tty;
156 unsigned int status, ch, lsr, flg, max_count = 256;
157
158 status = UART_GET_LSR(port); /* clears pending LSR interrupts */
159 while ((status & URLS_URDR) && max_count--) {
160 ch = UART_GET_CHAR(port);
161 flg = TTY_NORMAL;
162
163 port->icount.rx++;
164
165 /*
166 * Note that the error handling code is
167 * out of the main execution path
168 */
169 lsr = UART_GET_LSR(port) | UART_DUMMY_LSR_RX;
170 if (unlikely(lsr & (URLS_URBI | URLS_URPE | URLS_URFE | URLS_URROE))) {
171 if (lsr & URLS_URBI) {
172 lsr &= ~(URLS_URFE | URLS_URPE);
173 port->icount.brk++;
174 if (uart_handle_break(port))
175 goto ignore_char;
176 }
177 if (lsr & URLS_URPE)
178 port->icount.parity++;
179 if (lsr & URLS_URFE)
180 port->icount.frame++;
181 if (lsr & URLS_URROE)
182 port->icount.overrun++;
183
184 lsr &= port->read_status_mask;
185
186 if (lsr & URLS_URBI)
187 flg = TTY_BREAK;
188 else if (lsr & URLS_URPE)
189 flg = TTY_PARITY;
190 else if (lsr & URLS_URFE)
191 flg = TTY_FRAME;
192 }
193
194 if (uart_handle_sysrq_char(port, ch))
195 goto ignore_char;
196
197 uart_insert_char(port, lsr, URLS_URROE, ch, flg);
198
199ignore_char:
200 status = UART_GET_LSR(port);
201 }
202 tty_flip_buffer_push(tty);
203
204 return IRQ_HANDLED;
205}
206
207
208static irqreturn_t ks8695uart_tx_chars(int irq, void *dev_id)
209{
210 struct uart_port *port = dev_id;
211 struct circ_buf *xmit = &port->state->xmit;
212 unsigned int count;
213
214 if (port->x_char) {
215 KS8695_CLR_TX_INT();
216 UART_PUT_CHAR(port, port->x_char);
217 port->icount.tx++;
218 port->x_char = 0;
219 return IRQ_HANDLED;
220 }
221
222 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
223 ks8695uart_stop_tx(port);
224 return IRQ_HANDLED;
225 }
226
227 count = 16; /* fifo size */
228 while (!uart_circ_empty(xmit) && (count-- > 0)) {
229 KS8695_CLR_TX_INT();
230 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
231
232 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
233 port->icount.tx++;
234 }
235
236 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
237 uart_write_wakeup(port);
238
239 if (uart_circ_empty(xmit))
240 ks8695uart_stop_tx(port);
241
242 return IRQ_HANDLED;
243}
244
245static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
246{
247 struct uart_port *port = dev_id;
248 unsigned int status;
249
250 /*
251 * clear modem interrupt by reading MSR
252 */
253 status = UART_GET_MSR(port);
254
255 if (status & URMS_URDDCD)
256 uart_handle_dcd_change(port, status & URMS_URDDCD);
257
258 if (status & URMS_URDDST)
259 port->icount.dsr++;
260
261 if (status & URMS_URDCTS)
262 uart_handle_cts_change(port, status & URMS_URDCTS);
263
264 if (status & URMS_URTERI)
265 port->icount.rng++;
266
267 wake_up_interruptible(&port->state->port.delta_msr_wait);
268
269 return IRQ_HANDLED;
270}
271
272static unsigned int ks8695uart_tx_empty(struct uart_port *port)
273{
274 return (UART_GET_LSR(port) & URLS_URTE) ? TIOCSER_TEMT : 0;
275}
276
277static unsigned int ks8695uart_get_mctrl(struct uart_port *port)
278{
279 unsigned int result = 0;
280 unsigned int status;
281
282 status = UART_GET_MSR(port);
283 if (status & URMS_URDCD)
284 result |= TIOCM_CAR;
285 if (status & URMS_URDSR)
286 result |= TIOCM_DSR;
287 if (status & URMS_URCTS)
288 result |= TIOCM_CTS;
289 if (status & URMS_URRI)
290 result |= TIOCM_RI;
291
292 return result;
293}
294
295static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
296{
297 unsigned int mcr;
298
299 mcr = UART_GET_MCR(port);
300 if (mctrl & TIOCM_RTS)
301 mcr |= URMC_URRTS;
302 else
303 mcr &= ~URMC_URRTS;
304
305 if (mctrl & TIOCM_DTR)
306 mcr |= URMC_URDTR;
307 else
308 mcr &= ~URMC_URDTR;
309
310 UART_PUT_MCR(port, mcr);
311}
312
313static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
314{
315 unsigned int lcr;
316
317 lcr = UART_GET_LCR(port);
318
319 if (break_state == -1)
320 lcr |= URLC_URSBC;
321 else
322 lcr &= ~URLC_URSBC;
323
324 UART_PUT_LCR(port, lcr);
325}
326
327static int ks8695uart_startup(struct uart_port *port)
328{
329 int retval;
330
331 set_irq_flags(KS8695_IRQ_UART_TX, IRQF_VALID | IRQF_NOAUTOEN);
332 tx_enable(port, 0);
333 rx_enable(port, 1);
334 ms_enable(port, 1);
335
336 /*
337 * Allocate the IRQ
338 */
339 retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, IRQF_DISABLED, "UART TX", port);
340 if (retval)
341 goto err_tx;
342
343 retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, IRQF_DISABLED, "UART RX", port);
344 if (retval)
345 goto err_rx;
346
347 retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, IRQF_DISABLED, "UART LineStatus", port);
348 if (retval)
349 goto err_ls;
350
351 retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, IRQF_DISABLED, "UART ModemStatus", port);
352 if (retval)
353 goto err_ms;
354
355 return 0;
356
357err_ms:
358 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
359err_ls:
360 free_irq(KS8695_IRQ_UART_RX, port);
361err_rx:
362 free_irq(KS8695_IRQ_UART_TX, port);
363err_tx:
364 return retval;
365}
366
367static void ks8695uart_shutdown(struct uart_port *port)
368{
369 /*
370 * Free the interrupt
371 */
372 free_irq(KS8695_IRQ_UART_RX, port);
373 free_irq(KS8695_IRQ_UART_TX, port);
374 free_irq(KS8695_IRQ_UART_MODEM_STATUS, port);
375 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
376
377 /* disable break condition and fifos */
378 UART_PUT_LCR(port, UART_GET_LCR(port) & ~URLC_URSBC);
379 UART_PUT_FCR(port, UART_GET_FCR(port) & ~URFC_URFE);
380}
381
382static void ks8695uart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
383{
384 unsigned int lcr, fcr = 0;
385 unsigned long flags;
386 unsigned int baud, quot;
387
388 /*
389 * Ask the core to calculate the divisor for us.
390 */
391 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
392 quot = uart_get_divisor(port, baud);
393
394 switch (termios->c_cflag & CSIZE) {
395 case CS5:
396 lcr = URCL_5;
397 break;
398 case CS6:
399 lcr = URCL_6;
400 break;
401 case CS7:
402 lcr = URCL_7;
403 break;
404 default:
405 lcr = URCL_8;
406 break;
407 }
408
409 /* stop bits */
410 if (termios->c_cflag & CSTOPB)
411 lcr |= URLC_URSB;
412
413 /* parity */
414 if (termios->c_cflag & PARENB) {
415 if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
416 if (termios->c_cflag & PARODD)
417 lcr |= URPE_MARK;
418 else
419 lcr |= URPE_SPACE;
420 }
421 else if (termios->c_cflag & PARODD)
422 lcr |= URPE_ODD;
423 else
424 lcr |= URPE_EVEN;
425 }
426
427 if (port->fifosize > 1)
428 fcr = URFC_URFRT_8 | URFC_URTFR | URFC_URRFR | URFC_URFE;
429
430 spin_lock_irqsave(&port->lock, flags);
431
432 /*
433 * Update the per-port timeout.
434 */
435 uart_update_timeout(port, termios->c_cflag, baud);
436
437 port->read_status_mask = URLS_URROE;
438 if (termios->c_iflag & INPCK)
439 port->read_status_mask |= (URLS_URFE | URLS_URPE);
440 if (termios->c_iflag & (BRKINT | PARMRK))
441 port->read_status_mask |= URLS_URBI;
442
443 /*
444 * Characters to ignore
445 */
446 port->ignore_status_mask = 0;
447 if (termios->c_iflag & IGNPAR)
448 port->ignore_status_mask |= (URLS_URFE | URLS_URPE);
449 if (termios->c_iflag & IGNBRK) {
450 port->ignore_status_mask |= URLS_URBI;
451 /*
452 * If we're ignoring parity and break indicators,
453 * ignore overruns too (for real raw support).
454 */
455 if (termios->c_iflag & IGNPAR)
456 port->ignore_status_mask |= URLS_URROE;
457 }
458
459 /*
460 * Ignore all characters if CREAD is not set.
461 */
462 if ((termios->c_cflag & CREAD) == 0)
463 port->ignore_status_mask |= UART_DUMMY_LSR_RX;
464
465 /* first, disable everything */
466 if (UART_ENABLE_MS(port, termios->c_cflag))
467 ks8695uart_enable_ms(port);
468 else
469 ks8695uart_disable_ms(port);
470
471 /* Set baud rate */
472 UART_PUT_BRDR(port, quot);
473
474 UART_PUT_LCR(port, lcr);
475 UART_PUT_FCR(port, fcr);
476
477 spin_unlock_irqrestore(&port->lock, flags);
478}
479
480static const char *ks8695uart_type(struct uart_port *port)
481{
482 return port->type == PORT_KS8695 ? "KS8695" : NULL;
483}
484
485/*
486 * Release the memory region(s) being used by 'port'
487 */
488static void ks8695uart_release_port(struct uart_port *port)
489{
490 release_mem_region(port->mapbase, UART_PORT_SIZE);
491}
492
493/*
494 * Request the memory region(s) being used by 'port'
495 */
496static int ks8695uart_request_port(struct uart_port *port)
497{
498 return request_mem_region(port->mapbase, UART_PORT_SIZE,
499 "serial_ks8695") != NULL ? 0 : -EBUSY;
500}
501
502/*
503 * Configure/autoconfigure the port.
504 */
505static void ks8695uart_config_port(struct uart_port *port, int flags)
506{
507 if (flags & UART_CONFIG_TYPE) {
508 port->type = PORT_KS8695;
509 ks8695uart_request_port(port);
510 }
511}
512
513/*
514 * verify the new serial_struct (for TIOCSSERIAL).
515 */
516static int ks8695uart_verify_port(struct uart_port *port, struct serial_struct *ser)
517{
518 int ret = 0;
519
520 if (ser->type != PORT_UNKNOWN && ser->type != PORT_KS8695)
521 ret = -EINVAL;
522 if (ser->irq != port->irq)
523 ret = -EINVAL;
524 if (ser->baud_base < 9600)
525 ret = -EINVAL;
526 return ret;
527}
528
529static struct uart_ops ks8695uart_pops = {
530 .tx_empty = ks8695uart_tx_empty,
531 .set_mctrl = ks8695uart_set_mctrl,
532 .get_mctrl = ks8695uart_get_mctrl,
533 .stop_tx = ks8695uart_stop_tx,
534 .start_tx = ks8695uart_start_tx,
535 .stop_rx = ks8695uart_stop_rx,
536 .enable_ms = ks8695uart_enable_ms,
537 .break_ctl = ks8695uart_break_ctl,
538 .startup = ks8695uart_startup,
539 .shutdown = ks8695uart_shutdown,
540 .set_termios = ks8695uart_set_termios,
541 .type = ks8695uart_type,
542 .release_port = ks8695uart_release_port,
543 .request_port = ks8695uart_request_port,
544 .config_port = ks8695uart_config_port,
545 .verify_port = ks8695uart_verify_port,
546};
547
548static struct uart_port ks8695uart_ports[SERIAL_KS8695_NR] = {
549 {
550 .membase = (void *) KS8695_UART_VA,
551 .mapbase = KS8695_UART_VA,
552 .iotype = SERIAL_IO_MEM,
553 .irq = KS8695_IRQ_UART_TX,
554 .uartclk = KS8695_CLOCK_RATE * 16,
555 .fifosize = 16,
556 .ops = &ks8695uart_pops,
557 .flags = ASYNC_BOOT_AUTOCONF,
558 .line = 0,
559 }
560};
561
562#ifdef CONFIG_SERIAL_KS8695_CONSOLE
563static void ks8695_console_putchar(struct uart_port *port, int ch)
564{
565 while (!(UART_GET_LSR(port) & URLS_URTHRE))
566 barrier();
567
568 UART_PUT_CHAR(port, ch);
569}
570
571static void ks8695_console_write(struct console *co, const char *s, u_int count)
572{
573 struct uart_port *port = ks8695uart_ports + co->index;
574
575 uart_console_write(port, s, count, ks8695_console_putchar);
576}
577
578static void __init ks8695_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
579{
580 unsigned int lcr;
581
582 lcr = UART_GET_LCR(port);
583
584 switch (lcr & URLC_PARITY) {
585 case URPE_ODD:
586 *parity = 'o';
587 break;
588 case URPE_EVEN:
589 *parity = 'e';
590 break;
591 default:
592 *parity = 'n';
593 }
594
595 switch (lcr & URLC_URCL) {
596 case URCL_5:
597 *bits = 5;
598 break;
599 case URCL_6:
600 *bits = 6;
601 break;
602 case URCL_7:
603 *bits = 7;
604 break;
605 default:
606 *bits = 8;
607 }
608
609 *baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
610 *baud /= 16;
611 *baud &= 0xFFFFFFF0;
612}
613
614static int __init ks8695_console_setup(struct console *co, char *options)
615{
616 struct uart_port *port;
617 int baud = 115200;
618 int bits = 8;
619 int parity = 'n';
620 int flow = 'n';
621
622 /*
623 * Check whether an invalid uart number has been specified, and
624 * if so, search for the first available port that does have
625 * console support.
626 */
627 port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
628
629 if (options)
630 uart_parse_options(options, &baud, &parity, &bits, &flow);
631 else
632 ks8695_console_get_options(port, &baud, &parity, &bits);
633
634 return uart_set_options(port, co, baud, parity, bits, flow);
635}
636
637static struct uart_driver ks8695_reg;
638
639static struct console ks8695_console = {
640 .name = SERIAL_KS8695_DEVNAME,
641 .write = ks8695_console_write,
642 .device = uart_console_device,
643 .setup = ks8695_console_setup,
644 .flags = CON_PRINTBUFFER,
645 .index = -1,
646 .data = &ks8695_reg,
647};
648
649static int __init ks8695_console_init(void)
650{
651 add_preferred_console(SERIAL_KS8695_DEVNAME, 0, NULL);
652 register_console(&ks8695_console);
653 return 0;
654}
655
656console_initcall(ks8695_console_init);
657
658#define KS8695_CONSOLE &ks8695_console
659#else
660#define KS8695_CONSOLE NULL
661#endif
662
663static struct uart_driver ks8695_reg = {
664 .owner = THIS_MODULE,
665 .driver_name = "serial_ks8695",
666 .dev_name = SERIAL_KS8695_DEVNAME,
667 .major = SERIAL_KS8695_MAJOR,
668 .minor = SERIAL_KS8695_MINOR,
669 .nr = SERIAL_KS8695_NR,
670 .cons = KS8695_CONSOLE,
671};
672
673static int __init ks8695uart_init(void)
674{
675 int i, ret;
676
677 printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
678
679 ret = uart_register_driver(&ks8695_reg);
680 if (ret)
681 return ret;
682
683 for (i = 0; i < SERIAL_KS8695_NR; i++)
684 uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
685
686 return 0;
687}
688
689static void __exit ks8695uart_exit(void)
690{
691 int i;
692
693 for (i = 0; i < SERIAL_KS8695_NR; i++)
694 uart_remove_one_port(&ks8695_reg, &ks8695uart_ports[0]);
695 uart_unregister_driver(&ks8695_reg);
696}
697
698module_init(ks8695uart_init);
699module_exit(ks8695uart_exit);
700
701MODULE_DESCRIPTION("KS8695 serial port driver");
702MODULE_AUTHOR("Micrel Inc.");
703MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Driver for KS8695 serial ports
4 *
5 * Based on drivers/serial/serial_amba.c, by Kam Lee.
6 *
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>
18
19#include <asm/io.h>
20#include <asm/irq.h>
21#include <asm/mach/irq.h>
22
23#include <mach/regs-uart.h>
24#include <mach/regs-irq.h>
25
26#if defined(CONFIG_SERIAL_KS8695_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
27#define SUPPORT_SYSRQ
28#endif
29
30#include <linux/serial_core.h>
31
32
33#define SERIAL_KS8695_MAJOR 204
34#define SERIAL_KS8695_MINOR 16
35#define SERIAL_KS8695_DEVNAME "ttyAM"
36
37#define SERIAL_KS8695_NR 1
38
39/*
40 * Access macros for the KS8695 UART
41 */
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)
54
55#define KS8695_CLR_TX_INT() __raw_writel(1 << KS8695_IRQ_UART_TX, KS8695_IRQ_VA + KS8695_INTST)
56
57#define UART_DUMMY_LSR_RX 0x100
58#define UART_PORT_SIZE (KS8695_USR - KS8695_URRB + 4)
59
60static inline int tx_enabled(struct uart_port *port)
61{
62 return port->unused[0] & 1;
63}
64
65static inline int rx_enabled(struct uart_port *port)
66{
67 return port->unused[0] & 2;
68}
69
70static inline int ms_enabled(struct uart_port *port)
71{
72 return port->unused[0] & 4;
73}
74
75static inline void ms_enable(struct uart_port *port, int enabled)
76{
77 if(enabled)
78 port->unused[0] |= 4;
79 else
80 port->unused[0] &= ~4;
81}
82
83static inline void rx_enable(struct uart_port *port, int enabled)
84{
85 if(enabled)
86 port->unused[0] |= 2;
87 else
88 port->unused[0] &= ~2;
89}
90
91static inline void tx_enable(struct uart_port *port, int enabled)
92{
93 if(enabled)
94 port->unused[0] |= 1;
95 else
96 port->unused[0] &= ~1;
97}
98
99
100#ifdef SUPPORT_SYSRQ
101static struct console ks8695_console;
102#endif
103
104static void ks8695uart_stop_tx(struct uart_port *port)
105{
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.
110 */
111 disable_irq_nosync(KS8695_IRQ_UART_TX);
112 tx_enable(port, 0);
113 }
114}
115
116static void ks8695uart_start_tx(struct uart_port *port)
117{
118 if (!tx_enabled(port)) {
119 enable_irq(KS8695_IRQ_UART_TX);
120 tx_enable(port, 1);
121 }
122}
123
124static void ks8695uart_stop_rx(struct uart_port *port)
125{
126 if (rx_enabled(port)) {
127 disable_irq(KS8695_IRQ_UART_RX);
128 rx_enable(port, 0);
129 }
130}
131
132static void ks8695uart_enable_ms(struct uart_port *port)
133{
134 if (!ms_enabled(port)) {
135 enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
136 ms_enable(port,1);
137 }
138}
139
140static void ks8695uart_disable_ms(struct uart_port *port)
141{
142 if (ms_enabled(port)) {
143 disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
144 ms_enable(port,0);
145 }
146}
147
148static irqreturn_t ks8695uart_rx_chars(int irq, void *dev_id)
149{
150 struct uart_port *port = dev_id;
151 unsigned int status, ch, lsr, flg, max_count = 256;
152
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;
157
158 port->icount.rx++;
159
160 /*
161 * Note that the error handling code is
162 * out of the main execution path
163 */
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;
171 }
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++;
178
179 lsr &= port->read_status_mask;
180
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;
187 }
188
189 if (uart_handle_sysrq_char(port, ch))
190 goto ignore_char;
191
192 uart_insert_char(port, lsr, URLS_URROE, ch, flg);
193
194ignore_char:
195 status = UART_GET_LSR(port);
196 }
197 tty_flip_buffer_push(&port->state->port);
198
199 return IRQ_HANDLED;
200}
201
202
203static irqreturn_t ks8695uart_tx_chars(int irq, void *dev_id)
204{
205 struct uart_port *port = dev_id;
206 struct circ_buf *xmit = &port->state->xmit;
207 unsigned int count;
208
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;
215 }
216
217 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
218 ks8695uart_stop_tx(port);
219 return IRQ_HANDLED;
220 }
221
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]);
226
227 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
228 port->icount.tx++;
229 }
230
231 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
232 uart_write_wakeup(port);
233
234 if (uart_circ_empty(xmit))
235 ks8695uart_stop_tx(port);
236
237 return IRQ_HANDLED;
238}
239
240static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
241{
242 struct uart_port *port = dev_id;
243 unsigned int status;
244
245 /*
246 * clear modem interrupt by reading MSR
247 */
248 status = UART_GET_MSR(port);
249
250 if (status & URMS_URDDCD)
251 uart_handle_dcd_change(port, status & URMS_URDDCD);
252
253 if (status & URMS_URDDST)
254 port->icount.dsr++;
255
256 if (status & URMS_URDCTS)
257 uart_handle_cts_change(port, status & URMS_URDCTS);
258
259 if (status & URMS_URTERI)
260 port->icount.rng++;
261
262 wake_up_interruptible(&port->state->port.delta_msr_wait);
263
264 return IRQ_HANDLED;
265}
266
267static unsigned int ks8695uart_tx_empty(struct uart_port *port)
268{
269 return (UART_GET_LSR(port) & URLS_URTE) ? TIOCSER_TEMT : 0;
270}
271
272static unsigned int ks8695uart_get_mctrl(struct uart_port *port)
273{
274 unsigned int result = 0;
275 unsigned int status;
276
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;
286
287 return result;
288}
289
290static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
291{
292 unsigned int mcr;
293
294 mcr = UART_GET_MCR(port);
295 if (mctrl & TIOCM_RTS)
296 mcr |= URMC_URRTS;
297 else
298 mcr &= ~URMC_URRTS;
299
300 if (mctrl & TIOCM_DTR)
301 mcr |= URMC_URDTR;
302 else
303 mcr &= ~URMC_URDTR;
304
305 UART_PUT_MCR(port, mcr);
306}
307
308static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
309{
310 unsigned int lcr;
311
312 lcr = UART_GET_LCR(port);
313
314 if (break_state == -1)
315 lcr |= URLC_URSBC;
316 else
317 lcr &= ~URLC_URSBC;
318
319 UART_PUT_LCR(port, lcr);
320}
321
322static int ks8695uart_startup(struct uart_port *port)
323{
324 int retval;
325
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);
330
331 /*
332 * Allocate the IRQ
333 */
334 retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, 0, "UART TX", port);
335 if (retval)
336 goto err_tx;
337
338 retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, 0, "UART RX", port);
339 if (retval)
340 goto err_rx;
341
342 retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, 0, "UART LineStatus", port);
343 if (retval)
344 goto err_ls;
345
346 retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, 0, "UART ModemStatus", port);
347 if (retval)
348 goto err_ms;
349
350 return 0;
351
352err_ms:
353 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
354err_ls:
355 free_irq(KS8695_IRQ_UART_RX, port);
356err_rx:
357 free_irq(KS8695_IRQ_UART_TX, port);
358err_tx:
359 return retval;
360}
361
362static void ks8695uart_shutdown(struct uart_port *port)
363{
364 /*
365 * Free the interrupt
366 */
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);
371
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);
375}
376
377static void ks8695uart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
378{
379 unsigned int lcr, fcr = 0;
380 unsigned long flags;
381 unsigned int baud, quot;
382
383 /*
384 * Ask the core to calculate the divisor for us.
385 */
386 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
387 quot = uart_get_divisor(port, baud);
388
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;
402 }
403
404 /* stop bits */
405 if (termios->c_cflag & CSTOPB)
406 lcr |= URLC_URSB;
407
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;
415 }
416 else if (termios->c_cflag & PARODD)
417 lcr |= URPE_ODD;
418 else
419 lcr |= URPE_EVEN;
420 }
421
422 if (port->fifosize > 1)
423 fcr = URFC_URFRT_8 | URFC_URTFR | URFC_URRFR | URFC_URFE;
424
425 spin_lock_irqsave(&port->lock, flags);
426
427 /*
428 * Update the per-port timeout.
429 */
430 uart_update_timeout(port, termios->c_cflag, baud);
431
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;
437
438 /*
439 * Characters to ignore
440 */
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;
446 /*
447 * If we're ignoring parity and break indicators,
448 * ignore overruns too (for real raw support).
449 */
450 if (termios->c_iflag & IGNPAR)
451 port->ignore_status_mask |= URLS_URROE;
452 }
453
454 /*
455 * Ignore all characters if CREAD is not set.
456 */
457 if ((termios->c_cflag & CREAD) == 0)
458 port->ignore_status_mask |= UART_DUMMY_LSR_RX;
459
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);
465
466 /* Set baud rate */
467 UART_PUT_BRDR(port, quot);
468
469 UART_PUT_LCR(port, lcr);
470 UART_PUT_FCR(port, fcr);
471
472 spin_unlock_irqrestore(&port->lock, flags);
473}
474
475static const char *ks8695uart_type(struct uart_port *port)
476{
477 return port->type == PORT_KS8695 ? "KS8695" : NULL;
478}
479
480/*
481 * Release the memory region(s) being used by 'port'
482 */
483static void ks8695uart_release_port(struct uart_port *port)
484{
485 release_mem_region(port->mapbase, UART_PORT_SIZE);
486}
487
488/*
489 * Request the memory region(s) being used by 'port'
490 */
491static int ks8695uart_request_port(struct uart_port *port)
492{
493 return request_mem_region(port->mapbase, UART_PORT_SIZE,
494 "serial_ks8695") != NULL ? 0 : -EBUSY;
495}
496
497/*
498 * Configure/autoconfigure the port.
499 */
500static void ks8695uart_config_port(struct uart_port *port, int flags)
501{
502 if (flags & UART_CONFIG_TYPE) {
503 port->type = PORT_KS8695;
504 ks8695uart_request_port(port);
505 }
506}
507
508/*
509 * verify the new serial_struct (for TIOCSSERIAL).
510 */
511static int ks8695uart_verify_port(struct uart_port *port, struct serial_struct *ser)
512{
513 int ret = 0;
514
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;
522}
523
524static 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,
541};
542
543static struct uart_port ks8695uart_ports[SERIAL_KS8695_NR] = {
544 {
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,
554 }
555};
556
557#ifdef CONFIG_SERIAL_KS8695_CONSOLE
558static void ks8695_console_putchar(struct uart_port *port, int ch)
559{
560 while (!(UART_GET_LSR(port) & URLS_URTHRE))
561 barrier();
562
563 UART_PUT_CHAR(port, ch);
564}
565
566static void ks8695_console_write(struct console *co, const char *s, u_int count)
567{
568 struct uart_port *port = ks8695uart_ports + co->index;
569
570 uart_console_write(port, s, count, ks8695_console_putchar);
571}
572
573static void __init ks8695_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
574{
575 unsigned int lcr;
576
577 lcr = UART_GET_LCR(port);
578
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';
588 }
589
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;
602 }
603
604 *baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
605 *baud /= 16;
606 *baud &= 0xFFFFFFF0;
607}
608
609static int __init ks8695_console_setup(struct console *co, char *options)
610{
611 struct uart_port *port;
612 int baud = 115200;
613 int bits = 8;
614 int parity = 'n';
615 int flow = 'n';
616
617 /*
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.
621 */
622 port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
623
624 if (options)
625 uart_parse_options(options, &baud, &parity, &bits, &flow);
626 else
627 ks8695_console_get_options(port, &baud, &parity, &bits);
628
629 return uart_set_options(port, co, baud, parity, bits, flow);
630}
631
632static struct uart_driver ks8695_reg;
633
634static 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,
642};
643
644static int __init ks8695_console_init(void)
645{
646 add_preferred_console(SERIAL_KS8695_DEVNAME, 0, NULL);
647 register_console(&ks8695_console);
648 return 0;
649}
650
651console_initcall(ks8695_console_init);
652
653#define KS8695_CONSOLE &ks8695_console
654#else
655#define KS8695_CONSOLE NULL
656#endif
657
658static 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,
666};
667
668static int __init ks8695uart_init(void)
669{
670 int i, ret;
671
672 printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
673
674 ret = uart_register_driver(&ks8695_reg);
675 if (ret)
676 return ret;
677
678 for (i = 0; i < SERIAL_KS8695_NR; i++)
679 uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
680
681 return 0;
682}
683
684static void __exit ks8695uart_exit(void)
685{
686 int i;
687
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);
691}
692
693module_init(ks8695uart_init);
694module_exit(ks8695uart_exit);
695
696MODULE_DESCRIPTION("KS8695 serial port driver");
697MODULE_AUTHOR("Micrel Inc.");
698MODULE_LICENSE("GPL");