musb_host.c 59 KB

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