ehci-hcd.c 46 KB

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