Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / arch / cris / arch-v10 / kernel / debugport.c
1 /* Serialport functions for debugging
2  *
3  * Copyright (c) 2000 Axis Communications AB
4  *
5  * Authors:  Bjorn Wesen
6  *
7  * Exports:
8  *    console_print_etrax(char *buf)
9  *    int getDebugChar()
10  *    putDebugChar(int)
11  *    enableDebugIRQ()
12  *    init_etrax_debug()
13  *
14  * $Log: debugport.c,v $
15  * Revision 1.27  2005/06/10 10:34:14  starvik
16  * Real console support
17  *
18  * Revision 1.26  2005/06/07 07:06:07  starvik
19  * Added LF->CR translation to make ETRAX customers happy.
20  *
21  * Revision 1.25  2005/03/08 08:56:47  mikaelam
22  * Do only set index as port->index if port is defined, otherwise use the index from the command line
23  *
24  * Revision 1.24  2005/01/19 10:26:33  mikaelam
25  * Return the cris serial driver in console device driver callback function
26  *
27  * Revision 1.23  2005/01/14 10:12:17  starvik
28  * KGDB on separate port.
29  * Console fixes from 2.4.
30  *
31  * Revision 1.22  2005/01/11 16:06:13  starvik
32  * typo
33  *
34  * Revision 1.21  2005/01/11 13:49:14  starvik
35  * Added raw_printk to be used where we don't trust the console.
36  *
37  * Revision 1.20  2004/12/27 11:18:32  starvik
38  * Merge of Linux 2.6.10 (not functional yet).
39  *
40  * Revision 1.19  2004/10/21 07:26:16  starvik
41  * Made it possible to specify console settings on kernel command line.
42  *
43  * Revision 1.18  2004/10/19 13:07:37  starvik
44  * Merge of Linux 2.6.9
45  *
46  * Revision 1.17  2004/09/29 10:33:46  starvik
47  * Resolved a dealock when printing debug from kernel.
48  *
49  * Revision 1.16  2004/08/24 06:12:19  starvik
50  * Whitespace cleanup
51  *
52  * Revision 1.15  2004/08/16 12:37:19  starvik
53  * Merge of Linux 2.6.8
54  *
55  * Revision 1.14  2004/05/17 13:11:29  starvik
56  * Disable DMA until real serial driver is up
57  *
58  * Revision 1.13  2004/05/14 07:58:01  starvik
59  * Merge of changes from 2.4
60  *
61  * Revision 1.12  2003/09/11 07:29:49  starvik
62  * Merge of Linux 2.6.0-test5
63  *
64  * Revision 1.11  2003/07/07 09:53:36  starvik
65  * Revert all the 2.5.74 merge changes to make the console work again
66  *
67  * Revision 1.9  2003/02/17 17:07:23  starvik
68  * Solved the problem with corrupted debug output (from Linux 2.4)
69  *   * Wait until DMA, FIFO and pipe is empty before and after transmissions
70  *   * Buffer data until a FIFO flush can be triggered.
71  *
72  * Revision 1.8  2003/01/22 06:48:36  starvik
73  * Fixed warnings issued by GCC 3.2.1
74  *
75  * Revision 1.7  2002/12/12 08:26:32  starvik
76  * Don't use C-comments inside CVS comments
77  *
78  * Revision 1.6  2002/12/11 15:42:02  starvik
79  * Extracted v10 (ETRAX 100LX) specific stuff from arch/cris/kernel/
80  *
81  * Revision 1.5  2002/11/20 06:58:03  starvik
82  * Compiles with kgdb
83  *
84  * Revision 1.4  2002/11/19 14:35:24  starvik
85  * Changes from linux 2.4
86  * Changed struct initializer syntax to the currently prefered notation
87  *
88  * Revision 1.3  2002/11/06 09:47:03  starvik
89  * Modified for new interrupt macros
90  *
91  * Revision 1.2  2002/01/21 15:21:50  bjornw
92  * Update for kdev_t changes
93  *
94  * Revision 1.6  2001/04/17 13:58:39  orjanf
95  * * Renamed CONFIG_KGDB to CONFIG_ETRAX_KGDB.
96  *
97  * Revision 1.5  2001/03/26 14:22:05  bjornw
98  * Namechange of some config options
99  *
100  * Revision 1.4  2000/10/06 12:37:26  bjornw
101  * Use physical addresses when talking to DMA
102  *
103  *
104  */
105
106 #include <linux/config.h>
107 #include <linux/console.h>
108 #include <linux/init.h>
109 #include <linux/major.h>
110 #include <linux/delay.h>
111 #include <linux/tty.h>
112 #include <asm/system.h>
113 #include <asm/arch/svinto.h>
114 #include <asm/io.h>             /* Get SIMCOUT. */
115
116 struct dbg_port
117 {
118   unsigned int index;
119   const volatile unsigned* read;
120   volatile char* write;
121   volatile unsigned* xoff;
122   volatile char* baud;
123   volatile char* tr_ctrl;
124   volatile char* rec_ctrl;
125   unsigned long irq;
126   unsigned int started;
127   unsigned long baudrate;
128   unsigned char parity;
129   unsigned int bits;
130 };
131
132 struct dbg_port ports[]=
133 {
134   {
135     0,
136     R_SERIAL0_READ,
137     R_SERIAL0_TR_DATA,
138     R_SERIAL0_XOFF,
139     R_SERIAL0_BAUD,
140     R_SERIAL0_TR_CTRL,
141     R_SERIAL0_REC_CTRL,
142     IO_STATE(R_IRQ_MASK1_SET, ser0_data, set),
143     0,
144     115200,
145     'N',
146     8
147   },
148   {
149     1,
150     R_SERIAL1_READ,
151     R_SERIAL1_TR_DATA,
152     R_SERIAL1_XOFF,
153     R_SERIAL1_BAUD,
154     R_SERIAL1_TR_CTRL,
155     R_SERIAL1_REC_CTRL,
156     IO_STATE(R_IRQ_MASK1_SET, ser1_data, set),
157     0,
158     115200,
159     'N',
160     8
161   },
162   {
163     2,
164     R_SERIAL2_READ,
165     R_SERIAL2_TR_DATA,
166     R_SERIAL2_XOFF,
167     R_SERIAL2_BAUD,
168     R_SERIAL2_TR_CTRL,
169     R_SERIAL2_REC_CTRL,
170     IO_STATE(R_IRQ_MASK1_SET, ser2_data, set),
171     0,
172     115200,
173     'N',
174     8
175   },
176   {
177     3,
178     R_SERIAL3_READ,
179     R_SERIAL3_TR_DATA,
180     R_SERIAL3_XOFF,
181     R_SERIAL3_BAUD,
182     R_SERIAL3_TR_CTRL,
183     R_SERIAL3_REC_CTRL,
184     IO_STATE(R_IRQ_MASK1_SET, ser3_data, set),
185     0,
186     115200,
187     'N',
188     8
189   }
190 };
191
192 extern struct tty_driver *serial_driver;
193
194 struct dbg_port* port =
195 #if defined(CONFIG_ETRAX_DEBUG_PORT0)
196   &ports[0];
197 #elif defined(CONFIG_ETRAX_DEBUG_PORT1)
198   &ports[1];
199 #elif defined(CONFIG_ETRAX_DEBUG_PORT2)
200   &ports[2];
201 #elif defined(CONFIG_ETRAX_DEBUG_PORT3)
202   &ports[3];
203 #else
204   NULL;
205 #endif
206
207 static struct dbg_port* kgdb_port =
208 #if defined(CONFIG_ETRAX_KGDB_PORT0)
209   &ports[0];
210 #elif defined(CONFIG_ETRAX_KGDB_PORT1)
211   &ports[1];
212 #elif defined(CONFIG_ETRAX_KGDB_PORT2)
213   &ports[2];
214 #elif defined(CONFIG_ETRAX_KGDB_PORT3)
215   &ports[3];
216 #else
217   NULL;
218 #endif
219
220 static void
221 start_port(struct dbg_port* p)
222 {
223         unsigned long rec_ctrl = 0;
224         unsigned long tr_ctrl = 0;
225
226         if (!p)
227                 return;
228
229         if (p->started)
230                 return;
231         p->started = 1;
232
233         if (p->index == 0)
234         {
235                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
236                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
237         }
238         else if (p->index == 1)
239         {
240                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
241                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
242         }
243         else if (p->index == 2)
244         {
245                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
246                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
247                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
248                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
249                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser2, select);
250         }
251         else
252         {
253                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
254                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
255                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
256                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
257                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser3, select);
258         }
259
260         *R_GEN_CONFIG = genconfig_shadow;
261
262         *p->xoff =
263                 IO_STATE(R_SERIAL0_XOFF, tx_stop, enable) |
264                 IO_STATE(R_SERIAL0_XOFF, auto_xoff, disable) |
265                 IO_FIELD(R_SERIAL0_XOFF, xoff_char, 0);
266
267         switch (p->baudrate)
268         {
269         case 0:
270         case 115200:
271                 *p->baud =
272                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
273                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
274                 break;
275         case 1200:
276                 *p->baud =
277                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c1200Hz) |
278                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c1200Hz);
279                 break;
280         case 2400:
281                 *p->baud =
282                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c2400Hz) |
283                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c2400Hz);
284                 break;
285         case 4800:
286                 *p->baud =
287                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c4800Hz) |
288                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c4800Hz);
289                 break;
290         case 9600:
291                 *p->baud =
292                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c9600Hz) |
293                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c9600Hz);
294                   break;
295         case 19200:
296                 *p->baud =
297                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c19k2Hz) |
298                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c19k2Hz);
299                  break;
300         case 38400:
301                 *p->baud =
302                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c38k4Hz) |
303                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c38k4Hz);
304                 break;
305         case 57600:
306                 *p->baud =
307                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c57k6Hz) |
308                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c57k6Hz);
309                 break;
310         default:
311                 *p->baud =
312                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
313                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
314                   break;
315         }
316
317         if (p->parity == 'E') {
318                 rec_ctrl =
319                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
320                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
321                 tr_ctrl =
322                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
323                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
324         } else if (p->parity == 'O') {
325                 rec_ctrl =
326                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd) |
327                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
328                 tr_ctrl =
329                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd) |
330                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
331         } else {
332                 rec_ctrl =
333                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
334                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, disable);
335                 tr_ctrl =
336                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
337                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, disable);
338         }
339         if (p->bits == 7)
340         {
341                 rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
342                 tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
343         }
344         else
345         {
346                 rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_8bit);
347                 tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_8bit);
348         }
349
350         *p->rec_ctrl =
351                 IO_STATE(R_SERIAL0_REC_CTRL, dma_err, stop) |
352                 IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable) |
353                 IO_STATE(R_SERIAL0_REC_CTRL, rts_, active) |
354                 IO_STATE(R_SERIAL0_REC_CTRL, sampling, middle) |
355                 IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, normal) |
356                 rec_ctrl;
357
358         *p->tr_ctrl =
359                 IO_FIELD(R_SERIAL0_TR_CTRL, txd, 0) |
360                 IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable) |
361                 IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, disabled) |
362                 IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, one_bit) |
363                 IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, normal) |
364                 tr_ctrl;
365 }
366
367 static void
368 console_write_direct(struct console *co, const char *buf, unsigned int len)
369 {
370         int i;
371         unsigned long flags;
372         local_irq_save(flags);
373
374         if (!port)
375                 return;
376
377         /* Send data */
378         for (i = 0; i < len; i++) {
379                 /* LF -> CRLF */
380                 if (buf[i] == '\n') {
381                         while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
382                         ;
383                         *port->write = '\r';
384                 }
385                 /* Wait until transmitter is ready and send.*/
386                 while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
387                         ;
388                 *port->write = buf[i];
389         }
390         local_irq_restore(flags);
391 }
392
393 int raw_printk(const char *fmt, ...)
394 {
395         static char buf[1024];
396         int printed_len;
397         static int first = 1;
398         if (first) {
399                 /* Force reinitialization of the port to get manual mode. */
400                 port->started = 0;
401                 start_port(port);
402                 first = 0;
403         }
404         va_list args;
405         va_start(args, fmt);
406         printed_len = vsnprintf(buf, sizeof(buf), fmt, args);
407         va_end(args);
408         console_write_direct(NULL, buf, strlen(buf));
409         return printed_len;
410 }
411
412 static void
413 console_write(struct console *co, const char *buf, unsigned int len)
414 {
415         if (!port)
416                 return;
417
418 #ifdef CONFIG_SVINTO_SIM
419         /* no use to simulate the serial debug output */
420         SIMCOUT(buf, len);
421         return;
422 #endif
423
424         console_write_direct(co, buf, len);
425 }
426
427 /* legacy function */
428
429 void
430 console_print_etrax(const char *buf)
431 {
432         console_write(NULL, buf, strlen(buf));
433 }
434
435 /* Use polling to get a single character FROM the debug port */
436
437 int
438 getDebugChar(void)
439 {
440         unsigned long readval;
441
442         if (!kgdb_port)
443                 return 0;
444
445         do {
446                 readval = *kgdb_port->read;
447         } while (!(readval & IO_MASK(R_SERIAL0_READ, data_avail)));
448
449         return (readval & IO_MASK(R_SERIAL0_READ, data_in));
450 }
451
452 /* Use polling to put a single character to the debug port */
453
454 void
455 putDebugChar(int val)
456 {
457         if (!kgdb_port)
458                 return;
459
460         while (!(*kgdb_port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
461                 ;
462         *kgdb_port->write = val;
463 }
464
465 /* Enable irq for receiving chars on the debug port, used by kgdb */
466
467 void
468 enableDebugIRQ(void)
469 {
470         if (!kgdb_port)
471                 return;
472
473         *R_IRQ_MASK1_SET = kgdb_port->irq;
474         /* use R_VECT_MASK directly, since we really bypass Linux normal
475          * IRQ handling in kgdb anyway, we don't need to use enable_irq
476          */
477         *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
478
479         *kgdb_port->rec_ctrl = IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
480 }
481
482 static int __init
483 console_setup(struct console *co, char *options)
484 {
485         char* s;
486
487         if (options) {
488                 port = &ports[co->index];
489                 port->baudrate = 115200;
490                 port->parity = 'N';
491                 port->bits = 8;
492                 port->baudrate = simple_strtoul(options, NULL, 10);
493                 s = options;
494                 while(*s >= '0' && *s <= '9')
495                         s++;
496                 if (*s) port->parity = *s++;
497                 if (*s) port->bits   = *s++ - '0';
498                 port->started = 0;
499                 start_port(0);
500         }
501         return 0;
502 }
503
504 /* This is a dummy serial device that throws away anything written to it.
505  * This is used when no debug output is wanted.
506  */
507 static struct tty_driver dummy_driver;
508
509 static int dummy_open(struct tty_struct *tty, struct file * filp)
510 {
511         return 0;
512 }
513
514 static void dummy_close(struct tty_struct *tty, struct file * filp)
515 {
516 }
517
518 static int dummy_write(struct tty_struct * tty,
519                        const unsigned char *buf, int count)
520 {
521         return count;
522 }
523
524 static int
525 dummy_write_room(struct tty_struct *tty)
526 {
527         return 8192;
528 }
529
530 void __init
531 init_dummy_console(void)
532 {
533         memset(&dummy_driver, 0, sizeof(struct tty_driver));
534         dummy_driver.driver_name = "serial";
535         dummy_driver.name = "ttyS";
536         dummy_driver.major = TTY_MAJOR;
537         dummy_driver.minor_start = 68;
538         dummy_driver.num = 1;       /* etrax100 has 4 serial ports */
539         dummy_driver.type = TTY_DRIVER_TYPE_SERIAL;
540         dummy_driver.subtype = SERIAL_TYPE_NORMAL;
541         dummy_driver.init_termios = tty_std_termios;
542         dummy_driver.init_termios.c_cflag =
543                 B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
544         dummy_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
545
546         dummy_driver.open = dummy_open;
547         dummy_driver.close = dummy_close;
548         dummy_driver.write = dummy_write;
549         dummy_driver.write_room = dummy_write_room;
550         if (tty_register_driver(&dummy_driver))
551                 panic("Couldn't register dummy serial driver\n");
552 }
553
554 static struct tty_driver*
555 etrax_console_device(struct console* co, int *index)
556 {
557         if (port)
558                 *index = port->index;
559         return port ? serial_driver : &dummy_driver;
560 }
561
562 static struct console sercons = {
563         name : "ttyS",
564         write: console_write,
565         read : NULL,
566         device : etrax_console_device,
567         unblank : NULL,
568         setup : console_setup,
569         flags : CON_PRINTBUFFER,
570         index : -1,
571         cflag : 0,
572         next : NULL
573 };
574 static struct console sercons0 = {
575         name : "ttyS",
576         write: console_write,
577         read : NULL,
578         device : etrax_console_device,
579         unblank : NULL,
580         setup : console_setup,
581         flags : CON_PRINTBUFFER,
582         index : 0,
583         cflag : 0,
584         next : NULL
585 };
586
587 static struct console sercons1 = {
588         name : "ttyS",
589         write: console_write,
590         read : NULL,
591         device : etrax_console_device,
592         unblank : NULL,
593         setup : console_setup,
594         flags : CON_PRINTBUFFER,
595         index : 1,
596         cflag : 0,
597         next : NULL
598 };
599 static struct console sercons2 = {
600         name : "ttyS",
601         write: console_write,
602         read : NULL,
603         device : etrax_console_device,
604         unblank : NULL,
605         setup : console_setup,
606         flags : CON_PRINTBUFFER,
607         index : 2,
608         cflag : 0,
609         next : NULL
610 };
611 static struct console sercons3 = {
612         name : "ttyS",
613         write: console_write,
614         read : NULL,
615         device : etrax_console_device,
616         unblank : NULL,
617         setup : console_setup,
618         flags : CON_PRINTBUFFER,
619         index : 3,
620         cflag : 0,
621         next : NULL
622 };
623 /*
624  *      Register console (for printk's etc)
625  */
626
627 int __init
628 init_etrax_debug(void)
629 {
630         static int first = 1;
631
632         if (!first) {
633                 unregister_console(&sercons);
634                 register_console(&sercons0);
635                 register_console(&sercons1);
636                 register_console(&sercons2);
637                 register_console(&sercons3);
638                 init_dummy_console();
639                 return 0;
640         }
641
642         first = 0;
643         register_console(&sercons);
644         start_port(port);
645 #ifdef CONFIG_ETRAX_KGDB
646         start_port(kgdb_port);
647 #endif
648         return 0;
649 }
650 __initcall(init_etrax_debug);