musb_host.c 58 KB

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