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