ehci-hcd.c 27 KB

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