ste_dma40_ll.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. bool term_int)
  167. {
  168. int total_size = 0;
  169. int i;
  170. struct scatterlist *current_sg = sg;
  171. dma_addr_t next_lli_phys;
  172. dma_addr_t dst;
  173. int err = 0;
  174. for_each_sg(sg, current_sg, sg_len, i) {
  175. total_size += sg_dma_len(current_sg);
  176. /* If this scatter list entry is the last one, no next link */
  177. if (sg_len - 1 == i)
  178. next_lli_phys = 0;
  179. else
  180. next_lli_phys = ALIGN(lli_phys + (i + 1) *
  181. sizeof(struct d40_phy_lli),
  182. D40_LLI_ALIGN);
  183. if (target)
  184. dst = target;
  185. else
  186. dst = sg_phys(current_sg);
  187. err = d40_phy_fill_lli(&lli[i],
  188. dst,
  189. sg_dma_len(current_sg),
  190. psize,
  191. next_lli_phys,
  192. reg_cfg,
  193. !next_lli_phys,
  194. data_width,
  195. target == dst);
  196. if (err)
  197. goto err;
  198. }
  199. return total_size;
  200. err:
  201. return err;
  202. }
  203. void d40_phy_lli_write(void __iomem *virtbase,
  204. u32 phy_chan_num,
  205. struct d40_phy_lli *lli_dst,
  206. struct d40_phy_lli *lli_src)
  207. {
  208. writel(lli_src->reg_cfg, virtbase + D40_DREG_PCBASE +
  209. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSCFG);
  210. writel(lli_src->reg_elt, virtbase + D40_DREG_PCBASE +
  211. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSELT);
  212. writel(lli_src->reg_ptr, virtbase + D40_DREG_PCBASE +
  213. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSPTR);
  214. writel(lli_src->reg_lnk, virtbase + D40_DREG_PCBASE +
  215. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SSLNK);
  216. writel(lli_dst->reg_cfg, virtbase + D40_DREG_PCBASE +
  217. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDCFG);
  218. writel(lli_dst->reg_elt, virtbase + D40_DREG_PCBASE +
  219. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDELT);
  220. writel(lli_dst->reg_ptr, virtbase + D40_DREG_PCBASE +
  221. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDPTR);
  222. writel(lli_dst->reg_lnk, virtbase + D40_DREG_PCBASE +
  223. phy_chan_num * D40_DREG_PCDELTA + D40_CHAN_REG_SDLNK);
  224. }
  225. /* DMA logical lli operations */
  226. void d40_log_fill_lli(struct d40_log_lli *lli,
  227. dma_addr_t data, u32 data_size,
  228. u32 lli_next_off, u32 reg_cfg,
  229. u32 data_width,
  230. bool term_int, bool addr_inc)
  231. {
  232. lli->lcsp13 = reg_cfg;
  233. /* The number of elements to transfer */
  234. lli->lcsp02 = ((data_size >> data_width) <<
  235. D40_MEM_LCSP0_ECNT_POS) & D40_MEM_LCSP0_ECNT_MASK;
  236. /* 16 LSBs address of the current element */
  237. lli->lcsp02 |= data & D40_MEM_LCSP0_SPTR_MASK;
  238. /* 16 MSBs address of the current element */
  239. lli->lcsp13 |= data & D40_MEM_LCSP1_SPTR_MASK;
  240. if (addr_inc)
  241. lli->lcsp13 |= D40_MEM_LCSP1_SCFG_INCR_MASK;
  242. lli->lcsp13 |= D40_MEM_LCSP3_DTCP_MASK;
  243. /* If this scatter list entry is the last one, no next link */
  244. lli->lcsp13 |= (lli_next_off << D40_MEM_LCSP1_SLOS_POS) &
  245. D40_MEM_LCSP1_SLOS_MASK;
  246. if (term_int)
  247. lli->lcsp13 |= D40_MEM_LCSP1_SCFG_TIM_MASK;
  248. else
  249. lli->lcsp13 &= ~D40_MEM_LCSP1_SCFG_TIM_MASK;
  250. }
  251. int d40_log_sg_to_dev(struct d40_lcla_elem *lcla,
  252. struct scatterlist *sg,
  253. int sg_len,
  254. struct d40_log_lli_bidir *lli,
  255. struct d40_def_lcsp *lcsp,
  256. u32 src_data_width,
  257. u32 dst_data_width,
  258. enum dma_data_direction direction,
  259. bool term_int, dma_addr_t dev_addr, int max_len,
  260. int llis_per_log)
  261. {
  262. int total_size = 0;
  263. struct scatterlist *current_sg = sg;
  264. int i;
  265. u32 next_lli_off_dst;
  266. u32 next_lli_off_src;
  267. next_lli_off_src = 0;
  268. next_lli_off_dst = 0;
  269. for_each_sg(sg, current_sg, sg_len, i) {
  270. total_size += sg_dma_len(current_sg);
  271. /*
  272. * If this scatter list entry is the last one or
  273. * max length, terminate link.
  274. */
  275. if (sg_len - 1 == i || ((i+1) % max_len == 0)) {
  276. next_lli_off_src = 0;
  277. next_lli_off_dst = 0;
  278. } else {
  279. if (next_lli_off_dst == 0 &&
  280. next_lli_off_src == 0) {
  281. /* The first lli will be at next_lli_off */
  282. next_lli_off_dst = (lcla->dst_id *
  283. llis_per_log + 1);
  284. next_lli_off_src = (lcla->src_id *
  285. llis_per_log + 1);
  286. } else {
  287. next_lli_off_dst++;
  288. next_lli_off_src++;
  289. }
  290. }
  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. next_lli_off_src,
  296. lcsp->lcsp1, src_data_width,
  297. term_int && !next_lli_off_src,
  298. true);
  299. d40_log_fill_lli(&lli->dst[i],
  300. dev_addr,
  301. sg_dma_len(current_sg),
  302. next_lli_off_dst,
  303. lcsp->lcsp3, dst_data_width,
  304. /* No next == terminal interrupt */
  305. term_int && !next_lli_off_dst,
  306. false);
  307. } else {
  308. d40_log_fill_lli(&lli->dst[i],
  309. sg_phys(current_sg),
  310. sg_dma_len(current_sg),
  311. next_lli_off_dst,
  312. lcsp->lcsp3, dst_data_width,
  313. /* No next == terminal interrupt */
  314. term_int && !next_lli_off_dst,
  315. true);
  316. d40_log_fill_lli(&lli->src[i],
  317. dev_addr,
  318. sg_dma_len(current_sg),
  319. next_lli_off_src,
  320. lcsp->lcsp1, src_data_width,
  321. term_int && !next_lli_off_src,
  322. false);
  323. }
  324. }
  325. return total_size;
  326. }
  327. int d40_log_sg_to_lli(int lcla_id,
  328. struct scatterlist *sg,
  329. int sg_len,
  330. struct d40_log_lli *lli_sg,
  331. u32 lcsp13, /* src or dst*/
  332. u32 data_width,
  333. bool term_int, int max_len, int llis_per_log)
  334. {
  335. int total_size = 0;
  336. struct scatterlist *current_sg = sg;
  337. int i;
  338. u32 next_lli_off = 0;
  339. for_each_sg(sg, current_sg, sg_len, i) {
  340. total_size += sg_dma_len(current_sg);
  341. /*
  342. * If this scatter list entry is the last one or
  343. * max length, terminate link.
  344. */
  345. if (sg_len - 1 == i || ((i+1) % max_len == 0))
  346. next_lli_off = 0;
  347. else {
  348. if (next_lli_off == 0)
  349. /* The first lli will be at next_lli_off */
  350. next_lli_off = lcla_id * llis_per_log + 1;
  351. else
  352. next_lli_off++;
  353. }
  354. d40_log_fill_lli(&lli_sg[i],
  355. sg_phys(current_sg),
  356. sg_dma_len(current_sg),
  357. next_lli_off,
  358. lcsp13, data_width,
  359. term_int && !next_lli_off,
  360. true);
  361. }
  362. return total_size;
  363. }
  364. void d40_log_lli_write(struct d40_log_lli_full *lcpa,
  365. struct d40_log_lli *lcla_src,
  366. struct d40_log_lli *lcla_dst,
  367. struct d40_log_lli *lli_dst,
  368. struct d40_log_lli *lli_src,
  369. int llis_per_log)
  370. {
  371. u32 slos = 0;
  372. u32 dlos = 0;
  373. int i;
  374. lcpa->lcsp0 = lli_src->lcsp02;
  375. lcpa->lcsp1 = lli_src->lcsp13;
  376. lcpa->lcsp2 = lli_dst->lcsp02;
  377. lcpa->lcsp3 = lli_dst->lcsp13;
  378. slos = lli_src->lcsp13 & D40_MEM_LCSP1_SLOS_MASK;
  379. dlos = lli_dst->lcsp13 & D40_MEM_LCSP3_DLOS_MASK;
  380. for (i = 0; (i < llis_per_log) && slos && dlos; i++) {
  381. writel(lli_src[i+1].lcsp02, &lcla_src[i].lcsp02);
  382. writel(lli_src[i+1].lcsp13, &lcla_src[i].lcsp13);
  383. writel(lli_dst[i+1].lcsp02, &lcla_dst[i].lcsp02);
  384. writel(lli_dst[i+1].lcsp13, &lcla_dst[i].lcsp13);
  385. slos = lli_src[i+1].lcsp13 & D40_MEM_LCSP1_SLOS_MASK;
  386. dlos = lli_dst[i+1].lcsp13 & D40_MEM_LCSP3_DLOS_MASK;
  387. }
  388. }