ehci-dbgp.c 23 KB

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