omap_udc.c 80 KB

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