MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / serial / t32.c
blobe50c4033a5a18d3f458de37cd5171962dc6b0e72
1 /*
2 * linux/drivers/serial/t32.c
4 * serial port emulation driver for the TRACE32 Terminal.
6 * JTAG1 protocol version for ICD:
7 * Copyright (C) 2003, 2004 Hyok S. Choi (hyok.choi@samsung.com)
8 * SAMSUNG ELECTRONICS Co.,Ltd.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * Changelog:
15 * Oct-2003 Hyok S. Choi Created
16 * Feb-2004 Hyok S. Choi Updated for serial_core.c and 2.6 kernel
17 * Apr-2004 Hyok S. Choi xmit_string_CR added
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/tty.h>
24 #include <linux/ioport.h>
25 #include <linux/init.h>
26 #include <linux/serial.h>
27 #include <linux/console.h>
28 #include <linux/sysrq.h>
29 #include <linux/major.h>
31 #include <asm/io.h>
32 #include <asm/irq.h>
34 #include <linux/serial_core.h>
36 #if 0
37 /* real irq interrupt used */
38 #define T32_IRQ_USED
39 #else
40 /* scheduled work used */
41 #undef T32_IRQ_USED
42 #endif
44 #ifndef T32_IRQ_USED
45 static struct work_struct t32_poll_task;
46 static void t32_poll(void *data);
47 #endif
49 #define UART_NR 1 /* we have only one JTAG port */
51 #define SERIAL_T32_NAME "ttyJ"
52 #define SERIAL_T32_MAJOR 4
53 #define SERIAL_T32_MINOR 64
55 static int __inline__ __check_JTAG_RX_FLAG(void)
57 int __ret=0;
58 __asm__ __volatile__(
59 " mrc p14, 0, %0, c0, c0 @ read comms control reg\n"
60 " and %0, %0, #1 @ jtag read buffer status"
61 : "=r" (__ret)
64 /* if __ret == 0 : no input yet
65 == 1 : a character pending */
66 return __ret;
69 static void __inline__ __get_JTAG_RX(volatile char *p)
71 __asm__ __volatile__(
72 " mrc p14, 0, r3, c1, c0 @ read comms data reg to r5\n"
73 " strb r3, [%0] @ str a char"
74 : /* no output */
75 : "r" (p)
76 : "r3", "memory");
80 static int __inline__ __check_JTAG_TX_FLAG(void)
82 int __ret=0;
83 __asm__ __volatile__(
84 " mrc p14, 0, %0, c0, c0 @ read comms control reg\n"
85 " and %0, %0, #2 @ the read buffer status"
86 : "=r" (__ret)
89 /* if __ret == 0 : tx is available
90 == 2 : tx busy */
91 return __ret;
94 void xmit_string(char *p, int len)
96 #ifndef CONFIG_JTAG_T32_OUTPUT_DISABLE
98 r0 = string ; string address
99 r1 = 2 ; state check bit (write)
100 r4 = *string ; character
101 r7 = 0 ; count
103 __asm__ __volatile__(
104 " mov r7, #0\n"
105 "1: mrc p14, 0, r3, c0, c0 @ read comms control reg\n"
106 " and r3, r3, #2 @ the write buffer status\n"
107 " cmp r3, #2 @ is it available?\n"
108 " beq 1b @ is not, wait till then\n"
109 " ldrb r4, [%0] @ load a char\n"
110 " mcr p14, 0, r4, c1, c0 @ write it\n"
111 " add %0, %0, #1 @ str address increase one\n"
112 " add r7, r7, #1 @ count increase\n"
113 " cmp r7, %1 @ compare with str length\n"
114 " bne 1b @ if it is not yet, loop"
115 : /* no output register */
116 : "r" (p), "r" (len)
117 : "r7", "r3", "r4");
118 #endif
121 void xmit_string_CR(char *p, int len)
123 #ifndef CONFIG_JTAG_T32_OUTPUT_DISABLE
125 r0 = string ; string address
126 r1 = 2 ; state check bit (write)
127 r4 = *string ; character
128 r7 = 0 ; count
130 __asm__ __volatile__(
131 " mov r7, #0\n"
132 " ldrb r4, [%0] @ load a char\n"
133 "1: mrc p14, 0, r3, c0, c0 @ read comms control reg\n"
134 " and r3, r3, #2 @ the write buffer status\n"
135 " cmp r3, #2 @ is it available?\n"
136 " beq 1b @ is not, wait till then\n"
137 " mcr p14, 0, r4, c1, c0 @ write it\n"
138 " cmp r4, #0x0a @ is it LF?\n"
139 " bne 2f @ if it is not, continue\n"
140 " mov r4, #0x0d @ set the CR\n"
141 " b 1b @ loop for writing CR\n"
142 "2: ldrb r4, [%0, #1]! @ load a char\n"
143 " add r7, r7, #1 @ count increase\n"
144 " cmp r7, %1 @ compare with str length\n"
145 " bne 1b @ if it is not yet, loop"
146 : /* no output register */
147 : "r" (p), "r" (len)
148 : "r7", "r3", "r4");
149 #endif
153 static void
154 t32_stop_tx(struct uart_port *port, unsigned int tty_stop)
158 static inline void
159 t32_transmit_buffer(struct uart_port *port)
161 struct circ_buf *xmit = &port->info->xmit;
163 int pendings = uart_circ_chars_pending(xmit);
165 if(pendings + xmit->tail > UART_XMIT_SIZE)
167 xmit_string(&(xmit->buf[xmit->tail]), UART_XMIT_SIZE - xmit->tail);
168 xmit_string(&(xmit->buf[0]), xmit->head);
169 } else
170 xmit_string(&(xmit->buf[xmit->tail]), pendings);
172 xmit->tail = (xmit->tail + pendings) & (UART_XMIT_SIZE-1);
173 port->icount.tx += pendings;
175 if (uart_circ_empty(xmit))
176 t32_stop_tx(port, 0);
179 static inline void
180 t32_transmit_x_char(struct uart_port *port)
182 xmit_string(&port->x_char, 1);
183 port->icount.tx++;
184 port->x_char = 0;
187 static void
188 t32_start_tx(struct uart_port *port, unsigned int tty_start)
190 t32_transmit_buffer(port);
193 static void
194 t32_stop_rx(struct uart_port *port)
198 static void
199 t32_enable_ms(struct uart_port *port)
203 static inline void
204 t32_rx_chars(struct uart_port *port)
206 unsigned char ch;
207 struct tty_struct *tty = port->info->tty;
210 * check input.
211 * checking JTAG flag is better to resolve the status test.
212 * incount is NOT used for JTAG1 protocol.
215 if (__check_JTAG_RX_FLAG())
216 /* if __ret == 0 : no input yet
217 == 1 : a character pending */
219 /* for JTAG 1 protocol, incount is always 1. */
220 __get_JTAG_RX(&ch);
221 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
222 *tty->flip.char_buf_ptr++ = ch;
223 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
224 port->icount.rx++;
225 tty->flip.count++;
227 tty_flip_buffer_push(tty);
231 static inline void
232 t32_overrun_chars(struct uart_port *port)
234 port->icount.overrun++;
237 static inline void
238 t32_tx_chars(struct uart_port *port)
240 struct circ_buf *xmit = &port->info->xmit;
242 if (port->x_char) {
243 t32_transmit_x_char(port);
244 return;
247 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
248 t32_stop_tx(port, 0);
249 return;
252 t32_transmit_buffer(port);
254 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
255 uart_write_wakeup(port);
258 #ifdef T32_IRQ_USED /* real IRQ used */
259 static irqreturn_t
260 t32_int(int irq, void *dev_id, struct pt_regs *regs)
262 struct uart_port *port = dev_id;
263 int handled = 0;
265 spin_lock(&port->lock);
267 t32_rx_chars(port);
268 t32_tx_chars(port);
270 handled = 1;
271 spin_unlock(&port->lock);
273 return IRQ_RETVAL(handled);
276 #else /* emulation by scheduled work */
277 static void
278 t32_poll(void *data)
280 struct uart_port *port = data;
282 spin_lock(&port->lock);
284 t32_rx_chars(port);
285 t32_tx_chars(port);
287 schedule_delayed_work(&t32_poll_task, 1);
289 spin_unlock(&port->lock);
292 #endif /* end of T32_IRQ_USED */
294 static unsigned int
295 t32_tx_empty(struct uart_port *port)
297 return TIOCSER_TEMT;
300 static unsigned int
301 t32_get_mctrl(struct uart_port *port)
303 return 0;
306 static void
307 t32_set_mctrl(struct uart_port *port, unsigned int mctrl)
311 static void
312 t32_break_ctl(struct uart_port *port, int break_state)
316 static int t32_startup(struct uart_port *port)
318 #ifdef T32_IRQ_USED /* real IRQ used */
319 int retval;
321 /* Allocate the IRQ */
322 retval = request_irq(port->irq, t32_int, SA_INTERRUPT,
323 "serial_t32", port);
324 if (retval)
325 return retval;
326 #else /* emulation */
327 /* Initialize the work, and shcedule it. */
328 INIT_WORK(&t32_poll_task, t32_poll, port);
329 schedule_delayed_work(&t32_poll_task, 1);
330 #endif
332 return 0;
335 static void t32_shutdown(struct uart_port *port)
339 static void
340 t32_set_termios(struct uart_port *port, struct termios *termios,
341 struct termios *old)
343 #ifdef T32_IRQ_USED
344 unsigned long flags;
345 #endif
346 unsigned int baud, quot;
349 * We don't support parity, stop bits, or anything other
350 * than 8 bits, so clear these termios flags.
352 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CREAD);
353 termios->c_cflag |= CS8;
356 * We don't appear to support any error conditions either.
358 termios->c_iflag &= ~(INPCK | IGNPAR | IGNBRK | BRKINT);
361 * Ask the core to calculate the divisor for us.
363 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
364 quot = uart_get_divisor(port, baud);
366 #ifdef T32_IRQ_USED
367 spin_lock_irqsave(&port->lock, flags);
368 #endif
370 uart_update_timeout(port, termios->c_cflag, baud);
372 #ifdef T32_IRQ_USED
373 spin_unlock_irqrestore(&port->lock, flags);
374 #endif
377 static const char *t32_type(struct uart_port *port)
379 return port->type == PORT_T32_JTAG1 ? "T32" : NULL;
382 static void t32_release_port(struct uart_port *port)
386 static int t32_request_port(struct uart_port *port)
388 return 0;
392 * Configure/autoconfigure the port.
394 static void t32_config_port(struct uart_port *port, int flags)
396 if (flags & UART_CONFIG_TYPE) {
397 port->type = PORT_T32_JTAG1;
398 t32_request_port(port);
403 * verify the new serial_struct (for TIOCSSERIAL).
405 static int t32_verify_port(struct uart_port *port, struct serial_struct *ser)
407 int ret = 0;
408 if (ser->type != PORT_UNKNOWN && ser->type != PORT_T32_JTAG1)
409 ret = -EINVAL;
410 if (ser->irq < 0 || ser->irq >= NR_IRQS)
411 ret = -EINVAL;
412 if (ser->baud_base < 9600)
413 ret = -EINVAL;
414 return ret;
417 static struct uart_ops t32_pops = {
418 .tx_empty = t32_tx_empty,
419 .set_mctrl = t32_set_mctrl,
420 .get_mctrl = t32_get_mctrl,
421 .stop_tx = t32_stop_tx,
422 .start_tx = t32_start_tx,
423 .stop_rx = t32_stop_rx,
424 .enable_ms = t32_enable_ms,
425 .break_ctl = t32_break_ctl,
426 .startup = t32_startup,
427 .shutdown = t32_shutdown,
428 .set_termios = t32_set_termios,
429 .type = t32_type,
430 .release_port = t32_release_port,
431 .request_port = t32_request_port,
432 .config_port = t32_config_port,
433 .verify_port = t32_verify_port,
436 static struct uart_port t32_ports[UART_NR] = {
438 .membase = (char*)0x12345678, /* we need these garbages */
439 .mapbase = 0x12345678, /* for serial_core.c */
440 .iotype = SERIAL_IO_MEM,
441 #ifdef T32_IRQ_USED
442 .irq = INT_N_EXT0,
443 #else
444 .irq = 0,
445 #endif
446 .uartclk = 14745600,
447 .fifosize = 0,
448 .ops = &t32_pops,
449 .flags = ASYNC_BOOT_AUTOCONF,
450 .line = 0,
455 #ifdef CONFIG_SERIAL_T32_CONSOLE
457 static void
458 t32_console_write(struct console *co, const char *s, unsigned int count)
460 xmit_string_CR((char*)s, count);
464 * Read the current UART setup.
466 static void __init
467 t32_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
469 *baud = 9600;
470 *parity = 'n';
471 *bits = 8;
474 static int __init
475 t32_console_setup(struct console *co, char *options)
477 struct uart_port *port;
478 int baud = 9600;
479 int bits = 8;
480 int parity = 'n';
481 int flow = 'n';
483 if (co->index >= UART_NR)
484 co->index = 0;
485 port = &t32_ports[co->index];
487 if (options)
488 uart_parse_options(options, &baud, &parity, &bits, &flow);
489 else
490 t32_console_get_options(port, &baud, &parity, &bits);
492 return uart_set_options(port, co, baud, parity, bits, flow);
495 extern struct uart_driver t32_reg;
496 static struct console t32_console = {
497 .name = SERIAL_T32_NAME,
498 .write = t32_console_write,
499 .device = uart_console_device,
500 .setup = t32_console_setup,
501 .flags = CON_PRINTBUFFER,
502 .index = -1,
503 .data = &t32_reg,
506 static int __init t32_console_init(void)
508 register_console(&t32_console);
509 return 0;
511 console_initcall(t32_console_init);
513 #define T32_CONSOLE &t32_console
514 #else
515 #define T32_CONSOLE NULL
516 #endif
518 static struct uart_driver t32_reg = {
519 .owner = THIS_MODULE,
520 .driver_name = SERIAL_T32_NAME,
521 .dev_name = SERIAL_T32_NAME,
522 .major = SERIAL_T32_MAJOR,
523 .minor = SERIAL_T32_MINOR,
524 .nr = UART_NR,
525 .cons = T32_CONSOLE,
528 static int __init
529 t32_init(void)
531 int ret;
533 printk(KERN_INFO "T32: JTAG1 Serial emulation driver driver $Revision: 1.1 $\n");
535 ret = uart_register_driver(&t32_reg);
536 if (ret == 0) {
537 int i;
539 for (i = 0; i < UART_NR; i++)
540 uart_add_one_port(&t32_reg, &t32_ports[i]);
542 return ret;
545 __initcall(t32_init);
547 MODULE_DESCRIPTION("T32 JTAG1 serial and console emulation driver");
548 MODULE_AUTHOR("Hyok S. Choi <hyok.choi@samsung.com>");
549 MODULE_SUPPORTED_DEVICE("ttyJ");
550 MODULE_LICENSE("GPL");