macboing.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * http://til.info.apple.com/techinfo.nsf/artnum/n16405
  108. *
  109. * --David Kilzer
  110. */
  111. mac_special_bell = mac_av_start_bell;
  112. break;
  113. case MAC_MODEL_Q650:
  114. case MAC_MODEL_Q700:
  115. case MAC_MODEL_Q800:
  116. case MAC_MODEL_Q900:
  117. case MAC_MODEL_Q950:
  118. /*
  119. * Currently not implemented!
  120. */
  121. mac_special_bell = NULL;
  122. break;
  123. default:
  124. /*
  125. * Every switch needs a default
  126. */
  127. mac_special_bell = NULL;
  128. break;
  129. }
  130. /*
  131. * init the wave table with a simple triangular wave
  132. * A sine wave would sure be nicer here ...
  133. */
  134. for ( i = 0; i < 0x400; i++ )
  135. {
  136. mac_asc_wave_tab[ i ] = i / 4;
  137. mac_asc_wave_tab[ i + 0x400 ] = 0xFF - i / 4;
  138. }
  139. mac_asc_inited = 1;
  140. }
  141. /*
  142. * Called to make noise; current single entry to the boing driver.
  143. * Does the job for simple ASC, calls other routines else.
  144. * XXX Fixme:
  145. * Should be split into asc_mksound, easc_mksound, av_mksound and
  146. * function pointer set in mac_init_asc which would be called at
  147. * init time.
  148. * _This_ is rather ugly ...
  149. */
  150. void mac_mksound( unsigned int freq, unsigned int length )
  151. {
  152. __u32 cfreq = ( freq << 5 ) / 468;
  153. __u32 flags;
  154. int i;
  155. if ( mac_special_bell == NULL )
  156. {
  157. /* Do nothing */
  158. return;
  159. }
  160. if ( !mac_asc_inited )
  161. mac_init_asc();
  162. if ( mac_special_bell )
  163. {
  164. mac_special_bell( freq, length, 128 );
  165. return;
  166. }
  167. if ( freq < 20 || freq > 20000 || length == 0 )
  168. {
  169. mac_nosound( 0 );
  170. return;
  171. }
  172. local_irq_save(flags);
  173. del_timer( &mac_sound_timer );
  174. for ( i = 0; i < 0x800; i++ )
  175. mac_asc_regs[ i ] = 0;
  176. for ( i = 0; i < 0x800; i++ )
  177. mac_asc_regs[ i ] = mac_asc_wave_tab[ i ];
  178. for ( i = 0; i < 8; i++ )
  179. *( __u32* )( ( __u32 )mac_asc_regs + ASC_CONTROL + 0x814 + 8 * i ) = cfreq;
  180. mac_asc_regs[ 0x807 ] = 0;
  181. mac_asc_regs[ ASC_VOLUME ] = 128;
  182. mac_asc_regs[ 0x805 ] = 0;
  183. mac_asc_regs[ 0x80F ] = 0;
  184. mac_asc_regs[ ASC_MODE ] = ASC_MODE_SAMPLE;
  185. mac_asc_regs[ ASC_ENABLE ] = ASC_ENABLE_SAMPLE;
  186. mac_sound_timer.expires = jiffies + length;
  187. add_timer( &mac_sound_timer );
  188. local_irq_restore(flags);
  189. }
  190. /*
  191. * regular ASC: stop whining ..
  192. */
  193. static void mac_nosound( unsigned long ignored )
  194. {
  195. mac_asc_regs[ ASC_ENABLE ] = 0;
  196. }
  197. /*
  198. * EASC entry; init EASC, don't load wavetable, schedule 'start whining'.
  199. */
  200. static void mac_quadra_start_bell( unsigned int freq, unsigned int length, unsigned int volume )
  201. {
  202. __u32 flags;
  203. /* if the bell is already ringing, ring longer */
  204. if ( mac_bell_duration > 0 )
  205. {
  206. mac_bell_duration += length;
  207. return;
  208. }
  209. mac_bell_duration = length;
  210. mac_bell_phase = 0;
  211. mac_bell_phasepersample = ( freq * sizeof( mac_asc_wave_tab ) ) / mac_asc_samplespersec;
  212. /* this is reasonably big for small frequencies */
  213. local_irq_save(flags);
  214. /* set the volume */
  215. mac_asc_regs[ 0x806 ] = volume;
  216. /* set up the ASC registers */
  217. if ( mac_asc_regs[ 0x801 ] != 1 )
  218. {
  219. /* select mono mode */
  220. mac_asc_regs[ 0x807 ] = 0;
  221. /* select sampled sound mode */
  222. mac_asc_regs[ 0x802 ] = 0;
  223. /* ??? */
  224. mac_asc_regs[ 0x801 ] = 1;
  225. mac_asc_regs[ 0x803 ] |= 0x80;
  226. mac_asc_regs[ 0x803 ] &= 0x7F;
  227. }
  228. mac_sound_timer.function = mac_quadra_ring_bell;
  229. mac_sound_timer.expires = jiffies + 1;
  230. add_timer( &mac_sound_timer );
  231. local_irq_restore(flags);
  232. }
  233. /*
  234. * EASC 'start/continue whining'; I'm not sure why the above function didn't
  235. * already load the wave table, or at least call this one...
  236. * This piece keeps reloading the wave table until done.
  237. */
  238. static void mac_quadra_ring_bell( unsigned long ignored )
  239. {
  240. int i, count = mac_asc_samplespersec / HZ;
  241. __u32 flags;
  242. /*
  243. * we neither want a sound buffer overflow nor underflow, so we need to match
  244. * the number of samples per timer interrupt as exactly as possible.
  245. * using the asc interrupt will give better results in the future
  246. * ...and the possibility to use a real sample (a boingy noise, maybe...)
  247. */
  248. local_irq_save(flags);
  249. del_timer( &mac_sound_timer );
  250. if ( mac_bell_duration-- > 0 )
  251. {
  252. for ( i = 0; i < count; i++ )
  253. {
  254. mac_bell_phase += mac_bell_phasepersample;
  255. mac_asc_regs[ 0 ] = mac_asc_wave_tab[ mac_bell_phase & ( sizeof( mac_asc_wave_tab ) - 1 ) ];
  256. }
  257. mac_sound_timer.expires = jiffies + 1;
  258. add_timer( &mac_sound_timer );
  259. }
  260. else
  261. mac_asc_regs[ 0x801 ] = 0;
  262. local_irq_restore(flags);
  263. }
  264. /*
  265. * AV code - please fill in.
  266. */
  267. static void mac_av_start_bell( unsigned int freq, unsigned int length, unsigned int volume )
  268. {
  269. }