ehci-dbgp.c 26 KB

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