sprd-dma.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /*
  2. * Copyright (C) 2017 Spreadtrum Communications Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/dma/sprd-dma.h>
  9. #include <linux/errno.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_dma.h>
  17. #include <linux/of_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/slab.h>
  20. #include "virt-dma.h"
  21. #define SPRD_DMA_CHN_REG_OFFSET 0x1000
  22. #define SPRD_DMA_CHN_REG_LENGTH 0x40
  23. #define SPRD_DMA_MEMCPY_MIN_SIZE 64
  24. /* DMA global registers definition */
  25. #define SPRD_DMA_GLB_PAUSE 0x0
  26. #define SPRD_DMA_GLB_FRAG_WAIT 0x4
  27. #define SPRD_DMA_GLB_REQ_PEND0_EN 0x8
  28. #define SPRD_DMA_GLB_REQ_PEND1_EN 0xc
  29. #define SPRD_DMA_GLB_INT_RAW_STS 0x10
  30. #define SPRD_DMA_GLB_INT_MSK_STS 0x14
  31. #define SPRD_DMA_GLB_REQ_STS 0x18
  32. #define SPRD_DMA_GLB_CHN_EN_STS 0x1c
  33. #define SPRD_DMA_GLB_DEBUG_STS 0x20
  34. #define SPRD_DMA_GLB_ARB_SEL_STS 0x24
  35. #define SPRD_DMA_GLB_REQ_UID(uid) (0x4 * ((uid) - 1))
  36. #define SPRD_DMA_GLB_REQ_UID_OFFSET 0x2000
  37. /* DMA channel registers definition */
  38. #define SPRD_DMA_CHN_PAUSE 0x0
  39. #define SPRD_DMA_CHN_REQ 0x4
  40. #define SPRD_DMA_CHN_CFG 0x8
  41. #define SPRD_DMA_CHN_INTC 0xc
  42. #define SPRD_DMA_CHN_SRC_ADDR 0x10
  43. #define SPRD_DMA_CHN_DES_ADDR 0x14
  44. #define SPRD_DMA_CHN_FRG_LEN 0x18
  45. #define SPRD_DMA_CHN_BLK_LEN 0x1c
  46. #define SPRD_DMA_CHN_TRSC_LEN 0x20
  47. #define SPRD_DMA_CHN_TRSF_STEP 0x24
  48. #define SPRD_DMA_CHN_WARP_PTR 0x28
  49. #define SPRD_DMA_CHN_WARP_TO 0x2c
  50. #define SPRD_DMA_CHN_LLIST_PTR 0x30
  51. #define SPRD_DMA_CHN_FRAG_STEP 0x34
  52. #define SPRD_DMA_CHN_SRC_BLK_STEP 0x38
  53. #define SPRD_DMA_CHN_DES_BLK_STEP 0x3c
  54. /* SPRD_DMA_CHN_INTC register definition */
  55. #define SPRD_DMA_INT_MASK GENMASK(4, 0)
  56. #define SPRD_DMA_INT_CLR_OFFSET 24
  57. #define SPRD_DMA_FRAG_INT_EN BIT(0)
  58. #define SPRD_DMA_BLK_INT_EN BIT(1)
  59. #define SPRD_DMA_TRANS_INT_EN BIT(2)
  60. #define SPRD_DMA_LIST_INT_EN BIT(3)
  61. #define SPRD_DMA_CFG_ERR_INT_EN BIT(4)
  62. /* SPRD_DMA_CHN_CFG register definition */
  63. #define SPRD_DMA_CHN_EN BIT(0)
  64. #define SPRD_DMA_WAIT_BDONE_OFFSET 24
  65. #define SPRD_DMA_DONOT_WAIT_BDONE 1
  66. /* SPRD_DMA_CHN_REQ register definition */
  67. #define SPRD_DMA_REQ_EN BIT(0)
  68. /* SPRD_DMA_CHN_PAUSE register definition */
  69. #define SPRD_DMA_PAUSE_EN BIT(0)
  70. #define SPRD_DMA_PAUSE_STS BIT(2)
  71. #define SPRD_DMA_PAUSE_CNT 0x2000
  72. /* DMA_CHN_WARP_* register definition */
  73. #define SPRD_DMA_HIGH_ADDR_MASK GENMASK(31, 28)
  74. #define SPRD_DMA_LOW_ADDR_MASK GENMASK(31, 0)
  75. #define SPRD_DMA_HIGH_ADDR_OFFSET 4
  76. /* SPRD_DMA_CHN_INTC register definition */
  77. #define SPRD_DMA_FRAG_INT_STS BIT(16)
  78. #define SPRD_DMA_BLK_INT_STS BIT(17)
  79. #define SPRD_DMA_TRSC_INT_STS BIT(18)
  80. #define SPRD_DMA_LIST_INT_STS BIT(19)
  81. #define SPRD_DMA_CFGERR_INT_STS BIT(20)
  82. #define SPRD_DMA_CHN_INT_STS \
  83. (SPRD_DMA_FRAG_INT_STS | SPRD_DMA_BLK_INT_STS | \
  84. SPRD_DMA_TRSC_INT_STS | SPRD_DMA_LIST_INT_STS | \
  85. SPRD_DMA_CFGERR_INT_STS)
  86. /* SPRD_DMA_CHN_FRG_LEN register definition */
  87. #define SPRD_DMA_SRC_DATAWIDTH_OFFSET 30
  88. #define SPRD_DMA_DES_DATAWIDTH_OFFSET 28
  89. #define SPRD_DMA_SWT_MODE_OFFSET 26
  90. #define SPRD_DMA_REQ_MODE_OFFSET 24
  91. #define SPRD_DMA_REQ_MODE_MASK GENMASK(1, 0)
  92. #define SPRD_DMA_FIX_SEL_OFFSET 21
  93. #define SPRD_DMA_FIX_EN_OFFSET 20
  94. #define SPRD_DMA_LLIST_END_OFFSET 19
  95. #define SPRD_DMA_FRG_LEN_MASK GENMASK(16, 0)
  96. /* SPRD_DMA_CHN_BLK_LEN register definition */
  97. #define SPRD_DMA_BLK_LEN_MASK GENMASK(16, 0)
  98. /* SPRD_DMA_CHN_TRSC_LEN register definition */
  99. #define SPRD_DMA_TRSC_LEN_MASK GENMASK(27, 0)
  100. /* SPRD_DMA_CHN_TRSF_STEP register definition */
  101. #define SPRD_DMA_DEST_TRSF_STEP_OFFSET 16
  102. #define SPRD_DMA_SRC_TRSF_STEP_OFFSET 0
  103. #define SPRD_DMA_TRSF_STEP_MASK GENMASK(15, 0)
  104. /* define the DMA transfer step type */
  105. #define SPRD_DMA_NONE_STEP 0
  106. #define SPRD_DMA_BYTE_STEP 1
  107. #define SPRD_DMA_SHORT_STEP 2
  108. #define SPRD_DMA_WORD_STEP 4
  109. #define SPRD_DMA_DWORD_STEP 8
  110. #define SPRD_DMA_SOFTWARE_UID 0
  111. /* dma data width values */
  112. enum sprd_dma_datawidth {
  113. SPRD_DMA_DATAWIDTH_1_BYTE,
  114. SPRD_DMA_DATAWIDTH_2_BYTES,
  115. SPRD_DMA_DATAWIDTH_4_BYTES,
  116. SPRD_DMA_DATAWIDTH_8_BYTES,
  117. };
  118. /* dma channel hardware configuration */
  119. struct sprd_dma_chn_hw {
  120. u32 pause;
  121. u32 req;
  122. u32 cfg;
  123. u32 intc;
  124. u32 src_addr;
  125. u32 des_addr;
  126. u32 frg_len;
  127. u32 blk_len;
  128. u32 trsc_len;
  129. u32 trsf_step;
  130. u32 wrap_ptr;
  131. u32 wrap_to;
  132. u32 llist_ptr;
  133. u32 frg_step;
  134. u32 src_blk_step;
  135. u32 des_blk_step;
  136. };
  137. /* dma request description */
  138. struct sprd_dma_desc {
  139. struct virt_dma_desc vd;
  140. struct sprd_dma_chn_hw chn_hw;
  141. };
  142. /* dma channel description */
  143. struct sprd_dma_chn {
  144. struct virt_dma_chan vc;
  145. void __iomem *chn_base;
  146. struct dma_slave_config slave_cfg;
  147. u32 chn_num;
  148. u32 dev_id;
  149. struct sprd_dma_desc *cur_desc;
  150. };
  151. /* SPRD dma device */
  152. struct sprd_dma_dev {
  153. struct dma_device dma_dev;
  154. void __iomem *glb_base;
  155. struct clk *clk;
  156. struct clk *ashb_clk;
  157. int irq;
  158. u32 total_chns;
  159. struct sprd_dma_chn channels[0];
  160. };
  161. static void sprd_dma_free_desc(struct virt_dma_desc *vd);
  162. static bool sprd_dma_filter_fn(struct dma_chan *chan, void *param);
  163. static struct of_dma_filter_info sprd_dma_info = {
  164. .filter_fn = sprd_dma_filter_fn,
  165. };
  166. static inline struct sprd_dma_chn *to_sprd_dma_chan(struct dma_chan *c)
  167. {
  168. return container_of(c, struct sprd_dma_chn, vc.chan);
  169. }
  170. static inline struct sprd_dma_dev *to_sprd_dma_dev(struct dma_chan *c)
  171. {
  172. struct sprd_dma_chn *schan = to_sprd_dma_chan(c);
  173. return container_of(schan, struct sprd_dma_dev, channels[c->chan_id]);
  174. }
  175. static inline struct sprd_dma_desc *to_sprd_dma_desc(struct virt_dma_desc *vd)
  176. {
  177. return container_of(vd, struct sprd_dma_desc, vd);
  178. }
  179. static void sprd_dma_chn_update(struct sprd_dma_chn *schan, u32 reg,
  180. u32 mask, u32 val)
  181. {
  182. u32 orig = readl(schan->chn_base + reg);
  183. u32 tmp;
  184. tmp = (orig & ~mask) | val;
  185. writel(tmp, schan->chn_base + reg);
  186. }
  187. static int sprd_dma_enable(struct sprd_dma_dev *sdev)
  188. {
  189. int ret;
  190. ret = clk_prepare_enable(sdev->clk);
  191. if (ret)
  192. return ret;
  193. /*
  194. * The ashb_clk is optional and only for AGCP DMA controller, so we
  195. * need add one condition to check if the ashb_clk need enable.
  196. */
  197. if (!IS_ERR(sdev->ashb_clk))
  198. ret = clk_prepare_enable(sdev->ashb_clk);
  199. return ret;
  200. }
  201. static void sprd_dma_disable(struct sprd_dma_dev *sdev)
  202. {
  203. clk_disable_unprepare(sdev->clk);
  204. /*
  205. * Need to check if we need disable the optional ashb_clk for AGCP DMA.
  206. */
  207. if (!IS_ERR(sdev->ashb_clk))
  208. clk_disable_unprepare(sdev->ashb_clk);
  209. }
  210. static void sprd_dma_set_uid(struct sprd_dma_chn *schan)
  211. {
  212. struct sprd_dma_dev *sdev = to_sprd_dma_dev(&schan->vc.chan);
  213. u32 dev_id = schan->dev_id;
  214. if (dev_id != SPRD_DMA_SOFTWARE_UID) {
  215. u32 uid_offset = SPRD_DMA_GLB_REQ_UID_OFFSET +
  216. SPRD_DMA_GLB_REQ_UID(dev_id);
  217. writel(schan->chn_num + 1, sdev->glb_base + uid_offset);
  218. }
  219. }
  220. static void sprd_dma_unset_uid(struct sprd_dma_chn *schan)
  221. {
  222. struct sprd_dma_dev *sdev = to_sprd_dma_dev(&schan->vc.chan);
  223. u32 dev_id = schan->dev_id;
  224. if (dev_id != SPRD_DMA_SOFTWARE_UID) {
  225. u32 uid_offset = SPRD_DMA_GLB_REQ_UID_OFFSET +
  226. SPRD_DMA_GLB_REQ_UID(dev_id);
  227. writel(0, sdev->glb_base + uid_offset);
  228. }
  229. }
  230. static void sprd_dma_clear_int(struct sprd_dma_chn *schan)
  231. {
  232. sprd_dma_chn_update(schan, SPRD_DMA_CHN_INTC,
  233. SPRD_DMA_INT_MASK << SPRD_DMA_INT_CLR_OFFSET,
  234. SPRD_DMA_INT_MASK << SPRD_DMA_INT_CLR_OFFSET);
  235. }
  236. static void sprd_dma_enable_chn(struct sprd_dma_chn *schan)
  237. {
  238. sprd_dma_chn_update(schan, SPRD_DMA_CHN_CFG, SPRD_DMA_CHN_EN,
  239. SPRD_DMA_CHN_EN);
  240. }
  241. static void sprd_dma_disable_chn(struct sprd_dma_chn *schan)
  242. {
  243. sprd_dma_chn_update(schan, SPRD_DMA_CHN_CFG, SPRD_DMA_CHN_EN, 0);
  244. }
  245. static void sprd_dma_soft_request(struct sprd_dma_chn *schan)
  246. {
  247. sprd_dma_chn_update(schan, SPRD_DMA_CHN_REQ, SPRD_DMA_REQ_EN,
  248. SPRD_DMA_REQ_EN);
  249. }
  250. static void sprd_dma_pause_resume(struct sprd_dma_chn *schan, bool enable)
  251. {
  252. struct sprd_dma_dev *sdev = to_sprd_dma_dev(&schan->vc.chan);
  253. u32 pause, timeout = SPRD_DMA_PAUSE_CNT;
  254. if (enable) {
  255. sprd_dma_chn_update(schan, SPRD_DMA_CHN_PAUSE,
  256. SPRD_DMA_PAUSE_EN, SPRD_DMA_PAUSE_EN);
  257. do {
  258. pause = readl(schan->chn_base + SPRD_DMA_CHN_PAUSE);
  259. if (pause & SPRD_DMA_PAUSE_STS)
  260. break;
  261. cpu_relax();
  262. } while (--timeout > 0);
  263. if (!timeout)
  264. dev_warn(sdev->dma_dev.dev,
  265. "pause dma controller timeout\n");
  266. } else {
  267. sprd_dma_chn_update(schan, SPRD_DMA_CHN_PAUSE,
  268. SPRD_DMA_PAUSE_EN, 0);
  269. }
  270. }
  271. static void sprd_dma_stop_and_disable(struct sprd_dma_chn *schan)
  272. {
  273. u32 cfg = readl(schan->chn_base + SPRD_DMA_CHN_CFG);
  274. if (!(cfg & SPRD_DMA_CHN_EN))
  275. return;
  276. sprd_dma_pause_resume(schan, true);
  277. sprd_dma_disable_chn(schan);
  278. }
  279. static unsigned long sprd_dma_get_dst_addr(struct sprd_dma_chn *schan)
  280. {
  281. unsigned long addr, addr_high;
  282. addr = readl(schan->chn_base + SPRD_DMA_CHN_DES_ADDR);
  283. addr_high = readl(schan->chn_base + SPRD_DMA_CHN_WARP_TO) &
  284. SPRD_DMA_HIGH_ADDR_MASK;
  285. return addr | (addr_high << SPRD_DMA_HIGH_ADDR_OFFSET);
  286. }
  287. static enum sprd_dma_int_type sprd_dma_get_int_type(struct sprd_dma_chn *schan)
  288. {
  289. struct sprd_dma_dev *sdev = to_sprd_dma_dev(&schan->vc.chan);
  290. u32 intc_sts = readl(schan->chn_base + SPRD_DMA_CHN_INTC) &
  291. SPRD_DMA_CHN_INT_STS;
  292. switch (intc_sts) {
  293. case SPRD_DMA_CFGERR_INT_STS:
  294. return SPRD_DMA_CFGERR_INT;
  295. case SPRD_DMA_LIST_INT_STS:
  296. return SPRD_DMA_LIST_INT;
  297. case SPRD_DMA_TRSC_INT_STS:
  298. return SPRD_DMA_TRANS_INT;
  299. case SPRD_DMA_BLK_INT_STS:
  300. return SPRD_DMA_BLK_INT;
  301. case SPRD_DMA_FRAG_INT_STS:
  302. return SPRD_DMA_FRAG_INT;
  303. default:
  304. dev_warn(sdev->dma_dev.dev, "incorrect dma interrupt type\n");
  305. return SPRD_DMA_NO_INT;
  306. }
  307. }
  308. static enum sprd_dma_req_mode sprd_dma_get_req_type(struct sprd_dma_chn *schan)
  309. {
  310. u32 frag_reg = readl(schan->chn_base + SPRD_DMA_CHN_FRG_LEN);
  311. return (frag_reg >> SPRD_DMA_REQ_MODE_OFFSET) & SPRD_DMA_REQ_MODE_MASK;
  312. }
  313. static void sprd_dma_set_chn_config(struct sprd_dma_chn *schan,
  314. struct sprd_dma_desc *sdesc)
  315. {
  316. struct sprd_dma_chn_hw *cfg = &sdesc->chn_hw;
  317. writel(cfg->pause, schan->chn_base + SPRD_DMA_CHN_PAUSE);
  318. writel(cfg->cfg, schan->chn_base + SPRD_DMA_CHN_CFG);
  319. writel(cfg->intc, schan->chn_base + SPRD_DMA_CHN_INTC);
  320. writel(cfg->src_addr, schan->chn_base + SPRD_DMA_CHN_SRC_ADDR);
  321. writel(cfg->des_addr, schan->chn_base + SPRD_DMA_CHN_DES_ADDR);
  322. writel(cfg->frg_len, schan->chn_base + SPRD_DMA_CHN_FRG_LEN);
  323. writel(cfg->blk_len, schan->chn_base + SPRD_DMA_CHN_BLK_LEN);
  324. writel(cfg->trsc_len, schan->chn_base + SPRD_DMA_CHN_TRSC_LEN);
  325. writel(cfg->trsf_step, schan->chn_base + SPRD_DMA_CHN_TRSF_STEP);
  326. writel(cfg->wrap_ptr, schan->chn_base + SPRD_DMA_CHN_WARP_PTR);
  327. writel(cfg->wrap_to, schan->chn_base + SPRD_DMA_CHN_WARP_TO);
  328. writel(cfg->llist_ptr, schan->chn_base + SPRD_DMA_CHN_LLIST_PTR);
  329. writel(cfg->frg_step, schan->chn_base + SPRD_DMA_CHN_FRAG_STEP);
  330. writel(cfg->src_blk_step, schan->chn_base + SPRD_DMA_CHN_SRC_BLK_STEP);
  331. writel(cfg->des_blk_step, schan->chn_base + SPRD_DMA_CHN_DES_BLK_STEP);
  332. writel(cfg->req, schan->chn_base + SPRD_DMA_CHN_REQ);
  333. }
  334. static void sprd_dma_start(struct sprd_dma_chn *schan)
  335. {
  336. struct virt_dma_desc *vd = vchan_next_desc(&schan->vc);
  337. if (!vd)
  338. return;
  339. list_del(&vd->node);
  340. schan->cur_desc = to_sprd_dma_desc(vd);
  341. /*
  342. * Copy the DMA configuration from DMA descriptor to this hardware
  343. * channel.
  344. */
  345. sprd_dma_set_chn_config(schan, schan->cur_desc);
  346. sprd_dma_set_uid(schan);
  347. sprd_dma_enable_chn(schan);
  348. if (schan->dev_id == SPRD_DMA_SOFTWARE_UID)
  349. sprd_dma_soft_request(schan);
  350. }
  351. static void sprd_dma_stop(struct sprd_dma_chn *schan)
  352. {
  353. sprd_dma_stop_and_disable(schan);
  354. sprd_dma_unset_uid(schan);
  355. sprd_dma_clear_int(schan);
  356. }
  357. static bool sprd_dma_check_trans_done(struct sprd_dma_desc *sdesc,
  358. enum sprd_dma_int_type int_type,
  359. enum sprd_dma_req_mode req_mode)
  360. {
  361. if (int_type == SPRD_DMA_NO_INT)
  362. return false;
  363. if (int_type >= req_mode + 1)
  364. return true;
  365. else
  366. return false;
  367. }
  368. static irqreturn_t dma_irq_handle(int irq, void *dev_id)
  369. {
  370. struct sprd_dma_dev *sdev = (struct sprd_dma_dev *)dev_id;
  371. u32 irq_status = readl(sdev->glb_base + SPRD_DMA_GLB_INT_MSK_STS);
  372. struct sprd_dma_chn *schan;
  373. struct sprd_dma_desc *sdesc;
  374. enum sprd_dma_req_mode req_type;
  375. enum sprd_dma_int_type int_type;
  376. bool trans_done = false;
  377. u32 i;
  378. while (irq_status) {
  379. i = __ffs(irq_status);
  380. irq_status &= (irq_status - 1);
  381. schan = &sdev->channels[i];
  382. spin_lock(&schan->vc.lock);
  383. int_type = sprd_dma_get_int_type(schan);
  384. req_type = sprd_dma_get_req_type(schan);
  385. sprd_dma_clear_int(schan);
  386. sdesc = schan->cur_desc;
  387. /* Check if the dma request descriptor is done. */
  388. trans_done = sprd_dma_check_trans_done(sdesc, int_type,
  389. req_type);
  390. if (trans_done == true) {
  391. vchan_cookie_complete(&sdesc->vd);
  392. schan->cur_desc = NULL;
  393. sprd_dma_start(schan);
  394. }
  395. spin_unlock(&schan->vc.lock);
  396. }
  397. return IRQ_HANDLED;
  398. }
  399. static int sprd_dma_alloc_chan_resources(struct dma_chan *chan)
  400. {
  401. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  402. int ret;
  403. ret = pm_runtime_get_sync(chan->device->dev);
  404. if (ret < 0)
  405. return ret;
  406. schan->dev_id = SPRD_DMA_SOFTWARE_UID;
  407. return 0;
  408. }
  409. static void sprd_dma_free_chan_resources(struct dma_chan *chan)
  410. {
  411. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  412. struct virt_dma_desc *cur_vd = NULL;
  413. unsigned long flags;
  414. spin_lock_irqsave(&schan->vc.lock, flags);
  415. if (schan->cur_desc)
  416. cur_vd = &schan->cur_desc->vd;
  417. sprd_dma_stop(schan);
  418. spin_unlock_irqrestore(&schan->vc.lock, flags);
  419. if (cur_vd)
  420. sprd_dma_free_desc(cur_vd);
  421. vchan_free_chan_resources(&schan->vc);
  422. pm_runtime_put(chan->device->dev);
  423. }
  424. static enum dma_status sprd_dma_tx_status(struct dma_chan *chan,
  425. dma_cookie_t cookie,
  426. struct dma_tx_state *txstate)
  427. {
  428. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  429. struct virt_dma_desc *vd;
  430. unsigned long flags;
  431. enum dma_status ret;
  432. u32 pos;
  433. ret = dma_cookie_status(chan, cookie, txstate);
  434. if (ret == DMA_COMPLETE || !txstate)
  435. return ret;
  436. spin_lock_irqsave(&schan->vc.lock, flags);
  437. vd = vchan_find_desc(&schan->vc, cookie);
  438. if (vd) {
  439. struct sprd_dma_desc *sdesc = to_sprd_dma_desc(vd);
  440. struct sprd_dma_chn_hw *hw = &sdesc->chn_hw;
  441. if (hw->trsc_len > 0)
  442. pos = hw->trsc_len;
  443. else if (hw->blk_len > 0)
  444. pos = hw->blk_len;
  445. else if (hw->frg_len > 0)
  446. pos = hw->frg_len;
  447. else
  448. pos = 0;
  449. } else if (schan->cur_desc && schan->cur_desc->vd.tx.cookie == cookie) {
  450. pos = sprd_dma_get_dst_addr(schan);
  451. } else {
  452. pos = 0;
  453. }
  454. spin_unlock_irqrestore(&schan->vc.lock, flags);
  455. dma_set_residue(txstate, pos);
  456. return ret;
  457. }
  458. static void sprd_dma_issue_pending(struct dma_chan *chan)
  459. {
  460. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  461. unsigned long flags;
  462. spin_lock_irqsave(&schan->vc.lock, flags);
  463. if (vchan_issue_pending(&schan->vc) && !schan->cur_desc)
  464. sprd_dma_start(schan);
  465. spin_unlock_irqrestore(&schan->vc.lock, flags);
  466. }
  467. static int sprd_dma_get_datawidth(enum dma_slave_buswidth buswidth)
  468. {
  469. switch (buswidth) {
  470. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  471. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  472. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  473. case DMA_SLAVE_BUSWIDTH_8_BYTES:
  474. return ffs(buswidth) - 1;
  475. default:
  476. return -EINVAL;
  477. }
  478. }
  479. static int sprd_dma_get_step(enum dma_slave_buswidth buswidth)
  480. {
  481. switch (buswidth) {
  482. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  483. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  484. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  485. case DMA_SLAVE_BUSWIDTH_8_BYTES:
  486. return buswidth;
  487. default:
  488. return -EINVAL;
  489. }
  490. }
  491. static int sprd_dma_fill_desc(struct dma_chan *chan,
  492. struct sprd_dma_desc *sdesc,
  493. dma_addr_t src, dma_addr_t dst, u32 len,
  494. enum dma_transfer_direction dir,
  495. unsigned long flags,
  496. struct dma_slave_config *slave_cfg)
  497. {
  498. struct sprd_dma_dev *sdev = to_sprd_dma_dev(chan);
  499. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  500. struct sprd_dma_chn_hw *hw = &sdesc->chn_hw;
  501. u32 req_mode = (flags >> SPRD_DMA_REQ_SHIFT) & SPRD_DMA_REQ_MODE_MASK;
  502. u32 int_mode = flags & SPRD_DMA_INT_MASK;
  503. int src_datawidth, dst_datawidth, src_step, dst_step;
  504. u32 temp, fix_mode = 0, fix_en = 0;
  505. if (dir == DMA_MEM_TO_DEV) {
  506. src_step = sprd_dma_get_step(slave_cfg->src_addr_width);
  507. if (src_step < 0) {
  508. dev_err(sdev->dma_dev.dev, "invalid source step\n");
  509. return src_step;
  510. }
  511. dst_step = SPRD_DMA_NONE_STEP;
  512. } else {
  513. dst_step = sprd_dma_get_step(slave_cfg->dst_addr_width);
  514. if (dst_step < 0) {
  515. dev_err(sdev->dma_dev.dev, "invalid destination step\n");
  516. return dst_step;
  517. }
  518. src_step = SPRD_DMA_NONE_STEP;
  519. }
  520. src_datawidth = sprd_dma_get_datawidth(slave_cfg->src_addr_width);
  521. if (src_datawidth < 0) {
  522. dev_err(sdev->dma_dev.dev, "invalid source datawidth\n");
  523. return src_datawidth;
  524. }
  525. dst_datawidth = sprd_dma_get_datawidth(slave_cfg->dst_addr_width);
  526. if (dst_datawidth < 0) {
  527. dev_err(sdev->dma_dev.dev, "invalid destination datawidth\n");
  528. return dst_datawidth;
  529. }
  530. if (slave_cfg->slave_id)
  531. schan->dev_id = slave_cfg->slave_id;
  532. hw->cfg = SPRD_DMA_DONOT_WAIT_BDONE << SPRD_DMA_WAIT_BDONE_OFFSET;
  533. /*
  534. * wrap_ptr and wrap_to will save the high 4 bits source address and
  535. * destination address.
  536. */
  537. hw->wrap_ptr = (src >> SPRD_DMA_HIGH_ADDR_OFFSET) & SPRD_DMA_HIGH_ADDR_MASK;
  538. hw->wrap_to = (dst >> SPRD_DMA_HIGH_ADDR_OFFSET) & SPRD_DMA_HIGH_ADDR_MASK;
  539. hw->src_addr = src & SPRD_DMA_LOW_ADDR_MASK;
  540. hw->des_addr = dst & SPRD_DMA_LOW_ADDR_MASK;
  541. /*
  542. * If the src step and dst step both are 0 or both are not 0, that means
  543. * we can not enable the fix mode. If one is 0 and another one is not,
  544. * we can enable the fix mode.
  545. */
  546. if ((src_step != 0 && dst_step != 0) || (src_step | dst_step) == 0) {
  547. fix_en = 0;
  548. } else {
  549. fix_en = 1;
  550. if (src_step)
  551. fix_mode = 1;
  552. else
  553. fix_mode = 0;
  554. }
  555. hw->intc = int_mode | SPRD_DMA_CFG_ERR_INT_EN;
  556. temp = src_datawidth << SPRD_DMA_SRC_DATAWIDTH_OFFSET;
  557. temp |= dst_datawidth << SPRD_DMA_DES_DATAWIDTH_OFFSET;
  558. temp |= req_mode << SPRD_DMA_REQ_MODE_OFFSET;
  559. temp |= fix_mode << SPRD_DMA_FIX_SEL_OFFSET;
  560. temp |= fix_en << SPRD_DMA_FIX_EN_OFFSET;
  561. temp |= slave_cfg->src_maxburst & SPRD_DMA_FRG_LEN_MASK;
  562. hw->frg_len = temp;
  563. hw->blk_len = slave_cfg->src_maxburst & SPRD_DMA_BLK_LEN_MASK;
  564. hw->trsc_len = len & SPRD_DMA_TRSC_LEN_MASK;
  565. temp = (dst_step & SPRD_DMA_TRSF_STEP_MASK) << SPRD_DMA_DEST_TRSF_STEP_OFFSET;
  566. temp |= (src_step & SPRD_DMA_TRSF_STEP_MASK) << SPRD_DMA_SRC_TRSF_STEP_OFFSET;
  567. hw->trsf_step = temp;
  568. hw->frg_step = 0;
  569. hw->src_blk_step = 0;
  570. hw->des_blk_step = 0;
  571. return 0;
  572. }
  573. static struct dma_async_tx_descriptor *
  574. sprd_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  575. size_t len, unsigned long flags)
  576. {
  577. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  578. struct sprd_dma_desc *sdesc;
  579. struct sprd_dma_chn_hw *hw;
  580. enum sprd_dma_datawidth datawidth;
  581. u32 step, temp;
  582. sdesc = kzalloc(sizeof(*sdesc), GFP_NOWAIT);
  583. if (!sdesc)
  584. return NULL;
  585. hw = &sdesc->chn_hw;
  586. hw->cfg = SPRD_DMA_DONOT_WAIT_BDONE << SPRD_DMA_WAIT_BDONE_OFFSET;
  587. hw->intc = SPRD_DMA_TRANS_INT | SPRD_DMA_CFG_ERR_INT_EN;
  588. hw->src_addr = src & SPRD_DMA_LOW_ADDR_MASK;
  589. hw->des_addr = dest & SPRD_DMA_LOW_ADDR_MASK;
  590. hw->wrap_ptr = (src >> SPRD_DMA_HIGH_ADDR_OFFSET) &
  591. SPRD_DMA_HIGH_ADDR_MASK;
  592. hw->wrap_to = (dest >> SPRD_DMA_HIGH_ADDR_OFFSET) &
  593. SPRD_DMA_HIGH_ADDR_MASK;
  594. if (IS_ALIGNED(len, 8)) {
  595. datawidth = SPRD_DMA_DATAWIDTH_8_BYTES;
  596. step = SPRD_DMA_DWORD_STEP;
  597. } else if (IS_ALIGNED(len, 4)) {
  598. datawidth = SPRD_DMA_DATAWIDTH_4_BYTES;
  599. step = SPRD_DMA_WORD_STEP;
  600. } else if (IS_ALIGNED(len, 2)) {
  601. datawidth = SPRD_DMA_DATAWIDTH_2_BYTES;
  602. step = SPRD_DMA_SHORT_STEP;
  603. } else {
  604. datawidth = SPRD_DMA_DATAWIDTH_1_BYTE;
  605. step = SPRD_DMA_BYTE_STEP;
  606. }
  607. temp = datawidth << SPRD_DMA_SRC_DATAWIDTH_OFFSET;
  608. temp |= datawidth << SPRD_DMA_DES_DATAWIDTH_OFFSET;
  609. temp |= SPRD_DMA_TRANS_REQ << SPRD_DMA_REQ_MODE_OFFSET;
  610. temp |= len & SPRD_DMA_FRG_LEN_MASK;
  611. hw->frg_len = temp;
  612. hw->blk_len = len & SPRD_DMA_BLK_LEN_MASK;
  613. hw->trsc_len = len & SPRD_DMA_TRSC_LEN_MASK;
  614. temp = (step & SPRD_DMA_TRSF_STEP_MASK) << SPRD_DMA_DEST_TRSF_STEP_OFFSET;
  615. temp |= (step & SPRD_DMA_TRSF_STEP_MASK) << SPRD_DMA_SRC_TRSF_STEP_OFFSET;
  616. hw->trsf_step = temp;
  617. return vchan_tx_prep(&schan->vc, &sdesc->vd, flags);
  618. }
  619. static struct dma_async_tx_descriptor *
  620. sprd_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  621. unsigned int sglen, enum dma_transfer_direction dir,
  622. unsigned long flags, void *context)
  623. {
  624. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  625. struct dma_slave_config *slave_cfg = &schan->slave_cfg;
  626. dma_addr_t src = 0, dst = 0;
  627. struct sprd_dma_desc *sdesc;
  628. struct scatterlist *sg;
  629. u32 len = 0;
  630. int ret, i;
  631. /* TODO: now we only support one sg for each DMA configuration. */
  632. if (!is_slave_direction(dir) || sglen > 1)
  633. return NULL;
  634. sdesc = kzalloc(sizeof(*sdesc), GFP_NOWAIT);
  635. if (!sdesc)
  636. return NULL;
  637. for_each_sg(sgl, sg, sglen, i) {
  638. len = sg_dma_len(sg);
  639. if (dir == DMA_MEM_TO_DEV) {
  640. src = sg_dma_address(sg);
  641. dst = slave_cfg->dst_addr;
  642. } else {
  643. src = slave_cfg->src_addr;
  644. dst = sg_dma_address(sg);
  645. }
  646. }
  647. ret = sprd_dma_fill_desc(chan, sdesc, src, dst, len, dir, flags,
  648. slave_cfg);
  649. if (ret) {
  650. kfree(sdesc);
  651. return NULL;
  652. }
  653. return vchan_tx_prep(&schan->vc, &sdesc->vd, flags);
  654. }
  655. static int sprd_dma_slave_config(struct dma_chan *chan,
  656. struct dma_slave_config *config)
  657. {
  658. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  659. struct dma_slave_config *slave_cfg = &schan->slave_cfg;
  660. if (!is_slave_direction(config->direction))
  661. return -EINVAL;
  662. memcpy(slave_cfg, config, sizeof(*config));
  663. return 0;
  664. }
  665. static int sprd_dma_pause(struct dma_chan *chan)
  666. {
  667. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  668. unsigned long flags;
  669. spin_lock_irqsave(&schan->vc.lock, flags);
  670. sprd_dma_pause_resume(schan, true);
  671. spin_unlock_irqrestore(&schan->vc.lock, flags);
  672. return 0;
  673. }
  674. static int sprd_dma_resume(struct dma_chan *chan)
  675. {
  676. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  677. unsigned long flags;
  678. spin_lock_irqsave(&schan->vc.lock, flags);
  679. sprd_dma_pause_resume(schan, false);
  680. spin_unlock_irqrestore(&schan->vc.lock, flags);
  681. return 0;
  682. }
  683. static int sprd_dma_terminate_all(struct dma_chan *chan)
  684. {
  685. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  686. struct virt_dma_desc *cur_vd = NULL;
  687. unsigned long flags;
  688. LIST_HEAD(head);
  689. spin_lock_irqsave(&schan->vc.lock, flags);
  690. if (schan->cur_desc)
  691. cur_vd = &schan->cur_desc->vd;
  692. sprd_dma_stop(schan);
  693. vchan_get_all_descriptors(&schan->vc, &head);
  694. spin_unlock_irqrestore(&schan->vc.lock, flags);
  695. if (cur_vd)
  696. sprd_dma_free_desc(cur_vd);
  697. vchan_dma_desc_free_list(&schan->vc, &head);
  698. return 0;
  699. }
  700. static void sprd_dma_free_desc(struct virt_dma_desc *vd)
  701. {
  702. struct sprd_dma_desc *sdesc = to_sprd_dma_desc(vd);
  703. kfree(sdesc);
  704. }
  705. static bool sprd_dma_filter_fn(struct dma_chan *chan, void *param)
  706. {
  707. struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
  708. struct sprd_dma_dev *sdev = to_sprd_dma_dev(&schan->vc.chan);
  709. u32 req = *(u32 *)param;
  710. if (req < sdev->total_chns)
  711. return req == schan->chn_num + 1;
  712. else
  713. return false;
  714. }
  715. static int sprd_dma_probe(struct platform_device *pdev)
  716. {
  717. struct device_node *np = pdev->dev.of_node;
  718. struct sprd_dma_dev *sdev;
  719. struct sprd_dma_chn *dma_chn;
  720. struct resource *res;
  721. u32 chn_count;
  722. int ret, i;
  723. ret = device_property_read_u32(&pdev->dev, "#dma-channels", &chn_count);
  724. if (ret) {
  725. dev_err(&pdev->dev, "get dma channels count failed\n");
  726. return ret;
  727. }
  728. sdev = devm_kzalloc(&pdev->dev,
  729. struct_size(sdev, channels, chn_count),
  730. GFP_KERNEL);
  731. if (!sdev)
  732. return -ENOMEM;
  733. sdev->clk = devm_clk_get(&pdev->dev, "enable");
  734. if (IS_ERR(sdev->clk)) {
  735. dev_err(&pdev->dev, "get enable clock failed\n");
  736. return PTR_ERR(sdev->clk);
  737. }
  738. /* ashb clock is optional for AGCP DMA */
  739. sdev->ashb_clk = devm_clk_get(&pdev->dev, "ashb_eb");
  740. if (IS_ERR(sdev->ashb_clk))
  741. dev_warn(&pdev->dev, "no optional ashb eb clock\n");
  742. /*
  743. * We have three DMA controllers: AP DMA, AON DMA and AGCP DMA. For AGCP
  744. * DMA controller, it can or do not request the irq, which will save
  745. * system power without resuming system by DMA interrupts if AGCP DMA
  746. * does not request the irq. Thus the DMA interrupts property should
  747. * be optional.
  748. */
  749. sdev->irq = platform_get_irq(pdev, 0);
  750. if (sdev->irq > 0) {
  751. ret = devm_request_irq(&pdev->dev, sdev->irq, dma_irq_handle,
  752. 0, "sprd_dma", (void *)sdev);
  753. if (ret < 0) {
  754. dev_err(&pdev->dev, "request dma irq failed\n");
  755. return ret;
  756. }
  757. } else {
  758. dev_warn(&pdev->dev, "no interrupts for the dma controller\n");
  759. }
  760. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  761. sdev->glb_base = devm_ioremap_resource(&pdev->dev, res);
  762. if (IS_ERR(sdev->glb_base))
  763. return PTR_ERR(sdev->glb_base);
  764. dma_cap_set(DMA_MEMCPY, sdev->dma_dev.cap_mask);
  765. sdev->total_chns = chn_count;
  766. sdev->dma_dev.chancnt = chn_count;
  767. INIT_LIST_HEAD(&sdev->dma_dev.channels);
  768. INIT_LIST_HEAD(&sdev->dma_dev.global_node);
  769. sdev->dma_dev.dev = &pdev->dev;
  770. sdev->dma_dev.device_alloc_chan_resources = sprd_dma_alloc_chan_resources;
  771. sdev->dma_dev.device_free_chan_resources = sprd_dma_free_chan_resources;
  772. sdev->dma_dev.device_tx_status = sprd_dma_tx_status;
  773. sdev->dma_dev.device_issue_pending = sprd_dma_issue_pending;
  774. sdev->dma_dev.device_prep_dma_memcpy = sprd_dma_prep_dma_memcpy;
  775. sdev->dma_dev.device_prep_slave_sg = sprd_dma_prep_slave_sg;
  776. sdev->dma_dev.device_config = sprd_dma_slave_config;
  777. sdev->dma_dev.device_pause = sprd_dma_pause;
  778. sdev->dma_dev.device_resume = sprd_dma_resume;
  779. sdev->dma_dev.device_terminate_all = sprd_dma_terminate_all;
  780. for (i = 0; i < chn_count; i++) {
  781. dma_chn = &sdev->channels[i];
  782. dma_chn->chn_num = i;
  783. dma_chn->cur_desc = NULL;
  784. /* get each channel's registers base address. */
  785. dma_chn->chn_base = sdev->glb_base + SPRD_DMA_CHN_REG_OFFSET +
  786. SPRD_DMA_CHN_REG_LENGTH * i;
  787. dma_chn->vc.desc_free = sprd_dma_free_desc;
  788. vchan_init(&dma_chn->vc, &sdev->dma_dev);
  789. }
  790. platform_set_drvdata(pdev, sdev);
  791. ret = sprd_dma_enable(sdev);
  792. if (ret)
  793. return ret;
  794. pm_runtime_set_active(&pdev->dev);
  795. pm_runtime_enable(&pdev->dev);
  796. ret = pm_runtime_get_sync(&pdev->dev);
  797. if (ret < 0)
  798. goto err_rpm;
  799. ret = dma_async_device_register(&sdev->dma_dev);
  800. if (ret < 0) {
  801. dev_err(&pdev->dev, "register dma device failed:%d\n", ret);
  802. goto err_register;
  803. }
  804. sprd_dma_info.dma_cap = sdev->dma_dev.cap_mask;
  805. ret = of_dma_controller_register(np, of_dma_simple_xlate,
  806. &sprd_dma_info);
  807. if (ret)
  808. goto err_of_register;
  809. pm_runtime_put(&pdev->dev);
  810. return 0;
  811. err_of_register:
  812. dma_async_device_unregister(&sdev->dma_dev);
  813. err_register:
  814. pm_runtime_put_noidle(&pdev->dev);
  815. pm_runtime_disable(&pdev->dev);
  816. err_rpm:
  817. sprd_dma_disable(sdev);
  818. return ret;
  819. }
  820. static int sprd_dma_remove(struct platform_device *pdev)
  821. {
  822. struct sprd_dma_dev *sdev = platform_get_drvdata(pdev);
  823. struct sprd_dma_chn *c, *cn;
  824. int ret;
  825. ret = pm_runtime_get_sync(&pdev->dev);
  826. if (ret < 0)
  827. return ret;
  828. /* explicitly free the irq */
  829. if (sdev->irq > 0)
  830. devm_free_irq(&pdev->dev, sdev->irq, sdev);
  831. list_for_each_entry_safe(c, cn, &sdev->dma_dev.channels,
  832. vc.chan.device_node) {
  833. list_del(&c->vc.chan.device_node);
  834. tasklet_kill(&c->vc.task);
  835. }
  836. of_dma_controller_free(pdev->dev.of_node);
  837. dma_async_device_unregister(&sdev->dma_dev);
  838. sprd_dma_disable(sdev);
  839. pm_runtime_put_noidle(&pdev->dev);
  840. pm_runtime_disable(&pdev->dev);
  841. return 0;
  842. }
  843. static const struct of_device_id sprd_dma_match[] = {
  844. { .compatible = "sprd,sc9860-dma", },
  845. {},
  846. };
  847. static int __maybe_unused sprd_dma_runtime_suspend(struct device *dev)
  848. {
  849. struct sprd_dma_dev *sdev = dev_get_drvdata(dev);
  850. sprd_dma_disable(sdev);
  851. return 0;
  852. }
  853. static int __maybe_unused sprd_dma_runtime_resume(struct device *dev)
  854. {
  855. struct sprd_dma_dev *sdev = dev_get_drvdata(dev);
  856. int ret;
  857. ret = sprd_dma_enable(sdev);
  858. if (ret)
  859. dev_err(sdev->dma_dev.dev, "enable dma failed\n");
  860. return ret;
  861. }
  862. static const struct dev_pm_ops sprd_dma_pm_ops = {
  863. SET_RUNTIME_PM_OPS(sprd_dma_runtime_suspend,
  864. sprd_dma_runtime_resume,
  865. NULL)
  866. };
  867. static struct platform_driver sprd_dma_driver = {
  868. .probe = sprd_dma_probe,
  869. .remove = sprd_dma_remove,
  870. .driver = {
  871. .name = "sprd-dma",
  872. .of_match_table = sprd_dma_match,
  873. .pm = &sprd_dma_pm_ops,
  874. },
  875. };
  876. module_platform_driver(sprd_dma_driver);
  877. MODULE_LICENSE("GPL v2");
  878. MODULE_DESCRIPTION("DMA driver for Spreadtrum");
  879. MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
  880. MODULE_ALIAS("platform:sprd-dma");