musb_host.c 74 KB

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