Merge git://git.infradead.org/battery-2.6
[pandora-kernel.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 preferred 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/console.h>
107 #include <linux/init.h>
108 #include <linux/major.h>
109 #include <linux/delay.h>
110 #include <linux/tty.h>
111 #include <asm/system.h>
112 #include <asm/arch/svinto.h>
113 #include <asm/io.h>             /* Get SIMCOUT. */
114
115 struct dbg_port
116 {
117   unsigned int index;
118   const volatile unsigned* read;
119   volatile char* write;
120   volatile unsigned* xoff;
121   volatile char* baud;
122   volatile char* tr_ctrl;
123   volatile char* rec_ctrl;
124   unsigned long irq;
125   unsigned int started;
126   unsigned long baudrate;
127   unsigned char parity;
128   unsigned int bits;
129 };
130
131 struct dbg_port ports[]=
132 {
133   {
134     0,
135     R_SERIAL0_READ,
136     R_SERIAL0_TR_DATA,
137     R_SERIAL0_XOFF,
138     R_SERIAL0_BAUD,
139     R_SERIAL0_TR_CTRL,
140     R_SERIAL0_REC_CTRL,
141     IO_STATE(R_IRQ_MASK1_SET, ser0_data, set),
142     0,
143     115200,
144     'N',
145     8
146   },
147   {
148     1,
149     R_SERIAL1_READ,
150     R_SERIAL1_TR_DATA,
151     R_SERIAL1_XOFF,
152     R_SERIAL1_BAUD,
153     R_SERIAL1_TR_CTRL,
154     R_SERIAL1_REC_CTRL,
155     IO_STATE(R_IRQ_MASK1_SET, ser1_data, set),
156     0,
157     115200,
158     'N',
159     8
160   },
161   {
162     2,
163     R_SERIAL2_READ,
164     R_SERIAL2_TR_DATA,
165     R_SERIAL2_XOFF,
166     R_SERIAL2_BAUD,
167     R_SERIAL2_TR_CTRL,
168     R_SERIAL2_REC_CTRL,
169     IO_STATE(R_IRQ_MASK1_SET, ser2_data, set),
170     0,
171     115200,
172     'N',
173     8
174   },
175   {
176     3,
177     R_SERIAL3_READ,
178     R_SERIAL3_TR_DATA,
179     R_SERIAL3_XOFF,
180     R_SERIAL3_BAUD,
181     R_SERIAL3_TR_CTRL,
182     R_SERIAL3_REC_CTRL,
183     IO_STATE(R_IRQ_MASK1_SET, ser3_data, set),
184     0,
185     115200,
186     'N',
187     8
188   }
189 };
190
191 extern struct tty_driver *serial_driver;
192
193 struct dbg_port* port =
194 #if defined(CONFIG_ETRAX_DEBUG_PORT0)
195   &ports[0];
196 #elif defined(CONFIG_ETRAX_DEBUG_PORT1)
197   &ports[1];
198 #elif defined(CONFIG_ETRAX_DEBUG_PORT2)
199   &ports[2];
200 #elif defined(CONFIG_ETRAX_DEBUG_PORT3)
201   &ports[3];
202 #else
203   NULL;
204 #endif
205
206 static struct dbg_port* kgdb_port =
207 #if defined(CONFIG_ETRAX_KGDB_PORT0)
208   &ports[0];
209 #elif defined(CONFIG_ETRAX_KGDB_PORT1)
210   &ports[1];
211 #elif defined(CONFIG_ETRAX_KGDB_PORT2)
212   &ports[2];
213 #elif defined(CONFIG_ETRAX_KGDB_PORT3)
214   &ports[3];
215 #else
216   NULL;
217 #endif
218
219 static void
220 start_port(struct dbg_port* p)
221 {
222         unsigned long rec_ctrl = 0;
223         unsigned long tr_ctrl = 0;
224
225         if (!p)
226                 return;
227
228         if (p->started)
229                 return;
230         p->started = 1;
231
232         if (p->index == 0)
233         {
234                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
235                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
236         }
237         else if (p->index == 1)
238         {
239                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
240                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
241         }
242         else if (p->index == 2)
243         {
244                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
245                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
246                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
247                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
248                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser2, select);
249         }
250         else
251         {
252                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
253                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
254                 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
255                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
256                 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser3, select);
257         }
258
259         *R_GEN_CONFIG = genconfig_shadow;
260
261         *p->xoff =
262                 IO_STATE(R_SERIAL0_XOFF, tx_stop, enable) |
263                 IO_STATE(R_SERIAL0_XOFF, auto_xoff, disable) |
264                 IO_FIELD(R_SERIAL0_XOFF, xoff_char, 0);
265
266         switch (p->baudrate)
267         {
268         case 0:
269         case 115200:
270                 *p->baud =
271                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
272                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
273                 break;
274         case 1200:
275                 *p->baud =
276                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c1200Hz) |
277                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c1200Hz);
278                 break;
279         case 2400:
280                 *p->baud =
281                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c2400Hz) |
282                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c2400Hz);
283                 break;
284         case 4800:
285                 *p->baud =
286                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c4800Hz) |
287                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c4800Hz);
288                 break;
289         case 9600:
290                 *p->baud =
291                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c9600Hz) |
292                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c9600Hz);
293                   break;
294         case 19200:
295                 *p->baud =
296                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c19k2Hz) |
297                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c19k2Hz);
298                  break;
299         case 38400:
300                 *p->baud =
301                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c38k4Hz) |
302                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c38k4Hz);
303                 break;
304         case 57600:
305                 *p->baud =
306                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c57k6Hz) |
307                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c57k6Hz);
308                 break;
309         default:
310                 *p->baud =
311                   IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
312                   IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
313                   break;
314         }
315
316         if (p->parity == 'E') {
317                 rec_ctrl =
318                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
319                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
320                 tr_ctrl =
321                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
322                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
323         } else if (p->parity == 'O') {
324                 rec_ctrl =
325                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd) |
326                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
327                 tr_ctrl =
328                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd) |
329                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
330         } else {
331                 rec_ctrl =
332                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
333                   IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, disable);
334                 tr_ctrl =
335                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
336                   IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, disable);
337         }
338         if (p->bits == 7)
339         {
340                 rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
341                 tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
342         }
343         else
344         {
345                 rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_8bit);
346                 tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_8bit);
347         }
348
349         *p->rec_ctrl =
350                 IO_STATE(R_SERIAL0_REC_CTRL, dma_err, stop) |
351                 IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable) |
352                 IO_STATE(R_SERIAL0_REC_CTRL, rts_, active) |
353                 IO_STATE(R_SERIAL0_REC_CTRL, sampling, middle) |
354                 IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, normal) |
355                 rec_ctrl;
356
357         *p->tr_ctrl =
358                 IO_FIELD(R_SERIAL0_TR_CTRL, txd, 0) |
359                 IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable) |
360                 IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, disabled) |
361                 IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, one_bit) |
362                 IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, normal) |
363                 tr_ctrl;
364 }
365
366 static void
367 console_write_direct(struct console *co, const char *buf, unsigned int len)
368 {
369         int i;
370         unsigned long flags;
371         local_irq_save(flags);
372
373         if (!port)
374                 return;
375
376         /* Send data */
377         for (i = 0; i < len; i++) {
378                 /* LF -> CRLF */
379                 if (buf[i] == '\n') {
380                         while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
381                         ;
382                         *port->write = '\r';
383                 }
384                 /* Wait until transmitter is ready and send.*/
385                 while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
386                         ;
387                 *port->write = buf[i];
388         }
389         local_irq_restore(flags);
390 }
391
392 int raw_printk(const char *fmt, ...)
393 {
394         static char buf[1024];
395         int printed_len;
396         static int first = 1;
397         if (first) {
398                 /* Force reinitialization of the port to get manual mode. */
399                 port->started = 0;
400                 start_port(port);
401                 first = 0;
402         }
403         va_list args;
404         va_start(args, fmt);
405         printed_len = vsnprintf(buf, sizeof(buf), fmt, args);
406         va_end(args);
407         console_write_direct(NULL, buf, strlen(buf));
408         return printed_len;
409 }
410
411 static void
412 console_write(struct console *co, const char *buf, unsigned int len)
413 {
414         if (!port)
415                 return;
416
417 #ifdef CONFIG_SVINTO_SIM
418         /* no use to simulate the serial debug output */
419         SIMCOUT(buf, len);
420         return;
421 #endif
422
423         console_write_direct(co, buf, len);
424 }
425
426 /* legacy function */
427
428 void
429 console_print_etrax(const char *buf)
430 {
431         console_write(NULL, buf, strlen(buf));
432 }
433
434 /* Use polling to get a single character FROM the debug port */
435
436 int
437 getDebugChar(void)
438 {
439         unsigned long readval;
440
441         if (!kgdb_port)
442                 return 0;
443
444         do {
445                 readval = *kgdb_port->read;
446         } while (!(readval & IO_MASK(R_SERIAL0_READ, data_avail)));
447
448         return (readval & IO_MASK(R_SERIAL0_READ, data_in));
449 }
450
451 /* Use polling to put a single character to the debug port */
452
453 void
454 putDebugChar(int val)
455 {
456         if (!kgdb_port)
457                 return;
458
459         while (!(*kgdb_port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
460                 ;
461         *kgdb_port->write = val;
462 }
463
464 /* Enable irq for receiving chars on the debug port, used by kgdb */
465
466 void
467 enableDebugIRQ(void)
468 {
469         if (!kgdb_port)
470                 return;
471
472         *R_IRQ_MASK1_SET = kgdb_port->irq;
473         /* use R_VECT_MASK directly, since we really bypass Linux normal
474          * IRQ handling in kgdb anyway, we don't need to use enable_irq
475          */
476         *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
477
478         *kgdb_port->rec_ctrl = IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
479 }
480
481 static int __init
482 console_setup(struct console *co, char *options)
483 {
484         char* s;
485
486         if (options) {
487                 port = &ports[co->index];
488                 port->baudrate = 115200;
489                 port->parity = 'N';
490                 port->bits = 8;
491                 port->baudrate = simple_strtoul(options, NULL, 10);
492                 s = options;
493                 while(*s >= '0' && *s <= '9')
494                         s++;
495                 if (*s) port->parity = *s++;
496                 if (*s) port->bits   = *s++ - '0';
497                 port->started = 0;
498                 start_port(0);
499         }
500         return 0;
501 }
502
503 /* This is a dummy serial device that throws away anything written to it.
504  * This is used when no debug output is wanted.
505  */
506 static struct tty_driver dummy_driver;
507
508 static int dummy_open(struct tty_struct *tty, struct file * filp)
509 {
510         return 0;
511 }
512
513 static void dummy_close(struct tty_struct *tty, struct file * filp)
514 {
515 }
516
517 static int dummy_write(struct tty_struct * tty,
518                        const unsigned char *buf, int count)
519 {
520         return count;
521 }
522
523 static int
524 dummy_write_room(struct tty_struct *tty)
525 {
526         return 8192;
527 }
528
529 void __init
530 init_dummy_console(void)
531 {
532         memset(&dummy_driver, 0, sizeof(struct tty_driver));
533         dummy_driver.driver_name = "serial";
534         dummy_driver.name = "ttyS";
535         dummy_driver.major = TTY_MAJOR;
536         dummy_driver.minor_start = 68;
537         dummy_driver.num = 1;       /* etrax100 has 4 serial ports */
538         dummy_driver.type = TTY_DRIVER_TYPE_SERIAL;
539         dummy_driver.subtype = SERIAL_TYPE_NORMAL;
540         dummy_driver.init_termios = tty_std_termios;
541         dummy_driver.init_termios.c_cflag =
542                 B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
543         dummy_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
544
545         dummy_driver.open = dummy_open;
546         dummy_driver.close = dummy_close;
547         dummy_driver.write = dummy_write;
548         dummy_driver.write_room = dummy_write_room;
549         if (tty_register_driver(&dummy_driver))
550                 panic("Couldn't register dummy serial driver\n");
551 }
552
553 static struct tty_driver*
554 etrax_console_device(struct console* co, int *index)
555 {
556         if (port)
557                 *index = port->index;
558         return port ? serial_driver : &dummy_driver;
559 }
560
561 static struct console sercons = {
562         name : "ttyS",
563         write: console_write,
564         read : NULL,
565         device : etrax_console_device,
566         unblank : NULL,
567         setup : console_setup,
568         flags : CON_PRINTBUFFER,
569         index : -1,
570         cflag : 0,
571         next : NULL
572 };
573 static struct console sercons0 = {
574         name : "ttyS",
575         write: console_write,
576         read : NULL,
577         device : etrax_console_device,
578         unblank : NULL,
579         setup : console_setup,
580         flags : CON_PRINTBUFFER,
581         index : 0,
582         cflag : 0,
583         next : NULL
584 };
585
586 static struct console sercons1 = {
587         name : "ttyS",
588         write: console_write,
589         read : NULL,
590         device : etrax_console_device,
591         unblank : NULL,
592         setup : console_setup,
593         flags : CON_PRINTBUFFER,
594         index : 1,
595         cflag : 0,
596         next : NULL
597 };
598 static struct console sercons2 = {
599         name : "ttyS",
600         write: console_write,
601         read : NULL,
602         device : etrax_console_device,
603         unblank : NULL,
604         setup : console_setup,
605         flags : CON_PRINTBUFFER,
606         index : 2,
607         cflag : 0,
608         next : NULL
609 };
610 static struct console sercons3 = {
611         name : "ttyS",
612         write: console_write,
613         read : NULL,
614         device : etrax_console_device,
615         unblank : NULL,
616         setup : console_setup,
617         flags : CON_PRINTBUFFER,
618         index : 3,
619         cflag : 0,
620         next : NULL
621 };
622 /*
623  *      Register console (for printk's etc)
624  */
625
626 int __init
627 init_etrax_debug(void)
628 {
629         static int first = 1;
630
631         if (!first) {
632                 unregister_console(&sercons);
633                 register_console(&sercons0);
634                 register_console(&sercons1);
635                 register_console(&sercons2);
636                 register_console(&sercons3);
637                 init_dummy_console();
638                 return 0;
639         }
640
641         first = 0;
642         register_console(&sercons);
643         start_port(port);
644 #ifdef CONFIG_ETRAX_KGDB
645         start_port(kgdb_port);
646 #endif
647         return 0;
648 }
649 __initcall(init_etrax_debug);