rz-dmac.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas RZ/G2L DMA Controller Driver
  4. *
  5. * Based on imx-dma.c
  6. *
  7. * Copyright (C) 2021 Renesas Electronics Corp.
  8. * Copyright 2010 Sascha Hauer, Pengutronix <[email protected]>
  9. * Copyright 2012 Javier Martin, Vista Silicon <[email protected]>
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/iopoll.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_dma.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include "../dmaengine.h"
  26. #include "../virt-dma.h"
  27. enum rz_dmac_prep_type {
  28. RZ_DMAC_DESC_MEMCPY,
  29. RZ_DMAC_DESC_SLAVE_SG,
  30. };
  31. struct rz_lmdesc {
  32. u32 header;
  33. u32 sa;
  34. u32 da;
  35. u32 tb;
  36. u32 chcfg;
  37. u32 chitvl;
  38. u32 chext;
  39. u32 nxla;
  40. };
  41. struct rz_dmac_desc {
  42. struct virt_dma_desc vd;
  43. dma_addr_t src;
  44. dma_addr_t dest;
  45. size_t len;
  46. struct list_head node;
  47. enum dma_transfer_direction direction;
  48. enum rz_dmac_prep_type type;
  49. /* For slave sg */
  50. struct scatterlist *sg;
  51. unsigned int sgcount;
  52. };
  53. #define to_rz_dmac_desc(d) container_of(d, struct rz_dmac_desc, vd)
  54. struct rz_dmac_chan {
  55. struct virt_dma_chan vc;
  56. void __iomem *ch_base;
  57. void __iomem *ch_cmn_base;
  58. unsigned int index;
  59. int irq;
  60. struct rz_dmac_desc *desc;
  61. int descs_allocated;
  62. enum dma_slave_buswidth src_word_size;
  63. enum dma_slave_buswidth dst_word_size;
  64. dma_addr_t src_per_address;
  65. dma_addr_t dst_per_address;
  66. u32 chcfg;
  67. u32 chctrl;
  68. int mid_rid;
  69. struct list_head ld_free;
  70. struct list_head ld_queue;
  71. struct list_head ld_active;
  72. struct {
  73. struct rz_lmdesc *base;
  74. struct rz_lmdesc *head;
  75. struct rz_lmdesc *tail;
  76. dma_addr_t base_dma;
  77. } lmdesc;
  78. };
  79. #define to_rz_dmac_chan(c) container_of(c, struct rz_dmac_chan, vc.chan)
  80. struct rz_dmac {
  81. struct dma_device engine;
  82. struct device *dev;
  83. void __iomem *base;
  84. void __iomem *ext_base;
  85. unsigned int n_channels;
  86. struct rz_dmac_chan *channels;
  87. DECLARE_BITMAP(modules, 1024);
  88. };
  89. #define to_rz_dmac(d) container_of(d, struct rz_dmac, engine)
  90. /*
  91. * -----------------------------------------------------------------------------
  92. * Registers
  93. */
  94. #define CHSTAT 0x0024
  95. #define CHCTRL 0x0028
  96. #define CHCFG 0x002c
  97. #define NXLA 0x0038
  98. #define DCTRL 0x0000
  99. #define EACH_CHANNEL_OFFSET 0x0040
  100. #define CHANNEL_0_7_OFFSET 0x0000
  101. #define CHANNEL_0_7_COMMON_BASE 0x0300
  102. #define CHANNEL_8_15_OFFSET 0x0400
  103. #define CHANNEL_8_15_COMMON_BASE 0x0700
  104. #define CHSTAT_ER BIT(4)
  105. #define CHSTAT_EN BIT(0)
  106. #define CHCTRL_CLRINTMSK BIT(17)
  107. #define CHCTRL_CLRSUS BIT(9)
  108. #define CHCTRL_CLRTC BIT(6)
  109. #define CHCTRL_CLREND BIT(5)
  110. #define CHCTRL_CLRRQ BIT(4)
  111. #define CHCTRL_SWRST BIT(3)
  112. #define CHCTRL_STG BIT(2)
  113. #define CHCTRL_CLREN BIT(1)
  114. #define CHCTRL_SETEN BIT(0)
  115. #define CHCTRL_DEFAULT (CHCTRL_CLRINTMSK | CHCTRL_CLRSUS | \
  116. CHCTRL_CLRTC | CHCTRL_CLREND | \
  117. CHCTRL_CLRRQ | CHCTRL_SWRST | \
  118. CHCTRL_CLREN)
  119. #define CHCFG_DMS BIT(31)
  120. #define CHCFG_DEM BIT(24)
  121. #define CHCFG_DAD BIT(21)
  122. #define CHCFG_SAD BIT(20)
  123. #define CHCFG_REQD BIT(3)
  124. #define CHCFG_SEL(bits) ((bits) & 0x07)
  125. #define CHCFG_MEM_COPY (0x80400008)
  126. #define CHCFG_FILL_DDS_MASK GENMASK(19, 16)
  127. #define CHCFG_FILL_SDS_MASK GENMASK(15, 12)
  128. #define CHCFG_FILL_TM(a) (((a) & BIT(5)) << 22)
  129. #define CHCFG_FILL_AM(a) (((a) & GENMASK(4, 2)) << 6)
  130. #define CHCFG_FILL_LVL(a) (((a) & BIT(1)) << 5)
  131. #define CHCFG_FILL_HIEN(a) (((a) & BIT(0)) << 5)
  132. #define MID_RID_MASK GENMASK(9, 0)
  133. #define CHCFG_MASK GENMASK(15, 10)
  134. #define CHCFG_DS_INVALID 0xFF
  135. #define DCTRL_LVINT BIT(1)
  136. #define DCTRL_PR BIT(0)
  137. #define DCTRL_DEFAULT (DCTRL_LVINT | DCTRL_PR)
  138. /* LINK MODE DESCRIPTOR */
  139. #define HEADER_LV BIT(0)
  140. #define RZ_DMAC_MAX_CHAN_DESCRIPTORS 16
  141. #define RZ_DMAC_MAX_CHANNELS 16
  142. #define DMAC_NR_LMDESC 64
  143. /*
  144. * -----------------------------------------------------------------------------
  145. * Device access
  146. */
  147. static void rz_dmac_writel(struct rz_dmac *dmac, unsigned int val,
  148. unsigned int offset)
  149. {
  150. writel(val, dmac->base + offset);
  151. }
  152. static void rz_dmac_ext_writel(struct rz_dmac *dmac, unsigned int val,
  153. unsigned int offset)
  154. {
  155. writel(val, dmac->ext_base + offset);
  156. }
  157. static u32 rz_dmac_ext_readl(struct rz_dmac *dmac, unsigned int offset)
  158. {
  159. return readl(dmac->ext_base + offset);
  160. }
  161. static void rz_dmac_ch_writel(struct rz_dmac_chan *channel, unsigned int val,
  162. unsigned int offset, int which)
  163. {
  164. if (which)
  165. writel(val, channel->ch_base + offset);
  166. else
  167. writel(val, channel->ch_cmn_base + offset);
  168. }
  169. static u32 rz_dmac_ch_readl(struct rz_dmac_chan *channel,
  170. unsigned int offset, int which)
  171. {
  172. if (which)
  173. return readl(channel->ch_base + offset);
  174. else
  175. return readl(channel->ch_cmn_base + offset);
  176. }
  177. /*
  178. * -----------------------------------------------------------------------------
  179. * Initialization
  180. */
  181. static void rz_lmdesc_setup(struct rz_dmac_chan *channel,
  182. struct rz_lmdesc *lmdesc)
  183. {
  184. u32 nxla;
  185. channel->lmdesc.base = lmdesc;
  186. channel->lmdesc.head = lmdesc;
  187. channel->lmdesc.tail = lmdesc;
  188. nxla = channel->lmdesc.base_dma;
  189. while (lmdesc < (channel->lmdesc.base + (DMAC_NR_LMDESC - 1))) {
  190. lmdesc->header = 0;
  191. nxla += sizeof(*lmdesc);
  192. lmdesc->nxla = nxla;
  193. lmdesc++;
  194. }
  195. lmdesc->header = 0;
  196. lmdesc->nxla = channel->lmdesc.base_dma;
  197. }
  198. /*
  199. * -----------------------------------------------------------------------------
  200. * Descriptors preparation
  201. */
  202. static void rz_dmac_lmdesc_recycle(struct rz_dmac_chan *channel)
  203. {
  204. struct rz_lmdesc *lmdesc = channel->lmdesc.head;
  205. while (!(lmdesc->header & HEADER_LV)) {
  206. lmdesc->header = 0;
  207. lmdesc++;
  208. if (lmdesc >= (channel->lmdesc.base + DMAC_NR_LMDESC))
  209. lmdesc = channel->lmdesc.base;
  210. }
  211. channel->lmdesc.head = lmdesc;
  212. }
  213. static void rz_dmac_enable_hw(struct rz_dmac_chan *channel)
  214. {
  215. struct dma_chan *chan = &channel->vc.chan;
  216. struct rz_dmac *dmac = to_rz_dmac(chan->device);
  217. unsigned long flags;
  218. u32 nxla;
  219. u32 chctrl;
  220. u32 chstat;
  221. dev_dbg(dmac->dev, "%s channel %d\n", __func__, channel->index);
  222. local_irq_save(flags);
  223. rz_dmac_lmdesc_recycle(channel);
  224. nxla = channel->lmdesc.base_dma +
  225. (sizeof(struct rz_lmdesc) * (channel->lmdesc.head -
  226. channel->lmdesc.base));
  227. chstat = rz_dmac_ch_readl(channel, CHSTAT, 1);
  228. if (!(chstat & CHSTAT_EN)) {
  229. chctrl = (channel->chctrl | CHCTRL_SETEN);
  230. rz_dmac_ch_writel(channel, nxla, NXLA, 1);
  231. rz_dmac_ch_writel(channel, channel->chcfg, CHCFG, 1);
  232. rz_dmac_ch_writel(channel, CHCTRL_SWRST, CHCTRL, 1);
  233. rz_dmac_ch_writel(channel, chctrl, CHCTRL, 1);
  234. }
  235. local_irq_restore(flags);
  236. }
  237. static void rz_dmac_disable_hw(struct rz_dmac_chan *channel)
  238. {
  239. struct dma_chan *chan = &channel->vc.chan;
  240. struct rz_dmac *dmac = to_rz_dmac(chan->device);
  241. unsigned long flags;
  242. dev_dbg(dmac->dev, "%s channel %d\n", __func__, channel->index);
  243. local_irq_save(flags);
  244. rz_dmac_ch_writel(channel, CHCTRL_DEFAULT, CHCTRL, 1);
  245. local_irq_restore(flags);
  246. }
  247. static void rz_dmac_set_dmars_register(struct rz_dmac *dmac, int nr, u32 dmars)
  248. {
  249. u32 dmars_offset = (nr / 2) * 4;
  250. u32 shift = (nr % 2) * 16;
  251. u32 dmars32;
  252. dmars32 = rz_dmac_ext_readl(dmac, dmars_offset);
  253. dmars32 &= ~(0xffff << shift);
  254. dmars32 |= dmars << shift;
  255. rz_dmac_ext_writel(dmac, dmars32, dmars_offset);
  256. }
  257. static void rz_dmac_prepare_desc_for_memcpy(struct rz_dmac_chan *channel)
  258. {
  259. struct dma_chan *chan = &channel->vc.chan;
  260. struct rz_dmac *dmac = to_rz_dmac(chan->device);
  261. struct rz_lmdesc *lmdesc = channel->lmdesc.tail;
  262. struct rz_dmac_desc *d = channel->desc;
  263. u32 chcfg = CHCFG_MEM_COPY;
  264. /* prepare descriptor */
  265. lmdesc->sa = d->src;
  266. lmdesc->da = d->dest;
  267. lmdesc->tb = d->len;
  268. lmdesc->chcfg = chcfg;
  269. lmdesc->chitvl = 0;
  270. lmdesc->chext = 0;
  271. lmdesc->header = HEADER_LV;
  272. rz_dmac_set_dmars_register(dmac, channel->index, 0);
  273. channel->chcfg = chcfg;
  274. channel->chctrl = CHCTRL_STG | CHCTRL_SETEN;
  275. }
  276. static void rz_dmac_prepare_descs_for_slave_sg(struct rz_dmac_chan *channel)
  277. {
  278. struct dma_chan *chan = &channel->vc.chan;
  279. struct rz_dmac *dmac = to_rz_dmac(chan->device);
  280. struct rz_dmac_desc *d = channel->desc;
  281. struct scatterlist *sg, *sgl = d->sg;
  282. struct rz_lmdesc *lmdesc;
  283. unsigned int i, sg_len = d->sgcount;
  284. channel->chcfg |= CHCFG_SEL(channel->index) | CHCFG_DEM | CHCFG_DMS;
  285. if (d->direction == DMA_DEV_TO_MEM) {
  286. channel->chcfg |= CHCFG_SAD;
  287. channel->chcfg &= ~CHCFG_REQD;
  288. } else {
  289. channel->chcfg |= CHCFG_DAD | CHCFG_REQD;
  290. }
  291. lmdesc = channel->lmdesc.tail;
  292. for (i = 0, sg = sgl; i < sg_len; i++, sg = sg_next(sg)) {
  293. if (d->direction == DMA_DEV_TO_MEM) {
  294. lmdesc->sa = channel->src_per_address;
  295. lmdesc->da = sg_dma_address(sg);
  296. } else {
  297. lmdesc->sa = sg_dma_address(sg);
  298. lmdesc->da = channel->dst_per_address;
  299. }
  300. lmdesc->tb = sg_dma_len(sg);
  301. lmdesc->chitvl = 0;
  302. lmdesc->chext = 0;
  303. if (i == (sg_len - 1)) {
  304. lmdesc->chcfg = (channel->chcfg & ~CHCFG_DEM);
  305. lmdesc->header = HEADER_LV;
  306. } else {
  307. lmdesc->chcfg = channel->chcfg;
  308. lmdesc->header = HEADER_LV;
  309. }
  310. if (++lmdesc >= (channel->lmdesc.base + DMAC_NR_LMDESC))
  311. lmdesc = channel->lmdesc.base;
  312. }
  313. channel->lmdesc.tail = lmdesc;
  314. rz_dmac_set_dmars_register(dmac, channel->index, channel->mid_rid);
  315. channel->chctrl = CHCTRL_SETEN;
  316. }
  317. static int rz_dmac_xfer_desc(struct rz_dmac_chan *chan)
  318. {
  319. struct rz_dmac_desc *d = chan->desc;
  320. struct virt_dma_desc *vd;
  321. vd = vchan_next_desc(&chan->vc);
  322. if (!vd)
  323. return 0;
  324. list_del(&vd->node);
  325. switch (d->type) {
  326. case RZ_DMAC_DESC_MEMCPY:
  327. rz_dmac_prepare_desc_for_memcpy(chan);
  328. break;
  329. case RZ_DMAC_DESC_SLAVE_SG:
  330. rz_dmac_prepare_descs_for_slave_sg(chan);
  331. break;
  332. default:
  333. return -EINVAL;
  334. }
  335. rz_dmac_enable_hw(chan);
  336. return 0;
  337. }
  338. /*
  339. * -----------------------------------------------------------------------------
  340. * DMA engine operations
  341. */
  342. static int rz_dmac_alloc_chan_resources(struct dma_chan *chan)
  343. {
  344. struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
  345. while (channel->descs_allocated < RZ_DMAC_MAX_CHAN_DESCRIPTORS) {
  346. struct rz_dmac_desc *desc;
  347. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  348. if (!desc)
  349. break;
  350. list_add_tail(&desc->node, &channel->ld_free);
  351. channel->descs_allocated++;
  352. }
  353. if (!channel->descs_allocated)
  354. return -ENOMEM;
  355. return channel->descs_allocated;
  356. }
  357. static void rz_dmac_free_chan_resources(struct dma_chan *chan)
  358. {
  359. struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
  360. struct rz_dmac *dmac = to_rz_dmac(chan->device);
  361. struct rz_lmdesc *lmdesc = channel->lmdesc.base;
  362. struct rz_dmac_desc *desc, *_desc;
  363. unsigned long flags;
  364. unsigned int i;
  365. spin_lock_irqsave(&channel->vc.lock, flags);
  366. for (i = 0; i < DMAC_NR_LMDESC; i++)
  367. lmdesc[i].header = 0;
  368. rz_dmac_disable_hw(channel);
  369. list_splice_tail_init(&channel->ld_active, &channel->ld_free);
  370. list_splice_tail_init(&channel->ld_queue, &channel->ld_free);
  371. if (channel->mid_rid >= 0) {
  372. clear_bit(channel->mid_rid, dmac->modules);
  373. channel->mid_rid = -EINVAL;
  374. }
  375. spin_unlock_irqrestore(&channel->vc.lock, flags);
  376. list_for_each_entry_safe(desc, _desc, &channel->ld_free, node) {
  377. kfree(desc);
  378. channel->descs_allocated--;
  379. }
  380. INIT_LIST_HEAD(&channel->ld_free);
  381. vchan_free_chan_resources(&channel->vc);
  382. }
  383. static struct dma_async_tx_descriptor *
  384. rz_dmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  385. size_t len, unsigned long flags)
  386. {
  387. struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
  388. struct rz_dmac *dmac = to_rz_dmac(chan->device);
  389. struct rz_dmac_desc *desc;
  390. dev_dbg(dmac->dev, "%s channel: %d src=0x%pad dst=0x%pad len=%zu\n",
  391. __func__, channel->index, &src, &dest, len);
  392. if (list_empty(&channel->ld_free))
  393. return NULL;
  394. desc = list_first_entry(&channel->ld_free, struct rz_dmac_desc, node);
  395. desc->type = RZ_DMAC_DESC_MEMCPY;
  396. desc->src = src;
  397. desc->dest = dest;
  398. desc->len = len;
  399. desc->direction = DMA_MEM_TO_MEM;
  400. list_move_tail(channel->ld_free.next, &channel->ld_queue);
  401. return vchan_tx_prep(&channel->vc, &desc->vd, flags);
  402. }
  403. static struct dma_async_tx_descriptor *
  404. rz_dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  405. unsigned int sg_len,
  406. enum dma_transfer_direction direction,
  407. unsigned long flags, void *context)
  408. {
  409. struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
  410. struct rz_dmac_desc *desc;
  411. struct scatterlist *sg;
  412. int dma_length = 0;
  413. int i = 0;
  414. if (list_empty(&channel->ld_free))
  415. return NULL;
  416. desc = list_first_entry(&channel->ld_free, struct rz_dmac_desc, node);
  417. for_each_sg(sgl, sg, sg_len, i) {
  418. dma_length += sg_dma_len(sg);
  419. }
  420. desc->type = RZ_DMAC_DESC_SLAVE_SG;
  421. desc->sg = sgl;
  422. desc->sgcount = sg_len;
  423. desc->len = dma_length;
  424. desc->direction = direction;
  425. if (direction == DMA_DEV_TO_MEM)
  426. desc->src = channel->src_per_address;
  427. else
  428. desc->dest = channel->dst_per_address;
  429. list_move_tail(channel->ld_free.next, &channel->ld_queue);
  430. return vchan_tx_prep(&channel->vc, &desc->vd, flags);
  431. }
  432. static int rz_dmac_terminate_all(struct dma_chan *chan)
  433. {
  434. struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
  435. unsigned long flags;
  436. LIST_HEAD(head);
  437. rz_dmac_disable_hw(channel);
  438. spin_lock_irqsave(&channel->vc.lock, flags);
  439. list_splice_tail_init(&channel->ld_active, &channel->ld_free);
  440. list_splice_tail_init(&channel->ld_queue, &channel->ld_free);
  441. spin_unlock_irqrestore(&channel->vc.lock, flags);
  442. vchan_get_all_descriptors(&channel->vc, &head);
  443. vchan_dma_desc_free_list(&channel->vc, &head);
  444. return 0;
  445. }
  446. static void rz_dmac_issue_pending(struct dma_chan *chan)
  447. {
  448. struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
  449. struct rz_dmac *dmac = to_rz_dmac(chan->device);
  450. struct rz_dmac_desc *desc;
  451. unsigned long flags;
  452. spin_lock_irqsave(&channel->vc.lock, flags);
  453. if (!list_empty(&channel->ld_queue)) {
  454. desc = list_first_entry(&channel->ld_queue,
  455. struct rz_dmac_desc, node);
  456. channel->desc = desc;
  457. if (vchan_issue_pending(&channel->vc)) {
  458. if (rz_dmac_xfer_desc(channel) < 0)
  459. dev_warn(dmac->dev, "ch: %d couldn't issue DMA xfer\n",
  460. channel->index);
  461. else
  462. list_move_tail(channel->ld_queue.next,
  463. &channel->ld_active);
  464. }
  465. }
  466. spin_unlock_irqrestore(&channel->vc.lock, flags);
  467. }
  468. static u8 rz_dmac_ds_to_val_mapping(enum dma_slave_buswidth ds)
  469. {
  470. u8 i;
  471. static const enum dma_slave_buswidth ds_lut[] = {
  472. DMA_SLAVE_BUSWIDTH_1_BYTE,
  473. DMA_SLAVE_BUSWIDTH_2_BYTES,
  474. DMA_SLAVE_BUSWIDTH_4_BYTES,
  475. DMA_SLAVE_BUSWIDTH_8_BYTES,
  476. DMA_SLAVE_BUSWIDTH_16_BYTES,
  477. DMA_SLAVE_BUSWIDTH_32_BYTES,
  478. DMA_SLAVE_BUSWIDTH_64_BYTES,
  479. DMA_SLAVE_BUSWIDTH_128_BYTES,
  480. };
  481. for (i = 0; i < ARRAY_SIZE(ds_lut); i++) {
  482. if (ds_lut[i] == ds)
  483. return i;
  484. }
  485. return CHCFG_DS_INVALID;
  486. }
  487. static int rz_dmac_config(struct dma_chan *chan,
  488. struct dma_slave_config *config)
  489. {
  490. struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
  491. u32 val;
  492. channel->src_per_address = config->src_addr;
  493. channel->src_word_size = config->src_addr_width;
  494. channel->dst_per_address = config->dst_addr;
  495. channel->dst_word_size = config->dst_addr_width;
  496. val = rz_dmac_ds_to_val_mapping(config->dst_addr_width);
  497. if (val == CHCFG_DS_INVALID)
  498. return -EINVAL;
  499. channel->chcfg &= ~CHCFG_FILL_DDS_MASK;
  500. channel->chcfg |= FIELD_PREP(CHCFG_FILL_DDS_MASK, val);
  501. val = rz_dmac_ds_to_val_mapping(config->src_addr_width);
  502. if (val == CHCFG_DS_INVALID)
  503. return -EINVAL;
  504. channel->chcfg &= ~CHCFG_FILL_SDS_MASK;
  505. channel->chcfg |= FIELD_PREP(CHCFG_FILL_SDS_MASK, val);
  506. return 0;
  507. }
  508. static void rz_dmac_virt_desc_free(struct virt_dma_desc *vd)
  509. {
  510. /*
  511. * Place holder
  512. * Descriptor allocation is done during alloc_chan_resources and
  513. * get freed during free_chan_resources.
  514. * list is used to manage the descriptors and avoid any memory
  515. * allocation/free during DMA read/write.
  516. */
  517. }
  518. static void rz_dmac_device_synchronize(struct dma_chan *chan)
  519. {
  520. struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
  521. struct rz_dmac *dmac = to_rz_dmac(chan->device);
  522. u32 chstat;
  523. int ret;
  524. ret = read_poll_timeout(rz_dmac_ch_readl, chstat, !(chstat & CHSTAT_EN),
  525. 100, 100000, false, channel, CHSTAT, 1);
  526. if (ret < 0)
  527. dev_warn(dmac->dev, "DMA Timeout");
  528. rz_dmac_set_dmars_register(dmac, channel->index, 0);
  529. }
  530. /*
  531. * -----------------------------------------------------------------------------
  532. * IRQ handling
  533. */
  534. static void rz_dmac_irq_handle_channel(struct rz_dmac_chan *channel)
  535. {
  536. struct dma_chan *chan = &channel->vc.chan;
  537. struct rz_dmac *dmac = to_rz_dmac(chan->device);
  538. u32 chstat, chctrl;
  539. chstat = rz_dmac_ch_readl(channel, CHSTAT, 1);
  540. if (chstat & CHSTAT_ER) {
  541. dev_err(dmac->dev, "DMAC err CHSTAT_%d = %08X\n",
  542. channel->index, chstat);
  543. rz_dmac_ch_writel(channel, CHCTRL_DEFAULT, CHCTRL, 1);
  544. goto done;
  545. }
  546. chctrl = rz_dmac_ch_readl(channel, CHCTRL, 1);
  547. rz_dmac_ch_writel(channel, chctrl | CHCTRL_CLREND, CHCTRL, 1);
  548. done:
  549. return;
  550. }
  551. static irqreturn_t rz_dmac_irq_handler(int irq, void *dev_id)
  552. {
  553. struct rz_dmac_chan *channel = dev_id;
  554. if (channel) {
  555. rz_dmac_irq_handle_channel(channel);
  556. return IRQ_WAKE_THREAD;
  557. }
  558. /* handle DMAERR irq */
  559. return IRQ_HANDLED;
  560. }
  561. static irqreturn_t rz_dmac_irq_handler_thread(int irq, void *dev_id)
  562. {
  563. struct rz_dmac_chan *channel = dev_id;
  564. struct rz_dmac_desc *desc = NULL;
  565. unsigned long flags;
  566. spin_lock_irqsave(&channel->vc.lock, flags);
  567. if (list_empty(&channel->ld_active)) {
  568. /* Someone might have called terminate all */
  569. goto out;
  570. }
  571. desc = list_first_entry(&channel->ld_active, struct rz_dmac_desc, node);
  572. vchan_cookie_complete(&desc->vd);
  573. list_move_tail(channel->ld_active.next, &channel->ld_free);
  574. if (!list_empty(&channel->ld_queue)) {
  575. desc = list_first_entry(&channel->ld_queue, struct rz_dmac_desc,
  576. node);
  577. channel->desc = desc;
  578. if (rz_dmac_xfer_desc(channel) == 0)
  579. list_move_tail(channel->ld_queue.next, &channel->ld_active);
  580. }
  581. out:
  582. spin_unlock_irqrestore(&channel->vc.lock, flags);
  583. return IRQ_HANDLED;
  584. }
  585. /*
  586. * -----------------------------------------------------------------------------
  587. * OF xlate and channel filter
  588. */
  589. static bool rz_dmac_chan_filter(struct dma_chan *chan, void *arg)
  590. {
  591. struct rz_dmac_chan *channel = to_rz_dmac_chan(chan);
  592. struct rz_dmac *dmac = to_rz_dmac(chan->device);
  593. struct of_phandle_args *dma_spec = arg;
  594. u32 ch_cfg;
  595. channel->mid_rid = dma_spec->args[0] & MID_RID_MASK;
  596. ch_cfg = (dma_spec->args[0] & CHCFG_MASK) >> 10;
  597. channel->chcfg = CHCFG_FILL_TM(ch_cfg) | CHCFG_FILL_AM(ch_cfg) |
  598. CHCFG_FILL_LVL(ch_cfg) | CHCFG_FILL_HIEN(ch_cfg);
  599. return !test_and_set_bit(channel->mid_rid, dmac->modules);
  600. }
  601. static struct dma_chan *rz_dmac_of_xlate(struct of_phandle_args *dma_spec,
  602. struct of_dma *ofdma)
  603. {
  604. dma_cap_mask_t mask;
  605. if (dma_spec->args_count != 1)
  606. return NULL;
  607. /* Only slave DMA channels can be allocated via DT */
  608. dma_cap_zero(mask);
  609. dma_cap_set(DMA_SLAVE, mask);
  610. return dma_request_channel(mask, rz_dmac_chan_filter, dma_spec);
  611. }
  612. /*
  613. * -----------------------------------------------------------------------------
  614. * Probe and remove
  615. */
  616. static int rz_dmac_chan_probe(struct rz_dmac *dmac,
  617. struct rz_dmac_chan *channel,
  618. unsigned int index)
  619. {
  620. struct platform_device *pdev = to_platform_device(dmac->dev);
  621. struct rz_lmdesc *lmdesc;
  622. char pdev_irqname[5];
  623. char *irqname;
  624. int ret;
  625. channel->index = index;
  626. channel->mid_rid = -EINVAL;
  627. /* Request the channel interrupt. */
  628. sprintf(pdev_irqname, "ch%u", index);
  629. channel->irq = platform_get_irq_byname(pdev, pdev_irqname);
  630. if (channel->irq < 0)
  631. return channel->irq;
  632. irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:%u",
  633. dev_name(dmac->dev), index);
  634. if (!irqname)
  635. return -ENOMEM;
  636. ret = devm_request_threaded_irq(dmac->dev, channel->irq,
  637. rz_dmac_irq_handler,
  638. rz_dmac_irq_handler_thread, 0,
  639. irqname, channel);
  640. if (ret) {
  641. dev_err(dmac->dev, "failed to request IRQ %u (%d)\n",
  642. channel->irq, ret);
  643. return ret;
  644. }
  645. /* Set io base address for each channel */
  646. if (index < 8) {
  647. channel->ch_base = dmac->base + CHANNEL_0_7_OFFSET +
  648. EACH_CHANNEL_OFFSET * index;
  649. channel->ch_cmn_base = dmac->base + CHANNEL_0_7_COMMON_BASE;
  650. } else {
  651. channel->ch_base = dmac->base + CHANNEL_8_15_OFFSET +
  652. EACH_CHANNEL_OFFSET * (index - 8);
  653. channel->ch_cmn_base = dmac->base + CHANNEL_8_15_COMMON_BASE;
  654. }
  655. /* Allocate descriptors */
  656. lmdesc = dma_alloc_coherent(&pdev->dev,
  657. sizeof(struct rz_lmdesc) * DMAC_NR_LMDESC,
  658. &channel->lmdesc.base_dma, GFP_KERNEL);
  659. if (!lmdesc) {
  660. dev_err(&pdev->dev, "Can't allocate memory (lmdesc)\n");
  661. return -ENOMEM;
  662. }
  663. rz_lmdesc_setup(channel, lmdesc);
  664. /* Initialize register for each channel */
  665. rz_dmac_ch_writel(channel, CHCTRL_DEFAULT, CHCTRL, 1);
  666. channel->vc.desc_free = rz_dmac_virt_desc_free;
  667. vchan_init(&channel->vc, &dmac->engine);
  668. INIT_LIST_HEAD(&channel->ld_queue);
  669. INIT_LIST_HEAD(&channel->ld_free);
  670. INIT_LIST_HEAD(&channel->ld_active);
  671. return 0;
  672. }
  673. static int rz_dmac_parse_of(struct device *dev, struct rz_dmac *dmac)
  674. {
  675. struct device_node *np = dev->of_node;
  676. int ret;
  677. ret = of_property_read_u32(np, "dma-channels", &dmac->n_channels);
  678. if (ret < 0) {
  679. dev_err(dev, "unable to read dma-channels property\n");
  680. return ret;
  681. }
  682. if (!dmac->n_channels || dmac->n_channels > RZ_DMAC_MAX_CHANNELS) {
  683. dev_err(dev, "invalid number of channels %u\n", dmac->n_channels);
  684. return -EINVAL;
  685. }
  686. return 0;
  687. }
  688. static int rz_dmac_probe(struct platform_device *pdev)
  689. {
  690. const char *irqname = "error";
  691. struct dma_device *engine;
  692. struct rz_dmac *dmac;
  693. int channel_num;
  694. unsigned int i;
  695. int ret;
  696. int irq;
  697. dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
  698. if (!dmac)
  699. return -ENOMEM;
  700. dmac->dev = &pdev->dev;
  701. platform_set_drvdata(pdev, dmac);
  702. ret = rz_dmac_parse_of(&pdev->dev, dmac);
  703. if (ret < 0)
  704. return ret;
  705. dmac->channels = devm_kcalloc(&pdev->dev, dmac->n_channels,
  706. sizeof(*dmac->channels), GFP_KERNEL);
  707. if (!dmac->channels)
  708. return -ENOMEM;
  709. /* Request resources */
  710. dmac->base = devm_platform_ioremap_resource(pdev, 0);
  711. if (IS_ERR(dmac->base))
  712. return PTR_ERR(dmac->base);
  713. dmac->ext_base = devm_platform_ioremap_resource(pdev, 1);
  714. if (IS_ERR(dmac->ext_base))
  715. return PTR_ERR(dmac->ext_base);
  716. /* Register interrupt handler for error */
  717. irq = platform_get_irq_byname(pdev, irqname);
  718. if (irq < 0)
  719. return irq;
  720. ret = devm_request_irq(&pdev->dev, irq, rz_dmac_irq_handler, 0,
  721. irqname, NULL);
  722. if (ret) {
  723. dev_err(&pdev->dev, "failed to request IRQ %u (%d)\n",
  724. irq, ret);
  725. return ret;
  726. }
  727. /* Initialize the channels. */
  728. INIT_LIST_HEAD(&dmac->engine.channels);
  729. pm_runtime_enable(&pdev->dev);
  730. ret = pm_runtime_resume_and_get(&pdev->dev);
  731. if (ret < 0) {
  732. dev_err(&pdev->dev, "pm_runtime_resume_and_get failed\n");
  733. goto err_pm_disable;
  734. }
  735. for (i = 0; i < dmac->n_channels; i++) {
  736. ret = rz_dmac_chan_probe(dmac, &dmac->channels[i], i);
  737. if (ret < 0)
  738. goto err;
  739. }
  740. /* Register the DMAC as a DMA provider for DT. */
  741. ret = of_dma_controller_register(pdev->dev.of_node, rz_dmac_of_xlate,
  742. NULL);
  743. if (ret < 0)
  744. goto err;
  745. /* Register the DMA engine device. */
  746. engine = &dmac->engine;
  747. dma_cap_set(DMA_SLAVE, engine->cap_mask);
  748. dma_cap_set(DMA_MEMCPY, engine->cap_mask);
  749. rz_dmac_writel(dmac, DCTRL_DEFAULT, CHANNEL_0_7_COMMON_BASE + DCTRL);
  750. rz_dmac_writel(dmac, DCTRL_DEFAULT, CHANNEL_8_15_COMMON_BASE + DCTRL);
  751. engine->dev = &pdev->dev;
  752. engine->device_alloc_chan_resources = rz_dmac_alloc_chan_resources;
  753. engine->device_free_chan_resources = rz_dmac_free_chan_resources;
  754. engine->device_tx_status = dma_cookie_status;
  755. engine->device_prep_slave_sg = rz_dmac_prep_slave_sg;
  756. engine->device_prep_dma_memcpy = rz_dmac_prep_dma_memcpy;
  757. engine->device_config = rz_dmac_config;
  758. engine->device_terminate_all = rz_dmac_terminate_all;
  759. engine->device_issue_pending = rz_dmac_issue_pending;
  760. engine->device_synchronize = rz_dmac_device_synchronize;
  761. engine->copy_align = DMAENGINE_ALIGN_1_BYTE;
  762. dma_set_max_seg_size(engine->dev, U32_MAX);
  763. ret = dma_async_device_register(engine);
  764. if (ret < 0) {
  765. dev_err(&pdev->dev, "unable to register\n");
  766. goto dma_register_err;
  767. }
  768. return 0;
  769. dma_register_err:
  770. of_dma_controller_free(pdev->dev.of_node);
  771. err:
  772. channel_num = i ? i - 1 : 0;
  773. for (i = 0; i < channel_num; i++) {
  774. struct rz_dmac_chan *channel = &dmac->channels[i];
  775. dma_free_coherent(&pdev->dev,
  776. sizeof(struct rz_lmdesc) * DMAC_NR_LMDESC,
  777. channel->lmdesc.base,
  778. channel->lmdesc.base_dma);
  779. }
  780. pm_runtime_put(&pdev->dev);
  781. err_pm_disable:
  782. pm_runtime_disable(&pdev->dev);
  783. return ret;
  784. }
  785. static int rz_dmac_remove(struct platform_device *pdev)
  786. {
  787. struct rz_dmac *dmac = platform_get_drvdata(pdev);
  788. unsigned int i;
  789. for (i = 0; i < dmac->n_channels; i++) {
  790. struct rz_dmac_chan *channel = &dmac->channels[i];
  791. dma_free_coherent(&pdev->dev,
  792. sizeof(struct rz_lmdesc) * DMAC_NR_LMDESC,
  793. channel->lmdesc.base,
  794. channel->lmdesc.base_dma);
  795. }
  796. of_dma_controller_free(pdev->dev.of_node);
  797. dma_async_device_unregister(&dmac->engine);
  798. pm_runtime_put(&pdev->dev);
  799. pm_runtime_disable(&pdev->dev);
  800. return 0;
  801. }
  802. static const struct of_device_id of_rz_dmac_match[] = {
  803. { .compatible = "renesas,rz-dmac", },
  804. { /* Sentinel */ }
  805. };
  806. MODULE_DEVICE_TABLE(of, of_rz_dmac_match);
  807. static struct platform_driver rz_dmac_driver = {
  808. .driver = {
  809. .name = "rz-dmac",
  810. .of_match_table = of_rz_dmac_match,
  811. },
  812. .probe = rz_dmac_probe,
  813. .remove = rz_dmac_remove,
  814. };
  815. module_platform_driver(rz_dmac_driver);
  816. MODULE_DESCRIPTION("Renesas RZ/G2L DMA Controller Driver");
  817. MODULE_AUTHOR("Biju Das <[email protected]>");
  818. MODULE_LICENSE("GPL v2");