macboing.c 8.2 KB

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