ehci-dbgp.c 24 KB

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