ehci-hcd.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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 <asm/byteorder.h>
  25. #include <asm/unaligned.h>
  26. #include <usb.h>
  27. #include <asm/io.h>
  28. #include <malloc.h>
  29. #include <watchdog.h>
  30. #include "ehci.h"
  31. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  32. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  33. #endif
  34. static struct ehci_ctrl {
  35. struct ehci_hccr *hccr; /* R/O registers, not need for volatile */
  36. struct ehci_hcor *hcor;
  37. int rootdev;
  38. uint16_t portreset;
  39. struct QH qh_list __attribute__((aligned(USB_DMA_MINALIGN)));
  40. } ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
  41. #define ALIGN_END_ADDR(type, ptr, size) \
  42. ((uint32_t)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
  43. static struct descriptor {
  44. struct usb_hub_descriptor hub;
  45. struct usb_device_descriptor device;
  46. struct usb_linux_config_descriptor config;
  47. struct usb_linux_interface_descriptor interface;
  48. struct usb_endpoint_descriptor endpoint;
  49. } __attribute__ ((packed)) descriptor = {
  50. {
  51. 0x8, /* bDescLength */
  52. 0x29, /* bDescriptorType: hub descriptor */
  53. 2, /* bNrPorts -- runtime modified */
  54. 0, /* wHubCharacteristics */
  55. 10, /* bPwrOn2PwrGood */
  56. 0, /* bHubCntrCurrent */
  57. {}, /* Device removable */
  58. {} /* at most 7 ports! XXX */
  59. },
  60. {
  61. 0x12, /* bLength */
  62. 1, /* bDescriptorType: UDESC_DEVICE */
  63. cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
  64. 9, /* bDeviceClass: UDCLASS_HUB */
  65. 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
  66. 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
  67. 64, /* bMaxPacketSize: 64 bytes */
  68. 0x0000, /* idVendor */
  69. 0x0000, /* idProduct */
  70. cpu_to_le16(0x0100), /* bcdDevice */
  71. 1, /* iManufacturer */
  72. 2, /* iProduct */
  73. 0, /* iSerialNumber */
  74. 1 /* bNumConfigurations: 1 */
  75. },
  76. {
  77. 0x9,
  78. 2, /* bDescriptorType: UDESC_CONFIG */
  79. cpu_to_le16(0x19),
  80. 1, /* bNumInterface */
  81. 1, /* bConfigurationValue */
  82. 0, /* iConfiguration */
  83. 0x40, /* bmAttributes: UC_SELF_POWER */
  84. 0 /* bMaxPower */
  85. },
  86. {
  87. 0x9, /* bLength */
  88. 4, /* bDescriptorType: UDESC_INTERFACE */
  89. 0, /* bInterfaceNumber */
  90. 0, /* bAlternateSetting */
  91. 1, /* bNumEndpoints */
  92. 9, /* bInterfaceClass: UICLASS_HUB */
  93. 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
  94. 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
  95. 0 /* iInterface */
  96. },
  97. {
  98. 0x7, /* bLength */
  99. 5, /* bDescriptorType: UDESC_ENDPOINT */
  100. 0x81, /* bEndpointAddress:
  101. * UE_DIR_IN | EHCI_INTR_ENDPT
  102. */
  103. 3, /* bmAttributes: UE_INTERRUPT */
  104. 8, /* wMaxPacketSize */
  105. 255 /* bInterval */
  106. },
  107. };
  108. #if defined(CONFIG_EHCI_IS_TDI)
  109. #define ehci_is_TDI() (1)
  110. #else
  111. #define ehci_is_TDI() (0)
  112. #endif
  113. void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
  114. {
  115. mdelay(50);
  116. }
  117. void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
  118. __attribute__((weak, alias("__ehci_powerup_fixup")));
  119. static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
  120. {
  121. uint32_t result;
  122. do {
  123. result = ehci_readl(ptr);
  124. udelay(5);
  125. if (result == ~(uint32_t)0)
  126. return -1;
  127. result &= mask;
  128. if (result == done)
  129. return 0;
  130. usec--;
  131. } while (usec > 0);
  132. return -1;
  133. }
  134. static int ehci_reset(int index)
  135. {
  136. uint32_t cmd;
  137. uint32_t tmp;
  138. uint32_t *reg_ptr;
  139. int ret = 0;
  140. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  141. cmd = (cmd & ~CMD_RUN) | CMD_RESET;
  142. ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
  143. ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
  144. CMD_RESET, 0, 250 * 1000);
  145. if (ret < 0) {
  146. printf("EHCI fail to reset\n");
  147. goto out;
  148. }
  149. if (ehci_is_TDI()) {
  150. reg_ptr = (uint32_t *)((u8 *)ehcic[index].hcor + USBMODE);
  151. tmp = ehci_readl(reg_ptr);
  152. tmp |= USBMODE_CM_HC;
  153. #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
  154. tmp |= USBMODE_BE;
  155. #endif
  156. ehci_writel(reg_ptr, tmp);
  157. }
  158. #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
  159. cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
  160. cmd &= ~TXFIFO_THRESH_MASK;
  161. cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
  162. ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
  163. #endif
  164. out:
  165. return ret;
  166. }
  167. static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
  168. {
  169. uint32_t delta, next;
  170. uint32_t addr = (uint32_t)buf;
  171. int idx;
  172. if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
  173. debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
  174. flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
  175. idx = 0;
  176. while (idx < QT_BUFFER_CNT) {
  177. td->qt_buffer[idx] = cpu_to_hc32(addr);
  178. td->qt_buffer_hi[idx] = 0;
  179. next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
  180. delta = next - addr;
  181. if (delta >= sz)
  182. break;
  183. sz -= delta;
  184. addr = next;
  185. idx++;
  186. }
  187. if (idx == QT_BUFFER_CNT) {
  188. printf("out of buffer pointers (%u bytes left)\n", sz);
  189. return -1;
  190. }
  191. return 0;
  192. }
  193. static int
  194. ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
  195. int length, struct devrequest *req)
  196. {
  197. ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
  198. struct qTD *qtd;
  199. int qtd_count = 0;
  200. int qtd_counter = 0;
  201. volatile struct qTD *vtd;
  202. unsigned long ts;
  203. uint32_t *tdp;
  204. uint32_t endpt, maxpacket, token, usbsts;
  205. uint32_t c, toggle;
  206. uint32_t cmd;
  207. int timeout;
  208. int ret = 0;
  209. struct ehci_ctrl *ctrl = dev->controller;
  210. debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
  211. buffer, length, req);
  212. if (req != NULL)
  213. debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
  214. req->request, req->request,
  215. req->requesttype, req->requesttype,
  216. le16_to_cpu(req->value), le16_to_cpu(req->value),
  217. le16_to_cpu(req->index));
  218. #define PKT_ALIGN 512
  219. /*
  220. * The USB transfer is split into qTD transfers. Eeach qTD transfer is
  221. * described by a transfer descriptor (the qTD). The qTDs form a linked
  222. * list with a queue head (QH).
  223. *
  224. * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
  225. * have its beginning in a qTD transfer and its end in the following
  226. * one, so the qTD transfer lengths have to be chosen accordingly.
  227. *
  228. * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
  229. * single pages. The first data buffer can start at any offset within a
  230. * page (not considering the cache-line alignment issues), while the
  231. * following buffers must be page-aligned. There is no alignment
  232. * constraint on the size of a qTD transfer.
  233. */
  234. if (req != NULL)
  235. /* 1 qTD will be needed for SETUP, and 1 for ACK. */
  236. qtd_count += 1 + 1;
  237. if (length > 0 || req == NULL) {
  238. /*
  239. * Determine the qTD transfer size that will be used for the
  240. * data payload (not considering the first qTD transfer, which
  241. * may be longer or shorter, and the final one, which may be
  242. * shorter).
  243. *
  244. * In order to keep each packet within a qTD transfer, the qTD
  245. * transfer size is aligned to PKT_ALIGN, which is a multiple of
  246. * wMaxPacketSize (except in some cases for interrupt transfers,
  247. * see comment in submit_int_msg()).
  248. *
  249. * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
  250. * QT_BUFFER_CNT full pages will be used.
  251. */
  252. int xfr_sz = QT_BUFFER_CNT;
  253. /*
  254. * However, if the input buffer is not aligned to PKT_ALIGN, the
  255. * qTD transfer size will be one page shorter, and the first qTD
  256. * data buffer of each transfer will be page-unaligned.
  257. */
  258. if ((uint32_t)buffer & (PKT_ALIGN - 1))
  259. xfr_sz--;
  260. /* Convert the qTD transfer size to bytes. */
  261. xfr_sz *= EHCI_PAGE_SIZE;
  262. /*
  263. * Approximate by excess the number of qTDs that will be
  264. * required for the data payload. The exact formula is way more
  265. * complicated and saves at most 2 qTDs, i.e. a total of 128
  266. * bytes.
  267. */
  268. qtd_count += 2 + length / xfr_sz;
  269. }
  270. /*
  271. * Threshold value based on the worst-case total size of the allocated qTDs for
  272. * a mass-storage transfer of 65535 blocks of 512 bytes.
  273. */
  274. #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
  275. #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
  276. #endif
  277. qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
  278. if (qtd == NULL) {
  279. printf("unable to allocate TDs\n");
  280. return -1;
  281. }
  282. memset(qh, 0, sizeof(struct QH));
  283. memset(qtd, 0, qtd_count * sizeof(*qtd));
  284. toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  285. /*
  286. * Setup QH (3.6 in ehci-r10.pdf)
  287. *
  288. * qh_link ................. 03-00 H
  289. * qh_endpt1 ............... 07-04 H
  290. * qh_endpt2 ............... 0B-08 H
  291. * - qh_curtd
  292. * qh_overlay.qt_next ...... 13-10 H
  293. * - qh_overlay.qt_altnext
  294. */
  295. qh->qh_link = cpu_to_hc32((uint32_t)&ctrl->qh_list | QH_LINK_TYPE_QH);
  296. c = usb_pipespeed(pipe) != USB_SPEED_HIGH && !usb_pipeendpoint(pipe);
  297. maxpacket = usb_maxpacket(dev, pipe);
  298. endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
  299. QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
  300. QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
  301. QH_ENDPT1_EPS(usb_pipespeed(pipe)) |
  302. QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
  303. QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
  304. qh->qh_endpt1 = cpu_to_hc32(endpt);
  305. endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_PORTNUM(dev->portnr) |
  306. QH_ENDPT2_HUBADDR(dev->parent->devnum) |
  307. QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
  308. qh->qh_endpt2 = cpu_to_hc32(endpt);
  309. qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  310. tdp = &qh->qh_overlay.qt_next;
  311. if (req != NULL) {
  312. /*
  313. * Setup request qTD (3.5 in ehci-r10.pdf)
  314. *
  315. * qt_next ................ 03-00 H
  316. * qt_altnext ............. 07-04 H
  317. * qt_token ............... 0B-08 H
  318. *
  319. * [ buffer, buffer_hi ] loaded with "req".
  320. */
  321. qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  322. qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  323. token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
  324. QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
  325. QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
  326. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  327. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  328. if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
  329. printf("unable to construct SETUP TD\n");
  330. goto fail;
  331. }
  332. /* Update previous qTD! */
  333. *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
  334. tdp = &qtd[qtd_counter++].qt_next;
  335. toggle = 1;
  336. }
  337. if (length > 0 || req == NULL) {
  338. uint8_t *buf_ptr = buffer;
  339. int left_length = length;
  340. do {
  341. /*
  342. * Determine the size of this qTD transfer. By default,
  343. * QT_BUFFER_CNT full pages can be used.
  344. */
  345. int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
  346. /*
  347. * However, if the input buffer is not page-aligned, the
  348. * portion of the first page before the buffer start
  349. * offset within that page is unusable.
  350. */
  351. xfr_bytes -= (uint32_t)buf_ptr & (EHCI_PAGE_SIZE - 1);
  352. /*
  353. * In order to keep each packet within a qTD transfer,
  354. * align the qTD transfer size to PKT_ALIGN.
  355. */
  356. xfr_bytes &= ~(PKT_ALIGN - 1);
  357. /*
  358. * This transfer may be shorter than the available qTD
  359. * transfer size that has just been computed.
  360. */
  361. xfr_bytes = min(xfr_bytes, left_length);
  362. /*
  363. * Setup request qTD (3.5 in ehci-r10.pdf)
  364. *
  365. * qt_next ................ 03-00 H
  366. * qt_altnext ............. 07-04 H
  367. * qt_token ............... 0B-08 H
  368. *
  369. * [ buffer, buffer_hi ] loaded with "buffer".
  370. */
  371. qtd[qtd_counter].qt_next =
  372. cpu_to_hc32(QT_NEXT_TERMINATE);
  373. qtd[qtd_counter].qt_altnext =
  374. cpu_to_hc32(QT_NEXT_TERMINATE);
  375. token = QT_TOKEN_DT(toggle) |
  376. QT_TOKEN_TOTALBYTES(xfr_bytes) |
  377. QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
  378. QT_TOKEN_CERR(3) |
  379. QT_TOKEN_PID(usb_pipein(pipe) ?
  380. QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
  381. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  382. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  383. if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
  384. xfr_bytes)) {
  385. printf("unable to construct DATA TD\n");
  386. goto fail;
  387. }
  388. /* Update previous qTD! */
  389. *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
  390. tdp = &qtd[qtd_counter++].qt_next;
  391. /*
  392. * Data toggle has to be adjusted since the qTD transfer
  393. * size is not always an even multiple of
  394. * wMaxPacketSize.
  395. */
  396. if ((xfr_bytes / maxpacket) & 1)
  397. toggle ^= 1;
  398. buf_ptr += xfr_bytes;
  399. left_length -= xfr_bytes;
  400. } while (left_length > 0);
  401. }
  402. if (req != NULL) {
  403. /*
  404. * Setup request qTD (3.5 in ehci-r10.pdf)
  405. *
  406. * qt_next ................ 03-00 H
  407. * qt_altnext ............. 07-04 H
  408. * qt_token ............... 0B-08 H
  409. */
  410. qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  411. qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  412. token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
  413. QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
  414. QT_TOKEN_PID(usb_pipein(pipe) ?
  415. QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
  416. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  417. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  418. /* Update previous qTD! */
  419. *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
  420. tdp = &qtd[qtd_counter++].qt_next;
  421. }
  422. ctrl->qh_list.qh_link = cpu_to_hc32((uint32_t)qh | QH_LINK_TYPE_QH);
  423. /* Flush dcache */
  424. flush_dcache_range((uint32_t)&ctrl->qh_list,
  425. ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
  426. flush_dcache_range((uint32_t)qh, ALIGN_END_ADDR(struct QH, qh, 1));
  427. flush_dcache_range((uint32_t)qtd,
  428. ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
  429. /* Set async. queue head pointer. */
  430. ehci_writel(&ctrl->hcor->or_asynclistaddr, (uint32_t)&ctrl->qh_list);
  431. usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
  432. ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
  433. /* Enable async. schedule. */
  434. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  435. cmd |= CMD_ASE;
  436. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  437. ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
  438. 100 * 1000);
  439. if (ret < 0) {
  440. printf("EHCI fail timeout STS_ASS set\n");
  441. goto fail;
  442. }
  443. /* Wait for TDs to be processed. */
  444. ts = get_timer(0);
  445. vtd = &qtd[qtd_counter - 1];
  446. timeout = USB_TIMEOUT_MS(pipe);
  447. do {
  448. /* Invalidate dcache */
  449. invalidate_dcache_range((uint32_t)&ctrl->qh_list,
  450. ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
  451. invalidate_dcache_range((uint32_t)qh,
  452. ALIGN_END_ADDR(struct QH, qh, 1));
  453. invalidate_dcache_range((uint32_t)qtd,
  454. ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
  455. token = hc32_to_cpu(vtd->qt_token);
  456. if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
  457. break;
  458. WATCHDOG_RESET();
  459. } while (get_timer(ts) < timeout);
  460. /*
  461. * Invalidate the memory area occupied by buffer
  462. * Don't try to fix the buffer alignment, if it isn't properly
  463. * aligned it's upper layer's fault so let invalidate_dcache_range()
  464. * vow about it. But we have to fix the length as it's actual
  465. * transfer length and can be unaligned. This is potentially
  466. * dangerous operation, it's responsibility of the calling
  467. * code to make sure enough space is reserved.
  468. */
  469. invalidate_dcache_range((uint32_t)buffer,
  470. ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN));
  471. /* Check that the TD processing happened */
  472. if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
  473. printf("EHCI timed out on TD - token=%#x\n", token);
  474. /* Disable async schedule. */
  475. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  476. cmd &= ~CMD_ASE;
  477. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  478. ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
  479. 100 * 1000);
  480. if (ret < 0) {
  481. printf("EHCI fail timeout STS_ASS reset\n");
  482. goto fail;
  483. }
  484. token = hc32_to_cpu(qh->qh_overlay.qt_token);
  485. if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
  486. debug("TOKEN=%#x\n", token);
  487. switch (QT_TOKEN_GET_STATUS(token) &
  488. ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
  489. case 0:
  490. toggle = QT_TOKEN_GET_DT(token);
  491. usb_settoggle(dev, usb_pipeendpoint(pipe),
  492. usb_pipeout(pipe), toggle);
  493. dev->status = 0;
  494. break;
  495. case QT_TOKEN_STATUS_HALTED:
  496. dev->status = USB_ST_STALLED;
  497. break;
  498. case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
  499. case QT_TOKEN_STATUS_DATBUFERR:
  500. dev->status = USB_ST_BUF_ERR;
  501. break;
  502. case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
  503. case QT_TOKEN_STATUS_BABBLEDET:
  504. dev->status = USB_ST_BABBLE_DET;
  505. break;
  506. default:
  507. dev->status = USB_ST_CRC_ERR;
  508. if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
  509. dev->status |= USB_ST_STALLED;
  510. break;
  511. }
  512. dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
  513. } else {
  514. dev->act_len = 0;
  515. debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
  516. dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
  517. ehci_readl(&ctrl->hcor->or_portsc[0]),
  518. ehci_readl(&ctrl->hcor->or_portsc[1]));
  519. }
  520. free(qtd);
  521. return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
  522. fail:
  523. free(qtd);
  524. return -1;
  525. }
  526. static inline int min3(int a, int b, int c)
  527. {
  528. if (b < a)
  529. a = b;
  530. if (c < a)
  531. a = c;
  532. return a;
  533. }
  534. int
  535. ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
  536. int length, struct devrequest *req)
  537. {
  538. uint8_t tmpbuf[4];
  539. u16 typeReq;
  540. void *srcptr = NULL;
  541. int len, srclen;
  542. uint32_t reg;
  543. uint32_t *status_reg;
  544. struct ehci_ctrl *ctrl = dev->controller;
  545. if (le16_to_cpu(req->index) > CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
  546. printf("The request port(%d) is not configured\n",
  547. le16_to_cpu(req->index) - 1);
  548. return -1;
  549. }
  550. status_reg = (uint32_t *)&ctrl->hcor->or_portsc[
  551. le16_to_cpu(req->index) - 1];
  552. srclen = 0;
  553. debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
  554. req->request, req->request,
  555. req->requesttype, req->requesttype,
  556. le16_to_cpu(req->value), le16_to_cpu(req->index));
  557. typeReq = req->request | req->requesttype << 8;
  558. switch (typeReq) {
  559. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  560. switch (le16_to_cpu(req->value) >> 8) {
  561. case USB_DT_DEVICE:
  562. debug("USB_DT_DEVICE request\n");
  563. srcptr = &descriptor.device;
  564. srclen = descriptor.device.bLength;
  565. break;
  566. case USB_DT_CONFIG:
  567. debug("USB_DT_CONFIG config\n");
  568. srcptr = &descriptor.config;
  569. srclen = descriptor.config.bLength +
  570. descriptor.interface.bLength +
  571. descriptor.endpoint.bLength;
  572. break;
  573. case USB_DT_STRING:
  574. debug("USB_DT_STRING config\n");
  575. switch (le16_to_cpu(req->value) & 0xff) {
  576. case 0: /* Language */
  577. srcptr = "\4\3\1\0";
  578. srclen = 4;
  579. break;
  580. case 1: /* Vendor */
  581. srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
  582. srclen = 14;
  583. break;
  584. case 2: /* Product */
  585. srcptr = "\52\3E\0H\0C\0I\0 "
  586. "\0H\0o\0s\0t\0 "
  587. "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
  588. srclen = 42;
  589. break;
  590. default:
  591. debug("unknown value DT_STRING %x\n",
  592. le16_to_cpu(req->value));
  593. goto unknown;
  594. }
  595. break;
  596. default:
  597. debug("unknown value %x\n", le16_to_cpu(req->value));
  598. goto unknown;
  599. }
  600. break;
  601. case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
  602. switch (le16_to_cpu(req->value) >> 8) {
  603. case USB_DT_HUB:
  604. debug("USB_DT_HUB config\n");
  605. srcptr = &descriptor.hub;
  606. srclen = descriptor.hub.bLength;
  607. break;
  608. default:
  609. debug("unknown value %x\n", le16_to_cpu(req->value));
  610. goto unknown;
  611. }
  612. break;
  613. case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
  614. debug("USB_REQ_SET_ADDRESS\n");
  615. ctrl->rootdev = le16_to_cpu(req->value);
  616. break;
  617. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  618. debug("USB_REQ_SET_CONFIGURATION\n");
  619. /* Nothing to do */
  620. break;
  621. case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
  622. tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
  623. tmpbuf[1] = 0;
  624. srcptr = tmpbuf;
  625. srclen = 2;
  626. break;
  627. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  628. memset(tmpbuf, 0, 4);
  629. reg = ehci_readl(status_reg);
  630. if (reg & EHCI_PS_CS)
  631. tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
  632. if (reg & EHCI_PS_PE)
  633. tmpbuf[0] |= USB_PORT_STAT_ENABLE;
  634. if (reg & EHCI_PS_SUSP)
  635. tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
  636. if (reg & EHCI_PS_OCA)
  637. tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
  638. if (reg & EHCI_PS_PR)
  639. tmpbuf[0] |= USB_PORT_STAT_RESET;
  640. if (reg & EHCI_PS_PP)
  641. tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
  642. if (ehci_is_TDI()) {
  643. switch (PORTSC_PSPD(reg)) {
  644. case PORTSC_PSPD_FS:
  645. break;
  646. case PORTSC_PSPD_LS:
  647. tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
  648. break;
  649. case PORTSC_PSPD_HS:
  650. default:
  651. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  652. break;
  653. }
  654. } else {
  655. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  656. }
  657. if (reg & EHCI_PS_CSC)
  658. tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
  659. if (reg & EHCI_PS_PEC)
  660. tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
  661. if (reg & EHCI_PS_OCC)
  662. tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
  663. if (ctrl->portreset & (1 << le16_to_cpu(req->index)))
  664. tmpbuf[2] |= USB_PORT_STAT_C_RESET;
  665. srcptr = tmpbuf;
  666. srclen = 4;
  667. break;
  668. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  669. reg = ehci_readl(status_reg);
  670. reg &= ~EHCI_PS_CLEAR;
  671. switch (le16_to_cpu(req->value)) {
  672. case USB_PORT_FEAT_ENABLE:
  673. reg |= EHCI_PS_PE;
  674. ehci_writel(status_reg, reg);
  675. break;
  676. case USB_PORT_FEAT_POWER:
  677. if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
  678. reg |= EHCI_PS_PP;
  679. ehci_writel(status_reg, reg);
  680. }
  681. break;
  682. case USB_PORT_FEAT_RESET:
  683. if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
  684. !ehci_is_TDI() &&
  685. EHCI_PS_IS_LOWSPEED(reg)) {
  686. /* Low speed device, give up ownership. */
  687. debug("port %d low speed --> companion\n",
  688. req->index - 1);
  689. reg |= EHCI_PS_PO;
  690. ehci_writel(status_reg, reg);
  691. break;
  692. } else {
  693. int ret;
  694. reg |= EHCI_PS_PR;
  695. reg &= ~EHCI_PS_PE;
  696. ehci_writel(status_reg, reg);
  697. /*
  698. * caller must wait, then call GetPortStatus
  699. * usb 2.0 specification say 50 ms resets on
  700. * root
  701. */
  702. ehci_powerup_fixup(status_reg, &reg);
  703. ehci_writel(status_reg, reg & ~EHCI_PS_PR);
  704. /*
  705. * A host controller must terminate the reset
  706. * and stabilize the state of the port within
  707. * 2 milliseconds
  708. */
  709. ret = handshake(status_reg, EHCI_PS_PR, 0,
  710. 2 * 1000);
  711. if (!ret)
  712. ctrl->portreset |=
  713. 1 << le16_to_cpu(req->index);
  714. else
  715. printf("port(%d) reset error\n",
  716. le16_to_cpu(req->index) - 1);
  717. }
  718. break;
  719. default:
  720. debug("unknown feature %x\n", le16_to_cpu(req->value));
  721. goto unknown;
  722. }
  723. /* unblock posted writes */
  724. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  725. break;
  726. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  727. reg = ehci_readl(status_reg);
  728. switch (le16_to_cpu(req->value)) {
  729. case USB_PORT_FEAT_ENABLE:
  730. reg &= ~EHCI_PS_PE;
  731. break;
  732. case USB_PORT_FEAT_C_ENABLE:
  733. reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_PE;
  734. break;
  735. case USB_PORT_FEAT_POWER:
  736. if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
  737. reg = reg & ~(EHCI_PS_CLEAR | EHCI_PS_PP);
  738. case USB_PORT_FEAT_C_CONNECTION:
  739. reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_CSC;
  740. break;
  741. case USB_PORT_FEAT_OVER_CURRENT:
  742. reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_OCC;
  743. break;
  744. case USB_PORT_FEAT_C_RESET:
  745. ctrl->portreset &= ~(1 << le16_to_cpu(req->index));
  746. break;
  747. default:
  748. debug("unknown feature %x\n", le16_to_cpu(req->value));
  749. goto unknown;
  750. }
  751. ehci_writel(status_reg, reg);
  752. /* unblock posted write */
  753. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  754. break;
  755. default:
  756. debug("Unknown request\n");
  757. goto unknown;
  758. }
  759. mdelay(1);
  760. len = min3(srclen, le16_to_cpu(req->length), length);
  761. if (srcptr != NULL && len > 0)
  762. memcpy(buffer, srcptr, len);
  763. else
  764. debug("Len is 0\n");
  765. dev->act_len = len;
  766. dev->status = 0;
  767. return 0;
  768. unknown:
  769. debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
  770. req->requesttype, req->request, le16_to_cpu(req->value),
  771. le16_to_cpu(req->index), le16_to_cpu(req->length));
  772. dev->act_len = 0;
  773. dev->status = USB_ST_STALLED;
  774. return -1;
  775. }
  776. int usb_lowlevel_stop(int index)
  777. {
  778. return ehci_hcd_stop(index);
  779. }
  780. int usb_lowlevel_init(int index, void **controller)
  781. {
  782. uint32_t reg;
  783. uint32_t cmd;
  784. struct QH *qh_list;
  785. if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
  786. return -1;
  787. /* EHCI spec section 4.1 */
  788. if (ehci_reset(index))
  789. return -1;
  790. #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
  791. if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
  792. return -1;
  793. #endif
  794. qh_list = &ehcic[index].qh_list;
  795. /* Set head of reclaim list */
  796. memset(qh_list, 0, sizeof(*qh_list));
  797. qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
  798. qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
  799. QH_ENDPT1_EPS(USB_SPEED_HIGH));
  800. qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
  801. qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  802. qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  803. qh_list->qh_overlay.qt_token =
  804. cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
  805. reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
  806. descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
  807. printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  808. /* Port Indicators */
  809. if (HCS_INDICATOR(reg))
  810. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  811. | 0x80, &descriptor.hub.wHubCharacteristics);
  812. /* Port Power Control */
  813. if (HCS_PPC(reg))
  814. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  815. | 0x01, &descriptor.hub.wHubCharacteristics);
  816. /* Start the host controller. */
  817. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  818. /*
  819. * Philips, Intel, and maybe others need CMD_RUN before the
  820. * root hub will detect new devices (why?); NEC doesn't
  821. */
  822. cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  823. cmd |= CMD_RUN;
  824. ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
  825. /* take control over the ports */
  826. cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
  827. cmd |= FLAG_CF;
  828. ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
  829. /* unblock posted write */
  830. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  831. mdelay(5);
  832. reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
  833. printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
  834. ehcic[index].rootdev = 0;
  835. *controller = &ehcic[index];
  836. return 0;
  837. }
  838. int
  839. submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  840. int length)
  841. {
  842. if (usb_pipetype(pipe) != PIPE_BULK) {
  843. debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
  844. return -1;
  845. }
  846. return ehci_submit_async(dev, pipe, buffer, length, NULL);
  847. }
  848. int
  849. submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  850. int length, struct devrequest *setup)
  851. {
  852. struct ehci_ctrl *ctrl = dev->controller;
  853. if (usb_pipetype(pipe) != PIPE_CONTROL) {
  854. debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
  855. return -1;
  856. }
  857. if (usb_pipedevice(pipe) == ctrl->rootdev) {
  858. if (!ctrl->rootdev)
  859. dev->speed = USB_SPEED_HIGH;
  860. return ehci_submit_root(dev, pipe, buffer, length, setup);
  861. }
  862. return ehci_submit_async(dev, pipe, buffer, length, setup);
  863. }
  864. int
  865. submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  866. int length, int interval)
  867. {
  868. debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
  869. dev, pipe, buffer, length, interval);
  870. /*
  871. * Interrupt transfers requiring several transactions are not supported
  872. * because bInterval is ignored.
  873. *
  874. * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
  875. * <= PKT_ALIGN if several qTDs are required, while the USB
  876. * specification does not constrain this for interrupt transfers. That
  877. * means that ehci_submit_async() would support interrupt transfers
  878. * requiring several transactions only as long as the transfer size does
  879. * not require more than a single qTD.
  880. */
  881. if (length > usb_maxpacket(dev, pipe)) {
  882. printf("%s: Interrupt transfers requiring several transactions "
  883. "are not supported.\n", __func__);
  884. return -1;
  885. }
  886. return ehci_submit_async(dev, pipe, buffer, length, NULL);
  887. }