imx_udc.c 38 KB

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