musb_host.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258
  1. /*
  2. * MUSB OTG driver host support
  3. *
  4. * Copyright 2005 Mentor Graphics Corporation
  5. * Copyright (C) 2005-2006 by Texas Instruments
  6. * Copyright (C) 2006-2007 Nokia Corporation
  7. * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  26. * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  28. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  29. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  32. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. #include <linux/module.h>
  36. #include <linux/kernel.h>
  37. #include <linux/delay.h>
  38. #include <linux/sched.h>
  39. #include <linux/slab.h>
  40. #include <linux/errno.h>
  41. #include <linux/init.h>
  42. #include <linux/list.h>
  43. #include "musb_core.h"
  44. #include "musb_host.h"
  45. /* MUSB HOST status 22-mar-2006
  46. *
  47. * - There's still lots of partial code duplication for fault paths, so
  48. * they aren't handled as consistently as they need to be.
  49. *
  50. * - PIO mostly behaved when last tested.
  51. * + including ep0, with all usbtest cases 9, 10
  52. * + usbtest 14 (ep0out) doesn't seem to run at all
  53. * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
  54. * configurations, but otherwise double buffering passes basic tests.
  55. * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
  56. *
  57. * - DMA (CPPI) ... partially behaves, not currently recommended
  58. * + about 1/15 the speed of typical EHCI implementations (PCI)
  59. * + RX, all too often reqpkt seems to misbehave after tx
  60. * + TX, no known issues (other than evident silicon issue)
  61. *
  62. * - DMA (Mentor/OMAP) ...has at least toggle update problems
  63. *
  64. * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
  65. * starvation ... nothing yet for TX, interrupt, or bulk.
  66. *
  67. * - Not tested with HNP, but some SRP paths seem to behave.
  68. *
  69. * NOTE 24-August-2006:
  70. *
  71. * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
  72. * extra endpoint for periodic use enabling hub + keybd + mouse. That
  73. * mostly works, except that with "usbnet" it's easy to trigger cases
  74. * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
  75. * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
  76. * although ARP RX wins. (That test was done with a full speed link.)
  77. */
  78. /*
  79. * NOTE on endpoint usage:
  80. *
  81. * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
  82. * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
  83. * (Yes, bulk _could_ use more of the endpoints than that, and would even
  84. * benefit from it.)
  85. *
  86. * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
  87. * So far that scheduling is both dumb and optimistic: the endpoint will be
  88. * "claimed" until its software queue is no longer refilled. No multiplexing
  89. * of transfers between endpoints, or anything clever.
  90. */
  91. static void musb_ep_program(struct musb *musb, u8 epnum,
  92. struct urb *urb, int is_out,
  93. u8 *buf, u32 offset, u32 len);
  94. /*
  95. * Clear TX fifo. Needed to avoid BABBLE errors.
  96. */
  97. static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
  98. {
  99. void __iomem *epio = ep->regs;
  100. u16 csr;
  101. u16 lastcsr = 0;
  102. int retries = 1000;
  103. csr = musb_readw(epio, MUSB_TXCSR);
  104. while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  105. if (csr != lastcsr)
  106. DBG(3, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
  107. lastcsr = csr;
  108. csr |= MUSB_TXCSR_FLUSHFIFO;
  109. musb_writew(epio, MUSB_TXCSR, csr);
  110. csr = musb_readw(epio, MUSB_TXCSR);
  111. if (WARN(retries-- < 1,
  112. "Could not flush host TX%d fifo: csr: %04x\n",
  113. ep->epnum, csr))
  114. return;
  115. mdelay(1);
  116. }
  117. }
  118. static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
  119. {
  120. void __iomem *epio = ep->regs;
  121. u16 csr;
  122. int retries = 5;
  123. /* scrub any data left in the fifo */
  124. do {
  125. csr = musb_readw(epio, MUSB_TXCSR);
  126. if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
  127. break;
  128. musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
  129. csr = musb_readw(epio, MUSB_TXCSR);
  130. udelay(10);
  131. } while (--retries);
  132. WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
  133. ep->epnum, csr);
  134. /* and reset for the next transfer */
  135. musb_writew(epio, MUSB_TXCSR, 0);
  136. }
  137. /*
  138. * Start transmit. Caller is responsible for locking shared resources.
  139. * musb must be locked.
  140. */
  141. static inline void musb_h_tx_start(struct musb_hw_ep *ep)
  142. {
  143. u16 txcsr;
  144. /* NOTE: no locks here; caller should lock and select EP */
  145. if (ep->epnum) {
  146. txcsr = musb_readw(ep->regs, MUSB_TXCSR);
  147. txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
  148. musb_writew(ep->regs, MUSB_TXCSR, txcsr);
  149. } else {
  150. txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
  151. musb_writew(ep->regs, MUSB_CSR0, txcsr);
  152. }
  153. }
  154. static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
  155. {
  156. u16 txcsr;
  157. /* NOTE: no locks here; caller should lock and select EP */
  158. txcsr = musb_readw(ep->regs, MUSB_TXCSR);
  159. txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
  160. if (is_cppi_enabled())
  161. txcsr |= MUSB_TXCSR_DMAMODE;
  162. musb_writew(ep->regs, MUSB_TXCSR, txcsr);
  163. }
  164. static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
  165. {
  166. if (is_in != 0 || ep->is_shared_fifo)
  167. ep->in_qh = qh;
  168. if (is_in == 0 || ep->is_shared_fifo)
  169. ep->out_qh = qh;
  170. }
  171. static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
  172. {
  173. return is_in ? ep->in_qh : ep->out_qh;
  174. }
  175. /*
  176. * Start the URB at the front of an endpoint's queue
  177. * end must be claimed from the caller.
  178. *
  179. * Context: controller locked, irqs blocked
  180. */
  181. static void
  182. musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
  183. {
  184. u16 frame;
  185. u32 len;
  186. void __iomem *mbase = musb->mregs;
  187. struct urb *urb = next_urb(qh);
  188. void *buf = urb->transfer_buffer;
  189. u32 offset = 0;
  190. struct musb_hw_ep *hw_ep = qh->hw_ep;
  191. unsigned pipe = urb->pipe;
  192. u8 address = usb_pipedevice(pipe);
  193. int epnum = hw_ep->epnum;
  194. /* initialize software qh state */
  195. qh->offset = 0;
  196. qh->segsize = 0;
  197. /* gather right source of data */
  198. switch (qh->type) {
  199. case USB_ENDPOINT_XFER_CONTROL:
  200. /* control transfers always start with SETUP */
  201. is_in = 0;
  202. musb->ep0_stage = MUSB_EP0_START;
  203. buf = urb->setup_packet;
  204. len = 8;
  205. break;
  206. case USB_ENDPOINT_XFER_ISOC:
  207. qh->iso_idx = 0;
  208. qh->frame = 0;
  209. offset = urb->iso_frame_desc[0].offset;
  210. len = urb->iso_frame_desc[0].length;
  211. break;
  212. default: /* bulk, interrupt */
  213. /* actual_length may be nonzero on retry paths */
  214. buf = urb->transfer_buffer + urb->actual_length;
  215. len = urb->transfer_buffer_length - urb->actual_length;
  216. }
  217. DBG(4, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
  218. qh, urb, address, qh->epnum,
  219. is_in ? "in" : "out",
  220. ({char *s; switch (qh->type) {
  221. case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
  222. case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
  223. case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
  224. default: s = "-intr"; break;
  225. }; s; }),
  226. epnum, buf + offset, len);
  227. /* Configure endpoint */
  228. musb_ep_set_qh(hw_ep, is_in, qh);
  229. musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
  230. /* transmit may have more work: start it when it is time */
  231. if (is_in)
  232. return;
  233. /* determine if the time is right for a periodic transfer */
  234. switch (qh->type) {
  235. case USB_ENDPOINT_XFER_ISOC:
  236. case USB_ENDPOINT_XFER_INT:
  237. DBG(3, "check whether there's still time for periodic Tx\n");
  238. frame = musb_readw(mbase, MUSB_FRAME);
  239. /* FIXME this doesn't implement that scheduling policy ...
  240. * or handle framecounter wrapping
  241. */
  242. if ((urb->transfer_flags & URB_ISO_ASAP)
  243. || (frame >= urb->start_frame)) {
  244. /* REVISIT the SOF irq handler shouldn't duplicate
  245. * this code; and we don't init urb->start_frame...
  246. */
  247. qh->frame = 0;
  248. goto start;
  249. } else {
  250. qh->frame = urb->start_frame;
  251. /* enable SOF interrupt so we can count down */
  252. DBG(1, "SOF for %d\n", epnum);
  253. #if 1 /* ifndef CONFIG_ARCH_DAVINCI */
  254. musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
  255. #endif
  256. }
  257. break;
  258. default:
  259. start:
  260. DBG(4, "Start TX%d %s\n", epnum,
  261. hw_ep->tx_channel ? "dma" : "pio");
  262. if (!hw_ep->tx_channel)
  263. musb_h_tx_start(hw_ep);
  264. else if (is_cppi_enabled() || tusb_dma_omap())
  265. musb_h_tx_dma_start(hw_ep);
  266. }
  267. }
  268. /* Context: caller owns controller lock, IRQs are blocked */
  269. static void musb_giveback(struct musb *musb, struct urb *urb, int status)
  270. __releases(musb->lock)
  271. __acquires(musb->lock)
  272. {
  273. DBG(({ int level; switch (status) {
  274. case 0:
  275. level = 4;
  276. break;
  277. /* common/boring faults */
  278. case -EREMOTEIO:
  279. case -ESHUTDOWN:
  280. case -ECONNRESET:
  281. case -EPIPE:
  282. level = 3;
  283. break;
  284. default:
  285. level = 2;
  286. break;
  287. }; level; }),
  288. "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
  289. urb, urb->complete, status,
  290. usb_pipedevice(urb->pipe),
  291. usb_pipeendpoint(urb->pipe),
  292. usb_pipein(urb->pipe) ? "in" : "out",
  293. urb->actual_length, urb->transfer_buffer_length
  294. );
  295. usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb);
  296. spin_unlock(&musb->lock);
  297. usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status);
  298. spin_lock(&musb->lock);
  299. }
  300. /* For bulk/interrupt endpoints only */
  301. static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
  302. struct urb *urb)
  303. {
  304. void __iomem *epio = qh->hw_ep->regs;
  305. u16 csr;
  306. /*
  307. * FIXME: the current Mentor DMA code seems to have
  308. * problems getting toggle correct.
  309. */
  310. if (is_in)
  311. csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
  312. else
  313. csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
  314. usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
  315. }
  316. /*
  317. * Advance this hardware endpoint's queue, completing the specified URB and
  318. * advancing to either the next URB queued to that qh, or else invalidating
  319. * that qh and advancing to the next qh scheduled after the current one.
  320. *
  321. * Context: caller owns controller lock, IRQs are blocked
  322. */
  323. static void musb_advance_schedule(struct musb *musb, struct urb *urb,
  324. struct musb_hw_ep *hw_ep, int is_in)
  325. {
  326. struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
  327. struct musb_hw_ep *ep = qh->hw_ep;
  328. int ready = qh->is_ready;
  329. int status;
  330. status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
  331. /* save toggle eagerly, for paranoia */
  332. switch (qh->type) {
  333. case USB_ENDPOINT_XFER_BULK:
  334. case USB_ENDPOINT_XFER_INT:
  335. musb_save_toggle(qh, is_in, urb);
  336. break;
  337. case USB_ENDPOINT_XFER_ISOC:
  338. if (urb->error_count)
  339. status = -EXDEV;
  340. break;
  341. }
  342. qh->is_ready = 0;
  343. musb_giveback(musb, urb, status);
  344. qh->is_ready = ready;
  345. /* reclaim resources (and bandwidth) ASAP; deschedule it, and
  346. * invalidate qh as soon as list_empty(&hep->urb_list)
  347. */
  348. if (list_empty(&qh->hep->urb_list)) {
  349. struct list_head *head;
  350. if (is_in)
  351. ep->rx_reinit = 1;
  352. else
  353. ep->tx_reinit = 1;
  354. /* Clobber old pointers to this qh */
  355. musb_ep_set_qh(ep, is_in, NULL);
  356. qh->hep->hcpriv = NULL;
  357. switch (qh->type) {
  358. case USB_ENDPOINT_XFER_CONTROL:
  359. case USB_ENDPOINT_XFER_BULK:
  360. /* fifo policy for these lists, except that NAKing
  361. * should rotate a qh to the end (for fairness).
  362. */
  363. if (qh->mux == 1) {
  364. head = qh->ring.prev;
  365. list_del(&qh->ring);
  366. kfree(qh);
  367. qh = first_qh(head);
  368. break;
  369. }
  370. case USB_ENDPOINT_XFER_ISOC:
  371. case USB_ENDPOINT_XFER_INT:
  372. /* this is where periodic bandwidth should be
  373. * de-allocated if it's tracked and allocated;
  374. * and where we'd update the schedule tree...
  375. */
  376. kfree(qh);
  377. qh = NULL;
  378. break;
  379. }
  380. }
  381. if (qh != NULL && qh->is_ready) {
  382. DBG(4, "... next ep%d %cX urb %p\n",
  383. hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
  384. musb_start_urb(musb, is_in, qh);
  385. }
  386. }
  387. static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
  388. {
  389. /* we don't want fifo to fill itself again;
  390. * ignore dma (various models),
  391. * leave toggle alone (may not have been saved yet)
  392. */
  393. csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
  394. csr &= ~(MUSB_RXCSR_H_REQPKT
  395. | MUSB_RXCSR_H_AUTOREQ
  396. | MUSB_RXCSR_AUTOCLEAR);
  397. /* write 2x to allow double buffering */
  398. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  399. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  400. /* flush writebuffer */
  401. return musb_readw(hw_ep->regs, MUSB_RXCSR);
  402. }
  403. /*
  404. * PIO RX for a packet (or part of it).
  405. */
  406. static bool
  407. musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
  408. {
  409. u16 rx_count;
  410. u8 *buf;
  411. u16 csr;
  412. bool done = false;
  413. u32 length;
  414. int do_flush = 0;
  415. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  416. void __iomem *epio = hw_ep->regs;
  417. struct musb_qh *qh = hw_ep->in_qh;
  418. int pipe = urb->pipe;
  419. void *buffer = urb->transfer_buffer;
  420. /* musb_ep_select(mbase, epnum); */
  421. rx_count = musb_readw(epio, MUSB_RXCOUNT);
  422. DBG(3, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
  423. urb->transfer_buffer, qh->offset,
  424. urb->transfer_buffer_length);
  425. /* unload FIFO */
  426. if (usb_pipeisoc(pipe)) {
  427. int status = 0;
  428. struct usb_iso_packet_descriptor *d;
  429. if (iso_err) {
  430. status = -EILSEQ;
  431. urb->error_count++;
  432. }
  433. d = urb->iso_frame_desc + qh->iso_idx;
  434. buf = buffer + d->offset;
  435. length = d->length;
  436. if (rx_count > length) {
  437. if (status == 0) {
  438. status = -EOVERFLOW;
  439. urb->error_count++;
  440. }
  441. DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
  442. do_flush = 1;
  443. } else
  444. length = rx_count;
  445. urb->actual_length += length;
  446. d->actual_length = length;
  447. d->status = status;
  448. /* see if we are done */
  449. done = (++qh->iso_idx >= urb->number_of_packets);
  450. } else {
  451. /* non-isoch */
  452. buf = buffer + qh->offset;
  453. length = urb->transfer_buffer_length - qh->offset;
  454. if (rx_count > length) {
  455. if (urb->status == -EINPROGRESS)
  456. urb->status = -EOVERFLOW;
  457. DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
  458. do_flush = 1;
  459. } else
  460. length = rx_count;
  461. urb->actual_length += length;
  462. qh->offset += length;
  463. /* see if we are done */
  464. done = (urb->actual_length == urb->transfer_buffer_length)
  465. || (rx_count < qh->maxpacket)
  466. || (urb->status != -EINPROGRESS);
  467. if (done
  468. && (urb->status == -EINPROGRESS)
  469. && (urb->transfer_flags & URB_SHORT_NOT_OK)
  470. && (urb->actual_length
  471. < urb->transfer_buffer_length))
  472. urb->status = -EREMOTEIO;
  473. }
  474. musb_read_fifo(hw_ep, length, buf);
  475. csr = musb_readw(epio, MUSB_RXCSR);
  476. csr |= MUSB_RXCSR_H_WZC_BITS;
  477. if (unlikely(do_flush))
  478. musb_h_flush_rxfifo(hw_ep, csr);
  479. else {
  480. /* REVISIT this assumes AUTOCLEAR is never set */
  481. csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
  482. if (!done)
  483. csr |= MUSB_RXCSR_H_REQPKT;
  484. musb_writew(epio, MUSB_RXCSR, csr);
  485. }
  486. return done;
  487. }
  488. /* we don't always need to reinit a given side of an endpoint...
  489. * when we do, use tx/rx reinit routine and then construct a new CSR
  490. * to address data toggle, NYET, and DMA or PIO.
  491. *
  492. * it's possible that driver bugs (especially for DMA) or aborting a
  493. * transfer might have left the endpoint busier than it should be.
  494. * the busy/not-empty tests are basically paranoia.
  495. */
  496. static void
  497. musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
  498. {
  499. u16 csr;
  500. /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
  501. * That always uses tx_reinit since ep0 repurposes TX register
  502. * offsets; the initial SETUP packet is also a kind of OUT.
  503. */
  504. /* if programmed for Tx, put it in RX mode */
  505. if (ep->is_shared_fifo) {
  506. csr = musb_readw(ep->regs, MUSB_TXCSR);
  507. if (csr & MUSB_TXCSR_MODE) {
  508. musb_h_tx_flush_fifo(ep);
  509. csr = musb_readw(ep->regs, MUSB_TXCSR);
  510. musb_writew(ep->regs, MUSB_TXCSR,
  511. csr | MUSB_TXCSR_FRCDATATOG);
  512. }
  513. /*
  514. * Clear the MODE bit (and everything else) to enable Rx.
  515. * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
  516. */
  517. if (csr & MUSB_TXCSR_DMAMODE)
  518. musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
  519. musb_writew(ep->regs, MUSB_TXCSR, 0);
  520. /* scrub all previous state, clearing toggle */
  521. } else {
  522. csr = musb_readw(ep->regs, MUSB_RXCSR);
  523. if (csr & MUSB_RXCSR_RXPKTRDY)
  524. WARNING("rx%d, packet/%d ready?\n", ep->epnum,
  525. musb_readw(ep->regs, MUSB_RXCOUNT));
  526. musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
  527. }
  528. /* target addr and (for multipoint) hub addr/port */
  529. if (musb->is_multipoint) {
  530. musb_write_rxfunaddr(ep->target_regs, qh->addr_reg);
  531. musb_write_rxhubaddr(ep->target_regs, qh->h_addr_reg);
  532. musb_write_rxhubport(ep->target_regs, qh->h_port_reg);
  533. } else
  534. musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
  535. /* protocol/endpoint, interval/NAKlimit, i/o size */
  536. musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
  537. musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
  538. /* NOTE: bulk combining rewrites high bits of maxpacket */
  539. musb_writew(ep->regs, MUSB_RXMAXP, qh->maxpacket);
  540. ep->rx_reinit = 0;
  541. }
  542. static bool musb_tx_dma_program(struct dma_controller *dma,
  543. struct musb_hw_ep *hw_ep, struct musb_qh *qh,
  544. struct urb *urb, u32 offset, u32 length)
  545. {
  546. struct dma_channel *channel = hw_ep->tx_channel;
  547. void __iomem *epio = hw_ep->regs;
  548. u16 pkt_size = qh->maxpacket;
  549. u16 csr;
  550. u8 mode;
  551. #ifdef CONFIG_USB_INVENTRA_DMA
  552. if (length > channel->max_len)
  553. length = channel->max_len;
  554. csr = musb_readw(epio, MUSB_TXCSR);
  555. if (length > pkt_size) {
  556. mode = 1;
  557. csr |= MUSB_TXCSR_AUTOSET
  558. | MUSB_TXCSR_DMAMODE
  559. | MUSB_TXCSR_DMAENAB;
  560. } else {
  561. mode = 0;
  562. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
  563. csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
  564. }
  565. channel->desired_mode = mode;
  566. musb_writew(epio, MUSB_TXCSR, csr);
  567. #else
  568. if (!is_cppi_enabled() && !tusb_dma_omap())
  569. return false;
  570. channel->actual_len = 0;
  571. /*
  572. * TX uses "RNDIS" mode automatically but needs help
  573. * to identify the zero-length-final-packet case.
  574. */
  575. mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
  576. #endif
  577. qh->segsize = length;
  578. if (!dma->channel_program(channel, pkt_size, mode,
  579. urb->transfer_dma + offset, length)) {
  580. dma->channel_release(channel);
  581. hw_ep->tx_channel = NULL;
  582. csr = musb_readw(epio, MUSB_TXCSR);
  583. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
  584. musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
  585. return false;
  586. }
  587. return true;
  588. }
  589. /*
  590. * Program an HDRC endpoint as per the given URB
  591. * Context: irqs blocked, controller lock held
  592. */
  593. static void musb_ep_program(struct musb *musb, u8 epnum,
  594. struct urb *urb, int is_out,
  595. u8 *buf, u32 offset, u32 len)
  596. {
  597. struct dma_controller *dma_controller;
  598. struct dma_channel *dma_channel;
  599. u8 dma_ok;
  600. void __iomem *mbase = musb->mregs;
  601. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  602. void __iomem *epio = hw_ep->regs;
  603. struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
  604. u16 packet_sz = qh->maxpacket;
  605. DBG(3, "%s hw%d urb %p spd%d dev%d ep%d%s "
  606. "h_addr%02x h_port%02x bytes %d\n",
  607. is_out ? "-->" : "<--",
  608. epnum, urb, urb->dev->speed,
  609. qh->addr_reg, qh->epnum, is_out ? "out" : "in",
  610. qh->h_addr_reg, qh->h_port_reg,
  611. len);
  612. musb_ep_select(mbase, epnum);
  613. /* candidate for DMA? */
  614. dma_controller = musb->dma_controller;
  615. if (is_dma_capable() && epnum && dma_controller) {
  616. dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
  617. if (!dma_channel) {
  618. dma_channel = dma_controller->channel_alloc(
  619. dma_controller, hw_ep, is_out);
  620. if (is_out)
  621. hw_ep->tx_channel = dma_channel;
  622. else
  623. hw_ep->rx_channel = dma_channel;
  624. }
  625. } else
  626. dma_channel = NULL;
  627. /* make sure we clear DMAEnab, autoSet bits from previous run */
  628. /* OUT/transmit/EP0 or IN/receive? */
  629. if (is_out) {
  630. u16 csr;
  631. u16 int_txe;
  632. u16 load_count;
  633. csr = musb_readw(epio, MUSB_TXCSR);
  634. /* disable interrupt in case we flush */
  635. int_txe = musb_readw(mbase, MUSB_INTRTXE);
  636. musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
  637. /* general endpoint setup */
  638. if (epnum) {
  639. /* flush all old state, set default */
  640. musb_h_tx_flush_fifo(hw_ep);
  641. /*
  642. * We must not clear the DMAMODE bit before or in
  643. * the same cycle with the DMAENAB bit, so we clear
  644. * the latter first...
  645. */
  646. csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
  647. | MUSB_TXCSR_AUTOSET
  648. | MUSB_TXCSR_DMAENAB
  649. | MUSB_TXCSR_FRCDATATOG
  650. | MUSB_TXCSR_H_RXSTALL
  651. | MUSB_TXCSR_H_ERROR
  652. | MUSB_TXCSR_TXPKTRDY
  653. );
  654. csr |= MUSB_TXCSR_MODE;
  655. if (usb_gettoggle(urb->dev, qh->epnum, 1))
  656. csr |= MUSB_TXCSR_H_WR_DATATOGGLE
  657. | MUSB_TXCSR_H_DATATOGGLE;
  658. else
  659. csr |= MUSB_TXCSR_CLRDATATOG;
  660. musb_writew(epio, MUSB_TXCSR, csr);
  661. /* REVISIT may need to clear FLUSHFIFO ... */
  662. csr &= ~MUSB_TXCSR_DMAMODE;
  663. musb_writew(epio, MUSB_TXCSR, csr);
  664. csr = musb_readw(epio, MUSB_TXCSR);
  665. } else {
  666. /* endpoint 0: just flush */
  667. musb_h_ep0_flush_fifo(hw_ep);
  668. }
  669. /* target addr and (for multipoint) hub addr/port */
  670. if (musb->is_multipoint) {
  671. musb_write_txfunaddr(mbase, epnum, qh->addr_reg);
  672. musb_write_txhubaddr(mbase, epnum, qh->h_addr_reg);
  673. musb_write_txhubport(mbase, epnum, qh->h_port_reg);
  674. /* FIXME if !epnum, do the same for RX ... */
  675. } else
  676. musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
  677. /* protocol/endpoint/interval/NAKlimit */
  678. if (epnum) {
  679. musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
  680. if (can_bulk_split(musb, qh->type))
  681. musb_writew(epio, MUSB_TXMAXP,
  682. packet_sz
  683. | ((hw_ep->max_packet_sz_tx /
  684. packet_sz) - 1) << 11);
  685. else
  686. musb_writew(epio, MUSB_TXMAXP,
  687. packet_sz);
  688. musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
  689. } else {
  690. musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
  691. if (musb->is_multipoint)
  692. musb_writeb(epio, MUSB_TYPE0,
  693. qh->type_reg);
  694. }
  695. if (can_bulk_split(musb, qh->type))
  696. load_count = min((u32) hw_ep->max_packet_sz_tx,
  697. len);
  698. else
  699. load_count = min((u32) packet_sz, len);
  700. if (dma_channel && musb_tx_dma_program(dma_controller,
  701. hw_ep, qh, urb, offset, len))
  702. load_count = 0;
  703. if (load_count) {
  704. /* PIO to load FIFO */
  705. qh->segsize = load_count;
  706. musb_write_fifo(hw_ep, load_count, buf);
  707. }
  708. /* re-enable interrupt */
  709. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  710. /* IN/receive */
  711. } else {
  712. u16 csr;
  713. if (hw_ep->rx_reinit) {
  714. musb_rx_reinit(musb, qh, hw_ep);
  715. /* init new state: toggle and NYET, maybe DMA later */
  716. if (usb_gettoggle(urb->dev, qh->epnum, 0))
  717. csr = MUSB_RXCSR_H_WR_DATATOGGLE
  718. | MUSB_RXCSR_H_DATATOGGLE;
  719. else
  720. csr = 0;
  721. if (qh->type == USB_ENDPOINT_XFER_INT)
  722. csr |= MUSB_RXCSR_DISNYET;
  723. } else {
  724. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  725. if (csr & (MUSB_RXCSR_RXPKTRDY
  726. | MUSB_RXCSR_DMAENAB
  727. | MUSB_RXCSR_H_REQPKT))
  728. ERR("broken !rx_reinit, ep%d csr %04x\n",
  729. hw_ep->epnum, csr);
  730. /* scrub any stale state, leaving toggle alone */
  731. csr &= MUSB_RXCSR_DISNYET;
  732. }
  733. /* kick things off */
  734. if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
  735. /* candidate for DMA */
  736. if (dma_channel) {
  737. dma_channel->actual_len = 0L;
  738. qh->segsize = len;
  739. /* AUTOREQ is in a DMA register */
  740. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  741. csr = musb_readw(hw_ep->regs,
  742. MUSB_RXCSR);
  743. /* unless caller treats short rx transfers as
  744. * errors, we dare not queue multiple transfers.
  745. */
  746. dma_ok = dma_controller->channel_program(
  747. dma_channel, packet_sz,
  748. !(urb->transfer_flags
  749. & URB_SHORT_NOT_OK),
  750. urb->transfer_dma + offset,
  751. qh->segsize);
  752. if (!dma_ok) {
  753. dma_controller->channel_release(
  754. dma_channel);
  755. hw_ep->rx_channel = NULL;
  756. dma_channel = NULL;
  757. } else
  758. csr |= MUSB_RXCSR_DMAENAB;
  759. }
  760. }
  761. csr |= MUSB_RXCSR_H_REQPKT;
  762. DBG(7, "RXCSR%d := %04x\n", epnum, csr);
  763. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  764. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  765. }
  766. }
  767. /*
  768. * Service the default endpoint (ep0) as host.
  769. * Return true until it's time to start the status stage.
  770. */
  771. static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
  772. {
  773. bool more = false;
  774. u8 *fifo_dest = NULL;
  775. u16 fifo_count = 0;
  776. struct musb_hw_ep *hw_ep = musb->control_ep;
  777. struct musb_qh *qh = hw_ep->in_qh;
  778. struct usb_ctrlrequest *request;
  779. switch (musb->ep0_stage) {
  780. case MUSB_EP0_IN:
  781. fifo_dest = urb->transfer_buffer + urb->actual_length;
  782. fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
  783. urb->actual_length);
  784. if (fifo_count < len)
  785. urb->status = -EOVERFLOW;
  786. musb_read_fifo(hw_ep, fifo_count, fifo_dest);
  787. urb->actual_length += fifo_count;
  788. if (len < qh->maxpacket) {
  789. /* always terminate on short read; it's
  790. * rarely reported as an error.
  791. */
  792. } else if (urb->actual_length <
  793. urb->transfer_buffer_length)
  794. more = true;
  795. break;
  796. case MUSB_EP0_START:
  797. request = (struct usb_ctrlrequest *) urb->setup_packet;
  798. if (!request->wLength) {
  799. DBG(4, "start no-DATA\n");
  800. break;
  801. } else if (request->bRequestType & USB_DIR_IN) {
  802. DBG(4, "start IN-DATA\n");
  803. musb->ep0_stage = MUSB_EP0_IN;
  804. more = true;
  805. break;
  806. } else {
  807. DBG(4, "start OUT-DATA\n");
  808. musb->ep0_stage = MUSB_EP0_OUT;
  809. more = true;
  810. }
  811. /* FALLTHROUGH */
  812. case MUSB_EP0_OUT:
  813. fifo_count = min_t(size_t, qh->maxpacket,
  814. urb->transfer_buffer_length -
  815. urb->actual_length);
  816. if (fifo_count) {
  817. fifo_dest = (u8 *) (urb->transfer_buffer
  818. + urb->actual_length);
  819. DBG(3, "Sending %d byte%s to ep0 fifo %p\n",
  820. fifo_count,
  821. (fifo_count == 1) ? "" : "s",
  822. fifo_dest);
  823. musb_write_fifo(hw_ep, fifo_count, fifo_dest);
  824. urb->actual_length += fifo_count;
  825. more = true;
  826. }
  827. break;
  828. default:
  829. ERR("bogus ep0 stage %d\n", musb->ep0_stage);
  830. break;
  831. }
  832. return more;
  833. }
  834. /*
  835. * Handle default endpoint interrupt as host. Only called in IRQ time
  836. * from musb_interrupt().
  837. *
  838. * called with controller irqlocked
  839. */
  840. irqreturn_t musb_h_ep0_irq(struct musb *musb)
  841. {
  842. struct urb *urb;
  843. u16 csr, len;
  844. int status = 0;
  845. void __iomem *mbase = musb->mregs;
  846. struct musb_hw_ep *hw_ep = musb->control_ep;
  847. void __iomem *epio = hw_ep->regs;
  848. struct musb_qh *qh = hw_ep->in_qh;
  849. bool complete = false;
  850. irqreturn_t retval = IRQ_NONE;
  851. /* ep0 only has one queue, "in" */
  852. urb = next_urb(qh);
  853. musb_ep_select(mbase, 0);
  854. csr = musb_readw(epio, MUSB_CSR0);
  855. len = (csr & MUSB_CSR0_RXPKTRDY)
  856. ? musb_readb(epio, MUSB_COUNT0)
  857. : 0;
  858. DBG(4, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
  859. csr, qh, len, urb, musb->ep0_stage);
  860. /* if we just did status stage, we are done */
  861. if (MUSB_EP0_STATUS == musb->ep0_stage) {
  862. retval = IRQ_HANDLED;
  863. complete = true;
  864. }
  865. /* prepare status */
  866. if (csr & MUSB_CSR0_H_RXSTALL) {
  867. DBG(6, "STALLING ENDPOINT\n");
  868. status = -EPIPE;
  869. } else if (csr & MUSB_CSR0_H_ERROR) {
  870. DBG(2, "no response, csr0 %04x\n", csr);
  871. status = -EPROTO;
  872. } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
  873. DBG(2, "control NAK timeout\n");
  874. /* NOTE: this code path would be a good place to PAUSE a
  875. * control transfer, if another one is queued, so that
  876. * ep0 is more likely to stay busy. That's already done
  877. * for bulk RX transfers.
  878. *
  879. * if (qh->ring.next != &musb->control), then
  880. * we have a candidate... NAKing is *NOT* an error
  881. */
  882. musb_writew(epio, MUSB_CSR0, 0);
  883. retval = IRQ_HANDLED;
  884. }
  885. if (status) {
  886. DBG(6, "aborting\n");
  887. retval = IRQ_HANDLED;
  888. if (urb)
  889. urb->status = status;
  890. complete = true;
  891. /* use the proper sequence to abort the transfer */
  892. if (csr & MUSB_CSR0_H_REQPKT) {
  893. csr &= ~MUSB_CSR0_H_REQPKT;
  894. musb_writew(epio, MUSB_CSR0, csr);
  895. csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
  896. musb_writew(epio, MUSB_CSR0, csr);
  897. } else {
  898. musb_h_ep0_flush_fifo(hw_ep);
  899. }
  900. musb_writeb(epio, MUSB_NAKLIMIT0, 0);
  901. /* clear it */
  902. musb_writew(epio, MUSB_CSR0, 0);
  903. }
  904. if (unlikely(!urb)) {
  905. /* stop endpoint since we have no place for its data, this
  906. * SHOULD NEVER HAPPEN! */
  907. ERR("no URB for end 0\n");
  908. musb_h_ep0_flush_fifo(hw_ep);
  909. goto done;
  910. }
  911. if (!complete) {
  912. /* call common logic and prepare response */
  913. if (musb_h_ep0_continue(musb, len, urb)) {
  914. /* more packets required */
  915. csr = (MUSB_EP0_IN == musb->ep0_stage)
  916. ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
  917. } else {
  918. /* data transfer complete; perform status phase */
  919. if (usb_pipeout(urb->pipe)
  920. || !urb->transfer_buffer_length)
  921. csr = MUSB_CSR0_H_STATUSPKT
  922. | MUSB_CSR0_H_REQPKT;
  923. else
  924. csr = MUSB_CSR0_H_STATUSPKT
  925. | MUSB_CSR0_TXPKTRDY;
  926. /* flag status stage */
  927. musb->ep0_stage = MUSB_EP0_STATUS;
  928. DBG(5, "ep0 STATUS, csr %04x\n", csr);
  929. }
  930. musb_writew(epio, MUSB_CSR0, csr);
  931. retval = IRQ_HANDLED;
  932. } else
  933. musb->ep0_stage = MUSB_EP0_IDLE;
  934. /* call completion handler if done */
  935. if (complete)
  936. musb_advance_schedule(musb, urb, hw_ep, 1);
  937. done:
  938. return retval;
  939. }
  940. #ifdef CONFIG_USB_INVENTRA_DMA
  941. /* Host side TX (OUT) using Mentor DMA works as follows:
  942. submit_urb ->
  943. - if queue was empty, Program Endpoint
  944. - ... which starts DMA to fifo in mode 1 or 0
  945. DMA Isr (transfer complete) -> TxAvail()
  946. - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
  947. only in musb_cleanup_urb)
  948. - TxPktRdy has to be set in mode 0 or for
  949. short packets in mode 1.
  950. */
  951. #endif
  952. /* Service a Tx-Available or dma completion irq for the endpoint */
  953. void musb_host_tx(struct musb *musb, u8 epnum)
  954. {
  955. int pipe;
  956. bool done = false;
  957. u16 tx_csr;
  958. size_t length = 0;
  959. size_t offset = 0;
  960. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  961. void __iomem *epio = hw_ep->regs;
  962. struct musb_qh *qh = hw_ep->out_qh;
  963. struct urb *urb = next_urb(qh);
  964. u32 status = 0;
  965. void __iomem *mbase = musb->mregs;
  966. struct dma_channel *dma;
  967. musb_ep_select(mbase, epnum);
  968. tx_csr = musb_readw(epio, MUSB_TXCSR);
  969. /* with CPPI, DMA sometimes triggers "extra" irqs */
  970. if (!urb) {
  971. DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
  972. return;
  973. }
  974. pipe = urb->pipe;
  975. dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
  976. DBG(4, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
  977. dma ? ", dma" : "");
  978. /* check for errors */
  979. if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
  980. /* dma was disabled, fifo flushed */
  981. DBG(3, "TX end %d stall\n", epnum);
  982. /* stall; record URB status */
  983. status = -EPIPE;
  984. } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
  985. /* (NON-ISO) dma was disabled, fifo flushed */
  986. DBG(3, "TX 3strikes on ep=%d\n", epnum);
  987. status = -ETIMEDOUT;
  988. } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
  989. DBG(6, "TX end=%d device not responding\n", epnum);
  990. /* NOTE: this code path would be a good place to PAUSE a
  991. * transfer, if there's some other (nonperiodic) tx urb
  992. * that could use this fifo. (dma complicates it...)
  993. * That's already done for bulk RX transfers.
  994. *
  995. * if (bulk && qh->ring.next != &musb->out_bulk), then
  996. * we have a candidate... NAKing is *NOT* an error
  997. */
  998. musb_ep_select(mbase, epnum);
  999. musb_writew(epio, MUSB_TXCSR,
  1000. MUSB_TXCSR_H_WZC_BITS
  1001. | MUSB_TXCSR_TXPKTRDY);
  1002. return;
  1003. }
  1004. if (status) {
  1005. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1006. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1007. (void) musb->dma_controller->channel_abort(dma);
  1008. }
  1009. /* do the proper sequence to abort the transfer in the
  1010. * usb core; the dma engine should already be stopped.
  1011. */
  1012. musb_h_tx_flush_fifo(hw_ep);
  1013. tx_csr &= ~(MUSB_TXCSR_AUTOSET
  1014. | MUSB_TXCSR_DMAENAB
  1015. | MUSB_TXCSR_H_ERROR
  1016. | MUSB_TXCSR_H_RXSTALL
  1017. | MUSB_TXCSR_H_NAKTIMEOUT
  1018. );
  1019. musb_ep_select(mbase, epnum);
  1020. musb_writew(epio, MUSB_TXCSR, tx_csr);
  1021. /* REVISIT may need to clear FLUSHFIFO ... */
  1022. musb_writew(epio, MUSB_TXCSR, tx_csr);
  1023. musb_writeb(epio, MUSB_TXINTERVAL, 0);
  1024. done = true;
  1025. }
  1026. /* second cppi case */
  1027. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1028. DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
  1029. return;
  1030. }
  1031. if (is_dma_capable() && dma && !status) {
  1032. /*
  1033. * DMA has completed. But if we're using DMA mode 1 (multi
  1034. * packet DMA), we need a terminal TXPKTRDY interrupt before
  1035. * we can consider this transfer completed, lest we trash
  1036. * its last packet when writing the next URB's data. So we
  1037. * switch back to mode 0 to get that interrupt; we'll come
  1038. * back here once it happens.
  1039. */
  1040. if (tx_csr & MUSB_TXCSR_DMAMODE) {
  1041. /*
  1042. * We shouldn't clear DMAMODE with DMAENAB set; so
  1043. * clear them in a safe order. That should be OK
  1044. * once TXPKTRDY has been set (and I've never seen
  1045. * it being 0 at this moment -- DMA interrupt latency
  1046. * is significant) but if it hasn't been then we have
  1047. * no choice but to stop being polite and ignore the
  1048. * programmer's guide... :-)
  1049. *
  1050. * Note that we must write TXCSR with TXPKTRDY cleared
  1051. * in order not to re-trigger the packet send (this bit
  1052. * can't be cleared by CPU), and there's another caveat:
  1053. * TXPKTRDY may be set shortly and then cleared in the
  1054. * double-buffered FIFO mode, so we do an extra TXCSR
  1055. * read for debouncing...
  1056. */
  1057. tx_csr &= musb_readw(epio, MUSB_TXCSR);
  1058. if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
  1059. tx_csr &= ~(MUSB_TXCSR_DMAENAB |
  1060. MUSB_TXCSR_TXPKTRDY);
  1061. musb_writew(epio, MUSB_TXCSR,
  1062. tx_csr | MUSB_TXCSR_H_WZC_BITS);
  1063. }
  1064. tx_csr &= ~(MUSB_TXCSR_DMAMODE |
  1065. MUSB_TXCSR_TXPKTRDY);
  1066. musb_writew(epio, MUSB_TXCSR,
  1067. tx_csr | MUSB_TXCSR_H_WZC_BITS);
  1068. /*
  1069. * There is no guarantee that we'll get an interrupt
  1070. * after clearing DMAMODE as we might have done this
  1071. * too late (after TXPKTRDY was cleared by controller).
  1072. * Re-read TXCSR as we have spoiled its previous value.
  1073. */
  1074. tx_csr = musb_readw(epio, MUSB_TXCSR);
  1075. }
  1076. /*
  1077. * We may get here from a DMA completion or TXPKTRDY interrupt.
  1078. * In any case, we must check the FIFO status here and bail out
  1079. * only if the FIFO still has data -- that should prevent the
  1080. * "missed" TXPKTRDY interrupts and deal with double-buffered
  1081. * FIFO mode too...
  1082. */
  1083. if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
  1084. DBG(2, "DMA complete but packet still in FIFO, "
  1085. "CSR %04x\n", tx_csr);
  1086. return;
  1087. }
  1088. }
  1089. if (!status || dma || usb_pipeisoc(pipe)) {
  1090. if (dma)
  1091. length = dma->actual_len;
  1092. else
  1093. length = qh->segsize;
  1094. qh->offset += length;
  1095. if (usb_pipeisoc(pipe)) {
  1096. struct usb_iso_packet_descriptor *d;
  1097. d = urb->iso_frame_desc + qh->iso_idx;
  1098. d->actual_length = length;
  1099. d->status = status;
  1100. if (++qh->iso_idx >= urb->number_of_packets) {
  1101. done = true;
  1102. } else {
  1103. d++;
  1104. offset = d->offset;
  1105. length = d->length;
  1106. }
  1107. } else if (dma) {
  1108. done = true;
  1109. } else {
  1110. /* see if we need to send more data, or ZLP */
  1111. if (qh->segsize < qh->maxpacket)
  1112. done = true;
  1113. else if (qh->offset == urb->transfer_buffer_length
  1114. && !(urb->transfer_flags
  1115. & URB_ZERO_PACKET))
  1116. done = true;
  1117. if (!done) {
  1118. offset = qh->offset;
  1119. length = urb->transfer_buffer_length - offset;
  1120. }
  1121. }
  1122. }
  1123. /* urb->status != -EINPROGRESS means request has been faulted,
  1124. * so we must abort this transfer after cleanup
  1125. */
  1126. if (urb->status != -EINPROGRESS) {
  1127. done = true;
  1128. if (status == 0)
  1129. status = urb->status;
  1130. }
  1131. if (done) {
  1132. /* set status */
  1133. urb->status = status;
  1134. urb->actual_length = qh->offset;
  1135. musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
  1136. return;
  1137. } else if (usb_pipeisoc(pipe) && dma) {
  1138. if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
  1139. offset, length))
  1140. return;
  1141. } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
  1142. DBG(1, "not complete, but DMA enabled?\n");
  1143. return;
  1144. }
  1145. /*
  1146. * PIO: start next packet in this URB.
  1147. *
  1148. * REVISIT: some docs say that when hw_ep->tx_double_buffered,
  1149. * (and presumably, FIFO is not half-full) we should write *two*
  1150. * packets before updating TXCSR; other docs disagree...
  1151. */
  1152. if (length > qh->maxpacket)
  1153. length = qh->maxpacket;
  1154. musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
  1155. qh->segsize = length;
  1156. musb_ep_select(mbase, epnum);
  1157. musb_writew(epio, MUSB_TXCSR,
  1158. MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
  1159. }
  1160. #ifdef CONFIG_USB_INVENTRA_DMA
  1161. /* Host side RX (IN) using Mentor DMA works as follows:
  1162. submit_urb ->
  1163. - if queue was empty, ProgramEndpoint
  1164. - first IN token is sent out (by setting ReqPkt)
  1165. LinuxIsr -> RxReady()
  1166. /\ => first packet is received
  1167. | - Set in mode 0 (DmaEnab, ~ReqPkt)
  1168. | -> DMA Isr (transfer complete) -> RxReady()
  1169. | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
  1170. | - if urb not complete, send next IN token (ReqPkt)
  1171. | | else complete urb.
  1172. | |
  1173. ---------------------------
  1174. *
  1175. * Nuances of mode 1:
  1176. * For short packets, no ack (+RxPktRdy) is sent automatically
  1177. * (even if AutoClear is ON)
  1178. * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
  1179. * automatically => major problem, as collecting the next packet becomes
  1180. * difficult. Hence mode 1 is not used.
  1181. *
  1182. * REVISIT
  1183. * All we care about at this driver level is that
  1184. * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
  1185. * (b) termination conditions are: short RX, or buffer full;
  1186. * (c) fault modes include
  1187. * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
  1188. * (and that endpoint's dma queue stops immediately)
  1189. * - overflow (full, PLUS more bytes in the terminal packet)
  1190. *
  1191. * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
  1192. * thus be a great candidate for using mode 1 ... for all but the
  1193. * last packet of one URB's transfer.
  1194. */
  1195. #endif
  1196. /* Schedule next QH from musb->in_bulk and move the current qh to
  1197. * the end; avoids starvation for other endpoints.
  1198. */
  1199. static void musb_bulk_rx_nak_timeout(struct musb *musb, struct musb_hw_ep *ep)
  1200. {
  1201. struct dma_channel *dma;
  1202. struct urb *urb;
  1203. void __iomem *mbase = musb->mregs;
  1204. void __iomem *epio = ep->regs;
  1205. struct musb_qh *cur_qh, *next_qh;
  1206. u16 rx_csr;
  1207. musb_ep_select(mbase, ep->epnum);
  1208. dma = is_dma_capable() ? ep->rx_channel : NULL;
  1209. /* clear nak timeout bit */
  1210. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1211. rx_csr |= MUSB_RXCSR_H_WZC_BITS;
  1212. rx_csr &= ~MUSB_RXCSR_DATAERROR;
  1213. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1214. cur_qh = first_qh(&musb->in_bulk);
  1215. if (cur_qh) {
  1216. urb = next_urb(cur_qh);
  1217. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1218. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1219. musb->dma_controller->channel_abort(dma);
  1220. urb->actual_length += dma->actual_len;
  1221. dma->actual_len = 0L;
  1222. }
  1223. musb_save_toggle(cur_qh, 1, urb);
  1224. /* move cur_qh to end of queue */
  1225. list_move_tail(&cur_qh->ring, &musb->in_bulk);
  1226. /* get the next qh from musb->in_bulk */
  1227. next_qh = first_qh(&musb->in_bulk);
  1228. /* set rx_reinit and schedule the next qh */
  1229. ep->rx_reinit = 1;
  1230. musb_start_urb(musb, 1, next_qh);
  1231. }
  1232. }
  1233. /*
  1234. * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
  1235. * and high-bandwidth IN transfer cases.
  1236. */
  1237. void musb_host_rx(struct musb *musb, u8 epnum)
  1238. {
  1239. struct urb *urb;
  1240. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1241. void __iomem *epio = hw_ep->regs;
  1242. struct musb_qh *qh = hw_ep->in_qh;
  1243. size_t xfer_len;
  1244. void __iomem *mbase = musb->mregs;
  1245. int pipe;
  1246. u16 rx_csr, val;
  1247. bool iso_err = false;
  1248. bool done = false;
  1249. u32 status;
  1250. struct dma_channel *dma;
  1251. musb_ep_select(mbase, epnum);
  1252. urb = next_urb(qh);
  1253. dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
  1254. status = 0;
  1255. xfer_len = 0;
  1256. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1257. val = rx_csr;
  1258. if (unlikely(!urb)) {
  1259. /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
  1260. * usbtest #11 (unlinks) triggers it regularly, sometimes
  1261. * with fifo full. (Only with DMA??)
  1262. */
  1263. DBG(3, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
  1264. musb_readw(epio, MUSB_RXCOUNT));
  1265. musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
  1266. return;
  1267. }
  1268. pipe = urb->pipe;
  1269. DBG(5, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
  1270. epnum, rx_csr, urb->actual_length,
  1271. dma ? dma->actual_len : 0);
  1272. /* check for errors, concurrent stall & unlink is not really
  1273. * handled yet! */
  1274. if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
  1275. DBG(3, "RX end %d STALL\n", epnum);
  1276. /* stall; record URB status */
  1277. status = -EPIPE;
  1278. } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
  1279. DBG(3, "end %d RX proto error\n", epnum);
  1280. status = -EPROTO;
  1281. musb_writeb(epio, MUSB_RXINTERVAL, 0);
  1282. } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
  1283. if (USB_ENDPOINT_XFER_ISOC != qh->type) {
  1284. DBG(6, "RX end %d NAK timeout\n", epnum);
  1285. /* NOTE: NAKing is *NOT* an error, so we want to
  1286. * continue. Except ... if there's a request for
  1287. * another QH, use that instead of starving it.
  1288. *
  1289. * Devices like Ethernet and serial adapters keep
  1290. * reads posted at all times, which will starve
  1291. * other devices without this logic.
  1292. */
  1293. if (usb_pipebulk(urb->pipe)
  1294. && qh->mux == 1
  1295. && !list_is_singular(&musb->in_bulk)) {
  1296. musb_bulk_rx_nak_timeout(musb, hw_ep);
  1297. return;
  1298. }
  1299. musb_ep_select(mbase, epnum);
  1300. rx_csr |= MUSB_RXCSR_H_WZC_BITS;
  1301. rx_csr &= ~MUSB_RXCSR_DATAERROR;
  1302. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1303. goto finish;
  1304. } else {
  1305. DBG(4, "RX end %d ISO data error\n", epnum);
  1306. /* packet error reported later */
  1307. iso_err = true;
  1308. }
  1309. }
  1310. /* faults abort the transfer */
  1311. if (status) {
  1312. /* clean up dma and collect transfer count */
  1313. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1314. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1315. (void) musb->dma_controller->channel_abort(dma);
  1316. xfer_len = dma->actual_len;
  1317. }
  1318. musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
  1319. musb_writeb(epio, MUSB_RXINTERVAL, 0);
  1320. done = true;
  1321. goto finish;
  1322. }
  1323. if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
  1324. /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
  1325. ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
  1326. goto finish;
  1327. }
  1328. /* thorough shutdown for now ... given more precise fault handling
  1329. * and better queueing support, we might keep a DMA pipeline going
  1330. * while processing this irq for earlier completions.
  1331. */
  1332. /* FIXME this is _way_ too much in-line logic for Mentor DMA */
  1333. #ifndef CONFIG_USB_INVENTRA_DMA
  1334. if (rx_csr & MUSB_RXCSR_H_REQPKT) {
  1335. /* REVISIT this happened for a while on some short reads...
  1336. * the cleanup still needs investigation... looks bad...
  1337. * and also duplicates dma cleanup code above ... plus,
  1338. * shouldn't this be the "half full" double buffer case?
  1339. */
  1340. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1341. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1342. (void) musb->dma_controller->channel_abort(dma);
  1343. xfer_len = dma->actual_len;
  1344. done = true;
  1345. }
  1346. DBG(2, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
  1347. xfer_len, dma ? ", dma" : "");
  1348. rx_csr &= ~MUSB_RXCSR_H_REQPKT;
  1349. musb_ep_select(mbase, epnum);
  1350. musb_writew(epio, MUSB_RXCSR,
  1351. MUSB_RXCSR_H_WZC_BITS | rx_csr);
  1352. }
  1353. #endif
  1354. if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
  1355. xfer_len = dma->actual_len;
  1356. val &= ~(MUSB_RXCSR_DMAENAB
  1357. | MUSB_RXCSR_H_AUTOREQ
  1358. | MUSB_RXCSR_AUTOCLEAR
  1359. | MUSB_RXCSR_RXPKTRDY);
  1360. musb_writew(hw_ep->regs, MUSB_RXCSR, val);
  1361. #ifdef CONFIG_USB_INVENTRA_DMA
  1362. if (usb_pipeisoc(pipe)) {
  1363. struct usb_iso_packet_descriptor *d;
  1364. d = urb->iso_frame_desc + qh->iso_idx;
  1365. d->actual_length = xfer_len;
  1366. /* even if there was an error, we did the dma
  1367. * for iso_frame_desc->length
  1368. */
  1369. if (d->status != EILSEQ && d->status != -EOVERFLOW)
  1370. d->status = 0;
  1371. if (++qh->iso_idx >= urb->number_of_packets)
  1372. done = true;
  1373. else
  1374. done = false;
  1375. } else {
  1376. /* done if urb buffer is full or short packet is recd */
  1377. done = (urb->actual_length + xfer_len >=
  1378. urb->transfer_buffer_length
  1379. || dma->actual_len < qh->maxpacket);
  1380. }
  1381. /* send IN token for next packet, without AUTOREQ */
  1382. if (!done) {
  1383. val |= MUSB_RXCSR_H_REQPKT;
  1384. musb_writew(epio, MUSB_RXCSR,
  1385. MUSB_RXCSR_H_WZC_BITS | val);
  1386. }
  1387. DBG(4, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
  1388. done ? "off" : "reset",
  1389. musb_readw(epio, MUSB_RXCSR),
  1390. musb_readw(epio, MUSB_RXCOUNT));
  1391. #else
  1392. done = true;
  1393. #endif
  1394. } else if (urb->status == -EINPROGRESS) {
  1395. /* if no errors, be sure a packet is ready for unloading */
  1396. if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
  1397. status = -EPROTO;
  1398. ERR("Rx interrupt with no errors or packet!\n");
  1399. /* FIXME this is another "SHOULD NEVER HAPPEN" */
  1400. /* SCRUB (RX) */
  1401. /* do the proper sequence to abort the transfer */
  1402. musb_ep_select(mbase, epnum);
  1403. val &= ~MUSB_RXCSR_H_REQPKT;
  1404. musb_writew(epio, MUSB_RXCSR, val);
  1405. goto finish;
  1406. }
  1407. /* we are expecting IN packets */
  1408. #ifdef CONFIG_USB_INVENTRA_DMA
  1409. if (dma) {
  1410. struct dma_controller *c;
  1411. u16 rx_count;
  1412. int ret, length;
  1413. dma_addr_t buf;
  1414. rx_count = musb_readw(epio, MUSB_RXCOUNT);
  1415. DBG(2, "RX%d count %d, buffer 0x%x len %d/%d\n",
  1416. epnum, rx_count,
  1417. urb->transfer_dma
  1418. + urb->actual_length,
  1419. qh->offset,
  1420. urb->transfer_buffer_length);
  1421. c = musb->dma_controller;
  1422. if (usb_pipeisoc(pipe)) {
  1423. int status = 0;
  1424. struct usb_iso_packet_descriptor *d;
  1425. d = urb->iso_frame_desc + qh->iso_idx;
  1426. if (iso_err) {
  1427. status = -EILSEQ;
  1428. urb->error_count++;
  1429. }
  1430. if (rx_count > d->length) {
  1431. if (status == 0) {
  1432. status = -EOVERFLOW;
  1433. urb->error_count++;
  1434. }
  1435. DBG(2, "** OVERFLOW %d into %d\n",\
  1436. rx_count, d->length);
  1437. length = d->length;
  1438. } else
  1439. length = rx_count;
  1440. d->status = status;
  1441. buf = urb->transfer_dma + d->offset;
  1442. } else {
  1443. length = rx_count;
  1444. buf = urb->transfer_dma +
  1445. urb->actual_length;
  1446. }
  1447. dma->desired_mode = 0;
  1448. #ifdef USE_MODE1
  1449. /* because of the issue below, mode 1 will
  1450. * only rarely behave with correct semantics.
  1451. */
  1452. if ((urb->transfer_flags &
  1453. URB_SHORT_NOT_OK)
  1454. && (urb->transfer_buffer_length -
  1455. urb->actual_length)
  1456. > qh->maxpacket)
  1457. dma->desired_mode = 1;
  1458. if (rx_count < hw_ep->max_packet_sz_rx) {
  1459. length = rx_count;
  1460. dma->bDesiredMode = 0;
  1461. } else {
  1462. length = urb->transfer_buffer_length;
  1463. }
  1464. #endif
  1465. /* Disadvantage of using mode 1:
  1466. * It's basically usable only for mass storage class; essentially all
  1467. * other protocols also terminate transfers on short packets.
  1468. *
  1469. * Details:
  1470. * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
  1471. * If you try to use mode 1 for (transfer_buffer_length - 512), and try
  1472. * to use the extra IN token to grab the last packet using mode 0, then
  1473. * the problem is that you cannot be sure when the device will send the
  1474. * last packet and RxPktRdy set. Sometimes the packet is recd too soon
  1475. * such that it gets lost when RxCSR is re-set at the end of the mode 1
  1476. * transfer, while sometimes it is recd just a little late so that if you
  1477. * try to configure for mode 0 soon after the mode 1 transfer is
  1478. * completed, you will find rxcount 0. Okay, so you might think why not
  1479. * wait for an interrupt when the pkt is recd. Well, you won't get any!
  1480. */
  1481. val = musb_readw(epio, MUSB_RXCSR);
  1482. val &= ~MUSB_RXCSR_H_REQPKT;
  1483. if (dma->desired_mode == 0)
  1484. val &= ~MUSB_RXCSR_H_AUTOREQ;
  1485. else
  1486. val |= MUSB_RXCSR_H_AUTOREQ;
  1487. val |= MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAENAB;
  1488. musb_writew(epio, MUSB_RXCSR,
  1489. MUSB_RXCSR_H_WZC_BITS | val);
  1490. /* REVISIT if when actual_length != 0,
  1491. * transfer_buffer_length needs to be
  1492. * adjusted first...
  1493. */
  1494. ret = c->channel_program(
  1495. dma, qh->maxpacket,
  1496. dma->desired_mode, buf, length);
  1497. if (!ret) {
  1498. c->channel_release(dma);
  1499. hw_ep->rx_channel = NULL;
  1500. dma = NULL;
  1501. /* REVISIT reset CSR */
  1502. }
  1503. }
  1504. #endif /* Mentor DMA */
  1505. if (!dma) {
  1506. done = musb_host_packet_rx(musb, urb,
  1507. epnum, iso_err);
  1508. DBG(6, "read %spacket\n", done ? "last " : "");
  1509. }
  1510. }
  1511. finish:
  1512. urb->actual_length += xfer_len;
  1513. qh->offset += xfer_len;
  1514. if (done) {
  1515. if (urb->status == -EINPROGRESS)
  1516. urb->status = status;
  1517. musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
  1518. }
  1519. }
  1520. /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
  1521. * the software schedule associates multiple such nodes with a given
  1522. * host side hardware endpoint + direction; scheduling may activate
  1523. * that hardware endpoint.
  1524. */
  1525. static int musb_schedule(
  1526. struct musb *musb,
  1527. struct musb_qh *qh,
  1528. int is_in)
  1529. {
  1530. int idle;
  1531. int best_diff;
  1532. int best_end, epnum;
  1533. struct musb_hw_ep *hw_ep = NULL;
  1534. struct list_head *head = NULL;
  1535. /* use fixed hardware for control and bulk */
  1536. if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
  1537. head = &musb->control;
  1538. hw_ep = musb->control_ep;
  1539. goto success;
  1540. }
  1541. /* else, periodic transfers get muxed to other endpoints */
  1542. /*
  1543. * We know this qh hasn't been scheduled, so all we need to do
  1544. * is choose which hardware endpoint to put it on ...
  1545. *
  1546. * REVISIT what we really want here is a regular schedule tree
  1547. * like e.g. OHCI uses.
  1548. */
  1549. best_diff = 4096;
  1550. best_end = -1;
  1551. for (epnum = 1, hw_ep = musb->endpoints + 1;
  1552. epnum < musb->nr_endpoints;
  1553. epnum++, hw_ep++) {
  1554. int diff;
  1555. if (musb_ep_get_qh(hw_ep, is_in) != NULL)
  1556. continue;
  1557. if (hw_ep == musb->bulk_ep)
  1558. continue;
  1559. if (is_in)
  1560. diff = hw_ep->max_packet_sz_rx - qh->maxpacket;
  1561. else
  1562. diff = hw_ep->max_packet_sz_tx - qh->maxpacket;
  1563. if (diff >= 0 && best_diff > diff) {
  1564. best_diff = diff;
  1565. best_end = epnum;
  1566. }
  1567. }
  1568. /* use bulk reserved ep1 if no other ep is free */
  1569. if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
  1570. hw_ep = musb->bulk_ep;
  1571. if (is_in)
  1572. head = &musb->in_bulk;
  1573. else
  1574. head = &musb->out_bulk;
  1575. /* Enable bulk RX NAK timeout scheme when bulk requests are
  1576. * multiplexed. This scheme doen't work in high speed to full
  1577. * speed scenario as NAK interrupts are not coming from a
  1578. * full speed device connected to a high speed device.
  1579. * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
  1580. * 4 (8 frame or 8ms) for FS device.
  1581. */
  1582. if (is_in && qh->dev)
  1583. qh->intv_reg =
  1584. (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
  1585. goto success;
  1586. } else if (best_end < 0) {
  1587. return -ENOSPC;
  1588. }
  1589. idle = 1;
  1590. qh->mux = 0;
  1591. hw_ep = musb->endpoints + best_end;
  1592. DBG(4, "qh %p periodic slot %d\n", qh, best_end);
  1593. success:
  1594. if (head) {
  1595. idle = list_empty(head);
  1596. list_add_tail(&qh->ring, head);
  1597. qh->mux = 1;
  1598. }
  1599. qh->hw_ep = hw_ep;
  1600. qh->hep->hcpriv = qh;
  1601. if (idle)
  1602. musb_start_urb(musb, is_in, qh);
  1603. return 0;
  1604. }
  1605. static int musb_urb_enqueue(
  1606. struct usb_hcd *hcd,
  1607. struct urb *urb,
  1608. gfp_t mem_flags)
  1609. {
  1610. unsigned long flags;
  1611. struct musb *musb = hcd_to_musb(hcd);
  1612. struct usb_host_endpoint *hep = urb->ep;
  1613. struct musb_qh *qh;
  1614. struct usb_endpoint_descriptor *epd = &hep->desc;
  1615. int ret;
  1616. unsigned type_reg;
  1617. unsigned interval;
  1618. /* host role must be active */
  1619. if (!is_host_active(musb) || !musb->is_active)
  1620. return -ENODEV;
  1621. spin_lock_irqsave(&musb->lock, flags);
  1622. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  1623. qh = ret ? NULL : hep->hcpriv;
  1624. if (qh)
  1625. urb->hcpriv = qh;
  1626. spin_unlock_irqrestore(&musb->lock, flags);
  1627. /* DMA mapping was already done, if needed, and this urb is on
  1628. * hep->urb_list now ... so we're done, unless hep wasn't yet
  1629. * scheduled onto a live qh.
  1630. *
  1631. * REVISIT best to keep hep->hcpriv valid until the endpoint gets
  1632. * disabled, testing for empty qh->ring and avoiding qh setup costs
  1633. * except for the first urb queued after a config change.
  1634. */
  1635. if (qh || ret)
  1636. return ret;
  1637. /* Allocate and initialize qh, minimizing the work done each time
  1638. * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
  1639. *
  1640. * REVISIT consider a dedicated qh kmem_cache, so it's harder
  1641. * for bugs in other kernel code to break this driver...
  1642. */
  1643. qh = kzalloc(sizeof *qh, mem_flags);
  1644. if (!qh) {
  1645. spin_lock_irqsave(&musb->lock, flags);
  1646. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1647. spin_unlock_irqrestore(&musb->lock, flags);
  1648. return -ENOMEM;
  1649. }
  1650. qh->hep = hep;
  1651. qh->dev = urb->dev;
  1652. INIT_LIST_HEAD(&qh->ring);
  1653. qh->is_ready = 1;
  1654. qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize);
  1655. /* no high bandwidth support yet */
  1656. if (qh->maxpacket & ~0x7ff) {
  1657. ret = -EMSGSIZE;
  1658. goto done;
  1659. }
  1660. qh->epnum = usb_endpoint_num(epd);
  1661. qh->type = usb_endpoint_type(epd);
  1662. /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
  1663. qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
  1664. /* precompute rxtype/txtype/type0 register */
  1665. type_reg = (qh->type << 4) | qh->epnum;
  1666. switch (urb->dev->speed) {
  1667. case USB_SPEED_LOW:
  1668. type_reg |= 0xc0;
  1669. break;
  1670. case USB_SPEED_FULL:
  1671. type_reg |= 0x80;
  1672. break;
  1673. default:
  1674. type_reg |= 0x40;
  1675. }
  1676. qh->type_reg = type_reg;
  1677. /* Precompute RXINTERVAL/TXINTERVAL register */
  1678. switch (qh->type) {
  1679. case USB_ENDPOINT_XFER_INT:
  1680. /*
  1681. * Full/low speeds use the linear encoding,
  1682. * high speed uses the logarithmic encoding.
  1683. */
  1684. if (urb->dev->speed <= USB_SPEED_FULL) {
  1685. interval = max_t(u8, epd->bInterval, 1);
  1686. break;
  1687. }
  1688. /* FALLTHROUGH */
  1689. case USB_ENDPOINT_XFER_ISOC:
  1690. /* ISO always uses logarithmic encoding */
  1691. interval = min_t(u8, epd->bInterval, 16);
  1692. break;
  1693. default:
  1694. /* REVISIT we actually want to use NAK limits, hinting to the
  1695. * transfer scheduling logic to try some other qh, e.g. try
  1696. * for 2 msec first:
  1697. *
  1698. * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
  1699. *
  1700. * The downside of disabling this is that transfer scheduling
  1701. * gets VERY unfair for nonperiodic transfers; a misbehaving
  1702. * peripheral could make that hurt. That's perfectly normal
  1703. * for reads from network or serial adapters ... so we have
  1704. * partial NAKlimit support for bulk RX.
  1705. *
  1706. * The upside of disabling it is simpler transfer scheduling.
  1707. */
  1708. interval = 0;
  1709. }
  1710. qh->intv_reg = interval;
  1711. /* precompute addressing for external hub/tt ports */
  1712. if (musb->is_multipoint) {
  1713. struct usb_device *parent = urb->dev->parent;
  1714. if (parent != hcd->self.root_hub) {
  1715. qh->h_addr_reg = (u8) parent->devnum;
  1716. /* set up tt info if needed */
  1717. if (urb->dev->tt) {
  1718. qh->h_port_reg = (u8) urb->dev->ttport;
  1719. if (urb->dev->tt->hub)
  1720. qh->h_addr_reg =
  1721. (u8) urb->dev->tt->hub->devnum;
  1722. if (urb->dev->tt->multi)
  1723. qh->h_addr_reg |= 0x80;
  1724. }
  1725. }
  1726. }
  1727. /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
  1728. * until we get real dma queues (with an entry for each urb/buffer),
  1729. * we only have work to do in the former case.
  1730. */
  1731. spin_lock_irqsave(&musb->lock, flags);
  1732. if (hep->hcpriv) {
  1733. /* some concurrent activity submitted another urb to hep...
  1734. * odd, rare, error prone, but legal.
  1735. */
  1736. kfree(qh);
  1737. ret = 0;
  1738. } else
  1739. ret = musb_schedule(musb, qh,
  1740. epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
  1741. if (ret == 0) {
  1742. urb->hcpriv = qh;
  1743. /* FIXME set urb->start_frame for iso/intr, it's tested in
  1744. * musb_start_urb(), but otherwise only konicawc cares ...
  1745. */
  1746. }
  1747. spin_unlock_irqrestore(&musb->lock, flags);
  1748. done:
  1749. if (ret != 0) {
  1750. spin_lock_irqsave(&musb->lock, flags);
  1751. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1752. spin_unlock_irqrestore(&musb->lock, flags);
  1753. kfree(qh);
  1754. }
  1755. return ret;
  1756. }
  1757. /*
  1758. * abort a transfer that's at the head of a hardware queue.
  1759. * called with controller locked, irqs blocked
  1760. * that hardware queue advances to the next transfer, unless prevented
  1761. */
  1762. static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
  1763. {
  1764. struct musb_hw_ep *ep = qh->hw_ep;
  1765. void __iomem *epio = ep->regs;
  1766. unsigned hw_end = ep->epnum;
  1767. void __iomem *regs = ep->musb->mregs;
  1768. int is_in = usb_pipein(urb->pipe);
  1769. int status = 0;
  1770. u16 csr;
  1771. musb_ep_select(regs, hw_end);
  1772. if (is_dma_capable()) {
  1773. struct dma_channel *dma;
  1774. dma = is_in ? ep->rx_channel : ep->tx_channel;
  1775. if (dma) {
  1776. status = ep->musb->dma_controller->channel_abort(dma);
  1777. DBG(status ? 1 : 3,
  1778. "abort %cX%d DMA for urb %p --> %d\n",
  1779. is_in ? 'R' : 'T', ep->epnum,
  1780. urb, status);
  1781. urb->actual_length += dma->actual_len;
  1782. }
  1783. }
  1784. /* turn off DMA requests, discard state, stop polling ... */
  1785. if (is_in) {
  1786. /* giveback saves bulk toggle */
  1787. csr = musb_h_flush_rxfifo(ep, 0);
  1788. /* REVISIT we still get an irq; should likely clear the
  1789. * endpoint's irq status here to avoid bogus irqs.
  1790. * clearing that status is platform-specific...
  1791. */
  1792. } else if (ep->epnum) {
  1793. musb_h_tx_flush_fifo(ep);
  1794. csr = musb_readw(epio, MUSB_TXCSR);
  1795. csr &= ~(MUSB_TXCSR_AUTOSET
  1796. | MUSB_TXCSR_DMAENAB
  1797. | MUSB_TXCSR_H_RXSTALL
  1798. | MUSB_TXCSR_H_NAKTIMEOUT
  1799. | MUSB_TXCSR_H_ERROR
  1800. | MUSB_TXCSR_TXPKTRDY);
  1801. musb_writew(epio, MUSB_TXCSR, csr);
  1802. /* REVISIT may need to clear FLUSHFIFO ... */
  1803. musb_writew(epio, MUSB_TXCSR, csr);
  1804. /* flush cpu writebuffer */
  1805. csr = musb_readw(epio, MUSB_TXCSR);
  1806. } else {
  1807. musb_h_ep0_flush_fifo(ep);
  1808. }
  1809. if (status == 0)
  1810. musb_advance_schedule(ep->musb, urb, ep, is_in);
  1811. return status;
  1812. }
  1813. static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1814. {
  1815. struct musb *musb = hcd_to_musb(hcd);
  1816. struct musb_qh *qh;
  1817. unsigned long flags;
  1818. int is_in = usb_pipein(urb->pipe);
  1819. int ret;
  1820. DBG(4, "urb=%p, dev%d ep%d%s\n", urb,
  1821. usb_pipedevice(urb->pipe),
  1822. usb_pipeendpoint(urb->pipe),
  1823. is_in ? "in" : "out");
  1824. spin_lock_irqsave(&musb->lock, flags);
  1825. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  1826. if (ret)
  1827. goto done;
  1828. qh = urb->hcpriv;
  1829. if (!qh)
  1830. goto done;
  1831. /*
  1832. * Any URB not actively programmed into endpoint hardware can be
  1833. * immediately given back; that's any URB not at the head of an
  1834. * endpoint queue, unless someday we get real DMA queues. And even
  1835. * if it's at the head, it might not be known to the hardware...
  1836. *
  1837. * Otherwise abort current transfer, pending DMA, etc.; urb->status
  1838. * has already been updated. This is a synchronous abort; it'd be
  1839. * OK to hold off until after some IRQ, though.
  1840. *
  1841. * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
  1842. */
  1843. if (!qh->is_ready
  1844. || urb->urb_list.prev != &qh->hep->urb_list
  1845. || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
  1846. int ready = qh->is_ready;
  1847. qh->is_ready = 0;
  1848. musb_giveback(musb, urb, 0);
  1849. qh->is_ready = ready;
  1850. /* If nothing else (usually musb_giveback) is using it
  1851. * and its URB list has emptied, recycle this qh.
  1852. */
  1853. if (ready && list_empty(&qh->hep->urb_list)) {
  1854. qh->hep->hcpriv = NULL;
  1855. list_del(&qh->ring);
  1856. kfree(qh);
  1857. }
  1858. } else
  1859. ret = musb_cleanup_urb(urb, qh);
  1860. done:
  1861. spin_unlock_irqrestore(&musb->lock, flags);
  1862. return ret;
  1863. }
  1864. /* disable an endpoint */
  1865. static void
  1866. musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
  1867. {
  1868. u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
  1869. unsigned long flags;
  1870. struct musb *musb = hcd_to_musb(hcd);
  1871. struct musb_qh *qh;
  1872. struct urb *urb;
  1873. spin_lock_irqsave(&musb->lock, flags);
  1874. qh = hep->hcpriv;
  1875. if (qh == NULL)
  1876. goto exit;
  1877. /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
  1878. /* Kick the first URB off the hardware, if needed */
  1879. qh->is_ready = 0;
  1880. if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
  1881. urb = next_urb(qh);
  1882. /* make software (then hardware) stop ASAP */
  1883. if (!urb->unlinked)
  1884. urb->status = -ESHUTDOWN;
  1885. /* cleanup */
  1886. musb_cleanup_urb(urb, qh);
  1887. /* Then nuke all the others ... and advance the
  1888. * queue on hw_ep (e.g. bulk ring) when we're done.
  1889. */
  1890. while (!list_empty(&hep->urb_list)) {
  1891. urb = next_urb(qh);
  1892. urb->status = -ESHUTDOWN;
  1893. musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
  1894. }
  1895. } else {
  1896. /* Just empty the queue; the hardware is busy with
  1897. * other transfers, and since !qh->is_ready nothing
  1898. * will activate any of these as it advances.
  1899. */
  1900. while (!list_empty(&hep->urb_list))
  1901. musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
  1902. hep->hcpriv = NULL;
  1903. list_del(&qh->ring);
  1904. kfree(qh);
  1905. }
  1906. exit:
  1907. spin_unlock_irqrestore(&musb->lock, flags);
  1908. }
  1909. static int musb_h_get_frame_number(struct usb_hcd *hcd)
  1910. {
  1911. struct musb *musb = hcd_to_musb(hcd);
  1912. return musb_readw(musb->mregs, MUSB_FRAME);
  1913. }
  1914. static int musb_h_start(struct usb_hcd *hcd)
  1915. {
  1916. struct musb *musb = hcd_to_musb(hcd);
  1917. /* NOTE: musb_start() is called when the hub driver turns
  1918. * on port power, or when (OTG) peripheral starts.
  1919. */
  1920. hcd->state = HC_STATE_RUNNING;
  1921. musb->port1_status = 0;
  1922. return 0;
  1923. }
  1924. static void musb_h_stop(struct usb_hcd *hcd)
  1925. {
  1926. musb_stop(hcd_to_musb(hcd));
  1927. hcd->state = HC_STATE_HALT;
  1928. }
  1929. static int musb_bus_suspend(struct usb_hcd *hcd)
  1930. {
  1931. struct musb *musb = hcd_to_musb(hcd);
  1932. if (musb->xceiv->state == OTG_STATE_A_SUSPEND)
  1933. return 0;
  1934. if (is_host_active(musb) && musb->is_active) {
  1935. WARNING("trying to suspend as %s is_active=%i\n",
  1936. otg_state_string(musb), musb->is_active);
  1937. return -EBUSY;
  1938. } else
  1939. return 0;
  1940. }
  1941. static int musb_bus_resume(struct usb_hcd *hcd)
  1942. {
  1943. /* resuming child port does the work */
  1944. return 0;
  1945. }
  1946. const struct hc_driver musb_hc_driver = {
  1947. .description = "musb-hcd",
  1948. .product_desc = "MUSB HDRC host driver",
  1949. .hcd_priv_size = sizeof(struct musb),
  1950. .flags = HCD_USB2 | HCD_MEMORY,
  1951. /* not using irq handler or reset hooks from usbcore, since
  1952. * those must be shared with peripheral code for OTG configs
  1953. */
  1954. .start = musb_h_start,
  1955. .stop = musb_h_stop,
  1956. .get_frame_number = musb_h_get_frame_number,
  1957. .urb_enqueue = musb_urb_enqueue,
  1958. .urb_dequeue = musb_urb_dequeue,
  1959. .endpoint_disable = musb_h_disable,
  1960. .hub_status_data = musb_hub_status_data,
  1961. .hub_control = musb_hub_control,
  1962. .bus_suspend = musb_bus_suspend,
  1963. .bus_resume = musb_bus_resume,
  1964. /* .start_port_reset = NULL, */
  1965. /* .hub_irq_enable = NULL, */
  1966. };