ide.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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/schk.h>
  28. #include <kernel/block.h>
  29. #include <arch/x86/paging.h>
  30. #include <arch/io.h>
  31. #include <lib/string.h>
  32. #include <modules/ide.h>
  33. void (* ide_intr) (void) = NULL;
  34. #define HD0 1
  35. #define HD1 2
  36. int intr_dev = 0;
  37. __io_t hda_base, hda_ctl;
  38. __io_t hdb_base, hdb_ctl;
  39. static char buffer[512] = {0};
  40. __uint64_t hda_nsectors, hdb_nsectors;
  41. errno_t ide_init(struct pci_header0 *hdr) {
  42. schk_init();
  43. __io_t hd0_base, hd0_ctl;
  44. __io_t hd1_base, hd1_ctl;
  45. ide_info("PCI BAR0: \t%q\n", hdr->bar0);
  46. ide_info("PCI BAR1: \t%q\n", hdr->bar1);
  47. ide_info("PCI BAR2: \t%q\n", hdr->bar2);
  48. ide_info("PCI BAR3: \t%q\n", hdr->bar3);
  49. ide_info("PCI BAR4: \t%q\n", hdr->bar4);
  50. ide_info("PCI BAR5: \t%q\n", hdr->bar5);
  51. ide_info("irq nr: \t%b\n", hdr->irqnr);
  52. ide_info("program interface: %q\n", hdr->hdr.prog_if);
  53. if (hdr->hdr.prog_if & PROGIF_HD0MODE) {
  54. hd0_base = hdr->bar0 & PCI_IO_MASK;
  55. hd0_ctl = hdr->bar1 & PCI_IO_MASK;
  56. }
  57. else {
  58. hd0_base = 0x1f0;
  59. hd0_ctl = 0x3f6;
  60. }
  61. if (hdr->hdr.prog_if & PROGIF_HD1MODE) {
  62. hd1_base = hdr->bar2 & PCI_IO_MASK;
  63. hd1_ctl = hdr->bar3 & PCI_IO_MASK;
  64. }
  65. else {
  66. hd1_base = 0x170;
  67. hd1_ctl = 0x376;
  68. }
  69. hda_base = hd0_base;
  70. hda_ctl = hd0_ctl;
  71. hdb_base = hd1_base;
  72. hdb_ctl = hd1_ctl;
  73. ide_info("hda port: %w hda control port: %w\n", hd0_base, hd0_ctl);
  74. ide_info("hdb port: %w hdb control port: %w\n", hd1_base, hd1_ctl);
  75. set_irq(0x0e, do_ide_intr);
  76. set_irq(0x0f, do_ide_intr);
  77. ide_intr = do_ide_id;
  78. intr_dev = HD0;
  79. outb(0x00, hd0_ctl);
  80. outb(0x00, hd0_base + PORT_ERR);
  81. outb(0x00, hd0_base + PORT_NSECTORS);
  82. outb(0x00, hd0_base + PORT_ADDR0);
  83. outb(0x00, hd0_base + PORT_ADDR1);
  84. outb(0x00, hd0_base + PORT_ADDR2);
  85. outb(0x00, hd0_base + PORT_DEV);
  86. outb(ATACMD_ID, hd0_base + PORT_STAT);
  87. while (ide_intr) hlt();
  88. ide_info("ide_init: id completed. \n");
  89. ide_intr = do_ide_id;
  90. intr_dev = HD1;
  91. outb(0x00, hd1_ctl);
  92. outb(0x00, hd1_base + PORT_ERR);
  93. outb(0x00, hd1_base + PORT_NSECTORS);
  94. outb(0x00, hd1_base + PORT_ADDR0);
  95. outb(0x00, hd1_base + PORT_ADDR1);
  96. outb(0x00, hd1_base + PORT_ADDR2);
  97. outb(0x00, hd1_base + PORT_DEV);
  98. outb(ATACMD_ID, hd1_base + PORT_STAT);
  99. while (ide_intr) hlt();
  100. ide_info("ide_init: id completed. \n");
  101. schk_leave();
  102. return 0;
  103. }
  104. void do_ide_intr(void) {
  105. if (!ide_intr) {
  106. ide_info("unexcepted ide interrupt. \n");
  107. }
  108. else {
  109. ide_intr();
  110. ide_intr = NULL;
  111. }
  112. return;
  113. }
  114. void do_ide_id(void) {
  115. __io_t dev = intr_dev == HD0 ? hda_base : hdb_base;
  116. __uint16_t buffer[256] = {0};
  117. insw(dev + PORT_DATA, buffer, 256);
  118. ide_info("sectors count: %d. \n", *(__uint64_t *) (buffer + 100));
  119. return;
  120. }
  121. void do_ide_read(void) {
  122. __io_t dev = intr_dev == HD0 ? hda_base : hdb_base;
  123. insw(dev + PORT_DATA, &buffer, 256);
  124. ide_info("read completed. \n");
  125. return;
  126. }
  127. void do_ide_write(void) {
  128. ide_info("write completed. \n");
  129. return;
  130. }
  131. int ide_cmdout(int dev, int cmd, char *buffer, int nsect, __uint64_t lba) {
  132. if (nsect > 255)
  133. panic("ide_cmdout: bad nsect. ");
  134. if (cmd != BLKREAD && cmd != BLKWRITE)
  135. panic("ide_cmdout: bad cmd. ");
  136. intr_dev = dev;
  137. ide_intr = cmd == BLKREAD ? do_ide_read : do_ide_write;
  138. __io_t port = dev == HD0 ? hda_base : hdb_base;
  139. __io_t ctl = dev == HD0 ? hda_ctl : hdb_ctl;
  140. outb(0x00, ctl);
  141. wait_disk_not_busy(port);
  142. outb(0xe0, port + PORT_DEV);
  143. outb(0x00, port + PORT_ERR);
  144. outb(nsect, port + PORT_NSECTORS);
  145. outb(lba & 0xff, port + PORT_ADDR0);
  146. outb((lba >> 8) & 0xff, port + PORT_ADDR1);
  147. outb((lba >> 16) & 0xff, port + PORT_ADDR2);
  148. wait_disk_ready(port);
  149. outb(cmd == BLKREAD ? ATACMD_RD : ATACMD_WR, port + PORT_STAT);
  150. if (cmd == BLKWRITE)
  151. outsw(port + PORT_DATA, buffer, 256);
  152. return 0;
  153. }