| 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/core/qdev.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_SPECv2_00_VERS = 2, |
| 60 | SD_PHY_SPECv3_01_VERS = 3, |
| 61 | }; |
| 62 | |
| 63 | typedef enum { |
| 64 | SD_VOLTAGE_0_4V = 400, /* currently not supported */ |
| 65 | SD_VOLTAGE_1_8V = 1800, |
| 66 | SD_VOLTAGE_3_0V = 3000, |
| 67 | SD_VOLTAGE_3_3V = 3300, |
| 68 | } sd_voltage_mv_t; |
| 69 | |
| 70 | typedef enum { |
| 71 | UHS_NOT_SUPPORTED = 0, |
| 72 | UHS_I = 1, |
| 73 | UHS_II = 2, /* currently not supported */ |
| 74 | UHS_III = 3, /* currently not supported */ |
| 75 | } sd_uhs_mode_t; |
| 76 | |
| 77 | typedef struct { |
| 78 | uint8_t cmd; |
| 79 | uint32_t arg; |
| 80 | uint8_t crc; |
| 81 | } SDRequest; |
| 82 | |
| 83 | |
| 84 | #define TYPE_SD_CARD "sd-card" |
| 85 | OBJECT_DECLARE_TYPE(SDState, SDCardClass, SD_CARD) |
| 86 | |
| 87 | #define TYPE_SD_CARD_SPI "sd-card-spi" |
| 88 | DECLARE_INSTANCE_CHECKER(SDState, SD_CARD_SPI, TYPE_SD_CARD_SPI) |
| 89 | |
| 90 | #define TYPE_EMMC "emmc" |
| 91 | DECLARE_INSTANCE_CHECKER(SDState, EMMC, TYPE_EMMC) |
| 92 | |
| 93 | struct SDCardClass { |
| 94 | /*< private >*/ |
| 95 | DeviceClass parent_class; |
| 96 | /*< public >*/ |
| 97 | |
| 98 | /** |
| 99 | * Process a SD command request. |
| 100 | * @sd: card |
| 101 | * @req: command request |
| 102 | * @resp: buffer to receive the command response |
| 103 | * @respsz: size of @resp buffer |
| 104 | * |
| 105 | * Return: size of the response |
| 106 | */ |
| 107 | size_t (*do_command)(SDState *sd, SDRequest *req, |
| 108 | uint8_t *resp, size_t respsz); |
| 109 | /** |
| 110 | * Write data to a SD card. |
| 111 | * @sd: card |
| 112 | * @value: data to write |
| 113 | * @len: length of data |
| 114 | * |
| 115 | * Write data on the data lines of a SD card. May write not all data, in |
| 116 | * which case it should be called again. At least one byte must be consumed. |
| 117 | * |
| 118 | * Return: number of bytes actually written. >= 1 |
| 119 | */ |
| 120 | size_t (*write_data)(SDState *sd, const void *buf, size_t len); |
| 121 | /** |
| 122 | * Read a byte from a SD card. |
| 123 | * @sd: card |
| 124 | * @buf: buffer to receive the data |
| 125 | * @len: size of data to read |
| 126 | * |
| 127 | * Read data from the data lines of a SD card. This may not read all |
| 128 | * requested data, in this case it should be called again with the remaining |
| 129 | * buffer. At least one byte must be read. |
| 130 | * |
| 131 | * Return: number of bytes actually read. >= 1 |
| 132 | */ |
| 133 | size_t (*read_data)(SDState *sd, void* buf, size_t len); |
| 134 | bool (*receive_ready)(SDState *sd); |
| 135 | bool (*data_ready)(SDState *sd); |
| 136 | void (*set_voltage)(SDState *sd, uint16_t millivolts); |
| 137 | uint8_t (*get_dat_lines)(SDState *sd); |
| 138 | bool (*get_cmd_line)(SDState *sd); |
| 139 | bool (*get_inserted)(SDState *sd); |
| 140 | bool (*get_readonly)(SDState *sd); |
| 141 | void (*set_cid)(SDState *sd); |
| 142 | void (*set_csd)(SDState *sd, uint64_t size); |
| 143 | |
| 144 | const struct SDProto *proto; |
| 145 | }; |
| 146 | |
| 147 | #define TYPE_SD_BUS "sd-bus" |
| 148 | OBJECT_DECLARE_TYPE(SDBus, SDBusClass, |
| 149 | SD_BUS) |
| 150 | |
| 151 | struct SDBus { |
| 152 | BusState qbus; |
| 153 | }; |
| 154 | |
| 155 | struct SDBusClass { |
| 156 | /*< private >*/ |
| 157 | BusClass parent_class; |
| 158 | /*< public >*/ |
| 159 | |
| 160 | /* These methods are called by the SD device to notify the controller |
| 161 | * when the card insertion or readonly status changes |
| 162 | */ |
| 163 | void (*set_inserted)(DeviceState *dev, bool inserted); |
| 164 | void (*set_readonly)(DeviceState *dev, bool readonly); |
| 165 | }; |
| 166 | |
| 167 | /* Functions to be used by qdevified callers (working via |
| 168 | * an SDBus rather than directly with SDState) |
| 169 | */ |
| 170 | void sdbus_set_voltage(SDBus *sdbus, uint16_t millivolts); |
| 171 | uint8_t sdbus_get_dat_lines(SDBus *sdbus); |
| 172 | bool sdbus_get_cmd_line(SDBus *sdbus); |
| 173 | /** |
| 174 | * sdbus_do_command: Process a SD command request |
| 175 | * @sd: card |
| 176 | * @req: command request |
| 177 | * @resp: buffer to receive the command response |
| 178 | * @respsz: size of @resp buffer |
| 179 | * |
| 180 | * Return: size of the response |
| 181 | */ |
| 182 | size_t sdbus_do_command(SDBus *sd, SDRequest *req, uint8_t *resp, size_t respsz); |
| 183 | /** |
| 184 | * Write a byte to a SD bus. |
| 185 | * @sd: bus |
| 186 | * @value: byte to write |
| 187 | * |
| 188 | * Write a byte on the data lines of a SD bus. |
| 189 | */ |
| 190 | void sdbus_write_byte(SDBus *sd, uint8_t value); |
| 191 | /** |
| 192 | * Read a byte from a SD bus. |
| 193 | * @sd: bus |
| 194 | * |
| 195 | * Read a byte from the data lines of a SD bus. |
| 196 | * |
| 197 | * Return: byte value read |
| 198 | */ |
| 199 | uint8_t sdbus_read_byte(SDBus *sd); |
| 200 | /** |
| 201 | * Write data to a SD bus. |
| 202 | * @sdbus: bus |
| 203 | * @buf: data to write |
| 204 | * @length: number of bytes to write |
| 205 | * |
| 206 | * Write multiple bytes of data on the data lines of a SD bus. |
| 207 | */ |
| 208 | void sdbus_write_data(SDBus *sdbus, const void *buf, size_t length); |
| 209 | /** |
| 210 | * Read data from a SD bus. |
| 211 | * @sdbus: bus |
| 212 | * @buf: buffer to read data into |
| 213 | * @length: number of bytes to read |
| 214 | * |
| 215 | * Read multiple bytes of data on the data lines of a SD bus. |
| 216 | */ |
| 217 | void sdbus_read_data(SDBus *sdbus, void *buf, size_t length); |
| 218 | bool sdbus_receive_ready(SDBus *sd); |
| 219 | bool sdbus_data_ready(SDBus *sd); |
| 220 | bool sdbus_get_inserted(SDBus *sd); |
| 221 | bool sdbus_get_readonly(SDBus *sd); |
| 222 | /** |
| 223 | * sdbus_reparent_card: Reparent an SD card from one controller to another |
| 224 | * @from: controller bus to remove card from |
| 225 | * @to: controller bus to move card to |
| 226 | * |
| 227 | * Reparent an SD card, effectively unplugging it from one controller |
| 228 | * and inserting it into another. This is useful for SoCs like the |
| 229 | * bcm2835 which have two SD controllers and connect a single SD card |
| 230 | * to them, selected by the guest reprogramming GPIO line routing. |
| 231 | */ |
| 232 | void sdbus_reparent_card(SDBus *from, SDBus *to); |
| 233 | |
| 234 | /* Functions to be used by SD devices to report back to qdevified controllers */ |
| 235 | void sdbus_set_inserted(SDBus *sd, bool inserted); |
| 236 | void sdbus_set_readonly(SDBus *sd, bool inserted); |
| 237 | |
| 238 | #endif /* HW_SD_H */ |