fsl_udc_core.c 73 KB

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