xhci.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * USB HOST XHCI Controller stack
  4. *
  5. * Based on xHCI host controller driver in linux-kernel
  6. * by Sarah Sharp.
  7. *
  8. * Copyright (C) 2008 Intel Corp.
  9. * Author: Sarah Sharp
  10. *
  11. * Copyright (C) 2013 Samsung Electronics Co.Ltd
  12. * Authors: Vivek Gautam <gautam.vivek@samsung.com>
  13. * Vikas Sajjan <vikas.sajjan@samsung.com>
  14. */
  15. /**
  16. * This file gives the xhci stack for usb3.0 looking into
  17. * xhci specification Rev1.0 (5/21/10).
  18. * The quirk devices support hasn't been given yet.
  19. */
  20. #include <common.h>
  21. #include <cpu_func.h>
  22. #include <dm.h>
  23. #include <dm/device_compat.h>
  24. #include <log.h>
  25. #include <malloc.h>
  26. #include <usb.h>
  27. #include <usb/xhci.h>
  28. #include <watchdog.h>
  29. #include <asm/byteorder.h>
  30. #include <asm/cache.h>
  31. #include <asm/unaligned.h>
  32. #include <linux/bitops.h>
  33. #include <linux/bug.h>
  34. #include <linux/delay.h>
  35. #include <linux/errno.h>
  36. #include <linux/iopoll.h>
  37. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  38. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  39. #endif
  40. static struct descriptor {
  41. struct usb_hub_descriptor hub;
  42. struct usb_device_descriptor device;
  43. struct usb_config_descriptor config;
  44. struct usb_interface_descriptor interface;
  45. struct usb_endpoint_descriptor endpoint;
  46. struct usb_ss_ep_comp_descriptor ep_companion;
  47. } __attribute__ ((packed)) descriptor = {
  48. {
  49. 0xc, /* bDescLength */
  50. 0x2a, /* bDescriptorType: hub descriptor */
  51. 2, /* bNrPorts -- runtime modified */
  52. cpu_to_le16(0x8), /* wHubCharacteristics */
  53. 10, /* bPwrOn2PwrGood */
  54. 0, /* bHubCntrCurrent */
  55. { /* Device removable */
  56. } /* at most 7 ports! XXX */
  57. },
  58. {
  59. 0x12, /* bLength */
  60. 1, /* bDescriptorType: UDESC_DEVICE */
  61. cpu_to_le16(0x0300), /* bcdUSB: v3.0 */
  62. 9, /* bDeviceClass: UDCLASS_HUB */
  63. 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
  64. 3, /* bDeviceProtocol: UDPROTO_SSHUBSTT */
  65. 9, /* bMaxPacketSize: 512 bytes 2^9 */
  66. 0x0000, /* idVendor */
  67. 0x0000, /* idProduct */
  68. cpu_to_le16(0x0100), /* bcdDevice */
  69. 1, /* iManufacturer */
  70. 2, /* iProduct */
  71. 0, /* iSerialNumber */
  72. 1 /* bNumConfigurations: 1 */
  73. },
  74. {
  75. 0x9,
  76. 2, /* bDescriptorType: UDESC_CONFIG */
  77. cpu_to_le16(0x1f), /* includes SS endpoint descriptor */
  78. 1, /* bNumInterface */
  79. 1, /* bConfigurationValue */
  80. 0, /* iConfiguration */
  81. 0x40, /* bmAttributes: UC_SELF_POWER */
  82. 0 /* bMaxPower */
  83. },
  84. {
  85. 0x9, /* bLength */
  86. 4, /* bDescriptorType: UDESC_INTERFACE */
  87. 0, /* bInterfaceNumber */
  88. 0, /* bAlternateSetting */
  89. 1, /* bNumEndpoints */
  90. 9, /* bInterfaceClass: UICLASS_HUB */
  91. 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
  92. 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
  93. 0 /* iInterface */
  94. },
  95. {
  96. 0x7, /* bLength */
  97. 5, /* bDescriptorType: UDESC_ENDPOINT */
  98. 0x81, /* bEndpointAddress: IN endpoint 1 */
  99. 3, /* bmAttributes: UE_INTERRUPT */
  100. 8, /* wMaxPacketSize */
  101. 255 /* bInterval */
  102. },
  103. {
  104. 0x06, /* ss_bLength */
  105. 0x30, /* ss_bDescriptorType: SS EP Companion */
  106. 0x00, /* ss_bMaxBurst: allows 1 TX between ACKs */
  107. /* ss_bmAttributes: 1 packet per service interval */
  108. 0x00,
  109. /* ss_wBytesPerInterval: 15 bits for max 15 ports */
  110. cpu_to_le16(0x02),
  111. },
  112. };
  113. #if !CONFIG_IS_ENABLED(DM_USB)
  114. static struct xhci_ctrl xhcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
  115. #endif
  116. struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev)
  117. {
  118. #if CONFIG_IS_ENABLED(DM_USB)
  119. struct udevice *dev;
  120. /* Find the USB controller */
  121. for (dev = udev->dev;
  122. device_get_uclass_id(dev) != UCLASS_USB;
  123. dev = dev->parent)
  124. ;
  125. return dev_get_priv(dev);
  126. #else
  127. return udev->controller;
  128. #endif
  129. }
  130. /**
  131. * Waits for as per specified amount of time
  132. * for the "result" to match with "done"
  133. *
  134. * @param ptr pointer to the register to be read
  135. * @param mask mask for the value read
  136. * @param done value to be campared with result
  137. * @param usec time to wait till
  138. * @return 0 if handshake is success else < 0 on failure
  139. */
  140. static int
  141. handshake(uint32_t volatile *ptr, uint32_t mask, uint32_t done, int usec)
  142. {
  143. uint32_t result;
  144. int ret;
  145. ret = readx_poll_sleep_timeout(xhci_readl, ptr, result,
  146. (result & mask) == done || result == U32_MAX,
  147. 1, usec);
  148. if (result == U32_MAX) /* card removed */
  149. return -ENODEV;
  150. return ret;
  151. }
  152. /**
  153. * Set the run bit and wait for the host to be running.
  154. *
  155. * @param hcor pointer to host controller operation registers
  156. * @return status of the Handshake
  157. */
  158. static int xhci_start(struct xhci_hcor *hcor)
  159. {
  160. u32 temp;
  161. int ret;
  162. puts("Starting the controller\n");
  163. temp = xhci_readl(&hcor->or_usbcmd);
  164. temp |= (CMD_RUN);
  165. xhci_writel(&hcor->or_usbcmd, temp);
  166. /*
  167. * Wait for the HCHalted Status bit to be 0 to indicate the host is
  168. * running.
  169. */
  170. ret = handshake(&hcor->or_usbsts, STS_HALT, 0, XHCI_MAX_HALT_USEC);
  171. if (ret)
  172. debug("Host took too long to start, "
  173. "waited %u microseconds.\n",
  174. XHCI_MAX_HALT_USEC);
  175. return ret;
  176. }
  177. /**
  178. * Resets the XHCI Controller
  179. *
  180. * @param hcor pointer to host controller operation registers
  181. * @return -EBUSY if XHCI Controller is not halted else status of handshake
  182. */
  183. static int xhci_reset(struct xhci_hcor *hcor)
  184. {
  185. u32 cmd;
  186. u32 state;
  187. int ret;
  188. /* Halting the Host first */
  189. debug("// Halt the HC: %p\n", hcor);
  190. state = xhci_readl(&hcor->or_usbsts) & STS_HALT;
  191. if (!state) {
  192. cmd = xhci_readl(&hcor->or_usbcmd);
  193. cmd &= ~CMD_RUN;
  194. xhci_writel(&hcor->or_usbcmd, cmd);
  195. }
  196. ret = handshake(&hcor->or_usbsts,
  197. STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
  198. if (ret) {
  199. printf("Host not halted after %u microseconds.\n",
  200. XHCI_MAX_HALT_USEC);
  201. return -EBUSY;
  202. }
  203. debug("// Reset the HC\n");
  204. cmd = xhci_readl(&hcor->or_usbcmd);
  205. cmd |= CMD_RESET;
  206. xhci_writel(&hcor->or_usbcmd, cmd);
  207. ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC);
  208. if (ret)
  209. return ret;
  210. /*
  211. * xHCI cannot write to any doorbells or operational registers other
  212. * than status until the "Controller Not Ready" flag is cleared.
  213. */
  214. return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC);
  215. }
  216. /**
  217. * Used for passing endpoint bitmasks between the core and HCDs.
  218. * Find the index for an endpoint given its descriptor.
  219. * Use the return value to right shift 1 for the bitmask.
  220. *
  221. * Index = (epnum * 2) + direction - 1,
  222. * where direction = 0 for OUT, 1 for IN.
  223. * For control endpoints, the IN index is used (OUT index is unused), so
  224. * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
  225. *
  226. * @param desc USB enpdoint Descriptor
  227. * @return index of the Endpoint
  228. */
  229. static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc)
  230. {
  231. unsigned int index;
  232. if (usb_endpoint_xfer_control(desc))
  233. index = (unsigned int)(usb_endpoint_num(desc) * 2);
  234. else
  235. index = (unsigned int)((usb_endpoint_num(desc) * 2) -
  236. (usb_endpoint_dir_in(desc) ? 0 : 1));
  237. return index;
  238. }
  239. /*
  240. * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
  241. * microframes, rounded down to nearest power of 2.
  242. */
  243. static unsigned int xhci_microframes_to_exponent(unsigned int desc_interval,
  244. unsigned int min_exponent,
  245. unsigned int max_exponent)
  246. {
  247. unsigned int interval;
  248. interval = fls(desc_interval) - 1;
  249. interval = clamp_val(interval, min_exponent, max_exponent);
  250. if ((1 << interval) != desc_interval)
  251. debug("rounding interval to %d microframes, "\
  252. "ep desc says %d microframes\n",
  253. 1 << interval, desc_interval);
  254. return interval;
  255. }
  256. static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
  257. struct usb_endpoint_descriptor *endpt_desc)
  258. {
  259. if (endpt_desc->bInterval == 0)
  260. return 0;
  261. return xhci_microframes_to_exponent(endpt_desc->bInterval, 0, 15);
  262. }
  263. static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
  264. struct usb_endpoint_descriptor *endpt_desc)
  265. {
  266. return xhci_microframes_to_exponent(endpt_desc->bInterval * 8, 3, 10);
  267. }
  268. /*
  269. * Convert interval expressed as 2^(bInterval - 1) == interval into
  270. * straight exponent value 2^n == interval.
  271. */
  272. static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
  273. struct usb_endpoint_descriptor *endpt_desc)
  274. {
  275. unsigned int interval;
  276. interval = clamp_val(endpt_desc->bInterval, 1, 16) - 1;
  277. if (interval != endpt_desc->bInterval - 1)
  278. debug("ep %#x - rounding interval to %d %sframes\n",
  279. endpt_desc->bEndpointAddress, 1 << interval,
  280. udev->speed == USB_SPEED_FULL ? "" : "micro");
  281. if (udev->speed == USB_SPEED_FULL) {
  282. /*
  283. * Full speed isoc endpoints specify interval in frames,
  284. * not microframes. We are using microframes everywhere,
  285. * so adjust accordingly.
  286. */
  287. interval += 3; /* 1 frame = 2^3 uframes */
  288. }
  289. return interval;
  290. }
  291. /*
  292. * Return the polling or NAK interval.
  293. *
  294. * The polling interval is expressed in "microframes". If xHCI's Interval field
  295. * is set to N, it will service the endpoint every 2^(Interval)*125us.
  296. *
  297. * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
  298. * is set to 0.
  299. */
  300. static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
  301. struct usb_endpoint_descriptor *endpt_desc)
  302. {
  303. unsigned int interval = 0;
  304. switch (udev->speed) {
  305. case USB_SPEED_HIGH:
  306. /* Max NAK rate */
  307. if (usb_endpoint_xfer_control(endpt_desc) ||
  308. usb_endpoint_xfer_bulk(endpt_desc)) {
  309. interval = xhci_parse_microframe_interval(udev,
  310. endpt_desc);
  311. break;
  312. }
  313. /* Fall through - SS and HS isoc/int have same decoding */
  314. case USB_SPEED_SUPER:
  315. if (usb_endpoint_xfer_int(endpt_desc) ||
  316. usb_endpoint_xfer_isoc(endpt_desc)) {
  317. interval = xhci_parse_exponent_interval(udev,
  318. endpt_desc);
  319. }
  320. break;
  321. case USB_SPEED_FULL:
  322. if (usb_endpoint_xfer_isoc(endpt_desc)) {
  323. interval = xhci_parse_exponent_interval(udev,
  324. endpt_desc);
  325. break;
  326. }
  327. /*
  328. * Fall through for interrupt endpoint interval decoding
  329. * since it uses the same rules as low speed interrupt
  330. * endpoints.
  331. */
  332. case USB_SPEED_LOW:
  333. if (usb_endpoint_xfer_int(endpt_desc) ||
  334. usb_endpoint_xfer_isoc(endpt_desc)) {
  335. interval = xhci_parse_frame_interval(udev, endpt_desc);
  336. }
  337. break;
  338. default:
  339. BUG();
  340. }
  341. return interval;
  342. }
  343. /*
  344. * The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
  345. * High speed endpoint descriptors can define "the number of additional
  346. * transaction opportunities per microframe", but that goes in the Max Burst
  347. * endpoint context field.
  348. */
  349. static u32 xhci_get_endpoint_mult(struct usb_device *udev,
  350. struct usb_endpoint_descriptor *endpt_desc,
  351. struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
  352. {
  353. if (udev->speed < USB_SPEED_SUPER ||
  354. !usb_endpoint_xfer_isoc(endpt_desc))
  355. return 0;
  356. return ss_ep_comp_desc->bmAttributes;
  357. }
  358. static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
  359. struct usb_endpoint_descriptor *endpt_desc,
  360. struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
  361. {
  362. /* Super speed and Plus have max burst in ep companion desc */
  363. if (udev->speed >= USB_SPEED_SUPER)
  364. return ss_ep_comp_desc->bMaxBurst;
  365. if (udev->speed == USB_SPEED_HIGH &&
  366. (usb_endpoint_xfer_isoc(endpt_desc) ||
  367. usb_endpoint_xfer_int(endpt_desc)))
  368. return usb_endpoint_maxp_mult(endpt_desc) - 1;
  369. return 0;
  370. }
  371. /*
  372. * Return the maximum endpoint service interval time (ESIT) payload.
  373. * Basically, this is the maxpacket size, multiplied by the burst size
  374. * and mult size.
  375. */
  376. static u32 xhci_get_max_esit_payload(struct usb_device *udev,
  377. struct usb_endpoint_descriptor *endpt_desc,
  378. struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
  379. {
  380. int max_burst;
  381. int max_packet;
  382. /* Only applies for interrupt or isochronous endpoints */
  383. if (usb_endpoint_xfer_control(endpt_desc) ||
  384. usb_endpoint_xfer_bulk(endpt_desc))
  385. return 0;
  386. /* SuperSpeed Isoc ep with less than 48k per esit */
  387. if (udev->speed >= USB_SPEED_SUPER)
  388. return le16_to_cpu(ss_ep_comp_desc->wBytesPerInterval);
  389. max_packet = usb_endpoint_maxp(endpt_desc);
  390. max_burst = usb_endpoint_maxp_mult(endpt_desc);
  391. /* A 0 in max burst means 1 transfer per ESIT */
  392. return max_packet * max_burst;
  393. }
  394. /**
  395. * Issue a configure endpoint command or evaluate context command
  396. * and wait for it to finish.
  397. *
  398. * @param udev pointer to the Device Data Structure
  399. * @param ctx_change flag to indicate the Context has changed or NOT
  400. * @return 0 on success, -1 on failure
  401. */
  402. static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change)
  403. {
  404. struct xhci_container_ctx *in_ctx;
  405. struct xhci_virt_device *virt_dev;
  406. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  407. union xhci_trb *event;
  408. virt_dev = ctrl->devs[udev->slot_id];
  409. in_ctx = virt_dev->in_ctx;
  410. xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size);
  411. xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0,
  412. ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP);
  413. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  414. BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
  415. != udev->slot_id);
  416. switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
  417. case COMP_SUCCESS:
  418. debug("Successful %s command\n",
  419. ctx_change ? "Evaluate Context" : "Configure Endpoint");
  420. break;
  421. default:
  422. printf("ERROR: %s command returned completion code %d.\n",
  423. ctx_change ? "Evaluate Context" : "Configure Endpoint",
  424. GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
  425. return -EINVAL;
  426. }
  427. xhci_acknowledge_event(ctrl);
  428. return 0;
  429. }
  430. /**
  431. * Configure the endpoint, programming the device contexts.
  432. *
  433. * @param udev pointer to the USB device structure
  434. * @return returns the status of the xhci_configure_endpoints
  435. */
  436. static int xhci_set_configuration(struct usb_device *udev)
  437. {
  438. struct xhci_container_ctx *in_ctx;
  439. struct xhci_container_ctx *out_ctx;
  440. struct xhci_input_control_ctx *ctrl_ctx;
  441. struct xhci_slot_ctx *slot_ctx;
  442. struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM];
  443. int cur_ep;
  444. int max_ep_flag = 0;
  445. int ep_index;
  446. unsigned int dir;
  447. unsigned int ep_type;
  448. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  449. int num_of_ep;
  450. int ep_flag = 0;
  451. u64 trb_64 = 0;
  452. int slot_id = udev->slot_id;
  453. struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
  454. struct usb_interface *ifdesc;
  455. u32 max_esit_payload;
  456. unsigned int interval;
  457. unsigned int mult;
  458. unsigned int max_burst;
  459. unsigned int avg_trb_len;
  460. unsigned int err_count = 0;
  461. out_ctx = virt_dev->out_ctx;
  462. in_ctx = virt_dev->in_ctx;
  463. num_of_ep = udev->config.if_desc[0].no_of_ep;
  464. ifdesc = &udev->config.if_desc[0];
  465. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  466. /* Initialize the input context control */
  467. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
  468. ctrl_ctx->drop_flags = 0;
  469. /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
  470. for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
  471. ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
  472. ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
  473. if (max_ep_flag < ep_flag)
  474. max_ep_flag = ep_flag;
  475. }
  476. xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
  477. /* slot context */
  478. xhci_slot_copy(ctrl, in_ctx, out_ctx);
  479. slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
  480. slot_ctx->dev_info &= ~(cpu_to_le32(LAST_CTX_MASK));
  481. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0);
  482. xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0);
  483. /* filling up ep contexts */
  484. for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
  485. struct usb_endpoint_descriptor *endpt_desc = NULL;
  486. struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc = NULL;
  487. endpt_desc = &ifdesc->ep_desc[cur_ep];
  488. ss_ep_comp_desc = &ifdesc->ss_ep_comp_desc[cur_ep];
  489. trb_64 = 0;
  490. /*
  491. * Get values to fill the endpoint context, mostly from ep
  492. * descriptor. The average TRB buffer lengt for bulk endpoints
  493. * is unclear as we have no clue on scatter gather list entry
  494. * size. For Isoc and Int, set it to max available.
  495. * See xHCI 1.1 spec 4.14.1.1 for details.
  496. */
  497. max_esit_payload = xhci_get_max_esit_payload(udev, endpt_desc,
  498. ss_ep_comp_desc);
  499. interval = xhci_get_endpoint_interval(udev, endpt_desc);
  500. mult = xhci_get_endpoint_mult(udev, endpt_desc,
  501. ss_ep_comp_desc);
  502. max_burst = xhci_get_endpoint_max_burst(udev, endpt_desc,
  503. ss_ep_comp_desc);
  504. avg_trb_len = max_esit_payload;
  505. ep_index = xhci_get_ep_index(endpt_desc);
  506. ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
  507. /* Allocate the ep rings */
  508. virt_dev->eps[ep_index].ring = xhci_ring_alloc(ctrl, 1, true);
  509. if (!virt_dev->eps[ep_index].ring)
  510. return -ENOMEM;
  511. /*NOTE: ep_desc[0] actually represents EP1 and so on */
  512. dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7);
  513. ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2));
  514. ep_ctx[ep_index]->ep_info =
  515. cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
  516. EP_INTERVAL(interval) | EP_MULT(mult));
  517. ep_ctx[ep_index]->ep_info2 = cpu_to_le32(EP_TYPE(ep_type));
  518. ep_ctx[ep_index]->ep_info2 |=
  519. cpu_to_le32(MAX_PACKET
  520. (get_unaligned(&endpt_desc->wMaxPacketSize)));
  521. /* Allow 3 retries for everything but isoc, set CErr = 3 */
  522. if (!usb_endpoint_xfer_isoc(endpt_desc))
  523. err_count = 3;
  524. ep_ctx[ep_index]->ep_info2 |=
  525. cpu_to_le32(MAX_BURST(max_burst) |
  526. ERROR_COUNT(err_count));
  527. trb_64 = xhci_virt_to_bus(ctrl, virt_dev->eps[ep_index].ring->enqueue);
  528. ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 |
  529. virt_dev->eps[ep_index].ring->cycle_state);
  530. /*
  531. * xHCI spec 6.2.3:
  532. * 'Average TRB Length' should be 8 for control endpoints.
  533. */
  534. if (usb_endpoint_xfer_control(endpt_desc))
  535. avg_trb_len = 8;
  536. ep_ctx[ep_index]->tx_info =
  537. cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
  538. EP_AVG_TRB_LENGTH(avg_trb_len));
  539. /*
  540. * The MediaTek xHCI defines some extra SW parameters which
  541. * are put into reserved DWs in Slot and Endpoint Contexts
  542. * for synchronous endpoints.
  543. */
  544. if (ctrl->quirks & XHCI_MTK_HOST) {
  545. ep_ctx[ep_index]->reserved[0] =
  546. cpu_to_le32(EP_BPKTS(1) | EP_BBM(1));
  547. }
  548. }
  549. return xhci_configure_endpoints(udev, false);
  550. }
  551. /**
  552. * Issue an Address Device command (which will issue a SetAddress request to
  553. * the device).
  554. *
  555. * @param udev pointer to the Device Data Structure
  556. * @return 0 if successful else error code on failure
  557. */
  558. static int xhci_address_device(struct usb_device *udev, int root_portnr)
  559. {
  560. int ret = 0;
  561. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  562. struct xhci_slot_ctx *slot_ctx;
  563. struct xhci_input_control_ctx *ctrl_ctx;
  564. struct xhci_virt_device *virt_dev;
  565. int slot_id = udev->slot_id;
  566. union xhci_trb *event;
  567. virt_dev = ctrl->devs[slot_id];
  568. /*
  569. * This is the first Set Address since device plug-in
  570. * so setting up the slot context.
  571. */
  572. debug("Setting up addressable devices %p\n", ctrl->dcbaa);
  573. xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr);
  574. ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
  575. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
  576. ctrl_ctx->drop_flags = 0;
  577. xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV);
  578. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  579. BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
  580. switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
  581. case COMP_CTX_STATE:
  582. case COMP_EBADSLT:
  583. printf("Setup ERROR: address device command for slot %d.\n",
  584. slot_id);
  585. ret = -EINVAL;
  586. break;
  587. case COMP_TX_ERR:
  588. puts("Device not responding to set address.\n");
  589. ret = -EPROTO;
  590. break;
  591. case COMP_DEV_ERR:
  592. puts("ERROR: Incompatible device"
  593. "for address device command.\n");
  594. ret = -ENODEV;
  595. break;
  596. case COMP_SUCCESS:
  597. debug("Successful Address Device command\n");
  598. udev->status = 0;
  599. break;
  600. default:
  601. printf("ERROR: unexpected command completion code 0x%x.\n",
  602. GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
  603. ret = -EINVAL;
  604. break;
  605. }
  606. xhci_acknowledge_event(ctrl);
  607. if (ret < 0)
  608. /*
  609. * TODO: Unsuccessful Address Device command shall leave the
  610. * slot in default state. So, issue Disable Slot command now.
  611. */
  612. return ret;
  613. xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
  614. virt_dev->out_ctx->size);
  615. slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx);
  616. debug("xHC internal address is: %d\n",
  617. le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
  618. return 0;
  619. }
  620. /**
  621. * Issue Enable slot command to the controller to allocate
  622. * device slot and assign the slot id. It fails if the xHC
  623. * ran out of device slots, the Enable Slot command timed out,
  624. * or allocating memory failed.
  625. *
  626. * @param udev pointer to the Device Data Structure
  627. * @return Returns 0 on succes else return error code on failure
  628. */
  629. static int _xhci_alloc_device(struct usb_device *udev)
  630. {
  631. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  632. union xhci_trb *event;
  633. int ret;
  634. /*
  635. * Root hub will be first device to be initailized.
  636. * If this device is root-hub, don't do any xHC related
  637. * stuff.
  638. */
  639. if (ctrl->rootdev == 0) {
  640. udev->speed = USB_SPEED_SUPER;
  641. return 0;
  642. }
  643. xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT);
  644. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  645. BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
  646. != COMP_SUCCESS);
  647. udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags));
  648. xhci_acknowledge_event(ctrl);
  649. ret = xhci_alloc_virt_device(ctrl, udev->slot_id);
  650. if (ret < 0) {
  651. /*
  652. * TODO: Unsuccessful Address Device command shall leave
  653. * the slot in default. So, issue Disable Slot command now.
  654. */
  655. puts("Could not allocate xHCI USB device data structures\n");
  656. return ret;
  657. }
  658. return 0;
  659. }
  660. #if !CONFIG_IS_ENABLED(DM_USB)
  661. int usb_alloc_device(struct usb_device *udev)
  662. {
  663. return _xhci_alloc_device(udev);
  664. }
  665. #endif
  666. /*
  667. * Full speed devices may have a max packet size greater than 8 bytes, but the
  668. * USB core doesn't know that until it reads the first 8 bytes of the
  669. * descriptor. If the usb_device's max packet size changes after that point,
  670. * we need to issue an evaluate context command and wait on it.
  671. *
  672. * @param udev pointer to the Device Data Structure
  673. * @return returns the status of the xhci_configure_endpoints
  674. */
  675. int xhci_check_maxpacket(struct usb_device *udev)
  676. {
  677. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  678. unsigned int slot_id = udev->slot_id;
  679. int ep_index = 0; /* control endpoint */
  680. struct xhci_container_ctx *in_ctx;
  681. struct xhci_container_ctx *out_ctx;
  682. struct xhci_input_control_ctx *ctrl_ctx;
  683. struct xhci_ep_ctx *ep_ctx;
  684. int max_packet_size;
  685. int hw_max_packet_size;
  686. int ret = 0;
  687. out_ctx = ctrl->devs[slot_id]->out_ctx;
  688. xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
  689. ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
  690. hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  691. max_packet_size = udev->epmaxpacketin[0];
  692. if (hw_max_packet_size != max_packet_size) {
  693. debug("Max Packet Size for ep 0 changed.\n");
  694. debug("Max packet size in usb_device = %d\n", max_packet_size);
  695. debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size);
  696. debug("Issuing evaluate context command.\n");
  697. /* Set up the modified control endpoint 0 */
  698. xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx,
  699. ctrl->devs[slot_id]->out_ctx, ep_index);
  700. in_ctx = ctrl->devs[slot_id]->in_ctx;
  701. ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
  702. ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET(MAX_PACKET_MASK));
  703. ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
  704. /*
  705. * Set up the input context flags for the command
  706. * FIXME: This won't work if a non-default control endpoint
  707. * changes max packet sizes.
  708. */
  709. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  710. ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
  711. ctrl_ctx->drop_flags = 0;
  712. ret = xhci_configure_endpoints(udev, true);
  713. }
  714. return ret;
  715. }
  716. /**
  717. * Clears the Change bits of the Port Status Register
  718. *
  719. * @param wValue request value
  720. * @param wIndex request index
  721. * @param addr address of posrt status register
  722. * @param port_status state of port status register
  723. * @return none
  724. */
  725. static void xhci_clear_port_change_bit(u16 wValue,
  726. u16 wIndex, volatile uint32_t *addr, u32 port_status)
  727. {
  728. char *port_change_bit;
  729. u32 status;
  730. switch (wValue) {
  731. case USB_PORT_FEAT_C_RESET:
  732. status = PORT_RC;
  733. port_change_bit = "reset";
  734. break;
  735. case USB_PORT_FEAT_C_CONNECTION:
  736. status = PORT_CSC;
  737. port_change_bit = "connect";
  738. break;
  739. case USB_PORT_FEAT_C_OVER_CURRENT:
  740. status = PORT_OCC;
  741. port_change_bit = "over-current";
  742. break;
  743. case USB_PORT_FEAT_C_ENABLE:
  744. status = PORT_PEC;
  745. port_change_bit = "enable/disable";
  746. break;
  747. case USB_PORT_FEAT_C_SUSPEND:
  748. status = PORT_PLC;
  749. port_change_bit = "suspend/resume";
  750. break;
  751. default:
  752. /* Should never happen */
  753. return;
  754. }
  755. /* Change bits are all write 1 to clear */
  756. xhci_writel(addr, port_status | status);
  757. port_status = xhci_readl(addr);
  758. debug("clear port %s change, actual port %d status = 0x%x\n",
  759. port_change_bit, wIndex, port_status);
  760. }
  761. /**
  762. * Save Read Only (RO) bits and save read/write bits where
  763. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  764. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  765. *
  766. * @param state state of the Port Status and Control Regsiter
  767. * @return a value that would result in the port being in the
  768. * same state, if the value was written to the port
  769. * status control register.
  770. */
  771. static u32 xhci_port_state_to_neutral(u32 state)
  772. {
  773. /* Save read-only status and port state */
  774. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  775. }
  776. /**
  777. * Submits the Requests to the XHCI Host Controller
  778. *
  779. * @param udev pointer to the USB device structure
  780. * @param pipe contains the DIR_IN or OUT , devnum
  781. * @param buffer buffer to be read/written based on the request
  782. * @return returns 0 if successful else -1 on failure
  783. */
  784. static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
  785. void *buffer, struct devrequest *req)
  786. {
  787. uint8_t tmpbuf[4];
  788. u16 typeReq;
  789. void *srcptr = NULL;
  790. int len, srclen;
  791. uint32_t reg;
  792. volatile uint32_t *status_reg;
  793. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  794. struct xhci_hccr *hccr = ctrl->hccr;
  795. struct xhci_hcor *hcor = ctrl->hcor;
  796. int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1));
  797. if ((req->requesttype & USB_RT_PORT) &&
  798. le16_to_cpu(req->index) > max_ports) {
  799. printf("The request port(%d) exceeds maximum port number\n",
  800. le16_to_cpu(req->index) - 1);
  801. return -EINVAL;
  802. }
  803. status_reg = (volatile uint32_t *)
  804. (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc);
  805. srclen = 0;
  806. typeReq = req->request | req->requesttype << 8;
  807. switch (typeReq) {
  808. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  809. switch (le16_to_cpu(req->value) >> 8) {
  810. case USB_DT_DEVICE:
  811. debug("USB_DT_DEVICE request\n");
  812. srcptr = &descriptor.device;
  813. srclen = 0x12;
  814. break;
  815. case USB_DT_CONFIG:
  816. debug("USB_DT_CONFIG config\n");
  817. srcptr = &descriptor.config;
  818. srclen = 0x19;
  819. break;
  820. case USB_DT_STRING:
  821. debug("USB_DT_STRING config\n");
  822. switch (le16_to_cpu(req->value) & 0xff) {
  823. case 0: /* Language */
  824. srcptr = "\4\3\11\4";
  825. srclen = 4;
  826. break;
  827. case 1: /* Vendor String */
  828. srcptr = "\16\3U\0-\0B\0o\0o\0t\0";
  829. srclen = 14;
  830. break;
  831. case 2: /* Product Name */
  832. srcptr = "\52\3X\0H\0C\0I\0 "
  833. "\0H\0o\0s\0t\0 "
  834. "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
  835. srclen = 42;
  836. break;
  837. default:
  838. printf("unknown value DT_STRING %x\n",
  839. le16_to_cpu(req->value));
  840. goto unknown;
  841. }
  842. break;
  843. default:
  844. printf("unknown value %x\n", le16_to_cpu(req->value));
  845. goto unknown;
  846. }
  847. break;
  848. case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
  849. switch (le16_to_cpu(req->value) >> 8) {
  850. case USB_DT_HUB:
  851. case USB_DT_SS_HUB:
  852. debug("USB_DT_HUB config\n");
  853. srcptr = &descriptor.hub;
  854. srclen = 0x8;
  855. break;
  856. default:
  857. printf("unknown value %x\n", le16_to_cpu(req->value));
  858. goto unknown;
  859. }
  860. break;
  861. case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
  862. debug("USB_REQ_SET_ADDRESS\n");
  863. ctrl->rootdev = le16_to_cpu(req->value);
  864. break;
  865. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  866. /* Do nothing */
  867. break;
  868. case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
  869. tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
  870. tmpbuf[1] = 0;
  871. srcptr = tmpbuf;
  872. srclen = 2;
  873. break;
  874. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  875. memset(tmpbuf, 0, 4);
  876. reg = xhci_readl(status_reg);
  877. if (reg & PORT_CONNECT) {
  878. tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
  879. switch (reg & DEV_SPEED_MASK) {
  880. case XDEV_FS:
  881. debug("SPEED = FULLSPEED\n");
  882. break;
  883. case XDEV_LS:
  884. debug("SPEED = LOWSPEED\n");
  885. tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
  886. break;
  887. case XDEV_HS:
  888. debug("SPEED = HIGHSPEED\n");
  889. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  890. break;
  891. case XDEV_SS:
  892. debug("SPEED = SUPERSPEED\n");
  893. tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8;
  894. break;
  895. }
  896. }
  897. if (reg & PORT_PE)
  898. tmpbuf[0] |= USB_PORT_STAT_ENABLE;
  899. if ((reg & PORT_PLS_MASK) == XDEV_U3)
  900. tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
  901. if (reg & PORT_OC)
  902. tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
  903. if (reg & PORT_RESET)
  904. tmpbuf[0] |= USB_PORT_STAT_RESET;
  905. if (reg & PORT_POWER)
  906. /*
  907. * XXX: This Port power bit (for USB 3.0 hub)
  908. * we are faking in USB 2.0 hub port status;
  909. * since there's a change in bit positions in
  910. * two:
  911. * USB 2.0 port status PP is at position[8]
  912. * USB 3.0 port status PP is at position[9]
  913. * So, we are still keeping it at position [8]
  914. */
  915. tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
  916. if (reg & PORT_CSC)
  917. tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
  918. if (reg & PORT_PEC)
  919. tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
  920. if (reg & PORT_OCC)
  921. tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
  922. if (reg & PORT_RC)
  923. tmpbuf[2] |= USB_PORT_STAT_C_RESET;
  924. srcptr = tmpbuf;
  925. srclen = 4;
  926. break;
  927. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  928. reg = xhci_readl(status_reg);
  929. reg = xhci_port_state_to_neutral(reg);
  930. switch (le16_to_cpu(req->value)) {
  931. case USB_PORT_FEAT_ENABLE:
  932. reg |= PORT_PE;
  933. xhci_writel(status_reg, reg);
  934. break;
  935. case USB_PORT_FEAT_POWER:
  936. reg |= PORT_POWER;
  937. xhci_writel(status_reg, reg);
  938. break;
  939. case USB_PORT_FEAT_RESET:
  940. reg |= PORT_RESET;
  941. xhci_writel(status_reg, reg);
  942. break;
  943. default:
  944. printf("unknown feature %x\n", le16_to_cpu(req->value));
  945. goto unknown;
  946. }
  947. break;
  948. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  949. reg = xhci_readl(status_reg);
  950. reg = xhci_port_state_to_neutral(reg);
  951. switch (le16_to_cpu(req->value)) {
  952. case USB_PORT_FEAT_ENABLE:
  953. reg &= ~PORT_PE;
  954. break;
  955. case USB_PORT_FEAT_POWER:
  956. reg &= ~PORT_POWER;
  957. break;
  958. case USB_PORT_FEAT_C_RESET:
  959. case USB_PORT_FEAT_C_CONNECTION:
  960. case USB_PORT_FEAT_C_OVER_CURRENT:
  961. case USB_PORT_FEAT_C_ENABLE:
  962. xhci_clear_port_change_bit((le16_to_cpu(req->value)),
  963. le16_to_cpu(req->index),
  964. status_reg, reg);
  965. break;
  966. default:
  967. printf("unknown feature %x\n", le16_to_cpu(req->value));
  968. goto unknown;
  969. }
  970. xhci_writel(status_reg, reg);
  971. break;
  972. default:
  973. puts("Unknown request\n");
  974. goto unknown;
  975. }
  976. debug("scrlen = %d\n req->length = %d\n",
  977. srclen, le16_to_cpu(req->length));
  978. len = min(srclen, (int)le16_to_cpu(req->length));
  979. if (srcptr != NULL && len > 0)
  980. memcpy(buffer, srcptr, len);
  981. else
  982. debug("Len is 0\n");
  983. udev->act_len = len;
  984. udev->status = 0;
  985. return 0;
  986. unknown:
  987. udev->act_len = 0;
  988. udev->status = USB_ST_STALLED;
  989. return -ENODEV;
  990. }
  991. /**
  992. * Submits the INT request to XHCI Host cotroller
  993. *
  994. * @param udev pointer to the USB device
  995. * @param pipe contains the DIR_IN or OUT , devnum
  996. * @param buffer buffer to be read/written based on the request
  997. * @param length length of the buffer
  998. * @param interval interval of the interrupt
  999. * @return 0
  1000. */
  1001. static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe,
  1002. void *buffer, int length, int interval,
  1003. bool nonblock)
  1004. {
  1005. if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
  1006. printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
  1007. return -EINVAL;
  1008. }
  1009. /*
  1010. * xHCI uses normal TRBs for both bulk and interrupt. When the
  1011. * interrupt endpoint is to be serviced, the xHC will consume
  1012. * (at most) one TD. A TD (comprised of sg list entries) can
  1013. * take several service intervals to transmit.
  1014. */
  1015. return xhci_bulk_tx(udev, pipe, length, buffer);
  1016. }
  1017. /**
  1018. * submit the BULK type of request to the USB Device
  1019. *
  1020. * @param udev pointer to the USB device
  1021. * @param pipe contains the DIR_IN or OUT , devnum
  1022. * @param buffer buffer to be read/written based on the request
  1023. * @param length length of the buffer
  1024. * @return returns 0 if successful else -1 on failure
  1025. */
  1026. static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe,
  1027. void *buffer, int length)
  1028. {
  1029. if (usb_pipetype(pipe) != PIPE_BULK) {
  1030. printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
  1031. return -EINVAL;
  1032. }
  1033. return xhci_bulk_tx(udev, pipe, length, buffer);
  1034. }
  1035. /**
  1036. * submit the control type of request to the Root hub/Device based on the devnum
  1037. *
  1038. * @param udev pointer to the USB device
  1039. * @param pipe contains the DIR_IN or OUT , devnum
  1040. * @param buffer buffer to be read/written based on the request
  1041. * @param length length of the buffer
  1042. * @param setup Request type
  1043. * @param root_portnr Root port number that this device is on
  1044. * @return returns 0 if successful else -1 on failure
  1045. */
  1046. static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe,
  1047. void *buffer, int length,
  1048. struct devrequest *setup, int root_portnr)
  1049. {
  1050. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  1051. int ret = 0;
  1052. if (usb_pipetype(pipe) != PIPE_CONTROL) {
  1053. printf("non-control pipe (type=%lu)", usb_pipetype(pipe));
  1054. return -EINVAL;
  1055. }
  1056. if (usb_pipedevice(pipe) == ctrl->rootdev)
  1057. return xhci_submit_root(udev, pipe, buffer, setup);
  1058. if (setup->request == USB_REQ_SET_ADDRESS &&
  1059. (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  1060. return xhci_address_device(udev, root_portnr);
  1061. if (setup->request == USB_REQ_SET_CONFIGURATION &&
  1062. (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1063. ret = xhci_set_configuration(udev);
  1064. if (ret) {
  1065. puts("Failed to configure xHCI endpoint\n");
  1066. return ret;
  1067. }
  1068. }
  1069. return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
  1070. }
  1071. static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
  1072. {
  1073. struct xhci_hccr *hccr;
  1074. struct xhci_hcor *hcor;
  1075. uint32_t val;
  1076. uint32_t val2;
  1077. uint32_t reg;
  1078. hccr = ctrl->hccr;
  1079. hcor = ctrl->hcor;
  1080. /*
  1081. * Program the Number of Device Slots Enabled field in the CONFIG
  1082. * register with the max value of slots the HC can handle.
  1083. */
  1084. val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
  1085. val2 = xhci_readl(&hcor->or_config);
  1086. val |= (val2 & ~HCS_SLOTS_MASK);
  1087. xhci_writel(&hcor->or_config, val);
  1088. /* initializing xhci data structures */
  1089. if (xhci_mem_init(ctrl, hccr, hcor) < 0)
  1090. return -ENOMEM;
  1091. reg = xhci_readl(&hccr->cr_hcsparams1);
  1092. descriptor.hub.bNbrPorts = HCS_MAX_PORTS(reg);
  1093. printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  1094. /* Port Indicators */
  1095. reg = xhci_readl(&hccr->cr_hccparams);
  1096. if (HCS_INDICATOR(reg))
  1097. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  1098. | 0x80, &descriptor.hub.wHubCharacteristics);
  1099. /* Port Power Control */
  1100. if (HCC_PPC(reg))
  1101. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  1102. | 0x01, &descriptor.hub.wHubCharacteristics);
  1103. if (xhci_start(hcor)) {
  1104. xhci_reset(hcor);
  1105. return -ENODEV;
  1106. }
  1107. /* Zero'ing IRQ control register and IRQ pending register */
  1108. xhci_writel(&ctrl->ir_set->irq_control, 0x0);
  1109. xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
  1110. reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
  1111. printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
  1112. ctrl->hci_version = reg;
  1113. return 0;
  1114. }
  1115. static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
  1116. {
  1117. u32 temp;
  1118. xhci_reset(ctrl->hcor);
  1119. debug("// Disabling event ring interrupts\n");
  1120. temp = xhci_readl(&ctrl->hcor->or_usbsts);
  1121. xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
  1122. temp = xhci_readl(&ctrl->ir_set->irq_pending);
  1123. xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
  1124. return 0;
  1125. }
  1126. #if !CONFIG_IS_ENABLED(DM_USB)
  1127. int submit_control_msg(struct usb_device *udev, unsigned long pipe,
  1128. void *buffer, int length, struct devrequest *setup)
  1129. {
  1130. struct usb_device *hop = udev;
  1131. if (hop->parent)
  1132. while (hop->parent->parent)
  1133. hop = hop->parent;
  1134. return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
  1135. hop->portnr);
  1136. }
  1137. int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  1138. int length)
  1139. {
  1140. return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
  1141. }
  1142. int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  1143. int length, int interval, bool nonblock)
  1144. {
  1145. return _xhci_submit_int_msg(udev, pipe, buffer, length, interval,
  1146. nonblock);
  1147. }
  1148. /**
  1149. * Intialises the XHCI host controller
  1150. * and allocates the necessary data structures
  1151. *
  1152. * @param index index to the host controller data structure
  1153. * @return pointer to the intialised controller
  1154. */
  1155. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  1156. {
  1157. struct xhci_hccr *hccr;
  1158. struct xhci_hcor *hcor;
  1159. struct xhci_ctrl *ctrl;
  1160. int ret;
  1161. *controller = NULL;
  1162. if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0)
  1163. return -ENODEV;
  1164. if (xhci_reset(hcor) != 0)
  1165. return -ENODEV;
  1166. ctrl = &xhcic[index];
  1167. ctrl->hccr = hccr;
  1168. ctrl->hcor = hcor;
  1169. ret = xhci_lowlevel_init(ctrl);
  1170. if (ret) {
  1171. ctrl->hccr = NULL;
  1172. ctrl->hcor = NULL;
  1173. } else {
  1174. *controller = &xhcic[index];
  1175. }
  1176. return ret;
  1177. }
  1178. /**
  1179. * Stops the XHCI host controller
  1180. * and cleans up all the related data structures
  1181. *
  1182. * @param index index to the host controller data structure
  1183. * @return none
  1184. */
  1185. int usb_lowlevel_stop(int index)
  1186. {
  1187. struct xhci_ctrl *ctrl = (xhcic + index);
  1188. if (ctrl->hcor) {
  1189. xhci_lowlevel_stop(ctrl);
  1190. xhci_hcd_stop(index);
  1191. xhci_cleanup(ctrl);
  1192. }
  1193. return 0;
  1194. }
  1195. #endif /* CONFIG_IS_ENABLED(DM_USB) */
  1196. #if CONFIG_IS_ENABLED(DM_USB)
  1197. static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
  1198. unsigned long pipe, void *buffer, int length,
  1199. struct devrequest *setup)
  1200. {
  1201. struct usb_device *uhop;
  1202. struct udevice *hub;
  1203. int root_portnr = 0;
  1204. debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
  1205. dev->name, udev, udev->dev->name, udev->portnr);
  1206. hub = udev->dev;
  1207. if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
  1208. /* Figure out our port number on the root hub */
  1209. if (usb_hub_is_root_hub(hub)) {
  1210. root_portnr = udev->portnr;
  1211. } else {
  1212. while (!usb_hub_is_root_hub(hub->parent))
  1213. hub = hub->parent;
  1214. uhop = dev_get_parent_priv(hub);
  1215. root_portnr = uhop->portnr;
  1216. }
  1217. }
  1218. /*
  1219. struct usb_device *hop = udev;
  1220. if (hop->parent)
  1221. while (hop->parent->parent)
  1222. hop = hop->parent;
  1223. */
  1224. return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
  1225. root_portnr);
  1226. }
  1227. static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
  1228. unsigned long pipe, void *buffer, int length)
  1229. {
  1230. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1231. return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
  1232. }
  1233. static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
  1234. unsigned long pipe, void *buffer, int length,
  1235. int interval, bool nonblock)
  1236. {
  1237. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1238. return _xhci_submit_int_msg(udev, pipe, buffer, length, interval,
  1239. nonblock);
  1240. }
  1241. static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
  1242. {
  1243. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1244. return _xhci_alloc_device(udev);
  1245. }
  1246. static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev)
  1247. {
  1248. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1249. struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev);
  1250. struct xhci_virt_device *virt_dev;
  1251. struct xhci_input_control_ctx *ctrl_ctx;
  1252. struct xhci_container_ctx *out_ctx;
  1253. struct xhci_container_ctx *in_ctx;
  1254. struct xhci_slot_ctx *slot_ctx;
  1255. int slot_id = udev->slot_id;
  1256. unsigned think_time;
  1257. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1258. /* Ignore root hubs */
  1259. if (usb_hub_is_root_hub(udev->dev))
  1260. return 0;
  1261. virt_dev = ctrl->devs[slot_id];
  1262. BUG_ON(!virt_dev);
  1263. out_ctx = virt_dev->out_ctx;
  1264. in_ctx = virt_dev->in_ctx;
  1265. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  1266. /* Initialize the input context control */
  1267. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
  1268. ctrl_ctx->drop_flags = 0;
  1269. xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
  1270. /* slot context */
  1271. xhci_slot_copy(ctrl, in_ctx, out_ctx);
  1272. slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
  1273. /* Update hub related fields */
  1274. slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
  1275. /*
  1276. * refer to section 6.2.2: MTT should be 0 for full speed hub,
  1277. * but it may be already set to 1 when setup an xHCI virtual
  1278. * device, so clear it anyway.
  1279. */
  1280. if (hub->tt.multi)
  1281. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  1282. else if (udev->speed == USB_SPEED_FULL)
  1283. slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
  1284. slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild));
  1285. /*
  1286. * Set TT think time - convert from ns to FS bit times.
  1287. * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns
  1288. *
  1289. * 0 = 8 FS bit times, 1 = 16 FS bit times,
  1290. * 2 = 24 FS bit times, 3 = 32 FS bit times.
  1291. *
  1292. * This field shall be 0 if the device is not a high-spped hub.
  1293. */
  1294. think_time = hub->tt.think_time;
  1295. if (think_time != 0)
  1296. think_time = (think_time / 666) - 1;
  1297. if (udev->speed == USB_SPEED_HIGH)
  1298. slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time));
  1299. slot_ctx->dev_state = 0;
  1300. return xhci_configure_endpoints(udev, false);
  1301. }
  1302. static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size)
  1303. {
  1304. /*
  1305. * xHCD allocates one segment which includes 64 TRBs for each endpoint
  1306. * and the last TRB in this segment is configured as a link TRB to form
  1307. * a TRB ring. Each TRB can transfer up to 64K bytes, however data
  1308. * buffers referenced by transfer TRBs shall not span 64KB boundaries.
  1309. * Hence the maximum number of TRBs we can use in one transfer is 62.
  1310. */
  1311. *size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE;
  1312. return 0;
  1313. }
  1314. int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
  1315. struct xhci_hcor *hcor)
  1316. {
  1317. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1318. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  1319. int ret;
  1320. debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
  1321. ctrl, hccr, hcor);
  1322. ctrl->dev = dev;
  1323. /*
  1324. * XHCI needs to issue a Address device command to setup
  1325. * proper device context structures, before it can interact
  1326. * with the device. So a get_descriptor will fail before any
  1327. * of that is done for XHCI unlike EHCI.
  1328. */
  1329. priv->desc_before_addr = false;
  1330. ret = xhci_reset(hcor);
  1331. if (ret)
  1332. goto err;
  1333. ctrl->hccr = hccr;
  1334. ctrl->hcor = hcor;
  1335. ret = xhci_lowlevel_init(ctrl);
  1336. if (ret)
  1337. goto err;
  1338. return 0;
  1339. err:
  1340. free(ctrl);
  1341. debug("%s: failed, ret=%d\n", __func__, ret);
  1342. return ret;
  1343. }
  1344. int xhci_deregister(struct udevice *dev)
  1345. {
  1346. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1347. xhci_lowlevel_stop(ctrl);
  1348. xhci_cleanup(ctrl);
  1349. return 0;
  1350. }
  1351. struct dm_usb_ops xhci_usb_ops = {
  1352. .control = xhci_submit_control_msg,
  1353. .bulk = xhci_submit_bulk_msg,
  1354. .interrupt = xhci_submit_int_msg,
  1355. .alloc_device = xhci_alloc_device,
  1356. .update_hub_device = xhci_update_hub_device,
  1357. .get_max_xfer_size = xhci_get_max_xfer_size,
  1358. };
  1359. #endif