musb_host.c 76 KB

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