ehci-dbgp.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*
  2. * Standalone EHCI usb debug driver
  3. *
  4. * Originally written by:
  5. * Eric W. Biederman" <ebiederm@xmission.com> and
  6. * Yinghai Lu <yhlu.kernel@gmail.com>
  7. *
  8. * Changes for early/late printk and HW errata:
  9. * Jason Wessel <jason.wessel@windriver.com>
  10. * Copyright (C) 2009 Wind River Systems, Inc.
  11. *
  12. */
  13. #include <linux/console.h>
  14. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/pci_regs.h>
  17. #include <linux/pci_ids.h>
  18. #include <linux/usb/ch9.h>
  19. #include <linux/usb/ehci_def.h>
  20. #include <linux/delay.h>
  21. #include <linux/serial_core.h>
  22. #include <linux/kconfig.h>
  23. #include <linux/kgdb.h>
  24. #include <linux/kthread.h>
  25. #include <asm/io.h>
  26. #include <asm/pci-direct.h>
  27. #include <asm/fixmap.h>
  28. /* The code here is intended to talk directly to the EHCI debug port
  29. * and does not require that you have any kind of USB host controller
  30. * drivers or USB device drivers compiled into the kernel.
  31. *
  32. * If you make a change to anything in here, the following test cases
  33. * need to pass where a USB debug device works in the following
  34. * configurations.
  35. *
  36. * 1. boot args: earlyprintk=dbgp
  37. * o kernel compiled with # CONFIG_USB_EHCI_HCD is not set
  38. * o kernel compiled with CONFIG_USB_EHCI_HCD=y
  39. * 2. boot args: earlyprintk=dbgp,keep
  40. * o kernel compiled with # CONFIG_USB_EHCI_HCD is not set
  41. * o kernel compiled with CONFIG_USB_EHCI_HCD=y
  42. * 3. boot args: earlyprintk=dbgp console=ttyUSB0
  43. * o kernel has CONFIG_USB_EHCI_HCD=y and
  44. * CONFIG_USB_SERIAL_DEBUG=y
  45. * 4. boot args: earlyprintk=vga,dbgp
  46. * o kernel compiled with # CONFIG_USB_EHCI_HCD is not set
  47. * o kernel compiled with CONFIG_USB_EHCI_HCD=y
  48. *
  49. * For the 4th configuration you can turn on or off the DBGP_DEBUG
  50. * such that you can debug the dbgp device's driver code.
  51. */
  52. static int dbgp_phys_port = 1;
  53. static struct ehci_caps __iomem *ehci_caps;
  54. static struct ehci_regs __iomem *ehci_regs;
  55. static struct ehci_dbg_port __iomem *ehci_debug;
  56. static int dbgp_not_safe; /* Cannot use debug device during ehci reset */
  57. static unsigned int dbgp_endpoint_out;
  58. static unsigned int dbgp_endpoint_in;
  59. struct ehci_dev {
  60. u32 bus;
  61. u32 slot;
  62. u32 func;
  63. };
  64. static struct ehci_dev ehci_dev;
  65. #define USB_DEBUG_DEVNUM 127
  66. #ifdef DBGP_DEBUG
  67. #define dbgp_printk printk
  68. static void dbgp_ehci_status(char *str)
  69. {
  70. if (!ehci_debug)
  71. return;
  72. dbgp_printk("dbgp: %s\n", str);
  73. dbgp_printk(" Debug control: %08x", readl(&ehci_debug->control));
  74. dbgp_printk(" ehci cmd : %08x", readl(&ehci_regs->command));
  75. dbgp_printk(" ehci conf flg: %08x\n",
  76. readl(&ehci_regs->configured_flag));
  77. dbgp_printk(" ehci status : %08x", readl(&ehci_regs->status));
  78. dbgp_printk(" ehci portsc : %08x\n",
  79. readl(&ehci_regs->port_status[dbgp_phys_port - 1]));
  80. }
  81. #else
  82. static inline void dbgp_ehci_status(char *str) { }
  83. static inline void dbgp_printk(const char *fmt, ...) { }
  84. #endif
  85. static inline u32 dbgp_len_update(u32 x, u32 len)
  86. {
  87. return (x & ~0x0f) | (len & 0x0f);
  88. }
  89. #ifdef CONFIG_KGDB
  90. static struct kgdb_io kgdbdbgp_io_ops;
  91. #define dbgp_kgdb_mode (dbg_io_ops == &kgdbdbgp_io_ops)
  92. #else
  93. #define dbgp_kgdb_mode (0)
  94. #endif
  95. /* Local version of HC_LENGTH macro as ehci struct is not available here */
  96. #define EARLY_HC_LENGTH(p) (0x00ff & (p)) /* bits 7 : 0 */
  97. /*
  98. * USB Packet IDs (PIDs)
  99. */
  100. /* token */
  101. #define USB_PID_OUT 0xe1
  102. #define USB_PID_IN 0x69
  103. #define USB_PID_SOF 0xa5
  104. #define USB_PID_SETUP 0x2d
  105. /* handshake */
  106. #define USB_PID_ACK 0xd2
  107. #define USB_PID_NAK 0x5a
  108. #define USB_PID_STALL 0x1e
  109. #define USB_PID_NYET 0x96
  110. /* data */
  111. #define USB_PID_DATA0 0xc3
  112. #define USB_PID_DATA1 0x4b
  113. #define USB_PID_DATA2 0x87
  114. #define USB_PID_MDATA 0x0f
  115. /* Special */
  116. #define USB_PID_PREAMBLE 0x3c
  117. #define USB_PID_ERR 0x3c
  118. #define USB_PID_SPLIT 0x78
  119. #define USB_PID_PING 0xb4
  120. #define USB_PID_UNDEF_0 0xf0
  121. #define USB_PID_DATA_TOGGLE 0x88
  122. #define DBGP_CLAIM (DBGP_OWNER | DBGP_ENABLED | DBGP_INUSE)
  123. #define PCI_CAP_ID_EHCI_DEBUG 0xa
  124. #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
  125. #define HUB_SHORT_RESET_TIME 10
  126. #define HUB_LONG_RESET_TIME 200
  127. #define HUB_RESET_TIMEOUT 500
  128. #define DBGP_MAX_PACKET 8
  129. #define DBGP_TIMEOUT (250 * 1000)
  130. #define DBGP_LOOPS 1000
  131. static inline u32 dbgp_pid_write_update(u32 x, u32 tok)
  132. {
  133. static int data0 = USB_PID_DATA1;
  134. data0 ^= USB_PID_DATA_TOGGLE;
  135. return (x & 0xffff0000) | (data0 << 8) | (tok & 0xff);
  136. }
  137. static inline u32 dbgp_pid_read_update(u32 x, u32 tok)
  138. {
  139. return (x & 0xffff0000) | (USB_PID_DATA0 << 8) | (tok & 0xff);
  140. }
  141. static int dbgp_wait_until_complete(void)
  142. {
  143. u32 ctrl;
  144. int loop = DBGP_TIMEOUT;
  145. do {
  146. ctrl = readl(&ehci_debug->control);
  147. /* Stop when the transaction is finished */
  148. if (ctrl & DBGP_DONE)
  149. break;
  150. udelay(1);
  151. } while (--loop > 0);
  152. if (!loop)
  153. return -DBGP_TIMEOUT;
  154. /*
  155. * Now that we have observed the completed transaction,
  156. * clear the done bit.
  157. */
  158. writel(ctrl | DBGP_DONE, &ehci_debug->control);
  159. return (ctrl & DBGP_ERROR) ? -DBGP_ERRCODE(ctrl) : DBGP_LEN(ctrl);
  160. }
  161. static inline void dbgp_mdelay(int ms)
  162. {
  163. int i;
  164. while (ms--) {
  165. for (i = 0; i < 1000; i++)
  166. outb(0x1, 0x80);
  167. }
  168. }
  169. static void dbgp_breath(void)
  170. {
  171. /* Sleep to give the debug port a chance to breathe */
  172. }
  173. static int dbgp_wait_until_done(unsigned ctrl, int loop)
  174. {
  175. u32 pids, lpid;
  176. int ret;
  177. retry:
  178. writel(ctrl | DBGP_GO, &ehci_debug->control);
  179. ret = dbgp_wait_until_complete();
  180. pids = readl(&ehci_debug->pids);
  181. lpid = DBGP_PID_GET(pids);
  182. if (ret < 0) {
  183. /* A -DBGP_TIMEOUT failure here means the device has
  184. * failed, perhaps because it was unplugged, in which
  185. * case we do not want to hang the system so the dbgp
  186. * will be marked as unsafe to use. EHCI reset is the
  187. * only way to recover if you unplug the dbgp device.
  188. */
  189. if (ret == -DBGP_TIMEOUT && !dbgp_not_safe)
  190. dbgp_not_safe = 1;
  191. if (ret == -DBGP_ERR_BAD && --loop > 0)
  192. goto retry;
  193. return ret;
  194. }
  195. /*
  196. * If the port is getting full or it has dropped data
  197. * start pacing ourselves, not necessary but it's friendly.
  198. */
  199. if ((lpid == USB_PID_NAK) || (lpid == USB_PID_NYET))
  200. dbgp_breath();
  201. /* If I get a NACK reissue the transmission */
  202. if (lpid == USB_PID_NAK) {
  203. if (--loop > 0)
  204. goto retry;
  205. }
  206. return ret;
  207. }
  208. static inline void dbgp_set_data(const void *buf, int size)
  209. {
  210. const unsigned char *bytes = buf;
  211. u32 lo, hi;
  212. int i;
  213. lo = hi = 0;
  214. for (i = 0; i < 4 && i < size; i++)
  215. lo |= bytes[i] << (8*i);
  216. for (; i < 8 && i < size; i++)
  217. hi |= bytes[i] << (8*(i - 4));
  218. writel(lo, &ehci_debug->data03);
  219. writel(hi, &ehci_debug->data47);
  220. }
  221. static inline void dbgp_get_data(void *buf, int size)
  222. {
  223. unsigned char *bytes = buf;
  224. u32 lo, hi;
  225. int i;
  226. lo = readl(&ehci_debug->data03);
  227. hi = readl(&ehci_debug->data47);
  228. for (i = 0; i < 4 && i < size; i++)
  229. bytes[i] = (lo >> (8*i)) & 0xff;
  230. for (; i < 8 && i < size; i++)
  231. bytes[i] = (hi >> (8*(i - 4))) & 0xff;
  232. }
  233. static int dbgp_bulk_write(unsigned devnum, unsigned endpoint,
  234. const char *bytes, int size)
  235. {
  236. int ret;
  237. u32 addr;
  238. u32 pids, ctrl;
  239. if (size > DBGP_MAX_PACKET)
  240. return -1;
  241. addr = DBGP_EPADDR(devnum, endpoint);
  242. pids = readl(&ehci_debug->pids);
  243. pids = dbgp_pid_write_update(pids, USB_PID_OUT);
  244. ctrl = readl(&ehci_debug->control);
  245. ctrl = dbgp_len_update(ctrl, size);
  246. ctrl |= DBGP_OUT;
  247. ctrl |= DBGP_GO;
  248. dbgp_set_data(bytes, size);
  249. writel(addr, &ehci_debug->address);
  250. writel(pids, &ehci_debug->pids);
  251. ret = dbgp_wait_until_done(ctrl, DBGP_LOOPS);
  252. return ret;
  253. }
  254. static int dbgp_bulk_read(unsigned devnum, unsigned endpoint, void *data,
  255. int size, int loops)
  256. {
  257. u32 pids, addr, ctrl;
  258. int ret;
  259. if (size > DBGP_MAX_PACKET)
  260. return -1;
  261. addr = DBGP_EPADDR(devnum, endpoint);
  262. pids = readl(&ehci_debug->pids);
  263. pids = dbgp_pid_read_update(pids, USB_PID_IN);
  264. ctrl = readl(&ehci_debug->control);
  265. ctrl = dbgp_len_update(ctrl, size);
  266. ctrl &= ~DBGP_OUT;
  267. ctrl |= DBGP_GO;
  268. writel(addr, &ehci_debug->address);
  269. writel(pids, &ehci_debug->pids);
  270. ret = dbgp_wait_until_done(ctrl, loops);
  271. if (ret < 0)
  272. return ret;
  273. if (size > ret)
  274. size = ret;
  275. dbgp_get_data(data, size);
  276. return ret;
  277. }
  278. static int dbgp_control_msg(unsigned devnum, int requesttype,
  279. int request, int value, int index, void *data, int size)
  280. {
  281. u32 pids, addr, ctrl;
  282. struct usb_ctrlrequest req;
  283. int read;
  284. int ret;
  285. read = (requesttype & USB_DIR_IN) != 0;
  286. if (size > (read ? DBGP_MAX_PACKET : 0))
  287. return -1;
  288. /* Compute the control message */
  289. req.bRequestType = requesttype;
  290. req.bRequest = request;
  291. req.wValue = cpu_to_le16(value);
  292. req.wIndex = cpu_to_le16(index);
  293. req.wLength = cpu_to_le16(size);
  294. pids = DBGP_PID_SET(USB_PID_DATA0, USB_PID_SETUP);
  295. addr = DBGP_EPADDR(devnum, 0);
  296. ctrl = readl(&ehci_debug->control);
  297. ctrl = dbgp_len_update(ctrl, sizeof(req));
  298. ctrl |= DBGP_OUT;
  299. ctrl |= DBGP_GO;
  300. /* Send the setup message */
  301. dbgp_set_data(&req, sizeof(req));
  302. writel(addr, &ehci_debug->address);
  303. writel(pids, &ehci_debug->pids);
  304. ret = dbgp_wait_until_done(ctrl, DBGP_LOOPS);
  305. if (ret < 0)
  306. return ret;
  307. /* Read the result */
  308. return dbgp_bulk_read(devnum, 0, data, size, DBGP_LOOPS);
  309. }
  310. /* Find a PCI capability */
  311. static u32 __init find_cap(u32 num, u32 slot, u32 func, int cap)
  312. {
  313. u8 pos;
  314. int bytes;
  315. if (!(read_pci_config_16(num, slot, func, PCI_STATUS) &
  316. PCI_STATUS_CAP_LIST))
  317. return 0;
  318. pos = read_pci_config_byte(num, slot, func, PCI_CAPABILITY_LIST);
  319. for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
  320. u8 id;
  321. pos &= ~3;
  322. id = read_pci_config_byte(num, slot, func, pos+PCI_CAP_LIST_ID);
  323. if (id == 0xff)
  324. break;
  325. if (id == cap)
  326. return pos;
  327. pos = read_pci_config_byte(num, slot, func,
  328. pos+PCI_CAP_LIST_NEXT);
  329. }
  330. return 0;
  331. }
  332. static u32 __init __find_dbgp(u32 bus, u32 slot, u32 func)
  333. {
  334. u32 class;
  335. class = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
  336. if ((class >> 8) != PCI_CLASS_SERIAL_USB_EHCI)
  337. return 0;
  338. return find_cap(bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);
  339. }
  340. static u32 __init find_dbgp(int ehci_num, u32 *rbus, u32 *rslot, u32 *rfunc)
  341. {
  342. u32 bus, slot, func;
  343. for (bus = 0; bus < 256; bus++) {
  344. for (slot = 0; slot < 32; slot++) {
  345. for (func = 0; func < 8; func++) {
  346. unsigned cap;
  347. cap = __find_dbgp(bus, slot, func);
  348. if (!cap)
  349. continue;
  350. if (ehci_num-- != 0)
  351. continue;
  352. *rbus = bus;
  353. *rslot = slot;
  354. *rfunc = func;
  355. return cap;
  356. }
  357. }
  358. }
  359. return 0;
  360. }
  361. static int dbgp_ehci_startup(void)
  362. {
  363. u32 ctrl, cmd, status;
  364. int loop;
  365. /* Claim ownership, but do not enable yet */
  366. ctrl = readl(&ehci_debug->control);
  367. ctrl |= DBGP_OWNER;
  368. ctrl &= ~(DBGP_ENABLED | DBGP_INUSE);
  369. writel(ctrl, &ehci_debug->control);
  370. udelay(1);
  371. dbgp_ehci_status("EHCI startup");
  372. /* Start the ehci running */
  373. cmd = readl(&ehci_regs->command);
  374. cmd &= ~(CMD_LRESET | CMD_IAAD | CMD_PSE | CMD_ASE | CMD_RESET);
  375. cmd |= CMD_RUN;
  376. writel(cmd, &ehci_regs->command);
  377. /* Ensure everything is routed to the EHCI */
  378. writel(FLAG_CF, &ehci_regs->configured_flag);
  379. /* Wait until the controller is no longer halted */
  380. loop = 1000;
  381. do {
  382. status = readl(&ehci_regs->status);
  383. if (!(status & STS_HALT))
  384. break;
  385. udelay(1);
  386. } while (--loop > 0);
  387. if (!loop) {
  388. dbgp_printk("ehci can not be started\n");
  389. return -ENODEV;
  390. }
  391. dbgp_printk("ehci started\n");
  392. return 0;
  393. }
  394. static int dbgp_ehci_controller_reset(void)
  395. {
  396. int loop = 250 * 1000;
  397. u32 cmd;
  398. /* Reset the EHCI controller */
  399. cmd = readl(&ehci_regs->command);
  400. cmd |= CMD_RESET;
  401. writel(cmd, &ehci_regs->command);
  402. do {
  403. cmd = readl(&ehci_regs->command);
  404. } while ((cmd & CMD_RESET) && (--loop > 0));
  405. if (!loop) {
  406. dbgp_printk("can not reset ehci\n");
  407. return -1;
  408. }
  409. dbgp_ehci_status("ehci reset done");
  410. return 0;
  411. }
  412. static int ehci_wait_for_port(int port);
  413. /* Return 0 on success
  414. * Return -ENODEV for any general failure
  415. * Return -EIO if wait for port fails
  416. */
  417. static int _dbgp_external_startup(void)
  418. {
  419. int devnum;
  420. struct usb_debug_descriptor dbgp_desc;
  421. int ret;
  422. u32 ctrl, portsc, cmd;
  423. int dbg_port = dbgp_phys_port;
  424. int tries = 3;
  425. int reset_port_tries = 1;
  426. int try_hard_once = 1;
  427. try_port_reset_again:
  428. ret = dbgp_ehci_startup();
  429. if (ret)
  430. return ret;
  431. /* Wait for a device to show up in the debug port */
  432. ret = ehci_wait_for_port(dbg_port);
  433. if (ret < 0) {
  434. portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
  435. if (!(portsc & PORT_CONNECT) && try_hard_once) {
  436. /* Last ditch effort to try to force enable
  437. * the debug device by using the packet test
  438. * ehci command to try and wake it up. */
  439. try_hard_once = 0;
  440. cmd = readl(&ehci_regs->command);
  441. cmd &= ~CMD_RUN;
  442. writel(cmd, &ehci_regs->command);
  443. portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
  444. portsc |= PORT_TEST_PKT;
  445. writel(portsc, &ehci_regs->port_status[dbg_port - 1]);
  446. dbgp_ehci_status("Trying to force debug port online");
  447. mdelay(50);
  448. dbgp_ehci_controller_reset();
  449. goto try_port_reset_again;
  450. } else if (reset_port_tries--) {
  451. goto try_port_reset_again;
  452. }
  453. dbgp_printk("No device found in debug port\n");
  454. return -EIO;
  455. }
  456. dbgp_ehci_status("wait for port done");
  457. /* Enable the debug port */
  458. ctrl = readl(&ehci_debug->control);
  459. ctrl |= DBGP_CLAIM;
  460. writel(ctrl, &ehci_debug->control);
  461. ctrl = readl(&ehci_debug->control);
  462. if ((ctrl & DBGP_CLAIM) != DBGP_CLAIM) {
  463. dbgp_printk("No device in debug port\n");
  464. writel(ctrl & ~DBGP_CLAIM, &ehci_debug->control);
  465. return -ENODEV;
  466. }
  467. dbgp_ehci_status("debug ported enabled");
  468. /* Completely transfer the debug device to the debug controller */
  469. portsc = readl(&ehci_regs->port_status[dbg_port - 1]);
  470. portsc &= ~PORT_PE;
  471. writel(portsc, &ehci_regs->port_status[dbg_port - 1]);
  472. dbgp_mdelay(100);
  473. try_again:
  474. /* Find the debug device and make it device number 127 */
  475. for (devnum = 0; devnum <= 127; devnum++) {
  476. ret = dbgp_control_msg(devnum,
  477. USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  478. USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
  479. &dbgp_desc, sizeof(dbgp_desc));
  480. if (ret > 0)
  481. break;
  482. }
  483. if (devnum > 127) {
  484. dbgp_printk("Could not find attached debug device\n");
  485. goto err;
  486. }
  487. dbgp_endpoint_out = dbgp_desc.bDebugOutEndpoint;
  488. dbgp_endpoint_in = dbgp_desc.bDebugInEndpoint;
  489. /* Move the device to 127 if it isn't already there */
  490. if (devnum != USB_DEBUG_DEVNUM) {
  491. ret = dbgp_control_msg(devnum,
  492. USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  493. USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
  494. if (ret < 0) {
  495. dbgp_printk("Could not move attached device to %d\n",
  496. USB_DEBUG_DEVNUM);
  497. goto err;
  498. }
  499. devnum = USB_DEBUG_DEVNUM;
  500. dbgp_printk("debug device renamed to 127\n");
  501. }
  502. /* Enable the debug interface */
  503. ret = dbgp_control_msg(USB_DEBUG_DEVNUM,
  504. USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  505. USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
  506. if (ret < 0) {
  507. dbgp_printk(" Could not enable the debug device\n");
  508. goto err;
  509. }
  510. dbgp_printk("debug interface enabled\n");
  511. /* Perform a small write to get the even/odd data state in sync
  512. */
  513. ret = dbgp_bulk_write(USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ", 1);
  514. if (ret < 0) {
  515. dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
  516. goto err;
  517. }
  518. dbgp_printk("small write done\n");
  519. dbgp_not_safe = 0;
  520. return 0;
  521. err:
  522. if (tries--)
  523. goto try_again;
  524. return -ENODEV;
  525. }
  526. static int ehci_reset_port(int port)
  527. {
  528. u32 portsc;
  529. u32 delay_time, delay;
  530. int loop;
  531. dbgp_ehci_status("reset port");
  532. /* Reset the usb debug port */
  533. portsc = readl(&ehci_regs->port_status[port - 1]);
  534. portsc &= ~PORT_PE;
  535. portsc |= PORT_RESET;
  536. writel(portsc, &ehci_regs->port_status[port - 1]);
  537. delay = HUB_ROOT_RESET_TIME;
  538. for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
  539. delay_time += delay) {
  540. dbgp_mdelay(delay);
  541. portsc = readl(&ehci_regs->port_status[port - 1]);
  542. if (!(portsc & PORT_RESET))
  543. break;
  544. }
  545. if (portsc & PORT_RESET) {
  546. /* force reset to complete */
  547. loop = 100 * 1000;
  548. writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
  549. &ehci_regs->port_status[port - 1]);
  550. do {
  551. udelay(1);
  552. portsc = readl(&ehci_regs->port_status[port-1]);
  553. } while ((portsc & PORT_RESET) && (--loop > 0));
  554. }
  555. /* Device went away? */
  556. if (!(portsc & PORT_CONNECT))
  557. return -ENOTCONN;
  558. /* bomb out completely if something weird happened */
  559. if ((portsc & PORT_CSC))
  560. return -EINVAL;
  561. /* If we've finished resetting, then break out of the loop */
  562. if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
  563. return 0;
  564. return -EBUSY;
  565. }
  566. static int ehci_wait_for_port(int port)
  567. {
  568. u32 status;
  569. int ret, reps;
  570. for (reps = 0; reps < 300; reps++) {
  571. status = readl(&ehci_regs->status);
  572. if (status & STS_PCD)
  573. break;
  574. dbgp_mdelay(1);
  575. }
  576. ret = ehci_reset_port(port);
  577. if (ret == 0)
  578. return 0;
  579. return -ENOTCONN;
  580. }
  581. typedef void (*set_debug_port_t)(int port);
  582. static void __init default_set_debug_port(int port)
  583. {
  584. }
  585. static set_debug_port_t __initdata set_debug_port = default_set_debug_port;
  586. static void __init nvidia_set_debug_port(int port)
  587. {
  588. u32 dword;
  589. dword = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  590. 0x74);
  591. dword &= ~(0x0f<<12);
  592. dword |= ((port & 0x0f)<<12);
  593. write_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func, 0x74,
  594. dword);
  595. dbgp_printk("set debug port to %d\n", port);
  596. }
  597. static void __init detect_set_debug_port(void)
  598. {
  599. u32 vendorid;
  600. vendorid = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  601. 0x00);
  602. if ((vendorid & 0xffff) == 0x10de) {
  603. dbgp_printk("using nvidia set_debug_port\n");
  604. set_debug_port = nvidia_set_debug_port;
  605. }
  606. }
  607. /* The code in early_ehci_bios_handoff() is derived from the usb pci
  608. * quirk initialization, but altered so as to use the early PCI
  609. * routines. */
  610. #define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
  611. #define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
  612. static void __init early_ehci_bios_handoff(void)
  613. {
  614. u32 hcc_params = readl(&ehci_caps->hcc_params);
  615. int offset = (hcc_params >> 8) & 0xff;
  616. u32 cap;
  617. int msec;
  618. if (!offset)
  619. return;
  620. cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
  621. ehci_dev.func, offset);
  622. dbgp_printk("dbgp: ehci BIOS state %08x\n", cap);
  623. if ((cap & 0xff) == 1 && (cap & EHCI_USBLEGSUP_BIOS)) {
  624. dbgp_printk("dbgp: BIOS handoff\n");
  625. write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
  626. ehci_dev.func, offset + 3, 1);
  627. }
  628. /* if boot firmware now owns EHCI, spin till it hands it over. */
  629. msec = 1000;
  630. while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
  631. mdelay(10);
  632. msec -= 10;
  633. cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
  634. ehci_dev.func, offset);
  635. }
  636. if (cap & EHCI_USBLEGSUP_BIOS) {
  637. /* well, possibly buggy BIOS... try to shut it down,
  638. * and hope nothing goes too wrong */
  639. dbgp_printk("dbgp: BIOS handoff failed: %08x\n", cap);
  640. write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
  641. ehci_dev.func, offset + 2, 0);
  642. }
  643. /* just in case, always disable EHCI SMIs */
  644. write_pci_config_byte(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  645. offset + EHCI_USBLEGCTLSTS, 0);
  646. }
  647. static int __init ehci_setup(void)
  648. {
  649. u32 ctrl, portsc, hcs_params;
  650. u32 debug_port, new_debug_port = 0, n_ports;
  651. int ret, i;
  652. int port_map_tried;
  653. int playtimes = 3;
  654. early_ehci_bios_handoff();
  655. try_next_time:
  656. port_map_tried = 0;
  657. try_next_port:
  658. hcs_params = readl(&ehci_caps->hcs_params);
  659. debug_port = HCS_DEBUG_PORT(hcs_params);
  660. dbgp_phys_port = debug_port;
  661. n_ports = HCS_N_PORTS(hcs_params);
  662. dbgp_printk("debug_port: %d\n", debug_port);
  663. dbgp_printk("n_ports: %d\n", n_ports);
  664. dbgp_ehci_status("");
  665. for (i = 1; i <= n_ports; i++) {
  666. portsc = readl(&ehci_regs->port_status[i-1]);
  667. dbgp_printk("portstatus%d: %08x\n", i, portsc);
  668. }
  669. if (port_map_tried && (new_debug_port != debug_port)) {
  670. if (--playtimes) {
  671. set_debug_port(new_debug_port);
  672. goto try_next_time;
  673. }
  674. return -1;
  675. }
  676. /* Only reset the controller if it is not already in the
  677. * configured state */
  678. if (!(readl(&ehci_regs->configured_flag) & FLAG_CF)) {
  679. if (dbgp_ehci_controller_reset() != 0)
  680. return -1;
  681. } else {
  682. dbgp_ehci_status("ehci skip - already configured");
  683. }
  684. ret = _dbgp_external_startup();
  685. if (ret == -EIO)
  686. goto next_debug_port;
  687. if (ret < 0) {
  688. /* Things didn't work so remove my claim */
  689. ctrl = readl(&ehci_debug->control);
  690. ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
  691. writel(ctrl, &ehci_debug->control);
  692. return -1;
  693. }
  694. return 0;
  695. next_debug_port:
  696. port_map_tried |= (1<<(debug_port - 1));
  697. new_debug_port = ((debug_port-1+1)%n_ports) + 1;
  698. if (port_map_tried != ((1<<n_ports) - 1)) {
  699. set_debug_port(new_debug_port);
  700. goto try_next_port;
  701. }
  702. if (--playtimes) {
  703. set_debug_port(new_debug_port);
  704. goto try_next_time;
  705. }
  706. return -1;
  707. }
  708. int __init early_dbgp_init(char *s)
  709. {
  710. u32 debug_port, bar, offset;
  711. u32 bus, slot, func, cap;
  712. void __iomem *ehci_bar;
  713. u32 dbgp_num;
  714. u32 bar_val;
  715. char *e;
  716. int ret;
  717. u8 byte;
  718. if (!early_pci_allowed())
  719. return -1;
  720. dbgp_num = 0;
  721. if (*s)
  722. dbgp_num = simple_strtoul(s, &e, 10);
  723. dbgp_printk("dbgp_num: %d\n", dbgp_num);
  724. cap = find_dbgp(dbgp_num, &bus, &slot, &func);
  725. if (!cap)
  726. return -1;
  727. dbgp_printk("Found EHCI debug port on %02x:%02x.%1x\n", bus, slot,
  728. func);
  729. debug_port = read_pci_config(bus, slot, func, cap);
  730. bar = (debug_port >> 29) & 0x7;
  731. bar = (bar * 4) + 0xc;
  732. offset = (debug_port >> 16) & 0xfff;
  733. dbgp_printk("bar: %02x offset: %03x\n", bar, offset);
  734. if (bar != PCI_BASE_ADDRESS_0) {
  735. dbgp_printk("only debug ports on bar 1 handled.\n");
  736. return -1;
  737. }
  738. bar_val = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
  739. dbgp_printk("bar_val: %02x offset: %03x\n", bar_val, offset);
  740. if (bar_val & ~PCI_BASE_ADDRESS_MEM_MASK) {
  741. dbgp_printk("only simple 32bit mmio bars supported\n");
  742. return -1;
  743. }
  744. /* double check if the mem space is enabled */
  745. byte = read_pci_config_byte(bus, slot, func, 0x04);
  746. if (!(byte & 0x2)) {
  747. byte |= 0x02;
  748. write_pci_config_byte(bus, slot, func, 0x04, byte);
  749. dbgp_printk("mmio for ehci enabled\n");
  750. }
  751. /*
  752. * FIXME I don't have the bar size so just guess PAGE_SIZE is more
  753. * than enough. 1K is the biggest I have seen.
  754. */
  755. set_fixmap_nocache(FIX_DBGP_BASE, bar_val & PAGE_MASK);
  756. ehci_bar = (void __iomem *)__fix_to_virt(FIX_DBGP_BASE);
  757. ehci_bar += bar_val & ~PAGE_MASK;
  758. dbgp_printk("ehci_bar: %p\n", ehci_bar);
  759. ehci_caps = ehci_bar;
  760. ehci_regs = ehci_bar + EARLY_HC_LENGTH(readl(&ehci_caps->hc_capbase));
  761. ehci_debug = ehci_bar + offset;
  762. ehci_dev.bus = bus;
  763. ehci_dev.slot = slot;
  764. ehci_dev.func = func;
  765. detect_set_debug_port();
  766. ret = ehci_setup();
  767. if (ret < 0) {
  768. dbgp_printk("ehci_setup failed\n");
  769. ehci_debug = NULL;
  770. return -1;
  771. }
  772. dbgp_ehci_status("early_init_complete");
  773. return 0;
  774. }
  775. static void early_dbgp_write(struct console *con, const char *str, u32 n)
  776. {
  777. int chunk, ret;
  778. char buf[DBGP_MAX_PACKET];
  779. int use_cr = 0;
  780. u32 cmd, ctrl;
  781. int reset_run = 0;
  782. if (!ehci_debug || dbgp_not_safe)
  783. return;
  784. cmd = readl(&ehci_regs->command);
  785. if (unlikely(!(cmd & CMD_RUN))) {
  786. /* If the ehci controller is not in the run state do extended
  787. * checks to see if the acpi or some other initialization also
  788. * reset the ehci debug port */
  789. ctrl = readl(&ehci_debug->control);
  790. if (!(ctrl & DBGP_ENABLED)) {
  791. dbgp_not_safe = 1;
  792. _dbgp_external_startup();
  793. } else {
  794. cmd |= CMD_RUN;
  795. writel(cmd, &ehci_regs->command);
  796. reset_run = 1;
  797. }
  798. }
  799. while (n > 0) {
  800. for (chunk = 0; chunk < DBGP_MAX_PACKET && n > 0;
  801. str++, chunk++, n--) {
  802. if (!use_cr && *str == '\n') {
  803. use_cr = 1;
  804. buf[chunk] = '\r';
  805. str--;
  806. n++;
  807. continue;
  808. }
  809. if (use_cr)
  810. use_cr = 0;
  811. buf[chunk] = *str;
  812. }
  813. if (chunk > 0) {
  814. ret = dbgp_bulk_write(USB_DEBUG_DEVNUM,
  815. dbgp_endpoint_out, buf, chunk);
  816. }
  817. }
  818. if (unlikely(reset_run)) {
  819. cmd = readl(&ehci_regs->command);
  820. cmd &= ~CMD_RUN;
  821. writel(cmd, &ehci_regs->command);
  822. }
  823. }
  824. struct console early_dbgp_console = {
  825. .name = "earlydbg",
  826. .write = early_dbgp_write,
  827. .flags = CON_PRINTBUFFER,
  828. .index = -1,
  829. };
  830. #if IS_ENABLED(CONFIG_USB)
  831. int dbgp_reset_prep(struct usb_hcd *hcd)
  832. {
  833. int ret = xen_dbgp_reset_prep(hcd);
  834. u32 ctrl;
  835. if (ret)
  836. return ret;
  837. dbgp_not_safe = 1;
  838. if (!ehci_debug)
  839. return 0;
  840. if ((early_dbgp_console.index != -1 &&
  841. !(early_dbgp_console.flags & CON_BOOT)) ||
  842. dbgp_kgdb_mode)
  843. return 1;
  844. /* This means the console is not initialized, or should get
  845. * shutdown so as to allow for reuse of the usb device, which
  846. * means it is time to shutdown the usb debug port. */
  847. ctrl = readl(&ehci_debug->control);
  848. if (ctrl & DBGP_ENABLED) {
  849. ctrl &= ~(DBGP_CLAIM);
  850. writel(ctrl, &ehci_debug->control);
  851. }
  852. return 0;
  853. }
  854. EXPORT_SYMBOL_GPL(dbgp_reset_prep);
  855. int dbgp_external_startup(struct usb_hcd *hcd)
  856. {
  857. return xen_dbgp_external_startup(hcd) ?: _dbgp_external_startup();
  858. }
  859. EXPORT_SYMBOL_GPL(dbgp_external_startup);
  860. #endif /* USB */
  861. #ifdef CONFIG_KGDB
  862. static char kgdbdbgp_buf[DBGP_MAX_PACKET];
  863. static int kgdbdbgp_buf_sz;
  864. static int kgdbdbgp_buf_idx;
  865. static int kgdbdbgp_loop_cnt = DBGP_LOOPS;
  866. static int kgdbdbgp_read_char(void)
  867. {
  868. int ret;
  869. if (kgdbdbgp_buf_idx < kgdbdbgp_buf_sz) {
  870. char ch = kgdbdbgp_buf[kgdbdbgp_buf_idx++];
  871. return ch;
  872. }
  873. ret = dbgp_bulk_read(USB_DEBUG_DEVNUM, dbgp_endpoint_in,
  874. &kgdbdbgp_buf, DBGP_MAX_PACKET,
  875. kgdbdbgp_loop_cnt);
  876. if (ret <= 0)
  877. return NO_POLL_CHAR;
  878. kgdbdbgp_buf_sz = ret;
  879. kgdbdbgp_buf_idx = 1;
  880. return kgdbdbgp_buf[0];
  881. }
  882. static void kgdbdbgp_write_char(u8 chr)
  883. {
  884. early_dbgp_write(NULL, &chr, 1);
  885. }
  886. static struct kgdb_io kgdbdbgp_io_ops = {
  887. .name = "kgdbdbgp",
  888. .read_char = kgdbdbgp_read_char,
  889. .write_char = kgdbdbgp_write_char,
  890. };
  891. static int kgdbdbgp_wait_time;
  892. static int __init kgdbdbgp_parse_config(char *str)
  893. {
  894. char *ptr;
  895. if (!ehci_debug) {
  896. if (early_dbgp_init(str))
  897. return -1;
  898. }
  899. ptr = strchr(str, ',');
  900. if (ptr) {
  901. ptr++;
  902. kgdbdbgp_wait_time = simple_strtoul(ptr, &ptr, 10);
  903. }
  904. kgdb_register_io_module(&kgdbdbgp_io_ops);
  905. kgdbdbgp_io_ops.is_console = early_dbgp_console.index != -1;
  906. return 0;
  907. }
  908. early_param("kgdbdbgp", kgdbdbgp_parse_config);
  909. static int kgdbdbgp_reader_thread(void *ptr)
  910. {
  911. int ret;
  912. while (readl(&ehci_debug->control) & DBGP_ENABLED) {
  913. kgdbdbgp_loop_cnt = 1;
  914. ret = kgdbdbgp_read_char();
  915. kgdbdbgp_loop_cnt = DBGP_LOOPS;
  916. if (ret != NO_POLL_CHAR) {
  917. if (ret == 0x3 || ret == '$') {
  918. if (ret == '$')
  919. kgdbdbgp_buf_idx--;
  920. kgdb_breakpoint();
  921. }
  922. continue;
  923. }
  924. schedule_timeout_interruptible(kgdbdbgp_wait_time * HZ);
  925. }
  926. return 0;
  927. }
  928. static int __init kgdbdbgp_start_thread(void)
  929. {
  930. if (dbgp_kgdb_mode && kgdbdbgp_wait_time)
  931. kthread_run(kgdbdbgp_reader_thread, NULL, "%s", "dbgp");
  932. return 0;
  933. }
  934. module_init(kgdbdbgp_start_thread);
  935. #endif /* CONFIG_KGDB */