cppi_dma.c 43 KB

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