ste_dma40_ll.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2007-2010
  4. * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
  5. * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/dmaengine.h>
  9. #include "ste_dma40.h"
  10. #include "ste_dma40_ll.h"
  11. static u8 d40_width_to_bits(enum dma_slave_buswidth width)
  12. {
  13. if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  14. return STEDMA40_ESIZE_8_BIT;
  15. else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  16. return STEDMA40_ESIZE_16_BIT;
  17. else if (width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  18. return STEDMA40_ESIZE_64_BIT;
  19. else
  20. return STEDMA40_ESIZE_32_BIT;
  21. }
  22. /* Sets up proper LCSP1 and LCSP3 register for a logical channel */
  23. void d40_log_cfg(struct stedma40_chan_cfg *cfg,
  24. u32 *lcsp1, u32 *lcsp3)
  25. {
  26. u32 l3 = 0; /* dst */
  27. u32 l1 = 0; /* src */
  28. /* src is mem? -> increase address pos */
  29. if (cfg->dir == DMA_MEM_TO_DEV ||
  30. cfg->dir == DMA_MEM_TO_MEM)
  31. l1 |= BIT(D40_MEM_LCSP1_SCFG_INCR_POS);
  32. /* dst is mem? -> increase address pos */
  33. if (cfg->dir == DMA_DEV_TO_MEM ||
  34. cfg->dir == DMA_MEM_TO_MEM)
  35. l3 |= BIT(D40_MEM_LCSP3_DCFG_INCR_POS);
  36. /* src is hw? -> master port 1 */
  37. if (cfg->dir == DMA_DEV_TO_MEM ||
  38. cfg->dir == DMA_DEV_TO_DEV)
  39. l1 |= BIT(D40_MEM_LCSP1_SCFG_MST_POS);
  40. /* dst is hw? -> master port 1 */
  41. if (cfg->dir == DMA_MEM_TO_DEV ||
  42. cfg->dir == DMA_DEV_TO_DEV)
  43. l3 |= BIT(D40_MEM_LCSP3_DCFG_MST_POS);
  44. l3 |= BIT(D40_MEM_LCSP3_DCFG_EIM_POS);
  45. l3 |= cfg->dst_info.psize << D40_MEM_LCSP3_DCFG_PSIZE_POS;
  46. l3 |= d40_width_to_bits(cfg->dst_info.data_width)
  47. << D40_MEM_LCSP3_DCFG_ESIZE_POS;
  48. l1 |= BIT(D40_MEM_LCSP1_SCFG_EIM_POS);
  49. l1 |= cfg->src_info.psize << D40_MEM_LCSP1_SCFG_PSIZE_POS;
  50. l1 |= d40_width_to_bits(cfg->src_info.data_width)
  51. << D40_MEM_LCSP1_SCFG_ESIZE_POS;
  52. *lcsp1 = l1;
  53. *lcsp3 = l3;
  54. }
  55. void d40_phy_cfg(struct stedma40_chan_cfg *cfg, u32 *src_cfg, u32 *dst_cfg)
  56. {
  57. u32 src = 0;
  58. u32 dst = 0;
  59. if ((cfg->dir == DMA_DEV_TO_MEM) ||
  60. (cfg->dir == DMA_DEV_TO_DEV)) {
  61. /* Set master port to 1 */
  62. src |= BIT(D40_SREG_CFG_MST_POS);
  63. src |= D40_TYPE_TO_EVENT(cfg->dev_type);
  64. if (cfg->src_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
  65. src |= BIT(D40_SREG_CFG_PHY_TM_POS);
  66. else
  67. src |= 3 << D40_SREG_CFG_PHY_TM_POS;
  68. }
  69. if ((cfg->dir == DMA_MEM_TO_DEV) ||
  70. (cfg->dir == DMA_DEV_TO_DEV)) {
  71. /* Set master port to 1 */
  72. dst |= BIT(D40_SREG_CFG_MST_POS);
  73. dst |= D40_TYPE_TO_EVENT(cfg->dev_type);
  74. if (cfg->dst_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
  75. dst |= BIT(D40_SREG_CFG_PHY_TM_POS);
  76. else
  77. dst |= 3 << D40_SREG_CFG_PHY_TM_POS;
  78. }
  79. /* Interrupt on end of transfer for destination */
  80. dst |= BIT(D40_SREG_CFG_TIM_POS);
  81. /* Generate interrupt on error */
  82. src |= BIT(D40_SREG_CFG_EIM_POS);
  83. dst |= BIT(D40_SREG_CFG_EIM_POS);
  84. /* PSIZE */
  85. if (cfg->src_info.psize != STEDMA40_PSIZE_PHY_1) {
  86. src |= BIT(D40_SREG_CFG_PHY_PEN_POS);
  87. src |= cfg->src_info.psize << D40_SREG_CFG_PSIZE_POS;
  88. }
  89. if (cfg->dst_info.psize != STEDMA40_PSIZE_PHY_1) {
  90. dst |= BIT(D40_SREG_CFG_PHY_PEN_POS);
  91. dst |= cfg->dst_info.psize << D40_SREG_CFG_PSIZE_POS;
  92. }
  93. /* Element size */
  94. src |= d40_width_to_bits(cfg->src_info.data_width)
  95. << D40_SREG_CFG_ESIZE_POS;
  96. dst |= d40_width_to_bits(cfg->dst_info.data_width)
  97. << D40_SREG_CFG_ESIZE_POS;
  98. /* Set the priority bit to high for the physical channel */
  99. if (cfg->high_priority) {
  100. src |= BIT(D40_SREG_CFG_PRI_POS);
  101. dst |= BIT(D40_SREG_CFG_PRI_POS);
  102. }
  103. if (cfg->src_info.big_endian)
  104. src |= BIT(D40_SREG_CFG_LBE_POS);
  105. if (cfg->dst_info.big_endian)
  106. dst |= BIT(D40_SREG_CFG_LBE_POS);
  107. *src_cfg = src;
  108. *dst_cfg = dst;
  109. }
  110. static int d40_phy_fill_lli(struct d40_phy_lli *lli,
  111. dma_addr_t data,
  112. u32 data_size,
  113. dma_addr_t next_lli,
  114. u32 reg_cfg,
  115. struct stedma40_half_channel_info *info,
  116. unsigned int flags)
  117. {
  118. bool addr_inc = flags & LLI_ADDR_INC;
  119. bool term_int = flags & LLI_TERM_INT;
  120. unsigned int data_width = info->data_width;
  121. int psize = info->psize;
  122. int num_elems;
  123. if (psize == STEDMA40_PSIZE_PHY_1)
  124. num_elems = 1;
  125. else
  126. num_elems = 2 << psize;
  127. /* Must be aligned */
  128. if (!IS_ALIGNED(data, data_width))
  129. return -EINVAL;
  130. /* Transfer size can't be smaller than (num_elms * elem_size) */
  131. if (data_size < num_elems * data_width)
  132. return -EINVAL;
  133. /* The number of elements. IE now many chunks */
  134. lli->reg_elt = (data_size / data_width) << D40_SREG_ELEM_PHY_ECNT_POS;
  135. /*
  136. * Distance to next element sized entry.
  137. * Usually the size of the element unless you want gaps.
  138. */
  139. if (addr_inc)
  140. lli->reg_elt |= data_width << D40_SREG_ELEM_PHY_EIDX_POS;
  141. /* Where the data is */
  142. lli->reg_ptr = data;
  143. lli->reg_cfg = reg_cfg;
  144. /* If this scatter list entry is the last one, no next link */
  145. if (next_lli == 0)
  146. lli->reg_lnk = BIT(D40_SREG_LNK_PHY_TCP_POS);
  147. else
  148. lli->reg_lnk = next_lli;
  149. /* Set/clear interrupt generation on this link item.*/
  150. if (term_int)
  151. lli->reg_cfg |= BIT(D40_SREG_CFG_TIM_POS);
  152. else
  153. lli->reg_cfg &= ~BIT(D40_SREG_CFG_TIM_POS);
  154. /*
  155. * Post link - D40_SREG_LNK_PHY_PRE_POS = 0
  156. * Relink happens after transfer completion.
  157. */
  158. return 0;
  159. }
  160. static int d40_seg_size(int size, int data_width1, int data_width2)
  161. {
  162. u32 max_w = max(data_width1, data_width2);
  163. u32 min_w = min(data_width1, data_width2);
  164. u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w);
  165. if (seg_max > STEDMA40_MAX_SEG_SIZE)
  166. seg_max -= max_w;
  167. if (size <= seg_max)
  168. return size;
  169. if (size <= 2 * seg_max)
  170. return ALIGN(size / 2, max_w);
  171. return seg_max;
  172. }
  173. static struct d40_phy_lli *
  174. d40_phy_buf_to_lli(struct d40_phy_lli *lli, dma_addr_t addr, u32 size,
  175. dma_addr_t lli_phys, dma_addr_t first_phys, u32 reg_cfg,
  176. struct stedma40_half_channel_info *info,
  177. struct stedma40_half_channel_info *otherinfo,
  178. unsigned long flags)
  179. {
  180. bool lastlink = flags & LLI_LAST_LINK;
  181. bool addr_inc = flags & LLI_ADDR_INC;
  182. bool term_int = flags & LLI_TERM_INT;
  183. bool cyclic = flags & LLI_CYCLIC;
  184. int err;
  185. dma_addr_t next = lli_phys;
  186. int size_rest = size;
  187. int size_seg = 0;
  188. /*
  189. * This piece may be split up based on d40_seg_size(); we only want the
  190. * term int on the last part.
  191. */
  192. if (term_int)
  193. flags &= ~LLI_TERM_INT;
  194. do {
  195. size_seg = d40_seg_size(size_rest, info->data_width,
  196. otherinfo->data_width);
  197. size_rest -= size_seg;
  198. if (size_rest == 0 && term_int)
  199. flags |= LLI_TERM_INT;
  200. if (size_rest == 0 && lastlink)
  201. next = cyclic ? first_phys : 0;
  202. else
  203. next = ALIGN(next + sizeof(struct d40_phy_lli),
  204. D40_LLI_ALIGN);
  205. err = d40_phy_fill_lli(lli, addr, size_seg, next,
  206. reg_cfg, info, flags);
  207. if (err)
  208. goto err;
  209. lli++;
  210. if (addr_inc)
  211. addr += size_seg;
  212. } while (size_rest);
  213. return lli;
  214. err:
  215. return NULL;
  216. }
  217. int d40_phy_sg_to_lli(struct scatterlist *sg,
  218. int sg_len,
  219. dma_addr_t target,
  220. struct d40_phy_lli *lli_sg,
  221. dma_addr_t lli_phys,
  222. u32 reg_cfg,
  223. struct stedma40_half_channel_info *info,
  224. struct stedma40_half_channel_info *otherinfo,
  225. unsigned long flags)
  226. {
  227. int total_size = 0;
  228. int i;
  229. struct scatterlist *current_sg = sg;
  230. struct d40_phy_lli *lli = lli_sg;
  231. dma_addr_t l_phys = lli_phys;
  232. if (!target)
  233. flags |= LLI_ADDR_INC;
  234. for_each_sg(sg, current_sg, sg_len, i) {
  235. dma_addr_t sg_addr = sg_dma_address(current_sg);
  236. unsigned int len = sg_dma_len(current_sg);
  237. dma_addr_t dst = target ?: sg_addr;
  238. total_size += sg_dma_len(current_sg);
  239. if (i == sg_len - 1)
  240. flags |= LLI_TERM_INT | LLI_LAST_LINK;
  241. l_phys = ALIGN(lli_phys + (lli - lli_sg) *
  242. sizeof(struct d40_phy_lli), D40_LLI_ALIGN);
  243. lli = d40_phy_buf_to_lli(lli, dst, len, l_phys, lli_phys,
  244. reg_cfg, info, otherinfo, flags);
  245. if (lli == NULL)
  246. return -EINVAL;
  247. }
  248. return total_size;
  249. }
  250. /* DMA logical lli operations */
  251. static void d40_log_lli_link(struct d40_log_lli *lli_dst,
  252. struct d40_log_lli *lli_src,
  253. int next, unsigned int flags)
  254. {
  255. bool interrupt = flags & LLI_TERM_INT;
  256. u32 slos = 0;
  257. u32 dlos = 0;
  258. if (next != -EINVAL) {
  259. slos = next * 2;
  260. dlos = next * 2 + 1;
  261. }
  262. if (interrupt) {
  263. lli_dst->lcsp13 |= D40_MEM_LCSP1_SCFG_TIM_MASK;
  264. lli_dst->lcsp13 |= D40_MEM_LCSP3_DTCP_MASK;
  265. }
  266. lli_src->lcsp13 = (lli_src->lcsp13 & ~D40_MEM_LCSP1_SLOS_MASK) |
  267. (slos << D40_MEM_LCSP1_SLOS_POS);
  268. lli_dst->lcsp13 = (lli_dst->lcsp13 & ~D40_MEM_LCSP1_SLOS_MASK) |
  269. (dlos << D40_MEM_LCSP1_SLOS_POS);
  270. }
  271. void d40_log_lli_lcpa_write(struct d40_log_lli_full *lcpa,
  272. struct d40_log_lli *lli_dst,
  273. struct d40_log_lli *lli_src,
  274. int next, unsigned int flags)
  275. {
  276. d40_log_lli_link(lli_dst, lli_src, next, flags);
  277. writel_relaxed(lli_src->lcsp02, &lcpa[0].lcsp0);
  278. writel_relaxed(lli_src->lcsp13, &lcpa[0].lcsp1);
  279. writel_relaxed(lli_dst->lcsp02, &lcpa[0].lcsp2);
  280. writel_relaxed(lli_dst->lcsp13, &lcpa[0].lcsp3);
  281. }
  282. void d40_log_lli_lcla_write(struct d40_log_lli *lcla,
  283. struct d40_log_lli *lli_dst,
  284. struct d40_log_lli *lli_src,
  285. int next, unsigned int flags)
  286. {
  287. d40_log_lli_link(lli_dst, lli_src, next, flags);
  288. writel_relaxed(lli_src->lcsp02, &lcla[0].lcsp02);
  289. writel_relaxed(lli_src->lcsp13, &lcla[0].lcsp13);
  290. writel_relaxed(lli_dst->lcsp02, &lcla[1].lcsp02);
  291. writel_relaxed(lli_dst->lcsp13, &lcla[1].lcsp13);
  292. }
  293. static void d40_log_fill_lli(struct d40_log_lli *lli,
  294. dma_addr_t data, u32 data_size,
  295. u32 reg_cfg,
  296. u32 data_width,
  297. unsigned int flags)
  298. {
  299. bool addr_inc = flags & LLI_ADDR_INC;
  300. lli->lcsp13 = reg_cfg;
  301. /* The number of elements to transfer */
  302. lli->lcsp02 = ((data_size / data_width) <<
  303. D40_MEM_LCSP0_ECNT_POS) & D40_MEM_LCSP0_ECNT_MASK;
  304. BUG_ON((data_size / data_width) > STEDMA40_MAX_SEG_SIZE);
  305. /* 16 LSBs address of the current element */
  306. lli->lcsp02 |= data & D40_MEM_LCSP0_SPTR_MASK;
  307. /* 16 MSBs address of the current element */
  308. lli->lcsp13 |= data & D40_MEM_LCSP1_SPTR_MASK;
  309. if (addr_inc)
  310. lli->lcsp13 |= D40_MEM_LCSP1_SCFG_INCR_MASK;
  311. }
  312. static struct d40_log_lli *d40_log_buf_to_lli(struct d40_log_lli *lli_sg,
  313. dma_addr_t addr,
  314. int size,
  315. u32 lcsp13, /* src or dst*/
  316. u32 data_width1,
  317. u32 data_width2,
  318. unsigned int flags)
  319. {
  320. bool addr_inc = flags & LLI_ADDR_INC;
  321. struct d40_log_lli *lli = lli_sg;
  322. int size_rest = size;
  323. int size_seg = 0;
  324. do {
  325. size_seg = d40_seg_size(size_rest, data_width1, data_width2);
  326. size_rest -= size_seg;
  327. d40_log_fill_lli(lli,
  328. addr,
  329. size_seg,
  330. lcsp13, data_width1,
  331. flags);
  332. if (addr_inc)
  333. addr += size_seg;
  334. lli++;
  335. } while (size_rest);
  336. return lli;
  337. }
  338. int d40_log_sg_to_lli(struct scatterlist *sg,
  339. int sg_len,
  340. dma_addr_t dev_addr,
  341. struct d40_log_lli *lli_sg,
  342. u32 lcsp13, /* src or dst*/
  343. u32 data_width1, u32 data_width2)
  344. {
  345. int total_size = 0;
  346. struct scatterlist *current_sg = sg;
  347. int i;
  348. struct d40_log_lli *lli = lli_sg;
  349. unsigned long flags = 0;
  350. if (!dev_addr)
  351. flags |= LLI_ADDR_INC;
  352. for_each_sg(sg, current_sg, sg_len, i) {
  353. dma_addr_t sg_addr = sg_dma_address(current_sg);
  354. unsigned int len = sg_dma_len(current_sg);
  355. dma_addr_t addr = dev_addr ?: sg_addr;
  356. total_size += sg_dma_len(current_sg);
  357. lli = d40_log_buf_to_lli(lli, addr, len,
  358. lcsp13,
  359. data_width1,
  360. data_width2,
  361. flags);
  362. }
  363. return total_size;
  364. }