cppi_dma.c 44 KB

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