fsl_udc_core.c 73 KB

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