fsl_udc_core.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805
  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_start(struct usb_gadget_driver *driver,
  1057. int (*bind)(struct usb_gadget *, struct usb_gadget_driver *));
  1058. static int fsl_stop(struct usb_gadget_driver *driver);
  1059. /* defined in gadget.h */
  1060. static struct usb_gadget_ops fsl_gadget_ops = {
  1061. .get_frame = fsl_get_frame,
  1062. .wakeup = fsl_wakeup,
  1063. /* .set_selfpowered = fsl_set_selfpowered, */ /* Always selfpowered */
  1064. .vbus_session = fsl_vbus_session,
  1065. .vbus_draw = fsl_vbus_draw,
  1066. .pullup = fsl_pullup,
  1067. .start = fsl_start,
  1068. .stop = fsl_stop,
  1069. };
  1070. /* Set protocol stall on ep0, protocol stall will automatically be cleared
  1071. on new transaction */
  1072. static void ep0stall(struct fsl_udc *udc)
  1073. {
  1074. u32 tmp;
  1075. /* must set tx and rx to stall at the same time */
  1076. tmp = fsl_readl(&dr_regs->endptctrl[0]);
  1077. tmp |= EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL;
  1078. fsl_writel(tmp, &dr_regs->endptctrl[0]);
  1079. udc->ep0_state = WAIT_FOR_SETUP;
  1080. udc->ep0_dir = 0;
  1081. }
  1082. /* Prime a status phase for ep0 */
  1083. static int ep0_prime_status(struct fsl_udc *udc, int direction)
  1084. {
  1085. struct fsl_req *req = udc->status_req;
  1086. struct fsl_ep *ep;
  1087. if (direction == EP_DIR_IN)
  1088. udc->ep0_dir = USB_DIR_IN;
  1089. else
  1090. udc->ep0_dir = USB_DIR_OUT;
  1091. ep = &udc->eps[0];
  1092. if (udc->ep0_state != DATA_STATE_XMIT)
  1093. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1094. req->ep = ep;
  1095. req->req.length = 0;
  1096. req->req.status = -EINPROGRESS;
  1097. req->req.actual = 0;
  1098. req->req.complete = NULL;
  1099. req->dtd_count = 0;
  1100. req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
  1101. req->req.buf, req->req.length,
  1102. ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1103. req->mapped = 1;
  1104. if (fsl_req_to_dtd(req, GFP_ATOMIC) == 0)
  1105. fsl_queue_td(ep, req);
  1106. else
  1107. return -ENOMEM;
  1108. list_add_tail(&req->queue, &ep->queue);
  1109. return 0;
  1110. }
  1111. static void udc_reset_ep_queue(struct fsl_udc *udc, u8 pipe)
  1112. {
  1113. struct fsl_ep *ep = get_ep_by_pipe(udc, pipe);
  1114. if (ep->name)
  1115. nuke(ep, -ESHUTDOWN);
  1116. }
  1117. /*
  1118. * ch9 Set address
  1119. */
  1120. static void ch9setaddress(struct fsl_udc *udc, u16 value, u16 index, u16 length)
  1121. {
  1122. /* Save the new address to device struct */
  1123. udc->device_address = (u8) value;
  1124. /* Update usb state */
  1125. udc->usb_state = USB_STATE_ADDRESS;
  1126. /* Status phase */
  1127. if (ep0_prime_status(udc, EP_DIR_IN))
  1128. ep0stall(udc);
  1129. }
  1130. /*
  1131. * ch9 Get status
  1132. */
  1133. static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
  1134. u16 index, u16 length)
  1135. {
  1136. u16 tmp = 0; /* Status, cpu endian */
  1137. struct fsl_req *req;
  1138. struct fsl_ep *ep;
  1139. ep = &udc->eps[0];
  1140. if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  1141. /* Get device status */
  1142. tmp = 1 << USB_DEVICE_SELF_POWERED;
  1143. tmp |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
  1144. } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
  1145. /* Get interface status */
  1146. /* We don't have interface information in udc driver */
  1147. tmp = 0;
  1148. } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
  1149. /* Get endpoint status */
  1150. struct fsl_ep *target_ep;
  1151. target_ep = get_ep_by_pipe(udc, get_pipe_by_windex(index));
  1152. /* stall if endpoint doesn't exist */
  1153. if (!target_ep->ep.desc)
  1154. goto stall;
  1155. tmp = dr_ep_get_stall(ep_index(target_ep), ep_is_in(target_ep))
  1156. << USB_ENDPOINT_HALT;
  1157. }
  1158. udc->ep0_dir = USB_DIR_IN;
  1159. /* Borrow the per device status_req */
  1160. req = udc->status_req;
  1161. /* Fill in the reqest structure */
  1162. *((u16 *) req->req.buf) = cpu_to_le16(tmp);
  1163. req->ep = ep;
  1164. req->req.length = 2;
  1165. req->req.status = -EINPROGRESS;
  1166. req->req.actual = 0;
  1167. req->req.complete = NULL;
  1168. req->dtd_count = 0;
  1169. req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
  1170. req->req.buf, req->req.length,
  1171. ep_is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1172. req->mapped = 1;
  1173. /* prime the data phase */
  1174. if ((fsl_req_to_dtd(req, GFP_ATOMIC) == 0))
  1175. fsl_queue_td(ep, req);
  1176. else /* no mem */
  1177. goto stall;
  1178. list_add_tail(&req->queue, &ep->queue);
  1179. udc->ep0_state = DATA_STATE_XMIT;
  1180. if (ep0_prime_status(udc, EP_DIR_OUT))
  1181. ep0stall(udc);
  1182. return;
  1183. stall:
  1184. ep0stall(udc);
  1185. }
  1186. static void setup_received_irq(struct fsl_udc *udc,
  1187. struct usb_ctrlrequest *setup)
  1188. {
  1189. u16 wValue = le16_to_cpu(setup->wValue);
  1190. u16 wIndex = le16_to_cpu(setup->wIndex);
  1191. u16 wLength = le16_to_cpu(setup->wLength);
  1192. udc_reset_ep_queue(udc, 0);
  1193. /* We process some stardard setup requests here */
  1194. switch (setup->bRequest) {
  1195. case USB_REQ_GET_STATUS:
  1196. /* Data+Status phase from udc */
  1197. if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
  1198. != (USB_DIR_IN | USB_TYPE_STANDARD))
  1199. break;
  1200. ch9getstatus(udc, setup->bRequestType, wValue, wIndex, wLength);
  1201. return;
  1202. case USB_REQ_SET_ADDRESS:
  1203. /* Status phase from udc */
  1204. if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
  1205. | USB_RECIP_DEVICE))
  1206. break;
  1207. ch9setaddress(udc, wValue, wIndex, wLength);
  1208. return;
  1209. case USB_REQ_CLEAR_FEATURE:
  1210. case USB_REQ_SET_FEATURE:
  1211. /* Status phase from udc */
  1212. {
  1213. int rc = -EOPNOTSUPP;
  1214. u16 ptc = 0;
  1215. if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
  1216. == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
  1217. int pipe = get_pipe_by_windex(wIndex);
  1218. struct fsl_ep *ep;
  1219. if (wValue != 0 || wLength != 0 || pipe >= udc->max_ep)
  1220. break;
  1221. ep = get_ep_by_pipe(udc, pipe);
  1222. spin_unlock(&udc->lock);
  1223. rc = fsl_ep_set_halt(&ep->ep,
  1224. (setup->bRequest == USB_REQ_SET_FEATURE)
  1225. ? 1 : 0);
  1226. spin_lock(&udc->lock);
  1227. } else if ((setup->bRequestType & (USB_RECIP_MASK
  1228. | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
  1229. | USB_TYPE_STANDARD)) {
  1230. /* Note: The driver has not include OTG support yet.
  1231. * This will be set when OTG support is added */
  1232. if (wValue == USB_DEVICE_TEST_MODE)
  1233. ptc = wIndex >> 8;
  1234. else if (gadget_is_otg(&udc->gadget)) {
  1235. if (setup->bRequest ==
  1236. USB_DEVICE_B_HNP_ENABLE)
  1237. udc->gadget.b_hnp_enable = 1;
  1238. else if (setup->bRequest ==
  1239. USB_DEVICE_A_HNP_SUPPORT)
  1240. udc->gadget.a_hnp_support = 1;
  1241. else if (setup->bRequest ==
  1242. USB_DEVICE_A_ALT_HNP_SUPPORT)
  1243. udc->gadget.a_alt_hnp_support = 1;
  1244. }
  1245. rc = 0;
  1246. } else
  1247. break;
  1248. if (rc == 0) {
  1249. if (ep0_prime_status(udc, EP_DIR_IN))
  1250. ep0stall(udc);
  1251. }
  1252. if (ptc) {
  1253. u32 tmp;
  1254. mdelay(10);
  1255. tmp = fsl_readl(&dr_regs->portsc1) | (ptc << 16);
  1256. fsl_writel(tmp, &dr_regs->portsc1);
  1257. printk(KERN_INFO "udc: switch to test mode %d.\n", ptc);
  1258. }
  1259. return;
  1260. }
  1261. default:
  1262. break;
  1263. }
  1264. /* Requests handled by gadget */
  1265. if (wLength) {
  1266. /* Data phase from gadget, status phase from udc */
  1267. udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
  1268. ? USB_DIR_IN : USB_DIR_OUT;
  1269. spin_unlock(&udc->lock);
  1270. if (udc->driver->setup(&udc->gadget,
  1271. &udc->local_setup_buff) < 0)
  1272. ep0stall(udc);
  1273. spin_lock(&udc->lock);
  1274. udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
  1275. ? DATA_STATE_XMIT : DATA_STATE_RECV;
  1276. /*
  1277. * If the data stage is IN, send status prime immediately.
  1278. * See 2.0 Spec chapter 8.5.3.3 for detail.
  1279. */
  1280. if (udc->ep0_state == DATA_STATE_XMIT)
  1281. if (ep0_prime_status(udc, EP_DIR_OUT))
  1282. ep0stall(udc);
  1283. } else {
  1284. /* No data phase, IN status from gadget */
  1285. udc->ep0_dir = USB_DIR_IN;
  1286. spin_unlock(&udc->lock);
  1287. if (udc->driver->setup(&udc->gadget,
  1288. &udc->local_setup_buff) < 0)
  1289. ep0stall(udc);
  1290. spin_lock(&udc->lock);
  1291. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1292. }
  1293. }
  1294. /* Process request for Data or Status phase of ep0
  1295. * prime status phase if needed */
  1296. static void ep0_req_complete(struct fsl_udc *udc, struct fsl_ep *ep0,
  1297. struct fsl_req *req)
  1298. {
  1299. if (udc->usb_state == USB_STATE_ADDRESS) {
  1300. /* Set the new address */
  1301. u32 new_address = (u32) udc->device_address;
  1302. fsl_writel(new_address << USB_DEVICE_ADDRESS_BIT_POS,
  1303. &dr_regs->deviceaddr);
  1304. }
  1305. done(ep0, req, 0);
  1306. switch (udc->ep0_state) {
  1307. case DATA_STATE_XMIT:
  1308. /* already primed at setup_received_irq */
  1309. udc->ep0_state = WAIT_FOR_OUT_STATUS;
  1310. break;
  1311. case DATA_STATE_RECV:
  1312. /* send status phase */
  1313. if (ep0_prime_status(udc, EP_DIR_IN))
  1314. ep0stall(udc);
  1315. break;
  1316. case WAIT_FOR_OUT_STATUS:
  1317. udc->ep0_state = WAIT_FOR_SETUP;
  1318. break;
  1319. case WAIT_FOR_SETUP:
  1320. ERR("Unexpect ep0 packets\n");
  1321. break;
  1322. default:
  1323. ep0stall(udc);
  1324. break;
  1325. }
  1326. }
  1327. /* Tripwire mechanism to ensure a setup packet payload is extracted without
  1328. * being corrupted by another incoming setup packet */
  1329. static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
  1330. {
  1331. u32 temp;
  1332. struct ep_queue_head *qh;
  1333. struct fsl_usb2_platform_data *pdata = udc->pdata;
  1334. qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
  1335. /* Clear bit in ENDPTSETUPSTAT */
  1336. temp = fsl_readl(&dr_regs->endptsetupstat);
  1337. fsl_writel(temp | (1 << ep_num), &dr_regs->endptsetupstat);
  1338. /* while a hazard exists when setup package arrives */
  1339. do {
  1340. /* Set Setup Tripwire */
  1341. temp = fsl_readl(&dr_regs->usbcmd);
  1342. fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
  1343. /* Copy the setup packet to local buffer */
  1344. if (pdata->le_setup_buf) {
  1345. u32 *p = (u32 *)buffer_ptr;
  1346. u32 *s = (u32 *)qh->setup_buffer;
  1347. /* Convert little endian setup buffer to CPU endian */
  1348. *p++ = le32_to_cpu(*s++);
  1349. *p = le32_to_cpu(*s);
  1350. } else {
  1351. memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
  1352. }
  1353. } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
  1354. /* Clear Setup Tripwire */
  1355. temp = fsl_readl(&dr_regs->usbcmd);
  1356. fsl_writel(temp & ~USB_CMD_SUTW, &dr_regs->usbcmd);
  1357. }
  1358. /* process-ep_req(): free the completed Tds for this req */
  1359. static int process_ep_req(struct fsl_udc *udc, int pipe,
  1360. struct fsl_req *curr_req)
  1361. {
  1362. struct ep_td_struct *curr_td;
  1363. int td_complete, actual, remaining_length, j, tmp;
  1364. int status = 0;
  1365. int errors = 0;
  1366. struct ep_queue_head *curr_qh = &udc->ep_qh[pipe];
  1367. int direction = pipe % 2;
  1368. curr_td = curr_req->head;
  1369. td_complete = 0;
  1370. actual = curr_req->req.length;
  1371. for (j = 0; j < curr_req->dtd_count; j++) {
  1372. remaining_length = (hc32_to_cpu(curr_td->size_ioc_sts)
  1373. & DTD_PACKET_SIZE)
  1374. >> DTD_LENGTH_BIT_POS;
  1375. actual -= remaining_length;
  1376. errors = hc32_to_cpu(curr_td->size_ioc_sts);
  1377. if (errors & DTD_ERROR_MASK) {
  1378. if (errors & DTD_STATUS_HALTED) {
  1379. ERR("dTD error %08x QH=%d\n", errors, pipe);
  1380. /* Clear the errors and Halt condition */
  1381. tmp = hc32_to_cpu(curr_qh->size_ioc_int_sts);
  1382. tmp &= ~errors;
  1383. curr_qh->size_ioc_int_sts = cpu_to_hc32(tmp);
  1384. status = -EPIPE;
  1385. /* FIXME: continue with next queued TD? */
  1386. break;
  1387. }
  1388. if (errors & DTD_STATUS_DATA_BUFF_ERR) {
  1389. VDBG("Transfer overflow");
  1390. status = -EPROTO;
  1391. break;
  1392. } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
  1393. VDBG("ISO error");
  1394. status = -EILSEQ;
  1395. break;
  1396. } else
  1397. ERR("Unknown error has occurred (0x%x)!\n",
  1398. errors);
  1399. } else if (hc32_to_cpu(curr_td->size_ioc_sts)
  1400. & DTD_STATUS_ACTIVE) {
  1401. VDBG("Request not complete");
  1402. status = REQ_UNCOMPLETE;
  1403. return status;
  1404. } else if (remaining_length) {
  1405. if (direction) {
  1406. VDBG("Transmit dTD remaining length not zero");
  1407. status = -EPROTO;
  1408. break;
  1409. } else {
  1410. td_complete++;
  1411. break;
  1412. }
  1413. } else {
  1414. td_complete++;
  1415. VDBG("dTD transmitted successful");
  1416. }
  1417. if (j != curr_req->dtd_count - 1)
  1418. curr_td = (struct ep_td_struct *)curr_td->next_td_virt;
  1419. }
  1420. if (status)
  1421. return status;
  1422. curr_req->req.actual = actual;
  1423. return 0;
  1424. }
  1425. /* Process a DTD completion interrupt */
  1426. static void dtd_complete_irq(struct fsl_udc *udc)
  1427. {
  1428. u32 bit_pos;
  1429. int i, ep_num, direction, bit_mask, status;
  1430. struct fsl_ep *curr_ep;
  1431. struct fsl_req *curr_req, *temp_req;
  1432. /* Clear the bits in the register */
  1433. bit_pos = fsl_readl(&dr_regs->endptcomplete);
  1434. fsl_writel(bit_pos, &dr_regs->endptcomplete);
  1435. if (!bit_pos)
  1436. return;
  1437. for (i = 0; i < udc->max_ep; i++) {
  1438. ep_num = i >> 1;
  1439. direction = i % 2;
  1440. bit_mask = 1 << (ep_num + 16 * direction);
  1441. if (!(bit_pos & bit_mask))
  1442. continue;
  1443. curr_ep = get_ep_by_pipe(udc, i);
  1444. /* If the ep is configured */
  1445. if (curr_ep->name == NULL) {
  1446. WARNING("Invalid EP?");
  1447. continue;
  1448. }
  1449. /* process the req queue until an uncomplete request */
  1450. list_for_each_entry_safe(curr_req, temp_req, &curr_ep->queue,
  1451. queue) {
  1452. status = process_ep_req(udc, i, curr_req);
  1453. VDBG("status of process_ep_req= %d, ep = %d",
  1454. status, ep_num);
  1455. if (status == REQ_UNCOMPLETE)
  1456. break;
  1457. /* write back status to req */
  1458. curr_req->req.status = status;
  1459. if (ep_num == 0) {
  1460. ep0_req_complete(udc, curr_ep, curr_req);
  1461. break;
  1462. } else
  1463. done(curr_ep, curr_req, status);
  1464. }
  1465. }
  1466. }
  1467. static inline enum usb_device_speed portscx_device_speed(u32 reg)
  1468. {
  1469. switch (reg & PORTSCX_PORT_SPEED_MASK) {
  1470. case PORTSCX_PORT_SPEED_HIGH:
  1471. return USB_SPEED_HIGH;
  1472. case PORTSCX_PORT_SPEED_FULL:
  1473. return USB_SPEED_FULL;
  1474. case PORTSCX_PORT_SPEED_LOW:
  1475. return USB_SPEED_LOW;
  1476. default:
  1477. return USB_SPEED_UNKNOWN;
  1478. }
  1479. }
  1480. /* Process a port change interrupt */
  1481. static void port_change_irq(struct fsl_udc *udc)
  1482. {
  1483. if (udc->bus_reset)
  1484. udc->bus_reset = 0;
  1485. /* Bus resetting is finished */
  1486. if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET))
  1487. /* Get the speed */
  1488. udc->gadget.speed =
  1489. portscx_device_speed(fsl_readl(&dr_regs->portsc1));
  1490. /* Update USB state */
  1491. if (!udc->resume_state)
  1492. udc->usb_state = USB_STATE_DEFAULT;
  1493. }
  1494. /* Process suspend interrupt */
  1495. static void suspend_irq(struct fsl_udc *udc)
  1496. {
  1497. udc->resume_state = udc->usb_state;
  1498. udc->usb_state = USB_STATE_SUSPENDED;
  1499. /* report suspend to the driver, serial.c does not support this */
  1500. if (udc->driver->suspend)
  1501. udc->driver->suspend(&udc->gadget);
  1502. }
  1503. static void bus_resume(struct fsl_udc *udc)
  1504. {
  1505. udc->usb_state = udc->resume_state;
  1506. udc->resume_state = 0;
  1507. /* report resume to the driver, serial.c does not support this */
  1508. if (udc->driver->resume)
  1509. udc->driver->resume(&udc->gadget);
  1510. }
  1511. /* Clear up all ep queues */
  1512. static int reset_queues(struct fsl_udc *udc)
  1513. {
  1514. u8 pipe;
  1515. for (pipe = 0; pipe < udc->max_pipes; pipe++)
  1516. udc_reset_ep_queue(udc, pipe);
  1517. /* report disconnect; the driver is already quiesced */
  1518. spin_unlock(&udc->lock);
  1519. udc->driver->disconnect(&udc->gadget);
  1520. spin_lock(&udc->lock);
  1521. return 0;
  1522. }
  1523. /* Process reset interrupt */
  1524. static void reset_irq(struct fsl_udc *udc)
  1525. {
  1526. u32 temp;
  1527. unsigned long timeout;
  1528. /* Clear the device address */
  1529. temp = fsl_readl(&dr_regs->deviceaddr);
  1530. fsl_writel(temp & ~USB_DEVICE_ADDRESS_MASK, &dr_regs->deviceaddr);
  1531. udc->device_address = 0;
  1532. /* Clear usb state */
  1533. udc->resume_state = 0;
  1534. udc->ep0_dir = 0;
  1535. udc->ep0_state = WAIT_FOR_SETUP;
  1536. udc->remote_wakeup = 0; /* default to 0 on reset */
  1537. udc->gadget.b_hnp_enable = 0;
  1538. udc->gadget.a_hnp_support = 0;
  1539. udc->gadget.a_alt_hnp_support = 0;
  1540. /* Clear all the setup token semaphores */
  1541. temp = fsl_readl(&dr_regs->endptsetupstat);
  1542. fsl_writel(temp, &dr_regs->endptsetupstat);
  1543. /* Clear all the endpoint complete status bits */
  1544. temp = fsl_readl(&dr_regs->endptcomplete);
  1545. fsl_writel(temp, &dr_regs->endptcomplete);
  1546. timeout = jiffies + 100;
  1547. while (fsl_readl(&dr_regs->endpointprime)) {
  1548. /* Wait until all endptprime bits cleared */
  1549. if (time_after(jiffies, timeout)) {
  1550. ERR("Timeout for reset\n");
  1551. break;
  1552. }
  1553. cpu_relax();
  1554. }
  1555. /* Write 1s to the flush register */
  1556. fsl_writel(0xffffffff, &dr_regs->endptflush);
  1557. if (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET) {
  1558. VDBG("Bus reset");
  1559. /* Bus is reseting */
  1560. udc->bus_reset = 1;
  1561. /* Reset all the queues, include XD, dTD, EP queue
  1562. * head and TR Queue */
  1563. reset_queues(udc);
  1564. udc->usb_state = USB_STATE_DEFAULT;
  1565. } else {
  1566. VDBG("Controller reset");
  1567. /* initialize usb hw reg except for regs for EP, not
  1568. * touch usbintr reg */
  1569. dr_controller_setup(udc);
  1570. /* Reset all internal used Queues */
  1571. reset_queues(udc);
  1572. ep0_setup(udc);
  1573. /* Enable DR IRQ reg, Set Run bit, change udc state */
  1574. dr_controller_run(udc);
  1575. udc->usb_state = USB_STATE_ATTACHED;
  1576. }
  1577. }
  1578. /*
  1579. * USB device controller interrupt handler
  1580. */
  1581. static irqreturn_t fsl_udc_irq(int irq, void *_udc)
  1582. {
  1583. struct fsl_udc *udc = _udc;
  1584. u32 irq_src;
  1585. irqreturn_t status = IRQ_NONE;
  1586. unsigned long flags;
  1587. /* Disable ISR for OTG host mode */
  1588. if (udc->stopped)
  1589. return IRQ_NONE;
  1590. spin_lock_irqsave(&udc->lock, flags);
  1591. irq_src = fsl_readl(&dr_regs->usbsts) & fsl_readl(&dr_regs->usbintr);
  1592. /* Clear notification bits */
  1593. fsl_writel(irq_src, &dr_regs->usbsts);
  1594. /* VDBG("irq_src [0x%8x]", irq_src); */
  1595. /* Need to resume? */
  1596. if (udc->usb_state == USB_STATE_SUSPENDED)
  1597. if ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SUSPEND) == 0)
  1598. bus_resume(udc);
  1599. /* USB Interrupt */
  1600. if (irq_src & USB_STS_INT) {
  1601. VDBG("Packet int");
  1602. /* Setup package, we only support ep0 as control ep */
  1603. if (fsl_readl(&dr_regs->endptsetupstat) & EP_SETUP_STATUS_EP0) {
  1604. tripwire_handler(udc, 0,
  1605. (u8 *) (&udc->local_setup_buff));
  1606. setup_received_irq(udc, &udc->local_setup_buff);
  1607. status = IRQ_HANDLED;
  1608. }
  1609. /* completion of dtd */
  1610. if (fsl_readl(&dr_regs->endptcomplete)) {
  1611. dtd_complete_irq(udc);
  1612. status = IRQ_HANDLED;
  1613. }
  1614. }
  1615. /* SOF (for ISO transfer) */
  1616. if (irq_src & USB_STS_SOF) {
  1617. status = IRQ_HANDLED;
  1618. }
  1619. /* Port Change */
  1620. if (irq_src & USB_STS_PORT_CHANGE) {
  1621. port_change_irq(udc);
  1622. status = IRQ_HANDLED;
  1623. }
  1624. /* Reset Received */
  1625. if (irq_src & USB_STS_RESET) {
  1626. VDBG("reset int");
  1627. reset_irq(udc);
  1628. status = IRQ_HANDLED;
  1629. }
  1630. /* Sleep Enable (Suspend) */
  1631. if (irq_src & USB_STS_SUSPEND) {
  1632. suspend_irq(udc);
  1633. status = IRQ_HANDLED;
  1634. }
  1635. if (irq_src & (USB_STS_ERR | USB_STS_SYS_ERR)) {
  1636. VDBG("Error IRQ %x", irq_src);
  1637. }
  1638. spin_unlock_irqrestore(&udc->lock, flags);
  1639. return status;
  1640. }
  1641. /*----------------------------------------------------------------*
  1642. * Hook to gadget drivers
  1643. * Called by initialization code of gadget drivers
  1644. *----------------------------------------------------------------*/
  1645. static int fsl_start(struct usb_gadget_driver *driver,
  1646. int (*bind)(struct usb_gadget *, struct usb_gadget_driver *))
  1647. {
  1648. int retval = -ENODEV;
  1649. unsigned long flags = 0;
  1650. if (!udc_controller)
  1651. return -ENODEV;
  1652. if (!driver || driver->max_speed < USB_SPEED_FULL
  1653. || !bind || !driver->disconnect || !driver->setup)
  1654. return -EINVAL;
  1655. if (udc_controller->driver)
  1656. return -EBUSY;
  1657. /* lock is needed but whether should use this lock or another */
  1658. spin_lock_irqsave(&udc_controller->lock, flags);
  1659. driver->driver.bus = NULL;
  1660. /* hook up the driver */
  1661. udc_controller->driver = driver;
  1662. udc_controller->gadget.dev.driver = &driver->driver;
  1663. spin_unlock_irqrestore(&udc_controller->lock, flags);
  1664. /* bind udc driver to gadget driver */
  1665. retval = bind(&udc_controller->gadget, driver);
  1666. if (retval) {
  1667. VDBG("bind to %s --> %d", driver->driver.name, retval);
  1668. udc_controller->gadget.dev.driver = NULL;
  1669. udc_controller->driver = NULL;
  1670. goto out;
  1671. }
  1672. if (!IS_ERR_OR_NULL(udc_controller->transceiver)) {
  1673. /* Suspend the controller until OTG enable it */
  1674. udc_controller->stopped = 1;
  1675. printk(KERN_INFO "Suspend udc for OTG auto detect\n");
  1676. /* connect to bus through transceiver */
  1677. if (!IS_ERR_OR_NULL(udc_controller->transceiver)) {
  1678. retval = otg_set_peripheral(
  1679. udc_controller->transceiver->otg,
  1680. &udc_controller->gadget);
  1681. if (retval < 0) {
  1682. ERR("can't bind to transceiver\n");
  1683. driver->unbind(&udc_controller->gadget);
  1684. udc_controller->gadget.dev.driver = 0;
  1685. udc_controller->driver = 0;
  1686. return retval;
  1687. }
  1688. }
  1689. } else {
  1690. /* Enable DR IRQ reg and set USBCMD reg Run bit */
  1691. dr_controller_run(udc_controller);
  1692. udc_controller->usb_state = USB_STATE_ATTACHED;
  1693. udc_controller->ep0_state = WAIT_FOR_SETUP;
  1694. udc_controller->ep0_dir = 0;
  1695. }
  1696. printk(KERN_INFO "%s: bind to driver %s\n",
  1697. udc_controller->gadget.name, driver->driver.name);
  1698. out:
  1699. if (retval)
  1700. printk(KERN_WARNING "gadget driver register failed %d\n",
  1701. retval);
  1702. return retval;
  1703. }
  1704. /* Disconnect from gadget driver */
  1705. static int fsl_stop(struct usb_gadget_driver *driver)
  1706. {
  1707. struct fsl_ep *loop_ep;
  1708. unsigned long flags;
  1709. if (!udc_controller)
  1710. return -ENODEV;
  1711. if (!driver || driver != udc_controller->driver || !driver->unbind)
  1712. return -EINVAL;
  1713. if (!IS_ERR_OR_NULL(udc_controller->transceiver))
  1714. otg_set_peripheral(udc_controller->transceiver->otg, NULL);
  1715. /* stop DR, disable intr */
  1716. dr_controller_stop(udc_controller);
  1717. /* in fact, no needed */
  1718. udc_controller->usb_state = USB_STATE_ATTACHED;
  1719. udc_controller->ep0_state = WAIT_FOR_SETUP;
  1720. udc_controller->ep0_dir = 0;
  1721. /* stand operation */
  1722. spin_lock_irqsave(&udc_controller->lock, flags);
  1723. udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
  1724. nuke(&udc_controller->eps[0], -ESHUTDOWN);
  1725. list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
  1726. ep.ep_list)
  1727. nuke(loop_ep, -ESHUTDOWN);
  1728. spin_unlock_irqrestore(&udc_controller->lock, flags);
  1729. /* report disconnect; the controller is already quiesced */
  1730. driver->disconnect(&udc_controller->gadget);
  1731. /* unbind gadget and unhook driver. */
  1732. driver->unbind(&udc_controller->gadget);
  1733. udc_controller->gadget.dev.driver = NULL;
  1734. udc_controller->driver = NULL;
  1735. printk(KERN_WARNING "unregistered gadget driver '%s'\n",
  1736. driver->driver.name);
  1737. return 0;
  1738. }
  1739. /*-------------------------------------------------------------------------
  1740. PROC File System Support
  1741. -------------------------------------------------------------------------*/
  1742. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1743. #include <linux/seq_file.h>
  1744. static const char proc_filename[] = "driver/fsl_usb2_udc";
  1745. static int fsl_proc_read(char *page, char **start, off_t off, int count,
  1746. int *eof, void *_dev)
  1747. {
  1748. char *buf = page;
  1749. char *next = buf;
  1750. unsigned size = count;
  1751. unsigned long flags;
  1752. int t, i;
  1753. u32 tmp_reg;
  1754. struct fsl_ep *ep = NULL;
  1755. struct fsl_req *req;
  1756. struct fsl_udc *udc = udc_controller;
  1757. if (off != 0)
  1758. return 0;
  1759. spin_lock_irqsave(&udc->lock, flags);
  1760. /* ------basic driver information ---- */
  1761. t = scnprintf(next, size,
  1762. DRIVER_DESC "\n"
  1763. "%s version: %s\n"
  1764. "Gadget driver: %s\n\n",
  1765. driver_name, DRIVER_VERSION,
  1766. udc->driver ? udc->driver->driver.name : "(none)");
  1767. size -= t;
  1768. next += t;
  1769. /* ------ DR Registers ----- */
  1770. tmp_reg = fsl_readl(&dr_regs->usbcmd);
  1771. t = scnprintf(next, size,
  1772. "USBCMD reg:\n"
  1773. "SetupTW: %d\n"
  1774. "Run/Stop: %s\n\n",
  1775. (tmp_reg & USB_CMD_SUTW) ? 1 : 0,
  1776. (tmp_reg & USB_CMD_RUN_STOP) ? "Run" : "Stop");
  1777. size -= t;
  1778. next += t;
  1779. tmp_reg = fsl_readl(&dr_regs->usbsts);
  1780. t = scnprintf(next, size,
  1781. "USB Status Reg:\n"
  1782. "Dr Suspend: %d Reset Received: %d System Error: %s "
  1783. "USB Error Interrupt: %s\n\n",
  1784. (tmp_reg & USB_STS_SUSPEND) ? 1 : 0,
  1785. (tmp_reg & USB_STS_RESET) ? 1 : 0,
  1786. (tmp_reg & USB_STS_SYS_ERR) ? "Err" : "Normal",
  1787. (tmp_reg & USB_STS_ERR) ? "Err detected" : "No err");
  1788. size -= t;
  1789. next += t;
  1790. tmp_reg = fsl_readl(&dr_regs->usbintr);
  1791. t = scnprintf(next, size,
  1792. "USB Interrupt Enable Reg:\n"
  1793. "Sleep Enable: %d SOF Received Enable: %d "
  1794. "Reset Enable: %d\n"
  1795. "System Error Enable: %d "
  1796. "Port Change Dectected Enable: %d\n"
  1797. "USB Error Intr Enable: %d USB Intr Enable: %d\n\n",
  1798. (tmp_reg & USB_INTR_DEVICE_SUSPEND) ? 1 : 0,
  1799. (tmp_reg & USB_INTR_SOF_EN) ? 1 : 0,
  1800. (tmp_reg & USB_INTR_RESET_EN) ? 1 : 0,
  1801. (tmp_reg & USB_INTR_SYS_ERR_EN) ? 1 : 0,
  1802. (tmp_reg & USB_INTR_PTC_DETECT_EN) ? 1 : 0,
  1803. (tmp_reg & USB_INTR_ERR_INT_EN) ? 1 : 0,
  1804. (tmp_reg & USB_INTR_INT_EN) ? 1 : 0);
  1805. size -= t;
  1806. next += t;
  1807. tmp_reg = fsl_readl(&dr_regs->frindex);
  1808. t = scnprintf(next, size,
  1809. "USB Frame Index Reg: Frame Number is 0x%x\n\n",
  1810. (tmp_reg & USB_FRINDEX_MASKS));
  1811. size -= t;
  1812. next += t;
  1813. tmp_reg = fsl_readl(&dr_regs->deviceaddr);
  1814. t = scnprintf(next, size,
  1815. "USB Device Address Reg: Device Addr is 0x%x\n\n",
  1816. (tmp_reg & USB_DEVICE_ADDRESS_MASK));
  1817. size -= t;
  1818. next += t;
  1819. tmp_reg = fsl_readl(&dr_regs->endpointlistaddr);
  1820. t = scnprintf(next, size,
  1821. "USB Endpoint List Address Reg: "
  1822. "Device Addr is 0x%x\n\n",
  1823. (tmp_reg & USB_EP_LIST_ADDRESS_MASK));
  1824. size -= t;
  1825. next += t;
  1826. tmp_reg = fsl_readl(&dr_regs->portsc1);
  1827. t = scnprintf(next, size,
  1828. "USB Port Status&Control Reg:\n"
  1829. "Port Transceiver Type : %s Port Speed: %s\n"
  1830. "PHY Low Power Suspend: %s Port Reset: %s "
  1831. "Port Suspend Mode: %s\n"
  1832. "Over-current Change: %s "
  1833. "Port Enable/Disable Change: %s\n"
  1834. "Port Enabled/Disabled: %s "
  1835. "Current Connect Status: %s\n\n", ( {
  1836. char *s;
  1837. switch (tmp_reg & PORTSCX_PTS_FSLS) {
  1838. case PORTSCX_PTS_UTMI:
  1839. s = "UTMI"; break;
  1840. case PORTSCX_PTS_ULPI:
  1841. s = "ULPI "; break;
  1842. case PORTSCX_PTS_FSLS:
  1843. s = "FS/LS Serial"; break;
  1844. default:
  1845. s = "None"; break;
  1846. }
  1847. s;} ),
  1848. usb_speed_string(portscx_device_speed(tmp_reg)),
  1849. (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ?
  1850. "Normal PHY mode" : "Low power mode",
  1851. (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" :
  1852. "Not in Reset",
  1853. (tmp_reg & PORTSCX_PORT_SUSPEND) ? "In " : "Not in",
  1854. (tmp_reg & PORTSCX_OVER_CURRENT_CHG) ? "Dected" :
  1855. "No",
  1856. (tmp_reg & PORTSCX_PORT_EN_DIS_CHANGE) ? "Disable" :
  1857. "Not change",
  1858. (tmp_reg & PORTSCX_PORT_ENABLE) ? "Enable" :
  1859. "Not correct",
  1860. (tmp_reg & PORTSCX_CURRENT_CONNECT_STATUS) ?
  1861. "Attached" : "Not-Att");
  1862. size -= t;
  1863. next += t;
  1864. tmp_reg = fsl_readl(&dr_regs->usbmode);
  1865. t = scnprintf(next, size,
  1866. "USB Mode Reg: Controller Mode is: %s\n\n", ( {
  1867. char *s;
  1868. switch (tmp_reg & USB_MODE_CTRL_MODE_HOST) {
  1869. case USB_MODE_CTRL_MODE_IDLE:
  1870. s = "Idle"; break;
  1871. case USB_MODE_CTRL_MODE_DEVICE:
  1872. s = "Device Controller"; break;
  1873. case USB_MODE_CTRL_MODE_HOST:
  1874. s = "Host Controller"; break;
  1875. default:
  1876. s = "None"; break;
  1877. }
  1878. s;
  1879. } ));
  1880. size -= t;
  1881. next += t;
  1882. tmp_reg = fsl_readl(&dr_regs->endptsetupstat);
  1883. t = scnprintf(next, size,
  1884. "Endpoint Setup Status Reg: SETUP on ep 0x%x\n\n",
  1885. (tmp_reg & EP_SETUP_STATUS_MASK));
  1886. size -= t;
  1887. next += t;
  1888. for (i = 0; i < udc->max_ep / 2; i++) {
  1889. tmp_reg = fsl_readl(&dr_regs->endptctrl[i]);
  1890. t = scnprintf(next, size, "EP Ctrl Reg [0x%x]: = [0x%x]\n",
  1891. i, tmp_reg);
  1892. size -= t;
  1893. next += t;
  1894. }
  1895. tmp_reg = fsl_readl(&dr_regs->endpointprime);
  1896. t = scnprintf(next, size, "EP Prime Reg = [0x%x]\n\n", tmp_reg);
  1897. size -= t;
  1898. next += t;
  1899. #ifndef CONFIG_ARCH_MXC
  1900. if (udc->pdata->have_sysif_regs) {
  1901. tmp_reg = usb_sys_regs->snoop1;
  1902. t = scnprintf(next, size, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg);
  1903. size -= t;
  1904. next += t;
  1905. tmp_reg = usb_sys_regs->control;
  1906. t = scnprintf(next, size, "General Control Reg : = [0x%x]\n\n",
  1907. tmp_reg);
  1908. size -= t;
  1909. next += t;
  1910. }
  1911. #endif
  1912. /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
  1913. ep = &udc->eps[0];
  1914. t = scnprintf(next, size, "For %s Maxpkt is 0x%x index is 0x%x\n",
  1915. ep->ep.name, ep_maxpacket(ep), ep_index(ep));
  1916. size -= t;
  1917. next += t;
  1918. if (list_empty(&ep->queue)) {
  1919. t = scnprintf(next, size, "its req queue is empty\n\n");
  1920. size -= t;
  1921. next += t;
  1922. } else {
  1923. list_for_each_entry(req, &ep->queue, queue) {
  1924. t = scnprintf(next, size,
  1925. "req %p actual 0x%x length 0x%x buf %p\n",
  1926. &req->req, req->req.actual,
  1927. req->req.length, req->req.buf);
  1928. size -= t;
  1929. next += t;
  1930. }
  1931. }
  1932. /* other gadget->eplist ep */
  1933. list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
  1934. if (ep->ep.desc) {
  1935. t = scnprintf(next, size,
  1936. "\nFor %s Maxpkt is 0x%x "
  1937. "index is 0x%x\n",
  1938. ep->ep.name, ep_maxpacket(ep),
  1939. ep_index(ep));
  1940. size -= t;
  1941. next += t;
  1942. if (list_empty(&ep->queue)) {
  1943. t = scnprintf(next, size,
  1944. "its req queue is empty\n\n");
  1945. size -= t;
  1946. next += t;
  1947. } else {
  1948. list_for_each_entry(req, &ep->queue, queue) {
  1949. t = scnprintf(next, size,
  1950. "req %p actual 0x%x length "
  1951. "0x%x buf %p\n",
  1952. &req->req, req->req.actual,
  1953. req->req.length, req->req.buf);
  1954. size -= t;
  1955. next += t;
  1956. } /* end for each_entry of ep req */
  1957. } /* end for else */
  1958. } /* end for if(ep->queue) */
  1959. } /* end (ep->desc) */
  1960. spin_unlock_irqrestore(&udc->lock, flags);
  1961. *eof = 1;
  1962. return count - size;
  1963. }
  1964. #define create_proc_file() create_proc_read_entry(proc_filename, \
  1965. 0, NULL, fsl_proc_read, NULL)
  1966. #define remove_proc_file() remove_proc_entry(proc_filename, NULL)
  1967. #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
  1968. #define create_proc_file() do {} while (0)
  1969. #define remove_proc_file() do {} while (0)
  1970. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  1971. /*-------------------------------------------------------------------------*/
  1972. /* Release udc structures */
  1973. static void fsl_udc_release(struct device *dev)
  1974. {
  1975. complete(udc_controller->done);
  1976. dma_free_coherent(dev->parent, udc_controller->ep_qh_size,
  1977. udc_controller->ep_qh, udc_controller->ep_qh_dma);
  1978. kfree(udc_controller);
  1979. }
  1980. /******************************************************************
  1981. Internal structure setup functions
  1982. *******************************************************************/
  1983. /*------------------------------------------------------------------
  1984. * init resource for globle controller
  1985. * Return the udc handle on success or NULL on failure
  1986. ------------------------------------------------------------------*/
  1987. static int __init struct_udc_setup(struct fsl_udc *udc,
  1988. struct platform_device *pdev)
  1989. {
  1990. struct fsl_usb2_platform_data *pdata;
  1991. size_t size;
  1992. pdata = pdev->dev.platform_data;
  1993. udc->phy_mode = pdata->phy_mode;
  1994. udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL);
  1995. if (!udc->eps) {
  1996. ERR("malloc fsl_ep failed\n");
  1997. return -1;
  1998. }
  1999. /* initialized QHs, take care of alignment */
  2000. size = udc->max_ep * sizeof(struct ep_queue_head);
  2001. if (size < QH_ALIGNMENT)
  2002. size = QH_ALIGNMENT;
  2003. else if ((size % QH_ALIGNMENT) != 0) {
  2004. size += QH_ALIGNMENT + 1;
  2005. size &= ~(QH_ALIGNMENT - 1);
  2006. }
  2007. udc->ep_qh = dma_alloc_coherent(&pdev->dev, size,
  2008. &udc->ep_qh_dma, GFP_KERNEL);
  2009. if (!udc->ep_qh) {
  2010. ERR("malloc QHs for udc failed\n");
  2011. kfree(udc->eps);
  2012. return -1;
  2013. }
  2014. udc->ep_qh_size = size;
  2015. /* Initialize ep0 status request structure */
  2016. /* FIXME: fsl_alloc_request() ignores ep argument */
  2017. udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
  2018. struct fsl_req, req);
  2019. /* allocate a small amount of memory to get valid address */
  2020. udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
  2021. udc->resume_state = USB_STATE_NOTATTACHED;
  2022. udc->usb_state = USB_STATE_POWERED;
  2023. udc->ep0_dir = 0;
  2024. udc->remote_wakeup = 0; /* default to 0 on reset */
  2025. return 0;
  2026. }
  2027. /*----------------------------------------------------------------
  2028. * Setup the fsl_ep struct for eps
  2029. * Link fsl_ep->ep to gadget->ep_list
  2030. * ep0out is not used so do nothing here
  2031. * ep0in should be taken care
  2032. *--------------------------------------------------------------*/
  2033. static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
  2034. char *name, int link)
  2035. {
  2036. struct fsl_ep *ep = &udc->eps[index];
  2037. ep->udc = udc;
  2038. strcpy(ep->name, name);
  2039. ep->ep.name = ep->name;
  2040. ep->ep.ops = &fsl_ep_ops;
  2041. ep->stopped = 0;
  2042. /* for ep0: maxP defined in desc
  2043. * for other eps, maxP is set by epautoconfig() called by gadget layer
  2044. */
  2045. ep->ep.maxpacket = (unsigned short) ~0;
  2046. /* the queue lists any req for this ep */
  2047. INIT_LIST_HEAD(&ep->queue);
  2048. /* gagdet.ep_list used for ep_autoconfig so no ep0 */
  2049. if (link)
  2050. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  2051. ep->gadget = &udc->gadget;
  2052. ep->qh = &udc->ep_qh[index];
  2053. return 0;
  2054. }
  2055. /* Driver probe function
  2056. * all intialization operations implemented here except enabling usb_intr reg
  2057. * board setup should have been done in the platform code
  2058. */
  2059. static int __init fsl_udc_probe(struct platform_device *pdev)
  2060. {
  2061. struct fsl_usb2_platform_data *pdata;
  2062. struct resource *res;
  2063. int ret = -ENODEV;
  2064. unsigned int i;
  2065. u32 dccparams;
  2066. udc_controller = kzalloc(sizeof(struct fsl_udc), GFP_KERNEL);
  2067. if (udc_controller == NULL) {
  2068. ERR("malloc udc failed\n");
  2069. return -ENOMEM;
  2070. }
  2071. pdata = pdev->dev.platform_data;
  2072. udc_controller->pdata = pdata;
  2073. spin_lock_init(&udc_controller->lock);
  2074. udc_controller->stopped = 1;
  2075. #ifdef CONFIG_USB_OTG
  2076. if (pdata->operating_mode == FSL_USB2_DR_OTG) {
  2077. udc_controller->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
  2078. if (IS_ERR_OR_NULL(udc_controller->transceiver)) {
  2079. ERR("Can't find OTG driver!\n");
  2080. ret = -ENODEV;
  2081. goto err_kfree;
  2082. }
  2083. }
  2084. #endif
  2085. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2086. if (!res) {
  2087. ret = -ENXIO;
  2088. goto err_kfree;
  2089. }
  2090. if (pdata->operating_mode == FSL_USB2_DR_DEVICE) {
  2091. if (!request_mem_region(res->start, resource_size(res),
  2092. driver_name)) {
  2093. ERR("request mem region for %s failed\n", pdev->name);
  2094. ret = -EBUSY;
  2095. goto err_kfree;
  2096. }
  2097. }
  2098. dr_regs = ioremap(res->start, resource_size(res));
  2099. if (!dr_regs) {
  2100. ret = -ENOMEM;
  2101. goto err_release_mem_region;
  2102. }
  2103. pdata->regs = (void *)dr_regs;
  2104. /*
  2105. * do platform specific init: check the clock, grab/config pins, etc.
  2106. */
  2107. if (pdata->init && pdata->init(pdev)) {
  2108. ret = -ENODEV;
  2109. goto err_iounmap_noclk;
  2110. }
  2111. /* Set accessors only after pdata->init() ! */
  2112. fsl_set_accessors(pdata);
  2113. #ifndef CONFIG_ARCH_MXC
  2114. if (pdata->have_sysif_regs)
  2115. usb_sys_regs = (void *)dr_regs + USB_DR_SYS_OFFSET;
  2116. #endif
  2117. /* Initialize USB clocks */
  2118. ret = fsl_udc_clk_init(pdev);
  2119. if (ret < 0)
  2120. goto err_iounmap_noclk;
  2121. /* Read Device Controller Capability Parameters register */
  2122. dccparams = fsl_readl(&dr_regs->dccparams);
  2123. if (!(dccparams & DCCPARAMS_DC)) {
  2124. ERR("This SOC doesn't support device role\n");
  2125. ret = -ENODEV;
  2126. goto err_iounmap;
  2127. }
  2128. /* Get max device endpoints */
  2129. /* DEN is bidirectional ep number, max_ep doubles the number */
  2130. udc_controller->max_ep = (dccparams & DCCPARAMS_DEN_MASK) * 2;
  2131. udc_controller->irq = platform_get_irq(pdev, 0);
  2132. if (!udc_controller->irq) {
  2133. ret = -ENODEV;
  2134. goto err_iounmap;
  2135. }
  2136. ret = request_irq(udc_controller->irq, fsl_udc_irq, IRQF_SHARED,
  2137. driver_name, udc_controller);
  2138. if (ret != 0) {
  2139. ERR("cannot request irq %d err %d\n",
  2140. udc_controller->irq, ret);
  2141. goto err_iounmap;
  2142. }
  2143. /* Initialize the udc structure including QH member and other member */
  2144. if (struct_udc_setup(udc_controller, pdev)) {
  2145. ERR("Can't initialize udc data structure\n");
  2146. ret = -ENOMEM;
  2147. goto err_free_irq;
  2148. }
  2149. if (IS_ERR_OR_NULL(udc_controller->transceiver)) {
  2150. /* initialize usb hw reg except for regs for EP,
  2151. * leave usbintr reg untouched */
  2152. dr_controller_setup(udc_controller);
  2153. }
  2154. ret = fsl_udc_clk_finalize(pdev);
  2155. if (ret)
  2156. goto err_free_irq;
  2157. /* Setup gadget structure */
  2158. udc_controller->gadget.ops = &fsl_gadget_ops;
  2159. udc_controller->gadget.max_speed = USB_SPEED_HIGH;
  2160. udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
  2161. INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
  2162. udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
  2163. udc_controller->gadget.name = driver_name;
  2164. /* Setup gadget.dev and register with kernel */
  2165. dev_set_name(&udc_controller->gadget.dev, "gadget");
  2166. udc_controller->gadget.dev.release = fsl_udc_release;
  2167. udc_controller->gadget.dev.parent = &pdev->dev;
  2168. udc_controller->gadget.dev.of_node = pdev->dev.of_node;
  2169. ret = device_register(&udc_controller->gadget.dev);
  2170. if (ret < 0)
  2171. goto err_free_irq;
  2172. if (!IS_ERR_OR_NULL(udc_controller->transceiver))
  2173. udc_controller->gadget.is_otg = 1;
  2174. /* setup QH and epctrl for ep0 */
  2175. ep0_setup(udc_controller);
  2176. /* setup udc->eps[] for ep0 */
  2177. struct_ep_setup(udc_controller, 0, "ep0", 0);
  2178. /* for ep0: the desc defined here;
  2179. * for other eps, gadget layer called ep_enable with defined desc
  2180. */
  2181. udc_controller->eps[0].ep.desc = &fsl_ep0_desc;
  2182. udc_controller->eps[0].ep.maxpacket = USB_MAX_CTRL_PAYLOAD;
  2183. /* setup the udc->eps[] for non-control endpoints and link
  2184. * to gadget.ep_list */
  2185. for (i = 1; i < (int)(udc_controller->max_ep / 2); i++) {
  2186. char name[14];
  2187. sprintf(name, "ep%dout", i);
  2188. struct_ep_setup(udc_controller, i * 2, name, 1);
  2189. sprintf(name, "ep%din", i);
  2190. struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
  2191. }
  2192. /* use dma_pool for TD management */
  2193. udc_controller->td_pool = dma_pool_create("udc_td", &pdev->dev,
  2194. sizeof(struct ep_td_struct),
  2195. DTD_ALIGNMENT, UDC_DMA_BOUNDARY);
  2196. if (udc_controller->td_pool == NULL) {
  2197. ret = -ENOMEM;
  2198. goto err_unregister;
  2199. }
  2200. ret = usb_add_gadget_udc(&pdev->dev, &udc_controller->gadget);
  2201. if (ret)
  2202. goto err_del_udc;
  2203. create_proc_file();
  2204. return 0;
  2205. err_del_udc:
  2206. dma_pool_destroy(udc_controller->td_pool);
  2207. err_unregister:
  2208. device_unregister(&udc_controller->gadget.dev);
  2209. err_free_irq:
  2210. free_irq(udc_controller->irq, udc_controller);
  2211. err_iounmap:
  2212. if (pdata->exit)
  2213. pdata->exit(pdev);
  2214. fsl_udc_clk_release();
  2215. err_iounmap_noclk:
  2216. iounmap(dr_regs);
  2217. err_release_mem_region:
  2218. if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
  2219. release_mem_region(res->start, resource_size(res));
  2220. err_kfree:
  2221. kfree(udc_controller);
  2222. udc_controller = NULL;
  2223. return ret;
  2224. }
  2225. /* Driver removal function
  2226. * Free resources and finish pending transactions
  2227. */
  2228. static int __exit fsl_udc_remove(struct platform_device *pdev)
  2229. {
  2230. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2231. struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
  2232. DECLARE_COMPLETION(done);
  2233. if (!udc_controller)
  2234. return -ENODEV;
  2235. usb_del_gadget_udc(&udc_controller->gadget);
  2236. udc_controller->done = &done;
  2237. fsl_udc_clk_release();
  2238. /* DR has been stopped in usb_gadget_unregister_driver() */
  2239. remove_proc_file();
  2240. /* Free allocated memory */
  2241. kfree(udc_controller->status_req->req.buf);
  2242. kfree(udc_controller->status_req);
  2243. kfree(udc_controller->eps);
  2244. dma_pool_destroy(udc_controller->td_pool);
  2245. free_irq(udc_controller->irq, udc_controller);
  2246. iounmap(dr_regs);
  2247. if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
  2248. release_mem_region(res->start, resource_size(res));
  2249. device_unregister(&udc_controller->gadget.dev);
  2250. /* free udc --wait for the release() finished */
  2251. wait_for_completion(&done);
  2252. /*
  2253. * do platform specific un-initialization:
  2254. * release iomux pins, etc.
  2255. */
  2256. if (pdata->exit)
  2257. pdata->exit(pdev);
  2258. return 0;
  2259. }
  2260. /*-----------------------------------------------------------------
  2261. * Modify Power management attributes
  2262. * Used by OTG statemachine to disable gadget temporarily
  2263. -----------------------------------------------------------------*/
  2264. static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state)
  2265. {
  2266. dr_controller_stop(udc_controller);
  2267. return 0;
  2268. }
  2269. /*-----------------------------------------------------------------
  2270. * Invoked on USB resume. May be called in_interrupt.
  2271. * Here we start the DR controller and enable the irq
  2272. *-----------------------------------------------------------------*/
  2273. static int fsl_udc_resume(struct platform_device *pdev)
  2274. {
  2275. /* Enable DR irq reg and set controller Run */
  2276. if (udc_controller->stopped) {
  2277. dr_controller_setup(udc_controller);
  2278. dr_controller_run(udc_controller);
  2279. }
  2280. udc_controller->usb_state = USB_STATE_ATTACHED;
  2281. udc_controller->ep0_state = WAIT_FOR_SETUP;
  2282. udc_controller->ep0_dir = 0;
  2283. return 0;
  2284. }
  2285. static int fsl_udc_otg_suspend(struct device *dev, pm_message_t state)
  2286. {
  2287. struct fsl_udc *udc = udc_controller;
  2288. u32 mode, usbcmd;
  2289. mode = fsl_readl(&dr_regs->usbmode) & USB_MODE_CTRL_MODE_MASK;
  2290. pr_debug("%s(): mode 0x%x stopped %d\n", __func__, mode, udc->stopped);
  2291. /*
  2292. * If the controller is already stopped, then this must be a
  2293. * PM suspend. Remember this fact, so that we will leave the
  2294. * controller stopped at PM resume time.
  2295. */
  2296. if (udc->stopped) {
  2297. pr_debug("gadget already stopped, leaving early\n");
  2298. udc->already_stopped = 1;
  2299. return 0;
  2300. }
  2301. if (mode != USB_MODE_CTRL_MODE_DEVICE) {
  2302. pr_debug("gadget not in device mode, leaving early\n");
  2303. return 0;
  2304. }
  2305. /* stop the controller */
  2306. usbcmd = fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP;
  2307. fsl_writel(usbcmd, &dr_regs->usbcmd);
  2308. udc->stopped = 1;
  2309. pr_info("USB Gadget suspended\n");
  2310. return 0;
  2311. }
  2312. static int fsl_udc_otg_resume(struct device *dev)
  2313. {
  2314. pr_debug("%s(): stopped %d already_stopped %d\n", __func__,
  2315. udc_controller->stopped, udc_controller->already_stopped);
  2316. /*
  2317. * If the controller was stopped at suspend time, then
  2318. * don't resume it now.
  2319. */
  2320. if (udc_controller->already_stopped) {
  2321. udc_controller->already_stopped = 0;
  2322. pr_debug("gadget was already stopped, leaving early\n");
  2323. return 0;
  2324. }
  2325. pr_info("USB Gadget resume\n");
  2326. return fsl_udc_resume(NULL);
  2327. }
  2328. /*-------------------------------------------------------------------------
  2329. Register entry point for the peripheral controller driver
  2330. --------------------------------------------------------------------------*/
  2331. static const struct platform_device_id fsl_udc_devtype[] = {
  2332. {
  2333. .name = "imx-udc-mx27",
  2334. }, {
  2335. .name = "imx-udc-mx51",
  2336. }, {
  2337. /* sentinel */
  2338. }
  2339. };
  2340. MODULE_DEVICE_TABLE(platform, fsl_udc_devtype);
  2341. static struct platform_driver udc_driver = {
  2342. .remove = __exit_p(fsl_udc_remove),
  2343. /* Just for FSL i.mx SoC currently */
  2344. .id_table = fsl_udc_devtype,
  2345. /* these suspend and resume are not usb suspend and resume */
  2346. .suspend = fsl_udc_suspend,
  2347. .resume = fsl_udc_resume,
  2348. .driver = {
  2349. .name = (char *)driver_name,
  2350. .owner = THIS_MODULE,
  2351. /* udc suspend/resume called from OTG driver */
  2352. .suspend = fsl_udc_otg_suspend,
  2353. .resume = fsl_udc_otg_resume,
  2354. },
  2355. };
  2356. static int __init udc_init(void)
  2357. {
  2358. printk(KERN_INFO "%s (%s)\n", driver_desc, DRIVER_VERSION);
  2359. return platform_driver_probe(&udc_driver, fsl_udc_probe);
  2360. }
  2361. module_init(udc_init);
  2362. static void __exit udc_exit(void)
  2363. {
  2364. platform_driver_unregister(&udc_driver);
  2365. printk(KERN_WARNING "%s unregistered\n", driver_desc);
  2366. }
  2367. module_exit(udc_exit);
  2368. MODULE_DESCRIPTION(DRIVER_DESC);
  2369. MODULE_AUTHOR(DRIVER_AUTHOR);
  2370. MODULE_LICENSE("GPL");
  2371. MODULE_ALIAS("platform:fsl-usb2-udc");