Merge git://git.infradead.org/~kmpark/onenand-mtd-2.6
[sfrench/cifs-2.6.git] / arch / ppc / xmon / start.c
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>
18
19 static volatile unsigned char *sccc, *sccd;
20 unsigned int TXRDY, RXRDY, DLAB;
21 static int xmon_expect(const char *str, unsigned int timeout);
22
23 static int via_modem;
24
25 #define TB_SPEED        25000000
26
27 static inline unsigned int readtb(void)
28 {
29         unsigned int ret;
30
31         asm volatile("mftb %0" : "=r" (ret) :);
32         return ret;
33 }
34
35 void buf_access(void)
36 {
37         if (DLAB)
38                 sccd[3] &= ~DLAB;       /* reset DLAB */
39 }
40
41
42 #ifdef CONFIG_MAGIC_SYSRQ
43 static void sysrq_handle_xmon(int key, struct pt_regs *regs,
44                               struct tty_struct *tty)
45 {
46         xmon(regs);
47 }
48
49 static struct sysrq_key_op sysrq_xmon_op =
50 {
51         .handler =      sysrq_handle_xmon,
52         .help_msg =     "Xmon",
53         .action_msg =   "Entering xmon",
54 };
55 #endif
56
57 void
58 xmon_map_scc(void)
59 {
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 */
74
75 #ifndef CONFIG_PPC_PREP
76         sccc = sccd + 5;
77         TXRDY = 0x20;
78         RXRDY = 1;
79         DLAB = 0x80;
80 #endif
81
82         register_sysrq_key('x', &sysrq_xmon_op);
83 }
84
85 static int scc_initialized;
86
87 void xmon_init_scc(void);
88
89 int
90 xmon_write(void *handle, void *ptr, int nb)
91 {
92         char *p = ptr;
93         int i, c, ct;
94
95 #ifdef CONFIG_SMP
96         static unsigned long xmon_write_lock;
97         int lock_wait = 1000000;
98         int locked;
99
100         while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
101                 if (--lock_wait == 0)
102                         break;
103 #endif
104
105         if (!scc_initialized)
106                 xmon_init_scc();
107         ct = 0;
108         for (i = 0; i < nb; ++i) {
109                 while ((*sccc & TXRDY) == 0)
110                         ;
111                 c = p[i];
112                 if (c == '\n' && !ct) {
113                         c = '\r';
114                         ct = 1;
115                         --i;
116                 } else {
117                         ct = 0;
118                 }
119                 buf_access();
120                 *sccd = c;
121                 eieio();
122         }
123
124 #ifdef CONFIG_SMP
125         if (!locked)
126                 clear_bit(0, &xmon_write_lock);
127 #endif
128         return nb;
129 }
130
131 int xmon_wants_key;
132
133
134 int
135 xmon_read(void *handle, void *ptr, int nb)
136 {
137     char *p = ptr;
138     int i;
139
140     if (!scc_initialized)
141         xmon_init_scc();
142     for (i = 0; i < nb; ++i) {
143         while ((*sccc & RXRDY) == 0)
144             ;
145         buf_access();
146         *p++ = *sccd;
147     }
148     return i;
149 }
150
151 int
152 xmon_read_poll(void)
153 {
154         if ((*sccc & RXRDY) == 0) {
155                 ;
156                 return -1;
157         }
158         buf_access();
159         return *sccd;
160 }
161
162 void
163 xmon_init_scc(void)
164 {
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;
173                         }
174                         xmon_write(NULL, "+++", 3);
175                         xmon_expect("OK", 3);
176                 }
177         }
178 }
179
180
181 void *xmon_stdin;
182 void *xmon_stdout;
183 void *xmon_stderr;
184
185 void
186 xmon_init(int arg)
187 {
188         xmon_map_scc();
189 }
190
191 int
192 xmon_putc(int c, void *f)
193 {
194     char ch = c;
195
196     if (c == '\n')
197         xmon_putc('\r', f);
198     return xmon_write(f, &ch, 1) == 1? c: -1;
199 }
200
201 int
202 xmon_putchar(int c)
203 {
204     return xmon_putc(c, xmon_stdout);
205 }
206
207 int
208 xmon_fputs(char *str, void *f)
209 {
210     int n = strlen(str);
211
212     return xmon_write(f, str, n) == n? 0: -1;
213 }
214
215 int
216 xmon_readchar(void)
217 {
218     char ch;
219
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;
227         }
228     }
229 }
230
231 static char line[256];
232 static char *lineptr;
233 static int lineleft;
234
235 int xmon_expect(const char *str, unsigned int timeout)
236 {
237         int c;
238         unsigned int t0;
239
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;
250                         }
251                         if (c == '\n')
252                                 break;
253                         if (c != '\r' && lineptr < &line[sizeof(line) - 1])
254                                 *lineptr++ = c;
255                 }
256                 *lineptr = 0;
257         } while (strstr(line, str) == NULL);
258         return 1;
259 }
260
261 int
262 xmon_getchar(void)
263 {
264     int c;
265
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;
276             }
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;
285                 }
286                 break;
287             case 'U' & 0x1F:
288                 while (lineptr > line) {
289                     xmon_putchar('\b');
290                     xmon_putchar(' ');
291                     xmon_putchar('\b');
292                     --lineptr;
293                 }
294                 break;
295             default:
296                 if (lineptr >= &line[sizeof(line) - 1])
297                     xmon_putchar('\a');
298                 else {
299                     xmon_putchar(c);
300                     *lineptr++ = c;
301                 }
302             }
303         }
304         lineleft = lineptr - line;
305         lineptr = line;
306     }
307     if (lineleft == 0)
308         return -1;
309     --lineleft;
310     return *lineptr++;
311 }
312
313 char *
314 xmon_fgets(char *str, int nb, void *f)
315 {
316     char *p;
317     int c;
318
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;
325         }
326         *p++ = c;
327         if (c == '\n')
328             break;
329     }
330     *p = 0;
331     return str;
332 }
333
334 void
335 xmon_enter(void)
336 {
337 }
338
339 void
340 xmon_leave(void)
341 {
342 }