SB16.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Memory.h>
  27. #include <AK/Singleton.h>
  28. #include <AK/StringView.h>
  29. #include <Kernel/Debug.h>
  30. #include <Kernel/Devices/SB16.h>
  31. #include <Kernel/IO.h>
  32. #include <Kernel/Thread.h>
  33. #include <Kernel/VM/AnonymousVMObject.h>
  34. #include <Kernel/VM/MemoryManager.h>
  35. namespace Kernel {
  36. #define SB16_DEFAULT_IRQ 5
  37. enum class SampleFormat : u8 {
  38. Signed = 0x10,
  39. Stereo = 0x20,
  40. };
  41. const u16 DSP_READ = 0x22A;
  42. const u16 DSP_WRITE = 0x22C;
  43. const u16 DSP_STATUS = 0x22E;
  44. const u16 DSP_R_ACK = 0x22F;
  45. /* Write a value to the DSP write register */
  46. void SB16::dsp_write(u8 value)
  47. {
  48. while (IO::in8(DSP_WRITE) & 0x80) {
  49. ;
  50. }
  51. IO::out8(DSP_WRITE, value);
  52. }
  53. /* Reads the value of the DSP read register */
  54. u8 SB16::dsp_read()
  55. {
  56. while (!(IO::in8(DSP_STATUS) & 0x80)) {
  57. ;
  58. }
  59. return IO::in8(DSP_READ);
  60. }
  61. /* Changes the sample rate of sound output */
  62. void SB16::set_sample_rate(uint16_t hz)
  63. {
  64. dsp_write(0x41); // output
  65. dsp_write((u8)(hz >> 8));
  66. dsp_write((u8)hz);
  67. dsp_write(0x42); // input
  68. dsp_write((u8)(hz >> 8));
  69. dsp_write((u8)hz);
  70. }
  71. static AK::Singleton<SB16> s_the;
  72. SB16::SB16()
  73. : IRQHandler(SB16_DEFAULT_IRQ)
  74. , CharacterDevice(42, 42) // ### ?
  75. {
  76. initialize();
  77. }
  78. SB16::~SB16()
  79. {
  80. }
  81. void SB16::create()
  82. {
  83. s_the.ensure_instance();
  84. }
  85. SB16& SB16::the()
  86. {
  87. return *s_the;
  88. }
  89. void SB16::initialize()
  90. {
  91. disable_irq();
  92. IO::out8(0x226, 1);
  93. IO::delay(32);
  94. IO::out8(0x226, 0);
  95. auto data = dsp_read();
  96. if (data != 0xaa) {
  97. klog() << "SB16: sb not ready";
  98. return;
  99. }
  100. // Get the version info
  101. dsp_write(0xe1);
  102. m_major_version = dsp_read();
  103. auto vmin = dsp_read();
  104. klog() << "SB16: found version " << m_major_version << "." << vmin;
  105. set_irq_register(SB16_DEFAULT_IRQ);
  106. klog() << "SB16: IRQ " << get_irq_line();
  107. }
  108. void SB16::set_irq_register(u8 irq_number)
  109. {
  110. u8 bitmask;
  111. switch (irq_number) {
  112. case 2:
  113. bitmask = 0;
  114. break;
  115. case 5:
  116. bitmask = 0b10;
  117. break;
  118. case 7:
  119. bitmask = 0b100;
  120. break;
  121. case 10:
  122. bitmask = 0b1000;
  123. break;
  124. default:
  125. ASSERT_NOT_REACHED();
  126. }
  127. IO::out8(0x224, 0x80);
  128. IO::out8(0x225, bitmask);
  129. }
  130. u8 SB16::get_irq_line()
  131. {
  132. IO::out8(0x224, 0x80);
  133. u8 bitmask = IO::in8(0x225);
  134. switch (bitmask) {
  135. case 0:
  136. return 2;
  137. case 0b10:
  138. return 5;
  139. case 0b100:
  140. return 7;
  141. case 0b1000:
  142. return 10;
  143. }
  144. return bitmask;
  145. }
  146. void SB16::set_irq_line(u8 irq_number)
  147. {
  148. InterruptDisabler disabler;
  149. if (irq_number == get_irq_line())
  150. return;
  151. set_irq_register(irq_number);
  152. change_irq_number(irq_number);
  153. }
  154. bool SB16::can_read(const FileDescription&, size_t) const
  155. {
  156. return false;
  157. }
  158. KResultOr<size_t> SB16::read(FileDescription&, size_t, UserOrKernelBuffer&, size_t)
  159. {
  160. return 0;
  161. }
  162. void SB16::dma_start(uint32_t length)
  163. {
  164. const auto addr = m_dma_region->physical_page(0)->paddr().get();
  165. const u8 channel = 5; // 16-bit samples use DMA channel 5 (on the master DMA controller)
  166. const u8 mode = 0x48;
  167. // Disable the DMA channel
  168. IO::out8(0xd4, 4 + (channel % 4));
  169. // Clear the byte pointer flip-flop
  170. IO::out8(0xd8, 0);
  171. // Write the DMA mode for the transfer
  172. IO::out8(0xd6, (channel % 4) | mode);
  173. // Write the offset of the buffer
  174. u16 offset = (addr / 2) % 65536;
  175. IO::out8(0xc4, (u8)offset);
  176. IO::out8(0xc4, (u8)(offset >> 8));
  177. // Write the transfer length
  178. IO::out8(0xc6, (u8)(length - 1));
  179. IO::out8(0xc6, (u8)((length - 1) >> 8));
  180. // Write the buffer
  181. IO::out8(0x8b, addr >> 16);
  182. // Enable the DMA channel
  183. IO::out8(0xd4, (channel % 4));
  184. }
  185. void SB16::handle_irq(const RegisterState&)
  186. {
  187. // Stop sound output ready for the next block.
  188. dsp_write(0xd5);
  189. IO::in8(DSP_STATUS); // 8 bit interrupt
  190. if (m_major_version >= 4)
  191. IO::in8(DSP_R_ACK); // 16 bit interrupt
  192. m_irq_queue.wake_all();
  193. }
  194. void SB16::wait_for_irq()
  195. {
  196. m_irq_queue.wait_on({}, "SB16");
  197. disable_irq();
  198. }
  199. KResultOr<size_t> SB16::write(FileDescription&, size_t, const UserOrKernelBuffer& data, size_t length)
  200. {
  201. if (!m_dma_region) {
  202. auto page = MM.allocate_supervisor_physical_page();
  203. if (!page)
  204. return ENOMEM;
  205. auto vmobject = AnonymousVMObject::create_with_physical_page(*page);
  206. m_dma_region = MM.allocate_kernel_region_with_vmobject(*vmobject, PAGE_SIZE, "SB16 DMA buffer", Region::Access::Write);
  207. if (!m_dma_region)
  208. return ENOMEM;
  209. }
  210. #if SB16_DEBUG
  211. klog() << "SB16: Writing buffer of " << length << " bytes";
  212. #endif
  213. ASSERT(length <= PAGE_SIZE);
  214. const int BLOCK_SIZE = 32 * 1024;
  215. if (length > BLOCK_SIZE) {
  216. return ENOSPC;
  217. }
  218. u8 mode = (u8)SampleFormat::Signed | (u8)SampleFormat::Stereo;
  219. const int sample_rate = 44100;
  220. set_sample_rate(sample_rate);
  221. if (!data.read(m_dma_region->vaddr().as_ptr(), length))
  222. return EFAULT;
  223. dma_start(length);
  224. // 16-bit single-cycle output.
  225. // FIXME: Implement auto-initialized output.
  226. u8 command = 0xb0;
  227. u16 sample_count = length / sizeof(i16);
  228. if (mode & (u8)SampleFormat::Stereo)
  229. sample_count /= 2;
  230. sample_count -= 1;
  231. cli();
  232. enable_irq();
  233. dsp_write(command);
  234. dsp_write(mode);
  235. dsp_write((u8)sample_count);
  236. dsp_write((u8)(sample_count >> 8));
  237. wait_for_irq();
  238. return length;
  239. }
  240. }