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