omap_udc.c 77 KB

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