IDEController.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. void IDEController::start_request(const StorageDevice&, AsyncBlockDeviceRequest&)
  45. {
  46. ASSERT_NOT_REACHED();
  47. }
  48. void IDEController::complete_current_request(AsyncDeviceRequest::RequestResult)
  49. {
  50. ASSERT_NOT_REACHED();
  51. }
  52. IDEController::IDEController(PCI::Address address, bool force_pio)
  53. : StorageController(address)
  54. {
  55. initialize(force_pio);
  56. }
  57. IDEController::~IDEController()
  58. {
  59. }
  60. void IDEController::initialize(bool force_pio)
  61. {
  62. auto bus_master_base = IOAddress(PCI::get_BAR4(pci_address()) & (~1));
  63. auto bar0 = PCI::get_BAR0(pci_address());
  64. auto base_io = (bar0 == 0x1 || bar0 == 0) ? IOAddress(0x1F0) : IOAddress(bar0);
  65. auto bar1 = PCI::get_BAR1(pci_address());
  66. auto control_io = (bar1 == 0x1 || bar1 == 0) ? IOAddress(0x3F6) : IOAddress(bar1);
  67. m_channels.append(IDEChannel::create(*this, { base_io, control_io, bus_master_base }, IDEChannel::ChannelType::Primary, force_pio));
  68. auto bar2 = PCI::get_BAR2(pci_address());
  69. base_io = (bar2 == 0x1 || bar2 == 0) ? IOAddress(0x170) : IOAddress(bar2);
  70. auto bar3 = PCI::get_BAR3(pci_address());
  71. control_io = (bar3 == 0x1 || bar3 == 0) ? IOAddress(0x376) : IOAddress(bar3);
  72. m_channels.append(IDEChannel::create(*this, { base_io, control_io, bus_master_base.offset(8) }, IDEChannel::ChannelType::Secondary, force_pio));
  73. }
  74. RefPtr<StorageDevice> IDEController::device(u32 index)
  75. {
  76. switch (index) {
  77. case 0:
  78. return m_channels[0].master_device();
  79. case 1:
  80. return m_channels[0].slave_device();
  81. case 2:
  82. return m_channels[1].master_device();
  83. case 3:
  84. return m_channels[1].slave_device();
  85. }
  86. return nullptr;
  87. }
  88. }