// SPDX-License-Identifier: GPL-2.0-only
/*
* IMG Multi-threaded DMA Controller (MDC)
*
* Copyright (C) 2009,2012,2013 Imagination Technologies Ltd.
* Copyright (C) 2014 Google, Inc.
*/
#include <linux/clk.h>
#include <linux/dma-mapping.h>
#include <linux/dmaengine.h>
#include <linux/dmapool.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_dma.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include "dmaengine.h"
#include "virt-dma.h"
#define MDC_MAX_DMA_CHANNELS 32
#define MDC_GENERAL_CONFIG 0x000
#define MDC_GENERAL_CONFIG_LIST_IEN BIT(31)
#define MDC_GENERAL_CONFIG_IEN BIT(29)
#define MDC_GENERAL_CONFIG_LEVEL_INT BIT(28)
#define MDC_GENERAL_CONFIG_INC_W BIT(12)
#define MDC_GENERAL_CONFIG_INC_R BIT(8)
#define MDC_GENERAL_CONFIG_PHYSICAL_W BIT(7)
#define MDC_GENERAL_CONFIG_WIDTH_W_SHIFT 4
#define MDC_GENERAL_CONFIG_WIDTH_W_MASK 0x7
#define MDC_GENERAL_CONFIG_PHYSICAL_R BIT(3)
#define MDC_GENERAL_CONFIG_WIDTH_R_SHIFT 0
#define MDC_GENERAL_CONFIG_WIDTH_R_MASK 0x7
#define MDC_READ_PORT_CONFIG 0x004
#define MDC_READ_PORT_CONFIG_STHREAD_SHIFT 28
#define MDC_READ_PORT_CONFIG_STHREAD_MASK 0xf
#define MDC_READ_PORT_CONFIG_RTHREAD_SHIFT 24
#define MDC_READ_PORT_CONFIG_RTHREAD_MASK 0xf
#define MDC_READ_PORT_CONFIG_WTHREAD_SHIFT 16
#define MDC_READ_PORT_CONFIG_WTHREAD_MASK 0xf
#define MDC_READ_PORT_CONFIG_BURST_SIZE_SHIFT 4
#define MDC_READ_PORT_CONFIG_BURST_SIZE_MASK 0xff
#define MDC_READ_PORT_CONFIG_DREQ_ENABLE BIT(1)
#define MDC_READ_ADDRESS 0x008
#define MDC_WRITE_ADDRESS 0x00c
#define MDC_TRANSFER_SIZE 0x010
#define MDC_TRANSFER_SIZE_MASK 0xffffff
#define MDC_LIST_NODE_ADDRESS 0x014
#define MDC_CMDS_PROCESSED 0x018
#define MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT 16
#define MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK 0x3f
#define MDC_CMDS_PROCESSED_INT_ACTIVE BIT(8)
#define MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT 0
#define MDC_CMDS_PROCESSED_CMDS_DONE_MASK 0x3f
#define MDC_CONTROL_AND_STATUS 0x01c
#define MDC_CONTROL_AND_STATUS_CANCEL BIT(20)
#define MDC_CONTROL_AND_STATUS_LIST_EN BIT(4)
#define MDC_CONTROL_AND_STATUS_EN BIT(0)
#define MDC_ACTIVE_TRANSFER_SIZE 0x030
#define MDC_GLOBAL_CONFIG_A 0x900
#define MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_SHIFT 16
#define MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_MASK 0xff
#define MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_SHIFT 8
#define MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_MASK 0xff
#define MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_SHIFT 0
#define MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_MASK 0xff
struct mdc_hw_list_desc {
u32 gen_conf;
u32 readport_conf;
u32 read_addr;
u32 write_addr;
u32 xfer_size;
u32 node_addr;
u32 cmds_done;
u32 ctrl_status;
/*
* Not part of the list descriptor, but instead used by the CPU to
* traverse the list.
*/
struct mdc_hw_list_desc *next_desc;
};
struct mdc_tx_desc {
struct mdc_chan *chan;
struct virt_dma_desc vd;
dma_addr_t list_phys;
struct mdc_hw_list_desc *list;
bool cyclic;
bool cmd_loaded;
unsigned int list_len;
unsigned int list_period_len;
size_t list_xfer_size;
unsigned int list_cmds_done;
};
struct mdc_chan {
struct mdc_dma *mdma;
struct virt_dma_chan vc;
struct dma_slave_config config;
struct mdc_tx_desc *desc;
int irq;
unsigned int periph;
unsigned int thread;
unsigned int chan_nr;
};
struct mdc_dma_soc_data {
void (*enable_chan)(struct mdc_chan *mchan);
void (*disable_chan)(struct mdc_chan *mchan);
};
struct mdc_dma {
struct dma_device dma_dev;
void __iomem *regs;
struct clk *clk;
struct dma_pool *desc_pool;
struct regmap *periph_regs;
spinlock_t lock;
unsigned int nr_threads;
unsigned int nr_channels;
unsigned int bus_width;
unsigned int max_burst_mult;
unsigned int max_xfer_size;
const struct mdc_dma_soc_data *soc;
struct mdc_chan channels[MDC_MAX_DMA_CHANNELS];
};
static inline u32 mdc_readl(struct mdc_dma *mdma, u32 reg)
{
return readl(mdma->regs + reg);
}
static inline void mdc_writel(struct mdc_dma *mdma, u32 val, u32 reg)
{
writel(val, mdma->regs + reg);
}
static inline u32 mdc_chan_readl(struct mdc_chan *mchan, u32 reg)
{
return mdc_readl(mchan->mdma, mchan->chan_nr * 0x040 + reg);
}
static inline void mdc_chan_writel(struct mdc_chan *mchan, u32 val, u32 reg)
{
mdc_writel(mchan->mdma, val, mchan->chan_nr * 0x040 + reg);
}
static inline struct mdc_chan *to_mdc_chan(struct dma_chan *c)
{
return container_of(to_virt_chan(c), struct mdc_chan, vc);
}
static inline struct mdc_tx_desc *to_mdc_desc(struct dma_async_tx_descriptor *t)
{
struct virt_dma_desc *vdesc = container_of(t, struct virt_dma_desc, tx);
return container_of(vdesc, struct mdc_tx_desc, vd);
}
static inline struct device *mdma2dev(struct mdc_dma *mdma)
{
return mdma->dma_dev.dev;
}
static inline unsigned int to_mdc_width(unsigned int bytes)
{
return ffs(bytes) - 1;
}
static inline void mdc_set_read_width(struct mdc_hw_list_desc *ldesc,
unsigned int bytes)
{
ldesc->gen_conf |= to_mdc_width(bytes) <<
MDC_GENERAL_CONFIG_WIDTH_R_SHIFT;
}
static inline void mdc_set_write_width(struct mdc_hw_list_desc *ldesc,
unsigned int bytes)
{
ldesc->gen_conf |= to_mdc_width(bytes) <<
MDC_GENERAL_CONFIG_WIDTH_W_SHIFT;
}
static void mdc_list_desc_config(