coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
spi_sdcard.c File Reference
#include <stdint.h>
#include <string.h>
#include <spi-generic.h>
#include <spi_sdcard.h>
#include <crc_byte.h>
#include <commonlib/helpers.h>
#include <console/console.h>
Include dependency graph for spi_sdcard.c:

Go to the source code of this file.

Macros

#define dprintk(fmt, args...)    do {} while (0)
 
#define SDCARD_TYPE_SDSC   1
 
#define SDCARD_TYPE_SDHC   2
 
#define SDCARD_TYPE_SDXC   3
 
#define GO_IDLE_STATE   0
 
#define SEND_OP_COND   1
 
#define SWITCH_FUNC   6
 
#define SEND_IF_COND   8
 
#define SEND_CSD   9
 
#define SEND_CID   10
 
#define STOP_TRANSMISSION   12
 
#define SEND_STATUS   13
 
#define SET_BLOCKLEN   16
 
#define READ_SINGLE_BLOCK   17
 
#define READ_MULTIPLEBLOCK   18
 
#define WRITE_BLOCK   24
 
#define WRITE_MULTIPLEBLOCK   25
 
#define PROGRAM_CSD   27
 
#define SET_WRITE_PROT   28
 
#define CLR_WRITE_PROT   29
 
#define SEND_WRITE_PROT   30
 
#define ERASE_WR_BLK_START_ADDR   32
 
#define ERASE_WR_BLK_END_ADDR   33
 
#define ERASE   38
 
#define LOCK_UNLOCK   42
 
#define APP_CMD   55
 
#define GEN_CMD   56
 
#define READ_OCR   58
 
#define CRC_ON_OFF   59
 
#define SD_STATUS   13
 
#define SEND_NUM_WR_BLOCKS   22
 
#define SET_WR_BLK_ERASE_COUNT   23
 
#define SD_SEND_OP_COND   41
 
#define SET_CLR_CARD_DETECT   42
 
#define SEND_SCR   51
 
#define CT_BLOCK_START   0xfe
 
#define CT_MULTIPLE_BLOCK_START   0xfc
 
#define CT_MULTIPLE_BLOCK_STOP   0xfd
 
#define CT_RESPONSE_MASK   0x1f
 
#define CT_RESPONSE_ACCEPTED   0x05
 
#define CT_RESPONSE_REJECTED_CRC   0x0b
 
#define CT_RESPONSE_REJECTED_WRITE_ERR   0x0d
 
#define RSP_R1   0
 
#define RSP_R1b   1
 
#define RSP_R2   2
 
#define RSP_R3   3
 
#define RSP_R4   4
 
#define RSP_R5   5
 
#define RSP_R7   7
 
#define RSP_ERR_CARD_IS_LOCKED   (1 << 0)
 
#define RSP_ERR_WP_ERASE_SKIP   (1 << 1)
 
#define RSP_ERR_GENERAL   (1 << 2)
 
#define RSP_ERR_CC   (1 << 3)
 
#define RSP_ERR_ECC   (1 << 4)
 
#define RSP_ERR_WP_VIOLATION   (1 << 5)
 
#define RSP_ERR_ERASE_PARAM   (1 << 6)
 
#define RSP_ERR_OUT_OF_RANGE   (1 << 7)
 
#define RSP_ERR_IN_IDLE   (1 << 8)
 
#define RSP_ERR_ERASE_RESET   (1 << 9)
 
#define RSP_ERR_ILLEGAL_COMMAND   (1 << 10)
 
#define RSP_ERR_COM_CRC   (1 << 11)
 
#define RSP_ERR_ERASE_SEQUENCE   (1 << 12)
 
#define RSP_ERR_ADDRESS   (1 << 13)
 
#define RSP_ERR_PARAMETER   (1 << 14)
 
#define BLOCK_SIZE   512
 

Functions

static unsigned long long extract_bits (uint8_t *buff, int width, int start, int end)
 
static void spi_sdcard_enable_cs (const struct spi_sdcard *card)
 
static void spi_sdcard_disable_cs (const struct spi_sdcard *card)
 
static void spi_sdcard_sendbyte (const struct spi_sdcard *card, uint8_t b)
 
static uint8_t spi_sdcard_recvbyte (const struct spi_sdcard *card)
 
static uint8_t spi_sdcard_calculate_command_crc (uint8_t cmd, uint32_t argument)
 
static int lookup_cmd_response_type (uint8_t cmd)
 
static int lookup_acmd_response_type (uint8_t cmd)
 
static int lookup_response_length (int response_type)
 
static int response_resolve (int response_type, uint8_t *response, uint32_t *out_register)
 
static int spi_sdcard_do_command_help (const struct spi_sdcard *card, int is_acmd, uint8_t cmd, uint32_t argument, uint32_t *out_register)
 
static int spi_sdcard_do_command (const struct spi_sdcard *card, uint8_t cmd, uint32_t argument, uint32_t *out_register)
 
static int spi_sdcard_do_app_command (const struct spi_sdcard *card, uint8_t cmd, uint32_t argument, uint32_t *out_register)
 
size_t spi_sdcard_size (const struct spi_sdcard *card)
 
int spi_sdcard_init (struct spi_sdcard *card, const unsigned int bus, const unsigned int cs)
 
int spi_sdcard_single_read (const struct spi_sdcard *card, size_t block_address, void *buff)
 
int spi_sdcard_multiple_read (const struct spi_sdcard *card, size_t start_block_address, size_t end_block_address, void *buff)
 
int spi_sdcard_read (const struct spi_sdcard *card, void *dest, size_t offset, size_t count)
 
int spi_sdcard_single_write (const struct spi_sdcard *card, size_t block_address, void *buff)
 
int spi_sdcard_multiple_write (const struct spi_sdcard *card, size_t start_block_address, size_t end_block_address, void *buff)
 
int spi_sdcard_erase (const struct spi_sdcard *card, size_t start_block_address, size_t end_block_address)
 
int spi_sdcard_erase_all (const struct spi_sdcard *card)
 

Macro Definition Documentation

◆ APP_CMD

#define APP_CMD   55

Definition at line 46 of file spi_sdcard.c.

◆ BLOCK_SIZE

#define BLOCK_SIZE   512

Definition at line 93 of file spi_sdcard.c.

◆ CLR_WRITE_PROT

#define CLR_WRITE_PROT   29

Definition at line 40 of file spi_sdcard.c.

◆ CRC_ON_OFF

#define CRC_ON_OFF   59

Definition at line 49 of file spi_sdcard.c.

◆ CT_BLOCK_START

#define CT_BLOCK_START   0xfe

Definition at line 60 of file spi_sdcard.c.

◆ CT_MULTIPLE_BLOCK_START

#define CT_MULTIPLE_BLOCK_START   0xfc

Definition at line 61 of file spi_sdcard.c.

◆ CT_MULTIPLE_BLOCK_STOP

#define CT_MULTIPLE_BLOCK_STOP   0xfd

Definition at line 62 of file spi_sdcard.c.

◆ CT_RESPONSE_ACCEPTED

#define CT_RESPONSE_ACCEPTED   0x05

Definition at line 64 of file spi_sdcard.c.

◆ CT_RESPONSE_MASK

#define CT_RESPONSE_MASK   0x1f

Definition at line 63 of file spi_sdcard.c.

◆ CT_RESPONSE_REJECTED_CRC

#define CT_RESPONSE_REJECTED_CRC   0x0b

Definition at line 65 of file spi_sdcard.c.

◆ CT_RESPONSE_REJECTED_WRITE_ERR

#define CT_RESPONSE_REJECTED_WRITE_ERR   0x0d

Definition at line 66 of file spi_sdcard.c.

◆ dprintk

#define dprintk (   fmt,
  args... 
)     do {} while (0)

Definition at line 16 of file spi_sdcard.c.

◆ ERASE

#define ERASE   38

Definition at line 44 of file spi_sdcard.c.

◆ ERASE_WR_BLK_END_ADDR

#define ERASE_WR_BLK_END_ADDR   33

Definition at line 43 of file spi_sdcard.c.

◆ ERASE_WR_BLK_START_ADDR

#define ERASE_WR_BLK_START_ADDR   32

Definition at line 42 of file spi_sdcard.c.

◆ GEN_CMD

#define GEN_CMD   56

Definition at line 47 of file spi_sdcard.c.

◆ GO_IDLE_STATE

#define GO_IDLE_STATE   0

Definition at line 25 of file spi_sdcard.c.

◆ LOCK_UNLOCK

#define LOCK_UNLOCK   42

Definition at line 45 of file spi_sdcard.c.

◆ PROGRAM_CSD

#define PROGRAM_CSD   27

Definition at line 38 of file spi_sdcard.c.

◆ READ_MULTIPLEBLOCK

#define READ_MULTIPLEBLOCK   18

Definition at line 35 of file spi_sdcard.c.

◆ READ_OCR

#define READ_OCR   58

Definition at line 48 of file spi_sdcard.c.

◆ READ_SINGLE_BLOCK

#define READ_SINGLE_BLOCK   17

Definition at line 34 of file spi_sdcard.c.

◆ RSP_ERR_ADDRESS

#define RSP_ERR_ADDRESS   (1 << 13)

Definition at line 90 of file spi_sdcard.c.

◆ RSP_ERR_CARD_IS_LOCKED

#define RSP_ERR_CARD_IS_LOCKED   (1 << 0)

Definition at line 77 of file spi_sdcard.c.

◆ RSP_ERR_CC

#define RSP_ERR_CC   (1 << 3)

Definition at line 80 of file spi_sdcard.c.

◆ RSP_ERR_COM_CRC

#define RSP_ERR_COM_CRC   (1 << 11)

Definition at line 88 of file spi_sdcard.c.

◆ RSP_ERR_ECC

#define RSP_ERR_ECC   (1 << 4)

Definition at line 81 of file spi_sdcard.c.

◆ RSP_ERR_ERASE_PARAM

#define RSP_ERR_ERASE_PARAM   (1 << 6)

Definition at line 83 of file spi_sdcard.c.

◆ RSP_ERR_ERASE_RESET

#define RSP_ERR_ERASE_RESET   (1 << 9)

Definition at line 86 of file spi_sdcard.c.

◆ RSP_ERR_ERASE_SEQUENCE

#define RSP_ERR_ERASE_SEQUENCE   (1 << 12)

Definition at line 89 of file spi_sdcard.c.

◆ RSP_ERR_GENERAL

#define RSP_ERR_GENERAL   (1 << 2)

Definition at line 79 of file spi_sdcard.c.

◆ RSP_ERR_ILLEGAL_COMMAND

#define RSP_ERR_ILLEGAL_COMMAND   (1 << 10)

Definition at line 87 of file spi_sdcard.c.

◆ RSP_ERR_IN_IDLE

#define RSP_ERR_IN_IDLE   (1 << 8)

Definition at line 85 of file spi_sdcard.c.

◆ RSP_ERR_OUT_OF_RANGE

#define RSP_ERR_OUT_OF_RANGE   (1 << 7)

Definition at line 84 of file spi_sdcard.c.

◆ RSP_ERR_PARAMETER

#define RSP_ERR_PARAMETER   (1 << 14)

Definition at line 91 of file spi_sdcard.c.

◆ RSP_ERR_WP_ERASE_SKIP

#define RSP_ERR_WP_ERASE_SKIP   (1 << 1)

Definition at line 78 of file spi_sdcard.c.

◆ RSP_ERR_WP_VIOLATION

#define RSP_ERR_WP_VIOLATION   (1 << 5)

Definition at line 82 of file spi_sdcard.c.

◆ RSP_R1

#define RSP_R1   0

Definition at line 69 of file spi_sdcard.c.

◆ RSP_R1b

#define RSP_R1b   1

Definition at line 70 of file spi_sdcard.c.

◆ RSP_R2

#define RSP_R2   2

Definition at line 71 of file spi_sdcard.c.

◆ RSP_R3

#define RSP_R3   3

Definition at line 72 of file spi_sdcard.c.

◆ RSP_R4

#define RSP_R4   4

Definition at line 73 of file spi_sdcard.c.

◆ RSP_R5

#define RSP_R5   5

Definition at line 74 of file spi_sdcard.c.

◆ RSP_R7

#define RSP_R7   7

Definition at line 75 of file spi_sdcard.c.

◆ SD_SEND_OP_COND

#define SD_SEND_OP_COND   41

Definition at line 55 of file spi_sdcard.c.

◆ SD_STATUS

#define SD_STATUS   13

Definition at line 52 of file spi_sdcard.c.

◆ SDCARD_TYPE_SDHC

#define SDCARD_TYPE_SDHC   2

Definition at line 21 of file spi_sdcard.c.

◆ SDCARD_TYPE_SDSC

#define SDCARD_TYPE_SDSC   1

Definition at line 20 of file spi_sdcard.c.

◆ SDCARD_TYPE_SDXC

#define SDCARD_TYPE_SDXC   3

Definition at line 22 of file spi_sdcard.c.

◆ SEND_CID

#define SEND_CID   10

Definition at line 30 of file spi_sdcard.c.

◆ SEND_CSD

#define SEND_CSD   9

Definition at line 29 of file spi_sdcard.c.

◆ SEND_IF_COND

#define SEND_IF_COND   8

Definition at line 28 of file spi_sdcard.c.

◆ SEND_NUM_WR_BLOCKS

#define SEND_NUM_WR_BLOCKS   22

Definition at line 53 of file spi_sdcard.c.

◆ SEND_OP_COND

#define SEND_OP_COND   1

Definition at line 26 of file spi_sdcard.c.

◆ SEND_SCR

#define SEND_SCR   51

Definition at line 57 of file spi_sdcard.c.

◆ SEND_STATUS

#define SEND_STATUS   13

Definition at line 32 of file spi_sdcard.c.

◆ SEND_WRITE_PROT

#define SEND_WRITE_PROT   30

Definition at line 41 of file spi_sdcard.c.

◆ SET_BLOCKLEN

#define SET_BLOCKLEN   16

Definition at line 33 of file spi_sdcard.c.

◆ SET_CLR_CARD_DETECT

#define SET_CLR_CARD_DETECT   42

Definition at line 56 of file spi_sdcard.c.

◆ SET_WR_BLK_ERASE_COUNT

#define SET_WR_BLK_ERASE_COUNT   23

Definition at line 54 of file spi_sdcard.c.

◆ SET_WRITE_PROT

#define SET_WRITE_PROT   28

Definition at line 39 of file spi_sdcard.c.

◆ STOP_TRANSMISSION

#define STOP_TRANSMISSION   12

Definition at line 31 of file spi_sdcard.c.

◆ SWITCH_FUNC

#define SWITCH_FUNC   6

Definition at line 27 of file spi_sdcard.c.

◆ WRITE_BLOCK

#define WRITE_BLOCK   24

Definition at line 36 of file spi_sdcard.c.

◆ WRITE_MULTIPLEBLOCK

#define WRITE_MULTIPLEBLOCK   25

Definition at line 37 of file spi_sdcard.c.

Function Documentation

◆ extract_bits()

static unsigned long long extract_bits ( uint8_t buff,
int  width,
int  start,
int  end 
)
static

Definition at line 95 of file spi_sdcard.c.

References width.

Referenced by spi_sdcard_size().

Here is the caller graph for this function:

◆ lookup_acmd_response_type()

static int lookup_acmd_response_type ( uint8_t  cmd)
static

Definition at line 180 of file spi_sdcard.c.

References RSP_R1, RSP_R2, SD_SEND_OP_COND, SD_STATUS, SEND_NUM_WR_BLOCKS, SEND_SCR, SET_CLR_CARD_DETECT, and SET_WR_BLK_ERASE_COUNT.

Referenced by spi_sdcard_do_command_help().

Here is the caller graph for this function:

◆ lookup_cmd_response_type()

◆ lookup_response_length()

static int lookup_response_length ( int  response_type)
static

Definition at line 195 of file spi_sdcard.c.

References RSP_R1, RSP_R1b, RSP_R2, RSP_R3, and RSP_R7.

Referenced by spi_sdcard_do_command_help().

Here is the caller graph for this function:

◆ response_resolve()

static int response_resolve ( int  response_type,
uint8_t response,
uint32_t out_register 
)
static

Definition at line 210 of file spi_sdcard.c.

References __unused, dprintk, NULL, RSP_R1, RSP_R1b, RSP_R2, RSP_R3, and RSP_R7.

Referenced by spi_sdcard_do_command_help().

Here is the caller graph for this function:

◆ spi_sdcard_calculate_command_crc()

static uint8_t spi_sdcard_calculate_command_crc ( uint8_t  cmd,
uint32_t  argument 
)
static

Definition at line 132 of file spi_sdcard.c.

References crc7_byte().

Referenced by spi_sdcard_do_command_help().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_sdcard_disable_cs()

static void spi_sdcard_disable_cs ( const struct spi_sdcard card)
static

Definition at line 113 of file spi_sdcard.c.

References card, spi_sdcard::slave, and spi_release_bus().

Referenced by spi_sdcard_do_command_help(), spi_sdcard_init(), spi_sdcard_multiple_read(), spi_sdcard_multiple_write(), spi_sdcard_single_read(), spi_sdcard_single_write(), and spi_sdcard_size().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_sdcard_do_app_command()

static int spi_sdcard_do_app_command ( const struct spi_sdcard card,
uint8_t  cmd,
uint32_t  argument,
uint32_t out_register 
)
static

Definition at line 351 of file spi_sdcard.c.

References APP_CMD, card, NULL, spi_sdcard_do_command(), and spi_sdcard_do_command_help().

Referenced by spi_sdcard_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_sdcard_do_command()

static int spi_sdcard_do_command ( const struct spi_sdcard card,
uint8_t  cmd,
uint32_t  argument,
uint32_t out_register 
)
static

Definition at line 343 of file spi_sdcard.c.

References card, and spi_sdcard_do_command_help().

Referenced by spi_sdcard_do_app_command(), spi_sdcard_erase(), spi_sdcard_init(), spi_sdcard_multiple_read(), spi_sdcard_multiple_write(), spi_sdcard_single_read(), spi_sdcard_single_write(), and spi_sdcard_size().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_sdcard_do_command_help()

static int spi_sdcard_do_command_help ( const struct spi_sdcard card,
int  is_acmd,
uint8_t  cmd,
uint32_t  argument,
uint32_t out_register 
)
static

◆ spi_sdcard_enable_cs()

static void spi_sdcard_enable_cs ( const struct spi_sdcard card)
static

Definition at line 108 of file spi_sdcard.c.

References card, spi_sdcard::slave, and spi_claim_bus().

Referenced by spi_sdcard_do_command_help(), spi_sdcard_multiple_read(), spi_sdcard_multiple_write(), spi_sdcard_single_read(), spi_sdcard_single_write(), and spi_sdcard_size().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_sdcard_erase()

int spi_sdcard_erase ( const struct spi_sdcard card,
size_t  start_block_address,
size_t  end_block_address 
)

Definition at line 772 of file spi_sdcard.c.

References card, ERASE, ERASE_WR_BLK_END_ADDR, ERASE_WR_BLK_START_ADDR, NULL, SDCARD_TYPE_SDSC, spi_sdcard_do_command(), and spi_sdcard::type.

Referenced by spi_sdcard_erase_all().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_sdcard_erase_all()

int spi_sdcard_erase_all ( const struct spi_sdcard card)

Definition at line 798 of file spi_sdcard.c.

References BLOCK_SIZE, card, spi_sdcard_erase(), and spi_sdcard_size().

Here is the call graph for this function:

◆ spi_sdcard_init()

int spi_sdcard_init ( struct spi_sdcard card,
const unsigned int  bus,
const unsigned int  cs 
)

◆ spi_sdcard_multiple_read()

int spi_sdcard_multiple_read ( const struct spi_sdcard card,
size_t  start_block_address,
size_t  end_block_address,
void buff 
)

Definition at line 544 of file spi_sdcard.c.

References c, card, crc16_byte(), CT_BLOCK_START, NULL, READ_MULTIPLEBLOCK, SDCARD_TYPE_SDSC, SEND_STATUS, spi_sdcard_disable_cs(), spi_sdcard_do_command(), spi_sdcard_enable_cs(), spi_sdcard_recvbyte(), STOP_TRANSMISSION, spi_sdcard::type, and wait().

Referenced by spi_sdcard_read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_sdcard_multiple_write()

int spi_sdcard_multiple_write ( const struct spi_sdcard card,
size_t  start_block_address,
size_t  end_block_address,
void buff 
)

◆ spi_sdcard_read()

int spi_sdcard_read ( const struct spi_sdcard card,
void dest,
size_t  offset,
size_t  count 
)

Definition at line 603 of file spi_sdcard.c.

References BLOCK_SIZE, card, count, memcpy(), offset, spi_sdcard_multiple_read(), and spi_sdcard_single_read().

Here is the call graph for this function:

◆ spi_sdcard_recvbyte()

static uint8_t spi_sdcard_recvbyte ( const struct spi_sdcard card)
static

Definition at line 124 of file spi_sdcard.c.

References card, dprintk, spi_sdcard::slave, and spi_xfer().

Referenced by spi_sdcard_do_command_help(), spi_sdcard_multiple_read(), spi_sdcard_multiple_write(), spi_sdcard_single_read(), spi_sdcard_single_write(), and spi_sdcard_size().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_sdcard_sendbyte()

static void spi_sdcard_sendbyte ( const struct spi_sdcard card,
uint8_t  b 
)
static

Definition at line 118 of file spi_sdcard.c.

References card, dprintk, NULL, spi_sdcard::slave, and spi_xfer().

Referenced by spi_sdcard_do_command_help(), spi_sdcard_init(), spi_sdcard_multiple_write(), and spi_sdcard_single_write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_sdcard_single_read()

int spi_sdcard_single_read ( const struct spi_sdcard card,
size_t  block_address,
void buff 
)

Definition at line 496 of file spi_sdcard.c.

References c, card, crc16_byte(), CT_BLOCK_START, NULL, READ_SINGLE_BLOCK, SDCARD_TYPE_SDSC, spi_sdcard_disable_cs(), spi_sdcard_do_command(), spi_sdcard_enable_cs(), spi_sdcard_recvbyte(), spi_sdcard::type, and wait().

Referenced by spi_sdcard_read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_sdcard_single_write()

int spi_sdcard_single_write ( const struct spi_sdcard card,
size_t  block_address,
void buff 
)

◆ spi_sdcard_size()

size_t spi_sdcard_size ( const struct spi_sdcard card)

Definition at line 363 of file spi_sdcard.c.

References c, card, crc16_byte(), CT_BLOCK_START, extract_bits(), NULL, SEND_CSD, spi_sdcard_disable_cs(), spi_sdcard_do_command(), spi_sdcard_enable_cs(), spi_sdcard_recvbyte(), and wait().

Referenced by spi_sdcard_erase_all(), and spi_sdcard_init().

Here is the call graph for this function:
Here is the caller graph for this function: