2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-03-12 08:15:46 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-07-28 10:44:01 -03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-08-24 22:35:19 -03:00
|
|
|
#include <AK/Singleton.h>
|
2020-03-23 09:45:10 -03:00
|
|
|
#include <AK/StringView.h>
|
2021-10-01 03:58:50 -03:00
|
|
|
#include <Kernel/Arch/x86/IO.h>
|
2021-11-20 12:23:09 -03:00
|
|
|
#include <Kernel/Bus/PCI/API.h>
|
2021-08-06 05:45:34 -03:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2019-06-07 06:43:58 -03:00
|
|
|
#include <Kernel/Process.h>
|
2021-06-22 12:40:16 -03:00
|
|
|
#include <Kernel/Sections.h>
|
2021-11-13 05:19:31 -03:00
|
|
|
#include <Kernel/Storage/ATA/ATADiskDevice.h>
|
2021-11-20 11:42:00 -03:00
|
|
|
#include <Kernel/Storage/ATA/Definitions.h>
|
2021-11-19 06:52:07 -03:00
|
|
|
#include <Kernel/Storage/ATA/GenericIDE/Channel.h>
|
|
|
|
|
#include <Kernel/Storage/ATA/GenericIDE/Controller.h>
|
2021-02-06 03:36:38 -03:00
|
|
|
#include <Kernel/WorkQueue.h>
|
2018-11-10 12:15:31 -02:00
|
|
|
|
2020-02-15 21:27:42 -03:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2019-07-28 10:44:01 -03:00
|
|
|
#define PATA_PRIMARY_IRQ 14
|
|
|
|
|
#define PATA_SECONDARY_IRQ 15
|
2018-11-10 12:15:31 -02:00
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
UNMAP_AFTER_INIT NonnullRefPtr<IDEChannel> IDEChannel::create(IDEController const& controller, IOAddressGroup io_group, ChannelType type)
|
2018-10-16 09:17:43 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
auto ata_identify_data_buffer = KBuffer::try_create_with_size("ATA Identify Page"sv, 4096, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow).release_value();
|
|
|
|
|
return adopt_ref(*new IDEChannel(controller, io_group, type, move(ata_identify_data_buffer)));
|
2018-10-16 09:17:43 -03:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
UNMAP_AFTER_INIT NonnullRefPtr<IDEChannel> IDEChannel::create(IDEController const& controller, u8 irq, IOAddressGroup io_group, ChannelType type)
|
2021-03-27 15:44:25 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
auto ata_identify_data_buffer = KBuffer::try_create_with_size("ATA Identify Page"sv, 4096, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow).release_value();
|
|
|
|
|
return adopt_ref(*new IDEChannel(controller, irq, io_group, type, move(ata_identify_data_buffer)));
|
2021-03-27 15:44:25 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
StringView IDEChannel::channel_type_string() const
|
2021-11-20 12:23:09 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
if (m_channel_type == ChannelType::Primary)
|
|
|
|
|
return "Primary"sv;
|
|
|
|
|
return "Secondary"sv;
|
2021-11-20 12:23:09 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
bool IDEChannel::select_device_and_wait_until_not_busy(DeviceType device_type, size_t milliseconds_timeout)
|
2021-11-20 12:23:09 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
IO::delay(20);
|
|
|
|
|
u8 slave = device_type == DeviceType::Slave;
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0xA0 | (slave << 4)); // First, we need to select the drive itself
|
|
|
|
|
IO::delay(20);
|
|
|
|
|
size_t time_elapsed = 0;
|
|
|
|
|
while (m_io_group.control_base().in<u8>() & ATA_SR_BSY && time_elapsed <= milliseconds_timeout) {
|
|
|
|
|
IO::delay(1000);
|
|
|
|
|
time_elapsed++;
|
|
|
|
|
}
|
|
|
|
|
return time_elapsed <= milliseconds_timeout;
|
2021-11-20 12:23:09 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::port_phy_reset()
|
2018-10-16 09:17:43 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
MutexLocker locker(m_lock);
|
|
|
|
|
SpinlockLocker hard_locker(m_hard_lock);
|
2021-04-16 12:03:11 -03:00
|
|
|
// reset the channel
|
|
|
|
|
u8 device_control = m_io_group.control_base().in<u8>();
|
|
|
|
|
// Wait 30 milliseconds
|
|
|
|
|
IO::delay(30000);
|
|
|
|
|
m_io_group.control_base().out<u8>(device_control | (1 << 2));
|
|
|
|
|
// Wait 30 milliseconds
|
|
|
|
|
IO::delay(30000);
|
|
|
|
|
m_io_group.control_base().out<u8>(device_control);
|
|
|
|
|
// Wait up to 30 seconds before failing
|
2022-01-11 21:20:30 -03:00
|
|
|
if (!select_device_and_wait_until_not_busy(DeviceType::Master, 30000)) {
|
2021-04-16 12:03:11 -03:00
|
|
|
dbgln("IDEChannel: reset failed, busy flag on master stuck");
|
2021-11-26 14:39:26 -03:00
|
|
|
return Error::from_errno(EBUSY);
|
2021-04-16 12:03:11 -03:00
|
|
|
}
|
|
|
|
|
// Wait up to 30 seconds before failing
|
2022-01-11 21:20:30 -03:00
|
|
|
if (!select_device_and_wait_until_not_busy(DeviceType::Slave, 30000)) {
|
2021-04-16 12:03:11 -03:00
|
|
|
dbgln("IDEChannel: reset failed, busy flag on slave stuck");
|
2021-11-26 14:39:26 -03:00
|
|
|
return Error::from_errno(EBUSY);
|
2021-04-16 12:03:11 -03:00
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
2021-04-16 12:03:11 -03:00
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::allocate_resources_for_pci_ide_controller(Badge<PCIIDEController>, bool force_pio)
|
|
|
|
|
{
|
|
|
|
|
return allocate_resources(force_pio);
|
|
|
|
|
}
|
|
|
|
|
ErrorOr<void> IDEChannel::allocate_resources_for_isa_ide_controller(Badge<ISAIDEController>)
|
|
|
|
|
{
|
|
|
|
|
return allocate_resources(false);
|
|
|
|
|
}
|
2021-02-05 01:51:47 -03:00
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
UNMAP_AFTER_INIT ErrorOr<void> IDEChannel::allocate_resources(bool force_pio)
|
|
|
|
|
{
|
|
|
|
|
dbgln_if(PATA_DEBUG, "IDEChannel: {} IO base: {}", channel_type_string(), m_io_group.io_base());
|
|
|
|
|
dbgln_if(PATA_DEBUG, "IDEChannel: {} control base: {}", channel_type_string(), m_io_group.control_base());
|
|
|
|
|
if (m_io_group.bus_master_base().has_value())
|
|
|
|
|
dbgln_if(PATA_DEBUG, "IDEChannel: {} bus master base: {}", channel_type_string(), m_io_group.bus_master_base().value());
|
|
|
|
|
else
|
|
|
|
|
dbgln_if(PATA_DEBUG, "IDEChannel: {} bus master base disabled", channel_type_string());
|
2021-11-20 12:23:09 -03:00
|
|
|
|
|
|
|
|
if (!force_pio) {
|
|
|
|
|
m_dma_enabled = true;
|
|
|
|
|
VERIFY(m_io_group.bus_master_base().has_value());
|
|
|
|
|
// Let's try to set up DMA transfers.
|
2021-11-26 14:39:26 -03:00
|
|
|
|
|
|
|
|
m_prdt_region = TRY(MM.allocate_dma_buffer_page("IDE PRDT"sv, Memory::Region::Access::ReadWrite, m_prdt_page));
|
|
|
|
|
VERIFY(!m_prdt_page.is_null());
|
|
|
|
|
m_dma_buffer_region = TRY(MM.allocate_dma_buffer_page("IDE DMA region"sv, Memory::Region::Access::ReadWrite, m_dma_buffer_page));
|
|
|
|
|
VERIFY(!m_dma_buffer_page.is_null());
|
2021-11-20 12:23:09 -03:00
|
|
|
|
|
|
|
|
prdt().end_of_table = 0x8000;
|
|
|
|
|
|
|
|
|
|
// clear bus master interrupt status
|
|
|
|
|
m_io_group.bus_master_base().value().offset(2).out<u8>(m_io_group.bus_master_base().value().offset(2).in<u8>() | 4);
|
|
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
return {};
|
2018-10-16 09:17:43 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
UNMAP_AFTER_INIT IDEChannel::IDEChannel(IDEController const& controller, u8 irq, IOAddressGroup io_group, ChannelType type, NonnullOwnPtr<KBuffer> ata_identify_data_buffer)
|
|
|
|
|
: ATAPort(controller, (type == ChannelType::Primary ? 0 : 1), move(ata_identify_data_buffer))
|
|
|
|
|
, IRQHandler(irq)
|
2021-03-27 15:44:25 -03:00
|
|
|
, m_channel_type(type)
|
|
|
|
|
, m_io_group(io_group)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
UNMAP_AFTER_INIT IDEChannel::IDEChannel(IDEController const& controller, IOAddressGroup io_group, ChannelType type, NonnullOwnPtr<KBuffer> ata_identify_data_buffer)
|
|
|
|
|
: ATAPort(controller, (type == ChannelType::Primary ? 0 : 1), move(ata_identify_data_buffer))
|
|
|
|
|
, IRQHandler(type == ChannelType::Primary ? PATA_PRIMARY_IRQ : PATA_SECONDARY_IRQ)
|
2021-03-27 15:44:25 -03:00
|
|
|
, m_channel_type(type)
|
|
|
|
|
, m_io_group(io_group)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 16:15:15 -03:00
|
|
|
UNMAP_AFTER_INIT IDEChannel::~IDEChannel() = default;
|
2019-05-18 23:40:30 -03:00
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
bool IDEChannel::handle_irq(RegisterState const&)
|
2020-03-05 22:22:53 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
auto result = handle_interrupt_after_dma_transaction();
|
|
|
|
|
// FIXME: Propagate errors properly
|
|
|
|
|
VERIFY(!result.is_error());
|
|
|
|
|
return result.release_value();
|
2021-11-20 12:23:09 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::stop_busmastering()
|
2021-11-20 12:23:09 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
|
|
|
|
VERIFY(m_io_group.bus_master_base().has_value());
|
|
|
|
|
m_io_group.bus_master_base().value().out<u8>(0);
|
|
|
|
|
return {};
|
2020-11-02 15:16:01 -03:00
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::start_busmastering(TransactionDirection direction)
|
2020-11-02 15:16:01 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
|
|
|
|
VERIFY(m_io_group.bus_master_base().has_value());
|
|
|
|
|
m_io_group.bus_master_base().value().out<u8>(direction != TransactionDirection::Write ? 0x9 : 0x1);
|
|
|
|
|
return {};
|
2020-03-05 22:22:53 -03:00
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::force_busmastering_status_clean()
|
2018-11-10 12:15:31 -02:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
|
|
|
|
VERIFY(m_io_group.bus_master_base().has_value());
|
|
|
|
|
m_io_group.bus_master_base().value().offset(2).out<u8>(m_io_group.bus_master_base().value().offset(2).in<u8>() | 4);
|
|
|
|
|
return {};
|
2018-11-10 12:15:31 -02:00
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<u8> IDEChannel::busmastering_status()
|
2021-01-29 16:37:40 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(m_io_group.bus_master_base().has_value());
|
|
|
|
|
return m_io_group.bus_master_base().value().offset(2).in<u8>();
|
2021-01-29 16:37:40 -03:00
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::prepare_transaction_with_busmastering(TransactionDirection direction, PhysicalAddress prdt_buffer)
|
2018-11-10 12:15:31 -02:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
|
|
|
|
m_io_group.bus_master_base().value().offset(4).out<u32>(prdt_buffer.get());
|
|
|
|
|
m_io_group.bus_master_base().value().out<u8>(direction != TransactionDirection::Write ? 0x8 : 0);
|
|
|
|
|
// Turn on "Interrupt" and "Error" flag. The error flag should be cleared by hardware.
|
|
|
|
|
m_io_group.bus_master_base().value().offset(2).out<u8>(m_io_group.bus_master_base().value().offset(2).in<u8>() | 0x6);
|
|
|
|
|
return {};
|
2021-11-20 12:23:09 -03:00
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::initiate_transaction(TransactionDirection)
|
2021-11-20 12:23:09 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
|
|
|
|
return {};
|
2018-11-10 12:15:31 -02:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<u8> IDEChannel::task_file_status()
|
2021-11-20 12:23:09 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
|
|
|
|
return m_io_group.control_base().in<u8>();
|
2021-11-20 12:23:09 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<u8> IDEChannel::task_file_error()
|
2018-11-10 12:15:31 -02:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
|
|
|
|
return m_io_group.io_base().offset(ATA_REG_ERROR).in<u8>();
|
2019-07-28 10:44:01 -03:00
|
|
|
}
|
2018-11-10 12:15:31 -02:00
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<bool> IDEChannel::detect_presence_on_selected_device()
|
2021-01-29 16:37:40 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0x55);
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0xAA);
|
|
|
|
|
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0xAA);
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0x55);
|
|
|
|
|
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0x55);
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0xAA);
|
|
|
|
|
|
|
|
|
|
auto nsectors_value = m_io_group.io_base().offset(ATA_REG_SECCOUNT0).in<u8>();
|
|
|
|
|
auto lba0 = m_io_group.io_base().offset(ATA_REG_LBA0).in<u8>();
|
|
|
|
|
|
|
|
|
|
if (lba0 == 0xAA && nsectors_value == 0x55)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
2021-04-16 12:03:11 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::wait_if_busy_until_timeout(size_t timeout_in_milliseconds)
|
2021-04-16 12:03:11 -03:00
|
|
|
{
|
|
|
|
|
size_t time_elapsed = 0;
|
2021-11-26 14:39:26 -03:00
|
|
|
while (m_io_group.control_base().in<u8>() & ATA_SR_BSY && time_elapsed <= timeout_in_milliseconds) {
|
2021-04-16 12:03:11 -03:00
|
|
|
IO::delay(1000);
|
|
|
|
|
time_elapsed++;
|
|
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
if (time_elapsed <= timeout_in_milliseconds)
|
|
|
|
|
return {};
|
|
|
|
|
return Error::from_errno(EBUSY);
|
2021-01-29 16:37:40 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::force_clear_interrupts()
|
2019-07-28 10:44:01 -03:00
|
|
|
{
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
|
|
|
|
|
return {};
|
2019-05-18 22:46:50 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::load_taskfile_into_registers(ATAPort::TaskFile const& task_file, LBAMode lba_mode, size_t completion_timeout_in_milliseconds)
|
2021-01-29 16:37:40 -03:00
|
|
|
{
|
2021-03-27 03:53:59 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(m_hard_lock.is_locked());
|
2021-01-29 16:37:40 -03:00
|
|
|
u8 head = 0;
|
2021-11-26 14:39:26 -03:00
|
|
|
if (lba_mode == LBAMode::FortyEightBit) {
|
2021-01-29 16:37:40 -03:00
|
|
|
head = 0;
|
2021-11-26 14:39:26 -03:00
|
|
|
} else if (lba_mode == LBAMode::TwentyEightBit) {
|
|
|
|
|
head = (task_file.lba_high[0] & 0x0F);
|
2021-01-29 16:37:40 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
// Note: Preserve the selected drive, always use LBA addressing
|
|
|
|
|
auto driver_register = ((m_io_group.io_base().offset(ATA_REG_HDDEVSEL).in<u8>() & (1 << 4)) | (head | (1 << 5) | (1 << 6)));
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(driver_register);
|
|
|
|
|
IO::delay(50);
|
2021-01-29 16:37:40 -03:00
|
|
|
|
|
|
|
|
if (lba_mode == LBAMode::FortyEightBit) {
|
2021-11-26 14:39:26 -03:00
|
|
|
m_io_group.io_base().offset(ATA_REG_SECCOUNT1).out<u8>((task_file.count >> 8) & 0xFF);
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_LBA3).out<u8>(task_file.lba_high[0]);
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_LBA4).out<u8>(task_file.lba_high[1]);
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_LBA5).out<u8>(task_file.lba_high[2]);
|
2021-01-29 16:37:40 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(task_file.count & 0xFF);
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(task_file.lba_low[0]);
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(task_file.lba_low[1]);
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(task_file.lba_low[2]);
|
2021-01-29 16:37:40 -03:00
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
// FIXME: Set a timeout here?
|
|
|
|
|
size_t time_elapsed = 0;
|
2021-01-29 16:37:40 -03:00
|
|
|
for (;;) {
|
2021-11-26 14:39:26 -03:00
|
|
|
if (time_elapsed > completion_timeout_in_milliseconds)
|
|
|
|
|
return Error::from_errno(EBUSY);
|
|
|
|
|
// FIXME: Use task_file_status method
|
Kernel: Restore IDE PIO functionality
This change can be actually seen as two logical changes, the first
change is about to ensure we only read the ATA Status register only
once, because if we read it multiple times, we acknowledge interrupts
unintentionally. To solve this issue, we always use the alternate Status
register and only read the original status register in the IRQ handler.
The second change is how we handle interrupts - if we use DMA, we can
just complete the request and return from the IRQ handler. For PIO mode,
it's more complicated. For PIO write operation, after setting the ATA
registers, we send out the data to IO port, and wait for an interrupt.
For PIO read operation, we set the ATA registers, and wait for an
interrupt to fire, then we just read from the data IO port.
2021-02-01 14:12:08 -03:00
|
|
|
auto status = m_io_group.control_base().in<u8>();
|
2021-01-29 16:37:40 -03:00
|
|
|
if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
|
|
|
|
|
break;
|
2021-11-26 14:39:26 -03:00
|
|
|
IO::delay(1000);
|
|
|
|
|
time_elapsed++;
|
2021-01-29 16:37:40 -03:00
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(task_file.command);
|
|
|
|
|
return {};
|
2020-11-02 15:16:01 -03:00
|
|
|
}
|
2019-05-18 22:46:50 -03:00
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::device_select(size_t device_index)
|
2020-11-02 15:16:01 -03:00
|
|
|
{
|
2021-03-27 03:53:59 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
2021-11-26 14:39:26 -03:00
|
|
|
if (device_index > 1)
|
|
|
|
|
return Error::from_errno(EINVAL);
|
|
|
|
|
IO::delay(20);
|
|
|
|
|
m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0xA0 | ((device_index) << 4));
|
|
|
|
|
IO::delay(20);
|
|
|
|
|
return {};
|
2018-11-10 12:15:31 -02:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::enable_interrupts()
|
2018-11-10 12:15:31 -02:00
|
|
|
{
|
2021-03-27 03:53:59 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
2021-11-26 14:39:26 -03:00
|
|
|
m_io_group.control_base().out<u8>(0);
|
|
|
|
|
m_interrupts_enabled = true;
|
|
|
|
|
return {};
|
2020-11-02 15:16:01 -03:00
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::disable_interrupts()
|
2020-11-02 15:16:01 -03:00
|
|
|
{
|
2021-03-27 03:53:59 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
2021-11-26 14:39:26 -03:00
|
|
|
m_io_group.control_base().out<u8>(1 << 1);
|
|
|
|
|
m_interrupts_enabled = false;
|
|
|
|
|
return {};
|
2019-07-28 10:44:01 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::read_pio_data_to_buffer(UserOrKernelBuffer& buffer, size_t block_offset, size_t words_count)
|
2019-07-28 10:44:01 -03:00
|
|
|
{
|
2021-03-27 03:53:59 -03:00
|
|
|
VERIFY(m_lock.is_locked());
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(words_count == 256);
|
|
|
|
|
for (u32 i = 0; i < 256; ++i) {
|
|
|
|
|
u16 data = m_io_group.io_base().offset(ATA_REG_DATA).in<u16>();
|
|
|
|
|
// FIXME: Don't assume 512 bytes sector
|
|
|
|
|
TRY(buffer.write(&data, block_offset * 512 + (i * 2), 2));
|
2021-11-20 12:23:09 -03:00
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
return {};
|
2021-11-20 12:23:09 -03:00
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
ErrorOr<void> IDEChannel::write_pio_data_from_buffer(UserOrKernelBuffer const& buffer, size_t block_offset, size_t words_count)
|
2021-11-20 12:23:09 -03:00
|
|
|
{
|
|
|
|
|
VERIFY(m_lock.is_locked());
|
2021-11-26 14:39:26 -03:00
|
|
|
VERIFY(words_count == 256);
|
|
|
|
|
for (u32 i = 0; i < 256; ++i) {
|
|
|
|
|
u16 buf;
|
|
|
|
|
// FIXME: Don't assume 512 bytes sector
|
|
|
|
|
TRY(buffer.read(&buf, block_offset * 512 + (i * 2), 2));
|
|
|
|
|
IO::out16(m_io_group.io_base().offset(ATA_REG_DATA).get(), buf);
|
2021-11-20 12:23:09 -03:00
|
|
|
}
|
2021-11-26 14:39:26 -03:00
|
|
|
return {};
|
2018-11-18 11:57:41 -02:00
|
|
|
}
|
2020-02-15 21:27:42 -03:00
|
|
|
}
|