musb_host.c 60 KB

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