brintos

brintos / linux-shallow public Read only

0
0
Text · 21.9 KiB · 2342bbb Raw
886 lines · c
1// SPDX-License-Identifier: GPL-2.02//3// Renesas R-Car Audio DMAC support4//5// Copyright (C) 2015 Renesas Electronics Corp.6// Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>7 8#include <linux/delay.h>9#include <linux/of_dma.h>10#include <sound/dmaengine_pcm.h>11#include "rsnd.h"12 13/*14 * Audio DMAC peri peri register15 */16#define PDMASAR		0x0017#define PDMADAR		0x0418#define PDMACHCR	0x0c19 20/* PDMACHCR */21#define PDMACHCR_DE		(1 << 0)22 23 24struct rsnd_dmaen {25	struct dma_chan		*chan;26};27 28struct rsnd_dmapp {29	int			dmapp_id;30	u32			chcr;31};32 33struct rsnd_dma {34	struct rsnd_mod		mod;35	struct rsnd_mod		*mod_from;36	struct rsnd_mod		*mod_to;37	dma_addr_t		src_addr;38	dma_addr_t		dst_addr;39	union {40		struct rsnd_dmaen en;41		struct rsnd_dmapp pp;42	} dma;43};44 45struct rsnd_dma_ctrl {46	void __iomem *ppbase;47	phys_addr_t ppres;48	int dmaen_num;49	int dmapp_num;50};51 52#define rsnd_priv_to_dmac(p)	((struct rsnd_dma_ctrl *)(p)->dma)53#define rsnd_mod_to_dma(_mod) container_of((_mod), struct rsnd_dma, mod)54#define rsnd_dma_to_dmaen(dma)	(&(dma)->dma.en)55#define rsnd_dma_to_dmapp(dma)	(&(dma)->dma.pp)56 57/* for DEBUG */58static struct rsnd_mod_ops mem_ops = {59	.name = "mem",60};61 62static struct rsnd_mod mem = {63};64 65/*66 *		Audio DMAC67 */68static struct dma_chan *rsnd_dmaen_request_channel(struct rsnd_dai_stream *io,69						   struct rsnd_mod *mod_from,70						   struct rsnd_mod *mod_to)71{72	if ((!mod_from && !mod_to) ||73	    (mod_from && mod_to))74		return NULL;75 76	if (mod_from)77		return rsnd_mod_dma_req(io, mod_from);78	else79		return rsnd_mod_dma_req(io, mod_to);80}81 82static int rsnd_dmaen_stop(struct rsnd_mod *mod,83			   struct rsnd_dai_stream *io,84			   struct rsnd_priv *priv)85{86	return snd_dmaengine_pcm_trigger(io->substream, SNDRV_PCM_TRIGGER_STOP);87}88 89static int rsnd_dmaen_cleanup(struct rsnd_mod *mod,90			      struct rsnd_dai_stream *io,91			      struct rsnd_priv *priv)92{93	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);94	struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);95 96	/*97	 * DMAEngine release uses mutex lock.98	 * Thus, it shouldn't be called under spinlock.99	 * Let's call it under prepare100	 */101	if (dmaen->chan)102		snd_dmaengine_pcm_close_release_chan(io->substream);103 104	dmaen->chan = NULL;105 106	return 0;107}108 109static int rsnd_dmaen_prepare(struct rsnd_mod *mod,110			      struct rsnd_dai_stream *io,111			      struct rsnd_priv *priv)112{113	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);114	struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);115	struct device *dev = rsnd_priv_to_dev(priv);116 117	/* maybe suspended */118	if (dmaen->chan)119		return 0;120 121	/*122	 * DMAEngine request uses mutex lock.123	 * Thus, it shouldn't be called under spinlock.124	 * Let's call it under prepare125	 */126	dmaen->chan = rsnd_dmaen_request_channel(io,127						 dma->mod_from,128						 dma->mod_to);129	if (IS_ERR_OR_NULL(dmaen->chan)) {130		dmaen->chan = NULL;131		dev_err(dev, "can't get dma channel\n");132		return -EIO;133	}134 135	return snd_dmaengine_pcm_open(io->substream, dmaen->chan);136}137 138static int rsnd_dmaen_start(struct rsnd_mod *mod,139			    struct rsnd_dai_stream *io,140			    struct rsnd_priv *priv)141{142	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);143	struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);144	struct device *dev = rsnd_priv_to_dev(priv);145	struct dma_slave_config cfg = {};146	enum dma_slave_buswidth buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;147	int ret;148 149	/*150	 * in case of monaural data writing or reading through Audio-DMAC151	 * data is always in Left Justified format, so both src and dst152	 * DMA Bus width need to be set equal to physical data width.153	 */154	if (rsnd_runtime_channel_original(io) == 1) {155		struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);156		int bits = snd_pcm_format_physical_width(runtime->format);157 158		switch (bits) {159		case 8:160			buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;161			break;162		case 16:163			buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;164			break;165		case 32:166			buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;167			break;168		default:169			dev_err(dev, "invalid format width %d\n", bits);170			return -EINVAL;171		}172	}173 174	cfg.direction	= snd_pcm_substream_to_dma_direction(io->substream);175	cfg.src_addr	= dma->src_addr;176	cfg.dst_addr	= dma->dst_addr;177	cfg.src_addr_width = buswidth;178	cfg.dst_addr_width = buswidth;179 180	dev_dbg(dev, "%s %pad -> %pad\n",181		rsnd_mod_name(mod),182		&cfg.src_addr, &cfg.dst_addr);183 184	ret = dmaengine_slave_config(dmaen->chan, &cfg);185	if (ret < 0)186		return ret;187 188	return snd_dmaengine_pcm_trigger(io->substream, SNDRV_PCM_TRIGGER_START);189}190 191struct dma_chan *rsnd_dma_request_channel(struct device_node *of_node, char *name,192					  struct rsnd_mod *mod, char *x)193{194	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);195	struct device *dev = rsnd_priv_to_dev(priv);196	struct dma_chan *chan = NULL;197	struct device_node *np;198	int i = 0;199 200	for_each_child_of_node(of_node, np) {201		i = rsnd_node_fixed_index(dev, np, name, i);202		if (i < 0) {203			chan = NULL;204			of_node_put(np);205			break;206		}207 208		if (i == rsnd_mod_id_raw(mod) && (!chan))209			chan = of_dma_request_slave_channel(np, x);210		i++;211	}212 213	/* It should call of_node_put(), since, it is rsnd_xxx_of_node() */214	of_node_put(of_node);215 216	return chan;217}218 219static int rsnd_dmaen_attach(struct rsnd_dai_stream *io,220			   struct rsnd_dma *dma,221			   struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)222{223	struct rsnd_priv *priv = rsnd_io_to_priv(io);224	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);225	struct dma_chan *chan;226 227	/* try to get DMAEngine channel */228	chan = rsnd_dmaen_request_channel(io, mod_from, mod_to);229	if (IS_ERR_OR_NULL(chan)) {230		/* Let's follow when -EPROBE_DEFER case */231		if (PTR_ERR(chan) == -EPROBE_DEFER)232			return PTR_ERR(chan);233 234		/*235		 * DMA failed. try to PIO mode236		 * see237		 *	rsnd_ssi_fallback()238		 *	rsnd_rdai_continuance_probe()239		 */240		return -EAGAIN;241	}242 243	/*244	 * use it for IPMMU if needed245	 * see246	 *	rsnd_preallocate_pages()247	 */248	io->dmac_dev = chan->device->dev;249 250	dma_release_channel(chan);251 252	dmac->dmaen_num++;253 254	return 0;255}256 257static int rsnd_dmaen_pointer(struct rsnd_mod *mod,258			      struct rsnd_dai_stream *io,259			      snd_pcm_uframes_t *pointer)260{261	*pointer = snd_dmaengine_pcm_pointer(io->substream);262 263	return 0;264}265 266static struct rsnd_mod_ops rsnd_dmaen_ops = {267	.name		= "audmac",268	.prepare	= rsnd_dmaen_prepare,269	.cleanup	= rsnd_dmaen_cleanup,270	.start		= rsnd_dmaen_start,271	.stop		= rsnd_dmaen_stop,272	.pointer	= rsnd_dmaen_pointer,273	.get_status	= rsnd_mod_get_status,274};275 276/*277 *		Audio DMAC peri peri278 */279static const u8 gen2_id_table_ssiu[] = {280	/* SSI00 ~ SSI07 */281	0x00, 0x01, 0x02, 0x03, 0x39, 0x3a, 0x3b, 0x3c,282	/* SSI10 ~ SSI17 */283	0x04, 0x05, 0x06, 0x07, 0x3d, 0x3e, 0x3f, 0x40,284	/* SSI20 ~ SSI27 */285	0x08, 0x09, 0x0a, 0x0b, 0x41, 0x42, 0x43, 0x44,286	/* SSI30 ~ SSI37 */287	0x0c, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b,288	/* SSI40 ~ SSI47 */289	0x0d, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52,290	/* SSI5 */291	0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,292	/* SSI6 */293	0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,294	/* SSI7 */295	0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,296	/* SSI8 */297	0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,298	/* SSI90 ~ SSI97 */299	0x12, 0x13, 0x14, 0x15, 0x53, 0x54, 0x55, 0x56,300};301static const u8 gen2_id_table_scu[] = {302	0x2d, /* SCU_SRCI0 */303	0x2e, /* SCU_SRCI1 */304	0x2f, /* SCU_SRCI2 */305	0x30, /* SCU_SRCI3 */306	0x31, /* SCU_SRCI4 */307	0x32, /* SCU_SRCI5 */308	0x33, /* SCU_SRCI6 */309	0x34, /* SCU_SRCI7 */310	0x35, /* SCU_SRCI8 */311	0x36, /* SCU_SRCI9 */312};313static const u8 gen2_id_table_cmd[] = {314	0x37, /* SCU_CMD0 */315	0x38, /* SCU_CMD1 */316};317 318static u32 rsnd_dmapp_get_id(struct rsnd_dai_stream *io,319			     struct rsnd_mod *mod)320{321	struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);322	struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);323	struct rsnd_mod *src = rsnd_io_to_mod_src(io);324	struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);325	const u8 *entry = NULL;326	int id = 255;327	int size = 0;328 329	if ((mod == ssi) ||330	    (mod == ssiu)) {331		int busif = rsnd_mod_id_sub(ssiu);332 333		entry = gen2_id_table_ssiu;334		size = ARRAY_SIZE(gen2_id_table_ssiu);335		id = (rsnd_mod_id(mod) * 8) + busif;336	} else if (mod == src) {337		entry = gen2_id_table_scu;338		size = ARRAY_SIZE(gen2_id_table_scu);339		id = rsnd_mod_id(mod);340	} else if (mod == dvc) {341		entry = gen2_id_table_cmd;342		size = ARRAY_SIZE(gen2_id_table_cmd);343		id = rsnd_mod_id(mod);344	}345 346	if ((!entry) || (size <= id)) {347		struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));348 349		dev_err(dev, "unknown connection (%s)\n", rsnd_mod_name(mod));350 351		/* use non-prohibited SRS number as error */352		return 0x00; /* SSI00 */353	}354 355	return entry[id];356}357 358static u32 rsnd_dmapp_get_chcr(struct rsnd_dai_stream *io,359			       struct rsnd_mod *mod_from,360			       struct rsnd_mod *mod_to)361{362	return	(rsnd_dmapp_get_id(io, mod_from) << 24) +363		(rsnd_dmapp_get_id(io, mod_to) << 16);364}365 366#define rsnd_dmapp_addr(dmac, dma, reg) \367	(dmac->ppbase + 0x20 + reg + \368	 (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id))369static void rsnd_dmapp_write(struct rsnd_dma *dma, u32 data, u32 reg)370{371	struct rsnd_mod *mod = rsnd_mod_get(dma);372	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);373	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);374	struct device *dev = rsnd_priv_to_dev(priv);375 376	dev_dbg(dev, "w 0x%px : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data);377 378	iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg));379}380 381static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg)382{383	struct rsnd_mod *mod = rsnd_mod_get(dma);384	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);385	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);386 387	return ioread32(rsnd_dmapp_addr(dmac, dma, reg));388}389 390static void rsnd_dmapp_bset(struct rsnd_dma *dma, u32 data, u32 mask, u32 reg)391{392	struct rsnd_mod *mod = rsnd_mod_get(dma);393	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);394	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);395	void __iomem *addr = rsnd_dmapp_addr(dmac, dma, reg);396	u32 val = ioread32(addr);397 398	val &= ~mask;399	val |= (data & mask);400 401	iowrite32(val, addr);402}403 404static int rsnd_dmapp_stop(struct rsnd_mod *mod,405			   struct rsnd_dai_stream *io,406			   struct rsnd_priv *priv)407{408	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);409	int i;410 411	rsnd_dmapp_bset(dma, 0,  PDMACHCR_DE, PDMACHCR);412 413	for (i = 0; i < 1024; i++) {414		if (0 == (rsnd_dmapp_read(dma, PDMACHCR) & PDMACHCR_DE))415			return 0;416		udelay(1);417	}418 419	return -EIO;420}421 422static int rsnd_dmapp_start(struct rsnd_mod *mod,423			    struct rsnd_dai_stream *io,424			    struct rsnd_priv *priv)425{426	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);427	struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);428 429	rsnd_dmapp_write(dma, dma->src_addr,	PDMASAR);430	rsnd_dmapp_write(dma, dma->dst_addr,	PDMADAR);431	rsnd_dmapp_write(dma, dmapp->chcr,	PDMACHCR);432 433	return 0;434}435 436static int rsnd_dmapp_attach(struct rsnd_dai_stream *io,437			     struct rsnd_dma *dma,438			     struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)439{440	struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);441	struct rsnd_priv *priv = rsnd_io_to_priv(io);442	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);443	struct device *dev = rsnd_priv_to_dev(priv);444 445	dmapp->dmapp_id = dmac->dmapp_num;446	dmapp->chcr = rsnd_dmapp_get_chcr(io, mod_from, mod_to) | PDMACHCR_DE;447 448	dmac->dmapp_num++;449 450	dev_dbg(dev, "id/src/dst/chcr = %d/%pad/%pad/%08x\n",451		dmapp->dmapp_id, &dma->src_addr, &dma->dst_addr, dmapp->chcr);452 453	return 0;454}455 456#ifdef CONFIG_DEBUG_FS457static void rsnd_dmapp_debug_info(struct seq_file *m,458				  struct rsnd_dai_stream *io,459				  struct rsnd_mod *mod)460{461	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);462	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);463	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);464	struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);465 466	rsnd_debugfs_reg_show(m, dmac->ppres, dmac->ppbase,467			      0x20 + 0x10 * dmapp->dmapp_id, 0x10);468}469#define DEBUG_INFO .debug_info = rsnd_dmapp_debug_info470#else471#define DEBUG_INFO472#endif473 474static struct rsnd_mod_ops rsnd_dmapp_ops = {475	.name		= "audmac-pp",476	.start		= rsnd_dmapp_start,477	.stop		= rsnd_dmapp_stop,478	.quit		= rsnd_dmapp_stop,479	.get_status	= rsnd_mod_get_status,480	DEBUG_INFO481};482 483/*484 *		Common DMAC Interface485 */486 487/*488 *	DMA read/write register offset489 *490 *	RSND_xxx_I_N	for Audio DMAC input491 *	RSND_xxx_O_N	for Audio DMAC output492 *	RSND_xxx_I_P	for Audio DMAC peri peri input493 *	RSND_xxx_O_P	for Audio DMAC peri peri output494 *495 *	ex) R-Car H2 case496 *	      mod        / DMAC in    / DMAC out   / DMAC PP in / DMAC pp out497 *	SSI : 0xec541000 / 0xec241008 / 0xec24100c498 *	SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000499 *	SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000500 *	CMD : 0xec500000 /            / 0xec008000                0xec308000501 */502#define RDMA_SSI_I_N(addr, i)	(addr ##_reg - 0x00300000 + (0x40 * i) + 0x8)503#define RDMA_SSI_O_N(addr, i)	(addr ##_reg - 0x00300000 + (0x40 * i) + 0xc)504 505#define RDMA_SSIU_I_N(addr, i, j) (addr ##_reg - 0x00441000 + (0x1000 * (i)) + (((j) / 4) * 0xA000) + (((j) % 4) * 0x400) - (0x4000 * ((i) / 9) * ((j) / 4)))506#define RDMA_SSIU_O_N(addr, i, j) RDMA_SSIU_I_N(addr, i, j)507 508#define RDMA_SSIU_I_P(addr, i, j) (addr ##_reg - 0x00141000 + (0x1000 * (i)) + (((j) / 4) * 0xA000) + (((j) % 4) * 0x400) - (0x4000 * ((i) / 9) * ((j) / 4)))509#define RDMA_SSIU_O_P(addr, i, j) RDMA_SSIU_I_P(addr, i, j)510 511#define RDMA_SRC_I_N(addr, i)	(addr ##_reg - 0x00500000 + (0x400 * i))512#define RDMA_SRC_O_N(addr, i)	(addr ##_reg - 0x004fc000 + (0x400 * i))513 514#define RDMA_SRC_I_P(addr, i)	(addr ##_reg - 0x00200000 + (0x400 * i))515#define RDMA_SRC_O_P(addr, i)	(addr ##_reg - 0x001fc000 + (0x400 * i))516 517#define RDMA_CMD_O_N(addr, i)	(addr ##_reg - 0x004f8000 + (0x400 * i))518#define RDMA_CMD_O_P(addr, i)	(addr ##_reg - 0x001f8000 + (0x400 * i))519 520static dma_addr_t521rsnd_gen2_dma_addr(struct rsnd_dai_stream *io,522		   struct rsnd_mod *mod,523		   int is_play, int is_from)524{525	struct rsnd_priv *priv = rsnd_io_to_priv(io);526	struct device *dev = rsnd_priv_to_dev(priv);527	phys_addr_t ssi_reg = rsnd_gen_get_phy_addr(priv, RSND_BASE_SSI);528	phys_addr_t src_reg = rsnd_gen_get_phy_addr(priv, RSND_BASE_SCU);529	int is_ssi = !!(rsnd_io_to_mod_ssi(io) == mod) ||530		     !!(rsnd_io_to_mod_ssiu(io) == mod);531	int use_src = !!rsnd_io_to_mod_src(io);532	int use_cmd = !!rsnd_io_to_mod_dvc(io) ||533		      !!rsnd_io_to_mod_mix(io) ||534		      !!rsnd_io_to_mod_ctu(io);535	int id = rsnd_mod_id(mod);536	int busif = rsnd_mod_id_sub(rsnd_io_to_mod_ssiu(io));537	struct dma_addr {538		dma_addr_t out_addr;539		dma_addr_t in_addr;540	} dma_addrs[3][2][3] = {541		/* SRC */542		/* Capture */543		{{{ 0,				0 },544		  { RDMA_SRC_O_N(src, id),	RDMA_SRC_I_P(src, id) },545		  { RDMA_CMD_O_N(src, id),	RDMA_SRC_I_P(src, id) } },546		 /* Playback */547		 {{ 0,				0, },548		  { RDMA_SRC_O_P(src, id),	RDMA_SRC_I_N(src, id) },549		  { RDMA_CMD_O_P(src, id),	RDMA_SRC_I_N(src, id) } }550		},551		/* SSI */552		/* Capture */553		{{{ RDMA_SSI_O_N(ssi, id),		0 },554		  { RDMA_SSIU_O_P(ssi, id, busif),	0 },555		  { RDMA_SSIU_O_P(ssi, id, busif),	0 } },556		 /* Playback */557		 {{ 0,			RDMA_SSI_I_N(ssi, id) },558		  { 0,			RDMA_SSIU_I_P(ssi, id, busif) },559		  { 0,			RDMA_SSIU_I_P(ssi, id, busif) } }560		},561		/* SSIU */562		/* Capture */563		{{{ RDMA_SSIU_O_N(ssi, id, busif),	0 },564		  { RDMA_SSIU_O_P(ssi, id, busif),	0 },565		  { RDMA_SSIU_O_P(ssi, id, busif),	0 } },566		 /* Playback */567		 {{ 0,			RDMA_SSIU_I_N(ssi, id, busif) },568		  { 0,			RDMA_SSIU_I_P(ssi, id, busif) },569		  { 0,			RDMA_SSIU_I_P(ssi, id, busif) } } },570	};571 572	/*573	 * FIXME574	 *575	 * We can't support SSI9-4/5/6/7, because its address is576	 * out of calculation rule577	 */578	if ((id == 9) && (busif >= 4))579		dev_err(dev, "This driver doesn't support SSI%d-%d, so far",580			id, busif);581 582	/* it shouldn't happen */583	if (use_cmd && !use_src)584		dev_err(dev, "DVC is selected without SRC\n");585 586	/* use SSIU or SSI ? */587	if (is_ssi && rsnd_ssi_use_busif(io))588		is_ssi++;589 590	return (is_from) ?591		dma_addrs[is_ssi][is_play][use_src + use_cmd].out_addr :592		dma_addrs[is_ssi][is_play][use_src + use_cmd].in_addr;593}594 595/*596 *	Gen4 DMA read/write register offset597 *598 *	ex) R-Car V4H case599 *		  mod		/ SYS-DMAC in	/ SYS-DMAC out600 *	SSI_SDMC: 0xec400000	/ 0xec400000	/ 0xec400000601 */602#define RDMA_SSI_SDMC(addr, i)	(addr + (0x8000 * i))603static dma_addr_t604rsnd_gen4_dma_addr(struct rsnd_dai_stream *io, struct rsnd_mod *mod,605		   int is_play, int is_from)606{607	struct rsnd_priv *priv = rsnd_io_to_priv(io);608	phys_addr_t addr = rsnd_gen_get_phy_addr(priv, RSND_BASE_SDMC);609	int id = rsnd_mod_id(mod);610	int busif = rsnd_mod_id_sub(mod);611 612	/*613	 * SSI0 only is supported614	 */615	if (id != 0) {616		struct device *dev = rsnd_priv_to_dev(priv);617 618		dev_err(dev, "This driver doesn't support non SSI0");619		return -EINVAL;620	}621 622	return RDMA_SSI_SDMC(addr, busif);623}624 625static dma_addr_t rsnd_dma_addr(struct rsnd_dai_stream *io,626				struct rsnd_mod *mod,627				int is_play, int is_from)628{629	struct rsnd_priv *priv = rsnd_io_to_priv(io);630 631	if (!mod)632		return 0;633 634	/*635	 * gen1 uses default DMA addr636	 */637	if (rsnd_is_gen1(priv))638		return 0;639	else if (rsnd_is_gen4(priv))640		return rsnd_gen4_dma_addr(io, mod, is_play, is_from);641	else642		return rsnd_gen2_dma_addr(io, mod, is_play, is_from);643}644 645#define MOD_MAX (RSND_MOD_MAX + 1) /* +Memory */646static void rsnd_dma_of_path(struct rsnd_mod *this,647			     struct rsnd_dai_stream *io,648			     int is_play,649			     struct rsnd_mod **mod_from,650			     struct rsnd_mod **mod_to)651{652	struct rsnd_mod *ssi;653	struct rsnd_mod *src = rsnd_io_to_mod_src(io);654	struct rsnd_mod *ctu = rsnd_io_to_mod_ctu(io);655	struct rsnd_mod *mix = rsnd_io_to_mod_mix(io);656	struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);657	struct rsnd_mod *mod[MOD_MAX];658	struct rsnd_mod *mod_start, *mod_end;659	struct rsnd_priv *priv = rsnd_mod_to_priv(this);660	struct device *dev = rsnd_priv_to_dev(priv);661	int nr, i, idx;662 663	/*664	 * It should use "rcar_sound,ssiu" on DT.665	 * But, we need to keep compatibility for old version.666	 *667	 * If it has "rcar_sound.ssiu", it will be used.668	 * If not, "rcar_sound.ssi" will be used.669	 * see670	 *	rsnd_ssiu_dma_req()671	 *	rsnd_ssi_dma_req()672	 */673	if (rsnd_ssiu_of_node(priv)) {674		struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);675 676		/* use SSIU */677		ssi = ssiu;678		if (this == rsnd_io_to_mod_ssi(io))679			this = ssiu;680	} else {681		/* keep compatible, use SSI */682		ssi = rsnd_io_to_mod_ssi(io);683	}684 685	if (!ssi)686		return;687 688	nr = 0;689	for (i = 0; i < MOD_MAX; i++) {690		mod[i] = NULL;691		nr += !!rsnd_io_to_mod(io, i);692	}693 694	/*695	 * [S] -*-> [E]696	 * [S] -*-> SRC -o-> [E]697	 * [S] -*-> SRC -> DVC -o-> [E]698	 * [S] -*-> SRC -> CTU -> MIX -> DVC -o-> [E]699	 *700	 * playback	[S] = mem701	 *		[E] = SSI702	 *703	 * capture	[S] = SSI704	 *		[E] = mem705	 *706	 * -*->		Audio DMAC707	 * -o->		Audio DMAC peri peri708	 */709	mod_start	= (is_play) ? NULL : ssi;710	mod_end		= (is_play) ? ssi  : NULL;711 712	idx = 0;713	mod[idx++] = mod_start;714	for (i = 1; i < nr; i++) {715		if (src) {716			mod[idx++] = src;717			src = NULL;718		} else if (ctu) {719			mod[idx++] = ctu;720			ctu = NULL;721		} else if (mix) {722			mod[idx++] = mix;723			mix = NULL;724		} else if (dvc) {725			mod[idx++] = dvc;726			dvc = NULL;727		}728	}729	mod[idx] = mod_end;730 731	/*732	 *		| SSI | SRC |733	 * -------------+-----+-----+734	 *  is_play	|  o  |  *  |735	 * !is_play	|  *  |  o  |736	 */737	if ((this == ssi) == (is_play)) {738		*mod_from	= mod[idx - 1];739		*mod_to		= mod[idx];740	} else {741		*mod_from	= mod[0];742		*mod_to		= mod[1];743	}744 745	dev_dbg(dev, "module connection (this is %s)\n", rsnd_mod_name(this));746	for (i = 0; i <= idx; i++) {747		dev_dbg(dev, "  %s%s\n",748			rsnd_mod_name(mod[i] ? mod[i] : &mem),749			(mod[i] == *mod_from) ? " from" :750			(mod[i] == *mod_to)   ? " to" : "");751	}752}753 754static int rsnd_dma_alloc(struct rsnd_dai_stream *io, struct rsnd_mod *mod,755			  struct rsnd_mod **dma_mod)756{757	struct rsnd_mod *mod_from = NULL;758	struct rsnd_mod *mod_to = NULL;759	struct rsnd_priv *priv = rsnd_io_to_priv(io);760	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);761	struct device *dev = rsnd_priv_to_dev(priv);762	struct rsnd_dma *dma;763	struct rsnd_mod_ops *ops;764	enum rsnd_mod_type type;765	int (*attach)(struct rsnd_dai_stream *io, struct rsnd_dma *dma,766		      struct rsnd_mod *mod_from, struct rsnd_mod *mod_to);767	int is_play = rsnd_io_is_play(io);768	int ret, dma_id;769 770	/*771	 * DMA failed. try to PIO mode772	 * see773	 *	rsnd_ssi_fallback()774	 *	rsnd_rdai_continuance_probe()775	 */776	if (!dmac)777		return -EAGAIN;778 779	rsnd_dma_of_path(mod, io, is_play, &mod_from, &mod_to);780 781	/* for Gen2 or later */782	if (mod_from && mod_to) {783		ops	= &rsnd_dmapp_ops;784		attach	= rsnd_dmapp_attach;785		dma_id	= dmac->dmapp_num;786		type	= RSND_MOD_AUDMAPP;787	} else {788		ops	= &rsnd_dmaen_ops;789		attach	= rsnd_dmaen_attach;790		dma_id	= dmac->dmaen_num;791		type	= RSND_MOD_AUDMA;792	}793 794	/* for Gen1, overwrite */795	if (rsnd_is_gen1(priv)) {796		ops	= &rsnd_dmaen_ops;797		attach	= rsnd_dmaen_attach;798		dma_id	= dmac->dmaen_num;799		type	= RSND_MOD_AUDMA;800	}801 802	dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);803	if (!dma)804		return -ENOMEM;805 806	*dma_mod = rsnd_mod_get(dma);807 808	ret = rsnd_mod_init(priv, *dma_mod, ops, NULL,809			    type, dma_id);810	if (ret < 0)811		return ret;812 813	dev_dbg(dev, "%s %s -> %s\n",814		rsnd_mod_name(*dma_mod),815		rsnd_mod_name(mod_from ? mod_from : &mem),816		rsnd_mod_name(mod_to   ? mod_to   : &mem));817 818	ret = attach(io, dma, mod_from, mod_to);819	if (ret < 0)820		return ret;821 822	dma->src_addr = rsnd_dma_addr(io, mod_from, is_play, 1);823	dma->dst_addr = rsnd_dma_addr(io, mod_to,   is_play, 0);824	dma->mod_from = mod_from;825	dma->mod_to   = mod_to;826 827	return 0;828}829 830int rsnd_dma_attach(struct rsnd_dai_stream *io, struct rsnd_mod *mod,831		    struct rsnd_mod **dma_mod)832{833	if (!(*dma_mod)) {834		int ret = rsnd_dma_alloc(io, mod, dma_mod);835 836		if (ret < 0)837			return ret;838	}839 840	return rsnd_dai_connect(*dma_mod, io, (*dma_mod)->type);841}842 843int rsnd_dma_probe(struct rsnd_priv *priv)844{845	struct platform_device *pdev = rsnd_priv_to_pdev(priv);846	struct device *dev = rsnd_priv_to_dev(priv);847	struct rsnd_dma_ctrl *dmac;848	struct resource *res;849 850	/*851	 * for Gen1852	 */853	if (rsnd_is_gen1(priv))854		return 0;855 856	/*857	 * for Gen2 or later858	 */859	dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL);860	if (!dmac) {861		dev_err(dev, "dma allocate failed\n");862		return 0; /* it will be PIO mode */863	}864 865	/* for Gen4 doesn't have DMA-pp */866	if (rsnd_is_gen4(priv))867		goto audmapp_end;868 869	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp");870	if (!res) {871		dev_err(dev, "lack of audmapp in DT\n");872		return 0; /* it will be PIO mode */873	}874 875	dmac->dmapp_num = 0;876	dmac->ppres  = res->start;877	dmac->ppbase = devm_ioremap_resource(dev, res);878	if (IS_ERR(dmac->ppbase))879		return PTR_ERR(dmac->ppbase);880audmapp_end:881	priv->dma = dmac;882 883	/* dummy mem mod for debug */884	return rsnd_mod_init(NULL, &mem, &mem_ops, NULL, 0, 0);885}886