macboing.c 8.0 KB

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