/* * Audio support for PS3 * Copyright (C) 2007 Sony Computer Entertainment Inc. * All rights reserved. * Copyright 2006, 2007 Sony Corporation * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License * as published by the Free Software Foundation; version 2 of the Licence. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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. */ static dma_addr_t v_to_bus(struct snd_ps3_card_info *card, void *paddr, int ch) { return card->dma_start_bus_addr[ch] + (paddr - card->dma_start_vaddr[ch]); }; /* * increment ring buffer pointer. * NOTE: caller must hold write spinlock */ static void snd_ps3_bump_buffer(struct snd_ps3_card_info *card, enum snd_ps3_ch ch, size_t byte_count, int stage) { if (!stage) card->dma_last_transfer_vaddr[ch] = card->dma_next_transfer_vaddr[ch]; card->dma_next_transfer_vaddr[ch] += byte_count; if ((card->dma_start_vaddr[ch] + (card->dma_buffer_size / 2)) <= card->dma_next_transfer_vaddr[ch]) { card->dma_next_transfer_vaddr[ch] = card->dma_start_vaddr[ch]; } } /* * setup dmac to send data to audio and attenuate samples on the ring buffer */ static int snd_ps3_program_dma(struct snd_ps3_card_info *card, enum snd_ps3_dma_filltype filltype) { /* this dmac does not support over 4G */ uint32_t dma_addr; int fill_stages, dma_ch, stage; enum snd_ps3_ch ch; uint32_t ch0_kick_event = 0; /* initialize to mute gcc */ void *start_vaddr; unsigned long irqsave; int silent = 0; switch (filltype) { case SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL: silent = 1; /* intentionally fall thru */ case SND_PS3_DMA_FILLTYPE_FIRSTFILL: ch0_kick_event = PS3_AUDIO_KICK_EVENT_ALWAYS; break; case SND_PS3_DMA_FILLTYPE_SILENT_RUNNING: silent = 1; /* intentionally fall thru */ case SND_PS3_DMA_FILLTYPE_RUNNING: ch0_kick_event = PS3_AUDIO_KICK_EVENT_SERIALOUT0_EMPTY; break; } snd_ps3_verify_dma_stop(card, 700, 0); fill_stages = 4; spin_lock_irqsave(&card->dma_lock, irqsave); for (ch = 0; ch < 2; ch++) { start_vaddr = card->dma_next_transfer_vaddr[0]; for (stage = 0; stage < fill_stages; stage++) { dma_ch = stage * 2 + ch; if (silent) dma_addr = card->null_buffer_start_dma_addr; else dma_addr = v_to_bus(card, card->dma_next_transfer_vaddr[ch], ch); write_reg(PS3_AUDIO_SOURCE(dma_ch), (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY | dma_addr)); /* dst: fixed to 3wire#0 */ if (ch == 0) write_reg(PS3_AUDIO_DEST(dma_ch), (PS3_AUDIO_DEST_TARGET_AUDIOFIFO | PS3_AUDIO_AO_3W_LDATA(0))); else write_reg(PS3_AUDIO_DEST(dma_ch), (PS3_AUDIO_DEST_TARGET_AUDIOFIFO | PS3_AUDIO_AO_3W_RDATA(0))); /* count always 1 DMA block (1/2 stage = 128 bytes) */ write_reg(PS3_AUDIO_DMASIZE(dma_ch), 0); /* bump pointer if needed */ if (!silent) snd_ps3_bump_buffer(card, ch, PS3_AUDIO_DMAC_BLOCK_SIZE, stage); /* kick event */ if (dma_ch == 0) write_reg(PS3_AUDIO_KICK(dma_ch), ch0_kick_event); else write_reg(PS3_AUDIO_KICK(dma_ch), PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch - 1) | PS3_AUDIO_KICK_REQUEST); } } /* ensure the hardware sees the change */ wmb(); spin_unlock_irqrestore(&card->dma_lock, irqsave); return 0; } /* * Interrupt handler */ static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id) { uint32_t port_
/*
 * Copyright 2014 Tilera Corporation. All Rights Reserved.
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation, version 2.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 *   NON INFRINGEMENT.  See the GNU General Public License for
 *   more details.
 */

#include <linux/errno.h>
#include <linux/spinlock.h>
#include <linux/module.h>
#include <linux/atomic.h>
#include <linux/interrupt.h>

#include <asm/processor.h>
#include <asm/pmc.h>

perf_irq_t perf_irq = NULL;
int handle_perf_interrupt(struct pt_regs *regs, int fault)
{
	int retval;

	if (!perf_irq)
		panic("Unexpected PERF_COUNT interrupt %d\n", fault);

	nmi_enter();
	retval = perf_irq(regs, fault);
	nmi_exit();
	return retval;
}

/* Reserve PMC hardware if it is available. */
perf_irq_t reserve_pmc_hardware(perf_irq_t new_perf_irq)
{
	return cmpxchg(&perf_irq, NULL, new_perf_irq);
}
EXPORT_SYMBOL(reserve_pmc_hardware);

/* Release PMC hardware. */
void release_pmc_hardware(void)
{
	perf_irq = NULL;
}
EXPORT_SYMBOL(release_pmc_hardware);


/*
 * Get current overflow status of each performance counter,
 * and auxiliary performance counter.
 */
unsigned long
pmc_get_overflow(void)
{
	unsigned long status;

	/*
	 * merge base+aux into a single vector
	 */
	status = __insn_mfspr(SPR_PERF_COUNT_STS);
	status |= __insn_mfspr(SPR_AUX_PERF_COUNT_STS) << TILE_BASE_COUNTERS;
	return status;
}

/*
 * Clear the status bit for the corresponding counter, if written
 * with a one.
 */
void
pmc_ack_overflow(unsigned long status)
{
	/*
	 * clear overflow status by writing ones
	 */
	__insn_mtspr(SPR_PERF_COUNT_STS, status);
	__insn_mtspr(SPR_AUX_PERF_COUNT_STS, status >> TILE_BASE_COUNTERS);
}

/*
 * The perf count interrupts are masked and unmasked explicitly,
 * and only here.  The normal irq_enable() does not enable them,
 * and irq_disable() does not disable them.  That lets these
 * routines drive the perf count interrupts orthogonally.
 *
 * We also mask the perf count interrupts on entry to the perf count
 * interrupt handler in assembly code, and by default unmask them
 * again (with interrupt critical section protection) just before
 * returning from the interrupt.  If the perf count handler returns
 * a non-zero error code, then we don't re-enable them before returning.
 *
 * For Pro, we rely on both interrupts being in the same word to update
 * them atomically so we never have one enabled and one disabled.
 */

#if CHIP_HAS_SPLIT_INTR_MASK()
# if INT_PERF_COUNT < 32 || INT_AUX_PERF_COUNT < 32
#  error Fix assumptions about which word PERF_COUNT interrupts are in
# endif
#endif

static inline unsigned long long pmc_mask(void)
{
	unsigned long long mask = 1ULL << INT_PERF_COUNT;
	mask |= 1ULL << INT_AUX_PERF_COUNT;
	return mask;
}

void unmask_pmc_interrupts(void)
{
	interrupt_mask_reset_mask(pmc_mask());
}

void mask_pmc_interrupts(void)
{
	interrupt_mask_set_mask(pmc_mask());
}
set default sample rate/word width */ snd_ps3_init_avsetting(&the_card); /* register the card */ ret = snd_card_register(the_card.card); if (ret < 0) goto clean_dma_map; pr_info("%s started. start_delay=%dms\n", the_card.card->longname, the_card.start_delay); return 0; clean_dma_map: dma_free_coherent(&the_card.ps3_dev->core, PAGE_SIZE, the_card.null_buffer_start_vaddr, the_card.null_buffer_start_dma_addr); clean_card: snd_card_free(the_card.card); clean_irq: snd_ps3_free_irq(); clean_dma_region: ps3_dma_region_free(dev->d_region); clean_mmio: snd_ps3_unmap_mmio(); clean_dev_map: lv1_gpu_device_unmap(2); clean_open: ps3_close_hv_device(dev); /* * there is no destructor function to pcm. * midlayer automatically releases if the card removed */ return ret; }; /* snd_ps3_probe */ /* called when module removal */ static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev) { int ret; pr_info("%s:start id=%d\n", __func__, dev->match_id); if (dev->match_id != PS3_MATCH_ID_SOUND) return -ENXIO; /* * ctl and preallocate buffer will be freed in * snd_card_free */ ret = snd_card_free(the_card.card); if (ret) pr_info("%s: ctl freecard=%d\n", __func__, ret); dma_free_coherent(&dev->core, PAGE_SIZE, the_card.null_buffer_start_vaddr, the_card.null_buffer_start_dma_addr); ps3_dma_region_free(dev->d_region); snd_ps3_free_irq(); snd_ps3_unmap_mmio(); lv1_gpu_device_unmap(2); ps3_close_hv_device(dev); pr_info("%s:end id=%d\n", __func__, dev->match_id); return 0; } /* snd_ps3_remove */ static struct ps3_system_bus_driver snd_ps3_bus_driver_info = { .match_id = PS3_MATCH_ID_SOUND, .probe = snd_ps3_driver_probe, .remove = snd_ps3_driver_remove, .shutdown = snd_ps3_driver_remove, .core = { .name = SND_PS3_DRIVER_NAME, .owner = THIS_MODULE, }, }; /* * module/subsystem initialize/terminate */ static int __init snd_ps3_init(void) { int ret; if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) return -ENXIO; memset(&the_card, 0, sizeof(the_card)); spin_lock_init(&the_card.dma_lock); /* register systembus DRIVER, this calls our probe() func */ ret = ps3_system_bus_driver_register(&snd_ps3_bus_driver_info); return ret; } module_init(snd_ps3_init); static void __exit snd_ps3_exit(void) { ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info); } module_exit(snd_ps3_exit); MODULE_LICENSE("GPL v2"); MODULE_DESCRIPTION("PS3 sound driver"); MODULE_AUTHOR("Sony Computer Entertainment Inc."); MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND);