2 * Copyright (C) 1996 Paul Mackerras.
4 #include <linux/string.h>
5 #include <asm/machdep.h>
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/sysrq.h>
11 #include <linux/bitops.h>
13 #include <asm/machdep.h>
14 #include <asm/errno.h>
15 #include <asm/processor.h>
16 #include <asm/delay.h>
17 #include <asm/btext.h>
19 static volatile unsigned char *sccc, *sccd;
20 unsigned int TXRDY, RXRDY, DLAB;
21 static int xmon_expect(const char *str, unsigned int timeout);
25 #define TB_SPEED 25000000
27 static inline unsigned int readtb(void)
31 asm volatile("mftb %0" : "=r" (ret) :);
38 sccd[3] &= ~DLAB; /* reset DLAB */
42 #ifdef CONFIG_MAGIC_SYSRQ
43 static void sysrq_handle_xmon(int key, struct pt_regs *regs,
44 struct tty_struct *tty)
49 static struct sysrq_key_op sysrq_xmon_op =
51 .handler = sysrq_handle_xmon,
53 .action_msg = "Entering xmon",
60 #ifdef CONFIG_PPC_PREP
61 volatile unsigned char *base;
63 #elif defined(CONFIG_GEMINI)
64 /* should already be mapped by the kernel boot */
65 sccc = (volatile unsigned char *) 0xffeffb0d;
66 sccd = (volatile unsigned char *) 0xffeffb08;
70 #elif defined(CONFIG_405GP)
71 sccc = (volatile unsigned char *)0xef600305;
72 sccd = (volatile unsigned char *)0xef600300;
78 register_sysrq_key('x', &sysrq_xmon_op);
81 static int scc_initialized;
83 void xmon_init_scc(void);
86 xmon_write(void *handle, void *ptr, int nb)
92 static unsigned long xmon_write_lock;
93 int lock_wait = 1000000;
96 while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
101 if (!scc_initialized)
104 for (i = 0; i < nb; ++i) {
105 while ((*sccc & TXRDY) == 0)
108 if (c == '\n' && !ct) {
122 clear_bit(0, &xmon_write_lock);
131 xmon_read(void *handle, void *ptr, int nb)
136 if (!scc_initialized)
138 for (i = 0; i < nb; ++i) {
139 while ((*sccc & RXRDY) == 0)
150 if ((*sccc & RXRDY) == 0) {
164 xmon_write(NULL, "ATE1V1\r", 7);
165 if (xmon_expect("OK", 5)) {
166 xmon_write(NULL, "ATA\r", 4);
167 if (xmon_expect("CONNECT", 40))
170 xmon_write(NULL, "+++", 3);
171 xmon_expect("OK", 3);
188 xmon_putc(int c, void *f)
194 return xmon_write(f, &ch, 1) == 1? c: -1;
200 return xmon_putc(c, xmon_stdout);
204 xmon_fputs(char *str, void *f)
208 return xmon_write(f, str, n) == n? 0: -1;
217 switch (xmon_read(xmon_stdin, &ch, 1)) {
221 xmon_printf("read(stdin) returned -1\r\n", 0, 0);
227 static char line[256];
228 static char *lineptr;
231 int xmon_expect(const char *str, unsigned int timeout)
241 c = xmon_read_poll();
243 if (readtb() - t0 > timeout)
249 if (c != '\r' && lineptr < &line[sizeof(line) - 1])
253 } while (strstr(line, str) == NULL);
266 if (c == -1 || c == 4)
268 if (c == '\r' || c == '\n') {
276 if (lineptr > line) {
284 while (lineptr > line) {
292 if (lineptr >= &line[sizeof(line) - 1])
300 lineleft = lineptr - line;
310 xmon_fgets(char *str, int nb, void *f)
315 for (p = str; p < str + nb - 1; ) {