start.c 5.6 KB

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