start.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright (C) 1996 Paul Mackerras.
  3. */
  4. #include <linux/config.h>
  5. #include <linux/string.h>
  6. #include <asm/machdep.h>
  7. #include <asm/io.h>
  8. #include <asm/page.h>
  9. #include <linux/adb.h>
  10. #include <linux/pmu.h>
  11. #include <linux/cuda.h>
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/sysrq.h>
  15. #include <linux/bitops.h>
  16. #include <asm/xmon.h>
  17. #include <asm/prom.h>
  18. #include <asm/bootx.h>
  19. #include <asm/machdep.h>
  20. #include <asm/errno.h>
  21. #include <asm/processor.h>
  22. #include <asm/delay.h>
  23. #include <asm/btext.h>
  24. static volatile unsigned char *sccc, *sccd;
  25. unsigned int TXRDY, RXRDY, DLAB;
  26. static int xmon_expect(const char *str, unsigned int timeout);
  27. static int use_screen;
  28. static int via_modem;
  29. static int xmon_use_sccb;
  30. #define TB_SPEED 25000000
  31. static inline unsigned int readtb(void)
  32. {
  33. unsigned int ret;
  34. asm volatile("mftb %0" : "=r" (ret) :);
  35. return ret;
  36. }
  37. void buf_access(void)
  38. {
  39. if (DLAB)
  40. sccd[3] &= ~DLAB; /* reset DLAB */
  41. }
  42. extern int adb_init(void);
  43. #ifdef CONFIG_PPC_CHRP
  44. /*
  45. * This looks in the "ranges" property for the primary PCI host bridge
  46. * to find the physical address of the start of PCI/ISA I/O space.
  47. * It is basically a cut-down version of pci_process_bridge_OF_ranges.
  48. */
  49. static unsigned long chrp_find_phys_io_base(void)
  50. {
  51. struct device_node *node;
  52. unsigned int *ranges;
  53. unsigned long base = CHRP_ISA_IO_BASE;
  54. int rlen = 0;
  55. int np;
  56. node = find_devices("isa");
  57. if (node != NULL) {
  58. node = node->parent;
  59. if (node == NULL || node->type == NULL
  60. || strcmp(node->type, "pci") != 0)
  61. node = NULL;
  62. }
  63. if (node == NULL)
  64. node = find_devices("pci");
  65. if (node == NULL)
  66. return base;
  67. ranges = (unsigned int *) get_property(node, "ranges", &rlen);
  68. np = prom_n_addr_cells(node) + 5;
  69. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  70. if ((ranges[0] >> 24) == 1 && ranges[2] == 0) {
  71. /* I/O space starting at 0, grab the phys base */
  72. base = ranges[np - 3];
  73. break;
  74. }
  75. ranges += np;
  76. }
  77. return base;
  78. }
  79. #endif /* CONFIG_PPC_CHRP */
  80. #ifdef CONFIG_MAGIC_SYSRQ
  81. static void sysrq_handle_xmon(int key, struct pt_regs *regs,
  82. struct tty_struct *tty)
  83. {
  84. xmon(regs);
  85. }
  86. static struct sysrq_key_op sysrq_xmon_op =
  87. {
  88. .handler = sysrq_handle_xmon,
  89. .help_msg = "Xmon",
  90. .action_msg = "Entering xmon",
  91. };
  92. #endif
  93. void
  94. xmon_map_scc(void)
  95. {
  96. #ifdef CONFIG_PPC_MULTIPLATFORM
  97. volatile unsigned char *base;
  98. #ifdef CONFIG_PPC_CHRP
  99. base = (volatile unsigned char *) isa_io_base;
  100. if (_machine == _MACH_chrp)
  101. base = (volatile unsigned char *)
  102. ioremap(chrp_find_phys_io_base(), 0x1000);
  103. sccc = base + 0x3fd;
  104. sccd = base + 0x3f8;
  105. if (xmon_use_sccb) {
  106. sccc -= 0x100;
  107. sccd -= 0x100;
  108. }
  109. TXRDY = 0x20;
  110. RXRDY = 1;
  111. DLAB = 0x80;
  112. #endif /* CONFIG_PPC_CHRP */
  113. #elif defined(CONFIG_GEMINI)
  114. /* should already be mapped by the kernel boot */
  115. sccc = (volatile unsigned char *) 0xffeffb0d;
  116. sccd = (volatile unsigned char *) 0xffeffb08;
  117. TXRDY = 0x20;
  118. RXRDY = 1;
  119. DLAB = 0x80;
  120. #elif defined(CONFIG_405GP)
  121. sccc = (volatile unsigned char *)0xef600305;
  122. sccd = (volatile unsigned char *)0xef600300;
  123. TXRDY = 0x20;
  124. RXRDY = 1;
  125. DLAB = 0x80;
  126. #endif /* platform */
  127. register_sysrq_key('x', &sysrq_xmon_op);
  128. }
  129. static int scc_initialized = 0;
  130. void xmon_init_scc(void);
  131. int
  132. xmon_write(void *handle, void *ptr, int nb)
  133. {
  134. char *p = ptr;
  135. int i, c, ct;
  136. #ifdef CONFIG_SMP
  137. static unsigned long xmon_write_lock;
  138. int lock_wait = 1000000;
  139. int locked;
  140. while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
  141. if (--lock_wait == 0)
  142. break;
  143. #endif
  144. #ifdef CONFIG_BOOTX_TEXT
  145. if (use_screen) {
  146. /* write it on the screen */
  147. for (i = 0; i < nb; ++i)
  148. btext_drawchar(*p++);
  149. goto out;
  150. }
  151. #endif
  152. if (!scc_initialized)
  153. xmon_init_scc();
  154. ct = 0;
  155. for (i = 0; i < nb; ++i) {
  156. while ((*sccc & TXRDY) == 0)
  157. ;
  158. c = p[i];
  159. if (c == '\n' && !ct) {
  160. c = '\r';
  161. ct = 1;
  162. --i;
  163. } else {
  164. ct = 0;
  165. }
  166. buf_access();
  167. *sccd = c;
  168. eieio();
  169. }
  170. out:
  171. #ifdef CONFIG_SMP
  172. if (!locked)
  173. clear_bit(0, &xmon_write_lock);
  174. #endif
  175. return nb;
  176. }
  177. int xmon_wants_key;
  178. int xmon_adb_keycode;
  179. #ifdef CONFIG_BOOTX_TEXT
  180. static int xmon_adb_shiftstate;
  181. static unsigned char xmon_keytab[128] =
  182. "asdfhgzxcv\000bqwer" /* 0x00 - 0x0f */
  183. "yt123465=97-80]o" /* 0x10 - 0x1f */
  184. "u[ip\rlj'k;\\,/nm." /* 0x20 - 0x2f */
  185. "\t `\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
  186. "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
  187. "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
  188. static unsigned char xmon_shift_keytab[128] =
  189. "ASDFHGZXCV\000BQWER" /* 0x00 - 0x0f */
  190. "YT!@#$^%+(&_*)}O" /* 0x10 - 0x1f */
  191. "U{IP\rLJ\"K:|<?NM>" /* 0x20 - 0x2f */
  192. "\t ~\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
  193. "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
  194. "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
  195. static int
  196. xmon_get_adb_key(void)
  197. {
  198. int k, t, on;
  199. xmon_wants_key = 1;
  200. for (;;) {
  201. xmon_adb_keycode = -1;
  202. t = 0;
  203. on = 0;
  204. do {
  205. if (--t < 0) {
  206. on = 1 - on;
  207. btext_drawchar(on? 0xdb: 0x20);
  208. btext_drawchar('\b');
  209. t = 200000;
  210. }
  211. do_poll_adb();
  212. } while (xmon_adb_keycode == -1);
  213. k = xmon_adb_keycode;
  214. if (on)
  215. btext_drawstring(" \b");
  216. /* test for shift keys */
  217. if ((k & 0x7f) == 0x38 || (k & 0x7f) == 0x7b) {
  218. xmon_adb_shiftstate = (k & 0x80) == 0;
  219. continue;
  220. }
  221. if (k >= 0x80)
  222. continue; /* ignore up transitions */
  223. k = (xmon_adb_shiftstate? xmon_shift_keytab: xmon_keytab)[k];
  224. if (k != 0)
  225. break;
  226. }
  227. xmon_wants_key = 0;
  228. return k;
  229. }
  230. #endif /* CONFIG_BOOTX_TEXT */
  231. int
  232. xmon_read(void *handle, void *ptr, int nb)
  233. {
  234. char *p = ptr;
  235. int i;
  236. #ifdef CONFIG_BOOTX_TEXT
  237. if (use_screen) {
  238. for (i = 0; i < nb; ++i)
  239. *p++ = xmon_get_adb_key();
  240. return i;
  241. }
  242. #endif
  243. if (!scc_initialized)
  244. xmon_init_scc();
  245. for (i = 0; i < nb; ++i) {
  246. while ((*sccc & RXRDY) == 0)
  247. do_poll_adb();
  248. buf_access();
  249. *p++ = *sccd;
  250. }
  251. return i;
  252. }
  253. int
  254. xmon_read_poll(void)
  255. {
  256. if ((*sccc & RXRDY) == 0) {
  257. do_poll_adb();
  258. return -1;
  259. }
  260. buf_access();
  261. return *sccd;
  262. }
  263. void
  264. xmon_init_scc(void)
  265. {
  266. if ( _machine == _MACH_chrp )
  267. {
  268. sccd[3] = 0x83; eieio(); /* LCR = 8N1 + DLAB */
  269. sccd[0] = 12; eieio(); /* DLL = 9600 baud */
  270. sccd[1] = 0; eieio();
  271. sccd[2] = 0; eieio(); /* FCR = 0 */
  272. sccd[3] = 3; eieio(); /* LCR = 8N1 */
  273. sccd[1] = 0; eieio(); /* IER = 0 */
  274. }
  275. scc_initialized = 1;
  276. if (via_modem) {
  277. for (;;) {
  278. xmon_write(NULL, "ATE1V1\r", 7);
  279. if (xmon_expect("OK", 5)) {
  280. xmon_write(NULL, "ATA\r", 4);
  281. if (xmon_expect("CONNECT", 40))
  282. break;
  283. }
  284. xmon_write(NULL, "+++", 3);
  285. xmon_expect("OK", 3);
  286. }
  287. }
  288. }
  289. #if 0
  290. extern int (*prom_entry)(void *);
  291. int
  292. xmon_exit(void)
  293. {
  294. struct prom_args {
  295. char *service;
  296. } args;
  297. for (;;) {
  298. args.service = "exit";
  299. (*prom_entry)(&args);
  300. }
  301. }
  302. #endif
  303. void *xmon_stdin;
  304. void *xmon_stdout;
  305. void *xmon_stderr;
  306. void
  307. xmon_init(int arg)
  308. {
  309. xmon_map_scc();
  310. }
  311. int
  312. xmon_putc(int c, void *f)
  313. {
  314. char ch = c;
  315. if (c == '\n')
  316. xmon_putc('\r', f);
  317. return xmon_write(f, &ch, 1) == 1? c: -1;
  318. }
  319. int
  320. xmon_putchar(int c)
  321. {
  322. return xmon_putc(c, xmon_stdout);
  323. }
  324. int
  325. xmon_fputs(char *str, void *f)
  326. {
  327. int n = strlen(str);
  328. return xmon_write(f, str, n) == n? 0: -1;
  329. }
  330. int
  331. xmon_readchar(void)
  332. {
  333. char ch;
  334. for (;;) {
  335. switch (xmon_read(xmon_stdin, &ch, 1)) {
  336. case 1:
  337. return ch;
  338. case -1:
  339. xmon_printf("read(stdin) returned -1\r\n", 0, 0);
  340. return -1;
  341. }
  342. }
  343. }
  344. static char line[256];
  345. static char *lineptr;
  346. static int lineleft;
  347. int xmon_expect(const char *str, unsigned int timeout)
  348. {
  349. int c;
  350. unsigned int t0;
  351. timeout *= TB_SPEED;
  352. t0 = readtb();
  353. do {
  354. lineptr = line;
  355. for (;;) {
  356. c = xmon_read_poll();
  357. if (c == -1) {
  358. if (readtb() - t0 > timeout)
  359. return 0;
  360. continue;
  361. }
  362. if (c == '\n')
  363. break;
  364. if (c != '\r' && lineptr < &line[sizeof(line) - 1])
  365. *lineptr++ = c;
  366. }
  367. *lineptr = 0;
  368. } while (strstr(line, str) == NULL);
  369. return 1;
  370. }
  371. int
  372. xmon_getchar(void)
  373. {
  374. int c;
  375. if (lineleft == 0) {
  376. lineptr = line;
  377. for (;;) {
  378. c = xmon_readchar();
  379. if (c == -1 || c == 4)
  380. break;
  381. if (c == '\r' || c == '\n') {
  382. *lineptr++ = '\n';
  383. xmon_putchar('\n');
  384. break;
  385. }
  386. switch (c) {
  387. case 0177:
  388. case '\b':
  389. if (lineptr > line) {
  390. xmon_putchar('\b');
  391. xmon_putchar(' ');
  392. xmon_putchar('\b');
  393. --lineptr;
  394. }
  395. break;
  396. case 'U' & 0x1F:
  397. while (lineptr > line) {
  398. xmon_putchar('\b');
  399. xmon_putchar(' ');
  400. xmon_putchar('\b');
  401. --lineptr;
  402. }
  403. break;
  404. default:
  405. if (lineptr >= &line[sizeof(line) - 1])
  406. xmon_putchar('\a');
  407. else {
  408. xmon_putchar(c);
  409. *lineptr++ = c;
  410. }
  411. }
  412. }
  413. lineleft = lineptr - line;
  414. lineptr = line;
  415. }
  416. if (lineleft == 0)
  417. return -1;
  418. --lineleft;
  419. return *lineptr++;
  420. }
  421. char *
  422. xmon_fgets(char *str, int nb, void *f)
  423. {
  424. char *p;
  425. int c;
  426. for (p = str; p < str + nb - 1; ) {
  427. c = xmon_getchar();
  428. if (c == -1) {
  429. if (p == str)
  430. return NULL;
  431. break;
  432. }
  433. *p++ = c;
  434. if (c == '\n')
  435. break;
  436. }
  437. *p = 0;
  438. return str;
  439. }
  440. void
  441. xmon_enter(void)
  442. {
  443. }
  444. void
  445. xmon_leave(void)
  446. {
  447. }