omap_udc.c 78 KB

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