musb_host.c 64 KB

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