Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, Freescale Semiconductor, Inc |
| 3 | * Andy Fleming |
| 4 | * |
| 5 | * Copyright 2013 Google Inc. All rights reserved. |
| 6 | * Copyright 2017 Intel Corporation |
| 7 | * |
| 8 | * MultiMediaCard (MMC), eMMC and Secure Digital (SD) common initialization |
| 9 | * code which brings the card into the standby state. This code is controller |
| 10 | * independent. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | */ |
| 22 | |
| 23 | #include <assert.h> |
Lee Leahy | 48dbc66 | 2017-05-08 16:56:03 -0700 | [diff] [blame] | 24 | #include <commonlib/storage.h> |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 25 | #include <delay.h> |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 26 | #include <endian.h> |
| 27 | #include "mmc.h" |
| 28 | #include "sd_mmc.h" |
| 29 | #include "storage.h" |
| 30 | #include <string.h> |
| 31 | #include <timer.h> |
| 32 | |
| 33 | uint64_t sd_mmc_extract_uint32_bits(const uint32_t *array, int start, int count) |
| 34 | { |
| 35 | int i; |
| 36 | uint64_t value = 0; |
| 37 | |
| 38 | for (i = 0; i < count; i++, start++) { |
| 39 | value <<= 1; |
| 40 | value |= (array[start / 32] >> (31 - (start % 32))) & 0x1; |
| 41 | } |
| 42 | return value; |
| 43 | } |
| 44 | |
| 45 | static uint32_t sd_mmc_calculate_transfer_speed(uint32_t csd0) |
| 46 | { |
| 47 | uint32_t mult, freq; |
| 48 | |
| 49 | /* frequency bases, divided by 10 to be nice to platforms without |
| 50 | * floating point */ |
| 51 | static const int fbase[] = { |
| 52 | 10000, |
| 53 | 100000, |
| 54 | 1000000, |
| 55 | 10000000, |
| 56 | }; |
| 57 | /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice |
| 58 | * to platforms without floating point. */ |
| 59 | static const int multipliers[] = { |
| 60 | 0, // reserved |
| 61 | 10, |
| 62 | 12, |
| 63 | 13, |
| 64 | 15, |
| 65 | 20, |
| 66 | 25, |
| 67 | 30, |
| 68 | 35, |
| 69 | 40, |
| 70 | 45, |
| 71 | 50, |
| 72 | 55, |
| 73 | 60, |
| 74 | 70, |
| 75 | 80, |
| 76 | }; |
| 77 | |
| 78 | /* divide frequency by 10, since the mults are 10x bigger */ |
| 79 | freq = fbase[csd0 & 0x7]; |
| 80 | mult = multipliers[(csd0 >> 3) & 0xf]; |
| 81 | return freq * mult; |
| 82 | } |
| 83 | |
Bora Guvendik | e1416fd | 2018-03-08 16:18:54 -0800 | [diff] [blame] | 84 | int sd_mmc_go_idle(struct storage_media *media) |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 85 | { |
| 86 | struct sd_mmc_ctrlr *ctrlr = media->ctrlr; |
| 87 | |
| 88 | // Some cards can't accept idle commands without delay. |
| 89 | if (ctrlr->mdelay_before_cmd0) |
| 90 | mdelay(ctrlr->mdelay_before_cmd0); |
| 91 | |
| 92 | struct mmc_command cmd; |
| 93 | cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; |
| 94 | cmd.cmdarg = 0; |
| 95 | cmd.resp_type = CARD_RSP_NONE; |
| 96 | cmd.flags = 0; |
| 97 | |
| 98 | int err = ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 99 | if (err) |
| 100 | return err; |
| 101 | |
| 102 | // Some cards need more than half second to respond to next command (ex, |
| 103 | // SEND_OP_COND). |
| 104 | if (ctrlr->mdelay_after_cmd0) |
| 105 | mdelay(ctrlr->mdelay_after_cmd0); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int sd_mmc_send_status(struct storage_media *media, ssize_t tries) |
| 111 | { |
| 112 | struct mmc_command cmd; |
| 113 | struct sd_mmc_ctrlr *ctrlr = media->ctrlr; |
| 114 | |
| 115 | cmd.cmdidx = MMC_CMD_SEND_STATUS; |
| 116 | cmd.resp_type = CARD_RSP_R1; |
| 117 | cmd.cmdarg = media->rca << 16; |
| 118 | cmd.flags = 0; |
| 119 | |
| 120 | while (tries--) { |
| 121 | int err = ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 122 | if (err) |
| 123 | return err; |
| 124 | else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) |
| 125 | break; |
| 126 | else if (cmd.response[0] & MMC_STATUS_MASK) { |
| 127 | sd_mmc_error("Status Error: %#8.8x\n", cmd.response[0]); |
| 128 | return CARD_COMM_ERR; |
| 129 | } |
| 130 | |
| 131 | udelay(100); |
| 132 | } |
| 133 | |
| 134 | sd_mmc_trace("CURR STATE:%d\n", |
| 135 | (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9); |
| 136 | |
| 137 | if (tries < 0) { |
| 138 | sd_mmc_error("Timeout waiting card ready\n"); |
| 139 | return CARD_TIMEOUT; |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | int sd_mmc_set_blocklen(struct sd_mmc_ctrlr *ctrlr, int len) |
| 145 | { |
| 146 | struct mmc_command cmd; |
| 147 | cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; |
| 148 | cmd.resp_type = CARD_RSP_R1; |
| 149 | cmd.cmdarg = len; |
| 150 | cmd.flags = 0; |
| 151 | |
| 152 | return ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 153 | } |
| 154 | |
| 155 | int sd_mmc_enter_standby(struct storage_media *media) |
| 156 | { |
| 157 | struct mmc_command cmd; |
| 158 | struct sd_mmc_ctrlr *ctrlr = media->ctrlr; |
| 159 | int err; |
| 160 | |
| 161 | SET_BUS_WIDTH(ctrlr, 1); |
| 162 | SET_CLOCK(ctrlr, 1); |
| 163 | |
| 164 | /* Reset the Card */ |
| 165 | err = sd_mmc_go_idle(media); |
| 166 | if (err) |
| 167 | return err; |
| 168 | |
| 169 | /* Test for SD version 2 */ |
| 170 | err = CARD_TIMEOUT; |
Lee Leahy | 48dbc66 | 2017-05-08 16:56:03 -0700 | [diff] [blame] | 171 | if (IS_ENABLED(CONFIG_COMMONLIB_STORAGE_SD)) { |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 172 | err = sd_send_if_cond(media); |
| 173 | |
| 174 | /* Get SD card operating condition */ |
| 175 | if (!err) |
| 176 | err = sd_send_op_cond(media); |
| 177 | } |
| 178 | |
| 179 | /* If the command timed out, we check for an MMC card */ |
Lee Leahy | 48dbc66 | 2017-05-08 16:56:03 -0700 | [diff] [blame] | 180 | if (IS_ENABLED(CONFIG_COMMONLIB_STORAGE_MMC) && (err == CARD_TIMEOUT)) { |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 181 | /* Some cards seem to need this */ |
| 182 | sd_mmc_go_idle(media); |
| 183 | |
| 184 | err = mmc_send_op_cond(media); |
| 185 | if (err == CARD_IN_PROGRESS) |
| 186 | err = mmc_complete_op_cond(media); |
| 187 | } |
| 188 | |
| 189 | if (err) { |
| 190 | sd_mmc_error( |
| 191 | "Card did not respond to voltage select!\n"); |
| 192 | return CARD_UNUSABLE_ERR; |
| 193 | } |
| 194 | |
| 195 | /* Put the Card in Identify Mode */ |
| 196 | cmd.cmdidx = MMC_CMD_ALL_SEND_CID; |
| 197 | cmd.resp_type = CARD_RSP_R2; |
| 198 | cmd.cmdarg = 0; |
| 199 | cmd.flags = 0; |
| 200 | err = ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 201 | if (err) |
| 202 | return err; |
| 203 | memcpy(media->cid, cmd.response, sizeof(media->cid)); |
| 204 | |
| 205 | /* |
| 206 | * For MMC cards, set the Relative Address. |
| 207 | * For SD cards, get the Relatvie Address. |
| 208 | * This also puts the cards into Standby State |
| 209 | */ |
| 210 | cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; |
| 211 | cmd.cmdarg = media->rca << 16; |
| 212 | cmd.resp_type = CARD_RSP_R6; |
| 213 | cmd.flags = 0; |
| 214 | err = ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 215 | if (err) |
| 216 | return err; |
| 217 | if (IS_SD(media)) |
| 218 | media->rca = (cmd.response[0] >> 16) & 0xffff; |
| 219 | |
| 220 | /* Get the Card-Specific Data */ |
| 221 | cmd.cmdidx = MMC_CMD_SEND_CSD; |
| 222 | cmd.resp_type = CARD_RSP_R2; |
| 223 | cmd.cmdarg = media->rca << 16; |
| 224 | cmd.flags = 0; |
| 225 | err = ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 226 | |
| 227 | /* Waiting for the ready status */ |
| 228 | sd_mmc_send_status(media, SD_MMC_IO_RETRIES); |
| 229 | if (err) |
| 230 | return err; |
| 231 | |
| 232 | memcpy(media->csd, cmd.response, sizeof(media->csd)); |
| 233 | if (media->version == MMC_VERSION_UNKNOWN) { |
| 234 | int version = sd_mmc_extract_uint32_bits(media->csd, 2, 4); |
| 235 | switch (version) { |
| 236 | case 0: |
| 237 | media->version = MMC_VERSION_1_2; |
| 238 | break; |
| 239 | case 1: |
| 240 | media->version = MMC_VERSION_1_4; |
| 241 | break; |
| 242 | case 2: |
| 243 | media->version = MMC_VERSION_2_2; |
| 244 | break; |
| 245 | case 3: |
| 246 | media->version = MMC_VERSION_3; |
| 247 | break; |
| 248 | case 4: |
| 249 | media->version = MMC_VERSION_4; |
| 250 | break; |
| 251 | default: |
| 252 | media->version = MMC_VERSION_1_2; |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | media->tran_speed = sd_mmc_calculate_transfer_speed(media->csd[0]); |
| 257 | |
| 258 | /* Determine the read and write block lengths */ |
| 259 | media->read_bl_len = 1 << sd_mmc_extract_uint32_bits(media->csd, 44, 4); |
| 260 | if (IS_SD(media)) |
| 261 | media->write_bl_len = media->read_bl_len; |
| 262 | else |
| 263 | media->write_bl_len = |
| 264 | 1 << sd_mmc_extract_uint32_bits(media->csd, 102, 4); |
| 265 | |
| 266 | sd_mmc_debug("mmc media info: version=%#x, tran_speed=%d\n", |
| 267 | media->version, (int)media->tran_speed); |
| 268 | |
| 269 | return 0; |
| 270 | } |