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.h"
34
35#define OUT_OF_RANGE (1 << 31)
36#define ADDRESS_ERROR (1 << 30)
37#define BLOCK_LEN_ERROR (1 << 29)
38#define ERASE_SEQ_ERROR (1 << 28)
39#define ERASE_PARAM (1 << 27)
40#define WP_VIOLATION (1 << 26)
41#define CARD_IS_LOCKED (1 << 25)
42#define LOCK_UNLOCK_FAILED (1 << 24)
43#define COM_CRC_ERROR (1 << 23)
44#define ILLEGAL_COMMAND (1 << 22)
45#define CARD_ECC_FAILED (1 << 21)
46#define CC_ERROR (1 << 20)
47#define SD_ERROR (1 << 19)
48#define CID_CSD_OVERWRITE (1 << 16)
49#define WP_ERASE_SKIP (1 << 15)
50#define CARD_ECC_DISABLED (1 << 14)
51#define ERASE_RESET (1 << 13)
52#define CURRENT_STATE (7 << 9)
53#define READY_FOR_DATA (1 << 8)
54#define APP_CMD (1 << 5)
55#define AKE_SEQ_ERROR (1 << 3)
56
57enum SDPhySpecificationVersion {
58 SD_PHY_SPECv1_10_VERS = 1,
59 SD_PHY_SPECv2_00_VERS = 2,
60 SD_PHY_SPECv3_01_VERS = 3,
61};
62
63typedef enum {
64 SD_VOLTAGE_0_4V = 400,
65 SD_VOLTAGE_1_8V = 1800,
66 SD_VOLTAGE_3_0V = 3000,
67 SD_VOLTAGE_3_3V = 3300,
68} sd_voltage_mv_t;
69
70typedef enum {
71 UHS_NOT_SUPPORTED = 0,
72 UHS_I = 1,
73 UHS_II = 2,
74 UHS_III = 3,
75} sd_uhs_mode_t;
76
77typedef enum {
78 sd_none = -1,
79 sd_bc = 0,
80 sd_bcr,
81 sd_ac,
82 sd_adtc,
83} sd_cmd_type_t;
84
85typedef struct {
86 uint8_t cmd;
87 uint32_t arg;
88 uint8_t crc;
89} SDRequest;
90
91typedef struct SDState SDState;
92typedef struct SDBus SDBus;
93
94#define TYPE_SD_CARD "sd-card"
95#define SD_CARD(obj) OBJECT_CHECK(SDState, (obj), TYPE_SD_CARD)
96#define SD_CARD_CLASS(klass) \
97 OBJECT_CLASS_CHECK(SDCardClass, (klass), TYPE_SD_CARD)
98#define SD_CARD_GET_CLASS(obj) \
99 OBJECT_GET_CLASS(SDCardClass, (obj), TYPE_SD_CARD)
100
101typedef struct {
102
103 DeviceClass parent_class;
104
105
106 int (*do_command)(SDState *sd, SDRequest *req, uint8_t *response);
107 void (*write_data)(SDState *sd, uint8_t value);
108 uint8_t (*read_data)(SDState *sd);
109 bool (*data_ready)(SDState *sd);
110 void (*set_voltage)(SDState *sd, uint16_t millivolts);
111 uint8_t (*get_dat_lines)(SDState *sd);
112 bool (*get_cmd_line)(SDState *sd);
113 void (*enable)(SDState *sd, bool enable);
114 bool (*get_inserted)(SDState *sd);
115 bool (*get_readonly)(SDState *sd);
116} SDCardClass;
117
118#define TYPE_SD_BUS "sd-bus"
119#define SD_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SD_BUS)
120#define SD_BUS_CLASS(klass) OBJECT_CLASS_CHECK(SDBusClass, (klass), TYPE_SD_BUS)
121#define SD_BUS_GET_CLASS(obj) OBJECT_GET_CLASS(SDBusClass, (obj), TYPE_SD_BUS)
122
123struct SDBus {
124 BusState qbus;
125};
126
127typedef struct {
128
129 BusClass parent_class;
130
131
132
133
134
135 void (*set_inserted)(DeviceState *dev, bool inserted);
136 void (*set_readonly)(DeviceState *dev, bool readonly);
137} SDBusClass;
138
139
140SDState *sd_init(BlockBackend *bs, bool is_spi);
141int sd_do_command(SDState *sd, SDRequest *req,
142 uint8_t *response);
143void sd_write_data(SDState *sd, uint8_t value);
144uint8_t sd_read_data(SDState *sd);
145void sd_set_cb(SDState *sd, qemu_irq readonly, qemu_irq insert);
146bool sd_data_ready(SDState *sd);
147
148
149
150
151
152
153
154void sd_enable(SDState *sd, bool enable);
155
156
157
158
159void sdbus_set_voltage(SDBus *sdbus, uint16_t millivolts);
160uint8_t sdbus_get_dat_lines(SDBus *sdbus);
161bool sdbus_get_cmd_line(SDBus *sdbus);
162int sdbus_do_command(SDBus *sd, SDRequest *req, uint8_t *response);
163void sdbus_write_data(SDBus *sd, uint8_t value);
164uint8_t sdbus_read_data(SDBus *sd);
165bool sdbus_data_ready(SDBus *sd);
166bool sdbus_get_inserted(SDBus *sd);
167bool sdbus_get_readonly(SDBus *sd);
168
169
170
171
172
173
174
175
176
177
178void sdbus_reparent_card(SDBus *from, SDBus *to);
179
180
181void sdbus_set_inserted(SDBus *sd, bool inserted);
182void sdbus_set_readonly(SDBus *sd, bool inserted);
183
184#endif
185