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> 13d4622df3SStephen Warren #include <errno.h> 14272cc70bSAndy Fleming #include <mmc.h> 15272cc70bSAndy Fleming #include <part.h> 16272cc70bSAndy Fleming #include <malloc.h> 17272cc70bSAndy Fleming #include <linux/list.h> 189b1f942cSRabin Vincent #include <div64.h> 19da61fa5fSPaul Burton #include "mmc_private.h" 20272cc70bSAndy Fleming 21272cc70bSAndy Fleming static struct list_head mmc_devices; 22272cc70bSAndy Fleming static int cur_dev_num = -1; 23272cc70bSAndy Fleming 24750121c3SJeroen Hofstee __weak int board_mmc_getwp(struct mmc *mmc) 25d23d8d7eSNikita Kiryanov { 26d23d8d7eSNikita Kiryanov return -1; 27d23d8d7eSNikita Kiryanov } 28d23d8d7eSNikita Kiryanov 29d23d8d7eSNikita Kiryanov int mmc_getwp(struct mmc *mmc) 30d23d8d7eSNikita Kiryanov { 31d23d8d7eSNikita Kiryanov int wp; 32d23d8d7eSNikita Kiryanov 33d23d8d7eSNikita Kiryanov wp = board_mmc_getwp(mmc); 34d23d8d7eSNikita Kiryanov 35d4e1da4eSPeter Korsgaard if (wp < 0) { 3693bfd616SPantelis Antoniou if (mmc->cfg->ops->getwp) 3793bfd616SPantelis Antoniou wp = mmc->cfg->ops->getwp(mmc); 38d4e1da4eSPeter Korsgaard else 39d4e1da4eSPeter Korsgaard wp = 0; 40d4e1da4eSPeter Korsgaard } 41d23d8d7eSNikita Kiryanov 42d23d8d7eSNikita Kiryanov return wp; 43d23d8d7eSNikita Kiryanov } 44d23d8d7eSNikita Kiryanov 45cee9ab7cSJeroen Hofstee __weak int board_mmc_getcd(struct mmc *mmc) 46cee9ab7cSJeroen Hofstee { 4711fdade2SStefano Babic return -1; 4811fdade2SStefano Babic } 4911fdade2SStefano Babic 50da61fa5fSPaul Burton int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) 51272cc70bSAndy Fleming { 525db2fe3aSRaffaele Recalcati int ret; 538635ff9eSMarek Vasut 548635ff9eSMarek Vasut #ifdef CONFIG_MMC_TRACE 555db2fe3aSRaffaele Recalcati int i; 565db2fe3aSRaffaele Recalcati u8 *ptr; 575db2fe3aSRaffaele Recalcati 585db2fe3aSRaffaele Recalcati printf("CMD_SEND:%d\n", cmd->cmdidx); 595db2fe3aSRaffaele Recalcati printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg); 6093bfd616SPantelis Antoniou ret = mmc->cfg->ops->send_cmd(mmc, cmd, data); 615db2fe3aSRaffaele Recalcati switch (cmd->resp_type) { 625db2fe3aSRaffaele Recalcati case MMC_RSP_NONE: 635db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_NONE\n"); 645db2fe3aSRaffaele Recalcati break; 655db2fe3aSRaffaele Recalcati case MMC_RSP_R1: 665db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n", 675db2fe3aSRaffaele Recalcati cmd->response[0]); 685db2fe3aSRaffaele Recalcati break; 695db2fe3aSRaffaele Recalcati case MMC_RSP_R1b: 705db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n", 715db2fe3aSRaffaele Recalcati cmd->response[0]); 725db2fe3aSRaffaele Recalcati break; 735db2fe3aSRaffaele Recalcati case MMC_RSP_R2: 745db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R2\t\t 0x%08X \n", 755db2fe3aSRaffaele Recalcati cmd->response[0]); 765db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 775db2fe3aSRaffaele Recalcati cmd->response[1]); 785db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 795db2fe3aSRaffaele Recalcati cmd->response[2]); 805db2fe3aSRaffaele Recalcati printf("\t\t \t\t 0x%08X \n", 815db2fe3aSRaffaele Recalcati cmd->response[3]); 825db2fe3aSRaffaele Recalcati printf("\n"); 835db2fe3aSRaffaele Recalcati printf("\t\t\t\t\tDUMPING DATA\n"); 845db2fe3aSRaffaele Recalcati for (i = 0; i < 4; i++) { 855db2fe3aSRaffaele Recalcati int j; 865db2fe3aSRaffaele Recalcati printf("\t\t\t\t\t%03d - ", i*4); 87146bec79SDirk Behme ptr = (u8 *)&cmd->response[i]; 885db2fe3aSRaffaele Recalcati ptr += 3; 895db2fe3aSRaffaele Recalcati for (j = 0; j < 4; j++) 905db2fe3aSRaffaele Recalcati printf("%02X ", *ptr--); 915db2fe3aSRaffaele Recalcati printf("\n"); 925db2fe3aSRaffaele Recalcati } 935db2fe3aSRaffaele Recalcati break; 945db2fe3aSRaffaele Recalcati case MMC_RSP_R3: 955db2fe3aSRaffaele Recalcati printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n", 965db2fe3aSRaffaele Recalcati cmd->response[0]); 975db2fe3aSRaffaele Recalcati break; 985db2fe3aSRaffaele Recalcati default: 995db2fe3aSRaffaele Recalcati printf("\t\tERROR MMC rsp not supported\n"); 1005db2fe3aSRaffaele Recalcati break; 1015db2fe3aSRaffaele Recalcati } 1025db2fe3aSRaffaele Recalcati #else 10393bfd616SPantelis Antoniou ret = mmc->cfg->ops->send_cmd(mmc, cmd, data); 1045db2fe3aSRaffaele Recalcati #endif 1058635ff9eSMarek Vasut return ret; 106272cc70bSAndy Fleming } 107272cc70bSAndy Fleming 108da61fa5fSPaul Burton int mmc_send_status(struct mmc *mmc, int timeout) 1095d4fc8d9SRaffaele Recalcati { 1105d4fc8d9SRaffaele Recalcati struct mmc_cmd cmd; 111d617c426SJan Kloetzke int err, retries = 5; 1125d4fc8d9SRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 1135d4fc8d9SRaffaele Recalcati int status; 1145d4fc8d9SRaffaele Recalcati #endif 1155d4fc8d9SRaffaele Recalcati 1165d4fc8d9SRaffaele Recalcati cmd.cmdidx = MMC_CMD_SEND_STATUS; 1175d4fc8d9SRaffaele Recalcati cmd.resp_type = MMC_RSP_R1; 118aaf3d41aSMarek Vasut if (!mmc_host_is_spi(mmc)) 119aaf3d41aSMarek Vasut cmd.cmdarg = mmc->rca << 16; 1205d4fc8d9SRaffaele Recalcati 1215d4fc8d9SRaffaele Recalcati do { 1225d4fc8d9SRaffaele Recalcati err = mmc_send_cmd(mmc, &cmd, NULL); 123d617c426SJan Kloetzke if (!err) { 124d617c426SJan Kloetzke if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) && 125d617c426SJan Kloetzke (cmd.response[0] & MMC_STATUS_CURR_STATE) != 126d617c426SJan Kloetzke MMC_STATE_PRG) 1275d4fc8d9SRaffaele Recalcati break; 128d617c426SJan Kloetzke else if (cmd.response[0] & MMC_STATUS_MASK) { 12956196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 130d617c426SJan Kloetzke printf("Status Error: 0x%08X\n", 131d617c426SJan Kloetzke cmd.response[0]); 13256196826SPaul Burton #endif 133d617c426SJan Kloetzke return COMM_ERR; 134d617c426SJan Kloetzke } 135d617c426SJan Kloetzke } else if (--retries < 0) 136d617c426SJan Kloetzke return err; 1375d4fc8d9SRaffaele Recalcati 1385d4fc8d9SRaffaele Recalcati udelay(1000); 1395d4fc8d9SRaffaele Recalcati 1405d4fc8d9SRaffaele Recalcati } while (timeout--); 1415d4fc8d9SRaffaele Recalcati 1425db2fe3aSRaffaele Recalcati #ifdef CONFIG_MMC_TRACE 1435db2fe3aSRaffaele Recalcati status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9; 1445db2fe3aSRaffaele Recalcati printf("CURR STATE:%d\n", status); 1455db2fe3aSRaffaele Recalcati #endif 1465b0c942fSJongman Heo if (timeout <= 0) { 14756196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1485d4fc8d9SRaffaele Recalcati printf("Timeout waiting card ready\n"); 14956196826SPaul Burton #endif 1505d4fc8d9SRaffaele Recalcati return TIMEOUT; 1515d4fc8d9SRaffaele Recalcati } 1526b2221b0SAndrew Gabbasov if (cmd.response[0] & MMC_STATUS_SWITCH_ERROR) 1536b2221b0SAndrew Gabbasov return SWITCH_ERR; 1545d4fc8d9SRaffaele Recalcati 1555d4fc8d9SRaffaele Recalcati return 0; 1565d4fc8d9SRaffaele Recalcati } 1575d4fc8d9SRaffaele Recalcati 158da61fa5fSPaul Burton int mmc_set_blocklen(struct mmc *mmc, int len) 159272cc70bSAndy Fleming { 160272cc70bSAndy Fleming struct mmc_cmd cmd; 161272cc70bSAndy Fleming 162786e8f81SAndrew Gabbasov if (mmc->ddr_mode) 163d22e3d46SJaehoon Chung return 0; 164d22e3d46SJaehoon Chung 165272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; 166272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 167272cc70bSAndy Fleming cmd.cmdarg = len; 168272cc70bSAndy Fleming 169272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, NULL); 170272cc70bSAndy Fleming } 171272cc70bSAndy Fleming 172272cc70bSAndy Fleming struct mmc *find_mmc_device(int dev_num) 173272cc70bSAndy Fleming { 174272cc70bSAndy Fleming struct mmc *m; 175272cc70bSAndy Fleming struct list_head *entry; 176272cc70bSAndy Fleming 177272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 178272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 179272cc70bSAndy Fleming 180272cc70bSAndy Fleming if (m->block_dev.dev == dev_num) 181272cc70bSAndy Fleming return m; 182272cc70bSAndy Fleming } 183272cc70bSAndy Fleming 18456196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 185272cc70bSAndy Fleming printf("MMC Device %d not found\n", dev_num); 18656196826SPaul Burton #endif 187272cc70bSAndy Fleming 188272cc70bSAndy Fleming return NULL; 189272cc70bSAndy Fleming } 190272cc70bSAndy Fleming 191ff8fef56SSascha Silbe static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start, 192fdbb873eSKim Phillips lbaint_t blkcnt) 193272cc70bSAndy Fleming { 194272cc70bSAndy Fleming struct mmc_cmd cmd; 195272cc70bSAndy Fleming struct mmc_data data; 196272cc70bSAndy Fleming 1974a1a06bcSAlagu Sankar if (blkcnt > 1) 1984a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; 1994a1a06bcSAlagu Sankar else 200272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; 201272cc70bSAndy Fleming 202272cc70bSAndy Fleming if (mmc->high_capacity) 2034a1a06bcSAlagu Sankar cmd.cmdarg = start; 204272cc70bSAndy Fleming else 2054a1a06bcSAlagu Sankar cmd.cmdarg = start * mmc->read_bl_len; 206272cc70bSAndy Fleming 207272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 208272cc70bSAndy Fleming 209272cc70bSAndy Fleming data.dest = dst; 2104a1a06bcSAlagu Sankar data.blocks = blkcnt; 211272cc70bSAndy Fleming data.blocksize = mmc->read_bl_len; 212272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 213272cc70bSAndy Fleming 2144a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, &data)) 2154a1a06bcSAlagu Sankar return 0; 2164a1a06bcSAlagu Sankar 2174a1a06bcSAlagu Sankar if (blkcnt > 1) { 2184a1a06bcSAlagu Sankar cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; 2194a1a06bcSAlagu Sankar cmd.cmdarg = 0; 2204a1a06bcSAlagu Sankar cmd.resp_type = MMC_RSP_R1b; 2214a1a06bcSAlagu Sankar if (mmc_send_cmd(mmc, &cmd, NULL)) { 22256196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 2234a1a06bcSAlagu Sankar printf("mmc fail to send stop cmd\n"); 22456196826SPaul Burton #endif 2254a1a06bcSAlagu Sankar return 0; 2264a1a06bcSAlagu Sankar } 227272cc70bSAndy Fleming } 228272cc70bSAndy Fleming 2294a1a06bcSAlagu Sankar return blkcnt; 230272cc70bSAndy Fleming } 231272cc70bSAndy Fleming 232ff8fef56SSascha Silbe static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst) 233272cc70bSAndy Fleming { 2344a1a06bcSAlagu Sankar lbaint_t cur, blocks_todo = blkcnt; 235272cc70bSAndy Fleming 2364a1a06bcSAlagu Sankar if (blkcnt == 0) 2374a1a06bcSAlagu Sankar return 0; 2384a1a06bcSAlagu Sankar 2394a1a06bcSAlagu Sankar struct mmc *mmc = find_mmc_device(dev_num); 240272cc70bSAndy Fleming if (!mmc) 241272cc70bSAndy Fleming return 0; 242272cc70bSAndy Fleming 243d2bf29e3SLei Wen if ((start + blkcnt) > mmc->block_dev.lba) { 24456196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 245ff8fef56SSascha Silbe printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n", 246d2bf29e3SLei Wen start + blkcnt, mmc->block_dev.lba); 24756196826SPaul Burton #endif 248d2bf29e3SLei Wen return 0; 249d2bf29e3SLei Wen } 250272cc70bSAndy Fleming 2514a1a06bcSAlagu Sankar if (mmc_set_blocklen(mmc, mmc->read_bl_len)) 252272cc70bSAndy Fleming return 0; 253272cc70bSAndy Fleming 2544a1a06bcSAlagu Sankar do { 25593bfd616SPantelis Antoniou cur = (blocks_todo > mmc->cfg->b_max) ? 25693bfd616SPantelis Antoniou mmc->cfg->b_max : blocks_todo; 2574a1a06bcSAlagu Sankar if(mmc_read_blocks(mmc, dst, start, cur) != cur) 2584a1a06bcSAlagu Sankar return 0; 2594a1a06bcSAlagu Sankar blocks_todo -= cur; 2604a1a06bcSAlagu Sankar start += cur; 2614a1a06bcSAlagu Sankar dst += cur * mmc->read_bl_len; 2624a1a06bcSAlagu Sankar } while (blocks_todo > 0); 263272cc70bSAndy Fleming 264272cc70bSAndy Fleming return blkcnt; 265272cc70bSAndy Fleming } 266272cc70bSAndy Fleming 267fdbb873eSKim Phillips static int mmc_go_idle(struct mmc *mmc) 268272cc70bSAndy Fleming { 269272cc70bSAndy Fleming struct mmc_cmd cmd; 270272cc70bSAndy Fleming int err; 271272cc70bSAndy Fleming 272272cc70bSAndy Fleming udelay(1000); 273272cc70bSAndy Fleming 274272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; 275272cc70bSAndy Fleming cmd.cmdarg = 0; 276272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_NONE; 277272cc70bSAndy Fleming 278272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 279272cc70bSAndy Fleming 280272cc70bSAndy Fleming if (err) 281272cc70bSAndy Fleming return err; 282272cc70bSAndy Fleming 283272cc70bSAndy Fleming udelay(2000); 284272cc70bSAndy Fleming 285272cc70bSAndy Fleming return 0; 286272cc70bSAndy Fleming } 287272cc70bSAndy Fleming 288fdbb873eSKim Phillips static int sd_send_op_cond(struct mmc *mmc) 289272cc70bSAndy Fleming { 290272cc70bSAndy Fleming int timeout = 1000; 291272cc70bSAndy Fleming int err; 292272cc70bSAndy Fleming struct mmc_cmd cmd; 293272cc70bSAndy Fleming 294272cc70bSAndy Fleming do { 295272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 296272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 297272cc70bSAndy Fleming cmd.cmdarg = 0; 298272cc70bSAndy Fleming 299272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 300272cc70bSAndy Fleming 301272cc70bSAndy Fleming if (err) 302272cc70bSAndy Fleming return err; 303272cc70bSAndy Fleming 304272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; 305272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R3; 306250de12bSStefano Babic 307250de12bSStefano Babic /* 308250de12bSStefano Babic * Most cards do not answer if some reserved bits 309250de12bSStefano Babic * in the ocr are set. However, Some controller 310250de12bSStefano Babic * can set bit 7 (reserved for low voltages), but 311250de12bSStefano Babic * how to manage low voltages SD card is not yet 312250de12bSStefano Babic * specified. 313250de12bSStefano Babic */ 314d52ebf10SThomas Chou cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 : 31593bfd616SPantelis Antoniou (mmc->cfg->voltages & 0xff8000); 316272cc70bSAndy Fleming 317272cc70bSAndy Fleming if (mmc->version == SD_VERSION_2) 318272cc70bSAndy Fleming cmd.cmdarg |= OCR_HCS; 319272cc70bSAndy Fleming 320272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 321272cc70bSAndy Fleming 322272cc70bSAndy Fleming if (err) 323272cc70bSAndy Fleming return err; 324272cc70bSAndy Fleming 325272cc70bSAndy Fleming udelay(1000); 326272cc70bSAndy Fleming } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--); 327272cc70bSAndy Fleming 328272cc70bSAndy Fleming if (timeout <= 0) 329272cc70bSAndy Fleming return UNUSABLE_ERR; 330272cc70bSAndy Fleming 331272cc70bSAndy Fleming if (mmc->version != SD_VERSION_2) 332272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 333272cc70bSAndy Fleming 334d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 335d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 336d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 337d52ebf10SThomas Chou cmd.cmdarg = 0; 338d52ebf10SThomas Chou 339d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 340d52ebf10SThomas Chou 341d52ebf10SThomas Chou if (err) 342d52ebf10SThomas Chou return err; 343d52ebf10SThomas Chou } 344d52ebf10SThomas Chou 345998be3ddSRabin Vincent mmc->ocr = cmd.response[0]; 346272cc70bSAndy Fleming 347272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 348272cc70bSAndy Fleming mmc->rca = 0; 349272cc70bSAndy Fleming 350272cc70bSAndy Fleming return 0; 351272cc70bSAndy Fleming } 352272cc70bSAndy Fleming 353e9550449SChe-Liang Chiou /* We pass in the cmd since otherwise the init seems to fail */ 354e9550449SChe-Liang Chiou static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd, 355e9550449SChe-Liang Chiou int use_arg) 356272cc70bSAndy Fleming { 357272cc70bSAndy Fleming int err; 358272cc70bSAndy Fleming 359e9550449SChe-Liang Chiou cmd->cmdidx = MMC_CMD_SEND_OP_COND; 360e9550449SChe-Liang Chiou cmd->resp_type = MMC_RSP_R3; 361e9550449SChe-Liang Chiou cmd->cmdarg = 0; 362e9550449SChe-Liang Chiou if (use_arg && !mmc_host_is_spi(mmc)) { 363e9550449SChe-Liang Chiou cmd->cmdarg = 36493bfd616SPantelis Antoniou (mmc->cfg->voltages & 365e9550449SChe-Liang Chiou (mmc->op_cond_response & OCR_VOLTAGE_MASK)) | 366e9550449SChe-Liang Chiou (mmc->op_cond_response & OCR_ACCESS_MODE); 367e9550449SChe-Liang Chiou 36893bfd616SPantelis Antoniou if (mmc->cfg->host_caps & MMC_MODE_HC) 369e9550449SChe-Liang Chiou cmd->cmdarg |= OCR_HCS; 370e9550449SChe-Liang Chiou } 371e9550449SChe-Liang Chiou err = mmc_send_cmd(mmc, cmd, NULL); 372e9550449SChe-Liang Chiou if (err) 373e9550449SChe-Liang Chiou return err; 374e9550449SChe-Liang Chiou mmc->op_cond_response = cmd->response[0]; 375e9550449SChe-Liang Chiou return 0; 376e9550449SChe-Liang Chiou } 377e9550449SChe-Liang Chiou 378750121c3SJeroen Hofstee static int mmc_send_op_cond(struct mmc *mmc) 379e9550449SChe-Liang Chiou { 380e9550449SChe-Liang Chiou struct mmc_cmd cmd; 381e9550449SChe-Liang Chiou int err, i; 382e9550449SChe-Liang Chiou 383272cc70bSAndy Fleming /* Some cards seem to need this */ 384272cc70bSAndy Fleming mmc_go_idle(mmc); 385272cc70bSAndy Fleming 38631cacbabSRaffaele Recalcati /* Asking to the card its capabilities */ 387e9550449SChe-Liang Chiou mmc->op_cond_pending = 1; 388e9550449SChe-Liang Chiou for (i = 0; i < 2; i++) { 389e9550449SChe-Liang Chiou err = mmc_send_op_cond_iter(mmc, &cmd, i != 0); 39031cacbabSRaffaele Recalcati if (err) 39131cacbabSRaffaele Recalcati return err; 39231cacbabSRaffaele Recalcati 393e9550449SChe-Liang Chiou /* exit if not busy (flag seems to be inverted) */ 394e9550449SChe-Liang Chiou if (mmc->op_cond_response & OCR_BUSY) 395e9550449SChe-Liang Chiou return 0; 396e9550449SChe-Liang Chiou } 397e9550449SChe-Liang Chiou return IN_PROGRESS; 398e9550449SChe-Liang Chiou } 39931cacbabSRaffaele Recalcati 400750121c3SJeroen Hofstee static int mmc_complete_op_cond(struct mmc *mmc) 401e9550449SChe-Liang Chiou { 402e9550449SChe-Liang Chiou struct mmc_cmd cmd; 403e9550449SChe-Liang Chiou int timeout = 1000; 404e9550449SChe-Liang Chiou uint start; 405e9550449SChe-Liang Chiou int err; 406e9550449SChe-Liang Chiou 407e9550449SChe-Liang Chiou mmc->op_cond_pending = 0; 408e9550449SChe-Liang Chiou start = get_timer(0); 409272cc70bSAndy Fleming do { 410e9550449SChe-Liang Chiou err = mmc_send_op_cond_iter(mmc, &cmd, 1); 411272cc70bSAndy Fleming if (err) 412272cc70bSAndy Fleming return err; 413e9550449SChe-Liang Chiou if (get_timer(start) > timeout) 414272cc70bSAndy Fleming return UNUSABLE_ERR; 415e9550449SChe-Liang Chiou udelay(100); 416e9550449SChe-Liang Chiou } while (!(mmc->op_cond_response & OCR_BUSY)); 417272cc70bSAndy Fleming 418d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* read OCR for spi */ 419d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_READ_OCR; 420d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R3; 421d52ebf10SThomas Chou cmd.cmdarg = 0; 422d52ebf10SThomas Chou 423d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 424d52ebf10SThomas Chou 425d52ebf10SThomas Chou if (err) 426d52ebf10SThomas Chou return err; 427d52ebf10SThomas Chou } 428d52ebf10SThomas Chou 429272cc70bSAndy Fleming mmc->version = MMC_VERSION_UNKNOWN; 430998be3ddSRabin Vincent mmc->ocr = cmd.response[0]; 431272cc70bSAndy Fleming 432272cc70bSAndy Fleming mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); 433def816a2SStephen Warren mmc->rca = 1; 434272cc70bSAndy Fleming 435272cc70bSAndy Fleming return 0; 436272cc70bSAndy Fleming } 437272cc70bSAndy Fleming 438272cc70bSAndy Fleming 439fdbb873eSKim Phillips static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd) 440272cc70bSAndy Fleming { 441272cc70bSAndy Fleming struct mmc_cmd cmd; 442272cc70bSAndy Fleming struct mmc_data data; 443272cc70bSAndy Fleming int err; 444272cc70bSAndy Fleming 445272cc70bSAndy Fleming /* Get the Card Status Register */ 446272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; 447272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 448272cc70bSAndy Fleming cmd.cmdarg = 0; 449272cc70bSAndy Fleming 450cdfd1ac6SYoshihiro Shimoda data.dest = (char *)ext_csd; 451272cc70bSAndy Fleming data.blocks = 1; 4528bfa195eSSimon Glass data.blocksize = MMC_MAX_BLOCK_LEN; 453272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 454272cc70bSAndy Fleming 455272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 456272cc70bSAndy Fleming 457272cc70bSAndy Fleming return err; 458272cc70bSAndy Fleming } 459272cc70bSAndy Fleming 460272cc70bSAndy Fleming 461fdbb873eSKim Phillips static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) 462272cc70bSAndy Fleming { 463272cc70bSAndy Fleming struct mmc_cmd cmd; 4645d4fc8d9SRaffaele Recalcati int timeout = 1000; 4655d4fc8d9SRaffaele Recalcati int ret; 466272cc70bSAndy Fleming 467272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SWITCH; 468272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1b; 469272cc70bSAndy Fleming cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 470272cc70bSAndy Fleming (index << 16) | 471272cc70bSAndy Fleming (value << 8); 472272cc70bSAndy Fleming 4735d4fc8d9SRaffaele Recalcati ret = mmc_send_cmd(mmc, &cmd, NULL); 4745d4fc8d9SRaffaele Recalcati 4755d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 47693ad0d18SJan Kloetzke if (!ret) 47793ad0d18SJan Kloetzke ret = mmc_send_status(mmc, timeout); 4785d4fc8d9SRaffaele Recalcati 4795d4fc8d9SRaffaele Recalcati return ret; 4805d4fc8d9SRaffaele Recalcati 481272cc70bSAndy Fleming } 482272cc70bSAndy Fleming 483fdbb873eSKim Phillips static int mmc_change_freq(struct mmc *mmc) 484272cc70bSAndy Fleming { 4858bfa195eSSimon Glass ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); 486272cc70bSAndy Fleming char cardtype; 487272cc70bSAndy Fleming int err; 488272cc70bSAndy Fleming 489786e8f81SAndrew Gabbasov mmc->card_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; 490272cc70bSAndy Fleming 491d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 492d52ebf10SThomas Chou return 0; 493d52ebf10SThomas Chou 494272cc70bSAndy Fleming /* Only version 4 supports high-speed */ 495272cc70bSAndy Fleming if (mmc->version < MMC_VERSION_4) 496272cc70bSAndy Fleming return 0; 497272cc70bSAndy Fleming 498272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 499272cc70bSAndy Fleming 500272cc70bSAndy Fleming if (err) 501272cc70bSAndy Fleming return err; 502272cc70bSAndy Fleming 5030560db18SLei Wen cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf; 504272cc70bSAndy Fleming 505272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); 506272cc70bSAndy Fleming 507272cc70bSAndy Fleming if (err) 5086b2221b0SAndrew Gabbasov return err == SWITCH_ERR ? 0 : err; 509272cc70bSAndy Fleming 510272cc70bSAndy Fleming /* Now check to see that it worked */ 511272cc70bSAndy Fleming err = mmc_send_ext_csd(mmc, ext_csd); 512272cc70bSAndy Fleming 513272cc70bSAndy Fleming if (err) 514272cc70bSAndy Fleming return err; 515272cc70bSAndy Fleming 516272cc70bSAndy Fleming /* No high-speed support */ 5170560db18SLei Wen if (!ext_csd[EXT_CSD_HS_TIMING]) 518272cc70bSAndy Fleming return 0; 519272cc70bSAndy Fleming 520272cc70bSAndy Fleming /* High Speed is set, there are two types: 52MHz and 26MHz */ 521d22e3d46SJaehoon Chung if (cardtype & EXT_CSD_CARD_TYPE_52) { 522201d5ac4SAndrew Gabbasov if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V) 523d22e3d46SJaehoon Chung mmc->card_caps |= MMC_MODE_DDR_52MHz; 524272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; 525d22e3d46SJaehoon Chung } else { 526272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 527d22e3d46SJaehoon Chung } 528272cc70bSAndy Fleming 529272cc70bSAndy Fleming return 0; 530272cc70bSAndy Fleming } 531272cc70bSAndy Fleming 532f866a46dSStephen Warren static int mmc_set_capacity(struct mmc *mmc, int part_num) 533f866a46dSStephen Warren { 534f866a46dSStephen Warren switch (part_num) { 535f866a46dSStephen Warren case 0: 536f866a46dSStephen Warren mmc->capacity = mmc->capacity_user; 537f866a46dSStephen Warren break; 538f866a46dSStephen Warren case 1: 539f866a46dSStephen Warren case 2: 540f866a46dSStephen Warren mmc->capacity = mmc->capacity_boot; 541f866a46dSStephen Warren break; 542f866a46dSStephen Warren case 3: 543f866a46dSStephen Warren mmc->capacity = mmc->capacity_rpmb; 544f866a46dSStephen Warren break; 545f866a46dSStephen Warren case 4: 546f866a46dSStephen Warren case 5: 547f866a46dSStephen Warren case 6: 548f866a46dSStephen Warren case 7: 549f866a46dSStephen Warren mmc->capacity = mmc->capacity_gp[part_num - 4]; 550f866a46dSStephen Warren break; 551f866a46dSStephen Warren default: 552f866a46dSStephen Warren return -1; 553f866a46dSStephen Warren } 554f866a46dSStephen Warren 555f866a46dSStephen Warren mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); 556f866a46dSStephen Warren 557f866a46dSStephen Warren return 0; 558f866a46dSStephen Warren } 559f866a46dSStephen Warren 560d2356284SStephen Warren int mmc_select_hwpart(int dev_num, int hwpart) 561d2356284SStephen Warren { 562d2356284SStephen Warren struct mmc *mmc = find_mmc_device(dev_num); 563d2356284SStephen Warren int ret; 564d2356284SStephen Warren 565d2356284SStephen Warren if (!mmc) 566d4622df3SStephen Warren return -ENODEV; 567d2356284SStephen Warren 568d2356284SStephen Warren if (mmc->part_num == hwpart) 569d2356284SStephen Warren return 0; 570d2356284SStephen Warren 571d2356284SStephen Warren if (mmc->part_config == MMCPART_NOAVAILABLE) { 572d2356284SStephen Warren printf("Card doesn't support part_switch\n"); 573d4622df3SStephen Warren return -EMEDIUMTYPE; 574d2356284SStephen Warren } 575d2356284SStephen Warren 576d2356284SStephen Warren ret = mmc_switch_part(dev_num, hwpart); 577d2356284SStephen Warren if (ret) 578d4622df3SStephen Warren return ret; 579d2356284SStephen Warren 580d2356284SStephen Warren mmc->part_num = hwpart; 581d2356284SStephen Warren 582d2356284SStephen Warren return 0; 583d2356284SStephen Warren } 584d2356284SStephen Warren 585d2356284SStephen Warren 586bc897b1dSLei Wen int mmc_switch_part(int dev_num, unsigned int part_num) 587bc897b1dSLei Wen { 588bc897b1dSLei Wen struct mmc *mmc = find_mmc_device(dev_num); 589f866a46dSStephen Warren int ret; 590bc897b1dSLei Wen 591bc897b1dSLei Wen if (!mmc) 592bc897b1dSLei Wen return -1; 593bc897b1dSLei Wen 594f866a46dSStephen Warren ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, 595bc897b1dSLei Wen (mmc->part_config & ~PART_ACCESS_MASK) 596bc897b1dSLei Wen | (part_num & PART_ACCESS_MASK)); 597f866a46dSStephen Warren 5986dc93e70SPeter Bigot /* 5996dc93e70SPeter Bigot * Set the capacity if the switch succeeded or was intended 6006dc93e70SPeter Bigot * to return to representing the raw device. 6016dc93e70SPeter Bigot */ 6026dc93e70SPeter Bigot if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) 6036dc93e70SPeter Bigot ret = mmc_set_capacity(mmc, part_num); 6046dc93e70SPeter Bigot 6056dc93e70SPeter Bigot return ret; 606bc897b1dSLei Wen } 607bc897b1dSLei Wen 608ac9da0e0SDiego Santa Cruz int mmc_hwpart_config(struct mmc *mmc, 609ac9da0e0SDiego Santa Cruz const struct mmc_hwpart_conf *conf, 610ac9da0e0SDiego Santa Cruz enum mmc_hwpart_conf_mode mode) 611ac9da0e0SDiego Santa Cruz { 612ac9da0e0SDiego Santa Cruz u8 part_attrs = 0; 613ac9da0e0SDiego Santa Cruz u32 enh_size_mult; 614ac9da0e0SDiego Santa Cruz u32 enh_start_addr; 615ac9da0e0SDiego Santa Cruz u32 gp_size_mult[4]; 616ac9da0e0SDiego Santa Cruz u32 max_enh_size_mult; 617ac9da0e0SDiego Santa Cruz u32 tot_enh_size_mult = 0; 618*8dda5b0eSDiego Santa Cruz u8 wr_rel_set; 619ac9da0e0SDiego Santa Cruz int i, pidx, err; 620ac9da0e0SDiego Santa Cruz ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); 621ac9da0e0SDiego Santa Cruz 622ac9da0e0SDiego Santa Cruz if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE) 623ac9da0e0SDiego Santa Cruz return -EINVAL; 624ac9da0e0SDiego Santa Cruz 625ac9da0e0SDiego Santa Cruz if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) { 626ac9da0e0SDiego Santa Cruz printf("eMMC >= 4.4 required for enhanced user data area\n"); 627ac9da0e0SDiego Santa Cruz return -EMEDIUMTYPE; 628ac9da0e0SDiego Santa Cruz } 629ac9da0e0SDiego Santa Cruz 630ac9da0e0SDiego Santa Cruz if (!(mmc->part_support & PART_SUPPORT)) { 631ac9da0e0SDiego Santa Cruz printf("Card does not support partitioning\n"); 632ac9da0e0SDiego Santa Cruz return -EMEDIUMTYPE; 633ac9da0e0SDiego Santa Cruz } 634ac9da0e0SDiego Santa Cruz 635ac9da0e0SDiego Santa Cruz if (!mmc->hc_wp_grp_size) { 636ac9da0e0SDiego Santa Cruz printf("Card does not define HC WP group size\n"); 637ac9da0e0SDiego Santa Cruz return -EMEDIUMTYPE; 638ac9da0e0SDiego Santa Cruz } 639ac9da0e0SDiego Santa Cruz 640ac9da0e0SDiego Santa Cruz /* check partition alignment and total enhanced size */ 641ac9da0e0SDiego Santa Cruz if (conf->user.enh_size) { 642ac9da0e0SDiego Santa Cruz if (conf->user.enh_size % mmc->hc_wp_grp_size || 643ac9da0e0SDiego Santa Cruz conf->user.enh_start % mmc->hc_wp_grp_size) { 644ac9da0e0SDiego Santa Cruz printf("User data enhanced area not HC WP group " 645ac9da0e0SDiego Santa Cruz "size aligned\n"); 646ac9da0e0SDiego Santa Cruz return -EINVAL; 647ac9da0e0SDiego Santa Cruz } 648ac9da0e0SDiego Santa Cruz part_attrs |= EXT_CSD_ENH_USR; 649ac9da0e0SDiego Santa Cruz enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size; 650ac9da0e0SDiego Santa Cruz if (mmc->high_capacity) { 651ac9da0e0SDiego Santa Cruz enh_start_addr = conf->user.enh_start; 652ac9da0e0SDiego Santa Cruz } else { 653ac9da0e0SDiego Santa Cruz enh_start_addr = (conf->user.enh_start << 9); 654ac9da0e0SDiego Santa Cruz } 655ac9da0e0SDiego Santa Cruz } else { 656ac9da0e0SDiego Santa Cruz enh_size_mult = 0; 657ac9da0e0SDiego Santa Cruz enh_start_addr = 0; 658ac9da0e0SDiego Santa Cruz } 659ac9da0e0SDiego Santa Cruz tot_enh_size_mult += enh_size_mult; 660ac9da0e0SDiego Santa Cruz 661ac9da0e0SDiego Santa Cruz for (pidx = 0; pidx < 4; pidx++) { 662ac9da0e0SDiego Santa Cruz if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) { 663ac9da0e0SDiego Santa Cruz printf("GP%i partition not HC WP group size " 664ac9da0e0SDiego Santa Cruz "aligned\n", pidx+1); 665ac9da0e0SDiego Santa Cruz return -EINVAL; 666ac9da0e0SDiego Santa Cruz } 667ac9da0e0SDiego Santa Cruz gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size; 668ac9da0e0SDiego Santa Cruz if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) { 669ac9da0e0SDiego Santa Cruz part_attrs |= EXT_CSD_ENH_GP(pidx); 670ac9da0e0SDiego Santa Cruz tot_enh_size_mult += gp_size_mult[pidx]; 671ac9da0e0SDiego Santa Cruz } 672ac9da0e0SDiego Santa Cruz } 673ac9da0e0SDiego Santa Cruz 674ac9da0e0SDiego Santa Cruz if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) { 675ac9da0e0SDiego Santa Cruz printf("Card does not support enhanced attribute\n"); 676ac9da0e0SDiego Santa Cruz return -EMEDIUMTYPE; 677ac9da0e0SDiego Santa Cruz } 678ac9da0e0SDiego Santa Cruz 679ac9da0e0SDiego Santa Cruz err = mmc_send_ext_csd(mmc, ext_csd); 680ac9da0e0SDiego Santa Cruz if (err) 681ac9da0e0SDiego Santa Cruz return err; 682ac9da0e0SDiego Santa Cruz 683ac9da0e0SDiego Santa Cruz max_enh_size_mult = 684ac9da0e0SDiego Santa Cruz (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) + 685ac9da0e0SDiego Santa Cruz (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) + 686ac9da0e0SDiego Santa Cruz ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]; 687ac9da0e0SDiego Santa Cruz if (tot_enh_size_mult > max_enh_size_mult) { 688ac9da0e0SDiego Santa Cruz printf("Total enhanced size exceeds maximum (%u > %u)\n", 689ac9da0e0SDiego Santa Cruz tot_enh_size_mult, max_enh_size_mult); 690ac9da0e0SDiego Santa Cruz return -EMEDIUMTYPE; 691ac9da0e0SDiego Santa Cruz } 692ac9da0e0SDiego Santa Cruz 693*8dda5b0eSDiego Santa Cruz /* The default value of EXT_CSD_WR_REL_SET is device 694*8dda5b0eSDiego Santa Cruz * dependent, the values can only be changed if the 695*8dda5b0eSDiego Santa Cruz * EXT_CSD_HS_CTRL_REL bit is set. The values can be 696*8dda5b0eSDiego Santa Cruz * changed only once and before partitioning is completed. */ 697*8dda5b0eSDiego Santa Cruz wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET]; 698*8dda5b0eSDiego Santa Cruz if (conf->user.wr_rel_change) { 699*8dda5b0eSDiego Santa Cruz if (conf->user.wr_rel_set) 700*8dda5b0eSDiego Santa Cruz wr_rel_set |= EXT_CSD_WR_DATA_REL_USR; 701*8dda5b0eSDiego Santa Cruz else 702*8dda5b0eSDiego Santa Cruz wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR; 703*8dda5b0eSDiego Santa Cruz } 704*8dda5b0eSDiego Santa Cruz for (pidx = 0; pidx < 4; pidx++) { 705*8dda5b0eSDiego Santa Cruz if (conf->gp_part[pidx].wr_rel_change) { 706*8dda5b0eSDiego Santa Cruz if (conf->gp_part[pidx].wr_rel_set) 707*8dda5b0eSDiego Santa Cruz wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx); 708*8dda5b0eSDiego Santa Cruz else 709*8dda5b0eSDiego Santa Cruz wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx); 710*8dda5b0eSDiego Santa Cruz } 711*8dda5b0eSDiego Santa Cruz } 712*8dda5b0eSDiego Santa Cruz 713*8dda5b0eSDiego Santa Cruz if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] && 714*8dda5b0eSDiego Santa Cruz !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) { 715*8dda5b0eSDiego Santa Cruz puts("Card does not support host controlled partition write " 716*8dda5b0eSDiego Santa Cruz "reliability settings\n"); 717*8dda5b0eSDiego Santa Cruz return -EMEDIUMTYPE; 718*8dda5b0eSDiego Santa Cruz } 719*8dda5b0eSDiego Santa Cruz 720ac9da0e0SDiego Santa Cruz if (ext_csd[EXT_CSD_PARTITION_SETTING] & 721ac9da0e0SDiego Santa Cruz EXT_CSD_PARTITION_SETTING_COMPLETED) { 722ac9da0e0SDiego Santa Cruz printf("Card already partitioned\n"); 723ac9da0e0SDiego Santa Cruz return -EPERM; 724ac9da0e0SDiego Santa Cruz } 725ac9da0e0SDiego Santa Cruz 726ac9da0e0SDiego Santa Cruz if (mode == MMC_HWPART_CONF_CHECK) 727ac9da0e0SDiego Santa Cruz return 0; 728ac9da0e0SDiego Santa Cruz 729ac9da0e0SDiego Santa Cruz /* Partitioning requires high-capacity size definitions */ 730ac9da0e0SDiego Santa Cruz if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) { 731ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 732ac9da0e0SDiego Santa Cruz EXT_CSD_ERASE_GROUP_DEF, 1); 733ac9da0e0SDiego Santa Cruz 734ac9da0e0SDiego Santa Cruz if (err) 735ac9da0e0SDiego Santa Cruz return err; 736ac9da0e0SDiego Santa Cruz 737ac9da0e0SDiego Santa Cruz ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1; 738ac9da0e0SDiego Santa Cruz 739ac9da0e0SDiego Santa Cruz /* update erase group size to be high-capacity */ 740ac9da0e0SDiego Santa Cruz mmc->erase_grp_size = 741ac9da0e0SDiego Santa Cruz ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024; 742ac9da0e0SDiego Santa Cruz 743ac9da0e0SDiego Santa Cruz } 744ac9da0e0SDiego Santa Cruz 745ac9da0e0SDiego Santa Cruz /* all OK, write the configuration */ 746ac9da0e0SDiego Santa Cruz for (i = 0; i < 4; i++) { 747ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 748ac9da0e0SDiego Santa Cruz EXT_CSD_ENH_START_ADDR+i, 749ac9da0e0SDiego Santa Cruz (enh_start_addr >> (i*8)) & 0xFF); 750ac9da0e0SDiego Santa Cruz if (err) 751ac9da0e0SDiego Santa Cruz return err; 752ac9da0e0SDiego Santa Cruz } 753ac9da0e0SDiego Santa Cruz for (i = 0; i < 3; i++) { 754ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 755ac9da0e0SDiego Santa Cruz EXT_CSD_ENH_SIZE_MULT+i, 756ac9da0e0SDiego Santa Cruz (enh_size_mult >> (i*8)) & 0xFF); 757ac9da0e0SDiego Santa Cruz if (err) 758ac9da0e0SDiego Santa Cruz return err; 759ac9da0e0SDiego Santa Cruz } 760ac9da0e0SDiego Santa Cruz for (pidx = 0; pidx < 4; pidx++) { 761ac9da0e0SDiego Santa Cruz for (i = 0; i < 3; i++) { 762ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 763ac9da0e0SDiego Santa Cruz EXT_CSD_GP_SIZE_MULT+pidx*3+i, 764ac9da0e0SDiego Santa Cruz (gp_size_mult[pidx] >> (i*8)) & 0xFF); 765ac9da0e0SDiego Santa Cruz if (err) 766ac9da0e0SDiego Santa Cruz return err; 767ac9da0e0SDiego Santa Cruz } 768ac9da0e0SDiego Santa Cruz } 769ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 770ac9da0e0SDiego Santa Cruz EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs); 771ac9da0e0SDiego Santa Cruz if (err) 772ac9da0e0SDiego Santa Cruz return err; 773ac9da0e0SDiego Santa Cruz 774ac9da0e0SDiego Santa Cruz if (mode == MMC_HWPART_CONF_SET) 775ac9da0e0SDiego Santa Cruz return 0; 776ac9da0e0SDiego Santa Cruz 777*8dda5b0eSDiego Santa Cruz /* The WR_REL_SET is a write-once register but shall be 778*8dda5b0eSDiego Santa Cruz * written before setting PART_SETTING_COMPLETED. As it is 779*8dda5b0eSDiego Santa Cruz * write-once we can only write it when completing the 780*8dda5b0eSDiego Santa Cruz * partitioning. */ 781*8dda5b0eSDiego Santa Cruz if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) { 782*8dda5b0eSDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 783*8dda5b0eSDiego Santa Cruz EXT_CSD_WR_REL_SET, wr_rel_set); 784*8dda5b0eSDiego Santa Cruz if (err) 785*8dda5b0eSDiego Santa Cruz return err; 786*8dda5b0eSDiego Santa Cruz } 787*8dda5b0eSDiego Santa Cruz 788ac9da0e0SDiego Santa Cruz /* Setting PART_SETTING_COMPLETED confirms the partition 789ac9da0e0SDiego Santa Cruz * configuration but it only becomes effective after power 790ac9da0e0SDiego Santa Cruz * cycle, so we do not adjust the partition related settings 791ac9da0e0SDiego Santa Cruz * in the mmc struct. */ 792ac9da0e0SDiego Santa Cruz 793ac9da0e0SDiego Santa Cruz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 794ac9da0e0SDiego Santa Cruz EXT_CSD_PARTITION_SETTING, 795ac9da0e0SDiego Santa Cruz EXT_CSD_PARTITION_SETTING_COMPLETED); 796ac9da0e0SDiego Santa Cruz if (err) 797ac9da0e0SDiego Santa Cruz return err; 798ac9da0e0SDiego Santa Cruz 799ac9da0e0SDiego Santa Cruz return 0; 800ac9da0e0SDiego Santa Cruz } 801ac9da0e0SDiego Santa Cruz 80248972d90SThierry Reding int mmc_getcd(struct mmc *mmc) 80348972d90SThierry Reding { 80448972d90SThierry Reding int cd; 80548972d90SThierry Reding 80648972d90SThierry Reding cd = board_mmc_getcd(mmc); 80748972d90SThierry Reding 808d4e1da4eSPeter Korsgaard if (cd < 0) { 80993bfd616SPantelis Antoniou if (mmc->cfg->ops->getcd) 81093bfd616SPantelis Antoniou cd = mmc->cfg->ops->getcd(mmc); 811d4e1da4eSPeter Korsgaard else 812d4e1da4eSPeter Korsgaard cd = 1; 813d4e1da4eSPeter Korsgaard } 81448972d90SThierry Reding 81548972d90SThierry Reding return cd; 81648972d90SThierry Reding } 81748972d90SThierry Reding 818fdbb873eSKim Phillips static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) 819272cc70bSAndy Fleming { 820272cc70bSAndy Fleming struct mmc_cmd cmd; 821272cc70bSAndy Fleming struct mmc_data data; 822272cc70bSAndy Fleming 823272cc70bSAndy Fleming /* Switch the frequency */ 824272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SWITCH_FUNC; 825272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 826272cc70bSAndy Fleming cmd.cmdarg = (mode << 31) | 0xffffff; 827272cc70bSAndy Fleming cmd.cmdarg &= ~(0xf << (group * 4)); 828272cc70bSAndy Fleming cmd.cmdarg |= value << (group * 4); 829272cc70bSAndy Fleming 830272cc70bSAndy Fleming data.dest = (char *)resp; 831272cc70bSAndy Fleming data.blocksize = 64; 832272cc70bSAndy Fleming data.blocks = 1; 833272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 834272cc70bSAndy Fleming 835272cc70bSAndy Fleming return mmc_send_cmd(mmc, &cmd, &data); 836272cc70bSAndy Fleming } 837272cc70bSAndy Fleming 838272cc70bSAndy Fleming 839fdbb873eSKim Phillips static int sd_change_freq(struct mmc *mmc) 840272cc70bSAndy Fleming { 841272cc70bSAndy Fleming int err; 842272cc70bSAndy Fleming struct mmc_cmd cmd; 843f781dd38SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2); 844f781dd38SAnton staaf ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16); 845272cc70bSAndy Fleming struct mmc_data data; 846272cc70bSAndy Fleming int timeout; 847272cc70bSAndy Fleming 848272cc70bSAndy Fleming mmc->card_caps = 0; 849272cc70bSAndy Fleming 850d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) 851d52ebf10SThomas Chou return 0; 852d52ebf10SThomas Chou 853272cc70bSAndy Fleming /* Read the SCR to find out if this card supports higher speeds */ 854272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 855272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 856272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 857272cc70bSAndy Fleming 858272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 859272cc70bSAndy Fleming 860272cc70bSAndy Fleming if (err) 861272cc70bSAndy Fleming return err; 862272cc70bSAndy Fleming 863272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SEND_SCR; 864272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 865272cc70bSAndy Fleming cmd.cmdarg = 0; 866272cc70bSAndy Fleming 867272cc70bSAndy Fleming timeout = 3; 868272cc70bSAndy Fleming 869272cc70bSAndy Fleming retry_scr: 870f781dd38SAnton staaf data.dest = (char *)scr; 871272cc70bSAndy Fleming data.blocksize = 8; 872272cc70bSAndy Fleming data.blocks = 1; 873272cc70bSAndy Fleming data.flags = MMC_DATA_READ; 874272cc70bSAndy Fleming 875272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, &data); 876272cc70bSAndy Fleming 877272cc70bSAndy Fleming if (err) { 878272cc70bSAndy Fleming if (timeout--) 879272cc70bSAndy Fleming goto retry_scr; 880272cc70bSAndy Fleming 881272cc70bSAndy Fleming return err; 882272cc70bSAndy Fleming } 883272cc70bSAndy Fleming 8844e3d89baSYauhen Kharuzhy mmc->scr[0] = __be32_to_cpu(scr[0]); 8854e3d89baSYauhen Kharuzhy mmc->scr[1] = __be32_to_cpu(scr[1]); 886272cc70bSAndy Fleming 887272cc70bSAndy Fleming switch ((mmc->scr[0] >> 24) & 0xf) { 888272cc70bSAndy Fleming case 0: 889272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 890272cc70bSAndy Fleming break; 891272cc70bSAndy Fleming case 1: 892272cc70bSAndy Fleming mmc->version = SD_VERSION_1_10; 893272cc70bSAndy Fleming break; 894272cc70bSAndy Fleming case 2: 895272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 8961741c64dSJaehoon Chung if ((mmc->scr[0] >> 15) & 0x1) 8971741c64dSJaehoon Chung mmc->version = SD_VERSION_3; 898272cc70bSAndy Fleming break; 899272cc70bSAndy Fleming default: 900272cc70bSAndy Fleming mmc->version = SD_VERSION_1_0; 901272cc70bSAndy Fleming break; 902272cc70bSAndy Fleming } 903272cc70bSAndy Fleming 904b44c7083SAlagu Sankar if (mmc->scr[0] & SD_DATA_4BIT) 905b44c7083SAlagu Sankar mmc->card_caps |= MMC_MODE_4BIT; 906b44c7083SAlagu Sankar 907272cc70bSAndy Fleming /* Version 1.0 doesn't support switching */ 908272cc70bSAndy Fleming if (mmc->version == SD_VERSION_1_0) 909272cc70bSAndy Fleming return 0; 910272cc70bSAndy Fleming 911272cc70bSAndy Fleming timeout = 4; 912272cc70bSAndy Fleming while (timeout--) { 913272cc70bSAndy Fleming err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, 914f781dd38SAnton staaf (u8 *)switch_status); 915272cc70bSAndy Fleming 916272cc70bSAndy Fleming if (err) 917272cc70bSAndy Fleming return err; 918272cc70bSAndy Fleming 919272cc70bSAndy Fleming /* The high-speed function is busy. Try again */ 9204e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) 921272cc70bSAndy Fleming break; 922272cc70bSAndy Fleming } 923272cc70bSAndy Fleming 924272cc70bSAndy Fleming /* If high-speed isn't supported, we return */ 9254e3d89baSYauhen Kharuzhy if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) 926272cc70bSAndy Fleming return 0; 927272cc70bSAndy Fleming 9282c3fbf4cSMacpaul Lin /* 9292c3fbf4cSMacpaul Lin * If the host doesn't support SD_HIGHSPEED, do not switch card to 9302c3fbf4cSMacpaul Lin * HIGHSPEED mode even if the card support SD_HIGHSPPED. 9312c3fbf4cSMacpaul Lin * This can avoid furthur problem when the card runs in different 9322c3fbf4cSMacpaul Lin * mode between the host. 9332c3fbf4cSMacpaul Lin */ 93493bfd616SPantelis Antoniou if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) && 93593bfd616SPantelis Antoniou (mmc->cfg->host_caps & MMC_MODE_HS))) 9362c3fbf4cSMacpaul Lin return 0; 9372c3fbf4cSMacpaul Lin 938f781dd38SAnton staaf err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status); 939272cc70bSAndy Fleming 940272cc70bSAndy Fleming if (err) 941272cc70bSAndy Fleming return err; 942272cc70bSAndy Fleming 9434e3d89baSYauhen Kharuzhy if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) 944272cc70bSAndy Fleming mmc->card_caps |= MMC_MODE_HS; 945272cc70bSAndy Fleming 946272cc70bSAndy Fleming return 0; 947272cc70bSAndy Fleming } 948272cc70bSAndy Fleming 949272cc70bSAndy Fleming /* frequency bases */ 950272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */ 9515f837c2cSMike Frysinger static const int fbase[] = { 952272cc70bSAndy Fleming 10000, 953272cc70bSAndy Fleming 100000, 954272cc70bSAndy Fleming 1000000, 955272cc70bSAndy Fleming 10000000, 956272cc70bSAndy Fleming }; 957272cc70bSAndy Fleming 958272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice 959272cc70bSAndy Fleming * to platforms without floating point. 960272cc70bSAndy Fleming */ 9615f837c2cSMike Frysinger static const int multipliers[] = { 962272cc70bSAndy Fleming 0, /* reserved */ 963272cc70bSAndy Fleming 10, 964272cc70bSAndy Fleming 12, 965272cc70bSAndy Fleming 13, 966272cc70bSAndy Fleming 15, 967272cc70bSAndy Fleming 20, 968272cc70bSAndy Fleming 25, 969272cc70bSAndy Fleming 30, 970272cc70bSAndy Fleming 35, 971272cc70bSAndy Fleming 40, 972272cc70bSAndy Fleming 45, 973272cc70bSAndy Fleming 50, 974272cc70bSAndy Fleming 55, 975272cc70bSAndy Fleming 60, 976272cc70bSAndy Fleming 70, 977272cc70bSAndy Fleming 80, 978272cc70bSAndy Fleming }; 979272cc70bSAndy Fleming 980fdbb873eSKim Phillips static void mmc_set_ios(struct mmc *mmc) 981272cc70bSAndy Fleming { 98293bfd616SPantelis Antoniou if (mmc->cfg->ops->set_ios) 98393bfd616SPantelis Antoniou mmc->cfg->ops->set_ios(mmc); 984272cc70bSAndy Fleming } 985272cc70bSAndy Fleming 986272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock) 987272cc70bSAndy Fleming { 98893bfd616SPantelis Antoniou if (clock > mmc->cfg->f_max) 98993bfd616SPantelis Antoniou clock = mmc->cfg->f_max; 990272cc70bSAndy Fleming 99193bfd616SPantelis Antoniou if (clock < mmc->cfg->f_min) 99293bfd616SPantelis Antoniou clock = mmc->cfg->f_min; 993272cc70bSAndy Fleming 994272cc70bSAndy Fleming mmc->clock = clock; 995272cc70bSAndy Fleming 996272cc70bSAndy Fleming mmc_set_ios(mmc); 997272cc70bSAndy Fleming } 998272cc70bSAndy Fleming 999fdbb873eSKim Phillips static void mmc_set_bus_width(struct mmc *mmc, uint width) 1000272cc70bSAndy Fleming { 1001272cc70bSAndy Fleming mmc->bus_width = width; 1002272cc70bSAndy Fleming 1003272cc70bSAndy Fleming mmc_set_ios(mmc); 1004272cc70bSAndy Fleming } 1005272cc70bSAndy Fleming 1006fdbb873eSKim Phillips static int mmc_startup(struct mmc *mmc) 1007272cc70bSAndy Fleming { 1008f866a46dSStephen Warren int err, i; 1009272cc70bSAndy Fleming uint mult, freq; 1010639b7827SYoshihiro Shimoda u64 cmult, csize, capacity; 1011272cc70bSAndy Fleming struct mmc_cmd cmd; 10128bfa195eSSimon Glass ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN); 10138bfa195eSSimon Glass ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN); 10145d4fc8d9SRaffaele Recalcati int timeout = 1000; 10150c453bb7SDiego Santa Cruz bool has_parts = false; 10168a0cf490SDiego Santa Cruz bool part_completed; 1017272cc70bSAndy Fleming 1018d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON 1019d52ebf10SThomas Chou if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */ 1020d52ebf10SThomas Chou cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF; 1021d52ebf10SThomas Chou cmd.resp_type = MMC_RSP_R1; 1022d52ebf10SThomas Chou cmd.cmdarg = 1; 1023d52ebf10SThomas Chou err = mmc_send_cmd(mmc, &cmd, NULL); 1024d52ebf10SThomas Chou 1025d52ebf10SThomas Chou if (err) 1026d52ebf10SThomas Chou return err; 1027d52ebf10SThomas Chou } 1028d52ebf10SThomas Chou #endif 1029d52ebf10SThomas Chou 1030272cc70bSAndy Fleming /* Put the Card in Identify Mode */ 1031d52ebf10SThomas Chou cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID : 1032d52ebf10SThomas Chou MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */ 1033272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 1034272cc70bSAndy Fleming cmd.cmdarg = 0; 1035272cc70bSAndy Fleming 1036272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1037272cc70bSAndy Fleming 1038272cc70bSAndy Fleming if (err) 1039272cc70bSAndy Fleming return err; 1040272cc70bSAndy Fleming 1041272cc70bSAndy Fleming memcpy(mmc->cid, cmd.response, 16); 1042272cc70bSAndy Fleming 1043272cc70bSAndy Fleming /* 1044272cc70bSAndy Fleming * For MMC cards, set the Relative Address. 1045272cc70bSAndy Fleming * For SD cards, get the Relatvie Address. 1046272cc70bSAndy Fleming * This also puts the cards into Standby State 1047272cc70bSAndy Fleming */ 1048d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 1049272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; 1050272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1051272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R6; 1052272cc70bSAndy Fleming 1053272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1054272cc70bSAndy Fleming 1055272cc70bSAndy Fleming if (err) 1056272cc70bSAndy Fleming return err; 1057272cc70bSAndy Fleming 1058272cc70bSAndy Fleming if (IS_SD(mmc)) 1059998be3ddSRabin Vincent mmc->rca = (cmd.response[0] >> 16) & 0xffff; 1060d52ebf10SThomas Chou } 1061272cc70bSAndy Fleming 1062272cc70bSAndy Fleming /* Get the Card-Specific Data */ 1063272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SEND_CSD; 1064272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R2; 1065272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1066272cc70bSAndy Fleming 1067272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1068272cc70bSAndy Fleming 10695d4fc8d9SRaffaele Recalcati /* Waiting for the ready status */ 10705d4fc8d9SRaffaele Recalcati mmc_send_status(mmc, timeout); 10715d4fc8d9SRaffaele Recalcati 1072272cc70bSAndy Fleming if (err) 1073272cc70bSAndy Fleming return err; 1074272cc70bSAndy Fleming 1075998be3ddSRabin Vincent mmc->csd[0] = cmd.response[0]; 1076998be3ddSRabin Vincent mmc->csd[1] = cmd.response[1]; 1077998be3ddSRabin Vincent mmc->csd[2] = cmd.response[2]; 1078998be3ddSRabin Vincent mmc->csd[3] = cmd.response[3]; 1079272cc70bSAndy Fleming 1080272cc70bSAndy Fleming if (mmc->version == MMC_VERSION_UNKNOWN) { 10810b453ffeSRabin Vincent int version = (cmd.response[0] >> 26) & 0xf; 1082272cc70bSAndy Fleming 1083272cc70bSAndy Fleming switch (version) { 1084272cc70bSAndy Fleming case 0: 1085272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 1086272cc70bSAndy Fleming break; 1087272cc70bSAndy Fleming case 1: 1088272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_4; 1089272cc70bSAndy Fleming break; 1090272cc70bSAndy Fleming case 2: 1091272cc70bSAndy Fleming mmc->version = MMC_VERSION_2_2; 1092272cc70bSAndy Fleming break; 1093272cc70bSAndy Fleming case 3: 1094272cc70bSAndy Fleming mmc->version = MMC_VERSION_3; 1095272cc70bSAndy Fleming break; 1096272cc70bSAndy Fleming case 4: 1097272cc70bSAndy Fleming mmc->version = MMC_VERSION_4; 1098272cc70bSAndy Fleming break; 1099272cc70bSAndy Fleming default: 1100272cc70bSAndy Fleming mmc->version = MMC_VERSION_1_2; 1101272cc70bSAndy Fleming break; 1102272cc70bSAndy Fleming } 1103272cc70bSAndy Fleming } 1104272cc70bSAndy Fleming 1105272cc70bSAndy Fleming /* divide frequency by 10, since the mults are 10x bigger */ 11060b453ffeSRabin Vincent freq = fbase[(cmd.response[0] & 0x7)]; 11070b453ffeSRabin Vincent mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; 1108272cc70bSAndy Fleming 1109272cc70bSAndy Fleming mmc->tran_speed = freq * mult; 1110272cc70bSAndy Fleming 1111ab71188cSMarkus Niebel mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1); 1112998be3ddSRabin Vincent mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); 1113272cc70bSAndy Fleming 1114272cc70bSAndy Fleming if (IS_SD(mmc)) 1115272cc70bSAndy Fleming mmc->write_bl_len = mmc->read_bl_len; 1116272cc70bSAndy Fleming else 1117998be3ddSRabin Vincent mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); 1118272cc70bSAndy Fleming 1119272cc70bSAndy Fleming if (mmc->high_capacity) { 1120272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3f) << 16 1121272cc70bSAndy Fleming | (mmc->csd[2] & 0xffff0000) >> 16; 1122272cc70bSAndy Fleming cmult = 8; 1123272cc70bSAndy Fleming } else { 1124272cc70bSAndy Fleming csize = (mmc->csd[1] & 0x3ff) << 2 1125272cc70bSAndy Fleming | (mmc->csd[2] & 0xc0000000) >> 30; 1126272cc70bSAndy Fleming cmult = (mmc->csd[2] & 0x00038000) >> 15; 1127272cc70bSAndy Fleming } 1128272cc70bSAndy Fleming 1129f866a46dSStephen Warren mmc->capacity_user = (csize + 1) << (cmult + 2); 1130f866a46dSStephen Warren mmc->capacity_user *= mmc->read_bl_len; 1131f866a46dSStephen Warren mmc->capacity_boot = 0; 1132f866a46dSStephen Warren mmc->capacity_rpmb = 0; 1133f866a46dSStephen Warren for (i = 0; i < 4; i++) 1134f866a46dSStephen Warren mmc->capacity_gp[i] = 0; 1135272cc70bSAndy Fleming 11368bfa195eSSimon Glass if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN) 11378bfa195eSSimon Glass mmc->read_bl_len = MMC_MAX_BLOCK_LEN; 1138272cc70bSAndy Fleming 11398bfa195eSSimon Glass if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN) 11408bfa195eSSimon Glass mmc->write_bl_len = MMC_MAX_BLOCK_LEN; 1141272cc70bSAndy Fleming 1142ab71188cSMarkus Niebel if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) { 1143ab71188cSMarkus Niebel cmd.cmdidx = MMC_CMD_SET_DSR; 1144ab71188cSMarkus Niebel cmd.cmdarg = (mmc->dsr & 0xffff) << 16; 1145ab71188cSMarkus Niebel cmd.resp_type = MMC_RSP_NONE; 1146ab71188cSMarkus Niebel if (mmc_send_cmd(mmc, &cmd, NULL)) 1147ab71188cSMarkus Niebel printf("MMC: SET_DSR failed\n"); 1148ab71188cSMarkus Niebel } 1149ab71188cSMarkus Niebel 1150272cc70bSAndy Fleming /* Select the card, and put it into Transfer Mode */ 1151d52ebf10SThomas Chou if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */ 1152272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_SELECT_CARD; 1153fe8f7066SAjay Bhargav cmd.resp_type = MMC_RSP_R1; 1154272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1155272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1156272cc70bSAndy Fleming 1157272cc70bSAndy Fleming if (err) 1158272cc70bSAndy Fleming return err; 1159d52ebf10SThomas Chou } 1160272cc70bSAndy Fleming 1161e6f99a56SLei Wen /* 1162e6f99a56SLei Wen * For SD, its erase group is always one sector 1163e6f99a56SLei Wen */ 1164e6f99a56SLei Wen mmc->erase_grp_size = 1; 1165bc897b1dSLei Wen mmc->part_config = MMCPART_NOAVAILABLE; 1166d23e2c09SSukumar Ghorai if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) { 1167d23e2c09SSukumar Ghorai /* check ext_csd version and capacity */ 1168d23e2c09SSukumar Ghorai err = mmc_send_ext_csd(mmc, ext_csd); 11699cf199ebSDiego Santa Cruz if (err) 11709cf199ebSDiego Santa Cruz return err; 11719cf199ebSDiego Santa Cruz if (ext_csd[EXT_CSD_REV] >= 2) { 1172639b7827SYoshihiro Shimoda /* 1173639b7827SYoshihiro Shimoda * According to the JEDEC Standard, the value of 1174639b7827SYoshihiro Shimoda * ext_csd's capacity is valid if the value is more 1175639b7827SYoshihiro Shimoda * than 2GB 1176639b7827SYoshihiro Shimoda */ 11770560db18SLei Wen capacity = ext_csd[EXT_CSD_SEC_CNT] << 0 11780560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 11790560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 11800560db18SLei Wen | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; 11818bfa195eSSimon Glass capacity *= MMC_MAX_BLOCK_LEN; 1182b1f1e821SŁukasz Majewski if ((capacity >> 20) > 2 * 1024) 1183f866a46dSStephen Warren mmc->capacity_user = capacity; 1184d23e2c09SSukumar Ghorai } 1185bc897b1dSLei Wen 118664f4a619SJaehoon Chung switch (ext_csd[EXT_CSD_REV]) { 118764f4a619SJaehoon Chung case 1: 118864f4a619SJaehoon Chung mmc->version = MMC_VERSION_4_1; 118964f4a619SJaehoon Chung break; 119064f4a619SJaehoon Chung case 2: 119164f4a619SJaehoon Chung mmc->version = MMC_VERSION_4_2; 119264f4a619SJaehoon Chung break; 119364f4a619SJaehoon Chung case 3: 119464f4a619SJaehoon Chung mmc->version = MMC_VERSION_4_3; 119564f4a619SJaehoon Chung break; 119664f4a619SJaehoon Chung case 5: 119764f4a619SJaehoon Chung mmc->version = MMC_VERSION_4_41; 119864f4a619SJaehoon Chung break; 119964f4a619SJaehoon Chung case 6: 120064f4a619SJaehoon Chung mmc->version = MMC_VERSION_4_5; 120164f4a619SJaehoon Chung break; 1202edab723bSMarkus Niebel case 7: 1203edab723bSMarkus Niebel mmc->version = MMC_VERSION_5_0; 1204edab723bSMarkus Niebel break; 120564f4a619SJaehoon Chung } 120664f4a619SJaehoon Chung 12078a0cf490SDiego Santa Cruz /* The partition data may be non-zero but it is only 12088a0cf490SDiego Santa Cruz * effective if PARTITION_SETTING_COMPLETED is set in 12098a0cf490SDiego Santa Cruz * EXT_CSD, so ignore any data if this bit is not set, 12108a0cf490SDiego Santa Cruz * except for enabling the high-capacity group size 12118a0cf490SDiego Santa Cruz * definition (see below). */ 12128a0cf490SDiego Santa Cruz part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] & 12138a0cf490SDiego Santa Cruz EXT_CSD_PARTITION_SETTING_COMPLETED); 12148a0cf490SDiego Santa Cruz 12150c453bb7SDiego Santa Cruz /* store the partition info of emmc */ 12160c453bb7SDiego Santa Cruz mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT]; 12170c453bb7SDiego Santa Cruz if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) || 12180c453bb7SDiego Santa Cruz ext_csd[EXT_CSD_BOOT_MULT]) 12190c453bb7SDiego Santa Cruz mmc->part_config = ext_csd[EXT_CSD_PART_CONF]; 12208a0cf490SDiego Santa Cruz if (part_completed && 12218a0cf490SDiego Santa Cruz (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT)) 12220c453bb7SDiego Santa Cruz mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE]; 12230c453bb7SDiego Santa Cruz 12240c453bb7SDiego Santa Cruz mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17; 12250c453bb7SDiego Santa Cruz 12260c453bb7SDiego Santa Cruz mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17; 12270c453bb7SDiego Santa Cruz 12280c453bb7SDiego Santa Cruz for (i = 0; i < 4; i++) { 12290c453bb7SDiego Santa Cruz int idx = EXT_CSD_GP_SIZE_MULT + i * 3; 12308a0cf490SDiego Santa Cruz uint mult = (ext_csd[idx + 2] << 16) + 12310c453bb7SDiego Santa Cruz (ext_csd[idx + 1] << 8) + ext_csd[idx]; 12328a0cf490SDiego Santa Cruz if (mult) 12338a0cf490SDiego Santa Cruz has_parts = true; 12348a0cf490SDiego Santa Cruz if (!part_completed) 12358a0cf490SDiego Santa Cruz continue; 12368a0cf490SDiego Santa Cruz mmc->capacity_gp[i] = mult; 12370c453bb7SDiego Santa Cruz mmc->capacity_gp[i] *= 12380c453bb7SDiego Santa Cruz ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; 12390c453bb7SDiego Santa Cruz mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 1240f8e89d67SDiego Santa Cruz mmc->capacity_gp[i] <<= 19; 12410c453bb7SDiego Santa Cruz } 12420c453bb7SDiego Santa Cruz 12438a0cf490SDiego Santa Cruz if (part_completed) { 1244a7f852b6SDiego Santa Cruz mmc->enh_user_size = 1245a7f852b6SDiego Santa Cruz (ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) + 1246a7f852b6SDiego Santa Cruz (ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) + 1247a7f852b6SDiego Santa Cruz ext_csd[EXT_CSD_ENH_SIZE_MULT]; 1248a7f852b6SDiego Santa Cruz mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; 1249a7f852b6SDiego Santa Cruz mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 1250a7f852b6SDiego Santa Cruz mmc->enh_user_size <<= 19; 1251a7f852b6SDiego Santa Cruz mmc->enh_user_start = 1252a7f852b6SDiego Santa Cruz (ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) + 1253a7f852b6SDiego Santa Cruz (ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) + 1254a7f852b6SDiego Santa Cruz (ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) + 1255a7f852b6SDiego Santa Cruz ext_csd[EXT_CSD_ENH_START_ADDR]; 1256a7f852b6SDiego Santa Cruz if (mmc->high_capacity) 1257a7f852b6SDiego Santa Cruz mmc->enh_user_start <<= 9; 12588a0cf490SDiego Santa Cruz } 1259a7f852b6SDiego Santa Cruz 1260e6f99a56SLei Wen /* 12611937e5aaSOliver Metz * Host needs to enable ERASE_GRP_DEF bit if device is 12621937e5aaSOliver Metz * partitioned. This bit will be lost every time after a reset 12631937e5aaSOliver Metz * or power off. This will affect erase size. 1264e6f99a56SLei Wen */ 12658a0cf490SDiego Santa Cruz if (part_completed) 12660c453bb7SDiego Santa Cruz has_parts = true; 12671937e5aaSOliver Metz if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) && 12680c453bb7SDiego Santa Cruz (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB)) 12690c453bb7SDiego Santa Cruz has_parts = true; 12700c453bb7SDiego Santa Cruz if (has_parts) { 12711937e5aaSOliver Metz err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 12721937e5aaSOliver Metz EXT_CSD_ERASE_GROUP_DEF, 1); 12731937e5aaSOliver Metz 12741937e5aaSOliver Metz if (err) 12751937e5aaSOliver Metz return err; 1276021a8055SHannes Petermaier else 1277021a8055SHannes Petermaier ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1; 1278037dc0abSDiego Santa Cruz } 12791937e5aaSOliver Metz 1280037dc0abSDiego Santa Cruz if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) { 12811937e5aaSOliver Metz /* Read out group size from ext_csd */ 12820560db18SLei Wen mmc->erase_grp_size = 1283a4ff9f83SDiego Santa Cruz ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024; 1284d7b29129SMarkus Niebel /* 1285d7b29129SMarkus Niebel * if high capacity and partition setting completed 1286d7b29129SMarkus Niebel * SEC_COUNT is valid even if it is smaller than 2 GiB 1287d7b29129SMarkus Niebel * JEDEC Standard JESD84-B45, 6.2.4 1288d7b29129SMarkus Niebel */ 12898a0cf490SDiego Santa Cruz if (mmc->high_capacity && part_completed) { 1290d7b29129SMarkus Niebel capacity = (ext_csd[EXT_CSD_SEC_CNT]) | 1291d7b29129SMarkus Niebel (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) | 1292d7b29129SMarkus Niebel (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) | 1293d7b29129SMarkus Niebel (ext_csd[EXT_CSD_SEC_CNT + 3] << 24); 1294d7b29129SMarkus Niebel capacity *= MMC_MAX_BLOCK_LEN; 1295d7b29129SMarkus Niebel mmc->capacity_user = capacity; 1296d7b29129SMarkus Niebel } 12978bfa195eSSimon Glass } else { 12981937e5aaSOliver Metz /* Calculate the group size from the csd value. */ 1299e6f99a56SLei Wen int erase_gsz, erase_gmul; 1300e6f99a56SLei Wen erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10; 1301e6f99a56SLei Wen erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5; 1302e6f99a56SLei Wen mmc->erase_grp_size = (erase_gsz + 1) 1303e6f99a56SLei Wen * (erase_gmul + 1); 1304e6f99a56SLei Wen } 1305037dc0abSDiego Santa Cruz 1306037dc0abSDiego Santa Cruz mmc->hc_wp_grp_size = 1024 1307037dc0abSDiego Santa Cruz * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] 1308037dc0abSDiego Santa Cruz * ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; 1309f866a46dSStephen Warren } 1310f866a46dSStephen Warren 1311f866a46dSStephen Warren err = mmc_set_capacity(mmc, mmc->part_num); 1312f866a46dSStephen Warren if (err) 1313f866a46dSStephen Warren return err; 1314d23e2c09SSukumar Ghorai 1315272cc70bSAndy Fleming if (IS_SD(mmc)) 1316272cc70bSAndy Fleming err = sd_change_freq(mmc); 1317272cc70bSAndy Fleming else 1318272cc70bSAndy Fleming err = mmc_change_freq(mmc); 1319272cc70bSAndy Fleming 1320272cc70bSAndy Fleming if (err) 1321272cc70bSAndy Fleming return err; 1322272cc70bSAndy Fleming 1323272cc70bSAndy Fleming /* Restrict card's capabilities by what the host can do */ 132493bfd616SPantelis Antoniou mmc->card_caps &= mmc->cfg->host_caps; 1325272cc70bSAndy Fleming 1326272cc70bSAndy Fleming if (IS_SD(mmc)) { 1327272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_4BIT) { 1328272cc70bSAndy Fleming cmd.cmdidx = MMC_CMD_APP_CMD; 1329272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 1330272cc70bSAndy Fleming cmd.cmdarg = mmc->rca << 16; 1331272cc70bSAndy Fleming 1332272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1333272cc70bSAndy Fleming if (err) 1334272cc70bSAndy Fleming return err; 1335272cc70bSAndy Fleming 1336272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; 1337272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R1; 1338272cc70bSAndy Fleming cmd.cmdarg = 2; 1339272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1340272cc70bSAndy Fleming if (err) 1341272cc70bSAndy Fleming return err; 1342272cc70bSAndy Fleming 1343272cc70bSAndy Fleming mmc_set_bus_width(mmc, 4); 1344272cc70bSAndy Fleming } 1345272cc70bSAndy Fleming 1346272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) 1347ad5fd922SJaehoon Chung mmc->tran_speed = 50000000; 1348272cc70bSAndy Fleming else 1349ad5fd922SJaehoon Chung mmc->tran_speed = 25000000; 1350272cc70bSAndy Fleming } else { 13517798f6dbSAndy Fleming int idx; 13527798f6dbSAndy Fleming 13537798f6dbSAndy Fleming /* An array of possible bus widths in order of preference */ 13547798f6dbSAndy Fleming static unsigned ext_csd_bits[] = { 1355d22e3d46SJaehoon Chung EXT_CSD_DDR_BUS_WIDTH_8, 1356d22e3d46SJaehoon Chung EXT_CSD_DDR_BUS_WIDTH_4, 13577798f6dbSAndy Fleming EXT_CSD_BUS_WIDTH_8, 13587798f6dbSAndy Fleming EXT_CSD_BUS_WIDTH_4, 13597798f6dbSAndy Fleming EXT_CSD_BUS_WIDTH_1, 13607798f6dbSAndy Fleming }; 13617798f6dbSAndy Fleming 13627798f6dbSAndy Fleming /* An array to map CSD bus widths to host cap bits */ 13637798f6dbSAndy Fleming static unsigned ext_to_hostcaps[] = { 1364786e8f81SAndrew Gabbasov [EXT_CSD_DDR_BUS_WIDTH_4] = 1365786e8f81SAndrew Gabbasov MMC_MODE_DDR_52MHz | MMC_MODE_4BIT, 1366786e8f81SAndrew Gabbasov [EXT_CSD_DDR_BUS_WIDTH_8] = 1367786e8f81SAndrew Gabbasov MMC_MODE_DDR_52MHz | MMC_MODE_8BIT, 13687798f6dbSAndy Fleming [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT, 13697798f6dbSAndy Fleming [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT, 13707798f6dbSAndy Fleming }; 13717798f6dbSAndy Fleming 13727798f6dbSAndy Fleming /* An array to map chosen bus width to an integer */ 13737798f6dbSAndy Fleming static unsigned widths[] = { 1374d22e3d46SJaehoon Chung 8, 4, 8, 4, 1, 13757798f6dbSAndy Fleming }; 13767798f6dbSAndy Fleming 13777798f6dbSAndy Fleming for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) { 13787798f6dbSAndy Fleming unsigned int extw = ext_csd_bits[idx]; 1379786e8f81SAndrew Gabbasov unsigned int caps = ext_to_hostcaps[extw]; 13807798f6dbSAndy Fleming 13817798f6dbSAndy Fleming /* 1382786e8f81SAndrew Gabbasov * Check to make sure the card and controller support 1383786e8f81SAndrew Gabbasov * these capabilities 13847798f6dbSAndy Fleming */ 1385786e8f81SAndrew Gabbasov if ((mmc->card_caps & caps) != caps) 13867798f6dbSAndy Fleming continue; 13877798f6dbSAndy Fleming 1388272cc70bSAndy Fleming err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, 13897798f6dbSAndy Fleming EXT_CSD_BUS_WIDTH, extw); 1390272cc70bSAndy Fleming 1391272cc70bSAndy Fleming if (err) 13924137894eSLei Wen continue; 1393272cc70bSAndy Fleming 1394786e8f81SAndrew Gabbasov mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0; 13957798f6dbSAndy Fleming mmc_set_bus_width(mmc, widths[idx]); 1396272cc70bSAndy Fleming 13974137894eSLei Wen err = mmc_send_ext_csd(mmc, test_csd); 1398272cc70bSAndy Fleming 1399786e8f81SAndrew Gabbasov if (err) 1400786e8f81SAndrew Gabbasov continue; 1401786e8f81SAndrew Gabbasov 1402786e8f81SAndrew Gabbasov /* Only compare read only fields */ 1403786e8f81SAndrew Gabbasov if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] 1404786e8f81SAndrew Gabbasov == test_csd[EXT_CSD_PARTITIONING_SUPPORT] && 1405786e8f81SAndrew Gabbasov ext_csd[EXT_CSD_HC_WP_GRP_SIZE] 1406786e8f81SAndrew Gabbasov == test_csd[EXT_CSD_HC_WP_GRP_SIZE] && 1407786e8f81SAndrew Gabbasov ext_csd[EXT_CSD_REV] 1408786e8f81SAndrew Gabbasov == test_csd[EXT_CSD_REV] && 1409786e8f81SAndrew Gabbasov ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] 1410786e8f81SAndrew Gabbasov == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] && 1411786e8f81SAndrew Gabbasov memcmp(&ext_csd[EXT_CSD_SEC_CNT], 1412786e8f81SAndrew Gabbasov &test_csd[EXT_CSD_SEC_CNT], 4) == 0) 14134137894eSLei Wen break; 1414786e8f81SAndrew Gabbasov else 1415786e8f81SAndrew Gabbasov err = SWITCH_ERR; 14164137894eSLei Wen } 1417786e8f81SAndrew Gabbasov 1418786e8f81SAndrew Gabbasov if (err) 1419786e8f81SAndrew Gabbasov return err; 1420272cc70bSAndy Fleming 1421272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS) { 1422272cc70bSAndy Fleming if (mmc->card_caps & MMC_MODE_HS_52MHz) 1423ad5fd922SJaehoon Chung mmc->tran_speed = 52000000; 1424272cc70bSAndy Fleming else 1425ad5fd922SJaehoon Chung mmc->tran_speed = 26000000; 1426272cc70bSAndy Fleming } 1427ad5fd922SJaehoon Chung } 1428ad5fd922SJaehoon Chung 1429ad5fd922SJaehoon Chung mmc_set_clock(mmc, mmc->tran_speed); 1430272cc70bSAndy Fleming 14315af8f45cSAndrew Gabbasov /* Fix the block length for DDR mode */ 14325af8f45cSAndrew Gabbasov if (mmc->ddr_mode) { 14335af8f45cSAndrew Gabbasov mmc->read_bl_len = MMC_MAX_BLOCK_LEN; 14345af8f45cSAndrew Gabbasov mmc->write_bl_len = MMC_MAX_BLOCK_LEN; 14355af8f45cSAndrew Gabbasov } 14365af8f45cSAndrew Gabbasov 1437272cc70bSAndy Fleming /* fill in device description */ 1438272cc70bSAndy Fleming mmc->block_dev.lun = 0; 1439272cc70bSAndy Fleming mmc->block_dev.type = 0; 1440272cc70bSAndy Fleming mmc->block_dev.blksz = mmc->read_bl_len; 14410472fbfdSEgbert Eich mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz); 14429b1f942cSRabin Vincent mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); 144356196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1444babce5f6STaylor Hutt sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x", 1445babce5f6STaylor Hutt mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff), 1446babce5f6STaylor Hutt (mmc->cid[3] >> 16) & 0xffff); 1447babce5f6STaylor Hutt sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff, 14480b453ffeSRabin Vincent (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, 1449babce5f6STaylor Hutt (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff, 1450babce5f6STaylor Hutt (mmc->cid[2] >> 24) & 0xff); 1451babce5f6STaylor Hutt sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf, 1452babce5f6STaylor Hutt (mmc->cid[2] >> 16) & 0xf); 145356196826SPaul Burton #else 145456196826SPaul Burton mmc->block_dev.vendor[0] = 0; 145556196826SPaul Burton mmc->block_dev.product[0] = 0; 145656196826SPaul Burton mmc->block_dev.revision[0] = 0; 145756196826SPaul Burton #endif 1458122efd43SMikhail Kshevetskiy #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT) 1459272cc70bSAndy Fleming init_part(&mmc->block_dev); 1460122efd43SMikhail Kshevetskiy #endif 1461272cc70bSAndy Fleming 1462272cc70bSAndy Fleming return 0; 1463272cc70bSAndy Fleming } 1464272cc70bSAndy Fleming 1465fdbb873eSKim Phillips static int mmc_send_if_cond(struct mmc *mmc) 1466272cc70bSAndy Fleming { 1467272cc70bSAndy Fleming struct mmc_cmd cmd; 1468272cc70bSAndy Fleming int err; 1469272cc70bSAndy Fleming 1470272cc70bSAndy Fleming cmd.cmdidx = SD_CMD_SEND_IF_COND; 1471272cc70bSAndy Fleming /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ 147293bfd616SPantelis Antoniou cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa; 1473272cc70bSAndy Fleming cmd.resp_type = MMC_RSP_R7; 1474272cc70bSAndy Fleming 1475272cc70bSAndy Fleming err = mmc_send_cmd(mmc, &cmd, NULL); 1476272cc70bSAndy Fleming 1477272cc70bSAndy Fleming if (err) 1478272cc70bSAndy Fleming return err; 1479272cc70bSAndy Fleming 1480998be3ddSRabin Vincent if ((cmd.response[0] & 0xff) != 0xaa) 1481272cc70bSAndy Fleming return UNUSABLE_ERR; 1482272cc70bSAndy Fleming else 1483272cc70bSAndy Fleming mmc->version = SD_VERSION_2; 1484272cc70bSAndy Fleming 1485272cc70bSAndy Fleming return 0; 1486272cc70bSAndy Fleming } 1487272cc70bSAndy Fleming 148893bfd616SPantelis Antoniou /* not used any more */ 148993bfd616SPantelis Antoniou int __deprecated mmc_register(struct mmc *mmc) 1490272cc70bSAndy Fleming { 149193bfd616SPantelis Antoniou #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 149293bfd616SPantelis Antoniou printf("%s is deprecated! use mmc_create() instead.\n", __func__); 149393bfd616SPantelis Antoniou #endif 149493bfd616SPantelis Antoniou return -1; 149593bfd616SPantelis Antoniou } 149693bfd616SPantelis Antoniou 149793bfd616SPantelis Antoniou struct mmc *mmc_create(const struct mmc_config *cfg, void *priv) 149893bfd616SPantelis Antoniou { 149993bfd616SPantelis Antoniou struct mmc *mmc; 150093bfd616SPantelis Antoniou 150193bfd616SPantelis Antoniou /* quick validation */ 150293bfd616SPantelis Antoniou if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL || 150393bfd616SPantelis Antoniou cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0) 150493bfd616SPantelis Antoniou return NULL; 150593bfd616SPantelis Antoniou 150693bfd616SPantelis Antoniou mmc = calloc(1, sizeof(*mmc)); 150793bfd616SPantelis Antoniou if (mmc == NULL) 150893bfd616SPantelis Antoniou return NULL; 150993bfd616SPantelis Antoniou 151093bfd616SPantelis Antoniou mmc->cfg = cfg; 151193bfd616SPantelis Antoniou mmc->priv = priv; 151293bfd616SPantelis Antoniou 151393bfd616SPantelis Antoniou /* the following chunk was mmc_register() */ 151493bfd616SPantelis Antoniou 1515ab71188cSMarkus Niebel /* Setup dsr related values */ 1516ab71188cSMarkus Niebel mmc->dsr_imp = 0; 1517ab71188cSMarkus Niebel mmc->dsr = 0xffffffff; 1518272cc70bSAndy Fleming /* Setup the universal parts of the block interface just once */ 1519272cc70bSAndy Fleming mmc->block_dev.if_type = IF_TYPE_MMC; 1520272cc70bSAndy Fleming mmc->block_dev.dev = cur_dev_num++; 1521272cc70bSAndy Fleming mmc->block_dev.removable = 1; 1522272cc70bSAndy Fleming mmc->block_dev.block_read = mmc_bread; 1523272cc70bSAndy Fleming mmc->block_dev.block_write = mmc_bwrite; 1524e6f99a56SLei Wen mmc->block_dev.block_erase = mmc_berase; 152593bfd616SPantelis Antoniou 152693bfd616SPantelis Antoniou /* setup initial part type */ 152793bfd616SPantelis Antoniou mmc->block_dev.part_type = mmc->cfg->part_type; 1528272cc70bSAndy Fleming 1529272cc70bSAndy Fleming INIT_LIST_HEAD(&mmc->link); 1530272cc70bSAndy Fleming 1531272cc70bSAndy Fleming list_add_tail(&mmc->link, &mmc_devices); 1532272cc70bSAndy Fleming 153393bfd616SPantelis Antoniou return mmc; 153493bfd616SPantelis Antoniou } 153593bfd616SPantelis Antoniou 153693bfd616SPantelis Antoniou void mmc_destroy(struct mmc *mmc) 153793bfd616SPantelis Antoniou { 153893bfd616SPantelis Antoniou /* only freeing memory for now */ 153993bfd616SPantelis Antoniou free(mmc); 1540272cc70bSAndy Fleming } 1541272cc70bSAndy Fleming 1542df3fc526SMatthew McClintock #ifdef CONFIG_PARTITIONS 1543272cc70bSAndy Fleming block_dev_desc_t *mmc_get_dev(int dev) 1544272cc70bSAndy Fleming { 1545272cc70bSAndy Fleming struct mmc *mmc = find_mmc_device(dev); 15466bb4b4bcSBenoît Thébaudeau if (!mmc || mmc_init(mmc)) 154740242bc3SŁukasz Majewski return NULL; 1548272cc70bSAndy Fleming 154940242bc3SŁukasz Majewski return &mmc->block_dev; 1550272cc70bSAndy Fleming } 1551df3fc526SMatthew McClintock #endif 1552272cc70bSAndy Fleming 155395de9ab2SPaul Kocialkowski /* board-specific MMC power initializations. */ 155495de9ab2SPaul Kocialkowski __weak void board_mmc_power_init(void) 155595de9ab2SPaul Kocialkowski { 155695de9ab2SPaul Kocialkowski } 155795de9ab2SPaul Kocialkowski 1558e9550449SChe-Liang Chiou int mmc_start_init(struct mmc *mmc) 1559272cc70bSAndy Fleming { 1560afd5932bSMacpaul Lin int err; 1561272cc70bSAndy Fleming 1562ab769f22SPantelis Antoniou /* we pretend there's no card when init is NULL */ 156393bfd616SPantelis Antoniou if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) { 156448972d90SThierry Reding mmc->has_init = 0; 156556196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 156648972d90SThierry Reding printf("MMC: no card present\n"); 156756196826SPaul Burton #endif 156848972d90SThierry Reding return NO_CARD_ERR; 156948972d90SThierry Reding } 157048972d90SThierry Reding 1571bc897b1dSLei Wen if (mmc->has_init) 1572bc897b1dSLei Wen return 0; 1573bc897b1dSLei Wen 157495de9ab2SPaul Kocialkowski board_mmc_power_init(); 157595de9ab2SPaul Kocialkowski 1576ab769f22SPantelis Antoniou /* made sure it's not NULL earlier */ 157793bfd616SPantelis Antoniou err = mmc->cfg->ops->init(mmc); 1578272cc70bSAndy Fleming 1579272cc70bSAndy Fleming if (err) 1580272cc70bSAndy Fleming return err; 1581272cc70bSAndy Fleming 1582786e8f81SAndrew Gabbasov mmc->ddr_mode = 0; 1583b86b85e2SIlya Yanok mmc_set_bus_width(mmc, 1); 1584b86b85e2SIlya Yanok mmc_set_clock(mmc, 1); 1585b86b85e2SIlya Yanok 1586272cc70bSAndy Fleming /* Reset the Card */ 1587272cc70bSAndy Fleming err = mmc_go_idle(mmc); 1588272cc70bSAndy Fleming 1589272cc70bSAndy Fleming if (err) 1590272cc70bSAndy Fleming return err; 1591272cc70bSAndy Fleming 1592bc897b1dSLei Wen /* The internal partition reset to user partition(0) at every CMD0*/ 1593bc897b1dSLei Wen mmc->part_num = 0; 1594bc897b1dSLei Wen 1595272cc70bSAndy Fleming /* Test for SD version 2 */ 1596272cc70bSAndy Fleming err = mmc_send_if_cond(mmc); 1597272cc70bSAndy Fleming 1598272cc70bSAndy Fleming /* Now try to get the SD card's operating condition */ 1599272cc70bSAndy Fleming err = sd_send_op_cond(mmc); 1600272cc70bSAndy Fleming 1601272cc70bSAndy Fleming /* If the command timed out, we check for an MMC card */ 1602272cc70bSAndy Fleming if (err == TIMEOUT) { 1603272cc70bSAndy Fleming err = mmc_send_op_cond(mmc); 1604272cc70bSAndy Fleming 1605e9550449SChe-Liang Chiou if (err && err != IN_PROGRESS) { 160656196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 1607272cc70bSAndy Fleming printf("Card did not respond to voltage select!\n"); 160856196826SPaul Burton #endif 1609272cc70bSAndy Fleming return UNUSABLE_ERR; 1610272cc70bSAndy Fleming } 1611272cc70bSAndy Fleming } 1612272cc70bSAndy Fleming 1613e9550449SChe-Liang Chiou if (err == IN_PROGRESS) 1614e9550449SChe-Liang Chiou mmc->init_in_progress = 1; 1615e9550449SChe-Liang Chiou 1616e9550449SChe-Liang Chiou return err; 1617e9550449SChe-Liang Chiou } 1618e9550449SChe-Liang Chiou 1619e9550449SChe-Liang Chiou static int mmc_complete_init(struct mmc *mmc) 1620e9550449SChe-Liang Chiou { 1621e9550449SChe-Liang Chiou int err = 0; 1622e9550449SChe-Liang Chiou 1623e9550449SChe-Liang Chiou if (mmc->op_cond_pending) 1624e9550449SChe-Liang Chiou err = mmc_complete_op_cond(mmc); 1625e9550449SChe-Liang Chiou 1626e9550449SChe-Liang Chiou if (!err) 1627bc897b1dSLei Wen err = mmc_startup(mmc); 1628bc897b1dSLei Wen if (err) 1629bc897b1dSLei Wen mmc->has_init = 0; 1630bc897b1dSLei Wen else 1631bc897b1dSLei Wen mmc->has_init = 1; 1632e9550449SChe-Liang Chiou mmc->init_in_progress = 0; 1633e9550449SChe-Liang Chiou return err; 1634e9550449SChe-Liang Chiou } 1635e9550449SChe-Liang Chiou 1636e9550449SChe-Liang Chiou int mmc_init(struct mmc *mmc) 1637e9550449SChe-Liang Chiou { 1638e9550449SChe-Liang Chiou int err = IN_PROGRESS; 1639d803fea5SMateusz Zalega unsigned start; 1640e9550449SChe-Liang Chiou 1641e9550449SChe-Liang Chiou if (mmc->has_init) 1642e9550449SChe-Liang Chiou return 0; 1643d803fea5SMateusz Zalega 1644d803fea5SMateusz Zalega start = get_timer(0); 1645d803fea5SMateusz Zalega 1646e9550449SChe-Liang Chiou if (!mmc->init_in_progress) 1647e9550449SChe-Liang Chiou err = mmc_start_init(mmc); 1648e9550449SChe-Liang Chiou 1649e9550449SChe-Liang Chiou if (!err || err == IN_PROGRESS) 1650e9550449SChe-Liang Chiou err = mmc_complete_init(mmc); 1651e9550449SChe-Liang Chiou debug("%s: %d, time %lu\n", __func__, err, get_timer(start)); 1652bc897b1dSLei Wen return err; 1653272cc70bSAndy Fleming } 1654272cc70bSAndy Fleming 1655ab71188cSMarkus Niebel int mmc_set_dsr(struct mmc *mmc, u16 val) 1656ab71188cSMarkus Niebel { 1657ab71188cSMarkus Niebel mmc->dsr = val; 1658ab71188cSMarkus Niebel return 0; 1659ab71188cSMarkus Niebel } 1660ab71188cSMarkus Niebel 1661cee9ab7cSJeroen Hofstee /* CPU-specific MMC initializations */ 1662cee9ab7cSJeroen Hofstee __weak int cpu_mmc_init(bd_t *bis) 1663272cc70bSAndy Fleming { 1664272cc70bSAndy Fleming return -1; 1665272cc70bSAndy Fleming } 1666272cc70bSAndy Fleming 1667cee9ab7cSJeroen Hofstee /* board-specific MMC initializations. */ 1668cee9ab7cSJeroen Hofstee __weak int board_mmc_init(bd_t *bis) 1669cee9ab7cSJeroen Hofstee { 1670cee9ab7cSJeroen Hofstee return -1; 1671cee9ab7cSJeroen Hofstee } 1672272cc70bSAndy Fleming 167356196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) 167456196826SPaul Burton 1675272cc70bSAndy Fleming void print_mmc_devices(char separator) 1676272cc70bSAndy Fleming { 1677272cc70bSAndy Fleming struct mmc *m; 1678272cc70bSAndy Fleming struct list_head *entry; 1679272cc70bSAndy Fleming 1680272cc70bSAndy Fleming list_for_each(entry, &mmc_devices) { 1681272cc70bSAndy Fleming m = list_entry(entry, struct mmc, link); 1682272cc70bSAndy Fleming 168393bfd616SPantelis Antoniou printf("%s: %d", m->cfg->name, m->block_dev.dev); 1684272cc70bSAndy Fleming 1685e75eaf10SLubomir Popov if (entry->next != &mmc_devices) { 1686272cc70bSAndy Fleming printf("%c", separator); 1687e75eaf10SLubomir Popov if (separator != '\n') 1688e75eaf10SLubomir Popov puts (" "); 1689e75eaf10SLubomir Popov } 1690272cc70bSAndy Fleming } 1691272cc70bSAndy Fleming 1692272cc70bSAndy Fleming printf("\n"); 1693272cc70bSAndy Fleming } 1694272cc70bSAndy Fleming 169556196826SPaul Burton #else 169656196826SPaul Burton void print_mmc_devices(char separator) { } 169756196826SPaul Burton #endif 169856196826SPaul Burton 1699ea6ebe21SLei Wen int get_mmc_num(void) 1700ea6ebe21SLei Wen { 1701ea6ebe21SLei Wen return cur_dev_num; 1702ea6ebe21SLei Wen } 1703ea6ebe21SLei Wen 1704e9550449SChe-Liang Chiou void mmc_set_preinit(struct mmc *mmc, int preinit) 1705e9550449SChe-Liang Chiou { 1706e9550449SChe-Liang Chiou mmc->preinit = preinit; 1707e9550449SChe-Liang Chiou } 1708e9550449SChe-Liang Chiou 1709e9550449SChe-Liang Chiou static void do_preinit(void) 1710e9550449SChe-Liang Chiou { 1711e9550449SChe-Liang Chiou struct mmc *m; 1712e9550449SChe-Liang Chiou struct list_head *entry; 1713e9550449SChe-Liang Chiou 1714e9550449SChe-Liang Chiou list_for_each(entry, &mmc_devices) { 1715e9550449SChe-Liang Chiou m = list_entry(entry, struct mmc, link); 1716e9550449SChe-Liang Chiou 1717e9550449SChe-Liang Chiou if (m->preinit) 1718e9550449SChe-Liang Chiou mmc_start_init(m); 1719e9550449SChe-Liang Chiou } 1720e9550449SChe-Liang Chiou } 1721e9550449SChe-Liang Chiou 1722e9550449SChe-Liang Chiou 1723272cc70bSAndy Fleming int mmc_initialize(bd_t *bis) 1724272cc70bSAndy Fleming { 1725272cc70bSAndy Fleming INIT_LIST_HEAD (&mmc_devices); 1726272cc70bSAndy Fleming cur_dev_num = 0; 1727272cc70bSAndy Fleming 1728272cc70bSAndy Fleming if (board_mmc_init(bis) < 0) 1729272cc70bSAndy Fleming cpu_mmc_init(bis); 1730272cc70bSAndy Fleming 1731bb0dc108SYing Zhang #ifndef CONFIG_SPL_BUILD 1732272cc70bSAndy Fleming print_mmc_devices(','); 1733bb0dc108SYing Zhang #endif 1734272cc70bSAndy Fleming 1735e9550449SChe-Liang Chiou do_preinit(); 1736272cc70bSAndy Fleming return 0; 1737272cc70bSAndy Fleming } 17383690d6d6SAmar 17393690d6d6SAmar #ifdef CONFIG_SUPPORT_EMMC_BOOT 17403690d6d6SAmar /* 17413690d6d6SAmar * This function changes the size of boot partition and the size of rpmb 17423690d6d6SAmar * partition present on EMMC devices. 17433690d6d6SAmar * 17443690d6d6SAmar * Input Parameters: 17453690d6d6SAmar * struct *mmc: pointer for the mmc device strcuture 17463690d6d6SAmar * bootsize: size of boot partition 17473690d6d6SAmar * rpmbsize: size of rpmb partition 17483690d6d6SAmar * 17493690d6d6SAmar * Returns 0 on success. 17503690d6d6SAmar */ 17513690d6d6SAmar 17523690d6d6SAmar int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize, 17533690d6d6SAmar unsigned long rpmbsize) 17543690d6d6SAmar { 17553690d6d6SAmar int err; 17563690d6d6SAmar struct mmc_cmd cmd; 17573690d6d6SAmar 17583690d6d6SAmar /* Only use this command for raw EMMC moviNAND. Enter backdoor mode */ 17593690d6d6SAmar cmd.cmdidx = MMC_CMD_RES_MAN; 17603690d6d6SAmar cmd.resp_type = MMC_RSP_R1b; 17613690d6d6SAmar cmd.cmdarg = MMC_CMD62_ARG1; 17623690d6d6SAmar 17633690d6d6SAmar err = mmc_send_cmd(mmc, &cmd, NULL); 17643690d6d6SAmar if (err) { 17653690d6d6SAmar debug("mmc_boot_partition_size_change: Error1 = %d\n", err); 17663690d6d6SAmar return err; 17673690d6d6SAmar } 17683690d6d6SAmar 17693690d6d6SAmar /* Boot partition changing mode */ 17703690d6d6SAmar cmd.cmdidx = MMC_CMD_RES_MAN; 17713690d6d6SAmar cmd.resp_type = MMC_RSP_R1b; 17723690d6d6SAmar cmd.cmdarg = MMC_CMD62_ARG2; 17733690d6d6SAmar 17743690d6d6SAmar err = mmc_send_cmd(mmc, &cmd, NULL); 17753690d6d6SAmar if (err) { 17763690d6d6SAmar debug("mmc_boot_partition_size_change: Error2 = %d\n", err); 17773690d6d6SAmar return err; 17783690d6d6SAmar } 17793690d6d6SAmar /* boot partition size is multiple of 128KB */ 17803690d6d6SAmar bootsize = (bootsize * 1024) / 128; 17813690d6d6SAmar 17823690d6d6SAmar /* Arg: boot partition size */ 17833690d6d6SAmar cmd.cmdidx = MMC_CMD_RES_MAN; 17843690d6d6SAmar cmd.resp_type = MMC_RSP_R1b; 17853690d6d6SAmar cmd.cmdarg = bootsize; 17863690d6d6SAmar 17873690d6d6SAmar err = mmc_send_cmd(mmc, &cmd, NULL); 17883690d6d6SAmar if (err) { 17893690d6d6SAmar debug("mmc_boot_partition_size_change: Error3 = %d\n", err); 17903690d6d6SAmar return err; 17913690d6d6SAmar } 17923690d6d6SAmar /* RPMB partition size is multiple of 128KB */ 17933690d6d6SAmar rpmbsize = (rpmbsize * 1024) / 128; 17943690d6d6SAmar /* Arg: RPMB partition size */ 17953690d6d6SAmar cmd.cmdidx = MMC_CMD_RES_MAN; 17963690d6d6SAmar cmd.resp_type = MMC_RSP_R1b; 17973690d6d6SAmar cmd.cmdarg = rpmbsize; 17983690d6d6SAmar 17993690d6d6SAmar err = mmc_send_cmd(mmc, &cmd, NULL); 18003690d6d6SAmar if (err) { 18013690d6d6SAmar debug("mmc_boot_partition_size_change: Error4 = %d\n", err); 18023690d6d6SAmar return err; 18033690d6d6SAmar } 18043690d6d6SAmar return 0; 18053690d6d6SAmar } 18063690d6d6SAmar 18073690d6d6SAmar /* 18085a99b9deSTom Rini * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH 18095a99b9deSTom Rini * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH 18105a99b9deSTom Rini * and BOOT_MODE. 18115a99b9deSTom Rini * 18125a99b9deSTom Rini * Returns 0 on success. 18135a99b9deSTom Rini */ 18145a99b9deSTom Rini int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode) 18155a99b9deSTom Rini { 18165a99b9deSTom Rini int err; 18175a99b9deSTom Rini 18185a99b9deSTom Rini err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH, 18195a99b9deSTom Rini EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) | 18205a99b9deSTom Rini EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) | 18215a99b9deSTom Rini EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width)); 18225a99b9deSTom Rini 18235a99b9deSTom Rini if (err) 18245a99b9deSTom Rini return err; 18255a99b9deSTom Rini return 0; 18265a99b9deSTom Rini } 18275a99b9deSTom Rini 18285a99b9deSTom Rini /* 1829792970b0STom Rini * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG) 1830792970b0STom Rini * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and 1831792970b0STom Rini * PARTITION_ACCESS. 1832792970b0STom Rini * 1833792970b0STom Rini * Returns 0 on success. 1834792970b0STom Rini */ 1835792970b0STom Rini int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access) 1836792970b0STom Rini { 1837792970b0STom Rini int err; 1838792970b0STom Rini 1839792970b0STom Rini err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF, 1840792970b0STom Rini EXT_CSD_BOOT_ACK(ack) | 1841792970b0STom Rini EXT_CSD_BOOT_PART_NUM(part_num) | 1842792970b0STom Rini EXT_CSD_PARTITION_ACCESS(access)); 1843792970b0STom Rini 1844792970b0STom Rini if (err) 1845792970b0STom Rini return err; 1846792970b0STom Rini return 0; 1847792970b0STom Rini } 184833ace362STom Rini 184933ace362STom Rini /* 185033ace362STom Rini * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value 185133ace362STom Rini * for enable. Note that this is a write-once field for non-zero values. 185233ace362STom Rini * 185333ace362STom Rini * Returns 0 on success. 185433ace362STom Rini */ 185533ace362STom Rini int mmc_set_rst_n_function(struct mmc *mmc, u8 enable) 185633ace362STom Rini { 185733ace362STom Rini return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION, 185833ace362STom Rini enable); 185933ace362STom Rini } 18603690d6d6SAmar #endif 1861