1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * MultiMediaCard (MMC), eMMC and Secure Digital (SD) common code which
4 * transitions the card from the standby state to the transfer state. The
5 * common code supports read operations, erase and write operations are in
6 * a separate modules. This code is controller independent.
9 #include <commonlib/storage.h>
14 #define DECIMAL_CAPACITY_MULTIPLIER 1000ULL
15 #define HEX_CAPACITY_MULTIPLIER 1024ULL
18 const char *const units
;
22 static void display_capacity(struct storage_media
*media
, int partition_number
)
25 uint64_t decimal_divisor
;
26 const char *decimal_units
;
28 const char *hex_units
;
31 const char *separator
;
32 const struct capacity decimal_list
[] = {
33 {"TB", DECIMAL_CAPACITY_MULTIPLIER
* DECIMAL_CAPACITY_MULTIPLIER
34 * DECIMAL_CAPACITY_MULTIPLIER
35 * DECIMAL_CAPACITY_MULTIPLIER
},
36 {"GB", DECIMAL_CAPACITY_MULTIPLIER
* DECIMAL_CAPACITY_MULTIPLIER
37 * DECIMAL_CAPACITY_MULTIPLIER
},
38 {"MB", DECIMAL_CAPACITY_MULTIPLIER
39 * DECIMAL_CAPACITY_MULTIPLIER
},
40 {"KB", DECIMAL_CAPACITY_MULTIPLIER
},
43 const struct capacity hex_list
[] = {
44 {"TiB", HEX_CAPACITY_MULTIPLIER
* HEX_CAPACITY_MULTIPLIER
45 * HEX_CAPACITY_MULTIPLIER
* HEX_CAPACITY_MULTIPLIER
},
46 {"GiB", HEX_CAPACITY_MULTIPLIER
* HEX_CAPACITY_MULTIPLIER
47 * HEX_CAPACITY_MULTIPLIER
},
48 {"MiB", HEX_CAPACITY_MULTIPLIER
* HEX_CAPACITY_MULTIPLIER
},
49 {"KiB", HEX_CAPACITY_MULTIPLIER
},
53 /* Get the partition name */
54 capacity
= media
->capacity
[partition_number
];
55 name
= storage_partition_name(media
, partition_number
);
57 if (CONFIG(COMMONLIB_STORAGE_MMC
) && !IS_SD(media
))
60 /* Determine the decimal divisor for the capacity */
61 for (index
= 0; index
< ARRAY_SIZE(decimal_list
) - 1; index
++) {
62 if (capacity
>= decimal_list
[index
].bytes
)
65 decimal_divisor
= decimal_list
[index
].bytes
;
66 decimal_units
= decimal_list
[index
].units
;
68 /* Determine the hex divisor for the capacity */
69 for (index
= 0; index
< ARRAY_SIZE(hex_list
) - 1; index
++) {
70 if (capacity
>= hex_list
[index
].bytes
)
73 hex_divisor
= hex_list
[index
].bytes
;
74 hex_units
= hex_list
[index
].units
;
76 /* Display the capacity */
77 sdhc_debug("%3lld.%03lld %sytes (%3lld.%03lld %sytes)%s%s\n",
78 capacity
/ decimal_divisor
,
79 (capacity
/ (decimal_divisor
/ 1000)) % 1000,
81 capacity
/ hex_divisor
,
82 ((capacity
/ (hex_divisor
/ 1024)) * 1000 / 1024) % 1000,
88 void storage_display_setup(struct storage_media
*media
)
92 /* Display the device info */
93 sd_mmc_debug("Man %06x Snr %u ",
95 (((media
->cid
[2] & 0xffff) << 16) |
96 ((media
->cid
[3] >> 16) & 0xffff)));
97 sd_mmc_debug("Product %c%c%c%c", media
->cid
[0] & 0xff,
98 (media
->cid
[1] >> 24), (media
->cid
[1] >> 16) & 0xff,
99 (media
->cid
[1] >> 8) & 0xff);
100 if (!IS_SD(media
)) /* eMMC product string is longer */
101 sd_mmc_debug("%c%c", media
->cid
[1] & 0xff,
102 (media
->cid
[2] >> 24) & 0xff);
103 sd_mmc_debug(" Revision %d.%d\n", (media
->cid
[2] >> 20) & 0xf,
104 (media
->cid
[2] >> 16) & 0xf);
106 /* Display the erase block size */
107 sdhc_debug("Erase block size: 0x%08x\n", media
->erase_blocks
108 * media
->write_bl_len
);
110 /* Display the partition capacities */
111 if (CONFIG(SDHC_DEBUG
)) {
112 for (partition_number
= 0; partition_number
113 < ARRAY_SIZE(media
->capacity
); partition_number
++) {
114 if (!media
->capacity
[partition_number
])
116 display_capacity(media
, partition_number
);
121 int storage_startup(struct storage_media
*media
)
125 uint64_t cmult
, csize
;
126 struct mmc_command cmd
;
127 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
129 /* Determine the storage capacity */
130 if (media
->high_capacity
) {
132 csize
= sd_mmc_extract_uint32_bits(media
->csd
, 58, 22);
134 csize
= sd_mmc_extract_uint32_bits(media
->csd
, 54, 12);
135 cmult
= sd_mmc_extract_uint32_bits(media
->csd
, 78, 3);
137 capacity
= (csize
+ 1) << (cmult
+ 2);
138 capacity
*= media
->read_bl_len
;
139 media
->capacity
[0] = capacity
;
141 /* Limit the block size to 512 bytes */
142 if (media
->read_bl_len
> 512)
143 media
->read_bl_len
= 512;
144 if (media
->write_bl_len
> 512)
145 media
->write_bl_len
= 512;
147 /* Get the erase size in blocks */
148 media
->erase_blocks
=
149 (sd_mmc_extract_uint32_bits(media
->csd
, 47, 3) + 1)
150 * (sd_mmc_extract_uint32_bits(media
->csd
, 42, 5) + 1);
152 /* Select the card, and put it into Transfer Mode */
153 cmd
.cmdidx
= MMC_CMD_SELECT_CARD
;
154 cmd
.resp_type
= CARD_RSP_R1
;
155 cmd
.cmdarg
= media
->rca
<< 16;
157 err
= ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
);
161 /* Increase the bus frequency */
162 if (CONFIG(COMMONLIB_STORAGE_SD
) && IS_SD(media
))
163 err
= sd_change_freq(media
);
164 else if (CONFIG(COMMONLIB_STORAGE_MMC
)) {
165 err
= mmc_change_freq(media
);
167 mmc_update_capacity(media
);
172 /* Restrict card's capabilities by what the controller can do */
173 media
->caps
&= ctrlr
->caps
;
175 /* Increase the bus width if possible */
176 if (CONFIG(COMMONLIB_STORAGE_SD
) && IS_SD(media
))
177 err
= sd_set_bus_width(media
);
178 else if (CONFIG(COMMONLIB_STORAGE_MMC
))
179 err
= mmc_set_bus_width(media
);
183 /* Display the card setup */
184 storage_display_setup(media
);
188 int storage_setup_media(struct storage_media
*media
, struct sd_mmc_ctrlr
*ctrlr
)
192 memset(media
, 0, sizeof(*media
));
193 media
->ctrlr
= ctrlr
;
195 err
= sd_mmc_enter_standby(media
);
198 return storage_startup(media
);
201 static int storage_read(struct storage_media
*media
, void *dest
, uint32_t start
,
202 uint32_t block_count
)
204 struct mmc_command cmd
;
205 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
207 cmd
.resp_type
= CARD_RSP_R1
;
211 cmd
.cmdidx
= MMC_CMD_READ_MULTIPLE_BLOCK
;
213 cmd
.cmdidx
= MMC_CMD_READ_SINGLE_BLOCK
;
215 if (media
->high_capacity
)
218 cmd
.cmdarg
= start
* media
->read_bl_len
;
220 struct mmc_data data
;
222 data
.blocks
= block_count
;
223 data
.blocksize
= media
->read_bl_len
;
224 data
.flags
= DATA_FLAG_READ
;
226 if (ctrlr
->send_cmd(ctrlr
, &cmd
, &data
))
229 if ((block_count
> 1) && !(ctrlr
->caps
230 & DRVR_CAP_AUTO_CMD12
)) {
231 cmd
.cmdidx
= MMC_CMD_STOP_TRANSMISSION
;
233 cmd
.resp_type
= CARD_RSP_R1b
;
234 cmd
.flags
= CMD_FLAG_IGNORE_INHIBIT
;
235 if (ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
)) {
236 sd_mmc_error("Failed to send stop cmd\n");
240 /* Waiting for the ready status */
241 sd_mmc_send_status(media
, SD_MMC_IO_RETRIES
);
247 /////////////////////////////////////////////////////////////////////////////
248 // BlockDevice utilities and callbacks
250 int storage_block_setup(struct storage_media
*media
, uint64_t start
,
251 uint64_t count
, int is_read
)
253 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
254 int partition_number
;
259 uint32_t bl_len
= is_read
? media
->read_bl_len
:
262 /* Validate the block range */
263 partition_number
= media
->partition_config
& EXT_CSD_PART_ACCESS_MASK
;
264 if (((start
* bl_len
) > media
->capacity
[partition_number
])
265 || (((start
+ count
) * bl_len
) >
266 media
->capacity
[partition_number
])) {
267 sd_mmc_error("Block range exceeds device capacity\n");
272 * CMD16 only applies to single data rate mode, and block
273 * length for double data rate is always 512 bytes.
275 if ((ctrlr
->timing
== BUS_TIMING_UHS_DDR50
) ||
276 (ctrlr
->timing
== BUS_TIMING_MMC_DDR52
) ||
277 (ctrlr
->timing
== BUS_TIMING_MMC_HS400
) ||
278 (ctrlr
->timing
== BUS_TIMING_MMC_HS400ES
))
280 if (sd_mmc_set_blocklen(ctrlr
, bl_len
))
286 uint64_t storage_block_read(struct storage_media
*media
, uint64_t start
,
287 uint64_t count
, void *buffer
)
289 uint8_t *dest
= (uint8_t *)buffer
;
291 if (storage_block_setup(media
, start
, count
, 1) == 0)
294 uint64_t todo
= count
;
295 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
297 uint32_t cur
= (uint32_t)MIN(todo
, ctrlr
->b_max
);
298 if (storage_read(media
, dest
, start
, cur
) != cur
)
301 sd_mmc_trace("%s: Got %d blocks, more %d (total %d) to go.\n",
302 __func__
, (int)cur
, (int)todo
, (int)count
);
304 dest
+= cur
* media
->read_bl_len
;
309 int storage_set_partition(struct storage_media
*media
,
310 unsigned int partition_number
)
314 /* Select the partition */
316 if (CONFIG(COMMONLIB_STORAGE_SD
) && IS_SD(media
))
317 err
= sd_set_partition(media
, partition_number
);
318 else if (CONFIG(COMMONLIB_STORAGE_MMC
))
319 err
= mmc_set_partition(media
, partition_number
);
321 sd_mmc_error("Invalid partition number!\n");
325 const char *storage_partition_name(struct storage_media
*media
,
326 unsigned int partition_number
)
330 /* Get the partition name */
332 if (CONFIG(COMMONLIB_STORAGE_SD
) && IS_SD(media
))
333 name
= sd_partition_name(media
, partition_number
);
334 else if (CONFIG(COMMONLIB_STORAGE_MMC
))
335 name
= mmc_partition_name(media
, partition_number
);
339 unsigned int storage_get_current_partition(struct storage_media
*media
)
341 return media
->partition_config
& EXT_CSD_PART_ACCESS_MASK
;