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; 118aaf3d41aSMarek Vasut if (!mmc_host_is_spi(mmc)) 119aaf3d41aSMarek Vasut cmd.cmdarg = mmc->rca << 16; 1205d4fc8d9SRaffaele Recalcati cmd.flags = 0; 1215d4fc8d9SRaffaele Recalcati 1225d4fc8d9SRaffaele Recalcati do { 1235d4fc8d9SRaffaele Recalcati err = mmc_send_cmd(mmc, &cmd, NULL); 1245d4fc8d9SRaffaele Recalcati if (err) 1255d4fc8d9SRaffaele Recalcati return err; 1265d4fc8d9SRaffaele Recalcati else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) 1275d4fc8d9SRaffaele Recalcati break; 1285d4fc8d9SRaffaele Recalcati 1295d4fc8d9SRaffaele Recalcati udelay(1000); 1305d4fc8d9SRaffaele Recalcati 1315d4fc8d9SRaffaele Recalcati if (cmd.response[0] & MMC_STATUS_MASK) { 1325d4fc8d9SRaffaele Recalcati printf("Status Error: 0x%08X\n", cmd.response[0]); 1335d4fc8d9SRaffaele Recalcati return COMM_ERR; 1345d4fc8d9SRaffaele Recalcati } 1355d4fc8d9SRaffaele Recalcati } while (timeout--); 1365d4fc8d9SRaffaele Recalcati 1375db2fe3aSRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 1385db2fe3aSRaffaele Recalcati status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9; 1395db2fe3aSRaffaele Recalcati printf("CURR STATE:%d\n", status); 1405db2fe3aSRaffaele Recalcati #endif 1415d4fc8d9SRaffaele Recalcati if (!timeout) { 1425d4fc8d9SRaffaele Recalcati printf("Timeout waiting card ready\n"); 1435d4fc8d9SRaffaele Recalcati return TIMEOUT; 1445d4fc8d9SRaffaele Recalcati } 1455d4fc8d9SRaffaele Recalcati 1465d4fc8d9SRaffaele Recalcati return 0; 1475d4fc8d9SRaffaele Recalcati } 1485d4fc8d9SRaffaele Recalcati 149272cc70bSAndy Fleming int mmc_set_blocklen(struct mmc *mmc, int len) 150272cc70bSAndy Fleming { 151272cc70bSAndy Fleming struct mmc_cmd cmd; 152272cc70bSAndy Fleming 153272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; 154272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 155272cc70bSAndy Fleming cmd.cmdarg = len; 156272cc70bSAndy Fleming cmd.flags = 0; 157272cc70bSAndy Fleming 158272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, NULL); 159272cc70bSAndy Fleming } 160272cc70bSAndy Fleming 161272cc70bSAndy Fleming struct mmc *find_mmc_device(int dev_num) 162272cc70bSAndy Fleming { 163272cc70bSAndy Fleming struct mmc *m; 164272cc70bSAndy Fleming struct list_head *entry; 165272cc70bSAndy Fleming 166272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 167272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 168272cc70bSAndy Fleming 169272cc70bSAndy Fleming if (m->block_dev.dev == dev_num) 170272cc70bSAndy Fleming return m; 171272cc70bSAndy Fleming } 172272cc70bSAndy Fleming 173272cc70bSAndy Fleming printf("MMC Device %d not found\n", dev_num); 174272cc70bSAndy Fleming 175272cc70bSAndy Fleming return NULL; 176272cc70bSAndy Fleming } 177272cc70bSAndy Fleming 178e6f99a56SLei Wen static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt) 179e6f99a56SLei Wen { 180e6f99a56SLei Wen struct mmc_cmd cmd; 181e6f99a56SLei Wen ulong end; 182e6f99a56SLei Wen int err, start_cmd, end_cmd; 183e6f99a56SLei Wen 184e6f99a56SLei Wen if (mmc->high_capacity) 185e6f99a56SLei Wen end = start + blkcnt - 1; 186e6f99a56SLei Wen else { 187e6f99a56SLei Wen end = (start + blkcnt - 1) * mmc->write_bl_len; 188e6f99a56SLei Wen start *= mmc->write_bl_len; 189e6f99a56SLei Wen } 190e6f99a56SLei Wen 191e6f99a56SLei Wen if (IS_SD(mmc)) { 192e6f99a56SLei Wen start_cmd = SD_CMD_ERASE_WR_BLK_START; 193e6f99a56SLei Wen end_cmd = SD_CMD_ERASE_WR_BLK_END; 194e6f99a56SLei Wen } else { 195e6f99a56SLei Wen start_cmd = MMC_CMD_ERASE_GROUP_START; 196e6f99a56SLei Wen end_cmd = MMC_CMD_ERASE_GROUP_END; 197e6f99a56SLei Wen } 198e6f99a56SLei Wen 199e6f99a56SLei Wen cmd.cmdidx = start_cmd; 200e6f99a56SLei Wen cmd.cmdarg = start; 201e6f99a56SLei Wen cmd.resp_type = MMC_RSP_R1; 202e6f99a56SLei Wen cmd.flags = 0; 203e6f99a56SLei Wen 204e6f99a56SLei Wen err = mmc_send_cmd(mmc, &cmd, NULL); 205e6f99a56SLei Wen if (err) 206e6f99a56SLei Wen goto err_out; 207e6f99a56SLei Wen 208e6f99a56SLei Wen cmd.cmdidx = end_cmd; 209e6f99a56SLei Wen cmd.cmdarg = end; 210e6f99a56SLei Wen 211e6f99a56SLei Wen err = mmc_send_cmd(mmc, &cmd, NULL); 212e6f99a56SLei Wen if (err) 213e6f99a56SLei Wen goto err_out; 214e6f99a56SLei Wen 215e6f99a56SLei Wen cmd.cmdidx = MMC_CMD_ERASE; 216e6f99a56SLei Wen cmd.cmdarg = SECURE_ERASE; 217e6f99a56SLei Wen cmd.resp_type = MMC_RSP_R1b; 218e6f99a56SLei Wen 219e6f99a56SLei Wen err = mmc_send_cmd(mmc, &cmd, NULL); 220e6f99a56SLei Wen if (err) 221e6f99a56SLei Wen goto err_out; 222e6f99a56SLei Wen 223e6f99a56SLei Wen return 0; 224e6f99a56SLei Wen 225e6f99a56SLei Wen err_out: 226e6f99a56SLei Wen puts("mmc erase failed\n"); 227e6f99a56SLei Wen return err; 228e6f99a56SLei Wen } 229e6f99a56SLei Wen 230e6f99a56SLei Wen static unsigned long 231e6f99a56SLei Wen mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt) 232e6f99a56SLei Wen { 233e6f99a56SLei Wen int err = 0; 234e6f99a56SLei Wen struct mmc *mmc = find_mmc_device(dev_num); 235e6f99a56SLei Wen lbaint_t blk = 0, blk_r = 0; 236e6f99a56SLei Wen 237e6f99a56SLei Wen if (!mmc) 238e6f99a56SLei Wen return -1; 239e6f99a56SLei Wen 240e6f99a56SLei Wen if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size)) 241e6f99a56SLei Wen printf("\n\nCaution! Your devices Erase group is 0x%x\n" 242e6f99a56SLei Wen "The erase range would be change to 0x%lx~0x%lx\n\n", 243e6f99a56SLei Wen mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1), 244e6f99a56SLei Wen ((start + blkcnt + mmc->erase_grp_size) 245e6f99a56SLei Wen & ~(mmc->erase_grp_size - 1)) - 1); 246e6f99a56SLei Wen 247e6f99a56SLei Wen while (blk < blkcnt) { 248e6f99a56SLei Wen blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ? 249e6f99a56SLei Wen mmc->erase_grp_size : (blkcnt - blk); 250e6f99a56SLei Wen err = mmc_erase_t(mmc, start + blk, blk_r); 251e6f99a56SLei Wen if (err) 252e6f99a56SLei Wen break; 253e6f99a56SLei Wen 254e6f99a56SLei Wen blk += blk_r; 255e6f99a56SLei Wen } 256e6f99a56SLei Wen 257e6f99a56SLei Wen return blk; 258e6f99a56SLei Wen } 259e6f99a56SLei Wen 260272cc70bSAndy Fleming static ulong 2610158126eSLei Wen mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src) 262272cc70bSAndy Fleming { 263272cc70bSAndy Fleming struct mmc_cmd cmd; 264272cc70bSAndy Fleming struct mmc_data data; 2655d4fc8d9SRaffaele Recalcati int timeout = 1000; 266272cc70bSAndy Fleming 267d2bf29e3SLei Wen if ((start + blkcnt) > mmc->block_dev.lba) { 268def412b6SSteve Sakoman printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", 269d2bf29e3SLei Wen start + blkcnt, mmc->block_dev.lba); 270d2bf29e3SLei Wen return 0; 271d2bf29e3SLei Wen } 272272cc70bSAndy Fleming 273272cc70bSAndy Fleming if (blkcnt > 1) 274272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; 275272cc70bSAndy Fleming else 276272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; 277272cc70bSAndy Fleming 278272cc70bSAndy Fleming if (mmc->high_capacity) 279272cc70bSAndy Fleming cmd.cmdarg = start; 280272cc70bSAndy Fleming else 281def412b6SSteve Sakoman cmd.cmdarg = start * mmc->write_bl_len; 282272cc70bSAndy Fleming 283272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 284272cc70bSAndy Fleming cmd.flags = 0; 285272cc70bSAndy Fleming 286272cc70bSAndy Fleming data.src = src; 287272cc70bSAndy Fleming data.blocks = blkcnt; 288def412b6SSteve Sakoman data.blocksize = mmc->write_bl_len; 289272cc70bSAndy Fleming data.flags = MMC_DATA_WRITE; 290272cc70bSAndy Fleming 291def412b6SSteve Sakoman if (mmc_send_cmd(mmc, &cmd, &data)) { 292def412b6SSteve Sakoman printf("mmc write failed\n"); 293def412b6SSteve Sakoman return 0; 294272cc70bSAndy Fleming } 295272cc70bSAndy Fleming 296d52ebf10SThomas Chou /* SPI multiblock writes terminate using a special 297d52ebf10SThomas Chou * token, not a STOP_TRANSMISSION request. 298d52ebf10SThomas Chou */ 299d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc) && blkcnt > 1) { 300272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 301272cc70bSAndy Fleming cmd.cmdarg = 0; 302272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 303272cc70bSAndy Fleming cmd.flags = 0; 304def412b6SSteve Sakoman if (mmc_send_cmd(mmc, &cmd, NULL)) { 305def412b6SSteve Sakoman printf("mmc fail to send stop cmd\n"); 306def412b6SSteve Sakoman return 0; 307272cc70bSAndy Fleming } 3085d4fc8d9SRaffaele Recalcati 3095d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 3105d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 3110158126eSLei Wen } 3120158126eSLei Wen 3130158126eSLei Wen return blkcnt; 3140158126eSLei Wen } 3150158126eSLei Wen 3160158126eSLei Wen static ulong 3170158126eSLei Wen mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src) 3180158126eSLei Wen { 3190158126eSLei Wen lbaint_t cur, blocks_todo = blkcnt; 3200158126eSLei Wen 321def412b6SSteve Sakoman struct mmc *mmc = find_mmc_device(dev_num); 3220158126eSLei Wen if (!mmc) 323def412b6SSteve Sakoman return 0; 3240158126eSLei Wen 325def412b6SSteve Sakoman if (mmc_set_blocklen(mmc, mmc->write_bl_len)) 326def412b6SSteve Sakoman return 0; 3270158126eSLei Wen 3280158126eSLei Wen do { 3298feafcc4SJohn Rigby cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo; 3300158126eSLei Wen if(mmc_write_blocks(mmc, start, cur, src) != cur) 331def412b6SSteve Sakoman return 0; 3320158126eSLei Wen blocks_todo -= cur; 3330158126eSLei Wen start += cur; 3340158126eSLei Wen src += cur * mmc->write_bl_len; 3350158126eSLei Wen } while (blocks_todo > 0); 336272cc70bSAndy Fleming 337272cc70bSAndy Fleming return blkcnt; 338272cc70bSAndy Fleming } 339272cc70bSAndy Fleming 3404a1a06bcSAlagu Sankar int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt) 341272cc70bSAndy Fleming { 342272cc70bSAndy Fleming struct mmc_cmd cmd; 343272cc70bSAndy Fleming struct mmc_data data; 3445d4fc8d9SRaffaele Recalcati int timeout = 1000; 345272cc70bSAndy Fleming 3464a1a06bcSAlagu Sankar if (blkcnt > 1) 3474a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; 3484a1a06bcSAlagu Sankar else 349272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; 350272cc70bSAndy Fleming 351272cc70bSAndy Fleming if (mmc->high_capacity) 3524a1a06bcSAlagu Sankar cmd.cmdarg = start; 353272cc70bSAndy Fleming else 3544a1a06bcSAlagu Sankar cmd.cmdarg = start * mmc->read_bl_len; 355272cc70bSAndy Fleming 356272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 357272cc70bSAndy Fleming cmd.flags = 0; 358272cc70bSAndy Fleming 359272cc70bSAndy Fleming data.dest = dst; 3604a1a06bcSAlagu Sankar data.blocks = blkcnt; 361272cc70bSAndy Fleming data.blocksize = mmc->read_bl_len; 362272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 363272cc70bSAndy Fleming 3644a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, &data)) 3654a1a06bcSAlagu Sankar return 0; 3664a1a06bcSAlagu Sankar 3674a1a06bcSAlagu Sankar if (blkcnt > 1) { 3684a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 3694a1a06bcSAlagu Sankar cmd.cmdarg = 0; 3704a1a06bcSAlagu Sankar cmd.resp_type = MMC_RSP_R1b; 3714a1a06bcSAlagu Sankar cmd.flags = 0; 3724a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, NULL)) { 3734a1a06bcSAlagu Sankar printf("mmc fail to send stop cmd\n"); 3744a1a06bcSAlagu Sankar return 0; 3754a1a06bcSAlagu Sankar } 3765d4fc8d9SRaffaele Recalcati 3775d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 3785d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 379272cc70bSAndy Fleming } 380272cc70bSAndy Fleming 3814a1a06bcSAlagu Sankar return blkcnt; 382272cc70bSAndy Fleming } 383272cc70bSAndy Fleming 384272cc70bSAndy Fleming static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst) 385272cc70bSAndy Fleming { 3864a1a06bcSAlagu Sankar lbaint_t cur, blocks_todo = blkcnt; 387272cc70bSAndy Fleming 3884a1a06bcSAlagu Sankar if (blkcnt == 0) 3894a1a06bcSAlagu Sankar return 0; 3904a1a06bcSAlagu Sankar 3914a1a06bcSAlagu Sankar struct mmc *mmc = find_mmc_device(dev_num); 392272cc70bSAndy Fleming if (!mmc) 393272cc70bSAndy Fleming return 0; 394272cc70bSAndy Fleming 395d2bf29e3SLei Wen if ((start + blkcnt) > mmc->block_dev.lba) { 3964a1a06bcSAlagu Sankar printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", 397d2bf29e3SLei Wen start + blkcnt, mmc->block_dev.lba); 398d2bf29e3SLei Wen return 0; 399d2bf29e3SLei Wen } 400272cc70bSAndy Fleming 4014a1a06bcSAlagu Sankar if (mmc_set_blocklen(mmc, mmc->read_bl_len)) 402272cc70bSAndy Fleming return 0; 403272cc70bSAndy Fleming 4044a1a06bcSAlagu Sankar do { 4058feafcc4SJohn Rigby cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo; 4064a1a06bcSAlagu Sankar if(mmc_read_blocks(mmc, dst, start, cur) != cur) 4074a1a06bcSAlagu Sankar return 0; 4084a1a06bcSAlagu Sankar blocks_todo -= cur; 4094a1a06bcSAlagu Sankar start += cur; 4104a1a06bcSAlagu Sankar dst += cur * mmc->read_bl_len; 4114a1a06bcSAlagu Sankar } while (blocks_todo > 0); 412272cc70bSAndy Fleming 413272cc70bSAndy Fleming return blkcnt; 414272cc70bSAndy Fleming } 415272cc70bSAndy Fleming 416272cc70bSAndy Fleming int mmc_go_idle(struct mmc* mmc) 417272cc70bSAndy Fleming { 418272cc70bSAndy Fleming struct mmc_cmd cmd; 419272cc70bSAndy Fleming int err; 420272cc70bSAndy Fleming 421272cc70bSAndy Fleming udelay(1000); 422272cc70bSAndy Fleming 423272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; 424272cc70bSAndy Fleming cmd.cmdarg = 0; 425272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_NONE; 426272cc70bSAndy Fleming cmd.flags = 0; 427272cc70bSAndy Fleming 428272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 429272cc70bSAndy Fleming 430272cc70bSAndy Fleming if (err) 431272cc70bSAndy Fleming return err; 432272cc70bSAndy Fleming 433272cc70bSAndy Fleming udelay(2000); 434272cc70bSAndy Fleming 435272cc70bSAndy Fleming return 0; 436272cc70bSAndy Fleming } 437272cc70bSAndy Fleming 438272cc70bSAndy Fleming int 439272cc70bSAndy Fleming sd_send_op_cond(struct mmc *mmc) 440272cc70bSAndy Fleming { 441272cc70bSAndy Fleming int timeout = 1000; 442272cc70bSAndy Fleming int err; 443272cc70bSAndy Fleming struct mmc_cmd cmd; 444272cc70bSAndy Fleming 445272cc70bSAndy Fleming do { 446272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 447272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 448272cc70bSAndy Fleming cmd.cmdarg = 0; 449272cc70bSAndy Fleming cmd.flags = 0; 450272cc70bSAndy Fleming 451272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 452272cc70bSAndy Fleming 453272cc70bSAndy Fleming if (err) 454272cc70bSAndy Fleming return err; 455272cc70bSAndy Fleming 456272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; 457272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R3; 458250de12bSStefano Babic 459250de12bSStefano Babic /* 460250de12bSStefano Babic * Most cards do not answer if some reserved bits 461250de12bSStefano Babic * in the ocr are set. However, Some controller 462250de12bSStefano Babic * can set bit 7 (reserved for low voltages), but 463250de12bSStefano Babic * how to manage low voltages SD card is not yet 464250de12bSStefano Babic * specified. 465250de12bSStefano Babic */ 466d52ebf10SThomas Chou cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 : 467d52ebf10SThomas Chou (mmc->voltages & 0xff8000); 468272cc70bSAndy Fleming 469272cc70bSAndy Fleming if (mmc->version == SD_VERSION_2) 470272cc70bSAndy Fleming cmd.cmdarg |= OCR_HCS; 471272cc70bSAndy Fleming 472272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 473272cc70bSAndy Fleming 474272cc70bSAndy Fleming if (err) 475272cc70bSAndy Fleming return err; 476272cc70bSAndy Fleming 477272cc70bSAndy Fleming udelay(1000); 478272cc70bSAndy Fleming } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--); 479272cc70bSAndy Fleming 480272cc70bSAndy Fleming if (timeout <= 0) 481272cc70bSAndy Fleming return UNUSABLE_ERR; 482272cc70bSAndy Fleming 483272cc70bSAndy Fleming if (mmc->version != SD_VERSION_2) 484272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 485272cc70bSAndy Fleming 486d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 487d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 488d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 489d52ebf10SThomas Chou cmd.cmdarg = 0; 490d52ebf10SThomas Chou cmd.flags = 0; 491d52ebf10SThomas Chou 492d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 493d52ebf10SThomas Chou 494d52ebf10SThomas Chou if (err) 495d52ebf10SThomas Chou return err; 496d52ebf10SThomas Chou } 497d52ebf10SThomas Chou 498998be3ddSRabin Vincent mmc->ocr = cmd.response[0]; 499272cc70bSAndy Fleming 500272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 501272cc70bSAndy Fleming mmc->rca = 0; 502272cc70bSAndy Fleming 503272cc70bSAndy Fleming return 0; 504272cc70bSAndy Fleming } 505272cc70bSAndy Fleming 506272cc70bSAndy Fleming int mmc_send_op_cond(struct mmc *mmc) 507272cc70bSAndy Fleming { 50831cacbabSRaffaele Recalcati int timeout = 10000; 509272cc70bSAndy Fleming struct mmc_cmd cmd; 510272cc70bSAndy Fleming int err; 511272cc70bSAndy Fleming 512272cc70bSAndy Fleming /* Some cards seem to need this */ 513272cc70bSAndy Fleming mmc_go_idle(mmc); 514272cc70bSAndy Fleming 51531cacbabSRaffaele Recalcati /* Asking to the card its capabilities */ 51631cacbabSRaffaele Recalcati cmd.cmdidx = MMC_CMD_SEND_OP_COND; 51731cacbabSRaffaele Recalcati cmd.resp_type = MMC_RSP_R3; 51831cacbabSRaffaele Recalcati cmd.cmdarg = 0; 51931cacbabSRaffaele Recalcati cmd.flags = 0; 52031cacbabSRaffaele Recalcati 52131cacbabSRaffaele Recalcati err = mmc_send_cmd(mmc, &cmd, NULL); 52231cacbabSRaffaele Recalcati 52331cacbabSRaffaele Recalcati if (err) 52431cacbabSRaffaele Recalcati return err; 52531cacbabSRaffaele Recalcati 52631cacbabSRaffaele Recalcati udelay(1000); 52731cacbabSRaffaele Recalcati 528272cc70bSAndy Fleming do { 529272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_OP_COND; 530272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R3; 53131cacbabSRaffaele Recalcati cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 : 53231cacbabSRaffaele Recalcati (mmc->voltages & 53331cacbabSRaffaele Recalcati (cmd.response[0] & OCR_VOLTAGE_MASK)) | 53431cacbabSRaffaele Recalcati (cmd.response[0] & OCR_ACCESS_MODE)); 535b1f1e821SŁukasz Majewski 536b1f1e821SŁukasz Majewski if (mmc->host_caps & MMC_MODE_HC) 537b1f1e821SŁukasz Majewski cmd.cmdarg |= OCR_HCS; 538b1f1e821SŁukasz Majewski 539272cc70bSAndy Fleming cmd.flags = 0; 540272cc70bSAndy Fleming 541272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 542272cc70bSAndy Fleming 543272cc70bSAndy Fleming if (err) 544272cc70bSAndy Fleming return err; 545272cc70bSAndy Fleming 546272cc70bSAndy Fleming udelay(1000); 547272cc70bSAndy Fleming } while (!(cmd.response[0] & OCR_BUSY) && timeout--); 548272cc70bSAndy Fleming 549272cc70bSAndy Fleming if (timeout <= 0) 550272cc70bSAndy Fleming return UNUSABLE_ERR; 551272cc70bSAndy Fleming 552d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 553d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 554d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 555d52ebf10SThomas Chou cmd.cmdarg = 0; 556d52ebf10SThomas Chou cmd.flags = 0; 557d52ebf10SThomas Chou 558d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 559d52ebf10SThomas Chou 560d52ebf10SThomas Chou if (err) 561d52ebf10SThomas Chou return err; 562d52ebf10SThomas Chou } 563d52ebf10SThomas Chou 564272cc70bSAndy Fleming mmc->version = MMC_VERSION_UNKNOWN; 565998be3ddSRabin Vincent mmc->ocr = cmd.response[0]; 566272cc70bSAndy Fleming 567272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 568272cc70bSAndy Fleming mmc->rca = 0; 569272cc70bSAndy Fleming 570272cc70bSAndy Fleming return 0; 571272cc70bSAndy Fleming } 572272cc70bSAndy Fleming 573272cc70bSAndy Fleming 574272cc70bSAndy Fleming int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd) 575272cc70bSAndy Fleming { 576272cc70bSAndy Fleming struct mmc_cmd cmd; 577272cc70bSAndy Fleming struct mmc_data data; 578272cc70bSAndy Fleming int err; 579272cc70bSAndy Fleming 580272cc70bSAndy Fleming /* Get the Card Status Register */ 581272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; 582272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 583272cc70bSAndy Fleming cmd.cmdarg = 0; 584272cc70bSAndy Fleming cmd.flags = 0; 585272cc70bSAndy Fleming 586272cc70bSAndy Fleming data.dest = ext_csd; 587272cc70bSAndy Fleming data.blocks = 1; 588272cc70bSAndy Fleming data.blocksize = 512; 589272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 590272cc70bSAndy Fleming 591272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 592272cc70bSAndy Fleming 593272cc70bSAndy Fleming return err; 594272cc70bSAndy Fleming } 595272cc70bSAndy Fleming 596272cc70bSAndy Fleming 597272cc70bSAndy Fleming int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) 598272cc70bSAndy Fleming { 599272cc70bSAndy Fleming struct mmc_cmd cmd; 6005d4fc8d9SRaffaele Recalcati int timeout = 1000; 6015d4fc8d9SRaffaele Recalcati int ret; 602272cc70bSAndy Fleming 603272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SWITCH; 604272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 605272cc70bSAndy Fleming cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 606272cc70bSAndy Fleming (index << 16) | 607272cc70bSAndy Fleming (value << 8); 608272cc70bSAndy Fleming cmd.flags = 0; 609272cc70bSAndy Fleming 6105d4fc8d9SRaffaele Recalcati ret = mmc_send_cmd(mmc, &cmd, NULL); 6115d4fc8d9SRaffaele Recalcati 6125d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 6135d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 6145d4fc8d9SRaffaele Recalcati 6155d4fc8d9SRaffaele Recalcati return ret; 6165d4fc8d9SRaffaele Recalcati 617272cc70bSAndy Fleming } 618272cc70bSAndy Fleming 619272cc70bSAndy Fleming int mmc_change_freq(struct mmc *mmc) 620272cc70bSAndy Fleming { 621a1969923SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512); 622272cc70bSAndy Fleming char cardtype; 623272cc70bSAndy Fleming int err; 624272cc70bSAndy Fleming 625272cc70bSAndy Fleming mmc->card_caps = 0; 626272cc70bSAndy Fleming 627d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 628d52ebf10SThomas Chou return 0; 629d52ebf10SThomas Chou 630272cc70bSAndy Fleming /* Only version 4 supports high-speed */ 631272cc70bSAndy Fleming if (mmc->version < MMC_VERSION_4) 632272cc70bSAndy Fleming return 0; 633272cc70bSAndy Fleming 634272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 635272cc70bSAndy Fleming 636272cc70bSAndy Fleming if (err) 637272cc70bSAndy Fleming return err; 638272cc70bSAndy Fleming 6390560db18SLei Wen cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf; 640272cc70bSAndy Fleming 641272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); 642272cc70bSAndy Fleming 643272cc70bSAndy Fleming if (err) 644272cc70bSAndy Fleming return err; 645272cc70bSAndy Fleming 646272cc70bSAndy Fleming /* Now check to see that it worked */ 647272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 648272cc70bSAndy Fleming 649272cc70bSAndy Fleming if (err) 650272cc70bSAndy Fleming return err; 651272cc70bSAndy Fleming 652272cc70bSAndy Fleming /* No high-speed support */ 6530560db18SLei Wen if (!ext_csd[EXT_CSD_HS_TIMING]) 654272cc70bSAndy Fleming return 0; 655272cc70bSAndy Fleming 656272cc70bSAndy Fleming /* High Speed is set, there are two types: 52MHz and 26MHz */ 657272cc70bSAndy Fleming if (cardtype & MMC_HS_52MHZ) 658272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; 659272cc70bSAndy Fleming else 660272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 661272cc70bSAndy Fleming 662272cc70bSAndy Fleming return 0; 663272cc70bSAndy Fleming } 664272cc70bSAndy Fleming 665bc897b1dSLei Wen int mmc_switch_part(int dev_num, unsigned int part_num) 666bc897b1dSLei Wen { 667bc897b1dSLei Wen struct mmc *mmc = find_mmc_device(dev_num); 668bc897b1dSLei Wen 669bc897b1dSLei Wen if (!mmc) 670bc897b1dSLei Wen return -1; 671bc897b1dSLei Wen 672bc897b1dSLei Wen return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, 673bc897b1dSLei Wen (mmc->part_config & ~PART_ACCESS_MASK) 674bc897b1dSLei Wen | (part_num & PART_ACCESS_MASK)); 675bc897b1dSLei Wen } 676bc897b1dSLei Wen 677272cc70bSAndy Fleming int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) 678272cc70bSAndy Fleming { 679272cc70bSAndy Fleming struct mmc_cmd cmd; 680272cc70bSAndy Fleming struct mmc_data data; 681272cc70bSAndy Fleming 682272cc70bSAndy Fleming /* Switch the frequency */ 683272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SWITCH_FUNC; 684272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 685272cc70bSAndy Fleming cmd.cmdarg = (mode << 31) | 0xffffff; 686272cc70bSAndy Fleming cmd.cmdarg &= ~(0xf << (group * 4)); 687272cc70bSAndy Fleming cmd.cmdarg |= value << (group * 4); 688272cc70bSAndy Fleming cmd.flags = 0; 689272cc70bSAndy Fleming 690272cc70bSAndy Fleming data.dest = (char *)resp; 691272cc70bSAndy Fleming data.blocksize = 64; 692272cc70bSAndy Fleming data.blocks = 1; 693272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 694272cc70bSAndy Fleming 695272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, &data); 696272cc70bSAndy Fleming } 697272cc70bSAndy Fleming 698272cc70bSAndy Fleming 699272cc70bSAndy Fleming int sd_change_freq(struct mmc *mmc) 700272cc70bSAndy Fleming { 701272cc70bSAndy Fleming int err; 702272cc70bSAndy Fleming struct mmc_cmd cmd; 703f781dd38SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2); 704f781dd38SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16); 705272cc70bSAndy Fleming struct mmc_data data; 706272cc70bSAndy Fleming int timeout; 707272cc70bSAndy Fleming 708272cc70bSAndy Fleming mmc->card_caps = 0; 709272cc70bSAndy Fleming 710d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 711d52ebf10SThomas Chou return 0; 712d52ebf10SThomas Chou 713272cc70bSAndy Fleming /* Read the SCR to find out if this card supports higher speeds */ 714272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 715272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 716272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 717272cc70bSAndy Fleming cmd.flags = 0; 718272cc70bSAndy Fleming 719272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 720272cc70bSAndy Fleming 721272cc70bSAndy Fleming if (err) 722272cc70bSAndy Fleming return err; 723272cc70bSAndy Fleming 724272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_SCR; 725272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 726272cc70bSAndy Fleming cmd.cmdarg = 0; 727272cc70bSAndy Fleming cmd.flags = 0; 728272cc70bSAndy Fleming 729272cc70bSAndy Fleming timeout = 3; 730272cc70bSAndy Fleming 731272cc70bSAndy Fleming retry_scr: 732f781dd38SAnton staaf data.dest = (char *)scr; 733272cc70bSAndy Fleming data.blocksize = 8; 734272cc70bSAndy Fleming data.blocks = 1; 735272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 736272cc70bSAndy Fleming 737272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 738272cc70bSAndy Fleming 739272cc70bSAndy Fleming if (err) { 740272cc70bSAndy Fleming if (timeout--) 741272cc70bSAndy Fleming goto retry_scr; 742272cc70bSAndy Fleming 743272cc70bSAndy Fleming return err; 744272cc70bSAndy Fleming } 745272cc70bSAndy Fleming 7464e3d89baSYauhen Kharuzhy mmc->scr[0] = __be32_to_cpu(scr[0]); 7474e3d89baSYauhen Kharuzhy mmc->scr[1] = __be32_to_cpu(scr[1]); 748272cc70bSAndy Fleming 749272cc70bSAndy Fleming switch ((mmc->scr[0] >> 24) & 0xf) { 750272cc70bSAndy Fleming case 0: 751272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 752272cc70bSAndy Fleming break; 753272cc70bSAndy Fleming case 1: 754272cc70bSAndy Fleming mmc->version = SD_VERSION_1_10; 755272cc70bSAndy Fleming break; 756272cc70bSAndy Fleming case 2: 757272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 758272cc70bSAndy Fleming break; 759272cc70bSAndy Fleming default: 760272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 761272cc70bSAndy Fleming break; 762272cc70bSAndy Fleming } 763272cc70bSAndy Fleming 764b44c7083SAlagu Sankar if (mmc->scr[0] & SD_DATA_4BIT) 765b44c7083SAlagu Sankar mmc->card_caps |= MMC_MODE_4BIT; 766b44c7083SAlagu Sankar 767272cc70bSAndy Fleming /* Version 1.0 doesn't support switching */ 768272cc70bSAndy Fleming if (mmc->version == SD_VERSION_1_0) 769272cc70bSAndy Fleming return 0; 770272cc70bSAndy Fleming 771272cc70bSAndy Fleming timeout = 4; 772272cc70bSAndy Fleming while (timeout--) { 773272cc70bSAndy Fleming err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, 774f781dd38SAnton staaf (u8 *)switch_status); 775272cc70bSAndy Fleming 776272cc70bSAndy Fleming if (err) 777272cc70bSAndy Fleming return err; 778272cc70bSAndy Fleming 779272cc70bSAndy Fleming /* The high-speed function is busy. Try again */ 7804e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) 781272cc70bSAndy Fleming break; 782272cc70bSAndy Fleming } 783272cc70bSAndy Fleming 784272cc70bSAndy Fleming /* If high-speed isn't supported, we return */ 7854e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) 786272cc70bSAndy Fleming return 0; 787272cc70bSAndy Fleming 788f781dd38SAnton staaf err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status); 789272cc70bSAndy Fleming 790272cc70bSAndy Fleming if (err) 791272cc70bSAndy Fleming return err; 792272cc70bSAndy Fleming 7934e3d89baSYauhen Kharuzhy if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) 794272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 795272cc70bSAndy Fleming 796272cc70bSAndy Fleming return 0; 797272cc70bSAndy Fleming } 798272cc70bSAndy Fleming 799272cc70bSAndy Fleming /* frequency bases */ 800272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */ 8015f837c2cSMike Frysinger static const int fbase[] = { 802272cc70bSAndy Fleming 10000, 803272cc70bSAndy Fleming 100000, 804272cc70bSAndy Fleming 1000000, 805272cc70bSAndy Fleming 10000000, 806272cc70bSAndy Fleming }; 807272cc70bSAndy Fleming 808272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice 809272cc70bSAndy Fleming * to platforms without floating point. 810272cc70bSAndy Fleming */ 8115f837c2cSMike Frysinger static const int multipliers[] = { 812272cc70bSAndy Fleming 0, /* reserved */ 813272cc70bSAndy Fleming 10, 814272cc70bSAndy Fleming 12, 815272cc70bSAndy Fleming 13, 816272cc70bSAndy Fleming 15, 817272cc70bSAndy Fleming 20, 818272cc70bSAndy Fleming 25, 819272cc70bSAndy Fleming 30, 820272cc70bSAndy Fleming 35, 821272cc70bSAndy Fleming 40, 822272cc70bSAndy Fleming 45, 823272cc70bSAndy Fleming 50, 824272cc70bSAndy Fleming 55, 825272cc70bSAndy Fleming 60, 826272cc70bSAndy Fleming 70, 827272cc70bSAndy Fleming 80, 828272cc70bSAndy Fleming }; 829272cc70bSAndy Fleming 830272cc70bSAndy Fleming void mmc_set_ios(struct mmc *mmc) 831272cc70bSAndy Fleming { 832272cc70bSAndy Fleming mmc->set_ios(mmc); 833272cc70bSAndy Fleming } 834272cc70bSAndy Fleming 835272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock) 836272cc70bSAndy Fleming { 837272cc70bSAndy Fleming if (clock > mmc->f_max) 838272cc70bSAndy Fleming clock = mmc->f_max; 839272cc70bSAndy Fleming 840272cc70bSAndy Fleming if (clock < mmc->f_min) 841272cc70bSAndy Fleming clock = mmc->f_min; 842272cc70bSAndy Fleming 843272cc70bSAndy Fleming mmc->clock = clock; 844272cc70bSAndy Fleming 845272cc70bSAndy Fleming mmc_set_ios(mmc); 846272cc70bSAndy Fleming } 847272cc70bSAndy Fleming 848272cc70bSAndy Fleming void mmc_set_bus_width(struct mmc *mmc, uint width) 849272cc70bSAndy Fleming { 850272cc70bSAndy Fleming mmc->bus_width = width; 851272cc70bSAndy Fleming 852272cc70bSAndy Fleming mmc_set_ios(mmc); 853272cc70bSAndy Fleming } 854272cc70bSAndy Fleming 855272cc70bSAndy Fleming int mmc_startup(struct mmc *mmc) 856272cc70bSAndy Fleming { 8574137894eSLei Wen int err, width; 858272cc70bSAndy Fleming uint mult, freq; 859639b7827SYoshihiro Shimoda u64 cmult, csize, capacity; 860272cc70bSAndy Fleming struct mmc_cmd cmd; 861a1969923SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512); 8624137894eSLei Wen ALLOC_CACHE_ALIGN_BUFFER(char, test_csd, 512); 8635d4fc8d9SRaffaele Recalcati int timeout = 1000; 864272cc70bSAndy Fleming 865d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON 866d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ 867d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF; 868d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R1; 869d52ebf10SThomas Chou cmd.cmdarg = 1; 870d52ebf10SThomas Chou cmd.flags = 0; 871d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 872d52ebf10SThomas Chou 873d52ebf10SThomas Chou if (err) 874d52ebf10SThomas Chou return err; 875d52ebf10SThomas Chou } 876d52ebf10SThomas Chou #endif 877d52ebf10SThomas Chou 878272cc70bSAndy Fleming /* Put the Card in Identify Mode */ 879d52ebf10SThomas Chou cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID : 880d52ebf10SThomas Chou MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */ 881272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 882272cc70bSAndy Fleming cmd.cmdarg = 0; 883272cc70bSAndy Fleming cmd.flags = 0; 884272cc70bSAndy Fleming 885272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 886272cc70bSAndy Fleming 887272cc70bSAndy Fleming if (err) 888272cc70bSAndy Fleming return err; 889272cc70bSAndy Fleming 890272cc70bSAndy Fleming memcpy(mmc->cid, cmd.response, 16); 891272cc70bSAndy Fleming 892272cc70bSAndy Fleming /* 893272cc70bSAndy Fleming * For MMC cards, set the Relative Address. 894272cc70bSAndy Fleming * For SD cards, get the Relatvie Address. 895272cc70bSAndy Fleming * This also puts the cards into Standby State 896272cc70bSAndy Fleming */ 897d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 898272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; 899272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 900272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R6; 901272cc70bSAndy Fleming cmd.flags = 0; 902272cc70bSAndy Fleming 903272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 904272cc70bSAndy Fleming 905272cc70bSAndy Fleming if (err) 906272cc70bSAndy Fleming return err; 907272cc70bSAndy Fleming 908272cc70bSAndy Fleming if (IS_SD(mmc)) 909998be3ddSRabin Vincent mmc->rca = (cmd.response[0] >> 16) & 0xffff; 910d52ebf10SThomas Chou } 911272cc70bSAndy Fleming 912272cc70bSAndy Fleming /* Get the Card-Specific Data */ 913272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_CSD; 914272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 915272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 916272cc70bSAndy Fleming cmd.flags = 0; 917272cc70bSAndy Fleming 918272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 919272cc70bSAndy Fleming 9205d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 9215d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 9225d4fc8d9SRaffaele Recalcati 923272cc70bSAndy Fleming if (err) 924272cc70bSAndy Fleming return err; 925272cc70bSAndy Fleming 926998be3ddSRabin Vincent mmc->csd[0] = cmd.response[0]; 927998be3ddSRabin Vincent mmc->csd[1] = cmd.response[1]; 928998be3ddSRabin Vincent mmc->csd[2] = cmd.response[2]; 929998be3ddSRabin Vincent mmc->csd[3] = cmd.response[3]; 930272cc70bSAndy Fleming 931272cc70bSAndy Fleming if (mmc->version == MMC_VERSION_UNKNOWN) { 9320b453ffeSRabin Vincent int version = (cmd.response[0] >> 26) & 0xf; 933272cc70bSAndy Fleming 934272cc70bSAndy Fleming switch (version) { 935272cc70bSAndy Fleming case 0: 936272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 937272cc70bSAndy Fleming break; 938272cc70bSAndy Fleming case 1: 939272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_4; 940272cc70bSAndy Fleming break; 941272cc70bSAndy Fleming case 2: 942272cc70bSAndy Fleming mmc->version = MMC_VERSION_2_2; 943272cc70bSAndy Fleming break; 944272cc70bSAndy Fleming case 3: 945272cc70bSAndy Fleming mmc->version = MMC_VERSION_3; 946272cc70bSAndy Fleming break; 947272cc70bSAndy Fleming case 4: 948272cc70bSAndy Fleming mmc->version = MMC_VERSION_4; 949272cc70bSAndy Fleming break; 950272cc70bSAndy Fleming default: 951272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 952272cc70bSAndy Fleming break; 953272cc70bSAndy Fleming } 954272cc70bSAndy Fleming } 955272cc70bSAndy Fleming 956272cc70bSAndy Fleming /* divide frequency by 10, since the mults are 10x bigger */ 9570b453ffeSRabin Vincent freq = fbase[(cmd.response[0] & 0x7)]; 9580b453ffeSRabin Vincent mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; 959272cc70bSAndy Fleming 960272cc70bSAndy Fleming mmc->tran_speed = freq * mult; 961272cc70bSAndy Fleming 962998be3ddSRabin Vincent mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); 963272cc70bSAndy Fleming 964272cc70bSAndy Fleming if (IS_SD(mmc)) 965272cc70bSAndy Fleming mmc->write_bl_len = mmc->read_bl_len; 966272cc70bSAndy Fleming else 967998be3ddSRabin Vincent mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); 968272cc70bSAndy Fleming 969272cc70bSAndy Fleming if (mmc->high_capacity) { 970272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3f) << 16 971272cc70bSAndy Fleming | (mmc->csd[2] & 0xffff0000) >> 16; 972272cc70bSAndy Fleming cmult = 8; 973272cc70bSAndy Fleming } else { 974272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3ff) << 2 975272cc70bSAndy Fleming | (mmc->csd[2] & 0xc0000000) >> 30; 976272cc70bSAndy Fleming cmult = (mmc->csd[2] & 0x00038000) >> 15; 977272cc70bSAndy Fleming } 978272cc70bSAndy Fleming 979272cc70bSAndy Fleming mmc->capacity = (csize + 1) << (cmult + 2); 980272cc70bSAndy Fleming mmc->capacity *= mmc->read_bl_len; 981272cc70bSAndy Fleming 982272cc70bSAndy Fleming if (mmc->read_bl_len > 512) 983272cc70bSAndy Fleming mmc->read_bl_len = 512; 984272cc70bSAndy Fleming 985272cc70bSAndy Fleming if (mmc->write_bl_len > 512) 986272cc70bSAndy Fleming mmc->write_bl_len = 512; 987272cc70bSAndy Fleming 988272cc70bSAndy Fleming /* Select the card, and put it into Transfer Mode */ 989d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 990272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SELECT_CARD; 991*fe8f7066SAjay Bhargav cmd.resp_type = MMC_RSP_R1; 992272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 993272cc70bSAndy Fleming cmd.flags = 0; 994272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 995272cc70bSAndy Fleming 996272cc70bSAndy Fleming if (err) 997272cc70bSAndy Fleming return err; 998d52ebf10SThomas Chou } 999272cc70bSAndy Fleming 1000e6f99a56SLei Wen /* 1001e6f99a56SLei Wen * For SD, its erase group is always one sector 1002e6f99a56SLei Wen */ 1003e6f99a56SLei Wen mmc->erase_grp_size = 1; 1004bc897b1dSLei Wen mmc->part_config = MMCPART_NOAVAILABLE; 1005d23e2c09SSukumar Ghorai if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) { 1006d23e2c09SSukumar Ghorai /* check ext_csd version and capacity */ 1007d23e2c09SSukumar Ghorai err = mmc_send_ext_csd(mmc, ext_csd); 10080560db18SLei Wen if (!err & (ext_csd[EXT_CSD_REV] >= 2)) { 1009639b7827SYoshihiro Shimoda /* 1010639b7827SYoshihiro Shimoda * According to the JEDEC Standard, the value of 1011639b7827SYoshihiro Shimoda * ext_csd's capacity is valid if the value is more 1012639b7827SYoshihiro Shimoda * than 2GB 1013639b7827SYoshihiro Shimoda */ 10140560db18SLei Wen capacity = ext_csd[EXT_CSD_SEC_CNT] << 0 10150560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 10160560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 10170560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; 1018639b7827SYoshihiro Shimoda capacity *= 512; 1019b1f1e821SŁukasz Majewski if ((capacity >> 20) > 2 * 1024) 1020639b7827SYoshihiro Shimoda mmc->capacity = capacity; 1021d23e2c09SSukumar Ghorai } 1022bc897b1dSLei Wen 1023e6f99a56SLei Wen /* 1024e6f99a56SLei Wen * Check whether GROUP_DEF is set, if yes, read out 1025e6f99a56SLei Wen * group size from ext_csd directly, or calculate 1026e6f99a56SLei Wen * the group size from the csd value. 1027e6f99a56SLei Wen */ 10280560db18SLei Wen if (ext_csd[EXT_CSD_ERASE_GROUP_DEF]) 10290560db18SLei Wen mmc->erase_grp_size = 10300560db18SLei Wen ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024; 1031e6f99a56SLei Wen else { 1032e6f99a56SLei Wen int erase_gsz, erase_gmul; 1033e6f99a56SLei Wen erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10; 1034e6f99a56SLei Wen erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5; 1035e6f99a56SLei Wen mmc->erase_grp_size = (erase_gsz + 1) 1036e6f99a56SLei Wen * (erase_gmul + 1); 1037e6f99a56SLei Wen } 1038e6f99a56SLei Wen 1039bc897b1dSLei Wen /* store the partition info of emmc */ 10400560db18SLei Wen if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) 10410560db18SLei Wen mmc->part_config = ext_csd[EXT_CSD_PART_CONF]; 1042d23e2c09SSukumar Ghorai } 1043d23e2c09SSukumar Ghorai 1044272cc70bSAndy Fleming if (IS_SD(mmc)) 1045272cc70bSAndy Fleming err = sd_change_freq(mmc); 1046272cc70bSAndy Fleming else 1047272cc70bSAndy Fleming err = mmc_change_freq(mmc); 1048272cc70bSAndy Fleming 1049272cc70bSAndy Fleming if (err) 1050272cc70bSAndy Fleming return err; 1051272cc70bSAndy Fleming 1052272cc70bSAndy Fleming /* Restrict card's capabilities by what the host can do */ 1053272cc70bSAndy Fleming mmc->card_caps &= mmc->host_caps; 1054272cc70bSAndy Fleming 1055272cc70bSAndy Fleming if (IS_SD(mmc)) { 1056272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_4BIT) { 1057272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 1058272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 1059272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1060272cc70bSAndy Fleming cmd.flags = 0; 1061272cc70bSAndy Fleming 1062272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1063272cc70bSAndy Fleming if (err) 1064272cc70bSAndy Fleming return err; 1065272cc70bSAndy Fleming 1066272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; 1067272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 1068272cc70bSAndy Fleming cmd.cmdarg = 2; 1069272cc70bSAndy Fleming cmd.flags = 0; 1070272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1071272cc70bSAndy Fleming if (err) 1072272cc70bSAndy Fleming return err; 1073272cc70bSAndy Fleming 1074272cc70bSAndy Fleming mmc_set_bus_width(mmc, 4); 1075272cc70bSAndy Fleming } 1076272cc70bSAndy Fleming 1077272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) 1078272cc70bSAndy Fleming mmc_set_clock(mmc, 50000000); 1079272cc70bSAndy Fleming else 1080272cc70bSAndy Fleming mmc_set_clock(mmc, 25000000); 1081272cc70bSAndy Fleming } else { 10824137894eSLei Wen for (width = EXT_CSD_BUS_WIDTH_8; width >= 0; width--) { 1083272cc70bSAndy Fleming /* Set the card to use 4 bit*/ 1084272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 10854137894eSLei Wen EXT_CSD_BUS_WIDTH, width); 1086272cc70bSAndy Fleming 1087272cc70bSAndy Fleming if (err) 10884137894eSLei Wen continue; 1089272cc70bSAndy Fleming 10904137894eSLei Wen if (!width) { 10914137894eSLei Wen mmc_set_bus_width(mmc, 1); 10924137894eSLei Wen break; 10934137894eSLei Wen } else 10944137894eSLei Wen mmc_set_bus_width(mmc, 4 * width); 1095272cc70bSAndy Fleming 10964137894eSLei Wen err = mmc_send_ext_csd(mmc, test_csd); 10974137894eSLei Wen if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \ 10984137894eSLei Wen == test_csd[EXT_CSD_PARTITIONING_SUPPORT] 10994137894eSLei Wen && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \ 11004137894eSLei Wen == test_csd[EXT_CSD_ERASE_GROUP_DEF] \ 11014137894eSLei Wen && ext_csd[EXT_CSD_REV] \ 11024137894eSLei Wen == test_csd[EXT_CSD_REV] 11034137894eSLei Wen && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \ 11044137894eSLei Wen == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] 11054137894eSLei Wen && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \ 11064137894eSLei Wen &test_csd[EXT_CSD_SEC_CNT], 4) == 0) { 1107272cc70bSAndy Fleming 11084137894eSLei Wen mmc->card_caps |= width; 11094137894eSLei Wen break; 11104137894eSLei Wen } 1111272cc70bSAndy Fleming } 1112272cc70bSAndy Fleming 1113272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) { 1114272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS_52MHz) 1115272cc70bSAndy Fleming mmc_set_clock(mmc, 52000000); 1116272cc70bSAndy Fleming else 1117272cc70bSAndy Fleming mmc_set_clock(mmc, 26000000); 1118272cc70bSAndy Fleming } else 1119272cc70bSAndy Fleming mmc_set_clock(mmc, 20000000); 1120272cc70bSAndy Fleming } 1121272cc70bSAndy Fleming 1122272cc70bSAndy Fleming /* fill in device description */ 1123272cc70bSAndy Fleming mmc->block_dev.lun = 0; 1124272cc70bSAndy Fleming mmc->block_dev.type = 0; 1125272cc70bSAndy Fleming mmc->block_dev.blksz = mmc->read_bl_len; 11269b1f942cSRabin Vincent mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); 11270b453ffeSRabin Vincent sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8, 11280b453ffeSRabin Vincent (mmc->cid[2] << 8) | (mmc->cid[3] >> 24)); 11290b453ffeSRabin Vincent sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff, 11300b453ffeSRabin Vincent (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, 11310b453ffeSRabin Vincent (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); 11320b453ffeSRabin Vincent sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28, 11330b453ffeSRabin Vincent (mmc->cid[2] >> 24) & 0xf); 1134272cc70bSAndy Fleming init_part(&mmc->block_dev); 1135272cc70bSAndy Fleming 1136272cc70bSAndy Fleming return 0; 1137272cc70bSAndy Fleming } 1138272cc70bSAndy Fleming 1139272cc70bSAndy Fleming int mmc_send_if_cond(struct mmc *mmc) 1140272cc70bSAndy Fleming { 1141272cc70bSAndy Fleming struct mmc_cmd cmd; 1142272cc70bSAndy Fleming int err; 1143272cc70bSAndy Fleming 1144272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_IF_COND; 1145272cc70bSAndy Fleming /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ 1146272cc70bSAndy Fleming cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa; 1147272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R7; 1148272cc70bSAndy Fleming cmd.flags = 0; 1149272cc70bSAndy Fleming 1150272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1151272cc70bSAndy Fleming 1152272cc70bSAndy Fleming if (err) 1153272cc70bSAndy Fleming return err; 1154272cc70bSAndy Fleming 1155998be3ddSRabin Vincent if ((cmd.response[0] & 0xff) != 0xaa) 1156272cc70bSAndy Fleming return UNUSABLE_ERR; 1157272cc70bSAndy Fleming else 1158272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 1159272cc70bSAndy Fleming 1160272cc70bSAndy Fleming return 0; 1161272cc70bSAndy Fleming } 1162272cc70bSAndy Fleming 1163272cc70bSAndy Fleming int mmc_register(struct mmc *mmc) 1164272cc70bSAndy Fleming { 1165272cc70bSAndy Fleming /* Setup the universal parts of the block interface just once */ 1166272cc70bSAndy Fleming mmc->block_dev.if_type = IF_TYPE_MMC; 1167272cc70bSAndy Fleming mmc->block_dev.dev = cur_dev_num++; 1168272cc70bSAndy Fleming mmc->block_dev.removable = 1; 1169272cc70bSAndy Fleming mmc->block_dev.block_read = mmc_bread; 1170272cc70bSAndy Fleming mmc->block_dev.block_write = mmc_bwrite; 1171e6f99a56SLei Wen mmc->block_dev.block_erase = mmc_berase; 11728feafcc4SJohn Rigby if (!mmc->b_max) 11738feafcc4SJohn Rigby mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; 1174272cc70bSAndy Fleming 1175272cc70bSAndy Fleming INIT_LIST_HEAD (&mmc->link); 1176272cc70bSAndy Fleming 1177272cc70bSAndy Fleming list_add_tail (&mmc->link, &mmc_devices); 1178272cc70bSAndy Fleming 1179272cc70bSAndy Fleming return 0; 1180272cc70bSAndy Fleming } 1181272cc70bSAndy Fleming 1182df3fc526SMatthew McClintock #ifdef CONFIG_PARTITIONS 1183272cc70bSAndy Fleming block_dev_desc_t *mmc_get_dev(int dev) 1184272cc70bSAndy Fleming { 1185272cc70bSAndy Fleming struct mmc *mmc = find_mmc_device(dev); 1186272cc70bSAndy Fleming 1187e85649c7SRabin Vincent return mmc ? &mmc->block_dev : NULL; 1188272cc70bSAndy Fleming } 1189df3fc526SMatthew McClintock #endif 1190272cc70bSAndy Fleming 1191272cc70bSAndy Fleming int mmc_init(struct mmc *mmc) 1192272cc70bSAndy Fleming { 119302f3029fSLei Wen int err, retry = 3; 1194272cc70bSAndy Fleming 1195bc897b1dSLei Wen if (mmc->has_init) 1196bc897b1dSLei Wen return 0; 1197bc897b1dSLei Wen 1198272cc70bSAndy Fleming err = mmc->init(mmc); 1199272cc70bSAndy Fleming 1200272cc70bSAndy Fleming if (err) 1201272cc70bSAndy Fleming return err; 1202272cc70bSAndy Fleming 1203b86b85e2SIlya Yanok mmc_set_bus_width(mmc, 1); 1204b86b85e2SIlya Yanok mmc_set_clock(mmc, 1); 1205b86b85e2SIlya Yanok 1206272cc70bSAndy Fleming /* Reset the Card */ 1207272cc70bSAndy Fleming err = mmc_go_idle(mmc); 1208272cc70bSAndy Fleming 1209272cc70bSAndy Fleming if (err) 1210272cc70bSAndy Fleming return err; 1211272cc70bSAndy Fleming 1212bc897b1dSLei Wen /* The internal partition reset to user partition(0) at every CMD0*/ 1213bc897b1dSLei Wen mmc->part_num = 0; 1214bc897b1dSLei Wen 1215272cc70bSAndy Fleming /* Test for SD version 2 */ 121602f3029fSLei Wen /* 121702f3029fSLei Wen * retry here for 3 times, as for some controller it has dynamic 121802f3029fSLei Wen * clock gating, and only toggle out clk when the first cmd0 send 121902f3029fSLei Wen * out, while some card strictly obey the 74 clocks rule, the interval 122002f3029fSLei Wen * may not be sufficient between the cmd0 and this cmd8, retry to 122102f3029fSLei Wen * fulfil the clock requirement 122202f3029fSLei Wen */ 122302f3029fSLei Wen do { 1224272cc70bSAndy Fleming err = mmc_send_if_cond(mmc); 122502f3029fSLei Wen } while (--retry > 0 && err); 122602f3029fSLei Wen 122702f3029fSLei Wen if (err) 122802f3029fSLei Wen return err; 1229272cc70bSAndy Fleming 1230272cc70bSAndy Fleming /* Now try to get the SD card's operating condition */ 1231272cc70bSAndy Fleming err = sd_send_op_cond(mmc); 1232272cc70bSAndy Fleming 1233272cc70bSAndy Fleming /* If the command timed out, we check for an MMC card */ 1234272cc70bSAndy Fleming if (err == TIMEOUT) { 1235272cc70bSAndy Fleming err = mmc_send_op_cond(mmc); 1236272cc70bSAndy Fleming 1237272cc70bSAndy Fleming if (err) { 1238272cc70bSAndy Fleming printf("Card did not respond to voltage select!\n"); 1239272cc70bSAndy Fleming return UNUSABLE_ERR; 1240272cc70bSAndy Fleming } 1241272cc70bSAndy Fleming } 1242272cc70bSAndy Fleming 1243bc897b1dSLei Wen err = mmc_startup(mmc); 1244bc897b1dSLei Wen if (err) 1245bc897b1dSLei Wen mmc->has_init = 0; 1246bc897b1dSLei Wen else 1247bc897b1dSLei Wen mmc->has_init = 1; 1248bc897b1dSLei Wen return err; 1249272cc70bSAndy Fleming } 1250272cc70bSAndy Fleming 1251272cc70bSAndy Fleming /* 1252272cc70bSAndy Fleming * CPU and board-specific MMC initializations. Aliased function 1253272cc70bSAndy Fleming * signals caller to move on 1254272cc70bSAndy Fleming */ 1255272cc70bSAndy Fleming static int __def_mmc_init(bd_t *bis) 1256272cc70bSAndy Fleming { 1257272cc70bSAndy Fleming return -1; 1258272cc70bSAndy Fleming } 1259272cc70bSAndy Fleming 1260f9a109b3SPeter Tyser int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); 1261f9a109b3SPeter Tyser int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); 1262272cc70bSAndy Fleming 1263272cc70bSAndy Fleming void print_mmc_devices(char separator) 1264272cc70bSAndy Fleming { 1265272cc70bSAndy Fleming struct mmc *m; 1266272cc70bSAndy Fleming struct list_head *entry; 1267272cc70bSAndy Fleming 1268272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 1269272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 1270272cc70bSAndy Fleming 1271272cc70bSAndy Fleming printf("%s: %d", m->name, m->block_dev.dev); 1272272cc70bSAndy Fleming 1273272cc70bSAndy Fleming if (entry->next != &mmc_devices) 1274272cc70bSAndy Fleming printf("%c ", separator); 1275272cc70bSAndy Fleming } 1276272cc70bSAndy Fleming 1277272cc70bSAndy Fleming printf("\n"); 1278272cc70bSAndy Fleming } 1279272cc70bSAndy Fleming 1280ea6ebe21SLei Wen int get_mmc_num(void) 1281ea6ebe21SLei Wen { 1282ea6ebe21SLei Wen return cur_dev_num; 1283ea6ebe21SLei Wen } 1284ea6ebe21SLei Wen 1285272cc70bSAndy Fleming int mmc_initialize(bd_t *bis) 1286272cc70bSAndy Fleming { 1287272cc70bSAndy Fleming INIT_LIST_HEAD (&mmc_devices); 1288272cc70bSAndy Fleming cur_dev_num = 0; 1289272cc70bSAndy Fleming 1290272cc70bSAndy Fleming if (board_mmc_init(bis) < 0) 1291272cc70bSAndy Fleming cpu_mmc_init(bis); 1292272cc70bSAndy Fleming 1293272cc70bSAndy Fleming print_mmc_devices(','); 1294272cc70bSAndy Fleming 1295272cc70bSAndy Fleming return 0; 1296272cc70bSAndy Fleming } 1297