owl-dma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. */
  163. struct owl_dma_pchan {
  164. u32 id;
  165. void __iomem *base;
  166. struct owl_dma_vchan *vchan;
  167. };
  168. /**
  169. * struct owl_dma_pchan - Wrapper for DMA ENGINE channel
  170. * @vc: wrappped virtual channel
  171. * @pchan: the physical channel utilized by this channel
  172. * @txd: active transaction on this channel
  173. */
  174. struct owl_dma_vchan {
  175. struct virt_dma_chan vc;
  176. struct owl_dma_pchan *pchan;
  177. struct owl_dma_txd *txd;
  178. };
  179. /**
  180. * struct owl_dma - Holder for the Owl DMA controller
  181. * @dma: dma engine for this instance
  182. * @base: virtual memory base for the DMA controller
  183. * @clk: clock for the DMA controller
  184. * @lock: a lock to use when change DMA controller global register
  185. * @lli_pool: a pool for the LLI descriptors
  186. * @nr_pchans: the number of physical channels
  187. * @pchans: array of data for the physical channels
  188. * @nr_vchans: the number of physical channels
  189. * @vchans: array of data for the physical channels
  190. */
  191. struct owl_dma {
  192. struct dma_device dma;
  193. void __iomem *base;
  194. struct clk *clk;
  195. spinlock_t lock;
  196. struct dma_pool *lli_pool;
  197. int irq;
  198. unsigned int nr_pchans;
  199. struct owl_dma_pchan *pchans;
  200. unsigned int nr_vchans;
  201. struct owl_dma_vchan *vchans;
  202. };
  203. static void pchan_update(struct owl_dma_pchan *pchan, u32 reg,
  204. u32 val, bool state)
  205. {
  206. u32 regval;
  207. regval = readl(pchan->base + reg);
  208. if (state)
  209. regval |= val;
  210. else
  211. regval &= ~val;
  212. writel(val, pchan->base + reg);
  213. }
  214. static void pchan_writel(struct owl_dma_pchan *pchan, u32 reg, u32 data)
  215. {
  216. writel(data, pchan->base + reg);
  217. }
  218. static u32 pchan_readl(struct owl_dma_pchan *pchan, u32 reg)
  219. {
  220. return readl(pchan->base + reg);
  221. }
  222. static void dma_update(struct owl_dma *od, u32 reg, u32 val, bool state)
  223. {
  224. u32 regval;
  225. regval = readl(od->base + reg);
  226. if (state)
  227. regval |= val;
  228. else
  229. regval &= ~val;
  230. writel(val, od->base + reg);
  231. }
  232. static void dma_writel(struct owl_dma *od, u32 reg, u32 data)
  233. {
  234. writel(data, od->base + reg);
  235. }
  236. static u32 dma_readl(struct owl_dma *od, u32 reg)
  237. {
  238. return readl(od->base + reg);
  239. }
  240. static inline struct owl_dma *to_owl_dma(struct dma_device *dd)
  241. {
  242. return container_of(dd, struct owl_dma, dma);
  243. }
  244. static struct device *chan2dev(struct dma_chan *chan)
  245. {
  246. return &chan->dev->device;
  247. }
  248. static inline struct owl_dma_vchan *to_owl_vchan(struct dma_chan *chan)
  249. {
  250. return container_of(chan, struct owl_dma_vchan, vc.chan);
  251. }
  252. static inline struct owl_dma_txd *to_owl_txd(struct dma_async_tx_descriptor *tx)
  253. {
  254. return container_of(tx, struct owl_dma_txd, vd.tx);
  255. }
  256. static inline u32 llc_hw_ctrla(u32 mode, u32 llc_ctl)
  257. {
  258. u32 ctl;
  259. ctl = BIT_FIELD(mode, 4, 28, 28) |
  260. BIT_FIELD(mode, 8, 16, 20) |
  261. BIT_FIELD(mode, 4, 8, 16) |
  262. BIT_FIELD(mode, 6, 0, 10) |
  263. BIT_FIELD(llc_ctl, 2, 10, 8) |
  264. BIT_FIELD(llc_ctl, 2, 8, 6);
  265. return ctl;
  266. }
  267. static inline u32 llc_hw_ctrlb(u32 int_ctl)
  268. {
  269. u32 ctl;
  270. ctl = BIT_FIELD(int_ctl, 7, 0, 18);
  271. return ctl;
  272. }
  273. static void owl_dma_free_lli(struct owl_dma *od,
  274. struct owl_dma_lli *lli)
  275. {
  276. list_del(&lli->node);
  277. dma_pool_free(od->lli_pool, lli, lli->phys);
  278. }
  279. static struct owl_dma_lli *owl_dma_alloc_lli(struct owl_dma *od)
  280. {
  281. struct owl_dma_lli *lli;
  282. dma_addr_t phys;
  283. lli = dma_pool_alloc(od->lli_pool, GFP_NOWAIT, &phys);
  284. if (!lli)
  285. return NULL;
  286. INIT_LIST_HEAD(&lli->node);
  287. lli->phys = phys;
  288. return lli;
  289. }
  290. static struct owl_dma_lli *owl_dma_add_lli(struct owl_dma_txd *txd,
  291. struct owl_dma_lli *prev,
  292. struct owl_dma_lli *next)
  293. {
  294. list_add_tail(&next->node, &txd->lli_list);
  295. if (prev) {
  296. prev->hw.next_lli = next->phys;
  297. prev->hw.ctrla |= llc_hw_ctrla(OWL_DMA_MODE_LME, 0);
  298. }
  299. return next;
  300. }
  301. static inline int owl_dma_cfg_lli(struct owl_dma_vchan *vchan,
  302. struct owl_dma_lli *lli,
  303. dma_addr_t src, dma_addr_t dst,
  304. u32 len, enum dma_transfer_direction dir)
  305. {
  306. struct owl_dma_lli_hw *hw = &lli->hw;
  307. u32 mode;
  308. mode = OWL_DMA_MODE_PW(0);
  309. switch (dir) {
  310. case DMA_MEM_TO_MEM:
  311. mode |= OWL_DMA_MODE_TS(0) | OWL_DMA_MODE_ST_DCU |
  312. OWL_DMA_MODE_DT_DCU | OWL_DMA_MODE_SAM_INC |
  313. OWL_DMA_MODE_DAM_INC;
  314. break;
  315. default:
  316. return -EINVAL;
  317. }
  318. hw->next_lli = 0; /* One link list by default */
  319. hw->saddr = src;
  320. hw->daddr = dst;
  321. hw->fcnt = 1; /* Frame count fixed as 1 */
  322. hw->flen = len; /* Max frame length is 1MB */
  323. hw->src_stride = 0;
  324. hw->dst_stride = 0;
  325. hw->ctrla = llc_hw_ctrla(mode,
  326. OWL_DMA_LLC_SAV_LOAD_NEXT |
  327. OWL_DMA_LLC_DAV_LOAD_NEXT);
  328. hw->ctrlb = llc_hw_ctrlb(OWL_DMA_INTCTL_SUPER_BLOCK);
  329. return 0;
  330. }
  331. static struct owl_dma_pchan *owl_dma_get_pchan(struct owl_dma *od,
  332. struct owl_dma_vchan *vchan)
  333. {
  334. struct owl_dma_pchan *pchan = NULL;
  335. unsigned long flags;
  336. int i;
  337. for (i = 0; i < od->nr_pchans; i++) {
  338. pchan = &od->pchans[i];
  339. spin_lock_irqsave(&od->lock, flags);
  340. if (!pchan->vchan) {
  341. pchan->vchan = vchan;
  342. spin_unlock_irqrestore(&od->lock, flags);
  343. break;
  344. }
  345. spin_unlock_irqrestore(&od->lock, flags);
  346. }
  347. return pchan;
  348. }
  349. static int owl_dma_pchan_busy(struct owl_dma *od, struct owl_dma_pchan *pchan)
  350. {
  351. unsigned int val;
  352. val = dma_readl(od, OWL_DMA_IDLE_STAT);
  353. return !(val & (1 << pchan->id));
  354. }
  355. static void owl_dma_terminate_pchan(struct owl_dma *od,
  356. struct owl_dma_pchan *pchan)
  357. {
  358. unsigned long flags;
  359. u32 irq_pd;
  360. pchan_writel(pchan, OWL_DMAX_START, 0);
  361. pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
  362. spin_lock_irqsave(&od->lock, flags);
  363. dma_update(od, OWL_DMA_IRQ_EN0, (1 << pchan->id), false);
  364. irq_pd = dma_readl(od, OWL_DMA_IRQ_PD0);
  365. if (irq_pd & (1 << pchan->id)) {
  366. dev_warn(od->dma.dev,
  367. "terminating pchan %d that still has pending irq\n",
  368. pchan->id);
  369. dma_writel(od, OWL_DMA_IRQ_PD0, (1 << pchan->id));
  370. }
  371. pchan->vchan = NULL;
  372. spin_unlock_irqrestore(&od->lock, flags);
  373. }
  374. static int owl_dma_start_next_txd(struct owl_dma_vchan *vchan)
  375. {
  376. struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
  377. struct virt_dma_desc *vd = vchan_next_desc(&vchan->vc);
  378. struct owl_dma_pchan *pchan = vchan->pchan;
  379. struct owl_dma_txd *txd = to_owl_txd(&vd->tx);
  380. struct owl_dma_lli *lli;
  381. unsigned long flags;
  382. u32 int_ctl;
  383. list_del(&vd->node);
  384. vchan->txd = txd;
  385. /* Wait for channel inactive */
  386. while (owl_dma_pchan_busy(od, pchan))
  387. cpu_relax();
  388. lli = list_first_entry(&txd->lli_list,
  389. struct owl_dma_lli, node);
  390. int_ctl = OWL_DMA_INTCTL_SUPER_BLOCK;
  391. pchan_writel(pchan, OWL_DMAX_MODE, OWL_DMA_MODE_LME);
  392. pchan_writel(pchan, OWL_DMAX_LINKLIST_CTL,
  393. OWL_DMA_LLC_SAV_LOAD_NEXT | OWL_DMA_LLC_DAV_LOAD_NEXT);
  394. pchan_writel(pchan, OWL_DMAX_NEXT_DESCRIPTOR, lli->phys);
  395. pchan_writel(pchan, OWL_DMAX_INT_CTL, int_ctl);
  396. /* Clear IRQ status for this pchan */
  397. pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
  398. spin_lock_irqsave(&od->lock, flags);
  399. dma_update(od, OWL_DMA_IRQ_EN0, (1 << pchan->id), true);
  400. spin_unlock_irqrestore(&od->lock, flags);
  401. dev_dbg(chan2dev(&vchan->vc.chan), "starting pchan %d\n", pchan->id);
  402. /* Start DMA transfer for this pchan */
  403. pchan_writel(pchan, OWL_DMAX_START, 0x1);
  404. return 0;
  405. }
  406. static void owl_dma_phy_free(struct owl_dma *od, struct owl_dma_vchan *vchan)
  407. {
  408. /* Ensure that the physical channel is stopped */
  409. owl_dma_terminate_pchan(od, vchan->pchan);
  410. vchan->pchan = NULL;
  411. }
  412. static irqreturn_t owl_dma_interrupt(int irq, void *dev_id)
  413. {
  414. struct owl_dma *od = dev_id;
  415. struct owl_dma_vchan *vchan;
  416. struct owl_dma_pchan *pchan;
  417. unsigned long pending;
  418. int i;
  419. unsigned int global_irq_pending, chan_irq_pending;
  420. spin_lock(&od->lock);
  421. pending = dma_readl(od, OWL_DMA_IRQ_PD0);
  422. /* Clear IRQ status for each pchan */
  423. for_each_set_bit(i, &pending, od->nr_pchans) {
  424. pchan = &od->pchans[i];
  425. pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
  426. }
  427. /* Clear pending IRQ */
  428. dma_writel(od, OWL_DMA_IRQ_PD0, pending);
  429. /* Check missed pending IRQ */
  430. for (i = 0; i < od->nr_pchans; i++) {
  431. pchan = &od->pchans[i];
  432. chan_irq_pending = pchan_readl(pchan, OWL_DMAX_INT_CTL) &
  433. pchan_readl(pchan, OWL_DMAX_INT_STATUS);
  434. /* Dummy read to ensure OWL_DMA_IRQ_PD0 value is updated */
  435. dma_readl(od, OWL_DMA_IRQ_PD0);
  436. global_irq_pending = dma_readl(od, OWL_DMA_IRQ_PD0);
  437. if (chan_irq_pending && !(global_irq_pending & BIT(i))) {
  438. dev_dbg(od->dma.dev,
  439. "global and channel IRQ pending match err\n");
  440. /* Clear IRQ status for this pchan */
  441. pchan_update(pchan, OWL_DMAX_INT_STATUS,
  442. 0xff, false);
  443. /* Update global IRQ pending */
  444. pending |= BIT(i);
  445. }
  446. }
  447. spin_unlock(&od->lock);
  448. for_each_set_bit(i, &pending, od->nr_pchans) {
  449. struct owl_dma_txd *txd;
  450. pchan = &od->pchans[i];
  451. vchan = pchan->vchan;
  452. if (!vchan) {
  453. dev_warn(od->dma.dev, "no vchan attached on pchan %d\n",
  454. pchan->id);
  455. continue;
  456. }
  457. spin_lock(&vchan->vc.lock);
  458. txd = vchan->txd;
  459. if (txd) {
  460. vchan->txd = NULL;
  461. vchan_cookie_complete(&txd->vd);
  462. /*
  463. * Start the next descriptor (if any),
  464. * otherwise free this channel.
  465. */
  466. if (vchan_next_desc(&vchan->vc))
  467. owl_dma_start_next_txd(vchan);
  468. else
  469. owl_dma_phy_free(od, vchan);
  470. }
  471. spin_unlock(&vchan->vc.lock);
  472. }
  473. return IRQ_HANDLED;
  474. }
  475. static void owl_dma_free_txd(struct owl_dma *od, struct owl_dma_txd *txd)
  476. {
  477. struct owl_dma_lli *lli, *_lli;
  478. if (unlikely(!txd))
  479. return;
  480. list_for_each_entry_safe(lli, _lli, &txd->lli_list, node)
  481. owl_dma_free_lli(od, lli);
  482. kfree(txd);
  483. }
  484. static void owl_dma_desc_free(struct virt_dma_desc *vd)
  485. {
  486. struct owl_dma *od = to_owl_dma(vd->tx.chan->device);
  487. struct owl_dma_txd *txd = to_owl_txd(&vd->tx);
  488. owl_dma_free_txd(od, txd);
  489. }
  490. static int owl_dma_terminate_all(struct dma_chan *chan)
  491. {
  492. struct owl_dma *od = to_owl_dma(chan->device);
  493. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  494. unsigned long flags;
  495. LIST_HEAD(head);
  496. spin_lock_irqsave(&vchan->vc.lock, flags);
  497. if (vchan->pchan)
  498. owl_dma_phy_free(od, vchan);
  499. if (vchan->txd) {
  500. owl_dma_desc_free(&vchan->txd->vd);
  501. vchan->txd = NULL;
  502. }
  503. vchan_get_all_descriptors(&vchan->vc, &head);
  504. vchan_dma_desc_free_list(&vchan->vc, &head);
  505. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  506. return 0;
  507. }
  508. static u32 owl_dma_getbytes_chan(struct owl_dma_vchan *vchan)
  509. {
  510. struct owl_dma_pchan *pchan;
  511. struct owl_dma_txd *txd;
  512. struct owl_dma_lli *lli;
  513. unsigned int next_lli_phy;
  514. size_t bytes;
  515. pchan = vchan->pchan;
  516. txd = vchan->txd;
  517. if (!pchan || !txd)
  518. return 0;
  519. /* Get remain count of current node in link list */
  520. bytes = pchan_readl(pchan, OWL_DMAX_REMAIN_CNT);
  521. /* Loop through the preceding nodes to get total remaining bytes */
  522. if (pchan_readl(pchan, OWL_DMAX_MODE) & OWL_DMA_MODE_LME) {
  523. next_lli_phy = pchan_readl(pchan, OWL_DMAX_NEXT_DESCRIPTOR);
  524. list_for_each_entry(lli, &txd->lli_list, node) {
  525. /* Start from the next active node */
  526. if (lli->phys == next_lli_phy) {
  527. list_for_each_entry(lli, &txd->lli_list, node)
  528. bytes += lli->hw.flen;
  529. break;
  530. }
  531. }
  532. }
  533. return bytes;
  534. }
  535. static enum dma_status owl_dma_tx_status(struct dma_chan *chan,
  536. dma_cookie_t cookie,
  537. struct dma_tx_state *state)
  538. {
  539. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  540. struct owl_dma_lli *lli;
  541. struct virt_dma_desc *vd;
  542. struct owl_dma_txd *txd;
  543. enum dma_status ret;
  544. unsigned long flags;
  545. size_t bytes = 0;
  546. ret = dma_cookie_status(chan, cookie, state);
  547. if (ret == DMA_COMPLETE || !state)
  548. return ret;
  549. spin_lock_irqsave(&vchan->vc.lock, flags);
  550. vd = vchan_find_desc(&vchan->vc, cookie);
  551. if (vd) {
  552. txd = to_owl_txd(&vd->tx);
  553. list_for_each_entry(lli, &txd->lli_list, node)
  554. bytes += lli->hw.flen;
  555. } else {
  556. bytes = owl_dma_getbytes_chan(vchan);
  557. }
  558. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  559. dma_set_residue(state, bytes);
  560. return ret;
  561. }
  562. static void owl_dma_phy_alloc_and_start(struct owl_dma_vchan *vchan)
  563. {
  564. struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
  565. struct owl_dma_pchan *pchan;
  566. pchan = owl_dma_get_pchan(od, vchan);
  567. if (!pchan)
  568. return;
  569. dev_dbg(od->dma.dev, "allocated pchan %d\n", pchan->id);
  570. vchan->pchan = pchan;
  571. owl_dma_start_next_txd(vchan);
  572. }
  573. static void owl_dma_issue_pending(struct dma_chan *chan)
  574. {
  575. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  576. unsigned long flags;
  577. spin_lock_irqsave(&vchan->vc.lock, flags);
  578. if (vchan_issue_pending(&vchan->vc)) {
  579. if (!vchan->pchan)
  580. owl_dma_phy_alloc_and_start(vchan);
  581. }
  582. spin_unlock_irqrestore(&vchan->vc.lock, flags);
  583. }
  584. static struct dma_async_tx_descriptor
  585. *owl_dma_prep_memcpy(struct dma_chan *chan,
  586. dma_addr_t dst, dma_addr_t src,
  587. size_t len, unsigned long flags)
  588. {
  589. struct owl_dma *od = to_owl_dma(chan->device);
  590. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  591. struct owl_dma_txd *txd;
  592. struct owl_dma_lli *lli, *prev = NULL;
  593. size_t offset, bytes;
  594. int ret;
  595. if (!len)
  596. return NULL;
  597. txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
  598. if (!txd)
  599. return NULL;
  600. INIT_LIST_HEAD(&txd->lli_list);
  601. /* Process the transfer as frame by frame */
  602. for (offset = 0; offset < len; offset += bytes) {
  603. lli = owl_dma_alloc_lli(od);
  604. if (!lli) {
  605. dev_warn(chan2dev(chan), "failed to allocate lli\n");
  606. goto err_txd_free;
  607. }
  608. bytes = min_t(size_t, (len - offset), OWL_DMA_FRAME_MAX_LENGTH);
  609. ret = owl_dma_cfg_lli(vchan, lli, src + offset, dst + offset,
  610. bytes, DMA_MEM_TO_MEM);
  611. if (ret) {
  612. dev_warn(chan2dev(chan), "failed to config lli\n");
  613. goto err_txd_free;
  614. }
  615. prev = owl_dma_add_lli(txd, prev, lli);
  616. }
  617. return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
  618. err_txd_free:
  619. owl_dma_free_txd(od, txd);
  620. return NULL;
  621. }
  622. static void owl_dma_free_chan_resources(struct dma_chan *chan)
  623. {
  624. struct owl_dma_vchan *vchan = to_owl_vchan(chan);
  625. /* Ensure all queued descriptors are freed */
  626. vchan_free_chan_resources(&vchan->vc);
  627. }
  628. static inline void owl_dma_free(struct owl_dma *od)
  629. {
  630. struct owl_dma_vchan *vchan = NULL;
  631. struct owl_dma_vchan *next;
  632. list_for_each_entry_safe(vchan,
  633. next, &od->dma.channels, vc.chan.device_node) {
  634. list_del(&vchan->vc.chan.device_node);
  635. tasklet_kill(&vchan->vc.task);
  636. }
  637. }
  638. static int owl_dma_probe(struct platform_device *pdev)
  639. {
  640. struct device_node *np = pdev->dev.of_node;
  641. struct owl_dma *od;
  642. struct resource *res;
  643. int ret, i, nr_channels, nr_requests;
  644. od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  645. if (!od)
  646. return -ENOMEM;
  647. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  648. if (!res)
  649. return -EINVAL;
  650. od->base = devm_ioremap_resource(&pdev->dev, res);
  651. if (IS_ERR(od->base))
  652. return PTR_ERR(od->base);
  653. ret = of_property_read_u32(np, "dma-channels", &nr_channels);
  654. if (ret) {
  655. dev_err(&pdev->dev, "can't get dma-channels\n");
  656. return ret;
  657. }
  658. ret = of_property_read_u32(np, "dma-requests", &nr_requests);
  659. if (ret) {
  660. dev_err(&pdev->dev, "can't get dma-requests\n");
  661. return ret;
  662. }
  663. dev_info(&pdev->dev, "dma-channels %d, dma-requests %d\n",
  664. nr_channels, nr_requests);
  665. od->nr_pchans = nr_channels;
  666. od->nr_vchans = nr_requests;
  667. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  668. platform_set_drvdata(pdev, od);
  669. spin_lock_init(&od->lock);
  670. dma_cap_set(DMA_MEMCPY, od->dma.cap_mask);
  671. od->dma.dev = &pdev->dev;
  672. od->dma.device_free_chan_resources = owl_dma_free_chan_resources;
  673. od->dma.device_tx_status = owl_dma_tx_status;
  674. od->dma.device_issue_pending = owl_dma_issue_pending;
  675. od->dma.device_prep_dma_memcpy = owl_dma_prep_memcpy;
  676. od->dma.device_terminate_all = owl_dma_terminate_all;
  677. od->dma.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  678. od->dma.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  679. od->dma.directions = BIT(DMA_MEM_TO_MEM);
  680. od->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  681. INIT_LIST_HEAD(&od->dma.channels);
  682. od->clk = devm_clk_get(&pdev->dev, NULL);
  683. if (IS_ERR(od->clk)) {
  684. dev_err(&pdev->dev, "unable to get clock\n");
  685. return PTR_ERR(od->clk);
  686. }
  687. /*
  688. * Eventhough the DMA controller is capable of generating 4
  689. * IRQ's for DMA priority feature, we only use 1 IRQ for
  690. * simplification.
  691. */
  692. od->irq = platform_get_irq(pdev, 0);
  693. ret = devm_request_irq(&pdev->dev, od->irq, owl_dma_interrupt, 0,
  694. dev_name(&pdev->dev), od);
  695. if (ret) {
  696. dev_err(&pdev->dev, "unable to request IRQ\n");
  697. return ret;
  698. }
  699. /* Init physical channel */
  700. od->pchans = devm_kcalloc(&pdev->dev, od->nr_pchans,
  701. sizeof(struct owl_dma_pchan), GFP_KERNEL);
  702. if (!od->pchans)
  703. return -ENOMEM;
  704. for (i = 0; i < od->nr_pchans; i++) {
  705. struct owl_dma_pchan *pchan = &od->pchans[i];
  706. pchan->id = i;
  707. pchan->base = od->base + OWL_DMA_CHAN_BASE(i);
  708. }
  709. /* Init virtual channel */
  710. od->vchans = devm_kcalloc(&pdev->dev, od->nr_vchans,
  711. sizeof(struct owl_dma_vchan), GFP_KERNEL);
  712. if (!od->vchans)
  713. return -ENOMEM;
  714. for (i = 0; i < od->nr_vchans; i++) {
  715. struct owl_dma_vchan *vchan = &od->vchans[i];
  716. vchan->vc.desc_free = owl_dma_desc_free;
  717. vchan_init(&vchan->vc, &od->dma);
  718. }
  719. /* Create a pool of consistent memory blocks for hardware descriptors */
  720. od->lli_pool = dma_pool_create(dev_name(od->dma.dev), od->dma.dev,
  721. sizeof(struct owl_dma_lli),
  722. __alignof__(struct owl_dma_lli),
  723. 0);
  724. if (!od->lli_pool) {
  725. dev_err(&pdev->dev, "unable to allocate DMA descriptor pool\n");
  726. return -ENOMEM;
  727. }
  728. clk_prepare_enable(od->clk);
  729. ret = dma_async_device_register(&od->dma);
  730. if (ret) {
  731. dev_err(&pdev->dev, "failed to register DMA engine device\n");
  732. goto err_pool_free;
  733. }
  734. return 0;
  735. err_pool_free:
  736. clk_disable_unprepare(od->clk);
  737. dma_pool_destroy(od->lli_pool);
  738. return ret;
  739. }
  740. static int owl_dma_remove(struct platform_device *pdev)
  741. {
  742. struct owl_dma *od = platform_get_drvdata(pdev);
  743. dma_async_device_unregister(&od->dma);
  744. /* Mask all interrupts for this execution environment */
  745. dma_writel(od, OWL_DMA_IRQ_EN0, 0x0);
  746. /* Make sure we won't have any further interrupts */
  747. devm_free_irq(od->dma.dev, od->irq, od);
  748. owl_dma_free(od);
  749. clk_disable_unprepare(od->clk);
  750. dma_pool_destroy(od->lli_pool);
  751. return 0;
  752. }
  753. static const struct of_device_id owl_dma_match[] = {
  754. { .compatible = "actions,s900-dma", },
  755. { /* sentinel */ }
  756. };
  757. MODULE_DEVICE_TABLE(of, owl_dma_match);
  758. static struct platform_driver owl_dma_driver = {
  759. .probe = owl_dma_probe,
  760. .remove = owl_dma_remove,
  761. .driver = {
  762. .name = "dma-owl",
  763. .of_match_table = of_match_ptr(owl_dma_match),
  764. },
  765. };
  766. static int owl_dma_init(void)
  767. {
  768. return platform_driver_register(&owl_dma_driver);
  769. }
  770. subsys_initcall(owl_dma_init);
  771. static void __exit owl_dma_exit(void)
  772. {
  773. platform_driver_unregister(&owl_dma_driver);
  774. }
  775. module_exit(owl_dma_exit);
  776. MODULE_AUTHOR("David Liu <liuwei@actions-semi.com>");
  777. MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
  778. MODULE_DESCRIPTION("Actions Semi Owl SoCs DMA driver");
  779. MODULE_LICENSE("GPL");