ehci-hcd.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. /*-
  2. * Copyright (c) 2007-2008, Juniper Networks, Inc.
  3. * Copyright (c) 2008, Excito Elektronik i Skåne AB
  4. * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2 of
  11. * the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <errno.h>
  25. #include <asm/byteorder.h>
  26. #include <asm/unaligned.h>
  27. #include <usb.h>
  28. #include <asm/io.h>
  29. #include <malloc.h>
  30. #include <watchdog.h>
  31. #include <linux/compiler.h>
  32. #include "ehci.h"
  33. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  34. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  35. #endif
  36. static struct ehci_ctrl {
  37. struct ehci_hccr *hccr; /* R/O registers, not need for volatile */
  38. struct ehci_hcor *hcor;
  39. int rootdev;
  40. uint16_t portreset;
  41. struct QH qh_list __aligned(USB_DMA_MINALIGN);
  42. struct QH periodic_queue __aligned(USB_DMA_MINALIGN);
  43. uint32_t *periodic_list;
  44. int ntds;
  45. } ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
  46. #define ALIGN_END_ADDR(type, ptr, size) \
  47. ((uint32_t)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
  48. static struct descriptor {
  49. struct usb_hub_descriptor hub;
  50. struct usb_device_descriptor device;
  51. struct usb_linux_config_descriptor config;
  52. struct usb_linux_interface_descriptor interface;
  53. struct usb_endpoint_descriptor endpoint;
  54. } __attribute__ ((packed)) descriptor = {
  55. {
  56. 0x8, /* bDescLength */
  57. 0x29, /* bDescriptorType: hub descriptor */
  58. 2, /* bNrPorts -- runtime modified */
  59. 0, /* wHubCharacteristics */
  60. 10, /* bPwrOn2PwrGood */
  61. 0, /* bHubCntrCurrent */
  62. {}, /* Device removable */
  63. {} /* at most 7 ports! XXX */
  64. },
  65. {
  66. 0x12, /* bLength */
  67. 1, /* bDescriptorType: UDESC_DEVICE */
  68. cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
  69. 9, /* bDeviceClass: UDCLASS_HUB */
  70. 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
  71. 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
  72. 64, /* bMaxPacketSize: 64 bytes */
  73. 0x0000, /* idVendor */
  74. 0x0000, /* idProduct */
  75. cpu_to_le16(0x0100), /* bcdDevice */
  76. 1, /* iManufacturer */
  77. 2, /* iProduct */
  78. 0, /* iSerialNumber */
  79. 1 /* bNumConfigurations: 1 */
  80. },
  81. {
  82. 0x9,
  83. 2, /* bDescriptorType: UDESC_CONFIG */
  84. cpu_to_le16(0x19),
  85. 1, /* bNumInterface */
  86. 1, /* bConfigurationValue */
  87. 0, /* iConfiguration */
  88. 0x40, /* bmAttributes: UC_SELF_POWER */
  89. 0 /* bMaxPower */
  90. },
  91. {
  92. 0x9, /* bLength */
  93. 4, /* bDescriptorType: UDESC_INTERFACE */
  94. 0, /* bInterfaceNumber */
  95. 0, /* bAlternateSetting */
  96. 1, /* bNumEndpoints */
  97. 9, /* bInterfaceClass: UICLASS_HUB */
  98. 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
  99. 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
  100. 0 /* iInterface */
  101. },
  102. {
  103. 0x7, /* bLength */
  104. 5, /* bDescriptorType: UDESC_ENDPOINT */
  105. 0x81, /* bEndpointAddress:
  106. * UE_DIR_IN | EHCI_INTR_ENDPT
  107. */
  108. 3, /* bmAttributes: UE_INTERRUPT */
  109. 8, /* wMaxPacketSize */
  110. 255 /* bInterval */
  111. },
  112. };
  113. #if defined(CONFIG_EHCI_IS_TDI)
  114. #define ehci_is_TDI() (1)
  115. #else
  116. #define ehci_is_TDI() (0)
  117. #endif
  118. int __ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
  119. {
  120. return PORTSC_PSPD(reg);
  121. }
  122. int ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
  123. __attribute__((weak, alias("__ehci_get_port_speed")));
  124. void __ehci_set_usbmode(int index)
  125. {
  126. uint32_t tmp;
  127. uint32_t *reg_ptr;
  128. reg_ptr = (uint32_t *)((u8 *)&ehcic[index].hcor->or_usbcmd + USBMODE);
  129. tmp = ehci_readl(reg_ptr);
  130. tmp |= USBMODE_CM_HC;
  131. #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
  132. tmp |= USBMODE_BE;
  133. #endif
  134. ehci_writel(reg_ptr, tmp);
  135. }
  136. void ehci_set_usbmode(int index)
  137. __attribute__((weak, alias("__ehci_set_usbmode")));
  138. void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
  139. {
  140. mdelay(50);
  141. }
  142. void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
  143. __attribute__((weak, alias("__ehci_powerup_fixup")));
  144. static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
  145. {
  146. uint32_t result;
  147. do {
  148. result = ehci_readl(ptr);
  149. udelay(5);
  150. if (result == ~(uint32_t)0)
  151. return -1;
  152. result &= mask;
  153. if (result == done)
  154. return 0;
  155. usec--;
  156. } while (usec > 0);
  157. return -1;
  158. }
  159. static int ehci_reset(int index)
  160. {
  161. uint32_t cmd;
  162. int ret = 0;
  163. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  164. cmd = (cmd & ~CMD_RUN) | CMD_RESET;
  165. ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
  166. ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
  167. CMD_RESET, 0, 250 * 1000);
  168. if (ret < 0) {
  169. printf("EHCI fail to reset\n");
  170. goto out;
  171. }
  172. if (ehci_is_TDI())
  173. ehci_set_usbmode(index);
  174. #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
  175. cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
  176. cmd &= ~TXFIFO_THRESH_MASK;
  177. cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
  178. ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
  179. #endif
  180. out:
  181. return ret;
  182. }
  183. static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
  184. {
  185. uint32_t delta, next;
  186. uint32_t addr = (uint32_t)buf;
  187. int idx;
  188. if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
  189. debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
  190. flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
  191. idx = 0;
  192. while (idx < QT_BUFFER_CNT) {
  193. td->qt_buffer[idx] = cpu_to_hc32(addr);
  194. td->qt_buffer_hi[idx] = 0;
  195. next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
  196. delta = next - addr;
  197. if (delta >= sz)
  198. break;
  199. sz -= delta;
  200. addr = next;
  201. idx++;
  202. }
  203. if (idx == QT_BUFFER_CNT) {
  204. printf("out of buffer pointers (%u bytes left)\n", sz);
  205. return -1;
  206. }
  207. return 0;
  208. }
  209. static inline u8 ehci_encode_speed(enum usb_device_speed speed)
  210. {
  211. #define QH_HIGH_SPEED 2
  212. #define QH_FULL_SPEED 0
  213. #define QH_LOW_SPEED 1
  214. if (speed == USB_SPEED_HIGH)
  215. return QH_HIGH_SPEED;
  216. if (speed == USB_SPEED_LOW)
  217. return QH_LOW_SPEED;
  218. return QH_FULL_SPEED;
  219. }
  220. static int
  221. ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
  222. int length, struct devrequest *req)
  223. {
  224. ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
  225. struct qTD *qtd;
  226. int qtd_count = 0;
  227. int qtd_counter = 0;
  228. volatile struct qTD *vtd;
  229. unsigned long ts;
  230. uint32_t *tdp;
  231. uint32_t endpt, maxpacket, token, usbsts;
  232. uint32_t c, toggle;
  233. uint32_t cmd;
  234. int timeout;
  235. int ret = 0;
  236. struct ehci_ctrl *ctrl = dev->controller;
  237. debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
  238. buffer, length, req);
  239. if (req != NULL)
  240. debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
  241. req->request, req->request,
  242. req->requesttype, req->requesttype,
  243. le16_to_cpu(req->value), le16_to_cpu(req->value),
  244. le16_to_cpu(req->index));
  245. #define PKT_ALIGN 512
  246. /*
  247. * The USB transfer is split into qTD transfers. Eeach qTD transfer is
  248. * described by a transfer descriptor (the qTD). The qTDs form a linked
  249. * list with a queue head (QH).
  250. *
  251. * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
  252. * have its beginning in a qTD transfer and its end in the following
  253. * one, so the qTD transfer lengths have to be chosen accordingly.
  254. *
  255. * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
  256. * single pages. The first data buffer can start at any offset within a
  257. * page (not considering the cache-line alignment issues), while the
  258. * following buffers must be page-aligned. There is no alignment
  259. * constraint on the size of a qTD transfer.
  260. */
  261. if (req != NULL)
  262. /* 1 qTD will be needed for SETUP, and 1 for ACK. */
  263. qtd_count += 1 + 1;
  264. if (length > 0 || req == NULL) {
  265. /*
  266. * Determine the qTD transfer size that will be used for the
  267. * data payload (not considering the first qTD transfer, which
  268. * may be longer or shorter, and the final one, which may be
  269. * shorter).
  270. *
  271. * In order to keep each packet within a qTD transfer, the qTD
  272. * transfer size is aligned to PKT_ALIGN, which is a multiple of
  273. * wMaxPacketSize (except in some cases for interrupt transfers,
  274. * see comment in submit_int_msg()).
  275. *
  276. * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
  277. * QT_BUFFER_CNT full pages will be used.
  278. */
  279. int xfr_sz = QT_BUFFER_CNT;
  280. /*
  281. * However, if the input buffer is not aligned to PKT_ALIGN, the
  282. * qTD transfer size will be one page shorter, and the first qTD
  283. * data buffer of each transfer will be page-unaligned.
  284. */
  285. if ((uint32_t)buffer & (PKT_ALIGN - 1))
  286. xfr_sz--;
  287. /* Convert the qTD transfer size to bytes. */
  288. xfr_sz *= EHCI_PAGE_SIZE;
  289. /*
  290. * Approximate by excess the number of qTDs that will be
  291. * required for the data payload. The exact formula is way more
  292. * complicated and saves at most 2 qTDs, i.e. a total of 128
  293. * bytes.
  294. */
  295. qtd_count += 2 + length / xfr_sz;
  296. }
  297. /*
  298. * Threshold value based on the worst-case total size of the allocated qTDs for
  299. * a mass-storage transfer of 65535 blocks of 512 bytes.
  300. */
  301. #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
  302. #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
  303. #endif
  304. qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
  305. if (qtd == NULL) {
  306. printf("unable to allocate TDs\n");
  307. return -1;
  308. }
  309. memset(qh, 0, sizeof(struct QH));
  310. memset(qtd, 0, qtd_count * sizeof(*qtd));
  311. toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  312. /*
  313. * Setup QH (3.6 in ehci-r10.pdf)
  314. *
  315. * qh_link ................. 03-00 H
  316. * qh_endpt1 ............... 07-04 H
  317. * qh_endpt2 ............... 0B-08 H
  318. * - qh_curtd
  319. * qh_overlay.qt_next ...... 13-10 H
  320. * - qh_overlay.qt_altnext
  321. */
  322. qh->qh_link = cpu_to_hc32((uint32_t)&ctrl->qh_list | QH_LINK_TYPE_QH);
  323. c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
  324. maxpacket = usb_maxpacket(dev, pipe);
  325. endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
  326. QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
  327. QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
  328. QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
  329. QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
  330. QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
  331. qh->qh_endpt1 = cpu_to_hc32(endpt);
  332. endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_PORTNUM(dev->portnr) |
  333. QH_ENDPT2_HUBADDR(dev->parent->devnum) |
  334. QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
  335. qh->qh_endpt2 = cpu_to_hc32(endpt);
  336. qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  337. tdp = &qh->qh_overlay.qt_next;
  338. if (req != NULL) {
  339. /*
  340. * Setup request qTD (3.5 in ehci-r10.pdf)
  341. *
  342. * qt_next ................ 03-00 H
  343. * qt_altnext ............. 07-04 H
  344. * qt_token ............... 0B-08 H
  345. *
  346. * [ buffer, buffer_hi ] loaded with "req".
  347. */
  348. qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  349. qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  350. token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
  351. QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
  352. QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
  353. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  354. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  355. if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
  356. printf("unable to construct SETUP TD\n");
  357. goto fail;
  358. }
  359. /* Update previous qTD! */
  360. *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
  361. tdp = &qtd[qtd_counter++].qt_next;
  362. toggle = 1;
  363. }
  364. if (length > 0 || req == NULL) {
  365. uint8_t *buf_ptr = buffer;
  366. int left_length = length;
  367. do {
  368. /*
  369. * Determine the size of this qTD transfer. By default,
  370. * QT_BUFFER_CNT full pages can be used.
  371. */
  372. int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
  373. /*
  374. * However, if the input buffer is not page-aligned, the
  375. * portion of the first page before the buffer start
  376. * offset within that page is unusable.
  377. */
  378. xfr_bytes -= (uint32_t)buf_ptr & (EHCI_PAGE_SIZE - 1);
  379. /*
  380. * In order to keep each packet within a qTD transfer,
  381. * align the qTD transfer size to PKT_ALIGN.
  382. */
  383. xfr_bytes &= ~(PKT_ALIGN - 1);
  384. /*
  385. * This transfer may be shorter than the available qTD
  386. * transfer size that has just been computed.
  387. */
  388. xfr_bytes = min(xfr_bytes, left_length);
  389. /*
  390. * Setup request qTD (3.5 in ehci-r10.pdf)
  391. *
  392. * qt_next ................ 03-00 H
  393. * qt_altnext ............. 07-04 H
  394. * qt_token ............... 0B-08 H
  395. *
  396. * [ buffer, buffer_hi ] loaded with "buffer".
  397. */
  398. qtd[qtd_counter].qt_next =
  399. cpu_to_hc32(QT_NEXT_TERMINATE);
  400. qtd[qtd_counter].qt_altnext =
  401. cpu_to_hc32(QT_NEXT_TERMINATE);
  402. token = QT_TOKEN_DT(toggle) |
  403. QT_TOKEN_TOTALBYTES(xfr_bytes) |
  404. QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
  405. QT_TOKEN_CERR(3) |
  406. QT_TOKEN_PID(usb_pipein(pipe) ?
  407. QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
  408. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  409. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  410. if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
  411. xfr_bytes)) {
  412. printf("unable to construct DATA TD\n");
  413. goto fail;
  414. }
  415. /* Update previous qTD! */
  416. *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
  417. tdp = &qtd[qtd_counter++].qt_next;
  418. /*
  419. * Data toggle has to be adjusted since the qTD transfer
  420. * size is not always an even multiple of
  421. * wMaxPacketSize.
  422. */
  423. if ((xfr_bytes / maxpacket) & 1)
  424. toggle ^= 1;
  425. buf_ptr += xfr_bytes;
  426. left_length -= xfr_bytes;
  427. } while (left_length > 0);
  428. }
  429. if (req != NULL) {
  430. /*
  431. * Setup request qTD (3.5 in ehci-r10.pdf)
  432. *
  433. * qt_next ................ 03-00 H
  434. * qt_altnext ............. 07-04 H
  435. * qt_token ............... 0B-08 H
  436. */
  437. qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  438. qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  439. token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
  440. QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
  441. QT_TOKEN_PID(usb_pipein(pipe) ?
  442. QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
  443. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  444. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  445. /* Update previous qTD! */
  446. *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
  447. tdp = &qtd[qtd_counter++].qt_next;
  448. }
  449. ctrl->qh_list.qh_link = cpu_to_hc32((uint32_t)qh | QH_LINK_TYPE_QH);
  450. /* Flush dcache */
  451. flush_dcache_range((uint32_t)&ctrl->qh_list,
  452. ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
  453. flush_dcache_range((uint32_t)qh, ALIGN_END_ADDR(struct QH, qh, 1));
  454. flush_dcache_range((uint32_t)qtd,
  455. ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
  456. /* Set async. queue head pointer. */
  457. ehci_writel(&ctrl->hcor->or_asynclistaddr, (uint32_t)&ctrl->qh_list);
  458. usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
  459. ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
  460. /* Enable async. schedule. */
  461. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  462. cmd |= CMD_ASE;
  463. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  464. ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
  465. 100 * 1000);
  466. if (ret < 0) {
  467. printf("EHCI fail timeout STS_ASS set\n");
  468. goto fail;
  469. }
  470. /* Wait for TDs to be processed. */
  471. ts = get_timer(0);
  472. vtd = &qtd[qtd_counter - 1];
  473. timeout = USB_TIMEOUT_MS(pipe);
  474. do {
  475. /* Invalidate dcache */
  476. invalidate_dcache_range((uint32_t)&ctrl->qh_list,
  477. ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
  478. invalidate_dcache_range((uint32_t)qh,
  479. ALIGN_END_ADDR(struct QH, qh, 1));
  480. invalidate_dcache_range((uint32_t)qtd,
  481. ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
  482. token = hc32_to_cpu(vtd->qt_token);
  483. if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
  484. break;
  485. WATCHDOG_RESET();
  486. } while (get_timer(ts) < timeout);
  487. /*
  488. * Invalidate the memory area occupied by buffer
  489. * Don't try to fix the buffer alignment, if it isn't properly
  490. * aligned it's upper layer's fault so let invalidate_dcache_range()
  491. * vow about it. But we have to fix the length as it's actual
  492. * transfer length and can be unaligned. This is potentially
  493. * dangerous operation, it's responsibility of the calling
  494. * code to make sure enough space is reserved.
  495. */
  496. invalidate_dcache_range((uint32_t)buffer,
  497. ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN));
  498. /* Check that the TD processing happened */
  499. if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
  500. printf("EHCI timed out on TD - token=%#x\n", token);
  501. /* Disable async schedule. */
  502. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  503. cmd &= ~CMD_ASE;
  504. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  505. ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
  506. 100 * 1000);
  507. if (ret < 0) {
  508. printf("EHCI fail timeout STS_ASS reset\n");
  509. goto fail;
  510. }
  511. token = hc32_to_cpu(qh->qh_overlay.qt_token);
  512. if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
  513. debug("TOKEN=%#x\n", token);
  514. switch (QT_TOKEN_GET_STATUS(token) &
  515. ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
  516. case 0:
  517. toggle = QT_TOKEN_GET_DT(token);
  518. usb_settoggle(dev, usb_pipeendpoint(pipe),
  519. usb_pipeout(pipe), toggle);
  520. dev->status = 0;
  521. break;
  522. case QT_TOKEN_STATUS_HALTED:
  523. dev->status = USB_ST_STALLED;
  524. break;
  525. case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
  526. case QT_TOKEN_STATUS_DATBUFERR:
  527. dev->status = USB_ST_BUF_ERR;
  528. break;
  529. case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
  530. case QT_TOKEN_STATUS_BABBLEDET:
  531. dev->status = USB_ST_BABBLE_DET;
  532. break;
  533. default:
  534. dev->status = USB_ST_CRC_ERR;
  535. if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
  536. dev->status |= USB_ST_STALLED;
  537. break;
  538. }
  539. dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
  540. } else {
  541. dev->act_len = 0;
  542. debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
  543. dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
  544. ehci_readl(&ctrl->hcor->or_portsc[0]),
  545. ehci_readl(&ctrl->hcor->or_portsc[1]));
  546. }
  547. free(qtd);
  548. return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
  549. fail:
  550. free(qtd);
  551. return -1;
  552. }
  553. static inline int min3(int a, int b, int c)
  554. {
  555. if (b < a)
  556. a = b;
  557. if (c < a)
  558. a = c;
  559. return a;
  560. }
  561. int
  562. ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
  563. int length, struct devrequest *req)
  564. {
  565. uint8_t tmpbuf[4];
  566. u16 typeReq;
  567. void *srcptr = NULL;
  568. int len, srclen;
  569. uint32_t reg;
  570. uint32_t *status_reg;
  571. struct ehci_ctrl *ctrl = dev->controller;
  572. if (le16_to_cpu(req->index) > CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
  573. printf("The request port(%d) is not configured\n",
  574. le16_to_cpu(req->index) - 1);
  575. return -1;
  576. }
  577. status_reg = (uint32_t *)&ctrl->hcor->or_portsc[
  578. le16_to_cpu(req->index) - 1];
  579. srclen = 0;
  580. debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
  581. req->request, req->request,
  582. req->requesttype, req->requesttype,
  583. le16_to_cpu(req->value), le16_to_cpu(req->index));
  584. typeReq = req->request | req->requesttype << 8;
  585. switch (typeReq) {
  586. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  587. switch (le16_to_cpu(req->value) >> 8) {
  588. case USB_DT_DEVICE:
  589. debug("USB_DT_DEVICE request\n");
  590. srcptr = &descriptor.device;
  591. srclen = descriptor.device.bLength;
  592. break;
  593. case USB_DT_CONFIG:
  594. debug("USB_DT_CONFIG config\n");
  595. srcptr = &descriptor.config;
  596. srclen = descriptor.config.bLength +
  597. descriptor.interface.bLength +
  598. descriptor.endpoint.bLength;
  599. break;
  600. case USB_DT_STRING:
  601. debug("USB_DT_STRING config\n");
  602. switch (le16_to_cpu(req->value) & 0xff) {
  603. case 0: /* Language */
  604. srcptr = "\4\3\1\0";
  605. srclen = 4;
  606. break;
  607. case 1: /* Vendor */
  608. srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
  609. srclen = 14;
  610. break;
  611. case 2: /* Product */
  612. srcptr = "\52\3E\0H\0C\0I\0 "
  613. "\0H\0o\0s\0t\0 "
  614. "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
  615. srclen = 42;
  616. break;
  617. default:
  618. debug("unknown value DT_STRING %x\n",
  619. le16_to_cpu(req->value));
  620. goto unknown;
  621. }
  622. break;
  623. default:
  624. debug("unknown value %x\n", le16_to_cpu(req->value));
  625. goto unknown;
  626. }
  627. break;
  628. case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
  629. switch (le16_to_cpu(req->value) >> 8) {
  630. case USB_DT_HUB:
  631. debug("USB_DT_HUB config\n");
  632. srcptr = &descriptor.hub;
  633. srclen = descriptor.hub.bLength;
  634. break;
  635. default:
  636. debug("unknown value %x\n", le16_to_cpu(req->value));
  637. goto unknown;
  638. }
  639. break;
  640. case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
  641. debug("USB_REQ_SET_ADDRESS\n");
  642. ctrl->rootdev = le16_to_cpu(req->value);
  643. break;
  644. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  645. debug("USB_REQ_SET_CONFIGURATION\n");
  646. /* Nothing to do */
  647. break;
  648. case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
  649. tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
  650. tmpbuf[1] = 0;
  651. srcptr = tmpbuf;
  652. srclen = 2;
  653. break;
  654. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  655. memset(tmpbuf, 0, 4);
  656. reg = ehci_readl(status_reg);
  657. if (reg & EHCI_PS_CS)
  658. tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
  659. if (reg & EHCI_PS_PE)
  660. tmpbuf[0] |= USB_PORT_STAT_ENABLE;
  661. if (reg & EHCI_PS_SUSP)
  662. tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
  663. if (reg & EHCI_PS_OCA)
  664. tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
  665. if (reg & EHCI_PS_PR)
  666. tmpbuf[0] |= USB_PORT_STAT_RESET;
  667. if (reg & EHCI_PS_PP)
  668. tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
  669. if (ehci_is_TDI()) {
  670. switch (ehci_get_port_speed(ctrl->hcor, reg)) {
  671. case PORTSC_PSPD_FS:
  672. break;
  673. case PORTSC_PSPD_LS:
  674. tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
  675. break;
  676. case PORTSC_PSPD_HS:
  677. default:
  678. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  679. break;
  680. }
  681. } else {
  682. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  683. }
  684. if (reg & EHCI_PS_CSC)
  685. tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
  686. if (reg & EHCI_PS_PEC)
  687. tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
  688. if (reg & EHCI_PS_OCC)
  689. tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
  690. if (ctrl->portreset & (1 << le16_to_cpu(req->index)))
  691. tmpbuf[2] |= USB_PORT_STAT_C_RESET;
  692. srcptr = tmpbuf;
  693. srclen = 4;
  694. break;
  695. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  696. reg = ehci_readl(status_reg);
  697. reg &= ~EHCI_PS_CLEAR;
  698. switch (le16_to_cpu(req->value)) {
  699. case USB_PORT_FEAT_ENABLE:
  700. reg |= EHCI_PS_PE;
  701. ehci_writel(status_reg, reg);
  702. break;
  703. case USB_PORT_FEAT_POWER:
  704. if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
  705. reg |= EHCI_PS_PP;
  706. ehci_writel(status_reg, reg);
  707. }
  708. break;
  709. case USB_PORT_FEAT_RESET:
  710. if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
  711. !ehci_is_TDI() &&
  712. EHCI_PS_IS_LOWSPEED(reg)) {
  713. /* Low speed device, give up ownership. */
  714. debug("port %d low speed --> companion\n",
  715. req->index - 1);
  716. reg |= EHCI_PS_PO;
  717. ehci_writel(status_reg, reg);
  718. break;
  719. } else {
  720. int ret;
  721. reg |= EHCI_PS_PR;
  722. reg &= ~EHCI_PS_PE;
  723. ehci_writel(status_reg, reg);
  724. /*
  725. * caller must wait, then call GetPortStatus
  726. * usb 2.0 specification say 50 ms resets on
  727. * root
  728. */
  729. ehci_powerup_fixup(status_reg, &reg);
  730. ehci_writel(status_reg, reg & ~EHCI_PS_PR);
  731. /*
  732. * A host controller must terminate the reset
  733. * and stabilize the state of the port within
  734. * 2 milliseconds
  735. */
  736. ret = handshake(status_reg, EHCI_PS_PR, 0,
  737. 2 * 1000);
  738. if (!ret)
  739. ctrl->portreset |=
  740. 1 << le16_to_cpu(req->index);
  741. else
  742. printf("port(%d) reset error\n",
  743. le16_to_cpu(req->index) - 1);
  744. }
  745. break;
  746. default:
  747. debug("unknown feature %x\n", le16_to_cpu(req->value));
  748. goto unknown;
  749. }
  750. /* unblock posted writes */
  751. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  752. break;
  753. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  754. reg = ehci_readl(status_reg);
  755. switch (le16_to_cpu(req->value)) {
  756. case USB_PORT_FEAT_ENABLE:
  757. reg &= ~EHCI_PS_PE;
  758. break;
  759. case USB_PORT_FEAT_C_ENABLE:
  760. reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_PE;
  761. break;
  762. case USB_PORT_FEAT_POWER:
  763. if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
  764. reg = reg & ~(EHCI_PS_CLEAR | EHCI_PS_PP);
  765. case USB_PORT_FEAT_C_CONNECTION:
  766. reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_CSC;
  767. break;
  768. case USB_PORT_FEAT_OVER_CURRENT:
  769. reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_OCC;
  770. break;
  771. case USB_PORT_FEAT_C_RESET:
  772. ctrl->portreset &= ~(1 << le16_to_cpu(req->index));
  773. break;
  774. default:
  775. debug("unknown feature %x\n", le16_to_cpu(req->value));
  776. goto unknown;
  777. }
  778. ehci_writel(status_reg, reg);
  779. /* unblock posted write */
  780. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  781. break;
  782. default:
  783. debug("Unknown request\n");
  784. goto unknown;
  785. }
  786. mdelay(1);
  787. len = min3(srclen, le16_to_cpu(req->length), length);
  788. if (srcptr != NULL && len > 0)
  789. memcpy(buffer, srcptr, len);
  790. else
  791. debug("Len is 0\n");
  792. dev->act_len = len;
  793. dev->status = 0;
  794. return 0;
  795. unknown:
  796. debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
  797. req->requesttype, req->request, le16_to_cpu(req->value),
  798. le16_to_cpu(req->index), le16_to_cpu(req->length));
  799. dev->act_len = 0;
  800. dev->status = USB_ST_STALLED;
  801. return -1;
  802. }
  803. int usb_lowlevel_stop(int index)
  804. {
  805. return ehci_hcd_stop(index);
  806. }
  807. int usb_lowlevel_init(int index, void **controller)
  808. {
  809. uint32_t reg;
  810. uint32_t cmd;
  811. struct QH *qh_list;
  812. struct QH *periodic;
  813. int i;
  814. if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
  815. return -1;
  816. /* EHCI spec section 4.1 */
  817. if (ehci_reset(index))
  818. return -1;
  819. #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
  820. if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
  821. return -1;
  822. #endif
  823. /* Set the high address word (aka segment) for 64-bit controller */
  824. if (ehci_readl(&ehcic[index].hccr->cr_hccparams) & 1)
  825. ehci_writel(ehcic[index].hcor->or_ctrldssegment, 0);
  826. qh_list = &ehcic[index].qh_list;
  827. /* Set head of reclaim list */
  828. memset(qh_list, 0, sizeof(*qh_list));
  829. qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
  830. qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
  831. QH_ENDPT1_EPS(USB_SPEED_HIGH));
  832. qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
  833. qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  834. qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  835. qh_list->qh_overlay.qt_token =
  836. cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
  837. /* Set async. queue head pointer. */
  838. ehci_writel(&ehcic[index].hcor->or_asynclistaddr, (uint32_t)qh_list);
  839. /*
  840. * Set up periodic list
  841. * Step 1: Parent QH for all periodic transfers.
  842. */
  843. periodic = &ehcic[index].periodic_queue;
  844. memset(periodic, 0, sizeof(*periodic));
  845. periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
  846. periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  847. periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  848. /*
  849. * Step 2: Setup frame-list: Every microframe, USB tries the same list.
  850. * In particular, device specifications on polling frequency
  851. * are disregarded. Keyboards seem to send NAK/NYet reliably
  852. * when polled with an empty buffer.
  853. *
  854. * Split Transactions will be spread across microframes using
  855. * S-mask and C-mask.
  856. */
  857. ehcic[index].periodic_list = memalign(4096, 1024*4);
  858. if (!ehcic[index].periodic_list)
  859. return -ENOMEM;
  860. for (i = 0; i < 1024; i++) {
  861. ehcic[index].periodic_list[i] = (uint32_t)periodic
  862. | QH_LINK_TYPE_QH;
  863. }
  864. /* Set periodic list base address */
  865. ehci_writel(&ehcic[index].hcor->or_periodiclistbase,
  866. (uint32_t)ehcic[index].periodic_list);
  867. reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
  868. descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
  869. debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  870. /* Port Indicators */
  871. if (HCS_INDICATOR(reg))
  872. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  873. | 0x80, &descriptor.hub.wHubCharacteristics);
  874. /* Port Power Control */
  875. if (HCS_PPC(reg))
  876. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  877. | 0x01, &descriptor.hub.wHubCharacteristics);
  878. /* Start the host controller. */
  879. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  880. /*
  881. * Philips, Intel, and maybe others need CMD_RUN before the
  882. * root hub will detect new devices (why?); NEC doesn't
  883. */
  884. cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  885. cmd |= CMD_RUN;
  886. ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
  887. /* take control over the ports */
  888. cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
  889. cmd |= FLAG_CF;
  890. ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
  891. /* unblock posted write */
  892. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  893. mdelay(5);
  894. reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
  895. printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
  896. ehcic[index].rootdev = 0;
  897. *controller = &ehcic[index];
  898. return 0;
  899. }
  900. int
  901. submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  902. int length)
  903. {
  904. if (usb_pipetype(pipe) != PIPE_BULK) {
  905. debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
  906. return -1;
  907. }
  908. return ehci_submit_async(dev, pipe, buffer, length, NULL);
  909. }
  910. int
  911. submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  912. int length, struct devrequest *setup)
  913. {
  914. struct ehci_ctrl *ctrl = dev->controller;
  915. if (usb_pipetype(pipe) != PIPE_CONTROL) {
  916. debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
  917. return -1;
  918. }
  919. if (usb_pipedevice(pipe) == ctrl->rootdev) {
  920. if (!ctrl->rootdev)
  921. dev->speed = USB_SPEED_HIGH;
  922. return ehci_submit_root(dev, pipe, buffer, length, setup);
  923. }
  924. return ehci_submit_async(dev, pipe, buffer, length, setup);
  925. }
  926. struct int_queue {
  927. struct QH *first;
  928. struct QH *current;
  929. struct QH *last;
  930. struct qTD *tds;
  931. };
  932. #define NEXT_QH(qh) (struct QH *)((qh)->qh_link & ~0x1f)
  933. static int
  934. enable_periodic(struct ehci_ctrl *ctrl)
  935. {
  936. uint32_t cmd;
  937. struct ehci_hcor *hcor = ctrl->hcor;
  938. int ret;
  939. cmd = ehci_readl(&hcor->or_usbcmd);
  940. cmd |= CMD_PSE;
  941. ehci_writel(&hcor->or_usbcmd, cmd);
  942. ret = handshake((uint32_t *)&hcor->or_usbsts,
  943. STS_PSS, STS_PSS, 100 * 1000);
  944. if (ret < 0) {
  945. printf("EHCI failed: timeout when enabling periodic list\n");
  946. return -ETIMEDOUT;
  947. }
  948. udelay(1000);
  949. return 0;
  950. }
  951. static int
  952. disable_periodic(struct ehci_ctrl *ctrl)
  953. {
  954. uint32_t cmd;
  955. struct ehci_hcor *hcor = ctrl->hcor;
  956. int ret;
  957. cmd = ehci_readl(&hcor->or_usbcmd);
  958. cmd &= ~CMD_PSE;
  959. ehci_writel(&hcor->or_usbcmd, cmd);
  960. ret = handshake((uint32_t *)&hcor->or_usbsts,
  961. STS_PSS, 0, 100 * 1000);
  962. if (ret < 0) {
  963. printf("EHCI failed: timeout when disabling periodic list\n");
  964. return -ETIMEDOUT;
  965. }
  966. return 0;
  967. }
  968. static int periodic_schedules;
  969. struct int_queue *
  970. create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
  971. int elementsize, void *buffer)
  972. {
  973. struct ehci_ctrl *ctrl = dev->controller;
  974. struct int_queue *result = NULL;
  975. int i;
  976. debug("Enter create_int_queue\n");
  977. if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
  978. debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
  979. return NULL;
  980. }
  981. /* limit to 4 full pages worth of data -
  982. * we can safely fit them in a single TD,
  983. * no matter the alignment
  984. */
  985. if (elementsize >= 16384) {
  986. debug("too large elements for interrupt transfers\n");
  987. return NULL;
  988. }
  989. result = malloc(sizeof(*result));
  990. if (!result) {
  991. debug("ehci intr queue: out of memory\n");
  992. goto fail1;
  993. }
  994. result->first = memalign(32, sizeof(struct QH) * queuesize);
  995. if (!result->first) {
  996. debug("ehci intr queue: out of memory\n");
  997. goto fail2;
  998. }
  999. result->current = result->first;
  1000. result->last = result->first + queuesize - 1;
  1001. result->tds = memalign(32, sizeof(struct qTD) * queuesize);
  1002. if (!result->tds) {
  1003. debug("ehci intr queue: out of memory\n");
  1004. goto fail3;
  1005. }
  1006. memset(result->first, 0, sizeof(struct QH) * queuesize);
  1007. memset(result->tds, 0, sizeof(struct qTD) * queuesize);
  1008. for (i = 0; i < queuesize; i++) {
  1009. struct QH *qh = result->first + i;
  1010. struct qTD *td = result->tds + i;
  1011. void **buf = &qh->buffer;
  1012. qh->qh_link = (uint32_t)(qh+1) | QH_LINK_TYPE_QH;
  1013. if (i == queuesize - 1)
  1014. qh->qh_link = QH_LINK_TERMINATE;
  1015. qh->qh_overlay.qt_next = (uint32_t)td;
  1016. qh->qh_endpt1 = (0 << 28) | /* No NAK reload (ehci 4.9) */
  1017. (usb_maxpacket(dev, pipe) << 16) | /* MPS */
  1018. (1 << 14) |
  1019. QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
  1020. (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
  1021. (usb_pipedevice(pipe) << 0);
  1022. qh->qh_endpt2 = (1 << 30) | /* 1 Tx per mframe */
  1023. (1 << 0); /* S-mask: microframe 0 */
  1024. if (dev->speed == USB_SPEED_LOW ||
  1025. dev->speed == USB_SPEED_FULL) {
  1026. debug("TT: port: %d, hub address: %d\n",
  1027. dev->portnr, dev->parent->devnum);
  1028. qh->qh_endpt2 |= (dev->portnr << 23) |
  1029. (dev->parent->devnum << 16) |
  1030. (0x1c << 8); /* C-mask: microframes 2-4 */
  1031. }
  1032. td->qt_next = QT_NEXT_TERMINATE;
  1033. td->qt_altnext = QT_NEXT_TERMINATE;
  1034. debug("communication direction is '%s'\n",
  1035. usb_pipein(pipe) ? "in" : "out");
  1036. td->qt_token = (elementsize << 16) |
  1037. ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
  1038. 0x80; /* active */
  1039. td->qt_buffer[0] = (uint32_t)buffer + i * elementsize;
  1040. td->qt_buffer[1] = (td->qt_buffer[0] + 0x1000) & ~0xfff;
  1041. td->qt_buffer[2] = (td->qt_buffer[0] + 0x2000) & ~0xfff;
  1042. td->qt_buffer[3] = (td->qt_buffer[0] + 0x3000) & ~0xfff;
  1043. td->qt_buffer[4] = (td->qt_buffer[0] + 0x4000) & ~0xfff;
  1044. *buf = buffer + i * elementsize;
  1045. }
  1046. if (disable_periodic(ctrl) < 0) {
  1047. debug("FATAL: periodic should never fail, but did");
  1048. goto fail3;
  1049. }
  1050. /* hook up to periodic list */
  1051. struct QH *list = &ctrl->periodic_queue;
  1052. result->last->qh_link = list->qh_link;
  1053. list->qh_link = (uint32_t)result->first | QH_LINK_TYPE_QH;
  1054. if (enable_periodic(ctrl) < 0) {
  1055. debug("FATAL: periodic should never fail, but did");
  1056. goto fail3;
  1057. }
  1058. periodic_schedules++;
  1059. debug("Exit create_int_queue\n");
  1060. return result;
  1061. fail3:
  1062. if (result->tds)
  1063. free(result->tds);
  1064. fail2:
  1065. if (result->first)
  1066. free(result->first);
  1067. if (result)
  1068. free(result);
  1069. fail1:
  1070. return NULL;
  1071. }
  1072. void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
  1073. {
  1074. struct QH *cur = queue->current;
  1075. /* depleted queue */
  1076. if (cur == NULL) {
  1077. debug("Exit poll_int_queue with completed queue\n");
  1078. return NULL;
  1079. }
  1080. /* still active */
  1081. if (cur->qh_overlay.qt_token & 0x80) {
  1082. debug("Exit poll_int_queue with no completed intr transfer. "
  1083. "token is %x\n", cur->qh_overlay.qt_token);
  1084. return NULL;
  1085. }
  1086. if (!(cur->qh_link & QH_LINK_TERMINATE))
  1087. queue->current++;
  1088. else
  1089. queue->current = NULL;
  1090. debug("Exit poll_int_queue with completed intr transfer. "
  1091. "token is %x at %p (first at %p)\n", cur->qh_overlay.qt_token,
  1092. &cur->qh_overlay.qt_token, queue->first);
  1093. return cur->buffer;
  1094. }
  1095. /* Do not free buffers associated with QHs, they're owned by someone else */
  1096. int
  1097. destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
  1098. {
  1099. struct ehci_ctrl *ctrl = dev->controller;
  1100. int result = -1;
  1101. unsigned long timeout;
  1102. if (disable_periodic(ctrl) < 0) {
  1103. debug("FATAL: periodic should never fail, but did");
  1104. goto out;
  1105. }
  1106. periodic_schedules--;
  1107. struct QH *cur = &ctrl->periodic_queue;
  1108. timeout = get_timer(0) + 500; /* abort after 500ms */
  1109. while (!(cur->qh_link & QH_LINK_TERMINATE)) {
  1110. debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
  1111. if (NEXT_QH(cur) == queue->first) {
  1112. debug("found candidate. removing from chain\n");
  1113. cur->qh_link = queue->last->qh_link;
  1114. result = 0;
  1115. break;
  1116. }
  1117. cur = NEXT_QH(cur);
  1118. if (get_timer(0) > timeout) {
  1119. printf("Timeout destroying interrupt endpoint queue\n");
  1120. result = -1;
  1121. goto out;
  1122. }
  1123. }
  1124. if (periodic_schedules > 0) {
  1125. result = enable_periodic(ctrl);
  1126. if (result < 0)
  1127. debug("FATAL: periodic should never fail, but did");
  1128. }
  1129. out:
  1130. free(queue->tds);
  1131. free(queue->first);
  1132. free(queue);
  1133. return result;
  1134. }
  1135. int
  1136. submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  1137. int length, int interval)
  1138. {
  1139. void *backbuffer;
  1140. struct int_queue *queue;
  1141. unsigned long timeout;
  1142. int result = 0, ret;
  1143. debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
  1144. dev, pipe, buffer, length, interval);
  1145. /*
  1146. * Interrupt transfers requiring several transactions are not supported
  1147. * because bInterval is ignored.
  1148. *
  1149. * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
  1150. * <= PKT_ALIGN if several qTDs are required, while the USB
  1151. * specification does not constrain this for interrupt transfers. That
  1152. * means that ehci_submit_async() would support interrupt transfers
  1153. * requiring several transactions only as long as the transfer size does
  1154. * not require more than a single qTD.
  1155. */
  1156. if (length > usb_maxpacket(dev, pipe)) {
  1157. printf("%s: Interrupt transfers requiring several "
  1158. "transactions are not supported.\n", __func__);
  1159. return -1;
  1160. }
  1161. queue = create_int_queue(dev, pipe, 1, length, buffer);
  1162. timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
  1163. while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
  1164. if (get_timer(0) > timeout) {
  1165. printf("Timeout poll on interrupt endpoint\n");
  1166. result = -ETIMEDOUT;
  1167. break;
  1168. }
  1169. if (backbuffer != buffer) {
  1170. debug("got wrong buffer back (%x instead of %x)\n",
  1171. (uint32_t)backbuffer, (uint32_t)buffer);
  1172. return -EINVAL;
  1173. }
  1174. ret = destroy_int_queue(dev, queue);
  1175. if (ret < 0)
  1176. return ret;
  1177. /* everything worked out fine */
  1178. return result;
  1179. }