ALSA: ASoC: fix SNDCTL_DSP_SYNC support in Freescale 8610 sound drivers

If an OSS application calls SNDCTL_DSP_SYNC, then ALSA will call the driver's
_hw_params and _prepare functions again. On the Freescale MPC8610 DMA ASoC
driver, this caused the DMA controller to be unneccessarily re-programmed, and
apparently it doesn't like that. The DMA will then not operate when
instructed. This patch relocates much of the DMA programming to
fsl_dma_open(), which is called only once.

Signed-off-by: Timur Tabi <timur@freescale.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>

authored by Timur Tabi and committed by Takashi Iwai bf9c8c9d 11589418

+131 -118
+131 -118
sound/soc/fsl/fsl_dma.c
··· 327 * fsl_dma_open: open a new substream. 328 * 329 * Each substream has its own DMA buffer. 330 */ 331 static int fsl_dma_open(struct snd_pcm_substream *substream) 332 { 333 struct snd_pcm_runtime *runtime = substream->runtime; 334 struct fsl_dma_private *dma_private; 335 dma_addr_t ld_buf_phys; 336 unsigned int channel; 337 int ret = 0; 338 339 /* 340 * Reject any DMA buffer whose size is not a multiple of the period ··· 456 snd_soc_set_runtime_hwparams(substream, &fsl_dma_hardware); 457 runtime->private_data = dma_private; 458 459 - return 0; 460 - } 461 462 - /** 463 - * fsl_dma_hw_params: allocate the DMA buffer and the DMA link descriptors. 464 - * 465 - * ALSA divides the DMA buffer into N periods. We create NUM_DMA_LINKS link 466 - * descriptors that ping-pong from one period to the next. For example, if 467 - * there are six periods and two link descriptors, this is how they look 468 - * before playback starts: 469 - * 470 - * The last link descriptor 471 - * ____________ points back to the first 472 - * | | 473 - * V | 474 - * ___ ___ | 475 - * | |->| |->| 476 - * |___| |___| 477 - * | | 478 - * | | 479 - * V V 480 - * _________________________________________ 481 - * | | | | | | | The DMA buffer is 482 - * | | | | | | | divided into 6 parts 483 - * |______|______|______|______|______|______| 484 - * 485 - * and here's how they look after the first period is finished playing: 486 - * 487 - * ____________ 488 - * | | 489 - * V | 490 - * ___ ___ | 491 - * | |->| |->| 492 - * |___| |___| 493 - * | | 494 - * |______________ 495 - * | | 496 - * V V 497 - * _________________________________________ 498 - * | | | | | | | 499 - * | | | | | | | 500 - * |______|______|______|______|______|______| 501 - * 502 - * The first link descriptor now points to the third period. The DMA 503 - * controller is currently playing the second period. When it finishes, it 504 - * will jump back to the first descriptor and play the third period. 505 - * 506 - * There are four reasons we do this: 507 - * 508 - * 1. The only way to get the DMA controller to automatically restart the 509 - * transfer when it gets to the end of the buffer is to use chaining 510 - * mode. Basic direct mode doesn't offer that feature. 511 - * 2. We need to receive an interrupt at the end of every period. The DMA 512 - * controller can generate an interrupt at the end of every link transfer 513 - * (aka segment). Making each period into a DMA segment will give us the 514 - * interrupts we need. 515 - * 3. By creating only two link descriptors, regardless of the number of 516 - * periods, we do not need to reallocate the link descriptors if the 517 - * number of periods changes. 518 - * 4. All of the audio data is still stored in a single, contiguous DMA 519 - * buffer, which is what ALSA expects. We're just dividing it into 520 - * contiguous parts, and creating a link descriptor for each one. 521 - * 522 - * Note that due to a quirk of the SSI's STX register, the target address 523 - * for the DMA operations depends on the sample size. So we don't program 524 - * the dest_addr (for playback -- source_addr for capture) fields in the 525 - * link descriptors here. We do that in fsl_dma_prepare() 526 - */ 527 - static int fsl_dma_hw_params(struct snd_pcm_substream *substream, 528 - struct snd_pcm_hw_params *hw_params) 529 - { 530 - struct snd_pcm_runtime *runtime = substream->runtime; 531 - struct fsl_dma_private *dma_private = runtime->private_data; 532 - struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel; 533 534 - dma_addr_t temp_addr; /* Pointer to next period */ 535 - u64 temp_link; /* Pointer to next link descriptor */ 536 - u32 mr; /* Temporary variable for MR register */ 537 - 538 - unsigned int i; 539 - 540 - /* Get all the parameters we need */ 541 - size_t buffer_size = params_buffer_bytes(hw_params); 542 - size_t period_size = params_period_bytes(hw_params); 543 - 544 - /* Initialize our DMA tracking variables */ 545 - dma_private->period_size = period_size; 546 - dma_private->num_periods = params_periods(hw_params); 547 - dma_private->dma_buf_end = dma_private->dma_buf_phys + buffer_size; 548 - dma_private->dma_buf_next = dma_private->dma_buf_phys + 549 - (NUM_DMA_LINKS * period_size); 550 - if (dma_private->dma_buf_next >= dma_private->dma_buf_end) 551 - dma_private->dma_buf_next = dma_private->dma_buf_phys; 552 - 553 - /* 554 - * Initialize each link descriptor. 555 - * 556 - * The actual address in STX0 (destination for playback, source for 557 - * capture) is based on the sample size, but we don't know the sample 558 - * size in this function, so we'll have to adjust that later. See 559 - * comments in fsl_dma_prepare(). 560 - * 561 - * The DMA controller does not have a cache, so the CPU does not 562 - * need to tell it to flush its cache. However, the DMA 563 - * controller does need to tell the CPU to flush its cache. 564 - * That's what the SNOOP bit does. 565 - * 566 - * Also, even though the DMA controller supports 36-bit addressing, for 567 - * simplicity we currently support only 32-bit addresses for the audio 568 - * buffer itself. 569 - */ 570 - temp_addr = substream->dma_buffer.addr; 571 temp_link = dma_private->ld_buf_phys + 572 sizeof(struct fsl_dma_link_descriptor); 573 574 for (i = 0; i < NUM_DMA_LINKS; i++) { 575 struct fsl_dma_link_descriptor *link = &dma_private->link[i]; 576 577 - link->count = cpu_to_be32(period_size); 578 link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP); 579 link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP); 580 link->next = cpu_to_be64(temp_link); 581 582 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 583 - link->source_addr = cpu_to_be32(temp_addr); 584 - else 585 - link->dest_addr = cpu_to_be32(temp_addr); 586 - 587 - temp_addr += period_size; 588 temp_link += sizeof(struct fsl_dma_link_descriptor); 589 } 590 /* The last link descriptor points to the first */ ··· 495 * We want External Master Start and External Master Pause enabled, 496 * because the SSI is controlling the DMA controller. We want the DMA 497 * controller to be set up in advance, and then we signal only the SSI 498 - * to start transfering. 499 * 500 * We want End-Of-Segment Interrupts enabled, because this will generate 501 * an interrupt at the end of each segment (each link descriptor ··· 515 CCSR_DMA_MR_DAHE : CCSR_DMA_MR_SAHE; 516 517 out_be32(&dma_channel->mr, mr); 518 519 return 0; 520 }
··· 327 * fsl_dma_open: open a new substream. 328 * 329 * Each substream has its own DMA buffer. 330 + * 331 + * ALSA divides the DMA buffer into N periods. We create NUM_DMA_LINKS link 332 + * descriptors that ping-pong from one period to the next. For example, if 333 + * there are six periods and two link descriptors, this is how they look 334 + * before playback starts: 335 + * 336 + * The last link descriptor 337 + * ____________ points back to the first 338 + * | | 339 + * V | 340 + * ___ ___ | 341 + * | |->| |->| 342 + * |___| |___| 343 + * | | 344 + * | | 345 + * V V 346 + * _________________________________________ 347 + * | | | | | | | The DMA buffer is 348 + * | | | | | | | divided into 6 parts 349 + * |______|______|______|______|______|______| 350 + * 351 + * and here's how they look after the first period is finished playing: 352 + * 353 + * ____________ 354 + * | | 355 + * V | 356 + * ___ ___ | 357 + * | |->| |->| 358 + * |___| |___| 359 + * | | 360 + * |______________ 361 + * | | 362 + * V V 363 + * _________________________________________ 364 + * | | | | | | | 365 + * | | | | | | | 366 + * |______|______|______|______|______|______| 367 + * 368 + * The first link descriptor now points to the third period. The DMA 369 + * controller is currently playing the second period. When it finishes, it 370 + * will jump back to the first descriptor and play the third period. 371 + * 372 + * There are four reasons we do this: 373 + * 374 + * 1. The only way to get the DMA controller to automatically restart the 375 + * transfer when it gets to the end of the buffer is to use chaining 376 + * mode. Basic direct mode doesn't offer that feature. 377 + * 2. We need to receive an interrupt at the end of every period. The DMA 378 + * controller can generate an interrupt at the end of every link transfer 379 + * (aka segment). Making each period into a DMA segment will give us the 380 + * interrupts we need. 381 + * 3. By creating only two link descriptors, regardless of the number of 382 + * periods, we do not need to reallocate the link descriptors if the 383 + * number of periods changes. 384 + * 4. All of the audio data is still stored in a single, contiguous DMA 385 + * buffer, which is what ALSA expects. We're just dividing it into 386 + * contiguous parts, and creating a link descriptor for each one. 387 */ 388 static int fsl_dma_open(struct snd_pcm_substream *substream) 389 { 390 struct snd_pcm_runtime *runtime = substream->runtime; 391 struct fsl_dma_private *dma_private; 392 + struct ccsr_dma_channel __iomem *dma_channel; 393 dma_addr_t ld_buf_phys; 394 + u64 temp_link; /* Pointer to next link descriptor */ 395 + u32 mr; 396 unsigned int channel; 397 int ret = 0; 398 + unsigned int i; 399 400 /* 401 * Reject any DMA buffer whose size is not a multiple of the period ··· 395 snd_soc_set_runtime_hwparams(substream, &fsl_dma_hardware); 396 runtime->private_data = dma_private; 397 398 + /* Program the fixed DMA controller parameters */ 399 400 + dma_channel = dma_private->dma_channel; 401 402 temp_link = dma_private->ld_buf_phys + 403 sizeof(struct fsl_dma_link_descriptor); 404 405 for (i = 0; i < NUM_DMA_LINKS; i++) { 406 struct fsl_dma_link_descriptor *link = &dma_private->link[i]; 407 408 link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP); 409 link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP); 410 link->next = cpu_to_be64(temp_link); 411 412 temp_link += sizeof(struct fsl_dma_link_descriptor); 413 } 414 /* The last link descriptor points to the first */ ··· 549 * We want External Master Start and External Master Pause enabled, 550 * because the SSI is controlling the DMA controller. We want the DMA 551 * controller to be set up in advance, and then we signal only the SSI 552 + * to start transferring. 553 * 554 * We want End-Of-Segment Interrupts enabled, because this will generate 555 * an interrupt at the end of each segment (each link descriptor ··· 569 CCSR_DMA_MR_DAHE : CCSR_DMA_MR_SAHE; 570 571 out_be32(&dma_channel->mr, mr); 572 + 573 + return 0; 574 + } 575 + 576 + /** 577 + * fsl_dma_hw_params: continue initializing the DMA links 578 + * 579 + * This function obtains hardware parameters about the opened stream and 580 + * programs the DMA controller accordingly. 581 + * 582 + * Note that due to a quirk of the SSI's STX register, the target address 583 + * for the DMA operations depends on the sample size. So we don't program 584 + * the dest_addr (for playback -- source_addr for capture) fields in the 585 + * link descriptors here. We do that in fsl_dma_prepare() 586 + */ 587 + static int fsl_dma_hw_params(struct snd_pcm_substream *substream, 588 + struct snd_pcm_hw_params *hw_params) 589 + { 590 + struct snd_pcm_runtime *runtime = substream->runtime; 591 + struct fsl_dma_private *dma_private = runtime->private_data; 592 + 593 + dma_addr_t temp_addr; /* Pointer to next period */ 594 + 595 + unsigned int i; 596 + 597 + /* Get all the parameters we need */ 598 + size_t buffer_size = params_buffer_bytes(hw_params); 599 + size_t period_size = params_period_bytes(hw_params); 600 + 601 + /* Initialize our DMA tracking variables */ 602 + dma_private->period_size = period_size; 603 + dma_private->num_periods = params_periods(hw_params); 604 + dma_private->dma_buf_end = dma_private->dma_buf_phys + buffer_size; 605 + dma_private->dma_buf_next = dma_private->dma_buf_phys + 606 + (NUM_DMA_LINKS * period_size); 607 + if (dma_private->dma_buf_next >= dma_private->dma_buf_end) 608 + dma_private->dma_buf_next = dma_private->dma_buf_phys; 609 + 610 + /* 611 + * The actual address in STX0 (destination for playback, source for 612 + * capture) is based on the sample size, but we don't know the sample 613 + * size in this function, so we'll have to adjust that later. See 614 + * comments in fsl_dma_prepare(). 615 + * 616 + * The DMA controller does not have a cache, so the CPU does not 617 + * need to tell it to flush its cache. However, the DMA 618 + * controller does need to tell the CPU to flush its cache. 619 + * That's what the SNOOP bit does. 620 + * 621 + * Also, even though the DMA controller supports 36-bit addressing, for 622 + * simplicity we currently support only 32-bit addresses for the audio 623 + * buffer itself. 624 + */ 625 + temp_addr = substream->dma_buffer.addr; 626 + 627 + for (i = 0; i < NUM_DMA_LINKS; i++) { 628 + struct fsl_dma_link_descriptor *link = &dma_private->link[i]; 629 + 630 + link->count = cpu_to_be32(period_size); 631 + 632 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 633 + link->source_addr = cpu_to_be32(temp_addr); 634 + else 635 + link->dest_addr = cpu_to_be32(temp_addr); 636 + 637 + temp_addr += period_size; 638 + } 639 640 return 0; 641 }