cppi_dma.c 43 KB

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