fsl_udc_core.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769
  1. /*
  2. * Copyright (C) 2004-2007,2011-2012 Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Author: Li Yang <leoli@freescale.com>
  6. * Jiang Bo <tanya.jiang@freescale.com>
  7. *
  8. * Description:
  9. * Freescale high-speed USB SOC DR module device controller driver.
  10. * This can be found on MPC8349E/MPC8313E/MPC5121E cpus.
  11. * The driver is previously named as mpc_udc. Based on bare board
  12. * code from Dave Liu and Shlomi Gridish.
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the License, or (at your
  17. * option) any later version.
  18. */
  19. #undef VERBOSE
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/ioport.h>
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/err.h>
  26. #include <linux/slab.h>
  27. #include <linux/init.h>
  28. #include <linux/list.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/mm.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/device.h>
  34. #include <linux/usb/ch9.h>
  35. #include <linux/usb/gadget.h>
  36. #include <linux/usb/otg.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/fsl_devices.h>
  40. #include <linux/dmapool.h>
  41. #include <linux/delay.h>
  42. #include <linux/of_device.h>
  43. #include <asm/byteorder.h>
  44. #include <asm/io.h>
  45. #include <asm/unaligned.h>
  46. #include <asm/dma.h>
  47. #include "fsl_usb2_udc.h"
  48. #define DRIVER_DESC "Freescale High-Speed USB SOC Device Controller driver"
  49. #define DRIVER_AUTHOR "Li Yang/Jiang Bo"
  50. #define DRIVER_VERSION "Apr 20, 2007"
  51. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  52. static const char driver_name[] = "fsl-usb2-udc";
  53. static const char driver_desc[] = DRIVER_DESC;
  54. static struct usb_dr_device *dr_regs;
  55. static struct usb_sys_interface *usb_sys_regs;
  56. /* it is initialized in probe() */
  57. static struct fsl_udc *udc_controller = NULL;
  58. static const struct usb_endpoint_descriptor
  59. fsl_ep0_desc = {
  60. .bLength = USB_DT_ENDPOINT_SIZE,
  61. .bDescriptorType = USB_DT_ENDPOINT,
  62. .bEndpointAddress = 0,
  63. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  64. .wMaxPacketSize = USB_MAX_CTRL_PAYLOAD,
  65. };
  66. static void fsl_ep_fifo_flush(struct usb_ep *_ep);
  67. #ifdef CONFIG_PPC32
  68. /*
  69. * On some SoCs, the USB controller registers can be big or little endian,
  70. * depending on the version of the chip. In order to be able to run the
  71. * same kernel binary on 2 different versions of an SoC, the BE/LE decision
  72. * must be made at run time. _fsl_readl and fsl_writel are pointers to the
  73. * BE or LE readl() and writel() functions, and fsl_readl() and fsl_writel()
  74. * call through those pointers. Platform code for SoCs that have BE USB
  75. * registers should set pdata->big_endian_mmio flag.
  76. *
  77. * This also applies to controller-to-cpu accessors for the USB descriptors,
  78. * since their endianness is also SoC dependant. Platform code for SoCs that
  79. * have BE USB descriptors should set pdata->big_endian_desc flag.
  80. */
  81. static u32 _fsl_readl_be(const unsigned __iomem *p)
  82. {
  83. return in_be32(p);
  84. }
  85. static u32 _fsl_readl_le(const unsigned __iomem *p)
  86. {
  87. return in_le32(p);
  88. }
  89. static void _fsl_writel_be(u32 v, unsigned __iomem *p)
  90. {
  91. out_be32(p, v);
  92. }
  93. static void _fsl_writel_le(u32 v, unsigned __iomem *p)
  94. {
  95. out_le32(p, v);
  96. }
  97. static u32 (*_fsl_readl)(const unsigned __iomem *p);
  98. static void (*_fsl_writel)(u32 v, unsigned __iomem *p);
  99. #define fsl_readl(p) (*_fsl_readl)((p))
  100. #define fsl_writel(v, p) (*_fsl_writel)((v), (p))
  101. static inline void fsl_set_accessors(struct fsl_usb2_platform_data *pdata)
  102. {
  103. if (pdata->big_endian_mmio) {
  104. _fsl_readl = _fsl_readl_be;
  105. _fsl_writel = _fsl_writel_be;
  106. } else {
  107. _fsl_readl = _fsl_readl_le;
  108. _fsl_writel = _fsl_writel_le;
  109. }
  110. }
  111. static inline u32 cpu_to_hc32(const u32 x)
  112. {
  113. return udc_controller->pdata->big_endian_desc
  114. ? (__force u32)cpu_to_be32(x)
  115. : (__force u32)cpu_to_le32(x);
  116. }
  117. static inline u32 hc32_to_cpu(const u32 x)
  118. {
  119. return udc_controller->pdata->big_endian_desc
  120. ? be32_to_cpu((__force __be32)x)
  121. : le32_to_cpu((__force __le32)x);
  122. }
  123. #else /* !CONFIG_PPC32 */
  124. static inline void fsl_set_accessors(struct fsl_usb2_platform_data *pdata) {}
  125. #define fsl_readl(addr) readl(addr)
  126. #define fsl_writel(val32, addr) writel(val32, addr)
  127. #define cpu_to_hc32(x) cpu_to_le32(x)
  128. #define hc32_to_cpu(x) le32_to_cpu(x)
  129. #endif /* CONFIG_PPC32 */
  130. /********************************************************************
  131. * Internal Used Function
  132. ********************************************************************/
  133. /*-----------------------------------------------------------------
  134. * done() - retire a request; caller blocked irqs
  135. * @status : request status to be set, only works when
  136. * request is still in progress.
  137. *--------------------------------------------------------------*/
  138. static void done(struct fsl_ep *ep, struct fsl_req *req, int status)
  139. {
  140. struct fsl_udc *udc = NULL;
  141. unsigned char stopped = ep->stopped;
  142. struct ep_td_struct *curr_td, *next_td;
  143. int j;
  144. udc = (struct fsl_udc *)ep->udc;
  145. /* Removed the req from fsl_ep->queue */
  146. list_del_init(&req->queue);
  147. /* req.status should be set as -EINPROGRESS in ep_queue() */
  148. if (req->req.status == -EINPROGRESS)
  149. req->req.status = status;
  150. else
  151. status = req->req.status;
  152. /* Free dtd for the request */
  153. next_td = req->head;
  154. for (j = 0; j < req->dtd_count; j++) {
  155. curr_td = next_td;
  156. if (j != req->dtd_count - 1) {
  157. next_td = curr_td->next_td_virt;
  158. }
  159. dma_pool_free(udc->td_pool, curr_td, curr_td->td_dma);
  160. }
  161. if (req->mapped) {
  162. dma_unmap_single(ep->udc->gadget.dev.parent,
  163. req->req.dma, req->req.length,
  164. ep_is_in(ep)
  165. ? DMA_TO_DEVICE
  166. : DMA_FROM_DEVICE);
  167. req->req.dma = DMA_ADDR_INVALID;
  168. req->mapped = 0;
  169. } else
  170. dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
  171. req->req.dma, req->req.length,
  172. ep_is_in(ep)
  173. ? DMA_TO_DEVICE
  174. : DMA_FROM_DEVICE);
  175. if (status && (status != -ESHUTDOWN))
  176. VDBG("complete %s req %p stat %d len %u/%u",
  177. ep->ep.name, &req->req, status,
  178. req->req.actual, req->req.length);
  179. ep->stopped = 1;
  180. spin_unlock(&ep->udc->lock);
  181. /* complete() is from gadget layer,
  182. * eg fsg->bulk_in_complete() */
  183. if (req->req.complete)
  184. req->req.complete(&ep->ep, &req->req);
  185. spin_lock(&ep->udc->lock);
  186. ep->stopped = stopped;
  187. }
  188. /*-----------------------------------------------------------------
  189. * nuke(): delete all requests related to this ep
  190. * called with spinlock held
  191. *--------------------------------------------------------------*/
  192. static void nuke(struct fsl_ep *ep, int status)
  193. {
  194. ep->stopped = 1;
  195. /* Flush fifo */
  196. fsl_ep_fifo_flush(&ep->ep);
  197. /* Whether this eq has request linked */
  198. while (!list_empty(&ep->queue)) {
  199. struct fsl_req *req = NULL;
  200. req = list_entry(ep->queue.next, struct fsl_req, queue);
  201. done(ep, req, status);
  202. }
  203. }
  204. /*------------------------------------------------------------------
  205. Internal Hardware related function
  206. ------------------------------------------------------------------*/
  207. static int dr_controller_setup(struct fsl_udc *udc)
  208. {
  209. unsigned int tmp, portctrl, ep_num;
  210. unsigned int max_no_of_ep;
  211. unsigned int ctrl;
  212. unsigned long timeout;
  213. #define FSL_UDC_RESET_TIMEOUT 1000
  214. /* Config PHY interface */
  215. portctrl = fsl_readl(&dr_regs->portsc1);
  216. portctrl &= ~(PORTSCX_PHY_TYPE_SEL | PORTSCX_PORT_WIDTH);
  217. switch (udc->phy_mode) {
  218. case FSL_USB2_PHY_ULPI:
  219. if (udc->pdata->have_sysif_regs) {
  220. if (udc->pdata->controller_ver) {
  221. /* controller version 1.6 or above */
  222. ctrl = __raw_readl(&usb_sys_regs->control);
  223. ctrl &= ~USB_CTRL_UTMI_PHY_EN;
  224. ctrl |= USB_CTRL_USB_EN;
  225. __raw_writel(ctrl, &usb_sys_regs->control);
  226. }
  227. }
  228. portctrl |= PORTSCX_PTS_ULPI;
  229. break;
  230. case FSL_USB2_PHY_UTMI_WIDE:
  231. portctrl |= PORTSCX_PTW_16BIT;
  232. /* fall through */
  233. case FSL_USB2_PHY_UTMI:
  234. if (udc->pdata->have_sysif_regs) {
  235. if (udc->pdata->controller_ver) {
  236. /* controller version 1.6 or above */
  237. ctrl = __raw_readl(&usb_sys_regs->control);
  238. ctrl |= (USB_CTRL_UTMI_PHY_EN |
  239. USB_CTRL_USB_EN);
  240. __raw_writel(ctrl, &usb_sys_regs->control);
  241. mdelay(FSL_UTMI_PHY_DLY); /* Delay for UTMI
  242. PHY CLK to become stable - 10ms*/
  243. }
  244. }
  245. portctrl |= PORTSCX_PTS_UTMI;
  246. break;
  247. case FSL_USB2_PHY_SERIAL:
  248. portctrl |= PORTSCX_PTS_FSLS;
  249. break;
  250. default:
  251. return -EINVAL;
  252. }
  253. fsl_writel(portctrl, &dr_regs->portsc1);
  254. /* Stop and reset the usb controller */
  255. tmp = fsl_readl(&dr_regs->usbcmd);
  256. tmp &= ~USB_CMD_RUN_STOP;
  257. fsl_writel(tmp, &dr_regs->usbcmd);
  258. tmp = fsl_readl(&dr_regs->usbcmd);
  259. tmp |= USB_CMD_CTRL_RESET;
  260. fsl_writel(tmp, &dr_regs->usbcmd);
  261. /* Wait for reset to complete */
  262. timeout = jiffies + FSL_UDC_RESET_TIMEOUT;
  263. while (fsl_readl(&dr_regs->usbcmd) & USB_CMD_CTRL_RESET) {
  264. if (time_after(jiffies, timeout)) {
  265. ERR("udc reset timeout!\n");
  266. return -ETIMEDOUT;
  267. }
  268. cpu_relax();
  269. }
  270. /* Set the controller as device mode */
  271. tmp = fsl_readl(&dr_regs->usbmode);
  272. tmp &= ~USB_MODE_CTRL_MODE_MASK; /* clear mode bits */
  273. tmp |= USB_MODE_CTRL_MODE_DEVICE;
  274. /* Disable Setup Lockout */
  275. tmp |= USB_MODE_SETUP_LOCK_OFF;
  276. if (udc->pdata->es)
  277. tmp |= USB_MODE_ES;
  278. fsl_writel(tmp, &dr_regs->usbmode);
  279. /* Clear the setup status */
  280. fsl_writel(0, &dr_regs->usbsts);
  281. tmp = udc->ep_qh_dma;
  282. tmp &= USB_EP_LIST_ADDRESS_MASK;
  283. fsl_writel(tmp, &dr_regs->endpointlistaddr);
  284. VDBG("vir[qh_base] is %p phy[qh_base] is 0x%8x reg is 0x%8x",
  285. udc->ep_qh, (int)tmp,
  286. fsl_readl(&dr_regs->endpointlistaddr));
  287. max_no_of_ep = (0x0000001F & fsl_readl(&dr_regs->dccparams));
  288. for (ep_num = 1; ep_num < max_no_of_ep; ep_num++) {
  289. tmp = fsl_readl(&dr_regs->endptctrl[ep_num]);
  290. tmp &= ~(EPCTRL_TX_TYPE | EPCTRL_RX_TYPE);
  291. tmp |= (EPCTRL_EP_TYPE_BULK << EPCTRL_TX_EP_TYPE_SHIFT)
  292. | (EPCTRL_EP_TYPE_BULK << EPCTRL_RX_EP_TYPE_SHIFT);
  293. fsl_writel(tmp, &dr_regs->endptctrl[ep_num]);
  294. }
  295. /* Config control enable i/o output, cpu endian register */
  296. #ifndef CONFIG_ARCH_MXC
  297. if (udc->pdata->have_sysif_regs) {
  298. ctrl = __raw_readl(&usb_sys_regs->control);
  299. ctrl |= USB_CTRL_IOENB;
  300. __raw_writel(ctrl, &usb_sys_regs->control);
  301. }
  302. #endif
  303. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  304. /* Turn on cache snooping hardware, since some PowerPC platforms
  305. * wholly rely on hardware to deal with cache coherent. */
  306. if (udc->pdata->have_sysif_regs) {
  307. /* Setup Snooping for all the 4GB space */
  308. tmp = SNOOP_SIZE_2GB; /* starts from 0x0, size 2G */
  309. __raw_writel(tmp, &usb_sys_regs->snoop1);
  310. tmp |= 0x80000000; /* starts from 0x8000000, size 2G */
  311. __raw_writel(tmp, &usb_sys_regs->snoop2);
  312. }
  313. #endif
  314. return 0;
  315. }
  316. /* Enable DR irq and set controller to run state */
  317. static void dr_controller_run(struct fsl_udc *udc)
  318. {
  319. u32 temp;
  320. /* Enable DR irq reg */
  321. temp = USB_INTR_INT_EN | USB_INTR_ERR_INT_EN
  322. | USB_INTR_PTC_DETECT_EN | USB_INTR_RESET_EN
  323. | USB_INTR_DEVICE_SUSPEND | USB_INTR_SYS_ERR_EN;
  324. fsl_writel(temp, &dr_regs->usbintr);
  325. /* Clear stopped bit */
  326. udc->stopped = 0;
  327. /* Set the controller as device mode */
  328. temp = fsl_readl(&dr_regs->usbmode);
  329. temp |= USB_MODE_CTRL_MODE_DEVICE;
  330. fsl_writel(temp, &dr_regs->usbmode);
  331. /* Set controller to Run */
  332. temp = fsl_readl(&dr_regs->usbcmd);
  333. temp |= USB_CMD_RUN_STOP;
  334. fsl_writel(temp, &dr_regs->usbcmd);
  335. }
  336. static void dr_controller_stop(struct fsl_udc *udc)
  337. {
  338. unsigned int tmp;
  339. pr_debug("%s\n", __func__);
  340. /* if we're in OTG mode, and the Host is currently using the port,
  341. * stop now and don't rip the controller out from under the
  342. * ehci driver
  343. */
  344. if (udc->gadget.is_otg) {
  345. if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID)) {
  346. pr_debug("udc: Leaving early\n");
  347. return;
  348. }
  349. }
  350. /* disable all INTR */
  351. fsl_writel(0, &dr_regs->usbintr);
  352. /* Set stopped bit for isr */
  353. udc->stopped = 1;
  354. /* disable IO output */
  355. /* usb_sys_regs->control = 0; */
  356. /* set controller to Stop */
  357. tmp = fsl_readl(&dr_regs->usbcmd);
  358. tmp &= ~USB_CMD_RUN_STOP;
  359. fsl_writel(tmp, &dr_regs->usbcmd);
  360. }
  361. static void dr_ep_setup(unsigned char ep_num, unsigned char dir,
  362. unsigned char ep_type)
  363. {
  364. unsigned int tmp_epctrl = 0;
  365. tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  366. if (dir) {
  367. if (ep_num)
  368. tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
  369. tmp_epctrl |= EPCTRL_TX_ENABLE;
  370. tmp_epctrl &= ~EPCTRL_TX_TYPE;
  371. tmp_epctrl |= ((unsigned int)(ep_type)
  372. << EPCTRL_TX_EP_TYPE_SHIFT);
  373. } else {
  374. if (ep_num)
  375. tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
  376. tmp_epctrl |= EPCTRL_RX_ENABLE;
  377. tmp_epctrl &= ~EPCTRL_RX_TYPE;
  378. tmp_epctrl |= ((unsigned int)(ep_type)
  379. << EPCTRL_RX_EP_TYPE_SHIFT);
  380. }
  381. fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
  382. }
  383. static void
  384. dr_ep_change_stall(unsigned char ep_num, unsigned char dir, int value)
  385. {
  386. u32 tmp_epctrl = 0;
  387. tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  388. if (value) {
  389. /* set the stall bit */
  390. if (dir)
  391. tmp_epctrl |= EPCTRL_TX_EP_STALL;
  392. else
  393. tmp_epctrl |= EPCTRL_RX_EP_STALL;
  394. } else {
  395. /* clear the stall bit and reset data toggle */
  396. if (dir) {
  397. tmp_epctrl &= ~EPCTRL_TX_EP_STALL;
  398. tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
  399. } else {
  400. tmp_epctrl &= ~EPCTRL_RX_EP_STALL;
  401. tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
  402. }
  403. }
  404. fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
  405. }
  406. /* Get stall status of a specific ep
  407. Return: 0: not stalled; 1:stalled */
  408. static int dr_ep_get_stall(unsigned char ep_num, unsigned char dir)
  409. {
  410. u32 epctrl;
  411. epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  412. if (dir)
  413. return (epctrl & EPCTRL_TX_EP_STALL) ? 1 : 0;
  414. else
  415. return (epctrl & EPCTRL_RX_EP_STALL) ? 1 : 0;
  416. }
  417. /********************************************************************
  418. Internal Structure Build up functions
  419. ********************************************************************/
  420. /*------------------------------------------------------------------
  421. * struct_ep_qh_setup(): set the Endpoint Capabilites field of QH
  422. * @zlt: Zero Length Termination Select (1: disable; 0: enable)
  423. * @mult: Mult field
  424. ------------------------------------------------------------------*/
  425. static void struct_ep_qh_setup(struct fsl_udc *udc, unsigned char ep_num,
  426. unsigned char dir, unsigned char ep_type,
  427. unsigned int max_pkt_len,
  428. unsigned int zlt, unsigned char mult)
  429. {
  430. struct ep_queue_head *p_QH = &udc->ep_qh[2 * ep_num + dir];
  431. unsigned int tmp = 0;
  432. /* set the Endpoint Capabilites in QH */
  433. switch (ep_type) {
  434. case USB_ENDPOINT_XFER_CONTROL:
  435. /* Interrupt On Setup (IOS). for control ep */
  436. tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
  437. | EP_QUEUE_HEAD_IOS;
  438. break;
  439. case USB_ENDPOINT_XFER_ISOC:
  440. tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
  441. | (mult << EP_QUEUE_HEAD_MULT_POS);
  442. break;
  443. case USB_ENDPOINT_XFER_BULK:
  444. case USB_ENDPOINT_XFER_INT:
  445. tmp = max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS;
  446. break;
  447. default:
  448. VDBG("error ep type is %d", ep_type);
  449. return;
  450. }
  451. if (zlt)
  452. tmp |= EP_QUEUE_HEAD_ZLT_SEL;
  453. p_QH->max_pkt_length = cpu_to_hc32(tmp);
  454. p_QH->next_dtd_ptr = 1;
  455. p_QH->size_ioc_int_sts = 0;
  456. }
  457. /* Setup qh structure and ep register for ep0. */
  458. static void ep0_setup(struct fsl_udc *udc)
  459. {
  460. /* the intialization of an ep includes: fields in QH, Regs,
  461. * fsl_ep struct */
  462. struct_ep_qh_setup(udc, 0, USB_RECV, USB_ENDPOINT_XFER_CONTROL,
  463. USB_MAX_CTRL_PAYLOAD, 0, 0);
  464. struct_ep_qh_setup(udc, 0, USB_SEND, USB_ENDPOINT_XFER_CONTROL,
  465. USB_MAX_CTRL_PAYLOAD, 0, 0);
  466. dr_ep_setup(0, USB_RECV, USB_ENDPOINT_XFER_CONTROL);
  467. dr_ep_setup(0, USB_SEND, USB_ENDPOINT_XFER_CONTROL);
  468. return;
  469. }
  470. /***********************************************************************
  471. Endpoint Management Functions
  472. ***********************************************************************/
  473. /*-------------------------------------------------------------------------
  474. * when configurations are set, or when interface settings change
  475. * for example the do_set_interface() in gadget layer,
  476. * the driver will enable or disable the relevant endpoints
  477. * ep0 doesn't use this routine. It is always enabled.
  478. -------------------------------------------------------------------------*/
  479. static int fsl_ep_enable(struct usb_ep *_ep,
  480. const struct usb_endpoint_descriptor *desc)
  481. {
  482. struct fsl_udc *udc = NULL;
  483. struct fsl_ep *ep = NULL;
  484. unsigned short max = 0;
  485. unsigned char mult = 0, zlt;
  486. int retval = -EINVAL;
  487. unsigned long flags = 0;
  488. ep = container_of(_ep, struct fsl_ep, ep);
  489. /* catch various bogus parameters */
  490. if (!_ep || !desc
  491. || (desc->bDescriptorType != USB_DT_ENDPOINT))
  492. return -EINVAL;
  493. udc = ep->udc;
  494. if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
  495. return -ESHUTDOWN;
  496. max = usb_endpoint_maxp(desc);
  497. /* Disable automatic zlp generation. Driver is responsible to indicate
  498. * explicitly through req->req.zero. This is needed to enable multi-td
  499. * request. */
  500. zlt = 1;
  501. /* Assume the max packet size from gadget is always correct */
  502. switch (desc->bmAttributes & 0x03) {
  503. case USB_ENDPOINT_XFER_CONTROL:
  504. case USB_ENDPOINT_XFER_BULK:
  505. case USB_ENDPOINT_XFER_INT:
  506. /* mult = 0. Execute N Transactions as demonstrated by
  507. * the USB variable length packet protocol where N is
  508. * computed using the Maximum Packet Length (dQH) and
  509. * the Total Bytes field (dTD) */
  510. mult = 0;
  511. break;
  512. case USB_ENDPOINT_XFER_ISOC:
  513. /* Calculate transactions needed for high bandwidth iso */
  514. mult = (unsigned char)(1 + ((max >> 11) & 0x03));
  515. max = max & 0x7ff; /* bit 0~10 */
  516. /* 3 transactions at most */
  517. if (mult > 3)
  518. goto en_done;
  519. break;
  520. default:
  521. goto en_done;
  522. }
  523. spin_lock_irqsave(&udc->lock, flags);
  524. ep->ep.maxpacket = max;
  525. ep->ep.desc = desc;
  526. ep->stopped = 0;
  527. /* Controller related setup */
  528. /* Init EPx Queue Head (Ep Capabilites field in QH
  529. * according to max, zlt, mult) */
  530. struct_ep_qh_setup(udc, (unsigned char) ep_index(ep),
  531. (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
  532. ? USB_SEND : USB_RECV),
  533. (unsigned char) (desc->bmAttributes
  534. & USB_ENDPOINT_XFERTYPE_MASK),
  535. max, zlt, mult);
  536. /* Init endpoint ctrl register */
  537. dr_ep_setup((unsigned char) ep_index(ep),
  538. (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
  539. ? USB_SEND : USB_RECV),
  540. (unsigned char) (desc->bmAttributes
  541. & USB_ENDPOINT_XFERTYPE_MASK));
  542. spin_unlock_irqrestore(&udc->lock, flags);
  543. retval = 0;
  544. VDBG("enabled %s (ep%d%s) maxpacket %d",ep->ep.name,
  545. ep->ep.desc->bEndpointAddress & 0x0f,
  546. (desc->bEndpointAddress & USB_DIR_IN)
  547. ? "in" : "out", max);
  548. en_done:
  549. return retval;
  550. }
  551. /*---------------------------------------------------------------------
  552. * @ep : the ep being unconfigured. May not be ep0
  553. * Any pending and uncomplete req will complete with status (-ESHUTDOWN)
  554. *---------------------------------------------------------------------*/
  555. static int fsl_ep_disable(struct usb_ep *_ep)
  556. {
  557. struct fsl_udc *udc = NULL;
  558. struct fsl_ep *ep = NULL;
  559. unsigned long flags = 0;
  560. u32 epctrl;
  561. int ep_num;
  562. ep = container_of(_ep, struct fsl_ep, ep);
  563. if (!_ep || !ep->ep.desc) {
  564. VDBG("%s not enabled", _ep ? ep->ep.name : NULL);
  565. return -EINVAL;
  566. }
  567. /* disable ep on controller */
  568. ep_num = ep_index(ep);
  569. epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  570. if (ep_is_in(ep)) {
  571. epctrl &= ~(EPCTRL_TX_ENABLE | EPCTRL_TX_TYPE);
  572. epctrl |= EPCTRL_EP_TYPE_BULK << EPCTRL_TX_EP_TYPE_SHIFT;
  573. } else {
  574. epctrl &= ~(EPCTRL_RX_ENABLE | EPCTRL_TX_TYPE);
  575. epctrl |= EPCTRL_EP_TYPE_BULK << EPCTRL_RX_EP_TYPE_SHIFT;
  576. }
  577. fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
  578. udc = (struct fsl_udc *)ep->udc;
  579. spin_lock_irqsave(&udc->lock, flags);
  580. /* nuke all pending requests (does flush) */
  581. nuke(ep, -ESHUTDOWN);
  582. ep->ep.desc = NULL;
  583. ep->stopped = 1;
  584. spin_unlock_irqrestore(&udc->lock, flags);
  585. VDBG("disabled %s OK", _ep->name);
  586. return 0;
  587. }
  588. /*---------------------------------------------------------------------
  589. * allocate a request object used by this endpoint
  590. * the main operation is to insert the req->queue to the eq->queue
  591. * Returns the request, or null if one could not be allocated
  592. *---------------------------------------------------------------------*/
  593. static struct usb_request *
  594. fsl_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  595. {
  596. struct fsl_req *req = NULL;
  597. req = kzalloc(sizeof *req, gfp_flags);
  598. if (!req)
  599. return NULL;
  600. req->req.dma = DMA_ADDR_INVALID;
  601. INIT_LIST_HEAD(&req->queue);
  602. return &req->req;
  603. }
  604. static void fsl_free_request(struct usb_ep *_ep, struct usb_request *_req)
  605. {
  606. struct fsl_req *req = NULL;
  607. req = container_of(_req, struct fsl_req, req);
  608. if (_req)
  609. kfree(req);
  610. }
  611. /* Actually add a dTD chain to an empty dQH and let go */
  612. static void fsl_prime_ep(struct fsl_ep *ep, struct ep_td_struct *td)
  613. {
  614. struct ep_queue_head *qh = get_qh_by_ep(ep);
  615. /* Write dQH next pointer and terminate bit to 0 */
  616. qh->next_dtd_ptr = cpu_to_hc32(td->td_dma
  617. & EP_QUEUE_HEAD_NEXT_POINTER_MASK);
  618. /* Clear active and halt bit */
  619. qh->size_ioc_int_sts &= cpu_to_hc32(~(EP_QUEUE_HEAD_STATUS_ACTIVE
  620. | EP_QUEUE_HEAD_STATUS_HALT));
  621. /* Ensure that updates to the QH will occur before priming. */
  622. wmb();
  623. /* Prime endpoint by writing correct bit to ENDPTPRIME */
  624. fsl_writel(ep_is_in(ep) ? (1 << (ep_index(ep) + 16))
  625. : (1 << (ep_index(ep))), &dr_regs->endpointprime);
  626. }
  627. /* Add dTD chain to the dQH of an EP */
  628. static void fsl_queue_td(struct fsl_ep *ep, struct fsl_req *req)
  629. {
  630. u32 temp, bitmask, tmp_stat;
  631. /* VDBG("QH addr Register 0x%8x", dr_regs->endpointlistaddr);
  632. VDBG("ep_qh[%d] addr is 0x%8x", i, (u32)&(ep->udc->ep_qh[i])); */
  633. bitmask = ep_is_in(ep)
  634. ? (1 << (ep_index(ep) + 16))
  635. : (1 << (ep_index(ep)));
  636. /* check if the pipe is empty */
  637. if (!(list_empty(&ep->queue)) && !(ep_index(ep) == 0)) {
  638. /* Add td to the end */
  639. struct fsl_req *lastreq;
  640. lastreq = list_entry(ep->queue.prev, struct fsl_req, queue);
  641. lastreq->tail->next_td_ptr =
  642. cpu_to_hc32(req->head->td_dma & DTD_ADDR_MASK);
  643. /* Ensure dTD's next dtd pointer to be updated */
  644. wmb();
  645. /* Read prime bit, if 1 goto done */
  646. if (fsl_readl(&dr_regs->endpointprime) & bitmask)
  647. return;
  648. do {
  649. /* Set ATDTW bit in USBCMD */
  650. temp = fsl_readl(&dr_regs->usbcmd);
  651. fsl_writel(temp | USB_CMD_ATDTW, &dr_regs->usbcmd);
  652. /* Read correct status bit */
  653. tmp_stat = fsl_readl(&dr_regs->endptstatus) & bitmask;
  654. } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_ATDTW));
  655. /* Write ATDTW bit to 0 */
  656. temp = fsl_readl(&dr_regs->usbcmd);
  657. fsl_writel(temp & ~USB_CMD_ATDTW, &dr_regs->usbcmd);
  658. if (tmp_stat)
  659. return;
  660. }
  661. fsl_prime_ep(ep, req->head);
  662. }
  663. /* Fill in the dTD structure
  664. * @req: request that the transfer belongs to
  665. * @length: return actually data length of the dTD
  666. * @dma: return dma address of the dTD
  667. * @is_last: return flag if it is the last dTD of the request
  668. * return: pointer to the built dTD */
  669. static struct ep_td_struct *fsl_build_dtd(struct fsl_req *req, unsigned *length,
  670. dma_addr_t *dma, int *is_last, gfp_t gfp_flags)
  671. {
  672. u32 swap_temp;
  673. struct ep_td_struct *dtd;
  674. /* how big will this transfer be? */
  675. *length = min(req->req.length - req->req.actual,
  676. (unsigned)EP_MAX_LENGTH_TRANSFER);
  677. dtd = dma_pool_alloc(udc_controller->td_pool, gfp_flags, dma);
  678. if (dtd == NULL)
  679. return dtd;
  680. dtd->td_dma = *dma;
  681. /* Clear reserved field */
  682. swap_temp = hc32_to_cpu(dtd->size_ioc_sts);
  683. swap_temp &= ~DTD_RESERVED_FIELDS;
  684. dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
  685. /* Init all of buffer page pointers */
  686. swap_temp = (u32) (req->req.dma + req->req.actual);
  687. dtd->buff_ptr0 = cpu_to_hc32(swap_temp);
  688. dtd->buff_ptr1 = cpu_to_hc32(swap_temp + 0x1000);
  689. dtd->buff_ptr2 = cpu_to_hc32(swap_temp + 0x2000);
  690. dtd->buff_ptr3 = cpu_to_hc32(swap_temp + 0x3000);
  691. dtd->buff_ptr4 = cpu_to_hc32(swap_temp + 0x4000);
  692. req->req.actual += *length;
  693. /* zlp is needed if req->req.zero is set */
  694. if (req->req.zero) {
  695. if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
  696. *is_last = 1;
  697. else
  698. *is_last = 0;
  699. } else if (req->req.length == req->req.actual)
  700. *is_last = 1;
  701. else
  702. *is_last = 0;
  703. if ((*is_last) == 0)
  704. VDBG("multi-dtd request!");
  705. /* Fill in the transfer size; set active bit */
  706. swap_temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
  707. /* Enable interrupt for the last dtd of a request */
  708. if (*is_last && !req->req.no_interrupt)
  709. swap_temp |= DTD_IOC;
  710. dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
  711. mb();
  712. VDBG("length = %d address= 0x%x", *length, (int)*dma);
  713. return dtd;
  714. }
  715. /* Generate dtd chain for a request */
  716. static int fsl_req_to_dtd(struct fsl_req *req, gfp_t gfp_flags)
  717. {
  718. unsigned count;
  719. int is_last;
  720. int is_first =1;
  721. struct ep_td_struct *last_dtd = NULL, *dtd;
  722. dma_addr_t dma;
  723. do {
  724. dtd = fsl_build_dtd(req, &count, &dma, &is_last, gfp_flags);
  725. if (dtd == NULL)
  726. return -ENOMEM;
  727. if (is_first) {
  728. is_first = 0;
  729. req->head = dtd;
  730. } else {
  731. last_dtd->next_td_ptr = cpu_to_hc32(dma);
  732. last_dtd->next_td_virt = dtd;
  733. }
  734. last_dtd = dtd;
  735. req->dtd_count++;
  736. } while (!is_last);
  737. dtd->next_td_ptr = cpu_to_hc32(DTD_NEXT_TERMINATE);
  738. req->tail = dtd;
  739. return 0;
  740. }
  741. /* queues (submits) an I/O request to an endpoint */
  742. static int
  743. fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  744. {
  745. struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
  746. struct fsl_req *req = container_of(_req, struct fsl_req, req);
  747. struct fsl_udc *udc;
  748. unsigned long flags;
  749. /* catch various bogus parameters */
  750. if (!_req || !req->req.complete || !req->req.buf
  751. || !list_empty(&req->queue)) {
  752. VDBG("%s, bad params", __func__);
  753. return -EINVAL;
  754. }
  755. if (unlikely(!_ep || !ep->ep.desc)) {
  756. VDBG("%s, bad ep", __func__);
  757. return -EINVAL;
  758. }
  759. if (usb_endpoint_xfer_isoc(ep->ep.desc)) {
  760. if (req->req.length > ep->ep.maxpacket)
  761. return -EMSGSIZE;
  762. }
  763. udc = ep->udc;
  764. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
  765. return -ESHUTDOWN;
  766. req->ep = ep;
  767. /* map virtual address to hardware */
  768. if (req->req.dma == DMA_ADDR_INVALID) {
  769. req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
  770. req->req.buf,
  771. req->req.length, ep_is_in(ep)
  772. ? DMA_TO_DEVICE
  773. : DMA_FROM_DEVICE);
  774. req->mapped = 1;
  775. } else {
  776. dma_sync_single_for_device(ep->udc->gadget.dev.parent,
  777. req->req.dma, req->req.length,
  778. ep_is_in(ep)
  779. ? DMA_TO_DEVICE
  780. : DMA_FROM_DEVICE);
  781. req->mapped = 0;
  782. }
  783. req->req.status = -EINPROGRESS;
  784. req->req.actual = 0;
  785. req->dtd_count = 0;
  786. /* build dtds and push them to device queue */
  787. if (!fsl_req_to_dtd(req, gfp_flags)) {
  788. spin_lock_irqsave(&udc->lock, flags);
  789. fsl_queue_td(ep, req);
  790. } else {
  791. return -ENOMEM;
  792. }
  793. /* irq handler advances the queue */
  794. if (req != NULL)
  795. list_add_tail(&req->queue, &ep->queue);
  796. spin_unlock_irqrestore(&udc->lock, flags);
  797. return 0;
  798. }
  799. /* dequeues (cancels, unlinks) an I/O request from an endpoint */
  800. static int fsl_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  801. {
  802. struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
  803. struct fsl_req *req;
  804. unsigned long flags;
  805. int ep_num, stopped, ret = 0;
  806. u32 epctrl;
  807. if (!_ep || !_req)
  808. return -EINVAL;
  809. spin_lock_irqsave(&ep->udc->lock, flags);
  810. stopped = ep->stopped;
  811. /* Stop the ep before we deal with the queue */
  812. ep->stopped = 1;
  813. ep_num = ep_index(ep);
  814. epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  815. if (ep_is_in(ep))
  816. epctrl &= ~EPCTRL_TX_ENABLE;
  817. else
  818. epctrl &= ~EPCTRL_RX_ENABLE;
  819. fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
  820. /* make sure it's actually queued on this endpoint */
  821. list_for_each_entry(req, &ep->queue, queue) {
  822. if (&req->req == _req)
  823. break;
  824. }
  825. if (&req->req != _req) {
  826. ret = -EINVAL;
  827. goto out;
  828. }
  829. /* The request is in progress, or completed but not dequeued */
  830. if (ep->queue.next == &req->queue) {
  831. _req->status = -ECONNRESET;
  832. fsl_ep_fifo_flush(_ep); /* flush current transfer */
  833. /* The request isn't the last request in this ep queue */
  834. if (req->queue.next != &ep->queue) {
  835. struct fsl_req *next_req;
  836. next_req = list_entry(req->queue.next, struct fsl_req,
  837. queue);
  838. /* prime with dTD of next request */
  839. fsl_prime_ep(ep, next_req->head);
  840. }
  841. /* The request hasn't been processed, patch up the TD chain */
  842. } else {
  843. struct fsl_req *prev_req;
  844. prev_req = list_entry(req->queue.prev, struct fsl_req, queue);
  845. prev_req->tail->next_td_ptr = req->tail->next_td_ptr;
  846. }
  847. done(ep, req, -ECONNRESET);
  848. /* Enable EP */
  849. out: epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
  850. if (ep_is_in(ep))
  851. epctrl |= EPCTRL_TX_ENABLE;
  852. else
  853. epctrl |= EPCTRL_RX_ENABLE;
  854. fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
  855. ep->stopped = stopped;
  856. spin_unlock_irqrestore(&ep->udc->lock, flags);
  857. return ret;
  858. }
  859. /*-------------------------------------------------------------------------*/
  860. /*-----------------------------------------------------------------
  861. * modify the endpoint halt feature
  862. * @ep: the non-isochronous endpoint being stalled
  863. * @value: 1--set halt 0--clear halt
  864. * Returns zero, or a negative error code.
  865. *----------------------------------------------------------------*/
  866. static int fsl_ep_set_halt(struct usb_ep *_ep, int value)
  867. {
  868. struct fsl_ep *ep = NULL;
  869. unsigned long flags = 0;
  870. int status = -EOPNOTSUPP; /* operation not supported */
  871. unsigned char ep_dir = 0, ep_num = 0;
  872. struct fsl_udc *udc = NULL;
  873. ep = container_of(_ep, struct fsl_ep, ep);
  874. udc = ep->udc;
  875. if (!_ep || !ep->ep.desc) {
  876. status = -EINVAL;
  877. goto out;
  878. }
  879. if (usb_endpoint_xfer_isoc(ep->ep.desc)) {
  880. status = -EOPNOTSUPP;
  881. goto out;
  882. }
  883. /* Attempt to halt IN ep will fail if any transfer requests
  884. * are still queue */
  885. if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
  886. status = -EAGAIN;
  887. goto out;
  888. }
  889. status = 0;
  890. ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
  891. ep_num = (unsigned char)(ep_index(ep));
  892. spin_lock_irqsave(&ep->udc->lock, flags);
  893. dr_ep_change_stall(ep_num, ep_dir, value);
  894. spin_unlock_irqrestore(&ep->udc->lock, flags);
  895. if (ep_index(ep) == 0) {
  896. udc->ep0_state = WAIT_FOR_SETUP;
  897. udc->ep0_dir = 0;
  898. }
  899. out:
  900. VDBG(" %s %s halt stat %d", ep->ep.name,
  901. value ? "set" : "clear", status);
  902. return status;
  903. }
  904. static int fsl_ep_fifo_status(struct usb_ep *_ep)
  905. {
  906. struct fsl_ep *ep;
  907. struct fsl_udc *udc;
  908. int size = 0;
  909. u32 bitmask;
  910. struct ep_queue_head *qh;
  911. ep = container_of(_ep, struct fsl_ep, ep);
  912. if (!_ep || (!ep->ep.desc && ep_index(ep) != 0))
  913. return -ENODEV;
  914. udc = (struct fsl_udc *)ep->udc;
  915. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
  916. return -ESHUTDOWN;
  917. qh = get_qh_by_ep(ep);
  918. bitmask = (ep_is_in(ep)) ? (1 << (ep_index(ep) + 16)) :
  919. (1 << (ep_index(ep)));
  920. if (fsl_readl(&dr_regs->endptstatus) & bitmask)
  921. size = (qh->size_ioc_int_sts & DTD_PACKET_SIZE)
  922. >> DTD_LENGTH_BIT_POS;
  923. pr_debug("%s %u\n", __func__, size);
  924. return size;
  925. }
  926. static void fsl_ep_fifo_flush(struct usb_ep *_ep)
  927. {
  928. struct fsl_ep *ep;
  929. int ep_num, ep_dir;
  930. u32 bits;
  931. unsigned long timeout;
  932. #define FSL_UDC_FLUSH_TIMEOUT 1000
  933. if (!_ep) {
  934. return;
  935. } else {
  936. ep = container_of(_ep, struct fsl_ep, ep);
  937. if (!ep->ep.desc)
  938. return;
  939. }
  940. ep_num = ep_index(ep);
  941. ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
  942. if (ep_num == 0)
  943. bits = (1 << 16) | 1;
  944. else if (ep_dir == USB_SEND)
  945. bits = 1 << (16 + ep_num);
  946. else
  947. bits = 1 << ep_num;
  948. timeout = jiffies + FSL_UDC_FLUSH_TIMEOUT;
  949. do {
  950. fsl_writel(bits, &dr_regs->endptflush);
  951. /* Wait until flush complete */
  952. while (fsl_readl(&dr_regs->endptflush)) {
  953. if (time_after(jiffies, timeout)) {
  954. ERR("ep flush timeout\n");
  955. return;
  956. }
  957. cpu_relax();
  958. }
  959. /* See if we need to flush again */
  960. } while (fsl_readl(&dr_regs->endptstatus) & bits);
  961. }
  962. static struct usb_ep_ops fsl_ep_ops = {
  963. .enable = fsl_ep_enable,
  964. .disable = fsl_ep_disable,
  965. .alloc_request = fsl_alloc_request,
  966. .free_request = fsl_free_request,
  967. .queue = fsl_ep_queue,
  968. .dequeue = fsl_ep_dequeue,
  969. .set_halt = fsl_ep_set_halt,
  970. .fifo_status = fsl_ep_fifo_status,
  971. .fifo_flush = fsl_ep_fifo_flush, /* flush fifo */
  972. };
  973. /*-------------------------------------------------------------------------
  974. Gadget Driver Layer Operations
  975. -------------------------------------------------------------------------*/
  976. /*----------------------------------------------------------------------
  977. * Get the current frame number (from DR frame_index Reg )
  978. *----------------------------------------------------------------------*/
  979. static int fsl_get_frame(struct usb_gadget *gadget)
  980. {
  981. return (int)(fsl_readl(&dr_regs->frindex) & USB_FRINDEX_MASKS);
  982. }
  983. /*-----------------------------------------------------------------------
  984. * Tries to wake up the host connected to this gadget
  985. -----------------------------------------------------------------------*/
  986. static int fsl_wakeup(struct usb_gadget *gadget)
  987. {
  988. struct fsl_udc *udc = container_of(gadget, struct fsl_udc, gadget);
  989. u32 portsc;
  990. /* Remote wakeup feature not enabled by host */
  991. if (!udc->remote_wakeup)
  992. return -ENOTSUPP;
  993. portsc = fsl_readl(&dr_regs->portsc1);
  994. /* not suspended? */
  995. if (!(portsc & PORTSCX_PORT_SUSPEND))
  996. return 0;
  997. /* trigger force resume */
  998. portsc |= PORTSCX_PORT_FORCE_RESUME;
  999. fsl_writel(portsc, &dr_regs->portsc1);
  1000. return 0;
  1001. }
  1002. static int can_pullup(struct fsl_udc *udc)
  1003. {
  1004. return udc->driver && udc->softconnect && udc->vbus_active;
  1005. }
  1006. /* Notify controller that VBUS is powered, Called by whatever
  1007. detects VBUS sessions */
  1008. static int fsl_vbus_session(struct usb_gadget *gadget, int is_active)
  1009. {
  1010. struct fsl_udc *udc;
  1011. unsigned long flags;
  1012. udc = container_of(gadget, struct fsl_udc, gadget);
  1013. spin_lock_irqsave(&udc->lock, flags);
  1014. VDBG("VBUS %s", is_active ? "on" : "off");
  1015. udc->vbus_active = (is_active != 0);
  1016. if (can_pullup(udc))
  1017. fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
  1018. &dr_regs->usbcmd);
  1019. else
  1020. fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
  1021. &dr_regs->usbcmd);
  1022. spin_unlock_irqrestore(&udc->lock, flags);
  1023. return 0;
  1024. }
  1025. /* constrain controller's VBUS power usage
  1026. * This call is used by gadget drivers during SET_CONFIGURATION calls,
  1027. * reporting how much power the device may consume. For example, this
  1028. * could affect how quickly batteries are recharged.
  1029. *
  1030. * Returns zero on success, else negative errno.
  1031. */
  1032. static int fsl_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  1033. {
  1034. struct fsl_udc *udc;
  1035. udc = container_of(gadget, struct fsl_udc, gadget);
  1036. if (!IS_ERR_OR_NULL(udc->transceiver))
  1037. return usb_phy_set_power(udc->transceiver, mA);
  1038. return -ENOTSUPP;
  1039. }
  1040. /* Change Data+ pullup status
  1041. * this func is used by usb_gadget_connect/disconnet
  1042. */
  1043. static int fsl_pullup(struct usb_gadget *gadget, int is_on)
  1044. {
  1045. struct fsl_udc *udc;
  1046. udc = container_of(gadget, struct fsl_udc, gadget);
  1047. udc->softconnect = (is_on != 0);
  1048. if (can_pullup(udc))
  1049. fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
  1050. &dr_regs->usbcmd);
  1051. else
  1052. fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
  1053. &dr_regs->usbcmd);
  1054. return 0;
  1055. }
  1056. static int fsl_udc_start(struct usb_gadget *g,
  1057. struct usb_gadget_driver *driver);
  1058. static int fsl_udc_stop(struct usb_gadget *g,
  1059. struct usb_gadget_driver *driver);
  1060. /* defined in gadget.h */
  1061. static const struct usb_gadget_ops fsl_gadget_ops = {
  1062. .get_frame = fsl_get_frame,
  1063. .wakeup = fsl_wakeup,
  1064. /* .set_selfpowered = fsl_set_selfpowered, */ /* Always selfpowered */
  1065. .vbus_session = fsl_vbus_session,
  1066. .vbus_draw = fsl_vbus_draw,
  1067. .pullup = fsl_pullup,
  1068. .udc_start = fsl_udc_start,
  1069. .udc_stop = fsl_udc_stop,
  1070. };
  1071. /* Set protocol stall on ep0, protocol stall will automatically be cleared
  1072. on new transaction */
  1073. static void ep0stall(struct fsl_udc *udc)
  1074. {
  1075. u32 tmp;
  1076. /* must set tx and rx to stall at the same time */
  1077. tmp = fsl_readl(&dr_regs->endptctrl[0]);
  1078. tmp |= EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL;
  1079. fsl_writel(tmp, &dr_regs->endptctrl[0]);
  1080. udc->ep0_state = WAIT_FOR_SETUP;
  1081. udc->ep0_dir = 0;
  1082. }
  1083. /* Prime a status phase for ep0 */
  1084. static int ep0_prime_status(struct fsl_udc *udc, int direction)
  1085. {
  1086. struct fsl_req *req = udc->status_req;
  1087. struct fsl_ep *ep;
  1088. if (direction == EP_DIR_IN)
  1089. udc->ep0_dir = USB_DIR_IN;
  1090. else
  1091. udc->ep0_dir = USB_DIR_OUT;
  1092. ep = &udc->eps[0];
  1093. if (udc->ep0_state != DATA_STATE_XMIT)
  1094. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1095. req->ep = ep;
  1096. req->req.length = 0;
  1097. req->req.status = -EINPROGRESS;
  1098. req->req.actual = 0;
  1099. req->req.complete = NULL;
  1100. req->dtd_count = 0;
  1101. req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
  1102. req->req.buf, req->req.length,
  1103. ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1104. req->mapped = 1;
  1105. if (fsl_req_to_dtd(req, GFP_ATOMIC) == 0)
  1106. fsl_queue_td(ep, req);
  1107. else
  1108. return -ENOMEM;
  1109. list_add_tail(&req->queue, &ep->queue);
  1110. return 0;
  1111. }
  1112. static void udc_reset_ep_queue(struct fsl_udc *udc, u8 pipe)
  1113. {
  1114. struct fsl_ep *ep = get_ep_by_pipe(udc, pipe);
  1115. if (ep->name)
  1116. nuke(ep, -ESHUTDOWN);
  1117. }
  1118. /*
  1119. * ch9 Set address
  1120. */
  1121. static void ch9setaddress(struct fsl_udc *udc, u16 value, u16 index, u16 length)
  1122. {
  1123. /* Save the new address to device struct */
  1124. udc->device_address = (u8) value;
  1125. /* Update usb state */
  1126. udc->usb_state = USB_STATE_ADDRESS;
  1127. /* Status phase */
  1128. if (ep0_prime_status(udc, EP_DIR_IN))
  1129. ep0stall(udc);
  1130. }
  1131. /*
  1132. * ch9 Get status
  1133. */
  1134. static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
  1135. u16 index, u16 length)
  1136. {
  1137. u16 tmp = 0; /* Status, cpu endian */
  1138. struct fsl_req *req;
  1139. struct fsl_ep *ep;
  1140. ep = &udc->eps[0];
  1141. if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  1142. /* Get device status */
  1143. tmp = 1 << USB_DEVICE_SELF_POWERED;
  1144. tmp |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
  1145. } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
  1146. /* Get interface status */
  1147. /* We don't have interface information in udc driver */
  1148. tmp = 0;
  1149. } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
  1150. /* Get endpoint status */
  1151. struct fsl_ep *target_ep;
  1152. target_ep = get_ep_by_pipe(udc, get_pipe_by_windex(index));
  1153. /* stall if endpoint doesn't exist */
  1154. if (!target_ep->ep.desc)
  1155. goto stall;
  1156. tmp = dr_ep_get_stall(ep_index(target_ep), ep_is_in(target_ep))
  1157. << USB_ENDPOINT_HALT;
  1158. }
  1159. udc->ep0_dir = USB_DIR_IN;
  1160. /* Borrow the per device status_req */
  1161. req = udc->status_req;
  1162. /* Fill in the reqest structure */
  1163. *((u16 *) req->req.buf) = cpu_to_le16(tmp);
  1164. req->ep = ep;
  1165. req->req.length = 2;
  1166. req->req.status = -EINPROGRESS;
  1167. req->req.actual = 0;
  1168. req->req.complete = NULL;
  1169. req->dtd_count = 0;
  1170. req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
  1171. req->req.buf, req->req.length,
  1172. ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1173. req->mapped = 1;
  1174. /* prime the data phase */
  1175. if ((fsl_req_to_dtd(req, GFP_ATOMIC) == 0))
  1176. fsl_queue_td(ep, req);
  1177. else /* no mem */
  1178. goto stall;
  1179. list_add_tail(&req->queue, &ep->queue);
  1180. udc->ep0_state = DATA_STATE_XMIT;
  1181. if (ep0_prime_status(udc, EP_DIR_OUT))
  1182. ep0stall(udc);
  1183. return;
  1184. stall:
  1185. ep0stall(udc);
  1186. }
  1187. static void setup_received_irq(struct fsl_udc *udc,
  1188. struct usb_ctrlrequest *setup)
  1189. {
  1190. u16 wValue = le16_to_cpu(setup->wValue);
  1191. u16 wIndex = le16_to_cpu(setup->wIndex);
  1192. u16 wLength = le16_to_cpu(setup->wLength);
  1193. udc_reset_ep_queue(udc, 0);
  1194. /* We process some stardard setup requests here */
  1195. switch (setup->bRequest) {
  1196. case USB_REQ_GET_STATUS:
  1197. /* Data+Status phase from udc */
  1198. if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
  1199. != (USB_DIR_IN | USB_TYPE_STANDARD))
  1200. break;
  1201. ch9getstatus(udc, setup->bRequestType, wValue, wIndex, wLength);
  1202. return;
  1203. case USB_REQ_SET_ADDRESS:
  1204. /* Status phase from udc */
  1205. if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
  1206. | USB_RECIP_DEVICE))
  1207. break;
  1208. ch9setaddress(udc, wValue, wIndex, wLength);
  1209. return;
  1210. case USB_REQ_CLEAR_FEATURE:
  1211. case USB_REQ_SET_FEATURE:
  1212. /* Status phase from udc */
  1213. {
  1214. int rc = -EOPNOTSUPP;
  1215. u16 ptc = 0;
  1216. if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
  1217. == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
  1218. int pipe = get_pipe_by_windex(wIndex);
  1219. struct fsl_ep *ep;
  1220. if (wValue != 0 || wLength != 0 || pipe >= udc->max_ep)
  1221. break;
  1222. ep = get_ep_by_pipe(udc, pipe);
  1223. spin_unlock(&udc->lock);
  1224. rc = fsl_ep_set_halt(&ep->ep,
  1225. (setup->bRequest == USB_REQ_SET_FEATURE)
  1226. ? 1 : 0);
  1227. spin_lock(&udc->lock);
  1228. } else if ((setup->bRequestType & (USB_RECIP_MASK
  1229. | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
  1230. | USB_TYPE_STANDARD)) {
  1231. /* Note: The driver has not include OTG support yet.
  1232. * This will be set when OTG support is added */
  1233. if (wValue == USB_DEVICE_TEST_MODE)
  1234. ptc = wIndex >> 8;
  1235. else if (gadget_is_otg(&udc->gadget)) {
  1236. if (setup->bRequest ==
  1237. USB_DEVICE_B_HNP_ENABLE)
  1238. udc->gadget.b_hnp_enable = 1;
  1239. else if (setup->bRequest ==
  1240. USB_DEVICE_A_HNP_SUPPORT)
  1241. udc->gadget.a_hnp_support = 1;
  1242. else if (setup->bRequest ==
  1243. USB_DEVICE_A_ALT_HNP_SUPPORT)
  1244. udc->gadget.a_alt_hnp_support = 1;
  1245. }
  1246. rc = 0;
  1247. } else
  1248. break;
  1249. if (rc == 0) {
  1250. if (ep0_prime_status(udc, EP_DIR_IN))
  1251. ep0stall(udc);
  1252. }
  1253. if (ptc) {
  1254. u32 tmp;
  1255. mdelay(10);
  1256. tmp = fsl_readl(&dr_regs->portsc1) | (ptc << 16);
  1257. fsl_writel(tmp, &dr_regs->portsc1);
  1258. printk(KERN_INFO "udc: switch to test mode %d.\n", ptc);
  1259. }
  1260. return;
  1261. }
  1262. default:
  1263. break;
  1264. }
  1265. /* Requests handled by gadget */
  1266. if (wLength) {
  1267. /* Data phase from gadget, status phase from udc */
  1268. udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
  1269. ? USB_DIR_IN : USB_DIR_OUT;
  1270. spin_unlock(&udc->lock);
  1271. if (udc->driver->setup(&udc->gadget,
  1272. &udc->local_setup_buff) < 0)
  1273. ep0stall(udc);
  1274. spin_lock(&udc->lock);
  1275. udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
  1276. ? DATA_STATE_XMIT : DATA_STATE_RECV;
  1277. /*
  1278. * If the data stage is IN, send status prime immediately.
  1279. * See 2.0 Spec chapter 8.5.3.3 for detail.
  1280. */
  1281. if (udc->ep0_state == DATA_STATE_XMIT)
  1282. if (ep0_prime_status(udc, EP_DIR_OUT))
  1283. ep0stall(udc);
  1284. } else {
  1285. /* No data phase, IN status from gadget */
  1286. udc->ep0_dir = USB_DIR_IN;
  1287. spin_unlock(&udc->lock);
  1288. if (udc->driver->setup(&udc->gadget,
  1289. &udc->local_setup_buff) < 0)
  1290. ep0stall(udc);
  1291. spin_lock(&udc->lock);
  1292. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1293. }
  1294. }
  1295. /* Process request for Data or Status phase of ep0
  1296. * prime status phase if needed */
  1297. static void ep0_req_complete(struct fsl_udc *udc, struct fsl_ep *ep0,
  1298. struct fsl_req *req)
  1299. {
  1300. if (udc->usb_state == USB_STATE_ADDRESS) {
  1301. /* Set the new address */
  1302. u32 new_address = (u32) udc->device_address;
  1303. fsl_writel(new_address << USB_DEVICE_ADDRESS_BIT_POS,
  1304. &dr_regs->deviceaddr);
  1305. }
  1306. done(ep0, req, 0);
  1307. switch (udc->ep0_state) {
  1308. case DATA_STATE_XMIT:
  1309. /* already primed at setup_received_irq */
  1310. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1311. break;
  1312. case DATA_STATE_RECV:
  1313. /* send status phase */
  1314. if (ep0_prime_status(udc, EP_DIR_IN))
  1315. ep0stall(udc);
  1316. break;
  1317. case WAIT_FOR_OUT_STATUS:
  1318. udc->ep0_state = WAIT_FOR_SETUP;
  1319. break;
  1320. case WAIT_FOR_SETUP:
  1321. ERR("Unexpect ep0 packets\n");
  1322. break;
  1323. default:
  1324. ep0stall(udc);
  1325. break;
  1326. }
  1327. }
  1328. /* Tripwire mechanism to ensure a setup packet payload is extracted without
  1329. * being corrupted by another incoming setup packet */
  1330. static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
  1331. {
  1332. u32 temp;
  1333. struct ep_queue_head *qh;
  1334. struct fsl_usb2_platform_data *pdata = udc->pdata;
  1335. qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
  1336. /* Clear bit in ENDPTSETUPSTAT */
  1337. temp = fsl_readl(&dr_regs->endptsetupstat);
  1338. fsl_writel(temp | (1 << ep_num), &dr_regs->endptsetupstat);
  1339. /* while a hazard exists when setup package arrives */
  1340. do {
  1341. /* Set Setup Tripwire */
  1342. temp = fsl_readl(&dr_regs->usbcmd);
  1343. fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
  1344. /* Copy the setup packet to local buffer */
  1345. if (pdata->le_setup_buf) {
  1346. u32 *p = (u32 *)buffer_ptr;
  1347. u32 *s = (u32 *)qh->setup_buffer;
  1348. /* Convert little endian setup buffer to CPU endian */
  1349. *p++ = le32_to_cpu(*s++);
  1350. *p = le32_to_cpu(*s);
  1351. } else {
  1352. memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
  1353. }
  1354. } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
  1355. /* Clear Setup Tripwire */
  1356. temp = fsl_readl(&dr_regs->usbcmd);
  1357. fsl_writel(temp & ~USB_CMD_SUTW, &dr_regs->usbcmd);
  1358. }
  1359. /* process-ep_req(): free the completed Tds for this req */
  1360. static int process_ep_req(struct fsl_udc *udc, int pipe,
  1361. struct fsl_req *curr_req)
  1362. {
  1363. struct ep_td_struct *curr_td;
  1364. int td_complete, actual, remaining_length, j, tmp;
  1365. int status = 0;
  1366. int errors = 0;
  1367. struct ep_queue_head *curr_qh = &udc->ep_qh[pipe];
  1368. int direction = pipe % 2;
  1369. curr_td = curr_req->head;
  1370. td_complete = 0;
  1371. actual = curr_req->req.length;
  1372. for (j = 0; j < curr_req->dtd_count; j++) {
  1373. remaining_length = (hc32_to_cpu(curr_td->size_ioc_sts)
  1374. & DTD_PACKET_SIZE)
  1375. >> DTD_LENGTH_BIT_POS;
  1376. actual -= remaining_length;
  1377. errors = hc32_to_cpu(curr_td->size_ioc_sts);
  1378. if (errors & DTD_ERROR_MASK) {
  1379. if (errors & DTD_STATUS_HALTED) {
  1380. ERR("dTD error %08x QH=%d\n", errors, pipe);
  1381. /* Clear the errors and Halt condition */
  1382. tmp = hc32_to_cpu(curr_qh->size_ioc_int_sts);
  1383. tmp &= ~errors;
  1384. curr_qh->size_ioc_int_sts = cpu_to_hc32(tmp);
  1385. status = -EPIPE;
  1386. /* FIXME: continue with next queued TD? */
  1387. break;
  1388. }
  1389. if (errors & DTD_STATUS_DATA_BUFF_ERR) {
  1390. VDBG("Transfer overflow");
  1391. status = -EPROTO;
  1392. break;
  1393. } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
  1394. VDBG("ISO error");
  1395. status = -EILSEQ;
  1396. break;
  1397. } else
  1398. ERR("Unknown error has occurred (0x%x)!\n",
  1399. errors);
  1400. } else if (hc32_to_cpu(curr_td->size_ioc_sts)
  1401. & DTD_STATUS_ACTIVE) {
  1402. VDBG("Request not complete");
  1403. status = REQ_UNCOMPLETE;
  1404. return status;
  1405. } else if (remaining_length) {
  1406. if (direction) {
  1407. VDBG("Transmit dTD remaining length not zero");
  1408. status = -EPROTO;
  1409. break;
  1410. } else {
  1411. td_complete++;
  1412. break;
  1413. }
  1414. } else {
  1415. td_complete++;
  1416. VDBG("dTD transmitted successful");
  1417. }
  1418. if (j != curr_req->dtd_count - 1)
  1419. curr_td = (struct ep_td_struct *)curr_td->next_td_virt;
  1420. }
  1421. if (status)
  1422. return status;
  1423. curr_req->req.actual = actual;
  1424. return 0;
  1425. }
  1426. /* Process a DTD completion interrupt */
  1427. static void dtd_complete_irq(struct fsl_udc *udc)
  1428. {
  1429. u32 bit_pos;
  1430. int i, ep_num, direction, bit_mask, status;
  1431. struct fsl_ep *curr_ep;
  1432. struct fsl_req *curr_req, *temp_req;
  1433. /* Clear the bits in the register */
  1434. bit_pos = fsl_readl(&dr_regs->endptcomplete);
  1435. fsl_writel(bit_pos, &dr_regs->endptcomplete);
  1436. if (!bit_pos)
  1437. return;
  1438. for (i = 0; i < udc->max_ep; i++) {
  1439. ep_num = i >> 1;
  1440. direction = i % 2;
  1441. bit_mask = 1 << (ep_num + 16 * direction);
  1442. if (!(bit_pos & bit_mask))
  1443. continue;
  1444. curr_ep = get_ep_by_pipe(udc, i);
  1445. /* If the ep is configured */
  1446. if (curr_ep->name == NULL) {
  1447. WARNING("Invalid EP?");
  1448. continue;
  1449. }
  1450. /* process the req queue until an uncomplete request */
  1451. list_for_each_entry_safe(curr_req, temp_req, &curr_ep->queue,
  1452. queue) {
  1453. status = process_ep_req(udc, i, curr_req);
  1454. VDBG("status of process_ep_req= %d, ep = %d",
  1455. status, ep_num);
  1456. if (status == REQ_UNCOMPLETE)
  1457. break;
  1458. /* write back status to req */
  1459. curr_req->req.status = status;
  1460. if (ep_num == 0) {
  1461. ep0_req_complete(udc, curr_ep, curr_req);
  1462. break;
  1463. } else
  1464. done(curr_ep, curr_req, status);
  1465. }
  1466. }
  1467. }
  1468. static inline enum usb_device_speed portscx_device_speed(u32 reg)
  1469. {
  1470. switch (reg & PORTSCX_PORT_SPEED_MASK) {
  1471. case PORTSCX_PORT_SPEED_HIGH:
  1472. return USB_SPEED_HIGH;
  1473. case PORTSCX_PORT_SPEED_FULL:
  1474. return USB_SPEED_FULL;
  1475. case PORTSCX_PORT_SPEED_LOW:
  1476. return USB_SPEED_LOW;
  1477. default:
  1478. return USB_SPEED_UNKNOWN;
  1479. }
  1480. }
  1481. /* Process a port change interrupt */
  1482. static void port_change_irq(struct fsl_udc *udc)
  1483. {
  1484. if (udc->bus_reset)
  1485. udc->bus_reset = 0;
  1486. /* Bus resetting is finished */
  1487. if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET))
  1488. /* Get the speed */
  1489. udc->gadget.speed =
  1490. portscx_device_speed(fsl_readl(&dr_regs->portsc1));
  1491. /* Update USB state */
  1492. if (!udc->resume_state)
  1493. udc->usb_state = USB_STATE_DEFAULT;
  1494. }
  1495. /* Process suspend interrupt */
  1496. static void suspend_irq(struct fsl_udc *udc)
  1497. {
  1498. udc->resume_state = udc->usb_state;
  1499. udc->usb_state = USB_STATE_SUSPENDED;
  1500. /* report suspend to the driver, serial.c does not support this */
  1501. if (udc->driver->suspend)
  1502. udc->driver->suspend(&udc->gadget);
  1503. }
  1504. static void bus_resume(struct fsl_udc *udc)
  1505. {
  1506. udc->usb_state = udc->resume_state;
  1507. udc->resume_state = 0;
  1508. /* report resume to the driver, serial.c does not support this */
  1509. if (udc->driver->resume)
  1510. udc->driver->resume(&udc->gadget);
  1511. }
  1512. /* Clear up all ep queues */
  1513. static int reset_queues(struct fsl_udc *udc)
  1514. {
  1515. u8 pipe;
  1516. for (pipe = 0; pipe < udc->max_pipes; pipe++)
  1517. udc_reset_ep_queue(udc, pipe);
  1518. /* report disconnect; the driver is already quiesced */
  1519. spin_unlock(&udc->lock);
  1520. udc->driver->disconnect(&udc->gadget);
  1521. spin_lock(&udc->lock);
  1522. return 0;
  1523. }
  1524. /* Process reset interrupt */
  1525. static void reset_irq(struct fsl_udc *udc)
  1526. {
  1527. u32 temp;
  1528. unsigned long timeout;
  1529. /* Clear the device address */
  1530. temp = fsl_readl(&dr_regs->deviceaddr);
  1531. fsl_writel(temp & ~USB_DEVICE_ADDRESS_MASK, &dr_regs->deviceaddr);
  1532. udc->device_address = 0;
  1533. /* Clear usb state */
  1534. udc->resume_state = 0;
  1535. udc->ep0_dir = 0;
  1536. udc->ep0_state = WAIT_FOR_SETUP;
  1537. udc->remote_wakeup = 0; /* default to 0 on reset */
  1538. udc->gadget.b_hnp_enable = 0;
  1539. udc->gadget.a_hnp_support = 0;
  1540. udc->gadget.a_alt_hnp_support = 0;
  1541. /* Clear all the setup token semaphores */
  1542. temp = fsl_readl(&dr_regs->endptsetupstat);
  1543. fsl_writel(temp, &dr_regs->endptsetupstat);
  1544. /* Clear all the endpoint complete status bits */
  1545. temp = fsl_readl(&dr_regs->endptcomplete);
  1546. fsl_writel(temp, &dr_regs->endptcomplete);
  1547. timeout = jiffies + 100;
  1548. while (fsl_readl(&dr_regs->endpointprime)) {
  1549. /* Wait until all endptprime bits cleared */
  1550. if (time_after(jiffies, timeout)) {
  1551. ERR("Timeout for reset\n");
  1552. break;
  1553. }
  1554. cpu_relax();
  1555. }
  1556. /* Write 1s to the flush register */
  1557. fsl_writel(0xffffffff, &dr_regs->endptflush);
  1558. if (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET) {
  1559. VDBG("Bus reset");
  1560. /* Bus is reseting */
  1561. udc->bus_reset = 1;
  1562. /* Reset all the queues, include XD, dTD, EP queue
  1563. * head and TR Queue */
  1564. reset_queues(udc);
  1565. udc->usb_state = USB_STATE_DEFAULT;
  1566. } else {
  1567. VDBG("Controller reset");
  1568. /* initialize usb hw reg except for regs for EP, not
  1569. * touch usbintr reg */
  1570. dr_controller_setup(udc);
  1571. /* Reset all internal used Queues */
  1572. reset_queues(udc);
  1573. ep0_setup(udc);
  1574. /* Enable DR IRQ reg, Set Run bit, change udc state */
  1575. dr_controller_run(udc);
  1576. udc->usb_state = USB_STATE_ATTACHED;
  1577. }
  1578. }
  1579. /*
  1580. * USB device controller interrupt handler
  1581. */
  1582. static irqreturn_t fsl_udc_irq(int irq, void *_udc)
  1583. {
  1584. struct fsl_udc *udc = _udc;
  1585. u32 irq_src;
  1586. irqreturn_t status = IRQ_NONE;
  1587. unsigned long flags;
  1588. /* Disable ISR for OTG host mode */
  1589. if (udc->stopped)
  1590. return IRQ_NONE;
  1591. spin_lock_irqsave(&udc->lock, flags);
  1592. irq_src = fsl_readl(&dr_regs->usbsts) & fsl_readl(&dr_regs->usbintr);
  1593. /* Clear notification bits */
  1594. fsl_writel(irq_src, &dr_regs->usbsts);
  1595. /* VDBG("irq_src [0x%8x]", irq_src); */
  1596. /* Need to resume? */
  1597. if (udc->usb_state == USB_STATE_SUSPENDED)
  1598. if ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SUSPEND) == 0)
  1599. bus_resume(udc);
  1600. /* USB Interrupt */
  1601. if (irq_src & USB_STS_INT) {
  1602. VDBG("Packet int");
  1603. /* Setup package, we only support ep0 as control ep */
  1604. if (fsl_readl(&dr_regs->endptsetupstat) & EP_SETUP_STATUS_EP0) {
  1605. tripwire_handler(udc, 0,
  1606. (u8 *) (&udc->local_setup_buff));
  1607. setup_received_irq(udc, &udc->local_setup_buff);
  1608. status = IRQ_HANDLED;
  1609. }
  1610. /* completion of dtd */
  1611. if (fsl_readl(&dr_regs->endptcomplete)) {
  1612. dtd_complete_irq(udc);
  1613. status = IRQ_HANDLED;
  1614. }
  1615. }
  1616. /* SOF (for ISO transfer) */
  1617. if (irq_src & USB_STS_SOF) {
  1618. status = IRQ_HANDLED;
  1619. }
  1620. /* Port Change */
  1621. if (irq_src & USB_STS_PORT_CHANGE) {
  1622. port_change_irq(udc);
  1623. status = IRQ_HANDLED;
  1624. }
  1625. /* Reset Received */
  1626. if (irq_src & USB_STS_RESET) {
  1627. VDBG("reset int");
  1628. reset_irq(udc);
  1629. status = IRQ_HANDLED;
  1630. }
  1631. /* Sleep Enable (Suspend) */
  1632. if (irq_src & USB_STS_SUSPEND) {
  1633. suspend_irq(udc);
  1634. status = IRQ_HANDLED;
  1635. }
  1636. if (irq_src & (USB_STS_ERR | USB_STS_SYS_ERR)) {
  1637. VDBG("Error IRQ %x", irq_src);
  1638. }
  1639. spin_unlock_irqrestore(&udc->lock, flags);
  1640. return status;
  1641. }
  1642. /*----------------------------------------------------------------*
  1643. * Hook to gadget drivers
  1644. * Called by initialization code of gadget drivers
  1645. *----------------------------------------------------------------*/
  1646. static int fsl_udc_start(struct usb_gadget *g,
  1647. struct usb_gadget_driver *driver)
  1648. {
  1649. int retval = 0;
  1650. unsigned long flags = 0;
  1651. /* lock is needed but whether should use this lock or another */
  1652. spin_lock_irqsave(&udc_controller->lock, flags);
  1653. driver->driver.bus = NULL;
  1654. /* hook up the driver */
  1655. udc_controller->driver = driver;
  1656. udc_controller->gadget.dev.driver = &driver->driver;
  1657. spin_unlock_irqrestore(&udc_controller->lock, flags);
  1658. if (!IS_ERR_OR_NULL(udc_controller->transceiver)) {
  1659. /* Suspend the controller until OTG enable it */
  1660. udc_controller->stopped = 1;
  1661. printk(KERN_INFO "Suspend udc for OTG auto detect\n");
  1662. /* connect to bus through transceiver */
  1663. if (!IS_ERR_OR_NULL(udc_controller->transceiver)) {
  1664. retval = otg_set_peripheral(
  1665. udc_controller->transceiver->otg,
  1666. &udc_controller->gadget);
  1667. if (retval < 0) {
  1668. ERR("can't bind to transceiver\n");
  1669. driver->unbind(&udc_controller->gadget);
  1670. udc_controller->gadget.dev.driver = 0;
  1671. udc_controller->driver = 0;
  1672. return retval;
  1673. }
  1674. }
  1675. } else {
  1676. /* Enable DR IRQ reg and set USBCMD reg Run bit */
  1677. dr_controller_run(udc_controller);
  1678. udc_controller->usb_state = USB_STATE_ATTACHED;
  1679. udc_controller->ep0_state = WAIT_FOR_SETUP;
  1680. udc_controller->ep0_dir = 0;
  1681. }
  1682. return retval;
  1683. }
  1684. /* Disconnect from gadget driver */
  1685. static int fsl_udc_stop(struct usb_gadget *g,
  1686. struct usb_gadget_driver *driver)
  1687. {
  1688. struct fsl_ep *loop_ep;
  1689. unsigned long flags;
  1690. if (!IS_ERR_OR_NULL(udc_controller->transceiver))
  1691. otg_set_peripheral(udc_controller->transceiver->otg, NULL);
  1692. /* stop DR, disable intr */
  1693. dr_controller_stop(udc_controller);
  1694. /* in fact, no needed */
  1695. udc_controller->usb_state = USB_STATE_ATTACHED;
  1696. udc_controller->ep0_state = WAIT_FOR_SETUP;
  1697. udc_controller->ep0_dir = 0;
  1698. /* stand operation */
  1699. spin_lock_irqsave(&udc_controller->lock, flags);
  1700. udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
  1701. nuke(&udc_controller->eps[0], -ESHUTDOWN);
  1702. list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
  1703. ep.ep_list)
  1704. nuke(loop_ep, -ESHUTDOWN);
  1705. spin_unlock_irqrestore(&udc_controller->lock, flags);
  1706. udc_controller->gadget.dev.driver = NULL;
  1707. udc_controller->driver = NULL;
  1708. return 0;
  1709. }
  1710. /*-------------------------------------------------------------------------
  1711. PROC File System Support
  1712. -------------------------------------------------------------------------*/
  1713. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1714. #include <linux/seq_file.h>
  1715. static const char proc_filename[] = "driver/fsl_usb2_udc";
  1716. static int fsl_proc_read(char *page, char **start, off_t off, int count,
  1717. int *eof, void *_dev)
  1718. {
  1719. char *buf = page;
  1720. char *next = buf;
  1721. unsigned size = count;
  1722. unsigned long flags;
  1723. int t, i;
  1724. u32 tmp_reg;
  1725. struct fsl_ep *ep = NULL;
  1726. struct fsl_req *req;
  1727. struct fsl_udc *udc = udc_controller;
  1728. if (off != 0)
  1729. return 0;
  1730. spin_lock_irqsave(&udc->lock, flags);
  1731. /* ------basic driver information ---- */
  1732. t = scnprintf(next, size,
  1733. DRIVER_DESC "\n"
  1734. "%s version: %s\n"
  1735. "Gadget driver: %s\n\n",
  1736. driver_name, DRIVER_VERSION,
  1737. udc->driver ? udc->driver->driver.name : "(none)");
  1738. size -= t;
  1739. next += t;
  1740. /* ------ DR Registers ----- */
  1741. tmp_reg = fsl_readl(&dr_regs->usbcmd);
  1742. t = scnprintf(next, size,
  1743. "USBCMD reg:\n"
  1744. "SetupTW: %d\n"
  1745. "Run/Stop: %s\n\n",
  1746. (tmp_reg & USB_CMD_SUTW) ? 1 : 0,
  1747. (tmp_reg & USB_CMD_RUN_STOP) ? "Run" : "Stop");
  1748. size -= t;
  1749. next += t;
  1750. tmp_reg = fsl_readl(&dr_regs->usbsts);
  1751. t = scnprintf(next, size,
  1752. "USB Status Reg:\n"
  1753. "Dr Suspend: %d Reset Received: %d System Error: %s "
  1754. "USB Error Interrupt: %s\n\n",
  1755. (tmp_reg & USB_STS_SUSPEND) ? 1 : 0,
  1756. (tmp_reg & USB_STS_RESET) ? 1 : 0,
  1757. (tmp_reg & USB_STS_SYS_ERR) ? "Err" : "Normal",
  1758. (tmp_reg & USB_STS_ERR) ? "Err detected" : "No err");
  1759. size -= t;
  1760. next += t;
  1761. tmp_reg = fsl_readl(&dr_regs->usbintr);
  1762. t = scnprintf(next, size,
  1763. "USB Interrupt Enable Reg:\n"
  1764. "Sleep Enable: %d SOF Received Enable: %d "
  1765. "Reset Enable: %d\n"
  1766. "System Error Enable: %d "
  1767. "Port Change Dectected Enable: %d\n"
  1768. "USB Error Intr Enable: %d USB Intr Enable: %d\n\n",
  1769. (tmp_reg & USB_INTR_DEVICE_SUSPEND) ? 1 : 0,
  1770. (tmp_reg & USB_INTR_SOF_EN) ? 1 : 0,
  1771. (tmp_reg & USB_INTR_RESET_EN) ? 1 : 0,
  1772. (tmp_reg & USB_INTR_SYS_ERR_EN) ? 1 : 0,
  1773. (tmp_reg & USB_INTR_PTC_DETECT_EN) ? 1 : 0,
  1774. (tmp_reg & USB_INTR_ERR_INT_EN) ? 1 : 0,
  1775. (tmp_reg & USB_INTR_INT_EN) ? 1 : 0);
  1776. size -= t;
  1777. next += t;
  1778. tmp_reg = fsl_readl(&dr_regs->frindex);
  1779. t = scnprintf(next, size,
  1780. "USB Frame Index Reg: Frame Number is 0x%x\n\n",
  1781. (tmp_reg & USB_FRINDEX_MASKS));
  1782. size -= t;
  1783. next += t;
  1784. tmp_reg = fsl_readl(&dr_regs->deviceaddr);
  1785. t = scnprintf(next, size,
  1786. "USB Device Address Reg: Device Addr is 0x%x\n\n",
  1787. (tmp_reg & USB_DEVICE_ADDRESS_MASK));
  1788. size -= t;
  1789. next += t;
  1790. tmp_reg = fsl_readl(&dr_regs->endpointlistaddr);
  1791. t = scnprintf(next, size,
  1792. "USB Endpoint List Address Reg: "
  1793. "Device Addr is 0x%x\n\n",
  1794. (tmp_reg & USB_EP_LIST_ADDRESS_MASK));
  1795. size -= t;
  1796. next += t;
  1797. tmp_reg = fsl_readl(&dr_regs->portsc1);
  1798. t = scnprintf(next, size,
  1799. "USB Port Status&Control Reg:\n"
  1800. "Port Transceiver Type : %s Port Speed: %s\n"
  1801. "PHY Low Power Suspend: %s Port Reset: %s "
  1802. "Port Suspend Mode: %s\n"
  1803. "Over-current Change: %s "
  1804. "Port Enable/Disable Change: %s\n"
  1805. "Port Enabled/Disabled: %s "
  1806. "Current Connect Status: %s\n\n", ( {
  1807. char *s;
  1808. switch (tmp_reg & PORTSCX_PTS_FSLS) {
  1809. case PORTSCX_PTS_UTMI:
  1810. s = "UTMI"; break;
  1811. case PORTSCX_PTS_ULPI:
  1812. s = "ULPI "; break;
  1813. case PORTSCX_PTS_FSLS:
  1814. s = "FS/LS Serial"; break;
  1815. default:
  1816. s = "None"; break;
  1817. }
  1818. s;} ),
  1819. usb_speed_string(portscx_device_speed(tmp_reg)),
  1820. (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ?
  1821. "Normal PHY mode" : "Low power mode",
  1822. (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" :
  1823. "Not in Reset",
  1824. (tmp_reg & PORTSCX_PORT_SUSPEND) ? "In " : "Not in",
  1825. (tmp_reg & PORTSCX_OVER_CURRENT_CHG) ? "Dected" :
  1826. "No",
  1827. (tmp_reg & PORTSCX_PORT_EN_DIS_CHANGE) ? "Disable" :
  1828. "Not change",
  1829. (tmp_reg & PORTSCX_PORT_ENABLE) ? "Enable" :
  1830. "Not correct",
  1831. (tmp_reg & PORTSCX_CURRENT_CONNECT_STATUS) ?
  1832. "Attached" : "Not-Att");
  1833. size -= t;
  1834. next += t;
  1835. tmp_reg = fsl_readl(&dr_regs->usbmode);
  1836. t = scnprintf(next, size,
  1837. "USB Mode Reg: Controller Mode is: %s\n\n", ( {
  1838. char *s;
  1839. switch (tmp_reg & USB_MODE_CTRL_MODE_HOST) {
  1840. case USB_MODE_CTRL_MODE_IDLE:
  1841. s = "Idle"; break;
  1842. case USB_MODE_CTRL_MODE_DEVICE:
  1843. s = "Device Controller"; break;
  1844. case USB_MODE_CTRL_MODE_HOST:
  1845. s = "Host Controller"; break;
  1846. default:
  1847. s = "None"; break;
  1848. }
  1849. s;
  1850. } ));
  1851. size -= t;
  1852. next += t;
  1853. tmp_reg = fsl_readl(&dr_regs->endptsetupstat);
  1854. t = scnprintf(next, size,
  1855. "Endpoint Setup Status Reg: SETUP on ep 0x%x\n\n",
  1856. (tmp_reg & EP_SETUP_STATUS_MASK));
  1857. size -= t;
  1858. next += t;
  1859. for (i = 0; i < udc->max_ep / 2; i++) {
  1860. tmp_reg = fsl_readl(&dr_regs->endptctrl[i]);
  1861. t = scnprintf(next, size, "EP Ctrl Reg [0x%x]: = [0x%x]\n",
  1862. i, tmp_reg);
  1863. size -= t;
  1864. next += t;
  1865. }
  1866. tmp_reg = fsl_readl(&dr_regs->endpointprime);
  1867. t = scnprintf(next, size, "EP Prime Reg = [0x%x]\n\n", tmp_reg);
  1868. size -= t;
  1869. next += t;
  1870. #ifndef CONFIG_ARCH_MXC
  1871. if (udc->pdata->have_sysif_regs) {
  1872. tmp_reg = usb_sys_regs->snoop1;
  1873. t = scnprintf(next, size, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg);
  1874. size -= t;
  1875. next += t;
  1876. tmp_reg = usb_sys_regs->control;
  1877. t = scnprintf(next, size, "General Control Reg : = [0x%x]\n\n",
  1878. tmp_reg);
  1879. size -= t;
  1880. next += t;
  1881. }
  1882. #endif
  1883. /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
  1884. ep = &udc->eps[0];
  1885. t = scnprintf(next, size, "For %s Maxpkt is 0x%x index is 0x%x\n",
  1886. ep->ep.name, ep_maxpacket(ep), ep_index(ep));
  1887. size -= t;
  1888. next += t;
  1889. if (list_empty(&ep->queue)) {
  1890. t = scnprintf(next, size, "its req queue is empty\n\n");
  1891. size -= t;
  1892. next += t;
  1893. } else {
  1894. list_for_each_entry(req, &ep->queue, queue) {
  1895. t = scnprintf(next, size,
  1896. "req %p actual 0x%x length 0x%x buf %p\n",
  1897. &req->req, req->req.actual,
  1898. req->req.length, req->req.buf);
  1899. size -= t;
  1900. next += t;
  1901. }
  1902. }
  1903. /* other gadget->eplist ep */
  1904. list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
  1905. if (ep->ep.desc) {
  1906. t = scnprintf(next, size,
  1907. "\nFor %s Maxpkt is 0x%x "
  1908. "index is 0x%x\n",
  1909. ep->ep.name, ep_maxpacket(ep),
  1910. ep_index(ep));
  1911. size -= t;
  1912. next += t;
  1913. if (list_empty(&ep->queue)) {
  1914. t = scnprintf(next, size,
  1915. "its req queue is empty\n\n");
  1916. size -= t;
  1917. next += t;
  1918. } else {
  1919. list_for_each_entry(req, &ep->queue, queue) {
  1920. t = scnprintf(next, size,
  1921. "req %p actual 0x%x length "
  1922. "0x%x buf %p\n",
  1923. &req->req, req->req.actual,
  1924. req->req.length, req->req.buf);
  1925. size -= t;
  1926. next += t;
  1927. } /* end for each_entry of ep req */
  1928. } /* end for else */
  1929. } /* end for if(ep->queue) */
  1930. } /* end (ep->desc) */
  1931. spin_unlock_irqrestore(&udc->lock, flags);
  1932. *eof = 1;
  1933. return count - size;
  1934. }
  1935. #define create_proc_file() create_proc_read_entry(proc_filename, \
  1936. 0, NULL, fsl_proc_read, NULL)
  1937. #define remove_proc_file() remove_proc_entry(proc_filename, NULL)
  1938. #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
  1939. #define create_proc_file() do {} while (0)
  1940. #define remove_proc_file() do {} while (0)
  1941. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  1942. /*-------------------------------------------------------------------------*/
  1943. /* Release udc structures */
  1944. static void fsl_udc_release(struct device *dev)
  1945. {
  1946. complete(udc_controller->done);
  1947. dma_free_coherent(dev->parent, udc_controller->ep_qh_size,
  1948. udc_controller->ep_qh, udc_controller->ep_qh_dma);
  1949. kfree(udc_controller);
  1950. }
  1951. /******************************************************************
  1952. Internal structure setup functions
  1953. *******************************************************************/
  1954. /*------------------------------------------------------------------
  1955. * init resource for globle controller
  1956. * Return the udc handle on success or NULL on failure
  1957. ------------------------------------------------------------------*/
  1958. static int __init struct_udc_setup(struct fsl_udc *udc,
  1959. struct platform_device *pdev)
  1960. {
  1961. struct fsl_usb2_platform_data *pdata;
  1962. size_t size;
  1963. pdata = pdev->dev.platform_data;
  1964. udc->phy_mode = pdata->phy_mode;
  1965. udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL);
  1966. if (!udc->eps) {
  1967. ERR("malloc fsl_ep failed\n");
  1968. return -1;
  1969. }
  1970. /* initialized QHs, take care of alignment */
  1971. size = udc->max_ep * sizeof(struct ep_queue_head);
  1972. if (size < QH_ALIGNMENT)
  1973. size = QH_ALIGNMENT;
  1974. else if ((size % QH_ALIGNMENT) != 0) {
  1975. size += QH_ALIGNMENT + 1;
  1976. size &= ~(QH_ALIGNMENT - 1);
  1977. }
  1978. udc->ep_qh = dma_alloc_coherent(&pdev->dev, size,
  1979. &udc->ep_qh_dma, GFP_KERNEL);
  1980. if (!udc->ep_qh) {
  1981. ERR("malloc QHs for udc failed\n");
  1982. kfree(udc->eps);
  1983. return -1;
  1984. }
  1985. udc->ep_qh_size = size;
  1986. /* Initialize ep0 status request structure */
  1987. /* FIXME: fsl_alloc_request() ignores ep argument */
  1988. udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
  1989. struct fsl_req, req);
  1990. /* allocate a small amount of memory to get valid address */
  1991. udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
  1992. udc->resume_state = USB_STATE_NOTATTACHED;
  1993. udc->usb_state = USB_STATE_POWERED;
  1994. udc->ep0_dir = 0;
  1995. udc->remote_wakeup = 0; /* default to 0 on reset */
  1996. return 0;
  1997. }
  1998. /*----------------------------------------------------------------
  1999. * Setup the fsl_ep struct for eps
  2000. * Link fsl_ep->ep to gadget->ep_list
  2001. * ep0out is not used so do nothing here
  2002. * ep0in should be taken care
  2003. *--------------------------------------------------------------*/
  2004. static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
  2005. char *name, int link)
  2006. {
  2007. struct fsl_ep *ep = &udc->eps[index];
  2008. ep->udc = udc;
  2009. strcpy(ep->name, name);
  2010. ep->ep.name = ep->name;
  2011. ep->ep.ops = &fsl_ep_ops;
  2012. ep->stopped = 0;
  2013. /* for ep0: maxP defined in desc
  2014. * for other eps, maxP is set by epautoconfig() called by gadget layer
  2015. */
  2016. ep->ep.maxpacket = (unsigned short) ~0;
  2017. /* the queue lists any req for this ep */
  2018. INIT_LIST_HEAD(&ep->queue);
  2019. /* gagdet.ep_list used for ep_autoconfig so no ep0 */
  2020. if (link)
  2021. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  2022. ep->gadget = &udc->gadget;
  2023. ep->qh = &udc->ep_qh[index];
  2024. return 0;
  2025. }
  2026. /* Driver probe function
  2027. * all intialization operations implemented here except enabling usb_intr reg
  2028. * board setup should have been done in the platform code
  2029. */
  2030. static int __init fsl_udc_probe(struct platform_device *pdev)
  2031. {
  2032. struct fsl_usb2_platform_data *pdata;
  2033. struct resource *res;
  2034. int ret = -ENODEV;
  2035. unsigned int i;
  2036. u32 dccparams;
  2037. udc_controller = kzalloc(sizeof(struct fsl_udc), GFP_KERNEL);
  2038. if (udc_controller == NULL) {
  2039. ERR("malloc udc failed\n");
  2040. return -ENOMEM;
  2041. }
  2042. pdata = pdev->dev.platform_data;
  2043. udc_controller->pdata = pdata;
  2044. spin_lock_init(&udc_controller->lock);
  2045. udc_controller->stopped = 1;
  2046. #ifdef CONFIG_USB_OTG
  2047. if (pdata->operating_mode == FSL_USB2_DR_OTG) {
  2048. udc_controller->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
  2049. if (IS_ERR_OR_NULL(udc_controller->transceiver)) {
  2050. ERR("Can't find OTG driver!\n");
  2051. ret = -ENODEV;
  2052. goto err_kfree;
  2053. }
  2054. }
  2055. #endif
  2056. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2057. if (!res) {
  2058. ret = -ENXIO;
  2059. goto err_kfree;
  2060. }
  2061. if (pdata->operating_mode == FSL_USB2_DR_DEVICE) {
  2062. if (!request_mem_region(res->start, resource_size(res),
  2063. driver_name)) {
  2064. ERR("request mem region for %s failed\n", pdev->name);
  2065. ret = -EBUSY;
  2066. goto err_kfree;
  2067. }
  2068. }
  2069. dr_regs = ioremap(res->start, resource_size(res));
  2070. if (!dr_regs) {
  2071. ret = -ENOMEM;
  2072. goto err_release_mem_region;
  2073. }
  2074. pdata->regs = (void *)dr_regs;
  2075. /*
  2076. * do platform specific init: check the clock, grab/config pins, etc.
  2077. */
  2078. if (pdata->init && pdata->init(pdev)) {
  2079. ret = -ENODEV;
  2080. goto err_iounmap_noclk;
  2081. }
  2082. /* Set accessors only after pdata->init() ! */
  2083. fsl_set_accessors(pdata);
  2084. #ifndef CONFIG_ARCH_MXC
  2085. if (pdata->have_sysif_regs)
  2086. usb_sys_regs = (void *)dr_regs + USB_DR_SYS_OFFSET;
  2087. #endif
  2088. /* Initialize USB clocks */
  2089. ret = fsl_udc_clk_init(pdev);
  2090. if (ret < 0)
  2091. goto err_iounmap_noclk;
  2092. /* Read Device Controller Capability Parameters register */
  2093. dccparams = fsl_readl(&dr_regs->dccparams);
  2094. if (!(dccparams & DCCPARAMS_DC)) {
  2095. ERR("This SOC doesn't support device role\n");
  2096. ret = -ENODEV;
  2097. goto err_iounmap;
  2098. }
  2099. /* Get max device endpoints */
  2100. /* DEN is bidirectional ep number, max_ep doubles the number */
  2101. udc_controller->max_ep = (dccparams & DCCPARAMS_DEN_MASK) * 2;
  2102. udc_controller->irq = platform_get_irq(pdev, 0);
  2103. if (!udc_controller->irq) {
  2104. ret = -ENODEV;
  2105. goto err_iounmap;
  2106. }
  2107. ret = request_irq(udc_controller->irq, fsl_udc_irq, IRQF_SHARED,
  2108. driver_name, udc_controller);
  2109. if (ret != 0) {
  2110. ERR("cannot request irq %d err %d\n",
  2111. udc_controller->irq, ret);
  2112. goto err_iounmap;
  2113. }
  2114. /* Initialize the udc structure including QH member and other member */
  2115. if (struct_udc_setup(udc_controller, pdev)) {
  2116. ERR("Can't initialize udc data structure\n");
  2117. ret = -ENOMEM;
  2118. goto err_free_irq;
  2119. }
  2120. if (IS_ERR_OR_NULL(udc_controller->transceiver)) {
  2121. /* initialize usb hw reg except for regs for EP,
  2122. * leave usbintr reg untouched */
  2123. dr_controller_setup(udc_controller);
  2124. }
  2125. ret = fsl_udc_clk_finalize(pdev);
  2126. if (ret)
  2127. goto err_free_irq;
  2128. /* Setup gadget structure */
  2129. udc_controller->gadget.ops = &fsl_gadget_ops;
  2130. udc_controller->gadget.max_speed = USB_SPEED_HIGH;
  2131. udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
  2132. INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
  2133. udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
  2134. udc_controller->gadget.name = driver_name;
  2135. /* Setup gadget.dev and register with kernel */
  2136. dev_set_name(&udc_controller->gadget.dev, "gadget");
  2137. udc_controller->gadget.dev.release = fsl_udc_release;
  2138. udc_controller->gadget.dev.parent = &pdev->dev;
  2139. udc_controller->gadget.dev.of_node = pdev->dev.of_node;
  2140. ret = device_register(&udc_controller->gadget.dev);
  2141. if (ret < 0)
  2142. goto err_free_irq;
  2143. if (!IS_ERR_OR_NULL(udc_controller->transceiver))
  2144. udc_controller->gadget.is_otg = 1;
  2145. /* setup QH and epctrl for ep0 */
  2146. ep0_setup(udc_controller);
  2147. /* setup udc->eps[] for ep0 */
  2148. struct_ep_setup(udc_controller, 0, "ep0", 0);
  2149. /* for ep0: the desc defined here;
  2150. * for other eps, gadget layer called ep_enable with defined desc
  2151. */
  2152. udc_controller->eps[0].ep.desc = &fsl_ep0_desc;
  2153. udc_controller->eps[0].ep.maxpacket = USB_MAX_CTRL_PAYLOAD;
  2154. /* setup the udc->eps[] for non-control endpoints and link
  2155. * to gadget.ep_list */
  2156. for (i = 1; i < (int)(udc_controller->max_ep / 2); i++) {
  2157. char name[14];
  2158. sprintf(name, "ep%dout", i);
  2159. struct_ep_setup(udc_controller, i * 2, name, 1);
  2160. sprintf(name, "ep%din", i);
  2161. struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
  2162. }
  2163. /* use dma_pool for TD management */
  2164. udc_controller->td_pool = dma_pool_create("udc_td", &pdev->dev,
  2165. sizeof(struct ep_td_struct),
  2166. DTD_ALIGNMENT, UDC_DMA_BOUNDARY);
  2167. if (udc_controller->td_pool == NULL) {
  2168. ret = -ENOMEM;
  2169. goto err_unregister;
  2170. }
  2171. ret = usb_add_gadget_udc(&pdev->dev, &udc_controller->gadget);
  2172. if (ret)
  2173. goto err_del_udc;
  2174. create_proc_file();
  2175. return 0;
  2176. err_del_udc:
  2177. dma_pool_destroy(udc_controller->td_pool);
  2178. err_unregister:
  2179. device_unregister(&udc_controller->gadget.dev);
  2180. err_free_irq:
  2181. free_irq(udc_controller->irq, udc_controller);
  2182. err_iounmap:
  2183. if (pdata->exit)
  2184. pdata->exit(pdev);
  2185. fsl_udc_clk_release();
  2186. err_iounmap_noclk:
  2187. iounmap(dr_regs);
  2188. err_release_mem_region:
  2189. if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
  2190. release_mem_region(res->start, resource_size(res));
  2191. err_kfree:
  2192. kfree(udc_controller);
  2193. udc_controller = NULL;
  2194. return ret;
  2195. }
  2196. /* Driver removal function
  2197. * Free resources and finish pending transactions
  2198. */
  2199. static int __exit fsl_udc_remove(struct platform_device *pdev)
  2200. {
  2201. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2202. struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
  2203. DECLARE_COMPLETION(done);
  2204. if (!udc_controller)
  2205. return -ENODEV;
  2206. usb_del_gadget_udc(&udc_controller->gadget);
  2207. udc_controller->done = &done;
  2208. fsl_udc_clk_release();
  2209. /* DR has been stopped in usb_gadget_unregister_driver() */
  2210. remove_proc_file();
  2211. /* Free allocated memory */
  2212. kfree(udc_controller->status_req->req.buf);
  2213. kfree(udc_controller->status_req);
  2214. kfree(udc_controller->eps);
  2215. dma_pool_destroy(udc_controller->td_pool);
  2216. free_irq(udc_controller->irq, udc_controller);
  2217. iounmap(dr_regs);
  2218. if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
  2219. release_mem_region(res->start, resource_size(res));
  2220. device_unregister(&udc_controller->gadget.dev);
  2221. /* free udc --wait for the release() finished */
  2222. wait_for_completion(&done);
  2223. /*
  2224. * do platform specific un-initialization:
  2225. * release iomux pins, etc.
  2226. */
  2227. if (pdata->exit)
  2228. pdata->exit(pdev);
  2229. return 0;
  2230. }
  2231. /*-----------------------------------------------------------------
  2232. * Modify Power management attributes
  2233. * Used by OTG statemachine to disable gadget temporarily
  2234. -----------------------------------------------------------------*/
  2235. static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state)
  2236. {
  2237. dr_controller_stop(udc_controller);
  2238. return 0;
  2239. }
  2240. /*-----------------------------------------------------------------
  2241. * Invoked on USB resume. May be called in_interrupt.
  2242. * Here we start the DR controller and enable the irq
  2243. *-----------------------------------------------------------------*/
  2244. static int fsl_udc_resume(struct platform_device *pdev)
  2245. {
  2246. /* Enable DR irq reg and set controller Run */
  2247. if (udc_controller->stopped) {
  2248. dr_controller_setup(udc_controller);
  2249. dr_controller_run(udc_controller);
  2250. }
  2251. udc_controller->usb_state = USB_STATE_ATTACHED;
  2252. udc_controller->ep0_state = WAIT_FOR_SETUP;
  2253. udc_controller->ep0_dir = 0;
  2254. return 0;
  2255. }
  2256. static int fsl_udc_otg_suspend(struct device *dev, pm_message_t state)
  2257. {
  2258. struct fsl_udc *udc = udc_controller;
  2259. u32 mode, usbcmd;
  2260. mode = fsl_readl(&dr_regs->usbmode) & USB_MODE_CTRL_MODE_MASK;
  2261. pr_debug("%s(): mode 0x%x stopped %d\n", __func__, mode, udc->stopped);
  2262. /*
  2263. * If the controller is already stopped, then this must be a
  2264. * PM suspend. Remember this fact, so that we will leave the
  2265. * controller stopped at PM resume time.
  2266. */
  2267. if (udc->stopped) {
  2268. pr_debug("gadget already stopped, leaving early\n");
  2269. udc->already_stopped = 1;
  2270. return 0;
  2271. }
  2272. if (mode != USB_MODE_CTRL_MODE_DEVICE) {
  2273. pr_debug("gadget not in device mode, leaving early\n");
  2274. return 0;
  2275. }
  2276. /* stop the controller */
  2277. usbcmd = fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP;
  2278. fsl_writel(usbcmd, &dr_regs->usbcmd);
  2279. udc->stopped = 1;
  2280. pr_info("USB Gadget suspended\n");
  2281. return 0;
  2282. }
  2283. static int fsl_udc_otg_resume(struct device *dev)
  2284. {
  2285. pr_debug("%s(): stopped %d already_stopped %d\n", __func__,
  2286. udc_controller->stopped, udc_controller->already_stopped);
  2287. /*
  2288. * If the controller was stopped at suspend time, then
  2289. * don't resume it now.
  2290. */
  2291. if (udc_controller->already_stopped) {
  2292. udc_controller->already_stopped = 0;
  2293. pr_debug("gadget was already stopped, leaving early\n");
  2294. return 0;
  2295. }
  2296. pr_info("USB Gadget resume\n");
  2297. return fsl_udc_resume(NULL);
  2298. }
  2299. /*-------------------------------------------------------------------------
  2300. Register entry point for the peripheral controller driver
  2301. --------------------------------------------------------------------------*/
  2302. static const struct platform_device_id fsl_udc_devtype[] = {
  2303. {
  2304. .name = "imx-udc-mx27",
  2305. }, {
  2306. .name = "imx-udc-mx51",
  2307. }, {
  2308. /* sentinel */
  2309. }
  2310. };
  2311. MODULE_DEVICE_TABLE(platform, fsl_udc_devtype);
  2312. static struct platform_driver udc_driver = {
  2313. .remove = __exit_p(fsl_udc_remove),
  2314. /* Just for FSL i.mx SoC currently */
  2315. .id_table = fsl_udc_devtype,
  2316. /* these suspend and resume are not usb suspend and resume */
  2317. .suspend = fsl_udc_suspend,
  2318. .resume = fsl_udc_resume,
  2319. .driver = {
  2320. .name = (char *)driver_name,
  2321. .owner = THIS_MODULE,
  2322. /* udc suspend/resume called from OTG driver */
  2323. .suspend = fsl_udc_otg_suspend,
  2324. .resume = fsl_udc_otg_resume,
  2325. },
  2326. };
  2327. static int __init udc_init(void)
  2328. {
  2329. printk(KERN_INFO "%s (%s)\n", driver_desc, DRIVER_VERSION);
  2330. return platform_driver_probe(&udc_driver, fsl_udc_probe);
  2331. }
  2332. module_init(udc_init);
  2333. static void __exit udc_exit(void)
  2334. {
  2335. platform_driver_unregister(&udc_driver);
  2336. printk(KERN_WARNING "%s unregistered\n", driver_desc);
  2337. }
  2338. module_exit(udc_exit);
  2339. MODULE_DESCRIPTION(DRIVER_DESC);
  2340. MODULE_AUTHOR(DRIVER_AUTHOR);
  2341. MODULE_LICENSE("GPL");
  2342. MODULE_ALIAS("platform:fsl-usb2-udc");