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#define OCR_CCS_BITN 30
57
58typedef enum {
59 sd_none = -1,
60 sd_bc = 0,
61 sd_bcr,
62 sd_ac,
63 sd_adtc,
64} sd_cmd_type_t;
65
66typedef struct {
67 uint8_t cmd;
68 uint32_t arg;
69 uint8_t crc;
70} SDRequest;
71
72typedef struct SDState SDState;
73typedef struct SDBus SDBus;
74
75#define TYPE_SD_CARD "sd-card"
76#define SD_CARD(obj) OBJECT_CHECK(SDState, (obj), TYPE_SD_CARD)
77#define SD_CARD_CLASS(klass) \
78 OBJECT_CLASS_CHECK(SDCardClass, (klass), TYPE_SD_CARD)
79#define SD_CARD_GET_CLASS(obj) \
80 OBJECT_GET_CLASS(SDCardClass, (obj), TYPE_SD_CARD)
81
82typedef struct {
83
84 DeviceClass parent_class;
85
86
87 int (*do_command)(SDState *sd, SDRequest *req, uint8_t *response);
88 void (*write_data)(SDState *sd, uint8_t value);
89 uint8_t (*read_data)(SDState *sd);
90 bool (*data_ready)(SDState *sd);
91 void (*enable)(SDState *sd, bool enable);
92 bool (*get_inserted)(SDState *sd);
93 bool (*get_readonly)(SDState *sd);
94} SDCardClass;
95
96#define TYPE_SD_BUS "sd-bus"
97#define SD_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SD_BUS)
98#define SD_BUS_CLASS(klass) OBJECT_CLASS_CHECK(SDBusClass, (klass), TYPE_SD_BUS)
99#define SD_BUS_GET_CLASS(obj) OBJECT_GET_CLASS(SDBusClass, (obj), TYPE_SD_BUS)
100
101struct SDBus {
102 BusState qbus;
103};
104
105typedef struct {
106
107 BusClass parent_class;
108
109
110
111
112
113 void (*set_inserted)(DeviceState *dev, bool inserted);
114 void (*set_readonly)(DeviceState *dev, bool readonly);
115} SDBusClass;
116
117
118SDState *sd_init(BlockBackend *bs, bool is_spi);
119int sd_do_command(SDState *sd, SDRequest *req,
120 uint8_t *response);
121void sd_write_data(SDState *sd, uint8_t value);
122uint8_t sd_read_data(SDState *sd);
123void sd_set_cb(SDState *sd, qemu_irq readonly, qemu_irq insert);
124bool sd_data_ready(SDState *sd);
125
126
127
128
129
130
131
132void sd_enable(SDState *sd, bool enable);
133
134
135
136
137int sdbus_do_command(SDBus *sd, SDRequest *req, uint8_t *response);
138void sdbus_write_data(SDBus *sd, uint8_t value);
139uint8_t sdbus_read_data(SDBus *sd);
140bool sdbus_data_ready(SDBus *sd);
141bool sdbus_get_inserted(SDBus *sd);
142bool sdbus_get_readonly(SDBus *sd);
143
144
145
146
147
148
149
150
151
152
153void sdbus_reparent_card(SDBus *from, SDBus *to);
154
155
156void sdbus_set_inserted(SDBus *sd, bool inserted);
157void sdbus_set_readonly(SDBus *sd, bool inserted);
158
159#endif
160