1/* 2 * Renesas R-Car Audio DMAC support 3 * 4 * Copyright (C) 2015 Renesas Electronics Corp. 5 * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 6 * 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. 10 */ 11#include <linux/delay.h> 12#include <linux/of_dma.h> 13#include "rsnd.h" 14 15/* 16 * Audio DMAC peri peri register 17 */ 18#define PDMASAR 0x00 19#define PDMADAR 0x04 20#define PDMACHCR 0x0c 21 22/* PDMACHCR */ 23#define PDMACHCR_DE (1 << 0) 24 25struct rsnd_dma_ctrl { 26 void __iomem *base; 27 int dmapp_num; 28}; 29 30#define rsnd_priv_to_dmac(p) ((struct rsnd_dma_ctrl *)(p)->dma) 31 32/* 33 * Audio DMAC 34 */ 35static void rsnd_dmaen_complete(void *data) 36{ 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); 40 41 /* 42 * Renesas sound Gen1 needs 1 DMAC, 43 * Gen2 needs 2 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. 47 * 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 51 */ 52 53 rsnd_dai_pointer_update(io, io->byte_per_period); 54} 55 56static void rsnd_dmaen_stop(struct rsnd_dma *dma) 57{ 58 struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma); 59 60 dmaengine_terminate_all(dmaen->chan); 61} 62 63static void rsnd_dmaen_start(struct rsnd_dma *dma) 64{ 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); 73 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); 80 81 if (!desc) { 82 dev_err(dev, "dmaengine_prep_slave_sg() fail\n"); 83 return; 84 } 85 86 desc->callback = rsnd_dmaen_complete; 87 desc->callback_param = dma; 88 89 if (dmaengine_submit(desc) < 0) { 90 dev_err(dev, "dmaengine_submit() fail\n"); 91 return; 92 } 93 94 dma_async_issue_pending(dmaen->chan); 95} 96 97struct dma_chan *rsnd_dma_request_channel(struct device_node *of_node, 98 struct rsnd_mod *mod, char *name) 99{ 100 struct dma_chan *chan; 101 struct device_node *np; 102 int i = 0; 103 104 for_each_child_of_node(of_node, np) { 105 if (i == rsnd_mod_id(mod)) 106 break; 107 i++; 108 } 109 110 chan = of_dma_request_slave_channel(np, name); 111 112 of_node_put(np); 113 of_node_put(of_node); 114 115 return chan; 116} 117 118static struct dma_chan *rsnd_dmaen_request_channel(struct rsnd_mod *mod_from, 119 struct rsnd_mod *mod_to) 120{ 121 if ((!mod_from && !mod_to) || 122 (mod_from && mod_to)) 123 return NULL; 124 125 if (mod_from) 126 return rsnd_mod_dma_req(mod_from); 127 else 128 return rsnd_mod_dma_req(mod_to); 129} 130 131static 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) 133{ 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); 140 int ret; 141 142 if (dmaen->chan) { 143 dev_err(dev, "it already has dma channel\n"); 144 return -EIO; 145 } 146 147 if (dev->of_node) { 148 dmaen->chan = rsnd_dmaen_request_channel(mod_from, mod_to); 149 } else { 150 dma_cap_mask_t mask; 151 152 dma_cap_zero(mask); 153 dma_cap_set(DMA_SLAVE, mask); 154 155 dmaen->chan = dma_request_channel(mask, shdma_chan_filter, 156 (void *)id); 157 } 158 if (IS_ERR_OR_NULL(dmaen->chan)) { 159 dmaen->chan = NULL; 160 dev_err(dev, "can't get dma channel\n"); 161 goto rsnd_dma_channel_err; 162 } 163 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; 169 170 dev_dbg(dev, "dma : %pad -> %pad\n", 171 &cfg.src_addr, &cfg.dst_addr); 172 173 ret = dmaengine_slave_config(dmaen->chan, &cfg); 174 if (ret < 0) 175 goto rsnd_dma_init_err; 176 177 return 0; 178 179rsnd_dma_init_err: 180 rsnd_dma_quit(dma); 181rsnd_dma_channel_err: 182 183 /* 184 * DMA failed. try to PIO mode 185 * see 186 * rsnd_ssi_fallback() 187 * rsnd_rdai_continuance_probe() 188 */ 189 return -EAGAIN; 190} 191 192static void rsnd_dmaen_quit(struct rsnd_dma *dma) 193{ 194 struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma); 195 196 if (dmaen->chan) 197 dma_release_channel(dmaen->chan); 198 199 dmaen->chan = NULL; 200} 201 202static 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, 207}; 208 209/* 210 * Audio DMAC peri peri 211 */ 212static const u8 gen2_id_table_ssiu[] = { 213 0x00, /* SSI00 */ 214 0x04, /* SSI10 */ 215 0x08, /* SSI20 */ 216 0x0c, /* SSI3 */ 217 0x0d, /* SSI4 */ 218 0x0e, /* SSI5 */ 219 0x0f, /* SSI6 */ 220 0x10, /* SSI7 */ 221 0x11, /* SSI8 */ 222 0x12, /* SSI90 */ 223}; 224static 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 */ 235}; 236static const u8 gen2_id_table_cmd[] = { 237 0x37, /* SCU_CMD0 */ 238 0x38, /* SCU_CMD1 */ 239}; 240 241static u32 rsnd_dmapp_get_id(struct rsnd_mod *mod) 242{ 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); 249 int size = 0; 250 251 if (mod == ssi) { 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); 260 } 261 262 if (!entry) 263 return 0xFF; 264 265 if (size <= id) 266 return 0xFF; 267 268 return entry[id]; 269} 270 271static u32 rsnd_dmapp_get_chcr(struct rsnd_mod *mod_from, 272 struct rsnd_mod *mod_to) 273{ 274 return (rsnd_dmapp_get_id(mod_from) << 24) + 275 (rsnd_dmapp_get_id(mod_to) << 16); 276} 277 278#define rsnd_dmapp_addr(dmac, dma, reg) \ 279 (dmac->base + 0x20 + reg + \ 280 (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id)) 281static void rsnd_dmapp_write(struct rsnd_dma *dma, u32 data, u32 reg) 282{ 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); 287 288 dev_dbg(dev, "w %p : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data); 289 290 iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg)); 291} 292 293static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg) 294{ 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); 298 299 return ioread32(rsnd_dmapp_addr(dmac, dma, reg)); 300} 301 302static void rsnd_dmapp_stop(struct rsnd_dma *dma) 303{ 304 int i; 305 306 rsnd_dmapp_write(dma, 0, PDMACHCR); 307 308 for (i = 0; i < 1024; i++) { 309 if (0 == rsnd_dmapp_read(dma, PDMACHCR)) 310 return; 311 udelay(1); 312 } 313} 314 315static void rsnd_dmapp_start(struct rsnd_dma *dma) 316{ 317 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma); 318 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); 322} 323 324static 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) 326{ 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); 330 331 dmapp->dmapp_id = dmac->dmapp_num; 332 dmapp->chcr = rsnd_dmapp_get_chcr(mod_from, mod_to) | PDMACHCR_DE; 333 334 dmac->dmapp_num++; 335 336 rsnd_dmapp_stop(dma); 337 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); 340 341 return 0; 342} 343 344static 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, 349}; 350 351/* 352 * Common DMAC Interface 353 */ 354 355/* 356 * DMA read/write register offset 357 * 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 362 * 363 * ex) R-Car H2 case 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 369 */ 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) 372 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)) 375 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)) 378 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)) 381 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)) 384 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)) 387 388static dma_addr_t 389rsnd_gen2_dma_addr(struct rsnd_priv *priv, 390 struct rsnd_mod *mod, 391 int is_play, int is_from) 392{ 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); 401 struct dma_addr { 402 dma_addr_t out_addr; 403 dma_addr_t in_addr; 404 } dma_addrs[3][2][3] = { 405 /* SRC */ 406 {{{ 0, 0 }, 407 /* Capture */ 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) } }, 410 /* Playback */ 411 {{ 0, 0, }, 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) } } 414 }, 415 /* SSI */ 416 /* Capture */ 417 {{{ RDMA_SSI_O_N(ssi, id), 0 }, 418 { RDMA_SSIU_O_P(ssi, id), 0 }, 419 { RDMA_SSIU_O_P(ssi, id), 0 } }, 420 /* Playback */ 421 {{ 0, RDMA_SSI_I_N(ssi, id) }, 422 { 0, RDMA_SSIU_I_P(ssi, id) }, 423 { 0, RDMA_SSIU_I_P(ssi, id) } } 424 }, 425 /* SSIU */ 426 /* Capture */ 427 {{{ RDMA_SSIU_O_N(ssi, id), 0 }, 428 { RDMA_SSIU_O_P(ssi, id), 0 }, 429 { RDMA_SSIU_O_P(ssi, id), 0 } }, 430 /* Playback */ 431 {{ 0, RDMA_SSIU_I_N(ssi, id) }, 432 { 0, RDMA_SSIU_I_P(ssi, id) }, 433 { 0, RDMA_SSIU_I_P(ssi, id) } } }, 434 }; 435 436 /* it shouldn't happen */ 437 if (use_dvc && !use_src) 438 dev_err(dev, "DVC is selected without SRC\n"); 439 440 /* use SSIU or SSI ? */ 441 if (is_ssi && rsnd_ssi_use_busif(mod)) 442 is_ssi++; 443 444 return (is_from) ? 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; 447} 448 449static dma_addr_t rsnd_dma_addr(struct rsnd_priv *priv, 450 struct rsnd_mod *mod, 451 int is_play, int is_from) 452{ 453 /* 454 * gen1 uses default DMA addr 455 */ 456 if (rsnd_is_gen1(priv)) 457 return 0; 458 459 if (!mod) 460 return 0; 461 462 return rsnd_gen2_dma_addr(priv, mod, is_play, is_from); 463} 464 465#define MOD_MAX 4 /* MEM/SSI/SRC/DVC */ 466static void rsnd_dma_of_path(struct rsnd_dma *dma, 467 int is_play, 468 struct rsnd_mod **mod_from, 469 struct rsnd_mod **mod_to) 470{ 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]; 477 int i, index; 478 479 480 for (i = 0; i < MOD_MAX; i++) 481 mod[i] = NULL; 482 483 /* 484 * in play case... 485 * 486 * src -> dst 487 * 488 * mem -> SSI 489 * mem -> SRC -> SSI 490 * mem -> SRC -> DVC -> SSI 491 */ 492 mod[0] = NULL; /* for "mem" */ 493 index = 1; 494 for (i = 1; i < MOD_MAX; i++) { 495 if (!src) { 496 mod[i] = ssi; 497 } else if (!dvc) { 498 mod[i] = src; 499 src = NULL; 500 } else { 501 if ((!is_play) && (this == src)) 502 this = dvc; 503 504 mod[i] = (is_play) ? src : dvc; 505 i++; 506 mod[i] = (is_play) ? dvc : src; 507 src = NULL; 508 dvc = NULL; 509 } 510 511 if (mod[i] == this) 512 index = i; 513 514 if (mod[i] == ssi) 515 break; 516 } 517 518 if (is_play) { 519 *mod_from = mod[index - 1]; 520 *mod_to = mod[index]; 521 } else { 522 *mod_from = mod[index]; 523 *mod_to = mod[index - 1]; 524 } 525} 526 527void rsnd_dma_stop(struct rsnd_dma *dma) 528{ 529 dma->ops->stop(dma); 530} 531 532void rsnd_dma_start(struct rsnd_dma *dma) 533{ 534 dma->ops->start(dma); 535} 536 537void rsnd_dma_quit(struct rsnd_dma *dma) 538{ 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); 542 543 if (!dmac) 544 return; 545 546 dma->ops->quit(dma); 547} 548 549int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, int id) 550{ 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); 557 558 /* 559 * DMA failed. try to PIO mode 560 * see 561 * rsnd_ssi_fallback() 562 * rsnd_rdai_continuance_probe() 563 */ 564 if (!dmac) 565 return -EAGAIN; 566 567 rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to); 568 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); 571 572 /* for Gen2 */ 573 if (mod_from && mod_to) 574 dma->ops = &rsnd_dmapp_ops; 575 else 576 dma->ops = &rsnd_dmaen_ops; 577 578 /* for Gen1, overwrite */ 579 if (rsnd_is_gen1(priv)) 580 dma->ops = &rsnd_dmaen_ops; 581 582 return dma->ops->init(priv, dma, id, mod_from, mod_to); 583} 584 585int rsnd_dma_probe(struct platform_device *pdev, 586 const struct rsnd_of_data *of_data, 587 struct rsnd_priv *priv) 588{ 589 struct device *dev = rsnd_priv_to_dev(priv); 590 struct rsnd_dma_ctrl *dmac; 591 struct resource *res; 592 593 /* 594 * for Gen1 595 */ 596 if (rsnd_is_gen1(priv)) 597 return 0; 598 599 /* 600 * for Gen2 601 */ 602 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp"); 603 dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL); 604 if (!dmac || !res) { 605 dev_err(dev, "dma allocate failed\n"); 606 return 0; /* it will be PIO mode */ 607 } 608 609 dmac->dmapp_num = 0; 610 dmac->base = devm_ioremap_resource(dev, res); 611 if (IS_ERR(dmac->base)) 612 return PTR_ERR(dmac->base); 613 614 priv->dma = dmac; 615 616 return 0; 617} 618