macboing.c 8.2 KB

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