musb_host.c 77 KB

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