xhci.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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 <linux/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. (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  851. return xhci_address_device(udev, root_portnr);
  852. if (setup->request == USB_REQ_SET_CONFIGURATION &&
  853. (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  854. ret = xhci_set_configuration(udev);
  855. if (ret) {
  856. puts("Failed to configure xHCI endpoint\n");
  857. return ret;
  858. }
  859. }
  860. return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
  861. }
  862. static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
  863. {
  864. struct xhci_hccr *hccr;
  865. struct xhci_hcor *hcor;
  866. uint32_t val;
  867. uint32_t val2;
  868. uint32_t reg;
  869. hccr = ctrl->hccr;
  870. hcor = ctrl->hcor;
  871. /*
  872. * Program the Number of Device Slots Enabled field in the CONFIG
  873. * register with the max value of slots the HC can handle.
  874. */
  875. val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
  876. val2 = xhci_readl(&hcor->or_config);
  877. val |= (val2 & ~HCS_SLOTS_MASK);
  878. xhci_writel(&hcor->or_config, val);
  879. /* initializing xhci data structures */
  880. if (xhci_mem_init(ctrl, hccr, hcor) < 0)
  881. return -ENOMEM;
  882. reg = xhci_readl(&hccr->cr_hcsparams1);
  883. descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
  884. HCS_MAX_PORTS_SHIFT);
  885. printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  886. /* Port Indicators */
  887. reg = xhci_readl(&hccr->cr_hccparams);
  888. if (HCS_INDICATOR(reg))
  889. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  890. | 0x80, &descriptor.hub.wHubCharacteristics);
  891. /* Port Power Control */
  892. if (HCC_PPC(reg))
  893. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  894. | 0x01, &descriptor.hub.wHubCharacteristics);
  895. if (xhci_start(hcor)) {
  896. xhci_reset(hcor);
  897. return -ENODEV;
  898. }
  899. /* Zero'ing IRQ control register and IRQ pending register */
  900. xhci_writel(&ctrl->ir_set->irq_control, 0x0);
  901. xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
  902. reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
  903. printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
  904. return 0;
  905. }
  906. static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
  907. {
  908. u32 temp;
  909. xhci_reset(ctrl->hcor);
  910. debug("// Disabling event ring interrupts\n");
  911. temp = xhci_readl(&ctrl->hcor->or_usbsts);
  912. xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
  913. temp = xhci_readl(&ctrl->ir_set->irq_pending);
  914. xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
  915. return 0;
  916. }
  917. #ifndef CONFIG_DM_USB
  918. int submit_control_msg(struct usb_device *udev, unsigned long pipe,
  919. void *buffer, int length, struct devrequest *setup)
  920. {
  921. struct usb_device *hop = udev;
  922. if (hop->parent)
  923. while (hop->parent->parent)
  924. hop = hop->parent;
  925. return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
  926. hop->portnr);
  927. }
  928. int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  929. int length)
  930. {
  931. return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
  932. }
  933. int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  934. int length, int interval)
  935. {
  936. return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
  937. }
  938. /**
  939. * Intialises the XHCI host controller
  940. * and allocates the necessary data structures
  941. *
  942. * @param index index to the host controller data structure
  943. * @return pointer to the intialised controller
  944. */
  945. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  946. {
  947. struct xhci_hccr *hccr;
  948. struct xhci_hcor *hcor;
  949. struct xhci_ctrl *ctrl;
  950. int ret;
  951. *controller = NULL;
  952. if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0)
  953. return -ENODEV;
  954. if (xhci_reset(hcor) != 0)
  955. return -ENODEV;
  956. ctrl = &xhcic[index];
  957. ctrl->hccr = hccr;
  958. ctrl->hcor = hcor;
  959. ret = xhci_lowlevel_init(ctrl);
  960. if (ret) {
  961. ctrl->hccr = NULL;
  962. ctrl->hcor = NULL;
  963. } else {
  964. *controller = &xhcic[index];
  965. }
  966. return ret;
  967. }
  968. /**
  969. * Stops the XHCI host controller
  970. * and cleans up all the related data structures
  971. *
  972. * @param index index to the host controller data structure
  973. * @return none
  974. */
  975. int usb_lowlevel_stop(int index)
  976. {
  977. struct xhci_ctrl *ctrl = (xhcic + index);
  978. if (ctrl->hcor) {
  979. xhci_lowlevel_stop(ctrl);
  980. xhci_hcd_stop(index);
  981. xhci_cleanup(ctrl);
  982. }
  983. return 0;
  984. }
  985. #endif /* CONFIG_DM_USB */
  986. #ifdef CONFIG_DM_USB
  987. /*
  988. static struct usb_device *get_usb_device(struct udevice *dev)
  989. {
  990. struct usb_device *udev;
  991. if (device_get_uclass_id(dev) == UCLASS_USB)
  992. udev = dev_get_uclass_priv(dev);
  993. else
  994. udev = dev_get_parent_priv(dev);
  995. return udev;
  996. }
  997. */
  998. static bool is_root_hub(struct udevice *dev)
  999. {
  1000. if (device_get_uclass_id(dev->parent) != UCLASS_USB_HUB)
  1001. return true;
  1002. return false;
  1003. }
  1004. static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
  1005. unsigned long pipe, void *buffer, int length,
  1006. struct devrequest *setup)
  1007. {
  1008. struct usb_device *uhop;
  1009. struct udevice *hub;
  1010. int root_portnr = 0;
  1011. debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
  1012. dev->name, udev, udev->dev->name, udev->portnr);
  1013. hub = udev->dev;
  1014. if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
  1015. /* Figure out our port number on the root hub */
  1016. if (is_root_hub(hub)) {
  1017. root_portnr = udev->portnr;
  1018. } else {
  1019. while (!is_root_hub(hub->parent))
  1020. hub = hub->parent;
  1021. uhop = dev_get_parent_priv(hub);
  1022. root_portnr = uhop->portnr;
  1023. }
  1024. }
  1025. /*
  1026. struct usb_device *hop = udev;
  1027. if (hop->parent)
  1028. while (hop->parent->parent)
  1029. hop = hop->parent;
  1030. */
  1031. return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
  1032. root_portnr);
  1033. }
  1034. static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
  1035. unsigned long pipe, void *buffer, int length)
  1036. {
  1037. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1038. return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
  1039. }
  1040. static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
  1041. unsigned long pipe, void *buffer, int length,
  1042. int interval)
  1043. {
  1044. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1045. return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
  1046. }
  1047. static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
  1048. {
  1049. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1050. return _xhci_alloc_device(udev);
  1051. }
  1052. int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
  1053. struct xhci_hcor *hcor)
  1054. {
  1055. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1056. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  1057. int ret;
  1058. debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
  1059. ctrl, hccr, hcor);
  1060. ctrl->dev = dev;
  1061. /*
  1062. * XHCI needs to issue a Address device command to setup
  1063. * proper device context structures, before it can interact
  1064. * with the device. So a get_descriptor will fail before any
  1065. * of that is done for XHCI unlike EHCI.
  1066. */
  1067. priv->desc_before_addr = false;
  1068. ret = xhci_reset(hcor);
  1069. if (ret)
  1070. goto err;
  1071. ctrl->hccr = hccr;
  1072. ctrl->hcor = hcor;
  1073. ret = xhci_lowlevel_init(ctrl);
  1074. if (ret)
  1075. goto err;
  1076. return 0;
  1077. err:
  1078. free(ctrl);
  1079. debug("%s: failed, ret=%d\n", __func__, ret);
  1080. return ret;
  1081. }
  1082. int xhci_deregister(struct udevice *dev)
  1083. {
  1084. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1085. xhci_lowlevel_stop(ctrl);
  1086. xhci_cleanup(ctrl);
  1087. return 0;
  1088. }
  1089. struct dm_usb_ops xhci_usb_ops = {
  1090. .control = xhci_submit_control_msg,
  1091. .bulk = xhci_submit_bulk_msg,
  1092. .interrupt = xhci_submit_int_msg,
  1093. .alloc_device = xhci_alloc_device,
  1094. };
  1095. #endif