early_printk.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. #include <linux/console.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/string.h>
  5. #include <linux/screen_info.h>
  6. #include <linux/usb/ch9.h>
  7. #include <linux/pci_regs.h>
  8. #include <linux/pci_ids.h>
  9. #include <linux/errno.h>
  10. #include <asm/io.h>
  11. #include <asm/processor.h>
  12. #include <asm/fcntl.h>
  13. #include <asm/setup.h>
  14. #include <xen/hvc-console.h>
  15. #include <asm/pci-direct.h>
  16. #include <asm/fixmap.h>
  17. #include <linux/usb/ehci_def.h>
  18. /* Simple VGA output */
  19. #define VGABASE (__ISA_IO_base + 0xb8000)
  20. static int max_ypos = 25, max_xpos = 80;
  21. static int current_ypos = 25, current_xpos;
  22. static void early_vga_write(struct console *con, const char *str, unsigned n)
  23. {
  24. char c;
  25. int i, k, j;
  26. while ((c = *str++) != '\0' && n-- > 0) {
  27. if (current_ypos >= max_ypos) {
  28. /* scroll 1 line up */
  29. for (k = 1, j = 0; k < max_ypos; k++, j++) {
  30. for (i = 0; i < max_xpos; i++) {
  31. writew(readw(VGABASE+2*(max_xpos*k+i)),
  32. VGABASE + 2*(max_xpos*j + i));
  33. }
  34. }
  35. for (i = 0; i < max_xpos; i++)
  36. writew(0x720, VGABASE + 2*(max_xpos*j + i));
  37. current_ypos = max_ypos-1;
  38. }
  39. if (c == '\n') {
  40. current_xpos = 0;
  41. current_ypos++;
  42. } else if (c != '\r') {
  43. writew(((0x7 << 8) | (unsigned short) c),
  44. VGABASE + 2*(max_xpos*current_ypos +
  45. current_xpos++));
  46. if (current_xpos >= max_xpos) {
  47. current_xpos = 0;
  48. current_ypos++;
  49. }
  50. }
  51. }
  52. }
  53. static struct console early_vga_console = {
  54. .name = "earlyvga",
  55. .write = early_vga_write,
  56. .flags = CON_PRINTBUFFER,
  57. .index = -1,
  58. };
  59. /* Serial functions loosely based on a similar package from Klaus P. Gerlicher */
  60. static int early_serial_base = 0x3f8; /* ttyS0 */
  61. #define XMTRDY 0x20
  62. #define DLAB 0x80
  63. #define TXR 0 /* Transmit register (WRITE) */
  64. #define RXR 0 /* Receive register (READ) */
  65. #define IER 1 /* Interrupt Enable */
  66. #define IIR 2 /* Interrupt ID */
  67. #define FCR 2 /* FIFO control */
  68. #define LCR 3 /* Line control */
  69. #define MCR 4 /* Modem control */
  70. #define LSR 5 /* Line Status */
  71. #define MSR 6 /* Modem Status */
  72. #define DLL 0 /* Divisor Latch Low */
  73. #define DLH 1 /* Divisor latch High */
  74. static int early_serial_putc(unsigned char ch)
  75. {
  76. unsigned timeout = 0xffff;
  77. while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
  78. cpu_relax();
  79. outb(ch, early_serial_base + TXR);
  80. return timeout ? 0 : -1;
  81. }
  82. static void early_serial_write(struct console *con, const char *s, unsigned n)
  83. {
  84. while (*s && n-- > 0) {
  85. if (*s == '\n')
  86. early_serial_putc('\r');
  87. early_serial_putc(*s);
  88. s++;
  89. }
  90. }
  91. #define DEFAULT_BAUD 9600
  92. static __init void early_serial_init(char *s)
  93. {
  94. unsigned char c;
  95. unsigned divisor;
  96. unsigned baud = DEFAULT_BAUD;
  97. char *e;
  98. if (*s == ',')
  99. ++s;
  100. if (*s) {
  101. unsigned port;
  102. if (!strncmp(s, "0x", 2)) {
  103. early_serial_base = simple_strtoul(s, &e, 16);
  104. } else {
  105. static const int __initconst bases[] = { 0x3f8, 0x2f8 };
  106. if (!strncmp(s, "ttyS", 4))
  107. s += 4;
  108. port = simple_strtoul(s, &e, 10);
  109. if (port > 1 || s == e)
  110. port = 0;
  111. early_serial_base = bases[port];
  112. }
  113. s += strcspn(s, ",");
  114. if (*s == ',')
  115. s++;
  116. }
  117. outb(0x3, early_serial_base + LCR); /* 8n1 */
  118. outb(0, early_serial_base + IER); /* no interrupt */
  119. outb(0, early_serial_base + FCR); /* no fifo */
  120. outb(0x3, early_serial_base + MCR); /* DTR + RTS */
  121. if (*s) {
  122. baud = simple_strtoul(s, &e, 0);
  123. if (baud == 0 || s == e)
  124. baud = DEFAULT_BAUD;
  125. }
  126. divisor = 115200 / baud;
  127. c = inb(early_serial_base + LCR);
  128. outb(c | DLAB, early_serial_base + LCR);
  129. outb(divisor & 0xff, early_serial_base + DLL);
  130. outb((divisor >> 8) & 0xff, early_serial_base + DLH);
  131. outb(c & ~DLAB, early_serial_base + LCR);
  132. }
  133. static struct console early_serial_console = {
  134. .name = "earlyser",
  135. .write = early_serial_write,
  136. .flags = CON_PRINTBUFFER,
  137. .index = -1,
  138. };
  139. #ifdef CONFIG_EARLY_PRINTK_DBGP
  140. static struct ehci_caps __iomem *ehci_caps;
  141. static struct ehci_regs __iomem *ehci_regs;
  142. static struct ehci_dbg_port __iomem *ehci_debug;
  143. static unsigned int dbgp_endpoint_out;
  144. struct ehci_dev {
  145. u32 bus;
  146. u32 slot;
  147. u32 func;
  148. };
  149. static struct ehci_dev ehci_dev;
  150. #define USB_DEBUG_DEVNUM 127
  151. #define DBGP_DATA_TOGGLE 0x8800
  152. static inline u32 dbgp_pid_update(u32 x, u32 tok)
  153. {
  154. return ((x ^ DBGP_DATA_TOGGLE) & 0xffff00) | (tok & 0xff);
  155. }
  156. static inline u32 dbgp_len_update(u32 x, u32 len)
  157. {
  158. return (x & ~0x0f) | (len & 0x0f);
  159. }
  160. /*
  161. * USB Packet IDs (PIDs)
  162. */
  163. /* token */
  164. #define USB_PID_OUT 0xe1
  165. #define USB_PID_IN 0x69
  166. #define USB_PID_SOF 0xa5
  167. #define USB_PID_SETUP 0x2d
  168. /* handshake */
  169. #define USB_PID_ACK 0xd2
  170. #define USB_PID_NAK 0x5a
  171. #define USB_PID_STALL 0x1e
  172. #define USB_PID_NYET 0x96
  173. /* data */
  174. #define USB_PID_DATA0 0xc3
  175. #define USB_PID_DATA1 0x4b
  176. #define USB_PID_DATA2 0x87
  177. #define USB_PID_MDATA 0x0f
  178. /* Special */
  179. #define USB_PID_PREAMBLE 0x3c
  180. #define USB_PID_ERR 0x3c
  181. #define USB_PID_SPLIT 0x78
  182. #define USB_PID_PING 0xb4
  183. #define USB_PID_UNDEF_0 0xf0
  184. #define USB_PID_DATA_TOGGLE 0x88
  185. #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
  186. #define PCI_CAP_ID_EHCI_DEBUG 0xa
  187. #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
  188. #define HUB_SHORT_RESET_TIME 10
  189. #define HUB_LONG_RESET_TIME 200
  190. #define HUB_RESET_TIMEOUT 500
  191. #define DBGP_MAX_PACKET 8
  192. static int dbgp_wait_until_complete(void)
  193. {
  194. u32 ctrl;
  195. int loop = 0x100000;
  196. do {
  197. ctrl = readl(&ehci_debug->control);
  198. /* Stop when the transaction is finished */
  199. if (ctrl & DBGP_DONE)
  200. break;
  201. } while (--loop > 0);
  202. if (!loop)
  203. return -1;
  204. /*
  205. * Now that we have observed the completed transaction,
  206. * clear the done bit.
  207. */
  208. writel(ctrl | DBGP_DONE, &ehci_debug->control);
  209. return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
  210. }
  211. static void dbgp_mdelay(int ms)
  212. {
  213. int i;
  214. while (ms--) {
  215. for (i = 0; i < 1000; i++)
  216. outb(0x1, 0x80);
  217. }
  218. }
  219. static void dbgp_breath(void)
  220. {
  221. /* Sleep to give the debug port a chance to breathe */
  222. }
  223. static int dbgp_wait_until_done(unsigned ctrl)
  224. {
  225. u32 pids, lpid;
  226. int ret;
  227. int loop = 3;
  228. retry:
  229. writel(ctrl | DBGP_GO, &ehci_debug->control);
  230. ret = dbgp_wait_until_complete();
  231. pids = readl(&ehci_debug->pids);
  232. lpid = DBGP_PID_GET(pids);
  233. if (ret < 0)
  234. return ret;
  235. /*
  236. * If the port is getting full or it has dropped data
  237. * start pacing ourselves, not necessary but it's friendly.
  238. */
  239. if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
  240. dbgp_breath();
  241. /* If I get a NACK reissue the transmission */
  242. if (lpid == USB_PID_NAK) {
  243. if (--loop > 0)
  244. goto retry;
  245. }
  246. return ret;
  247. }
  248. static void dbgp_set_data(const void *buf, int size)
  249. {
  250. const unsigned char *bytes = buf;
  251. u32 lo, hi;
  252. int i;
  253. lo = hi = 0;
  254. for (i = 0; i < 4 && i < size; i++)
  255. lo |= bytes[i] << (8*i);
  256. for (; i < 8 && i < size; i++)
  257. hi |= bytes[i] << (8*(i - 4));
  258. writel(lo, &ehci_debug->data03);
  259. writel(hi, &ehci_debug->data47);
  260. }
  261. static void dbgp_get_data(void *buf, int size)
  262. {
  263. unsigned char *bytes = buf;
  264. u32 lo, hi;
  265. int i;
  266. lo = readl(&ehci_debug->data03);
  267. hi = readl(&ehci_debug->data47);
  268. for (i = 0; i < 4 && i < size; i++)
  269. bytes[i] = (lo >> (8*i)) & 0xff;
  270. for (; i < 8 && i < size; i++)
  271. bytes[i] = (hi >> (8*(i - 4))) & 0xff;
  272. }
  273. static int dbgp_bulk_write(unsigned devnum, unsigned endpoint,
  274. const char *bytes, int size)
  275. {
  276. u32 pids, addr, ctrl;
  277. int ret;
  278. if (size > DBGP_MAX_PACKET)
  279. return -1;
  280. addr = DBGP_EPADDR(devnum, endpoint);
  281. pids = readl(&ehci_debug->pids);
  282. pids = dbgp_pid_update(pids, USB_PID_OUT);
  283. ctrl = readl(&ehci_debug->control);
  284. ctrl = dbgp_len_update(ctrl, size);
  285. ctrl |= DBGP_OUT;
  286. ctrl |= DBGP_GO;
  287. dbgp_set_data(bytes, size);
  288. writel(addr, &ehci_debug->address);
  289. writel(pids, &ehci_debug->pids);
  290. ret = dbgp_wait_until_done(ctrl);
  291. if (ret < 0)
  292. return ret;
  293. return ret;
  294. }
  295. static int dbgp_bulk_read(unsigned devnum, unsigned endpoint, void *data,
  296. int size)
  297. {
  298. u32 pids, addr, ctrl;
  299. int ret;
  300. if (size > DBGP_MAX_PACKET)
  301. return -1;
  302. addr = DBGP_EPADDR(devnum, endpoint);
  303. pids = readl(&ehci_debug->pids);
  304. pids = dbgp_pid_update(pids, USB_PID_IN);
  305. ctrl = readl(&ehci_debug->control);
  306. ctrl = dbgp_len_update(ctrl, size);
  307. ctrl &= ~DBGP_OUT;
  308. ctrl |= DBGP_GO;
  309. writel(addr, &ehci_debug->address);
  310. writel(pids, &ehci_debug->pids);
  311. ret = dbgp_wait_until_done(ctrl);
  312. if (ret < 0)
  313. return ret;
  314. if (size > ret)
  315. size = ret;
  316. dbgp_get_data(data, size);
  317. return ret;
  318. }
  319. static int dbgp_control_msg(unsigned devnum, int requesttype, int request,
  320. int value, int index, void *data, int size)
  321. {
  322. u32 pids, addr, ctrl;
  323. struct usb_ctrlrequest req;
  324. int read;
  325. int ret;
  326. read = (requesttype & USB_DIR_IN) != 0;
  327. if (size > (read ? DBGP_MAX_PACKET:0))
  328. return -1;
  329. /* Compute the control message */
  330. req.bRequestType = requesttype;
  331. req.bRequest = request;
  332. req.wValue = cpu_to_le16(value);
  333. req.wIndex = cpu_to_le16(index);
  334. req.wLength = cpu_to_le16(size);
  335. pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
  336. addr = DBGP_EPADDR(devnum, 0);
  337. ctrl = readl(&ehci_debug->control);
  338. ctrl = dbgp_len_update(ctrl, sizeof(req));
  339. ctrl |= DBGP_OUT;
  340. ctrl |= DBGP_GO;
  341. /* Send the setup message */
  342. dbgp_set_data(&req, sizeof(req));
  343. writel(addr, &ehci_debug->address);
  344. writel(pids, &ehci_debug->pids);
  345. ret = dbgp_wait_until_done(ctrl);
  346. if (ret < 0)
  347. return ret;
  348. /* Read the result */
  349. return dbgp_bulk_read(devnum, 0, data, size);
  350. }
  351. /* Find a PCI capability */
  352. static u32 __init find_cap(u32 num, u32 slot, u32 func, int cap)
  353. {
  354. u8 pos;
  355. int bytes;
  356. if (!(read_pci_config_16(num, slot, func, PCI_STATUS) &
  357. PCI_STATUS_CAP_LIST))
  358. return 0;
  359. pos = read_pci_config_byte(num, slot, func, PCI_CAPABILITY_LIST);
  360. for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
  361. u8 id;
  362. pos &= ~3;
  363. id = read_pci_config_byte(num, slot, func, pos+PCI_CAP_LIST_ID);
  364. if (id == 0xff)
  365. break;
  366. if (id == cap)
  367. return pos;
  368. pos = read_pci_config_byte(num, slot, func,
  369. pos+PCI_CAP_LIST_NEXT);
  370. }
  371. return 0;
  372. }
  373. static u32 __init __find_dbgp(u32 bus, u32 slot, u32 func)
  374. {
  375. u32 class;
  376. class = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
  377. if ((class >> 8) != PCI_CLASS_SERIAL_USB_EHCI)
  378. return 0;
  379. return find_cap(bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);
  380. }
  381. static u32 __init find_dbgp(int ehci_num, u32 *rbus, u32 *rslot, u32 *rfunc)
  382. {
  383. u32 bus, slot, func;
  384. for (bus = 0; bus < 256; bus++) {
  385. for (slot = 0; slot < 32; slot++) {
  386. for (func = 0; func < 8; func++) {
  387. unsigned cap;
  388. cap = __find_dbgp(bus, slot, func);
  389. if (!cap)
  390. continue;
  391. if (ehci_num-- != 0)
  392. continue;
  393. *rbus = bus;
  394. *rslot = slot;
  395. *rfunc = func;
  396. return cap;
  397. }
  398. }
  399. }
  400. return 0;
  401. }
  402. static int ehci_reset_port(int port)
  403. {
  404. u32 portsc;
  405. u32 delay_time, delay;
  406. int loop;
  407. /* Reset the usb debug port */
  408. portsc = readl(&ehci_regs->port_status[port - 1]);
  409. portsc &= ~PORT_PE;
  410. portsc |= PORT_RESET;
  411. writel(portsc, &ehci_regs->port_status[port - 1]);
  412. delay = HUB_ROOT_RESET_TIME;
  413. for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
  414. delay_time += delay) {
  415. dbgp_mdelay(delay);
  416. portsc = readl(&ehci_regs->port_status[port - 1]);
  417. if (portsc & PORT_RESET) {
  418. /* force reset to complete */
  419. loop = 2;
  420. writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
  421. &ehci_regs->port_status[port - 1]);
  422. do {
  423. portsc = readl(&ehci_regs->port_status[port-1]);
  424. } while ((portsc & PORT_RESET) && (--loop > 0));
  425. }
  426. /* Device went away? */
  427. if (!(portsc & PORT_CONNECT))
  428. return -ENOTCONN;
  429. /* bomb out completely if something weird happend */
  430. if ((portsc & PORT_CSC))
  431. return -EINVAL;
  432. /* If we've finished resetting, then break out of the loop */
  433. if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
  434. return 0;
  435. }
  436. return -EBUSY;
  437. }
  438. static int ehci_wait_for_port(int port)
  439. {
  440. u32 status;
  441. int ret, reps;
  442. for (reps = 0; reps < 3; reps++) {
  443. dbgp_mdelay(100);
  444. status = readl(&ehci_regs->status);
  445. if (status & STS_PCD) {
  446. ret = ehci_reset_port(port);
  447. if (ret == 0)
  448. return 0;
  449. }
  450. }
  451. return -ENOTCONN;
  452. }
  453. #ifdef DBGP_DEBUG
  454. # define dbgp_printk early_printk
  455. #else
  456. static inline void dbgp_printk(const char *fmt, ...) { }
  457. #endif
  458. typedef void (*set_debug_port_t)(int port);
  459. static void default_set_debug_port(int port)
  460. {
  461. }
  462. static set_debug_port_t set_debug_port = default_set_debug_port;
  463. static void nvidia_set_debug_port(int port)
  464. {
  465. u32 dword;
  466. dword = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  467. 0x74);
  468. dword &= ~(0x0f<<12);
  469. dword |= ((port & 0x0f)<<12);
  470. write_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func, 0x74,
  471. dword);
  472. dbgp_printk("set debug port to %d\n", port);
  473. }
  474. static void __init detect_set_debug_port(void)
  475. {
  476. u32 vendorid;
  477. vendorid = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  478. 0x00);
  479. if ((vendorid & 0xffff) == 0x10de) {
  480. dbgp_printk("using nvidia set_debug_port\n");
  481. set_debug_port = nvidia_set_debug_port;
  482. }
  483. }
  484. static int __init ehci_setup(void)
  485. {
  486. struct usb_debug_descriptor dbgp_desc;
  487. u32 cmd, ctrl, status, portsc, hcs_params;
  488. u32 debug_port, new_debug_port = 0, n_ports;
  489. u32 devnum;
  490. int ret, i;
  491. int loop;
  492. int port_map_tried;
  493. int playtimes = 3;
  494. try_next_time:
  495. port_map_tried = 0;
  496. try_next_port:
  497. hcs_params = readl(&ehci_caps->hcs_params);
  498. debug_port = HCS_DEBUG_PORT(hcs_params);
  499. n_ports = HCS_N_PORTS(hcs_params);
  500. dbgp_printk("debug_port: %d\n", debug_port);
  501. dbgp_printk("n_ports: %d\n", n_ports);
  502. for (i = 1; i <= n_ports; i++) {
  503. portsc = readl(&ehci_regs->port_status[i-1]);
  504. dbgp_printk("portstatus%d: %08x\n", i, portsc);
  505. }
  506. if (port_map_tried && (new_debug_port != debug_port)) {
  507. if (--playtimes) {
  508. set_debug_port(new_debug_port);
  509. goto try_next_time;
  510. }
  511. return -1;
  512. }
  513. loop = 10;
  514. /* Reset the EHCI controller */
  515. cmd = readl(&ehci_regs->command);
  516. cmd |= CMD_RESET;
  517. writel(cmd, &ehci_regs->command);
  518. do {
  519. cmd = readl(&ehci_regs->command);
  520. } while ((cmd & CMD_RESET) && (--loop > 0));
  521. if (!loop) {
  522. dbgp_printk("can not reset ehci\n");
  523. return -1;
  524. }
  525. dbgp_printk("ehci reset done\n");
  526. /* Claim ownership, but do not enable yet */
  527. ctrl = readl(&ehci_debug->control);
  528. ctrl |= DBGP_OWNER;
  529. ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
  530. writel(ctrl, &ehci_debug->control);
  531. /* Start the ehci running */
  532. cmd = readl(&ehci_regs->command);
  533. cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
  534. cmd |= CMD_RUN;
  535. writel(cmd, &ehci_regs->command);
  536. /* Ensure everything is routed to the EHCI */
  537. writel(FLAG_CF, &ehci_regs->configured_flag);
  538. /* Wait until the controller is no longer halted */
  539. loop = 10;
  540. do {
  541. status = readl(&ehci_regs->status);
  542. } while ((status & STS_HALT) && (--loop > 0));
  543. if (!loop) {
  544. dbgp_printk("ehci can be started\n");
  545. return -1;
  546. }
  547. dbgp_printk("ehci started\n");
  548. /* Wait for a device to show up in the debug port */
  549. ret = ehci_wait_for_port(debug_port);
  550. if (ret < 0) {
  551. dbgp_printk("No device found in debug port\n");
  552. goto next_debug_port;
  553. }
  554. dbgp_printk("ehci wait for port done\n");
  555. /* Enable the debug port */
  556. ctrl = readl(&ehci_debug->control);
  557. ctrl |= DBGP_CLAIM;
  558. writel(ctrl, &ehci_debug->control);
  559. ctrl = readl(&ehci_debug->control);
  560. if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
  561. dbgp_printk("No device in debug port\n");
  562. writel(ctrl & ~DBGP_CLAIM, &ehci_debug->control);
  563. goto err;
  564. }
  565. dbgp_printk("debug ported enabled\n");
  566. /* Completely transfer the debug device to the debug controller */
  567. portsc = readl(&ehci_regs->port_status[debug_port - 1]);
  568. portsc &= ~PORT_PE;
  569. writel(portsc, &ehci_regs->port_status[debug_port - 1]);
  570. dbgp_mdelay(100);
  571. /* Find the debug device and make it device number 127 */
  572. for (devnum = 0; devnum <= 127; devnum++) {
  573. ret = dbgp_control_msg(devnum,
  574. USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  575. USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
  576. &dbgp_desc, sizeof(dbgp_desc));
  577. if (ret > 0)
  578. break;
  579. }
  580. if (devnum > 127) {
  581. dbgp_printk("Could not find attached debug device\n");
  582. goto err;
  583. }
  584. if (ret < 0) {
  585. dbgp_printk("Attached device is not a debug device\n");
  586. goto err;
  587. }
  588. dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
  589. /* Move the device to 127 if it isn't already there */
  590. if (devnum != USB_DEBUG_DEVNUM) {
  591. ret = dbgp_control_msg(devnum,
  592. USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  593. USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
  594. if (ret < 0) {
  595. dbgp_printk("Could not move attached device to %d\n",
  596. USB_DEBUG_DEVNUM);
  597. goto err;
  598. }
  599. devnum = USB_DEBUG_DEVNUM;
  600. dbgp_printk("debug device renamed to 127\n");
  601. }
  602. /* Enable the debug interface */
  603. ret = dbgp_control_msg(USB_DEBUG_DEVNUM,
  604. USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  605. USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
  606. if (ret < 0) {
  607. dbgp_printk(" Could not enable the debug device\n");
  608. goto err;
  609. }
  610. dbgp_printk("debug interface enabled\n");
  611. /* Perform a small write to get the even/odd data state in sync
  612. */
  613. ret = dbgp_bulk_write(USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ", 1);
  614. if (ret < 0) {
  615. dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
  616. goto err;
  617. }
  618. dbgp_printk("small write doned\n");
  619. return 0;
  620. err:
  621. /* Things didn't work so remove my claim */
  622. ctrl = readl(&ehci_debug->control);
  623. ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
  624. writel(ctrl, &ehci_debug->control);
  625. return -1;
  626. next_debug_port:
  627. port_map_tried |= (1<<(debug_port - 1));
  628. new_debug_port = ((debug_port-1+1)%n_ports) + 1;
  629. if (port_map_tried != ((1<<n_ports) - 1)) {
  630. set_debug_port(new_debug_port);
  631. goto try_next_port;
  632. }
  633. if (--playtimes) {
  634. set_debug_port(new_debug_port);
  635. goto try_next_time;
  636. }
  637. return -1;
  638. }
  639. static int __init early_dbgp_init(char *s)
  640. {
  641. u32 debug_port, bar, offset;
  642. u32 bus, slot, func, cap;
  643. void __iomem *ehci_bar;
  644. u32 dbgp_num;
  645. u32 bar_val;
  646. char *e;
  647. int ret;
  648. u8 byte;
  649. if (!early_pci_allowed())
  650. return -1;
  651. dbgp_num = 0;
  652. if (*s)
  653. dbgp_num = simple_strtoul(s, &e, 10);
  654. dbgp_printk("dbgp_num: %d\n", dbgp_num);
  655. cap = find_dbgp(dbgp_num, &bus, &slot, &func);
  656. if (!cap)
  657. return -1;
  658. dbgp_printk("Found EHCI debug port on %02x:%02x.%1x\n", bus, slot,
  659. func);
  660. debug_port = read_pci_config(bus, slot, func, cap);
  661. bar = (debug_port >> 29) & 0x7;
  662. bar = (bar * 4) + 0xc;
  663. offset = (debug_port >> 16) & 0xfff;
  664. dbgp_printk("bar: %02x offset: %03x\n", bar, offset);
  665. if (bar != PCI_BASE_ADDRESS_0) {
  666. dbgp_printk("only debug ports on bar 1 handled.\n");
  667. return -1;
  668. }
  669. bar_val = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
  670. dbgp_printk("bar_val: %02x offset: %03x\n", bar_val, offset);
  671. if (bar_val & ~PCI_BASE_ADDRESS_MEM_MASK) {
  672. dbgp_printk("only simple 32bit mmio bars supported\n");
  673. return -1;
  674. }
  675. /* double check if the mem space is enabled */
  676. byte = read_pci_config_byte(bus, slot, func, 0x04);
  677. if (!(byte & 0x2)) {
  678. byte |= 0x02;
  679. write_pci_config_byte(bus, slot, func, 0x04, byte);
  680. dbgp_printk("mmio for ehci enabled\n");
  681. }
  682. /*
  683. * FIXME I don't have the bar size so just guess PAGE_SIZE is more
  684. * than enough. 1K is the biggest I have seen.
  685. */
  686. set_fixmap_nocache(FIX_DBGP_BASE, bar_val & PAGE_MASK);
  687. ehci_bar = (void __iomem *)__fix_to_virt(FIX_DBGP_BASE);
  688. ehci_bar += bar_val & ~PAGE_MASK;
  689. dbgp_printk("ehci_bar: %p\n", ehci_bar);
  690. ehci_caps = ehci_bar;
  691. ehci_regs = ehci_bar + HC_LENGTH(readl(&ehci_caps->hc_capbase));
  692. ehci_debug = ehci_bar + offset;
  693. ehci_dev.bus = bus;
  694. ehci_dev.slot = slot;
  695. ehci_dev.func = func;
  696. detect_set_debug_port();
  697. ret = ehci_setup();
  698. if (ret < 0) {
  699. dbgp_printk("ehci_setup failed\n");
  700. ehci_debug = NULL;
  701. return -1;
  702. }
  703. return 0;
  704. }
  705. static void early_dbgp_write(struct console *con, const char *str, u32 n)
  706. {
  707. int chunk, ret;
  708. if (!ehci_debug)
  709. return;
  710. while (n > 0) {
  711. chunk = n;
  712. if (chunk > DBGP_MAX_PACKET)
  713. chunk = DBGP_MAX_PACKET;
  714. ret = dbgp_bulk_write(USB_DEBUG_DEVNUM,
  715. dbgp_endpoint_out, str, chunk);
  716. str += chunk;
  717. n -= chunk;
  718. }
  719. }
  720. static struct console early_dbgp_console = {
  721. .name = "earlydbg",
  722. .write = early_dbgp_write,
  723. .flags = CON_PRINTBUFFER,
  724. .index = -1,
  725. };
  726. #endif
  727. /* Direct interface for emergencies */
  728. static struct console *early_console = &early_vga_console;
  729. static int __initdata early_console_initialized;
  730. asmlinkage void early_printk(const char *fmt, ...)
  731. {
  732. char buf[512];
  733. int n;
  734. va_list ap;
  735. va_start(ap, fmt);
  736. n = vscnprintf(buf, sizeof(buf), fmt, ap);
  737. early_console->write(early_console, buf, n);
  738. va_end(ap);
  739. }
  740. static int __init setup_early_printk(char *buf)
  741. {
  742. int keep_early;
  743. if (!buf)
  744. return 0;
  745. if (early_console_initialized)
  746. return 0;
  747. early_console_initialized = 1;
  748. keep_early = (strstr(buf, "keep") != NULL);
  749. if (!strncmp(buf, "serial", 6)) {
  750. early_serial_init(buf + 6);
  751. early_console = &early_serial_console;
  752. } else if (!strncmp(buf, "ttyS", 4)) {
  753. early_serial_init(buf);
  754. early_console = &early_serial_console;
  755. } else if (!strncmp(buf, "vga", 3)
  756. && boot_params.screen_info.orig_video_isVGA == 1) {
  757. max_xpos = boot_params.screen_info.orig_video_cols;
  758. max_ypos = boot_params.screen_info.orig_video_lines;
  759. current_ypos = boot_params.screen_info.orig_y;
  760. early_console = &early_vga_console;
  761. #ifdef CONFIG_EARLY_PRINTK_DBGP
  762. } else if (!strncmp(buf, "dbgp", 4)) {
  763. if (early_dbgp_init(buf+4) < 0)
  764. return 0;
  765. early_console = &early_dbgp_console;
  766. /*
  767. * usb subsys will reset ehci controller, so don't keep
  768. * that early console
  769. */
  770. keep_early = 0;
  771. #endif
  772. #ifdef CONFIG_HVC_XEN
  773. } else if (!strncmp(buf, "xen", 3)) {
  774. early_console = &xenboot_console;
  775. #endif
  776. }
  777. if (keep_early)
  778. early_console->flags &= ~CON_BOOT;
  779. else
  780. early_console->flags |= CON_BOOT;
  781. register_console(early_console);
  782. return 0;
  783. }
  784. early_param("earlyprintk", setup_early_printk);