1272cc70bSAndy Fleming /* 2272cc70bSAndy Fleming * Copyright 2008, Freescale Semiconductor, Inc 3272cc70bSAndy Fleming * Andy Fleming 4272cc70bSAndy Fleming * 5272cc70bSAndy Fleming * Based vaguely on the Linux code 6272cc70bSAndy Fleming * 7272cc70bSAndy Fleming * See file CREDITS for list of people who contributed to this 8272cc70bSAndy Fleming * project. 9272cc70bSAndy Fleming * 10272cc70bSAndy Fleming * This program is free software; you can redistribute it and/or 11272cc70bSAndy Fleming * modify it under the terms of the GNU General Public License as 12272cc70bSAndy Fleming * published by the Free Software Foundation; either version 2 of 13272cc70bSAndy Fleming * the License, or (at your option) any later version. 14272cc70bSAndy Fleming * 15272cc70bSAndy Fleming * This program is distributed in the hope that it will be useful, 16272cc70bSAndy Fleming * but WITHOUT ANY WARRANTY; without even the implied warranty of 17272cc70bSAndy Fleming * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18272cc70bSAndy Fleming * GNU General Public License for more details. 19272cc70bSAndy Fleming * 20272cc70bSAndy Fleming * You should have received a copy of the GNU General Public License 21272cc70bSAndy Fleming * along with this program; if not, write to the Free Software 22272cc70bSAndy Fleming * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 23272cc70bSAndy Fleming * MA 02111-1307 USA 24272cc70bSAndy Fleming */ 25272cc70bSAndy Fleming 26272cc70bSAndy Fleming #include <config.h> 27272cc70bSAndy Fleming #include <common.h> 28272cc70bSAndy Fleming #include <command.h> 29272cc70bSAndy Fleming #include <mmc.h> 30272cc70bSAndy Fleming #include <part.h> 31272cc70bSAndy Fleming #include <malloc.h> 32272cc70bSAndy Fleming #include <linux/list.h> 339b1f942cSRabin Vincent #include <div64.h> 34272cc70bSAndy Fleming 35ce0fbcd2SMatt Waddel /* Set block count limit because of 16 bit register limit on some hardware*/ 36ce0fbcd2SMatt Waddel #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT 37ce0fbcd2SMatt Waddel #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535 38ce0fbcd2SMatt Waddel #endif 39ce0fbcd2SMatt Waddel 40272cc70bSAndy Fleming static struct list_head mmc_devices; 41272cc70bSAndy Fleming static int cur_dev_num = -1; 42272cc70bSAndy Fleming 4311fdade2SStefano Babic int __board_mmc_getcd(u8 *cd, struct mmc *mmc) { 4411fdade2SStefano Babic return -1; 4511fdade2SStefano Babic } 4611fdade2SStefano Babic 4711fdade2SStefano Babic int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak, 4811fdade2SStefano Babic alias("__board_mmc_getcd"))); 4911fdade2SStefano Babic 50272cc70bSAndy Fleming int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) 51272cc70bSAndy Fleming { 525db2fe3aSRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 535db2fe3aSRaffaele Recalcati int ret; 545db2fe3aSRaffaele Recalcati int i; 555db2fe3aSRaffaele Recalcati u8 *ptr; 565db2fe3aSRaffaele Recalcati 575db2fe3aSRaffaele Recalcati printf("CMD_SEND:%d\n", cmd->cmdidx); 585db2fe3aSRaffaele Recalcati printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg); 595db2fe3aSRaffaele Recalcati printf("\t\tFLAG\t\t\t %d\n", cmd->flags); 605db2fe3aSRaffaele Recalcati ret = mmc->send_cmd(mmc, cmd, data); 615db2fe3aSRaffaele Recalcati switch (cmd->resp_type) { 625db2fe3aSRaffaele Recalcati case MMC_RSP_NONE: 635db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_NONE\n"); 645db2fe3aSRaffaele Recalcati break; 655db2fe3aSRaffaele Recalcati case MMC_RSP_R1: 665db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n", 675db2fe3aSRaffaele Recalcati cmd->response[0]); 685db2fe3aSRaffaele Recalcati break; 695db2fe3aSRaffaele Recalcati case MMC_RSP_R1b: 705db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n", 715db2fe3aSRaffaele Recalcati cmd->response[0]); 725db2fe3aSRaffaele Recalcati break; 735db2fe3aSRaffaele Recalcati case MMC_RSP_R2: 745db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R2\t\t 0x%08X \n", 755db2fe3aSRaffaele Recalcati cmd->response[0]); 765db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 775db2fe3aSRaffaele Recalcati cmd->response[1]); 785db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 795db2fe3aSRaffaele Recalcati cmd->response[2]); 805db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 815db2fe3aSRaffaele Recalcati cmd->response[3]); 825db2fe3aSRaffaele Recalcati printf("\n"); 835db2fe3aSRaffaele Recalcati printf("\t\t\t\t\tDUMPING DATA\n"); 845db2fe3aSRaffaele Recalcati for (i = 0; i < 4; i++) { 855db2fe3aSRaffaele Recalcati int j; 865db2fe3aSRaffaele Recalcati printf("\t\t\t\t\t%03d - ", i*4); 875db2fe3aSRaffaele Recalcati ptr = &cmd->response[i]; 885db2fe3aSRaffaele Recalcati ptr += 3; 895db2fe3aSRaffaele Recalcati for (j = 0; j < 4; j++) 905db2fe3aSRaffaele Recalcati printf("%02X ", *ptr--); 915db2fe3aSRaffaele Recalcati printf("\n"); 925db2fe3aSRaffaele Recalcati } 935db2fe3aSRaffaele Recalcati break; 945db2fe3aSRaffaele Recalcati case MMC_RSP_R3: 955db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n", 965db2fe3aSRaffaele Recalcati cmd->response[0]); 975db2fe3aSRaffaele Recalcati break; 985db2fe3aSRaffaele Recalcati default: 995db2fe3aSRaffaele Recalcati printf("\t\tERROR MMC rsp not supported\n"); 1005db2fe3aSRaffaele Recalcati break; 1015db2fe3aSRaffaele Recalcati } 1025db2fe3aSRaffaele Recalcati return ret; 1035db2fe3aSRaffaele Recalcati #else 104272cc70bSAndy Fleming return mmc->send_cmd(mmc, cmd, data); 1055db2fe3aSRaffaele Recalcati #endif 106272cc70bSAndy Fleming } 107272cc70bSAndy Fleming 1085d4fc8d9SRaffaele Recalcati int mmc_send_status(struct mmc *mmc, int timeout) 1095d4fc8d9SRaffaele Recalcati { 1105d4fc8d9SRaffaele Recalcati struct mmc_cmd cmd; 1115d4fc8d9SRaffaele Recalcati int err; 1125d4fc8d9SRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 1135d4fc8d9SRaffaele Recalcati int status; 1145d4fc8d9SRaffaele Recalcati #endif 1155d4fc8d9SRaffaele Recalcati 1165d4fc8d9SRaffaele Recalcati cmd.cmdidx = MMC_CMD_SEND_STATUS; 1175d4fc8d9SRaffaele Recalcati cmd.resp_type = MMC_RSP_R1; 1185d4fc8d9SRaffaele Recalcati cmd.cmdarg = 0; 1195d4fc8d9SRaffaele Recalcati cmd.flags = 0; 1205d4fc8d9SRaffaele Recalcati 1215d4fc8d9SRaffaele Recalcati do { 1225d4fc8d9SRaffaele Recalcati err = mmc_send_cmd(mmc, &cmd, NULL); 1235d4fc8d9SRaffaele Recalcati if (err) 1245d4fc8d9SRaffaele Recalcati return err; 1255d4fc8d9SRaffaele Recalcati else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) 1265d4fc8d9SRaffaele Recalcati break; 1275d4fc8d9SRaffaele Recalcati 1285d4fc8d9SRaffaele Recalcati udelay(1000); 1295d4fc8d9SRaffaele Recalcati 1305d4fc8d9SRaffaele Recalcati if (cmd.response[0] & MMC_STATUS_MASK) { 1315d4fc8d9SRaffaele Recalcati printf("Status Error: 0x%08X\n", cmd.response[0]); 1325d4fc8d9SRaffaele Recalcati return COMM_ERR; 1335d4fc8d9SRaffaele Recalcati } 1345d4fc8d9SRaffaele Recalcati } while (timeout--); 1355d4fc8d9SRaffaele Recalcati 1365db2fe3aSRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 1375db2fe3aSRaffaele Recalcati status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9; 1385db2fe3aSRaffaele Recalcati printf("CURR STATE:%d\n", status); 1395db2fe3aSRaffaele Recalcati #endif 1405d4fc8d9SRaffaele Recalcati if (!timeout) { 1415d4fc8d9SRaffaele Recalcati printf("Timeout waiting card ready\n"); 1425d4fc8d9SRaffaele Recalcati return TIMEOUT; 1435d4fc8d9SRaffaele Recalcati } 1445d4fc8d9SRaffaele Recalcati 1455d4fc8d9SRaffaele Recalcati return 0; 1465d4fc8d9SRaffaele Recalcati } 1475d4fc8d9SRaffaele Recalcati 148272cc70bSAndy Fleming int mmc_set_blocklen(struct mmc *mmc, int len) 149272cc70bSAndy Fleming { 150272cc70bSAndy Fleming struct mmc_cmd cmd; 151272cc70bSAndy Fleming 152272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; 153272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 154272cc70bSAndy Fleming cmd.cmdarg = len; 155272cc70bSAndy Fleming cmd.flags = 0; 156272cc70bSAndy Fleming 157272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, NULL); 158272cc70bSAndy Fleming } 159272cc70bSAndy Fleming 160272cc70bSAndy Fleming struct mmc *find_mmc_device(int dev_num) 161272cc70bSAndy Fleming { 162272cc70bSAndy Fleming struct mmc *m; 163272cc70bSAndy Fleming struct list_head *entry; 164272cc70bSAndy Fleming 165272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 166272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 167272cc70bSAndy Fleming 168272cc70bSAndy Fleming if (m->block_dev.dev == dev_num) 169272cc70bSAndy Fleming return m; 170272cc70bSAndy Fleming } 171272cc70bSAndy Fleming 172272cc70bSAndy Fleming printf("MMC Device %d not found\n", dev_num); 173272cc70bSAndy Fleming 174272cc70bSAndy Fleming return NULL; 175272cc70bSAndy Fleming } 176272cc70bSAndy Fleming 177*e6f99a56SLei Wen static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt) 178*e6f99a56SLei Wen { 179*e6f99a56SLei Wen struct mmc_cmd cmd; 180*e6f99a56SLei Wen ulong end; 181*e6f99a56SLei Wen int err, start_cmd, end_cmd; 182*e6f99a56SLei Wen 183*e6f99a56SLei Wen if (mmc->high_capacity) 184*e6f99a56SLei Wen end = start + blkcnt - 1; 185*e6f99a56SLei Wen else { 186*e6f99a56SLei Wen end = (start + blkcnt - 1) * mmc->write_bl_len; 187*e6f99a56SLei Wen start *= mmc->write_bl_len; 188*e6f99a56SLei Wen } 189*e6f99a56SLei Wen 190*e6f99a56SLei Wen if (IS_SD(mmc)) { 191*e6f99a56SLei Wen start_cmd = SD_CMD_ERASE_WR_BLK_START; 192*e6f99a56SLei Wen end_cmd = SD_CMD_ERASE_WR_BLK_END; 193*e6f99a56SLei Wen } else { 194*e6f99a56SLei Wen start_cmd = MMC_CMD_ERASE_GROUP_START; 195*e6f99a56SLei Wen end_cmd = MMC_CMD_ERASE_GROUP_END; 196*e6f99a56SLei Wen } 197*e6f99a56SLei Wen 198*e6f99a56SLei Wen cmd.cmdidx = start_cmd; 199*e6f99a56SLei Wen cmd.cmdarg = start; 200*e6f99a56SLei Wen cmd.resp_type = MMC_RSP_R1; 201*e6f99a56SLei Wen cmd.flags = 0; 202*e6f99a56SLei Wen 203*e6f99a56SLei Wen err = mmc_send_cmd(mmc, &cmd, NULL); 204*e6f99a56SLei Wen if (err) 205*e6f99a56SLei Wen goto err_out; 206*e6f99a56SLei Wen 207*e6f99a56SLei Wen cmd.cmdidx = end_cmd; 208*e6f99a56SLei Wen cmd.cmdarg = end; 209*e6f99a56SLei Wen 210*e6f99a56SLei Wen err = mmc_send_cmd(mmc, &cmd, NULL); 211*e6f99a56SLei Wen if (err) 212*e6f99a56SLei Wen goto err_out; 213*e6f99a56SLei Wen 214*e6f99a56SLei Wen cmd.cmdidx = MMC_CMD_ERASE; 215*e6f99a56SLei Wen cmd.cmdarg = SECURE_ERASE; 216*e6f99a56SLei Wen cmd.resp_type = MMC_RSP_R1b; 217*e6f99a56SLei Wen 218*e6f99a56SLei Wen err = mmc_send_cmd(mmc, &cmd, NULL); 219*e6f99a56SLei Wen if (err) 220*e6f99a56SLei Wen goto err_out; 221*e6f99a56SLei Wen 222*e6f99a56SLei Wen return 0; 223*e6f99a56SLei Wen 224*e6f99a56SLei Wen err_out: 225*e6f99a56SLei Wen puts("mmc erase failed\n"); 226*e6f99a56SLei Wen return err; 227*e6f99a56SLei Wen } 228*e6f99a56SLei Wen 229*e6f99a56SLei Wen static unsigned long 230*e6f99a56SLei Wen mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt) 231*e6f99a56SLei Wen { 232*e6f99a56SLei Wen int err = 0; 233*e6f99a56SLei Wen struct mmc *mmc = find_mmc_device(dev_num); 234*e6f99a56SLei Wen lbaint_t blk = 0, blk_r = 0; 235*e6f99a56SLei Wen 236*e6f99a56SLei Wen if (!mmc) 237*e6f99a56SLei Wen return -1; 238*e6f99a56SLei Wen 239*e6f99a56SLei Wen if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size)) 240*e6f99a56SLei Wen printf("\n\nCaution! Your devices Erase group is 0x%x\n" 241*e6f99a56SLei Wen "The erase range would be change to 0x%lx~0x%lx\n\n", 242*e6f99a56SLei Wen mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1), 243*e6f99a56SLei Wen ((start + blkcnt + mmc->erase_grp_size) 244*e6f99a56SLei Wen & ~(mmc->erase_grp_size - 1)) - 1); 245*e6f99a56SLei Wen 246*e6f99a56SLei Wen while (blk < blkcnt) { 247*e6f99a56SLei Wen blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ? 248*e6f99a56SLei Wen mmc->erase_grp_size : (blkcnt - blk); 249*e6f99a56SLei Wen err = mmc_erase_t(mmc, start + blk, blk_r); 250*e6f99a56SLei Wen if (err) 251*e6f99a56SLei Wen break; 252*e6f99a56SLei Wen 253*e6f99a56SLei Wen blk += blk_r; 254*e6f99a56SLei Wen } 255*e6f99a56SLei Wen 256*e6f99a56SLei Wen return blk; 257*e6f99a56SLei Wen } 258*e6f99a56SLei Wen 259272cc70bSAndy Fleming static ulong 2600158126eSLei Wen mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src) 261272cc70bSAndy Fleming { 262272cc70bSAndy Fleming struct mmc_cmd cmd; 263272cc70bSAndy Fleming struct mmc_data data; 2645d4fc8d9SRaffaele Recalcati int timeout = 1000; 265272cc70bSAndy Fleming 266d2bf29e3SLei Wen if ((start + blkcnt) > mmc->block_dev.lba) { 267def412b6SSteve Sakoman printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", 268d2bf29e3SLei Wen start + blkcnt, mmc->block_dev.lba); 269d2bf29e3SLei Wen return 0; 270d2bf29e3SLei Wen } 271272cc70bSAndy Fleming 272272cc70bSAndy Fleming if (blkcnt > 1) 273272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; 274272cc70bSAndy Fleming else 275272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; 276272cc70bSAndy Fleming 277272cc70bSAndy Fleming if (mmc->high_capacity) 278272cc70bSAndy Fleming cmd.cmdarg = start; 279272cc70bSAndy Fleming else 280def412b6SSteve Sakoman cmd.cmdarg = start * mmc->write_bl_len; 281272cc70bSAndy Fleming 282272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 283272cc70bSAndy Fleming cmd.flags = 0; 284272cc70bSAndy Fleming 285272cc70bSAndy Fleming data.src = src; 286272cc70bSAndy Fleming data.blocks = blkcnt; 287def412b6SSteve Sakoman data.blocksize = mmc->write_bl_len; 288272cc70bSAndy Fleming data.flags = MMC_DATA_WRITE; 289272cc70bSAndy Fleming 290def412b6SSteve Sakoman if (mmc_send_cmd(mmc, &cmd, &data)) { 291def412b6SSteve Sakoman printf("mmc write failed\n"); 292def412b6SSteve Sakoman return 0; 293272cc70bSAndy Fleming } 294272cc70bSAndy Fleming 295d52ebf10SThomas Chou /* SPI multiblock writes terminate using a special 296d52ebf10SThomas Chou * token, not a STOP_TRANSMISSION request. 297d52ebf10SThomas Chou */ 298d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc) && blkcnt > 1) { 299272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 300272cc70bSAndy Fleming cmd.cmdarg = 0; 301272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 302272cc70bSAndy Fleming cmd.flags = 0; 303def412b6SSteve Sakoman if (mmc_send_cmd(mmc, &cmd, NULL)) { 304def412b6SSteve Sakoman printf("mmc fail to send stop cmd\n"); 305def412b6SSteve Sakoman return 0; 306272cc70bSAndy Fleming } 3075d4fc8d9SRaffaele Recalcati 3085d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 3095d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 3100158126eSLei Wen } 3110158126eSLei Wen 3120158126eSLei Wen return blkcnt; 3130158126eSLei Wen } 3140158126eSLei Wen 3150158126eSLei Wen static ulong 3160158126eSLei Wen mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src) 3170158126eSLei Wen { 3180158126eSLei Wen lbaint_t cur, blocks_todo = blkcnt; 3190158126eSLei Wen 320def412b6SSteve Sakoman struct mmc *mmc = find_mmc_device(dev_num); 3210158126eSLei Wen if (!mmc) 322def412b6SSteve Sakoman return 0; 3230158126eSLei Wen 324def412b6SSteve Sakoman if (mmc_set_blocklen(mmc, mmc->write_bl_len)) 325def412b6SSteve Sakoman return 0; 3260158126eSLei Wen 3270158126eSLei Wen do { 3288feafcc4SJohn Rigby cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo; 3290158126eSLei Wen if(mmc_write_blocks(mmc, start, cur, src) != cur) 330def412b6SSteve Sakoman return 0; 3310158126eSLei Wen blocks_todo -= cur; 3320158126eSLei Wen start += cur; 3330158126eSLei Wen src += cur * mmc->write_bl_len; 3340158126eSLei Wen } while (blocks_todo > 0); 335272cc70bSAndy Fleming 336272cc70bSAndy Fleming return blkcnt; 337272cc70bSAndy Fleming } 338272cc70bSAndy Fleming 3394a1a06bcSAlagu Sankar int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt) 340272cc70bSAndy Fleming { 341272cc70bSAndy Fleming struct mmc_cmd cmd; 342272cc70bSAndy Fleming struct mmc_data data; 3435d4fc8d9SRaffaele Recalcati int timeout = 1000; 344272cc70bSAndy Fleming 3454a1a06bcSAlagu Sankar if (blkcnt > 1) 3464a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; 3474a1a06bcSAlagu Sankar else 348272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; 349272cc70bSAndy Fleming 350272cc70bSAndy Fleming if (mmc->high_capacity) 3514a1a06bcSAlagu Sankar cmd.cmdarg = start; 352272cc70bSAndy Fleming else 3534a1a06bcSAlagu Sankar cmd.cmdarg = start * mmc->read_bl_len; 354272cc70bSAndy Fleming 355272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 356272cc70bSAndy Fleming cmd.flags = 0; 357272cc70bSAndy Fleming 358272cc70bSAndy Fleming data.dest = dst; 3594a1a06bcSAlagu Sankar data.blocks = blkcnt; 360272cc70bSAndy Fleming data.blocksize = mmc->read_bl_len; 361272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 362272cc70bSAndy Fleming 3634a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, &data)) 3644a1a06bcSAlagu Sankar return 0; 3654a1a06bcSAlagu Sankar 3664a1a06bcSAlagu Sankar if (blkcnt > 1) { 3674a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 3684a1a06bcSAlagu Sankar cmd.cmdarg = 0; 3694a1a06bcSAlagu Sankar cmd.resp_type = MMC_RSP_R1b; 3704a1a06bcSAlagu Sankar cmd.flags = 0; 3714a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, NULL)) { 3724a1a06bcSAlagu Sankar printf("mmc fail to send stop cmd\n"); 3734a1a06bcSAlagu Sankar return 0; 3744a1a06bcSAlagu Sankar } 3755d4fc8d9SRaffaele Recalcati 3765d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 3775d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 378272cc70bSAndy Fleming } 379272cc70bSAndy Fleming 3804a1a06bcSAlagu Sankar return blkcnt; 381272cc70bSAndy Fleming } 382272cc70bSAndy Fleming 383272cc70bSAndy Fleming static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst) 384272cc70bSAndy Fleming { 3854a1a06bcSAlagu Sankar lbaint_t cur, blocks_todo = blkcnt; 386272cc70bSAndy Fleming 3874a1a06bcSAlagu Sankar if (blkcnt == 0) 3884a1a06bcSAlagu Sankar return 0; 3894a1a06bcSAlagu Sankar 3904a1a06bcSAlagu Sankar struct mmc *mmc = find_mmc_device(dev_num); 391272cc70bSAndy Fleming if (!mmc) 392272cc70bSAndy Fleming return 0; 393272cc70bSAndy Fleming 394d2bf29e3SLei Wen if ((start + blkcnt) > mmc->block_dev.lba) { 3954a1a06bcSAlagu Sankar printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", 396d2bf29e3SLei Wen start + blkcnt, mmc->block_dev.lba); 397d2bf29e3SLei Wen return 0; 398d2bf29e3SLei Wen } 399272cc70bSAndy Fleming 4004a1a06bcSAlagu Sankar if (mmc_set_blocklen(mmc, mmc->read_bl_len)) 401272cc70bSAndy Fleming return 0; 402272cc70bSAndy Fleming 4034a1a06bcSAlagu Sankar do { 4048feafcc4SJohn Rigby cur = (blocks_todo > mmc->b_max) ? mmc->b_max : blocks_todo; 4054a1a06bcSAlagu Sankar if(mmc_read_blocks(mmc, dst, start, cur) != cur) 4064a1a06bcSAlagu Sankar return 0; 4074a1a06bcSAlagu Sankar blocks_todo -= cur; 4084a1a06bcSAlagu Sankar start += cur; 4094a1a06bcSAlagu Sankar dst += cur * mmc->read_bl_len; 4104a1a06bcSAlagu Sankar } while (blocks_todo > 0); 411272cc70bSAndy Fleming 412272cc70bSAndy Fleming return blkcnt; 413272cc70bSAndy Fleming } 414272cc70bSAndy Fleming 415272cc70bSAndy Fleming int mmc_go_idle(struct mmc* mmc) 416272cc70bSAndy Fleming { 417272cc70bSAndy Fleming struct mmc_cmd cmd; 418272cc70bSAndy Fleming int err; 419272cc70bSAndy Fleming 420272cc70bSAndy Fleming udelay(1000); 421272cc70bSAndy Fleming 422272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; 423272cc70bSAndy Fleming cmd.cmdarg = 0; 424272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_NONE; 425272cc70bSAndy Fleming cmd.flags = 0; 426272cc70bSAndy Fleming 427272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 428272cc70bSAndy Fleming 429272cc70bSAndy Fleming if (err) 430272cc70bSAndy Fleming return err; 431272cc70bSAndy Fleming 432272cc70bSAndy Fleming udelay(2000); 433272cc70bSAndy Fleming 434272cc70bSAndy Fleming return 0; 435272cc70bSAndy Fleming } 436272cc70bSAndy Fleming 437272cc70bSAndy Fleming int 438272cc70bSAndy Fleming sd_send_op_cond(struct mmc *mmc) 439272cc70bSAndy Fleming { 440272cc70bSAndy Fleming int timeout = 1000; 441272cc70bSAndy Fleming int err; 442272cc70bSAndy Fleming struct mmc_cmd cmd; 443272cc70bSAndy Fleming 444272cc70bSAndy Fleming do { 445272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 446272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 447272cc70bSAndy Fleming cmd.cmdarg = 0; 448272cc70bSAndy Fleming cmd.flags = 0; 449272cc70bSAndy Fleming 450272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 451272cc70bSAndy Fleming 452272cc70bSAndy Fleming if (err) 453272cc70bSAndy Fleming return err; 454272cc70bSAndy Fleming 455272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; 456272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R3; 457250de12bSStefano Babic 458250de12bSStefano Babic /* 459250de12bSStefano Babic * Most cards do not answer if some reserved bits 460250de12bSStefano Babic * in the ocr are set. However, Some controller 461250de12bSStefano Babic * can set bit 7 (reserved for low voltages), but 462250de12bSStefano Babic * how to manage low voltages SD card is not yet 463250de12bSStefano Babic * specified. 464250de12bSStefano Babic */ 465d52ebf10SThomas Chou cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 : 466d52ebf10SThomas Chou (mmc->voltages & 0xff8000); 467272cc70bSAndy Fleming 468272cc70bSAndy Fleming if (mmc->version == SD_VERSION_2) 469272cc70bSAndy Fleming cmd.cmdarg |= OCR_HCS; 470272cc70bSAndy Fleming 471272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 472272cc70bSAndy Fleming 473272cc70bSAndy Fleming if (err) 474272cc70bSAndy Fleming return err; 475272cc70bSAndy Fleming 476272cc70bSAndy Fleming udelay(1000); 477272cc70bSAndy Fleming } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--); 478272cc70bSAndy Fleming 479272cc70bSAndy Fleming if (timeout <= 0) 480272cc70bSAndy Fleming return UNUSABLE_ERR; 481272cc70bSAndy Fleming 482272cc70bSAndy Fleming if (mmc->version != SD_VERSION_2) 483272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 484272cc70bSAndy Fleming 485d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 486d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 487d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 488d52ebf10SThomas Chou cmd.cmdarg = 0; 489d52ebf10SThomas Chou cmd.flags = 0; 490d52ebf10SThomas Chou 491d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 492d52ebf10SThomas Chou 493d52ebf10SThomas Chou if (err) 494d52ebf10SThomas Chou return err; 495d52ebf10SThomas Chou } 496d52ebf10SThomas Chou 497998be3ddSRabin Vincent mmc->ocr = cmd.response[0]; 498272cc70bSAndy Fleming 499272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 500272cc70bSAndy Fleming mmc->rca = 0; 501272cc70bSAndy Fleming 502272cc70bSAndy Fleming return 0; 503272cc70bSAndy Fleming } 504272cc70bSAndy Fleming 505272cc70bSAndy Fleming int mmc_send_op_cond(struct mmc *mmc) 506272cc70bSAndy Fleming { 50731cacbabSRaffaele Recalcati int timeout = 10000; 508272cc70bSAndy Fleming struct mmc_cmd cmd; 509272cc70bSAndy Fleming int err; 510272cc70bSAndy Fleming 511272cc70bSAndy Fleming /* Some cards seem to need this */ 512272cc70bSAndy Fleming mmc_go_idle(mmc); 513272cc70bSAndy Fleming 51431cacbabSRaffaele Recalcati /* Asking to the card its capabilities */ 51531cacbabSRaffaele Recalcati cmd.cmdidx = MMC_CMD_SEND_OP_COND; 51631cacbabSRaffaele Recalcati cmd.resp_type = MMC_RSP_R3; 51731cacbabSRaffaele Recalcati cmd.cmdarg = 0; 51831cacbabSRaffaele Recalcati cmd.flags = 0; 51931cacbabSRaffaele Recalcati 52031cacbabSRaffaele Recalcati err = mmc_send_cmd(mmc, &cmd, NULL); 52131cacbabSRaffaele Recalcati 52231cacbabSRaffaele Recalcati if (err) 52331cacbabSRaffaele Recalcati return err; 52431cacbabSRaffaele Recalcati 52531cacbabSRaffaele Recalcati udelay(1000); 52631cacbabSRaffaele Recalcati 527272cc70bSAndy Fleming do { 528272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_OP_COND; 529272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R3; 53031cacbabSRaffaele Recalcati cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 : 53131cacbabSRaffaele Recalcati (mmc->voltages & 53231cacbabSRaffaele Recalcati (cmd.response[0] & OCR_VOLTAGE_MASK)) | 53331cacbabSRaffaele Recalcati (cmd.response[0] & OCR_ACCESS_MODE)); 534272cc70bSAndy Fleming cmd.flags = 0; 535272cc70bSAndy Fleming 536272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 537272cc70bSAndy Fleming 538272cc70bSAndy Fleming if (err) 539272cc70bSAndy Fleming return err; 540272cc70bSAndy Fleming 541272cc70bSAndy Fleming udelay(1000); 542272cc70bSAndy Fleming } while (!(cmd.response[0] & OCR_BUSY) && timeout--); 543272cc70bSAndy Fleming 544272cc70bSAndy Fleming if (timeout <= 0) 545272cc70bSAndy Fleming return UNUSABLE_ERR; 546272cc70bSAndy Fleming 547d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 548d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 549d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 550d52ebf10SThomas Chou cmd.cmdarg = 0; 551d52ebf10SThomas Chou cmd.flags = 0; 552d52ebf10SThomas Chou 553d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 554d52ebf10SThomas Chou 555d52ebf10SThomas Chou if (err) 556d52ebf10SThomas Chou return err; 557d52ebf10SThomas Chou } 558d52ebf10SThomas Chou 559272cc70bSAndy Fleming mmc->version = MMC_VERSION_UNKNOWN; 560998be3ddSRabin Vincent mmc->ocr = cmd.response[0]; 561272cc70bSAndy Fleming 562272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 563272cc70bSAndy Fleming mmc->rca = 0; 564272cc70bSAndy Fleming 565272cc70bSAndy Fleming return 0; 566272cc70bSAndy Fleming } 567272cc70bSAndy Fleming 568272cc70bSAndy Fleming 569272cc70bSAndy Fleming int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd) 570272cc70bSAndy Fleming { 571272cc70bSAndy Fleming struct mmc_cmd cmd; 572272cc70bSAndy Fleming struct mmc_data data; 573272cc70bSAndy Fleming int err; 574272cc70bSAndy Fleming 575272cc70bSAndy Fleming /* Get the Card Status Register */ 576272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; 577272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 578272cc70bSAndy Fleming cmd.cmdarg = 0; 579272cc70bSAndy Fleming cmd.flags = 0; 580272cc70bSAndy Fleming 581272cc70bSAndy Fleming data.dest = ext_csd; 582272cc70bSAndy Fleming data.blocks = 1; 583272cc70bSAndy Fleming data.blocksize = 512; 584272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 585272cc70bSAndy Fleming 586272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 587272cc70bSAndy Fleming 588272cc70bSAndy Fleming return err; 589272cc70bSAndy Fleming } 590272cc70bSAndy Fleming 591272cc70bSAndy Fleming 592272cc70bSAndy Fleming int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) 593272cc70bSAndy Fleming { 594272cc70bSAndy Fleming struct mmc_cmd cmd; 5955d4fc8d9SRaffaele Recalcati int timeout = 1000; 5965d4fc8d9SRaffaele Recalcati int ret; 597272cc70bSAndy Fleming 598272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SWITCH; 599272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 600272cc70bSAndy Fleming cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 601272cc70bSAndy Fleming (index << 16) | 602272cc70bSAndy Fleming (value << 8); 603272cc70bSAndy Fleming cmd.flags = 0; 604272cc70bSAndy Fleming 6055d4fc8d9SRaffaele Recalcati ret = mmc_send_cmd(mmc, &cmd, NULL); 6065d4fc8d9SRaffaele Recalcati 6075d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 6085d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 6095d4fc8d9SRaffaele Recalcati 6105d4fc8d9SRaffaele Recalcati return ret; 6115d4fc8d9SRaffaele Recalcati 612272cc70bSAndy Fleming } 613272cc70bSAndy Fleming 614272cc70bSAndy Fleming int mmc_change_freq(struct mmc *mmc) 615272cc70bSAndy Fleming { 616272cc70bSAndy Fleming char ext_csd[512]; 617272cc70bSAndy Fleming char cardtype; 618272cc70bSAndy Fleming int err; 619272cc70bSAndy Fleming 620272cc70bSAndy Fleming mmc->card_caps = 0; 621272cc70bSAndy Fleming 622d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 623d52ebf10SThomas Chou return 0; 624d52ebf10SThomas Chou 625272cc70bSAndy Fleming /* Only version 4 supports high-speed */ 626272cc70bSAndy Fleming if (mmc->version < MMC_VERSION_4) 627272cc70bSAndy Fleming return 0; 628272cc70bSAndy Fleming 629272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_4BIT; 630272cc70bSAndy Fleming 631272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 632272cc70bSAndy Fleming 633272cc70bSAndy Fleming if (err) 634272cc70bSAndy Fleming return err; 635272cc70bSAndy Fleming 636272cc70bSAndy Fleming cardtype = ext_csd[196] & 0xf; 637272cc70bSAndy Fleming 638272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); 639272cc70bSAndy Fleming 640272cc70bSAndy Fleming if (err) 641272cc70bSAndy Fleming return err; 642272cc70bSAndy Fleming 643272cc70bSAndy Fleming /* Now check to see that it worked */ 644272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 645272cc70bSAndy Fleming 646272cc70bSAndy Fleming if (err) 647272cc70bSAndy Fleming return err; 648272cc70bSAndy Fleming 649272cc70bSAndy Fleming /* No high-speed support */ 650272cc70bSAndy Fleming if (!ext_csd[185]) 651272cc70bSAndy Fleming return 0; 652272cc70bSAndy Fleming 653272cc70bSAndy Fleming /* High Speed is set, there are two types: 52MHz and 26MHz */ 654272cc70bSAndy Fleming if (cardtype & MMC_HS_52MHZ) 655272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; 656272cc70bSAndy Fleming else 657272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 658272cc70bSAndy Fleming 659272cc70bSAndy Fleming return 0; 660272cc70bSAndy Fleming } 661272cc70bSAndy Fleming 662bc897b1dSLei Wen int mmc_switch_part(int dev_num, unsigned int part_num) 663bc897b1dSLei Wen { 664bc897b1dSLei Wen struct mmc *mmc = find_mmc_device(dev_num); 665bc897b1dSLei Wen 666bc897b1dSLei Wen if (!mmc) 667bc897b1dSLei Wen return -1; 668bc897b1dSLei Wen 669bc897b1dSLei Wen return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, 670bc897b1dSLei Wen (mmc->part_config & ~PART_ACCESS_MASK) 671bc897b1dSLei Wen | (part_num & PART_ACCESS_MASK)); 672bc897b1dSLei Wen } 673bc897b1dSLei Wen 674272cc70bSAndy Fleming int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) 675272cc70bSAndy Fleming { 676272cc70bSAndy Fleming struct mmc_cmd cmd; 677272cc70bSAndy Fleming struct mmc_data data; 678272cc70bSAndy Fleming 679272cc70bSAndy Fleming /* Switch the frequency */ 680272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SWITCH_FUNC; 681272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 682272cc70bSAndy Fleming cmd.cmdarg = (mode << 31) | 0xffffff; 683272cc70bSAndy Fleming cmd.cmdarg &= ~(0xf << (group * 4)); 684272cc70bSAndy Fleming cmd.cmdarg |= value << (group * 4); 685272cc70bSAndy Fleming cmd.flags = 0; 686272cc70bSAndy Fleming 687272cc70bSAndy Fleming data.dest = (char *)resp; 688272cc70bSAndy Fleming data.blocksize = 64; 689272cc70bSAndy Fleming data.blocks = 1; 690272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 691272cc70bSAndy Fleming 692272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, &data); 693272cc70bSAndy Fleming } 694272cc70bSAndy Fleming 695272cc70bSAndy Fleming 696272cc70bSAndy Fleming int sd_change_freq(struct mmc *mmc) 697272cc70bSAndy Fleming { 698272cc70bSAndy Fleming int err; 699272cc70bSAndy Fleming struct mmc_cmd cmd; 700272cc70bSAndy Fleming uint scr[2]; 701272cc70bSAndy Fleming uint switch_status[16]; 702272cc70bSAndy Fleming struct mmc_data data; 703272cc70bSAndy Fleming int timeout; 704272cc70bSAndy Fleming 705272cc70bSAndy Fleming mmc->card_caps = 0; 706272cc70bSAndy Fleming 707d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 708d52ebf10SThomas Chou return 0; 709d52ebf10SThomas Chou 710272cc70bSAndy Fleming /* Read the SCR to find out if this card supports higher speeds */ 711272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 712272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 713272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 714272cc70bSAndy Fleming cmd.flags = 0; 715272cc70bSAndy Fleming 716272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 717272cc70bSAndy Fleming 718272cc70bSAndy Fleming if (err) 719272cc70bSAndy Fleming return err; 720272cc70bSAndy Fleming 721272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_SCR; 722272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 723272cc70bSAndy Fleming cmd.cmdarg = 0; 724272cc70bSAndy Fleming cmd.flags = 0; 725272cc70bSAndy Fleming 726272cc70bSAndy Fleming timeout = 3; 727272cc70bSAndy Fleming 728272cc70bSAndy Fleming retry_scr: 729272cc70bSAndy Fleming data.dest = (char *)&scr; 730272cc70bSAndy Fleming data.blocksize = 8; 731272cc70bSAndy Fleming data.blocks = 1; 732272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 733272cc70bSAndy Fleming 734272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 735272cc70bSAndy Fleming 736272cc70bSAndy Fleming if (err) { 737272cc70bSAndy Fleming if (timeout--) 738272cc70bSAndy Fleming goto retry_scr; 739272cc70bSAndy Fleming 740272cc70bSAndy Fleming return err; 741272cc70bSAndy Fleming } 742272cc70bSAndy Fleming 7434e3d89baSYauhen Kharuzhy mmc->scr[0] = __be32_to_cpu(scr[0]); 7444e3d89baSYauhen Kharuzhy mmc->scr[1] = __be32_to_cpu(scr[1]); 745272cc70bSAndy Fleming 746272cc70bSAndy Fleming switch ((mmc->scr[0] >> 24) & 0xf) { 747272cc70bSAndy Fleming case 0: 748272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 749272cc70bSAndy Fleming break; 750272cc70bSAndy Fleming case 1: 751272cc70bSAndy Fleming mmc->version = SD_VERSION_1_10; 752272cc70bSAndy Fleming break; 753272cc70bSAndy Fleming case 2: 754272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 755272cc70bSAndy Fleming break; 756272cc70bSAndy Fleming default: 757272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 758272cc70bSAndy Fleming break; 759272cc70bSAndy Fleming } 760272cc70bSAndy Fleming 761b44c7083SAlagu Sankar if (mmc->scr[0] & SD_DATA_4BIT) 762b44c7083SAlagu Sankar mmc->card_caps |= MMC_MODE_4BIT; 763b44c7083SAlagu Sankar 764272cc70bSAndy Fleming /* Version 1.0 doesn't support switching */ 765272cc70bSAndy Fleming if (mmc->version == SD_VERSION_1_0) 766272cc70bSAndy Fleming return 0; 767272cc70bSAndy Fleming 768272cc70bSAndy Fleming timeout = 4; 769272cc70bSAndy Fleming while (timeout--) { 770272cc70bSAndy Fleming err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, 771272cc70bSAndy Fleming (u8 *)&switch_status); 772272cc70bSAndy Fleming 773272cc70bSAndy Fleming if (err) 774272cc70bSAndy Fleming return err; 775272cc70bSAndy Fleming 776272cc70bSAndy Fleming /* The high-speed function is busy. Try again */ 7774e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) 778272cc70bSAndy Fleming break; 779272cc70bSAndy Fleming } 780272cc70bSAndy Fleming 781272cc70bSAndy Fleming /* If high-speed isn't supported, we return */ 7824e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) 783272cc70bSAndy Fleming return 0; 784272cc70bSAndy Fleming 785272cc70bSAndy Fleming err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status); 786272cc70bSAndy Fleming 787272cc70bSAndy Fleming if (err) 788272cc70bSAndy Fleming return err; 789272cc70bSAndy Fleming 7904e3d89baSYauhen Kharuzhy if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) 791272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 792272cc70bSAndy Fleming 793272cc70bSAndy Fleming return 0; 794272cc70bSAndy Fleming } 795272cc70bSAndy Fleming 796272cc70bSAndy Fleming /* frequency bases */ 797272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */ 7985f837c2cSMike Frysinger static const int fbase[] = { 799272cc70bSAndy Fleming 10000, 800272cc70bSAndy Fleming 100000, 801272cc70bSAndy Fleming 1000000, 802272cc70bSAndy Fleming 10000000, 803272cc70bSAndy Fleming }; 804272cc70bSAndy Fleming 805272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice 806272cc70bSAndy Fleming * to platforms without floating point. 807272cc70bSAndy Fleming */ 8085f837c2cSMike Frysinger static const int multipliers[] = { 809272cc70bSAndy Fleming 0, /* reserved */ 810272cc70bSAndy Fleming 10, 811272cc70bSAndy Fleming 12, 812272cc70bSAndy Fleming 13, 813272cc70bSAndy Fleming 15, 814272cc70bSAndy Fleming 20, 815272cc70bSAndy Fleming 25, 816272cc70bSAndy Fleming 30, 817272cc70bSAndy Fleming 35, 818272cc70bSAndy Fleming 40, 819272cc70bSAndy Fleming 45, 820272cc70bSAndy Fleming 50, 821272cc70bSAndy Fleming 55, 822272cc70bSAndy Fleming 60, 823272cc70bSAndy Fleming 70, 824272cc70bSAndy Fleming 80, 825272cc70bSAndy Fleming }; 826272cc70bSAndy Fleming 827272cc70bSAndy Fleming void mmc_set_ios(struct mmc *mmc) 828272cc70bSAndy Fleming { 829272cc70bSAndy Fleming mmc->set_ios(mmc); 830272cc70bSAndy Fleming } 831272cc70bSAndy Fleming 832272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock) 833272cc70bSAndy Fleming { 834272cc70bSAndy Fleming if (clock > mmc->f_max) 835272cc70bSAndy Fleming clock = mmc->f_max; 836272cc70bSAndy Fleming 837272cc70bSAndy Fleming if (clock < mmc->f_min) 838272cc70bSAndy Fleming clock = mmc->f_min; 839272cc70bSAndy Fleming 840272cc70bSAndy Fleming mmc->clock = clock; 841272cc70bSAndy Fleming 842272cc70bSAndy Fleming mmc_set_ios(mmc); 843272cc70bSAndy Fleming } 844272cc70bSAndy Fleming 845272cc70bSAndy Fleming void mmc_set_bus_width(struct mmc *mmc, uint width) 846272cc70bSAndy Fleming { 847272cc70bSAndy Fleming mmc->bus_width = width; 848272cc70bSAndy Fleming 849272cc70bSAndy Fleming mmc_set_ios(mmc); 850272cc70bSAndy Fleming } 851272cc70bSAndy Fleming 852272cc70bSAndy Fleming int mmc_startup(struct mmc *mmc) 853272cc70bSAndy Fleming { 854272cc70bSAndy Fleming int err; 855272cc70bSAndy Fleming uint mult, freq; 856272cc70bSAndy Fleming u64 cmult, csize; 857272cc70bSAndy Fleming struct mmc_cmd cmd; 858d23e2c09SSukumar Ghorai char ext_csd[512]; 8595d4fc8d9SRaffaele Recalcati int timeout = 1000; 860272cc70bSAndy Fleming 861d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON 862d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ 863d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF; 864d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R1; 865d52ebf10SThomas Chou cmd.cmdarg = 1; 866d52ebf10SThomas Chou cmd.flags = 0; 867d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 868d52ebf10SThomas Chou 869d52ebf10SThomas Chou if (err) 870d52ebf10SThomas Chou return err; 871d52ebf10SThomas Chou } 872d52ebf10SThomas Chou #endif 873d52ebf10SThomas Chou 874272cc70bSAndy Fleming /* Put the Card in Identify Mode */ 875d52ebf10SThomas Chou cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID : 876d52ebf10SThomas Chou MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */ 877272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 878272cc70bSAndy Fleming cmd.cmdarg = 0; 879272cc70bSAndy Fleming cmd.flags = 0; 880272cc70bSAndy Fleming 881272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 882272cc70bSAndy Fleming 883272cc70bSAndy Fleming if (err) 884272cc70bSAndy Fleming return err; 885272cc70bSAndy Fleming 886272cc70bSAndy Fleming memcpy(mmc->cid, cmd.response, 16); 887272cc70bSAndy Fleming 888272cc70bSAndy Fleming /* 889272cc70bSAndy Fleming * For MMC cards, set the Relative Address. 890272cc70bSAndy Fleming * For SD cards, get the Relatvie Address. 891272cc70bSAndy Fleming * This also puts the cards into Standby State 892272cc70bSAndy Fleming */ 893d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 894272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; 895272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 896272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R6; 897272cc70bSAndy Fleming cmd.flags = 0; 898272cc70bSAndy Fleming 899272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 900272cc70bSAndy Fleming 901272cc70bSAndy Fleming if (err) 902272cc70bSAndy Fleming return err; 903272cc70bSAndy Fleming 904272cc70bSAndy Fleming if (IS_SD(mmc)) 905998be3ddSRabin Vincent mmc->rca = (cmd.response[0] >> 16) & 0xffff; 906d52ebf10SThomas Chou } 907272cc70bSAndy Fleming 908272cc70bSAndy Fleming /* Get the Card-Specific Data */ 909272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_CSD; 910272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 911272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 912272cc70bSAndy Fleming cmd.flags = 0; 913272cc70bSAndy Fleming 914272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 915272cc70bSAndy Fleming 9165d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 9175d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 9185d4fc8d9SRaffaele Recalcati 919272cc70bSAndy Fleming if (err) 920272cc70bSAndy Fleming return err; 921272cc70bSAndy Fleming 922998be3ddSRabin Vincent mmc->csd[0] = cmd.response[0]; 923998be3ddSRabin Vincent mmc->csd[1] = cmd.response[1]; 924998be3ddSRabin Vincent mmc->csd[2] = cmd.response[2]; 925998be3ddSRabin Vincent mmc->csd[3] = cmd.response[3]; 926272cc70bSAndy Fleming 927272cc70bSAndy Fleming if (mmc->version == MMC_VERSION_UNKNOWN) { 9280b453ffeSRabin Vincent int version = (cmd.response[0] >> 26) & 0xf; 929272cc70bSAndy Fleming 930272cc70bSAndy Fleming switch (version) { 931272cc70bSAndy Fleming case 0: 932272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 933272cc70bSAndy Fleming break; 934272cc70bSAndy Fleming case 1: 935272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_4; 936272cc70bSAndy Fleming break; 937272cc70bSAndy Fleming case 2: 938272cc70bSAndy Fleming mmc->version = MMC_VERSION_2_2; 939272cc70bSAndy Fleming break; 940272cc70bSAndy Fleming case 3: 941272cc70bSAndy Fleming mmc->version = MMC_VERSION_3; 942272cc70bSAndy Fleming break; 943272cc70bSAndy Fleming case 4: 944272cc70bSAndy Fleming mmc->version = MMC_VERSION_4; 945272cc70bSAndy Fleming break; 946272cc70bSAndy Fleming default: 947272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 948272cc70bSAndy Fleming break; 949272cc70bSAndy Fleming } 950272cc70bSAndy Fleming } 951272cc70bSAndy Fleming 952272cc70bSAndy Fleming /* divide frequency by 10, since the mults are 10x bigger */ 9530b453ffeSRabin Vincent freq = fbase[(cmd.response[0] & 0x7)]; 9540b453ffeSRabin Vincent mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; 955272cc70bSAndy Fleming 956272cc70bSAndy Fleming mmc->tran_speed = freq * mult; 957272cc70bSAndy Fleming 958998be3ddSRabin Vincent mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); 959272cc70bSAndy Fleming 960272cc70bSAndy Fleming if (IS_SD(mmc)) 961272cc70bSAndy Fleming mmc->write_bl_len = mmc->read_bl_len; 962272cc70bSAndy Fleming else 963998be3ddSRabin Vincent mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); 964272cc70bSAndy Fleming 965272cc70bSAndy Fleming if (mmc->high_capacity) { 966272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3f) << 16 967272cc70bSAndy Fleming | (mmc->csd[2] & 0xffff0000) >> 16; 968272cc70bSAndy Fleming cmult = 8; 969272cc70bSAndy Fleming } else { 970272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3ff) << 2 971272cc70bSAndy Fleming | (mmc->csd[2] & 0xc0000000) >> 30; 972272cc70bSAndy Fleming cmult = (mmc->csd[2] & 0x00038000) >> 15; 973272cc70bSAndy Fleming } 974272cc70bSAndy Fleming 975272cc70bSAndy Fleming mmc->capacity = (csize + 1) << (cmult + 2); 976272cc70bSAndy Fleming mmc->capacity *= mmc->read_bl_len; 977272cc70bSAndy Fleming 978272cc70bSAndy Fleming if (mmc->read_bl_len > 512) 979272cc70bSAndy Fleming mmc->read_bl_len = 512; 980272cc70bSAndy Fleming 981272cc70bSAndy Fleming if (mmc->write_bl_len > 512) 982272cc70bSAndy Fleming mmc->write_bl_len = 512; 983272cc70bSAndy Fleming 984272cc70bSAndy Fleming /* Select the card, and put it into Transfer Mode */ 985d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 986272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SELECT_CARD; 987272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 988272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 989272cc70bSAndy Fleming cmd.flags = 0; 990272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 991272cc70bSAndy Fleming 992272cc70bSAndy Fleming if (err) 993272cc70bSAndy Fleming return err; 994d52ebf10SThomas Chou } 995272cc70bSAndy Fleming 996*e6f99a56SLei Wen /* 997*e6f99a56SLei Wen * For SD, its erase group is always one sector 998*e6f99a56SLei Wen */ 999*e6f99a56SLei Wen mmc->erase_grp_size = 1; 1000bc897b1dSLei Wen mmc->part_config = MMCPART_NOAVAILABLE; 1001d23e2c09SSukumar Ghorai if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) { 1002d23e2c09SSukumar Ghorai /* check ext_csd version and capacity */ 1003d23e2c09SSukumar Ghorai err = mmc_send_ext_csd(mmc, ext_csd); 1004d23e2c09SSukumar Ghorai if (!err & (ext_csd[192] >= 2)) { 1005d23e2c09SSukumar Ghorai mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 | 1006d23e2c09SSukumar Ghorai ext_csd[214] << 16 | ext_csd[215] << 24; 1007d23e2c09SSukumar Ghorai mmc->capacity *= 512; 1008d23e2c09SSukumar Ghorai } 1009bc897b1dSLei Wen 1010*e6f99a56SLei Wen /* 1011*e6f99a56SLei Wen * Check whether GROUP_DEF is set, if yes, read out 1012*e6f99a56SLei Wen * group size from ext_csd directly, or calculate 1013*e6f99a56SLei Wen * the group size from the csd value. 1014*e6f99a56SLei Wen */ 1015*e6f99a56SLei Wen if (ext_csd[175]) 1016*e6f99a56SLei Wen mmc->erase_grp_size = ext_csd[224] * 512 * 1024; 1017*e6f99a56SLei Wen else { 1018*e6f99a56SLei Wen int erase_gsz, erase_gmul; 1019*e6f99a56SLei Wen erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10; 1020*e6f99a56SLei Wen erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5; 1021*e6f99a56SLei Wen mmc->erase_grp_size = (erase_gsz + 1) 1022*e6f99a56SLei Wen * (erase_gmul + 1); 1023*e6f99a56SLei Wen } 1024*e6f99a56SLei Wen 1025bc897b1dSLei Wen /* store the partition info of emmc */ 1026bc897b1dSLei Wen if (ext_csd[160] & PART_SUPPORT) 1027bc897b1dSLei Wen mmc->part_config = ext_csd[179]; 1028d23e2c09SSukumar Ghorai } 1029d23e2c09SSukumar Ghorai 1030272cc70bSAndy Fleming if (IS_SD(mmc)) 1031272cc70bSAndy Fleming err = sd_change_freq(mmc); 1032272cc70bSAndy Fleming else 1033272cc70bSAndy Fleming err = mmc_change_freq(mmc); 1034272cc70bSAndy Fleming 1035272cc70bSAndy Fleming if (err) 1036272cc70bSAndy Fleming return err; 1037272cc70bSAndy Fleming 1038272cc70bSAndy Fleming /* Restrict card's capabilities by what the host can do */ 1039272cc70bSAndy Fleming mmc->card_caps &= mmc->host_caps; 1040272cc70bSAndy Fleming 1041272cc70bSAndy Fleming if (IS_SD(mmc)) { 1042272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_4BIT) { 1043272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 1044272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 1045272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1046272cc70bSAndy Fleming cmd.flags = 0; 1047272cc70bSAndy Fleming 1048272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1049272cc70bSAndy Fleming if (err) 1050272cc70bSAndy Fleming return err; 1051272cc70bSAndy Fleming 1052272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; 1053272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 1054272cc70bSAndy Fleming cmd.cmdarg = 2; 1055272cc70bSAndy Fleming cmd.flags = 0; 1056272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1057272cc70bSAndy Fleming if (err) 1058272cc70bSAndy Fleming return err; 1059272cc70bSAndy Fleming 1060272cc70bSAndy Fleming mmc_set_bus_width(mmc, 4); 1061272cc70bSAndy Fleming } 1062272cc70bSAndy Fleming 1063272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) 1064272cc70bSAndy Fleming mmc_set_clock(mmc, 50000000); 1065272cc70bSAndy Fleming else 1066272cc70bSAndy Fleming mmc_set_clock(mmc, 25000000); 1067272cc70bSAndy Fleming } else { 1068272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_4BIT) { 1069272cc70bSAndy Fleming /* Set the card to use 4 bit*/ 1070272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 1071272cc70bSAndy Fleming EXT_CSD_BUS_WIDTH, 1072272cc70bSAndy Fleming EXT_CSD_BUS_WIDTH_4); 1073272cc70bSAndy Fleming 1074272cc70bSAndy Fleming if (err) 1075272cc70bSAndy Fleming return err; 1076272cc70bSAndy Fleming 1077272cc70bSAndy Fleming mmc_set_bus_width(mmc, 4); 1078272cc70bSAndy Fleming } else if (mmc->card_caps & MMC_MODE_8BIT) { 1079272cc70bSAndy Fleming /* Set the card to use 8 bit*/ 1080272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 1081272cc70bSAndy Fleming EXT_CSD_BUS_WIDTH, 1082272cc70bSAndy Fleming EXT_CSD_BUS_WIDTH_8); 1083272cc70bSAndy Fleming 1084272cc70bSAndy Fleming if (err) 1085272cc70bSAndy Fleming return err; 1086272cc70bSAndy Fleming 1087272cc70bSAndy Fleming mmc_set_bus_width(mmc, 8); 1088272cc70bSAndy Fleming } 1089272cc70bSAndy Fleming 1090272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) { 1091272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS_52MHz) 1092272cc70bSAndy Fleming mmc_set_clock(mmc, 52000000); 1093272cc70bSAndy Fleming else 1094272cc70bSAndy Fleming mmc_set_clock(mmc, 26000000); 1095272cc70bSAndy Fleming } else 1096272cc70bSAndy Fleming mmc_set_clock(mmc, 20000000); 1097272cc70bSAndy Fleming } 1098272cc70bSAndy Fleming 1099272cc70bSAndy Fleming /* fill in device description */ 1100272cc70bSAndy Fleming mmc->block_dev.lun = 0; 1101272cc70bSAndy Fleming mmc->block_dev.type = 0; 1102272cc70bSAndy Fleming mmc->block_dev.blksz = mmc->read_bl_len; 11039b1f942cSRabin Vincent mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); 11040b453ffeSRabin Vincent sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8, 11050b453ffeSRabin Vincent (mmc->cid[2] << 8) | (mmc->cid[3] >> 24)); 11060b453ffeSRabin Vincent sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff, 11070b453ffeSRabin Vincent (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, 11080b453ffeSRabin Vincent (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); 11090b453ffeSRabin Vincent sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28, 11100b453ffeSRabin Vincent (mmc->cid[2] >> 24) & 0xf); 1111272cc70bSAndy Fleming init_part(&mmc->block_dev); 1112272cc70bSAndy Fleming 1113272cc70bSAndy Fleming return 0; 1114272cc70bSAndy Fleming } 1115272cc70bSAndy Fleming 1116272cc70bSAndy Fleming int mmc_send_if_cond(struct mmc *mmc) 1117272cc70bSAndy Fleming { 1118272cc70bSAndy Fleming struct mmc_cmd cmd; 1119272cc70bSAndy Fleming int err; 1120272cc70bSAndy Fleming 1121272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_IF_COND; 1122272cc70bSAndy Fleming /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ 1123272cc70bSAndy Fleming cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa; 1124272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R7; 1125272cc70bSAndy Fleming cmd.flags = 0; 1126272cc70bSAndy Fleming 1127272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1128272cc70bSAndy Fleming 1129272cc70bSAndy Fleming if (err) 1130272cc70bSAndy Fleming return err; 1131272cc70bSAndy Fleming 1132998be3ddSRabin Vincent if ((cmd.response[0] & 0xff) != 0xaa) 1133272cc70bSAndy Fleming return UNUSABLE_ERR; 1134272cc70bSAndy Fleming else 1135272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 1136272cc70bSAndy Fleming 1137272cc70bSAndy Fleming return 0; 1138272cc70bSAndy Fleming } 1139272cc70bSAndy Fleming 1140272cc70bSAndy Fleming int mmc_register(struct mmc *mmc) 1141272cc70bSAndy Fleming { 1142272cc70bSAndy Fleming /* Setup the universal parts of the block interface just once */ 1143272cc70bSAndy Fleming mmc->block_dev.if_type = IF_TYPE_MMC; 1144272cc70bSAndy Fleming mmc->block_dev.dev = cur_dev_num++; 1145272cc70bSAndy Fleming mmc->block_dev.removable = 1; 1146272cc70bSAndy Fleming mmc->block_dev.block_read = mmc_bread; 1147272cc70bSAndy Fleming mmc->block_dev.block_write = mmc_bwrite; 1148*e6f99a56SLei Wen mmc->block_dev.block_erase = mmc_berase; 11498feafcc4SJohn Rigby if (!mmc->b_max) 11508feafcc4SJohn Rigby mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; 1151272cc70bSAndy Fleming 1152272cc70bSAndy Fleming INIT_LIST_HEAD (&mmc->link); 1153272cc70bSAndy Fleming 1154272cc70bSAndy Fleming list_add_tail (&mmc->link, &mmc_devices); 1155272cc70bSAndy Fleming 1156272cc70bSAndy Fleming return 0; 1157272cc70bSAndy Fleming } 1158272cc70bSAndy Fleming 1159272cc70bSAndy Fleming block_dev_desc_t *mmc_get_dev(int dev) 1160272cc70bSAndy Fleming { 1161272cc70bSAndy Fleming struct mmc *mmc = find_mmc_device(dev); 1162272cc70bSAndy Fleming 1163e85649c7SRabin Vincent return mmc ? &mmc->block_dev : NULL; 1164272cc70bSAndy Fleming } 1165272cc70bSAndy Fleming 1166272cc70bSAndy Fleming int mmc_init(struct mmc *mmc) 1167272cc70bSAndy Fleming { 1168272cc70bSAndy Fleming int err; 1169272cc70bSAndy Fleming 1170bc897b1dSLei Wen if (mmc->has_init) 1171bc897b1dSLei Wen return 0; 1172bc897b1dSLei Wen 1173272cc70bSAndy Fleming err = mmc->init(mmc); 1174272cc70bSAndy Fleming 1175272cc70bSAndy Fleming if (err) 1176272cc70bSAndy Fleming return err; 1177272cc70bSAndy Fleming 1178b86b85e2SIlya Yanok mmc_set_bus_width(mmc, 1); 1179b86b85e2SIlya Yanok mmc_set_clock(mmc, 1); 1180b86b85e2SIlya Yanok 1181272cc70bSAndy Fleming /* Reset the Card */ 1182272cc70bSAndy Fleming err = mmc_go_idle(mmc); 1183272cc70bSAndy Fleming 1184272cc70bSAndy Fleming if (err) 1185272cc70bSAndy Fleming return err; 1186272cc70bSAndy Fleming 1187bc897b1dSLei Wen /* The internal partition reset to user partition(0) at every CMD0*/ 1188bc897b1dSLei Wen mmc->part_num = 0; 1189bc897b1dSLei Wen 1190272cc70bSAndy Fleming /* Test for SD version 2 */ 1191272cc70bSAndy Fleming err = mmc_send_if_cond(mmc); 1192272cc70bSAndy Fleming 1193272cc70bSAndy Fleming /* Now try to get the SD card's operating condition */ 1194272cc70bSAndy Fleming err = sd_send_op_cond(mmc); 1195272cc70bSAndy Fleming 1196272cc70bSAndy Fleming /* If the command timed out, we check for an MMC card */ 1197272cc70bSAndy Fleming if (err == TIMEOUT) { 1198272cc70bSAndy Fleming err = mmc_send_op_cond(mmc); 1199272cc70bSAndy Fleming 1200272cc70bSAndy Fleming if (err) { 1201272cc70bSAndy Fleming printf("Card did not respond to voltage select!\n"); 1202272cc70bSAndy Fleming return UNUSABLE_ERR; 1203272cc70bSAndy Fleming } 1204272cc70bSAndy Fleming } 1205272cc70bSAndy Fleming 1206bc897b1dSLei Wen err = mmc_startup(mmc); 1207bc897b1dSLei Wen if (err) 1208bc897b1dSLei Wen mmc->has_init = 0; 1209bc897b1dSLei Wen else 1210bc897b1dSLei Wen mmc->has_init = 1; 1211bc897b1dSLei Wen return err; 1212272cc70bSAndy Fleming } 1213272cc70bSAndy Fleming 1214272cc70bSAndy Fleming /* 1215272cc70bSAndy Fleming * CPU and board-specific MMC initializations. Aliased function 1216272cc70bSAndy Fleming * signals caller to move on 1217272cc70bSAndy Fleming */ 1218272cc70bSAndy Fleming static int __def_mmc_init(bd_t *bis) 1219272cc70bSAndy Fleming { 1220272cc70bSAndy Fleming return -1; 1221272cc70bSAndy Fleming } 1222272cc70bSAndy Fleming 1223f9a109b3SPeter Tyser int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); 1224f9a109b3SPeter Tyser int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); 1225272cc70bSAndy Fleming 1226272cc70bSAndy Fleming void print_mmc_devices(char separator) 1227272cc70bSAndy Fleming { 1228272cc70bSAndy Fleming struct mmc *m; 1229272cc70bSAndy Fleming struct list_head *entry; 1230272cc70bSAndy Fleming 1231272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 1232272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 1233272cc70bSAndy Fleming 1234272cc70bSAndy Fleming printf("%s: %d", m->name, m->block_dev.dev); 1235272cc70bSAndy Fleming 1236272cc70bSAndy Fleming if (entry->next != &mmc_devices) 1237272cc70bSAndy Fleming printf("%c ", separator); 1238272cc70bSAndy Fleming } 1239272cc70bSAndy Fleming 1240272cc70bSAndy Fleming printf("\n"); 1241272cc70bSAndy Fleming } 1242272cc70bSAndy Fleming 1243ea6ebe21SLei Wen int get_mmc_num(void) 1244ea6ebe21SLei Wen { 1245ea6ebe21SLei Wen return cur_dev_num; 1246ea6ebe21SLei Wen } 1247ea6ebe21SLei Wen 1248272cc70bSAndy Fleming int mmc_initialize(bd_t *bis) 1249272cc70bSAndy Fleming { 1250272cc70bSAndy Fleming INIT_LIST_HEAD (&mmc_devices); 1251272cc70bSAndy Fleming cur_dev_num = 0; 1252272cc70bSAndy Fleming 1253272cc70bSAndy Fleming if (board_mmc_init(bis) < 0) 1254272cc70bSAndy Fleming cpu_mmc_init(bis); 1255272cc70bSAndy Fleming 1256272cc70bSAndy Fleming print_mmc_devices(','); 1257272cc70bSAndy Fleming 1258272cc70bSAndy Fleming return 0; 1259272cc70bSAndy Fleming } 1260