musb_host.c 76 KB

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