ti-edma3.c 16 KB

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