omap_udc.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153
  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 <linux/prefetch.h>
  46. #include <asm/byteorder.h>
  47. #include <asm/io.h>
  48. #include <asm/irq.h>
  49. #include <asm/system.h>
  50. #include <asm/unaligned.h>
  51. #include <asm/mach-types.h>
  52. #include <plat/dma.h>
  53. #include <plat/usb.h>
  54. #include "omap_udc.h"
  55. #undef USB_TRACE
  56. /* bulk DMA seems to be behaving for both IN and OUT */
  57. #define USE_DMA
  58. /* ISO too */
  59. #define USE_ISO
  60. #define DRIVER_DESC "OMAP UDC driver"
  61. #define DRIVER_VERSION "4 October 2004"
  62. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  63. #define OMAP2_DMA_CH(ch) (((ch) - 1) << 1)
  64. #define OMAP24XX_DMA(name, ch) (OMAP24XX_DMA_##name + OMAP2_DMA_CH(ch))
  65. /*
  66. * The OMAP UDC needs _very_ early endpoint setup: before enabling the
  67. * D+ pullup to allow enumeration. That's too early for the gadget
  68. * framework to use from usb_endpoint_enable(), which happens after
  69. * enumeration as part of activating an interface. (But if we add an
  70. * optional new "UDC not yet running" state to the gadget driver model,
  71. * even just during driver binding, the endpoint autoconfig logic is the
  72. * natural spot to manufacture new endpoints.)
  73. *
  74. * So instead of using endpoint enable calls to control the hardware setup,
  75. * this driver defines a "fifo mode" parameter. It's used during driver
  76. * initialization to choose among a set of pre-defined endpoint configs.
  77. * See omap_udc_setup() for available modes, or to add others. That code
  78. * lives in an init section, so use this driver as a module if you need
  79. * to change the fifo mode after the kernel boots.
  80. *
  81. * Gadget drivers normally ignore endpoints they don't care about, and
  82. * won't include them in configuration descriptors. That means only
  83. * misbehaving hosts would even notice they exist.
  84. */
  85. #ifdef USE_ISO
  86. static unsigned fifo_mode = 3;
  87. #else
  88. static unsigned fifo_mode = 0;
  89. #endif
  90. /* "modprobe omap_udc fifo_mode=42", or else as a kernel
  91. * boot parameter "omap_udc:fifo_mode=42"
  92. */
  93. module_param (fifo_mode, uint, 0);
  94. MODULE_PARM_DESC (fifo_mode, "endpoint configuration");
  95. #ifdef USE_DMA
  96. static unsigned use_dma = 1;
  97. /* "modprobe omap_udc use_dma=y", or else as a kernel
  98. * boot parameter "omap_udc:use_dma=y"
  99. */
  100. module_param (use_dma, bool, 0);
  101. MODULE_PARM_DESC (use_dma, "enable/disable DMA");
  102. #else /* !USE_DMA */
  103. /* save a bit of code */
  104. #define use_dma 0
  105. #endif /* !USE_DMA */
  106. static const char driver_name [] = "omap_udc";
  107. static const char driver_desc [] = DRIVER_DESC;
  108. /*-------------------------------------------------------------------------*/
  109. /* there's a notion of "current endpoint" for modifying endpoint
  110. * state, and PIO access to its FIFO.
  111. */
  112. static void use_ep(struct omap_ep *ep, u16 select)
  113. {
  114. u16 num = ep->bEndpointAddress & 0x0f;
  115. if (ep->bEndpointAddress & USB_DIR_IN)
  116. num |= UDC_EP_DIR;
  117. omap_writew(num | select, UDC_EP_NUM);
  118. /* when select, MUST deselect later !! */
  119. }
  120. static inline void deselect_ep(void)
  121. {
  122. u16 w;
  123. w = omap_readw(UDC_EP_NUM);
  124. w &= ~UDC_EP_SEL;
  125. omap_writew(w, UDC_EP_NUM);
  126. /* 6 wait states before TX will happen */
  127. }
  128. static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
  129. /*-------------------------------------------------------------------------*/
  130. static int omap_ep_enable(struct usb_ep *_ep,
  131. const struct usb_endpoint_descriptor *desc)
  132. {
  133. struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
  134. struct omap_udc *udc;
  135. unsigned long flags;
  136. u16 maxp;
  137. /* catch various bogus parameters */
  138. if (!_ep || !desc || ep->desc
  139. || desc->bDescriptorType != USB_DT_ENDPOINT
  140. || ep->bEndpointAddress != desc->bEndpointAddress
  141. || ep->maxpacket < usb_endpoint_maxp(desc)) {
  142. DBG("%s, bad ep or descriptor\n", __func__);
  143. return -EINVAL;
  144. }
  145. maxp = usb_endpoint_maxp(desc);
  146. if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
  147. && maxp != ep->maxpacket)
  148. || usb_endpoint_maxp(desc) > 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 int omap_udc_start(struct usb_gadget_driver *driver,
  1173. int (*bind)(struct usb_gadget *));
  1174. static int omap_udc_stop(struct usb_gadget_driver *driver);
  1175. static struct usb_gadget_ops omap_gadget_ops = {
  1176. .get_frame = omap_get_frame,
  1177. .wakeup = omap_wakeup,
  1178. .set_selfpowered = omap_set_selfpowered,
  1179. .vbus_session = omap_vbus_session,
  1180. .vbus_draw = omap_vbus_draw,
  1181. .pullup = omap_pullup,
  1182. .start = omap_udc_start,
  1183. .stop = omap_udc_stop,
  1184. };
  1185. /*-------------------------------------------------------------------------*/
  1186. /* dequeue ALL requests; caller holds udc->lock */
  1187. static void nuke(struct omap_ep *ep, int status)
  1188. {
  1189. struct omap_req *req;
  1190. ep->stopped = 1;
  1191. if (use_dma && ep->dma_channel)
  1192. dma_channel_release(ep);
  1193. use_ep(ep, 0);
  1194. omap_writew(UDC_CLR_EP, UDC_CTRL);
  1195. if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
  1196. omap_writew(UDC_SET_HALT, UDC_CTRL);
  1197. while (!list_empty(&ep->queue)) {
  1198. req = list_entry(ep->queue.next, struct omap_req, queue);
  1199. done(ep, req, status);
  1200. }
  1201. }
  1202. /* caller holds udc->lock */
  1203. static void udc_quiesce(struct omap_udc *udc)
  1204. {
  1205. struct omap_ep *ep;
  1206. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1207. nuke(&udc->ep[0], -ESHUTDOWN);
  1208. list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list)
  1209. nuke(ep, -ESHUTDOWN);
  1210. }
  1211. /*-------------------------------------------------------------------------*/
  1212. static void update_otg(struct omap_udc *udc)
  1213. {
  1214. u16 devstat;
  1215. if (!gadget_is_otg(&udc->gadget))
  1216. return;
  1217. if (omap_readl(OTG_CTRL) & OTG_ID)
  1218. devstat = omap_readw(UDC_DEVSTAT);
  1219. else
  1220. devstat = 0;
  1221. udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
  1222. udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
  1223. udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
  1224. /* Enable HNP early, avoiding races on suspend irq path.
  1225. * ASSUMES OTG state machine B_BUS_REQ input is true.
  1226. */
  1227. if (udc->gadget.b_hnp_enable) {
  1228. u32 l;
  1229. l = omap_readl(OTG_CTRL);
  1230. l |= OTG_B_HNPEN | OTG_B_BUSREQ;
  1231. l &= ~OTG_PULLUP;
  1232. omap_writel(l, OTG_CTRL);
  1233. }
  1234. }
  1235. static void ep0_irq(struct omap_udc *udc, u16 irq_src)
  1236. {
  1237. struct omap_ep *ep0 = &udc->ep[0];
  1238. struct omap_req *req = NULL;
  1239. ep0->irqs++;
  1240. /* Clear any pending requests and then scrub any rx/tx state
  1241. * before starting to handle the SETUP request.
  1242. */
  1243. if (irq_src & UDC_SETUP) {
  1244. u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
  1245. nuke(ep0, 0);
  1246. if (ack) {
  1247. omap_writew(ack, UDC_IRQ_SRC);
  1248. irq_src = UDC_SETUP;
  1249. }
  1250. }
  1251. /* IN/OUT packets mean we're in the DATA or STATUS stage.
  1252. * This driver uses only uses protocol stalls (ep0 never halts),
  1253. * and if we got this far the gadget driver already had a
  1254. * chance to stall. Tries to be forgiving of host oddities.
  1255. *
  1256. * NOTE: the last chance gadget drivers have to stall control
  1257. * requests is during their request completion callback.
  1258. */
  1259. if (!list_empty(&ep0->queue))
  1260. req = container_of(ep0->queue.next, struct omap_req, queue);
  1261. /* IN == TX to host */
  1262. if (irq_src & UDC_EP0_TX) {
  1263. int stat;
  1264. omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
  1265. omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
  1266. stat = omap_readw(UDC_STAT_FLG);
  1267. if (stat & UDC_ACK) {
  1268. if (udc->ep0_in) {
  1269. /* write next IN packet from response,
  1270. * or set up the status stage.
  1271. */
  1272. if (req)
  1273. stat = write_fifo(ep0, req);
  1274. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1275. if (!req && udc->ep0_pending) {
  1276. omap_writew(UDC_EP_SEL, UDC_EP_NUM);
  1277. omap_writew(UDC_CLR_EP, UDC_CTRL);
  1278. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1279. omap_writew(0, UDC_EP_NUM);
  1280. udc->ep0_pending = 0;
  1281. } /* else: 6 wait states before it'll tx */
  1282. } else {
  1283. /* ack status stage of OUT transfer */
  1284. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1285. if (req)
  1286. done(ep0, req, 0);
  1287. }
  1288. req = NULL;
  1289. } else if (stat & UDC_STALL) {
  1290. omap_writew(UDC_CLR_HALT, UDC_CTRL);
  1291. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1292. } else {
  1293. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1294. }
  1295. }
  1296. /* OUT == RX from host */
  1297. if (irq_src & UDC_EP0_RX) {
  1298. int stat;
  1299. omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
  1300. omap_writew(UDC_EP_SEL, UDC_EP_NUM);
  1301. stat = omap_readw(UDC_STAT_FLG);
  1302. if (stat & UDC_ACK) {
  1303. if (!udc->ep0_in) {
  1304. stat = 0;
  1305. /* read next OUT packet of request, maybe
  1306. * reactiviting the fifo; stall on errors.
  1307. */
  1308. if (!req || (stat = read_fifo(ep0, req)) < 0) {
  1309. omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
  1310. udc->ep0_pending = 0;
  1311. stat = 0;
  1312. } else if (stat == 0)
  1313. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1314. omap_writew(0, UDC_EP_NUM);
  1315. /* activate status stage */
  1316. if (stat == 1) {
  1317. done(ep0, req, 0);
  1318. /* that may have STALLed ep0... */
  1319. omap_writew(UDC_EP_SEL | UDC_EP_DIR,
  1320. UDC_EP_NUM);
  1321. omap_writew(UDC_CLR_EP, UDC_CTRL);
  1322. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1323. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1324. udc->ep0_pending = 0;
  1325. }
  1326. } else {
  1327. /* ack status stage of IN transfer */
  1328. omap_writew(0, UDC_EP_NUM);
  1329. if (req)
  1330. done(ep0, req, 0);
  1331. }
  1332. } else if (stat & UDC_STALL) {
  1333. omap_writew(UDC_CLR_HALT, UDC_CTRL);
  1334. omap_writew(0, UDC_EP_NUM);
  1335. } else {
  1336. omap_writew(0, UDC_EP_NUM);
  1337. }
  1338. }
  1339. /* SETUP starts all control transfers */
  1340. if (irq_src & UDC_SETUP) {
  1341. union u {
  1342. u16 word[4];
  1343. struct usb_ctrlrequest r;
  1344. } u;
  1345. int status = -EINVAL;
  1346. struct omap_ep *ep;
  1347. /* read the (latest) SETUP message */
  1348. do {
  1349. omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
  1350. /* two bytes at a time */
  1351. u.word[0] = omap_readw(UDC_DATA);
  1352. u.word[1] = omap_readw(UDC_DATA);
  1353. u.word[2] = omap_readw(UDC_DATA);
  1354. u.word[3] = omap_readw(UDC_DATA);
  1355. omap_writew(0, UDC_EP_NUM);
  1356. } while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
  1357. #define w_value le16_to_cpu(u.r.wValue)
  1358. #define w_index le16_to_cpu(u.r.wIndex)
  1359. #define w_length le16_to_cpu(u.r.wLength)
  1360. /* Delegate almost all control requests to the gadget driver,
  1361. * except for a handful of ch9 status/feature requests that
  1362. * hardware doesn't autodecode _and_ the gadget API hides.
  1363. */
  1364. udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
  1365. udc->ep0_set_config = 0;
  1366. udc->ep0_pending = 1;
  1367. ep0->stopped = 0;
  1368. ep0->ackwait = 0;
  1369. switch (u.r.bRequest) {
  1370. case USB_REQ_SET_CONFIGURATION:
  1371. /* udc needs to know when ep != 0 is valid */
  1372. if (u.r.bRequestType != USB_RECIP_DEVICE)
  1373. goto delegate;
  1374. if (w_length != 0)
  1375. goto do_stall;
  1376. udc->ep0_set_config = 1;
  1377. udc->ep0_reset_config = (w_value == 0);
  1378. VDBG("set config %d\n", w_value);
  1379. /* update udc NOW since gadget driver may start
  1380. * queueing requests immediately; clear config
  1381. * later if it fails the request.
  1382. */
  1383. if (udc->ep0_reset_config)
  1384. omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
  1385. else
  1386. omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
  1387. update_otg(udc);
  1388. goto delegate;
  1389. case USB_REQ_CLEAR_FEATURE:
  1390. /* clear endpoint halt */
  1391. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  1392. goto delegate;
  1393. if (w_value != USB_ENDPOINT_HALT
  1394. || w_length != 0)
  1395. goto do_stall;
  1396. ep = &udc->ep[w_index & 0xf];
  1397. if (ep != ep0) {
  1398. if (w_index & USB_DIR_IN)
  1399. ep += 16;
  1400. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  1401. || !ep->desc)
  1402. goto do_stall;
  1403. use_ep(ep, 0);
  1404. omap_writew(udc->clr_halt, UDC_CTRL);
  1405. ep->ackwait = 0;
  1406. if (!(ep->bEndpointAddress & USB_DIR_IN)) {
  1407. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1408. ep->ackwait = 1 + ep->double_buf;
  1409. }
  1410. /* NOTE: assumes the host behaves sanely,
  1411. * only clearing real halts. Else we may
  1412. * need to kill pending transfers and then
  1413. * restart the queue... very messy for DMA!
  1414. */
  1415. }
  1416. VDBG("%s halt cleared by host\n", ep->name);
  1417. goto ep0out_status_stage;
  1418. case USB_REQ_SET_FEATURE:
  1419. /* set endpoint halt */
  1420. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  1421. goto delegate;
  1422. if (w_value != USB_ENDPOINT_HALT
  1423. || w_length != 0)
  1424. goto do_stall;
  1425. ep = &udc->ep[w_index & 0xf];
  1426. if (w_index & USB_DIR_IN)
  1427. ep += 16;
  1428. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  1429. || ep == ep0 || !ep->desc)
  1430. goto do_stall;
  1431. if (use_dma && ep->has_dma) {
  1432. /* this has rude side-effects (aborts) and
  1433. * can't really work if DMA-IN is active
  1434. */
  1435. DBG("%s host set_halt, NYET \n", ep->name);
  1436. goto do_stall;
  1437. }
  1438. use_ep(ep, 0);
  1439. /* can't halt if fifo isn't empty... */
  1440. omap_writew(UDC_CLR_EP, UDC_CTRL);
  1441. omap_writew(UDC_SET_HALT, UDC_CTRL);
  1442. VDBG("%s halted by host\n", ep->name);
  1443. ep0out_status_stage:
  1444. status = 0;
  1445. omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
  1446. omap_writew(UDC_CLR_EP, UDC_CTRL);
  1447. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1448. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1449. udc->ep0_pending = 0;
  1450. break;
  1451. case USB_REQ_GET_STATUS:
  1452. /* USB_ENDPOINT_HALT status? */
  1453. if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
  1454. goto intf_status;
  1455. /* ep0 never stalls */
  1456. if (!(w_index & 0xf))
  1457. goto zero_status;
  1458. /* only active endpoints count */
  1459. ep = &udc->ep[w_index & 0xf];
  1460. if (w_index & USB_DIR_IN)
  1461. ep += 16;
  1462. if (!ep->desc)
  1463. goto do_stall;
  1464. /* iso never stalls */
  1465. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
  1466. goto zero_status;
  1467. /* FIXME don't assume non-halted endpoints!! */
  1468. ERR("%s status, can't report\n", ep->ep.name);
  1469. goto do_stall;
  1470. intf_status:
  1471. /* return interface status. if we were pedantic,
  1472. * we'd detect non-existent interfaces, and stall.
  1473. */
  1474. if (u.r.bRequestType
  1475. != (USB_DIR_IN|USB_RECIP_INTERFACE))
  1476. goto delegate;
  1477. zero_status:
  1478. /* return two zero bytes */
  1479. omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
  1480. omap_writew(0, UDC_DATA);
  1481. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1482. omap_writew(UDC_EP_DIR, UDC_EP_NUM);
  1483. status = 0;
  1484. VDBG("GET_STATUS, interface %d\n", w_index);
  1485. /* next, status stage */
  1486. break;
  1487. default:
  1488. delegate:
  1489. /* activate the ep0out fifo right away */
  1490. if (!udc->ep0_in && w_length) {
  1491. omap_writew(0, UDC_EP_NUM);
  1492. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1493. }
  1494. /* gadget drivers see class/vendor specific requests,
  1495. * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
  1496. * and more
  1497. */
  1498. VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
  1499. u.r.bRequestType, u.r.bRequest,
  1500. w_value, w_index, w_length);
  1501. #undef w_value
  1502. #undef w_index
  1503. #undef w_length
  1504. /* The gadget driver may return an error here,
  1505. * causing an immediate protocol stall.
  1506. *
  1507. * Else it must issue a response, either queueing a
  1508. * response buffer for the DATA stage, or halting ep0
  1509. * (causing a protocol stall, not a real halt). A
  1510. * zero length buffer means no DATA stage.
  1511. *
  1512. * It's fine to issue that response after the setup()
  1513. * call returns, and this IRQ was handled.
  1514. */
  1515. udc->ep0_setup = 1;
  1516. spin_unlock(&udc->lock);
  1517. status = udc->driver->setup (&udc->gadget, &u.r);
  1518. spin_lock(&udc->lock);
  1519. udc->ep0_setup = 0;
  1520. }
  1521. if (status < 0) {
  1522. do_stall:
  1523. VDBG("req %02x.%02x protocol STALL; stat %d\n",
  1524. u.r.bRequestType, u.r.bRequest, status);
  1525. if (udc->ep0_set_config) {
  1526. if (udc->ep0_reset_config)
  1527. WARNING("error resetting config?\n");
  1528. else
  1529. omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
  1530. }
  1531. omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
  1532. udc->ep0_pending = 0;
  1533. }
  1534. }
  1535. }
  1536. /*-------------------------------------------------------------------------*/
  1537. #define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
  1538. static void devstate_irq(struct omap_udc *udc, u16 irq_src)
  1539. {
  1540. u16 devstat, change;
  1541. devstat = omap_readw(UDC_DEVSTAT);
  1542. change = devstat ^ udc->devstat;
  1543. udc->devstat = devstat;
  1544. if (change & (UDC_USB_RESET|UDC_ATT)) {
  1545. udc_quiesce(udc);
  1546. if (change & UDC_ATT) {
  1547. /* driver for any external transceiver will
  1548. * have called omap_vbus_session() already
  1549. */
  1550. if (devstat & UDC_ATT) {
  1551. udc->gadget.speed = USB_SPEED_FULL;
  1552. VDBG("connect\n");
  1553. if (!udc->transceiver)
  1554. pullup_enable(udc);
  1555. // if (driver->connect) call it
  1556. } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  1557. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1558. if (!udc->transceiver)
  1559. pullup_disable(udc);
  1560. DBG("disconnect, gadget %s\n",
  1561. udc->driver->driver.name);
  1562. if (udc->driver->disconnect) {
  1563. spin_unlock(&udc->lock);
  1564. udc->driver->disconnect(&udc->gadget);
  1565. spin_lock(&udc->lock);
  1566. }
  1567. }
  1568. change &= ~UDC_ATT;
  1569. }
  1570. if (change & UDC_USB_RESET) {
  1571. if (devstat & UDC_USB_RESET) {
  1572. VDBG("RESET=1\n");
  1573. } else {
  1574. udc->gadget.speed = USB_SPEED_FULL;
  1575. INFO("USB reset done, gadget %s\n",
  1576. udc->driver->driver.name);
  1577. /* ep0 traffic is legal from now on */
  1578. omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
  1579. UDC_IRQ_EN);
  1580. }
  1581. change &= ~UDC_USB_RESET;
  1582. }
  1583. }
  1584. if (change & UDC_SUS) {
  1585. if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  1586. // FIXME tell isp1301 to suspend/resume (?)
  1587. if (devstat & UDC_SUS) {
  1588. VDBG("suspend\n");
  1589. update_otg(udc);
  1590. /* HNP could be under way already */
  1591. if (udc->gadget.speed == USB_SPEED_FULL
  1592. && udc->driver->suspend) {
  1593. spin_unlock(&udc->lock);
  1594. udc->driver->suspend(&udc->gadget);
  1595. spin_lock(&udc->lock);
  1596. }
  1597. if (udc->transceiver)
  1598. otg_set_suspend(udc->transceiver, 1);
  1599. } else {
  1600. VDBG("resume\n");
  1601. if (udc->transceiver)
  1602. otg_set_suspend(udc->transceiver, 0);
  1603. if (udc->gadget.speed == USB_SPEED_FULL
  1604. && udc->driver->resume) {
  1605. spin_unlock(&udc->lock);
  1606. udc->driver->resume(&udc->gadget);
  1607. spin_lock(&udc->lock);
  1608. }
  1609. }
  1610. }
  1611. change &= ~UDC_SUS;
  1612. }
  1613. if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
  1614. update_otg(udc);
  1615. change &= ~OTG_FLAGS;
  1616. }
  1617. change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
  1618. if (change)
  1619. VDBG("devstat %03x, ignore change %03x\n",
  1620. devstat, change);
  1621. omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
  1622. }
  1623. static irqreturn_t omap_udc_irq(int irq, void *_udc)
  1624. {
  1625. struct omap_udc *udc = _udc;
  1626. u16 irq_src;
  1627. irqreturn_t status = IRQ_NONE;
  1628. unsigned long flags;
  1629. spin_lock_irqsave(&udc->lock, flags);
  1630. irq_src = omap_readw(UDC_IRQ_SRC);
  1631. /* Device state change (usb ch9 stuff) */
  1632. if (irq_src & UDC_DS_CHG) {
  1633. devstate_irq(_udc, irq_src);
  1634. status = IRQ_HANDLED;
  1635. irq_src &= ~UDC_DS_CHG;
  1636. }
  1637. /* EP0 control transfers */
  1638. if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
  1639. ep0_irq(_udc, irq_src);
  1640. status = IRQ_HANDLED;
  1641. irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
  1642. }
  1643. /* DMA transfer completion */
  1644. if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
  1645. dma_irq(_udc, irq_src);
  1646. status = IRQ_HANDLED;
  1647. irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
  1648. }
  1649. irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
  1650. if (irq_src)
  1651. DBG("udc_irq, unhandled %03x\n", irq_src);
  1652. spin_unlock_irqrestore(&udc->lock, flags);
  1653. return status;
  1654. }
  1655. /* workaround for seemingly-lost IRQs for RX ACKs... */
  1656. #define PIO_OUT_TIMEOUT (jiffies + HZ/3)
  1657. #define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
  1658. static void pio_out_timer(unsigned long _ep)
  1659. {
  1660. struct omap_ep *ep = (void *) _ep;
  1661. unsigned long flags;
  1662. u16 stat_flg;
  1663. spin_lock_irqsave(&ep->udc->lock, flags);
  1664. if (!list_empty(&ep->queue) && ep->ackwait) {
  1665. use_ep(ep, UDC_EP_SEL);
  1666. stat_flg = omap_readw(UDC_STAT_FLG);
  1667. if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
  1668. || (ep->double_buf && HALF_FULL(stat_flg)))) {
  1669. struct omap_req *req;
  1670. VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
  1671. req = container_of(ep->queue.next,
  1672. struct omap_req, queue);
  1673. (void) read_fifo(ep, req);
  1674. omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
  1675. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1676. ep->ackwait = 1 + ep->double_buf;
  1677. } else
  1678. deselect_ep();
  1679. }
  1680. mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
  1681. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1682. }
  1683. static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
  1684. {
  1685. u16 epn_stat, irq_src;
  1686. irqreturn_t status = IRQ_NONE;
  1687. struct omap_ep *ep;
  1688. int epnum;
  1689. struct omap_udc *udc = _dev;
  1690. struct omap_req *req;
  1691. unsigned long flags;
  1692. spin_lock_irqsave(&udc->lock, flags);
  1693. epn_stat = omap_readw(UDC_EPN_STAT);
  1694. irq_src = omap_readw(UDC_IRQ_SRC);
  1695. /* handle OUT first, to avoid some wasteful NAKs */
  1696. if (irq_src & UDC_EPN_RX) {
  1697. epnum = (epn_stat >> 8) & 0x0f;
  1698. omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
  1699. status = IRQ_HANDLED;
  1700. ep = &udc->ep[epnum];
  1701. ep->irqs++;
  1702. omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
  1703. ep->fnf = 0;
  1704. if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
  1705. ep->ackwait--;
  1706. if (!list_empty(&ep->queue)) {
  1707. int stat;
  1708. req = container_of(ep->queue.next,
  1709. struct omap_req, queue);
  1710. stat = read_fifo(ep, req);
  1711. if (!ep->double_buf)
  1712. ep->fnf = 1;
  1713. }
  1714. }
  1715. /* min 6 clock delay before clearing EP_SEL ... */
  1716. epn_stat = omap_readw(UDC_EPN_STAT);
  1717. epn_stat = omap_readw(UDC_EPN_STAT);
  1718. omap_writew(epnum, UDC_EP_NUM);
  1719. /* enabling fifo _after_ clearing ACK, contrary to docs,
  1720. * reduces lossage; timer still needed though (sigh).
  1721. */
  1722. if (ep->fnf) {
  1723. omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
  1724. ep->ackwait = 1 + ep->double_buf;
  1725. }
  1726. mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
  1727. }
  1728. /* then IN transfers */
  1729. else if (irq_src & UDC_EPN_TX) {
  1730. epnum = epn_stat & 0x0f;
  1731. omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
  1732. status = IRQ_HANDLED;
  1733. ep = &udc->ep[16 + epnum];
  1734. ep->irqs++;
  1735. omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
  1736. if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
  1737. ep->ackwait = 0;
  1738. if (!list_empty(&ep->queue)) {
  1739. req = container_of(ep->queue.next,
  1740. struct omap_req, queue);
  1741. (void) write_fifo(ep, req);
  1742. }
  1743. }
  1744. /* min 6 clock delay before clearing EP_SEL ... */
  1745. epn_stat = omap_readw(UDC_EPN_STAT);
  1746. epn_stat = omap_readw(UDC_EPN_STAT);
  1747. omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
  1748. /* then 6 clocks before it'd tx */
  1749. }
  1750. spin_unlock_irqrestore(&udc->lock, flags);
  1751. return status;
  1752. }
  1753. #ifdef USE_ISO
  1754. static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
  1755. {
  1756. struct omap_udc *udc = _dev;
  1757. struct omap_ep *ep;
  1758. int pending = 0;
  1759. unsigned long flags;
  1760. spin_lock_irqsave(&udc->lock, flags);
  1761. /* handle all non-DMA ISO transfers */
  1762. list_for_each_entry (ep, &udc->iso, iso) {
  1763. u16 stat;
  1764. struct omap_req *req;
  1765. if (ep->has_dma || list_empty(&ep->queue))
  1766. continue;
  1767. req = list_entry(ep->queue.next, struct omap_req, queue);
  1768. use_ep(ep, UDC_EP_SEL);
  1769. stat = omap_readw(UDC_STAT_FLG);
  1770. /* NOTE: like the other controller drivers, this isn't
  1771. * currently reporting lost or damaged frames.
  1772. */
  1773. if (ep->bEndpointAddress & USB_DIR_IN) {
  1774. if (stat & UDC_MISS_IN)
  1775. /* done(ep, req, -EPROTO) */;
  1776. else
  1777. write_fifo(ep, req);
  1778. } else {
  1779. int status = 0;
  1780. if (stat & UDC_NO_RXPACKET)
  1781. status = -EREMOTEIO;
  1782. else if (stat & UDC_ISO_ERR)
  1783. status = -EILSEQ;
  1784. else if (stat & UDC_DATA_FLUSH)
  1785. status = -ENOSR;
  1786. if (status)
  1787. /* done(ep, req, status) */;
  1788. else
  1789. read_fifo(ep, req);
  1790. }
  1791. deselect_ep();
  1792. /* 6 wait states before next EP */
  1793. ep->irqs++;
  1794. if (!list_empty(&ep->queue))
  1795. pending = 1;
  1796. }
  1797. if (!pending) {
  1798. u16 w;
  1799. w = omap_readw(UDC_IRQ_EN);
  1800. w &= ~UDC_SOF_IE;
  1801. omap_writew(w, UDC_IRQ_EN);
  1802. }
  1803. omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
  1804. spin_unlock_irqrestore(&udc->lock, flags);
  1805. return IRQ_HANDLED;
  1806. }
  1807. #endif
  1808. /*-------------------------------------------------------------------------*/
  1809. static inline int machine_without_vbus_sense(void)
  1810. {
  1811. return (machine_is_omap_innovator()
  1812. || machine_is_omap_osk()
  1813. || machine_is_omap_apollon()
  1814. #ifndef CONFIG_MACH_OMAP_H4_OTG
  1815. || machine_is_omap_h4()
  1816. #endif
  1817. || machine_is_sx1()
  1818. || cpu_is_omap7xx() /* No known omap7xx boards with vbus sense */
  1819. );
  1820. }
  1821. static int omap_udc_start(struct usb_gadget_driver *driver,
  1822. int (*bind)(struct usb_gadget *))
  1823. {
  1824. int status = -ENODEV;
  1825. struct omap_ep *ep;
  1826. unsigned long flags;
  1827. /* basic sanity tests */
  1828. if (!udc)
  1829. return -ENODEV;
  1830. if (!driver
  1831. // FIXME if otg, check: driver->is_otg
  1832. || driver->speed < USB_SPEED_FULL
  1833. || !bind || !driver->setup)
  1834. return -EINVAL;
  1835. spin_lock_irqsave(&udc->lock, flags);
  1836. if (udc->driver) {
  1837. spin_unlock_irqrestore(&udc->lock, flags);
  1838. return -EBUSY;
  1839. }
  1840. /* reset state */
  1841. list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
  1842. ep->irqs = 0;
  1843. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
  1844. continue;
  1845. use_ep(ep, 0);
  1846. omap_writew(UDC_SET_HALT, UDC_CTRL);
  1847. }
  1848. udc->ep0_pending = 0;
  1849. udc->ep[0].irqs = 0;
  1850. udc->softconnect = 1;
  1851. /* hook up the driver */
  1852. driver->driver.bus = NULL;
  1853. udc->driver = driver;
  1854. udc->gadget.dev.driver = &driver->driver;
  1855. spin_unlock_irqrestore(&udc->lock, flags);
  1856. if (udc->dc_clk != NULL)
  1857. omap_udc_enable_clock(1);
  1858. status = bind(&udc->gadget);
  1859. if (status) {
  1860. DBG("bind to %s --> %d\n", driver->driver.name, status);
  1861. udc->gadget.dev.driver = NULL;
  1862. udc->driver = NULL;
  1863. goto done;
  1864. }
  1865. DBG("bound to driver %s\n", driver->driver.name);
  1866. omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
  1867. /* connect to bus through transceiver */
  1868. if (udc->transceiver) {
  1869. status = otg_set_peripheral(udc->transceiver, &udc->gadget);
  1870. if (status < 0) {
  1871. ERR("can't bind to transceiver\n");
  1872. if (driver->unbind) {
  1873. driver->unbind (&udc->gadget);
  1874. udc->gadget.dev.driver = NULL;
  1875. udc->driver = NULL;
  1876. }
  1877. goto done;
  1878. }
  1879. } else {
  1880. if (can_pullup(udc))
  1881. pullup_enable (udc);
  1882. else
  1883. pullup_disable (udc);
  1884. }
  1885. /* boards that don't have VBUS sensing can't autogate 48MHz;
  1886. * can't enter deep sleep while a gadget driver is active.
  1887. */
  1888. if (machine_without_vbus_sense())
  1889. omap_vbus_session(&udc->gadget, 1);
  1890. done:
  1891. if (udc->dc_clk != NULL)
  1892. omap_udc_enable_clock(0);
  1893. return status;
  1894. }
  1895. static int omap_udc_stop(struct usb_gadget_driver *driver)
  1896. {
  1897. unsigned long flags;
  1898. int status = -ENODEV;
  1899. if (!udc)
  1900. return -ENODEV;
  1901. if (!driver || driver != udc->driver || !driver->unbind)
  1902. return -EINVAL;
  1903. if (udc->dc_clk != NULL)
  1904. omap_udc_enable_clock(1);
  1905. if (machine_without_vbus_sense())
  1906. omap_vbus_session(&udc->gadget, 0);
  1907. if (udc->transceiver)
  1908. (void) otg_set_peripheral(udc->transceiver, NULL);
  1909. else
  1910. pullup_disable(udc);
  1911. spin_lock_irqsave(&udc->lock, flags);
  1912. udc_quiesce(udc);
  1913. spin_unlock_irqrestore(&udc->lock, flags);
  1914. driver->unbind(&udc->gadget);
  1915. udc->gadget.dev.driver = NULL;
  1916. udc->driver = NULL;
  1917. if (udc->dc_clk != NULL)
  1918. omap_udc_enable_clock(0);
  1919. DBG("unregistered driver '%s'\n", driver->driver.name);
  1920. return status;
  1921. }
  1922. /*-------------------------------------------------------------------------*/
  1923. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1924. #include <linux/seq_file.h>
  1925. static const char proc_filename[] = "driver/udc";
  1926. #define FOURBITS "%s%s%s%s"
  1927. #define EIGHTBITS FOURBITS FOURBITS
  1928. static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
  1929. {
  1930. u16 stat_flg;
  1931. struct omap_req *req;
  1932. char buf[20];
  1933. use_ep(ep, 0);
  1934. if (use_dma && ep->has_dma)
  1935. snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
  1936. (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
  1937. ep->dma_channel - 1, ep->lch);
  1938. else
  1939. buf[0] = 0;
  1940. stat_flg = omap_readw(UDC_STAT_FLG);
  1941. seq_printf(s,
  1942. "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
  1943. ep->name, buf,
  1944. ep->double_buf ? "dbuf " : "",
  1945. ({char *s; switch(ep->ackwait){
  1946. case 0: s = ""; break;
  1947. case 1: s = "(ackw) "; break;
  1948. case 2: s = "(ackw2) "; break;
  1949. default: s = "(?) "; break;
  1950. } s;}),
  1951. ep->irqs, stat_flg,
  1952. (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
  1953. (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
  1954. (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
  1955. (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
  1956. (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
  1957. (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
  1958. (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
  1959. (stat_flg & UDC_STALL) ? "STALL " : "",
  1960. (stat_flg & UDC_NAK) ? "NAK " : "",
  1961. (stat_flg & UDC_ACK) ? "ACK " : "",
  1962. (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
  1963. (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
  1964. (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
  1965. if (list_empty (&ep->queue))
  1966. seq_printf(s, "\t(queue empty)\n");
  1967. else
  1968. list_for_each_entry (req, &ep->queue, queue) {
  1969. unsigned length = req->req.actual;
  1970. if (use_dma && buf[0]) {
  1971. length += ((ep->bEndpointAddress & USB_DIR_IN)
  1972. ? dma_src_len : dma_dest_len)
  1973. (ep, req->req.dma + length);
  1974. buf[0] = 0;
  1975. }
  1976. seq_printf(s, "\treq %p len %d/%d buf %p\n",
  1977. &req->req, length,
  1978. req->req.length, req->req.buf);
  1979. }
  1980. }
  1981. static char *trx_mode(unsigned m, int enabled)
  1982. {
  1983. switch (m) {
  1984. case 0: return enabled ? "*6wire" : "unused";
  1985. case 1: return "4wire";
  1986. case 2: return "3wire";
  1987. case 3: return "6wire";
  1988. default: return "unknown";
  1989. }
  1990. }
  1991. static int proc_otg_show(struct seq_file *s)
  1992. {
  1993. u32 tmp;
  1994. u32 trans = 0;
  1995. char *ctrl_name = "(UNKNOWN)";
  1996. /* XXX This needs major revision for OMAP2+ */
  1997. tmp = omap_readl(OTG_REV);
  1998. if (cpu_class_is_omap1()) {
  1999. ctrl_name = "tranceiver_ctrl";
  2000. trans = omap_readw(USB_TRANSCEIVER_CTRL);
  2001. }
  2002. seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
  2003. tmp >> 4, tmp & 0xf, ctrl_name, trans);
  2004. tmp = omap_readw(OTG_SYSCON_1);
  2005. seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
  2006. FOURBITS "\n", tmp,
  2007. trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
  2008. trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
  2009. (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
  2010. ? "internal"
  2011. : trx_mode(USB0_TRX_MODE(tmp), 1),
  2012. (tmp & OTG_IDLE_EN) ? " !otg" : "",
  2013. (tmp & HST_IDLE_EN) ? " !host" : "",
  2014. (tmp & DEV_IDLE_EN) ? " !dev" : "",
  2015. (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
  2016. tmp = omap_readl(OTG_SYSCON_2);
  2017. seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
  2018. " b_ase_brst=%d hmc=%d\n", tmp,
  2019. (tmp & OTG_EN) ? " otg_en" : "",
  2020. (tmp & USBX_SYNCHRO) ? " synchro" : "",
  2021. // much more SRP stuff
  2022. (tmp & SRP_DATA) ? " srp_data" : "",
  2023. (tmp & SRP_VBUS) ? " srp_vbus" : "",
  2024. (tmp & OTG_PADEN) ? " otg_paden" : "",
  2025. (tmp & HMC_PADEN) ? " hmc_paden" : "",
  2026. (tmp & UHOST_EN) ? " uhost_en" : "",
  2027. (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
  2028. (tmp & HMC_TLLATTACH) ? " tllattach" : "",
  2029. B_ASE_BRST(tmp),
  2030. OTG_HMC(tmp));
  2031. tmp = omap_readl(OTG_CTRL);
  2032. seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
  2033. (tmp & OTG_ASESSVLD) ? " asess" : "",
  2034. (tmp & OTG_BSESSEND) ? " bsess_end" : "",
  2035. (tmp & OTG_BSESSVLD) ? " bsess" : "",
  2036. (tmp & OTG_VBUSVLD) ? " vbus" : "",
  2037. (tmp & OTG_ID) ? " id" : "",
  2038. (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
  2039. (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
  2040. (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
  2041. (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
  2042. (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
  2043. (tmp & OTG_BUSDROP) ? " busdrop" : "",
  2044. (tmp & OTG_PULLDOWN) ? " down" : "",
  2045. (tmp & OTG_PULLUP) ? " up" : "",
  2046. (tmp & OTG_DRV_VBUS) ? " drv" : "",
  2047. (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
  2048. (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
  2049. (tmp & OTG_PU_ID) ? " pu_id" : ""
  2050. );
  2051. tmp = omap_readw(OTG_IRQ_EN);
  2052. seq_printf(s, "otg_irq_en %04x" "\n", tmp);
  2053. tmp = omap_readw(OTG_IRQ_SRC);
  2054. seq_printf(s, "otg_irq_src %04x" "\n", tmp);
  2055. tmp = omap_readw(OTG_OUTCTRL);
  2056. seq_printf(s, "otg_outctrl %04x" "\n", tmp);
  2057. tmp = omap_readw(OTG_TEST);
  2058. seq_printf(s, "otg_test %04x" "\n", tmp);
  2059. return 0;
  2060. }
  2061. static int proc_udc_show(struct seq_file *s, void *_)
  2062. {
  2063. u32 tmp;
  2064. struct omap_ep *ep;
  2065. unsigned long flags;
  2066. spin_lock_irqsave(&udc->lock, flags);
  2067. seq_printf(s, "%s, version: " DRIVER_VERSION
  2068. #ifdef USE_ISO
  2069. " (iso)"
  2070. #endif
  2071. "%s\n",
  2072. driver_desc,
  2073. use_dma ? " (dma)" : "");
  2074. tmp = omap_readw(UDC_REV) & 0xff;
  2075. seq_printf(s,
  2076. "UDC rev %d.%d, fifo mode %d, gadget %s\n"
  2077. "hmc %d, transceiver %s\n",
  2078. tmp >> 4, tmp & 0xf,
  2079. fifo_mode,
  2080. udc->driver ? udc->driver->driver.name : "(none)",
  2081. HMC,
  2082. udc->transceiver
  2083. ? udc->transceiver->label
  2084. : ((cpu_is_omap1710() || cpu_is_omap24xx())
  2085. ? "external" : "(none)"));
  2086. if (cpu_class_is_omap1()) {
  2087. seq_printf(s, "ULPD control %04x req %04x status %04x\n",
  2088. omap_readw(ULPD_CLOCK_CTRL),
  2089. omap_readw(ULPD_SOFT_REQ),
  2090. omap_readw(ULPD_STATUS_REQ));
  2091. }
  2092. /* OTG controller registers */
  2093. if (!cpu_is_omap15xx())
  2094. proc_otg_show(s);
  2095. tmp = omap_readw(UDC_SYSCON1);
  2096. seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
  2097. (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
  2098. (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
  2099. (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
  2100. (tmp & UDC_NAK_EN) ? " nak" : "",
  2101. (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
  2102. (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
  2103. (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
  2104. (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
  2105. // syscon2 is write-only
  2106. /* UDC controller registers */
  2107. if (!(tmp & UDC_PULLUP_EN)) {
  2108. seq_printf(s, "(suspended)\n");
  2109. spin_unlock_irqrestore(&udc->lock, flags);
  2110. return 0;
  2111. }
  2112. tmp = omap_readw(UDC_DEVSTAT);
  2113. seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
  2114. (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
  2115. (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
  2116. (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
  2117. (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
  2118. (tmp & UDC_USB_RESET) ? " usb_reset" : "",
  2119. (tmp & UDC_SUS) ? " SUS" : "",
  2120. (tmp & UDC_CFG) ? " CFG" : "",
  2121. (tmp & UDC_ADD) ? " ADD" : "",
  2122. (tmp & UDC_DEF) ? " DEF" : "",
  2123. (tmp & UDC_ATT) ? " ATT" : "");
  2124. seq_printf(s, "sof %04x\n", omap_readw(UDC_SOF));
  2125. tmp = omap_readw(UDC_IRQ_EN);
  2126. seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
  2127. (tmp & UDC_SOF_IE) ? " sof" : "",
  2128. (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
  2129. (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
  2130. (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
  2131. (tmp & UDC_EP0_IE) ? " ep0" : "");
  2132. tmp = omap_readw(UDC_IRQ_SRC);
  2133. seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
  2134. (tmp & UDC_TXN_DONE) ? " txn_done" : "",
  2135. (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
  2136. (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
  2137. (tmp & UDC_IRQ_SOF) ? " sof" : "",
  2138. (tmp & UDC_EPN_RX) ? " epn_rx" : "",
  2139. (tmp & UDC_EPN_TX) ? " epn_tx" : "",
  2140. (tmp & UDC_DS_CHG) ? " ds_chg" : "",
  2141. (tmp & UDC_SETUP) ? " setup" : "",
  2142. (tmp & UDC_EP0_RX) ? " ep0out" : "",
  2143. (tmp & UDC_EP0_TX) ? " ep0in" : "");
  2144. if (use_dma) {
  2145. unsigned i;
  2146. tmp = omap_readw(UDC_DMA_IRQ_EN);
  2147. seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
  2148. (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
  2149. (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
  2150. (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
  2151. (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
  2152. (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
  2153. (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
  2154. (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
  2155. (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
  2156. (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
  2157. tmp = omap_readw(UDC_RXDMA_CFG);
  2158. seq_printf(s, "rxdma_cfg %04x\n", tmp);
  2159. if (tmp) {
  2160. for (i = 0; i < 3; i++) {
  2161. if ((tmp & (0x0f << (i * 4))) == 0)
  2162. continue;
  2163. seq_printf(s, "rxdma[%d] %04x\n", i,
  2164. omap_readw(UDC_RXDMA(i + 1)));
  2165. }
  2166. }
  2167. tmp = omap_readw(UDC_TXDMA_CFG);
  2168. seq_printf(s, "txdma_cfg %04x\n", tmp);
  2169. if (tmp) {
  2170. for (i = 0; i < 3; i++) {
  2171. if (!(tmp & (0x0f << (i * 4))))
  2172. continue;
  2173. seq_printf(s, "txdma[%d] %04x\n", i,
  2174. omap_readw(UDC_TXDMA(i + 1)));
  2175. }
  2176. }
  2177. }
  2178. tmp = omap_readw(UDC_DEVSTAT);
  2179. if (tmp & UDC_ATT) {
  2180. proc_ep_show(s, &udc->ep[0]);
  2181. if (tmp & UDC_ADD) {
  2182. list_for_each_entry (ep, &udc->gadget.ep_list,
  2183. ep.ep_list) {
  2184. if (ep->desc)
  2185. proc_ep_show(s, ep);
  2186. }
  2187. }
  2188. }
  2189. spin_unlock_irqrestore(&udc->lock, flags);
  2190. return 0;
  2191. }
  2192. static int proc_udc_open(struct inode *inode, struct file *file)
  2193. {
  2194. return single_open(file, proc_udc_show, NULL);
  2195. }
  2196. static const struct file_operations proc_ops = {
  2197. .owner = THIS_MODULE,
  2198. .open = proc_udc_open,
  2199. .read = seq_read,
  2200. .llseek = seq_lseek,
  2201. .release = single_release,
  2202. };
  2203. static void create_proc_file(void)
  2204. {
  2205. proc_create(proc_filename, 0, NULL, &proc_ops);
  2206. }
  2207. static void remove_proc_file(void)
  2208. {
  2209. remove_proc_entry(proc_filename, NULL);
  2210. }
  2211. #else
  2212. static inline void create_proc_file(void) {}
  2213. static inline void remove_proc_file(void) {}
  2214. #endif
  2215. /*-------------------------------------------------------------------------*/
  2216. /* Before this controller can enumerate, we need to pick an endpoint
  2217. * configuration, or "fifo_mode" That involves allocating 2KB of packet
  2218. * buffer space among the endpoints we'll be operating.
  2219. *
  2220. * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
  2221. * UDC_SYSCON_1.CFG_LOCK is set can now work. We won't use that
  2222. * capability yet though.
  2223. */
  2224. static unsigned __init
  2225. omap_ep_setup(char *name, u8 addr, u8 type,
  2226. unsigned buf, unsigned maxp, int dbuf)
  2227. {
  2228. struct omap_ep *ep;
  2229. u16 epn_rxtx = 0;
  2230. /* OUT endpoints first, then IN */
  2231. ep = &udc->ep[addr & 0xf];
  2232. if (addr & USB_DIR_IN)
  2233. ep += 16;
  2234. /* in case of ep init table bugs */
  2235. BUG_ON(ep->name[0]);
  2236. /* chip setup ... bit values are same for IN, OUT */
  2237. if (type == USB_ENDPOINT_XFER_ISOC) {
  2238. switch (maxp) {
  2239. case 8: epn_rxtx = 0 << 12; break;
  2240. case 16: epn_rxtx = 1 << 12; break;
  2241. case 32: epn_rxtx = 2 << 12; break;
  2242. case 64: epn_rxtx = 3 << 12; break;
  2243. case 128: epn_rxtx = 4 << 12; break;
  2244. case 256: epn_rxtx = 5 << 12; break;
  2245. case 512: epn_rxtx = 6 << 12; break;
  2246. default: BUG();
  2247. }
  2248. epn_rxtx |= UDC_EPN_RX_ISO;
  2249. dbuf = 1;
  2250. } else {
  2251. /* double-buffering "not supported" on 15xx,
  2252. * and ignored for PIO-IN on newer chips
  2253. * (for more reliable behavior)
  2254. */
  2255. if (!use_dma || cpu_is_omap15xx() || cpu_is_omap24xx())
  2256. dbuf = 0;
  2257. switch (maxp) {
  2258. case 8: epn_rxtx = 0 << 12; break;
  2259. case 16: epn_rxtx = 1 << 12; break;
  2260. case 32: epn_rxtx = 2 << 12; break;
  2261. case 64: epn_rxtx = 3 << 12; break;
  2262. default: BUG();
  2263. }
  2264. if (dbuf && addr)
  2265. epn_rxtx |= UDC_EPN_RX_DB;
  2266. init_timer(&ep->timer);
  2267. ep->timer.function = pio_out_timer;
  2268. ep->timer.data = (unsigned long) ep;
  2269. }
  2270. if (addr)
  2271. epn_rxtx |= UDC_EPN_RX_VALID;
  2272. BUG_ON(buf & 0x07);
  2273. epn_rxtx |= buf >> 3;
  2274. DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
  2275. name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
  2276. if (addr & USB_DIR_IN)
  2277. omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
  2278. else
  2279. omap_writew(epn_rxtx, UDC_EP_RX(addr));
  2280. /* next endpoint's buffer starts after this one's */
  2281. buf += maxp;
  2282. if (dbuf)
  2283. buf += maxp;
  2284. BUG_ON(buf > 2048);
  2285. /* set up driver data structures */
  2286. BUG_ON(strlen(name) >= sizeof ep->name);
  2287. strlcpy(ep->name, name, sizeof ep->name);
  2288. INIT_LIST_HEAD(&ep->queue);
  2289. INIT_LIST_HEAD(&ep->iso);
  2290. ep->bEndpointAddress = addr;
  2291. ep->bmAttributes = type;
  2292. ep->double_buf = dbuf;
  2293. ep->udc = udc;
  2294. ep->ep.name = ep->name;
  2295. ep->ep.ops = &omap_ep_ops;
  2296. ep->ep.maxpacket = ep->maxpacket = maxp;
  2297. list_add_tail (&ep->ep.ep_list, &udc->gadget.ep_list);
  2298. return buf;
  2299. }
  2300. static void omap_udc_release(struct device *dev)
  2301. {
  2302. complete(udc->done);
  2303. kfree (udc);
  2304. udc = NULL;
  2305. }
  2306. static int __init
  2307. omap_udc_setup(struct platform_device *odev, struct otg_transceiver *xceiv)
  2308. {
  2309. unsigned tmp, buf;
  2310. /* abolish any previous hardware state */
  2311. omap_writew(0, UDC_SYSCON1);
  2312. omap_writew(0, UDC_IRQ_EN);
  2313. omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
  2314. omap_writew(0, UDC_DMA_IRQ_EN);
  2315. omap_writew(0, UDC_RXDMA_CFG);
  2316. omap_writew(0, UDC_TXDMA_CFG);
  2317. /* UDC_PULLUP_EN gates the chip clock */
  2318. // OTG_SYSCON_1 |= DEV_IDLE_EN;
  2319. udc = kzalloc(sizeof(*udc), GFP_KERNEL);
  2320. if (!udc)
  2321. return -ENOMEM;
  2322. spin_lock_init (&udc->lock);
  2323. udc->gadget.ops = &omap_gadget_ops;
  2324. udc->gadget.ep0 = &udc->ep[0].ep;
  2325. INIT_LIST_HEAD(&udc->gadget.ep_list);
  2326. INIT_LIST_HEAD(&udc->iso);
  2327. udc->gadget.speed = USB_SPEED_UNKNOWN;
  2328. udc->gadget.name = driver_name;
  2329. device_initialize(&udc->gadget.dev);
  2330. dev_set_name(&udc->gadget.dev, "gadget");
  2331. udc->gadget.dev.release = omap_udc_release;
  2332. udc->gadget.dev.parent = &odev->dev;
  2333. if (use_dma)
  2334. udc->gadget.dev.dma_mask = odev->dev.dma_mask;
  2335. udc->transceiver = xceiv;
  2336. /* ep0 is special; put it right after the SETUP buffer */
  2337. buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
  2338. 8 /* after SETUP */, 64 /* maxpacket */, 0);
  2339. list_del_init(&udc->ep[0].ep.ep_list);
  2340. /* initially disable all non-ep0 endpoints */
  2341. for (tmp = 1; tmp < 15; tmp++) {
  2342. omap_writew(0, UDC_EP_RX(tmp));
  2343. omap_writew(0, UDC_EP_TX(tmp));
  2344. }
  2345. #define OMAP_BULK_EP(name,addr) \
  2346. buf = omap_ep_setup(name "-bulk", addr, \
  2347. USB_ENDPOINT_XFER_BULK, buf, 64, 1);
  2348. #define OMAP_INT_EP(name,addr, maxp) \
  2349. buf = omap_ep_setup(name "-int", addr, \
  2350. USB_ENDPOINT_XFER_INT, buf, maxp, 0);
  2351. #define OMAP_ISO_EP(name,addr, maxp) \
  2352. buf = omap_ep_setup(name "-iso", addr, \
  2353. USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
  2354. switch (fifo_mode) {
  2355. case 0:
  2356. OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
  2357. OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
  2358. OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
  2359. break;
  2360. case 1:
  2361. OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
  2362. OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
  2363. OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
  2364. OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
  2365. OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
  2366. OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
  2367. OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
  2368. OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
  2369. OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
  2370. OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
  2371. OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
  2372. OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
  2373. OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
  2374. OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
  2375. OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
  2376. OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
  2377. OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
  2378. OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
  2379. OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
  2380. OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
  2381. OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
  2382. OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
  2383. break;
  2384. #ifdef USE_ISO
  2385. case 2: /* mixed iso/bulk */
  2386. OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
  2387. OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
  2388. OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
  2389. OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
  2390. OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
  2391. OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
  2392. OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
  2393. OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
  2394. break;
  2395. case 3: /* mixed bulk/iso */
  2396. OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
  2397. OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
  2398. OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
  2399. OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
  2400. OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
  2401. OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
  2402. OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
  2403. OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
  2404. OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
  2405. break;
  2406. #endif
  2407. /* add more modes as needed */
  2408. default:
  2409. ERR("unsupported fifo_mode #%d\n", fifo_mode);
  2410. return -ENODEV;
  2411. }
  2412. omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
  2413. INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
  2414. return 0;
  2415. }
  2416. static int __init omap_udc_probe(struct platform_device *pdev)
  2417. {
  2418. int status = -ENODEV;
  2419. int hmc;
  2420. struct otg_transceiver *xceiv = NULL;
  2421. const char *type = NULL;
  2422. struct omap_usb_config *config = pdev->dev.platform_data;
  2423. struct clk *dc_clk;
  2424. struct clk *hhc_clk;
  2425. /* NOTE: "knows" the order of the resources! */
  2426. if (!request_mem_region(pdev->resource[0].start,
  2427. pdev->resource[0].end - pdev->resource[0].start + 1,
  2428. driver_name)) {
  2429. DBG("request_mem_region failed\n");
  2430. return -EBUSY;
  2431. }
  2432. if (cpu_is_omap16xx()) {
  2433. dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
  2434. hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
  2435. BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
  2436. /* can't use omap_udc_enable_clock yet */
  2437. clk_enable(dc_clk);
  2438. clk_enable(hhc_clk);
  2439. udelay(100);
  2440. }
  2441. if (cpu_is_omap24xx()) {
  2442. dc_clk = clk_get(&pdev->dev, "usb_fck");
  2443. hhc_clk = clk_get(&pdev->dev, "usb_l4_ick");
  2444. BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
  2445. /* can't use omap_udc_enable_clock yet */
  2446. clk_enable(dc_clk);
  2447. clk_enable(hhc_clk);
  2448. udelay(100);
  2449. }
  2450. if (cpu_is_omap7xx()) {
  2451. dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
  2452. hhc_clk = clk_get(&pdev->dev, "l3_ocpi_ck");
  2453. BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
  2454. /* can't use omap_udc_enable_clock yet */
  2455. clk_enable(dc_clk);
  2456. clk_enable(hhc_clk);
  2457. udelay(100);
  2458. }
  2459. INFO("OMAP UDC rev %d.%d%s\n",
  2460. omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
  2461. config->otg ? ", Mini-AB" : "");
  2462. /* use the mode given to us by board init code */
  2463. if (cpu_is_omap15xx()) {
  2464. hmc = HMC_1510;
  2465. type = "(unknown)";
  2466. if (machine_without_vbus_sense()) {
  2467. /* just set up software VBUS detect, and then
  2468. * later rig it so we always report VBUS.
  2469. * FIXME without really sensing VBUS, we can't
  2470. * know when to turn PULLUP_EN on/off; and that
  2471. * means we always "need" the 48MHz clock.
  2472. */
  2473. u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
  2474. tmp &= ~VBUS_CTRL_1510;
  2475. omap_writel(tmp, FUNC_MUX_CTRL_0);
  2476. tmp |= VBUS_MODE_1510;
  2477. tmp &= ~VBUS_CTRL_1510;
  2478. omap_writel(tmp, FUNC_MUX_CTRL_0);
  2479. }
  2480. } else {
  2481. /* The transceiver may package some GPIO logic or handle
  2482. * loopback and/or transceiverless setup; if we find one,
  2483. * use it. Except for OTG, we don't _need_ to talk to one;
  2484. * but not having one probably means no VBUS detection.
  2485. */
  2486. xceiv = otg_get_transceiver();
  2487. if (xceiv)
  2488. type = xceiv->label;
  2489. else if (config->otg) {
  2490. DBG("OTG requires external transceiver!\n");
  2491. goto cleanup0;
  2492. }
  2493. hmc = HMC_1610;
  2494. if (cpu_is_omap24xx()) {
  2495. /* this could be transceiverless in one of the
  2496. * "we don't need to know" modes.
  2497. */
  2498. type = "external";
  2499. goto known;
  2500. }
  2501. switch (hmc) {
  2502. case 0: /* POWERUP DEFAULT == 0 */
  2503. case 4:
  2504. case 12:
  2505. case 20:
  2506. if (!cpu_is_omap1710()) {
  2507. type = "integrated";
  2508. break;
  2509. }
  2510. /* FALL THROUGH */
  2511. case 3:
  2512. case 11:
  2513. case 16:
  2514. case 19:
  2515. case 25:
  2516. if (!xceiv) {
  2517. DBG("external transceiver not registered!\n");
  2518. type = "unknown";
  2519. }
  2520. break;
  2521. case 21: /* internal loopback */
  2522. type = "loopback";
  2523. break;
  2524. case 14: /* transceiverless */
  2525. if (cpu_is_omap1710())
  2526. goto bad_on_1710;
  2527. /* FALL THROUGH */
  2528. case 13:
  2529. case 15:
  2530. type = "no";
  2531. break;
  2532. default:
  2533. bad_on_1710:
  2534. ERR("unrecognized UDC HMC mode %d\n", hmc);
  2535. goto cleanup0;
  2536. }
  2537. }
  2538. known:
  2539. INFO("hmc mode %d, %s transceiver\n", hmc, type);
  2540. /* a "gadget" abstracts/virtualizes the controller */
  2541. status = omap_udc_setup(pdev, xceiv);
  2542. if (status) {
  2543. goto cleanup0;
  2544. }
  2545. xceiv = NULL;
  2546. // "udc" is now valid
  2547. pullup_disable(udc);
  2548. #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
  2549. udc->gadget.is_otg = (config->otg != 0);
  2550. #endif
  2551. /* starting with omap1710 es2.0, clear toggle is a separate bit */
  2552. if (omap_readw(UDC_REV) >= 0x61)
  2553. udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
  2554. else
  2555. udc->clr_halt = UDC_RESET_EP;
  2556. /* USB general purpose IRQ: ep0, state changes, dma, etc */
  2557. status = request_irq(pdev->resource[1].start, omap_udc_irq,
  2558. IRQF_SAMPLE_RANDOM, driver_name, udc);
  2559. if (status != 0) {
  2560. ERR("can't get irq %d, err %d\n",
  2561. (int) pdev->resource[1].start, status);
  2562. goto cleanup1;
  2563. }
  2564. /* USB "non-iso" IRQ (PIO for all but ep0) */
  2565. status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
  2566. IRQF_SAMPLE_RANDOM, "omap_udc pio", udc);
  2567. if (status != 0) {
  2568. ERR("can't get irq %d, err %d\n",
  2569. (int) pdev->resource[2].start, status);
  2570. goto cleanup2;
  2571. }
  2572. #ifdef USE_ISO
  2573. status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
  2574. IRQF_DISABLED, "omap_udc iso", udc);
  2575. if (status != 0) {
  2576. ERR("can't get irq %d, err %d\n",
  2577. (int) pdev->resource[3].start, status);
  2578. goto cleanup3;
  2579. }
  2580. #endif
  2581. if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
  2582. udc->dc_clk = dc_clk;
  2583. udc->hhc_clk = hhc_clk;
  2584. clk_disable(hhc_clk);
  2585. clk_disable(dc_clk);
  2586. }
  2587. if (cpu_is_omap24xx()) {
  2588. udc->dc_clk = dc_clk;
  2589. udc->hhc_clk = hhc_clk;
  2590. /* FIXME OMAP2 don't release hhc & dc clock */
  2591. #if 0
  2592. clk_disable(hhc_clk);
  2593. clk_disable(dc_clk);
  2594. #endif
  2595. }
  2596. create_proc_file();
  2597. status = device_add(&udc->gadget.dev);
  2598. if (status)
  2599. goto cleanup4;
  2600. status = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
  2601. if (!status)
  2602. return status;
  2603. /* If fail, fall through */
  2604. cleanup4:
  2605. remove_proc_file();
  2606. #ifdef USE_ISO
  2607. cleanup3:
  2608. free_irq(pdev->resource[2].start, udc);
  2609. #endif
  2610. cleanup2:
  2611. free_irq(pdev->resource[1].start, udc);
  2612. cleanup1:
  2613. kfree (udc);
  2614. udc = NULL;
  2615. cleanup0:
  2616. if (xceiv)
  2617. otg_put_transceiver(xceiv);
  2618. if (cpu_is_omap16xx() || cpu_is_omap24xx() || cpu_is_omap7xx()) {
  2619. clk_disable(hhc_clk);
  2620. clk_disable(dc_clk);
  2621. clk_put(hhc_clk);
  2622. clk_put(dc_clk);
  2623. }
  2624. release_mem_region(pdev->resource[0].start,
  2625. pdev->resource[0].end - pdev->resource[0].start + 1);
  2626. return status;
  2627. }
  2628. static int __exit omap_udc_remove(struct platform_device *pdev)
  2629. {
  2630. DECLARE_COMPLETION_ONSTACK(done);
  2631. if (!udc)
  2632. return -ENODEV;
  2633. usb_del_gadget_udc(&udc->gadget);
  2634. if (udc->driver)
  2635. return -EBUSY;
  2636. udc->done = &done;
  2637. pullup_disable(udc);
  2638. if (udc->transceiver) {
  2639. otg_put_transceiver(udc->transceiver);
  2640. udc->transceiver = NULL;
  2641. }
  2642. omap_writew(0, UDC_SYSCON1);
  2643. remove_proc_file();
  2644. #ifdef USE_ISO
  2645. free_irq(pdev->resource[3].start, udc);
  2646. #endif
  2647. free_irq(pdev->resource[2].start, udc);
  2648. free_irq(pdev->resource[1].start, udc);
  2649. if (udc->dc_clk) {
  2650. if (udc->clk_requested)
  2651. omap_udc_enable_clock(0);
  2652. clk_put(udc->hhc_clk);
  2653. clk_put(udc->dc_clk);
  2654. }
  2655. release_mem_region(pdev->resource[0].start,
  2656. pdev->resource[0].end - pdev->resource[0].start + 1);
  2657. device_unregister(&udc->gadget.dev);
  2658. wait_for_completion(&done);
  2659. return 0;
  2660. }
  2661. /* suspend/resume/wakeup from sysfs (echo > power/state) or when the
  2662. * system is forced into deep sleep
  2663. *
  2664. * REVISIT we should probably reject suspend requests when there's a host
  2665. * session active, rather than disconnecting, at least on boards that can
  2666. * report VBUS irqs (UDC_DEVSTAT.UDC_ATT). And in any case, we need to
  2667. * make host resumes and VBUS detection trigger OMAP wakeup events; that
  2668. * may involve talking to an external transceiver (e.g. isp1301).
  2669. */
  2670. static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
  2671. {
  2672. u32 devstat;
  2673. devstat = omap_readw(UDC_DEVSTAT);
  2674. /* we're requesting 48 MHz clock if the pullup is enabled
  2675. * (== we're attached to the host) and we're not suspended,
  2676. * which would prevent entry to deep sleep...
  2677. */
  2678. if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
  2679. WARNING("session active; suspend requires disconnect\n");
  2680. omap_pullup(&udc->gadget, 0);
  2681. }
  2682. return 0;
  2683. }
  2684. static int omap_udc_resume(struct platform_device *dev)
  2685. {
  2686. DBG("resume + wakeup/SRP\n");
  2687. omap_pullup(&udc->gadget, 1);
  2688. /* maybe the host would enumerate us if we nudged it */
  2689. msleep(100);
  2690. return omap_wakeup(&udc->gadget);
  2691. }
  2692. /*-------------------------------------------------------------------------*/
  2693. static struct platform_driver udc_driver = {
  2694. .remove = __exit_p(omap_udc_remove),
  2695. .suspend = omap_udc_suspend,
  2696. .resume = omap_udc_resume,
  2697. .driver = {
  2698. .owner = THIS_MODULE,
  2699. .name = (char *) driver_name,
  2700. },
  2701. };
  2702. static int __init udc_init(void)
  2703. {
  2704. /* Disable DMA for omap7xx -- it doesn't work right. */
  2705. if (cpu_is_omap7xx())
  2706. use_dma = 0;
  2707. INFO("%s, version: " DRIVER_VERSION
  2708. #ifdef USE_ISO
  2709. " (iso)"
  2710. #endif
  2711. "%s\n", driver_desc,
  2712. use_dma ? " (dma)" : "");
  2713. return platform_driver_probe(&udc_driver, omap_udc_probe);
  2714. }
  2715. module_init(udc_init);
  2716. static void __exit udc_exit(void)
  2717. {
  2718. platform_driver_unregister(&udc_driver);
  2719. }
  2720. module_exit(udc_exit);
  2721. MODULE_DESCRIPTION(DRIVER_DESC);
  2722. MODULE_LICENSE("GPL");
  2723. MODULE_ALIAS("platform:omap_udc");