omap_udc.c 80 KB

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