omap_udc.c 77 KB

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