omap_udc.c 80 KB

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