omap_udc.c 78 KB

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