]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/mmc/mmc.c
mmc: remove the hard setting for tran_speed
[karo-tx-uboot.git] / drivers / mmc / mmc.c
index 6db37b1fc548a49be870d06f4737e5b799e6db76..9f746a304051297caf59aeb929bab89384a05689 100644 (file)
@@ -47,10 +47,105 @@ int __board_mmc_getcd(struct mmc *mmc) {
 int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
        alias("__board_mmc_getcd")));
 
+#ifdef CONFIG_MMC_BOUNCE_BUFFER
+static int mmc_bounce_need_bounce(struct mmc_data *orig)
+{
+       ulong addr, len;
+
+       if (orig->flags & MMC_DATA_READ)
+               addr = (ulong)orig->dest;
+       else
+               addr = (ulong)orig->src;
+
+       if (addr % ARCH_DMA_MINALIGN) {
+               debug("MMC: Unaligned data destination address %08lx!\n", addr);
+               return 1;
+       }
+
+       len = (ulong)(orig->blocksize * orig->blocks);
+       if (len % ARCH_DMA_MINALIGN) {
+               debug("MMC: Unaligned data destination length %08lx!\n", len);
+               return 1;
+       }
+
+       return 0;
+}
+
+static int mmc_bounce_buffer_start(struct mmc_data *backup,
+                                       struct mmc_data *orig)
+{
+       ulong origlen, len;
+       void *buffer;
+
+       if (!orig)
+               return 0;
+
+       if (!mmc_bounce_need_bounce(orig))
+               return 0;
+
+       memcpy(backup, orig, sizeof(struct mmc_data));
+
+       origlen = orig->blocksize * orig->blocks;
+       len = roundup(origlen, ARCH_DMA_MINALIGN);
+       buffer = memalign(ARCH_DMA_MINALIGN, len);
+       if (!buffer) {
+               puts("MMC: Error allocating MMC bounce buffer!\n");
+               return 1;
+       }
+
+       if (orig->flags & MMC_DATA_READ) {
+               orig->dest = buffer;
+       } else {
+               memcpy(buffer, orig->src, origlen);
+               orig->src = buffer;
+       }
+
+       return 0;
+}
+
+static void mmc_bounce_buffer_stop(struct mmc_data *backup,
+                                       struct mmc_data *orig)
+{
+       ulong len;
+
+       if (!orig)
+               return;
+
+       if (!mmc_bounce_need_bounce(backup))
+               return;
+
+       if (backup->flags & MMC_DATA_READ) {
+               len = backup->blocksize * backup->blocks;
+               memcpy(backup->dest, orig->dest, len);
+               free(orig->dest);
+               orig->dest = backup->dest;
+       } else {
+               free((void *)orig->src);
+               orig->src = backup->src;
+       }
+
+       return;
+
+}
+#else
+static inline int mmc_bounce_buffer_start(struct mmc_data *backup,
+                                       struct mmc_data *orig) { return 0; }
+static inline void mmc_bounce_buffer_stop(struct mmc_data *backup,
+                                       struct mmc_data *orig) { }
+#endif
+
 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
 {
-#ifdef CONFIG_MMC_TRACE
+       struct mmc_data backup;
        int ret;
+
+       memset(&backup, 0, sizeof(backup));
+
+       ret = mmc_bounce_buffer_start(&backup, data);
+       if (ret)
+               return ret;
+
+#ifdef CONFIG_MMC_TRACE
        int i;
        u8 *ptr;
 
@@ -84,7 +179,7 @@ int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
                        for (i = 0; i < 4; i++) {
                                int j;
                                printf("\t\t\t\t\t%03d - ", i*4);
-                               ptr = &cmd->response[i];
+                               ptr = (u8 *)&cmd->response[i];
                                ptr += 3;
                                for (j = 0; j < 4; j++)
                                        printf("%02X ", *ptr--);
@@ -99,16 +194,17 @@ int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
                        printf("\t\tERROR MMC rsp not supported\n");
                        break;
        }
-       return ret;
 #else
-       return mmc->send_cmd(mmc, cmd, data);
+       ret = mmc->send_cmd(mmc, cmd, data);
 #endif
+       mmc_bounce_buffer_stop(&backup, data);
+       return ret;
 }
 
 int mmc_send_status(struct mmc *mmc, int timeout)
 {
        struct mmc_cmd cmd;
-       int err;
+       int err, retries = 5;
 #ifdef CONFIG_MMC_TRACE
        int status;
 #endif
@@ -121,17 +217,21 @@ int mmc_send_status(struct mmc *mmc, int timeout)
 
        do {
                err = mmc_send_cmd(mmc, &cmd, NULL);
-               if (err)
+               if (!err) {
+                       if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
+                           (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
+                            MMC_STATE_PRG)
+                               break;
+                       else if (cmd.response[0] & MMC_STATUS_MASK) {
+                               printf("Status Error: 0x%08X\n",
+                                       cmd.response[0]);
+                               return COMM_ERR;
+                       }
+               } else if (--retries < 0)
                        return err;
-               else if (cmd.response[0] & MMC_STATUS_RDY_FOR_DATA)
-                       break;
 
                udelay(1000);
 
-               if (cmd.response[0] & MMC_STATUS_MASK) {
-                       printf("Status Error: 0x%08X\n", cmd.response[0]);
-                       return COMM_ERR;
-               }
        } while (timeout--);
 
 #ifdef CONFIG_MMC_TRACE
@@ -305,11 +405,12 @@ mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
                        printf("mmc fail to send stop cmd\n");
                        return 0;
                }
-
-               /* Waiting for the ready status */
-               mmc_send_status(mmc, timeout);
        }
 
+       /* Waiting for the ready status */
+       if (mmc_send_status(mmc, timeout))
+               return 0;
+
        return blkcnt;
 }
 
@@ -341,7 +442,6 @@ int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
 {
        struct mmc_cmd cmd;
        struct mmc_data data;
-       int timeout = 1000;
 
        if (blkcnt > 1)
                cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
@@ -373,9 +473,6 @@ int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
                        printf("mmc fail to send stop cmd\n");
                        return 0;
                }
-
-               /* Waiting for the ready status */
-               mmc_send_status(mmc, timeout);
        }
 
        return blkcnt;
@@ -610,7 +707,8 @@ int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
        ret = mmc_send_cmd(mmc, &cmd, NULL);
 
        /* Waiting for the ready status */
-       mmc_send_status(mmc, timeout);
+       if (!ret)
+               ret = mmc_send_status(mmc, timeout);
 
        return ret;
 
@@ -1097,11 +1195,13 @@ int mmc_startup(struct mmc *mmc)
                }
 
                if (mmc->card_caps & MMC_MODE_HS)
-                       mmc_set_clock(mmc, 50000000);
+                       mmc->tran_speed = 50000000;
                else
-                       mmc_set_clock(mmc, 25000000);
+                       mmc->tran_speed = 25000000;
        } else {
-               for (width = EXT_CSD_BUS_WIDTH_8; width >= 0; width--) {
+               width = ((mmc->host_caps & MMC_MODE_MASK_WIDTH_BITS) >>
+                        MMC_MODE_WIDTH_BITS_SHIFT);
+               for (; width >= 0; width--) {
                        /* Set the card to use 4 bit*/
                        err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
                                        EXT_CSD_BUS_WIDTH, width);
@@ -1134,13 +1234,14 @@ int mmc_startup(struct mmc *mmc)
 
                if (mmc->card_caps & MMC_MODE_HS) {
                        if (mmc->card_caps & MMC_MODE_HS_52MHz)
-                               mmc_set_clock(mmc, 52000000);
+                               mmc->tran_speed = 52000000;
                        else
-                               mmc_set_clock(mmc, 26000000);
-               } else
-                       mmc_set_clock(mmc, 20000000);
+                               mmc->tran_speed = 26000000;
+               }
        }
 
+       mmc_set_clock(mmc, mmc->tran_speed);
+
        /* fill in device description */
        mmc->block_dev.lun = 0;
        mmc->block_dev.type = 0;