ste_dma40_ll.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2007-2010
  3. * Author: Per Friden <per.friden@stericsson.com> for ST-Ericsson
  4. * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
  5. * License terms: GNU General Public License (GPL) version 2
  6. */
  7. #include <linux/kernel.h>
  8. #include <plat/ste_dma40.h>
  9. #include "ste_dma40_ll.h"
  10. /* Sets up proper LCSP1 and LCSP3 register for a logical channel */
  11. void d40_log_cfg(struct stedma40_chan_cfg *cfg,
  12. u32 *lcsp1, u32 *lcsp3)
  13. {
  14. u32 l3 = 0; /* dst */
  15. u32 l1 = 0; /* src */
  16. /* src is mem? -> increase address pos */
  17. if (cfg->dir == STEDMA40_MEM_TO_PERIPH ||
  18. cfg->dir == STEDMA40_MEM_TO_MEM)
  19. l1 |= 1 << D40_MEM_LCSP1_SCFG_INCR_POS;
  20. /* dst is mem? -> increase address pos */
  21. if (cfg->dir == STEDMA40_PERIPH_TO_MEM ||
  22. cfg->dir == STEDMA40_MEM_TO_MEM)
  23. l3 |= 1 << D40_MEM_LCSP3_DCFG_INCR_POS;
  24. /* src is hw? -> master port 1 */
  25. if (cfg->dir == STEDMA40_PERIPH_TO_MEM ||
  26. cfg->dir == STEDMA40_PERIPH_TO_PERIPH)
  27. l1 |= 1 << D40_MEM_LCSP1_SCFG_MST_POS;
  28. /* dst is hw? -> master port 1 */
  29. if (cfg->dir == STEDMA40_MEM_TO_PERIPH ||
  30. cfg->dir == STEDMA40_PERIPH_TO_PERIPH)
  31. l3 |= 1 << D40_MEM_LCSP3_DCFG_MST_POS;
  32. l3 |= 1 << D40_MEM_LCSP3_DCFG_EIM_POS;
  33. l3 |= cfg->dst_info.psize << D40_MEM_LCSP3_DCFG_PSIZE_POS;
  34. l3 |= cfg->dst_info.data_width << D40_MEM_LCSP3_DCFG_ESIZE_POS;
  35. l1 |= 1 << D40_MEM_LCSP1_SCFG_EIM_POS;
  36. l1 |= cfg->src_info.psize << D40_MEM_LCSP1_SCFG_PSIZE_POS;
  37. l1 |= cfg->src_info.data_width << D40_MEM_LCSP1_SCFG_ESIZE_POS;
  38. *lcsp1 = l1;
  39. *lcsp3 = l3;
  40. }
  41. /* Sets up SRC and DST CFG register for both logical and physical channels */
  42. void d40_phy_cfg(struct stedma40_chan_cfg *cfg,
  43. u32 *src_cfg, u32 *dst_cfg, bool is_log)
  44. {
  45. u32 src = 0;
  46. u32 dst = 0;
  47. if (!is_log) {
  48. /* Physical channel */
  49. if ((cfg->dir == STEDMA40_PERIPH_TO_MEM) ||
  50. (cfg->dir == STEDMA40_PERIPH_TO_PERIPH)) {
  51. /* Set master port to 1 */
  52. src |= 1 << D40_SREG_CFG_MST_POS;
  53. src |= D40_TYPE_TO_EVENT(cfg->src_dev_type);
  54. if (cfg->src_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
  55. src |= 1 << D40_SREG_CFG_PHY_TM_POS;
  56. else
  57. src |= 3 << D40_SREG_CFG_PHY_TM_POS;
  58. }
  59. if ((cfg->dir == STEDMA40_MEM_TO_PERIPH) ||
  60. (cfg->dir == STEDMA40_PERIPH_TO_PERIPH)) {
  61. /* Set master port to 1 */
  62. dst |= 1 << D40_SREG_CFG_MST_POS;
  63. dst |= D40_TYPE_TO_EVENT(cfg->dst_dev_type);
  64. if (cfg->dst_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
  65. dst |= 1 << D40_SREG_CFG_PHY_TM_POS;
  66. else
  67. dst |= 3 << D40_SREG_CFG_PHY_TM_POS;
  68. }
  69. /* Interrupt on end of transfer for destination */
  70. dst |= 1 << D40_SREG_CFG_TIM_POS;
  71. /* Generate interrupt on error */
  72. src |= 1 << D40_SREG_CFG_EIM_POS;
  73. dst |= 1 << D40_SREG_CFG_EIM_POS;
  74. /* PSIZE */
  75. if (cfg->src_info.psize != STEDMA40_PSIZE_PHY_1) {
  76. src |= 1 << D40_SREG_CFG_PHY_PEN_POS;
  77. src |= cfg->src_info.psize << D40_SREG_CFG_PSIZE_POS;
  78. }
  79. if (cfg->dst_info.psize != STEDMA40_PSIZE_PHY_1) {
  80. dst |= 1 << D40_SREG_CFG_PHY_PEN_POS;
  81. dst |= cfg->dst_info.psize << D40_SREG_CFG_PSIZE_POS;
  82. }
  83. /* Element size */
  84. src |= cfg->src_info.data_width << D40_SREG_CFG_ESIZE_POS;
  85. dst |= cfg->dst_info.data_width << D40_SREG_CFG_ESIZE_POS;
  86. } else {
  87. /* Logical channel */
  88. dst |= 1 << D40_SREG_CFG_LOG_GIM_POS;
  89. src |= 1 << D40_SREG_CFG_LOG_GIM_POS;
  90. }
  91. if (cfg->high_priority) {
  92. src |= 1 << D40_SREG_CFG_PRI_POS;
  93. dst |= 1 << D40_SREG_CFG_PRI_POS;
  94. }
  95. src |= cfg->src_info.endianess << D40_SREG_CFG_LBE_POS;
  96. dst |= cfg->dst_info.endianess << D40_SREG_CFG_LBE_POS;
  97. *src_cfg = src;
  98. *dst_cfg = dst;
  99. }
  100. int d40_phy_fill_lli(struct d40_phy_lli *lli,
  101. dma_addr_t data,
  102. u32 data_size,
  103. int psize,
  104. dma_addr_t next_lli,
  105. u32 reg_cfg,
  106. bool term_int,
  107. u32 data_width,
  108. bool is_device)
  109. {
  110. int num_elems;
  111. if (psize == STEDMA40_PSIZE_PHY_1)
  112. num_elems = 1;
  113. else
  114. num_elems = 2 << psize;
  115. /*
  116. * Size is 16bit. data_width is 8, 16, 32 or 64 bit
  117. * Block large than 64 KiB must be split.
  118. */
  119. if (data_size > (0xffff << data_width))
  120. return -EINVAL;
  121. /* Must be aligned */
  122. if (!IS_ALIGNED(data, 0x1 << data_width))
  123. return -EINVAL;
  124. /* Transfer size can't be smaller than (num_elms * elem_size) */
  125. if (data_size < num_elems * (0x1 << data_width))
  126. return -EINVAL;
  127. /* The number of elements. IE now many chunks */
  128. lli->reg_elt = (data_size >> data_width) << D40_SREG_ELEM_PHY_ECNT_POS;
  129. /*
  130. * Distance to next element sized entry.
  131. * Usually the size of the element unless you want gaps.
  132. */
  133. if (!is_device)
  134. lli->reg_elt |= (0x1 << data_width) <<
  135. D40_SREG_ELEM_PHY_EIDX_POS;
  136. /* Where the data is */
  137. lli->reg_ptr = data;
  138. lli->reg_cfg = reg_cfg;
  139. /* If this scatter list entry is the last one, no next link */
  140. if (next_lli == 0)
  141. lli->reg_lnk = 0x1 << D40_SREG_LNK_PHY_TCP_POS;
  142. else
  143. lli->reg_lnk = next_lli;
  144. /* Set/clear interrupt generation on this link item.*/
  145. if (term_int)
  146. lli->reg_cfg |= 0x1 << D40_SREG_CFG_TIM_POS;
  147. else
  148. lli->reg_cfg &= ~(0x1 << D40_SREG_CFG_TIM_POS);
  149. /* Post link */
  150. lli->reg_lnk |= 0 << D40_SREG_LNK_PHY_PRE_POS;
  151. return 0;
  152. }
  153. int d40_phy_sg_to_lli(struct scatterlist *sg,
  154. int sg_len,
  155. dma_addr_t target,
  156. struct d40_phy_lli *lli,
  157. dma_addr_t lli_phys,
  158. u32 reg_cfg,
  159. u32 data_width,
  160. int psize)
  161. {
  162. int total_size = 0;
  163. int i;
  164. struct scatterlist *current_sg = sg;
  165. dma_addr_t next_lli_phys;
  166. dma_addr_t dst;
  167. int err = 0;
  168. for_each_sg(sg, current_sg, sg_len, i) {
  169. total_size += sg_dma_len(current_sg);
  170. /* If this scatter list entry is the last one, no next link */
  171. if (sg_len - 1 == i)
  172. next_lli_phys = 0;
  173. else
  174. next_lli_phys = ALIGN(lli_phys + (i + 1) *
  175. sizeof(struct d40_phy_lli),
  176. D40_LLI_ALIGN);
  177. if (target)
  178. dst = target;
  179. else
  180. dst = sg_phys(current_sg);
  181. err = d40_phy_fill_lli(&lli[i],
  182. dst,
  183. sg_dma_len(current_sg),
  184. psize,
  185. next_lli_phys,
  186. reg_cfg,
  187. !next_lli_phys,
  188. data_width,
  189. target == dst);
  190. if (err)
  191. goto err;
  192. }
  193. return total_size;
  194. err:
  195. return err;
  196. }
  197. void d40_phy_lli_write(void __iomem *virtbase,
  198. u32 phy_chan_num,
  199. struct d40_phy_lli *lli_dst,
  200. struct d40_phy_lli *lli_src)
  201. {
  202. writel(lli_src->reg_cfg, virtbase + D40_DREG_PCBASE +
  203. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSCFG);
  204. writel(lli_src->reg_elt, virtbase + D40_DREG_PCBASE +
  205. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSELT);
  206. writel(lli_src->reg_ptr, virtbase + D40_DREG_PCBASE +
  207. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSPTR);
  208. writel(lli_src->reg_lnk, virtbase + D40_DREG_PCBASE +
  209. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSLNK);
  210. writel(lli_dst->reg_cfg, virtbase + D40_DREG_PCBASE +
  211. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDCFG);
  212. writel(lli_dst->reg_elt, virtbase + D40_DREG_PCBASE +
  213. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDELT);
  214. writel(lli_dst->reg_ptr, virtbase + D40_DREG_PCBASE +
  215. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDPTR);
  216. writel(lli_dst->reg_lnk, virtbase + D40_DREG_PCBASE +
  217. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDLNK);
  218. }
  219. /* DMA logical lli operations */
  220. static void d40_log_lli_link(struct d40_log_lli *lli_dst,
  221. struct d40_log_lli *lli_src,
  222. int next)
  223. {
  224. u32 slos = 0;
  225. u32 dlos = 0;
  226. if (next != -EINVAL) {
  227. slos = next * 2;
  228. dlos = next * 2 + 1;
  229. } else {
  230. lli_dst->lcsp13 |= D40_MEM_LCSP1_SCFG_TIM_MASK;
  231. lli_dst->lcsp13 |= D40_MEM_LCSP3_DTCP_MASK;
  232. }
  233. lli_src->lcsp13 = (lli_src->lcsp13 & ~D40_MEM_LCSP1_SLOS_MASK) |
  234. (slos << D40_MEM_LCSP1_SLOS_POS);
  235. lli_dst->lcsp13 = (lli_dst->lcsp13 & ~D40_MEM_LCSP1_SLOS_MASK) |
  236. (dlos << D40_MEM_LCSP1_SLOS_POS);
  237. }
  238. void d40_log_lli_lcpa_write(struct d40_log_lli_full *lcpa,
  239. struct d40_log_lli *lli_dst,
  240. struct d40_log_lli *lli_src,
  241. int next)
  242. {
  243. d40_log_lli_link(lli_dst, lli_src, next);
  244. writel(lli_src->lcsp02, &lcpa[0].lcsp0);
  245. writel(lli_src->lcsp13, &lcpa[0].lcsp1);
  246. writel(lli_dst->lcsp02, &lcpa[0].lcsp2);
  247. writel(lli_dst->lcsp13, &lcpa[0].lcsp3);
  248. }
  249. void d40_log_lli_lcla_write(struct d40_log_lli *lcla,
  250. struct d40_log_lli *lli_dst,
  251. struct d40_log_lli *lli_src,
  252. int next)
  253. {
  254. d40_log_lli_link(lli_dst, lli_src, next);
  255. writel(lli_src->lcsp02, &lcla[0].lcsp02);
  256. writel(lli_src->lcsp13, &lcla[0].lcsp13);
  257. writel(lli_dst->lcsp02, &lcla[1].lcsp02);
  258. writel(lli_dst->lcsp13, &lcla[1].lcsp13);
  259. }
  260. void d40_log_fill_lli(struct d40_log_lli *lli,
  261. dma_addr_t data, u32 data_size,
  262. u32 reg_cfg,
  263. u32 data_width,
  264. bool addr_inc)
  265. {
  266. lli->lcsp13 = reg_cfg;
  267. /* The number of elements to transfer */
  268. lli->lcsp02 = ((data_size >> data_width) <<
  269. D40_MEM_LCSP0_ECNT_POS) & D40_MEM_LCSP0_ECNT_MASK;
  270. /* 16 LSBs address of the current element */
  271. lli->lcsp02 |= data & D40_MEM_LCSP0_SPTR_MASK;
  272. /* 16 MSBs address of the current element */
  273. lli->lcsp13 |= data & D40_MEM_LCSP1_SPTR_MASK;
  274. if (addr_inc)
  275. lli->lcsp13 |= D40_MEM_LCSP1_SCFG_INCR_MASK;
  276. }
  277. int d40_log_sg_to_dev(struct scatterlist *sg,
  278. int sg_len,
  279. struct d40_log_lli_bidir *lli,
  280. struct d40_def_lcsp *lcsp,
  281. u32 src_data_width,
  282. u32 dst_data_width,
  283. enum dma_data_direction direction,
  284. dma_addr_t dev_addr)
  285. {
  286. int total_size = 0;
  287. struct scatterlist *current_sg = sg;
  288. int i;
  289. for_each_sg(sg, current_sg, sg_len, i) {
  290. total_size += sg_dma_len(current_sg);
  291. if (direction == DMA_TO_DEVICE) {
  292. d40_log_fill_lli(&lli->src[i],
  293. sg_phys(current_sg),
  294. sg_dma_len(current_sg),
  295. lcsp->lcsp1, src_data_width,
  296. true);
  297. d40_log_fill_lli(&lli->dst[i],
  298. dev_addr,
  299. sg_dma_len(current_sg),
  300. lcsp->lcsp3, dst_data_width,
  301. false);
  302. } else {
  303. d40_log_fill_lli(&lli->dst[i],
  304. sg_phys(current_sg),
  305. sg_dma_len(current_sg),
  306. lcsp->lcsp3, dst_data_width,
  307. true);
  308. d40_log_fill_lli(&lli->src[i],
  309. dev_addr,
  310. sg_dma_len(current_sg),
  311. lcsp->lcsp1, src_data_width,
  312. false);
  313. }
  314. }
  315. return total_size;
  316. }
  317. int d40_log_sg_to_lli(struct scatterlist *sg,
  318. int sg_len,
  319. struct d40_log_lli *lli_sg,
  320. u32 lcsp13, /* src or dst*/
  321. u32 data_width)
  322. {
  323. int total_size = 0;
  324. struct scatterlist *current_sg = sg;
  325. int i;
  326. for_each_sg(sg, current_sg, sg_len, i) {
  327. total_size += sg_dma_len(current_sg);
  328. d40_log_fill_lli(&lli_sg[i],
  329. sg_phys(current_sg),
  330. sg_dma_len(current_sg),
  331. lcsp13, data_width,
  332. true);
  333. }
  334. return total_size;
  335. }