musb_host.c 78 KB

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