blob: d74a883e5bdeab5b82e1069e6cae3fe038c1472d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 1996 Paul Mackerras.
3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/string.h>
5#include <asm/machdep.h>
6#include <asm/io.h>
7#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/kernel.h>
9#include <linux/errno.h>
10#include <linux/sysrq.h>
11#include <linux/bitops.h>
12#include <asm/xmon.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/machdep.h>
14#include <asm/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/processor.h>
16#include <asm/delay.h>
17#include <asm/btext.h>
Josh Boyerb7e89212006-09-07 13:27:58 -050018#include <asm/ibm4xx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20static volatile unsigned char *sccc, *sccd;
21unsigned int TXRDY, RXRDY, DLAB;
22static int xmon_expect(const char *str, unsigned int timeout);
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static int via_modem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define TB_SPEED 25000000
27
28static inline unsigned int readtb(void)
29{
30 unsigned int ret;
31
32 asm volatile("mftb %0" : "=r" (ret) :);
33 return ret;
34}
35
36void buf_access(void)
37{
38 if (DLAB)
39 sccd[3] &= ~DLAB; /* reset DLAB */
40}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#ifdef CONFIG_MAGIC_SYSRQ
44static void sysrq_handle_xmon(int key, struct pt_regs *regs,
45 struct tty_struct *tty)
46{
47 xmon(regs);
48}
49
50static struct sysrq_key_op sysrq_xmon_op =
51{
52 .handler = sysrq_handle_xmon,
53 .help_msg = "Xmon",
54 .action_msg = "Entering xmon",
55};
56#endif
57
58void
59xmon_map_scc(void)
60{
Josh Boyerb7e89212006-09-07 13:27:58 -050061#if defined(CONFIG_GEMINI)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* should already be mapped by the kernel boot */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 sccd = (volatile unsigned char *) 0xffeffb08;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#elif defined(CONFIG_405GP)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 sccd = (volatile unsigned char *)0xef600300;
Josh Boyerb7e89212006-09-07 13:27:58 -050066#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);
77#endif /* platform */
78
79#ifndef CONFIG_PPC_PREP
80 sccc = sccd + 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 TXRDY = 0x20;
82 RXRDY = 1;
83 DLAB = 0x80;
Josh Boyerb7e89212006-09-07 13:27:58 -050084#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 register_sysrq_key('x', &sysrq_xmon_op);
87}
88
Olaf Heringc57914a2006-02-21 21:06:41 +010089static int scc_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91void xmon_init_scc(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93int
94xmon_write(void *handle, void *ptr, int nb)
95{
96 char *p = ptr;
97 int i, c, ct;
98
99#ifdef CONFIG_SMP
100 static unsigned long xmon_write_lock;
101 int lock_wait = 1000000;
102 int locked;
103
104 while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
105 if (--lock_wait == 0)
106 break;
107#endif
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (!scc_initialized)
110 xmon_init_scc();
111 ct = 0;
112 for (i = 0; i < nb; ++i) {
113 while ((*sccc & TXRDY) == 0)
Olaf Hering0728a2f2006-02-11 18:21:47 +0100114 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 c = p[i];
116 if (c == '\n' && !ct) {
117 c = '\r';
118 ct = 1;
119 --i;
120 } else {
121 ct = 0;
122 }
123 buf_access();
124 *sccd = c;
125 eieio();
126 }
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#ifdef CONFIG_SMP
129 if (!locked)
130 clear_bit(0, &xmon_write_lock);
131#endif
132 return nb;
133}
134
135int xmon_wants_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138int
139xmon_read(void *handle, void *ptr, int nb)
140{
141 char *p = ptr;
142 int i;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!scc_initialized)
145 xmon_init_scc();
146 for (i = 0; i < nb; ++i) {
147 while ((*sccc & RXRDY) == 0)
Olaf Heringc57914a2006-02-21 21:06:41 +0100148 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 buf_access();
150 *p++ = *sccd;
151 }
152 return i;
153}
154
155int
156xmon_read_poll(void)
157{
158 if ((*sccc & RXRDY) == 0) {
Olaf Heringc57914a2006-02-21 21:06:41 +0100159 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -1;
161 }
162 buf_access();
163 return *sccd;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166void
167xmon_init_scc(void)
168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 scc_initialized = 1;
170 if (via_modem) {
171 for (;;) {
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))
176 break;
177 }
178 xmon_write(NULL, "+++", 3);
179 xmon_expect("OK", 3);
180 }
181 }
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185void *xmon_stdin;
186void *xmon_stdout;
187void *xmon_stderr;
188
189void
Paul Mackerrasfd582ec2005-10-11 22:08:12 +1000190xmon_init(int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Paul Mackerrasfd582ec2005-10-11 22:08:12 +1000192 xmon_map_scc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195int
196xmon_putc(int c, void *f)
197{
198 char ch = c;
199
200 if (c == '\n')
201 xmon_putc('\r', f);
202 return xmon_write(f, &ch, 1) == 1? c: -1;
203}
204
205int
206xmon_putchar(int c)
207{
208 return xmon_putc(c, xmon_stdout);
209}
210
211int
212xmon_fputs(char *str, void *f)
213{
214 int n = strlen(str);
215
216 return xmon_write(f, str, n) == n? 0: -1;
217}
218
219int
220xmon_readchar(void)
221{
222 char ch;
223
224 for (;;) {
225 switch (xmon_read(xmon_stdin, &ch, 1)) {
226 case 1:
227 return ch;
228 case -1:
229 xmon_printf("read(stdin) returned -1\r\n", 0, 0);
230 return -1;
231 }
232 }
233}
234
235static char line[256];
236static char *lineptr;
237static int lineleft;
238
239int xmon_expect(const char *str, unsigned int timeout)
240{
241 int c;
242 unsigned int t0;
243
244 timeout *= TB_SPEED;
245 t0 = readtb();
246 do {
247 lineptr = line;
248 for (;;) {
249 c = xmon_read_poll();
250 if (c == -1) {
251 if (readtb() - t0 > timeout)
252 return 0;
253 continue;
254 }
255 if (c == '\n')
256 break;
257 if (c != '\r' && lineptr < &line[sizeof(line) - 1])
258 *lineptr++ = c;
259 }
260 *lineptr = 0;
261 } while (strstr(line, str) == NULL);
262 return 1;
263}
264
265int
266xmon_getchar(void)
267{
268 int c;
269
270 if (lineleft == 0) {
271 lineptr = line;
272 for (;;) {
273 c = xmon_readchar();
274 if (c == -1 || c == 4)
275 break;
276 if (c == '\r' || c == '\n') {
277 *lineptr++ = '\n';
278 xmon_putchar('\n');
279 break;
280 }
281 switch (c) {
282 case 0177:
283 case '\b':
284 if (lineptr > line) {
285 xmon_putchar('\b');
286 xmon_putchar(' ');
287 xmon_putchar('\b');
288 --lineptr;
289 }
290 break;
291 case 'U' & 0x1F:
292 while (lineptr > line) {
293 xmon_putchar('\b');
294 xmon_putchar(' ');
295 xmon_putchar('\b');
296 --lineptr;
297 }
298 break;
299 default:
300 if (lineptr >= &line[sizeof(line) - 1])
301 xmon_putchar('\a');
302 else {
303 xmon_putchar(c);
304 *lineptr++ = c;
305 }
306 }
307 }
308 lineleft = lineptr - line;
309 lineptr = line;
310 }
311 if (lineleft == 0)
312 return -1;
313 --lineleft;
314 return *lineptr++;
315}
316
317char *
318xmon_fgets(char *str, int nb, void *f)
319{
320 char *p;
321 int c;
322
323 for (p = str; p < str + nb - 1; ) {
324 c = xmon_getchar();
325 if (c == -1) {
326 if (p == str)
327 return NULL;
328 break;
329 }
330 *p++ = c;
331 if (c == '\n')
332 break;
333 }
334 *p = 0;
335 return str;
336}
337
338void
339xmon_enter(void)
340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343void
344xmon_leave(void)
345{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}