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