cppi_dma.c 44 KB

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