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 43314284b1SThierry Reding int __board_mmc_getcd(struct mmc *mmc) { 4411fdade2SStefano Babic return -1; 4511fdade2SStefano Babic } 4611fdade2SStefano Babic 47314284b1SThierry Reding int board_mmc_getcd(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; 111*d617c426SJan Kloetzke int err, retries = 5; 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); 124*d617c426SJan Kloetzke if (!err) { 125*d617c426SJan Kloetzke if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) && 126*d617c426SJan Kloetzke (cmd.response[0] & MMC_STATUS_CURR_STATE) != 127*d617c426SJan Kloetzke MMC_STATE_PRG) 1285d4fc8d9SRaffaele Recalcati break; 129*d617c426SJan Kloetzke else if (cmd.response[0] & MMC_STATUS_MASK) { 130*d617c426SJan Kloetzke printf("Status Error: 0x%08X\n", 131*d617c426SJan Kloetzke cmd.response[0]); 132*d617c426SJan Kloetzke return COMM_ERR; 133*d617c426SJan Kloetzke } 134*d617c426SJan Kloetzke } else if (--retries < 0) 135*d617c426SJan Kloetzke return err; 1365d4fc8d9SRaffaele Recalcati 1375d4fc8d9SRaffaele Recalcati udelay(1000); 1385d4fc8d9SRaffaele Recalcati 1395d4fc8d9SRaffaele Recalcati } while (timeout--); 1405d4fc8d9SRaffaele Recalcati 1415db2fe3aSRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 1425db2fe3aSRaffaele Recalcati status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9; 1435db2fe3aSRaffaele Recalcati printf("CURR STATE:%d\n", status); 1445db2fe3aSRaffaele Recalcati #endif 1455d4fc8d9SRaffaele Recalcati if (!timeout) { 1465d4fc8d9SRaffaele Recalcati printf("Timeout waiting card ready\n"); 1475d4fc8d9SRaffaele Recalcati return TIMEOUT; 1485d4fc8d9SRaffaele Recalcati } 1495d4fc8d9SRaffaele Recalcati 1505d4fc8d9SRaffaele Recalcati return 0; 1515d4fc8d9SRaffaele Recalcati } 1525d4fc8d9SRaffaele Recalcati 153272cc70bSAndy Fleming int mmc_set_blocklen(struct mmc *mmc, int len) 154272cc70bSAndy Fleming { 155272cc70bSAndy Fleming struct mmc_cmd cmd; 156272cc70bSAndy Fleming 157272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; 158272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 159272cc70bSAndy Fleming cmd.cmdarg = len; 160272cc70bSAndy Fleming cmd.flags = 0; 161272cc70bSAndy Fleming 162272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, NULL); 163272cc70bSAndy Fleming } 164272cc70bSAndy Fleming 165272cc70bSAndy Fleming struct mmc *find_mmc_device(int dev_num) 166272cc70bSAndy Fleming { 167272cc70bSAndy Fleming struct mmc *m; 168272cc70bSAndy Fleming struct list_head *entry; 169272cc70bSAndy Fleming 170272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 171272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 172272cc70bSAndy Fleming 173272cc70bSAndy Fleming if (m->block_dev.dev == dev_num) 174272cc70bSAndy Fleming return m; 175272cc70bSAndy Fleming } 176272cc70bSAndy Fleming 177272cc70bSAndy Fleming printf("MMC Device %d not found\n", dev_num); 178272cc70bSAndy Fleming 179272cc70bSAndy Fleming return NULL; 180272cc70bSAndy Fleming } 181272cc70bSAndy Fleming 182e6f99a56SLei Wen static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt) 183e6f99a56SLei Wen { 184e6f99a56SLei Wen struct mmc_cmd cmd; 185e6f99a56SLei Wen ulong end; 186e6f99a56SLei Wen int err, start_cmd, end_cmd; 187e6f99a56SLei Wen 188e6f99a56SLei Wen if (mmc->high_capacity) 189e6f99a56SLei Wen end = start + blkcnt - 1; 190e6f99a56SLei Wen else { 191e6f99a56SLei Wen end = (start + blkcnt - 1) * mmc->write_bl_len; 192e6f99a56SLei Wen start *= mmc->write_bl_len; 193e6f99a56SLei Wen } 194e6f99a56SLei Wen 195e6f99a56SLei Wen if (IS_SD(mmc)) { 196e6f99a56SLei Wen start_cmd = SD_CMD_ERASE_WR_BLK_START; 197e6f99a56SLei Wen end_cmd = SD_CMD_ERASE_WR_BLK_END; 198e6f99a56SLei Wen } else { 199e6f99a56SLei Wen start_cmd = MMC_CMD_ERASE_GROUP_START; 200e6f99a56SLei Wen end_cmd = MMC_CMD_ERASE_GROUP_END; 201e6f99a56SLei Wen } 202e6f99a56SLei Wen 203e6f99a56SLei Wen cmd.cmdidx = start_cmd; 204e6f99a56SLei Wen cmd.cmdarg = start; 205e6f99a56SLei Wen cmd.resp_type = MMC_RSP_R1; 206e6f99a56SLei Wen cmd.flags = 0; 207e6f99a56SLei Wen 208e6f99a56SLei Wen err = mmc_send_cmd(mmc, &cmd, NULL); 209e6f99a56SLei Wen if (err) 210e6f99a56SLei Wen goto err_out; 211e6f99a56SLei Wen 212e6f99a56SLei Wen cmd.cmdidx = end_cmd; 213e6f99a56SLei Wen cmd.cmdarg = end; 214e6f99a56SLei Wen 215e6f99a56SLei Wen err = mmc_send_cmd(mmc, &cmd, NULL); 216e6f99a56SLei Wen if (err) 217e6f99a56SLei Wen goto err_out; 218e6f99a56SLei Wen 219e6f99a56SLei Wen cmd.cmdidx = MMC_CMD_ERASE; 220e6f99a56SLei Wen cmd.cmdarg = SECURE_ERASE; 221e6f99a56SLei Wen cmd.resp_type = MMC_RSP_R1b; 222e6f99a56SLei Wen 223e6f99a56SLei Wen err = mmc_send_cmd(mmc, &cmd, NULL); 224e6f99a56SLei Wen if (err) 225e6f99a56SLei Wen goto err_out; 226e6f99a56SLei Wen 227e6f99a56SLei Wen return 0; 228e6f99a56SLei Wen 229e6f99a56SLei Wen err_out: 230e6f99a56SLei Wen puts("mmc erase failed\n"); 231e6f99a56SLei Wen return err; 232e6f99a56SLei Wen } 233e6f99a56SLei Wen 234e6f99a56SLei Wen static unsigned long 235e6f99a56SLei Wen mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt) 236e6f99a56SLei Wen { 237e6f99a56SLei Wen int err = 0; 238e6f99a56SLei Wen struct mmc *mmc = find_mmc_device(dev_num); 239e6f99a56SLei Wen lbaint_t blk = 0, blk_r = 0; 240e6f99a56SLei Wen 241e6f99a56SLei Wen if (!mmc) 242e6f99a56SLei Wen return -1; 243e6f99a56SLei Wen 244e6f99a56SLei Wen if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size)) 245e6f99a56SLei Wen printf("\n\nCaution! Your devices Erase group is 0x%x\n" 246e6f99a56SLei Wen "The erase range would be change to 0x%lx~0x%lx\n\n", 247e6f99a56SLei Wen mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1), 248e6f99a56SLei Wen ((start + blkcnt + mmc->erase_grp_size) 249e6f99a56SLei Wen & ~(mmc->erase_grp_size - 1)) - 1); 250e6f99a56SLei Wen 251e6f99a56SLei Wen while (blk < blkcnt) { 252e6f99a56SLei Wen blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ? 253e6f99a56SLei Wen mmc->erase_grp_size : (blkcnt - blk); 254e6f99a56SLei Wen err = mmc_erase_t(mmc, start + blk, blk_r); 255e6f99a56SLei Wen if (err) 256e6f99a56SLei Wen break; 257e6f99a56SLei Wen 258e6f99a56SLei Wen blk += blk_r; 259e6f99a56SLei Wen } 260e6f99a56SLei Wen 261e6f99a56SLei Wen return blk; 262e6f99a56SLei Wen } 263e6f99a56SLei Wen 264272cc70bSAndy Fleming static ulong 2650158126eSLei Wen mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src) 266272cc70bSAndy Fleming { 267272cc70bSAndy Fleming struct mmc_cmd cmd; 268272cc70bSAndy Fleming struct mmc_data data; 2695d4fc8d9SRaffaele Recalcati int timeout = 1000; 270272cc70bSAndy Fleming 271d2bf29e3SLei Wen if ((start + blkcnt) > mmc->block_dev.lba) { 272def412b6SSteve Sakoman printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", 273d2bf29e3SLei Wen start + blkcnt, mmc->block_dev.lba); 274d2bf29e3SLei Wen return 0; 275d2bf29e3SLei Wen } 276272cc70bSAndy Fleming 277272cc70bSAndy Fleming if (blkcnt > 1) 278272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; 279272cc70bSAndy Fleming else 280272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; 281272cc70bSAndy Fleming 282272cc70bSAndy Fleming if (mmc->high_capacity) 283272cc70bSAndy Fleming cmd.cmdarg = start; 284272cc70bSAndy Fleming else 285def412b6SSteve Sakoman cmd.cmdarg = start * mmc->write_bl_len; 286272cc70bSAndy Fleming 287272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 288272cc70bSAndy Fleming cmd.flags = 0; 289272cc70bSAndy Fleming 290272cc70bSAndy Fleming data.src = src; 291272cc70bSAndy Fleming data.blocks = blkcnt; 292def412b6SSteve Sakoman data.blocksize = mmc->write_bl_len; 293272cc70bSAndy Fleming data.flags = MMC_DATA_WRITE; 294272cc70bSAndy Fleming 295def412b6SSteve Sakoman if (mmc_send_cmd(mmc, &cmd, &data)) { 296def412b6SSteve Sakoman printf("mmc write failed\n"); 297def412b6SSteve Sakoman return 0; 298272cc70bSAndy Fleming } 299272cc70bSAndy Fleming 300d52ebf10SThomas Chou /* SPI multiblock writes terminate using a special 301d52ebf10SThomas Chou * token, not a STOP_TRANSMISSION request. 302d52ebf10SThomas Chou */ 303d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc) && blkcnt > 1) { 304272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 305272cc70bSAndy Fleming cmd.cmdarg = 0; 306272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 307272cc70bSAndy Fleming cmd.flags = 0; 308def412b6SSteve Sakoman if (mmc_send_cmd(mmc, &cmd, NULL)) { 309def412b6SSteve Sakoman printf("mmc fail to send stop cmd\n"); 310def412b6SSteve Sakoman return 0; 311272cc70bSAndy Fleming } 31293ad0d18SJan Kloetzke } 3135d4fc8d9SRaffaele Recalcati 3145d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 31593ad0d18SJan Kloetzke if (mmc_send_status(mmc, timeout)) 31693ad0d18SJan Kloetzke return 0; 3170158126eSLei Wen 3180158126eSLei Wen return blkcnt; 3190158126eSLei Wen } 3200158126eSLei Wen 3210158126eSLei Wen static ulong 3220158126eSLei Wen mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src) 3230158126eSLei Wen { 3240158126eSLei Wen lbaint_t cur, blocks_todo = blkcnt; 3250158126eSLei Wen 326def412b6SSteve Sakoman struct mmc *mmc = find_mmc_device(dev_num); 3270158126eSLei Wen if (!mmc) 328def412b6SSteve Sakoman return 0; 3290158126eSLei Wen 330def412b6SSteve Sakoman if (mmc_set_blocklen(mmc, mmc->write_bl_len)) 331def412b6SSteve Sakoman return 0; 3320158126eSLei Wen 3330158126eSLei Wen do { 3348feafcc4SJohn Rigby cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo; 3350158126eSLei Wen if(mmc_write_blocks(mmc, start, cur, src) != cur) 336def412b6SSteve Sakoman return 0; 3370158126eSLei Wen blocks_todo -= cur; 3380158126eSLei Wen start += cur; 3390158126eSLei Wen src += cur * mmc->write_bl_len; 3400158126eSLei Wen } while (blocks_todo > 0); 341272cc70bSAndy Fleming 342272cc70bSAndy Fleming return blkcnt; 343272cc70bSAndy Fleming } 344272cc70bSAndy Fleming 3454a1a06bcSAlagu Sankar int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt) 346272cc70bSAndy Fleming { 347272cc70bSAndy Fleming struct mmc_cmd cmd; 348272cc70bSAndy Fleming struct mmc_data data; 349272cc70bSAndy Fleming 3504a1a06bcSAlagu Sankar if (blkcnt > 1) 3514a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; 3524a1a06bcSAlagu Sankar else 353272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; 354272cc70bSAndy Fleming 355272cc70bSAndy Fleming if (mmc->high_capacity) 3564a1a06bcSAlagu Sankar cmd.cmdarg = start; 357272cc70bSAndy Fleming else 3584a1a06bcSAlagu Sankar cmd.cmdarg = start * mmc->read_bl_len; 359272cc70bSAndy Fleming 360272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 361272cc70bSAndy Fleming cmd.flags = 0; 362272cc70bSAndy Fleming 363272cc70bSAndy Fleming data.dest = dst; 3644a1a06bcSAlagu Sankar data.blocks = blkcnt; 365272cc70bSAndy Fleming data.blocksize = mmc->read_bl_len; 366272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 367272cc70bSAndy Fleming 3684a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, &data)) 3694a1a06bcSAlagu Sankar return 0; 3704a1a06bcSAlagu Sankar 3714a1a06bcSAlagu Sankar if (blkcnt > 1) { 3724a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 3734a1a06bcSAlagu Sankar cmd.cmdarg = 0; 3744a1a06bcSAlagu Sankar cmd.resp_type = MMC_RSP_R1b; 3754a1a06bcSAlagu Sankar cmd.flags = 0; 3764a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, NULL)) { 3774a1a06bcSAlagu Sankar printf("mmc fail to send stop cmd\n"); 3784a1a06bcSAlagu Sankar return 0; 3794a1a06bcSAlagu Sankar } 380272cc70bSAndy Fleming } 381272cc70bSAndy Fleming 3824a1a06bcSAlagu Sankar return blkcnt; 383272cc70bSAndy Fleming } 384272cc70bSAndy Fleming 385272cc70bSAndy Fleming static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst) 386272cc70bSAndy Fleming { 3874a1a06bcSAlagu Sankar lbaint_t cur, blocks_todo = blkcnt; 388272cc70bSAndy Fleming 3894a1a06bcSAlagu Sankar if (blkcnt == 0) 3904a1a06bcSAlagu Sankar return 0; 3914a1a06bcSAlagu Sankar 3924a1a06bcSAlagu Sankar struct mmc *mmc = find_mmc_device(dev_num); 393272cc70bSAndy Fleming if (!mmc) 394272cc70bSAndy Fleming return 0; 395272cc70bSAndy Fleming 396d2bf29e3SLei Wen if ((start + blkcnt) > mmc->block_dev.lba) { 3974a1a06bcSAlagu Sankar printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", 398d2bf29e3SLei Wen start + blkcnt, mmc->block_dev.lba); 399d2bf29e3SLei Wen return 0; 400d2bf29e3SLei Wen } 401272cc70bSAndy Fleming 4024a1a06bcSAlagu Sankar if (mmc_set_blocklen(mmc, mmc->read_bl_len)) 403272cc70bSAndy Fleming return 0; 404272cc70bSAndy Fleming 4054a1a06bcSAlagu Sankar do { 4068feafcc4SJohn Rigby cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo; 4074a1a06bcSAlagu Sankar if(mmc_read_blocks(mmc, dst, start, cur) != cur) 4084a1a06bcSAlagu Sankar return 0; 4094a1a06bcSAlagu Sankar blocks_todo -= cur; 4104a1a06bcSAlagu Sankar start += cur; 4114a1a06bcSAlagu Sankar dst += cur * mmc->read_bl_len; 4124a1a06bcSAlagu Sankar } while (blocks_todo > 0); 413272cc70bSAndy Fleming 414272cc70bSAndy Fleming return blkcnt; 415272cc70bSAndy Fleming } 416272cc70bSAndy Fleming 417272cc70bSAndy Fleming int mmc_go_idle(struct mmc* mmc) 418272cc70bSAndy Fleming { 419272cc70bSAndy Fleming struct mmc_cmd cmd; 420272cc70bSAndy Fleming int err; 421272cc70bSAndy Fleming 422272cc70bSAndy Fleming udelay(1000); 423272cc70bSAndy Fleming 424272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; 425272cc70bSAndy Fleming cmd.cmdarg = 0; 426272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_NONE; 427272cc70bSAndy Fleming cmd.flags = 0; 428272cc70bSAndy Fleming 429272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 430272cc70bSAndy Fleming 431272cc70bSAndy Fleming if (err) 432272cc70bSAndy Fleming return err; 433272cc70bSAndy Fleming 434272cc70bSAndy Fleming udelay(2000); 435272cc70bSAndy Fleming 436272cc70bSAndy Fleming return 0; 437272cc70bSAndy Fleming } 438272cc70bSAndy Fleming 439272cc70bSAndy Fleming int 440272cc70bSAndy Fleming sd_send_op_cond(struct mmc *mmc) 441272cc70bSAndy Fleming { 442272cc70bSAndy Fleming int timeout = 1000; 443272cc70bSAndy Fleming int err; 444272cc70bSAndy Fleming struct mmc_cmd cmd; 445272cc70bSAndy Fleming 446272cc70bSAndy Fleming do { 447272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 448272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 449272cc70bSAndy Fleming cmd.cmdarg = 0; 450272cc70bSAndy Fleming cmd.flags = 0; 451272cc70bSAndy Fleming 452272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 453272cc70bSAndy Fleming 454272cc70bSAndy Fleming if (err) 455272cc70bSAndy Fleming return err; 456272cc70bSAndy Fleming 457272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; 458272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R3; 459250de12bSStefano Babic 460250de12bSStefano Babic /* 461250de12bSStefano Babic * Most cards do not answer if some reserved bits 462250de12bSStefano Babic * in the ocr are set. However, Some controller 463250de12bSStefano Babic * can set bit 7 (reserved for low voltages), but 464250de12bSStefano Babic * how to manage low voltages SD card is not yet 465250de12bSStefano Babic * specified. 466250de12bSStefano Babic */ 467d52ebf10SThomas Chou cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 : 468d52ebf10SThomas Chou (mmc->voltages & 0xff8000); 469272cc70bSAndy Fleming 470272cc70bSAndy Fleming if (mmc->version == SD_VERSION_2) 471272cc70bSAndy Fleming cmd.cmdarg |= OCR_HCS; 472272cc70bSAndy Fleming 473272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 474272cc70bSAndy Fleming 475272cc70bSAndy Fleming if (err) 476272cc70bSAndy Fleming return err; 477272cc70bSAndy Fleming 478272cc70bSAndy Fleming udelay(1000); 479272cc70bSAndy Fleming } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--); 480272cc70bSAndy Fleming 481272cc70bSAndy Fleming if (timeout <= 0) 482272cc70bSAndy Fleming return UNUSABLE_ERR; 483272cc70bSAndy Fleming 484272cc70bSAndy Fleming if (mmc->version != SD_VERSION_2) 485272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 486272cc70bSAndy Fleming 487d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 488d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 489d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 490d52ebf10SThomas Chou cmd.cmdarg = 0; 491d52ebf10SThomas Chou cmd.flags = 0; 492d52ebf10SThomas Chou 493d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 494d52ebf10SThomas Chou 495d52ebf10SThomas Chou if (err) 496d52ebf10SThomas Chou return err; 497d52ebf10SThomas Chou } 498d52ebf10SThomas Chou 499998be3ddSRabin Vincent mmc->ocr = cmd.response[0]; 500272cc70bSAndy Fleming 501272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 502272cc70bSAndy Fleming mmc->rca = 0; 503272cc70bSAndy Fleming 504272cc70bSAndy Fleming return 0; 505272cc70bSAndy Fleming } 506272cc70bSAndy Fleming 507272cc70bSAndy Fleming int mmc_send_op_cond(struct mmc *mmc) 508272cc70bSAndy Fleming { 50931cacbabSRaffaele Recalcati int timeout = 10000; 510272cc70bSAndy Fleming struct mmc_cmd cmd; 511272cc70bSAndy Fleming int err; 512272cc70bSAndy Fleming 513272cc70bSAndy Fleming /* Some cards seem to need this */ 514272cc70bSAndy Fleming mmc_go_idle(mmc); 515272cc70bSAndy Fleming 51631cacbabSRaffaele Recalcati /* Asking to the card its capabilities */ 51731cacbabSRaffaele Recalcati cmd.cmdidx = MMC_CMD_SEND_OP_COND; 51831cacbabSRaffaele Recalcati cmd.resp_type = MMC_RSP_R3; 51931cacbabSRaffaele Recalcati cmd.cmdarg = 0; 52031cacbabSRaffaele Recalcati cmd.flags = 0; 52131cacbabSRaffaele Recalcati 52231cacbabSRaffaele Recalcati err = mmc_send_cmd(mmc, &cmd, NULL); 52331cacbabSRaffaele Recalcati 52431cacbabSRaffaele Recalcati if (err) 52531cacbabSRaffaele Recalcati return err; 52631cacbabSRaffaele Recalcati 52731cacbabSRaffaele Recalcati udelay(1000); 52831cacbabSRaffaele Recalcati 529272cc70bSAndy Fleming do { 530272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_OP_COND; 531272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R3; 53231cacbabSRaffaele Recalcati cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 : 53331cacbabSRaffaele Recalcati (mmc->voltages & 53431cacbabSRaffaele Recalcati (cmd.response[0] & OCR_VOLTAGE_MASK)) | 53531cacbabSRaffaele Recalcati (cmd.response[0] & OCR_ACCESS_MODE)); 536b1f1e821SŁukasz Majewski 537b1f1e821SŁukasz Majewski if (mmc->host_caps & MMC_MODE_HC) 538b1f1e821SŁukasz Majewski cmd.cmdarg |= OCR_HCS; 539b1f1e821SŁukasz Majewski 540272cc70bSAndy Fleming cmd.flags = 0; 541272cc70bSAndy Fleming 542272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 543272cc70bSAndy Fleming 544272cc70bSAndy Fleming if (err) 545272cc70bSAndy Fleming return err; 546272cc70bSAndy Fleming 547272cc70bSAndy Fleming udelay(1000); 548272cc70bSAndy Fleming } while (!(cmd.response[0] & OCR_BUSY) && timeout--); 549272cc70bSAndy Fleming 550272cc70bSAndy Fleming if (timeout <= 0) 551272cc70bSAndy Fleming return UNUSABLE_ERR; 552272cc70bSAndy Fleming 553d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 554d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 555d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 556d52ebf10SThomas Chou cmd.cmdarg = 0; 557d52ebf10SThomas Chou cmd.flags = 0; 558d52ebf10SThomas Chou 559d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 560d52ebf10SThomas Chou 561d52ebf10SThomas Chou if (err) 562d52ebf10SThomas Chou return err; 563d52ebf10SThomas Chou } 564d52ebf10SThomas Chou 565272cc70bSAndy Fleming mmc->version = MMC_VERSION_UNKNOWN; 566998be3ddSRabin Vincent mmc->ocr = cmd.response[0]; 567272cc70bSAndy Fleming 568272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 569272cc70bSAndy Fleming mmc->rca = 0; 570272cc70bSAndy Fleming 571272cc70bSAndy Fleming return 0; 572272cc70bSAndy Fleming } 573272cc70bSAndy Fleming 574272cc70bSAndy Fleming 575272cc70bSAndy Fleming int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd) 576272cc70bSAndy Fleming { 577272cc70bSAndy Fleming struct mmc_cmd cmd; 578272cc70bSAndy Fleming struct mmc_data data; 579272cc70bSAndy Fleming int err; 580272cc70bSAndy Fleming 581272cc70bSAndy Fleming /* Get the Card Status Register */ 582272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; 583272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 584272cc70bSAndy Fleming cmd.cmdarg = 0; 585272cc70bSAndy Fleming cmd.flags = 0; 586272cc70bSAndy Fleming 587272cc70bSAndy Fleming data.dest = ext_csd; 588272cc70bSAndy Fleming data.blocks = 1; 589272cc70bSAndy Fleming data.blocksize = 512; 590272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 591272cc70bSAndy Fleming 592272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 593272cc70bSAndy Fleming 594272cc70bSAndy Fleming return err; 595272cc70bSAndy Fleming } 596272cc70bSAndy Fleming 597272cc70bSAndy Fleming 598272cc70bSAndy Fleming int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) 599272cc70bSAndy Fleming { 600272cc70bSAndy Fleming struct mmc_cmd cmd; 6015d4fc8d9SRaffaele Recalcati int timeout = 1000; 6025d4fc8d9SRaffaele Recalcati int ret; 603272cc70bSAndy Fleming 604272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SWITCH; 605272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 606272cc70bSAndy Fleming cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 607272cc70bSAndy Fleming (index << 16) | 608272cc70bSAndy Fleming (value << 8); 609272cc70bSAndy Fleming cmd.flags = 0; 610272cc70bSAndy Fleming 6115d4fc8d9SRaffaele Recalcati ret = mmc_send_cmd(mmc, &cmd, NULL); 6125d4fc8d9SRaffaele Recalcati 6135d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 61493ad0d18SJan Kloetzke if (!ret) 61593ad0d18SJan Kloetzke ret = mmc_send_status(mmc, timeout); 6165d4fc8d9SRaffaele Recalcati 6175d4fc8d9SRaffaele Recalcati return ret; 6185d4fc8d9SRaffaele Recalcati 619272cc70bSAndy Fleming } 620272cc70bSAndy Fleming 621272cc70bSAndy Fleming int mmc_change_freq(struct mmc *mmc) 622272cc70bSAndy Fleming { 623a1969923SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512); 624272cc70bSAndy Fleming char cardtype; 625272cc70bSAndy Fleming int err; 626272cc70bSAndy Fleming 627272cc70bSAndy Fleming mmc->card_caps = 0; 628272cc70bSAndy Fleming 629d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 630d52ebf10SThomas Chou return 0; 631d52ebf10SThomas Chou 632272cc70bSAndy Fleming /* Only version 4 supports high-speed */ 633272cc70bSAndy Fleming if (mmc->version < MMC_VERSION_4) 634272cc70bSAndy Fleming return 0; 635272cc70bSAndy Fleming 636272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 637272cc70bSAndy Fleming 638272cc70bSAndy Fleming if (err) 639272cc70bSAndy Fleming return err; 640272cc70bSAndy Fleming 6410560db18SLei Wen cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf; 642272cc70bSAndy Fleming 643272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); 644272cc70bSAndy Fleming 645272cc70bSAndy Fleming if (err) 646272cc70bSAndy Fleming return err; 647272cc70bSAndy Fleming 648272cc70bSAndy Fleming /* Now check to see that it worked */ 649272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 650272cc70bSAndy Fleming 651272cc70bSAndy Fleming if (err) 652272cc70bSAndy Fleming return err; 653272cc70bSAndy Fleming 654272cc70bSAndy Fleming /* No high-speed support */ 6550560db18SLei Wen if (!ext_csd[EXT_CSD_HS_TIMING]) 656272cc70bSAndy Fleming return 0; 657272cc70bSAndy Fleming 658272cc70bSAndy Fleming /* High Speed is set, there are two types: 52MHz and 26MHz */ 659272cc70bSAndy Fleming if (cardtype & MMC_HS_52MHZ) 660272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; 661272cc70bSAndy Fleming else 662272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 663272cc70bSAndy Fleming 664272cc70bSAndy Fleming return 0; 665272cc70bSAndy Fleming } 666272cc70bSAndy Fleming 667bc897b1dSLei Wen int mmc_switch_part(int dev_num, unsigned int part_num) 668bc897b1dSLei Wen { 669bc897b1dSLei Wen struct mmc *mmc = find_mmc_device(dev_num); 670bc897b1dSLei Wen 671bc897b1dSLei Wen if (!mmc) 672bc897b1dSLei Wen return -1; 673bc897b1dSLei Wen 674bc897b1dSLei Wen return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, 675bc897b1dSLei Wen (mmc->part_config & ~PART_ACCESS_MASK) 676bc897b1dSLei Wen | (part_num & PART_ACCESS_MASK)); 677bc897b1dSLei Wen } 678bc897b1dSLei Wen 67948972d90SThierry Reding int mmc_getcd(struct mmc *mmc) 68048972d90SThierry Reding { 68148972d90SThierry Reding int cd; 68248972d90SThierry Reding 68348972d90SThierry Reding cd = board_mmc_getcd(mmc); 68448972d90SThierry Reding 68548972d90SThierry Reding if ((cd < 0) && mmc->getcd) 68648972d90SThierry Reding cd = mmc->getcd(mmc); 68748972d90SThierry Reding 68848972d90SThierry Reding return cd; 68948972d90SThierry Reding } 69048972d90SThierry Reding 691272cc70bSAndy Fleming int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) 692272cc70bSAndy Fleming { 693272cc70bSAndy Fleming struct mmc_cmd cmd; 694272cc70bSAndy Fleming struct mmc_data data; 695272cc70bSAndy Fleming 696272cc70bSAndy Fleming /* Switch the frequency */ 697272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SWITCH_FUNC; 698272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 699272cc70bSAndy Fleming cmd.cmdarg = (mode << 31) | 0xffffff; 700272cc70bSAndy Fleming cmd.cmdarg &= ~(0xf << (group * 4)); 701272cc70bSAndy Fleming cmd.cmdarg |= value << (group * 4); 702272cc70bSAndy Fleming cmd.flags = 0; 703272cc70bSAndy Fleming 704272cc70bSAndy Fleming data.dest = (char *)resp; 705272cc70bSAndy Fleming data.blocksize = 64; 706272cc70bSAndy Fleming data.blocks = 1; 707272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 708272cc70bSAndy Fleming 709272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, &data); 710272cc70bSAndy Fleming } 711272cc70bSAndy Fleming 712272cc70bSAndy Fleming 713272cc70bSAndy Fleming int sd_change_freq(struct mmc *mmc) 714272cc70bSAndy Fleming { 715272cc70bSAndy Fleming int err; 716272cc70bSAndy Fleming struct mmc_cmd cmd; 717f781dd38SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2); 718f781dd38SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16); 719272cc70bSAndy Fleming struct mmc_data data; 720272cc70bSAndy Fleming int timeout; 721272cc70bSAndy Fleming 722272cc70bSAndy Fleming mmc->card_caps = 0; 723272cc70bSAndy Fleming 724d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 725d52ebf10SThomas Chou return 0; 726d52ebf10SThomas Chou 727272cc70bSAndy Fleming /* Read the SCR to find out if this card supports higher speeds */ 728272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 729272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 730272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 731272cc70bSAndy Fleming cmd.flags = 0; 732272cc70bSAndy Fleming 733272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 734272cc70bSAndy Fleming 735272cc70bSAndy Fleming if (err) 736272cc70bSAndy Fleming return err; 737272cc70bSAndy Fleming 738272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_SCR; 739272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 740272cc70bSAndy Fleming cmd.cmdarg = 0; 741272cc70bSAndy Fleming cmd.flags = 0; 742272cc70bSAndy Fleming 743272cc70bSAndy Fleming timeout = 3; 744272cc70bSAndy Fleming 745272cc70bSAndy Fleming retry_scr: 746f781dd38SAnton staaf data.dest = (char *)scr; 747272cc70bSAndy Fleming data.blocksize = 8; 748272cc70bSAndy Fleming data.blocks = 1; 749272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 750272cc70bSAndy Fleming 751272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 752272cc70bSAndy Fleming 753272cc70bSAndy Fleming if (err) { 754272cc70bSAndy Fleming if (timeout--) 755272cc70bSAndy Fleming goto retry_scr; 756272cc70bSAndy Fleming 757272cc70bSAndy Fleming return err; 758272cc70bSAndy Fleming } 759272cc70bSAndy Fleming 7604e3d89baSYauhen Kharuzhy mmc->scr[0] = __be32_to_cpu(scr[0]); 7614e3d89baSYauhen Kharuzhy mmc->scr[1] = __be32_to_cpu(scr[1]); 762272cc70bSAndy Fleming 763272cc70bSAndy Fleming switch ((mmc->scr[0] >> 24) & 0xf) { 764272cc70bSAndy Fleming case 0: 765272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 766272cc70bSAndy Fleming break; 767272cc70bSAndy Fleming case 1: 768272cc70bSAndy Fleming mmc->version = SD_VERSION_1_10; 769272cc70bSAndy Fleming break; 770272cc70bSAndy Fleming case 2: 771272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 772272cc70bSAndy Fleming break; 773272cc70bSAndy Fleming default: 774272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 775272cc70bSAndy Fleming break; 776272cc70bSAndy Fleming } 777272cc70bSAndy Fleming 778b44c7083SAlagu Sankar if (mmc->scr[0] & SD_DATA_4BIT) 779b44c7083SAlagu Sankar mmc->card_caps |= MMC_MODE_4BIT; 780b44c7083SAlagu Sankar 781272cc70bSAndy Fleming /* Version 1.0 doesn't support switching */ 782272cc70bSAndy Fleming if (mmc->version == SD_VERSION_1_0) 783272cc70bSAndy Fleming return 0; 784272cc70bSAndy Fleming 785272cc70bSAndy Fleming timeout = 4; 786272cc70bSAndy Fleming while (timeout--) { 787272cc70bSAndy Fleming err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, 788f781dd38SAnton staaf (u8 *)switch_status); 789272cc70bSAndy Fleming 790272cc70bSAndy Fleming if (err) 791272cc70bSAndy Fleming return err; 792272cc70bSAndy Fleming 793272cc70bSAndy Fleming /* The high-speed function is busy. Try again */ 7944e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) 795272cc70bSAndy Fleming break; 796272cc70bSAndy Fleming } 797272cc70bSAndy Fleming 798272cc70bSAndy Fleming /* If high-speed isn't supported, we return */ 7994e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) 800272cc70bSAndy Fleming return 0; 801272cc70bSAndy Fleming 8022c3fbf4cSMacpaul Lin /* 8032c3fbf4cSMacpaul Lin * If the host doesn't support SD_HIGHSPEED, do not switch card to 8042c3fbf4cSMacpaul Lin * HIGHSPEED mode even if the card support SD_HIGHSPPED. 8052c3fbf4cSMacpaul Lin * This can avoid furthur problem when the card runs in different 8062c3fbf4cSMacpaul Lin * mode between the host. 8072c3fbf4cSMacpaul Lin */ 8082c3fbf4cSMacpaul Lin if (!((mmc->host_caps & MMC_MODE_HS_52MHz) && 8092c3fbf4cSMacpaul Lin (mmc->host_caps & MMC_MODE_HS))) 8102c3fbf4cSMacpaul Lin return 0; 8112c3fbf4cSMacpaul Lin 812f781dd38SAnton staaf err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status); 813272cc70bSAndy Fleming 814272cc70bSAndy Fleming if (err) 815272cc70bSAndy Fleming return err; 816272cc70bSAndy Fleming 8174e3d89baSYauhen Kharuzhy if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) 818272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 819272cc70bSAndy Fleming 820272cc70bSAndy Fleming return 0; 821272cc70bSAndy Fleming } 822272cc70bSAndy Fleming 823272cc70bSAndy Fleming /* frequency bases */ 824272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */ 8255f837c2cSMike Frysinger static const int fbase[] = { 826272cc70bSAndy Fleming 10000, 827272cc70bSAndy Fleming 100000, 828272cc70bSAndy Fleming 1000000, 829272cc70bSAndy Fleming 10000000, 830272cc70bSAndy Fleming }; 831272cc70bSAndy Fleming 832272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice 833272cc70bSAndy Fleming * to platforms without floating point. 834272cc70bSAndy Fleming */ 8355f837c2cSMike Frysinger static const int multipliers[] = { 836272cc70bSAndy Fleming 0, /* reserved */ 837272cc70bSAndy Fleming 10, 838272cc70bSAndy Fleming 12, 839272cc70bSAndy Fleming 13, 840272cc70bSAndy Fleming 15, 841272cc70bSAndy Fleming 20, 842272cc70bSAndy Fleming 25, 843272cc70bSAndy Fleming 30, 844272cc70bSAndy Fleming 35, 845272cc70bSAndy Fleming 40, 846272cc70bSAndy Fleming 45, 847272cc70bSAndy Fleming 50, 848272cc70bSAndy Fleming 55, 849272cc70bSAndy Fleming 60, 850272cc70bSAndy Fleming 70, 851272cc70bSAndy Fleming 80, 852272cc70bSAndy Fleming }; 853272cc70bSAndy Fleming 854272cc70bSAndy Fleming void mmc_set_ios(struct mmc *mmc) 855272cc70bSAndy Fleming { 856272cc70bSAndy Fleming mmc->set_ios(mmc); 857272cc70bSAndy Fleming } 858272cc70bSAndy Fleming 859272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock) 860272cc70bSAndy Fleming { 861272cc70bSAndy Fleming if (clock > mmc->f_max) 862272cc70bSAndy Fleming clock = mmc->f_max; 863272cc70bSAndy Fleming 864272cc70bSAndy Fleming if (clock < mmc->f_min) 865272cc70bSAndy Fleming clock = mmc->f_min; 866272cc70bSAndy Fleming 867272cc70bSAndy Fleming mmc->clock = clock; 868272cc70bSAndy Fleming 869272cc70bSAndy Fleming mmc_set_ios(mmc); 870272cc70bSAndy Fleming } 871272cc70bSAndy Fleming 872272cc70bSAndy Fleming void mmc_set_bus_width(struct mmc *mmc, uint width) 873272cc70bSAndy Fleming { 874272cc70bSAndy Fleming mmc->bus_width = width; 875272cc70bSAndy Fleming 876272cc70bSAndy Fleming mmc_set_ios(mmc); 877272cc70bSAndy Fleming } 878272cc70bSAndy Fleming 879272cc70bSAndy Fleming int mmc_startup(struct mmc *mmc) 880272cc70bSAndy Fleming { 8814137894eSLei Wen int err, width; 882272cc70bSAndy Fleming uint mult, freq; 883639b7827SYoshihiro Shimoda u64 cmult, csize, capacity; 884272cc70bSAndy Fleming struct mmc_cmd cmd; 885a1969923SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(char, ext_csd, 512); 8864137894eSLei Wen ALLOC_CACHE_ALIGN_BUFFER(char, test_csd, 512); 8875d4fc8d9SRaffaele Recalcati int timeout = 1000; 888272cc70bSAndy Fleming 889d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON 890d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ 891d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF; 892d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R1; 893d52ebf10SThomas Chou cmd.cmdarg = 1; 894d52ebf10SThomas Chou cmd.flags = 0; 895d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 896d52ebf10SThomas Chou 897d52ebf10SThomas Chou if (err) 898d52ebf10SThomas Chou return err; 899d52ebf10SThomas Chou } 900d52ebf10SThomas Chou #endif 901d52ebf10SThomas Chou 902272cc70bSAndy Fleming /* Put the Card in Identify Mode */ 903d52ebf10SThomas Chou cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID : 904d52ebf10SThomas Chou MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */ 905272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 906272cc70bSAndy Fleming cmd.cmdarg = 0; 907272cc70bSAndy Fleming cmd.flags = 0; 908272cc70bSAndy Fleming 909272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 910272cc70bSAndy Fleming 911272cc70bSAndy Fleming if (err) 912272cc70bSAndy Fleming return err; 913272cc70bSAndy Fleming 914272cc70bSAndy Fleming memcpy(mmc->cid, cmd.response, 16); 915272cc70bSAndy Fleming 916272cc70bSAndy Fleming /* 917272cc70bSAndy Fleming * For MMC cards, set the Relative Address. 918272cc70bSAndy Fleming * For SD cards, get the Relatvie Address. 919272cc70bSAndy Fleming * This also puts the cards into Standby State 920272cc70bSAndy Fleming */ 921d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 922272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; 923272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 924272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R6; 925272cc70bSAndy Fleming cmd.flags = 0; 926272cc70bSAndy Fleming 927272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 928272cc70bSAndy Fleming 929272cc70bSAndy Fleming if (err) 930272cc70bSAndy Fleming return err; 931272cc70bSAndy Fleming 932272cc70bSAndy Fleming if (IS_SD(mmc)) 933998be3ddSRabin Vincent mmc->rca = (cmd.response[0] >> 16) & 0xffff; 934d52ebf10SThomas Chou } 935272cc70bSAndy Fleming 936272cc70bSAndy Fleming /* Get the Card-Specific Data */ 937272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_CSD; 938272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 939272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 940272cc70bSAndy Fleming cmd.flags = 0; 941272cc70bSAndy Fleming 942272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 943272cc70bSAndy Fleming 9445d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 9455d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 9465d4fc8d9SRaffaele Recalcati 947272cc70bSAndy Fleming if (err) 948272cc70bSAndy Fleming return err; 949272cc70bSAndy Fleming 950998be3ddSRabin Vincent mmc->csd[0] = cmd.response[0]; 951998be3ddSRabin Vincent mmc->csd[1] = cmd.response[1]; 952998be3ddSRabin Vincent mmc->csd[2] = cmd.response[2]; 953998be3ddSRabin Vincent mmc->csd[3] = cmd.response[3]; 954272cc70bSAndy Fleming 955272cc70bSAndy Fleming if (mmc->version == MMC_VERSION_UNKNOWN) { 9560b453ffeSRabin Vincent int version = (cmd.response[0] >> 26) & 0xf; 957272cc70bSAndy Fleming 958272cc70bSAndy Fleming switch (version) { 959272cc70bSAndy Fleming case 0: 960272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 961272cc70bSAndy Fleming break; 962272cc70bSAndy Fleming case 1: 963272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_4; 964272cc70bSAndy Fleming break; 965272cc70bSAndy Fleming case 2: 966272cc70bSAndy Fleming mmc->version = MMC_VERSION_2_2; 967272cc70bSAndy Fleming break; 968272cc70bSAndy Fleming case 3: 969272cc70bSAndy Fleming mmc->version = MMC_VERSION_3; 970272cc70bSAndy Fleming break; 971272cc70bSAndy Fleming case 4: 972272cc70bSAndy Fleming mmc->version = MMC_VERSION_4; 973272cc70bSAndy Fleming break; 974272cc70bSAndy Fleming default: 975272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 976272cc70bSAndy Fleming break; 977272cc70bSAndy Fleming } 978272cc70bSAndy Fleming } 979272cc70bSAndy Fleming 980272cc70bSAndy Fleming /* divide frequency by 10, since the mults are 10x bigger */ 9810b453ffeSRabin Vincent freq = fbase[(cmd.response[0] & 0x7)]; 9820b453ffeSRabin Vincent mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; 983272cc70bSAndy Fleming 984272cc70bSAndy Fleming mmc->tran_speed = freq * mult; 985272cc70bSAndy Fleming 986998be3ddSRabin Vincent mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); 987272cc70bSAndy Fleming 988272cc70bSAndy Fleming if (IS_SD(mmc)) 989272cc70bSAndy Fleming mmc->write_bl_len = mmc->read_bl_len; 990272cc70bSAndy Fleming else 991998be3ddSRabin Vincent mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); 992272cc70bSAndy Fleming 993272cc70bSAndy Fleming if (mmc->high_capacity) { 994272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3f) << 16 995272cc70bSAndy Fleming | (mmc->csd[2] & 0xffff0000) >> 16; 996272cc70bSAndy Fleming cmult = 8; 997272cc70bSAndy Fleming } else { 998272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3ff) << 2 999272cc70bSAndy Fleming | (mmc->csd[2] & 0xc0000000) >> 30; 1000272cc70bSAndy Fleming cmult = (mmc->csd[2] & 0x00038000) >> 15; 1001272cc70bSAndy Fleming } 1002272cc70bSAndy Fleming 1003272cc70bSAndy Fleming mmc->capacity = (csize + 1) << (cmult + 2); 1004272cc70bSAndy Fleming mmc->capacity *= mmc->read_bl_len; 1005272cc70bSAndy Fleming 1006272cc70bSAndy Fleming if (mmc->read_bl_len > 512) 1007272cc70bSAndy Fleming mmc->read_bl_len = 512; 1008272cc70bSAndy Fleming 1009272cc70bSAndy Fleming if (mmc->write_bl_len > 512) 1010272cc70bSAndy Fleming mmc->write_bl_len = 512; 1011272cc70bSAndy Fleming 1012272cc70bSAndy Fleming /* Select the card, and put it into Transfer Mode */ 1013d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 1014272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SELECT_CARD; 1015fe8f7066SAjay Bhargav cmd.resp_type = MMC_RSP_R1; 1016272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1017272cc70bSAndy Fleming cmd.flags = 0; 1018272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1019272cc70bSAndy Fleming 1020272cc70bSAndy Fleming if (err) 1021272cc70bSAndy Fleming return err; 1022d52ebf10SThomas Chou } 1023272cc70bSAndy Fleming 1024e6f99a56SLei Wen /* 1025e6f99a56SLei Wen * For SD, its erase group is always one sector 1026e6f99a56SLei Wen */ 1027e6f99a56SLei Wen mmc->erase_grp_size = 1; 1028bc897b1dSLei Wen mmc->part_config = MMCPART_NOAVAILABLE; 1029d23e2c09SSukumar Ghorai if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) { 1030d23e2c09SSukumar Ghorai /* check ext_csd version and capacity */ 1031d23e2c09SSukumar Ghorai err = mmc_send_ext_csd(mmc, ext_csd); 10320560db18SLei Wen if (!err & (ext_csd[EXT_CSD_REV] >= 2)) { 1033639b7827SYoshihiro Shimoda /* 1034639b7827SYoshihiro Shimoda * According to the JEDEC Standard, the value of 1035639b7827SYoshihiro Shimoda * ext_csd's capacity is valid if the value is more 1036639b7827SYoshihiro Shimoda * than 2GB 1037639b7827SYoshihiro Shimoda */ 10380560db18SLei Wen capacity = ext_csd[EXT_CSD_SEC_CNT] << 0 10390560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 10400560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 10410560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; 1042639b7827SYoshihiro Shimoda capacity *= 512; 1043b1f1e821SŁukasz Majewski if ((capacity >> 20) > 2 * 1024) 1044639b7827SYoshihiro Shimoda mmc->capacity = capacity; 1045d23e2c09SSukumar Ghorai } 1046bc897b1dSLei Wen 1047e6f99a56SLei Wen /* 1048e6f99a56SLei Wen * Check whether GROUP_DEF is set, if yes, read out 1049e6f99a56SLei Wen * group size from ext_csd directly, or calculate 1050e6f99a56SLei Wen * the group size from the csd value. 1051e6f99a56SLei Wen */ 10520560db18SLei Wen if (ext_csd[EXT_CSD_ERASE_GROUP_DEF]) 10530560db18SLei Wen mmc->erase_grp_size = 10540560db18SLei Wen ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024; 1055e6f99a56SLei Wen else { 1056e6f99a56SLei Wen int erase_gsz, erase_gmul; 1057e6f99a56SLei Wen erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10; 1058e6f99a56SLei Wen erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5; 1059e6f99a56SLei Wen mmc->erase_grp_size = (erase_gsz + 1) 1060e6f99a56SLei Wen * (erase_gmul + 1); 1061e6f99a56SLei Wen } 1062e6f99a56SLei Wen 1063bc897b1dSLei Wen /* store the partition info of emmc */ 10640560db18SLei Wen if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) 10650560db18SLei Wen mmc->part_config = ext_csd[EXT_CSD_PART_CONF]; 1066d23e2c09SSukumar Ghorai } 1067d23e2c09SSukumar Ghorai 1068272cc70bSAndy Fleming if (IS_SD(mmc)) 1069272cc70bSAndy Fleming err = sd_change_freq(mmc); 1070272cc70bSAndy Fleming else 1071272cc70bSAndy Fleming err = mmc_change_freq(mmc); 1072272cc70bSAndy Fleming 1073272cc70bSAndy Fleming if (err) 1074272cc70bSAndy Fleming return err; 1075272cc70bSAndy Fleming 1076272cc70bSAndy Fleming /* Restrict card's capabilities by what the host can do */ 1077272cc70bSAndy Fleming mmc->card_caps &= mmc->host_caps; 1078272cc70bSAndy Fleming 1079272cc70bSAndy Fleming if (IS_SD(mmc)) { 1080272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_4BIT) { 1081272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 1082272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 1083272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1084272cc70bSAndy Fleming cmd.flags = 0; 1085272cc70bSAndy Fleming 1086272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1087272cc70bSAndy Fleming if (err) 1088272cc70bSAndy Fleming return err; 1089272cc70bSAndy Fleming 1090272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; 1091272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 1092272cc70bSAndy Fleming cmd.cmdarg = 2; 1093272cc70bSAndy Fleming cmd.flags = 0; 1094272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1095272cc70bSAndy Fleming if (err) 1096272cc70bSAndy Fleming return err; 1097272cc70bSAndy Fleming 1098272cc70bSAndy Fleming mmc_set_bus_width(mmc, 4); 1099272cc70bSAndy Fleming } 1100272cc70bSAndy Fleming 1101272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) 1102272cc70bSAndy Fleming mmc_set_clock(mmc, 50000000); 1103272cc70bSAndy Fleming else 1104272cc70bSAndy Fleming mmc_set_clock(mmc, 25000000); 1105272cc70bSAndy Fleming } else { 11064137894eSLei Wen for (width = EXT_CSD_BUS_WIDTH_8; width >= 0; width--) { 1107272cc70bSAndy Fleming /* Set the card to use 4 bit*/ 1108272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 11094137894eSLei Wen EXT_CSD_BUS_WIDTH, width); 1110272cc70bSAndy Fleming 1111272cc70bSAndy Fleming if (err) 11124137894eSLei Wen continue; 1113272cc70bSAndy Fleming 11144137894eSLei Wen if (!width) { 11154137894eSLei Wen mmc_set_bus_width(mmc, 1); 11164137894eSLei Wen break; 11174137894eSLei Wen } else 11184137894eSLei Wen mmc_set_bus_width(mmc, 4 * width); 1119272cc70bSAndy Fleming 11204137894eSLei Wen err = mmc_send_ext_csd(mmc, test_csd); 11214137894eSLei Wen if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \ 11224137894eSLei Wen == test_csd[EXT_CSD_PARTITIONING_SUPPORT] 11234137894eSLei Wen && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \ 11244137894eSLei Wen == test_csd[EXT_CSD_ERASE_GROUP_DEF] \ 11254137894eSLei Wen && ext_csd[EXT_CSD_REV] \ 11264137894eSLei Wen == test_csd[EXT_CSD_REV] 11274137894eSLei Wen && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \ 11284137894eSLei Wen == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] 11294137894eSLei Wen && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \ 11304137894eSLei Wen &test_csd[EXT_CSD_SEC_CNT], 4) == 0) { 1131272cc70bSAndy Fleming 11324137894eSLei Wen mmc->card_caps |= width; 11334137894eSLei Wen break; 11344137894eSLei Wen } 1135272cc70bSAndy Fleming } 1136272cc70bSAndy Fleming 1137272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) { 1138272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS_52MHz) 1139272cc70bSAndy Fleming mmc_set_clock(mmc, 52000000); 1140272cc70bSAndy Fleming else 1141272cc70bSAndy Fleming mmc_set_clock(mmc, 26000000); 1142272cc70bSAndy Fleming } else 1143272cc70bSAndy Fleming mmc_set_clock(mmc, 20000000); 1144272cc70bSAndy Fleming } 1145272cc70bSAndy Fleming 1146272cc70bSAndy Fleming /* fill in device description */ 1147272cc70bSAndy Fleming mmc->block_dev.lun = 0; 1148272cc70bSAndy Fleming mmc->block_dev.type = 0; 1149272cc70bSAndy Fleming mmc->block_dev.blksz = mmc->read_bl_len; 11509b1f942cSRabin Vincent mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); 11510b453ffeSRabin Vincent sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8, 11520b453ffeSRabin Vincent (mmc->cid[2] << 8) | (mmc->cid[3] >> 24)); 11530b453ffeSRabin Vincent sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff, 11540b453ffeSRabin Vincent (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, 11550b453ffeSRabin Vincent (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); 11560b453ffeSRabin Vincent sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28, 11570b453ffeSRabin Vincent (mmc->cid[2] >> 24) & 0xf); 1158272cc70bSAndy Fleming init_part(&mmc->block_dev); 1159272cc70bSAndy Fleming 1160272cc70bSAndy Fleming return 0; 1161272cc70bSAndy Fleming } 1162272cc70bSAndy Fleming 1163272cc70bSAndy Fleming int mmc_send_if_cond(struct mmc *mmc) 1164272cc70bSAndy Fleming { 1165272cc70bSAndy Fleming struct mmc_cmd cmd; 1166272cc70bSAndy Fleming int err; 1167272cc70bSAndy Fleming 1168272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_IF_COND; 1169272cc70bSAndy Fleming /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ 1170272cc70bSAndy Fleming cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa; 1171272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R7; 1172272cc70bSAndy Fleming cmd.flags = 0; 1173272cc70bSAndy Fleming 1174272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1175272cc70bSAndy Fleming 1176272cc70bSAndy Fleming if (err) 1177272cc70bSAndy Fleming return err; 1178272cc70bSAndy Fleming 1179998be3ddSRabin Vincent if ((cmd.response[0] & 0xff) != 0xaa) 1180272cc70bSAndy Fleming return UNUSABLE_ERR; 1181272cc70bSAndy Fleming else 1182272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 1183272cc70bSAndy Fleming 1184272cc70bSAndy Fleming return 0; 1185272cc70bSAndy Fleming } 1186272cc70bSAndy Fleming 1187272cc70bSAndy Fleming int mmc_register(struct mmc *mmc) 1188272cc70bSAndy Fleming { 1189272cc70bSAndy Fleming /* Setup the universal parts of the block interface just once */ 1190272cc70bSAndy Fleming mmc->block_dev.if_type = IF_TYPE_MMC; 1191272cc70bSAndy Fleming mmc->block_dev.dev = cur_dev_num++; 1192272cc70bSAndy Fleming mmc->block_dev.removable = 1; 1193272cc70bSAndy Fleming mmc->block_dev.block_read = mmc_bread; 1194272cc70bSAndy Fleming mmc->block_dev.block_write = mmc_bwrite; 1195e6f99a56SLei Wen mmc->block_dev.block_erase = mmc_berase; 11968feafcc4SJohn Rigby if (!mmc->b_max) 11978feafcc4SJohn Rigby mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; 1198272cc70bSAndy Fleming 1199272cc70bSAndy Fleming INIT_LIST_HEAD (&mmc->link); 1200272cc70bSAndy Fleming 1201272cc70bSAndy Fleming list_add_tail (&mmc->link, &mmc_devices); 1202272cc70bSAndy Fleming 1203272cc70bSAndy Fleming return 0; 1204272cc70bSAndy Fleming } 1205272cc70bSAndy Fleming 1206df3fc526SMatthew McClintock #ifdef CONFIG_PARTITIONS 1207272cc70bSAndy Fleming block_dev_desc_t *mmc_get_dev(int dev) 1208272cc70bSAndy Fleming { 1209272cc70bSAndy Fleming struct mmc *mmc = find_mmc_device(dev); 1210272cc70bSAndy Fleming 1211e85649c7SRabin Vincent return mmc ? &mmc->block_dev : NULL; 1212272cc70bSAndy Fleming } 1213df3fc526SMatthew McClintock #endif 1214272cc70bSAndy Fleming 1215272cc70bSAndy Fleming int mmc_init(struct mmc *mmc) 1216272cc70bSAndy Fleming { 1217afd5932bSMacpaul Lin int err; 1218272cc70bSAndy Fleming 121948972d90SThierry Reding if (mmc_getcd(mmc) == 0) { 122048972d90SThierry Reding mmc->has_init = 0; 122148972d90SThierry Reding printf("MMC: no card present\n"); 122248972d90SThierry Reding return NO_CARD_ERR; 122348972d90SThierry Reding } 122448972d90SThierry Reding 1225bc897b1dSLei Wen if (mmc->has_init) 1226bc897b1dSLei Wen return 0; 1227bc897b1dSLei Wen 1228272cc70bSAndy Fleming err = mmc->init(mmc); 1229272cc70bSAndy Fleming 1230272cc70bSAndy Fleming if (err) 1231272cc70bSAndy Fleming return err; 1232272cc70bSAndy Fleming 1233b86b85e2SIlya Yanok mmc_set_bus_width(mmc, 1); 1234b86b85e2SIlya Yanok mmc_set_clock(mmc, 1); 1235b86b85e2SIlya Yanok 1236272cc70bSAndy Fleming /* Reset the Card */ 1237272cc70bSAndy Fleming err = mmc_go_idle(mmc); 1238272cc70bSAndy Fleming 1239272cc70bSAndy Fleming if (err) 1240272cc70bSAndy Fleming return err; 1241272cc70bSAndy Fleming 1242bc897b1dSLei Wen /* The internal partition reset to user partition(0) at every CMD0*/ 1243bc897b1dSLei Wen mmc->part_num = 0; 1244bc897b1dSLei Wen 1245272cc70bSAndy Fleming /* Test for SD version 2 */ 1246272cc70bSAndy Fleming err = mmc_send_if_cond(mmc); 1247272cc70bSAndy Fleming 1248272cc70bSAndy Fleming /* Now try to get the SD card's operating condition */ 1249272cc70bSAndy Fleming err = sd_send_op_cond(mmc); 1250272cc70bSAndy Fleming 1251272cc70bSAndy Fleming /* If the command timed out, we check for an MMC card */ 1252272cc70bSAndy Fleming if (err == TIMEOUT) { 1253272cc70bSAndy Fleming err = mmc_send_op_cond(mmc); 1254272cc70bSAndy Fleming 1255272cc70bSAndy Fleming if (err) { 1256272cc70bSAndy Fleming printf("Card did not respond to voltage select!\n"); 1257272cc70bSAndy Fleming return UNUSABLE_ERR; 1258272cc70bSAndy Fleming } 1259272cc70bSAndy Fleming } 1260272cc70bSAndy Fleming 1261bc897b1dSLei Wen err = mmc_startup(mmc); 1262bc897b1dSLei Wen if (err) 1263bc897b1dSLei Wen mmc->has_init = 0; 1264bc897b1dSLei Wen else 1265bc897b1dSLei Wen mmc->has_init = 1; 1266bc897b1dSLei Wen return err; 1267272cc70bSAndy Fleming } 1268272cc70bSAndy Fleming 1269272cc70bSAndy Fleming /* 1270272cc70bSAndy Fleming * CPU and board-specific MMC initializations. Aliased function 1271272cc70bSAndy Fleming * signals caller to move on 1272272cc70bSAndy Fleming */ 1273272cc70bSAndy Fleming static int __def_mmc_init(bd_t *bis) 1274272cc70bSAndy Fleming { 1275272cc70bSAndy Fleming return -1; 1276272cc70bSAndy Fleming } 1277272cc70bSAndy Fleming 1278f9a109b3SPeter Tyser int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); 1279f9a109b3SPeter Tyser int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); 1280272cc70bSAndy Fleming 1281272cc70bSAndy Fleming void print_mmc_devices(char separator) 1282272cc70bSAndy Fleming { 1283272cc70bSAndy Fleming struct mmc *m; 1284272cc70bSAndy Fleming struct list_head *entry; 1285272cc70bSAndy Fleming 1286272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 1287272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 1288272cc70bSAndy Fleming 1289272cc70bSAndy Fleming printf("%s: %d", m->name, m->block_dev.dev); 1290272cc70bSAndy Fleming 1291272cc70bSAndy Fleming if (entry->next != &mmc_devices) 1292272cc70bSAndy Fleming printf("%c ", separator); 1293272cc70bSAndy Fleming } 1294272cc70bSAndy Fleming 1295272cc70bSAndy Fleming printf("\n"); 1296272cc70bSAndy Fleming } 1297272cc70bSAndy Fleming 1298ea6ebe21SLei Wen int get_mmc_num(void) 1299ea6ebe21SLei Wen { 1300ea6ebe21SLei Wen return cur_dev_num; 1301ea6ebe21SLei Wen } 1302ea6ebe21SLei Wen 1303272cc70bSAndy Fleming int mmc_initialize(bd_t *bis) 1304272cc70bSAndy Fleming { 1305272cc70bSAndy Fleming INIT_LIST_HEAD (&mmc_devices); 1306272cc70bSAndy Fleming cur_dev_num = 0; 1307272cc70bSAndy Fleming 1308272cc70bSAndy Fleming if (board_mmc_init(bis) < 0) 1309272cc70bSAndy Fleming cpu_mmc_init(bis); 1310272cc70bSAndy Fleming 1311272cc70bSAndy Fleming print_mmc_devices(','); 1312272cc70bSAndy Fleming 1313272cc70bSAndy Fleming return 0; 1314272cc70bSAndy Fleming } 1315