omap_udc.c 77 KB

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