omap_udc.c 80 KB

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