fotg210-udc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * FOTG210 UDC Driver supports Bulk transfer so far
  4. *
  5. * Copyright (C) 2013 Faraday Technology Corporation
  6. *
  7. * Author : Yuan-Hsin Chen <yhchen@faraday-tech.com>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/err.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/usb/ch9.h>
  17. #include <linux/usb/gadget.h>
  18. #include <linux/usb/otg.h>
  19. #include <linux/usb/phy.h>
  20. #include "fotg210.h"
  21. #include "fotg210-udc.h"
  22. #define DRIVER_DESC "FOTG210 USB Device Controller Driver"
  23. #define DRIVER_VERSION "30-April-2013"
  24. static const char udc_name[] = "fotg210_udc";
  25. static const char * const fotg210_ep_name[] = {
  26. "ep0", "ep1", "ep2", "ep3", "ep4"};
  27. static void fotg210_ack_int(struct fotg210_udc *fotg210, u32 offset, u32 mask)
  28. {
  29. u32 value = ioread32(fotg210->reg + offset);
  30. value &= ~mask;
  31. iowrite32(value, fotg210->reg + offset);
  32. }
  33. static void fotg210_disable_fifo_int(struct fotg210_ep *ep)
  34. {
  35. u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
  36. if (ep->dir_in)
  37. value |= DMISGR1_MF_IN_INT(ep->epnum - 1);
  38. else
  39. value |= DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
  40. iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
  41. }
  42. static void fotg210_enable_fifo_int(struct fotg210_ep *ep)
  43. {
  44. u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
  45. if (ep->dir_in)
  46. value &= ~DMISGR1_MF_IN_INT(ep->epnum - 1);
  47. else
  48. value &= ~DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
  49. iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
  50. }
  51. static void fotg210_set_cxdone(struct fotg210_udc *fotg210)
  52. {
  53. u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
  54. value |= DCFESR_CX_DONE;
  55. iowrite32(value, fotg210->reg + FOTG210_DCFESR);
  56. }
  57. static void fotg210_done(struct fotg210_ep *ep, struct fotg210_request *req,
  58. int status)
  59. {
  60. list_del_init(&req->queue);
  61. /* don't modify queue heads during completion callback */
  62. if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
  63. req->req.status = -ESHUTDOWN;
  64. else
  65. req->req.status = status;
  66. spin_unlock(&ep->fotg210->lock);
  67. usb_gadget_giveback_request(&ep->ep, &req->req);
  68. spin_lock(&ep->fotg210->lock);
  69. if (ep->epnum) {
  70. if (list_empty(&ep->queue))
  71. fotg210_disable_fifo_int(ep);
  72. } else {
  73. fotg210_set_cxdone(ep->fotg210);
  74. }
  75. }
  76. static void fotg210_fifo_ep_mapping(struct fotg210_ep *ep, u32 epnum,
  77. u32 dir_in)
  78. {
  79. struct fotg210_udc *fotg210 = ep->fotg210;
  80. u32 val;
  81. /* Driver should map an ep to a fifo and then map the fifo
  82. * to the ep. What a brain-damaged design!
  83. */
  84. /* map a fifo to an ep */
  85. val = ioread32(fotg210->reg + FOTG210_EPMAP);
  86. val &= ~EPMAP_FIFONOMSK(epnum, dir_in);
  87. val |= EPMAP_FIFONO(epnum, dir_in);
  88. iowrite32(val, fotg210->reg + FOTG210_EPMAP);
  89. /* map the ep to the fifo */
  90. val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
  91. val &= ~FIFOMAP_EPNOMSK(epnum);
  92. val |= FIFOMAP_EPNO(epnum);
  93. iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
  94. /* enable fifo */
  95. val = ioread32(fotg210->reg + FOTG210_FIFOCF);
  96. val |= FIFOCF_FIFO_EN(epnum - 1);
  97. iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
  98. }
  99. static void fotg210_set_fifo_dir(struct fotg210_ep *ep, u32 epnum, u32 dir_in)
  100. {
  101. struct fotg210_udc *fotg210 = ep->fotg210;
  102. u32 val;
  103. val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
  104. val |= (dir_in ? FIFOMAP_DIRIN(epnum - 1) : FIFOMAP_DIROUT(epnum - 1));
  105. iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
  106. }
  107. static void fotg210_set_tfrtype(struct fotg210_ep *ep, u32 epnum, u32 type)
  108. {
  109. struct fotg210_udc *fotg210 = ep->fotg210;
  110. u32 val;
  111. val = ioread32(fotg210->reg + FOTG210_FIFOCF);
  112. val |= FIFOCF_TYPE(type, epnum - 1);
  113. iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
  114. }
  115. static void fotg210_set_mps(struct fotg210_ep *ep, u32 epnum, u32 mps,
  116. u32 dir_in)
  117. {
  118. struct fotg210_udc *fotg210 = ep->fotg210;
  119. u32 val;
  120. u32 offset = dir_in ? FOTG210_INEPMPSR(epnum) :
  121. FOTG210_OUTEPMPSR(epnum);
  122. val = ioread32(fotg210->reg + offset);
  123. val |= INOUTEPMPSR_MPS(mps);
  124. iowrite32(val, fotg210->reg + offset);
  125. }
  126. static int fotg210_config_ep(struct fotg210_ep *ep,
  127. const struct usb_endpoint_descriptor *desc)
  128. {
  129. struct fotg210_udc *fotg210 = ep->fotg210;
  130. fotg210_set_fifo_dir(ep, ep->epnum, ep->dir_in);
  131. fotg210_set_tfrtype(ep, ep->epnum, ep->type);
  132. fotg210_set_mps(ep, ep->epnum, ep->ep.maxpacket, ep->dir_in);
  133. fotg210_fifo_ep_mapping(ep, ep->epnum, ep->dir_in);
  134. fotg210->ep[ep->epnum] = ep;
  135. return 0;
  136. }
  137. static int fotg210_ep_enable(struct usb_ep *_ep,
  138. const struct usb_endpoint_descriptor *desc)
  139. {
  140. struct fotg210_ep *ep;
  141. ep = container_of(_ep, struct fotg210_ep, ep);
  142. ep->desc = desc;
  143. ep->epnum = usb_endpoint_num(desc);
  144. ep->type = usb_endpoint_type(desc);
  145. ep->dir_in = usb_endpoint_dir_in(desc);
  146. ep->ep.maxpacket = usb_endpoint_maxp(desc);
  147. return fotg210_config_ep(ep, desc);
  148. }
  149. static void fotg210_reset_tseq(struct fotg210_udc *fotg210, u8 epnum)
  150. {
  151. struct fotg210_ep *ep = fotg210->ep[epnum];
  152. u32 value;
  153. void __iomem *reg;
  154. reg = (ep->dir_in) ?
  155. fotg210->reg + FOTG210_INEPMPSR(epnum) :
  156. fotg210->reg + FOTG210_OUTEPMPSR(epnum);
  157. /* Note: Driver needs to set and clear INOUTEPMPSR_RESET_TSEQ
  158. * bit. Controller wouldn't clear this bit. WTF!!!
  159. */
  160. value = ioread32(reg);
  161. value |= INOUTEPMPSR_RESET_TSEQ;
  162. iowrite32(value, reg);
  163. value = ioread32(reg);
  164. value &= ~INOUTEPMPSR_RESET_TSEQ;
  165. iowrite32(value, reg);
  166. }
  167. static int fotg210_ep_release(struct fotg210_ep *ep)
  168. {
  169. if (!ep->epnum)
  170. return 0;
  171. ep->epnum = 0;
  172. ep->stall = 0;
  173. ep->wedged = 0;
  174. fotg210_reset_tseq(ep->fotg210, ep->epnum);
  175. return 0;
  176. }
  177. static int fotg210_ep_disable(struct usb_ep *_ep)
  178. {
  179. struct fotg210_ep *ep;
  180. struct fotg210_request *req;
  181. unsigned long flags;
  182. BUG_ON(!_ep);
  183. ep = container_of(_ep, struct fotg210_ep, ep);
  184. while (!list_empty(&ep->queue)) {
  185. req = list_entry(ep->queue.next,
  186. struct fotg210_request, queue);
  187. spin_lock_irqsave(&ep->fotg210->lock, flags);
  188. fotg210_done(ep, req, -ECONNRESET);
  189. spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  190. }
  191. return fotg210_ep_release(ep);
  192. }
  193. static struct usb_request *fotg210_ep_alloc_request(struct usb_ep *_ep,
  194. gfp_t gfp_flags)
  195. {
  196. struct fotg210_request *req;
  197. req = kzalloc(sizeof(struct fotg210_request), gfp_flags);
  198. if (!req)
  199. return NULL;
  200. INIT_LIST_HEAD(&req->queue);
  201. return &req->req;
  202. }
  203. static void fotg210_ep_free_request(struct usb_ep *_ep,
  204. struct usb_request *_req)
  205. {
  206. struct fotg210_request *req;
  207. req = container_of(_req, struct fotg210_request, req);
  208. kfree(req);
  209. }
  210. static void fotg210_enable_dma(struct fotg210_ep *ep,
  211. dma_addr_t d, u32 len)
  212. {
  213. u32 value;
  214. struct fotg210_udc *fotg210 = ep->fotg210;
  215. /* set transfer length and direction */
  216. value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
  217. value &= ~(DMACPSR1_DMA_LEN(0xFFFF) | DMACPSR1_DMA_TYPE(1));
  218. value |= DMACPSR1_DMA_LEN(len) | DMACPSR1_DMA_TYPE(ep->dir_in);
  219. iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
  220. /* set device DMA target FIFO number */
  221. value = ioread32(fotg210->reg + FOTG210_DMATFNR);
  222. if (ep->epnum)
  223. value |= DMATFNR_ACC_FN(ep->epnum - 1);
  224. else
  225. value |= DMATFNR_ACC_CXF;
  226. iowrite32(value, fotg210->reg + FOTG210_DMATFNR);
  227. /* set DMA memory address */
  228. iowrite32(d, fotg210->reg + FOTG210_DMACPSR2);
  229. /* enable MDMA_EROR and MDMA_CMPLT interrupt */
  230. value = ioread32(fotg210->reg + FOTG210_DMISGR2);
  231. value &= ~(DMISGR2_MDMA_CMPLT | DMISGR2_MDMA_ERROR);
  232. iowrite32(value, fotg210->reg + FOTG210_DMISGR2);
  233. /* start DMA */
  234. value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
  235. value |= DMACPSR1_DMA_START;
  236. iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
  237. }
  238. static void fotg210_disable_dma(struct fotg210_ep *ep)
  239. {
  240. iowrite32(DMATFNR_DISDMA, ep->fotg210->reg + FOTG210_DMATFNR);
  241. }
  242. static void fotg210_wait_dma_done(struct fotg210_ep *ep)
  243. {
  244. u32 value;
  245. do {
  246. value = ioread32(ep->fotg210->reg + FOTG210_DISGR2);
  247. if ((value & DISGR2_USBRST_INT) ||
  248. (value & DISGR2_DMA_ERROR))
  249. goto dma_reset;
  250. } while (!(value & DISGR2_DMA_CMPLT));
  251. fotg210_ack_int(ep->fotg210, FOTG210_DISGR2, DISGR2_DMA_CMPLT);
  252. return;
  253. dma_reset:
  254. value = ioread32(ep->fotg210->reg + FOTG210_DMACPSR1);
  255. value |= DMACPSR1_DMA_ABORT;
  256. iowrite32(value, ep->fotg210->reg + FOTG210_DMACPSR1);
  257. /* reset fifo */
  258. if (ep->epnum) {
  259. value = ioread32(ep->fotg210->reg +
  260. FOTG210_FIBCR(ep->epnum - 1));
  261. value |= FIBCR_FFRST;
  262. iowrite32(value, ep->fotg210->reg +
  263. FOTG210_FIBCR(ep->epnum - 1));
  264. } else {
  265. value = ioread32(ep->fotg210->reg + FOTG210_DCFESR);
  266. value |= DCFESR_CX_CLR;
  267. iowrite32(value, ep->fotg210->reg + FOTG210_DCFESR);
  268. }
  269. }
  270. static void fotg210_start_dma(struct fotg210_ep *ep,
  271. struct fotg210_request *req)
  272. {
  273. struct device *dev = &ep->fotg210->gadget.dev;
  274. dma_addr_t d;
  275. u8 *buffer;
  276. u32 length;
  277. if (ep->epnum) {
  278. if (ep->dir_in) {
  279. buffer = req->req.buf;
  280. length = req->req.length;
  281. } else {
  282. buffer = req->req.buf + req->req.actual;
  283. length = ioread32(ep->fotg210->reg +
  284. FOTG210_FIBCR(ep->epnum - 1)) & FIBCR_BCFX;
  285. if (length > req->req.length - req->req.actual)
  286. length = req->req.length - req->req.actual;
  287. }
  288. } else {
  289. buffer = req->req.buf + req->req.actual;
  290. if (req->req.length - req->req.actual > ep->ep.maxpacket)
  291. length = ep->ep.maxpacket;
  292. else
  293. length = req->req.length - req->req.actual;
  294. }
  295. d = dma_map_single(dev, buffer, length,
  296. ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  297. if (dma_mapping_error(dev, d)) {
  298. pr_err("dma_mapping_error\n");
  299. return;
  300. }
  301. fotg210_enable_dma(ep, d, length);
  302. /* check if dma is done */
  303. fotg210_wait_dma_done(ep);
  304. fotg210_disable_dma(ep);
  305. /* update actual transfer length */
  306. req->req.actual += length;
  307. dma_unmap_single(dev, d, length, DMA_TO_DEVICE);
  308. }
  309. static void fotg210_ep0_queue(struct fotg210_ep *ep,
  310. struct fotg210_request *req)
  311. {
  312. if (!req->req.length) {
  313. fotg210_done(ep, req, 0);
  314. return;
  315. }
  316. if (ep->dir_in) { /* if IN */
  317. fotg210_start_dma(ep, req);
  318. if (req->req.length == req->req.actual)
  319. fotg210_done(ep, req, 0);
  320. } else { /* OUT */
  321. u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR0);
  322. value &= ~DMISGR0_MCX_OUT_INT;
  323. iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR0);
  324. }
  325. }
  326. static int fotg210_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
  327. gfp_t gfp_flags)
  328. {
  329. struct fotg210_ep *ep;
  330. struct fotg210_request *req;
  331. unsigned long flags;
  332. int request = 0;
  333. ep = container_of(_ep, struct fotg210_ep, ep);
  334. req = container_of(_req, struct fotg210_request, req);
  335. if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
  336. return -ESHUTDOWN;
  337. spin_lock_irqsave(&ep->fotg210->lock, flags);
  338. if (list_empty(&ep->queue))
  339. request = 1;
  340. list_add_tail(&req->queue, &ep->queue);
  341. req->req.actual = 0;
  342. req->req.status = -EINPROGRESS;
  343. if (!ep->epnum) /* ep0 */
  344. fotg210_ep0_queue(ep, req);
  345. else if (request && !ep->stall)
  346. fotg210_enable_fifo_int(ep);
  347. spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  348. return 0;
  349. }
  350. static int fotg210_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  351. {
  352. struct fotg210_ep *ep;
  353. struct fotg210_request *req;
  354. unsigned long flags;
  355. ep = container_of(_ep, struct fotg210_ep, ep);
  356. req = container_of(_req, struct fotg210_request, req);
  357. spin_lock_irqsave(&ep->fotg210->lock, flags);
  358. if (!list_empty(&ep->queue))
  359. fotg210_done(ep, req, -ECONNRESET);
  360. spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  361. return 0;
  362. }
  363. static void fotg210_set_epnstall(struct fotg210_ep *ep)
  364. {
  365. struct fotg210_udc *fotg210 = ep->fotg210;
  366. u32 value;
  367. void __iomem *reg;
  368. /* check if IN FIFO is empty before stall */
  369. if (ep->dir_in) {
  370. do {
  371. value = ioread32(fotg210->reg + FOTG210_DCFESR);
  372. } while (!(value & DCFESR_FIFO_EMPTY(ep->epnum - 1)));
  373. }
  374. reg = (ep->dir_in) ?
  375. fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
  376. fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
  377. value = ioread32(reg);
  378. value |= INOUTEPMPSR_STL_EP;
  379. iowrite32(value, reg);
  380. }
  381. static void fotg210_clear_epnstall(struct fotg210_ep *ep)
  382. {
  383. struct fotg210_udc *fotg210 = ep->fotg210;
  384. u32 value;
  385. void __iomem *reg;
  386. reg = (ep->dir_in) ?
  387. fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
  388. fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
  389. value = ioread32(reg);
  390. value &= ~INOUTEPMPSR_STL_EP;
  391. iowrite32(value, reg);
  392. }
  393. static int fotg210_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge)
  394. {
  395. struct fotg210_ep *ep;
  396. struct fotg210_udc *fotg210;
  397. unsigned long flags;
  398. ep = container_of(_ep, struct fotg210_ep, ep);
  399. fotg210 = ep->fotg210;
  400. spin_lock_irqsave(&ep->fotg210->lock, flags);
  401. if (value) {
  402. fotg210_set_epnstall(ep);
  403. ep->stall = 1;
  404. if (wedge)
  405. ep->wedged = 1;
  406. } else {
  407. fotg210_reset_tseq(fotg210, ep->epnum);
  408. fotg210_clear_epnstall(ep);
  409. ep->stall = 0;
  410. ep->wedged = 0;
  411. if (!list_empty(&ep->queue))
  412. fotg210_enable_fifo_int(ep);
  413. }
  414. spin_unlock_irqrestore(&ep->fotg210->lock, flags);
  415. return 0;
  416. }
  417. static int fotg210_ep_set_halt(struct usb_ep *_ep, int value)
  418. {
  419. return fotg210_set_halt_and_wedge(_ep, value, 0);
  420. }
  421. static int fotg210_ep_set_wedge(struct usb_ep *_ep)
  422. {
  423. return fotg210_set_halt_and_wedge(_ep, 1, 1);
  424. }
  425. static void fotg210_ep_fifo_flush(struct usb_ep *_ep)
  426. {
  427. }
  428. static const struct usb_ep_ops fotg210_ep_ops = {
  429. .enable = fotg210_ep_enable,
  430. .disable = fotg210_ep_disable,
  431. .alloc_request = fotg210_ep_alloc_request,
  432. .free_request = fotg210_ep_free_request,
  433. .queue = fotg210_ep_queue,
  434. .dequeue = fotg210_ep_dequeue,
  435. .set_halt = fotg210_ep_set_halt,
  436. .fifo_flush = fotg210_ep_fifo_flush,
  437. .set_wedge = fotg210_ep_set_wedge,
  438. };
  439. static void fotg210_clear_tx0byte(struct fotg210_udc *fotg210)
  440. {
  441. u32 value = ioread32(fotg210->reg + FOTG210_TX0BYTE);
  442. value &= ~(TX0BYTE_EP1 | TX0BYTE_EP2 | TX0BYTE_EP3
  443. | TX0BYTE_EP4);
  444. iowrite32(value, fotg210->reg + FOTG210_TX0BYTE);
  445. }
  446. static void fotg210_clear_rx0byte(struct fotg210_udc *fotg210)
  447. {
  448. u32 value = ioread32(fotg210->reg + FOTG210_RX0BYTE);
  449. value &= ~(RX0BYTE_EP1 | RX0BYTE_EP2 | RX0BYTE_EP3
  450. | RX0BYTE_EP4);
  451. iowrite32(value, fotg210->reg + FOTG210_RX0BYTE);
  452. }
  453. /* read 8-byte setup packet only */
  454. static void fotg210_rdsetupp(struct fotg210_udc *fotg210,
  455. u8 *buffer)
  456. {
  457. int i = 0;
  458. u8 *tmp = buffer;
  459. u32 data;
  460. u32 length = 8;
  461. iowrite32(DMATFNR_ACC_CXF, fotg210->reg + FOTG210_DMATFNR);
  462. for (i = (length >> 2); i > 0; i--) {
  463. data = ioread32(fotg210->reg + FOTG210_CXPORT);
  464. *tmp = data & 0xFF;
  465. *(tmp + 1) = (data >> 8) & 0xFF;
  466. *(tmp + 2) = (data >> 16) & 0xFF;
  467. *(tmp + 3) = (data >> 24) & 0xFF;
  468. tmp = tmp + 4;
  469. }
  470. switch (length % 4) {
  471. case 1:
  472. data = ioread32(fotg210->reg + FOTG210_CXPORT);
  473. *tmp = data & 0xFF;
  474. break;
  475. case 2:
  476. data = ioread32(fotg210->reg + FOTG210_CXPORT);
  477. *tmp = data & 0xFF;
  478. *(tmp + 1) = (data >> 8) & 0xFF;
  479. break;
  480. case 3:
  481. data = ioread32(fotg210->reg + FOTG210_CXPORT);
  482. *tmp = data & 0xFF;
  483. *(tmp + 1) = (data >> 8) & 0xFF;
  484. *(tmp + 2) = (data >> 16) & 0xFF;
  485. break;
  486. default:
  487. break;
  488. }
  489. iowrite32(DMATFNR_DISDMA, fotg210->reg + FOTG210_DMATFNR);
  490. }
  491. static void fotg210_set_configuration(struct fotg210_udc *fotg210)
  492. {
  493. u32 value = ioread32(fotg210->reg + FOTG210_DAR);
  494. value |= DAR_AFT_CONF;
  495. iowrite32(value, fotg210->reg + FOTG210_DAR);
  496. }
  497. static void fotg210_set_dev_addr(struct fotg210_udc *fotg210, u32 addr)
  498. {
  499. u32 value = ioread32(fotg210->reg + FOTG210_DAR);
  500. value |= (addr & 0x7F);
  501. iowrite32(value, fotg210->reg + FOTG210_DAR);
  502. }
  503. static void fotg210_set_cxstall(struct fotg210_udc *fotg210)
  504. {
  505. u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
  506. value |= DCFESR_CX_STL;
  507. iowrite32(value, fotg210->reg + FOTG210_DCFESR);
  508. }
  509. static void fotg210_request_error(struct fotg210_udc *fotg210)
  510. {
  511. fotg210_set_cxstall(fotg210);
  512. pr_err("request error!!\n");
  513. }
  514. static void fotg210_set_address(struct fotg210_udc *fotg210,
  515. struct usb_ctrlrequest *ctrl)
  516. {
  517. if (le16_to_cpu(ctrl->wValue) >= 0x0100) {
  518. fotg210_request_error(fotg210);
  519. } else {
  520. fotg210_set_dev_addr(fotg210, le16_to_cpu(ctrl->wValue));
  521. fotg210_set_cxdone(fotg210);
  522. }
  523. }
  524. static void fotg210_set_feature(struct fotg210_udc *fotg210,
  525. struct usb_ctrlrequest *ctrl)
  526. {
  527. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  528. case USB_RECIP_DEVICE:
  529. fotg210_set_cxdone(fotg210);
  530. break;
  531. case USB_RECIP_INTERFACE:
  532. fotg210_set_cxdone(fotg210);
  533. break;
  534. case USB_RECIP_ENDPOINT: {
  535. u8 epnum;
  536. epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  537. if (epnum)
  538. fotg210_set_epnstall(fotg210->ep[epnum]);
  539. else
  540. fotg210_set_cxstall(fotg210);
  541. fotg210_set_cxdone(fotg210);
  542. }
  543. break;
  544. default:
  545. fotg210_request_error(fotg210);
  546. break;
  547. }
  548. }
  549. static void fotg210_clear_feature(struct fotg210_udc *fotg210,
  550. struct usb_ctrlrequest *ctrl)
  551. {
  552. struct fotg210_ep *ep =
  553. fotg210->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK];
  554. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  555. case USB_RECIP_DEVICE:
  556. fotg210_set_cxdone(fotg210);
  557. break;
  558. case USB_RECIP_INTERFACE:
  559. fotg210_set_cxdone(fotg210);
  560. break;
  561. case USB_RECIP_ENDPOINT:
  562. if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) {
  563. if (ep->wedged) {
  564. fotg210_set_cxdone(fotg210);
  565. break;
  566. }
  567. if (ep->stall)
  568. fotg210_set_halt_and_wedge(&ep->ep, 0, 0);
  569. }
  570. fotg210_set_cxdone(fotg210);
  571. break;
  572. default:
  573. fotg210_request_error(fotg210);
  574. break;
  575. }
  576. }
  577. static int fotg210_is_epnstall(struct fotg210_ep *ep)
  578. {
  579. struct fotg210_udc *fotg210 = ep->fotg210;
  580. u32 value;
  581. void __iomem *reg;
  582. reg = (ep->dir_in) ?
  583. fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
  584. fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
  585. value = ioread32(reg);
  586. return value & INOUTEPMPSR_STL_EP ? 1 : 0;
  587. }
  588. /* For EP0 requests triggered by this driver (currently GET_STATUS response) */
  589. static void fotg210_ep0_complete(struct usb_ep *_ep, struct usb_request *req)
  590. {
  591. struct fotg210_ep *ep;
  592. struct fotg210_udc *fotg210;
  593. ep = container_of(_ep, struct fotg210_ep, ep);
  594. fotg210 = ep->fotg210;
  595. if (req->status || req->actual != req->length) {
  596. dev_warn(&fotg210->gadget.dev, "EP0 request failed: %d\n", req->status);
  597. }
  598. }
  599. static void fotg210_get_status(struct fotg210_udc *fotg210,
  600. struct usb_ctrlrequest *ctrl)
  601. {
  602. u8 epnum;
  603. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  604. case USB_RECIP_DEVICE:
  605. fotg210->ep0_data = cpu_to_le16(1 << USB_DEVICE_SELF_POWERED);
  606. break;
  607. case USB_RECIP_INTERFACE:
  608. fotg210->ep0_data = cpu_to_le16(0);
  609. break;
  610. case USB_RECIP_ENDPOINT:
  611. epnum = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK;
  612. if (epnum)
  613. fotg210->ep0_data =
  614. cpu_to_le16(fotg210_is_epnstall(fotg210->ep[epnum])
  615. << USB_ENDPOINT_HALT);
  616. else
  617. fotg210_request_error(fotg210);
  618. break;
  619. default:
  620. fotg210_request_error(fotg210);
  621. return; /* exit */
  622. }
  623. fotg210->ep0_req->buf = &fotg210->ep0_data;
  624. fotg210->ep0_req->length = 2;
  625. spin_unlock(&fotg210->lock);
  626. fotg210_ep_queue(fotg210->gadget.ep0, fotg210->ep0_req, GFP_ATOMIC);
  627. spin_lock(&fotg210->lock);
  628. }
  629. static int fotg210_setup_packet(struct fotg210_udc *fotg210,
  630. struct usb_ctrlrequest *ctrl)
  631. {
  632. u8 *p = (u8 *)ctrl;
  633. u8 ret = 0;
  634. fotg210_rdsetupp(fotg210, p);
  635. fotg210->ep[0]->dir_in = ctrl->bRequestType & USB_DIR_IN;
  636. if (fotg210->gadget.speed == USB_SPEED_UNKNOWN) {
  637. u32 value = ioread32(fotg210->reg + FOTG210_DMCR);
  638. fotg210->gadget.speed = value & DMCR_HS_EN ?
  639. USB_SPEED_HIGH : USB_SPEED_FULL;
  640. }
  641. /* check request */
  642. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  643. switch (ctrl->bRequest) {
  644. case USB_REQ_GET_STATUS:
  645. fotg210_get_status(fotg210, ctrl);
  646. break;
  647. case USB_REQ_CLEAR_FEATURE:
  648. fotg210_clear_feature(fotg210, ctrl);
  649. break;
  650. case USB_REQ_SET_FEATURE:
  651. fotg210_set_feature(fotg210, ctrl);
  652. break;
  653. case USB_REQ_SET_ADDRESS:
  654. fotg210_set_address(fotg210, ctrl);
  655. break;
  656. case USB_REQ_SET_CONFIGURATION:
  657. fotg210_set_configuration(fotg210);
  658. ret = 1;
  659. break;
  660. default:
  661. ret = 1;
  662. break;
  663. }
  664. } else {
  665. ret = 1;
  666. }
  667. return ret;
  668. }
  669. static void fotg210_ep0out(struct fotg210_udc *fotg210)
  670. {
  671. struct fotg210_ep *ep = fotg210->ep[0];
  672. if (!list_empty(&ep->queue) && !ep->dir_in) {
  673. struct fotg210_request *req;
  674. req = list_first_entry(&ep->queue,
  675. struct fotg210_request, queue);
  676. if (req->req.length)
  677. fotg210_start_dma(ep, req);
  678. if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
  679. fotg210_done(ep, req, 0);
  680. } else {
  681. pr_err("%s : empty queue\n", __func__);
  682. }
  683. }
  684. static void fotg210_ep0in(struct fotg210_udc *fotg210)
  685. {
  686. struct fotg210_ep *ep = fotg210->ep[0];
  687. if ((!list_empty(&ep->queue)) && (ep->dir_in)) {
  688. struct fotg210_request *req;
  689. req = list_entry(ep->queue.next,
  690. struct fotg210_request, queue);
  691. if (req->req.length)
  692. fotg210_start_dma(ep, req);
  693. if (req->req.actual == req->req.length)
  694. fotg210_done(ep, req, 0);
  695. } else {
  696. fotg210_set_cxdone(fotg210);
  697. }
  698. }
  699. static void fotg210_in_fifo_handler(struct fotg210_ep *ep)
  700. {
  701. struct fotg210_request *req = list_entry(ep->queue.next,
  702. struct fotg210_request, queue);
  703. if (req->req.length)
  704. fotg210_start_dma(ep, req);
  705. fotg210_done(ep, req, 0);
  706. }
  707. static void fotg210_out_fifo_handler(struct fotg210_ep *ep)
  708. {
  709. struct fotg210_request *req = list_entry(ep->queue.next,
  710. struct fotg210_request, queue);
  711. int disgr1 = ioread32(ep->fotg210->reg + FOTG210_DISGR1);
  712. fotg210_start_dma(ep, req);
  713. /* Complete the request when it's full or a short packet arrived.
  714. * Like other drivers, short_not_ok isn't handled.
  715. */
  716. if (req->req.length == req->req.actual ||
  717. (disgr1 & DISGR1_SPK_INT(ep->epnum - 1)))
  718. fotg210_done(ep, req, 0);
  719. }
  720. static irqreturn_t fotg210_irq(int irq, void *_fotg210)
  721. {
  722. struct fotg210_udc *fotg210 = _fotg210;
  723. u32 int_grp = ioread32(fotg210->reg + FOTG210_DIGR);
  724. u32 int_msk = ioread32(fotg210->reg + FOTG210_DMIGR);
  725. int_grp &= ~int_msk;
  726. spin_lock(&fotg210->lock);
  727. if (int_grp & DIGR_INT_G2) {
  728. void __iomem *reg = fotg210->reg + FOTG210_DISGR2;
  729. u32 int_grp2 = ioread32(reg);
  730. u32 int_msk2 = ioread32(fotg210->reg + FOTG210_DMISGR2);
  731. int_grp2 &= ~int_msk2;
  732. if (int_grp2 & DISGR2_USBRST_INT) {
  733. usb_gadget_udc_reset(&fotg210->gadget,
  734. fotg210->driver);
  735. fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_USBRST_INT);
  736. pr_info("fotg210 udc reset\n");
  737. }
  738. if (int_grp2 & DISGR2_SUSP_INT) {
  739. fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_SUSP_INT);
  740. pr_info("fotg210 udc suspend\n");
  741. }
  742. if (int_grp2 & DISGR2_RESM_INT) {
  743. fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_RESM_INT);
  744. pr_info("fotg210 udc resume\n");
  745. }
  746. if (int_grp2 & DISGR2_ISO_SEQ_ERR_INT) {
  747. fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_ISO_SEQ_ERR_INT);
  748. pr_info("fotg210 iso sequence error\n");
  749. }
  750. if (int_grp2 & DISGR2_ISO_SEQ_ABORT_INT) {
  751. fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_ISO_SEQ_ABORT_INT);
  752. pr_info("fotg210 iso sequence abort\n");
  753. }
  754. if (int_grp2 & DISGR2_TX0BYTE_INT) {
  755. fotg210_clear_tx0byte(fotg210);
  756. fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_TX0BYTE_INT);
  757. pr_info("fotg210 transferred 0 byte\n");
  758. }
  759. if (int_grp2 & DISGR2_RX0BYTE_INT) {
  760. fotg210_clear_rx0byte(fotg210);
  761. fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_RX0BYTE_INT);
  762. pr_info("fotg210 received 0 byte\n");
  763. }
  764. if (int_grp2 & DISGR2_DMA_ERROR) {
  765. fotg210_ack_int(fotg210, FOTG210_DISGR2, DISGR2_DMA_ERROR);
  766. }
  767. }
  768. if (int_grp & DIGR_INT_G0) {
  769. void __iomem *reg = fotg210->reg + FOTG210_DISGR0;
  770. u32 int_grp0 = ioread32(reg);
  771. u32 int_msk0 = ioread32(fotg210->reg + FOTG210_DMISGR0);
  772. struct usb_ctrlrequest ctrl;
  773. int_grp0 &= ~int_msk0;
  774. /* the highest priority in this source register */
  775. if (int_grp0 & DISGR0_CX_COMABT_INT) {
  776. fotg210_ack_int(fotg210, FOTG210_DISGR0, DISGR0_CX_COMABT_INT);
  777. pr_info("fotg210 CX command abort\n");
  778. }
  779. if (int_grp0 & DISGR0_CX_SETUP_INT) {
  780. if (fotg210_setup_packet(fotg210, &ctrl)) {
  781. spin_unlock(&fotg210->lock);
  782. if (fotg210->driver->setup(&fotg210->gadget,
  783. &ctrl) < 0)
  784. fotg210_set_cxstall(fotg210);
  785. spin_lock(&fotg210->lock);
  786. }
  787. }
  788. if (int_grp0 & DISGR0_CX_COMEND_INT)
  789. pr_info("fotg210 cmd end\n");
  790. if (int_grp0 & DISGR0_CX_IN_INT)
  791. fotg210_ep0in(fotg210);
  792. if (int_grp0 & DISGR0_CX_OUT_INT)
  793. fotg210_ep0out(fotg210);
  794. if (int_grp0 & DISGR0_CX_COMFAIL_INT) {
  795. fotg210_set_cxstall(fotg210);
  796. pr_info("fotg210 ep0 fail\n");
  797. }
  798. }
  799. if (int_grp & DIGR_INT_G1) {
  800. void __iomem *reg = fotg210->reg + FOTG210_DISGR1;
  801. u32 int_grp1 = ioread32(reg);
  802. u32 int_msk1 = ioread32(fotg210->reg + FOTG210_DMISGR1);
  803. int fifo;
  804. int_grp1 &= ~int_msk1;
  805. for (fifo = 0; fifo < FOTG210_MAX_FIFO_NUM; fifo++) {
  806. if (int_grp1 & DISGR1_IN_INT(fifo))
  807. fotg210_in_fifo_handler(fotg210->ep[fifo + 1]);
  808. if ((int_grp1 & DISGR1_OUT_INT(fifo)) ||
  809. (int_grp1 & DISGR1_SPK_INT(fifo)))
  810. fotg210_out_fifo_handler(fotg210->ep[fifo + 1]);
  811. }
  812. }
  813. spin_unlock(&fotg210->lock);
  814. return IRQ_HANDLED;
  815. }
  816. static void fotg210_disable_unplug(struct fotg210_udc *fotg210)
  817. {
  818. u32 reg = ioread32(fotg210->reg + FOTG210_PHYTMSR);
  819. reg &= ~PHYTMSR_UNPLUG;
  820. iowrite32(reg, fotg210->reg + FOTG210_PHYTMSR);
  821. }
  822. static int fotg210_udc_start(struct usb_gadget *g,
  823. struct usb_gadget_driver *driver)
  824. {
  825. struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
  826. u32 value;
  827. int ret;
  828. /* hook up the driver */
  829. fotg210->driver = driver;
  830. fotg210->gadget.dev.of_node = fotg210->dev->of_node;
  831. fotg210->gadget.speed = USB_SPEED_UNKNOWN;
  832. dev_info(fotg210->dev, "bound driver %s\n", driver->driver.name);
  833. if (!IS_ERR_OR_NULL(fotg210->phy)) {
  834. ret = otg_set_peripheral(fotg210->phy->otg,
  835. &fotg210->gadget);
  836. if (ret)
  837. dev_err(fotg210->dev, "can't bind to phy\n");
  838. }
  839. /* chip enable */
  840. value = ioread32(fotg210->reg + FOTG210_DMCR);
  841. value |= DMCR_CHIP_EN;
  842. iowrite32(value, fotg210->reg + FOTG210_DMCR);
  843. /* enable device global interrupt */
  844. value = ioread32(fotg210->reg + FOTG210_DMCR);
  845. value |= DMCR_GLINT_EN;
  846. iowrite32(value, fotg210->reg + FOTG210_DMCR);
  847. return 0;
  848. }
  849. static void fotg210_init(struct fotg210_udc *fotg210)
  850. {
  851. u32 value;
  852. /* disable global interrupt and set int polarity to active high */
  853. iowrite32(GMIR_MHC_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
  854. fotg210->reg + FOTG210_GMIR);
  855. /* mask interrupts for groups other than 0-2 */
  856. iowrite32(~(DMIGR_MINT_G0 | DMIGR_MINT_G1 | DMIGR_MINT_G2),
  857. fotg210->reg + FOTG210_DMIGR);
  858. /* udc software reset */
  859. iowrite32(DMCR_SFRST, fotg210->reg + FOTG210_DMCR);
  860. /* Better wait a bit, but without a datasheet, no idea how long. */
  861. usleep_range(100, 200);
  862. /* disable device global interrupt */
  863. value = ioread32(fotg210->reg + FOTG210_DMCR);
  864. value &= ~DMCR_GLINT_EN;
  865. iowrite32(value, fotg210->reg + FOTG210_DMCR);
  866. /* enable only grp2 irqs we handle */
  867. iowrite32(~(DISGR2_DMA_ERROR | DISGR2_RX0BYTE_INT | DISGR2_TX0BYTE_INT
  868. | DISGR2_ISO_SEQ_ABORT_INT | DISGR2_ISO_SEQ_ERR_INT
  869. | DISGR2_RESM_INT | DISGR2_SUSP_INT | DISGR2_USBRST_INT),
  870. fotg210->reg + FOTG210_DMISGR2);
  871. /* disable all fifo interrupt */
  872. iowrite32(~(u32)0, fotg210->reg + FOTG210_DMISGR1);
  873. /* disable cmd end */
  874. value = ioread32(fotg210->reg + FOTG210_DMISGR0);
  875. value |= DMISGR0_MCX_COMEND;
  876. iowrite32(value, fotg210->reg + FOTG210_DMISGR0);
  877. }
  878. static int fotg210_udc_stop(struct usb_gadget *g)
  879. {
  880. struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
  881. unsigned long flags;
  882. if (!IS_ERR_OR_NULL(fotg210->phy))
  883. return otg_set_peripheral(fotg210->phy->otg, NULL);
  884. spin_lock_irqsave(&fotg210->lock, flags);
  885. fotg210_init(fotg210);
  886. fotg210->driver = NULL;
  887. fotg210->gadget.speed = USB_SPEED_UNKNOWN;
  888. spin_unlock_irqrestore(&fotg210->lock, flags);
  889. return 0;
  890. }
  891. /**
  892. * fotg210_vbus_session - Called by external transceiver to enable/disable udc
  893. * @g: usb gadget
  894. * @is_active: 0 if should disable UDC VBUS, 1 if should enable
  895. *
  896. * Returns: %0
  897. */
  898. static int fotg210_vbus_session(struct usb_gadget *g, int is_active)
  899. {
  900. struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
  901. /* Call down to core integration layer to drive or disable VBUS */
  902. fotg210_vbus(fotg210->fotg, is_active);
  903. return 0;
  904. }
  905. static const struct usb_gadget_ops fotg210_gadget_ops = {
  906. .udc_start = fotg210_udc_start,
  907. .udc_stop = fotg210_udc_stop,
  908. .vbus_session = fotg210_vbus_session,
  909. };
  910. /**
  911. * fotg210_phy_event - Called by phy upon VBus event
  912. * @nb: notifier block
  913. * @action: phy action, is vbus connect or disconnect
  914. * @data: the usb_gadget structure in fotg210
  915. *
  916. * Called by the USB Phy when a cable connect or disconnect is sensed.
  917. *
  918. * Returns: NOTIFY_OK or NOTIFY_DONE
  919. */
  920. static int fotg210_phy_event(struct notifier_block *nb, unsigned long action,
  921. void *data)
  922. {
  923. struct usb_gadget *gadget = data;
  924. if (!gadget)
  925. return NOTIFY_DONE;
  926. switch (action) {
  927. case USB_EVENT_VBUS:
  928. usb_gadget_vbus_connect(gadget);
  929. return NOTIFY_OK;
  930. case USB_EVENT_NONE:
  931. usb_gadget_vbus_disconnect(gadget);
  932. return NOTIFY_OK;
  933. default:
  934. return NOTIFY_DONE;
  935. }
  936. }
  937. static struct notifier_block fotg210_phy_notifier = {
  938. .notifier_call = fotg210_phy_event,
  939. };
  940. int fotg210_udc_remove(struct platform_device *pdev)
  941. {
  942. struct fotg210_udc *fotg210 = platform_get_drvdata(pdev);
  943. int i;
  944. usb_del_gadget_udc(&fotg210->gadget);
  945. if (!IS_ERR_OR_NULL(fotg210->phy)) {
  946. usb_unregister_notifier(fotg210->phy, &fotg210_phy_notifier);
  947. usb_put_phy(fotg210->phy);
  948. }
  949. iounmap(fotg210->reg);
  950. free_irq(platform_get_irq(pdev, 0), fotg210);
  951. fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
  952. for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
  953. kfree(fotg210->ep[i]);
  954. kfree(fotg210);
  955. return 0;
  956. }
  957. int fotg210_udc_probe(struct platform_device *pdev, struct fotg210 *fotg)
  958. {
  959. struct fotg210_udc *fotg210 = NULL;
  960. struct device *dev = &pdev->dev;
  961. int irq;
  962. int ret = 0;
  963. int i;
  964. irq = platform_get_irq(pdev, 0);
  965. if (irq < 0)
  966. return irq;
  967. /* initialize udc */
  968. fotg210 = kzalloc(sizeof(struct fotg210_udc), GFP_KERNEL);
  969. if (fotg210 == NULL)
  970. return -ENOMEM;
  971. fotg210->dev = dev;
  972. fotg210->fotg = fotg;
  973. fotg210->phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
  974. if (IS_ERR(fotg210->phy)) {
  975. ret = PTR_ERR(fotg210->phy);
  976. if (ret == -EPROBE_DEFER)
  977. goto err_free;
  978. dev_info(dev, "no PHY found\n");
  979. fotg210->phy = NULL;
  980. } else {
  981. ret = usb_phy_init(fotg210->phy);
  982. if (ret)
  983. goto err_free;
  984. dev_info(dev, "found and initialized PHY\n");
  985. }
  986. ret = -ENOMEM;
  987. for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
  988. fotg210->ep[i] = kzalloc(sizeof(struct fotg210_ep), GFP_KERNEL);
  989. if (!fotg210->ep[i])
  990. goto err_alloc;
  991. }
  992. fotg210->reg = fotg->base;
  993. spin_lock_init(&fotg210->lock);
  994. platform_set_drvdata(pdev, fotg210);
  995. fotg210->gadget.ops = &fotg210_gadget_ops;
  996. fotg210->gadget.max_speed = USB_SPEED_HIGH;
  997. fotg210->gadget.dev.parent = dev;
  998. fotg210->gadget.dev.dma_mask = dev->dma_mask;
  999. fotg210->gadget.name = udc_name;
  1000. INIT_LIST_HEAD(&fotg210->gadget.ep_list);
  1001. for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
  1002. struct fotg210_ep *ep = fotg210->ep[i];
  1003. if (i) {
  1004. INIT_LIST_HEAD(&fotg210->ep[i]->ep.ep_list);
  1005. list_add_tail(&fotg210->ep[i]->ep.ep_list,
  1006. &fotg210->gadget.ep_list);
  1007. }
  1008. ep->fotg210 = fotg210;
  1009. INIT_LIST_HEAD(&ep->queue);
  1010. ep->ep.name = fotg210_ep_name[i];
  1011. ep->ep.ops = &fotg210_ep_ops;
  1012. usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
  1013. if (i == 0) {
  1014. ep->ep.caps.type_control = true;
  1015. } else {
  1016. ep->ep.caps.type_iso = true;
  1017. ep->ep.caps.type_bulk = true;
  1018. ep->ep.caps.type_int = true;
  1019. }
  1020. ep->ep.caps.dir_in = true;
  1021. ep->ep.caps.dir_out = true;
  1022. }
  1023. usb_ep_set_maxpacket_limit(&fotg210->ep[0]->ep, 0x40);
  1024. fotg210->gadget.ep0 = &fotg210->ep[0]->ep;
  1025. INIT_LIST_HEAD(&fotg210->gadget.ep0->ep_list);
  1026. fotg210->ep0_req = fotg210_ep_alloc_request(&fotg210->ep[0]->ep,
  1027. GFP_KERNEL);
  1028. if (fotg210->ep0_req == NULL)
  1029. goto err_map;
  1030. fotg210->ep0_req->complete = fotg210_ep0_complete;
  1031. fotg210_init(fotg210);
  1032. fotg210_disable_unplug(fotg210);
  1033. ret = request_irq(irq, fotg210_irq, IRQF_SHARED,
  1034. udc_name, fotg210);
  1035. if (ret < 0) {
  1036. dev_err_probe(dev, ret, "request_irq error\n");
  1037. goto err_req;
  1038. }
  1039. if (!IS_ERR_OR_NULL(fotg210->phy))
  1040. usb_register_notifier(fotg210->phy, &fotg210_phy_notifier);
  1041. ret = usb_add_gadget_udc(dev, &fotg210->gadget);
  1042. if (ret)
  1043. goto err_add_udc;
  1044. dev_info(dev, "version %s\n", DRIVER_VERSION);
  1045. return 0;
  1046. err_add_udc:
  1047. if (!IS_ERR_OR_NULL(fotg210->phy))
  1048. usb_unregister_notifier(fotg210->phy, &fotg210_phy_notifier);
  1049. free_irq(irq, fotg210);
  1050. err_req:
  1051. fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
  1052. err_map:
  1053. iounmap(fotg210->reg);
  1054. err_alloc:
  1055. for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
  1056. kfree(fotg210->ep[i]);
  1057. err_free:
  1058. kfree(fotg210);
  1059. return ret;
  1060. }