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