2 * Renesas R-Car Audio DMAC support
4 * Copyright (C) 2015 Renesas Electronics Corp.
5 * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/delay.h>
12 #include <linux/of_dma.h>
16 * Audio DMAC peri peri register
23 #define PDMACHCR_DE (1 << 0)
25 struct rsnd_dma_ctrl {
30 #define rsnd_priv_to_dmac(p) ((struct rsnd_dma_ctrl *)(p)->dma)
35 static void rsnd_dmaen_complete(void *data)
37 struct rsnd_dma *dma = (struct rsnd_dma *)data;
38 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
39 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
42 * Renesas sound Gen1 needs 1 DMAC,
44 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
45 * But, Audio-DMAC-peri-peri doesn't have interrupt,
46 * and this driver is assuming that here.
48 * If Audio-DMAC-peri-peri has interrpt,
49 * rsnd_dai_pointer_update() will be called twice,
50 * ant it will breaks io->byte_pos
53 rsnd_dai_pointer_update(io, io->byte_per_period);
56 static void rsnd_dmaen_stop(struct rsnd_dma *dma)
58 struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
60 dmaengine_terminate_all(dmaen->chan);
63 static void rsnd_dmaen_start(struct rsnd_dma *dma)
65 struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
66 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
67 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
68 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
69 struct snd_pcm_substream *substream = io->substream;
70 struct device *dev = rsnd_priv_to_dev(priv);
71 struct dma_async_tx_descriptor *desc;
72 int is_play = rsnd_io_is_play(io);
74 desc = dmaengine_prep_dma_cyclic(dmaen->chan,
75 substream->runtime->dma_addr,
76 snd_pcm_lib_buffer_bytes(substream),
77 snd_pcm_lib_period_bytes(substream),
78 is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
79 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
82 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
86 desc->callback = rsnd_dmaen_complete;
87 desc->callback_param = dma;
89 if (dmaengine_submit(desc) < 0) {
90 dev_err(dev, "dmaengine_submit() fail\n");
94 dma_async_issue_pending(dmaen->chan);
97 struct dma_chan *rsnd_dma_request_channel(struct device_node *of_node,
98 struct rsnd_mod *mod, char *name)
100 struct dma_chan *chan;
101 struct device_node *np;
104 for_each_child_of_node(of_node, np) {
105 if (i == rsnd_mod_id(mod))
110 chan = of_dma_request_slave_channel(np, name);
113 of_node_put(of_node);
118 static struct dma_chan *rsnd_dmaen_request_channel(struct rsnd_mod *mod_from,
119 struct rsnd_mod *mod_to)
121 if ((!mod_from && !mod_to) ||
122 (mod_from && mod_to))
126 return rsnd_mod_dma_req(mod_from);
128 return rsnd_mod_dma_req(mod_to);
131 static int rsnd_dmaen_init(struct rsnd_priv *priv, struct rsnd_dma *dma, int id,
132 struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
134 struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
135 struct device *dev = rsnd_priv_to_dev(priv);
136 struct dma_slave_config cfg = {};
137 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
138 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
139 int is_play = rsnd_io_is_play(io);
143 dev_err(dev, "it already has dma channel\n");
148 dmaen->chan = rsnd_dmaen_request_channel(mod_from, mod_to);
153 dma_cap_set(DMA_SLAVE, mask);
155 dmaen->chan = dma_request_channel(mask, shdma_chan_filter,
158 if (IS_ERR_OR_NULL(dmaen->chan)) {
160 dev_err(dev, "can't get dma channel\n");
161 goto rsnd_dma_channel_err;
164 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
165 cfg.src_addr = dma->src_addr;
166 cfg.dst_addr = dma->dst_addr;
167 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
168 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
170 dev_dbg(dev, "dma : %pad -> %pad\n",
171 &cfg.src_addr, &cfg.dst_addr);
173 ret = dmaengine_slave_config(dmaen->chan, &cfg);
175 goto rsnd_dma_init_err;
181 rsnd_dma_channel_err:
184 * DMA failed. try to PIO mode
186 * rsnd_ssi_fallback()
187 * rsnd_rdai_continuance_probe()
192 static void rsnd_dmaen_quit(struct rsnd_dma *dma)
194 struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
197 dma_release_channel(dmaen->chan);
202 static struct rsnd_dma_ops rsnd_dmaen_ops = {
203 .start = rsnd_dmaen_start,
204 .stop = rsnd_dmaen_stop,
205 .init = rsnd_dmaen_init,
206 .quit = rsnd_dmaen_quit,
210 * Audio DMAC peri peri
212 static const u8 gen2_id_table_ssiu[] = {
224 static const u8 gen2_id_table_scu[] = {
225 0x2d, /* SCU_SRCI0 */
226 0x2e, /* SCU_SRCI1 */
227 0x2f, /* SCU_SRCI2 */
228 0x30, /* SCU_SRCI3 */
229 0x31, /* SCU_SRCI4 */
230 0x32, /* SCU_SRCI5 */
231 0x33, /* SCU_SRCI6 */
232 0x34, /* SCU_SRCI7 */
233 0x35, /* SCU_SRCI8 */
234 0x36, /* SCU_SRCI9 */
236 static const u8 gen2_id_table_cmd[] = {
241 static u32 rsnd_dmapp_get_id(struct rsnd_mod *mod)
243 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
244 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
245 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
246 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
247 const u8 *entry = NULL;
248 int id = rsnd_mod_id(mod);
252 entry = gen2_id_table_ssiu;
253 size = ARRAY_SIZE(gen2_id_table_ssiu);
254 } else if (mod == src) {
255 entry = gen2_id_table_scu;
256 size = ARRAY_SIZE(gen2_id_table_scu);
257 } else if (mod == dvc) {
258 entry = gen2_id_table_cmd;
259 size = ARRAY_SIZE(gen2_id_table_cmd);
271 static u32 rsnd_dmapp_get_chcr(struct rsnd_mod *mod_from,
272 struct rsnd_mod *mod_to)
274 return (rsnd_dmapp_get_id(mod_from) << 24) +
275 (rsnd_dmapp_get_id(mod_to) << 16);
278 #define rsnd_dmapp_addr(dmac, dma, reg) \
279 (dmac->base + 0x20 + reg + \
280 (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id))
281 static void rsnd_dmapp_write(struct rsnd_dma *dma, u32 data, u32 reg)
283 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
284 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
285 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
286 struct device *dev = rsnd_priv_to_dev(priv);
288 dev_dbg(dev, "w %p : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data);
290 iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg));
293 static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg)
295 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
296 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
297 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
299 return ioread32(rsnd_dmapp_addr(dmac, dma, reg));
302 static void rsnd_dmapp_stop(struct rsnd_dma *dma)
306 rsnd_dmapp_write(dma, 0, PDMACHCR);
308 for (i = 0; i < 1024; i++) {
309 if (0 == rsnd_dmapp_read(dma, PDMACHCR))
315 static void rsnd_dmapp_start(struct rsnd_dma *dma)
317 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
319 rsnd_dmapp_write(dma, dma->src_addr, PDMASAR);
320 rsnd_dmapp_write(dma, dma->dst_addr, PDMADAR);
321 rsnd_dmapp_write(dma, dmapp->chcr, PDMACHCR);
324 static int rsnd_dmapp_init(struct rsnd_priv *priv, struct rsnd_dma *dma, int id,
325 struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
327 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
328 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
329 struct device *dev = rsnd_priv_to_dev(priv);
331 dmapp->dmapp_id = dmac->dmapp_num;
332 dmapp->chcr = rsnd_dmapp_get_chcr(mod_from, mod_to) | PDMACHCR_DE;
336 rsnd_dmapp_stop(dma);
338 dev_dbg(dev, "id/src/dst/chcr = %d/%pad/%pad/%08x\n",
339 dmapp->dmapp_id, &dma->src_addr, &dma->dst_addr, dmapp->chcr);
344 static struct rsnd_dma_ops rsnd_dmapp_ops = {
345 .start = rsnd_dmapp_start,
346 .stop = rsnd_dmapp_stop,
347 .init = rsnd_dmapp_init,
348 .quit = rsnd_dmapp_stop,
352 * Common DMAC Interface
356 * DMA read/write register offset
358 * RSND_xxx_I_N for Audio DMAC input
359 * RSND_xxx_O_N for Audio DMAC output
360 * RSND_xxx_I_P for Audio DMAC peri peri input
361 * RSND_xxx_O_P for Audio DMAC peri peri output
364 * mod / DMAC in / DMAC out / DMAC PP in / DMAC pp out
365 * SSI : 0xec541000 / 0xec241008 / 0xec24100c
366 * SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000
367 * SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000
368 * CMD : 0xec500000 / / 0xec008000 0xec308000
370 #define RDMA_SSI_I_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0x8)
371 #define RDMA_SSI_O_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0xc)
373 #define RDMA_SSIU_I_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
374 #define RDMA_SSIU_O_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
376 #define RDMA_SSIU_I_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
377 #define RDMA_SSIU_O_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
379 #define RDMA_SRC_I_N(addr, i) (addr ##_reg - 0x00500000 + (0x400 * i))
380 #define RDMA_SRC_O_N(addr, i) (addr ##_reg - 0x004fc000 + (0x400 * i))
382 #define RDMA_SRC_I_P(addr, i) (addr ##_reg - 0x00200000 + (0x400 * i))
383 #define RDMA_SRC_O_P(addr, i) (addr ##_reg - 0x001fc000 + (0x400 * i))
385 #define RDMA_CMD_O_N(addr, i) (addr ##_reg - 0x004f8000 + (0x400 * i))
386 #define RDMA_CMD_O_P(addr, i) (addr ##_reg - 0x001f8000 + (0x400 * i))
389 rsnd_gen2_dma_addr(struct rsnd_priv *priv,
390 struct rsnd_mod *mod,
391 int is_play, int is_from)
393 struct device *dev = rsnd_priv_to_dev(priv);
394 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
395 phys_addr_t ssi_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SSI);
396 phys_addr_t src_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SCU);
397 int is_ssi = !!(rsnd_io_to_mod_ssi(io) == mod);
398 int use_src = !!rsnd_io_to_mod_src(io);
399 int use_dvc = !!rsnd_io_to_mod_dvc(io);
400 int id = rsnd_mod_id(mod);
404 } dma_addrs[3][2][3] = {
408 { RDMA_SRC_O_N(src, id), RDMA_SRC_I_P(src, id) },
409 { RDMA_CMD_O_N(src, id), RDMA_SRC_I_P(src, id) } },
412 { RDMA_SRC_O_P(src, id), RDMA_SRC_I_N(src, id) },
413 { RDMA_CMD_O_P(src, id), RDMA_SRC_I_N(src, id) } }
417 {{{ RDMA_SSI_O_N(ssi, id), 0 },
418 { RDMA_SSIU_O_P(ssi, id), 0 },
419 { RDMA_SSIU_O_P(ssi, id), 0 } },
421 {{ 0, RDMA_SSI_I_N(ssi, id) },
422 { 0, RDMA_SSIU_I_P(ssi, id) },
423 { 0, RDMA_SSIU_I_P(ssi, id) } }
427 {{{ RDMA_SSIU_O_N(ssi, id), 0 },
428 { RDMA_SSIU_O_P(ssi, id), 0 },
429 { RDMA_SSIU_O_P(ssi, id), 0 } },
431 {{ 0, RDMA_SSIU_I_N(ssi, id) },
432 { 0, RDMA_SSIU_I_P(ssi, id) },
433 { 0, RDMA_SSIU_I_P(ssi, id) } } },
436 /* it shouldn't happen */
437 if (use_dvc && !use_src)
438 dev_err(dev, "DVC is selected without SRC\n");
440 /* use SSIU or SSI ? */
441 if (is_ssi && rsnd_ssi_use_busif(mod))
445 dma_addrs[is_ssi][is_play][use_src + use_dvc].out_addr :
446 dma_addrs[is_ssi][is_play][use_src + use_dvc].in_addr;
449 static dma_addr_t rsnd_dma_addr(struct rsnd_priv *priv,
450 struct rsnd_mod *mod,
451 int is_play, int is_from)
454 * gen1 uses default DMA addr
456 if (rsnd_is_gen1(priv))
462 return rsnd_gen2_dma_addr(priv, mod, is_play, is_from);
465 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
466 static void rsnd_dma_of_path(struct rsnd_dma *dma,
468 struct rsnd_mod **mod_from,
469 struct rsnd_mod **mod_to)
471 struct rsnd_mod *this = rsnd_dma_to_mod(dma);
472 struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
473 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
474 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
475 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
476 struct rsnd_mod *mod[MOD_MAX];
480 for (i = 0; i < MOD_MAX; i++)
490 * mem -> SRC -> DVC -> SSI
492 mod[0] = NULL; /* for "mem" */
494 for (i = 1; i < MOD_MAX; i++) {
501 if ((!is_play) && (this == src))
504 mod[i] = (is_play) ? src : dvc;
506 mod[i] = (is_play) ? dvc : src;
519 *mod_from = mod[index - 1];
520 *mod_to = mod[index];
522 *mod_from = mod[index];
523 *mod_to = mod[index - 1];
527 void rsnd_dma_stop(struct rsnd_dma *dma)
532 void rsnd_dma_start(struct rsnd_dma *dma)
534 dma->ops->start(dma);
537 void rsnd_dma_quit(struct rsnd_dma *dma)
539 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
540 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
541 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
549 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, int id)
551 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
552 struct rsnd_mod *mod_from;
553 struct rsnd_mod *mod_to;
554 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
555 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
556 int is_play = rsnd_io_is_play(io);
559 * DMA failed. try to PIO mode
561 * rsnd_ssi_fallback()
562 * rsnd_rdai_continuance_probe()
567 rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to);
569 dma->src_addr = rsnd_dma_addr(priv, mod_from, is_play, 1);
570 dma->dst_addr = rsnd_dma_addr(priv, mod_to, is_play, 0);
573 if (mod_from && mod_to)
574 dma->ops = &rsnd_dmapp_ops;
576 dma->ops = &rsnd_dmaen_ops;
578 /* for Gen1, overwrite */
579 if (rsnd_is_gen1(priv))
580 dma->ops = &rsnd_dmaen_ops;
582 return dma->ops->init(priv, dma, id, mod_from, mod_to);
585 int rsnd_dma_probe(struct platform_device *pdev,
586 const struct rsnd_of_data *of_data,
587 struct rsnd_priv *priv)
589 struct device *dev = rsnd_priv_to_dev(priv);
590 struct rsnd_dma_ctrl *dmac;
591 struct resource *res;
596 if (rsnd_is_gen1(priv))
602 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp");
603 dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL);
605 dev_err(dev, "dma allocate failed\n");
606 return 0; /* it will be PIO mode */
610 dmac->base = devm_ioremap_resource(dev, res);
611 if (IS_ERR(dmac->base))
612 return PTR_ERR(dmac->base);