musb_host.c 72 KB

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