ste_dma40_ll.c 12 KB

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