vserver 1.9.5.x5
[linux-2.6.git] / arch / ppc / xmon / start.c
1 /*
2  * Copyright (C) 1996 Paul Mackerras.
3  */
4 #include <linux/config.h>
5 #include <linux/string.h>
6 #include <asm/machdep.h>
7 #include <asm/io.h>
8 #include <asm/page.h>
9 #include <linux/adb.h>
10 #include <linux/pmu.h>
11 #include <linux/cuda.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/sysrq.h>
15 #include <linux/bitops.h>
16 #include <asm/xmon.h>
17 #include <asm/prom.h>
18 #include <asm/bootx.h>
19 #include <asm/machdep.h>
20 #include <asm/errno.h>
21 #include <asm/pmac_feature.h>
22 #include <asm/processor.h>
23 #include <asm/delay.h>
24 #include <asm/btext.h>
25
26 static volatile unsigned char *sccc, *sccd;
27 unsigned int TXRDY, RXRDY, DLAB;
28 static int xmon_expect(const char *str, unsigned int timeout);
29
30 static int use_serial;
31 static int use_screen;
32 static int via_modem;
33 static int xmon_use_sccb;
34 static struct device_node *channel_node;
35
36 #define TB_SPEED        25000000
37
38 static inline unsigned int readtb(void)
39 {
40         unsigned int ret;
41
42         asm volatile("mftb %0" : "=r" (ret) :);
43         return ret;
44 }
45
46 void buf_access(void)
47 {
48         if (DLAB)
49                 sccd[3] &= ~DLAB;       /* reset DLAB */
50 }
51
52 extern int adb_init(void);
53
54 #ifdef CONFIG_PPC_CHRP
55 /*
56  * This looks in the "ranges" property for the primary PCI host bridge
57  * to find the physical address of the start of PCI/ISA I/O space.
58  * It is basically a cut-down version of pci_process_bridge_OF_ranges.
59  */
60 static unsigned long chrp_find_phys_io_base(void)
61 {
62         struct device_node *node;
63         unsigned int *ranges;
64         unsigned long base = CHRP_ISA_IO_BASE;
65         int rlen = 0;
66         int np;
67
68         node = find_devices("isa");
69         if (node != NULL) {
70                 node = node->parent;
71                 if (node == NULL || node->type == NULL
72                     || strcmp(node->type, "pci") != 0)
73                         node = NULL;
74         }
75         if (node == NULL)
76                 node = find_devices("pci");
77         if (node == NULL)
78                 return base;
79
80         ranges = (unsigned int *) get_property(node, "ranges", &rlen);
81         np = prom_n_addr_cells(node) + 5;
82         while ((rlen -= np * sizeof(unsigned int)) >= 0) {
83                 if ((ranges[0] >> 24) == 1 && ranges[2] == 0) {
84                         /* I/O space starting at 0, grab the phys base */
85                         base = ranges[np - 3];
86                         break;
87                 }
88                 ranges += np;
89         }
90         return base;
91 }
92 #endif /* CONFIG_PPC_CHRP */
93
94 #ifdef CONFIG_MAGIC_SYSRQ
95 static void sysrq_handle_xmon(int key, struct pt_regs *regs,
96                               struct tty_struct *tty)
97 {
98         xmon(regs);
99 }
100
101 static struct sysrq_key_op sysrq_xmon_op =
102 {
103         .handler =      sysrq_handle_xmon,
104         .help_msg =     "Xmon",
105         .action_msg =   "Entering xmon",
106 };
107 #endif
108
109 void
110 xmon_map_scc(void)
111 {
112 #ifdef CONFIG_PPC_MULTIPLATFORM
113         volatile unsigned char *base;
114
115         if (_machine == _MACH_Pmac) {
116                 struct device_node *np;
117                 unsigned long addr;
118 #ifdef CONFIG_BOOTX_TEXT
119                 if (!use_screen && !use_serial
120                     && !machine_is_compatible("iMac")) {
121                         /* see if there is a keyboard in the device tree
122                            with a parent of type "adb" */
123                         for (np = find_devices("keyboard"); np; np = np->next)
124                                 if (np->parent && np->parent->type
125                                     && strcmp(np->parent->type, "adb") == 0)
126                                         break;
127
128                         /* needs to be hacked if xmon_printk is to be used
129                            from within find_via_pmu() */
130 #ifdef CONFIG_ADB_PMU
131                         if (np != NULL && boot_text_mapped && find_via_pmu())
132                                 use_screen = 1;
133 #endif
134 #ifdef CONFIG_ADB_CUDA
135                         if (np != NULL && boot_text_mapped && find_via_cuda())
136                                 use_screen = 1;
137 #endif
138                 }
139                 if (!use_screen && (np = find_devices("escc")) != NULL) {
140                         /*
141                          * look for the device node for the serial port
142                          * we're using and see if it says it has a modem
143                          */
144                         char *name = xmon_use_sccb? "ch-b": "ch-a";
145                         char *slots;
146                         int l;
147
148                         np = np->child;
149                         while (np != NULL && strcmp(np->name, name) != 0)
150                                 np = np->sibling;
151                         if (np != NULL) {
152                                 /* XXX should parse this properly */
153                                 channel_node = np;
154                                 slots = get_property(np, "slot-names", &l);
155                                 if (slots != NULL && l >= 10
156                                     && strcmp(slots+4, "Modem") == 0)
157                                         via_modem = 1;
158                         }
159                 }
160                 btext_drawstring("xmon uses ");
161                 if (use_screen)
162                         btext_drawstring("screen and keyboard\n");
163                 else {
164                         if (via_modem)
165                                 btext_drawstring("modem on ");
166                         btext_drawstring(xmon_use_sccb? "printer": "modem");
167                         btext_drawstring(" port\n");
168                 }
169
170 #endif /* CONFIG_BOOTX_TEXT */
171
172 #ifdef CHRP_ESCC
173                 addr = 0xc1013020;
174 #else
175                 addr = 0xf3013020;
176 #endif
177                 TXRDY = 4;
178                 RXRDY = 1;
179         
180                 np = find_devices("mac-io");
181                 if (np && np->n_addrs)
182                         addr = np->addrs[0].address + 0x13020;
183                 base = (volatile unsigned char *) ioremap(addr & PAGE_MASK, PAGE_SIZE);
184                 sccc = base + (addr & ~PAGE_MASK);
185                 sccd = sccc + 0x10;
186
187         } else {
188                 base = (volatile unsigned char *) isa_io_base;
189                 if (_machine == _MACH_chrp)
190                         base = (volatile unsigned char *)
191                                 ioremap(chrp_find_phys_io_base(), 0x1000);
192
193                 sccc = base + 0x3fd;
194                 sccd = base + 0x3f8;
195                 if (xmon_use_sccb) {
196                         sccc -= 0x100;
197                         sccd -= 0x100;
198                 }
199                 TXRDY = 0x20;
200                 RXRDY = 1;
201                 DLAB = 0x80;
202         }
203 #elif defined(CONFIG_GEMINI)
204         /* should already be mapped by the kernel boot */
205         sccc = (volatile unsigned char *) 0xffeffb0d;
206         sccd = (volatile unsigned char *) 0xffeffb08;
207         TXRDY = 0x20;
208         RXRDY = 1;
209         DLAB = 0x80;
210 #elif defined(CONFIG_405GP)
211         sccc = (volatile unsigned char *)0xef600305;
212         sccd = (volatile unsigned char *)0xef600300;
213         TXRDY = 0x20;
214         RXRDY = 1;
215         DLAB = 0x80;
216 #endif /* platform */
217
218 #ifdef CONFIG_MAGIC_SYSRQ
219         __sysrq_put_key_op('x', &sysrq_xmon_op);
220 #endif
221 }
222
223 static int scc_initialized = 0;
224
225 void xmon_init_scc(void);
226 extern void cuda_poll(void);
227
228 static inline void do_poll_adb(void)
229 {
230 #ifdef CONFIG_ADB_PMU
231         if (sys_ctrler == SYS_CTRLER_PMU)
232                 pmu_poll_adb();
233 #endif /* CONFIG_ADB_PMU */
234 #ifdef CONFIG_ADB_CUDA
235         if (sys_ctrler == SYS_CTRLER_CUDA)
236                 cuda_poll();
237 #endif /* CONFIG_ADB_CUDA */
238 }
239
240 int
241 xmon_write(void *handle, void *ptr, int nb)
242 {
243         char *p = ptr;
244         int i, c, ct;
245
246 #ifdef CONFIG_SMP
247         static unsigned long xmon_write_lock;
248         int lock_wait = 1000000;
249         int locked;
250
251         while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
252                 if (--lock_wait == 0)
253                         break;
254 #endif
255
256 #ifdef CONFIG_BOOTX_TEXT
257         if (use_screen) {
258                 /* write it on the screen */
259                 for (i = 0; i < nb; ++i)
260                         btext_drawchar(*p++);
261                 goto out;
262         }
263 #endif
264         if (!scc_initialized)
265                 xmon_init_scc();
266         ct = 0;
267         for (i = 0; i < nb; ++i) {
268                 while ((*sccc & TXRDY) == 0)
269                         do_poll_adb();
270                 c = p[i];
271                 if (c == '\n' && !ct) {
272                         c = '\r';
273                         ct = 1;
274                         --i;
275                 } else {
276                         ct = 0;
277                 }
278                 buf_access();
279                 *sccd = c;
280                 eieio();
281         }
282
283  out:
284 #ifdef CONFIG_SMP
285         if (!locked)
286                 clear_bit(0, &xmon_write_lock);
287 #endif
288         return nb;
289 }
290
291 int xmon_wants_key;
292 int xmon_adb_keycode;
293
294 #ifdef CONFIG_BOOTX_TEXT
295 static int xmon_adb_shiftstate;
296
297 static unsigned char xmon_keytab[128] =
298         "asdfhgzxcv\000bqwer"                           /* 0x00 - 0x0f */
299         "yt123465=97-80]o"                              /* 0x10 - 0x1f */
300         "u[ip\rlj'k;\\,/nm."                            /* 0x20 - 0x2f */
301         "\t `\177\0\033\0\0\0\0\0\0\0\0\0\0"            /* 0x30 - 0x3f */
302         "\0.\0*\0+\0\0\0\0\0/\r\0-\0"                   /* 0x40 - 0x4f */
303         "\0\0000123456789\0\0\0";                       /* 0x50 - 0x5f */
304
305 static unsigned char xmon_shift_keytab[128] =
306         "ASDFHGZXCV\000BQWER"                           /* 0x00 - 0x0f */
307         "YT!@#$^%+(&_*)}O"                              /* 0x10 - 0x1f */
308         "U{IP\rLJ\"K:|<?NM>"                            /* 0x20 - 0x2f */
309         "\t ~\177\0\033\0\0\0\0\0\0\0\0\0\0"            /* 0x30 - 0x3f */
310         "\0.\0*\0+\0\0\0\0\0/\r\0-\0"                   /* 0x40 - 0x4f */
311         "\0\0000123456789\0\0\0";                       /* 0x50 - 0x5f */
312
313 static int
314 xmon_get_adb_key(void)
315 {
316         int k, t, on;
317
318         xmon_wants_key = 1;
319         for (;;) {
320                 xmon_adb_keycode = -1;
321                 t = 0;
322                 on = 0;
323                 do {
324                         if (--t < 0) {
325                                 on = 1 - on;
326                                 btext_drawchar(on? 0xdb: 0x20);
327                                 btext_drawchar('\b');
328                                 t = 200000;
329                         }
330                         do_poll_adb();
331                 } while (xmon_adb_keycode == -1);
332                 k = xmon_adb_keycode;
333                 if (on)
334                         btext_drawstring(" \b");
335
336                 /* test for shift keys */
337                 if ((k & 0x7f) == 0x38 || (k & 0x7f) == 0x7b) {
338                         xmon_adb_shiftstate = (k & 0x80) == 0;
339                         continue;
340                 }
341                 if (k >= 0x80)
342                         continue;       /* ignore up transitions */
343                 k = (xmon_adb_shiftstate? xmon_shift_keytab: xmon_keytab)[k];
344                 if (k != 0)
345                         break;
346         }
347         xmon_wants_key = 0;
348         return k;
349 }
350 #endif /* CONFIG_BOOTX_TEXT */
351
352 int
353 xmon_read(void *handle, void *ptr, int nb)
354 {
355     char *p = ptr;
356     int i;
357
358 #ifdef CONFIG_BOOTX_TEXT
359     if (use_screen) {
360         for (i = 0; i < nb; ++i)
361             *p++ = xmon_get_adb_key();
362         return i;
363     }
364 #endif
365     if (!scc_initialized)
366         xmon_init_scc();
367     for (i = 0; i < nb; ++i) {
368         while ((*sccc & RXRDY) == 0)
369             do_poll_adb();
370         buf_access();
371         *p++ = *sccd;
372     }
373     return i;
374 }
375
376 int
377 xmon_read_poll(void)
378 {
379         if ((*sccc & RXRDY) == 0) {
380                 do_poll_adb();
381                 return -1;
382         }
383         buf_access();
384         return *sccd;
385 }
386
387 static unsigned char scc_inittab[] = {
388     13, 0,              /* set baud rate divisor */
389     12, 1,
390     14, 1,              /* baud rate gen enable, src=rtxc */
391     11, 0x50,           /* clocks = br gen */
392     5,  0xea,           /* tx 8 bits, assert DTR & RTS */
393     4,  0x46,           /* x16 clock, 1 stop */
394     3,  0xc1,           /* rx enable, 8 bits */
395 };
396
397 void
398 xmon_init_scc(void)
399 {
400         if ( _machine == _MACH_chrp )
401         {
402                 sccd[3] = 0x83; eieio();        /* LCR = 8N1 + DLAB */
403                 sccd[0] = 12; eieio();          /* DLL = 9600 baud */
404                 sccd[1] = 0; eieio();
405                 sccd[2] = 0; eieio();           /* FCR = 0 */
406                 sccd[3] = 3; eieio();           /* LCR = 8N1 */
407                 sccd[1] = 0; eieio();           /* IER = 0 */
408         }
409         else if ( _machine == _MACH_Pmac )
410         {
411                 int i, x;
412
413                 if (channel_node != 0)
414                         pmac_call_feature(
415                                 PMAC_FTR_SCC_ENABLE,
416                                 channel_node,
417                                 PMAC_SCC_ASYNC | PMAC_SCC_FLAG_XMON, 1);
418                         printk(KERN_INFO "Serial port locked ON by debugger !\n");
419                 if (via_modem && channel_node != 0) {
420                         unsigned int t0;
421
422                         pmac_call_feature(
423                                 PMAC_FTR_MODEM_ENABLE,
424                                 channel_node, 0, 1);
425                         printk(KERN_INFO "Modem powered up by debugger !\n");
426                         t0 = readtb();
427                         while (readtb() - t0 < 3*TB_SPEED)
428                                 eieio();
429                 }
430                 /* use the B channel if requested */
431                 if (xmon_use_sccb) {
432                         sccc = (volatile unsigned char *)
433                                 ((unsigned long)sccc & ~0x20);
434                         sccd = sccc + 0x10;
435                 }
436                 for (i = 20000; i != 0; --i) {
437                         x = *sccc; eieio();
438                 }
439                 *sccc = 9; eieio();             /* reset A or B side */
440                 *sccc = ((unsigned long)sccc & 0x20)? 0x80: 0x40; eieio();
441                 for (i = 0; i < sizeof(scc_inittab); ++i) {
442                         *sccc = scc_inittab[i];
443                         eieio();
444                 }
445         }
446         scc_initialized = 1;
447         if (via_modem) {
448                 for (;;) {
449                         xmon_write(NULL, "ATE1V1\r", 7);
450                         if (xmon_expect("OK", 5)) {
451                                 xmon_write(NULL, "ATA\r", 4);
452                                 if (xmon_expect("CONNECT", 40))
453                                         break;
454                         }
455                         xmon_write(NULL, "+++", 3);
456                         xmon_expect("OK", 3);
457                 }
458         }
459 }
460
461 #if 0
462 extern int (*prom_entry)(void *);
463
464 int
465 xmon_exit(void)
466 {
467     struct prom_args {
468         char *service;
469     } args;
470
471     for (;;) {
472         args.service = "exit";
473         (*prom_entry)(&args);
474     }
475 }
476 #endif
477
478 void *xmon_stdin;
479 void *xmon_stdout;
480 void *xmon_stderr;
481
482 void
483 xmon_init(void)
484 {
485 }
486
487 int
488 xmon_putc(int c, void *f)
489 {
490     char ch = c;
491
492     if (c == '\n')
493         xmon_putc('\r', f);
494     return xmon_write(f, &ch, 1) == 1? c: -1;
495 }
496
497 int
498 xmon_putchar(int c)
499 {
500     return xmon_putc(c, xmon_stdout);
501 }
502
503 int
504 xmon_fputs(char *str, void *f)
505 {
506     int n = strlen(str);
507
508     return xmon_write(f, str, n) == n? 0: -1;
509 }
510
511 int
512 xmon_readchar(void)
513 {
514     char ch;
515
516     for (;;) {
517         switch (xmon_read(xmon_stdin, &ch, 1)) {
518         case 1:
519             return ch;
520         case -1:
521             xmon_printf("read(stdin) returned -1\r\n", 0, 0);
522             return -1;
523         }
524     }
525 }
526
527 static char line[256];
528 static char *lineptr;
529 static int lineleft;
530
531 int xmon_expect(const char *str, unsigned int timeout)
532 {
533         int c;
534         unsigned int t0;
535
536         timeout *= TB_SPEED;
537         t0 = readtb();
538         do {
539                 lineptr = line;
540                 for (;;) {
541                         c = xmon_read_poll();
542                         if (c == -1) {
543                                 if (readtb() - t0 > timeout)
544                                         return 0;
545                                 continue;
546                         }
547                         if (c == '\n')
548                                 break;
549                         if (c != '\r' && lineptr < &line[sizeof(line) - 1])
550                                 *lineptr++ = c;
551                 }
552                 *lineptr = 0;
553         } while (strstr(line, str) == NULL);
554         return 1;
555 }
556
557 int
558 xmon_getchar(void)
559 {
560     int c;
561
562     if (lineleft == 0) {
563         lineptr = line;
564         for (;;) {
565             c = xmon_readchar();
566             if (c == -1 || c == 4)
567                 break;
568             if (c == '\r' || c == '\n') {
569                 *lineptr++ = '\n';
570                 xmon_putchar('\n');
571                 break;
572             }
573             switch (c) {
574             case 0177:
575             case '\b':
576                 if (lineptr > line) {
577                     xmon_putchar('\b');
578                     xmon_putchar(' ');
579                     xmon_putchar('\b');
580                     --lineptr;
581                 }
582                 break;
583             case 'U' & 0x1F:
584                 while (lineptr > line) {
585                     xmon_putchar('\b');
586                     xmon_putchar(' ');
587                     xmon_putchar('\b');
588                     --lineptr;
589                 }
590                 break;
591             default:
592                 if (lineptr >= &line[sizeof(line) - 1])
593                     xmon_putchar('\a');
594                 else {
595                     xmon_putchar(c);
596                     *lineptr++ = c;
597                 }
598             }
599         }
600         lineleft = lineptr - line;
601         lineptr = line;
602     }
603     if (lineleft == 0)
604         return -1;
605     --lineleft;
606     return *lineptr++;
607 }
608
609 char *
610 xmon_fgets(char *str, int nb, void *f)
611 {
612     char *p;
613     int c;
614
615     for (p = str; p < str + nb - 1; ) {
616         c = xmon_getchar();
617         if (c == -1) {
618             if (p == str)
619                 return NULL;
620             break;
621         }
622         *p++ = c;
623         if (c == '\n')
624             break;
625     }
626     *p = 0;
627     return str;
628 }
629
630 void
631 xmon_enter(void)
632 {
633 #ifdef CONFIG_ADB_PMU
634         if (_machine == _MACH_Pmac) {
635                 pmu_suspend();
636         }
637 #endif
638 }
639
640 void
641 xmon_leave(void)
642 {
643 #ifdef CONFIG_ADB_PMU
644         if (_machine == _MACH_Pmac) {
645                 pmu_resume();
646         }
647 #endif
648 }