dwc2_udc_otg.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * drivers/usb/gadget/dwc2_udc_otg.c
  3. * Designware DWC2 on-chip full/high speed USB OTG 2.0 device controllers
  4. *
  5. * Copyright (C) 2008 for Samsung Electronics
  6. *
  7. * BSP Support for Samsung's UDC driver
  8. * available at:
  9. * git://git.kernel.org/pub/scm/linux/kernel/git/kki_ap/linux-2.6-samsung.git
  10. *
  11. * State machine bugfixes:
  12. * Marek Szyprowski <m.szyprowski@samsung.com>
  13. *
  14. * Ported to u-boot:
  15. * Marek Szyprowski <m.szyprowski@samsung.com>
  16. * Lukasz Majewski <l.majewski@samsumg.com>
  17. *
  18. * SPDX-License-Identifier: GPL-2.0+
  19. */
  20. #undef DEBUG
  21. #include <common.h>
  22. #include <asm/errno.h>
  23. #include <linux/list.h>
  24. #include <malloc.h>
  25. #include <linux/usb/ch9.h>
  26. #include <linux/usb/gadget.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/unaligned.h>
  29. #include <asm/io.h>
  30. #include <asm/mach-types.h>
  31. #include "dwc2_udc_otg_regs.h"
  32. #include "dwc2_udc_otg_priv.h"
  33. #include <usb/lin_gadget_compat.h>
  34. /***********************************************************/
  35. #define OTG_DMA_MODE 1
  36. #define DEBUG_SETUP 0
  37. #define DEBUG_EP0 0
  38. #define DEBUG_ISR 0
  39. #define DEBUG_OUT_EP 0
  40. #define DEBUG_IN_EP 0
  41. #include <usb/dwc2_udc.h>
  42. #define EP0_CON 0
  43. #define EP_MASK 0xF
  44. static char *state_names[] = {
  45. "WAIT_FOR_SETUP",
  46. "DATA_STATE_XMIT",
  47. "DATA_STATE_NEED_ZLP",
  48. "WAIT_FOR_OUT_STATUS",
  49. "DATA_STATE_RECV",
  50. "WAIT_FOR_COMPLETE",
  51. "WAIT_FOR_OUT_COMPLETE",
  52. "WAIT_FOR_IN_COMPLETE",
  53. "WAIT_FOR_NULL_COMPLETE",
  54. };
  55. #define DRIVER_DESC "DWC2 HS USB OTG Device Driver, (c) Samsung Electronics"
  56. #define DRIVER_VERSION "15 March 2009"
  57. struct dwc2_udc *the_controller;
  58. static const char driver_name[] = "dwc2-udc";
  59. static const char driver_desc[] = DRIVER_DESC;
  60. static const char ep0name[] = "ep0-control";
  61. /* Max packet size*/
  62. static unsigned int ep0_fifo_size = 64;
  63. static unsigned int ep_fifo_size = 512;
  64. static unsigned int ep_fifo_size2 = 1024;
  65. static int reset_available = 1;
  66. static struct usb_ctrlrequest *usb_ctrl;
  67. static dma_addr_t usb_ctrl_dma_addr;
  68. /*
  69. Local declarations.
  70. */
  71. static int dwc2_ep_enable(struct usb_ep *ep,
  72. const struct usb_endpoint_descriptor *);
  73. static int dwc2_ep_disable(struct usb_ep *ep);
  74. static struct usb_request *dwc2_alloc_request(struct usb_ep *ep,
  75. gfp_t gfp_flags);
  76. static void dwc2_free_request(struct usb_ep *ep, struct usb_request *);
  77. static int dwc2_queue(struct usb_ep *ep, struct usb_request *, gfp_t gfp_flags);
  78. static int dwc2_dequeue(struct usb_ep *ep, struct usb_request *);
  79. static int dwc2_fifo_status(struct usb_ep *ep);
  80. static void dwc2_fifo_flush(struct usb_ep *ep);
  81. static void dwc2_ep0_read(struct dwc2_udc *dev);
  82. static void dwc2_ep0_kick(struct dwc2_udc *dev, struct dwc2_ep *ep);
  83. static void dwc2_handle_ep0(struct dwc2_udc *dev);
  84. static int dwc2_ep0_write(struct dwc2_udc *dev);
  85. static int write_fifo_ep0(struct dwc2_ep *ep, struct dwc2_request *req);
  86. static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status);
  87. static void stop_activity(struct dwc2_udc *dev,
  88. struct usb_gadget_driver *driver);
  89. static int udc_enable(struct dwc2_udc *dev);
  90. static void udc_set_address(struct dwc2_udc *dev, unsigned char address);
  91. static void reconfig_usbd(struct dwc2_udc *dev);
  92. static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed);
  93. static void nuke(struct dwc2_ep *ep, int status);
  94. static int dwc2_udc_set_halt(struct usb_ep *_ep, int value);
  95. static void dwc2_udc_set_nak(struct dwc2_ep *ep);
  96. void set_udc_gadget_private_data(void *p)
  97. {
  98. debug_cond(DEBUG_SETUP != 0,
  99. "%s: the_controller: 0x%p, p: 0x%p\n", __func__,
  100. the_controller, p);
  101. the_controller->gadget.dev.device_data = p;
  102. }
  103. void *get_udc_gadget_private_data(struct usb_gadget *gadget)
  104. {
  105. return gadget->dev.device_data;
  106. }
  107. static struct usb_ep_ops dwc2_ep_ops = {
  108. .enable = dwc2_ep_enable,
  109. .disable = dwc2_ep_disable,
  110. .alloc_request = dwc2_alloc_request,
  111. .free_request = dwc2_free_request,
  112. .queue = dwc2_queue,
  113. .dequeue = dwc2_dequeue,
  114. .set_halt = dwc2_udc_set_halt,
  115. .fifo_status = dwc2_fifo_status,
  116. .fifo_flush = dwc2_fifo_flush,
  117. };
  118. #define create_proc_files() do {} while (0)
  119. #define remove_proc_files() do {} while (0)
  120. /***********************************************************/
  121. void __iomem *regs_otg;
  122. struct dwc2_usbotg_reg *reg;
  123. bool dfu_usb_get_reset(void)
  124. {
  125. return !!(readl(&reg->gintsts) & INT_RESET);
  126. }
  127. __weak void otg_phy_init(struct dwc2_udc *dev) {}
  128. __weak void otg_phy_off(struct dwc2_udc *dev) {}
  129. /***********************************************************/
  130. #include "dwc2_udc_otg_xfer_dma.c"
  131. /*
  132. * udc_disable - disable USB device controller
  133. */
  134. static void udc_disable(struct dwc2_udc *dev)
  135. {
  136. debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
  137. udc_set_address(dev, 0);
  138. dev->ep0state = WAIT_FOR_SETUP;
  139. dev->gadget.speed = USB_SPEED_UNKNOWN;
  140. dev->usb_address = 0;
  141. otg_phy_off(dev);
  142. }
  143. /*
  144. * udc_reinit - initialize software state
  145. */
  146. static void udc_reinit(struct dwc2_udc *dev)
  147. {
  148. unsigned int i;
  149. debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
  150. /* device/ep0 records init */
  151. INIT_LIST_HEAD(&dev->gadget.ep_list);
  152. INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
  153. dev->ep0state = WAIT_FOR_SETUP;
  154. /* basic endpoint records init */
  155. for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) {
  156. struct dwc2_ep *ep = &dev->ep[i];
  157. if (i != 0)
  158. list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
  159. ep->desc = 0;
  160. ep->stopped = 0;
  161. INIT_LIST_HEAD(&ep->queue);
  162. ep->pio_irqs = 0;
  163. }
  164. /* the rest was statically initialized, and is read-only */
  165. }
  166. #define BYTES2MAXP(x) (x / 8)
  167. #define MAXP2BYTES(x) (x * 8)
  168. /* until it's enabled, this UDC should be completely invisible
  169. * to any USB host.
  170. */
  171. static int udc_enable(struct dwc2_udc *dev)
  172. {
  173. debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
  174. otg_phy_init(dev);
  175. reconfig_usbd(dev);
  176. debug_cond(DEBUG_SETUP != 0,
  177. "DWC2 USB 2.0 OTG Controller Core Initialized : 0x%x\n",
  178. readl(&reg->gintmsk));
  179. dev->gadget.speed = USB_SPEED_UNKNOWN;
  180. return 0;
  181. }
  182. /*
  183. Register entry point for the peripheral controller driver.
  184. */
  185. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  186. {
  187. struct dwc2_udc *dev = the_controller;
  188. int retval = 0;
  189. unsigned long flags = 0;
  190. debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
  191. if (!driver
  192. || (driver->speed != USB_SPEED_FULL
  193. && driver->speed != USB_SPEED_HIGH)
  194. || !driver->bind || !driver->disconnect || !driver->setup)
  195. return -EINVAL;
  196. if (!dev)
  197. return -ENODEV;
  198. if (dev->driver)
  199. return -EBUSY;
  200. spin_lock_irqsave(&dev->lock, flags);
  201. /* first hook up the driver ... */
  202. dev->driver = driver;
  203. spin_unlock_irqrestore(&dev->lock, flags);
  204. if (retval) { /* TODO */
  205. printf("target device_add failed, error %d\n", retval);
  206. return retval;
  207. }
  208. retval = driver->bind(&dev->gadget);
  209. if (retval) {
  210. debug_cond(DEBUG_SETUP != 0,
  211. "%s: bind to driver --> error %d\n",
  212. dev->gadget.name, retval);
  213. dev->driver = 0;
  214. return retval;
  215. }
  216. enable_irq(IRQ_OTG);
  217. debug_cond(DEBUG_SETUP != 0,
  218. "Registered gadget driver %s\n", dev->gadget.name);
  219. udc_enable(dev);
  220. return 0;
  221. }
  222. /*
  223. * Unregister entry point for the peripheral controller driver.
  224. */
  225. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  226. {
  227. struct dwc2_udc *dev = the_controller;
  228. unsigned long flags = 0;
  229. if (!dev)
  230. return -ENODEV;
  231. if (!driver || driver != dev->driver)
  232. return -EINVAL;
  233. spin_lock_irqsave(&dev->lock, flags);
  234. dev->driver = 0;
  235. stop_activity(dev, driver);
  236. spin_unlock_irqrestore(&dev->lock, flags);
  237. driver->unbind(&dev->gadget);
  238. disable_irq(IRQ_OTG);
  239. udc_disable(dev);
  240. return 0;
  241. }
  242. /*
  243. * done - retire a request; caller blocked irqs
  244. */
  245. static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status)
  246. {
  247. unsigned int stopped = ep->stopped;
  248. debug("%s: %s %p, req = %p, stopped = %d\n",
  249. __func__, ep->ep.name, ep, &req->req, stopped);
  250. list_del_init(&req->queue);
  251. if (likely(req->req.status == -EINPROGRESS))
  252. req->req.status = status;
  253. else
  254. status = req->req.status;
  255. if (status && status != -ESHUTDOWN) {
  256. debug("complete %s req %p stat %d len %u/%u\n",
  257. ep->ep.name, &req->req, status,
  258. req->req.actual, req->req.length);
  259. }
  260. /* don't modify queue heads during completion callback */
  261. ep->stopped = 1;
  262. #ifdef DEBUG
  263. printf("calling complete callback\n");
  264. {
  265. int i, len = req->req.length;
  266. printf("pkt[%d] = ", req->req.length);
  267. if (len > 64)
  268. len = 64;
  269. for (i = 0; i < len; i++) {
  270. printf("%02x", ((u8 *)req->req.buf)[i]);
  271. if ((i & 7) == 7)
  272. printf(" ");
  273. }
  274. printf("\n");
  275. }
  276. #endif
  277. spin_unlock(&ep->dev->lock);
  278. req->req.complete(&ep->ep, &req->req);
  279. spin_lock(&ep->dev->lock);
  280. debug("callback completed\n");
  281. ep->stopped = stopped;
  282. }
  283. /*
  284. * nuke - dequeue ALL requests
  285. */
  286. static void nuke(struct dwc2_ep *ep, int status)
  287. {
  288. struct dwc2_request *req;
  289. debug("%s: %s %p\n", __func__, ep->ep.name, ep);
  290. /* called with irqs blocked */
  291. while (!list_empty(&ep->queue)) {
  292. req = list_entry(ep->queue.next, struct dwc2_request, queue);
  293. done(ep, req, status);
  294. }
  295. }
  296. static void stop_activity(struct dwc2_udc *dev,
  297. struct usb_gadget_driver *driver)
  298. {
  299. int i;
  300. /* don't disconnect drivers more than once */
  301. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  302. driver = 0;
  303. dev->gadget.speed = USB_SPEED_UNKNOWN;
  304. /* prevent new request submissions, kill any outstanding requests */
  305. for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) {
  306. struct dwc2_ep *ep = &dev->ep[i];
  307. ep->stopped = 1;
  308. nuke(ep, -ESHUTDOWN);
  309. }
  310. /* report disconnect; the driver is already quiesced */
  311. if (driver) {
  312. spin_unlock(&dev->lock);
  313. driver->disconnect(&dev->gadget);
  314. spin_lock(&dev->lock);
  315. }
  316. /* re-init driver-visible data structures */
  317. udc_reinit(dev);
  318. }
  319. static void reconfig_usbd(struct dwc2_udc *dev)
  320. {
  321. /* 2. Soft-reset OTG Core and then unreset again. */
  322. int i;
  323. unsigned int uTemp = writel(CORE_SOFT_RESET, &reg->grstctl);
  324. uint32_t dflt_gusbcfg;
  325. debug("Reseting OTG controller\n");
  326. dflt_gusbcfg =
  327. 0<<15 /* PHY Low Power Clock sel*/
  328. |1<<14 /* Non-Periodic TxFIFO Rewind Enable*/
  329. |0x5<<10 /* Turnaround time*/
  330. |0<<9 | 0<<8 /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/
  331. /* 1:SRP enable] H1= 1,1*/
  332. |0<<7 /* Ulpi DDR sel*/
  333. |0<<6 /* 0: high speed utmi+, 1: full speed serial*/
  334. |0<<4 /* 0: utmi+, 1:ulpi*/
  335. #ifdef CONFIG_USB_GADGET_DWC2_OTG_PHY_BUS_WIDTH_8
  336. |0<<3 /* phy i/f 0:8bit, 1:16bit*/
  337. #else
  338. |1<<3 /* phy i/f 0:8bit, 1:16bit*/
  339. #endif
  340. |0x7<<0; /* HS/FS Timeout**/
  341. if (dev->pdata->usb_gusbcfg)
  342. dflt_gusbcfg = dev->pdata->usb_gusbcfg;
  343. writel(dflt_gusbcfg, &reg->gusbcfg);
  344. /* 3. Put the OTG device core in the disconnected state.*/
  345. uTemp = readl(&reg->dctl);
  346. uTemp |= SOFT_DISCONNECT;
  347. writel(uTemp, &reg->dctl);
  348. udelay(20);
  349. /* 4. Make the OTG device core exit from the disconnected state.*/
  350. uTemp = readl(&reg->dctl);
  351. uTemp = uTemp & ~SOFT_DISCONNECT;
  352. writel(uTemp, &reg->dctl);
  353. /* 5. Configure OTG Core to initial settings of device mode.*/
  354. /* [][1: full speed(30Mhz) 0:high speed]*/
  355. writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, &reg->dcfg);
  356. mdelay(1);
  357. /* 6. Unmask the core interrupts*/
  358. writel(GINTMSK_INIT, &reg->gintmsk);
  359. /* 7. Set NAK bit of EP0, EP1, EP2*/
  360. writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[EP0_CON].doepctl);
  361. writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[EP0_CON].diepctl);
  362. for (i = 1; i < DWC2_MAX_ENDPOINTS; i++) {
  363. writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[i].doepctl);
  364. writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[i].diepctl);
  365. }
  366. /* 8. Unmask EPO interrupts*/
  367. writel(((1 << EP0_CON) << DAINT_OUT_BIT)
  368. | (1 << EP0_CON), &reg->daintmsk);
  369. /* 9. Unmask device OUT EP common interrupts*/
  370. writel(DOEPMSK_INIT, &reg->doepmsk);
  371. /* 10. Unmask device IN EP common interrupts*/
  372. writel(DIEPMSK_INIT, &reg->diepmsk);
  373. /* 11. Set Rx FIFO Size (in 32-bit words) */
  374. writel(RX_FIFO_SIZE >> 2, &reg->grxfsiz);
  375. /* 12. Set Non Periodic Tx FIFO Size */
  376. writel((NPTX_FIFO_SIZE >> 2) << 16 | ((RX_FIFO_SIZE >> 2)) << 0,
  377. &reg->gnptxfsiz);
  378. for (i = 1; i < DWC2_MAX_HW_ENDPOINTS; i++)
  379. writel((PTX_FIFO_SIZE >> 2) << 16 |
  380. ((RX_FIFO_SIZE + NPTX_FIFO_SIZE +
  381. PTX_FIFO_SIZE*(i-1)) >> 2) << 0,
  382. &reg->dieptxf[i-1]);
  383. /* Flush the RX FIFO */
  384. writel(RX_FIFO_FLUSH, &reg->grstctl);
  385. while (readl(&reg->grstctl) & RX_FIFO_FLUSH)
  386. debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
  387. /* Flush all the Tx FIFO's */
  388. writel(TX_FIFO_FLUSH_ALL, &reg->grstctl);
  389. writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, &reg->grstctl);
  390. while (readl(&reg->grstctl) & TX_FIFO_FLUSH)
  391. debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
  392. /* 13. Clear NAK bit of EP0, EP1, EP2*/
  393. /* For Slave mode*/
  394. /* EP0: Control OUT */
  395. writel(DEPCTL_EPDIS | DEPCTL_CNAK,
  396. &reg->out_endp[EP0_CON].doepctl);
  397. /* 14. Initialize OTG Link Core.*/
  398. writel(GAHBCFG_INIT, &reg->gahbcfg);
  399. }
  400. static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed)
  401. {
  402. unsigned int ep_ctrl;
  403. int i;
  404. if (speed == USB_SPEED_HIGH) {
  405. ep0_fifo_size = 64;
  406. ep_fifo_size = 512;
  407. ep_fifo_size2 = 1024;
  408. dev->gadget.speed = USB_SPEED_HIGH;
  409. } else {
  410. ep0_fifo_size = 64;
  411. ep_fifo_size = 64;
  412. ep_fifo_size2 = 64;
  413. dev->gadget.speed = USB_SPEED_FULL;
  414. }
  415. dev->ep[0].ep.maxpacket = ep0_fifo_size;
  416. for (i = 1; i < DWC2_MAX_ENDPOINTS; i++)
  417. dev->ep[i].ep.maxpacket = ep_fifo_size;
  418. /* EP0 - Control IN (64 bytes)*/
  419. ep_ctrl = readl(&reg->in_endp[EP0_CON].diepctl);
  420. writel(ep_ctrl|(0<<0), &reg->in_endp[EP0_CON].diepctl);
  421. /* EP0 - Control OUT (64 bytes)*/
  422. ep_ctrl = readl(&reg->out_endp[EP0_CON].doepctl);
  423. writel(ep_ctrl|(0<<0), &reg->out_endp[EP0_CON].doepctl);
  424. }
  425. static int dwc2_ep_enable(struct usb_ep *_ep,
  426. const struct usb_endpoint_descriptor *desc)
  427. {
  428. struct dwc2_ep *ep;
  429. struct dwc2_udc *dev;
  430. unsigned long flags = 0;
  431. debug("%s: %p\n", __func__, _ep);
  432. ep = container_of(_ep, struct dwc2_ep, ep);
  433. if (!_ep || !desc || ep->desc || _ep->name == ep0name
  434. || desc->bDescriptorType != USB_DT_ENDPOINT
  435. || ep->bEndpointAddress != desc->bEndpointAddress
  436. || ep_maxpacket(ep) <
  437. le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
  438. debug("%s: bad ep or descriptor\n", __func__);
  439. return -EINVAL;
  440. }
  441. /* xfer types must match, except that interrupt ~= bulk */
  442. if (ep->bmAttributes != desc->bmAttributes
  443. && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
  444. && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
  445. debug("%s: %s type mismatch\n", __func__, _ep->name);
  446. return -EINVAL;
  447. }
  448. /* hardware _could_ do smaller, but driver doesn't */
  449. if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK &&
  450. le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) >
  451. ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) {
  452. debug("%s: bad %s maxpacket\n", __func__, _ep->name);
  453. return -ERANGE;
  454. }
  455. dev = ep->dev;
  456. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  457. debug("%s: bogus device state\n", __func__);
  458. return -ESHUTDOWN;
  459. }
  460. ep->stopped = 0;
  461. ep->desc = desc;
  462. ep->pio_irqs = 0;
  463. ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
  464. /* Reset halt state */
  465. dwc2_udc_set_nak(ep);
  466. dwc2_udc_set_halt(_ep, 0);
  467. spin_lock_irqsave(&ep->dev->lock, flags);
  468. dwc2_udc_ep_activate(ep);
  469. spin_unlock_irqrestore(&ep->dev->lock, flags);
  470. debug("%s: enabled %s, stopped = %d, maxpacket = %d\n",
  471. __func__, _ep->name, ep->stopped, ep->ep.maxpacket);
  472. return 0;
  473. }
  474. /*
  475. * Disable EP
  476. */
  477. static int dwc2_ep_disable(struct usb_ep *_ep)
  478. {
  479. struct dwc2_ep *ep;
  480. unsigned long flags = 0;
  481. debug("%s: %p\n", __func__, _ep);
  482. ep = container_of(_ep, struct dwc2_ep, ep);
  483. if (!_ep || !ep->desc) {
  484. debug("%s: %s not enabled\n", __func__,
  485. _ep ? ep->ep.name : NULL);
  486. return -EINVAL;
  487. }
  488. spin_lock_irqsave(&ep->dev->lock, flags);
  489. /* Nuke all pending requests */
  490. nuke(ep, -ESHUTDOWN);
  491. ep->desc = 0;
  492. ep->stopped = 1;
  493. spin_unlock_irqrestore(&ep->dev->lock, flags);
  494. debug("%s: disabled %s\n", __func__, _ep->name);
  495. return 0;
  496. }
  497. static struct usb_request *dwc2_alloc_request(struct usb_ep *ep,
  498. gfp_t gfp_flags)
  499. {
  500. struct dwc2_request *req;
  501. debug("%s: %s %p\n", __func__, ep->name, ep);
  502. req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req));
  503. if (!req)
  504. return 0;
  505. memset(req, 0, sizeof *req);
  506. INIT_LIST_HEAD(&req->queue);
  507. return &req->req;
  508. }
  509. static void dwc2_free_request(struct usb_ep *ep, struct usb_request *_req)
  510. {
  511. struct dwc2_request *req;
  512. debug("%s: %p\n", __func__, ep);
  513. req = container_of(_req, struct dwc2_request, req);
  514. WARN_ON(!list_empty(&req->queue));
  515. kfree(req);
  516. }
  517. /* dequeue JUST ONE request */
  518. static int dwc2_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  519. {
  520. struct dwc2_ep *ep;
  521. struct dwc2_request *req;
  522. unsigned long flags = 0;
  523. debug("%s: %p\n", __func__, _ep);
  524. ep = container_of(_ep, struct dwc2_ep, ep);
  525. if (!_ep || ep->ep.name == ep0name)
  526. return -EINVAL;
  527. spin_lock_irqsave(&ep->dev->lock, flags);
  528. /* make sure it's actually queued on this endpoint */
  529. list_for_each_entry(req, &ep->queue, queue) {
  530. if (&req->req == _req)
  531. break;
  532. }
  533. if (&req->req != _req) {
  534. spin_unlock_irqrestore(&ep->dev->lock, flags);
  535. return -EINVAL;
  536. }
  537. done(ep, req, -ECONNRESET);
  538. spin_unlock_irqrestore(&ep->dev->lock, flags);
  539. return 0;
  540. }
  541. /*
  542. * Return bytes in EP FIFO
  543. */
  544. static int dwc2_fifo_status(struct usb_ep *_ep)
  545. {
  546. int count = 0;
  547. struct dwc2_ep *ep;
  548. ep = container_of(_ep, struct dwc2_ep, ep);
  549. if (!_ep) {
  550. debug("%s: bad ep\n", __func__);
  551. return -ENODEV;
  552. }
  553. debug("%s: %d\n", __func__, ep_index(ep));
  554. /* LPD can't report unclaimed bytes from IN fifos */
  555. if (ep_is_in(ep))
  556. return -EOPNOTSUPP;
  557. return count;
  558. }
  559. /*
  560. * Flush EP FIFO
  561. */
  562. static void dwc2_fifo_flush(struct usb_ep *_ep)
  563. {
  564. struct dwc2_ep *ep;
  565. ep = container_of(_ep, struct dwc2_ep, ep);
  566. if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
  567. debug("%s: bad ep\n", __func__);
  568. return;
  569. }
  570. debug("%s: %d\n", __func__, ep_index(ep));
  571. }
  572. static const struct usb_gadget_ops dwc2_udc_ops = {
  573. /* current versions must always be self-powered */
  574. };
  575. static struct dwc2_udc memory = {
  576. .usb_address = 0,
  577. .gadget = {
  578. .ops = &dwc2_udc_ops,
  579. .ep0 = &memory.ep[0].ep,
  580. .name = driver_name,
  581. },
  582. /* control endpoint */
  583. .ep[0] = {
  584. .ep = {
  585. .name = ep0name,
  586. .ops = &dwc2_ep_ops,
  587. .maxpacket = EP0_FIFO_SIZE,
  588. },
  589. .dev = &memory,
  590. .bEndpointAddress = 0,
  591. .bmAttributes = 0,
  592. .ep_type = ep_control,
  593. },
  594. /* first group of endpoints */
  595. .ep[1] = {
  596. .ep = {
  597. .name = "ep1in-bulk",
  598. .ops = &dwc2_ep_ops,
  599. .maxpacket = EP_FIFO_SIZE,
  600. },
  601. .dev = &memory,
  602. .bEndpointAddress = USB_DIR_IN | 1,
  603. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  604. .ep_type = ep_bulk_out,
  605. .fifo_num = 1,
  606. },
  607. .ep[2] = {
  608. .ep = {
  609. .name = "ep2out-bulk",
  610. .ops = &dwc2_ep_ops,
  611. .maxpacket = EP_FIFO_SIZE,
  612. },
  613. .dev = &memory,
  614. .bEndpointAddress = USB_DIR_OUT | 2,
  615. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  616. .ep_type = ep_bulk_in,
  617. .fifo_num = 2,
  618. },
  619. .ep[3] = {
  620. .ep = {
  621. .name = "ep3in-int",
  622. .ops = &dwc2_ep_ops,
  623. .maxpacket = EP_FIFO_SIZE,
  624. },
  625. .dev = &memory,
  626. .bEndpointAddress = USB_DIR_IN | 3,
  627. .bmAttributes = USB_ENDPOINT_XFER_INT,
  628. .ep_type = ep_interrupt,
  629. .fifo_num = 3,
  630. },
  631. };
  632. /*
  633. * probe - binds to the platform device
  634. */
  635. int dwc2_udc_probe(struct dwc2_plat_otg_data *pdata)
  636. {
  637. struct dwc2_udc *dev = &memory;
  638. int retval = 0;
  639. debug("%s: %p\n", __func__, pdata);
  640. dev->pdata = pdata;
  641. reg = (struct dwc2_usbotg_reg *)pdata->regs_otg;
  642. /* regs_otg = (void *)pdata->regs_otg; */
  643. dev->gadget.is_dualspeed = 1; /* Hack only*/
  644. dev->gadget.is_otg = 0;
  645. dev->gadget.is_a_peripheral = 0;
  646. dev->gadget.b_hnp_enable = 0;
  647. dev->gadget.a_hnp_support = 0;
  648. dev->gadget.a_alt_hnp_support = 0;
  649. the_controller = dev;
  650. usb_ctrl = memalign(CONFIG_SYS_CACHELINE_SIZE,
  651. ROUND(sizeof(struct usb_ctrlrequest),
  652. CONFIG_SYS_CACHELINE_SIZE));
  653. if (!usb_ctrl) {
  654. error("No memory available for UDC!\n");
  655. return -ENOMEM;
  656. }
  657. usb_ctrl_dma_addr = (dma_addr_t) usb_ctrl;
  658. udc_reinit(dev);
  659. return retval;
  660. }
  661. int usb_gadget_handle_interrupts(int index)
  662. {
  663. u32 intr_status = readl(&reg->gintsts);
  664. u32 gintmsk = readl(&reg->gintmsk);
  665. if (intr_status & gintmsk)
  666. return dwc2_udc_irq(1, (void *)the_controller);
  667. return 0;
  668. }