ehci-hcd.c 46 KB

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