ci_udc.c 27 KB

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