// SPDX-License-Identifier: GPL-2.01/*2* Mac bong noise generator. Note - we ought to put a boingy noise3* here 8)4*5* ----------------------------------------------------------------------6* 16.11.98:7* rewrote some functions, added support for Enhanced ASC (Quadras)8* after the NetBSD asc.c console bell patch by Colin Wood/Frederick Bruck9* Juergen Mellinger ([email protected])10*/1112#include <linux/sched.h>13#include <linux/timer.h>1415#include <asm/macintosh.h>16#include <asm/mac_asc.h>1718#include "mac.h"1920static int mac_asc_inited;21/*22* dumb triangular wave table23*/24static __u8 mac_asc_wave_tab[ 0x800 ];2526/*27* where the ASC hides ...28*/29static volatile __u8* mac_asc_regs = ( void* )0x50F14000;3031/*32* sample rate; is this a good default value?33*/34static unsigned long mac_asc_samplespersec = 11050;35static int mac_bell_duration;36static unsigned long mac_bell_phase; /* 0..2*Pi -> 0..0x800 (wavetable size) */37static unsigned long mac_bell_phasepersample;3839/*40* some function protos41*/42static void mac_init_asc( void );43static void mac_nosound(struct timer_list *);44static void mac_quadra_start_bell( unsigned int, unsigned int, unsigned int );45static void mac_quadra_ring_bell(struct timer_list *);46static void mac_av_start_bell( unsigned int, unsigned int, unsigned int );47static void ( *mac_special_bell )( unsigned int, unsigned int, unsigned int );4849/*50* our timer to start/continue/stop the bell51*/52static DEFINE_TIMER(mac_sound_timer, mac_nosound);5354/*55* Sort of initialize the sound chip (called from mac_mksound on the first56* beep).57*/58static void mac_init_asc( void )59{60int i;6162/*63* do some machine specific initialization64* BTW:65* the NetBSD Quadra patch identifies the Enhanced Apple Sound Chip via66* mac_asc_regs[ 0x800 ] & 0xF0 != 067* this makes no sense here, because we have to set the default sample68* rate anyway if we want correct frequencies69*/70switch ( macintosh_config->ident )71{72case MAC_MODEL_IIFX:73/*74* The IIfx is always special ...75*/76mac_asc_regs = ( void* )0x50010000;77break;78/*79* not sure about how correct this list is80* machines with the EASC enhanced apple sound chip81*/82case MAC_MODEL_Q630:83case MAC_MODEL_P475:84mac_special_bell = mac_quadra_start_bell;85mac_asc_samplespersec = 22150;86break;87case MAC_MODEL_C660:88case MAC_MODEL_Q840:89/*90* The Quadra 660AV and 840AV use the "Singer" custom ASIC for sound I/O.91* It appears to be similar to the "AWACS" custom ASIC in the Power Mac92* [678]100. Because Singer and AWACS may have a similar hardware93* interface, this would imply that the code in drivers/sound/dmasound.c94* for AWACS could be used as a basis for Singer support. All we have to95* do is figure out how to do DMA on the 660AV/840AV through the PSC and96* figure out where the Singer hardware sits in memory. (I'd look in the97* vicinity of the AWACS location in a Power Mac [678]100 first, or the98* current location of the Apple Sound Chip--ASC--in other Macs.) The99* Power Mac [678]100 info can be found in MkLinux Mach kernel sources.100*101* Quoted from Apple's Tech Info Library, article number 16405:102* "Among desktop Macintosh computers, only the 660AV, 840AV, and Power103* Macintosh models have 16-bit audio input and output capability104* because of the AT&T DSP3210 hardware circuitry and the 16-bit Singer105* codec circuitry in the AVs. The Audio Waveform Amplifier and106* Converter (AWAC) chip in the Power Macintosh performs the same107* 16-bit I/O functionality. The PowerBook 500 series computers108* support 16-bit stereo output, but only mono input."109*110* Technical Information Library (TIL) article number 16405.111* https://support.apple.com/kb/TA32601112*113* --David Kilzer114*/115mac_special_bell = mac_av_start_bell;116break;117case MAC_MODEL_Q650:118case MAC_MODEL_Q700:119case MAC_MODEL_Q800:120case MAC_MODEL_Q900:121case MAC_MODEL_Q950:122/*123* Currently not implemented!124*/125mac_special_bell = NULL;126break;127default:128/*129* Every switch needs a default130*/131mac_special_bell = NULL;132break;133}134135/*136* init the wave table with a simple triangular wave137* A sine wave would sure be nicer here ...138*/139for ( i = 0; i < 0x400; i++ )140{141mac_asc_wave_tab[ i ] = i / 4;142mac_asc_wave_tab[ i + 0x400 ] = 0xFF - i / 4;143}144mac_asc_inited = 1;145}146147/*148* Called to make noise; current single entry to the boing driver.149* Does the job for simple ASC, calls other routines else.150* XXX Fixme:151* Should be split into asc_mksound, easc_mksound, av_mksound and152* function pointer set in mac_init_asc which would be called at153* init time.154* _This_ is rather ugly ...155*/156void mac_mksound( unsigned int freq, unsigned int length )157{158__u32 cfreq = ( freq << 5 ) / 468;159unsigned long flags;160int i;161162if ( mac_special_bell == NULL )163{164/* Do nothing */165return;166}167168if ( !mac_asc_inited )169mac_init_asc();170171if ( mac_special_bell )172{173mac_special_bell( freq, length, 128 );174return;175}176177if ( freq < 20 || freq > 20000 || length == 0 )178{179mac_nosound( 0 );180return;181}182183local_irq_save(flags);184185timer_delete(&mac_sound_timer);186187for ( i = 0; i < 0x800; i++ )188mac_asc_regs[ i ] = 0;189for ( i = 0; i < 0x800; i++ )190mac_asc_regs[ i ] = mac_asc_wave_tab[ i ];191192for ( i = 0; i < 8; i++ )193*( __u32* )( ( __u32 )mac_asc_regs + ASC_CONTROL + 0x814 + 8 * i ) = cfreq;194195mac_asc_regs[ 0x807 ] = 0;196mac_asc_regs[ ASC_VOLUME ] = 128;197mac_asc_regs[ 0x805 ] = 0;198mac_asc_regs[ 0x80F ] = 0;199mac_asc_regs[ ASC_MODE ] = ASC_MODE_SAMPLE;200mac_asc_regs[ ASC_ENABLE ] = ASC_ENABLE_SAMPLE;201202mac_sound_timer.expires = jiffies + length;203add_timer( &mac_sound_timer );204205local_irq_restore(flags);206}207208/*209* regular ASC: stop whining ..210*/211static void mac_nosound(struct timer_list *unused)212{213mac_asc_regs[ ASC_ENABLE ] = 0;214}215216/*217* EASC entry; init EASC, don't load wavetable, schedule 'start whining'.218*/219static void mac_quadra_start_bell( unsigned int freq, unsigned int length, unsigned int volume )220{221unsigned long flags;222223/* if the bell is already ringing, ring longer */224if ( mac_bell_duration > 0 )225{226mac_bell_duration += length;227return;228}229230mac_bell_duration = length;231mac_bell_phase = 0;232mac_bell_phasepersample = ( freq * sizeof( mac_asc_wave_tab ) ) / mac_asc_samplespersec;233/* this is reasonably big for small frequencies */234235local_irq_save(flags);236237/* set the volume */238mac_asc_regs[ 0x806 ] = volume;239240/* set up the ASC registers */241if ( mac_asc_regs[ 0x801 ] != 1 )242{243/* select mono mode */244mac_asc_regs[ 0x807 ] = 0;245/* select sampled sound mode */246mac_asc_regs[ 0x802 ] = 0;247/* ??? */248mac_asc_regs[ 0x801 ] = 1;249mac_asc_regs[ 0x803 ] |= 0x80;250mac_asc_regs[ 0x803 ] &= 0x7F;251}252253mac_sound_timer.function = mac_quadra_ring_bell;254mac_sound_timer.expires = jiffies + 1;255add_timer( &mac_sound_timer );256257local_irq_restore(flags);258}259260/*261* EASC 'start/continue whining'; I'm not sure why the above function didn't262* already load the wave table, or at least call this one...263* This piece keeps reloading the wave table until done.264*/265static void mac_quadra_ring_bell(struct timer_list *unused)266{267int i, count = mac_asc_samplespersec / HZ;268unsigned long flags;269270/*271* we neither want a sound buffer overflow nor underflow, so we need to match272* the number of samples per timer interrupt as exactly as possible.273* using the asc interrupt will give better results in the future274* ...and the possibility to use a real sample (a boingy noise, maybe...)275*/276277local_irq_save(flags);278279timer_delete(&mac_sound_timer);280281if ( mac_bell_duration-- > 0 )282{283for ( i = 0; i < count; i++ )284{285mac_bell_phase += mac_bell_phasepersample;286mac_asc_regs[ 0 ] = mac_asc_wave_tab[ mac_bell_phase & ( sizeof( mac_asc_wave_tab ) - 1 ) ];287}288mac_sound_timer.expires = jiffies + 1;289add_timer( &mac_sound_timer );290}291else292mac_asc_regs[ 0x801 ] = 0;293294local_irq_restore(flags);295}296297/*298* AV code - please fill in.299*/300static void mac_av_start_bell( unsigned int freq, unsigned int length, unsigned int volume )301{302}303304305