musb_host.c 68 KB

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