omap_udc.c 78 KB

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