ehci-hcd.c 26 KB

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