ti-edma3.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Enhanced Direct Memory Access (EDMA3) Controller
  3. *
  4. * (C) Copyright 2014
  5. * Texas Instruments Incorporated, <www.ti.com>
  6. *
  7. * Author: Ivan Khoronzhuk <ivan.khoronzhuk@ti.com>
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <asm/io.h>
  12. #include <common.h>
  13. #include <asm/ti-common/ti-edma3.h>
  14. #define EDMA3_SL_BASE(slot) (0x4000 + ((slot) << 5))
  15. #define EDMA3_SL_MAX_NUM 512
  16. #define EDMA3_SLOPT_FIFO_WIDTH_MASK (0x7 << 8)
  17. #define EDMA3_QCHMAP(ch) 0x0200 + ((ch) << 2)
  18. #define EDMA3_CHMAP_PARSET_MASK 0x1ff
  19. #define EDMA3_CHMAP_PARSET_SHIFT 0x5
  20. #define EDMA3_CHMAP_TRIGWORD_SHIFT 0x2
  21. #define EDMA3_QEMCR 0x314
  22. #define EDMA3_IPR 0x1068
  23. #define EDMA3_IPRH 0x106c
  24. #define EDMA3_ICR 0x1070
  25. #define EDMA3_ICRH 0x1074
  26. #define EDMA3_QEECR 0x1088
  27. #define EDMA3_QEESR 0x108c
  28. #define EDMA3_QSECR 0x1094
  29. /**
  30. * qedma3_start - start qdma on a channel
  31. * @base: base address of edma
  32. * @cfg: pinter to struct edma3_channel_config where you can set
  33. * the slot number to associate with, the chnum, which corresponds
  34. * your quick channel number 0-7, complete code - transfer complete code
  35. * and trigger slot word - which has to correspond to the word number in
  36. * edma3_slot_layout struct for generating event.
  37. *
  38. */
  39. void qedma3_start(u32 base, struct edma3_channel_config *cfg)
  40. {
  41. u32 qchmap;
  42. /* Clear the pending int bit */
  43. if (cfg->complete_code < 32)
  44. __raw_writel(1 << cfg->complete_code, base + EDMA3_ICR);
  45. else
  46. __raw_writel(1 << cfg->complete_code, base + EDMA3_ICRH);
  47. /* Map parameter set and trigger word 7 to quick channel */
  48. qchmap = ((EDMA3_CHMAP_PARSET_MASK & cfg->slot)
  49. << EDMA3_CHMAP_PARSET_SHIFT) |
  50. (cfg->trigger_slot_word << EDMA3_CHMAP_TRIGWORD_SHIFT);
  51. __raw_writel(qchmap, base + EDMA3_QCHMAP(cfg->chnum));
  52. /* Clear missed event if set*/
  53. __raw_writel(1 << cfg->chnum, base + EDMA3_QSECR);
  54. __raw_writel(1 << cfg->chnum, base + EDMA3_QEMCR);
  55. /* Enable qdma channel event */
  56. __raw_writel(1 << cfg->chnum, base + EDMA3_QEESR);
  57. }
  58. /**
  59. * edma3_set_dest - set initial DMA destination address in parameter RAM slot
  60. * @base: base address of edma
  61. * @slot: parameter RAM slot being configured
  62. * @dst: physical address of destination (memory, controller FIFO, etc)
  63. * @addressMode: INCR, except in very rare cases
  64. * @width: ignored unless @addressMode is FIFO, else specifies the
  65. * width to use when addressing the fifo (e.g. W8BIT, W32BIT)
  66. *
  67. * Note that the destination address is modified during the DMA transfer
  68. * according to edma3_set_dest_index().
  69. */
  70. void edma3_set_dest(u32 base, int slot, u32 dst, enum edma3_address_mode mode,
  71. enum edma3_fifo_width width)
  72. {
  73. u32 opt;
  74. struct edma3_slot_layout *rg;
  75. rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
  76. opt = __raw_readl(&rg->opt);
  77. if (mode == FIFO)
  78. opt = (opt & EDMA3_SLOPT_FIFO_WIDTH_MASK) |
  79. (EDMA3_SLOPT_DST_ADDR_CONST_MODE |
  80. EDMA3_SLOPT_FIFO_WIDTH_SET(width));
  81. else
  82. opt &= ~EDMA3_SLOPT_DST_ADDR_CONST_MODE;
  83. __raw_writel(opt, &rg->opt);
  84. __raw_writel(dst, &rg->dst);
  85. }
  86. /**
  87. * edma3_set_dest_index - configure DMA destination address indexing
  88. * @base: base address of edma
  89. * @slot: parameter RAM slot being configured
  90. * @bidx: byte offset between destination arrays in a frame
  91. * @cidx: byte offset between destination frames in a block
  92. *
  93. * Offsets are specified to support either contiguous or discontiguous
  94. * memory transfers, or repeated access to a hardware register, as needed.
  95. * When accessing hardware registers, both offsets are normally zero.
  96. */
  97. void edma3_set_dest_index(u32 base, unsigned slot, int bidx, int cidx)
  98. {
  99. u32 src_dst_bidx;
  100. u32 src_dst_cidx;
  101. struct edma3_slot_layout *rg;
  102. rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
  103. src_dst_bidx = __raw_readl(&rg->src_dst_bidx);
  104. src_dst_cidx = __raw_readl(&rg->src_dst_cidx);
  105. __raw_writel((src_dst_bidx & 0x0000ffff) | (bidx << 16),
  106. &rg->src_dst_bidx);
  107. __raw_writel((src_dst_cidx & 0x0000ffff) | (cidx << 16),
  108. &rg->src_dst_cidx);
  109. }
  110. /**
  111. * edma3_set_dest_addr - set destination address for slot only
  112. */
  113. void edma3_set_dest_addr(u32 base, int slot, u32 dst)
  114. {
  115. struct edma3_slot_layout *rg;
  116. rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
  117. __raw_writel(dst, &rg->dst);
  118. }
  119. /**
  120. * edma3_set_src - set initial DMA source address in parameter RAM slot
  121. * @base: base address of edma
  122. * @slot: parameter RAM slot being configured
  123. * @src_port: physical address of source (memory, controller FIFO, etc)
  124. * @mode: INCR, except in very rare cases
  125. * @width: ignored unless @addressMode is FIFO, else specifies the
  126. * width to use when addressing the fifo (e.g. W8BIT, W32BIT)
  127. *
  128. * Note that the source address is modified during the DMA transfer
  129. * according to edma3_set_src_index().
  130. */
  131. void edma3_set_src(u32 base, int slot, u32 src, enum edma3_address_mode mode,
  132. enum edma3_fifo_width width)
  133. {
  134. u32 opt;
  135. struct edma3_slot_layout *rg;
  136. rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
  137. opt = __raw_readl(&rg->opt);
  138. if (mode == FIFO)
  139. opt = (opt & EDMA3_SLOPT_FIFO_WIDTH_MASK) |
  140. (EDMA3_SLOPT_DST_ADDR_CONST_MODE |
  141. EDMA3_SLOPT_FIFO_WIDTH_SET(width));
  142. else
  143. opt &= ~EDMA3_SLOPT_DST_ADDR_CONST_MODE;
  144. __raw_writel(opt, &rg->opt);
  145. __raw_writel(src, &rg->src);
  146. }
  147. /**
  148. * edma3_set_src_index - configure DMA source address indexing
  149. * @base: base address of edma
  150. * @slot: parameter RAM slot being configured
  151. * @bidx: byte offset between source arrays in a frame
  152. * @cidx: byte offset between source frames in a block
  153. *
  154. * Offsets are specified to support either contiguous or discontiguous
  155. * memory transfers, or repeated access to a hardware register, as needed.
  156. * When accessing hardware registers, both offsets are normally zero.
  157. */
  158. void edma3_set_src_index(u32 base, unsigned slot, int bidx, int cidx)
  159. {
  160. u32 src_dst_bidx;
  161. u32 src_dst_cidx;
  162. struct edma3_slot_layout *rg;
  163. rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
  164. src_dst_bidx = __raw_readl(&rg->src_dst_bidx);
  165. src_dst_cidx = __raw_readl(&rg->src_dst_cidx);
  166. __raw_writel((src_dst_bidx & 0xffff0000) | bidx,
  167. &rg->src_dst_bidx);
  168. __raw_writel((src_dst_cidx & 0xffff0000) | cidx,
  169. &rg->src_dst_cidx);
  170. }
  171. /**
  172. * edma3_set_src_addr - set source address for slot only
  173. */
  174. void edma3_set_src_addr(u32 base, int slot, u32 src)
  175. {
  176. struct edma3_slot_layout *rg;
  177. rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
  178. __raw_writel(src, &rg->src);
  179. }
  180. /**
  181. * edma3_set_transfer_params - configure DMA transfer parameters
  182. * @base: base address of edma
  183. * @slot: parameter RAM slot being configured
  184. * @acnt: how many bytes per array (at least one)
  185. * @bcnt: how many arrays per frame (at least one)
  186. * @ccnt: how many frames per block (at least one)
  187. * @bcnt_rld: used only for A-Synchronized transfers; this specifies
  188. * the value to reload into bcnt when it decrements to zero
  189. * @sync_mode: ASYNC or ABSYNC
  190. *
  191. * See the EDMA3 documentation to understand how to configure and link
  192. * transfers using the fields in PaRAM slots. If you are not doing it
  193. * all at once with edma3_write_slot(), you will use this routine
  194. * plus two calls each for source and destination, setting the initial
  195. * address and saying how to index that address.
  196. *
  197. * An example of an A-Synchronized transfer is a serial link using a
  198. * single word shift register. In that case, @acnt would be equal to
  199. * that word size; the serial controller issues a DMA synchronization
  200. * event to transfer each word, and memory access by the DMA transfer
  201. * controller will be word-at-a-time.
  202. *
  203. * An example of an AB-Synchronized transfer is a device using a FIFO.
  204. * In that case, @acnt equals the FIFO width and @bcnt equals its depth.
  205. * The controller with the FIFO issues DMA synchronization events when
  206. * the FIFO threshold is reached, and the DMA transfer controller will
  207. * transfer one frame to (or from) the FIFO. It will probably use
  208. * efficient burst modes to access memory.
  209. */
  210. void edma3_set_transfer_params(u32 base, int slot, int acnt,
  211. int bcnt, int ccnt, u16 bcnt_rld,
  212. enum edma3_sync_dimension sync_mode)
  213. {
  214. u32 opt;
  215. u32 link_bcntrld;
  216. struct edma3_slot_layout *rg;
  217. rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
  218. link_bcntrld = __raw_readl(&rg->link_bcntrld);
  219. __raw_writel((bcnt_rld << 16) | (0x0000ffff & link_bcntrld),
  220. &rg->link_bcntrld);
  221. opt = __raw_readl(&rg->opt);
  222. if (sync_mode == ASYNC)
  223. __raw_writel(opt & ~EDMA3_SLOPT_AB_SYNC, &rg->opt);
  224. else
  225. __raw_writel(opt | EDMA3_SLOPT_AB_SYNC, &rg->opt);
  226. /* Set the acount, bcount, ccount registers */
  227. __raw_writel((bcnt << 16) | (acnt & 0xffff), &rg->a_b_cnt);
  228. __raw_writel(0xffff & ccnt, &rg->ccnt);
  229. }
  230. /**
  231. * edma3_write_slot - write parameter RAM data for slot
  232. * @base: base address of edma
  233. * @slot: number of parameter RAM slot being modified
  234. * @param: data to be written into parameter RAM slot
  235. *
  236. * Use this to assign all parameters of a transfer at once. This
  237. * allows more efficient setup of transfers than issuing multiple
  238. * calls to set up those parameters in small pieces, and provides
  239. * complete control over all transfer options.
  240. */
  241. void edma3_write_slot(u32 base, int slot, struct edma3_slot_layout *param)
  242. {
  243. int i;
  244. u32 *p = (u32 *)param;
  245. u32 *addr = (u32 *)(base + EDMA3_SL_BASE(slot));
  246. for (i = 0; i < sizeof(struct edma3_slot_layout)/4; i += 4)
  247. __raw_writel(*p++, addr++);
  248. }
  249. /**
  250. * edma3_read_slot - read parameter RAM data from slot
  251. * @base: base address of edma
  252. * @slot: number of parameter RAM slot being copied
  253. * @param: where to store copy of parameter RAM data
  254. *
  255. * Use this to read data from a parameter RAM slot, perhaps to
  256. * save them as a template for later reuse.
  257. */
  258. void edma3_read_slot(u32 base, int slot, struct edma3_slot_layout *param)
  259. {
  260. int i;
  261. u32 *p = (u32 *)param;
  262. u32 *addr = (u32 *)(base + EDMA3_SL_BASE(slot));
  263. for (i = 0; i < sizeof(struct edma3_slot_layout)/4; i += 4)
  264. *p++ = __raw_readl(addr++);
  265. }
  266. void edma3_slot_configure(u32 base, int slot, struct edma3_slot_config *cfg)
  267. {
  268. struct edma3_slot_layout *rg;
  269. rg = (struct edma3_slot_layout *)(base + EDMA3_SL_BASE(slot));
  270. __raw_writel(cfg->opt, &rg->opt);
  271. __raw_writel(cfg->src, &rg->src);
  272. __raw_writel((cfg->bcnt << 16) | (cfg->acnt & 0xffff), &rg->a_b_cnt);
  273. __raw_writel(cfg->dst, &rg->dst);
  274. __raw_writel((cfg->dst_bidx << 16) |
  275. (cfg->src_bidx & 0xffff), &rg->src_dst_bidx);
  276. __raw_writel((cfg->bcntrld << 16) |
  277. (cfg->link & 0xffff), &rg->link_bcntrld);
  278. __raw_writel((cfg->dst_cidx << 16) |
  279. (cfg->src_cidx & 0xffff), &rg->src_dst_cidx);
  280. __raw_writel(0xffff & cfg->ccnt, &rg->ccnt);
  281. }
  282. /**
  283. * edma3_check_for_transfer - check if transfer coplete by checking
  284. * interrupt pending bit. Clear interrupt pending bit if complete.
  285. * @base: base address of edma
  286. * @cfg: pinter to struct edma3_channel_config which was passed
  287. * to qedma3_start when you started qdma channel
  288. *
  289. * Return 0 if complete, 1 if not.
  290. */
  291. int edma3_check_for_transfer(u32 base, struct edma3_channel_config *cfg)
  292. {
  293. u32 inum;
  294. u32 ipr_base;
  295. u32 icr_base;
  296. if (cfg->complete_code < 32) {
  297. ipr_base = base + EDMA3_IPR;
  298. icr_base = base + EDMA3_ICR;
  299. inum = 1 << cfg->complete_code;
  300. } else {
  301. ipr_base = base + EDMA3_IPRH;
  302. icr_base = base + EDMA3_ICRH;
  303. inum = 1 << (cfg->complete_code - 32);
  304. }
  305. /* check complete interrupt */
  306. if (!(__raw_readl(ipr_base) & inum))
  307. return 1;
  308. /* clean up the pending int bit */
  309. __raw_writel(inum, icr_base);
  310. return 0;
  311. }
  312. /**
  313. * qedma3_stop - stops dma on the channel passed
  314. * @base: base address of edma
  315. * @cfg: pinter to struct edma3_channel_config which was passed
  316. * to qedma3_start when you started qdma channel
  317. */
  318. void qedma3_stop(u32 base, struct edma3_channel_config *cfg)
  319. {
  320. /* Disable qdma channel event */
  321. __raw_writel(1 << cfg->chnum, base + EDMA3_QEECR);
  322. /* clean up the interrupt indication */
  323. if (cfg->complete_code < 32)
  324. __raw_writel(1 << cfg->complete_code, base + EDMA3_ICR);
  325. else
  326. __raw_writel(1 << cfg->complete_code, base + EDMA3_ICRH);
  327. /* Clear missed event if set*/
  328. __raw_writel(1 << cfg->chnum, base + EDMA3_QSECR);
  329. __raw_writel(1 << cfg->chnum, base + EDMA3_QEMCR);
  330. /* Clear the channel map */
  331. __raw_writel(0, base + EDMA3_QCHMAP(cfg->chnum));
  332. }
  333. void edma3_transfer(unsigned long edma3_base_addr, unsigned int
  334. edma_slot_num, void *dst, void *src, size_t len)
  335. {
  336. struct edma3_slot_config slot;
  337. struct edma3_channel_config edma_channel;
  338. int b_cnt_value = 1;
  339. int rem_bytes = 0;
  340. int a_cnt_value = len;
  341. unsigned int addr = (unsigned int) (dst);
  342. unsigned int max_acnt = 0x7FFFU;
  343. if (len > max_acnt) {
  344. b_cnt_value = (len / max_acnt);
  345. rem_bytes = (len % max_acnt);
  346. a_cnt_value = max_acnt;
  347. }
  348. slot.opt = 0;
  349. slot.src = ((unsigned int) src);
  350. slot.acnt = a_cnt_value;
  351. slot.bcnt = b_cnt_value;
  352. slot.ccnt = 1;
  353. slot.src_bidx = a_cnt_value;
  354. slot.dst_bidx = a_cnt_value;
  355. slot.src_cidx = 0;
  356. slot.dst_cidx = 0;
  357. slot.link = EDMA3_PARSET_NULL_LINK;
  358. slot.bcntrld = 0;
  359. slot.opt = EDMA3_SLOPT_TRANS_COMP_INT_ENB |
  360. EDMA3_SLOPT_COMP_CODE(0) |
  361. EDMA3_SLOPT_STATIC | EDMA3_SLOPT_AB_SYNC;
  362. edma3_slot_configure(edma3_base_addr, edma_slot_num, &slot);
  363. edma_channel.slot = edma_slot_num;
  364. edma_channel.chnum = 0;
  365. edma_channel.complete_code = 0;
  366. /* set event trigger to dst update */
  367. edma_channel.trigger_slot_word = EDMA3_TWORD(dst);
  368. qedma3_start(edma3_base_addr, &edma_channel);
  369. edma3_set_dest_addr(edma3_base_addr, edma_channel.slot, addr);
  370. while (edma3_check_for_transfer(edma3_base_addr, &edma_channel))
  371. ;
  372. qedma3_stop(edma3_base_addr, &edma_channel);
  373. if (rem_bytes != 0) {
  374. slot.opt = 0;
  375. slot.src =
  376. (b_cnt_value * max_acnt) + ((unsigned int) src);
  377. slot.acnt = rem_bytes;
  378. slot.bcnt = 1;
  379. slot.ccnt = 1;
  380. slot.src_bidx = rem_bytes;
  381. slot.dst_bidx = rem_bytes;
  382. slot.src_cidx = 0;
  383. slot.dst_cidx = 0;
  384. slot.link = EDMA3_PARSET_NULL_LINK;
  385. slot.bcntrld = 0;
  386. slot.opt = EDMA3_SLOPT_TRANS_COMP_INT_ENB |
  387. EDMA3_SLOPT_COMP_CODE(0) |
  388. EDMA3_SLOPT_STATIC | EDMA3_SLOPT_AB_SYNC;
  389. edma3_slot_configure(edma3_base_addr, edma_slot_num, &slot);
  390. edma_channel.slot = edma_slot_num;
  391. edma_channel.chnum = 0;
  392. edma_channel.complete_code = 0;
  393. /* set event trigger to dst update */
  394. edma_channel.trigger_slot_word = EDMA3_TWORD(dst);
  395. qedma3_start(edma3_base_addr, &edma_channel);
  396. edma3_set_dest_addr(edma3_base_addr, edma_channel.slot, addr +
  397. (max_acnt * b_cnt_value));
  398. while (edma3_check_for_transfer(edma3_base_addr, &edma_channel))
  399. ;
  400. qedma3_stop(edma3_base_addr, &edma_channel);
  401. }
  402. }