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