ehci-hcd.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  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. * SPDX-License-Identifier: GPL-2.0
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <asm/byteorder.h>
  14. #include <asm/unaligned.h>
  15. #include <usb.h>
  16. #include <asm/io.h>
  17. #include <malloc.h>
  18. #include <memalign.h>
  19. #include <watchdog.h>
  20. #include <linux/compiler.h>
  21. #include "ehci.h"
  22. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  23. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  24. #endif
  25. /*
  26. * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
  27. * Let's time out after 8 to have a little safety margin on top of that.
  28. */
  29. #define HCHALT_TIMEOUT (8 * 1000)
  30. #ifndef CONFIG_DM_USB
  31. static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
  32. #endif
  33. #define ALIGN_END_ADDR(type, ptr, size) \
  34. ((unsigned long)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
  35. static struct descriptor {
  36. struct usb_hub_descriptor hub;
  37. struct usb_device_descriptor device;
  38. struct usb_linux_config_descriptor config;
  39. struct usb_linux_interface_descriptor interface;
  40. struct usb_endpoint_descriptor endpoint;
  41. } __attribute__ ((packed)) descriptor = {
  42. {
  43. 0x8, /* bDescLength */
  44. 0x29, /* bDescriptorType: hub descriptor */
  45. 2, /* bNrPorts -- runtime modified */
  46. 0, /* wHubCharacteristics */
  47. 10, /* bPwrOn2PwrGood */
  48. 0, /* bHubCntrCurrent */
  49. { /* Device removable */
  50. } /* at most 7 ports! XXX */
  51. },
  52. {
  53. 0x12, /* bLength */
  54. 1, /* bDescriptorType: UDESC_DEVICE */
  55. cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
  56. 9, /* bDeviceClass: UDCLASS_HUB */
  57. 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
  58. 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
  59. 64, /* bMaxPacketSize: 64 bytes */
  60. 0x0000, /* idVendor */
  61. 0x0000, /* idProduct */
  62. cpu_to_le16(0x0100), /* bcdDevice */
  63. 1, /* iManufacturer */
  64. 2, /* iProduct */
  65. 0, /* iSerialNumber */
  66. 1 /* bNumConfigurations: 1 */
  67. },
  68. {
  69. 0x9,
  70. 2, /* bDescriptorType: UDESC_CONFIG */
  71. cpu_to_le16(0x19),
  72. 1, /* bNumInterface */
  73. 1, /* bConfigurationValue */
  74. 0, /* iConfiguration */
  75. 0x40, /* bmAttributes: UC_SELF_POWER */
  76. 0 /* bMaxPower */
  77. },
  78. {
  79. 0x9, /* bLength */
  80. 4, /* bDescriptorType: UDESC_INTERFACE */
  81. 0, /* bInterfaceNumber */
  82. 0, /* bAlternateSetting */
  83. 1, /* bNumEndpoints */
  84. 9, /* bInterfaceClass: UICLASS_HUB */
  85. 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
  86. 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
  87. 0 /* iInterface */
  88. },
  89. {
  90. 0x7, /* bLength */
  91. 5, /* bDescriptorType: UDESC_ENDPOINT */
  92. 0x81, /* bEndpointAddress:
  93. * UE_DIR_IN | EHCI_INTR_ENDPT
  94. */
  95. 3, /* bmAttributes: UE_INTERRUPT */
  96. 8, /* wMaxPacketSize */
  97. 255 /* bInterval */
  98. },
  99. };
  100. #if defined(CONFIG_EHCI_IS_TDI)
  101. #define ehci_is_TDI() (1)
  102. #else
  103. #define ehci_is_TDI() (0)
  104. #endif
  105. static struct ehci_ctrl *ehci_get_ctrl(struct usb_device *udev)
  106. {
  107. #ifdef CONFIG_DM_USB
  108. return dev_get_priv(usb_get_bus(udev->dev));
  109. #else
  110. return udev->controller;
  111. #endif
  112. }
  113. static int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
  114. {
  115. return PORTSC_PSPD(reg);
  116. }
  117. static void ehci_set_usbmode(struct ehci_ctrl *ctrl)
  118. {
  119. uint32_t tmp;
  120. uint32_t *reg_ptr;
  121. reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd + USBMODE);
  122. tmp = ehci_readl(reg_ptr);
  123. tmp |= USBMODE_CM_HC;
  124. #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
  125. tmp |= USBMODE_BE;
  126. #else
  127. tmp &= ~USBMODE_BE;
  128. #endif
  129. ehci_writel(reg_ptr, tmp);
  130. }
  131. static void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
  132. uint32_t *reg)
  133. {
  134. mdelay(50);
  135. }
  136. static uint32_t *ehci_get_portsc_register(struct ehci_ctrl *ctrl, int port)
  137. {
  138. int max_ports = HCS_N_PORTS(ehci_readl(&ctrl->hccr->cr_hcsparams));
  139. if (port < 0 || port >= max_ports) {
  140. /* Printing the message would cause a scan failure! */
  141. debug("The request port(%u) exceeds maximum port number\n",
  142. port);
  143. return NULL;
  144. }
  145. return (uint32_t *)&ctrl->hcor->or_portsc[port];
  146. }
  147. static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
  148. {
  149. uint32_t result;
  150. do {
  151. result = ehci_readl(ptr);
  152. udelay(5);
  153. if (result == ~(uint32_t)0)
  154. return -1;
  155. result &= mask;
  156. if (result == done)
  157. return 0;
  158. usec--;
  159. } while (usec > 0);
  160. return -1;
  161. }
  162. static int ehci_reset(struct ehci_ctrl *ctrl)
  163. {
  164. uint32_t cmd;
  165. int ret = 0;
  166. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  167. cmd = (cmd & ~CMD_RUN) | CMD_RESET;
  168. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  169. ret = handshake((uint32_t *)&ctrl->hcor->or_usbcmd,
  170. CMD_RESET, 0, 250 * 1000);
  171. if (ret < 0) {
  172. printf("EHCI fail to reset\n");
  173. goto out;
  174. }
  175. if (ehci_is_TDI())
  176. ctrl->ops.set_usb_mode(ctrl);
  177. #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
  178. cmd = ehci_readl(&ctrl->hcor->or_txfilltuning);
  179. cmd &= ~TXFIFO_THRESH_MASK;
  180. cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
  181. ehci_writel(&ctrl->hcor->or_txfilltuning, cmd);
  182. #endif
  183. out:
  184. return ret;
  185. }
  186. static int ehci_shutdown(struct ehci_ctrl *ctrl)
  187. {
  188. int i, ret = 0;
  189. uint32_t cmd, reg;
  190. int max_ports = HCS_N_PORTS(ehci_readl(&ctrl->hccr->cr_hcsparams));
  191. if (!ctrl || !ctrl->hcor)
  192. return -EINVAL;
  193. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  194. /* If not run, directly return */
  195. if (!(cmd & CMD_RUN))
  196. return 0;
  197. cmd &= ~(CMD_PSE | CMD_ASE);
  198. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  199. ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
  200. 100 * 1000);
  201. if (!ret) {
  202. for (i = 0; i < max_ports; i++) {
  203. reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
  204. reg |= EHCI_PS_SUSP;
  205. ehci_writel(&ctrl->hcor->or_portsc[i], reg);
  206. }
  207. cmd &= ~CMD_RUN;
  208. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  209. ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
  210. HCHALT_TIMEOUT);
  211. }
  212. if (ret)
  213. puts("EHCI failed to shut down host controller.\n");
  214. return ret;
  215. }
  216. static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
  217. {
  218. uint32_t delta, next;
  219. unsigned long addr = (unsigned long)buf;
  220. int idx;
  221. if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
  222. debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
  223. flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
  224. idx = 0;
  225. while (idx < QT_BUFFER_CNT) {
  226. td->qt_buffer[idx] = cpu_to_hc32(virt_to_phys((void *)addr));
  227. td->qt_buffer_hi[idx] = 0;
  228. next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
  229. delta = next - addr;
  230. if (delta >= sz)
  231. break;
  232. sz -= delta;
  233. addr = next;
  234. idx++;
  235. }
  236. if (idx == QT_BUFFER_CNT) {
  237. printf("out of buffer pointers (%zu bytes left)\n", sz);
  238. return -1;
  239. }
  240. return 0;
  241. }
  242. static inline u8 ehci_encode_speed(enum usb_device_speed speed)
  243. {
  244. #define QH_HIGH_SPEED 2
  245. #define QH_FULL_SPEED 0
  246. #define QH_LOW_SPEED 1
  247. if (speed == USB_SPEED_HIGH)
  248. return QH_HIGH_SPEED;
  249. if (speed == USB_SPEED_LOW)
  250. return QH_LOW_SPEED;
  251. return QH_FULL_SPEED;
  252. }
  253. static void ehci_update_endpt2_dev_n_port(struct usb_device *udev,
  254. struct QH *qh)
  255. {
  256. uint8_t portnr = 0;
  257. uint8_t hubaddr = 0;
  258. if (udev->speed != USB_SPEED_LOW && udev->speed != USB_SPEED_FULL)
  259. return;
  260. usb_find_usb2_hub_address_port(udev, &hubaddr, &portnr);
  261. qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(portnr) |
  262. QH_ENDPT2_HUBADDR(hubaddr));
  263. }
  264. static int
  265. ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
  266. int length, struct devrequest *req)
  267. {
  268. ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
  269. struct qTD *qtd;
  270. int qtd_count = 0;
  271. int qtd_counter = 0;
  272. volatile struct qTD *vtd;
  273. unsigned long ts;
  274. uint32_t *tdp;
  275. uint32_t endpt, maxpacket, token, usbsts;
  276. uint32_t c, toggle;
  277. uint32_t cmd;
  278. int timeout;
  279. int ret = 0;
  280. struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
  281. debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
  282. buffer, length, req);
  283. if (req != NULL)
  284. debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
  285. req->request, req->request,
  286. req->requesttype, req->requesttype,
  287. le16_to_cpu(req->value), le16_to_cpu(req->value),
  288. le16_to_cpu(req->index));
  289. #define PKT_ALIGN 512
  290. /*
  291. * The USB transfer is split into qTD transfers. Eeach qTD transfer is
  292. * described by a transfer descriptor (the qTD). The qTDs form a linked
  293. * list with a queue head (QH).
  294. *
  295. * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
  296. * have its beginning in a qTD transfer and its end in the following
  297. * one, so the qTD transfer lengths have to be chosen accordingly.
  298. *
  299. * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
  300. * single pages. The first data buffer can start at any offset within a
  301. * page (not considering the cache-line alignment issues), while the
  302. * following buffers must be page-aligned. There is no alignment
  303. * constraint on the size of a qTD transfer.
  304. */
  305. if (req != NULL)
  306. /* 1 qTD will be needed for SETUP, and 1 for ACK. */
  307. qtd_count += 1 + 1;
  308. if (length > 0 || req == NULL) {
  309. /*
  310. * Determine the qTD transfer size that will be used for the
  311. * data payload (not considering the first qTD transfer, which
  312. * may be longer or shorter, and the final one, which may be
  313. * shorter).
  314. *
  315. * In order to keep each packet within a qTD transfer, the qTD
  316. * transfer size is aligned to PKT_ALIGN, which is a multiple of
  317. * wMaxPacketSize (except in some cases for interrupt transfers,
  318. * see comment in submit_int_msg()).
  319. *
  320. * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
  321. * QT_BUFFER_CNT full pages will be used.
  322. */
  323. int xfr_sz = QT_BUFFER_CNT;
  324. /*
  325. * However, if the input buffer is not aligned to PKT_ALIGN, the
  326. * qTD transfer size will be one page shorter, and the first qTD
  327. * data buffer of each transfer will be page-unaligned.
  328. */
  329. if ((unsigned long)buffer & (PKT_ALIGN - 1))
  330. xfr_sz--;
  331. /* Convert the qTD transfer size to bytes. */
  332. xfr_sz *= EHCI_PAGE_SIZE;
  333. /*
  334. * Approximate by excess the number of qTDs that will be
  335. * required for the data payload. The exact formula is way more
  336. * complicated and saves at most 2 qTDs, i.e. a total of 128
  337. * bytes.
  338. */
  339. qtd_count += 2 + length / xfr_sz;
  340. }
  341. /*
  342. * Threshold value based on the worst-case total size of the allocated qTDs for
  343. * a mass-storage transfer of 65535 blocks of 512 bytes.
  344. */
  345. #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
  346. #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
  347. #endif
  348. qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
  349. if (qtd == NULL) {
  350. printf("unable to allocate TDs\n");
  351. return -1;
  352. }
  353. memset(qh, 0, sizeof(struct QH));
  354. memset(qtd, 0, qtd_count * sizeof(*qtd));
  355. toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  356. /*
  357. * Setup QH (3.6 in ehci-r10.pdf)
  358. *
  359. * qh_link ................. 03-00 H
  360. * qh_endpt1 ............... 07-04 H
  361. * qh_endpt2 ............... 0B-08 H
  362. * - qh_curtd
  363. * qh_overlay.qt_next ...... 13-10 H
  364. * - qh_overlay.qt_altnext
  365. */
  366. qh->qh_link = cpu_to_hc32(virt_to_phys(&ctrl->qh_list) | QH_LINK_TYPE_QH);
  367. c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
  368. maxpacket = usb_maxpacket(dev, pipe);
  369. endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
  370. QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
  371. QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
  372. QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
  373. QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
  374. QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
  375. qh->qh_endpt1 = cpu_to_hc32(endpt);
  376. endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
  377. qh->qh_endpt2 = cpu_to_hc32(endpt);
  378. ehci_update_endpt2_dev_n_port(dev, qh);
  379. qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  380. qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  381. tdp = &qh->qh_overlay.qt_next;
  382. if (req != NULL) {
  383. /*
  384. * Setup request qTD (3.5 in ehci-r10.pdf)
  385. *
  386. * qt_next ................ 03-00 H
  387. * qt_altnext ............. 07-04 H
  388. * qt_token ............... 0B-08 H
  389. *
  390. * [ buffer, buffer_hi ] loaded with "req".
  391. */
  392. qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  393. qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  394. token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
  395. QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
  396. QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
  397. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  398. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  399. if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
  400. printf("unable to construct SETUP TD\n");
  401. goto fail;
  402. }
  403. /* Update previous qTD! */
  404. *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
  405. tdp = &qtd[qtd_counter++].qt_next;
  406. toggle = 1;
  407. }
  408. if (length > 0 || req == NULL) {
  409. uint8_t *buf_ptr = buffer;
  410. int left_length = length;
  411. do {
  412. /*
  413. * Determine the size of this qTD transfer. By default,
  414. * QT_BUFFER_CNT full pages can be used.
  415. */
  416. int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
  417. /*
  418. * However, if the input buffer is not page-aligned, the
  419. * portion of the first page before the buffer start
  420. * offset within that page is unusable.
  421. */
  422. xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
  423. /*
  424. * In order to keep each packet within a qTD transfer,
  425. * align the qTD transfer size to PKT_ALIGN.
  426. */
  427. xfr_bytes &= ~(PKT_ALIGN - 1);
  428. /*
  429. * This transfer may be shorter than the available qTD
  430. * transfer size that has just been computed.
  431. */
  432. xfr_bytes = min(xfr_bytes, left_length);
  433. /*
  434. * Setup request qTD (3.5 in ehci-r10.pdf)
  435. *
  436. * qt_next ................ 03-00 H
  437. * qt_altnext ............. 07-04 H
  438. * qt_token ............... 0B-08 H
  439. *
  440. * [ buffer, buffer_hi ] loaded with "buffer".
  441. */
  442. qtd[qtd_counter].qt_next =
  443. cpu_to_hc32(QT_NEXT_TERMINATE);
  444. qtd[qtd_counter].qt_altnext =
  445. cpu_to_hc32(QT_NEXT_TERMINATE);
  446. token = QT_TOKEN_DT(toggle) |
  447. QT_TOKEN_TOTALBYTES(xfr_bytes) |
  448. QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
  449. QT_TOKEN_CERR(3) |
  450. QT_TOKEN_PID(usb_pipein(pipe) ?
  451. QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
  452. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  453. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  454. if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
  455. xfr_bytes)) {
  456. printf("unable to construct DATA TD\n");
  457. goto fail;
  458. }
  459. /* Update previous qTD! */
  460. *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
  461. tdp = &qtd[qtd_counter++].qt_next;
  462. /*
  463. * Data toggle has to be adjusted since the qTD transfer
  464. * size is not always an even multiple of
  465. * wMaxPacketSize.
  466. */
  467. if ((xfr_bytes / maxpacket) & 1)
  468. toggle ^= 1;
  469. buf_ptr += xfr_bytes;
  470. left_length -= xfr_bytes;
  471. } while (left_length > 0);
  472. }
  473. if (req != NULL) {
  474. /*
  475. * Setup request qTD (3.5 in ehci-r10.pdf)
  476. *
  477. * qt_next ................ 03-00 H
  478. * qt_altnext ............. 07-04 H
  479. * qt_token ............... 0B-08 H
  480. */
  481. qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  482. qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  483. token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
  484. QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
  485. QT_TOKEN_PID(usb_pipein(pipe) ?
  486. QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
  487. QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
  488. qtd[qtd_counter].qt_token = cpu_to_hc32(token);
  489. /* Update previous qTD! */
  490. *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
  491. tdp = &qtd[qtd_counter++].qt_next;
  492. }
  493. ctrl->qh_list.qh_link = cpu_to_hc32(virt_to_phys(qh) | QH_LINK_TYPE_QH);
  494. /* Flush dcache */
  495. flush_dcache_range((unsigned long)&ctrl->qh_list,
  496. ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
  497. flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
  498. flush_dcache_range((unsigned long)qtd,
  499. ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
  500. /* Set async. queue head pointer. */
  501. ehci_writel(&ctrl->hcor->or_asynclistaddr, virt_to_phys(&ctrl->qh_list));
  502. usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
  503. ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
  504. /* Enable async. schedule. */
  505. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  506. cmd |= CMD_ASE;
  507. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  508. ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
  509. 100 * 1000);
  510. if (ret < 0) {
  511. printf("EHCI fail timeout STS_ASS set\n");
  512. goto fail;
  513. }
  514. /* Wait for TDs to be processed. */
  515. ts = get_timer(0);
  516. vtd = &qtd[qtd_counter - 1];
  517. timeout = USB_TIMEOUT_MS(pipe);
  518. do {
  519. /* Invalidate dcache */
  520. invalidate_dcache_range((unsigned long)&ctrl->qh_list,
  521. ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
  522. invalidate_dcache_range((unsigned long)qh,
  523. ALIGN_END_ADDR(struct QH, qh, 1));
  524. invalidate_dcache_range((unsigned long)qtd,
  525. ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
  526. token = hc32_to_cpu(vtd->qt_token);
  527. if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
  528. break;
  529. WATCHDOG_RESET();
  530. } while (get_timer(ts) < timeout);
  531. /*
  532. * Invalidate the memory area occupied by buffer
  533. * Don't try to fix the buffer alignment, if it isn't properly
  534. * aligned it's upper layer's fault so let invalidate_dcache_range()
  535. * vow about it. But we have to fix the length as it's actual
  536. * transfer length and can be unaligned. This is potentially
  537. * dangerous operation, it's responsibility of the calling
  538. * code to make sure enough space is reserved.
  539. */
  540. invalidate_dcache_range((unsigned long)buffer,
  541. ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
  542. /* Check that the TD processing happened */
  543. if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
  544. printf("EHCI timed out on TD - token=%#x\n", token);
  545. /* Disable async schedule. */
  546. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  547. cmd &= ~CMD_ASE;
  548. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  549. ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
  550. 100 * 1000);
  551. if (ret < 0) {
  552. printf("EHCI fail timeout STS_ASS reset\n");
  553. goto fail;
  554. }
  555. token = hc32_to_cpu(qh->qh_overlay.qt_token);
  556. if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
  557. debug("TOKEN=%#x\n", token);
  558. switch (QT_TOKEN_GET_STATUS(token) &
  559. ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
  560. case 0:
  561. toggle = QT_TOKEN_GET_DT(token);
  562. usb_settoggle(dev, usb_pipeendpoint(pipe),
  563. usb_pipeout(pipe), toggle);
  564. dev->status = 0;
  565. break;
  566. case QT_TOKEN_STATUS_HALTED:
  567. dev->status = USB_ST_STALLED;
  568. break;
  569. case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
  570. case QT_TOKEN_STATUS_DATBUFERR:
  571. dev->status = USB_ST_BUF_ERR;
  572. break;
  573. case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
  574. case QT_TOKEN_STATUS_BABBLEDET:
  575. dev->status = USB_ST_BABBLE_DET;
  576. break;
  577. default:
  578. dev->status = USB_ST_CRC_ERR;
  579. if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
  580. dev->status |= USB_ST_STALLED;
  581. break;
  582. }
  583. dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
  584. } else {
  585. dev->act_len = 0;
  586. #ifndef CONFIG_USB_EHCI_FARADAY
  587. debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
  588. dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
  589. ehci_readl(&ctrl->hcor->or_portsc[0]),
  590. ehci_readl(&ctrl->hcor->or_portsc[1]));
  591. #endif
  592. }
  593. free(qtd);
  594. return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
  595. fail:
  596. free(qtd);
  597. return -1;
  598. }
  599. static int ehci_submit_root(struct usb_device *dev, unsigned long pipe,
  600. void *buffer, int length, struct devrequest *req)
  601. {
  602. uint8_t tmpbuf[4];
  603. u16 typeReq;
  604. void *srcptr = NULL;
  605. int len, srclen;
  606. uint32_t reg;
  607. uint32_t *status_reg;
  608. int port = le16_to_cpu(req->index) & 0xff;
  609. struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
  610. srclen = 0;
  611. debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
  612. req->request, req->request,
  613. req->requesttype, req->requesttype,
  614. le16_to_cpu(req->value), le16_to_cpu(req->index));
  615. typeReq = req->request | req->requesttype << 8;
  616. switch (typeReq) {
  617. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  618. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  619. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  620. status_reg = ctrl->ops.get_portsc_register(ctrl, port - 1);
  621. if (!status_reg)
  622. return -1;
  623. break;
  624. default:
  625. status_reg = NULL;
  626. break;
  627. }
  628. switch (typeReq) {
  629. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  630. switch (le16_to_cpu(req->value) >> 8) {
  631. case USB_DT_DEVICE:
  632. debug("USB_DT_DEVICE request\n");
  633. srcptr = &descriptor.device;
  634. srclen = descriptor.device.bLength;
  635. break;
  636. case USB_DT_CONFIG:
  637. debug("USB_DT_CONFIG config\n");
  638. srcptr = &descriptor.config;
  639. srclen = descriptor.config.bLength +
  640. descriptor.interface.bLength +
  641. descriptor.endpoint.bLength;
  642. break;
  643. case USB_DT_STRING:
  644. debug("USB_DT_STRING config\n");
  645. switch (le16_to_cpu(req->value) & 0xff) {
  646. case 0: /* Language */
  647. srcptr = "\4\3\1\0";
  648. srclen = 4;
  649. break;
  650. case 1: /* Vendor */
  651. srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
  652. srclen = 14;
  653. break;
  654. case 2: /* Product */
  655. srcptr = "\52\3E\0H\0C\0I\0 "
  656. "\0H\0o\0s\0t\0 "
  657. "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
  658. srclen = 42;
  659. break;
  660. default:
  661. debug("unknown value DT_STRING %x\n",
  662. le16_to_cpu(req->value));
  663. goto unknown;
  664. }
  665. break;
  666. default:
  667. debug("unknown value %x\n", le16_to_cpu(req->value));
  668. goto unknown;
  669. }
  670. break;
  671. case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
  672. switch (le16_to_cpu(req->value) >> 8) {
  673. case USB_DT_HUB:
  674. debug("USB_DT_HUB config\n");
  675. srcptr = &descriptor.hub;
  676. srclen = descriptor.hub.bLength;
  677. break;
  678. default:
  679. debug("unknown value %x\n", le16_to_cpu(req->value));
  680. goto unknown;
  681. }
  682. break;
  683. case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
  684. debug("USB_REQ_SET_ADDRESS\n");
  685. ctrl->rootdev = le16_to_cpu(req->value);
  686. break;
  687. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  688. debug("USB_REQ_SET_CONFIGURATION\n");
  689. /* Nothing to do */
  690. break;
  691. case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
  692. tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
  693. tmpbuf[1] = 0;
  694. srcptr = tmpbuf;
  695. srclen = 2;
  696. break;
  697. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  698. memset(tmpbuf, 0, 4);
  699. reg = ehci_readl(status_reg);
  700. if (reg & EHCI_PS_CS)
  701. tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
  702. if (reg & EHCI_PS_PE)
  703. tmpbuf[0] |= USB_PORT_STAT_ENABLE;
  704. if (reg & EHCI_PS_SUSP)
  705. tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
  706. if (reg & EHCI_PS_OCA)
  707. tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
  708. if (reg & EHCI_PS_PR)
  709. tmpbuf[0] |= USB_PORT_STAT_RESET;
  710. if (reg & EHCI_PS_PP)
  711. tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
  712. if (ehci_is_TDI()) {
  713. switch (ctrl->ops.get_port_speed(ctrl, reg)) {
  714. case PORTSC_PSPD_FS:
  715. break;
  716. case PORTSC_PSPD_LS:
  717. tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
  718. break;
  719. case PORTSC_PSPD_HS:
  720. default:
  721. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  722. break;
  723. }
  724. } else {
  725. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  726. }
  727. if (reg & EHCI_PS_CSC)
  728. tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
  729. if (reg & EHCI_PS_PEC)
  730. tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
  731. if (reg & EHCI_PS_OCC)
  732. tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
  733. if (ctrl->portreset & (1 << port))
  734. tmpbuf[2] |= USB_PORT_STAT_C_RESET;
  735. srcptr = tmpbuf;
  736. srclen = 4;
  737. break;
  738. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  739. reg = ehci_readl(status_reg);
  740. reg &= ~EHCI_PS_CLEAR;
  741. switch (le16_to_cpu(req->value)) {
  742. case USB_PORT_FEAT_ENABLE:
  743. reg |= EHCI_PS_PE;
  744. ehci_writel(status_reg, reg);
  745. break;
  746. case USB_PORT_FEAT_POWER:
  747. if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
  748. reg |= EHCI_PS_PP;
  749. ehci_writel(status_reg, reg);
  750. }
  751. break;
  752. case USB_PORT_FEAT_RESET:
  753. if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
  754. !ehci_is_TDI() &&
  755. EHCI_PS_IS_LOWSPEED(reg)) {
  756. /* Low speed device, give up ownership. */
  757. debug("port %d low speed --> companion\n",
  758. port - 1);
  759. reg |= EHCI_PS_PO;
  760. ehci_writel(status_reg, reg);
  761. return -ENXIO;
  762. } else {
  763. int ret;
  764. reg |= EHCI_PS_PR;
  765. reg &= ~EHCI_PS_PE;
  766. ehci_writel(status_reg, reg);
  767. /*
  768. * caller must wait, then call GetPortStatus
  769. * usb 2.0 specification say 50 ms resets on
  770. * root
  771. */
  772. ctrl->ops.powerup_fixup(ctrl, status_reg, &reg);
  773. ehci_writel(status_reg, reg & ~EHCI_PS_PR);
  774. /*
  775. * A host controller must terminate the reset
  776. * and stabilize the state of the port within
  777. * 2 milliseconds
  778. */
  779. ret = handshake(status_reg, EHCI_PS_PR, 0,
  780. 2 * 1000);
  781. if (!ret) {
  782. reg = ehci_readl(status_reg);
  783. if ((reg & (EHCI_PS_PE | EHCI_PS_CS))
  784. == EHCI_PS_CS && !ehci_is_TDI()) {
  785. debug("port %d full speed --> companion\n", port - 1);
  786. reg &= ~EHCI_PS_CLEAR;
  787. reg |= EHCI_PS_PO;
  788. ehci_writel(status_reg, reg);
  789. return -ENXIO;
  790. } else {
  791. ctrl->portreset |= 1 << port;
  792. }
  793. } else {
  794. printf("port(%d) reset error\n",
  795. port - 1);
  796. }
  797. }
  798. break;
  799. case USB_PORT_FEAT_TEST:
  800. ehci_shutdown(ctrl);
  801. reg &= ~(0xf << 16);
  802. reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
  803. ehci_writel(status_reg, reg);
  804. break;
  805. default:
  806. debug("unknown feature %x\n", le16_to_cpu(req->value));
  807. goto unknown;
  808. }
  809. /* unblock posted writes */
  810. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  811. break;
  812. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  813. reg = ehci_readl(status_reg);
  814. reg &= ~EHCI_PS_CLEAR;
  815. switch (le16_to_cpu(req->value)) {
  816. case USB_PORT_FEAT_ENABLE:
  817. reg &= ~EHCI_PS_PE;
  818. break;
  819. case USB_PORT_FEAT_C_ENABLE:
  820. reg |= EHCI_PS_PE;
  821. break;
  822. case USB_PORT_FEAT_POWER:
  823. if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
  824. reg &= ~EHCI_PS_PP;
  825. break;
  826. case USB_PORT_FEAT_C_CONNECTION:
  827. reg |= EHCI_PS_CSC;
  828. break;
  829. case USB_PORT_FEAT_OVER_CURRENT:
  830. reg |= EHCI_PS_OCC;
  831. break;
  832. case USB_PORT_FEAT_C_RESET:
  833. ctrl->portreset &= ~(1 << port);
  834. break;
  835. default:
  836. debug("unknown feature %x\n", le16_to_cpu(req->value));
  837. goto unknown;
  838. }
  839. ehci_writel(status_reg, reg);
  840. /* unblock posted write */
  841. (void) ehci_readl(&ctrl->hcor->or_usbcmd);
  842. break;
  843. default:
  844. debug("Unknown request\n");
  845. goto unknown;
  846. }
  847. mdelay(1);
  848. len = min3(srclen, (int)le16_to_cpu(req->length), length);
  849. if (srcptr != NULL && len > 0)
  850. memcpy(buffer, srcptr, len);
  851. else
  852. debug("Len is 0\n");
  853. dev->act_len = len;
  854. dev->status = 0;
  855. return 0;
  856. unknown:
  857. debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
  858. req->requesttype, req->request, le16_to_cpu(req->value),
  859. le16_to_cpu(req->index), le16_to_cpu(req->length));
  860. dev->act_len = 0;
  861. dev->status = USB_ST_STALLED;
  862. return -1;
  863. }
  864. static const struct ehci_ops default_ehci_ops = {
  865. .set_usb_mode = ehci_set_usbmode,
  866. .get_port_speed = ehci_get_port_speed,
  867. .powerup_fixup = ehci_powerup_fixup,
  868. .get_portsc_register = ehci_get_portsc_register,
  869. };
  870. static void ehci_setup_ops(struct ehci_ctrl *ctrl, const struct ehci_ops *ops)
  871. {
  872. if (!ops) {
  873. ctrl->ops = default_ehci_ops;
  874. } else {
  875. ctrl->ops = *ops;
  876. if (!ctrl->ops.set_usb_mode)
  877. ctrl->ops.set_usb_mode = ehci_set_usbmode;
  878. if (!ctrl->ops.get_port_speed)
  879. ctrl->ops.get_port_speed = ehci_get_port_speed;
  880. if (!ctrl->ops.powerup_fixup)
  881. ctrl->ops.powerup_fixup = ehci_powerup_fixup;
  882. if (!ctrl->ops.get_portsc_register)
  883. ctrl->ops.get_portsc_register =
  884. ehci_get_portsc_register;
  885. }
  886. }
  887. #ifndef CONFIG_DM_USB
  888. void ehci_set_controller_priv(int index, void *priv, const struct ehci_ops *ops)
  889. {
  890. struct ehci_ctrl *ctrl = &ehcic[index];
  891. ctrl->priv = priv;
  892. ehci_setup_ops(ctrl, ops);
  893. }
  894. void *ehci_get_controller_priv(int index)
  895. {
  896. return ehcic[index].priv;
  897. }
  898. #endif
  899. static int ehci_common_init(struct ehci_ctrl *ctrl, uint tweaks)
  900. {
  901. struct QH *qh_list;
  902. struct QH *periodic;
  903. uint32_t reg;
  904. uint32_t cmd;
  905. int i;
  906. /* Set the high address word (aka segment) for 64-bit controller */
  907. if (ehci_readl(&ctrl->hccr->cr_hccparams) & 1)
  908. ehci_writel(&ctrl->hcor->or_ctrldssegment, 0);
  909. qh_list = &ctrl->qh_list;
  910. /* Set head of reclaim list */
  911. memset(qh_list, 0, sizeof(*qh_list));
  912. qh_list->qh_link = cpu_to_hc32(virt_to_phys(qh_list) | QH_LINK_TYPE_QH);
  913. qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
  914. QH_ENDPT1_EPS(USB_SPEED_HIGH));
  915. qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  916. qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  917. qh_list->qh_overlay.qt_token =
  918. cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
  919. flush_dcache_range((unsigned long)qh_list,
  920. ALIGN_END_ADDR(struct QH, qh_list, 1));
  921. /* Set async. queue head pointer. */
  922. ehci_writel(&ctrl->hcor->or_asynclistaddr, virt_to_phys(qh_list));
  923. /*
  924. * Set up periodic list
  925. * Step 1: Parent QH for all periodic transfers.
  926. */
  927. ctrl->periodic_schedules = 0;
  928. periodic = &ctrl->periodic_queue;
  929. memset(periodic, 0, sizeof(*periodic));
  930. periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
  931. periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
  932. periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
  933. flush_dcache_range((unsigned long)periodic,
  934. ALIGN_END_ADDR(struct QH, periodic, 1));
  935. /*
  936. * Step 2: Setup frame-list: Every microframe, USB tries the same list.
  937. * In particular, device specifications on polling frequency
  938. * are disregarded. Keyboards seem to send NAK/NYet reliably
  939. * when polled with an empty buffer.
  940. *
  941. * Split Transactions will be spread across microframes using
  942. * S-mask and C-mask.
  943. */
  944. if (ctrl->periodic_list == NULL)
  945. ctrl->periodic_list = memalign(4096, 1024 * 4);
  946. if (!ctrl->periodic_list)
  947. return -ENOMEM;
  948. for (i = 0; i < 1024; i++) {
  949. ctrl->periodic_list[i] = cpu_to_hc32((unsigned long)periodic
  950. | QH_LINK_TYPE_QH);
  951. }
  952. flush_dcache_range((unsigned long)ctrl->periodic_list,
  953. ALIGN_END_ADDR(uint32_t, ctrl->periodic_list,
  954. 1024));
  955. /* Set periodic list base address */
  956. ehci_writel(&ctrl->hcor->or_periodiclistbase,
  957. (unsigned long)ctrl->periodic_list);
  958. reg = ehci_readl(&ctrl->hccr->cr_hcsparams);
  959. descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
  960. debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  961. /* Port Indicators */
  962. if (HCS_INDICATOR(reg))
  963. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  964. | 0x80, &descriptor.hub.wHubCharacteristics);
  965. /* Port Power Control */
  966. if (HCS_PPC(reg))
  967. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  968. | 0x01, &descriptor.hub.wHubCharacteristics);
  969. /* Start the host controller. */
  970. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  971. /*
  972. * Philips, Intel, and maybe others need CMD_RUN before the
  973. * root hub will detect new devices (why?); NEC doesn't
  974. */
  975. cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  976. cmd |= CMD_RUN;
  977. ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
  978. if (!(tweaks & EHCI_TWEAK_NO_INIT_CF)) {
  979. /* take control over the ports */
  980. cmd = ehci_readl(&ctrl->hcor->or_configflag);
  981. cmd |= FLAG_CF;
  982. ehci_writel(&ctrl->hcor->or_configflag, cmd);
  983. }
  984. /* unblock posted write */
  985. cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
  986. mdelay(5);
  987. reg = HC_VERSION(ehci_readl(&ctrl->hccr->cr_capbase));
  988. printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
  989. return 0;
  990. }
  991. #ifndef CONFIG_DM_USB
  992. int usb_lowlevel_stop(int index)
  993. {
  994. ehci_shutdown(&ehcic[index]);
  995. return ehci_hcd_stop(index);
  996. }
  997. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  998. {
  999. struct ehci_ctrl *ctrl = &ehcic[index];
  1000. uint tweaks = 0;
  1001. int rc;
  1002. /**
  1003. * Set ops to default_ehci_ops, ehci_hcd_init should call
  1004. * ehci_set_controller_priv to change any of these function pointers.
  1005. */
  1006. ctrl->ops = default_ehci_ops;
  1007. rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
  1008. if (rc)
  1009. return rc;
  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;
  1444. debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p, init=%d\n", __func__,
  1445. dev->name, ctrl, hccr, hcor, init);
  1446. priv->desc_before_addr = true;
  1447. ehci_setup_ops(ctrl, ops);
  1448. ctrl->hccr = hccr;
  1449. ctrl->hcor = hcor;
  1450. ctrl->priv = ctrl;
  1451. ctrl->init = init;
  1452. if (ctrl->init == USB_INIT_DEVICE)
  1453. goto done;
  1454. ret = ehci_reset(ctrl);
  1455. if (ret)
  1456. goto err;
  1457. if (ctrl->ops.init_after_reset) {
  1458. ret = ctrl->ops.init_after_reset(ctrl);
  1459. if (ret)
  1460. goto err;
  1461. }
  1462. ret = ehci_common_init(ctrl, tweaks);
  1463. if (ret)
  1464. goto err;
  1465. done:
  1466. return 0;
  1467. err:
  1468. free(ctrl);
  1469. debug("%s: failed, ret=%d\n", __func__, ret);
  1470. return ret;
  1471. }
  1472. int ehci_deregister(struct udevice *dev)
  1473. {
  1474. struct ehci_ctrl *ctrl = dev_get_priv(dev);
  1475. if (ctrl->init == USB_INIT_DEVICE)
  1476. return 0;
  1477. ehci_shutdown(ctrl);
  1478. return 0;
  1479. }
  1480. struct dm_usb_ops ehci_usb_ops = {
  1481. .control = ehci_submit_control_msg,
  1482. .bulk = ehci_submit_bulk_msg,
  1483. .interrupt = ehci_submit_int_msg,
  1484. .create_int_queue = ehci_create_int_queue,
  1485. .poll_int_queue = ehci_poll_int_queue,
  1486. .destroy_int_queue = ehci_destroy_int_queue,
  1487. .get_max_xfer_size = ehci_get_max_xfer_size,
  1488. };
  1489. #endif