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