musb_host.c 78 KB

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