ide.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. #include <kernel/console.h>
  23. #include <kernel/pci.h>
  24. #include <kernel/mm.h>
  25. #include <kernel/int.h>
  26. #include <kernel/panic.h>
  27. #include <kernel/block.h>
  28. #include <arch/x86/paging.h>
  29. #include <arch/io.h>
  30. #include <lib/string.h>
  31. #include <modules/ide.h>
  32. void (* ide_intr) (void) = NULL;
  33. #define HD0 1
  34. #define HD1 2
  35. int intr_dev = 0;
  36. __io_t hda_base, hda_ctl;
  37. __io_t hdb_base, hdb_ctl;
  38. static char buffer[512] = {0};
  39. __uint64_t hda_nsectors, hdb_nsectors;
  40. errno_t ide_init(struct pci_header0 *hdr) {
  41. __io_t hd0_base, hd0_ctl;
  42. __io_t hd1_base, hd1_ctl;
  43. ide_info("PCI BAR0: \t%q\n", hdr->bar0);
  44. ide_info("PCI BAR1: \t%q\n", hdr->bar1);
  45. ide_info("PCI BAR2: \t%q\n", hdr->bar2);
  46. ide_info("PCI BAR3: \t%q\n", hdr->bar3);
  47. ide_info("PCI BAR4: \t%q\n", hdr->bar4);
  48. ide_info("PCI BAR5: \t%q\n", hdr->bar5);
  49. ide_info("irq nr: \t%b\n", hdr->irqnr);
  50. ide_info("program interface: %q\n", hdr->hdr.prog_if);
  51. if (hdr->hdr.prog_if & PROGIF_HD0MODE) {
  52. hd0_base = hdr->bar0 & PCI_IO_MASK;
  53. hd0_ctl = hdr->bar1 & PCI_IO_MASK;
  54. }
  55. else {
  56. hd0_base = 0x1f0;
  57. hd0_ctl = 0x3f6;
  58. }
  59. if (hdr->hdr.prog_if & PROGIF_HD1MODE) {
  60. hd1_base = hdr->bar2 & PCI_IO_MASK;
  61. hd1_ctl = hdr->bar3 & PCI_IO_MASK;
  62. }
  63. else {
  64. hd1_base = 0x170;
  65. hd1_ctl = 0x376;
  66. }
  67. hda_base = hd0_base;
  68. hda_ctl = hd0_ctl;
  69. hdb_base = hd1_base;
  70. hdb_ctl = hd1_ctl;
  71. ide_info("hda port: %w hda control port: %w\n", hd0_base, hd0_ctl);
  72. ide_info("hdb port: %w hdb control port: %w\n", hd1_base, hd1_ctl);
  73. set_irq(0x0e, do_ide_intr);
  74. set_irq(0x0f, do_ide_intr);
  75. ide_intr = do_ide_id;
  76. intr_dev = HD0;
  77. outb(0x00, hd0_ctl);
  78. outb(0x00, hd0_base + PORT_ERR);
  79. outb(0x00, hd0_base + PORT_NSECTORS);
  80. outb(0x00, hd0_base + PORT_ADDR0);
  81. outb(0x00, hd0_base + PORT_ADDR1);
  82. outb(0x00, hd0_base + PORT_ADDR2);
  83. outb(0x00, hd0_base + PORT_DEV);
  84. outb(ATACMD_ID, hd0_base + PORT_STAT);
  85. while (ide_intr) hlt();
  86. ide_info("ide_init: id completed. \n");
  87. ide_intr = do_ide_id;
  88. intr_dev = HD1;
  89. outb(0x00, hd1_ctl);
  90. outb(0x00, hd1_base + PORT_ERR);
  91. outb(0x00, hd1_base + PORT_NSECTORS);
  92. outb(0x00, hd1_base + PORT_ADDR0);
  93. outb(0x00, hd1_base + PORT_ADDR1);
  94. outb(0x00, hd1_base + PORT_ADDR2);
  95. outb(0x00, hd1_base + PORT_DEV);
  96. outb(ATACMD_ID, hd1_base + PORT_STAT);
  97. while (ide_intr) hlt();
  98. ide_info("ide_init: id completed. \n");
  99. return 0;
  100. }
  101. void do_ide_intr(void) {
  102. if (!ide_intr) {
  103. ide_info("unexcepted ide interrupt. \n");
  104. }
  105. else {
  106. ide_intr();
  107. ide_intr = NULL;
  108. }
  109. return;
  110. }
  111. void do_ide_id(void) {
  112. __io_t dev = intr_dev == HD0 ? hda_base : hdb_base;
  113. __uint16_t buffer[256] = {0};
  114. insw(dev + PORT_DATA, buffer, 256);
  115. ide_info("sectors count: %d. \n", *(__uint64_t *) (buffer + 100));
  116. return;
  117. }
  118. void do_ide_read(void) {
  119. __io_t dev = intr_dev == HD0 ? hda_base : hdb_base;
  120. insw(dev + PORT_DATA, &buffer, 256);
  121. ide_info("read completed. \n");
  122. return;
  123. }
  124. void do_ide_write(void) {
  125. ide_info("write completed. \n");
  126. return;
  127. }
  128. int ide_cmdout(int dev, int cmd, char *buffer, int nsect, __uint64_t lba) {
  129. if (nsect > 255)
  130. panic("ide_cmdout: bad nsect. ");
  131. if (cmd != BLKREAD && cmd != BLKWRITE)
  132. panic("ide_cmdout: bad cmd. ");
  133. intr_dev = dev;
  134. ide_intr = cmd == BLKREAD ? do_ide_read : do_ide_write;
  135. __io_t port = dev == HD0 ? hda_base : hdb_base;
  136. __io_t ctl = dev == HD0 ? hda_ctl : hdb_ctl;
  137. outb(0x00, ctl);
  138. wait_disk_not_busy(port);
  139. outb(0xe0, port + PORT_DEV);
  140. outb(0x00, port + PORT_ERR);
  141. outb(nsect, port + PORT_NSECTORS);
  142. outb(lba & 0xff, port + PORT_ADDR0);
  143. outb((lba >> 8) & 0xff, port + PORT_ADDR1);
  144. outb((lba >> 16) & 0xff, port + PORT_ADDR2);
  145. wait_disk_ready(port);
  146. outb(cmd == BLKREAD ? ATACMD_RD : ATACMD_WR, port + PORT_STAT);
  147. if (cmd == BLKWRITE)
  148. outsw(port + PORT_DATA, buffer, 256);
  149. return 0;
  150. }