Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Patrick Georgi | afd4c87 | 2020-05-05 23:43:18 +0200 | [diff] [blame] | 2 | /* |
Martin Roth | 0443ac2 | 2019-08-30 21:29:41 -0600 | [diff] [blame] | 3 | * Secure Digital (SD) card specific support code |
| 4 | * This code is controller independent |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Lee Leahy | 48dbc66 | 2017-05-08 16:56:03 -0700 | [diff] [blame] | 7 | #include <commonlib/sd_mmc_ctrlr.h> |
| 8 | #include <commonlib/storage.h> |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 9 | #include <delay.h> |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 10 | #include <endian.h> |
Elyes HAOUAS | add76f9 | 2019-03-21 09:55:49 +0100 | [diff] [blame] | 11 | |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 12 | #include "sd_mmc.h" |
| 13 | #include "storage.h" |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 14 | |
| 15 | int sd_send_if_cond(struct storage_media *media) |
| 16 | { |
| 17 | struct mmc_command cmd; |
| 18 | struct sd_mmc_ctrlr *ctrlr = media->ctrlr; |
| 19 | |
| 20 | cmd.cmdidx = SD_CMD_SEND_IF_COND; |
| 21 | /* Set if controller supports voltages between 2.7 and 3.6 V. */ |
| 22 | cmd.cmdarg = ((ctrlr->voltages & 0xff8000) != 0) << 8 | 0xaa; |
| 23 | cmd.resp_type = CARD_RSP_R7; |
| 24 | cmd.flags = 0; |
| 25 | int err = ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 26 | if (err) |
| 27 | return err; |
| 28 | |
| 29 | if ((cmd.response[0] & 0xff) != 0xaa) |
| 30 | return CARD_UNUSABLE_ERR; |
| 31 | media->version = SD_VERSION_2; |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | int sd_send_op_cond(struct storage_media *media) |
| 36 | { |
| 37 | int err; |
| 38 | struct mmc_command cmd; |
| 39 | struct sd_mmc_ctrlr *ctrlr = media->ctrlr; |
| 40 | |
| 41 | int tries = SD_MMC_IO_RETRIES; |
| 42 | while (tries--) { |
| 43 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 44 | cmd.resp_type = CARD_RSP_R1; |
| 45 | cmd.cmdarg = 0; |
| 46 | cmd.flags = 0; |
| 47 | |
| 48 | err = ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 49 | if (err) |
| 50 | return err; |
| 51 | |
| 52 | cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; |
| 53 | cmd.resp_type = CARD_RSP_R3; |
| 54 | |
| 55 | /* |
| 56 | * Most cards do not answer if some reserved bits |
| 57 | * in the ocr are set. However, Some controller |
| 58 | * can set bit 7 (reserved for low voltages), but |
| 59 | * how to manage low voltages SD card is not yet |
| 60 | * specified. |
| 61 | */ |
| 62 | cmd.cmdarg = (ctrlr->voltages & 0xff8000); |
| 63 | |
| 64 | if (media->version == SD_VERSION_2) |
| 65 | cmd.cmdarg |= OCR_HCS; |
| 66 | |
| 67 | err = ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 68 | if (err) |
| 69 | return err; |
| 70 | |
| 71 | // OCR_BUSY means "initialization complete". |
| 72 | if (cmd.response[0] & OCR_BUSY) |
| 73 | break; |
| 74 | |
| 75 | udelay(100); |
| 76 | } |
| 77 | if (tries < 0) |
| 78 | return CARD_UNUSABLE_ERR; |
| 79 | |
| 80 | if (media->version != SD_VERSION_2) |
| 81 | media->version = SD_VERSION_1_0; |
| 82 | |
| 83 | media->ocr = cmd.response[0]; |
| 84 | media->high_capacity = ((media->ocr & OCR_HCS) == OCR_HCS); |
| 85 | media->rca = 0; |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int sd_switch(struct sd_mmc_ctrlr *ctrlr, int mode, int group, |
| 90 | uint8_t value, uint8_t *resp) |
| 91 | { |
| 92 | /* Switch the frequency */ |
| 93 | struct mmc_command cmd; |
| 94 | cmd.cmdidx = SD_CMD_SWITCH_FUNC; |
| 95 | cmd.resp_type = CARD_RSP_R1; |
| 96 | cmd.cmdarg = (mode << 31) | (0xffffff & ~(0xf << (group * 4))) | |
| 97 | (value << (group * 4)); |
| 98 | cmd.flags = 0; |
| 99 | |
| 100 | struct mmc_data data; |
| 101 | data.dest = (char *)resp; |
| 102 | data.blocksize = 64; |
| 103 | data.blocks = 1; |
| 104 | data.flags = DATA_FLAG_READ; |
| 105 | |
| 106 | return ctrlr->send_cmd(ctrlr, &cmd, &data); |
| 107 | } |
| 108 | |
| 109 | static void sd_recalculate_clock(struct storage_media *media) |
| 110 | { |
| 111 | uint32_t clock = 1; |
| 112 | |
| 113 | if (media->caps & DRVR_CAP_HS) |
| 114 | clock = CLOCK_50MHZ; |
| 115 | else |
| 116 | clock = CLOCK_25MHZ; |
| 117 | SET_CLOCK(media->ctrlr, clock); |
| 118 | } |
| 119 | |
| 120 | int sd_change_freq(struct storage_media *media) |
| 121 | { |
Lee Leahy | f542aca | 2017-05-11 14:51:25 -0700 | [diff] [blame] | 122 | int delay; |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 123 | int err, timeout; |
| 124 | struct mmc_command cmd; |
| 125 | struct sd_mmc_ctrlr *ctrlr = media->ctrlr; |
| 126 | struct mmc_data data; |
| 127 | ALLOC_CACHE_ALIGN_BUFFER(uint32_t, scr, 2); |
| 128 | ALLOC_CACHE_ALIGN_BUFFER(uint32_t, switch_status, 16); |
| 129 | |
| 130 | media->caps = 0; |
| 131 | |
| 132 | /* Read the SCR to find out if this card supports higher speeds */ |
| 133 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 134 | cmd.resp_type = CARD_RSP_R1; |
| 135 | cmd.cmdarg = media->rca << 16; |
| 136 | cmd.flags = 0; |
| 137 | |
| 138 | err = ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 139 | if (err) |
| 140 | return err; |
| 141 | |
| 142 | cmd.cmdidx = SD_CMD_APP_SEND_SCR; |
| 143 | cmd.resp_type = CARD_RSP_R1; |
| 144 | cmd.cmdarg = 0; |
| 145 | cmd.flags = 0; |
| 146 | |
| 147 | timeout = 3; |
| 148 | while (timeout--) { |
| 149 | data.dest = (char *)scr; |
| 150 | data.blocksize = 8; |
| 151 | data.blocks = 1; |
| 152 | data.flags = DATA_FLAG_READ; |
| 153 | err = ctrlr->send_cmd(ctrlr, &cmd, &data); |
| 154 | if (!err) |
| 155 | break; |
| 156 | } |
| 157 | if (err) { |
| 158 | sd_mmc_error("%s returning %d\n", __func__, err); |
| 159 | return err; |
| 160 | } |
| 161 | |
| 162 | media->scr[0] = be32toh(scr[0]); |
| 163 | media->scr[1] = be32toh(scr[1]); |
| 164 | |
| 165 | switch ((media->scr[0] >> 24) & 0xf) { |
| 166 | case 0: |
| 167 | media->version = SD_VERSION_1_0; |
| 168 | break; |
| 169 | case 1: |
| 170 | media->version = SD_VERSION_1_10; |
| 171 | break; |
| 172 | case 2: |
| 173 | media->version = SD_VERSION_2; |
| 174 | break; |
| 175 | default: |
| 176 | media->version = SD_VERSION_1_0; |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | if (media->scr[0] & SD_DATA_4BIT) |
| 181 | media->caps |= DRVR_CAP_4BIT; |
| 182 | |
| 183 | /* Version 1.0 doesn't support switching */ |
| 184 | if (media->version == SD_VERSION_1_0) |
| 185 | goto out; |
| 186 | |
| 187 | timeout = 4; |
| 188 | while (timeout--) { |
| 189 | err = sd_switch(ctrlr, SD_SWITCH_CHECK, 0, 1, |
| 190 | (uint8_t *)switch_status); |
| 191 | if (err) |
| 192 | return err; |
| 193 | |
| 194 | /* The high-speed function is busy. Try again */ |
| 195 | if (!(ntohl(switch_status[7]) & SD_HIGHSPEED_BUSY)) |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | /* If high-speed isn't supported, we return */ |
| 200 | if (!(ntohl(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) |
| 201 | goto out; |
| 202 | |
| 203 | /* |
| 204 | * If the controller doesn't support SD_HIGHSPEED, do not switch the |
| 205 | * card to HIGHSPEED mode even if the card support SD_HIGHSPPED. |
| 206 | * This can avoid a further problem when the card runs in different |
| 207 | * mode than the controller. |
| 208 | */ |
| 209 | if (!((ctrlr->caps & DRVR_CAP_HS52) && (ctrlr->caps & DRVR_CAP_HS))) |
| 210 | goto out; |
| 211 | |
Elyes HAOUAS | 23e3f9d | 2020-02-03 21:07:19 +0100 | [diff] [blame] | 212 | /* Give the card time to recover after the switch operation. Wait for |
Lee Leahy | f542aca | 2017-05-11 14:51:25 -0700 | [diff] [blame] | 213 | * 9 (>= 8) clock cycles receiving the switch status. |
| 214 | */ |
| 215 | delay = (9000000 + ctrlr->bus_hz - 1) / ctrlr->bus_hz; |
| 216 | udelay(delay); |
| 217 | |
| 218 | /* Switch to high speed */ |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 219 | err = sd_switch(ctrlr, SD_SWITCH_SWITCH, 0, 1, |
| 220 | (uint8_t *)switch_status); |
| 221 | if (err) |
| 222 | return err; |
| 223 | |
Lee Leahy | f542aca | 2017-05-11 14:51:25 -0700 | [diff] [blame] | 224 | /* Give the card time to perform the switch operation. Wait for 9 |
| 225 | * (>= 8) clock cycles receiving the switch status. |
| 226 | */ |
| 227 | udelay(delay); |
| 228 | |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 229 | if ((ntohl(switch_status[4]) & 0x0f000000) == 0x01000000) { |
| 230 | media->caps |= DRVR_CAP_HS; |
| 231 | SET_TIMING(ctrlr, BUS_TIMING_SD_HS); |
| 232 | } |
| 233 | |
| 234 | out: |
| 235 | sd_recalculate_clock(media); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | int sd_set_bus_width(struct storage_media *media) |
| 240 | { |
| 241 | int err; |
| 242 | struct mmc_command cmd; |
| 243 | struct sd_mmc_ctrlr *ctrlr = media->ctrlr; |
| 244 | |
| 245 | if (media->caps & DRVR_CAP_4BIT) { |
| 246 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 247 | cmd.resp_type = CARD_RSP_R1; |
| 248 | cmd.cmdarg = media->rca << 16; |
| 249 | cmd.flags = 0; |
| 250 | |
| 251 | err = ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 252 | if (err) |
| 253 | return err; |
| 254 | |
| 255 | cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; |
| 256 | cmd.resp_type = CARD_RSP_R1; |
| 257 | cmd.cmdarg = 2; |
| 258 | cmd.flags = 0; |
| 259 | err = ctrlr->send_cmd(ctrlr, &cmd, NULL); |
| 260 | if (err) |
| 261 | return err; |
| 262 | |
| 263 | SET_BUS_WIDTH(ctrlr, 4); |
| 264 | } |
| 265 | return 0; |
| 266 | } |
| 267 | |
Lee Leahy | eef40eb | 2017-03-23 10:54:57 -0700 | [diff] [blame] | 268 | int sd_set_partition(struct storage_media *media, |
| 269 | unsigned int partition_number) |
| 270 | { |
| 271 | /* Validate the partition number */ |
| 272 | if (partition_number) |
| 273 | return -1; |
| 274 | |
| 275 | /* Update the partition number */ |
| 276 | media->partition_config = partition_number; |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | const char *sd_partition_name(struct storage_media *media, |
| 281 | unsigned int partition_number) |
| 282 | { |
| 283 | return ""; |
| 284 | } |