ste_dma40_ll.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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_TIM_POS;
  33. l3 |= 1 << D40_MEM_LCSP3_DCFG_EIM_POS;
  34. l3 |= cfg->dst_info.psize << D40_MEM_LCSP3_DCFG_PSIZE_POS;
  35. l3 |= cfg->dst_info.data_width << D40_MEM_LCSP3_DCFG_ESIZE_POS;
  36. l3 |= 1 << D40_MEM_LCSP3_DTCP_POS;
  37. l1 |= 1 << D40_MEM_LCSP1_SCFG_EIM_POS;
  38. l1 |= cfg->src_info.psize << D40_MEM_LCSP1_SCFG_PSIZE_POS;
  39. l1 |= cfg->src_info.data_width << D40_MEM_LCSP1_SCFG_ESIZE_POS;
  40. l1 |= 1 << D40_MEM_LCSP1_STCP_POS;
  41. *lcsp1 = l1;
  42. *lcsp3 = l3;
  43. }
  44. /* Sets up SRC and DST CFG register for both logical and physical channels */
  45. void d40_phy_cfg(struct stedma40_chan_cfg *cfg,
  46. u32 *src_cfg, u32 *dst_cfg, bool is_log)
  47. {
  48. u32 src = 0;
  49. u32 dst = 0;
  50. if (!is_log) {
  51. /* Physical channel */
  52. if ((cfg->dir == STEDMA40_PERIPH_TO_MEM) ||
  53. (cfg->dir == STEDMA40_PERIPH_TO_PERIPH)) {
  54. /* Set master port to 1 */
  55. src |= 1 << D40_SREG_CFG_MST_POS;
  56. src |= D40_TYPE_TO_EVENT(cfg->src_dev_type);
  57. if (cfg->src_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
  58. src |= 1 << D40_SREG_CFG_PHY_TM_POS;
  59. else
  60. src |= 3 << D40_SREG_CFG_PHY_TM_POS;
  61. }
  62. if ((cfg->dir == STEDMA40_MEM_TO_PERIPH) ||
  63. (cfg->dir == STEDMA40_PERIPH_TO_PERIPH)) {
  64. /* Set master port to 1 */
  65. dst |= 1 << D40_SREG_CFG_MST_POS;
  66. dst |= D40_TYPE_TO_EVENT(cfg->dst_dev_type);
  67. if (cfg->dst_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
  68. dst |= 1 << D40_SREG_CFG_PHY_TM_POS;
  69. else
  70. dst |= 3 << D40_SREG_CFG_PHY_TM_POS;
  71. }
  72. /* Interrupt on end of transfer for destination */
  73. dst |= 1 << D40_SREG_CFG_TIM_POS;
  74. /* Generate interrupt on error */
  75. src |= 1 << D40_SREG_CFG_EIM_POS;
  76. dst |= 1 << D40_SREG_CFG_EIM_POS;
  77. /* PSIZE */
  78. if (cfg->src_info.psize != STEDMA40_PSIZE_PHY_1) {
  79. src |= 1 << D40_SREG_CFG_PHY_PEN_POS;
  80. src |= cfg->src_info.psize << D40_SREG_CFG_PSIZE_POS;
  81. }
  82. if (cfg->dst_info.psize != STEDMA40_PSIZE_PHY_1) {
  83. dst |= 1 << D40_SREG_CFG_PHY_PEN_POS;
  84. dst |= cfg->dst_info.psize << D40_SREG_CFG_PSIZE_POS;
  85. }
  86. /* Element size */
  87. src |= cfg->src_info.data_width << D40_SREG_CFG_ESIZE_POS;
  88. dst |= cfg->dst_info.data_width << D40_SREG_CFG_ESIZE_POS;
  89. } else {
  90. /* Logical channel */
  91. dst |= 1 << D40_SREG_CFG_LOG_GIM_POS;
  92. src |= 1 << D40_SREG_CFG_LOG_GIM_POS;
  93. }
  94. if (cfg->channel_type & STEDMA40_HIGH_PRIORITY_CHANNEL) {
  95. src |= 1 << D40_SREG_CFG_PRI_POS;
  96. dst |= 1 << D40_SREG_CFG_PRI_POS;
  97. }
  98. src |= cfg->src_info.endianess << D40_SREG_CFG_LBE_POS;
  99. dst |= cfg->dst_info.endianess << D40_SREG_CFG_LBE_POS;
  100. *src_cfg = src;
  101. *dst_cfg = dst;
  102. }
  103. int d40_phy_fill_lli(struct d40_phy_lli *lli,
  104. dma_addr_t data,
  105. u32 data_size,
  106. int psize,
  107. dma_addr_t next_lli,
  108. u32 reg_cfg,
  109. bool term_int,
  110. u32 data_width,
  111. bool is_device)
  112. {
  113. int num_elems;
  114. if (psize == STEDMA40_PSIZE_PHY_1)
  115. num_elems = 1;
  116. else
  117. num_elems = 2 << psize;
  118. /*
  119. * Size is 16bit. data_width is 8, 16, 32 or 64 bit
  120. * Block large than 64 KiB must be split.
  121. */
  122. if (data_size > (0xffff << data_width))
  123. return -EINVAL;
  124. /* Must be aligned */
  125. if (!IS_ALIGNED(data, 0x1 << data_width))
  126. return -EINVAL;
  127. /* Transfer size can't be smaller than (num_elms * elem_size) */
  128. if (data_size < num_elems * (0x1 << data_width))
  129. return -EINVAL;
  130. /* The number of elements. IE now many chunks */
  131. lli->reg_elt = (data_size >> data_width) << D40_SREG_ELEM_PHY_ECNT_POS;
  132. /*
  133. * Distance to next element sized entry.
  134. * Usually the size of the element unless you want gaps.
  135. */
  136. if (!is_device)
  137. lli->reg_elt |= (0x1 << data_width) <<
  138. D40_SREG_ELEM_PHY_EIDX_POS;
  139. /* Where the data is */
  140. lli->reg_ptr = data;
  141. lli->reg_cfg = reg_cfg;
  142. /* If this scatter list entry is the last one, no next link */
  143. if (next_lli == 0)
  144. lli->reg_lnk = 0x1 << D40_SREG_LNK_PHY_TCP_POS;
  145. else
  146. lli->reg_lnk = next_lli;
  147. /* Set/clear interrupt generation on this link item.*/
  148. if (term_int)
  149. lli->reg_cfg |= 0x1 << D40_SREG_CFG_TIM_POS;
  150. else
  151. lli->reg_cfg &= ~(0x1 << D40_SREG_CFG_TIM_POS);
  152. /* Post link */
  153. lli->reg_lnk |= 0 << D40_SREG_LNK_PHY_PRE_POS;
  154. return 0;
  155. }
  156. int d40_phy_sg_to_lli(struct scatterlist *sg,
  157. int sg_len,
  158. dma_addr_t target,
  159. struct d40_phy_lli *lli,
  160. dma_addr_t lli_phys,
  161. u32 reg_cfg,
  162. u32 data_width,
  163. int psize)
  164. {
  165. int total_size = 0;
  166. int i;
  167. struct scatterlist *current_sg = sg;
  168. dma_addr_t next_lli_phys;
  169. dma_addr_t dst;
  170. int err = 0;
  171. for_each_sg(sg, current_sg, sg_len, i) {
  172. total_size += sg_dma_len(current_sg);
  173. /* If this scatter list entry is the last one, no next link */
  174. if (sg_len - 1 == i)
  175. next_lli_phys = 0;
  176. else
  177. next_lli_phys = ALIGN(lli_phys + (i + 1) *
  178. sizeof(struct d40_phy_lli),
  179. D40_LLI_ALIGN);
  180. if (target)
  181. dst = target;
  182. else
  183. dst = sg_phys(current_sg);
  184. err = d40_phy_fill_lli(&lli[i],
  185. dst,
  186. sg_dma_len(current_sg),
  187. psize,
  188. next_lli_phys,
  189. reg_cfg,
  190. !next_lli_phys,
  191. data_width,
  192. target == dst);
  193. if (err)
  194. goto err;
  195. }
  196. return total_size;
  197. err:
  198. return err;
  199. }
  200. void d40_phy_lli_write(void __iomem *virtbase,
  201. u32 phy_chan_num,
  202. struct d40_phy_lli *lli_dst,
  203. struct d40_phy_lli *lli_src)
  204. {
  205. writel(lli_src->reg_cfg, virtbase + D40_DREG_PCBASE +
  206. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSCFG);
  207. writel(lli_src->reg_elt, virtbase + D40_DREG_PCBASE +
  208. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSELT);
  209. writel(lli_src->reg_ptr, virtbase + D40_DREG_PCBASE +
  210. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSPTR);
  211. writel(lli_src->reg_lnk, virtbase + D40_DREG_PCBASE +
  212. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSLNK);
  213. writel(lli_dst->reg_cfg, virtbase + D40_DREG_PCBASE +
  214. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDCFG);
  215. writel(lli_dst->reg_elt, virtbase + D40_DREG_PCBASE +
  216. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDELT);
  217. writel(lli_dst->reg_ptr, virtbase + D40_DREG_PCBASE +
  218. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDPTR);
  219. writel(lli_dst->reg_lnk, virtbase + D40_DREG_PCBASE +
  220. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDLNK);
  221. }
  222. /* DMA logical lli operations */
  223. void d40_log_fill_lli(struct d40_log_lli *lli,
  224. dma_addr_t data, u32 data_size,
  225. u32 lli_next_off, u32 reg_cfg,
  226. u32 data_width,
  227. bool term_int, bool addr_inc)
  228. {
  229. lli->lcsp13 = reg_cfg;
  230. /* The number of elements to transfer */
  231. lli->lcsp02 = ((data_size >> data_width) <<
  232. D40_MEM_LCSP0_ECNT_POS) & D40_MEM_LCSP0_ECNT_MASK;
  233. /* 16 LSBs address of the current element */
  234. lli->lcsp02 |= data & D40_MEM_LCSP0_SPTR_MASK;
  235. /* 16 MSBs address of the current element */
  236. lli->lcsp13 |= data & D40_MEM_LCSP1_SPTR_MASK;
  237. if (addr_inc)
  238. lli->lcsp13 |= D40_MEM_LCSP1_SCFG_INCR_MASK;
  239. lli->lcsp13 |= D40_MEM_LCSP3_DTCP_MASK;
  240. /* If this scatter list entry is the last one, no next link */
  241. lli->lcsp13 |= (lli_next_off << D40_MEM_LCSP1_SLOS_POS) &
  242. D40_MEM_LCSP1_SLOS_MASK;
  243. if (term_int)
  244. lli->lcsp13 |= D40_MEM_LCSP1_SCFG_TIM_MASK;
  245. else
  246. lli->lcsp13 &= ~D40_MEM_LCSP1_SCFG_TIM_MASK;
  247. }
  248. int d40_log_sg_to_dev(struct d40_lcla_elem *lcla,
  249. struct scatterlist *sg,
  250. int sg_len,
  251. struct d40_log_lli_bidir *lli,
  252. struct d40_def_lcsp *lcsp,
  253. u32 src_data_width,
  254. u32 dst_data_width,
  255. enum dma_data_direction direction,
  256. dma_addr_t dev_addr, int max_len,
  257. int llis_per_log)
  258. {
  259. int total_size = 0;
  260. struct scatterlist *current_sg = sg;
  261. int i;
  262. u32 next_lli_off_dst = 0;
  263. u32 next_lli_off_src = 0;
  264. for_each_sg(sg, current_sg, sg_len, i) {
  265. total_size += sg_dma_len(current_sg);
  266. /*
  267. * If this scatter list entry is the last one or
  268. * max length, terminate link.
  269. */
  270. if (sg_len - 1 == i || ((i+1) % max_len == 0)) {
  271. next_lli_off_src = 0;
  272. next_lli_off_dst = 0;
  273. } else {
  274. if (next_lli_off_dst == 0 &&
  275. next_lli_off_src == 0) {
  276. /* The first lli will be at next_lli_off */
  277. next_lli_off_dst = (lcla->dst_id *
  278. llis_per_log + 1);
  279. next_lli_off_src = (lcla->src_id *
  280. llis_per_log + 1);
  281. } else {
  282. next_lli_off_dst++;
  283. next_lli_off_src++;
  284. }
  285. }
  286. if (direction == DMA_TO_DEVICE) {
  287. d40_log_fill_lli(&lli->src[i],
  288. sg_phys(current_sg),
  289. sg_dma_len(current_sg),
  290. next_lli_off_src,
  291. lcsp->lcsp1, src_data_width,
  292. false,
  293. true);
  294. d40_log_fill_lli(&lli->dst[i],
  295. dev_addr,
  296. sg_dma_len(current_sg),
  297. next_lli_off_dst,
  298. lcsp->lcsp3, dst_data_width,
  299. /* No next == terminal interrupt */
  300. !next_lli_off_dst,
  301. false);
  302. } else {
  303. d40_log_fill_lli(&lli->dst[i],
  304. sg_phys(current_sg),
  305. sg_dma_len(current_sg),
  306. next_lli_off_dst,
  307. lcsp->lcsp3, dst_data_width,
  308. /* No next == terminal interrupt */
  309. !next_lli_off_dst,
  310. true);
  311. d40_log_fill_lli(&lli->src[i],
  312. dev_addr,
  313. sg_dma_len(current_sg),
  314. next_lli_off_src,
  315. lcsp->lcsp1, src_data_width,
  316. false,
  317. false);
  318. }
  319. }
  320. return total_size;
  321. }
  322. int d40_log_sg_to_lli(int lcla_id,
  323. struct scatterlist *sg,
  324. int sg_len,
  325. struct d40_log_lli *lli_sg,
  326. u32 lcsp13, /* src or dst*/
  327. u32 data_width,
  328. int max_len, int llis_per_log)
  329. {
  330. int total_size = 0;
  331. struct scatterlist *current_sg = sg;
  332. int i;
  333. u32 next_lli_off = 0;
  334. for_each_sg(sg, current_sg, sg_len, i) {
  335. total_size += sg_dma_len(current_sg);
  336. /*
  337. * If this scatter list entry is the last one or
  338. * max length, terminate link.
  339. */
  340. if (sg_len - 1 == i || ((i+1) % max_len == 0))
  341. next_lli_off = 0;
  342. else {
  343. if (next_lli_off == 0)
  344. /* The first lli will be at next_lli_off */
  345. next_lli_off = lcla_id * llis_per_log + 1;
  346. else
  347. next_lli_off++;
  348. }
  349. d40_log_fill_lli(&lli_sg[i],
  350. sg_phys(current_sg),
  351. sg_dma_len(current_sg),
  352. next_lli_off,
  353. lcsp13, data_width,
  354. !next_lli_off,
  355. true);
  356. }
  357. return total_size;
  358. }
  359. int d40_log_lli_write(struct d40_log_lli_full *lcpa,
  360. struct d40_log_lli *lcla_src,
  361. struct d40_log_lli *lcla_dst,
  362. struct d40_log_lli *lli_dst,
  363. struct d40_log_lli *lli_src,
  364. int llis_per_log)
  365. {
  366. u32 slos;
  367. u32 dlos;
  368. int i;
  369. writel(lli_src->lcsp02, &lcpa->lcsp0);
  370. writel(lli_src->lcsp13, &lcpa->lcsp1);
  371. writel(lli_dst->lcsp02, &lcpa->lcsp2);
  372. writel(lli_dst->lcsp13, &lcpa->lcsp3);
  373. slos = lli_src->lcsp13 & D40_MEM_LCSP1_SLOS_MASK;
  374. dlos = lli_dst->lcsp13 & D40_MEM_LCSP3_DLOS_MASK;
  375. for (i = 0; (i < llis_per_log) && slos && dlos; i++) {
  376. writel(lli_src[i + 1].lcsp02, &lcla_src[i].lcsp02);
  377. writel(lli_src[i + 1].lcsp13, &lcla_src[i].lcsp13);
  378. writel(lli_dst[i + 1].lcsp02, &lcla_dst[i].lcsp02);
  379. writel(lli_dst[i + 1].lcsp13, &lcla_dst[i].lcsp13);
  380. slos = lli_src[i + 1].lcsp13 & D40_MEM_LCSP1_SLOS_MASK;
  381. dlos = lli_dst[i + 1].lcsp13 & D40_MEM_LCSP3_DLOS_MASK;
  382. }
  383. return i;
  384. }