ehci-hcd.c 28 KB

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