start.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. extern void cuda_poll(void);
  132. static inline void do_poll_adb(void)
  133. {
  134. #ifdef CONFIG_ADB_PMU
  135. if (sys_ctrler == SYS_CTRLER_PMU)
  136. pmu_poll_adb();
  137. #endif /* CONFIG_ADB_PMU */
  138. #ifdef CONFIG_ADB_CUDA
  139. if (sys_ctrler == SYS_CTRLER_CUDA)
  140. cuda_poll();
  141. #endif /* CONFIG_ADB_CUDA */
  142. }
  143. int
  144. xmon_write(void *handle, void *ptr, int nb)
  145. {
  146. char *p = ptr;
  147. int i, c, ct;
  148. #ifdef CONFIG_SMP
  149. static unsigned long xmon_write_lock;
  150. int lock_wait = 1000000;
  151. int locked;
  152. while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
  153. if (--lock_wait == 0)
  154. break;
  155. #endif
  156. #ifdef CONFIG_BOOTX_TEXT
  157. if (use_screen) {
  158. /* write it on the screen */
  159. for (i = 0; i < nb; ++i)
  160. btext_drawchar(*p++);
  161. goto out;
  162. }
  163. #endif
  164. if (!scc_initialized)
  165. xmon_init_scc();
  166. ct = 0;
  167. for (i = 0; i < nb; ++i) {
  168. while ((*sccc & TXRDY) == 0)
  169. do_poll_adb();
  170. c = p[i];
  171. if (c == '\n' && !ct) {
  172. c = '\r';
  173. ct = 1;
  174. --i;
  175. } else {
  176. ct = 0;
  177. }
  178. buf_access();
  179. *sccd = c;
  180. eieio();
  181. }
  182. out:
  183. #ifdef CONFIG_SMP
  184. if (!locked)
  185. clear_bit(0, &xmon_write_lock);
  186. #endif
  187. return nb;
  188. }
  189. int xmon_wants_key;
  190. int xmon_adb_keycode;
  191. #ifdef CONFIG_BOOTX_TEXT
  192. static int xmon_adb_shiftstate;
  193. static unsigned char xmon_keytab[128] =
  194. "asdfhgzxcv\000bqwer" /* 0x00 - 0x0f */
  195. "yt123465=97-80]o" /* 0x10 - 0x1f */
  196. "u[ip\rlj'k;\\,/nm." /* 0x20 - 0x2f */
  197. "\t `\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
  198. "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
  199. "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
  200. static unsigned char xmon_shift_keytab[128] =
  201. "ASDFHGZXCV\000BQWER" /* 0x00 - 0x0f */
  202. "YT!@#$^%+(&_*)}O" /* 0x10 - 0x1f */
  203. "U{IP\rLJ\"K:|<?NM>" /* 0x20 - 0x2f */
  204. "\t ~\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
  205. "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
  206. "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
  207. static int
  208. xmon_get_adb_key(void)
  209. {
  210. int k, t, on;
  211. xmon_wants_key = 1;
  212. for (;;) {
  213. xmon_adb_keycode = -1;
  214. t = 0;
  215. on = 0;
  216. do {
  217. if (--t < 0) {
  218. on = 1 - on;
  219. btext_drawchar(on? 0xdb: 0x20);
  220. btext_drawchar('\b');
  221. t = 200000;
  222. }
  223. do_poll_adb();
  224. } while (xmon_adb_keycode == -1);
  225. k = xmon_adb_keycode;
  226. if (on)
  227. btext_drawstring(" \b");
  228. /* test for shift keys */
  229. if ((k & 0x7f) == 0x38 || (k & 0x7f) == 0x7b) {
  230. xmon_adb_shiftstate = (k & 0x80) == 0;
  231. continue;
  232. }
  233. if (k >= 0x80)
  234. continue; /* ignore up transitions */
  235. k = (xmon_adb_shiftstate? xmon_shift_keytab: xmon_keytab)[k];
  236. if (k != 0)
  237. break;
  238. }
  239. xmon_wants_key = 0;
  240. return k;
  241. }
  242. #endif /* CONFIG_BOOTX_TEXT */
  243. int
  244. xmon_read(void *handle, void *ptr, int nb)
  245. {
  246. char *p = ptr;
  247. int i;
  248. #ifdef CONFIG_BOOTX_TEXT
  249. if (use_screen) {
  250. for (i = 0; i < nb; ++i)
  251. *p++ = xmon_get_adb_key();
  252. return i;
  253. }
  254. #endif
  255. if (!scc_initialized)
  256. xmon_init_scc();
  257. for (i = 0; i < nb; ++i) {
  258. while ((*sccc & RXRDY) == 0)
  259. do_poll_adb();
  260. buf_access();
  261. *p++ = *sccd;
  262. }
  263. return i;
  264. }
  265. int
  266. xmon_read_poll(void)
  267. {
  268. if ((*sccc & RXRDY) == 0) {
  269. do_poll_adb();
  270. return -1;
  271. }
  272. buf_access();
  273. return *sccd;
  274. }
  275. void
  276. xmon_init_scc(void)
  277. {
  278. if ( _machine == _MACH_chrp )
  279. {
  280. sccd[3] = 0x83; eieio(); /* LCR = 8N1 + DLAB */
  281. sccd[0] = 12; eieio(); /* DLL = 9600 baud */
  282. sccd[1] = 0; eieio();
  283. sccd[2] = 0; eieio(); /* FCR = 0 */
  284. sccd[3] = 3; eieio(); /* LCR = 8N1 */
  285. sccd[1] = 0; eieio(); /* IER = 0 */
  286. }
  287. scc_initialized = 1;
  288. if (via_modem) {
  289. for (;;) {
  290. xmon_write(NULL, "ATE1V1\r", 7);
  291. if (xmon_expect("OK", 5)) {
  292. xmon_write(NULL, "ATA\r", 4);
  293. if (xmon_expect("CONNECT", 40))
  294. break;
  295. }
  296. xmon_write(NULL, "+++", 3);
  297. xmon_expect("OK", 3);
  298. }
  299. }
  300. }
  301. #if 0
  302. extern int (*prom_entry)(void *);
  303. int
  304. xmon_exit(void)
  305. {
  306. struct prom_args {
  307. char *service;
  308. } args;
  309. for (;;) {
  310. args.service = "exit";
  311. (*prom_entry)(&args);
  312. }
  313. }
  314. #endif
  315. void *xmon_stdin;
  316. void *xmon_stdout;
  317. void *xmon_stderr;
  318. void
  319. xmon_init(int arg)
  320. {
  321. xmon_map_scc();
  322. }
  323. int
  324. xmon_putc(int c, void *f)
  325. {
  326. char ch = c;
  327. if (c == '\n')
  328. xmon_putc('\r', f);
  329. return xmon_write(f, &ch, 1) == 1? c: -1;
  330. }
  331. int
  332. xmon_putchar(int c)
  333. {
  334. return xmon_putc(c, xmon_stdout);
  335. }
  336. int
  337. xmon_fputs(char *str, void *f)
  338. {
  339. int n = strlen(str);
  340. return xmon_write(f, str, n) == n? 0: -1;
  341. }
  342. int
  343. xmon_readchar(void)
  344. {
  345. char ch;
  346. for (;;) {
  347. switch (xmon_read(xmon_stdin, &ch, 1)) {
  348. case 1:
  349. return ch;
  350. case -1:
  351. xmon_printf("read(stdin) returned -1\r\n", 0, 0);
  352. return -1;
  353. }
  354. }
  355. }
  356. static char line[256];
  357. static char *lineptr;
  358. static int lineleft;
  359. int xmon_expect(const char *str, unsigned int timeout)
  360. {
  361. int c;
  362. unsigned int t0;
  363. timeout *= TB_SPEED;
  364. t0 = readtb();
  365. do {
  366. lineptr = line;
  367. for (;;) {
  368. c = xmon_read_poll();
  369. if (c == -1) {
  370. if (readtb() - t0 > timeout)
  371. return 0;
  372. continue;
  373. }
  374. if (c == '\n')
  375. break;
  376. if (c != '\r' && lineptr < &line[sizeof(line) - 1])
  377. *lineptr++ = c;
  378. }
  379. *lineptr = 0;
  380. } while (strstr(line, str) == NULL);
  381. return 1;
  382. }
  383. int
  384. xmon_getchar(void)
  385. {
  386. int c;
  387. if (lineleft == 0) {
  388. lineptr = line;
  389. for (;;) {
  390. c = xmon_readchar();
  391. if (c == -1 || c == 4)
  392. break;
  393. if (c == '\r' || c == '\n') {
  394. *lineptr++ = '\n';
  395. xmon_putchar('\n');
  396. break;
  397. }
  398. switch (c) {
  399. case 0177:
  400. case '\b':
  401. if (lineptr > line) {
  402. xmon_putchar('\b');
  403. xmon_putchar(' ');
  404. xmon_putchar('\b');
  405. --lineptr;
  406. }
  407. break;
  408. case 'U' & 0x1F:
  409. while (lineptr > line) {
  410. xmon_putchar('\b');
  411. xmon_putchar(' ');
  412. xmon_putchar('\b');
  413. --lineptr;
  414. }
  415. break;
  416. default:
  417. if (lineptr >= &line[sizeof(line) - 1])
  418. xmon_putchar('\a');
  419. else {
  420. xmon_putchar(c);
  421. *lineptr++ = c;
  422. }
  423. }
  424. }
  425. lineleft = lineptr - line;
  426. lineptr = line;
  427. }
  428. if (lineleft == 0)
  429. return -1;
  430. --lineleft;
  431. return *lineptr++;
  432. }
  433. char *
  434. xmon_fgets(char *str, int nb, void *f)
  435. {
  436. char *p;
  437. int c;
  438. for (p = str; p < str + nb - 1; ) {
  439. c = xmon_getchar();
  440. if (c == -1) {
  441. if (p == str)
  442. return NULL;
  443. break;
  444. }
  445. *p++ = c;
  446. if (c == '\n')
  447. break;
  448. }
  449. *p = 0;
  450. return str;
  451. }
  452. void
  453. xmon_enter(void)
  454. {
  455. }
  456. void
  457. xmon_leave(void)
  458. {
  459. }