musb_host.c 64 KB

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