ahci.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. struct ahci_hba_portctl {
  23. __uint32_t cmd_listl;
  24. __uint32_t cmd_listh;
  25. __uint32_t fisl;
  26. __uint32_t fish;
  27. __uint32_t int_status;
  28. __uint32_t int_enable;
  29. __uint32_t cmd;
  30. __uint32_t rsv0;
  31. __uint32_t tfd;
  32. #define SIG_ATA 0x00000101
  33. #define SIG_ATAPI 0xeb140101
  34. #define SIG_SEMB 0xc33c0101
  35. #define SIG_PORTMUL 0x96690101
  36. __uint32_t sig;
  37. __uint32_t status;
  38. __uint32_t ctl;
  39. __uint32_t error;
  40. __uint32_t active;
  41. __uint32_t ci;
  42. __uint32_t sntf;
  43. __uint32_t fbs;
  44. __uint32_t rsv1[11];
  45. __uint32_t vendor[4];
  46. } __attribute__ ((packed));
  47. /* the pcihdr.bar5 points to this */
  48. struct ahci_hba {
  49. /* general host control */
  50. __uint32_t cap0;
  51. __uint32_t ghc;
  52. __uint32_t int_status;
  53. __uint32_t implted_ports;
  54. __uint32_t version;
  55. __uint32_t ccc_ctl, ccc_pts;
  56. __uint32_t em_loc, em_ctl;
  57. __uint32_t cap1;
  58. __uint32_t bohc;
  59. char rsvd[0x74];
  60. /* 1-31 is optional */
  61. struct ahci_hba_portctl portctl[32];
  62. } __attribute__ ((packed));
  63. extern volatile struct ahci_hba *hba;
  64. #define ahci_info(fmt, ...) printk(yellow, "ahci: " fmt, ##__VA_ARGS__)
  65. #define FIS_HOSTTODEV 0x27
  66. #define FIS_DEVTOHOST 0x34
  67. #define FIS_DMA_ACT 0x39
  68. #define FIS_DMA_SETUP 0x41
  69. #define FIS_DATA 0x46
  70. #define FIS_BIST 0x58
  71. #define FIS_PIO_SETUP 0x5f
  72. #define FIS_DEV_BITS 0xa1
  73. #define DEV_NODEV 0x00
  74. #define DEV_SATA 0x01
  75. #define DEV_ATAPI 0x02
  76. #define DEV_PORTMUL 0x03
  77. #define DEV_SEMB 0x04
  78. struct fis_h2d {
  79. __uint8_t fis_type;
  80. __uint8_t pmport : 4;
  81. __uint8_t rsvd0 : 3;
  82. /* cmd / ctrl */
  83. __uint8_t cmd : 1;
  84. __uint8_t command;
  85. __uint8_t feature_low;
  86. __uint8_t lba0, lba1, lba2;
  87. __uint8_t dev;
  88. __uint8_t lba3, lba4, lba5;
  89. __uint8_t feature_high;
  90. __uint8_t count_low;
  91. __uint8_t count_high;
  92. __uint8_t icc;
  93. __uint8_t ctl;
  94. __uint32_t rsvd1;
  95. } __attribute__ ((packed));
  96. struct fis_d2h {
  97. __uint8_t fis_type;
  98. __uint8_t pmport : 4;
  99. __uint8_t rsvd0 : 2;
  100. __uint8_t irq : 1;
  101. __uint8_t rsvd1 : 1;
  102. __uint8_t status;
  103. __uint8_t error;
  104. __uint8_t lba0, lba1, lba2;
  105. __uint8_t dev;
  106. __uint8_t lba3, lba4, lba5;
  107. __uint8_t rsvd2;
  108. __uint8_t count_low, count_high;
  109. __uint16_t rsvd3;
  110. __uint32_t rsvd4;
  111. } __attribute__ ((packed));
  112. struct fis_data {
  113. __uint8_t fis_type;
  114. __uint8_t pmport : 4;
  115. __uint8_t rsvd0 : 4;
  116. __uint16_t rsvd1;
  117. __uint32_t data[0];
  118. } __attribute__ ((packed));
  119. /* the command slot (in the command list) */
  120. struct cmd_header {
  121. /* fis length in __uint32_t */
  122. __uint8_t fis_len : 5;
  123. __uint8_t atapi : 1;
  124. __uint8_t wr : 1;
  125. __uint8_t pre : 1;
  126. __uint8_t rst : 1;
  127. __uint8_t bist : 1;
  128. __uint8_t clear : 1;
  129. __uint8_t rsvd0 : 1;
  130. __uint8_t pmport : 4;
  131. /* prdt length (in entries) */
  132. __uint16_t prdt_len;
  133. __uint32_t bytes_transm;
  134. __uint32_t cmd_table_low;
  135. __uint32_t cmd_table_high;
  136. __uint32_t rsvd1[4];
  137. } __attribute__ ((packed));
  138. struct prdt_entry {
  139. __uint32_t buf_low;
  140. __uint32_t buf_high;
  141. __uint32_t rsvd0;
  142. __uint32_t nbytes : 22;
  143. __uint32_t rsvd1 : 9;
  144. __uint32_t irq : 1;
  145. } __attribute__ ((packed));
  146. /* this is points from the cmd_header */
  147. struct cmd_table {
  148. __uint8_t cmd_fis[64];
  149. __uint8_t atapi_cmd[16];
  150. __uint8_t rsvd0[48];
  151. /* max 65536 */
  152. struct prdt_entry prdts[65536];
  153. } __attribute__ ((packed));
  154. void do_ahci_intr(void);
  155. void set_base(volatile struct ahci_hba_portctl *, int);
  156. void ahci_read(volatile struct ahci_hba_portctl *, __uint64_t, __uint16_t, char *);
  157. __uint64_t ahci_buffer;
  158. #define HBA_PORT_CMD_ST 0x0001
  159. #define HBA_PORT_CMD_FRE 0x0010
  160. #define HBA_PORT_CMD_FR 0x4000
  161. #define HBA_PORT_CMD_CR 0x8000
  162. #define ATACMD_RD 0x20
  163. #define ATACMD_RD48 0x24
  164. #define ATACMD_WR 0x30
  165. #define ATACMD_WR48 0x34
  166. #define ATACMD_RDDMA 0xc8
  167. #define ATACMD_RDDMA48 0x25
  168. #define ATACMD_WRDMA 0xca
  169. #define ATACMD_WRDMA48 0x35
  170. #define ATACMD_ID 0xec