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>
18 #include <asm/ibm4xx.h>
20 static volatile unsigned char *sccc, *sccd;
21 unsigned int TXRDY, RXRDY, DLAB;
22 static int xmon_expect(const char *str, unsigned int timeout);
26 #define TB_SPEED 25000000
28 static inline unsigned int readtb(void)
32 asm volatile("mftb %0" : "=r" (ret) :);
39 sccd[3] &= ~DLAB; /* reset DLAB */
43 #ifdef CONFIG_MAGIC_SYSRQ
44 static void sysrq_handle_xmon(int key, struct pt_regs *regs,
45 struct tty_struct *tty)
50 static struct sysrq_key_op sysrq_xmon_op =
52 .handler = sysrq_handle_xmon,
54 .action_msg = "Entering xmon",
61 #if defined(CONFIG_GEMINI)
62 /* should already be mapped by the kernel boot */
63 sccd = (volatile unsigned char *) 0xffeffb08;
64 #elif defined(CONFIG_405GP)
65 sccd = (volatile unsigned char *)0xef600300;
66 #elif defined(CONFIG_440EP)
67 sccd = (volatile unsigned char *) ioremap(PPC440EP_UART0_ADDR, 8);
68 #elif defined(CONFIG_440SP)
69 sccd = (volatile unsigned char *) ioremap64(PPC440SP_UART0_ADDR, 8);
70 #elif defined(CONFIG_440SPE)
71 sccd = (volatile unsigned char *) ioremap64(PPC440SPE_UART0_ADDR, 8);
72 #elif defined(CONFIG_44x)
73 /* This is the default for 44x platforms. Any boards that have a
74 different UART address need to be put in cases before this or the
75 port will be mapped incorrectly */
76 sccd = (volatile unsigned char *) ioremap64(PPC440GP_UART0_ADDR, 8);
79 #ifndef CONFIG_PPC_PREP
86 register_sysrq_key('x', &sysrq_xmon_op);
89 static int scc_initialized;
91 void xmon_init_scc(void);
94 xmon_write(void *handle, void *ptr, int nb)
100 static unsigned long xmon_write_lock;
101 int lock_wait = 1000000;
104 while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
105 if (--lock_wait == 0)
109 if (!scc_initialized)
112 for (i = 0; i < nb; ++i) {
113 while ((*sccc & TXRDY) == 0)
116 if (c == '\n' && !ct) {
130 clear_bit(0, &xmon_write_lock);
139 xmon_read(void *handle, void *ptr, int nb)
144 if (!scc_initialized)
146 for (i = 0; i < nb; ++i) {
147 while ((*sccc & RXRDY) == 0)
158 if ((*sccc & RXRDY) == 0) {
172 xmon_write(NULL, "ATE1V1\r", 7);
173 if (xmon_expect("OK", 5)) {
174 xmon_write(NULL, "ATA\r", 4);
175 if (xmon_expect("CONNECT", 40))
178 xmon_write(NULL, "+++", 3);
179 xmon_expect("OK", 3);
196 xmon_putc(int c, void *f)
202 return xmon_write(f, &ch, 1) == 1? c: -1;
208 return xmon_putc(c, xmon_stdout);
212 xmon_fputs(char *str, void *f)
216 return xmon_write(f, str, n) == n? 0: -1;
225 switch (xmon_read(xmon_stdin, &ch, 1)) {
229 xmon_printf("read(stdin) returned -1\r\n", 0, 0);
235 static char line[256];
236 static char *lineptr;
239 int xmon_expect(const char *str, unsigned int timeout)
249 c = xmon_read_poll();
251 if (readtb() - t0 > timeout)
257 if (c != '\r' && lineptr < &line[sizeof(line) - 1])
261 } while (strstr(line, str) == NULL);
274 if (c == -1 || c == 4)
276 if (c == '\r' || c == '\n') {
284 if (lineptr > line) {
292 while (lineptr > line) {
300 if (lineptr >= &line[sizeof(line) - 1])
308 lineleft = lineptr - line;
318 xmon_fgets(char *str, int nb, void *f)
323 for (p = str; p < str + nb - 1; ) {