musb_host.c 59 KB

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