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