xhci.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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. ep_ctx[ep_index]->tx_info =
  531. cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
  532. EP_AVG_TRB_LENGTH(avg_trb_len));
  533. }
  534. return xhci_configure_endpoints(udev, false);
  535. }
  536. /**
  537. * Issue an Address Device command (which will issue a SetAddress request to
  538. * the device).
  539. *
  540. * @param udev pointer to the Device Data Structure
  541. * @return 0 if successful else error code on failure
  542. */
  543. static int xhci_address_device(struct usb_device *udev, int root_portnr)
  544. {
  545. int ret = 0;
  546. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  547. struct xhci_slot_ctx *slot_ctx;
  548. struct xhci_input_control_ctx *ctrl_ctx;
  549. struct xhci_virt_device *virt_dev;
  550. int slot_id = udev->slot_id;
  551. union xhci_trb *event;
  552. virt_dev = ctrl->devs[slot_id];
  553. /*
  554. * This is the first Set Address since device plug-in
  555. * so setting up the slot context.
  556. */
  557. debug("Setting up addressable devices %p\n", ctrl->dcbaa);
  558. xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr);
  559. ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
  560. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
  561. ctrl_ctx->drop_flags = 0;
  562. xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV);
  563. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  564. BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
  565. switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
  566. case COMP_CTX_STATE:
  567. case COMP_EBADSLT:
  568. printf("Setup ERROR: address device command for slot %d.\n",
  569. slot_id);
  570. ret = -EINVAL;
  571. break;
  572. case COMP_TX_ERR:
  573. puts("Device not responding to set address.\n");
  574. ret = -EPROTO;
  575. break;
  576. case COMP_DEV_ERR:
  577. puts("ERROR: Incompatible device"
  578. "for address device command.\n");
  579. ret = -ENODEV;
  580. break;
  581. case COMP_SUCCESS:
  582. debug("Successful Address Device command\n");
  583. udev->status = 0;
  584. break;
  585. default:
  586. printf("ERROR: unexpected command completion code 0x%x.\n",
  587. GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
  588. ret = -EINVAL;
  589. break;
  590. }
  591. xhci_acknowledge_event(ctrl);
  592. if (ret < 0)
  593. /*
  594. * TODO: Unsuccessful Address Device command shall leave the
  595. * slot in default state. So, issue Disable Slot command now.
  596. */
  597. return ret;
  598. xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
  599. virt_dev->out_ctx->size);
  600. slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx);
  601. debug("xHC internal address is: %d\n",
  602. le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
  603. return 0;
  604. }
  605. /**
  606. * Issue Enable slot command to the controller to allocate
  607. * device slot and assign the slot id. It fails if the xHC
  608. * ran out of device slots, the Enable Slot command timed out,
  609. * or allocating memory failed.
  610. *
  611. * @param udev pointer to the Device Data Structure
  612. * @return Returns 0 on succes else return error code on failure
  613. */
  614. static int _xhci_alloc_device(struct usb_device *udev)
  615. {
  616. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  617. union xhci_trb *event;
  618. int ret;
  619. /*
  620. * Root hub will be first device to be initailized.
  621. * If this device is root-hub, don't do any xHC related
  622. * stuff.
  623. */
  624. if (ctrl->rootdev == 0) {
  625. udev->speed = USB_SPEED_SUPER;
  626. return 0;
  627. }
  628. xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT);
  629. event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  630. BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
  631. != COMP_SUCCESS);
  632. udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags));
  633. xhci_acknowledge_event(ctrl);
  634. ret = xhci_alloc_virt_device(ctrl, udev->slot_id);
  635. if (ret < 0) {
  636. /*
  637. * TODO: Unsuccessful Address Device command shall leave
  638. * the slot in default. So, issue Disable Slot command now.
  639. */
  640. puts("Could not allocate xHCI USB device data structures\n");
  641. return ret;
  642. }
  643. return 0;
  644. }
  645. #ifndef CONFIG_DM_USB
  646. int usb_alloc_device(struct usb_device *udev)
  647. {
  648. return _xhci_alloc_device(udev);
  649. }
  650. #endif
  651. /*
  652. * Full speed devices may have a max packet size greater than 8 bytes, but the
  653. * USB core doesn't know that until it reads the first 8 bytes of the
  654. * descriptor. If the usb_device's max packet size changes after that point,
  655. * we need to issue an evaluate context command and wait on it.
  656. *
  657. * @param udev pointer to the Device Data Structure
  658. * @return returns the status of the xhci_configure_endpoints
  659. */
  660. int xhci_check_maxpacket(struct usb_device *udev)
  661. {
  662. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  663. unsigned int slot_id = udev->slot_id;
  664. int ep_index = 0; /* control endpoint */
  665. struct xhci_container_ctx *in_ctx;
  666. struct xhci_container_ctx *out_ctx;
  667. struct xhci_input_control_ctx *ctrl_ctx;
  668. struct xhci_ep_ctx *ep_ctx;
  669. int max_packet_size;
  670. int hw_max_packet_size;
  671. int ret = 0;
  672. out_ctx = ctrl->devs[slot_id]->out_ctx;
  673. xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
  674. ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
  675. hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  676. max_packet_size = udev->epmaxpacketin[0];
  677. if (hw_max_packet_size != max_packet_size) {
  678. debug("Max Packet Size for ep 0 changed.\n");
  679. debug("Max packet size in usb_device = %d\n", max_packet_size);
  680. debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size);
  681. debug("Issuing evaluate context command.\n");
  682. /* Set up the modified control endpoint 0 */
  683. xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx,
  684. ctrl->devs[slot_id]->out_ctx, ep_index);
  685. in_ctx = ctrl->devs[slot_id]->in_ctx;
  686. ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
  687. ep_ctx->ep_info2 &= cpu_to_le32(~((0xffff & MAX_PACKET_MASK)
  688. << MAX_PACKET_SHIFT));
  689. ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
  690. /*
  691. * Set up the input context flags for the command
  692. * FIXME: This won't work if a non-default control endpoint
  693. * changes max packet sizes.
  694. */
  695. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  696. ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
  697. ctrl_ctx->drop_flags = 0;
  698. ret = xhci_configure_endpoints(udev, true);
  699. }
  700. return ret;
  701. }
  702. /**
  703. * Clears the Change bits of the Port Status Register
  704. *
  705. * @param wValue request value
  706. * @param wIndex request index
  707. * @param addr address of posrt status register
  708. * @param port_status state of port status register
  709. * @return none
  710. */
  711. static void xhci_clear_port_change_bit(u16 wValue,
  712. u16 wIndex, volatile uint32_t *addr, u32 port_status)
  713. {
  714. char *port_change_bit;
  715. u32 status;
  716. switch (wValue) {
  717. case USB_PORT_FEAT_C_RESET:
  718. status = PORT_RC;
  719. port_change_bit = "reset";
  720. break;
  721. case USB_PORT_FEAT_C_CONNECTION:
  722. status = PORT_CSC;
  723. port_change_bit = "connect";
  724. break;
  725. case USB_PORT_FEAT_C_OVER_CURRENT:
  726. status = PORT_OCC;
  727. port_change_bit = "over-current";
  728. break;
  729. case USB_PORT_FEAT_C_ENABLE:
  730. status = PORT_PEC;
  731. port_change_bit = "enable/disable";
  732. break;
  733. case USB_PORT_FEAT_C_SUSPEND:
  734. status = PORT_PLC;
  735. port_change_bit = "suspend/resume";
  736. break;
  737. default:
  738. /* Should never happen */
  739. return;
  740. }
  741. /* Change bits are all write 1 to clear */
  742. xhci_writel(addr, port_status | status);
  743. port_status = xhci_readl(addr);
  744. debug("clear port %s change, actual port %d status = 0x%x\n",
  745. port_change_bit, wIndex, port_status);
  746. }
  747. /**
  748. * Save Read Only (RO) bits and save read/write bits where
  749. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  750. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  751. *
  752. * @param state state of the Port Status and Control Regsiter
  753. * @return a value that would result in the port being in the
  754. * same state, if the value was written to the port
  755. * status control register.
  756. */
  757. static u32 xhci_port_state_to_neutral(u32 state)
  758. {
  759. /* Save read-only status and port state */
  760. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  761. }
  762. /**
  763. * Submits the Requests to the XHCI Host Controller
  764. *
  765. * @param udev pointer to the USB device structure
  766. * @param pipe contains the DIR_IN or OUT , devnum
  767. * @param buffer buffer to be read/written based on the request
  768. * @return returns 0 if successful else -1 on failure
  769. */
  770. static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
  771. void *buffer, struct devrequest *req)
  772. {
  773. uint8_t tmpbuf[4];
  774. u16 typeReq;
  775. void *srcptr = NULL;
  776. int len, srclen;
  777. uint32_t reg;
  778. volatile uint32_t *status_reg;
  779. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  780. struct xhci_hccr *hccr = ctrl->hccr;
  781. struct xhci_hcor *hcor = ctrl->hcor;
  782. int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1));
  783. if ((req->requesttype & USB_RT_PORT) &&
  784. le16_to_cpu(req->index) > max_ports) {
  785. printf("The request port(%d) exceeds maximum port number\n",
  786. le16_to_cpu(req->index) - 1);
  787. return -EINVAL;
  788. }
  789. status_reg = (volatile uint32_t *)
  790. (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc);
  791. srclen = 0;
  792. typeReq = req->request | req->requesttype << 8;
  793. switch (typeReq) {
  794. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  795. switch (le16_to_cpu(req->value) >> 8) {
  796. case USB_DT_DEVICE:
  797. debug("USB_DT_DEVICE request\n");
  798. srcptr = &descriptor.device;
  799. srclen = 0x12;
  800. break;
  801. case USB_DT_CONFIG:
  802. debug("USB_DT_CONFIG config\n");
  803. srcptr = &descriptor.config;
  804. srclen = 0x19;
  805. break;
  806. case USB_DT_STRING:
  807. debug("USB_DT_STRING config\n");
  808. switch (le16_to_cpu(req->value) & 0xff) {
  809. case 0: /* Language */
  810. srcptr = "\4\3\11\4";
  811. srclen = 4;
  812. break;
  813. case 1: /* Vendor String */
  814. srcptr = "\16\3U\0-\0B\0o\0o\0t\0";
  815. srclen = 14;
  816. break;
  817. case 2: /* Product Name */
  818. srcptr = "\52\3X\0H\0C\0I\0 "
  819. "\0H\0o\0s\0t\0 "
  820. "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
  821. srclen = 42;
  822. break;
  823. default:
  824. printf("unknown value DT_STRING %x\n",
  825. le16_to_cpu(req->value));
  826. goto unknown;
  827. }
  828. break;
  829. default:
  830. printf("unknown value %x\n", le16_to_cpu(req->value));
  831. goto unknown;
  832. }
  833. break;
  834. case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
  835. switch (le16_to_cpu(req->value) >> 8) {
  836. case USB_DT_HUB:
  837. case USB_DT_SS_HUB:
  838. debug("USB_DT_HUB config\n");
  839. srcptr = &descriptor.hub;
  840. srclen = 0x8;
  841. break;
  842. default:
  843. printf("unknown value %x\n", le16_to_cpu(req->value));
  844. goto unknown;
  845. }
  846. break;
  847. case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
  848. debug("USB_REQ_SET_ADDRESS\n");
  849. ctrl->rootdev = le16_to_cpu(req->value);
  850. break;
  851. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  852. /* Do nothing */
  853. break;
  854. case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
  855. tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
  856. tmpbuf[1] = 0;
  857. srcptr = tmpbuf;
  858. srclen = 2;
  859. break;
  860. case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
  861. memset(tmpbuf, 0, 4);
  862. reg = xhci_readl(status_reg);
  863. if (reg & PORT_CONNECT) {
  864. tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
  865. switch (reg & DEV_SPEED_MASK) {
  866. case XDEV_FS:
  867. debug("SPEED = FULLSPEED\n");
  868. break;
  869. case XDEV_LS:
  870. debug("SPEED = LOWSPEED\n");
  871. tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
  872. break;
  873. case XDEV_HS:
  874. debug("SPEED = HIGHSPEED\n");
  875. tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
  876. break;
  877. case XDEV_SS:
  878. debug("SPEED = SUPERSPEED\n");
  879. tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8;
  880. break;
  881. }
  882. }
  883. if (reg & PORT_PE)
  884. tmpbuf[0] |= USB_PORT_STAT_ENABLE;
  885. if ((reg & PORT_PLS_MASK) == XDEV_U3)
  886. tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
  887. if (reg & PORT_OC)
  888. tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
  889. if (reg & PORT_RESET)
  890. tmpbuf[0] |= USB_PORT_STAT_RESET;
  891. if (reg & PORT_POWER)
  892. /*
  893. * XXX: This Port power bit (for USB 3.0 hub)
  894. * we are faking in USB 2.0 hub port status;
  895. * since there's a change in bit positions in
  896. * two:
  897. * USB 2.0 port status PP is at position[8]
  898. * USB 3.0 port status PP is at position[9]
  899. * So, we are still keeping it at position [8]
  900. */
  901. tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
  902. if (reg & PORT_CSC)
  903. tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
  904. if (reg & PORT_PEC)
  905. tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
  906. if (reg & PORT_OCC)
  907. tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
  908. if (reg & PORT_RC)
  909. tmpbuf[2] |= USB_PORT_STAT_C_RESET;
  910. srcptr = tmpbuf;
  911. srclen = 4;
  912. break;
  913. case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  914. reg = xhci_readl(status_reg);
  915. reg = xhci_port_state_to_neutral(reg);
  916. switch (le16_to_cpu(req->value)) {
  917. case USB_PORT_FEAT_ENABLE:
  918. reg |= PORT_PE;
  919. xhci_writel(status_reg, reg);
  920. break;
  921. case USB_PORT_FEAT_POWER:
  922. reg |= PORT_POWER;
  923. xhci_writel(status_reg, reg);
  924. break;
  925. case USB_PORT_FEAT_RESET:
  926. reg |= PORT_RESET;
  927. xhci_writel(status_reg, reg);
  928. break;
  929. default:
  930. printf("unknown feature %x\n", le16_to_cpu(req->value));
  931. goto unknown;
  932. }
  933. break;
  934. case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
  935. reg = xhci_readl(status_reg);
  936. reg = xhci_port_state_to_neutral(reg);
  937. switch (le16_to_cpu(req->value)) {
  938. case USB_PORT_FEAT_ENABLE:
  939. reg &= ~PORT_PE;
  940. break;
  941. case USB_PORT_FEAT_POWER:
  942. reg &= ~PORT_POWER;
  943. break;
  944. case USB_PORT_FEAT_C_RESET:
  945. case USB_PORT_FEAT_C_CONNECTION:
  946. case USB_PORT_FEAT_C_OVER_CURRENT:
  947. case USB_PORT_FEAT_C_ENABLE:
  948. xhci_clear_port_change_bit((le16_to_cpu(req->value)),
  949. le16_to_cpu(req->index),
  950. status_reg, reg);
  951. break;
  952. default:
  953. printf("unknown feature %x\n", le16_to_cpu(req->value));
  954. goto unknown;
  955. }
  956. xhci_writel(status_reg, reg);
  957. break;
  958. default:
  959. puts("Unknown request\n");
  960. goto unknown;
  961. }
  962. debug("scrlen = %d\n req->length = %d\n",
  963. srclen, le16_to_cpu(req->length));
  964. len = min(srclen, (int)le16_to_cpu(req->length));
  965. if (srcptr != NULL && len > 0)
  966. memcpy(buffer, srcptr, len);
  967. else
  968. debug("Len is 0\n");
  969. udev->act_len = len;
  970. udev->status = 0;
  971. return 0;
  972. unknown:
  973. udev->act_len = 0;
  974. udev->status = USB_ST_STALLED;
  975. return -ENODEV;
  976. }
  977. /**
  978. * Submits the INT request to XHCI Host cotroller
  979. *
  980. * @param udev pointer to the USB device
  981. * @param pipe contains the DIR_IN or OUT , devnum
  982. * @param buffer buffer to be read/written based on the request
  983. * @param length length of the buffer
  984. * @param interval interval of the interrupt
  985. * @return 0
  986. */
  987. static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe,
  988. void *buffer, int length, int interval)
  989. {
  990. if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
  991. printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
  992. return -EINVAL;
  993. }
  994. /*
  995. * xHCI uses normal TRBs for both bulk and interrupt. When the
  996. * interrupt endpoint is to be serviced, the xHC will consume
  997. * (at most) one TD. A TD (comprised of sg list entries) can
  998. * take several service intervals to transmit.
  999. */
  1000. return xhci_bulk_tx(udev, pipe, length, buffer);
  1001. }
  1002. /**
  1003. * submit the BULK type of request to the USB Device
  1004. *
  1005. * @param udev pointer to the USB device
  1006. * @param pipe contains the DIR_IN or OUT , devnum
  1007. * @param buffer buffer to be read/written based on the request
  1008. * @param length length of the buffer
  1009. * @return returns 0 if successful else -1 on failure
  1010. */
  1011. static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe,
  1012. void *buffer, int length)
  1013. {
  1014. if (usb_pipetype(pipe) != PIPE_BULK) {
  1015. printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
  1016. return -EINVAL;
  1017. }
  1018. return xhci_bulk_tx(udev, pipe, length, buffer);
  1019. }
  1020. /**
  1021. * submit the control type of request to the Root hub/Device based on the devnum
  1022. *
  1023. * @param udev pointer to the USB device
  1024. * @param pipe contains the DIR_IN or OUT , devnum
  1025. * @param buffer buffer to be read/written based on the request
  1026. * @param length length of the buffer
  1027. * @param setup Request type
  1028. * @param root_portnr Root port number that this device is on
  1029. * @return returns 0 if successful else -1 on failure
  1030. */
  1031. static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe,
  1032. void *buffer, int length,
  1033. struct devrequest *setup, int root_portnr)
  1034. {
  1035. struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
  1036. int ret = 0;
  1037. if (usb_pipetype(pipe) != PIPE_CONTROL) {
  1038. printf("non-control pipe (type=%lu)", usb_pipetype(pipe));
  1039. return -EINVAL;
  1040. }
  1041. if (usb_pipedevice(pipe) == ctrl->rootdev)
  1042. return xhci_submit_root(udev, pipe, buffer, setup);
  1043. if (setup->request == USB_REQ_SET_ADDRESS &&
  1044. (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD)
  1045. return xhci_address_device(udev, root_portnr);
  1046. if (setup->request == USB_REQ_SET_CONFIGURATION &&
  1047. (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1048. ret = xhci_set_configuration(udev);
  1049. if (ret) {
  1050. puts("Failed to configure xHCI endpoint\n");
  1051. return ret;
  1052. }
  1053. }
  1054. return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
  1055. }
  1056. static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
  1057. {
  1058. struct xhci_hccr *hccr;
  1059. struct xhci_hcor *hcor;
  1060. uint32_t val;
  1061. uint32_t val2;
  1062. uint32_t reg;
  1063. hccr = ctrl->hccr;
  1064. hcor = ctrl->hcor;
  1065. /*
  1066. * Program the Number of Device Slots Enabled field in the CONFIG
  1067. * register with the max value of slots the HC can handle.
  1068. */
  1069. val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
  1070. val2 = xhci_readl(&hcor->or_config);
  1071. val |= (val2 & ~HCS_SLOTS_MASK);
  1072. xhci_writel(&hcor->or_config, val);
  1073. /* initializing xhci data structures */
  1074. if (xhci_mem_init(ctrl, hccr, hcor) < 0)
  1075. return -ENOMEM;
  1076. reg = xhci_readl(&hccr->cr_hcsparams1);
  1077. descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
  1078. HCS_MAX_PORTS_SHIFT);
  1079. printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
  1080. /* Port Indicators */
  1081. reg = xhci_readl(&hccr->cr_hccparams);
  1082. if (HCS_INDICATOR(reg))
  1083. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  1084. | 0x80, &descriptor.hub.wHubCharacteristics);
  1085. /* Port Power Control */
  1086. if (HCC_PPC(reg))
  1087. put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
  1088. | 0x01, &descriptor.hub.wHubCharacteristics);
  1089. if (xhci_start(hcor)) {
  1090. xhci_reset(hcor);
  1091. return -ENODEV;
  1092. }
  1093. /* Zero'ing IRQ control register and IRQ pending register */
  1094. xhci_writel(&ctrl->ir_set->irq_control, 0x0);
  1095. xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
  1096. reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
  1097. printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
  1098. return 0;
  1099. }
  1100. static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
  1101. {
  1102. u32 temp;
  1103. xhci_reset(ctrl->hcor);
  1104. debug("// Disabling event ring interrupts\n");
  1105. temp = xhci_readl(&ctrl->hcor->or_usbsts);
  1106. xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
  1107. temp = xhci_readl(&ctrl->ir_set->irq_pending);
  1108. xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
  1109. return 0;
  1110. }
  1111. #ifndef CONFIG_DM_USB
  1112. int submit_control_msg(struct usb_device *udev, unsigned long pipe,
  1113. void *buffer, int length, struct devrequest *setup)
  1114. {
  1115. struct usb_device *hop = udev;
  1116. if (hop->parent)
  1117. while (hop->parent->parent)
  1118. hop = hop->parent;
  1119. return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
  1120. hop->portnr);
  1121. }
  1122. int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  1123. int length)
  1124. {
  1125. return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
  1126. }
  1127. int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  1128. int length, int interval)
  1129. {
  1130. return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
  1131. }
  1132. /**
  1133. * Intialises the XHCI host controller
  1134. * and allocates the necessary data structures
  1135. *
  1136. * @param index index to the host controller data structure
  1137. * @return pointer to the intialised controller
  1138. */
  1139. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  1140. {
  1141. struct xhci_hccr *hccr;
  1142. struct xhci_hcor *hcor;
  1143. struct xhci_ctrl *ctrl;
  1144. int ret;
  1145. *controller = NULL;
  1146. if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0)
  1147. return -ENODEV;
  1148. if (xhci_reset(hcor) != 0)
  1149. return -ENODEV;
  1150. ctrl = &xhcic[index];
  1151. ctrl->hccr = hccr;
  1152. ctrl->hcor = hcor;
  1153. ret = xhci_lowlevel_init(ctrl);
  1154. if (ret) {
  1155. ctrl->hccr = NULL;
  1156. ctrl->hcor = NULL;
  1157. } else {
  1158. *controller = &xhcic[index];
  1159. }
  1160. return ret;
  1161. }
  1162. /**
  1163. * Stops the XHCI host controller
  1164. * and cleans up all the related data structures
  1165. *
  1166. * @param index index to the host controller data structure
  1167. * @return none
  1168. */
  1169. int usb_lowlevel_stop(int index)
  1170. {
  1171. struct xhci_ctrl *ctrl = (xhcic + index);
  1172. if (ctrl->hcor) {
  1173. xhci_lowlevel_stop(ctrl);
  1174. xhci_hcd_stop(index);
  1175. xhci_cleanup(ctrl);
  1176. }
  1177. return 0;
  1178. }
  1179. #endif /* CONFIG_DM_USB */
  1180. #ifdef CONFIG_DM_USB
  1181. static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
  1182. unsigned long pipe, void *buffer, int length,
  1183. struct devrequest *setup)
  1184. {
  1185. struct usb_device *uhop;
  1186. struct udevice *hub;
  1187. int root_portnr = 0;
  1188. debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
  1189. dev->name, udev, udev->dev->name, udev->portnr);
  1190. hub = udev->dev;
  1191. if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
  1192. /* Figure out our port number on the root hub */
  1193. if (usb_hub_is_root_hub(hub)) {
  1194. root_portnr = udev->portnr;
  1195. } else {
  1196. while (!usb_hub_is_root_hub(hub->parent))
  1197. hub = hub->parent;
  1198. uhop = dev_get_parent_priv(hub);
  1199. root_portnr = uhop->portnr;
  1200. }
  1201. }
  1202. /*
  1203. struct usb_device *hop = udev;
  1204. if (hop->parent)
  1205. while (hop->parent->parent)
  1206. hop = hop->parent;
  1207. */
  1208. return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
  1209. root_portnr);
  1210. }
  1211. static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
  1212. unsigned long pipe, void *buffer, int length)
  1213. {
  1214. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1215. return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
  1216. }
  1217. static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
  1218. unsigned long pipe, void *buffer, int length,
  1219. int interval)
  1220. {
  1221. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1222. return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
  1223. }
  1224. static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
  1225. {
  1226. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1227. return _xhci_alloc_device(udev);
  1228. }
  1229. static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev)
  1230. {
  1231. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1232. struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev);
  1233. struct xhci_virt_device *virt_dev;
  1234. struct xhci_input_control_ctx *ctrl_ctx;
  1235. struct xhci_container_ctx *out_ctx;
  1236. struct xhci_container_ctx *in_ctx;
  1237. struct xhci_slot_ctx *slot_ctx;
  1238. int slot_id = udev->slot_id;
  1239. unsigned think_time;
  1240. debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
  1241. /* Ignore root hubs */
  1242. if (usb_hub_is_root_hub(udev->dev))
  1243. return 0;
  1244. virt_dev = ctrl->devs[slot_id];
  1245. BUG_ON(!virt_dev);
  1246. out_ctx = virt_dev->out_ctx;
  1247. in_ctx = virt_dev->in_ctx;
  1248. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  1249. /* Initialize the input context control */
  1250. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  1251. ctrl_ctx->drop_flags = 0;
  1252. xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
  1253. /* slot context */
  1254. xhci_slot_copy(ctrl, in_ctx, out_ctx);
  1255. slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
  1256. /* Update hub related fields */
  1257. slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
  1258. if (hub->tt.multi && udev->speed == USB_SPEED_HIGH)
  1259. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  1260. slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild));
  1261. /*
  1262. * Set TT think time - convert from ns to FS bit times.
  1263. * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns
  1264. *
  1265. * 0 = 8 FS bit times, 1 = 16 FS bit times,
  1266. * 2 = 24 FS bit times, 3 = 32 FS bit times.
  1267. *
  1268. * This field shall be 0 if the device is not a high-spped hub.
  1269. */
  1270. think_time = hub->tt.think_time;
  1271. if (think_time != 0)
  1272. think_time = (think_time / 666) - 1;
  1273. if (udev->speed == USB_SPEED_HIGH)
  1274. slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time));
  1275. return xhci_configure_endpoints(udev, false);
  1276. }
  1277. static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size)
  1278. {
  1279. /*
  1280. * xHCD allocates one segment which includes 64 TRBs for each endpoint
  1281. * and the last TRB in this segment is configured as a link TRB to form
  1282. * a TRB ring. Each TRB can transfer up to 64K bytes, however data
  1283. * buffers referenced by transfer TRBs shall not span 64KB boundaries.
  1284. * Hence the maximum number of TRBs we can use in one transfer is 62.
  1285. */
  1286. *size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE;
  1287. return 0;
  1288. }
  1289. int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
  1290. struct xhci_hcor *hcor)
  1291. {
  1292. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1293. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  1294. int ret;
  1295. debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
  1296. ctrl, hccr, hcor);
  1297. ctrl->dev = dev;
  1298. /*
  1299. * XHCI needs to issue a Address device command to setup
  1300. * proper device context structures, before it can interact
  1301. * with the device. So a get_descriptor will fail before any
  1302. * of that is done for XHCI unlike EHCI.
  1303. */
  1304. priv->desc_before_addr = false;
  1305. ret = xhci_reset(hcor);
  1306. if (ret)
  1307. goto err;
  1308. ctrl->hccr = hccr;
  1309. ctrl->hcor = hcor;
  1310. ret = xhci_lowlevel_init(ctrl);
  1311. if (ret)
  1312. goto err;
  1313. return 0;
  1314. err:
  1315. free(ctrl);
  1316. debug("%s: failed, ret=%d\n", __func__, ret);
  1317. return ret;
  1318. }
  1319. int xhci_deregister(struct udevice *dev)
  1320. {
  1321. struct xhci_ctrl *ctrl = dev_get_priv(dev);
  1322. xhci_lowlevel_stop(ctrl);
  1323. xhci_cleanup(ctrl);
  1324. return 0;
  1325. }
  1326. struct dm_usb_ops xhci_usb_ops = {
  1327. .control = xhci_submit_control_msg,
  1328. .bulk = xhci_submit_bulk_msg,
  1329. .interrupt = xhci_submit_int_msg,
  1330. .alloc_device = xhci_alloc_device,
  1331. .update_hub_device = xhci_update_hub_device,
  1332. .get_max_xfer_size = xhci_get_max_xfer_size,
  1333. };
  1334. #endif