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)) {
159 dev_err(dev, "can't get dma channel\n");
160 goto rsnd_dma_channel_err;
163 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
164 cfg.src_addr = dma->src_addr;
165 cfg.dst_addr = dma->dst_addr;
166 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
167 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
169 dev_dbg(dev, "dma : %pad -> %pad\n",
170 &cfg.src_addr, &cfg.dst_addr);
172 ret = dmaengine_slave_config(dmaen->chan, &cfg);
174 goto rsnd_dma_init_err;
180 rsnd_dma_channel_err:
183 * DMA failed. try to PIO mode
185 * rsnd_ssi_fallback()
186 * rsnd_rdai_continuance_probe()
191 static void rsnd_dmaen_quit(struct rsnd_dma *dma)
193 struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
196 dma_release_channel(dmaen->chan);
201 static struct rsnd_dma_ops rsnd_dmaen_ops = {
202 .start = rsnd_dmaen_start,
203 .stop = rsnd_dmaen_stop,
204 .init = rsnd_dmaen_init,
205 .quit = rsnd_dmaen_quit,
209 * Audio DMAC peri peri
211 static const u8 gen2_id_table_ssiu[] = {
223 static const u8 gen2_id_table_scu[] = {
224 0x2d, /* SCU_SRCI0 */
225 0x2e, /* SCU_SRCI1 */
226 0x2f, /* SCU_SRCI2 */
227 0x30, /* SCU_SRCI3 */
228 0x31, /* SCU_SRCI4 */
229 0x32, /* SCU_SRCI5 */
230 0x33, /* SCU_SRCI6 */
231 0x34, /* SCU_SRCI7 */
232 0x35, /* SCU_SRCI8 */
233 0x36, /* SCU_SRCI9 */
235 static const u8 gen2_id_table_cmd[] = {
240 static u32 rsnd_dmapp_get_id(struct rsnd_mod *mod)
242 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
243 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
244 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
245 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
246 const u8 *entry = NULL;
247 int id = rsnd_mod_id(mod);
251 entry = gen2_id_table_ssiu;
252 size = ARRAY_SIZE(gen2_id_table_ssiu);
253 } else if (mod == src) {
254 entry = gen2_id_table_scu;
255 size = ARRAY_SIZE(gen2_id_table_scu);
256 } else if (mod == dvc) {
257 entry = gen2_id_table_cmd;
258 size = ARRAY_SIZE(gen2_id_table_cmd);
270 static u32 rsnd_dmapp_get_chcr(struct rsnd_mod *mod_from,
271 struct rsnd_mod *mod_to)
273 return (rsnd_dmapp_get_id(mod_from) << 24) +
274 (rsnd_dmapp_get_id(mod_to) << 16);
277 #define rsnd_dmapp_addr(dmac, dma, reg) \
278 (dmac->base + 0x20 + reg + \
279 (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id))
280 static void rsnd_dmapp_write(struct rsnd_dma *dma, u32 data, u32 reg)
282 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
283 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
284 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
285 struct device *dev = rsnd_priv_to_dev(priv);
287 dev_dbg(dev, "w %p : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data);
289 iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg));
292 static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg)
294 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
295 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
296 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
298 return ioread32(rsnd_dmapp_addr(dmac, dma, reg));
301 static void rsnd_dmapp_stop(struct rsnd_dma *dma)
305 rsnd_dmapp_write(dma, 0, PDMACHCR);
307 for (i = 0; i < 1024; i++) {
308 if (0 == rsnd_dmapp_read(dma, PDMACHCR))
314 static void rsnd_dmapp_start(struct rsnd_dma *dma)
316 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
318 rsnd_dmapp_write(dma, dma->src_addr, PDMASAR);
319 rsnd_dmapp_write(dma, dma->dst_addr, PDMADAR);
320 rsnd_dmapp_write(dma, dmapp->chcr, PDMACHCR);
323 static int rsnd_dmapp_init(struct rsnd_priv *priv, struct rsnd_dma *dma, int id,
324 struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
326 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
327 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
328 struct device *dev = rsnd_priv_to_dev(priv);
330 dmapp->dmapp_id = dmac->dmapp_num;
331 dmapp->chcr = rsnd_dmapp_get_chcr(mod_from, mod_to) | PDMACHCR_DE;
335 rsnd_dmapp_stop(dma);
337 dev_dbg(dev, "id/src/dst/chcr = %d/%pad/%pad/%08x\n",
338 dmapp->dmapp_id, &dma->src_addr, &dma->dst_addr, dmapp->chcr);
343 static struct rsnd_dma_ops rsnd_dmapp_ops = {
344 .start = rsnd_dmapp_start,
345 .stop = rsnd_dmapp_stop,
346 .init = rsnd_dmapp_init,
347 .quit = rsnd_dmapp_stop,
351 * Common DMAC Interface
355 * DMA read/write register offset
357 * RSND_xxx_I_N for Audio DMAC input
358 * RSND_xxx_O_N for Audio DMAC output
359 * RSND_xxx_I_P for Audio DMAC peri peri input
360 * RSND_xxx_O_P for Audio DMAC peri peri output
363 * mod / DMAC in / DMAC out / DMAC PP in / DMAC pp out
364 * SSI : 0xec541000 / 0xec241008 / 0xec24100c
365 * SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000
366 * SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000
367 * CMD : 0xec500000 / / 0xec008000 0xec308000
369 #define RDMA_SSI_I_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0x8)
370 #define RDMA_SSI_O_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0xc)
372 #define RDMA_SSIU_I_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
373 #define RDMA_SSIU_O_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
375 #define RDMA_SSIU_I_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
376 #define RDMA_SSIU_O_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
378 #define RDMA_SRC_I_N(addr, i) (addr ##_reg - 0x00500000 + (0x400 * i))
379 #define RDMA_SRC_O_N(addr, i) (addr ##_reg - 0x004fc000 + (0x400 * i))
381 #define RDMA_SRC_I_P(addr, i) (addr ##_reg - 0x00200000 + (0x400 * i))
382 #define RDMA_SRC_O_P(addr, i) (addr ##_reg - 0x001fc000 + (0x400 * i))
384 #define RDMA_CMD_O_N(addr, i) (addr ##_reg - 0x004f8000 + (0x400 * i))
385 #define RDMA_CMD_O_P(addr, i) (addr ##_reg - 0x001f8000 + (0x400 * i))
388 rsnd_gen2_dma_addr(struct rsnd_priv *priv,
389 struct rsnd_mod *mod,
390 int is_play, int is_from)
392 struct device *dev = rsnd_priv_to_dev(priv);
393 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
394 phys_addr_t ssi_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SSI);
395 phys_addr_t src_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SCU);
396 int is_ssi = !!(rsnd_io_to_mod_ssi(io) == mod);
397 int use_src = !!rsnd_io_to_mod_src(io);
398 int use_dvc = !!rsnd_io_to_mod_dvc(io);
399 int id = rsnd_mod_id(mod);
403 } dma_addrs[3][2][3] = {
407 { RDMA_SRC_O_N(src, id), RDMA_SRC_I_P(src, id) },
408 { RDMA_CMD_O_N(src, id), RDMA_SRC_I_P(src, id) } },
411 { RDMA_SRC_O_P(src, id), RDMA_SRC_I_N(src, id) },
412 { RDMA_CMD_O_P(src, id), RDMA_SRC_I_N(src, id) } }
416 {{{ RDMA_SSI_O_N(ssi, id), 0 },
417 { RDMA_SSIU_O_P(ssi, id), 0 },
418 { RDMA_SSIU_O_P(ssi, id), 0 } },
420 {{ 0, RDMA_SSI_I_N(ssi, id) },
421 { 0, RDMA_SSIU_I_P(ssi, id) },
422 { 0, RDMA_SSIU_I_P(ssi, id) } }
426 {{{ RDMA_SSIU_O_N(ssi, id), 0 },
427 { RDMA_SSIU_O_P(ssi, id), 0 },
428 { RDMA_SSIU_O_P(ssi, id), 0 } },
430 {{ 0, RDMA_SSIU_I_N(ssi, id) },
431 { 0, RDMA_SSIU_I_P(ssi, id) },
432 { 0, RDMA_SSIU_I_P(ssi, id) } } },
435 /* it shouldn't happen */
436 if (use_dvc && !use_src)
437 dev_err(dev, "DVC is selected without SRC\n");
439 /* use SSIU or SSI ? */
440 if (is_ssi && rsnd_ssi_use_busif(mod))
444 dma_addrs[is_ssi][is_play][use_src + use_dvc].out_addr :
445 dma_addrs[is_ssi][is_play][use_src + use_dvc].in_addr;
448 static dma_addr_t rsnd_dma_addr(struct rsnd_priv *priv,
449 struct rsnd_mod *mod,
450 int is_play, int is_from)
453 * gen1 uses default DMA addr
455 if (rsnd_is_gen1(priv))
461 return rsnd_gen2_dma_addr(priv, mod, is_play, is_from);
464 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
465 static void rsnd_dma_of_path(struct rsnd_dma *dma,
467 struct rsnd_mod **mod_from,
468 struct rsnd_mod **mod_to)
470 struct rsnd_mod *this = rsnd_dma_to_mod(dma);
471 struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
472 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
473 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
474 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
475 struct rsnd_mod *mod[MOD_MAX];
479 for (i = 0; i < MOD_MAX; i++)
489 * mem -> SRC -> DVC -> SSI
491 mod[0] = NULL; /* for "mem" */
493 for (i = 1; i < MOD_MAX; i++) {
500 if ((!is_play) && (this == src))
503 mod[i] = (is_play) ? src : dvc;
505 mod[i] = (is_play) ? dvc : src;
518 *mod_from = mod[index - 1];
519 *mod_to = mod[index];
521 *mod_from = mod[index];
522 *mod_to = mod[index - 1];
526 void rsnd_dma_stop(struct rsnd_dma *dma)
531 void rsnd_dma_start(struct rsnd_dma *dma)
533 dma->ops->start(dma);
536 void rsnd_dma_quit(struct rsnd_dma *dma)
538 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
539 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
540 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
548 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, int id)
550 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
551 struct rsnd_mod *mod_from;
552 struct rsnd_mod *mod_to;
553 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
554 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
555 int is_play = rsnd_io_is_play(io);
558 * DMA failed. try to PIO mode
560 * rsnd_ssi_fallback()
561 * rsnd_rdai_continuance_probe()
566 rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to);
568 dma->src_addr = rsnd_dma_addr(priv, mod_from, is_play, 1);
569 dma->dst_addr = rsnd_dma_addr(priv, mod_to, is_play, 0);
572 if (mod_from && mod_to)
573 dma->ops = &rsnd_dmapp_ops;
575 dma->ops = &rsnd_dmaen_ops;
577 /* for Gen1, overwrite */
578 if (rsnd_is_gen1(priv))
579 dma->ops = &rsnd_dmaen_ops;
581 return dma->ops->init(priv, dma, id, mod_from, mod_to);
584 int rsnd_dma_probe(struct platform_device *pdev,
585 const struct rsnd_of_data *of_data,
586 struct rsnd_priv *priv)
588 struct device *dev = rsnd_priv_to_dev(priv);
589 struct rsnd_dma_ctrl *dmac;
590 struct resource *res;
595 if (rsnd_is_gen1(priv))
601 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp");
602 dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL);
604 dev_err(dev, "dma allocate failed\n");
605 return 0; /* it will be PIO mode */
609 dmac->base = devm_ioremap_resource(dev, res);
610 if (IS_ERR(dmac->base))
611 return PTR_ERR(dmac->base);