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 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 dev_err(dev, "can't get dma channel\n"); 160 goto rsnd_dma_channel_err; 161 } 162 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; 168 169 dev_dbg(dev, "dma : %pad -> %pad\n", 170 &cfg.src_addr, &cfg.dst_addr); 171 172 ret = dmaengine_slave_config(dmaen->chan, &cfg); 173 if (ret < 0) 174 goto rsnd_dma_init_err; 175 176 return 0; 177 178 rsnd_dma_init_err: 179 rsnd_dma_quit(dma); 180 rsnd_dma_channel_err: 181 182 /* 183 * DMA failed. try to PIO mode 184 * see 185 * rsnd_ssi_fallback() 186 * rsnd_rdai_continuance_probe() 187 */ 188 return -EAGAIN; 189 } 190 191 static void rsnd_dmaen_quit(struct rsnd_dma *dma) 192 { 193 struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma); 194 195 if (dmaen->chan) 196 dma_release_channel(dmaen->chan); 197 198 dmaen->chan = NULL; 199 } 200 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, 206 }; 207 208 /* 209 * Audio DMAC peri peri 210 */ 211 static const u8 gen2_id_table_ssiu[] = { 212 0x00, /* SSI00 */ 213 0x04, /* SSI10 */ 214 0x08, /* SSI20 */ 215 0x0c, /* SSI3 */ 216 0x0d, /* SSI4 */ 217 0x0e, /* SSI5 */ 218 0x0f, /* SSI6 */ 219 0x10, /* SSI7 */ 220 0x11, /* SSI8 */ 221 0x12, /* SSI90 */ 222 }; 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 */ 234 }; 235 static const u8 gen2_id_table_cmd[] = { 236 0x37, /* SCU_CMD0 */ 237 0x38, /* SCU_CMD1 */ 238 }; 239 240 static u32 rsnd_dmapp_get_id(struct rsnd_mod *mod) 241 { 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); 248 int size = 0; 249 250 if (mod == ssi) { 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); 259 } 260 261 if (!entry) 262 return 0xFF; 263 264 if (size <= id) 265 return 0xFF; 266 267 return entry[id]; 268 } 269 270 static u32 rsnd_dmapp_get_chcr(struct rsnd_mod *mod_from, 271 struct rsnd_mod *mod_to) 272 { 273 return (rsnd_dmapp_get_id(mod_from) << 24) + 274 (rsnd_dmapp_get_id(mod_to) << 16); 275 } 276 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) 281 { 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); 286 287 dev_dbg(dev, "w %p : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data); 288 289 iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg)); 290 } 291 292 static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg) 293 { 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); 297 298 return ioread32(rsnd_dmapp_addr(dmac, dma, reg)); 299 } 300 301 static void rsnd_dmapp_stop(struct rsnd_dma *dma) 302 { 303 int i; 304 305 rsnd_dmapp_write(dma, 0, PDMACHCR); 306 307 for (i = 0; i < 1024; i++) { 308 if (0 == rsnd_dmapp_read(dma, PDMACHCR)) 309 return; 310 udelay(1); 311 } 312 } 313 314 static void rsnd_dmapp_start(struct rsnd_dma *dma) 315 { 316 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma); 317 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); 321 } 322 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) 325 { 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); 329 330 dmapp->dmapp_id = dmac->dmapp_num; 331 dmapp->chcr = rsnd_dmapp_get_chcr(mod_from, mod_to) | PDMACHCR_DE; 332 333 dmac->dmapp_num++; 334 335 rsnd_dmapp_stop(dma); 336 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); 339 340 return 0; 341 } 342 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, 348 }; 349 350 /* 351 * Common DMAC Interface 352 */ 353 354 /* 355 * DMA read/write register offset 356 * 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 361 * 362 * ex) R-Car H2 case 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 368 */ 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) 371 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)) 374 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)) 377 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)) 380 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)) 383 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)) 386 387 static dma_addr_t 388 rsnd_gen2_dma_addr(struct rsnd_priv *priv, 389 struct rsnd_mod *mod, 390 int is_play, int is_from) 391 { 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); 400 struct dma_addr { 401 dma_addr_t out_addr; 402 dma_addr_t in_addr; 403 } dma_addrs[3][2][3] = { 404 /* SRC */ 405 {{{ 0, 0 }, 406 /* Capture */ 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) } }, 409 /* Playback */ 410 {{ 0, 0, }, 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) } } 413 }, 414 /* SSI */ 415 /* Capture */ 416 {{{ RDMA_SSI_O_N(ssi, id), 0 }, 417 { RDMA_SSIU_O_P(ssi, id), 0 }, 418 { RDMA_SSIU_O_P(ssi, id), 0 } }, 419 /* Playback */ 420 {{ 0, RDMA_SSI_I_N(ssi, id) }, 421 { 0, RDMA_SSIU_I_P(ssi, id) }, 422 { 0, RDMA_SSIU_I_P(ssi, id) } } 423 }, 424 /* SSIU */ 425 /* Capture */ 426 {{{ RDMA_SSIU_O_N(ssi, id), 0 }, 427 { RDMA_SSIU_O_P(ssi, id), 0 }, 428 { RDMA_SSIU_O_P(ssi, id), 0 } }, 429 /* Playback */ 430 {{ 0, RDMA_SSIU_I_N(ssi, id) }, 431 { 0, RDMA_SSIU_I_P(ssi, id) }, 432 { 0, RDMA_SSIU_I_P(ssi, id) } } }, 433 }; 434 435 /* it shouldn't happen */ 436 if (use_dvc && !use_src) 437 dev_err(dev, "DVC is selected without SRC\n"); 438 439 /* use SSIU or SSI ? */ 440 if (is_ssi && rsnd_ssi_use_busif(mod)) 441 is_ssi++; 442 443 return (is_from) ? 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; 446 } 447 448 static dma_addr_t rsnd_dma_addr(struct rsnd_priv *priv, 449 struct rsnd_mod *mod, 450 int is_play, int is_from) 451 { 452 /* 453 * gen1 uses default DMA addr 454 */ 455 if (rsnd_is_gen1(priv)) 456 return 0; 457 458 if (!mod) 459 return 0; 460 461 return rsnd_gen2_dma_addr(priv, mod, is_play, is_from); 462 } 463 464 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */ 465 static void rsnd_dma_of_path(struct rsnd_dma *dma, 466 int is_play, 467 struct rsnd_mod **mod_from, 468 struct rsnd_mod **mod_to) 469 { 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]; 476 int i, index; 477 478 479 for (i = 0; i < MOD_MAX; i++) 480 mod[i] = NULL; 481 482 /* 483 * in play case... 484 * 485 * src -> dst 486 * 487 * mem -> SSI 488 * mem -> SRC -> SSI 489 * mem -> SRC -> DVC -> SSI 490 */ 491 mod[0] = NULL; /* for "mem" */ 492 index = 1; 493 for (i = 1; i < MOD_MAX; i++) { 494 if (!src) { 495 mod[i] = ssi; 496 } else if (!dvc) { 497 mod[i] = src; 498 src = NULL; 499 } else { 500 if ((!is_play) && (this == src)) 501 this = dvc; 502 503 mod[i] = (is_play) ? src : dvc; 504 i++; 505 mod[i] = (is_play) ? dvc : src; 506 src = NULL; 507 dvc = NULL; 508 } 509 510 if (mod[i] == this) 511 index = i; 512 513 if (mod[i] == ssi) 514 break; 515 } 516 517 if (is_play) { 518 *mod_from = mod[index - 1]; 519 *mod_to = mod[index]; 520 } else { 521 *mod_from = mod[index]; 522 *mod_to = mod[index - 1]; 523 } 524 } 525 526 void rsnd_dma_stop(struct rsnd_dma *dma) 527 { 528 dma->ops->stop(dma); 529 } 530 531 void rsnd_dma_start(struct rsnd_dma *dma) 532 { 533 dma->ops->start(dma); 534 } 535 536 void rsnd_dma_quit(struct rsnd_dma *dma) 537 { 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); 541 542 if (!dmac) 543 return; 544 545 dma->ops->quit(dma); 546 } 547 548 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, int id) 549 { 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); 556 557 /* 558 * DMA failed. try to PIO mode 559 * see 560 * rsnd_ssi_fallback() 561 * rsnd_rdai_continuance_probe() 562 */ 563 if (!dmac) 564 return -EAGAIN; 565 566 rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to); 567 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); 570 571 /* for Gen2 */ 572 if (mod_from && mod_to) 573 dma->ops = &rsnd_dmapp_ops; 574 else 575 dma->ops = &rsnd_dmaen_ops; 576 577 /* for Gen1, overwrite */ 578 if (rsnd_is_gen1(priv)) 579 dma->ops = &rsnd_dmaen_ops; 580 581 return dma->ops->init(priv, dma, id, mod_from, mod_to); 582 } 583 584 int rsnd_dma_probe(struct platform_device *pdev, 585 const struct rsnd_of_data *of_data, 586 struct rsnd_priv *priv) 587 { 588 struct device *dev = rsnd_priv_to_dev(priv); 589 struct rsnd_dma_ctrl *dmac; 590 struct resource *res; 591 592 /* 593 * for Gen1 594 */ 595 if (rsnd_is_gen1(priv)) 596 return 0; 597 598 /* 599 * for Gen2 600 */ 601 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp"); 602 dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL); 603 if (!dmac || !res) { 604 dev_err(dev, "dma allocate failed\n"); 605 return 0; /* it will be PIO mode */ 606 } 607 608 dmac->dmapp_num = 0; 609 dmac->base = devm_ioremap_resource(dev, res); 610 if (IS_ERR(dmac->base)) 611 return PTR_ERR(dmac->base); 612 613 priv->dma = dmac; 614 615 return 0; 616 } 617