omap_udc.c 78 KB

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