omap_udc.c 76 KB

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