Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 1 | /* |
Ahmad Fatoum | ee8f342 | 2022-05-23 17:06:37 +0200 | [diff] [blame] | 2 | * Copyright (c) 2018-2022, ARM Limited and Contributors. All rights reserved. |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | /* Define a simple and generic interface to access eMMC and SD-card devices. */ |
| 8 | |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 9 | #include <assert.h> |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 10 | #include <errno.h> |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 11 | #include <stdbool.h> |
| 12 | #include <string.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | |
| 14 | #include <arch_helpers.h> |
| 15 | #include <common/debug.h> |
| 16 | #include <drivers/delay_timer.h> |
| 17 | #include <drivers/mmc.h> |
| 18 | #include <lib/utils.h> |
Jayanth Dodderi Chidanand | 345af84 | 2022-09-09 17:21:24 +0100 | [diff] [blame] | 19 | #include <plat/common/common_def.h> |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 20 | |
| 21 | #define MMC_DEFAULT_MAX_RETRIES 5 |
| 22 | #define SEND_OP_COND_MAX_RETRIES 100 |
| 23 | |
| 24 | #define MULT_BY_512K_SHIFT 19 |
| 25 | |
| 26 | static const struct mmc_ops *ops; |
| 27 | static unsigned int mmc_ocr_value; |
| 28 | static struct mmc_csd_emmc mmc_csd; |
Yann Gautier | 0307b71 | 2019-06-12 15:55:37 +0200 | [diff] [blame] | 29 | static struct sd_switch_status sd_switch_func_status; |
Haojian Zhuang | df7271c | 2018-08-02 14:50:12 +0800 | [diff] [blame] | 30 | static unsigned char mmc_ext_csd[512] __aligned(16); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 31 | static unsigned int mmc_flags; |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 32 | static struct mmc_device_info *mmc_dev_info; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 33 | static unsigned int rca; |
Tien Hock, Loh | 313e900 | 2019-03-07 11:34:20 +0800 | [diff] [blame] | 34 | static unsigned int scr[2]__aligned(16) = { 0 }; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 35 | |
| 36 | static const unsigned char tran_speed_base[16] = { |
| 37 | 0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80 |
| 38 | }; |
| 39 | |
| 40 | static const unsigned char sd_tran_speed_base[16] = { |
| 41 | 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 |
| 42 | }; |
| 43 | |
| 44 | static bool is_cmd23_enabled(void) |
| 45 | { |
| 46 | return ((mmc_flags & MMC_FLAG_CMD23) != 0U); |
| 47 | } |
| 48 | |
Yann Gautier | 0307b71 | 2019-06-12 15:55:37 +0200 | [diff] [blame] | 49 | static bool is_sd_cmd6_enabled(void) |
| 50 | { |
| 51 | return ((mmc_flags & MMC_FLAG_SD_CMD6) != 0U); |
| 52 | } |
| 53 | |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 54 | static int mmc_send_cmd(unsigned int idx, unsigned int arg, |
| 55 | unsigned int r_type, unsigned int *r_data) |
| 56 | { |
| 57 | struct mmc_cmd cmd; |
| 58 | int ret; |
| 59 | |
| 60 | zeromem(&cmd, sizeof(struct mmc_cmd)); |
| 61 | |
| 62 | cmd.cmd_idx = idx; |
| 63 | cmd.cmd_arg = arg; |
| 64 | cmd.resp_type = r_type; |
| 65 | |
| 66 | ret = ops->send_cmd(&cmd); |
| 67 | |
| 68 | if ((ret == 0) && (r_data != NULL)) { |
| 69 | int i; |
| 70 | |
| 71 | for (i = 0; i < 4; i++) { |
| 72 | *r_data = cmd.resp_data[i]; |
| 73 | r_data++; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (ret != 0) { |
| 78 | VERBOSE("Send command %u error: %d\n", idx, ret); |
| 79 | } |
| 80 | |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | static int mmc_device_state(void) |
| 85 | { |
| 86 | int retries = MMC_DEFAULT_MAX_RETRIES; |
| 87 | unsigned int resp_data[4]; |
| 88 | |
| 89 | do { |
| 90 | int ret; |
| 91 | |
| 92 | if (retries == 0) { |
| 93 | ERROR("CMD13 failed after %d retries\n", |
| 94 | MMC_DEFAULT_MAX_RETRIES); |
| 95 | return -EIO; |
| 96 | } |
| 97 | |
| 98 | ret = mmc_send_cmd(MMC_CMD(13), rca << RCA_SHIFT_OFFSET, |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 99 | MMC_RESPONSE_R1, &resp_data[0]); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 100 | if (ret != 0) { |
Tien Hock, Loh | 313e900 | 2019-03-07 11:34:20 +0800 | [diff] [blame] | 101 | retries--; |
| 102 | continue; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if ((resp_data[0] & STATUS_SWITCH_ERROR) != 0U) { |
| 106 | return -EIO; |
| 107 | } |
| 108 | |
| 109 | retries--; |
| 110 | } while ((resp_data[0] & STATUS_READY_FOR_DATA) == 0U); |
| 111 | |
| 112 | return MMC_GET_STATE(resp_data[0]); |
| 113 | } |
| 114 | |
Vyacheslav Yurkov | b3d5f34 | 2021-03-30 08:16:20 +0200 | [diff] [blame] | 115 | static int mmc_send_part_switch_cmd(unsigned int part_config) |
| 116 | { |
| 117 | int ret; |
| 118 | unsigned int part_time = 0; |
| 119 | |
| 120 | ret = mmc_send_cmd(MMC_CMD(6), |
| 121 | EXTCSD_WRITE_BYTES | |
| 122 | EXTCSD_CMD(CMD_EXTCSD_PARTITION_CONFIG) | |
| 123 | EXTCSD_VALUE(part_config) | |
| 124 | EXTCSD_CMD_SET_NORMAL, |
| 125 | MMC_RESPONSE_R1B, NULL); |
| 126 | if (ret != 0) { |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | /* Partition switch timing is in 10ms units */ |
| 131 | part_time = mmc_ext_csd[CMD_EXTCSD_PART_SWITCH_TIME] * 10; |
| 132 | |
| 133 | mdelay(part_time); |
| 134 | |
| 135 | do { |
| 136 | ret = mmc_device_state(); |
| 137 | if (ret < 0) { |
| 138 | return ret; |
| 139 | } |
| 140 | } while (ret == MMC_STATE_PRG); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 145 | static int mmc_set_ext_csd(unsigned int ext_cmd, unsigned int value) |
| 146 | { |
| 147 | int ret; |
| 148 | |
| 149 | ret = mmc_send_cmd(MMC_CMD(6), |
| 150 | EXTCSD_WRITE_BYTES | EXTCSD_CMD(ext_cmd) | |
| 151 | EXTCSD_VALUE(value) | EXTCSD_CMD_SET_NORMAL, |
Bryan O'Donoghue | e6b56cc | 2018-08-15 16:25:30 +0100 | [diff] [blame] | 152 | MMC_RESPONSE_R1B, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 153 | if (ret != 0) { |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | do { |
| 158 | ret = mmc_device_state(); |
| 159 | if (ret < 0) { |
| 160 | return ret; |
| 161 | } |
| 162 | } while (ret == MMC_STATE_PRG); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int mmc_sd_switch(unsigned int bus_width) |
| 168 | { |
| 169 | int ret; |
| 170 | int retries = MMC_DEFAULT_MAX_RETRIES; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 171 | unsigned int bus_width_arg = 0; |
| 172 | |
| 173 | ret = ops->prepare(0, (uintptr_t)&scr, sizeof(scr)); |
| 174 | if (ret != 0) { |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | /* CMD55: Application Specific Command */ |
| 179 | ret = mmc_send_cmd(MMC_CMD(55), rca << RCA_SHIFT_OFFSET, |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 180 | MMC_RESPONSE_R5, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 181 | if (ret != 0) { |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | /* ACMD51: SEND_SCR */ |
| 186 | do { |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 187 | ret = mmc_send_cmd(MMC_ACMD(51), 0, MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 188 | if ((ret != 0) && (retries == 0)) { |
| 189 | ERROR("ACMD51 failed after %d retries (ret=%d)\n", |
| 190 | MMC_DEFAULT_MAX_RETRIES, ret); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | retries--; |
| 195 | } while (ret != 0); |
| 196 | |
| 197 | ret = ops->read(0, (uintptr_t)&scr, sizeof(scr)); |
| 198 | if (ret != 0) { |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | if (((scr[0] & SD_SCR_BUS_WIDTH_4) != 0U) && |
| 203 | (bus_width == MMC_BUS_WIDTH_4)) { |
| 204 | bus_width_arg = 2; |
| 205 | } |
| 206 | |
| 207 | /* CMD55: Application Specific Command */ |
| 208 | ret = mmc_send_cmd(MMC_CMD(55), rca << RCA_SHIFT_OFFSET, |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 209 | MMC_RESPONSE_R5, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 210 | if (ret != 0) { |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | /* ACMD6: SET_BUS_WIDTH */ |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 215 | ret = mmc_send_cmd(MMC_ACMD(6), bus_width_arg, MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 216 | if (ret != 0) { |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | do { |
| 221 | ret = mmc_device_state(); |
| 222 | if (ret < 0) { |
| 223 | return ret; |
| 224 | } |
| 225 | } while (ret == MMC_STATE_PRG); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int mmc_set_ios(unsigned int clk, unsigned int bus_width) |
| 231 | { |
| 232 | int ret; |
| 233 | unsigned int width = bus_width; |
| 234 | |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 235 | if (mmc_dev_info->mmc_dev_type != MMC_IS_EMMC) { |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 236 | if (width == MMC_BUS_WIDTH_8) { |
| 237 | WARN("Wrong bus config for SD-card, force to 4\n"); |
| 238 | width = MMC_BUS_WIDTH_4; |
| 239 | } |
| 240 | ret = mmc_sd_switch(width); |
| 241 | if (ret != 0) { |
| 242 | return ret; |
| 243 | } |
| 244 | } else if (mmc_csd.spec_vers == 4U) { |
| 245 | ret = mmc_set_ext_csd(CMD_EXTCSD_BUS_WIDTH, |
| 246 | (unsigned int)width); |
| 247 | if (ret != 0) { |
| 248 | return ret; |
| 249 | } |
| 250 | } else { |
| 251 | VERBOSE("Wrong MMC type or spec version\n"); |
| 252 | } |
| 253 | |
| 254 | return ops->set_ios(clk, width); |
| 255 | } |
| 256 | |
| 257 | static int mmc_fill_device_info(void) |
| 258 | { |
| 259 | unsigned long long c_size; |
| 260 | unsigned int speed_idx; |
| 261 | unsigned int nb_blocks; |
| 262 | unsigned int freq_unit; |
Antonio Nino Diaz | 5d95e0a | 2018-08-10 13:04:02 +0100 | [diff] [blame] | 263 | int ret = 0; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 264 | struct mmc_csd_sd_v2 *csd_sd_v2; |
| 265 | |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 266 | switch (mmc_dev_info->mmc_dev_type) { |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 267 | case MMC_IS_EMMC: |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 268 | mmc_dev_info->block_size = MMC_BLOCK_SIZE; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 269 | |
| 270 | ret = ops->prepare(0, (uintptr_t)&mmc_ext_csd, |
| 271 | sizeof(mmc_ext_csd)); |
| 272 | if (ret != 0) { |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | /* MMC CMD8: SEND_EXT_CSD */ |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 277 | ret = mmc_send_cmd(MMC_CMD(8), 0, MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 278 | if (ret != 0) { |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | ret = ops->read(0, (uintptr_t)&mmc_ext_csd, |
| 283 | sizeof(mmc_ext_csd)); |
| 284 | if (ret != 0) { |
| 285 | return ret; |
| 286 | } |
| 287 | |
Haojian Zhuang | 05274cb | 2018-11-21 09:19:49 +0800 | [diff] [blame] | 288 | do { |
| 289 | ret = mmc_device_state(); |
| 290 | if (ret < 0) { |
| 291 | return ret; |
| 292 | } |
| 293 | } while (ret != MMC_STATE_TRAN); |
| 294 | |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 295 | nb_blocks = (mmc_ext_csd[CMD_EXTCSD_SEC_CNT] << 0) | |
| 296 | (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 1] << 8) | |
| 297 | (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 2] << 16) | |
| 298 | (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 3] << 24); |
| 299 | |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 300 | mmc_dev_info->device_size = (unsigned long long)nb_blocks * |
| 301 | mmc_dev_info->block_size; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 302 | |
| 303 | break; |
| 304 | |
| 305 | case MMC_IS_SD: |
| 306 | /* |
| 307 | * Use the same mmc_csd struct, as required fields here |
| 308 | * (READ_BL_LEN, C_SIZE, CSIZE_MULT) are common with eMMC. |
| 309 | */ |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 310 | mmc_dev_info->block_size = BIT_32(mmc_csd.read_bl_len); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 311 | |
| 312 | c_size = ((unsigned long long)mmc_csd.c_size_high << 2U) | |
| 313 | (unsigned long long)mmc_csd.c_size_low; |
| 314 | assert(c_size != 0xFFFU); |
| 315 | |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 316 | mmc_dev_info->device_size = (c_size + 1U) * |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 317 | BIT_64(mmc_csd.c_size_mult + 2U) * |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 318 | mmc_dev_info->block_size; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 319 | |
| 320 | break; |
| 321 | |
| 322 | case MMC_IS_SD_HC: |
| 323 | assert(mmc_csd.csd_structure == 1U); |
| 324 | |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 325 | mmc_dev_info->block_size = MMC_BLOCK_SIZE; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 326 | |
| 327 | /* Need to use mmc_csd_sd_v2 struct */ |
| 328 | csd_sd_v2 = (struct mmc_csd_sd_v2 *)&mmc_csd; |
| 329 | c_size = ((unsigned long long)csd_sd_v2->c_size_high << 16) | |
| 330 | (unsigned long long)csd_sd_v2->c_size_low; |
| 331 | |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 332 | mmc_dev_info->device_size = (c_size + 1U) << MULT_BY_512K_SHIFT; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 333 | |
| 334 | break; |
| 335 | |
| 336 | default: |
| 337 | ret = -EINVAL; |
| 338 | break; |
| 339 | } |
| 340 | |
Yann Gautier | e8eb33f | 2019-01-17 11:17:05 +0100 | [diff] [blame] | 341 | if (ret < 0) { |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | speed_idx = (mmc_csd.tran_speed & CSD_TRAN_SPEED_MULT_MASK) >> |
| 346 | CSD_TRAN_SPEED_MULT_SHIFT; |
| 347 | |
| 348 | assert(speed_idx > 0U); |
| 349 | |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 350 | if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) { |
| 351 | mmc_dev_info->max_bus_freq = tran_speed_base[speed_idx]; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 352 | } else { |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 353 | mmc_dev_info->max_bus_freq = sd_tran_speed_base[speed_idx]; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | freq_unit = mmc_csd.tran_speed & CSD_TRAN_SPEED_UNIT_MASK; |
| 357 | while (freq_unit != 0U) { |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 358 | mmc_dev_info->max_bus_freq *= 10U; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 359 | --freq_unit; |
| 360 | } |
| 361 | |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 362 | mmc_dev_info->max_bus_freq *= 10000U; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
Yann Gautier | 0307b71 | 2019-06-12 15:55:37 +0200 | [diff] [blame] | 367 | static int sd_switch(unsigned int mode, unsigned char group, |
| 368 | unsigned char func) |
| 369 | { |
| 370 | unsigned int group_shift = (group - 1U) * 4U; |
| 371 | unsigned int group_mask = GENMASK(group_shift + 3U, group_shift); |
| 372 | unsigned int arg; |
| 373 | int ret; |
| 374 | |
| 375 | ret = ops->prepare(0, (uintptr_t)&sd_switch_func_status, |
| 376 | sizeof(sd_switch_func_status)); |
| 377 | if (ret != 0) { |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | /* MMC CMD6: SWITCH_FUNC */ |
| 382 | arg = mode | SD_SWITCH_ALL_GROUPS_MASK; |
| 383 | arg &= ~group_mask; |
| 384 | arg |= func << group_shift; |
| 385 | ret = mmc_send_cmd(MMC_CMD(6), arg, MMC_RESPONSE_R1, NULL); |
| 386 | if (ret != 0) { |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | return ops->read(0, (uintptr_t)&sd_switch_func_status, |
| 391 | sizeof(sd_switch_func_status)); |
| 392 | } |
| 393 | |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 394 | static int sd_send_op_cond(void) |
| 395 | { |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 396 | int n; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 397 | unsigned int resp_data[4]; |
| 398 | |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 399 | for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) { |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 400 | int ret; |
| 401 | |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 402 | /* CMD55: Application Specific Command */ |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 403 | ret = mmc_send_cmd(MMC_CMD(55), 0, MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 404 | if (ret != 0) { |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | /* ACMD41: SD_SEND_OP_COND */ |
Tien Hock, Loh | 313e900 | 2019-03-07 11:34:20 +0800 | [diff] [blame] | 409 | ret = mmc_send_cmd(MMC_ACMD(41), OCR_HCS | |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 410 | mmc_dev_info->ocr_voltage, MMC_RESPONSE_R3, |
Tien Hock, Loh | 313e900 | 2019-03-07 11:34:20 +0800 | [diff] [blame] | 411 | &resp_data[0]); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 412 | if (ret != 0) { |
| 413 | return ret; |
| 414 | } |
| 415 | |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 416 | if ((resp_data[0] & OCR_POWERUP) != 0U) { |
| 417 | mmc_ocr_value = resp_data[0]; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 418 | |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 419 | if ((mmc_ocr_value & OCR_HCS) != 0U) { |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 420 | mmc_dev_info->mmc_dev_type = MMC_IS_SD_HC; |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 421 | } else { |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 422 | mmc_dev_info->mmc_dev_type = MMC_IS_SD; |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 423 | } |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 424 | |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
Yann Gautier | 38d8035 | 2019-08-16 16:49:41 +0200 | [diff] [blame] | 428 | mdelay(10); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 431 | ERROR("ACMD41 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES); |
| 432 | |
| 433 | return -EIO; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 436 | static int mmc_reset_to_idle(void) |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 437 | { |
| 438 | int ret; |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 439 | |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 440 | /* CMD0: reset to IDLE */ |
| 441 | ret = mmc_send_cmd(MMC_CMD(0), 0, 0, NULL); |
| 442 | if (ret != 0) { |
| 443 | return ret; |
| 444 | } |
| 445 | |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 446 | mdelay(2); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 447 | |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static int mmc_send_op_cond(void) |
| 452 | { |
| 453 | int ret, n; |
| 454 | unsigned int resp_data[4]; |
| 455 | |
Yann Gautier | 4c7888a | 2018-11-29 15:43:37 +0100 | [diff] [blame] | 456 | ret = mmc_reset_to_idle(); |
| 457 | if (ret != 0) { |
| 458 | return ret; |
Yann Gautier | e77be52 | 2021-03-23 13:30:43 +0100 | [diff] [blame] | 459 | } |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 460 | |
| 461 | for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) { |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 462 | ret = mmc_send_cmd(MMC_CMD(1), OCR_SECTOR_MODE | |
| 463 | OCR_VDD_MIN_2V7 | OCR_VDD_MIN_1V7, |
Bryan O'Donoghue | e62f419 | 2018-08-15 15:59:07 +0100 | [diff] [blame] | 464 | MMC_RESPONSE_R3, &resp_data[0]); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 465 | if (ret != 0) { |
| 466 | return ret; |
| 467 | } |
| 468 | |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 469 | if ((resp_data[0] & OCR_POWERUP) != 0U) { |
| 470 | mmc_ocr_value = resp_data[0]; |
| 471 | return 0; |
| 472 | } |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 473 | |
Joakim Bech | 8428a04 | 2018-12-18 10:09:02 +0100 | [diff] [blame] | 474 | mdelay(10); |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 475 | } |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 476 | |
Yann Gautier | de6959e | 2018-08-02 13:46:17 +0200 | [diff] [blame] | 477 | ERROR("CMD1 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES); |
| 478 | |
| 479 | return -EIO; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static int mmc_enumerate(unsigned int clk, unsigned int bus_width) |
| 483 | { |
| 484 | int ret; |
| 485 | unsigned int resp_data[4]; |
| 486 | |
| 487 | ops->init(); |
| 488 | |
Yann Gautier | 4c7888a | 2018-11-29 15:43:37 +0100 | [diff] [blame] | 489 | ret = mmc_reset_to_idle(); |
| 490 | if (ret != 0) { |
| 491 | return ret; |
Yann Gautier | e77be52 | 2021-03-23 13:30:43 +0100 | [diff] [blame] | 492 | } |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 493 | |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 494 | if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) { |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 495 | ret = mmc_send_op_cond(); |
Haojian Zhuang | ed59cd5 | 2018-08-02 14:48:17 +0800 | [diff] [blame] | 496 | } else { |
| 497 | /* CMD8: Send Interface Condition Command */ |
| 498 | ret = mmc_send_cmd(MMC_CMD(8), VHS_2_7_3_6_V | CMD8_CHECK_PATTERN, |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 499 | MMC_RESPONSE_R5, &resp_data[0]); |
Haojian Zhuang | ed59cd5 | 2018-08-02 14:48:17 +0800 | [diff] [blame] | 500 | |
| 501 | if ((ret == 0) && ((resp_data[0] & 0xffU) == CMD8_CHECK_PATTERN)) { |
| 502 | ret = sd_send_op_cond(); |
| 503 | } |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 504 | } |
| 505 | if (ret != 0) { |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | /* CMD2: Card Identification */ |
Shawn Guo | ab3b62b | 2018-09-28 14:21:01 +0800 | [diff] [blame] | 510 | ret = mmc_send_cmd(MMC_CMD(2), 0, MMC_RESPONSE_R2, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 511 | if (ret != 0) { |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | /* CMD3: Set Relative Address */ |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 516 | if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) { |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 517 | rca = MMC_FIX_RCA; |
| 518 | ret = mmc_send_cmd(MMC_CMD(3), rca << RCA_SHIFT_OFFSET, |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 519 | MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 520 | if (ret != 0) { |
| 521 | return ret; |
| 522 | } |
| 523 | } else { |
| 524 | ret = mmc_send_cmd(MMC_CMD(3), 0, |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 525 | MMC_RESPONSE_R6, &resp_data[0]); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 526 | if (ret != 0) { |
| 527 | return ret; |
| 528 | } |
| 529 | |
| 530 | rca = (resp_data[0] & 0xFFFF0000U) >> 16; |
| 531 | } |
| 532 | |
| 533 | /* CMD9: CSD Register */ |
| 534 | ret = mmc_send_cmd(MMC_CMD(9), rca << RCA_SHIFT_OFFSET, |
Shawn Guo | ab3b62b | 2018-09-28 14:21:01 +0800 | [diff] [blame] | 535 | MMC_RESPONSE_R2, &resp_data[0]); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 536 | if (ret != 0) { |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | memcpy(&mmc_csd, &resp_data, sizeof(resp_data)); |
| 541 | |
| 542 | /* CMD7: Select Card */ |
| 543 | ret = mmc_send_cmd(MMC_CMD(7), rca << RCA_SHIFT_OFFSET, |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 544 | MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 545 | if (ret != 0) { |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | do { |
| 550 | ret = mmc_device_state(); |
| 551 | if (ret < 0) { |
| 552 | return ret; |
| 553 | } |
| 554 | } while (ret != MMC_STATE_TRAN); |
| 555 | |
Haojian Zhuang | d618b27 | 2018-08-04 18:04:30 +0800 | [diff] [blame] | 556 | ret = mmc_set_ios(clk, bus_width); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 557 | if (ret != 0) { |
| 558 | return ret; |
| 559 | } |
| 560 | |
Yann Gautier | 0307b71 | 2019-06-12 15:55:37 +0200 | [diff] [blame] | 561 | ret = mmc_fill_device_info(); |
| 562 | if (ret != 0) { |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | if (is_sd_cmd6_enabled() && |
| 567 | (mmc_dev_info->mmc_dev_type == MMC_IS_SD_HC)) { |
| 568 | /* Try to switch to High Speed Mode */ |
| 569 | ret = sd_switch(SD_SWITCH_FUNC_CHECK, 1U, 1U); |
| 570 | if (ret != 0) { |
| 571 | return ret; |
| 572 | } |
| 573 | |
| 574 | if ((sd_switch_func_status.support_g1 & BIT(9)) == 0U) { |
| 575 | /* High speed not supported, keep default speed */ |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | ret = sd_switch(SD_SWITCH_FUNC_SWITCH, 1U, 1U); |
| 580 | if (ret != 0) { |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | if ((sd_switch_func_status.sel_g2_g1 & 0x1U) == 0U) { |
| 585 | /* Cannot switch to high speed, keep default speed */ |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | mmc_dev_info->max_bus_freq = 50000000U; |
| 590 | ret = ops->set_ios(clk, bus_width); |
| 591 | } |
| 592 | |
| 593 | return ret; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 594 | } |
| 595 | |
Haojian Zhuang | d87f0b7 | 2018-08-02 14:49:51 +0800 | [diff] [blame] | 596 | size_t mmc_read_blocks(int lba, uintptr_t buf, size_t size) |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 597 | { |
| 598 | int ret; |
| 599 | unsigned int cmd_idx, cmd_arg; |
| 600 | |
| 601 | assert((ops != NULL) && |
| 602 | (ops->read != NULL) && |
| 603 | (size != 0U) && |
| 604 | ((size & MMC_BLOCK_MASK) == 0U)); |
| 605 | |
| 606 | ret = ops->prepare(lba, buf, size); |
| 607 | if (ret != 0) { |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | if (is_cmd23_enabled()) { |
| 612 | /* Set block count */ |
| 613 | ret = mmc_send_cmd(MMC_CMD(23), size / MMC_BLOCK_SIZE, |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 614 | MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 615 | if (ret != 0) { |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | cmd_idx = MMC_CMD(18); |
| 620 | } else { |
| 621 | if (size > MMC_BLOCK_SIZE) { |
| 622 | cmd_idx = MMC_CMD(18); |
| 623 | } else { |
| 624 | cmd_idx = MMC_CMD(17); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | if (((mmc_ocr_value & OCR_ACCESS_MODE_MASK) == OCR_BYTE_MODE) && |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 629 | (mmc_dev_info->mmc_dev_type != MMC_IS_SD_HC)) { |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 630 | cmd_arg = lba * MMC_BLOCK_SIZE; |
| 631 | } else { |
| 632 | cmd_arg = lba; |
| 633 | } |
| 634 | |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 635 | ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 636 | if (ret != 0) { |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | ret = ops->read(lba, buf, size); |
| 641 | if (ret != 0) { |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | /* Wait buffer empty */ |
| 646 | do { |
| 647 | ret = mmc_device_state(); |
| 648 | if (ret < 0) { |
| 649 | return 0; |
| 650 | } |
| 651 | } while ((ret != MMC_STATE_TRAN) && (ret != MMC_STATE_DATA)); |
| 652 | |
| 653 | if (!is_cmd23_enabled() && (size > MMC_BLOCK_SIZE)) { |
Bryan O'Donoghue | e6b56cc | 2018-08-15 16:25:30 +0100 | [diff] [blame] | 654 | ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 655 | if (ret != 0) { |
| 656 | return 0; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | return size; |
| 661 | } |
| 662 | |
Haojian Zhuang | d87f0b7 | 2018-08-02 14:49:51 +0800 | [diff] [blame] | 663 | size_t mmc_write_blocks(int lba, const uintptr_t buf, size_t size) |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 664 | { |
| 665 | int ret; |
| 666 | unsigned int cmd_idx, cmd_arg; |
| 667 | |
| 668 | assert((ops != NULL) && |
| 669 | (ops->write != NULL) && |
| 670 | (size != 0U) && |
| 671 | ((buf & MMC_BLOCK_MASK) == 0U) && |
| 672 | ((size & MMC_BLOCK_MASK) == 0U)); |
| 673 | |
| 674 | ret = ops->prepare(lba, buf, size); |
| 675 | if (ret != 0) { |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | if (is_cmd23_enabled()) { |
| 680 | /* Set block count */ |
| 681 | ret = mmc_send_cmd(MMC_CMD(23), size / MMC_BLOCK_SIZE, |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 682 | MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 683 | if (ret != 0) { |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | cmd_idx = MMC_CMD(25); |
| 688 | } else { |
| 689 | if (size > MMC_BLOCK_SIZE) { |
| 690 | cmd_idx = MMC_CMD(25); |
| 691 | } else { |
| 692 | cmd_idx = MMC_CMD(24); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | if ((mmc_ocr_value & OCR_ACCESS_MODE_MASK) == OCR_BYTE_MODE) { |
| 697 | cmd_arg = lba * MMC_BLOCK_SIZE; |
| 698 | } else { |
| 699 | cmd_arg = lba; |
| 700 | } |
| 701 | |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 702 | ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 703 | if (ret != 0) { |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | ret = ops->write(lba, buf, size); |
| 708 | if (ret != 0) { |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | /* Wait buffer empty */ |
| 713 | do { |
| 714 | ret = mmc_device_state(); |
| 715 | if (ret < 0) { |
| 716 | return 0; |
| 717 | } |
| 718 | } while ((ret != MMC_STATE_TRAN) && (ret != MMC_STATE_RCV)); |
| 719 | |
| 720 | if (!is_cmd23_enabled() && (size > MMC_BLOCK_SIZE)) { |
Bryan O'Donoghue | e6b56cc | 2018-08-15 16:25:30 +0100 | [diff] [blame] | 721 | ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 722 | if (ret != 0) { |
| 723 | return 0; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | return size; |
| 728 | } |
| 729 | |
Haojian Zhuang | d87f0b7 | 2018-08-02 14:49:51 +0800 | [diff] [blame] | 730 | size_t mmc_erase_blocks(int lba, size_t size) |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 731 | { |
| 732 | int ret; |
| 733 | |
| 734 | assert(ops != NULL); |
| 735 | assert((size != 0U) && ((size & MMC_BLOCK_MASK) == 0U)); |
| 736 | |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 737 | ret = mmc_send_cmd(MMC_CMD(35), lba, MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 738 | if (ret != 0) { |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | ret = mmc_send_cmd(MMC_CMD(36), lba + (size / MMC_BLOCK_SIZE) - 1U, |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 743 | MMC_RESPONSE_R1, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 744 | if (ret != 0) { |
| 745 | return 0; |
| 746 | } |
| 747 | |
Yann Gautier | f2e8b16 | 2018-09-28 16:48:37 +0200 | [diff] [blame] | 748 | ret = mmc_send_cmd(MMC_CMD(38), lba, MMC_RESPONSE_R1B, NULL); |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 749 | if (ret != 0) { |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | do { |
| 754 | ret = mmc_device_state(); |
| 755 | if (ret < 0) { |
| 756 | return 0; |
| 757 | } |
| 758 | } while (ret != MMC_STATE_TRAN); |
| 759 | |
| 760 | return size; |
| 761 | } |
| 762 | |
Vyacheslav Yurkov | b3d5f34 | 2021-03-30 08:16:20 +0200 | [diff] [blame] | 763 | static int mmc_part_switch(unsigned int part_type) |
| 764 | { |
| 765 | uint8_t part_config = mmc_ext_csd[CMD_EXTCSD_PARTITION_CONFIG]; |
| 766 | |
| 767 | part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
| 768 | part_config |= part_type; |
| 769 | |
| 770 | return mmc_send_part_switch_cmd(part_config); |
| 771 | } |
| 772 | |
| 773 | static unsigned char mmc_current_boot_part(void) |
| 774 | { |
| 775 | return PART_CFG_CURRENT_BOOT_PARTITION(mmc_ext_csd[CMD_EXTCSD_PARTITION_CONFIG]); |
| 776 | } |
| 777 | |
Ahmad Fatoum | ee8f342 | 2022-05-23 17:06:37 +0200 | [diff] [blame] | 778 | int mmc_part_switch_current_boot(void) |
Vyacheslav Yurkov | b3d5f34 | 2021-03-30 08:16:20 +0200 | [diff] [blame] | 779 | { |
Vyacheslav Yurkov | b3d5f34 | 2021-03-30 08:16:20 +0200 | [diff] [blame] | 780 | unsigned char current_boot_part = mmc_current_boot_part(); |
Ahmad Fatoum | ee8f342 | 2022-05-23 17:06:37 +0200 | [diff] [blame] | 781 | int ret; |
Vyacheslav Yurkov | b3d5f34 | 2021-03-30 08:16:20 +0200 | [diff] [blame] | 782 | |
| 783 | if (current_boot_part != 1U && |
| 784 | current_boot_part != 2U) { |
| 785 | ERROR("Got unexpected value for active boot partition, %u\n", current_boot_part); |
Ahmad Fatoum | ee8f342 | 2022-05-23 17:06:37 +0200 | [diff] [blame] | 786 | return -EIO; |
Vyacheslav Yurkov | b3d5f34 | 2021-03-30 08:16:20 +0200 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | ret = mmc_part_switch(current_boot_part); |
| 790 | if (ret < 0) { |
| 791 | ERROR("Failed to switch to boot partition, %d\n", ret); |
Ahmad Fatoum | ee8f342 | 2022-05-23 17:06:37 +0200 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | int mmc_part_switch_user(void) |
| 798 | { |
| 799 | int ret; |
| 800 | |
Ahmad Fatoum | b61eb75 | 2022-05-31 10:03:04 +0200 | [diff] [blame] | 801 | ret = mmc_part_switch(PART_CFG_BOOT_PARTITION_NO_ACCESS); |
Ahmad Fatoum | ee8f342 | 2022-05-23 17:06:37 +0200 | [diff] [blame] | 802 | if (ret < 0) { |
| 803 | ERROR("Failed to switch to user partition, %d\n", ret); |
| 804 | } |
| 805 | |
| 806 | return ret; |
| 807 | } |
| 808 | |
Yann Gautier | 053d7f2 | 2022-09-01 19:23:39 +0200 | [diff] [blame] | 809 | size_t mmc_boot_part_size(void) |
| 810 | { |
| 811 | return mmc_ext_csd[CMD_EXTCSD_BOOT_SIZE_MULT] * SZ_128K; |
| 812 | } |
| 813 | |
Ahmad Fatoum | ee8f342 | 2022-05-23 17:06:37 +0200 | [diff] [blame] | 814 | size_t mmc_boot_part_read_blocks(int lba, uintptr_t buf, size_t size) |
| 815 | { |
| 816 | size_t size_read; |
| 817 | int ret; |
| 818 | |
| 819 | ret = mmc_part_switch_current_boot(); |
| 820 | if (ret < 0) { |
Vyacheslav Yurkov | b3d5f34 | 2021-03-30 08:16:20 +0200 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | size_read = mmc_read_blocks(lba, buf, size); |
| 825 | |
Ahmad Fatoum | ee8f342 | 2022-05-23 17:06:37 +0200 | [diff] [blame] | 826 | ret = mmc_part_switch_user(); |
Vyacheslav Yurkov | b3d5f34 | 2021-03-30 08:16:20 +0200 | [diff] [blame] | 827 | if (ret < 0) { |
Vyacheslav Yurkov | b3d5f34 | 2021-03-30 08:16:20 +0200 | [diff] [blame] | 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | return size_read; |
| 832 | } |
| 833 | |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 834 | int mmc_init(const struct mmc_ops *ops_ptr, unsigned int clk, |
| 835 | unsigned int width, unsigned int flags, |
| 836 | struct mmc_device_info *device_info) |
| 837 | { |
| 838 | assert((ops_ptr != NULL) && |
| 839 | (ops_ptr->init != NULL) && |
| 840 | (ops_ptr->send_cmd != NULL) && |
| 841 | (ops_ptr->set_ios != NULL) && |
| 842 | (ops_ptr->prepare != NULL) && |
| 843 | (ops_ptr->read != NULL) && |
| 844 | (ops_ptr->write != NULL) && |
| 845 | (device_info != NULL) && |
| 846 | (clk != 0) && |
| 847 | ((width == MMC_BUS_WIDTH_1) || |
| 848 | (width == MMC_BUS_WIDTH_4) || |
| 849 | (width == MMC_BUS_WIDTH_8) || |
| 850 | (width == MMC_BUS_WIDTH_DDR_4) || |
| 851 | (width == MMC_BUS_WIDTH_DDR_8))); |
| 852 | |
| 853 | ops = ops_ptr; |
| 854 | mmc_flags = flags; |
Yann Gautier | d8b6b8e | 2021-03-22 14:02:54 +0100 | [diff] [blame] | 855 | mmc_dev_info = device_info; |
Yann Gautier | 1e5e85a | 2018-07-03 18:32:12 +0200 | [diff] [blame] | 856 | |
| 857 | return mmc_enumerate(clk, width); |
| 858 | } |