2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-03-08 14:33:14 +03:00
|
|
|
#include <AK/Memory.h>
|
2020-08-25 04:35:19 +03:00
|
|
|
#include <AK/Singleton.h>
|
2020-03-23 15:45:10 +03:00
|
|
|
#include <AK/StringView.h>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2019-07-13 21:15:17 +03:00
|
|
|
#include <Kernel/Devices/SB16.h>
|
2020-09-18 10:49:51 +03:00
|
|
|
#include <Kernel/IO.h>
|
2019-07-13 21:15:17 +03:00
|
|
|
#include <Kernel/Thread.h>
|
2020-01-20 15:07:29 +03:00
|
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
2019-07-12 20:28:01 +03:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
2020-02-22 21:19:13 +03:00
|
|
|
#define SB16_DEFAULT_IRQ 5
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2019-07-13 18:08:45 +03:00
|
|
|
enum class SampleFormat : u8 {
|
|
|
|
Signed = 0x10,
|
|
|
|
Stereo = 0x20,
|
|
|
|
};
|
|
|
|
|
2019-07-12 20:28:01 +03:00
|
|
|
const u16 DSP_READ = 0x22A;
|
|
|
|
const u16 DSP_WRITE = 0x22C;
|
|
|
|
const u16 DSP_STATUS = 0x22E;
|
|
|
|
const u16 DSP_R_ACK = 0x22F;
|
|
|
|
|
|
|
|
/* Write a value to the DSP write register */
|
|
|
|
void SB16::dsp_write(u8 value)
|
|
|
|
{
|
|
|
|
while (IO::in8(DSP_WRITE) & 0x80) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
IO::out8(DSP_WRITE, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reads the value of the DSP read register */
|
|
|
|
u8 SB16::dsp_read()
|
|
|
|
{
|
|
|
|
while (!(IO::in8(DSP_STATUS) & 0x80)) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
return IO::in8(DSP_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Changes the sample rate of sound output */
|
|
|
|
void SB16::set_sample_rate(uint16_t hz)
|
|
|
|
{
|
|
|
|
dsp_write(0x41); // output
|
|
|
|
dsp_write((u8)(hz >> 8));
|
|
|
|
dsp_write((u8)hz);
|
|
|
|
dsp_write(0x42); // input
|
|
|
|
dsp_write((u8)(hz >> 8));
|
|
|
|
dsp_write((u8)hz);
|
|
|
|
}
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
static AK::Singleton<SB16> s_the;
|
2019-07-12 20:28:01 +03:00
|
|
|
|
|
|
|
SB16::SB16()
|
2020-02-22 21:19:13 +03:00
|
|
|
: IRQHandler(SB16_DEFAULT_IRQ)
|
2019-07-12 20:28:01 +03:00
|
|
|
, CharacterDevice(42, 42) // ### ?
|
|
|
|
{
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
SB16::~SB16()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-05 09:18:46 +03:00
|
|
|
void SB16::detect()
|
|
|
|
{
|
|
|
|
IO::out8(0x226, 1);
|
|
|
|
IO::delay(32);
|
|
|
|
IO::out8(0x226, 0);
|
|
|
|
|
|
|
|
auto data = dsp_read();
|
|
|
|
if (data != 0xaa) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SB16::create();
|
|
|
|
}
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
void SB16::create()
|
|
|
|
{
|
|
|
|
s_the.ensure_instance();
|
|
|
|
}
|
|
|
|
|
2019-07-12 20:28:01 +03:00
|
|
|
SB16& SB16::the()
|
|
|
|
{
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SB16::initialize()
|
|
|
|
{
|
2020-01-23 00:23:50 +03:00
|
|
|
disable_irq();
|
2019-12-01 14:53:19 +03:00
|
|
|
|
2019-07-12 20:28:01 +03:00
|
|
|
IO::out8(0x226, 1);
|
2020-05-16 11:39:06 +03:00
|
|
|
IO::delay(32);
|
2019-07-12 20:28:01 +03:00
|
|
|
IO::out8(0x226, 0);
|
|
|
|
|
|
|
|
auto data = dsp_read();
|
|
|
|
if (data != 0xaa) {
|
2020-03-01 22:45:39 +03:00
|
|
|
klog() << "SB16: sb not ready";
|
2019-07-12 20:28:01 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the version info
|
|
|
|
dsp_write(0xe1);
|
|
|
|
m_major_version = dsp_read();
|
|
|
|
auto vmin = dsp_read();
|
|
|
|
|
2020-03-01 22:45:39 +03:00
|
|
|
klog() << "SB16: found version " << m_major_version << "." << vmin;
|
2020-02-22 21:19:13 +03:00
|
|
|
set_irq_register(SB16_DEFAULT_IRQ);
|
2020-03-01 22:45:39 +03:00
|
|
|
klog() << "SB16: IRQ " << get_irq_line();
|
2020-02-22 21:19:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SB16::set_irq_register(u8 irq_number)
|
|
|
|
{
|
|
|
|
u8 bitmask;
|
|
|
|
switch (irq_number) {
|
|
|
|
case 2:
|
|
|
|
bitmask = 0;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
bitmask = 0b10;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
bitmask = 0b100;
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
bitmask = 0b1000;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
IO::out8(0x224, 0x80);
|
|
|
|
IO::out8(0x225, bitmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 SB16::get_irq_line()
|
|
|
|
{
|
|
|
|
IO::out8(0x224, 0x80);
|
|
|
|
u8 bitmask = IO::in8(0x225);
|
|
|
|
switch (bitmask) {
|
|
|
|
case 0:
|
|
|
|
return 2;
|
|
|
|
case 0b10:
|
|
|
|
return 5;
|
|
|
|
case 0b100:
|
|
|
|
return 7;
|
|
|
|
case 0b1000:
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
return bitmask;
|
|
|
|
}
|
|
|
|
void SB16::set_irq_line(u8 irq_number)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
if (irq_number == get_irq_line())
|
|
|
|
return;
|
|
|
|
set_irq_register(irq_number);
|
|
|
|
change_irq_number(irq_number);
|
2019-07-12 20:28:01 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:44:42 +03:00
|
|
|
bool SB16::can_read(const FileDescription&, size_t) const
|
2019-07-12 20:28:01 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
KResultOr<size_t> SB16::read(FileDescription&, size_t, UserOrKernelBuffer&, size_t)
|
2019-07-12 20:28:01 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SB16::dma_start(uint32_t length)
|
|
|
|
{
|
2020-04-28 17:19:50 +03:00
|
|
|
const auto addr = m_dma_region->physical_page(0)->paddr().get();
|
2019-07-13 18:08:45 +03:00
|
|
|
const u8 channel = 5; // 16-bit samples use DMA channel 5 (on the master DMA controller)
|
2020-05-31 04:38:54 +03:00
|
|
|
const u8 mode = 0x48;
|
2019-07-12 20:28:01 +03:00
|
|
|
|
|
|
|
// Disable the DMA channel
|
2019-07-13 18:08:45 +03:00
|
|
|
IO::out8(0xd4, 4 + (channel % 4));
|
2019-07-12 20:28:01 +03:00
|
|
|
|
|
|
|
// Clear the byte pointer flip-flop
|
2019-07-13 18:08:45 +03:00
|
|
|
IO::out8(0xd8, 0);
|
2019-07-12 20:28:01 +03:00
|
|
|
|
|
|
|
// Write the DMA mode for the transfer
|
2019-07-13 18:08:45 +03:00
|
|
|
IO::out8(0xd6, (channel % 4) | mode);
|
2019-07-12 20:28:01 +03:00
|
|
|
|
|
|
|
// Write the offset of the buffer
|
2019-07-13 22:28:35 +03:00
|
|
|
u16 offset = (addr / 2) % 65536;
|
|
|
|
IO::out8(0xc4, (u8)offset);
|
|
|
|
IO::out8(0xc4, (u8)(offset >> 8));
|
2019-07-12 20:28:01 +03:00
|
|
|
|
|
|
|
// Write the transfer length
|
2019-07-13 18:08:45 +03:00
|
|
|
IO::out8(0xc6, (u8)(length - 1));
|
|
|
|
IO::out8(0xc6, (u8)((length - 1) >> 8));
|
2019-07-12 20:28:01 +03:00
|
|
|
|
|
|
|
// Write the buffer
|
2019-07-13 18:08:45 +03:00
|
|
|
IO::out8(0x8b, addr >> 16);
|
2019-07-12 20:28:01 +03:00
|
|
|
|
|
|
|
// Enable the DMA channel
|
2019-07-13 18:08:45 +03:00
|
|
|
IO::out8(0xd4, (channel % 4));
|
2019-07-12 20:28:01 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 17:24:29 +03:00
|
|
|
void SB16::handle_irq(const RegisterState&)
|
2019-07-14 16:02:02 +03:00
|
|
|
{
|
|
|
|
// Stop sound output ready for the next block.
|
|
|
|
dsp_write(0xd5);
|
|
|
|
|
|
|
|
IO::in8(DSP_STATUS); // 8 bit interrupt
|
|
|
|
if (m_major_version >= 4)
|
|
|
|
IO::in8(DSP_R_ACK); // 16 bit interrupt
|
|
|
|
|
2019-12-01 14:53:19 +03:00
|
|
|
m_irq_queue.wake_all();
|
2019-07-14 16:02:02 +03:00
|
|
|
}
|
|
|
|
|
2019-07-12 20:28:01 +03:00
|
|
|
void SB16::wait_for_irq()
|
|
|
|
{
|
2021-02-15 02:02:14 +03:00
|
|
|
m_irq_queue.wait_forever("SB16");
|
2020-01-23 00:23:50 +03:00
|
|
|
disable_irq();
|
2019-07-12 20:28:01 +03:00
|
|
|
}
|
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
KResultOr<size_t> SB16::write(FileDescription&, size_t, const UserOrKernelBuffer& data, size_t length)
|
2019-07-12 20:28:01 +03:00
|
|
|
{
|
2020-01-20 15:07:29 +03:00
|
|
|
if (!m_dma_region) {
|
|
|
|
auto page = MM.allocate_supervisor_physical_page();
|
2020-09-16 20:47:47 +03:00
|
|
|
if (!page)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOMEM;
|
2020-01-20 15:07:29 +03:00
|
|
|
auto vmobject = AnonymousVMObject::create_with_physical_page(*page);
|
|
|
|
m_dma_region = MM.allocate_kernel_region_with_vmobject(*vmobject, PAGE_SIZE, "SB16 DMA buffer", Region::Access::Write);
|
2020-09-16 20:47:47 +03:00
|
|
|
if (!m_dma_region)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOMEM;
|
2020-01-20 15:07:29 +03:00
|
|
|
}
|
2019-07-12 20:28:01 +03:00
|
|
|
|
2021-01-24 17:28:26 +03:00
|
|
|
#if SB16_DEBUG
|
2020-03-01 22:45:39 +03:00
|
|
|
klog() << "SB16: Writing buffer of " << length << " bytes";
|
2019-07-13 18:55:39 +03:00
|
|
|
#endif
|
2019-07-13 18:08:45 +03:00
|
|
|
ASSERT(length <= PAGE_SIZE);
|
2019-07-12 20:28:01 +03:00
|
|
|
const int BLOCK_SIZE = 32 * 1024;
|
|
|
|
if (length > BLOCK_SIZE) {
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOSPC;
|
2019-07-12 20:28:01 +03:00
|
|
|
}
|
2019-07-13 18:08:45 +03:00
|
|
|
|
2019-07-13 18:51:18 +03:00
|
|
|
u8 mode = (u8)SampleFormat::Signed | (u8)SampleFormat::Stereo;
|
2019-07-12 20:28:01 +03:00
|
|
|
|
|
|
|
const int sample_rate = 44100;
|
|
|
|
set_sample_rate(sample_rate);
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!data.read(m_dma_region->vaddr().as_ptr(), length))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2019-07-13 18:08:45 +03:00
|
|
|
dma_start(length);
|
2019-07-12 20:28:01 +03:00
|
|
|
|
2019-07-13 22:28:35 +03:00
|
|
|
// 16-bit single-cycle output.
|
|
|
|
// FIXME: Implement auto-initialized output.
|
|
|
|
u8 command = 0xb0;
|
2019-07-12 20:28:01 +03:00
|
|
|
|
2019-07-13 18:47:57 +03:00
|
|
|
u16 sample_count = length / sizeof(i16);
|
|
|
|
if (mode & (u8)SampleFormat::Stereo)
|
|
|
|
sample_count /= 2;
|
|
|
|
|
|
|
|
sample_count -= 1;
|
|
|
|
|
2020-03-06 04:22:53 +03:00
|
|
|
cli();
|
|
|
|
enable_irq();
|
|
|
|
|
2019-07-12 20:28:01 +03:00
|
|
|
dsp_write(command);
|
|
|
|
dsp_write(mode);
|
2019-07-13 18:47:57 +03:00
|
|
|
dsp_write((u8)sample_count);
|
|
|
|
dsp_write((u8)(sample_count >> 8));
|
2019-07-12 20:28:01 +03:00
|
|
|
|
|
|
|
wait_for_irq();
|
|
|
|
return length;
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|