musb_host.c 60 KB

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