Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / ppc / xmon / start.c
blob9056fe58aaa15d3dcadee93026064d8c75a5eda6
1 /*
2 * Copyright (C) 1996 Paul Mackerras.
3 */
4 #include <linux/string.h>
5 #include <asm/machdep.h>
6 #include <asm/io.h>
7 #include <asm/page.h>
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/sysrq.h>
11 #include <linux/bitops.h>
12 #include <asm/xmon.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);
23 static int via_modem;
25 #define TB_SPEED 25000000
27 static inline unsigned int readtb(void)
29 unsigned int ret;
31 asm volatile("mftb %0" : "=r" (ret) :);
32 return ret;
35 void buf_access(void)
37 if (DLAB)
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)
46 xmon(regs);
49 static struct sysrq_key_op sysrq_xmon_op =
51 .handler = sysrq_handle_xmon,
52 .help_msg = "Xmon",
53 .action_msg = "Entering xmon",
55 #endif
57 void
58 xmon_map_scc(void)
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);
73 #endif /* platform */
75 #ifndef CONFIG_PPC_PREP
76 sccc = sccd + 5;
77 TXRDY = 0x20;
78 RXRDY = 1;
79 DLAB = 0x80;
80 #endif
82 register_sysrq_key('x', &sysrq_xmon_op);
85 static int scc_initialized;
87 void xmon_init_scc(void);
89 int
90 xmon_write(void *handle, void *ptr, int nb)
92 char *p = ptr;
93 int i, c, ct;
95 #ifdef CONFIG_SMP
96 static unsigned long xmon_write_lock;
97 int lock_wait = 1000000;
98 int locked;
100 while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
101 if (--lock_wait == 0)
102 break;
103 #endif
105 if (!scc_initialized)
106 xmon_init_scc();
107 ct = 0;
108 for (i = 0; i < nb; ++i) {
109 while ((*sccc & TXRDY) == 0)
111 c = p[i];
112 if (c == '\n' && !ct) {
113 c = '\r';
114 ct = 1;
115 --i;
116 } else {
117 ct = 0;
119 buf_access();
120 *sccd = c;
121 eieio();
124 #ifdef CONFIG_SMP
125 if (!locked)
126 clear_bit(0, &xmon_write_lock);
127 #endif
128 return nb;
131 int xmon_wants_key;
135 xmon_read(void *handle, void *ptr, int nb)
137 char *p = ptr;
138 int i;
140 if (!scc_initialized)
141 xmon_init_scc();
142 for (i = 0; i < nb; ++i) {
143 while ((*sccc & RXRDY) == 0)
145 buf_access();
146 *p++ = *sccd;
148 return i;
152 xmon_read_poll(void)
154 if ((*sccc & RXRDY) == 0) {
156 return -1;
158 buf_access();
159 return *sccd;
162 void
163 xmon_init_scc(void)
165 scc_initialized = 1;
166 if (via_modem) {
167 for (;;) {
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))
172 break;
174 xmon_write(NULL, "+++", 3);
175 xmon_expect("OK", 3);
181 void *xmon_stdin;
182 void *xmon_stdout;
183 void *xmon_stderr;
185 void
186 xmon_init(int arg)
188 xmon_map_scc();
192 xmon_putc(int c, void *f)
194 char ch = c;
196 if (c == '\n')
197 xmon_putc('\r', f);
198 return xmon_write(f, &ch, 1) == 1? c: -1;
202 xmon_putchar(int c)
204 return xmon_putc(c, xmon_stdout);
208 xmon_fputs(char *str, void *f)
210 int n = strlen(str);
212 return xmon_write(f, str, n) == n? 0: -1;
216 xmon_readchar(void)
218 char ch;
220 for (;;) {
221 switch (xmon_read(xmon_stdin, &ch, 1)) {
222 case 1:
223 return ch;
224 case -1:
225 xmon_printf("read(stdin) returned -1\r\n", 0, 0);
226 return -1;
231 static char line[256];
232 static char *lineptr;
233 static int lineleft;
235 int xmon_expect(const char *str, unsigned int timeout)
237 int c;
238 unsigned int t0;
240 timeout *= TB_SPEED;
241 t0 = readtb();
242 do {
243 lineptr = line;
244 for (;;) {
245 c = xmon_read_poll();
246 if (c == -1) {
247 if (readtb() - t0 > timeout)
248 return 0;
249 continue;
251 if (c == '\n')
252 break;
253 if (c != '\r' && lineptr < &line[sizeof(line) - 1])
254 *lineptr++ = c;
256 *lineptr = 0;
257 } while (strstr(line, str) == NULL);
258 return 1;
262 xmon_getchar(void)
264 int c;
266 if (lineleft == 0) {
267 lineptr = line;
268 for (;;) {
269 c = xmon_readchar();
270 if (c == -1 || c == 4)
271 break;
272 if (c == '\r' || c == '\n') {
273 *lineptr++ = '\n';
274 xmon_putchar('\n');
275 break;
277 switch (c) {
278 case 0177:
279 case '\b':
280 if (lineptr > line) {
281 xmon_putchar('\b');
282 xmon_putchar(' ');
283 xmon_putchar('\b');
284 --lineptr;
286 break;
287 case 'U' & 0x1F:
288 while (lineptr > line) {
289 xmon_putchar('\b');
290 xmon_putchar(' ');
291 xmon_putchar('\b');
292 --lineptr;
294 break;
295 default:
296 if (lineptr >= &line[sizeof(line) - 1])
297 xmon_putchar('\a');
298 else {
299 xmon_putchar(c);
300 *lineptr++ = c;
304 lineleft = lineptr - line;
305 lineptr = line;
307 if (lineleft == 0)
308 return -1;
309 --lineleft;
310 return *lineptr++;
313 char *
314 xmon_fgets(char *str, int nb, void *f)
316 char *p;
317 int c;
319 for (p = str; p < str + nb - 1; ) {
320 c = xmon_getchar();
321 if (c == -1) {
322 if (p == str)
323 return NULL;
324 break;
326 *p++ = c;
327 if (c == '\n')
328 break;
330 *p = 0;
331 return str;
334 void
335 xmon_enter(void)
339 void
340 xmon_leave(void)