musb_host.c 66 KB

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