Merge tag 'r8169-20060920-00' of git://electric-eye.fr.zoreil.com/home/romieu/linux...
[pandora-kernel.git] / drivers / mmc / imxmmc.c
index 79358e2..fb6565b 100644 (file)
@@ -25,7 +25,6 @@
  *             deficiencies
  *
  */
-#include <linux/config.h>
 
 #ifdef CONFIG_MMC_DEBUG
 #define DEBUG
@@ -92,6 +91,8 @@ struct imxmci_host {
        int                     dma_allocated;
 
        unsigned char           actual_bus_width;
+
+       int                     prev_cmd_code;
 };
 
 #define IMXMCI_PEND_IRQ_b      0
@@ -218,15 +219,17 @@ static int imxmci_busy_wait_for_status(struct imxmci_host *host,
        if(!loops)
                return 0;
 
-       dev_info(mmc_dev(host->mmc), "busy wait for %d usec in %s, STATUS = 0x%x (0x%x)\n",
-               loops, where, *pstat, stat_mask);
+       /* The busy-wait is expected there for clock <8MHz due to SDHC hardware flaws */
+       if(!(stat_mask & STATUS_END_CMD_RESP) || (host->mmc->ios.clock>=8000000))
+               dev_info(mmc_dev(host->mmc), "busy wait for %d usec in %s, STATUS = 0x%x (0x%x)\n",
+                       loops, where, *pstat, stat_mask);
        return loops;
 }
 
 static void imxmci_setup_data(struct imxmci_host *host, struct mmc_data *data)
 {
        unsigned int nob = data->blocks;
-       unsigned int blksz = 1 << data->blksz_bits;
+       unsigned int blksz = data->blksz;
        unsigned int datasz = nob * blksz;
        int i;
 
@@ -247,16 +250,14 @@ static void imxmci_setup_data(struct imxmci_host *host, struct mmc_data *data)
         * partial FIFO fills and reads. The length has to be rounded up to burst size multiple.
         * This is required for SCR read at least.
         */
-       if (datasz < 64) {
+       if (datasz < 512) {
                host->dma_size = datasz;
                if (data->flags & MMC_DATA_READ) {
                        host->dma_dir = DMA_FROM_DEVICE;
 
                        /* Hack to enable read SCR */
-                       if(datasz < 16) {
-                               MMC_NOB = 1;
-                               MMC_BLK_LEN = 16;
-                       }
+                       MMC_NOB = 1;
+                       MMC_BLK_LEN = 512;
                } else {
                        host->dma_dir = DMA_TO_DEVICE;
                }
@@ -333,6 +334,9 @@ static void imxmci_start_cmd(struct imxmci_host *host, struct mmc_command *cmd,
        WARN_ON(host->cmd != NULL);
        host->cmd = cmd;
 
+       /* Ensure, that clock are stopped else command programming and start fails */
+       imxmci_stop_clock(host);
+
        if (cmd->flags & MMC_RSP_BUSY)
                cmdat |= CMD_DAT_CONT_BUSY;
 
@@ -405,6 +409,9 @@ static void imxmci_finish_request(struct imxmci_host *host, struct mmc_request *
 
        spin_unlock_irqrestore(&host->lock, flags);
 
+       if(req && req->cmd)
+               host->prev_cmd_code = req->cmd->opcode;
+
        host->req = NULL;
        host->cmd = NULL;
        host->data = NULL;
@@ -549,11 +556,10 @@ static int imxmci_cpu_driven_data(struct imxmci_host *host, unsigned int *pstat)
 {
        int i;
        int burst_len;
-       int flush_len;
        int trans_done = 0;
        unsigned int stat = *pstat;
 
-       if(host->actual_bus_width == MMC_BUS_WIDTH_4)
+       if(host->actual_bus_width != MMC_BUS_WIDTH_4)
                burst_len = 16;
        else
                burst_len = 64;
@@ -562,45 +568,43 @@ static int imxmci_cpu_driven_data(struct imxmci_host *host, unsigned int *pstat)
        dev_dbg(mmc_dev(host->mmc), "imxmci_cpu_driven_data running STATUS = 0x%x\n",
                stat);
 
+       udelay(20);     /* required for clocks < 8MHz*/
+
        if(host->dma_dir == DMA_FROM_DEVICE) {
                imxmci_busy_wait_for_status(host, &stat,
                                STATUS_APPL_BUFF_FF | STATUS_DATA_TRANS_DONE,
-                               20, "imxmci_cpu_driven_data read");
+                               50, "imxmci_cpu_driven_data read");
 
                while((stat & (STATUS_APPL_BUFF_FF |  STATUS_DATA_TRANS_DONE)) &&
-                     (host->data_cnt < host->dma_size)) {
-                       if(burst_len >= host->dma_size - host->data_cnt) {
-                               flush_len = burst_len;
-                               burst_len = host->dma_size - host->data_cnt;
-                               flush_len -= burst_len;
-                               host->data_cnt = host->dma_size;
-                               trans_done = 1;
-                       } else {
-                               flush_len = 0;
-                               host->data_cnt += burst_len;
-                       }
+                     (host->data_cnt < 512)) {
+
+                       udelay(20);     /* required for clocks < 8MHz*/
 
                        for(i = burst_len; i>=2 ; i-=2) {
-                               *(host->data_ptr++) = MMC_BUFFER_ACCESS;
-                               udelay(20);     /* required for clocks < 8MHz*/
+                               u16 data;
+                               data = MMC_BUFFER_ACCESS;
+                               udelay(10);     /* required for clocks < 8MHz*/
+                               if(host->data_cnt+2 <= host->dma_size) {
+                                       *(host->data_ptr++) = data;
+                               } else {
+                                       if(host->data_cnt < host->dma_size)
+                                               *(u8*)(host->data_ptr) = data;
+                               }
+                               host->data_cnt += 2;
                        }
 
-                       if(i == 1)
-                               *(u8*)(host->data_ptr) = MMC_BUFFER_ACCESS;
-
                        stat = MMC_STATUS;
 
-                       /* Flush extra bytes from FIFO */
-                       while(flush_len >= 2){
-                               flush_len -= 2;
-                               i = MMC_BUFFER_ACCESS;
-                               stat = MMC_STATUS;
-                               stat &= ~STATUS_CRC_READ_ERR; /* Stupid but required there */
-                       }
-
-                       dev_dbg(mmc_dev(host->mmc), "imxmci_cpu_driven_data read burst %d STATUS = 0x%x\n",
-                               burst_len, stat);
+                       dev_dbg(mmc_dev(host->mmc), "imxmci_cpu_driven_data read %d burst %d STATUS = 0x%x\n",
+                               host->data_cnt, burst_len, stat);
                }
+
+               if((stat & STATUS_DATA_TRANS_DONE) && (host->data_cnt >= 512))
+                       trans_done = 1;
+
+               if(host->dma_size & 0x1ff)
+                       stat &= ~STATUS_CRC_READ_ERR;
+
        } else {
                imxmci_busy_wait_for_status(host, &stat,
                                STATUS_APPL_BUFF_FE,
@@ -689,8 +693,8 @@ static void imxmci_tasklet_fnc(unsigned long data)
                       what, stat, MMC_INT_MASK);
                dev_err(mmc_dev(host->mmc), "CMD_DAT_CONT = 0x%04x, MMC_BLK_LEN = 0x%04x, MMC_NOB = 0x%04x, DMA_CCR = 0x%08x\n",
                       MMC_CMD_DAT_CONT, MMC_BLK_LEN, MMC_NOB, CCR(host->dma));
-               dev_err(mmc_dev(host->mmc), "CMD%d, bus %d-bit, dma_size = 0x%x\n",
-                      host->cmd?host->cmd->opcode:0, 1<<host->actual_bus_width, host->dma_size);
+               dev_err(mmc_dev(host->mmc), "CMD%d, prevCMD%d, bus %d-bit, dma_size = 0x%x\n",
+                      host->cmd?host->cmd->opcode:0, host->prev_cmd_code, 1<<host->actual_bus_width, host->dma_size);
        }
 
        if(!host->present || timeout)
@@ -746,10 +750,6 @@ static void imxmci_tasklet_fnc(unsigned long data)
                        data_dir_mask = STATUS_DATA_TRANS_DONE;
                }
 
-               imxmci_busy_wait_for_status(host, &stat,
-                               data_dir_mask,
-                               50, "imxmci_tasklet_fnc data");
-
                if(stat & data_dir_mask) {
                        clear_bit(IMXMCI_PEND_DMA_END_b, &host->pending_events);
                        imxmci_data_done(host, stat);
@@ -865,7 +865,11 @@ static void imxmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
 
                imxmci_stop_clock(host);
                MMC_CLK_RATE = (prescaler<<3) | clk;
-               imxmci_start_clock(host);
+               /*
+                * Under my understanding, clock should not be started there, because it would
+                * initiate SDHC sequencer and send last or random command into card
+                */
+               /*imxmci_start_clock(host);*/
 
                dev_dbg(mmc_dev(host->mmc), "MMC_CLK_RATE: 0x%08x\n", MMC_CLK_RATE);
        } else {