1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Mac bong noise generator. Note - we ought to put a boingy noise
4*4882a593Smuzhiyun * here 8)
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * ----------------------------------------------------------------------
7*4882a593Smuzhiyun * 16.11.98:
8*4882a593Smuzhiyun * rewrote some functions, added support for Enhanced ASC (Quadras)
9*4882a593Smuzhiyun * after the NetBSD asc.c console bell patch by Colin Wood/Frederick Bruck
10*4882a593Smuzhiyun * Juergen Mellinger (juergen.mellinger@t-online.de)
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/sched.h>
14*4882a593Smuzhiyun #include <linux/timer.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <asm/macintosh.h>
17*4882a593Smuzhiyun #include <asm/mac_asc.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun static int mac_asc_inited;
20*4882a593Smuzhiyun /*
21*4882a593Smuzhiyun * dumb triangular wave table
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun static __u8 mac_asc_wave_tab[ 0x800 ];
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun * Alan's original sine table; needs interpolating to 0x800
27*4882a593Smuzhiyun * (hint: interpolate or hardwire [0 -> Pi/2[, it's symmetric)
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun static const signed char sine_data[] = {
30*4882a593Smuzhiyun 0, 39, 75, 103, 121, 127, 121, 103, 75, 39,
31*4882a593Smuzhiyun 0, -39, -75, -103, -121, -127, -121, -103, -75, -39
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun * where the ASC hides ...
36*4882a593Smuzhiyun */
37*4882a593Smuzhiyun static volatile __u8* mac_asc_regs = ( void* )0x50F14000;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun * sample rate; is this a good default value?
41*4882a593Smuzhiyun */
42*4882a593Smuzhiyun static unsigned long mac_asc_samplespersec = 11050;
43*4882a593Smuzhiyun static int mac_bell_duration;
44*4882a593Smuzhiyun static unsigned long mac_bell_phase; /* 0..2*Pi -> 0..0x800 (wavetable size) */
45*4882a593Smuzhiyun static unsigned long mac_bell_phasepersample;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun * some function protos
49*4882a593Smuzhiyun */
50*4882a593Smuzhiyun static void mac_init_asc( void );
51*4882a593Smuzhiyun static void mac_nosound(struct timer_list *);
52*4882a593Smuzhiyun static void mac_quadra_start_bell( unsigned int, unsigned int, unsigned int );
53*4882a593Smuzhiyun static void mac_quadra_ring_bell(struct timer_list *);
54*4882a593Smuzhiyun static void mac_av_start_bell( unsigned int, unsigned int, unsigned int );
55*4882a593Smuzhiyun static void ( *mac_special_bell )( unsigned int, unsigned int, unsigned int );
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * our timer to start/continue/stop the bell
59*4882a593Smuzhiyun */
60*4882a593Smuzhiyun static DEFINE_TIMER(mac_sound_timer, mac_nosound);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /*
63*4882a593Smuzhiyun * Sort of initialize the sound chip (called from mac_mksound on the first
64*4882a593Smuzhiyun * beep).
65*4882a593Smuzhiyun */
mac_init_asc(void)66*4882a593Smuzhiyun static void mac_init_asc( void )
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun int i;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun * do some machine specific initialization
72*4882a593Smuzhiyun * BTW:
73*4882a593Smuzhiyun * the NetBSD Quadra patch identifies the Enhanced Apple Sound Chip via
74*4882a593Smuzhiyun * mac_asc_regs[ 0x800 ] & 0xF0 != 0
75*4882a593Smuzhiyun * this makes no sense here, because we have to set the default sample
76*4882a593Smuzhiyun * rate anyway if we want correct frequencies
77*4882a593Smuzhiyun */
78*4882a593Smuzhiyun switch ( macintosh_config->ident )
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun case MAC_MODEL_IIFX:
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun * The IIfx is always special ...
83*4882a593Smuzhiyun */
84*4882a593Smuzhiyun mac_asc_regs = ( void* )0x50010000;
85*4882a593Smuzhiyun break;
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * not sure about how correct this list is
88*4882a593Smuzhiyun * machines with the EASC enhanced apple sound chip
89*4882a593Smuzhiyun */
90*4882a593Smuzhiyun case MAC_MODEL_Q630:
91*4882a593Smuzhiyun case MAC_MODEL_P475:
92*4882a593Smuzhiyun mac_special_bell = mac_quadra_start_bell;
93*4882a593Smuzhiyun mac_asc_samplespersec = 22150;
94*4882a593Smuzhiyun break;
95*4882a593Smuzhiyun case MAC_MODEL_C660:
96*4882a593Smuzhiyun case MAC_MODEL_Q840:
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun * The Quadra 660AV and 840AV use the "Singer" custom ASIC for sound I/O.
99*4882a593Smuzhiyun * It appears to be similar to the "AWACS" custom ASIC in the Power Mac
100*4882a593Smuzhiyun * [678]100. Because Singer and AWACS may have a similar hardware
101*4882a593Smuzhiyun * interface, this would imply that the code in drivers/sound/dmasound.c
102*4882a593Smuzhiyun * for AWACS could be used as a basis for Singer support. All we have to
103*4882a593Smuzhiyun * do is figure out how to do DMA on the 660AV/840AV through the PSC and
104*4882a593Smuzhiyun * figure out where the Singer hardware sits in memory. (I'd look in the
105*4882a593Smuzhiyun * vicinity of the AWACS location in a Power Mac [678]100 first, or the
106*4882a593Smuzhiyun * current location of the Apple Sound Chip--ASC--in other Macs.) The
107*4882a593Smuzhiyun * Power Mac [678]100 info can be found in MkLinux Mach kernel sources.
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * Quoted from Apple's Tech Info Library, article number 16405:
110*4882a593Smuzhiyun * "Among desktop Macintosh computers, only the 660AV, 840AV, and Power
111*4882a593Smuzhiyun * Macintosh models have 16-bit audio input and output capability
112*4882a593Smuzhiyun * because of the AT&T DSP3210 hardware circuitry and the 16-bit Singer
113*4882a593Smuzhiyun * codec circuitry in the AVs. The Audio Waveform Amplifier and
114*4882a593Smuzhiyun * Converter (AWAC) chip in the Power Macintosh performs the same
115*4882a593Smuzhiyun * 16-bit I/O functionality. The PowerBook 500 series computers
116*4882a593Smuzhiyun * support 16-bit stereo output, but only mono input."
117*4882a593Smuzhiyun *
118*4882a593Smuzhiyun * Technical Information Library (TIL) article number 16405.
119*4882a593Smuzhiyun * https://support.apple.com/kb/TA32601
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * --David Kilzer
122*4882a593Smuzhiyun */
123*4882a593Smuzhiyun mac_special_bell = mac_av_start_bell;
124*4882a593Smuzhiyun break;
125*4882a593Smuzhiyun case MAC_MODEL_Q650:
126*4882a593Smuzhiyun case MAC_MODEL_Q700:
127*4882a593Smuzhiyun case MAC_MODEL_Q800:
128*4882a593Smuzhiyun case MAC_MODEL_Q900:
129*4882a593Smuzhiyun case MAC_MODEL_Q950:
130*4882a593Smuzhiyun /*
131*4882a593Smuzhiyun * Currently not implemented!
132*4882a593Smuzhiyun */
133*4882a593Smuzhiyun mac_special_bell = NULL;
134*4882a593Smuzhiyun break;
135*4882a593Smuzhiyun default:
136*4882a593Smuzhiyun /*
137*4882a593Smuzhiyun * Every switch needs a default
138*4882a593Smuzhiyun */
139*4882a593Smuzhiyun mac_special_bell = NULL;
140*4882a593Smuzhiyun break;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /*
144*4882a593Smuzhiyun * init the wave table with a simple triangular wave
145*4882a593Smuzhiyun * A sine wave would sure be nicer here ...
146*4882a593Smuzhiyun */
147*4882a593Smuzhiyun for ( i = 0; i < 0x400; i++ )
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun mac_asc_wave_tab[ i ] = i / 4;
150*4882a593Smuzhiyun mac_asc_wave_tab[ i + 0x400 ] = 0xFF - i / 4;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun mac_asc_inited = 1;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun * Called to make noise; current single entry to the boing driver.
157*4882a593Smuzhiyun * Does the job for simple ASC, calls other routines else.
158*4882a593Smuzhiyun * XXX Fixme:
159*4882a593Smuzhiyun * Should be split into asc_mksound, easc_mksound, av_mksound and
160*4882a593Smuzhiyun * function pointer set in mac_init_asc which would be called at
161*4882a593Smuzhiyun * init time.
162*4882a593Smuzhiyun * _This_ is rather ugly ...
163*4882a593Smuzhiyun */
mac_mksound(unsigned int freq,unsigned int length)164*4882a593Smuzhiyun void mac_mksound( unsigned int freq, unsigned int length )
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun __u32 cfreq = ( freq << 5 ) / 468;
167*4882a593Smuzhiyun unsigned long flags;
168*4882a593Smuzhiyun int i;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun if ( mac_special_bell == NULL )
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun /* Do nothing */
173*4882a593Smuzhiyun return;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun if ( !mac_asc_inited )
177*4882a593Smuzhiyun mac_init_asc();
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun if ( mac_special_bell )
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun mac_special_bell( freq, length, 128 );
182*4882a593Smuzhiyun return;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun if ( freq < 20 || freq > 20000 || length == 0 )
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun mac_nosound( 0 );
188*4882a593Smuzhiyun return;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun local_irq_save(flags);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun del_timer( &mac_sound_timer );
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun for ( i = 0; i < 0x800; i++ )
196*4882a593Smuzhiyun mac_asc_regs[ i ] = 0;
197*4882a593Smuzhiyun for ( i = 0; i < 0x800; i++ )
198*4882a593Smuzhiyun mac_asc_regs[ i ] = mac_asc_wave_tab[ i ];
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun for ( i = 0; i < 8; i++ )
201*4882a593Smuzhiyun *( __u32* )( ( __u32 )mac_asc_regs + ASC_CONTROL + 0x814 + 8 * i ) = cfreq;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun mac_asc_regs[ 0x807 ] = 0;
204*4882a593Smuzhiyun mac_asc_regs[ ASC_VOLUME ] = 128;
205*4882a593Smuzhiyun mac_asc_regs[ 0x805 ] = 0;
206*4882a593Smuzhiyun mac_asc_regs[ 0x80F ] = 0;
207*4882a593Smuzhiyun mac_asc_regs[ ASC_MODE ] = ASC_MODE_SAMPLE;
208*4882a593Smuzhiyun mac_asc_regs[ ASC_ENABLE ] = ASC_ENABLE_SAMPLE;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun mac_sound_timer.expires = jiffies + length;
211*4882a593Smuzhiyun add_timer( &mac_sound_timer );
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun local_irq_restore(flags);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun * regular ASC: stop whining ..
218*4882a593Smuzhiyun */
mac_nosound(struct timer_list * unused)219*4882a593Smuzhiyun static void mac_nosound(struct timer_list *unused)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun mac_asc_regs[ ASC_ENABLE ] = 0;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /*
225*4882a593Smuzhiyun * EASC entry; init EASC, don't load wavetable, schedule 'start whining'.
226*4882a593Smuzhiyun */
mac_quadra_start_bell(unsigned int freq,unsigned int length,unsigned int volume)227*4882a593Smuzhiyun static void mac_quadra_start_bell( unsigned int freq, unsigned int length, unsigned int volume )
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun unsigned long flags;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /* if the bell is already ringing, ring longer */
232*4882a593Smuzhiyun if ( mac_bell_duration > 0 )
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun mac_bell_duration += length;
235*4882a593Smuzhiyun return;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun mac_bell_duration = length;
239*4882a593Smuzhiyun mac_bell_phase = 0;
240*4882a593Smuzhiyun mac_bell_phasepersample = ( freq * sizeof( mac_asc_wave_tab ) ) / mac_asc_samplespersec;
241*4882a593Smuzhiyun /* this is reasonably big for small frequencies */
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun local_irq_save(flags);
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /* set the volume */
246*4882a593Smuzhiyun mac_asc_regs[ 0x806 ] = volume;
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /* set up the ASC registers */
249*4882a593Smuzhiyun if ( mac_asc_regs[ 0x801 ] != 1 )
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun /* select mono mode */
252*4882a593Smuzhiyun mac_asc_regs[ 0x807 ] = 0;
253*4882a593Smuzhiyun /* select sampled sound mode */
254*4882a593Smuzhiyun mac_asc_regs[ 0x802 ] = 0;
255*4882a593Smuzhiyun /* ??? */
256*4882a593Smuzhiyun mac_asc_regs[ 0x801 ] = 1;
257*4882a593Smuzhiyun mac_asc_regs[ 0x803 ] |= 0x80;
258*4882a593Smuzhiyun mac_asc_regs[ 0x803 ] &= 0x7F;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun mac_sound_timer.function = mac_quadra_ring_bell;
262*4882a593Smuzhiyun mac_sound_timer.expires = jiffies + 1;
263*4882a593Smuzhiyun add_timer( &mac_sound_timer );
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun local_irq_restore(flags);
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun /*
269*4882a593Smuzhiyun * EASC 'start/continue whining'; I'm not sure why the above function didn't
270*4882a593Smuzhiyun * already load the wave table, or at least call this one...
271*4882a593Smuzhiyun * This piece keeps reloading the wave table until done.
272*4882a593Smuzhiyun */
mac_quadra_ring_bell(struct timer_list * unused)273*4882a593Smuzhiyun static void mac_quadra_ring_bell(struct timer_list *unused)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun int i, count = mac_asc_samplespersec / HZ;
276*4882a593Smuzhiyun unsigned long flags;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun /*
279*4882a593Smuzhiyun * we neither want a sound buffer overflow nor underflow, so we need to match
280*4882a593Smuzhiyun * the number of samples per timer interrupt as exactly as possible.
281*4882a593Smuzhiyun * using the asc interrupt will give better results in the future
282*4882a593Smuzhiyun * ...and the possibility to use a real sample (a boingy noise, maybe...)
283*4882a593Smuzhiyun */
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun local_irq_save(flags);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun del_timer( &mac_sound_timer );
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun if ( mac_bell_duration-- > 0 )
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun for ( i = 0; i < count; i++ )
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun mac_bell_phase += mac_bell_phasepersample;
294*4882a593Smuzhiyun mac_asc_regs[ 0 ] = mac_asc_wave_tab[ mac_bell_phase & ( sizeof( mac_asc_wave_tab ) - 1 ) ];
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun mac_sound_timer.expires = jiffies + 1;
297*4882a593Smuzhiyun add_timer( &mac_sound_timer );
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun else
300*4882a593Smuzhiyun mac_asc_regs[ 0x801 ] = 0;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun local_irq_restore(flags);
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun /*
306*4882a593Smuzhiyun * AV code - please fill in.
307*4882a593Smuzhiyun */
mac_av_start_bell(unsigned int freq,unsigned int length,unsigned int volume)308*4882a593Smuzhiyun static void mac_av_start_bell( unsigned int freq, unsigned int length, unsigned int volume )
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun }
311