musb_host.c 58 KB

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