cppi_dma.c 43 KB

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