omap_udc.c 78 KB

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