8203ab5e6f902880961f7d3236fecfb99e394cb3
[lunaix-os.git] / lunaix-os / includes / hal / ahci / hba.h
1 #ifndef __LUNAIX_HBA_H
2 #define __LUNAIX_HBA_H
3
4 #include <lunaix/types.h>
5
6 #define HBA_RCAP 0
7 #define HBA_RGHC 1
8 #define HBA_RIS 2
9 #define HBA_RPI 3
10 #define HBA_RVER 4
11
12 #define HBA_RPBASE (0x40)
13 #define HBA_RPSIZE (0x80 >> 2)
14 #define HBA_RPxCLB 0
15 #define HBA_RPxFB 2
16 #define HBA_RPxIS 4
17 #define HBA_RPxIE 5
18 #define HBA_RPxCMD 6
19 #define HBA_RPxTFD 8
20 #define HBA_RPxSIG 9
21 #define HBA_RPxSSTS 10
22 #define HBA_RPxSCTL 11
23 #define HBA_RPxSERR 12
24 #define HBA_RPxSACT 13
25 #define HBA_RPxCI 14
26 #define HBA_RPxSNTF 15
27 #define HBA_RPxFBS 16
28
29 #define HBA_PxCMD_FRE (1 << 4)
30 #define HBA_PxCMD_CR (1 << 15)
31 #define HBA_PxCMD_FR (1 << 14)
32 #define HBA_PxCMD_ST (1)
33 #define HBA_PxINTR_DMA (1 << 2)
34 #define HBA_PxINTR_DHR (1)
35 #define HBA_PxINTR_DPS (1 << 5)
36 #define HBA_PxINTR_TFEE (1 << 30)
37 #define HBA_PxINTR_IFE (1 << 27)
38 #define HBA_PxTFD_ERR (1)
39 #define HBA_PxTFD_BSY (1 << 7)
40 #define HBA_PxTFD_DRQ (1 << 3)
41
42 #define HBA_RGHC_ACHI_ENABLE (1 << 31)
43 #define HBA_RGHC_INTR_ENABLE (1 << 1)
44 #define HBA_RGHC_RESET 1
45
46 #define HBA_RPxSSTS_PWR(x) (((x) >> 8) & 0xf)
47 #define HBA_RPxSSTS_IF(x) (((x) >> 4) & 0xf)
48 #define HBA_RPxSSTS_PHYSTATE(x) ((x)&0xf)
49
50 #define hba_clear_reg(reg) (reg) = -1
51
52 #define HBA_DEV_SIG_ATAPI 0xeb140101
53 #define HBA_DEV_SIG_ATA 0x00000101
54
55 #define __HBA_PACKED__ __attribute__((packed))
56
57 typedef unsigned int hba_reg_t;
58
59 #define HBA_CMDH_FIS_LEN(fis_bytes) (((fis_bytes) / 4) & 0x1f)
60 #define HBA_CMDH_ATAPI (1 << 5)
61 #define HBA_CMDH_WRITE (1 << 6)
62 #define HBA_CMDH_PREFETCH (1 << 7)
63 #define HBA_CMDH_R (1 << 8)
64 #define HBA_CMDH_CLR_BUSY (1 << 10)
65 #define HBA_CMDH_PRDT_LEN(entries) (((entries)&0xffff) << 16)
66
67 struct hba_cmdh
68 {
69     uint16_t options;
70     uint16_t prdt_len;
71     uint32_t transferred_size;
72     uint32_t cmd_table_base;
73     uint32_t reserved[5];
74 } __HBA_PACKED__;
75
76 #define HBA_PRDTE_BYTE_CNT(cnt) ((cnt & 0x3FFFFF) | 0x1)
77
78 struct hba_prdte
79 {
80     uint32_t data_base;
81     uint32_t reserved[2];
82     uint32_t byte_count;
83 } __HBA_PACKED__;
84
85 struct hba_cmdt
86 {
87     uint8_t command_fis[64];
88     uint8_t atapi_cmd[16];
89     uint8_t reserved[0x30];
90     struct hba_prdte entries[3];
91 } __HBA_PACKED__;
92
93 #define HBA_DEV_FEXTLBA 1
94 #define HBA_DEV_FATAPI (1 << 1)
95
96 struct hba_port;
97
98 struct hba_device
99 {
100     char serial_num[20];
101     char model[40];
102     uint32_t flags;
103     uint64_t max_lba;
104     uint32_t block_size;
105     uint64_t wwn;
106     uint8_t cbd_size;
107     struct
108     {
109         uint8_t sense_key;
110         uint8_t error;
111         uint8_t status;
112         uint8_t reserve;
113     } last_result;
114     uint32_t alignment_offset;
115     uint32_t block_per_sec;
116     uint32_t capabilities;
117     struct hba_port* port;
118
119     struct
120     {
121         int (*identify)(struct hba_device* dev);
122         int (*read_buffer)(struct hba_device* dev,
123                            uint64_t lba,
124                            void* buffer,
125                            uint32_t size);
126         int (*write_buffer)(struct hba_device* dev,
127                             uint64_t lba,
128                             void* buffer,
129                             uint32_t size);
130     } ops;
131 };
132
133 struct hba_port
134 {
135     volatile hba_reg_t* regs;
136     unsigned int ssts;
137     struct hba_cmdh* cmdlst;
138     void* fis;
139     struct hba_device* device;
140 };
141
142 struct ahci_hba
143 {
144     volatile hba_reg_t* base;
145     unsigned int ports_num;
146     unsigned int ports_bmp;
147     unsigned int cmd_slots;
148     unsigned int version;
149     struct hba_port* ports[32];
150 };
151
152 int
153 hba_prepare_cmd(struct hba_port* port,
154                 struct hba_cmdt** cmdt,
155                 struct hba_cmdh** cmdh,
156                 void* buffer,
157                 unsigned int size);
158
159 #endif /* __LUNAIX_HBA_H */