Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (C) 2014 Emilio López
   4 * Emilio López <emilio@elopez.com.ar>
   5 */
   6
   7#include <linux/bitmap.h>
   8#include <linux/bitops.h>
   9#include <linux/clk.h>
  10#include <linux/dma-mapping.h>
  11#include <linux/dmaengine.h>
  12#include <linux/dmapool.h>
  13#include <linux/interrupt.h>
  14#include <linux/module.h>
  15#include <linux/of_dma.h>
  16#include <linux/platform_device.h>
  17#include <linux/slab.h>
  18#include <linux/spinlock.h>
  19
  20#include "virt-dma.h"
  21
  22/** Common macros to normal and dedicated DMA registers **/
  23
  24#define SUN4I_DMA_CFG_LOADING			BIT(31)
  25#define SUN4I_DMA_CFG_DST_DATA_WIDTH(width)	((width) << 25)
  26#define SUN4I_DMA_CFG_DST_BURST_LENGTH(len)	((len) << 23)
  27#define SUN4I_DMA_CFG_DST_ADDR_MODE(mode)	((mode) << 21)
  28#define SUN4I_DMA_CFG_DST_DRQ_TYPE(type)	((type) << 16)
  29#define SUN4I_DMA_CFG_SRC_DATA_WIDTH(width)	((width) << 9)
  30#define SUN4I_DMA_CFG_SRC_BURST_LENGTH(len)	((len) << 7)
  31#define SUN4I_DMA_CFG_SRC_ADDR_MODE(mode)	((mode) << 5)
  32#define SUN4I_DMA_CFG_SRC_DRQ_TYPE(type)	(type)
  33
  34/** Normal DMA register values **/
  35
  36/* Normal DMA source/destination data request type values */
  37#define SUN4I_NDMA_DRQ_TYPE_SDRAM		0x16
  38#define SUN4I_NDMA_DRQ_TYPE_LIMIT		(0x1F + 1)
  39
  40/** Normal DMA register layout **/
  41
  42/* Dedicated DMA source/destination address mode values */
  43#define SUN4I_NDMA_ADDR_MODE_LINEAR		0
  44#define SUN4I_NDMA_ADDR_MODE_IO			1
  45
  46/* Normal DMA configuration register layout */
  47#define SUN4I_NDMA_CFG_CONT_MODE		BIT(30)
  48#define SUN4I_NDMA_CFG_WAIT_STATE(n)		((n) << 27)
  49#define SUN4I_NDMA_CFG_DST_NON_SECURE		BIT(22)
  50#define SUN4I_NDMA_CFG_BYTE_COUNT_MODE_REMAIN	BIT(15)
  51#define SUN4I_NDMA_CFG_SRC_NON_SECURE		BIT(6)
  52
  53/** Dedicated DMA register values **/
  54
  55/* Dedicated DMA source/destination address mode values */
  56#define SUN4I_DDMA_ADDR_MODE_LINEAR		0
  57#define SUN4I_DDMA_ADDR_MODE_IO			1
  58#define SUN4I_DDMA_ADDR_MODE_HORIZONTAL_PAGE	2
  59#define SUN4I_DDMA_ADDR_MODE_VERTICAL_PAGE	3
  60
  61/* Dedicated DMA source/destination data request type values */
  62#define SUN4I_DDMA_DRQ_TYPE_SDRAM		0x1
  63#define SUN4I_DDMA_DRQ_TYPE_LIMIT		(0x1F + 1)
  64
  65/** Dedicated DMA register layout **/
  66
  67/* Dedicated DMA configuration register layout */
  68#define SUN4I_DDMA_CFG_BUSY			BIT(30)
  69#define SUN4I_DDMA_CFG_CONT_MODE		BIT(29)
  70#define SUN4I_DDMA_CFG_DST_NON_SECURE		BIT(28)
  71#define SUN4I_DDMA_CFG_BYTE_COUNT_MODE_REMAIN	BIT(15)
  72#define SUN4I_DDMA_CFG_SRC_NON_SECURE		BIT(12)
  73
  74/* Dedicated DMA parameter register layout */
  75#define SUN4I_DDMA_PARA_DST_DATA_BLK_SIZE(n)	(((n) - 1) << 24)
  76#define SUN4I_DDMA_PARA_DST_WAIT_CYCLES(n)	(((n) - 1) << 16)
  77#define SUN4I_DDMA_PARA_SRC_DATA_BLK_SIZE(n)	(((n) - 1) << 8)
  78#define SUN4I_DDMA_PARA_SRC_WAIT_CYCLES(n)	(((n) - 1) << 0)
  79
  80/** DMA register offsets **/
  81
  82/* General register offsets */
  83#define SUN4I_DMA_IRQ_ENABLE_REG		0x0
  84#define SUN4I_DMA_IRQ_PENDING_STATUS_REG	0x4
  85
  86/* Normal DMA register offsets */
  87#define SUN4I_NDMA_CHANNEL_REG_BASE(n)		(0x100 + (n) * 0x20)
  88#define SUN4I_NDMA_CFG_REG			0x0
  89#define SUN4I_NDMA_SRC_ADDR_REG			0x4
  90#define SUN4I_NDMA_DST_ADDR_REG		0x8
  91#define SUN4I_NDMA_BYTE_COUNT_REG		0xC
  92
  93/* Dedicated DMA register offsets */
  94#define SUN4I_DDMA_CHANNEL_REG_BASE(n)		(0x300 + (n) * 0x20)
  95#define SUN4I_DDMA_CFG_REG			0x0
  96#define SUN4I_DDMA_SRC_ADDR_REG			0x4
  97#define SUN4I_DDMA_DST_ADDR_REG		0x8
  98#define SUN4I_DDMA_BYTE_COUNT_REG		0xC
  99#define SUN4I_DDMA_PARA_REG			0x18
 100
 101/** DMA Driver **/
 102
 103/*
 104 * Normal DMA has 8 channels, and Dedicated DMA has another 8, so
 105 * that's 16 channels. As for endpoints, there's 29 and 21
 106 * respectively. Given that the Normal DMA endpoints (other than
 107 * SDRAM) can be used as tx/rx, we need 78 vchans in total
 108 */
 109#define SUN4I_NDMA_NR_MAX_CHANNELS	8
 110#define SUN4I_DDMA_NR_MAX_CHANNELS	8
 111#define SUN4I_DMA_NR_MAX_CHANNELS					\
 112	(SUN4I_NDMA_NR_MAX_CHANNELS + SUN4I_DDMA_NR_MAX_CHANNELS)
 113#define SUN4I_NDMA_NR_MAX_VCHANS	(29 * 2 - 1)
 114#define SUN4I_DDMA_NR_MAX_VCHANS	21
 115#define SUN4I_DMA_NR_MAX_VCHANS						\
 116	(SUN4I_NDMA_NR_MAX_VCHANS + SUN4I_DDMA_NR_MAX_VCHANS)
 117
 118/* This set of SUN4I_DDMA timing parameters were found experimentally while
 119 * working with the SPI driver and seem to make it behave correctly */
 120#define SUN4I_DDMA_MAGIC_SPI_PARAMETERS \
 121	(SUN4I_DDMA_PARA_DST_DATA_BLK_SIZE(1) |			\
 122	 SUN4I_DDMA_PARA_SRC_DATA_BLK_SIZE(1) |				\
 123	 SUN4I_DDMA_PARA_DST_WAIT_CYCLES(2) |				\
 124	 SUN4I_DDMA_PARA_SRC_WAIT_CYCLES(2))
 125
 126/*
 127 * Normal DMA supports individual transfers (segments) up to 128k.
 128 * Dedicated DMA supports transfers up to 16M. We can only report
 129 * one size limit, so we have to use the smaller value.
 130 */
 131#define SUN4I_NDMA_MAX_SEG_SIZE		SZ_128K
 132#define SUN4I_DDMA_MAX_SEG_SIZE		SZ_16M
 133#define SUN4I_DMA_MAX_SEG_SIZE		SUN4I_NDMA_MAX_SEG_SIZE
 134
 135struct sun4i_dma_pchan {
 136	/* Register base of channel */
 137	void __iomem			*base;
 138	/* vchan currently being serviced */
 139	struct sun4i_dma_vchan		*vchan;
 140	/* Is this a dedicated pchan? */
 141	int				is_dedicated;
 142};
 143
 144struct sun4i_dma_vchan {
 145	struct virt_dma_chan		vc;
 146	struct dma_slave_config		cfg;
 147	struct sun4i_dma_pchan		*pchan;
 148	struct sun4i_dma_promise	*processing;
 149	struct sun4i_dma_contract	*contract;
 150	u8				endpoint;
 151	int				is_dedicated;
 152};
 153
 154struct sun4i_dma_promise {
 155	u32				cfg;
 156	u32				para;
 157	dma_addr_t			src;
 158	dma_addr_t			dst;
 159	size_t				len;
 160	struct list_head		list;
 161};
 162
 163/* A contract is a set of promises */
 164struct sun4i_dma_contract {
 165	struct virt_dma_desc		vd;
 166	struct list_head		demands;
 167	struct list_head		completed_demands;
 168	bool				is_cyclic : 1;
 169	bool				use_half_int : 1;
 170};
 171
 172struct sun4i_dma_dev {
 173	DECLARE_BITMAP(pchans_used, SUN4I_DMA_NR_MAX_CHANNELS);
 174	struct dma_device		slave;
 175	struct sun4i_dma_pchan		*pchans;
 176	struct sun4i_dma_vchan		*vchans;
 177	void __iomem			*base;
 178	struct clk			*clk;
 179	int				irq;
 180	spinlock_t			lock;
 181};
 182
 183static struct sun4i_dma_dev *to_sun4i_dma_dev(struct dma_device *dev)
 184{
 185	return container_of(dev, struct sun4i_dma_dev, slave);
 186}
 187
 188static struct sun4i_dma_vchan *to_sun4i_dma_vchan(struct dma_chan *chan)
 189{
 190	return container_of(chan, struct sun4i_dma_vchan, vc.chan);
 191}
 192
 193static struct sun4i_dma_contract *to_sun4i_dma_contract(struct virt_dma_desc *vd)
 194{
 195	return container_of(vd, struct sun4i_dma_contract, vd);
 196}
 197
 198static struct device *chan2dev(struct dma_chan *chan)
 199{
 200	return &chan->dev->device;
 201}
 202
 203static int convert_burst(u32 maxburst)
 204{
 205	if (maxburst > 8)
 206		return -EINVAL;
 207
 208	/* 1 -> 0, 4 -> 1, 8 -> 2 */
 209	return (maxburst >> 2);
 210}
 211
 212static int convert_buswidth(enum dma_slave_buswidth addr_width)
 213{
 214	if (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES)
 215		return -EINVAL;
 216
 217	/* 8 (1 byte) -> 0, 16 (2 bytes) -> 1, 32 (4 bytes) -> 2 */
 218	return (addr_width >> 1);
 219}
 220
 221static void sun4i_dma_free_chan_resources(struct dma_chan *chan)
 222{
 223	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 224
 225	vchan_free_chan_resources(&vchan->vc);
 226}
 227
 228static struct sun4i_dma_pchan *find_and_use_pchan(struct sun4i_dma_dev *priv,
 229						  struct sun4i_dma_vchan *vchan)
 230{
 231	struct sun4i_dma_pchan *pchan = NULL, *pchans = priv->pchans;
 232	unsigned long flags;
 233	int i, max;
 234
 235	/*
 236	 * pchans 0-SUN4I_NDMA_NR_MAX_CHANNELS are normal, and
 237	 * SUN4I_NDMA_NR_MAX_CHANNELS+ are dedicated ones
 238	 */
 239	if (vchan->is_dedicated) {
 240		i = SUN4I_NDMA_NR_MAX_CHANNELS;
 241		max = SUN4I_DMA_NR_MAX_CHANNELS;
 242	} else {
 243		i = 0;
 244		max = SUN4I_NDMA_NR_MAX_CHANNELS;
 245	}
 246
 247	spin_lock_irqsave(&priv->lock, flags);
 248	for_each_clear_bit_from(i, priv->pchans_used, max) {
 249		pchan = &pchans[i];
 250		pchan->vchan = vchan;
 251		set_bit(i, priv->pchans_used);
 252		break;
 253	}
 254	spin_unlock_irqrestore(&priv->lock, flags);
 255
 256	return pchan;
 257}
 258
 259static void release_pchan(struct sun4i_dma_dev *priv,
 260			  struct sun4i_dma_pchan *pchan)
 261{
 262	unsigned long flags;
 263	int nr = pchan - priv->pchans;
 264
 265	spin_lock_irqsave(&priv->lock, flags);
 266
 267	pchan->vchan = NULL;
 268	clear_bit(nr, priv->pchans_used);
 269
 270	spin_unlock_irqrestore(&priv->lock, flags);
 271}
 272
 273static void configure_pchan(struct sun4i_dma_pchan *pchan,
 274			    struct sun4i_dma_promise *d)
 275{
 276	/*
 277	 * Configure addresses and misc parameters depending on type
 278	 * SUN4I_DDMA has an extra field with timing parameters
 279	 */
 280	if (pchan->is_dedicated) {
 281		writel_relaxed(d->src, pchan->base + SUN4I_DDMA_SRC_ADDR_REG);
 282		writel_relaxed(d->dst, pchan->base + SUN4I_DDMA_DST_ADDR_REG);
 283		writel_relaxed(d->len, pchan->base + SUN4I_DDMA_BYTE_COUNT_REG);
 284		writel_relaxed(d->para, pchan->base + SUN4I_DDMA_PARA_REG);
 285		writel_relaxed(d->cfg, pchan->base + SUN4I_DDMA_CFG_REG);
 286	} else {
 287		writel_relaxed(d->src, pchan->base + SUN4I_NDMA_SRC_ADDR_REG);
 288		writel_relaxed(d->dst, pchan->base + SUN4I_NDMA_DST_ADDR_REG);
 289		writel_relaxed(d->len, pchan->base + SUN4I_NDMA_BYTE_COUNT_REG);
 290		writel_relaxed(d->cfg, pchan->base + SUN4I_NDMA_CFG_REG);
 291	}
 292}
 293
 294static void set_pchan_interrupt(struct sun4i_dma_dev *priv,
 295				struct sun4i_dma_pchan *pchan,
 296				int half, int end)
 297{
 298	u32 reg;
 299	int pchan_number = pchan - priv->pchans;
 300	unsigned long flags;
 301
 302	spin_lock_irqsave(&priv->lock, flags);
 303
 304	reg = readl_relaxed(priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
 305
 306	if (half)
 307		reg |= BIT(pchan_number * 2);
 308	else
 309		reg &= ~BIT(pchan_number * 2);
 310
 311	if (end)
 312		reg |= BIT(pchan_number * 2 + 1);
 313	else
 314		reg &= ~BIT(pchan_number * 2 + 1);
 315
 316	writel_relaxed(reg, priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
 317
 318	spin_unlock_irqrestore(&priv->lock, flags);
 319}
 320
 321/*
 322 * Execute pending operations on a vchan
 323 *
 324 * When given a vchan, this function will try to acquire a suitable
 325 * pchan and, if successful, will configure it to fulfill a promise
 326 * from the next pending contract.
 327 *
 328 * This function must be called with &vchan->vc.lock held.
 329 */
 330static int __execute_vchan_pending(struct sun4i_dma_dev *priv,
 331				   struct sun4i_dma_vchan *vchan)
 332{
 333	struct sun4i_dma_promise *promise = NULL;
 334	struct sun4i_dma_contract *contract = NULL;
 335	struct sun4i_dma_pchan *pchan;
 336	struct virt_dma_desc *vd;
 337	int ret;
 338
 339	lockdep_assert_held(&vchan->vc.lock);
 340
 341	/* We need a pchan to do anything, so secure one if available */
 342	pchan = find_and_use_pchan(priv, vchan);
 343	if (!pchan)
 344		return -EBUSY;
 345
 346	/*
 347	 * Channel endpoints must not be repeated, so if this vchan
 348	 * has already submitted some work, we can't do anything else
 349	 */
 350	if (vchan->processing) {
 351		dev_dbg(chan2dev(&vchan->vc.chan),
 352			"processing something to this endpoint already\n");
 353		ret = -EBUSY;
 354		goto release_pchan;
 355	}
 356
 357	do {
 358		/* Figure out which contract we're working with today */
 359		vd = vchan_next_desc(&vchan->vc);
 360		if (!vd) {
 361			dev_dbg(chan2dev(&vchan->vc.chan),
 362				"No pending contract found");
 363			ret = 0;
 364			goto release_pchan;
 365		}
 366
 367		contract = to_sun4i_dma_contract(vd);
 368		if (list_empty(&contract->demands)) {
 369			/* The contract has been completed so mark it as such */
 370			list_del(&contract->vd.node);
 371			vchan_cookie_complete(&contract->vd);
 372			dev_dbg(chan2dev(&vchan->vc.chan),
 373				"Empty contract found and marked complete");
 374		}
 375	} while (list_empty(&contract->demands));
 376
 377	/* Now find out what we need to do */
 378	promise = list_first_entry(&contract->demands,
 379				   struct sun4i_dma_promise, list);
 380	vchan->processing = promise;
 381
 382	/* ... and make it reality */
 383	if (promise) {
 384		vchan->contract = contract;
 385		vchan->pchan = pchan;
 386		set_pchan_interrupt(priv, pchan, contract->use_half_int, 1);
 387		configure_pchan(pchan, promise);
 388	}
 389
 390	return 0;
 391
 392release_pchan:
 393	release_pchan(priv, pchan);
 394	return ret;
 395}
 396
 397static int sanitize_config(struct dma_slave_config *sconfig,
 398			   enum dma_transfer_direction direction)
 399{
 400	switch (direction) {
 401	case DMA_MEM_TO_DEV:
 402		if ((sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) ||
 403		    !sconfig->dst_maxburst)
 404			return -EINVAL;
 405
 406		if (sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
 407			sconfig->src_addr_width = sconfig->dst_addr_width;
 408
 409		if (!sconfig->src_maxburst)
 410			sconfig->src_maxburst = sconfig->dst_maxburst;
 411
 412		break;
 413
 414	case DMA_DEV_TO_MEM:
 415		if ((sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) ||
 416		    !sconfig->src_maxburst)
 417			return -EINVAL;
 418
 419		if (sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
 420			sconfig->dst_addr_width = sconfig->src_addr_width;
 421
 422		if (!sconfig->dst_maxburst)
 423			sconfig->dst_maxburst = sconfig->src_maxburst;
 424
 425		break;
 426	default:
 427		return 0;
 428	}
 429
 430	return 0;
 431}
 432
 433/*
 434 * Generate a promise, to be used in a normal DMA contract.
 435 *
 436 * A NDMA promise contains all the information required to program the
 437 * normal part of the DMA Engine and get data copied. A non-executed
 438 * promise will live in the demands list on a contract. Once it has been
 439 * completed, it will be moved to the completed demands list for later freeing.
 440 * All linked promises will be freed when the corresponding contract is freed
 441 */
 442static struct sun4i_dma_promise *
 443generate_ndma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
 444		      size_t len, struct dma_slave_config *sconfig,
 445		      enum dma_transfer_direction direction)
 446{
 447	struct sun4i_dma_promise *promise;
 448	int ret;
 449
 450	ret = sanitize_config(sconfig, direction);
 451	if (ret)
 452		return NULL;
 453
 454	promise = kzalloc(sizeof(*promise), GFP_NOWAIT);
 455	if (!promise)
 456		return NULL;
 457
 458	promise->src = src;
 459	promise->dst = dest;
 460	promise->len = len;
 461	promise->cfg = SUN4I_DMA_CFG_LOADING |
 462		SUN4I_NDMA_CFG_BYTE_COUNT_MODE_REMAIN;
 463
 464	dev_dbg(chan2dev(chan),
 465		"src burst %d, dst burst %d, src buswidth %d, dst buswidth %d",
 466		sconfig->src_maxburst, sconfig->dst_maxburst,
 467		sconfig->src_addr_width, sconfig->dst_addr_width);
 468
 469	/* Source burst */
 470	ret = convert_burst(sconfig->src_maxburst);
 471	if (ret < 0)
 472		goto fail;
 473	promise->cfg |= SUN4I_DMA_CFG_SRC_BURST_LENGTH(ret);
 474
 475	/* Destination burst */
 476	ret = convert_burst(sconfig->dst_maxburst);
 477	if (ret < 0)
 478		goto fail;
 479	promise->cfg |= SUN4I_DMA_CFG_DST_BURST_LENGTH(ret);
 480
 481	/* Source bus width */
 482	ret = convert_buswidth(sconfig->src_addr_width);
 483	if (ret < 0)
 484		goto fail;
 485	promise->cfg |= SUN4I_DMA_CFG_SRC_DATA_WIDTH(ret);
 486
 487	/* Destination bus width */
 488	ret = convert_buswidth(sconfig->dst_addr_width);
 489	if (ret < 0)
 490		goto fail;
 491	promise->cfg |= SUN4I_DMA_CFG_DST_DATA_WIDTH(ret);
 492
 493	return promise;
 494
 495fail:
 496	kfree(promise);
 497	return NULL;
 498}
 499
 500/*
 501 * Generate a promise, to be used in a dedicated DMA contract.
 502 *
 503 * A DDMA promise contains all the information required to program the
 504 * Dedicated part of the DMA Engine and get data copied. A non-executed
 505 * promise will live in the demands list on a contract. Once it has been
 506 * completed, it will be moved to the completed demands list for later freeing.
 507 * All linked promises will be freed when the corresponding contract is freed
 508 */
 509static struct sun4i_dma_promise *
 510generate_ddma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
 511		      size_t len, struct dma_slave_config *sconfig)
 512{
 513	struct sun4i_dma_promise *promise;
 514	int ret;
 515
 516	promise = kzalloc(sizeof(*promise), GFP_NOWAIT);
 517	if (!promise)
 518		return NULL;
 519
 520	promise->src = src;
 521	promise->dst = dest;
 522	promise->len = len;
 523	promise->cfg = SUN4I_DMA_CFG_LOADING |
 524		SUN4I_DDMA_CFG_BYTE_COUNT_MODE_REMAIN;
 525
 526	/* Source burst */
 527	ret = convert_burst(sconfig->src_maxburst);
 528	if (ret < 0)
 529		goto fail;
 530	promise->cfg |= SUN4I_DMA_CFG_SRC_BURST_LENGTH(ret);
 531
 532	/* Destination burst */
 533	ret = convert_burst(sconfig->dst_maxburst);
 534	if (ret < 0)
 535		goto fail;
 536	promise->cfg |= SUN4I_DMA_CFG_DST_BURST_LENGTH(ret);
 537
 538	/* Source bus width */
 539	ret = convert_buswidth(sconfig->src_addr_width);
 540	if (ret < 0)
 541		goto fail;
 542	promise->cfg |= SUN4I_DMA_CFG_SRC_DATA_WIDTH(ret);
 543
 544	/* Destination bus width */
 545	ret = convert_buswidth(sconfig->dst_addr_width);
 546	if (ret < 0)
 547		goto fail;
 548	promise->cfg |= SUN4I_DMA_CFG_DST_DATA_WIDTH(ret);
 549
 550	return promise;
 551
 552fail:
 553	kfree(promise);
 554	return NULL;
 555}
 556
 557/*
 558 * Generate a contract
 559 *
 560 * Contracts function as DMA descriptors. As our hardware does not support
 561 * linked lists, we need to implement SG via software. We use a contract
 562 * to hold all the pieces of the request and process them serially one
 563 * after another. Each piece is represented as a promise.
 564 */
 565static struct sun4i_dma_contract *generate_dma_contract(void)
 566{
 567	struct sun4i_dma_contract *contract;
 568
 569	contract = kzalloc(sizeof(*contract), GFP_NOWAIT);
 570	if (!contract)
 571		return NULL;
 572
 573	INIT_LIST_HEAD(&contract->demands);
 574	INIT_LIST_HEAD(&contract->completed_demands);
 575
 576	return contract;
 577}
 578
 579/*
 580 * Get next promise on a cyclic transfer
 581 *
 582 * Cyclic contracts contain a series of promises which are executed on a
 583 * loop. This function returns the next promise from a cyclic contract,
 584 * so it can be programmed into the hardware.
 585 */
 586static struct sun4i_dma_promise *
 587get_next_cyclic_promise(struct sun4i_dma_contract *contract)
 588{
 589	struct sun4i_dma_promise *promise;
 590
 591	promise = list_first_entry_or_null(&contract->demands,
 592					   struct sun4i_dma_promise, list);
 593	if (!promise) {
 594		list_splice_init(&contract->completed_demands,
 595				 &contract->demands);
 596		promise = list_first_entry(&contract->demands,
 597					   struct sun4i_dma_promise, list);
 598	}
 599
 600	return promise;
 601}
 602
 603/*
 604 * Free a contract and all its associated promises
 605 */
 606static void sun4i_dma_free_contract(struct virt_dma_desc *vd)
 607{
 608	struct sun4i_dma_contract *contract = to_sun4i_dma_contract(vd);
 609	struct sun4i_dma_promise *promise, *tmp;
 610
 611	/* Free all the demands and completed demands */
 612	list_for_each_entry_safe(promise, tmp, &contract->demands, list)
 613		kfree(promise);
 614
 615	list_for_each_entry_safe(promise, tmp, &contract->completed_demands, list)
 616		kfree(promise);
 617
 618	kfree(contract);
 619}
 620
 621static struct dma_async_tx_descriptor *
 622sun4i_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
 623			  dma_addr_t src, size_t len, unsigned long flags)
 624{
 625	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 626	struct dma_slave_config *sconfig = &vchan->cfg;
 627	struct sun4i_dma_promise *promise;
 628	struct sun4i_dma_contract *contract;
 629
 630	contract = generate_dma_contract();
 631	if (!contract)
 632		return NULL;
 633
 634	/*
 635	 * We can only do the copy to bus aligned addresses, so
 636	 * choose the best one so we get decent performance. We also
 637	 * maximize the burst size for this same reason.
 638	 */
 639	sconfig->src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
 640	sconfig->dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
 641	sconfig->src_maxburst = 8;
 642	sconfig->dst_maxburst = 8;
 643
 644	if (vchan->is_dedicated)
 645		promise = generate_ddma_promise(chan, src, dest, len, sconfig);
 646	else
 647		promise = generate_ndma_promise(chan, src, dest, len, sconfig,
 648						DMA_MEM_TO_MEM);
 649
 650	if (!promise) {
 651		kfree(contract);
 652		return NULL;
 653	}
 654
 655	/* Configure memcpy mode */
 656	if (vchan->is_dedicated) {
 657		promise->cfg |= SUN4I_DMA_CFG_SRC_DRQ_TYPE(SUN4I_DDMA_DRQ_TYPE_SDRAM) |
 658				SUN4I_DMA_CFG_DST_DRQ_TYPE(SUN4I_DDMA_DRQ_TYPE_SDRAM);
 659	} else {
 660		promise->cfg |= SUN4I_DMA_CFG_SRC_DRQ_TYPE(SUN4I_NDMA_DRQ_TYPE_SDRAM) |
 661				SUN4I_DMA_CFG_DST_DRQ_TYPE(SUN4I_NDMA_DRQ_TYPE_SDRAM);
 662	}
 663
 664	/* Fill the contract with our only promise */
 665	list_add_tail(&promise->list, &contract->demands);
 666
 667	/* And add it to the vchan */
 668	return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
 669}
 670
 671static struct dma_async_tx_descriptor *
 672sun4i_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf, size_t len,
 673			  size_t period_len, enum dma_transfer_direction dir,
 674			  unsigned long flags)
 675{
 676	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 677	struct dma_slave_config *sconfig = &vchan->cfg;
 678	struct sun4i_dma_promise *promise;
 679	struct sun4i_dma_contract *contract;
 680	dma_addr_t src, dest;
 681	u32 endpoints;
 682	int nr_periods, offset, plength, i;
 683	u8 ram_type, io_mode, linear_mode;
 684
 685	if (!is_slave_direction(dir)) {
 686		dev_err(chan2dev(chan), "Invalid DMA direction\n");
 687		return NULL;
 688	}
 689
 690	contract = generate_dma_contract();
 691	if (!contract)
 692		return NULL;
 693
 694	contract->is_cyclic = 1;
 695
 696	if (vchan->is_dedicated) {
 697		io_mode = SUN4I_DDMA_ADDR_MODE_IO;
 698		linear_mode = SUN4I_DDMA_ADDR_MODE_LINEAR;
 699		ram_type = SUN4I_DDMA_DRQ_TYPE_SDRAM;
 700	} else {
 701		io_mode = SUN4I_NDMA_ADDR_MODE_IO;
 702		linear_mode = SUN4I_NDMA_ADDR_MODE_LINEAR;
 703		ram_type = SUN4I_NDMA_DRQ_TYPE_SDRAM;
 704	}
 705
 706	if (dir == DMA_MEM_TO_DEV) {
 707		src = buf;
 708		dest = sconfig->dst_addr;
 709		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(vchan->endpoint) |
 710			    SUN4I_DMA_CFG_DST_ADDR_MODE(io_mode) |
 711			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(ram_type) |
 712			    SUN4I_DMA_CFG_SRC_ADDR_MODE(linear_mode);
 713	} else {
 714		src = sconfig->src_addr;
 715		dest = buf;
 716		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(ram_type) |
 717			    SUN4I_DMA_CFG_DST_ADDR_MODE(linear_mode) |
 718			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
 719			    SUN4I_DMA_CFG_SRC_ADDR_MODE(io_mode);
 720	}
 721
 722	/*
 723	 * We will be using half done interrupts to make two periods
 724	 * out of a promise, so we need to program the DMA engine less
 725	 * often
 726	 */
 727
 728	/*
 729	 * The engine can interrupt on half-transfer, so we can use
 730	 * this feature to program the engine half as often as if we
 731	 * didn't use it (keep in mind the hardware doesn't support
 732	 * linked lists).
 733	 *
 734	 * Say you have a set of periods (| marks the start/end, I for
 735	 * interrupt, P for programming the engine to do a new
 736	 * transfer), the easy but slow way would be to do
 737	 *
 738	 *  |---|---|---|---| (periods / promises)
 739	 *  P  I,P I,P I,P  I
 740	 *
 741	 * Using half transfer interrupts you can do
 742	 *
 743	 *  |-------|-------| (promises as configured on hw)
 744	 *  |---|---|---|---| (periods)
 745	 *  P   I  I,P  I   I
 746	 *
 747	 * Which requires half the engine programming for the same
 748	 * functionality.
 749	 *
 750	 * This only works if two periods fit in a single promise. That will
 751	 * always be the case for dedicated DMA, where the hardware has a much
 752	 * larger maximum transfer size than advertised to clients.
 753	 */
 754	if (vchan->is_dedicated || period_len <= SUN4I_NDMA_MAX_SEG_SIZE / 2) {
 755		period_len *= 2;
 756		contract->use_half_int = 1;
 757	}
 758
 759	nr_periods = DIV_ROUND_UP(len, period_len);
 760	for (i = 0; i < nr_periods; i++) {
 761		/* Calculate the offset in the buffer and the length needed */
 762		offset = i * period_len;
 763		plength = min((len - offset), period_len);
 764		if (dir == DMA_MEM_TO_DEV)
 765			src = buf + offset;
 766		else
 767			dest = buf + offset;
 768
 769		/* Make the promise */
 770		if (vchan->is_dedicated)
 771			promise = generate_ddma_promise(chan, src, dest,
 772							plength, sconfig);
 773		else
 774			promise = generate_ndma_promise(chan, src, dest,
 775							plength, sconfig, dir);
 776
 777		if (!promise) {
 778			/* TODO: should we free everything? */
 779			return NULL;
 780		}
 781		promise->cfg |= endpoints;
 782
 783		/* Then add it to the contract */
 784		list_add_tail(&promise->list, &contract->demands);
 785	}
 786
 787	/* And add it to the vchan */
 788	return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
 789}
 790
 791static struct dma_async_tx_descriptor *
 792sun4i_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
 793			unsigned int sg_len, enum dma_transfer_direction dir,
 794			unsigned long flags, void *context)
 795{
 796	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 797	struct dma_slave_config *sconfig = &vchan->cfg;
 798	struct sun4i_dma_promise *promise;
 799	struct sun4i_dma_contract *contract;
 800	u8 ram_type, io_mode, linear_mode;
 801	struct scatterlist *sg;
 802	dma_addr_t srcaddr, dstaddr;
 803	u32 endpoints, para;
 804	int i;
 805
 806	if (!sgl)
 807		return NULL;
 808
 809	if (!is_slave_direction(dir)) {
 810		dev_err(chan2dev(chan), "Invalid DMA direction\n");
 811		return NULL;
 812	}
 813
 814	contract = generate_dma_contract();
 815	if (!contract)
 816		return NULL;
 817
 818	if (vchan->is_dedicated) {
 819		io_mode = SUN4I_DDMA_ADDR_MODE_IO;
 820		linear_mode = SUN4I_DDMA_ADDR_MODE_LINEAR;
 821		ram_type = SUN4I_DDMA_DRQ_TYPE_SDRAM;
 822	} else {
 823		io_mode = SUN4I_NDMA_ADDR_MODE_IO;
 824		linear_mode = SUN4I_NDMA_ADDR_MODE_LINEAR;
 825		ram_type = SUN4I_NDMA_DRQ_TYPE_SDRAM;
 826	}
 827
 828	if (dir == DMA_MEM_TO_DEV)
 829		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(vchan->endpoint) |
 830			    SUN4I_DMA_CFG_DST_ADDR_MODE(io_mode) |
 831			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(ram_type) |
 832			    SUN4I_DMA_CFG_SRC_ADDR_MODE(linear_mode);
 833	else
 834		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(ram_type) |
 835			    SUN4I_DMA_CFG_DST_ADDR_MODE(linear_mode) |
 836			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
 837			    SUN4I_DMA_CFG_SRC_ADDR_MODE(io_mode);
 838
 839	for_each_sg(sgl, sg, sg_len, i) {
 840		/* Figure out addresses */
 841		if (dir == DMA_MEM_TO_DEV) {
 842			srcaddr = sg_dma_address(sg);
 843			dstaddr = sconfig->dst_addr;
 844		} else {
 845			srcaddr = sconfig->src_addr;
 846			dstaddr = sg_dma_address(sg);
 847		}
 848
 849		/*
 850		 * These are the magic DMA engine timings that keep SPI going.
 851		 * I haven't seen any interface on DMAEngine to configure
 852		 * timings, and so far they seem to work for everything we
 853		 * support, so I've kept them here. I don't know if other
 854		 * devices need different timings because, as usual, we only
 855		 * have the "para" bitfield meanings, but no comment on what
 856		 * the values should be when doing a certain operation :|
 857		 */
 858		para = SUN4I_DDMA_MAGIC_SPI_PARAMETERS;
 859
 860		/* And make a suitable promise */
 861		if (vchan->is_dedicated)
 862			promise = generate_ddma_promise(chan, srcaddr, dstaddr,
 863							sg_dma_len(sg),
 864							sconfig);
 865		else
 866			promise = generate_ndma_promise(chan, srcaddr, dstaddr,
 867							sg_dma_len(sg),
 868							sconfig, dir);
 869
 870		if (!promise)
 871			return NULL; /* TODO: should we free everything? */
 872
 873		promise->cfg |= endpoints;
 874		promise->para = para;
 875
 876		/* Then add it to the contract */
 877		list_add_tail(&promise->list, &contract->demands);
 878	}
 879
 880	/*
 881	 * Once we've got all the promises ready, add the contract
 882	 * to the pending list on the vchan
 883	 */
 884	return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
 885}
 886
 887static int sun4i_dma_terminate_all(struct dma_chan *chan)
 888{
 889	struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
 890	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 891	struct sun4i_dma_pchan *pchan = vchan->pchan;
 892	LIST_HEAD(head);
 893	unsigned long flags;
 894
 895	spin_lock_irqsave(&vchan->vc.lock, flags);
 896	vchan_get_all_descriptors(&vchan->vc, &head);
 897	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 898
 899	/*
 900	 * Clearing the configuration register will halt the pchan. Interrupts
 901	 * may still trigger, so don't forget to disable them.
 902	 */
 903	if (pchan) {
 904		if (pchan->is_dedicated)
 905			writel(0, pchan->base + SUN4I_DDMA_CFG_REG);
 906		else
 907			writel(0, pchan->base + SUN4I_NDMA_CFG_REG);
 908		set_pchan_interrupt(priv, pchan, 0, 0);
 909		release_pchan(priv, pchan);
 910	}
 911
 912	spin_lock_irqsave(&vchan->vc.lock, flags);
 913	/* Clear these so the vchan is usable again */
 914	vchan->processing = NULL;
 915	vchan->pchan = NULL;
 916	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 917
 918	vchan_dma_desc_free_list(&vchan->vc, &head);
 919
 920	return 0;
 921}
 922
 923static int sun4i_dma_config(struct dma_chan *chan,
 924			    struct dma_slave_config *config)
 925{
 926	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 927
 928	memcpy(&vchan->cfg, config, sizeof(*config));
 929
 930	return 0;
 931}
 932
 933static struct dma_chan *sun4i_dma_of_xlate(struct of_phandle_args *dma_spec,
 934					   struct of_dma *ofdma)
 935{
 936	struct sun4i_dma_dev *priv = ofdma->of_dma_data;
 937	struct sun4i_dma_vchan *vchan;
 938	struct dma_chan *chan;
 939	u8 is_dedicated = dma_spec->args[0];
 940	u8 endpoint = dma_spec->args[1];
 941
 942	/* Check if type is Normal or Dedicated */
 943	if (is_dedicated != 0 && is_dedicated != 1)
 944		return NULL;
 945
 946	/* Make sure the endpoint looks sane */
 947	if ((is_dedicated && endpoint >= SUN4I_DDMA_DRQ_TYPE_LIMIT) ||
 948	    (!is_dedicated && endpoint >= SUN4I_NDMA_DRQ_TYPE_LIMIT))
 949		return NULL;
 950
 951	chan = dma_get_any_slave_channel(&priv->slave);
 952	if (!chan)
 953		return NULL;
 954
 955	/* Assign the endpoint to the vchan */
 956	vchan = to_sun4i_dma_vchan(chan);
 957	vchan->is_dedicated = is_dedicated;
 958	vchan->endpoint = endpoint;
 959
 960	return chan;
 961}
 962
 963static enum dma_status sun4i_dma_tx_status(struct dma_chan *chan,
 964					   dma_cookie_t cookie,
 965					   struct dma_tx_state *state)
 966{
 967	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 968	struct sun4i_dma_pchan *pchan = vchan->pchan;
 969	struct sun4i_dma_contract *contract;
 970	struct sun4i_dma_promise *promise;
 971	struct virt_dma_desc *vd;
 972	unsigned long flags;
 973	enum dma_status ret;
 974	size_t bytes = 0;
 975
 976	ret = dma_cookie_status(chan, cookie, state);
 977	if (!state || (ret == DMA_COMPLETE))
 978		return ret;
 979
 980	spin_lock_irqsave(&vchan->vc.lock, flags);
 981	vd = vchan_find_desc(&vchan->vc, cookie);
 982	if (!vd)
 983		goto exit;
 984	contract = to_sun4i_dma_contract(vd);
 985
 986	list_for_each_entry(promise, &contract->demands, list)
 987		bytes += promise->len;
 988
 989	/*
 990	 * The hardware is configured to return the remaining byte
 991	 * quantity. If possible, replace the first listed element's
 992	 * full size with the actual remaining amount
 993	 */
 994	promise = list_first_entry_or_null(&contract->demands,
 995					   struct sun4i_dma_promise, list);
 996	if (promise && pchan) {
 997		bytes -= promise->len;
 998		if (pchan->is_dedicated)
 999			bytes += readl(pchan->base + SUN4I_DDMA_BYTE_COUNT_REG);
1000		else
1001			bytes += readl(pchan->base + SUN4I_NDMA_BYTE_COUNT_REG);
1002	}
1003
1004exit:
1005
1006	dma_set_residue(state, bytes);
1007	spin_unlock_irqrestore(&vchan->vc.lock, flags);
1008
1009	return ret;
1010}
1011
1012static void sun4i_dma_issue_pending(struct dma_chan *chan)
1013{
1014	struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
1015	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
1016	unsigned long flags;
1017
1018	spin_lock_irqsave(&vchan->vc.lock, flags);
1019
1020	/*
1021	 * If there are pending transactions for this vchan, push one of
1022	 * them into the engine to get the ball rolling.
1023	 */
1024	if (vchan_issue_pending(&vchan->vc))
1025		__execute_vchan_pending(priv, vchan);
1026
1027	spin_unlock_irqrestore(&vchan->vc.lock, flags);
1028}
1029
1030static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
1031{
1032	struct sun4i_dma_dev *priv = dev_id;
1033	struct sun4i_dma_pchan *pchans = priv->pchans, *pchan;
1034	struct sun4i_dma_vchan *vchan;
1035	struct sun4i_dma_contract *contract;
1036	struct sun4i_dma_promise *promise;
1037	unsigned long pendirq, irqs, disableirqs;
1038	int bit, i, free_room, allow_mitigation = 1;
1039
1040	pendirq = readl_relaxed(priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1041
1042handle_pending:
1043
1044	disableirqs = 0;
1045	free_room = 0;
1046
1047	for_each_set_bit(bit, &pendirq, 32) {
1048		pchan = &pchans[bit >> 1];
1049		vchan = pchan->vchan;
1050		if (!vchan) /* a terminated channel may still interrupt */
1051			continue;
1052		contract = vchan->contract;
1053
1054		/*
1055		 * Disable the IRQ and free the pchan if it's an end
1056		 * interrupt (odd bit)
1057		 */
1058		if (bit & 1) {
1059			spin_lock(&vchan->vc.lock);
1060
1061			/*
1062			 * Move the promise into the completed list now that
1063			 * we're done with it
1064			 */
1065			list_move_tail(&vchan->processing->list,
1066				       &contract->completed_demands);
 
1067
1068			/*
1069			 * Cyclic DMA transfers are special:
1070			 * - There's always something we can dispatch
1071			 * - We need to run the callback
1072			 * - Latency is very important, as this is used by audio
1073			 * We therefore just cycle through the list and dispatch
1074			 * whatever we have here, reusing the pchan. There's
1075			 * no need to run the thread after this.
1076			 *
1077			 * For non-cyclic transfers we need to look around,
1078			 * so we can program some more work, or notify the
1079			 * client that their transfers have been completed.
1080			 */
1081			if (contract->is_cyclic) {
1082				promise = get_next_cyclic_promise(contract);
1083				vchan->processing = promise;
1084				configure_pchan(pchan, promise);
1085				vchan_cyclic_callback(&contract->vd);
1086			} else {
1087				vchan->processing = NULL;
1088				vchan->pchan = NULL;
1089
1090				free_room = 1;
1091				disableirqs |= BIT(bit);
1092				release_pchan(priv, pchan);
1093			}
1094
1095			spin_unlock(&vchan->vc.lock);
1096		} else {
1097			/* Half done interrupt */
1098			if (contract->is_cyclic)
1099				vchan_cyclic_callback(&contract->vd);
1100			else
1101				disableirqs |= BIT(bit);
1102		}
1103	}
1104
1105	/* Disable the IRQs for events we handled */
1106	spin_lock(&priv->lock);
1107	irqs = readl_relaxed(priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
1108	writel_relaxed(irqs & ~disableirqs,
1109		       priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
1110	spin_unlock(&priv->lock);
1111
1112	/* Writing 1 to the pending field will clear the pending interrupt */
1113	writel_relaxed(pendirq, priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1114
1115	/*
1116	 * If a pchan was freed, we may be able to schedule something else,
1117	 * so have a look around
1118	 */
1119	if (free_room) {
1120		for (i = 0; i < SUN4I_DMA_NR_MAX_VCHANS; i++) {
1121			vchan = &priv->vchans[i];
1122			spin_lock(&vchan->vc.lock);
1123			__execute_vchan_pending(priv, vchan);
1124			spin_unlock(&vchan->vc.lock);
1125		}
1126	}
1127
1128	/*
1129	 * Handle newer interrupts if some showed up, but only do it once
1130	 * to avoid a too long a loop
1131	 */
1132	if (allow_mitigation) {
1133		pendirq = readl_relaxed(priv->base +
1134					SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1135		if (pendirq) {
1136			allow_mitigation = 0;
1137			goto handle_pending;
1138		}
1139	}
1140
1141	return IRQ_HANDLED;
1142}
1143
1144static int sun4i_dma_probe(struct platform_device *pdev)
1145{
1146	struct sun4i_dma_dev *priv;
 
1147	int i, j, ret;
1148
1149	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1150	if (!priv)
1151		return -ENOMEM;
1152
1153	priv->base = devm_platform_ioremap_resource(pdev, 0);
 
1154	if (IS_ERR(priv->base))
1155		return PTR_ERR(priv->base);
1156
1157	priv->irq = platform_get_irq(pdev, 0);
1158	if (priv->irq < 0)
1159		return priv->irq;
1160
1161	priv->clk = devm_clk_get(&pdev->dev, NULL);
1162	if (IS_ERR(priv->clk)) {
1163		dev_err(&pdev->dev, "No clock specified\n");
1164		return PTR_ERR(priv->clk);
1165	}
1166
1167	platform_set_drvdata(pdev, priv);
1168	spin_lock_init(&priv->lock);
1169
1170	dma_set_max_seg_size(&pdev->dev, SUN4I_DMA_MAX_SEG_SIZE);
1171
1172	dma_cap_zero(priv->slave.cap_mask);
1173	dma_cap_set(DMA_PRIVATE, priv->slave.cap_mask);
1174	dma_cap_set(DMA_MEMCPY, priv->slave.cap_mask);
1175	dma_cap_set(DMA_CYCLIC, priv->slave.cap_mask);
1176	dma_cap_set(DMA_SLAVE, priv->slave.cap_mask);
1177
1178	INIT_LIST_HEAD(&priv->slave.channels);
1179	priv->slave.device_free_chan_resources	= sun4i_dma_free_chan_resources;
1180	priv->slave.device_tx_status		= sun4i_dma_tx_status;
1181	priv->slave.device_issue_pending	= sun4i_dma_issue_pending;
1182	priv->slave.device_prep_slave_sg	= sun4i_dma_prep_slave_sg;
1183	priv->slave.device_prep_dma_memcpy	= sun4i_dma_prep_dma_memcpy;
1184	priv->slave.device_prep_dma_cyclic	= sun4i_dma_prep_dma_cyclic;
1185	priv->slave.device_config		= sun4i_dma_config;
1186	priv->slave.device_terminate_all	= sun4i_dma_terminate_all;
1187	priv->slave.copy_align			= 2;
1188	priv->slave.src_addr_widths		= BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1189						  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1190						  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1191	priv->slave.dst_addr_widths		= BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1192						  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1193						  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1194	priv->slave.directions			= BIT(DMA_DEV_TO_MEM) |
1195						  BIT(DMA_MEM_TO_DEV);
1196	priv->slave.residue_granularity		= DMA_RESIDUE_GRANULARITY_BURST;
1197
1198	priv->slave.dev = &pdev->dev;
1199
1200	priv->pchans = devm_kcalloc(&pdev->dev, SUN4I_DMA_NR_MAX_CHANNELS,
1201				    sizeof(struct sun4i_dma_pchan), GFP_KERNEL);
1202	priv->vchans = devm_kcalloc(&pdev->dev, SUN4I_DMA_NR_MAX_VCHANS,
1203				    sizeof(struct sun4i_dma_vchan), GFP_KERNEL);
1204	if (!priv->vchans || !priv->pchans)
1205		return -ENOMEM;
1206
1207	/*
1208	 * [0..SUN4I_NDMA_NR_MAX_CHANNELS) are normal pchans, and
1209	 * [SUN4I_NDMA_NR_MAX_CHANNELS..SUN4I_DMA_NR_MAX_CHANNELS) are
1210	 * dedicated ones
1211	 */
1212	for (i = 0; i < SUN4I_NDMA_NR_MAX_CHANNELS; i++)
1213		priv->pchans[i].base = priv->base +
1214			SUN4I_NDMA_CHANNEL_REG_BASE(i);
1215
1216	for (j = 0; i < SUN4I_DMA_NR_MAX_CHANNELS; i++, j++) {
1217		priv->pchans[i].base = priv->base +
1218			SUN4I_DDMA_CHANNEL_REG_BASE(j);
1219		priv->pchans[i].is_dedicated = 1;
1220	}
1221
1222	for (i = 0; i < SUN4I_DMA_NR_MAX_VCHANS; i++) {
1223		struct sun4i_dma_vchan *vchan = &priv->vchans[i];
1224
1225		spin_lock_init(&vchan->vc.lock);
1226		vchan->vc.desc_free = sun4i_dma_free_contract;
1227		vchan_init(&vchan->vc, &priv->slave);
1228	}
1229
1230	ret = clk_prepare_enable(priv->clk);
1231	if (ret) {
1232		dev_err(&pdev->dev, "Couldn't enable the clock\n");
1233		return ret;
1234	}
1235
1236	/*
1237	 * Make sure the IRQs are all disabled and accounted for. The bootloader
1238	 * likes to leave these dirty
1239	 */
1240	writel(0, priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
1241	writel(0xFFFFFFFF, priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1242
1243	ret = devm_request_irq(&pdev->dev, priv->irq, sun4i_dma_interrupt,
1244			       0, dev_name(&pdev->dev), priv);
1245	if (ret) {
1246		dev_err(&pdev->dev, "Cannot request IRQ\n");
1247		goto err_clk_disable;
1248	}
1249
1250	ret = dma_async_device_register(&priv->slave);
1251	if (ret) {
1252		dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
1253		goto err_clk_disable;
1254	}
1255
1256	ret = of_dma_controller_register(pdev->dev.of_node, sun4i_dma_of_xlate,
1257					 priv);
1258	if (ret) {
1259		dev_err(&pdev->dev, "of_dma_controller_register failed\n");
1260		goto err_dma_unregister;
1261	}
1262
1263	dev_dbg(&pdev->dev, "Successfully probed SUN4I_DMA\n");
1264
1265	return 0;
1266
1267err_dma_unregister:
1268	dma_async_device_unregister(&priv->slave);
1269err_clk_disable:
1270	clk_disable_unprepare(priv->clk);
1271	return ret;
1272}
1273
1274static void sun4i_dma_remove(struct platform_device *pdev)
1275{
1276	struct sun4i_dma_dev *priv = platform_get_drvdata(pdev);
1277
1278	/* Disable IRQ so no more work is scheduled */
1279	disable_irq(priv->irq);
1280
1281	of_dma_controller_free(pdev->dev.of_node);
1282	dma_async_device_unregister(&priv->slave);
1283
1284	clk_disable_unprepare(priv->clk);
 
 
1285}
1286
1287static const struct of_device_id sun4i_dma_match[] = {
1288	{ .compatible = "allwinner,sun4i-a10-dma" },
1289	{ /* sentinel */ },
1290};
1291MODULE_DEVICE_TABLE(of, sun4i_dma_match);
1292
1293static struct platform_driver sun4i_dma_driver = {
1294	.probe	= sun4i_dma_probe,
1295	.remove_new = sun4i_dma_remove,
1296	.driver	= {
1297		.name		= "sun4i-dma",
1298		.of_match_table	= sun4i_dma_match,
1299	},
1300};
1301
1302module_platform_driver(sun4i_dma_driver);
1303
1304MODULE_DESCRIPTION("Allwinner A10 Dedicated DMA Controller Driver");
1305MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
1306MODULE_LICENSE("GPL");
v5.9
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (C) 2014 Emilio López
   4 * Emilio López <emilio@elopez.com.ar>
   5 */
   6
   7#include <linux/bitmap.h>
   8#include <linux/bitops.h>
   9#include <linux/clk.h>
 
  10#include <linux/dmaengine.h>
  11#include <linux/dmapool.h>
  12#include <linux/interrupt.h>
  13#include <linux/module.h>
  14#include <linux/of_dma.h>
  15#include <linux/platform_device.h>
  16#include <linux/slab.h>
  17#include <linux/spinlock.h>
  18
  19#include "virt-dma.h"
  20
  21/** Common macros to normal and dedicated DMA registers **/
  22
  23#define SUN4I_DMA_CFG_LOADING			BIT(31)
  24#define SUN4I_DMA_CFG_DST_DATA_WIDTH(width)	((width) << 25)
  25#define SUN4I_DMA_CFG_DST_BURST_LENGTH(len)	((len) << 23)
  26#define SUN4I_DMA_CFG_DST_ADDR_MODE(mode)	((mode) << 21)
  27#define SUN4I_DMA_CFG_DST_DRQ_TYPE(type)	((type) << 16)
  28#define SUN4I_DMA_CFG_SRC_DATA_WIDTH(width)	((width) << 9)
  29#define SUN4I_DMA_CFG_SRC_BURST_LENGTH(len)	((len) << 7)
  30#define SUN4I_DMA_CFG_SRC_ADDR_MODE(mode)	((mode) << 5)
  31#define SUN4I_DMA_CFG_SRC_DRQ_TYPE(type)	(type)
  32
  33/** Normal DMA register values **/
  34
  35/* Normal DMA source/destination data request type values */
  36#define SUN4I_NDMA_DRQ_TYPE_SDRAM		0x16
  37#define SUN4I_NDMA_DRQ_TYPE_LIMIT		(0x1F + 1)
  38
  39/** Normal DMA register layout **/
  40
  41/* Dedicated DMA source/destination address mode values */
  42#define SUN4I_NDMA_ADDR_MODE_LINEAR		0
  43#define SUN4I_NDMA_ADDR_MODE_IO			1
  44
  45/* Normal DMA configuration register layout */
  46#define SUN4I_NDMA_CFG_CONT_MODE		BIT(30)
  47#define SUN4I_NDMA_CFG_WAIT_STATE(n)		((n) << 27)
  48#define SUN4I_NDMA_CFG_DST_NON_SECURE		BIT(22)
  49#define SUN4I_NDMA_CFG_BYTE_COUNT_MODE_REMAIN	BIT(15)
  50#define SUN4I_NDMA_CFG_SRC_NON_SECURE		BIT(6)
  51
  52/** Dedicated DMA register values **/
  53
  54/* Dedicated DMA source/destination address mode values */
  55#define SUN4I_DDMA_ADDR_MODE_LINEAR		0
  56#define SUN4I_DDMA_ADDR_MODE_IO			1
  57#define SUN4I_DDMA_ADDR_MODE_HORIZONTAL_PAGE	2
  58#define SUN4I_DDMA_ADDR_MODE_VERTICAL_PAGE	3
  59
  60/* Dedicated DMA source/destination data request type values */
  61#define SUN4I_DDMA_DRQ_TYPE_SDRAM		0x1
  62#define SUN4I_DDMA_DRQ_TYPE_LIMIT		(0x1F + 1)
  63
  64/** Dedicated DMA register layout **/
  65
  66/* Dedicated DMA configuration register layout */
  67#define SUN4I_DDMA_CFG_BUSY			BIT(30)
  68#define SUN4I_DDMA_CFG_CONT_MODE		BIT(29)
  69#define SUN4I_DDMA_CFG_DST_NON_SECURE		BIT(28)
  70#define SUN4I_DDMA_CFG_BYTE_COUNT_MODE_REMAIN	BIT(15)
  71#define SUN4I_DDMA_CFG_SRC_NON_SECURE		BIT(12)
  72
  73/* Dedicated DMA parameter register layout */
  74#define SUN4I_DDMA_PARA_DST_DATA_BLK_SIZE(n)	(((n) - 1) << 24)
  75#define SUN4I_DDMA_PARA_DST_WAIT_CYCLES(n)	(((n) - 1) << 16)
  76#define SUN4I_DDMA_PARA_SRC_DATA_BLK_SIZE(n)	(((n) - 1) << 8)
  77#define SUN4I_DDMA_PARA_SRC_WAIT_CYCLES(n)	(((n) - 1) << 0)
  78
  79/** DMA register offsets **/
  80
  81/* General register offsets */
  82#define SUN4I_DMA_IRQ_ENABLE_REG		0x0
  83#define SUN4I_DMA_IRQ_PENDING_STATUS_REG	0x4
  84
  85/* Normal DMA register offsets */
  86#define SUN4I_NDMA_CHANNEL_REG_BASE(n)		(0x100 + (n) * 0x20)
  87#define SUN4I_NDMA_CFG_REG			0x0
  88#define SUN4I_NDMA_SRC_ADDR_REG			0x4
  89#define SUN4I_NDMA_DST_ADDR_REG		0x8
  90#define SUN4I_NDMA_BYTE_COUNT_REG		0xC
  91
  92/* Dedicated DMA register offsets */
  93#define SUN4I_DDMA_CHANNEL_REG_BASE(n)		(0x300 + (n) * 0x20)
  94#define SUN4I_DDMA_CFG_REG			0x0
  95#define SUN4I_DDMA_SRC_ADDR_REG			0x4
  96#define SUN4I_DDMA_DST_ADDR_REG		0x8
  97#define SUN4I_DDMA_BYTE_COUNT_REG		0xC
  98#define SUN4I_DDMA_PARA_REG			0x18
  99
 100/** DMA Driver **/
 101
 102/*
 103 * Normal DMA has 8 channels, and Dedicated DMA has another 8, so
 104 * that's 16 channels. As for endpoints, there's 29 and 21
 105 * respectively. Given that the Normal DMA endpoints (other than
 106 * SDRAM) can be used as tx/rx, we need 78 vchans in total
 107 */
 108#define SUN4I_NDMA_NR_MAX_CHANNELS	8
 109#define SUN4I_DDMA_NR_MAX_CHANNELS	8
 110#define SUN4I_DMA_NR_MAX_CHANNELS					\
 111	(SUN4I_NDMA_NR_MAX_CHANNELS + SUN4I_DDMA_NR_MAX_CHANNELS)
 112#define SUN4I_NDMA_NR_MAX_VCHANS	(29 * 2 - 1)
 113#define SUN4I_DDMA_NR_MAX_VCHANS	21
 114#define SUN4I_DMA_NR_MAX_VCHANS						\
 115	(SUN4I_NDMA_NR_MAX_VCHANS + SUN4I_DDMA_NR_MAX_VCHANS)
 116
 117/* This set of SUN4I_DDMA timing parameters were found experimentally while
 118 * working with the SPI driver and seem to make it behave correctly */
 119#define SUN4I_DDMA_MAGIC_SPI_PARAMETERS \
 120	(SUN4I_DDMA_PARA_DST_DATA_BLK_SIZE(1) |			\
 121	 SUN4I_DDMA_PARA_SRC_DATA_BLK_SIZE(1) |				\
 122	 SUN4I_DDMA_PARA_DST_WAIT_CYCLES(2) |				\
 123	 SUN4I_DDMA_PARA_SRC_WAIT_CYCLES(2))
 124
 
 
 
 
 
 
 
 
 
 125struct sun4i_dma_pchan {
 126	/* Register base of channel */
 127	void __iomem			*base;
 128	/* vchan currently being serviced */
 129	struct sun4i_dma_vchan		*vchan;
 130	/* Is this a dedicated pchan? */
 131	int				is_dedicated;
 132};
 133
 134struct sun4i_dma_vchan {
 135	struct virt_dma_chan		vc;
 136	struct dma_slave_config		cfg;
 137	struct sun4i_dma_pchan		*pchan;
 138	struct sun4i_dma_promise	*processing;
 139	struct sun4i_dma_contract	*contract;
 140	u8				endpoint;
 141	int				is_dedicated;
 142};
 143
 144struct sun4i_dma_promise {
 145	u32				cfg;
 146	u32				para;
 147	dma_addr_t			src;
 148	dma_addr_t			dst;
 149	size_t				len;
 150	struct list_head		list;
 151};
 152
 153/* A contract is a set of promises */
 154struct sun4i_dma_contract {
 155	struct virt_dma_desc		vd;
 156	struct list_head		demands;
 157	struct list_head		completed_demands;
 158	int				is_cyclic;
 
 159};
 160
 161struct sun4i_dma_dev {
 162	DECLARE_BITMAP(pchans_used, SUN4I_DMA_NR_MAX_CHANNELS);
 163	struct dma_device		slave;
 164	struct sun4i_dma_pchan		*pchans;
 165	struct sun4i_dma_vchan		*vchans;
 166	void __iomem			*base;
 167	struct clk			*clk;
 168	int				irq;
 169	spinlock_t			lock;
 170};
 171
 172static struct sun4i_dma_dev *to_sun4i_dma_dev(struct dma_device *dev)
 173{
 174	return container_of(dev, struct sun4i_dma_dev, slave);
 175}
 176
 177static struct sun4i_dma_vchan *to_sun4i_dma_vchan(struct dma_chan *chan)
 178{
 179	return container_of(chan, struct sun4i_dma_vchan, vc.chan);
 180}
 181
 182static struct sun4i_dma_contract *to_sun4i_dma_contract(struct virt_dma_desc *vd)
 183{
 184	return container_of(vd, struct sun4i_dma_contract, vd);
 185}
 186
 187static struct device *chan2dev(struct dma_chan *chan)
 188{
 189	return &chan->dev->device;
 190}
 191
 192static int convert_burst(u32 maxburst)
 193{
 194	if (maxburst > 8)
 195		return -EINVAL;
 196
 197	/* 1 -> 0, 4 -> 1, 8 -> 2 */
 198	return (maxburst >> 2);
 199}
 200
 201static int convert_buswidth(enum dma_slave_buswidth addr_width)
 202{
 203	if (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES)
 204		return -EINVAL;
 205
 206	/* 8 (1 byte) -> 0, 16 (2 bytes) -> 1, 32 (4 bytes) -> 2 */
 207	return (addr_width >> 1);
 208}
 209
 210static void sun4i_dma_free_chan_resources(struct dma_chan *chan)
 211{
 212	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 213
 214	vchan_free_chan_resources(&vchan->vc);
 215}
 216
 217static struct sun4i_dma_pchan *find_and_use_pchan(struct sun4i_dma_dev *priv,
 218						  struct sun4i_dma_vchan *vchan)
 219{
 220	struct sun4i_dma_pchan *pchan = NULL, *pchans = priv->pchans;
 221	unsigned long flags;
 222	int i, max;
 223
 224	/*
 225	 * pchans 0-SUN4I_NDMA_NR_MAX_CHANNELS are normal, and
 226	 * SUN4I_NDMA_NR_MAX_CHANNELS+ are dedicated ones
 227	 */
 228	if (vchan->is_dedicated) {
 229		i = SUN4I_NDMA_NR_MAX_CHANNELS;
 230		max = SUN4I_DMA_NR_MAX_CHANNELS;
 231	} else {
 232		i = 0;
 233		max = SUN4I_NDMA_NR_MAX_CHANNELS;
 234	}
 235
 236	spin_lock_irqsave(&priv->lock, flags);
 237	for_each_clear_bit_from(i, priv->pchans_used, max) {
 238		pchan = &pchans[i];
 239		pchan->vchan = vchan;
 240		set_bit(i, priv->pchans_used);
 241		break;
 242	}
 243	spin_unlock_irqrestore(&priv->lock, flags);
 244
 245	return pchan;
 246}
 247
 248static void release_pchan(struct sun4i_dma_dev *priv,
 249			  struct sun4i_dma_pchan *pchan)
 250{
 251	unsigned long flags;
 252	int nr = pchan - priv->pchans;
 253
 254	spin_lock_irqsave(&priv->lock, flags);
 255
 256	pchan->vchan = NULL;
 257	clear_bit(nr, priv->pchans_used);
 258
 259	spin_unlock_irqrestore(&priv->lock, flags);
 260}
 261
 262static void configure_pchan(struct sun4i_dma_pchan *pchan,
 263			    struct sun4i_dma_promise *d)
 264{
 265	/*
 266	 * Configure addresses and misc parameters depending on type
 267	 * SUN4I_DDMA has an extra field with timing parameters
 268	 */
 269	if (pchan->is_dedicated) {
 270		writel_relaxed(d->src, pchan->base + SUN4I_DDMA_SRC_ADDR_REG);
 271		writel_relaxed(d->dst, pchan->base + SUN4I_DDMA_DST_ADDR_REG);
 272		writel_relaxed(d->len, pchan->base + SUN4I_DDMA_BYTE_COUNT_REG);
 273		writel_relaxed(d->para, pchan->base + SUN4I_DDMA_PARA_REG);
 274		writel_relaxed(d->cfg, pchan->base + SUN4I_DDMA_CFG_REG);
 275	} else {
 276		writel_relaxed(d->src, pchan->base + SUN4I_NDMA_SRC_ADDR_REG);
 277		writel_relaxed(d->dst, pchan->base + SUN4I_NDMA_DST_ADDR_REG);
 278		writel_relaxed(d->len, pchan->base + SUN4I_NDMA_BYTE_COUNT_REG);
 279		writel_relaxed(d->cfg, pchan->base + SUN4I_NDMA_CFG_REG);
 280	}
 281}
 282
 283static void set_pchan_interrupt(struct sun4i_dma_dev *priv,
 284				struct sun4i_dma_pchan *pchan,
 285				int half, int end)
 286{
 287	u32 reg;
 288	int pchan_number = pchan - priv->pchans;
 289	unsigned long flags;
 290
 291	spin_lock_irqsave(&priv->lock, flags);
 292
 293	reg = readl_relaxed(priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
 294
 295	if (half)
 296		reg |= BIT(pchan_number * 2);
 297	else
 298		reg &= ~BIT(pchan_number * 2);
 299
 300	if (end)
 301		reg |= BIT(pchan_number * 2 + 1);
 302	else
 303		reg &= ~BIT(pchan_number * 2 + 1);
 304
 305	writel_relaxed(reg, priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
 306
 307	spin_unlock_irqrestore(&priv->lock, flags);
 308}
 309
 310/*
 311 * Execute pending operations on a vchan
 312 *
 313 * When given a vchan, this function will try to acquire a suitable
 314 * pchan and, if successful, will configure it to fulfill a promise
 315 * from the next pending contract.
 316 *
 317 * This function must be called with &vchan->vc.lock held.
 318 */
 319static int __execute_vchan_pending(struct sun4i_dma_dev *priv,
 320				   struct sun4i_dma_vchan *vchan)
 321{
 322	struct sun4i_dma_promise *promise = NULL;
 323	struct sun4i_dma_contract *contract = NULL;
 324	struct sun4i_dma_pchan *pchan;
 325	struct virt_dma_desc *vd;
 326	int ret;
 327
 328	lockdep_assert_held(&vchan->vc.lock);
 329
 330	/* We need a pchan to do anything, so secure one if available */
 331	pchan = find_and_use_pchan(priv, vchan);
 332	if (!pchan)
 333		return -EBUSY;
 334
 335	/*
 336	 * Channel endpoints must not be repeated, so if this vchan
 337	 * has already submitted some work, we can't do anything else
 338	 */
 339	if (vchan->processing) {
 340		dev_dbg(chan2dev(&vchan->vc.chan),
 341			"processing something to this endpoint already\n");
 342		ret = -EBUSY;
 343		goto release_pchan;
 344	}
 345
 346	do {
 347		/* Figure out which contract we're working with today */
 348		vd = vchan_next_desc(&vchan->vc);
 349		if (!vd) {
 350			dev_dbg(chan2dev(&vchan->vc.chan),
 351				"No pending contract found");
 352			ret = 0;
 353			goto release_pchan;
 354		}
 355
 356		contract = to_sun4i_dma_contract(vd);
 357		if (list_empty(&contract->demands)) {
 358			/* The contract has been completed so mark it as such */
 359			list_del(&contract->vd.node);
 360			vchan_cookie_complete(&contract->vd);
 361			dev_dbg(chan2dev(&vchan->vc.chan),
 362				"Empty contract found and marked complete");
 363		}
 364	} while (list_empty(&contract->demands));
 365
 366	/* Now find out what we need to do */
 367	promise = list_first_entry(&contract->demands,
 368				   struct sun4i_dma_promise, list);
 369	vchan->processing = promise;
 370
 371	/* ... and make it reality */
 372	if (promise) {
 373		vchan->contract = contract;
 374		vchan->pchan = pchan;
 375		set_pchan_interrupt(priv, pchan, contract->is_cyclic, 1);
 376		configure_pchan(pchan, promise);
 377	}
 378
 379	return 0;
 380
 381release_pchan:
 382	release_pchan(priv, pchan);
 383	return ret;
 384}
 385
 386static int sanitize_config(struct dma_slave_config *sconfig,
 387			   enum dma_transfer_direction direction)
 388{
 389	switch (direction) {
 390	case DMA_MEM_TO_DEV:
 391		if ((sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) ||
 392		    !sconfig->dst_maxburst)
 393			return -EINVAL;
 394
 395		if (sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
 396			sconfig->src_addr_width = sconfig->dst_addr_width;
 397
 398		if (!sconfig->src_maxburst)
 399			sconfig->src_maxburst = sconfig->dst_maxburst;
 400
 401		break;
 402
 403	case DMA_DEV_TO_MEM:
 404		if ((sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) ||
 405		    !sconfig->src_maxburst)
 406			return -EINVAL;
 407
 408		if (sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
 409			sconfig->dst_addr_width = sconfig->src_addr_width;
 410
 411		if (!sconfig->dst_maxburst)
 412			sconfig->dst_maxburst = sconfig->src_maxburst;
 413
 414		break;
 415	default:
 416		return 0;
 417	}
 418
 419	return 0;
 420}
 421
 422/*
 423 * Generate a promise, to be used in a normal DMA contract.
 424 *
 425 * A NDMA promise contains all the information required to program the
 426 * normal part of the DMA Engine and get data copied. A non-executed
 427 * promise will live in the demands list on a contract. Once it has been
 428 * completed, it will be moved to the completed demands list for later freeing.
 429 * All linked promises will be freed when the corresponding contract is freed
 430 */
 431static struct sun4i_dma_promise *
 432generate_ndma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
 433		      size_t len, struct dma_slave_config *sconfig,
 434		      enum dma_transfer_direction direction)
 435{
 436	struct sun4i_dma_promise *promise;
 437	int ret;
 438
 439	ret = sanitize_config(sconfig, direction);
 440	if (ret)
 441		return NULL;
 442
 443	promise = kzalloc(sizeof(*promise), GFP_NOWAIT);
 444	if (!promise)
 445		return NULL;
 446
 447	promise->src = src;
 448	promise->dst = dest;
 449	promise->len = len;
 450	promise->cfg = SUN4I_DMA_CFG_LOADING |
 451		SUN4I_NDMA_CFG_BYTE_COUNT_MODE_REMAIN;
 452
 453	dev_dbg(chan2dev(chan),
 454		"src burst %d, dst burst %d, src buswidth %d, dst buswidth %d",
 455		sconfig->src_maxburst, sconfig->dst_maxburst,
 456		sconfig->src_addr_width, sconfig->dst_addr_width);
 457
 458	/* Source burst */
 459	ret = convert_burst(sconfig->src_maxburst);
 460	if (ret < 0)
 461		goto fail;
 462	promise->cfg |= SUN4I_DMA_CFG_SRC_BURST_LENGTH(ret);
 463
 464	/* Destination burst */
 465	ret = convert_burst(sconfig->dst_maxburst);
 466	if (ret < 0)
 467		goto fail;
 468	promise->cfg |= SUN4I_DMA_CFG_DST_BURST_LENGTH(ret);
 469
 470	/* Source bus width */
 471	ret = convert_buswidth(sconfig->src_addr_width);
 472	if (ret < 0)
 473		goto fail;
 474	promise->cfg |= SUN4I_DMA_CFG_SRC_DATA_WIDTH(ret);
 475
 476	/* Destination bus width */
 477	ret = convert_buswidth(sconfig->dst_addr_width);
 478	if (ret < 0)
 479		goto fail;
 480	promise->cfg |= SUN4I_DMA_CFG_DST_DATA_WIDTH(ret);
 481
 482	return promise;
 483
 484fail:
 485	kfree(promise);
 486	return NULL;
 487}
 488
 489/*
 490 * Generate a promise, to be used in a dedicated DMA contract.
 491 *
 492 * A DDMA promise contains all the information required to program the
 493 * Dedicated part of the DMA Engine and get data copied. A non-executed
 494 * promise will live in the demands list on a contract. Once it has been
 495 * completed, it will be moved to the completed demands list for later freeing.
 496 * All linked promises will be freed when the corresponding contract is freed
 497 */
 498static struct sun4i_dma_promise *
 499generate_ddma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
 500		      size_t len, struct dma_slave_config *sconfig)
 501{
 502	struct sun4i_dma_promise *promise;
 503	int ret;
 504
 505	promise = kzalloc(sizeof(*promise), GFP_NOWAIT);
 506	if (!promise)
 507		return NULL;
 508
 509	promise->src = src;
 510	promise->dst = dest;
 511	promise->len = len;
 512	promise->cfg = SUN4I_DMA_CFG_LOADING |
 513		SUN4I_DDMA_CFG_BYTE_COUNT_MODE_REMAIN;
 514
 515	/* Source burst */
 516	ret = convert_burst(sconfig->src_maxburst);
 517	if (ret < 0)
 518		goto fail;
 519	promise->cfg |= SUN4I_DMA_CFG_SRC_BURST_LENGTH(ret);
 520
 521	/* Destination burst */
 522	ret = convert_burst(sconfig->dst_maxburst);
 523	if (ret < 0)
 524		goto fail;
 525	promise->cfg |= SUN4I_DMA_CFG_DST_BURST_LENGTH(ret);
 526
 527	/* Source bus width */
 528	ret = convert_buswidth(sconfig->src_addr_width);
 529	if (ret < 0)
 530		goto fail;
 531	promise->cfg |= SUN4I_DMA_CFG_SRC_DATA_WIDTH(ret);
 532
 533	/* Destination bus width */
 534	ret = convert_buswidth(sconfig->dst_addr_width);
 535	if (ret < 0)
 536		goto fail;
 537	promise->cfg |= SUN4I_DMA_CFG_DST_DATA_WIDTH(ret);
 538
 539	return promise;
 540
 541fail:
 542	kfree(promise);
 543	return NULL;
 544}
 545
 546/*
 547 * Generate a contract
 548 *
 549 * Contracts function as DMA descriptors. As our hardware does not support
 550 * linked lists, we need to implement SG via software. We use a contract
 551 * to hold all the pieces of the request and process them serially one
 552 * after another. Each piece is represented as a promise.
 553 */
 554static struct sun4i_dma_contract *generate_dma_contract(void)
 555{
 556	struct sun4i_dma_contract *contract;
 557
 558	contract = kzalloc(sizeof(*contract), GFP_NOWAIT);
 559	if (!contract)
 560		return NULL;
 561
 562	INIT_LIST_HEAD(&contract->demands);
 563	INIT_LIST_HEAD(&contract->completed_demands);
 564
 565	return contract;
 566}
 567
 568/*
 569 * Get next promise on a cyclic transfer
 570 *
 571 * Cyclic contracts contain a series of promises which are executed on a
 572 * loop. This function returns the next promise from a cyclic contract,
 573 * so it can be programmed into the hardware.
 574 */
 575static struct sun4i_dma_promise *
 576get_next_cyclic_promise(struct sun4i_dma_contract *contract)
 577{
 578	struct sun4i_dma_promise *promise;
 579
 580	promise = list_first_entry_or_null(&contract->demands,
 581					   struct sun4i_dma_promise, list);
 582	if (!promise) {
 583		list_splice_init(&contract->completed_demands,
 584				 &contract->demands);
 585		promise = list_first_entry(&contract->demands,
 586					   struct sun4i_dma_promise, list);
 587	}
 588
 589	return promise;
 590}
 591
 592/*
 593 * Free a contract and all its associated promises
 594 */
 595static void sun4i_dma_free_contract(struct virt_dma_desc *vd)
 596{
 597	struct sun4i_dma_contract *contract = to_sun4i_dma_contract(vd);
 598	struct sun4i_dma_promise *promise, *tmp;
 599
 600	/* Free all the demands and completed demands */
 601	list_for_each_entry_safe(promise, tmp, &contract->demands, list)
 602		kfree(promise);
 603
 604	list_for_each_entry_safe(promise, tmp, &contract->completed_demands, list)
 605		kfree(promise);
 606
 607	kfree(contract);
 608}
 609
 610static struct dma_async_tx_descriptor *
 611sun4i_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
 612			  dma_addr_t src, size_t len, unsigned long flags)
 613{
 614	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 615	struct dma_slave_config *sconfig = &vchan->cfg;
 616	struct sun4i_dma_promise *promise;
 617	struct sun4i_dma_contract *contract;
 618
 619	contract = generate_dma_contract();
 620	if (!contract)
 621		return NULL;
 622
 623	/*
 624	 * We can only do the copy to bus aligned addresses, so
 625	 * choose the best one so we get decent performance. We also
 626	 * maximize the burst size for this same reason.
 627	 */
 628	sconfig->src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
 629	sconfig->dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
 630	sconfig->src_maxburst = 8;
 631	sconfig->dst_maxburst = 8;
 632
 633	if (vchan->is_dedicated)
 634		promise = generate_ddma_promise(chan, src, dest, len, sconfig);
 635	else
 636		promise = generate_ndma_promise(chan, src, dest, len, sconfig,
 637						DMA_MEM_TO_MEM);
 638
 639	if (!promise) {
 640		kfree(contract);
 641		return NULL;
 642	}
 643
 644	/* Configure memcpy mode */
 645	if (vchan->is_dedicated) {
 646		promise->cfg |= SUN4I_DMA_CFG_SRC_DRQ_TYPE(SUN4I_DDMA_DRQ_TYPE_SDRAM) |
 647				SUN4I_DMA_CFG_DST_DRQ_TYPE(SUN4I_DDMA_DRQ_TYPE_SDRAM);
 648	} else {
 649		promise->cfg |= SUN4I_DMA_CFG_SRC_DRQ_TYPE(SUN4I_NDMA_DRQ_TYPE_SDRAM) |
 650				SUN4I_DMA_CFG_DST_DRQ_TYPE(SUN4I_NDMA_DRQ_TYPE_SDRAM);
 651	}
 652
 653	/* Fill the contract with our only promise */
 654	list_add_tail(&promise->list, &contract->demands);
 655
 656	/* And add it to the vchan */
 657	return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
 658}
 659
 660static struct dma_async_tx_descriptor *
 661sun4i_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf, size_t len,
 662			  size_t period_len, enum dma_transfer_direction dir,
 663			  unsigned long flags)
 664{
 665	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 666	struct dma_slave_config *sconfig = &vchan->cfg;
 667	struct sun4i_dma_promise *promise;
 668	struct sun4i_dma_contract *contract;
 669	dma_addr_t src, dest;
 670	u32 endpoints;
 671	int nr_periods, offset, plength, i;
 672	u8 ram_type, io_mode, linear_mode;
 673
 674	if (!is_slave_direction(dir)) {
 675		dev_err(chan2dev(chan), "Invalid DMA direction\n");
 676		return NULL;
 677	}
 678
 679	contract = generate_dma_contract();
 680	if (!contract)
 681		return NULL;
 682
 683	contract->is_cyclic = 1;
 684
 685	if (vchan->is_dedicated) {
 686		io_mode = SUN4I_DDMA_ADDR_MODE_IO;
 687		linear_mode = SUN4I_DDMA_ADDR_MODE_LINEAR;
 688		ram_type = SUN4I_DDMA_DRQ_TYPE_SDRAM;
 689	} else {
 690		io_mode = SUN4I_NDMA_ADDR_MODE_IO;
 691		linear_mode = SUN4I_NDMA_ADDR_MODE_LINEAR;
 692		ram_type = SUN4I_NDMA_DRQ_TYPE_SDRAM;
 693	}
 694
 695	if (dir == DMA_MEM_TO_DEV) {
 696		src = buf;
 697		dest = sconfig->dst_addr;
 698		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(vchan->endpoint) |
 699			    SUN4I_DMA_CFG_DST_ADDR_MODE(io_mode) |
 700			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(ram_type) |
 701			    SUN4I_DMA_CFG_SRC_ADDR_MODE(linear_mode);
 702	} else {
 703		src = sconfig->src_addr;
 704		dest = buf;
 705		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(ram_type) |
 706			    SUN4I_DMA_CFG_DST_ADDR_MODE(linear_mode) |
 707			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
 708			    SUN4I_DMA_CFG_SRC_ADDR_MODE(io_mode);
 709	}
 710
 711	/*
 712	 * We will be using half done interrupts to make two periods
 713	 * out of a promise, so we need to program the DMA engine less
 714	 * often
 715	 */
 716
 717	/*
 718	 * The engine can interrupt on half-transfer, so we can use
 719	 * this feature to program the engine half as often as if we
 720	 * didn't use it (keep in mind the hardware doesn't support
 721	 * linked lists).
 722	 *
 723	 * Say you have a set of periods (| marks the start/end, I for
 724	 * interrupt, P for programming the engine to do a new
 725	 * transfer), the easy but slow way would be to do
 726	 *
 727	 *  |---|---|---|---| (periods / promises)
 728	 *  P  I,P I,P I,P  I
 729	 *
 730	 * Using half transfer interrupts you can do
 731	 *
 732	 *  |-------|-------| (promises as configured on hw)
 733	 *  |---|---|---|---| (periods)
 734	 *  P   I  I,P  I   I
 735	 *
 736	 * Which requires half the engine programming for the same
 737	 * functionality.
 
 
 
 
 738	 */
 739	nr_periods = DIV_ROUND_UP(len / period_len, 2);
 
 
 
 
 
 740	for (i = 0; i < nr_periods; i++) {
 741		/* Calculate the offset in the buffer and the length needed */
 742		offset = i * period_len * 2;
 743		plength = min((len - offset), (period_len * 2));
 744		if (dir == DMA_MEM_TO_DEV)
 745			src = buf + offset;
 746		else
 747			dest = buf + offset;
 748
 749		/* Make the promise */
 750		if (vchan->is_dedicated)
 751			promise = generate_ddma_promise(chan, src, dest,
 752							plength, sconfig);
 753		else
 754			promise = generate_ndma_promise(chan, src, dest,
 755							plength, sconfig, dir);
 756
 757		if (!promise) {
 758			/* TODO: should we free everything? */
 759			return NULL;
 760		}
 761		promise->cfg |= endpoints;
 762
 763		/* Then add it to the contract */
 764		list_add_tail(&promise->list, &contract->demands);
 765	}
 766
 767	/* And add it to the vchan */
 768	return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
 769}
 770
 771static struct dma_async_tx_descriptor *
 772sun4i_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
 773			unsigned int sg_len, enum dma_transfer_direction dir,
 774			unsigned long flags, void *context)
 775{
 776	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 777	struct dma_slave_config *sconfig = &vchan->cfg;
 778	struct sun4i_dma_promise *promise;
 779	struct sun4i_dma_contract *contract;
 780	u8 ram_type, io_mode, linear_mode;
 781	struct scatterlist *sg;
 782	dma_addr_t srcaddr, dstaddr;
 783	u32 endpoints, para;
 784	int i;
 785
 786	if (!sgl)
 787		return NULL;
 788
 789	if (!is_slave_direction(dir)) {
 790		dev_err(chan2dev(chan), "Invalid DMA direction\n");
 791		return NULL;
 792	}
 793
 794	contract = generate_dma_contract();
 795	if (!contract)
 796		return NULL;
 797
 798	if (vchan->is_dedicated) {
 799		io_mode = SUN4I_DDMA_ADDR_MODE_IO;
 800		linear_mode = SUN4I_DDMA_ADDR_MODE_LINEAR;
 801		ram_type = SUN4I_DDMA_DRQ_TYPE_SDRAM;
 802	} else {
 803		io_mode = SUN4I_NDMA_ADDR_MODE_IO;
 804		linear_mode = SUN4I_NDMA_ADDR_MODE_LINEAR;
 805		ram_type = SUN4I_NDMA_DRQ_TYPE_SDRAM;
 806	}
 807
 808	if (dir == DMA_MEM_TO_DEV)
 809		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(vchan->endpoint) |
 810			    SUN4I_DMA_CFG_DST_ADDR_MODE(io_mode) |
 811			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(ram_type) |
 812			    SUN4I_DMA_CFG_SRC_ADDR_MODE(linear_mode);
 813	else
 814		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(ram_type) |
 815			    SUN4I_DMA_CFG_DST_ADDR_MODE(linear_mode) |
 816			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
 817			    SUN4I_DMA_CFG_SRC_ADDR_MODE(io_mode);
 818
 819	for_each_sg(sgl, sg, sg_len, i) {
 820		/* Figure out addresses */
 821		if (dir == DMA_MEM_TO_DEV) {
 822			srcaddr = sg_dma_address(sg);
 823			dstaddr = sconfig->dst_addr;
 824		} else {
 825			srcaddr = sconfig->src_addr;
 826			dstaddr = sg_dma_address(sg);
 827		}
 828
 829		/*
 830		 * These are the magic DMA engine timings that keep SPI going.
 831		 * I haven't seen any interface on DMAEngine to configure
 832		 * timings, and so far they seem to work for everything we
 833		 * support, so I've kept them here. I don't know if other
 834		 * devices need different timings because, as usual, we only
 835		 * have the "para" bitfield meanings, but no comment on what
 836		 * the values should be when doing a certain operation :|
 837		 */
 838		para = SUN4I_DDMA_MAGIC_SPI_PARAMETERS;
 839
 840		/* And make a suitable promise */
 841		if (vchan->is_dedicated)
 842			promise = generate_ddma_promise(chan, srcaddr, dstaddr,
 843							sg_dma_len(sg),
 844							sconfig);
 845		else
 846			promise = generate_ndma_promise(chan, srcaddr, dstaddr,
 847							sg_dma_len(sg),
 848							sconfig, dir);
 849
 850		if (!promise)
 851			return NULL; /* TODO: should we free everything? */
 852
 853		promise->cfg |= endpoints;
 854		promise->para = para;
 855
 856		/* Then add it to the contract */
 857		list_add_tail(&promise->list, &contract->demands);
 858	}
 859
 860	/*
 861	 * Once we've got all the promises ready, add the contract
 862	 * to the pending list on the vchan
 863	 */
 864	return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
 865}
 866
 867static int sun4i_dma_terminate_all(struct dma_chan *chan)
 868{
 869	struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
 870	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 871	struct sun4i_dma_pchan *pchan = vchan->pchan;
 872	LIST_HEAD(head);
 873	unsigned long flags;
 874
 875	spin_lock_irqsave(&vchan->vc.lock, flags);
 876	vchan_get_all_descriptors(&vchan->vc, &head);
 877	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 878
 879	/*
 880	 * Clearing the configuration register will halt the pchan. Interrupts
 881	 * may still trigger, so don't forget to disable them.
 882	 */
 883	if (pchan) {
 884		if (pchan->is_dedicated)
 885			writel(0, pchan->base + SUN4I_DDMA_CFG_REG);
 886		else
 887			writel(0, pchan->base + SUN4I_NDMA_CFG_REG);
 888		set_pchan_interrupt(priv, pchan, 0, 0);
 889		release_pchan(priv, pchan);
 890	}
 891
 892	spin_lock_irqsave(&vchan->vc.lock, flags);
 893	/* Clear these so the vchan is usable again */
 894	vchan->processing = NULL;
 895	vchan->pchan = NULL;
 896	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 897
 898	vchan_dma_desc_free_list(&vchan->vc, &head);
 899
 900	return 0;
 901}
 902
 903static int sun4i_dma_config(struct dma_chan *chan,
 904			    struct dma_slave_config *config)
 905{
 906	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 907
 908	memcpy(&vchan->cfg, config, sizeof(*config));
 909
 910	return 0;
 911}
 912
 913static struct dma_chan *sun4i_dma_of_xlate(struct of_phandle_args *dma_spec,
 914					   struct of_dma *ofdma)
 915{
 916	struct sun4i_dma_dev *priv = ofdma->of_dma_data;
 917	struct sun4i_dma_vchan *vchan;
 918	struct dma_chan *chan;
 919	u8 is_dedicated = dma_spec->args[0];
 920	u8 endpoint = dma_spec->args[1];
 921
 922	/* Check if type is Normal or Dedicated */
 923	if (is_dedicated != 0 && is_dedicated != 1)
 924		return NULL;
 925
 926	/* Make sure the endpoint looks sane */
 927	if ((is_dedicated && endpoint >= SUN4I_DDMA_DRQ_TYPE_LIMIT) ||
 928	    (!is_dedicated && endpoint >= SUN4I_NDMA_DRQ_TYPE_LIMIT))
 929		return NULL;
 930
 931	chan = dma_get_any_slave_channel(&priv->slave);
 932	if (!chan)
 933		return NULL;
 934
 935	/* Assign the endpoint to the vchan */
 936	vchan = to_sun4i_dma_vchan(chan);
 937	vchan->is_dedicated = is_dedicated;
 938	vchan->endpoint = endpoint;
 939
 940	return chan;
 941}
 942
 943static enum dma_status sun4i_dma_tx_status(struct dma_chan *chan,
 944					   dma_cookie_t cookie,
 945					   struct dma_tx_state *state)
 946{
 947	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 948	struct sun4i_dma_pchan *pchan = vchan->pchan;
 949	struct sun4i_dma_contract *contract;
 950	struct sun4i_dma_promise *promise;
 951	struct virt_dma_desc *vd;
 952	unsigned long flags;
 953	enum dma_status ret;
 954	size_t bytes = 0;
 955
 956	ret = dma_cookie_status(chan, cookie, state);
 957	if (!state || (ret == DMA_COMPLETE))
 958		return ret;
 959
 960	spin_lock_irqsave(&vchan->vc.lock, flags);
 961	vd = vchan_find_desc(&vchan->vc, cookie);
 962	if (!vd)
 963		goto exit;
 964	contract = to_sun4i_dma_contract(vd);
 965
 966	list_for_each_entry(promise, &contract->demands, list)
 967		bytes += promise->len;
 968
 969	/*
 970	 * The hardware is configured to return the remaining byte
 971	 * quantity. If possible, replace the first listed element's
 972	 * full size with the actual remaining amount
 973	 */
 974	promise = list_first_entry_or_null(&contract->demands,
 975					   struct sun4i_dma_promise, list);
 976	if (promise && pchan) {
 977		bytes -= promise->len;
 978		if (pchan->is_dedicated)
 979			bytes += readl(pchan->base + SUN4I_DDMA_BYTE_COUNT_REG);
 980		else
 981			bytes += readl(pchan->base + SUN4I_NDMA_BYTE_COUNT_REG);
 982	}
 983
 984exit:
 985
 986	dma_set_residue(state, bytes);
 987	spin_unlock_irqrestore(&vchan->vc.lock, flags);
 988
 989	return ret;
 990}
 991
 992static void sun4i_dma_issue_pending(struct dma_chan *chan)
 993{
 994	struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
 995	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
 996	unsigned long flags;
 997
 998	spin_lock_irqsave(&vchan->vc.lock, flags);
 999
1000	/*
1001	 * If there are pending transactions for this vchan, push one of
1002	 * them into the engine to get the ball rolling.
1003	 */
1004	if (vchan_issue_pending(&vchan->vc))
1005		__execute_vchan_pending(priv, vchan);
1006
1007	spin_unlock_irqrestore(&vchan->vc.lock, flags);
1008}
1009
1010static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
1011{
1012	struct sun4i_dma_dev *priv = dev_id;
1013	struct sun4i_dma_pchan *pchans = priv->pchans, *pchan;
1014	struct sun4i_dma_vchan *vchan;
1015	struct sun4i_dma_contract *contract;
1016	struct sun4i_dma_promise *promise;
1017	unsigned long pendirq, irqs, disableirqs;
1018	int bit, i, free_room, allow_mitigation = 1;
1019
1020	pendirq = readl_relaxed(priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1021
1022handle_pending:
1023
1024	disableirqs = 0;
1025	free_room = 0;
1026
1027	for_each_set_bit(bit, &pendirq, 32) {
1028		pchan = &pchans[bit >> 1];
1029		vchan = pchan->vchan;
1030		if (!vchan) /* a terminated channel may still interrupt */
1031			continue;
1032		contract = vchan->contract;
1033
1034		/*
1035		 * Disable the IRQ and free the pchan if it's an end
1036		 * interrupt (odd bit)
1037		 */
1038		if (bit & 1) {
1039			spin_lock(&vchan->vc.lock);
1040
1041			/*
1042			 * Move the promise into the completed list now that
1043			 * we're done with it
1044			 */
1045			list_del(&vchan->processing->list);
1046			list_add_tail(&vchan->processing->list,
1047				      &contract->completed_demands);
1048
1049			/*
1050			 * Cyclic DMA transfers are special:
1051			 * - There's always something we can dispatch
1052			 * - We need to run the callback
1053			 * - Latency is very important, as this is used by audio
1054			 * We therefore just cycle through the list and dispatch
1055			 * whatever we have here, reusing the pchan. There's
1056			 * no need to run the thread after this.
1057			 *
1058			 * For non-cyclic transfers we need to look around,
1059			 * so we can program some more work, or notify the
1060			 * client that their transfers have been completed.
1061			 */
1062			if (contract->is_cyclic) {
1063				promise = get_next_cyclic_promise(contract);
1064				vchan->processing = promise;
1065				configure_pchan(pchan, promise);
1066				vchan_cyclic_callback(&contract->vd);
1067			} else {
1068				vchan->processing = NULL;
1069				vchan->pchan = NULL;
1070
1071				free_room = 1;
1072				disableirqs |= BIT(bit);
1073				release_pchan(priv, pchan);
1074			}
1075
1076			spin_unlock(&vchan->vc.lock);
1077		} else {
1078			/* Half done interrupt */
1079			if (contract->is_cyclic)
1080				vchan_cyclic_callback(&contract->vd);
1081			else
1082				disableirqs |= BIT(bit);
1083		}
1084	}
1085
1086	/* Disable the IRQs for events we handled */
1087	spin_lock(&priv->lock);
1088	irqs = readl_relaxed(priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
1089	writel_relaxed(irqs & ~disableirqs,
1090		       priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
1091	spin_unlock(&priv->lock);
1092
1093	/* Writing 1 to the pending field will clear the pending interrupt */
1094	writel_relaxed(pendirq, priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1095
1096	/*
1097	 * If a pchan was freed, we may be able to schedule something else,
1098	 * so have a look around
1099	 */
1100	if (free_room) {
1101		for (i = 0; i < SUN4I_DMA_NR_MAX_VCHANS; i++) {
1102			vchan = &priv->vchans[i];
1103			spin_lock(&vchan->vc.lock);
1104			__execute_vchan_pending(priv, vchan);
1105			spin_unlock(&vchan->vc.lock);
1106		}
1107	}
1108
1109	/*
1110	 * Handle newer interrupts if some showed up, but only do it once
1111	 * to avoid a too long a loop
1112	 */
1113	if (allow_mitigation) {
1114		pendirq = readl_relaxed(priv->base +
1115					SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1116		if (pendirq) {
1117			allow_mitigation = 0;
1118			goto handle_pending;
1119		}
1120	}
1121
1122	return IRQ_HANDLED;
1123}
1124
1125static int sun4i_dma_probe(struct platform_device *pdev)
1126{
1127	struct sun4i_dma_dev *priv;
1128	struct resource *res;
1129	int i, j, ret;
1130
1131	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1132	if (!priv)
1133		return -ENOMEM;
1134
1135	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1136	priv->base = devm_ioremap_resource(&pdev->dev, res);
1137	if (IS_ERR(priv->base))
1138		return PTR_ERR(priv->base);
1139
1140	priv->irq = platform_get_irq(pdev, 0);
1141	if (priv->irq < 0)
1142		return priv->irq;
1143
1144	priv->clk = devm_clk_get(&pdev->dev, NULL);
1145	if (IS_ERR(priv->clk)) {
1146		dev_err(&pdev->dev, "No clock specified\n");
1147		return PTR_ERR(priv->clk);
1148	}
1149
1150	platform_set_drvdata(pdev, priv);
1151	spin_lock_init(&priv->lock);
1152
 
 
1153	dma_cap_zero(priv->slave.cap_mask);
1154	dma_cap_set(DMA_PRIVATE, priv->slave.cap_mask);
1155	dma_cap_set(DMA_MEMCPY, priv->slave.cap_mask);
1156	dma_cap_set(DMA_CYCLIC, priv->slave.cap_mask);
1157	dma_cap_set(DMA_SLAVE, priv->slave.cap_mask);
1158
1159	INIT_LIST_HEAD(&priv->slave.channels);
1160	priv->slave.device_free_chan_resources	= sun4i_dma_free_chan_resources;
1161	priv->slave.device_tx_status		= sun4i_dma_tx_status;
1162	priv->slave.device_issue_pending	= sun4i_dma_issue_pending;
1163	priv->slave.device_prep_slave_sg	= sun4i_dma_prep_slave_sg;
1164	priv->slave.device_prep_dma_memcpy	= sun4i_dma_prep_dma_memcpy;
1165	priv->slave.device_prep_dma_cyclic	= sun4i_dma_prep_dma_cyclic;
1166	priv->slave.device_config		= sun4i_dma_config;
1167	priv->slave.device_terminate_all	= sun4i_dma_terminate_all;
1168	priv->slave.copy_align			= 2;
1169	priv->slave.src_addr_widths		= BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1170						  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1171						  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1172	priv->slave.dst_addr_widths		= BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1173						  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1174						  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1175	priv->slave.directions			= BIT(DMA_DEV_TO_MEM) |
1176						  BIT(DMA_MEM_TO_DEV);
1177	priv->slave.residue_granularity		= DMA_RESIDUE_GRANULARITY_BURST;
1178
1179	priv->slave.dev = &pdev->dev;
1180
1181	priv->pchans = devm_kcalloc(&pdev->dev, SUN4I_DMA_NR_MAX_CHANNELS,
1182				    sizeof(struct sun4i_dma_pchan), GFP_KERNEL);
1183	priv->vchans = devm_kcalloc(&pdev->dev, SUN4I_DMA_NR_MAX_VCHANS,
1184				    sizeof(struct sun4i_dma_vchan), GFP_KERNEL);
1185	if (!priv->vchans || !priv->pchans)
1186		return -ENOMEM;
1187
1188	/*
1189	 * [0..SUN4I_NDMA_NR_MAX_CHANNELS) are normal pchans, and
1190	 * [SUN4I_NDMA_NR_MAX_CHANNELS..SUN4I_DMA_NR_MAX_CHANNELS) are
1191	 * dedicated ones
1192	 */
1193	for (i = 0; i < SUN4I_NDMA_NR_MAX_CHANNELS; i++)
1194		priv->pchans[i].base = priv->base +
1195			SUN4I_NDMA_CHANNEL_REG_BASE(i);
1196
1197	for (j = 0; i < SUN4I_DMA_NR_MAX_CHANNELS; i++, j++) {
1198		priv->pchans[i].base = priv->base +
1199			SUN4I_DDMA_CHANNEL_REG_BASE(j);
1200		priv->pchans[i].is_dedicated = 1;
1201	}
1202
1203	for (i = 0; i < SUN4I_DMA_NR_MAX_VCHANS; i++) {
1204		struct sun4i_dma_vchan *vchan = &priv->vchans[i];
1205
1206		spin_lock_init(&vchan->vc.lock);
1207		vchan->vc.desc_free = sun4i_dma_free_contract;
1208		vchan_init(&vchan->vc, &priv->slave);
1209	}
1210
1211	ret = clk_prepare_enable(priv->clk);
1212	if (ret) {
1213		dev_err(&pdev->dev, "Couldn't enable the clock\n");
1214		return ret;
1215	}
1216
1217	/*
1218	 * Make sure the IRQs are all disabled and accounted for. The bootloader
1219	 * likes to leave these dirty
1220	 */
1221	writel(0, priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
1222	writel(0xFFFFFFFF, priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1223
1224	ret = devm_request_irq(&pdev->dev, priv->irq, sun4i_dma_interrupt,
1225			       0, dev_name(&pdev->dev), priv);
1226	if (ret) {
1227		dev_err(&pdev->dev, "Cannot request IRQ\n");
1228		goto err_clk_disable;
1229	}
1230
1231	ret = dma_async_device_register(&priv->slave);
1232	if (ret) {
1233		dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
1234		goto err_clk_disable;
1235	}
1236
1237	ret = of_dma_controller_register(pdev->dev.of_node, sun4i_dma_of_xlate,
1238					 priv);
1239	if (ret) {
1240		dev_err(&pdev->dev, "of_dma_controller_register failed\n");
1241		goto err_dma_unregister;
1242	}
1243
1244	dev_dbg(&pdev->dev, "Successfully probed SUN4I_DMA\n");
1245
1246	return 0;
1247
1248err_dma_unregister:
1249	dma_async_device_unregister(&priv->slave);
1250err_clk_disable:
1251	clk_disable_unprepare(priv->clk);
1252	return ret;
1253}
1254
1255static int sun4i_dma_remove(struct platform_device *pdev)
1256{
1257	struct sun4i_dma_dev *priv = platform_get_drvdata(pdev);
1258
1259	/* Disable IRQ so no more work is scheduled */
1260	disable_irq(priv->irq);
1261
1262	of_dma_controller_free(pdev->dev.of_node);
1263	dma_async_device_unregister(&priv->slave);
1264
1265	clk_disable_unprepare(priv->clk);
1266
1267	return 0;
1268}
1269
1270static const struct of_device_id sun4i_dma_match[] = {
1271	{ .compatible = "allwinner,sun4i-a10-dma" },
1272	{ /* sentinel */ },
1273};
1274MODULE_DEVICE_TABLE(of, sun4i_dma_match);
1275
1276static struct platform_driver sun4i_dma_driver = {
1277	.probe	= sun4i_dma_probe,
1278	.remove	= sun4i_dma_remove,
1279	.driver	= {
1280		.name		= "sun4i-dma",
1281		.of_match_table	= sun4i_dma_match,
1282	},
1283};
1284
1285module_platform_driver(sun4i_dma_driver);
1286
1287MODULE_DESCRIPTION("Allwinner A10 Dedicated DMA Controller Driver");
1288MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
1289MODULE_LICENSE("GPL");