2007-11-25 00:35:08 +01:00
|
|
|
/*
|
|
|
|
* SSI to SD card adapter.
|
|
|
|
*
|
2009-05-14 23:35:09 +02:00
|
|
|
* Copyright (c) 2007-2009 CodeSourcery.
|
2007-11-25 00:35:08 +01:00
|
|
|
* Written by Paul Brook
|
|
|
|
*
|
2021-01-28 07:30:32 +01:00
|
|
|
* Copyright (c) 2021 Wind River Systems, Inc.
|
|
|
|
* Improved by Bin Meng <bin.meng@windriver.com>
|
|
|
|
*
|
|
|
|
* Validated with U-Boot v2021.01 and Linux v5.10 mmc_spi driver
|
|
|
|
*
|
2011-06-26 04:21:35 +02:00
|
|
|
* This code is licensed under the GNU GPL v2.
|
2012-01-13 17:44:23 +01:00
|
|
|
*
|
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2007-11-25 00:35:08 +01:00
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:30 +01:00
|
|
|
#include "qemu/osdep.h"
|
2012-12-17 18:20:04 +01:00
|
|
|
#include "sysemu/blockdev.h"
|
2016-01-21 15:15:03 +01:00
|
|
|
#include "hw/ssi/ssi.h"
|
2019-08-12 07:23:45 +02:00
|
|
|
#include "migration/vmstate.h"
|
2019-08-12 07:23:51 +02:00
|
|
|
#include "hw/qdev-properties.h"
|
2015-10-08 15:21:01 +02:00
|
|
|
#include "hw/sd/sd.h"
|
2016-07-04 14:06:37 +02:00
|
|
|
#include "qapi/error.h"
|
2021-01-23 11:39:58 +01:00
|
|
|
#include "qemu/crc-ccitt.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2020-09-03 22:43:22 +02:00
|
|
|
#include "qom/object.h"
|
2007-11-25 00:35:08 +01:00
|
|
|
|
|
|
|
//#define DEBUG_SSI_SD 1
|
|
|
|
|
|
|
|
#ifdef DEBUG_SSI_SD
|
2009-05-13 19:53:17 +02:00
|
|
|
#define DPRINTF(fmt, ...) \
|
|
|
|
do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
|
|
|
|
#define BADF(fmt, ...) \
|
|
|
|
do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
|
2007-11-25 00:35:08 +01:00
|
|
|
#else
|
2009-05-13 19:53:17 +02:00
|
|
|
#define DPRINTF(fmt, ...) do {} while(0)
|
|
|
|
#define BADF(fmt, ...) \
|
|
|
|
do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
|
2007-11-25 00:35:08 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef enum {
|
2016-09-22 19:13:08 +02:00
|
|
|
SSI_SD_CMD = 0,
|
2007-11-25 00:35:08 +01:00
|
|
|
SSI_SD_CMDARG,
|
2021-01-23 11:39:54 +01:00
|
|
|
SSI_SD_PREP_RESP,
|
2007-11-25 00:35:08 +01:00
|
|
|
SSI_SD_RESPONSE,
|
2021-01-23 11:39:59 +01:00
|
|
|
SSI_SD_PREP_DATA,
|
2007-11-25 00:35:08 +01:00
|
|
|
SSI_SD_DATA_START,
|
|
|
|
SSI_SD_DATA_READ,
|
2021-01-23 11:39:58 +01:00
|
|
|
SSI_SD_DATA_CRC16,
|
2021-01-28 07:30:31 +01:00
|
|
|
SSI_SD_DATA_WRITE,
|
|
|
|
SSI_SD_SKIP_CRC16,
|
2007-11-25 00:35:08 +01:00
|
|
|
} ssi_sd_mode;
|
|
|
|
|
2020-09-03 22:43:22 +02:00
|
|
|
struct ssi_sd_state {
|
2020-10-12 14:49:55 +02:00
|
|
|
SSIPeripheral ssidev;
|
2016-09-22 19:13:08 +02:00
|
|
|
uint32_t mode;
|
2007-11-25 00:35:08 +01:00
|
|
|
int cmd;
|
|
|
|
uint8_t cmdarg[4];
|
|
|
|
uint8_t response[5];
|
2021-01-23 11:39:58 +01:00
|
|
|
uint16_t crc16;
|
2021-01-28 07:30:27 +01:00
|
|
|
int32_t read_bytes;
|
2021-01-28 07:30:31 +01:00
|
|
|
int32_t write_bytes;
|
2016-09-22 19:13:08 +02:00
|
|
|
int32_t arglen;
|
|
|
|
int32_t response_pos;
|
|
|
|
int32_t stopping;
|
2018-02-22 16:12:52 +01:00
|
|
|
SDBus sdbus;
|
2020-09-03 22:43:22 +02:00
|
|
|
};
|
2007-11-25 00:35:08 +01:00
|
|
|
|
2018-01-16 14:28:12 +01:00
|
|
|
#define TYPE_SSI_SD "ssi-sd"
|
2020-09-16 20:25:19 +02:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(ssi_sd_state, SSI_SD)
|
2018-01-16 14:28:12 +01:00
|
|
|
|
2007-11-25 00:35:08 +01:00
|
|
|
/* State word bits. */
|
|
|
|
#define SSI_SDR_LOCKED 0x0001
|
|
|
|
#define SSI_SDR_WP_ERASE 0x0002
|
|
|
|
#define SSI_SDR_ERROR 0x0004
|
|
|
|
#define SSI_SDR_CC_ERROR 0x0008
|
|
|
|
#define SSI_SDR_ECC_FAILED 0x0010
|
|
|
|
#define SSI_SDR_WP_VIOLATION 0x0020
|
|
|
|
#define SSI_SDR_ERASE_PARAM 0x0040
|
|
|
|
#define SSI_SDR_OUT_OF_RANGE 0x0080
|
|
|
|
#define SSI_SDR_IDLE 0x0100
|
|
|
|
#define SSI_SDR_ERASE_RESET 0x0200
|
|
|
|
#define SSI_SDR_ILLEGAL_COMMAND 0x0400
|
|
|
|
#define SSI_SDR_COM_CRC_ERROR 0x0800
|
|
|
|
#define SSI_SDR_ERASE_SEQ_ERROR 0x1000
|
|
|
|
#define SSI_SDR_ADDRESS_ERROR 0x2000
|
|
|
|
#define SSI_SDR_PARAMETER_ERROR 0x4000
|
|
|
|
|
2021-01-28 07:30:32 +01:00
|
|
|
/* multiple block write */
|
|
|
|
#define SSI_TOKEN_MULTI_WRITE 0xfc
|
|
|
|
/* terminate multiple block write */
|
|
|
|
#define SSI_TOKEN_STOP_TRAN 0xfd
|
2021-01-23 11:40:02 +01:00
|
|
|
/* single block read/write, multiple block read */
|
|
|
|
#define SSI_TOKEN_SINGLE 0xfe
|
|
|
|
|
|
|
|
/* dummy value - don't care */
|
|
|
|
#define SSI_DUMMY 0xff
|
|
|
|
|
2021-01-28 07:30:31 +01:00
|
|
|
/* data accepted */
|
|
|
|
#define DATA_RESPONSE_ACCEPTED 0x05
|
|
|
|
|
2020-10-12 14:49:55 +02:00
|
|
|
static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)
|
2007-11-25 00:35:08 +01:00
|
|
|
{
|
2020-07-03 17:59:46 +02:00
|
|
|
ssi_sd_state *s = SSI_SD(dev);
|
2021-01-28 07:30:32 +01:00
|
|
|
SDRequest request;
|
|
|
|
uint8_t longresp[16];
|
2007-11-25 00:35:08 +01:00
|
|
|
|
2021-01-28 07:30:27 +01:00
|
|
|
/*
|
|
|
|
* Special case: allow CMD12 (STOP TRANSMISSION) while reading data.
|
|
|
|
*
|
|
|
|
* See "Physical Layer Specification Version 8.00" chapter 7.5.2.2,
|
|
|
|
* to avoid conflict between CMD12 response and next data block,
|
|
|
|
* timing of CMD12 should be controlled as follows:
|
|
|
|
*
|
|
|
|
* - CMD12 issued at the timing that end bit of CMD12 and end bit of
|
|
|
|
* data block is overlapped
|
|
|
|
* - CMD12 issued after one clock cycle after host receives a token
|
|
|
|
* (either Start Block token or Data Error token)
|
|
|
|
*
|
|
|
|
* We need to catch CMD12 in all of the data read states.
|
|
|
|
*/
|
|
|
|
if (s->mode >= SSI_SD_PREP_DATA && s->mode <= SSI_SD_DATA_CRC16) {
|
|
|
|
if (val == 0x4c) {
|
|
|
|
s->mode = SSI_SD_CMD;
|
|
|
|
/* There must be at least one byte delay before the card responds */
|
|
|
|
s->stopping = 1;
|
|
|
|
}
|
2007-11-25 00:35:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (s->mode) {
|
|
|
|
case SSI_SD_CMD:
|
2021-01-28 07:30:31 +01:00
|
|
|
switch (val) {
|
|
|
|
case SSI_DUMMY:
|
2007-11-25 00:35:08 +01:00
|
|
|
DPRINTF("NULL command\n");
|
2021-01-23 11:40:02 +01:00
|
|
|
return SSI_DUMMY;
|
2021-01-28 07:30:31 +01:00
|
|
|
break;
|
|
|
|
case SSI_TOKEN_SINGLE:
|
2021-01-28 07:30:32 +01:00
|
|
|
case SSI_TOKEN_MULTI_WRITE:
|
2021-01-28 07:30:31 +01:00
|
|
|
DPRINTF("Start write block\n");
|
|
|
|
s->mode = SSI_SD_DATA_WRITE;
|
2021-01-28 07:30:32 +01:00
|
|
|
return SSI_DUMMY;
|
|
|
|
case SSI_TOKEN_STOP_TRAN:
|
|
|
|
DPRINTF("Stop multiple write\n");
|
|
|
|
|
|
|
|
/* manually issue cmd12 to stop the transfer */
|
|
|
|
request.cmd = 12;
|
|
|
|
request.arg = 0;
|
|
|
|
s->arglen = sdbus_do_command(&s->sdbus, &request, longresp);
|
|
|
|
if (s->arglen <= 0) {
|
|
|
|
s->arglen = 1;
|
|
|
|
/* a zero value indicates the card is busy */
|
|
|
|
s->response[0] = 0;
|
|
|
|
DPRINTF("SD card busy\n");
|
|
|
|
} else {
|
|
|
|
s->arglen = 1;
|
|
|
|
/* a non-zero value indicates the card is ready */
|
|
|
|
s->response[0] = SSI_DUMMY;
|
|
|
|
}
|
|
|
|
|
2021-01-28 07:30:31 +01:00
|
|
|
return SSI_DUMMY;
|
2007-11-25 00:35:08 +01:00
|
|
|
}
|
2021-01-28 07:30:31 +01:00
|
|
|
|
2007-11-25 00:35:08 +01:00
|
|
|
s->cmd = val & 0x3f;
|
|
|
|
s->mode = SSI_SD_CMDARG;
|
|
|
|
s->arglen = 0;
|
2021-01-23 11:40:02 +01:00
|
|
|
return SSI_DUMMY;
|
2007-11-25 00:35:08 +01:00
|
|
|
case SSI_SD_CMDARG:
|
|
|
|
if (s->arglen == 4) {
|
|
|
|
/* FIXME: Check CRC. */
|
|
|
|
request.cmd = s->cmd;
|
2018-06-29 16:11:20 +02:00
|
|
|
request.arg = ldl_be_p(s->cmdarg);
|
2007-11-25 00:35:08 +01:00
|
|
|
DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
|
2018-02-22 16:12:52 +01:00
|
|
|
s->arglen = sdbus_do_command(&s->sdbus, &request, longresp);
|
2007-11-25 00:35:08 +01:00
|
|
|
if (s->arglen <= 0) {
|
|
|
|
s->arglen = 1;
|
|
|
|
s->response[0] = 4;
|
|
|
|
DPRINTF("SD command failed\n");
|
2021-01-28 07:30:33 +01:00
|
|
|
} else if (s->cmd == 8 || s->cmd == 58) {
|
|
|
|
/* CMD8/CMD58 returns R3/R7 response */
|
|
|
|
DPRINTF("Returned R3/R7\n");
|
2007-11-25 00:35:08 +01:00
|
|
|
s->arglen = 5;
|
|
|
|
s->response[0] = 1;
|
|
|
|
memcpy(&s->response[1], longresp, 4);
|
|
|
|
} else if (s->arglen != 4) {
|
|
|
|
BADF("Unexpected response to cmd %d\n", s->cmd);
|
|
|
|
/* Illegal command is about as near as we can get. */
|
|
|
|
s->arglen = 1;
|
|
|
|
s->response[0] = 4;
|
|
|
|
} else {
|
|
|
|
/* All other commands return status. */
|
|
|
|
uint32_t cardstatus;
|
|
|
|
uint16_t status;
|
|
|
|
/* CMD13 returns a 2-byte statuse work. Other commands
|
|
|
|
only return the first byte. */
|
|
|
|
s->arglen = (s->cmd == 13) ? 2 : 1;
|
2021-01-28 07:30:35 +01:00
|
|
|
|
|
|
|
/* handle R1b */
|
|
|
|
if (s->cmd == 28 || s->cmd == 29 || s->cmd == 38) {
|
|
|
|
s->stopping = 1;
|
|
|
|
}
|
|
|
|
|
2018-06-29 16:11:20 +02:00
|
|
|
cardstatus = ldl_be_p(longresp);
|
2007-11-25 00:35:08 +01:00
|
|
|
status = 0;
|
|
|
|
if (((cardstatus >> 9) & 0xf) < 4)
|
|
|
|
status |= SSI_SDR_IDLE;
|
|
|
|
if (cardstatus & ERASE_RESET)
|
|
|
|
status |= SSI_SDR_ERASE_RESET;
|
|
|
|
if (cardstatus & ILLEGAL_COMMAND)
|
|
|
|
status |= SSI_SDR_ILLEGAL_COMMAND;
|
|
|
|
if (cardstatus & COM_CRC_ERROR)
|
|
|
|
status |= SSI_SDR_COM_CRC_ERROR;
|
|
|
|
if (cardstatus & ERASE_SEQ_ERROR)
|
|
|
|
status |= SSI_SDR_ERASE_SEQ_ERROR;
|
|
|
|
if (cardstatus & ADDRESS_ERROR)
|
|
|
|
status |= SSI_SDR_ADDRESS_ERROR;
|
|
|
|
if (cardstatus & CARD_IS_LOCKED)
|
|
|
|
status |= SSI_SDR_LOCKED;
|
|
|
|
if (cardstatus & (LOCK_UNLOCK_FAILED | WP_ERASE_SKIP))
|
|
|
|
status |= SSI_SDR_WP_ERASE;
|
|
|
|
if (cardstatus & SD_ERROR)
|
|
|
|
status |= SSI_SDR_ERROR;
|
|
|
|
if (cardstatus & CC_ERROR)
|
|
|
|
status |= SSI_SDR_CC_ERROR;
|
|
|
|
if (cardstatus & CARD_ECC_FAILED)
|
|
|
|
status |= SSI_SDR_ECC_FAILED;
|
|
|
|
if (cardstatus & WP_VIOLATION)
|
|
|
|
status |= SSI_SDR_WP_VIOLATION;
|
|
|
|
if (cardstatus & ERASE_PARAM)
|
|
|
|
status |= SSI_SDR_ERASE_PARAM;
|
|
|
|
if (cardstatus & (OUT_OF_RANGE | CID_CSD_OVERWRITE))
|
|
|
|
status |= SSI_SDR_OUT_OF_RANGE;
|
|
|
|
/* ??? Don't know what Parameter Error really means, so
|
|
|
|
assume it's set if the second byte is nonzero. */
|
|
|
|
if (status & 0xff)
|
|
|
|
status |= SSI_SDR_PARAMETER_ERROR;
|
|
|
|
s->response[0] = status >> 8;
|
|
|
|
s->response[1] = status;
|
|
|
|
DPRINTF("Card status 0x%02x\n", status);
|
|
|
|
}
|
2021-01-23 11:39:54 +01:00
|
|
|
s->mode = SSI_SD_PREP_RESP;
|
2007-11-25 00:35:08 +01:00
|
|
|
s->response_pos = 0;
|
|
|
|
} else {
|
|
|
|
s->cmdarg[s->arglen++] = val;
|
|
|
|
}
|
2021-01-23 11:40:02 +01:00
|
|
|
return SSI_DUMMY;
|
2021-01-23 11:39:54 +01:00
|
|
|
case SSI_SD_PREP_RESP:
|
|
|
|
DPRINTF("Prepare card response (Ncr)\n");
|
|
|
|
s->mode = SSI_SD_RESPONSE;
|
2021-01-23 11:40:02 +01:00
|
|
|
return SSI_DUMMY;
|
2007-11-25 00:35:08 +01:00
|
|
|
case SSI_SD_RESPONSE:
|
|
|
|
if (s->response_pos < s->arglen) {
|
|
|
|
DPRINTF("Response 0x%02x\n", s->response[s->response_pos]);
|
|
|
|
return s->response[s->response_pos++];
|
|
|
|
}
|
2021-01-28 07:30:34 +01:00
|
|
|
if (s->stopping) {
|
|
|
|
s->stopping = 0;
|
|
|
|
s->mode = SSI_SD_CMD;
|
|
|
|
return SSI_DUMMY;
|
|
|
|
}
|
2018-02-22 16:12:52 +01:00
|
|
|
if (sdbus_data_ready(&s->sdbus)) {
|
2007-11-25 00:35:08 +01:00
|
|
|
DPRINTF("Data read\n");
|
|
|
|
s->mode = SSI_SD_DATA_START;
|
|
|
|
} else {
|
|
|
|
DPRINTF("End of command\n");
|
|
|
|
s->mode = SSI_SD_CMD;
|
|
|
|
}
|
2021-01-23 11:40:02 +01:00
|
|
|
return SSI_DUMMY;
|
2021-01-23 11:39:59 +01:00
|
|
|
case SSI_SD_PREP_DATA:
|
|
|
|
DPRINTF("Prepare data block (Nac)\n");
|
|
|
|
s->mode = SSI_SD_DATA_START;
|
2021-01-23 11:40:02 +01:00
|
|
|
return SSI_DUMMY;
|
2007-11-25 00:35:08 +01:00
|
|
|
case SSI_SD_DATA_START:
|
|
|
|
DPRINTF("Start read block\n");
|
|
|
|
s->mode = SSI_SD_DATA_READ;
|
2021-01-23 11:39:58 +01:00
|
|
|
s->response_pos = 0;
|
2021-01-23 11:40:02 +01:00
|
|
|
return SSI_TOKEN_SINGLE;
|
2007-11-25 00:35:08 +01:00
|
|
|
case SSI_SD_DATA_READ:
|
2020-08-14 11:23:42 +02:00
|
|
|
val = sdbus_read_byte(&s->sdbus);
|
2021-01-28 07:30:27 +01:00
|
|
|
s->read_bytes++;
|
2021-01-23 11:39:58 +01:00
|
|
|
s->crc16 = crc_ccitt_false(s->crc16, (uint8_t *)&val, 1);
|
2021-01-28 07:30:27 +01:00
|
|
|
if (!sdbus_data_ready(&s->sdbus) || s->read_bytes == 512) {
|
2007-11-25 00:35:08 +01:00
|
|
|
DPRINTF("Data read end\n");
|
2021-01-23 11:39:58 +01:00
|
|
|
s->mode = SSI_SD_DATA_CRC16;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
case SSI_SD_DATA_CRC16:
|
|
|
|
val = (s->crc16 & 0xff00) >> 8;
|
|
|
|
s->crc16 <<= 8;
|
|
|
|
s->response_pos++;
|
|
|
|
if (s->response_pos == 2) {
|
|
|
|
DPRINTF("CRC16 read end\n");
|
2021-01-28 07:30:27 +01:00
|
|
|
if (s->read_bytes == 512 && s->cmd != 17) {
|
|
|
|
s->mode = SSI_SD_PREP_DATA;
|
|
|
|
} else {
|
|
|
|
s->mode = SSI_SD_CMD;
|
|
|
|
}
|
|
|
|
s->read_bytes = 0;
|
2021-01-23 11:39:58 +01:00
|
|
|
s->response_pos = 0;
|
2007-11-25 00:35:08 +01:00
|
|
|
}
|
|
|
|
return val;
|
2021-01-28 07:30:31 +01:00
|
|
|
case SSI_SD_DATA_WRITE:
|
|
|
|
sdbus_write_byte(&s->sdbus, val);
|
|
|
|
s->write_bytes++;
|
|
|
|
if (!sdbus_receive_ready(&s->sdbus) || s->write_bytes == 512) {
|
|
|
|
DPRINTF("Data write end\n");
|
|
|
|
s->mode = SSI_SD_SKIP_CRC16;
|
|
|
|
s->response_pos = 0;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
case SSI_SD_SKIP_CRC16:
|
|
|
|
/* we don't verify the crc16 */
|
|
|
|
s->response_pos++;
|
|
|
|
if (s->response_pos == 2) {
|
|
|
|
DPRINTF("CRC16 receive end\n");
|
|
|
|
s->mode = SSI_SD_RESPONSE;
|
|
|
|
s->write_bytes = 0;
|
|
|
|
s->arglen = 1;
|
|
|
|
s->response[0] = DATA_RESPONSE_ACCEPTED;
|
|
|
|
s->response_pos = 0;
|
|
|
|
}
|
|
|
|
return SSI_DUMMY;
|
2007-11-25 00:35:08 +01:00
|
|
|
}
|
|
|
|
/* Should never happen. */
|
2021-01-23 11:40:02 +01:00
|
|
|
return SSI_DUMMY;
|
2007-11-25 00:35:08 +01:00
|
|
|
}
|
|
|
|
|
2016-09-22 19:13:08 +02:00
|
|
|
static int ssi_sd_post_load(void *opaque, int version_id)
|
2008-07-02 18:48:32 +02:00
|
|
|
{
|
|
|
|
ssi_sd_state *s = (ssi_sd_state *)opaque;
|
|
|
|
|
2021-01-28 07:30:31 +01:00
|
|
|
if (s->mode > SSI_SD_SKIP_CRC16) {
|
2008-07-02 18:48:32 +02:00
|
|
|
return -EINVAL;
|
2016-09-22 19:13:08 +02:00
|
|
|
}
|
2014-04-28 15:08:14 +02:00
|
|
|
if (s->mode == SSI_SD_CMDARG &&
|
|
|
|
(s->arglen < 0 || s->arglen >= ARRAY_SIZE(s->cmdarg))) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (s->mode == SSI_SD_RESPONSE &&
|
|
|
|
(s->response_pos < 0 || s->response_pos >= ARRAY_SIZE(s->response) ||
|
|
|
|
(!s->stopping && s->arglen > ARRAY_SIZE(s->response)))) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2008-07-02 18:48:32 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-22 19:13:08 +02:00
|
|
|
static const VMStateDescription vmstate_ssi_sd = {
|
|
|
|
.name = "ssi_sd",
|
2021-01-28 07:30:31 +01:00
|
|
|
.version_id = 7,
|
|
|
|
.minimum_version_id = 7,
|
2016-09-22 19:13:08 +02:00
|
|
|
.post_load = ssi_sd_post_load,
|
|
|
|
.fields = (VMStateField []) {
|
|
|
|
VMSTATE_UINT32(mode, ssi_sd_state),
|
|
|
|
VMSTATE_INT32(cmd, ssi_sd_state),
|
|
|
|
VMSTATE_UINT8_ARRAY(cmdarg, ssi_sd_state, 4),
|
|
|
|
VMSTATE_UINT8_ARRAY(response, ssi_sd_state, 5),
|
2021-01-23 11:39:58 +01:00
|
|
|
VMSTATE_UINT16(crc16, ssi_sd_state),
|
2021-01-28 07:30:27 +01:00
|
|
|
VMSTATE_INT32(read_bytes, ssi_sd_state),
|
2021-01-28 07:30:31 +01:00
|
|
|
VMSTATE_INT32(write_bytes, ssi_sd_state),
|
2016-09-22 19:13:08 +02:00
|
|
|
VMSTATE_INT32(arglen, ssi_sd_state),
|
|
|
|
VMSTATE_INT32(response_pos, ssi_sd_state),
|
|
|
|
VMSTATE_INT32(stopping, ssi_sd_state),
|
2020-10-12 14:49:55 +02:00
|
|
|
VMSTATE_SSI_PERIPHERAL(ssidev, ssi_sd_state),
|
2016-09-22 19:13:08 +02:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-12 14:49:55 +02:00
|
|
|
static void ssi_sd_realize(SSIPeripheral *d, Error **errp)
|
2007-11-25 00:35:08 +01:00
|
|
|
{
|
2020-07-07 18:50:32 +02:00
|
|
|
ERRP_GUARD();
|
2020-07-03 17:59:46 +02:00
|
|
|
ssi_sd_state *s = SSI_SD(d);
|
2018-02-22 16:12:52 +01:00
|
|
|
DeviceState *carddev;
|
2011-01-28 11:21:37 +01:00
|
|
|
DriveInfo *dinfo;
|
2007-11-25 00:35:08 +01:00
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS,
|
|
|
|
DEVICE(d), "sd-bus");
|
|
|
|
|
|
|
|
/* Create and plug in the sd card */
|
2015-03-23 19:03:13 +01:00
|
|
|
/* FIXME use a qdev drive property instead of drive_get_next() */
|
2011-01-28 11:21:37 +01:00
|
|
|
dinfo = drive_get_next(IF_SD);
|
qdev: Convert uses of qdev_create() with Coccinelle
This is the transformation explained in the commit before previous.
Takes care of just one pattern that needs conversion. More to come in
this series.
Coccinelle script:
@ depends on !(file in "hw/arm/highbank.c")@
expression bus, type_name, dev, expr;
@@
- dev = qdev_create(bus, type_name);
+ dev = qdev_new(type_name);
... when != dev = expr
- qdev_init_nofail(dev);
+ qdev_realize_and_unref(dev, bus, &error_fatal);
@@
expression bus, type_name, dev, expr;
identifier DOWN;
@@
- dev = DOWN(qdev_create(bus, type_name));
+ dev = DOWN(qdev_new(type_name));
... when != dev = expr
- qdev_init_nofail(DEVICE(dev));
+ qdev_realize_and_unref(DEVICE(dev), bus, &error_fatal);
@@
expression bus, type_name, expr;
identifier dev;
@@
- DeviceState *dev = qdev_create(bus, type_name);
+ DeviceState *dev = qdev_new(type_name);
... when != dev = expr
- qdev_init_nofail(dev);
+ qdev_realize_and_unref(dev, bus, &error_fatal);
@@
expression bus, type_name, dev, expr, errp;
symbol true;
@@
- dev = qdev_create(bus, type_name);
+ dev = qdev_new(type_name);
... when != dev = expr
- object_property_set_bool(OBJECT(dev), true, "realized", errp);
+ qdev_realize_and_unref(dev, bus, errp);
@@
expression bus, type_name, expr, errp;
identifier dev;
symbol true;
@@
- DeviceState *dev = qdev_create(bus, type_name);
+ DeviceState *dev = qdev_new(type_name);
... when != dev = expr
- object_property_set_bool(OBJECT(dev), true, "realized", errp);
+ qdev_realize_and_unref(dev, bus, errp);
The first rule exempts hw/arm/highbank.c, because it matches along two
control flow paths there, with different @type_name. Covered by the
next commit's manual conversions.
Missing #include "qapi/error.h" added manually.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200610053247.1583243-10-armbru@redhat.com>
[Conflicts in hw/misc/empty_slot.c and hw/sparc/leon3.c resolved]
2020-06-10 07:31:58 +02:00
|
|
|
carddev = qdev_new(TYPE_SD_CARD);
|
2018-02-22 16:12:52 +01:00
|
|
|
if (dinfo) {
|
2020-07-07 18:06:00 +02:00
|
|
|
if (!qdev_prop_set_drive_err(carddev, "drive",
|
2020-07-07 18:50:32 +02:00
|
|
|
blk_by_legacy_dinfo(dinfo), errp)) {
|
2020-03-17 13:57:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2018-02-22 16:12:52 +01:00
|
|
|
}
|
2020-03-17 13:57:41 +01:00
|
|
|
|
2020-07-07 18:50:32 +02:00
|
|
|
if (!object_property_set_bool(OBJECT(carddev), "spi", true, errp)) {
|
2020-03-17 13:57:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2020-07-07 18:50:32 +02:00
|
|
|
if (!qdev_realize_and_unref(carddev, BUS(&s->sdbus), errp)) {
|
2020-03-17 13:57:41 +01:00
|
|
|
goto fail;
|
2013-09-13 15:51:47 +02:00
|
|
|
}
|
2020-03-17 13:57:41 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
2020-07-07 18:50:32 +02:00
|
|
|
error_prepend(errp, "failed to init SD card: ");
|
2007-11-25 00:35:08 +01:00
|
|
|
}
|
2009-05-14 23:35:09 +02:00
|
|
|
|
2018-01-16 14:28:12 +01:00
|
|
|
static void ssi_sd_reset(DeviceState *dev)
|
|
|
|
{
|
|
|
|
ssi_sd_state *s = SSI_SD(dev);
|
|
|
|
|
|
|
|
s->mode = SSI_SD_CMD;
|
|
|
|
s->cmd = 0;
|
|
|
|
memset(s->cmdarg, 0, sizeof(s->cmdarg));
|
|
|
|
memset(s->response, 0, sizeof(s->response));
|
2021-01-23 11:39:58 +01:00
|
|
|
s->crc16 = 0;
|
2021-01-28 07:30:27 +01:00
|
|
|
s->read_bytes = 0;
|
2021-01-28 07:30:31 +01:00
|
|
|
s->write_bytes = 0;
|
2018-01-16 14:28:12 +01:00
|
|
|
s->arglen = 0;
|
|
|
|
s->response_pos = 0;
|
|
|
|
s->stopping = 0;
|
|
|
|
}
|
|
|
|
|
2011-12-16 20:36:39 +01:00
|
|
|
static void ssi_sd_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2016-09-22 19:13:08 +02:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2020-10-12 14:49:55 +02:00
|
|
|
SSIPeripheralClass *k = SSI_PERIPHERAL_CLASS(klass);
|
2011-12-16 20:36:39 +01:00
|
|
|
|
2016-07-04 14:06:37 +02:00
|
|
|
k->realize = ssi_sd_realize;
|
2011-12-16 20:36:39 +01:00
|
|
|
k->transfer = ssi_sd_transfer;
|
2012-07-31 08:42:04 +02:00
|
|
|
k->cs_polarity = SSI_CS_LOW;
|
2016-09-22 19:13:08 +02:00
|
|
|
dc->vmsd = &vmstate_ssi_sd;
|
2018-01-16 14:28:12 +01:00
|
|
|
dc->reset = ssi_sd_reset;
|
2018-10-24 08:50:16 +02:00
|
|
|
/* Reason: init() method uses drive_get_next() */
|
|
|
|
dc->user_creatable = false;
|
2011-12-16 20:36:39 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo ssi_sd_info = {
|
2018-01-16 14:28:12 +01:00
|
|
|
.name = TYPE_SSI_SD,
|
2020-10-12 14:49:55 +02:00
|
|
|
.parent = TYPE_SSI_PERIPHERAL,
|
2011-12-08 04:34:16 +01:00
|
|
|
.instance_size = sizeof(ssi_sd_state),
|
|
|
|
.class_init = ssi_sd_class_init,
|
2009-05-14 23:35:09 +02:00
|
|
|
};
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
static void ssi_sd_register_types(void)
|
2009-05-14 23:35:09 +02:00
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
type_register_static(&ssi_sd_info);
|
2009-05-14 23:35:09 +02:00
|
|
|
}
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
type_init(ssi_sd_register_types)
|