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_405GP)
62 sccd
= (volatile unsigned char *)0xef600300;
63 #elif defined(CONFIG_440EP)
64 sccd
= (volatile unsigned char *) ioremap(PPC440EP_UART0_ADDR
, 8);
65 #elif defined(CONFIG_440SP)
66 sccd
= (volatile unsigned char *) ioremap64(PPC440SP_UART0_ADDR
, 8);
67 #elif defined(CONFIG_440SPE)
68 sccd
= (volatile unsigned char *) ioremap64(PPC440SPE_UART0_ADDR
, 8);
69 #elif defined(CONFIG_44x)
70 /* This is the default for 44x platforms. Any boards that have a
71 different UART address need to be put in cases before this or the
72 port will be mapped incorrectly */
73 sccd
= (volatile unsigned char *) ioremap64(PPC440GP_UART0_ADDR
, 8);
76 #ifndef CONFIG_PPC_PREP
83 register_sysrq_key('x', &sysrq_xmon_op
);
86 static int scc_initialized
;
88 void xmon_init_scc(void);
91 xmon_write(void *handle
, void *ptr
, int nb
)
97 static unsigned long xmon_write_lock
;
98 int lock_wait
= 1000000;
101 while ((locked
= test_and_set_bit(0, &xmon_write_lock
)) != 0)
102 if (--lock_wait
== 0)
106 if (!scc_initialized
)
109 for (i
= 0; i
< nb
; ++i
) {
110 while ((*sccc
& TXRDY
) == 0)
113 if (c
== '\n' && !ct
) {
127 clear_bit(0, &xmon_write_lock
);
136 xmon_read(void *handle
, void *ptr
, int nb
)
141 if (!scc_initialized
)
143 for (i
= 0; i
< nb
; ++i
) {
144 while ((*sccc
& RXRDY
) == 0)
155 if ((*sccc
& RXRDY
) == 0) {
169 xmon_write(NULL
, "ATE1V1\r", 7);
170 if (xmon_expect("OK", 5)) {
171 xmon_write(NULL
, "ATA\r", 4);
172 if (xmon_expect("CONNECT", 40))
175 xmon_write(NULL
, "+++", 3);
176 xmon_expect("OK", 3);
193 xmon_putc(int c
, void *f
)
199 return xmon_write(f
, &ch
, 1) == 1? c
: -1;
205 return xmon_putc(c
, xmon_stdout
);
209 xmon_fputs(char *str
, void *f
)
213 return xmon_write(f
, str
, n
) == n
? 0: -1;
222 switch (xmon_read(xmon_stdin
, &ch
, 1)) {
226 xmon_printf("read(stdin) returned -1\r\n", 0, 0);
232 static char line
[256];
233 static char *lineptr
;
236 int xmon_expect(const char *str
, unsigned int timeout
)
246 c
= xmon_read_poll();
248 if (readtb() - t0
> timeout
)
254 if (c
!= '\r' && lineptr
< &line
[sizeof(line
) - 1])
258 } while (strstr(line
, str
) == NULL
);
271 if (c
== -1 || c
== 4)
273 if (c
== '\r' || c
== '\n') {
281 if (lineptr
> line
) {
289 while (lineptr
> line
) {
297 if (lineptr
>= &line
[sizeof(line
) - 1])
305 lineleft
= lineptr
- line
;
315 xmon_fgets(char *str
, int nb
, void *f
)
320 for (p
= str
; p
< str
+ nb
- 1; ) {