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