cppi_dma.c 43 KB

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