start.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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/pmac_feature.h>
  22. #include <asm/processor.h>
  23. #include <asm/delay.h>
  24. #include <asm/btext.h>
  25. static volatile unsigned char *sccc, *sccd;
  26. unsigned int TXRDY, RXRDY, DLAB;
  27. static int xmon_expect(const char *str, unsigned int timeout);
  28. static int use_serial;
  29. static int use_screen;
  30. static int via_modem;
  31. static int xmon_use_sccb;
  32. static struct device_node *channel_node;
  33. #define TB_SPEED 25000000
  34. static inline unsigned int readtb(void)
  35. {
  36. unsigned int ret;
  37. asm volatile("mftb %0" : "=r" (ret) :);
  38. return ret;
  39. }
  40. void buf_access(void)
  41. {
  42. if (DLAB)
  43. sccd[3] &= ~DLAB; /* reset DLAB */
  44. }
  45. extern int adb_init(void);
  46. #ifdef CONFIG_PPC_CHRP
  47. /*
  48. * This looks in the "ranges" property for the primary PCI host bridge
  49. * to find the physical address of the start of PCI/ISA I/O space.
  50. * It is basically a cut-down version of pci_process_bridge_OF_ranges.
  51. */
  52. static unsigned long chrp_find_phys_io_base(void)
  53. {
  54. struct device_node *node;
  55. unsigned int *ranges;
  56. unsigned long base = CHRP_ISA_IO_BASE;
  57. int rlen = 0;
  58. int np;
  59. node = find_devices("isa");
  60. if (node != NULL) {
  61. node = node->parent;
  62. if (node == NULL || node->type == NULL
  63. || strcmp(node->type, "pci") != 0)
  64. node = NULL;
  65. }
  66. if (node == NULL)
  67. node = find_devices("pci");
  68. if (node == NULL)
  69. return base;
  70. ranges = (unsigned int *) get_property(node, "ranges", &rlen);
  71. np = prom_n_addr_cells(node) + 5;
  72. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  73. if ((ranges[0] >> 24) == 1 && ranges[2] == 0) {
  74. /* I/O space starting at 0, grab the phys base */
  75. base = ranges[np - 3];
  76. break;
  77. }
  78. ranges += np;
  79. }
  80. return base;
  81. }
  82. #endif /* CONFIG_PPC_CHRP */
  83. #ifdef CONFIG_MAGIC_SYSRQ
  84. static void sysrq_handle_xmon(int key, struct pt_regs *regs,
  85. struct tty_struct *tty)
  86. {
  87. xmon(regs);
  88. }
  89. static struct sysrq_key_op sysrq_xmon_op =
  90. {
  91. .handler = sysrq_handle_xmon,
  92. .help_msg = "Xmon",
  93. .action_msg = "Entering xmon",
  94. };
  95. #endif
  96. void
  97. xmon_map_scc(void)
  98. {
  99. #ifdef CONFIG_PPC_MULTIPLATFORM
  100. volatile unsigned char *base;
  101. if (_machine == _MACH_Pmac) {
  102. struct device_node *np;
  103. unsigned long addr;
  104. #ifdef CONFIG_BOOTX_TEXT
  105. if (!use_screen && !use_serial
  106. && !machine_is_compatible("iMac")) {
  107. /* see if there is a keyboard in the device tree
  108. with a parent of type "adb" */
  109. for (np = find_devices("keyboard"); np; np = np->next)
  110. if (np->parent && np->parent->type
  111. && strcmp(np->parent->type, "adb") == 0)
  112. break;
  113. /* needs to be hacked if xmon_printk is to be used
  114. from within find_via_pmu() */
  115. #ifdef CONFIG_ADB_PMU
  116. if (np != NULL && boot_text_mapped && find_via_pmu())
  117. use_screen = 1;
  118. #endif
  119. #ifdef CONFIG_ADB_CUDA
  120. if (np != NULL && boot_text_mapped && find_via_cuda())
  121. use_screen = 1;
  122. #endif
  123. }
  124. if (!use_screen && (np = find_devices("escc")) != NULL) {
  125. /*
  126. * look for the device node for the serial port
  127. * we're using and see if it says it has a modem
  128. */
  129. char *name = xmon_use_sccb? "ch-b": "ch-a";
  130. char *slots;
  131. int l;
  132. np = np->child;
  133. while (np != NULL && strcmp(np->name, name) != 0)
  134. np = np->sibling;
  135. if (np != NULL) {
  136. /* XXX should parse this properly */
  137. channel_node = np;
  138. slots = get_property(np, "slot-names", &l);
  139. if (slots != NULL && l >= 10
  140. && strcmp(slots+4, "Modem") == 0)
  141. via_modem = 1;
  142. }
  143. }
  144. btext_drawstring("xmon uses ");
  145. if (use_screen)
  146. btext_drawstring("screen and keyboard\n");
  147. else {
  148. if (via_modem)
  149. btext_drawstring("modem on ");
  150. btext_drawstring(xmon_use_sccb? "printer": "modem");
  151. btext_drawstring(" port\n");
  152. }
  153. #endif /* CONFIG_BOOTX_TEXT */
  154. #ifdef CHRP_ESCC
  155. addr = 0xc1013020;
  156. #else
  157. addr = 0xf3013020;
  158. #endif
  159. TXRDY = 4;
  160. RXRDY = 1;
  161. np = find_devices("mac-io");
  162. if (np && np->n_addrs)
  163. addr = np->addrs[0].address + 0x13020;
  164. base = (volatile unsigned char *) ioremap(addr & PAGE_MASK, PAGE_SIZE);
  165. sccc = base + (addr & ~PAGE_MASK);
  166. sccd = sccc + 0x10;
  167. } else {
  168. base = (volatile unsigned char *) isa_io_base;
  169. if (_machine == _MACH_chrp)
  170. base = (volatile unsigned char *)
  171. ioremap(chrp_find_phys_io_base(), 0x1000);
  172. sccc = base + 0x3fd;
  173. sccd = base + 0x3f8;
  174. if (xmon_use_sccb) {
  175. sccc -= 0x100;
  176. sccd -= 0x100;
  177. }
  178. TXRDY = 0x20;
  179. RXRDY = 1;
  180. DLAB = 0x80;
  181. }
  182. #elif defined(CONFIG_GEMINI)
  183. /* should already be mapped by the kernel boot */
  184. sccc = (volatile unsigned char *) 0xffeffb0d;
  185. sccd = (volatile unsigned char *) 0xffeffb08;
  186. TXRDY = 0x20;
  187. RXRDY = 1;
  188. DLAB = 0x80;
  189. #elif defined(CONFIG_405GP)
  190. sccc = (volatile unsigned char *)0xef600305;
  191. sccd = (volatile unsigned char *)0xef600300;
  192. TXRDY = 0x20;
  193. RXRDY = 1;
  194. DLAB = 0x80;
  195. #endif /* platform */
  196. register_sysrq_key('x', &sysrq_xmon_op);
  197. }
  198. static int scc_initialized = 0;
  199. void xmon_init_scc(void);
  200. extern void cuda_poll(void);
  201. static inline void do_poll_adb(void)
  202. {
  203. #ifdef CONFIG_ADB_PMU
  204. if (sys_ctrler == SYS_CTRLER_PMU)
  205. pmu_poll_adb();
  206. #endif /* CONFIG_ADB_PMU */
  207. #ifdef CONFIG_ADB_CUDA
  208. if (sys_ctrler == SYS_CTRLER_CUDA)
  209. cuda_poll();
  210. #endif /* CONFIG_ADB_CUDA */
  211. }
  212. int
  213. xmon_write(void *handle, void *ptr, int nb)
  214. {
  215. char *p = ptr;
  216. int i, c, ct;
  217. #ifdef CONFIG_SMP
  218. static unsigned long xmon_write_lock;
  219. int lock_wait = 1000000;
  220. int locked;
  221. while ((locked = test_and_set_bit(0, &xmon_write_lock)) != 0)
  222. if (--lock_wait == 0)
  223. break;
  224. #endif
  225. #ifdef CONFIG_BOOTX_TEXT
  226. if (use_screen) {
  227. /* write it on the screen */
  228. for (i = 0; i < nb; ++i)
  229. btext_drawchar(*p++);
  230. goto out;
  231. }
  232. #endif
  233. if (!scc_initialized)
  234. xmon_init_scc();
  235. ct = 0;
  236. for (i = 0; i < nb; ++i) {
  237. while ((*sccc & TXRDY) == 0)
  238. do_poll_adb();
  239. c = p[i];
  240. if (c == '\n' && !ct) {
  241. c = '\r';
  242. ct = 1;
  243. --i;
  244. } else {
  245. ct = 0;
  246. }
  247. buf_access();
  248. *sccd = c;
  249. eieio();
  250. }
  251. out:
  252. #ifdef CONFIG_SMP
  253. if (!locked)
  254. clear_bit(0, &xmon_write_lock);
  255. #endif
  256. return nb;
  257. }
  258. int xmon_wants_key;
  259. int xmon_adb_keycode;
  260. #ifdef CONFIG_BOOTX_TEXT
  261. static int xmon_adb_shiftstate;
  262. static unsigned char xmon_keytab[128] =
  263. "asdfhgzxcv\000bqwer" /* 0x00 - 0x0f */
  264. "yt123465=97-80]o" /* 0x10 - 0x1f */
  265. "u[ip\rlj'k;\\,/nm." /* 0x20 - 0x2f */
  266. "\t `\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
  267. "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
  268. "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
  269. static unsigned char xmon_shift_keytab[128] =
  270. "ASDFHGZXCV\000BQWER" /* 0x00 - 0x0f */
  271. "YT!@#$^%+(&_*)}O" /* 0x10 - 0x1f */
  272. "U{IP\rLJ\"K:|<?NM>" /* 0x20 - 0x2f */
  273. "\t ~\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */
  274. "\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */
  275. "\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */
  276. static int
  277. xmon_get_adb_key(void)
  278. {
  279. int k, t, on;
  280. xmon_wants_key = 1;
  281. for (;;) {
  282. xmon_adb_keycode = -1;
  283. t = 0;
  284. on = 0;
  285. do {
  286. if (--t < 0) {
  287. on = 1 - on;
  288. btext_drawchar(on? 0xdb: 0x20);
  289. btext_drawchar('\b');
  290. t = 200000;
  291. }
  292. do_poll_adb();
  293. } while (xmon_adb_keycode == -1);
  294. k = xmon_adb_keycode;
  295. if (on)
  296. btext_drawstring(" \b");
  297. /* test for shift keys */
  298. if ((k & 0x7f) == 0x38 || (k & 0x7f) == 0x7b) {
  299. xmon_adb_shiftstate = (k & 0x80) == 0;
  300. continue;
  301. }
  302. if (k >= 0x80)
  303. continue; /* ignore up transitions */
  304. k = (xmon_adb_shiftstate? xmon_shift_keytab: xmon_keytab)[k];
  305. if (k != 0)
  306. break;
  307. }
  308. xmon_wants_key = 0;
  309. return k;
  310. }
  311. #endif /* CONFIG_BOOTX_TEXT */
  312. int
  313. xmon_read(void *handle, void *ptr, int nb)
  314. {
  315. char *p = ptr;
  316. int i;
  317. #ifdef CONFIG_BOOTX_TEXT
  318. if (use_screen) {
  319. for (i = 0; i < nb; ++i)
  320. *p++ = xmon_get_adb_key();
  321. return i;
  322. }
  323. #endif
  324. if (!scc_initialized)
  325. xmon_init_scc();
  326. for (i = 0; i < nb; ++i) {
  327. while ((*sccc & RXRDY) == 0)
  328. do_poll_adb();
  329. buf_access();
  330. *p++ = *sccd;
  331. }
  332. return i;
  333. }
  334. int
  335. xmon_read_poll(void)
  336. {
  337. if ((*sccc & RXRDY) == 0) {
  338. do_poll_adb();
  339. return -1;
  340. }
  341. buf_access();
  342. return *sccd;
  343. }
  344. static unsigned char scc_inittab[] = {
  345. 13, 0, /* set baud rate divisor */
  346. 12, 1,
  347. 14, 1, /* baud rate gen enable, src=rtxc */
  348. 11, 0x50, /* clocks = br gen */
  349. 5, 0xea, /* tx 8 bits, assert DTR & RTS */
  350. 4, 0x46, /* x16 clock, 1 stop */
  351. 3, 0xc1, /* rx enable, 8 bits */
  352. };
  353. void
  354. xmon_init_scc(void)
  355. {
  356. if ( _machine == _MACH_chrp )
  357. {
  358. sccd[3] = 0x83; eieio(); /* LCR = 8N1 + DLAB */
  359. sccd[0] = 12; eieio(); /* DLL = 9600 baud */
  360. sccd[1] = 0; eieio();
  361. sccd[2] = 0; eieio(); /* FCR = 0 */
  362. sccd[3] = 3; eieio(); /* LCR = 8N1 */
  363. sccd[1] = 0; eieio(); /* IER = 0 */
  364. }
  365. else if ( _machine == _MACH_Pmac )
  366. {
  367. int i, x;
  368. if (channel_node != 0)
  369. pmac_call_feature(
  370. PMAC_FTR_SCC_ENABLE,
  371. channel_node,
  372. PMAC_SCC_ASYNC | PMAC_SCC_FLAG_XMON, 1);
  373. printk(KERN_INFO "Serial port locked ON by debugger !\n");
  374. if (via_modem && channel_node != 0) {
  375. unsigned int t0;
  376. pmac_call_feature(
  377. PMAC_FTR_MODEM_ENABLE,
  378. channel_node, 0, 1);
  379. printk(KERN_INFO "Modem powered up by debugger !\n");
  380. t0 = readtb();
  381. while (readtb() - t0 < 3*TB_SPEED)
  382. eieio();
  383. }
  384. /* use the B channel if requested */
  385. if (xmon_use_sccb) {
  386. sccc = (volatile unsigned char *)
  387. ((unsigned long)sccc & ~0x20);
  388. sccd = sccc + 0x10;
  389. }
  390. for (i = 20000; i != 0; --i) {
  391. x = *sccc; eieio();
  392. }
  393. *sccc = 9; eieio(); /* reset A or B side */
  394. *sccc = ((unsigned long)sccc & 0x20)? 0x80: 0x40; eieio();
  395. for (i = 0; i < sizeof(scc_inittab); ++i) {
  396. *sccc = scc_inittab[i];
  397. eieio();
  398. }
  399. }
  400. scc_initialized = 1;
  401. if (via_modem) {
  402. for (;;) {
  403. xmon_write(NULL, "ATE1V1\r", 7);
  404. if (xmon_expect("OK", 5)) {
  405. xmon_write(NULL, "ATA\r", 4);
  406. if (xmon_expect("CONNECT", 40))
  407. break;
  408. }
  409. xmon_write(NULL, "+++", 3);
  410. xmon_expect("OK", 3);
  411. }
  412. }
  413. }
  414. #if 0
  415. extern int (*prom_entry)(void *);
  416. int
  417. xmon_exit(void)
  418. {
  419. struct prom_args {
  420. char *service;
  421. } args;
  422. for (;;) {
  423. args.service = "exit";
  424. (*prom_entry)(&args);
  425. }
  426. }
  427. #endif
  428. void *xmon_stdin;
  429. void *xmon_stdout;
  430. void *xmon_stderr;
  431. void
  432. xmon_init(void)
  433. {
  434. }
  435. int
  436. xmon_putc(int c, void *f)
  437. {
  438. char ch = c;
  439. if (c == '\n')
  440. xmon_putc('\r', f);
  441. return xmon_write(f, &ch, 1) == 1? c: -1;
  442. }
  443. int
  444. xmon_putchar(int c)
  445. {
  446. return xmon_putc(c, xmon_stdout);
  447. }
  448. int
  449. xmon_fputs(char *str, void *f)
  450. {
  451. int n = strlen(str);
  452. return xmon_write(f, str, n) == n? 0: -1;
  453. }
  454. int
  455. xmon_readchar(void)
  456. {
  457. char ch;
  458. for (;;) {
  459. switch (xmon_read(xmon_stdin, &ch, 1)) {
  460. case 1:
  461. return ch;
  462. case -1:
  463. xmon_printf("read(stdin) returned -1\r\n", 0, 0);
  464. return -1;
  465. }
  466. }
  467. }
  468. static char line[256];
  469. static char *lineptr;
  470. static int lineleft;
  471. int xmon_expect(const char *str, unsigned int timeout)
  472. {
  473. int c;
  474. unsigned int t0;
  475. timeout *= TB_SPEED;
  476. t0 = readtb();
  477. do {
  478. lineptr = line;
  479. for (;;) {
  480. c = xmon_read_poll();
  481. if (c == -1) {
  482. if (readtb() - t0 > timeout)
  483. return 0;
  484. continue;
  485. }
  486. if (c == '\n')
  487. break;
  488. if (c != '\r' && lineptr < &line[sizeof(line) - 1])
  489. *lineptr++ = c;
  490. }
  491. *lineptr = 0;
  492. } while (strstr(line, str) == NULL);
  493. return 1;
  494. }
  495. int
  496. xmon_getchar(void)
  497. {
  498. int c;
  499. if (lineleft == 0) {
  500. lineptr = line;
  501. for (;;) {
  502. c = xmon_readchar();
  503. if (c == -1 || c == 4)
  504. break;
  505. if (c == '\r' || c == '\n') {
  506. *lineptr++ = '\n';
  507. xmon_putchar('\n');
  508. break;
  509. }
  510. switch (c) {
  511. case 0177:
  512. case '\b':
  513. if (lineptr > line) {
  514. xmon_putchar('\b');
  515. xmon_putchar(' ');
  516. xmon_putchar('\b');
  517. --lineptr;
  518. }
  519. break;
  520. case 'U' & 0x1F:
  521. while (lineptr > line) {
  522. xmon_putchar('\b');
  523. xmon_putchar(' ');
  524. xmon_putchar('\b');
  525. --lineptr;
  526. }
  527. break;
  528. default:
  529. if (lineptr >= &line[sizeof(line) - 1])
  530. xmon_putchar('\a');
  531. else {
  532. xmon_putchar(c);
  533. *lineptr++ = c;
  534. }
  535. }
  536. }
  537. lineleft = lineptr - line;
  538. lineptr = line;
  539. }
  540. if (lineleft == 0)
  541. return -1;
  542. --lineleft;
  543. return *lineptr++;
  544. }
  545. char *
  546. xmon_fgets(char *str, int nb, void *f)
  547. {
  548. char *p;
  549. int c;
  550. for (p = str; p < str + nb - 1; ) {
  551. c = xmon_getchar();
  552. if (c == -1) {
  553. if (p == str)
  554. return NULL;
  555. break;
  556. }
  557. *p++ = c;
  558. if (c == '\n')
  559. break;
  560. }
  561. *p = 0;
  562. return str;
  563. }
  564. void
  565. xmon_enter(void)
  566. {
  567. #ifdef CONFIG_ADB_PMU
  568. if (_machine == _MACH_Pmac) {
  569. pmu_suspend();
  570. }
  571. #endif
  572. }
  573. void
  574. xmon_leave(void)
  575. {
  576. #ifdef CONFIG_ADB_PMU
  577. if (_machine == _MACH_Pmac) {
  578. pmu_resume();
  579. }
  580. #endif
  581. }