2 * linux/drivers/char/serial_ks8695.c
4 * Driver for KS8695 serial port
6 * Based on drivers/serial/serial_ks8695uart.c, by Kam Lee.
8 * Copyright 2002 Micrel Inc.
9 * (C) Copyrght 2006 Greg Ungerer <gerg@snapgear.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/module.h>
27 #include <linux/errno.h>
28 #include <linux/signal.h>
29 #include <linux/sched.h>
30 #include <linux/interrupt.h>
31 #include <linux/tty.h>
32 #include <linux/tty_flip.h>
33 #include <linux/major.h>
34 #include <linux/string.h>
35 #include <linux/fcntl.h>
36 #include <linux/ptrace.h>
37 #include <linux/ioport.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/circ_buf.h>
42 #include <linux/serial.h>
43 #include <linux/console.h>
44 #include <linux/sysrq.h>
45 #include <linux/serial_core.h>
46 #include <asm/system.h>
49 #include <asm/uaccess.h>
50 #include <asm/bitops.h>
52 #define KS8695_UART_NR 1
54 #ifdef CONFIG_SERIAL_KS8695_COM
55 #define KS8695_SERIAL_MAJOR 4
56 #define KS8695_SERIAL_MINOR 64
57 #define KS8695_SERIAL_DEV_NAME "ttyS"
59 #define KS8695_SERIAL_MAJOR 204
60 #define KS8695_SERIAL_MINOR 16
61 #define KS8695_SERIAL_DEV_NAME "ttyAM"
65 * Access macros for the KS8695 UART
67 #define UART_GET_INT_STATUS(p) (*(volatile unsigned int *)((p)->membase + KS8695_INT_STATUS))
68 #define UART_CLR_INT_STATUS(p, c) (*(unsigned int *)((p)->membase + KS8695_INT_STATUS) = (c))
69 #define UART_GET_CHAR(p) ((*(volatile unsigned int *)((p)->membase + KS8695_UART_RX_BUFFER)) & 0xFF)
70 #define UART_PUT_CHAR(p, c) (*(unsigned int *)((p)->membase + KS8695_UART_TX_HOLDING) = (c))
71 #define UART_GET_IER(p) (*(volatile unsigned int *)((p)->membase + KS8695_INT_ENABLE))
72 #define UART_PUT_IER(p, c) (*(unsigned int *)((p)->membase + KS8695_INT_ENABLE) = (c))
73 #define UART_GET_FCR(p) (*(volatile unsigned int *)((p)->membase + KS8695_UART_FIFO_CTRL))
74 #define UART_PUT_FCR(p, c) (*(unsigned int *)((p)->membase + KS8695_UART_FIFO_CTRL) = (c))
75 #define UART_GET_MSR(p) (*(volatile unsigned int *)((p)->membase + KS8695_UART_MODEM_STATUS))
76 #define UART_GET_LSR(p) (*(volatile unsigned int *)((p)->membase + KS8695_UART_LINE_STATUS))
77 #define UART_GET_LCR(p) (*(volatile unsigned int *)((p)->membase + KS8695_UART_LINE_CTRL))
78 #define UART_PUT_LCR(p, c) (*(unsigned int *)((p)->membase + KS8695_UART_LINE_CTRL) = (c))
79 #define UART_GET_MCR(p) (*(volatile unsigned int *)((p)->membase + KS8695_UART_MODEM_CTRL))
80 #define UART_PUT_MCR(p, c) (*(unsigned int *)((p)->membase + KS8695_UART_MODEM_CTRL) = (c))
81 #define UART_GET_BRDR(p) (*(volatile unsigned int *)((p)->membase + KS8695_UART_DIVISOR))
82 #define UART_PUT_BRDR(p, c) (*(unsigned int *)((p)->membase + KS8695_UART_DIVISOR) = (c))
83 #define UART_RX_DATA(s) (((s) & KS8695_UART_LINES_RXFE) != 0)
84 #define UART_TX_READY(s) (((s) & KS8695_UART_LINES_TXFE) != 0)
86 #define UART_DUMMY_LSR_RX 0x100
88 static void ks8695uart_stop_tx(struct uart_port
*port
)
92 ier
= UART_GET_IER(port
);
93 if (ier
& KS8695_INT_ENABLE_TX
)
97 static void ks8695uart_start_tx(struct uart_port
*port
)
101 ier
= UART_GET_IER(port
);
102 if ((ier
& KS8695_INT_ENABLE_TX
) == 0)
106 static void ks8695uart_stop_rx(struct uart_port
*port
)
110 ier
= UART_GET_IER(port
);
111 ier
&= ~KS8695_INT_ENABLE_RX
;
112 UART_PUT_IER(port
, ier
);
115 static void ks8695uart_enable_ms(struct uart_port
*port
)
117 UART_PUT_IER(port
, UART_GET_IER(port
) | KS8695_INT_ENABLE_MODEM
);
120 static irqreturn_t
ks8695uart_tx_chars(int irq
, void *data
)
122 struct uart_port
*port
= data
;
123 struct circ_buf
*xmit
= &port
->info
->xmit
;
127 UART_CLR_INT_STATUS(port
, KS8695_INTMASK_UART_TX
);
128 UART_PUT_CHAR(port
, (unsigned int) port
->x_char
);
134 for (i
= 0; (i
< 16); i
++) {
135 if (xmit
->head
== xmit
->tail
)
137 UART_CLR_INT_STATUS(port
, KS8695_INTMASK_UART_TX
);
138 UART_PUT_CHAR(port
, (unsigned int) (xmit
->buf
[xmit
->tail
]));
139 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
143 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
144 uart_write_wakeup(port
);
146 if (xmit
->head
== xmit
->tail
)
152 static irqreturn_t
ks8695uart_rx_chars(int irq
, void *data
)
154 struct uart_port
*port
= data
;
155 struct tty_struct
*tty
= port
->info
->tty
;
156 unsigned int status
, ch
, lsr
, max_count
= 256;
159 status
= UART_GET_LSR(port
);
160 while (UART_RX_DATA(status
) && max_count
--) {
162 ch
= UART_GET_CHAR(port
);
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 (lsr
& KS8695_UART_LINES_ANY
) {
172 if (lsr
& KS8695_UART_LINES_BE
) {
173 lsr
&= ~(KS8695_UART_LINES_FE
| KS8695_UART_LINES_FE
);
175 if (uart_handle_break(port
))
177 } else if (lsr
& KS8695_UART_LINES_PE
)
178 port
->icount
.parity
++;
179 else if (lsr
& KS8695_UART_LINES_FE
)
180 port
->icount
.frame
++;
181 if (lsr
& KS8695_UART_LINES_OE
)
182 port
->icount
.overrun
++;
184 lsr
&= port
->read_status_mask
;
186 if (lsr
& KS8695_UART_LINES_BE
)
188 else if (lsr
& KS8695_UART_LINES_PE
)
190 else if (lsr
& KS8695_UART_LINES_FE
)
194 if (uart_handle_sysrq_char(port
, ch
))
197 uart_insert_char(port
, lsr
, KS8695_UART_LINES_OE
, ch
, flag
);
200 status
= UART_GET_LSR(port
);
203 tty_flip_buffer_push(tty
);
208 static irqreturn_t
ks8695uart_modem(int irq
, void *data
)
210 struct uart_port
*port
= data
;
211 unsigned int status
, delta
;
213 /* clear modem interrupt by reading MSR */
214 status
= UART_GET_MSR(port
);
215 delta
= status
& 0x0B;
220 if (delta
& KS8695_UART_MODEM_DDCD
)
221 uart_handle_dcd_change(port
, status
& KS8695_UART_MODEM_DDCD
);
222 if (delta
& KS8695_UART_MODEM_DDSR
)
224 if (delta
& KS8695_UART_MODEM_DCTS
)
225 uart_handle_cts_change(port
, status
& KS8695_UART_MODEM_DCTS
);
226 wake_up_interruptible(&port
->info
->delta_msr_wait
);
231 static unsigned int ks8695uart_tx_empty(struct uart_port
*port
)
235 status
= UART_GET_LSR(port
);
236 return UART_TX_READY(status
) ? TIOCSER_TEMT
: 0;
239 static unsigned int ks8695uart_get_mctrl(struct uart_port
*port
)
241 unsigned int result
= 0;
244 status
= UART_GET_MSR(port
);
245 if (status
& KS8695_UART_MODEM_DCD
)
247 if (status
& KS8695_UART_MODEM_DSR
)
249 if (status
& KS8695_UART_MODEM_CTS
)
255 static void ks8695uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
259 mcr
= UART_GET_MCR(port
);
260 if (mctrl
& TIOCM_RTS
)
261 mcr
|= KS8695_UART_MODEMC_RTS
;
263 mcr
&= ~KS8695_UART_MODEMC_RTS
;
265 if (mctrl
& TIOCM_DTR
)
266 mcr
|= KS8695_UART_MODEMC_DTR
;
268 mcr
&= ~KS8695_UART_MODEMC_DTR
;
270 UART_PUT_MCR(port
, mcr
);
273 static void ks8695uart_break_ctl(struct uart_port
*port
, int break_state
)
277 lcr
= UART_GET_LCR(port
);
278 if (break_state
== -1)
279 lcr
|= KS8695_UART_LINEC_BRK
;
281 lcr
&= ~KS8695_UART_LINEC_BRK
;
282 UART_PUT_LCR(port
, lcr
);
285 static int ks8695uart_startup(struct uart_port
*port
)
289 retval
= request_irq(KS8695_INT_UART_TX
, ks8695uart_tx_chars
, SA_SHIRQ
| SA_INTERRUPT
, "KS8695 uart(TX)", port
);
293 retval
= request_irq(KS8695_INT_UART_RX
, ks8695uart_rx_chars
, SA_SHIRQ
| SA_INTERRUPT
, "KS8695 uart(RX)", port
);
297 retval
= request_irq(KS8695_INT_UART_LINE_ERR
, ks8695uart_rx_chars
, SA_SHIRQ
| SA_INTERRUPT
, "KS8695 uart(error)", port
);
301 retval
= request_irq(KS8695_INT_UART_MODEMS
, ks8695uart_modem
, SA_SHIRQ
| SA_INTERRUPT
, "KS8695 uart(modem)", port
);
308 static void ks8695uart_shutdown(struct uart_port
*port
)
310 /* disable break condition and fifos */
311 UART_PUT_LCR(port
, UART_GET_LCR(port
) & ~KS8695_UART_LINEC_BRK
);
312 UART_PUT_FCR(port
, UART_GET_FCR(port
) & ~KS8695_UART_FIFO_FEN
);
314 free_irq(KS8695_INT_UART_RX
, port
);
315 free_irq(KS8695_INT_UART_TX
, port
);
316 free_irq(KS8695_INT_UART_LINE_ERR
, port
);
317 free_irq(KS8695_INT_UART_MODEMS
, port
);
320 static void ks8695uart_set_termios(struct uart_port
*port
, struct termios
*termios
, struct termios
*old
)
322 unsigned int baud
, lcr
, fcr
= 0;
325 /* byte size and parity */
326 switch (termios
->c_cflag
& CSIZE
) {
327 case CS5
: lcr
= KS8695_UART_LINEC_WLEN5
; break;
328 case CS6
: lcr
= KS8695_UART_LINEC_WLEN6
; break;
329 case CS7
: lcr
= KS8695_UART_LINEC_WLEN7
; break;
330 default: lcr
= KS8695_UART_LINEC_WLEN8
; break; // CS8
333 if (termios
->c_cflag
& CSTOPB
)
334 lcr
|= KS8695_UART_LINEC_STP2
;
335 if (termios
->c_cflag
& PARENB
) {
336 lcr
|= KS8695_UART_LINEC_PEN
;
337 if (!(termios
->c_cflag
& PARODD
))
338 lcr
|= KS8695_UART_LINEC_EPS
;
341 if (port
->fifosize
> 1)
342 fcr
= KS8695_UART_FIFO_TRIG04
| KS8695_UART_FIFO_TXRST
| KS8695_UART_FIFO_RXRST
| KS8695_UART_FIFO_FEN
;
344 port
->read_status_mask
= KS8695_UART_LINES_OE
;
345 if (termios
->c_iflag
& INPCK
)
346 port
->read_status_mask
|= (KS8695_UART_LINES_FE
| KS8695_UART_LINES_PE
);
347 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
348 port
->read_status_mask
|= KS8695_UART_LINES_BE
;
350 /* Characters to ignore */
351 port
->ignore_status_mask
= 0;
352 if (termios
->c_iflag
& IGNPAR
)
353 port
->ignore_status_mask
|= (KS8695_UART_LINES_FE
| KS8695_UART_LINES_PE
);
354 if (termios
->c_iflag
& IGNBRK
) {
355 port
->ignore_status_mask
|= KS8695_UART_LINES_BE
;
357 * If we're ignoring parity and break indicators,
358 * ignore overruns too (for real raw support).
360 if (termios
->c_iflag
& IGNPAR
)
361 port
->ignore_status_mask
|= KS8695_UART_LINES_OE
;
364 /* Ignore all characters if CREAD is not set. */
365 if ((termios
->c_cflag
& CREAD
) == 0)
366 port
->ignore_status_mask
|= UART_DUMMY_LSR_RX
;
368 baud
= uart_get_baud_rate(port
, termios
, old
, 50, 230400);
370 /* first, disable everything */
371 local_irq_save(flags
);
373 if ((port
->flags
& ASYNC_HARDPPS_CD
) ||
374 (termios
->c_cflag
& CRTSCTS
) || !(termios
->c_cflag
& CLOCAL
))
375 ks8695uart_enable_ms(port
);
377 UART_PUT_BRDR(port
, port
->uartclk
/ baud
);
378 UART_PUT_LCR(port
, lcr
);
379 UART_PUT_FCR(port
, fcr
);
381 local_irq_restore(flags
);
384 static const char *ks8695uart_type(struct uart_port
*port
)
386 return port
->type
== PORT_KS8695
? "KS8695" : NULL
;
390 * Release the memory region(s) being used by 'port'
392 static void ks8695uart_release_port(struct uart_port
*port
)
394 release_mem_region(port
->mapbase
, 0x24);
398 * Request the memory region(s) being used by 'port'
400 static int ks8695uart_request_port(struct uart_port
*port
)
402 return request_mem_region(port
->mapbase
, 0x24, "KS8695 UART") != NULL
? 0 : -EBUSY
;
406 * Configure/autoconfigure the port.
408 static void ks8695uart_config_port(struct uart_port
*port
, int flags
)
410 if (flags
& UART_CONFIG_TYPE
) {
411 port
->type
= PORT_KS8695
;
412 ks8695uart_request_port(port
);
417 * verify the new serial_struct (for TIOCSSERIAL).
419 static int ks8695uart_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
421 if ((ser
->type
!= PORT_UNKNOWN
) && (ser
->type
!= PORT_KS8695
))
423 if ((ser
->irq
< 0) || (ser
->irq
>= NR_IRQS
))
425 if (ser
->baud_base
< 9600)
430 static struct uart_ops ks8695uart_ops
= {
431 .tx_empty
= ks8695uart_tx_empty
,
432 .set_mctrl
= ks8695uart_set_mctrl
,
433 .get_mctrl
= ks8695uart_get_mctrl
,
434 .stop_tx
= ks8695uart_stop_tx
,
435 .start_tx
= ks8695uart_start_tx
,
436 .stop_rx
= ks8695uart_stop_rx
,
437 .enable_ms
= ks8695uart_enable_ms
,
438 .break_ctl
= ks8695uart_break_ctl
,
439 .startup
= ks8695uart_startup
,
440 .shutdown
= ks8695uart_shutdown
,
441 .set_termios
= ks8695uart_set_termios
,
442 .type
= ks8695uart_type
,
443 .release_port
= ks8695uart_release_port
,
444 .request_port
= ks8695uart_request_port
,
445 .config_port
= ks8695uart_config_port
,
446 .verify_port
= ks8695uart_verify_port
,
449 static struct uart_port ks8695uart_ports
[KS8695_UART_NR
] = {
452 .membase
= (void *) KS8695_IO_VIRT
,
453 .mapbase
= KS8695_IO_BASE
+ KS8695_UART_RX_BUFFER
,
454 .iotype
= SERIAL_IO_MEM
,
455 .irq
= KS8695_INT_UART_RX
,
458 .ops
= &ks8695uart_ops
,
459 .flags
= ASYNC_BOOT_AUTOCONF
,
463 #ifdef CONFIG_SERIAL_KS8695_CONSOLE
466 * Force a single char out the serial. It must go out, poll the ready
467 * register until we can send it, and make sure it is sent.
469 static void ks8695uart_console_putc(struct console
*co
, const char c
)
471 struct uart_port
*port
= ks8695uart_ports
+ co
->index
;
473 while (!UART_TX_READY(UART_GET_LSR(port
)))
476 UART_PUT_CHAR(port
, (unsigned int) c
);
478 while (!UART_TX_READY(UART_GET_LSR(port
)))
482 static void ks8695uart_console_write(struct console
*co
, const char *s
, unsigned int count
)
486 for (i
= 0; i
< count
; i
++, s
++) {
487 ks8695uart_console_putc(co
, *s
);
489 ks8695uart_console_putc(co
, '\r');
493 static int __init
ks8695uart_console_setup(struct console
*co
, char *options
)
495 struct uart_port
*port
;
501 port
= uart_get_console(ks8695uart_ports
, KS8695_UART_NR
, co
);
503 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
505 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
508 static struct uart_driver ks8695uart_reg
;
509 static struct console ks8695uart_console
= {
510 .name
= KS8695_SERIAL_DEV_NAME
,
511 .write
= ks8695uart_console_write
,
512 .device
= uart_console_device
,
513 .setup
= ks8695uart_console_setup
,
514 .flags
= CON_PRINTBUFFER
,
516 .data
= &ks8695uart_reg
,
519 static int __init
ks8695uart_console_init(void)
521 register_console(&ks8695uart_console
);
525 console_initcall(ks8695uart_console_init
);
527 #define KS8695UART_CONSOLE &ks8695uart_console
529 #define KS8695UART_CONSOLE NULL
532 static struct uart_driver ks8695uart_reg
= {
533 .owner
= THIS_MODULE
,
534 .driver_name
= "serial_ks8695",
535 .dev_name
= KS8695_SERIAL_DEV_NAME
,
536 .major
= KS8695_SERIAL_MAJOR
,
537 .minor
= KS8695_SERIAL_MINOR
,
538 .nr
= KS8695_UART_NR
,
539 .cons
= KS8695UART_CONSOLE
,
542 static int __init
ks8695uart_init(void)
546 rc
= uart_register_driver(&ks8695uart_reg
);
548 for (i
= 0; (i
< KS8695_UART_NR
); i
++)
549 uart_add_one_port(&ks8695uart_reg
, &ks8695uart_ports
[i
]);
555 static void __exit
ks8695uart_exit(void)
559 for (i
= 0; (i
< KS8695_UART_NR
); i
++)
560 uart_remove_one_port(&ks8695uart_reg
, &ks8695uart_ports
[i
]);
561 uart_unregister_driver(&ks8695uart_reg
);
564 module_init(ks8695uart_init
);
565 module_exit(ks8695uart_exit
);
567 MODULE_AUTHOR("Micrel Semiconductor");
568 MODULE_DESCRIPTION("KS8695 serial port driver");
569 MODULE_LICENSE("GPL");