omap_udc.c 77 KB

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