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 25 struct 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 */ 35 static 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 56 static 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 63 static 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 97 struct 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 118 static 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 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) 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 dev_dbg(dev, "Audio DMAC init\n"); 148 149 if (dev->of_node) { 150 dmaen->chan = rsnd_dmaen_request_channel(mod_from, mod_to); 151 } else { 152 dma_cap_mask_t mask; 153 154 dma_cap_zero(mask); 155 dma_cap_set(DMA_SLAVE, mask); 156 157 dmaen->chan = dma_request_channel(mask, shdma_chan_filter, 158 (void *)id); 159 } 160 if (IS_ERR_OR_NULL(dmaen->chan)) { 161 dev_err(dev, "can't get dma channel\n"); 162 goto rsnd_dma_channel_err; 163 } 164 165 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM; 166 cfg.src_addr = dma->src_addr; 167 cfg.dst_addr = dma->dst_addr; 168 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 169 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 170 171 dev_dbg(dev, "dma : %pad -> %pad\n", 172 &cfg.src_addr, &cfg.dst_addr); 173 174 ret = dmaengine_slave_config(dmaen->chan, &cfg); 175 if (ret < 0) 176 goto rsnd_dma_init_err; 177 178 return 0; 179 180 rsnd_dma_init_err: 181 rsnd_dma_quit(dma); 182 rsnd_dma_channel_err: 183 184 /* 185 * DMA failed. try to PIO mode 186 * see 187 * rsnd_ssi_fallback() 188 * rsnd_rdai_continuance_probe() 189 */ 190 return -EAGAIN; 191 } 192 193 static void rsnd_dmaen_quit(struct rsnd_dma *dma) 194 { 195 struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma); 196 197 if (dmaen->chan) 198 dma_release_channel(dmaen->chan); 199 200 dmaen->chan = NULL; 201 } 202 203 static struct rsnd_dma_ops rsnd_dmaen_ops = { 204 .start = rsnd_dmaen_start, 205 .stop = rsnd_dmaen_stop, 206 .init = rsnd_dmaen_init, 207 .quit = rsnd_dmaen_quit, 208 }; 209 210 /* 211 * Audio DMAC peri peri 212 */ 213 static const u8 gen2_id_table_ssiu[] = { 214 0x00, /* SSI00 */ 215 0x04, /* SSI10 */ 216 0x08, /* SSI20 */ 217 0x0c, /* SSI3 */ 218 0x0d, /* SSI4 */ 219 0x0e, /* SSI5 */ 220 0x0f, /* SSI6 */ 221 0x10, /* SSI7 */ 222 0x11, /* SSI8 */ 223 0x12, /* SSI90 */ 224 }; 225 static const u8 gen2_id_table_scu[] = { 226 0x2d, /* SCU_SRCI0 */ 227 0x2e, /* SCU_SRCI1 */ 228 0x2f, /* SCU_SRCI2 */ 229 0x30, /* SCU_SRCI3 */ 230 0x31, /* SCU_SRCI4 */ 231 0x32, /* SCU_SRCI5 */ 232 0x33, /* SCU_SRCI6 */ 233 0x34, /* SCU_SRCI7 */ 234 0x35, /* SCU_SRCI8 */ 235 0x36, /* SCU_SRCI9 */ 236 }; 237 static const u8 gen2_id_table_cmd[] = { 238 0x37, /* SCU_CMD0 */ 239 0x38, /* SCU_CMD1 */ 240 }; 241 242 static u32 rsnd_dmapp_get_id(struct rsnd_mod *mod) 243 { 244 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); 245 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io); 246 struct rsnd_mod *src = rsnd_io_to_mod_src(io); 247 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io); 248 const u8 *entry = NULL; 249 int id = rsnd_mod_id(mod); 250 int size = 0; 251 252 if (mod == ssi) { 253 entry = gen2_id_table_ssiu; 254 size = ARRAY_SIZE(gen2_id_table_ssiu); 255 } else if (mod == src) { 256 entry = gen2_id_table_scu; 257 size = ARRAY_SIZE(gen2_id_table_scu); 258 } else if (mod == dvc) { 259 entry = gen2_id_table_cmd; 260 size = ARRAY_SIZE(gen2_id_table_cmd); 261 } 262 263 if (!entry) 264 return 0xFF; 265 266 if (size <= id) 267 return 0xFF; 268 269 return entry[id]; 270 } 271 272 static u32 rsnd_dmapp_get_chcr(struct rsnd_mod *mod_from, 273 struct rsnd_mod *mod_to) 274 { 275 return (rsnd_dmapp_get_id(mod_from) << 24) + 276 (rsnd_dmapp_get_id(mod_to) << 16); 277 } 278 279 #define rsnd_dmapp_addr(dmac, dma, reg) \ 280 (dmac->base + 0x20 + reg + \ 281 (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id)) 282 static void rsnd_dmapp_write(struct rsnd_dma *dma, u32 data, u32 reg) 283 { 284 struct rsnd_mod *mod = rsnd_dma_to_mod(dma); 285 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 286 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv); 287 struct device *dev = rsnd_priv_to_dev(priv); 288 289 dev_dbg(dev, "w %p : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data); 290 291 iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg)); 292 } 293 294 static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg) 295 { 296 struct rsnd_mod *mod = rsnd_dma_to_mod(dma); 297 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 298 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv); 299 300 return ioread32(rsnd_dmapp_addr(dmac, dma, reg)); 301 } 302 303 static void rsnd_dmapp_stop(struct rsnd_dma *dma) 304 { 305 int i; 306 307 rsnd_dmapp_write(dma, 0, PDMACHCR); 308 309 for (i = 0; i < 1024; i++) { 310 if (0 == rsnd_dmapp_read(dma, PDMACHCR)) 311 return; 312 udelay(1); 313 } 314 } 315 316 static void rsnd_dmapp_start(struct rsnd_dma *dma) 317 { 318 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma); 319 320 rsnd_dmapp_write(dma, dma->src_addr, PDMASAR); 321 rsnd_dmapp_write(dma, dma->dst_addr, PDMADAR); 322 rsnd_dmapp_write(dma, dmapp->chcr, PDMACHCR); 323 } 324 325 static int rsnd_dmapp_init(struct rsnd_priv *priv, struct rsnd_dma *dma, int id, 326 struct rsnd_mod *mod_from, struct rsnd_mod *mod_to) 327 { 328 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma); 329 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv); 330 struct device *dev = rsnd_priv_to_dev(priv); 331 332 dev_dbg(dev, "Audio DMAC peri peri init\n"); 333 334 dmapp->dmapp_id = dmac->dmapp_num; 335 dmapp->chcr = rsnd_dmapp_get_chcr(mod_from, mod_to) | PDMACHCR_DE; 336 337 dmac->dmapp_num++; 338 339 rsnd_dmapp_stop(dma); 340 341 dev_dbg(dev, "id/src/dst/chcr = %d/%pad/%pad/%08x\n", 342 dmapp->dmapp_id, &dma->src_addr, &dma->dst_addr, dmapp->chcr); 343 344 return 0; 345 } 346 347 static struct rsnd_dma_ops rsnd_dmapp_ops = { 348 .start = rsnd_dmapp_start, 349 .stop = rsnd_dmapp_stop, 350 .init = rsnd_dmapp_init, 351 .quit = rsnd_dmapp_stop, 352 }; 353 354 /* 355 * Common DMAC Interface 356 */ 357 358 /* 359 * DMA read/write register offset 360 * 361 * RSND_xxx_I_N for Audio DMAC input 362 * RSND_xxx_O_N for Audio DMAC output 363 * RSND_xxx_I_P for Audio DMAC peri peri input 364 * RSND_xxx_O_P for Audio DMAC peri peri output 365 * 366 * ex) R-Car H2 case 367 * mod / DMAC in / DMAC out / DMAC PP in / DMAC pp out 368 * SSI : 0xec541000 / 0xec241008 / 0xec24100c 369 * SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000 370 * SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000 371 * CMD : 0xec500000 / / 0xec008000 0xec308000 372 */ 373 #define RDMA_SSI_I_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0x8) 374 #define RDMA_SSI_O_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0xc) 375 376 #define RDMA_SSIU_I_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i)) 377 #define RDMA_SSIU_O_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i)) 378 379 #define RDMA_SSIU_I_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i)) 380 #define RDMA_SSIU_O_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i)) 381 382 #define RDMA_SRC_I_N(addr, i) (addr ##_reg - 0x00500000 + (0x400 * i)) 383 #define RDMA_SRC_O_N(addr, i) (addr ##_reg - 0x004fc000 + (0x400 * i)) 384 385 #define RDMA_SRC_I_P(addr, i) (addr ##_reg - 0x00200000 + (0x400 * i)) 386 #define RDMA_SRC_O_P(addr, i) (addr ##_reg - 0x001fc000 + (0x400 * i)) 387 388 #define RDMA_CMD_O_N(addr, i) (addr ##_reg - 0x004f8000 + (0x400 * i)) 389 #define RDMA_CMD_O_P(addr, i) (addr ##_reg - 0x001f8000 + (0x400 * i)) 390 391 static dma_addr_t 392 rsnd_gen2_dma_addr(struct rsnd_priv *priv, 393 struct rsnd_mod *mod, 394 int is_play, int is_from) 395 { 396 struct device *dev = rsnd_priv_to_dev(priv); 397 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); 398 phys_addr_t ssi_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SSI); 399 phys_addr_t src_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SCU); 400 int is_ssi = !!(rsnd_io_to_mod_ssi(io) == mod); 401 int use_src = !!rsnd_io_to_mod_src(io); 402 int use_dvc = !!rsnd_io_to_mod_dvc(io); 403 int id = rsnd_mod_id(mod); 404 struct dma_addr { 405 dma_addr_t out_addr; 406 dma_addr_t in_addr; 407 } dma_addrs[3][2][3] = { 408 /* SRC */ 409 {{{ 0, 0 }, 410 /* Capture */ 411 { RDMA_SRC_O_N(src, id), RDMA_SRC_I_P(src, id) }, 412 { RDMA_CMD_O_N(src, id), RDMA_SRC_I_P(src, id) } }, 413 /* Playback */ 414 {{ 0, 0, }, 415 { RDMA_SRC_O_P(src, id), RDMA_SRC_I_N(src, id) }, 416 { RDMA_CMD_O_P(src, id), RDMA_SRC_I_N(src, id) } } 417 }, 418 /* SSI */ 419 /* Capture */ 420 {{{ RDMA_SSI_O_N(ssi, id), 0 }, 421 { RDMA_SSIU_O_P(ssi, id), 0 }, 422 { RDMA_SSIU_O_P(ssi, id), 0 } }, 423 /* Playback */ 424 {{ 0, RDMA_SSI_I_N(ssi, id) }, 425 { 0, RDMA_SSIU_I_P(ssi, id) }, 426 { 0, RDMA_SSIU_I_P(ssi, id) } } 427 }, 428 /* SSIU */ 429 /* Capture */ 430 {{{ RDMA_SSIU_O_N(ssi, id), 0 }, 431 { RDMA_SSIU_O_P(ssi, id), 0 }, 432 { RDMA_SSIU_O_P(ssi, id), 0 } }, 433 /* Playback */ 434 {{ 0, RDMA_SSIU_I_N(ssi, id) }, 435 { 0, RDMA_SSIU_I_P(ssi, id) }, 436 { 0, RDMA_SSIU_I_P(ssi, id) } } }, 437 }; 438 439 /* it shouldn't happen */ 440 if (use_dvc && !use_src) 441 dev_err(dev, "DVC is selected without SRC\n"); 442 443 /* use SSIU or SSI ? */ 444 if (is_ssi && rsnd_ssi_use_busif(mod)) 445 is_ssi++; 446 447 return (is_from) ? 448 dma_addrs[is_ssi][is_play][use_src + use_dvc].out_addr : 449 dma_addrs[is_ssi][is_play][use_src + use_dvc].in_addr; 450 } 451 452 static dma_addr_t rsnd_dma_addr(struct rsnd_priv *priv, 453 struct rsnd_mod *mod, 454 int is_play, int is_from) 455 { 456 /* 457 * gen1 uses default DMA addr 458 */ 459 if (rsnd_is_gen1(priv)) 460 return 0; 461 462 if (!mod) 463 return 0; 464 465 return rsnd_gen2_dma_addr(priv, mod, is_play, is_from); 466 } 467 468 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */ 469 static void rsnd_dma_of_path(struct rsnd_dma *dma, 470 int is_play, 471 struct rsnd_mod **mod_from, 472 struct rsnd_mod **mod_to) 473 { 474 struct rsnd_mod *this = rsnd_dma_to_mod(dma); 475 struct rsnd_dai_stream *io = rsnd_mod_to_io(this); 476 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io); 477 struct rsnd_mod *src = rsnd_io_to_mod_src(io); 478 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io); 479 struct rsnd_mod *mod[MOD_MAX]; 480 int i, index; 481 482 483 for (i = 0; i < MOD_MAX; i++) 484 mod[i] = NULL; 485 486 /* 487 * in play case... 488 * 489 * src -> dst 490 * 491 * mem -> SSI 492 * mem -> SRC -> SSI 493 * mem -> SRC -> DVC -> SSI 494 */ 495 mod[0] = NULL; /* for "mem" */ 496 index = 1; 497 for (i = 1; i < MOD_MAX; i++) { 498 if (!src) { 499 mod[i] = ssi; 500 } else if (!dvc) { 501 mod[i] = src; 502 src = NULL; 503 } else { 504 if ((!is_play) && (this == src)) 505 this = dvc; 506 507 mod[i] = (is_play) ? src : dvc; 508 i++; 509 mod[i] = (is_play) ? dvc : src; 510 src = NULL; 511 dvc = NULL; 512 } 513 514 if (mod[i] == this) 515 index = i; 516 517 if (mod[i] == ssi) 518 break; 519 } 520 521 if (is_play) { 522 *mod_from = mod[index - 1]; 523 *mod_to = mod[index]; 524 } else { 525 *mod_from = mod[index]; 526 *mod_to = mod[index - 1]; 527 } 528 } 529 530 void rsnd_dma_stop(struct rsnd_dma *dma) 531 { 532 dma->ops->stop(dma); 533 } 534 535 void rsnd_dma_start(struct rsnd_dma *dma) 536 { 537 dma->ops->start(dma); 538 } 539 540 void rsnd_dma_quit(struct rsnd_dma *dma) 541 { 542 dma->ops->quit(dma); 543 } 544 545 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, int id) 546 { 547 struct rsnd_mod *mod = rsnd_dma_to_mod(dma); 548 struct rsnd_mod *mod_from; 549 struct rsnd_mod *mod_to; 550 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); 551 int is_play = rsnd_io_is_play(io); 552 553 rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to); 554 555 dma->src_addr = rsnd_dma_addr(priv, mod_from, is_play, 1); 556 dma->dst_addr = rsnd_dma_addr(priv, mod_to, is_play, 0); 557 558 /* for Gen2 */ 559 if (mod_from && mod_to) 560 dma->ops = &rsnd_dmapp_ops; 561 else 562 dma->ops = &rsnd_dmaen_ops; 563 564 /* for Gen1, overwrite */ 565 if (rsnd_is_gen1(priv)) 566 dma->ops = &rsnd_dmaen_ops; 567 568 return dma->ops->init(priv, dma, id, mod_from, mod_to); 569 } 570 571 int rsnd_dma_probe(struct platform_device *pdev, 572 const struct rsnd_of_data *of_data, 573 struct rsnd_priv *priv) 574 { 575 struct device *dev = rsnd_priv_to_dev(priv); 576 struct rsnd_dma_ctrl *dmac; 577 struct resource *res; 578 579 /* 580 * for Gen1 581 */ 582 if (rsnd_is_gen1(priv)) 583 return 0; 584 585 /* 586 * for Gen2 587 */ 588 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp"); 589 dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL); 590 if (!dmac || !res) { 591 dev_err(dev, "dma allocate failed\n"); 592 return -ENOMEM; 593 } 594 595 dmac->dmapp_num = 0; 596 dmac->base = devm_ioremap_resource(dev, res); 597 if (IS_ERR(dmac->base)) 598 return PTR_ERR(dmac->base); 599 600 priv->dma = dmac; 601 602 return 0; 603 } 604