ehci-dbgp.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Standalone EHCI usb debug driver
  4. *
  5. * Originally written by:
  6. * Eric W. Biederman" <ebiederm@xmission.com> and
  7. * Yinghai Lu <yhlu.kernel@gmail.com>
  8. *
  9. * Changes for early/late printk and HW errata:
  10. * Jason Wessel <jason.wessel@windriver.com>
  11. * Copyright (C) 2009 Wind River Systems, Inc.
  12. *
  13. */
  14. #include <linux/console.h>
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/pci_regs.h>
  18. #include <linux/pci_ids.h>
  19. #include <linux/usb/ch9.h>
  20. #include <linux/usb/ehci_def.h>
  21. #include <linux/delay.h>
  22. #include <linux/serial_core.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. dbgp_printk("debug device renamed to 127\n");
  500. }
  501. /* Enable the debug interface */
  502. ret = dbgp_control_msg(USB_DEBUG_DEVNUM,
  503. USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  504. USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
  505. if (ret < 0) {
  506. dbgp_printk(" Could not enable the debug device\n");
  507. goto err;
  508. }
  509. dbgp_printk("debug interface enabled\n");
  510. /* Perform a small write to get the even/odd data state in sync
  511. */
  512. ret = dbgp_bulk_write(USB_DEBUG_DEVNUM, dbgp_endpoint_out, " ", 1);
  513. if (ret < 0) {
  514. dbgp_printk("dbgp_bulk_write failed: %d\n", ret);
  515. goto err;
  516. }
  517. dbgp_printk("small write done\n");
  518. dbgp_not_safe = 0;
  519. return 0;
  520. err:
  521. if (tries--)
  522. goto try_again;
  523. return -ENODEV;
  524. }
  525. static int ehci_reset_port(int port)
  526. {
  527. u32 portsc;
  528. u32 delay_time, delay;
  529. int loop;
  530. dbgp_ehci_status("reset port");
  531. /* Reset the usb debug port */
  532. portsc = readl(&ehci_regs->port_status[port - 1]);
  533. portsc &= ~PORT_PE;
  534. portsc |= PORT_RESET;
  535. writel(portsc, &ehci_regs->port_status[port - 1]);
  536. delay = HUB_ROOT_RESET_TIME;
  537. for (delay_time = 0; delay_time < HUB_RESET_TIMEOUT;
  538. delay_time += delay) {
  539. dbgp_mdelay(delay);
  540. portsc = readl(&ehci_regs->port_status[port - 1]);
  541. if (!(portsc & PORT_RESET))
  542. break;
  543. }
  544. if (portsc & PORT_RESET) {
  545. /* force reset to complete */
  546. loop = 100 * 1000;
  547. writel(portsc & ~(PORT_RWC_BITS | PORT_RESET),
  548. &ehci_regs->port_status[port - 1]);
  549. do {
  550. udelay(1);
  551. portsc = readl(&ehci_regs->port_status[port-1]);
  552. } while ((portsc & PORT_RESET) && (--loop > 0));
  553. }
  554. /* Device went away? */
  555. if (!(portsc & PORT_CONNECT))
  556. return -ENOTCONN;
  557. /* bomb out completely if something weird happened */
  558. if ((portsc & PORT_CSC))
  559. return -EINVAL;
  560. /* If we've finished resetting, then break out of the loop */
  561. if (!(portsc & PORT_RESET) && (portsc & PORT_PE))
  562. return 0;
  563. return -EBUSY;
  564. }
  565. static int ehci_wait_for_port(int port)
  566. {
  567. u32 status;
  568. int ret, reps;
  569. for (reps = 0; reps < 300; reps++) {
  570. status = readl(&ehci_regs->status);
  571. if (status & STS_PCD)
  572. break;
  573. dbgp_mdelay(1);
  574. }
  575. ret = ehci_reset_port(port);
  576. if (ret == 0)
  577. return 0;
  578. return -ENOTCONN;
  579. }
  580. typedef void (*set_debug_port_t)(int port);
  581. static void __init default_set_debug_port(int port)
  582. {
  583. }
  584. static set_debug_port_t __initdata set_debug_port = default_set_debug_port;
  585. static void __init nvidia_set_debug_port(int port)
  586. {
  587. u32 dword;
  588. dword = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  589. 0x74);
  590. dword &= ~(0x0f<<12);
  591. dword |= ((port & 0x0f)<<12);
  592. write_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func, 0x74,
  593. dword);
  594. dbgp_printk("set debug port to %d\n", port);
  595. }
  596. static void __init detect_set_debug_port(void)
  597. {
  598. u32 vendorid;
  599. vendorid = read_pci_config(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  600. 0x00);
  601. if ((vendorid & 0xffff) == 0x10de) {
  602. dbgp_printk("using nvidia set_debug_port\n");
  603. set_debug_port = nvidia_set_debug_port;
  604. }
  605. }
  606. /* The code in early_ehci_bios_handoff() is derived from the usb pci
  607. * quirk initialization, but altered so as to use the early PCI
  608. * routines. */
  609. #define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
  610. #define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
  611. static void __init early_ehci_bios_handoff(void)
  612. {
  613. u32 hcc_params = readl(&ehci_caps->hcc_params);
  614. int offset = (hcc_params >> 8) & 0xff;
  615. u32 cap;
  616. int msec;
  617. if (!offset)
  618. return;
  619. cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
  620. ehci_dev.func, offset);
  621. dbgp_printk("dbgp: ehci BIOS state %08x\n", cap);
  622. if ((cap & 0xff) == 1 && (cap & EHCI_USBLEGSUP_BIOS)) {
  623. dbgp_printk("dbgp: BIOS handoff\n");
  624. write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
  625. ehci_dev.func, offset + 3, 1);
  626. }
  627. /* if boot firmware now owns EHCI, spin till it hands it over. */
  628. msec = 1000;
  629. while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
  630. mdelay(10);
  631. msec -= 10;
  632. cap = read_pci_config(ehci_dev.bus, ehci_dev.slot,
  633. ehci_dev.func, offset);
  634. }
  635. if (cap & EHCI_USBLEGSUP_BIOS) {
  636. /* well, possibly buggy BIOS... try to shut it down,
  637. * and hope nothing goes too wrong */
  638. dbgp_printk("dbgp: BIOS handoff failed: %08x\n", cap);
  639. write_pci_config_byte(ehci_dev.bus, ehci_dev.slot,
  640. ehci_dev.func, offset + 2, 0);
  641. }
  642. /* just in case, always disable EHCI SMIs */
  643. write_pci_config_byte(ehci_dev.bus, ehci_dev.slot, ehci_dev.func,
  644. offset + EHCI_USBLEGCTLSTS, 0);
  645. }
  646. static int __init ehci_setup(void)
  647. {
  648. u32 ctrl, portsc, hcs_params;
  649. u32 debug_port, new_debug_port = 0, n_ports;
  650. int ret, i;
  651. int port_map_tried;
  652. int playtimes = 3;
  653. early_ehci_bios_handoff();
  654. try_next_time:
  655. port_map_tried = 0;
  656. try_next_port:
  657. hcs_params = readl(&ehci_caps->hcs_params);
  658. debug_port = HCS_DEBUG_PORT(hcs_params);
  659. dbgp_phys_port = debug_port;
  660. n_ports = HCS_N_PORTS(hcs_params);
  661. dbgp_printk("debug_port: %d\n", debug_port);
  662. dbgp_printk("n_ports: %d\n", n_ports);
  663. dbgp_ehci_status("");
  664. for (i = 1; i <= n_ports; i++) {
  665. portsc = readl(&ehci_regs->port_status[i-1]);
  666. dbgp_printk("portstatus%d: %08x\n", i, portsc);
  667. }
  668. if (port_map_tried && (new_debug_port != debug_port)) {
  669. if (--playtimes) {
  670. set_debug_port(new_debug_port);
  671. goto try_next_time;
  672. }
  673. return -1;
  674. }
  675. /* Only reset the controller if it is not already in the
  676. * configured state */
  677. if (!(readl(&ehci_regs->configured_flag) & FLAG_CF)) {
  678. if (dbgp_ehci_controller_reset() != 0)
  679. return -1;
  680. } else {
  681. dbgp_ehci_status("ehci skip - already configured");
  682. }
  683. ret = _dbgp_external_startup();
  684. if (ret == -EIO)
  685. goto next_debug_port;
  686. if (ret < 0) {
  687. /* Things didn't work so remove my claim */
  688. ctrl = readl(&ehci_debug->control);
  689. ctrl &= ~(DBGP_CLAIM | DBGP_OUT);
  690. writel(ctrl, &ehci_debug->control);
  691. return -1;
  692. }
  693. return 0;
  694. next_debug_port:
  695. port_map_tried |= (1<<(debug_port - 1));
  696. new_debug_port = ((debug_port-1+1)%n_ports) + 1;
  697. if (port_map_tried != ((1<<n_ports) - 1)) {
  698. set_debug_port(new_debug_port);
  699. goto try_next_port;
  700. }
  701. if (--playtimes) {
  702. set_debug_port(new_debug_port);
  703. goto try_next_time;
  704. }
  705. return -1;
  706. }
  707. int __init early_dbgp_init(char *s)
  708. {
  709. u32 debug_port, bar, offset;
  710. u32 bus, slot, func, cap;
  711. void __iomem *ehci_bar;
  712. u32 dbgp_num;
  713. u32 bar_val;
  714. char *e;
  715. int ret;
  716. u8 byte;
  717. if (!early_pci_allowed())
  718. return -1;
  719. dbgp_num = 0;
  720. if (*s)
  721. dbgp_num = simple_strtoul(s, &e, 10);
  722. dbgp_printk("dbgp_num: %d\n", dbgp_num);
  723. cap = find_dbgp(dbgp_num, &bus, &slot, &func);
  724. if (!cap)
  725. return -1;
  726. dbgp_printk("Found EHCI debug port on %02x:%02x.%1x\n", bus, slot,
  727. func);
  728. debug_port = read_pci_config(bus, slot, func, cap);
  729. bar = (debug_port >> 29) & 0x7;
  730. bar = (bar * 4) + 0xc;
  731. offset = (debug_port >> 16) & 0xfff;
  732. dbgp_printk("bar: %02x offset: %03x\n", bar, offset);
  733. if (bar != PCI_BASE_ADDRESS_0) {
  734. dbgp_printk("only debug ports on bar 1 handled.\n");
  735. return -1;
  736. }
  737. bar_val = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
  738. dbgp_printk("bar_val: %02x offset: %03x\n", bar_val, offset);
  739. if (bar_val & ~PCI_BASE_ADDRESS_MEM_MASK) {
  740. dbgp_printk("only simple 32bit mmio bars supported\n");
  741. return -1;
  742. }
  743. /* double check if the mem space is enabled */
  744. byte = read_pci_config_byte(bus, slot, func, 0x04);
  745. if (!(byte & 0x2)) {
  746. byte |= 0x02;
  747. write_pci_config_byte(bus, slot, func, 0x04, byte);
  748. dbgp_printk("mmio for ehci enabled\n");
  749. }
  750. /*
  751. * FIXME I don't have the bar size so just guess PAGE_SIZE is more
  752. * than enough. 1K is the biggest I have seen.
  753. */
  754. set_fixmap_nocache(FIX_DBGP_BASE, bar_val & PAGE_MASK);
  755. ehci_bar = (void __iomem *)__fix_to_virt(FIX_DBGP_BASE);
  756. ehci_bar += bar_val & ~PAGE_MASK;
  757. dbgp_printk("ehci_bar: %p\n", ehci_bar);
  758. ehci_caps = ehci_bar;
  759. ehci_regs = ehci_bar + EARLY_HC_LENGTH(readl(&ehci_caps->hc_capbase));
  760. ehci_debug = ehci_bar + offset;
  761. ehci_dev.bus = bus;
  762. ehci_dev.slot = slot;
  763. ehci_dev.func = func;
  764. detect_set_debug_port();
  765. ret = ehci_setup();
  766. if (ret < 0) {
  767. dbgp_printk("ehci_setup failed\n");
  768. ehci_debug = NULL;
  769. return -1;
  770. }
  771. dbgp_ehci_status("early_init_complete");
  772. return 0;
  773. }
  774. static void early_dbgp_write(struct console *con, const char *str, u32 n)
  775. {
  776. int chunk, ret;
  777. char buf[DBGP_MAX_PACKET];
  778. int use_cr = 0;
  779. u32 cmd, ctrl;
  780. int reset_run = 0;
  781. if (!ehci_debug || dbgp_not_safe)
  782. return;
  783. cmd = readl(&ehci_regs->command);
  784. if (unlikely(!(cmd & CMD_RUN))) {
  785. /* If the ehci controller is not in the run state do extended
  786. * checks to see if the acpi or some other initialization also
  787. * reset the ehci debug port */
  788. ctrl = readl(&ehci_debug->control);
  789. if (!(ctrl & DBGP_ENABLED)) {
  790. dbgp_not_safe = 1;
  791. _dbgp_external_startup();
  792. } else {
  793. cmd |= CMD_RUN;
  794. writel(cmd, &ehci_regs->command);
  795. reset_run = 1;
  796. }
  797. }
  798. while (n > 0) {
  799. for (chunk = 0; chunk < DBGP_MAX_PACKET && n > 0;
  800. str++, chunk++, n--) {
  801. if (!use_cr && *str == '\n') {
  802. use_cr = 1;
  803. buf[chunk] = '\r';
  804. str--;
  805. n++;
  806. continue;
  807. }
  808. if (use_cr)
  809. use_cr = 0;
  810. buf[chunk] = *str;
  811. }
  812. if (chunk > 0) {
  813. ret = dbgp_bulk_write(USB_DEBUG_DEVNUM,
  814. dbgp_endpoint_out, buf, chunk);
  815. }
  816. }
  817. if (unlikely(reset_run)) {
  818. cmd = readl(&ehci_regs->command);
  819. cmd &= ~CMD_RUN;
  820. writel(cmd, &ehci_regs->command);
  821. }
  822. }
  823. struct console early_dbgp_console = {
  824. .name = "earlydbg",
  825. .write = early_dbgp_write,
  826. .flags = CON_PRINTBUFFER,
  827. .index = -1,
  828. };
  829. #if IS_ENABLED(CONFIG_USB)
  830. int dbgp_reset_prep(struct usb_hcd *hcd)
  831. {
  832. int ret = xen_dbgp_reset_prep(hcd);
  833. u32 ctrl;
  834. if (ret)
  835. return ret;
  836. dbgp_not_safe = 1;
  837. if (!ehci_debug)
  838. return 0;
  839. if ((early_dbgp_console.index != -1 &&
  840. !(early_dbgp_console.flags & CON_BOOT)) ||
  841. dbgp_kgdb_mode)
  842. return 1;
  843. /* This means the console is not initialized, or should get
  844. * shutdown so as to allow for reuse of the usb device, which
  845. * means it is time to shutdown the usb debug port. */
  846. ctrl = readl(&ehci_debug->control);
  847. if (ctrl & DBGP_ENABLED) {
  848. ctrl &= ~(DBGP_CLAIM);
  849. writel(ctrl, &ehci_debug->control);
  850. }
  851. return 0;
  852. }
  853. EXPORT_SYMBOL_GPL(dbgp_reset_prep);
  854. int dbgp_external_startup(struct usb_hcd *hcd)
  855. {
  856. return xen_dbgp_external_startup(hcd) ?: _dbgp_external_startup();
  857. }
  858. EXPORT_SYMBOL_GPL(dbgp_external_startup);
  859. #endif /* USB */
  860. #ifdef CONFIG_KGDB
  861. static char kgdbdbgp_buf[DBGP_MAX_PACKET];
  862. static int kgdbdbgp_buf_sz;
  863. static int kgdbdbgp_buf_idx;
  864. static int kgdbdbgp_loop_cnt = DBGP_LOOPS;
  865. static int kgdbdbgp_read_char(void)
  866. {
  867. int ret;
  868. if (kgdbdbgp_buf_idx < kgdbdbgp_buf_sz) {
  869. char ch = kgdbdbgp_buf[kgdbdbgp_buf_idx++];
  870. return ch;
  871. }
  872. ret = dbgp_bulk_read(USB_DEBUG_DEVNUM, dbgp_endpoint_in,
  873. &kgdbdbgp_buf, DBGP_MAX_PACKET,
  874. kgdbdbgp_loop_cnt);
  875. if (ret <= 0)
  876. return NO_POLL_CHAR;
  877. kgdbdbgp_buf_sz = ret;
  878. kgdbdbgp_buf_idx = 1;
  879. return kgdbdbgp_buf[0];
  880. }
  881. static void kgdbdbgp_write_char(u8 chr)
  882. {
  883. early_dbgp_write(NULL, &chr, 1);
  884. }
  885. static struct kgdb_io kgdbdbgp_io_ops = {
  886. .name = "kgdbdbgp",
  887. .read_char = kgdbdbgp_read_char,
  888. .write_char = kgdbdbgp_write_char,
  889. };
  890. static int kgdbdbgp_wait_time;
  891. static int __init kgdbdbgp_parse_config(char *str)
  892. {
  893. char *ptr;
  894. if (!ehci_debug) {
  895. if (early_dbgp_init(str))
  896. return -1;
  897. }
  898. ptr = strchr(str, ',');
  899. if (ptr) {
  900. ptr++;
  901. kgdbdbgp_wait_time = simple_strtoul(ptr, &ptr, 10);
  902. }
  903. kgdb_register_io_module(&kgdbdbgp_io_ops);
  904. kgdbdbgp_io_ops.is_console = early_dbgp_console.index != -1;
  905. return 0;
  906. }
  907. early_param("kgdbdbgp", kgdbdbgp_parse_config);
  908. static int kgdbdbgp_reader_thread(void *ptr)
  909. {
  910. int ret;
  911. while (readl(&ehci_debug->control) & DBGP_ENABLED) {
  912. kgdbdbgp_loop_cnt = 1;
  913. ret = kgdbdbgp_read_char();
  914. kgdbdbgp_loop_cnt = DBGP_LOOPS;
  915. if (ret != NO_POLL_CHAR) {
  916. if (ret == 0x3 || ret == '$') {
  917. if (ret == '$')
  918. kgdbdbgp_buf_idx--;
  919. kgdb_breakpoint();
  920. }
  921. continue;
  922. }
  923. schedule_timeout_interruptible(kgdbdbgp_wait_time * HZ);
  924. }
  925. return 0;
  926. }
  927. static int __init kgdbdbgp_start_thread(void)
  928. {
  929. if (dbgp_kgdb_mode && kgdbdbgp_wait_time)
  930. kthread_run(kgdbdbgp_reader_thread, NULL, "%s", "dbgp");
  931. return 0;
  932. }
  933. device_initcall(kgdbdbgp_start_thread);
  934. #endif /* CONFIG_KGDB */