// SPDX-License-Identifier: GPL-2.0-only
/*
* Audio support for PS3
* Copyright (C) 2007 Sony Computer Entertainment Inc.
* All rights reserved.
* Copyright 2006, 2007 Sony Corporation
*/
#include <linux/dma-mapping.h>
#include <linux/dmapool.h>
#include <linux/gfp.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/module.h>
#include <sound/asound.h>
#include <sound/control.h>
#include <sound/core.h>
#include <sound/initval.h>
#include <sound/memalloc.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <asm/dma.h>
#include <asm/firmware.h>
#include <asm/lv1call.h>
#include <asm/ps3.h>
#include <asm/ps3av.h>
#include "snd_ps3.h"
#include "snd_ps3_reg.h"
/*
* global
*/
static struct snd_ps3_card_info the_card;
static int snd_ps3_start_delay = CONFIG_SND_PS3_DEFAULT_START_DELAY;
module_param_named(start_delay, snd_ps3_start_delay, uint, 0644);
MODULE_PARM_DESC(start_delay, "time to insert silent data in ms");
static int index = SNDRV_DEFAULT_IDX1;
static char *id = SNDRV_DEFAULT_STR1;
module_param(index, int, 0444);
MODULE_PARM_DESC(index, "Index value for PS3 soundchip.");
module_param(id, charp, 0444);
MODULE_PARM_DESC(id, "ID string for PS3 soundchip.");
/*
* PS3 audio register access
*/
static inline u32 read_reg(unsigned int reg)
{
return in_be32(the_card.mapped_mmio_vaddr + reg);
}
static inline void write_reg(unsigned int reg, u32 val)
{
out_be32(the_card.mapped_mmio_vaddr + reg, val);
}
static inline void update_reg(unsigned int reg, u32 or_val)
{
u32 newval = read_reg(reg) | or_val;
write_reg(reg, newval);
}
static inline void update_mask_reg(unsigned int reg, u32 mask, u32 or_val)
{
u32 newval = (read_reg(reg) & mask) | or_val;
write_reg(reg, newval);
}
/*
* ALSA defs
*/
static const struct snd_pcm_hardware snd_ps3_pcm_hw = {
.info = (SNDRV_PCM_INFO_MMAP |
SNDRV_PCM_INFO_NONINTERLEAVED |
SNDRV_PCM_INFO_MMAP_VALID),
.formats = (SNDRV_PCM_FMTBIT_S16_BE |
SNDRV_PCM_FMTBIT_S24_BE),
.rates = (SNDRV_PCM_RATE_44100 |
SNDRV_PCM_RATE_48000 |
SNDRV_PCM_RATE_88200 |
SNDRV_PCM_RATE_96000),
.rate_min = 44100,
.rate_max = 96000,
.channels_min = 2, /* stereo only */
.channels_max = 2,
.buffer_bytes_max = PS3_AUDIO_FIFO_SIZE * 64,
/* interrupt by four stages */
.period_bytes_min = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
.period_bytes_max = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
.periods_min = 16,
.periods_max = 32, /* buffer_size_max/ period_bytes_max */
.fifo_size = PS3_AUDIO_FIFO_SIZE
};
static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info *card,
int count, int force_stop)
{
int dma_ch, done, retries, stop_forced = 0;
uint32_t status;
for (dma_ch = 0; dma_ch < 8; dma_ch++) {
retries = count;
do {
status = read_reg(PS3_AUDIO_KICK(dma_ch)) &
PS3_AUDIO_KICK_STATUS_MASK;
switch (status) {
case PS3_AUDIO_KICK_STATUS_DONE:
case PS3_AUDIO_KICK_STATUS_NOTIFY:
case PS3_AUDIO_KICK_STATUS_CLEAR:
case PS3_AUDIO_KICK_STATUS_ERROR:
done = 1;
break;
default:
done = 0;
udelay(10);
}
} while (!done && --retries);
if (!retries && force_stop) {
pr_info("%s: DMA ch %d is not stopped.",
__func__, dma_ch);
/* last resort. force to stop dma.
* NOTE: this cause DMA done interrupts
*/
update_reg(PS3_AUDIO_CONFIG, PS3_AUDIO_CONFIG_CLEAR);
stop_forced = 1;
}
}
return stop_forced;
}
/*
* wait for all dma is done.
* NOTE: caller should reset card->running before call.
* If not, the interrupt handler will re-start DMA,
* then DMA is never stopped.
*/
static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info *card)
{
int stop_forced;
/*
* wait for the last dma is done
*/
/*
* expected maximum DMA done time is 5.7ms + something (DMA itself).
* 5.7ms is from 16bit/sample 2ch 44.1Khz; the time next
* DMA kick event would occur.
*/
stop_forced = snd_ps3_verify_dma_stop(card, 700, 1);
/*
* clear outstanding interrupts.
*/
update_reg(PS3_AUDIO_INTR_0, 0);
update_reg(PS3_AUDIO_AX_IS, 0);
/*
*revert CLEAR bit since it will not reset automatically after DMA stop
*/
if (stop_forced)
update_mask_reg(PS3_AUDIO_CONFIG, ~PS3_AUDIO_CONFIG_CLEAR, 0);
/* ensure the hardware sees changes */
wmb();
}
static void snd_ps3_kick_dma(struct snd_ps3_card_info *card)
{
update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST);
/* ensure the hardware sees the change */
wmb();
}
/*
* convert virtual addr to ioif bus addr.
*/
stat