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