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 * 71a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 8272cc70bSAndy Fleming */ 9272cc70bSAndy Fleming 10272cc70bSAndy Fleming #include <config.h> 11272cc70bSAndy Fleming #include <common.h> 12272cc70bSAndy Fleming #include <command.h> 138e3332e2SSjoerd Simons #include <dm.h> 148e3332e2SSjoerd Simons #include <dm/device-internal.h> 15d4622df3SStephen Warren #include <errno.h> 16272cc70bSAndy Fleming #include <mmc.h> 17272cc70bSAndy Fleming #include <part.h> 18272cc70bSAndy Fleming #include <malloc.h> 19cf92e05cSSimon Glass #include <memalign.h> 20272cc70bSAndy Fleming #include <linux/list.h> 219b1f942cSRabin Vincent #include <div64.h> 22da61fa5fSPaul Burton #include "mmc_private.h" 23272cc70bSAndy Fleming 24272cc70bSAndy Fleming static struct list_head mmc_devices; 25272cc70bSAndy Fleming static int cur_dev_num = -1; 26272cc70bSAndy Fleming 27750121c3SJeroen Hofstee __weak int board_mmc_getwp(struct mmc *mmc) 28d23d8d7eSNikita Kiryanov { 29d23d8d7eSNikita Kiryanov return -1; 30d23d8d7eSNikita Kiryanov } 31d23d8d7eSNikita Kiryanov 32d23d8d7eSNikita Kiryanov int mmc_getwp(struct mmc *mmc) 33d23d8d7eSNikita Kiryanov { 34d23d8d7eSNikita Kiryanov int wp; 35d23d8d7eSNikita Kiryanov 36d23d8d7eSNikita Kiryanov wp = board_mmc_getwp(mmc); 37d23d8d7eSNikita Kiryanov 38d4e1da4eSPeter Korsgaard if (wp < 0) { 3993bfd616SPantelis Antoniou if (mmc->cfg->ops->getwp) 4093bfd616SPantelis Antoniou wp = mmc->cfg->ops->getwp(mmc); 41d4e1da4eSPeter Korsgaard else 42d4e1da4eSPeter Korsgaard wp = 0; 43d4e1da4eSPeter Korsgaard } 44d23d8d7eSNikita Kiryanov 45d23d8d7eSNikita Kiryanov return wp; 46d23d8d7eSNikita Kiryanov } 47d23d8d7eSNikita Kiryanov 48cee9ab7cSJeroen Hofstee __weak int board_mmc_getcd(struct mmc *mmc) 49cee9ab7cSJeroen Hofstee { 5011fdade2SStefano Babic return -1; 5111fdade2SStefano Babic } 5211fdade2SStefano Babic 53da61fa5fSPaul Burton int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) 54272cc70bSAndy Fleming { 555db2fe3aSRaffaele Recalcati int ret; 568635ff9eSMarek Vasut 578635ff9eSMarek Vasut #ifdef CONFIG_MMC_TRACE 585db2fe3aSRaffaele Recalcati int i; 595db2fe3aSRaffaele Recalcati u8 *ptr; 605db2fe3aSRaffaele Recalcati 615db2fe3aSRaffaele Recalcati printf("CMD_SEND:%d\n", cmd->cmdidx); 625db2fe3aSRaffaele Recalcati printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg); 6393bfd616SPantelis Antoniou ret = mmc->cfg->ops->send_cmd(mmc, cmd, data); 647863ce58SBin Meng if (ret) { 657863ce58SBin Meng printf("\t\tRET\t\t\t %d\n", ret); 667863ce58SBin Meng } else { 675db2fe3aSRaffaele Recalcati switch (cmd->resp_type) { 685db2fe3aSRaffaele Recalcati case MMC_RSP_NONE: 695db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_NONE\n"); 705db2fe3aSRaffaele Recalcati break; 715db2fe3aSRaffaele Recalcati case MMC_RSP_R1: 725db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n", 735db2fe3aSRaffaele Recalcati cmd->response[0]); 745db2fe3aSRaffaele Recalcati break; 755db2fe3aSRaffaele Recalcati case MMC_RSP_R1b: 765db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n", 775db2fe3aSRaffaele Recalcati cmd->response[0]); 785db2fe3aSRaffaele Recalcati break; 795db2fe3aSRaffaele Recalcati case MMC_RSP_R2: 805db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R2\t\t 0x%08X \n", 815db2fe3aSRaffaele Recalcati cmd->response[0]); 825db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 835db2fe3aSRaffaele Recalcati cmd->response[1]); 845db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 855db2fe3aSRaffaele Recalcati cmd->response[2]); 865db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 875db2fe3aSRaffaele Recalcati cmd->response[3]); 885db2fe3aSRaffaele Recalcati printf("\n"); 895db2fe3aSRaffaele Recalcati printf("\t\t\t\t\tDUMPING DATA\n"); 905db2fe3aSRaffaele Recalcati for (i = 0; i < 4; i++) { 915db2fe3aSRaffaele Recalcati int j; 925db2fe3aSRaffaele Recalcati printf("\t\t\t\t\t%03d - ", i*4); 93146bec79SDirk Behme ptr = (u8 *)&cmd->response[i]; 945db2fe3aSRaffaele Recalcati ptr += 3; 955db2fe3aSRaffaele Recalcati for (j = 0; j < 4; j++) 965db2fe3aSRaffaele Recalcati printf("%02X ", *ptr--); 975db2fe3aSRaffaele Recalcati printf("\n"); 985db2fe3aSRaffaele Recalcati } 995db2fe3aSRaffaele Recalcati break; 1005db2fe3aSRaffaele Recalcati case MMC_RSP_R3: 1015db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n", 1025db2fe3aSRaffaele Recalcati cmd->response[0]); 1035db2fe3aSRaffaele Recalcati break; 1045db2fe3aSRaffaele Recalcati default: 1055db2fe3aSRaffaele Recalcati printf("\t\tERROR MMC rsp not supported\n"); 1065db2fe3aSRaffaele Recalcati break; 1075db2fe3aSRaffaele Recalcati } 1087863ce58SBin Meng } 1095db2fe3aSRaffaele Recalcati #else 11093bfd616SPantelis Antoniou ret = mmc->cfg->ops->send_cmd(mmc, cmd, data); 1115db2fe3aSRaffaele Recalcati #endif 1128635ff9eSMarek Vasut return ret; 113272cc70bSAndy Fleming } 114272cc70bSAndy Fleming 115da61fa5fSPaul Burton int mmc_send_status(struct mmc *mmc, int timeout) 1165d4fc8d9SRaffaele Recalcati { 1175d4fc8d9SRaffaele Recalcati struct mmc_cmd cmd; 118d617c426SJan Kloetzke int err, retries = 5; 1195d4fc8d9SRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 1205d4fc8d9SRaffaele Recalcati int status; 1215d4fc8d9SRaffaele Recalcati #endif 1225d4fc8d9SRaffaele Recalcati 1235d4fc8d9SRaffaele Recalcati cmd.cmdidx = MMC_CMD_SEND_STATUS; 1245d4fc8d9SRaffaele Recalcati cmd.resp_type = MMC_RSP_R1; 125aaf3d41aSMarek Vasut if (!mmc_host_is_spi(mmc)) 126aaf3d41aSMarek Vasut cmd.cmdarg = mmc->rca << 16; 1275d4fc8d9SRaffaele Recalcati 1281677eef4SAndrew Gabbasov while (1) { 1295d4fc8d9SRaffaele Recalcati err = mmc_send_cmd(mmc, &cmd, NULL); 130d617c426SJan Kloetzke if (!err) { 131d617c426SJan Kloetzke if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) && 132d617c426SJan Kloetzke (cmd.response[0] & MMC_STATUS_CURR_STATE) != 133d617c426SJan Kloetzke MMC_STATE_PRG) 1345d4fc8d9SRaffaele Recalcati break; 135d617c426SJan Kloetzke else if (cmd.response[0] & MMC_STATUS_MASK) { 13656196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 137d617c426SJan Kloetzke printf("Status Error: 0x%08X\n", 138d617c426SJan Kloetzke cmd.response[0]); 13956196826SPaul Burton #endif 140d617c426SJan Kloetzke return COMM_ERR; 141d617c426SJan Kloetzke } 142d617c426SJan Kloetzke } else if (--retries < 0) 143d617c426SJan Kloetzke return err; 1445d4fc8d9SRaffaele Recalcati 1451677eef4SAndrew Gabbasov if (timeout-- <= 0) 1461677eef4SAndrew Gabbasov break; 1475d4fc8d9SRaffaele Recalcati 1481677eef4SAndrew Gabbasov udelay(1000); 1491677eef4SAndrew Gabbasov } 1505d4fc8d9SRaffaele Recalcati 1515db2fe3aSRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 1525db2fe3aSRaffaele Recalcati status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9; 1535db2fe3aSRaffaele Recalcati printf("CURR STATE:%d\n", status); 1545db2fe3aSRaffaele Recalcati #endif 1555b0c942fSJongman Heo if (timeout <= 0) { 15656196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1575d4fc8d9SRaffaele Recalcati printf("Timeout waiting card ready\n"); 15856196826SPaul Burton #endif 1595d4fc8d9SRaffaele Recalcati return TIMEOUT; 1605d4fc8d9SRaffaele Recalcati } 1616b2221b0SAndrew Gabbasov if (cmd.response[0] & MMC_STATUS_SWITCH_ERROR) 1626b2221b0SAndrew Gabbasov return SWITCH_ERR; 1635d4fc8d9SRaffaele Recalcati 1645d4fc8d9SRaffaele Recalcati return 0; 1655d4fc8d9SRaffaele Recalcati } 1665d4fc8d9SRaffaele Recalcati 167da61fa5fSPaul Burton int mmc_set_blocklen(struct mmc *mmc, int len) 168272cc70bSAndy Fleming { 169272cc70bSAndy Fleming struct mmc_cmd cmd; 170272cc70bSAndy Fleming 171786e8f81SAndrew Gabbasov if (mmc->ddr_mode) 172d22e3d46SJaehoon Chung return 0; 173d22e3d46SJaehoon Chung 174272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; 175272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 176272cc70bSAndy Fleming cmd.cmdarg = len; 177272cc70bSAndy Fleming 178272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, NULL); 179272cc70bSAndy Fleming } 180272cc70bSAndy Fleming 181272cc70bSAndy Fleming struct mmc *find_mmc_device(int dev_num) 182272cc70bSAndy Fleming { 183272cc70bSAndy Fleming struct mmc *m; 184272cc70bSAndy Fleming struct list_head *entry; 185272cc70bSAndy Fleming 186272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 187272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 188272cc70bSAndy Fleming 189bcce53d0SSimon Glass if (m->block_dev.devnum == dev_num) 190272cc70bSAndy Fleming return m; 191272cc70bSAndy Fleming } 192272cc70bSAndy Fleming 19356196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 194272cc70bSAndy Fleming printf("MMC Device %d not found\n", dev_num); 19556196826SPaul Burton #endif 196272cc70bSAndy Fleming 197272cc70bSAndy Fleming return NULL; 198272cc70bSAndy Fleming } 199272cc70bSAndy Fleming 200ff8fef56SSascha Silbe static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start, 201fdbb873eSKim Phillips lbaint_t blkcnt) 202272cc70bSAndy Fleming { 203272cc70bSAndy Fleming struct mmc_cmd cmd; 204272cc70bSAndy Fleming struct mmc_data data; 205272cc70bSAndy Fleming 2064a1a06bcSAlagu Sankar if (blkcnt > 1) 2074a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; 2084a1a06bcSAlagu Sankar else 209272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; 210272cc70bSAndy Fleming 211272cc70bSAndy Fleming if (mmc->high_capacity) 2124a1a06bcSAlagu Sankar cmd.cmdarg = start; 213272cc70bSAndy Fleming else 2144a1a06bcSAlagu Sankar cmd.cmdarg = start * mmc->read_bl_len; 215272cc70bSAndy Fleming 216272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 217272cc70bSAndy Fleming 218272cc70bSAndy Fleming data.dest = dst; 2194a1a06bcSAlagu Sankar data.blocks = blkcnt; 220272cc70bSAndy Fleming data.blocksize = mmc->read_bl_len; 221272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 222272cc70bSAndy Fleming 2234a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, &data)) 2244a1a06bcSAlagu Sankar return 0; 2254a1a06bcSAlagu Sankar 2264a1a06bcSAlagu Sankar if (blkcnt > 1) { 2274a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 2284a1a06bcSAlagu Sankar cmd.cmdarg = 0; 2294a1a06bcSAlagu Sankar cmd.resp_type = MMC_RSP_R1b; 2304a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, NULL)) { 23156196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 2324a1a06bcSAlagu Sankar printf("mmc fail to send stop cmd\n"); 23356196826SPaul Burton #endif 2344a1a06bcSAlagu Sankar return 0; 2354a1a06bcSAlagu Sankar } 236272cc70bSAndy Fleming } 237272cc70bSAndy Fleming 2384a1a06bcSAlagu Sankar return blkcnt; 239272cc70bSAndy Fleming } 240272cc70bSAndy Fleming 2414101f687SSimon Glass static ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, 2427c4213f6SStephen Warren lbaint_t blkcnt, void *dst) 243272cc70bSAndy Fleming { 244bcce53d0SSimon Glass int dev_num = block_dev->devnum; 245873cc1d7SStephen Warren int err; 2464a1a06bcSAlagu Sankar lbaint_t cur, blocks_todo = blkcnt; 247272cc70bSAndy Fleming 2484a1a06bcSAlagu Sankar if (blkcnt == 0) 2494a1a06bcSAlagu Sankar return 0; 2504a1a06bcSAlagu Sankar 2514a1a06bcSAlagu Sankar struct mmc *mmc = find_mmc_device(dev_num); 252272cc70bSAndy Fleming if (!mmc) 253272cc70bSAndy Fleming return 0; 254272cc70bSAndy Fleming 255873cc1d7SStephen Warren err = mmc_select_hwpart(dev_num, block_dev->hwpart); 256873cc1d7SStephen Warren if (err < 0) 257873cc1d7SStephen Warren return 0; 258873cc1d7SStephen Warren 259d2bf29e3SLei Wen if ((start + blkcnt) > mmc->block_dev.lba) { 26056196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 261ff8fef56SSascha Silbe printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", 262d2bf29e3SLei Wen start + blkcnt, mmc->block_dev.lba); 26356196826SPaul Burton #endif 264d2bf29e3SLei Wen return 0; 265d2bf29e3SLei Wen } 266272cc70bSAndy Fleming 26711692991SSimon Glass if (mmc_set_blocklen(mmc, mmc->read_bl_len)) { 26811692991SSimon Glass debug("%s: Failed to set blocklen\n", __func__); 269272cc70bSAndy Fleming return 0; 27011692991SSimon Glass } 271272cc70bSAndy Fleming 2724a1a06bcSAlagu Sankar do { 27393bfd616SPantelis Antoniou cur = (blocks_todo > mmc->cfg->b_max) ? 27493bfd616SPantelis Antoniou mmc->cfg->b_max : blocks_todo; 27511692991SSimon Glass if (mmc_read_blocks(mmc, dst, start, cur) != cur) { 27611692991SSimon Glass debug("%s: Failed to read blocks\n", __func__); 2774a1a06bcSAlagu Sankar return 0; 27811692991SSimon Glass } 2794a1a06bcSAlagu Sankar blocks_todo -= cur; 2804a1a06bcSAlagu Sankar start += cur; 2814a1a06bcSAlagu Sankar dst += cur * mmc->read_bl_len; 2824a1a06bcSAlagu Sankar } while (blocks_todo > 0); 283272cc70bSAndy Fleming 284272cc70bSAndy Fleming return blkcnt; 285272cc70bSAndy Fleming } 286272cc70bSAndy Fleming 287fdbb873eSKim Phillips static int mmc_go_idle(struct mmc *mmc) 288272cc70bSAndy Fleming { 289272cc70bSAndy Fleming struct mmc_cmd cmd; 290272cc70bSAndy Fleming int err; 291272cc70bSAndy Fleming 292272cc70bSAndy Fleming udelay(1000); 293272cc70bSAndy Fleming 294272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; 295272cc70bSAndy Fleming cmd.cmdarg = 0; 296272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_NONE; 297272cc70bSAndy Fleming 298272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 299272cc70bSAndy Fleming 300272cc70bSAndy Fleming if (err) 301272cc70bSAndy Fleming return err; 302272cc70bSAndy Fleming 303272cc70bSAndy Fleming udelay(2000); 304272cc70bSAndy Fleming 305272cc70bSAndy Fleming return 0; 306272cc70bSAndy Fleming } 307272cc70bSAndy Fleming 308fdbb873eSKim Phillips static int sd_send_op_cond(struct mmc *mmc) 309272cc70bSAndy Fleming { 310272cc70bSAndy Fleming int timeout = 1000; 311272cc70bSAndy Fleming int err; 312272cc70bSAndy Fleming struct mmc_cmd cmd; 313272cc70bSAndy Fleming 3141677eef4SAndrew Gabbasov while (1) { 315272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 316272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 317272cc70bSAndy Fleming cmd.cmdarg = 0; 318272cc70bSAndy Fleming 319272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 320272cc70bSAndy Fleming 321272cc70bSAndy Fleming if (err) 322272cc70bSAndy Fleming return err; 323272cc70bSAndy Fleming 324272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; 325272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R3; 326250de12bSStefano Babic 327250de12bSStefano Babic /* 328250de12bSStefano Babic * Most cards do not answer if some reserved bits 329250de12bSStefano Babic * in the ocr are set. However, Some controller 330250de12bSStefano Babic * can set bit 7 (reserved for low voltages), but 331250de12bSStefano Babic * how to manage low voltages SD card is not yet 332250de12bSStefano Babic * specified. 333250de12bSStefano Babic */ 334d52ebf10SThomas Chou cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 : 33593bfd616SPantelis Antoniou (mmc->cfg->voltages & 0xff8000); 336272cc70bSAndy Fleming 337272cc70bSAndy Fleming if (mmc->version == SD_VERSION_2) 338272cc70bSAndy Fleming cmd.cmdarg |= OCR_HCS; 339272cc70bSAndy Fleming 340272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 341272cc70bSAndy Fleming 342272cc70bSAndy Fleming if (err) 343272cc70bSAndy Fleming return err; 344272cc70bSAndy Fleming 3451677eef4SAndrew Gabbasov if (cmd.response[0] & OCR_BUSY) 3461677eef4SAndrew Gabbasov break; 347272cc70bSAndy Fleming 3481677eef4SAndrew Gabbasov if (timeout-- <= 0) 349272cc70bSAndy Fleming return UNUSABLE_ERR; 350272cc70bSAndy Fleming 3511677eef4SAndrew Gabbasov udelay(1000); 3521677eef4SAndrew Gabbasov } 3531677eef4SAndrew Gabbasov 354272cc70bSAndy Fleming if (mmc->version != SD_VERSION_2) 355272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 356272cc70bSAndy Fleming 357d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 358d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 359d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 360d52ebf10SThomas Chou cmd.cmdarg = 0; 361d52ebf10SThomas Chou 362d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 363d52ebf10SThomas Chou 364d52ebf10SThomas Chou if (err) 365d52ebf10SThomas Chou return err; 366d52ebf10SThomas Chou } 367d52ebf10SThomas Chou 368998be3ddSRabin Vincent mmc->ocr = cmd.response[0]; 369272cc70bSAndy Fleming 370272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 371272cc70bSAndy Fleming mmc->rca = 0; 372272cc70bSAndy Fleming 373272cc70bSAndy Fleming return 0; 374272cc70bSAndy Fleming } 375272cc70bSAndy Fleming 3765289b535SAndrew Gabbasov static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg) 377272cc70bSAndy Fleming { 3785289b535SAndrew Gabbasov struct mmc_cmd cmd; 379272cc70bSAndy Fleming int err; 380272cc70bSAndy Fleming 3815289b535SAndrew Gabbasov cmd.cmdidx = MMC_CMD_SEND_OP_COND; 3825289b535SAndrew Gabbasov cmd.resp_type = MMC_RSP_R3; 3835289b535SAndrew Gabbasov cmd.cmdarg = 0; 3845a20397bSRob Herring if (use_arg && !mmc_host_is_spi(mmc)) 3855a20397bSRob Herring cmd.cmdarg = OCR_HCS | 38693bfd616SPantelis Antoniou (mmc->cfg->voltages & 387a626c8d4SAndrew Gabbasov (mmc->ocr & OCR_VOLTAGE_MASK)) | 388a626c8d4SAndrew Gabbasov (mmc->ocr & OCR_ACCESS_MODE); 389e9550449SChe-Liang Chiou 3905289b535SAndrew Gabbasov err = mmc_send_cmd(mmc, &cmd, NULL); 391e9550449SChe-Liang Chiou if (err) 392e9550449SChe-Liang Chiou return err; 3935289b535SAndrew Gabbasov mmc->ocr = cmd.response[0]; 394e9550449SChe-Liang Chiou return 0; 395e9550449SChe-Liang Chiou } 396e9550449SChe-Liang Chiou 397750121c3SJeroen Hofstee static int mmc_send_op_cond(struct mmc *mmc) 398e9550449SChe-Liang Chiou { 399e9550449SChe-Liang Chiou int err, i; 400e9550449SChe-Liang Chiou 401272cc70bSAndy Fleming /* Some cards seem to need this */ 402272cc70bSAndy Fleming mmc_go_idle(mmc); 403272cc70bSAndy Fleming 40431cacbabSRaffaele Recalcati /* Asking to the card its capabilities */ 405e9550449SChe-Liang Chiou for (i = 0; i < 2; i++) { 4065289b535SAndrew Gabbasov err = mmc_send_op_cond_iter(mmc, i != 0); 40731cacbabSRaffaele Recalcati if (err) 40831cacbabSRaffaele Recalcati return err; 40931cacbabSRaffaele Recalcati 410e9550449SChe-Liang Chiou /* exit if not busy (flag seems to be inverted) */ 411a626c8d4SAndrew Gabbasov if (mmc->ocr & OCR_BUSY) 412bd47c135SAndrew Gabbasov break; 413e9550449SChe-Liang Chiou } 414bd47c135SAndrew Gabbasov mmc->op_cond_pending = 1; 415bd47c135SAndrew Gabbasov return 0; 416e9550449SChe-Liang Chiou } 41731cacbabSRaffaele Recalcati 418750121c3SJeroen Hofstee static int mmc_complete_op_cond(struct mmc *mmc) 419e9550449SChe-Liang Chiou { 420e9550449SChe-Liang Chiou struct mmc_cmd cmd; 421e9550449SChe-Liang Chiou int timeout = 1000; 422e9550449SChe-Liang Chiou uint start; 423e9550449SChe-Liang Chiou int err; 424e9550449SChe-Liang Chiou 425e9550449SChe-Liang Chiou mmc->op_cond_pending = 0; 426cc17c01fSAndrew Gabbasov if (!(mmc->ocr & OCR_BUSY)) { 427e9550449SChe-Liang Chiou start = get_timer(0); 4281677eef4SAndrew Gabbasov while (1) { 4295289b535SAndrew Gabbasov err = mmc_send_op_cond_iter(mmc, 1); 430272cc70bSAndy Fleming if (err) 431272cc70bSAndy Fleming return err; 4321677eef4SAndrew Gabbasov if (mmc->ocr & OCR_BUSY) 4331677eef4SAndrew Gabbasov break; 434e9550449SChe-Liang Chiou if (get_timer(start) > timeout) 435272cc70bSAndy Fleming return UNUSABLE_ERR; 436e9550449SChe-Liang Chiou udelay(100); 4371677eef4SAndrew Gabbasov } 438cc17c01fSAndrew Gabbasov } 439272cc70bSAndy Fleming 440d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 441d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 442d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 443d52ebf10SThomas Chou cmd.cmdarg = 0; 444d52ebf10SThomas Chou 445d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 446d52ebf10SThomas Chou 447d52ebf10SThomas Chou if (err) 448d52ebf10SThomas Chou return err; 449a626c8d4SAndrew Gabbasov 450a626c8d4SAndrew Gabbasov mmc->ocr = cmd.response[0]; 451d52ebf10SThomas Chou } 452d52ebf10SThomas Chou 453272cc70bSAndy Fleming mmc->version = MMC_VERSION_UNKNOWN; 454272cc70bSAndy Fleming 455272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 456def816a2SStephen Warren mmc->rca = 1; 457272cc70bSAndy Fleming 458272cc70bSAndy Fleming return 0; 459272cc70bSAndy Fleming } 460272cc70bSAndy Fleming 461272cc70bSAndy Fleming 462fdbb873eSKim Phillips static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd) 463272cc70bSAndy Fleming { 464272cc70bSAndy Fleming struct mmc_cmd cmd; 465272cc70bSAndy Fleming struct mmc_data data; 466272cc70bSAndy Fleming int err; 467272cc70bSAndy Fleming 468272cc70bSAndy Fleming /* Get the Card Status Register */ 469272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; 470272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 471272cc70bSAndy Fleming cmd.cmdarg = 0; 472272cc70bSAndy Fleming 473cdfd1ac6SYoshihiro Shimoda data.dest = (char *)ext_csd; 474272cc70bSAndy Fleming data.blocks = 1; 4758bfa195eSSimon Glass data.blocksize = MMC_MAX_BLOCK_LEN; 476272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 477272cc70bSAndy Fleming 478272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 479272cc70bSAndy Fleming 480272cc70bSAndy Fleming return err; 481272cc70bSAndy Fleming } 482272cc70bSAndy Fleming 483272cc70bSAndy Fleming 484fdbb873eSKim Phillips static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) 485272cc70bSAndy Fleming { 486272cc70bSAndy Fleming struct mmc_cmd cmd; 4875d4fc8d9SRaffaele Recalcati int timeout = 1000; 4885d4fc8d9SRaffaele Recalcati int ret; 489272cc70bSAndy Fleming 490272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SWITCH; 491272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 492272cc70bSAndy Fleming cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 493272cc70bSAndy Fleming (index << 16) | 494272cc70bSAndy Fleming (value << 8); 495272cc70bSAndy Fleming 4965d4fc8d9SRaffaele Recalcati ret = mmc_send_cmd(mmc, &cmd, NULL); 4975d4fc8d9SRaffaele Recalcati 4985d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 49993ad0d18SJan Kloetzke if (!ret) 50093ad0d18SJan Kloetzke ret = mmc_send_status(mmc, timeout); 5015d4fc8d9SRaffaele Recalcati 5025d4fc8d9SRaffaele Recalcati return ret; 5035d4fc8d9SRaffaele Recalcati 504272cc70bSAndy Fleming } 505272cc70bSAndy Fleming 506fdbb873eSKim Phillips static int mmc_change_freq(struct mmc *mmc) 507272cc70bSAndy Fleming { 5088bfa195eSSimon Glass ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); 509272cc70bSAndy Fleming char cardtype; 510272cc70bSAndy Fleming int err; 511272cc70bSAndy Fleming 512fc5b32fbSAndrew Gabbasov mmc->card_caps = 0; 513272cc70bSAndy Fleming 514d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 515d52ebf10SThomas Chou return 0; 516d52ebf10SThomas Chou 517272cc70bSAndy Fleming /* Only version 4 supports high-speed */ 518272cc70bSAndy Fleming if (mmc->version < MMC_VERSION_4) 519272cc70bSAndy Fleming return 0; 520272cc70bSAndy Fleming 521fc5b32fbSAndrew Gabbasov mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT; 522fc5b32fbSAndrew Gabbasov 523272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 524272cc70bSAndy Fleming 525272cc70bSAndy Fleming if (err) 526272cc70bSAndy Fleming return err; 527272cc70bSAndy Fleming 5280560db18SLei Wen cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf; 529272cc70bSAndy Fleming 530272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); 531272cc70bSAndy Fleming 532272cc70bSAndy Fleming if (err) 5336b2221b0SAndrew Gabbasov return err == SWITCH_ERR ? 0 : err; 534272cc70bSAndy Fleming 535272cc70bSAndy Fleming /* Now check to see that it worked */ 536272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 537272cc70bSAndy Fleming 538272cc70bSAndy Fleming if (err) 539272cc70bSAndy Fleming return err; 540272cc70bSAndy Fleming 541272cc70bSAndy Fleming /* No high-speed support */ 5420560db18SLei Wen if (!ext_csd[EXT_CSD_HS_TIMING]) 543272cc70bSAndy Fleming return 0; 544272cc70bSAndy Fleming 545272cc70bSAndy Fleming /* High Speed is set, there are two types: 52MHz and 26MHz */ 546d22e3d46SJaehoon Chung if (cardtype & EXT_CSD_CARD_TYPE_52) { 547201d5ac4SAndrew Gabbasov if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V) 548d22e3d46SJaehoon Chung mmc->card_caps |= MMC_MODE_DDR_52MHz; 549272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; 550d22e3d46SJaehoon Chung } else { 551272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 552d22e3d46SJaehoon Chung } 553272cc70bSAndy Fleming 554272cc70bSAndy Fleming return 0; 555272cc70bSAndy Fleming } 556272cc70bSAndy Fleming 557f866a46dSStephen Warren static int mmc_set_capacity(struct mmc *mmc, int part_num) 558f866a46dSStephen Warren { 559f866a46dSStephen Warren switch (part_num) { 560f866a46dSStephen Warren case 0: 561f866a46dSStephen Warren mmc->capacity = mmc->capacity_user; 562f866a46dSStephen Warren break; 563f866a46dSStephen Warren case 1: 564f866a46dSStephen Warren case 2: 565f866a46dSStephen Warren mmc->capacity = mmc->capacity_boot; 566f866a46dSStephen Warren break; 567f866a46dSStephen Warren case 3: 568f866a46dSStephen Warren mmc->capacity = mmc->capacity_rpmb; 569f866a46dSStephen Warren break; 570f866a46dSStephen Warren case 4: 571f866a46dSStephen Warren case 5: 572f866a46dSStephen Warren case 6: 573f866a46dSStephen Warren case 7: 574f866a46dSStephen Warren mmc->capacity = mmc->capacity_gp[part_num - 4]; 575f866a46dSStephen Warren break; 576f866a46dSStephen Warren default: 577f866a46dSStephen Warren return -1; 578f866a46dSStephen Warren } 579f866a46dSStephen Warren 580f866a46dSStephen Warren mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); 581f866a46dSStephen Warren 582f866a46dSStephen Warren return 0; 583f866a46dSStephen Warren } 584f866a46dSStephen Warren 585bc897b1dSLei Wen int mmc_switch_part(int dev_num, unsigned int part_num) 586bc897b1dSLei Wen { 587bc897b1dSLei Wen struct mmc *mmc = find_mmc_device(dev_num); 588f866a46dSStephen Warren int ret; 589bc897b1dSLei Wen 590bc897b1dSLei Wen if (!mmc) 591bc897b1dSLei Wen return -1; 592bc897b1dSLei Wen 593f866a46dSStephen Warren ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, 594bc897b1dSLei Wen (mmc->part_config & ~PART_ACCESS_MASK) 595bc897b1dSLei Wen | (part_num & PART_ACCESS_MASK)); 596f866a46dSStephen Warren 5976dc93e70SPeter Bigot /* 5986dc93e70SPeter Bigot * Set the capacity if the switch succeeded or was intended 5996dc93e70SPeter Bigot * to return to representing the raw device. 6006dc93e70SPeter Bigot */ 601873cc1d7SStephen Warren if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) { 6026dc93e70SPeter Bigot ret = mmc_set_capacity(mmc, part_num); 603873cc1d7SStephen Warren mmc->block_dev.hwpart = part_num; 604873cc1d7SStephen Warren } 6056dc93e70SPeter Bigot 6066dc93e70SPeter Bigot return ret; 607bc897b1dSLei Wen } 608bc897b1dSLei Wen 609*ff3882acSSimon Glass int mmc_select_hwpart(int dev_num, int hwpart) 610*ff3882acSSimon Glass { 611*ff3882acSSimon Glass struct mmc *mmc = find_mmc_device(dev_num); 612*ff3882acSSimon Glass int ret; 613*ff3882acSSimon Glass 614*ff3882acSSimon Glass if (!mmc) 615*ff3882acSSimon Glass return -ENODEV; 616*ff3882acSSimon Glass 617*ff3882acSSimon Glass if (mmc->block_dev.hwpart == hwpart) 618*ff3882acSSimon Glass return 0; 619*ff3882acSSimon Glass 620*ff3882acSSimon Glass if (mmc->part_config == MMCPART_NOAVAILABLE) 621*ff3882acSSimon Glass return -EMEDIUMTYPE; 622*ff3882acSSimon Glass 623*ff3882acSSimon Glass ret = mmc_switch_part(dev_num, hwpart); 624*ff3882acSSimon Glass if (ret) 625*ff3882acSSimon Glass return ret; 626*ff3882acSSimon Glass 627*ff3882acSSimon Glass return 0; 628*ff3882acSSimon Glass } 629*ff3882acSSimon Glass 630ac9da0e0SDiego Santa Cruz int mmc_hwpart_config(struct mmc *mmc, 631ac9da0e0SDiego Santa Cruz const struct mmc_hwpart_conf *conf, 632ac9da0e0SDiego Santa Cruz enum mmc_hwpart_conf_mode mode) 633ac9da0e0SDiego Santa Cruz { 634ac9da0e0SDiego Santa Cruz u8 part_attrs = 0; 635ac9da0e0SDiego Santa Cruz u32 enh_size_mult; 636ac9da0e0SDiego Santa Cruz u32 enh_start_addr; 637ac9da0e0SDiego Santa Cruz u32 gp_size_mult[4]; 638ac9da0e0SDiego Santa Cruz u32 max_enh_size_mult; 639ac9da0e0SDiego Santa Cruz u32 tot_enh_size_mult = 0; 6408dda5b0eSDiego Santa Cruz u8 wr_rel_set; 641ac9da0e0SDiego Santa Cruz int i, pidx, err; 642ac9da0e0SDiego Santa Cruz ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); 643ac9da0e0SDiego Santa Cruz 644ac9da0e0SDiego Santa Cruz if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE) 645ac9da0e0SDiego Santa Cruz return -EINVAL; 646ac9da0e0SDiego Santa Cruz 647ac9da0e0SDiego Santa Cruz if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) { 648ac9da0e0SDiego Santa Cruz printf("eMMC >= 4.4 required for enhanced user data area\n"); 649ac9da0e0SDiego Santa Cruz return -EMEDIUMTYPE; 650ac9da0e0SDiego Santa Cruz } 651ac9da0e0SDiego Santa Cruz 652ac9da0e0SDiego Santa Cruz if (!(mmc->part_support & PART_SUPPORT)) { 653ac9da0e0SDiego Santa Cruz printf("Card does not support partitioning\n"); 654ac9da0e0SDiego Santa Cruz return -EMEDIUMTYPE; 655ac9da0e0SDiego Santa Cruz } 656ac9da0e0SDiego Santa Cruz 657ac9da0e0SDiego Santa Cruz if (!mmc->hc_wp_grp_size) { 658ac9da0e0SDiego Santa Cruz printf("Card does not define HC WP group size\n"); 659ac9da0e0SDiego Santa Cruz return -EMEDIUMTYPE; 660ac9da0e0SDiego Santa Cruz } 661ac9da0e0SDiego Santa Cruz 662ac9da0e0SDiego Santa Cruz /* check partition alignment and total enhanced size */ 663ac9da0e0SDiego Santa Cruz if (conf->user.enh_size) { 664ac9da0e0SDiego Santa Cruz if (conf->user.enh_size % mmc->hc_wp_grp_size || 665ac9da0e0SDiego Santa Cruz conf->user.enh_start % mmc->hc_wp_grp_size) { 666ac9da0e0SDiego Santa Cruz printf("User data enhanced area not HC WP group " 667ac9da0e0SDiego Santa Cruz "size aligned\n"); 668ac9da0e0SDiego Santa Cruz return -EINVAL; 669ac9da0e0SDiego Santa Cruz } 670ac9da0e0SDiego Santa Cruz part_attrs |= EXT_CSD_ENH_USR; 671ac9da0e0SDiego Santa Cruz enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size; 672ac9da0e0SDiego Santa Cruz if (mmc->high_capacity) { 673ac9da0e0SDiego Santa Cruz enh_start_addr = conf->user.enh_start; 674ac9da0e0SDiego Santa Cruz } else { 675ac9da0e0SDiego Santa Cruz enh_start_addr = (conf->user.enh_start << 9); 676ac9da0e0SDiego Santa Cruz } 677ac9da0e0SDiego Santa Cruz } else { 678ac9da0e0SDiego Santa Cruz enh_size_mult = 0; 679ac9da0e0SDiego Santa Cruz enh_start_addr = 0; 680ac9da0e0SDiego Santa Cruz } 681ac9da0e0SDiego Santa Cruz tot_enh_size_mult += enh_size_mult; 682ac9da0e0SDiego Santa Cruz 683ac9da0e0SDiego Santa Cruz for (pidx = 0; pidx < 4; pidx++) { 684ac9da0e0SDiego Santa Cruz if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) { 685ac9da0e0SDiego Santa Cruz printf("GP%i partition not HC WP group size " 686ac9da0e0SDiego Santa Cruz "aligned\n", pidx+1); 687ac9da0e0SDiego Santa Cruz return -EINVAL; 688ac9da0e0SDiego Santa Cruz } 689ac9da0e0SDiego Santa Cruz gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size; 690ac9da0e0SDiego Santa Cruz if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) { 691ac9da0e0SDiego Santa Cruz part_attrs |= EXT_CSD_ENH_GP(pidx); 692ac9da0e0SDiego Santa Cruz tot_enh_size_mult += gp_size_mult[pidx]; 693ac9da0e0SDiego Santa Cruz } 694ac9da0e0SDiego Santa Cruz } 695ac9da0e0SDiego Santa Cruz 696ac9da0e0SDiego Santa Cruz if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) { 697ac9da0e0SDiego Santa Cruz printf("Card does not support enhanced attribute\n"); 698ac9da0e0SDiego Santa Cruz return -EMEDIUMTYPE; 699ac9da0e0SDiego Santa Cruz } 700ac9da0e0SDiego Santa Cruz 701ac9da0e0SDiego Santa Cruz err = mmc_send_ext_csd(mmc, ext_csd); 702ac9da0e0SDiego Santa Cruz if (err) 703ac9da0e0SDiego Santa Cruz return err; 704ac9da0e0SDiego Santa Cruz 705ac9da0e0SDiego Santa Cruz max_enh_size_mult = 706ac9da0e0SDiego Santa Cruz (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) + 707ac9da0e0SDiego Santa Cruz (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) + 708ac9da0e0SDiego Santa Cruz ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]; 709ac9da0e0SDiego Santa Cruz if (tot_enh_size_mult > max_enh_size_mult) { 710ac9da0e0SDiego Santa Cruz printf("Total enhanced size exceeds maximum (%u > %u)\n", 711ac9da0e0SDiego Santa Cruz tot_enh_size_mult, max_enh_size_mult); 712ac9da0e0SDiego Santa Cruz return -EMEDIUMTYPE; 713ac9da0e0SDiego Santa Cruz } 714ac9da0e0SDiego Santa Cruz 7158dda5b0eSDiego Santa Cruz /* The default value of EXT_CSD_WR_REL_SET is device 7168dda5b0eSDiego Santa Cruz * dependent, the values can only be changed if the 7178dda5b0eSDiego Santa Cruz * EXT_CSD_HS_CTRL_REL bit is set. The values can be 7188dda5b0eSDiego Santa Cruz * changed only once and before partitioning is completed. */ 7198dda5b0eSDiego Santa Cruz wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET]; 7208dda5b0eSDiego Santa Cruz if (conf->user.wr_rel_change) { 7218dda5b0eSDiego Santa Cruz if (conf->user.wr_rel_set) 7228dda5b0eSDiego Santa Cruz wr_rel_set |= EXT_CSD_WR_DATA_REL_USR; 7238dda5b0eSDiego Santa Cruz else 7248dda5b0eSDiego Santa Cruz wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR; 7258dda5b0eSDiego Santa Cruz } 7268dda5b0eSDiego Santa Cruz for (pidx = 0; pidx < 4; pidx++) { 7278dda5b0eSDiego Santa Cruz if (conf->gp_part[pidx].wr_rel_change) { 7288dda5b0eSDiego Santa Cruz if (conf->gp_part[pidx].wr_rel_set) 7298dda5b0eSDiego Santa Cruz wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx); 7308dda5b0eSDiego Santa Cruz else 7318dda5b0eSDiego Santa Cruz wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx); 7328dda5b0eSDiego Santa Cruz } 7338dda5b0eSDiego Santa Cruz } 7348dda5b0eSDiego Santa Cruz 7358dda5b0eSDiego Santa Cruz if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] && 7368dda5b0eSDiego Santa Cruz !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) { 7378dda5b0eSDiego Santa Cruz puts("Card does not support host controlled partition write " 7388dda5b0eSDiego Santa Cruz "reliability settings\n"); 7398dda5b0eSDiego Santa Cruz return -EMEDIUMTYPE; 7408dda5b0eSDiego Santa Cruz } 7418dda5b0eSDiego Santa Cruz 742ac9da0e0SDiego Santa Cruz if (ext_csd[EXT_CSD_PARTITION_SETTING] & 743ac9da0e0SDiego Santa Cruz EXT_CSD_PARTITION_SETTING_COMPLETED) { 744ac9da0e0SDiego Santa Cruz printf("Card already partitioned\n"); 745ac9da0e0SDiego Santa Cruz return -EPERM; 746ac9da0e0SDiego Santa Cruz } 747ac9da0e0SDiego Santa Cruz 748ac9da0e0SDiego Santa Cruz if (mode == MMC_HWPART_CONF_CHECK) 749ac9da0e0SDiego Santa Cruz return 0; 750ac9da0e0SDiego Santa Cruz 751ac9da0e0SDiego Santa Cruz /* Partitioning requires high-capacity size definitions */ 752ac9da0e0SDiego Santa Cruz if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) { 753ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 754ac9da0e0SDiego Santa Cruz EXT_CSD_ERASE_GROUP_DEF, 1); 755ac9da0e0SDiego Santa Cruz 756ac9da0e0SDiego Santa Cruz if (err) 757ac9da0e0SDiego Santa Cruz return err; 758ac9da0e0SDiego Santa Cruz 759ac9da0e0SDiego Santa Cruz ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1; 760ac9da0e0SDiego Santa Cruz 761ac9da0e0SDiego Santa Cruz /* update erase group size to be high-capacity */ 762ac9da0e0SDiego Santa Cruz mmc->erase_grp_size = 763ac9da0e0SDiego Santa Cruz ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024; 764ac9da0e0SDiego Santa Cruz 765ac9da0e0SDiego Santa Cruz } 766ac9da0e0SDiego Santa Cruz 767ac9da0e0SDiego Santa Cruz /* all OK, write the configuration */ 768ac9da0e0SDiego Santa Cruz for (i = 0; i < 4; i++) { 769ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 770ac9da0e0SDiego Santa Cruz EXT_CSD_ENH_START_ADDR+i, 771ac9da0e0SDiego Santa Cruz (enh_start_addr >> (i*8)) & 0xFF); 772ac9da0e0SDiego Santa Cruz if (err) 773ac9da0e0SDiego Santa Cruz return err; 774ac9da0e0SDiego Santa Cruz } 775ac9da0e0SDiego Santa Cruz for (i = 0; i < 3; i++) { 776ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 777ac9da0e0SDiego Santa Cruz EXT_CSD_ENH_SIZE_MULT+i, 778ac9da0e0SDiego Santa Cruz (enh_size_mult >> (i*8)) & 0xFF); 779ac9da0e0SDiego Santa Cruz if (err) 780ac9da0e0SDiego Santa Cruz return err; 781ac9da0e0SDiego Santa Cruz } 782ac9da0e0SDiego Santa Cruz for (pidx = 0; pidx < 4; pidx++) { 783ac9da0e0SDiego Santa Cruz for (i = 0; i < 3; i++) { 784ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 785ac9da0e0SDiego Santa Cruz EXT_CSD_GP_SIZE_MULT+pidx*3+i, 786ac9da0e0SDiego Santa Cruz (gp_size_mult[pidx] >> (i*8)) & 0xFF); 787ac9da0e0SDiego Santa Cruz if (err) 788ac9da0e0SDiego Santa Cruz return err; 789ac9da0e0SDiego Santa Cruz } 790ac9da0e0SDiego Santa Cruz } 791ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 792ac9da0e0SDiego Santa Cruz EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs); 793ac9da0e0SDiego Santa Cruz if (err) 794ac9da0e0SDiego Santa Cruz return err; 795ac9da0e0SDiego Santa Cruz 796ac9da0e0SDiego Santa Cruz if (mode == MMC_HWPART_CONF_SET) 797ac9da0e0SDiego Santa Cruz return 0; 798ac9da0e0SDiego Santa Cruz 7998dda5b0eSDiego Santa Cruz /* The WR_REL_SET is a write-once register but shall be 8008dda5b0eSDiego Santa Cruz * written before setting PART_SETTING_COMPLETED. As it is 8018dda5b0eSDiego Santa Cruz * write-once we can only write it when completing the 8028dda5b0eSDiego Santa Cruz * partitioning. */ 8038dda5b0eSDiego Santa Cruz if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) { 8048dda5b0eSDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 8058dda5b0eSDiego Santa Cruz EXT_CSD_WR_REL_SET, wr_rel_set); 8068dda5b0eSDiego Santa Cruz if (err) 8078dda5b0eSDiego Santa Cruz return err; 8088dda5b0eSDiego Santa Cruz } 8098dda5b0eSDiego Santa Cruz 810ac9da0e0SDiego Santa Cruz /* Setting PART_SETTING_COMPLETED confirms the partition 811ac9da0e0SDiego Santa Cruz * configuration but it only becomes effective after power 812ac9da0e0SDiego Santa Cruz * cycle, so we do not adjust the partition related settings 813ac9da0e0SDiego Santa Cruz * in the mmc struct. */ 814ac9da0e0SDiego Santa Cruz 815ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 816ac9da0e0SDiego Santa Cruz EXT_CSD_PARTITION_SETTING, 817ac9da0e0SDiego Santa Cruz EXT_CSD_PARTITION_SETTING_COMPLETED); 818ac9da0e0SDiego Santa Cruz if (err) 819ac9da0e0SDiego Santa Cruz return err; 820ac9da0e0SDiego Santa Cruz 821ac9da0e0SDiego Santa Cruz return 0; 822ac9da0e0SDiego Santa Cruz } 823ac9da0e0SDiego Santa Cruz 82448972d90SThierry Reding int mmc_getcd(struct mmc *mmc) 82548972d90SThierry Reding { 82648972d90SThierry Reding int cd; 82748972d90SThierry Reding 82848972d90SThierry Reding cd = board_mmc_getcd(mmc); 82948972d90SThierry Reding 830d4e1da4eSPeter Korsgaard if (cd < 0) { 83193bfd616SPantelis Antoniou if (mmc->cfg->ops->getcd) 83293bfd616SPantelis Antoniou cd = mmc->cfg->ops->getcd(mmc); 833d4e1da4eSPeter Korsgaard else 834d4e1da4eSPeter Korsgaard cd = 1; 835d4e1da4eSPeter Korsgaard } 83648972d90SThierry Reding 83748972d90SThierry Reding return cd; 83848972d90SThierry Reding } 83948972d90SThierry Reding 840fdbb873eSKim Phillips static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) 841272cc70bSAndy Fleming { 842272cc70bSAndy Fleming struct mmc_cmd cmd; 843272cc70bSAndy Fleming struct mmc_data data; 844272cc70bSAndy Fleming 845272cc70bSAndy Fleming /* Switch the frequency */ 846272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SWITCH_FUNC; 847272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 848272cc70bSAndy Fleming cmd.cmdarg = (mode << 31) | 0xffffff; 849272cc70bSAndy Fleming cmd.cmdarg &= ~(0xf << (group * 4)); 850272cc70bSAndy Fleming cmd.cmdarg |= value << (group * 4); 851272cc70bSAndy Fleming 852272cc70bSAndy Fleming data.dest = (char *)resp; 853272cc70bSAndy Fleming data.blocksize = 64; 854272cc70bSAndy Fleming data.blocks = 1; 855272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 856272cc70bSAndy Fleming 857272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, &data); 858272cc70bSAndy Fleming } 859272cc70bSAndy Fleming 860272cc70bSAndy Fleming 861fdbb873eSKim Phillips static int sd_change_freq(struct mmc *mmc) 862272cc70bSAndy Fleming { 863272cc70bSAndy Fleming int err; 864272cc70bSAndy Fleming struct mmc_cmd cmd; 865f781dd38SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2); 866f781dd38SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16); 867272cc70bSAndy Fleming struct mmc_data data; 868272cc70bSAndy Fleming int timeout; 869272cc70bSAndy Fleming 870272cc70bSAndy Fleming mmc->card_caps = 0; 871272cc70bSAndy Fleming 872d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 873d52ebf10SThomas Chou return 0; 874d52ebf10SThomas Chou 875272cc70bSAndy Fleming /* Read the SCR to find out if this card supports higher speeds */ 876272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 877272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 878272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 879272cc70bSAndy Fleming 880272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 881272cc70bSAndy Fleming 882272cc70bSAndy Fleming if (err) 883272cc70bSAndy Fleming return err; 884272cc70bSAndy Fleming 885272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_SCR; 886272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 887272cc70bSAndy Fleming cmd.cmdarg = 0; 888272cc70bSAndy Fleming 889272cc70bSAndy Fleming timeout = 3; 890272cc70bSAndy Fleming 891272cc70bSAndy Fleming retry_scr: 892f781dd38SAnton staaf data.dest = (char *)scr; 893272cc70bSAndy Fleming data.blocksize = 8; 894272cc70bSAndy Fleming data.blocks = 1; 895272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 896272cc70bSAndy Fleming 897272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 898272cc70bSAndy Fleming 899272cc70bSAndy Fleming if (err) { 900272cc70bSAndy Fleming if (timeout--) 901272cc70bSAndy Fleming goto retry_scr; 902272cc70bSAndy Fleming 903272cc70bSAndy Fleming return err; 904272cc70bSAndy Fleming } 905272cc70bSAndy Fleming 9064e3d89baSYauhen Kharuzhy mmc->scr[0] = __be32_to_cpu(scr[0]); 9074e3d89baSYauhen Kharuzhy mmc->scr[1] = __be32_to_cpu(scr[1]); 908272cc70bSAndy Fleming 909272cc70bSAndy Fleming switch ((mmc->scr[0] >> 24) & 0xf) { 910272cc70bSAndy Fleming case 0: 911272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 912272cc70bSAndy Fleming break; 913272cc70bSAndy Fleming case 1: 914272cc70bSAndy Fleming mmc->version = SD_VERSION_1_10; 915272cc70bSAndy Fleming break; 916272cc70bSAndy Fleming case 2: 917272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 9181741c64dSJaehoon Chung if ((mmc->scr[0] >> 15) & 0x1) 9191741c64dSJaehoon Chung mmc->version = SD_VERSION_3; 920272cc70bSAndy Fleming break; 921272cc70bSAndy Fleming default: 922272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 923272cc70bSAndy Fleming break; 924272cc70bSAndy Fleming } 925272cc70bSAndy Fleming 926b44c7083SAlagu Sankar if (mmc->scr[0] & SD_DATA_4BIT) 927b44c7083SAlagu Sankar mmc->card_caps |= MMC_MODE_4BIT; 928b44c7083SAlagu Sankar 929272cc70bSAndy Fleming /* Version 1.0 doesn't support switching */ 930272cc70bSAndy Fleming if (mmc->version == SD_VERSION_1_0) 931272cc70bSAndy Fleming return 0; 932272cc70bSAndy Fleming 933272cc70bSAndy Fleming timeout = 4; 934272cc70bSAndy Fleming while (timeout--) { 935272cc70bSAndy Fleming err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, 936f781dd38SAnton staaf (u8 *)switch_status); 937272cc70bSAndy Fleming 938272cc70bSAndy Fleming if (err) 939272cc70bSAndy Fleming return err; 940272cc70bSAndy Fleming 941272cc70bSAndy Fleming /* The high-speed function is busy. Try again */ 9424e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) 943272cc70bSAndy Fleming break; 944272cc70bSAndy Fleming } 945272cc70bSAndy Fleming 946272cc70bSAndy Fleming /* If high-speed isn't supported, we return */ 9474e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) 948272cc70bSAndy Fleming return 0; 949272cc70bSAndy Fleming 9502c3fbf4cSMacpaul Lin /* 9512c3fbf4cSMacpaul Lin * If the host doesn't support SD_HIGHSPEED, do not switch card to 9522c3fbf4cSMacpaul Lin * HIGHSPEED mode even if the card support SD_HIGHSPPED. 9532c3fbf4cSMacpaul Lin * This can avoid furthur problem when the card runs in different 9542c3fbf4cSMacpaul Lin * mode between the host. 9552c3fbf4cSMacpaul Lin */ 95693bfd616SPantelis Antoniou if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) && 95793bfd616SPantelis Antoniou (mmc->cfg->host_caps & MMC_MODE_HS))) 9582c3fbf4cSMacpaul Lin return 0; 9592c3fbf4cSMacpaul Lin 960f781dd38SAnton staaf err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status); 961272cc70bSAndy Fleming 962272cc70bSAndy Fleming if (err) 963272cc70bSAndy Fleming return err; 964272cc70bSAndy Fleming 9654e3d89baSYauhen Kharuzhy if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) 966272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 967272cc70bSAndy Fleming 968272cc70bSAndy Fleming return 0; 969272cc70bSAndy Fleming } 970272cc70bSAndy Fleming 971272cc70bSAndy Fleming /* frequency bases */ 972272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */ 9735f837c2cSMike Frysinger static const int fbase[] = { 974272cc70bSAndy Fleming 10000, 975272cc70bSAndy Fleming 100000, 976272cc70bSAndy Fleming 1000000, 977272cc70bSAndy Fleming 10000000, 978272cc70bSAndy Fleming }; 979272cc70bSAndy Fleming 980272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice 981272cc70bSAndy Fleming * to platforms without floating point. 982272cc70bSAndy Fleming */ 9835f837c2cSMike Frysinger static const int multipliers[] = { 984272cc70bSAndy Fleming 0, /* reserved */ 985272cc70bSAndy Fleming 10, 986272cc70bSAndy Fleming 12, 987272cc70bSAndy Fleming 13, 988272cc70bSAndy Fleming 15, 989272cc70bSAndy Fleming 20, 990272cc70bSAndy Fleming 25, 991272cc70bSAndy Fleming 30, 992272cc70bSAndy Fleming 35, 993272cc70bSAndy Fleming 40, 994272cc70bSAndy Fleming 45, 995272cc70bSAndy Fleming 50, 996272cc70bSAndy Fleming 55, 997272cc70bSAndy Fleming 60, 998272cc70bSAndy Fleming 70, 999272cc70bSAndy Fleming 80, 1000272cc70bSAndy Fleming }; 1001272cc70bSAndy Fleming 1002fdbb873eSKim Phillips static void mmc_set_ios(struct mmc *mmc) 1003272cc70bSAndy Fleming { 100493bfd616SPantelis Antoniou if (mmc->cfg->ops->set_ios) 100593bfd616SPantelis Antoniou mmc->cfg->ops->set_ios(mmc); 1006272cc70bSAndy Fleming } 1007272cc70bSAndy Fleming 1008272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock) 1009272cc70bSAndy Fleming { 101093bfd616SPantelis Antoniou if (clock > mmc->cfg->f_max) 101193bfd616SPantelis Antoniou clock = mmc->cfg->f_max; 1012272cc70bSAndy Fleming 101393bfd616SPantelis Antoniou if (clock < mmc->cfg->f_min) 101493bfd616SPantelis Antoniou clock = mmc->cfg->f_min; 1015272cc70bSAndy Fleming 1016272cc70bSAndy Fleming mmc->clock = clock; 1017272cc70bSAndy Fleming 1018272cc70bSAndy Fleming mmc_set_ios(mmc); 1019272cc70bSAndy Fleming } 1020272cc70bSAndy Fleming 1021fdbb873eSKim Phillips static void mmc_set_bus_width(struct mmc *mmc, uint width) 1022272cc70bSAndy Fleming { 1023272cc70bSAndy Fleming mmc->bus_width = width; 1024272cc70bSAndy Fleming 1025272cc70bSAndy Fleming mmc_set_ios(mmc); 1026272cc70bSAndy Fleming } 1027272cc70bSAndy Fleming 1028fdbb873eSKim Phillips static int mmc_startup(struct mmc *mmc) 1029272cc70bSAndy Fleming { 1030f866a46dSStephen Warren int err, i; 1031272cc70bSAndy Fleming uint mult, freq; 1032639b7827SYoshihiro Shimoda u64 cmult, csize, capacity; 1033272cc70bSAndy Fleming struct mmc_cmd cmd; 10348bfa195eSSimon Glass ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); 10358bfa195eSSimon Glass ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN); 10365d4fc8d9SRaffaele Recalcati int timeout = 1000; 10370c453bb7SDiego Santa Cruz bool has_parts = false; 10388a0cf490SDiego Santa Cruz bool part_completed; 1039272cc70bSAndy Fleming 1040d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON 1041d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ 1042d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF; 1043d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R1; 1044d52ebf10SThomas Chou cmd.cmdarg = 1; 1045d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 1046d52ebf10SThomas Chou 1047d52ebf10SThomas Chou if (err) 1048d52ebf10SThomas Chou return err; 1049d52ebf10SThomas Chou } 1050d52ebf10SThomas Chou #endif 1051d52ebf10SThomas Chou 1052272cc70bSAndy Fleming /* Put the Card in Identify Mode */ 1053d52ebf10SThomas Chou cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID : 1054d52ebf10SThomas Chou MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */ 1055272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 1056272cc70bSAndy Fleming cmd.cmdarg = 0; 1057272cc70bSAndy Fleming 1058272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1059272cc70bSAndy Fleming 1060272cc70bSAndy Fleming if (err) 1061272cc70bSAndy Fleming return err; 1062272cc70bSAndy Fleming 1063272cc70bSAndy Fleming memcpy(mmc->cid, cmd.response, 16); 1064272cc70bSAndy Fleming 1065272cc70bSAndy Fleming /* 1066272cc70bSAndy Fleming * For MMC cards, set the Relative Address. 1067272cc70bSAndy Fleming * For SD cards, get the Relatvie Address. 1068272cc70bSAndy Fleming * This also puts the cards into Standby State 1069272cc70bSAndy Fleming */ 1070d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 1071272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; 1072272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1073272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R6; 1074272cc70bSAndy Fleming 1075272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1076272cc70bSAndy Fleming 1077272cc70bSAndy Fleming if (err) 1078272cc70bSAndy Fleming return err; 1079272cc70bSAndy Fleming 1080272cc70bSAndy Fleming if (IS_SD(mmc)) 1081998be3ddSRabin Vincent mmc->rca = (cmd.response[0] >> 16) & 0xffff; 1082d52ebf10SThomas Chou } 1083272cc70bSAndy Fleming 1084272cc70bSAndy Fleming /* Get the Card-Specific Data */ 1085272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_CSD; 1086272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 1087272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1088272cc70bSAndy Fleming 1089272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1090272cc70bSAndy Fleming 10915d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 10925d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 10935d4fc8d9SRaffaele Recalcati 1094272cc70bSAndy Fleming if (err) 1095272cc70bSAndy Fleming return err; 1096272cc70bSAndy Fleming 1097998be3ddSRabin Vincent mmc->csd[0] = cmd.response[0]; 1098998be3ddSRabin Vincent mmc->csd[1] = cmd.response[1]; 1099998be3ddSRabin Vincent mmc->csd[2] = cmd.response[2]; 1100998be3ddSRabin Vincent mmc->csd[3] = cmd.response[3]; 1101272cc70bSAndy Fleming 1102272cc70bSAndy Fleming if (mmc->version == MMC_VERSION_UNKNOWN) { 11030b453ffeSRabin Vincent int version = (cmd.response[0] >> 26) & 0xf; 1104272cc70bSAndy Fleming 1105272cc70bSAndy Fleming switch (version) { 1106272cc70bSAndy Fleming case 0: 1107272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 1108272cc70bSAndy Fleming break; 1109272cc70bSAndy Fleming case 1: 1110272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_4; 1111272cc70bSAndy Fleming break; 1112272cc70bSAndy Fleming case 2: 1113272cc70bSAndy Fleming mmc->version = MMC_VERSION_2_2; 1114272cc70bSAndy Fleming break; 1115272cc70bSAndy Fleming case 3: 1116272cc70bSAndy Fleming mmc->version = MMC_VERSION_3; 1117272cc70bSAndy Fleming break; 1118272cc70bSAndy Fleming case 4: 1119272cc70bSAndy Fleming mmc->version = MMC_VERSION_4; 1120272cc70bSAndy Fleming break; 1121272cc70bSAndy Fleming default: 1122272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 1123272cc70bSAndy Fleming break; 1124272cc70bSAndy Fleming } 1125272cc70bSAndy Fleming } 1126272cc70bSAndy Fleming 1127272cc70bSAndy Fleming /* divide frequency by 10, since the mults are 10x bigger */ 11280b453ffeSRabin Vincent freq = fbase[(cmd.response[0] & 0x7)]; 11290b453ffeSRabin Vincent mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; 1130272cc70bSAndy Fleming 1131272cc70bSAndy Fleming mmc->tran_speed = freq * mult; 1132272cc70bSAndy Fleming 1133ab71188cSMarkus Niebel mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1); 1134998be3ddSRabin Vincent mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); 1135272cc70bSAndy Fleming 1136272cc70bSAndy Fleming if (IS_SD(mmc)) 1137272cc70bSAndy Fleming mmc->write_bl_len = mmc->read_bl_len; 1138272cc70bSAndy Fleming else 1139998be3ddSRabin Vincent mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); 1140272cc70bSAndy Fleming 1141272cc70bSAndy Fleming if (mmc->high_capacity) { 1142272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3f) << 16 1143272cc70bSAndy Fleming | (mmc->csd[2] & 0xffff0000) >> 16; 1144272cc70bSAndy Fleming cmult = 8; 1145272cc70bSAndy Fleming } else { 1146272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3ff) << 2 1147272cc70bSAndy Fleming | (mmc->csd[2] & 0xc0000000) >> 30; 1148272cc70bSAndy Fleming cmult = (mmc->csd[2] & 0x00038000) >> 15; 1149272cc70bSAndy Fleming } 1150272cc70bSAndy Fleming 1151f866a46dSStephen Warren mmc->capacity_user = (csize + 1) << (cmult + 2); 1152f866a46dSStephen Warren mmc->capacity_user *= mmc->read_bl_len; 1153f866a46dSStephen Warren mmc->capacity_boot = 0; 1154f866a46dSStephen Warren mmc->capacity_rpmb = 0; 1155f866a46dSStephen Warren for (i = 0; i < 4; i++) 1156f866a46dSStephen Warren mmc->capacity_gp[i] = 0; 1157272cc70bSAndy Fleming 11588bfa195eSSimon Glass if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN) 11598bfa195eSSimon Glass mmc->read_bl_len = MMC_MAX_BLOCK_LEN; 1160272cc70bSAndy Fleming 11618bfa195eSSimon Glass if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN) 11628bfa195eSSimon Glass mmc->write_bl_len = MMC_MAX_BLOCK_LEN; 1163272cc70bSAndy Fleming 1164ab71188cSMarkus Niebel if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) { 1165ab71188cSMarkus Niebel cmd.cmdidx = MMC_CMD_SET_DSR; 1166ab71188cSMarkus Niebel cmd.cmdarg = (mmc->dsr & 0xffff) << 16; 1167ab71188cSMarkus Niebel cmd.resp_type = MMC_RSP_NONE; 1168ab71188cSMarkus Niebel if (mmc_send_cmd(mmc, &cmd, NULL)) 1169ab71188cSMarkus Niebel printf("MMC: SET_DSR failed\n"); 1170ab71188cSMarkus Niebel } 1171ab71188cSMarkus Niebel 1172272cc70bSAndy Fleming /* Select the card, and put it into Transfer Mode */ 1173d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 1174272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SELECT_CARD; 1175fe8f7066SAjay Bhargav cmd.resp_type = MMC_RSP_R1; 1176272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1177272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1178272cc70bSAndy Fleming 1179272cc70bSAndy Fleming if (err) 1180272cc70bSAndy Fleming return err; 1181d52ebf10SThomas Chou } 1182272cc70bSAndy Fleming 1183e6f99a56SLei Wen /* 1184e6f99a56SLei Wen * For SD, its erase group is always one sector 1185e6f99a56SLei Wen */ 1186e6f99a56SLei Wen mmc->erase_grp_size = 1; 1187bc897b1dSLei Wen mmc->part_config = MMCPART_NOAVAILABLE; 1188d23e2c09SSukumar Ghorai if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) { 1189d23e2c09SSukumar Ghorai /* check ext_csd version and capacity */ 1190d23e2c09SSukumar Ghorai err = mmc_send_ext_csd(mmc, ext_csd); 11919cf199ebSDiego Santa Cruz if (err) 11929cf199ebSDiego Santa Cruz return err; 11939cf199ebSDiego Santa Cruz if (ext_csd[EXT_CSD_REV] >= 2) { 1194639b7827SYoshihiro Shimoda /* 1195639b7827SYoshihiro Shimoda * According to the JEDEC Standard, the value of 1196639b7827SYoshihiro Shimoda * ext_csd's capacity is valid if the value is more 1197639b7827SYoshihiro Shimoda * than 2GB 1198639b7827SYoshihiro Shimoda */ 11990560db18SLei Wen capacity = ext_csd[EXT_CSD_SEC_CNT] << 0 12000560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 12010560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 12020560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; 12038bfa195eSSimon Glass capacity *= MMC_MAX_BLOCK_LEN; 1204b1f1e821SŁukasz Majewski if ((capacity >> 20) > 2 * 1024) 1205f866a46dSStephen Warren mmc->capacity_user = capacity; 1206d23e2c09SSukumar Ghorai } 1207bc897b1dSLei Wen 120864f4a619SJaehoon Chung switch (ext_csd[EXT_CSD_REV]) { 120964f4a619SJaehoon Chung case 1: 121064f4a619SJaehoon Chung mmc->version = MMC_VERSION_4_1; 121164f4a619SJaehoon Chung break; 121264f4a619SJaehoon Chung case 2: 121364f4a619SJaehoon Chung mmc->version = MMC_VERSION_4_2; 121464f4a619SJaehoon Chung break; 121564f4a619SJaehoon Chung case 3: 121664f4a619SJaehoon Chung mmc->version = MMC_VERSION_4_3; 121764f4a619SJaehoon Chung break; 121864f4a619SJaehoon Chung case 5: 121964f4a619SJaehoon Chung mmc->version = MMC_VERSION_4_41; 122064f4a619SJaehoon Chung break; 122164f4a619SJaehoon Chung case 6: 122264f4a619SJaehoon Chung mmc->version = MMC_VERSION_4_5; 122364f4a619SJaehoon Chung break; 1224edab723bSMarkus Niebel case 7: 1225edab723bSMarkus Niebel mmc->version = MMC_VERSION_5_0; 1226edab723bSMarkus Niebel break; 122764f4a619SJaehoon Chung } 122864f4a619SJaehoon Chung 12298a0cf490SDiego Santa Cruz /* The partition data may be non-zero but it is only 12308a0cf490SDiego Santa Cruz * effective if PARTITION_SETTING_COMPLETED is set in 12318a0cf490SDiego Santa Cruz * EXT_CSD, so ignore any data if this bit is not set, 12328a0cf490SDiego Santa Cruz * except for enabling the high-capacity group size 12338a0cf490SDiego Santa Cruz * definition (see below). */ 12348a0cf490SDiego Santa Cruz part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] & 12358a0cf490SDiego Santa Cruz EXT_CSD_PARTITION_SETTING_COMPLETED); 12368a0cf490SDiego Santa Cruz 12370c453bb7SDiego Santa Cruz /* store the partition info of emmc */ 12380c453bb7SDiego Santa Cruz mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT]; 12390c453bb7SDiego Santa Cruz if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) || 12400c453bb7SDiego Santa Cruz ext_csd[EXT_CSD_BOOT_MULT]) 12410c453bb7SDiego Santa Cruz mmc->part_config = ext_csd[EXT_CSD_PART_CONF]; 12428a0cf490SDiego Santa Cruz if (part_completed && 12438a0cf490SDiego Santa Cruz (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT)) 12440c453bb7SDiego Santa Cruz mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE]; 12450c453bb7SDiego Santa Cruz 12460c453bb7SDiego Santa Cruz mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17; 12470c453bb7SDiego Santa Cruz 12480c453bb7SDiego Santa Cruz mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17; 12490c453bb7SDiego Santa Cruz 12500c453bb7SDiego Santa Cruz for (i = 0; i < 4; i++) { 12510c453bb7SDiego Santa Cruz int idx = EXT_CSD_GP_SIZE_MULT + i * 3; 12528a0cf490SDiego Santa Cruz uint mult = (ext_csd[idx + 2] << 16) + 12530c453bb7SDiego Santa Cruz (ext_csd[idx + 1] << 8) + ext_csd[idx]; 12548a0cf490SDiego Santa Cruz if (mult) 12558a0cf490SDiego Santa Cruz has_parts = true; 12568a0cf490SDiego Santa Cruz if (!part_completed) 12578a0cf490SDiego Santa Cruz continue; 12588a0cf490SDiego Santa Cruz mmc->capacity_gp[i] = mult; 12590c453bb7SDiego Santa Cruz mmc->capacity_gp[i] *= 12600c453bb7SDiego Santa Cruz ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; 12610c453bb7SDiego Santa Cruz mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 1262f8e89d67SDiego Santa Cruz mmc->capacity_gp[i] <<= 19; 12630c453bb7SDiego Santa Cruz } 12640c453bb7SDiego Santa Cruz 12658a0cf490SDiego Santa Cruz if (part_completed) { 1266a7f852b6SDiego Santa Cruz mmc->enh_user_size = 1267a7f852b6SDiego Santa Cruz (ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) + 1268a7f852b6SDiego Santa Cruz (ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) + 1269a7f852b6SDiego Santa Cruz ext_csd[EXT_CSD_ENH_SIZE_MULT]; 1270a7f852b6SDiego Santa Cruz mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; 1271a7f852b6SDiego Santa Cruz mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 1272a7f852b6SDiego Santa Cruz mmc->enh_user_size <<= 19; 1273a7f852b6SDiego Santa Cruz mmc->enh_user_start = 1274a7f852b6SDiego Santa Cruz (ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) + 1275a7f852b6SDiego Santa Cruz (ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) + 1276a7f852b6SDiego Santa Cruz (ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) + 1277a7f852b6SDiego Santa Cruz ext_csd[EXT_CSD_ENH_START_ADDR]; 1278a7f852b6SDiego Santa Cruz if (mmc->high_capacity) 1279a7f852b6SDiego Santa Cruz mmc->enh_user_start <<= 9; 12808a0cf490SDiego Santa Cruz } 1281a7f852b6SDiego Santa Cruz 1282e6f99a56SLei Wen /* 12831937e5aaSOliver Metz * Host needs to enable ERASE_GRP_DEF bit if device is 12841937e5aaSOliver Metz * partitioned. This bit will be lost every time after a reset 12851937e5aaSOliver Metz * or power off. This will affect erase size. 1286e6f99a56SLei Wen */ 12878a0cf490SDiego Santa Cruz if (part_completed) 12880c453bb7SDiego Santa Cruz has_parts = true; 12891937e5aaSOliver Metz if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) && 12900c453bb7SDiego Santa Cruz (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB)) 12910c453bb7SDiego Santa Cruz has_parts = true; 12920c453bb7SDiego Santa Cruz if (has_parts) { 12931937e5aaSOliver Metz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 12941937e5aaSOliver Metz EXT_CSD_ERASE_GROUP_DEF, 1); 12951937e5aaSOliver Metz 12961937e5aaSOliver Metz if (err) 12971937e5aaSOliver Metz return err; 1298021a8055SHannes Petermaier else 1299021a8055SHannes Petermaier ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1; 1300037dc0abSDiego Santa Cruz } 13011937e5aaSOliver Metz 1302037dc0abSDiego Santa Cruz if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) { 13031937e5aaSOliver Metz /* Read out group size from ext_csd */ 13040560db18SLei Wen mmc->erase_grp_size = 1305a4ff9f83SDiego Santa Cruz ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024; 1306d7b29129SMarkus Niebel /* 1307d7b29129SMarkus Niebel * if high capacity and partition setting completed 1308d7b29129SMarkus Niebel * SEC_COUNT is valid even if it is smaller than 2 GiB 1309d7b29129SMarkus Niebel * JEDEC Standard JESD84-B45, 6.2.4 1310d7b29129SMarkus Niebel */ 13118a0cf490SDiego Santa Cruz if (mmc->high_capacity && part_completed) { 1312d7b29129SMarkus Niebel capacity = (ext_csd[EXT_CSD_SEC_CNT]) | 1313d7b29129SMarkus Niebel (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) | 1314d7b29129SMarkus Niebel (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) | 1315d7b29129SMarkus Niebel (ext_csd[EXT_CSD_SEC_CNT + 3] << 24); 1316d7b29129SMarkus Niebel capacity *= MMC_MAX_BLOCK_LEN; 1317d7b29129SMarkus Niebel mmc->capacity_user = capacity; 1318d7b29129SMarkus Niebel } 13198bfa195eSSimon Glass } else { 13201937e5aaSOliver Metz /* Calculate the group size from the csd value. */ 1321e6f99a56SLei Wen int erase_gsz, erase_gmul; 1322e6f99a56SLei Wen erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10; 1323e6f99a56SLei Wen erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5; 1324e6f99a56SLei Wen mmc->erase_grp_size = (erase_gsz + 1) 1325e6f99a56SLei Wen * (erase_gmul + 1); 1326e6f99a56SLei Wen } 1327037dc0abSDiego Santa Cruz 1328037dc0abSDiego Santa Cruz mmc->hc_wp_grp_size = 1024 1329037dc0abSDiego Santa Cruz * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] 1330037dc0abSDiego Santa Cruz * ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 13319e41a00bSDiego Santa Cruz 13329e41a00bSDiego Santa Cruz mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET]; 1333f866a46dSStephen Warren } 1334f866a46dSStephen Warren 1335873cc1d7SStephen Warren err = mmc_set_capacity(mmc, mmc->block_dev.hwpart); 1336f866a46dSStephen Warren if (err) 1337f866a46dSStephen Warren return err; 1338d23e2c09SSukumar Ghorai 1339272cc70bSAndy Fleming if (IS_SD(mmc)) 1340272cc70bSAndy Fleming err = sd_change_freq(mmc); 1341272cc70bSAndy Fleming else 1342272cc70bSAndy Fleming err = mmc_change_freq(mmc); 1343272cc70bSAndy Fleming 1344272cc70bSAndy Fleming if (err) 1345272cc70bSAndy Fleming return err; 1346272cc70bSAndy Fleming 1347272cc70bSAndy Fleming /* Restrict card's capabilities by what the host can do */ 134893bfd616SPantelis Antoniou mmc->card_caps &= mmc->cfg->host_caps; 1349272cc70bSAndy Fleming 1350272cc70bSAndy Fleming if (IS_SD(mmc)) { 1351272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_4BIT) { 1352272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 1353272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 1354272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1355272cc70bSAndy Fleming 1356272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1357272cc70bSAndy Fleming if (err) 1358272cc70bSAndy Fleming return err; 1359272cc70bSAndy Fleming 1360272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; 1361272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 1362272cc70bSAndy Fleming cmd.cmdarg = 2; 1363272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1364272cc70bSAndy Fleming if (err) 1365272cc70bSAndy Fleming return err; 1366272cc70bSAndy Fleming 1367272cc70bSAndy Fleming mmc_set_bus_width(mmc, 4); 1368272cc70bSAndy Fleming } 1369272cc70bSAndy Fleming 1370272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) 1371ad5fd922SJaehoon Chung mmc->tran_speed = 50000000; 1372272cc70bSAndy Fleming else 1373ad5fd922SJaehoon Chung mmc->tran_speed = 25000000; 1374fc5b32fbSAndrew Gabbasov } else if (mmc->version >= MMC_VERSION_4) { 1375fc5b32fbSAndrew Gabbasov /* Only version 4 of MMC supports wider bus widths */ 13767798f6dbSAndy Fleming int idx; 13777798f6dbSAndy Fleming 13787798f6dbSAndy Fleming /* An array of possible bus widths in order of preference */ 13797798f6dbSAndy Fleming static unsigned ext_csd_bits[] = { 1380d22e3d46SJaehoon Chung EXT_CSD_DDR_BUS_WIDTH_8, 1381d22e3d46SJaehoon Chung EXT_CSD_DDR_BUS_WIDTH_4, 13827798f6dbSAndy Fleming EXT_CSD_BUS_WIDTH_8, 13837798f6dbSAndy Fleming EXT_CSD_BUS_WIDTH_4, 13847798f6dbSAndy Fleming EXT_CSD_BUS_WIDTH_1, 13857798f6dbSAndy Fleming }; 13867798f6dbSAndy Fleming 13877798f6dbSAndy Fleming /* An array to map CSD bus widths to host cap bits */ 13887798f6dbSAndy Fleming static unsigned ext_to_hostcaps[] = { 1389786e8f81SAndrew Gabbasov [EXT_CSD_DDR_BUS_WIDTH_4] = 1390786e8f81SAndrew Gabbasov MMC_MODE_DDR_52MHz | MMC_MODE_4BIT, 1391786e8f81SAndrew Gabbasov [EXT_CSD_DDR_BUS_WIDTH_8] = 1392786e8f81SAndrew Gabbasov MMC_MODE_DDR_52MHz | MMC_MODE_8BIT, 13937798f6dbSAndy Fleming [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT, 13947798f6dbSAndy Fleming [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT, 13957798f6dbSAndy Fleming }; 13967798f6dbSAndy Fleming 13977798f6dbSAndy Fleming /* An array to map chosen bus width to an integer */ 13987798f6dbSAndy Fleming static unsigned widths[] = { 1399d22e3d46SJaehoon Chung 8, 4, 8, 4, 1, 14007798f6dbSAndy Fleming }; 14017798f6dbSAndy Fleming 14027798f6dbSAndy Fleming for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) { 14037798f6dbSAndy Fleming unsigned int extw = ext_csd_bits[idx]; 1404786e8f81SAndrew Gabbasov unsigned int caps = ext_to_hostcaps[extw]; 14057798f6dbSAndy Fleming 14067798f6dbSAndy Fleming /* 1407bf477073SAndrew Gabbasov * If the bus width is still not changed, 1408bf477073SAndrew Gabbasov * don't try to set the default again. 1409bf477073SAndrew Gabbasov * Otherwise, recover from switch attempts 1410bf477073SAndrew Gabbasov * by switching to 1-bit bus width. 1411bf477073SAndrew Gabbasov */ 1412bf477073SAndrew Gabbasov if (extw == EXT_CSD_BUS_WIDTH_1 && 1413bf477073SAndrew Gabbasov mmc->bus_width == 1) { 1414bf477073SAndrew Gabbasov err = 0; 1415bf477073SAndrew Gabbasov break; 1416bf477073SAndrew Gabbasov } 1417bf477073SAndrew Gabbasov 1418bf477073SAndrew Gabbasov /* 1419786e8f81SAndrew Gabbasov * Check to make sure the card and controller support 1420786e8f81SAndrew Gabbasov * these capabilities 14217798f6dbSAndy Fleming */ 1422786e8f81SAndrew Gabbasov if ((mmc->card_caps & caps) != caps) 14237798f6dbSAndy Fleming continue; 14247798f6dbSAndy Fleming 1425272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 14267798f6dbSAndy Fleming EXT_CSD_BUS_WIDTH, extw); 1427272cc70bSAndy Fleming 1428272cc70bSAndy Fleming if (err) 14294137894eSLei Wen continue; 1430272cc70bSAndy Fleming 1431786e8f81SAndrew Gabbasov mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0; 14327798f6dbSAndy Fleming mmc_set_bus_width(mmc, widths[idx]); 1433272cc70bSAndy Fleming 14344137894eSLei Wen err = mmc_send_ext_csd(mmc, test_csd); 1435272cc70bSAndy Fleming 1436786e8f81SAndrew Gabbasov if (err) 1437786e8f81SAndrew Gabbasov continue; 1438786e8f81SAndrew Gabbasov 1439786e8f81SAndrew Gabbasov /* Only compare read only fields */ 1440786e8f81SAndrew Gabbasov if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] 1441786e8f81SAndrew Gabbasov == test_csd[EXT_CSD_PARTITIONING_SUPPORT] && 1442786e8f81SAndrew Gabbasov ext_csd[EXT_CSD_HC_WP_GRP_SIZE] 1443786e8f81SAndrew Gabbasov == test_csd[EXT_CSD_HC_WP_GRP_SIZE] && 1444786e8f81SAndrew Gabbasov ext_csd[EXT_CSD_REV] 1445786e8f81SAndrew Gabbasov == test_csd[EXT_CSD_REV] && 1446786e8f81SAndrew Gabbasov ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] 1447786e8f81SAndrew Gabbasov == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] && 1448786e8f81SAndrew Gabbasov memcmp(&ext_csd[EXT_CSD_SEC_CNT], 1449786e8f81SAndrew Gabbasov &test_csd[EXT_CSD_SEC_CNT], 4) == 0) 14504137894eSLei Wen break; 1451786e8f81SAndrew Gabbasov else 1452786e8f81SAndrew Gabbasov err = SWITCH_ERR; 14534137894eSLei Wen } 1454786e8f81SAndrew Gabbasov 1455786e8f81SAndrew Gabbasov if (err) 1456786e8f81SAndrew Gabbasov return err; 1457272cc70bSAndy Fleming 1458272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) { 1459272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS_52MHz) 1460ad5fd922SJaehoon Chung mmc->tran_speed = 52000000; 1461272cc70bSAndy Fleming else 1462ad5fd922SJaehoon Chung mmc->tran_speed = 26000000; 1463272cc70bSAndy Fleming } 1464ad5fd922SJaehoon Chung } 1465ad5fd922SJaehoon Chung 1466ad5fd922SJaehoon Chung mmc_set_clock(mmc, mmc->tran_speed); 1467272cc70bSAndy Fleming 14685af8f45cSAndrew Gabbasov /* Fix the block length for DDR mode */ 14695af8f45cSAndrew Gabbasov if (mmc->ddr_mode) { 14705af8f45cSAndrew Gabbasov mmc->read_bl_len = MMC_MAX_BLOCK_LEN; 14715af8f45cSAndrew Gabbasov mmc->write_bl_len = MMC_MAX_BLOCK_LEN; 14725af8f45cSAndrew Gabbasov } 14735af8f45cSAndrew Gabbasov 1474272cc70bSAndy Fleming /* fill in device description */ 1475272cc70bSAndy Fleming mmc->block_dev.lun = 0; 1476873cc1d7SStephen Warren mmc->block_dev.hwpart = 0; 1477272cc70bSAndy Fleming mmc->block_dev.type = 0; 1478272cc70bSAndy Fleming mmc->block_dev.blksz = mmc->read_bl_len; 14790472fbfdSEgbert Eich mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz); 14809b1f942cSRabin Vincent mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); 1481fc011f64SSjoerd Simons #if !defined(CONFIG_SPL_BUILD) || \ 1482fc011f64SSjoerd Simons (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \ 1483fc011f64SSjoerd Simons !defined(CONFIG_USE_TINY_PRINTF)) 1484babce5f6STaylor Hutt sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x", 1485babce5f6STaylor Hutt mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff), 1486babce5f6STaylor Hutt (mmc->cid[3] >> 16) & 0xffff); 1487babce5f6STaylor Hutt sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff, 14880b453ffeSRabin Vincent (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, 1489babce5f6STaylor Hutt (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff, 1490babce5f6STaylor Hutt (mmc->cid[2] >> 24) & 0xff); 1491babce5f6STaylor Hutt sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf, 1492babce5f6STaylor Hutt (mmc->cid[2] >> 16) & 0xf); 149356196826SPaul Burton #else 149456196826SPaul Burton mmc->block_dev.vendor[0] = 0; 149556196826SPaul Burton mmc->block_dev.product[0] = 0; 149656196826SPaul Burton mmc->block_dev.revision[0] = 0; 149756196826SPaul Burton #endif 1498122efd43SMikhail Kshevetskiy #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT) 14993e8bd469SSimon Glass part_init(&mmc->block_dev); 1500122efd43SMikhail Kshevetskiy #endif 1501272cc70bSAndy Fleming 1502272cc70bSAndy Fleming return 0; 1503272cc70bSAndy Fleming } 1504272cc70bSAndy Fleming 1505fdbb873eSKim Phillips static int mmc_send_if_cond(struct mmc *mmc) 1506272cc70bSAndy Fleming { 1507272cc70bSAndy Fleming struct mmc_cmd cmd; 1508272cc70bSAndy Fleming int err; 1509272cc70bSAndy Fleming 1510272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_IF_COND; 1511272cc70bSAndy Fleming /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ 151293bfd616SPantelis Antoniou cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa; 1513272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R7; 1514272cc70bSAndy Fleming 1515272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1516272cc70bSAndy Fleming 1517272cc70bSAndy Fleming if (err) 1518272cc70bSAndy Fleming return err; 1519272cc70bSAndy Fleming 1520998be3ddSRabin Vincent if ((cmd.response[0] & 0xff) != 0xaa) 1521272cc70bSAndy Fleming return UNUSABLE_ERR; 1522272cc70bSAndy Fleming else 1523272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 1524272cc70bSAndy Fleming 1525272cc70bSAndy Fleming return 0; 1526272cc70bSAndy Fleming } 1527272cc70bSAndy Fleming 152893bfd616SPantelis Antoniou /* not used any more */ 152993bfd616SPantelis Antoniou int __deprecated mmc_register(struct mmc *mmc) 1530272cc70bSAndy Fleming { 153193bfd616SPantelis Antoniou #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 153293bfd616SPantelis Antoniou printf("%s is deprecated! use mmc_create() instead.\n", __func__); 153393bfd616SPantelis Antoniou #endif 153493bfd616SPantelis Antoniou return -1; 153593bfd616SPantelis Antoniou } 153693bfd616SPantelis Antoniou 153793bfd616SPantelis Antoniou struct mmc *mmc_create(const struct mmc_config *cfg, void *priv) 153893bfd616SPantelis Antoniou { 153993bfd616SPantelis Antoniou struct mmc *mmc; 154093bfd616SPantelis Antoniou 154193bfd616SPantelis Antoniou /* quick validation */ 154293bfd616SPantelis Antoniou if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL || 154393bfd616SPantelis Antoniou cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0) 154493bfd616SPantelis Antoniou return NULL; 154593bfd616SPantelis Antoniou 154693bfd616SPantelis Antoniou mmc = calloc(1, sizeof(*mmc)); 154793bfd616SPantelis Antoniou if (mmc == NULL) 154893bfd616SPantelis Antoniou return NULL; 154993bfd616SPantelis Antoniou 155093bfd616SPantelis Antoniou mmc->cfg = cfg; 155193bfd616SPantelis Antoniou mmc->priv = priv; 155293bfd616SPantelis Antoniou 155393bfd616SPantelis Antoniou /* the following chunk was mmc_register() */ 155493bfd616SPantelis Antoniou 1555ab71188cSMarkus Niebel /* Setup dsr related values */ 1556ab71188cSMarkus Niebel mmc->dsr_imp = 0; 1557ab71188cSMarkus Niebel mmc->dsr = 0xffffffff; 1558272cc70bSAndy Fleming /* Setup the universal parts of the block interface just once */ 1559272cc70bSAndy Fleming mmc->block_dev.if_type = IF_TYPE_MMC; 1560bcce53d0SSimon Glass mmc->block_dev.devnum = cur_dev_num++; 1561272cc70bSAndy Fleming mmc->block_dev.removable = 1; 1562272cc70bSAndy Fleming mmc->block_dev.block_read = mmc_bread; 1563272cc70bSAndy Fleming mmc->block_dev.block_write = mmc_bwrite; 1564e6f99a56SLei Wen mmc->block_dev.block_erase = mmc_berase; 156593bfd616SPantelis Antoniou 156693bfd616SPantelis Antoniou /* setup initial part type */ 156793bfd616SPantelis Antoniou mmc->block_dev.part_type = mmc->cfg->part_type; 1568272cc70bSAndy Fleming 1569272cc70bSAndy Fleming INIT_LIST_HEAD(&mmc->link); 1570272cc70bSAndy Fleming 1571272cc70bSAndy Fleming list_add_tail(&mmc->link, &mmc_devices); 1572272cc70bSAndy Fleming 157393bfd616SPantelis Antoniou return mmc; 157493bfd616SPantelis Antoniou } 157593bfd616SPantelis Antoniou 157693bfd616SPantelis Antoniou void mmc_destroy(struct mmc *mmc) 157793bfd616SPantelis Antoniou { 157893bfd616SPantelis Antoniou /* only freeing memory for now */ 157993bfd616SPantelis Antoniou free(mmc); 1580272cc70bSAndy Fleming } 1581272cc70bSAndy Fleming 15823c457f4dSSimon Glass static int mmc_get_dev(int dev, struct blk_desc **descp) 1583663acabdSSimon Glass { 1584663acabdSSimon Glass struct mmc *mmc = find_mmc_device(dev); 1585663acabdSSimon Glass int ret; 1586663acabdSSimon Glass 1587663acabdSSimon Glass if (!mmc) 1588663acabdSSimon Glass return -ENODEV; 1589663acabdSSimon Glass ret = mmc_init(mmc); 1590663acabdSSimon Glass if (ret) 1591663acabdSSimon Glass return ret; 1592663acabdSSimon Glass 1593663acabdSSimon Glass *descp = &mmc->block_dev; 1594663acabdSSimon Glass 1595663acabdSSimon Glass return 0; 1596663acabdSSimon Glass } 1597663acabdSSimon Glass 159895de9ab2SPaul Kocialkowski /* board-specific MMC power initializations. */ 159995de9ab2SPaul Kocialkowski __weak void board_mmc_power_init(void) 160095de9ab2SPaul Kocialkowski { 160195de9ab2SPaul Kocialkowski } 160295de9ab2SPaul Kocialkowski 1603e9550449SChe-Liang Chiou int mmc_start_init(struct mmc *mmc) 1604272cc70bSAndy Fleming { 1605afd5932bSMacpaul Lin int err; 1606272cc70bSAndy Fleming 1607ab769f22SPantelis Antoniou /* we pretend there's no card when init is NULL */ 160893bfd616SPantelis Antoniou if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) { 160948972d90SThierry Reding mmc->has_init = 0; 161056196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 161148972d90SThierry Reding printf("MMC: no card present\n"); 161256196826SPaul Burton #endif 161348972d90SThierry Reding return NO_CARD_ERR; 161448972d90SThierry Reding } 161548972d90SThierry Reding 1616bc897b1dSLei Wen if (mmc->has_init) 1617bc897b1dSLei Wen return 0; 1618bc897b1dSLei Wen 16195a8dbdc6SYangbo Lu #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT 16205a8dbdc6SYangbo Lu mmc_adapter_card_type_ident(); 16215a8dbdc6SYangbo Lu #endif 162295de9ab2SPaul Kocialkowski board_mmc_power_init(); 162395de9ab2SPaul Kocialkowski 1624ab769f22SPantelis Antoniou /* made sure it's not NULL earlier */ 162593bfd616SPantelis Antoniou err = mmc->cfg->ops->init(mmc); 1626272cc70bSAndy Fleming 1627272cc70bSAndy Fleming if (err) 1628272cc70bSAndy Fleming return err; 1629272cc70bSAndy Fleming 1630786e8f81SAndrew Gabbasov mmc->ddr_mode = 0; 1631b86b85e2SIlya Yanok mmc_set_bus_width(mmc, 1); 1632b86b85e2SIlya Yanok mmc_set_clock(mmc, 1); 1633b86b85e2SIlya Yanok 1634272cc70bSAndy Fleming /* Reset the Card */ 1635272cc70bSAndy Fleming err = mmc_go_idle(mmc); 1636272cc70bSAndy Fleming 1637272cc70bSAndy Fleming if (err) 1638272cc70bSAndy Fleming return err; 1639272cc70bSAndy Fleming 1640bc897b1dSLei Wen /* The internal partition reset to user partition(0) at every CMD0*/ 1641873cc1d7SStephen Warren mmc->block_dev.hwpart = 0; 1642bc897b1dSLei Wen 1643272cc70bSAndy Fleming /* Test for SD version 2 */ 1644272cc70bSAndy Fleming err = mmc_send_if_cond(mmc); 1645272cc70bSAndy Fleming 1646272cc70bSAndy Fleming /* Now try to get the SD card's operating condition */ 1647272cc70bSAndy Fleming err = sd_send_op_cond(mmc); 1648272cc70bSAndy Fleming 1649272cc70bSAndy Fleming /* If the command timed out, we check for an MMC card */ 1650272cc70bSAndy Fleming if (err == TIMEOUT) { 1651272cc70bSAndy Fleming err = mmc_send_op_cond(mmc); 1652272cc70bSAndy Fleming 1653bd47c135SAndrew Gabbasov if (err) { 165456196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1655272cc70bSAndy Fleming printf("Card did not respond to voltage select!\n"); 165656196826SPaul Burton #endif 1657272cc70bSAndy Fleming return UNUSABLE_ERR; 1658272cc70bSAndy Fleming } 1659272cc70bSAndy Fleming } 1660272cc70bSAndy Fleming 1661bd47c135SAndrew Gabbasov if (!err) 1662e9550449SChe-Liang Chiou mmc->init_in_progress = 1; 1663e9550449SChe-Liang Chiou 1664e9550449SChe-Liang Chiou return err; 1665e9550449SChe-Liang Chiou } 1666e9550449SChe-Liang Chiou 1667e9550449SChe-Liang Chiou static int mmc_complete_init(struct mmc *mmc) 1668e9550449SChe-Liang Chiou { 1669e9550449SChe-Liang Chiou int err = 0; 1670e9550449SChe-Liang Chiou 1671bd47c135SAndrew Gabbasov mmc->init_in_progress = 0; 1672e9550449SChe-Liang Chiou if (mmc->op_cond_pending) 1673e9550449SChe-Liang Chiou err = mmc_complete_op_cond(mmc); 1674e9550449SChe-Liang Chiou 1675e9550449SChe-Liang Chiou if (!err) 1676bc897b1dSLei Wen err = mmc_startup(mmc); 1677bc897b1dSLei Wen if (err) 1678bc897b1dSLei Wen mmc->has_init = 0; 1679bc897b1dSLei Wen else 1680bc897b1dSLei Wen mmc->has_init = 1; 1681e9550449SChe-Liang Chiou return err; 1682e9550449SChe-Liang Chiou } 1683e9550449SChe-Liang Chiou 1684e9550449SChe-Liang Chiou int mmc_init(struct mmc *mmc) 1685e9550449SChe-Liang Chiou { 1686bd47c135SAndrew Gabbasov int err = 0; 1687d803fea5SMateusz Zalega unsigned start; 1688e9550449SChe-Liang Chiou 1689e9550449SChe-Liang Chiou if (mmc->has_init) 1690e9550449SChe-Liang Chiou return 0; 1691d803fea5SMateusz Zalega 1692d803fea5SMateusz Zalega start = get_timer(0); 1693d803fea5SMateusz Zalega 1694e9550449SChe-Liang Chiou if (!mmc->init_in_progress) 1695e9550449SChe-Liang Chiou err = mmc_start_init(mmc); 1696e9550449SChe-Liang Chiou 1697bd47c135SAndrew Gabbasov if (!err) 1698e9550449SChe-Liang Chiou err = mmc_complete_init(mmc); 1699e9550449SChe-Liang Chiou debug("%s: %d, time %lu\n", __func__, err, get_timer(start)); 1700bc897b1dSLei Wen return err; 1701272cc70bSAndy Fleming } 1702272cc70bSAndy Fleming 1703ab71188cSMarkus Niebel int mmc_set_dsr(struct mmc *mmc, u16 val) 1704ab71188cSMarkus Niebel { 1705ab71188cSMarkus Niebel mmc->dsr = val; 1706ab71188cSMarkus Niebel return 0; 1707ab71188cSMarkus Niebel } 1708ab71188cSMarkus Niebel 1709cee9ab7cSJeroen Hofstee /* CPU-specific MMC initializations */ 1710cee9ab7cSJeroen Hofstee __weak int cpu_mmc_init(bd_t *bis) 1711272cc70bSAndy Fleming { 1712272cc70bSAndy Fleming return -1; 1713272cc70bSAndy Fleming } 1714272cc70bSAndy Fleming 1715cee9ab7cSJeroen Hofstee /* board-specific MMC initializations. */ 1716cee9ab7cSJeroen Hofstee __weak int board_mmc_init(bd_t *bis) 1717cee9ab7cSJeroen Hofstee { 1718cee9ab7cSJeroen Hofstee return -1; 1719cee9ab7cSJeroen Hofstee } 1720272cc70bSAndy Fleming 172156196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 172256196826SPaul Burton 1723272cc70bSAndy Fleming void print_mmc_devices(char separator) 1724272cc70bSAndy Fleming { 1725272cc70bSAndy Fleming struct mmc *m; 1726272cc70bSAndy Fleming struct list_head *entry; 172734dd9284SPrzemyslaw Marczak char *mmc_type; 1728272cc70bSAndy Fleming 1729272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 1730272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 1731272cc70bSAndy Fleming 173234dd9284SPrzemyslaw Marczak if (m->has_init) 173334dd9284SPrzemyslaw Marczak mmc_type = IS_SD(m) ? "SD" : "eMMC"; 173434dd9284SPrzemyslaw Marczak else 173534dd9284SPrzemyslaw Marczak mmc_type = NULL; 173634dd9284SPrzemyslaw Marczak 1737bcce53d0SSimon Glass printf("%s: %d", m->cfg->name, m->block_dev.devnum); 173834dd9284SPrzemyslaw Marczak if (mmc_type) 173934dd9284SPrzemyslaw Marczak printf(" (%s)", mmc_type); 1740272cc70bSAndy Fleming 1741e75eaf10SLubomir Popov if (entry->next != &mmc_devices) { 1742272cc70bSAndy Fleming printf("%c", separator); 1743e75eaf10SLubomir Popov if (separator != '\n') 1744e75eaf10SLubomir Popov puts (" "); 1745e75eaf10SLubomir Popov } 1746272cc70bSAndy Fleming } 1747272cc70bSAndy Fleming 1748272cc70bSAndy Fleming printf("\n"); 1749272cc70bSAndy Fleming } 1750272cc70bSAndy Fleming 175156196826SPaul Burton #else 175256196826SPaul Burton void print_mmc_devices(char separator) { } 175356196826SPaul Burton #endif 175456196826SPaul Burton 1755ea6ebe21SLei Wen int get_mmc_num(void) 1756ea6ebe21SLei Wen { 1757ea6ebe21SLei Wen return cur_dev_num; 1758ea6ebe21SLei Wen } 1759ea6ebe21SLei Wen 1760e9550449SChe-Liang Chiou void mmc_set_preinit(struct mmc *mmc, int preinit) 1761e9550449SChe-Liang Chiou { 1762e9550449SChe-Liang Chiou mmc->preinit = preinit; 1763e9550449SChe-Liang Chiou } 1764e9550449SChe-Liang Chiou 1765e9550449SChe-Liang Chiou static void do_preinit(void) 1766e9550449SChe-Liang Chiou { 1767e9550449SChe-Liang Chiou struct mmc *m; 1768e9550449SChe-Liang Chiou struct list_head *entry; 1769e9550449SChe-Liang Chiou 1770e9550449SChe-Liang Chiou list_for_each(entry, &mmc_devices) { 1771e9550449SChe-Liang Chiou m = list_entry(entry, struct mmc, link); 1772e9550449SChe-Liang Chiou 17735a8dbdc6SYangbo Lu #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT 17745a8dbdc6SYangbo Lu mmc_set_preinit(m, 1); 17755a8dbdc6SYangbo Lu #endif 1776e9550449SChe-Liang Chiou if (m->preinit) 1777e9550449SChe-Liang Chiou mmc_start_init(m); 1778e9550449SChe-Liang Chiou } 1779e9550449SChe-Liang Chiou } 1780e9550449SChe-Liang Chiou 17818e3332e2SSjoerd Simons #if defined(CONFIG_DM_MMC) && defined(CONFIG_SPL_BUILD) 17828e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis) 17838e3332e2SSjoerd Simons { 17848e3332e2SSjoerd Simons return 0; 17858e3332e2SSjoerd Simons } 17868e3332e2SSjoerd Simons #elif defined(CONFIG_DM_MMC) 17878e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis) 17888e3332e2SSjoerd Simons { 17894a1db6d8SSimon Glass int ret, i; 17908e3332e2SSjoerd Simons struct uclass *uc; 17914a1db6d8SSimon Glass struct udevice *dev; 17928e3332e2SSjoerd Simons 17938e3332e2SSjoerd Simons ret = uclass_get(UCLASS_MMC, &uc); 17948e3332e2SSjoerd Simons if (ret) 17958e3332e2SSjoerd Simons return ret; 17968e3332e2SSjoerd Simons 17974a1db6d8SSimon Glass /* 17984a1db6d8SSimon Glass * Try to add them in sequence order. Really with driver model we 17994a1db6d8SSimon Glass * should allow holes, but the current MMC list does not allow that. 18004a1db6d8SSimon Glass * So if we request 0, 1, 3 we will get 0, 1, 2. 18014a1db6d8SSimon Glass */ 18024a1db6d8SSimon Glass for (i = 0; ; i++) { 18034a1db6d8SSimon Glass ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev); 18044a1db6d8SSimon Glass if (ret == -ENODEV) 18054a1db6d8SSimon Glass break; 18064a1db6d8SSimon Glass } 18074a1db6d8SSimon Glass uclass_foreach_dev(dev, uc) { 18084a1db6d8SSimon Glass ret = device_probe(dev); 18098e3332e2SSjoerd Simons if (ret) 18104a1db6d8SSimon Glass printf("%s - probe failed: %d\n", dev->name, ret); 18118e3332e2SSjoerd Simons } 18128e3332e2SSjoerd Simons 18138e3332e2SSjoerd Simons return 0; 18148e3332e2SSjoerd Simons } 18158e3332e2SSjoerd Simons #else 18168e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis) 18178e3332e2SSjoerd Simons { 18188e3332e2SSjoerd Simons if (board_mmc_init(bis) < 0) 18198e3332e2SSjoerd Simons cpu_mmc_init(bis); 18208e3332e2SSjoerd Simons 18218e3332e2SSjoerd Simons return 0; 18228e3332e2SSjoerd Simons } 18238e3332e2SSjoerd Simons #endif 1824e9550449SChe-Liang Chiou 1825272cc70bSAndy Fleming int mmc_initialize(bd_t *bis) 1826272cc70bSAndy Fleming { 18271b26bab1SDaniel Kochmański static int initialized = 0; 18288e3332e2SSjoerd Simons int ret; 18291b26bab1SDaniel Kochmański if (initialized) /* Avoid initializing mmc multiple times */ 18301b26bab1SDaniel Kochmański return 0; 18311b26bab1SDaniel Kochmański initialized = 1; 18321b26bab1SDaniel Kochmański 1833272cc70bSAndy Fleming INIT_LIST_HEAD (&mmc_devices); 1834272cc70bSAndy Fleming cur_dev_num = 0; 1835272cc70bSAndy Fleming 18368e3332e2SSjoerd Simons ret = mmc_probe(bis); 18378e3332e2SSjoerd Simons if (ret) 18388e3332e2SSjoerd Simons return ret; 1839272cc70bSAndy Fleming 1840bb0dc108SYing Zhang #ifndef CONFIG_SPL_BUILD 1841272cc70bSAndy Fleming print_mmc_devices(','); 1842bb0dc108SYing Zhang #endif 1843272cc70bSAndy Fleming 1844e9550449SChe-Liang Chiou do_preinit(); 1845272cc70bSAndy Fleming return 0; 1846272cc70bSAndy Fleming } 18473690d6d6SAmar 18483690d6d6SAmar #ifdef CONFIG_SUPPORT_EMMC_BOOT 18493690d6d6SAmar /* 18503690d6d6SAmar * This function changes the size of boot partition and the size of rpmb 18513690d6d6SAmar * partition present on EMMC devices. 18523690d6d6SAmar * 18533690d6d6SAmar * Input Parameters: 18543690d6d6SAmar * struct *mmc: pointer for the mmc device strcuture 18553690d6d6SAmar * bootsize: size of boot partition 18563690d6d6SAmar * rpmbsize: size of rpmb partition 18573690d6d6SAmar * 18583690d6d6SAmar * Returns 0 on success. 18593690d6d6SAmar */ 18603690d6d6SAmar 18613690d6d6SAmar int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize, 18623690d6d6SAmar unsigned long rpmbsize) 18633690d6d6SAmar { 18643690d6d6SAmar int err; 18653690d6d6SAmar struct mmc_cmd cmd; 18663690d6d6SAmar 18673690d6d6SAmar /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */ 18683690d6d6SAmar cmd.cmdidx = MMC_CMD_RES_MAN; 18693690d6d6SAmar cmd.resp_type = MMC_RSP_R1b; 18703690d6d6SAmar cmd.cmdarg = MMC_CMD62_ARG1; 18713690d6d6SAmar 18723690d6d6SAmar err = mmc_send_cmd(mmc, &cmd, NULL); 18733690d6d6SAmar if (err) { 18743690d6d6SAmar debug("mmc_boot_partition_size_change: Error1 = %d\n", err); 18753690d6d6SAmar return err; 18763690d6d6SAmar } 18773690d6d6SAmar 18783690d6d6SAmar /* Boot partition changing mode */ 18793690d6d6SAmar cmd.cmdidx = MMC_CMD_RES_MAN; 18803690d6d6SAmar cmd.resp_type = MMC_RSP_R1b; 18813690d6d6SAmar cmd.cmdarg = MMC_CMD62_ARG2; 18823690d6d6SAmar 18833690d6d6SAmar err = mmc_send_cmd(mmc, &cmd, NULL); 18843690d6d6SAmar if (err) { 18853690d6d6SAmar debug("mmc_boot_partition_size_change: Error2 = %d\n", err); 18863690d6d6SAmar return err; 18873690d6d6SAmar } 18883690d6d6SAmar /* boot partition size is multiple of 128KB */ 18893690d6d6SAmar bootsize = (bootsize * 1024) / 128; 18903690d6d6SAmar 18913690d6d6SAmar /* Arg: boot partition size */ 18923690d6d6SAmar cmd.cmdidx = MMC_CMD_RES_MAN; 18933690d6d6SAmar cmd.resp_type = MMC_RSP_R1b; 18943690d6d6SAmar cmd.cmdarg = bootsize; 18953690d6d6SAmar 18963690d6d6SAmar err = mmc_send_cmd(mmc, &cmd, NULL); 18973690d6d6SAmar if (err) { 18983690d6d6SAmar debug("mmc_boot_partition_size_change: Error3 = %d\n", err); 18993690d6d6SAmar return err; 19003690d6d6SAmar } 19013690d6d6SAmar /* RPMB partition size is multiple of 128KB */ 19023690d6d6SAmar rpmbsize = (rpmbsize * 1024) / 128; 19033690d6d6SAmar /* Arg: RPMB partition size */ 19043690d6d6SAmar cmd.cmdidx = MMC_CMD_RES_MAN; 19053690d6d6SAmar cmd.resp_type = MMC_RSP_R1b; 19063690d6d6SAmar cmd.cmdarg = rpmbsize; 19073690d6d6SAmar 19083690d6d6SAmar err = mmc_send_cmd(mmc, &cmd, NULL); 19093690d6d6SAmar if (err) { 19103690d6d6SAmar debug("mmc_boot_partition_size_change: Error4 = %d\n", err); 19113690d6d6SAmar return err; 19123690d6d6SAmar } 19133690d6d6SAmar return 0; 19143690d6d6SAmar } 19153690d6d6SAmar 19163690d6d6SAmar /* 19175a99b9deSTom Rini * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH 19185a99b9deSTom Rini * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH 19195a99b9deSTom Rini * and BOOT_MODE. 19205a99b9deSTom Rini * 19215a99b9deSTom Rini * Returns 0 on success. 19225a99b9deSTom Rini */ 19235a99b9deSTom Rini int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode) 19245a99b9deSTom Rini { 19255a99b9deSTom Rini int err; 19265a99b9deSTom Rini 19275a99b9deSTom Rini err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH, 19285a99b9deSTom Rini EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) | 19295a99b9deSTom Rini EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) | 19305a99b9deSTom Rini EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width)); 19315a99b9deSTom Rini 19325a99b9deSTom Rini if (err) 19335a99b9deSTom Rini return err; 19345a99b9deSTom Rini return 0; 19355a99b9deSTom Rini } 19365a99b9deSTom Rini 19375a99b9deSTom Rini /* 1938792970b0STom Rini * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG) 1939792970b0STom Rini * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and 1940792970b0STom Rini * PARTITION_ACCESS. 1941792970b0STom Rini * 1942792970b0STom Rini * Returns 0 on success. 1943792970b0STom Rini */ 1944792970b0STom Rini int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access) 1945792970b0STom Rini { 1946792970b0STom Rini int err; 1947792970b0STom Rini 1948792970b0STom Rini err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, 1949792970b0STom Rini EXT_CSD_BOOT_ACK(ack) | 1950792970b0STom Rini EXT_CSD_BOOT_PART_NUM(part_num) | 1951792970b0STom Rini EXT_CSD_PARTITION_ACCESS(access)); 1952792970b0STom Rini 1953792970b0STom Rini if (err) 1954792970b0STom Rini return err; 1955792970b0STom Rini return 0; 1956792970b0STom Rini } 195733ace362STom Rini 195833ace362STom Rini /* 195933ace362STom Rini * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value 196033ace362STom Rini * for enable. Note that this is a write-once field for non-zero values. 196133ace362STom Rini * 196233ace362STom Rini * Returns 0 on success. 196333ace362STom Rini */ 196433ace362STom Rini int mmc_set_rst_n_function(struct mmc *mmc, u8 enable) 196533ace362STom Rini { 196633ace362STom Rini return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION, 196733ace362STom Rini enable); 196833ace362STom Rini } 19693690d6d6SAmar #endif 1970663acabdSSimon Glass 1971663acabdSSimon Glass U_BOOT_LEGACY_BLK(mmc) = { 1972663acabdSSimon Glass .if_typename = "mmc", 1973663acabdSSimon Glass .if_type = IF_TYPE_MMC, 1974663acabdSSimon Glass .max_devs = -1, 19753c457f4dSSimon Glass .get_dev = mmc_get_dev, 1976663acabdSSimon Glass }; 1977