xhci.c 28 KB

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