omap_udc.c 80 KB

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