xref: /qemu/include/hw/sd/sd.h (revision bcebf102)
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.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 
57 typedef enum {
58     SD_VOLTAGE_0_4V     = 400,  /* currently not supported */
59     SD_VOLTAGE_1_8V     = 1800,
60     SD_VOLTAGE_3_0V     = 3000,
61     SD_VOLTAGE_3_3V     = 3300,
62 } sd_voltage_mv_t;
63 
64 typedef enum  {
65     UHS_NOT_SUPPORTED   = 0,
66     UHS_I               = 1,
67     UHS_II              = 2,    /* currently not supported */
68     UHS_III             = 3,    /* currently not supported */
69 } sd_uhs_mode_t;
70 
71 typedef enum {
72     sd_none = -1,
73     sd_bc = 0,	/* broadcast -- no response */
74     sd_bcr,	/* broadcast with response */
75     sd_ac,	/* addressed -- no data transfer */
76     sd_adtc,	/* addressed with data transfer */
77 } sd_cmd_type_t;
78 
79 typedef struct {
80     uint8_t cmd;
81     uint32_t arg;
82     uint8_t crc;
83 } SDRequest;
84 
85 typedef struct SDState SDState;
86 typedef struct SDBus SDBus;
87 
88 #define TYPE_SD_CARD "sd-card"
89 #define SD_CARD(obj) OBJECT_CHECK(SDState, (obj), TYPE_SD_CARD)
90 #define SD_CARD_CLASS(klass) \
91     OBJECT_CLASS_CHECK(SDCardClass, (klass), TYPE_SD_CARD)
92 #define SD_CARD_GET_CLASS(obj) \
93     OBJECT_GET_CLASS(SDCardClass, (obj), TYPE_SD_CARD)
94 
95 typedef struct {
96     /*< private >*/
97     DeviceClass parent_class;
98     /*< public >*/
99 
100     int (*do_command)(SDState *sd, SDRequest *req, uint8_t *response);
101     void (*write_data)(SDState *sd, uint8_t value);
102     uint8_t (*read_data)(SDState *sd);
103     bool (*data_ready)(SDState *sd);
104     void (*set_voltage)(SDState *sd, uint16_t millivolts);
105     uint8_t (*get_dat_lines)(SDState *sd);
106     bool (*get_cmd_line)(SDState *sd);
107     void (*enable)(SDState *sd, bool enable);
108     bool (*get_inserted)(SDState *sd);
109     bool (*get_readonly)(SDState *sd);
110 } SDCardClass;
111 
112 #define TYPE_SD_BUS "sd-bus"
113 #define SD_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SD_BUS)
114 #define SD_BUS_CLASS(klass) OBJECT_CLASS_CHECK(SDBusClass, (klass), TYPE_SD_BUS)
115 #define SD_BUS_GET_CLASS(obj) OBJECT_GET_CLASS(SDBusClass, (obj), TYPE_SD_BUS)
116 
117 struct SDBus {
118     BusState qbus;
119 };
120 
121 typedef struct {
122     /*< private >*/
123     BusClass parent_class;
124     /*< public >*/
125 
126     /* These methods are called by the SD device to notify the controller
127      * when the card insertion or readonly status changes
128      */
129     void (*set_inserted)(DeviceState *dev, bool inserted);
130     void (*set_readonly)(DeviceState *dev, bool readonly);
131 } SDBusClass;
132 
133 /* Legacy functions to be used only by non-qdevified callers */
134 SDState *sd_init(BlockBackend *bs, bool is_spi);
135 int sd_do_command(SDState *sd, SDRequest *req,
136                   uint8_t *response);
137 void sd_write_data(SDState *sd, uint8_t value);
138 uint8_t sd_read_data(SDState *sd);
139 void sd_set_cb(SDState *sd, qemu_irq readonly, qemu_irq insert);
140 bool sd_data_ready(SDState *sd);
141 /* sd_enable should not be used -- it is only used on the nseries boards,
142  * where it is part of a broken implementation of the MMC card slot switch
143  * (there should be two card slots which are multiplexed to a single MMC
144  * controller, but instead we model it with one card and controller and
145  * disable the card when the second slot is selected, so it looks like the
146  * second slot is always empty).
147  */
148 void sd_enable(SDState *sd, bool enable);
149 
150 /* Functions to be used by qdevified callers (working via
151  * an SDBus rather than directly with SDState)
152  */
153 void sdbus_set_voltage(SDBus *sdbus, uint16_t millivolts);
154 uint8_t sdbus_get_dat_lines(SDBus *sdbus);
155 bool sdbus_get_cmd_line(SDBus *sdbus);
156 int sdbus_do_command(SDBus *sd, SDRequest *req, uint8_t *response);
157 void sdbus_write_data(SDBus *sd, uint8_t value);
158 uint8_t sdbus_read_data(SDBus *sd);
159 bool sdbus_data_ready(SDBus *sd);
160 bool sdbus_get_inserted(SDBus *sd);
161 bool sdbus_get_readonly(SDBus *sd);
162 /**
163  * sdbus_reparent_card: Reparent an SD card from one controller to another
164  * @from: controller bus to remove card from
165  * @to: controller bus to move card to
166  *
167  * Reparent an SD card, effectively unplugging it from one controller
168  * and inserting it into another. This is useful for SoCs like the
169  * bcm2835 which have two SD controllers and connect a single SD card
170  * to them, selected by the guest reprogramming GPIO line routing.
171  */
172 void sdbus_reparent_card(SDBus *from, SDBus *to);
173 
174 /* Functions to be used by SD devices to report back to qdevified controllers */
175 void sdbus_set_inserted(SDBus *sd, bool inserted);
176 void sdbus_set_readonly(SDBus *sd, bool inserted);
177 
178 #endif /* HW_SD_H */
179