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 SWTICH_ERROR (1 << 7)
55#define APP_CMD (1 << 5)
56#define AKE_SEQ_ERROR (1 << 3)
57#define OCR_CCS_BITN 30
58
59#define EXCSD_BUS_WIDTH_OFFSET 183
60#define BUS_WIDTH_8_MASK 0x4
61#define BUS_WIDTH_4_MASK 0x2
62
63#define SD_TUNING_BLOCK_SIZE 64
64#define MMC_TUNING_BLOCK_SIZE 128
65
66typedef enum {
67 sd_none = -1,
68 sd_bc = 0,
69 sd_bcr,
70 sd_ac,
71 sd_adtc,
72} sd_cmd_type_t;
73
74typedef struct {
75 uint8_t cmd;
76 uint32_t arg;
77 uint8_t crc;
78} SDRequest;
79
80#define SD_VOLTAGE_33 33
81#define SD_VOLTAGE_18 18
82
83typedef struct SDState SDState;
84typedef struct SDBus SDBus;
85
86#define TYPE_SD_CARD "sd-card"
87#define SD_CARD(obj) OBJECT_CHECK(SDState, (obj), TYPE_SD_CARD)
88#define SD_CARD_CLASS(klass) \
89 OBJECT_CLASS_CHECK(SDCardClass, (klass), TYPE_SD_CARD)
90#define SD_CARD_GET_CLASS(obj) \
91 OBJECT_GET_CLASS(SDCardClass, (obj), TYPE_SD_CARD)
92
93typedef struct {
94
95 DeviceClass parent_class;
96
97
98 uint8_t (*get_dat_lines)(SDState *sd);
99 bool (*get_cmd_line)(SDState *sd);
100 void (*set_voltage)(SDState *sd, int v);
101 int (*do_command)(SDState *sd, SDRequest *req, uint8_t *response);
102 void (*write_data)(SDState *sd, uint8_t value);
103 uint8_t (*read_data)(SDState *sd);
104 bool (*data_ready)(SDState *sd);
105 void (*enable)(SDState *sd, bool enable);
106 bool (*get_inserted)(SDState *sd);
107 bool (*get_readonly)(SDState *sd);
108} SDCardClass;
109
110#define TYPE_SD_BUS "sd-bus"
111#define SD_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SD_BUS)
112#define SD_BUS_CLASS(klass) OBJECT_CLASS_CHECK(SDBusClass, (klass), TYPE_SD_BUS)
113#define SD_BUS_GET_CLASS(obj) OBJECT_GET_CLASS(SDBusClass, (obj), TYPE_SD_BUS)
114
115struct SDBus {
116 BusState qbus;
117};
118
119typedef struct {
120
121 BusClass parent_class;
122
123
124
125
126
127 void (*set_inserted)(DeviceState *dev, bool inserted);
128 void (*set_readonly)(DeviceState *dev, bool readonly);
129} SDBusClass;
130
131
132SDState *sd_init(BlockBackend *bs, bool is_spi);
133SDState *mmc_init(BlockBackend *bs);
134
135int sd_do_command(SDState *sd, SDRequest *req,
136 uint8_t *response);
137void sd_write_data(SDState *sd, uint8_t value);
138uint8_t sd_read_data(SDState *sd);
139void sd_set_cb(SDState *sd, qemu_irq readonly, qemu_irq insert);
140bool sd_data_ready(SDState *sd);
141
142
143
144
145
146
147
148void sd_enable(SDState *sd, bool enable);
149uint8_t sd_get_dat_lines(SDState *sd);
150bool sd_get_cmd_line(SDState *sd);
151void sd_set_voltage(SDState *sd, int v);
152
153
154
155
156uint8_t sdbus_get_dat_lines(SDBus *sdbus);
157bool sdbus_get_cmd_line(SDBus *sdbus);
158void sdbus_set_voltage(SDBus *sdbus, int v);
159int sdbus_do_command(SDBus *sd, SDRequest *req, uint8_t *response);
160void sdbus_write_data(SDBus *sd, uint8_t value);
161uint8_t sdbus_read_data(SDBus *sd);
162bool sdbus_data_ready(SDBus *sd);
163bool sdbus_get_inserted(SDBus *sd);
164bool sdbus_get_readonly(SDBus *sd);
165
166
167
168
169
170
171
172
173
174
175void sdbus_reparent_card(SDBus *from, SDBus *to);
176
177
178void sdbus_set_inserted(SDBus *sd, bool inserted);
179void sdbus_set_readonly(SDBus *sd, bool inserted);
180
181#endif
182