omap_udc.c 77 KB

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