ehci-dbgp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. #include <linux/console.h>
  2. #include <linux/errno.h>
  3. #include <linux/pci_regs.h>
  4. #include <linux/pci_ids.h>
  5. #include <linux/usb/ch9.h>
  6. #include <linux/usb/ehci_def.h>
  7. #include <linux/delay.h>
  8. #include <asm/io.h>
  9. #include <asm/pci-direct.h>
  10. #include <asm/fixmap.h>
  11. #ifdef DBGP_DEBUG
  12. # define dbgp_printk printk
  13. #else
  14. static inline void dbgp_printk(const char *fmt, ...) { }
  15. #endif
  16. static struct ehci_caps __iomem *ehci_caps;
  17. static struct ehci_regs __iomem *ehci_regs;
  18. static struct ehci_dbg_port __iomem *ehci_debug;
  19. static unsigned int dbgp_endpoint_out;
  20. struct ehci_dev {
  21. u32 bus;
  22. u32 slot;
  23. u32 func;
  24. };
  25. static struct ehci_dev ehci_dev;
  26. #define USB_DEBUG_DEVNUM 127
  27. #define DBGP_DATA_TOGGLE 0x8800
  28. static inline u32 dbgp_pid_update(u32 x, u32 tok)
  29. {
  30. return ((x ^ DBGP_DATA_TOGGLE) & 0xffff00) | (tok & 0xff);
  31. }
  32. static inline u32 dbgp_len_update(u32 x, u32 len)
  33. {
  34. return (x & ~0x0f) | (len & 0x0f);
  35. }
  36. /*
  37. * USB Packet IDs (PIDs)
  38. */
  39. /* token */
  40. #define USB_PID_OUT 0xe1
  41. #define USB_PID_IN 0x69
  42. #define USB_PID_SOF 0xa5
  43. #define USB_PID_SETUP 0x2d
  44. /* handshake */
  45. #define USB_PID_ACK 0xd2
  46. #define USB_PID_NAK 0x5a
  47. #define USB_PID_STALL 0x1e
  48. #define USB_PID_NYET 0x96
  49. /* data */
  50. #define USB_PID_DATA0 0xc3
  51. #define USB_PID_DATA1 0x4b
  52. #define USB_PID_DATA2 0x87
  53. #define USB_PID_MDATA 0x0f
  54. /* Special */
  55. #define USB_PID_PREAMBLE 0x3c
  56. #define USB_PID_ERR 0x3c
  57. #define USB_PID_SPLIT 0x78
  58. #define USB_PID_PING 0xb4
  59. #define USB_PID_UNDEF_0 0xf0
  60. #define USB_PID_DATA_TOGGLE 0x88
  61. #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
  62. #define PCI_CAP_ID_EHCI_DEBUG 0xa
  63. #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
  64. #define HUB_SHORT_RESET_TIME 10
  65. #define HUB_LONG_RESET_TIME 200
  66. #define HUB_RESET_TIMEOUT 500
  67. #define DBGP_MAX_PACKET 8
  68. static int dbgp_wait_until_complete(void)
  69. {
  70. u32 ctrl;
  71. int loop = 0x100000;
  72. do {
  73. ctrl = readl(&ehci_debug->control);
  74. /* Stop when the transaction is finished */
  75. if (ctrl & DBGP_DONE)
  76. break;
  77. } while (--loop > 0);
  78. if (!loop)
  79. return -1;
  80. /*
  81. * Now that we have observed the completed transaction,
  82. * clear the done bit.
  83. */
  84. writel(ctrl | DBGP_DONE, &ehci_debug->control);
  85. return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
  86. }
  87. static void __init dbgp_mdelay(int ms)
  88. {
  89. int i;
  90. while (ms--) {
  91. for (i = 0; i < 1000; i++)
  92. outb(0x1, 0x80);
  93. }
  94. }
  95. static void dbgp_breath(void)
  96. {
  97. /* Sleep to give the debug port a chance to breathe */
  98. }
  99. static int dbgp_wait_until_done(unsigned ctrl)
  100. {
  101. u32 pids, lpid;
  102. int ret;
  103. int loop = 3;
  104. retry:
  105. writel(ctrl | DBGP_GO, &ehci_debug->control);
  106. ret = dbgp_wait_until_complete();
  107. pids = readl(&ehci_debug->pids);
  108. lpid = DBGP_PID_GET(pids);
  109. if (ret < 0)
  110. return ret;
  111. /*
  112. * If the port is getting full or it has dropped data
  113. * start pacing ourselves, not necessary but it's friendly.
  114. */
  115. if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
  116. dbgp_breath();
  117. /* If I get a NACK reissue the transmission */
  118. if (lpid == USB_PID_NAK) {
  119. if (--loop > 0)
  120. goto retry;
  121. }
  122. return ret;
  123. }
  124. static void dbgp_set_data(const void *buf, int size)
  125. {
  126. const unsigned char *bytes = buf;
  127. u32 lo, hi;
  128. int i;
  129. lo = hi = 0;
  130. for (i = 0; i < 4 && i < size; i++)
  131. lo |= bytes[i] << (8*i);
  132. for (; i < 8 && i < size; i++)
  133. hi |= bytes[i] << (8*(i - 4));
  134. writel(lo, &ehci_debug->data03);
  135. writel(hi, &ehci_debug->data47);
  136. }
  137. static void __init dbgp_get_data(void *buf, int size)
  138. {
  139. unsigned char *bytes = buf;
  140. u32 lo, hi;
  141. int i;
  142. lo = readl(&ehci_debug->data03);
  143. hi = readl(&ehci_debug->data47);
  144. for (i = 0; i < 4 && i < size; i++)
  145. bytes[i] = (lo >> (8*i)) & 0xff;
  146. for (; i < 8 && i < size; i++)
  147. bytes[i] = (hi >> (8*(i - 4))) & 0xff;
  148. }
  149. static int dbgp_bulk_write(unsigned devnum, unsigned endpoint,
  150. const char *bytes, int size)
  151. {
  152. u32 pids, addr, ctrl;
  153. int ret;
  154. if (size > DBGP_MAX_PACKET)
  155. return -1;
  156. addr = DBGP_EPADDR(devnum, endpoint);
  157. pids = readl(&ehci_debug->pids);
  158. pids = dbgp_pid_update(pids, USB_PID_OUT);
  159. ctrl = readl(&ehci_debug->control);
  160. ctrl = dbgp_len_update(ctrl, size);
  161. ctrl |= DBGP_OUT;
  162. ctrl |= DBGP_GO;
  163. dbgp_set_data(bytes, size);
  164. writel(addr, &ehci_debug->address);
  165. writel(pids, &ehci_debug->pids);
  166. ret = dbgp_wait_until_done(ctrl);
  167. if (ret < 0)
  168. return ret;
  169. return ret;
  170. }
  171. static int __init dbgp_bulk_read(unsigned devnum, unsigned endpoint, void *data,
  172. int size)
  173. {
  174. u32 pids, addr, ctrl;
  175. int ret;
  176. if (size > DBGP_MAX_PACKET)
  177. return -1;
  178. addr = DBGP_EPADDR(devnum, endpoint);
  179. pids = readl(&ehci_debug->pids);
  180. pids = dbgp_pid_update(pids, USB_PID_IN);
  181. ctrl = readl(&ehci_debug->control);
  182. ctrl = dbgp_len_update(ctrl, size);
  183. ctrl &= ~DBGP_OUT;
  184. ctrl |= DBGP_GO;
  185. writel(addr, &ehci_debug->address);
  186. writel(pids, &ehci_debug->pids);
  187. ret = dbgp_wait_until_done(ctrl);
  188. if (ret < 0)
  189. return ret;
  190. if (size > ret)
  191. size = ret;
  192. dbgp_get_data(data, size);
  193. return ret;
  194. }
  195. static int __init dbgp_control_msg(unsigned devnum, int requesttype,
  196. int request, int value, int index, void *data, int size)
  197. {
  198. u32 pids, addr, ctrl;
  199. struct usb_ctrlrequest req;
  200. int read;
  201. int ret;
  202. read = (requesttype & USB_DIR_IN) != 0;
  203. if (size > (read ? DBGP_MAX_PACKET:0))
  204. return -1;
  205. /* Compute the control message */
  206. req.bRequestType = requesttype;
  207. req.bRequest = request;
  208. req.wValue = cpu_to_le16(value);
  209. req.wIndex = cpu_to_le16(index);
  210. req.wLength = cpu_to_le16(size);
  211. pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
  212. addr = DBGP_EPADDR(devnum, 0);
  213. ctrl = readl(&ehci_debug->control);
  214. ctrl = dbgp_len_update(ctrl, sizeof(req));
  215. ctrl |= DBGP_OUT;
  216. ctrl |= DBGP_GO;
  217. /* Send the setup message */
  218. dbgp_set_data(&req, sizeof(req));
  219. writel(addr, &ehci_debug->address);
  220. writel(pids, &ehci_debug->pids);
  221. ret = dbgp_wait_until_done(ctrl);
  222. if (ret < 0)
  223. return ret;
  224. /* Read the result */
  225. return dbgp_bulk_read(devnum, 0, data, size);
  226. }
  227. /* Find a PCI capability */
  228. static u32 __init find_cap(u32 num, u32 slot, u32 func, int cap)
  229. {
  230. u8 pos;
  231. int bytes;
  232. if (!(read_pci_config_16(num, slot, func, PCI_STATUS) &
  233. PCI_STATUS_CAP_LIST))
  234. return 0;
  235. pos = read_pci_config_byte(num, slot, func, PCI_CAPABILITY_LIST);
  236. for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
  237. u8 id;
  238. pos &= ~3;
  239. id = read_pci_config_byte(num, slot, func, pos+PCI_CAP_LIST_ID);
  240. if (id == 0xff)
  241. break;
  242. if (id == cap)
  243. return pos;
  244. pos = read_pci_config_byte(num, slot, func,
  245. pos+PCI_CAP_LIST_NEXT);
  246. }
  247. return 0;
  248. }
  249. static u32 __init __find_dbgp(u32 bus, u32 slot, u32 func)
  250. {
  251. u32 class;
  252. class = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
  253. if ((class >> 8) != PCI_CLASS_SERIAL_USB_EHCI)
  254. return 0;
  255. return find_cap(bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);
  256. }
  257. static u32 __init find_dbgp(int ehci_num, u32 *rbus, u32 *rslot, u32 *rfunc)
  258. {
  259. u32 bus, slot, func;
  260. for (bus = 0; bus < 256; bus++) {
  261. for (slot = 0; slot < 32; slot++) {
  262. for (func = 0; func < 8; func++) {
  263. unsigned cap;
  264. cap = __find_dbgp(bus, slot, func);
  265. if (!cap)
  266. continue;
  267. if (ehci_num-- != 0)
  268. continue;
  269. *rbus = bus;
  270. *rslot = slot;
  271. *rfunc = func;
  272. return cap;
  273. }
  274. }
  275. }
  276. return 0;
  277. }
  278. static int __init ehci_reset_port(int port)
  279. {
  280. u32 portsc;
  281. u32 delay_time, delay;
  282. int loop;
  283. dbgp_printk("ehci_reset_port %i\n", port);
  284. /* Reset the usb debug port */
  285. portsc = readl(&ehci_regs->port_status[port - 1]);
  286. portsc &= ~PORT_PE;
  287. portsc |= PORT_RESET;
  288. writel(portsc, &ehci_regs->port_status[port - 1]);
  289. delay = HUB_ROOT_RESET_TIME;
  290. for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
  291. delay_time += delay) {
  292. dbgp_mdelay(delay);
  293. portsc = readl(&ehci_regs->port_status[port - 1]);
  294. if (!(portsc & PORT_RESET))
  295. break;
  296. }
  297. if (portsc & PORT_RESET) {
  298. /* force reset to complete */
  299. loop = 100 * 1000;
  300. writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
  301. &ehci_regs->port_status[port - 1]);
  302. do {
  303. udelay(1);
  304. portsc = readl(&ehci_regs->port_status[port-1]);
  305. } while ((portsc & PORT_RESET) && (--loop > 0));
  306. }
  307. /* Device went away? */
  308. if (!(portsc & PORT_CONNECT))
  309. return -ENOTCONN;
  310. /* bomb out completely if something weird happend */
  311. if ((portsc & PORT_CSC))
  312. return -EINVAL;
  313. /* If we've finished resetting, then break out of the loop */
  314. if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
  315. return 0;
  316. return -EBUSY;
  317. }
  318. static int __init ehci_wait_for_port(int port)
  319. {
  320. u32 status;
  321. int ret, reps;
  322. for (reps = 0; reps < 300; reps++) {
  323. status = readl(&ehci_regs->status);
  324. if (status & STS_PCD)
  325. break;
  326. dbgp_mdelay(1);
  327. }
  328. ret = ehci_reset_port(port);
  329. if (ret == 0)
  330. return 0;
  331. return -ENOTCONN;
  332. }
  333. typedef void (*set_debug_port_t)(int port);
  334. static void __init default_set_debug_port(int port)
  335. {
  336. }
  337. static set_debug_port_t __initdata set_debug_port = default_set_debug_port;
  338. static void __init nvidia_set_debug_port(int port)
  339. {
  340. u32 dword;
  341. dword = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  342. 0x74);
  343. dword &= ~(0x0f<<12);
  344. dword |= ((port & 0x0f)<<12);
  345. write_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func, 0x74,
  346. dword);
  347. dbgp_printk("set debug port to %d\n", port);
  348. }
  349. static void __init detect_set_debug_port(void)
  350. {
  351. u32 vendorid;
  352. vendorid = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  353. 0x00);
  354. if ((vendorid & 0xffff) == 0x10de) {
  355. dbgp_printk("using nvidia set_debug_port\n");
  356. set_debug_port = nvidia_set_debug_port;
  357. }
  358. }
  359. /* The code in early_ehci_bios_handoff() is derived from the usb pci
  360. * quirk initialization, but altered so as to use the early PCI
  361. * routines. */
  362. #define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
  363. #define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
  364. static void __init early_ehci_bios_handoff(void)
  365. {
  366. u32 hcc_params = readl(&ehci_caps->hcc_params);
  367. int offset = (hcc_params >> 8) & 0xff;
  368. u32 cap;
  369. int msec;
  370. if (!offset)
  371. return;
  372. cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
  373. ehci_dev.func, offset);
  374. dbgp_printk("dbgp: ehci BIOS state %08x\n", cap);
  375. if ((cap & 0xff) == 1 && (cap & EHCI_USBLEGSUP_BIOS)) {
  376. dbgp_printk("dbgp: BIOS handoff\n");
  377. write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
  378. ehci_dev.func, offset + 3, 1);
  379. }
  380. /* if boot firmware now owns EHCI, spin till it hands it over. */
  381. msec = 1000;
  382. while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
  383. mdelay(10);
  384. msec -= 10;
  385. cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
  386. ehci_dev.func, offset);
  387. }
  388. if (cap & EHCI_USBLEGSUP_BIOS) {
  389. /* well, possibly buggy BIOS... try to shut it down,
  390. * and hope nothing goes too wrong */
  391. dbgp_printk("dbgp: BIOS handoff failed: %08x\n", cap);
  392. write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
  393. ehci_dev.func, offset + 2, 0);
  394. }
  395. /* just in case, always disable EHCI SMIs */
  396. write_pci_config_byte(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  397. offset + EHCI_USBLEGCTLSTS, 0);
  398. }
  399. static int __init ehci_setup(void)
  400. {
  401. struct usb_debug_descriptor dbgp_desc;
  402. u32 cmd, ctrl, status, portsc, hcs_params;
  403. u32 debug_port, new_debug_port = 0, n_ports;
  404. u32 devnum;
  405. int ret, i;
  406. int loop;
  407. int port_map_tried;
  408. int playtimes = 3;
  409. early_ehci_bios_handoff();
  410. try_next_time:
  411. port_map_tried = 0;
  412. try_next_port:
  413. hcs_params = readl(&ehci_caps->hcs_params);
  414. debug_port = HCS_DEBUG_PORT(hcs_params);
  415. n_ports = HCS_N_PORTS(hcs_params);
  416. dbgp_printk("debug_port: %d\n", debug_port);
  417. dbgp_printk("n_ports: %d\n", n_ports);
  418. for (i = 1; i <= n_ports; i++) {
  419. portsc = readl(&ehci_regs->port_status[i-1]);
  420. dbgp_printk("portstatus%d: %08x\n", i, portsc);
  421. }
  422. if (port_map_tried && (new_debug_port != debug_port)) {
  423. if (--playtimes) {
  424. set_debug_port(new_debug_port);
  425. goto try_next_time;
  426. }
  427. return -1;
  428. }
  429. loop = 250 * 1000;
  430. /* Reset the EHCI controller */
  431. cmd = readl(&ehci_regs->command);
  432. cmd |= CMD_RESET;
  433. writel(cmd, &ehci_regs->command);
  434. do {
  435. cmd = readl(&ehci_regs->command);
  436. } while ((cmd & CMD_RESET) && (--loop > 0));
  437. if (!loop) {
  438. dbgp_printk("can not reset ehci\n");
  439. return -1;
  440. }
  441. dbgp_printk("ehci reset done\n");
  442. /* Claim ownership, but do not enable yet */
  443. ctrl = readl(&ehci_debug->control);
  444. ctrl |= DBGP_OWNER;
  445. ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
  446. writel(ctrl, &ehci_debug->control);
  447. udelay(1);
  448. /* Start the ehci running */
  449. cmd = readl(&ehci_regs->command);
  450. cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
  451. cmd |= CMD_RUN;
  452. writel(cmd, &ehci_regs->command);
  453. /* Ensure everything is routed to the EHCI */
  454. writel(FLAG_CF, &ehci_regs->configured_flag);
  455. /* Wait until the controller is no longer halted */
  456. loop = 10;
  457. do {
  458. status = readl(&ehci_regs->status);
  459. if (!(status & STS_HALT))
  460. break;
  461. udelay(1);
  462. } while (--loop > 0);
  463. if (!loop) {
  464. dbgp_printk("ehci can not be started\n");
  465. return -1;
  466. }
  467. dbgp_printk("ehci started\n");
  468. /* Wait for a device to show up in the debug port */
  469. ret = ehci_wait_for_port(debug_port);
  470. if (ret < 0) {
  471. dbgp_printk("No device found in debug port\n");
  472. goto next_debug_port;
  473. }
  474. dbgp_printk("ehci wait for port done\n");
  475. /* Enable the debug port */
  476. ctrl = readl(&ehci_debug->control);
  477. ctrl |= DBGP_CLAIM;
  478. writel(ctrl, &ehci_debug->control);
  479. ctrl = readl(&ehci_debug->control);
  480. if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
  481. dbgp_printk("No device in debug port\n");
  482. writel(ctrl & ~DBGP_CLAIM, &ehci_debug->control);
  483. goto err;
  484. }
  485. dbgp_printk("debug ported enabled\n");
  486. /* Completely transfer the debug device to the debug controller */
  487. portsc = readl(&ehci_regs->port_status[debug_port - 1]);
  488. portsc &= ~PORT_PE;
  489. writel(portsc, &ehci_regs->port_status[debug_port - 1]);
  490. dbgp_mdelay(100);
  491. /* Find the debug device and make it device number 127 */
  492. for (devnum = 0; devnum <= 127; devnum++) {
  493. ret = dbgp_control_msg(devnum,
  494. USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  495. USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
  496. &dbgp_desc, sizeof(dbgp_desc));
  497. if (ret > 0)
  498. break;
  499. }
  500. if (devnum > 127) {
  501. dbgp_printk("Could not find attached debug device\n");
  502. goto err;
  503. }
  504. if (ret < 0) {
  505. dbgp_printk("Attached device is not a debug device\n");
  506. goto err;
  507. }
  508. dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
  509. /* Move the device to 127 if it isn't already there */
  510. if (devnum != USB_DEBUG_DEVNUM) {
  511. ret = dbgp_control_msg(devnum,
  512. USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  513. USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
  514. if (ret < 0) {
  515. dbgp_printk("Could not move attached device to %d\n",
  516. USB_DEBUG_DEVNUM);
  517. goto err;
  518. }
  519. devnum = USB_DEBUG_DEVNUM;
  520. dbgp_printk("debug device renamed to 127\n");
  521. }
  522. /* Enable the debug interface */
  523. ret = dbgp_control_msg(USB_DEBUG_DEVNUM,
  524. USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  525. USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
  526. if (ret < 0) {
  527. dbgp_printk(" Could not enable the debug device\n");
  528. goto err;
  529. }
  530. dbgp_printk("debug interface enabled\n");
  531. /* Perform a small write to get the even/odd data state in sync
  532. */
  533. ret = dbgp_bulk_write(USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ", 1);
  534. if (ret < 0) {
  535. dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
  536. goto err;
  537. }
  538. dbgp_printk("small write doned\n");
  539. return 0;
  540. err:
  541. /* Things didn't work so remove my claim */
  542. ctrl = readl(&ehci_debug->control);
  543. ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
  544. writel(ctrl, &ehci_debug->control);
  545. return -1;
  546. next_debug_port:
  547. port_map_tried |= (1<<(debug_port - 1));
  548. new_debug_port = ((debug_port-1+1)%n_ports) + 1;
  549. if (port_map_tried != ((1<<n_ports) - 1)) {
  550. set_debug_port(new_debug_port);
  551. goto try_next_port;
  552. }
  553. if (--playtimes) {
  554. set_debug_port(new_debug_port);
  555. goto try_next_time;
  556. }
  557. return -1;
  558. }
  559. int __init early_dbgp_init(char *s)
  560. {
  561. u32 debug_port, bar, offset;
  562. u32 bus, slot, func, cap;
  563. void __iomem *ehci_bar;
  564. u32 dbgp_num;
  565. u32 bar_val;
  566. char *e;
  567. int ret;
  568. u8 byte;
  569. if (!early_pci_allowed())
  570. return -1;
  571. dbgp_num = 0;
  572. if (*s)
  573. dbgp_num = simple_strtoul(s, &e, 10);
  574. dbgp_printk("dbgp_num: %d\n", dbgp_num);
  575. cap = find_dbgp(dbgp_num, &bus, &slot, &func);
  576. if (!cap)
  577. return -1;
  578. dbgp_printk("Found EHCI debug port on %02x:%02x.%1x\n", bus, slot,
  579. func);
  580. debug_port = read_pci_config(bus, slot, func, cap);
  581. bar = (debug_port >> 29) & 0x7;
  582. bar = (bar * 4) + 0xc;
  583. offset = (debug_port >> 16) & 0xfff;
  584. dbgp_printk("bar: %02x offset: %03x\n", bar, offset);
  585. if (bar != PCI_BASE_ADDRESS_0) {
  586. dbgp_printk("only debug ports on bar 1 handled.\n");
  587. return -1;
  588. }
  589. bar_val = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
  590. dbgp_printk("bar_val: %02x offset: %03x\n", bar_val, offset);
  591. if (bar_val & ~PCI_BASE_ADDRESS_MEM_MASK) {
  592. dbgp_printk("only simple 32bit mmio bars supported\n");
  593. return -1;
  594. }
  595. /* double check if the mem space is enabled */
  596. byte = read_pci_config_byte(bus, slot, func, 0x04);
  597. if (!(byte & 0x2)) {
  598. byte |= 0x02;
  599. write_pci_config_byte(bus, slot, func, 0x04, byte);
  600. dbgp_printk("mmio for ehci enabled\n");
  601. }
  602. /*
  603. * FIXME I don't have the bar size so just guess PAGE_SIZE is more
  604. * than enough. 1K is the biggest I have seen.
  605. */
  606. set_fixmap_nocache(FIX_DBGP_BASE, bar_val & PAGE_MASK);
  607. ehci_bar = (void __iomem *)__fix_to_virt(FIX_DBGP_BASE);
  608. ehci_bar += bar_val & ~PAGE_MASK;
  609. dbgp_printk("ehci_bar: %p\n", ehci_bar);
  610. ehci_caps = ehci_bar;
  611. ehci_regs = ehci_bar + HC_LENGTH(readl(&ehci_caps->hc_capbase));
  612. ehci_debug = ehci_bar + offset;
  613. ehci_dev.bus = bus;
  614. ehci_dev.slot = slot;
  615. ehci_dev.func = func;
  616. detect_set_debug_port();
  617. ret = ehci_setup();
  618. if (ret < 0) {
  619. dbgp_printk("ehci_setup failed\n");
  620. ehci_debug = NULL;
  621. return -1;
  622. }
  623. return 0;
  624. }
  625. static void early_dbgp_write(struct console *con, const char *str, u32 n)
  626. {
  627. int chunk, ret;
  628. char buf[DBGP_MAX_PACKET];
  629. int use_cr = 0;
  630. if (!ehci_debug)
  631. return;
  632. while (n > 0) {
  633. for (chunk = 0; chunk < DBGP_MAX_PACKET && n > 0;
  634. str++, chunk++, n--) {
  635. if (!use_cr && *str == '\n') {
  636. use_cr = 1;
  637. buf[chunk] = '\r';
  638. str--;
  639. n++;
  640. continue;
  641. }
  642. if (use_cr)
  643. use_cr = 0;
  644. buf[chunk] = *str;
  645. }
  646. ret = dbgp_bulk_write(USB_DEBUG_DEVNUM,
  647. dbgp_endpoint_out, buf, chunk);
  648. }
  649. }
  650. struct console early_dbgp_console = {
  651. .name = "earlydbg",
  652. .write = early_dbgp_write,
  653. .flags = CON_PRINTBUFFER,
  654. .index = -1,
  655. };