2 * Copyright (C) 1996 Paul Mackerras.
4 #include <linux/config.h>
5 #include <linux/string.h>
6 #include <asm/machdep.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>
18 #include <asm/bootx.h>
19 #include <asm/machdep.h>
20 #include <asm/errno.h>
21 #include <asm/processor.h>
22 #include <asm/delay.h>
23 #include <asm/btext.h>
25 static volatile unsigned char *sccc, *sccd;
26 unsigned int TXRDY, RXRDY, DLAB;
27 static int xmon_expect(const char *str, unsigned int timeout);
29 static int use_screen;
31 static int xmon_use_sccb;
33 #define TB_SPEED 25000000
35 static inline unsigned int readtb(void)
39 asm volatile("mftb %0" : "=r" (ret) :);
46 sccd[3] &= ~DLAB; /* reset DLAB */
49 extern int adb_init(void);
51 #ifdef CONFIG_PPC_CHRP
53 * This looks in the "ranges" property for the primary PCI host bridge
54 * to find the physical address of the start of PCI/ISA I/O space.
55 * It is basically a cut-down version of pci_process_bridge_OF_ranges.
57 static unsigned long chrp_find_phys_io_base(void)
59 struct device_node *node;
61 unsigned long base = CHRP_ISA_IO_BASE;
65 node = find_devices("isa");
68 if (node == NULL || node->type == NULL
69 || strcmp(node->type, "pci") != 0)
73 node = find_devices("pci");
77 ranges = (unsigned int *) get_property(node, "ranges", &rlen);
78 np = prom_n_addr_cells(node) + 5;
79 while ((rlen -= np * sizeof(unsigned int)) >= 0) {
80 if ((ranges[0] >> 24) == 1 && ranges[2] == 0) {
81 /* I/O space starting at 0, grab the phys base */
82 base = ranges[np - 3];
89 #endif /* CONFIG_PPC_CHRP */
91 #ifdef CONFIG_MAGIC_SYSRQ
92 static void sysrq_handle_xmon(int key, struct pt_regs *regs,
93 struct tty_struct *tty)
98 static struct sysrq_key_op sysrq_xmon_op =
100 .handler = sysrq_handle_xmon,
102 .action_msg = "Entering xmon",
109 #ifdef CONFIG_PPC_MULTIPLATFORM
110 volatile unsigned char *base;
112 #ifdef CONFIG_PPC_CHRP
113 base = (volatile unsigned char *) isa_io_base;
114 if (_machine == _MACH_chrp)
115 base = (volatile unsigned char *)
116 ioremap(chrp_find_phys_io_base(), 0x1000);
127 #endif /* CONFIG_PPC_CHRP */
128 #elif defined(CONFIG_GEMINI)
129 /* should already be mapped by the kernel boot */
130 sccc = (volatile unsigned char *) 0xffeffb0d;
131 sccd = (volatile unsigned char *) 0xffeffb08;
135 #elif defined(CONFIG_405GP)
136 sccc = (volatile unsigned char *)0xef600305;
137 sccd = (volatile unsigned char *)0xef600300;
141 #endif /* platform */
143 register_sysrq_key('x', &sysrq_xmon_op);
146 static int scc_initialized = 0;
148 void xmon_init_scc(void);
151 xmon_write(void *handle, void *ptr, int nb)
157 static unsigned long xmon_write_lock;
158 int lock_wait = 1000000;
161 while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
162 if (--lock_wait == 0)
166 #ifdef CONFIG_BOOTX_TEXT
168 /* write it on the screen */
169 for (i = 0; i < nb; ++i)
170 btext_drawchar(*p++);
174 if (!scc_initialized)
177 for (i = 0; i < nb; ++i) {
178 while ((*sccc & TXRDY) == 0)
181 if (c == '\n' && !ct) {
196 clear_bit(0, &xmon_write_lock);
202 int xmon_adb_keycode;
204 #ifdef CONFIG_BOOTX_TEXT
205 static int xmon_adb_shiftstate;
207 static unsigned char xmon_keytab[128] =
208 "asdfhgzxcv\000bqwer" /* 0x00 - 0x0f */
209 "yt123465=97-80]o" /* 0x10 - 0x1f */
210 "u[ip\rlj'k;\\,/nm." /* 0x20 - 0x2f */
211 "\t `\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
212 "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
213 "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
215 static unsigned char xmon_shift_keytab[128] =
216 "ASDFHGZXCV\000BQWER" /* 0x00 - 0x0f */
217 "YT!@#$^%+(&_*)}O" /* 0x10 - 0x1f */
218 "U{IP\rLJ\"K:|<?NM>" /* 0x20 - 0x2f */
219 "\t ~\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
220 "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
221 "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
224 xmon_get_adb_key(void)
230 xmon_adb_keycode = -1;
236 btext_drawchar(on? 0xdb: 0x20);
237 btext_drawchar('\b');
241 } while (xmon_adb_keycode == -1);
242 k = xmon_adb_keycode;
244 btext_drawstring(" \b");
246 /* test for shift keys */
247 if ((k & 0x7f) == 0x38 || (k & 0x7f) == 0x7b) {
248 xmon_adb_shiftstate = (k & 0x80) == 0;
252 continue; /* ignore up transitions */
253 k = (xmon_adb_shiftstate? xmon_shift_keytab: xmon_keytab)[k];
260 #endif /* CONFIG_BOOTX_TEXT */
263 xmon_read(void *handle, void *ptr, int nb)
268 #ifdef CONFIG_BOOTX_TEXT
270 for (i = 0; i < nb; ++i)
271 *p++ = xmon_get_adb_key();
275 if (!scc_initialized)
277 for (i = 0; i < nb; ++i) {
278 while ((*sccc & RXRDY) == 0)
289 if ((*sccc & RXRDY) == 0) {
300 if ( _machine == _MACH_chrp )
302 sccd[3] = 0x83; eieio(); /* LCR = 8N1 + DLAB */
303 sccd[0] = 12; eieio(); /* DLL = 9600 baud */
304 sccd[1] = 0; eieio();
305 sccd[2] = 0; eieio(); /* FCR = 0 */
306 sccd[3] = 3; eieio(); /* LCR = 8N1 */
307 sccd[1] = 0; eieio(); /* IER = 0 */
312 xmon_write(NULL, "ATE1V1\r", 7);
313 if (xmon_expect("OK", 5)) {
314 xmon_write(NULL, "ATA\r", 4);
315 if (xmon_expect("CONNECT", 40))
318 xmon_write(NULL, "+++", 3);
319 xmon_expect("OK", 3);
325 extern int (*prom_entry)(void *);
335 args.service = "exit";
336 (*prom_entry)(&args);
352 xmon_putc(int c, void *f)
358 return xmon_write(f, &ch, 1) == 1? c: -1;
364 return xmon_putc(c, xmon_stdout);
368 xmon_fputs(char *str, void *f)
372 return xmon_write(f, str, n) == n? 0: -1;
381 switch (xmon_read(xmon_stdin, &ch, 1)) {
385 xmon_printf("read(stdin) returned -1\r\n", 0, 0);
391 static char line[256];
392 static char *lineptr;
395 int xmon_expect(const char *str, unsigned int timeout)
405 c = xmon_read_poll();
407 if (readtb() - t0 > timeout)
413 if (c != '\r' && lineptr < &line[sizeof(line) - 1])
417 } while (strstr(line, str) == NULL);
430 if (c == -1 || c == 4)
432 if (c == '\r' || c == '\n') {
440 if (lineptr > line) {
448 while (lineptr > line) {
456 if (lineptr >= &line[sizeof(line) - 1])
464 lineleft = lineptr - line;
474 xmon_fgets(char *str, int nb, void *f)
479 for (p = str; p < str + nb - 1; ) {