musb_host.c 74 KB

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