Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, Freescale Semiconductor, Inc |
| 3 | * Andy Fleming |
| 4 | * |
| 5 | * Based vaguely on the Linux code |
| 6 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <config.h> |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
| 13 | #include <mmc.h> |
| 14 | #include <part.h> |
| 15 | #include <malloc.h> |
| 16 | #include <linux/list.h> |
Rabin Vincent | 9b1f942 | 2009-04-05 13:30:54 +0530 | [diff] [blame] | 17 | #include <div64.h> |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 18 | |
Matt Waddel | ce0fbcd | 2011-02-24 16:35:23 +0000 | [diff] [blame] | 19 | /* Set block count limit because of 16 bit register limit on some hardware*/ |
| 20 | #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT |
| 21 | #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535 |
| 22 | #endif |
| 23 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 24 | static struct list_head mmc_devices; |
| 25 | static int cur_dev_num = -1; |
| 26 | |
Nikita Kiryanov | d23d8d7 | 2012-12-03 02:19:46 +0000 | [diff] [blame] | 27 | int __weak board_mmc_getwp(struct mmc *mmc) |
| 28 | { |
| 29 | return -1; |
| 30 | } |
| 31 | |
| 32 | int mmc_getwp(struct mmc *mmc) |
| 33 | { |
| 34 | int wp; |
| 35 | |
| 36 | wp = board_mmc_getwp(mmc); |
| 37 | |
Peter Korsgaard | d4e1da4 | 2013-03-21 04:00:03 +0000 | [diff] [blame] | 38 | if (wp < 0) { |
| 39 | if (mmc->getwp) |
| 40 | wp = mmc->getwp(mmc); |
| 41 | else |
| 42 | wp = 0; |
| 43 | } |
Nikita Kiryanov | d23d8d7 | 2012-12-03 02:19:46 +0000 | [diff] [blame] | 44 | |
| 45 | return wp; |
| 46 | } |
| 47 | |
Thierry Reding | 314284b | 2012-01-02 01:15:36 +0000 | [diff] [blame] | 48 | int __board_mmc_getcd(struct mmc *mmc) { |
Stefano Babic | 11fdade | 2010-02-05 15:04:43 +0100 | [diff] [blame] | 49 | return -1; |
| 50 | } |
| 51 | |
Thierry Reding | 314284b | 2012-01-02 01:15:36 +0000 | [diff] [blame] | 52 | int board_mmc_getcd(struct mmc *mmc)__attribute__((weak, |
Stefano Babic | 11fdade | 2010-02-05 15:04:43 +0100 | [diff] [blame] | 53 | alias("__board_mmc_getcd"))); |
| 54 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 55 | static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 56 | struct mmc_data *data) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 57 | { |
Marek Vasut | 8635ff9 | 2012-03-15 18:41:35 +0000 | [diff] [blame] | 58 | struct mmc_data backup; |
Raffaele Recalcati | 5db2fe3 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 59 | int ret; |
Marek Vasut | 8635ff9 | 2012-03-15 18:41:35 +0000 | [diff] [blame] | 60 | |
| 61 | memset(&backup, 0, sizeof(backup)); |
| 62 | |
Marek Vasut | 8635ff9 | 2012-03-15 18:41:35 +0000 | [diff] [blame] | 63 | #ifdef CONFIG_MMC_TRACE |
Raffaele Recalcati | 5db2fe3 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 64 | int i; |
| 65 | u8 *ptr; |
| 66 | |
| 67 | printf("CMD_SEND:%d\n", cmd->cmdidx); |
| 68 | printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg); |
Raffaele Recalcati | 5db2fe3 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 69 | ret = mmc->send_cmd(mmc, cmd, data); |
| 70 | switch (cmd->resp_type) { |
| 71 | case MMC_RSP_NONE: |
| 72 | printf("\t\tMMC_RSP_NONE\n"); |
| 73 | break; |
| 74 | case MMC_RSP_R1: |
| 75 | printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n", |
| 76 | cmd->response[0]); |
| 77 | break; |
| 78 | case MMC_RSP_R1b: |
| 79 | printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n", |
| 80 | cmd->response[0]); |
| 81 | break; |
| 82 | case MMC_RSP_R2: |
| 83 | printf("\t\tMMC_RSP_R2\t\t 0x%08X \n", |
| 84 | cmd->response[0]); |
| 85 | printf("\t\t \t\t 0x%08X \n", |
| 86 | cmd->response[1]); |
| 87 | printf("\t\t \t\t 0x%08X \n", |
| 88 | cmd->response[2]); |
| 89 | printf("\t\t \t\t 0x%08X \n", |
| 90 | cmd->response[3]); |
| 91 | printf("\n"); |
| 92 | printf("\t\t\t\t\tDUMPING DATA\n"); |
| 93 | for (i = 0; i < 4; i++) { |
| 94 | int j; |
| 95 | printf("\t\t\t\t\t%03d - ", i*4); |
Dirk Behme | 146bec7 | 2012-03-08 02:35:34 +0000 | [diff] [blame] | 96 | ptr = (u8 *)&cmd->response[i]; |
Raffaele Recalcati | 5db2fe3 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 97 | ptr += 3; |
| 98 | for (j = 0; j < 4; j++) |
| 99 | printf("%02X ", *ptr--); |
| 100 | printf("\n"); |
| 101 | } |
| 102 | break; |
| 103 | case MMC_RSP_R3: |
| 104 | printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n", |
| 105 | cmd->response[0]); |
| 106 | break; |
| 107 | default: |
| 108 | printf("\t\tERROR MMC rsp not supported\n"); |
| 109 | break; |
| 110 | } |
Raffaele Recalcati | 5db2fe3 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 111 | #else |
Marek Vasut | 8635ff9 | 2012-03-15 18:41:35 +0000 | [diff] [blame] | 112 | ret = mmc->send_cmd(mmc, cmd, data); |
Raffaele Recalcati | 5db2fe3 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 113 | #endif |
Marek Vasut | 8635ff9 | 2012-03-15 18:41:35 +0000 | [diff] [blame] | 114 | return ret; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 117 | static int mmc_send_status(struct mmc *mmc, int timeout) |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 118 | { |
| 119 | struct mmc_cmd cmd; |
Jan Kloetzke | d617c42 | 2012-02-05 22:29:12 +0000 | [diff] [blame] | 120 | int err, retries = 5; |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 121 | #ifdef CONFIG_MMC_TRACE |
| 122 | int status; |
| 123 | #endif |
| 124 | |
| 125 | cmd.cmdidx = MMC_CMD_SEND_STATUS; |
| 126 | cmd.resp_type = MMC_RSP_R1; |
Marek Vasut | aaf3d41 | 2011-08-10 09:24:48 +0200 | [diff] [blame] | 127 | if (!mmc_host_is_spi(mmc)) |
| 128 | cmd.cmdarg = mmc->rca << 16; |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 129 | |
| 130 | do { |
| 131 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Jan Kloetzke | d617c42 | 2012-02-05 22:29:12 +0000 | [diff] [blame] | 132 | if (!err) { |
| 133 | if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) && |
| 134 | (cmd.response[0] & MMC_STATUS_CURR_STATE) != |
| 135 | MMC_STATE_PRG) |
| 136 | break; |
| 137 | else if (cmd.response[0] & MMC_STATUS_MASK) { |
| 138 | printf("Status Error: 0x%08X\n", |
| 139 | cmd.response[0]); |
| 140 | return COMM_ERR; |
| 141 | } |
| 142 | } else if (--retries < 0) |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 143 | return err; |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 144 | |
| 145 | udelay(1000); |
| 146 | |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 147 | } while (timeout--); |
| 148 | |
Raffaele Recalcati | 5db2fe3 | 2011-03-11 02:01:14 +0000 | [diff] [blame] | 149 | #ifdef CONFIG_MMC_TRACE |
| 150 | status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9; |
| 151 | printf("CURR STATE:%d\n", status); |
| 152 | #endif |
Jongman Heo | 5b0c942 | 2012-06-03 21:32:13 +0000 | [diff] [blame] | 153 | if (timeout <= 0) { |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 154 | printf("Timeout waiting card ready\n"); |
| 155 | return TIMEOUT; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 161 | static int mmc_set_blocklen(struct mmc *mmc, int len) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 162 | { |
| 163 | struct mmc_cmd cmd; |
| 164 | |
| 165 | cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; |
| 166 | cmd.resp_type = MMC_RSP_R1; |
| 167 | cmd.cmdarg = len; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 168 | |
| 169 | return mmc_send_cmd(mmc, &cmd, NULL); |
| 170 | } |
| 171 | |
| 172 | struct mmc *find_mmc_device(int dev_num) |
| 173 | { |
| 174 | struct mmc *m; |
| 175 | struct list_head *entry; |
| 176 | |
| 177 | list_for_each(entry, &mmc_devices) { |
| 178 | m = list_entry(entry, struct mmc, link); |
| 179 | |
| 180 | if (m->block_dev.dev == dev_num) |
| 181 | return m; |
| 182 | } |
| 183 | |
| 184 | printf("MMC Device %d not found\n", dev_num); |
| 185 | |
| 186 | return NULL; |
| 187 | } |
| 188 | |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 189 | static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt) |
| 190 | { |
| 191 | struct mmc_cmd cmd; |
| 192 | ulong end; |
| 193 | int err, start_cmd, end_cmd; |
| 194 | |
| 195 | if (mmc->high_capacity) |
| 196 | end = start + blkcnt - 1; |
| 197 | else { |
| 198 | end = (start + blkcnt - 1) * mmc->write_bl_len; |
| 199 | start *= mmc->write_bl_len; |
| 200 | } |
| 201 | |
| 202 | if (IS_SD(mmc)) { |
| 203 | start_cmd = SD_CMD_ERASE_WR_BLK_START; |
| 204 | end_cmd = SD_CMD_ERASE_WR_BLK_END; |
| 205 | } else { |
| 206 | start_cmd = MMC_CMD_ERASE_GROUP_START; |
| 207 | end_cmd = MMC_CMD_ERASE_GROUP_END; |
| 208 | } |
| 209 | |
| 210 | cmd.cmdidx = start_cmd; |
| 211 | cmd.cmdarg = start; |
| 212 | cmd.resp_type = MMC_RSP_R1; |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 213 | |
| 214 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 215 | if (err) |
| 216 | goto err_out; |
| 217 | |
| 218 | cmd.cmdidx = end_cmd; |
| 219 | cmd.cmdarg = end; |
| 220 | |
| 221 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 222 | if (err) |
| 223 | goto err_out; |
| 224 | |
| 225 | cmd.cmdidx = MMC_CMD_ERASE; |
| 226 | cmd.cmdarg = SECURE_ERASE; |
| 227 | cmd.resp_type = MMC_RSP_R1b; |
| 228 | |
| 229 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 230 | if (err) |
| 231 | goto err_out; |
| 232 | |
| 233 | return 0; |
| 234 | |
| 235 | err_out: |
| 236 | puts("mmc erase failed\n"); |
| 237 | return err; |
| 238 | } |
| 239 | |
| 240 | static unsigned long |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 241 | mmc_berase(int dev_num, lbaint_t start, lbaint_t blkcnt) |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 242 | { |
| 243 | int err = 0; |
| 244 | struct mmc *mmc = find_mmc_device(dev_num); |
| 245 | lbaint_t blk = 0, blk_r = 0; |
Jerry Huang | d2d8afa | 2012-05-17 23:00:51 +0000 | [diff] [blame] | 246 | int timeout = 1000; |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 247 | |
| 248 | if (!mmc) |
| 249 | return -1; |
| 250 | |
| 251 | if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size)) |
| 252 | printf("\n\nCaution! Your devices Erase group is 0x%x\n" |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 253 | "The erase range would be change to " |
| 254 | "0x" LBAF "~0x" LBAF "\n\n", |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 255 | mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1), |
| 256 | ((start + blkcnt + mmc->erase_grp_size) |
| 257 | & ~(mmc->erase_grp_size - 1)) - 1); |
| 258 | |
| 259 | while (blk < blkcnt) { |
| 260 | blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ? |
| 261 | mmc->erase_grp_size : (blkcnt - blk); |
| 262 | err = mmc_erase_t(mmc, start + blk, blk_r); |
| 263 | if (err) |
| 264 | break; |
| 265 | |
| 266 | blk += blk_r; |
Jerry Huang | d2d8afa | 2012-05-17 23:00:51 +0000 | [diff] [blame] | 267 | |
| 268 | /* Waiting for the ready status */ |
| 269 | if (mmc_send_status(mmc, timeout)) |
| 270 | return 0; |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | return blk; |
| 274 | } |
| 275 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 276 | static ulong |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 277 | mmc_write_blocks(struct mmc *mmc, lbaint_t start, lbaint_t blkcnt, const void*src) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 278 | { |
| 279 | struct mmc_cmd cmd; |
| 280 | struct mmc_data data; |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 281 | int timeout = 1000; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 282 | |
Lei Wen | d2bf29e | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 283 | if ((start + blkcnt) > mmc->block_dev.lba) { |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 284 | printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", |
Lei Wen | d2bf29e | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 285 | start + blkcnt, mmc->block_dev.lba); |
| 286 | return 0; |
| 287 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 288 | |
Ruud Commandeur | a586c0a | 2013-05-22 13:19:43 +0200 | [diff] [blame] | 289 | if (blkcnt == 0) |
| 290 | return 0; |
| 291 | else if (blkcnt == 1) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 292 | cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; |
Ruud Commandeur | a586c0a | 2013-05-22 13:19:43 +0200 | [diff] [blame] | 293 | else |
| 294 | cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 295 | |
| 296 | if (mmc->high_capacity) |
| 297 | cmd.cmdarg = start; |
| 298 | else |
Steve Sakoman | def412b | 2010-10-28 09:00:26 -0700 | [diff] [blame] | 299 | cmd.cmdarg = start * mmc->write_bl_len; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 300 | |
| 301 | cmd.resp_type = MMC_RSP_R1; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 302 | |
| 303 | data.src = src; |
| 304 | data.blocks = blkcnt; |
Steve Sakoman | def412b | 2010-10-28 09:00:26 -0700 | [diff] [blame] | 305 | data.blocksize = mmc->write_bl_len; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 306 | data.flags = MMC_DATA_WRITE; |
| 307 | |
Steve Sakoman | def412b | 2010-10-28 09:00:26 -0700 | [diff] [blame] | 308 | if (mmc_send_cmd(mmc, &cmd, &data)) { |
| 309 | printf("mmc write failed\n"); |
| 310 | return 0; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 313 | /* SPI multiblock writes terminate using a special |
| 314 | * token, not a STOP_TRANSMISSION request. |
| 315 | */ |
| 316 | if (!mmc_host_is_spi(mmc) && blkcnt > 1) { |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 317 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; |
| 318 | cmd.cmdarg = 0; |
| 319 | cmd.resp_type = MMC_RSP_R1b; |
Steve Sakoman | def412b | 2010-10-28 09:00:26 -0700 | [diff] [blame] | 320 | if (mmc_send_cmd(mmc, &cmd, NULL)) { |
| 321 | printf("mmc fail to send stop cmd\n"); |
| 322 | return 0; |
Lei Wen | 0158126 | 2010-10-14 13:38:11 +0800 | [diff] [blame] | 323 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 324 | } |
| 325 | |
Jan Kloetzke | 93ad0d1 | 2012-02-05 22:29:11 +0000 | [diff] [blame] | 326 | /* Waiting for the ready status */ |
| 327 | if (mmc_send_status(mmc, timeout)) |
| 328 | return 0; |
| 329 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 330 | return blkcnt; |
| 331 | } |
| 332 | |
Lei Wen | 0158126 | 2010-10-14 13:38:11 +0800 | [diff] [blame] | 333 | static ulong |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 334 | mmc_bwrite(int dev_num, lbaint_t start, lbaint_t blkcnt, const void*src) |
Lei Wen | 0158126 | 2010-10-14 13:38:11 +0800 | [diff] [blame] | 335 | { |
Lei Wen | 0158126 | 2010-10-14 13:38:11 +0800 | [diff] [blame] | 336 | lbaint_t cur, blocks_todo = blkcnt; |
| 337 | |
Steve Sakoman | def412b | 2010-10-28 09:00:26 -0700 | [diff] [blame] | 338 | struct mmc *mmc = find_mmc_device(dev_num); |
Lei Wen | 0158126 | 2010-10-14 13:38:11 +0800 | [diff] [blame] | 339 | if (!mmc) |
Steve Sakoman | def412b | 2010-10-28 09:00:26 -0700 | [diff] [blame] | 340 | return 0; |
Lei Wen | 0158126 | 2010-10-14 13:38:11 +0800 | [diff] [blame] | 341 | |
Steve Sakoman | def412b | 2010-10-28 09:00:26 -0700 | [diff] [blame] | 342 | if (mmc_set_blocklen(mmc, mmc->write_bl_len)) |
| 343 | return 0; |
Lei Wen | 0158126 | 2010-10-14 13:38:11 +0800 | [diff] [blame] | 344 | |
| 345 | do { |
John Rigby | 8feafcc | 2011-04-18 05:50:08 +0000 | [diff] [blame] | 346 | cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo; |
Lei Wen | 0158126 | 2010-10-14 13:38:11 +0800 | [diff] [blame] | 347 | if(mmc_write_blocks(mmc, start, cur, src) != cur) |
Steve Sakoman | def412b | 2010-10-28 09:00:26 -0700 | [diff] [blame] | 348 | return 0; |
Lei Wen | 0158126 | 2010-10-14 13:38:11 +0800 | [diff] [blame] | 349 | blocks_todo -= cur; |
| 350 | start += cur; |
| 351 | src += cur * mmc->write_bl_len; |
| 352 | } while (blocks_todo > 0); |
| 353 | |
| 354 | return blkcnt; |
| 355 | } |
| 356 | |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 357 | static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start, |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 358 | lbaint_t blkcnt) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 359 | { |
| 360 | struct mmc_cmd cmd; |
| 361 | struct mmc_data data; |
| 362 | |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 363 | if (blkcnt > 1) |
| 364 | cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; |
| 365 | else |
| 366 | cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 367 | |
| 368 | if (mmc->high_capacity) |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 369 | cmd.cmdarg = start; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 370 | else |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 371 | cmd.cmdarg = start * mmc->read_bl_len; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 372 | |
| 373 | cmd.resp_type = MMC_RSP_R1; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 374 | |
| 375 | data.dest = dst; |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 376 | data.blocks = blkcnt; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 377 | data.blocksize = mmc->read_bl_len; |
| 378 | data.flags = MMC_DATA_READ; |
| 379 | |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 380 | if (mmc_send_cmd(mmc, &cmd, &data)) |
| 381 | return 0; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 382 | |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 383 | if (blkcnt > 1) { |
| 384 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; |
| 385 | cmd.cmdarg = 0; |
| 386 | cmd.resp_type = MMC_RSP_R1b; |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 387 | if (mmc_send_cmd(mmc, &cmd, NULL)) { |
| 388 | printf("mmc fail to send stop cmd\n"); |
| 389 | return 0; |
| 390 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 391 | } |
| 392 | |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 393 | return blkcnt; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 394 | } |
| 395 | |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 396 | static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 397 | { |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 398 | lbaint_t cur, blocks_todo = blkcnt; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 399 | |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 400 | if (blkcnt == 0) |
| 401 | return 0; |
| 402 | |
| 403 | struct mmc *mmc = find_mmc_device(dev_num); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 404 | if (!mmc) |
| 405 | return 0; |
| 406 | |
Lei Wen | d2bf29e | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 407 | if ((start + blkcnt) > mmc->block_dev.lba) { |
Sascha Silbe | ff8fef5 | 2013-06-14 13:07:25 +0200 | [diff] [blame] | 408 | printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", |
Lei Wen | d2bf29e | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 409 | start + blkcnt, mmc->block_dev.lba); |
| 410 | return 0; |
| 411 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 412 | |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 413 | if (mmc_set_blocklen(mmc, mmc->read_bl_len)) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 414 | return 0; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 415 | |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 416 | do { |
John Rigby | 8feafcc | 2011-04-18 05:50:08 +0000 | [diff] [blame] | 417 | cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo; |
Alagu Sankar | 4a1a06b | 2010-10-25 07:23:56 -0700 | [diff] [blame] | 418 | if(mmc_read_blocks(mmc, dst, start, cur) != cur) |
| 419 | return 0; |
| 420 | blocks_todo -= cur; |
| 421 | start += cur; |
| 422 | dst += cur * mmc->read_bl_len; |
| 423 | } while (blocks_todo > 0); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 424 | |
| 425 | return blkcnt; |
| 426 | } |
| 427 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 428 | static int mmc_go_idle(struct mmc *mmc) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 429 | { |
| 430 | struct mmc_cmd cmd; |
| 431 | int err; |
| 432 | |
| 433 | udelay(1000); |
| 434 | |
| 435 | cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; |
| 436 | cmd.cmdarg = 0; |
| 437 | cmd.resp_type = MMC_RSP_NONE; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 438 | |
| 439 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 440 | |
| 441 | if (err) |
| 442 | return err; |
| 443 | |
| 444 | udelay(2000); |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 449 | static int sd_send_op_cond(struct mmc *mmc) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 450 | { |
| 451 | int timeout = 1000; |
| 452 | int err; |
| 453 | struct mmc_cmd cmd; |
| 454 | |
| 455 | do { |
| 456 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 457 | cmd.resp_type = MMC_RSP_R1; |
| 458 | cmd.cmdarg = 0; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 459 | |
| 460 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 461 | |
| 462 | if (err) |
| 463 | return err; |
| 464 | |
| 465 | cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; |
| 466 | cmd.resp_type = MMC_RSP_R3; |
Stefano Babic | 250de12 | 2010-01-20 18:20:39 +0100 | [diff] [blame] | 467 | |
| 468 | /* |
| 469 | * Most cards do not answer if some reserved bits |
| 470 | * in the ocr are set. However, Some controller |
| 471 | * can set bit 7 (reserved for low voltages), but |
| 472 | * how to manage low voltages SD card is not yet |
| 473 | * specified. |
| 474 | */ |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 475 | cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 : |
| 476 | (mmc->voltages & 0xff8000); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 477 | |
| 478 | if (mmc->version == SD_VERSION_2) |
| 479 | cmd.cmdarg |= OCR_HCS; |
| 480 | |
| 481 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 482 | |
| 483 | if (err) |
| 484 | return err; |
| 485 | |
| 486 | udelay(1000); |
| 487 | } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--); |
| 488 | |
| 489 | if (timeout <= 0) |
| 490 | return UNUSABLE_ERR; |
| 491 | |
| 492 | if (mmc->version != SD_VERSION_2) |
| 493 | mmc->version = SD_VERSION_1_0; |
| 494 | |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 495 | if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ |
| 496 | cmd.cmdidx = MMC_CMD_SPI_READ_OCR; |
| 497 | cmd.resp_type = MMC_RSP_R3; |
| 498 | cmd.cmdarg = 0; |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 499 | |
| 500 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 501 | |
| 502 | if (err) |
| 503 | return err; |
| 504 | } |
| 505 | |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 506 | mmc->ocr = cmd.response[0]; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 507 | |
| 508 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); |
| 509 | mmc->rca = 0; |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
Che-Liang Chiou | e955044 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 514 | /* We pass in the cmd since otherwise the init seems to fail */ |
| 515 | static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd, |
| 516 | int use_arg) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 517 | { |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 518 | int err; |
| 519 | |
Che-Liang Chiou | e955044 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 520 | cmd->cmdidx = MMC_CMD_SEND_OP_COND; |
| 521 | cmd->resp_type = MMC_RSP_R3; |
| 522 | cmd->cmdarg = 0; |
| 523 | if (use_arg && !mmc_host_is_spi(mmc)) { |
| 524 | cmd->cmdarg = |
| 525 | (mmc->voltages & |
| 526 | (mmc->op_cond_response & OCR_VOLTAGE_MASK)) | |
| 527 | (mmc->op_cond_response & OCR_ACCESS_MODE); |
| 528 | |
| 529 | if (mmc->host_caps & MMC_MODE_HC) |
| 530 | cmd->cmdarg |= OCR_HCS; |
| 531 | } |
| 532 | err = mmc_send_cmd(mmc, cmd, NULL); |
| 533 | if (err) |
| 534 | return err; |
| 535 | mmc->op_cond_response = cmd->response[0]; |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | int mmc_send_op_cond(struct mmc *mmc) |
| 540 | { |
| 541 | struct mmc_cmd cmd; |
| 542 | int err, i; |
| 543 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 544 | /* Some cards seem to need this */ |
| 545 | mmc_go_idle(mmc); |
| 546 | |
Raffaele Recalcati | 31cacba | 2011-03-11 02:01:13 +0000 | [diff] [blame] | 547 | /* Asking to the card its capabilities */ |
Che-Liang Chiou | e955044 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 548 | mmc->op_cond_pending = 1; |
| 549 | for (i = 0; i < 2; i++) { |
| 550 | err = mmc_send_op_cond_iter(mmc, &cmd, i != 0); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 551 | if (err) |
| 552 | return err; |
| 553 | |
Che-Liang Chiou | e955044 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 554 | /* exit if not busy (flag seems to be inverted) */ |
| 555 | if (mmc->op_cond_response & OCR_BUSY) |
| 556 | return 0; |
| 557 | } |
| 558 | return IN_PROGRESS; |
| 559 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 560 | |
Che-Liang Chiou | e955044 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 561 | int mmc_complete_op_cond(struct mmc *mmc) |
| 562 | { |
| 563 | struct mmc_cmd cmd; |
| 564 | int timeout = 1000; |
| 565 | uint start; |
| 566 | int err; |
| 567 | |
| 568 | mmc->op_cond_pending = 0; |
| 569 | start = get_timer(0); |
| 570 | do { |
| 571 | err = mmc_send_op_cond_iter(mmc, &cmd, 1); |
| 572 | if (err) |
| 573 | return err; |
| 574 | if (get_timer(start) > timeout) |
| 575 | return UNUSABLE_ERR; |
| 576 | udelay(100); |
| 577 | } while (!(mmc->op_cond_response & OCR_BUSY)); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 578 | |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 579 | if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ |
| 580 | cmd.cmdidx = MMC_CMD_SPI_READ_OCR; |
| 581 | cmd.resp_type = MMC_RSP_R3; |
| 582 | cmd.cmdarg = 0; |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 583 | |
| 584 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 585 | |
| 586 | if (err) |
| 587 | return err; |
| 588 | } |
| 589 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 590 | mmc->version = MMC_VERSION_UNKNOWN; |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 591 | mmc->ocr = cmd.response[0]; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 592 | |
| 593 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); |
| 594 | mmc->rca = 0; |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 600 | static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 601 | { |
| 602 | struct mmc_cmd cmd; |
| 603 | struct mmc_data data; |
| 604 | int err; |
| 605 | |
| 606 | /* Get the Card Status Register */ |
| 607 | cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; |
| 608 | cmd.resp_type = MMC_RSP_R1; |
| 609 | cmd.cmdarg = 0; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 610 | |
Yoshihiro Shimoda | cdfd1ac | 2012-06-07 19:09:11 +0000 | [diff] [blame] | 611 | data.dest = (char *)ext_csd; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 612 | data.blocks = 1; |
Simon Glass | 8bfa195 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 613 | data.blocksize = MMC_MAX_BLOCK_LEN; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 614 | data.flags = MMC_DATA_READ; |
| 615 | |
| 616 | err = mmc_send_cmd(mmc, &cmd, &data); |
| 617 | |
| 618 | return err; |
| 619 | } |
| 620 | |
| 621 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 622 | static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 623 | { |
| 624 | struct mmc_cmd cmd; |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 625 | int timeout = 1000; |
| 626 | int ret; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 627 | |
| 628 | cmd.cmdidx = MMC_CMD_SWITCH; |
| 629 | cmd.resp_type = MMC_RSP_R1b; |
| 630 | cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 631 | (index << 16) | |
| 632 | (value << 8); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 633 | |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 634 | ret = mmc_send_cmd(mmc, &cmd, NULL); |
| 635 | |
| 636 | /* Waiting for the ready status */ |
Jan Kloetzke | 93ad0d1 | 2012-02-05 22:29:11 +0000 | [diff] [blame] | 637 | if (!ret) |
| 638 | ret = mmc_send_status(mmc, timeout); |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 639 | |
| 640 | return ret; |
| 641 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 642 | } |
| 643 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 644 | static int mmc_change_freq(struct mmc *mmc) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 645 | { |
Simon Glass | 8bfa195 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 646 | ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 647 | char cardtype; |
| 648 | int err; |
| 649 | |
| 650 | mmc->card_caps = 0; |
| 651 | |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 652 | if (mmc_host_is_spi(mmc)) |
| 653 | return 0; |
| 654 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 655 | /* Only version 4 supports high-speed */ |
| 656 | if (mmc->version < MMC_VERSION_4) |
| 657 | return 0; |
| 658 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 659 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 660 | |
| 661 | if (err) |
| 662 | return err; |
| 663 | |
Lei Wen | 0560db1 | 2011-10-03 20:35:10 +0000 | [diff] [blame] | 664 | cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 665 | |
| 666 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); |
| 667 | |
| 668 | if (err) |
| 669 | return err; |
| 670 | |
| 671 | /* Now check to see that it worked */ |
| 672 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 673 | |
| 674 | if (err) |
| 675 | return err; |
| 676 | |
| 677 | /* No high-speed support */ |
Lei Wen | 0560db1 | 2011-10-03 20:35:10 +0000 | [diff] [blame] | 678 | if (!ext_csd[EXT_CSD_HS_TIMING]) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 679 | return 0; |
| 680 | |
| 681 | /* High Speed is set, there are two types: 52MHz and 26MHz */ |
| 682 | if (cardtype & MMC_HS_52MHZ) |
| 683 | mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
| 684 | else |
| 685 | mmc->card_caps |= MMC_MODE_HS; |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
Stephen Warren | f866a46 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 690 | static int mmc_set_capacity(struct mmc *mmc, int part_num) |
| 691 | { |
| 692 | switch (part_num) { |
| 693 | case 0: |
| 694 | mmc->capacity = mmc->capacity_user; |
| 695 | break; |
| 696 | case 1: |
| 697 | case 2: |
| 698 | mmc->capacity = mmc->capacity_boot; |
| 699 | break; |
| 700 | case 3: |
| 701 | mmc->capacity = mmc->capacity_rpmb; |
| 702 | break; |
| 703 | case 4: |
| 704 | case 5: |
| 705 | case 6: |
| 706 | case 7: |
| 707 | mmc->capacity = mmc->capacity_gp[part_num - 4]; |
| 708 | break; |
| 709 | default: |
| 710 | return -1; |
| 711 | } |
| 712 | |
| 713 | mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 718 | int mmc_switch_part(int dev_num, unsigned int part_num) |
| 719 | { |
| 720 | struct mmc *mmc = find_mmc_device(dev_num); |
Stephen Warren | f866a46 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 721 | int ret; |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 722 | |
| 723 | if (!mmc) |
| 724 | return -1; |
| 725 | |
Stephen Warren | f866a46 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 726 | ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, |
| 727 | (mmc->part_config & ~PART_ACCESS_MASK) |
| 728 | | (part_num & PART_ACCESS_MASK)); |
| 729 | if (ret) |
| 730 | return ret; |
| 731 | |
| 732 | return mmc_set_capacity(mmc, part_num); |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Thierry Reding | 48972d9 | 2012-01-02 01:15:37 +0000 | [diff] [blame] | 735 | int mmc_getcd(struct mmc *mmc) |
| 736 | { |
| 737 | int cd; |
| 738 | |
| 739 | cd = board_mmc_getcd(mmc); |
| 740 | |
Peter Korsgaard | d4e1da4 | 2013-03-21 04:00:03 +0000 | [diff] [blame] | 741 | if (cd < 0) { |
| 742 | if (mmc->getcd) |
| 743 | cd = mmc->getcd(mmc); |
| 744 | else |
| 745 | cd = 1; |
| 746 | } |
Thierry Reding | 48972d9 | 2012-01-02 01:15:37 +0000 | [diff] [blame] | 747 | |
| 748 | return cd; |
| 749 | } |
| 750 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 751 | static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 752 | { |
| 753 | struct mmc_cmd cmd; |
| 754 | struct mmc_data data; |
| 755 | |
| 756 | /* Switch the frequency */ |
| 757 | cmd.cmdidx = SD_CMD_SWITCH_FUNC; |
| 758 | cmd.resp_type = MMC_RSP_R1; |
| 759 | cmd.cmdarg = (mode << 31) | 0xffffff; |
| 760 | cmd.cmdarg &= ~(0xf << (group * 4)); |
| 761 | cmd.cmdarg |= value << (group * 4); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 762 | |
| 763 | data.dest = (char *)resp; |
| 764 | data.blocksize = 64; |
| 765 | data.blocks = 1; |
| 766 | data.flags = MMC_DATA_READ; |
| 767 | |
| 768 | return mmc_send_cmd(mmc, &cmd, &data); |
| 769 | } |
| 770 | |
| 771 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 772 | static int sd_change_freq(struct mmc *mmc) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 773 | { |
| 774 | int err; |
| 775 | struct mmc_cmd cmd; |
Anton staaf | f781dd3 | 2011-10-03 13:54:59 +0000 | [diff] [blame] | 776 | ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2); |
| 777 | ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 778 | struct mmc_data data; |
| 779 | int timeout; |
| 780 | |
| 781 | mmc->card_caps = 0; |
| 782 | |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 783 | if (mmc_host_is_spi(mmc)) |
| 784 | return 0; |
| 785 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 786 | /* Read the SCR to find out if this card supports higher speeds */ |
| 787 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 788 | cmd.resp_type = MMC_RSP_R1; |
| 789 | cmd.cmdarg = mmc->rca << 16; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 790 | |
| 791 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 792 | |
| 793 | if (err) |
| 794 | return err; |
| 795 | |
| 796 | cmd.cmdidx = SD_CMD_APP_SEND_SCR; |
| 797 | cmd.resp_type = MMC_RSP_R1; |
| 798 | cmd.cmdarg = 0; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 799 | |
| 800 | timeout = 3; |
| 801 | |
| 802 | retry_scr: |
Anton staaf | f781dd3 | 2011-10-03 13:54:59 +0000 | [diff] [blame] | 803 | data.dest = (char *)scr; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 804 | data.blocksize = 8; |
| 805 | data.blocks = 1; |
| 806 | data.flags = MMC_DATA_READ; |
| 807 | |
| 808 | err = mmc_send_cmd(mmc, &cmd, &data); |
| 809 | |
| 810 | if (err) { |
| 811 | if (timeout--) |
| 812 | goto retry_scr; |
| 813 | |
| 814 | return err; |
| 815 | } |
| 816 | |
Yauhen Kharuzhy | 4e3d89b | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 817 | mmc->scr[0] = __be32_to_cpu(scr[0]); |
| 818 | mmc->scr[1] = __be32_to_cpu(scr[1]); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 819 | |
| 820 | switch ((mmc->scr[0] >> 24) & 0xf) { |
| 821 | case 0: |
| 822 | mmc->version = SD_VERSION_1_0; |
| 823 | break; |
| 824 | case 1: |
| 825 | mmc->version = SD_VERSION_1_10; |
| 826 | break; |
| 827 | case 2: |
| 828 | mmc->version = SD_VERSION_2; |
Jaehoon Chung | 1741c64 | 2013-01-29 22:58:16 +0000 | [diff] [blame] | 829 | if ((mmc->scr[0] >> 15) & 0x1) |
| 830 | mmc->version = SD_VERSION_3; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 831 | break; |
| 832 | default: |
| 833 | mmc->version = SD_VERSION_1_0; |
| 834 | break; |
| 835 | } |
| 836 | |
Alagu Sankar | b44c708 | 2010-05-12 15:08:24 +0530 | [diff] [blame] | 837 | if (mmc->scr[0] & SD_DATA_4BIT) |
| 838 | mmc->card_caps |= MMC_MODE_4BIT; |
| 839 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 840 | /* Version 1.0 doesn't support switching */ |
| 841 | if (mmc->version == SD_VERSION_1_0) |
| 842 | return 0; |
| 843 | |
| 844 | timeout = 4; |
| 845 | while (timeout--) { |
| 846 | err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, |
Anton staaf | f781dd3 | 2011-10-03 13:54:59 +0000 | [diff] [blame] | 847 | (u8 *)switch_status); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 848 | |
| 849 | if (err) |
| 850 | return err; |
| 851 | |
| 852 | /* The high-speed function is busy. Try again */ |
Yauhen Kharuzhy | 4e3d89b | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 853 | if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 854 | break; |
| 855 | } |
| 856 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 857 | /* If high-speed isn't supported, we return */ |
Yauhen Kharuzhy | 4e3d89b | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 858 | if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 859 | return 0; |
| 860 | |
Macpaul Lin | 2c3fbf4 | 2011-11-28 16:31:09 +0000 | [diff] [blame] | 861 | /* |
| 862 | * If the host doesn't support SD_HIGHSPEED, do not switch card to |
| 863 | * HIGHSPEED mode even if the card support SD_HIGHSPPED. |
| 864 | * This can avoid furthur problem when the card runs in different |
| 865 | * mode between the host. |
| 866 | */ |
| 867 | if (!((mmc->host_caps & MMC_MODE_HS_52MHz) && |
| 868 | (mmc->host_caps & MMC_MODE_HS))) |
| 869 | return 0; |
| 870 | |
Anton staaf | f781dd3 | 2011-10-03 13:54:59 +0000 | [diff] [blame] | 871 | err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 872 | |
| 873 | if (err) |
| 874 | return err; |
| 875 | |
Yauhen Kharuzhy | 4e3d89b | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 876 | if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 877 | mmc->card_caps |= MMC_MODE_HS; |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | /* frequency bases */ |
| 883 | /* divided by 10 to be nice to platforms without floating point */ |
Mike Frysinger | 5f837c2 | 2010-10-20 01:15:53 +0000 | [diff] [blame] | 884 | static const int fbase[] = { |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 885 | 10000, |
| 886 | 100000, |
| 887 | 1000000, |
| 888 | 10000000, |
| 889 | }; |
| 890 | |
| 891 | /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice |
| 892 | * to platforms without floating point. |
| 893 | */ |
Mike Frysinger | 5f837c2 | 2010-10-20 01:15:53 +0000 | [diff] [blame] | 894 | static const int multipliers[] = { |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 895 | 0, /* reserved */ |
| 896 | 10, |
| 897 | 12, |
| 898 | 13, |
| 899 | 15, |
| 900 | 20, |
| 901 | 25, |
| 902 | 30, |
| 903 | 35, |
| 904 | 40, |
| 905 | 45, |
| 906 | 50, |
| 907 | 55, |
| 908 | 60, |
| 909 | 70, |
| 910 | 80, |
| 911 | }; |
| 912 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 913 | static void mmc_set_ios(struct mmc *mmc) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 914 | { |
| 915 | mmc->set_ios(mmc); |
| 916 | } |
| 917 | |
| 918 | void mmc_set_clock(struct mmc *mmc, uint clock) |
| 919 | { |
| 920 | if (clock > mmc->f_max) |
| 921 | clock = mmc->f_max; |
| 922 | |
| 923 | if (clock < mmc->f_min) |
| 924 | clock = mmc->f_min; |
| 925 | |
| 926 | mmc->clock = clock; |
| 927 | |
| 928 | mmc_set_ios(mmc); |
| 929 | } |
| 930 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 931 | static void mmc_set_bus_width(struct mmc *mmc, uint width) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 932 | { |
| 933 | mmc->bus_width = width; |
| 934 | |
| 935 | mmc_set_ios(mmc); |
| 936 | } |
| 937 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 938 | static int mmc_startup(struct mmc *mmc) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 939 | { |
Stephen Warren | f866a46 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 940 | int err, i; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 941 | uint mult, freq; |
Yoshihiro Shimoda | 639b782 | 2011-07-04 22:13:26 +0000 | [diff] [blame] | 942 | u64 cmult, csize, capacity; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 943 | struct mmc_cmd cmd; |
Simon Glass | 8bfa195 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 944 | ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); |
| 945 | ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN); |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 946 | int timeout = 1000; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 947 | |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 948 | #ifdef CONFIG_MMC_SPI_CRC_ON |
| 949 | if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ |
| 950 | cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF; |
| 951 | cmd.resp_type = MMC_RSP_R1; |
| 952 | cmd.cmdarg = 1; |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 953 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 954 | |
| 955 | if (err) |
| 956 | return err; |
| 957 | } |
| 958 | #endif |
| 959 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 960 | /* Put the Card in Identify Mode */ |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 961 | cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID : |
| 962 | MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */ |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 963 | cmd.resp_type = MMC_RSP_R2; |
| 964 | cmd.cmdarg = 0; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 965 | |
| 966 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 967 | |
| 968 | if (err) |
| 969 | return err; |
| 970 | |
| 971 | memcpy(mmc->cid, cmd.response, 16); |
| 972 | |
| 973 | /* |
| 974 | * For MMC cards, set the Relative Address. |
| 975 | * For SD cards, get the Relatvie Address. |
| 976 | * This also puts the cards into Standby State |
| 977 | */ |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 978 | if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ |
| 979 | cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; |
| 980 | cmd.cmdarg = mmc->rca << 16; |
| 981 | cmd.resp_type = MMC_RSP_R6; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 982 | |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 983 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 984 | |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 985 | if (err) |
| 986 | return err; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 987 | |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 988 | if (IS_SD(mmc)) |
| 989 | mmc->rca = (cmd.response[0] >> 16) & 0xffff; |
| 990 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 991 | |
| 992 | /* Get the Card-Specific Data */ |
| 993 | cmd.cmdidx = MMC_CMD_SEND_CSD; |
| 994 | cmd.resp_type = MMC_RSP_R2; |
| 995 | cmd.cmdarg = mmc->rca << 16; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 996 | |
| 997 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 998 | |
Raffaele Recalcati | 5d4fc8d | 2011-03-11 02:01:12 +0000 | [diff] [blame] | 999 | /* Waiting for the ready status */ |
| 1000 | mmc_send_status(mmc, timeout); |
| 1001 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1002 | if (err) |
| 1003 | return err; |
| 1004 | |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 1005 | mmc->csd[0] = cmd.response[0]; |
| 1006 | mmc->csd[1] = cmd.response[1]; |
| 1007 | mmc->csd[2] = cmd.response[2]; |
| 1008 | mmc->csd[3] = cmd.response[3]; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1009 | |
| 1010 | if (mmc->version == MMC_VERSION_UNKNOWN) { |
Rabin Vincent | 0b453ff | 2009-04-05 13:30:55 +0530 | [diff] [blame] | 1011 | int version = (cmd.response[0] >> 26) & 0xf; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1012 | |
| 1013 | switch (version) { |
| 1014 | case 0: |
| 1015 | mmc->version = MMC_VERSION_1_2; |
| 1016 | break; |
| 1017 | case 1: |
| 1018 | mmc->version = MMC_VERSION_1_4; |
| 1019 | break; |
| 1020 | case 2: |
| 1021 | mmc->version = MMC_VERSION_2_2; |
| 1022 | break; |
| 1023 | case 3: |
| 1024 | mmc->version = MMC_VERSION_3; |
| 1025 | break; |
| 1026 | case 4: |
| 1027 | mmc->version = MMC_VERSION_4; |
| 1028 | break; |
| 1029 | default: |
| 1030 | mmc->version = MMC_VERSION_1_2; |
| 1031 | break; |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | /* divide frequency by 10, since the mults are 10x bigger */ |
Rabin Vincent | 0b453ff | 2009-04-05 13:30:55 +0530 | [diff] [blame] | 1036 | freq = fbase[(cmd.response[0] & 0x7)]; |
| 1037 | mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1038 | |
| 1039 | mmc->tran_speed = freq * mult; |
| 1040 | |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 1041 | mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1042 | |
| 1043 | if (IS_SD(mmc)) |
| 1044 | mmc->write_bl_len = mmc->read_bl_len; |
| 1045 | else |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 1046 | mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1047 | |
| 1048 | if (mmc->high_capacity) { |
| 1049 | csize = (mmc->csd[1] & 0x3f) << 16 |
| 1050 | | (mmc->csd[2] & 0xffff0000) >> 16; |
| 1051 | cmult = 8; |
| 1052 | } else { |
| 1053 | csize = (mmc->csd[1] & 0x3ff) << 2 |
| 1054 | | (mmc->csd[2] & 0xc0000000) >> 30; |
| 1055 | cmult = (mmc->csd[2] & 0x00038000) >> 15; |
| 1056 | } |
| 1057 | |
Stephen Warren | f866a46 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1058 | mmc->capacity_user = (csize + 1) << (cmult + 2); |
| 1059 | mmc->capacity_user *= mmc->read_bl_len; |
| 1060 | mmc->capacity_boot = 0; |
| 1061 | mmc->capacity_rpmb = 0; |
| 1062 | for (i = 0; i < 4; i++) |
| 1063 | mmc->capacity_gp[i] = 0; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1064 | |
Simon Glass | 8bfa195 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 1065 | if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN) |
| 1066 | mmc->read_bl_len = MMC_MAX_BLOCK_LEN; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1067 | |
Simon Glass | 8bfa195 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 1068 | if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN) |
| 1069 | mmc->write_bl_len = MMC_MAX_BLOCK_LEN; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1070 | |
| 1071 | /* Select the card, and put it into Transfer Mode */ |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1072 | if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ |
| 1073 | cmd.cmdidx = MMC_CMD_SELECT_CARD; |
Ajay Bhargav | fe8f706 | 2011-10-05 03:13:23 +0000 | [diff] [blame] | 1074 | cmd.resp_type = MMC_RSP_R1; |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1075 | cmd.cmdarg = mmc->rca << 16; |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1076 | err = mmc_send_cmd(mmc, &cmd, NULL); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1077 | |
Thomas Chou | d52ebf1 | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1078 | if (err) |
| 1079 | return err; |
| 1080 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1081 | |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 1082 | /* |
| 1083 | * For SD, its erase group is always one sector |
| 1084 | */ |
| 1085 | mmc->erase_grp_size = 1; |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1086 | mmc->part_config = MMCPART_NOAVAILABLE; |
Sukumar Ghorai | d23e2c0 | 2010-09-20 18:29:29 +0530 | [diff] [blame] | 1087 | if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) { |
| 1088 | /* check ext_csd version and capacity */ |
| 1089 | err = mmc_send_ext_csd(mmc, ext_csd); |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 1090 | if (!err && (ext_csd[EXT_CSD_REV] >= 2)) { |
Yoshihiro Shimoda | 639b782 | 2011-07-04 22:13:26 +0000 | [diff] [blame] | 1091 | /* |
| 1092 | * According to the JEDEC Standard, the value of |
| 1093 | * ext_csd's capacity is valid if the value is more |
| 1094 | * than 2GB |
| 1095 | */ |
Lei Wen | 0560db1 | 2011-10-03 20:35:10 +0000 | [diff] [blame] | 1096 | capacity = ext_csd[EXT_CSD_SEC_CNT] << 0 |
| 1097 | | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
| 1098 | | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
| 1099 | | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; |
Simon Glass | 8bfa195 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 1100 | capacity *= MMC_MAX_BLOCK_LEN; |
Łukasz Majewski | b1f1e821 | 2011-07-05 02:19:44 +0000 | [diff] [blame] | 1101 | if ((capacity >> 20) > 2 * 1024) |
Stephen Warren | f866a46 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1102 | mmc->capacity_user = capacity; |
Sukumar Ghorai | d23e2c0 | 2010-09-20 18:29:29 +0530 | [diff] [blame] | 1103 | } |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1104 | |
Jaehoon Chung | 64f4a61 | 2013-01-29 19:31:16 +0000 | [diff] [blame] | 1105 | switch (ext_csd[EXT_CSD_REV]) { |
| 1106 | case 1: |
| 1107 | mmc->version = MMC_VERSION_4_1; |
| 1108 | break; |
| 1109 | case 2: |
| 1110 | mmc->version = MMC_VERSION_4_2; |
| 1111 | break; |
| 1112 | case 3: |
| 1113 | mmc->version = MMC_VERSION_4_3; |
| 1114 | break; |
| 1115 | case 5: |
| 1116 | mmc->version = MMC_VERSION_4_41; |
| 1117 | break; |
| 1118 | case 6: |
| 1119 | mmc->version = MMC_VERSION_4_5; |
| 1120 | break; |
| 1121 | } |
| 1122 | |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 1123 | /* |
| 1124 | * Check whether GROUP_DEF is set, if yes, read out |
| 1125 | * group size from ext_csd directly, or calculate |
| 1126 | * the group size from the csd value. |
| 1127 | */ |
Simon Glass | 8bfa195 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 1128 | if (ext_csd[EXT_CSD_ERASE_GROUP_DEF]) { |
Lei Wen | 0560db1 | 2011-10-03 20:35:10 +0000 | [diff] [blame] | 1129 | mmc->erase_grp_size = |
Simon Glass | 8bfa195 | 2013-04-03 08:54:30 +0000 | [diff] [blame] | 1130 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * |
| 1131 | MMC_MAX_BLOCK_LEN * 1024; |
| 1132 | } else { |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 1133 | int erase_gsz, erase_gmul; |
| 1134 | erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10; |
| 1135 | erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5; |
| 1136 | mmc->erase_grp_size = (erase_gsz + 1) |
| 1137 | * (erase_gmul + 1); |
| 1138 | } |
| 1139 | |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1140 | /* store the partition info of emmc */ |
Stephen Warren | 8948ea8 | 2012-07-30 10:55:43 +0000 | [diff] [blame] | 1141 | if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) || |
| 1142 | ext_csd[EXT_CSD_BOOT_MULT]) |
Lei Wen | 0560db1 | 2011-10-03 20:35:10 +0000 | [diff] [blame] | 1143 | mmc->part_config = ext_csd[EXT_CSD_PART_CONF]; |
Stephen Warren | f866a46 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1144 | |
| 1145 | mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17; |
| 1146 | |
| 1147 | mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17; |
| 1148 | |
| 1149 | for (i = 0; i < 4; i++) { |
| 1150 | int idx = EXT_CSD_GP_SIZE_MULT + i * 3; |
| 1151 | mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) + |
| 1152 | (ext_csd[idx + 1] << 8) + ext_csd[idx]; |
| 1153 | mmc->capacity_gp[i] *= |
| 1154 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
| 1155 | mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
| 1156 | } |
Sukumar Ghorai | d23e2c0 | 2010-09-20 18:29:29 +0530 | [diff] [blame] | 1157 | } |
| 1158 | |
Stephen Warren | f866a46 | 2013-06-11 15:14:01 -0600 | [diff] [blame] | 1159 | err = mmc_set_capacity(mmc, mmc->part_num); |
| 1160 | if (err) |
| 1161 | return err; |
| 1162 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1163 | if (IS_SD(mmc)) |
| 1164 | err = sd_change_freq(mmc); |
| 1165 | else |
| 1166 | err = mmc_change_freq(mmc); |
| 1167 | |
| 1168 | if (err) |
| 1169 | return err; |
| 1170 | |
| 1171 | /* Restrict card's capabilities by what the host can do */ |
| 1172 | mmc->card_caps &= mmc->host_caps; |
| 1173 | |
| 1174 | if (IS_SD(mmc)) { |
| 1175 | if (mmc->card_caps & MMC_MODE_4BIT) { |
| 1176 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 1177 | cmd.resp_type = MMC_RSP_R1; |
| 1178 | cmd.cmdarg = mmc->rca << 16; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1179 | |
| 1180 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1181 | if (err) |
| 1182 | return err; |
| 1183 | |
| 1184 | cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; |
| 1185 | cmd.resp_type = MMC_RSP_R1; |
| 1186 | cmd.cmdarg = 2; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1187 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1188 | if (err) |
| 1189 | return err; |
| 1190 | |
| 1191 | mmc_set_bus_width(mmc, 4); |
| 1192 | } |
| 1193 | |
| 1194 | if (mmc->card_caps & MMC_MODE_HS) |
Jaehoon Chung | ad5fd92 | 2012-03-26 21:16:03 +0000 | [diff] [blame] | 1195 | mmc->tran_speed = 50000000; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1196 | else |
Jaehoon Chung | ad5fd92 | 2012-03-26 21:16:03 +0000 | [diff] [blame] | 1197 | mmc->tran_speed = 25000000; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1198 | } else { |
Andy Fleming | 7798f6d | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1199 | int idx; |
| 1200 | |
| 1201 | /* An array of possible bus widths in order of preference */ |
| 1202 | static unsigned ext_csd_bits[] = { |
| 1203 | EXT_CSD_BUS_WIDTH_8, |
| 1204 | EXT_CSD_BUS_WIDTH_4, |
| 1205 | EXT_CSD_BUS_WIDTH_1, |
| 1206 | }; |
| 1207 | |
| 1208 | /* An array to map CSD bus widths to host cap bits */ |
| 1209 | static unsigned ext_to_hostcaps[] = { |
| 1210 | [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT, |
| 1211 | [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT, |
| 1212 | }; |
| 1213 | |
| 1214 | /* An array to map chosen bus width to an integer */ |
| 1215 | static unsigned widths[] = { |
| 1216 | 8, 4, 1, |
| 1217 | }; |
| 1218 | |
| 1219 | for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) { |
| 1220 | unsigned int extw = ext_csd_bits[idx]; |
| 1221 | |
| 1222 | /* |
| 1223 | * Check to make sure the controller supports |
| 1224 | * this bus width, if it's more than 1 |
| 1225 | */ |
| 1226 | if (extw != EXT_CSD_BUS_WIDTH_1 && |
| 1227 | !(mmc->host_caps & ext_to_hostcaps[extw])) |
| 1228 | continue; |
| 1229 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1230 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
Andy Fleming | 7798f6d | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1231 | EXT_CSD_BUS_WIDTH, extw); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1232 | |
| 1233 | if (err) |
Lei Wen | 4137894 | 2011-10-03 20:35:11 +0000 | [diff] [blame] | 1234 | continue; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1235 | |
Andy Fleming | 7798f6d | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1236 | mmc_set_bus_width(mmc, widths[idx]); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1237 | |
Lei Wen | 4137894 | 2011-10-03 20:35:11 +0000 | [diff] [blame] | 1238 | err = mmc_send_ext_csd(mmc, test_csd); |
| 1239 | if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \ |
| 1240 | == test_csd[EXT_CSD_PARTITIONING_SUPPORT] |
| 1241 | && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \ |
| 1242 | == test_csd[EXT_CSD_ERASE_GROUP_DEF] \ |
| 1243 | && ext_csd[EXT_CSD_REV] \ |
| 1244 | == test_csd[EXT_CSD_REV] |
| 1245 | && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \ |
| 1246 | == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] |
| 1247 | && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \ |
| 1248 | &test_csd[EXT_CSD_SEC_CNT], 4) == 0) { |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1249 | |
Andy Fleming | 7798f6d | 2012-10-31 19:02:38 +0000 | [diff] [blame] | 1250 | mmc->card_caps |= ext_to_hostcaps[extw]; |
Lei Wen | 4137894 | 2011-10-03 20:35:11 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | if (mmc->card_caps & MMC_MODE_HS) { |
| 1256 | if (mmc->card_caps & MMC_MODE_HS_52MHz) |
Jaehoon Chung | ad5fd92 | 2012-03-26 21:16:03 +0000 | [diff] [blame] | 1257 | mmc->tran_speed = 52000000; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1258 | else |
Jaehoon Chung | ad5fd92 | 2012-03-26 21:16:03 +0000 | [diff] [blame] | 1259 | mmc->tran_speed = 26000000; |
| 1260 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1261 | } |
| 1262 | |
Jaehoon Chung | ad5fd92 | 2012-03-26 21:16:03 +0000 | [diff] [blame] | 1263 | mmc_set_clock(mmc, mmc->tran_speed); |
| 1264 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1265 | /* fill in device description */ |
| 1266 | mmc->block_dev.lun = 0; |
| 1267 | mmc->block_dev.type = 0; |
| 1268 | mmc->block_dev.blksz = mmc->read_bl_len; |
Egbert Eich | 0472fbf | 2013-04-09 21:11:56 +0000 | [diff] [blame] | 1269 | mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz); |
Rabin Vincent | 9b1f942 | 2009-04-05 13:30:54 +0530 | [diff] [blame] | 1270 | mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); |
Taylor Hutt | babce5f | 2012-10-20 17:15:59 +0000 | [diff] [blame] | 1271 | sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x", |
| 1272 | mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff), |
| 1273 | (mmc->cid[3] >> 16) & 0xffff); |
| 1274 | sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff, |
| 1275 | (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, |
| 1276 | (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff, |
| 1277 | (mmc->cid[2] >> 24) & 0xff); |
| 1278 | sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf, |
| 1279 | (mmc->cid[2] >> 16) & 0xf); |
Mikhail Kshevetskiy | 122efd4 | 2012-07-09 08:53:38 +0000 | [diff] [blame] | 1280 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1281 | init_part(&mmc->block_dev); |
Mikhail Kshevetskiy | 122efd4 | 2012-07-09 08:53:38 +0000 | [diff] [blame] | 1282 | #endif |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1283 | |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
Kim Phillips | fdbb873 | 2012-10-29 13:34:43 +0000 | [diff] [blame] | 1287 | static int mmc_send_if_cond(struct mmc *mmc) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1288 | { |
| 1289 | struct mmc_cmd cmd; |
| 1290 | int err; |
| 1291 | |
| 1292 | cmd.cmdidx = SD_CMD_SEND_IF_COND; |
| 1293 | /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ |
| 1294 | cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa; |
| 1295 | cmd.resp_type = MMC_RSP_R7; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1296 | |
| 1297 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1298 | |
| 1299 | if (err) |
| 1300 | return err; |
| 1301 | |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 1302 | if ((cmd.response[0] & 0xff) != 0xaa) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1303 | return UNUSABLE_ERR; |
| 1304 | else |
| 1305 | mmc->version = SD_VERSION_2; |
| 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | int mmc_register(struct mmc *mmc) |
| 1311 | { |
| 1312 | /* Setup the universal parts of the block interface just once */ |
| 1313 | mmc->block_dev.if_type = IF_TYPE_MMC; |
| 1314 | mmc->block_dev.dev = cur_dev_num++; |
| 1315 | mmc->block_dev.removable = 1; |
| 1316 | mmc->block_dev.block_read = mmc_bread; |
| 1317 | mmc->block_dev.block_write = mmc_bwrite; |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 1318 | mmc->block_dev.block_erase = mmc_berase; |
John Rigby | 8feafcc | 2011-04-18 05:50:08 +0000 | [diff] [blame] | 1319 | if (!mmc->b_max) |
| 1320 | mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1321 | |
| 1322 | INIT_LIST_HEAD (&mmc->link); |
| 1323 | |
| 1324 | list_add_tail (&mmc->link, &mmc_devices); |
| 1325 | |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 1329 | #ifdef CONFIG_PARTITIONS |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1330 | block_dev_desc_t *mmc_get_dev(int dev) |
| 1331 | { |
| 1332 | struct mmc *mmc = find_mmc_device(dev); |
Benoît Thébaudeau | 6bb4b4b | 2012-08-10 08:59:12 +0000 | [diff] [blame] | 1333 | if (!mmc || mmc_init(mmc)) |
Łukasz Majewski | 40242bc | 2012-04-19 02:39:18 +0000 | [diff] [blame] | 1334 | return NULL; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1335 | |
Łukasz Majewski | 40242bc | 2012-04-19 02:39:18 +0000 | [diff] [blame] | 1336 | return &mmc->block_dev; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1337 | } |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 1338 | #endif |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1339 | |
Che-Liang Chiou | e955044 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1340 | int mmc_start_init(struct mmc *mmc) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1341 | { |
Macpaul Lin | afd5932 | 2011-11-14 23:35:39 +0000 | [diff] [blame] | 1342 | int err; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1343 | |
Thierry Reding | 48972d9 | 2012-01-02 01:15:37 +0000 | [diff] [blame] | 1344 | if (mmc_getcd(mmc) == 0) { |
| 1345 | mmc->has_init = 0; |
| 1346 | printf("MMC: no card present\n"); |
| 1347 | return NO_CARD_ERR; |
| 1348 | } |
| 1349 | |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1350 | if (mmc->has_init) |
| 1351 | return 0; |
| 1352 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1353 | err = mmc->init(mmc); |
| 1354 | |
| 1355 | if (err) |
| 1356 | return err; |
| 1357 | |
Ilya Yanok | b86b85e | 2009-06-29 17:53:16 +0400 | [diff] [blame] | 1358 | mmc_set_bus_width(mmc, 1); |
| 1359 | mmc_set_clock(mmc, 1); |
| 1360 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1361 | /* Reset the Card */ |
| 1362 | err = mmc_go_idle(mmc); |
| 1363 | |
| 1364 | if (err) |
| 1365 | return err; |
| 1366 | |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1367 | /* The internal partition reset to user partition(0) at every CMD0*/ |
| 1368 | mmc->part_num = 0; |
| 1369 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1370 | /* Test for SD version 2 */ |
Macpaul Lin | afd5932 | 2011-11-14 23:35:39 +0000 | [diff] [blame] | 1371 | err = mmc_send_if_cond(mmc); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1372 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1373 | /* Now try to get the SD card's operating condition */ |
| 1374 | err = sd_send_op_cond(mmc); |
| 1375 | |
| 1376 | /* If the command timed out, we check for an MMC card */ |
| 1377 | if (err == TIMEOUT) { |
| 1378 | err = mmc_send_op_cond(mmc); |
| 1379 | |
Che-Liang Chiou | e955044 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1380 | if (err && err != IN_PROGRESS) { |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1381 | printf("Card did not respond to voltage select!\n"); |
| 1382 | return UNUSABLE_ERR; |
| 1383 | } |
| 1384 | } |
| 1385 | |
Che-Liang Chiou | e955044 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1386 | if (err == IN_PROGRESS) |
| 1387 | mmc->init_in_progress = 1; |
| 1388 | |
| 1389 | return err; |
| 1390 | } |
| 1391 | |
| 1392 | static int mmc_complete_init(struct mmc *mmc) |
| 1393 | { |
| 1394 | int err = 0; |
| 1395 | |
| 1396 | if (mmc->op_cond_pending) |
| 1397 | err = mmc_complete_op_cond(mmc); |
| 1398 | |
| 1399 | if (!err) |
| 1400 | err = mmc_startup(mmc); |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1401 | if (err) |
| 1402 | mmc->has_init = 0; |
| 1403 | else |
| 1404 | mmc->has_init = 1; |
Che-Liang Chiou | e955044 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1405 | mmc->init_in_progress = 0; |
| 1406 | return err; |
| 1407 | } |
| 1408 | |
| 1409 | int mmc_init(struct mmc *mmc) |
| 1410 | { |
| 1411 | int err = IN_PROGRESS; |
| 1412 | unsigned start = get_timer(0); |
| 1413 | |
| 1414 | if (mmc->has_init) |
| 1415 | return 0; |
| 1416 | if (!mmc->init_in_progress) |
| 1417 | err = mmc_start_init(mmc); |
| 1418 | |
| 1419 | if (!err || err == IN_PROGRESS) |
| 1420 | err = mmc_complete_init(mmc); |
| 1421 | debug("%s: %d, time %lu\n", __func__, err, get_timer(start)); |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 1422 | return err; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | /* |
| 1426 | * CPU and board-specific MMC initializations. Aliased function |
| 1427 | * signals caller to move on |
| 1428 | */ |
| 1429 | static int __def_mmc_init(bd_t *bis) |
| 1430 | { |
| 1431 | return -1; |
| 1432 | } |
| 1433 | |
Peter Tyser | f9a109b | 2009-04-20 11:08:46 -0500 | [diff] [blame] | 1434 | int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); |
| 1435 | int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1436 | |
| 1437 | void print_mmc_devices(char separator) |
| 1438 | { |
| 1439 | struct mmc *m; |
| 1440 | struct list_head *entry; |
| 1441 | |
| 1442 | list_for_each(entry, &mmc_devices) { |
| 1443 | m = list_entry(entry, struct mmc, link); |
| 1444 | |
| 1445 | printf("%s: %d", m->name, m->block_dev.dev); |
| 1446 | |
| 1447 | if (entry->next != &mmc_devices) |
| 1448 | printf("%c ", separator); |
| 1449 | } |
| 1450 | |
| 1451 | printf("\n"); |
| 1452 | } |
| 1453 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 1454 | int get_mmc_num(void) |
| 1455 | { |
| 1456 | return cur_dev_num; |
| 1457 | } |
| 1458 | |
Che-Liang Chiou | e955044 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1459 | void mmc_set_preinit(struct mmc *mmc, int preinit) |
| 1460 | { |
| 1461 | mmc->preinit = preinit; |
| 1462 | } |
| 1463 | |
| 1464 | static void do_preinit(void) |
| 1465 | { |
| 1466 | struct mmc *m; |
| 1467 | struct list_head *entry; |
| 1468 | |
| 1469 | list_for_each(entry, &mmc_devices) { |
| 1470 | m = list_entry(entry, struct mmc, link); |
| 1471 | |
| 1472 | if (m->preinit) |
| 1473 | mmc_start_init(m); |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1478 | int mmc_initialize(bd_t *bis) |
| 1479 | { |
| 1480 | INIT_LIST_HEAD (&mmc_devices); |
| 1481 | cur_dev_num = 0; |
| 1482 | |
| 1483 | if (board_mmc_init(bis) < 0) |
| 1484 | cpu_mmc_init(bis); |
| 1485 | |
| 1486 | print_mmc_devices(','); |
| 1487 | |
Che-Liang Chiou | e955044 | 2012-11-28 15:21:13 +0000 | [diff] [blame] | 1488 | do_preinit(); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 1489 | return 0; |
| 1490 | } |
Amar | 3690d6d | 2013-04-27 11:42:58 +0530 | [diff] [blame] | 1491 | |
| 1492 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
| 1493 | /* |
| 1494 | * This function changes the size of boot partition and the size of rpmb |
| 1495 | * partition present on EMMC devices. |
| 1496 | * |
| 1497 | * Input Parameters: |
| 1498 | * struct *mmc: pointer for the mmc device strcuture |
| 1499 | * bootsize: size of boot partition |
| 1500 | * rpmbsize: size of rpmb partition |
| 1501 | * |
| 1502 | * Returns 0 on success. |
| 1503 | */ |
| 1504 | |
| 1505 | int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize, |
| 1506 | unsigned long rpmbsize) |
| 1507 | { |
| 1508 | int err; |
| 1509 | struct mmc_cmd cmd; |
| 1510 | |
| 1511 | /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */ |
| 1512 | cmd.cmdidx = MMC_CMD_RES_MAN; |
| 1513 | cmd.resp_type = MMC_RSP_R1b; |
| 1514 | cmd.cmdarg = MMC_CMD62_ARG1; |
| 1515 | |
| 1516 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1517 | if (err) { |
| 1518 | debug("mmc_boot_partition_size_change: Error1 = %d\n", err); |
| 1519 | return err; |
| 1520 | } |
| 1521 | |
| 1522 | /* Boot partition changing mode */ |
| 1523 | cmd.cmdidx = MMC_CMD_RES_MAN; |
| 1524 | cmd.resp_type = MMC_RSP_R1b; |
| 1525 | cmd.cmdarg = MMC_CMD62_ARG2; |
| 1526 | |
| 1527 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1528 | if (err) { |
| 1529 | debug("mmc_boot_partition_size_change: Error2 = %d\n", err); |
| 1530 | return err; |
| 1531 | } |
| 1532 | /* boot partition size is multiple of 128KB */ |
| 1533 | bootsize = (bootsize * 1024) / 128; |
| 1534 | |
| 1535 | /* Arg: boot partition size */ |
| 1536 | cmd.cmdidx = MMC_CMD_RES_MAN; |
| 1537 | cmd.resp_type = MMC_RSP_R1b; |
| 1538 | cmd.cmdarg = bootsize; |
| 1539 | |
| 1540 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1541 | if (err) { |
| 1542 | debug("mmc_boot_partition_size_change: Error3 = %d\n", err); |
| 1543 | return err; |
| 1544 | } |
| 1545 | /* RPMB partition size is multiple of 128KB */ |
| 1546 | rpmbsize = (rpmbsize * 1024) / 128; |
| 1547 | /* Arg: RPMB partition size */ |
| 1548 | cmd.cmdidx = MMC_CMD_RES_MAN; |
| 1549 | cmd.resp_type = MMC_RSP_R1b; |
| 1550 | cmd.cmdarg = rpmbsize; |
| 1551 | |
| 1552 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1553 | if (err) { |
| 1554 | debug("mmc_boot_partition_size_change: Error4 = %d\n", err); |
| 1555 | return err; |
| 1556 | } |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
| 1560 | /* |
| 1561 | * This function shall form and send the commands to open / close the |
| 1562 | * boot partition specified by user. |
| 1563 | * |
| 1564 | * Input Parameters: |
| 1565 | * ack: 0x0 - No boot acknowledge sent (default) |
| 1566 | * 0x1 - Boot acknowledge sent during boot operation |
| 1567 | * part_num: User selects boot data that will be sent to master |
| 1568 | * 0x0 - Device not boot enabled (default) |
| 1569 | * 0x1 - Boot partition 1 enabled for boot |
| 1570 | * 0x2 - Boot partition 2 enabled for boot |
| 1571 | * access: User selects partitions to access |
| 1572 | * 0x0 : No access to boot partition (default) |
| 1573 | * 0x1 : R/W boot partition 1 |
| 1574 | * 0x2 : R/W boot partition 2 |
| 1575 | * 0x3 : R/W Replay Protected Memory Block (RPMB) |
| 1576 | * |
| 1577 | * Returns 0 on success. |
| 1578 | */ |
| 1579 | int mmc_boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access) |
| 1580 | { |
| 1581 | int err; |
| 1582 | struct mmc_cmd cmd; |
| 1583 | |
| 1584 | /* Boot ack enable, boot partition enable , boot partition access */ |
| 1585 | cmd.cmdidx = MMC_CMD_SWITCH; |
| 1586 | cmd.resp_type = MMC_RSP_R1b; |
| 1587 | |
| 1588 | cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
| 1589 | (EXT_CSD_PART_CONF << 16) | |
| 1590 | ((EXT_CSD_BOOT_ACK(ack) | |
| 1591 | EXT_CSD_BOOT_PART_NUM(part_num) | |
| 1592 | EXT_CSD_PARTITION_ACCESS(access)) << 8); |
| 1593 | |
| 1594 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1595 | if (err) { |
| 1596 | if (access) { |
| 1597 | debug("mmc boot partition#%d open fail:Error1 = %d\n", |
| 1598 | part_num, err); |
| 1599 | } else { |
| 1600 | debug("mmc boot partition#%d close fail:Error = %d\n", |
| 1601 | part_num, err); |
| 1602 | } |
| 1603 | return err; |
| 1604 | } |
| 1605 | |
| 1606 | if (access) { |
| 1607 | /* 4bit transfer mode at booting time. */ |
| 1608 | cmd.cmdidx = MMC_CMD_SWITCH; |
| 1609 | cmd.resp_type = MMC_RSP_R1b; |
| 1610 | |
| 1611 | cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
| 1612 | (EXT_CSD_BOOT_BUS_WIDTH << 16) | |
| 1613 | ((1 << 0) << 8); |
| 1614 | |
| 1615 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 1616 | if (err) { |
| 1617 | debug("mmc boot partition#%d open fail:Error2 = %d\n", |
| 1618 | part_num, err); |
| 1619 | return err; |
| 1620 | } |
| 1621 | } |
| 1622 | return 0; |
| 1623 | } |
| 1624 | #endif |