1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#ifndef HW_SD_H
31#define HW_SD_H
32
33#include "hw/qdev-core.h"
34#include "qom/object.h"
35
36#define OUT_OF_RANGE (1 << 31)
37#define ADDRESS_ERROR (1 << 30)
38#define BLOCK_LEN_ERROR (1 << 29)
39#define ERASE_SEQ_ERROR (1 << 28)
40#define ERASE_PARAM (1 << 27)
41#define WP_VIOLATION (1 << 26)
42#define CARD_IS_LOCKED (1 << 25)
43#define LOCK_UNLOCK_FAILED (1 << 24)
44#define COM_CRC_ERROR (1 << 23)
45#define ILLEGAL_COMMAND (1 << 22)
46#define CARD_ECC_FAILED (1 << 21)
47#define CC_ERROR (1 << 20)
48#define SD_ERROR (1 << 19)
49#define CID_CSD_OVERWRITE (1 << 16)
50#define WP_ERASE_SKIP (1 << 15)
51#define CARD_ECC_DISABLED (1 << 14)
52#define ERASE_RESET (1 << 13)
53#define CURRENT_STATE (7 << 9)
54#define READY_FOR_DATA (1 << 8)
55#define APP_CMD (1 << 5)
56#define AKE_SEQ_ERROR (1 << 3)
57
58enum SDPhySpecificationVersion {
59 SD_PHY_SPECv1_10_VERS = 1,
60 SD_PHY_SPECv2_00_VERS = 2,
61 SD_PHY_SPECv3_01_VERS = 3,
62};
63
64typedef enum {
65 SD_VOLTAGE_0_4V = 400,
66 SD_VOLTAGE_1_8V = 1800,
67 SD_VOLTAGE_3_0V = 3000,
68 SD_VOLTAGE_3_3V = 3300,
69} sd_voltage_mv_t;
70
71typedef enum {
72 UHS_NOT_SUPPORTED = 0,
73 UHS_I = 1,
74 UHS_II = 2,
75 UHS_III = 3,
76} sd_uhs_mode_t;
77
78typedef enum {
79 sd_none = -1,
80 sd_bc = 0,
81 sd_bcr,
82 sd_ac,
83 sd_adtc,
84} sd_cmd_type_t;
85
86typedef struct {
87 uint8_t cmd;
88 uint32_t arg;
89 uint8_t crc;
90} SDRequest;
91
92
93#define TYPE_SD_CARD "sd-card"
94OBJECT_DECLARE_TYPE(SDState, SDCardClass, SD_CARD)
95
96struct SDCardClass {
97
98 DeviceClass parent_class;
99
100
101 int (*do_command)(SDState *sd, SDRequest *req, uint8_t *response);
102
103
104
105
106
107
108
109 void (*write_byte)(SDState *sd, uint8_t value);
110
111
112
113
114
115
116
117
118 uint8_t (*read_byte)(SDState *sd);
119 bool (*receive_ready)(SDState *sd);
120 bool (*data_ready)(SDState *sd);
121 void (*set_voltage)(SDState *sd, uint16_t millivolts);
122 uint8_t (*get_dat_lines)(SDState *sd);
123 bool (*get_cmd_line)(SDState *sd);
124 void (*enable)(SDState *sd, bool enable);
125 bool (*get_inserted)(SDState *sd);
126 bool (*get_readonly)(SDState *sd);
127};
128
129#define TYPE_SD_BUS "sd-bus"
130OBJECT_DECLARE_TYPE(SDBus, SDBusClass,
131 SD_BUS)
132
133struct SDBus {
134 BusState qbus;
135};
136
137struct SDBusClass {
138
139 BusClass parent_class;
140
141
142
143
144
145 void (*set_inserted)(DeviceState *dev, bool inserted);
146 void (*set_readonly)(DeviceState *dev, bool readonly);
147};
148
149
150
151
152void sdbus_set_voltage(SDBus *sdbus, uint16_t millivolts);
153uint8_t sdbus_get_dat_lines(SDBus *sdbus);
154bool sdbus_get_cmd_line(SDBus *sdbus);
155int sdbus_do_command(SDBus *sd, SDRequest *req, uint8_t *response);
156
157
158
159
160
161
162
163void sdbus_write_byte(SDBus *sd, uint8_t value);
164
165
166
167
168
169
170
171
172uint8_t sdbus_read_byte(SDBus *sd);
173
174
175
176
177
178
179
180
181void sdbus_write_data(SDBus *sdbus, const void *buf, size_t length);
182
183
184
185
186
187
188
189
190void sdbus_read_data(SDBus *sdbus, void *buf, size_t length);
191bool sdbus_receive_ready(SDBus *sd);
192bool sdbus_data_ready(SDBus *sd);
193bool sdbus_get_inserted(SDBus *sd);
194bool sdbus_get_readonly(SDBus *sd);
195
196
197
198
199
200
201
202
203
204
205void sdbus_reparent_card(SDBus *from, SDBus *to);
206
207
208void sdbus_set_inserted(SDBus *sd, bool inserted);
209void sdbus_set_readonly(SDBus *sd, bool inserted);
210
211#endif
212