Linux 3.8-rc7
[cris-mirror.git] / arch / cris / arch-v10 / kernel / debugport.c
blobf932c85fbde48403f11b9056994411d1d50cbccc
1 /* Serialport functions for debugging
3 * Copyright (c) 2000-2007 Axis Communications AB
5 * Authors: Bjorn Wesen
7 * Exports:
8 * console_print_etrax(char *buf)
9 * int getDebugChar()
10 * putDebugChar(int)
11 * enableDebugIRQ()
12 * init_etrax_debug()
16 #include <linux/console.h>
17 #include <linux/init.h>
18 #include <linux/major.h>
19 #include <linux/delay.h>
20 #include <linux/tty.h>
21 #include <arch/svinto.h>
22 #include <asm/io.h> /* Get SIMCOUT. */
24 extern void reset_watchdog(void);
26 struct dbg_port
28 unsigned int index;
29 const volatile unsigned* read;
30 volatile char* write;
31 volatile unsigned* xoff;
32 volatile char* baud;
33 volatile char* tr_ctrl;
34 volatile char* rec_ctrl;
35 unsigned long irq;
36 unsigned int started;
37 unsigned long baudrate;
38 unsigned char parity;
39 unsigned int bits;
42 struct dbg_port ports[]=
46 R_SERIAL0_READ,
47 R_SERIAL0_TR_DATA,
48 R_SERIAL0_XOFF,
49 R_SERIAL0_BAUD,
50 R_SERIAL0_TR_CTRL,
51 R_SERIAL0_REC_CTRL,
52 IO_STATE(R_IRQ_MASK1_SET, ser0_data, set),
54 115200,
55 'N',
60 R_SERIAL1_READ,
61 R_SERIAL1_TR_DATA,
62 R_SERIAL1_XOFF,
63 R_SERIAL1_BAUD,
64 R_SERIAL1_TR_CTRL,
65 R_SERIAL1_REC_CTRL,
66 IO_STATE(R_IRQ_MASK1_SET, ser1_data, set),
68 115200,
69 'N',
74 R_SERIAL2_READ,
75 R_SERIAL2_TR_DATA,
76 R_SERIAL2_XOFF,
77 R_SERIAL2_BAUD,
78 R_SERIAL2_TR_CTRL,
79 R_SERIAL2_REC_CTRL,
80 IO_STATE(R_IRQ_MASK1_SET, ser2_data, set),
82 115200,
83 'N',
88 R_SERIAL3_READ,
89 R_SERIAL3_TR_DATA,
90 R_SERIAL3_XOFF,
91 R_SERIAL3_BAUD,
92 R_SERIAL3_TR_CTRL,
93 R_SERIAL3_REC_CTRL,
94 IO_STATE(R_IRQ_MASK1_SET, ser3_data, set),
96 115200,
97 'N',
102 #ifdef CONFIG_ETRAX_SERIAL
103 extern struct tty_driver *serial_driver;
104 #endif
106 struct dbg_port* port =
107 #if defined(CONFIG_ETRAX_DEBUG_PORT0)
108 &ports[0];
109 #elif defined(CONFIG_ETRAX_DEBUG_PORT1)
110 &ports[1];
111 #elif defined(CONFIG_ETRAX_DEBUG_PORT2)
112 &ports[2];
113 #elif defined(CONFIG_ETRAX_DEBUG_PORT3)
114 &ports[3];
115 #else
116 NULL;
117 #endif
119 static struct dbg_port* kgdb_port =
120 #if defined(CONFIG_ETRAX_KGDB_PORT0)
121 &ports[0];
122 #elif defined(CONFIG_ETRAX_KGDB_PORT1)
123 &ports[1];
124 #elif defined(CONFIG_ETRAX_KGDB_PORT2)
125 &ports[2];
126 #elif defined(CONFIG_ETRAX_KGDB_PORT3)
127 &ports[3];
128 #else
129 NULL;
130 #endif
132 static void
133 start_port(struct dbg_port* p)
135 unsigned long rec_ctrl = 0;
136 unsigned long tr_ctrl = 0;
138 if (!p)
139 return;
141 if (p->started)
142 return;
143 p->started = 1;
145 if (p->index == 0)
147 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
148 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
150 else if (p->index == 1)
152 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
153 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
155 else if (p->index == 2)
157 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
158 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
159 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
160 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
161 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser2, select);
163 else
165 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
166 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
167 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
168 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
169 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser3, select);
172 *R_GEN_CONFIG = genconfig_shadow;
174 *p->xoff =
175 IO_STATE(R_SERIAL0_XOFF, tx_stop, enable) |
176 IO_STATE(R_SERIAL0_XOFF, auto_xoff, disable) |
177 IO_FIELD(R_SERIAL0_XOFF, xoff_char, 0);
179 switch (p->baudrate)
181 case 0:
182 case 115200:
183 *p->baud =
184 IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
185 IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
186 break;
187 case 1200:
188 *p->baud =
189 IO_STATE(R_SERIAL0_BAUD, tr_baud, c1200Hz) |
190 IO_STATE(R_SERIAL0_BAUD, rec_baud, c1200Hz);
191 break;
192 case 2400:
193 *p->baud =
194 IO_STATE(R_SERIAL0_BAUD, tr_baud, c2400Hz) |
195 IO_STATE(R_SERIAL0_BAUD, rec_baud, c2400Hz);
196 break;
197 case 4800:
198 *p->baud =
199 IO_STATE(R_SERIAL0_BAUD, tr_baud, c4800Hz) |
200 IO_STATE(R_SERIAL0_BAUD, rec_baud, c4800Hz);
201 break;
202 case 9600:
203 *p->baud =
204 IO_STATE(R_SERIAL0_BAUD, tr_baud, c9600Hz) |
205 IO_STATE(R_SERIAL0_BAUD, rec_baud, c9600Hz);
206 break;
207 case 19200:
208 *p->baud =
209 IO_STATE(R_SERIAL0_BAUD, tr_baud, c19k2Hz) |
210 IO_STATE(R_SERIAL0_BAUD, rec_baud, c19k2Hz);
211 break;
212 case 38400:
213 *p->baud =
214 IO_STATE(R_SERIAL0_BAUD, tr_baud, c38k4Hz) |
215 IO_STATE(R_SERIAL0_BAUD, rec_baud, c38k4Hz);
216 break;
217 case 57600:
218 *p->baud =
219 IO_STATE(R_SERIAL0_BAUD, tr_baud, c57k6Hz) |
220 IO_STATE(R_SERIAL0_BAUD, rec_baud, c57k6Hz);
221 break;
222 default:
223 *p->baud =
224 IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
225 IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
226 break;
229 if (p->parity == 'E') {
230 rec_ctrl =
231 IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
232 IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
233 tr_ctrl =
234 IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
235 IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
236 } else if (p->parity == 'O') {
237 rec_ctrl =
238 IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd) |
239 IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
240 tr_ctrl =
241 IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd) |
242 IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
243 } else {
244 rec_ctrl =
245 IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
246 IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, disable);
247 tr_ctrl =
248 IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
249 IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, disable);
251 if (p->bits == 7)
253 rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
254 tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
256 else
258 rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_8bit);
259 tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_8bit);
262 *p->rec_ctrl =
263 IO_STATE(R_SERIAL0_REC_CTRL, dma_err, stop) |
264 IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable) |
265 IO_STATE(R_SERIAL0_REC_CTRL, rts_, active) |
266 IO_STATE(R_SERIAL0_REC_CTRL, sampling, middle) |
267 IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, normal) |
268 rec_ctrl;
270 *p->tr_ctrl =
271 IO_FIELD(R_SERIAL0_TR_CTRL, txd, 0) |
272 IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable) |
273 IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, disabled) |
274 IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, one_bit) |
275 IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, normal) |
276 tr_ctrl;
279 static void
280 console_write_direct(struct console *co, const char *buf, unsigned int len)
282 int i;
283 unsigned long flags;
285 if (!port)
286 return;
288 local_irq_save(flags);
290 /* Send data */
291 for (i = 0; i < len; i++) {
292 /* LF -> CRLF */
293 if (buf[i] == '\n') {
294 while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
296 *port->write = '\r';
298 /* Wait until transmitter is ready and send.*/
299 while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
301 *port->write = buf[i];
305 * Feed the watchdog, otherwise it will reset the chip during boot.
306 * The time to send an ordinary boot message line (10-90 chars)
307 * varies between 1-8ms at 115200. What makes up for the additional
308 * 90ms that allows the watchdog to bite?
310 reset_watchdog();
312 local_irq_restore(flags);
315 static void
316 console_write(struct console *co, const char *buf, unsigned int len)
318 if (!port)
319 return;
321 #ifdef CONFIG_SVINTO_SIM
322 /* no use to simulate the serial debug output */
323 SIMCOUT(buf, len);
324 return;
325 #endif
327 console_write_direct(co, buf, len);
330 /* legacy function */
332 void
333 console_print_etrax(const char *buf)
335 console_write(NULL, buf, strlen(buf));
338 /* Use polling to get a single character FROM the debug port */
341 getDebugChar(void)
343 unsigned long readval;
345 if (!kgdb_port)
346 return 0;
348 do {
349 readval = *kgdb_port->read;
350 } while (!(readval & IO_MASK(R_SERIAL0_READ, data_avail)));
352 return (readval & IO_MASK(R_SERIAL0_READ, data_in));
355 /* Use polling to put a single character to the debug port */
357 void
358 putDebugChar(int val)
360 if (!kgdb_port)
361 return;
363 while (!(*kgdb_port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
365 *kgdb_port->write = val;
368 /* Enable irq for receiving chars on the debug port, used by kgdb */
370 void
371 enableDebugIRQ(void)
373 if (!kgdb_port)
374 return;
376 *R_IRQ_MASK1_SET = kgdb_port->irq;
377 /* use R_VECT_MASK directly, since we really bypass Linux normal
378 * IRQ handling in kgdb anyway, we don't need to use enable_irq
380 *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
382 *kgdb_port->rec_ctrl = IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
385 static int __init
386 console_setup(struct console *co, char *options)
388 char* s;
390 if (options) {
391 port = &ports[co->index];
392 port->baudrate = 115200;
393 port->parity = 'N';
394 port->bits = 8;
395 port->baudrate = simple_strtoul(options, NULL, 10);
396 s = options;
397 while(*s >= '0' && *s <= '9')
398 s++;
399 if (*s) port->parity = *s++;
400 if (*s) port->bits = *s++ - '0';
401 port->started = 0;
402 start_port(0);
404 return 0;
408 /* This is a dummy serial device that throws away anything written to it.
409 * This is used when no debug output is wanted.
411 static struct tty_driver dummy_driver;
413 static int dummy_open(struct tty_struct *tty, struct file * filp)
415 return 0;
418 static void dummy_close(struct tty_struct *tty, struct file * filp)
422 static int dummy_write(struct tty_struct * tty,
423 const unsigned char *buf, int count)
425 return count;
428 static int dummy_write_room(struct tty_struct *tty)
430 return 8192;
433 static const struct tty_operations dummy_ops = {
434 .open = dummy_open,
435 .close = dummy_close,
436 .write = dummy_write,
437 .write_room = dummy_write_room,
440 void __init
441 init_dummy_console(void)
443 memset(&dummy_driver, 0, sizeof(struct tty_driver));
444 dummy_driver.driver_name = "serial";
445 dummy_driver.name = "ttyS";
446 dummy_driver.major = TTY_MAJOR;
447 dummy_driver.minor_start = 68;
448 dummy_driver.num = 1; /* etrax100 has 4 serial ports */
449 dummy_driver.type = TTY_DRIVER_TYPE_SERIAL;
450 dummy_driver.subtype = SERIAL_TYPE_NORMAL;
451 dummy_driver.init_termios = tty_std_termios;
452 /* Normally B9600 default... */
453 dummy_driver.init_termios.c_cflag =
454 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
455 dummy_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
456 dummy_driver.init_termios.c_ispeed = 115200;
457 dummy_driver.init_termios.c_ospeed = 115200;
459 dummy_driver.ops = &dummy_ops;
460 if (tty_register_driver(&dummy_driver))
461 panic("Couldn't register dummy serial driver\n");
464 static struct tty_driver*
465 etrax_console_device(struct console* co, int *index)
467 if (port)
468 *index = port->index;
469 else
470 *index = 0;
471 #ifdef CONFIG_ETRAX_SERIAL
472 return port ? serial_driver : &dummy_driver;
473 #else
474 return &dummy_driver;
475 #endif
478 static struct console sercons = {
479 name : "ttyS",
480 write: console_write,
481 read : NULL,
482 device : etrax_console_device,
483 unblank : NULL,
484 setup : console_setup,
485 flags : CON_PRINTBUFFER,
486 index : -1,
487 cflag : 0,
488 next : NULL
490 static struct console sercons0 = {
491 name : "ttyS",
492 write: console_write,
493 read : NULL,
494 device : etrax_console_device,
495 unblank : NULL,
496 setup : console_setup,
497 flags : CON_PRINTBUFFER,
498 index : 0,
499 cflag : 0,
500 next : NULL
503 static struct console sercons1 = {
504 name : "ttyS",
505 write: console_write,
506 read : NULL,
507 device : etrax_console_device,
508 unblank : NULL,
509 setup : console_setup,
510 flags : CON_PRINTBUFFER,
511 index : 1,
512 cflag : 0,
513 next : NULL
515 static struct console sercons2 = {
516 name : "ttyS",
517 write: console_write,
518 read : NULL,
519 device : etrax_console_device,
520 unblank : NULL,
521 setup : console_setup,
522 flags : CON_PRINTBUFFER,
523 index : 2,
524 cflag : 0,
525 next : NULL
527 static struct console sercons3 = {
528 name : "ttyS",
529 write: console_write,
530 read : NULL,
531 device : etrax_console_device,
532 unblank : NULL,
533 setup : console_setup,
534 flags : CON_PRINTBUFFER,
535 index : 3,
536 cflag : 0,
537 next : NULL
540 * Register console (for printk's etc)
543 int __init
544 init_etrax_debug(void)
546 static int first = 1;
548 if (!first) {
549 unregister_console(&sercons);
550 register_console(&sercons0);
551 register_console(&sercons1);
552 register_console(&sercons2);
553 register_console(&sercons3);
554 init_dummy_console();
555 return 0;
558 first = 0;
559 register_console(&sercons);
560 start_port(port);
561 #ifdef CONFIG_ETRAX_KGDB
562 start_port(kgdb_port);
563 #endif
564 return 0;
566 __initcall(init_etrax_debug);