musb_host.c 74 KB

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