imx_udc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. /*
  2. * driver/usb/gadget/imx_udc.c
  3. *
  4. * Copyright (C) 2005 Mike Lee(eemike@gmail.com)
  5. * Copyright (C) 2008 Darius Augulis <augulis.darius@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/module.h>
  21. #include <linux/errno.h>
  22. #include <linux/list.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/device.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/clk.h>
  29. #include <linux/delay.h>
  30. #include <linux/usb/ch9.h>
  31. #include <linux/usb/gadget.h>
  32. #include <mach/usb.h>
  33. #include <mach/hardware.h>
  34. #include "imx_udc.h"
  35. static const char driver_name[] = "imx_udc";
  36. static const char ep0name[] = "ep0";
  37. void ep0_chg_stat(const char *label, struct imx_udc_struct *imx_usb,
  38. enum ep0_state stat);
  39. /*******************************************************************************
  40. * IMX UDC hardware related functions
  41. *******************************************************************************
  42. */
  43. void imx_udc_enable(struct imx_udc_struct *imx_usb)
  44. {
  45. int temp = __raw_readl(imx_usb->base + USB_CTRL);
  46. __raw_writel(temp | CTRL_FE_ENA | CTRL_AFE_ENA, imx_usb->base + USB_CTRL);
  47. imx_usb->gadget.speed = USB_SPEED_FULL;
  48. }
  49. void imx_udc_disable(struct imx_udc_struct *imx_usb)
  50. {
  51. int temp = __raw_readl(imx_usb->base + USB_CTRL);
  52. __raw_writel(temp & ~(CTRL_FE_ENA | CTRL_AFE_ENA),
  53. imx_usb->base + USB_CTRL);
  54. ep0_chg_stat(__func__, imx_usb, EP0_IDLE);
  55. imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
  56. }
  57. void imx_udc_reset(struct imx_udc_struct *imx_usb)
  58. {
  59. int temp = __raw_readl(imx_usb->base + USB_ENAB);
  60. /* set RST bit */
  61. __raw_writel(temp | ENAB_RST, imx_usb->base + USB_ENAB);
  62. /* wait RST bit to clear */
  63. do {} while (__raw_readl(imx_usb->base + USB_ENAB) & ENAB_RST);
  64. /* wait CFG bit to assert */
  65. do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG));
  66. /* udc module is now ready */
  67. }
  68. void imx_udc_config(struct imx_udc_struct *imx_usb)
  69. {
  70. u8 ep_conf[5];
  71. u8 i, j, cfg;
  72. struct imx_ep_struct *imx_ep;
  73. /* wait CFG bit to assert */
  74. do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG));
  75. /* Download the endpoint buffer for endpoint 0. */
  76. for (j = 0; j < 5; j++) {
  77. i = (j == 2 ? imx_usb->imx_ep[0].fifosize : 0x00);
  78. __raw_writeb(i, imx_usb->base + USB_DDAT);
  79. do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_BSY);
  80. }
  81. /* Download the endpoint buffers for endpoints 1-5.
  82. * We specify two configurations, one interface
  83. */
  84. for (cfg = 1; cfg < 3; cfg++) {
  85. for (i = 1; i < IMX_USB_NB_EP; i++) {
  86. imx_ep = &imx_usb->imx_ep[i];
  87. /* EP no | Config no */
  88. ep_conf[0] = (i << 4) | (cfg << 2);
  89. /* Type | Direction */
  90. ep_conf[1] = (imx_ep->bmAttributes << 3) |
  91. (EP_DIR(imx_ep) << 2);
  92. /* Max packet size */
  93. ep_conf[2] = imx_ep->fifosize;
  94. /* TRXTYP */
  95. ep_conf[3] = 0xC0;
  96. /* FIFO no */
  97. ep_conf[4] = i;
  98. D_INI(imx_usb->dev,
  99. "<%s> ep%d_conf[%d]:"
  100. "[%02x-%02x-%02x-%02x-%02x]\n",
  101. __func__, i, cfg,
  102. ep_conf[0], ep_conf[1], ep_conf[2],
  103. ep_conf[3], ep_conf[4]);
  104. for (j = 0; j < 5; j++) {
  105. __raw_writeb(ep_conf[j],
  106. imx_usb->base + USB_DDAT);
  107. do {} while (__raw_readl(imx_usb->base + USB_DADR)
  108. & DADR_BSY);
  109. }
  110. }
  111. }
  112. /* wait CFG bit to clear */
  113. do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG);
  114. }
  115. void imx_udc_init_irq(struct imx_udc_struct *imx_usb)
  116. {
  117. int i;
  118. /* Mask and clear all irqs */
  119. __raw_writel(0xFFFFFFFF, imx_usb->base + USB_MASK);
  120. __raw_writel(0xFFFFFFFF, imx_usb->base + USB_INTR);
  121. for (i = 0; i < IMX_USB_NB_EP; i++) {
  122. __raw_writel(0x1FF, imx_usb->base + USB_EP_MASK(i));
  123. __raw_writel(0x1FF, imx_usb->base + USB_EP_INTR(i));
  124. }
  125. /* Enable USB irqs */
  126. __raw_writel(INTR_MSOF | INTR_FRAME_MATCH, imx_usb->base + USB_MASK);
  127. /* Enable EP0 irqs */
  128. __raw_writel(0x1FF & ~(EPINTR_DEVREQ | EPINTR_MDEVREQ | EPINTR_EOT
  129. | EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL),
  130. imx_usb->base + USB_EP_MASK(0));
  131. }
  132. void imx_udc_init_ep(struct imx_udc_struct *imx_usb)
  133. {
  134. int i, max, temp;
  135. struct imx_ep_struct *imx_ep;
  136. for (i = 0; i < IMX_USB_NB_EP; i++) {
  137. imx_ep = &imx_usb->imx_ep[i];
  138. switch (imx_ep->fifosize) {
  139. case 8:
  140. max = 0;
  141. break;
  142. case 16:
  143. max = 1;
  144. break;
  145. case 32:
  146. max = 2;
  147. break;
  148. case 64:
  149. max = 3;
  150. break;
  151. default:
  152. max = 1;
  153. break;
  154. }
  155. temp = (EP_DIR(imx_ep) << 7) | (max << 5)
  156. | (imx_ep->bmAttributes << 3);
  157. __raw_writel(temp, imx_usb->base + USB_EP_STAT(i));
  158. __raw_writel(temp | EPSTAT_FLUSH, imx_usb->base + USB_EP_STAT(i));
  159. D_INI(imx_usb->dev, "<%s> ep%d_stat %08x\n", __func__, i,
  160. __raw_readl(imx_usb->base + USB_EP_STAT(i)));
  161. }
  162. }
  163. void imx_udc_init_fifo(struct imx_udc_struct *imx_usb)
  164. {
  165. int i, temp;
  166. struct imx_ep_struct *imx_ep;
  167. for (i = 0; i < IMX_USB_NB_EP; i++) {
  168. imx_ep = &imx_usb->imx_ep[i];
  169. /* Fifo control */
  170. temp = EP_DIR(imx_ep) ? 0x0B000000 : 0x0F000000;
  171. __raw_writel(temp, imx_usb->base + USB_EP_FCTRL(i));
  172. D_INI(imx_usb->dev, "<%s> ep%d_fctrl %08x\n", __func__, i,
  173. __raw_readl(imx_usb->base + USB_EP_FCTRL(i)));
  174. /* Fifo alarm */
  175. temp = (i ? imx_ep->fifosize / 2 : 0);
  176. __raw_writel(temp, imx_usb->base + USB_EP_FALRM(i));
  177. D_INI(imx_usb->dev, "<%s> ep%d_falrm %08x\n", __func__, i,
  178. __raw_readl(imx_usb->base + USB_EP_FALRM(i)));
  179. }
  180. }
  181. static void imx_udc_init(struct imx_udc_struct *imx_usb)
  182. {
  183. /* Reset UDC */
  184. imx_udc_reset(imx_usb);
  185. /* Download config to enpoint buffer */
  186. imx_udc_config(imx_usb);
  187. /* Setup interrups */
  188. imx_udc_init_irq(imx_usb);
  189. /* Setup endpoints */
  190. imx_udc_init_ep(imx_usb);
  191. /* Setup fifos */
  192. imx_udc_init_fifo(imx_usb);
  193. }
  194. void imx_ep_irq_enable(struct imx_ep_struct *imx_ep)
  195. {
  196. int i = EP_NO(imx_ep);
  197. __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i));
  198. __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i));
  199. __raw_writel(0x1FF & ~(EPINTR_EOT | EPINTR_EOF),
  200. imx_ep->imx_usb->base + USB_EP_MASK(i));
  201. }
  202. void imx_ep_irq_disable(struct imx_ep_struct *imx_ep)
  203. {
  204. int i = EP_NO(imx_ep);
  205. __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i));
  206. __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i));
  207. }
  208. int imx_ep_empty(struct imx_ep_struct *imx_ep)
  209. {
  210. struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
  211. return __raw_readl(imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep)))
  212. & FSTAT_EMPTY;
  213. }
  214. unsigned imx_fifo_bcount(struct imx_ep_struct *imx_ep)
  215. {
  216. struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
  217. return (__raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)))
  218. & EPSTAT_BCOUNT) >> 16;
  219. }
  220. void imx_flush(struct imx_ep_struct *imx_ep)
  221. {
  222. struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
  223. int temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
  224. __raw_writel(temp | EPSTAT_FLUSH,
  225. imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
  226. }
  227. void imx_ep_stall(struct imx_ep_struct *imx_ep)
  228. {
  229. struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
  230. int temp, i;
  231. D_ERR(imx_usb->dev, "<%s> Forced stall on %s\n", __func__, imx_ep->ep.name);
  232. imx_flush(imx_ep);
  233. /* Special care for ep0 */
  234. if (EP_NO(imx_ep)) {
  235. temp = __raw_readl(imx_usb->base + USB_CTRL);
  236. __raw_writel(temp | CTRL_CMDOVER | CTRL_CMDERROR, imx_usb->base + USB_CTRL);
  237. do { } while (__raw_readl(imx_usb->base + USB_CTRL) & CTRL_CMDOVER);
  238. temp = __raw_readl(imx_usb->base + USB_CTRL);
  239. __raw_writel(temp & ~CTRL_CMDERROR, imx_usb->base + USB_CTRL);
  240. }
  241. else {
  242. temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
  243. __raw_writel(temp | EPSTAT_STALL,
  244. imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
  245. for (i = 0; i < 100; i ++) {
  246. temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
  247. if (!(temp & EPSTAT_STALL))
  248. break;
  249. udelay(20);
  250. }
  251. if (i == 50)
  252. D_ERR(imx_usb->dev, "<%s> Non finished stall on %s\n",
  253. __func__, imx_ep->ep.name);
  254. }
  255. }
  256. static int imx_udc_get_frame(struct usb_gadget *_gadget)
  257. {
  258. struct imx_udc_struct *imx_usb = container_of(_gadget,
  259. struct imx_udc_struct, gadget);
  260. return __raw_readl(imx_usb->base + USB_FRAME) & 0x7FF;
  261. }
  262. static int imx_udc_wakeup(struct usb_gadget *_gadget)
  263. {
  264. return 0;
  265. }
  266. /*******************************************************************************
  267. * USB request control functions
  268. *******************************************************************************
  269. */
  270. static void ep_add_request(struct imx_ep_struct *imx_ep, struct imx_request *req)
  271. {
  272. if (unlikely(!req))
  273. return;
  274. req->in_use = 1;
  275. list_add_tail(&req->queue, &imx_ep->queue);
  276. }
  277. static void ep_del_request(struct imx_ep_struct *imx_ep, struct imx_request *req)
  278. {
  279. if (unlikely(!req))
  280. return;
  281. list_del_init(&req->queue);
  282. req->in_use = 0;
  283. }
  284. static void done(struct imx_ep_struct *imx_ep, struct imx_request *req, int status)
  285. {
  286. ep_del_request(imx_ep, req);
  287. if (likely(req->req.status == -EINPROGRESS))
  288. req->req.status = status;
  289. else
  290. status = req->req.status;
  291. if (status && status != -ESHUTDOWN)
  292. D_ERR(imx_ep->imx_usb->dev,
  293. "<%s> complete %s req %p stat %d len %u/%u\n", __func__,
  294. imx_ep->ep.name, &req->req, status,
  295. req->req.actual, req->req.length);
  296. req->req.complete(&imx_ep->ep, &req->req);
  297. }
  298. static void nuke(struct imx_ep_struct *imx_ep, int status)
  299. {
  300. struct imx_request *req;
  301. while (!list_empty(&imx_ep->queue)) {
  302. req = list_entry(imx_ep->queue.next, struct imx_request, queue);
  303. done(imx_ep, req, status);
  304. }
  305. }
  306. /*******************************************************************************
  307. * Data tansfer over USB functions
  308. *******************************************************************************
  309. */
  310. static int read_packet(struct imx_ep_struct *imx_ep, struct imx_request *req)
  311. {
  312. u8 *buf;
  313. int bytes_ep, bufferspace, count, i;
  314. bytes_ep = imx_fifo_bcount(imx_ep);
  315. bufferspace = req->req.length - req->req.actual;
  316. buf = req->req.buf + req->req.actual;
  317. prefetchw(buf);
  318. if (unlikely(imx_ep_empty(imx_ep)))
  319. count = 0; /* zlp */
  320. else
  321. count = min(bytes_ep, bufferspace);
  322. for (i = count; i > 0; i--)
  323. *buf++ = __raw_readb(imx_ep->imx_usb->base
  324. + USB_EP_FDAT0(EP_NO(imx_ep)));
  325. req->req.actual += count;
  326. return count;
  327. }
  328. static int write_packet(struct imx_ep_struct *imx_ep, struct imx_request *req)
  329. {
  330. u8 *buf;
  331. int length, count, temp;
  332. buf = req->req.buf + req->req.actual;
  333. prefetch(buf);
  334. length = min(req->req.length - req->req.actual, (u32)imx_ep->fifosize);
  335. if (imx_fifo_bcount(imx_ep) + length > imx_ep->fifosize) {
  336. D_TRX(imx_ep->imx_usb->dev, "<%s> packet overfill %s fifo\n",
  337. __func__, imx_ep->ep.name);
  338. return -1;
  339. }
  340. req->req.actual += length;
  341. count = length;
  342. if (!count && req->req.zero) { /* zlp */
  343. temp = __raw_readl(imx_ep->imx_usb->base
  344. + USB_EP_STAT(EP_NO(imx_ep)));
  345. __raw_writel(temp | EPSTAT_ZLPS, imx_ep->imx_usb->base
  346. + USB_EP_STAT(EP_NO(imx_ep)));
  347. D_TRX(imx_ep->imx_usb->dev, "<%s> zero packet\n", __func__);
  348. return 0;
  349. }
  350. while (count--) {
  351. if (count == 0) { /* last byte */
  352. temp = __raw_readl(imx_ep->imx_usb->base
  353. + USB_EP_FCTRL(EP_NO(imx_ep)));
  354. __raw_writel(temp | FCTRL_WFR, imx_ep->imx_usb->base
  355. + USB_EP_FCTRL(EP_NO(imx_ep)));
  356. }
  357. __raw_writeb(*buf++,
  358. imx_ep->imx_usb->base + USB_EP_FDAT0(EP_NO(imx_ep)));
  359. }
  360. return length;
  361. }
  362. static int read_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req)
  363. {
  364. int bytes = 0,
  365. count,
  366. completed = 0;
  367. while (__raw_readl(imx_ep->imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep)))
  368. & FSTAT_FR) {
  369. count = read_packet(imx_ep, req);
  370. bytes += count;
  371. completed = (count != imx_ep->fifosize);
  372. if (completed || req->req.actual == req->req.length) {
  373. completed = 1;
  374. break;
  375. }
  376. }
  377. if (completed || !req->req.length) {
  378. done(imx_ep, req, 0);
  379. D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n",
  380. __func__, imx_ep->ep.name, req,
  381. completed ? "completed" : "not completed");
  382. if (!EP_NO(imx_ep))
  383. ep0_chg_stat(__func__, imx_ep->imx_usb, EP0_IDLE);
  384. }
  385. D_TRX(imx_ep->imx_usb->dev, "<%s> bytes read: %d\n", __func__, bytes);
  386. return completed;
  387. }
  388. static int write_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req)
  389. {
  390. int bytes = 0,
  391. count,
  392. completed = 0;
  393. while (!completed) {
  394. count = write_packet(imx_ep, req);
  395. if (count < 0)
  396. break; /* busy */
  397. bytes += count;
  398. /* last packet "must be" short (or a zlp) */
  399. completed = (count != imx_ep->fifosize);
  400. if (unlikely(completed)) {
  401. done(imx_ep, req, 0);
  402. D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n",
  403. __func__, imx_ep->ep.name, req,
  404. completed ? "completed" : "not completed");
  405. if (!EP_NO(imx_ep))
  406. ep0_chg_stat(__func__, imx_ep->imx_usb, EP0_IDLE);
  407. }
  408. }
  409. D_TRX(imx_ep->imx_usb->dev, "<%s> bytes sent: %d\n", __func__, bytes);
  410. return completed;
  411. }
  412. /*******************************************************************************
  413. * Endpoint handlers
  414. *******************************************************************************
  415. */
  416. static int handle_ep(struct imx_ep_struct *imx_ep)
  417. {
  418. struct imx_request *req;
  419. int completed = 0;
  420. do {
  421. if (!list_empty(&imx_ep->queue))
  422. req = list_entry(imx_ep->queue.next,
  423. struct imx_request, queue);
  424. else {
  425. D_REQ(imx_ep->imx_usb->dev, "<%s> no request on %s\n",
  426. __func__, imx_ep->ep.name);
  427. return 0;
  428. }
  429. if (EP_DIR(imx_ep)) /* to host */
  430. completed = write_fifo(imx_ep, req);
  431. else /* to device */
  432. completed = read_fifo(imx_ep, req);
  433. dump_ep_stat(__func__, imx_ep);
  434. } while (completed);
  435. return 0;
  436. }
  437. static int handle_ep0(struct imx_ep_struct *imx_ep)
  438. {
  439. struct imx_request *req = NULL;
  440. int ret = 0;
  441. if (!list_empty(&imx_ep->queue))
  442. req = list_entry(imx_ep->queue.next, struct imx_request, queue);
  443. if (req) {
  444. switch (imx_ep->imx_usb->ep0state) {
  445. case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR */
  446. write_fifo(imx_ep, req);
  447. break;
  448. case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR */
  449. read_fifo(imx_ep, req);
  450. break;
  451. default:
  452. D_EP0(imx_ep->imx_usb->dev,
  453. "<%s> ep0 i/o, odd state %d\n",
  454. __func__, imx_ep->imx_usb->ep0state);
  455. ep_del_request(imx_ep, req);
  456. ret = -EL2HLT;
  457. break;
  458. }
  459. }
  460. return ret;
  461. }
  462. static void handle_ep0_devreq(struct imx_udc_struct *imx_usb)
  463. {
  464. struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[0];
  465. union {
  466. struct usb_ctrlrequest r;
  467. u8 raw[8];
  468. u32 word[2];
  469. } u;
  470. int temp, i;
  471. nuke(imx_ep, -EPROTO);
  472. /* read SETUP packet */
  473. for (i = 0; i < 2; i++) {
  474. if (imx_ep_empty(imx_ep)) {
  475. D_ERR(imx_usb->dev,
  476. "<%s> no setup packet received\n", __func__);
  477. goto stall;
  478. }
  479. u.word[i] = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep)));
  480. }
  481. temp = imx_ep_empty(imx_ep);
  482. while (!imx_ep_empty(imx_ep)) {
  483. i = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep)));
  484. D_ERR(imx_usb->dev,
  485. "<%s> wrong to have extra bytes for setup : 0x%08x\n",
  486. __func__, i);
  487. }
  488. if (!temp)
  489. goto stall;
  490. le16_to_cpus(&u.r.wValue);
  491. le16_to_cpus(&u.r.wIndex);
  492. le16_to_cpus(&u.r.wLength);
  493. D_REQ(imx_usb->dev, "<%s> SETUP %02x.%02x v%04x i%04x l%04x\n",
  494. __func__, u.r.bRequestType, u.r.bRequest,
  495. u.r.wValue, u.r.wIndex, u.r.wLength);
  496. if (imx_usb->set_config) {
  497. /* NACK the host by using CMDOVER */
  498. temp = __raw_readl(imx_usb->base + USB_CTRL);
  499. __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);
  500. D_ERR(imx_usb->dev,
  501. "<%s> set config req is pending, NACK the host\n",
  502. __func__);
  503. return;
  504. }
  505. if (u.r.bRequestType & USB_DIR_IN)
  506. ep0_chg_stat(__func__, imx_usb, EP0_IN_DATA_PHASE);
  507. else
  508. ep0_chg_stat(__func__, imx_usb, EP0_OUT_DATA_PHASE);
  509. i = imx_usb->driver->setup(&imx_usb->gadget, &u.r);
  510. if (i < 0) {
  511. D_ERR(imx_usb->dev, "<%s> device setup error %d\n",
  512. __func__, i);
  513. goto stall;
  514. }
  515. return;
  516. stall:
  517. D_ERR(imx_usb->dev, "<%s> protocol STALL\n", __func__);
  518. imx_ep_stall(imx_ep);
  519. ep0_chg_stat(__func__, imx_usb, EP0_STALL);
  520. return;
  521. }
  522. /*******************************************************************************
  523. * USB gadget callback functions
  524. *******************************************************************************
  525. */
  526. static int imx_ep_enable(struct usb_ep *usb_ep,
  527. const struct usb_endpoint_descriptor *desc)
  528. {
  529. struct imx_ep_struct *imx_ep = container_of(usb_ep,
  530. struct imx_ep_struct, ep);
  531. struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
  532. unsigned long flags;
  533. if (!usb_ep
  534. || !desc
  535. || !EP_NO(imx_ep)
  536. || desc->bDescriptorType != USB_DT_ENDPOINT
  537. || imx_ep->bEndpointAddress != desc->bEndpointAddress) {
  538. D_ERR(imx_usb->dev,
  539. "<%s> bad ep or descriptor\n", __func__);
  540. return -EINVAL;
  541. }
  542. if (imx_ep->bmAttributes != desc->bmAttributes) {
  543. D_ERR(imx_usb->dev,
  544. "<%s> %s type mismatch\n", __func__, usb_ep->name);
  545. return -EINVAL;
  546. }
  547. if (imx_ep->fifosize < le16_to_cpu(desc->wMaxPacketSize)) {
  548. D_ERR(imx_usb->dev,
  549. "<%s> bad %s maxpacket\n", __func__, usb_ep->name);
  550. return -ERANGE;
  551. }
  552. if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
  553. D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
  554. return -ESHUTDOWN;
  555. }
  556. local_irq_save(flags);
  557. imx_ep->stopped = 0;
  558. imx_flush(imx_ep);
  559. imx_ep_irq_enable(imx_ep);
  560. local_irq_restore(flags);
  561. D_EPX(imx_usb->dev, "<%s> ENABLED %s\n", __func__, usb_ep->name);
  562. return 0;
  563. }
  564. static int imx_ep_disable(struct usb_ep *usb_ep)
  565. {
  566. struct imx_ep_struct *imx_ep = container_of(usb_ep,
  567. struct imx_ep_struct, ep);
  568. unsigned long flags;
  569. if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
  570. D_ERR(imx_ep->imx_usb->dev, "<%s> %s can not be disabled\n",
  571. __func__, usb_ep ? imx_ep->ep.name : NULL);
  572. return -EINVAL;
  573. }
  574. local_irq_save(flags);
  575. imx_ep->stopped = 1;
  576. nuke(imx_ep, -ESHUTDOWN);
  577. imx_flush(imx_ep);
  578. imx_ep_irq_disable(imx_ep);
  579. local_irq_restore(flags);
  580. D_EPX(imx_ep->imx_usb->dev,
  581. "<%s> DISABLED %s\n", __func__, usb_ep->name);
  582. return 0;
  583. }
  584. static struct usb_request *imx_ep_alloc_request
  585. (struct usb_ep *usb_ep, gfp_t gfp_flags)
  586. {
  587. struct imx_request *req;
  588. req = kzalloc(sizeof *req, gfp_flags);
  589. if (!req || !usb_ep)
  590. return 0;
  591. INIT_LIST_HEAD(&req->queue);
  592. req->in_use = 0;
  593. return &req->req;
  594. }
  595. static void imx_ep_free_request
  596. (struct usb_ep *usb_ep, struct usb_request *usb_req)
  597. {
  598. struct imx_request *req;
  599. req = container_of(usb_req, struct imx_request, req);
  600. WARN_ON(!list_empty(&req->queue));
  601. kfree(req);
  602. }
  603. static int imx_ep_queue
  604. (struct usb_ep *usb_ep, struct usb_request *usb_req, gfp_t gfp_flags)
  605. {
  606. struct imx_ep_struct *imx_ep;
  607. struct imx_udc_struct *imx_usb;
  608. struct imx_request *req;
  609. unsigned long flags;
  610. int ret = 0;
  611. imx_ep = container_of(usb_ep, struct imx_ep_struct, ep);
  612. imx_usb = imx_ep->imx_usb;
  613. req = container_of(usb_req, struct imx_request, req);
  614. /*
  615. Special care on IMX udc.
  616. Ignore enqueue when after set configuration from the
  617. host. This assume all gadget drivers reply set
  618. configuration with the next ep0 req enqueue.
  619. */
  620. if (imx_usb->set_config && !EP_NO(imx_ep)) {
  621. imx_usb->set_config = 0;
  622. D_EPX(imx_usb->dev,
  623. "<%s> gadget reply set config\n", __func__);
  624. return 0;
  625. }
  626. if (unlikely(!usb_req || !req || !usb_req->complete || !usb_req->buf)) {
  627. D_ERR(imx_usb->dev, "<%s> bad params\n", __func__);
  628. return -EINVAL;
  629. }
  630. if (unlikely(!usb_ep || !imx_ep)) {
  631. D_ERR(imx_usb->dev, "<%s> bad ep\n", __func__);
  632. return -EINVAL;
  633. }
  634. if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
  635. D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
  636. return -ESHUTDOWN;
  637. }
  638. local_irq_save(flags);
  639. /* Debug */
  640. D_REQ(imx_usb->dev, "<%s> ep%d %s request for [%d] bytes\n",
  641. __func__, EP_NO(imx_ep),
  642. ((!EP_NO(imx_ep) && imx_ep->imx_usb->ep0state == EP0_IN_DATA_PHASE)
  643. || (EP_NO(imx_ep) && EP_DIR(imx_ep))) ? "IN" : "OUT", usb_req->length);
  644. dump_req(__func__, imx_ep, usb_req);
  645. if (imx_ep->stopped) {
  646. usb_req->status = -ESHUTDOWN;
  647. ret = -ESHUTDOWN;
  648. goto out;
  649. }
  650. if (req->in_use) {
  651. D_ERR(imx_usb->dev,
  652. "<%s> refusing to queue req %p (already queued)\n",
  653. __func__, req);
  654. goto out;
  655. }
  656. usb_req->status = -EINPROGRESS;
  657. usb_req->actual = 0;
  658. ep_add_request(imx_ep, req);
  659. if (!EP_NO(imx_ep))
  660. ret = handle_ep0(imx_ep);
  661. else
  662. ret = handle_ep(imx_ep);
  663. out:
  664. local_irq_restore(flags);
  665. return ret;
  666. }
  667. static int imx_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  668. {
  669. struct imx_ep_struct *imx_ep = container_of
  670. (usb_ep, struct imx_ep_struct, ep);
  671. struct imx_request *req;
  672. unsigned long flags;
  673. if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
  674. D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
  675. return -EINVAL;
  676. }
  677. local_irq_save(flags);
  678. /* make sure it's actually queued on this endpoint */
  679. list_for_each_entry(req, &imx_ep->queue, queue) {
  680. if (&req->req == usb_req)
  681. break;
  682. }
  683. if (&req->req != usb_req) {
  684. local_irq_restore(flags);
  685. return -EINVAL;
  686. }
  687. done(imx_ep, req, -ECONNRESET);
  688. local_irq_restore(flags);
  689. return 0;
  690. }
  691. static int imx_ep_set_halt(struct usb_ep *usb_ep, int value)
  692. {
  693. struct imx_ep_struct *imx_ep = container_of
  694. (usb_ep, struct imx_ep_struct, ep);
  695. unsigned long flags;
  696. if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
  697. D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
  698. return -EINVAL;
  699. }
  700. local_irq_save(flags);
  701. if ((imx_ep->bEndpointAddress & USB_DIR_IN)
  702. && !list_empty(&imx_ep->queue)) {
  703. local_irq_restore(flags);
  704. return -EAGAIN;
  705. }
  706. imx_ep_stall(imx_ep);
  707. local_irq_restore(flags);
  708. D_EPX(imx_ep->imx_usb->dev, "<%s> %s halt\n", __func__, usb_ep->name);
  709. return 0;
  710. }
  711. static int imx_ep_fifo_status(struct usb_ep *usb_ep)
  712. {
  713. struct imx_ep_struct *imx_ep = container_of
  714. (usb_ep, struct imx_ep_struct, ep);
  715. if (!usb_ep) {
  716. D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
  717. return -ENODEV;
  718. }
  719. if (imx_ep->imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
  720. return 0;
  721. else
  722. return imx_fifo_bcount(imx_ep);
  723. }
  724. static void imx_ep_fifo_flush(struct usb_ep *usb_ep)
  725. {
  726. struct imx_ep_struct *imx_ep = container_of
  727. (usb_ep, struct imx_ep_struct, ep);
  728. unsigned long flags;
  729. local_irq_save(flags);
  730. if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
  731. D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
  732. local_irq_restore(flags);
  733. return;
  734. }
  735. /* toggle and halt bits stay unchanged */
  736. imx_flush(imx_ep);
  737. local_irq_restore(flags);
  738. }
  739. static struct usb_ep_ops imx_ep_ops = {
  740. .enable = imx_ep_enable,
  741. .disable = imx_ep_disable,
  742. .alloc_request = imx_ep_alloc_request,
  743. .free_request = imx_ep_free_request,
  744. .queue = imx_ep_queue,
  745. .dequeue = imx_ep_dequeue,
  746. .set_halt = imx_ep_set_halt,
  747. .fifo_status = imx_ep_fifo_status,
  748. .fifo_flush = imx_ep_fifo_flush,
  749. };
  750. /*******************************************************************************
  751. * USB endpoint control functions
  752. *******************************************************************************
  753. */
  754. void ep0_chg_stat(const char *label,
  755. struct imx_udc_struct *imx_usb, enum ep0_state stat)
  756. {
  757. D_EP0(imx_usb->dev, "<%s> from %15s to %15s\n",
  758. label, state_name[imx_usb->ep0state], state_name[stat]);
  759. if (imx_usb->ep0state == stat)
  760. return;
  761. imx_usb->ep0state = stat;
  762. }
  763. static void usb_init_data(struct imx_udc_struct *imx_usb)
  764. {
  765. struct imx_ep_struct *imx_ep;
  766. u8 i;
  767. /* device/ep0 records init */
  768. INIT_LIST_HEAD(&imx_usb->gadget.ep_list);
  769. INIT_LIST_HEAD(&imx_usb->gadget.ep0->ep_list);
  770. ep0_chg_stat(__func__, imx_usb, EP0_IDLE);
  771. /* basic endpoint records init */
  772. for (i = 0; i < IMX_USB_NB_EP; i++) {
  773. imx_ep = &imx_usb->imx_ep[i];
  774. if (i) {
  775. list_add_tail(&imx_ep->ep.ep_list,
  776. &imx_usb->gadget.ep_list);
  777. imx_ep->stopped = 1;
  778. } else
  779. imx_ep->stopped = 0;
  780. INIT_LIST_HEAD(&imx_ep->queue);
  781. }
  782. }
  783. static void udc_stop_activity(struct imx_udc_struct *imx_usb,
  784. struct usb_gadget_driver *driver)
  785. {
  786. struct imx_ep_struct *imx_ep;
  787. int i;
  788. if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
  789. driver = NULL;
  790. /* prevent new request submissions, kill any outstanding requests */
  791. for (i = 1; i < IMX_USB_NB_EP; i++) {
  792. imx_ep = &imx_usb->imx_ep[i];
  793. imx_flush(imx_ep);
  794. imx_ep->stopped = 1;
  795. imx_ep_irq_disable(imx_ep);
  796. nuke(imx_ep, -ESHUTDOWN);
  797. }
  798. imx_usb->cfg = 0;
  799. imx_usb->intf = 0;
  800. imx_usb->alt = 0;
  801. if (driver)
  802. driver->disconnect(&imx_usb->gadget);
  803. }
  804. /*******************************************************************************
  805. * Interrupt handlers
  806. *******************************************************************************
  807. */
  808. static irqreturn_t imx_udc_irq(int irq, void *dev)
  809. {
  810. struct imx_udc_struct *imx_usb = dev;
  811. struct usb_ctrlrequest u;
  812. int temp, cfg, intf, alt;
  813. int intr = __raw_readl(imx_usb->base + USB_INTR);
  814. if (intr & (INTR_WAKEUP | INTR_SUSPEND | INTR_RESUME | INTR_RESET_START
  815. | INTR_RESET_STOP | INTR_CFG_CHG)) {
  816. dump_intr(__func__, intr, imx_usb->dev);
  817. dump_usb_stat(__func__, imx_usb);
  818. }
  819. if (!imx_usb->driver) {
  820. /*imx_udc_disable(imx_usb);*/
  821. goto end_irq;
  822. }
  823. if (intr & INTR_WAKEUP) {
  824. if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN
  825. && imx_usb->driver && imx_usb->driver->resume)
  826. imx_usb->driver->resume(&imx_usb->gadget);
  827. imx_usb->set_config = 0;
  828. imx_usb->gadget.speed = USB_SPEED_FULL;
  829. }
  830. if (intr & INTR_SUSPEND) {
  831. if (imx_usb->gadget.speed != USB_SPEED_UNKNOWN
  832. && imx_usb->driver && imx_usb->driver->suspend)
  833. imx_usb->driver->suspend(&imx_usb->gadget);
  834. imx_usb->set_config = 0;
  835. imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
  836. }
  837. if (intr & INTR_RESET_START) {
  838. __raw_writel(intr, imx_usb->base + USB_INTR);
  839. udc_stop_activity(imx_usb, imx_usb->driver);
  840. imx_usb->set_config = 0;
  841. imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
  842. }
  843. if (intr & INTR_RESET_STOP)
  844. imx_usb->gadget.speed = USB_SPEED_FULL;
  845. if (intr & INTR_CFG_CHG) {
  846. __raw_writel(INTR_CFG_CHG, imx_usb->base + USB_INTR);
  847. temp = __raw_readl(imx_usb->base + USB_STAT);
  848. cfg = (temp & STAT_CFG) >> 5;
  849. intf = (temp & STAT_INTF) >> 3;
  850. alt = temp & STAT_ALTSET;
  851. D_REQ(imx_usb->dev,
  852. "<%s> orig config C=%d, I=%d, A=%d / "
  853. "req config C=%d, I=%d, A=%d\n",
  854. __func__, imx_usb->cfg, imx_usb->intf, imx_usb->alt,
  855. cfg, intf, alt);
  856. if (cfg != 1 && cfg != 2)
  857. goto end_irq;
  858. imx_usb->set_config = 0;
  859. /* Config setup */
  860. if (imx_usb->cfg != cfg) {
  861. D_REQ(imx_usb->dev, "<%s> Change config start\n",__func__);
  862. u.bRequest = USB_REQ_SET_CONFIGURATION;
  863. u.bRequestType = USB_DIR_OUT |
  864. USB_TYPE_STANDARD |
  865. USB_RECIP_DEVICE;
  866. u.wValue = cfg;
  867. u.wIndex = 0;
  868. u.wLength = 0;
  869. imx_usb->cfg = cfg;
  870. imx_usb->set_config = 1;
  871. imx_usb->driver->setup(&imx_usb->gadget, &u);
  872. imx_usb->set_config = 0;
  873. D_REQ(imx_usb->dev, "<%s> Change config done\n",__func__);
  874. }
  875. if (imx_usb->intf != intf || imx_usb->alt != alt) {
  876. D_REQ(imx_usb->dev, "<%s> Change interface start\n",__func__);
  877. u.bRequest = USB_REQ_SET_INTERFACE;
  878. u.bRequestType = USB_DIR_OUT |
  879. USB_TYPE_STANDARD |
  880. USB_RECIP_INTERFACE;
  881. u.wValue = alt;
  882. u.wIndex = intf;
  883. u.wLength = 0;
  884. imx_usb->intf = intf;
  885. imx_usb->alt = alt;
  886. imx_usb->set_config = 1;
  887. imx_usb->driver->setup(&imx_usb->gadget, &u);
  888. imx_usb->set_config = 0;
  889. D_REQ(imx_usb->dev, "<%s> Change interface done\n",__func__);
  890. }
  891. }
  892. if (intr & INTR_SOF) {
  893. if (imx_usb->ep0state == EP0_IDLE) {
  894. temp = __raw_readl(imx_usb->base + USB_CTRL);
  895. __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);
  896. }
  897. }
  898. end_irq:
  899. __raw_writel(intr, imx_usb->base + USB_INTR);
  900. return IRQ_HANDLED;
  901. }
  902. static irqreturn_t imx_udc_ctrl_irq(int irq, void *dev)
  903. {
  904. struct imx_udc_struct *imx_usb = dev;
  905. int intr = __raw_readl(imx_usb->base + USB_EP_INTR(0));
  906. dump_ep_intr(__func__, 0, intr, imx_usb->dev);
  907. if (!imx_usb->driver) {
  908. __raw_writel(intr, imx_usb->base + USB_EP_INTR(0));
  909. return IRQ_HANDLED;
  910. }
  911. /* DEVREQ IRQ has highest priority */
  912. if (intr & (EPINTR_DEVREQ | EPINTR_MDEVREQ))
  913. handle_ep0_devreq(imx_usb);
  914. /* Seem i.MX is missing EOF interrupt sometimes.
  915. * Therefore we monitor both EOF and FIFO_EMPTY interrups
  916. * when transmiting, and both EOF and FIFO_FULL when
  917. * receiving data.
  918. */
  919. else if (intr & (EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL))
  920. handle_ep0(&imx_usb->imx_ep[0]);
  921. __raw_writel(intr, imx_usb->base + USB_EP_INTR(0));
  922. return IRQ_HANDLED;
  923. }
  924. static irqreturn_t imx_udc_bulk_irq(int irq, void *dev)
  925. {
  926. struct imx_udc_struct *imx_usb = dev;
  927. struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[irq - USBD_INT0];
  928. int intr = __raw_readl(imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
  929. dump_ep_intr(__func__, irq - USBD_INT0, intr, imx_usb->dev);
  930. if (!imx_usb->driver) {
  931. __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
  932. return IRQ_HANDLED;
  933. }
  934. handle_ep(imx_ep);
  935. __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
  936. return IRQ_HANDLED;
  937. }
  938. irq_handler_t intr_handler(int i)
  939. {
  940. switch (i) {
  941. case 0:
  942. return imx_udc_ctrl_irq;
  943. case 1:
  944. case 2:
  945. case 3:
  946. case 4:
  947. case 5:
  948. return imx_udc_bulk_irq;
  949. default:
  950. return imx_udc_irq;
  951. }
  952. }
  953. /*******************************************************************************
  954. * Static defined IMX UDC structure
  955. *******************************************************************************
  956. */
  957. static const struct usb_gadget_ops imx_udc_ops = {
  958. .get_frame = imx_udc_get_frame,
  959. .wakeup = imx_udc_wakeup,
  960. };
  961. static struct imx_udc_struct controller = {
  962. .gadget = {
  963. .ops = &imx_udc_ops,
  964. .ep0 = &controller.imx_ep[0].ep,
  965. .name = driver_name,
  966. .dev = {
  967. .bus_id = "gadget",
  968. },
  969. },
  970. .imx_ep[0] = {
  971. .ep = {
  972. .name = ep0name,
  973. .ops = &imx_ep_ops,
  974. .maxpacket = 32,
  975. },
  976. .imx_usb = &controller,
  977. .fifosize = 32,
  978. .bEndpointAddress = 0,
  979. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  980. },
  981. .imx_ep[1] = {
  982. .ep = {
  983. .name = "ep1in-bulk",
  984. .ops = &imx_ep_ops,
  985. .maxpacket = 64,
  986. },
  987. .imx_usb = &controller,
  988. .fifosize = 64,
  989. .bEndpointAddress = USB_DIR_IN | 1,
  990. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  991. },
  992. .imx_ep[2] = {
  993. .ep = {
  994. .name = "ep2out-bulk",
  995. .ops = &imx_ep_ops,
  996. .maxpacket = 64,
  997. },
  998. .imx_usb = &controller,
  999. .fifosize = 64,
  1000. .bEndpointAddress = USB_DIR_OUT | 2,
  1001. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1002. },
  1003. .imx_ep[3] = {
  1004. .ep = {
  1005. .name = "ep3out-bulk",
  1006. .ops = &imx_ep_ops,
  1007. .maxpacket = 32,
  1008. },
  1009. .imx_usb = &controller,
  1010. .fifosize = 32,
  1011. .bEndpointAddress = USB_DIR_OUT | 3,
  1012. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1013. },
  1014. .imx_ep[4] = {
  1015. .ep = {
  1016. .name = "ep4in-int",
  1017. .ops = &imx_ep_ops,
  1018. .maxpacket = 32,
  1019. },
  1020. .imx_usb = &controller,
  1021. .fifosize = 32,
  1022. .bEndpointAddress = USB_DIR_IN | 4,
  1023. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1024. },
  1025. .imx_ep[5] = {
  1026. .ep = {
  1027. .name = "ep5out-int",
  1028. .ops = &imx_ep_ops,
  1029. .maxpacket = 32,
  1030. },
  1031. .imx_usb = &controller,
  1032. .fifosize = 32,
  1033. .bEndpointAddress = USB_DIR_OUT | 5,
  1034. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1035. },
  1036. };
  1037. /*******************************************************************************
  1038. * USB gadged driver functions
  1039. *******************************************************************************
  1040. */
  1041. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  1042. {
  1043. struct imx_udc_struct *imx_usb = &controller;
  1044. int retval;
  1045. if (!driver
  1046. || driver->speed < USB_SPEED_FULL
  1047. || !driver->bind
  1048. || !driver->disconnect
  1049. || !driver->setup)
  1050. return -EINVAL;
  1051. if (!imx_usb)
  1052. return -ENODEV;
  1053. if (imx_usb->driver)
  1054. return -EBUSY;
  1055. /* first hook up the driver ... */
  1056. imx_usb->driver = driver;
  1057. imx_usb->gadget.dev.driver = &driver->driver;
  1058. retval = device_add(&imx_usb->gadget.dev);
  1059. if (retval)
  1060. goto fail;
  1061. retval = driver->bind(&imx_usb->gadget);
  1062. if (retval) {
  1063. D_ERR(imx_usb->dev, "<%s> bind to driver %s --> error %d\n",
  1064. __func__, driver->driver.name, retval);
  1065. device_del(&imx_usb->gadget.dev);
  1066. goto fail;
  1067. }
  1068. D_INI(imx_usb->dev, "<%s> registered gadget driver '%s'\n",
  1069. __func__, driver->driver.name);
  1070. imx_udc_enable(imx_usb);
  1071. return 0;
  1072. fail:
  1073. imx_usb->driver = NULL;
  1074. imx_usb->gadget.dev.driver = NULL;
  1075. return retval;
  1076. }
  1077. EXPORT_SYMBOL(usb_gadget_register_driver);
  1078. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1079. {
  1080. struct imx_udc_struct *imx_usb = &controller;
  1081. if (!imx_usb)
  1082. return -ENODEV;
  1083. if (!driver || driver != imx_usb->driver || !driver->unbind)
  1084. return -EINVAL;
  1085. udc_stop_activity(imx_usb, driver);
  1086. imx_udc_disable(imx_usb);
  1087. driver->unbind(&imx_usb->gadget);
  1088. imx_usb->gadget.dev.driver = NULL;
  1089. imx_usb->driver = NULL;
  1090. device_del(&imx_usb->gadget.dev);
  1091. D_INI(imx_usb->dev, "<%s> unregistered gadget driver '%s'\n",
  1092. __func__, driver->driver.name);
  1093. return 0;
  1094. }
  1095. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1096. /*******************************************************************************
  1097. * Module functions
  1098. *******************************************************************************
  1099. */
  1100. static int __init imx_udc_probe(struct platform_device *pdev)
  1101. {
  1102. struct imx_udc_struct *imx_usb = &controller;
  1103. struct resource *res;
  1104. struct imxusb_platform_data *pdata;
  1105. struct clk *clk;
  1106. void __iomem *base;
  1107. int ret = 0;
  1108. int i, res_size;
  1109. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1110. if (!res) {
  1111. dev_err(&pdev->dev, "can't get device resources\n");
  1112. return -ENODEV;
  1113. }
  1114. pdata = pdev->dev.platform_data;
  1115. if (!pdata) {
  1116. dev_err(&pdev->dev, "driver needs platform data\n");
  1117. return -ENODEV;
  1118. }
  1119. res_size = res->end - res->start + 1;
  1120. if (!request_mem_region(res->start, res_size, res->name)) {
  1121. dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n",
  1122. res_size, res->start);
  1123. return -ENOMEM;
  1124. }
  1125. if (pdata->init) {
  1126. ret = pdata->init(&pdev->dev);
  1127. if (ret)
  1128. goto fail0;
  1129. }
  1130. base = ioremap(res->start, res_size);
  1131. if (!base) {
  1132. dev_err(&pdev->dev, "ioremap failed\n");
  1133. ret = -EIO;
  1134. goto fail1;
  1135. }
  1136. clk = clk_get(NULL, "usbd_clk");
  1137. if (IS_ERR(clk)) {
  1138. ret = PTR_ERR(clk);
  1139. dev_err(&pdev->dev, "can't get USB clock\n");
  1140. goto fail2;
  1141. }
  1142. clk_enable(clk);
  1143. if (clk_get_rate(clk) != 48000000) {
  1144. D_INI(&pdev->dev,
  1145. "Bad USB clock (%d Hz), changing to 48000000 Hz\n",
  1146. (int)clk_get_rate(clk));
  1147. if (clk_set_rate(clk, 48000000)) {
  1148. dev_err(&pdev->dev,
  1149. "Unable to set correct USB clock (48MHz)\n");
  1150. ret = -EIO;
  1151. goto fail3;
  1152. }
  1153. }
  1154. for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
  1155. imx_usb->usbd_int[i] = platform_get_irq(pdev, i);
  1156. if (imx_usb->usbd_int[i] < 0) {
  1157. dev_err(&pdev->dev, "can't get irq number\n");
  1158. ret = -ENODEV;
  1159. goto fail3;
  1160. }
  1161. }
  1162. for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
  1163. ret = request_irq(imx_usb->usbd_int[i], intr_handler(i),
  1164. IRQF_DISABLED, driver_name, imx_usb);
  1165. if (ret) {
  1166. dev_err(&pdev->dev, "can't get irq %i, err %d\n",
  1167. imx_usb->usbd_int[i], ret);
  1168. for (--i; i >= 0; i--)
  1169. free_irq(imx_usb->usbd_int[i], imx_usb);
  1170. goto fail3;
  1171. }
  1172. }
  1173. imx_usb->res = res;
  1174. imx_usb->base = base;
  1175. imx_usb->clk = clk;
  1176. imx_usb->dev = &pdev->dev;
  1177. device_initialize(&imx_usb->gadget.dev);
  1178. imx_usb->gadget.dev.parent = &pdev->dev;
  1179. imx_usb->gadget.dev.dma_mask = pdev->dev.dma_mask;
  1180. platform_set_drvdata(pdev, imx_usb);
  1181. usb_init_data(imx_usb);
  1182. imx_udc_init(imx_usb);
  1183. return 0;
  1184. fail3:
  1185. clk_put(clk);
  1186. clk_disable(clk);
  1187. fail2:
  1188. iounmap(base);
  1189. fail1:
  1190. if (pdata->exit)
  1191. pdata->exit(&pdev->dev);
  1192. fail0:
  1193. release_mem_region(res->start, res_size);
  1194. return ret;
  1195. }
  1196. static int __exit imx_udc_remove(struct platform_device *pdev)
  1197. {
  1198. struct imx_udc_struct *imx_usb = platform_get_drvdata(pdev);
  1199. struct imxusb_platform_data *pdata = pdev->dev.platform_data;
  1200. int i;
  1201. imx_udc_disable(imx_usb);
  1202. for (i = 0; i < IMX_USB_NB_EP + 1; i++)
  1203. free_irq(imx_usb->usbd_int[i], imx_usb);
  1204. clk_put(imx_usb->clk);
  1205. clk_disable(imx_usb->clk);
  1206. iounmap(imx_usb->base);
  1207. release_mem_region(imx_usb->res->start,
  1208. imx_usb->res->end - imx_usb->res->start + 1);
  1209. if (pdata->exit)
  1210. pdata->exit(&pdev->dev);
  1211. platform_set_drvdata(pdev, NULL);
  1212. return 0;
  1213. }
  1214. /*----------------------------------------------------------------------------*/
  1215. #ifdef CONFIG_PM
  1216. #define imx_udc_suspend NULL
  1217. #define imx_udc_resume NULL
  1218. #else
  1219. #define imx_udc_suspend NULL
  1220. #define imx_udc_resume NULL
  1221. #endif
  1222. /*----------------------------------------------------------------------------*/
  1223. static struct platform_driver udc_driver = {
  1224. .driver = {
  1225. .name = driver_name,
  1226. .owner = THIS_MODULE,
  1227. },
  1228. .remove = __exit_p(imx_udc_remove),
  1229. .suspend = imx_udc_suspend,
  1230. .resume = imx_udc_resume,
  1231. };
  1232. static int __init udc_init(void)
  1233. {
  1234. return platform_driver_probe(&udc_driver, imx_udc_probe);
  1235. }
  1236. module_init(udc_init);
  1237. static void __exit udc_exit(void)
  1238. {
  1239. platform_driver_unregister(&udc_driver);
  1240. }
  1241. module_exit(udc_exit);
  1242. MODULE_DESCRIPTION("IMX USB Device Controller driver");
  1243. MODULE_AUTHOR("Darius Augulis <augulis.darius@gmail.com>");
  1244. MODULE_LICENSE("GPL");
  1245. MODULE_ALIAS("platform:imx_udc");