coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
sd_mmc.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * MultiMediaCard (MMC), eMMC and Secure Digital (SD) common initialization
4  * code which brings the card into the standby state. This code is controller
5  * independent.
6  */
7 
8 #include <commonlib/storage.h>
9 #include <delay.h>
10 #include <endian.h>
11 #include <string.h>
12 
13 #include "mmc.h"
14 #include "sd_mmc.h"
15 #include "storage.h"
16 
17 uint64_t sd_mmc_extract_uint32_bits(const uint32_t *array, int start, int count)
18 {
19  int i;
20  uint64_t value = 0;
21 
22  for (i = 0; i < count; i++, start++) {
23  value <<= 1;
24  value |= (array[start / 32] >> (31 - (start % 32))) & 0x1;
25  }
26  return value;
27 }
28 
30 {
31  uint32_t mult, freq;
32 
33  /* frequency bases, divided by 10 to be nice to platforms without
34  * floating point */
35  static const int fbase[] = {
36  10000,
37  100000,
38  1000000,
39  10000000,
40  };
41  /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
42  * to platforms without floating point. */
43  static const int multipliers[] = {
44  0, // reserved
45  10,
46  12,
47  13,
48  15,
49  20,
50  25,
51  30,
52  35,
53  40,
54  45,
55  50,
56  55,
57  60,
58  70,
59  80,
60  };
61 
62  /* divide frequency by 10, since the mults are 10x bigger */
63  freq = fbase[csd0 & 0x7];
64  mult = multipliers[(csd0 >> 3) & 0xf];
65  return freq * mult;
66 }
67 
69 {
70  struct sd_mmc_ctrlr *ctrlr = media->ctrlr;
71 
72  // Some cards can't accept idle commands without delay.
73  if (ctrlr->mdelay_before_cmd0)
74  mdelay(ctrlr->mdelay_before_cmd0);
75 
76  struct mmc_command cmd;
78  cmd.cmdarg = 0;
80  cmd.flags = 0;
81 
82  int err = ctrlr->send_cmd(ctrlr, &cmd, NULL);
83  if (err)
84  return err;
85 
86  // Some cards need more than half second to respond to next command (ex,
87  // SEND_OP_COND).
88  if (ctrlr->mdelay_after_cmd0)
89  mdelay(ctrlr->mdelay_after_cmd0);
90 
91  return 0;
92 }
93 
95 {
96  struct mmc_command cmd;
97  struct sd_mmc_ctrlr *ctrlr = media->ctrlr;
98 
100  cmd.resp_type = CARD_RSP_R1;
101  cmd.cmdarg = media->rca << 16;
102  cmd.flags = 0;
103 
104  while (tries--) {
105  int err = ctrlr->send_cmd(ctrlr, &cmd, NULL);
106  if (err)
107  return err;
108  else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA)
109  break;
110  else if (cmd.response[0] & MMC_STATUS_MASK) {
111  sd_mmc_error("Status Error: %#8.8x\n", cmd.response[0]);
112  return CARD_COMM_ERR;
113  }
114 
115  udelay(100);
116  }
117 
118  sd_mmc_trace("CURR STATE:%d\n",
119  (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9);
120 
121  if (tries < 0) {
122  sd_mmc_error("Timeout waiting card ready\n");
123  return CARD_TIMEOUT;
124  }
125  return 0;
126 }
127 
128 int sd_mmc_set_blocklen(struct sd_mmc_ctrlr *ctrlr, int len)
129 {
130  struct mmc_command cmd;
132  cmd.resp_type = CARD_RSP_R1;
133  cmd.cmdarg = len;
134  cmd.flags = 0;
135 
136  return ctrlr->send_cmd(ctrlr, &cmd, NULL);
137 }
138 
140 {
141  struct mmc_command cmd;
142  struct sd_mmc_ctrlr *ctrlr = media->ctrlr;
143  int err;
144 
145  SET_BUS_WIDTH(ctrlr, 1);
146  SET_CLOCK(ctrlr, 1);
147 
148  /* Reset the Card */
149  err = sd_mmc_go_idle(media);
150  if (err)
151  return err;
152 
153  /* Test for SD version 2 */
154  err = CARD_TIMEOUT;
155  if (CONFIG(COMMONLIB_STORAGE_SD)) {
156  err = sd_send_if_cond(media);
157 
158  /* Get SD card operating condition */
159  if (!err)
160  err = sd_send_op_cond(media);
161  }
162 
163  /* If the command timed out, we check for an MMC card */
164  if (CONFIG(COMMONLIB_STORAGE_MMC) && (err == CARD_TIMEOUT)) {
165  /* Some cards seem to need this */
167 
168  err = mmc_send_op_cond(media);
169  if (err == CARD_IN_PROGRESS)
171  }
172 
173  if (err) {
174  sd_mmc_error(
175  "Card did not respond to voltage select!\n");
176  return CARD_UNUSABLE_ERR;
177  }
178 
179  /* Put the Card in Identify Mode */
181  cmd.resp_type = CARD_RSP_R2;
182  cmd.cmdarg = 0;
183  cmd.flags = 0;
184  err = ctrlr->send_cmd(ctrlr, &cmd, NULL);
185  if (err)
186  return err;
187  memcpy(media->cid, cmd.response, sizeof(media->cid));
188 
189  /*
190  * For MMC cards, set the Relative Address.
191  * For SD cards, get the Relative Address.
192  * This also puts the cards into Standby State
193  */
195  cmd.cmdarg = media->rca << 16;
196  cmd.resp_type = CARD_RSP_R6;
197  cmd.flags = 0;
198  err = ctrlr->send_cmd(ctrlr, &cmd, NULL);
199  if (err)
200  return err;
201  if (IS_SD(media))
202  media->rca = (cmd.response[0] >> 16) & 0xffff;
203 
204  /* Get the Card-Specific Data */
205  cmd.cmdidx = MMC_CMD_SEND_CSD;
206  cmd.resp_type = CARD_RSP_R2;
207  cmd.cmdarg = media->rca << 16;
208  cmd.flags = 0;
209  err = ctrlr->send_cmd(ctrlr, &cmd, NULL);
210 
211  /* Waiting for the ready status */
213  if (err)
214  return err;
215 
216  memcpy(media->csd, cmd.response, sizeof(media->csd));
219  switch (version) {
220  case 0:
222  break;
223  case 1:
225  break;
226  case 2:
228  break;
229  case 3:
231  break;
232  case 4:
234  break;
235  default:
237  break;
238  }
239  }
241 
242  /* Determine the read and write block lengths */
244  if (IS_SD(media))
246  else
248  1 << sd_mmc_extract_uint32_bits(media->csd, 102, 4);
249 
250  sd_mmc_debug("mmc media info: version=%#x, tran_speed=%d\n",
251  media->version, (int)media->tran_speed);
252 
253  return 0;
254 }
pte_t value
Definition: mmu.c:91
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
int mmc_complete_op_cond(struct storage_media *media)
Definition: mmc.c:65
int mmc_send_op_cond(struct storage_media *media)
Definition: mmc.c:45
#define MMC_STATUS_MASK
Definition: mmc.h:15
#define MMC_STATUS_CURR_STATE
Definition: mmc.h:17
#define MMC_STATUS_RDY_FOR_DATA
Definition: mmc.h:16
int sd_send_if_cond(struct storage_media *media)
Definition: sd.c:15
int sd_send_op_cond(struct storage_media *media)
Definition: sd.c:35
void mdelay(unsigned int msecs)
Definition: delay.c:2
@ CONFIG
Definition: dsi_common.h:201
#define MMC_VERSION_1_4
Definition: storage.h:84
#define MMC_VERSION_3
Definition: storage.h:86
#define MMC_VERSION_1_2
Definition: storage.h:83
#define MMC_VERSION_2_2
Definition: storage.h:85
#define MMC_VERSION_4
Definition: storage.h:87
#define MMC_VERSION_UNKNOWN
Definition: storage.h:82
unsigned int version[2]
Definition: edid.c:55
static struct storage_media media
Definition: sd_media.c:21
uint64_t sd_mmc_extract_uint32_bits(const uint32_t *array, int start, int count)
Definition: sd_mmc.c:17
static uint32_t sd_mmc_calculate_transfer_speed(uint32_t csd0)
Definition: sd_mmc.c:29
int sd_mmc_go_idle(struct storage_media *media)
Definition: sd_mmc.c:68
int sd_mmc_send_status(struct storage_media *media, ssize_t tries)
Definition: sd_mmc.c:94
int sd_mmc_enter_standby(struct storage_media *media)
Definition: sd_mmc.c:139
int sd_mmc_set_blocklen(struct sd_mmc_ctrlr *ctrlr, int len)
Definition: sd_mmc.c:128
#define sd_mmc_trace(format...)
Definition: sd_mmc.h:82
#define SET_BUS_WIDTH(ctrlr, width)
Definition: sd_mmc.h:15
#define SD_MMC_IO_RETRIES
Definition: sd_mmc.h:11
#define sd_mmc_error(format...)
Definition: sd_mmc.h:87
#define sd_mmc_debug(format...)
Definition: sd_mmc.h:77
#define IS_SD(x)
Definition: sd_mmc.h:13
#define SET_CLOCK(ctrlr, clock_hz)
Definition: sd_mmc.h:21
#define MMC_CMD_GO_IDLE_STATE
Definition: sd_mmc_ctrlr.h:28
#define MMC_CMD_SEND_CSD
Definition: sd_mmc_ctrlr.h:33
#define CARD_RSP_NONE
Definition: sd_mmc_ctrlr.h:75
#define CARD_RSP_R1
Definition: sd_mmc_ctrlr.h:76
#define CARD_RSP_R2
Definition: sd_mmc_ctrlr.h:79
#define MMC_CMD_SEND_STATUS
Definition: sd_mmc_ctrlr.h:36
#define CARD_IN_PROGRESS
Definition: sd_mmc_ctrlr.h:14
#define CARD_UNUSABLE_ERR
Definition: sd_mmc_ctrlr.h:11
#define CARD_TIMEOUT
Definition: sd_mmc_ctrlr.h:13
#define CARD_COMM_ERR
Definition: sd_mmc_ctrlr.h:12
#define MMC_CMD_SET_BLOCKLEN
Definition: sd_mmc_ctrlr.h:37
#define CARD_RSP_R6
Definition: sd_mmc_ctrlr.h:83
#define MMC_CMD_ALL_SEND_CID
Definition: sd_mmc_ctrlr.h:30
#define SD_CMD_SEND_RELATIVE_ADDR
Definition: sd_mmc_ctrlr.h:56
#define NULL
Definition: stddef.h:19
__SIZE_TYPE__ ssize_t
Definition: stddef.h:13
unsigned int uint32_t
Definition: stdint.h:14
unsigned long long uint64_t
Definition: stdint.h:17
Definition: dw_i2c.c:39
uint32_t response[4]
Definition: sd_mmc_ctrlr.h:91
uint32_t cmdarg
Definition: sd_mmc_ctrlr.h:86
uint16_t cmdidx
Definition: sd_mmc_ctrlr.h:25
uint32_t flags
Definition: sd_mmc_ctrlr.h:92
uint32_t resp_type
Definition: sd_mmc_ctrlr.h:67
uint32_t mdelay_before_cmd0
Definition: sd_mmc_ctrlr.h:201
int(* send_cmd)(struct sd_mmc_ctrlr *ctrlr, struct mmc_command *cmd, struct mmc_data *data)
Definition: sd_mmc_ctrlr.h:121
uint32_t mdelay_after_cmd0
Definition: sd_mmc_ctrlr.h:202
uint32_t read_bl_len
Definition: storage.h:89
uint32_t version
Definition: storage.h:75
uint32_t write_bl_len
Definition: storage.h:90
uint32_t csd[4]
Definition: storage.h:108
struct sd_mmc_ctrlr * ctrlr
Definition: storage.h:63
uint32_t cid[4]
Definition: storage.h:109
uint32_t tran_speed
Definition: storage.h:92
uint16_t rca
Definition: storage.h:110
void udelay(uint32_t us)
Definition: udelay.c:15
#define count