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);
149 extern void cuda_poll(void);
151 static inline void do_poll_adb(void)
153 #ifdef CONFIG_ADB_PMU
154 if (sys_ctrler == SYS_CTRLER_PMU)
156 #endif /* CONFIG_ADB_PMU */
157 #ifdef CONFIG_ADB_CUDA
158 if (sys_ctrler == SYS_CTRLER_CUDA)
160 #endif /* CONFIG_ADB_CUDA */
164 xmon_write(void *handle, void *ptr, int nb)
170 static unsigned long xmon_write_lock;
171 int lock_wait = 1000000;
174 while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
175 if (--lock_wait == 0)
179 #ifdef CONFIG_BOOTX_TEXT
181 /* write it on the screen */
182 for (i = 0; i < nb; ++i)
183 btext_drawchar(*p++);
187 if (!scc_initialized)
190 for (i = 0; i < nb; ++i) {
191 while ((*sccc & TXRDY) == 0)
194 if (c == '\n' && !ct) {
209 clear_bit(0, &xmon_write_lock);
215 int xmon_adb_keycode;
217 #ifdef CONFIG_BOOTX_TEXT
218 static int xmon_adb_shiftstate;
220 static unsigned char xmon_keytab[128] =
221 "asdfhgzxcv\000bqwer" /* 0x00 - 0x0f */
222 "yt123465=97-80]o" /* 0x10 - 0x1f */
223 "u[ip\rlj'k;\\,/nm." /* 0x20 - 0x2f */
224 "\t `\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
225 "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
226 "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
228 static unsigned char xmon_shift_keytab[128] =
229 "ASDFHGZXCV\000BQWER" /* 0x00 - 0x0f */
230 "YT!@#$^%+(&_*)}O" /* 0x10 - 0x1f */
231 "U{IP\rLJ\"K:|<?NM>" /* 0x20 - 0x2f */
232 "\t ~\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
233 "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
234 "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
237 xmon_get_adb_key(void)
243 xmon_adb_keycode = -1;
249 btext_drawchar(on? 0xdb: 0x20);
250 btext_drawchar('\b');
254 } while (xmon_adb_keycode == -1);
255 k = xmon_adb_keycode;
257 btext_drawstring(" \b");
259 /* test for shift keys */
260 if ((k & 0x7f) == 0x38 || (k & 0x7f) == 0x7b) {
261 xmon_adb_shiftstate = (k & 0x80) == 0;
265 continue; /* ignore up transitions */
266 k = (xmon_adb_shiftstate? xmon_shift_keytab: xmon_keytab)[k];
273 #endif /* CONFIG_BOOTX_TEXT */
276 xmon_read(void *handle, void *ptr, int nb)
281 #ifdef CONFIG_BOOTX_TEXT
283 for (i = 0; i < nb; ++i)
284 *p++ = xmon_get_adb_key();
288 if (!scc_initialized)
290 for (i = 0; i < nb; ++i) {
291 while ((*sccc & RXRDY) == 0)
302 if ((*sccc & RXRDY) == 0) {
313 if ( _machine == _MACH_chrp )
315 sccd[3] = 0x83; eieio(); /* LCR = 8N1 + DLAB */
316 sccd[0] = 12; eieio(); /* DLL = 9600 baud */
317 sccd[1] = 0; eieio();
318 sccd[2] = 0; eieio(); /* FCR = 0 */
319 sccd[3] = 3; eieio(); /* LCR = 8N1 */
320 sccd[1] = 0; eieio(); /* IER = 0 */
325 xmon_write(NULL, "ATE1V1\r", 7);
326 if (xmon_expect("OK", 5)) {
327 xmon_write(NULL, "ATA\r", 4);
328 if (xmon_expect("CONNECT", 40))
331 xmon_write(NULL, "+++", 3);
332 xmon_expect("OK", 3);
338 extern int (*prom_entry)(void *);
348 args.service = "exit";
349 (*prom_entry)(&args);
365 xmon_putc(int c, void *f)
371 return xmon_write(f, &ch, 1) == 1? c: -1;
377 return xmon_putc(c, xmon_stdout);
381 xmon_fputs(char *str, void *f)
385 return xmon_write(f, str, n) == n? 0: -1;
394 switch (xmon_read(xmon_stdin, &ch, 1)) {
398 xmon_printf("read(stdin) returned -1\r\n", 0, 0);
404 static char line[256];
405 static char *lineptr;
408 int xmon_expect(const char *str, unsigned int timeout)
418 c = xmon_read_poll();
420 if (readtb() - t0 > timeout)
426 if (c != '\r' && lineptr < &line[sizeof(line) - 1])
430 } while (strstr(line, str) == NULL);
443 if (c == -1 || c == 4)
445 if (c == '\r' || c == '\n') {
453 if (lineptr > line) {
461 while (lineptr > line) {
469 if (lineptr >= &line[sizeof(line) - 1])
477 lineleft = lineptr - line;
487 xmon_fgets(char *str, int nb, void *f)
492 for (p = str; p < str + nb - 1; ) {