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/errno.h>
14 #include <asm/processor.h>
15 #include <asm/delay.h>
16 #include <asm/btext.h>
17 #include <asm/ibm4xx.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 #if defined(CONFIG_405GP)
61 sccd = (volatile unsigned char *)0xef600300;
62 #elif defined(CONFIG_440EP)
63 sccd = (volatile unsigned char *) ioremap(PPC440EP_UART0_ADDR, 8);
64 #elif defined(CONFIG_440SP)
65 sccd = (volatile unsigned char *) ioremap64(PPC440SP_UART0_ADDR, 8);
66 #elif defined(CONFIG_440SPE)
67 sccd = (volatile unsigned char *) ioremap64(PPC440SPE_UART0_ADDR, 8);
68 #elif defined(CONFIG_44x)
69 /* This is the default for 44x platforms. Any boards that have a
70 different UART address need to be put in cases before this or the
71 port will be mapped incorrectly */
72 sccd = (volatile unsigned char *) ioremap64(PPC440GP_UART0_ADDR, 8);
75 #ifndef CONFIG_PPC_PREP
82 register_sysrq_key('x', &sysrq_xmon_op);
85 static int scc_initialized;
87 void xmon_init_scc(void);
90 xmon_write(void *handle, void *ptr, int nb)
96 static unsigned long xmon_write_lock;
97 int lock_wait = 1000000;
100 while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
101 if (--lock_wait == 0)
105 if (!scc_initialized)
108 for (i = 0; i < nb; ++i) {
109 while ((*sccc & TXRDY) == 0)
112 if (c == '\n' && !ct) {
126 clear_bit(0, &xmon_write_lock);
135 xmon_read(void *handle, void *ptr, int nb)
140 if (!scc_initialized)
142 for (i = 0; i < nb; ++i) {
143 while ((*sccc & RXRDY) == 0)
154 if ((*sccc & RXRDY) == 0) {
168 xmon_write(NULL, "ATE1V1\r", 7);
169 if (xmon_expect("OK", 5)) {
170 xmon_write(NULL, "ATA\r", 4);
171 if (xmon_expect("CONNECT", 40))
174 xmon_write(NULL, "+++", 3);
175 xmon_expect("OK", 3);
192 xmon_putc(int c, void *f)
198 return xmon_write(f, &ch, 1) == 1? c: -1;
204 return xmon_putc(c, xmon_stdout);
208 xmon_fputs(char *str, void *f)
212 return xmon_write(f, str, n) == n? 0: -1;
221 switch (xmon_read(xmon_stdin, &ch, 1)) {
225 xmon_printf("read(stdin) returned -1\r\n", 0, 0);
231 static char line[256];
232 static char *lineptr;
235 int xmon_expect(const char *str, unsigned int timeout)
245 c = xmon_read_poll();
247 if (readtb() - t0 > timeout)
253 if (c != '\r' && lineptr < &line[sizeof(line) - 1])
257 } while (strstr(line, str) == NULL);
270 if (c == -1 || c == 4)
272 if (c == '\r' || c == '\n') {
280 if (lineptr > line) {
288 while (lineptr > line) {
296 if (lineptr >= &line[sizeof(line) - 1])
304 lineleft = lineptr - line;
314 xmon_fgets(char *str, int nb, void *f)
319 for (p = str; p < str + nb - 1; ) {