musb_host.c 57 KB

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