PATAChannel.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 "PATADiskDevice.h"
  27. #include <AK/ByteBuffer.h>
  28. #include <Kernel/Devices/PATAChannel.h>
  29. #include <Kernel/Devices/PIT.h>
  30. #include <Kernel/FileSystem/ProcFS.h>
  31. #include <Kernel/Process.h>
  32. #include <Kernel/VM/MemoryManager.h>
  33. #include <LibBareMetal/IO.h>
  34. namespace Kernel {
  35. #define PATA_PRIMARY_IRQ 14
  36. #define PATA_SECONDARY_IRQ 15
  37. //#define PATA_DEBUG
  38. #define ATA_SR_BSY 0x80
  39. #define ATA_SR_DRDY 0x40
  40. #define ATA_SR_DF 0x20
  41. #define ATA_SR_DSC 0x10
  42. #define ATA_SR_DRQ 0x08
  43. #define ATA_SR_CORR 0x04
  44. #define ATA_SR_IDX 0x02
  45. #define ATA_SR_ERR 0x01
  46. #define ATA_ER_BBK 0x80
  47. #define ATA_ER_UNC 0x40
  48. #define ATA_ER_MC 0x20
  49. #define ATA_ER_IDNF 0x10
  50. #define ATA_ER_MCR 0x08
  51. #define ATA_ER_ABRT 0x04
  52. #define ATA_ER_TK0NF 0x02
  53. #define ATA_ER_AMNF 0x01
  54. #define ATA_CMD_READ_PIO 0x20
  55. #define ATA_CMD_READ_PIO_EXT 0x24
  56. #define ATA_CMD_READ_DMA 0xC8
  57. #define ATA_CMD_READ_DMA_EXT 0x25
  58. #define ATA_CMD_WRITE_PIO 0x30
  59. #define ATA_CMD_WRITE_PIO_EXT 0x34
  60. #define ATA_CMD_WRITE_DMA 0xCA
  61. #define ATA_CMD_WRITE_DMA_EXT 0x35
  62. #define ATA_CMD_CACHE_FLUSH 0xE7
  63. #define ATA_CMD_CACHE_FLUSH_EXT 0xEA
  64. #define ATA_CMD_PACKET 0xA0
  65. #define ATA_CMD_IDENTIFY_PACKET 0xA1
  66. #define ATA_CMD_IDENTIFY 0xEC
  67. #define ATAPI_CMD_READ 0xA8
  68. #define ATAPI_CMD_EJECT 0x1B
  69. #define ATA_IDENT_DEVICETYPE 0
  70. #define ATA_IDENT_CYLINDERS 2
  71. #define ATA_IDENT_HEADS 6
  72. #define ATA_IDENT_SECTORS 12
  73. #define ATA_IDENT_SERIAL 20
  74. #define ATA_IDENT_MODEL 54
  75. #define ATA_IDENT_CAPABILITIES 98
  76. #define ATA_IDENT_FIELDVALID 106
  77. #define ATA_IDENT_MAX_LBA 120
  78. #define ATA_IDENT_COMMANDSETS 164
  79. #define ATA_IDENT_MAX_LBA_EXT 200
  80. #define IDE_ATA 0x00
  81. #define IDE_ATAPI 0x01
  82. #define ATA_REG_DATA 0x00
  83. #define ATA_REG_ERROR 0x01
  84. #define ATA_REG_FEATURES 0x01
  85. #define ATA_REG_SECCOUNT0 0x02
  86. #define ATA_REG_LBA0 0x03
  87. #define ATA_REG_LBA1 0x04
  88. #define ATA_REG_LBA2 0x05
  89. #define ATA_REG_HDDEVSEL 0x06
  90. #define ATA_REG_COMMAND 0x07
  91. #define ATA_REG_STATUS 0x07
  92. #define ATA_CTL_CONTROL 0x00
  93. #define ATA_CTL_ALTSTATUS 0x00
  94. #define ATA_CTL_DEVADDRESS 0x01
  95. #define PCI_Mass_Storage_Class 0x1
  96. #define PCI_IDE_Controller_Subclass 0x1
  97. static Lock& s_lock()
  98. {
  99. static Lock* lock;
  100. if (!lock)
  101. lock = new Lock;
  102. return *lock;
  103. };
  104. OwnPtr<PATAChannel> PATAChannel::create(ChannelType type, bool force_pio)
  105. {
  106. PCI::Address pci_address;
  107. PCI::enumerate_all([&](const PCI::Address& address, PCI::ID id) {
  108. if (PCI::get_class(address) == PCI_Mass_Storage_Class && PCI::get_subclass(address) == PCI_IDE_Controller_Subclass) {
  109. pci_address = address;
  110. klog() << "PATAChannel: PATA Controller found! id=" << String::format("%w", id.vendor_id) << ":" << String::format("%w", id.device_id);
  111. }
  112. });
  113. return make<PATAChannel>(pci_address, type, force_pio);
  114. }
  115. PATAChannel::PATAChannel(PCI::Address address, ChannelType type, bool force_pio)
  116. : PCI::Device(address, (type == ChannelType::Primary ? PATA_PRIMARY_IRQ : PATA_SECONDARY_IRQ))
  117. , m_channel_number((type == ChannelType::Primary ? 0 : 1))
  118. , m_io_base((type == ChannelType::Primary ? 0x1F0 : 0x170))
  119. , m_control_base((type == ChannelType::Primary ? 0x3f6 : 0x376))
  120. , m_bus_master_base(PCI::get_BAR4(pci_address()) & 0xfffc)
  121. {
  122. disable_irq();
  123. m_dma_enabled.resource() = true;
  124. ProcFS::add_sys_bool("ide_dma", m_dma_enabled);
  125. m_prdt_page = MM.allocate_supervisor_physical_page();
  126. initialize(force_pio);
  127. detect_disks();
  128. }
  129. PATAChannel::~PATAChannel()
  130. {
  131. }
  132. void PATAChannel::initialize(bool force_pio)
  133. {
  134. if (force_pio) {
  135. klog() << "PATAChannel: Requested to force PIO mode; not setting up DMA";
  136. return;
  137. }
  138. // Let's try to set up DMA transfers.
  139. PCI::enable_bus_mastering(pci_address());
  140. prdt().end_of_table = 0x8000;
  141. m_dma_buffer_page = MM.allocate_supervisor_physical_page();
  142. klog() << "PATAChannel: Bus master IDE: " << m_bus_master_base;
  143. }
  144. static void print_ide_status(u8 status)
  145. {
  146. klog() << "PATAChannel: print_ide_status: DRQ=" << ((status & ATA_SR_DRQ) != 0) << " BSY=" << ((status & ATA_SR_BSY) != 0) << " DRDY=" << ((status & ATA_SR_DRDY) != 0) << " DSC=" << ((status & ATA_SR_DSC) != 0) << " DF=" << ((status & ATA_SR_DF) != 0) << " CORR=" << ((status & ATA_SR_CORR) != 0) << " IDX=" << ((status & ATA_SR_IDX) != 0) << " ERR=" << ((status & ATA_SR_ERR) != 0);
  147. }
  148. void PATAChannel::wait_for_irq()
  149. {
  150. cli();
  151. enable_irq();
  152. Thread::current->wait_on(m_irq_queue);
  153. disable_irq();
  154. }
  155. void PATAChannel::handle_irq(RegisterState&)
  156. {
  157. u8 status = m_io_base.offset(ATA_REG_STATUS).in<u8>();
  158. if (status & ATA_SR_ERR) {
  159. print_ide_status(status);
  160. m_device_error = m_io_base.offset(ATA_REG_ERROR).in<u8>();
  161. klog() << "PATAChannel: Error " << String::format("%b", m_device_error) << "!";
  162. } else {
  163. m_device_error = 0;
  164. }
  165. #ifdef PATA_DEBUG
  166. klog() << "PATAChannel: interrupt: DRQ=" << ((status & ATA_SR_DRQ) != 0) << " BSY=" << ((status & ATA_SR_BSY) != 0) << " DRDY=" << ((status & ATA_SR_DRDY) != 0);
  167. #endif
  168. m_irq_queue.wake_all();
  169. }
  170. static void io_delay()
  171. {
  172. for (int i = 0; i < 4; ++i)
  173. IO::in8(0x3f6);
  174. }
  175. void PATAChannel::detect_disks()
  176. {
  177. // There are only two possible disks connected to a channel
  178. for (auto i = 0; i < 2; i++) {
  179. m_io_base.offset(ATA_REG_HDDEVSEL).out<u8>(0xA0 | (i << 4)); // First, we need to select the drive itself
  180. // Apparently these need to be 0 before sending IDENTIFY?!
  181. m_io_base.offset(ATA_REG_SECCOUNT0).out<u8>(0x00);
  182. m_io_base.offset(ATA_REG_LBA0).out<u8>(0x00);
  183. m_io_base.offset(ATA_REG_LBA1).out<u8>(0x00);
  184. m_io_base.offset(ATA_REG_LBA2).out<u8>(0x00);
  185. m_io_base.offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_IDENTIFY); // Send the ATA_IDENTIFY command
  186. // Wait for the BSY flag to be reset
  187. while (m_io_base.offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  188. ;
  189. if (m_io_base.offset(ATA_REG_STATUS).in<u8>() == 0x00) {
  190. #ifdef PATA_DEBUG
  191. klog() << "PATAChannel: No " << (i == 0 ? "master" : "slave") << " disk detected!";
  192. #endif
  193. continue;
  194. }
  195. ByteBuffer wbuf = ByteBuffer::create_uninitialized(512);
  196. ByteBuffer bbuf = ByteBuffer::create_uninitialized(512);
  197. u8* b = bbuf.data();
  198. u16* w = (u16*)wbuf.data();
  199. const u16* wbufbase = (u16*)wbuf.data();
  200. for (u32 i = 0; i < 256; ++i) {
  201. u16 data = m_io_base.offset(ATA_REG_DATA).in<u16>();
  202. *(w++) = data;
  203. *(b++) = MSB(data);
  204. *(b++) = LSB(data);
  205. }
  206. // "Unpad" the device name string.
  207. for (u32 i = 93; i > 54 && bbuf[i] == ' '; --i)
  208. bbuf[i] = 0;
  209. u8 cyls = wbufbase[1];
  210. u8 heads = wbufbase[3];
  211. u8 spt = wbufbase[6];
  212. klog() << "PATAChannel: Name=" << ((char*)bbuf.data() + 54) << ", C/H/Spt=" << cyls << "/" << heads << "/" << spt;
  213. int major = (m_channel_number == 0) ? 3 : 4;
  214. if (i == 0) {
  215. m_master = PATADiskDevice::create(*this, PATADiskDevice::DriveType::Master, major, 0);
  216. m_master->set_drive_geometry(cyls, heads, spt);
  217. } else {
  218. m_slave = PATADiskDevice::create(*this, PATADiskDevice::DriveType::Slave, major, 1);
  219. m_slave->set_drive_geometry(cyls, heads, spt);
  220. }
  221. }
  222. }
  223. bool PATAChannel::ata_read_sectors_with_dma(u32 lba, u16 count, u8* outbuf, bool slave_request)
  224. {
  225. LOCKER(s_lock());
  226. #ifdef PATA_DEBUG
  227. dbg() << "PATAChannel::ata_read_sectors_with_dma (" << lba << " x" << count << ") -> " << outbuf;
  228. #endif
  229. prdt().offset = m_dma_buffer_page->paddr();
  230. prdt().size = 512 * count;
  231. ASSERT(prdt().size <= PAGE_SIZE);
  232. // Stop bus master
  233. m_bus_master_base.out<u8>(0);
  234. // Write the PRDT location
  235. m_bus_master_base.offset(4).out(m_prdt_page->paddr().get());
  236. // Turn on "Interrupt" and "Error" flag. The error flag should be cleared by hardware.
  237. m_bus_master_base.offset(2).out<u8>(m_bus_master_base.offset(2).in<u8>() | 0x6);
  238. // Set transfer direction
  239. m_bus_master_base.out<u8>(0x8);
  240. while (m_io_base.offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  241. ;
  242. u8 devsel = 0xe0;
  243. if (slave_request)
  244. devsel |= 0x10;
  245. m_control_base.offset(ATA_CTL_CONTROL).out<u8>(0);
  246. m_io_base.offset(ATA_REG_HDDEVSEL).out<u8>(devsel | (static_cast<u8>(slave_request) << 4));
  247. io_delay();
  248. m_io_base.offset(ATA_REG_FEATURES).out<u8>(0);
  249. m_io_base.offset(ATA_REG_SECCOUNT0).out<u8>(0);
  250. m_io_base.offset(ATA_REG_LBA0).out<u8>(0);
  251. m_io_base.offset(ATA_REG_LBA1).out<u8>(0);
  252. m_io_base.offset(ATA_REG_LBA2).out<u8>(0);
  253. m_io_base.offset(ATA_REG_SECCOUNT0).out<u8>(count);
  254. m_io_base.offset(ATA_REG_LBA0).out<u8>((lba & 0x000000ff) >> 0);
  255. m_io_base.offset(ATA_REG_LBA1).out<u8>((lba & 0x0000ff00) >> 8);
  256. m_io_base.offset(ATA_REG_LBA2).out<u8>((lba & 0x00ff0000) >> 16);
  257. for (;;) {
  258. auto status = m_io_base.offset(ATA_REG_STATUS).in<u8>();
  259. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
  260. break;
  261. }
  262. m_io_base.offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_READ_DMA_EXT);
  263. io_delay();
  264. // Start bus master
  265. m_bus_master_base.out<u8>(0x9);
  266. wait_for_irq();
  267. if (m_device_error)
  268. return false;
  269. memcpy(outbuf, m_dma_buffer_page->paddr().offset(0xc0000000).as_ptr(), 512 * count);
  270. // I read somewhere that this may trigger a cache flush so let's do it.
  271. m_bus_master_base.offset(2).out<u8>(m_bus_master_base.offset(2).in<u8>() | 0x6);
  272. return true;
  273. }
  274. bool PATAChannel::ata_write_sectors_with_dma(u32 lba, u16 count, const u8* inbuf, bool slave_request)
  275. {
  276. LOCKER(s_lock());
  277. #ifdef PATA_DEBUG
  278. dbg() << "PATAChannel::ata_write_sectors_with_dma (" << lba << " x" << count << ") <- " << inbuf;
  279. #endif
  280. prdt().offset = m_dma_buffer_page->paddr();
  281. prdt().size = 512 * count;
  282. memcpy(m_dma_buffer_page->paddr().offset(0xc0000000).as_ptr(), inbuf, 512 * count);
  283. ASSERT(prdt().size <= PAGE_SIZE);
  284. // Stop bus master
  285. m_bus_master_base.out<u8>(0);
  286. // Write the PRDT location
  287. m_bus_master_base.offset(4).out<u32>(m_prdt_page->paddr().get());
  288. // Turn on "Interrupt" and "Error" flag. The error flag should be cleared by hardware.
  289. m_bus_master_base.offset(2).out<u8>(m_bus_master_base.offset(2).in<u8>() | 0x6);
  290. while (m_io_base.offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  291. ;
  292. u8 devsel = 0xe0;
  293. if (slave_request)
  294. devsel |= 0x10;
  295. m_control_base.offset(ATA_CTL_CONTROL).out<u8>(0);
  296. m_io_base.offset(ATA_REG_HDDEVSEL).out<u8>(devsel | (static_cast<u8>(slave_request) << 4));
  297. io_delay();
  298. m_io_base.offset(ATA_REG_FEATURES).out<u8>(0);
  299. m_io_base.offset(ATA_REG_SECCOUNT0).out<u8>(0);
  300. m_io_base.offset(ATA_REG_LBA0).out<u8>(0);
  301. m_io_base.offset(ATA_REG_LBA1).out<u8>(0);
  302. m_io_base.offset(ATA_REG_LBA2).out<u8>(0);
  303. m_io_base.offset(ATA_REG_SECCOUNT0).out<u8>(count);
  304. m_io_base.offset(ATA_REG_LBA0).out<u8>((lba & 0x000000ff) >> 0);
  305. m_io_base.offset(ATA_REG_LBA1).out<u8>((lba & 0x0000ff00) >> 8);
  306. m_io_base.offset(ATA_REG_LBA2).out<u8>((lba & 0x00ff0000) >> 16);
  307. for (;;) {
  308. auto status = m_io_base.offset(ATA_REG_STATUS).in<u8>();
  309. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
  310. break;
  311. }
  312. m_io_base.offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_WRITE_DMA_EXT);
  313. io_delay();
  314. // Start bus master
  315. m_bus_master_base.out<u8>(0x1);
  316. wait_for_irq();
  317. if (m_device_error)
  318. return false;
  319. // I read somewhere that this may trigger a cache flush so let's do it.
  320. m_bus_master_base.offset(2).out<u8>(m_bus_master_base.offset(2).in<u8>() | 0x6);
  321. return true;
  322. }
  323. bool PATAChannel::ata_read_sectors(u32 start_sector, u16 count, u8* outbuf, bool slave_request)
  324. {
  325. ASSERT(count <= 256);
  326. LOCKER(s_lock());
  327. #ifdef PATA_DEBUG
  328. dbg() << "PATAChannel::ata_read_sectors request (" << count << " sector(s) @ " << start_sector << " into " << outbuf << ")";
  329. #endif
  330. while (m_io_base.offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  331. ;
  332. #ifdef PATA_DEBUG
  333. klog() << "PATAChannel: Reading " << count << " sector(s) @ LBA " << start_sector;
  334. #endif
  335. u8 devsel = 0xe0;
  336. if (slave_request)
  337. devsel |= 0x10;
  338. m_io_base.offset(ATA_REG_SECCOUNT0).out<u8>(count == 256 ? 0 : LSB(count));
  339. m_io_base.offset(ATA_REG_LBA0).out<u8>(start_sector & 0xff);
  340. m_io_base.offset(ATA_REG_LBA1).out<u8>((start_sector >> 8) & 0xff);
  341. m_io_base.offset(ATA_REG_LBA2).out<u8>((start_sector >> 16) & 0xff);
  342. m_io_base.offset(ATA_REG_HDDEVSEL).out<u8>(devsel | ((start_sector >> 24) & 0xf));
  343. IO::out8(0x3F6, 0x08);
  344. while (!(m_io_base.offset(ATA_REG_STATUS).in<u8>() & ATA_SR_DRDY))
  345. ;
  346. m_io_base.offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_READ_PIO);
  347. wait_for_irq();
  348. if (m_device_error)
  349. return false;
  350. for (int i = 0; i < count; i++) {
  351. io_delay();
  352. while (m_io_base.offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  353. ;
  354. u8 status = m_io_base.offset(ATA_REG_STATUS).in<u8>();
  355. ASSERT(status & ATA_SR_DRQ);
  356. #ifdef PATA_DEBUG
  357. dbg() << "PATAChannel: Retrieving 512 bytes (part " << i << ") (status=" << String::format("%b", status) << "), outbuf=(" << (inbuf + (512 * i)) << ")...";
  358. #endif
  359. IO::repeated_in16(m_io_base.offset(ATA_REG_DATA).get(), outbuf + (512 * i), 256);
  360. }
  361. return true;
  362. }
  363. bool PATAChannel::ata_write_sectors(u32 start_sector, u16 count, const u8* inbuf, bool slave_request)
  364. {
  365. ASSERT(count <= 256);
  366. LOCKER(s_lock());
  367. #ifdef PATA_DEBUG
  368. klog() << "PATAChannel::ata_write_sectors request (" << count << " sector(s) @ " << start_sector << ")";
  369. #endif
  370. while (m_io_base.offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  371. ;
  372. #ifdef PATA_DEBUG
  373. klog() << "PATAChannel: Writing " << count << " sector(s) @ LBA " << start_sector;
  374. #endif
  375. u8 devsel = 0xe0;
  376. if (slave_request)
  377. devsel |= 0x10;
  378. m_io_base.offset(ATA_REG_SECCOUNT0).out<u8>(count == 256 ? 0 : LSB(count));
  379. m_io_base.offset(ATA_REG_LBA0).out<u8>(start_sector & 0xff);
  380. m_io_base.offset(ATA_REG_LBA1).out<u8>((start_sector >> 8) & 0xff);
  381. m_io_base.offset(ATA_REG_LBA2).out<u8>((start_sector >> 16) & 0xff);
  382. m_io_base.offset(ATA_REG_HDDEVSEL).out<u8>(devsel | ((start_sector >> 24) & 0xf));
  383. IO::out8(0x3F6, 0x08);
  384. while (!(m_io_base.offset(ATA_REG_STATUS).in<u8>() & ATA_SR_DRDY))
  385. ;
  386. m_io_base.offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_WRITE_PIO);
  387. for (int i = 0; i < count; i++) {
  388. io_delay();
  389. while (m_io_base.offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
  390. ;
  391. u8 status = m_io_base.offset(ATA_REG_STATUS).in<u8>();
  392. ASSERT(status & ATA_SR_DRQ);
  393. #ifdef PATA_DEBUG
  394. dbg() << "PATAChannel: Writing 512 bytes (part " << i << ") (status=" << String::format("%b", status) << "), inbuf=(" << (inbuf + (512 * i)) << ")...";
  395. #endif
  396. IO::repeated_out16(m_io_base.offset(ATA_REG_DATA).get(), inbuf + (512 * i), 256);
  397. wait_for_irq();
  398. status = m_io_base.offset(ATA_REG_STATUS).in<u8>();
  399. ASSERT(!(status & ATA_SR_BSY));
  400. }
  401. m_io_base.offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_CACHE_FLUSH);
  402. wait_for_irq();
  403. u8 status = m_io_base.offset(ATA_REG_STATUS).in<u8>();
  404. ASSERT(!(status & ATA_SR_BSY));
  405. return !m_device_error;
  406. }
  407. }