Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Renesas R-Car Audio DMAC support
0004 //
0005 // Copyright (C) 2015 Renesas Electronics Corp.
0006 // Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
0007 
0008 #include <linux/delay.h>
0009 #include <linux/of_dma.h>
0010 #include "rsnd.h"
0011 
0012 /*
0013  * Audio DMAC peri peri register
0014  */
0015 #define PDMASAR     0x00
0016 #define PDMADAR     0x04
0017 #define PDMACHCR    0x0c
0018 
0019 /* PDMACHCR */
0020 #define PDMACHCR_DE     (1 << 0)
0021 
0022 
0023 struct rsnd_dmaen {
0024     struct dma_chan     *chan;
0025     dma_cookie_t        cookie;
0026     unsigned int        dma_len;
0027 };
0028 
0029 struct rsnd_dmapp {
0030     int         dmapp_id;
0031     u32         chcr;
0032 };
0033 
0034 struct rsnd_dma {
0035     struct rsnd_mod     mod;
0036     struct rsnd_mod     *mod_from;
0037     struct rsnd_mod     *mod_to;
0038     dma_addr_t      src_addr;
0039     dma_addr_t      dst_addr;
0040     union {
0041         struct rsnd_dmaen en;
0042         struct rsnd_dmapp pp;
0043     } dma;
0044 };
0045 
0046 struct rsnd_dma_ctrl {
0047     void __iomem *ppbase;
0048     phys_addr_t ppres;
0049     int dmaen_num;
0050     int dmapp_num;
0051 };
0052 
0053 #define rsnd_priv_to_dmac(p)    ((struct rsnd_dma_ctrl *)(p)->dma)
0054 #define rsnd_mod_to_dma(_mod) container_of((_mod), struct rsnd_dma, mod)
0055 #define rsnd_dma_to_dmaen(dma)  (&(dma)->dma.en)
0056 #define rsnd_dma_to_dmapp(dma)  (&(dma)->dma.pp)
0057 
0058 /* for DEBUG */
0059 static struct rsnd_mod_ops mem_ops = {
0060     .name = "mem",
0061 };
0062 
0063 static struct rsnd_mod mem = {
0064 };
0065 
0066 /*
0067  *      Audio DMAC
0068  */
0069 static void __rsnd_dmaen_complete(struct rsnd_mod *mod,
0070                   struct rsnd_dai_stream *io)
0071 {
0072     if (rsnd_io_is_working(io))
0073         rsnd_dai_period_elapsed(io);
0074 }
0075 
0076 static void rsnd_dmaen_complete(void *data)
0077 {
0078     struct rsnd_mod *mod = data;
0079 
0080     rsnd_mod_interrupt(mod, __rsnd_dmaen_complete);
0081 }
0082 
0083 static struct dma_chan *rsnd_dmaen_request_channel(struct rsnd_dai_stream *io,
0084                            struct rsnd_mod *mod_from,
0085                            struct rsnd_mod *mod_to)
0086 {
0087     if ((!mod_from && !mod_to) ||
0088         (mod_from && mod_to))
0089         return NULL;
0090 
0091     if (mod_from)
0092         return rsnd_mod_dma_req(io, mod_from);
0093     else
0094         return rsnd_mod_dma_req(io, mod_to);
0095 }
0096 
0097 static int rsnd_dmaen_stop(struct rsnd_mod *mod,
0098                struct rsnd_dai_stream *io,
0099                struct rsnd_priv *priv)
0100 {
0101     struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
0102     struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
0103 
0104     if (dmaen->chan)
0105         dmaengine_terminate_async(dmaen->chan);
0106 
0107     return 0;
0108 }
0109 
0110 static int rsnd_dmaen_cleanup(struct rsnd_mod *mod,
0111                   struct rsnd_dai_stream *io,
0112                   struct rsnd_priv *priv)
0113 {
0114     struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
0115     struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
0116 
0117     /*
0118      * DMAEngine release uses mutex lock.
0119      * Thus, it shouldn't be called under spinlock.
0120      * Let's call it under prepare
0121      */
0122     if (dmaen->chan)
0123         dma_release_channel(dmaen->chan);
0124 
0125     dmaen->chan = NULL;
0126 
0127     return 0;
0128 }
0129 
0130 static int rsnd_dmaen_prepare(struct rsnd_mod *mod,
0131                   struct rsnd_dai_stream *io,
0132                   struct rsnd_priv *priv)
0133 {
0134     struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
0135     struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
0136     struct device *dev = rsnd_priv_to_dev(priv);
0137 
0138     /* maybe suspended */
0139     if (dmaen->chan)
0140         return 0;
0141 
0142     /*
0143      * DMAEngine request uses mutex lock.
0144      * Thus, it shouldn't be called under spinlock.
0145      * Let's call it under prepare
0146      */
0147     dmaen->chan = rsnd_dmaen_request_channel(io,
0148                          dma->mod_from,
0149                          dma->mod_to);
0150     if (IS_ERR_OR_NULL(dmaen->chan)) {
0151         dmaen->chan = NULL;
0152         dev_err(dev, "can't get dma channel\n");
0153         return -EIO;
0154     }
0155 
0156     return 0;
0157 }
0158 
0159 static int rsnd_dmaen_start(struct rsnd_mod *mod,
0160                 struct rsnd_dai_stream *io,
0161                 struct rsnd_priv *priv)
0162 {
0163     struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
0164     struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
0165     struct snd_pcm_substream *substream = io->substream;
0166     struct device *dev = rsnd_priv_to_dev(priv);
0167     struct dma_async_tx_descriptor *desc;
0168     struct dma_slave_config cfg = {};
0169     enum dma_slave_buswidth buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
0170     int is_play = rsnd_io_is_play(io);
0171     int ret;
0172 
0173     /*
0174      * in case of monaural data writing or reading through Audio-DMAC
0175      * data is always in Left Justified format, so both src and dst
0176      * DMA Bus width need to be set equal to physical data width.
0177      */
0178     if (rsnd_runtime_channel_original(io) == 1) {
0179         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
0180         int bits = snd_pcm_format_physical_width(runtime->format);
0181 
0182         switch (bits) {
0183         case 8:
0184             buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
0185             break;
0186         case 16:
0187             buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
0188             break;
0189         case 32:
0190             buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
0191             break;
0192         default:
0193             dev_err(dev, "invalid format width %d\n", bits);
0194             return -EINVAL;
0195         }
0196     }
0197 
0198     cfg.direction   = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
0199     cfg.src_addr    = dma->src_addr;
0200     cfg.dst_addr    = dma->dst_addr;
0201     cfg.src_addr_width = buswidth;
0202     cfg.dst_addr_width = buswidth;
0203 
0204     dev_dbg(dev, "%s %pad -> %pad\n",
0205         rsnd_mod_name(mod),
0206         &cfg.src_addr, &cfg.dst_addr);
0207 
0208     ret = dmaengine_slave_config(dmaen->chan, &cfg);
0209     if (ret < 0)
0210         return ret;
0211 
0212     desc = dmaengine_prep_dma_cyclic(dmaen->chan,
0213                      substream->runtime->dma_addr,
0214                      snd_pcm_lib_buffer_bytes(substream),
0215                      snd_pcm_lib_period_bytes(substream),
0216                      is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
0217                      DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
0218 
0219     if (!desc) {
0220         dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
0221         return -EIO;
0222     }
0223 
0224     desc->callback      = rsnd_dmaen_complete;
0225     desc->callback_param    = rsnd_mod_get(dma);
0226 
0227     dmaen->dma_len      = snd_pcm_lib_buffer_bytes(substream);
0228 
0229     dmaen->cookie = dmaengine_submit(desc);
0230     if (dmaen->cookie < 0) {
0231         dev_err(dev, "dmaengine_submit() fail\n");
0232         return -EIO;
0233     }
0234 
0235     dma_async_issue_pending(dmaen->chan);
0236 
0237     return 0;
0238 }
0239 
0240 struct dma_chan *rsnd_dma_request_channel(struct device_node *of_node, char *name,
0241                       struct rsnd_mod *mod, char *x)
0242 {
0243     struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0244     struct device *dev = rsnd_priv_to_dev(priv);
0245     struct dma_chan *chan = NULL;
0246     struct device_node *np;
0247     int i = 0;
0248 
0249     for_each_child_of_node(of_node, np) {
0250         i = rsnd_node_fixed_index(dev, np, name, i);
0251         if (i < 0) {
0252             chan = NULL;
0253             of_node_put(np);
0254             break;
0255         }
0256 
0257         if (i == rsnd_mod_id_raw(mod) && (!chan))
0258             chan = of_dma_request_slave_channel(np, x);
0259         i++;
0260     }
0261 
0262     /* It should call of_node_put(), since, it is rsnd_xxx_of_node() */
0263     of_node_put(of_node);
0264 
0265     return chan;
0266 }
0267 
0268 static int rsnd_dmaen_attach(struct rsnd_dai_stream *io,
0269                struct rsnd_dma *dma,
0270                struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
0271 {
0272     struct rsnd_priv *priv = rsnd_io_to_priv(io);
0273     struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
0274     struct dma_chan *chan;
0275 
0276     /* try to get DMAEngine channel */
0277     chan = rsnd_dmaen_request_channel(io, mod_from, mod_to);
0278     if (IS_ERR_OR_NULL(chan)) {
0279         /* Let's follow when -EPROBE_DEFER case */
0280         if (PTR_ERR(chan) == -EPROBE_DEFER)
0281             return PTR_ERR(chan);
0282 
0283         /*
0284          * DMA failed. try to PIO mode
0285          * see
0286          *  rsnd_ssi_fallback()
0287          *  rsnd_rdai_continuance_probe()
0288          */
0289         return -EAGAIN;
0290     }
0291 
0292     /*
0293      * use it for IPMMU if needed
0294      * see
0295      *  rsnd_preallocate_pages()
0296      */
0297     io->dmac_dev = chan->device->dev;
0298 
0299     dma_release_channel(chan);
0300 
0301     dmac->dmaen_num++;
0302 
0303     return 0;
0304 }
0305 
0306 static int rsnd_dmaen_pointer(struct rsnd_mod *mod,
0307                   struct rsnd_dai_stream *io,
0308                   snd_pcm_uframes_t *pointer)
0309 {
0310     struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
0311     struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
0312     struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
0313     struct dma_tx_state state;
0314     enum dma_status status;
0315     unsigned int pos = 0;
0316 
0317     status = dmaengine_tx_status(dmaen->chan, dmaen->cookie, &state);
0318     if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
0319         if (state.residue > 0 && state.residue <= dmaen->dma_len)
0320             pos = dmaen->dma_len - state.residue;
0321     }
0322     *pointer = bytes_to_frames(runtime, pos);
0323 
0324     return 0;
0325 }
0326 
0327 static struct rsnd_mod_ops rsnd_dmaen_ops = {
0328     .name       = "audmac",
0329     .prepare    = rsnd_dmaen_prepare,
0330     .cleanup    = rsnd_dmaen_cleanup,
0331     .start      = rsnd_dmaen_start,
0332     .stop       = rsnd_dmaen_stop,
0333     .pointer    = rsnd_dmaen_pointer,
0334     .get_status = rsnd_mod_get_status,
0335 };
0336 
0337 /*
0338  *      Audio DMAC peri peri
0339  */
0340 static const u8 gen2_id_table_ssiu[] = {
0341     /* SSI00 ~ SSI07 */
0342     0x00, 0x01, 0x02, 0x03, 0x39, 0x3a, 0x3b, 0x3c,
0343     /* SSI10 ~ SSI17 */
0344     0x04, 0x05, 0x06, 0x07, 0x3d, 0x3e, 0x3f, 0x40,
0345     /* SSI20 ~ SSI27 */
0346     0x08, 0x09, 0x0a, 0x0b, 0x41, 0x42, 0x43, 0x44,
0347     /* SSI30 ~ SSI37 */
0348     0x0c, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b,
0349     /* SSI40 ~ SSI47 */
0350     0x0d, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52,
0351     /* SSI5 */
0352     0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0353     /* SSI6 */
0354     0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0355     /* SSI7 */
0356     0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0357     /* SSI8 */
0358     0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0359     /* SSI90 ~ SSI97 */
0360     0x12, 0x13, 0x14, 0x15, 0x53, 0x54, 0x55, 0x56,
0361 };
0362 static const u8 gen2_id_table_scu[] = {
0363     0x2d, /* SCU_SRCI0 */
0364     0x2e, /* SCU_SRCI1 */
0365     0x2f, /* SCU_SRCI2 */
0366     0x30, /* SCU_SRCI3 */
0367     0x31, /* SCU_SRCI4 */
0368     0x32, /* SCU_SRCI5 */
0369     0x33, /* SCU_SRCI6 */
0370     0x34, /* SCU_SRCI7 */
0371     0x35, /* SCU_SRCI8 */
0372     0x36, /* SCU_SRCI9 */
0373 };
0374 static const u8 gen2_id_table_cmd[] = {
0375     0x37, /* SCU_CMD0 */
0376     0x38, /* SCU_CMD1 */
0377 };
0378 
0379 static u32 rsnd_dmapp_get_id(struct rsnd_dai_stream *io,
0380                  struct rsnd_mod *mod)
0381 {
0382     struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
0383     struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
0384     struct rsnd_mod *src = rsnd_io_to_mod_src(io);
0385     struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
0386     const u8 *entry = NULL;
0387     int id = 255;
0388     int size = 0;
0389 
0390     if ((mod == ssi) ||
0391         (mod == ssiu)) {
0392         int busif = rsnd_mod_id_sub(ssiu);
0393 
0394         entry = gen2_id_table_ssiu;
0395         size = ARRAY_SIZE(gen2_id_table_ssiu);
0396         id = (rsnd_mod_id(mod) * 8) + busif;
0397     } else if (mod == src) {
0398         entry = gen2_id_table_scu;
0399         size = ARRAY_SIZE(gen2_id_table_scu);
0400         id = rsnd_mod_id(mod);
0401     } else if (mod == dvc) {
0402         entry = gen2_id_table_cmd;
0403         size = ARRAY_SIZE(gen2_id_table_cmd);
0404         id = rsnd_mod_id(mod);
0405     }
0406 
0407     if ((!entry) || (size <= id)) {
0408         struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));
0409 
0410         dev_err(dev, "unknown connection (%s)\n", rsnd_mod_name(mod));
0411 
0412         /* use non-prohibited SRS number as error */
0413         return 0x00; /* SSI00 */
0414     }
0415 
0416     return entry[id];
0417 }
0418 
0419 static u32 rsnd_dmapp_get_chcr(struct rsnd_dai_stream *io,
0420                    struct rsnd_mod *mod_from,
0421                    struct rsnd_mod *mod_to)
0422 {
0423     return  (rsnd_dmapp_get_id(io, mod_from) << 24) +
0424         (rsnd_dmapp_get_id(io, mod_to) << 16);
0425 }
0426 
0427 #define rsnd_dmapp_addr(dmac, dma, reg) \
0428     (dmac->ppbase + 0x20 + reg + \
0429      (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id))
0430 static void rsnd_dmapp_write(struct rsnd_dma *dma, u32 data, u32 reg)
0431 {
0432     struct rsnd_mod *mod = rsnd_mod_get(dma);
0433     struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0434     struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
0435     struct device *dev = rsnd_priv_to_dev(priv);
0436 
0437     dev_dbg(dev, "w 0x%px : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data);
0438 
0439     iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg));
0440 }
0441 
0442 static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg)
0443 {
0444     struct rsnd_mod *mod = rsnd_mod_get(dma);
0445     struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0446     struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
0447 
0448     return ioread32(rsnd_dmapp_addr(dmac, dma, reg));
0449 }
0450 
0451 static void rsnd_dmapp_bset(struct rsnd_dma *dma, u32 data, u32 mask, u32 reg)
0452 {
0453     struct rsnd_mod *mod = rsnd_mod_get(dma);
0454     struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0455     struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
0456     void __iomem *addr = rsnd_dmapp_addr(dmac, dma, reg);
0457     u32 val = ioread32(addr);
0458 
0459     val &= ~mask;
0460     val |= (data & mask);
0461 
0462     iowrite32(val, addr);
0463 }
0464 
0465 static int rsnd_dmapp_stop(struct rsnd_mod *mod,
0466                struct rsnd_dai_stream *io,
0467                struct rsnd_priv *priv)
0468 {
0469     struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
0470     int i;
0471 
0472     rsnd_dmapp_bset(dma, 0,  PDMACHCR_DE, PDMACHCR);
0473 
0474     for (i = 0; i < 1024; i++) {
0475         if (0 == (rsnd_dmapp_read(dma, PDMACHCR) & PDMACHCR_DE))
0476             return 0;
0477         udelay(1);
0478     }
0479 
0480     return -EIO;
0481 }
0482 
0483 static int rsnd_dmapp_start(struct rsnd_mod *mod,
0484                 struct rsnd_dai_stream *io,
0485                 struct rsnd_priv *priv)
0486 {
0487     struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
0488     struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
0489 
0490     rsnd_dmapp_write(dma, dma->src_addr,    PDMASAR);
0491     rsnd_dmapp_write(dma, dma->dst_addr,    PDMADAR);
0492     rsnd_dmapp_write(dma, dmapp->chcr,  PDMACHCR);
0493 
0494     return 0;
0495 }
0496 
0497 static int rsnd_dmapp_attach(struct rsnd_dai_stream *io,
0498                  struct rsnd_dma *dma,
0499                  struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
0500 {
0501     struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
0502     struct rsnd_priv *priv = rsnd_io_to_priv(io);
0503     struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
0504     struct device *dev = rsnd_priv_to_dev(priv);
0505 
0506     dmapp->dmapp_id = dmac->dmapp_num;
0507     dmapp->chcr = rsnd_dmapp_get_chcr(io, mod_from, mod_to) | PDMACHCR_DE;
0508 
0509     dmac->dmapp_num++;
0510 
0511     dev_dbg(dev, "id/src/dst/chcr = %d/%pad/%pad/%08x\n",
0512         dmapp->dmapp_id, &dma->src_addr, &dma->dst_addr, dmapp->chcr);
0513 
0514     return 0;
0515 }
0516 
0517 #ifdef CONFIG_DEBUG_FS
0518 static void rsnd_dmapp_debug_info(struct seq_file *m,
0519                   struct rsnd_dai_stream *io,
0520                   struct rsnd_mod *mod)
0521 {
0522     struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
0523     struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
0524     struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
0525     struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
0526 
0527     rsnd_debugfs_reg_show(m, dmac->ppres, dmac->ppbase,
0528                   0x20 + 0x10 * dmapp->dmapp_id, 0x10);
0529 }
0530 #define DEBUG_INFO .debug_info = rsnd_dmapp_debug_info
0531 #else
0532 #define DEBUG_INFO
0533 #endif
0534 
0535 static struct rsnd_mod_ops rsnd_dmapp_ops = {
0536     .name       = "audmac-pp",
0537     .start      = rsnd_dmapp_start,
0538     .stop       = rsnd_dmapp_stop,
0539     .quit       = rsnd_dmapp_stop,
0540     .get_status = rsnd_mod_get_status,
0541     DEBUG_INFO
0542 };
0543 
0544 /*
0545  *      Common DMAC Interface
0546  */
0547 
0548 /*
0549  *  DMA read/write register offset
0550  *
0551  *  RSND_xxx_I_N    for Audio DMAC input
0552  *  RSND_xxx_O_N    for Audio DMAC output
0553  *  RSND_xxx_I_P    for Audio DMAC peri peri input
0554  *  RSND_xxx_O_P    for Audio DMAC peri peri output
0555  *
0556  *  ex) R-Car H2 case
0557  *        mod        / DMAC in    / DMAC out   / DMAC PP in / DMAC pp out
0558  *  SSI : 0xec541000 / 0xec241008 / 0xec24100c
0559  *  SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000
0560  *  SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000
0561  *  CMD : 0xec500000 /            / 0xec008000                0xec308000
0562  */
0563 #define RDMA_SSI_I_N(addr, i)   (addr ##_reg - 0x00300000 + (0x40 * i) + 0x8)
0564 #define RDMA_SSI_O_N(addr, i)   (addr ##_reg - 0x00300000 + (0x40 * i) + 0xc)
0565 
0566 #define RDMA_SSIU_I_N(addr, i, j) (addr ##_reg - 0x00441000 + (0x1000 * (i)) + (((j) / 4) * 0xA000) + (((j) % 4) * 0x400) - (0x4000 * ((i) / 9) * ((j) / 4)))
0567 #define RDMA_SSIU_O_N(addr, i, j) RDMA_SSIU_I_N(addr, i, j)
0568 
0569 #define RDMA_SSIU_I_P(addr, i, j) (addr ##_reg - 0x00141000 + (0x1000 * (i)) + (((j) / 4) * 0xA000) + (((j) % 4) * 0x400) - (0x4000 * ((i) / 9) * ((j) / 4)))
0570 #define RDMA_SSIU_O_P(addr, i, j) RDMA_SSIU_I_P(addr, i, j)
0571 
0572 #define RDMA_SRC_I_N(addr, i)   (addr ##_reg - 0x00500000 + (0x400 * i))
0573 #define RDMA_SRC_O_N(addr, i)   (addr ##_reg - 0x004fc000 + (0x400 * i))
0574 
0575 #define RDMA_SRC_I_P(addr, i)   (addr ##_reg - 0x00200000 + (0x400 * i))
0576 #define RDMA_SRC_O_P(addr, i)   (addr ##_reg - 0x001fc000 + (0x400 * i))
0577 
0578 #define RDMA_CMD_O_N(addr, i)   (addr ##_reg - 0x004f8000 + (0x400 * i))
0579 #define RDMA_CMD_O_P(addr, i)   (addr ##_reg - 0x001f8000 + (0x400 * i))
0580 
0581 static dma_addr_t
0582 rsnd_gen2_dma_addr(struct rsnd_dai_stream *io,
0583            struct rsnd_mod *mod,
0584            int is_play, int is_from)
0585 {
0586     struct rsnd_priv *priv = rsnd_io_to_priv(io);
0587     struct device *dev = rsnd_priv_to_dev(priv);
0588     phys_addr_t ssi_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SSI);
0589     phys_addr_t src_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SCU);
0590     int is_ssi = !!(rsnd_io_to_mod_ssi(io) == mod) ||
0591              !!(rsnd_io_to_mod_ssiu(io) == mod);
0592     int use_src = !!rsnd_io_to_mod_src(io);
0593     int use_cmd = !!rsnd_io_to_mod_dvc(io) ||
0594               !!rsnd_io_to_mod_mix(io) ||
0595               !!rsnd_io_to_mod_ctu(io);
0596     int id = rsnd_mod_id(mod);
0597     int busif = rsnd_mod_id_sub(rsnd_io_to_mod_ssiu(io));
0598     struct dma_addr {
0599         dma_addr_t out_addr;
0600         dma_addr_t in_addr;
0601     } dma_addrs[3][2][3] = {
0602         /* SRC */
0603         /* Capture */
0604         {{{ 0,              0 },
0605           { RDMA_SRC_O_N(src, id),  RDMA_SRC_I_P(src, id) },
0606           { RDMA_CMD_O_N(src, id),  RDMA_SRC_I_P(src, id) } },
0607          /* Playback */
0608          {{ 0,              0, },
0609           { RDMA_SRC_O_P(src, id),  RDMA_SRC_I_N(src, id) },
0610           { RDMA_CMD_O_P(src, id),  RDMA_SRC_I_N(src, id) } }
0611         },
0612         /* SSI */
0613         /* Capture */
0614         {{{ RDMA_SSI_O_N(ssi, id),      0 },
0615           { RDMA_SSIU_O_P(ssi, id, busif),  0 },
0616           { RDMA_SSIU_O_P(ssi, id, busif),  0 } },
0617          /* Playback */
0618          {{ 0,          RDMA_SSI_I_N(ssi, id) },
0619           { 0,          RDMA_SSIU_I_P(ssi, id, busif) },
0620           { 0,          RDMA_SSIU_I_P(ssi, id, busif) } }
0621         },
0622         /* SSIU */
0623         /* Capture */
0624         {{{ RDMA_SSIU_O_N(ssi, id, busif),  0 },
0625           { RDMA_SSIU_O_P(ssi, id, busif),  0 },
0626           { RDMA_SSIU_O_P(ssi, id, busif),  0 } },
0627          /* Playback */
0628          {{ 0,          RDMA_SSIU_I_N(ssi, id, busif) },
0629           { 0,          RDMA_SSIU_I_P(ssi, id, busif) },
0630           { 0,          RDMA_SSIU_I_P(ssi, id, busif) } } },
0631     };
0632 
0633     /*
0634      * FIXME
0635      *
0636      * We can't support SSI9-4/5/6/7, because its address is
0637      * out of calculation rule
0638      */
0639     if ((id == 9) && (busif >= 4))
0640         dev_err(dev, "This driver doesn't support SSI%d-%d, so far",
0641             id, busif);
0642 
0643     /* it shouldn't happen */
0644     if (use_cmd && !use_src)
0645         dev_err(dev, "DVC is selected without SRC\n");
0646 
0647     /* use SSIU or SSI ? */
0648     if (is_ssi && rsnd_ssi_use_busif(io))
0649         is_ssi++;
0650 
0651     return (is_from) ?
0652         dma_addrs[is_ssi][is_play][use_src + use_cmd].out_addr :
0653         dma_addrs[is_ssi][is_play][use_src + use_cmd].in_addr;
0654 }
0655 
0656 static dma_addr_t rsnd_dma_addr(struct rsnd_dai_stream *io,
0657                 struct rsnd_mod *mod,
0658                 int is_play, int is_from)
0659 {
0660     struct rsnd_priv *priv = rsnd_io_to_priv(io);
0661 
0662     /*
0663      * gen1 uses default DMA addr
0664      */
0665     if (rsnd_is_gen1(priv))
0666         return 0;
0667 
0668     if (!mod)
0669         return 0;
0670 
0671     return rsnd_gen2_dma_addr(io, mod, is_play, is_from);
0672 }
0673 
0674 #define MOD_MAX (RSND_MOD_MAX + 1) /* +Memory */
0675 static void rsnd_dma_of_path(struct rsnd_mod *this,
0676                  struct rsnd_dai_stream *io,
0677                  int is_play,
0678                  struct rsnd_mod **mod_from,
0679                  struct rsnd_mod **mod_to)
0680 {
0681     struct rsnd_mod *ssi;
0682     struct rsnd_mod *src = rsnd_io_to_mod_src(io);
0683     struct rsnd_mod *ctu = rsnd_io_to_mod_ctu(io);
0684     struct rsnd_mod *mix = rsnd_io_to_mod_mix(io);
0685     struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
0686     struct rsnd_mod *mod[MOD_MAX];
0687     struct rsnd_mod *mod_start, *mod_end;
0688     struct rsnd_priv *priv = rsnd_mod_to_priv(this);
0689     struct device *dev = rsnd_priv_to_dev(priv);
0690     int nr, i, idx;
0691 
0692     /*
0693      * It should use "rcar_sound,ssiu" on DT.
0694      * But, we need to keep compatibility for old version.
0695      *
0696      * If it has "rcar_sound.ssiu", it will be used.
0697      * If not, "rcar_sound.ssi" will be used.
0698      * see
0699      *  rsnd_ssiu_dma_req()
0700      *  rsnd_ssi_dma_req()
0701      */
0702     if (rsnd_ssiu_of_node(priv)) {
0703         struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
0704 
0705         /* use SSIU */
0706         ssi = ssiu;
0707         if (this == rsnd_io_to_mod_ssi(io))
0708             this = ssiu;
0709     } else {
0710         /* keep compatible, use SSI */
0711         ssi = rsnd_io_to_mod_ssi(io);
0712     }
0713 
0714     if (!ssi)
0715         return;
0716 
0717     nr = 0;
0718     for (i = 0; i < MOD_MAX; i++) {
0719         mod[i] = NULL;
0720         nr += !!rsnd_io_to_mod(io, i);
0721     }
0722 
0723     /*
0724      * [S] -*-> [E]
0725      * [S] -*-> SRC -o-> [E]
0726      * [S] -*-> SRC -> DVC -o-> [E]
0727      * [S] -*-> SRC -> CTU -> MIX -> DVC -o-> [E]
0728      *
0729      * playback [S] = mem
0730      *      [E] = SSI
0731      *
0732      * capture  [S] = SSI
0733      *      [E] = mem
0734      *
0735      * -*->     Audio DMAC
0736      * -o->     Audio DMAC peri peri
0737      */
0738     mod_start   = (is_play) ? NULL : ssi;
0739     mod_end     = (is_play) ? ssi  : NULL;
0740 
0741     idx = 0;
0742     mod[idx++] = mod_start;
0743     for (i = 1; i < nr; i++) {
0744         if (src) {
0745             mod[idx++] = src;
0746             src = NULL;
0747         } else if (ctu) {
0748             mod[idx++] = ctu;
0749             ctu = NULL;
0750         } else if (mix) {
0751             mod[idx++] = mix;
0752             mix = NULL;
0753         } else if (dvc) {
0754             mod[idx++] = dvc;
0755             dvc = NULL;
0756         }
0757     }
0758     mod[idx] = mod_end;
0759 
0760     /*
0761      *      | SSI | SRC |
0762      * -------------+-----+-----+
0763      *  is_play |  o  |  *  |
0764      * !is_play |  *  |  o  |
0765      */
0766     if ((this == ssi) == (is_play)) {
0767         *mod_from   = mod[idx - 1];
0768         *mod_to     = mod[idx];
0769     } else {
0770         *mod_from   = mod[0];
0771         *mod_to     = mod[1];
0772     }
0773 
0774     dev_dbg(dev, "module connection (this is %s)\n", rsnd_mod_name(this));
0775     for (i = 0; i <= idx; i++) {
0776         dev_dbg(dev, "  %s%s\n",
0777             rsnd_mod_name(mod[i] ? mod[i] : &mem),
0778             (mod[i] == *mod_from) ? " from" :
0779             (mod[i] == *mod_to)   ? " to" : "");
0780     }
0781 }
0782 
0783 static int rsnd_dma_alloc(struct rsnd_dai_stream *io, struct rsnd_mod *mod,
0784               struct rsnd_mod **dma_mod)
0785 {
0786     struct rsnd_mod *mod_from = NULL;
0787     struct rsnd_mod *mod_to = NULL;
0788     struct rsnd_priv *priv = rsnd_io_to_priv(io);
0789     struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
0790     struct device *dev = rsnd_priv_to_dev(priv);
0791     struct rsnd_dma *dma;
0792     struct rsnd_mod_ops *ops;
0793     enum rsnd_mod_type type;
0794     int (*attach)(struct rsnd_dai_stream *io, struct rsnd_dma *dma,
0795               struct rsnd_mod *mod_from, struct rsnd_mod *mod_to);
0796     int is_play = rsnd_io_is_play(io);
0797     int ret, dma_id;
0798 
0799     /*
0800      * DMA failed. try to PIO mode
0801      * see
0802      *  rsnd_ssi_fallback()
0803      *  rsnd_rdai_continuance_probe()
0804      */
0805     if (!dmac)
0806         return -EAGAIN;
0807 
0808     rsnd_dma_of_path(mod, io, is_play, &mod_from, &mod_to);
0809 
0810     /* for Gen2 or later */
0811     if (mod_from && mod_to) {
0812         ops = &rsnd_dmapp_ops;
0813         attach  = rsnd_dmapp_attach;
0814         dma_id  = dmac->dmapp_num;
0815         type    = RSND_MOD_AUDMAPP;
0816     } else {
0817         ops = &rsnd_dmaen_ops;
0818         attach  = rsnd_dmaen_attach;
0819         dma_id  = dmac->dmaen_num;
0820         type    = RSND_MOD_AUDMA;
0821     }
0822 
0823     /* for Gen1, overwrite */
0824     if (rsnd_is_gen1(priv)) {
0825         ops = &rsnd_dmaen_ops;
0826         attach  = rsnd_dmaen_attach;
0827         dma_id  = dmac->dmaen_num;
0828         type    = RSND_MOD_AUDMA;
0829     }
0830 
0831     dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
0832     if (!dma)
0833         return -ENOMEM;
0834 
0835     *dma_mod = rsnd_mod_get(dma);
0836 
0837     ret = rsnd_mod_init(priv, *dma_mod, ops, NULL,
0838                 type, dma_id);
0839     if (ret < 0)
0840         return ret;
0841 
0842     dev_dbg(dev, "%s %s -> %s\n",
0843         rsnd_mod_name(*dma_mod),
0844         rsnd_mod_name(mod_from ? mod_from : &mem),
0845         rsnd_mod_name(mod_to   ? mod_to   : &mem));
0846 
0847     ret = attach(io, dma, mod_from, mod_to);
0848     if (ret < 0)
0849         return ret;
0850 
0851     dma->src_addr = rsnd_dma_addr(io, mod_from, is_play, 1);
0852     dma->dst_addr = rsnd_dma_addr(io, mod_to,   is_play, 0);
0853     dma->mod_from = mod_from;
0854     dma->mod_to   = mod_to;
0855 
0856     return 0;
0857 }
0858 
0859 int rsnd_dma_attach(struct rsnd_dai_stream *io, struct rsnd_mod *mod,
0860             struct rsnd_mod **dma_mod)
0861 {
0862     if (!(*dma_mod)) {
0863         int ret = rsnd_dma_alloc(io, mod, dma_mod);
0864 
0865         if (ret < 0)
0866             return ret;
0867     }
0868 
0869     return rsnd_dai_connect(*dma_mod, io, (*dma_mod)->type);
0870 }
0871 
0872 int rsnd_dma_probe(struct rsnd_priv *priv)
0873 {
0874     struct platform_device *pdev = rsnd_priv_to_pdev(priv);
0875     struct device *dev = rsnd_priv_to_dev(priv);
0876     struct rsnd_dma_ctrl *dmac;
0877     struct resource *res;
0878 
0879     /*
0880      * for Gen1
0881      */
0882     if (rsnd_is_gen1(priv))
0883         return 0;
0884 
0885     /*
0886      * for Gen2 or later
0887      */
0888     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp");
0889     dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL);
0890     if (!dmac || !res) {
0891         dev_err(dev, "dma allocate failed\n");
0892         return 0; /* it will be PIO mode */
0893     }
0894 
0895     dmac->dmapp_num = 0;
0896     dmac->ppres  = res->start;
0897     dmac->ppbase = devm_ioremap_resource(dev, res);
0898     if (IS_ERR(dmac->ppbase))
0899         return PTR_ERR(dmac->ppbase);
0900 
0901     priv->dma = dmac;
0902 
0903     /* dummy mem mod for debug */
0904     return rsnd_mod_init(NULL, &mem, &mem_ops, NULL, 0, 0);
0905 }