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> |
| 33 | #include <mmc.h> |
Rabin Vincent | 9b1f942 | 2009-04-05 13:30:54 +0530 | [diff] [blame] | 34 | #include <div64.h> |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 35 | |
| 36 | static struct list_head mmc_devices; |
| 37 | static int cur_dev_num = -1; |
| 38 | |
Stefano Babic | 11fdade | 2010-02-05 15:04:43 +0100 | [diff] [blame] | 39 | int __board_mmc_getcd(u8 *cd, struct mmc *mmc) { |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak, |
| 44 | alias("__board_mmc_getcd"))); |
| 45 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 46 | int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 47 | { |
| 48 | return mmc->send_cmd(mmc, cmd, data); |
| 49 | } |
| 50 | |
| 51 | int mmc_set_blocklen(struct mmc *mmc, int len) |
| 52 | { |
| 53 | struct mmc_cmd cmd; |
| 54 | |
| 55 | cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; |
| 56 | cmd.resp_type = MMC_RSP_R1; |
| 57 | cmd.cmdarg = len; |
| 58 | cmd.flags = 0; |
| 59 | |
| 60 | return mmc_send_cmd(mmc, &cmd, NULL); |
| 61 | } |
| 62 | |
| 63 | struct mmc *find_mmc_device(int dev_num) |
| 64 | { |
| 65 | struct mmc *m; |
| 66 | struct list_head *entry; |
| 67 | |
| 68 | list_for_each(entry, &mmc_devices) { |
| 69 | m = list_entry(entry, struct mmc, link); |
| 70 | |
| 71 | if (m->block_dev.dev == dev_num) |
| 72 | return m; |
| 73 | } |
| 74 | |
| 75 | printf("MMC Device %d not found\n", dev_num); |
| 76 | |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | static ulong |
| 81 | mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src) |
| 82 | { |
| 83 | struct mmc_cmd cmd; |
| 84 | struct mmc_data data; |
| 85 | int err; |
| 86 | int stoperr = 0; |
| 87 | struct mmc *mmc = find_mmc_device(dev_num); |
| 88 | int blklen; |
| 89 | |
| 90 | if (!mmc) |
| 91 | return -1; |
| 92 | |
| 93 | blklen = mmc->write_bl_len; |
| 94 | |
Lei Wen | d2bf29e | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 95 | if ((start + blkcnt) > mmc->block_dev.lba) { |
Wolfgang Denk | fe64fd4 | 2010-09-19 01:03:20 +0200 | [diff] [blame] | 96 | printf("MMC: block number 0x%lx exceeds max(0x%lx)", |
Lei Wen | d2bf29e | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 97 | start + blkcnt, mmc->block_dev.lba); |
| 98 | return 0; |
| 99 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 100 | err = mmc_set_blocklen(mmc, mmc->write_bl_len); |
| 101 | |
| 102 | if (err) { |
| 103 | printf("set write bl len failed\n\r"); |
| 104 | return err; |
| 105 | } |
| 106 | |
| 107 | if (blkcnt > 1) |
| 108 | cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; |
| 109 | else |
| 110 | cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; |
| 111 | |
| 112 | if (mmc->high_capacity) |
| 113 | cmd.cmdarg = start; |
| 114 | else |
| 115 | cmd.cmdarg = start * blklen; |
| 116 | |
| 117 | cmd.resp_type = MMC_RSP_R1; |
| 118 | cmd.flags = 0; |
| 119 | |
| 120 | data.src = src; |
| 121 | data.blocks = blkcnt; |
| 122 | data.blocksize = blklen; |
| 123 | data.flags = MMC_DATA_WRITE; |
| 124 | |
| 125 | err = mmc_send_cmd(mmc, &cmd, &data); |
| 126 | |
| 127 | if (err) { |
| 128 | printf("mmc write failed\n\r"); |
| 129 | return err; |
| 130 | } |
| 131 | |
| 132 | if (blkcnt > 1) { |
| 133 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; |
| 134 | cmd.cmdarg = 0; |
| 135 | cmd.resp_type = MMC_RSP_R1b; |
| 136 | cmd.flags = 0; |
| 137 | stoperr = mmc_send_cmd(mmc, &cmd, NULL); |
| 138 | } |
| 139 | |
| 140 | return blkcnt; |
| 141 | } |
| 142 | |
| 143 | int mmc_read_block(struct mmc *mmc, void *dst, uint blocknum) |
| 144 | { |
| 145 | struct mmc_cmd cmd; |
| 146 | struct mmc_data data; |
| 147 | |
| 148 | cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; |
| 149 | |
| 150 | if (mmc->high_capacity) |
| 151 | cmd.cmdarg = blocknum; |
| 152 | else |
| 153 | cmd.cmdarg = blocknum * mmc->read_bl_len; |
| 154 | |
| 155 | cmd.resp_type = MMC_RSP_R1; |
| 156 | cmd.flags = 0; |
| 157 | |
| 158 | data.dest = dst; |
| 159 | data.blocks = 1; |
| 160 | data.blocksize = mmc->read_bl_len; |
| 161 | data.flags = MMC_DATA_READ; |
| 162 | |
| 163 | return mmc_send_cmd(mmc, &cmd, &data); |
| 164 | } |
| 165 | |
| 166 | int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size) |
| 167 | { |
| 168 | char *buffer; |
| 169 | int i; |
| 170 | int blklen = mmc->read_bl_len; |
Rabin Vincent | 9b1f942 | 2009-04-05 13:30:54 +0530 | [diff] [blame] | 171 | int startblock = lldiv(src, mmc->read_bl_len); |
| 172 | int endblock = lldiv(src + size - 1, mmc->read_bl_len); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 173 | int err = 0; |
| 174 | |
| 175 | /* Make a buffer big enough to hold all the blocks we might read */ |
| 176 | buffer = malloc(blklen); |
| 177 | |
| 178 | if (!buffer) { |
| 179 | printf("Could not allocate buffer for MMC read!\n"); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | /* We always do full block reads from the card */ |
| 184 | err = mmc_set_blocklen(mmc, mmc->read_bl_len); |
| 185 | |
| 186 | if (err) |
Wolfgang Denk | 8c4444f | 2010-03-11 23:35:43 +0100 | [diff] [blame] | 187 | goto free_buffer; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 188 | |
| 189 | for (i = startblock; i <= endblock; i++) { |
| 190 | int segment_size; |
| 191 | int offset; |
| 192 | |
| 193 | err = mmc_read_block(mmc, buffer, i); |
| 194 | |
| 195 | if (err) |
| 196 | goto free_buffer; |
| 197 | |
| 198 | /* |
| 199 | * The first block may not be aligned, so we |
| 200 | * copy from the desired point in the block |
| 201 | */ |
| 202 | offset = (src & (blklen - 1)); |
| 203 | segment_size = MIN(blklen - offset, size); |
| 204 | |
| 205 | memcpy(dst, buffer + offset, segment_size); |
| 206 | |
| 207 | dst += segment_size; |
| 208 | src += segment_size; |
| 209 | size -= segment_size; |
| 210 | } |
| 211 | |
| 212 | free_buffer: |
| 213 | free(buffer); |
| 214 | |
| 215 | return err; |
| 216 | } |
| 217 | |
| 218 | static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst) |
| 219 | { |
| 220 | int err; |
| 221 | int i; |
| 222 | struct mmc *mmc = find_mmc_device(dev_num); |
| 223 | |
| 224 | if (!mmc) |
| 225 | return 0; |
| 226 | |
Lei Wen | d2bf29e | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 227 | if ((start + blkcnt) > mmc->block_dev.lba) { |
Wolfgang Denk | fe64fd4 | 2010-09-19 01:03:20 +0200 | [diff] [blame] | 228 | printf("MMC: block number 0x%lx exceeds max(0x%lx)", |
Lei Wen | d2bf29e | 2010-09-13 22:07:27 +0800 | [diff] [blame] | 229 | start + blkcnt, mmc->block_dev.lba); |
| 230 | return 0; |
| 231 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 232 | /* We always do full block reads from the card */ |
| 233 | err = mmc_set_blocklen(mmc, mmc->read_bl_len); |
| 234 | |
| 235 | if (err) { |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) { |
| 240 | err = mmc_read_block(mmc, dst, i); |
| 241 | |
| 242 | if (err) { |
| 243 | printf("block read failed: %d\n", err); |
| 244 | return i - start; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return blkcnt; |
| 249 | } |
| 250 | |
| 251 | int mmc_go_idle(struct mmc* mmc) |
| 252 | { |
| 253 | struct mmc_cmd cmd; |
| 254 | int err; |
| 255 | |
| 256 | udelay(1000); |
| 257 | |
| 258 | cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; |
| 259 | cmd.cmdarg = 0; |
| 260 | cmd.resp_type = MMC_RSP_NONE; |
| 261 | cmd.flags = 0; |
| 262 | |
| 263 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 264 | |
| 265 | if (err) |
| 266 | return err; |
| 267 | |
| 268 | udelay(2000); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | int |
| 274 | sd_send_op_cond(struct mmc *mmc) |
| 275 | { |
| 276 | int timeout = 1000; |
| 277 | int err; |
| 278 | struct mmc_cmd cmd; |
| 279 | |
| 280 | do { |
| 281 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 282 | cmd.resp_type = MMC_RSP_R1; |
| 283 | cmd.cmdarg = 0; |
| 284 | cmd.flags = 0; |
| 285 | |
| 286 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 287 | |
| 288 | if (err) |
| 289 | return err; |
| 290 | |
| 291 | cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; |
| 292 | cmd.resp_type = MMC_RSP_R3; |
Stefano Babic | 250de12 | 2010-01-20 18:20:39 +0100 | [diff] [blame] | 293 | |
| 294 | /* |
| 295 | * Most cards do not answer if some reserved bits |
| 296 | * in the ocr are set. However, Some controller |
| 297 | * can set bit 7 (reserved for low voltages), but |
| 298 | * how to manage low voltages SD card is not yet |
| 299 | * specified. |
| 300 | */ |
| 301 | cmd.cmdarg = mmc->voltages & 0xff8000; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 302 | |
| 303 | if (mmc->version == SD_VERSION_2) |
| 304 | cmd.cmdarg |= OCR_HCS; |
| 305 | |
| 306 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 307 | |
| 308 | if (err) |
| 309 | return err; |
| 310 | |
| 311 | udelay(1000); |
| 312 | } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--); |
| 313 | |
| 314 | if (timeout <= 0) |
| 315 | return UNUSABLE_ERR; |
| 316 | |
| 317 | if (mmc->version != SD_VERSION_2) |
| 318 | mmc->version = SD_VERSION_1_0; |
| 319 | |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 320 | mmc->ocr = cmd.response[0]; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 321 | |
| 322 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); |
| 323 | mmc->rca = 0; |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | int mmc_send_op_cond(struct mmc *mmc) |
| 329 | { |
| 330 | int timeout = 1000; |
| 331 | struct mmc_cmd cmd; |
| 332 | int err; |
| 333 | |
| 334 | /* Some cards seem to need this */ |
| 335 | mmc_go_idle(mmc); |
| 336 | |
| 337 | do { |
| 338 | cmd.cmdidx = MMC_CMD_SEND_OP_COND; |
| 339 | cmd.resp_type = MMC_RSP_R3; |
| 340 | cmd.cmdarg = OCR_HCS | mmc->voltages; |
| 341 | cmd.flags = 0; |
| 342 | |
| 343 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 344 | |
| 345 | if (err) |
| 346 | return err; |
| 347 | |
| 348 | udelay(1000); |
| 349 | } while (!(cmd.response[0] & OCR_BUSY) && timeout--); |
| 350 | |
| 351 | if (timeout <= 0) |
| 352 | return UNUSABLE_ERR; |
| 353 | |
| 354 | mmc->version = MMC_VERSION_UNKNOWN; |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 355 | mmc->ocr = cmd.response[0]; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 356 | |
| 357 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); |
| 358 | mmc->rca = 0; |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd) |
| 365 | { |
| 366 | struct mmc_cmd cmd; |
| 367 | struct mmc_data data; |
| 368 | int err; |
| 369 | |
| 370 | /* Get the Card Status Register */ |
| 371 | cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; |
| 372 | cmd.resp_type = MMC_RSP_R1; |
| 373 | cmd.cmdarg = 0; |
| 374 | cmd.flags = 0; |
| 375 | |
| 376 | data.dest = ext_csd; |
| 377 | data.blocks = 1; |
| 378 | data.blocksize = 512; |
| 379 | data.flags = MMC_DATA_READ; |
| 380 | |
| 381 | err = mmc_send_cmd(mmc, &cmd, &data); |
| 382 | |
| 383 | return err; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) |
| 388 | { |
| 389 | struct mmc_cmd cmd; |
| 390 | |
| 391 | cmd.cmdidx = MMC_CMD_SWITCH; |
| 392 | cmd.resp_type = MMC_RSP_R1b; |
| 393 | cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
| 394 | (index << 16) | |
| 395 | (value << 8); |
| 396 | cmd.flags = 0; |
| 397 | |
| 398 | return mmc_send_cmd(mmc, &cmd, NULL); |
| 399 | } |
| 400 | |
| 401 | int mmc_change_freq(struct mmc *mmc) |
| 402 | { |
| 403 | char ext_csd[512]; |
| 404 | char cardtype; |
| 405 | int err; |
| 406 | |
| 407 | mmc->card_caps = 0; |
| 408 | |
| 409 | /* Only version 4 supports high-speed */ |
| 410 | if (mmc->version < MMC_VERSION_4) |
| 411 | return 0; |
| 412 | |
| 413 | mmc->card_caps |= MMC_MODE_4BIT; |
| 414 | |
| 415 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 416 | |
| 417 | if (err) |
| 418 | return err; |
| 419 | |
| 420 | if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215]) |
| 421 | mmc->high_capacity = 1; |
| 422 | |
| 423 | cardtype = ext_csd[196] & 0xf; |
| 424 | |
| 425 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); |
| 426 | |
| 427 | if (err) |
| 428 | return err; |
| 429 | |
| 430 | /* Now check to see that it worked */ |
| 431 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 432 | |
| 433 | if (err) |
| 434 | return err; |
| 435 | |
| 436 | /* No high-speed support */ |
| 437 | if (!ext_csd[185]) |
| 438 | return 0; |
| 439 | |
| 440 | /* High Speed is set, there are two types: 52MHz and 26MHz */ |
| 441 | if (cardtype & MMC_HS_52MHZ) |
| 442 | mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
| 443 | else |
| 444 | mmc->card_caps |= MMC_MODE_HS; |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) |
| 450 | { |
| 451 | struct mmc_cmd cmd; |
| 452 | struct mmc_data data; |
| 453 | |
| 454 | /* Switch the frequency */ |
| 455 | cmd.cmdidx = SD_CMD_SWITCH_FUNC; |
| 456 | cmd.resp_type = MMC_RSP_R1; |
| 457 | cmd.cmdarg = (mode << 31) | 0xffffff; |
| 458 | cmd.cmdarg &= ~(0xf << (group * 4)); |
| 459 | cmd.cmdarg |= value << (group * 4); |
| 460 | cmd.flags = 0; |
| 461 | |
| 462 | data.dest = (char *)resp; |
| 463 | data.blocksize = 64; |
| 464 | data.blocks = 1; |
| 465 | data.flags = MMC_DATA_READ; |
| 466 | |
| 467 | return mmc_send_cmd(mmc, &cmd, &data); |
| 468 | } |
| 469 | |
| 470 | |
| 471 | int sd_change_freq(struct mmc *mmc) |
| 472 | { |
| 473 | int err; |
| 474 | struct mmc_cmd cmd; |
| 475 | uint scr[2]; |
| 476 | uint switch_status[16]; |
| 477 | struct mmc_data data; |
| 478 | int timeout; |
| 479 | |
| 480 | mmc->card_caps = 0; |
| 481 | |
| 482 | /* Read the SCR to find out if this card supports higher speeds */ |
| 483 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 484 | cmd.resp_type = MMC_RSP_R1; |
| 485 | cmd.cmdarg = mmc->rca << 16; |
| 486 | cmd.flags = 0; |
| 487 | |
| 488 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 489 | |
| 490 | if (err) |
| 491 | return err; |
| 492 | |
| 493 | cmd.cmdidx = SD_CMD_APP_SEND_SCR; |
| 494 | cmd.resp_type = MMC_RSP_R1; |
| 495 | cmd.cmdarg = 0; |
| 496 | cmd.flags = 0; |
| 497 | |
| 498 | timeout = 3; |
| 499 | |
| 500 | retry_scr: |
| 501 | data.dest = (char *)&scr; |
| 502 | data.blocksize = 8; |
| 503 | data.blocks = 1; |
| 504 | data.flags = MMC_DATA_READ; |
| 505 | |
| 506 | err = mmc_send_cmd(mmc, &cmd, &data); |
| 507 | |
| 508 | if (err) { |
| 509 | if (timeout--) |
| 510 | goto retry_scr; |
| 511 | |
| 512 | return err; |
| 513 | } |
| 514 | |
Yauhen Kharuzhy | 4e3d89b | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 515 | mmc->scr[0] = __be32_to_cpu(scr[0]); |
| 516 | mmc->scr[1] = __be32_to_cpu(scr[1]); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 517 | |
| 518 | switch ((mmc->scr[0] >> 24) & 0xf) { |
| 519 | case 0: |
| 520 | mmc->version = SD_VERSION_1_0; |
| 521 | break; |
| 522 | case 1: |
| 523 | mmc->version = SD_VERSION_1_10; |
| 524 | break; |
| 525 | case 2: |
| 526 | mmc->version = SD_VERSION_2; |
| 527 | break; |
| 528 | default: |
| 529 | mmc->version = SD_VERSION_1_0; |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | /* Version 1.0 doesn't support switching */ |
| 534 | if (mmc->version == SD_VERSION_1_0) |
| 535 | return 0; |
| 536 | |
| 537 | timeout = 4; |
| 538 | while (timeout--) { |
| 539 | err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, |
| 540 | (u8 *)&switch_status); |
| 541 | |
| 542 | if (err) |
| 543 | return err; |
| 544 | |
| 545 | /* The high-speed function is busy. Try again */ |
Yauhen Kharuzhy | 4e3d89b | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 546 | if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | |
| 550 | if (mmc->scr[0] & SD_DATA_4BIT) |
| 551 | mmc->card_caps |= MMC_MODE_4BIT; |
| 552 | |
| 553 | /* If high-speed isn't supported, we return */ |
Yauhen Kharuzhy | 4e3d89b | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 554 | if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 555 | return 0; |
| 556 | |
| 557 | err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status); |
| 558 | |
| 559 | if (err) |
| 560 | return err; |
| 561 | |
Yauhen Kharuzhy | 4e3d89b | 2009-05-07 00:43:30 +0300 | [diff] [blame] | 562 | if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 563 | mmc->card_caps |= MMC_MODE_HS; |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | /* frequency bases */ |
| 569 | /* divided by 10 to be nice to platforms without floating point */ |
| 570 | int fbase[] = { |
| 571 | 10000, |
| 572 | 100000, |
| 573 | 1000000, |
| 574 | 10000000, |
| 575 | }; |
| 576 | |
| 577 | /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice |
| 578 | * to platforms without floating point. |
| 579 | */ |
| 580 | int multipliers[] = { |
| 581 | 0, /* reserved */ |
| 582 | 10, |
| 583 | 12, |
| 584 | 13, |
| 585 | 15, |
| 586 | 20, |
| 587 | 25, |
| 588 | 30, |
| 589 | 35, |
| 590 | 40, |
| 591 | 45, |
| 592 | 50, |
| 593 | 55, |
| 594 | 60, |
| 595 | 70, |
| 596 | 80, |
| 597 | }; |
| 598 | |
| 599 | void mmc_set_ios(struct mmc *mmc) |
| 600 | { |
| 601 | mmc->set_ios(mmc); |
| 602 | } |
| 603 | |
| 604 | void mmc_set_clock(struct mmc *mmc, uint clock) |
| 605 | { |
| 606 | if (clock > mmc->f_max) |
| 607 | clock = mmc->f_max; |
| 608 | |
| 609 | if (clock < mmc->f_min) |
| 610 | clock = mmc->f_min; |
| 611 | |
| 612 | mmc->clock = clock; |
| 613 | |
| 614 | mmc_set_ios(mmc); |
| 615 | } |
| 616 | |
| 617 | void mmc_set_bus_width(struct mmc *mmc, uint width) |
| 618 | { |
| 619 | mmc->bus_width = width; |
| 620 | |
| 621 | mmc_set_ios(mmc); |
| 622 | } |
| 623 | |
| 624 | int mmc_startup(struct mmc *mmc) |
| 625 | { |
| 626 | int err; |
| 627 | uint mult, freq; |
| 628 | u64 cmult, csize; |
| 629 | struct mmc_cmd cmd; |
Sukumar Ghorai | d23e2c0 | 2010-09-20 18:29:29 +0530 | [diff] [blame] | 630 | char ext_csd[512]; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 631 | |
| 632 | /* Put the Card in Identify Mode */ |
| 633 | cmd.cmdidx = MMC_CMD_ALL_SEND_CID; |
| 634 | cmd.resp_type = MMC_RSP_R2; |
| 635 | cmd.cmdarg = 0; |
| 636 | cmd.flags = 0; |
| 637 | |
| 638 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 639 | |
| 640 | if (err) |
| 641 | return err; |
| 642 | |
| 643 | memcpy(mmc->cid, cmd.response, 16); |
| 644 | |
| 645 | /* |
| 646 | * For MMC cards, set the Relative Address. |
| 647 | * For SD cards, get the Relatvie Address. |
| 648 | * This also puts the cards into Standby State |
| 649 | */ |
| 650 | cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; |
| 651 | cmd.cmdarg = mmc->rca << 16; |
| 652 | cmd.resp_type = MMC_RSP_R6; |
| 653 | cmd.flags = 0; |
| 654 | |
| 655 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 656 | |
| 657 | if (err) |
| 658 | return err; |
| 659 | |
| 660 | if (IS_SD(mmc)) |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 661 | mmc->rca = (cmd.response[0] >> 16) & 0xffff; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 662 | |
| 663 | /* Get the Card-Specific Data */ |
| 664 | cmd.cmdidx = MMC_CMD_SEND_CSD; |
| 665 | cmd.resp_type = MMC_RSP_R2; |
| 666 | cmd.cmdarg = mmc->rca << 16; |
| 667 | cmd.flags = 0; |
| 668 | |
| 669 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 670 | |
| 671 | if (err) |
| 672 | return err; |
| 673 | |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 674 | mmc->csd[0] = cmd.response[0]; |
| 675 | mmc->csd[1] = cmd.response[1]; |
| 676 | mmc->csd[2] = cmd.response[2]; |
| 677 | mmc->csd[3] = cmd.response[3]; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 678 | |
| 679 | if (mmc->version == MMC_VERSION_UNKNOWN) { |
Rabin Vincent | 0b453ff | 2009-04-05 13:30:55 +0530 | [diff] [blame] | 680 | int version = (cmd.response[0] >> 26) & 0xf; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 681 | |
| 682 | switch (version) { |
| 683 | case 0: |
| 684 | mmc->version = MMC_VERSION_1_2; |
| 685 | break; |
| 686 | case 1: |
| 687 | mmc->version = MMC_VERSION_1_4; |
| 688 | break; |
| 689 | case 2: |
| 690 | mmc->version = MMC_VERSION_2_2; |
| 691 | break; |
| 692 | case 3: |
| 693 | mmc->version = MMC_VERSION_3; |
| 694 | break; |
| 695 | case 4: |
| 696 | mmc->version = MMC_VERSION_4; |
| 697 | break; |
| 698 | default: |
| 699 | mmc->version = MMC_VERSION_1_2; |
| 700 | break; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /* divide frequency by 10, since the mults are 10x bigger */ |
Rabin Vincent | 0b453ff | 2009-04-05 13:30:55 +0530 | [diff] [blame] | 705 | freq = fbase[(cmd.response[0] & 0x7)]; |
| 706 | mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 707 | |
| 708 | mmc->tran_speed = freq * mult; |
| 709 | |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 710 | mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 711 | |
| 712 | if (IS_SD(mmc)) |
| 713 | mmc->write_bl_len = mmc->read_bl_len; |
| 714 | else |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 715 | mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 716 | |
| 717 | if (mmc->high_capacity) { |
| 718 | csize = (mmc->csd[1] & 0x3f) << 16 |
| 719 | | (mmc->csd[2] & 0xffff0000) >> 16; |
| 720 | cmult = 8; |
| 721 | } else { |
| 722 | csize = (mmc->csd[1] & 0x3ff) << 2 |
| 723 | | (mmc->csd[2] & 0xc0000000) >> 30; |
| 724 | cmult = (mmc->csd[2] & 0x00038000) >> 15; |
| 725 | } |
| 726 | |
| 727 | mmc->capacity = (csize + 1) << (cmult + 2); |
| 728 | mmc->capacity *= mmc->read_bl_len; |
| 729 | |
| 730 | if (mmc->read_bl_len > 512) |
| 731 | mmc->read_bl_len = 512; |
| 732 | |
| 733 | if (mmc->write_bl_len > 512) |
| 734 | mmc->write_bl_len = 512; |
| 735 | |
| 736 | /* Select the card, and put it into Transfer Mode */ |
| 737 | cmd.cmdidx = MMC_CMD_SELECT_CARD; |
| 738 | cmd.resp_type = MMC_RSP_R1b; |
| 739 | cmd.cmdarg = mmc->rca << 16; |
| 740 | cmd.flags = 0; |
| 741 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 742 | |
| 743 | if (err) |
| 744 | return err; |
| 745 | |
Sukumar Ghorai | d23e2c0 | 2010-09-20 18:29:29 +0530 | [diff] [blame] | 746 | if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) { |
| 747 | /* check ext_csd version and capacity */ |
| 748 | err = mmc_send_ext_csd(mmc, ext_csd); |
| 749 | if (!err & (ext_csd[192] >= 2)) { |
| 750 | mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 | |
| 751 | ext_csd[214] << 16 | ext_csd[215] << 24; |
| 752 | mmc->capacity *= 512; |
| 753 | } |
| 754 | } |
| 755 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 756 | if (IS_SD(mmc)) |
| 757 | err = sd_change_freq(mmc); |
| 758 | else |
| 759 | err = mmc_change_freq(mmc); |
| 760 | |
| 761 | if (err) |
| 762 | return err; |
| 763 | |
| 764 | /* Restrict card's capabilities by what the host can do */ |
| 765 | mmc->card_caps &= mmc->host_caps; |
| 766 | |
| 767 | if (IS_SD(mmc)) { |
| 768 | if (mmc->card_caps & MMC_MODE_4BIT) { |
| 769 | cmd.cmdidx = MMC_CMD_APP_CMD; |
| 770 | cmd.resp_type = MMC_RSP_R1; |
| 771 | cmd.cmdarg = mmc->rca << 16; |
| 772 | cmd.flags = 0; |
| 773 | |
| 774 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 775 | if (err) |
| 776 | return err; |
| 777 | |
| 778 | cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; |
| 779 | cmd.resp_type = MMC_RSP_R1; |
| 780 | cmd.cmdarg = 2; |
| 781 | cmd.flags = 0; |
| 782 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 783 | if (err) |
| 784 | return err; |
| 785 | |
| 786 | mmc_set_bus_width(mmc, 4); |
| 787 | } |
| 788 | |
| 789 | if (mmc->card_caps & MMC_MODE_HS) |
| 790 | mmc_set_clock(mmc, 50000000); |
| 791 | else |
| 792 | mmc_set_clock(mmc, 25000000); |
| 793 | } else { |
| 794 | if (mmc->card_caps & MMC_MODE_4BIT) { |
| 795 | /* Set the card to use 4 bit*/ |
| 796 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 797 | EXT_CSD_BUS_WIDTH, |
| 798 | EXT_CSD_BUS_WIDTH_4); |
| 799 | |
| 800 | if (err) |
| 801 | return err; |
| 802 | |
| 803 | mmc_set_bus_width(mmc, 4); |
| 804 | } else if (mmc->card_caps & MMC_MODE_8BIT) { |
| 805 | /* Set the card to use 8 bit*/ |
| 806 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, |
| 807 | EXT_CSD_BUS_WIDTH, |
| 808 | EXT_CSD_BUS_WIDTH_8); |
| 809 | |
| 810 | if (err) |
| 811 | return err; |
| 812 | |
| 813 | mmc_set_bus_width(mmc, 8); |
| 814 | } |
| 815 | |
| 816 | if (mmc->card_caps & MMC_MODE_HS) { |
| 817 | if (mmc->card_caps & MMC_MODE_HS_52MHz) |
| 818 | mmc_set_clock(mmc, 52000000); |
| 819 | else |
| 820 | mmc_set_clock(mmc, 26000000); |
| 821 | } else |
| 822 | mmc_set_clock(mmc, 20000000); |
| 823 | } |
| 824 | |
| 825 | /* fill in device description */ |
| 826 | mmc->block_dev.lun = 0; |
| 827 | mmc->block_dev.type = 0; |
| 828 | mmc->block_dev.blksz = mmc->read_bl_len; |
Rabin Vincent | 9b1f942 | 2009-04-05 13:30:54 +0530 | [diff] [blame] | 829 | mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); |
Rabin Vincent | 0b453ff | 2009-04-05 13:30:55 +0530 | [diff] [blame] | 830 | sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8, |
| 831 | (mmc->cid[2] << 8) | (mmc->cid[3] >> 24)); |
| 832 | sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff, |
| 833 | (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, |
| 834 | (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); |
| 835 | sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28, |
| 836 | (mmc->cid[2] >> 24) & 0xf); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 837 | init_part(&mmc->block_dev); |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | int mmc_send_if_cond(struct mmc *mmc) |
| 843 | { |
| 844 | struct mmc_cmd cmd; |
| 845 | int err; |
| 846 | |
| 847 | cmd.cmdidx = SD_CMD_SEND_IF_COND; |
| 848 | /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ |
| 849 | cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa; |
| 850 | cmd.resp_type = MMC_RSP_R7; |
| 851 | cmd.flags = 0; |
| 852 | |
| 853 | err = mmc_send_cmd(mmc, &cmd, NULL); |
| 854 | |
| 855 | if (err) |
| 856 | return err; |
| 857 | |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 858 | if ((cmd.response[0] & 0xff) != 0xaa) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 859 | return UNUSABLE_ERR; |
| 860 | else |
| 861 | mmc->version = SD_VERSION_2; |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | int mmc_register(struct mmc *mmc) |
| 867 | { |
| 868 | /* Setup the universal parts of the block interface just once */ |
| 869 | mmc->block_dev.if_type = IF_TYPE_MMC; |
| 870 | mmc->block_dev.dev = cur_dev_num++; |
| 871 | mmc->block_dev.removable = 1; |
| 872 | mmc->block_dev.block_read = mmc_bread; |
| 873 | mmc->block_dev.block_write = mmc_bwrite; |
| 874 | |
| 875 | INIT_LIST_HEAD (&mmc->link); |
| 876 | |
| 877 | list_add_tail (&mmc->link, &mmc_devices); |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | block_dev_desc_t *mmc_get_dev(int dev) |
| 883 | { |
| 884 | struct mmc *mmc = find_mmc_device(dev); |
| 885 | |
Rabin Vincent | e85649c | 2009-04-05 13:30:53 +0530 | [diff] [blame] | 886 | return mmc ? &mmc->block_dev : NULL; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | int mmc_init(struct mmc *mmc) |
| 890 | { |
| 891 | int err; |
| 892 | |
| 893 | err = mmc->init(mmc); |
| 894 | |
| 895 | if (err) |
| 896 | return err; |
| 897 | |
Ilya Yanok | b86b85e | 2009-06-29 17:53:16 +0400 | [diff] [blame] | 898 | mmc_set_bus_width(mmc, 1); |
| 899 | mmc_set_clock(mmc, 1); |
| 900 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 901 | /* Reset the Card */ |
| 902 | err = mmc_go_idle(mmc); |
| 903 | |
| 904 | if (err) |
| 905 | return err; |
| 906 | |
| 907 | /* Test for SD version 2 */ |
| 908 | err = mmc_send_if_cond(mmc); |
| 909 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 910 | /* Now try to get the SD card's operating condition */ |
| 911 | err = sd_send_op_cond(mmc); |
| 912 | |
| 913 | /* If the command timed out, we check for an MMC card */ |
| 914 | if (err == TIMEOUT) { |
| 915 | err = mmc_send_op_cond(mmc); |
| 916 | |
| 917 | if (err) { |
| 918 | printf("Card did not respond to voltage select!\n"); |
| 919 | return UNUSABLE_ERR; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | return mmc_startup(mmc); |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * CPU and board-specific MMC initializations. Aliased function |
| 928 | * signals caller to move on |
| 929 | */ |
| 930 | static int __def_mmc_init(bd_t *bis) |
| 931 | { |
| 932 | return -1; |
| 933 | } |
| 934 | |
Peter Tyser | f9a109b | 2009-04-20 11:08:46 -0500 | [diff] [blame] | 935 | int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); |
| 936 | 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] | 937 | |
| 938 | void print_mmc_devices(char separator) |
| 939 | { |
| 940 | struct mmc *m; |
| 941 | struct list_head *entry; |
| 942 | |
| 943 | list_for_each(entry, &mmc_devices) { |
| 944 | m = list_entry(entry, struct mmc, link); |
| 945 | |
| 946 | printf("%s: %d", m->name, m->block_dev.dev); |
| 947 | |
| 948 | if (entry->next != &mmc_devices) |
| 949 | printf("%c ", separator); |
| 950 | } |
| 951 | |
| 952 | printf("\n"); |
| 953 | } |
| 954 | |
| 955 | int mmc_initialize(bd_t *bis) |
| 956 | { |
| 957 | INIT_LIST_HEAD (&mmc_devices); |
| 958 | cur_dev_num = 0; |
| 959 | |
| 960 | if (board_mmc_init(bis) < 0) |
| 961 | cpu_mmc_init(bis); |
| 962 | |
| 963 | print_mmc_devices(','); |
| 964 | |
| 965 | return 0; |
| 966 | } |