qemu/include/hw/sd/sd.h
<<
>>
Prefs
   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#ifndef __hw_sd_h
  30#define __hw_sd_h               1
  31
  32#include "hw/qdev.h"
  33
  34#define OUT_OF_RANGE            (1 << 31)
  35#define ADDRESS_ERROR           (1 << 30)
  36#define BLOCK_LEN_ERROR         (1 << 29)
  37#define ERASE_SEQ_ERROR         (1 << 28)
  38#define ERASE_PARAM             (1 << 27)
  39#define WP_VIOLATION            (1 << 26)
  40#define CARD_IS_LOCKED          (1 << 25)
  41#define LOCK_UNLOCK_FAILED      (1 << 24)
  42#define COM_CRC_ERROR           (1 << 23)
  43#define ILLEGAL_COMMAND         (1 << 22)
  44#define CARD_ECC_FAILED         (1 << 21)
  45#define CC_ERROR                (1 << 20)
  46#define SD_ERROR                (1 << 19)
  47#define CID_CSD_OVERWRITE       (1 << 16)
  48#define WP_ERASE_SKIP           (1 << 15)
  49#define CARD_ECC_DISABLED       (1 << 14)
  50#define ERASE_RESET             (1 << 13)
  51#define CURRENT_STATE           (7 << 9)
  52#define READY_FOR_DATA          (1 << 8)
  53#define SWTICH_ERROR            (1 << 7)
  54#define APP_CMD                 (1 << 5)
  55#define AKE_SEQ_ERROR           (1 << 3)
  56#define OCR_CCS_BITN        30
  57
  58#define EXCSD_BUS_WIDTH_OFFSET 183
  59#define BUS_WIDTH_8_MASK    0x4
  60#define BUS_WIDTH_4_MASK    0x2
  61
  62#define SD_TUNING_BLOCK_SIZE    64
  63#define MMC_TUNING_BLOCK_SIZE   128
  64
  65typedef enum {
  66    sd_none = -1,
  67    sd_bc = 0,  /* broadcast -- no response */
  68    sd_bcr,     /* broadcast with response */
  69    sd_ac,      /* addressed -- no data transfer */
  70    sd_adtc,    /* addressed with data transfer */
  71} sd_cmd_type_t;
  72
  73typedef struct {
  74    uint8_t cmd;
  75    uint32_t arg;
  76    uint8_t crc;
  77} SDRequest;
  78
  79#define SD_VOLTAGE_33 33
  80#define SD_VOLTAGE_18 18
  81
  82typedef struct SDState SDState;
  83typedef struct SDBus SDBus;
  84
  85#define TYPE_SD_CARD "sd-card"
  86#define SD_CARD(obj) OBJECT_CHECK(SDState, (obj), TYPE_SD_CARD)
  87#define SD_CARD_CLASS(klass) \
  88    OBJECT_CLASS_CHECK(SDCardClass, (klass), TYPE_SD_CARD)
  89#define SD_CARD_GET_CLASS(obj) \
  90    OBJECT_GET_CLASS(SDCardClass, (obj), TYPE_SD_CARD)
  91
  92typedef struct {
  93    /*< private >*/
  94    DeviceClass parent_class;
  95    /*< public >*/
  96
  97    uint8_t (*get_dat_lines)(SDState *sd);
  98    bool (*get_cmd_line)(SDState *sd);
  99    void (*set_voltage)(SDState *sd, int v);
 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 (*enable)(SDState *sd, bool enable);
 105    bool (*get_inserted)(SDState *sd);
 106    bool (*get_readonly)(SDState *sd);
 107} SDCardClass;
 108
 109#define TYPE_SD_BUS "sd-bus"
 110#define SD_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SD_BUS)
 111#define SD_BUS_CLASS(klass) OBJECT_CLASS_CHECK(SDBusClass, (klass), TYPE_SD_BUS)
 112#define SD_BUS_GET_CLASS(obj) OBJECT_GET_CLASS(SDBusClass, (obj), TYPE_SD_BUS)
 113
 114struct SDBus {
 115    BusState qbus;
 116};
 117
 118typedef struct {
 119    /*< private >*/
 120    BusClass parent_class;
 121    /*< public >*/
 122
 123    /* These methods are called by the SD device to notify the controller
 124     * when the card insertion or readonly status changes
 125     */
 126    void (*set_inserted)(DeviceState *dev, bool inserted);
 127    void (*set_readonly)(DeviceState *dev, bool readonly);
 128} SDBusClass;
 129
 130/* Legacy functions to be used only by non-qdevified callers */
 131SDState *sd_init(BlockBackend *bs, bool is_spi);
 132SDState *mmc_init(BlockBackend *bs);
 133
 134int sd_do_command(SDState *sd, SDRequest *req,
 135                  uint8_t *response);
 136void sd_write_data(SDState *sd, uint8_t value);
 137uint8_t sd_read_data(SDState *sd);
 138void sd_set_cb(SDState *sd, qemu_irq readonly, qemu_irq insert);
 139bool sd_data_ready(SDState *sd);
 140/* sd_enable should not be used -- it is only used on the nseries boards,
 141 * where it is part of a broken implementation of the MMC card slot switch
 142 * (there should be two card slots which are multiplexed to a single MMC
 143 * controller, but instead we model it with one card and controller and
 144 * disable the card when the second slot is selected, so it looks like the
 145 * second slot is always empty).
 146 */
 147void sd_enable(SDState *sd, bool enable);
 148uint8_t sd_get_dat_lines(SDState *sd);
 149bool sd_get_cmd_line(SDState *sd);
 150void sd_set_voltage(SDState *sd, int v);
 151
 152/* Functions to be used by qdevified callers (working via
 153 * an SDBus rather than directly with SDState)
 154 */
 155uint8_t sdbus_get_dat_lines(SDBus *sdbus);
 156bool sdbus_get_cmd_line(SDBus *sdbus);
 157void sdbus_set_voltage(SDBus *sdbus, int v);
 158int sdbus_do_command(SDBus *sd, SDRequest *req, uint8_t *response);
 159void sdbus_write_data(SDBus *sd, uint8_t value);
 160uint8_t sdbus_read_data(SDBus *sd);
 161bool sdbus_data_ready(SDBus *sd);
 162bool sdbus_get_inserted(SDBus *sd);
 163bool sdbus_get_readonly(SDBus *sd);
 164
 165/* Functions to be used by SD devices to report back to qdevified controllers */
 166void sdbus_set_inserted(SDBus *sd, bool inserted);
 167void sdbus_set_readonly(SDBus *sd, bool inserted);
 168
 169#endif  /* __hw_sd_h */
 170