owl-dma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Actions Semi Owl SoCs DMA driver
  4. //
  5. // Copyright (c) 2014 Actions Semi Inc.
  6. // Author: David Liu <liuwei@actions-semi.com>
  7. //
  8. // Copyright (c) 2018 Linaro Ltd.
  9. // Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
  10. #include <linux/bitops.h>
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/dmapool.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/of_device.h>
  23. #include <linux/slab.h>
  24. #include "virt-dma.h"
  25. #define OWL_DMA_FRAME_MAX_LENGTH 0xfffff
  26. /* Global DMA Controller Registers */
  27. #define OWL_DMA_IRQ_PD0 0x00
  28. #define OWL_DMA_IRQ_PD1 0x04
  29. #define OWL_DMA_IRQ_PD2 0x08
  30. #define OWL_DMA_IRQ_PD3 0x0C
  31. #define OWL_DMA_IRQ_EN0 0x10
  32. #define OWL_DMA_IRQ_EN1 0x14
  33. #define OWL_DMA_IRQ_EN2 0x18
  34. #define OWL_DMA_IRQ_EN3 0x1C
  35. #define OWL_DMA_SECURE_ACCESS_CTL 0x20
  36. #define OWL_DMA_NIC_QOS 0x24
  37. #define OWL_DMA_DBGSEL 0x28
  38. #define OWL_DMA_IDLE_STAT 0x2C
  39. /* Channel Registers */
  40. #define OWL_DMA_CHAN_BASE(i) (0x100 + (i) * 0x100)
  41. #define OWL_DMAX_MODE 0x00
  42. #define OWL_DMAX_SOURCE 0x04
  43. #define OWL_DMAX_DESTINATION 0x08
  44. #define OWL_DMAX_FRAME_LEN 0x0C
  45. #define OWL_DMAX_FRAME_CNT 0x10
  46. #define OWL_DMAX_REMAIN_FRAME_CNT 0x14
  47. #define OWL_DMAX_REMAIN_CNT 0x18
  48. #define OWL_DMAX_SOURCE_STRIDE 0x1C
  49. #define OWL_DMAX_DESTINATION_STRIDE 0x20
  50. #define OWL_DMAX_START 0x24
  51. #define OWL_DMAX_PAUSE 0x28
  52. #define OWL_DMAX_CHAINED_CTL 0x2C
  53. #define OWL_DMAX_CONSTANT 0x30
  54. #define OWL_DMAX_LINKLIST_CTL 0x34
  55. #define OWL_DMAX_NEXT_DESCRIPTOR 0x38
  56. #define OWL_DMAX_CURRENT_DESCRIPTOR_NUM 0x3C
  57. #define OWL_DMAX_INT_CTL 0x40
  58. #define OWL_DMAX_INT_STATUS 0x44
  59. #define OWL_DMAX_CURRENT_SOURCE_POINTER 0x48
  60. #define OWL_DMAX_CURRENT_DESTINATION_POINTER 0x4C
  61. /* OWL_DMAX_MODE Bits */
  62. #define OWL_DMA_MODE_TS(x) (((x) & GENMASK(5, 0)) << 0)
  63. #define OWL_DMA_MODE_ST(x) (((x) & GENMASK(1, 0)) << 8)
  64. #define OWL_DMA_MODE_ST_DEV OWL_DMA_MODE_ST(0)
  65. #define OWL_DMA_MODE_ST_DCU OWL_DMA_MODE_ST(2)
  66. #define OWL_DMA_MODE_ST_SRAM OWL_DMA_MODE_ST(3)
  67. #define OWL_DMA_MODE_DT(x) (((x) & GENMASK(1, 0)) << 10)
  68. #define OWL_DMA_MODE_DT_DEV OWL_DMA_MODE_DT(0)
  69. #define OWL_DMA_MODE_DT_DCU OWL_DMA_MODE_DT(2)
  70. #define OWL_DMA_MODE_DT_SRAM OWL_DMA_MODE_DT(3)
  71. #define OWL_DMA_MODE_SAM(x) (((x) & GENMASK(1, 0)) << 16)
  72. #define OWL_DMA_MODE_SAM_CONST OWL_DMA_MODE_SAM(0)
  73. #define OWL_DMA_MODE_SAM_INC OWL_DMA_MODE_SAM(1)
  74. #define OWL_DMA_MODE_SAM_STRIDE OWL_DMA_MODE_SAM(2)
  75. #define OWL_DMA_MODE_DAM(x) (((x) & GENMASK(1, 0)) << 18)
  76. #define OWL_DMA_MODE_DAM_CONST OWL_DMA_MODE_DAM(0)
  77. #define OWL_DMA_MODE_DAM_INC OWL_DMA_MODE_DAM(1)
  78. #define OWL_DMA_MODE_DAM_STRIDE OWL_DMA_MODE_DAM(2)
  79. #define OWL_DMA_MODE_PW(x) (((x) & GENMASK(2, 0)) << 20)
  80. #define OWL_DMA_MODE_CB BIT(23)
  81. #define OWL_DMA_MODE_NDDBW(x) (((x) & 0x1) << 28)
  82. #define OWL_DMA_MODE_NDDBW_32BIT OWL_DMA_MODE_NDDBW(0)
  83. #define OWL_DMA_MODE_NDDBW_8BIT OWL_DMA_MODE_NDDBW(1)
  84. #define OWL_DMA_MODE_CFE BIT(29)
  85. #define OWL_DMA_MODE_LME BIT(30)
  86. #define OWL_DMA_MODE_CME BIT(31)
  87. /* OWL_DMAX_LINKLIST_CTL Bits */
  88. #define OWL_DMA_LLC_SAV(x) (((x) & GENMASK(1, 0)) << 8)
  89. #define OWL_DMA_LLC_SAV_INC OWL_DMA_LLC_SAV(0)
  90. #define OWL_DMA_LLC_SAV_LOAD_NEXT OWL_DMA_LLC_SAV(1)
  91. #define OWL_DMA_LLC_SAV_LOAD_PREV OWL_DMA_LLC_SAV(2)
  92. #define OWL_DMA_LLC_DAV(x) (((x) & GENMASK(1, 0)) << 10)
  93. #define OWL_DMA_LLC_DAV_INC OWL_DMA_LLC_DAV(0)
  94. #define OWL_DMA_LLC_DAV_LOAD_NEXT OWL_DMA_LLC_DAV(1)
  95. #define OWL_DMA_LLC_DAV_LOAD_PREV OWL_DMA_LLC_DAV(2)
  96. #define OWL_DMA_LLC_SUSPEND BIT(16)
  97. /* OWL_DMAX_INT_CTL Bits */
  98. #define OWL_DMA_INTCTL_BLOCK BIT(0)
  99. #define OWL_DMA_INTCTL_SUPER_BLOCK BIT(1)
  100. #define OWL_DMA_INTCTL_FRAME BIT(2)
  101. #define OWL_DMA_INTCTL_HALF_FRAME BIT(3)
  102. #define OWL_DMA_INTCTL_LAST_FRAME BIT(4)
  103. /* OWL_DMAX_INT_STATUS Bits */
  104. #define OWL_DMA_INTSTAT_BLOCK BIT(0)
  105. #define OWL_DMA_INTSTAT_SUPER_BLOCK BIT(1)
  106. #define OWL_DMA_INTSTAT_FRAME BIT(2)
  107. #define OWL_DMA_INTSTAT_HALF_FRAME BIT(3)
  108. #define OWL_DMA_INTSTAT_LAST_FRAME BIT(4)
  109. /* Pack shift and newshift in a single word */
  110. #define BIT_FIELD(val, width, shift, newshift) \
  111. ((((val) >> (shift)) & ((BIT(width)) - 1)) << (newshift))
  112. /**
  113. * struct owl_dma_lli_hw - Hardware link list for dma transfer
  114. * @next_lli: physical address of the next link list
  115. * @saddr: source physical address
  116. * @daddr: destination physical address
  117. * @flen: frame length
  118. * @fcnt: frame count
  119. * @src_stride: source stride
  120. * @dst_stride: destination stride
  121. * @ctrla: dma_mode and linklist ctrl config
  122. * @ctrlb: interrupt config
  123. * @const_num: data for constant fill
  124. */
  125. struct owl_dma_lli_hw {
  126. u32 next_lli;
  127. u32 saddr;
  128. u32 daddr;
  129. u32 flen:20;
  130. u32 fcnt:12;
  131. u32 src_stride;
  132. u32 dst_stride;
  133. u32 ctrla;
  134. u32 ctrlb;
  135. u32 const_num;
  136. };
  137. /**
  138. * struct owl_dma_lli - Link list for dma transfer
  139. * @hw: hardware link list
  140. * @phys: physical address of hardware link list
  141. * @node: node for txd's lli_list
  142. */
  143. struct owl_dma_lli {
  144. struct owl_dma_lli_hw hw;
  145. dma_addr_t phys;
  146. struct list_head node;
  147. };
  148. /**
  149. * struct owl_dma_txd - Wrapper for struct dma_async_tx_descriptor
  150. * @vd: virtual DMA descriptor
  151. * @lli_list: link list of lli nodes
  152. */
  153. struct owl_dma_txd {
  154. struct virt_dma_desc vd;
  155. struct list_head lli_list;
  156. };
  157. /**
  158. * struct owl_dma_pchan - Holder for the physical channels
  159. * @id: physical index to this channel
  160. * @base: virtual memory base for the dma channel
  161. * @vchan: the virtual channel currently being served by this physical channel
  162. * @lock: a lock to use when altering an instance of this struct
  163. */
  164. struct owl_dma_pchan {
  165. u32 id;
  166. void __iomem *base;
  167. struct owl_dma_vchan *vchan;
  168. spinlock_t lock;
  169. };
  170. /**
  171. * struct owl_dma_pchan - Wrapper for DMA ENGINE channel
  172. * @vc: wrappped virtual channel
  173. * @pchan: the physical channel utilized by this channel
  174. * @txd: active transaction on this channel
  175. */
  176. struct owl_dma_vchan {
  177. struct virt_dma_chan vc;
  178. struct owl_dma_pchan *pchan;
  179. struct owl_dma_txd *txd;
  180. };
  181. /**
  182. * struct owl_dma - Holder for the Owl DMA controller
  183. * @dma: dma engine for this instance
  184. * @base: virtual memory base for the DMA controller
  185. * @clk: clock for the DMA controller
  186. * @lock: a lock to use when change DMA controller global register
  187. * @lli_pool: a pool for the LLI descriptors
  188. * @nr_pchans: the number of physical channels
  189. * @pchans: array of data for the physical channels
  190. * @nr_vchans: the number of physical channels
  191. * @vchans: array of data for the physical channels
  192. */
  193. struct owl_dma {
  194. struct dma_device dma;
  195. void __iomem *base;
  196. struct clk *clk;
  197. spinlock_t lock;
  198. struct dma_pool *lli_pool;
  199. int irq;
  200. unsigned int nr_pchans;
  201. struct owl_dma_pchan *pchans;
  202. unsigned int nr_vchans;
  203. struct owl_dma_vchan *vchans;
  204. };
  205. static void pchan_update(struct owl_dma_pchan *pchan, u32 reg,
  206. u32 val, bool state)
  207. {
  208. u32 regval;
  209. regval = readl(pchan->base + reg);
  210. if (state)
  211. regval |= val;
  212. else
  213. regval &= ~val;
  214. writel(val, pchan->base + reg);
  215. }
  216. static void pchan_writel(struct owl_dma_pchan *pchan, u32 reg, u32 data)
  217. {
  218. writel(data, pchan->base + reg);
  219. }
  220. static u32 pchan_readl(struct owl_dma_pchan *pchan, u32 reg)
  221. {
  222. return readl(pchan->base + reg);
  223. }
  224. static void dma_update(struct owl_dma *od, u32 reg, u32 val, bool state)
  225. {
  226. u32 regval;
  227. regval = readl(od->base + reg);
  228. if (state)
  229. regval |= val;
  230. else
  231. regval &= ~val;
  232. writel(val, od->base + reg);
  233. }
  234. static void dma_writel(struct owl_dma *od, u32 reg, u32 data)
  235. {
  236. writel(data, od->base + reg);
  237. }
  238. static u32 dma_readl(struct owl_dma *od, u32 reg)
  239. {
  240. return readl(od->base + reg);
  241. }
  242. static inline struct owl_dma *to_owl_dma(struct dma_device *dd)
  243. {
  244. return container_of(dd, struct owl_dma, dma);
  245. }
  246. static struct device *chan2dev(struct dma_chan *chan)
  247. {
  248. return &chan->dev->device;
  249. }
  250. static inline struct owl_dma_vchan *to_owl_vchan(struct dma_chan *chan)
  251. {
  252. return container_of(chan, struct owl_dma_vchan, vc.chan);
  253. }
  254. static inline struct owl_dma_txd *to_owl_txd(struct dma_async_tx_descriptor *tx)
  255. {
  256. return container_of(tx, struct owl_dma_txd, vd.tx);
  257. }
  258. static inline u32 llc_hw_ctrla(u32 mode, u32 llc_ctl)
  259. {
  260. u32 ctl;
  261. ctl = BIT_FIELD(mode, 4, 28, 28) |
  262. BIT_FIELD(mode, 8, 16, 20) |
  263. BIT_FIELD(mode, 4, 8, 16) |
  264. BIT_FIELD(mode, 6, 0, 10) |
  265. BIT_FIELD(llc_ctl, 2, 10, 8) |
  266. BIT_FIELD(llc_ctl, 2, 8, 6);
  267. return ctl;
  268. }
  269. static inline u32 llc_hw_ctrlb(u32 int_ctl)
  270. {
  271. u32 ctl;
  272. ctl = BIT_FIELD(int_ctl, 7, 0, 18);
  273. return ctl;
  274. }
  275. static void owl_dma_free_lli(struct owl_dma *od,
  276. struct owl_dma_lli *lli)
  277. {
  278. list_del(&lli->node);
  279. dma_pool_free(od->lli_pool, lli, lli->phys);
  280. }
  281. static struct owl_dma_lli *owl_dma_alloc_lli(struct owl_dma *od)
  282. {
  283. struct owl_dma_lli *lli;
  284. dma_addr_t phys;
  285. lli = dma_pool_alloc(od->lli_pool, GFP_NOWAIT, &phys);
  286. if (!lli)
  287. return NULL;
  288. INIT_LIST_HEAD(&lli->node);
  289. lli->phys = phys;
  290. return lli;
  291. }
  292. static struct owl_dma_lli *owl_dma_add_lli(struct owl_dma_txd *txd,
  293. struct owl_dma_lli *prev,
  294. struct owl_dma_lli *next)
  295. {
  296. list_add_tail(&next->node, &txd->lli_list);
  297. if (prev) {
  298. prev->hw.next_lli = next->phys;
  299. prev->hw.ctrla |= llc_hw_ctrla(OWL_DMA_MODE_LME, 0);
  300. }
  301. return next;
  302. }
  303. static inline int owl_dma_cfg_lli(struct owl_dma_vchan *vchan,
  304. struct owl_dma_lli *lli,
  305. dma_addr_t src, dma_addr_t dst,
  306. u32 len, enum dma_transfer_direction dir)
  307. {
  308. struct owl_dma_lli_hw *hw = &lli->hw;
  309. u32 mode;
  310. mode = OWL_DMA_MODE_PW(0);
  311. switch (dir) {
  312. case DMA_MEM_TO_MEM:
  313. mode |= OWL_DMA_MODE_TS(0) | OWL_DMA_MODE_ST_DCU |
  314. OWL_DMA_MODE_DT_DCU | OWL_DMA_MODE_SAM_INC |
  315. OWL_DMA_MODE_DAM_INC;
  316. break;
  317. default:
  318. return -EINVAL;
  319. }
  320. hw->next_lli = 0; /* One link list by default */
  321. hw->saddr = src;
  322. hw->daddr = dst;
  323. hw->fcnt = 1; /* Frame count fixed as 1 */
  324. hw->flen = len; /* Max frame length is 1MB */
  325. hw->src_stride = 0;
  326. hw->dst_stride = 0;
  327. hw->ctrla = llc_hw_ctrla(mode,
  328. OWL_DMA_LLC_SAV_LOAD_NEXT |
  329. OWL_DMA_LLC_DAV_LOAD_NEXT);
  330. hw->ctrlb = llc_hw_ctrlb(OWL_DMA_INTCTL_SUPER_BLOCK);
  331. return 0;
  332. }
  333. static struct owl_dma_pchan *owl_dma_get_pchan(struct owl_dma *od,
  334. struct owl_dma_vchan *vchan)
  335. {
  336. struct owl_dma_pchan *pchan = NULL;
  337. unsigned long flags;
  338. int i;
  339. for (i = 0; i < od->nr_pchans; i++) {
  340. pchan = &od->pchans[i];
  341. spin_lock_irqsave(&pchan->lock, flags);
  342. if (!pchan->vchan) {
  343. pchan->vchan = vchan;
  344. spin_unlock_irqrestore(&pchan->lock, flags);
  345. break;
  346. }
  347. spin_unlock_irqrestore(&pchan->lock, flags);
  348. }
  349. return pchan;
  350. }
  351. static int owl_dma_pchan_busy(struct owl_dma *od, struct owl_dma_pchan *pchan)
  352. {
  353. unsigned int val;
  354. val = dma_readl(od, OWL_DMA_IDLE_STAT);
  355. return !(val & (1 << pchan->id));
  356. }
  357. static void owl_dma_terminate_pchan(struct owl_dma *od,
  358. struct owl_dma_pchan *pchan)
  359. {
  360. unsigned long flags;
  361. u32 irq_pd;
  362. pchan_writel(pchan, OWL_DMAX_START, 0);
  363. pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
  364. spin_lock_irqsave(&od->lock, flags);
  365. dma_update(od, OWL_DMA_IRQ_EN0, (1 << pchan->id), false);
  366. irq_pd = dma_readl(od, OWL_DMA_IRQ_PD0);
  367. if (irq_pd & (1 << pchan->id)) {
  368. dev_warn(od->dma.dev,
  369. "terminating pchan %d that still has pending irq\n",
  370. pchan->id);
  371. dma_writel(od, OWL_DMA_IRQ_PD0, (1 << pchan->id));
  372. }
  373. pchan->vchan = NULL;
  374. spin_unlock_irqrestore(&od->lock, flags);
  375. }
  376. static int owl_dma_start_next_txd(struct owl_dma_vchan *vchan)
  377. {
  378. struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
  379. struct virt_dma_desc *vd = vchan_next_desc(&vchan->vc);
  380. struct owl_dma_pchan *pchan = vchan->pchan;
  381. struct owl_dma_txd *txd = to_owl_txd(&vd->tx);
  382. struct owl_dma_lli *lli;
  383. unsigned long flags;
  384. u32 int_ctl;
  385. list_del(&vd->node);
  386. vchan->txd = txd;
  387. /* Wait for channel inactive */
  388. while (owl_dma_pchan_busy(od, pchan))
  389. cpu_relax();
  390. lli = list_first_entry(&txd->lli_list,
  391. struct owl_dma_lli, node);
  392. int_ctl = OWL_DMA_INTCTL_SUPER_BLOCK;
  393. pchan_writel(pchan, OWL_DMAX_MODE, OWL_DMA_MODE_LME);
  394. pchan_writel(pchan, OWL_DMAX_LINKLIST_CTL,
  395. OWL_DMA_LLC_SAV_LOAD_NEXT | OWL_DMA_LLC_DAV_LOAD_NEXT);
  396. pchan_writel(pchan, OWL_DMAX_NEXT_DESCRIPTOR, lli->phys);
  397. pchan_writel(pchan, OWL_DMAX_INT_CTL, int_ctl);
  398. /* Clear IRQ status for this pchan */
  399. pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
  400. spin_lock_irqsave(&od->lock, flags);
  401. dma_update(od, OWL_DMA_IRQ_EN0, (1 << pchan->id), true);
  402. spin_unlock_irqrestore(&od->lock, flags);
  403. dev_dbg(chan2dev(&vchan->vc.chan), "starting pchan %d\n", pchan->id);
  404. /* Start DMA transfer for this pchan */
  405. pchan_writel(pchan, OWL_DMAX_START, 0x1);
  406. return 0;
  407. }
  408. static void owl_dma_phy_free(struct owl_dma *od, struct owl_dma_vchan *vchan)
  409. {
  410. /* Ensure that the physical channel is stopped */
  411. owl_dma_terminate_pchan(od, vchan->pchan);
  412. vchan->pchan = NULL;
  413. }
  414. static irqreturn_t owl_dma_interrupt(int irq, void *dev_id)
  415. {
  416. struct owl_dma *od = dev_id;
  417. struct owl_dma_vchan *vchan;
  418. struct owl_dma_pchan *pchan;
  419. unsigned long pending;
  420. int i;
  421. unsigned int global_irq_pending, chan_irq_pending;
  422. spin_lock(&od->lock);
  423. pending = dma_readl(od, OWL_DMA_IRQ_PD0);
  424. /* Clear IRQ status for each pchan */
  425. for_each_set_bit(i, &pending, od->nr_pchans) {
  426. pchan = &od->pchans[i];
  427. pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
  428. }
  429. /* Clear pending IRQ */
  430. dma_writel(od, OWL_DMA_IRQ_PD0, pending);
  431. /* Check missed pending IRQ */
  432. for (i = 0; i < od->nr_pchans; i++) {
  433. pchan = &od->pchans[i];
  434. chan_irq_pending = pchan_readl(pchan, OWL_DMAX_INT_CTL) &
  435. pchan_readl(pchan, OWL_DMAX_INT_STATUS);
  436. /* Dummy read to ensure OWL_DMA_IRQ_PD0 value is updated */
  437. dma_readl(od, OWL_DMA_IRQ_PD0);
  438. global_irq_pending = dma_readl(od, OWL_DMA_IRQ_PD0);
  439. if (chan_irq_pending && !(global_irq_pending & BIT(i))) {
  440. dev_dbg(od->dma.dev,
  441. "global and channel IRQ pending match err\n");
  442. /* Clear IRQ status for this pchan */
  443. pchan_update(pchan, OWL_DMAX_INT_STATUS,
  444. 0xff, false);
  445. /* Update global IRQ pending */
  446. pending |= BIT(i);
  447. }
  448. }
  449. spin_unlock(&od->lock);
  450. for_each_set_bit(i, &pending, od->nr_pchans) {
  451. struct owl_dma_txd *txd;
  452. pchan = &od->pchans[i];
  453. vchan = pchan->vchan;
  454. if (!vchan) {
  455. dev_warn(od->dma.dev, "no vchan attached on pchan %d\n",
  456. pchan->id);
  457. continue;
  458. }
  459. spin_lock(&vchan->vc.lock);
  460. txd = vchan->txd;
  461. if (txd) {
  462. vchan->txd = NULL;
  463. vchan_cookie_complete(&txd->vd);
  464. /*
  465. * Start the next descriptor (if any),
  466. * otherwise free this channel.
  467. */
  468. if (vchan_next_desc(&vchan->vc))
  469. owl_dma_start_next_txd(vchan);
  470. else
  471. owl_dma_phy_free(od, vchan);
  472. }
  473. spin_unlock(&vchan->vc.lock);
  474. }
  475. return IRQ_HANDLED;
  476. }
  477. static void owl_dma_free_txd(struct owl_dma *od, struct owl_dma_txd *txd)
  478. {
  479. struct owl_dma_lli *lli, *_lli;
  480. if (unlikely(!txd))
  481. return;
  482. list_for_each_entry_safe(lli, _lli, &txd->lli_list, node)
  483. owl_dma_free_lli(od, lli);
  484. kfree(txd);
  485. }
  486. static void owl_dma_desc_free(struct virt_dma_desc *vd)
  487. {
  488. struct owl_dma *od = to_owl_dma(vd->tx.chan->device);
  489. struct owl_dma_txd *txd = to_owl_txd(&vd->tx);
  490. owl_dma_free_txd(od, txd);
  491. }
  492. static int owl_dma_terminate_all(struct dma_chan *chan)
  493. {
  494. struct owl_dma *od = to_owl_dma(chan->device);
  495. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  496. unsigned long flags;
  497. LIST_HEAD(head);
  498. spin_lock_irqsave(&vchan->vc.lock, flags);
  499. if (vchan->pchan)
  500. owl_dma_phy_free(od, vchan);
  501. if (vchan->txd) {
  502. owl_dma_desc_free(&vchan->txd->vd);
  503. vchan->txd = NULL;
  504. }
  505. vchan_get_all_descriptors(&vchan->vc, &head);
  506. vchan_dma_desc_free_list(&vchan->vc, &head);
  507. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  508. return 0;
  509. }
  510. static u32 owl_dma_getbytes_chan(struct owl_dma_vchan *vchan)
  511. {
  512. struct owl_dma_pchan *pchan;
  513. struct owl_dma_txd *txd;
  514. struct owl_dma_lli *lli;
  515. unsigned int next_lli_phy;
  516. size_t bytes;
  517. pchan = vchan->pchan;
  518. txd = vchan->txd;
  519. if (!pchan || !txd)
  520. return 0;
  521. /* Get remain count of current node in link list */
  522. bytes = pchan_readl(pchan, OWL_DMAX_REMAIN_CNT);
  523. /* Loop through the preceding nodes to get total remaining bytes */
  524. if (pchan_readl(pchan, OWL_DMAX_MODE) & OWL_DMA_MODE_LME) {
  525. next_lli_phy = pchan_readl(pchan, OWL_DMAX_NEXT_DESCRIPTOR);
  526. list_for_each_entry(lli, &txd->lli_list, node) {
  527. /* Start from the next active node */
  528. if (lli->phys == next_lli_phy) {
  529. list_for_each_entry(lli, &txd->lli_list, node)
  530. bytes += lli->hw.flen;
  531. break;
  532. }
  533. }
  534. }
  535. return bytes;
  536. }
  537. static enum dma_status owl_dma_tx_status(struct dma_chan *chan,
  538. dma_cookie_t cookie,
  539. struct dma_tx_state *state)
  540. {
  541. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  542. struct owl_dma_lli *lli;
  543. struct virt_dma_desc *vd;
  544. struct owl_dma_txd *txd;
  545. enum dma_status ret;
  546. unsigned long flags;
  547. size_t bytes = 0;
  548. ret = dma_cookie_status(chan, cookie, state);
  549. if (ret == DMA_COMPLETE || !state)
  550. return ret;
  551. spin_lock_irqsave(&vchan->vc.lock, flags);
  552. vd = vchan_find_desc(&vchan->vc, cookie);
  553. if (vd) {
  554. txd = to_owl_txd(&vd->tx);
  555. list_for_each_entry(lli, &txd->lli_list, node)
  556. bytes += lli->hw.flen;
  557. } else {
  558. bytes = owl_dma_getbytes_chan(vchan);
  559. }
  560. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  561. dma_set_residue(state, bytes);
  562. return ret;
  563. }
  564. static void owl_dma_phy_alloc_and_start(struct owl_dma_vchan *vchan)
  565. {
  566. struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
  567. struct owl_dma_pchan *pchan;
  568. pchan = owl_dma_get_pchan(od, vchan);
  569. if (!pchan)
  570. return;
  571. dev_dbg(od->dma.dev, "allocated pchan %d\n", pchan->id);
  572. vchan->pchan = pchan;
  573. owl_dma_start_next_txd(vchan);
  574. }
  575. static void owl_dma_issue_pending(struct dma_chan *chan)
  576. {
  577. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  578. unsigned long flags;
  579. spin_lock_irqsave(&vchan->vc.lock, flags);
  580. if (vchan_issue_pending(&vchan->vc)) {
  581. if (!vchan->pchan)
  582. owl_dma_phy_alloc_and_start(vchan);
  583. }
  584. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  585. }
  586. static struct dma_async_tx_descriptor
  587. *owl_dma_prep_memcpy(struct dma_chan *chan,
  588. dma_addr_t dst, dma_addr_t src,
  589. size_t len, unsigned long flags)
  590. {
  591. struct owl_dma *od = to_owl_dma(chan->device);
  592. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  593. struct owl_dma_txd *txd;
  594. struct owl_dma_lli *lli, *prev = NULL;
  595. size_t offset, bytes;
  596. int ret;
  597. if (!len)
  598. return NULL;
  599. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  600. if (!txd)
  601. return NULL;
  602. INIT_LIST_HEAD(&txd->lli_list);
  603. /* Process the transfer as frame by frame */
  604. for (offset = 0; offset < len; offset += bytes) {
  605. lli = owl_dma_alloc_lli(od);
  606. if (!lli) {
  607. dev_warn(chan2dev(chan), "failed to allocate lli\n");
  608. goto err_txd_free;
  609. }
  610. bytes = min_t(size_t, (len - offset), OWL_DMA_FRAME_MAX_LENGTH);
  611. ret = owl_dma_cfg_lli(vchan, lli, src + offset, dst + offset,
  612. bytes, DMA_MEM_TO_MEM);
  613. if (ret) {
  614. dev_warn(chan2dev(chan), "failed to config lli\n");
  615. goto err_txd_free;
  616. }
  617. prev = owl_dma_add_lli(txd, prev, lli);
  618. }
  619. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  620. err_txd_free:
  621. owl_dma_free_txd(od, txd);
  622. return NULL;
  623. }
  624. static void owl_dma_free_chan_resources(struct dma_chan *chan)
  625. {
  626. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  627. /* Ensure all queued descriptors are freed */
  628. vchan_free_chan_resources(&vchan->vc);
  629. }
  630. static inline void owl_dma_free(struct owl_dma *od)
  631. {
  632. struct owl_dma_vchan *vchan = NULL;
  633. struct owl_dma_vchan *next;
  634. list_for_each_entry_safe(vchan,
  635. next, &od->dma.channels, vc.chan.device_node) {
  636. list_del(&vchan->vc.chan.device_node);
  637. tasklet_kill(&vchan->vc.task);
  638. }
  639. }
  640. static int owl_dma_probe(struct platform_device *pdev)
  641. {
  642. struct device_node *np = pdev->dev.of_node;
  643. struct owl_dma *od;
  644. struct resource *res;
  645. int ret, i, nr_channels, nr_requests;
  646. od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  647. if (!od)
  648. return -ENOMEM;
  649. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  650. if (!res)
  651. return -EINVAL;
  652. od->base = devm_ioremap_resource(&pdev->dev, res);
  653. if (IS_ERR(od->base))
  654. return PTR_ERR(od->base);
  655. ret = of_property_read_u32(np, "dma-channels", &nr_channels);
  656. if (ret) {
  657. dev_err(&pdev->dev, "can't get dma-channels\n");
  658. return ret;
  659. }
  660. ret = of_property_read_u32(np, "dma-requests", &nr_requests);
  661. if (ret) {
  662. dev_err(&pdev->dev, "can't get dma-requests\n");
  663. return ret;
  664. }
  665. dev_info(&pdev->dev, "dma-channels %d, dma-requests %d\n",
  666. nr_channels, nr_requests);
  667. od->nr_pchans = nr_channels;
  668. od->nr_vchans = nr_requests;
  669. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  670. platform_set_drvdata(pdev, od);
  671. spin_lock_init(&od->lock);
  672. dma_cap_set(DMA_MEMCPY, od->dma.cap_mask);
  673. od->dma.dev = &pdev->dev;
  674. od->dma.device_free_chan_resources = owl_dma_free_chan_resources;
  675. od->dma.device_tx_status = owl_dma_tx_status;
  676. od->dma.device_issue_pending = owl_dma_issue_pending;
  677. od->dma.device_prep_dma_memcpy = owl_dma_prep_memcpy;
  678. od->dma.device_terminate_all = owl_dma_terminate_all;
  679. od->dma.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  680. od->dma.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  681. od->dma.directions = BIT(DMA_MEM_TO_MEM);
  682. od->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  683. INIT_LIST_HEAD(&od->dma.channels);
  684. od->clk = devm_clk_get(&pdev->dev, NULL);
  685. if (IS_ERR(od->clk)) {
  686. dev_err(&pdev->dev, "unable to get clock\n");
  687. return PTR_ERR(od->clk);
  688. }
  689. /*
  690. * Eventhough the DMA controller is capable of generating 4
  691. * IRQ's for DMA priority feature, we only use 1 IRQ for
  692. * simplification.
  693. */
  694. od->irq = platform_get_irq(pdev, 0);
  695. ret = devm_request_irq(&pdev->dev, od->irq, owl_dma_interrupt, 0,
  696. dev_name(&pdev->dev), od);
  697. if (ret) {
  698. dev_err(&pdev->dev, "unable to request IRQ\n");
  699. return ret;
  700. }
  701. /* Init physical channel */
  702. od->pchans = devm_kcalloc(&pdev->dev, od->nr_pchans,
  703. sizeof(struct owl_dma_pchan), GFP_KERNEL);
  704. if (!od->pchans)
  705. return -ENOMEM;
  706. for (i = 0; i < od->nr_pchans; i++) {
  707. struct owl_dma_pchan *pchan = &od->pchans[i];
  708. pchan->id = i;
  709. pchan->base = od->base + OWL_DMA_CHAN_BASE(i);
  710. }
  711. /* Init virtual channel */
  712. od->vchans = devm_kcalloc(&pdev->dev, od->nr_vchans,
  713. sizeof(struct owl_dma_vchan), GFP_KERNEL);
  714. if (!od->vchans)
  715. return -ENOMEM;
  716. for (i = 0; i < od->nr_vchans; i++) {
  717. struct owl_dma_vchan *vchan = &od->vchans[i];
  718. vchan->vc.desc_free = owl_dma_desc_free;
  719. vchan_init(&vchan->vc, &od->dma);
  720. }
  721. /* Create a pool of consistent memory blocks for hardware descriptors */
  722. od->lli_pool = dma_pool_create(dev_name(od->dma.dev), od->dma.dev,
  723. sizeof(struct owl_dma_lli),
  724. __alignof__(struct owl_dma_lli),
  725. 0);
  726. if (!od->lli_pool) {
  727. dev_err(&pdev->dev, "unable to allocate DMA descriptor pool\n");
  728. return -ENOMEM;
  729. }
  730. clk_prepare_enable(od->clk);
  731. ret = dma_async_device_register(&od->dma);
  732. if (ret) {
  733. dev_err(&pdev->dev, "failed to register DMA engine device\n");
  734. goto err_pool_free;
  735. }
  736. return 0;
  737. err_pool_free:
  738. clk_disable_unprepare(od->clk);
  739. dma_pool_destroy(od->lli_pool);
  740. return ret;
  741. }
  742. static int owl_dma_remove(struct platform_device *pdev)
  743. {
  744. struct owl_dma *od = platform_get_drvdata(pdev);
  745. dma_async_device_unregister(&od->dma);
  746. /* Mask all interrupts for this execution environment */
  747. dma_writel(od, OWL_DMA_IRQ_EN0, 0x0);
  748. /* Make sure we won't have any further interrupts */
  749. devm_free_irq(od->dma.dev, od->irq, od);
  750. owl_dma_free(od);
  751. clk_disable_unprepare(od->clk);
  752. return 0;
  753. }
  754. static const struct of_device_id owl_dma_match[] = {
  755. { .compatible = "actions,s900-dma", },
  756. { /* sentinel */ }
  757. };
  758. MODULE_DEVICE_TABLE(of, owl_dma_match);
  759. static struct platform_driver owl_dma_driver = {
  760. .probe = owl_dma_probe,
  761. .remove = owl_dma_remove,
  762. .driver = {
  763. .name = "dma-owl",
  764. .of_match_table = of_match_ptr(owl_dma_match),
  765. },
  766. };
  767. static int owl_dma_init(void)
  768. {
  769. return platform_driver_register(&owl_dma_driver);
  770. }
  771. subsys_initcall(owl_dma_init);
  772. static void __exit owl_dma_exit(void)
  773. {
  774. platform_driver_unregister(&owl_dma_driver);
  775. }
  776. module_exit(owl_dma_exit);
  777. MODULE_AUTHOR("David Liu <liuwei@actions-semi.com>");
  778. MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
  779. MODULE_DESCRIPTION("Actions Semi Owl SoCs DMA driver");
  780. MODULE_LICENSE("GPL");