Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (C) 2013-2014 Allwinner Tech Co., Ltd
   4 * Author: Sugar <shuge@allwinnertech.com>
   5 *
   6 * Copyright (C) 2014 Maxime Ripard
   7 * Maxime Ripard <maxime.ripard@free-electrons.com>
 
 
 
 
 
   8 */
   9
  10#include <linux/clk.h>
  11#include <linux/delay.h>
  12#include <linux/dma-mapping.h>
  13#include <linux/dmaengine.h>
  14#include <linux/dmapool.h>
  15#include <linux/interrupt.h>
  16#include <linux/module.h>
  17#include <linux/of.h>
  18#include <linux/of_dma.h>
 
  19#include <linux/platform_device.h>
  20#include <linux/reset.h>
  21#include <linux/slab.h>
  22#include <linux/types.h>
  23
  24#include "virt-dma.h"
  25
  26/*
  27 * Common registers
  28 */
  29#define DMA_IRQ_EN(x)		((x) * 0x04)
  30#define DMA_IRQ_HALF			BIT(0)
  31#define DMA_IRQ_PKG			BIT(1)
  32#define DMA_IRQ_QUEUE			BIT(2)
  33
  34#define DMA_IRQ_CHAN_NR			8
  35#define DMA_IRQ_CHAN_WIDTH		4
  36
  37
  38#define DMA_IRQ_STAT(x)		((x) * 0x04 + 0x10)
  39
  40#define DMA_STAT		0x30
  41
  42/* Offset between DMA_IRQ_EN and DMA_IRQ_STAT limits number of channels */
  43#define DMA_MAX_CHANNELS	(DMA_IRQ_CHAN_NR * 0x10 / 4)
  44
  45/*
  46 * sun8i specific registers
  47 */
  48#define SUN8I_DMA_GATE		0x20
  49#define SUN8I_DMA_GATE_ENABLE	0x4
  50
  51#define SUNXI_H3_SECURE_REG		0x20
  52#define SUNXI_H3_DMA_GATE		0x28
  53#define SUNXI_H3_DMA_GATE_ENABLE	0x4
  54/*
  55 * Channels specific registers
  56 */
  57#define DMA_CHAN_ENABLE		0x00
  58#define DMA_CHAN_ENABLE_START		BIT(0)
  59#define DMA_CHAN_ENABLE_STOP		0
  60
  61#define DMA_CHAN_PAUSE		0x04
  62#define DMA_CHAN_PAUSE_PAUSE		BIT(1)
  63#define DMA_CHAN_PAUSE_RESUME		0
  64
  65#define DMA_CHAN_LLI_ADDR	0x08
  66
  67#define DMA_CHAN_CUR_CFG	0x0c
  68#define DMA_CHAN_MAX_DRQ_A31		0x1f
  69#define DMA_CHAN_MAX_DRQ_H6		0x3f
  70#define DMA_CHAN_CFG_SRC_DRQ_A31(x)	((x) & DMA_CHAN_MAX_DRQ_A31)
  71#define DMA_CHAN_CFG_SRC_DRQ_H6(x)	((x) & DMA_CHAN_MAX_DRQ_H6)
  72#define DMA_CHAN_CFG_SRC_MODE_A31(x)	(((x) & 0x1) << 5)
  73#define DMA_CHAN_CFG_SRC_MODE_H6(x)	(((x) & 0x1) << 8)
  74#define DMA_CHAN_CFG_SRC_BURST_A31(x)	(((x) & 0x3) << 7)
  75#define DMA_CHAN_CFG_SRC_BURST_H3(x)	(((x) & 0x3) << 6)
  76#define DMA_CHAN_CFG_SRC_WIDTH(x)	(((x) & 0x3) << 9)
  77
  78#define DMA_CHAN_CFG_DST_DRQ_A31(x)	(DMA_CHAN_CFG_SRC_DRQ_A31(x) << 16)
  79#define DMA_CHAN_CFG_DST_DRQ_H6(x)	(DMA_CHAN_CFG_SRC_DRQ_H6(x) << 16)
  80#define DMA_CHAN_CFG_DST_MODE_A31(x)	(DMA_CHAN_CFG_SRC_MODE_A31(x) << 16)
  81#define DMA_CHAN_CFG_DST_MODE_H6(x)	(DMA_CHAN_CFG_SRC_MODE_H6(x) << 16)
  82#define DMA_CHAN_CFG_DST_BURST_A31(x)	(DMA_CHAN_CFG_SRC_BURST_A31(x) << 16)
  83#define DMA_CHAN_CFG_DST_BURST_H3(x)	(DMA_CHAN_CFG_SRC_BURST_H3(x) << 16)
  84#define DMA_CHAN_CFG_DST_WIDTH(x)	(DMA_CHAN_CFG_SRC_WIDTH(x) << 16)
  85
  86#define DMA_CHAN_CUR_SRC	0x10
  87
  88#define DMA_CHAN_CUR_DST	0x14
  89
  90#define DMA_CHAN_CUR_CNT	0x18
  91
  92#define DMA_CHAN_CUR_PARA	0x1c
  93
  94/*
  95 * LLI address mangling
  96 *
  97 * The LLI link physical address is also mangled, but we avoid dealing
  98 * with that by allocating LLIs from the DMA32 zone.
  99 */
 100#define SRC_HIGH_ADDR(x)		(((x) & 0x3U) << 16)
 101#define DST_HIGH_ADDR(x)		(((x) & 0x3U) << 18)
 102
 103/*
 104 * Various hardware related defines
 105 */
 106#define LLI_LAST_ITEM	0xfffff800
 107#define NORMAL_WAIT	8
 108#define DRQ_SDRAM	1
 109#define LINEAR_MODE     0
 110#define IO_MODE         1
 111
 112/* forward declaration */
 113struct sun6i_dma_dev;
 114
 115/*
 116 * Hardware channels / ports representation
 117 *
 118 * The hardware is used in several SoCs, with differing numbers
 119 * of channels and endpoints. This structure ties those numbers
 120 * to a certain compatible string.
 121 */
 122struct sun6i_dma_config {
 123	u32 nr_max_channels;
 124	u32 nr_max_requests;
 125	u32 nr_max_vchans;
 126	/*
 127	 * In the datasheets/user manuals of newer Allwinner SoCs, a special
 128	 * bit (bit 2 at register 0x20) is present.
 129	 * It's named "DMA MCLK interface circuit auto gating bit" in the
 130	 * documents, and the footnote of this register says that this bit
 131	 * should be set up when initializing the DMA controller.
 132	 * Allwinner A23/A33 user manuals do not have this bit documented,
 133	 * however these SoCs really have and need this bit, as seen in the
 134	 * BSP kernel source code.
 135	 */
 136	void (*clock_autogate_enable)(struct sun6i_dma_dev *);
 137	void (*set_burst_length)(u32 *p_cfg, s8 src_burst, s8 dst_burst);
 138	void (*set_drq)(u32 *p_cfg, s8 src_drq, s8 dst_drq);
 139	void (*set_mode)(u32 *p_cfg, s8 src_mode, s8 dst_mode);
 140	u32 src_burst_lengths;
 141	u32 dst_burst_lengths;
 142	u32 src_addr_widths;
 143	u32 dst_addr_widths;
 144	bool has_high_addr;
 145	bool has_mbus_clk;
 146};
 147
 148/*
 149 * Hardware representation of the LLI
 150 *
 151 * The hardware will be fed the physical address of this structure,
 152 * and read its content in order to start the transfer.
 153 */
 154struct sun6i_dma_lli {
 155	u32			cfg;
 156	u32			src;
 157	u32			dst;
 158	u32			len;
 159	u32			para;
 160	u32			p_lli_next;
 161
 162	/*
 163	 * This field is not used by the DMA controller, but will be
 164	 * used by the CPU to go through the list (mostly for dumping
 165	 * or freeing it).
 166	 */
 167	struct sun6i_dma_lli	*v_lli_next;
 168};
 169
 170
 171struct sun6i_desc {
 172	struct virt_dma_desc	vd;
 173	dma_addr_t		p_lli;
 174	struct sun6i_dma_lli	*v_lli;
 175};
 176
 177struct sun6i_pchan {
 178	u32			idx;
 179	void __iomem		*base;
 180	struct sun6i_vchan	*vchan;
 181	struct sun6i_desc	*desc;
 182	struct sun6i_desc	*done;
 183};
 184
 185struct sun6i_vchan {
 186	struct virt_dma_chan	vc;
 187	struct list_head	node;
 188	struct dma_slave_config	cfg;
 189	struct sun6i_pchan	*phy;
 190	u8			port;
 191	u8			irq_type;
 192	bool			cyclic;
 193};
 194
 195struct sun6i_dma_dev {
 196	struct dma_device	slave;
 197	void __iomem		*base;
 198	struct clk		*clk;
 199	struct clk		*clk_mbus;
 200	int			irq;
 201	spinlock_t		lock;
 202	struct reset_control	*rstc;
 203	struct tasklet_struct	task;
 204	atomic_t		tasklet_shutdown;
 205	struct list_head	pending;
 206	struct dma_pool		*pool;
 207	struct sun6i_pchan	*pchans;
 208	struct sun6i_vchan	*vchans;
 209	const struct sun6i_dma_config *cfg;
 210	u32			num_pchans;
 211	u32			num_vchans;
 212	u32			max_request;
 213};
 214
 215static struct device *chan2dev(struct dma_chan *chan)
 216{
 217	return &chan->dev->device;
 218}
 219
 220static inline struct sun6i_dma_dev *to_sun6i_dma_dev(struct dma_device *d)
 221{
 222	return container_of(d, struct sun6i_dma_dev, slave);
 223}
 224
 225static inline struct sun6i_vchan *to_sun6i_vchan(struct dma_chan *chan)
 226{
 227	return container_of(chan, struct sun6i_vchan, vc.chan);
 228}
 229
 230static inline struct sun6i_desc *
 231to_sun6i_desc(struct dma_async_tx_descriptor *tx)
 232{
 233	return container_of(tx, struct sun6i_desc, vd.tx);
 234}
 235
 236static inline void sun6i_dma_dump_com_regs(struct sun6i_dma_dev *sdev)
 237{
 238	dev_dbg(sdev->slave.dev, "Common register:\n"
 239		"\tmask0(%04x): 0x%08x\n"
 240		"\tmask1(%04x): 0x%08x\n"
 241		"\tpend0(%04x): 0x%08x\n"
 242		"\tpend1(%04x): 0x%08x\n"
 243		"\tstats(%04x): 0x%08x\n",
 244		DMA_IRQ_EN(0), readl(sdev->base + DMA_IRQ_EN(0)),
 245		DMA_IRQ_EN(1), readl(sdev->base + DMA_IRQ_EN(1)),
 246		DMA_IRQ_STAT(0), readl(sdev->base + DMA_IRQ_STAT(0)),
 247		DMA_IRQ_STAT(1), readl(sdev->base + DMA_IRQ_STAT(1)),
 248		DMA_STAT, readl(sdev->base + DMA_STAT));
 249}
 250
 251static inline void sun6i_dma_dump_chan_regs(struct sun6i_dma_dev *sdev,
 252					    struct sun6i_pchan *pchan)
 253{
 254	dev_dbg(sdev->slave.dev, "Chan %d reg:\n"
 
 
 255		"\t___en(%04x): \t0x%08x\n"
 256		"\tpause(%04x): \t0x%08x\n"
 257		"\tstart(%04x): \t0x%08x\n"
 258		"\t__cfg(%04x): \t0x%08x\n"
 259		"\t__src(%04x): \t0x%08x\n"
 260		"\t__dst(%04x): \t0x%08x\n"
 261		"\tcount(%04x): \t0x%08x\n"
 262		"\t_para(%04x): \t0x%08x\n\n",
 263		pchan->idx,
 264		DMA_CHAN_ENABLE,
 265		readl(pchan->base + DMA_CHAN_ENABLE),
 266		DMA_CHAN_PAUSE,
 267		readl(pchan->base + DMA_CHAN_PAUSE),
 268		DMA_CHAN_LLI_ADDR,
 269		readl(pchan->base + DMA_CHAN_LLI_ADDR),
 270		DMA_CHAN_CUR_CFG,
 271		readl(pchan->base + DMA_CHAN_CUR_CFG),
 272		DMA_CHAN_CUR_SRC,
 273		readl(pchan->base + DMA_CHAN_CUR_SRC),
 274		DMA_CHAN_CUR_DST,
 275		readl(pchan->base + DMA_CHAN_CUR_DST),
 276		DMA_CHAN_CUR_CNT,
 277		readl(pchan->base + DMA_CHAN_CUR_CNT),
 278		DMA_CHAN_CUR_PARA,
 279		readl(pchan->base + DMA_CHAN_CUR_PARA));
 280}
 281
 282static inline s8 convert_burst(u32 maxburst)
 283{
 284	switch (maxburst) {
 285	case 1:
 286		return 0;
 287	case 4:
 288		return 1;
 289	case 8:
 290		return 2;
 291	case 16:
 292		return 3;
 293	default:
 294		return -EINVAL;
 295	}
 296}
 297
 298static inline s8 convert_buswidth(enum dma_slave_buswidth addr_width)
 299{
 300	return ilog2(addr_width);
 301}
 302
 303static void sun6i_enable_clock_autogate_a23(struct sun6i_dma_dev *sdev)
 304{
 305	writel(SUN8I_DMA_GATE_ENABLE, sdev->base + SUN8I_DMA_GATE);
 306}
 307
 308static void sun6i_enable_clock_autogate_h3(struct sun6i_dma_dev *sdev)
 309{
 310	writel(SUNXI_H3_DMA_GATE_ENABLE, sdev->base + SUNXI_H3_DMA_GATE);
 311}
 312
 313static void sun6i_set_burst_length_a31(u32 *p_cfg, s8 src_burst, s8 dst_burst)
 314{
 315	*p_cfg |= DMA_CHAN_CFG_SRC_BURST_A31(src_burst) |
 316		  DMA_CHAN_CFG_DST_BURST_A31(dst_burst);
 317}
 318
 319static void sun6i_set_burst_length_h3(u32 *p_cfg, s8 src_burst, s8 dst_burst)
 320{
 321	*p_cfg |= DMA_CHAN_CFG_SRC_BURST_H3(src_burst) |
 322		  DMA_CHAN_CFG_DST_BURST_H3(dst_burst);
 323}
 324
 325static void sun6i_set_drq_a31(u32 *p_cfg, s8 src_drq, s8 dst_drq)
 326{
 327	*p_cfg |= DMA_CHAN_CFG_SRC_DRQ_A31(src_drq) |
 328		  DMA_CHAN_CFG_DST_DRQ_A31(dst_drq);
 329}
 330
 331static void sun6i_set_drq_h6(u32 *p_cfg, s8 src_drq, s8 dst_drq)
 332{
 333	*p_cfg |= DMA_CHAN_CFG_SRC_DRQ_H6(src_drq) |
 334		  DMA_CHAN_CFG_DST_DRQ_H6(dst_drq);
 335}
 336
 337static void sun6i_set_mode_a31(u32 *p_cfg, s8 src_mode, s8 dst_mode)
 338{
 339	*p_cfg |= DMA_CHAN_CFG_SRC_MODE_A31(src_mode) |
 340		  DMA_CHAN_CFG_DST_MODE_A31(dst_mode);
 341}
 342
 343static void sun6i_set_mode_h6(u32 *p_cfg, s8 src_mode, s8 dst_mode)
 344{
 345	*p_cfg |= DMA_CHAN_CFG_SRC_MODE_H6(src_mode) |
 346		  DMA_CHAN_CFG_DST_MODE_H6(dst_mode);
 347}
 348
 349static size_t sun6i_get_chan_size(struct sun6i_pchan *pchan)
 350{
 351	struct sun6i_desc *txd = pchan->desc;
 352	struct sun6i_dma_lli *lli;
 353	size_t bytes;
 354	dma_addr_t pos;
 355
 356	pos = readl(pchan->base + DMA_CHAN_LLI_ADDR);
 357	bytes = readl(pchan->base + DMA_CHAN_CUR_CNT);
 358
 359	if (pos == LLI_LAST_ITEM)
 360		return bytes;
 361
 362	for (lli = txd->v_lli; lli; lli = lli->v_lli_next) {
 363		if (lli->p_lli_next == pos) {
 364			for (lli = lli->v_lli_next; lli; lli = lli->v_lli_next)
 365				bytes += lli->len;
 366			break;
 367		}
 368	}
 369
 370	return bytes;
 371}
 372
 373static void *sun6i_dma_lli_add(struct sun6i_dma_lli *prev,
 374			       struct sun6i_dma_lli *next,
 375			       dma_addr_t next_phy,
 376			       struct sun6i_desc *txd)
 377{
 378	if ((!prev && !txd) || !next)
 379		return NULL;
 380
 381	if (!prev) {
 382		txd->p_lli = next_phy;
 383		txd->v_lli = next;
 384	} else {
 385		prev->p_lli_next = next_phy;
 386		prev->v_lli_next = next;
 387	}
 388
 389	next->p_lli_next = LLI_LAST_ITEM;
 390	next->v_lli_next = NULL;
 391
 392	return next;
 393}
 394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 395static inline void sun6i_dma_dump_lli(struct sun6i_vchan *vchan,
 396				      struct sun6i_dma_lli *v_lli,
 397				      dma_addr_t p_lli)
 398{
 
 
 399	dev_dbg(chan2dev(&vchan->vc.chan),
 400		"\n\tdesc:\tp - %pad v - 0x%p\n"
 401		"\t\tc - 0x%08x s - 0x%08x d - 0x%08x\n"
 402		"\t\tl - 0x%08x p - 0x%08x n - 0x%08x\n",
 403		&p_lli, v_lli,
 404		v_lli->cfg, v_lli->src, v_lli->dst,
 405		v_lli->len, v_lli->para, v_lli->p_lli_next);
 406}
 407
 408static void sun6i_dma_free_desc(struct virt_dma_desc *vd)
 409{
 410	struct sun6i_desc *txd = to_sun6i_desc(&vd->tx);
 411	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vd->tx.chan->device);
 412	struct sun6i_dma_lli *v_lli, *v_next;
 413	dma_addr_t p_lli, p_next;
 414
 415	if (unlikely(!txd))
 416		return;
 417
 418	p_lli = txd->p_lli;
 419	v_lli = txd->v_lli;
 420
 421	while (v_lli) {
 422		v_next = v_lli->v_lli_next;
 423		p_next = v_lli->p_lli_next;
 424
 425		dma_pool_free(sdev->pool, v_lli, p_lli);
 426
 427		v_lli = v_next;
 428		p_lli = p_next;
 429	}
 430
 431	kfree(txd);
 432}
 433
 434static int sun6i_dma_start_desc(struct sun6i_vchan *vchan)
 435{
 436	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vchan->vc.chan.device);
 437	struct virt_dma_desc *desc = vchan_next_desc(&vchan->vc);
 438	struct sun6i_pchan *pchan = vchan->phy;
 439	u32 irq_val, irq_reg, irq_offset;
 440
 441	if (!pchan)
 442		return -EAGAIN;
 443
 444	if (!desc) {
 445		pchan->desc = NULL;
 446		pchan->done = NULL;
 447		return -EAGAIN;
 448	}
 449
 450	list_del(&desc->node);
 451
 452	pchan->desc = to_sun6i_desc(&desc->tx);
 453	pchan->done = NULL;
 454
 455	sun6i_dma_dump_lli(vchan, pchan->desc->v_lli, pchan->desc->p_lli);
 456
 457	irq_reg = pchan->idx / DMA_IRQ_CHAN_NR;
 458	irq_offset = pchan->idx % DMA_IRQ_CHAN_NR;
 459
 460	vchan->irq_type = vchan->cyclic ? DMA_IRQ_PKG : DMA_IRQ_QUEUE;
 461
 462	irq_val = readl(sdev->base + DMA_IRQ_EN(irq_reg));
 463	irq_val &= ~((DMA_IRQ_HALF | DMA_IRQ_PKG | DMA_IRQ_QUEUE) <<
 464			(irq_offset * DMA_IRQ_CHAN_WIDTH));
 465	irq_val |= vchan->irq_type << (irq_offset * DMA_IRQ_CHAN_WIDTH);
 466	writel(irq_val, sdev->base + DMA_IRQ_EN(irq_reg));
 467
 468	writel(pchan->desc->p_lli, pchan->base + DMA_CHAN_LLI_ADDR);
 469	writel(DMA_CHAN_ENABLE_START, pchan->base + DMA_CHAN_ENABLE);
 470
 471	sun6i_dma_dump_com_regs(sdev);
 472	sun6i_dma_dump_chan_regs(sdev, pchan);
 473
 474	return 0;
 475}
 476
 477static void sun6i_dma_tasklet(struct tasklet_struct *t)
 478{
 479	struct sun6i_dma_dev *sdev = from_tasklet(sdev, t, task);
 
 480	struct sun6i_vchan *vchan;
 481	struct sun6i_pchan *pchan;
 482	unsigned int pchan_alloc = 0;
 483	unsigned int pchan_idx;
 484
 485	list_for_each_entry(vchan, &sdev->slave.channels, vc.chan.device_node) {
 486		spin_lock_irq(&vchan->vc.lock);
 487
 488		pchan = vchan->phy;
 489
 490		if (pchan && pchan->done) {
 491			if (sun6i_dma_start_desc(vchan)) {
 492				/*
 493				 * No current txd associated with this channel
 494				 */
 495				dev_dbg(sdev->slave.dev, "pchan %u: free\n",
 496					pchan->idx);
 497
 498				/* Mark this channel free */
 499				vchan->phy = NULL;
 500				pchan->vchan = NULL;
 501			}
 502		}
 503		spin_unlock_irq(&vchan->vc.lock);
 504	}
 505
 506	spin_lock_irq(&sdev->lock);
 507	for (pchan_idx = 0; pchan_idx < sdev->num_pchans; pchan_idx++) {
 508		pchan = &sdev->pchans[pchan_idx];
 509
 510		if (pchan->vchan || list_empty(&sdev->pending))
 511			continue;
 512
 513		vchan = list_first_entry(&sdev->pending,
 514					 struct sun6i_vchan, node);
 515
 516		/* Remove from pending channels */
 517		list_del_init(&vchan->node);
 518		pchan_alloc |= BIT(pchan_idx);
 519
 520		/* Mark this channel allocated */
 521		pchan->vchan = vchan;
 522		vchan->phy = pchan;
 523		dev_dbg(sdev->slave.dev, "pchan %u: alloc vchan %p\n",
 524			pchan->idx, &vchan->vc);
 525	}
 526	spin_unlock_irq(&sdev->lock);
 527
 528	for (pchan_idx = 0; pchan_idx < sdev->num_pchans; pchan_idx++) {
 529		if (!(pchan_alloc & BIT(pchan_idx)))
 530			continue;
 531
 532		pchan = sdev->pchans + pchan_idx;
 533		vchan = pchan->vchan;
 534		if (vchan) {
 535			spin_lock_irq(&vchan->vc.lock);
 536			sun6i_dma_start_desc(vchan);
 537			spin_unlock_irq(&vchan->vc.lock);
 538		}
 539	}
 540}
 541
 542static irqreturn_t sun6i_dma_interrupt(int irq, void *dev_id)
 543{
 544	struct sun6i_dma_dev *sdev = dev_id;
 545	struct sun6i_vchan *vchan;
 546	struct sun6i_pchan *pchan;
 547	int i, j, ret = IRQ_NONE;
 548	u32 status;
 549
 550	for (i = 0; i < sdev->num_pchans / DMA_IRQ_CHAN_NR; i++) {
 551		status = readl(sdev->base + DMA_IRQ_STAT(i));
 552		if (!status)
 553			continue;
 554
 555		dev_dbg(sdev->slave.dev, "DMA irq status %s: 0x%x\n",
 556			i ? "high" : "low", status);
 557
 558		writel(status, sdev->base + DMA_IRQ_STAT(i));
 559
 560		for (j = 0; (j < DMA_IRQ_CHAN_NR) && status; j++) {
 561			pchan = sdev->pchans + j;
 562			vchan = pchan->vchan;
 563			if (vchan && (status & vchan->irq_type)) {
 564				if (vchan->cyclic) {
 565					vchan_cyclic_callback(&pchan->desc->vd);
 566				} else {
 567					spin_lock(&vchan->vc.lock);
 568					vchan_cookie_complete(&pchan->desc->vd);
 569					pchan->done = pchan->desc;
 570					spin_unlock(&vchan->vc.lock);
 571				}
 572			}
 573
 574			status = status >> DMA_IRQ_CHAN_WIDTH;
 575		}
 576
 577		if (!atomic_read(&sdev->tasklet_shutdown))
 578			tasklet_schedule(&sdev->task);
 579		ret = IRQ_HANDLED;
 580	}
 581
 582	return ret;
 583}
 584
 585static int set_config(struct sun6i_dma_dev *sdev,
 586			struct dma_slave_config *sconfig,
 587			enum dma_transfer_direction direction,
 588			u32 *p_cfg)
 589{
 590	enum dma_slave_buswidth src_addr_width, dst_addr_width;
 591	u32 src_maxburst, dst_maxburst;
 592	s8 src_width, dst_width, src_burst, dst_burst;
 593
 594	src_addr_width = sconfig->src_addr_width;
 595	dst_addr_width = sconfig->dst_addr_width;
 596	src_maxburst = sconfig->src_maxburst;
 597	dst_maxburst = sconfig->dst_maxburst;
 598
 599	switch (direction) {
 600	case DMA_MEM_TO_DEV:
 601		if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
 602			src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
 603		src_maxburst = src_maxburst ? src_maxburst : 8;
 604		break;
 605	case DMA_DEV_TO_MEM:
 606		if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
 607			dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
 608		dst_maxburst = dst_maxburst ? dst_maxburst : 8;
 609		break;
 610	default:
 611		return -EINVAL;
 612	}
 613
 614	if (!(BIT(src_addr_width) & sdev->slave.src_addr_widths))
 615		return -EINVAL;
 616	if (!(BIT(dst_addr_width) & sdev->slave.dst_addr_widths))
 617		return -EINVAL;
 618	if (!(BIT(src_maxburst) & sdev->cfg->src_burst_lengths))
 619		return -EINVAL;
 620	if (!(BIT(dst_maxburst) & sdev->cfg->dst_burst_lengths))
 621		return -EINVAL;
 622
 623	src_width = convert_buswidth(src_addr_width);
 624	dst_width = convert_buswidth(dst_addr_width);
 625	dst_burst = convert_burst(dst_maxburst);
 626	src_burst = convert_burst(src_maxburst);
 627
 628	*p_cfg = DMA_CHAN_CFG_SRC_WIDTH(src_width) |
 629		DMA_CHAN_CFG_DST_WIDTH(dst_width);
 630
 631	sdev->cfg->set_burst_length(p_cfg, src_burst, dst_burst);
 632
 633	return 0;
 634}
 635
 636static inline void sun6i_dma_set_addr(struct sun6i_dma_dev *sdev,
 637				      struct sun6i_dma_lli *v_lli,
 638				      dma_addr_t src, dma_addr_t dst)
 639{
 640	v_lli->src = lower_32_bits(src);
 641	v_lli->dst = lower_32_bits(dst);
 642
 643	if (sdev->cfg->has_high_addr)
 644		v_lli->para |= SRC_HIGH_ADDR(upper_32_bits(src)) |
 645			       DST_HIGH_ADDR(upper_32_bits(dst));
 646}
 647
 648static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_memcpy(
 649		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
 650		size_t len, unsigned long flags)
 651{
 652	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 653	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 654	struct sun6i_dma_lli *v_lli;
 655	struct sun6i_desc *txd;
 656	dma_addr_t p_lli;
 657	s8 burst, width;
 658
 659	dev_dbg(chan2dev(chan),
 660		"%s; chan: %d, dest: %pad, src: %pad, len: %zu. flags: 0x%08lx\n",
 661		__func__, vchan->vc.chan.chan_id, &dest, &src, len, flags);
 662
 663	if (!len)
 664		return NULL;
 665
 666	txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
 667	if (!txd)
 668		return NULL;
 669
 670	v_lli = dma_pool_alloc(sdev->pool, GFP_DMA32 | GFP_NOWAIT, &p_lli);
 671	if (!v_lli) {
 672		dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
 673		goto err_txd_free;
 674	}
 675
 
 
 676	v_lli->len = len;
 677	v_lli->para = NORMAL_WAIT;
 678	sun6i_dma_set_addr(sdev, v_lli, src, dest);
 679
 680	burst = convert_burst(8);
 681	width = convert_buswidth(DMA_SLAVE_BUSWIDTH_4_BYTES);
 682	v_lli->cfg = DMA_CHAN_CFG_SRC_WIDTH(width) |
 
 
 
 
 
 
 683		DMA_CHAN_CFG_DST_WIDTH(width);
 684
 685	sdev->cfg->set_burst_length(&v_lli->cfg, burst, burst);
 686	sdev->cfg->set_drq(&v_lli->cfg, DRQ_SDRAM, DRQ_SDRAM);
 687	sdev->cfg->set_mode(&v_lli->cfg, LINEAR_MODE, LINEAR_MODE);
 688
 689	sun6i_dma_lli_add(NULL, v_lli, p_lli, txd);
 690
 691	sun6i_dma_dump_lli(vchan, v_lli, p_lli);
 692
 693	return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
 694
 695err_txd_free:
 696	kfree(txd);
 697	return NULL;
 698}
 699
 700static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg(
 701		struct dma_chan *chan, struct scatterlist *sgl,
 702		unsigned int sg_len, enum dma_transfer_direction dir,
 703		unsigned long flags, void *context)
 704{
 705	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 706	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 707	struct dma_slave_config *sconfig = &vchan->cfg;
 708	struct sun6i_dma_lli *v_lli, *prev = NULL;
 709	struct sun6i_desc *txd;
 710	struct scatterlist *sg;
 711	dma_addr_t p_lli;
 712	u32 lli_cfg;
 713	int i, ret;
 714
 715	if (!sgl)
 716		return NULL;
 717
 718	ret = set_config(sdev, sconfig, dir, &lli_cfg);
 719	if (ret) {
 720		dev_err(chan2dev(chan), "Invalid DMA configuration\n");
 721		return NULL;
 722	}
 723
 724	txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
 725	if (!txd)
 726		return NULL;
 727
 728	for_each_sg(sgl, sg, sg_len, i) {
 729		v_lli = dma_pool_alloc(sdev->pool, GFP_DMA32 | GFP_NOWAIT, &p_lli);
 730		if (!v_lli)
 731			goto err_lli_free;
 732
 733		v_lli->len = sg_dma_len(sg);
 734		v_lli->para = NORMAL_WAIT;
 735
 736		if (dir == DMA_MEM_TO_DEV) {
 737			sun6i_dma_set_addr(sdev, v_lli,
 738					   sg_dma_address(sg),
 739					   sconfig->dst_addr);
 740			v_lli->cfg = lli_cfg;
 741			sdev->cfg->set_drq(&v_lli->cfg, DRQ_SDRAM, vchan->port);
 742			sdev->cfg->set_mode(&v_lli->cfg, LINEAR_MODE, IO_MODE);
 
 
 
 
 743
 744			dev_dbg(chan2dev(chan),
 745				"%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
 746				__func__, vchan->vc.chan.chan_id,
 747				&sconfig->dst_addr, &sg_dma_address(sg),
 748				sg_dma_len(sg), flags);
 749
 750		} else {
 751			sun6i_dma_set_addr(sdev, v_lli,
 752					   sconfig->src_addr,
 753					   sg_dma_address(sg));
 754			v_lli->cfg = lli_cfg;
 755			sdev->cfg->set_drq(&v_lli->cfg, vchan->port, DRQ_SDRAM);
 756			sdev->cfg->set_mode(&v_lli->cfg, IO_MODE, LINEAR_MODE);
 
 
 
 
 757
 758			dev_dbg(chan2dev(chan),
 759				"%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
 760				__func__, vchan->vc.chan.chan_id,
 761				&sg_dma_address(sg), &sconfig->src_addr,
 762				sg_dma_len(sg), flags);
 763		}
 764
 765		prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
 766	}
 767
 768	dev_dbg(chan2dev(chan), "First: %pad\n", &txd->p_lli);
 769	for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli;
 770	     p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next)
 771		sun6i_dma_dump_lli(vchan, v_lli, p_lli);
 772
 773	return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
 774
 775err_lli_free:
 776	for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli;
 777	     p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next)
 778		dma_pool_free(sdev->pool, v_lli, p_lli);
 779	kfree(txd);
 780	return NULL;
 781}
 782
 783static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_cyclic(
 784					struct dma_chan *chan,
 785					dma_addr_t buf_addr,
 786					size_t buf_len,
 787					size_t period_len,
 788					enum dma_transfer_direction dir,
 789					unsigned long flags)
 790{
 791	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 792	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 793	struct dma_slave_config *sconfig = &vchan->cfg;
 794	struct sun6i_dma_lli *v_lli, *prev = NULL;
 795	struct sun6i_desc *txd;
 796	dma_addr_t p_lli;
 797	u32 lli_cfg;
 798	unsigned int i, periods = buf_len / period_len;
 799	int ret;
 800
 801	ret = set_config(sdev, sconfig, dir, &lli_cfg);
 802	if (ret) {
 803		dev_err(chan2dev(chan), "Invalid DMA configuration\n");
 804		return NULL;
 805	}
 806
 807	txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
 808	if (!txd)
 809		return NULL;
 810
 811	for (i = 0; i < periods; i++) {
 812		v_lli = dma_pool_alloc(sdev->pool, GFP_DMA32 | GFP_NOWAIT, &p_lli);
 813		if (!v_lli) {
 814			dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
 815			goto err_lli_free;
 816		}
 817
 818		v_lli->len = period_len;
 819		v_lli->para = NORMAL_WAIT;
 820
 821		if (dir == DMA_MEM_TO_DEV) {
 822			sun6i_dma_set_addr(sdev, v_lli,
 823					   buf_addr + period_len * i,
 824					   sconfig->dst_addr);
 825			v_lli->cfg = lli_cfg;
 826			sdev->cfg->set_drq(&v_lli->cfg, DRQ_SDRAM, vchan->port);
 827			sdev->cfg->set_mode(&v_lli->cfg, LINEAR_MODE, IO_MODE);
 828		} else {
 829			sun6i_dma_set_addr(sdev, v_lli,
 830					   sconfig->src_addr,
 831					   buf_addr + period_len * i);
 832			v_lli->cfg = lli_cfg;
 833			sdev->cfg->set_drq(&v_lli->cfg, vchan->port, DRQ_SDRAM);
 834			sdev->cfg->set_mode(&v_lli->cfg, IO_MODE, LINEAR_MODE);
 835		}
 836
 837		prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
 838	}
 839
 840	prev->p_lli_next = txd->p_lli;		/* cyclic list */
 841
 842	vchan->cyclic = true;
 843
 844	return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
 845
 
 
 846err_lli_free:
 847	for (p_lli = txd->p_lli, v_lli = txd->v_lli; v_lli;
 848	     p_lli = v_lli->p_lli_next, v_lli = v_lli->v_lli_next)
 849		dma_pool_free(sdev->pool, v_lli, p_lli);
 850	kfree(txd);
 851	return NULL;
 852}
 853
 854static int sun6i_dma_config(struct dma_chan *chan,
 855			    struct dma_slave_config *config)
 856{
 857	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 858
 859	memcpy(&vchan->cfg, config, sizeof(*config));
 860
 861	return 0;
 862}
 863
 864static int sun6i_dma_pause(struct dma_chan *chan)
 865{
 866	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 867	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 868	struct sun6i_pchan *pchan = vchan->phy;
 869
 870	dev_dbg(chan2dev(chan), "vchan %p: pause\n", &vchan->vc);
 871
 872	if (pchan) {
 873		writel(DMA_CHAN_PAUSE_PAUSE,
 874		       pchan->base + DMA_CHAN_PAUSE);
 875	} else {
 876		spin_lock(&sdev->lock);
 877		list_del_init(&vchan->node);
 878		spin_unlock(&sdev->lock);
 879	}
 880
 881	return 0;
 882}
 883
 884static int sun6i_dma_resume(struct dma_chan *chan)
 885{
 886	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 887	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 888	struct sun6i_pchan *pchan = vchan->phy;
 889	unsigned long flags;
 890
 891	dev_dbg(chan2dev(chan), "vchan %p: resume\n", &vchan->vc);
 892
 893	spin_lock_irqsave(&vchan->vc.lock, flags);
 894
 895	if (pchan) {
 896		writel(DMA_CHAN_PAUSE_RESUME,
 897		       pchan->base + DMA_CHAN_PAUSE);
 898	} else if (!list_empty(&vchan->vc.desc_issued)) {
 899		spin_lock(&sdev->lock);
 900		list_add_tail(&vchan->node, &sdev->pending);
 901		spin_unlock(&sdev->lock);
 902	}
 903
 904	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 905
 906	return 0;
 907}
 908
 909static int sun6i_dma_terminate_all(struct dma_chan *chan)
 910{
 911	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 912	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 913	struct sun6i_pchan *pchan = vchan->phy;
 914	unsigned long flags;
 915	LIST_HEAD(head);
 916
 917	spin_lock(&sdev->lock);
 918	list_del_init(&vchan->node);
 919	spin_unlock(&sdev->lock);
 920
 921	spin_lock_irqsave(&vchan->vc.lock, flags);
 922
 923	if (vchan->cyclic) {
 924		vchan->cyclic = false;
 925		if (pchan && pchan->desc) {
 926			struct virt_dma_desc *vd = &pchan->desc->vd;
 927			struct virt_dma_chan *vc = &vchan->vc;
 928
 929			list_add_tail(&vd->node, &vc->desc_completed);
 930		}
 931	}
 932
 933	vchan_get_all_descriptors(&vchan->vc, &head);
 934
 935	if (pchan) {
 936		writel(DMA_CHAN_ENABLE_STOP, pchan->base + DMA_CHAN_ENABLE);
 937		writel(DMA_CHAN_PAUSE_RESUME, pchan->base + DMA_CHAN_PAUSE);
 938
 939		vchan->phy = NULL;
 940		pchan->vchan = NULL;
 941		pchan->desc = NULL;
 942		pchan->done = NULL;
 943	}
 944
 945	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 946
 947	vchan_dma_desc_free_list(&vchan->vc, &head);
 948
 949	return 0;
 950}
 951
 952static enum dma_status sun6i_dma_tx_status(struct dma_chan *chan,
 953					   dma_cookie_t cookie,
 954					   struct dma_tx_state *state)
 955{
 956	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 957	struct sun6i_pchan *pchan = vchan->phy;
 958	struct sun6i_dma_lli *lli;
 959	struct virt_dma_desc *vd;
 960	struct sun6i_desc *txd;
 961	enum dma_status ret;
 962	unsigned long flags;
 963	size_t bytes = 0;
 964
 965	ret = dma_cookie_status(chan, cookie, state);
 966	if (ret == DMA_COMPLETE || !state)
 967		return ret;
 968
 969	spin_lock_irqsave(&vchan->vc.lock, flags);
 970
 971	vd = vchan_find_desc(&vchan->vc, cookie);
 972	txd = to_sun6i_desc(&vd->tx);
 973
 974	if (vd) {
 975		for (lli = txd->v_lli; lli != NULL; lli = lli->v_lli_next)
 976			bytes += lli->len;
 977	} else if (!pchan || !pchan->desc) {
 978		bytes = 0;
 979	} else {
 980		bytes = sun6i_get_chan_size(pchan);
 981	}
 982
 983	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 984
 985	dma_set_residue(state, bytes);
 986
 987	return ret;
 988}
 989
 990static void sun6i_dma_issue_pending(struct dma_chan *chan)
 991{
 992	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 993	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 994	unsigned long flags;
 995
 996	spin_lock_irqsave(&vchan->vc.lock, flags);
 997
 998	if (vchan_issue_pending(&vchan->vc)) {
 999		spin_lock(&sdev->lock);
1000
1001		if (!vchan->phy && list_empty(&vchan->node)) {
1002			list_add_tail(&vchan->node, &sdev->pending);
1003			tasklet_schedule(&sdev->task);
1004			dev_dbg(chan2dev(chan), "vchan %p: issued\n",
1005				&vchan->vc);
1006		}
1007
1008		spin_unlock(&sdev->lock);
1009	} else {
1010		dev_dbg(chan2dev(chan), "vchan %p: nothing to issue\n",
1011			&vchan->vc);
1012	}
1013
1014	spin_unlock_irqrestore(&vchan->vc.lock, flags);
1015}
1016
1017static void sun6i_dma_free_chan_resources(struct dma_chan *chan)
1018{
1019	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
1020	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
1021	unsigned long flags;
1022
1023	spin_lock_irqsave(&sdev->lock, flags);
1024	list_del_init(&vchan->node);
1025	spin_unlock_irqrestore(&sdev->lock, flags);
1026
1027	vchan_free_chan_resources(&vchan->vc);
1028}
1029
1030static struct dma_chan *sun6i_dma_of_xlate(struct of_phandle_args *dma_spec,
1031					   struct of_dma *ofdma)
1032{
1033	struct sun6i_dma_dev *sdev = ofdma->of_dma_data;
1034	struct sun6i_vchan *vchan;
1035	struct dma_chan *chan;
1036	u8 port = dma_spec->args[0];
1037
1038	if (port > sdev->max_request)
1039		return NULL;
1040
1041	chan = dma_get_any_slave_channel(&sdev->slave);
1042	if (!chan)
1043		return NULL;
1044
1045	vchan = to_sun6i_vchan(chan);
1046	vchan->port = port;
1047
1048	return chan;
1049}
1050
1051static inline void sun6i_kill_tasklet(struct sun6i_dma_dev *sdev)
1052{
1053	/* Disable all interrupts from DMA */
1054	writel(0, sdev->base + DMA_IRQ_EN(0));
1055	writel(0, sdev->base + DMA_IRQ_EN(1));
1056
1057	/* Prevent spurious interrupts from scheduling the tasklet */
1058	atomic_inc(&sdev->tasklet_shutdown);
1059
1060	/* Make sure we won't have any further interrupts */
1061	devm_free_irq(sdev->slave.dev, sdev->irq, sdev);
1062
1063	/* Actually prevent the tasklet from being scheduled */
1064	tasklet_kill(&sdev->task);
1065}
1066
1067static inline void sun6i_dma_free(struct sun6i_dma_dev *sdev)
1068{
1069	int i;
1070
1071	for (i = 0; i < sdev->num_vchans; i++) {
1072		struct sun6i_vchan *vchan = &sdev->vchans[i];
1073
1074		list_del(&vchan->vc.chan.device_node);
1075		tasklet_kill(&vchan->vc.task);
1076	}
1077}
1078
1079/*
1080 * For A31:
1081 *
1082 * There's 16 physical channels that can work in parallel.
1083 *
1084 * However we have 30 different endpoints for our requests.
1085 *
1086 * Since the channels are able to handle only an unidirectional
1087 * transfer, we need to allocate more virtual channels so that
1088 * everyone can grab one channel.
1089 *
1090 * Some devices can't work in both direction (mostly because it
1091 * wouldn't make sense), so we have a bit fewer virtual channels than
1092 * 2 channels per endpoints.
1093 */
1094
1095static struct sun6i_dma_config sun6i_a31_dma_cfg = {
1096	.nr_max_channels = 16,
1097	.nr_max_requests = 30,
1098	.nr_max_vchans   = 53,
1099	.set_burst_length = sun6i_set_burst_length_a31,
1100	.set_drq          = sun6i_set_drq_a31,
1101	.set_mode         = sun6i_set_mode_a31,
1102	.src_burst_lengths = BIT(1) | BIT(8),
1103	.dst_burst_lengths = BIT(1) | BIT(8),
1104	.src_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1105			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1106			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
1107	.dst_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1108			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1109			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
1110};
1111
1112/*
1113 * The A23 only has 8 physical channels, a maximum DRQ port id of 24,
1114 * and a total of 37 usable source and destination endpoints.
1115 */
1116
1117static struct sun6i_dma_config sun8i_a23_dma_cfg = {
1118	.nr_max_channels = 8,
1119	.nr_max_requests = 24,
1120	.nr_max_vchans   = 37,
1121	.clock_autogate_enable = sun6i_enable_clock_autogate_a23,
1122	.set_burst_length = sun6i_set_burst_length_a31,
1123	.set_drq          = sun6i_set_drq_a31,
1124	.set_mode         = sun6i_set_mode_a31,
1125	.src_burst_lengths = BIT(1) | BIT(8),
1126	.dst_burst_lengths = BIT(1) | BIT(8),
1127	.src_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1128			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1129			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
1130	.dst_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1131			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1132			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
1133};
1134
1135static struct sun6i_dma_config sun8i_a83t_dma_cfg = {
1136	.nr_max_channels = 8,
1137	.nr_max_requests = 28,
1138	.nr_max_vchans   = 39,
1139	.clock_autogate_enable = sun6i_enable_clock_autogate_a23,
1140	.set_burst_length = sun6i_set_burst_length_a31,
1141	.set_drq          = sun6i_set_drq_a31,
1142	.set_mode         = sun6i_set_mode_a31,
1143	.src_burst_lengths = BIT(1) | BIT(8),
1144	.dst_burst_lengths = BIT(1) | BIT(8),
1145	.src_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1146			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1147			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
1148	.dst_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1149			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1150			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
1151};
1152
1153/*
1154 * The H3 has 12 physical channels, a maximum DRQ port id of 27,
1155 * and a total of 34 usable source and destination endpoints.
1156 * It also supports additional burst lengths and bus widths,
1157 * and the burst length fields have different offsets.
1158 */
1159
1160static struct sun6i_dma_config sun8i_h3_dma_cfg = {
1161	.nr_max_channels = 12,
1162	.nr_max_requests = 27,
1163	.nr_max_vchans   = 34,
1164	.clock_autogate_enable = sun6i_enable_clock_autogate_h3,
1165	.set_burst_length = sun6i_set_burst_length_h3,
1166	.set_drq          = sun6i_set_drq_a31,
1167	.set_mode         = sun6i_set_mode_a31,
1168	.src_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
1169	.dst_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
1170	.src_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1171			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1172			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1173			     BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
1174	.dst_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1175			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1176			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1177			     BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
1178};
1179
1180/*
1181 * The A64 binding uses the number of dma channels from the
1182 * device tree node.
1183 */
1184static struct sun6i_dma_config sun50i_a64_dma_cfg = {
1185	.clock_autogate_enable = sun6i_enable_clock_autogate_h3,
1186	.set_burst_length = sun6i_set_burst_length_h3,
1187	.set_drq          = sun6i_set_drq_a31,
1188	.set_mode         = sun6i_set_mode_a31,
1189	.src_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
1190	.dst_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
1191	.src_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1192			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1193			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1194			     BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
1195	.dst_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1196			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1197			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1198			     BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
1199};
1200
1201/*
1202 * The A100 binding uses the number of dma channels from the
1203 * device tree node.
1204 */
1205static struct sun6i_dma_config sun50i_a100_dma_cfg = {
1206	.clock_autogate_enable = sun6i_enable_clock_autogate_h3,
1207	.set_burst_length = sun6i_set_burst_length_h3,
1208	.set_drq          = sun6i_set_drq_h6,
1209	.set_mode         = sun6i_set_mode_h6,
1210	.src_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
1211	.dst_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
1212	.src_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1213			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1214			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1215			     BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
1216	.dst_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1217			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1218			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1219			     BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
1220	.has_high_addr = true,
1221	.has_mbus_clk = true,
1222};
1223
1224/*
1225 * The H6 binding uses the number of dma channels from the
1226 * device tree node.
1227 */
1228static struct sun6i_dma_config sun50i_h6_dma_cfg = {
1229	.clock_autogate_enable = sun6i_enable_clock_autogate_h3,
1230	.set_burst_length = sun6i_set_burst_length_h3,
1231	.set_drq          = sun6i_set_drq_h6,
1232	.set_mode         = sun6i_set_mode_h6,
1233	.src_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
1234	.dst_burst_lengths = BIT(1) | BIT(4) | BIT(8) | BIT(16),
1235	.src_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1236			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1237			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1238			     BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
1239	.dst_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1240			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1241			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1242			     BIT(DMA_SLAVE_BUSWIDTH_8_BYTES),
1243	.has_mbus_clk = true,
1244};
1245
1246/*
1247 * The V3s have only 8 physical channels, a maximum DRQ port id of 23,
1248 * and a total of 24 usable source and destination endpoints.
1249 */
1250
1251static struct sun6i_dma_config sun8i_v3s_dma_cfg = {
1252	.nr_max_channels = 8,
1253	.nr_max_requests = 23,
1254	.nr_max_vchans   = 24,
1255	.clock_autogate_enable = sun6i_enable_clock_autogate_a23,
1256	.set_burst_length = sun6i_set_burst_length_a31,
1257	.set_drq          = sun6i_set_drq_a31,
1258	.set_mode         = sun6i_set_mode_a31,
1259	.src_burst_lengths = BIT(1) | BIT(8),
1260	.dst_burst_lengths = BIT(1) | BIT(8),
1261	.src_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1262			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1263			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
1264	.dst_addr_widths   = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1265			     BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1266			     BIT(DMA_SLAVE_BUSWIDTH_4_BYTES),
1267};
1268
1269static const struct of_device_id sun6i_dma_match[] = {
1270	{ .compatible = "allwinner,sun6i-a31-dma", .data = &sun6i_a31_dma_cfg },
1271	{ .compatible = "allwinner,sun8i-a23-dma", .data = &sun8i_a23_dma_cfg },
1272	{ .compatible = "allwinner,sun8i-a83t-dma", .data = &sun8i_a83t_dma_cfg },
1273	{ .compatible = "allwinner,sun8i-h3-dma", .data = &sun8i_h3_dma_cfg },
1274	{ .compatible = "allwinner,sun8i-v3s-dma", .data = &sun8i_v3s_dma_cfg },
1275	{ .compatible = "allwinner,sun20i-d1-dma", .data = &sun50i_a100_dma_cfg },
1276	{ .compatible = "allwinner,sun50i-a64-dma", .data = &sun50i_a64_dma_cfg },
1277	{ .compatible = "allwinner,sun50i-a100-dma", .data = &sun50i_a100_dma_cfg },
1278	{ .compatible = "allwinner,sun50i-h6-dma", .data = &sun50i_h6_dma_cfg },
1279	{ /* sentinel */ }
1280};
1281MODULE_DEVICE_TABLE(of, sun6i_dma_match);
1282
1283static int sun6i_dma_probe(struct platform_device *pdev)
1284{
1285	struct device_node *np = pdev->dev.of_node;
1286	struct sun6i_dma_dev *sdc;
 
1287	int ret, i;
1288
1289	sdc = devm_kzalloc(&pdev->dev, sizeof(*sdc), GFP_KERNEL);
1290	if (!sdc)
1291		return -ENOMEM;
1292
1293	sdc->cfg = of_device_get_match_data(&pdev->dev);
1294	if (!sdc->cfg)
1295		return -ENODEV;
 
1296
1297	sdc->base = devm_platform_ioremap_resource(pdev, 0);
 
1298	if (IS_ERR(sdc->base))
1299		return PTR_ERR(sdc->base);
1300
1301	sdc->irq = platform_get_irq(pdev, 0);
1302	if (sdc->irq < 0)
 
1303		return sdc->irq;
 
1304
1305	sdc->clk = devm_clk_get(&pdev->dev, NULL);
1306	if (IS_ERR(sdc->clk)) {
1307		dev_err(&pdev->dev, "No clock specified\n");
1308		return PTR_ERR(sdc->clk);
1309	}
1310
1311	if (sdc->cfg->has_mbus_clk) {
1312		sdc->clk_mbus = devm_clk_get(&pdev->dev, "mbus");
1313		if (IS_ERR(sdc->clk_mbus)) {
1314			dev_err(&pdev->dev, "No mbus clock specified\n");
1315			return PTR_ERR(sdc->clk_mbus);
1316		}
1317	}
1318
1319	sdc->rstc = devm_reset_control_get(&pdev->dev, NULL);
1320	if (IS_ERR(sdc->rstc)) {
1321		dev_err(&pdev->dev, "No reset controller specified\n");
1322		return PTR_ERR(sdc->rstc);
1323	}
1324
1325	sdc->pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
1326				     sizeof(struct sun6i_dma_lli), 4, 0);
1327	if (!sdc->pool) {
1328		dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1329		return -ENOMEM;
1330	}
1331
1332	platform_set_drvdata(pdev, sdc);
1333	INIT_LIST_HEAD(&sdc->pending);
1334	spin_lock_init(&sdc->lock);
1335
1336	dma_set_max_seg_size(&pdev->dev, SZ_32M - 1);
1337
1338	dma_cap_set(DMA_PRIVATE, sdc->slave.cap_mask);
1339	dma_cap_set(DMA_MEMCPY, sdc->slave.cap_mask);
1340	dma_cap_set(DMA_SLAVE, sdc->slave.cap_mask);
1341	dma_cap_set(DMA_CYCLIC, sdc->slave.cap_mask);
1342
1343	INIT_LIST_HEAD(&sdc->slave.channels);
1344	sdc->slave.device_free_chan_resources	= sun6i_dma_free_chan_resources;
1345	sdc->slave.device_tx_status		= sun6i_dma_tx_status;
1346	sdc->slave.device_issue_pending		= sun6i_dma_issue_pending;
1347	sdc->slave.device_prep_slave_sg		= sun6i_dma_prep_slave_sg;
1348	sdc->slave.device_prep_dma_memcpy	= sun6i_dma_prep_dma_memcpy;
1349	sdc->slave.device_prep_dma_cyclic	= sun6i_dma_prep_dma_cyclic;
1350	sdc->slave.copy_align			= DMAENGINE_ALIGN_4_BYTES;
1351	sdc->slave.device_config		= sun6i_dma_config;
1352	sdc->slave.device_pause			= sun6i_dma_pause;
1353	sdc->slave.device_resume		= sun6i_dma_resume;
1354	sdc->slave.device_terminate_all		= sun6i_dma_terminate_all;
1355	sdc->slave.src_addr_widths		= sdc->cfg->src_addr_widths;
1356	sdc->slave.dst_addr_widths		= sdc->cfg->dst_addr_widths;
 
 
 
 
1357	sdc->slave.directions			= BIT(DMA_DEV_TO_MEM) |
1358						  BIT(DMA_MEM_TO_DEV);
1359	sdc->slave.residue_granularity		= DMA_RESIDUE_GRANULARITY_BURST;
1360	sdc->slave.dev = &pdev->dev;
1361
1362	sdc->num_pchans = sdc->cfg->nr_max_channels;
1363	sdc->num_vchans = sdc->cfg->nr_max_vchans;
1364	sdc->max_request = sdc->cfg->nr_max_requests;
1365
1366	ret = of_property_read_u32(np, "dma-channels", &sdc->num_pchans);
1367	if (ret && !sdc->num_pchans) {
1368		dev_err(&pdev->dev, "Can't get dma-channels.\n");
1369		return ret;
1370	}
1371
1372	ret = of_property_read_u32(np, "dma-requests", &sdc->max_request);
1373	if (ret && !sdc->max_request) {
1374		dev_info(&pdev->dev, "Missing dma-requests, using %u.\n",
1375			 DMA_CHAN_MAX_DRQ_A31);
1376		sdc->max_request = DMA_CHAN_MAX_DRQ_A31;
1377	}
1378
1379	/*
1380	 * If the number of vchans is not specified, derive it from the
1381	 * highest port number, at most one channel per port and direction.
1382	 */
1383	if (!sdc->num_vchans)
1384		sdc->num_vchans = 2 * (sdc->max_request + 1);
1385
1386	sdc->pchans = devm_kcalloc(&pdev->dev, sdc->num_pchans,
1387				   sizeof(struct sun6i_pchan), GFP_KERNEL);
1388	if (!sdc->pchans)
1389		return -ENOMEM;
1390
1391	sdc->vchans = devm_kcalloc(&pdev->dev, sdc->num_vchans,
1392				   sizeof(struct sun6i_vchan), GFP_KERNEL);
1393	if (!sdc->vchans)
1394		return -ENOMEM;
1395
1396	tasklet_setup(&sdc->task, sun6i_dma_tasklet);
1397
1398	for (i = 0; i < sdc->num_pchans; i++) {
1399		struct sun6i_pchan *pchan = &sdc->pchans[i];
1400
1401		pchan->idx = i;
1402		pchan->base = sdc->base + 0x100 + i * 0x40;
1403	}
1404
1405	for (i = 0; i < sdc->num_vchans; i++) {
1406		struct sun6i_vchan *vchan = &sdc->vchans[i];
1407
1408		INIT_LIST_HEAD(&vchan->node);
1409		vchan->vc.desc_free = sun6i_dma_free_desc;
1410		vchan_init(&vchan->vc, &sdc->slave);
1411	}
1412
1413	ret = reset_control_deassert(sdc->rstc);
1414	if (ret) {
1415		dev_err(&pdev->dev, "Couldn't deassert the device from reset\n");
1416		goto err_chan_free;
1417	}
1418
1419	ret = clk_prepare_enable(sdc->clk);
1420	if (ret) {
1421		dev_err(&pdev->dev, "Couldn't enable the clock\n");
1422		goto err_reset_assert;
1423	}
1424
1425	if (sdc->cfg->has_mbus_clk) {
1426		ret = clk_prepare_enable(sdc->clk_mbus);
1427		if (ret) {
1428			dev_err(&pdev->dev, "Couldn't enable mbus clock\n");
1429			goto err_clk_disable;
1430		}
1431	}
1432
1433	ret = devm_request_irq(&pdev->dev, sdc->irq, sun6i_dma_interrupt, 0,
1434			       dev_name(&pdev->dev), sdc);
1435	if (ret) {
1436		dev_err(&pdev->dev, "Cannot request IRQ\n");
1437		goto err_mbus_clk_disable;
1438	}
1439
1440	ret = dma_async_device_register(&sdc->slave);
1441	if (ret) {
1442		dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
1443		goto err_irq_disable;
1444	}
1445
1446	ret = of_dma_controller_register(pdev->dev.of_node, sun6i_dma_of_xlate,
1447					 sdc);
1448	if (ret) {
1449		dev_err(&pdev->dev, "of_dma_controller_register failed\n");
1450		goto err_dma_unregister;
1451	}
1452
1453	if (sdc->cfg->clock_autogate_enable)
1454		sdc->cfg->clock_autogate_enable(sdc);
 
 
 
 
 
 
1455
1456	return 0;
1457
1458err_dma_unregister:
1459	dma_async_device_unregister(&sdc->slave);
1460err_irq_disable:
1461	sun6i_kill_tasklet(sdc);
1462err_mbus_clk_disable:
1463	clk_disable_unprepare(sdc->clk_mbus);
1464err_clk_disable:
1465	clk_disable_unprepare(sdc->clk);
1466err_reset_assert:
1467	reset_control_assert(sdc->rstc);
1468err_chan_free:
1469	sun6i_dma_free(sdc);
1470	return ret;
1471}
1472
1473static void sun6i_dma_remove(struct platform_device *pdev)
1474{
1475	struct sun6i_dma_dev *sdc = platform_get_drvdata(pdev);
1476
1477	of_dma_controller_free(pdev->dev.of_node);
1478	dma_async_device_unregister(&sdc->slave);
1479
1480	sun6i_kill_tasklet(sdc);
1481
1482	clk_disable_unprepare(sdc->clk_mbus);
1483	clk_disable_unprepare(sdc->clk);
1484	reset_control_assert(sdc->rstc);
1485
1486	sun6i_dma_free(sdc);
 
 
1487}
1488
1489static struct platform_driver sun6i_dma_driver = {
1490	.probe		= sun6i_dma_probe,
1491	.remove_new	= sun6i_dma_remove,
1492	.driver = {
1493		.name		= "sun6i-dma",
1494		.of_match_table	= sun6i_dma_match,
1495	},
1496};
1497module_platform_driver(sun6i_dma_driver);
1498
1499MODULE_DESCRIPTION("Allwinner A31 DMA Controller Driver");
1500MODULE_AUTHOR("Sugar <shuge@allwinnertech.com>");
1501MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1502MODULE_LICENSE("GPL");
v4.6
 
   1/*
   2 * Copyright (C) 2013-2014 Allwinner Tech Co., Ltd
   3 * Author: Sugar <shuge@allwinnertech.com>
   4 *
   5 * Copyright (C) 2014 Maxime Ripard
   6 * Maxime Ripard <maxime.ripard@free-electrons.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 */
  13
  14#include <linux/clk.h>
  15#include <linux/delay.h>
 
  16#include <linux/dmaengine.h>
  17#include <linux/dmapool.h>
  18#include <linux/interrupt.h>
  19#include <linux/module.h>
 
  20#include <linux/of_dma.h>
  21#include <linux/of_device.h>
  22#include <linux/platform_device.h>
  23#include <linux/reset.h>
  24#include <linux/slab.h>
  25#include <linux/types.h>
  26
  27#include "virt-dma.h"
  28
  29/*
  30 * Common registers
  31 */
  32#define DMA_IRQ_EN(x)		((x) * 0x04)
  33#define DMA_IRQ_HALF			BIT(0)
  34#define DMA_IRQ_PKG			BIT(1)
  35#define DMA_IRQ_QUEUE			BIT(2)
  36
  37#define DMA_IRQ_CHAN_NR			8
  38#define DMA_IRQ_CHAN_WIDTH		4
  39
  40
  41#define DMA_IRQ_STAT(x)		((x) * 0x04 + 0x10)
  42
  43#define DMA_STAT		0x30
  44
 
 
 
  45/*
  46 * sun8i specific registers
  47 */
  48#define SUN8I_DMA_GATE		0x20
  49#define SUN8I_DMA_GATE_ENABLE	0x4
  50
 
 
 
  51/*
  52 * Channels specific registers
  53 */
  54#define DMA_CHAN_ENABLE		0x00
  55#define DMA_CHAN_ENABLE_START		BIT(0)
  56#define DMA_CHAN_ENABLE_STOP		0
  57
  58#define DMA_CHAN_PAUSE		0x04
  59#define DMA_CHAN_PAUSE_PAUSE		BIT(1)
  60#define DMA_CHAN_PAUSE_RESUME		0
  61
  62#define DMA_CHAN_LLI_ADDR	0x08
  63
  64#define DMA_CHAN_CUR_CFG	0x0c
  65#define DMA_CHAN_CFG_SRC_DRQ(x)		((x) & 0x1f)
  66#define DMA_CHAN_CFG_SRC_IO_MODE	BIT(5)
  67#define DMA_CHAN_CFG_SRC_LINEAR_MODE	(0 << 5)
  68#define DMA_CHAN_CFG_SRC_BURST(x)	(((x) & 0x3) << 7)
 
 
 
 
  69#define DMA_CHAN_CFG_SRC_WIDTH(x)	(((x) & 0x3) << 9)
  70
  71#define DMA_CHAN_CFG_DST_DRQ(x)		(DMA_CHAN_CFG_SRC_DRQ(x) << 16)
  72#define DMA_CHAN_CFG_DST_IO_MODE	(DMA_CHAN_CFG_SRC_IO_MODE << 16)
  73#define DMA_CHAN_CFG_DST_LINEAR_MODE	(DMA_CHAN_CFG_SRC_LINEAR_MODE << 16)
  74#define DMA_CHAN_CFG_DST_BURST(x)	(DMA_CHAN_CFG_SRC_BURST(x) << 16)
 
 
  75#define DMA_CHAN_CFG_DST_WIDTH(x)	(DMA_CHAN_CFG_SRC_WIDTH(x) << 16)
  76
  77#define DMA_CHAN_CUR_SRC	0x10
  78
  79#define DMA_CHAN_CUR_DST	0x14
  80
  81#define DMA_CHAN_CUR_CNT	0x18
  82
  83#define DMA_CHAN_CUR_PARA	0x1c
  84
 
 
 
 
 
 
 
 
  85
  86/*
  87 * Various hardware related defines
  88 */
  89#define LLI_LAST_ITEM	0xfffff800
  90#define NORMAL_WAIT	8
  91#define DRQ_SDRAM	1
 
 
 
 
 
  92
  93/*
  94 * Hardware channels / ports representation
  95 *
  96 * The hardware is used in several SoCs, with differing numbers
  97 * of channels and endpoints. This structure ties those numbers
  98 * to a certain compatible string.
  99 */
 100struct sun6i_dma_config {
 101	u32 nr_max_channels;
 102	u32 nr_max_requests;
 103	u32 nr_max_vchans;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 104};
 105
 106/*
 107 * Hardware representation of the LLI
 108 *
 109 * The hardware will be fed the physical address of this structure,
 110 * and read its content in order to start the transfer.
 111 */
 112struct sun6i_dma_lli {
 113	u32			cfg;
 114	u32			src;
 115	u32			dst;
 116	u32			len;
 117	u32			para;
 118	u32			p_lli_next;
 119
 120	/*
 121	 * This field is not used by the DMA controller, but will be
 122	 * used by the CPU to go through the list (mostly for dumping
 123	 * or freeing it).
 124	 */
 125	struct sun6i_dma_lli	*v_lli_next;
 126};
 127
 128
 129struct sun6i_desc {
 130	struct virt_dma_desc	vd;
 131	dma_addr_t		p_lli;
 132	struct sun6i_dma_lli	*v_lli;
 133};
 134
 135struct sun6i_pchan {
 136	u32			idx;
 137	void __iomem		*base;
 138	struct sun6i_vchan	*vchan;
 139	struct sun6i_desc	*desc;
 140	struct sun6i_desc	*done;
 141};
 142
 143struct sun6i_vchan {
 144	struct virt_dma_chan	vc;
 145	struct list_head	node;
 146	struct dma_slave_config	cfg;
 147	struct sun6i_pchan	*phy;
 148	u8			port;
 
 
 149};
 150
 151struct sun6i_dma_dev {
 152	struct dma_device	slave;
 153	void __iomem		*base;
 154	struct clk		*clk;
 
 155	int			irq;
 156	spinlock_t		lock;
 157	struct reset_control	*rstc;
 158	struct tasklet_struct	task;
 159	atomic_t		tasklet_shutdown;
 160	struct list_head	pending;
 161	struct dma_pool		*pool;
 162	struct sun6i_pchan	*pchans;
 163	struct sun6i_vchan	*vchans;
 164	const struct sun6i_dma_config *cfg;
 
 
 
 165};
 166
 167static struct device *chan2dev(struct dma_chan *chan)
 168{
 169	return &chan->dev->device;
 170}
 171
 172static inline struct sun6i_dma_dev *to_sun6i_dma_dev(struct dma_device *d)
 173{
 174	return container_of(d, struct sun6i_dma_dev, slave);
 175}
 176
 177static inline struct sun6i_vchan *to_sun6i_vchan(struct dma_chan *chan)
 178{
 179	return container_of(chan, struct sun6i_vchan, vc.chan);
 180}
 181
 182static inline struct sun6i_desc *
 183to_sun6i_desc(struct dma_async_tx_descriptor *tx)
 184{
 185	return container_of(tx, struct sun6i_desc, vd.tx);
 186}
 187
 188static inline void sun6i_dma_dump_com_regs(struct sun6i_dma_dev *sdev)
 189{
 190	dev_dbg(sdev->slave.dev, "Common register:\n"
 191		"\tmask0(%04x): 0x%08x\n"
 192		"\tmask1(%04x): 0x%08x\n"
 193		"\tpend0(%04x): 0x%08x\n"
 194		"\tpend1(%04x): 0x%08x\n"
 195		"\tstats(%04x): 0x%08x\n",
 196		DMA_IRQ_EN(0), readl(sdev->base + DMA_IRQ_EN(0)),
 197		DMA_IRQ_EN(1), readl(sdev->base + DMA_IRQ_EN(1)),
 198		DMA_IRQ_STAT(0), readl(sdev->base + DMA_IRQ_STAT(0)),
 199		DMA_IRQ_STAT(1), readl(sdev->base + DMA_IRQ_STAT(1)),
 200		DMA_STAT, readl(sdev->base + DMA_STAT));
 201}
 202
 203static inline void sun6i_dma_dump_chan_regs(struct sun6i_dma_dev *sdev,
 204					    struct sun6i_pchan *pchan)
 205{
 206	phys_addr_t reg = virt_to_phys(pchan->base);
 207
 208	dev_dbg(sdev->slave.dev, "Chan %d reg: %pa\n"
 209		"\t___en(%04x): \t0x%08x\n"
 210		"\tpause(%04x): \t0x%08x\n"
 211		"\tstart(%04x): \t0x%08x\n"
 212		"\t__cfg(%04x): \t0x%08x\n"
 213		"\t__src(%04x): \t0x%08x\n"
 214		"\t__dst(%04x): \t0x%08x\n"
 215		"\tcount(%04x): \t0x%08x\n"
 216		"\t_para(%04x): \t0x%08x\n\n",
 217		pchan->idx, &reg,
 218		DMA_CHAN_ENABLE,
 219		readl(pchan->base + DMA_CHAN_ENABLE),
 220		DMA_CHAN_PAUSE,
 221		readl(pchan->base + DMA_CHAN_PAUSE),
 222		DMA_CHAN_LLI_ADDR,
 223		readl(pchan->base + DMA_CHAN_LLI_ADDR),
 224		DMA_CHAN_CUR_CFG,
 225		readl(pchan->base + DMA_CHAN_CUR_CFG),
 226		DMA_CHAN_CUR_SRC,
 227		readl(pchan->base + DMA_CHAN_CUR_SRC),
 228		DMA_CHAN_CUR_DST,
 229		readl(pchan->base + DMA_CHAN_CUR_DST),
 230		DMA_CHAN_CUR_CNT,
 231		readl(pchan->base + DMA_CHAN_CUR_CNT),
 232		DMA_CHAN_CUR_PARA,
 233		readl(pchan->base + DMA_CHAN_CUR_PARA));
 234}
 235
 236static inline s8 convert_burst(u32 maxburst)
 237{
 238	switch (maxburst) {
 239	case 1:
 240		return 0;
 
 
 241	case 8:
 242		return 2;
 
 
 243	default:
 244		return -EINVAL;
 245	}
 246}
 247
 248static inline s8 convert_buswidth(enum dma_slave_buswidth addr_width)
 249{
 250	if ((addr_width < DMA_SLAVE_BUSWIDTH_1_BYTE) ||
 251	    (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES))
 252		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 253
 254	return addr_width >> 1;
 255}
 256
 257static void *sun6i_dma_lli_add(struct sun6i_dma_lli *prev,
 258			       struct sun6i_dma_lli *next,
 259			       dma_addr_t next_phy,
 260			       struct sun6i_desc *txd)
 261{
 262	if ((!prev && !txd) || !next)
 263		return NULL;
 264
 265	if (!prev) {
 266		txd->p_lli = next_phy;
 267		txd->v_lli = next;
 268	} else {
 269		prev->p_lli_next = next_phy;
 270		prev->v_lli_next = next;
 271	}
 272
 273	next->p_lli_next = LLI_LAST_ITEM;
 274	next->v_lli_next = NULL;
 275
 276	return next;
 277}
 278
 279static inline int sun6i_dma_cfg_lli(struct sun6i_dma_lli *lli,
 280				    dma_addr_t src,
 281				    dma_addr_t dst, u32 len,
 282				    struct dma_slave_config *config)
 283{
 284	u8 src_width, dst_width, src_burst, dst_burst;
 285
 286	if (!config)
 287		return -EINVAL;
 288
 289	src_burst = convert_burst(config->src_maxburst);
 290	if (src_burst)
 291		return src_burst;
 292
 293	dst_burst = convert_burst(config->dst_maxburst);
 294	if (dst_burst)
 295		return dst_burst;
 296
 297	src_width = convert_buswidth(config->src_addr_width);
 298	if (src_width)
 299		return src_width;
 300
 301	dst_width = convert_buswidth(config->dst_addr_width);
 302	if (dst_width)
 303		return dst_width;
 304
 305	lli->cfg = DMA_CHAN_CFG_SRC_BURST(src_burst) |
 306		DMA_CHAN_CFG_SRC_WIDTH(src_width) |
 307		DMA_CHAN_CFG_DST_BURST(dst_burst) |
 308		DMA_CHAN_CFG_DST_WIDTH(dst_width);
 309
 310	lli->src = src;
 311	lli->dst = dst;
 312	lli->len = len;
 313	lli->para = NORMAL_WAIT;
 314
 315	return 0;
 316}
 317
 318static inline void sun6i_dma_dump_lli(struct sun6i_vchan *vchan,
 319				      struct sun6i_dma_lli *lli)
 
 320{
 321	phys_addr_t p_lli = virt_to_phys(lli);
 322
 323	dev_dbg(chan2dev(&vchan->vc.chan),
 324		"\n\tdesc:   p - %pa v - 0x%p\n"
 325		"\t\tc - 0x%08x s - 0x%08x d - 0x%08x\n"
 326		"\t\tl - 0x%08x p - 0x%08x n - 0x%08x\n",
 327		&p_lli, lli,
 328		lli->cfg, lli->src, lli->dst,
 329		lli->len, lli->para, lli->p_lli_next);
 330}
 331
 332static void sun6i_dma_free_desc(struct virt_dma_desc *vd)
 333{
 334	struct sun6i_desc *txd = to_sun6i_desc(&vd->tx);
 335	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vd->tx.chan->device);
 336	struct sun6i_dma_lli *v_lli, *v_next;
 337	dma_addr_t p_lli, p_next;
 338
 339	if (unlikely(!txd))
 340		return;
 341
 342	p_lli = txd->p_lli;
 343	v_lli = txd->v_lli;
 344
 345	while (v_lli) {
 346		v_next = v_lli->v_lli_next;
 347		p_next = v_lli->p_lli_next;
 348
 349		dma_pool_free(sdev->pool, v_lli, p_lli);
 350
 351		v_lli = v_next;
 352		p_lli = p_next;
 353	}
 354
 355	kfree(txd);
 356}
 357
 358static int sun6i_dma_start_desc(struct sun6i_vchan *vchan)
 359{
 360	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vchan->vc.chan.device);
 361	struct virt_dma_desc *desc = vchan_next_desc(&vchan->vc);
 362	struct sun6i_pchan *pchan = vchan->phy;
 363	u32 irq_val, irq_reg, irq_offset;
 364
 365	if (!pchan)
 366		return -EAGAIN;
 367
 368	if (!desc) {
 369		pchan->desc = NULL;
 370		pchan->done = NULL;
 371		return -EAGAIN;
 372	}
 373
 374	list_del(&desc->node);
 375
 376	pchan->desc = to_sun6i_desc(&desc->tx);
 377	pchan->done = NULL;
 378
 379	sun6i_dma_dump_lli(vchan, pchan->desc->v_lli);
 380
 381	irq_reg = pchan->idx / DMA_IRQ_CHAN_NR;
 382	irq_offset = pchan->idx % DMA_IRQ_CHAN_NR;
 383
 384	irq_val = readl(sdev->base + DMA_IRQ_EN(irq_offset));
 385	irq_val |= DMA_IRQ_QUEUE << (irq_offset * DMA_IRQ_CHAN_WIDTH);
 386	writel(irq_val, sdev->base + DMA_IRQ_EN(irq_offset));
 
 
 
 
 387
 388	writel(pchan->desc->p_lli, pchan->base + DMA_CHAN_LLI_ADDR);
 389	writel(DMA_CHAN_ENABLE_START, pchan->base + DMA_CHAN_ENABLE);
 390
 391	sun6i_dma_dump_com_regs(sdev);
 392	sun6i_dma_dump_chan_regs(sdev, pchan);
 393
 394	return 0;
 395}
 396
 397static void sun6i_dma_tasklet(unsigned long data)
 398{
 399	struct sun6i_dma_dev *sdev = (struct sun6i_dma_dev *)data;
 400	const struct sun6i_dma_config *cfg = sdev->cfg;
 401	struct sun6i_vchan *vchan;
 402	struct sun6i_pchan *pchan;
 403	unsigned int pchan_alloc = 0;
 404	unsigned int pchan_idx;
 405
 406	list_for_each_entry(vchan, &sdev->slave.channels, vc.chan.device_node) {
 407		spin_lock_irq(&vchan->vc.lock);
 408
 409		pchan = vchan->phy;
 410
 411		if (pchan && pchan->done) {
 412			if (sun6i_dma_start_desc(vchan)) {
 413				/*
 414				 * No current txd associated with this channel
 415				 */
 416				dev_dbg(sdev->slave.dev, "pchan %u: free\n",
 417					pchan->idx);
 418
 419				/* Mark this channel free */
 420				vchan->phy = NULL;
 421				pchan->vchan = NULL;
 422			}
 423		}
 424		spin_unlock_irq(&vchan->vc.lock);
 425	}
 426
 427	spin_lock_irq(&sdev->lock);
 428	for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
 429		pchan = &sdev->pchans[pchan_idx];
 430
 431		if (pchan->vchan || list_empty(&sdev->pending))
 432			continue;
 433
 434		vchan = list_first_entry(&sdev->pending,
 435					 struct sun6i_vchan, node);
 436
 437		/* Remove from pending channels */
 438		list_del_init(&vchan->node);
 439		pchan_alloc |= BIT(pchan_idx);
 440
 441		/* Mark this channel allocated */
 442		pchan->vchan = vchan;
 443		vchan->phy = pchan;
 444		dev_dbg(sdev->slave.dev, "pchan %u: alloc vchan %p\n",
 445			pchan->idx, &vchan->vc);
 446	}
 447	spin_unlock_irq(&sdev->lock);
 448
 449	for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
 450		if (!(pchan_alloc & BIT(pchan_idx)))
 451			continue;
 452
 453		pchan = sdev->pchans + pchan_idx;
 454		vchan = pchan->vchan;
 455		if (vchan) {
 456			spin_lock_irq(&vchan->vc.lock);
 457			sun6i_dma_start_desc(vchan);
 458			spin_unlock_irq(&vchan->vc.lock);
 459		}
 460	}
 461}
 462
 463static irqreturn_t sun6i_dma_interrupt(int irq, void *dev_id)
 464{
 465	struct sun6i_dma_dev *sdev = dev_id;
 466	struct sun6i_vchan *vchan;
 467	struct sun6i_pchan *pchan;
 468	int i, j, ret = IRQ_NONE;
 469	u32 status;
 470
 471	for (i = 0; i < sdev->cfg->nr_max_channels / DMA_IRQ_CHAN_NR; i++) {
 472		status = readl(sdev->base + DMA_IRQ_STAT(i));
 473		if (!status)
 474			continue;
 475
 476		dev_dbg(sdev->slave.dev, "DMA irq status %s: 0x%x\n",
 477			i ? "high" : "low", status);
 478
 479		writel(status, sdev->base + DMA_IRQ_STAT(i));
 480
 481		for (j = 0; (j < DMA_IRQ_CHAN_NR) && status; j++) {
 482			if (status & DMA_IRQ_QUEUE) {
 483				pchan = sdev->pchans + j;
 484				vchan = pchan->vchan;
 485
 486				if (vchan) {
 
 487					spin_lock(&vchan->vc.lock);
 488					vchan_cookie_complete(&pchan->desc->vd);
 489					pchan->done = pchan->desc;
 490					spin_unlock(&vchan->vc.lock);
 491				}
 492			}
 493
 494			status = status >> DMA_IRQ_CHAN_WIDTH;
 495		}
 496
 497		if (!atomic_read(&sdev->tasklet_shutdown))
 498			tasklet_schedule(&sdev->task);
 499		ret = IRQ_HANDLED;
 500	}
 501
 502	return ret;
 503}
 504
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 505static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_memcpy(
 506		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
 507		size_t len, unsigned long flags)
 508{
 509	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 510	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 511	struct sun6i_dma_lli *v_lli;
 512	struct sun6i_desc *txd;
 513	dma_addr_t p_lli;
 514	s8 burst, width;
 515
 516	dev_dbg(chan2dev(chan),
 517		"%s; chan: %d, dest: %pad, src: %pad, len: %zu. flags: 0x%08lx\n",
 518		__func__, vchan->vc.chan.chan_id, &dest, &src, len, flags);
 519
 520	if (!len)
 521		return NULL;
 522
 523	txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
 524	if (!txd)
 525		return NULL;
 526
 527	v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
 528	if (!v_lli) {
 529		dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
 530		goto err_txd_free;
 531	}
 532
 533	v_lli->src = src;
 534	v_lli->dst = dest;
 535	v_lli->len = len;
 536	v_lli->para = NORMAL_WAIT;
 
 537
 538	burst = convert_burst(8);
 539	width = convert_buswidth(DMA_SLAVE_BUSWIDTH_4_BYTES);
 540	v_lli->cfg |= DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
 541		DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
 542		DMA_CHAN_CFG_DST_LINEAR_MODE |
 543		DMA_CHAN_CFG_SRC_LINEAR_MODE |
 544		DMA_CHAN_CFG_SRC_BURST(burst) |
 545		DMA_CHAN_CFG_SRC_WIDTH(width) |
 546		DMA_CHAN_CFG_DST_BURST(burst) |
 547		DMA_CHAN_CFG_DST_WIDTH(width);
 548
 
 
 
 
 549	sun6i_dma_lli_add(NULL, v_lli, p_lli, txd);
 550
 551	sun6i_dma_dump_lli(vchan, v_lli);
 552
 553	return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
 554
 555err_txd_free:
 556	kfree(txd);
 557	return NULL;
 558}
 559
 560static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg(
 561		struct dma_chan *chan, struct scatterlist *sgl,
 562		unsigned int sg_len, enum dma_transfer_direction dir,
 563		unsigned long flags, void *context)
 564{
 565	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 566	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 567	struct dma_slave_config *sconfig = &vchan->cfg;
 568	struct sun6i_dma_lli *v_lli, *prev = NULL;
 569	struct sun6i_desc *txd;
 570	struct scatterlist *sg;
 571	dma_addr_t p_lli;
 
 572	int i, ret;
 573
 574	if (!sgl)
 575		return NULL;
 576
 577	if (!is_slave_direction(dir)) {
 578		dev_err(chan2dev(chan), "Invalid DMA direction\n");
 
 579		return NULL;
 580	}
 581
 582	txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
 583	if (!txd)
 584		return NULL;
 585
 586	for_each_sg(sgl, sg, sg_len, i) {
 587		v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
 588		if (!v_lli)
 589			goto err_lli_free;
 590
 
 
 
 591		if (dir == DMA_MEM_TO_DEV) {
 592			ret = sun6i_dma_cfg_lli(v_lli, sg_dma_address(sg),
 593						sconfig->dst_addr, sg_dma_len(sg),
 594						sconfig);
 595			if (ret)
 596				goto err_cur_lli_free;
 597
 598			v_lli->cfg |= DMA_CHAN_CFG_DST_IO_MODE |
 599				DMA_CHAN_CFG_SRC_LINEAR_MODE |
 600				DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
 601				DMA_CHAN_CFG_DST_DRQ(vchan->port);
 602
 603			dev_dbg(chan2dev(chan),
 604				"%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
 605				__func__, vchan->vc.chan.chan_id,
 606				&sconfig->dst_addr, &sg_dma_address(sg),
 607				sg_dma_len(sg), flags);
 608
 609		} else {
 610			ret = sun6i_dma_cfg_lli(v_lli, sconfig->src_addr,
 611						sg_dma_address(sg), sg_dma_len(sg),
 612						sconfig);
 613			if (ret)
 614				goto err_cur_lli_free;
 615
 616			v_lli->cfg |= DMA_CHAN_CFG_DST_LINEAR_MODE |
 617				DMA_CHAN_CFG_SRC_IO_MODE |
 618				DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
 619				DMA_CHAN_CFG_SRC_DRQ(vchan->port);
 620
 621			dev_dbg(chan2dev(chan),
 622				"%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
 623				__func__, vchan->vc.chan.chan_id,
 624				&sg_dma_address(sg), &sconfig->src_addr,
 625				sg_dma_len(sg), flags);
 626		}
 627
 628		prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
 629	}
 630
 631	dev_dbg(chan2dev(chan), "First: %pad\n", &txd->p_lli);
 632	for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
 633		sun6i_dma_dump_lli(vchan, prev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 634
 635	return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
 636
 637err_cur_lli_free:
 638	dma_pool_free(sdev->pool, v_lli, p_lli);
 639err_lli_free:
 640	for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
 641		dma_pool_free(sdev->pool, prev, virt_to_phys(prev));
 
 642	kfree(txd);
 643	return NULL;
 644}
 645
 646static int sun6i_dma_config(struct dma_chan *chan,
 647			    struct dma_slave_config *config)
 648{
 649	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 650
 651	memcpy(&vchan->cfg, config, sizeof(*config));
 652
 653	return 0;
 654}
 655
 656static int sun6i_dma_pause(struct dma_chan *chan)
 657{
 658	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 659	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 660	struct sun6i_pchan *pchan = vchan->phy;
 661
 662	dev_dbg(chan2dev(chan), "vchan %p: pause\n", &vchan->vc);
 663
 664	if (pchan) {
 665		writel(DMA_CHAN_PAUSE_PAUSE,
 666		       pchan->base + DMA_CHAN_PAUSE);
 667	} else {
 668		spin_lock(&sdev->lock);
 669		list_del_init(&vchan->node);
 670		spin_unlock(&sdev->lock);
 671	}
 672
 673	return 0;
 674}
 675
 676static int sun6i_dma_resume(struct dma_chan *chan)
 677{
 678	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 679	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 680	struct sun6i_pchan *pchan = vchan->phy;
 681	unsigned long flags;
 682
 683	dev_dbg(chan2dev(chan), "vchan %p: resume\n", &vchan->vc);
 684
 685	spin_lock_irqsave(&vchan->vc.lock, flags);
 686
 687	if (pchan) {
 688		writel(DMA_CHAN_PAUSE_RESUME,
 689		       pchan->base + DMA_CHAN_PAUSE);
 690	} else if (!list_empty(&vchan->vc.desc_issued)) {
 691		spin_lock(&sdev->lock);
 692		list_add_tail(&vchan->node, &sdev->pending);
 693		spin_unlock(&sdev->lock);
 694	}
 695
 696	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 697
 698	return 0;
 699}
 700
 701static int sun6i_dma_terminate_all(struct dma_chan *chan)
 702{
 703	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 704	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 705	struct sun6i_pchan *pchan = vchan->phy;
 706	unsigned long flags;
 707	LIST_HEAD(head);
 708
 709	spin_lock(&sdev->lock);
 710	list_del_init(&vchan->node);
 711	spin_unlock(&sdev->lock);
 712
 713	spin_lock_irqsave(&vchan->vc.lock, flags);
 714
 
 
 
 
 
 
 
 
 
 
 715	vchan_get_all_descriptors(&vchan->vc, &head);
 716
 717	if (pchan) {
 718		writel(DMA_CHAN_ENABLE_STOP, pchan->base + DMA_CHAN_ENABLE);
 719		writel(DMA_CHAN_PAUSE_RESUME, pchan->base + DMA_CHAN_PAUSE);
 720
 721		vchan->phy = NULL;
 722		pchan->vchan = NULL;
 723		pchan->desc = NULL;
 724		pchan->done = NULL;
 725	}
 726
 727	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 728
 729	vchan_dma_desc_free_list(&vchan->vc, &head);
 730
 731	return 0;
 732}
 733
 734static enum dma_status sun6i_dma_tx_status(struct dma_chan *chan,
 735					   dma_cookie_t cookie,
 736					   struct dma_tx_state *state)
 737{
 738	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 739	struct sun6i_pchan *pchan = vchan->phy;
 740	struct sun6i_dma_lli *lli;
 741	struct virt_dma_desc *vd;
 742	struct sun6i_desc *txd;
 743	enum dma_status ret;
 744	unsigned long flags;
 745	size_t bytes = 0;
 746
 747	ret = dma_cookie_status(chan, cookie, state);
 748	if (ret == DMA_COMPLETE)
 749		return ret;
 750
 751	spin_lock_irqsave(&vchan->vc.lock, flags);
 752
 753	vd = vchan_find_desc(&vchan->vc, cookie);
 754	txd = to_sun6i_desc(&vd->tx);
 755
 756	if (vd) {
 757		for (lli = txd->v_lli; lli != NULL; lli = lli->v_lli_next)
 758			bytes += lli->len;
 759	} else if (!pchan || !pchan->desc) {
 760		bytes = 0;
 761	} else {
 762		bytes = readl(pchan->base + DMA_CHAN_CUR_CNT);
 763	}
 764
 765	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 766
 767	dma_set_residue(state, bytes);
 768
 769	return ret;
 770}
 771
 772static void sun6i_dma_issue_pending(struct dma_chan *chan)
 773{
 774	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 775	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 776	unsigned long flags;
 777
 778	spin_lock_irqsave(&vchan->vc.lock, flags);
 779
 780	if (vchan_issue_pending(&vchan->vc)) {
 781		spin_lock(&sdev->lock);
 782
 783		if (!vchan->phy && list_empty(&vchan->node)) {
 784			list_add_tail(&vchan->node, &sdev->pending);
 785			tasklet_schedule(&sdev->task);
 786			dev_dbg(chan2dev(chan), "vchan %p: issued\n",
 787				&vchan->vc);
 788		}
 789
 790		spin_unlock(&sdev->lock);
 791	} else {
 792		dev_dbg(chan2dev(chan), "vchan %p: nothing to issue\n",
 793			&vchan->vc);
 794	}
 795
 796	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 797}
 798
 799static void sun6i_dma_free_chan_resources(struct dma_chan *chan)
 800{
 801	struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
 802	struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
 803	unsigned long flags;
 804
 805	spin_lock_irqsave(&sdev->lock, flags);
 806	list_del_init(&vchan->node);
 807	spin_unlock_irqrestore(&sdev->lock, flags);
 808
 809	vchan_free_chan_resources(&vchan->vc);
 810}
 811
 812static struct dma_chan *sun6i_dma_of_xlate(struct of_phandle_args *dma_spec,
 813					   struct of_dma *ofdma)
 814{
 815	struct sun6i_dma_dev *sdev = ofdma->of_dma_data;
 816	struct sun6i_vchan *vchan;
 817	struct dma_chan *chan;
 818	u8 port = dma_spec->args[0];
 819
 820	if (port > sdev->cfg->nr_max_requests)
 821		return NULL;
 822
 823	chan = dma_get_any_slave_channel(&sdev->slave);
 824	if (!chan)
 825		return NULL;
 826
 827	vchan = to_sun6i_vchan(chan);
 828	vchan->port = port;
 829
 830	return chan;
 831}
 832
 833static inline void sun6i_kill_tasklet(struct sun6i_dma_dev *sdev)
 834{
 835	/* Disable all interrupts from DMA */
 836	writel(0, sdev->base + DMA_IRQ_EN(0));
 837	writel(0, sdev->base + DMA_IRQ_EN(1));
 838
 839	/* Prevent spurious interrupts from scheduling the tasklet */
 840	atomic_inc(&sdev->tasklet_shutdown);
 841
 842	/* Make sure we won't have any further interrupts */
 843	devm_free_irq(sdev->slave.dev, sdev->irq, sdev);
 844
 845	/* Actually prevent the tasklet from being scheduled */
 846	tasklet_kill(&sdev->task);
 847}
 848
 849static inline void sun6i_dma_free(struct sun6i_dma_dev *sdev)
 850{
 851	int i;
 852
 853	for (i = 0; i < sdev->cfg->nr_max_vchans; i++) {
 854		struct sun6i_vchan *vchan = &sdev->vchans[i];
 855
 856		list_del(&vchan->vc.chan.device_node);
 857		tasklet_kill(&vchan->vc.task);
 858	}
 859}
 860
 861/*
 862 * For A31:
 863 *
 864 * There's 16 physical channels that can work in parallel.
 865 *
 866 * However we have 30 different endpoints for our requests.
 867 *
 868 * Since the channels are able to handle only an unidirectional
 869 * transfer, we need to allocate more virtual channels so that
 870 * everyone can grab one channel.
 871 *
 872 * Some devices can't work in both direction (mostly because it
 873 * wouldn't make sense), so we have a bit fewer virtual channels than
 874 * 2 channels per endpoints.
 875 */
 876
 877static struct sun6i_dma_config sun6i_a31_dma_cfg = {
 878	.nr_max_channels = 16,
 879	.nr_max_requests = 30,
 880	.nr_max_vchans   = 53,
 
 
 
 
 
 
 
 
 
 
 
 881};
 882
 883/*
 884 * The A23 only has 8 physical channels, a maximum DRQ port id of 24,
 885 * and a total of 37 usable source and destination endpoints.
 886 */
 887
 888static struct sun6i_dma_config sun8i_a23_dma_cfg = {
 889	.nr_max_channels = 8,
 890	.nr_max_requests = 24,
 891	.nr_max_vchans   = 37,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 892};
 893
 894/*
 895 * The H3 has 12 physical channels, a maximum DRQ port id of 27,
 896 * and a total of 34 usable source and destination endpoints.
 
 
 897 */
 898
 899static struct sun6i_dma_config sun8i_h3_dma_cfg = {
 900	.nr_max_channels = 12,
 901	.nr_max_requests = 27,
 902	.nr_max_vchans   = 34,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 903};
 904
 905static const struct of_device_id sun6i_dma_match[] = {
 906	{ .compatible = "allwinner,sun6i-a31-dma", .data = &sun6i_a31_dma_cfg },
 907	{ .compatible = "allwinner,sun8i-a23-dma", .data = &sun8i_a23_dma_cfg },
 
 908	{ .compatible = "allwinner,sun8i-h3-dma", .data = &sun8i_h3_dma_cfg },
 
 
 
 
 
 909	{ /* sentinel */ }
 910};
 911MODULE_DEVICE_TABLE(of, sun6i_dma_match);
 912
 913static int sun6i_dma_probe(struct platform_device *pdev)
 914{
 915	const struct of_device_id *device;
 916	struct sun6i_dma_dev *sdc;
 917	struct resource *res;
 918	int ret, i;
 919
 920	sdc = devm_kzalloc(&pdev->dev, sizeof(*sdc), GFP_KERNEL);
 921	if (!sdc)
 922		return -ENOMEM;
 923
 924	device = of_match_device(sun6i_dma_match, &pdev->dev);
 925	if (!device)
 926		return -ENODEV;
 927	sdc->cfg = device->data;
 928
 929	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 930	sdc->base = devm_ioremap_resource(&pdev->dev, res);
 931	if (IS_ERR(sdc->base))
 932		return PTR_ERR(sdc->base);
 933
 934	sdc->irq = platform_get_irq(pdev, 0);
 935	if (sdc->irq < 0) {
 936		dev_err(&pdev->dev, "Cannot claim IRQ\n");
 937		return sdc->irq;
 938	}
 939
 940	sdc->clk = devm_clk_get(&pdev->dev, NULL);
 941	if (IS_ERR(sdc->clk)) {
 942		dev_err(&pdev->dev, "No clock specified\n");
 943		return PTR_ERR(sdc->clk);
 944	}
 945
 
 
 
 
 
 
 
 
 946	sdc->rstc = devm_reset_control_get(&pdev->dev, NULL);
 947	if (IS_ERR(sdc->rstc)) {
 948		dev_err(&pdev->dev, "No reset controller specified\n");
 949		return PTR_ERR(sdc->rstc);
 950	}
 951
 952	sdc->pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
 953				     sizeof(struct sun6i_dma_lli), 4, 0);
 954	if (!sdc->pool) {
 955		dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
 956		return -ENOMEM;
 957	}
 958
 959	platform_set_drvdata(pdev, sdc);
 960	INIT_LIST_HEAD(&sdc->pending);
 961	spin_lock_init(&sdc->lock);
 962
 
 
 963	dma_cap_set(DMA_PRIVATE, sdc->slave.cap_mask);
 964	dma_cap_set(DMA_MEMCPY, sdc->slave.cap_mask);
 965	dma_cap_set(DMA_SLAVE, sdc->slave.cap_mask);
 
 966
 967	INIT_LIST_HEAD(&sdc->slave.channels);
 968	sdc->slave.device_free_chan_resources	= sun6i_dma_free_chan_resources;
 969	sdc->slave.device_tx_status		= sun6i_dma_tx_status;
 970	sdc->slave.device_issue_pending		= sun6i_dma_issue_pending;
 971	sdc->slave.device_prep_slave_sg		= sun6i_dma_prep_slave_sg;
 972	sdc->slave.device_prep_dma_memcpy	= sun6i_dma_prep_dma_memcpy;
 
 973	sdc->slave.copy_align			= DMAENGINE_ALIGN_4_BYTES;
 974	sdc->slave.device_config		= sun6i_dma_config;
 975	sdc->slave.device_pause			= sun6i_dma_pause;
 976	sdc->slave.device_resume		= sun6i_dma_resume;
 977	sdc->slave.device_terminate_all		= sun6i_dma_terminate_all;
 978	sdc->slave.src_addr_widths		= BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
 979						  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
 980						  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
 981	sdc->slave.dst_addr_widths		= BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
 982						  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
 983						  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
 984	sdc->slave.directions			= BIT(DMA_DEV_TO_MEM) |
 985						  BIT(DMA_MEM_TO_DEV);
 986	sdc->slave.residue_granularity		= DMA_RESIDUE_GRANULARITY_BURST;
 987	sdc->slave.dev = &pdev->dev;
 988
 989	sdc->pchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_channels,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 990				   sizeof(struct sun6i_pchan), GFP_KERNEL);
 991	if (!sdc->pchans)
 992		return -ENOMEM;
 993
 994	sdc->vchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_vchans,
 995				   sizeof(struct sun6i_vchan), GFP_KERNEL);
 996	if (!sdc->vchans)
 997		return -ENOMEM;
 998
 999	tasklet_init(&sdc->task, sun6i_dma_tasklet, (unsigned long)sdc);
1000
1001	for (i = 0; i < sdc->cfg->nr_max_channels; i++) {
1002		struct sun6i_pchan *pchan = &sdc->pchans[i];
1003
1004		pchan->idx = i;
1005		pchan->base = sdc->base + 0x100 + i * 0x40;
1006	}
1007
1008	for (i = 0; i < sdc->cfg->nr_max_vchans; i++) {
1009		struct sun6i_vchan *vchan = &sdc->vchans[i];
1010
1011		INIT_LIST_HEAD(&vchan->node);
1012		vchan->vc.desc_free = sun6i_dma_free_desc;
1013		vchan_init(&vchan->vc, &sdc->slave);
1014	}
1015
1016	ret = reset_control_deassert(sdc->rstc);
1017	if (ret) {
1018		dev_err(&pdev->dev, "Couldn't deassert the device from reset\n");
1019		goto err_chan_free;
1020	}
1021
1022	ret = clk_prepare_enable(sdc->clk);
1023	if (ret) {
1024		dev_err(&pdev->dev, "Couldn't enable the clock\n");
1025		goto err_reset_assert;
1026	}
1027
 
 
 
 
 
 
 
 
1028	ret = devm_request_irq(&pdev->dev, sdc->irq, sun6i_dma_interrupt, 0,
1029			       dev_name(&pdev->dev), sdc);
1030	if (ret) {
1031		dev_err(&pdev->dev, "Cannot request IRQ\n");
1032		goto err_clk_disable;
1033	}
1034
1035	ret = dma_async_device_register(&sdc->slave);
1036	if (ret) {
1037		dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
1038		goto err_irq_disable;
1039	}
1040
1041	ret = of_dma_controller_register(pdev->dev.of_node, sun6i_dma_of_xlate,
1042					 sdc);
1043	if (ret) {
1044		dev_err(&pdev->dev, "of_dma_controller_register failed\n");
1045		goto err_dma_unregister;
1046	}
1047
1048	/*
1049	 * sun8i variant requires us to toggle a dma gating register,
1050	 * as seen in Allwinner's SDK. This register is not documented
1051	 * in the A23 user manual.
1052	 */
1053	if (of_device_is_compatible(pdev->dev.of_node,
1054				    "allwinner,sun8i-a23-dma"))
1055		writel(SUN8I_DMA_GATE_ENABLE, sdc->base + SUN8I_DMA_GATE);
1056
1057	return 0;
1058
1059err_dma_unregister:
1060	dma_async_device_unregister(&sdc->slave);
1061err_irq_disable:
1062	sun6i_kill_tasklet(sdc);
 
 
1063err_clk_disable:
1064	clk_disable_unprepare(sdc->clk);
1065err_reset_assert:
1066	reset_control_assert(sdc->rstc);
1067err_chan_free:
1068	sun6i_dma_free(sdc);
1069	return ret;
1070}
1071
1072static int sun6i_dma_remove(struct platform_device *pdev)
1073{
1074	struct sun6i_dma_dev *sdc = platform_get_drvdata(pdev);
1075
1076	of_dma_controller_free(pdev->dev.of_node);
1077	dma_async_device_unregister(&sdc->slave);
1078
1079	sun6i_kill_tasklet(sdc);
1080
 
1081	clk_disable_unprepare(sdc->clk);
1082	reset_control_assert(sdc->rstc);
1083
1084	sun6i_dma_free(sdc);
1085
1086	return 0;
1087}
1088
1089static struct platform_driver sun6i_dma_driver = {
1090	.probe		= sun6i_dma_probe,
1091	.remove		= sun6i_dma_remove,
1092	.driver = {
1093		.name		= "sun6i-dma",
1094		.of_match_table	= sun6i_dma_match,
1095	},
1096};
1097module_platform_driver(sun6i_dma_driver);
1098
1099MODULE_DESCRIPTION("Allwinner A31 DMA Controller Driver");
1100MODULE_AUTHOR("Sugar <shuge@allwinnertech.com>");
1101MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1102MODULE_LICENSE("GPL");