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