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