ehci-hcd.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. /*-
  2. * Copyright (c) 2007-2008, Juniper Networks, Inc.
  3. * Copyright (c) 2008, Excito Elektronik i Skåne AB
  4. * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2 of
  11. * the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <errno.h>
  25. #include <asm/byteorder.h>
  26. #include <asm/unaligned.h>
  27. #include <usb.h>
  28. #include <asm/io.h>
  29. #include <malloc.h>
  30. #include <watchdog.h>
  31. #include <linux/compiler.h>
  32. #include "ehci.h"
  33. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  34. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  35. #endif
  36. /*
  37. * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
  38. * Let's time out after 8 to have a little safety margin on top of that.
  39. */
  40. #define HCHALT_TIMEOUT (8 * 1000)
  41. static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
  42. #define ALIGN_END_ADDR(type, ptr, size) \
  43. ((uint32_t)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
  44. static struct descriptor {
  45. struct usb_hub_descriptor hub;
  46. struct usb_device_descriptor device;
  47. struct usb_linux_config_descriptor config;
  48. struct usb_linux_interface_descriptor interface;
  49. struct usb_endpoint_descriptor endpoint;
  50. } __attribute__ ((packed)) descriptor = {
  51. {
  52. 0x8, /* bDescLength */
  53. 0x29, /* bDescriptorType: hub descriptor */
  54. 2, /* bNrPorts -- runtime modified */
  55. 0, /* wHubCharacteristics */
  56. 10, /* bPwrOn2PwrGood */
  57. 0, /* bHubCntrCurrent */
  58. {}, /* Device removable */
  59. {} /* at most 7 ports! XXX */
  60. },
  61. {
  62. 0x12, /* bLength */
  63. 1, /* bDescriptorType: UDESC_DEVICE */
  64. cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
  65. 9, /* bDeviceClass: UDCLASS_HUB */
  66. 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
  67. 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
  68. 64, /* bMaxPacketSize: 64 bytes */
  69. 0x0000, /* idVendor */
  70. 0x0000, /* idProduct */
  71. cpu_to_le16(0x0100), /* bcdDevice */
  72. 1, /* iManufacturer */
  73. 2, /* iProduct */
  74. 0, /* iSerialNumber */
  75. 1 /* bNumConfigurations: 1 */
  76. },
  77. {
  78. 0x9,
  79. 2, /* bDescriptorType: UDESC_CONFIG */
  80. cpu_to_le16(0x19),
  81. 1, /* bNumInterface */
  82. 1, /* bConfigurationValue */
  83. 0, /* iConfiguration */
  84. 0x40, /* bmAttributes: UC_SELF_POWER */
  85. 0 /* bMaxPower */
  86. },
  87. {
  88. 0x9, /* bLength */
  89. 4, /* bDescriptorType: UDESC_INTERFACE */
  90. 0, /* bInterfaceNumber */
  91. 0, /* bAlternateSetting */
  92. 1, /* bNumEndpoints */
  93. 9, /* bInterfaceClass: UICLASS_HUB */
  94. 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
  95. 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
  96. 0 /* iInterface */
  97. },
  98. {
  99. 0x7, /* bLength */
  100. 5, /* bDescriptorType: UDESC_ENDPOINT */
  101. 0x81, /* bEndpointAddress:
  102. * UE_DIR_IN | EHCI_INTR_ENDPT
  103. */
  104. 3, /* bmAttributes: UE_INTERRUPT */
  105. 8, /* wMaxPacketSize */
  106. 255 /* bInterval */
  107. },
  108. };
  109. #if defined(CONFIG_EHCI_IS_TDI)
  110. #define ehci_is_TDI() (1)
  111. #else
  112. #define ehci_is_TDI() (0)
  113. #endif
  114. int __ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
  115. {
  116. return PORTSC_PSPD(reg);
  117. }
  118. int ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
  119. __attribute__((weak, alias("__ehci_get_port_speed")));
  120. void __ehci_set_usbmode(int index)
  121. {
  122. uint32_t tmp;
  123. uint32_t *reg_ptr;
  124. reg_ptr = (uint32_t *)((u8 *)&ehcic[index].hcor->or_usbcmd + USBMODE);
  125. tmp = ehci_readl(reg_ptr);
  126. tmp |= USBMODE_CM_HC;
  127. #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
  128. tmp |= USBMODE_BE;
  129. #endif
  130. ehci_writel(reg_ptr, tmp);
  131. }
  132. void ehci_set_usbmode(int index)
  133. __attribute__((weak, alias("__ehci_set_usbmode")));
  134. void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
  135. {
  136. mdelay(50);
  137. }
  138. void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
  139. __attribute__((weak, alias("__ehci_powerup_fixup")));
  140. static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
  141. {
  142. uint32_t result;
  143. do {
  144. result = ehci_readl(ptr);
  145. udelay(5);
  146. if (result == ~(uint32_t)0)
  147. return -1;
  148. result &= mask;
  149. if (result == done)
  150. return 0;
  151. usec--;
  152. } while (usec > 0);
  153. return -1;
  154. }
  155. static int ehci_reset(int index)
  156. {
  157. uint32_t cmd;
  158. int ret = 0;
  159. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  160. cmd = (cmd & ~CMD_RUN) | CMD_RESET;
  161. ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
  162. ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
  163. CMD_RESET, 0, 250 * 1000);
  164. if (ret < 0) {
  165. printf("EHCI fail to reset\n");
  166. goto out;
  167. }
  168. if (ehci_is_TDI())
  169. ehci_set_usbmode(index);
  170. #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
  171. cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
  172. cmd &= ~TXFIFO_THRESH_MASK;
  173. cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
  174. ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
  175. #endif
  176. out:
  177. return ret;
  178. }
  179. static int ehci_shutdown(struct ehci_ctrl *ctrl)
  180. {
  181. int i, ret = 0;
  182. uint32_t cmd, reg;
  183. if (!ctrl || !ctrl->hcor)
  184. return -EINVAL;
  185. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  186. cmd &= ~(CMD_PSE | CMD_ASE);
  187. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  188. ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
  189. 100 * 1000);
  190. if (!ret) {
  191. for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
  192. reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
  193. reg |= EHCI_PS_SUSP;
  194. ehci_writel(&ctrl->hcor->or_portsc[i], reg);
  195. }
  196. cmd &= ~CMD_RUN;
  197. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  198. ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
  199. HCHALT_TIMEOUT);
  200. }
  201. if (ret)
  202. puts("EHCI failed to shut down host controller.\n");
  203. return ret;
  204. }
  205. static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
  206. {
  207. uint32_t delta, next;
  208. uint32_t addr = (uint32_t)buf;
  209. int idx;
  210. if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
  211. debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
  212. flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
  213. idx = 0;
  214. while (idx < QT_BUFFER_CNT) {
  215. td->qt_buffer[idx] = cpu_to_hc32(addr);
  216. td->qt_buffer_hi[idx] = 0;
  217. next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
  218. delta = next - addr;
  219. if (delta >= sz)
  220. break;
  221. sz -= delta;
  222. addr = next;
  223. idx++;
  224. }
  225. if (idx == QT_BUFFER_CNT) {
  226. printf("out of buffer pointers (%u bytes left)\n", sz);
  227. return -1;
  228. }
  229. return 0;
  230. }
  231. static inline u8 ehci_encode_speed(enum usb_device_speed speed)
  232. {
  233. #define QH_HIGH_SPEED 2
  234. #define QH_FULL_SPEED 0
  235. #define QH_LOW_SPEED 1
  236. if (speed == USB_SPEED_HIGH)
  237. return QH_HIGH_SPEED;
  238. if (speed == USB_SPEED_LOW)
  239. return QH_LOW_SPEED;
  240. return QH_FULL_SPEED;
  241. }
  242. static int
  243. ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
  244. int length, struct devrequest *req)
  245. {
  246. ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
  247. struct qTD *qtd;
  248. int qtd_count = 0;
  249. int qtd_counter = 0;
  250. volatile struct qTD *vtd;
  251. unsigned long ts;
  252. uint32_t *tdp;
  253. uint32_t endpt, maxpacket, token, usbsts;
  254. uint32_t c, toggle;
  255. uint32_t cmd;
  256. int timeout;
  257. int ret = 0;
  258. struct ehci_ctrl *ctrl = dev->controller;
  259. debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
  260. buffer, length, req);
  261. if (req != NULL)
  262. debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
  263. req->request, req->request,
  264. req->requesttype, req->requesttype,
  265. le16_to_cpu(req->value), le16_to_cpu(req->value),
  266. le16_to_cpu(req->index));
  267. #define PKT_ALIGN 512
  268. /*
  269. * The USB transfer is split into qTD transfers. Eeach qTD transfer is
  270. * described by a transfer descriptor (the qTD). The qTDs form a linked
  271. * list with a queue head (QH).
  272. *
  273. * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
  274. * have its beginning in a qTD transfer and its end in the following
  275. * one, so the qTD transfer lengths have to be chosen accordingly.
  276. *
  277. * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
  278. * single pages. The first data buffer can start at any offset within a
  279. * page (not considering the cache-line alignment issues), while the
  280. * following buffers must be page-aligned. There is no alignment
  281. * constraint on the size of a qTD transfer.
  282. */
  283. if (req != NULL)
  284. /* 1 qTD will be needed for SETUP, and 1 for ACK. */
  285. qtd_count += 1 + 1;
  286. if (length > 0 || req == NULL) {
  287. /*
  288. * Determine the qTD transfer size that will be used for the
  289. * data payload (not considering the first qTD transfer, which
  290. * may be longer or shorter, and the final one, which may be
  291. * shorter).
  292. *
  293. * In order to keep each packet within a qTD transfer, the qTD
  294. * transfer size is aligned to PKT_ALIGN, which is a multiple of
  295. * wMaxPacketSize (except in some cases for interrupt transfers,
  296. * see comment in submit_int_msg()).
  297. *
  298. * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
  299. * QT_BUFFER_CNT full pages will be used.
  300. */
  301. int xfr_sz = QT_BUFFER_CNT;
  302. /*
  303. * However, if the input buffer is not aligned to PKT_ALIGN, the
  304. * qTD transfer size will be one page shorter, and the first qTD
  305. * data buffer of each transfer will be page-unaligned.
  306. */
  307. if ((uint32_t)buffer & (PKT_ALIGN - 1))
  308. xfr_sz--;
  309. /* Convert the qTD transfer size to bytes. */
  310. xfr_sz *= EHCI_PAGE_SIZE;
  311. /*
  312. * Approximate by excess the number of qTDs that will be
  313. * required for the data payload. The exact formula is way more
  314. * complicated and saves at most 2 qTDs, i.e. a total of 128
  315. * bytes.
  316. */
  317. qtd_count += 2 + length / xfr_sz;
  318. }
  319. /*
  320. * Threshold value based on the worst-case total size of the allocated qTDs for
  321. * a mass-storage transfer of 65535 blocks of 512 bytes.
  322. */
  323. #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
  324. #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
  325. #endif
  326. qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
  327. if (qtd == NULL) {
  328. printf("unable to allocate TDs\n");
  329. return -1;
  330. }
  331. memset(qh, 0, sizeof(struct QH));
  332. memset(qtd, 0, qtd_count * sizeof(*qtd));
  333. toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  334. /*
  335. * Setup QH (3.6 in ehci-r10.pdf)
  336. *
  337. * qh_link ................. 03-00 H
  338. * qh_endpt1 ............... 07-04 H
  339. * qh_endpt2 ............... 0B-08 H
  340. * - qh_curtd
  341. * qh_overlay.qt_next ...... 13-10 H
  342. * - qh_overlay.qt_altnext
  343. */
  344. qh->qh_link = cpu_to_hc32((uint32_t)&ctrl->qh_list | QH_LINK_TYPE_QH);
  345. c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
  346. maxpacket = usb_maxpacket(dev, pipe);
  347. endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
  348. QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
  349. QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
  350. QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
  351. QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
  352. QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
  353. qh->qh_endpt1 = cpu_to_hc32(endpt);
  354. endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_PORTNUM(dev->portnr) |
  355. QH_ENDPT2_HUBADDR(dev->parent->devnum) |
  356. QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
  357. qh->qh_endpt2 = cpu_to_hc32(endpt);
  358. qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  359. qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  360. tdp = &qh->qh_overlay.qt_next;
  361. if (req != NULL) {
  362. /*
  363. * Setup request qTD (3.5 in ehci-r10.pdf)
  364. *
  365. * qt_next ................ 03-00 H
  366. * qt_altnext ............. 07-04 H
  367. * qt_token ............... 0B-08 H
  368. *
  369. * [ buffer, buffer_hi ] loaded with "req".
  370. */
  371. qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  372. qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  373. token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
  374. QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
  375. QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
  376. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  377. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  378. if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
  379. printf("unable to construct SETUP TD\n");
  380. goto fail;
  381. }
  382. /* Update previous qTD! */
  383. *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
  384. tdp = &qtd[qtd_counter++].qt_next;
  385. toggle = 1;
  386. }
  387. if (length > 0 || req == NULL) {
  388. uint8_t *buf_ptr = buffer;
  389. int left_length = length;
  390. do {
  391. /*
  392. * Determine the size of this qTD transfer. By default,
  393. * QT_BUFFER_CNT full pages can be used.
  394. */
  395. int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
  396. /*
  397. * However, if the input buffer is not page-aligned, the
  398. * portion of the first page before the buffer start
  399. * offset within that page is unusable.
  400. */
  401. xfr_bytes -= (uint32_t)buf_ptr & (EHCI_PAGE_SIZE - 1);
  402. /*
  403. * In order to keep each packet within a qTD transfer,
  404. * align the qTD transfer size to PKT_ALIGN.
  405. */
  406. xfr_bytes &= ~(PKT_ALIGN - 1);
  407. /*
  408. * This transfer may be shorter than the available qTD
  409. * transfer size that has just been computed.
  410. */
  411. xfr_bytes = min(xfr_bytes, left_length);
  412. /*
  413. * Setup request qTD (3.5 in ehci-r10.pdf)
  414. *
  415. * qt_next ................ 03-00 H
  416. * qt_altnext ............. 07-04 H
  417. * qt_token ............... 0B-08 H
  418. *
  419. * [ buffer, buffer_hi ] loaded with "buffer".
  420. */
  421. qtd[qtd_counter].qt_next =
  422. cpu_to_hc32(QT_NEXT_TERMINATE);
  423. qtd[qtd_counter].qt_altnext =
  424. cpu_to_hc32(QT_NEXT_TERMINATE);
  425. token = QT_TOKEN_DT(toggle) |
  426. QT_TOKEN_TOTALBYTES(xfr_bytes) |
  427. QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
  428. QT_TOKEN_CERR(3) |
  429. QT_TOKEN_PID(usb_pipein(pipe) ?
  430. QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
  431. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  432. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  433. if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
  434. xfr_bytes)) {
  435. printf("unable to construct DATA TD\n");
  436. goto fail;
  437. }
  438. /* Update previous qTD! */
  439. *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
  440. tdp = &qtd[qtd_counter++].qt_next;
  441. /*
  442. * Data toggle has to be adjusted since the qTD transfer
  443. * size is not always an even multiple of
  444. * wMaxPacketSize.
  445. */
  446. if ((xfr_bytes / maxpacket) & 1)
  447. toggle ^= 1;
  448. buf_ptr += xfr_bytes;
  449. left_length -= xfr_bytes;
  450. } while (left_length > 0);
  451. }
  452. if (req != NULL) {
  453. /*
  454. * Setup request qTD (3.5 in ehci-r10.pdf)
  455. *
  456. * qt_next ................ 03-00 H
  457. * qt_altnext ............. 07-04 H
  458. * qt_token ............... 0B-08 H
  459. */
  460. qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  461. qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  462. token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
  463. QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
  464. QT_TOKEN_PID(usb_pipein(pipe) ?
  465. QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
  466. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  467. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  468. /* Update previous qTD! */
  469. *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
  470. tdp = &qtd[qtd_counter++].qt_next;
  471. }
  472. ctrl->qh_list.qh_link = cpu_to_hc32((uint32_t)qh | QH_LINK_TYPE_QH);
  473. /* Flush dcache */
  474. flush_dcache_range((uint32_t)&ctrl->qh_list,
  475. ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
  476. flush_dcache_range((uint32_t)qh, ALIGN_END_ADDR(struct QH, qh, 1));
  477. flush_dcache_range((uint32_t)qtd,
  478. ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
  479. /* Set async. queue head pointer. */
  480. ehci_writel(&ctrl->hcor->or_asynclistaddr, (uint32_t)&ctrl->qh_list);
  481. usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
  482. ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
  483. /* Enable async. schedule. */
  484. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  485. cmd |= CMD_ASE;
  486. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  487. ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
  488. 100 * 1000);
  489. if (ret < 0) {
  490. printf("EHCI fail timeout STS_ASS set\n");
  491. goto fail;
  492. }
  493. /* Wait for TDs to be processed. */
  494. ts = get_timer(0);
  495. vtd = &qtd[qtd_counter - 1];
  496. timeout = USB_TIMEOUT_MS(pipe);
  497. do {
  498. /* Invalidate dcache */
  499. invalidate_dcache_range((uint32_t)&ctrl->qh_list,
  500. ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
  501. invalidate_dcache_range((uint32_t)qh,
  502. ALIGN_END_ADDR(struct QH, qh, 1));
  503. invalidate_dcache_range((uint32_t)qtd,
  504. ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
  505. token = hc32_to_cpu(vtd->qt_token);
  506. if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
  507. break;
  508. WATCHDOG_RESET();
  509. } while (get_timer(ts) < timeout);
  510. /*
  511. * Invalidate the memory area occupied by buffer
  512. * Don't try to fix the buffer alignment, if it isn't properly
  513. * aligned it's upper layer's fault so let invalidate_dcache_range()
  514. * vow about it. But we have to fix the length as it's actual
  515. * transfer length and can be unaligned. This is potentially
  516. * dangerous operation, it's responsibility of the calling
  517. * code to make sure enough space is reserved.
  518. */
  519. invalidate_dcache_range((uint32_t)buffer,
  520. ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN));
  521. /* Check that the TD processing happened */
  522. if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
  523. printf("EHCI timed out on TD - token=%#x\n", token);
  524. /* Disable async schedule. */
  525. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  526. cmd &= ~CMD_ASE;
  527. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  528. ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
  529. 100 * 1000);
  530. if (ret < 0) {
  531. printf("EHCI fail timeout STS_ASS reset\n");
  532. goto fail;
  533. }
  534. token = hc32_to_cpu(qh->qh_overlay.qt_token);
  535. if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
  536. debug("TOKEN=%#x\n", token);
  537. switch (QT_TOKEN_GET_STATUS(token) &
  538. ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
  539. case 0:
  540. toggle = QT_TOKEN_GET_DT(token);
  541. usb_settoggle(dev, usb_pipeendpoint(pipe),
  542. usb_pipeout(pipe), toggle);
  543. dev->status = 0;
  544. break;
  545. case QT_TOKEN_STATUS_HALTED:
  546. dev->status = USB_ST_STALLED;
  547. break;
  548. case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
  549. case QT_TOKEN_STATUS_DATBUFERR:
  550. dev->status = USB_ST_BUF_ERR;
  551. break;
  552. case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
  553. case QT_TOKEN_STATUS_BABBLEDET:
  554. dev->status = USB_ST_BABBLE_DET;
  555. break;
  556. default:
  557. dev->status = USB_ST_CRC_ERR;
  558. if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
  559. dev->status |= USB_ST_STALLED;
  560. break;
  561. }
  562. dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
  563. } else {
  564. dev->act_len = 0;
  565. #ifndef CONFIG_USB_EHCI_FARADAY
  566. debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
  567. dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
  568. ehci_readl(&ctrl->hcor->or_portsc[0]),
  569. ehci_readl(&ctrl->hcor->or_portsc[1]));
  570. #endif
  571. }
  572. free(qtd);
  573. return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
  574. fail:
  575. free(qtd);
  576. return -1;
  577. }
  578. __weak uint32_t *ehci_get_portsc_register(struct ehci_hcor *hcor, int port)
  579. {
  580. if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
  581. /* Printing the message would cause a scan failure! */
  582. debug("The request port(%u) is not configured\n", port);
  583. return NULL;
  584. }
  585. return (uint32_t *)&hcor->or_portsc[port];
  586. }
  587. int
  588. ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
  589. int length, struct devrequest *req)
  590. {
  591. uint8_t tmpbuf[4];
  592. u16 typeReq;
  593. void *srcptr = NULL;
  594. int len, srclen;
  595. uint32_t reg;
  596. uint32_t *status_reg;
  597. int port = le16_to_cpu(req->index) & 0xff;
  598. struct ehci_ctrl *ctrl = dev->controller;
  599. srclen = 0;
  600. debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
  601. req->request, req->request,
  602. req->requesttype, req->requesttype,
  603. le16_to_cpu(req->value), le16_to_cpu(req->index));
  604. typeReq = req->request | req->requesttype << 8;
  605. switch (typeReq) {
  606. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  607. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  608. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  609. status_reg = ehci_get_portsc_register(ctrl->hcor, port - 1);
  610. if (!status_reg)
  611. return -1;
  612. break;
  613. default:
  614. status_reg = NULL;
  615. break;
  616. }
  617. switch (typeReq) {
  618. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  619. switch (le16_to_cpu(req->value) >> 8) {
  620. case USB_DT_DEVICE:
  621. debug("USB_DT_DEVICE request\n");
  622. srcptr = &descriptor.device;
  623. srclen = descriptor.device.bLength;
  624. break;
  625. case USB_DT_CONFIG:
  626. debug("USB_DT_CONFIG config\n");
  627. srcptr = &descriptor.config;
  628. srclen = descriptor.config.bLength +
  629. descriptor.interface.bLength +
  630. descriptor.endpoint.bLength;
  631. break;
  632. case USB_DT_STRING:
  633. debug("USB_DT_STRING config\n");
  634. switch (le16_to_cpu(req->value) & 0xff) {
  635. case 0: /* Language */
  636. srcptr = "\4\3\1\0";
  637. srclen = 4;
  638. break;
  639. case 1: /* Vendor */
  640. srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
  641. srclen = 14;
  642. break;
  643. case 2: /* Product */
  644. srcptr = "\52\3E\0H\0C\0I\0 "
  645. "\0H\0o\0s\0t\0 "
  646. "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
  647. srclen = 42;
  648. break;
  649. default:
  650. debug("unknown value DT_STRING %x\n",
  651. le16_to_cpu(req->value));
  652. goto unknown;
  653. }
  654. break;
  655. default:
  656. debug("unknown value %x\n", le16_to_cpu(req->value));
  657. goto unknown;
  658. }
  659. break;
  660. case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
  661. switch (le16_to_cpu(req->value) >> 8) {
  662. case USB_DT_HUB:
  663. debug("USB_DT_HUB config\n");
  664. srcptr = &descriptor.hub;
  665. srclen = descriptor.hub.bLength;
  666. break;
  667. default:
  668. debug("unknown value %x\n", le16_to_cpu(req->value));
  669. goto unknown;
  670. }
  671. break;
  672. case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
  673. debug("USB_REQ_SET_ADDRESS\n");
  674. ctrl->rootdev = le16_to_cpu(req->value);
  675. break;
  676. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  677. debug("USB_REQ_SET_CONFIGURATION\n");
  678. /* Nothing to do */
  679. break;
  680. case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
  681. tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
  682. tmpbuf[1] = 0;
  683. srcptr = tmpbuf;
  684. srclen = 2;
  685. break;
  686. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  687. memset(tmpbuf, 0, 4);
  688. reg = ehci_readl(status_reg);
  689. if (reg & EHCI_PS_CS)
  690. tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
  691. if (reg & EHCI_PS_PE)
  692. tmpbuf[0] |= USB_PORT_STAT_ENABLE;
  693. if (reg & EHCI_PS_SUSP)
  694. tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
  695. if (reg & EHCI_PS_OCA)
  696. tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
  697. if (reg & EHCI_PS_PR)
  698. tmpbuf[0] |= USB_PORT_STAT_RESET;
  699. if (reg & EHCI_PS_PP)
  700. tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
  701. if (ehci_is_TDI()) {
  702. switch (ehci_get_port_speed(ctrl->hcor, reg)) {
  703. case PORTSC_PSPD_FS:
  704. break;
  705. case PORTSC_PSPD_LS:
  706. tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
  707. break;
  708. case PORTSC_PSPD_HS:
  709. default:
  710. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  711. break;
  712. }
  713. } else {
  714. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  715. }
  716. if (reg & EHCI_PS_CSC)
  717. tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
  718. if (reg & EHCI_PS_PEC)
  719. tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
  720. if (reg & EHCI_PS_OCC)
  721. tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
  722. if (ctrl->portreset & (1 << port))
  723. tmpbuf[2] |= USB_PORT_STAT_C_RESET;
  724. srcptr = tmpbuf;
  725. srclen = 4;
  726. break;
  727. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  728. reg = ehci_readl(status_reg);
  729. reg &= ~EHCI_PS_CLEAR;
  730. switch (le16_to_cpu(req->value)) {
  731. case USB_PORT_FEAT_ENABLE:
  732. reg |= EHCI_PS_PE;
  733. ehci_writel(status_reg, reg);
  734. break;
  735. case USB_PORT_FEAT_POWER:
  736. if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
  737. reg |= EHCI_PS_PP;
  738. ehci_writel(status_reg, reg);
  739. }
  740. break;
  741. case USB_PORT_FEAT_RESET:
  742. if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
  743. !ehci_is_TDI() &&
  744. EHCI_PS_IS_LOWSPEED(reg)) {
  745. /* Low speed device, give up ownership. */
  746. debug("port %d low speed --> companion\n",
  747. port - 1);
  748. reg |= EHCI_PS_PO;
  749. ehci_writel(status_reg, reg);
  750. break;
  751. } else {
  752. int ret;
  753. reg |= EHCI_PS_PR;
  754. reg &= ~EHCI_PS_PE;
  755. ehci_writel(status_reg, reg);
  756. /*
  757. * caller must wait, then call GetPortStatus
  758. * usb 2.0 specification say 50 ms resets on
  759. * root
  760. */
  761. ehci_powerup_fixup(status_reg, &reg);
  762. ehci_writel(status_reg, reg & ~EHCI_PS_PR);
  763. /*
  764. * A host controller must terminate the reset
  765. * and stabilize the state of the port within
  766. * 2 milliseconds
  767. */
  768. ret = handshake(status_reg, EHCI_PS_PR, 0,
  769. 2 * 1000);
  770. if (!ret)
  771. ctrl->portreset |= 1 << port;
  772. else
  773. printf("port(%d) reset error\n",
  774. port - 1);
  775. }
  776. break;
  777. case USB_PORT_FEAT_TEST:
  778. ehci_shutdown(ctrl);
  779. reg &= ~(0xf << 16);
  780. reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
  781. ehci_writel(status_reg, reg);
  782. break;
  783. default:
  784. debug("unknown feature %x\n", le16_to_cpu(req->value));
  785. goto unknown;
  786. }
  787. /* unblock posted writes */
  788. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  789. break;
  790. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  791. reg = ehci_readl(status_reg);
  792. reg &= ~EHCI_PS_CLEAR;
  793. switch (le16_to_cpu(req->value)) {
  794. case USB_PORT_FEAT_ENABLE:
  795. reg &= ~EHCI_PS_PE;
  796. break;
  797. case USB_PORT_FEAT_C_ENABLE:
  798. reg |= EHCI_PS_PE;
  799. break;
  800. case USB_PORT_FEAT_POWER:
  801. if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
  802. reg &= ~EHCI_PS_PP;
  803. break;
  804. case USB_PORT_FEAT_C_CONNECTION:
  805. reg |= EHCI_PS_CSC;
  806. break;
  807. case USB_PORT_FEAT_OVER_CURRENT:
  808. reg |= EHCI_PS_OCC;
  809. break;
  810. case USB_PORT_FEAT_C_RESET:
  811. ctrl->portreset &= ~(1 << port);
  812. break;
  813. default:
  814. debug("unknown feature %x\n", le16_to_cpu(req->value));
  815. goto unknown;
  816. }
  817. ehci_writel(status_reg, reg);
  818. /* unblock posted write */
  819. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  820. break;
  821. default:
  822. debug("Unknown request\n");
  823. goto unknown;
  824. }
  825. mdelay(1);
  826. len = min3(srclen, le16_to_cpu(req->length), length);
  827. if (srcptr != NULL && len > 0)
  828. memcpy(buffer, srcptr, len);
  829. else
  830. debug("Len is 0\n");
  831. dev->act_len = len;
  832. dev->status = 0;
  833. return 0;
  834. unknown:
  835. debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
  836. req->requesttype, req->request, le16_to_cpu(req->value),
  837. le16_to_cpu(req->index), le16_to_cpu(req->length));
  838. dev->act_len = 0;
  839. dev->status = USB_ST_STALLED;
  840. return -1;
  841. }
  842. int usb_lowlevel_stop(int index)
  843. {
  844. ehci_shutdown(&ehcic[index]);
  845. return ehci_hcd_stop(index);
  846. }
  847. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  848. {
  849. uint32_t reg;
  850. uint32_t cmd;
  851. struct QH *qh_list;
  852. struct QH *periodic;
  853. int i;
  854. int rc;
  855. rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
  856. if (rc)
  857. return rc;
  858. if (init == USB_INIT_DEVICE)
  859. goto done;
  860. /* EHCI spec section 4.1 */
  861. if (ehci_reset(index))
  862. return -1;
  863. #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
  864. rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
  865. if (rc)
  866. return rc;
  867. #endif
  868. /* Set the high address word (aka segment) for 64-bit controller */
  869. if (ehci_readl(&ehcic[index].hccr->cr_hccparams) & 1)
  870. ehci_writel(&ehcic[index].hcor->or_ctrldssegment, 0);
  871. qh_list = &ehcic[index].qh_list;
  872. /* Set head of reclaim list */
  873. memset(qh_list, 0, sizeof(*qh_list));
  874. qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
  875. qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
  876. QH_ENDPT1_EPS(USB_SPEED_HIGH));
  877. qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
  878. qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  879. qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  880. qh_list->qh_overlay.qt_token =
  881. cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
  882. flush_dcache_range((uint32_t)qh_list,
  883. ALIGN_END_ADDR(struct QH, qh_list, 1));
  884. /* Set async. queue head pointer. */
  885. ehci_writel(&ehcic[index].hcor->or_asynclistaddr, (uint32_t)qh_list);
  886. /*
  887. * Set up periodic list
  888. * Step 1: Parent QH for all periodic transfers.
  889. */
  890. periodic = &ehcic[index].periodic_queue;
  891. memset(periodic, 0, sizeof(*periodic));
  892. periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
  893. periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  894. periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  895. flush_dcache_range((uint32_t)periodic,
  896. ALIGN_END_ADDR(struct QH, periodic, 1));
  897. /*
  898. * Step 2: Setup frame-list: Every microframe, USB tries the same list.
  899. * In particular, device specifications on polling frequency
  900. * are disregarded. Keyboards seem to send NAK/NYet reliably
  901. * when polled with an empty buffer.
  902. *
  903. * Split Transactions will be spread across microframes using
  904. * S-mask and C-mask.
  905. */
  906. if (ehcic[index].periodic_list == NULL)
  907. ehcic[index].periodic_list = memalign(4096, 1024 * 4);
  908. if (!ehcic[index].periodic_list)
  909. return -ENOMEM;
  910. for (i = 0; i < 1024; i++) {
  911. ehcic[index].periodic_list[i] = cpu_to_hc32((uint32_t)periodic
  912. | QH_LINK_TYPE_QH);
  913. }
  914. flush_dcache_range((uint32_t)ehcic[index].periodic_list,
  915. ALIGN_END_ADDR(uint32_t, ehcic[index].periodic_list,
  916. 1024));
  917. /* Set periodic list base address */
  918. ehci_writel(&ehcic[index].hcor->or_periodiclistbase,
  919. (uint32_t)ehcic[index].periodic_list);
  920. reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
  921. descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
  922. debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  923. /* Port Indicators */
  924. if (HCS_INDICATOR(reg))
  925. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  926. | 0x80, &descriptor.hub.wHubCharacteristics);
  927. /* Port Power Control */
  928. if (HCS_PPC(reg))
  929. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  930. | 0x01, &descriptor.hub.wHubCharacteristics);
  931. /* Start the host controller. */
  932. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  933. /*
  934. * Philips, Intel, and maybe others need CMD_RUN before the
  935. * root hub will detect new devices (why?); NEC doesn't
  936. */
  937. cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  938. cmd |= CMD_RUN;
  939. ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
  940. #ifndef CONFIG_USB_EHCI_FARADAY
  941. /* take control over the ports */
  942. cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
  943. cmd |= FLAG_CF;
  944. ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
  945. #endif
  946. /* unblock posted write */
  947. cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
  948. mdelay(5);
  949. reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
  950. printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
  951. ehcic[index].rootdev = 0;
  952. done:
  953. *controller = &ehcic[index];
  954. return 0;
  955. }
  956. int
  957. submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  958. int length)
  959. {
  960. if (usb_pipetype(pipe) != PIPE_BULK) {
  961. debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
  962. return -1;
  963. }
  964. return ehci_submit_async(dev, pipe, buffer, length, NULL);
  965. }
  966. int
  967. submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  968. int length, struct devrequest *setup)
  969. {
  970. struct ehci_ctrl *ctrl = dev->controller;
  971. if (usb_pipetype(pipe) != PIPE_CONTROL) {
  972. debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
  973. return -1;
  974. }
  975. if (usb_pipedevice(pipe) == ctrl->rootdev) {
  976. if (!ctrl->rootdev)
  977. dev->speed = USB_SPEED_HIGH;
  978. return ehci_submit_root(dev, pipe, buffer, length, setup);
  979. }
  980. return ehci_submit_async(dev, pipe, buffer, length, setup);
  981. }
  982. struct int_queue {
  983. struct QH *first;
  984. struct QH *current;
  985. struct QH *last;
  986. struct qTD *tds;
  987. };
  988. #define NEXT_QH(qh) (struct QH *)(hc32_to_cpu((qh)->qh_link) & ~0x1f)
  989. static int
  990. enable_periodic(struct ehci_ctrl *ctrl)
  991. {
  992. uint32_t cmd;
  993. struct ehci_hcor *hcor = ctrl->hcor;
  994. int ret;
  995. cmd = ehci_readl(&hcor->or_usbcmd);
  996. cmd |= CMD_PSE;
  997. ehci_writel(&hcor->or_usbcmd, cmd);
  998. ret = handshake((uint32_t *)&hcor->or_usbsts,
  999. STS_PSS, STS_PSS, 100 * 1000);
  1000. if (ret < 0) {
  1001. printf("EHCI failed: timeout when enabling periodic list\n");
  1002. return -ETIMEDOUT;
  1003. }
  1004. udelay(1000);
  1005. return 0;
  1006. }
  1007. static int
  1008. disable_periodic(struct ehci_ctrl *ctrl)
  1009. {
  1010. uint32_t cmd;
  1011. struct ehci_hcor *hcor = ctrl->hcor;
  1012. int ret;
  1013. cmd = ehci_readl(&hcor->or_usbcmd);
  1014. cmd &= ~CMD_PSE;
  1015. ehci_writel(&hcor->or_usbcmd, cmd);
  1016. ret = handshake((uint32_t *)&hcor->or_usbsts,
  1017. STS_PSS, 0, 100 * 1000);
  1018. if (ret < 0) {
  1019. printf("EHCI failed: timeout when disabling periodic list\n");
  1020. return -ETIMEDOUT;
  1021. }
  1022. return 0;
  1023. }
  1024. static int periodic_schedules;
  1025. struct int_queue *
  1026. create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
  1027. int elementsize, void *buffer)
  1028. {
  1029. struct ehci_ctrl *ctrl = dev->controller;
  1030. struct int_queue *result = NULL;
  1031. int i;
  1032. debug("Enter create_int_queue\n");
  1033. if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
  1034. debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
  1035. return NULL;
  1036. }
  1037. /* limit to 4 full pages worth of data -
  1038. * we can safely fit them in a single TD,
  1039. * no matter the alignment
  1040. */
  1041. if (elementsize >= 16384) {
  1042. debug("too large elements for interrupt transfers\n");
  1043. return NULL;
  1044. }
  1045. result = malloc(sizeof(*result));
  1046. if (!result) {
  1047. debug("ehci intr queue: out of memory\n");
  1048. goto fail1;
  1049. }
  1050. result->first = memalign(USB_DMA_MINALIGN,
  1051. sizeof(struct QH) * queuesize);
  1052. if (!result->first) {
  1053. debug("ehci intr queue: out of memory\n");
  1054. goto fail2;
  1055. }
  1056. result->current = result->first;
  1057. result->last = result->first + queuesize - 1;
  1058. result->tds = memalign(USB_DMA_MINALIGN,
  1059. sizeof(struct qTD) * queuesize);
  1060. if (!result->tds) {
  1061. debug("ehci intr queue: out of memory\n");
  1062. goto fail3;
  1063. }
  1064. memset(result->first, 0, sizeof(struct QH) * queuesize);
  1065. memset(result->tds, 0, sizeof(struct qTD) * queuesize);
  1066. for (i = 0; i < queuesize; i++) {
  1067. struct QH *qh = result->first + i;
  1068. struct qTD *td = result->tds + i;
  1069. void **buf = &qh->buffer;
  1070. qh->qh_link = cpu_to_hc32((uint32_t)(qh+1) | QH_LINK_TYPE_QH);
  1071. if (i == queuesize - 1)
  1072. qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
  1073. qh->qh_overlay.qt_next = cpu_to_hc32((uint32_t)td);
  1074. qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  1075. qh->qh_endpt1 =
  1076. cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
  1077. (usb_maxpacket(dev, pipe) << 16) | /* MPS */
  1078. (1 << 14) |
  1079. QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
  1080. (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
  1081. (usb_pipedevice(pipe) << 0));
  1082. qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
  1083. (1 << 0)); /* S-mask: microframe 0 */
  1084. if (dev->speed == USB_SPEED_LOW ||
  1085. dev->speed == USB_SPEED_FULL) {
  1086. debug("TT: port: %d, hub address: %d\n",
  1087. dev->portnr, dev->parent->devnum);
  1088. qh->qh_endpt2 |= cpu_to_hc32((dev->portnr << 23) |
  1089. (dev->parent->devnum << 16) |
  1090. (0x1c << 8)); /* C-mask: microframes 2-4 */
  1091. }
  1092. td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  1093. td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  1094. debug("communication direction is '%s'\n",
  1095. usb_pipein(pipe) ? "in" : "out");
  1096. td->qt_token = cpu_to_hc32((elementsize << 16) |
  1097. ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
  1098. 0x80); /* active */
  1099. td->qt_buffer[0] =
  1100. cpu_to_hc32((uint32_t)buffer + i * elementsize);
  1101. td->qt_buffer[1] =
  1102. cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
  1103. td->qt_buffer[2] =
  1104. cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
  1105. td->qt_buffer[3] =
  1106. cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
  1107. td->qt_buffer[4] =
  1108. cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
  1109. *buf = buffer + i * elementsize;
  1110. }
  1111. flush_dcache_range((uint32_t)buffer,
  1112. ALIGN_END_ADDR(char, buffer,
  1113. queuesize * elementsize));
  1114. flush_dcache_range((uint32_t)result->first,
  1115. ALIGN_END_ADDR(struct QH, result->first,
  1116. queuesize));
  1117. flush_dcache_range((uint32_t)result->tds,
  1118. ALIGN_END_ADDR(struct qTD, result->tds,
  1119. queuesize));
  1120. if (disable_periodic(ctrl) < 0) {
  1121. debug("FATAL: periodic should never fail, but did");
  1122. goto fail3;
  1123. }
  1124. /* hook up to periodic list */
  1125. struct QH *list = &ctrl->periodic_queue;
  1126. result->last->qh_link = list->qh_link;
  1127. list->qh_link = cpu_to_hc32((uint32_t)result->first | QH_LINK_TYPE_QH);
  1128. flush_dcache_range((uint32_t)result->last,
  1129. ALIGN_END_ADDR(struct QH, result->last, 1));
  1130. flush_dcache_range((uint32_t)list,
  1131. ALIGN_END_ADDR(struct QH, list, 1));
  1132. if (enable_periodic(ctrl) < 0) {
  1133. debug("FATAL: periodic should never fail, but did");
  1134. goto fail3;
  1135. }
  1136. periodic_schedules++;
  1137. debug("Exit create_int_queue\n");
  1138. return result;
  1139. fail3:
  1140. if (result->tds)
  1141. free(result->tds);
  1142. fail2:
  1143. if (result->first)
  1144. free(result->first);
  1145. if (result)
  1146. free(result);
  1147. fail1:
  1148. return NULL;
  1149. }
  1150. void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
  1151. {
  1152. struct QH *cur = queue->current;
  1153. /* depleted queue */
  1154. if (cur == NULL) {
  1155. debug("Exit poll_int_queue with completed queue\n");
  1156. return NULL;
  1157. }
  1158. /* still active */
  1159. invalidate_dcache_range((uint32_t)cur,
  1160. ALIGN_END_ADDR(struct QH, cur, 1));
  1161. if (cur->qh_overlay.qt_token & cpu_to_hc32(0x80)) {
  1162. debug("Exit poll_int_queue with no completed intr transfer. "
  1163. "token is %x\n", cur->qh_overlay.qt_token);
  1164. return NULL;
  1165. }
  1166. if (!(cur->qh_link & QH_LINK_TERMINATE))
  1167. queue->current++;
  1168. else
  1169. queue->current = NULL;
  1170. debug("Exit poll_int_queue with completed intr transfer. "
  1171. "token is %x at %p (first at %p)\n", cur->qh_overlay.qt_token,
  1172. &cur->qh_overlay.qt_token, queue->first);
  1173. return cur->buffer;
  1174. }
  1175. /* Do not free buffers associated with QHs, they're owned by someone else */
  1176. int
  1177. destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
  1178. {
  1179. struct ehci_ctrl *ctrl = dev->controller;
  1180. int result = -1;
  1181. unsigned long timeout;
  1182. if (disable_periodic(ctrl) < 0) {
  1183. debug("FATAL: periodic should never fail, but did");
  1184. goto out;
  1185. }
  1186. periodic_schedules--;
  1187. struct QH *cur = &ctrl->periodic_queue;
  1188. timeout = get_timer(0) + 500; /* abort after 500ms */
  1189. while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
  1190. debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
  1191. if (NEXT_QH(cur) == queue->first) {
  1192. debug("found candidate. removing from chain\n");
  1193. cur->qh_link = queue->last->qh_link;
  1194. result = 0;
  1195. break;
  1196. }
  1197. cur = NEXT_QH(cur);
  1198. if (get_timer(0) > timeout) {
  1199. printf("Timeout destroying interrupt endpoint queue\n");
  1200. result = -1;
  1201. goto out;
  1202. }
  1203. }
  1204. if (periodic_schedules > 0) {
  1205. result = enable_periodic(ctrl);
  1206. if (result < 0)
  1207. debug("FATAL: periodic should never fail, but did");
  1208. }
  1209. out:
  1210. free(queue->tds);
  1211. free(queue->first);
  1212. free(queue);
  1213. return result;
  1214. }
  1215. int
  1216. submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  1217. int length, int interval)
  1218. {
  1219. void *backbuffer;
  1220. struct int_queue *queue;
  1221. unsigned long timeout;
  1222. int result = 0, ret;
  1223. debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
  1224. dev, pipe, buffer, length, interval);
  1225. /*
  1226. * Interrupt transfers requiring several transactions are not supported
  1227. * because bInterval is ignored.
  1228. *
  1229. * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
  1230. * <= PKT_ALIGN if several qTDs are required, while the USB
  1231. * specification does not constrain this for interrupt transfers. That
  1232. * means that ehci_submit_async() would support interrupt transfers
  1233. * requiring several transactions only as long as the transfer size does
  1234. * not require more than a single qTD.
  1235. */
  1236. if (length > usb_maxpacket(dev, pipe)) {
  1237. printf("%s: Interrupt transfers requiring several "
  1238. "transactions are not supported.\n", __func__);
  1239. return -1;
  1240. }
  1241. queue = create_int_queue(dev, pipe, 1, length, buffer);
  1242. timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
  1243. while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
  1244. if (get_timer(0) > timeout) {
  1245. printf("Timeout poll on interrupt endpoint\n");
  1246. result = -ETIMEDOUT;
  1247. break;
  1248. }
  1249. if (backbuffer != buffer) {
  1250. debug("got wrong buffer back (%x instead of %x)\n",
  1251. (uint32_t)backbuffer, (uint32_t)buffer);
  1252. return -EINVAL;
  1253. }
  1254. invalidate_dcache_range((uint32_t)buffer,
  1255. ALIGN_END_ADDR(char, buffer, length));
  1256. ret = destroy_int_queue(dev, queue);
  1257. if (ret < 0)
  1258. return ret;
  1259. /* everything worked out fine */
  1260. return result;
  1261. }