1272cc70bSAndy Fleming /* 2272cc70bSAndy Fleming * Copyright 2008, Freescale Semiconductor, Inc 3272cc70bSAndy Fleming * Andy Fleming 4272cc70bSAndy Fleming * 5272cc70bSAndy Fleming * Based vaguely on the Linux code 6272cc70bSAndy Fleming * 7272cc70bSAndy Fleming * See file CREDITS for list of people who contributed to this 8272cc70bSAndy Fleming * project. 9272cc70bSAndy Fleming * 10272cc70bSAndy Fleming * This program is free software; you can redistribute it and/or 11272cc70bSAndy Fleming * modify it under the terms of the GNU General Public License as 12272cc70bSAndy Fleming * published by the Free Software Foundation; either version 2 of 13272cc70bSAndy Fleming * the License, or (at your option) any later version. 14272cc70bSAndy Fleming * 15272cc70bSAndy Fleming * This program is distributed in the hope that it will be useful, 16272cc70bSAndy Fleming * but WITHOUT ANY WARRANTY; without even the implied warranty of 17272cc70bSAndy Fleming * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18272cc70bSAndy Fleming * GNU General Public License for more details. 19272cc70bSAndy Fleming * 20272cc70bSAndy Fleming * You should have received a copy of the GNU General Public License 21272cc70bSAndy Fleming * along with this program; if not, write to the Free Software 22272cc70bSAndy Fleming * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 23272cc70bSAndy Fleming * MA 02111-1307 USA 24272cc70bSAndy Fleming */ 25272cc70bSAndy Fleming 26272cc70bSAndy Fleming #include <config.h> 27272cc70bSAndy Fleming #include <common.h> 28272cc70bSAndy Fleming #include <command.h> 29272cc70bSAndy Fleming #include <mmc.h> 30272cc70bSAndy Fleming #include <part.h> 31272cc70bSAndy Fleming #include <malloc.h> 32272cc70bSAndy Fleming #include <linux/list.h> 339b1f942cSRabin Vincent #include <div64.h> 34272cc70bSAndy Fleming 35ce0fbcd2SMatt Waddel /* Set block count limit because of 16 bit register limit on some hardware*/ 36ce0fbcd2SMatt Waddel #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT 37ce0fbcd2SMatt Waddel #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535 38ce0fbcd2SMatt Waddel #endif 39ce0fbcd2SMatt Waddel 40272cc70bSAndy Fleming static struct list_head mmc_devices; 41272cc70bSAndy Fleming static int cur_dev_num = -1; 42272cc70bSAndy Fleming 4311fdade2SStefano Babic int __board_mmc_getcd(u8 *cd, struct mmc *mmc) { 4411fdade2SStefano Babic return -1; 4511fdade2SStefano Babic } 4611fdade2SStefano Babic 4711fdade2SStefano Babic int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak, 4811fdade2SStefano Babic alias("__board_mmc_getcd"))); 4911fdade2SStefano Babic 50272cc70bSAndy Fleming int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) 51272cc70bSAndy Fleming { 525db2fe3aSRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 535db2fe3aSRaffaele Recalcati int ret; 545db2fe3aSRaffaele Recalcati int i; 555db2fe3aSRaffaele Recalcati u8 *ptr; 565db2fe3aSRaffaele Recalcati 575db2fe3aSRaffaele Recalcati printf("CMD_SEND:%d\n", cmd->cmdidx); 585db2fe3aSRaffaele Recalcati printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg); 595db2fe3aSRaffaele Recalcati printf("\t\tFLAG\t\t\t %d\n", cmd->flags); 605db2fe3aSRaffaele Recalcati ret = mmc->send_cmd(mmc, cmd, data); 615db2fe3aSRaffaele Recalcati switch (cmd->resp_type) { 625db2fe3aSRaffaele Recalcati case MMC_RSP_NONE: 635db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_NONE\n"); 645db2fe3aSRaffaele Recalcati break; 655db2fe3aSRaffaele Recalcati case MMC_RSP_R1: 665db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n", 675db2fe3aSRaffaele Recalcati cmd->response[0]); 685db2fe3aSRaffaele Recalcati break; 695db2fe3aSRaffaele Recalcati case MMC_RSP_R1b: 705db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n", 715db2fe3aSRaffaele Recalcati cmd->response[0]); 725db2fe3aSRaffaele Recalcati break; 735db2fe3aSRaffaele Recalcati case MMC_RSP_R2: 745db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R2\t\t 0x%08X \n", 755db2fe3aSRaffaele Recalcati cmd->response[0]); 765db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 775db2fe3aSRaffaele Recalcati cmd->response[1]); 785db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 795db2fe3aSRaffaele Recalcati cmd->response[2]); 805db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 815db2fe3aSRaffaele Recalcati cmd->response[3]); 825db2fe3aSRaffaele Recalcati printf("\n"); 835db2fe3aSRaffaele Recalcati printf("\t\t\t\t\tDUMPING DATA\n"); 845db2fe3aSRaffaele Recalcati for (i = 0; i < 4; i++) { 855db2fe3aSRaffaele Recalcati int j; 865db2fe3aSRaffaele Recalcati printf("\t\t\t\t\t%03d - ", i*4); 875db2fe3aSRaffaele Recalcati ptr = &cmd->response[i]; 885db2fe3aSRaffaele Recalcati ptr += 3; 895db2fe3aSRaffaele Recalcati for (j = 0; j < 4; j++) 905db2fe3aSRaffaele Recalcati printf("%02X ", *ptr--); 915db2fe3aSRaffaele Recalcati printf("\n"); 925db2fe3aSRaffaele Recalcati } 935db2fe3aSRaffaele Recalcati break; 945db2fe3aSRaffaele Recalcati case MMC_RSP_R3: 955db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n", 965db2fe3aSRaffaele Recalcati cmd->response[0]); 975db2fe3aSRaffaele Recalcati break; 985db2fe3aSRaffaele Recalcati default: 995db2fe3aSRaffaele Recalcati printf("\t\tERROR MMC rsp not supported\n"); 1005db2fe3aSRaffaele Recalcati break; 1015db2fe3aSRaffaele Recalcati } 1025db2fe3aSRaffaele Recalcati return ret; 1035db2fe3aSRaffaele Recalcati #else 104272cc70bSAndy Fleming return mmc->send_cmd(mmc, cmd, data); 1055db2fe3aSRaffaele Recalcati #endif 106272cc70bSAndy Fleming } 107272cc70bSAndy Fleming 1085d4fc8d9SRaffaele Recalcati int mmc_send_status(struct mmc *mmc, int timeout) 1095d4fc8d9SRaffaele Recalcati { 1105d4fc8d9SRaffaele Recalcati struct mmc_cmd cmd; 1115d4fc8d9SRaffaele Recalcati int err; 1125d4fc8d9SRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 1135d4fc8d9SRaffaele Recalcati int status; 1145d4fc8d9SRaffaele Recalcati #endif 1155d4fc8d9SRaffaele Recalcati 1165d4fc8d9SRaffaele Recalcati cmd.cmdidx = MMC_CMD_SEND_STATUS; 1175d4fc8d9SRaffaele Recalcati cmd.resp_type = MMC_RSP_R1; 1185d4fc8d9SRaffaele Recalcati cmd.cmdarg = 0; 1195d4fc8d9SRaffaele Recalcati cmd.flags = 0; 1205d4fc8d9SRaffaele Recalcati 1215d4fc8d9SRaffaele Recalcati do { 1225d4fc8d9SRaffaele Recalcati err = mmc_send_cmd(mmc, &cmd, NULL); 1235d4fc8d9SRaffaele Recalcati if (err) 1245d4fc8d9SRaffaele Recalcati return err; 1255d4fc8d9SRaffaele Recalcati else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) 1265d4fc8d9SRaffaele Recalcati break; 1275d4fc8d9SRaffaele Recalcati 1285d4fc8d9SRaffaele Recalcati udelay(1000); 1295d4fc8d9SRaffaele Recalcati 1305d4fc8d9SRaffaele Recalcati if (cmd.response[0] & MMC_STATUS_MASK) { 1315d4fc8d9SRaffaele Recalcati printf("Status Error: 0x%08X\n", cmd.response[0]); 1325d4fc8d9SRaffaele Recalcati return COMM_ERR; 1335d4fc8d9SRaffaele Recalcati } 1345d4fc8d9SRaffaele Recalcati } while (timeout--); 1355d4fc8d9SRaffaele Recalcati 1365db2fe3aSRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 1375db2fe3aSRaffaele Recalcati status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9; 1385db2fe3aSRaffaele Recalcati printf("CURR STATE:%d\n", status); 1395db2fe3aSRaffaele Recalcati #endif 1405d4fc8d9SRaffaele Recalcati if (!timeout) { 1415d4fc8d9SRaffaele Recalcati printf("Timeout waiting card ready\n"); 1425d4fc8d9SRaffaele Recalcati return TIMEOUT; 1435d4fc8d9SRaffaele Recalcati } 1445d4fc8d9SRaffaele Recalcati 1455d4fc8d9SRaffaele Recalcati return 0; 1465d4fc8d9SRaffaele Recalcati } 1475d4fc8d9SRaffaele Recalcati 148272cc70bSAndy Fleming int mmc_set_blocklen(struct mmc *mmc, int len) 149272cc70bSAndy Fleming { 150272cc70bSAndy Fleming struct mmc_cmd cmd; 151272cc70bSAndy Fleming 152272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; 153272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 154272cc70bSAndy Fleming cmd.cmdarg = len; 155272cc70bSAndy Fleming cmd.flags = 0; 156272cc70bSAndy Fleming 157272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, NULL); 158272cc70bSAndy Fleming } 159272cc70bSAndy Fleming 160272cc70bSAndy Fleming struct mmc *find_mmc_device(int dev_num) 161272cc70bSAndy Fleming { 162272cc70bSAndy Fleming struct mmc *m; 163272cc70bSAndy Fleming struct list_head *entry; 164272cc70bSAndy Fleming 165272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 166272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 167272cc70bSAndy Fleming 168272cc70bSAndy Fleming if (m->block_dev.dev == dev_num) 169272cc70bSAndy Fleming return m; 170272cc70bSAndy Fleming } 171272cc70bSAndy Fleming 172272cc70bSAndy Fleming printf("MMC Device %d not found\n", dev_num); 173272cc70bSAndy Fleming 174272cc70bSAndy Fleming return NULL; 175272cc70bSAndy Fleming } 176272cc70bSAndy Fleming 177272cc70bSAndy Fleming static ulong 1780158126eSLei Wen mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src) 179272cc70bSAndy Fleming { 180272cc70bSAndy Fleming struct mmc_cmd cmd; 181272cc70bSAndy Fleming struct mmc_data data; 1825d4fc8d9SRaffaele Recalcati int timeout = 1000; 183272cc70bSAndy Fleming 184d2bf29e3SLei Wen if ((start + blkcnt) > mmc->block_dev.lba) { 185def412b6SSteve Sakoman printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", 186d2bf29e3SLei Wen start + blkcnt, mmc->block_dev.lba); 187d2bf29e3SLei Wen return 0; 188d2bf29e3SLei Wen } 189272cc70bSAndy Fleming 190272cc70bSAndy Fleming if (blkcnt > 1) 191272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; 192272cc70bSAndy Fleming else 193272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; 194272cc70bSAndy Fleming 195272cc70bSAndy Fleming if (mmc->high_capacity) 196272cc70bSAndy Fleming cmd.cmdarg = start; 197272cc70bSAndy Fleming else 198def412b6SSteve Sakoman cmd.cmdarg = start * mmc->write_bl_len; 199272cc70bSAndy Fleming 200272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 201272cc70bSAndy Fleming cmd.flags = 0; 202272cc70bSAndy Fleming 203272cc70bSAndy Fleming data.src = src; 204272cc70bSAndy Fleming data.blocks = blkcnt; 205def412b6SSteve Sakoman data.blocksize = mmc->write_bl_len; 206272cc70bSAndy Fleming data.flags = MMC_DATA_WRITE; 207272cc70bSAndy Fleming 208def412b6SSteve Sakoman if (mmc_send_cmd(mmc, &cmd, &data)) { 209def412b6SSteve Sakoman printf("mmc write failed\n"); 210def412b6SSteve Sakoman return 0; 211272cc70bSAndy Fleming } 212272cc70bSAndy Fleming 213d52ebf10SThomas Chou /* SPI multiblock writes terminate using a special 214d52ebf10SThomas Chou * token, not a STOP_TRANSMISSION request. 215d52ebf10SThomas Chou */ 216d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc) && blkcnt > 1) { 217272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 218272cc70bSAndy Fleming cmd.cmdarg = 0; 219272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 220272cc70bSAndy Fleming cmd.flags = 0; 221def412b6SSteve Sakoman if (mmc_send_cmd(mmc, &cmd, NULL)) { 222def412b6SSteve Sakoman printf("mmc fail to send stop cmd\n"); 223def412b6SSteve Sakoman return 0; 224272cc70bSAndy Fleming } 2255d4fc8d9SRaffaele Recalcati 2265d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 2275d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 2280158126eSLei Wen } 2290158126eSLei Wen 2300158126eSLei Wen return blkcnt; 2310158126eSLei Wen } 2320158126eSLei Wen 2330158126eSLei Wen static ulong 2340158126eSLei Wen mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src) 2350158126eSLei Wen { 2360158126eSLei Wen lbaint_t cur, blocks_todo = blkcnt; 2370158126eSLei Wen 238def412b6SSteve Sakoman struct mmc *mmc = find_mmc_device(dev_num); 2390158126eSLei Wen if (!mmc) 240def412b6SSteve Sakoman return 0; 2410158126eSLei Wen 242def412b6SSteve Sakoman if (mmc_set_blocklen(mmc, mmc->write_bl_len)) 243def412b6SSteve Sakoman return 0; 2440158126eSLei Wen 2450158126eSLei Wen do { 2468feafcc4SJohn Rigby cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo; 2470158126eSLei Wen if(mmc_write_blocks(mmc, start, cur, src) != cur) 248def412b6SSteve Sakoman return 0; 2490158126eSLei Wen blocks_todo -= cur; 2500158126eSLei Wen start += cur; 2510158126eSLei Wen src += cur * mmc->write_bl_len; 2520158126eSLei Wen } while (blocks_todo > 0); 253272cc70bSAndy Fleming 254272cc70bSAndy Fleming return blkcnt; 255272cc70bSAndy Fleming } 256272cc70bSAndy Fleming 2574a1a06bcSAlagu Sankar int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt) 258272cc70bSAndy Fleming { 259272cc70bSAndy Fleming struct mmc_cmd cmd; 260272cc70bSAndy Fleming struct mmc_data data; 2615d4fc8d9SRaffaele Recalcati int timeout = 1000; 262272cc70bSAndy Fleming 2634a1a06bcSAlagu Sankar if (blkcnt > 1) 2644a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; 2654a1a06bcSAlagu Sankar else 266272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; 267272cc70bSAndy Fleming 268272cc70bSAndy Fleming if (mmc->high_capacity) 2694a1a06bcSAlagu Sankar cmd.cmdarg = start; 270272cc70bSAndy Fleming else 2714a1a06bcSAlagu Sankar cmd.cmdarg = start * mmc->read_bl_len; 272272cc70bSAndy Fleming 273272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 274272cc70bSAndy Fleming cmd.flags = 0; 275272cc70bSAndy Fleming 276272cc70bSAndy Fleming data.dest = dst; 2774a1a06bcSAlagu Sankar data.blocks = blkcnt; 278272cc70bSAndy Fleming data.blocksize = mmc->read_bl_len; 279272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 280272cc70bSAndy Fleming 2814a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, &data)) 2824a1a06bcSAlagu Sankar return 0; 2834a1a06bcSAlagu Sankar 2844a1a06bcSAlagu Sankar if (blkcnt > 1) { 2854a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 2864a1a06bcSAlagu Sankar cmd.cmdarg = 0; 2874a1a06bcSAlagu Sankar cmd.resp_type = MMC_RSP_R1b; 2884a1a06bcSAlagu Sankar cmd.flags = 0; 2894a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, NULL)) { 2904a1a06bcSAlagu Sankar printf("mmc fail to send stop cmd\n"); 2914a1a06bcSAlagu Sankar return 0; 2924a1a06bcSAlagu Sankar } 2935d4fc8d9SRaffaele Recalcati 2945d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 2955d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 296272cc70bSAndy Fleming } 297272cc70bSAndy Fleming 2984a1a06bcSAlagu Sankar return blkcnt; 299272cc70bSAndy Fleming } 300272cc70bSAndy Fleming 301272cc70bSAndy Fleming static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst) 302272cc70bSAndy Fleming { 3034a1a06bcSAlagu Sankar lbaint_t cur, blocks_todo = blkcnt; 304272cc70bSAndy Fleming 3054a1a06bcSAlagu Sankar if (blkcnt == 0) 3064a1a06bcSAlagu Sankar return 0; 3074a1a06bcSAlagu Sankar 3084a1a06bcSAlagu Sankar struct mmc *mmc = find_mmc_device(dev_num); 309272cc70bSAndy Fleming if (!mmc) 310272cc70bSAndy Fleming return 0; 311272cc70bSAndy Fleming 312d2bf29e3SLei Wen if ((start + blkcnt) > mmc->block_dev.lba) { 3134a1a06bcSAlagu Sankar printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", 314d2bf29e3SLei Wen start + blkcnt, mmc->block_dev.lba); 315d2bf29e3SLei Wen return 0; 316d2bf29e3SLei Wen } 317272cc70bSAndy Fleming 3184a1a06bcSAlagu Sankar if (mmc_set_blocklen(mmc, mmc->read_bl_len)) 319272cc70bSAndy Fleming return 0; 320272cc70bSAndy Fleming 3214a1a06bcSAlagu Sankar do { 3228feafcc4SJohn Rigby cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo; 3234a1a06bcSAlagu Sankar if(mmc_read_blocks(mmc, dst, start, cur) != cur) 3244a1a06bcSAlagu Sankar return 0; 3254a1a06bcSAlagu Sankar blocks_todo -= cur; 3264a1a06bcSAlagu Sankar start += cur; 3274a1a06bcSAlagu Sankar dst += cur * mmc->read_bl_len; 3284a1a06bcSAlagu Sankar } while (blocks_todo > 0); 329272cc70bSAndy Fleming 330272cc70bSAndy Fleming return blkcnt; 331272cc70bSAndy Fleming } 332272cc70bSAndy Fleming 333272cc70bSAndy Fleming int mmc_go_idle(struct mmc* mmc) 334272cc70bSAndy Fleming { 335272cc70bSAndy Fleming struct mmc_cmd cmd; 336272cc70bSAndy Fleming int err; 337272cc70bSAndy Fleming 338272cc70bSAndy Fleming udelay(1000); 339272cc70bSAndy Fleming 340272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; 341272cc70bSAndy Fleming cmd.cmdarg = 0; 342272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_NONE; 343272cc70bSAndy Fleming cmd.flags = 0; 344272cc70bSAndy Fleming 345272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 346272cc70bSAndy Fleming 347272cc70bSAndy Fleming if (err) 348272cc70bSAndy Fleming return err; 349272cc70bSAndy Fleming 350272cc70bSAndy Fleming udelay(2000); 351272cc70bSAndy Fleming 352272cc70bSAndy Fleming return 0; 353272cc70bSAndy Fleming } 354272cc70bSAndy Fleming 355272cc70bSAndy Fleming int 356272cc70bSAndy Fleming sd_send_op_cond(struct mmc *mmc) 357272cc70bSAndy Fleming { 358272cc70bSAndy Fleming int timeout = 1000; 359272cc70bSAndy Fleming int err; 360272cc70bSAndy Fleming struct mmc_cmd cmd; 361272cc70bSAndy Fleming 362272cc70bSAndy Fleming do { 363272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 364272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 365272cc70bSAndy Fleming cmd.cmdarg = 0; 366272cc70bSAndy Fleming cmd.flags = 0; 367272cc70bSAndy Fleming 368272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 369272cc70bSAndy Fleming 370272cc70bSAndy Fleming if (err) 371272cc70bSAndy Fleming return err; 372272cc70bSAndy Fleming 373272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; 374272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R3; 375250de12bSStefano Babic 376250de12bSStefano Babic /* 377250de12bSStefano Babic * Most cards do not answer if some reserved bits 378250de12bSStefano Babic * in the ocr are set. However, Some controller 379250de12bSStefano Babic * can set bit 7 (reserved for low voltages), but 380250de12bSStefano Babic * how to manage low voltages SD card is not yet 381250de12bSStefano Babic * specified. 382250de12bSStefano Babic */ 383d52ebf10SThomas Chou cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 : 384d52ebf10SThomas Chou (mmc->voltages & 0xff8000); 385272cc70bSAndy Fleming 386272cc70bSAndy Fleming if (mmc->version == SD_VERSION_2) 387272cc70bSAndy Fleming cmd.cmdarg |= OCR_HCS; 388272cc70bSAndy Fleming 389272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 390272cc70bSAndy Fleming 391272cc70bSAndy Fleming if (err) 392272cc70bSAndy Fleming return err; 393272cc70bSAndy Fleming 394272cc70bSAndy Fleming udelay(1000); 395272cc70bSAndy Fleming } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--); 396272cc70bSAndy Fleming 397272cc70bSAndy Fleming if (timeout <= 0) 398272cc70bSAndy Fleming return UNUSABLE_ERR; 399272cc70bSAndy Fleming 400272cc70bSAndy Fleming if (mmc->version != SD_VERSION_2) 401272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 402272cc70bSAndy Fleming 403d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 404d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 405d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 406d52ebf10SThomas Chou cmd.cmdarg = 0; 407d52ebf10SThomas Chou cmd.flags = 0; 408d52ebf10SThomas Chou 409d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 410d52ebf10SThomas Chou 411d52ebf10SThomas Chou if (err) 412d52ebf10SThomas Chou return err; 413d52ebf10SThomas Chou } 414d52ebf10SThomas Chou 415998be3ddSRabin Vincent mmc->ocr = cmd.response[0]; 416272cc70bSAndy Fleming 417272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 418272cc70bSAndy Fleming mmc->rca = 0; 419272cc70bSAndy Fleming 420272cc70bSAndy Fleming return 0; 421272cc70bSAndy Fleming } 422272cc70bSAndy Fleming 423272cc70bSAndy Fleming int mmc_send_op_cond(struct mmc *mmc) 424272cc70bSAndy Fleming { 42531cacbabSRaffaele Recalcati int timeout = 10000; 426272cc70bSAndy Fleming struct mmc_cmd cmd; 427272cc70bSAndy Fleming int err; 428272cc70bSAndy Fleming 429272cc70bSAndy Fleming /* Some cards seem to need this */ 430272cc70bSAndy Fleming mmc_go_idle(mmc); 431272cc70bSAndy Fleming 43231cacbabSRaffaele Recalcati /* Asking to the card its capabilities */ 43331cacbabSRaffaele Recalcati cmd.cmdidx = MMC_CMD_SEND_OP_COND; 43431cacbabSRaffaele Recalcati cmd.resp_type = MMC_RSP_R3; 43531cacbabSRaffaele Recalcati cmd.cmdarg = 0; 43631cacbabSRaffaele Recalcati cmd.flags = 0; 43731cacbabSRaffaele Recalcati 43831cacbabSRaffaele Recalcati err = mmc_send_cmd(mmc, &cmd, NULL); 43931cacbabSRaffaele Recalcati 44031cacbabSRaffaele Recalcati if (err) 44131cacbabSRaffaele Recalcati return err; 44231cacbabSRaffaele Recalcati 44331cacbabSRaffaele Recalcati udelay(1000); 44431cacbabSRaffaele Recalcati 445272cc70bSAndy Fleming do { 446272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_OP_COND; 447272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R3; 44831cacbabSRaffaele Recalcati cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 : 44931cacbabSRaffaele Recalcati (mmc->voltages & 45031cacbabSRaffaele Recalcati (cmd.response[0] & OCR_VOLTAGE_MASK)) | 45131cacbabSRaffaele Recalcati (cmd.response[0] & OCR_ACCESS_MODE)); 452272cc70bSAndy Fleming cmd.flags = 0; 453272cc70bSAndy Fleming 454272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 455272cc70bSAndy Fleming 456272cc70bSAndy Fleming if (err) 457272cc70bSAndy Fleming return err; 458272cc70bSAndy Fleming 459272cc70bSAndy Fleming udelay(1000); 460272cc70bSAndy Fleming } while (!(cmd.response[0] & OCR_BUSY) && timeout--); 461272cc70bSAndy Fleming 462272cc70bSAndy Fleming if (timeout <= 0) 463272cc70bSAndy Fleming return UNUSABLE_ERR; 464272cc70bSAndy Fleming 465d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 466d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 467d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 468d52ebf10SThomas Chou cmd.cmdarg = 0; 469d52ebf10SThomas Chou cmd.flags = 0; 470d52ebf10SThomas Chou 471d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 472d52ebf10SThomas Chou 473d52ebf10SThomas Chou if (err) 474d52ebf10SThomas Chou return err; 475d52ebf10SThomas Chou } 476d52ebf10SThomas Chou 477272cc70bSAndy Fleming mmc->version = MMC_VERSION_UNKNOWN; 478998be3ddSRabin Vincent mmc->ocr = cmd.response[0]; 479272cc70bSAndy Fleming 480272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 481272cc70bSAndy Fleming mmc->rca = 0; 482272cc70bSAndy Fleming 483272cc70bSAndy Fleming return 0; 484272cc70bSAndy Fleming } 485272cc70bSAndy Fleming 486272cc70bSAndy Fleming 487272cc70bSAndy Fleming int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd) 488272cc70bSAndy Fleming { 489272cc70bSAndy Fleming struct mmc_cmd cmd; 490272cc70bSAndy Fleming struct mmc_data data; 491272cc70bSAndy Fleming int err; 492272cc70bSAndy Fleming 493272cc70bSAndy Fleming /* Get the Card Status Register */ 494272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; 495272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 496272cc70bSAndy Fleming cmd.cmdarg = 0; 497272cc70bSAndy Fleming cmd.flags = 0; 498272cc70bSAndy Fleming 499272cc70bSAndy Fleming data.dest = ext_csd; 500272cc70bSAndy Fleming data.blocks = 1; 501272cc70bSAndy Fleming data.blocksize = 512; 502272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 503272cc70bSAndy Fleming 504272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 505272cc70bSAndy Fleming 506272cc70bSAndy Fleming return err; 507272cc70bSAndy Fleming } 508272cc70bSAndy Fleming 509272cc70bSAndy Fleming 510272cc70bSAndy Fleming int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) 511272cc70bSAndy Fleming { 512272cc70bSAndy Fleming struct mmc_cmd cmd; 5135d4fc8d9SRaffaele Recalcati int timeout = 1000; 5145d4fc8d9SRaffaele Recalcati int ret; 515272cc70bSAndy Fleming 516272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SWITCH; 517272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 518272cc70bSAndy Fleming cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 519272cc70bSAndy Fleming (index << 16) | 520272cc70bSAndy Fleming (value << 8); 521272cc70bSAndy Fleming cmd.flags = 0; 522272cc70bSAndy Fleming 5235d4fc8d9SRaffaele Recalcati ret = mmc_send_cmd(mmc, &cmd, NULL); 5245d4fc8d9SRaffaele Recalcati 5255d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 5265d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 5275d4fc8d9SRaffaele Recalcati 5285d4fc8d9SRaffaele Recalcati return ret; 5295d4fc8d9SRaffaele Recalcati 530272cc70bSAndy Fleming } 531272cc70bSAndy Fleming 532272cc70bSAndy Fleming int mmc_change_freq(struct mmc *mmc) 533272cc70bSAndy Fleming { 534272cc70bSAndy Fleming char ext_csd[512]; 535272cc70bSAndy Fleming char cardtype; 536272cc70bSAndy Fleming int err; 537272cc70bSAndy Fleming 538272cc70bSAndy Fleming mmc->card_caps = 0; 539272cc70bSAndy Fleming 540d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 541d52ebf10SThomas Chou return 0; 542d52ebf10SThomas Chou 543272cc70bSAndy Fleming /* Only version 4 supports high-speed */ 544272cc70bSAndy Fleming if (mmc->version < MMC_VERSION_4) 545272cc70bSAndy Fleming return 0; 546272cc70bSAndy Fleming 547272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_4BIT; 548272cc70bSAndy Fleming 549272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 550272cc70bSAndy Fleming 551272cc70bSAndy Fleming if (err) 552272cc70bSAndy Fleming return err; 553272cc70bSAndy Fleming 554272cc70bSAndy Fleming cardtype = ext_csd[196] & 0xf; 555272cc70bSAndy Fleming 556272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); 557272cc70bSAndy Fleming 558272cc70bSAndy Fleming if (err) 559272cc70bSAndy Fleming return err; 560272cc70bSAndy Fleming 561272cc70bSAndy Fleming /* Now check to see that it worked */ 562272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 563272cc70bSAndy Fleming 564272cc70bSAndy Fleming if (err) 565272cc70bSAndy Fleming return err; 566272cc70bSAndy Fleming 567272cc70bSAndy Fleming /* No high-speed support */ 568272cc70bSAndy Fleming if (!ext_csd[185]) 569272cc70bSAndy Fleming return 0; 570272cc70bSAndy Fleming 571272cc70bSAndy Fleming /* High Speed is set, there are two types: 52MHz and 26MHz */ 572272cc70bSAndy Fleming if (cardtype & MMC_HS_52MHZ) 573272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; 574272cc70bSAndy Fleming else 575272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 576272cc70bSAndy Fleming 577272cc70bSAndy Fleming return 0; 578272cc70bSAndy Fleming } 579272cc70bSAndy Fleming 580272cc70bSAndy Fleming int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) 581272cc70bSAndy Fleming { 582272cc70bSAndy Fleming struct mmc_cmd cmd; 583272cc70bSAndy Fleming struct mmc_data data; 584272cc70bSAndy Fleming 585272cc70bSAndy Fleming /* Switch the frequency */ 586272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SWITCH_FUNC; 587272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 588272cc70bSAndy Fleming cmd.cmdarg = (mode << 31) | 0xffffff; 589272cc70bSAndy Fleming cmd.cmdarg &= ~(0xf << (group * 4)); 590272cc70bSAndy Fleming cmd.cmdarg |= value << (group * 4); 591272cc70bSAndy Fleming cmd.flags = 0; 592272cc70bSAndy Fleming 593272cc70bSAndy Fleming data.dest = (char *)resp; 594272cc70bSAndy Fleming data.blocksize = 64; 595272cc70bSAndy Fleming data.blocks = 1; 596272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 597272cc70bSAndy Fleming 598272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, &data); 599272cc70bSAndy Fleming } 600272cc70bSAndy Fleming 601272cc70bSAndy Fleming 602272cc70bSAndy Fleming int sd_change_freq(struct mmc *mmc) 603272cc70bSAndy Fleming { 604272cc70bSAndy Fleming int err; 605272cc70bSAndy Fleming struct mmc_cmd cmd; 606272cc70bSAndy Fleming uint scr[2]; 607272cc70bSAndy Fleming uint switch_status[16]; 608272cc70bSAndy Fleming struct mmc_data data; 609272cc70bSAndy Fleming int timeout; 610272cc70bSAndy Fleming 611272cc70bSAndy Fleming mmc->card_caps = 0; 612272cc70bSAndy Fleming 613d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 614d52ebf10SThomas Chou return 0; 615d52ebf10SThomas Chou 616272cc70bSAndy Fleming /* Read the SCR to find out if this card supports higher speeds */ 617272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 618272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 619272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 620272cc70bSAndy Fleming cmd.flags = 0; 621272cc70bSAndy Fleming 622272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 623272cc70bSAndy Fleming 624272cc70bSAndy Fleming if (err) 625272cc70bSAndy Fleming return err; 626272cc70bSAndy Fleming 627272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_SCR; 628272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 629272cc70bSAndy Fleming cmd.cmdarg = 0; 630272cc70bSAndy Fleming cmd.flags = 0; 631272cc70bSAndy Fleming 632272cc70bSAndy Fleming timeout = 3; 633272cc70bSAndy Fleming 634272cc70bSAndy Fleming retry_scr: 635272cc70bSAndy Fleming data.dest = (char *)&scr; 636272cc70bSAndy Fleming data.blocksize = 8; 637272cc70bSAndy Fleming data.blocks = 1; 638272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 639272cc70bSAndy Fleming 640272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 641272cc70bSAndy Fleming 642272cc70bSAndy Fleming if (err) { 643272cc70bSAndy Fleming if (timeout--) 644272cc70bSAndy Fleming goto retry_scr; 645272cc70bSAndy Fleming 646272cc70bSAndy Fleming return err; 647272cc70bSAndy Fleming } 648272cc70bSAndy Fleming 6494e3d89baSYauhen Kharuzhy mmc->scr[0] = __be32_to_cpu(scr[0]); 6504e3d89baSYauhen Kharuzhy mmc->scr[1] = __be32_to_cpu(scr[1]); 651272cc70bSAndy Fleming 652272cc70bSAndy Fleming switch ((mmc->scr[0] >> 24) & 0xf) { 653272cc70bSAndy Fleming case 0: 654272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 655272cc70bSAndy Fleming break; 656272cc70bSAndy Fleming case 1: 657272cc70bSAndy Fleming mmc->version = SD_VERSION_1_10; 658272cc70bSAndy Fleming break; 659272cc70bSAndy Fleming case 2: 660272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 661272cc70bSAndy Fleming break; 662272cc70bSAndy Fleming default: 663272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 664272cc70bSAndy Fleming break; 665272cc70bSAndy Fleming } 666272cc70bSAndy Fleming 667b44c7083SAlagu Sankar if (mmc->scr[0] & SD_DATA_4BIT) 668b44c7083SAlagu Sankar mmc->card_caps |= MMC_MODE_4BIT; 669b44c7083SAlagu Sankar 670272cc70bSAndy Fleming /* Version 1.0 doesn't support switching */ 671272cc70bSAndy Fleming if (mmc->version == SD_VERSION_1_0) 672272cc70bSAndy Fleming return 0; 673272cc70bSAndy Fleming 674272cc70bSAndy Fleming timeout = 4; 675272cc70bSAndy Fleming while (timeout--) { 676272cc70bSAndy Fleming err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, 677272cc70bSAndy Fleming (u8 *)&switch_status); 678272cc70bSAndy Fleming 679272cc70bSAndy Fleming if (err) 680272cc70bSAndy Fleming return err; 681272cc70bSAndy Fleming 682272cc70bSAndy Fleming /* The high-speed function is busy. Try again */ 6834e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) 684272cc70bSAndy Fleming break; 685272cc70bSAndy Fleming } 686272cc70bSAndy Fleming 687272cc70bSAndy Fleming /* If high-speed isn't supported, we return */ 6884e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) 689272cc70bSAndy Fleming return 0; 690272cc70bSAndy Fleming 691272cc70bSAndy Fleming err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status); 692272cc70bSAndy Fleming 693272cc70bSAndy Fleming if (err) 694272cc70bSAndy Fleming return err; 695272cc70bSAndy Fleming 6964e3d89baSYauhen Kharuzhy if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) 697272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 698272cc70bSAndy Fleming 699272cc70bSAndy Fleming return 0; 700272cc70bSAndy Fleming } 701272cc70bSAndy Fleming 702272cc70bSAndy Fleming /* frequency bases */ 703272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */ 7045f837c2cSMike Frysinger static const int fbase[] = { 705272cc70bSAndy Fleming 10000, 706272cc70bSAndy Fleming 100000, 707272cc70bSAndy Fleming 1000000, 708272cc70bSAndy Fleming 10000000, 709272cc70bSAndy Fleming }; 710272cc70bSAndy Fleming 711272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice 712272cc70bSAndy Fleming * to platforms without floating point. 713272cc70bSAndy Fleming */ 7145f837c2cSMike Frysinger static const int multipliers[] = { 715272cc70bSAndy Fleming 0, /* reserved */ 716272cc70bSAndy Fleming 10, 717272cc70bSAndy Fleming 12, 718272cc70bSAndy Fleming 13, 719272cc70bSAndy Fleming 15, 720272cc70bSAndy Fleming 20, 721272cc70bSAndy Fleming 25, 722272cc70bSAndy Fleming 30, 723272cc70bSAndy Fleming 35, 724272cc70bSAndy Fleming 40, 725272cc70bSAndy Fleming 45, 726272cc70bSAndy Fleming 50, 727272cc70bSAndy Fleming 55, 728272cc70bSAndy Fleming 60, 729272cc70bSAndy Fleming 70, 730272cc70bSAndy Fleming 80, 731272cc70bSAndy Fleming }; 732272cc70bSAndy Fleming 733272cc70bSAndy Fleming void mmc_set_ios(struct mmc *mmc) 734272cc70bSAndy Fleming { 735272cc70bSAndy Fleming mmc->set_ios(mmc); 736272cc70bSAndy Fleming } 737272cc70bSAndy Fleming 738272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock) 739272cc70bSAndy Fleming { 740272cc70bSAndy Fleming if (clock > mmc->f_max) 741272cc70bSAndy Fleming clock = mmc->f_max; 742272cc70bSAndy Fleming 743272cc70bSAndy Fleming if (clock < mmc->f_min) 744272cc70bSAndy Fleming clock = mmc->f_min; 745272cc70bSAndy Fleming 746272cc70bSAndy Fleming mmc->clock = clock; 747272cc70bSAndy Fleming 748272cc70bSAndy Fleming mmc_set_ios(mmc); 749272cc70bSAndy Fleming } 750272cc70bSAndy Fleming 751272cc70bSAndy Fleming void mmc_set_bus_width(struct mmc *mmc, uint width) 752272cc70bSAndy Fleming { 753272cc70bSAndy Fleming mmc->bus_width = width; 754272cc70bSAndy Fleming 755272cc70bSAndy Fleming mmc_set_ios(mmc); 756272cc70bSAndy Fleming } 757272cc70bSAndy Fleming 758272cc70bSAndy Fleming int mmc_startup(struct mmc *mmc) 759272cc70bSAndy Fleming { 760272cc70bSAndy Fleming int err; 761272cc70bSAndy Fleming uint mult, freq; 762272cc70bSAndy Fleming u64 cmult, csize; 763272cc70bSAndy Fleming struct mmc_cmd cmd; 764d23e2c09SSukumar Ghorai char ext_csd[512]; 7655d4fc8d9SRaffaele Recalcati int timeout = 1000; 766272cc70bSAndy Fleming 767d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON 768d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ 769d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF; 770d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R1; 771d52ebf10SThomas Chou cmd.cmdarg = 1; 772d52ebf10SThomas Chou cmd.flags = 0; 773d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 774d52ebf10SThomas Chou 775d52ebf10SThomas Chou if (err) 776d52ebf10SThomas Chou return err; 777d52ebf10SThomas Chou } 778d52ebf10SThomas Chou #endif 779d52ebf10SThomas Chou 780272cc70bSAndy Fleming /* Put the Card in Identify Mode */ 781d52ebf10SThomas Chou cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID : 782d52ebf10SThomas Chou MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */ 783272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 784272cc70bSAndy Fleming cmd.cmdarg = 0; 785272cc70bSAndy Fleming cmd.flags = 0; 786272cc70bSAndy Fleming 787272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 788272cc70bSAndy Fleming 789272cc70bSAndy Fleming if (err) 790272cc70bSAndy Fleming return err; 791272cc70bSAndy Fleming 792272cc70bSAndy Fleming memcpy(mmc->cid, cmd.response, 16); 793272cc70bSAndy Fleming 794272cc70bSAndy Fleming /* 795272cc70bSAndy Fleming * For MMC cards, set the Relative Address. 796272cc70bSAndy Fleming * For SD cards, get the Relatvie Address. 797272cc70bSAndy Fleming * This also puts the cards into Standby State 798272cc70bSAndy Fleming */ 799d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 800272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; 801272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 802272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R6; 803272cc70bSAndy Fleming cmd.flags = 0; 804272cc70bSAndy Fleming 805272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 806272cc70bSAndy Fleming 807272cc70bSAndy Fleming if (err) 808272cc70bSAndy Fleming return err; 809272cc70bSAndy Fleming 810272cc70bSAndy Fleming if (IS_SD(mmc)) 811998be3ddSRabin Vincent mmc->rca = (cmd.response[0] >> 16) & 0xffff; 812d52ebf10SThomas Chou } 813272cc70bSAndy Fleming 814272cc70bSAndy Fleming /* Get the Card-Specific Data */ 815272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_CSD; 816272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 817272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 818272cc70bSAndy Fleming cmd.flags = 0; 819272cc70bSAndy Fleming 820272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 821272cc70bSAndy Fleming 8225d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 8235d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 8245d4fc8d9SRaffaele Recalcati 825272cc70bSAndy Fleming if (err) 826272cc70bSAndy Fleming return err; 827272cc70bSAndy Fleming 828998be3ddSRabin Vincent mmc->csd[0] = cmd.response[0]; 829998be3ddSRabin Vincent mmc->csd[1] = cmd.response[1]; 830998be3ddSRabin Vincent mmc->csd[2] = cmd.response[2]; 831998be3ddSRabin Vincent mmc->csd[3] = cmd.response[3]; 832272cc70bSAndy Fleming 833272cc70bSAndy Fleming if (mmc->version == MMC_VERSION_UNKNOWN) { 8340b453ffeSRabin Vincent int version = (cmd.response[0] >> 26) & 0xf; 835272cc70bSAndy Fleming 836272cc70bSAndy Fleming switch (version) { 837272cc70bSAndy Fleming case 0: 838272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 839272cc70bSAndy Fleming break; 840272cc70bSAndy Fleming case 1: 841272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_4; 842272cc70bSAndy Fleming break; 843272cc70bSAndy Fleming case 2: 844272cc70bSAndy Fleming mmc->version = MMC_VERSION_2_2; 845272cc70bSAndy Fleming break; 846272cc70bSAndy Fleming case 3: 847272cc70bSAndy Fleming mmc->version = MMC_VERSION_3; 848272cc70bSAndy Fleming break; 849272cc70bSAndy Fleming case 4: 850272cc70bSAndy Fleming mmc->version = MMC_VERSION_4; 851272cc70bSAndy Fleming break; 852272cc70bSAndy Fleming default: 853272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 854272cc70bSAndy Fleming break; 855272cc70bSAndy Fleming } 856272cc70bSAndy Fleming } 857272cc70bSAndy Fleming 858272cc70bSAndy Fleming /* divide frequency by 10, since the mults are 10x bigger */ 8590b453ffeSRabin Vincent freq = fbase[(cmd.response[0] & 0x7)]; 8600b453ffeSRabin Vincent mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; 861272cc70bSAndy Fleming 862272cc70bSAndy Fleming mmc->tran_speed = freq * mult; 863272cc70bSAndy Fleming 864998be3ddSRabin Vincent mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); 865272cc70bSAndy Fleming 866272cc70bSAndy Fleming if (IS_SD(mmc)) 867272cc70bSAndy Fleming mmc->write_bl_len = mmc->read_bl_len; 868272cc70bSAndy Fleming else 869998be3ddSRabin Vincent mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); 870272cc70bSAndy Fleming 871272cc70bSAndy Fleming if (mmc->high_capacity) { 872272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3f) << 16 873272cc70bSAndy Fleming | (mmc->csd[2] & 0xffff0000) >> 16; 874272cc70bSAndy Fleming cmult = 8; 875272cc70bSAndy Fleming } else { 876272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3ff) << 2 877272cc70bSAndy Fleming | (mmc->csd[2] & 0xc0000000) >> 30; 878272cc70bSAndy Fleming cmult = (mmc->csd[2] & 0x00038000) >> 15; 879272cc70bSAndy Fleming } 880272cc70bSAndy Fleming 881272cc70bSAndy Fleming mmc->capacity = (csize + 1) << (cmult + 2); 882272cc70bSAndy Fleming mmc->capacity *= mmc->read_bl_len; 883272cc70bSAndy Fleming 884272cc70bSAndy Fleming if (mmc->read_bl_len > 512) 885272cc70bSAndy Fleming mmc->read_bl_len = 512; 886272cc70bSAndy Fleming 887272cc70bSAndy Fleming if (mmc->write_bl_len > 512) 888272cc70bSAndy Fleming mmc->write_bl_len = 512; 889272cc70bSAndy Fleming 890272cc70bSAndy Fleming /* Select the card, and put it into Transfer Mode */ 891d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 892272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SELECT_CARD; 893272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 894272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 895272cc70bSAndy Fleming cmd.flags = 0; 896272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 897272cc70bSAndy Fleming 898272cc70bSAndy Fleming if (err) 899272cc70bSAndy Fleming return err; 900d52ebf10SThomas Chou } 901272cc70bSAndy Fleming 902d23e2c09SSukumar Ghorai if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) { 903d23e2c09SSukumar Ghorai /* check ext_csd version and capacity */ 904d23e2c09SSukumar Ghorai err = mmc_send_ext_csd(mmc, ext_csd); 905d23e2c09SSukumar Ghorai if (!err & (ext_csd[192] >= 2)) { 906d23e2c09SSukumar Ghorai mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 | 907d23e2c09SSukumar Ghorai ext_csd[214] << 16 | ext_csd[215] << 24; 908d23e2c09SSukumar Ghorai mmc->capacity *= 512; 909d23e2c09SSukumar Ghorai } 910d23e2c09SSukumar Ghorai } 911d23e2c09SSukumar Ghorai 912272cc70bSAndy Fleming if (IS_SD(mmc)) 913272cc70bSAndy Fleming err = sd_change_freq(mmc); 914272cc70bSAndy Fleming else 915272cc70bSAndy Fleming err = mmc_change_freq(mmc); 916272cc70bSAndy Fleming 917272cc70bSAndy Fleming if (err) 918272cc70bSAndy Fleming return err; 919272cc70bSAndy Fleming 920272cc70bSAndy Fleming /* Restrict card's capabilities by what the host can do */ 921272cc70bSAndy Fleming mmc->card_caps &= mmc->host_caps; 922272cc70bSAndy Fleming 923272cc70bSAndy Fleming if (IS_SD(mmc)) { 924272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_4BIT) { 925272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 926272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 927272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 928272cc70bSAndy Fleming cmd.flags = 0; 929272cc70bSAndy Fleming 930272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 931272cc70bSAndy Fleming if (err) 932272cc70bSAndy Fleming return err; 933272cc70bSAndy Fleming 934272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; 935272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 936272cc70bSAndy Fleming cmd.cmdarg = 2; 937272cc70bSAndy Fleming cmd.flags = 0; 938272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 939272cc70bSAndy Fleming if (err) 940272cc70bSAndy Fleming return err; 941272cc70bSAndy Fleming 942272cc70bSAndy Fleming mmc_set_bus_width(mmc, 4); 943272cc70bSAndy Fleming } 944272cc70bSAndy Fleming 945272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) 946272cc70bSAndy Fleming mmc_set_clock(mmc, 50000000); 947272cc70bSAndy Fleming else 948272cc70bSAndy Fleming mmc_set_clock(mmc, 25000000); 949272cc70bSAndy Fleming } else { 950272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_4BIT) { 951272cc70bSAndy Fleming /* Set the card to use 4 bit*/ 952272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 953272cc70bSAndy Fleming EXT_CSD_BUS_WIDTH, 954272cc70bSAndy Fleming EXT_CSD_BUS_WIDTH_4); 955272cc70bSAndy Fleming 956272cc70bSAndy Fleming if (err) 957272cc70bSAndy Fleming return err; 958272cc70bSAndy Fleming 959272cc70bSAndy Fleming mmc_set_bus_width(mmc, 4); 960272cc70bSAndy Fleming } else if (mmc->card_caps & MMC_MODE_8BIT) { 961272cc70bSAndy Fleming /* Set the card to use 8 bit*/ 962272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 963272cc70bSAndy Fleming EXT_CSD_BUS_WIDTH, 964272cc70bSAndy Fleming EXT_CSD_BUS_WIDTH_8); 965272cc70bSAndy Fleming 966272cc70bSAndy Fleming if (err) 967272cc70bSAndy Fleming return err; 968272cc70bSAndy Fleming 969272cc70bSAndy Fleming mmc_set_bus_width(mmc, 8); 970272cc70bSAndy Fleming } 971272cc70bSAndy Fleming 972272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) { 973272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS_52MHz) 974272cc70bSAndy Fleming mmc_set_clock(mmc, 52000000); 975272cc70bSAndy Fleming else 976272cc70bSAndy Fleming mmc_set_clock(mmc, 26000000); 977272cc70bSAndy Fleming } else 978272cc70bSAndy Fleming mmc_set_clock(mmc, 20000000); 979272cc70bSAndy Fleming } 980272cc70bSAndy Fleming 981272cc70bSAndy Fleming /* fill in device description */ 982272cc70bSAndy Fleming mmc->block_dev.lun = 0; 983272cc70bSAndy Fleming mmc->block_dev.type = 0; 984272cc70bSAndy Fleming mmc->block_dev.blksz = mmc->read_bl_len; 9859b1f942cSRabin Vincent mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); 9860b453ffeSRabin Vincent sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8, 9870b453ffeSRabin Vincent (mmc->cid[2] << 8) | (mmc->cid[3] >> 24)); 9880b453ffeSRabin Vincent sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff, 9890b453ffeSRabin Vincent (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, 9900b453ffeSRabin Vincent (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); 9910b453ffeSRabin Vincent sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28, 9920b453ffeSRabin Vincent (mmc->cid[2] >> 24) & 0xf); 993272cc70bSAndy Fleming init_part(&mmc->block_dev); 994272cc70bSAndy Fleming 995272cc70bSAndy Fleming return 0; 996272cc70bSAndy Fleming } 997272cc70bSAndy Fleming 998272cc70bSAndy Fleming int mmc_send_if_cond(struct mmc *mmc) 999272cc70bSAndy Fleming { 1000272cc70bSAndy Fleming struct mmc_cmd cmd; 1001272cc70bSAndy Fleming int err; 1002272cc70bSAndy Fleming 1003272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_IF_COND; 1004272cc70bSAndy Fleming /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ 1005272cc70bSAndy Fleming cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa; 1006272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R7; 1007272cc70bSAndy Fleming cmd.flags = 0; 1008272cc70bSAndy Fleming 1009272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1010272cc70bSAndy Fleming 1011272cc70bSAndy Fleming if (err) 1012272cc70bSAndy Fleming return err; 1013272cc70bSAndy Fleming 1014998be3ddSRabin Vincent if ((cmd.response[0] & 0xff) != 0xaa) 1015272cc70bSAndy Fleming return UNUSABLE_ERR; 1016272cc70bSAndy Fleming else 1017272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 1018272cc70bSAndy Fleming 1019272cc70bSAndy Fleming return 0; 1020272cc70bSAndy Fleming } 1021272cc70bSAndy Fleming 1022272cc70bSAndy Fleming int mmc_register(struct mmc *mmc) 1023272cc70bSAndy Fleming { 1024272cc70bSAndy Fleming /* Setup the universal parts of the block interface just once */ 1025272cc70bSAndy Fleming mmc->block_dev.if_type = IF_TYPE_MMC; 1026272cc70bSAndy Fleming mmc->block_dev.dev = cur_dev_num++; 1027272cc70bSAndy Fleming mmc->block_dev.removable = 1; 1028272cc70bSAndy Fleming mmc->block_dev.block_read = mmc_bread; 1029272cc70bSAndy Fleming mmc->block_dev.block_write = mmc_bwrite; 10308feafcc4SJohn Rigby if (!mmc->b_max) 10318feafcc4SJohn Rigby mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; 1032272cc70bSAndy Fleming 1033272cc70bSAndy Fleming INIT_LIST_HEAD (&mmc->link); 1034272cc70bSAndy Fleming 1035272cc70bSAndy Fleming list_add_tail (&mmc->link, &mmc_devices); 1036272cc70bSAndy Fleming 1037272cc70bSAndy Fleming return 0; 1038272cc70bSAndy Fleming } 1039272cc70bSAndy Fleming 1040272cc70bSAndy Fleming block_dev_desc_t *mmc_get_dev(int dev) 1041272cc70bSAndy Fleming { 1042272cc70bSAndy Fleming struct mmc *mmc = find_mmc_device(dev); 1043272cc70bSAndy Fleming 1044e85649c7SRabin Vincent return mmc ? &mmc->block_dev : NULL; 1045272cc70bSAndy Fleming } 1046272cc70bSAndy Fleming 1047272cc70bSAndy Fleming int mmc_init(struct mmc *mmc) 1048272cc70bSAndy Fleming { 1049272cc70bSAndy Fleming int err; 1050272cc70bSAndy Fleming 1051272cc70bSAndy Fleming err = mmc->init(mmc); 1052272cc70bSAndy Fleming 1053272cc70bSAndy Fleming if (err) 1054272cc70bSAndy Fleming return err; 1055272cc70bSAndy Fleming 1056b86b85e2SIlya Yanok mmc_set_bus_width(mmc, 1); 1057b86b85e2SIlya Yanok mmc_set_clock(mmc, 1); 1058b86b85e2SIlya Yanok 1059272cc70bSAndy Fleming /* Reset the Card */ 1060272cc70bSAndy Fleming err = mmc_go_idle(mmc); 1061272cc70bSAndy Fleming 1062272cc70bSAndy Fleming if (err) 1063272cc70bSAndy Fleming return err; 1064272cc70bSAndy Fleming 1065272cc70bSAndy Fleming /* Test for SD version 2 */ 1066272cc70bSAndy Fleming err = mmc_send_if_cond(mmc); 1067272cc70bSAndy Fleming 1068272cc70bSAndy Fleming /* Now try to get the SD card's operating condition */ 1069272cc70bSAndy Fleming err = sd_send_op_cond(mmc); 1070272cc70bSAndy Fleming 1071272cc70bSAndy Fleming /* If the command timed out, we check for an MMC card */ 1072272cc70bSAndy Fleming if (err == TIMEOUT) { 1073272cc70bSAndy Fleming err = mmc_send_op_cond(mmc); 1074272cc70bSAndy Fleming 1075272cc70bSAndy Fleming if (err) { 1076272cc70bSAndy Fleming printf("Card did not respond to voltage select!\n"); 1077272cc70bSAndy Fleming return UNUSABLE_ERR; 1078272cc70bSAndy Fleming } 1079272cc70bSAndy Fleming } 1080272cc70bSAndy Fleming 1081272cc70bSAndy Fleming return mmc_startup(mmc); 1082272cc70bSAndy Fleming } 1083272cc70bSAndy Fleming 1084272cc70bSAndy Fleming /* 1085272cc70bSAndy Fleming * CPU and board-specific MMC initializations. Aliased function 1086272cc70bSAndy Fleming * signals caller to move on 1087272cc70bSAndy Fleming */ 1088272cc70bSAndy Fleming static int __def_mmc_init(bd_t *bis) 1089272cc70bSAndy Fleming { 1090272cc70bSAndy Fleming return -1; 1091272cc70bSAndy Fleming } 1092272cc70bSAndy Fleming 1093f9a109b3SPeter Tyser int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); 1094f9a109b3SPeter Tyser int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); 1095272cc70bSAndy Fleming 1096272cc70bSAndy Fleming void print_mmc_devices(char separator) 1097272cc70bSAndy Fleming { 1098272cc70bSAndy Fleming struct mmc *m; 1099272cc70bSAndy Fleming struct list_head *entry; 1100272cc70bSAndy Fleming 1101272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 1102272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 1103272cc70bSAndy Fleming 1104272cc70bSAndy Fleming printf("%s: %d", m->name, m->block_dev.dev); 1105272cc70bSAndy Fleming 1106272cc70bSAndy Fleming if (entry->next != &mmc_devices) 1107272cc70bSAndy Fleming printf("%c ", separator); 1108272cc70bSAndy Fleming } 1109272cc70bSAndy Fleming 1110272cc70bSAndy Fleming printf("\n"); 1111272cc70bSAndy Fleming } 1112272cc70bSAndy Fleming 1113*ea6ebe21SLei Wen int get_mmc_num(void) 1114*ea6ebe21SLei Wen { 1115*ea6ebe21SLei Wen return cur_dev_num; 1116*ea6ebe21SLei Wen } 1117*ea6ebe21SLei Wen 1118272cc70bSAndy Fleming int mmc_initialize(bd_t *bis) 1119272cc70bSAndy Fleming { 1120272cc70bSAndy Fleming INIT_LIST_HEAD (&mmc_devices); 1121272cc70bSAndy Fleming cur_dev_num = 0; 1122272cc70bSAndy Fleming 1123272cc70bSAndy Fleming if (board_mmc_init(bis) < 0) 1124272cc70bSAndy Fleming cpu_mmc_init(bis); 1125272cc70bSAndy Fleming 1126272cc70bSAndy Fleming print_mmc_devices(','); 1127272cc70bSAndy Fleming 1128272cc70bSAndy Fleming return 0; 1129272cc70bSAndy Fleming } 1130