xhci-mem.c 22 KB

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