omap_udc.c 80 KB

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