musb_host.c 64 KB

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