fsl_udc_core.c 74 KB

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