xref: /qemu/include/hw/sd/sd.h (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
1 /*
2  * SD Memory Card emulation.  Mostly correct for MMC too.
3  *
4  * Copyright (c) 2006 Andrzej Zaborowski  <balrog@zabor.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 
58 enum SDPhySpecificationVersion {
59     SD_PHY_SPECv1_10_VERS     = 1,
60     SD_PHY_SPECv2_00_VERS     = 2,
61     SD_PHY_SPECv3_01_VERS     = 3,
62 };
63 
64 typedef enum {
65     SD_VOLTAGE_0_4V     = 400,  /* currently not supported */
66     SD_VOLTAGE_1_8V     = 1800,
67     SD_VOLTAGE_3_0V     = 3000,
68     SD_VOLTAGE_3_3V     = 3300,
69 } sd_voltage_mv_t;
70 
71 typedef enum  {
72     UHS_NOT_SUPPORTED   = 0,
73     UHS_I               = 1,
74     UHS_II              = 2,    /* currently not supported */
75     UHS_III             = 3,    /* currently not supported */
76 } sd_uhs_mode_t;
77 
78 typedef enum {
79     sd_none = -1,
80     sd_bc = 0,	/* broadcast -- no response */
81     sd_bcr,	/* broadcast with response */
82     sd_ac,	/* addressed -- no data transfer */
83     sd_adtc,	/* addressed with data transfer */
84 } sd_cmd_type_t;
85 
86 typedef struct {
87     uint8_t cmd;
88     uint32_t arg;
89     uint8_t crc;
90 } SDRequest;
91 
92 typedef struct SDState SDState;
93 typedef struct SDBus SDBus;
94 
95 #define TYPE_SD_CARD "sd-card"
96 typedef struct SDCardClass SDCardClass;
97 #define SD_CARD(obj) OBJECT_CHECK(SDState, (obj), TYPE_SD_CARD)
98 #define SD_CARD_CLASS(klass) \
99     OBJECT_CLASS_CHECK(SDCardClass, (klass), TYPE_SD_CARD)
100 #define SD_CARD_GET_CLASS(obj) \
101     OBJECT_GET_CLASS(SDCardClass, (obj), TYPE_SD_CARD)
102 
103 struct SDCardClass {
104     /*< private >*/
105     DeviceClass parent_class;
106     /*< public >*/
107 
108     int (*do_command)(SDState *sd, SDRequest *req, uint8_t *response);
109     /**
110      * Write a byte to a SD card.
111      * @sd: card
112      * @value: byte to write
113      *
114      * Write a byte on the data lines of a SD card.
115      */
116     void (*write_byte)(SDState *sd, uint8_t value);
117     /**
118      * Read a byte from a SD card.
119      * @sd: card
120      *
121      * Read a byte from the data lines of a SD card.
122      *
123      * Return: byte value read
124      */
125     uint8_t (*read_byte)(SDState *sd);
126     bool (*data_ready)(SDState *sd);
127     void (*set_voltage)(SDState *sd, uint16_t millivolts);
128     uint8_t (*get_dat_lines)(SDState *sd);
129     bool (*get_cmd_line)(SDState *sd);
130     void (*enable)(SDState *sd, bool enable);
131     bool (*get_inserted)(SDState *sd);
132     bool (*get_readonly)(SDState *sd);
133 };
134 
135 #define TYPE_SD_BUS "sd-bus"
136 typedef struct SDBusClass SDBusClass;
137 #define SD_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SD_BUS)
138 #define SD_BUS_CLASS(klass) OBJECT_CLASS_CHECK(SDBusClass, (klass), TYPE_SD_BUS)
139 #define SD_BUS_GET_CLASS(obj) OBJECT_GET_CLASS(SDBusClass, (obj), TYPE_SD_BUS)
140 
141 struct SDBus {
142     BusState qbus;
143 };
144 
145 struct SDBusClass {
146     /*< private >*/
147     BusClass parent_class;
148     /*< public >*/
149 
150     /* These methods are called by the SD device to notify the controller
151      * when the card insertion or readonly status changes
152      */
153     void (*set_inserted)(DeviceState *dev, bool inserted);
154     void (*set_readonly)(DeviceState *dev, bool readonly);
155 };
156 
157 /* Functions to be used by qdevified callers (working via
158  * an SDBus rather than directly with SDState)
159  */
160 void sdbus_set_voltage(SDBus *sdbus, uint16_t millivolts);
161 uint8_t sdbus_get_dat_lines(SDBus *sdbus);
162 bool sdbus_get_cmd_line(SDBus *sdbus);
163 int sdbus_do_command(SDBus *sd, SDRequest *req, uint8_t *response);
164 /**
165  * Write a byte to a SD bus.
166  * @sd: bus
167  * @value: byte to write
168  *
169  * Write a byte on the data lines of a SD bus.
170  */
171 void sdbus_write_byte(SDBus *sd, uint8_t value);
172 /**
173  * Read a byte from a SD bus.
174  * @sd: bus
175  *
176  * Read a byte from the data lines of a SD bus.
177  *
178  * Return: byte value read
179  */
180 uint8_t sdbus_read_byte(SDBus *sd);
181 /**
182  * Write data to a SD bus.
183  * @sdbus: bus
184  * @buf: data to write
185  * @length: number of bytes to write
186  *
187  * Write multiple bytes of data on the data lines of a SD bus.
188  */
189 void sdbus_write_data(SDBus *sdbus, const void *buf, size_t length);
190 /**
191  * Read data from a SD bus.
192  * @sdbus: bus
193  * @buf: buffer to read data into
194  * @length: number of bytes to read
195  *
196  * Read multiple bytes of data on the data lines of a SD bus.
197  */
198 void sdbus_read_data(SDBus *sdbus, void *buf, size_t length);
199 bool sdbus_data_ready(SDBus *sd);
200 bool sdbus_get_inserted(SDBus *sd);
201 bool sdbus_get_readonly(SDBus *sd);
202 /**
203  * sdbus_reparent_card: Reparent an SD card from one controller to another
204  * @from: controller bus to remove card from
205  * @to: controller bus to move card to
206  *
207  * Reparent an SD card, effectively unplugging it from one controller
208  * and inserting it into another. This is useful for SoCs like the
209  * bcm2835 which have two SD controllers and connect a single SD card
210  * to them, selected by the guest reprogramming GPIO line routing.
211  */
212 void sdbus_reparent_card(SDBus *from, SDBus *to);
213 
214 /* Functions to be used by SD devices to report back to qdevified controllers */
215 void sdbus_set_inserted(SDBus *sd, bool inserted);
216 void sdbus_set_readonly(SDBus *sd, bool inserted);
217 
218 #endif /* HW_SD_H */
219