cppi_dma.c 44 KB

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