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