start.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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/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>
  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. static int via_modem;
  23. #define TB_SPEED 25000000
  24. static inline unsigned int readtb(void)
  25. {
  26. unsigned int ret;
  27. asm volatile("mftb %0" : "=r" (ret) :);
  28. return ret;
  29. }
  30. void buf_access(void)
  31. {
  32. if (DLAB)
  33. sccd[3] &= ~DLAB; /* reset DLAB */
  34. }
  35. #ifdef CONFIG_MAGIC_SYSRQ
  36. static void sysrq_handle_xmon(int key, struct pt_regs *regs,
  37. struct tty_struct *tty)
  38. {
  39. xmon(regs);
  40. }
  41. static struct sysrq_key_op sysrq_xmon_op =
  42. {
  43. .handler = sysrq_handle_xmon,
  44. .help_msg = "Xmon",
  45. .action_msg = "Entering xmon",
  46. };
  47. #endif
  48. void
  49. xmon_map_scc(void)
  50. {
  51. #if defined(CONFIG_405GP)
  52. sccd = (volatile unsigned char *)0xef600300;
  53. #elif defined(CONFIG_440EP)
  54. sccd = (volatile unsigned char *) ioremap(PPC440EP_UART0_ADDR, 8);
  55. #elif defined(CONFIG_440SP)
  56. sccd = (volatile unsigned char *) ioremap64(PPC440SP_UART0_ADDR, 8);
  57. #elif defined(CONFIG_440SPE)
  58. sccd = (volatile unsigned char *) ioremap64(PPC440SPE_UART0_ADDR, 8);
  59. #elif defined(CONFIG_44x)
  60. /* This is the default for 44x platforms. Any boards that have a
  61. different UART address need to be put in cases before this or the
  62. port will be mapped incorrectly */
  63. sccd = (volatile unsigned char *) ioremap64(PPC440GP_UART0_ADDR, 8);
  64. #endif /* platform */
  65. #ifndef CONFIG_PPC_PREP
  66. sccc = sccd + 5;
  67. TXRDY = 0x20;
  68. RXRDY = 1;
  69. DLAB = 0x80;
  70. #endif
  71. register_sysrq_key('x', &sysrq_xmon_op);
  72. }
  73. static int scc_initialized;
  74. void xmon_init_scc(void);
  75. int
  76. xmon_write(void *handle, void *ptr, int nb)
  77. {
  78. char *p = ptr;
  79. int i, c, ct;
  80. #ifdef CONFIG_SMP
  81. static unsigned long xmon_write_lock;
  82. int lock_wait = 1000000;
  83. int locked;
  84. while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
  85. if (--lock_wait == 0)
  86. break;
  87. #endif
  88. if (!scc_initialized)
  89. xmon_init_scc();
  90. ct = 0;
  91. for (i = 0; i < nb; ++i) {
  92. while ((*sccc & TXRDY) == 0)
  93. ;
  94. c = p[i];
  95. if (c == '\n' && !ct) {
  96. c = '\r';
  97. ct = 1;
  98. --i;
  99. } else {
  100. ct = 0;
  101. }
  102. buf_access();
  103. *sccd = c;
  104. eieio();
  105. }
  106. #ifdef CONFIG_SMP
  107. if (!locked)
  108. clear_bit(0, &xmon_write_lock);
  109. #endif
  110. return nb;
  111. }
  112. int xmon_wants_key;
  113. int
  114. xmon_read(void *handle, void *ptr, int nb)
  115. {
  116. char *p = ptr;
  117. int i;
  118. if (!scc_initialized)
  119. xmon_init_scc();
  120. for (i = 0; i < nb; ++i) {
  121. while ((*sccc & RXRDY) == 0)
  122. ;
  123. buf_access();
  124. *p++ = *sccd;
  125. }
  126. return i;
  127. }
  128. int
  129. xmon_read_poll(void)
  130. {
  131. if ((*sccc & RXRDY) == 0) {
  132. ;
  133. return -1;
  134. }
  135. buf_access();
  136. return *sccd;
  137. }
  138. void
  139. xmon_init_scc(void)
  140. {
  141. scc_initialized = 1;
  142. if (via_modem) {
  143. for (;;) {
  144. xmon_write(NULL, "ATE1V1\r", 7);
  145. if (xmon_expect("OK", 5)) {
  146. xmon_write(NULL, "ATA\r", 4);
  147. if (xmon_expect("CONNECT", 40))
  148. break;
  149. }
  150. xmon_write(NULL, "+++", 3);
  151. xmon_expect("OK", 3);
  152. }
  153. }
  154. }
  155. void *xmon_stdin;
  156. void *xmon_stdout;
  157. void *xmon_stderr;
  158. void
  159. xmon_init(int arg)
  160. {
  161. xmon_map_scc();
  162. }
  163. int
  164. xmon_putc(int c, void *f)
  165. {
  166. char ch = c;
  167. if (c == '\n')
  168. xmon_putc('\r', f);
  169. return xmon_write(f, &ch, 1) == 1? c: -1;
  170. }
  171. int
  172. xmon_putchar(int c)
  173. {
  174. return xmon_putc(c, xmon_stdout);
  175. }
  176. int
  177. xmon_fputs(char *str, void *f)
  178. {
  179. int n = strlen(str);
  180. return xmon_write(f, str, n) == n? 0: -1;
  181. }
  182. int
  183. xmon_readchar(void)
  184. {
  185. char ch;
  186. for (;;) {
  187. switch (xmon_read(xmon_stdin, &ch, 1)) {
  188. case 1:
  189. return ch;
  190. case -1:
  191. xmon_printf("read(stdin) returned -1\r\n", 0, 0);
  192. return -1;
  193. }
  194. }
  195. }
  196. static char line[256];
  197. static char *lineptr;
  198. static int lineleft;
  199. int xmon_expect(const char *str, unsigned int timeout)
  200. {
  201. int c;
  202. unsigned int t0;
  203. timeout *= TB_SPEED;
  204. t0 = readtb();
  205. do {
  206. lineptr = line;
  207. for (;;) {
  208. c = xmon_read_poll();
  209. if (c == -1) {
  210. if (readtb() - t0 > timeout)
  211. return 0;
  212. continue;
  213. }
  214. if (c == '\n')
  215. break;
  216. if (c != '\r' && lineptr < &line[sizeof(line) - 1])
  217. *lineptr++ = c;
  218. }
  219. *lineptr = 0;
  220. } while (strstr(line, str) == NULL);
  221. return 1;
  222. }
  223. int
  224. xmon_getchar(void)
  225. {
  226. int c;
  227. if (lineleft == 0) {
  228. lineptr = line;
  229. for (;;) {
  230. c = xmon_readchar();
  231. if (c == -1 || c == 4)
  232. break;
  233. if (c == '\r' || c == '\n') {
  234. *lineptr++ = '\n';
  235. xmon_putchar('\n');
  236. break;
  237. }
  238. switch (c) {
  239. case 0177:
  240. case '\b':
  241. if (lineptr > line) {
  242. xmon_putchar('\b');
  243. xmon_putchar(' ');
  244. xmon_putchar('\b');
  245. --lineptr;
  246. }
  247. break;
  248. case 'U' & 0x1F:
  249. while (lineptr > line) {
  250. xmon_putchar('\b');
  251. xmon_putchar(' ');
  252. xmon_putchar('\b');
  253. --lineptr;
  254. }
  255. break;
  256. default:
  257. if (lineptr >= &line[sizeof(line) - 1])
  258. xmon_putchar('\a');
  259. else {
  260. xmon_putchar(c);
  261. *lineptr++ = c;
  262. }
  263. }
  264. }
  265. lineleft = lineptr - line;
  266. lineptr = line;
  267. }
  268. if (lineleft == 0)
  269. return -1;
  270. --lineleft;
  271. return *lineptr++;
  272. }
  273. char *
  274. xmon_fgets(char *str, int nb, void *f)
  275. {
  276. char *p;
  277. int c;
  278. for (p = str; p < str + nb - 1; ) {
  279. c = xmon_getchar();
  280. if (c == -1) {
  281. if (p == str)
  282. return NULL;
  283. break;
  284. }
  285. *p++ = c;
  286. if (c == '\n')
  287. break;
  288. }
  289. *p = 0;
  290. return str;
  291. }
  292. void
  293. xmon_enter(void)
  294. {
  295. }
  296. void
  297. xmon_leave(void)
  298. {
  299. }