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