xhci.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /*
  2. * USB HOST XHCI Controller stack
  3. *
  4. * Based on xHCI host controller driver in linux-kernel
  5. * by Sarah Sharp.
  6. *
  7. * Copyright (C) 2008 Intel Corp.
  8. * Author: Sarah Sharp
  9. *
  10. * Copyright (C) 2013 Samsung Electronics Co.Ltd
  11. * Authors: Vivek Gautam <gautam.vivek@samsung.com>
  12. * Vikas Sajjan <vikas.sajjan@samsung.com>
  13. *
  14. * SPDX-License-Identifier: GPL-2.0+
  15. */
  16. /**
  17. * This file gives the xhci stack for usb3.0 looking into
  18. * xhci specification Rev1.0 (5/21/10).
  19. * The quirk devices support hasn't been given yet.
  20. */
  21. #include <common.h>
  22. #include <dm.h>
  23. #include <asm/byteorder.h>
  24. #include <usb.h>
  25. #include <malloc.h>
  26. #include <watchdog.h>
  27. #include <asm/cache.h>
  28. #include <asm/unaligned.h>
  29. #include <asm-generic/errno.h>
  30. #include "xhci.h"
  31. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  32. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  33. #endif
  34. static struct descriptor {
  35. struct usb_hub_descriptor hub;
  36. struct usb_device_descriptor device;
  37. struct usb_config_descriptor config;
  38. struct usb_interface_descriptor interface;
  39. struct usb_endpoint_descriptor endpoint;
  40. struct usb_ss_ep_comp_descriptor ep_companion;
  41. } __attribute__ ((packed)) descriptor = {
  42. {
  43. 0xc, /* bDescLength */
  44. 0x2a, /* bDescriptorType: hub descriptor */
  45. 2, /* bNrPorts -- runtime modified */
  46. cpu_to_le16(0x8), /* 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(0x0300), /* bcdUSB: v3.0 */
  56. 9, /* bDeviceClass: UDCLASS_HUB */
  57. 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
  58. 3, /* bDeviceProtocol: UDPROTO_SSHUBSTT */
  59. 9, /* bMaxPacketSize: 512 bytes 2^9 */
  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(0x1f), /* includes SS endpoint descriptor */
  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: IN endpoint 1 */
  93. 3, /* bmAttributes: UE_INTERRUPT */
  94. 8, /* wMaxPacketSize */
  95. 255 /* bInterval */
  96. },
  97. {
  98. 0x06, /* ss_bLength */
  99. 0x30, /* ss_bDescriptorType: SS EP Companion */
  100. 0x00, /* ss_bMaxBurst: allows 1 TX between ACKs */
  101. /* ss_bmAttributes: 1 packet per service interval */
  102. 0x00,
  103. /* ss_wBytesPerInterval: 15 bits for max 15 ports */
  104. cpu_to_le16(0x02),
  105. },
  106. };
  107. #ifndef CONFIG_DM_USB
  108. static struct xhci_ctrl xhcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
  109. #endif
  110. struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev)
  111. {
  112. #ifdef CONFIG_DM_USB
  113. struct udevice *dev;
  114. /* Find the USB controller */
  115. for (dev = udev->dev;
  116. device_get_uclass_id(dev) != UCLASS_USB;
  117. dev = dev->parent)
  118. ;
  119. return dev_get_priv(dev);
  120. #else
  121. return udev->controller;
  122. #endif
  123. }
  124. /**
  125. * Waits for as per specified amount of time
  126. * for the "result" to match with "done"
  127. *
  128. * @param ptr pointer to the register to be read
  129. * @param mask mask for the value read
  130. * @param done value to be campared with result
  131. * @param usec time to wait till
  132. * @return 0 if handshake is success else < 0 on failure
  133. */
  134. static int handshake(uint32_t volatile *ptr, uint32_t mask,
  135. uint32_t done, int usec)
  136. {
  137. uint32_t result;
  138. do {
  139. result = xhci_readl(ptr);
  140. if (result == ~(uint32_t)0)
  141. return -ENODEV;
  142. result &= mask;
  143. if (result == done)
  144. return 0;
  145. usec--;
  146. udelay(1);
  147. } while (usec > 0);
  148. return -ETIMEDOUT;
  149. }
  150. /**
  151. * Set the run bit and wait for the host to be running.
  152. *
  153. * @param hcor pointer to host controller operation registers
  154. * @return status of the Handshake
  155. */
  156. static int xhci_start(struct xhci_hcor *hcor)
  157. {
  158. u32 temp;
  159. int ret;
  160. puts("Starting the controller\n");
  161. temp = xhci_readl(&hcor->or_usbcmd);
  162. temp |= (CMD_RUN);
  163. xhci_writel(&hcor->or_usbcmd, temp);
  164. /*
  165. * Wait for the HCHalted Status bit to be 0 to indicate the host is
  166. * running.
  167. */
  168. ret = handshake(&hcor->or_usbsts, STS_HALT, 0, XHCI_MAX_HALT_USEC);
  169. if (ret)
  170. debug("Host took too long to start, "
  171. "waited %u microseconds.\n",
  172. XHCI_MAX_HALT_USEC);
  173. return ret;
  174. }
  175. /**
  176. * Resets the XHCI Controller
  177. *
  178. * @param hcor pointer to host controller operation registers
  179. * @return -EBUSY if XHCI Controller is not halted else status of handshake
  180. */
  181. int xhci_reset(struct xhci_hcor *hcor)
  182. {
  183. u32 cmd;
  184. u32 state;
  185. int ret;
  186. /* Halting the Host first */
  187. debug("// Halt the HC: %p\n", hcor);
  188. state = xhci_readl(&hcor->or_usbsts) & STS_HALT;
  189. if (!state) {
  190. cmd = xhci_readl(&hcor->or_usbcmd);
  191. cmd &= ~CMD_RUN;
  192. xhci_writel(&hcor->or_usbcmd, cmd);
  193. }
  194. ret = handshake(&hcor->or_usbsts,
  195. STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
  196. if (ret) {
  197. printf("Host not halted after %u microseconds.\n",
  198. XHCI_MAX_HALT_USEC);
  199. return -EBUSY;
  200. }
  201. debug("// Reset the HC\n");
  202. cmd = xhci_readl(&hcor->or_usbcmd);
  203. cmd |= CMD_RESET;
  204. xhci_writel(&hcor->or_usbcmd, cmd);
  205. ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC);
  206. if (ret)
  207. return ret;
  208. /*
  209. * xHCI cannot write to any doorbells or operational registers other
  210. * than status until the "Controller Not Ready" flag is cleared.
  211. */
  212. return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC);
  213. }
  214. /**
  215. * Used for passing endpoint bitmasks between the core and HCDs.
  216. * Find the index for an endpoint given its descriptor.
  217. * Use the return value to right shift 1 for the bitmask.
  218. *
  219. * Index = (epnum * 2) + direction - 1,
  220. * where direction = 0 for OUT, 1 for IN.
  221. * For control endpoints, the IN index is used (OUT index is unused), so
  222. * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
  223. *
  224. * @param desc USB enpdoint Descriptor
  225. * @return index of the Endpoint
  226. */
  227. static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc)
  228. {
  229. unsigned int index;
  230. if (usb_endpoint_xfer_control(desc))
  231. index = (unsigned int)(usb_endpoint_num(desc) * 2);
  232. else
  233. index = (unsigned int)((usb_endpoint_num(desc) * 2) -
  234. (usb_endpoint_dir_in(desc) ? 0 : 1));
  235. return index;
  236. }
  237. /**
  238. * Issue a configure endpoint command or evaluate context command
  239. * and wait for it to finish.
  240. *
  241. * @param udev pointer to the Device Data Structure
  242. * @param ctx_change flag to indicate the Context has changed or NOT
  243. * @return 0 on success, -1 on failure
  244. */
  245. static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change)
  246. {
  247. struct xhci_container_ctx *in_ctx;
  248. struct xhci_virt_device *virt_dev;
  249. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  250. union xhci_trb *event;
  251. virt_dev = ctrl->devs[udev->slot_id];
  252. in_ctx = virt_dev->in_ctx;
  253. xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size);
  254. xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0,
  255. ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP);
  256. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  257. BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
  258. != udev->slot_id);
  259. switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
  260. case COMP_SUCCESS:
  261. debug("Successful %s command\n",
  262. ctx_change ? "Evaluate Context" : "Configure Endpoint");
  263. break;
  264. default:
  265. printf("ERROR: %s command returned completion code %d.\n",
  266. ctx_change ? "Evaluate Context" : "Configure Endpoint",
  267. GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
  268. return -EINVAL;
  269. }
  270. xhci_acknowledge_event(ctrl);
  271. return 0;
  272. }
  273. /**
  274. * Configure the endpoint, programming the device contexts.
  275. *
  276. * @param udev pointer to the USB device structure
  277. * @return returns the status of the xhci_configure_endpoints
  278. */
  279. static int xhci_set_configuration(struct usb_device *udev)
  280. {
  281. struct xhci_container_ctx *in_ctx;
  282. struct xhci_container_ctx *out_ctx;
  283. struct xhci_input_control_ctx *ctrl_ctx;
  284. struct xhci_slot_ctx *slot_ctx;
  285. struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM];
  286. int cur_ep;
  287. int max_ep_flag = 0;
  288. int ep_index;
  289. unsigned int dir;
  290. unsigned int ep_type;
  291. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  292. int num_of_ep;
  293. int ep_flag = 0;
  294. u64 trb_64 = 0;
  295. int slot_id = udev->slot_id;
  296. struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
  297. struct usb_interface *ifdesc;
  298. out_ctx = virt_dev->out_ctx;
  299. in_ctx = virt_dev->in_ctx;
  300. num_of_ep = udev->config.if_desc[0].no_of_ep;
  301. ifdesc = &udev->config.if_desc[0];
  302. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  303. /* Zero the input context control */
  304. ctrl_ctx->add_flags = 0;
  305. ctrl_ctx->drop_flags = 0;
  306. /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
  307. for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
  308. ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
  309. ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
  310. if (max_ep_flag < ep_flag)
  311. max_ep_flag = ep_flag;
  312. }
  313. xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
  314. /* slot context */
  315. xhci_slot_copy(ctrl, in_ctx, out_ctx);
  316. slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
  317. slot_ctx->dev_info &= ~(LAST_CTX_MASK);
  318. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0);
  319. xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0);
  320. /* filling up ep contexts */
  321. for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
  322. struct usb_endpoint_descriptor *endpt_desc = NULL;
  323. endpt_desc = &ifdesc->ep_desc[cur_ep];
  324. trb_64 = 0;
  325. ep_index = xhci_get_ep_index(endpt_desc);
  326. ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
  327. /* Allocate the ep rings */
  328. virt_dev->eps[ep_index].ring = xhci_ring_alloc(1, true);
  329. if (!virt_dev->eps[ep_index].ring)
  330. return -ENOMEM;
  331. /*NOTE: ep_desc[0] actually represents EP1 and so on */
  332. dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7);
  333. ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2));
  334. ep_ctx[ep_index]->ep_info2 =
  335. cpu_to_le32(ep_type << EP_TYPE_SHIFT);
  336. ep_ctx[ep_index]->ep_info2 |=
  337. cpu_to_le32(MAX_PACKET
  338. (get_unaligned(&endpt_desc->wMaxPacketSize)));
  339. ep_ctx[ep_index]->ep_info2 |=
  340. cpu_to_le32(((0 & MAX_BURST_MASK) << MAX_BURST_SHIFT) |
  341. ((3 & ERROR_COUNT_MASK) << ERROR_COUNT_SHIFT));
  342. trb_64 = (uintptr_t)
  343. virt_dev->eps[ep_index].ring->enqueue;
  344. ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 |
  345. virt_dev->eps[ep_index].ring->cycle_state);
  346. }
  347. return xhci_configure_endpoints(udev, false);
  348. }
  349. /**
  350. * Issue an Address Device command (which will issue a SetAddress request to
  351. * the device).
  352. *
  353. * @param udev pointer to the Device Data Structure
  354. * @return 0 if successful else error code on failure
  355. */
  356. static int xhci_address_device(struct usb_device *udev, int root_portnr)
  357. {
  358. int ret = 0;
  359. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  360. struct xhci_slot_ctx *slot_ctx;
  361. struct xhci_input_control_ctx *ctrl_ctx;
  362. struct xhci_virt_device *virt_dev;
  363. int slot_id = udev->slot_id;
  364. union xhci_trb *event;
  365. virt_dev = ctrl->devs[slot_id];
  366. /*
  367. * This is the first Set Address since device plug-in
  368. * so setting up the slot context.
  369. */
  370. debug("Setting up addressable devices %p\n", ctrl->dcbaa);
  371. xhci_setup_addressable_virt_dev(ctrl, udev->slot_id, udev->speed,
  372. root_portnr);
  373. ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
  374. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
  375. ctrl_ctx->drop_flags = 0;
  376. xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV);
  377. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  378. BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
  379. switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
  380. case COMP_CTX_STATE:
  381. case COMP_EBADSLT:
  382. printf("Setup ERROR: address device command for slot %d.\n",
  383. slot_id);
  384. ret = -EINVAL;
  385. break;
  386. case COMP_TX_ERR:
  387. puts("Device not responding to set address.\n");
  388. ret = -EPROTO;
  389. break;
  390. case COMP_DEV_ERR:
  391. puts("ERROR: Incompatible device"
  392. "for address device command.\n");
  393. ret = -ENODEV;
  394. break;
  395. case COMP_SUCCESS:
  396. debug("Successful Address Device command\n");
  397. udev->status = 0;
  398. break;
  399. default:
  400. printf("ERROR: unexpected command completion code 0x%x.\n",
  401. GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
  402. ret = -EINVAL;
  403. break;
  404. }
  405. xhci_acknowledge_event(ctrl);
  406. if (ret < 0)
  407. /*
  408. * TODO: Unsuccessful Address Device command shall leave the
  409. * slot in default state. So, issue Disable Slot command now.
  410. */
  411. return ret;
  412. xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
  413. virt_dev->out_ctx->size);
  414. slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx);
  415. debug("xHC internal address is: %d\n",
  416. le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
  417. return 0;
  418. }
  419. /**
  420. * Issue Enable slot command to the controller to allocate
  421. * device slot and assign the slot id. It fails if the xHC
  422. * ran out of device slots, the Enable Slot command timed out,
  423. * or allocating memory failed.
  424. *
  425. * @param udev pointer to the Device Data Structure
  426. * @return Returns 0 on succes else return error code on failure
  427. */
  428. int _xhci_alloc_device(struct usb_device *udev)
  429. {
  430. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  431. union xhci_trb *event;
  432. int ret;
  433. /*
  434. * Root hub will be first device to be initailized.
  435. * If this device is root-hub, don't do any xHC related
  436. * stuff.
  437. */
  438. if (ctrl->rootdev == 0) {
  439. udev->speed = USB_SPEED_SUPER;
  440. return 0;
  441. }
  442. xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT);
  443. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  444. BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
  445. != COMP_SUCCESS);
  446. udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags));
  447. xhci_acknowledge_event(ctrl);
  448. ret = xhci_alloc_virt_device(ctrl, udev->slot_id);
  449. if (ret < 0) {
  450. /*
  451. * TODO: Unsuccessful Address Device command shall leave
  452. * the slot in default. So, issue Disable Slot command now.
  453. */
  454. puts("Could not allocate xHCI USB device data structures\n");
  455. return ret;
  456. }
  457. return 0;
  458. }
  459. #ifndef CONFIG_DM_USB
  460. int usb_alloc_device(struct usb_device *udev)
  461. {
  462. return _xhci_alloc_device(udev);
  463. }
  464. #endif
  465. /*
  466. * Full speed devices may have a max packet size greater than 8 bytes, but the
  467. * USB core doesn't know that until it reads the first 8 bytes of the
  468. * descriptor. If the usb_device's max packet size changes after that point,
  469. * we need to issue an evaluate context command and wait on it.
  470. *
  471. * @param udev pointer to the Device Data Structure
  472. * @return returns the status of the xhci_configure_endpoints
  473. */
  474. int xhci_check_maxpacket(struct usb_device *udev)
  475. {
  476. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  477. unsigned int slot_id = udev->slot_id;
  478. int ep_index = 0; /* control endpoint */
  479. struct xhci_container_ctx *in_ctx;
  480. struct xhci_container_ctx *out_ctx;
  481. struct xhci_input_control_ctx *ctrl_ctx;
  482. struct xhci_ep_ctx *ep_ctx;
  483. int max_packet_size;
  484. int hw_max_packet_size;
  485. int ret = 0;
  486. struct usb_interface *ifdesc;
  487. ifdesc = &udev->config.if_desc[0];
  488. out_ctx = ctrl->devs[slot_id]->out_ctx;
  489. xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
  490. ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
  491. hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  492. max_packet_size = usb_endpoint_maxp(&ifdesc->ep_desc[0]);
  493. if (hw_max_packet_size != max_packet_size) {
  494. debug("Max Packet Size for ep 0 changed.\n");
  495. debug("Max packet size in usb_device = %d\n", max_packet_size);
  496. debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size);
  497. debug("Issuing evaluate context command.\n");
  498. /* Set up the modified control endpoint 0 */
  499. xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx,
  500. ctrl->devs[slot_id]->out_ctx, ep_index);
  501. in_ctx = ctrl->devs[slot_id]->in_ctx;
  502. ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
  503. ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
  504. ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
  505. /*
  506. * Set up the input context flags for the command
  507. * FIXME: This won't work if a non-default control endpoint
  508. * changes max packet sizes.
  509. */
  510. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  511. ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
  512. ctrl_ctx->drop_flags = 0;
  513. ret = xhci_configure_endpoints(udev, true);
  514. }
  515. return ret;
  516. }
  517. /**
  518. * Clears the Change bits of the Port Status Register
  519. *
  520. * @param wValue request value
  521. * @param wIndex request index
  522. * @param addr address of posrt status register
  523. * @param port_status state of port status register
  524. * @return none
  525. */
  526. static void xhci_clear_port_change_bit(u16 wValue,
  527. u16 wIndex, volatile uint32_t *addr, u32 port_status)
  528. {
  529. char *port_change_bit;
  530. u32 status;
  531. switch (wValue) {
  532. case USB_PORT_FEAT_C_RESET:
  533. status = PORT_RC;
  534. port_change_bit = "reset";
  535. break;
  536. case USB_PORT_FEAT_C_CONNECTION:
  537. status = PORT_CSC;
  538. port_change_bit = "connect";
  539. break;
  540. case USB_PORT_FEAT_C_OVER_CURRENT:
  541. status = PORT_OCC;
  542. port_change_bit = "over-current";
  543. break;
  544. case USB_PORT_FEAT_C_ENABLE:
  545. status = PORT_PEC;
  546. port_change_bit = "enable/disable";
  547. break;
  548. case USB_PORT_FEAT_C_SUSPEND:
  549. status = PORT_PLC;
  550. port_change_bit = "suspend/resume";
  551. break;
  552. default:
  553. /* Should never happen */
  554. return;
  555. }
  556. /* Change bits are all write 1 to clear */
  557. xhci_writel(addr, port_status | status);
  558. port_status = xhci_readl(addr);
  559. debug("clear port %s change, actual port %d status = 0x%x\n",
  560. port_change_bit, wIndex, port_status);
  561. }
  562. /**
  563. * Save Read Only (RO) bits and save read/write bits where
  564. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  565. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  566. *
  567. * @param state state of the Port Status and Control Regsiter
  568. * @return a value that would result in the port being in the
  569. * same state, if the value was written to the port
  570. * status control register.
  571. */
  572. static u32 xhci_port_state_to_neutral(u32 state)
  573. {
  574. /* Save read-only status and port state */
  575. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  576. }
  577. /**
  578. * Submits the Requests to the XHCI Host Controller
  579. *
  580. * @param udev pointer to the USB device structure
  581. * @param pipe contains the DIR_IN or OUT , devnum
  582. * @param buffer buffer to be read/written based on the request
  583. * @return returns 0 if successful else -1 on failure
  584. */
  585. static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
  586. void *buffer, struct devrequest *req)
  587. {
  588. uint8_t tmpbuf[4];
  589. u16 typeReq;
  590. void *srcptr = NULL;
  591. int len, srclen;
  592. uint32_t reg;
  593. volatile uint32_t *status_reg;
  594. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  595. struct xhci_hcor *hcor = ctrl->hcor;
  596. if ((req->requesttype & USB_RT_PORT) &&
  597. le16_to_cpu(req->index) > CONFIG_SYS_USB_XHCI_MAX_ROOT_PORTS) {
  598. printf("The request port(%d) is not configured\n",
  599. le16_to_cpu(req->index) - 1);
  600. return -EINVAL;
  601. }
  602. status_reg = (volatile uint32_t *)
  603. (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc);
  604. srclen = 0;
  605. typeReq = req->request | req->requesttype << 8;
  606. switch (typeReq) {
  607. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  608. switch (le16_to_cpu(req->value) >> 8) {
  609. case USB_DT_DEVICE:
  610. debug("USB_DT_DEVICE request\n");
  611. srcptr = &descriptor.device;
  612. srclen = 0x12;
  613. break;
  614. case USB_DT_CONFIG:
  615. debug("USB_DT_CONFIG config\n");
  616. srcptr = &descriptor.config;
  617. srclen = 0x19;
  618. break;
  619. case USB_DT_STRING:
  620. debug("USB_DT_STRING config\n");
  621. switch (le16_to_cpu(req->value) & 0xff) {
  622. case 0: /* Language */
  623. srcptr = "\4\3\11\4";
  624. srclen = 4;
  625. break;
  626. case 1: /* Vendor String */
  627. srcptr = "\16\3U\0-\0B\0o\0o\0t\0";
  628. srclen = 14;
  629. break;
  630. case 2: /* Product Name */
  631. srcptr = "\52\3X\0H\0C\0I\0 "
  632. "\0H\0o\0s\0t\0 "
  633. "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
  634. srclen = 42;
  635. break;
  636. default:
  637. printf("unknown value DT_STRING %x\n",
  638. le16_to_cpu(req->value));
  639. goto unknown;
  640. }
  641. break;
  642. default:
  643. printf("unknown value %x\n", le16_to_cpu(req->value));
  644. goto unknown;
  645. }
  646. break;
  647. case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
  648. switch (le16_to_cpu(req->value) >> 8) {
  649. case USB_DT_HUB:
  650. debug("USB_DT_HUB config\n");
  651. srcptr = &descriptor.hub;
  652. srclen = 0x8;
  653. break;
  654. default:
  655. printf("unknown value %x\n", le16_to_cpu(req->value));
  656. goto unknown;
  657. }
  658. break;
  659. case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
  660. debug("USB_REQ_SET_ADDRESS\n");
  661. ctrl->rootdev = le16_to_cpu(req->value);
  662. break;
  663. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  664. /* Do nothing */
  665. break;
  666. case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
  667. tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
  668. tmpbuf[1] = 0;
  669. srcptr = tmpbuf;
  670. srclen = 2;
  671. break;
  672. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  673. memset(tmpbuf, 0, 4);
  674. reg = xhci_readl(status_reg);
  675. if (reg & PORT_CONNECT) {
  676. tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
  677. switch (reg & DEV_SPEED_MASK) {
  678. case XDEV_FS:
  679. debug("SPEED = FULLSPEED\n");
  680. break;
  681. case XDEV_LS:
  682. debug("SPEED = LOWSPEED\n");
  683. tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
  684. break;
  685. case XDEV_HS:
  686. debug("SPEED = HIGHSPEED\n");
  687. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  688. break;
  689. case XDEV_SS:
  690. debug("SPEED = SUPERSPEED\n");
  691. tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8;
  692. break;
  693. }
  694. }
  695. if (reg & PORT_PE)
  696. tmpbuf[0] |= USB_PORT_STAT_ENABLE;
  697. if ((reg & PORT_PLS_MASK) == XDEV_U3)
  698. tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
  699. if (reg & PORT_OC)
  700. tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
  701. if (reg & PORT_RESET)
  702. tmpbuf[0] |= USB_PORT_STAT_RESET;
  703. if (reg & PORT_POWER)
  704. /*
  705. * XXX: This Port power bit (for USB 3.0 hub)
  706. * we are faking in USB 2.0 hub port status;
  707. * since there's a change in bit positions in
  708. * two:
  709. * USB 2.0 port status PP is at position[8]
  710. * USB 3.0 port status PP is at position[9]
  711. * So, we are still keeping it at position [8]
  712. */
  713. tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
  714. if (reg & PORT_CSC)
  715. tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
  716. if (reg & PORT_PEC)
  717. tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
  718. if (reg & PORT_OCC)
  719. tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
  720. if (reg & PORT_RC)
  721. tmpbuf[2] |= USB_PORT_STAT_C_RESET;
  722. srcptr = tmpbuf;
  723. srclen = 4;
  724. break;
  725. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  726. reg = xhci_readl(status_reg);
  727. reg = xhci_port_state_to_neutral(reg);
  728. switch (le16_to_cpu(req->value)) {
  729. case USB_PORT_FEAT_ENABLE:
  730. reg |= PORT_PE;
  731. xhci_writel(status_reg, reg);
  732. break;
  733. case USB_PORT_FEAT_POWER:
  734. reg |= PORT_POWER;
  735. xhci_writel(status_reg, reg);
  736. break;
  737. case USB_PORT_FEAT_RESET:
  738. reg |= PORT_RESET;
  739. xhci_writel(status_reg, reg);
  740. break;
  741. default:
  742. printf("unknown feature %x\n", le16_to_cpu(req->value));
  743. goto unknown;
  744. }
  745. break;
  746. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  747. reg = xhci_readl(status_reg);
  748. reg = xhci_port_state_to_neutral(reg);
  749. switch (le16_to_cpu(req->value)) {
  750. case USB_PORT_FEAT_ENABLE:
  751. reg &= ~PORT_PE;
  752. break;
  753. case USB_PORT_FEAT_POWER:
  754. reg &= ~PORT_POWER;
  755. break;
  756. case USB_PORT_FEAT_C_RESET:
  757. case USB_PORT_FEAT_C_CONNECTION:
  758. case USB_PORT_FEAT_C_OVER_CURRENT:
  759. case USB_PORT_FEAT_C_ENABLE:
  760. xhci_clear_port_change_bit((le16_to_cpu(req->value)),
  761. le16_to_cpu(req->index),
  762. status_reg, reg);
  763. break;
  764. default:
  765. printf("unknown feature %x\n", le16_to_cpu(req->value));
  766. goto unknown;
  767. }
  768. xhci_writel(status_reg, reg);
  769. break;
  770. default:
  771. puts("Unknown request\n");
  772. goto unknown;
  773. }
  774. debug("scrlen = %d\n req->length = %d\n",
  775. srclen, le16_to_cpu(req->length));
  776. len = min(srclen, (int)le16_to_cpu(req->length));
  777. if (srcptr != NULL && len > 0)
  778. memcpy(buffer, srcptr, len);
  779. else
  780. debug("Len is 0\n");
  781. udev->act_len = len;
  782. udev->status = 0;
  783. return 0;
  784. unknown:
  785. udev->act_len = 0;
  786. udev->status = USB_ST_STALLED;
  787. return -ENODEV;
  788. }
  789. /**
  790. * Submits the INT request to XHCI Host cotroller
  791. *
  792. * @param udev pointer to the USB device
  793. * @param pipe contains the DIR_IN or OUT , devnum
  794. * @param buffer buffer to be read/written based on the request
  795. * @param length length of the buffer
  796. * @param interval interval of the interrupt
  797. * @return 0
  798. */
  799. static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe,
  800. void *buffer, int length, int interval)
  801. {
  802. /*
  803. * TODO: Not addressing any interrupt type transfer requests
  804. * Add support for it later.
  805. */
  806. return -EINVAL;
  807. }
  808. /**
  809. * submit the BULK type of request to the USB Device
  810. *
  811. * @param udev pointer to the USB device
  812. * @param pipe contains the DIR_IN or OUT , devnum
  813. * @param buffer buffer to be read/written based on the request
  814. * @param length length of the buffer
  815. * @return returns 0 if successful else -1 on failure
  816. */
  817. static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe,
  818. void *buffer, int length)
  819. {
  820. if (usb_pipetype(pipe) != PIPE_BULK) {
  821. printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
  822. return -EINVAL;
  823. }
  824. return xhci_bulk_tx(udev, pipe, length, buffer);
  825. }
  826. /**
  827. * submit the control type of request to the Root hub/Device based on the devnum
  828. *
  829. * @param udev pointer to the USB device
  830. * @param pipe contains the DIR_IN or OUT , devnum
  831. * @param buffer buffer to be read/written based on the request
  832. * @param length length of the buffer
  833. * @param setup Request type
  834. * @param root_portnr Root port number that this device is on
  835. * @return returns 0 if successful else -1 on failure
  836. */
  837. static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe,
  838. void *buffer, int length,
  839. struct devrequest *setup, int root_portnr)
  840. {
  841. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  842. int ret = 0;
  843. if (usb_pipetype(pipe) != PIPE_CONTROL) {
  844. printf("non-control pipe (type=%lu)", usb_pipetype(pipe));
  845. return -EINVAL;
  846. }
  847. if (usb_pipedevice(pipe) == ctrl->rootdev)
  848. return xhci_submit_root(udev, pipe, buffer, setup);
  849. if (setup->request == USB_REQ_SET_ADDRESS)
  850. return xhci_address_device(udev, root_portnr);
  851. if (setup->request == USB_REQ_SET_CONFIGURATION) {
  852. ret = xhci_set_configuration(udev);
  853. if (ret) {
  854. puts("Failed to configure xHCI endpoint\n");
  855. return ret;
  856. }
  857. }
  858. return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
  859. }
  860. static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
  861. {
  862. struct xhci_hccr *hccr;
  863. struct xhci_hcor *hcor;
  864. uint32_t val;
  865. uint32_t val2;
  866. uint32_t reg;
  867. hccr = ctrl->hccr;
  868. hcor = ctrl->hcor;
  869. /*
  870. * Program the Number of Device Slots Enabled field in the CONFIG
  871. * register with the max value of slots the HC can handle.
  872. */
  873. val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
  874. val2 = xhci_readl(&hcor->or_config);
  875. val |= (val2 & ~HCS_SLOTS_MASK);
  876. xhci_writel(&hcor->or_config, val);
  877. /* initializing xhci data structures */
  878. if (xhci_mem_init(ctrl, hccr, hcor) < 0)
  879. return -ENOMEM;
  880. reg = xhci_readl(&hccr->cr_hcsparams1);
  881. descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
  882. HCS_MAX_PORTS_SHIFT);
  883. printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  884. /* Port Indicators */
  885. reg = xhci_readl(&hccr->cr_hccparams);
  886. if (HCS_INDICATOR(reg))
  887. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  888. | 0x80, &descriptor.hub.wHubCharacteristics);
  889. /* Port Power Control */
  890. if (HCC_PPC(reg))
  891. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  892. | 0x01, &descriptor.hub.wHubCharacteristics);
  893. if (xhci_start(hcor)) {
  894. xhci_reset(hcor);
  895. return -ENODEV;
  896. }
  897. /* Zero'ing IRQ control register and IRQ pending register */
  898. xhci_writel(&ctrl->ir_set->irq_control, 0x0);
  899. xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
  900. reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
  901. printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
  902. return 0;
  903. }
  904. static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
  905. {
  906. u32 temp;
  907. xhci_reset(ctrl->hcor);
  908. debug("// Disabling event ring interrupts\n");
  909. temp = xhci_readl(&ctrl->hcor->or_usbsts);
  910. xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
  911. temp = xhci_readl(&ctrl->ir_set->irq_pending);
  912. xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
  913. return 0;
  914. }
  915. #ifndef CONFIG_DM_USB
  916. int submit_control_msg(struct usb_device *udev, unsigned long pipe,
  917. void *buffer, int length, struct devrequest *setup)
  918. {
  919. struct usb_device *hop = udev;
  920. if (hop->parent)
  921. while (hop->parent->parent)
  922. hop = hop->parent;
  923. return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
  924. hop->portnr);
  925. }
  926. int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  927. int length)
  928. {
  929. return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
  930. }
  931. int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  932. int length, int interval)
  933. {
  934. return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
  935. }
  936. /**
  937. * Intialises the XHCI host controller
  938. * and allocates the necessary data structures
  939. *
  940. * @param index index to the host controller data structure
  941. * @return pointer to the intialised controller
  942. */
  943. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  944. {
  945. struct xhci_hccr *hccr;
  946. struct xhci_hcor *hcor;
  947. struct xhci_ctrl *ctrl;
  948. int ret;
  949. *controller = NULL;
  950. if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0)
  951. return -ENODEV;
  952. if (xhci_reset(hcor) != 0)
  953. return -ENODEV;
  954. ctrl = &xhcic[index];
  955. ctrl->hccr = hccr;
  956. ctrl->hcor = hcor;
  957. ret = xhci_lowlevel_init(ctrl);
  958. if (ret) {
  959. ctrl->hccr = NULL;
  960. ctrl->hcor = NULL;
  961. } else {
  962. *controller = &xhcic[index];
  963. }
  964. return ret;
  965. }
  966. /**
  967. * Stops the XHCI host controller
  968. * and cleans up all the related data structures
  969. *
  970. * @param index index to the host controller data structure
  971. * @return none
  972. */
  973. int usb_lowlevel_stop(int index)
  974. {
  975. struct xhci_ctrl *ctrl = (xhcic + index);
  976. if (ctrl->hcor) {
  977. xhci_lowlevel_stop(ctrl);
  978. xhci_hcd_stop(index);
  979. xhci_cleanup(ctrl);
  980. }
  981. return 0;
  982. }
  983. #endif /* CONFIG_DM_USB */
  984. #ifdef CONFIG_DM_USB
  985. /*
  986. static struct usb_device *get_usb_device(struct udevice *dev)
  987. {
  988. struct usb_device *udev;
  989. if (device_get_uclass_id(dev) == UCLASS_USB)
  990. udev = dev_get_uclass_priv(dev);
  991. else
  992. udev = dev_get_parent_priv(dev);
  993. return udev;
  994. }
  995. */
  996. static bool is_root_hub(struct udevice *dev)
  997. {
  998. if (device_get_uclass_id(dev->parent) != UCLASS_USB_HUB)
  999. return true;
  1000. return false;
  1001. }
  1002. static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
  1003. unsigned long pipe, void *buffer, int length,
  1004. struct devrequest *setup)
  1005. {
  1006. struct usb_device *uhop;
  1007. struct udevice *hub;
  1008. int root_portnr = 0;
  1009. debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
  1010. dev->name, udev, udev->dev->name, udev->portnr);
  1011. hub = udev->dev;
  1012. if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
  1013. /* Figure out our port number on the root hub */
  1014. if (is_root_hub(hub)) {
  1015. root_portnr = udev->portnr;
  1016. } else {
  1017. while (!is_root_hub(hub->parent))
  1018. hub = hub->parent;
  1019. uhop = dev_get_parent_priv(hub);
  1020. root_portnr = uhop->portnr;
  1021. }
  1022. }
  1023. /*
  1024. struct usb_device *hop = udev;
  1025. if (hop->parent)
  1026. while (hop->parent->parent)
  1027. hop = hop->parent;
  1028. */
  1029. return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
  1030. root_portnr);
  1031. }
  1032. static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
  1033. unsigned long pipe, void *buffer, int length)
  1034. {
  1035. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1036. return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
  1037. }
  1038. static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
  1039. unsigned long pipe, void *buffer, int length,
  1040. int interval)
  1041. {
  1042. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1043. return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
  1044. }
  1045. static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
  1046. {
  1047. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1048. return _xhci_alloc_device(udev);
  1049. }
  1050. int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
  1051. struct xhci_hcor *hcor)
  1052. {
  1053. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1054. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  1055. int ret;
  1056. debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
  1057. ctrl, hccr, hcor);
  1058. ctrl->dev = dev;
  1059. /*
  1060. * XHCI needs to issue a Address device command to setup
  1061. * proper device context structures, before it can interact
  1062. * with the device. So a get_descriptor will fail before any
  1063. * of that is done for XHCI unlike EHCI.
  1064. */
  1065. priv->desc_before_addr = false;
  1066. ret = xhci_reset(hcor);
  1067. if (ret)
  1068. goto err;
  1069. ctrl->hccr = hccr;
  1070. ctrl->hcor = hcor;
  1071. ret = xhci_lowlevel_init(ctrl);
  1072. if (ret)
  1073. goto err;
  1074. return 0;
  1075. err:
  1076. free(ctrl);
  1077. debug("%s: failed, ret=%d\n", __func__, ret);
  1078. return ret;
  1079. }
  1080. int xhci_deregister(struct udevice *dev)
  1081. {
  1082. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1083. xhci_lowlevel_stop(ctrl);
  1084. xhci_cleanup(ctrl);
  1085. return 0;
  1086. }
  1087. struct dm_usb_ops xhci_usb_ops = {
  1088. .control = xhci_submit_control_msg,
  1089. .bulk = xhci_submit_bulk_msg,
  1090. .interrupt = xhci_submit_int_msg,
  1091. .alloc_device = xhci_alloc_device,
  1092. };
  1093. #endif