cppi_dma.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /*
  2. * Copyright (C) 2005-2006 by Texas Instruments
  3. *
  4. * This file implements a DMA interface using TI's CPPI DMA.
  5. * For now it's DaVinci-only, but CPPI isn't specific to DaVinci or USB.
  6. * The TUSB6020, using VLYNQ, has CPPI that looks much like DaVinci.
  7. */
  8. #include <linux/platform_device.h>
  9. #include <linux/slab.h>
  10. #include <linux/usb.h>
  11. #include "musb_core.h"
  12. #include "musb_debug.h"
  13. #include "cppi_dma.h"
  14. /* CPPI DMA status 7-mar-2006:
  15. *
  16. * - See musb_{host,gadget}.c for more info
  17. *
  18. * - Correct RX DMA generally forces the engine into irq-per-packet mode,
  19. * which can easily saturate the CPU under non-mass-storage loads.
  20. *
  21. * NOTES 24-aug-2006 (2.6.18-rc4):
  22. *
  23. * - peripheral RXDMA wedged in a test with packets of length 512/512/1.
  24. * evidently after the 1 byte packet was received and acked, the queue
  25. * of BDs got garbaged so it wouldn't empty the fifo. (rxcsr 0x2003,
  26. * and RX DMA0: 4 left, 80000000 8feff880, 8feff860 8feff860; 8f321401
  27. * 004001ff 00000001 .. 8feff860) Host was just getting NAKed on tx
  28. * of its next (512 byte) packet. IRQ issues?
  29. *
  30. * REVISIT: the "transfer DMA" glue between CPPI and USB fifos will
  31. * evidently also directly update the RX and TX CSRs ... so audit all
  32. * host and peripheral side DMA code to avoid CSR access after DMA has
  33. * been started.
  34. */
  35. /* REVISIT now we can avoid preallocating these descriptors; or
  36. * more simply, switch to a global freelist not per-channel ones.
  37. * Note: at full speed, 64 descriptors == 4K bulk data.
  38. */
  39. #define NUM_TXCHAN_BD 64
  40. #define NUM_RXCHAN_BD 64
  41. static inline void cpu_drain_writebuffer(void)
  42. {
  43. wmb();
  44. #ifdef CONFIG_CPU_ARM926T
  45. /* REVISIT this "should not be needed",
  46. * but lack of it sure seemed to hurt ...
  47. */
  48. asm("mcr p15, 0, r0, c7, c10, 4 @ drain write buffer\n");
  49. #endif
  50. }
  51. static inline struct cppi_descriptor *cppi_bd_alloc(struct cppi_channel *c)
  52. {
  53. struct cppi_descriptor *bd = c->freelist;
  54. if (bd)
  55. c->freelist = bd->next;
  56. return bd;
  57. }
  58. static inline void
  59. cppi_bd_free(struct cppi_channel *c, struct cppi_descriptor *bd)
  60. {
  61. if (!bd)
  62. return;
  63. bd->next = c->freelist;
  64. c->freelist = bd;
  65. }
  66. /*
  67. * Start DMA controller
  68. *
  69. * Initialize the DMA controller as necessary.
  70. */
  71. /* zero out entire rx state RAM entry for the channel */
  72. static void cppi_reset_rx(struct cppi_rx_stateram __iomem *rx)
  73. {
  74. musb_writel(&rx->rx_skipbytes, 0, 0);
  75. musb_writel(&rx->rx_head, 0, 0);
  76. musb_writel(&rx->rx_sop, 0, 0);
  77. musb_writel(&rx->rx_current, 0, 0);
  78. musb_writel(&rx->rx_buf_current, 0, 0);
  79. musb_writel(&rx->rx_len_len, 0, 0);
  80. musb_writel(&rx->rx_cnt_cnt, 0, 0);
  81. }
  82. /* zero out entire tx state RAM entry for the channel */
  83. static void cppi_reset_tx(struct cppi_tx_stateram __iomem *tx, u32 ptr)
  84. {
  85. musb_writel(&tx->tx_head, 0, 0);
  86. musb_writel(&tx->tx_buf, 0, 0);
  87. musb_writel(&tx->tx_current, 0, 0);
  88. musb_writel(&tx->tx_buf_current, 0, 0);
  89. musb_writel(&tx->tx_info, 0, 0);
  90. musb_writel(&tx->tx_rem_len, 0, 0);
  91. /* musb_writel(&tx->tx_dummy, 0, 0); */
  92. musb_writel(&tx->tx_complete, 0, ptr);
  93. }
  94. static void __init cppi_pool_init(struct cppi *cppi, struct cppi_channel *c)
  95. {
  96. int j;
  97. /* initialize channel fields */
  98. c->head = NULL;
  99. c->tail = NULL;
  100. c->last_processed = NULL;
  101. c->channel.status = MUSB_DMA_STATUS_UNKNOWN;
  102. c->controller = cppi;
  103. c->is_rndis = 0;
  104. c->freelist = NULL;
  105. /* build the BD Free list for the channel */
  106. for (j = 0; j < NUM_TXCHAN_BD + 1; j++) {
  107. struct cppi_descriptor *bd;
  108. dma_addr_t dma;
  109. bd = dma_pool_alloc(cppi->pool, GFP_KERNEL, &dma);
  110. bd->dma = dma;
  111. cppi_bd_free(c, bd);
  112. }
  113. }
  114. static int cppi_channel_abort(struct dma_channel *);
  115. static void cppi_pool_free(struct cppi_channel *c)
  116. {
  117. struct cppi *cppi = c->controller;
  118. struct cppi_descriptor *bd;
  119. (void) cppi_channel_abort(&c->channel);
  120. c->channel.status = MUSB_DMA_STATUS_UNKNOWN;
  121. c->controller = NULL;
  122. /* free all its bds */
  123. bd = c->last_processed;
  124. do {
  125. if (bd)
  126. dma_pool_free(cppi->pool, bd, bd->dma);
  127. bd = cppi_bd_alloc(c);
  128. } while (bd);
  129. c->last_processed = NULL;
  130. }
  131. static int __init cppi_controller_start(struct dma_controller *c)
  132. {
  133. struct cppi *controller;
  134. void __iomem *tibase;
  135. int i;
  136. controller = container_of(c, struct cppi, controller);
  137. /* do whatever is necessary to start controller */
  138. for (i = 0; i < ARRAY_SIZE(controller->tx); i++) {
  139. controller->tx[i].transmit = true;
  140. controller->tx[i].index = i;
  141. }
  142. for (i = 0; i < ARRAY_SIZE(controller->rx); i++) {
  143. controller->rx[i].transmit = false;
  144. controller->rx[i].index = i;
  145. }
  146. /* setup BD list on a per channel basis */
  147. for (i = 0; i < ARRAY_SIZE(controller->tx); i++)
  148. cppi_pool_init(controller, controller->tx + i);
  149. for (i = 0; i < ARRAY_SIZE(controller->rx); i++)
  150. cppi_pool_init(controller, controller->rx + i);
  151. tibase = controller->tibase;
  152. INIT_LIST_HEAD(&controller->tx_complete);
  153. /* initialise tx/rx channel head pointers to zero */
  154. for (i = 0; i < ARRAY_SIZE(controller->tx); i++) {
  155. struct cppi_channel *tx_ch = controller->tx + i;
  156. struct cppi_tx_stateram __iomem *tx;
  157. INIT_LIST_HEAD(&tx_ch->tx_complete);
  158. tx = tibase + DAVINCI_TXCPPI_STATERAM_OFFSET(i);
  159. tx_ch->state_ram = tx;
  160. cppi_reset_tx(tx, 0);
  161. }
  162. for (i = 0; i < ARRAY_SIZE(controller->rx); i++) {
  163. struct cppi_channel *rx_ch = controller->rx + i;
  164. struct cppi_rx_stateram __iomem *rx;
  165. INIT_LIST_HEAD(&rx_ch->tx_complete);
  166. rx = tibase + DAVINCI_RXCPPI_STATERAM_OFFSET(i);
  167. rx_ch->state_ram = rx;
  168. cppi_reset_rx(rx);
  169. }
  170. /* enable individual cppi channels */
  171. musb_writel(tibase, DAVINCI_TXCPPI_INTENAB_REG,
  172. DAVINCI_DMA_ALL_CHANNELS_ENABLE);
  173. musb_writel(tibase, DAVINCI_RXCPPI_INTENAB_REG,
  174. DAVINCI_DMA_ALL_CHANNELS_ENABLE);
  175. /* enable tx/rx CPPI control */
  176. musb_writel(tibase, DAVINCI_TXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_ENABLE);
  177. musb_writel(tibase, DAVINCI_RXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_ENABLE);
  178. /* disable RNDIS mode, also host rx RNDIS autorequest */
  179. musb_writel(tibase, DAVINCI_RNDIS_REG, 0);
  180. musb_writel(tibase, DAVINCI_AUTOREQ_REG, 0);
  181. return 0;
  182. }
  183. /*
  184. * Stop DMA controller
  185. *
  186. * De-Init the DMA controller as necessary.
  187. */
  188. static int cppi_controller_stop(struct dma_controller *c)
  189. {
  190. struct cppi *controller;
  191. void __iomem *tibase;
  192. int i;
  193. struct musb *musb;
  194. controller = container_of(c, struct cppi, controller);
  195. musb = controller->musb;
  196. tibase = controller->tibase;
  197. /* DISABLE INDIVIDUAL CHANNEL Interrupts */
  198. musb_writel(tibase, DAVINCI_TXCPPI_INTCLR_REG,
  199. DAVINCI_DMA_ALL_CHANNELS_ENABLE);
  200. musb_writel(tibase, DAVINCI_RXCPPI_INTCLR_REG,
  201. DAVINCI_DMA_ALL_CHANNELS_ENABLE);
  202. dev_dbg(musb->controller, "Tearing down RX and TX Channels\n");
  203. for (i = 0; i < ARRAY_SIZE(controller->tx); i++) {
  204. /* FIXME restructure of txdma to use bds like rxdma */
  205. controller->tx[i].last_processed = NULL;
  206. cppi_pool_free(controller->tx + i);
  207. }
  208. for (i = 0; i < ARRAY_SIZE(controller->rx); i++)
  209. cppi_pool_free(controller->rx + i);
  210. /* in Tx Case proper teardown is supported. We resort to disabling
  211. * Tx/Rx CPPI after cleanup of Tx channels. Before TX teardown is
  212. * complete TX CPPI cannot be disabled.
  213. */
  214. /*disable tx/rx cppi */
  215. musb_writel(tibase, DAVINCI_TXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_DISABLE);
  216. musb_writel(tibase, DAVINCI_RXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_DISABLE);
  217. return 0;
  218. }
  219. /* While dma channel is allocated, we only want the core irqs active
  220. * for fault reports, otherwise we'd get irqs that we don't care about.
  221. * Except for TX irqs, where dma done != fifo empty and reusable ...
  222. *
  223. * NOTE: docs don't say either way, but irq masking **enables** irqs.
  224. *
  225. * REVISIT same issue applies to pure PIO usage too, and non-cppi dma...
  226. */
  227. static inline void core_rxirq_disable(void __iomem *tibase, unsigned epnum)
  228. {
  229. musb_writel(tibase, DAVINCI_USB_INT_MASK_CLR_REG, 1 << (epnum + 8));
  230. }
  231. static inline void core_rxirq_enable(void __iomem *tibase, unsigned epnum)
  232. {
  233. musb_writel(tibase, DAVINCI_USB_INT_MASK_SET_REG, 1 << (epnum + 8));
  234. }
  235. /*
  236. * Allocate a CPPI Channel for DMA. With CPPI, channels are bound to
  237. * each transfer direction of a non-control endpoint, so allocating
  238. * (and deallocating) is mostly a way to notice bad housekeeping on
  239. * the software side. We assume the irqs are always active.
  240. */
  241. static struct dma_channel *
  242. cppi_channel_allocate(struct dma_controller *c,
  243. struct musb_hw_ep *ep, u8 transmit)
  244. {
  245. struct cppi *controller;
  246. u8 index;
  247. struct cppi_channel *cppi_ch;
  248. void __iomem *tibase;
  249. struct musb *musb;
  250. controller = container_of(c, struct cppi, controller);
  251. tibase = controller->tibase;
  252. musb = controller->musb;
  253. /* ep0 doesn't use DMA; remember cppi indices are 0..N-1 */
  254. index = ep->epnum - 1;
  255. /* return the corresponding CPPI Channel Handle, and
  256. * probably disable the non-CPPI irq until we need it.
  257. */
  258. if (transmit) {
  259. if (index >= ARRAY_SIZE(controller->tx)) {
  260. dev_dbg(musb->controller, "no %cX%d CPPI channel\n", 'T', index);
  261. return NULL;
  262. }
  263. cppi_ch = controller->tx + index;
  264. } else {
  265. if (index >= ARRAY_SIZE(controller->rx)) {
  266. dev_dbg(musb->controller, "no %cX%d CPPI channel\n", 'R', index);
  267. return NULL;
  268. }
  269. cppi_ch = controller->rx + index;
  270. core_rxirq_disable(tibase, ep->epnum);
  271. }
  272. /* REVISIT make this an error later once the same driver code works
  273. * with the other DMA engine too
  274. */
  275. if (cppi_ch->hw_ep)
  276. dev_dbg(musb->controller, "re-allocating DMA%d %cX channel %p\n",
  277. index, transmit ? 'T' : 'R', cppi_ch);
  278. cppi_ch->hw_ep = ep;
  279. cppi_ch->channel.status = MUSB_DMA_STATUS_FREE;
  280. cppi_ch->channel.max_len = 0x7fffffff;
  281. dev_dbg(musb->controller, "Allocate CPPI%d %cX\n", index, transmit ? 'T' : 'R');
  282. return &cppi_ch->channel;
  283. }
  284. /* Release a CPPI Channel. */
  285. static void cppi_channel_release(struct dma_channel *channel)
  286. {
  287. struct cppi_channel *c;
  288. void __iomem *tibase;
  289. /* REVISIT: for paranoia, check state and abort if needed... */
  290. c = container_of(channel, struct cppi_channel, channel);
  291. tibase = c->controller->tibase;
  292. if (!c->hw_ep)
  293. dev_dbg(c->controller->musb->controller,
  294. "releasing idle DMA channel %p\n", c);
  295. else if (!c->transmit)
  296. core_rxirq_enable(tibase, c->index + 1);
  297. /* for now, leave its cppi IRQ enabled (we won't trigger it) */
  298. c->hw_ep = NULL;
  299. channel->status = MUSB_DMA_STATUS_UNKNOWN;
  300. }
  301. /* Context: controller irqlocked */
  302. static void
  303. cppi_dump_rx(int level, struct cppi_channel *c, const char *tag)
  304. {
  305. void __iomem *base = c->controller->mregs;
  306. struct cppi_rx_stateram __iomem *rx = c->state_ram;
  307. musb_ep_select(base, c->index + 1);
  308. dev_dbg(c->controller->musb->controller,
  309. "RX DMA%d%s: %d left, csr %04x, "
  310. "%08x H%08x S%08x C%08x, "
  311. "B%08x L%08x %08x .. %08x"
  312. "\n",
  313. c->index, tag,
  314. musb_readl(c->controller->tibase,
  315. DAVINCI_RXCPPI_BUFCNT0_REG + 4 * c->index),
  316. musb_readw(c->hw_ep->regs, MUSB_RXCSR),
  317. musb_readl(&rx->rx_skipbytes, 0),
  318. musb_readl(&rx->rx_head, 0),
  319. musb_readl(&rx->rx_sop, 0),
  320. musb_readl(&rx->rx_current, 0),
  321. musb_readl(&rx->rx_buf_current, 0),
  322. musb_readl(&rx->rx_len_len, 0),
  323. musb_readl(&rx->rx_cnt_cnt, 0),
  324. musb_readl(&rx->rx_complete, 0)
  325. );
  326. }
  327. /* Context: controller irqlocked */
  328. static void
  329. cppi_dump_tx(int level, struct cppi_channel *c, const char *tag)
  330. {
  331. void __iomem *base = c->controller->mregs;
  332. struct cppi_tx_stateram __iomem *tx = c->state_ram;
  333. musb_ep_select(base, c->index + 1);
  334. dev_dbg(c->controller->musb->controller,
  335. "TX DMA%d%s: csr %04x, "
  336. "H%08x S%08x C%08x %08x, "
  337. "F%08x L%08x .. %08x"
  338. "\n",
  339. c->index, tag,
  340. musb_readw(c->hw_ep->regs, MUSB_TXCSR),
  341. musb_readl(&tx->tx_head, 0),
  342. musb_readl(&tx->tx_buf, 0),
  343. musb_readl(&tx->tx_current, 0),
  344. musb_readl(&tx->tx_buf_current, 0),
  345. musb_readl(&tx->tx_info, 0),
  346. musb_readl(&tx->tx_rem_len, 0),
  347. /* dummy/unused word 6 */
  348. musb_readl(&tx->tx_complete, 0)
  349. );
  350. }
  351. /* Context: controller irqlocked */
  352. static inline void
  353. cppi_rndis_update(struct cppi_channel *c, int is_rx,
  354. void __iomem *tibase, int is_rndis)
  355. {
  356. /* we may need to change the rndis flag for this cppi channel */
  357. if (c->is_rndis != is_rndis) {
  358. u32 value = musb_readl(tibase, DAVINCI_RNDIS_REG);
  359. u32 temp = 1 << (c->index);
  360. if (is_rx)
  361. temp <<= 16;
  362. if (is_rndis)
  363. value |= temp;
  364. else
  365. value &= ~temp;
  366. musb_writel(tibase, DAVINCI_RNDIS_REG, value);
  367. c->is_rndis = is_rndis;
  368. }
  369. }
  370. #ifdef CONFIG_USB_MUSB_DEBUG
  371. static void cppi_dump_rxbd(const char *tag, struct cppi_descriptor *bd)
  372. {
  373. pr_debug("RXBD/%s %08x: "
  374. "nxt %08x buf %08x off.blen %08x opt.plen %08x\n",
  375. tag, bd->dma,
  376. bd->hw_next, bd->hw_bufp, bd->hw_off_len,
  377. bd->hw_options);
  378. }
  379. #endif
  380. static void cppi_dump_rxq(int level, const char *tag, struct cppi_channel *rx)
  381. {
  382. #ifdef CONFIG_USB_MUSB_DEBUG
  383. struct cppi_descriptor *bd;
  384. if (!_dbg_level(level))
  385. return;
  386. cppi_dump_rx(level, rx, tag);
  387. if (rx->last_processed)
  388. cppi_dump_rxbd("last", rx->last_processed);
  389. for (bd = rx->head; bd; bd = bd->next)
  390. cppi_dump_rxbd("active", bd);
  391. #endif
  392. }
  393. /* NOTE: DaVinci autoreq is ignored except for host side "RNDIS" mode RX;
  394. * so we won't ever use it (see "CPPI RX Woes" below).
  395. */
  396. static inline int cppi_autoreq_update(struct cppi_channel *rx,
  397. void __iomem *tibase, int onepacket, unsigned n_bds)
  398. {
  399. u32 val;
  400. #ifdef RNDIS_RX_IS_USABLE
  401. u32 tmp;
  402. /* assert(is_host_active(musb)) */
  403. /* start from "AutoReq never" */
  404. tmp = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
  405. val = tmp & ~((0x3) << (rx->index * 2));
  406. /* HCD arranged reqpkt for packet #1. we arrange int
  407. * for all but the last one, maybe in two segments.
  408. */
  409. if (!onepacket) {
  410. #if 0
  411. /* use two segments, autoreq "all" then the last "never" */
  412. val |= ((0x3) << (rx->index * 2));
  413. n_bds--;
  414. #else
  415. /* one segment, autoreq "all-but-last" */
  416. val |= ((0x1) << (rx->index * 2));
  417. #endif
  418. }
  419. if (val != tmp) {
  420. int n = 100;
  421. /* make sure that autoreq is updated before continuing */
  422. musb_writel(tibase, DAVINCI_AUTOREQ_REG, val);
  423. do {
  424. tmp = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
  425. if (tmp == val)
  426. break;
  427. cpu_relax();
  428. } while (n-- > 0);
  429. }
  430. #endif
  431. /* REQPKT is turned off after each segment */
  432. if (n_bds && rx->channel.actual_len) {
  433. void __iomem *regs = rx->hw_ep->regs;
  434. val = musb_readw(regs, MUSB_RXCSR);
  435. if (!(val & MUSB_RXCSR_H_REQPKT)) {
  436. val |= MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_H_WZC_BITS;
  437. musb_writew(regs, MUSB_RXCSR, val);
  438. /* flush writebufer */
  439. val = musb_readw(regs, MUSB_RXCSR);
  440. }
  441. }
  442. return n_bds;
  443. }
  444. /* Buffer enqueuing Logic:
  445. *
  446. * - RX builds new queues each time, to help handle routine "early
  447. * termination" cases (faults, including errors and short reads)
  448. * more correctly.
  449. *
  450. * - for now, TX reuses the same queue of BDs every time
  451. *
  452. * REVISIT long term, we want a normal dynamic model.
  453. * ... the goal will be to append to the
  454. * existing queue, processing completed "dma buffers" (segments) on the fly.
  455. *
  456. * Otherwise we force an IRQ latency between requests, which slows us a lot
  457. * (especially in "transparent" dma). Unfortunately that model seems to be
  458. * inherent in the DMA model from the Mentor code, except in the rare case
  459. * of transfers big enough (~128+ KB) that we could append "middle" segments
  460. * in the TX paths. (RX can't do this, see below.)
  461. *
  462. * That's true even in the CPPI- friendly iso case, where most urbs have
  463. * several small segments provided in a group and where the "packet at a time"
  464. * "transparent" DMA model is always correct, even on the RX side.
  465. */
  466. /*
  467. * CPPI TX:
  468. * ========
  469. * TX is a lot more reasonable than RX; it doesn't need to run in
  470. * irq-per-packet mode very often. RNDIS mode seems to behave too
  471. * (except how it handles the exactly-N-packets case). Building a
  472. * txdma queue with multiple requests (urb or usb_request) looks
  473. * like it would work ... but fault handling would need much testing.
  474. *
  475. * The main issue with TX mode RNDIS relates to transfer lengths that
  476. * are an exact multiple of the packet length. It appears that there's
  477. * a hiccup in that case (maybe the DMA completes before the ZLP gets
  478. * written?) boiling down to not being able to rely on CPPI writing any
  479. * terminating zero length packet before the next transfer is written.
  480. * So that's punted to PIO; better yet, gadget drivers can avoid it.
  481. *
  482. * Plus, there's allegedly an undocumented constraint that rndis transfer
  483. * length be a multiple of 64 bytes ... but the chip doesn't act that
  484. * way, and we really don't _want_ that behavior anyway.
  485. *
  486. * On TX, "transparent" mode works ... although experiments have shown
  487. * problems trying to use the SOP/EOP bits in different USB packets.
  488. *
  489. * REVISIT try to handle terminating zero length packets using CPPI
  490. * instead of doing it by PIO after an IRQ. (Meanwhile, make Ethernet
  491. * links avoid that issue by forcing them to avoid zlps.)
  492. */
  493. static void
  494. cppi_next_tx_segment(struct musb *musb, struct cppi_channel *tx)
  495. {
  496. unsigned maxpacket = tx->maxpacket;
  497. dma_addr_t addr = tx->buf_dma + tx->offset;
  498. size_t length = tx->buf_len - tx->offset;
  499. struct cppi_descriptor *bd;
  500. unsigned n_bds;
  501. unsigned i;
  502. struct cppi_tx_stateram __iomem *tx_ram = tx->state_ram;
  503. int rndis;
  504. /* TX can use the CPPI "rndis" mode, where we can probably fit this
  505. * transfer in one BD and one IRQ. The only time we would NOT want
  506. * to use it is when hardware constraints prevent it, or if we'd
  507. * trigger the "send a ZLP?" confusion.
  508. */
  509. rndis = (maxpacket & 0x3f) == 0
  510. && length > maxpacket
  511. && length < 0xffff
  512. && (length % maxpacket) != 0;
  513. if (rndis) {
  514. maxpacket = length;
  515. n_bds = 1;
  516. } else {
  517. n_bds = length / maxpacket;
  518. if (!length || (length % maxpacket))
  519. n_bds++;
  520. n_bds = min(n_bds, (unsigned) NUM_TXCHAN_BD);
  521. length = min(n_bds * maxpacket, length);
  522. }
  523. dev_dbg(musb->controller, "TX DMA%d, pktSz %d %s bds %d dma 0x%llx len %u\n",
  524. tx->index,
  525. maxpacket,
  526. rndis ? "rndis" : "transparent",
  527. n_bds,
  528. (unsigned long long)addr, length);
  529. cppi_rndis_update(tx, 0, musb->ctrl_base, rndis);
  530. /* assuming here that channel_program is called during
  531. * transfer initiation ... current code maintains state
  532. * for one outstanding request only (no queues, not even
  533. * the implicit ones of an iso urb).
  534. */
  535. bd = tx->freelist;
  536. tx->head = bd;
  537. tx->last_processed = NULL;
  538. /* FIXME use BD pool like RX side does, and just queue
  539. * the minimum number for this request.
  540. */
  541. /* Prepare queue of BDs first, then hand it to hardware.
  542. * All BDs except maybe the last should be of full packet
  543. * size; for RNDIS there _is_ only that last packet.
  544. */
  545. for (i = 0; i < n_bds; ) {
  546. if (++i < n_bds && bd->next)
  547. bd->hw_next = bd->next->dma;
  548. else
  549. bd->hw_next = 0;
  550. bd->hw_bufp = tx->buf_dma + tx->offset;
  551. /* FIXME set EOP only on the last packet,
  552. * SOP only on the first ... avoid IRQs
  553. */
  554. if ((tx->offset + maxpacket) <= tx->buf_len) {
  555. tx->offset += maxpacket;
  556. bd->hw_off_len = maxpacket;
  557. bd->hw_options = CPPI_SOP_SET | CPPI_EOP_SET
  558. | CPPI_OWN_SET | maxpacket;
  559. } else {
  560. /* only this one may be a partial USB Packet */
  561. u32 partial_len;
  562. partial_len = tx->buf_len - tx->offset;
  563. tx->offset = tx->buf_len;
  564. bd->hw_off_len = partial_len;
  565. bd->hw_options = CPPI_SOP_SET | CPPI_EOP_SET
  566. | CPPI_OWN_SET | partial_len;
  567. if (partial_len == 0)
  568. bd->hw_options |= CPPI_ZERO_SET;
  569. }
  570. dev_dbg(musb->controller, "TXBD %p: nxt %08x buf %08x len %04x opt %08x\n",
  571. bd, bd->hw_next, bd->hw_bufp,
  572. bd->hw_off_len, bd->hw_options);
  573. /* update the last BD enqueued to the list */
  574. tx->tail = bd;
  575. bd = bd->next;
  576. }
  577. /* BDs live in DMA-coherent memory, but writes might be pending */
  578. cpu_drain_writebuffer();
  579. /* Write to the HeadPtr in state RAM to trigger */
  580. musb_writel(&tx_ram->tx_head, 0, (u32)tx->freelist->dma);
  581. cppi_dump_tx(5, tx, "/S");
  582. }
  583. /*
  584. * CPPI RX Woes:
  585. * =============
  586. * Consider a 1KB bulk RX buffer in two scenarios: (a) it's fed two 300 byte
  587. * packets back-to-back, and (b) it's fed two 512 byte packets back-to-back.
  588. * (Full speed transfers have similar scenarios.)
  589. *
  590. * The correct behavior for Linux is that (a) fills the buffer with 300 bytes,
  591. * and the next packet goes into a buffer that's queued later; while (b) fills
  592. * the buffer with 1024 bytes. How to do that with CPPI?
  593. *
  594. * - RX queues in "rndis" mode -- one single BD -- handle (a) correctly, but
  595. * (b) loses **BADLY** because nothing (!) happens when that second packet
  596. * fills the buffer, much less when a third one arrives. (Which makes this
  597. * not a "true" RNDIS mode. In the RNDIS protocol short-packet termination
  598. * is optional, and it's fine if peripherals -- not hosts! -- pad messages
  599. * out to end-of-buffer. Standard PCI host controller DMA descriptors
  600. * implement that mode by default ... which is no accident.)
  601. *
  602. * - RX queues in "transparent" mode -- two BDs with 512 bytes each -- have
  603. * converse problems: (b) is handled right, but (a) loses badly. CPPI RX
  604. * ignores SOP/EOP markings and processes both of those BDs; so both packets
  605. * are loaded into the buffer (with a 212 byte gap between them), and the next
  606. * buffer queued will NOT get its 300 bytes of data. (It seems like SOP/EOP
  607. * are intended as outputs for RX queues, not inputs...)
  608. *
  609. * - A variant of "transparent" mode -- one BD at a time -- is the only way to
  610. * reliably make both cases work, with software handling both cases correctly
  611. * and at the significant penalty of needing an IRQ per packet. (The lack of
  612. * I/O overlap can be slightly ameliorated by enabling double buffering.)
  613. *
  614. * So how to get rid of IRQ-per-packet? The transparent multi-BD case could
  615. * be used in special cases like mass storage, which sets URB_SHORT_NOT_OK
  616. * (or maybe its peripheral side counterpart) to flag (a) scenarios as errors
  617. * with guaranteed driver level fault recovery and scrubbing out what's left
  618. * of that garbaged datastream.
  619. *
  620. * But there seems to be no way to identify the cases where CPPI RNDIS mode
  621. * is appropriate -- which do NOT include RNDIS host drivers, but do include
  622. * the CDC Ethernet driver! -- and the documentation is incomplete/wrong.
  623. * So we can't _ever_ use RX RNDIS mode ... except by using a heuristic
  624. * that applies best on the peripheral side (and which could fail rudely).
  625. *
  626. * Leaving only "transparent" mode; we avoid multi-bd modes in almost all
  627. * cases other than mass storage class. Otherwise we're correct but slow,
  628. * since CPPI penalizes our need for a "true RNDIS" default mode.
  629. */
  630. /* Heuristic, intended to kick in for ethernet/rndis peripheral ONLY
  631. *
  632. * IFF
  633. * (a) peripheral mode ... since rndis peripherals could pad their
  634. * writes to hosts, causing i/o failure; or we'd have to cope with
  635. * a largely unknowable variety of host side protocol variants
  636. * (b) and short reads are NOT errors ... since full reads would
  637. * cause those same i/o failures
  638. * (c) and read length is
  639. * - less than 64KB (max per cppi descriptor)
  640. * - not a multiple of 4096 (g_zero default, full reads typical)
  641. * - N (>1) packets long, ditto (full reads not EXPECTED)
  642. * THEN
  643. * try rx rndis mode
  644. *
  645. * Cost of heuristic failing: RXDMA wedges at the end of transfers that
  646. * fill out the whole buffer. Buggy host side usb network drivers could
  647. * trigger that, but "in the field" such bugs seem to be all but unknown.
  648. *
  649. * So this module parameter lets the heuristic be disabled. When using
  650. * gadgetfs, the heuristic will probably need to be disabled.
  651. */
  652. static int cppi_rx_rndis = 1;
  653. module_param(cppi_rx_rndis, bool, 0);
  654. MODULE_PARM_DESC(cppi_rx_rndis, "enable/disable RX RNDIS heuristic");
  655. /**
  656. * cppi_next_rx_segment - dma read for the next chunk of a buffer
  657. * @musb: the controller
  658. * @rx: dma channel
  659. * @onepacket: true unless caller treats short reads as errors, and
  660. * performs fault recovery above usbcore.
  661. * Context: controller irqlocked
  662. *
  663. * See above notes about why we can't use multi-BD RX queues except in
  664. * rare cases (mass storage class), and can never use the hardware "rndis"
  665. * mode (since it's not a "true" RNDIS mode) with complete safety..
  666. *
  667. * It's ESSENTIAL that callers specify "onepacket" mode unless they kick in
  668. * code to recover from corrupted datastreams after each short transfer.
  669. */
  670. static void
  671. cppi_next_rx_segment(struct musb *musb, struct cppi_channel *rx, int onepacket)
  672. {
  673. unsigned maxpacket = rx->maxpacket;
  674. dma_addr_t addr = rx->buf_dma + rx->offset;
  675. size_t length = rx->buf_len - rx->offset;
  676. struct cppi_descriptor *bd, *tail;
  677. unsigned n_bds;
  678. unsigned i;
  679. void __iomem *tibase = musb->ctrl_base;
  680. int is_rndis = 0;
  681. struct cppi_rx_stateram __iomem *rx_ram = rx->state_ram;
  682. if (onepacket) {
  683. /* almost every USB driver, host or peripheral side */
  684. n_bds = 1;
  685. /* maybe apply the heuristic above */
  686. if (cppi_rx_rndis
  687. && is_peripheral_active(musb)
  688. && length > maxpacket
  689. && (length & ~0xffff) == 0
  690. && (length & 0x0fff) != 0
  691. && (length & (maxpacket - 1)) == 0) {
  692. maxpacket = length;
  693. is_rndis = 1;
  694. }
  695. } else {
  696. /* virtually nothing except mass storage class */
  697. if (length > 0xffff) {
  698. n_bds = 0xffff / maxpacket;
  699. length = n_bds * maxpacket;
  700. } else {
  701. n_bds = length / maxpacket;
  702. if (length % maxpacket)
  703. n_bds++;
  704. }
  705. if (n_bds == 1)
  706. onepacket = 1;
  707. else
  708. n_bds = min(n_bds, (unsigned) NUM_RXCHAN_BD);
  709. }
  710. /* In host mode, autorequest logic can generate some IN tokens; it's
  711. * tricky since we can't leave REQPKT set in RXCSR after the transfer
  712. * finishes. So: multipacket transfers involve two or more segments.
  713. * And always at least two IRQs ... RNDIS mode is not an option.
  714. */
  715. if (is_host_active(musb))
  716. n_bds = cppi_autoreq_update(rx, tibase, onepacket, n_bds);
  717. cppi_rndis_update(rx, 1, musb->ctrl_base, is_rndis);
  718. length = min(n_bds * maxpacket, length);
  719. dev_dbg(musb->controller, "RX DMA%d seg, maxp %d %s bds %d (cnt %d) "
  720. "dma 0x%llx len %u %u/%u\n",
  721. rx->index, maxpacket,
  722. onepacket
  723. ? (is_rndis ? "rndis" : "onepacket")
  724. : "multipacket",
  725. n_bds,
  726. musb_readl(tibase,
  727. DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
  728. & 0xffff,
  729. (unsigned long long)addr, length,
  730. rx->channel.actual_len, rx->buf_len);
  731. /* only queue one segment at a time, since the hardware prevents
  732. * correct queue shutdown after unexpected short packets
  733. */
  734. bd = cppi_bd_alloc(rx);
  735. rx->head = bd;
  736. /* Build BDs for all packets in this segment */
  737. for (i = 0, tail = NULL; bd && i < n_bds; i++, tail = bd) {
  738. u32 bd_len;
  739. if (i) {
  740. bd = cppi_bd_alloc(rx);
  741. if (!bd)
  742. break;
  743. tail->next = bd;
  744. tail->hw_next = bd->dma;
  745. }
  746. bd->hw_next = 0;
  747. /* all but the last packet will be maxpacket size */
  748. if (maxpacket < length)
  749. bd_len = maxpacket;
  750. else
  751. bd_len = length;
  752. bd->hw_bufp = addr;
  753. addr += bd_len;
  754. rx->offset += bd_len;
  755. bd->hw_off_len = (0 /*offset*/ << 16) + bd_len;
  756. bd->buflen = bd_len;
  757. bd->hw_options = CPPI_OWN_SET | (i == 0 ? length : 0);
  758. length -= bd_len;
  759. }
  760. /* we always expect at least one reusable BD! */
  761. if (!tail) {
  762. WARNING("rx dma%d -- no BDs? need %d\n", rx->index, n_bds);
  763. return;
  764. } else if (i < n_bds)
  765. WARNING("rx dma%d -- only %d of %d BDs\n", rx->index, i, n_bds);
  766. tail->next = NULL;
  767. tail->hw_next = 0;
  768. bd = rx->head;
  769. rx->tail = tail;
  770. /* short reads and other faults should terminate this entire
  771. * dma segment. we want one "dma packet" per dma segment, not
  772. * one per USB packet, terminating the whole queue at once...
  773. * NOTE that current hardware seems to ignore SOP and EOP.
  774. */
  775. bd->hw_options |= CPPI_SOP_SET;
  776. tail->hw_options |= CPPI_EOP_SET;
  777. #ifdef CONFIG_USB_MUSB_DEBUG
  778. if (_dbg_level(5)) {
  779. struct cppi_descriptor *d;
  780. for (d = rx->head; d; d = d->next)
  781. cppi_dump_rxbd("S", d);
  782. }
  783. #endif
  784. /* in case the preceding transfer left some state... */
  785. tail = rx->last_processed;
  786. if (tail) {
  787. tail->next = bd;
  788. tail->hw_next = bd->dma;
  789. }
  790. core_rxirq_enable(tibase, rx->index + 1);
  791. /* BDs live in DMA-coherent memory, but writes might be pending */
  792. cpu_drain_writebuffer();
  793. /* REVISIT specs say to write this AFTER the BUFCNT register
  794. * below ... but that loses badly.
  795. */
  796. musb_writel(&rx_ram->rx_head, 0, bd->dma);
  797. /* bufferCount must be at least 3, and zeroes on completion
  798. * unless it underflows below zero, or stops at two, or keeps
  799. * growing ... grr.
  800. */
  801. i = musb_readl(tibase,
  802. DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
  803. & 0xffff;
  804. if (!i)
  805. musb_writel(tibase,
  806. DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
  807. n_bds + 2);
  808. else if (n_bds > (i - 3))
  809. musb_writel(tibase,
  810. DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
  811. n_bds - (i - 3));
  812. i = musb_readl(tibase,
  813. DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
  814. & 0xffff;
  815. if (i < (2 + n_bds)) {
  816. dev_dbg(musb->controller, "bufcnt%d underrun - %d (for %d)\n",
  817. rx->index, i, n_bds);
  818. musb_writel(tibase,
  819. DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
  820. n_bds + 2);
  821. }
  822. cppi_dump_rx(4, rx, "/S");
  823. }
  824. /**
  825. * cppi_channel_program - program channel for data transfer
  826. * @ch: the channel
  827. * @maxpacket: max packet size
  828. * @mode: For RX, 1 unless the usb protocol driver promised to treat
  829. * all short reads as errors and kick in high level fault recovery.
  830. * For TX, ignored because of RNDIS mode races/glitches.
  831. * @dma_addr: dma address of buffer
  832. * @len: length of buffer
  833. * Context: controller irqlocked
  834. */
  835. static int cppi_channel_program(struct dma_channel *ch,
  836. u16 maxpacket, u8 mode,
  837. dma_addr_t dma_addr, u32 len)
  838. {
  839. struct cppi_channel *cppi_ch;
  840. struct cppi *controller;
  841. struct musb *musb;
  842. cppi_ch = container_of(ch, struct cppi_channel, channel);
  843. controller = cppi_ch->controller;
  844. musb = controller->musb;
  845. switch (ch->status) {
  846. case MUSB_DMA_STATUS_BUS_ABORT:
  847. case MUSB_DMA_STATUS_CORE_ABORT:
  848. /* fault irq handler should have handled cleanup */
  849. WARNING("%cX DMA%d not cleaned up after abort!\n",
  850. cppi_ch->transmit ? 'T' : 'R',
  851. cppi_ch->index);
  852. /* WARN_ON(1); */
  853. break;
  854. case MUSB_DMA_STATUS_BUSY:
  855. WARNING("program active channel? %cX DMA%d\n",
  856. cppi_ch->transmit ? 'T' : 'R',
  857. cppi_ch->index);
  858. /* WARN_ON(1); */
  859. break;
  860. case MUSB_DMA_STATUS_UNKNOWN:
  861. dev_dbg(musb->controller, "%cX DMA%d not allocated!\n",
  862. cppi_ch->transmit ? 'T' : 'R',
  863. cppi_ch->index);
  864. /* FALLTHROUGH */
  865. case MUSB_DMA_STATUS_FREE:
  866. break;
  867. }
  868. ch->status = MUSB_DMA_STATUS_BUSY;
  869. /* set transfer parameters, then queue up its first segment */
  870. cppi_ch->buf_dma = dma_addr;
  871. cppi_ch->offset = 0;
  872. cppi_ch->maxpacket = maxpacket;
  873. cppi_ch->buf_len = len;
  874. cppi_ch->channel.actual_len = 0;
  875. /* TX channel? or RX? */
  876. if (cppi_ch->transmit)
  877. cppi_next_tx_segment(musb, cppi_ch);
  878. else
  879. cppi_next_rx_segment(musb, cppi_ch, mode);
  880. return true;
  881. }
  882. static bool cppi_rx_scan(struct cppi *cppi, unsigned ch)
  883. {
  884. struct cppi_channel *rx = &cppi->rx[ch];
  885. struct cppi_rx_stateram __iomem *state = rx->state_ram;
  886. struct cppi_descriptor *bd;
  887. struct cppi_descriptor *last = rx->last_processed;
  888. bool completed = false;
  889. bool acked = false;
  890. int i;
  891. dma_addr_t safe2ack;
  892. void __iomem *regs = rx->hw_ep->regs;
  893. struct musb *musb = cppi->musb;
  894. cppi_dump_rx(6, rx, "/K");
  895. bd = last ? last->next : rx->head;
  896. if (!bd)
  897. return false;
  898. /* run through all completed BDs */
  899. for (i = 0, safe2ack = musb_readl(&state->rx_complete, 0);
  900. (safe2ack || completed) && bd && i < NUM_RXCHAN_BD;
  901. i++, bd = bd->next) {
  902. u16 len;
  903. /* catch latest BD writes from CPPI */
  904. rmb();
  905. if (!completed && (bd->hw_options & CPPI_OWN_SET))
  906. break;
  907. dev_dbg(musb->controller, "C/RXBD %llx: nxt %08x buf %08x "
  908. "off.len %08x opt.len %08x (%d)\n",
  909. (unsigned long long)bd->dma, bd->hw_next, bd->hw_bufp,
  910. bd->hw_off_len, bd->hw_options,
  911. rx->channel.actual_len);
  912. /* actual packet received length */
  913. if ((bd->hw_options & CPPI_SOP_SET) && !completed)
  914. len = bd->hw_off_len & CPPI_RECV_PKTLEN_MASK;
  915. else
  916. len = 0;
  917. if (bd->hw_options & CPPI_EOQ_MASK)
  918. completed = true;
  919. if (!completed && len < bd->buflen) {
  920. /* NOTE: when we get a short packet, RXCSR_H_REQPKT
  921. * must have been cleared, and no more DMA packets may
  922. * active be in the queue... TI docs didn't say, but
  923. * CPPI ignores those BDs even though OWN is still set.
  924. */
  925. completed = true;
  926. dev_dbg(musb->controller, "rx short %d/%d (%d)\n",
  927. len, bd->buflen,
  928. rx->channel.actual_len);
  929. }
  930. /* If we got here, we expect to ack at least one BD; meanwhile
  931. * CPPI may completing other BDs while we scan this list...
  932. *
  933. * RACE: we can notice OWN cleared before CPPI raises the
  934. * matching irq by writing that BD as the completion pointer.
  935. * In such cases, stop scanning and wait for the irq, avoiding
  936. * lost acks and states where BD ownership is unclear.
  937. */
  938. if (bd->dma == safe2ack) {
  939. musb_writel(&state->rx_complete, 0, safe2ack);
  940. safe2ack = musb_readl(&state->rx_complete, 0);
  941. acked = true;
  942. if (bd->dma == safe2ack)
  943. safe2ack = 0;
  944. }
  945. rx->channel.actual_len += len;
  946. cppi_bd_free(rx, last);
  947. last = bd;
  948. /* stop scanning on end-of-segment */
  949. if (bd->hw_next == 0)
  950. completed = true;
  951. }
  952. rx->last_processed = last;
  953. /* dma abort, lost ack, or ... */
  954. if (!acked && last) {
  955. int csr;
  956. if (safe2ack == 0 || safe2ack == rx->last_processed->dma)
  957. musb_writel(&state->rx_complete, 0, safe2ack);
  958. if (safe2ack == 0) {
  959. cppi_bd_free(rx, last);
  960. rx->last_processed = NULL;
  961. /* if we land here on the host side, H_REQPKT will
  962. * be clear and we need to restart the queue...
  963. */
  964. WARN_ON(rx->head);
  965. }
  966. musb_ep_select(cppi->mregs, rx->index + 1);
  967. csr = musb_readw(regs, MUSB_RXCSR);
  968. if (csr & MUSB_RXCSR_DMAENAB) {
  969. dev_dbg(musb->controller, "list%d %p/%p, last %llx%s, csr %04x\n",
  970. rx->index,
  971. rx->head, rx->tail,
  972. rx->last_processed
  973. ? (unsigned long long)
  974. rx->last_processed->dma
  975. : 0,
  976. completed ? ", completed" : "",
  977. csr);
  978. cppi_dump_rxq(4, "/what?", rx);
  979. }
  980. }
  981. if (!completed) {
  982. int csr;
  983. rx->head = bd;
  984. /* REVISIT seems like "autoreq all but EOP" doesn't...
  985. * setting it here "should" be racey, but seems to work
  986. */
  987. csr = musb_readw(rx->hw_ep->regs, MUSB_RXCSR);
  988. if (is_host_active(cppi->musb)
  989. && bd
  990. && !(csr & MUSB_RXCSR_H_REQPKT)) {
  991. csr |= MUSB_RXCSR_H_REQPKT;
  992. musb_writew(regs, MUSB_RXCSR,
  993. MUSB_RXCSR_H_WZC_BITS | csr);
  994. csr = musb_readw(rx->hw_ep->regs, MUSB_RXCSR);
  995. }
  996. } else {
  997. rx->head = NULL;
  998. rx->tail = NULL;
  999. }
  1000. cppi_dump_rx(6, rx, completed ? "/completed" : "/cleaned");
  1001. return completed;
  1002. }
  1003. irqreturn_t cppi_interrupt(int irq, void *dev_id)
  1004. {
  1005. struct musb *musb = dev_id;
  1006. struct cppi *cppi;
  1007. void __iomem *tibase;
  1008. struct musb_hw_ep *hw_ep = NULL;
  1009. u32 rx, tx;
  1010. int i, index;
  1011. unsigned long uninitialized_var(flags);
  1012. cppi = container_of(musb->dma_controller, struct cppi, controller);
  1013. if (cppi->irq)
  1014. spin_lock_irqsave(&musb->lock, flags);
  1015. tibase = musb->ctrl_base;
  1016. tx = musb_readl(tibase, DAVINCI_TXCPPI_MASKED_REG);
  1017. rx = musb_readl(tibase, DAVINCI_RXCPPI_MASKED_REG);
  1018. if (!tx && !rx) {
  1019. if (cppi->irq)
  1020. spin_unlock_irqrestore(&musb->lock, flags);
  1021. return IRQ_NONE;
  1022. }
  1023. dev_dbg(musb->controller, "CPPI IRQ Tx%x Rx%x\n", tx, rx);
  1024. /* process TX channels */
  1025. for (index = 0; tx; tx = tx >> 1, index++) {
  1026. struct cppi_channel *tx_ch;
  1027. struct cppi_tx_stateram __iomem *tx_ram;
  1028. bool completed = false;
  1029. struct cppi_descriptor *bd;
  1030. if (!(tx & 1))
  1031. continue;
  1032. tx_ch = cppi->tx + index;
  1033. tx_ram = tx_ch->state_ram;
  1034. /* FIXME need a cppi_tx_scan() routine, which
  1035. * can also be called from abort code
  1036. */
  1037. cppi_dump_tx(5, tx_ch, "/E");
  1038. bd = tx_ch->head;
  1039. /*
  1040. * If Head is null then this could mean that a abort interrupt
  1041. * that needs to be acknowledged.
  1042. */
  1043. if (NULL == bd) {
  1044. dev_dbg(musb->controller, "null BD\n");
  1045. musb_writel(&tx_ram->tx_complete, 0, 0);
  1046. continue;
  1047. }
  1048. /* run through all completed BDs */
  1049. for (i = 0; !completed && bd && i < NUM_TXCHAN_BD;
  1050. i++, bd = bd->next) {
  1051. u16 len;
  1052. /* catch latest BD writes from CPPI */
  1053. rmb();
  1054. if (bd->hw_options & CPPI_OWN_SET)
  1055. break;
  1056. dev_dbg(musb->controller, "C/TXBD %p n %x b %x off %x opt %x\n",
  1057. bd, bd->hw_next, bd->hw_bufp,
  1058. bd->hw_off_len, bd->hw_options);
  1059. len = bd->hw_off_len & CPPI_BUFFER_LEN_MASK;
  1060. tx_ch->channel.actual_len += len;
  1061. tx_ch->last_processed = bd;
  1062. /* write completion register to acknowledge
  1063. * processing of completed BDs, and possibly
  1064. * release the IRQ; EOQ might not be set ...
  1065. *
  1066. * REVISIT use the same ack strategy as rx
  1067. *
  1068. * REVISIT have observed bit 18 set; huh??
  1069. */
  1070. /* if ((bd->hw_options & CPPI_EOQ_MASK)) */
  1071. musb_writel(&tx_ram->tx_complete, 0, bd->dma);
  1072. /* stop scanning on end-of-segment */
  1073. if (bd->hw_next == 0)
  1074. completed = true;
  1075. }
  1076. /* on end of segment, maybe go to next one */
  1077. if (completed) {
  1078. /* cppi_dump_tx(4, tx_ch, "/complete"); */
  1079. /* transfer more, or report completion */
  1080. if (tx_ch->offset >= tx_ch->buf_len) {
  1081. tx_ch->head = NULL;
  1082. tx_ch->tail = NULL;
  1083. tx_ch->channel.status = MUSB_DMA_STATUS_FREE;
  1084. hw_ep = tx_ch->hw_ep;
  1085. musb_dma_completion(musb, index + 1, 1);
  1086. } else {
  1087. /* Bigger transfer than we could fit in
  1088. * that first batch of descriptors...
  1089. */
  1090. cppi_next_tx_segment(musb, tx_ch);
  1091. }
  1092. } else
  1093. tx_ch->head = bd;
  1094. }
  1095. /* Start processing the RX block */
  1096. for (index = 0; rx; rx = rx >> 1, index++) {
  1097. if (rx & 1) {
  1098. struct cppi_channel *rx_ch;
  1099. rx_ch = cppi->rx + index;
  1100. /* let incomplete dma segments finish */
  1101. if (!cppi_rx_scan(cppi, index))
  1102. continue;
  1103. /* start another dma segment if needed */
  1104. if (rx_ch->channel.actual_len != rx_ch->buf_len
  1105. && rx_ch->channel.actual_len
  1106. == rx_ch->offset) {
  1107. cppi_next_rx_segment(musb, rx_ch, 1);
  1108. continue;
  1109. }
  1110. /* all segments completed! */
  1111. rx_ch->channel.status = MUSB_DMA_STATUS_FREE;
  1112. hw_ep = rx_ch->hw_ep;
  1113. core_rxirq_disable(tibase, index + 1);
  1114. musb_dma_completion(musb, index + 1, 0);
  1115. }
  1116. }
  1117. /* write to CPPI EOI register to re-enable interrupts */
  1118. musb_writel(tibase, DAVINCI_CPPI_EOI_REG, 0);
  1119. if (cppi->irq)
  1120. spin_unlock_irqrestore(&musb->lock, flags);
  1121. return IRQ_HANDLED;
  1122. }
  1123. /* Instantiate a software object representing a DMA controller. */
  1124. struct dma_controller *__init
  1125. dma_controller_create(struct musb *musb, void __iomem *mregs)
  1126. {
  1127. struct cppi *controller;
  1128. struct device *dev = musb->controller;
  1129. struct platform_device *pdev = to_platform_device(dev);
  1130. int irq = platform_get_irq_byname(pdev, "dma");
  1131. controller = kzalloc(sizeof *controller, GFP_KERNEL);
  1132. if (!controller)
  1133. return NULL;
  1134. controller->mregs = mregs;
  1135. controller->tibase = mregs - DAVINCI_BASE_OFFSET;
  1136. controller->musb = musb;
  1137. controller->controller.start = cppi_controller_start;
  1138. controller->controller.stop = cppi_controller_stop;
  1139. controller->controller.channel_alloc = cppi_channel_allocate;
  1140. controller->controller.channel_release = cppi_channel_release;
  1141. controller->controller.channel_program = cppi_channel_program;
  1142. controller->controller.channel_abort = cppi_channel_abort;
  1143. /* NOTE: allocating from on-chip SRAM would give the least
  1144. * contention for memory access, if that ever matters here.
  1145. */
  1146. /* setup BufferPool */
  1147. controller->pool = dma_pool_create("cppi",
  1148. controller->musb->controller,
  1149. sizeof(struct cppi_descriptor),
  1150. CPPI_DESCRIPTOR_ALIGN, 0);
  1151. if (!controller->pool) {
  1152. kfree(controller);
  1153. return NULL;
  1154. }
  1155. if (irq > 0) {
  1156. if (request_irq(irq, cppi_interrupt, 0, "cppi-dma", musb)) {
  1157. dev_err(dev, "request_irq %d failed!\n", irq);
  1158. dma_controller_destroy(&controller->controller);
  1159. return NULL;
  1160. }
  1161. controller->irq = irq;
  1162. }
  1163. return &controller->controller;
  1164. }
  1165. /*
  1166. * Destroy a previously-instantiated DMA controller.
  1167. */
  1168. void dma_controller_destroy(struct dma_controller *c)
  1169. {
  1170. struct cppi *cppi;
  1171. cppi = container_of(c, struct cppi, controller);
  1172. if (cppi->irq)
  1173. free_irq(cppi->irq, cppi->musb);
  1174. /* assert: caller stopped the controller first */
  1175. dma_pool_destroy(cppi->pool);
  1176. kfree(cppi);
  1177. }
  1178. /*
  1179. * Context: controller irqlocked, endpoint selected
  1180. */
  1181. static int cppi_channel_abort(struct dma_channel *channel)
  1182. {
  1183. struct cppi_channel *cppi_ch;
  1184. struct cppi *controller;
  1185. void __iomem *mbase;
  1186. void __iomem *tibase;
  1187. void __iomem *regs;
  1188. u32 value;
  1189. struct cppi_descriptor *queue;
  1190. cppi_ch = container_of(channel, struct cppi_channel, channel);
  1191. controller = cppi_ch->controller;
  1192. switch (channel->status) {
  1193. case MUSB_DMA_STATUS_BUS_ABORT:
  1194. case MUSB_DMA_STATUS_CORE_ABORT:
  1195. /* from RX or TX fault irq handler */
  1196. case MUSB_DMA_STATUS_BUSY:
  1197. /* the hardware needs shutting down */
  1198. regs = cppi_ch->hw_ep->regs;
  1199. break;
  1200. case MUSB_DMA_STATUS_UNKNOWN:
  1201. case MUSB_DMA_STATUS_FREE:
  1202. return 0;
  1203. default:
  1204. return -EINVAL;
  1205. }
  1206. if (!cppi_ch->transmit && cppi_ch->head)
  1207. cppi_dump_rxq(3, "/abort", cppi_ch);
  1208. mbase = controller->mregs;
  1209. tibase = controller->tibase;
  1210. queue = cppi_ch->head;
  1211. cppi_ch->head = NULL;
  1212. cppi_ch->tail = NULL;
  1213. /* REVISIT should rely on caller having done this,
  1214. * and caller should rely on us not changing it.
  1215. * peripheral code is safe ... check host too.
  1216. */
  1217. musb_ep_select(mbase, cppi_ch->index + 1);
  1218. if (cppi_ch->transmit) {
  1219. struct cppi_tx_stateram __iomem *tx_ram;
  1220. /* REVISIT put timeouts on these controller handshakes */
  1221. cppi_dump_tx(6, cppi_ch, " (teardown)");
  1222. /* teardown DMA engine then usb core */
  1223. do {
  1224. value = musb_readl(tibase, DAVINCI_TXCPPI_TEAR_REG);
  1225. } while (!(value & CPPI_TEAR_READY));
  1226. musb_writel(tibase, DAVINCI_TXCPPI_TEAR_REG, cppi_ch->index);
  1227. tx_ram = cppi_ch->state_ram;
  1228. do {
  1229. value = musb_readl(&tx_ram->tx_complete, 0);
  1230. } while (0xFFFFFFFC != value);
  1231. /* FIXME clean up the transfer state ... here?
  1232. * the completion routine should get called with
  1233. * an appropriate status code.
  1234. */
  1235. value = musb_readw(regs, MUSB_TXCSR);
  1236. value &= ~MUSB_TXCSR_DMAENAB;
  1237. value |= MUSB_TXCSR_FLUSHFIFO;
  1238. musb_writew(regs, MUSB_TXCSR, value);
  1239. musb_writew(regs, MUSB_TXCSR, value);
  1240. /*
  1241. * 1. Write to completion Ptr value 0x1(bit 0 set)
  1242. * (write back mode)
  1243. * 2. Wait for abort interrupt and then put the channel in
  1244. * compare mode by writing 1 to the tx_complete register.
  1245. */
  1246. cppi_reset_tx(tx_ram, 1);
  1247. cppi_ch->head = NULL;
  1248. musb_writel(&tx_ram->tx_complete, 0, 1);
  1249. cppi_dump_tx(5, cppi_ch, " (done teardown)");
  1250. /* REVISIT tx side _should_ clean up the same way
  1251. * as the RX side ... this does no cleanup at all!
  1252. */
  1253. } else /* RX */ {
  1254. u16 csr;
  1255. /* NOTE: docs don't guarantee any of this works ... we
  1256. * expect that if the usb core stops telling the cppi core
  1257. * to pull more data from it, then it'll be safe to flush
  1258. * current RX DMA state iff any pending fifo transfer is done.
  1259. */
  1260. core_rxirq_disable(tibase, cppi_ch->index + 1);
  1261. /* for host, ensure ReqPkt is never set again */
  1262. if (is_host_active(cppi_ch->controller->musb)) {
  1263. value = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
  1264. value &= ~((0x3) << (cppi_ch->index * 2));
  1265. musb_writel(tibase, DAVINCI_AUTOREQ_REG, value);
  1266. }
  1267. csr = musb_readw(regs, MUSB_RXCSR);
  1268. /* for host, clear (just) ReqPkt at end of current packet(s) */
  1269. if (is_host_active(cppi_ch->controller->musb)) {
  1270. csr |= MUSB_RXCSR_H_WZC_BITS;
  1271. csr &= ~MUSB_RXCSR_H_REQPKT;
  1272. } else
  1273. csr |= MUSB_RXCSR_P_WZC_BITS;
  1274. /* clear dma enable */
  1275. csr &= ~(MUSB_RXCSR_DMAENAB);
  1276. musb_writew(regs, MUSB_RXCSR, csr);
  1277. csr = musb_readw(regs, MUSB_RXCSR);
  1278. /* Quiesce: wait for current dma to finish (if not cleanup).
  1279. * We can't use bit zero of stateram->rx_sop, since that
  1280. * refers to an entire "DMA packet" not just emptying the
  1281. * current fifo. Most segments need multiple usb packets.
  1282. */
  1283. if (channel->status == MUSB_DMA_STATUS_BUSY)
  1284. udelay(50);
  1285. /* scan the current list, reporting any data that was
  1286. * transferred and acking any IRQ
  1287. */
  1288. cppi_rx_scan(controller, cppi_ch->index);
  1289. /* clobber the existing state once it's idle
  1290. *
  1291. * NOTE: arguably, we should also wait for all the other
  1292. * RX channels to quiesce (how??) and then temporarily
  1293. * disable RXCPPI_CTRL_REG ... but it seems that we can
  1294. * rely on the controller restarting from state ram, with
  1295. * only RXCPPI_BUFCNT state being bogus. BUFCNT will
  1296. * correct itself after the next DMA transfer though.
  1297. *
  1298. * REVISIT does using rndis mode change that?
  1299. */
  1300. cppi_reset_rx(cppi_ch->state_ram);
  1301. /* next DMA request _should_ load cppi head ptr */
  1302. /* ... we don't "free" that list, only mutate it in place. */
  1303. cppi_dump_rx(5, cppi_ch, " (done abort)");
  1304. /* clean up previously pending bds */
  1305. cppi_bd_free(cppi_ch, cppi_ch->last_processed);
  1306. cppi_ch->last_processed = NULL;
  1307. while (queue) {
  1308. struct cppi_descriptor *tmp = queue->next;
  1309. cppi_bd_free(cppi_ch, queue);
  1310. queue = tmp;
  1311. }
  1312. }
  1313. channel->status = MUSB_DMA_STATUS_FREE;
  1314. cppi_ch->buf_dma = 0;
  1315. cppi_ch->offset = 0;
  1316. cppi_ch->buf_len = 0;
  1317. cppi_ch->maxpacket = 0;
  1318. return 0;
  1319. }
  1320. /* TBD Queries:
  1321. *
  1322. * Power Management ... probably turn off cppi during suspend, restart;
  1323. * check state ram? Clocking is presumably shared with usb core.
  1324. */