ci_udc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2011, Marvell Semiconductor Inc.
  4. * Lei Wen <leiwen@marvell.com>
  5. *
  6. * Back ported to the 8xx platform (from the 8260 platform) by
  7. * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <config.h>
  12. #include <net.h>
  13. #include <malloc.h>
  14. #include <asm/byteorder.h>
  15. #include <linux/errno.h>
  16. #include <asm/io.h>
  17. #include <asm/unaligned.h>
  18. #include <linux/types.h>
  19. #include <linux/usb/ch9.h>
  20. #include <linux/usb/gadget.h>
  21. #include <usb/ci_udc.h>
  22. #include "../host/ehci.h"
  23. #include "ci_udc.h"
  24. /*
  25. * Check if the system has too long cachelines. If the cachelines are
  26. * longer then 128b, the driver will not be able flush/invalidate data
  27. * cache over separate QH entries. We use 128b because one QH entry is
  28. * 64b long and there are always two QH list entries for each endpoint.
  29. */
  30. #if ARCH_DMA_MINALIGN > 128
  31. #error This driver can not work on systems with caches longer than 128b
  32. #endif
  33. /*
  34. * Every QTD must be individually aligned, since we can program any
  35. * QTD's address into HW. Cache flushing requires ARCH_DMA_MINALIGN,
  36. * and the USB HW requires 32-byte alignment. Align to both:
  37. */
  38. #define ILIST_ALIGN roundup(ARCH_DMA_MINALIGN, 32)
  39. /* Each QTD is this size */
  40. #define ILIST_ENT_RAW_SZ sizeof(struct ept_queue_item)
  41. /*
  42. * Align the size of the QTD too, so we can add this value to each
  43. * QTD's address to get another aligned address.
  44. */
  45. #define ILIST_ENT_SZ roundup(ILIST_ENT_RAW_SZ, ILIST_ALIGN)
  46. /* For each endpoint, we need 2 QTDs, one for each of IN and OUT */
  47. #define ILIST_SZ (NUM_ENDPOINTS * 2 * ILIST_ENT_SZ)
  48. #define EP_MAX_LENGTH_TRANSFER 0x4000
  49. #ifndef DEBUG
  50. #define DBG(x...) do {} while (0)
  51. #else
  52. #define DBG(x...) printf(x)
  53. static const char *reqname(unsigned r)
  54. {
  55. switch (r) {
  56. case USB_REQ_GET_STATUS: return "GET_STATUS";
  57. case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
  58. case USB_REQ_SET_FEATURE: return "SET_FEATURE";
  59. case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
  60. case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
  61. case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
  62. case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
  63. case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
  64. case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
  65. case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
  66. default: return "*UNKNOWN*";
  67. }
  68. }
  69. #endif
  70. static struct usb_endpoint_descriptor ep0_desc = {
  71. .bLength = sizeof(struct usb_endpoint_descriptor),
  72. .bDescriptorType = USB_DT_ENDPOINT,
  73. .bEndpointAddress = USB_DIR_IN,
  74. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  75. };
  76. static int ci_pullup(struct usb_gadget *gadget, int is_on);
  77. static int ci_ep_enable(struct usb_ep *ep,
  78. const struct usb_endpoint_descriptor *desc);
  79. static int ci_ep_disable(struct usb_ep *ep);
  80. static int ci_ep_queue(struct usb_ep *ep,
  81. struct usb_request *req, gfp_t gfp_flags);
  82. static int ci_ep_dequeue(struct usb_ep *ep, struct usb_request *req);
  83. static struct usb_request *
  84. ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
  85. static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
  86. static struct usb_gadget_ops ci_udc_ops = {
  87. .pullup = ci_pullup,
  88. };
  89. static struct usb_ep_ops ci_ep_ops = {
  90. .enable = ci_ep_enable,
  91. .disable = ci_ep_disable,
  92. .queue = ci_ep_queue,
  93. .dequeue = ci_ep_dequeue,
  94. .alloc_request = ci_ep_alloc_request,
  95. .free_request = ci_ep_free_request,
  96. };
  97. /* Init values for USB endpoints. */
  98. static const struct usb_ep ci_ep_init[5] = {
  99. [0] = { /* EP 0 */
  100. .maxpacket = 64,
  101. .name = "ep0",
  102. .ops = &ci_ep_ops,
  103. },
  104. [1] = {
  105. .maxpacket = 512,
  106. .name = "ep1in-bulk",
  107. .ops = &ci_ep_ops,
  108. },
  109. [2] = {
  110. .maxpacket = 512,
  111. .name = "ep2out-bulk",
  112. .ops = &ci_ep_ops,
  113. },
  114. [3] = {
  115. .maxpacket = 512,
  116. .name = "ep3in-int",
  117. .ops = &ci_ep_ops,
  118. },
  119. [4] = {
  120. .maxpacket = 512,
  121. .name = "ep-",
  122. .ops = &ci_ep_ops,
  123. },
  124. };
  125. static struct ci_drv controller = {
  126. .gadget = {
  127. .name = "ci_udc",
  128. .ops = &ci_udc_ops,
  129. .is_dualspeed = 1,
  130. },
  131. };
  132. /**
  133. * ci_get_qh() - return queue head for endpoint
  134. * @ep_num: Endpoint number
  135. * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
  136. *
  137. * This function returns the QH associated with particular endpoint
  138. * and it's direction.
  139. */
  140. static struct ept_queue_head *ci_get_qh(int ep_num, int dir_in)
  141. {
  142. return &controller.epts[(ep_num * 2) + dir_in];
  143. }
  144. /**
  145. * ci_get_qtd() - return queue item for endpoint
  146. * @ep_num: Endpoint number
  147. * @dir_in: Direction of the endpoint (IN = 1, OUT = 0)
  148. *
  149. * This function returns the QH associated with particular endpoint
  150. * and it's direction.
  151. */
  152. static struct ept_queue_item *ci_get_qtd(int ep_num, int dir_in)
  153. {
  154. int index = (ep_num * 2) + dir_in;
  155. uint8_t *imem = controller.items_mem + (index * ILIST_ENT_SZ);
  156. return (struct ept_queue_item *)imem;
  157. }
  158. /**
  159. * ci_flush_qh - flush cache over queue head
  160. * @ep_num: Endpoint number
  161. *
  162. * This function flushes cache over QH for particular endpoint.
  163. */
  164. static void ci_flush_qh(int ep_num)
  165. {
  166. struct ept_queue_head *head = ci_get_qh(ep_num, 0);
  167. const unsigned long start = (unsigned long)head;
  168. const unsigned long end = start + 2 * sizeof(*head);
  169. flush_dcache_range(start, end);
  170. }
  171. /**
  172. * ci_invalidate_qh - invalidate cache over queue head
  173. * @ep_num: Endpoint number
  174. *
  175. * This function invalidates cache over QH for particular endpoint.
  176. */
  177. static void ci_invalidate_qh(int ep_num)
  178. {
  179. struct ept_queue_head *head = ci_get_qh(ep_num, 0);
  180. unsigned long start = (unsigned long)head;
  181. unsigned long end = start + 2 * sizeof(*head);
  182. invalidate_dcache_range(start, end);
  183. }
  184. /**
  185. * ci_flush_qtd - flush cache over queue item
  186. * @ep_num: Endpoint number
  187. *
  188. * This function flushes cache over qTD pair for particular endpoint.
  189. */
  190. static void ci_flush_qtd(int ep_num)
  191. {
  192. struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
  193. const unsigned long start = (unsigned long)item;
  194. const unsigned long end = start + 2 * ILIST_ENT_SZ;
  195. flush_dcache_range(start, end);
  196. }
  197. /**
  198. * ci_flush_td - flush cache over queue item
  199. * @td: td pointer
  200. *
  201. * This function flushes cache for particular transfer descriptor.
  202. */
  203. static void ci_flush_td(struct ept_queue_item *td)
  204. {
  205. const unsigned long start = (unsigned long)td;
  206. const unsigned long end = (unsigned long)td + ILIST_ENT_SZ;
  207. flush_dcache_range(start, end);
  208. }
  209. /**
  210. * ci_invalidate_qtd - invalidate cache over queue item
  211. * @ep_num: Endpoint number
  212. *
  213. * This function invalidates cache over qTD pair for particular endpoint.
  214. */
  215. static void ci_invalidate_qtd(int ep_num)
  216. {
  217. struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
  218. const unsigned long start = (unsigned long)item;
  219. const unsigned long end = start + 2 * ILIST_ENT_SZ;
  220. invalidate_dcache_range(start, end);
  221. }
  222. /**
  223. * ci_invalidate_td - invalidate cache over queue item
  224. * @td: td pointer
  225. *
  226. * This function invalidates cache for particular transfer descriptor.
  227. */
  228. static void ci_invalidate_td(struct ept_queue_item *td)
  229. {
  230. const unsigned long start = (unsigned long)td;
  231. const unsigned long end = start + ILIST_ENT_SZ;
  232. invalidate_dcache_range(start, end);
  233. }
  234. static struct usb_request *
  235. ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
  236. {
  237. struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
  238. int num = -1;
  239. struct ci_req *ci_req;
  240. if (ci_ep->desc)
  241. num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  242. if (num == 0 && controller.ep0_req)
  243. return &controller.ep0_req->req;
  244. ci_req = calloc(1, sizeof(*ci_req));
  245. if (!ci_req)
  246. return NULL;
  247. INIT_LIST_HEAD(&ci_req->queue);
  248. if (num == 0)
  249. controller.ep0_req = ci_req;
  250. return &ci_req->req;
  251. }
  252. static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *req)
  253. {
  254. struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
  255. struct ci_req *ci_req = container_of(req, struct ci_req, req);
  256. int num = -1;
  257. if (ci_ep->desc)
  258. num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  259. if (num == 0) {
  260. if (!controller.ep0_req)
  261. return;
  262. controller.ep0_req = 0;
  263. }
  264. if (ci_req->b_buf)
  265. free(ci_req->b_buf);
  266. free(ci_req);
  267. }
  268. static void ep_enable(int num, int in, int maxpacket)
  269. {
  270. struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
  271. unsigned n;
  272. n = readl(&udc->epctrl[num]);
  273. if (in)
  274. n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
  275. else
  276. n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
  277. if (num != 0) {
  278. struct ept_queue_head *head = ci_get_qh(num, in);
  279. head->config = CONFIG_MAX_PKT(maxpacket) | CONFIG_ZLT;
  280. ci_flush_qh(num);
  281. }
  282. writel(n, &udc->epctrl[num]);
  283. }
  284. static int ci_ep_enable(struct usb_ep *ep,
  285. const struct usb_endpoint_descriptor *desc)
  286. {
  287. struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
  288. int num, in;
  289. num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  290. in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
  291. ci_ep->desc = desc;
  292. if (num) {
  293. int max = get_unaligned_le16(&desc->wMaxPacketSize);
  294. if ((max > 64) && (controller.gadget.speed == USB_SPEED_FULL))
  295. max = 64;
  296. if (ep->maxpacket != max) {
  297. DBG("%s: from %d to %d\n", __func__,
  298. ep->maxpacket, max);
  299. ep->maxpacket = max;
  300. }
  301. }
  302. ep_enable(num, in, ep->maxpacket);
  303. DBG("%s: num=%d maxpacket=%d\n", __func__, num, ep->maxpacket);
  304. return 0;
  305. }
  306. static int ci_ep_disable(struct usb_ep *ep)
  307. {
  308. struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
  309. ci_ep->desc = NULL;
  310. return 0;
  311. }
  312. static int ci_bounce(struct ci_req *ci_req, int in)
  313. {
  314. struct usb_request *req = &ci_req->req;
  315. unsigned long addr = (unsigned long)req->buf;
  316. unsigned long hwaddr;
  317. uint32_t aligned_used_len;
  318. /* Input buffer address is not aligned. */
  319. if (addr & (ARCH_DMA_MINALIGN - 1))
  320. goto align;
  321. /* Input buffer length is not aligned. */
  322. if (req->length & (ARCH_DMA_MINALIGN - 1))
  323. goto align;
  324. /* The buffer is well aligned, only flush cache. */
  325. ci_req->hw_len = req->length;
  326. ci_req->hw_buf = req->buf;
  327. goto flush;
  328. align:
  329. if (ci_req->b_buf && req->length > ci_req->b_len) {
  330. free(ci_req->b_buf);
  331. ci_req->b_buf = 0;
  332. }
  333. if (!ci_req->b_buf) {
  334. ci_req->b_len = roundup(req->length, ARCH_DMA_MINALIGN);
  335. ci_req->b_buf = memalign(ARCH_DMA_MINALIGN, ci_req->b_len);
  336. if (!ci_req->b_buf)
  337. return -ENOMEM;
  338. }
  339. ci_req->hw_len = ci_req->b_len;
  340. ci_req->hw_buf = ci_req->b_buf;
  341. if (in)
  342. memcpy(ci_req->hw_buf, req->buf, req->length);
  343. flush:
  344. hwaddr = (unsigned long)ci_req->hw_buf;
  345. aligned_used_len = roundup(req->length, ARCH_DMA_MINALIGN);
  346. flush_dcache_range(hwaddr, hwaddr + aligned_used_len);
  347. return 0;
  348. }
  349. static void ci_debounce(struct ci_req *ci_req, int in)
  350. {
  351. struct usb_request *req = &ci_req->req;
  352. unsigned long addr = (unsigned long)req->buf;
  353. unsigned long hwaddr = (unsigned long)ci_req->hw_buf;
  354. uint32_t aligned_used_len;
  355. if (in)
  356. return;
  357. aligned_used_len = roundup(req->actual, ARCH_DMA_MINALIGN);
  358. invalidate_dcache_range(hwaddr, hwaddr + aligned_used_len);
  359. if (addr == hwaddr)
  360. return; /* not a bounce */
  361. memcpy(req->buf, ci_req->hw_buf, req->actual);
  362. }
  363. static void ci_ep_submit_next_request(struct ci_ep *ci_ep)
  364. {
  365. struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
  366. struct ept_queue_item *item;
  367. struct ept_queue_head *head;
  368. int bit, num, len, in;
  369. struct ci_req *ci_req;
  370. u8 *buf;
  371. uint32_t len_left, len_this_dtd;
  372. struct ept_queue_item *dtd, *qtd;
  373. ci_ep->req_primed = true;
  374. num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  375. in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
  376. item = ci_get_qtd(num, in);
  377. head = ci_get_qh(num, in);
  378. ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
  379. len = ci_req->req.length;
  380. head->next = (unsigned long)item;
  381. head->info = 0;
  382. ci_req->dtd_count = 0;
  383. buf = ci_req->hw_buf;
  384. len_left = len;
  385. dtd = item;
  386. do {
  387. len_this_dtd = min(len_left, (unsigned)EP_MAX_LENGTH_TRANSFER);
  388. dtd->info = INFO_BYTES(len_this_dtd) | INFO_ACTIVE;
  389. dtd->page0 = (unsigned long)buf;
  390. dtd->page1 = ((unsigned long)buf & 0xfffff000) + 0x1000;
  391. dtd->page2 = ((unsigned long)buf & 0xfffff000) + 0x2000;
  392. dtd->page3 = ((unsigned long)buf & 0xfffff000) + 0x3000;
  393. dtd->page4 = ((unsigned long)buf & 0xfffff000) + 0x4000;
  394. len_left -= len_this_dtd;
  395. buf += len_this_dtd;
  396. if (len_left) {
  397. qtd = (struct ept_queue_item *)
  398. memalign(ILIST_ALIGN, ILIST_ENT_SZ);
  399. dtd->next = (unsigned long)qtd;
  400. dtd = qtd;
  401. memset(dtd, 0, ILIST_ENT_SZ);
  402. }
  403. ci_req->dtd_count++;
  404. } while (len_left);
  405. item = dtd;
  406. /*
  407. * When sending the data for an IN transaction, the attached host
  408. * knows that all data for the IN is sent when one of the following
  409. * occurs:
  410. * a) A zero-length packet is transmitted.
  411. * b) A packet with length that isn't an exact multiple of the ep's
  412. * maxpacket is transmitted.
  413. * c) Enough data is sent to exactly fill the host's maximum expected
  414. * IN transaction size.
  415. *
  416. * One of these conditions MUST apply at the end of an IN transaction,
  417. * or the transaction will not be considered complete by the host. If
  418. * none of (a)..(c) already applies, then we must force (a) to apply
  419. * by explicitly sending an extra zero-length packet.
  420. */
  421. /* IN !a !b !c */
  422. if (in && len && !(len % ci_ep->ep.maxpacket) && ci_req->req.zero) {
  423. /*
  424. * Each endpoint has 2 items allocated, even though typically
  425. * only 1 is used at a time since either an IN or an OUT but
  426. * not both is queued. For an IN transaction, item currently
  427. * points at the second of these items, so we know that we
  428. * can use the other to transmit the extra zero-length packet.
  429. */
  430. struct ept_queue_item *other_item = ci_get_qtd(num, 0);
  431. item->next = (unsigned long)other_item;
  432. item = other_item;
  433. item->info = INFO_ACTIVE;
  434. }
  435. item->next = TERMINATE;
  436. item->info |= INFO_IOC;
  437. ci_flush_qtd(num);
  438. item = (struct ept_queue_item *)(unsigned long)head->next;
  439. while (item->next != TERMINATE) {
  440. ci_flush_td((struct ept_queue_item *)(unsigned long)item->next);
  441. item = (struct ept_queue_item *)(unsigned long)item->next;
  442. }
  443. DBG("ept%d %s queue len %x, req %p, buffer %p\n",
  444. num, in ? "in" : "out", len, ci_req, ci_req->hw_buf);
  445. ci_flush_qh(num);
  446. if (in)
  447. bit = EPT_TX(num);
  448. else
  449. bit = EPT_RX(num);
  450. writel(bit, &udc->epprime);
  451. }
  452. static int ci_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  453. {
  454. struct ci_ep *ci_ep = container_of(_ep, struct ci_ep, ep);
  455. struct ci_req *ci_req;
  456. list_for_each_entry(ci_req, &ci_ep->queue, queue) {
  457. if (&ci_req->req == _req)
  458. break;
  459. }
  460. if (&ci_req->req != _req)
  461. return -EINVAL;
  462. list_del_init(&ci_req->queue);
  463. if (ci_req->req.status == -EINPROGRESS) {
  464. ci_req->req.status = -ECONNRESET;
  465. if (ci_req->req.complete)
  466. ci_req->req.complete(_ep, _req);
  467. }
  468. return 0;
  469. }
  470. static int ci_ep_queue(struct usb_ep *ep,
  471. struct usb_request *req, gfp_t gfp_flags)
  472. {
  473. struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
  474. struct ci_req *ci_req = container_of(req, struct ci_req, req);
  475. int in, ret;
  476. int __maybe_unused num;
  477. num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  478. in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
  479. if (!num && ci_ep->req_primed) {
  480. /*
  481. * The flipping of ep0 between IN and OUT relies on
  482. * ci_ep_queue consuming the current IN/OUT setting
  483. * immediately. If this is deferred to a later point when the
  484. * req is pulled out of ci_req->queue, then the IN/OUT setting
  485. * may have been changed since the req was queued, and state
  486. * will get out of sync. This condition doesn't occur today,
  487. * but could if bugs were introduced later, and this error
  488. * check will save a lot of debugging time.
  489. */
  490. printf("%s: ep0 transaction already in progress\n", __func__);
  491. return -EPROTO;
  492. }
  493. ret = ci_bounce(ci_req, in);
  494. if (ret)
  495. return ret;
  496. DBG("ept%d %s pre-queue req %p, buffer %p\n",
  497. num, in ? "in" : "out", ci_req, ci_req->hw_buf);
  498. list_add_tail(&ci_req->queue, &ci_ep->queue);
  499. if (!ci_ep->req_primed)
  500. ci_ep_submit_next_request(ci_ep);
  501. return 0;
  502. }
  503. static void flip_ep0_direction(void)
  504. {
  505. if (ep0_desc.bEndpointAddress == USB_DIR_IN) {
  506. DBG("%s: Flipping ep0 to OUT\n", __func__);
  507. ep0_desc.bEndpointAddress = 0;
  508. } else {
  509. DBG("%s: Flipping ep0 to IN\n", __func__);
  510. ep0_desc.bEndpointAddress = USB_DIR_IN;
  511. }
  512. }
  513. static void handle_ep_complete(struct ci_ep *ci_ep)
  514. {
  515. struct ept_queue_item *item, *next_td;
  516. int num, in, len, j;
  517. struct ci_req *ci_req;
  518. num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  519. in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
  520. item = ci_get_qtd(num, in);
  521. ci_invalidate_qtd(num);
  522. ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
  523. next_td = item;
  524. len = 0;
  525. for (j = 0; j < ci_req->dtd_count; j++) {
  526. ci_invalidate_td(next_td);
  527. item = next_td;
  528. len += (item->info >> 16) & 0x7fff;
  529. if (item->info & 0xff)
  530. printf("EP%d/%s FAIL info=%x pg0=%x\n",
  531. num, in ? "in" : "out", item->info, item->page0);
  532. if (j != ci_req->dtd_count - 1)
  533. next_td = (struct ept_queue_item *)(unsigned long)
  534. item->next;
  535. if (j != 0)
  536. free(item);
  537. }
  538. list_del_init(&ci_req->queue);
  539. ci_ep->req_primed = false;
  540. if (!list_empty(&ci_ep->queue))
  541. ci_ep_submit_next_request(ci_ep);
  542. ci_req->req.actual = ci_req->req.length - len;
  543. ci_debounce(ci_req, in);
  544. DBG("ept%d %s req %p, complete %x\n",
  545. num, in ? "in" : "out", ci_req, len);
  546. if (num != 0 || controller.ep0_data_phase)
  547. ci_req->req.complete(&ci_ep->ep, &ci_req->req);
  548. if (num == 0 && controller.ep0_data_phase) {
  549. /*
  550. * Data Stage is complete, so flip ep0 dir for Status Stage,
  551. * which always transfers a packet in the opposite direction.
  552. */
  553. DBG("%s: flip ep0 dir for Status Stage\n", __func__);
  554. flip_ep0_direction();
  555. controller.ep0_data_phase = false;
  556. ci_req->req.length = 0;
  557. usb_ep_queue(&ci_ep->ep, &ci_req->req, 0);
  558. }
  559. }
  560. #define SETUP(type, request) (((type) << 8) | (request))
  561. static void handle_setup(void)
  562. {
  563. struct ci_ep *ci_ep = &controller.ep[0];
  564. struct ci_req *ci_req;
  565. struct usb_request *req;
  566. struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
  567. struct ept_queue_head *head;
  568. struct usb_ctrlrequest r;
  569. int status = 0;
  570. int num, in, _num, _in, i;
  571. char *buf;
  572. ci_req = controller.ep0_req;
  573. req = &ci_req->req;
  574. head = ci_get_qh(0, 0); /* EP0 OUT */
  575. ci_invalidate_qh(0);
  576. memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
  577. #ifdef CONFIG_CI_UDC_HAS_HOSTPC
  578. writel(EPT_RX(0), &udc->epsetupstat);
  579. #else
  580. writel(EPT_RX(0), &udc->epstat);
  581. #endif
  582. DBG("handle setup %s, %x, %x index %x value %x length %x\n",
  583. reqname(r.bRequest), r.bRequestType, r.bRequest, r.wIndex,
  584. r.wValue, r.wLength);
  585. /* Set EP0 dir for Data Stage based on Setup Stage data */
  586. if (r.bRequestType & USB_DIR_IN) {
  587. DBG("%s: Set ep0 to IN for Data Stage\n", __func__);
  588. ep0_desc.bEndpointAddress = USB_DIR_IN;
  589. } else {
  590. DBG("%s: Set ep0 to OUT for Data Stage\n", __func__);
  591. ep0_desc.bEndpointAddress = 0;
  592. }
  593. if (r.wLength) {
  594. controller.ep0_data_phase = true;
  595. } else {
  596. /* 0 length -> no Data Stage. Flip dir for Status Stage */
  597. DBG("%s: 0 length: flip ep0 dir for Status Stage\n", __func__);
  598. flip_ep0_direction();
  599. controller.ep0_data_phase = false;
  600. }
  601. list_del_init(&ci_req->queue);
  602. ci_ep->req_primed = false;
  603. switch (SETUP(r.bRequestType, r.bRequest)) {
  604. case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
  605. _num = r.wIndex & 15;
  606. _in = !!(r.wIndex & 0x80);
  607. if ((r.wValue == 0) && (r.wLength == 0)) {
  608. req->length = 0;
  609. for (i = 0; i < NUM_ENDPOINTS; i++) {
  610. struct ci_ep *ep = &controller.ep[i];
  611. if (!ep->desc)
  612. continue;
  613. num = ep->desc->bEndpointAddress
  614. & USB_ENDPOINT_NUMBER_MASK;
  615. in = (ep->desc->bEndpointAddress
  616. & USB_DIR_IN) != 0;
  617. if ((num == _num) && (in == _in)) {
  618. ep_enable(num, in, ep->ep.maxpacket);
  619. usb_ep_queue(controller.gadget.ep0,
  620. req, 0);
  621. break;
  622. }
  623. }
  624. }
  625. return;
  626. case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
  627. /*
  628. * write address delayed (will take effect
  629. * after the next IN txn)
  630. */
  631. writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
  632. req->length = 0;
  633. usb_ep_queue(controller.gadget.ep0, req, 0);
  634. return;
  635. case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
  636. req->length = 2;
  637. buf = (char *)req->buf;
  638. buf[0] = 1 << USB_DEVICE_SELF_POWERED;
  639. buf[1] = 0;
  640. usb_ep_queue(controller.gadget.ep0, req, 0);
  641. return;
  642. }
  643. /* pass request up to the gadget driver */
  644. if (controller.driver)
  645. status = controller.driver->setup(&controller.gadget, &r);
  646. else
  647. status = -ENODEV;
  648. if (!status)
  649. return;
  650. DBG("STALL reqname %s type %x value %x, index %x\n",
  651. reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
  652. writel((1<<16) | (1 << 0), &udc->epctrl[0]);
  653. }
  654. static void stop_activity(void)
  655. {
  656. int i, num, in;
  657. struct ept_queue_head *head;
  658. struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
  659. writel(readl(&udc->epcomp), &udc->epcomp);
  660. #ifdef CONFIG_CI_UDC_HAS_HOSTPC
  661. writel(readl(&udc->epsetupstat), &udc->epsetupstat);
  662. #endif
  663. writel(readl(&udc->epstat), &udc->epstat);
  664. writel(0xffffffff, &udc->epflush);
  665. /* error out any pending reqs */
  666. for (i = 0; i < NUM_ENDPOINTS; i++) {
  667. if (i != 0)
  668. writel(0, &udc->epctrl[i]);
  669. if (controller.ep[i].desc) {
  670. num = controller.ep[i].desc->bEndpointAddress
  671. & USB_ENDPOINT_NUMBER_MASK;
  672. in = (controller.ep[i].desc->bEndpointAddress
  673. & USB_DIR_IN) != 0;
  674. head = ci_get_qh(num, in);
  675. head->info = INFO_ACTIVE;
  676. ci_flush_qh(num);
  677. }
  678. }
  679. }
  680. void udc_irq(void)
  681. {
  682. struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
  683. unsigned n = readl(&udc->usbsts);
  684. writel(n, &udc->usbsts);
  685. int bit, i, num, in;
  686. n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
  687. if (n == 0)
  688. return;
  689. if (n & STS_URI) {
  690. DBG("-- reset --\n");
  691. stop_activity();
  692. }
  693. if (n & STS_SLI)
  694. DBG("-- suspend --\n");
  695. if (n & STS_PCI) {
  696. int max = 64;
  697. int speed = USB_SPEED_FULL;
  698. #ifdef CONFIG_CI_UDC_HAS_HOSTPC
  699. bit = (readl(&udc->hostpc1_devlc) >> 25) & 3;
  700. #else
  701. bit = (readl(&udc->portsc) >> 26) & 3;
  702. #endif
  703. DBG("-- portchange %x %s\n", bit, (bit == 2) ? "High" : "Full");
  704. if (bit == 2) {
  705. speed = USB_SPEED_HIGH;
  706. max = 512;
  707. }
  708. controller.gadget.speed = speed;
  709. for (i = 1; i < NUM_ENDPOINTS; i++) {
  710. if (controller.ep[i].ep.maxpacket > max)
  711. controller.ep[i].ep.maxpacket = max;
  712. }
  713. }
  714. if (n & STS_UEI)
  715. printf("<UEI %x>\n", readl(&udc->epcomp));
  716. if ((n & STS_UI) || (n & STS_UEI)) {
  717. #ifdef CONFIG_CI_UDC_HAS_HOSTPC
  718. n = readl(&udc->epsetupstat);
  719. #else
  720. n = readl(&udc->epstat);
  721. #endif
  722. if (n & EPT_RX(0))
  723. handle_setup();
  724. n = readl(&udc->epcomp);
  725. if (n != 0)
  726. writel(n, &udc->epcomp);
  727. for (i = 0; i < NUM_ENDPOINTS && n; i++) {
  728. if (controller.ep[i].desc) {
  729. num = controller.ep[i].desc->bEndpointAddress
  730. & USB_ENDPOINT_NUMBER_MASK;
  731. in = (controller.ep[i].desc->bEndpointAddress
  732. & USB_DIR_IN) != 0;
  733. bit = (in) ? EPT_TX(num) : EPT_RX(num);
  734. if (n & bit)
  735. handle_ep_complete(&controller.ep[i]);
  736. }
  737. }
  738. }
  739. }
  740. int usb_gadget_handle_interrupts(int index)
  741. {
  742. u32 value;
  743. struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
  744. value = readl(&udc->usbsts);
  745. if (value)
  746. udc_irq();
  747. return value;
  748. }
  749. void udc_disconnect(void)
  750. {
  751. struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
  752. /* disable pullup */
  753. stop_activity();
  754. writel(USBCMD_FS2, &udc->usbcmd);
  755. udelay(800);
  756. if (controller.driver)
  757. controller.driver->disconnect(&controller.gadget);
  758. }
  759. static int ci_pullup(struct usb_gadget *gadget, int is_on)
  760. {
  761. struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
  762. if (is_on) {
  763. /* RESET */
  764. writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
  765. udelay(200);
  766. writel((unsigned long)controller.epts, &udc->epinitaddr);
  767. /* select DEVICE mode */
  768. writel(USBMODE_DEVICE, &udc->usbmode);
  769. #if !defined(CONFIG_USB_GADGET_DUALSPEED)
  770. /* Port force Full-Speed Connect */
  771. setbits_le32(&udc->portsc, PFSC);
  772. #endif
  773. writel(0xffffffff, &udc->epflush);
  774. /* Turn on the USB connection by enabling the pullup resistor */
  775. writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
  776. } else {
  777. udc_disconnect();
  778. }
  779. return 0;
  780. }
  781. static int ci_udc_probe(void)
  782. {
  783. struct ept_queue_head *head;
  784. int i;
  785. const int num = 2 * NUM_ENDPOINTS;
  786. const int eplist_min_align = 4096;
  787. const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
  788. const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
  789. const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
  790. /* The QH list must be aligned to 4096 bytes. */
  791. controller.epts = memalign(eplist_align, eplist_sz);
  792. if (!controller.epts)
  793. return -ENOMEM;
  794. memset(controller.epts, 0, eplist_sz);
  795. controller.items_mem = memalign(ILIST_ALIGN, ILIST_SZ);
  796. if (!controller.items_mem) {
  797. free(controller.epts);
  798. return -ENOMEM;
  799. }
  800. memset(controller.items_mem, 0, ILIST_SZ);
  801. for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
  802. /*
  803. * Configure QH for each endpoint. The structure of the QH list
  804. * is such that each two subsequent fields, N and N+1 where N is
  805. * even, in the QH list represent QH for one endpoint. The Nth
  806. * entry represents OUT configuration and the N+1th entry does
  807. * represent IN configuration of the endpoint.
  808. */
  809. head = controller.epts + i;
  810. if (i < 2)
  811. head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
  812. | CONFIG_ZLT | CONFIG_IOS;
  813. else
  814. head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
  815. | CONFIG_ZLT;
  816. head->next = TERMINATE;
  817. head->info = 0;
  818. if (i & 1) {
  819. ci_flush_qh(i / 2);
  820. ci_flush_qtd(i / 2);
  821. }
  822. }
  823. INIT_LIST_HEAD(&controller.gadget.ep_list);
  824. /* Init EP 0 */
  825. memcpy(&controller.ep[0].ep, &ci_ep_init[0], sizeof(*ci_ep_init));
  826. controller.ep[0].desc = &ep0_desc;
  827. INIT_LIST_HEAD(&controller.ep[0].queue);
  828. controller.ep[0].req_primed = false;
  829. controller.gadget.ep0 = &controller.ep[0].ep;
  830. INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
  831. /* Init EP 1..3 */
  832. for (i = 1; i < 4; i++) {
  833. memcpy(&controller.ep[i].ep, &ci_ep_init[i],
  834. sizeof(*ci_ep_init));
  835. INIT_LIST_HEAD(&controller.ep[i].queue);
  836. controller.ep[i].req_primed = false;
  837. list_add_tail(&controller.ep[i].ep.ep_list,
  838. &controller.gadget.ep_list);
  839. }
  840. /* Init EP 4..n */
  841. for (i = 4; i < NUM_ENDPOINTS; i++) {
  842. memcpy(&controller.ep[i].ep, &ci_ep_init[4],
  843. sizeof(*ci_ep_init));
  844. INIT_LIST_HEAD(&controller.ep[i].queue);
  845. controller.ep[i].req_primed = false;
  846. list_add_tail(&controller.ep[i].ep.ep_list,
  847. &controller.gadget.ep_list);
  848. }
  849. ci_ep_alloc_request(&controller.ep[0].ep, 0);
  850. if (!controller.ep0_req) {
  851. free(controller.items_mem);
  852. free(controller.epts);
  853. return -ENOMEM;
  854. }
  855. return 0;
  856. }
  857. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  858. {
  859. int ret;
  860. if (!driver)
  861. return -EINVAL;
  862. if (!driver->bind || !driver->setup || !driver->disconnect)
  863. return -EINVAL;
  864. if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
  865. return -EINVAL;
  866. #ifdef CONFIG_DM_USB
  867. ret = usb_setup_ehci_gadget(&controller.ctrl);
  868. #else
  869. ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
  870. #endif
  871. if (ret)
  872. return ret;
  873. ret = ci_udc_probe();
  874. if (ret) {
  875. DBG("udc probe failed, returned %d\n", ret);
  876. return ret;
  877. }
  878. ret = driver->bind(&controller.gadget);
  879. if (ret) {
  880. DBG("driver->bind() returned %d\n", ret);
  881. return ret;
  882. }
  883. controller.driver = driver;
  884. return 0;
  885. }
  886. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  887. {
  888. udc_disconnect();
  889. driver->unbind(&controller.gadget);
  890. controller.driver = NULL;
  891. ci_ep_free_request(&controller.ep[0].ep, &controller.ep0_req->req);
  892. free(controller.items_mem);
  893. free(controller.epts);
  894. return 0;
  895. }
  896. bool dfu_usb_get_reset(void)
  897. {
  898. struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
  899. return !!(readl(&udc->usbsts) & STS_URI);
  900. }