musb_host.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MUSB OTG driver host support
  4. *
  5. * Copyright 2005 Mentor Graphics Corporation
  6. * Copyright (C) 2005-2006 by Texas Instruments
  7. * Copyright (C) 2006-2007 Nokia Corporation
  8. * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/delay.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/errno.h>
  16. #include <linux/list.h>
  17. #include <linux/dma-mapping.h>
  18. #include "musb_core.h"
  19. #include "musb_host.h"
  20. #include "musb_trace.h"
  21. #define UDISK_INTERVAL 0x10
  22. /* MUSB HOST status 22-mar-2006
  23. *
  24. * - There's still lots of partial code duplication for fault paths, so
  25. * they aren't handled as consistently as they need to be.
  26. *
  27. * - PIO mostly behaved when last tested.
  28. * + including ep0, with all usbtest cases 9, 10
  29. * + usbtest 14 (ep0out) doesn't seem to run at all
  30. * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
  31. * configurations, but otherwise double buffering passes basic tests.
  32. * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
  33. *
  34. * - DMA (CPPI) ... partially behaves, not currently recommended
  35. * + about 1/15 the speed of typical EHCI implementations (PCI)
  36. * + RX, all too often reqpkt seems to misbehave after tx
  37. * + TX, no known issues (other than evident silicon issue)
  38. *
  39. * - DMA (Mentor/OMAP) ...has at least toggle update problems
  40. *
  41. * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
  42. * starvation ... nothing yet for TX, interrupt, or bulk.
  43. *
  44. * - Not tested with HNP, but some SRP paths seem to behave.
  45. *
  46. * NOTE 24-August-2006:
  47. *
  48. * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
  49. * extra endpoint for periodic use enabling hub + keybd + mouse. That
  50. * mostly works, except that with "usbnet" it's easy to trigger cases
  51. * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
  52. * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
  53. * although ARP RX wins. (That test was done with a full speed link.)
  54. */
  55. /*
  56. * NOTE on endpoint usage:
  57. *
  58. * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
  59. * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
  60. * (Yes, bulk _could_ use more of the endpoints than that, and would even
  61. * benefit from it.)
  62. *
  63. * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
  64. * So far that scheduling is both dumb and optimistic: the endpoint will be
  65. * "claimed" until its software queue is no longer refilled. No multiplexing
  66. * of transfers between endpoints, or anything clever.
  67. */
  68. struct musb *hcd_to_musb(struct usb_hcd *hcd)
  69. {
  70. return *(struct musb **) hcd->hcd_priv;
  71. }
  72. static void musb_ep_program(struct musb *musb, u8 epnum,
  73. struct urb *urb, int is_out,
  74. u8 *buf, u32 offset, u32 len);
  75. /*
  76. * Clear TX fifo. Needed to avoid BABBLE errors.
  77. */
  78. static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
  79. {
  80. struct musb *musb = ep->musb;
  81. void __iomem *epio = ep->regs;
  82. u16 csr;
  83. #if NICHOLAS_ADD
  84. int retries = 1;
  85. #else
  86. int retries = 1000;
  87. #endif
  88. csr = musb_readw(epio, MUSB_TXCSR);
  89. while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  90. csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_TXPKTRDY;
  91. musb_writew(epio, MUSB_TXCSR, csr);
  92. csr = musb_readw(epio, MUSB_TXCSR);
  93. /*
  94. * FIXME: sometimes the tx fifo flush failed, it has been
  95. * observed during device disconnect on AM335x.
  96. *
  97. * To reproduce the issue, ensure tx urb(s) are queued when
  98. * unplug the usb device which is connected to AM335x usb
  99. * host port.
  100. *
  101. * I found using a usb-ethernet device and running iperf
  102. * (client on AM335x) has very high chance to trigger it.
  103. *
  104. * Better to turn on musb_dbg() in musb_cleanup_urb() with
  105. * CPPI enabled to see the issue when aborting the tx channel.
  106. */
  107. if (dev_WARN_ONCE(musb->controller, retries-- < 1,
  108. "Could not flush host TX%d fifo: csr: %04x\n",
  109. ep->epnum, csr)){
  110. printk(KERN_ALERT "Host TX FIFONOTEMPTY csr: %02x\n", csr);
  111. return;
  112. }
  113. mdelay(1);
  114. }
  115. }
  116. static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
  117. {
  118. void __iomem *epio = ep->regs;
  119. u16 csr;
  120. int retries = 5;
  121. /* scrub any data left in the fifo */
  122. do {
  123. csr = musb_readw(epio, MUSB_TXCSR);
  124. if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
  125. break;
  126. musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
  127. csr = musb_readw(epio, MUSB_TXCSR);
  128. udelay(10);
  129. } while (--retries);
  130. WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
  131. ep->epnum, csr);
  132. /* and reset for the next transfer */
  133. musb_writew(epio, MUSB_TXCSR, 0);
  134. }
  135. /*
  136. * Start transmit. Caller is responsible for locking shared resources.
  137. * musb must be locked.
  138. */
  139. static inline void musb_h_tx_start(struct musb_hw_ep *ep)
  140. {
  141. u16 txcsr;
  142. /* NOTE: no locks here; caller should lock and select EP */
  143. if (ep->epnum) {
  144. txcsr = musb_readw(ep->regs, MUSB_TXCSR);
  145. txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
  146. musb_writew(ep->regs, MUSB_TXCSR, txcsr);
  147. } else {
  148. txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
  149. musb_writew(ep->regs, MUSB_CSR0, txcsr);
  150. }
  151. }
  152. static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
  153. {
  154. u16 txcsr;
  155. /* NOTE: no locks here; caller should lock and select EP */
  156. txcsr = musb_readw(ep->regs, MUSB_TXCSR);
  157. txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
  158. if (is_cppi_enabled(ep->musb))
  159. txcsr |= MUSB_TXCSR_DMAMODE;
  160. musb_writew(ep->regs, MUSB_TXCSR, txcsr);
  161. }
  162. static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
  163. {
  164. if (is_in != 0 || ep->is_shared_fifo)
  165. ep->in_qh = qh;
  166. if (is_in == 0 || ep->is_shared_fifo)
  167. ep->out_qh = qh;
  168. }
  169. static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
  170. {
  171. return is_in ? ep->in_qh : ep->out_qh;
  172. }
  173. /*
  174. * Start the URB at the front of an endpoint's queue
  175. * end must be claimed from the caller.
  176. *
  177. * Context: controller locked, irqs blocked
  178. */
  179. static void
  180. musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
  181. {
  182. u32 len;
  183. void __iomem *mbase = musb->mregs;
  184. struct urb *urb = next_urb(qh);
  185. void *buf = urb->transfer_buffer;
  186. u32 offset = 0;
  187. struct musb_hw_ep *hw_ep = qh->hw_ep;
  188. int epnum = hw_ep->epnum;
  189. if(epnum >= MUSB_C_NUM_EPS) //add++
  190. {
  191. printk(KERN_ERR "ERR: %s, Invalid epnum:%d, exit.\n.", __FUNCTION__, epnum);
  192. return;
  193. }
  194. /* initialize software qh state */
  195. qh->offset = 0;
  196. qh->segsize = 0;
  197. /* gather right source of data */
  198. switch (qh->type) {
  199. case USB_ENDPOINT_XFER_CONTROL:
  200. /* control transfers always start with SETUP */
  201. is_in = 0;
  202. musb->ep0_stage = MUSB_EP0_START;
  203. buf = urb->setup_packet;
  204. len = 8;
  205. break;
  206. case USB_ENDPOINT_XFER_ISOC:
  207. qh->iso_idx = 0;
  208. qh->frame = 0;
  209. offset = urb->iso_frame_desc[0].offset;
  210. len = urb->iso_frame_desc[0].length;
  211. break;
  212. default: /* bulk, interrupt */
  213. /* actual_length may be nonzero on retry paths */
  214. buf = urb->transfer_buffer + urb->actual_length;
  215. len = urb->transfer_buffer_length - urb->actual_length;
  216. }
  217. trace_musb_urb_start(musb, urb);
  218. /* Configure endpoint */
  219. musb_ep_set_qh(hw_ep, is_in, qh);
  220. musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
  221. /* transmit may have more work: start it when it is time */
  222. if (is_in)
  223. return;
  224. /* determine if the time is right for a periodic transfer */
  225. switch (qh->type) {
  226. case USB_ENDPOINT_XFER_ISOC:
  227. case USB_ENDPOINT_XFER_INT:
  228. musb_dbg(musb, "check whether there's still time for periodic Tx");
  229. /* FIXME this doesn't implement that scheduling policy ...
  230. * or handle framecounter wrapping
  231. */
  232. if (1) { /* Always assume URB_ISO_ASAP */
  233. /* REVISIT the SOF irq handler shouldn't duplicate
  234. * this code; and we don't init urb->start_frame...
  235. */
  236. qh->frame = 0;
  237. goto start;
  238. } else {
  239. qh->frame = urb->start_frame;
  240. /* enable SOF interrupt so we can count down */
  241. musb_dbg(musb, "SOF for %d", epnum);
  242. #if 1 /* ifndef CONFIG_ARCH_DAVINCI */
  243. musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
  244. #endif
  245. }
  246. break;
  247. default:
  248. start:
  249. musb_dbg(musb, "Start TX%d %s", epnum,
  250. hw_ep->tx_channel ? "dma" : "pio");
  251. if (!hw_ep->tx_channel)
  252. musb_h_tx_start(hw_ep);
  253. else if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
  254. musb_h_tx_dma_start(hw_ep);
  255. }
  256. }
  257. /* Context: caller owns controller lock, IRQs are blocked */
  258. static void musb_giveback(struct musb *musb, struct urb *urb, int status)
  259. __releases(musb->lock)
  260. __acquires(musb->lock)
  261. {
  262. trace_musb_urb_gb(musb, urb);
  263. usb_hcd_unlink_urb_from_ep(musb->hcd, urb);
  264. spin_unlock(&musb->lock);
  265. usb_hcd_giveback_urb(musb->hcd, urb, status);
  266. spin_lock(&musb->lock);
  267. }
  268. /* For bulk/interrupt endpoints only */
  269. static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
  270. struct urb *urb)
  271. {
  272. void __iomem *epio = qh->hw_ep->regs;
  273. u16 csr;
  274. /*
  275. * FIXME: the current Mentor DMA code seems to have
  276. * problems getting toggle correct.
  277. */
  278. if (is_in)
  279. csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
  280. else
  281. csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
  282. usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
  283. }
  284. /*
  285. * Advance this hardware endpoint's queue, completing the specified URB and
  286. * advancing to either the next URB queued to that qh, or else invalidating
  287. * that qh and advancing to the next qh scheduled after the current one.
  288. *
  289. * Context: caller owns controller lock, IRQs are blocked
  290. */
  291. static void musb_advance_schedule(struct musb *musb, struct urb *urb,
  292. struct musb_hw_ep *hw_ep, int is_in)
  293. {
  294. struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
  295. struct musb_hw_ep *ep = qh->hw_ep;
  296. int ready = qh->is_ready;
  297. int status;
  298. status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
  299. /* save toggle eagerly, for paranoia */
  300. switch (qh->type) {
  301. case USB_ENDPOINT_XFER_BULK:
  302. case USB_ENDPOINT_XFER_INT:
  303. musb_save_toggle(qh, is_in, urb);
  304. break;
  305. case USB_ENDPOINT_XFER_ISOC:
  306. if (status == 0 && urb->error_count)
  307. status = -EXDEV;
  308. break;
  309. }
  310. qh->is_ready = 0;
  311. musb_giveback(musb, urb, status);
  312. qh->is_ready = ready;
  313. /* reclaim resources (and bandwidth) ASAP; deschedule it, and
  314. * invalidate qh as soon as list_empty(&hep->urb_list)
  315. */
  316. if (list_empty(&qh->hep->urb_list)) {
  317. struct list_head *head;
  318. struct dma_controller *dma = musb->dma_controller;
  319. if (is_in) {
  320. ep->rx_reinit = 1;
  321. if (ep->rx_channel) {
  322. dma->channel_release(ep->rx_channel);
  323. ep->rx_channel = NULL;
  324. }
  325. } else {
  326. ep->tx_reinit = 1;
  327. if (ep->tx_channel) {
  328. dma->channel_release(ep->tx_channel);
  329. ep->tx_channel = NULL;
  330. }
  331. }
  332. /* Clobber old pointers to this qh */
  333. musb_ep_set_qh(ep, is_in, NULL);
  334. qh->hep->hcpriv = NULL;
  335. switch (qh->type) {
  336. case USB_ENDPOINT_XFER_CONTROL:
  337. case USB_ENDPOINT_XFER_BULK:
  338. /* fifo policy for these lists, except that NAKing
  339. * should rotate a qh to the end (for fairness).
  340. */
  341. if (qh->mux == 1) {
  342. head = qh->ring.prev;
  343. list_del(&qh->ring);
  344. kfree(qh);
  345. qh = first_qh(head);
  346. break;
  347. }
  348. /* else: fall through */
  349. case USB_ENDPOINT_XFER_ISOC:
  350. case USB_ENDPOINT_XFER_INT:
  351. /* this is where periodic bandwidth should be
  352. * de-allocated if it's tracked and allocated;
  353. * and where we'd update the schedule tree...
  354. */
  355. kfree(qh);
  356. qh = NULL;
  357. break;
  358. }
  359. }
  360. if (qh != NULL && qh->is_ready) {
  361. musb_dbg(musb, "... next ep%d %cX urb %p",
  362. hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
  363. musb_start_urb(musb, is_in, qh);
  364. }
  365. }
  366. static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
  367. {
  368. /* we don't want fifo to fill itself again;
  369. * ignore dma (various models),
  370. * leave toggle alone (may not have been saved yet)
  371. */
  372. csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
  373. csr &= ~(MUSB_RXCSR_H_REQPKT
  374. | MUSB_RXCSR_H_AUTOREQ
  375. | MUSB_RXCSR_AUTOCLEAR);
  376. /* write 2x to allow double buffering */
  377. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  378. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  379. /* flush writebuffer */
  380. return musb_readw(hw_ep->regs, MUSB_RXCSR);
  381. }
  382. /*
  383. * PIO RX for a packet (or part of it).
  384. */
  385. static bool
  386. musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
  387. {
  388. u16 rx_count;
  389. u8 *buf;
  390. u16 csr;
  391. bool done = false;
  392. u32 length;
  393. int do_flush = 0;
  394. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  395. void __iomem *epio = hw_ep->regs;
  396. struct musb_qh *qh = hw_ep->in_qh;
  397. int pipe = urb->pipe;
  398. void *buffer = urb->transfer_buffer;
  399. /* musb_ep_select(mbase, epnum); */
  400. rx_count = musb_readw(epio, MUSB_RXCOUNT);
  401. musb_dbg(musb, "RX%d count %d, buffer %p len %d/%d", epnum, rx_count,
  402. urb->transfer_buffer, qh->offset,
  403. urb->transfer_buffer_length);
  404. /* unload FIFO */
  405. if (usb_pipeisoc(pipe)) {
  406. int status = 0;
  407. struct usb_iso_packet_descriptor *d;
  408. if (iso_err) {
  409. status = -EILSEQ;
  410. urb->error_count++;
  411. }
  412. d = urb->iso_frame_desc + qh->iso_idx;
  413. buf = buffer + d->offset;
  414. length = d->length;
  415. if (rx_count > length) {
  416. if (status == 0) {
  417. status = -EOVERFLOW;
  418. urb->error_count++;
  419. }
  420. musb_dbg(musb, "OVERFLOW %d into %d", rx_count, length);
  421. do_flush = 1;
  422. } else
  423. length = rx_count;
  424. urb->actual_length += length;
  425. d->actual_length = length;
  426. d->status = status;
  427. /* see if we are done */
  428. done = (++qh->iso_idx >= urb->number_of_packets);
  429. } else {
  430. /* non-isoch */
  431. buf = buffer + qh->offset;
  432. length = urb->transfer_buffer_length - qh->offset;
  433. if (rx_count > length) {
  434. if (urb->status == -EINPROGRESS)
  435. urb->status = -EOVERFLOW;
  436. musb_dbg(musb, "OVERFLOW %d into %d", rx_count, length);
  437. do_flush = 1;
  438. } else
  439. length = rx_count;
  440. urb->actual_length += length;
  441. qh->offset += length;
  442. /* see if we are done */
  443. done = (urb->actual_length == urb->transfer_buffer_length)
  444. || (rx_count < qh->maxpacket)
  445. || (urb->status != -EINPROGRESS);
  446. if (done
  447. && (urb->status == -EINPROGRESS)
  448. && (urb->transfer_flags & URB_SHORT_NOT_OK)
  449. && (urb->actual_length
  450. < urb->transfer_buffer_length))
  451. urb->status = -EREMOTEIO;
  452. }
  453. musb_read_fifo(hw_ep, length, buf);
  454. csr = musb_readw(epio, MUSB_RXCSR);
  455. csr |= MUSB_RXCSR_H_WZC_BITS;
  456. if (unlikely(do_flush))
  457. musb_h_flush_rxfifo(hw_ep, csr);
  458. else {
  459. /* REVISIT this assumes AUTOCLEAR is never set */
  460. csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
  461. if (!done)
  462. csr |= MUSB_RXCSR_H_REQPKT;
  463. musb_writew(epio, MUSB_RXCSR, csr);
  464. }
  465. return done;
  466. }
  467. /* we don't always need to reinit a given side of an endpoint...
  468. * when we do, use tx/rx reinit routine and then construct a new CSR
  469. * to address data toggle, NYET, and DMA or PIO.
  470. *
  471. * it's possible that driver bugs (especially for DMA) or aborting a
  472. * transfer might have left the endpoint busier than it should be.
  473. * the busy/not-empty tests are basically paranoia.
  474. */
  475. static void
  476. musb_rx_reinit(struct musb *musb, struct musb_qh *qh, u8 epnum)
  477. {
  478. struct musb_hw_ep *ep = musb->endpoints + epnum;
  479. u16 csr;
  480. /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
  481. * That always uses tx_reinit since ep0 repurposes TX register
  482. * offsets; the initial SETUP packet is also a kind of OUT.
  483. */
  484. /* if programmed for Tx, put it in RX mode */
  485. if (ep->is_shared_fifo) {
  486. csr = musb_readw(ep->regs, MUSB_TXCSR);
  487. if (csr & MUSB_TXCSR_MODE) {
  488. musb_h_tx_flush_fifo(ep);
  489. csr = musb_readw(ep->regs, MUSB_TXCSR);
  490. musb_writew(ep->regs, MUSB_TXCSR,
  491. csr | MUSB_TXCSR_FRCDATATOG);
  492. #if NICHOLAS_ADD
  493. csr = musb_readw(ep->regs, MUSB_TXCSR);
  494. csr &= ~MUSB_TXCSR_MODE;
  495. musb_writew(ep->regs, MUSB_TXCSR, csr);
  496. #endif
  497. }
  498. /*
  499. * Clear the MODE bit (and everything else) to enable Rx.
  500. * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
  501. */
  502. #if NICHOLAS_ADD
  503. if (csr & MUSB_TXCSR_DMAMODE)
  504. {
  505. csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_AUTOSET);
  506. musb_writew(ep->regs, MUSB_TXCSR, csr);
  507. csr &= ~MUSB_TXCSR_DMAMODE;
  508. musb_writew(ep->regs, MUSB_TXCSR, csr);
  509. }
  510. #else
  511. if (csr & MUSB_TXCSR_DMAMODE)
  512. musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
  513. #endif
  514. musb_writew(ep->regs, MUSB_TXCSR, 0);
  515. /* scrub all previous state, clearing toggle */
  516. } else {
  517. csr = musb_readw(ep->regs, MUSB_RXCSR);
  518. if (csr & MUSB_RXCSR_RXPKTRDY)
  519. WARNING("rx%d, packet/%d ready?\n", ep->epnum,
  520. musb_readw(ep->regs, MUSB_RXCOUNT));
  521. musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
  522. }
  523. /* target addr and (for multipoint) hub addr/port */
  524. if (musb->is_multipoint) {
  525. musb_write_rxfunaddr(musb, epnum, qh->addr_reg);
  526. musb_write_rxhubaddr(musb, epnum, qh->h_addr_reg);
  527. musb_write_rxhubport(musb, epnum, qh->h_port_reg);
  528. } else
  529. musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
  530. /* protocol/endpoint, interval/NAKlimit, i/o size */
  531. musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
  532. musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
  533. /* NOTE: bulk combining rewrites high bits of maxpacket */
  534. /* Set RXMAXP with the FIFO size of the endpoint
  535. * to disable double buffer mode.
  536. */
  537. musb_writew(ep->regs, MUSB_RXMAXP,
  538. qh->maxpacket | ((qh->hb_mult - 1) << 11));
  539. ep->rx_reinit = 0;
  540. }
  541. static void musb_tx_dma_set_mode_mentor(struct dma_controller *dma,
  542. struct musb_hw_ep *hw_ep, struct musb_qh *qh,
  543. struct urb *urb, u32 offset,
  544. u32 *length, u8 *mode)
  545. {
  546. struct dma_channel *channel = hw_ep->tx_channel;
  547. void __iomem *epio = hw_ep->regs;
  548. u16 pkt_size = qh->maxpacket;
  549. u16 csr;
  550. if (*length > channel->max_len)
  551. *length = channel->max_len;
  552. csr = musb_readw(epio, MUSB_TXCSR);
  553. if (*length > pkt_size) {
  554. *mode = 1;
  555. csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
  556. /* autoset shouldn't be set in high bandwidth */
  557. /*
  558. * Enable Autoset according to table
  559. * below
  560. * bulk_split hb_mult Autoset_Enable
  561. * 0 1 Yes(Normal)
  562. * 0 >1 No(High BW ISO)
  563. * 1 1 Yes(HS bulk)
  564. * 1 >1 Yes(FS bulk)
  565. */
  566. if (qh->hb_mult == 1 || (qh->hb_mult > 1 &&
  567. can_bulk_split(hw_ep->musb, qh->type)))
  568. csr |= MUSB_TXCSR_AUTOSET;
  569. } else {
  570. *mode = 0;
  571. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
  572. csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
  573. }
  574. channel->desired_mode = *mode;
  575. musb_writew(epio, MUSB_TXCSR, csr);
  576. }
  577. static void musb_tx_dma_set_mode_cppi_tusb(struct dma_controller *dma,
  578. struct musb_hw_ep *hw_ep,
  579. struct musb_qh *qh,
  580. struct urb *urb,
  581. u32 offset,
  582. u32 *length,
  583. u8 *mode)
  584. {
  585. struct dma_channel *channel = hw_ep->tx_channel;
  586. channel->actual_len = 0;
  587. /*
  588. * TX uses "RNDIS" mode automatically but needs help
  589. * to identify the zero-length-final-packet case.
  590. */
  591. *mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
  592. }
  593. static bool musb_tx_dma_program(struct dma_controller *dma,
  594. struct musb_hw_ep *hw_ep, struct musb_qh *qh,
  595. struct urb *urb, u32 offset, u32 length)
  596. {
  597. struct dma_channel *channel = hw_ep->tx_channel;
  598. u16 pkt_size = qh->maxpacket;
  599. u8 mode;
  600. if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
  601. musb_tx_dma_set_mode_mentor(dma, hw_ep, qh, urb, offset,
  602. &length, &mode);
  603. else if (is_cppi_enabled(hw_ep->musb) || tusb_dma_omap(hw_ep->musb))
  604. musb_tx_dma_set_mode_cppi_tusb(dma, hw_ep, qh, urb, offset,
  605. &length, &mode);
  606. else
  607. return false;
  608. qh->segsize = length;
  609. /*
  610. * Ensure the data reaches to main memory before starting
  611. * DMA transfer
  612. */
  613. wmb();
  614. if (!dma->channel_program(channel, pkt_size, mode,
  615. urb->transfer_dma + offset, length)) {
  616. void __iomem *epio = hw_ep->regs;
  617. u16 csr;
  618. dma->channel_release(channel);
  619. hw_ep->tx_channel = NULL;
  620. csr = musb_readw(epio, MUSB_TXCSR);
  621. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
  622. musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
  623. return false;
  624. }
  625. return true;
  626. }
  627. #if NICHOLAS_ADD
  628. void musb_dma_channel_release(struct musb *musb)
  629. {
  630. struct dma_controller *dma_controller;
  631. struct musb_hw_ep *hw_ep;
  632. u8 i;
  633. dma_controller = musb->dma_controller;
  634. for(i=0; (i<musb->config->num_eps) && (i<MUSB_C_NUM_EPS); i++)
  635. {
  636. hw_ep = musb->endpoints + i;
  637. if(hw_ep->rx_channel)
  638. {
  639. dma_controller->channel_release(hw_ep->rx_channel);
  640. hw_ep->rx_channel = NULL;
  641. }
  642. if(hw_ep->tx_channel)
  643. {
  644. dma_controller->channel_release(hw_ep->tx_channel);
  645. hw_ep->tx_channel = NULL;
  646. }
  647. }
  648. }
  649. #endif
  650. /*
  651. * Program an HDRC endpoint as per the given URB
  652. * Context: irqs blocked, controller lock held
  653. */
  654. static void musb_ep_program(struct musb *musb, u8 epnum,
  655. struct urb *urb, int is_out,
  656. u8 *buf, u32 offset, u32 len)
  657. {
  658. struct dma_controller *dma_controller;
  659. struct dma_channel *dma_channel;
  660. u8 dma_ok;
  661. void __iomem *mbase = musb->mregs;
  662. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  663. void __iomem *epio = hw_ep->regs;
  664. struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
  665. u16 packet_sz = qh->maxpacket;
  666. u8 use_dma = 1;
  667. u16 csr;
  668. musb_dbg(musb, "%s hw%d urb %p spd%d dev%d ep%d%s "
  669. "h_addr%02x h_port%02x bytes %d",
  670. is_out ? "-->" : "<--",
  671. epnum, urb, urb->dev->speed,
  672. qh->addr_reg, qh->epnum, is_out ? "out" : "in",
  673. qh->h_addr_reg, qh->h_port_reg,
  674. len);
  675. musb_ep_select(mbase, epnum);
  676. if (is_out && !len) {
  677. use_dma = 0;
  678. csr = musb_readw(epio, MUSB_TXCSR);
  679. csr &= ~MUSB_TXCSR_DMAENAB;
  680. musb_writew(epio, MUSB_TXCSR, csr);
  681. hw_ep->tx_channel = NULL;
  682. }
  683. /* candidate for DMA? */
  684. dma_controller = musb->dma_controller;
  685. if (use_dma && is_dma_capable() && epnum && dma_controller) {
  686. dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
  687. if (!dma_channel) {
  688. dma_channel = dma_controller->channel_alloc(
  689. dma_controller, hw_ep, is_out);
  690. if (is_out)
  691. hw_ep->tx_channel = dma_channel;
  692. else
  693. hw_ep->rx_channel = dma_channel;
  694. }
  695. } else
  696. dma_channel = NULL;
  697. /* make sure we clear DMAEnab, autoSet bits from previous run */
  698. /* OUT/transmit/EP0 or IN/receive? */
  699. if (is_out) {
  700. u16 csr;
  701. u16 int_txe;
  702. u16 load_count;
  703. csr = musb_readw(epio, MUSB_TXCSR);
  704. /* disable interrupt in case we flush */
  705. int_txe = musb->intrtxe;
  706. musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
  707. /* general endpoint setup */
  708. if (epnum) {
  709. /* flush all old state, set default */
  710. /*
  711. * We could be flushing valid
  712. * packets in double buffering
  713. * case
  714. */
  715. if (!hw_ep->tx_double_buffered)
  716. musb_h_tx_flush_fifo(hw_ep);
  717. /*
  718. * We must not clear the DMAMODE bit before or in
  719. * the same cycle with the DMAENAB bit, so we clear
  720. * the latter first...
  721. */
  722. csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
  723. | MUSB_TXCSR_AUTOSET
  724. | MUSB_TXCSR_DMAENAB
  725. | MUSB_TXCSR_FRCDATATOG
  726. | MUSB_TXCSR_H_RXSTALL
  727. | MUSB_TXCSR_H_ERROR
  728. | MUSB_TXCSR_TXPKTRDY
  729. );
  730. csr |= MUSB_TXCSR_MODE;
  731. if (!hw_ep->tx_double_buffered) {
  732. if (usb_gettoggle(urb->dev, qh->epnum, 1))
  733. csr |= MUSB_TXCSR_H_WR_DATATOGGLE
  734. | MUSB_TXCSR_H_DATATOGGLE;
  735. else
  736. csr |= MUSB_TXCSR_CLRDATATOG;
  737. }
  738. musb_writew(epio, MUSB_TXCSR, csr);
  739. /* REVISIT may need to clear FLUSHFIFO ... */
  740. csr &= ~MUSB_TXCSR_DMAMODE;
  741. musb_writew(epio, MUSB_TXCSR, csr);
  742. csr = musb_readw(epio, MUSB_TXCSR);
  743. } else {
  744. /* endpoint 0: just flush */
  745. musb_h_ep0_flush_fifo(hw_ep);
  746. }
  747. /* target addr and (for multipoint) hub addr/port */
  748. if (musb->is_multipoint) {
  749. musb_write_txfunaddr(musb, epnum, qh->addr_reg);
  750. musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
  751. musb_write_txhubport(musb, epnum, qh->h_port_reg);
  752. /* FIXME if !epnum, do the same for RX ... */
  753. } else
  754. musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
  755. /* protocol/endpoint/interval/NAKlimit */
  756. if (epnum) {
  757. musb_ep_select(mbase, epnum);
  758. musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
  759. musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
  760. if (can_bulk_split(musb, qh->type)) {
  761. qh->hb_mult = hw_ep->max_packet_sz_tx
  762. / packet_sz;
  763. musb_writew(epio, MUSB_TXMAXP, packet_sz
  764. | ((qh->hb_mult) - 1) << 11);
  765. } else {
  766. musb_writew(epio, MUSB_TXMAXP,
  767. qh->maxpacket |
  768. ((qh->hb_mult - 1) << 11));
  769. }
  770. } else {
  771. musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
  772. if (musb->is_multipoint)
  773. musb_writeb(epio, MUSB_TYPE0,
  774. qh->type_reg);
  775. }
  776. #if NICHOLAS_ADD
  777. if (can_bulk_split(musb, qh->type))
  778. load_count = min((u32) hw_ep->max_packet_sz_tx,
  779. len);
  780. else
  781. load_count = min((u32) packet_sz, len);
  782. if (dma_channel && musb_tx_dma_program(dma_controller,
  783. hw_ep, qh, urb, offset, load_count))
  784. load_count = 0;
  785. #else
  786. if (can_bulk_split(musb, qh->type))
  787. load_count = min((u32) hw_ep->max_packet_sz_tx,
  788. len);
  789. else
  790. load_count = min((u32) packet_sz, len);
  791. if (dma_channel && musb_tx_dma_program(dma_controller,
  792. hw_ep, qh, urb, offset, len))
  793. load_count = 0;
  794. #endif
  795. if (load_count) {
  796. /* PIO to load FIFO */
  797. qh->segsize = load_count;
  798. if (!buf) {
  799. sg_miter_start(&qh->sg_miter, urb->sg, 1,
  800. SG_MITER_ATOMIC
  801. | SG_MITER_FROM_SG);
  802. if (!sg_miter_next(&qh->sg_miter)) {
  803. dev_err(musb->controller,
  804. "error: sg"
  805. "list empty\n");
  806. sg_miter_stop(&qh->sg_miter);
  807. goto finish;
  808. }
  809. buf = qh->sg_miter.addr + urb->sg->offset +
  810. urb->actual_length;
  811. load_count = min_t(u32, load_count,
  812. qh->sg_miter.length);
  813. musb_write_fifo(hw_ep, load_count, buf);
  814. qh->sg_miter.consumed = load_count;
  815. sg_miter_stop(&qh->sg_miter);
  816. } else
  817. musb_write_fifo(hw_ep, load_count, buf);
  818. }
  819. finish:
  820. /* re-enable interrupt */
  821. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  822. /* IN/receive */
  823. } else {
  824. u16 csr;
  825. if (hw_ep->rx_reinit) {
  826. musb_rx_reinit(musb, qh, epnum);
  827. /* init new state: toggle and NYET, maybe DMA later */
  828. if (usb_gettoggle(urb->dev, qh->epnum, 0))
  829. csr = MUSB_RXCSR_H_WR_DATATOGGLE
  830. | MUSB_RXCSR_H_DATATOGGLE;
  831. else
  832. #if NICHOLAS_ADD
  833. csr |= MUSB_RXCSR_CLRDATATOG;
  834. #else
  835. csr = 0;
  836. #endif
  837. if (qh->type == USB_ENDPOINT_XFER_INT)
  838. csr |= MUSB_RXCSR_DISNYET;
  839. } else {
  840. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  841. if (csr & (MUSB_RXCSR_RXPKTRDY
  842. | MUSB_RXCSR_DMAENAB
  843. | MUSB_RXCSR_H_REQPKT))
  844. ERR("broken !rx_reinit, ep%d csr %04x\n",
  845. hw_ep->epnum, csr);
  846. /* scrub any stale state, leaving toggle alone */
  847. csr &= MUSB_RXCSR_DISNYET;
  848. }
  849. /* kick things off */
  850. if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
  851. /* Candidate for DMA */
  852. dma_channel->actual_len = 0L;
  853. qh->segsize = len;
  854. /* AUTOREQ is in a DMA register */
  855. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  856. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  857. /*
  858. * Unless caller treats short RX transfers as
  859. * errors, we dare not queue multiple transfers.
  860. */
  861. dma_ok = dma_controller->channel_program(dma_channel,
  862. packet_sz, !(urb->transfer_flags &
  863. URB_SHORT_NOT_OK),
  864. urb->transfer_dma + offset,
  865. qh->segsize);
  866. if (!dma_ok) {
  867. dma_controller->channel_release(dma_channel);
  868. hw_ep->rx_channel = dma_channel = NULL;
  869. } else
  870. csr |= MUSB_RXCSR_DMAENAB;
  871. }
  872. csr |= MUSB_RXCSR_H_REQPKT;
  873. musb_dbg(musb, "RXCSR%d := %04x", epnum, csr);
  874. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  875. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  876. }
  877. }
  878. /* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
  879. * the end; avoids starvation for other endpoints.
  880. */
  881. static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
  882. int is_in)
  883. {
  884. struct dma_channel *dma;
  885. struct urb *urb;
  886. void __iomem *mbase = musb->mregs;
  887. void __iomem *epio = ep->regs;
  888. struct musb_qh *cur_qh, *next_qh;
  889. u16 rx_csr, tx_csr;
  890. musb_ep_select(mbase, ep->epnum);
  891. if (is_in) {
  892. dma = is_dma_capable() ? ep->rx_channel : NULL;
  893. /*
  894. * Need to stop the transaction by clearing REQPKT first
  895. * then the NAK Timeout bit ref MUSBMHDRC USB 2.0 HIGH-SPEED
  896. * DUAL-ROLE CONTROLLER Programmer's Guide, section 9.2.2
  897. */
  898. rx_csr = musb_readw(epio, MUSB_RXCSR);
  899. rx_csr |= MUSB_RXCSR_H_WZC_BITS;
  900. rx_csr &= ~MUSB_RXCSR_H_REQPKT;
  901. musb_writew(epio, MUSB_RXCSR, rx_csr);
  902. rx_csr &= ~MUSB_RXCSR_DATAERROR;
  903. musb_writew(epio, MUSB_RXCSR, rx_csr);
  904. cur_qh = first_qh(&musb->in_bulk);
  905. } else {
  906. dma = is_dma_capable() ? ep->tx_channel : NULL;
  907. /* clear nak timeout bit */
  908. tx_csr = musb_readw(epio, MUSB_TXCSR);
  909. tx_csr |= MUSB_TXCSR_H_WZC_BITS;
  910. tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
  911. musb_writew(epio, MUSB_TXCSR, tx_csr);
  912. cur_qh = first_qh(&musb->out_bulk);
  913. }
  914. if (cur_qh) {
  915. urb = next_urb(cur_qh);
  916. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  917. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  918. musb->dma_controller->channel_abort(dma);
  919. urb->actual_length += dma->actual_len;
  920. dma->actual_len = 0L;
  921. }
  922. musb_save_toggle(cur_qh, is_in, urb);
  923. if (is_in) {
  924. /* move cur_qh to end of queue */
  925. list_move_tail(&cur_qh->ring, &musb->in_bulk);
  926. /* get the next qh from musb->in_bulk */
  927. next_qh = first_qh(&musb->in_bulk);
  928. /* set rx_reinit and schedule the next qh */
  929. ep->rx_reinit = 1;
  930. } else {
  931. /* move cur_qh to end of queue */
  932. list_move_tail(&cur_qh->ring, &musb->out_bulk);
  933. /* get the next qh from musb->out_bulk */
  934. next_qh = first_qh(&musb->out_bulk);
  935. /* set tx_reinit and schedule the next qh */
  936. ep->tx_reinit = 1;
  937. }
  938. if (next_qh)
  939. musb_start_urb(musb, is_in, next_qh);
  940. }
  941. }
  942. /*
  943. * Service the default endpoint (ep0) as host.
  944. * Return true until it's time to start the status stage.
  945. */
  946. static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
  947. {
  948. bool more = false;
  949. u8 *fifo_dest = NULL;
  950. u16 fifo_count = 0;
  951. struct musb_hw_ep *hw_ep = musb->control_ep;
  952. struct musb_qh *qh = hw_ep->in_qh;
  953. struct usb_ctrlrequest *request;
  954. switch (musb->ep0_stage) {
  955. case MUSB_EP0_IN:
  956. fifo_dest = urb->transfer_buffer + urb->actual_length;
  957. fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
  958. urb->actual_length);
  959. if (fifo_count < len)
  960. urb->status = -EOVERFLOW;
  961. musb_read_fifo(hw_ep, fifo_count, fifo_dest);
  962. urb->actual_length += fifo_count;
  963. if (len < qh->maxpacket) {
  964. /* always terminate on short read; it's
  965. * rarely reported as an error.
  966. */
  967. } else if (urb->actual_length <
  968. urb->transfer_buffer_length)
  969. more = true;
  970. break;
  971. case MUSB_EP0_START:
  972. request = (struct usb_ctrlrequest *) urb->setup_packet;
  973. if (!request->wLength) {
  974. musb_dbg(musb, "start no-DATA");
  975. break;
  976. } else if (request->bRequestType & USB_DIR_IN) {
  977. musb_dbg(musb, "start IN-DATA");
  978. musb->ep0_stage = MUSB_EP0_IN;
  979. more = true;
  980. break;
  981. } else {
  982. musb_dbg(musb, "start OUT-DATA");
  983. musb->ep0_stage = MUSB_EP0_OUT;
  984. more = true;
  985. }
  986. /* FALLTHROUGH */
  987. case MUSB_EP0_OUT:
  988. fifo_count = min_t(size_t, qh->maxpacket,
  989. urb->transfer_buffer_length -
  990. urb->actual_length);
  991. if (fifo_count) {
  992. fifo_dest = (u8 *) (urb->transfer_buffer
  993. + urb->actual_length);
  994. musb_dbg(musb, "Sending %d byte%s to ep0 fifo %p",
  995. fifo_count,
  996. (fifo_count == 1) ? "" : "s",
  997. fifo_dest);
  998. musb_write_fifo(hw_ep, fifo_count, fifo_dest);
  999. urb->actual_length += fifo_count;
  1000. more = true;
  1001. }
  1002. break;
  1003. default:
  1004. ERR("bogus ep0 stage %d\n", musb->ep0_stage);
  1005. break;
  1006. }
  1007. return more;
  1008. }
  1009. /*
  1010. * Handle default endpoint interrupt as host. Only called in IRQ time
  1011. * from musb_interrupt().
  1012. *
  1013. * called with controller irqlocked
  1014. */
  1015. irqreturn_t musb_h_ep0_irq(struct musb *musb)
  1016. {
  1017. struct urb *urb;
  1018. u16 csr, len;
  1019. int status = 0;
  1020. void __iomem *mbase = musb->mregs;
  1021. struct musb_hw_ep *hw_ep = musb->control_ep;
  1022. void __iomem *epio = hw_ep->regs;
  1023. struct musb_qh *qh = hw_ep->in_qh;
  1024. bool complete = false;
  1025. irqreturn_t retval = IRQ_NONE;
  1026. /* ep0 only has one queue, "in" */
  1027. urb = next_urb(qh);
  1028. musb_ep_select(mbase, 0);
  1029. csr = musb_readw(epio, MUSB_CSR0);
  1030. len = (csr & MUSB_CSR0_RXPKTRDY)
  1031. ? musb_readb(epio, MUSB_COUNT0)
  1032. : 0;
  1033. musb_dbg(musb, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d",
  1034. csr, qh, len, urb, musb->ep0_stage);
  1035. /* if we just did status stage, we are done */
  1036. if (MUSB_EP0_STATUS == musb->ep0_stage) {
  1037. retval = IRQ_HANDLED;
  1038. complete = true;
  1039. }
  1040. /* prepare status */
  1041. if (csr & MUSB_CSR0_H_RXSTALL) {
  1042. musb_dbg(musb, "STALLING ENDPOINT");
  1043. status = -EPIPE;
  1044. } else if (csr & MUSB_CSR0_H_ERROR) {
  1045. musb_dbg(musb, "no response, csr0 %04x", csr);
  1046. status = -EPROTO;
  1047. } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
  1048. musb_dbg(musb, "control NAK timeout");
  1049. /* NOTE: this code path would be a good place to PAUSE a
  1050. * control transfer, if another one is queued, so that
  1051. * ep0 is more likely to stay busy. That's already done
  1052. * for bulk RX transfers.
  1053. *
  1054. * if (qh->ring.next != &musb->control), then
  1055. * we have a candidate... NAKing is *NOT* an error
  1056. */
  1057. musb_writew(epio, MUSB_CSR0, 0);
  1058. retval = IRQ_HANDLED;
  1059. }
  1060. if (status) {
  1061. musb_dbg(musb, "aborting");
  1062. retval = IRQ_HANDLED;
  1063. if (urb)
  1064. urb->status = status;
  1065. complete = true;
  1066. /* use the proper sequence to abort the transfer */
  1067. if (csr & MUSB_CSR0_H_REQPKT) {
  1068. csr &= ~MUSB_CSR0_H_REQPKT;
  1069. musb_writew(epio, MUSB_CSR0, csr);
  1070. csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
  1071. musb_writew(epio, MUSB_CSR0, csr);
  1072. } else {
  1073. musb_h_ep0_flush_fifo(hw_ep);
  1074. }
  1075. musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
  1076. /* clear it */
  1077. musb_writew(epio, MUSB_CSR0, 0);
  1078. }
  1079. if (unlikely(!urb)) {
  1080. /* stop endpoint since we have no place for its data, this
  1081. * SHOULD NEVER HAPPEN! */
  1082. ERR("no URB for end 0\n");
  1083. musb_h_ep0_flush_fifo(hw_ep);
  1084. goto done;
  1085. }
  1086. if (!complete) {
  1087. /* call common logic and prepare response */
  1088. if (musb_h_ep0_continue(musb, len, urb)) {
  1089. /* more packets required */
  1090. csr = (MUSB_EP0_IN == musb->ep0_stage)
  1091. ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
  1092. } else {
  1093. /* data transfer complete; perform status phase */
  1094. if (usb_pipeout(urb->pipe)
  1095. || !urb->transfer_buffer_length)
  1096. csr = MUSB_CSR0_H_STATUSPKT
  1097. | MUSB_CSR0_H_REQPKT;
  1098. else
  1099. csr = MUSB_CSR0_H_STATUSPKT
  1100. | MUSB_CSR0_TXPKTRDY;
  1101. /* disable ping token in status phase */
  1102. csr |= MUSB_CSR0_H_DIS_PING;
  1103. /* flag status stage */
  1104. musb->ep0_stage = MUSB_EP0_STATUS;
  1105. musb_dbg(musb, "ep0 STATUS, csr %04x", csr);
  1106. }
  1107. musb_writew(epio, MUSB_CSR0, csr);
  1108. retval = IRQ_HANDLED;
  1109. } else
  1110. musb->ep0_stage = MUSB_EP0_IDLE;
  1111. /* call completion handler if done */
  1112. if (complete)
  1113. musb_advance_schedule(musb, urb, hw_ep, 1);
  1114. done:
  1115. return retval;
  1116. }
  1117. #ifdef CONFIG_USB_INVENTRA_DMA
  1118. /* Host side TX (OUT) using Mentor DMA works as follows:
  1119. submit_urb ->
  1120. - if queue was empty, Program Endpoint
  1121. - ... which starts DMA to fifo in mode 1 or 0
  1122. DMA Isr (transfer complete) -> TxAvail()
  1123. - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
  1124. only in musb_cleanup_urb)
  1125. - TxPktRdy has to be set in mode 0 or for
  1126. short packets in mode 1.
  1127. */
  1128. #endif
  1129. /* Service a Tx-Available or dma completion irq for the endpoint */
  1130. void musb_host_tx(struct musb *musb, u8 epnum)
  1131. {
  1132. int pipe;
  1133. bool done = false;
  1134. u16 tx_csr;
  1135. size_t length = 0;
  1136. size_t offset = 0;
  1137. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1138. void __iomem *epio = hw_ep->regs;
  1139. struct musb_qh *qh = hw_ep->out_qh;
  1140. struct urb *urb = next_urb(qh);
  1141. u32 status = 0;
  1142. void __iomem *mbase = musb->mregs;
  1143. struct dma_channel *dma;
  1144. bool transfer_pending = false;
  1145. musb_ep_select(mbase, epnum);
  1146. tx_csr = musb_readw(epio, MUSB_TXCSR);
  1147. /* with CPPI, DMA sometimes triggers "extra" irqs */
  1148. if (!urb) {
  1149. musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
  1150. return;
  1151. }
  1152. pipe = urb->pipe;
  1153. dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
  1154. trace_musb_urb_tx(musb, urb);
  1155. musb_dbg(musb, "OUT/TX%d end, csr %04x%s", epnum, tx_csr,
  1156. dma ? ", dma" : "");
  1157. /* check for errors */
  1158. if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
  1159. /* dma was disabled, fifo flushed */
  1160. musb_dbg(musb, "TX end %d stall", epnum);
  1161. /* stall; record URB status */
  1162. status = -EPIPE;
  1163. } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
  1164. /* (NON-ISO) dma was disabled, fifo flushed */
  1165. musb_dbg(musb, "TX 3strikes on ep=%d", epnum);
  1166. #if NICHOLAS_ADD
  1167. musb_writew(epio, MUSB_TXCSR,
  1168. MUSB_TXCSR_H_WZC_BITS
  1169. | MUSB_TXCSR_TXPKTRDY);
  1170. if(qh->intv_reg == UDISK_INTERVAL){
  1171. //Is U Disk
  1172. status = -ETIMEDOUT;
  1173. }
  1174. else{
  1175. //IS Phone
  1176. return;
  1177. }
  1178. #else
  1179. status = -ETIMEDOUT;
  1180. #endif
  1181. } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
  1182. if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
  1183. && !list_is_singular(&musb->out_bulk)) {
  1184. musb_dbg(musb, "NAK timeout on TX%d ep", epnum);
  1185. musb_bulk_nak_timeout(musb, hw_ep, 0);
  1186. } else {
  1187. musb_dbg(musb, "TX ep%d device not responding", epnum);
  1188. /* NOTE: this code path would be a good place to PAUSE a
  1189. * transfer, if there's some other (nonperiodic) tx urb
  1190. * that could use this fifo. (dma complicates it...)
  1191. * That's already done for bulk RX transfers.
  1192. *
  1193. * if (bulk && qh->ring.next != &musb->out_bulk), then
  1194. * we have a candidate... NAKing is *NOT* an error
  1195. */
  1196. musb_ep_select(mbase, epnum);
  1197. musb_writew(epio, MUSB_TXCSR,
  1198. MUSB_TXCSR_H_WZC_BITS
  1199. | MUSB_TXCSR_TXPKTRDY);
  1200. }
  1201. return;
  1202. }
  1203. done:
  1204. if (status) {
  1205. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1206. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1207. musb->dma_controller->channel_abort(dma);
  1208. }
  1209. /* do the proper sequence to abort the transfer in the
  1210. * usb core; the dma engine should already be stopped.
  1211. */
  1212. musb_h_tx_flush_fifo(hw_ep);
  1213. tx_csr &= ~(MUSB_TXCSR_AUTOSET
  1214. | MUSB_TXCSR_DMAENAB
  1215. | MUSB_TXCSR_H_ERROR
  1216. | MUSB_TXCSR_H_RXSTALL
  1217. | MUSB_TXCSR_H_NAKTIMEOUT
  1218. );
  1219. musb_ep_select(mbase, epnum);
  1220. musb_writew(epio, MUSB_TXCSR, tx_csr);
  1221. /* REVISIT may need to clear FLUSHFIFO ... */
  1222. musb_writew(epio, MUSB_TXCSR, tx_csr);
  1223. musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
  1224. done = true;
  1225. }
  1226. /* second cppi case */
  1227. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1228. musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
  1229. return;
  1230. }
  1231. if (is_dma_capable() && dma && !status) {
  1232. /*
  1233. * DMA has completed. But if we're using DMA mode 1 (multi
  1234. * packet DMA), we need a terminal TXPKTRDY interrupt before
  1235. * we can consider this transfer completed, lest we trash
  1236. * its last packet when writing the next URB's data. So we
  1237. * switch back to mode 0 to get that interrupt; we'll come
  1238. * back here once it happens.
  1239. */
  1240. if (tx_csr & MUSB_TXCSR_DMAMODE) {
  1241. /*
  1242. * We shouldn't clear DMAMODE with DMAENAB set; so
  1243. * clear them in a safe order. That should be OK
  1244. * once TXPKTRDY has been set (and I've never seen
  1245. * it being 0 at this moment -- DMA interrupt latency
  1246. * is significant) but if it hasn't been then we have
  1247. * no choice but to stop being polite and ignore the
  1248. * programmer's guide... :-)
  1249. *
  1250. * Note that we must write TXCSR with TXPKTRDY cleared
  1251. * in order not to re-trigger the packet send (this bit
  1252. * can't be cleared by CPU), and there's another caveat:
  1253. * TXPKTRDY may be set shortly and then cleared in the
  1254. * double-buffered FIFO mode, so we do an extra TXCSR
  1255. * read for debouncing...
  1256. */
  1257. tx_csr &= musb_readw(epio, MUSB_TXCSR);
  1258. if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
  1259. tx_csr &= ~(MUSB_TXCSR_DMAENAB |
  1260. MUSB_TXCSR_TXPKTRDY);
  1261. musb_writew(epio, MUSB_TXCSR,
  1262. tx_csr | MUSB_TXCSR_H_WZC_BITS);
  1263. }
  1264. tx_csr &= ~(MUSB_TXCSR_DMAMODE |
  1265. MUSB_TXCSR_TXPKTRDY);
  1266. musb_writew(epio, MUSB_TXCSR,
  1267. tx_csr | MUSB_TXCSR_H_WZC_BITS);
  1268. /*
  1269. * There is no guarantee that we'll get an interrupt
  1270. * after clearing DMAMODE as we might have done this
  1271. * too late (after TXPKTRDY was cleared by controller).
  1272. * Re-read TXCSR as we have spoiled its previous value.
  1273. */
  1274. tx_csr = musb_readw(epio, MUSB_TXCSR);
  1275. }
  1276. /*
  1277. * We may get here from a DMA completion or TXPKTRDY interrupt.
  1278. * In any case, we must check the FIFO status here and bail out
  1279. * only if the FIFO still has data -- that should prevent the
  1280. * "missed" TXPKTRDY interrupts and deal with double-buffered
  1281. * FIFO mode too...
  1282. */
  1283. if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
  1284. musb_dbg(musb,
  1285. "DMA complete but FIFO not empty, CSR %04x",
  1286. tx_csr);
  1287. return;
  1288. }
  1289. }
  1290. if (!status || dma || usb_pipeisoc(pipe)) {
  1291. if (dma)
  1292. length = dma->actual_len;
  1293. else
  1294. length = qh->segsize;
  1295. qh->offset += length;
  1296. if (usb_pipeisoc(pipe)) {
  1297. struct usb_iso_packet_descriptor *d;
  1298. d = urb->iso_frame_desc + qh->iso_idx;
  1299. d->actual_length = length;
  1300. d->status = status;
  1301. if (++qh->iso_idx >= urb->number_of_packets) {
  1302. done = true;
  1303. } else {
  1304. d++;
  1305. offset = d->offset;
  1306. length = d->length;
  1307. }
  1308. } else if (dma && urb->transfer_buffer_length == qh->offset) {
  1309. done = true;
  1310. } else {
  1311. /* see if we need to send more data, or ZLP */
  1312. if (qh->segsize < qh->maxpacket)
  1313. done = true;
  1314. else if (qh->offset == urb->transfer_buffer_length
  1315. && !(urb->transfer_flags
  1316. & URB_ZERO_PACKET))
  1317. done = true;
  1318. if (!done) {
  1319. offset = qh->offset;
  1320. #if NICHOLAS_ADD
  1321. if (can_bulk_split(musb, qh->type))
  1322. length = min((u32) hw_ep->max_packet_sz_tx, urb->transfer_buffer_length - offset);
  1323. else
  1324. length = min((u32) qh->maxpacket, urb->transfer_buffer_length - offset);
  1325. #else
  1326. length = urb->transfer_buffer_length - offset;
  1327. #endif
  1328. transfer_pending = true;
  1329. }
  1330. }
  1331. }
  1332. /* urb->status != -EINPROGRESS means request has been faulted,
  1333. * so we must abort this transfer after cleanup
  1334. */
  1335. if (urb->status != -EINPROGRESS) {
  1336. done = true;
  1337. if (status == 0)
  1338. status = urb->status;
  1339. }
  1340. if (done) {
  1341. /* set status */
  1342. urb->status = status;
  1343. urb->actual_length = qh->offset;
  1344. musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
  1345. return;
  1346. } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
  1347. if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
  1348. offset, length)) {
  1349. if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
  1350. musb_h_tx_dma_start(hw_ep);
  1351. return;
  1352. }
  1353. } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
  1354. musb_dbg(musb, "not complete, but DMA enabled?");
  1355. return;
  1356. }
  1357. /*
  1358. * PIO: start next packet in this URB.
  1359. *
  1360. * REVISIT: some docs say that when hw_ep->tx_double_buffered,
  1361. * (and presumably, FIFO is not half-full) we should write *two*
  1362. * packets before updating TXCSR; other docs disagree...
  1363. */
  1364. if (length > qh->maxpacket)
  1365. length = qh->maxpacket;
  1366. /* Unmap the buffer so that CPU can use it */
  1367. usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
  1368. /*
  1369. * We need to map sg if the transfer_buffer is
  1370. * NULL.
  1371. */
  1372. if (!urb->transfer_buffer) {
  1373. /* sg_miter_start is already done in musb_ep_program */
  1374. if (!sg_miter_next(&qh->sg_miter)) {
  1375. dev_err(musb->controller, "error: sg list empty\n");
  1376. sg_miter_stop(&qh->sg_miter);
  1377. status = -EINVAL;
  1378. goto done;
  1379. }
  1380. length = min_t(u32, length, qh->sg_miter.length);
  1381. musb_write_fifo(hw_ep, length, qh->sg_miter.addr);
  1382. qh->sg_miter.consumed = length;
  1383. sg_miter_stop(&qh->sg_miter);
  1384. } else {
  1385. musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
  1386. }
  1387. qh->segsize = length;
  1388. musb_ep_select(mbase, epnum);
  1389. #if NICHOLAS_ADD
  1390. tx_csr = musb_readw(epio, MUSB_TXCSR);
  1391. musb_writew(epio, MUSB_TXCSR,
  1392. tx_csr | MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
  1393. #else
  1394. musb_writew(epio, MUSB_TXCSR,
  1395. MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
  1396. #endif
  1397. }
  1398. #ifdef CONFIG_USB_TI_CPPI41_DMA
  1399. /* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
  1400. static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
  1401. struct musb_hw_ep *hw_ep,
  1402. struct musb_qh *qh,
  1403. struct urb *urb,
  1404. size_t len)
  1405. {
  1406. struct dma_channel *channel = hw_ep->rx_channel;
  1407. void __iomem *epio = hw_ep->regs;
  1408. dma_addr_t *buf;
  1409. u32 length;
  1410. u16 val;
  1411. buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
  1412. (u32)urb->transfer_dma;
  1413. length = urb->iso_frame_desc[qh->iso_idx].length;
  1414. val = musb_readw(epio, MUSB_RXCSR);
  1415. val |= MUSB_RXCSR_DMAENAB;
  1416. musb_writew(hw_ep->regs, MUSB_RXCSR, val);
  1417. return dma->channel_program(channel, qh->maxpacket, 0,
  1418. (u32)buf, length);
  1419. }
  1420. #else
  1421. static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
  1422. struct musb_hw_ep *hw_ep,
  1423. struct musb_qh *qh,
  1424. struct urb *urb,
  1425. size_t len)
  1426. {
  1427. return false;
  1428. }
  1429. #endif
  1430. #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
  1431. defined(CONFIG_USB_TI_CPPI41_DMA)
  1432. /* Host side RX (IN) using Mentor DMA works as follows:
  1433. submit_urb ->
  1434. - if queue was empty, ProgramEndpoint
  1435. - first IN token is sent out (by setting ReqPkt)
  1436. LinuxIsr -> RxReady()
  1437. /\ => first packet is received
  1438. | - Set in mode 0 (DmaEnab, ~ReqPkt)
  1439. | -> DMA Isr (transfer complete) -> RxReady()
  1440. | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
  1441. | - if urb not complete, send next IN token (ReqPkt)
  1442. | | else complete urb.
  1443. | |
  1444. ---------------------------
  1445. *
  1446. * Nuances of mode 1:
  1447. * For short packets, no ack (+RxPktRdy) is sent automatically
  1448. * (even if AutoClear is ON)
  1449. * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
  1450. * automatically => major problem, as collecting the next packet becomes
  1451. * difficult. Hence mode 1 is not used.
  1452. *
  1453. * REVISIT
  1454. * All we care about at this driver level is that
  1455. * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
  1456. * (b) termination conditions are: short RX, or buffer full;
  1457. * (c) fault modes include
  1458. * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
  1459. * (and that endpoint's dma queue stops immediately)
  1460. * - overflow (full, PLUS more bytes in the terminal packet)
  1461. *
  1462. * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
  1463. * thus be a great candidate for using mode 1 ... for all but the
  1464. * last packet of one URB's transfer.
  1465. */
  1466. static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
  1467. struct musb_hw_ep *hw_ep,
  1468. struct musb_qh *qh,
  1469. struct urb *urb,
  1470. size_t len)
  1471. {
  1472. struct dma_channel *channel = hw_ep->rx_channel;
  1473. void __iomem *epio = hw_ep->regs;
  1474. u16 val;
  1475. int pipe;
  1476. bool done;
  1477. pipe = urb->pipe;
  1478. if (usb_pipeisoc(pipe)) {
  1479. struct usb_iso_packet_descriptor *d;
  1480. d = urb->iso_frame_desc + qh->iso_idx;
  1481. d->actual_length = len;
  1482. /* even if there was an error, we did the dma
  1483. * for iso_frame_desc->length
  1484. */
  1485. if (d->status != -EILSEQ && d->status != -EOVERFLOW)
  1486. d->status = 0;
  1487. if (++qh->iso_idx >= urb->number_of_packets) {
  1488. done = true;
  1489. } else {
  1490. /* REVISIT: Why ignore return value here? */
  1491. if (musb_dma_cppi41(hw_ep->musb))
  1492. done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
  1493. urb, len);
  1494. done = false;
  1495. }
  1496. } else {
  1497. /* done if urb buffer is full or short packet is recd */
  1498. done = (urb->actual_length + len >=
  1499. urb->transfer_buffer_length
  1500. || channel->actual_len < qh->maxpacket
  1501. || channel->rx_packet_done);
  1502. }
  1503. /* send IN token for next packet, without AUTOREQ */
  1504. if (!done) {
  1505. val = musb_readw(epio, MUSB_RXCSR);
  1506. val |= MUSB_RXCSR_H_REQPKT;
  1507. musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
  1508. }
  1509. return done;
  1510. }
  1511. /* Disadvantage of using mode 1:
  1512. * It's basically usable only for mass storage class; essentially all
  1513. * other protocols also terminate transfers on short packets.
  1514. *
  1515. * Details:
  1516. * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
  1517. * If you try to use mode 1 for (transfer_buffer_length - 512), and try
  1518. * to use the extra IN token to grab the last packet using mode 0, then
  1519. * the problem is that you cannot be sure when the device will send the
  1520. * last packet and RxPktRdy set. Sometimes the packet is recd too soon
  1521. * such that it gets lost when RxCSR is re-set at the end of the mode 1
  1522. * transfer, while sometimes it is recd just a little late so that if you
  1523. * try to configure for mode 0 soon after the mode 1 transfer is
  1524. * completed, you will find rxcount 0. Okay, so you might think why not
  1525. * wait for an interrupt when the pkt is recd. Well, you won't get any!
  1526. */
  1527. static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
  1528. struct musb_hw_ep *hw_ep,
  1529. struct musb_qh *qh,
  1530. struct urb *urb,
  1531. size_t len,
  1532. u8 iso_err)
  1533. {
  1534. struct musb *musb = hw_ep->musb;
  1535. void __iomem *epio = hw_ep->regs;
  1536. struct dma_channel *channel = hw_ep->rx_channel;
  1537. u16 rx_count, val;
  1538. int length, pipe, done;
  1539. dma_addr_t buf;
  1540. rx_count = musb_readw(epio, MUSB_RXCOUNT);
  1541. pipe = urb->pipe;
  1542. if (usb_pipeisoc(pipe)) {
  1543. int d_status = 0;
  1544. struct usb_iso_packet_descriptor *d;
  1545. d = urb->iso_frame_desc + qh->iso_idx;
  1546. if (iso_err) {
  1547. d_status = -EILSEQ;
  1548. urb->error_count++;
  1549. }
  1550. if (rx_count > d->length) {
  1551. if (d_status == 0) {
  1552. d_status = -EOVERFLOW;
  1553. urb->error_count++;
  1554. }
  1555. musb_dbg(musb, "** OVERFLOW %d into %d",
  1556. rx_count, d->length);
  1557. length = d->length;
  1558. } else
  1559. length = rx_count;
  1560. d->status = d_status;
  1561. buf = urb->transfer_dma + d->offset;
  1562. } else {
  1563. length = rx_count;
  1564. buf = urb->transfer_dma + urb->actual_length;
  1565. }
  1566. channel->desired_mode = 0;
  1567. #ifdef USE_MODE1
  1568. /* because of the issue below, mode 1 will
  1569. * only rarely behave with correct semantics.
  1570. */
  1571. if ((urb->transfer_flags & URB_SHORT_NOT_OK)
  1572. && (urb->transfer_buffer_length - urb->actual_length)
  1573. > qh->maxpacket)
  1574. channel->desired_mode = 1;
  1575. if (rx_count < hw_ep->max_packet_sz_rx) {
  1576. length = rx_count;
  1577. channel->desired_mode = 0;
  1578. } else {
  1579. length = urb->transfer_buffer_length;
  1580. }
  1581. #endif
  1582. /* See comments above on disadvantages of using mode 1 */
  1583. val = musb_readw(epio, MUSB_RXCSR);
  1584. val &= ~MUSB_RXCSR_H_REQPKT;
  1585. if (channel->desired_mode == 0)
  1586. val &= ~MUSB_RXCSR_H_AUTOREQ;
  1587. else
  1588. val |= MUSB_RXCSR_H_AUTOREQ;
  1589. val |= MUSB_RXCSR_DMAENAB;
  1590. /* autoclear shouldn't be set in high bandwidth */
  1591. if (qh->hb_mult == 1)
  1592. val |= MUSB_RXCSR_AUTOCLEAR;
  1593. musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
  1594. /* REVISIT if when actual_length != 0,
  1595. * transfer_buffer_length needs to be
  1596. * adjusted first...
  1597. */
  1598. #if NICHOLAS_ADD
  1599. //Nicholas fix bug
  1600. if(length > urb->transfer_buffer_length)
  1601. length = urb->transfer_buffer_length;
  1602. #endif
  1603. done = dma->channel_program(channel, qh->maxpacket,
  1604. channel->desired_mode,
  1605. buf, length);
  1606. if (!done) {
  1607. dma->channel_release(channel);
  1608. hw_ep->rx_channel = NULL;
  1609. channel = NULL;
  1610. val = musb_readw(epio, MUSB_RXCSR);
  1611. val &= ~(MUSB_RXCSR_DMAENAB
  1612. | MUSB_RXCSR_H_AUTOREQ
  1613. | MUSB_RXCSR_AUTOCLEAR);
  1614. musb_writew(epio, MUSB_RXCSR, val);
  1615. }
  1616. return done;
  1617. }
  1618. #else
  1619. static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
  1620. struct musb_hw_ep *hw_ep,
  1621. struct musb_qh *qh,
  1622. struct urb *urb,
  1623. size_t len)
  1624. {
  1625. return false;
  1626. }
  1627. static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
  1628. struct musb_hw_ep *hw_ep,
  1629. struct musb_qh *qh,
  1630. struct urb *urb,
  1631. size_t len,
  1632. u8 iso_err)
  1633. {
  1634. return false;
  1635. }
  1636. #endif
  1637. /*
  1638. * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
  1639. * and high-bandwidth IN transfer cases.
  1640. */
  1641. void musb_host_rx(struct musb *musb, u8 epnum)
  1642. {
  1643. struct urb *urb;
  1644. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1645. struct dma_controller *c = musb->dma_controller;
  1646. void __iomem *epio = hw_ep->regs;
  1647. struct musb_qh *qh = hw_ep->in_qh;
  1648. size_t xfer_len;
  1649. void __iomem *mbase = musb->mregs;
  1650. u16 rx_csr, val;
  1651. bool iso_err = false;
  1652. bool done = false;
  1653. u32 status;
  1654. struct dma_channel *dma;
  1655. unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
  1656. musb_ep_select(mbase, epnum);
  1657. urb = next_urb(qh);
  1658. dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
  1659. status = 0;
  1660. xfer_len = 0;
  1661. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1662. val = rx_csr;
  1663. if (unlikely(!urb)) {
  1664. /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
  1665. * usbtest #11 (unlinks) triggers it regularly, sometimes
  1666. * with fifo full. (Only with DMA??)
  1667. */
  1668. musb_dbg(musb, "BOGUS RX%d ready, csr %04x, count %d",
  1669. epnum, val, musb_readw(epio, MUSB_RXCOUNT));
  1670. musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
  1671. return;
  1672. }
  1673. trace_musb_urb_rx(musb, urb);
  1674. /* check for errors, concurrent stall & unlink is not really
  1675. * handled yet! */
  1676. if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
  1677. musb_dbg(musb, "RX end %d STALL", epnum);
  1678. /* stall; record URB status */
  1679. status = -EPIPE;
  1680. } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
  1681. musb_dbg(musb, "end %d RX proto error", epnum);
  1682. #if NICHOLAS_ADD
  1683. musb_ep_select(mbase, epnum);
  1684. rx_csr |= MUSB_RXCSR_H_WZC_BITS;
  1685. rx_csr &= ~MUSB_RXCSR_H_ERROR;
  1686. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1687. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1688. rx_csr |= MUSB_RXCSR_FLUSHFIFO;
  1689. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1690. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1691. rx_csr |= MUSB_RXCSR_H_REQPKT;
  1692. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1693. musb_writeb(epio, MUSB_RXINTERVAL, qh->intv_reg);
  1694. if(qh->intv_reg == UDISK_INTERVAL){
  1695. //Is U Disk
  1696. status = -ETIMEDOUT;
  1697. }
  1698. else{
  1699. //IS Phone
  1700. goto finish;
  1701. }
  1702. #else
  1703. status = -EPROTO;
  1704. musb_writeb(epio, MUSB_RXINTERVAL, qh->intv_reg);
  1705. rx_csr &= ~MUSB_RXCSR_H_ERROR;
  1706. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1707. #endif
  1708. } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
  1709. if (USB_ENDPOINT_XFER_ISOC != qh->type) {
  1710. musb_dbg(musb, "RX end %d NAK timeout", epnum);
  1711. /* NOTE: NAKing is *NOT* an error, so we want to
  1712. * continue. Except ... if there's a request for
  1713. * another QH, use that instead of starving it.
  1714. *
  1715. * Devices like Ethernet and serial adapters keep
  1716. * reads posted at all times, which will starve
  1717. * other devices without this logic.
  1718. */
  1719. if (usb_pipebulk(urb->pipe)
  1720. && qh->mux == 1
  1721. && !list_is_singular(&musb->in_bulk)) {
  1722. musb_bulk_nak_timeout(musb, hw_ep, 1);
  1723. return;
  1724. }
  1725. musb_ep_select(mbase, epnum);
  1726. rx_csr &= ~MUSB_RXCSR_H_REQPKT;
  1727. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1728. rx_csr &= ~MUSB_RXCSR_DATAERROR;
  1729. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1730. #if NICHOLAS_ADD
  1731. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1732. rx_csr |= MUSB_RXCSR_FLUSHFIFO;
  1733. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1734. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1735. rx_csr |= MUSB_RXCSR_H_REQPKT;
  1736. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1737. goto finish;
  1738. #else
  1739. goto finish;
  1740. #endif
  1741. } else {
  1742. musb_dbg(musb, "RX end %d ISO data error", epnum);
  1743. /* packet error reported later */
  1744. iso_err = true;
  1745. }
  1746. } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
  1747. musb_dbg(musb, "end %d high bandwidth incomplete ISO packet RX",
  1748. epnum);
  1749. status = -EPROTO;
  1750. }
  1751. /* faults abort the transfer */
  1752. if (status) {
  1753. /* clean up dma and collect transfer count */
  1754. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1755. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1756. musb->dma_controller->channel_abort(dma);
  1757. xfer_len = dma->actual_len;
  1758. }
  1759. musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
  1760. musb_writeb(epio, MUSB_RXINTERVAL, qh->intv_reg);
  1761. done = true;
  1762. goto finish;
  1763. }
  1764. if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
  1765. /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
  1766. ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
  1767. goto finish;
  1768. }
  1769. /* thorough shutdown for now ... given more precise fault handling
  1770. * and better queueing support, we might keep a DMA pipeline going
  1771. * while processing this irq for earlier completions.
  1772. */
  1773. /* FIXME this is _way_ too much in-line logic for Mentor DMA */
  1774. if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
  1775. (rx_csr & MUSB_RXCSR_H_REQPKT)) {
  1776. /* REVISIT this happened for a while on some short reads...
  1777. * the cleanup still needs investigation... looks bad...
  1778. * and also duplicates dma cleanup code above ... plus,
  1779. * shouldn't this be the "half full" double buffer case?
  1780. */
  1781. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1782. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1783. musb->dma_controller->channel_abort(dma);
  1784. xfer_len = dma->actual_len;
  1785. done = true;
  1786. }
  1787. musb_dbg(musb, "RXCSR%d %04x, reqpkt, len %zu%s", epnum, rx_csr,
  1788. xfer_len, dma ? ", dma" : "");
  1789. rx_csr &= ~MUSB_RXCSR_H_REQPKT;
  1790. musb_ep_select(mbase, epnum);
  1791. musb_writew(epio, MUSB_RXCSR,
  1792. MUSB_RXCSR_H_WZC_BITS | rx_csr);
  1793. }
  1794. if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
  1795. xfer_len = dma->actual_len;
  1796. val &= ~(MUSB_RXCSR_DMAENAB
  1797. | MUSB_RXCSR_H_AUTOREQ
  1798. | MUSB_RXCSR_AUTOCLEAR
  1799. | MUSB_RXCSR_RXPKTRDY);
  1800. musb_writew(hw_ep->regs, MUSB_RXCSR, val);
  1801. if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
  1802. musb_dma_cppi41(musb)) {
  1803. done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
  1804. musb_dbg(hw_ep->musb,
  1805. "ep %d dma %s, rxcsr %04x, rxcount %d",
  1806. epnum, done ? "off" : "reset",
  1807. musb_readw(epio, MUSB_RXCSR),
  1808. musb_readw(epio, MUSB_RXCOUNT));
  1809. } else {
  1810. done = true;
  1811. }
  1812. } else if (urb->status == -EINPROGRESS) {
  1813. /* if no errors, be sure a packet is ready for unloading */
  1814. if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
  1815. status = -EPROTO;
  1816. ERR("Rx interrupt with no errors or packet!\n");
  1817. /* FIXME this is another "SHOULD NEVER HAPPEN" */
  1818. /* SCRUB (RX) */
  1819. /* do the proper sequence to abort the transfer */
  1820. musb_ep_select(mbase, epnum);
  1821. val &= ~MUSB_RXCSR_H_REQPKT;
  1822. musb_writew(epio, MUSB_RXCSR, val);
  1823. goto finish;
  1824. }
  1825. /* we are expecting IN packets */
  1826. if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
  1827. musb_dma_cppi41(musb)) && dma) {
  1828. musb_dbg(hw_ep->musb,
  1829. "RX%d count %d, buffer 0x%llx len %d/%d",
  1830. epnum, musb_readw(epio, MUSB_RXCOUNT),
  1831. (unsigned long long) urb->transfer_dma
  1832. + urb->actual_length,
  1833. qh->offset,
  1834. urb->transfer_buffer_length);
  1835. if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
  1836. xfer_len, iso_err)) {
  1837. goto finish;
  1838. }
  1839. else {
  1840. #if NICHOLAS_ADD
  1841. dma = NULL;
  1842. #else
  1843. dev_err(musb->controller, "error: rx_dma failed\n");
  1844. #endif
  1845. }
  1846. }
  1847. if (!dma) {
  1848. unsigned int received_len;
  1849. /* Unmap the buffer so that CPU can use it */
  1850. usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
  1851. /*
  1852. * We need to map sg if the transfer_buffer is
  1853. * NULL.
  1854. */
  1855. if (!urb->transfer_buffer) {
  1856. qh->use_sg = true;
  1857. sg_miter_start(&qh->sg_miter, urb->sg, 1,
  1858. sg_flags);
  1859. }
  1860. if (qh->use_sg) {
  1861. if (!sg_miter_next(&qh->sg_miter)) {
  1862. dev_err(musb->controller, "error: sg list empty\n");
  1863. sg_miter_stop(&qh->sg_miter);
  1864. status = -EINVAL;
  1865. done = true;
  1866. goto finish;
  1867. }
  1868. urb->transfer_buffer = qh->sg_miter.addr;
  1869. received_len = urb->actual_length;
  1870. qh->offset = 0x0;
  1871. done = musb_host_packet_rx(musb, urb, epnum,
  1872. iso_err);
  1873. /* Calculate the number of bytes received */
  1874. received_len = urb->actual_length -
  1875. received_len;
  1876. qh->sg_miter.consumed = received_len;
  1877. sg_miter_stop(&qh->sg_miter);
  1878. } else {
  1879. done = musb_host_packet_rx(musb, urb,
  1880. epnum, iso_err);
  1881. }
  1882. musb_dbg(musb, "read %spacket", done ? "last " : "");
  1883. }
  1884. }
  1885. finish:
  1886. urb->actual_length += xfer_len;
  1887. qh->offset += xfer_len;
  1888. if (done) {
  1889. if (qh->use_sg) {
  1890. qh->use_sg = false;
  1891. urb->transfer_buffer = NULL;
  1892. }
  1893. if (urb->status == -EINPROGRESS)
  1894. urb->status = status;
  1895. musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
  1896. }
  1897. }
  1898. /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
  1899. * the software schedule associates multiple such nodes with a given
  1900. * host side hardware endpoint + direction; scheduling may activate
  1901. * that hardware endpoint.
  1902. */
  1903. static int musb_schedule(
  1904. struct musb *musb,
  1905. struct musb_qh *qh,
  1906. int is_in)
  1907. {
  1908. int idle = 0;
  1909. int best_diff;
  1910. int best_end, epnum;
  1911. struct musb_hw_ep *hw_ep = NULL;
  1912. struct list_head *head = NULL;
  1913. u8 toggle;
  1914. u8 txtype;
  1915. struct urb *urb = next_urb(qh);
  1916. /* use fixed hardware for control and bulk */
  1917. if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
  1918. head = &musb->control;
  1919. hw_ep = musb->control_ep;
  1920. goto success;
  1921. }
  1922. /* else, periodic transfers get muxed to other endpoints */
  1923. /*
  1924. * We know this qh hasn't been scheduled, so all we need to do
  1925. * is choose which hardware endpoint to put it on ...
  1926. *
  1927. * REVISIT what we really want here is a regular schedule tree
  1928. * like e.g. OHCI uses.
  1929. */
  1930. best_diff = 4096 * 2;
  1931. best_end = -1;
  1932. for (epnum = 1, hw_ep = musb->endpoints + 1;
  1933. (epnum < musb->nr_endpoints) && (epnum < MUSB_C_NUM_EPS);
  1934. epnum++, hw_ep++) {
  1935. int diff;
  1936. if (musb_ep_get_qh(hw_ep, is_in) != NULL)
  1937. continue;
  1938. if (hw_ep == musb->bulk_ep)
  1939. continue;
  1940. if (is_in)
  1941. diff = hw_ep->max_packet_sz_rx;
  1942. else
  1943. diff = hw_ep->max_packet_sz_tx;
  1944. diff -= (qh->maxpacket * qh->hb_mult);
  1945. if (diff >= 0 && best_diff > diff) {
  1946. /*
  1947. * Mentor controller has a bug in that if we schedule
  1948. * a BULK Tx transfer on an endpoint that had earlier
  1949. * handled ISOC then the BULK transfer has to start on
  1950. * a zero toggle. If the BULK transfer starts on a 1
  1951. * toggle then this transfer will fail as the mentor
  1952. * controller starts the Bulk transfer on a 0 toggle
  1953. * irrespective of the programming of the toggle bits
  1954. * in the TXCSR register. Check for this condition
  1955. * while allocating the EP for a Tx Bulk transfer. If
  1956. * so skip this EP.
  1957. */
  1958. hw_ep = musb->endpoints + epnum;
  1959. toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
  1960. txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
  1961. >> 4) & 0x3;
  1962. if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
  1963. toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
  1964. continue;
  1965. best_diff = diff;
  1966. best_end = epnum;
  1967. }
  1968. }
  1969. /* use bulk reserved ep1 if no other ep is free */
  1970. if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
  1971. hw_ep = musb->bulk_ep;
  1972. if (is_in)
  1973. head = &musb->in_bulk;
  1974. else
  1975. head = &musb->out_bulk;
  1976. /* Enable bulk RX/TX NAK timeout scheme when bulk requests are
  1977. * multiplexed. This scheme does not work in high speed to full
  1978. * speed scenario as NAK interrupts are not coming from a
  1979. * full speed device connected to a high speed device.
  1980. * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
  1981. * 4 (8 frame or 8ms) for FS device.
  1982. */
  1983. #if NICHOLAS_ADD
  1984. if (is_in && qh->dev)
  1985. #else
  1986. if (qh->dev)
  1987. #endif
  1988. qh->intv_reg =
  1989. (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
  1990. goto success;
  1991. } else if (best_end < 0) {
  1992. dev_err(musb->controller,
  1993. "%s hwep alloc failed for %dx%d\n",
  1994. musb_ep_xfertype_string(qh->type),
  1995. qh->hb_mult, qh->maxpacket);
  1996. return -ENOSPC;
  1997. }
  1998. idle = 1;
  1999. qh->mux = 0;
  2000. hw_ep = musb->endpoints + best_end;
  2001. musb_dbg(musb, "qh %p periodic slot %d", qh, best_end);
  2002. success:
  2003. if (head) {
  2004. idle = list_empty(head);
  2005. list_add_tail(&qh->ring, head);
  2006. qh->mux = 1;
  2007. }
  2008. qh->hw_ep = hw_ep;
  2009. qh->hep->hcpriv = qh;
  2010. if (idle)
  2011. musb_start_urb(musb, is_in, qh);
  2012. return 0;
  2013. }
  2014. static int musb_urb_enqueue(
  2015. struct usb_hcd *hcd,
  2016. struct urb *urb,
  2017. gfp_t mem_flags)
  2018. {
  2019. unsigned long flags;
  2020. struct musb *musb = hcd_to_musb(hcd);
  2021. struct usb_host_endpoint *hep = urb->ep;
  2022. struct musb_qh *qh;
  2023. struct usb_endpoint_descriptor *epd = &hep->desc;
  2024. int ret;
  2025. unsigned type_reg;
  2026. #if NICHOLAS_ADD
  2027. u8 interval = 0;
  2028. #else
  2029. unsigned interval;
  2030. #endif
  2031. /* host role must be active */
  2032. if (!is_host_active(musb) || !musb->is_active)
  2033. return -ENODEV;
  2034. trace_musb_urb_enq(musb, urb);
  2035. spin_lock_irqsave(&musb->lock, flags);
  2036. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  2037. qh = ret ? NULL : hep->hcpriv;
  2038. if (qh)
  2039. urb->hcpriv = qh;
  2040. spin_unlock_irqrestore(&musb->lock, flags);
  2041. /* DMA mapping was already done, if needed, and this urb is on
  2042. * hep->urb_list now ... so we're done, unless hep wasn't yet
  2043. * scheduled onto a live qh.
  2044. *
  2045. * REVISIT best to keep hep->hcpriv valid until the endpoint gets
  2046. * disabled, testing for empty qh->ring and avoiding qh setup costs
  2047. * except for the first urb queued after a config change.
  2048. */
  2049. if (qh || ret)
  2050. return ret;
  2051. /* Allocate and initialize qh, minimizing the work done each time
  2052. * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
  2053. *
  2054. * REVISIT consider a dedicated qh kmem_cache, so it's harder
  2055. * for bugs in other kernel code to break this driver...
  2056. */
  2057. qh = kzalloc(sizeof *qh, mem_flags);
  2058. if (!qh) {
  2059. spin_lock_irqsave(&musb->lock, flags);
  2060. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2061. spin_unlock_irqrestore(&musb->lock, flags);
  2062. return -ENOMEM;
  2063. }
  2064. qh->hep = hep;
  2065. qh->dev = urb->dev;
  2066. INIT_LIST_HEAD(&qh->ring);
  2067. qh->is_ready = 1;
  2068. qh->maxpacket = usb_endpoint_maxp(epd);
  2069. qh->type = usb_endpoint_type(epd);
  2070. /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
  2071. * Some musb cores don't support high bandwidth ISO transfers; and
  2072. * we don't (yet!) support high bandwidth interrupt transfers.
  2073. */
  2074. qh->hb_mult = usb_endpoint_maxp_mult(epd);
  2075. if (qh->hb_mult > 1) {
  2076. int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
  2077. if (ok)
  2078. ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
  2079. || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
  2080. if (!ok) {
  2081. dev_err(musb->controller,
  2082. "high bandwidth %s (%dx%d) not supported\n",
  2083. musb_ep_xfertype_string(qh->type),
  2084. qh->hb_mult, qh->maxpacket & 0x7ff);
  2085. ret = -EMSGSIZE;
  2086. goto done;
  2087. }
  2088. qh->maxpacket &= 0x7ff;
  2089. }
  2090. qh->epnum = usb_endpoint_num(epd);
  2091. /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
  2092. qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
  2093. /* precompute rxtype/txtype/type0 register */
  2094. type_reg = (qh->type << 4) | qh->epnum;
  2095. switch (urb->dev->speed) {
  2096. case USB_SPEED_LOW:
  2097. type_reg |= 0xc0;
  2098. break;
  2099. case USB_SPEED_FULL:
  2100. type_reg |= 0x80;
  2101. break;
  2102. default:
  2103. type_reg |= 0x40;
  2104. }
  2105. qh->type_reg = type_reg;
  2106. /* Precompute RXINTERVAL/TXINTERVAL register */
  2107. switch (qh->type) {
  2108. case USB_ENDPOINT_XFER_INT:
  2109. /*
  2110. * Full/low speeds use the linear encoding,
  2111. * high speed uses the logarithmic encoding.
  2112. */
  2113. if (urb->dev->speed <= USB_SPEED_FULL) {
  2114. interval = max_t(u8, epd->bInterval, 1);
  2115. break;
  2116. }
  2117. /* FALLTHROUGH */
  2118. case USB_ENDPOINT_XFER_ISOC:
  2119. /* ISO always uses logarithmic encoding */
  2120. interval = min_t(u8, epd->bInterval, 16);
  2121. break;
  2122. default:
  2123. /* REVISIT we actually want to use NAK limits, hinting to the
  2124. * transfer scheduling logic to try some other qh, e.g. try
  2125. * for 2 msec first:
  2126. *
  2127. * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
  2128. *
  2129. * The downside of disabling this is that transfer scheduling
  2130. * gets VERY unfair for nonperiodic transfers; a misbehaving
  2131. * peripheral could make that hurt. That's perfectly normal
  2132. * for reads from network or serial adapters ... so we have
  2133. * partial NAKlimit support for bulk RX.
  2134. *
  2135. * The upside of disabling it is simpler transfer scheduling.
  2136. */
  2137. #if NICHOLAS_ADD
  2138. if(urb->dev->actconfig)
  2139. {
  2140. struct usb_interface *intf = urb->dev->actconfig->interface[0];
  2141. if(intf)
  2142. {
  2143. struct usb_host_interface *cur_altsetting = intf->cur_altsetting;
  2144. if(cur_altsetting)
  2145. {
  2146. struct usb_interface_descriptor *desc = &cur_altsetting->desc;;
  2147. if(desc)
  2148. {
  2149. if(desc->bInterfaceClass == 0x8) //Is U Disk
  2150. interval = UDISK_INTERVAL;
  2151. else
  2152. interval = 0;
  2153. }
  2154. }
  2155. }
  2156. }
  2157. #else
  2158. interval = 0;
  2159. #endif
  2160. }
  2161. qh->intv_reg = interval;
  2162. /* precompute addressing for external hub/tt ports */
  2163. if (musb->is_multipoint) {
  2164. struct usb_device *parent = urb->dev->parent;
  2165. if (parent != hcd->self.root_hub) {
  2166. qh->h_addr_reg = (u8) parent->devnum;
  2167. /* set up tt info if needed */
  2168. if (urb->dev->tt) {
  2169. qh->h_port_reg = (u8) urb->dev->ttport;
  2170. if (urb->dev->tt->hub)
  2171. qh->h_addr_reg =
  2172. (u8) urb->dev->tt->hub->devnum;
  2173. if (urb->dev->tt->multi)
  2174. qh->h_addr_reg |= 0x80;
  2175. }
  2176. }
  2177. }
  2178. /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
  2179. * until we get real dma queues (with an entry for each urb/buffer),
  2180. * we only have work to do in the former case.
  2181. */
  2182. spin_lock_irqsave(&musb->lock, flags);
  2183. if (hep->hcpriv || !next_urb(qh)) {
  2184. /* some concurrent activity submitted another urb to hep...
  2185. * odd, rare, error prone, but legal.
  2186. */
  2187. kfree(qh);
  2188. qh = NULL;
  2189. ret = 0;
  2190. } else
  2191. ret = musb_schedule(musb, qh,
  2192. epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
  2193. if (ret == 0) {
  2194. urb->hcpriv = qh;
  2195. /* FIXME set urb->start_frame for iso/intr, it's tested in
  2196. * musb_start_urb(), but otherwise only konicawc cares ...
  2197. */
  2198. }
  2199. spin_unlock_irqrestore(&musb->lock, flags);
  2200. done:
  2201. if (ret != 0) {
  2202. spin_lock_irqsave(&musb->lock, flags);
  2203. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2204. spin_unlock_irqrestore(&musb->lock, flags);
  2205. kfree(qh);
  2206. }
  2207. return ret;
  2208. }
  2209. /*
  2210. * abort a transfer that's at the head of a hardware queue.
  2211. * called with controller locked, irqs blocked
  2212. * that hardware queue advances to the next transfer, unless prevented
  2213. */
  2214. static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
  2215. {
  2216. struct musb_hw_ep *ep = qh->hw_ep;
  2217. struct musb *musb = ep->musb;
  2218. void __iomem *epio = ep->regs;
  2219. unsigned hw_end = ep->epnum;
  2220. void __iomem *regs = ep->musb->mregs;
  2221. int is_in = usb_pipein(urb->pipe);
  2222. int status = 0;
  2223. u16 csr;
  2224. struct dma_channel *dma = NULL;
  2225. musb_ep_select(regs, hw_end);
  2226. if (is_dma_capable()) {
  2227. dma = is_in ? ep->rx_channel : ep->tx_channel;
  2228. if (dma) {
  2229. status = ep->musb->dma_controller->channel_abort(dma);
  2230. musb_dbg(musb, "abort %cX%d DMA for urb %p --> %d",
  2231. is_in ? 'R' : 'T', ep->epnum,
  2232. urb, status);
  2233. urb->actual_length += dma->actual_len;
  2234. }
  2235. }
  2236. /* turn off DMA requests, discard state, stop polling ... */
  2237. if (ep->epnum && is_in) {
  2238. /* giveback saves bulk toggle */
  2239. csr = musb_h_flush_rxfifo(ep, 0);
  2240. /* clear the endpoint's irq status here to avoid bogus irqs */
  2241. if (is_dma_capable() && dma)
  2242. musb_platform_clear_ep_rxintr(musb, ep->epnum);
  2243. } else if (ep->epnum) {
  2244. musb_h_tx_flush_fifo(ep);
  2245. csr = musb_readw(epio, MUSB_TXCSR);
  2246. csr &= ~(MUSB_TXCSR_AUTOSET
  2247. | MUSB_TXCSR_DMAENAB
  2248. | MUSB_TXCSR_H_RXSTALL
  2249. | MUSB_TXCSR_H_NAKTIMEOUT
  2250. | MUSB_TXCSR_H_ERROR
  2251. | MUSB_TXCSR_TXPKTRDY);
  2252. musb_writew(epio, MUSB_TXCSR, csr);
  2253. /* REVISIT may need to clear FLUSHFIFO ... */
  2254. musb_writew(epio, MUSB_TXCSR, csr);
  2255. /* flush cpu writebuffer */
  2256. csr = musb_readw(epio, MUSB_TXCSR);
  2257. } else {
  2258. musb_h_ep0_flush_fifo(ep);
  2259. }
  2260. if (status == 0)
  2261. musb_advance_schedule(ep->musb, urb, ep, is_in);
  2262. return status;
  2263. }
  2264. static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  2265. {
  2266. struct musb *musb = hcd_to_musb(hcd);
  2267. struct musb_qh *qh;
  2268. unsigned long flags;
  2269. int is_in = usb_pipein(urb->pipe);
  2270. int ret;
  2271. trace_musb_urb_deq(musb, urb);
  2272. spin_lock_irqsave(&musb->lock, flags);
  2273. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  2274. if (ret)
  2275. goto done;
  2276. qh = urb->hcpriv;
  2277. if (!qh)
  2278. goto done;
  2279. /*
  2280. * Any URB not actively programmed into endpoint hardware can be
  2281. * immediately given back; that's any URB not at the head of an
  2282. * endpoint queue, unless someday we get real DMA queues. And even
  2283. * if it's at the head, it might not be known to the hardware...
  2284. *
  2285. * Otherwise abort current transfer, pending DMA, etc.; urb->status
  2286. * has already been updated. This is a synchronous abort; it'd be
  2287. * OK to hold off until after some IRQ, though.
  2288. *
  2289. * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
  2290. */
  2291. if (!qh->is_ready
  2292. || urb->urb_list.prev != &qh->hep->urb_list
  2293. || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
  2294. int ready = qh->is_ready;
  2295. qh->is_ready = 0;
  2296. musb_giveback(musb, urb, 0);
  2297. qh->is_ready = ready;
  2298. /* If nothing else (usually musb_giveback) is using it
  2299. * and its URB list has emptied, recycle this qh.
  2300. */
  2301. if (ready && list_empty(&qh->hep->urb_list)) {
  2302. qh->hep->hcpriv = NULL;
  2303. list_del(&qh->ring);
  2304. kfree(qh);
  2305. }
  2306. } else
  2307. ret = musb_cleanup_urb(urb, qh);
  2308. done:
  2309. spin_unlock_irqrestore(&musb->lock, flags);
  2310. return ret;
  2311. }
  2312. /* disable an endpoint */
  2313. static void
  2314. musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
  2315. {
  2316. u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
  2317. unsigned long flags;
  2318. struct musb *musb = hcd_to_musb(hcd);
  2319. struct musb_qh *qh;
  2320. struct urb *urb;
  2321. spin_lock_irqsave(&musb->lock, flags);
  2322. qh = hep->hcpriv;
  2323. if (qh == NULL)
  2324. goto exit;
  2325. /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
  2326. /* Kick the first URB off the hardware, if needed */
  2327. qh->is_ready = 0;
  2328. if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
  2329. urb = next_urb(qh);
  2330. /* make software (then hardware) stop ASAP */
  2331. if (!urb->unlinked)
  2332. urb->status = -ESHUTDOWN;
  2333. /* cleanup */
  2334. musb_cleanup_urb(urb, qh);
  2335. /* Then nuke all the others ... and advance the
  2336. * queue on hw_ep (e.g. bulk ring) when we're done.
  2337. */
  2338. while (!list_empty(&hep->urb_list)) {
  2339. urb = next_urb(qh);
  2340. urb->status = -ESHUTDOWN;
  2341. musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
  2342. }
  2343. } else {
  2344. /* Just empty the queue; the hardware is busy with
  2345. * other transfers, and since !qh->is_ready nothing
  2346. * will activate any of these as it advances.
  2347. */
  2348. while (!list_empty(&hep->urb_list))
  2349. musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
  2350. hep->hcpriv = NULL;
  2351. list_del(&qh->ring);
  2352. kfree(qh);
  2353. }
  2354. exit:
  2355. spin_unlock_irqrestore(&musb->lock, flags);
  2356. }
  2357. static int musb_h_get_frame_number(struct usb_hcd *hcd)
  2358. {
  2359. struct musb *musb = hcd_to_musb(hcd);
  2360. return musb_readw(musb->mregs, MUSB_FRAME);
  2361. }
  2362. static int musb_h_start(struct usb_hcd *hcd)
  2363. {
  2364. struct musb *musb = hcd_to_musb(hcd);
  2365. /* NOTE: musb_start() is called when the hub driver turns
  2366. * on port power, or when (OTG) peripheral starts.
  2367. */
  2368. hcd->state = HC_STATE_RUNNING;
  2369. musb->port1_status = 0;
  2370. return 0;
  2371. }
  2372. static void musb_h_stop(struct usb_hcd *hcd)
  2373. {
  2374. musb_stop(hcd_to_musb(hcd));
  2375. hcd->state = HC_STATE_HALT;
  2376. }
  2377. static int musb_bus_suspend(struct usb_hcd *hcd)
  2378. {
  2379. struct musb *musb = hcd_to_musb(hcd);
  2380. u8 devctl;
  2381. int ret;
  2382. ret = musb_port_suspend(musb, true);
  2383. if (ret)
  2384. return ret;
  2385. if (!is_host_active(musb))
  2386. return 0;
  2387. switch (musb->xceiv->otg->state) {
  2388. case OTG_STATE_A_SUSPEND:
  2389. return 0;
  2390. case OTG_STATE_A_WAIT_VRISE:
  2391. /* ID could be grounded even if there's no device
  2392. * on the other end of the cable. NOTE that the
  2393. * A_WAIT_VRISE timers are messy with MUSB...
  2394. */
  2395. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  2396. if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
  2397. musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
  2398. break;
  2399. default:
  2400. break;
  2401. }
  2402. if (musb->is_active) {
  2403. WARNING("trying to suspend as %s while active\n",
  2404. usb_otg_state_string(musb->xceiv->otg->state));
  2405. return -EBUSY;
  2406. } else
  2407. return 0;
  2408. }
  2409. static int musb_bus_resume(struct usb_hcd *hcd)
  2410. {
  2411. struct musb *musb = hcd_to_musb(hcd);
  2412. if (musb->config &&
  2413. musb->config->host_port_deassert_reset_at_resume)
  2414. musb_port_reset(musb, false);
  2415. return 0;
  2416. }
  2417. #ifndef CONFIG_MUSB_PIO_ONLY
  2418. #define MUSB_USB_DMA_ALIGN 4
  2419. struct musb_temp_buffer {
  2420. void *kmalloc_ptr;
  2421. void *old_xfer_buffer;
  2422. u8 data[0];
  2423. };
  2424. static void musb_free_temp_buffer(struct urb *urb)
  2425. {
  2426. enum dma_data_direction dir;
  2427. struct musb_temp_buffer *temp;
  2428. size_t length;
  2429. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  2430. return;
  2431. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2432. temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
  2433. data);
  2434. if (dir == DMA_FROM_DEVICE) {
  2435. if (usb_pipeisoc(urb->pipe))
  2436. length = urb->transfer_buffer_length;
  2437. else
  2438. length = urb->actual_length;
  2439. memcpy(temp->old_xfer_buffer, temp->data, length);
  2440. }
  2441. urb->transfer_buffer = temp->old_xfer_buffer;
  2442. kfree(temp->kmalloc_ptr);
  2443. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  2444. }
  2445. static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
  2446. {
  2447. enum dma_data_direction dir;
  2448. struct musb_temp_buffer *temp;
  2449. void *kmalloc_ptr;
  2450. size_t kmalloc_size;
  2451. if (urb->num_sgs || urb->sg ||
  2452. urb->transfer_buffer_length == 0 ||
  2453. !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
  2454. return 0;
  2455. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2456. /* Allocate a buffer with enough padding for alignment */
  2457. kmalloc_size = urb->transfer_buffer_length +
  2458. sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
  2459. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  2460. if (!kmalloc_ptr)
  2461. return -ENOMEM;
  2462. /* Position our struct temp_buffer such that data is aligned */
  2463. temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
  2464. temp->kmalloc_ptr = kmalloc_ptr;
  2465. temp->old_xfer_buffer = urb->transfer_buffer;
  2466. if (dir == DMA_TO_DEVICE)
  2467. memcpy(temp->data, urb->transfer_buffer,
  2468. urb->transfer_buffer_length);
  2469. urb->transfer_buffer = temp->data;
  2470. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  2471. return 0;
  2472. }
  2473. static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  2474. gfp_t mem_flags)
  2475. {
  2476. struct musb *musb = hcd_to_musb(hcd);
  2477. int ret;
  2478. /*
  2479. * The DMA engine in RTL1.8 and above cannot handle
  2480. * DMA addresses that are not aligned to a 4 byte boundary.
  2481. * For such engine implemented (un)map_urb_for_dma hooks.
  2482. * Do not use these hooks for RTL<1.8
  2483. */
  2484. if (musb->hwvers < MUSB_HWVERS_1800)
  2485. return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  2486. ret = musb_alloc_temp_buffer(urb, mem_flags);
  2487. if (ret)
  2488. return ret;
  2489. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  2490. if (ret)
  2491. musb_free_temp_buffer(urb);
  2492. return ret;
  2493. }
  2494. static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  2495. {
  2496. struct musb *musb = hcd_to_musb(hcd);
  2497. usb_hcd_unmap_urb_for_dma(hcd, urb);
  2498. /* Do not use this hook for RTL<1.8 (see description above) */
  2499. if (musb->hwvers < MUSB_HWVERS_1800)
  2500. return;
  2501. musb_free_temp_buffer(urb);
  2502. }
  2503. #endif /* !CONFIG_MUSB_PIO_ONLY */
  2504. static const struct hc_driver musb_hc_driver = {
  2505. .description = "musb-hcd",
  2506. .product_desc = "MUSB HDRC host driver",
  2507. .hcd_priv_size = sizeof(struct musb *),
  2508. .flags = HCD_USB2 | HCD_MEMORY,
  2509. /* not using irq handler or reset hooks from usbcore, since
  2510. * those must be shared with peripheral code for OTG configs
  2511. */
  2512. .start = musb_h_start,
  2513. .stop = musb_h_stop,
  2514. .get_frame_number = musb_h_get_frame_number,
  2515. .urb_enqueue = musb_urb_enqueue,
  2516. .urb_dequeue = musb_urb_dequeue,
  2517. .endpoint_disable = musb_h_disable,
  2518. #ifndef CONFIG_MUSB_PIO_ONLY
  2519. .map_urb_for_dma = musb_map_urb_for_dma,
  2520. .unmap_urb_for_dma = musb_unmap_urb_for_dma,
  2521. #endif
  2522. .hub_status_data = musb_hub_status_data,
  2523. .hub_control = musb_hub_control,
  2524. .bus_suspend = musb_bus_suspend,
  2525. .bus_resume = musb_bus_resume,
  2526. /* .start_port_reset = NULL, */
  2527. /* .hub_irq_enable = NULL, */
  2528. };
  2529. int musb_host_alloc(struct musb *musb)
  2530. {
  2531. struct device *dev = musb->controller;
  2532. /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
  2533. musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
  2534. if (!musb->hcd)
  2535. return -EINVAL;
  2536. *musb->hcd->hcd_priv = (unsigned long) musb;
  2537. musb->hcd->self.uses_pio_for_control = 1;
  2538. musb->hcd->uses_new_polling = 1;
  2539. musb->hcd->has_tt = 1;
  2540. return 0;
  2541. }
  2542. void musb_host_cleanup(struct musb *musb)
  2543. {
  2544. if (musb->port_mode == MUSB_PERIPHERAL)
  2545. return;
  2546. usb_remove_hcd(musb->hcd);
  2547. }
  2548. void musb_host_free(struct musb *musb)
  2549. {
  2550. usb_put_hcd(musb->hcd);
  2551. }
  2552. int musb_host_setup(struct musb *musb, int power_budget)
  2553. {
  2554. int ret;
  2555. struct usb_hcd *hcd = musb->hcd;
  2556. if (musb->port_mode == MUSB_HOST) {
  2557. MUSB_HST_MODE(musb);
  2558. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  2559. }
  2560. otg_set_host(musb->xceiv->otg, &hcd->self);
  2561. /* don't support otg protocols */
  2562. hcd->self.otg_port = 0;
  2563. musb->xceiv->otg->host = &hcd->self;
  2564. hcd->power_budget = 2 * (power_budget ? : 250);
  2565. hcd->skip_phy_initialization = 1;
  2566. ret = usb_add_hcd(hcd, 0, 0);
  2567. if (ret < 0)
  2568. return ret;
  2569. device_wakeup_enable(hcd->self.controller);
  2570. return 0;
  2571. }
  2572. void musb_host_resume_root_hub(struct musb *musb)
  2573. {
  2574. usb_hcd_resume_root_hub(musb->hcd);
  2575. }
  2576. void musb_host_poke_root_hub(struct musb *musb)
  2577. {
  2578. MUSB_HST_MODE(musb);
  2579. if (musb->hcd->status_urb)
  2580. usb_hcd_poll_rh_status(musb->hcd);
  2581. else
  2582. usb_hcd_resume_root_hub(musb->hcd);
  2583. }