xref: /linux/sound/soc/renesas/rcar/dma.c (revision 177bf8620cf4ed290ee170a6c5966adc0924b336)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car Audio DMAC support
4 //
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 register
15  */
16 #define PDMASAR		0x00
17 #define PDMADAR		0x04
18 #define PDMACHCR	0x0c
19 
20 /* PDMACHCR */
21 #define PDMACHCR_DE		(1 << 0)
22 
23 
24 struct rsnd_dmaen {
25 	struct dma_chan		*chan;
26 };
27 
28 struct rsnd_dmapp {
29 	int			dmapp_id;
30 	u32			chcr;
31 };
32 
33 struct 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 
45 struct 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 */
58 static struct rsnd_mod_ops mem_ops = {
59 	.name = "mem",
60 };
61 
62 static struct rsnd_mod mem = {
63 };
64 
65 /*
66  *		Audio DMAC
67  */
rsnd_dmaen_request_channel(struct rsnd_dai_stream * io,struct rsnd_mod * mod_from,struct rsnd_mod * mod_to)68 static 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 	else
79 		return rsnd_mod_dma_req(io, mod_to);
80 }
81 
rsnd_dmaen_stop(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)82 static 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 
rsnd_dmaen_cleanup(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)89 static 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 prepare
100 	 */
101 	if (dmaen->chan)
102 		snd_dmaengine_pcm_close_release_chan(io->substream);
103 
104 	dmaen->chan = NULL;
105 
106 	return 0;
107 }
108 
rsnd_dmaen_prepare(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)109 static 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 prepare
125 	 */
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 
rsnd_dmaen_start(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)138 static 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-DMAC
151 	 * data is always in Left Justified format, so both src and dst
152 	 * 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 
rsnd_dma_request_channel(struct device_node * of_node,char * name,struct rsnd_mod * mod,char * x)191 struct 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 	int i = 0;
198 
199 	for_each_child_of_node_scoped(of_node, np) {
200 		i = rsnd_node_fixed_index(dev, np, name, i);
201 		if (i < 0) {
202 			chan = NULL;
203 			break;
204 		}
205 
206 		if (i == rsnd_mod_id_raw(mod) && (!chan))
207 			chan = of_dma_request_slave_channel(np, x);
208 		i++;
209 	}
210 
211 	/* It should call of_node_put(), since, it is rsnd_xxx_of_node() */
212 	of_node_put(of_node);
213 
214 	return chan;
215 }
216 
rsnd_dmaen_attach(struct rsnd_dai_stream * io,struct rsnd_dma * dma,struct rsnd_mod * mod_from,struct rsnd_mod * mod_to)217 static int rsnd_dmaen_attach(struct rsnd_dai_stream *io,
218 			   struct rsnd_dma *dma,
219 			   struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
220 {
221 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
222 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
223 	struct dma_chan *chan;
224 
225 	/* try to get DMAEngine channel */
226 	chan = rsnd_dmaen_request_channel(io, mod_from, mod_to);
227 	if (IS_ERR_OR_NULL(chan)) {
228 		/* Let's follow when -EPROBE_DEFER case */
229 		if (PTR_ERR(chan) == -EPROBE_DEFER)
230 			return PTR_ERR(chan);
231 
232 		/*
233 		 * DMA failed. try to PIO mode
234 		 * see
235 		 *	rsnd_ssi_fallback()
236 		 *	rsnd_rdai_continuance_probe()
237 		 */
238 		return -EAGAIN;
239 	}
240 
241 	/*
242 	 * use it for IPMMU if needed
243 	 * see
244 	 *	rsnd_preallocate_pages()
245 	 */
246 	io->dmac_dev = chan->device->dev;
247 
248 	dma_release_channel(chan);
249 
250 	dmac->dmaen_num++;
251 
252 	return 0;
253 }
254 
rsnd_dmaen_pointer(struct rsnd_mod * mod,struct rsnd_dai_stream * io,snd_pcm_uframes_t * pointer)255 static int rsnd_dmaen_pointer(struct rsnd_mod *mod,
256 			      struct rsnd_dai_stream *io,
257 			      snd_pcm_uframes_t *pointer)
258 {
259 	*pointer = snd_dmaengine_pcm_pointer(io->substream);
260 
261 	return 0;
262 }
263 
264 static struct rsnd_mod_ops rsnd_dmaen_ops = {
265 	.name		= "audmac",
266 	.prepare	= rsnd_dmaen_prepare,
267 	.cleanup	= rsnd_dmaen_cleanup,
268 	.start		= rsnd_dmaen_start,
269 	.stop		= rsnd_dmaen_stop,
270 	.pointer	= rsnd_dmaen_pointer,
271 	.get_status	= rsnd_mod_get_status,
272 };
273 
274 /*
275  *		Audio DMAC peri peri
276  */
277 static const u8 gen2_id_table_ssiu[] = {
278 	/* SSI00 ~ SSI07 */
279 	0x00, 0x01, 0x02, 0x03, 0x39, 0x3a, 0x3b, 0x3c,
280 	/* SSI10 ~ SSI17 */
281 	0x04, 0x05, 0x06, 0x07, 0x3d, 0x3e, 0x3f, 0x40,
282 	/* SSI20 ~ SSI27 */
283 	0x08, 0x09, 0x0a, 0x0b, 0x41, 0x42, 0x43, 0x44,
284 	/* SSI30 ~ SSI37 */
285 	0x0c, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b,
286 	/* SSI40 ~ SSI47 */
287 	0x0d, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52,
288 	/* SSI5 */
289 	0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
290 	/* SSI6 */
291 	0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292 	/* SSI7 */
293 	0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294 	/* SSI8 */
295 	0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
296 	/* SSI90 ~ SSI97 */
297 	0x12, 0x13, 0x14, 0x15, 0x53, 0x54, 0x55, 0x56,
298 };
299 static const u8 gen2_id_table_scu[] = {
300 	0x2d, /* SCU_SRCI0 */
301 	0x2e, /* SCU_SRCI1 */
302 	0x2f, /* SCU_SRCI2 */
303 	0x30, /* SCU_SRCI3 */
304 	0x31, /* SCU_SRCI4 */
305 	0x32, /* SCU_SRCI5 */
306 	0x33, /* SCU_SRCI6 */
307 	0x34, /* SCU_SRCI7 */
308 	0x35, /* SCU_SRCI8 */
309 	0x36, /* SCU_SRCI9 */
310 };
311 static const u8 gen2_id_table_cmd[] = {
312 	0x37, /* SCU_CMD0 */
313 	0x38, /* SCU_CMD1 */
314 };
315 
rsnd_dmapp_get_id(struct rsnd_dai_stream * io,struct rsnd_mod * mod)316 static u32 rsnd_dmapp_get_id(struct rsnd_dai_stream *io,
317 			     struct rsnd_mod *mod)
318 {
319 	struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
320 	struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
321 	struct rsnd_mod *src = rsnd_io_to_mod_src(io);
322 	struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
323 	const u8 *entry = NULL;
324 	int id = 255;
325 	int size = 0;
326 
327 	if ((mod == ssi) ||
328 	    (mod == ssiu)) {
329 		int busif = rsnd_mod_id_sub(ssiu);
330 
331 		entry = gen2_id_table_ssiu;
332 		size = ARRAY_SIZE(gen2_id_table_ssiu);
333 		id = (rsnd_mod_id(mod) * 8) + busif;
334 	} else if (mod == src) {
335 		entry = gen2_id_table_scu;
336 		size = ARRAY_SIZE(gen2_id_table_scu);
337 		id = rsnd_mod_id(mod);
338 	} else if (mod == dvc) {
339 		entry = gen2_id_table_cmd;
340 		size = ARRAY_SIZE(gen2_id_table_cmd);
341 		id = rsnd_mod_id(mod);
342 	}
343 
344 	if ((!entry) || (size <= id)) {
345 		struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));
346 
347 		dev_err(dev, "unknown connection (%s)\n", rsnd_mod_name(mod));
348 
349 		/* use non-prohibited SRS number as error */
350 		return 0x00; /* SSI00 */
351 	}
352 
353 	return entry[id];
354 }
355 
rsnd_dmapp_get_chcr(struct rsnd_dai_stream * io,struct rsnd_mod * mod_from,struct rsnd_mod * mod_to)356 static u32 rsnd_dmapp_get_chcr(struct rsnd_dai_stream *io,
357 			       struct rsnd_mod *mod_from,
358 			       struct rsnd_mod *mod_to)
359 {
360 	return	(rsnd_dmapp_get_id(io, mod_from) << 24) +
361 		(rsnd_dmapp_get_id(io, mod_to) << 16);
362 }
363 
364 #define rsnd_dmapp_addr(dmac, dma, reg) \
365 	(dmac->ppbase + 0x20 + reg + \
366 	 (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id))
rsnd_dmapp_write(struct rsnd_dma * dma,u32 data,u32 reg)367 static void rsnd_dmapp_write(struct rsnd_dma *dma, u32 data, u32 reg)
368 {
369 	struct rsnd_mod *mod = rsnd_mod_get(dma);
370 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
371 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
372 	struct device *dev = rsnd_priv_to_dev(priv);
373 
374 	dev_dbg(dev, "w 0x%px : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data);
375 
376 	iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg));
377 }
378 
rsnd_dmapp_read(struct rsnd_dma * dma,u32 reg)379 static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg)
380 {
381 	struct rsnd_mod *mod = rsnd_mod_get(dma);
382 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
383 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
384 
385 	return ioread32(rsnd_dmapp_addr(dmac, dma, reg));
386 }
387 
rsnd_dmapp_bset(struct rsnd_dma * dma,u32 data,u32 mask,u32 reg)388 static void rsnd_dmapp_bset(struct rsnd_dma *dma, u32 data, u32 mask, u32 reg)
389 {
390 	struct rsnd_mod *mod = rsnd_mod_get(dma);
391 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
392 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
393 	void __iomem *addr = rsnd_dmapp_addr(dmac, dma, reg);
394 	u32 val = ioread32(addr);
395 
396 	val &= ~mask;
397 	val |= (data & mask);
398 
399 	iowrite32(val, addr);
400 }
401 
rsnd_dmapp_stop(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)402 static int rsnd_dmapp_stop(struct rsnd_mod *mod,
403 			   struct rsnd_dai_stream *io,
404 			   struct rsnd_priv *priv)
405 {
406 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
407 	int i;
408 
409 	rsnd_dmapp_bset(dma, 0,  PDMACHCR_DE, PDMACHCR);
410 
411 	for (i = 0; i < 1024; i++) {
412 		if (0 == (rsnd_dmapp_read(dma, PDMACHCR) & PDMACHCR_DE))
413 			return 0;
414 		udelay(1);
415 	}
416 
417 	return -EIO;
418 }
419 
rsnd_dmapp_start(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)420 static int rsnd_dmapp_start(struct rsnd_mod *mod,
421 			    struct rsnd_dai_stream *io,
422 			    struct rsnd_priv *priv)
423 {
424 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
425 	struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
426 
427 	rsnd_dmapp_write(dma, dma->src_addr,	PDMASAR);
428 	rsnd_dmapp_write(dma, dma->dst_addr,	PDMADAR);
429 	rsnd_dmapp_write(dma, dmapp->chcr,	PDMACHCR);
430 
431 	return 0;
432 }
433 
rsnd_dmapp_attach(struct rsnd_dai_stream * io,struct rsnd_dma * dma,struct rsnd_mod * mod_from,struct rsnd_mod * mod_to)434 static int rsnd_dmapp_attach(struct rsnd_dai_stream *io,
435 			     struct rsnd_dma *dma,
436 			     struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
437 {
438 	struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
439 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
440 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
441 	struct device *dev = rsnd_priv_to_dev(priv);
442 
443 	dmapp->dmapp_id = dmac->dmapp_num;
444 	dmapp->chcr = rsnd_dmapp_get_chcr(io, mod_from, mod_to) | PDMACHCR_DE;
445 
446 	dmac->dmapp_num++;
447 
448 	dev_dbg(dev, "id/src/dst/chcr = %d/%pad/%pad/%08x\n",
449 		dmapp->dmapp_id, &dma->src_addr, &dma->dst_addr, dmapp->chcr);
450 
451 	return 0;
452 }
453 
454 #ifdef CONFIG_DEBUG_FS
rsnd_dmapp_debug_info(struct seq_file * m,struct rsnd_dai_stream * io,struct rsnd_mod * mod)455 static void rsnd_dmapp_debug_info(struct seq_file *m,
456 				  struct rsnd_dai_stream *io,
457 				  struct rsnd_mod *mod)
458 {
459 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
460 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
461 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
462 	struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
463 
464 	rsnd_debugfs_reg_show(m, dmac->ppres, dmac->ppbase,
465 			      0x20 + 0x10 * dmapp->dmapp_id, 0x10);
466 }
467 #define DEBUG_INFO .debug_info = rsnd_dmapp_debug_info
468 #else
469 #define DEBUG_INFO
470 #endif
471 
472 static struct rsnd_mod_ops rsnd_dmapp_ops = {
473 	.name		= "audmac-pp",
474 	.start		= rsnd_dmapp_start,
475 	.stop		= rsnd_dmapp_stop,
476 	.quit		= rsnd_dmapp_stop,
477 	.get_status	= rsnd_mod_get_status,
478 	DEBUG_INFO
479 };
480 
481 /*
482  *		Common DMAC Interface
483  */
484 
485 /*
486  *	DMA read/write register offset
487  *
488  *	RSND_xxx_I_N	for Audio DMAC input
489  *	RSND_xxx_O_N	for Audio DMAC output
490  *	RSND_xxx_I_P	for Audio DMAC peri peri input
491  *	RSND_xxx_O_P	for Audio DMAC peri peri output
492  *
493  *	ex) R-Car H2 case
494  *	      mod        / DMAC in    / DMAC out   / DMAC PP in / DMAC pp out
495  *	SSI : 0xec541000 / 0xec241008 / 0xec24100c
496  *	SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000
497  *	SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000
498  *	CMD : 0xec500000 /            / 0xec008000                0xec308000
499  */
500 #define RDMA_SSI_I_N(addr, i)	(addr ##_reg - 0x00300000 + (0x40 * i) + 0x8)
501 #define RDMA_SSI_O_N(addr, i)	(addr ##_reg - 0x00300000 + (0x40 * i) + 0xc)
502 
503 #define RDMA_SSIU_I_N(addr, i, j) (addr ##_reg - 0x00441000 + (0x1000 * (i)) + (((j) / 4) * 0xA000) + (((j) % 4) * 0x400) - (0x4000 * ((i) / 9) * ((j) / 4)))
504 #define RDMA_SSIU_O_N(addr, i, j) RDMA_SSIU_I_N(addr, i, j)
505 
506 #define RDMA_SSIU_I_P(addr, i, j) (addr ##_reg - 0x00141000 + (0x1000 * (i)) + (((j) / 4) * 0xA000) + (((j) % 4) * 0x400) - (0x4000 * ((i) / 9) * ((j) / 4)))
507 #define RDMA_SSIU_O_P(addr, i, j) RDMA_SSIU_I_P(addr, i, j)
508 
509 #define RDMA_SRC_I_N(addr, i)	(addr ##_reg - 0x00500000 + (0x400 * i))
510 #define RDMA_SRC_O_N(addr, i)	(addr ##_reg - 0x004fc000 + (0x400 * i))
511 
512 #define RDMA_SRC_I_P(addr, i)	(addr ##_reg - 0x00200000 + (0x400 * i))
513 #define RDMA_SRC_O_P(addr, i)	(addr ##_reg - 0x001fc000 + (0x400 * i))
514 
515 #define RDMA_CMD_O_N(addr, i)	(addr ##_reg - 0x004f8000 + (0x400 * i))
516 #define RDMA_CMD_O_P(addr, i)	(addr ##_reg - 0x001f8000 + (0x400 * i))
517 
518 static dma_addr_t
rsnd_gen2_dma_addr(struct rsnd_dai_stream * io,struct rsnd_mod * mod,int is_play,int is_from)519 rsnd_gen2_dma_addr(struct rsnd_dai_stream *io,
520 		   struct rsnd_mod *mod,
521 		   int is_play, int is_from)
522 {
523 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
524 	struct device *dev = rsnd_priv_to_dev(priv);
525 	phys_addr_t ssi_reg = rsnd_gen_get_phy_addr(priv, RSND_BASE_SSI);
526 	phys_addr_t src_reg = rsnd_gen_get_phy_addr(priv, RSND_BASE_SCU);
527 	int is_ssi = !!(rsnd_io_to_mod_ssi(io) == mod) ||
528 		     !!(rsnd_io_to_mod_ssiu(io) == mod);
529 	int use_src = !!rsnd_io_to_mod_src(io);
530 	int use_cmd = !!rsnd_io_to_mod_dvc(io) ||
531 		      !!rsnd_io_to_mod_mix(io) ||
532 		      !!rsnd_io_to_mod_ctu(io);
533 	int id = rsnd_mod_id(mod);
534 	int busif = rsnd_mod_id_sub(rsnd_io_to_mod_ssiu(io));
535 	struct dma_addr {
536 		dma_addr_t out_addr;
537 		dma_addr_t in_addr;
538 	} dma_addrs[3][2][3] = {
539 		/* SRC */
540 		/* Capture */
541 		{{{ 0,				0 },
542 		  { RDMA_SRC_O_N(src, id),	RDMA_SRC_I_P(src, id) },
543 		  { RDMA_CMD_O_N(src, id),	RDMA_SRC_I_P(src, id) } },
544 		 /* Playback */
545 		 {{ 0,				0, },
546 		  { RDMA_SRC_O_P(src, id),	RDMA_SRC_I_N(src, id) },
547 		  { RDMA_CMD_O_P(src, id),	RDMA_SRC_I_N(src, id) } }
548 		},
549 		/* SSI */
550 		/* Capture */
551 		{{{ RDMA_SSI_O_N(ssi, id),		0 },
552 		  { RDMA_SSIU_O_P(ssi, id, busif),	0 },
553 		  { RDMA_SSIU_O_P(ssi, id, busif),	0 } },
554 		 /* Playback */
555 		 {{ 0,			RDMA_SSI_I_N(ssi, id) },
556 		  { 0,			RDMA_SSIU_I_P(ssi, id, busif) },
557 		  { 0,			RDMA_SSIU_I_P(ssi, id, busif) } }
558 		},
559 		/* SSIU */
560 		/* Capture */
561 		{{{ RDMA_SSIU_O_N(ssi, id, busif),	0 },
562 		  { RDMA_SSIU_O_P(ssi, id, busif),	0 },
563 		  { RDMA_SSIU_O_P(ssi, id, busif),	0 } },
564 		 /* Playback */
565 		 {{ 0,			RDMA_SSIU_I_N(ssi, id, busif) },
566 		  { 0,			RDMA_SSIU_I_P(ssi, id, busif) },
567 		  { 0,			RDMA_SSIU_I_P(ssi, id, busif) } } },
568 	};
569 
570 	/*
571 	 * FIXME
572 	 *
573 	 * We can't support SSI9-4/5/6/7, because its address is
574 	 * out of calculation rule
575 	 */
576 	if ((id == 9) && (busif >= 4))
577 		dev_err(dev, "This driver doesn't support SSI%d-%d, so far",
578 			id, busif);
579 
580 	/* it shouldn't happen */
581 	if (use_cmd && !use_src)
582 		dev_err(dev, "DVC is selected without SRC\n");
583 
584 	/* use SSIU or SSI ? */
585 	if (is_ssi && rsnd_ssi_use_busif(io))
586 		is_ssi++;
587 
588 	return (is_from) ?
589 		dma_addrs[is_ssi][is_play][use_src + use_cmd].out_addr :
590 		dma_addrs[is_ssi][is_play][use_src + use_cmd].in_addr;
591 }
592 
593 /*
594  *	Gen4 DMA read/write register offset
595  *
596  *	ex) R-Car V4H case
597  *		  mod		/ SYS-DMAC in	/ SYS-DMAC out
598  *	SSI_SDMC: 0xec400000	/ 0xec400000	/ 0xec400000
599  */
600 #define RDMA_SSI_SDMC(addr, i)	(addr + (0x8000 * i))
601 static dma_addr_t
rsnd_gen4_dma_addr(struct rsnd_dai_stream * io,struct rsnd_mod * mod,int is_play,int is_from)602 rsnd_gen4_dma_addr(struct rsnd_dai_stream *io, struct rsnd_mod *mod,
603 		   int is_play, int is_from)
604 {
605 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
606 	phys_addr_t addr = rsnd_gen_get_phy_addr(priv, RSND_BASE_SDMC);
607 	int id = rsnd_mod_id(mod);
608 	int busif = rsnd_mod_id_sub(mod);
609 
610 	/*
611 	 * SSI0 only is supported
612 	 */
613 	if (id != 0) {
614 		struct device *dev = rsnd_priv_to_dev(priv);
615 
616 		dev_err(dev, "This driver doesn't support non SSI0");
617 		return -EINVAL;
618 	}
619 
620 	return RDMA_SSI_SDMC(addr, busif);
621 }
622 
rsnd_dma_addr(struct rsnd_dai_stream * io,struct rsnd_mod * mod,int is_play,int is_from)623 static dma_addr_t rsnd_dma_addr(struct rsnd_dai_stream *io,
624 				struct rsnd_mod *mod,
625 				int is_play, int is_from)
626 {
627 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
628 
629 	if (!mod)
630 		return 0;
631 
632 	/*
633 	 * gen1 uses default DMA addr
634 	 */
635 	if (rsnd_is_gen1(priv))
636 		return 0;
637 	else if (rsnd_is_gen4(priv))
638 		return rsnd_gen4_dma_addr(io, mod, is_play, is_from);
639 	else
640 		return rsnd_gen2_dma_addr(io, mod, is_play, is_from);
641 }
642 
643 #define MOD_MAX (RSND_MOD_MAX + 1) /* +Memory */
rsnd_dma_of_path(struct rsnd_mod * this,struct rsnd_dai_stream * io,int is_play,struct rsnd_mod ** mod_from,struct rsnd_mod ** mod_to)644 static void rsnd_dma_of_path(struct rsnd_mod *this,
645 			     struct rsnd_dai_stream *io,
646 			     int is_play,
647 			     struct rsnd_mod **mod_from,
648 			     struct rsnd_mod **mod_to)
649 {
650 	struct rsnd_mod *ssi;
651 	struct rsnd_mod *src = rsnd_io_to_mod_src(io);
652 	struct rsnd_mod *ctu = rsnd_io_to_mod_ctu(io);
653 	struct rsnd_mod *mix = rsnd_io_to_mod_mix(io);
654 	struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
655 	struct rsnd_mod *mod[MOD_MAX];
656 	struct rsnd_mod *mod_start, *mod_end;
657 	struct rsnd_priv *priv = rsnd_mod_to_priv(this);
658 	struct device *dev = rsnd_priv_to_dev(priv);
659 	int nr, i, idx;
660 
661 	/*
662 	 * It should use "rcar_sound,ssiu" on DT.
663 	 * But, we need to keep compatibility for old version.
664 	 *
665 	 * If it has "rcar_sound.ssiu", it will be used.
666 	 * If not, "rcar_sound.ssi" will be used.
667 	 * see
668 	 *	rsnd_ssiu_dma_req()
669 	 *	rsnd_ssi_dma_req()
670 	 */
671 	if (rsnd_ssiu_of_node(priv)) {
672 		struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
673 
674 		/* use SSIU */
675 		ssi = ssiu;
676 		if (this == rsnd_io_to_mod_ssi(io))
677 			this = ssiu;
678 	} else {
679 		/* keep compatible, use SSI */
680 		ssi = rsnd_io_to_mod_ssi(io);
681 	}
682 
683 	if (!ssi)
684 		return;
685 
686 	nr = 0;
687 	for (i = 0; i < MOD_MAX; i++) {
688 		mod[i] = NULL;
689 		nr += !!rsnd_io_to_mod(io, i);
690 	}
691 
692 	/*
693 	 * [S] -*-> [E]
694 	 * [S] -*-> SRC -o-> [E]
695 	 * [S] -*-> SRC -> DVC -o-> [E]
696 	 * [S] -*-> SRC -> CTU -> MIX -> DVC -o-> [E]
697 	 *
698 	 * playback	[S] = mem
699 	 *		[E] = SSI
700 	 *
701 	 * capture	[S] = SSI
702 	 *		[E] = mem
703 	 *
704 	 * -*->		Audio DMAC
705 	 * -o->		Audio DMAC peri peri
706 	 */
707 	mod_start	= (is_play) ? NULL : ssi;
708 	mod_end		= (is_play) ? ssi  : NULL;
709 
710 	idx = 0;
711 	mod[idx++] = mod_start;
712 	for (i = 1; i < nr; i++) {
713 		if (src) {
714 			mod[idx++] = src;
715 			src = NULL;
716 		} else if (ctu) {
717 			mod[idx++] = ctu;
718 			ctu = NULL;
719 		} else if (mix) {
720 			mod[idx++] = mix;
721 			mix = NULL;
722 		} else if (dvc) {
723 			mod[idx++] = dvc;
724 			dvc = NULL;
725 		}
726 	}
727 	mod[idx] = mod_end;
728 
729 	/*
730 	 *		| SSI | SRC |
731 	 * -------------+-----+-----+
732 	 *  is_play	|  o  |  *  |
733 	 * !is_play	|  *  |  o  |
734 	 */
735 	if ((this == ssi) == (is_play)) {
736 		*mod_from	= mod[idx - 1];
737 		*mod_to		= mod[idx];
738 	} else {
739 		*mod_from	= mod[0];
740 		*mod_to		= mod[1];
741 	}
742 
743 	dev_dbg(dev, "module connection (this is %s)\n", rsnd_mod_name(this));
744 	for (i = 0; i <= idx; i++) {
745 		dev_dbg(dev, "  %s%s\n",
746 			rsnd_mod_name(mod[i] ? mod[i] : &mem),
747 			(mod[i] == *mod_from) ? " from" :
748 			(mod[i] == *mod_to)   ? " to" : "");
749 	}
750 }
751 
rsnd_dma_alloc(struct rsnd_dai_stream * io,struct rsnd_mod * mod,struct rsnd_mod ** dma_mod)752 static int rsnd_dma_alloc(struct rsnd_dai_stream *io, struct rsnd_mod *mod,
753 			  struct rsnd_mod **dma_mod)
754 {
755 	struct rsnd_mod *mod_from = NULL;
756 	struct rsnd_mod *mod_to = NULL;
757 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
758 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
759 	struct device *dev = rsnd_priv_to_dev(priv);
760 	struct rsnd_dma *dma;
761 	struct rsnd_mod_ops *ops;
762 	enum rsnd_mod_type type;
763 	int (*attach)(struct rsnd_dai_stream *io, struct rsnd_dma *dma,
764 		      struct rsnd_mod *mod_from, struct rsnd_mod *mod_to);
765 	int is_play = rsnd_io_is_play(io);
766 	int ret, dma_id;
767 
768 	/*
769 	 * DMA failed. try to PIO mode
770 	 * see
771 	 *	rsnd_ssi_fallback()
772 	 *	rsnd_rdai_continuance_probe()
773 	 */
774 	if (!dmac)
775 		return -EAGAIN;
776 
777 	rsnd_dma_of_path(mod, io, is_play, &mod_from, &mod_to);
778 
779 	/* for Gen2 or later */
780 	if (mod_from && mod_to) {
781 		ops	= &rsnd_dmapp_ops;
782 		attach	= rsnd_dmapp_attach;
783 		dma_id	= dmac->dmapp_num;
784 		type	= RSND_MOD_AUDMAPP;
785 	} else {
786 		ops	= &rsnd_dmaen_ops;
787 		attach	= rsnd_dmaen_attach;
788 		dma_id	= dmac->dmaen_num;
789 		type	= RSND_MOD_AUDMA;
790 	}
791 
792 	/* for Gen1, overwrite */
793 	if (rsnd_is_gen1(priv)) {
794 		ops	= &rsnd_dmaen_ops;
795 		attach	= rsnd_dmaen_attach;
796 		dma_id	= dmac->dmaen_num;
797 		type	= RSND_MOD_AUDMA;
798 	}
799 
800 	dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
801 	if (!dma)
802 		return -ENOMEM;
803 
804 	*dma_mod = rsnd_mod_get(dma);
805 
806 	ret = rsnd_mod_init(priv, *dma_mod, ops, NULL,
807 			    type, dma_id);
808 	if (ret < 0)
809 		return ret;
810 
811 	dev_dbg(dev, "%s %s -> %s\n",
812 		rsnd_mod_name(*dma_mod),
813 		rsnd_mod_name(mod_from ? mod_from : &mem),
814 		rsnd_mod_name(mod_to   ? mod_to   : &mem));
815 
816 	ret = attach(io, dma, mod_from, mod_to);
817 	if (ret < 0)
818 		return ret;
819 
820 	dma->src_addr = rsnd_dma_addr(io, mod_from, is_play, 1);
821 	dma->dst_addr = rsnd_dma_addr(io, mod_to,   is_play, 0);
822 	dma->mod_from = mod_from;
823 	dma->mod_to   = mod_to;
824 
825 	return 0;
826 }
827 
rsnd_dma_attach(struct rsnd_dai_stream * io,struct rsnd_mod * mod,struct rsnd_mod ** dma_mod)828 int rsnd_dma_attach(struct rsnd_dai_stream *io, struct rsnd_mod *mod,
829 		    struct rsnd_mod **dma_mod)
830 {
831 	if (!(*dma_mod)) {
832 		int ret = rsnd_dma_alloc(io, mod, dma_mod);
833 
834 		if (ret < 0)
835 			return ret;
836 	}
837 
838 	return rsnd_dai_connect(*dma_mod, io, (*dma_mod)->type);
839 }
840 
rsnd_dma_probe(struct rsnd_priv * priv)841 int rsnd_dma_probe(struct rsnd_priv *priv)
842 {
843 	struct platform_device *pdev = rsnd_priv_to_pdev(priv);
844 	struct device *dev = rsnd_priv_to_dev(priv);
845 	struct rsnd_dma_ctrl *dmac;
846 	struct resource *res;
847 
848 	/*
849 	 * for Gen1
850 	 */
851 	if (rsnd_is_gen1(priv))
852 		return 0;
853 
854 	/*
855 	 * for Gen2 or later
856 	 */
857 	dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL);
858 	if (!dmac) {
859 		dev_err(dev, "dma allocate failed\n");
860 		return 0; /* it will be PIO mode */
861 	}
862 
863 	/* for Gen4 doesn't have DMA-pp */
864 	if (rsnd_is_gen4(priv))
865 		goto audmapp_end;
866 
867 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp");
868 	if (!res) {
869 		dev_err(dev, "lack of audmapp in DT\n");
870 		return 0; /* it will be PIO mode */
871 	}
872 
873 	dmac->dmapp_num = 0;
874 	dmac->ppres  = res->start;
875 	dmac->ppbase = devm_ioremap_resource(dev, res);
876 	if (IS_ERR(dmac->ppbase))
877 		return PTR_ERR(dmac->ppbase);
878 audmapp_end:
879 	priv->dma = dmac;
880 
881 	/* dummy mem mod for debug */
882 	return rsnd_mod_init(NULL, &mem, &mem_ops, NULL, 0, 0);
883 }
884