xhci-mem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. #include <common.h>
  17. #include <asm/byteorder.h>
  18. #include <usb.h>
  19. #include <malloc.h>
  20. #include <asm/cache.h>
  21. #include <asm-generic/errno.h>
  22. #include "xhci.h"
  23. #define CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE
  24. /**
  25. * flushes the address passed till the length
  26. *
  27. * @param addr pointer to memory region to be flushed
  28. * @param len the length of the cache line to be flushed
  29. * @return none
  30. */
  31. void xhci_flush_cache(uint32_t addr, u32 len)
  32. {
  33. BUG_ON((void *)addr == NULL || len == 0);
  34. flush_dcache_range(addr & ~(CACHELINE_SIZE - 1),
  35. ALIGN(addr + len, CACHELINE_SIZE));
  36. }
  37. /**
  38. * invalidates the address passed till the length
  39. *
  40. * @param addr pointer to memory region to be invalidates
  41. * @param len the length of the cache line to be invalidated
  42. * @return none
  43. */
  44. void xhci_inval_cache(uint32_t addr, u32 len)
  45. {
  46. BUG_ON((void *)addr == NULL || len == 0);
  47. invalidate_dcache_range(addr & ~(CACHELINE_SIZE - 1),
  48. ALIGN(addr + len, CACHELINE_SIZE));
  49. }
  50. /**
  51. * frees the "segment" pointer passed
  52. *
  53. * @param ptr pointer to "segement" to be freed
  54. * @return none
  55. */
  56. static void xhci_segment_free(struct xhci_segment *seg)
  57. {
  58. free(seg->trbs);
  59. seg->trbs = NULL;
  60. free(seg);
  61. }
  62. /**
  63. * frees the "ring" pointer passed
  64. *
  65. * @param ptr pointer to "ring" to be freed
  66. * @return none
  67. */
  68. static void xhci_ring_free(struct xhci_ring *ring)
  69. {
  70. struct xhci_segment *seg;
  71. struct xhci_segment *first_seg;
  72. BUG_ON(!ring);
  73. first_seg = ring->first_seg;
  74. seg = first_seg->next;
  75. while (seg != first_seg) {
  76. struct xhci_segment *next = seg->next;
  77. xhci_segment_free(seg);
  78. seg = next;
  79. }
  80. xhci_segment_free(first_seg);
  81. free(ring);
  82. }
  83. /**
  84. * frees the "xhci_container_ctx" pointer passed
  85. *
  86. * @param ptr pointer to "xhci_container_ctx" to be freed
  87. * @return none
  88. */
  89. static void xhci_free_container_ctx(struct xhci_container_ctx *ctx)
  90. {
  91. free(ctx->bytes);
  92. free(ctx);
  93. }
  94. /**
  95. * frees the virtual devices for "xhci_ctrl" pointer passed
  96. *
  97. * @param ptr pointer to "xhci_ctrl" whose virtual devices are to be freed
  98. * @return none
  99. */
  100. static void xhci_free_virt_devices(struct xhci_ctrl *ctrl)
  101. {
  102. int i;
  103. int slot_id;
  104. struct xhci_virt_device *virt_dev;
  105. /*
  106. * refactored here to loop through all virt_dev
  107. * Slot ID 0 is reserved
  108. */
  109. for (slot_id = 0; slot_id < MAX_HC_SLOTS; slot_id++) {
  110. virt_dev = ctrl->devs[slot_id];
  111. if (!virt_dev)
  112. continue;
  113. ctrl->dcbaa->dev_context_ptrs[slot_id] = 0;
  114. for (i = 0; i < 31; ++i)
  115. if (virt_dev->eps[i].ring)
  116. xhci_ring_free(virt_dev->eps[i].ring);
  117. if (virt_dev->in_ctx)
  118. xhci_free_container_ctx(virt_dev->in_ctx);
  119. if (virt_dev->out_ctx)
  120. xhci_free_container_ctx(virt_dev->out_ctx);
  121. free(virt_dev);
  122. /* make sure we are pointing to NULL */
  123. ctrl->devs[slot_id] = NULL;
  124. }
  125. }
  126. /**
  127. * frees all the memory allocated
  128. *
  129. * @param ptr pointer to "xhci_ctrl" to be cleaned up
  130. * @return none
  131. */
  132. void xhci_cleanup(struct xhci_ctrl *ctrl)
  133. {
  134. xhci_ring_free(ctrl->event_ring);
  135. xhci_ring_free(ctrl->cmd_ring);
  136. xhci_free_virt_devices(ctrl);
  137. free(ctrl->erst.entries);
  138. free(ctrl->dcbaa);
  139. memset(ctrl, '\0', sizeof(struct xhci_ctrl));
  140. }
  141. /**
  142. * Malloc the aligned memory
  143. *
  144. * @param size size of memory to be allocated
  145. * @return allocates the memory and returns the aligned pointer
  146. */
  147. static void *xhci_malloc(unsigned int size)
  148. {
  149. void *ptr;
  150. size_t cacheline_size = max(XHCI_ALIGNMENT, CACHELINE_SIZE);
  151. ptr = memalign(cacheline_size, ALIGN(size, cacheline_size));
  152. BUG_ON(!ptr);
  153. memset(ptr, '\0', size);
  154. xhci_flush_cache((uint32_t)ptr, size);
  155. return ptr;
  156. }
  157. /**
  158. * Make the prev segment point to the next segment.
  159. * Change the last TRB in the prev segment to be a Link TRB which points to the
  160. * address of the next segment. The caller needs to set any Link TRB
  161. * related flags, such as End TRB, Toggle Cycle, and no snoop.
  162. *
  163. * @param prev pointer to the previous segment
  164. * @param next pointer to the next segment
  165. * @param link_trbs flag to indicate whether to link the trbs or NOT
  166. * @return none
  167. */
  168. static void xhci_link_segments(struct xhci_segment *prev,
  169. struct xhci_segment *next, bool link_trbs)
  170. {
  171. u32 val;
  172. u64 val_64 = 0;
  173. if (!prev || !next)
  174. return;
  175. prev->next = next;
  176. if (link_trbs) {
  177. val_64 = (uintptr_t)next->trbs;
  178. prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = val_64;
  179. /*
  180. * Set the last TRB in the segment to
  181. * have a TRB type ID of Link TRB
  182. */
  183. val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control);
  184. val &= ~TRB_TYPE_BITMASK;
  185. val |= (TRB_LINK << TRB_TYPE_SHIFT);
  186. prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
  187. }
  188. }
  189. /**
  190. * Initialises the Ring's enqueue,dequeue,enq_seg pointers
  191. *
  192. * @param ring pointer to the RING to be intialised
  193. * @return none
  194. */
  195. static void xhci_initialize_ring_info(struct xhci_ring *ring)
  196. {
  197. /*
  198. * The ring is empty, so the enqueue pointer == dequeue pointer
  199. */
  200. ring->enqueue = ring->first_seg->trbs;
  201. ring->enq_seg = ring->first_seg;
  202. ring->dequeue = ring->enqueue;
  203. ring->deq_seg = ring->first_seg;
  204. /*
  205. * The ring is initialized to 0. The producer must write 1 to the
  206. * cycle bit to handover ownership of the TRB, so PCS = 1.
  207. * The consumer must compare CCS to the cycle bit to
  208. * check ownership, so CCS = 1.
  209. */
  210. ring->cycle_state = 1;
  211. }
  212. /**
  213. * Allocates a generic ring segment from the ring pool, sets the dma address,
  214. * initializes the segment to zero, and sets the private next pointer to NULL.
  215. * Section 4.11.1.1:
  216. * "All components of all Command and Transfer TRBs shall be initialized to '0'"
  217. *
  218. * @param none
  219. * @return pointer to the newly allocated SEGMENT
  220. */
  221. static struct xhci_segment *xhci_segment_alloc(void)
  222. {
  223. struct xhci_segment *seg;
  224. seg = (struct xhci_segment *)malloc(sizeof(struct xhci_segment));
  225. BUG_ON(!seg);
  226. seg->trbs = (union xhci_trb *)xhci_malloc(SEGMENT_SIZE);
  227. seg->next = NULL;
  228. return seg;
  229. }
  230. /**
  231. * Create a new ring with zero or more segments.
  232. * TODO: current code only uses one-time-allocated single-segment rings
  233. * of 1KB anyway, so we might as well get rid of all the segment and
  234. * linking code (and maybe increase the size a bit, e.g. 4KB).
  235. *
  236. *
  237. * Link each segment together into a ring.
  238. * Set the end flag and the cycle toggle bit on the last segment.
  239. * See section 4.9.2 and figures 15 and 16 of XHCI spec rev1.0.
  240. *
  241. * @param num_segs number of segments in the ring
  242. * @param link_trbs flag to indicate whether to link the trbs or NOT
  243. * @return pointer to the newly created RING
  244. */
  245. struct xhci_ring *xhci_ring_alloc(unsigned int num_segs, bool link_trbs)
  246. {
  247. struct xhci_ring *ring;
  248. struct xhci_segment *prev;
  249. ring = (struct xhci_ring *)malloc(sizeof(struct xhci_ring));
  250. BUG_ON(!ring);
  251. if (num_segs == 0)
  252. return ring;
  253. ring->first_seg = xhci_segment_alloc();
  254. BUG_ON(!ring->first_seg);
  255. num_segs--;
  256. prev = ring->first_seg;
  257. while (num_segs > 0) {
  258. struct xhci_segment *next;
  259. next = xhci_segment_alloc();
  260. BUG_ON(!next);
  261. xhci_link_segments(prev, next, link_trbs);
  262. prev = next;
  263. num_segs--;
  264. }
  265. xhci_link_segments(prev, ring->first_seg, link_trbs);
  266. if (link_trbs) {
  267. /* See section 4.9.2.1 and 6.4.4.1 */
  268. prev->trbs[TRBS_PER_SEGMENT-1].link.control |=
  269. cpu_to_le32(LINK_TOGGLE);
  270. }
  271. xhci_initialize_ring_info(ring);
  272. return ring;
  273. }
  274. /**
  275. * Allocates the Container context
  276. *
  277. * @param ctrl Host controller data structure
  278. * @param type type of XHCI Container Context
  279. * @return NULL if failed else pointer to the context on success
  280. */
  281. static struct xhci_container_ctx
  282. *xhci_alloc_container_ctx(struct xhci_ctrl *ctrl, int type)
  283. {
  284. struct xhci_container_ctx *ctx;
  285. ctx = (struct xhci_container_ctx *)
  286. malloc(sizeof(struct xhci_container_ctx));
  287. BUG_ON(!ctx);
  288. BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT));
  289. ctx->type = type;
  290. ctx->size = (MAX_EP_CTX_NUM + 1) *
  291. CTX_SIZE(readl(&ctrl->hccr->cr_hccparams));
  292. if (type == XHCI_CTX_TYPE_INPUT)
  293. ctx->size += CTX_SIZE(readl(&ctrl->hccr->cr_hccparams));
  294. ctx->bytes = (u8 *)xhci_malloc(ctx->size);
  295. return ctx;
  296. }
  297. /**
  298. * Allocating virtual device
  299. *
  300. * @param udev pointer to USB deivce structure
  301. * @return 0 on success else -1 on failure
  302. */
  303. int xhci_alloc_virt_device(struct usb_device *udev)
  304. {
  305. u64 byte_64 = 0;
  306. unsigned int slot_id = udev->slot_id;
  307. struct xhci_virt_device *virt_dev;
  308. struct xhci_ctrl *ctrl = udev->controller;
  309. /* Slot ID 0 is reserved */
  310. if (ctrl->devs[slot_id]) {
  311. printf("Virt dev for slot[%d] already allocated\n", slot_id);
  312. return -EEXIST;
  313. }
  314. ctrl->devs[slot_id] = (struct xhci_virt_device *)
  315. malloc(sizeof(struct xhci_virt_device));
  316. if (!ctrl->devs[slot_id]) {
  317. puts("Failed to allocate virtual device\n");
  318. return -ENOMEM;
  319. }
  320. memset(ctrl->devs[slot_id], 0, sizeof(struct xhci_virt_device));
  321. virt_dev = ctrl->devs[slot_id];
  322. /* Allocate the (output) device context that will be used in the HC. */
  323. virt_dev->out_ctx = xhci_alloc_container_ctx(ctrl,
  324. XHCI_CTX_TYPE_DEVICE);
  325. if (!virt_dev->out_ctx) {
  326. puts("Failed to allocate out context for virt dev\n");
  327. return -ENOMEM;
  328. }
  329. /* Allocate the (input) device context for address device command */
  330. virt_dev->in_ctx = xhci_alloc_container_ctx(ctrl,
  331. XHCI_CTX_TYPE_INPUT);
  332. if (!virt_dev->in_ctx) {
  333. puts("Failed to allocate in context for virt dev\n");
  334. return -ENOMEM;
  335. }
  336. /* Allocate endpoint 0 ring */
  337. virt_dev->eps[0].ring = xhci_ring_alloc(1, true);
  338. byte_64 = (uintptr_t)(virt_dev->out_ctx->bytes);
  339. /* Point to output device context in dcbaa. */
  340. ctrl->dcbaa->dev_context_ptrs[slot_id] = byte_64;
  341. xhci_flush_cache((uint32_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
  342. sizeof(__le64));
  343. return 0;
  344. }
  345. /**
  346. * Allocates the necessary data structures
  347. * for XHCI host controller
  348. *
  349. * @param ctrl Host controller data structure
  350. * @param hccr pointer to HOST Controller Control Registers
  351. * @param hcor pointer to HOST Controller Operational Registers
  352. * @return 0 if successful else -1 on failure
  353. */
  354. int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,
  355. struct xhci_hcor *hcor)
  356. {
  357. uint64_t val_64;
  358. uint64_t trb_64;
  359. uint32_t val;
  360. unsigned long deq;
  361. int i;
  362. struct xhci_segment *seg;
  363. /* DCBAA initialization */
  364. ctrl->dcbaa = (struct xhci_device_context_array *)
  365. xhci_malloc(sizeof(struct xhci_device_context_array));
  366. if (ctrl->dcbaa == NULL) {
  367. puts("unable to allocate DCBA\n");
  368. return -ENOMEM;
  369. }
  370. val_64 = (uintptr_t)ctrl->dcbaa;
  371. /* Set the pointer in DCBAA register */
  372. xhci_writeq(&hcor->or_dcbaap, val_64);
  373. /* Command ring control pointer register initialization */
  374. ctrl->cmd_ring = xhci_ring_alloc(1, true);
  375. /* Set the address in the Command Ring Control register */
  376. trb_64 = (uintptr_t)ctrl->cmd_ring->first_seg->trbs;
  377. val_64 = xhci_readq(&hcor->or_crcr);
  378. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  379. (trb_64 & (u64) ~CMD_RING_RSVD_BITS) |
  380. ctrl->cmd_ring->cycle_state;
  381. xhci_writeq(&hcor->or_crcr, val_64);
  382. /* write the address of db register */
  383. val = xhci_readl(&hccr->cr_dboff);
  384. val &= DBOFF_MASK;
  385. ctrl->dba = (struct xhci_doorbell_array *)((char *)hccr + val);
  386. /* write the address of runtime register */
  387. val = xhci_readl(&hccr->cr_rtsoff);
  388. val &= RTSOFF_MASK;
  389. ctrl->run_regs = (struct xhci_run_regs *)((char *)hccr + val);
  390. /* writting the address of ir_set structure */
  391. ctrl->ir_set = &ctrl->run_regs->ir_set[0];
  392. /* Event ring does not maintain link TRB */
  393. ctrl->event_ring = xhci_ring_alloc(ERST_NUM_SEGS, false);
  394. ctrl->erst.entries = (struct xhci_erst_entry *)
  395. xhci_malloc(sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS);
  396. ctrl->erst.num_entries = ERST_NUM_SEGS;
  397. for (val = 0, seg = ctrl->event_ring->first_seg;
  398. val < ERST_NUM_SEGS;
  399. val++) {
  400. trb_64 = 0;
  401. trb_64 = (uintptr_t)seg->trbs;
  402. struct xhci_erst_entry *entry = &ctrl->erst.entries[val];
  403. xhci_writeq(&entry->seg_addr, trb_64);
  404. entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
  405. entry->rsvd = 0;
  406. seg = seg->next;
  407. }
  408. xhci_flush_cache((uint32_t)ctrl->erst.entries,
  409. ERST_NUM_SEGS * sizeof(struct xhci_erst_entry));
  410. deq = (unsigned long)ctrl->event_ring->dequeue;
  411. /* Update HC event ring dequeue pointer */
  412. xhci_writeq(&ctrl->ir_set->erst_dequeue,
  413. (u64)deq & (u64)~ERST_PTR_MASK);
  414. /* set ERST count with the number of entries in the segment table */
  415. val = xhci_readl(&ctrl->ir_set->erst_size);
  416. val &= ERST_SIZE_MASK;
  417. val |= ERST_NUM_SEGS;
  418. xhci_writel(&ctrl->ir_set->erst_size, val);
  419. /* this is the event ring segment table pointer */
  420. val_64 = xhci_readq(&ctrl->ir_set->erst_base);
  421. val_64 &= ERST_PTR_MASK;
  422. val_64 |= ((u32)(ctrl->erst.entries) & ~ERST_PTR_MASK);
  423. xhci_writeq(&ctrl->ir_set->erst_base, val_64);
  424. /* initializing the virtual devices to NULL */
  425. for (i = 0; i < MAX_HC_SLOTS; ++i)
  426. ctrl->devs[i] = NULL;
  427. /*
  428. * Just Zero'ing this register completely,
  429. * or some spurious Device Notification Events
  430. * might screw things here.
  431. */
  432. xhci_writel(&hcor->or_dnctrl, 0x0);
  433. return 0;
  434. }
  435. /**
  436. * Give the input control context for the passed container context
  437. *
  438. * @param ctx pointer to the context
  439. * @return pointer to the Input control context data
  440. */
  441. struct xhci_input_control_ctx
  442. *xhci_get_input_control_ctx(struct xhci_container_ctx *ctx)
  443. {
  444. BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT);
  445. return (struct xhci_input_control_ctx *)ctx->bytes;
  446. }
  447. /**
  448. * Give the slot context for the passed container context
  449. *
  450. * @param ctrl Host controller data structure
  451. * @param ctx pointer to the context
  452. * @return pointer to the slot control context data
  453. */
  454. struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctrl *ctrl,
  455. struct xhci_container_ctx *ctx)
  456. {
  457. if (ctx->type == XHCI_CTX_TYPE_DEVICE)
  458. return (struct xhci_slot_ctx *)ctx->bytes;
  459. return (struct xhci_slot_ctx *)
  460. (ctx->bytes + CTX_SIZE(readl(&ctrl->hccr->cr_hccparams)));
  461. }
  462. /**
  463. * Gets the EP context from based on the ep_index
  464. *
  465. * @param ctrl Host controller data structure
  466. * @param ctx context container
  467. * @param ep_index index of the endpoint
  468. * @return pointer to the End point context
  469. */
  470. struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctrl *ctrl,
  471. struct xhci_container_ctx *ctx,
  472. unsigned int ep_index)
  473. {
  474. /* increment ep index by offset of start of ep ctx array */
  475. ep_index++;
  476. if (ctx->type == XHCI_CTX_TYPE_INPUT)
  477. ep_index++;
  478. return (struct xhci_ep_ctx *)
  479. (ctx->bytes +
  480. (ep_index * CTX_SIZE(readl(&ctrl->hccr->cr_hccparams))));
  481. }
  482. /**
  483. * Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
  484. * Useful when you want to change one particular aspect of the endpoint
  485. * and then issue a configure endpoint command.
  486. *
  487. * @param ctrl Host controller data structure
  488. * @param in_ctx contains the input context
  489. * @param out_ctx contains the input context
  490. * @param ep_index index of the end point
  491. * @return none
  492. */
  493. void xhci_endpoint_copy(struct xhci_ctrl *ctrl,
  494. struct xhci_container_ctx *in_ctx,
  495. struct xhci_container_ctx *out_ctx,
  496. unsigned int ep_index)
  497. {
  498. struct xhci_ep_ctx *out_ep_ctx;
  499. struct xhci_ep_ctx *in_ep_ctx;
  500. out_ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
  501. in_ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
  502. in_ep_ctx->ep_info = out_ep_ctx->ep_info;
  503. in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
  504. in_ep_ctx->deq = out_ep_ctx->deq;
  505. in_ep_ctx->tx_info = out_ep_ctx->tx_info;
  506. }
  507. /**
  508. * Copy output xhci_slot_ctx to the input xhci_slot_ctx.
  509. * Useful when you want to change one particular aspect of the endpoint
  510. * and then issue a configure endpoint command.
  511. * Only the context entries field matters, but
  512. * we'll copy the whole thing anyway.
  513. *
  514. * @param ctrl Host controller data structure
  515. * @param in_ctx contains the inpout context
  516. * @param out_ctx contains the inpout context
  517. * @return none
  518. */
  519. void xhci_slot_copy(struct xhci_ctrl *ctrl, struct xhci_container_ctx *in_ctx,
  520. struct xhci_container_ctx *out_ctx)
  521. {
  522. struct xhci_slot_ctx *in_slot_ctx;
  523. struct xhci_slot_ctx *out_slot_ctx;
  524. in_slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
  525. out_slot_ctx = xhci_get_slot_ctx(ctrl, out_ctx);
  526. in_slot_ctx->dev_info = out_slot_ctx->dev_info;
  527. in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
  528. in_slot_ctx->tt_info = out_slot_ctx->tt_info;
  529. in_slot_ctx->dev_state = out_slot_ctx->dev_state;
  530. }
  531. /**
  532. * Setup an xHCI virtual device for a Set Address command
  533. *
  534. * @param udev pointer to the Device Data Structure
  535. * @return returns negative value on failure else 0 on success
  536. */
  537. void xhci_setup_addressable_virt_dev(struct usb_device *udev)
  538. {
  539. struct usb_device *hop = udev;
  540. struct xhci_virt_device *virt_dev;
  541. struct xhci_ep_ctx *ep0_ctx;
  542. struct xhci_slot_ctx *slot_ctx;
  543. u32 port_num = 0;
  544. u64 trb_64 = 0;
  545. struct xhci_ctrl *ctrl = udev->controller;
  546. virt_dev = ctrl->devs[udev->slot_id];
  547. BUG_ON(!virt_dev);
  548. /* Extract the EP0 and Slot Ctrl */
  549. ep0_ctx = xhci_get_ep_ctx(ctrl, virt_dev->in_ctx, 0);
  550. slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->in_ctx);
  551. /* Only the control endpoint is valid - one endpoint context */
  552. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | 0);
  553. switch (udev->speed) {
  554. case USB_SPEED_SUPER:
  555. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
  556. break;
  557. case USB_SPEED_HIGH:
  558. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
  559. break;
  560. case USB_SPEED_FULL:
  561. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
  562. break;
  563. case USB_SPEED_LOW:
  564. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
  565. break;
  566. default:
  567. /* Speed was set earlier, this shouldn't happen. */
  568. BUG();
  569. }
  570. /* Extract the root hub port number */
  571. if (hop->parent)
  572. while (hop->parent->parent)
  573. hop = hop->parent;
  574. port_num = hop->portnr;
  575. debug("port_num = %d\n", port_num);
  576. slot_ctx->dev_info2 |=
  577. cpu_to_le32(((port_num & ROOT_HUB_PORT_MASK) <<
  578. ROOT_HUB_PORT_SHIFT));
  579. /* Step 4 - ring already allocated */
  580. /* Step 5 */
  581. ep0_ctx->ep_info2 = cpu_to_le32(CTRL_EP << EP_TYPE_SHIFT);
  582. debug("SPEED = %d\n", udev->speed);
  583. switch (udev->speed) {
  584. case USB_SPEED_SUPER:
  585. ep0_ctx->ep_info2 |= cpu_to_le32(((512 & MAX_PACKET_MASK) <<
  586. MAX_PACKET_SHIFT));
  587. debug("Setting Packet size = 512bytes\n");
  588. break;
  589. case USB_SPEED_HIGH:
  590. /* USB core guesses at a 64-byte max packet first for FS devices */
  591. case USB_SPEED_FULL:
  592. ep0_ctx->ep_info2 |= cpu_to_le32(((64 & MAX_PACKET_MASK) <<
  593. MAX_PACKET_SHIFT));
  594. debug("Setting Packet size = 64bytes\n");
  595. break;
  596. case USB_SPEED_LOW:
  597. ep0_ctx->ep_info2 |= cpu_to_le32(((8 & MAX_PACKET_MASK) <<
  598. MAX_PACKET_SHIFT));
  599. debug("Setting Packet size = 8bytes\n");
  600. break;
  601. default:
  602. /* New speed? */
  603. BUG();
  604. }
  605. /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
  606. ep0_ctx->ep_info2 |=
  607. cpu_to_le32(((0 & MAX_BURST_MASK) << MAX_BURST_SHIFT) |
  608. ((3 & ERROR_COUNT_MASK) << ERROR_COUNT_SHIFT));
  609. trb_64 = (uintptr_t)virt_dev->eps[0].ring->first_seg->trbs;
  610. ep0_ctx->deq = cpu_to_le64(trb_64 | virt_dev->eps[0].ring->cycle_state);
  611. /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
  612. xhci_flush_cache((uint32_t)ep0_ctx, sizeof(struct xhci_ep_ctx));
  613. xhci_flush_cache((uint32_t)slot_ctx, sizeof(struct xhci_slot_ctx));
  614. }