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