musb_host.c 75 KB

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