musb_host.c 73 KB

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