ehci-hcd.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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. int
  554. ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
  555. int length, struct devrequest *req)
  556. {
  557. uint8_t tmpbuf[4];
  558. u16 typeReq;
  559. void *srcptr = NULL;
  560. int len, srclen;
  561. uint32_t reg;
  562. uint32_t *status_reg;
  563. int port = le16_to_cpu(req->index) & 0xff;
  564. struct ehci_ctrl *ctrl = dev->controller;
  565. srclen = 0;
  566. debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
  567. req->request, req->request,
  568. req->requesttype, req->requesttype,
  569. le16_to_cpu(req->value), le16_to_cpu(req->index));
  570. typeReq = req->request | req->requesttype << 8;
  571. switch (typeReq) {
  572. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  573. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  574. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  575. if (!port || port > CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
  576. printf("The request port(%d) is not configured\n", port - 1);
  577. return -1;
  578. }
  579. status_reg = (uint32_t *)&ctrl->hcor->or_portsc[port - 1];
  580. break;
  581. default:
  582. status_reg = NULL;
  583. break;
  584. }
  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 << port))
  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. port - 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 |= 1 << port;
  740. else
  741. printf("port(%d) reset error\n",
  742. port - 1);
  743. }
  744. break;
  745. case USB_PORT_FEAT_TEST:
  746. reg &= ~(0xf << 16);
  747. reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
  748. ehci_writel(status_reg, reg);
  749. break;
  750. default:
  751. debug("unknown feature %x\n", le16_to_cpu(req->value));
  752. goto unknown;
  753. }
  754. /* unblock posted writes */
  755. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  756. break;
  757. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  758. reg = ehci_readl(status_reg);
  759. reg &= ~EHCI_PS_CLEAR;
  760. switch (le16_to_cpu(req->value)) {
  761. case USB_PORT_FEAT_ENABLE:
  762. reg &= ~EHCI_PS_PE;
  763. break;
  764. case USB_PORT_FEAT_C_ENABLE:
  765. reg |= EHCI_PS_PE;
  766. break;
  767. case USB_PORT_FEAT_POWER:
  768. if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
  769. reg &= ~EHCI_PS_PP;
  770. break;
  771. case USB_PORT_FEAT_C_CONNECTION:
  772. reg |= EHCI_PS_CSC;
  773. break;
  774. case USB_PORT_FEAT_OVER_CURRENT:
  775. reg |= EHCI_PS_OCC;
  776. break;
  777. case USB_PORT_FEAT_C_RESET:
  778. ctrl->portreset &= ~(1 << port);
  779. break;
  780. default:
  781. debug("unknown feature %x\n", le16_to_cpu(req->value));
  782. goto unknown;
  783. }
  784. ehci_writel(status_reg, reg);
  785. /* unblock posted write */
  786. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  787. break;
  788. default:
  789. debug("Unknown request\n");
  790. goto unknown;
  791. }
  792. mdelay(1);
  793. len = min3(srclen, le16_to_cpu(req->length), length);
  794. if (srcptr != NULL && len > 0)
  795. memcpy(buffer, srcptr, len);
  796. else
  797. debug("Len is 0\n");
  798. dev->act_len = len;
  799. dev->status = 0;
  800. return 0;
  801. unknown:
  802. debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
  803. req->requesttype, req->request, le16_to_cpu(req->value),
  804. le16_to_cpu(req->index), le16_to_cpu(req->length));
  805. dev->act_len = 0;
  806. dev->status = USB_ST_STALLED;
  807. return -1;
  808. }
  809. int usb_lowlevel_stop(int index)
  810. {
  811. return ehci_hcd_stop(index);
  812. }
  813. int usb_lowlevel_init(int index, void **controller)
  814. {
  815. uint32_t reg;
  816. uint32_t cmd;
  817. struct QH *qh_list;
  818. struct QH *periodic;
  819. int i;
  820. if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
  821. return -1;
  822. /* EHCI spec section 4.1 */
  823. if (ehci_reset(index))
  824. return -1;
  825. #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
  826. if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
  827. return -1;
  828. #endif
  829. /* Set the high address word (aka segment) for 64-bit controller */
  830. if (ehci_readl(&ehcic[index].hccr->cr_hccparams) & 1)
  831. ehci_writel(ehcic[index].hcor->or_ctrldssegment, 0);
  832. qh_list = &ehcic[index].qh_list;
  833. /* Set head of reclaim list */
  834. memset(qh_list, 0, sizeof(*qh_list));
  835. qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
  836. qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
  837. QH_ENDPT1_EPS(USB_SPEED_HIGH));
  838. qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
  839. qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  840. qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  841. qh_list->qh_overlay.qt_token =
  842. cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
  843. /* Set async. queue head pointer. */
  844. ehci_writel(&ehcic[index].hcor->or_asynclistaddr, (uint32_t)qh_list);
  845. /*
  846. * Set up periodic list
  847. * Step 1: Parent QH for all periodic transfers.
  848. */
  849. periodic = &ehcic[index].periodic_queue;
  850. memset(periodic, 0, sizeof(*periodic));
  851. periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
  852. periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  853. periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  854. /*
  855. * Step 2: Setup frame-list: Every microframe, USB tries the same list.
  856. * In particular, device specifications on polling frequency
  857. * are disregarded. Keyboards seem to send NAK/NYet reliably
  858. * when polled with an empty buffer.
  859. *
  860. * Split Transactions will be spread across microframes using
  861. * S-mask and C-mask.
  862. */
  863. ehcic[index].periodic_list = memalign(4096, 1024*4);
  864. if (!ehcic[index].periodic_list)
  865. return -ENOMEM;
  866. for (i = 0; i < 1024; i++) {
  867. ehcic[index].periodic_list[i] = (uint32_t)periodic
  868. | QH_LINK_TYPE_QH;
  869. }
  870. /* Set periodic list base address */
  871. ehci_writel(&ehcic[index].hcor->or_periodiclistbase,
  872. (uint32_t)ehcic[index].periodic_list);
  873. reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
  874. descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
  875. debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  876. /* Port Indicators */
  877. if (HCS_INDICATOR(reg))
  878. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  879. | 0x80, &descriptor.hub.wHubCharacteristics);
  880. /* Port Power Control */
  881. if (HCS_PPC(reg))
  882. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  883. | 0x01, &descriptor.hub.wHubCharacteristics);
  884. /* Start the host controller. */
  885. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  886. /*
  887. * Philips, Intel, and maybe others need CMD_RUN before the
  888. * root hub will detect new devices (why?); NEC doesn't
  889. */
  890. cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  891. cmd |= CMD_RUN;
  892. ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
  893. /* take control over the ports */
  894. cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
  895. cmd |= FLAG_CF;
  896. ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
  897. /* unblock posted write */
  898. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  899. mdelay(5);
  900. reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
  901. printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
  902. ehcic[index].rootdev = 0;
  903. *controller = &ehcic[index];
  904. return 0;
  905. }
  906. int
  907. submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  908. int length)
  909. {
  910. if (usb_pipetype(pipe) != PIPE_BULK) {
  911. debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
  912. return -1;
  913. }
  914. return ehci_submit_async(dev, pipe, buffer, length, NULL);
  915. }
  916. int
  917. submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  918. int length, struct devrequest *setup)
  919. {
  920. struct ehci_ctrl *ctrl = dev->controller;
  921. if (usb_pipetype(pipe) != PIPE_CONTROL) {
  922. debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
  923. return -1;
  924. }
  925. if (usb_pipedevice(pipe) == ctrl->rootdev) {
  926. if (!ctrl->rootdev)
  927. dev->speed = USB_SPEED_HIGH;
  928. return ehci_submit_root(dev, pipe, buffer, length, setup);
  929. }
  930. return ehci_submit_async(dev, pipe, buffer, length, setup);
  931. }
  932. struct int_queue {
  933. struct QH *first;
  934. struct QH *current;
  935. struct QH *last;
  936. struct qTD *tds;
  937. };
  938. #define NEXT_QH(qh) (struct QH *)((qh)->qh_link & ~0x1f)
  939. static int
  940. enable_periodic(struct ehci_ctrl *ctrl)
  941. {
  942. uint32_t cmd;
  943. struct ehci_hcor *hcor = ctrl->hcor;
  944. int ret;
  945. cmd = ehci_readl(&hcor->or_usbcmd);
  946. cmd |= CMD_PSE;
  947. ehci_writel(&hcor->or_usbcmd, cmd);
  948. ret = handshake((uint32_t *)&hcor->or_usbsts,
  949. STS_PSS, STS_PSS, 100 * 1000);
  950. if (ret < 0) {
  951. printf("EHCI failed: timeout when enabling periodic list\n");
  952. return -ETIMEDOUT;
  953. }
  954. udelay(1000);
  955. return 0;
  956. }
  957. static int
  958. disable_periodic(struct ehci_ctrl *ctrl)
  959. {
  960. uint32_t cmd;
  961. struct ehci_hcor *hcor = ctrl->hcor;
  962. int ret;
  963. cmd = ehci_readl(&hcor->or_usbcmd);
  964. cmd &= ~CMD_PSE;
  965. ehci_writel(&hcor->or_usbcmd, cmd);
  966. ret = handshake((uint32_t *)&hcor->or_usbsts,
  967. STS_PSS, 0, 100 * 1000);
  968. if (ret < 0) {
  969. printf("EHCI failed: timeout when disabling periodic list\n");
  970. return -ETIMEDOUT;
  971. }
  972. return 0;
  973. }
  974. static int periodic_schedules;
  975. struct int_queue *
  976. create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
  977. int elementsize, void *buffer)
  978. {
  979. struct ehci_ctrl *ctrl = dev->controller;
  980. struct int_queue *result = NULL;
  981. int i;
  982. debug("Enter create_int_queue\n");
  983. if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
  984. debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
  985. return NULL;
  986. }
  987. /* limit to 4 full pages worth of data -
  988. * we can safely fit them in a single TD,
  989. * no matter the alignment
  990. */
  991. if (elementsize >= 16384) {
  992. debug("too large elements for interrupt transfers\n");
  993. return NULL;
  994. }
  995. result = malloc(sizeof(*result));
  996. if (!result) {
  997. debug("ehci intr queue: out of memory\n");
  998. goto fail1;
  999. }
  1000. result->first = memalign(32, sizeof(struct QH) * queuesize);
  1001. if (!result->first) {
  1002. debug("ehci intr queue: out of memory\n");
  1003. goto fail2;
  1004. }
  1005. result->current = result->first;
  1006. result->last = result->first + queuesize - 1;
  1007. result->tds = memalign(32, sizeof(struct qTD) * queuesize);
  1008. if (!result->tds) {
  1009. debug("ehci intr queue: out of memory\n");
  1010. goto fail3;
  1011. }
  1012. memset(result->first, 0, sizeof(struct QH) * queuesize);
  1013. memset(result->tds, 0, sizeof(struct qTD) * queuesize);
  1014. for (i = 0; i < queuesize; i++) {
  1015. struct QH *qh = result->first + i;
  1016. struct qTD *td = result->tds + i;
  1017. void **buf = &qh->buffer;
  1018. qh->qh_link = (uint32_t)(qh+1) | QH_LINK_TYPE_QH;
  1019. if (i == queuesize - 1)
  1020. qh->qh_link = QH_LINK_TERMINATE;
  1021. qh->qh_overlay.qt_next = (uint32_t)td;
  1022. qh->qh_endpt1 = (0 << 28) | /* No NAK reload (ehci 4.9) */
  1023. (usb_maxpacket(dev, pipe) << 16) | /* MPS */
  1024. (1 << 14) |
  1025. QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
  1026. (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
  1027. (usb_pipedevice(pipe) << 0);
  1028. qh->qh_endpt2 = (1 << 30) | /* 1 Tx per mframe */
  1029. (1 << 0); /* S-mask: microframe 0 */
  1030. if (dev->speed == USB_SPEED_LOW ||
  1031. dev->speed == USB_SPEED_FULL) {
  1032. debug("TT: port: %d, hub address: %d\n",
  1033. dev->portnr, dev->parent->devnum);
  1034. qh->qh_endpt2 |= (dev->portnr << 23) |
  1035. (dev->parent->devnum << 16) |
  1036. (0x1c << 8); /* C-mask: microframes 2-4 */
  1037. }
  1038. td->qt_next = QT_NEXT_TERMINATE;
  1039. td->qt_altnext = QT_NEXT_TERMINATE;
  1040. debug("communication direction is '%s'\n",
  1041. usb_pipein(pipe) ? "in" : "out");
  1042. td->qt_token = (elementsize << 16) |
  1043. ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
  1044. 0x80; /* active */
  1045. td->qt_buffer[0] = (uint32_t)buffer + i * elementsize;
  1046. td->qt_buffer[1] = (td->qt_buffer[0] + 0x1000) & ~0xfff;
  1047. td->qt_buffer[2] = (td->qt_buffer[0] + 0x2000) & ~0xfff;
  1048. td->qt_buffer[3] = (td->qt_buffer[0] + 0x3000) & ~0xfff;
  1049. td->qt_buffer[4] = (td->qt_buffer[0] + 0x4000) & ~0xfff;
  1050. *buf = buffer + i * elementsize;
  1051. }
  1052. if (disable_periodic(ctrl) < 0) {
  1053. debug("FATAL: periodic should never fail, but did");
  1054. goto fail3;
  1055. }
  1056. /* hook up to periodic list */
  1057. struct QH *list = &ctrl->periodic_queue;
  1058. result->last->qh_link = list->qh_link;
  1059. list->qh_link = (uint32_t)result->first | QH_LINK_TYPE_QH;
  1060. if (enable_periodic(ctrl) < 0) {
  1061. debug("FATAL: periodic should never fail, but did");
  1062. goto fail3;
  1063. }
  1064. periodic_schedules++;
  1065. debug("Exit create_int_queue\n");
  1066. return result;
  1067. fail3:
  1068. if (result->tds)
  1069. free(result->tds);
  1070. fail2:
  1071. if (result->first)
  1072. free(result->first);
  1073. if (result)
  1074. free(result);
  1075. fail1:
  1076. return NULL;
  1077. }
  1078. void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
  1079. {
  1080. struct QH *cur = queue->current;
  1081. /* depleted queue */
  1082. if (cur == NULL) {
  1083. debug("Exit poll_int_queue with completed queue\n");
  1084. return NULL;
  1085. }
  1086. /* still active */
  1087. if (cur->qh_overlay.qt_token & 0x80) {
  1088. debug("Exit poll_int_queue with no completed intr transfer. "
  1089. "token is %x\n", cur->qh_overlay.qt_token);
  1090. return NULL;
  1091. }
  1092. if (!(cur->qh_link & QH_LINK_TERMINATE))
  1093. queue->current++;
  1094. else
  1095. queue->current = NULL;
  1096. debug("Exit poll_int_queue with completed intr transfer. "
  1097. "token is %x at %p (first at %p)\n", cur->qh_overlay.qt_token,
  1098. &cur->qh_overlay.qt_token, queue->first);
  1099. return cur->buffer;
  1100. }
  1101. /* Do not free buffers associated with QHs, they're owned by someone else */
  1102. int
  1103. destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
  1104. {
  1105. struct ehci_ctrl *ctrl = dev->controller;
  1106. int result = -1;
  1107. unsigned long timeout;
  1108. if (disable_periodic(ctrl) < 0) {
  1109. debug("FATAL: periodic should never fail, but did");
  1110. goto out;
  1111. }
  1112. periodic_schedules--;
  1113. struct QH *cur = &ctrl->periodic_queue;
  1114. timeout = get_timer(0) + 500; /* abort after 500ms */
  1115. while (!(cur->qh_link & QH_LINK_TERMINATE)) {
  1116. debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
  1117. if (NEXT_QH(cur) == queue->first) {
  1118. debug("found candidate. removing from chain\n");
  1119. cur->qh_link = queue->last->qh_link;
  1120. result = 0;
  1121. break;
  1122. }
  1123. cur = NEXT_QH(cur);
  1124. if (get_timer(0) > timeout) {
  1125. printf("Timeout destroying interrupt endpoint queue\n");
  1126. result = -1;
  1127. goto out;
  1128. }
  1129. }
  1130. if (periodic_schedules > 0) {
  1131. result = enable_periodic(ctrl);
  1132. if (result < 0)
  1133. debug("FATAL: periodic should never fail, but did");
  1134. }
  1135. out:
  1136. free(queue->tds);
  1137. free(queue->first);
  1138. free(queue);
  1139. return result;
  1140. }
  1141. int
  1142. submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  1143. int length, int interval)
  1144. {
  1145. void *backbuffer;
  1146. struct int_queue *queue;
  1147. unsigned long timeout;
  1148. int result = 0, ret;
  1149. debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
  1150. dev, pipe, buffer, length, interval);
  1151. /*
  1152. * Interrupt transfers requiring several transactions are not supported
  1153. * because bInterval is ignored.
  1154. *
  1155. * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
  1156. * <= PKT_ALIGN if several qTDs are required, while the USB
  1157. * specification does not constrain this for interrupt transfers. That
  1158. * means that ehci_submit_async() would support interrupt transfers
  1159. * requiring several transactions only as long as the transfer size does
  1160. * not require more than a single qTD.
  1161. */
  1162. if (length > usb_maxpacket(dev, pipe)) {
  1163. printf("%s: Interrupt transfers requiring several "
  1164. "transactions are not supported.\n", __func__);
  1165. return -1;
  1166. }
  1167. queue = create_int_queue(dev, pipe, 1, length, buffer);
  1168. timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
  1169. while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
  1170. if (get_timer(0) > timeout) {
  1171. printf("Timeout poll on interrupt endpoint\n");
  1172. result = -ETIMEDOUT;
  1173. break;
  1174. }
  1175. if (backbuffer != buffer) {
  1176. debug("got wrong buffer back (%x instead of %x)\n",
  1177. (uint32_t)backbuffer, (uint32_t)buffer);
  1178. return -EINVAL;
  1179. }
  1180. ret = destroy_int_queue(dev, queue);
  1181. if (ret < 0)
  1182. return ret;
  1183. /* everything worked out fine */
  1184. return result;
  1185. }