xhci.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  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. static 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. * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
  239. * microframes, rounded down to nearest power of 2.
  240. */
  241. static unsigned int xhci_microframes_to_exponent(unsigned int desc_interval,
  242. unsigned int min_exponent,
  243. unsigned int max_exponent)
  244. {
  245. unsigned int interval;
  246. interval = fls(desc_interval) - 1;
  247. interval = clamp_val(interval, min_exponent, max_exponent);
  248. if ((1 << interval) != desc_interval)
  249. debug("rounding interval to %d microframes, "\
  250. "ep desc says %d microframes\n",
  251. 1 << interval, desc_interval);
  252. return interval;
  253. }
  254. static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
  255. struct usb_endpoint_descriptor *endpt_desc)
  256. {
  257. if (endpt_desc->bInterval == 0)
  258. return 0;
  259. return xhci_microframes_to_exponent(endpt_desc->bInterval, 0, 15);
  260. }
  261. static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
  262. struct usb_endpoint_descriptor *endpt_desc)
  263. {
  264. return xhci_microframes_to_exponent(endpt_desc->bInterval * 8, 3, 10);
  265. }
  266. /*
  267. * Convert interval expressed as 2^(bInterval - 1) == interval into
  268. * straight exponent value 2^n == interval.
  269. */
  270. static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
  271. struct usb_endpoint_descriptor *endpt_desc)
  272. {
  273. unsigned int interval;
  274. interval = clamp_val(endpt_desc->bInterval, 1, 16) - 1;
  275. if (interval != endpt_desc->bInterval - 1)
  276. debug("ep %#x - rounding interval to %d %sframes\n",
  277. endpt_desc->bEndpointAddress, 1 << interval,
  278. udev->speed == USB_SPEED_FULL ? "" : "micro");
  279. if (udev->speed == USB_SPEED_FULL) {
  280. /*
  281. * Full speed isoc endpoints specify interval in frames,
  282. * not microframes. We are using microframes everywhere,
  283. * so adjust accordingly.
  284. */
  285. interval += 3; /* 1 frame = 2^3 uframes */
  286. }
  287. return interval;
  288. }
  289. /*
  290. * Return the polling or NAK interval.
  291. *
  292. * The polling interval is expressed in "microframes". If xHCI's Interval field
  293. * is set to N, it will service the endpoint every 2^(Interval)*125us.
  294. *
  295. * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
  296. * is set to 0.
  297. */
  298. static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
  299. struct usb_endpoint_descriptor *endpt_desc)
  300. {
  301. unsigned int interval = 0;
  302. switch (udev->speed) {
  303. case USB_SPEED_HIGH:
  304. /* Max NAK rate */
  305. if (usb_endpoint_xfer_control(endpt_desc) ||
  306. usb_endpoint_xfer_bulk(endpt_desc)) {
  307. interval = xhci_parse_microframe_interval(udev,
  308. endpt_desc);
  309. break;
  310. }
  311. /* Fall through - SS and HS isoc/int have same decoding */
  312. case USB_SPEED_SUPER:
  313. if (usb_endpoint_xfer_int(endpt_desc) ||
  314. usb_endpoint_xfer_isoc(endpt_desc)) {
  315. interval = xhci_parse_exponent_interval(udev,
  316. endpt_desc);
  317. }
  318. break;
  319. case USB_SPEED_FULL:
  320. if (usb_endpoint_xfer_isoc(endpt_desc)) {
  321. interval = xhci_parse_exponent_interval(udev,
  322. endpt_desc);
  323. break;
  324. }
  325. /*
  326. * Fall through for interrupt endpoint interval decoding
  327. * since it uses the same rules as low speed interrupt
  328. * endpoints.
  329. */
  330. case USB_SPEED_LOW:
  331. if (usb_endpoint_xfer_int(endpt_desc) ||
  332. usb_endpoint_xfer_isoc(endpt_desc)) {
  333. interval = xhci_parse_frame_interval(udev, endpt_desc);
  334. }
  335. break;
  336. default:
  337. BUG();
  338. }
  339. return interval;
  340. }
  341. /*
  342. * The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
  343. * High speed endpoint descriptors can define "the number of additional
  344. * transaction opportunities per microframe", but that goes in the Max Burst
  345. * endpoint context field.
  346. */
  347. static u32 xhci_get_endpoint_mult(struct usb_device *udev,
  348. struct usb_endpoint_descriptor *endpt_desc,
  349. struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
  350. {
  351. if (udev->speed < USB_SPEED_SUPER ||
  352. !usb_endpoint_xfer_isoc(endpt_desc))
  353. return 0;
  354. return ss_ep_comp_desc->bmAttributes;
  355. }
  356. static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
  357. struct usb_endpoint_descriptor *endpt_desc,
  358. struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
  359. {
  360. /* Super speed and Plus have max burst in ep companion desc */
  361. if (udev->speed >= USB_SPEED_SUPER)
  362. return ss_ep_comp_desc->bMaxBurst;
  363. if (udev->speed == USB_SPEED_HIGH &&
  364. (usb_endpoint_xfer_isoc(endpt_desc) ||
  365. usb_endpoint_xfer_int(endpt_desc)))
  366. return usb_endpoint_maxp_mult(endpt_desc) - 1;
  367. return 0;
  368. }
  369. /*
  370. * Return the maximum endpoint service interval time (ESIT) payload.
  371. * Basically, this is the maxpacket size, multiplied by the burst size
  372. * and mult size.
  373. */
  374. static u32 xhci_get_max_esit_payload(struct usb_device *udev,
  375. struct usb_endpoint_descriptor *endpt_desc,
  376. struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
  377. {
  378. int max_burst;
  379. int max_packet;
  380. /* Only applies for interrupt or isochronous endpoints */
  381. if (usb_endpoint_xfer_control(endpt_desc) ||
  382. usb_endpoint_xfer_bulk(endpt_desc))
  383. return 0;
  384. /* SuperSpeed Isoc ep with less than 48k per esit */
  385. if (udev->speed >= USB_SPEED_SUPER)
  386. return le16_to_cpu(ss_ep_comp_desc->wBytesPerInterval);
  387. max_packet = usb_endpoint_maxp(endpt_desc);
  388. max_burst = usb_endpoint_maxp_mult(endpt_desc);
  389. /* A 0 in max burst means 1 transfer per ESIT */
  390. return max_packet * max_burst;
  391. }
  392. /**
  393. * Issue a configure endpoint command or evaluate context command
  394. * and wait for it to finish.
  395. *
  396. * @param udev pointer to the Device Data Structure
  397. * @param ctx_change flag to indicate the Context has changed or NOT
  398. * @return 0 on success, -1 on failure
  399. */
  400. static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change)
  401. {
  402. struct xhci_container_ctx *in_ctx;
  403. struct xhci_virt_device *virt_dev;
  404. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  405. union xhci_trb *event;
  406. virt_dev = ctrl->devs[udev->slot_id];
  407. in_ctx = virt_dev->in_ctx;
  408. xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size);
  409. xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0,
  410. ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP);
  411. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  412. BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
  413. != udev->slot_id);
  414. switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
  415. case COMP_SUCCESS:
  416. debug("Successful %s command\n",
  417. ctx_change ? "Evaluate Context" : "Configure Endpoint");
  418. break;
  419. default:
  420. printf("ERROR: %s command returned completion code %d.\n",
  421. ctx_change ? "Evaluate Context" : "Configure Endpoint",
  422. GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
  423. return -EINVAL;
  424. }
  425. xhci_acknowledge_event(ctrl);
  426. return 0;
  427. }
  428. /**
  429. * Configure the endpoint, programming the device contexts.
  430. *
  431. * @param udev pointer to the USB device structure
  432. * @return returns the status of the xhci_configure_endpoints
  433. */
  434. static int xhci_set_configuration(struct usb_device *udev)
  435. {
  436. struct xhci_container_ctx *in_ctx;
  437. struct xhci_container_ctx *out_ctx;
  438. struct xhci_input_control_ctx *ctrl_ctx;
  439. struct xhci_slot_ctx *slot_ctx;
  440. struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM];
  441. int cur_ep;
  442. int max_ep_flag = 0;
  443. int ep_index;
  444. unsigned int dir;
  445. unsigned int ep_type;
  446. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  447. int num_of_ep;
  448. int ep_flag = 0;
  449. u64 trb_64 = 0;
  450. int slot_id = udev->slot_id;
  451. struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
  452. struct usb_interface *ifdesc;
  453. u32 max_esit_payload;
  454. unsigned int interval;
  455. unsigned int mult;
  456. unsigned int max_burst;
  457. unsigned int avg_trb_len;
  458. unsigned int err_count = 0;
  459. out_ctx = virt_dev->out_ctx;
  460. in_ctx = virt_dev->in_ctx;
  461. num_of_ep = udev->config.if_desc[0].no_of_ep;
  462. ifdesc = &udev->config.if_desc[0];
  463. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  464. /* Initialize the input context control */
  465. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
  466. ctrl_ctx->drop_flags = 0;
  467. /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
  468. for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
  469. ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
  470. ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
  471. if (max_ep_flag < ep_flag)
  472. max_ep_flag = ep_flag;
  473. }
  474. xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
  475. /* slot context */
  476. xhci_slot_copy(ctrl, in_ctx, out_ctx);
  477. slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
  478. slot_ctx->dev_info &= ~(LAST_CTX_MASK);
  479. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0);
  480. xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0);
  481. /* filling up ep contexts */
  482. for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
  483. struct usb_endpoint_descriptor *endpt_desc = NULL;
  484. struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc = NULL;
  485. endpt_desc = &ifdesc->ep_desc[cur_ep];
  486. ss_ep_comp_desc = &ifdesc->ss_ep_comp_desc[cur_ep];
  487. trb_64 = 0;
  488. /*
  489. * Get values to fill the endpoint context, mostly from ep
  490. * descriptor. The average TRB buffer lengt for bulk endpoints
  491. * is unclear as we have no clue on scatter gather list entry
  492. * size. For Isoc and Int, set it to max available.
  493. * See xHCI 1.1 spec 4.14.1.1 for details.
  494. */
  495. max_esit_payload = xhci_get_max_esit_payload(udev, endpt_desc,
  496. ss_ep_comp_desc);
  497. interval = xhci_get_endpoint_interval(udev, endpt_desc);
  498. mult = xhci_get_endpoint_mult(udev, endpt_desc,
  499. ss_ep_comp_desc);
  500. max_burst = xhci_get_endpoint_max_burst(udev, endpt_desc,
  501. ss_ep_comp_desc);
  502. avg_trb_len = max_esit_payload;
  503. ep_index = xhci_get_ep_index(endpt_desc);
  504. ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
  505. /* Allocate the ep rings */
  506. virt_dev->eps[ep_index].ring = xhci_ring_alloc(1, true);
  507. if (!virt_dev->eps[ep_index].ring)
  508. return -ENOMEM;
  509. /*NOTE: ep_desc[0] actually represents EP1 and so on */
  510. dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7);
  511. ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2));
  512. ep_ctx[ep_index]->ep_info =
  513. cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
  514. EP_INTERVAL(interval) | EP_MULT(mult));
  515. ep_ctx[ep_index]->ep_info2 =
  516. cpu_to_le32(ep_type << EP_TYPE_SHIFT);
  517. ep_ctx[ep_index]->ep_info2 |=
  518. cpu_to_le32(MAX_PACKET
  519. (get_unaligned(&endpt_desc->wMaxPacketSize)));
  520. /* Allow 3 retries for everything but isoc, set CErr = 3 */
  521. if (!usb_endpoint_xfer_isoc(endpt_desc))
  522. err_count = 3;
  523. ep_ctx[ep_index]->ep_info2 |=
  524. cpu_to_le32(MAX_BURST(max_burst) |
  525. ERROR_COUNT(err_count));
  526. trb_64 = (uintptr_t)
  527. 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. return xhci_configure_endpoints(udev, false);
  541. }
  542. /**
  543. * Issue an Address Device command (which will issue a SetAddress request to
  544. * the device).
  545. *
  546. * @param udev pointer to the Device Data Structure
  547. * @return 0 if successful else error code on failure
  548. */
  549. static int xhci_address_device(struct usb_device *udev, int root_portnr)
  550. {
  551. int ret = 0;
  552. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  553. struct xhci_slot_ctx *slot_ctx;
  554. struct xhci_input_control_ctx *ctrl_ctx;
  555. struct xhci_virt_device *virt_dev;
  556. int slot_id = udev->slot_id;
  557. union xhci_trb *event;
  558. virt_dev = ctrl->devs[slot_id];
  559. /*
  560. * This is the first Set Address since device plug-in
  561. * so setting up the slot context.
  562. */
  563. debug("Setting up addressable devices %p\n", ctrl->dcbaa);
  564. xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr);
  565. ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
  566. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
  567. ctrl_ctx->drop_flags = 0;
  568. xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV);
  569. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  570. BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
  571. switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
  572. case COMP_CTX_STATE:
  573. case COMP_EBADSLT:
  574. printf("Setup ERROR: address device command for slot %d.\n",
  575. slot_id);
  576. ret = -EINVAL;
  577. break;
  578. case COMP_TX_ERR:
  579. puts("Device not responding to set address.\n");
  580. ret = -EPROTO;
  581. break;
  582. case COMP_DEV_ERR:
  583. puts("ERROR: Incompatible device"
  584. "for address device command.\n");
  585. ret = -ENODEV;
  586. break;
  587. case COMP_SUCCESS:
  588. debug("Successful Address Device command\n");
  589. udev->status = 0;
  590. break;
  591. default:
  592. printf("ERROR: unexpected command completion code 0x%x.\n",
  593. GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
  594. ret = -EINVAL;
  595. break;
  596. }
  597. xhci_acknowledge_event(ctrl);
  598. if (ret < 0)
  599. /*
  600. * TODO: Unsuccessful Address Device command shall leave the
  601. * slot in default state. So, issue Disable Slot command now.
  602. */
  603. return ret;
  604. xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
  605. virt_dev->out_ctx->size);
  606. slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx);
  607. debug("xHC internal address is: %d\n",
  608. le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
  609. return 0;
  610. }
  611. /**
  612. * Issue Enable slot command to the controller to allocate
  613. * device slot and assign the slot id. It fails if the xHC
  614. * ran out of device slots, the Enable Slot command timed out,
  615. * or allocating memory failed.
  616. *
  617. * @param udev pointer to the Device Data Structure
  618. * @return Returns 0 on succes else return error code on failure
  619. */
  620. static int _xhci_alloc_device(struct usb_device *udev)
  621. {
  622. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  623. union xhci_trb *event;
  624. int ret;
  625. /*
  626. * Root hub will be first device to be initailized.
  627. * If this device is root-hub, don't do any xHC related
  628. * stuff.
  629. */
  630. if (ctrl->rootdev == 0) {
  631. udev->speed = USB_SPEED_SUPER;
  632. return 0;
  633. }
  634. xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT);
  635. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  636. BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
  637. != COMP_SUCCESS);
  638. udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags));
  639. xhci_acknowledge_event(ctrl);
  640. ret = xhci_alloc_virt_device(ctrl, udev->slot_id);
  641. if (ret < 0) {
  642. /*
  643. * TODO: Unsuccessful Address Device command shall leave
  644. * the slot in default. So, issue Disable Slot command now.
  645. */
  646. puts("Could not allocate xHCI USB device data structures\n");
  647. return ret;
  648. }
  649. return 0;
  650. }
  651. #ifndef CONFIG_DM_USB
  652. int usb_alloc_device(struct usb_device *udev)
  653. {
  654. return _xhci_alloc_device(udev);
  655. }
  656. #endif
  657. /*
  658. * Full speed devices may have a max packet size greater than 8 bytes, but the
  659. * USB core doesn't know that until it reads the first 8 bytes of the
  660. * descriptor. If the usb_device's max packet size changes after that point,
  661. * we need to issue an evaluate context command and wait on it.
  662. *
  663. * @param udev pointer to the Device Data Structure
  664. * @return returns the status of the xhci_configure_endpoints
  665. */
  666. int xhci_check_maxpacket(struct usb_device *udev)
  667. {
  668. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  669. unsigned int slot_id = udev->slot_id;
  670. int ep_index = 0; /* control endpoint */
  671. struct xhci_container_ctx *in_ctx;
  672. struct xhci_container_ctx *out_ctx;
  673. struct xhci_input_control_ctx *ctrl_ctx;
  674. struct xhci_ep_ctx *ep_ctx;
  675. int max_packet_size;
  676. int hw_max_packet_size;
  677. int ret = 0;
  678. out_ctx = ctrl->devs[slot_id]->out_ctx;
  679. xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
  680. ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
  681. hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  682. max_packet_size = udev->epmaxpacketin[0];
  683. if (hw_max_packet_size != max_packet_size) {
  684. debug("Max Packet Size for ep 0 changed.\n");
  685. debug("Max packet size in usb_device = %d\n", max_packet_size);
  686. debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size);
  687. debug("Issuing evaluate context command.\n");
  688. /* Set up the modified control endpoint 0 */
  689. xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx,
  690. ctrl->devs[slot_id]->out_ctx, ep_index);
  691. in_ctx = ctrl->devs[slot_id]->in_ctx;
  692. ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
  693. ep_ctx->ep_info2 &= cpu_to_le32(~((0xffff & MAX_PACKET_MASK)
  694. << MAX_PACKET_SHIFT));
  695. ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
  696. /*
  697. * Set up the input context flags for the command
  698. * FIXME: This won't work if a non-default control endpoint
  699. * changes max packet sizes.
  700. */
  701. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  702. ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
  703. ctrl_ctx->drop_flags = 0;
  704. ret = xhci_configure_endpoints(udev, true);
  705. }
  706. return ret;
  707. }
  708. /**
  709. * Clears the Change bits of the Port Status Register
  710. *
  711. * @param wValue request value
  712. * @param wIndex request index
  713. * @param addr address of posrt status register
  714. * @param port_status state of port status register
  715. * @return none
  716. */
  717. static void xhci_clear_port_change_bit(u16 wValue,
  718. u16 wIndex, volatile uint32_t *addr, u32 port_status)
  719. {
  720. char *port_change_bit;
  721. u32 status;
  722. switch (wValue) {
  723. case USB_PORT_FEAT_C_RESET:
  724. status = PORT_RC;
  725. port_change_bit = "reset";
  726. break;
  727. case USB_PORT_FEAT_C_CONNECTION:
  728. status = PORT_CSC;
  729. port_change_bit = "connect";
  730. break;
  731. case USB_PORT_FEAT_C_OVER_CURRENT:
  732. status = PORT_OCC;
  733. port_change_bit = "over-current";
  734. break;
  735. case USB_PORT_FEAT_C_ENABLE:
  736. status = PORT_PEC;
  737. port_change_bit = "enable/disable";
  738. break;
  739. case USB_PORT_FEAT_C_SUSPEND:
  740. status = PORT_PLC;
  741. port_change_bit = "suspend/resume";
  742. break;
  743. default:
  744. /* Should never happen */
  745. return;
  746. }
  747. /* Change bits are all write 1 to clear */
  748. xhci_writel(addr, port_status | status);
  749. port_status = xhci_readl(addr);
  750. debug("clear port %s change, actual port %d status = 0x%x\n",
  751. port_change_bit, wIndex, port_status);
  752. }
  753. /**
  754. * Save Read Only (RO) bits and save read/write bits where
  755. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  756. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  757. *
  758. * @param state state of the Port Status and Control Regsiter
  759. * @return a value that would result in the port being in the
  760. * same state, if the value was written to the port
  761. * status control register.
  762. */
  763. static u32 xhci_port_state_to_neutral(u32 state)
  764. {
  765. /* Save read-only status and port state */
  766. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  767. }
  768. /**
  769. * Submits the Requests to the XHCI Host Controller
  770. *
  771. * @param udev pointer to the USB device structure
  772. * @param pipe contains the DIR_IN or OUT , devnum
  773. * @param buffer buffer to be read/written based on the request
  774. * @return returns 0 if successful else -1 on failure
  775. */
  776. static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
  777. void *buffer, struct devrequest *req)
  778. {
  779. uint8_t tmpbuf[4];
  780. u16 typeReq;
  781. void *srcptr = NULL;
  782. int len, srclen;
  783. uint32_t reg;
  784. volatile uint32_t *status_reg;
  785. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  786. struct xhci_hccr *hccr = ctrl->hccr;
  787. struct xhci_hcor *hcor = ctrl->hcor;
  788. int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1));
  789. if ((req->requesttype & USB_RT_PORT) &&
  790. le16_to_cpu(req->index) > max_ports) {
  791. printf("The request port(%d) exceeds maximum port number\n",
  792. le16_to_cpu(req->index) - 1);
  793. return -EINVAL;
  794. }
  795. status_reg = (volatile uint32_t *)
  796. (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc);
  797. srclen = 0;
  798. typeReq = req->request | req->requesttype << 8;
  799. switch (typeReq) {
  800. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  801. switch (le16_to_cpu(req->value) >> 8) {
  802. case USB_DT_DEVICE:
  803. debug("USB_DT_DEVICE request\n");
  804. srcptr = &descriptor.device;
  805. srclen = 0x12;
  806. break;
  807. case USB_DT_CONFIG:
  808. debug("USB_DT_CONFIG config\n");
  809. srcptr = &descriptor.config;
  810. srclen = 0x19;
  811. break;
  812. case USB_DT_STRING:
  813. debug("USB_DT_STRING config\n");
  814. switch (le16_to_cpu(req->value) & 0xff) {
  815. case 0: /* Language */
  816. srcptr = "\4\3\11\4";
  817. srclen = 4;
  818. break;
  819. case 1: /* Vendor String */
  820. srcptr = "\16\3U\0-\0B\0o\0o\0t\0";
  821. srclen = 14;
  822. break;
  823. case 2: /* Product Name */
  824. srcptr = "\52\3X\0H\0C\0I\0 "
  825. "\0H\0o\0s\0t\0 "
  826. "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
  827. srclen = 42;
  828. break;
  829. default:
  830. printf("unknown value DT_STRING %x\n",
  831. le16_to_cpu(req->value));
  832. goto unknown;
  833. }
  834. break;
  835. default:
  836. printf("unknown value %x\n", le16_to_cpu(req->value));
  837. goto unknown;
  838. }
  839. break;
  840. case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
  841. switch (le16_to_cpu(req->value) >> 8) {
  842. case USB_DT_HUB:
  843. case USB_DT_SS_HUB:
  844. debug("USB_DT_HUB config\n");
  845. srcptr = &descriptor.hub;
  846. srclen = 0x8;
  847. break;
  848. default:
  849. printf("unknown value %x\n", le16_to_cpu(req->value));
  850. goto unknown;
  851. }
  852. break;
  853. case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
  854. debug("USB_REQ_SET_ADDRESS\n");
  855. ctrl->rootdev = le16_to_cpu(req->value);
  856. break;
  857. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  858. /* Do nothing */
  859. break;
  860. case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
  861. tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
  862. tmpbuf[1] = 0;
  863. srcptr = tmpbuf;
  864. srclen = 2;
  865. break;
  866. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  867. memset(tmpbuf, 0, 4);
  868. reg = xhci_readl(status_reg);
  869. if (reg & PORT_CONNECT) {
  870. tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
  871. switch (reg & DEV_SPEED_MASK) {
  872. case XDEV_FS:
  873. debug("SPEED = FULLSPEED\n");
  874. break;
  875. case XDEV_LS:
  876. debug("SPEED = LOWSPEED\n");
  877. tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
  878. break;
  879. case XDEV_HS:
  880. debug("SPEED = HIGHSPEED\n");
  881. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  882. break;
  883. case XDEV_SS:
  884. debug("SPEED = SUPERSPEED\n");
  885. tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8;
  886. break;
  887. }
  888. }
  889. if (reg & PORT_PE)
  890. tmpbuf[0] |= USB_PORT_STAT_ENABLE;
  891. if ((reg & PORT_PLS_MASK) == XDEV_U3)
  892. tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
  893. if (reg & PORT_OC)
  894. tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
  895. if (reg & PORT_RESET)
  896. tmpbuf[0] |= USB_PORT_STAT_RESET;
  897. if (reg & PORT_POWER)
  898. /*
  899. * XXX: This Port power bit (for USB 3.0 hub)
  900. * we are faking in USB 2.0 hub port status;
  901. * since there's a change in bit positions in
  902. * two:
  903. * USB 2.0 port status PP is at position[8]
  904. * USB 3.0 port status PP is at position[9]
  905. * So, we are still keeping it at position [8]
  906. */
  907. tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
  908. if (reg & PORT_CSC)
  909. tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
  910. if (reg & PORT_PEC)
  911. tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
  912. if (reg & PORT_OCC)
  913. tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
  914. if (reg & PORT_RC)
  915. tmpbuf[2] |= USB_PORT_STAT_C_RESET;
  916. srcptr = tmpbuf;
  917. srclen = 4;
  918. break;
  919. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  920. reg = xhci_readl(status_reg);
  921. reg = xhci_port_state_to_neutral(reg);
  922. switch (le16_to_cpu(req->value)) {
  923. case USB_PORT_FEAT_ENABLE:
  924. reg |= PORT_PE;
  925. xhci_writel(status_reg, reg);
  926. break;
  927. case USB_PORT_FEAT_POWER:
  928. reg |= PORT_POWER;
  929. xhci_writel(status_reg, reg);
  930. break;
  931. case USB_PORT_FEAT_RESET:
  932. reg |= PORT_RESET;
  933. xhci_writel(status_reg, reg);
  934. break;
  935. default:
  936. printf("unknown feature %x\n", le16_to_cpu(req->value));
  937. goto unknown;
  938. }
  939. break;
  940. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  941. reg = xhci_readl(status_reg);
  942. reg = xhci_port_state_to_neutral(reg);
  943. switch (le16_to_cpu(req->value)) {
  944. case USB_PORT_FEAT_ENABLE:
  945. reg &= ~PORT_PE;
  946. break;
  947. case USB_PORT_FEAT_POWER:
  948. reg &= ~PORT_POWER;
  949. break;
  950. case USB_PORT_FEAT_C_RESET:
  951. case USB_PORT_FEAT_C_CONNECTION:
  952. case USB_PORT_FEAT_C_OVER_CURRENT:
  953. case USB_PORT_FEAT_C_ENABLE:
  954. xhci_clear_port_change_bit((le16_to_cpu(req->value)),
  955. le16_to_cpu(req->index),
  956. status_reg, reg);
  957. break;
  958. default:
  959. printf("unknown feature %x\n", le16_to_cpu(req->value));
  960. goto unknown;
  961. }
  962. xhci_writel(status_reg, reg);
  963. break;
  964. default:
  965. puts("Unknown request\n");
  966. goto unknown;
  967. }
  968. debug("scrlen = %d\n req->length = %d\n",
  969. srclen, le16_to_cpu(req->length));
  970. len = min(srclen, (int)le16_to_cpu(req->length));
  971. if (srcptr != NULL && len > 0)
  972. memcpy(buffer, srcptr, len);
  973. else
  974. debug("Len is 0\n");
  975. udev->act_len = len;
  976. udev->status = 0;
  977. return 0;
  978. unknown:
  979. udev->act_len = 0;
  980. udev->status = USB_ST_STALLED;
  981. return -ENODEV;
  982. }
  983. /**
  984. * Submits the INT request to XHCI Host cotroller
  985. *
  986. * @param udev pointer to the USB device
  987. * @param pipe contains the DIR_IN or OUT , devnum
  988. * @param buffer buffer to be read/written based on the request
  989. * @param length length of the buffer
  990. * @param interval interval of the interrupt
  991. * @return 0
  992. */
  993. static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe,
  994. void *buffer, int length, int interval)
  995. {
  996. if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
  997. printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
  998. return -EINVAL;
  999. }
  1000. /*
  1001. * xHCI uses normal TRBs for both bulk and interrupt. When the
  1002. * interrupt endpoint is to be serviced, the xHC will consume
  1003. * (at most) one TD. A TD (comprised of sg list entries) can
  1004. * take several service intervals to transmit.
  1005. */
  1006. return xhci_bulk_tx(udev, pipe, length, buffer);
  1007. }
  1008. /**
  1009. * submit the BULK type of request to the USB Device
  1010. *
  1011. * @param udev pointer to the USB device
  1012. * @param pipe contains the DIR_IN or OUT , devnum
  1013. * @param buffer buffer to be read/written based on the request
  1014. * @param length length of the buffer
  1015. * @return returns 0 if successful else -1 on failure
  1016. */
  1017. static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe,
  1018. void *buffer, int length)
  1019. {
  1020. if (usb_pipetype(pipe) != PIPE_BULK) {
  1021. printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
  1022. return -EINVAL;
  1023. }
  1024. return xhci_bulk_tx(udev, pipe, length, buffer);
  1025. }
  1026. /**
  1027. * submit the control type of request to the Root hub/Device based on the devnum
  1028. *
  1029. * @param udev pointer to the USB device
  1030. * @param pipe contains the DIR_IN or OUT , devnum
  1031. * @param buffer buffer to be read/written based on the request
  1032. * @param length length of the buffer
  1033. * @param setup Request type
  1034. * @param root_portnr Root port number that this device is on
  1035. * @return returns 0 if successful else -1 on failure
  1036. */
  1037. static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe,
  1038. void *buffer, int length,
  1039. struct devrequest *setup, int root_portnr)
  1040. {
  1041. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  1042. int ret = 0;
  1043. if (usb_pipetype(pipe) != PIPE_CONTROL) {
  1044. printf("non-control pipe (type=%lu)", usb_pipetype(pipe));
  1045. return -EINVAL;
  1046. }
  1047. if (usb_pipedevice(pipe) == ctrl->rootdev)
  1048. return xhci_submit_root(udev, pipe, buffer, setup);
  1049. if (setup->request == USB_REQ_SET_ADDRESS &&
  1050. (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  1051. return xhci_address_device(udev, root_portnr);
  1052. if (setup->request == USB_REQ_SET_CONFIGURATION &&
  1053. (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1054. ret = xhci_set_configuration(udev);
  1055. if (ret) {
  1056. puts("Failed to configure xHCI endpoint\n");
  1057. return ret;
  1058. }
  1059. }
  1060. return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
  1061. }
  1062. static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
  1063. {
  1064. struct xhci_hccr *hccr;
  1065. struct xhci_hcor *hcor;
  1066. uint32_t val;
  1067. uint32_t val2;
  1068. uint32_t reg;
  1069. hccr = ctrl->hccr;
  1070. hcor = ctrl->hcor;
  1071. /*
  1072. * Program the Number of Device Slots Enabled field in the CONFIG
  1073. * register with the max value of slots the HC can handle.
  1074. */
  1075. val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
  1076. val2 = xhci_readl(&hcor->or_config);
  1077. val |= (val2 & ~HCS_SLOTS_MASK);
  1078. xhci_writel(&hcor->or_config, val);
  1079. /* initializing xhci data structures */
  1080. if (xhci_mem_init(ctrl, hccr, hcor) < 0)
  1081. return -ENOMEM;
  1082. reg = xhci_readl(&hccr->cr_hcsparams1);
  1083. descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
  1084. HCS_MAX_PORTS_SHIFT);
  1085. printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  1086. /* Port Indicators */
  1087. reg = xhci_readl(&hccr->cr_hccparams);
  1088. if (HCS_INDICATOR(reg))
  1089. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  1090. | 0x80, &descriptor.hub.wHubCharacteristics);
  1091. /* Port Power Control */
  1092. if (HCC_PPC(reg))
  1093. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  1094. | 0x01, &descriptor.hub.wHubCharacteristics);
  1095. if (xhci_start(hcor)) {
  1096. xhci_reset(hcor);
  1097. return -ENODEV;
  1098. }
  1099. /* Zero'ing IRQ control register and IRQ pending register */
  1100. xhci_writel(&ctrl->ir_set->irq_control, 0x0);
  1101. xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
  1102. reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
  1103. printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
  1104. return 0;
  1105. }
  1106. static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
  1107. {
  1108. u32 temp;
  1109. xhci_reset(ctrl->hcor);
  1110. debug("// Disabling event ring interrupts\n");
  1111. temp = xhci_readl(&ctrl->hcor->or_usbsts);
  1112. xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
  1113. temp = xhci_readl(&ctrl->ir_set->irq_pending);
  1114. xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
  1115. return 0;
  1116. }
  1117. #ifndef CONFIG_DM_USB
  1118. int submit_control_msg(struct usb_device *udev, unsigned long pipe,
  1119. void *buffer, int length, struct devrequest *setup)
  1120. {
  1121. struct usb_device *hop = udev;
  1122. if (hop->parent)
  1123. while (hop->parent->parent)
  1124. hop = hop->parent;
  1125. return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
  1126. hop->portnr);
  1127. }
  1128. int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  1129. int length)
  1130. {
  1131. return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
  1132. }
  1133. int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  1134. int length, int interval)
  1135. {
  1136. return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
  1137. }
  1138. /**
  1139. * Intialises the XHCI host controller
  1140. * and allocates the necessary data structures
  1141. *
  1142. * @param index index to the host controller data structure
  1143. * @return pointer to the intialised controller
  1144. */
  1145. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  1146. {
  1147. struct xhci_hccr *hccr;
  1148. struct xhci_hcor *hcor;
  1149. struct xhci_ctrl *ctrl;
  1150. int ret;
  1151. *controller = NULL;
  1152. if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0)
  1153. return -ENODEV;
  1154. if (xhci_reset(hcor) != 0)
  1155. return -ENODEV;
  1156. ctrl = &xhcic[index];
  1157. ctrl->hccr = hccr;
  1158. ctrl->hcor = hcor;
  1159. ret = xhci_lowlevel_init(ctrl);
  1160. if (ret) {
  1161. ctrl->hccr = NULL;
  1162. ctrl->hcor = NULL;
  1163. } else {
  1164. *controller = &xhcic[index];
  1165. }
  1166. return ret;
  1167. }
  1168. /**
  1169. * Stops the XHCI host controller
  1170. * and cleans up all the related data structures
  1171. *
  1172. * @param index index to the host controller data structure
  1173. * @return none
  1174. */
  1175. int usb_lowlevel_stop(int index)
  1176. {
  1177. struct xhci_ctrl *ctrl = (xhcic + index);
  1178. if (ctrl->hcor) {
  1179. xhci_lowlevel_stop(ctrl);
  1180. xhci_hcd_stop(index);
  1181. xhci_cleanup(ctrl);
  1182. }
  1183. return 0;
  1184. }
  1185. #endif /* CONFIG_DM_USB */
  1186. #ifdef CONFIG_DM_USB
  1187. static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
  1188. unsigned long pipe, void *buffer, int length,
  1189. struct devrequest *setup)
  1190. {
  1191. struct usb_device *uhop;
  1192. struct udevice *hub;
  1193. int root_portnr = 0;
  1194. debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
  1195. dev->name, udev, udev->dev->name, udev->portnr);
  1196. hub = udev->dev;
  1197. if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
  1198. /* Figure out our port number on the root hub */
  1199. if (usb_hub_is_root_hub(hub)) {
  1200. root_portnr = udev->portnr;
  1201. } else {
  1202. while (!usb_hub_is_root_hub(hub->parent))
  1203. hub = hub->parent;
  1204. uhop = dev_get_parent_priv(hub);
  1205. root_portnr = uhop->portnr;
  1206. }
  1207. }
  1208. /*
  1209. struct usb_device *hop = udev;
  1210. if (hop->parent)
  1211. while (hop->parent->parent)
  1212. hop = hop->parent;
  1213. */
  1214. return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
  1215. root_portnr);
  1216. }
  1217. static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
  1218. unsigned long pipe, void *buffer, int length)
  1219. {
  1220. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1221. return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
  1222. }
  1223. static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
  1224. unsigned long pipe, void *buffer, int length,
  1225. int interval)
  1226. {
  1227. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1228. return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
  1229. }
  1230. static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
  1231. {
  1232. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1233. return _xhci_alloc_device(udev);
  1234. }
  1235. static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev)
  1236. {
  1237. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1238. struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev);
  1239. struct xhci_virt_device *virt_dev;
  1240. struct xhci_input_control_ctx *ctrl_ctx;
  1241. struct xhci_container_ctx *out_ctx;
  1242. struct xhci_container_ctx *in_ctx;
  1243. struct xhci_slot_ctx *slot_ctx;
  1244. int slot_id = udev->slot_id;
  1245. unsigned think_time;
  1246. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1247. /* Ignore root hubs */
  1248. if (usb_hub_is_root_hub(udev->dev))
  1249. return 0;
  1250. virt_dev = ctrl->devs[slot_id];
  1251. BUG_ON(!virt_dev);
  1252. out_ctx = virt_dev->out_ctx;
  1253. in_ctx = virt_dev->in_ctx;
  1254. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  1255. /* Initialize the input context control */
  1256. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  1257. ctrl_ctx->drop_flags = 0;
  1258. xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
  1259. /* slot context */
  1260. xhci_slot_copy(ctrl, in_ctx, out_ctx);
  1261. slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
  1262. /* Update hub related fields */
  1263. slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
  1264. if (hub->tt.multi && udev->speed == USB_SPEED_HIGH)
  1265. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  1266. slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild));
  1267. /*
  1268. * Set TT think time - convert from ns to FS bit times.
  1269. * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns
  1270. *
  1271. * 0 = 8 FS bit times, 1 = 16 FS bit times,
  1272. * 2 = 24 FS bit times, 3 = 32 FS bit times.
  1273. *
  1274. * This field shall be 0 if the device is not a high-spped hub.
  1275. */
  1276. think_time = hub->tt.think_time;
  1277. if (think_time != 0)
  1278. think_time = (think_time / 666) - 1;
  1279. if (udev->speed == USB_SPEED_HIGH)
  1280. slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time));
  1281. return xhci_configure_endpoints(udev, false);
  1282. }
  1283. static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size)
  1284. {
  1285. /*
  1286. * xHCD allocates one segment which includes 64 TRBs for each endpoint
  1287. * and the last TRB in this segment is configured as a link TRB to form
  1288. * a TRB ring. Each TRB can transfer up to 64K bytes, however data
  1289. * buffers referenced by transfer TRBs shall not span 64KB boundaries.
  1290. * Hence the maximum number of TRBs we can use in one transfer is 62.
  1291. */
  1292. *size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE;
  1293. return 0;
  1294. }
  1295. int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
  1296. struct xhci_hcor *hcor)
  1297. {
  1298. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1299. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  1300. int ret;
  1301. debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
  1302. ctrl, hccr, hcor);
  1303. ctrl->dev = dev;
  1304. /*
  1305. * XHCI needs to issue a Address device command to setup
  1306. * proper device context structures, before it can interact
  1307. * with the device. So a get_descriptor will fail before any
  1308. * of that is done for XHCI unlike EHCI.
  1309. */
  1310. priv->desc_before_addr = false;
  1311. ret = xhci_reset(hcor);
  1312. if (ret)
  1313. goto err;
  1314. ctrl->hccr = hccr;
  1315. ctrl->hcor = hcor;
  1316. ret = xhci_lowlevel_init(ctrl);
  1317. if (ret)
  1318. goto err;
  1319. return 0;
  1320. err:
  1321. free(ctrl);
  1322. debug("%s: failed, ret=%d\n", __func__, ret);
  1323. return ret;
  1324. }
  1325. int xhci_deregister(struct udevice *dev)
  1326. {
  1327. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1328. xhci_lowlevel_stop(ctrl);
  1329. xhci_cleanup(ctrl);
  1330. return 0;
  1331. }
  1332. struct dm_usb_ops xhci_usb_ops = {
  1333. .control = xhci_submit_control_msg,
  1334. .bulk = xhci_submit_bulk_msg,
  1335. .interrupt = xhci_submit_int_msg,
  1336. .alloc_device = xhci_alloc_device,
  1337. .update_hub_device = xhci_update_hub_device,
  1338. .get_max_xfer_size = xhci_get_max_xfer_size,
  1339. };
  1340. #endif