omap_udc.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * omap_udc.c -- for OMAP full speed udc; most chips support OTG.
  4. *
  5. * Copyright (C) 2004 Texas Instruments, Inc.
  6. * Copyright (C) 2004-2005 David Brownell
  7. *
  8. * OMAP2 & DMA support by Kyungmin Park <[email protected]>
  9. */
  10. #undef DEBUG
  11. #undef VERBOSE
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/ioport.h>
  15. #include <linux/types.h>
  16. #include <linux/errno.h>
  17. #include <linux/delay.h>
  18. #include <linux/slab.h>
  19. #include <linux/timer.h>
  20. #include <linux/list.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/mm.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/usb/ch9.h>
  27. #include <linux/usb/gadget.h>
  28. #include <linux/usb/otg.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/clk.h>
  31. #include <linux/err.h>
  32. #include <linux/prefetch.h>
  33. #include <linux/io.h>
  34. #include <asm/byteorder.h>
  35. #include <asm/irq.h>
  36. #include <asm/unaligned.h>
  37. #include <asm/mach-types.h>
  38. #include <linux/omap-dma.h>
  39. #include <linux/platform_data/usb-omap1.h>
  40. #include <linux/soc/ti/omap1-usb.h>
  41. #include <linux/soc/ti/omap1-soc.h>
  42. #include <linux/soc/ti/omap1-io.h>
  43. #include "omap_udc.h"
  44. #undef USB_TRACE
  45. /* bulk DMA seems to be behaving for both IN and OUT */
  46. #define USE_DMA
  47. /* ISO too */
  48. #define USE_ISO
  49. #define DRIVER_DESC "OMAP UDC driver"
  50. #define DRIVER_VERSION "4 October 2004"
  51. #define OMAP_DMA_USB_W2FC_TX0 29
  52. #define OMAP_DMA_USB_W2FC_RX0 26
  53. /*
  54. * The OMAP UDC needs _very_ early endpoint setup: before enabling the
  55. * D+ pullup to allow enumeration. That's too early for the gadget
  56. * framework to use from usb_endpoint_enable(), which happens after
  57. * enumeration as part of activating an interface. (But if we add an
  58. * optional new "UDC not yet running" state to the gadget driver model,
  59. * even just during driver binding, the endpoint autoconfig logic is the
  60. * natural spot to manufacture new endpoints.)
  61. *
  62. * So instead of using endpoint enable calls to control the hardware setup,
  63. * this driver defines a "fifo mode" parameter. It's used during driver
  64. * initialization to choose among a set of pre-defined endpoint configs.
  65. * See omap_udc_setup() for available modes, or to add others. That code
  66. * lives in an init section, so use this driver as a module if you need
  67. * to change the fifo mode after the kernel boots.
  68. *
  69. * Gadget drivers normally ignore endpoints they don't care about, and
  70. * won't include them in configuration descriptors. That means only
  71. * misbehaving hosts would even notice they exist.
  72. */
  73. #ifdef USE_ISO
  74. static unsigned fifo_mode = 3;
  75. #else
  76. static unsigned fifo_mode;
  77. #endif
  78. /* "modprobe omap_udc fifo_mode=42", or else as a kernel
  79. * boot parameter "omap_udc:fifo_mode=42"
  80. */
  81. module_param(fifo_mode, uint, 0);
  82. MODULE_PARM_DESC(fifo_mode, "endpoint configuration");
  83. #ifdef USE_DMA
  84. static bool use_dma = 1;
  85. /* "modprobe omap_udc use_dma=y", or else as a kernel
  86. * boot parameter "omap_udc:use_dma=y"
  87. */
  88. module_param(use_dma, bool, 0);
  89. MODULE_PARM_DESC(use_dma, "enable/disable DMA");
  90. #else /* !USE_DMA */
  91. /* save a bit of code */
  92. #define use_dma 0
  93. #endif /* !USE_DMA */
  94. static const char driver_name[] = "omap_udc";
  95. static const char driver_desc[] = DRIVER_DESC;
  96. /*-------------------------------------------------------------------------*/
  97. /* there's a notion of "current endpoint" for modifying endpoint
  98. * state, and PIO access to its FIFO.
  99. */
  100. static void use_ep(struct omap_ep *ep, u16 select)
  101. {
  102. u16 num = ep->bEndpointAddress & 0x0f;
  103. if (ep->bEndpointAddress & USB_DIR_IN)
  104. num |= UDC_EP_DIR;
  105. omap_writew(num | select, UDC_EP_NUM);
  106. /* when select, MUST deselect later !! */
  107. }
  108. static inline void deselect_ep(void)
  109. {
  110. u16 w;
  111. w = omap_readw(UDC_EP_NUM);
  112. w &= ~UDC_EP_SEL;
  113. omap_writew(w, UDC_EP_NUM);
  114. /* 6 wait states before TX will happen */
  115. }
  116. static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
  117. /*-------------------------------------------------------------------------*/
  118. static int omap_ep_enable(struct usb_ep *_ep,
  119. const struct usb_endpoint_descriptor *desc)
  120. {
  121. struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
  122. struct omap_udc *udc;
  123. unsigned long flags;
  124. u16 maxp;
  125. /* catch various bogus parameters */
  126. if (!_ep || !desc
  127. || desc->bDescriptorType != USB_DT_ENDPOINT
  128. || ep->bEndpointAddress != desc->bEndpointAddress
  129. || ep->maxpacket < usb_endpoint_maxp(desc)) {
  130. DBG("%s, bad ep or descriptor\n", __func__);
  131. return -EINVAL;
  132. }
  133. maxp = usb_endpoint_maxp(desc);
  134. if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
  135. && maxp != ep->maxpacket)
  136. || usb_endpoint_maxp(desc) > ep->maxpacket
  137. || !desc->wMaxPacketSize) {
  138. DBG("%s, bad %s maxpacket\n", __func__, _ep->name);
  139. return -ERANGE;
  140. }
  141. #ifdef USE_ISO
  142. if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
  143. && desc->bInterval != 1)) {
  144. /* hardware wants period = 1; USB allows 2^(Interval-1) */
  145. DBG("%s, unsupported ISO period %dms\n", _ep->name,
  146. 1 << (desc->bInterval - 1));
  147. return -EDOM;
  148. }
  149. #else
  150. if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  151. DBG("%s, ISO nyet\n", _ep->name);
  152. return -EDOM;
  153. }
  154. #endif
  155. /* xfer types must match, except that interrupt ~= bulk */
  156. if (ep->bmAttributes != desc->bmAttributes
  157. && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
  158. && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
  159. DBG("%s, %s type mismatch\n", __func__, _ep->name);
  160. return -EINVAL;
  161. }
  162. udc = ep->udc;
  163. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  164. DBG("%s, bogus device state\n", __func__);
  165. return -ESHUTDOWN;
  166. }
  167. spin_lock_irqsave(&udc->lock, flags);
  168. ep->ep.desc = desc;
  169. ep->irqs = 0;
  170. ep->stopped = 0;
  171. ep->ep.maxpacket = maxp;
  172. /* set endpoint to initial state */
  173. ep->dma_channel = 0;
  174. ep->has_dma = 0;
  175. ep->lch = -1;
  176. use_ep(ep, UDC_EP_SEL);
  177. omap_writew(udc->clr_halt, UDC_CTRL);
  178. ep->ackwait = 0;
  179. deselect_ep();
  180. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
  181. list_add(&ep->iso, &udc->iso);
  182. /* maybe assign a DMA channel to this endpoint */
  183. if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
  184. /* FIXME ISO can dma, but prefers first channel */
  185. dma_channel_claim(ep, 0);
  186. /* PIO OUT may RX packets */
  187. if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
  188. && !ep->has_dma
  189. && !(ep->bEndpointAddress & USB_DIR_IN)) {
  190. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  191. ep->ackwait = 1 + ep->double_buf;
  192. }
  193. spin_unlock_irqrestore(&udc->lock, flags);
  194. VDBG("%s enabled\n", _ep->name);
  195. return 0;
  196. }
  197. static void nuke(struct omap_ep *, int status);
  198. static int omap_ep_disable(struct usb_ep *_ep)
  199. {
  200. struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
  201. unsigned long flags;
  202. if (!_ep || !ep->ep.desc) {
  203. DBG("%s, %s not enabled\n", __func__,
  204. _ep ? ep->ep.name : NULL);
  205. return -EINVAL;
  206. }
  207. spin_lock_irqsave(&ep->udc->lock, flags);
  208. ep->ep.desc = NULL;
  209. nuke(ep, -ESHUTDOWN);
  210. ep->ep.maxpacket = ep->maxpacket;
  211. ep->has_dma = 0;
  212. omap_writew(UDC_SET_HALT, UDC_CTRL);
  213. list_del_init(&ep->iso);
  214. del_timer(&ep->timer);
  215. spin_unlock_irqrestore(&ep->udc->lock, flags);
  216. VDBG("%s disabled\n", _ep->name);
  217. return 0;
  218. }
  219. /*-------------------------------------------------------------------------*/
  220. static struct usb_request *
  221. omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  222. {
  223. struct omap_req *req;
  224. req = kzalloc(sizeof(*req), gfp_flags);
  225. if (!req)
  226. return NULL;
  227. INIT_LIST_HEAD(&req->queue);
  228. return &req->req;
  229. }
  230. static void
  231. omap_free_request(struct usb_ep *ep, struct usb_request *_req)
  232. {
  233. struct omap_req *req = container_of(_req, struct omap_req, req);
  234. kfree(req);
  235. }
  236. /*-------------------------------------------------------------------------*/
  237. static void
  238. done(struct omap_ep *ep, struct omap_req *req, int status)
  239. {
  240. struct omap_udc *udc = ep->udc;
  241. unsigned stopped = ep->stopped;
  242. list_del_init(&req->queue);
  243. if (req->req.status == -EINPROGRESS)
  244. req->req.status = status;
  245. else
  246. status = req->req.status;
  247. if (use_dma && ep->has_dma)
  248. usb_gadget_unmap_request(&udc->gadget, &req->req,
  249. (ep->bEndpointAddress & USB_DIR_IN));
  250. #ifndef USB_TRACE
  251. if (status && status != -ESHUTDOWN)
  252. #endif
  253. VDBG("complete %s req %p stat %d len %u/%u\n",
  254. ep->ep.name, &req->req, status,
  255. req->req.actual, req->req.length);
  256. /* don't modify queue heads during completion callback */
  257. ep->stopped = 1;
  258. spin_unlock(&ep->udc->lock);
  259. usb_gadget_giveback_request(&ep->ep, &req->req);
  260. spin_lock(&ep->udc->lock);
  261. ep->stopped = stopped;
  262. }
  263. /*-------------------------------------------------------------------------*/
  264. #define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
  265. #define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
  266. #define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
  267. #define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
  268. static inline int
  269. write_packet(u8 *buf, struct omap_req *req, unsigned max)
  270. {
  271. unsigned len;
  272. u16 *wp;
  273. len = min(req->req.length - req->req.actual, max);
  274. req->req.actual += len;
  275. max = len;
  276. if (likely((((int)buf) & 1) == 0)) {
  277. wp = (u16 *)buf;
  278. while (max >= 2) {
  279. omap_writew(*wp++, UDC_DATA);
  280. max -= 2;
  281. }
  282. buf = (u8 *)wp;
  283. }
  284. while (max--)
  285. omap_writeb(*buf++, UDC_DATA);
  286. return len;
  287. }
  288. /* FIXME change r/w fifo calling convention */
  289. /* return: 0 = still running, 1 = completed, negative = errno */
  290. static int write_fifo(struct omap_ep *ep, struct omap_req *req)
  291. {
  292. u8 *buf;
  293. unsigned count;
  294. int is_last;
  295. u16 ep_stat;
  296. buf = req->req.buf + req->req.actual;
  297. prefetch(buf);
  298. /* PIO-IN isn't double buffered except for iso */
  299. ep_stat = omap_readw(UDC_STAT_FLG);
  300. if (ep_stat & UDC_FIFO_UNWRITABLE)
  301. return 0;
  302. count = ep->ep.maxpacket;
  303. count = write_packet(buf, req, count);
  304. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  305. ep->ackwait = 1;
  306. /* last packet is often short (sometimes a zlp) */
  307. if (count != ep->ep.maxpacket)
  308. is_last = 1;
  309. else if (req->req.length == req->req.actual
  310. && !req->req.zero)
  311. is_last = 1;
  312. else
  313. is_last = 0;
  314. /* NOTE: requests complete when all IN data is in a
  315. * FIFO (or sometimes later, if a zlp was needed).
  316. * Use usb_ep_fifo_status() where needed.
  317. */
  318. if (is_last)
  319. done(ep, req, 0);
  320. return is_last;
  321. }
  322. static inline int
  323. read_packet(u8 *buf, struct omap_req *req, unsigned avail)
  324. {
  325. unsigned len;
  326. u16 *wp;
  327. len = min(req->req.length - req->req.actual, avail);
  328. req->req.actual += len;
  329. avail = len;
  330. if (likely((((int)buf) & 1) == 0)) {
  331. wp = (u16 *)buf;
  332. while (avail >= 2) {
  333. *wp++ = omap_readw(UDC_DATA);
  334. avail -= 2;
  335. }
  336. buf = (u8 *)wp;
  337. }
  338. while (avail--)
  339. *buf++ = omap_readb(UDC_DATA);
  340. return len;
  341. }
  342. /* return: 0 = still running, 1 = queue empty, negative = errno */
  343. static int read_fifo(struct omap_ep *ep, struct omap_req *req)
  344. {
  345. u8 *buf;
  346. unsigned count, avail;
  347. int is_last;
  348. buf = req->req.buf + req->req.actual;
  349. prefetchw(buf);
  350. for (;;) {
  351. u16 ep_stat = omap_readw(UDC_STAT_FLG);
  352. is_last = 0;
  353. if (ep_stat & FIFO_EMPTY) {
  354. if (!ep->double_buf)
  355. break;
  356. ep->fnf = 1;
  357. }
  358. if (ep_stat & UDC_EP_HALTED)
  359. break;
  360. if (ep_stat & UDC_FIFO_FULL)
  361. avail = ep->ep.maxpacket;
  362. else {
  363. avail = omap_readw(UDC_RXFSTAT);
  364. ep->fnf = ep->double_buf;
  365. }
  366. count = read_packet(buf, req, avail);
  367. /* partial packet reads may not be errors */
  368. if (count < ep->ep.maxpacket) {
  369. is_last = 1;
  370. /* overflowed this request? flush extra data */
  371. if (count != avail) {
  372. req->req.status = -EOVERFLOW;
  373. avail -= count;
  374. while (avail--)
  375. omap_readw(UDC_DATA);
  376. }
  377. } else if (req->req.length == req->req.actual)
  378. is_last = 1;
  379. else
  380. is_last = 0;
  381. if (!ep->bEndpointAddress)
  382. break;
  383. if (is_last)
  384. done(ep, req, 0);
  385. break;
  386. }
  387. return is_last;
  388. }
  389. /*-------------------------------------------------------------------------*/
  390. static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
  391. {
  392. dma_addr_t end;
  393. /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
  394. * the last transfer's bytecount by more than a FIFO's worth.
  395. */
  396. if (cpu_is_omap15xx())
  397. return 0;
  398. end = omap_get_dma_src_pos(ep->lch);
  399. if (end == ep->dma_counter)
  400. return 0;
  401. end |= start & (0xffff << 16);
  402. if (end < start)
  403. end += 0x10000;
  404. return end - start;
  405. }
  406. static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
  407. {
  408. dma_addr_t end;
  409. end = omap_get_dma_dst_pos(ep->lch);
  410. if (end == ep->dma_counter)
  411. return 0;
  412. end |= start & (0xffff << 16);
  413. if (cpu_is_omap15xx())
  414. end++;
  415. if (end < start)
  416. end += 0x10000;
  417. return end - start;
  418. }
  419. /* Each USB transfer request using DMA maps to one or more DMA transfers.
  420. * When DMA completion isn't request completion, the UDC continues with
  421. * the next DMA transfer for that USB transfer.
  422. */
  423. static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
  424. {
  425. u16 txdma_ctrl, w;
  426. unsigned length = req->req.length - req->req.actual;
  427. const int sync_mode = cpu_is_omap15xx()
  428. ? OMAP_DMA_SYNC_FRAME
  429. : OMAP_DMA_SYNC_ELEMENT;
  430. int dma_trigger = 0;
  431. /* measure length in either bytes or packets */
  432. if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
  433. || (cpu_is_omap15xx() && length < ep->maxpacket)) {
  434. txdma_ctrl = UDC_TXN_EOT | length;
  435. omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
  436. length, 1, sync_mode, dma_trigger, 0);
  437. } else {
  438. length = min(length / ep->maxpacket,
  439. (unsigned) UDC_TXN_TSC + 1);
  440. txdma_ctrl = length;
  441. omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
  442. ep->ep.maxpacket >> 1, length, sync_mode,
  443. dma_trigger, 0);
  444. length *= ep->maxpacket;
  445. }
  446. omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
  447. OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
  448. 0, 0);
  449. omap_start_dma(ep->lch);
  450. ep->dma_counter = omap_get_dma_src_pos(ep->lch);
  451. w = omap_readw(UDC_DMA_IRQ_EN);
  452. w |= UDC_TX_DONE_IE(ep->dma_channel);
  453. omap_writew(w, UDC_DMA_IRQ_EN);
  454. omap_writew(UDC_TXN_START | txdma_ctrl, UDC_TXDMA(ep->dma_channel));
  455. req->dma_bytes = length;
  456. }
  457. static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
  458. {
  459. u16 w;
  460. if (status == 0) {
  461. req->req.actual += req->dma_bytes;
  462. /* return if this request needs to send data or zlp */
  463. if (req->req.actual < req->req.length)
  464. return;
  465. if (req->req.zero
  466. && req->dma_bytes != 0
  467. && (req->req.actual % ep->maxpacket) == 0)
  468. return;
  469. } else
  470. req->req.actual += dma_src_len(ep, req->req.dma
  471. + req->req.actual);
  472. /* tx completion */
  473. omap_stop_dma(ep->lch);
  474. w = omap_readw(UDC_DMA_IRQ_EN);
  475. w &= ~UDC_TX_DONE_IE(ep->dma_channel);
  476. omap_writew(w, UDC_DMA_IRQ_EN);
  477. done(ep, req, status);
  478. }
  479. static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
  480. {
  481. unsigned packets = req->req.length - req->req.actual;
  482. int dma_trigger = 0;
  483. u16 w;
  484. /* set up this DMA transfer, enable the fifo, start */
  485. packets /= ep->ep.maxpacket;
  486. packets = min(packets, (unsigned)UDC_RXN_TC + 1);
  487. req->dma_bytes = packets * ep->ep.maxpacket;
  488. omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
  489. ep->ep.maxpacket >> 1, packets,
  490. OMAP_DMA_SYNC_ELEMENT,
  491. dma_trigger, 0);
  492. omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
  493. OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
  494. 0, 0);
  495. ep->dma_counter = omap_get_dma_dst_pos(ep->lch);
  496. omap_writew(UDC_RXN_STOP | (packets - 1), UDC_RXDMA(ep->dma_channel));
  497. w = omap_readw(UDC_DMA_IRQ_EN);
  498. w |= UDC_RX_EOT_IE(ep->dma_channel);
  499. omap_writew(w, UDC_DMA_IRQ_EN);
  500. omap_writew(ep->bEndpointAddress & 0xf, UDC_EP_NUM);
  501. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  502. omap_start_dma(ep->lch);
  503. }
  504. static void
  505. finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
  506. {
  507. u16 count, w;
  508. if (status == 0)
  509. ep->dma_counter = (u16) (req->req.dma + req->req.actual);
  510. count = dma_dest_len(ep, req->req.dma + req->req.actual);
  511. count += req->req.actual;
  512. if (one)
  513. count--;
  514. if (count <= req->req.length)
  515. req->req.actual = count;
  516. if (count != req->dma_bytes || status)
  517. omap_stop_dma(ep->lch);
  518. /* if this wasn't short, request may need another transfer */
  519. else if (req->req.actual < req->req.length)
  520. return;
  521. /* rx completion */
  522. w = omap_readw(UDC_DMA_IRQ_EN);
  523. w &= ~UDC_RX_EOT_IE(ep->dma_channel);
  524. omap_writew(w, UDC_DMA_IRQ_EN);
  525. done(ep, req, status);
  526. }
  527. static void dma_irq(struct omap_udc *udc, u16 irq_src)
  528. {
  529. u16 dman_stat = omap_readw(UDC_DMAN_STAT);
  530. struct omap_ep *ep;
  531. struct omap_req *req;
  532. /* IN dma: tx to host */
  533. if (irq_src & UDC_TXN_DONE) {
  534. ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
  535. ep->irqs++;
  536. /* can see TXN_DONE after dma abort */
  537. if (!list_empty(&ep->queue)) {
  538. req = container_of(ep->queue.next,
  539. struct omap_req, queue);
  540. finish_in_dma(ep, req, 0);
  541. }
  542. omap_writew(UDC_TXN_DONE, UDC_IRQ_SRC);
  543. if (!list_empty(&ep->queue)) {
  544. req = container_of(ep->queue.next,
  545. struct omap_req, queue);
  546. next_in_dma(ep, req);
  547. }
  548. }
  549. /* OUT dma: rx from host */
  550. if (irq_src & UDC_RXN_EOT) {
  551. ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
  552. ep->irqs++;
  553. /* can see RXN_EOT after dma abort */
  554. if (!list_empty(&ep->queue)) {
  555. req = container_of(ep->queue.next,
  556. struct omap_req, queue);
  557. finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
  558. }
  559. omap_writew(UDC_RXN_EOT, UDC_IRQ_SRC);
  560. if (!list_empty(&ep->queue)) {
  561. req = container_of(ep->queue.next,
  562. struct omap_req, queue);
  563. next_out_dma(ep, req);
  564. }
  565. }
  566. if (irq_src & UDC_RXN_CNT) {
  567. ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
  568. ep->irqs++;
  569. /* omap15xx does this unasked... */
  570. VDBG("%s, RX_CNT irq?\n", ep->ep.name);
  571. omap_writew(UDC_RXN_CNT, UDC_IRQ_SRC);
  572. }
  573. }
  574. static void dma_error(int lch, u16 ch_status, void *data)
  575. {
  576. struct omap_ep *ep = data;
  577. /* if ch_status & OMAP_DMA_DROP_IRQ ... */
  578. /* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
  579. ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
  580. /* complete current transfer ... */
  581. }
  582. static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
  583. {
  584. u16 reg;
  585. int status, restart, is_in;
  586. int dma_channel;
  587. is_in = ep->bEndpointAddress & USB_DIR_IN;
  588. if (is_in)
  589. reg = omap_readw(UDC_TXDMA_CFG);
  590. else
  591. reg = omap_readw(UDC_RXDMA_CFG);
  592. reg |= UDC_DMA_REQ; /* "pulse" activated */
  593. ep->dma_channel = 0;
  594. ep->lch = -1;
  595. if (channel == 0 || channel > 3) {
  596. if ((reg & 0x0f00) == 0)
  597. channel = 3;
  598. else if ((reg & 0x00f0) == 0)
  599. channel = 2;
  600. else if ((reg & 0x000f) == 0) /* preferred for ISO */
  601. channel = 1;
  602. else {
  603. status = -EMLINK;
  604. goto just_restart;
  605. }
  606. }
  607. reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
  608. ep->dma_channel = channel;
  609. if (is_in) {
  610. dma_channel = OMAP_DMA_USB_W2FC_TX0 - 1 + channel;
  611. status = omap_request_dma(dma_channel,
  612. ep->ep.name, dma_error, ep, &ep->lch);
  613. if (status == 0) {
  614. omap_writew(reg, UDC_TXDMA_CFG);
  615. /* EMIFF or SDRC */
  616. omap_set_dma_src_burst_mode(ep->lch,
  617. OMAP_DMA_DATA_BURST_4);
  618. omap_set_dma_src_data_pack(ep->lch, 1);
  619. /* TIPB */
  620. omap_set_dma_dest_params(ep->lch,
  621. OMAP_DMA_PORT_TIPB,
  622. OMAP_DMA_AMODE_CONSTANT,
  623. UDC_DATA_DMA,
  624. 0, 0);
  625. }
  626. } else {
  627. dma_channel = OMAP_DMA_USB_W2FC_RX0 - 1 + channel;
  628. status = omap_request_dma(dma_channel,
  629. ep->ep.name, dma_error, ep, &ep->lch);
  630. if (status == 0) {
  631. omap_writew(reg, UDC_RXDMA_CFG);
  632. /* TIPB */
  633. omap_set_dma_src_params(ep->lch,
  634. OMAP_DMA_PORT_TIPB,
  635. OMAP_DMA_AMODE_CONSTANT,
  636. UDC_DATA_DMA,
  637. 0, 0);
  638. /* EMIFF or SDRC */
  639. omap_set_dma_dest_burst_mode(ep->lch,
  640. OMAP_DMA_DATA_BURST_4);
  641. omap_set_dma_dest_data_pack(ep->lch, 1);
  642. }
  643. }
  644. if (status)
  645. ep->dma_channel = 0;
  646. else {
  647. ep->has_dma = 1;
  648. omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
  649. /* channel type P: hw synch (fifo) */
  650. if (!cpu_is_omap15xx())
  651. omap_set_dma_channel_mode(ep->lch, OMAP_DMA_LCH_P);
  652. }
  653. just_restart:
  654. /* restart any queue, even if the claim failed */
  655. restart = !ep->stopped && !list_empty(&ep->queue);
  656. if (status)
  657. DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
  658. restart ? " (restart)" : "");
  659. else
  660. DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
  661. is_in ? 't' : 'r',
  662. ep->dma_channel - 1, ep->lch,
  663. restart ? " (restart)" : "");
  664. if (restart) {
  665. struct omap_req *req;
  666. req = container_of(ep->queue.next, struct omap_req, queue);
  667. if (ep->has_dma)
  668. (is_in ? next_in_dma : next_out_dma)(ep, req);
  669. else {
  670. use_ep(ep, UDC_EP_SEL);
  671. (is_in ? write_fifo : read_fifo)(ep, req);
  672. deselect_ep();
  673. if (!is_in) {
  674. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  675. ep->ackwait = 1 + ep->double_buf;
  676. }
  677. /* IN: 6 wait states before it'll tx */
  678. }
  679. }
  680. }
  681. static void dma_channel_release(struct omap_ep *ep)
  682. {
  683. int shift = 4 * (ep->dma_channel - 1);
  684. u16 mask = 0x0f << shift;
  685. struct omap_req *req;
  686. int active;
  687. /* abort any active usb transfer request */
  688. if (!list_empty(&ep->queue))
  689. req = container_of(ep->queue.next, struct omap_req, queue);
  690. else
  691. req = NULL;
  692. active = omap_get_dma_active_status(ep->lch);
  693. DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
  694. active ? "active" : "idle",
  695. (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
  696. ep->dma_channel - 1, req);
  697. /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
  698. * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
  699. */
  700. /* wait till current packet DMA finishes, and fifo empties */
  701. if (ep->bEndpointAddress & USB_DIR_IN) {
  702. omap_writew((omap_readw(UDC_TXDMA_CFG) & ~mask) | UDC_DMA_REQ,
  703. UDC_TXDMA_CFG);
  704. if (req) {
  705. finish_in_dma(ep, req, -ECONNRESET);
  706. /* clear FIFO; hosts probably won't empty it */
  707. use_ep(ep, UDC_EP_SEL);
  708. omap_writew(UDC_CLR_EP, UDC_CTRL);
  709. deselect_ep();
  710. }
  711. while (omap_readw(UDC_TXDMA_CFG) & mask)
  712. udelay(10);
  713. } else {
  714. omap_writew((omap_readw(UDC_RXDMA_CFG) & ~mask) | UDC_DMA_REQ,
  715. UDC_RXDMA_CFG);
  716. /* dma empties the fifo */
  717. while (omap_readw(UDC_RXDMA_CFG) & mask)
  718. udelay(10);
  719. if (req)
  720. finish_out_dma(ep, req, -ECONNRESET, 0);
  721. }
  722. omap_free_dma(ep->lch);
  723. ep->dma_channel = 0;
  724. ep->lch = -1;
  725. /* has_dma still set, till endpoint is fully quiesced */
  726. }
  727. /*-------------------------------------------------------------------------*/
  728. static int
  729. omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  730. {
  731. struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
  732. struct omap_req *req = container_of(_req, struct omap_req, req);
  733. struct omap_udc *udc;
  734. unsigned long flags;
  735. int is_iso = 0;
  736. /* catch various bogus parameters */
  737. if (!_req || !req->req.complete || !req->req.buf
  738. || !list_empty(&req->queue)) {
  739. DBG("%s, bad params\n", __func__);
  740. return -EINVAL;
  741. }
  742. if (!_ep || (!ep->ep.desc && ep->bEndpointAddress)) {
  743. DBG("%s, bad ep\n", __func__);
  744. return -EINVAL;
  745. }
  746. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  747. if (req->req.length > ep->ep.maxpacket)
  748. return -EMSGSIZE;
  749. is_iso = 1;
  750. }
  751. /* this isn't bogus, but OMAP DMA isn't the only hardware to
  752. * have a hard time with partial packet reads... reject it.
  753. */
  754. if (use_dma
  755. && ep->has_dma
  756. && ep->bEndpointAddress != 0
  757. && (ep->bEndpointAddress & USB_DIR_IN) == 0
  758. && (req->req.length % ep->ep.maxpacket) != 0) {
  759. DBG("%s, no partial packet OUT reads\n", __func__);
  760. return -EMSGSIZE;
  761. }
  762. udc = ep->udc;
  763. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
  764. return -ESHUTDOWN;
  765. if (use_dma && ep->has_dma)
  766. usb_gadget_map_request(&udc->gadget, &req->req,
  767. (ep->bEndpointAddress & USB_DIR_IN));
  768. VDBG("%s queue req %p, len %d buf %p\n",
  769. ep->ep.name, _req, _req->length, _req->buf);
  770. spin_lock_irqsave(&udc->lock, flags);
  771. req->req.status = -EINPROGRESS;
  772. req->req.actual = 0;
  773. /* maybe kickstart non-iso i/o queues */
  774. if (is_iso) {
  775. u16 w;
  776. w = omap_readw(UDC_IRQ_EN);
  777. w |= UDC_SOF_IE;
  778. omap_writew(w, UDC_IRQ_EN);
  779. } else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
  780. int is_in;
  781. if (ep->bEndpointAddress == 0) {
  782. if (!udc->ep0_pending || !list_empty(&ep->queue)) {
  783. spin_unlock_irqrestore(&udc->lock, flags);
  784. return -EL2HLT;
  785. }
  786. /* empty DATA stage? */
  787. is_in = udc->ep0_in;
  788. if (!req->req.length) {
  789. /* chip became CONFIGURED or ADDRESSED
  790. * earlier; drivers may already have queued
  791. * requests to non-control endpoints
  792. */
  793. if (udc->ep0_set_config) {
  794. u16 irq_en = omap_readw(UDC_IRQ_EN);
  795. irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
  796. if (!udc->ep0_reset_config)
  797. irq_en |= UDC_EPN_RX_IE
  798. | UDC_EPN_TX_IE;
  799. omap_writew(irq_en, UDC_IRQ_EN);
  800. }
  801. /* STATUS for zero length DATA stages is
  802. * always an IN ... even for IN transfers,
  803. * a weird case which seem to stall OMAP.
  804. */
  805. omap_writew(UDC_EP_SEL | UDC_EP_DIR,
  806. UDC_EP_NUM);
  807. omap_writew(UDC_CLR_EP, UDC_CTRL);
  808. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  809. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  810. /* cleanup */
  811. udc->ep0_pending = 0;
  812. done(ep, req, 0);
  813. req = NULL;
  814. /* non-empty DATA stage */
  815. } else if (is_in) {
  816. omap_writew(UDC_EP_SEL | UDC_EP_DIR,
  817. UDC_EP_NUM);
  818. } else {
  819. if (udc->ep0_setup)
  820. goto irq_wait;
  821. omap_writew(UDC_EP_SEL, UDC_EP_NUM);
  822. }
  823. } else {
  824. is_in = ep->bEndpointAddress & USB_DIR_IN;
  825. if (!ep->has_dma)
  826. use_ep(ep, UDC_EP_SEL);
  827. /* if ISO: SOF IRQs must be enabled/disabled! */
  828. }
  829. if (ep->has_dma)
  830. (is_in ? next_in_dma : next_out_dma)(ep, req);
  831. else if (req) {
  832. if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
  833. req = NULL;
  834. deselect_ep();
  835. if (!is_in) {
  836. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  837. ep->ackwait = 1 + ep->double_buf;
  838. }
  839. /* IN: 6 wait states before it'll tx */
  840. }
  841. }
  842. irq_wait:
  843. /* irq handler advances the queue */
  844. if (req != NULL)
  845. list_add_tail(&req->queue, &ep->queue);
  846. spin_unlock_irqrestore(&udc->lock, flags);
  847. return 0;
  848. }
  849. static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  850. {
  851. struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
  852. struct omap_req *req = NULL, *iter;
  853. unsigned long flags;
  854. if (!_ep || !_req)
  855. return -EINVAL;
  856. spin_lock_irqsave(&ep->udc->lock, flags);
  857. /* make sure it's actually queued on this endpoint */
  858. list_for_each_entry(iter, &ep->queue, queue) {
  859. if (&iter->req != _req)
  860. continue;
  861. req = iter;
  862. break;
  863. }
  864. if (!req) {
  865. spin_unlock_irqrestore(&ep->udc->lock, flags);
  866. return -EINVAL;
  867. }
  868. if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
  869. int channel = ep->dma_channel;
  870. /* releasing the channel cancels the request,
  871. * reclaiming the channel restarts the queue
  872. */
  873. dma_channel_release(ep);
  874. dma_channel_claim(ep, channel);
  875. } else
  876. done(ep, req, -ECONNRESET);
  877. spin_unlock_irqrestore(&ep->udc->lock, flags);
  878. return 0;
  879. }
  880. /*-------------------------------------------------------------------------*/
  881. static int omap_ep_set_halt(struct usb_ep *_ep, int value)
  882. {
  883. struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
  884. unsigned long flags;
  885. int status = -EOPNOTSUPP;
  886. spin_lock_irqsave(&ep->udc->lock, flags);
  887. /* just use protocol stalls for ep0; real halts are annoying */
  888. if (ep->bEndpointAddress == 0) {
  889. if (!ep->udc->ep0_pending)
  890. status = -EINVAL;
  891. else if (value) {
  892. if (ep->udc->ep0_set_config) {
  893. WARNING("error changing config?\n");
  894. omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
  895. }
  896. omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
  897. ep->udc->ep0_pending = 0;
  898. status = 0;
  899. } else /* NOP */
  900. status = 0;
  901. /* otherwise, all active non-ISO endpoints can halt */
  902. } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->ep.desc) {
  903. /* IN endpoints must already be idle */
  904. if ((ep->bEndpointAddress & USB_DIR_IN)
  905. && !list_empty(&ep->queue)) {
  906. status = -EAGAIN;
  907. goto done;
  908. }
  909. if (value) {
  910. int channel;
  911. if (use_dma && ep->dma_channel
  912. && !list_empty(&ep->queue)) {
  913. channel = ep->dma_channel;
  914. dma_channel_release(ep);
  915. } else
  916. channel = 0;
  917. use_ep(ep, UDC_EP_SEL);
  918. if (omap_readw(UDC_STAT_FLG) & UDC_NON_ISO_FIFO_EMPTY) {
  919. omap_writew(UDC_SET_HALT, UDC_CTRL);
  920. status = 0;
  921. } else
  922. status = -EAGAIN;
  923. deselect_ep();
  924. if (channel)
  925. dma_channel_claim(ep, channel);
  926. } else {
  927. use_ep(ep, 0);
  928. omap_writew(ep->udc->clr_halt, UDC_CTRL);
  929. ep->ackwait = 0;
  930. if (!(ep->bEndpointAddress & USB_DIR_IN)) {
  931. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  932. ep->ackwait = 1 + ep->double_buf;
  933. }
  934. }
  935. }
  936. done:
  937. VDBG("%s %s halt stat %d\n", ep->ep.name,
  938. value ? "set" : "clear", status);
  939. spin_unlock_irqrestore(&ep->udc->lock, flags);
  940. return status;
  941. }
  942. static const struct usb_ep_ops omap_ep_ops = {
  943. .enable = omap_ep_enable,
  944. .disable = omap_ep_disable,
  945. .alloc_request = omap_alloc_request,
  946. .free_request = omap_free_request,
  947. .queue = omap_ep_queue,
  948. .dequeue = omap_ep_dequeue,
  949. .set_halt = omap_ep_set_halt,
  950. /* fifo_status ... report bytes in fifo */
  951. /* fifo_flush ... flush fifo */
  952. };
  953. /*-------------------------------------------------------------------------*/
  954. static int omap_get_frame(struct usb_gadget *gadget)
  955. {
  956. u16 sof = omap_readw(UDC_SOF);
  957. return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
  958. }
  959. static int omap_wakeup(struct usb_gadget *gadget)
  960. {
  961. struct omap_udc *udc;
  962. unsigned long flags;
  963. int retval = -EHOSTUNREACH;
  964. udc = container_of(gadget, struct omap_udc, gadget);
  965. spin_lock_irqsave(&udc->lock, flags);
  966. if (udc->devstat & UDC_SUS) {
  967. /* NOTE: OTG spec erratum says that OTG devices may
  968. * issue wakeups without host enable.
  969. */
  970. if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
  971. DBG("remote wakeup...\n");
  972. omap_writew(UDC_RMT_WKP, UDC_SYSCON2);
  973. retval = 0;
  974. }
  975. /* NOTE: non-OTG systems may use SRP TOO... */
  976. } else if (!(udc->devstat & UDC_ATT)) {
  977. if (!IS_ERR_OR_NULL(udc->transceiver))
  978. retval = otg_start_srp(udc->transceiver->otg);
  979. }
  980. spin_unlock_irqrestore(&udc->lock, flags);
  981. return retval;
  982. }
  983. static int
  984. omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
  985. {
  986. struct omap_udc *udc;
  987. unsigned long flags;
  988. u16 syscon1;
  989. gadget->is_selfpowered = (is_selfpowered != 0);
  990. udc = container_of(gadget, struct omap_udc, gadget);
  991. spin_lock_irqsave(&udc->lock, flags);
  992. syscon1 = omap_readw(UDC_SYSCON1);
  993. if (is_selfpowered)
  994. syscon1 |= UDC_SELF_PWR;
  995. else
  996. syscon1 &= ~UDC_SELF_PWR;
  997. omap_writew(syscon1, UDC_SYSCON1);
  998. spin_unlock_irqrestore(&udc->lock, flags);
  999. return 0;
  1000. }
  1001. static int can_pullup(struct omap_udc *udc)
  1002. {
  1003. return udc->driver && udc->softconnect && udc->vbus_active;
  1004. }
  1005. static void pullup_enable(struct omap_udc *udc)
  1006. {
  1007. u16 w;
  1008. w = omap_readw(UDC_SYSCON1);
  1009. w |= UDC_PULLUP_EN;
  1010. omap_writew(w, UDC_SYSCON1);
  1011. if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
  1012. u32 l;
  1013. l = omap_readl(OTG_CTRL);
  1014. l |= OTG_BSESSVLD;
  1015. omap_writel(l, OTG_CTRL);
  1016. }
  1017. omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
  1018. }
  1019. static void pullup_disable(struct omap_udc *udc)
  1020. {
  1021. u16 w;
  1022. if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
  1023. u32 l;
  1024. l = omap_readl(OTG_CTRL);
  1025. l &= ~OTG_BSESSVLD;
  1026. omap_writel(l, OTG_CTRL);
  1027. }
  1028. omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
  1029. w = omap_readw(UDC_SYSCON1);
  1030. w &= ~UDC_PULLUP_EN;
  1031. omap_writew(w, UDC_SYSCON1);
  1032. }
  1033. static struct omap_udc *udc;
  1034. static void omap_udc_enable_clock(int enable)
  1035. {
  1036. if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
  1037. return;
  1038. if (enable) {
  1039. clk_enable(udc->dc_clk);
  1040. clk_enable(udc->hhc_clk);
  1041. udelay(100);
  1042. } else {
  1043. clk_disable(udc->hhc_clk);
  1044. clk_disable(udc->dc_clk);
  1045. }
  1046. }
  1047. /*
  1048. * Called by whatever detects VBUS sessions: external transceiver
  1049. * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
  1050. */
  1051. static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
  1052. {
  1053. struct omap_udc *udc;
  1054. unsigned long flags;
  1055. u32 l;
  1056. udc = container_of(gadget, struct omap_udc, gadget);
  1057. spin_lock_irqsave(&udc->lock, flags);
  1058. VDBG("VBUS %s\n", is_active ? "on" : "off");
  1059. udc->vbus_active = (is_active != 0);
  1060. if (cpu_is_omap15xx()) {
  1061. /* "software" detect, ignored if !VBUS_MODE_1510 */
  1062. l = omap_readl(FUNC_MUX_CTRL_0);
  1063. if (is_active)
  1064. l |= VBUS_CTRL_1510;
  1065. else
  1066. l &= ~VBUS_CTRL_1510;
  1067. omap_writel(l, FUNC_MUX_CTRL_0);
  1068. }
  1069. if (udc->dc_clk != NULL && is_active) {
  1070. if (!udc->clk_requested) {
  1071. omap_udc_enable_clock(1);
  1072. udc->clk_requested = 1;
  1073. }
  1074. }
  1075. if (can_pullup(udc))
  1076. pullup_enable(udc);
  1077. else
  1078. pullup_disable(udc);
  1079. if (udc->dc_clk != NULL && !is_active) {
  1080. if (udc->clk_requested) {
  1081. omap_udc_enable_clock(0);
  1082. udc->clk_requested = 0;
  1083. }
  1084. }
  1085. spin_unlock_irqrestore(&udc->lock, flags);
  1086. return 0;
  1087. }
  1088. static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  1089. {
  1090. struct omap_udc *udc;
  1091. udc = container_of(gadget, struct omap_udc, gadget);
  1092. if (!IS_ERR_OR_NULL(udc->transceiver))
  1093. return usb_phy_set_power(udc->transceiver, mA);
  1094. return -EOPNOTSUPP;
  1095. }
  1096. static int omap_pullup(struct usb_gadget *gadget, int is_on)
  1097. {
  1098. struct omap_udc *udc;
  1099. unsigned long flags;
  1100. udc = container_of(gadget, struct omap_udc, gadget);
  1101. spin_lock_irqsave(&udc->lock, flags);
  1102. udc->softconnect = (is_on != 0);
  1103. if (can_pullup(udc))
  1104. pullup_enable(udc);
  1105. else
  1106. pullup_disable(udc);
  1107. spin_unlock_irqrestore(&udc->lock, flags);
  1108. return 0;
  1109. }
  1110. static int omap_udc_start(struct usb_gadget *g,
  1111. struct usb_gadget_driver *driver);
  1112. static int omap_udc_stop(struct usb_gadget *g);
  1113. static const struct usb_gadget_ops omap_gadget_ops = {
  1114. .get_frame = omap_get_frame,
  1115. .wakeup = omap_wakeup,
  1116. .set_selfpowered = omap_set_selfpowered,
  1117. .vbus_session = omap_vbus_session,
  1118. .vbus_draw = omap_vbus_draw,
  1119. .pullup = omap_pullup,
  1120. .udc_start = omap_udc_start,
  1121. .udc_stop = omap_udc_stop,
  1122. };
  1123. /*-------------------------------------------------------------------------*/
  1124. /* dequeue ALL requests; caller holds udc->lock */
  1125. static void nuke(struct omap_ep *ep, int status)
  1126. {
  1127. struct omap_req *req;
  1128. ep->stopped = 1;
  1129. if (use_dma && ep->dma_channel)
  1130. dma_channel_release(ep);
  1131. use_ep(ep, 0);
  1132. omap_writew(UDC_CLR_EP, UDC_CTRL);
  1133. if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
  1134. omap_writew(UDC_SET_HALT, UDC_CTRL);
  1135. while (!list_empty(&ep->queue)) {
  1136. req = list_entry(ep->queue.next, struct omap_req, queue);
  1137. done(ep, req, status);
  1138. }
  1139. }
  1140. /* caller holds udc->lock */
  1141. static void udc_quiesce(struct omap_udc *udc)
  1142. {
  1143. struct omap_ep *ep;
  1144. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1145. nuke(&udc->ep[0], -ESHUTDOWN);
  1146. list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
  1147. nuke(ep, -ESHUTDOWN);
  1148. }
  1149. /*-------------------------------------------------------------------------*/
  1150. static void update_otg(struct omap_udc *udc)
  1151. {
  1152. u16 devstat;
  1153. if (!gadget_is_otg(&udc->gadget))
  1154. return;
  1155. if (omap_readl(OTG_CTRL) & OTG_ID)
  1156. devstat = omap_readw(UDC_DEVSTAT);
  1157. else
  1158. devstat = 0;
  1159. udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
  1160. udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
  1161. udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
  1162. /* Enable HNP early, avoiding races on suspend irq path.
  1163. * ASSUMES OTG state machine B_BUS_REQ input is true.
  1164. */
  1165. if (udc->gadget.b_hnp_enable) {
  1166. u32 l;
  1167. l = omap_readl(OTG_CTRL);
  1168. l |= OTG_B_HNPEN | OTG_B_BUSREQ;
  1169. l &= ~OTG_PULLUP;
  1170. omap_writel(l, OTG_CTRL);
  1171. }
  1172. }
  1173. static void ep0_irq(struct omap_udc *udc, u16 irq_src)
  1174. {
  1175. struct omap_ep *ep0 = &udc->ep[0];
  1176. struct omap_req *req = NULL;
  1177. ep0->irqs++;
  1178. /* Clear any pending requests and then scrub any rx/tx state
  1179. * before starting to handle the SETUP request.
  1180. */
  1181. if (irq_src & UDC_SETUP) {
  1182. u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
  1183. nuke(ep0, 0);
  1184. if (ack) {
  1185. omap_writew(ack, UDC_IRQ_SRC);
  1186. irq_src = UDC_SETUP;
  1187. }
  1188. }
  1189. /* IN/OUT packets mean we're in the DATA or STATUS stage.
  1190. * This driver uses only uses protocol stalls (ep0 never halts),
  1191. * and if we got this far the gadget driver already had a
  1192. * chance to stall. Tries to be forgiving of host oddities.
  1193. *
  1194. * NOTE: the last chance gadget drivers have to stall control
  1195. * requests is during their request completion callback.
  1196. */
  1197. if (!list_empty(&ep0->queue))
  1198. req = container_of(ep0->queue.next, struct omap_req, queue);
  1199. /* IN == TX to host */
  1200. if (irq_src & UDC_EP0_TX) {
  1201. int stat;
  1202. omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
  1203. omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
  1204. stat = omap_readw(UDC_STAT_FLG);
  1205. if (stat & UDC_ACK) {
  1206. if (udc->ep0_in) {
  1207. /* write next IN packet from response,
  1208. * or set up the status stage.
  1209. */
  1210. if (req)
  1211. stat = write_fifo(ep0, req);
  1212. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1213. if (!req && udc->ep0_pending) {
  1214. omap_writew(UDC_EP_SEL, UDC_EP_NUM);
  1215. omap_writew(UDC_CLR_EP, UDC_CTRL);
  1216. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1217. omap_writew(0, UDC_EP_NUM);
  1218. udc->ep0_pending = 0;
  1219. } /* else: 6 wait states before it'll tx */
  1220. } else {
  1221. /* ack status stage of OUT transfer */
  1222. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1223. if (req)
  1224. done(ep0, req, 0);
  1225. }
  1226. req = NULL;
  1227. } else if (stat & UDC_STALL) {
  1228. omap_writew(UDC_CLR_HALT, UDC_CTRL);
  1229. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1230. } else {
  1231. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1232. }
  1233. }
  1234. /* OUT == RX from host */
  1235. if (irq_src & UDC_EP0_RX) {
  1236. int stat;
  1237. omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
  1238. omap_writew(UDC_EP_SEL, UDC_EP_NUM);
  1239. stat = omap_readw(UDC_STAT_FLG);
  1240. if (stat & UDC_ACK) {
  1241. if (!udc->ep0_in) {
  1242. stat = 0;
  1243. /* read next OUT packet of request, maybe
  1244. * reactivating the fifo; stall on errors.
  1245. */
  1246. stat = read_fifo(ep0, req);
  1247. if (!req || stat < 0) {
  1248. omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
  1249. udc->ep0_pending = 0;
  1250. stat = 0;
  1251. } else if (stat == 0)
  1252. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1253. omap_writew(0, UDC_EP_NUM);
  1254. /* activate status stage */
  1255. if (stat == 1) {
  1256. done(ep0, req, 0);
  1257. /* that may have STALLed ep0... */
  1258. omap_writew(UDC_EP_SEL | UDC_EP_DIR,
  1259. UDC_EP_NUM);
  1260. omap_writew(UDC_CLR_EP, UDC_CTRL);
  1261. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1262. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1263. udc->ep0_pending = 0;
  1264. }
  1265. } else {
  1266. /* ack status stage of IN transfer */
  1267. omap_writew(0, UDC_EP_NUM);
  1268. if (req)
  1269. done(ep0, req, 0);
  1270. }
  1271. } else if (stat & UDC_STALL) {
  1272. omap_writew(UDC_CLR_HALT, UDC_CTRL);
  1273. omap_writew(0, UDC_EP_NUM);
  1274. } else {
  1275. omap_writew(0, UDC_EP_NUM);
  1276. }
  1277. }
  1278. /* SETUP starts all control transfers */
  1279. if (irq_src & UDC_SETUP) {
  1280. union u {
  1281. u16 word[4];
  1282. struct usb_ctrlrequest r;
  1283. } u;
  1284. int status = -EINVAL;
  1285. struct omap_ep *ep;
  1286. /* read the (latest) SETUP message */
  1287. do {
  1288. omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
  1289. /* two bytes at a time */
  1290. u.word[0] = omap_readw(UDC_DATA);
  1291. u.word[1] = omap_readw(UDC_DATA);
  1292. u.word[2] = omap_readw(UDC_DATA);
  1293. u.word[3] = omap_readw(UDC_DATA);
  1294. omap_writew(0, UDC_EP_NUM);
  1295. } while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
  1296. #define w_value le16_to_cpu(u.r.wValue)
  1297. #define w_index le16_to_cpu(u.r.wIndex)
  1298. #define w_length le16_to_cpu(u.r.wLength)
  1299. /* Delegate almost all control requests to the gadget driver,
  1300. * except for a handful of ch9 status/feature requests that
  1301. * hardware doesn't autodecode _and_ the gadget API hides.
  1302. */
  1303. udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
  1304. udc->ep0_set_config = 0;
  1305. udc->ep0_pending = 1;
  1306. ep0->stopped = 0;
  1307. ep0->ackwait = 0;
  1308. switch (u.r.bRequest) {
  1309. case USB_REQ_SET_CONFIGURATION:
  1310. /* udc needs to know when ep != 0 is valid */
  1311. if (u.r.bRequestType != USB_RECIP_DEVICE)
  1312. goto delegate;
  1313. if (w_length != 0)
  1314. goto do_stall;
  1315. udc->ep0_set_config = 1;
  1316. udc->ep0_reset_config = (w_value == 0);
  1317. VDBG("set config %d\n", w_value);
  1318. /* update udc NOW since gadget driver may start
  1319. * queueing requests immediately; clear config
  1320. * later if it fails the request.
  1321. */
  1322. if (udc->ep0_reset_config)
  1323. omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
  1324. else
  1325. omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
  1326. update_otg(udc);
  1327. goto delegate;
  1328. case USB_REQ_CLEAR_FEATURE:
  1329. /* clear endpoint halt */
  1330. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  1331. goto delegate;
  1332. if (w_value != USB_ENDPOINT_HALT
  1333. || w_length != 0)
  1334. goto do_stall;
  1335. ep = &udc->ep[w_index & 0xf];
  1336. if (ep != ep0) {
  1337. if (w_index & USB_DIR_IN)
  1338. ep += 16;
  1339. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  1340. || !ep->ep.desc)
  1341. goto do_stall;
  1342. use_ep(ep, 0);
  1343. omap_writew(udc->clr_halt, UDC_CTRL);
  1344. ep->ackwait = 0;
  1345. if (!(ep->bEndpointAddress & USB_DIR_IN)) {
  1346. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1347. ep->ackwait = 1 + ep->double_buf;
  1348. }
  1349. /* NOTE: assumes the host behaves sanely,
  1350. * only clearing real halts. Else we may
  1351. * need to kill pending transfers and then
  1352. * restart the queue... very messy for DMA!
  1353. */
  1354. }
  1355. VDBG("%s halt cleared by host\n", ep->name);
  1356. goto ep0out_status_stage;
  1357. case USB_REQ_SET_FEATURE:
  1358. /* set endpoint halt */
  1359. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  1360. goto delegate;
  1361. if (w_value != USB_ENDPOINT_HALT
  1362. || w_length != 0)
  1363. goto do_stall;
  1364. ep = &udc->ep[w_index & 0xf];
  1365. if (w_index & USB_DIR_IN)
  1366. ep += 16;
  1367. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  1368. || ep == ep0 || !ep->ep.desc)
  1369. goto do_stall;
  1370. if (use_dma && ep->has_dma) {
  1371. /* this has rude side-effects (aborts) and
  1372. * can't really work if DMA-IN is active
  1373. */
  1374. DBG("%s host set_halt, NYET\n", ep->name);
  1375. goto do_stall;
  1376. }
  1377. use_ep(ep, 0);
  1378. /* can't halt if fifo isn't empty... */
  1379. omap_writew(UDC_CLR_EP, UDC_CTRL);
  1380. omap_writew(UDC_SET_HALT, UDC_CTRL);
  1381. VDBG("%s halted by host\n", ep->name);
  1382. ep0out_status_stage:
  1383. status = 0;
  1384. omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
  1385. omap_writew(UDC_CLR_EP, UDC_CTRL);
  1386. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1387. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1388. udc->ep0_pending = 0;
  1389. break;
  1390. case USB_REQ_GET_STATUS:
  1391. /* USB_ENDPOINT_HALT status? */
  1392. if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
  1393. goto intf_status;
  1394. /* ep0 never stalls */
  1395. if (!(w_index & 0xf))
  1396. goto zero_status;
  1397. /* only active endpoints count */
  1398. ep = &udc->ep[w_index & 0xf];
  1399. if (w_index & USB_DIR_IN)
  1400. ep += 16;
  1401. if (!ep->ep.desc)
  1402. goto do_stall;
  1403. /* iso never stalls */
  1404. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
  1405. goto zero_status;
  1406. /* FIXME don't assume non-halted endpoints!! */
  1407. ERR("%s status, can't report\n", ep->ep.name);
  1408. goto do_stall;
  1409. intf_status:
  1410. /* return interface status. if we were pedantic,
  1411. * we'd detect non-existent interfaces, and stall.
  1412. */
  1413. if (u.r.bRequestType
  1414. != (USB_DIR_IN|USB_RECIP_INTERFACE))
  1415. goto delegate;
  1416. zero_status:
  1417. /* return two zero bytes */
  1418. omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
  1419. omap_writew(0, UDC_DATA);
  1420. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1421. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1422. status = 0;
  1423. VDBG("GET_STATUS, interface %d\n", w_index);
  1424. /* next, status stage */
  1425. break;
  1426. default:
  1427. delegate:
  1428. /* activate the ep0out fifo right away */
  1429. if (!udc->ep0_in && w_length) {
  1430. omap_writew(0, UDC_EP_NUM);
  1431. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1432. }
  1433. /* gadget drivers see class/vendor specific requests,
  1434. * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
  1435. * and more
  1436. */
  1437. VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
  1438. u.r.bRequestType, u.r.bRequest,
  1439. w_value, w_index, w_length);
  1440. #undef w_value
  1441. #undef w_index
  1442. #undef w_length
  1443. /* The gadget driver may return an error here,
  1444. * causing an immediate protocol stall.
  1445. *
  1446. * Else it must issue a response, either queueing a
  1447. * response buffer for the DATA stage, or halting ep0
  1448. * (causing a protocol stall, not a real halt). A
  1449. * zero length buffer means no DATA stage.
  1450. *
  1451. * It's fine to issue that response after the setup()
  1452. * call returns, and this IRQ was handled.
  1453. */
  1454. udc->ep0_setup = 1;
  1455. spin_unlock(&udc->lock);
  1456. status = udc->driver->setup(&udc->gadget, &u.r);
  1457. spin_lock(&udc->lock);
  1458. udc->ep0_setup = 0;
  1459. }
  1460. if (status < 0) {
  1461. do_stall:
  1462. VDBG("req %02x.%02x protocol STALL; stat %d\n",
  1463. u.r.bRequestType, u.r.bRequest, status);
  1464. if (udc->ep0_set_config) {
  1465. if (udc->ep0_reset_config)
  1466. WARNING("error resetting config?\n");
  1467. else
  1468. omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
  1469. }
  1470. omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
  1471. udc->ep0_pending = 0;
  1472. }
  1473. }
  1474. }
  1475. /*-------------------------------------------------------------------------*/
  1476. #define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
  1477. static void devstate_irq(struct omap_udc *udc, u16 irq_src)
  1478. {
  1479. u16 devstat, change;
  1480. devstat = omap_readw(UDC_DEVSTAT);
  1481. change = devstat ^ udc->devstat;
  1482. udc->devstat = devstat;
  1483. if (change & (UDC_USB_RESET|UDC_ATT)) {
  1484. udc_quiesce(udc);
  1485. if (change & UDC_ATT) {
  1486. /* driver for any external transceiver will
  1487. * have called omap_vbus_session() already
  1488. */
  1489. if (devstat & UDC_ATT) {
  1490. udc->gadget.speed = USB_SPEED_FULL;
  1491. VDBG("connect\n");
  1492. if (IS_ERR_OR_NULL(udc->transceiver))
  1493. pullup_enable(udc);
  1494. /* if (driver->connect) call it */
  1495. } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  1496. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1497. if (IS_ERR_OR_NULL(udc->transceiver))
  1498. pullup_disable(udc);
  1499. DBG("disconnect, gadget %s\n",
  1500. udc->driver->driver.name);
  1501. if (udc->driver->disconnect) {
  1502. spin_unlock(&udc->lock);
  1503. udc->driver->disconnect(&udc->gadget);
  1504. spin_lock(&udc->lock);
  1505. }
  1506. }
  1507. change &= ~UDC_ATT;
  1508. }
  1509. if (change & UDC_USB_RESET) {
  1510. if (devstat & UDC_USB_RESET) {
  1511. VDBG("RESET=1\n");
  1512. } else {
  1513. udc->gadget.speed = USB_SPEED_FULL;
  1514. INFO("USB reset done, gadget %s\n",
  1515. udc->driver->driver.name);
  1516. /* ep0 traffic is legal from now on */
  1517. omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
  1518. UDC_IRQ_EN);
  1519. }
  1520. change &= ~UDC_USB_RESET;
  1521. }
  1522. }
  1523. if (change & UDC_SUS) {
  1524. if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  1525. /* FIXME tell isp1301 to suspend/resume (?) */
  1526. if (devstat & UDC_SUS) {
  1527. VDBG("suspend\n");
  1528. update_otg(udc);
  1529. /* HNP could be under way already */
  1530. if (udc->gadget.speed == USB_SPEED_FULL
  1531. && udc->driver->suspend) {
  1532. spin_unlock(&udc->lock);
  1533. udc->driver->suspend(&udc->gadget);
  1534. spin_lock(&udc->lock);
  1535. }
  1536. if (!IS_ERR_OR_NULL(udc->transceiver))
  1537. usb_phy_set_suspend(
  1538. udc->transceiver, 1);
  1539. } else {
  1540. VDBG("resume\n");
  1541. if (!IS_ERR_OR_NULL(udc->transceiver))
  1542. usb_phy_set_suspend(
  1543. udc->transceiver, 0);
  1544. if (udc->gadget.speed == USB_SPEED_FULL
  1545. && udc->driver->resume) {
  1546. spin_unlock(&udc->lock);
  1547. udc->driver->resume(&udc->gadget);
  1548. spin_lock(&udc->lock);
  1549. }
  1550. }
  1551. }
  1552. change &= ~UDC_SUS;
  1553. }
  1554. if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
  1555. update_otg(udc);
  1556. change &= ~OTG_FLAGS;
  1557. }
  1558. change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
  1559. if (change)
  1560. VDBG("devstat %03x, ignore change %03x\n",
  1561. devstat, change);
  1562. omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
  1563. }
  1564. static irqreturn_t omap_udc_irq(int irq, void *_udc)
  1565. {
  1566. struct omap_udc *udc = _udc;
  1567. u16 irq_src;
  1568. irqreturn_t status = IRQ_NONE;
  1569. unsigned long flags;
  1570. spin_lock_irqsave(&udc->lock, flags);
  1571. irq_src = omap_readw(UDC_IRQ_SRC);
  1572. /* Device state change (usb ch9 stuff) */
  1573. if (irq_src & UDC_DS_CHG) {
  1574. devstate_irq(_udc, irq_src);
  1575. status = IRQ_HANDLED;
  1576. irq_src &= ~UDC_DS_CHG;
  1577. }
  1578. /* EP0 control transfers */
  1579. if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
  1580. ep0_irq(_udc, irq_src);
  1581. status = IRQ_HANDLED;
  1582. irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
  1583. }
  1584. /* DMA transfer completion */
  1585. if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
  1586. dma_irq(_udc, irq_src);
  1587. status = IRQ_HANDLED;
  1588. irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
  1589. }
  1590. irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
  1591. if (irq_src)
  1592. DBG("udc_irq, unhandled %03x\n", irq_src);
  1593. spin_unlock_irqrestore(&udc->lock, flags);
  1594. return status;
  1595. }
  1596. /* workaround for seemingly-lost IRQs for RX ACKs... */
  1597. #define PIO_OUT_TIMEOUT (jiffies + HZ/3)
  1598. #define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
  1599. static void pio_out_timer(struct timer_list *t)
  1600. {
  1601. struct omap_ep *ep = from_timer(ep, t, timer);
  1602. unsigned long flags;
  1603. u16 stat_flg;
  1604. spin_lock_irqsave(&ep->udc->lock, flags);
  1605. if (!list_empty(&ep->queue) && ep->ackwait) {
  1606. use_ep(ep, UDC_EP_SEL);
  1607. stat_flg = omap_readw(UDC_STAT_FLG);
  1608. if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
  1609. || (ep->double_buf && HALF_FULL(stat_flg)))) {
  1610. struct omap_req *req;
  1611. VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
  1612. req = container_of(ep->queue.next,
  1613. struct omap_req, queue);
  1614. (void) read_fifo(ep, req);
  1615. omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
  1616. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1617. ep->ackwait = 1 + ep->double_buf;
  1618. } else
  1619. deselect_ep();
  1620. }
  1621. mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
  1622. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1623. }
  1624. static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
  1625. {
  1626. u16 epn_stat, irq_src;
  1627. irqreturn_t status = IRQ_NONE;
  1628. struct omap_ep *ep;
  1629. int epnum;
  1630. struct omap_udc *udc = _dev;
  1631. struct omap_req *req;
  1632. unsigned long flags;
  1633. spin_lock_irqsave(&udc->lock, flags);
  1634. epn_stat = omap_readw(UDC_EPN_STAT);
  1635. irq_src = omap_readw(UDC_IRQ_SRC);
  1636. /* handle OUT first, to avoid some wasteful NAKs */
  1637. if (irq_src & UDC_EPN_RX) {
  1638. epnum = (epn_stat >> 8) & 0x0f;
  1639. omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
  1640. status = IRQ_HANDLED;
  1641. ep = &udc->ep[epnum];
  1642. ep->irqs++;
  1643. omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
  1644. ep->fnf = 0;
  1645. if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
  1646. ep->ackwait--;
  1647. if (!list_empty(&ep->queue)) {
  1648. int stat;
  1649. req = container_of(ep->queue.next,
  1650. struct omap_req, queue);
  1651. stat = read_fifo(ep, req);
  1652. if (!ep->double_buf)
  1653. ep->fnf = 1;
  1654. }
  1655. }
  1656. /* min 6 clock delay before clearing EP_SEL ... */
  1657. epn_stat = omap_readw(UDC_EPN_STAT);
  1658. epn_stat = omap_readw(UDC_EPN_STAT);
  1659. omap_writew(epnum, UDC_EP_NUM);
  1660. /* enabling fifo _after_ clearing ACK, contrary to docs,
  1661. * reduces lossage; timer still needed though (sigh).
  1662. */
  1663. if (ep->fnf) {
  1664. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1665. ep->ackwait = 1 + ep->double_buf;
  1666. }
  1667. mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
  1668. }
  1669. /* then IN transfers */
  1670. else if (irq_src & UDC_EPN_TX) {
  1671. epnum = epn_stat & 0x0f;
  1672. omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
  1673. status = IRQ_HANDLED;
  1674. ep = &udc->ep[16 + epnum];
  1675. ep->irqs++;
  1676. omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
  1677. if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
  1678. ep->ackwait = 0;
  1679. if (!list_empty(&ep->queue)) {
  1680. req = container_of(ep->queue.next,
  1681. struct omap_req, queue);
  1682. (void) write_fifo(ep, req);
  1683. }
  1684. }
  1685. /* min 6 clock delay before clearing EP_SEL ... */
  1686. epn_stat = omap_readw(UDC_EPN_STAT);
  1687. epn_stat = omap_readw(UDC_EPN_STAT);
  1688. omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
  1689. /* then 6 clocks before it'd tx */
  1690. }
  1691. spin_unlock_irqrestore(&udc->lock, flags);
  1692. return status;
  1693. }
  1694. #ifdef USE_ISO
  1695. static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
  1696. {
  1697. struct omap_udc *udc = _dev;
  1698. struct omap_ep *ep;
  1699. int pending = 0;
  1700. unsigned long flags;
  1701. spin_lock_irqsave(&udc->lock, flags);
  1702. /* handle all non-DMA ISO transfers */
  1703. list_for_each_entry(ep, &udc->iso, iso) {
  1704. u16 stat;
  1705. struct omap_req *req;
  1706. if (ep->has_dma || list_empty(&ep->queue))
  1707. continue;
  1708. req = list_entry(ep->queue.next, struct omap_req, queue);
  1709. use_ep(ep, UDC_EP_SEL);
  1710. stat = omap_readw(UDC_STAT_FLG);
  1711. /* NOTE: like the other controller drivers, this isn't
  1712. * currently reporting lost or damaged frames.
  1713. */
  1714. if (ep->bEndpointAddress & USB_DIR_IN) {
  1715. if (stat & UDC_MISS_IN)
  1716. /* done(ep, req, -EPROTO) */;
  1717. else
  1718. write_fifo(ep, req);
  1719. } else {
  1720. int status = 0;
  1721. if (stat & UDC_NO_RXPACKET)
  1722. status = -EREMOTEIO;
  1723. else if (stat & UDC_ISO_ERR)
  1724. status = -EILSEQ;
  1725. else if (stat & UDC_DATA_FLUSH)
  1726. status = -ENOSR;
  1727. if (status)
  1728. /* done(ep, req, status) */;
  1729. else
  1730. read_fifo(ep, req);
  1731. }
  1732. deselect_ep();
  1733. /* 6 wait states before next EP */
  1734. ep->irqs++;
  1735. if (!list_empty(&ep->queue))
  1736. pending = 1;
  1737. }
  1738. if (!pending) {
  1739. u16 w;
  1740. w = omap_readw(UDC_IRQ_EN);
  1741. w &= ~UDC_SOF_IE;
  1742. omap_writew(w, UDC_IRQ_EN);
  1743. }
  1744. omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
  1745. spin_unlock_irqrestore(&udc->lock, flags);
  1746. return IRQ_HANDLED;
  1747. }
  1748. #endif
  1749. /*-------------------------------------------------------------------------*/
  1750. static inline int machine_without_vbus_sense(void)
  1751. {
  1752. return machine_is_omap_innovator()
  1753. || machine_is_omap_osk()
  1754. || machine_is_omap_palmte()
  1755. || machine_is_sx1()
  1756. /* No known omap7xx boards with vbus sense */
  1757. || cpu_is_omap7xx();
  1758. }
  1759. static int omap_udc_start(struct usb_gadget *g,
  1760. struct usb_gadget_driver *driver)
  1761. {
  1762. int status;
  1763. struct omap_ep *ep;
  1764. unsigned long flags;
  1765. spin_lock_irqsave(&udc->lock, flags);
  1766. /* reset state */
  1767. list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
  1768. ep->irqs = 0;
  1769. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
  1770. continue;
  1771. use_ep(ep, 0);
  1772. omap_writew(UDC_SET_HALT, UDC_CTRL);
  1773. }
  1774. udc->ep0_pending = 0;
  1775. udc->ep[0].irqs = 0;
  1776. udc->softconnect = 1;
  1777. /* hook up the driver */
  1778. udc->driver = driver;
  1779. spin_unlock_irqrestore(&udc->lock, flags);
  1780. if (udc->dc_clk != NULL)
  1781. omap_udc_enable_clock(1);
  1782. omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
  1783. /* connect to bus through transceiver */
  1784. if (!IS_ERR_OR_NULL(udc->transceiver)) {
  1785. status = otg_set_peripheral(udc->transceiver->otg,
  1786. &udc->gadget);
  1787. if (status < 0) {
  1788. ERR("can't bind to transceiver\n");
  1789. udc->driver = NULL;
  1790. goto done;
  1791. }
  1792. } else {
  1793. status = 0;
  1794. if (can_pullup(udc))
  1795. pullup_enable(udc);
  1796. else
  1797. pullup_disable(udc);
  1798. }
  1799. /* boards that don't have VBUS sensing can't autogate 48MHz;
  1800. * can't enter deep sleep while a gadget driver is active.
  1801. */
  1802. if (machine_without_vbus_sense())
  1803. omap_vbus_session(&udc->gadget, 1);
  1804. done:
  1805. if (udc->dc_clk != NULL)
  1806. omap_udc_enable_clock(0);
  1807. return status;
  1808. }
  1809. static int omap_udc_stop(struct usb_gadget *g)
  1810. {
  1811. unsigned long flags;
  1812. if (udc->dc_clk != NULL)
  1813. omap_udc_enable_clock(1);
  1814. if (machine_without_vbus_sense())
  1815. omap_vbus_session(&udc->gadget, 0);
  1816. if (!IS_ERR_OR_NULL(udc->transceiver))
  1817. (void) otg_set_peripheral(udc->transceiver->otg, NULL);
  1818. else
  1819. pullup_disable(udc);
  1820. spin_lock_irqsave(&udc->lock, flags);
  1821. udc_quiesce(udc);
  1822. spin_unlock_irqrestore(&udc->lock, flags);
  1823. udc->driver = NULL;
  1824. if (udc->dc_clk != NULL)
  1825. omap_udc_enable_clock(0);
  1826. return 0;
  1827. }
  1828. /*-------------------------------------------------------------------------*/
  1829. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1830. #include <linux/seq_file.h>
  1831. static const char proc_filename[] = "driver/udc";
  1832. #define FOURBITS "%s%s%s%s"
  1833. #define EIGHTBITS "%s%s%s%s%s%s%s%s"
  1834. static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
  1835. {
  1836. u16 stat_flg;
  1837. struct omap_req *req;
  1838. char buf[20];
  1839. use_ep(ep, 0);
  1840. if (use_dma && ep->has_dma)
  1841. snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
  1842. (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
  1843. ep->dma_channel - 1, ep->lch);
  1844. else
  1845. buf[0] = 0;
  1846. stat_flg = omap_readw(UDC_STAT_FLG);
  1847. seq_printf(s,
  1848. "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
  1849. ep->name, buf,
  1850. ep->double_buf ? "dbuf " : "",
  1851. ({ char *s;
  1852. switch (ep->ackwait) {
  1853. case 0:
  1854. s = "";
  1855. break;
  1856. case 1:
  1857. s = "(ackw) ";
  1858. break;
  1859. case 2:
  1860. s = "(ackw2) ";
  1861. break;
  1862. default:
  1863. s = "(?) ";
  1864. break;
  1865. } s; }),
  1866. ep->irqs, stat_flg,
  1867. (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
  1868. (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
  1869. (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
  1870. (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
  1871. (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
  1872. (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
  1873. (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
  1874. (stat_flg & UDC_STALL) ? "STALL " : "",
  1875. (stat_flg & UDC_NAK) ? "NAK " : "",
  1876. (stat_flg & UDC_ACK) ? "ACK " : "",
  1877. (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
  1878. (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
  1879. (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
  1880. if (list_empty(&ep->queue))
  1881. seq_printf(s, "\t(queue empty)\n");
  1882. else
  1883. list_for_each_entry(req, &ep->queue, queue) {
  1884. unsigned length = req->req.actual;
  1885. if (use_dma && buf[0]) {
  1886. length += ((ep->bEndpointAddress & USB_DIR_IN)
  1887. ? dma_src_len : dma_dest_len)
  1888. (ep, req->req.dma + length);
  1889. buf[0] = 0;
  1890. }
  1891. seq_printf(s, "\treq %p len %d/%d buf %p\n",
  1892. &req->req, length,
  1893. req->req.length, req->req.buf);
  1894. }
  1895. }
  1896. static char *trx_mode(unsigned m, int enabled)
  1897. {
  1898. switch (m) {
  1899. case 0:
  1900. return enabled ? "*6wire" : "unused";
  1901. case 1:
  1902. return "4wire";
  1903. case 2:
  1904. return "3wire";
  1905. case 3:
  1906. return "6wire";
  1907. default:
  1908. return "unknown";
  1909. }
  1910. }
  1911. static int proc_otg_show(struct seq_file *s)
  1912. {
  1913. u32 tmp;
  1914. u32 trans = 0;
  1915. char *ctrl_name = "(UNKNOWN)";
  1916. tmp = omap_readl(OTG_REV);
  1917. ctrl_name = "transceiver_ctrl";
  1918. trans = omap_readw(USB_TRANSCEIVER_CTRL);
  1919. seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
  1920. tmp >> 4, tmp & 0xf, ctrl_name, trans);
  1921. tmp = omap_readw(OTG_SYSCON_1);
  1922. seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
  1923. FOURBITS "\n", tmp,
  1924. trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
  1925. trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
  1926. (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
  1927. ? "internal"
  1928. : trx_mode(USB0_TRX_MODE(tmp), 1),
  1929. (tmp & OTG_IDLE_EN) ? " !otg" : "",
  1930. (tmp & HST_IDLE_EN) ? " !host" : "",
  1931. (tmp & DEV_IDLE_EN) ? " !dev" : "",
  1932. (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
  1933. tmp = omap_readl(OTG_SYSCON_2);
  1934. seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
  1935. " b_ase_brst=%d hmc=%d\n", tmp,
  1936. (tmp & OTG_EN) ? " otg_en" : "",
  1937. (tmp & USBX_SYNCHRO) ? " synchro" : "",
  1938. /* much more SRP stuff */
  1939. (tmp & SRP_DATA) ? " srp_data" : "",
  1940. (tmp & SRP_VBUS) ? " srp_vbus" : "",
  1941. (tmp & OTG_PADEN) ? " otg_paden" : "",
  1942. (tmp & HMC_PADEN) ? " hmc_paden" : "",
  1943. (tmp & UHOST_EN) ? " uhost_en" : "",
  1944. (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
  1945. (tmp & HMC_TLLATTACH) ? " tllattach" : "",
  1946. B_ASE_BRST(tmp),
  1947. OTG_HMC(tmp));
  1948. tmp = omap_readl(OTG_CTRL);
  1949. seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
  1950. (tmp & OTG_ASESSVLD) ? " asess" : "",
  1951. (tmp & OTG_BSESSEND) ? " bsess_end" : "",
  1952. (tmp & OTG_BSESSVLD) ? " bsess" : "",
  1953. (tmp & OTG_VBUSVLD) ? " vbus" : "",
  1954. (tmp & OTG_ID) ? " id" : "",
  1955. (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
  1956. (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
  1957. (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
  1958. (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
  1959. (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
  1960. (tmp & OTG_BUSDROP) ? " busdrop" : "",
  1961. (tmp & OTG_PULLDOWN) ? " down" : "",
  1962. (tmp & OTG_PULLUP) ? " up" : "",
  1963. (tmp & OTG_DRV_VBUS) ? " drv" : "",
  1964. (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
  1965. (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
  1966. (tmp & OTG_PU_ID) ? " pu_id" : ""
  1967. );
  1968. tmp = omap_readw(OTG_IRQ_EN);
  1969. seq_printf(s, "otg_irq_en %04x" "\n", tmp);
  1970. tmp = omap_readw(OTG_IRQ_SRC);
  1971. seq_printf(s, "otg_irq_src %04x" "\n", tmp);
  1972. tmp = omap_readw(OTG_OUTCTRL);
  1973. seq_printf(s, "otg_outctrl %04x" "\n", tmp);
  1974. tmp = omap_readw(OTG_TEST);
  1975. seq_printf(s, "otg_test %04x" "\n", tmp);
  1976. return 0;
  1977. }
  1978. static int proc_udc_show(struct seq_file *s, void *_)
  1979. {
  1980. u32 tmp;
  1981. struct omap_ep *ep;
  1982. unsigned long flags;
  1983. spin_lock_irqsave(&udc->lock, flags);
  1984. seq_printf(s, "%s, version: " DRIVER_VERSION
  1985. #ifdef USE_ISO
  1986. " (iso)"
  1987. #endif
  1988. "%s\n",
  1989. driver_desc,
  1990. use_dma ? " (dma)" : "");
  1991. tmp = omap_readw(UDC_REV) & 0xff;
  1992. seq_printf(s,
  1993. "UDC rev %d.%d, fifo mode %d, gadget %s\n"
  1994. "hmc %d, transceiver %s\n",
  1995. tmp >> 4, tmp & 0xf,
  1996. fifo_mode,
  1997. udc->driver ? udc->driver->driver.name : "(none)",
  1998. HMC,
  1999. udc->transceiver
  2000. ? udc->transceiver->label
  2001. : (cpu_is_omap1710()
  2002. ? "external" : "(none)"));
  2003. seq_printf(s, "ULPD control %04x req %04x status %04x\n",
  2004. omap_readw(ULPD_CLOCK_CTRL),
  2005. omap_readw(ULPD_SOFT_REQ),
  2006. omap_readw(ULPD_STATUS_REQ));
  2007. /* OTG controller registers */
  2008. if (!cpu_is_omap15xx())
  2009. proc_otg_show(s);
  2010. tmp = omap_readw(UDC_SYSCON1);
  2011. seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
  2012. (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
  2013. (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
  2014. (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
  2015. (tmp & UDC_NAK_EN) ? " nak" : "",
  2016. (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
  2017. (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
  2018. (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
  2019. (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
  2020. /* syscon2 is write-only */
  2021. /* UDC controller registers */
  2022. if (!(tmp & UDC_PULLUP_EN)) {
  2023. seq_printf(s, "(suspended)\n");
  2024. spin_unlock_irqrestore(&udc->lock, flags);
  2025. return 0;
  2026. }
  2027. tmp = omap_readw(UDC_DEVSTAT);
  2028. seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
  2029. (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
  2030. (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
  2031. (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
  2032. (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
  2033. (tmp & UDC_USB_RESET) ? " usb_reset" : "",
  2034. (tmp & UDC_SUS) ? " SUS" : "",
  2035. (tmp & UDC_CFG) ? " CFG" : "",
  2036. (tmp & UDC_ADD) ? " ADD" : "",
  2037. (tmp & UDC_DEF) ? " DEF" : "",
  2038. (tmp & UDC_ATT) ? " ATT" : "");
  2039. seq_printf(s, "sof %04x\n", omap_readw(UDC_SOF));
  2040. tmp = omap_readw(UDC_IRQ_EN);
  2041. seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
  2042. (tmp & UDC_SOF_IE) ? " sof" : "",
  2043. (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
  2044. (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
  2045. (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
  2046. (tmp & UDC_EP0_IE) ? " ep0" : "");
  2047. tmp = omap_readw(UDC_IRQ_SRC);
  2048. seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
  2049. (tmp & UDC_TXN_DONE) ? " txn_done" : "",
  2050. (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
  2051. (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
  2052. (tmp & UDC_IRQ_SOF) ? " sof" : "",
  2053. (tmp & UDC_EPN_RX) ? " epn_rx" : "",
  2054. (tmp & UDC_EPN_TX) ? " epn_tx" : "",
  2055. (tmp & UDC_DS_CHG) ? " ds_chg" : "",
  2056. (tmp & UDC_SETUP) ? " setup" : "",
  2057. (tmp & UDC_EP0_RX) ? " ep0out" : "",
  2058. (tmp & UDC_EP0_TX) ? " ep0in" : "");
  2059. if (use_dma) {
  2060. unsigned i;
  2061. tmp = omap_readw(UDC_DMA_IRQ_EN);
  2062. seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
  2063. (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
  2064. (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
  2065. (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
  2066. (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
  2067. (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
  2068. (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
  2069. (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
  2070. (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
  2071. (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
  2072. tmp = omap_readw(UDC_RXDMA_CFG);
  2073. seq_printf(s, "rxdma_cfg %04x\n", tmp);
  2074. if (tmp) {
  2075. for (i = 0; i < 3; i++) {
  2076. if ((tmp & (0x0f << (i * 4))) == 0)
  2077. continue;
  2078. seq_printf(s, "rxdma[%d] %04x\n", i,
  2079. omap_readw(UDC_RXDMA(i + 1)));
  2080. }
  2081. }
  2082. tmp = omap_readw(UDC_TXDMA_CFG);
  2083. seq_printf(s, "txdma_cfg %04x\n", tmp);
  2084. if (tmp) {
  2085. for (i = 0; i < 3; i++) {
  2086. if (!(tmp & (0x0f << (i * 4))))
  2087. continue;
  2088. seq_printf(s, "txdma[%d] %04x\n", i,
  2089. omap_readw(UDC_TXDMA(i + 1)));
  2090. }
  2091. }
  2092. }
  2093. tmp = omap_readw(UDC_DEVSTAT);
  2094. if (tmp & UDC_ATT) {
  2095. proc_ep_show(s, &udc->ep[0]);
  2096. if (tmp & UDC_ADD) {
  2097. list_for_each_entry(ep, &udc->gadget.ep_list,
  2098. ep.ep_list) {
  2099. if (ep->ep.desc)
  2100. proc_ep_show(s, ep);
  2101. }
  2102. }
  2103. }
  2104. spin_unlock_irqrestore(&udc->lock, flags);
  2105. return 0;
  2106. }
  2107. static void create_proc_file(void)
  2108. {
  2109. proc_create_single(proc_filename, 0, NULL, proc_udc_show);
  2110. }
  2111. static void remove_proc_file(void)
  2112. {
  2113. remove_proc_entry(proc_filename, NULL);
  2114. }
  2115. #else
  2116. static inline void create_proc_file(void) {}
  2117. static inline void remove_proc_file(void) {}
  2118. #endif
  2119. /*-------------------------------------------------------------------------*/
  2120. /* Before this controller can enumerate, we need to pick an endpoint
  2121. * configuration, or "fifo_mode" That involves allocating 2KB of packet
  2122. * buffer space among the endpoints we'll be operating.
  2123. *
  2124. * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
  2125. * UDC_SYSCON_1.CFG_LOCK is set can now work. We won't use that
  2126. * capability yet though.
  2127. */
  2128. static unsigned
  2129. omap_ep_setup(char *name, u8 addr, u8 type,
  2130. unsigned buf, unsigned maxp, int dbuf)
  2131. {
  2132. struct omap_ep *ep;
  2133. u16 epn_rxtx = 0;
  2134. /* OUT endpoints first, then IN */
  2135. ep = &udc->ep[addr & 0xf];
  2136. if (addr & USB_DIR_IN)
  2137. ep += 16;
  2138. /* in case of ep init table bugs */
  2139. BUG_ON(ep->name[0]);
  2140. /* chip setup ... bit values are same for IN, OUT */
  2141. if (type == USB_ENDPOINT_XFER_ISOC) {
  2142. switch (maxp) {
  2143. case 8:
  2144. epn_rxtx = 0 << 12;
  2145. break;
  2146. case 16:
  2147. epn_rxtx = 1 << 12;
  2148. break;
  2149. case 32:
  2150. epn_rxtx = 2 << 12;
  2151. break;
  2152. case 64:
  2153. epn_rxtx = 3 << 12;
  2154. break;
  2155. case 128:
  2156. epn_rxtx = 4 << 12;
  2157. break;
  2158. case 256:
  2159. epn_rxtx = 5 << 12;
  2160. break;
  2161. case 512:
  2162. epn_rxtx = 6 << 12;
  2163. break;
  2164. default:
  2165. BUG();
  2166. }
  2167. epn_rxtx |= UDC_EPN_RX_ISO;
  2168. dbuf = 1;
  2169. } else {
  2170. /* double-buffering "not supported" on 15xx,
  2171. * and ignored for PIO-IN on newer chips
  2172. * (for more reliable behavior)
  2173. */
  2174. if (!use_dma || cpu_is_omap15xx())
  2175. dbuf = 0;
  2176. switch (maxp) {
  2177. case 8:
  2178. epn_rxtx = 0 << 12;
  2179. break;
  2180. case 16:
  2181. epn_rxtx = 1 << 12;
  2182. break;
  2183. case 32:
  2184. epn_rxtx = 2 << 12;
  2185. break;
  2186. case 64:
  2187. epn_rxtx = 3 << 12;
  2188. break;
  2189. default:
  2190. BUG();
  2191. }
  2192. if (dbuf && addr)
  2193. epn_rxtx |= UDC_EPN_RX_DB;
  2194. timer_setup(&ep->timer, pio_out_timer, 0);
  2195. }
  2196. if (addr)
  2197. epn_rxtx |= UDC_EPN_RX_VALID;
  2198. BUG_ON(buf & 0x07);
  2199. epn_rxtx |= buf >> 3;
  2200. DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
  2201. name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
  2202. if (addr & USB_DIR_IN)
  2203. omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
  2204. else
  2205. omap_writew(epn_rxtx, UDC_EP_RX(addr));
  2206. /* next endpoint's buffer starts after this one's */
  2207. buf += maxp;
  2208. if (dbuf)
  2209. buf += maxp;
  2210. BUG_ON(buf > 2048);
  2211. /* set up driver data structures */
  2212. BUG_ON(strlen(name) >= sizeof ep->name);
  2213. strscpy(ep->name, name, sizeof(ep->name));
  2214. INIT_LIST_HEAD(&ep->queue);
  2215. INIT_LIST_HEAD(&ep->iso);
  2216. ep->bEndpointAddress = addr;
  2217. ep->bmAttributes = type;
  2218. ep->double_buf = dbuf;
  2219. ep->udc = udc;
  2220. switch (type) {
  2221. case USB_ENDPOINT_XFER_CONTROL:
  2222. ep->ep.caps.type_control = true;
  2223. ep->ep.caps.dir_in = true;
  2224. ep->ep.caps.dir_out = true;
  2225. break;
  2226. case USB_ENDPOINT_XFER_ISOC:
  2227. ep->ep.caps.type_iso = true;
  2228. break;
  2229. case USB_ENDPOINT_XFER_BULK:
  2230. ep->ep.caps.type_bulk = true;
  2231. break;
  2232. case USB_ENDPOINT_XFER_INT:
  2233. ep->ep.caps.type_int = true;
  2234. break;
  2235. }
  2236. if (addr & USB_DIR_IN)
  2237. ep->ep.caps.dir_in = true;
  2238. else
  2239. ep->ep.caps.dir_out = true;
  2240. ep->ep.name = ep->name;
  2241. ep->ep.ops = &omap_ep_ops;
  2242. ep->maxpacket = maxp;
  2243. usb_ep_set_maxpacket_limit(&ep->ep, ep->maxpacket);
  2244. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  2245. return buf;
  2246. }
  2247. static void omap_udc_release(struct device *dev)
  2248. {
  2249. pullup_disable(udc);
  2250. if (!IS_ERR_OR_NULL(udc->transceiver)) {
  2251. usb_put_phy(udc->transceiver);
  2252. udc->transceiver = NULL;
  2253. }
  2254. omap_writew(0, UDC_SYSCON1);
  2255. remove_proc_file();
  2256. if (udc->dc_clk) {
  2257. if (udc->clk_requested)
  2258. omap_udc_enable_clock(0);
  2259. clk_unprepare(udc->hhc_clk);
  2260. clk_unprepare(udc->dc_clk);
  2261. clk_put(udc->hhc_clk);
  2262. clk_put(udc->dc_clk);
  2263. }
  2264. if (udc->done)
  2265. complete(udc->done);
  2266. kfree(udc);
  2267. }
  2268. static int
  2269. omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
  2270. {
  2271. unsigned tmp, buf;
  2272. /* abolish any previous hardware state */
  2273. omap_writew(0, UDC_SYSCON1);
  2274. omap_writew(0, UDC_IRQ_EN);
  2275. omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
  2276. omap_writew(0, UDC_DMA_IRQ_EN);
  2277. omap_writew(0, UDC_RXDMA_CFG);
  2278. omap_writew(0, UDC_TXDMA_CFG);
  2279. /* UDC_PULLUP_EN gates the chip clock */
  2280. /* OTG_SYSCON_1 |= DEV_IDLE_EN; */
  2281. udc = kzalloc(sizeof(*udc), GFP_KERNEL);
  2282. if (!udc)
  2283. return -ENOMEM;
  2284. spin_lock_init(&udc->lock);
  2285. udc->gadget.ops = &omap_gadget_ops;
  2286. udc->gadget.ep0 = &udc->ep[0].ep;
  2287. INIT_LIST_HEAD(&udc->gadget.ep_list);
  2288. INIT_LIST_HEAD(&udc->iso);
  2289. udc->gadget.speed = USB_SPEED_UNKNOWN;
  2290. udc->gadget.max_speed = USB_SPEED_FULL;
  2291. udc->gadget.name = driver_name;
  2292. udc->gadget.quirk_ep_out_aligned_size = 1;
  2293. udc->transceiver = xceiv;
  2294. /* ep0 is special; put it right after the SETUP buffer */
  2295. buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
  2296. 8 /* after SETUP */, 64 /* maxpacket */, 0);
  2297. list_del_init(&udc->ep[0].ep.ep_list);
  2298. /* initially disable all non-ep0 endpoints */
  2299. for (tmp = 1; tmp < 15; tmp++) {
  2300. omap_writew(0, UDC_EP_RX(tmp));
  2301. omap_writew(0, UDC_EP_TX(tmp));
  2302. }
  2303. #define OMAP_BULK_EP(name, addr) \
  2304. buf = omap_ep_setup(name "-bulk", addr, \
  2305. USB_ENDPOINT_XFER_BULK, buf, 64, 1);
  2306. #define OMAP_INT_EP(name, addr, maxp) \
  2307. buf = omap_ep_setup(name "-int", addr, \
  2308. USB_ENDPOINT_XFER_INT, buf, maxp, 0);
  2309. #define OMAP_ISO_EP(name, addr, maxp) \
  2310. buf = omap_ep_setup(name "-iso", addr, \
  2311. USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
  2312. switch (fifo_mode) {
  2313. case 0:
  2314. OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
  2315. OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
  2316. OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
  2317. break;
  2318. case 1:
  2319. OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
  2320. OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
  2321. OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
  2322. OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
  2323. OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
  2324. OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
  2325. OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
  2326. OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
  2327. OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
  2328. OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
  2329. OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
  2330. OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
  2331. OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
  2332. OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
  2333. OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
  2334. OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
  2335. OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
  2336. OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
  2337. OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
  2338. OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
  2339. OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
  2340. OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
  2341. break;
  2342. #ifdef USE_ISO
  2343. case 2: /* mixed iso/bulk */
  2344. OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
  2345. OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
  2346. OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
  2347. OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
  2348. OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
  2349. OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
  2350. OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
  2351. OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
  2352. break;
  2353. case 3: /* mixed bulk/iso */
  2354. OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
  2355. OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
  2356. OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
  2357. OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
  2358. OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
  2359. OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
  2360. OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
  2361. OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
  2362. OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
  2363. break;
  2364. #endif
  2365. /* add more modes as needed */
  2366. default:
  2367. ERR("unsupported fifo_mode #%d\n", fifo_mode);
  2368. return -ENODEV;
  2369. }
  2370. omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
  2371. INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
  2372. return 0;
  2373. }
  2374. static int omap_udc_probe(struct platform_device *pdev)
  2375. {
  2376. int status = -ENODEV;
  2377. int hmc;
  2378. struct usb_phy *xceiv = NULL;
  2379. const char *type = NULL;
  2380. struct omap_usb_config *config = dev_get_platdata(&pdev->dev);
  2381. struct clk *dc_clk = NULL;
  2382. struct clk *hhc_clk = NULL;
  2383. if (cpu_is_omap7xx())
  2384. use_dma = 0;
  2385. /* NOTE: "knows" the order of the resources! */
  2386. if (!request_mem_region(pdev->resource[0].start,
  2387. resource_size(&pdev->resource[0]),
  2388. driver_name)) {
  2389. DBG("request_mem_region failed\n");
  2390. return -EBUSY;
  2391. }
  2392. if (cpu_is_omap16xx()) {
  2393. dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
  2394. hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
  2395. BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
  2396. /* can't use omap_udc_enable_clock yet */
  2397. clk_prepare_enable(dc_clk);
  2398. clk_prepare_enable(hhc_clk);
  2399. udelay(100);
  2400. }
  2401. if (cpu_is_omap7xx()) {
  2402. dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
  2403. hhc_clk = clk_get(&pdev->dev, "l3_ocpi_ck");
  2404. BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
  2405. /* can't use omap_udc_enable_clock yet */
  2406. clk_prepare_enable(dc_clk);
  2407. clk_prepare_enable(hhc_clk);
  2408. udelay(100);
  2409. }
  2410. INFO("OMAP UDC rev %d.%d%s\n",
  2411. omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
  2412. config->otg ? ", Mini-AB" : "");
  2413. /* use the mode given to us by board init code */
  2414. if (cpu_is_omap15xx()) {
  2415. hmc = HMC_1510;
  2416. type = "(unknown)";
  2417. if (machine_without_vbus_sense()) {
  2418. /* just set up software VBUS detect, and then
  2419. * later rig it so we always report VBUS.
  2420. * FIXME without really sensing VBUS, we can't
  2421. * know when to turn PULLUP_EN on/off; and that
  2422. * means we always "need" the 48MHz clock.
  2423. */
  2424. u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
  2425. tmp &= ~VBUS_CTRL_1510;
  2426. omap_writel(tmp, FUNC_MUX_CTRL_0);
  2427. tmp |= VBUS_MODE_1510;
  2428. tmp &= ~VBUS_CTRL_1510;
  2429. omap_writel(tmp, FUNC_MUX_CTRL_0);
  2430. }
  2431. } else {
  2432. /* The transceiver may package some GPIO logic or handle
  2433. * loopback and/or transceiverless setup; if we find one,
  2434. * use it. Except for OTG, we don't _need_ to talk to one;
  2435. * but not having one probably means no VBUS detection.
  2436. */
  2437. xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
  2438. if (!IS_ERR_OR_NULL(xceiv))
  2439. type = xceiv->label;
  2440. else if (config->otg) {
  2441. DBG("OTG requires external transceiver!\n");
  2442. goto cleanup0;
  2443. }
  2444. hmc = HMC_1610;
  2445. switch (hmc) {
  2446. case 0: /* POWERUP DEFAULT == 0 */
  2447. case 4:
  2448. case 12:
  2449. case 20:
  2450. if (!cpu_is_omap1710()) {
  2451. type = "integrated";
  2452. break;
  2453. }
  2454. fallthrough;
  2455. case 3:
  2456. case 11:
  2457. case 16:
  2458. case 19:
  2459. case 25:
  2460. if (IS_ERR_OR_NULL(xceiv)) {
  2461. DBG("external transceiver not registered!\n");
  2462. type = "unknown";
  2463. }
  2464. break;
  2465. case 21: /* internal loopback */
  2466. type = "loopback";
  2467. break;
  2468. case 14: /* transceiverless */
  2469. if (cpu_is_omap1710())
  2470. goto bad_on_1710;
  2471. fallthrough;
  2472. case 13:
  2473. case 15:
  2474. type = "no";
  2475. break;
  2476. default:
  2477. bad_on_1710:
  2478. ERR("unrecognized UDC HMC mode %d\n", hmc);
  2479. goto cleanup0;
  2480. }
  2481. }
  2482. INFO("hmc mode %d, %s transceiver\n", hmc, type);
  2483. /* a "gadget" abstracts/virtualizes the controller */
  2484. status = omap_udc_setup(pdev, xceiv);
  2485. if (status)
  2486. goto cleanup0;
  2487. xceiv = NULL;
  2488. /* "udc" is now valid */
  2489. pullup_disable(udc);
  2490. #if IS_ENABLED(CONFIG_USB_OHCI_HCD)
  2491. udc->gadget.is_otg = (config->otg != 0);
  2492. #endif
  2493. /* starting with omap1710 es2.0, clear toggle is a separate bit */
  2494. if (omap_readw(UDC_REV) >= 0x61)
  2495. udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
  2496. else
  2497. udc->clr_halt = UDC_RESET_EP;
  2498. /* USB general purpose IRQ: ep0, state changes, dma, etc */
  2499. status = devm_request_irq(&pdev->dev, pdev->resource[1].start,
  2500. omap_udc_irq, 0, driver_name, udc);
  2501. if (status != 0) {
  2502. ERR("can't get irq %d, err %d\n",
  2503. (int) pdev->resource[1].start, status);
  2504. goto cleanup1;
  2505. }
  2506. /* USB "non-iso" IRQ (PIO for all but ep0) */
  2507. status = devm_request_irq(&pdev->dev, pdev->resource[2].start,
  2508. omap_udc_pio_irq, 0, "omap_udc pio", udc);
  2509. if (status != 0) {
  2510. ERR("can't get irq %d, err %d\n",
  2511. (int) pdev->resource[2].start, status);
  2512. goto cleanup1;
  2513. }
  2514. #ifdef USE_ISO
  2515. status = devm_request_irq(&pdev->dev, pdev->resource[3].start,
  2516. omap_udc_iso_irq, 0, "omap_udc iso", udc);
  2517. if (status != 0) {
  2518. ERR("can't get irq %d, err %d\n",
  2519. (int) pdev->resource[3].start, status);
  2520. goto cleanup1;
  2521. }
  2522. #endif
  2523. if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
  2524. udc->dc_clk = dc_clk;
  2525. udc->hhc_clk = hhc_clk;
  2526. clk_disable(hhc_clk);
  2527. clk_disable(dc_clk);
  2528. }
  2529. create_proc_file();
  2530. return usb_add_gadget_udc_release(&pdev->dev, &udc->gadget,
  2531. omap_udc_release);
  2532. cleanup1:
  2533. kfree(udc);
  2534. udc = NULL;
  2535. cleanup0:
  2536. if (!IS_ERR_OR_NULL(xceiv))
  2537. usb_put_phy(xceiv);
  2538. if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
  2539. clk_disable_unprepare(hhc_clk);
  2540. clk_disable_unprepare(dc_clk);
  2541. clk_put(hhc_clk);
  2542. clk_put(dc_clk);
  2543. }
  2544. release_mem_region(pdev->resource[0].start,
  2545. resource_size(&pdev->resource[0]));
  2546. return status;
  2547. }
  2548. static int omap_udc_remove(struct platform_device *pdev)
  2549. {
  2550. DECLARE_COMPLETION_ONSTACK(done);
  2551. udc->done = &done;
  2552. usb_del_gadget_udc(&udc->gadget);
  2553. wait_for_completion(&done);
  2554. release_mem_region(pdev->resource[0].start,
  2555. resource_size(&pdev->resource[0]));
  2556. return 0;
  2557. }
  2558. /* suspend/resume/wakeup from sysfs (echo > power/state) or when the
  2559. * system is forced into deep sleep
  2560. *
  2561. * REVISIT we should probably reject suspend requests when there's a host
  2562. * session active, rather than disconnecting, at least on boards that can
  2563. * report VBUS irqs (UDC_DEVSTAT.UDC_ATT). And in any case, we need to
  2564. * make host resumes and VBUS detection trigger OMAP wakeup events; that
  2565. * may involve talking to an external transceiver (e.g. isp1301).
  2566. */
  2567. static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
  2568. {
  2569. u32 devstat;
  2570. devstat = omap_readw(UDC_DEVSTAT);
  2571. /* we're requesting 48 MHz clock if the pullup is enabled
  2572. * (== we're attached to the host) and we're not suspended,
  2573. * which would prevent entry to deep sleep...
  2574. */
  2575. if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
  2576. WARNING("session active; suspend requires disconnect\n");
  2577. omap_pullup(&udc->gadget, 0);
  2578. }
  2579. return 0;
  2580. }
  2581. static int omap_udc_resume(struct platform_device *dev)
  2582. {
  2583. DBG("resume + wakeup/SRP\n");
  2584. omap_pullup(&udc->gadget, 1);
  2585. /* maybe the host would enumerate us if we nudged it */
  2586. msleep(100);
  2587. return omap_wakeup(&udc->gadget);
  2588. }
  2589. /*-------------------------------------------------------------------------*/
  2590. static struct platform_driver udc_driver = {
  2591. .probe = omap_udc_probe,
  2592. .remove = omap_udc_remove,
  2593. .suspend = omap_udc_suspend,
  2594. .resume = omap_udc_resume,
  2595. .driver = {
  2596. .name = driver_name,
  2597. },
  2598. };
  2599. module_platform_driver(udc_driver);
  2600. MODULE_DESCRIPTION(DRIVER_DESC);
  2601. MODULE_LICENSE("GPL");
  2602. MODULE_ALIAS("platform:omap_udc");