omap_udc.c 78 KB

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