IDEController.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  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/OwnPtr.h>
  27. #include <AK/RefPtr.h>
  28. #include <AK/Types.h>
  29. #include <Kernel/Storage/IDEController.h>
  30. #include <Kernel/Storage/PATADiskDevice.h>
  31. namespace Kernel {
  32. NonnullRefPtr<IDEController> IDEController::initialize(PCI::Address address, bool force_pio)
  33. {
  34. return adopt(*new IDEController(address, force_pio));
  35. }
  36. bool IDEController::reset()
  37. {
  38. TODO();
  39. }
  40. bool IDEController::shutdown()
  41. {
  42. TODO();
  43. }
  44. size_t IDEController::devices_count() const
  45. {
  46. size_t count = 0;
  47. for (u32 index = 0; index < 4; index++) {
  48. if (!device(index).is_null())
  49. count++;
  50. }
  51. return count;
  52. }
  53. void IDEController::start_request(const StorageDevice&, AsyncBlockDeviceRequest&)
  54. {
  55. ASSERT_NOT_REACHED();
  56. }
  57. void IDEController::complete_current_request(AsyncDeviceRequest::RequestResult)
  58. {
  59. ASSERT_NOT_REACHED();
  60. }
  61. IDEController::IDEController(PCI::Address address, bool force_pio)
  62. : StorageController()
  63. , PCI::DeviceController(address)
  64. {
  65. initialize(force_pio);
  66. }
  67. IDEController::~IDEController()
  68. {
  69. }
  70. void IDEController::initialize(bool force_pio)
  71. {
  72. auto bus_master_base = IOAddress(PCI::get_BAR4(pci_address()) & (~1));
  73. auto bar0 = PCI::get_BAR0(pci_address());
  74. auto base_io = (bar0 == 0x1 || bar0 == 0) ? IOAddress(0x1F0) : IOAddress(bar0);
  75. auto bar1 = PCI::get_BAR1(pci_address());
  76. auto control_io = (bar1 == 0x1 || bar1 == 0) ? IOAddress(0x3F6) : IOAddress(bar1);
  77. m_channels.append(IDEChannel::create(*this, { base_io, control_io, bus_master_base }, IDEChannel::ChannelType::Primary, force_pio));
  78. auto bar2 = PCI::get_BAR2(pci_address());
  79. base_io = (bar2 == 0x1 || bar2 == 0) ? IOAddress(0x170) : IOAddress(bar2);
  80. auto bar3 = PCI::get_BAR3(pci_address());
  81. control_io = (bar3 == 0x1 || bar3 == 0) ? IOAddress(0x376) : IOAddress(bar3);
  82. m_channels.append(IDEChannel::create(*this, { base_io, control_io, bus_master_base.offset(8) }, IDEChannel::ChannelType::Secondary, force_pio));
  83. }
  84. RefPtr<StorageDevice> IDEController::device_by_channel_and_position(u32 index) const
  85. {
  86. switch (index) {
  87. case 0:
  88. return m_channels[0].master_device();
  89. case 1:
  90. return m_channels[0].slave_device();
  91. case 2:
  92. return m_channels[1].master_device();
  93. case 3:
  94. return m_channels[1].slave_device();
  95. }
  96. ASSERT_NOT_REACHED();
  97. }
  98. RefPtr<StorageDevice> IDEController::device(u32 index) const
  99. {
  100. NonnullRefPtrVector<StorageDevice> connected_devices;
  101. for (size_t index = 0; index < 4; index++) {
  102. auto checked_device = device_by_channel_and_position(index);
  103. if (checked_device.is_null())
  104. continue;
  105. connected_devices.append(checked_device.release_nonnull());
  106. }
  107. if (index >= connected_devices.size())
  108. return nullptr;
  109. return connected_devices[index];
  110. }
  111. }