ehci-dbgp.c 16 KB

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