]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/mmc/mmc.c
Merge branch 'u-boot-samsung/master' into 'u-boot-arm/master'
[karo-tx-uboot.git] / drivers / mmc / mmc.c
index ac07bb9a2a979390fb130f4ae164e2e5fd75663e..55c2c68cdb23218945d0af727632745897278d72 100644 (file)
 #include <div64.h>
 #include "mmc_private.h"
 
-/* Set block count limit because of 16 bit register limit on some hardware*/
-#ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
-#define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
-#endif
-
 static struct list_head mmc_devices;
 static int cur_dev_num = -1;
 
@@ -37,8 +32,8 @@ int mmc_getwp(struct mmc *mmc)
        wp = board_mmc_getwp(mmc);
 
        if (wp < 0) {
-               if (mmc->ops->getwp)
-                       wp = mmc->ops->getwp(mmc);
+               if (mmc->cfg->ops->getwp)
+                       wp = mmc->cfg->ops->getwp(mmc);
                else
                        wp = 0;
        }
@@ -63,7 +58,7 @@ int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
 
        printf("CMD_SEND:%d\n", cmd->cmdidx);
        printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
-       ret = mmc->ops->send_cmd(mmc, cmd, data);
+       ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
        switch (cmd->resp_type) {
                case MMC_RSP_NONE:
                        printf("\t\tMMC_RSP_NONE\n");
@@ -106,7 +101,7 @@ int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
                        break;
        }
 #else
-       ret = mmc->ops->send_cmd(mmc, cmd, data);
+       ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
 #endif
        return ret;
 }
@@ -155,6 +150,8 @@ int mmc_send_status(struct mmc *mmc, int timeout)
 #endif
                return TIMEOUT;
        }
+       if (cmd.response[0] & MMC_STATUS_SWITCH_ERROR)
+               return SWITCH_ERR;
 
        return 0;
 }
@@ -163,6 +160,9 @@ int mmc_set_blocklen(struct mmc *mmc, int len)
 {
        struct mmc_cmd cmd;
 
+       if (mmc->card_caps & MMC_MODE_DDR_52MHz)
+               return 0;
+
        cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
        cmd.resp_type = MMC_RSP_R1;
        cmd.cmdarg = len;
@@ -253,7 +253,8 @@ static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
                return 0;
 
        do {
-               cur = (blocks_todo > mmc->b_max) ?  mmc->b_max : blocks_todo;
+               cur = (blocks_todo > mmc->cfg->b_max) ?
+                       mmc->cfg->b_max : blocks_todo;
                if(mmc_read_blocks(mmc, dst, start, cur) != cur)
                        return 0;
                blocks_todo -= cur;
@@ -312,7 +313,7 @@ static int sd_send_op_cond(struct mmc *mmc)
                 * specified.
                 */
                cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
-                       (mmc->voltages & 0xff8000);
+                       (mmc->cfg->voltages & 0xff8000);
 
                if (mmc->version == SD_VERSION_2)
                        cmd.cmdarg |= OCR_HCS;
@@ -361,11 +362,11 @@ static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
        cmd->cmdarg = 0;
        if (use_arg && !mmc_host_is_spi(mmc)) {
                cmd->cmdarg =
-                       (mmc->voltages &
+                       (mmc->cfg->voltages &
                        (mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
                        (mmc->op_cond_response & OCR_ACCESS_MODE);
 
-               if (mmc->host_caps & MMC_MODE_HC)
+               if (mmc->cfg->host_caps & MMC_MODE_HC)
                        cmd->cmdarg |= OCR_HCS;
        }
        err = mmc_send_cmd(mmc, cmd, NULL);
@@ -505,7 +506,7 @@ static int mmc_change_freq(struct mmc *mmc)
        err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
 
        if (err)
-               return err;
+               return err == SWITCH_ERR ? 0 : err;
 
        /* Now check to see that it worked */
        err = mmc_send_ext_csd(mmc, ext_csd);
@@ -518,10 +519,13 @@ static int mmc_change_freq(struct mmc *mmc)
                return 0;
 
        /* High Speed is set, there are two types: 52MHz and 26MHz */
-       if (cardtype & MMC_HS_52MHZ)
+       if (cardtype & EXT_CSD_CARD_TYPE_52) {
+               if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
+                       mmc->card_caps |= MMC_MODE_DDR_52MHz;
                mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
-       else
+       } else {
                mmc->card_caps |= MMC_MODE_HS;
+       }
 
        return 0;
 }
@@ -554,6 +558,32 @@ static int mmc_set_capacity(struct mmc *mmc, int part_num)
        return 0;
 }
 
+int mmc_select_hwpart(int dev_num, int hwpart)
+{
+       struct mmc *mmc = find_mmc_device(dev_num);
+       int ret;
+
+       if (!mmc)
+               return -1;
+
+       if (mmc->part_num == hwpart)
+               return 0;
+
+       if (mmc->part_config == MMCPART_NOAVAILABLE) {
+               printf("Card doesn't support part_switch\n");
+               return -1;
+       }
+
+       ret = mmc_switch_part(dev_num, hwpart);
+       if (ret)
+               return -1;
+
+       mmc->part_num = hwpart;
+
+       return 0;
+}
+
+
 int mmc_switch_part(int dev_num, unsigned int part_num)
 {
        struct mmc *mmc = find_mmc_device(dev_num);
@@ -578,8 +608,8 @@ int mmc_getcd(struct mmc *mmc)
        cd = board_mmc_getcd(mmc);
 
        if (cd < 0) {
-               if (mmc->ops->getcd)
-                       cd = mmc->ops->getcd(mmc);
+               if (mmc->cfg->ops->getcd)
+                       cd = mmc->cfg->ops->getcd(mmc);
                else
                        cd = 1;
        }
@@ -703,8 +733,8 @@ retry_scr:
         * This can avoid furthur problem when the card runs in different
         * mode between the host.
         */
-       if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
-               (mmc->host_caps & MMC_MODE_HS)))
+       if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
+               (mmc->cfg->host_caps & MMC_MODE_HS)))
                return 0;
 
        err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
@@ -751,17 +781,17 @@ static const int multipliers[] = {
 
 static void mmc_set_ios(struct mmc *mmc)
 {
-       if (mmc->ops->set_ios)
-               mmc->ops->set_ios(mmc);
+       if (mmc->cfg->ops->set_ios)
+               mmc->cfg->ops->set_ios(mmc);
 }
 
 void mmc_set_clock(struct mmc *mmc, uint clock)
 {
-       if (clock > mmc->f_max)
-               clock = mmc->f_max;
+       if (clock > mmc->cfg->f_max)
+               clock = mmc->cfg->f_max;
 
-       if (clock < mmc->f_min)
-               clock = mmc->f_min;
+       if (clock < mmc->cfg->f_min)
+               clock = mmc->cfg->f_min;
 
        mmc->clock = clock;
 
@@ -1027,7 +1057,7 @@ static int mmc_startup(struct mmc *mmc)
                return err;
 
        /* Restrict card's capabilities by what the host can do */
-       mmc->card_caps &= mmc->host_caps;
+       mmc->card_caps &= mmc->cfg->host_caps;
 
        if (IS_SD(mmc)) {
                if (mmc->card_caps & MMC_MODE_4BIT) {
@@ -1058,6 +1088,8 @@ static int mmc_startup(struct mmc *mmc)
 
                /* An array of possible bus widths in order of preference */
                static unsigned ext_csd_bits[] = {
+                       EXT_CSD_DDR_BUS_WIDTH_8,
+                       EXT_CSD_DDR_BUS_WIDTH_4,
                        EXT_CSD_BUS_WIDTH_8,
                        EXT_CSD_BUS_WIDTH_4,
                        EXT_CSD_BUS_WIDTH_1,
@@ -1065,13 +1097,15 @@ static int mmc_startup(struct mmc *mmc)
 
                /* An array to map CSD bus widths to host cap bits */
                static unsigned ext_to_hostcaps[] = {
+                       [EXT_CSD_DDR_BUS_WIDTH_4] = MMC_MODE_DDR_52MHz,
+                       [EXT_CSD_DDR_BUS_WIDTH_8] = MMC_MODE_DDR_52MHz,
                        [EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
                        [EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
                };
 
                /* An array to map chosen bus width to an integer */
                static unsigned widths[] = {
-                       8, 4, 1,
+                       8, 4, 8, 4, 1,
                };
 
                for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
@@ -1082,7 +1116,7 @@ static int mmc_startup(struct mmc *mmc)
                         * this bus width, if it's more than 1
                         */
                        if (extw != EXT_CSD_BUS_WIDTH_1 &&
-                                       !(mmc->host_caps & ext_to_hostcaps[extw]))
+                                       !(mmc->cfg->host_caps & ext_to_hostcaps[extw]))
                                continue;
 
                        err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
@@ -1155,7 +1189,7 @@ static int mmc_send_if_cond(struct mmc *mmc)
 
        cmd.cmdidx = SD_CMD_SEND_IF_COND;
        /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
-       cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
+       cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
        cmd.resp_type = MMC_RSP_R7;
 
        err = mmc_send_cmd(mmc, &cmd, NULL);
@@ -1171,8 +1205,33 @@ static int mmc_send_if_cond(struct mmc *mmc)
        return 0;
 }
 
-int mmc_register(struct mmc *mmc)
+/* not used any more */
+int __deprecated mmc_register(struct mmc *mmc)
 {
+#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
+       printf("%s is deprecated! use mmc_create() instead.\n", __func__);
+#endif
+       return -1;
+}
+
+struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
+{
+       struct mmc *mmc;
+
+       /* quick validation */
+       if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
+                       cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
+               return NULL;
+
+       mmc = calloc(1, sizeof(*mmc));
+       if (mmc == NULL)
+               return NULL;
+
+       mmc->cfg = cfg;
+       mmc->priv = priv;
+
+       /* the following chunk was mmc_register() */
+
        /* Setup dsr related values */
        mmc->dsr_imp = 0;
        mmc->dsr = 0xffffffff;
@@ -1183,14 +1242,21 @@ int mmc_register(struct mmc *mmc)
        mmc->block_dev.block_read = mmc_bread;
        mmc->block_dev.block_write = mmc_bwrite;
        mmc->block_dev.block_erase = mmc_berase;
-       if (!mmc->b_max)
-               mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
 
-       INIT_LIST_HEAD (&mmc->link);
+       /* setup initial part type */
+       mmc->block_dev.part_type = mmc->cfg->part_type;
 
-       list_add_tail (&mmc->link, &mmc_devices);
+       INIT_LIST_HEAD(&mmc->link);
 
-       return 0;
+       list_add_tail(&mmc->link, &mmc_devices);
+
+       return mmc;
+}
+
+void mmc_destroy(struct mmc *mmc)
+{
+       /* only freeing memory for now */
+       free(mmc);
 }
 
 #ifdef CONFIG_PARTITIONS
@@ -1209,7 +1275,7 @@ int mmc_start_init(struct mmc *mmc)
        int err;
 
        /* we pretend there's no card when init is NULL */
-       if (mmc_getcd(mmc) == 0 || mmc->ops->init == NULL) {
+       if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
                mmc->has_init = 0;
 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
                printf("MMC: no card present\n");
@@ -1221,7 +1287,7 @@ int mmc_start_init(struct mmc *mmc)
                return 0;
 
        /* made sure it's not NULL earlier */
-       err = mmc->ops->init(mmc);
+       err = mmc->cfg->ops->init(mmc);
 
        if (err)
                return err;
@@ -1282,10 +1348,13 @@ static int mmc_complete_init(struct mmc *mmc)
 int mmc_init(struct mmc *mmc)
 {
        int err = IN_PROGRESS;
-       unsigned start = get_timer(0);
+       unsigned start;
 
        if (mmc->has_init)
                return 0;
+
+       start = get_timer(0);
+
        if (!mmc->init_in_progress)
                err = mmc_start_init(mmc);
 
@@ -1323,7 +1392,7 @@ void print_mmc_devices(char separator)
        list_for_each(entry, &mmc_devices) {
                m = list_entry(entry, struct mmc, link);
 
-               printf("%s: %d", m->name, m->block_dev.dev);
+               printf("%s: %d", m->cfg->name, m->block_dev.dev);
 
                if (entry->next != &mmc_devices)
                        printf("%c ", separator);
@@ -1485,4 +1554,16 @@ int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
                return err;
        return 0;
 }
+
+/*
+ * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
+ * for enable.  Note that this is a write-once field for non-zero values.
+ *
+ * Returns 0 on success.
+ */
+int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
+{
+       return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,
+                         enable);
+}
 #endif