musb_host.c 72 KB

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