musb_host.c 57 KB

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