musb_host.c 75 KB

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