musb_host.c 59 KB

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