linux/drivers/staging/mt7621-mmc/sd.c
<<
>>
Prefs
   1/* Copyright Statement:
   2 *
   3 * This software/firmware and related documentation ("MediaTek Software") are
   4 * protected under relevant copyright laws. The information contained herein
   5 * is confidential and proprietary to MediaTek Inc. and/or its licensors.
   6 * Without the prior written permission of MediaTek inc. and/or its licensors,
   7 * any reproduction, modification, use or disclosure of MediaTek Software,
   8 * and information contained herein, in whole or in part, shall be strictly prohibited.
   9 *
  10 * MediaTek Inc. (C) 2010. All rights reserved.
  11 *
  12 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14 * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15 * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18 * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19 * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20 * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21 * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22 * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23 * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24 * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26 * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27 * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28 * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29 * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30 *
  31 * The following software/firmware and/or related documentation ("MediaTek Software")
  32 * have been modified by MediaTek Inc. All revisions are subject to any receiver's
  33 * applicable license agreements with MediaTek Inc.
  34 */
  35
  36#include <linux/module.h>
  37#include <linux/moduleparam.h>
  38#include <linux/init.h>
  39#include <linux/spinlock.h>
  40#include <linux/timer.h>
  41#include <linux/ioport.h>
  42#include <linux/device.h>
  43#include <linux/platform_device.h>
  44#include <linux/interrupt.h>
  45#include <linux/delay.h>
  46#include <linux/blkdev.h>
  47#include <linux/slab.h>
  48#include <linux/mmc/host.h>
  49#include <linux/mmc/card.h>
  50#include <linux/mmc/core.h>
  51#include <linux/mmc/mmc.h>
  52#include <linux/mmc/sd.h>
  53#include <linux/mmc/sdio.h>
  54#include <linux/dma-mapping.h>
  55
  56/* +++ by chhung */
  57#include <linux/types.h>
  58#include <linux/kernel.h>
  59#include <linux/version.h>
  60#include <linux/pm.h>
  61#include <linux/of.h>
  62
  63#define MSDC_SMPL_FALLING   (1)
  64#define MSDC_CD_PIN_EN      (1 << 0)  /* card detection pin is wired   */
  65#define MSDC_WP_PIN_EN      (1 << 1)  /* write protection pin is wired */
  66#define MSDC_REMOVABLE      (1 << 5)  /* removable slot                */
  67#define MSDC_SYS_SUSPEND    (1 << 6)  /* suspended by system           */
  68#define MSDC_HIGHSPEED      (1 << 7)
  69
  70//#define IRQ_SDC 14    //MT7620 /*FIXME*/
  71#ifdef CONFIG_SOC_MT7621
  72#define RALINK_SYSCTL_BASE              0xbe000000
  73#define RALINK_MSDC_BASE                0xbe130000
  74#else
  75#define RALINK_SYSCTL_BASE              0xb0000000
  76#define RALINK_MSDC_BASE                0xb0130000
  77#endif
  78#define IRQ_SDC                 22      /*FIXME*/
  79
  80#include <asm/dma.h>
  81/* end of +++ */
  82
  83
  84#include <asm/mach-ralink/ralink_regs.h>
  85
  86#if 0 /* --- by chhung */
  87#include <mach/board.h>
  88#include <mach/mt6575_devs.h>
  89#include <mach/mt6575_typedefs.h>
  90#include <mach/mt6575_clock_manager.h>
  91#include <mach/mt6575_pm_ldo.h>
  92//#include <mach/mt6575_pll.h>
  93//#include <mach/mt6575_gpio.h>
  94//#include <mach/mt6575_gpt_sw.h>
  95#include <asm/tcm.h>
  96// #include <mach/mt6575_gpt.h>
  97#endif /* end of --- */
  98
  99#include "mt6575_sd.h"
 100#include "dbg.h"
 101
 102/* +++ by chhung */
 103#include "board.h"
 104/* end of +++ */
 105
 106#if 0 /* --- by chhung */
 107#define isb() __asm__ __volatile__ ("" : : : "memory")
 108#define dsb() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 4" \
 109                                    : : "r" (0) : "memory")
 110#define dmb() __asm__ __volatile__ ("" : : : "memory")
 111#endif /* end of --- */
 112
 113#define DRV_NAME            "mtk-sd"
 114
 115#define HOST_MAX_NUM        (1) /* +/- by chhung */
 116
 117#if defined (CONFIG_SOC_MT7620)
 118#define HOST_MAX_MCLK       (48000000) /* +/- by chhung */
 119#elif defined (CONFIG_SOC_MT7621)
 120#define HOST_MAX_MCLK       (50000000) /* +/- by chhung */
 121#endif
 122#define HOST_MIN_MCLK       (260000)
 123
 124#define HOST_MAX_BLKSZ      (2048)
 125
 126#define MSDC_OCR_AVAIL      (MMC_VDD_28_29 | MMC_VDD_29_30 | MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33)
 127
 128#define GPIO_PULL_DOWN      (0)
 129#define GPIO_PULL_UP        (1)
 130
 131#if 0 /* --- by chhung */
 132#define MSDC_CLKSRC_REG     (0xf100000C)
 133#define PDN_REG           (0xF1000010) 
 134#endif /* end of --- */
 135
 136#define DEFAULT_DEBOUNCE    (8)       /* 8 cycles */
 137#define DEFAULT_DTOC        (40)      /* data timeout counter. 65536x40 sclk. */
 138
 139#define CMD_TIMEOUT         (HZ/10)     /* 100ms */
 140#define DAT_TIMEOUT         (HZ/2 * 5)  /* 500ms x5 */
 141
 142#define MAX_DMA_CNT         (64 * 1024 - 512)   /* a single transaction for WIFI may be 50K*/
 143
 144#define MAX_GPD_NUM         (1 + 1)  /* one null gpd */
 145#define MAX_BD_NUM          (1024)
 146#define MAX_BD_PER_GPD      (MAX_BD_NUM)
 147
 148#define MAX_HW_SGMTS        (MAX_BD_NUM)
 149#define MAX_PHY_SGMTS       (MAX_BD_NUM)
 150#define MAX_SGMT_SZ         (MAX_DMA_CNT)
 151#define MAX_REQ_SZ          (MAX_SGMT_SZ * 8)  
 152
 153#ifdef MT6575_SD_DEBUG
 154static struct msdc_regs *msdc_reg[HOST_MAX_NUM];
 155#endif 
 156
 157static int mtk_sw_poll;
 158
 159static int cd_active_low = 1;
 160
 161//=================================
 162#define PERI_MSDC0_PDN      (15)
 163//#define PERI_MSDC1_PDN    (16)
 164//#define PERI_MSDC2_PDN    (17)
 165//#define PERI_MSDC3_PDN    (18)
 166
 167struct msdc_host *msdc_6575_host[] = {NULL,NULL,NULL,NULL};
 168#if 0 /* --- by chhung */
 169/* gate means clock power down */
 170static int g_clk_gate = 0; 
 171#define msdc_gate_clock(id) \
 172    do { \
 173        g_clk_gate &= ~(1 << ((id) + PERI_MSDC0_PDN)); \
 174    } while(0)
 175/* not like power down register. 1 means clock on. */
 176#define msdc_ungate_clock(id) \
 177    do { \
 178        g_clk_gate |= 1 << ((id) + PERI_MSDC0_PDN); \
 179    } while(0)
 180
 181// do we need sync object or not 
 182void msdc_clk_status(int * status)
 183{
 184    *status = g_clk_gate;       
 185}
 186#endif /* end of --- */
 187
 188/* +++ by chhung */
 189struct msdc_hw msdc0_hw = {
 190        .clk_src        = 0,
 191        .cmd_edge       = MSDC_SMPL_FALLING,
 192        .data_edge      = MSDC_SMPL_FALLING,
 193        .clk_drv        = 4,
 194        .cmd_drv        = 4,
 195        .dat_drv        = 4,
 196        .data_pins      = 4,
 197        .data_offset    = 0,
 198        .flags          = MSDC_SYS_SUSPEND | MSDC_CD_PIN_EN | MSDC_REMOVABLE | MSDC_HIGHSPEED,
 199//      .flags          = MSDC_SYS_SUSPEND | MSDC_WP_PIN_EN | MSDC_CD_PIN_EN | MSDC_REMOVABLE,
 200};
 201
 202static struct resource mtk_sd_resources[] = {
 203        [0] = {
 204                .start  = RALINK_MSDC_BASE,
 205                .end    = RALINK_MSDC_BASE+0x3fff,
 206                .flags  = IORESOURCE_MEM,
 207        },
 208        [1] = {
 209                .start  = IRQ_SDC,      /*FIXME*/
 210                .end    = IRQ_SDC,      /*FIXME*/
 211                .flags  = IORESOURCE_IRQ,
 212        },
 213};
 214
 215static struct platform_device mtk_sd_device = {
 216        .name           = "mtk-sd",
 217        .id             = 0,
 218        .num_resources  = ARRAY_SIZE(mtk_sd_resources),
 219        .resource       = mtk_sd_resources,
 220};
 221/* end of +++ */
 222
 223static int msdc_rsp[] = {
 224    0,  /* RESP_NONE */
 225    1,  /* RESP_R1 */
 226    2,  /* RESP_R2 */
 227    3,  /* RESP_R3 */
 228    4,  /* RESP_R4 */
 229    1,  /* RESP_R5 */
 230    1,  /* RESP_R6 */
 231    1,  /* RESP_R7 */
 232    7,  /* RESP_R1b */
 233};
 234
 235/* For Inhanced DMA */
 236#define msdc_init_gpd_ex(gpd,extlen,cmd,arg,blknum) \
 237    do { \
 238        ((gpd_t*)gpd)->extlen = extlen; \
 239        ((gpd_t*)gpd)->cmd    = cmd; \
 240        ((gpd_t*)gpd)->arg    = arg; \
 241        ((gpd_t*)gpd)->blknum = blknum; \
 242    }while(0)
 243    
 244#define msdc_init_bd(bd, blkpad, dwpad, dptr, dlen) \
 245    do { \
 246        BUG_ON(dlen > 0xFFFFUL); \
 247        ((bd_t*)bd)->blkpad = blkpad; \
 248        ((bd_t*)bd)->dwpad  = dwpad; \
 249        ((bd_t*)bd)->ptr    = (void*)dptr; \
 250        ((bd_t*)bd)->buflen = dlen; \
 251    }while(0)
 252
 253#define msdc_txfifocnt()   ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16)
 254#define msdc_rxfifocnt()   ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) >> 0)
 255#define msdc_fifo_write32(v)   sdr_write32(MSDC_TXDATA, (v))
 256#define msdc_fifo_write8(v)    sdr_write8(MSDC_TXDATA, (v))
 257#define msdc_fifo_read32()   sdr_read32(MSDC_RXDATA)
 258#define msdc_fifo_read8()    sdr_read8(MSDC_RXDATA)     
 259
 260
 261#define msdc_dma_on()        sdr_clr_bits(MSDC_CFG, MSDC_CFG_PIO)
 262#define msdc_dma_off()       sdr_set_bits(MSDC_CFG, MSDC_CFG_PIO)
 263
 264#define msdc_retry(expr,retry,cnt) \
 265    do { \
 266        int backup = cnt; \
 267        while (retry) { \
 268            if (!(expr)) break; \
 269            if (cnt-- == 0) { \
 270                retry--; mdelay(1); cnt = backup; \
 271            } \
 272        } \
 273        WARN_ON(retry == 0); \
 274    } while(0)
 275
 276#if 0 /* --- by chhung */
 277#define msdc_reset() \
 278    do { \
 279        int retry = 3, cnt = 1000; \
 280        sdr_set_bits(MSDC_CFG, MSDC_CFG_RST); \
 281        dsb(); \
 282        msdc_retry(sdr_read32(MSDC_CFG) & MSDC_CFG_RST, retry, cnt); \
 283    } while(0)
 284#else
 285#define msdc_reset() \
 286    do { \
 287        int retry = 3, cnt = 1000; \
 288        sdr_set_bits(MSDC_CFG, MSDC_CFG_RST); \
 289        msdc_retry(sdr_read32(MSDC_CFG) & MSDC_CFG_RST, retry, cnt); \
 290    } while(0)
 291#endif /* end of +/- */
 292
 293#define msdc_clr_int() \
 294    do { \
 295        volatile u32 val = sdr_read32(MSDC_INT); \
 296        sdr_write32(MSDC_INT, val); \
 297    } while(0)
 298
 299#define msdc_clr_fifo() \
 300    do { \
 301        int retry = 3, cnt = 1000; \
 302        sdr_set_bits(MSDC_FIFOCS, MSDC_FIFOCS_CLR); \
 303        msdc_retry(sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_CLR, retry, cnt); \
 304    } while(0)
 305
 306#define msdc_irq_save(val) \
 307    do { \
 308        val = sdr_read32(MSDC_INTEN); \
 309        sdr_clr_bits(MSDC_INTEN, val); \
 310    } while(0)
 311        
 312#define msdc_irq_restore(val) \
 313    do { \
 314        sdr_set_bits(MSDC_INTEN, val); \
 315    } while(0)
 316
 317/* clock source for host: global */
 318#if defined (CONFIG_SOC_MT7620)
 319static u32 hclks[] = {48000000}; /* +/- by chhung */
 320#elif defined (CONFIG_SOC_MT7621)
 321static u32 hclks[] = {50000000}; /* +/- by chhung */
 322#endif
 323
 324//============================================
 325// the power for msdc host controller: global
 326//    always keep the VMC on. 
 327//============================================
 328#define msdc_vcore_on(host) \
 329    do { \
 330        INIT_MSG("[+]VMC ref. count<%d>", ++host->pwr_ref); \
 331        (void)hwPowerOn(MT65XX_POWER_LDO_VMC, VOL_3300, "SD"); \
 332    } while (0)
 333#define msdc_vcore_off(host) \
 334    do { \
 335        INIT_MSG("[-]VMC ref. count<%d>", --host->pwr_ref); \
 336        (void)hwPowerDown(MT65XX_POWER_LDO_VMC, "SD"); \
 337    } while (0)
 338
 339//====================================
 340// the vdd output for card: global 
 341//   always keep the VMCH on. 
 342//==================================== 
 343#define msdc_vdd_on(host) \
 344    do { \
 345        (void)hwPowerOn(MT65XX_POWER_LDO_VMCH, VOL_3300, "SD"); \
 346    } while (0)   
 347#define msdc_vdd_off(host) \
 348    do { \
 349        (void)hwPowerDown(MT65XX_POWER_LDO_VMCH, "SD"); \
 350    } while (0)      
 351
 352#define sdc_is_busy()          (sdr_read32(SDC_STS) & SDC_STS_SDCBUSY)
 353#define sdc_is_cmd_busy()      (sdr_read32(SDC_STS) & SDC_STS_CMDBUSY)
 354
 355#define sdc_send_cmd(cmd,arg) \
 356    do { \
 357        sdr_write32(SDC_ARG, (arg)); \
 358        sdr_write32(SDC_CMD, (cmd)); \
 359    } while(0)
 360
 361// can modify to read h/w register.
 362//#define is_card_present(h)   ((sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 0 : 1);
 363#define is_card_present(h)     (((struct msdc_host*)(h))->card_inserted)
 364
 365/* +++ by chhung */
 366#ifndef __ASSEMBLY__
 367#define PHYSADDR(a)             (((unsigned long)(a)) & 0x1fffffff)
 368#else
 369#define PHYSADDR(a)             ((a) & 0x1fffffff)
 370#endif
 371/* end of +++ */
 372static unsigned int msdc_do_command(struct msdc_host   *host, 
 373                                      struct mmc_command *cmd,
 374                                      int                 tune,
 375                                      unsigned long       timeout);  
 376                                     
 377static int msdc_tune_cmdrsp(struct msdc_host*host,struct mmc_command *cmd);
 378
 379#ifdef MT6575_SD_DEBUG
 380static void msdc_dump_card_status(struct msdc_host *host, u32 status)
 381{
 382/* N_MSG is currently a no-op */
 383#if 0
 384    static char *state[] = {
 385        "Idle",                 /* 0 */
 386        "Ready",                /* 1 */
 387        "Ident",                /* 2 */
 388        "Stby",                 /* 3 */
 389        "Tran",                 /* 4 */
 390        "Data",                 /* 5 */
 391        "Rcv",                  /* 6 */
 392        "Prg",                  /* 7 */
 393        "Dis",                  /* 8 */
 394        "Reserved",             /* 9 */
 395        "Reserved",             /* 10 */
 396        "Reserved",             /* 11 */
 397        "Reserved",             /* 12 */
 398        "Reserved",             /* 13 */
 399        "Reserved",             /* 14 */
 400        "I/O mode",             /* 15 */
 401    };
 402#endif
 403    if (status & R1_OUT_OF_RANGE)
 404        N_MSG(RSP, "[CARD_STATUS] Out of Range");
 405    if (status & R1_ADDRESS_ERROR)
 406        N_MSG(RSP, "[CARD_STATUS] Address Error");
 407    if (status & R1_BLOCK_LEN_ERROR)
 408        N_MSG(RSP, "[CARD_STATUS] Block Len Error");
 409    if (status & R1_ERASE_SEQ_ERROR)
 410        N_MSG(RSP, "[CARD_STATUS] Erase Seq Error");
 411    if (status & R1_ERASE_PARAM)
 412        N_MSG(RSP, "[CARD_STATUS] Erase Param");
 413    if (status & R1_WP_VIOLATION)
 414        N_MSG(RSP, "[CARD_STATUS] WP Violation");
 415    if (status & R1_CARD_IS_LOCKED)
 416        N_MSG(RSP, "[CARD_STATUS] Card is Locked");
 417    if (status & R1_LOCK_UNLOCK_FAILED)
 418        N_MSG(RSP, "[CARD_STATUS] Lock/Unlock Failed");
 419    if (status & R1_COM_CRC_ERROR)
 420        N_MSG(RSP, "[CARD_STATUS] Command CRC Error");
 421    if (status & R1_ILLEGAL_COMMAND)
 422        N_MSG(RSP, "[CARD_STATUS] Illegal Command");
 423    if (status & R1_CARD_ECC_FAILED)
 424        N_MSG(RSP, "[CARD_STATUS] Card ECC Failed");
 425    if (status & R1_CC_ERROR)
 426        N_MSG(RSP, "[CARD_STATUS] CC Error");
 427    if (status & R1_ERROR)
 428        N_MSG(RSP, "[CARD_STATUS] Error");
 429    if (status & R1_UNDERRUN)
 430        N_MSG(RSP, "[CARD_STATUS] Underrun");
 431    if (status & R1_OVERRUN)
 432        N_MSG(RSP, "[CARD_STATUS] Overrun");
 433    if (status & R1_CID_CSD_OVERWRITE)
 434        N_MSG(RSP, "[CARD_STATUS] CID/CSD Overwrite");
 435    if (status & R1_WP_ERASE_SKIP)
 436        N_MSG(RSP, "[CARD_STATUS] WP Eraser Skip");
 437    if (status & R1_CARD_ECC_DISABLED)
 438        N_MSG(RSP, "[CARD_STATUS] Card ECC Disabled");
 439    if (status & R1_ERASE_RESET)
 440        N_MSG(RSP, "[CARD_STATUS] Erase Reset");
 441    if (status & R1_READY_FOR_DATA)
 442        N_MSG(RSP, "[CARD_STATUS] Ready for Data");
 443    if (status & R1_SWITCH_ERROR)
 444        N_MSG(RSP, "[CARD_STATUS] Switch error");
 445    if (status & R1_APP_CMD)
 446        N_MSG(RSP, "[CARD_STATUS] App Command");
 447    
 448    N_MSG(RSP, "[CARD_STATUS] '%s' State", state[R1_CURRENT_STATE(status)]);
 449}
 450
 451static void msdc_dump_ocr_reg(struct msdc_host *host, u32 resp)
 452{
 453    if (resp & (1 << 7))
 454        N_MSG(RSP, "[OCR] Low Voltage Range");
 455    if (resp & (1 << 15))
 456        N_MSG(RSP, "[OCR] 2.7-2.8 volt");
 457    if (resp & (1 << 16))
 458        N_MSG(RSP, "[OCR] 2.8-2.9 volt");
 459    if (resp & (1 << 17))
 460        N_MSG(RSP, "[OCR] 2.9-3.0 volt");
 461    if (resp & (1 << 18))
 462        N_MSG(RSP, "[OCR] 3.0-3.1 volt");
 463    if (resp & (1 << 19))
 464        N_MSG(RSP, "[OCR] 3.1-3.2 volt");
 465    if (resp & (1 << 20))
 466        N_MSG(RSP, "[OCR] 3.2-3.3 volt");
 467    if (resp & (1 << 21))
 468        N_MSG(RSP, "[OCR] 3.3-3.4 volt");
 469    if (resp & (1 << 22))
 470        N_MSG(RSP, "[OCR] 3.4-3.5 volt");
 471    if (resp & (1 << 23))
 472        N_MSG(RSP, "[OCR] 3.5-3.6 volt");
 473    if (resp & (1 << 24))
 474        N_MSG(RSP, "[OCR] Switching to 1.8V Accepted (S18A)");
 475    if (resp & (1 << 30))
 476        N_MSG(RSP, "[OCR] Card Capacity Status (CCS)");
 477    if (resp & (1 << 31))
 478        N_MSG(RSP, "[OCR] Card Power Up Status (Idle)");
 479    else
 480        N_MSG(RSP, "[OCR] Card Power Up Status (Busy)");
 481}
 482
 483static void msdc_dump_rca_resp(struct msdc_host *host, u32 resp)
 484{
 485    u32 status = (((resp >> 15) & 0x1) << 23) |
 486                 (((resp >> 14) & 0x1) << 22) |
 487                 (((resp >> 13) & 0x1) << 19) |
 488                   (resp & 0x1fff);
 489    
 490    N_MSG(RSP, "[RCA] 0x%.4x", resp >> 16);
 491    msdc_dump_card_status(host, status);        
 492}
 493
 494static void msdc_dump_io_resp(struct msdc_host *host, u32 resp)
 495{
 496    u32 flags = (resp >> 8) & 0xFF;
 497#if 0
 498    char *state[] = {"DIS", "CMD", "TRN", "RFU"};
 499#endif
 500    if (flags & (1 << 7))
 501        N_MSG(RSP, "[IO] COM_CRC_ERR");
 502    if (flags & (1 << 6))
 503        N_MSG(RSP, "[IO] Illgal command");   
 504    if (flags & (1 << 3))
 505        N_MSG(RSP, "[IO] Error");
 506    if (flags & (1 << 2))
 507        N_MSG(RSP, "[IO] RFU");
 508    if (flags & (1 << 1))
 509        N_MSG(RSP, "[IO] Function number error");
 510    if (flags & (1 << 0))
 511        N_MSG(RSP, "[IO] Out of range");
 512
 513    N_MSG(RSP, "[IO] State: %s, Data:0x%x", state[(resp >> 12) & 0x3], resp & 0xFF);
 514}
 515#endif
 516
 517static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
 518{
 519    u32 base = host->base;
 520    u32 timeout, clk_ns;
 521
 522    host->timeout_ns   = ns;
 523    host->timeout_clks = clks;
 524
 525    clk_ns  = 1000000000UL / host->sclk;
 526    timeout = ns / clk_ns + clks;
 527    timeout = timeout >> 16; /* in 65536 sclk cycle unit */
 528    timeout = timeout > 1 ? timeout - 1 : 0;
 529    timeout = timeout > 255 ? 255 : timeout;
 530
 531    sdr_set_field(SDC_CFG, SDC_CFG_DTOC, timeout);
 532
 533    N_MSG(OPS, "Set read data timeout: %dns %dclks -> %d x 65536 cycles",
 534        ns, clks, timeout + 1);
 535}
 536
 537/* msdc_eirq_sdio() will be called when EIRQ(for WIFI) */
 538static void msdc_eirq_sdio(void *data)
 539{
 540    struct msdc_host *host = (struct msdc_host *)data;
 541
 542    N_MSG(INT, "SDIO EINT");
 543
 544    mmc_signal_sdio_irq(host->mmc);
 545}
 546
 547/* msdc_eirq_cd will not be used!  We not using EINT for card detection. */
 548static void msdc_eirq_cd(void *data)
 549{
 550    struct msdc_host *host = (struct msdc_host *)data;
 551
 552    N_MSG(INT, "CD EINT");
 553
 554#if 0
 555    tasklet_hi_schedule(&host->card_tasklet);
 556#else
 557    schedule_delayed_work(&host->card_delaywork, HZ);
 558#endif
 559}
 560
 561#if 0
 562static void msdc_tasklet_card(unsigned long arg)
 563{
 564    struct msdc_host *host = (struct msdc_host *)arg;
 565#else
 566static void msdc_tasklet_card(struct work_struct *work)
 567{
 568    struct msdc_host *host = (struct msdc_host *)container_of(work, 
 569                                struct msdc_host, card_delaywork.work);
 570#endif
 571    struct msdc_hw *hw = host->hw;
 572    u32 base = host->base;
 573    u32 inserted;       
 574    u32 status = 0;
 575    //u32 change = 0;
 576
 577    spin_lock(&host->lock);
 578
 579    if (hw->get_cd_status) { // NULL
 580        inserted = hw->get_cd_status();
 581    } else {
 582        status = sdr_read32(MSDC_PS);
 583        if (cd_active_low)
 584                inserted = (status & MSDC_PS_CDSTS) ? 0 : 1;
 585        else
 586                inserted = (status & MSDC_PS_CDSTS) ? 1 : 0;
 587    }
 588
 589#if 0
 590    change = host->card_inserted ^ inserted;
 591    host->card_inserted = inserted;
 592        
 593    if (change && !host->suspend) {
 594        if (inserted) {
 595            host->mmc->f_max = HOST_MAX_MCLK;  // work around           
 596        }       
 597        mmc_detect_change(host->mmc, msecs_to_jiffies(20));
 598    }
 599#else  /* Make sure: handle the last interrupt */
 600    host->card_inserted = inserted;    
 601    
 602    if (!host->suspend) {
 603        host->mmc->f_max = HOST_MAX_MCLK;       
 604        mmc_detect_change(host->mmc, msecs_to_jiffies(20));
 605    }   
 606    
 607    IRQ_MSG("card found<%s>", inserted ? "inserted" : "removed");       
 608#endif
 609
 610    spin_unlock(&host->lock);
 611}
 612
 613#if 0 /* --- by chhung */
 614/* For E2 only */
 615static u8 clk_src_bit[4] = {
 616   0, 3, 5, 7           
 617};
 618
 619static void msdc_select_clksrc(struct msdc_host* host, unsigned char clksrc)
 620{
 621    u32 val; 
 622    u32 base = host->base;
 623        
 624    BUG_ON(clksrc > 3); 
 625    INIT_MSG("set clock source to <%d>", clksrc);       
 626
 627    val = sdr_read32(MSDC_CLKSRC_REG);      
 628    if (sdr_read32(MSDC_ECO_VER) >= 4) {
 629        val &= ~(0x3  << clk_src_bit[host->id]); 
 630        val |= clksrc << clk_src_bit[host->id];                         
 631    } else {        
 632        val &= ~0x3; val |= clksrc;
 633    }    
 634    sdr_write32(MSDC_CLKSRC_REG, val);
 635            
 636    host->hclk = hclks[clksrc];     
 637    host->hw->clk_src = clksrc;
 638}
 639#endif /* end of --- */
 640
 641static void msdc_set_mclk(struct msdc_host *host, int ddr, unsigned int hz)
 642{
 643    //struct msdc_hw *hw = host->hw;
 644    u32 base = host->base;
 645    u32 mode;
 646    u32 flags;
 647    u32 div;
 648    u32 sclk;
 649    u32 hclk = host->hclk;
 650    //u8  clksrc = hw->clk_src;
 651
 652    if (!hz) { // set mmc system clock to 0 ?
 653        //ERR_MSG("set mclk to 0!!!");
 654        msdc_reset();
 655        return;
 656    }
 657
 658    msdc_irq_save(flags);
 659    
 660#if defined (CONFIG_MT7621_FPGA) || defined (CONFIG_MT7628_FPGA)
 661    mode = 0x0; /* use divisor */
 662    if (hz >= (hclk >> 1)) {
 663            div  = 0;         /* mean div = 1/2 */
 664            sclk = hclk >> 1; /* sclk = clk / 2 */
 665    } else {
 666            div  = (hclk + ((hz << 2) - 1)) / (hz << 2);
 667            sclk = (hclk >> 2) / div;
 668    }
 669#else
 670    if (ddr) {
 671        mode = 0x2; /* ddr mode and use divisor */
 672        if (hz >= (hclk >> 2)) {
 673                div  = 1;         /* mean div = 1/4 */
 674                sclk = hclk >> 2; /* sclk = clk / 4 */
 675        } else {
 676                div  = (hclk + ((hz << 2) - 1)) / (hz << 2);
 677                sclk = (hclk >> 2) / div;
 678        }
 679    } else if (hz >= hclk) { /* bug fix */
 680        mode = 0x1; /* no divisor and divisor is ignored */
 681        div  = 0;
 682        sclk = hclk; 
 683    } else {
 684        mode = 0x0; /* use divisor */
 685        if (hz >= (hclk >> 1)) {
 686                div  = 0;         /* mean div = 1/2 */
 687                sclk = hclk >> 1; /* sclk = clk / 2 */
 688        } else {
 689                div  = (hclk + ((hz << 2) - 1)) / (hz << 2);
 690                sclk = (hclk >> 2) / div;
 691        }
 692    }    
 693#endif
 694    /* set clock mode and divisor */
 695    sdr_set_field(MSDC_CFG, MSDC_CFG_CKMOD, mode);
 696    sdr_set_field(MSDC_CFG, MSDC_CFG_CKDIV, div);
 697   
 698    /* wait clock stable */
 699    while (!(sdr_read32(MSDC_CFG) & MSDC_CFG_CKSTB));
 700
 701    host->sclk = sclk;
 702    host->mclk = hz;
 703    msdc_set_timeout(host, host->timeout_ns, host->timeout_clks); // need?
 704     
 705    INIT_MSG("================");  
 706    INIT_MSG("!!! Set<%dKHz> Source<%dKHz> -> sclk<%dKHz>", hz/1000, hclk/1000, sclk/1000); 
 707    INIT_MSG("================");
 708
 709    msdc_irq_restore(flags);
 710}
 711
 712/* Fix me. when need to abort */
 713static void msdc_abort_data(struct msdc_host *host)
 714{
 715          u32 base = host->base;
 716          struct mmc_command *stop = host->mrq->stop;
 717
 718    ERR_MSG("Need to Abort. dma<%d>", host->dma_xfer);
 719    
 720    msdc_reset();
 721    msdc_clr_fifo();        
 722    msdc_clr_int();
 723
 724    // need to check FIFO count 0 ?
 725    
 726    if (stop) {  /* try to stop, but may not success */
 727        ERR_MSG("stop when abort CMD<%d>", stop->opcode);       
 728        (void)msdc_do_command(host, stop, 0, CMD_TIMEOUT);
 729    }
 730    
 731    //if (host->mclk >= 25000000) {
 732    //      msdc_set_mclk(host, 0, host->mclk >> 1);
 733    //}
 734}
 735
 736#if 0 /* --- by chhung */
 737static void msdc_pin_config(struct msdc_host *host, int mode)
 738{
 739    struct msdc_hw *hw = host->hw;
 740    u32 base = host->base;
 741    int pull = (mode == MSDC_PIN_PULL_UP) ? GPIO_PULL_UP : GPIO_PULL_DOWN;
 742
 743    /* Config WP pin */
 744    if (hw->flags & MSDC_WP_PIN_EN) {
 745        if (hw->config_gpio_pin) /* NULL */
 746            hw->config_gpio_pin(MSDC_WP_PIN, pull);
 747    }
 748
 749    switch (mode) {
 750    case MSDC_PIN_PULL_UP:
 751        //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 1); /* Check & FIXME */
 752        //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 0); /* Check & FIXME */
 753        sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 1);
 754        sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 0);
 755        sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 1);
 756        sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 0);
 757        break;
 758    case MSDC_PIN_PULL_DOWN:
 759        //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 0); /* Check & FIXME */
 760        //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 1); /* Check & FIXME */
 761        sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 0);
 762        sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 1);
 763        sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 0);
 764        sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 1);
 765        break;
 766    case MSDC_PIN_PULL_NONE:
 767    default:
 768        //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 0); /* Check & FIXME */
 769        //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 0); /* Check & FIXME */
 770        sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 0);
 771        sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 0);
 772        sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 0);
 773        sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 0);
 774        break;
 775    }
 776    
 777    N_MSG(CFG, "Pins mode(%d), down(%d), up(%d)", 
 778        mode, MSDC_PIN_PULL_DOWN, MSDC_PIN_PULL_UP);
 779}
 780
 781void msdc_pin_reset(struct msdc_host *host, int mode)
 782{
 783    struct msdc_hw *hw = (struct msdc_hw *)host->hw;
 784    u32 base = host->base;
 785    int pull = (mode == MSDC_PIN_PULL_UP) ? GPIO_PULL_UP : GPIO_PULL_DOWN;
 786
 787    /* Config reset pin */
 788    if (hw->flags & MSDC_RST_PIN_EN) {
 789        if (hw->config_gpio_pin) /* NULL */
 790            hw->config_gpio_pin(MSDC_RST_PIN, pull);
 791
 792        if (mode == MSDC_PIN_PULL_UP) {
 793            sdr_clr_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
 794        } else {
 795            sdr_set_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
 796        }
 797    }
 798}
 799
 800static void msdc_core_power(struct msdc_host *host, int on)
 801{
 802    N_MSG(CFG, "Turn %s %s power (copower: %d -> %d)", 
 803        on ? "on" : "off", "core", host->core_power, on);
 804
 805    if (on && host->core_power == 0) {
 806        msdc_vcore_on(host);
 807        host->core_power = 1;
 808        msleep(1);
 809    } else if (!on && host->core_power == 1) {
 810        msdc_vcore_off(host);
 811        host->core_power = 0;
 812        msleep(1);
 813    }
 814}
 815
 816static void msdc_host_power(struct msdc_host *host, int on)
 817{
 818    N_MSG(CFG, "Turn %s %s power ", on ? "on" : "off", "host");
 819
 820    if (on) {
 821        //msdc_core_power(host, 1); // need do card detection. 
 822        msdc_pin_reset(host, MSDC_PIN_PULL_UP);
 823    } else {
 824        msdc_pin_reset(host, MSDC_PIN_PULL_DOWN);
 825        //msdc_core_power(host, 0);
 826    }
 827}
 828
 829static void msdc_card_power(struct msdc_host *host, int on)
 830{
 831    N_MSG(CFG, "Turn %s %s power ", on ? "on" : "off", "card");
 832
 833    if (on) {
 834        msdc_pin_config(host, MSDC_PIN_PULL_UP);    
 835        if (host->hw->ext_power_on) {
 836            host->hw->ext_power_on();
 837        } else {
 838            //msdc_vdd_on(host);  // need todo card detection.
 839        }
 840        msleep(1);
 841    } else {
 842        if (host->hw->ext_power_off) {
 843            host->hw->ext_power_off();
 844        } else {
 845            //msdc_vdd_off(host);
 846        }
 847        msdc_pin_config(host, MSDC_PIN_PULL_DOWN);
 848        msleep(1);
 849    }
 850}
 851
 852static void msdc_set_power_mode(struct msdc_host *host, u8 mode)
 853{
 854    N_MSG(CFG, "Set power mode(%d)", mode);
 855
 856    if (host->power_mode == MMC_POWER_OFF && mode != MMC_POWER_OFF) {
 857        msdc_host_power(host, 1);
 858        msdc_card_power(host, 1);
 859    } else if (host->power_mode != MMC_POWER_OFF && mode == MMC_POWER_OFF) {
 860        msdc_card_power(host, 0);
 861        msdc_host_power(host, 0);
 862    }
 863    host->power_mode = mode;
 864}
 865#endif /* end of --- */
 866
 867#ifdef CONFIG_PM
 868/*
 869   register as callback function of WIFI(combo_sdio_register_pm) .    
 870   can called by msdc_drv_suspend/resume too. 
 871*/
 872static void msdc_pm(pm_message_t state, void *data)
 873{
 874    struct msdc_host *host = (struct msdc_host *)data;
 875    int evt = state.event;
 876
 877    if (evt == PM_EVENT_USER_RESUME || evt == PM_EVENT_USER_SUSPEND) {
 878        INIT_MSG("USR_%s: suspend<%d> power<%d>", 
 879                   evt == PM_EVENT_USER_RESUME ? "EVENT_USER_RESUME" : "EVENT_USER_SUSPEND", 
 880                   host->suspend, host->power_mode);            
 881    }
 882
 883    if (evt == PM_EVENT_SUSPEND || evt == PM_EVENT_USER_SUSPEND) {
 884        if (host->suspend) /* already suspend */  /* default 0*/
 885            return;
 886
 887        /* for memory card. already power off by mmc */
 888        if (evt == PM_EVENT_SUSPEND && host->power_mode == MMC_POWER_OFF)  
 889            return;
 890
 891        host->suspend = 1;
 892        host->pm_state = state;  /* default PMSG_RESUME */
 893        
 894        INIT_MSG("%s Suspend", evt == PM_EVENT_SUSPEND ? "PM" : "USR");                         
 895        if(host->hw->flags & MSDC_SYS_SUSPEND) /* set for card */
 896            (void)mmc_suspend_host(host->mmc);
 897        else { 
 898            // host->mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY;  /* just for double confirm */ /* --- by chhung */
 899            mmc_remove_host(host->mmc);
 900        }
 901    } else if (evt == PM_EVENT_RESUME || evt == PM_EVENT_USER_RESUME) {
 902        if (!host->suspend){
 903            //ERR_MSG("warning: already resume");       
 904            return;
 905        }
 906
 907        /* No PM resume when USR suspend */
 908        if (evt == PM_EVENT_RESUME && host->pm_state.event == PM_EVENT_USER_SUSPEND) {
 909            ERR_MSG("PM Resume when in USR Suspend");           /* won't happen. */
 910            return;
 911        }
 912        
 913        host->suspend = 0;
 914        host->pm_state = state;
 915        
 916        INIT_MSG("%s Resume", evt == PM_EVENT_RESUME ? "PM" : "USR");                
 917        if(host->hw->flags & MSDC_SYS_SUSPEND) { /* will not set for WIFI */
 918            (void)mmc_resume_host(host->mmc);
 919        }
 920        else { 
 921            // host->mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY; /* --- by chhung */
 922            mmc_add_host(host->mmc);
 923        }
 924    }
 925}
 926#endif
 927
 928/*--------------------------------------------------------------------------*/
 929/* mmc_host_ops members                                                      */
 930/*--------------------------------------------------------------------------*/
 931static unsigned int msdc_command_start(struct msdc_host   *host, 
 932                                      struct mmc_command *cmd,
 933                                      int                 tune,   /* not used */
 934                                      unsigned long       timeout)
 935{
 936    u32 base = host->base;
 937    u32 opcode = cmd->opcode;
 938    u32 rawcmd;
 939    u32 wints = MSDC_INT_CMDRDY  | MSDC_INT_RSPCRCERR  | MSDC_INT_CMDTMO  |  
 940                MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO | 
 941                MSDC_INT_ACMD19_DONE;  
 942                   
 943    u32 resp;  
 944    unsigned long tmo;
 945
 946    /* Protocol layer does not provide response type, but our hardware needs 
 947     * to know exact type, not just size!
 948     */
 949    if (opcode == MMC_SEND_OP_COND || opcode == SD_APP_OP_COND)
 950        resp = RESP_R3;
 951    else if (opcode == MMC_SET_RELATIVE_ADDR || opcode == SD_SEND_RELATIVE_ADDR)
 952        resp = (mmc_cmd_type(cmd) == MMC_CMD_BCR) ? RESP_R6 : RESP_R1;
 953    else if (opcode == MMC_FAST_IO)
 954        resp = RESP_R4;
 955    else if (opcode == MMC_GO_IRQ_STATE)
 956        resp = RESP_R5;
 957    else if (opcode == MMC_SELECT_CARD)
 958        resp = (cmd->arg != 0) ? RESP_R1B : RESP_NONE;
 959    else if (opcode == SD_IO_RW_DIRECT || opcode == SD_IO_RW_EXTENDED)
 960        resp = RESP_R1; /* SDIO workaround. */
 961    else if (opcode == SD_SEND_IF_COND && (mmc_cmd_type(cmd) == MMC_CMD_BCR))
 962        resp = RESP_R1;
 963    else {
 964        switch (mmc_resp_type(cmd)) {
 965        case MMC_RSP_R1:
 966            resp = RESP_R1;
 967            break;
 968        case MMC_RSP_R1B:
 969            resp = RESP_R1B;
 970            break;
 971        case MMC_RSP_R2:
 972            resp = RESP_R2;
 973            break;
 974        case MMC_RSP_R3:
 975            resp = RESP_R3;
 976            break;
 977        case MMC_RSP_NONE:
 978        default:
 979            resp = RESP_NONE;              
 980            break;
 981        }
 982    }
 983
 984    cmd->error = 0;
 985    /* rawcmd :
 986     * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 | 
 987     * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
 988     */    
 989    rawcmd = opcode | msdc_rsp[resp] << 7 | host->blksz << 16;
 990    
 991    if (opcode == MMC_READ_MULTIPLE_BLOCK) {
 992        rawcmd |= (2 << 11);
 993    } else if (opcode == MMC_READ_SINGLE_BLOCK) {
 994        rawcmd |= (1 << 11);
 995    } else if (opcode == MMC_WRITE_MULTIPLE_BLOCK) {
 996        rawcmd |= ((2 << 11) | (1 << 13));
 997    } else if (opcode == MMC_WRITE_BLOCK) {
 998        rawcmd |= ((1 << 11) | (1 << 13));
 999    } else if (opcode == SD_IO_RW_EXTENDED) {
1000        if (cmd->data->flags & MMC_DATA_WRITE)
1001            rawcmd |= (1 << 13);
1002        if (cmd->data->blocks > 1)
1003            rawcmd |= (2 << 11);
1004        else
1005            rawcmd |= (1 << 11);
1006    } else if (opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int)-1) {
1007        rawcmd |= (1 << 14);
1008    } else if ((opcode == SD_APP_SEND_SCR) || 
1009        (opcode == SD_APP_SEND_NUM_WR_BLKS) ||
1010        (opcode == SD_SWITCH && (mmc_cmd_type(cmd) == MMC_CMD_ADTC)) ||
1011        (opcode == SD_APP_SD_STATUS && (mmc_cmd_type(cmd) == MMC_CMD_ADTC)) ||
1012        (opcode == MMC_SEND_EXT_CSD && (mmc_cmd_type(cmd) == MMC_CMD_ADTC))) {
1013        rawcmd |= (1 << 11);
1014    } else if (opcode == MMC_STOP_TRANSMISSION) {
1015        rawcmd |= (1 << 14);
1016        rawcmd &= ~(0x0FFF << 16);
1017    }
1018
1019    N_MSG(CMD, "CMD<%d><0x%.8x> Arg<0x%.8x>", opcode , rawcmd, cmd->arg);
1020
1021    tmo = jiffies + timeout;
1022
1023    if (opcode == MMC_SEND_STATUS) {
1024        for (;;) {
1025            if (!sdc_is_cmd_busy())
1026                break;
1027                
1028            if (time_after(jiffies, tmo)) {
1029                ERR_MSG("XXX cmd_busy timeout: before CMD<%d>", opcode);        
1030                cmd->error = (unsigned int)-ETIMEDOUT;
1031                msdc_reset();
1032                goto end;
1033            } 
1034        }
1035    }else {
1036        for (;;) {       
1037            if (!sdc_is_busy())
1038                break;
1039            if (time_after(jiffies, tmo)) {
1040                ERR_MSG("XXX sdc_busy timeout: before CMD<%d>", opcode);        
1041                cmd->error = (unsigned int)-ETIMEDOUT;
1042                msdc_reset();
1043                goto end;      
1044            }   
1045        }    
1046    }   
1047    
1048    //BUG_ON(in_interrupt());
1049    host->cmd     = cmd;
1050    host->cmd_rsp = resp;               
1051    
1052    init_completion(&host->cmd_done);     
1053
1054    sdr_set_bits(MSDC_INTEN, wints);          
1055    sdc_send_cmd(rawcmd, cmd->arg);        
1056      
1057end:            
1058    return cmd->error;
1059}
1060
1061static unsigned int msdc_command_resp(struct msdc_host   *host, 
1062                                      struct mmc_command *cmd,
1063                                      int                 tune,
1064                                      unsigned long       timeout)
1065{
1066    u32 base = host->base;
1067    u32 opcode = cmd->opcode;
1068    //u32 rawcmd;
1069    u32 resp;
1070    u32 wints = MSDC_INT_CMDRDY  | MSDC_INT_RSPCRCERR  | MSDC_INT_CMDTMO  |  
1071                MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO | 
1072                MSDC_INT_ACMD19_DONE;     
1073    
1074    resp = host->cmd_rsp;
1075
1076    BUG_ON(in_interrupt());
1077    //init_completion(&host->cmd_done);
1078    //sdr_set_bits(MSDC_INTEN, wints);
1079        
1080    spin_unlock(&host->lock);   
1081    if(!wait_for_completion_timeout(&host->cmd_done, 10*timeout)){       
1082        ERR_MSG("XXX CMD<%d> wait_for_completion timeout ARG<0x%.8x>", opcode, cmd->arg);
1083        cmd->error = (unsigned int)-ETIMEDOUT;
1084        msdc_reset();
1085    }    
1086    spin_lock(&host->lock);
1087
1088    sdr_clr_bits(MSDC_INTEN, wints);
1089    host->cmd = NULL;
1090
1091//end:
1092#ifdef MT6575_SD_DEBUG
1093    switch (resp) {
1094    case RESP_NONE:
1095        N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)", opcode, cmd->error, resp);
1096        break;
1097    case RESP_R2:
1098        N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)= %.8x %.8x %.8x %.8x", 
1099            opcode, cmd->error, resp, cmd->resp[0], cmd->resp[1], 
1100            cmd->resp[2], cmd->resp[3]);          
1101        break;
1102    default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
1103        N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)= 0x%.8x", 
1104            opcode, cmd->error, resp, cmd->resp[0]);
1105        if (cmd->error == 0) {
1106            switch (resp) {
1107            case RESP_R1:
1108            case RESP_R1B:
1109                msdc_dump_card_status(host, cmd->resp[0]);
1110                break;
1111            case RESP_R3:
1112                msdc_dump_ocr_reg(host, cmd->resp[0]);
1113                break;
1114            case RESP_R5:
1115                msdc_dump_io_resp(host, cmd->resp[0]);
1116                break;
1117            case RESP_R6:
1118                msdc_dump_rca_resp(host, cmd->resp[0]);
1119                break;
1120            }
1121        }
1122        break;
1123    }
1124#endif
1125
1126    /* do we need to save card's RCA when SD_SEND_RELATIVE_ADDR */   
1127
1128    if (!tune) {
1129        return cmd->error;      
1130    }
1131
1132    /* memory card CRC */     
1133    if(host->hw->flags & MSDC_REMOVABLE && cmd->error == (unsigned int)(-EIO) ) {          
1134        if (sdr_read32(SDC_CMD) & 0x1800) { /* check if has data phase */ 
1135            msdc_abort_data(host);
1136        } else {
1137            /* do basic: reset*/        
1138            msdc_reset();
1139            msdc_clr_fifo();        
1140            msdc_clr_int();             
1141        } 
1142        cmd->error = msdc_tune_cmdrsp(host,cmd); 
1143    }
1144
1145    //  check DAT0 
1146    /* if (resp == RESP_R1B) {
1147        while ((sdr_read32(MSDC_PS) & 0x10000) != 0x10000);       
1148    } */ 
1149    /* CMD12 Error Handle */
1150                        
1151    return cmd->error;
1152}                                   
1153
1154static unsigned int msdc_do_command(struct msdc_host   *host, 
1155                                      struct mmc_command *cmd,
1156                                      int                 tune,
1157                                      unsigned long       timeout)
1158{
1159    if (msdc_command_start(host, cmd, tune, timeout)) 
1160        goto end;      
1161
1162    if (msdc_command_resp(host, cmd, tune, timeout)) 
1163        goto end;          
1164                    
1165end:    
1166
1167    N_MSG(CMD, "        return<%d> resp<0x%.8x>", cmd->error, cmd->resp[0]);    
1168    return cmd->error;
1169}
1170    
1171/* The abort condition when PIO read/write 
1172   tmo: 
1173*/
1174static int msdc_pio_abort(struct msdc_host *host, struct mmc_data *data, unsigned long tmo)
1175{
1176    int  ret = 0;       
1177    u32  base = host->base;
1178    
1179    if (atomic_read(&host->abort)) {    
1180        ret = 1;
1181    }    
1182
1183    if (time_after(jiffies, tmo)) {
1184        data->error = (unsigned int)-ETIMEDOUT;
1185        ERR_MSG("XXX PIO Data Timeout: CMD<%d>", host->mrq->cmd->opcode);
1186        ret = 1;                
1187    }      
1188    
1189    if(ret) {
1190        msdc_reset();
1191        msdc_clr_fifo();        
1192        msdc_clr_int();         
1193        ERR_MSG("msdc pio find abort");      
1194    }
1195    return ret; 
1196}
1197
1198/*
1199   Need to add a timeout, or WDT timeout, system reboot.      
1200*/
1201// pio mode data read/write
1202static int msdc_pio_read(struct msdc_host *host, struct mmc_data *data)
1203{
1204    struct scatterlist *sg = data->sg;
1205    u32  base = host->base;
1206    u32  num = data->sg_len;
1207    u32 *ptr;
1208    u8  *u8ptr;
1209    u32  left = 0;
1210    u32  count, size = 0;
1211    u32  wints = MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR ;     
1212    unsigned long tmo = jiffies + DAT_TIMEOUT;  
1213          
1214    sdr_set_bits(MSDC_INTEN, wints);
1215    while (num) {
1216        left = sg_dma_len(sg);
1217        ptr = sg_virt(sg);
1218        while (left) {
1219            if ((left >=  MSDC_FIFO_THD) && (msdc_rxfifocnt() >= MSDC_FIFO_THD)) {
1220                count = MSDC_FIFO_THD >> 2;
1221                do {
1222                    *ptr++ = msdc_fifo_read32();
1223                } while (--count);
1224                left -= MSDC_FIFO_THD;
1225            } else if ((left < MSDC_FIFO_THD) && msdc_rxfifocnt() >= left) {
1226                while (left > 3) {
1227                    *ptr++ = msdc_fifo_read32();
1228                    left -= 4;
1229                }
1230                 
1231                u8ptr = (u8 *)ptr; 
1232                while(left) {
1233                    * u8ptr++ = msdc_fifo_read8();
1234                    left--;       
1235                }
1236            }
1237            
1238            if (msdc_pio_abort(host, data, tmo)) {
1239                goto end;       
1240            }
1241        }
1242        size += sg_dma_len(sg);
1243        sg = sg_next(sg); num--;
1244    }
1245end:
1246    data->bytes_xfered += size;
1247    N_MSG(FIO, "        PIO Read<%d>bytes", size);
1248        
1249    sdr_clr_bits(MSDC_INTEN, wints);    
1250    if(data->error) ERR_MSG("read pio data->error<%d> left<%d> size<%d>", data->error, left, size);
1251    return data->error;
1252}
1253
1254/* please make sure won't using PIO when size >= 512 
1255   which means, memory card block read/write won't using pio
1256   then don't need to handle the CMD12 when data error. 
1257*/
1258static int msdc_pio_write(struct msdc_host* host, struct mmc_data *data)
1259{
1260    u32  base = host->base;
1261    struct scatterlist *sg = data->sg;
1262    u32  num = data->sg_len;
1263    u32 *ptr;
1264    u8  *u8ptr;
1265    u32  left;
1266    u32  count, size = 0;
1267    u32  wints = MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR ;      
1268    unsigned long tmo = jiffies + DAT_TIMEOUT;  
1269    
1270    sdr_set_bits(MSDC_INTEN, wints);    
1271    while (num) {
1272        left = sg_dma_len(sg);
1273        ptr = sg_virt(sg);
1274
1275        while (left) {
1276            if (left >= MSDC_FIFO_SZ && msdc_txfifocnt() == 0) {
1277                count = MSDC_FIFO_SZ >> 2;
1278                do {
1279                    msdc_fifo_write32(*ptr); ptr++;
1280                } while (--count);
1281                left -= MSDC_FIFO_SZ;
1282            } else if (left < MSDC_FIFO_SZ && msdc_txfifocnt() == 0) {
1283                while (left > 3) {
1284                    msdc_fifo_write32(*ptr); ptr++;
1285                    left -= 4;
1286                } 
1287                
1288                u8ptr = (u8*)ptr; 
1289                while(left){
1290                    msdc_fifo_write8(*u8ptr);   u8ptr++;
1291                    left--;
1292                }
1293            }
1294            
1295            if (msdc_pio_abort(host, data, tmo)) {
1296                goto end;       
1297            }                   
1298        }
1299        size += sg_dma_len(sg);
1300        sg = sg_next(sg); num--;
1301    }
1302end:    
1303    data->bytes_xfered += size;
1304    N_MSG(FIO, "        PIO Write<%d>bytes", size);
1305    if(data->error) ERR_MSG("write pio data->error<%d>", data->error);
1306        
1307    sdr_clr_bits(MSDC_INTEN, wints);  
1308    return data->error; 
1309}
1310
1311#if 0 /* --- by chhung */
1312// DMA resume / start / stop 
1313static void msdc_dma_resume(struct msdc_host *host)
1314{
1315    u32 base = host->base;
1316
1317    sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_RESUME, 1);
1318
1319    N_MSG(DMA, "DMA resume");
1320}
1321#endif /* end of --- */
1322
1323static void msdc_dma_start(struct msdc_host *host)
1324{
1325    u32 base = host->base;
1326    u32 wints = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR ; 
1327           
1328    sdr_set_bits(MSDC_INTEN, wints);
1329    //dsb(); /* --- by chhung */
1330    sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1);
1331
1332    N_MSG(DMA, "DMA start");
1333}
1334
1335static void msdc_dma_stop(struct msdc_host *host)
1336{
1337    u32 base = host->base;
1338    //u32 retries=500;
1339    u32 wints = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR ; 
1340    
1341    N_MSG(DMA, "DMA status: 0x%.8x",sdr_read32(MSDC_DMA_CFG));
1342    //while (sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS);
1343
1344    sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, 1);
1345    while (sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS);
1346
1347    //dsb(); /* --- by chhung */
1348    sdr_clr_bits(MSDC_INTEN, wints); /* Not just xfer_comp */
1349
1350    N_MSG(DMA, "DMA stop");
1351}
1352
1353#if 0 /* --- by chhung */
1354/* dump a gpd list */
1355static void msdc_dma_dump(struct msdc_host *host, struct msdc_dma *dma)
1356{
1357    gpd_t *gpd = dma->gpd; 
1358    bd_t   *bd = dma->bd;               
1359    bd_t   *ptr; 
1360    int i = 0; 
1361    int p_to_v; 
1362    
1363    if (dma->mode != MSDC_MODE_DMA_DESC) {
1364        return;         
1365    }    
1366
1367    ERR_MSG("try to dump gpd and bd");
1368
1369    /* dump gpd */
1370    ERR_MSG(".gpd<0x%.8x> gpd_phy<0x%.8x>", (int)gpd, (int)dma->gpd_addr);
1371    ERR_MSG("...hwo   <%d>", gpd->hwo );
1372    ERR_MSG("...bdp   <%d>", gpd->bdp );
1373    ERR_MSG("...chksum<0x%.8x>", gpd->chksum );
1374    //ERR_MSG("...intr  <0x%.8x>", gpd->intr );
1375    ERR_MSG("...next  <0x%.8x>", (int)gpd->next );
1376    ERR_MSG("...ptr   <0x%.8x>", (int)gpd->ptr );
1377    ERR_MSG("...buflen<0x%.8x>", gpd->buflen );
1378    //ERR_MSG("...extlen<0x%.8x>", gpd->extlen );
1379    //ERR_MSG("...arg   <0x%.8x>", gpd->arg );
1380    //ERR_MSG("...blknum<0x%.8x>", gpd->blknum );    
1381    //ERR_MSG("...cmd   <0x%.8x>", gpd->cmd );      
1382
1383    /* dump bd */
1384    ERR_MSG(".bd<0x%.8x> bd_phy<0x%.8x> gpd_ptr<0x%.8x>", (int)bd, (int)dma->bd_addr, (int)gpd->ptr);  
1385    ptr = bd; 
1386    p_to_v = ((u32)bd - (u32)dma->bd_addr);
1387    while (1) {
1388        ERR_MSG(".bd[%d]", i); i++;             
1389        ERR_MSG("...eol   <%d>", ptr->eol );
1390        ERR_MSG("...chksum<0x%.8x>", ptr->chksum );
1391        //ERR_MSG("...blkpad<0x%.8x>", ptr->blkpad );
1392        //ERR_MSG("...dwpad <0x%.8x>", ptr->dwpad );
1393        ERR_MSG("...next  <0x%.8x>", (int)ptr->next );
1394        ERR_MSG("...ptr   <0x%.8x>", (int)ptr->ptr );
1395        ERR_MSG("...buflen<0x%.8x>", (int)ptr->buflen );
1396        
1397        if (ptr->eol == 1) {
1398            break;      
1399        }
1400                             
1401        /* find the next bd, virtual address of ptr->next */
1402        /* don't need to enable when use malloc */
1403        //BUG_ON( (ptr->next + p_to_v)!=(ptr+1) );      
1404        //ERR_MSG(".next bd<0x%.8x><0x%.8x>", (ptr->next + p_to_v), (ptr+1));
1405        ptr++;               
1406    }    
1407    
1408    ERR_MSG("dump gpd and bd finished");
1409}
1410#endif /* end of --- */
1411
1412/* calc checksum */
1413static u8 msdc_dma_calcs(u8 *buf, u32 len)
1414{
1415    u32 i, sum = 0;
1416    for (i = 0; i < len; i++) {
1417        sum += buf[i];
1418    }
1419    return 0xFF - (u8)sum;
1420}
1421
1422/* gpd bd setup + dma registers */
1423static int msdc_dma_config(struct msdc_host *host, struct msdc_dma *dma)
1424{
1425    u32 base = host->base;
1426    u32 sglen = dma->sglen;
1427    //u32 i, j, num, bdlen, arg, xfersz;
1428    u32 j, num, bdlen;
1429    u8  blkpad, dwpad, chksum;
1430    struct scatterlist *sg = dma->sg;
1431    gpd_t *gpd;
1432    bd_t *bd;
1433
1434    switch (dma->mode) {
1435    case MSDC_MODE_DMA_BASIC:
1436        BUG_ON(dma->xfersz > 65535);
1437        BUG_ON(dma->sglen != 1);
1438        sdr_write32(MSDC_DMA_SA, PHYSADDR(sg_dma_address(sg)));
1439        sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_LASTBUF, 1);
1440//#if defined (CONFIG_RALINK_MT7620)
1441        if (ralink_soc == MT762X_SOC_MT7620A)
1442                sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_XFERSZ, sg_dma_len(sg));
1443//#elif defined (CONFIG_RALINK_MT7621) || defined (CONFIG_RALINK_MT7628)
1444        else
1445                sdr_write32((volatile u32*)(RALINK_MSDC_BASE+0xa8), sg_dma_len(sg));
1446//#endif
1447        sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_BRUSTSZ, dma->burstsz);
1448        sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_MODE, 0);
1449        break;
1450    case MSDC_MODE_DMA_DESC:
1451        blkpad = (dma->flags & DMA_FLAG_PAD_BLOCK) ? 1 : 0;
1452        dwpad  = (dma->flags & DMA_FLAG_PAD_DWORD) ? 1 : 0;
1453        chksum = (dma->flags & DMA_FLAG_EN_CHKSUM) ? 1 : 0;
1454
1455        /* calculate the required number of gpd */
1456        num = (sglen + MAX_BD_PER_GPD - 1) / MAX_BD_PER_GPD;        
1457        BUG_ON(num !=1 );        
1458        
1459        gpd = dma->gpd; 
1460        bd  = dma->bd; 
1461        bdlen = sglen; 
1462
1463        /* modify gpd*/
1464        //gpd->intr = 0; 
1465        gpd->hwo = 1;  /* hw will clear it */
1466        gpd->bdp = 1;     
1467        gpd->chksum = 0;  /* need to clear first. */   
1468        gpd->chksum = (chksum ? msdc_dma_calcs((u8 *)gpd, 16) : 0);
1469        
1470        /* modify bd*/          
1471        for (j = 0; j < bdlen; j++) {
1472            msdc_init_bd(&bd[j], blkpad, dwpad, sg_dma_address(sg), sg_dma_len(sg));            
1473            if(j == bdlen - 1) {
1474            bd[j].eol = 1;      /* the last bd */
1475            } else {
1476                bd[j].eol = 0;  
1477            }
1478            bd[j].chksum = 0; /* checksume need to clear first */
1479            bd[j].chksum = (chksum ? msdc_dma_calcs((u8 *)(&bd[j]), 16) : 0);         
1480            sg++;
1481        }
1482                
1483        dma->used_gpd += 2;
1484        dma->used_bd += bdlen;  
1485
1486        sdr_set_field(MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, chksum);
1487        sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_BRUSTSZ, dma->burstsz);
1488        sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_MODE, 1);
1489
1490        sdr_write32(MSDC_DMA_SA, PHYSADDR((u32)dma->gpd_addr));               
1491        break;
1492
1493    default:
1494        break;
1495    }
1496    
1497    N_MSG(DMA, "DMA_CTRL = 0x%x", sdr_read32(MSDC_DMA_CTRL));
1498    N_MSG(DMA, "DMA_CFG  = 0x%x", sdr_read32(MSDC_DMA_CFG));
1499    N_MSG(DMA, "DMA_SA   = 0x%x", sdr_read32(MSDC_DMA_SA));
1500
1501    return 0;
1502} 
1503
1504static void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma, 
1505    struct scatterlist *sg, unsigned int sglen)
1506{ 
1507    BUG_ON(sglen > MAX_BD_NUM); /* not support currently */
1508
1509    dma->sg = sg;
1510    dma->flags = DMA_FLAG_EN_CHKSUM;
1511    //dma->flags = DMA_FLAG_NONE; /* CHECKME */
1512    dma->sglen = sglen;
1513    dma->xfersz = host->xfer_size;
1514    dma->burstsz = MSDC_BRUST_64B;
1515    
1516    if (sglen == 1 && sg_dma_len(sg) <= MAX_DMA_CNT)
1517        dma->mode = MSDC_MODE_DMA_BASIC;
1518    else
1519        dma->mode = MSDC_MODE_DMA_DESC;
1520
1521    N_MSG(DMA, "DMA mode<%d> sglen<%d> xfersz<%d>", dma->mode, dma->sglen, dma->xfersz);
1522
1523    msdc_dma_config(host, dma);
1524    
1525    /*if (dma->mode == MSDC_MODE_DMA_DESC) {
1526        //msdc_dma_dump(host, dma);
1527    } */
1528}
1529
1530/* set block number before send command */
1531static void msdc_set_blknum(struct msdc_host *host, u32 blknum)
1532{
1533    u32 base = host->base;
1534
1535    sdr_write32(SDC_BLK_NUM, blknum);
1536}
1537
1538static int msdc_do_request(struct mmc_host*mmc, struct mmc_request*mrq)
1539{
1540    struct msdc_host *host = mmc_priv(mmc);
1541    struct mmc_command *cmd;
1542    struct mmc_data *data;
1543    u32 base = host->base;
1544    //u32 intsts = 0;     
1545          unsigned int left=0;
1546    int dma = 0, read = 1, dir = DMA_FROM_DEVICE, send_type=0;
1547    
1548    #define SND_DAT 0
1549    #define SND_CMD 1
1550
1551    BUG_ON(mmc == NULL);
1552    BUG_ON(mrq == NULL);    
1553
1554    host->error = 0;
1555    atomic_set(&host->abort, 0);
1556    
1557    cmd  = mrq->cmd;
1558    data = mrq->cmd->data;
1559   
1560#if 0 /* --- by chhung */
1561    //if(host->id ==1){
1562    N_MSG(OPS, "enable clock!");
1563    msdc_ungate_clock(host->id);       
1564                //}
1565#endif /* end of --- */
1566                
1567    if (!data) {
1568        send_type=SND_CMD;      
1569        if (msdc_do_command(host, cmd, 1, CMD_TIMEOUT) != 0) {
1570            goto done;         
1571        }
1572    } else {
1573        BUG_ON(data->blksz > HOST_MAX_BLKSZ);
1574        send_type=SND_DAT;
1575
1576        data->error = 0;
1577        read = data->flags & MMC_DATA_READ ? 1 : 0;
1578        host->data = data;
1579        host->xfer_size = data->blocks * data->blksz;
1580        host->blksz = data->blksz;
1581
1582        /* deside the transfer mode */
1583        if (drv_mode[host->id] == MODE_PIO) {
1584            host->dma_xfer = dma = 0;
1585        } else if (drv_mode[host->id] == MODE_DMA) {
1586            host->dma_xfer = dma = 1;           
1587        } else if (drv_mode[host->id] == MODE_SIZE_DEP) {
1588            host->dma_xfer = dma = ((host->xfer_size >= dma_size[host->id]) ? 1 : 0);   
1589        }      
1590
1591        if (read) {
1592            if ((host->timeout_ns != data->timeout_ns) ||
1593                (host->timeout_clks != data->timeout_clks)) {
1594                msdc_set_timeout(host, data->timeout_ns, data->timeout_clks);
1595            }
1596        }
1597        
1598        msdc_set_blknum(host, data->blocks);
1599        //msdc_clr_fifo();  /* no need */
1600
1601        if (dma) {
1602            msdc_dma_on();  /* enable DMA mode first!! */
1603            init_completion(&host->xfer_done);
1604            
1605            /* start the command first*/                
1606            if (msdc_command_start(host, cmd, 1, CMD_TIMEOUT) != 0)
1607                goto done;            
1608
1609            dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1610            (void)dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len, dir);
1611            msdc_dma_setup(host, &host->dma, data->sg, data->sg_len);            
1612                        
1613            /* then wait command done */
1614            if (msdc_command_resp(host, cmd, 1, CMD_TIMEOUT) != 0)
1615                goto done;            
1616
1617            /* for read, the data coming too fast, then CRC error 
1618               start DMA no business with CRC. */
1619            //init_completion(&host->xfer_done);           
1620            msdc_dma_start(host);
1621                       
1622            spin_unlock(&host->lock);
1623            if(!wait_for_completion_timeout(&host->xfer_done, DAT_TIMEOUT)){
1624                ERR_MSG("XXX CMD<%d> wait xfer_done<%d> timeout!!", cmd->opcode, data->blocks * data->blksz);
1625                ERR_MSG("    DMA_SA   = 0x%x", sdr_read32(MSDC_DMA_SA));
1626                ERR_MSG("    DMA_CA   = 0x%x", sdr_read32(MSDC_DMA_CA));         
1627                ERR_MSG("    DMA_CTRL = 0x%x", sdr_read32(MSDC_DMA_CTRL));
1628                ERR_MSG("    DMA_CFG  = 0x%x", sdr_read32(MSDC_DMA_CFG));           
1629                data->error = (unsigned int)-ETIMEDOUT;
1630                
1631                msdc_reset();
1632                msdc_clr_fifo();        
1633                msdc_clr_int(); 
1634            }
1635            spin_lock(&host->lock);
1636            msdc_dma_stop(host);             
1637        } else {
1638            /* Firstly: send command */
1639            if (msdc_do_command(host, cmd, 1, CMD_TIMEOUT) != 0) {
1640                goto done;
1641            }
1642                                             
1643            /* Secondly: pio data phase */           
1644            if (read) {
1645                if (msdc_pio_read(host, data)){
1646                    goto done;  
1647                }
1648            } else {
1649                if (msdc_pio_write(host, data)) {
1650                    goto done;          
1651                }
1652            }
1653
1654            /* For write case: make sure contents in fifo flushed to device */           
1655            if (!read) {                
1656                while (1) {
1657                    left=msdc_txfifocnt();                    
1658                    if (left == 0) {
1659                        break;  
1660                    }  
1661                    if (msdc_pio_abort(host, data, jiffies + DAT_TIMEOUT)) {
1662                        break;
1663                        /* Fix me: what about if data error, when stop ? how to? */
1664                    }                                    
1665                }
1666            } else {
1667                /* Fix me: read case: need to check CRC error */        
1668            }
1669
1670            /* For write case: SDCBUSY and Xfer_Comp will assert when DAT0 not busy. 
1671               For read case : SDCBUSY and Xfer_Comp will assert when last byte read out from FIFO.
1672            */                             
1673            
1674            /* try not to wait xfer_comp interrupt. 
1675               the next command will check SDC_BUSY. 
1676               SDC_BUSY means xfer_comp assert 
1677            */ 
1678                      
1679        } // PIO mode 
1680        
1681        /* Last: stop transfer */
1682        if (data->stop){ 
1683            if (msdc_do_command(host, data->stop, 0, CMD_TIMEOUT) != 0) {
1684                goto done; 
1685            }
1686        } 
1687    }
1688
1689done:
1690    if (data != NULL) {
1691        host->data = NULL;
1692        host->dma_xfer = 0;    
1693        if (dma != 0) {
1694            msdc_dma_off();     
1695            host->dma.used_bd  = 0;
1696            host->dma.used_gpd = 0;
1697            dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len, dir);
1698        }
1699        host->blksz = 0;  
1700                
1701#if 0 // don't stop twice!
1702        if(host->hw->flags & MSDC_REMOVABLE && data->error) {          
1703            msdc_abort_data(host);
1704            /* reset in IRQ, stop command has issued. -> No need */
1705        } 
1706#endif  
1707
1708        N_MSG(OPS, "CMD<%d> data<%s %s> blksz<%d> block<%d> error<%d>",cmd->opcode, (dma? "dma":"pio"), 
1709                (read ? "read ":"write") ,data->blksz, data->blocks, data->error);                
1710    }
1711
1712#if 0 /* --- by chhung */
1713#if 1    
1714    //if(host->id==1) {
1715    if(send_type==SND_CMD) {
1716        if(cmd->opcode == MMC_SEND_STATUS) {
1717            if((cmd->resp[0] & CARD_READY_FOR_DATA) ||(CARD_CURRENT_STATE(cmd->resp[0]) != 7)){
1718                N_MSG(OPS,"disable clock, CMD13 IDLE");
1719                msdc_gate_clock(host->id); 
1720            } 
1721        } else {
1722            N_MSG(OPS,"disable clock, CMD<%d>", cmd->opcode);   
1723            msdc_gate_clock(host->id);          
1724        }
1725    } else {
1726        if(read) {
1727                                N_MSG(OPS,"disable clock!!! Read CMD<%d>",cmd->opcode);
1728            msdc_gate_clock(host->id); 
1729        }
1730    }
1731    //}
1732#else
1733    msdc_gate_clock(host->id); 
1734#endif
1735#endif /* end of --- */
1736        
1737    if (mrq->cmd->error) host->error = 0x001;
1738    if (mrq->data && mrq->data->error) host->error |= 0x010;     
1739    if (mrq->stop && mrq->stop->error) host->error |= 0x100; 
1740
1741    //if (host->error) ERR_MSG("host->error<%d>", host->error);     
1742
1743    return host->error;
1744}
1745
1746static int msdc_app_cmd(struct mmc_host *mmc, struct msdc_host *host)
1747{
1748    struct mmc_command cmd;    
1749    struct mmc_request mrq;
1750    u32 err; 
1751
1752    memset(&cmd, 0, sizeof(struct mmc_command));    
1753    cmd.opcode = MMC_APP_CMD;    
1754#if 0   /* bug: we meet mmc->card is null when ACMD6 */   
1755    cmd.arg = mmc->card->rca << 16;
1756#else 
1757    cmd.arg = host->app_cmd_arg;     
1758#endif    
1759    cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1760
1761    memset(&mrq, 0, sizeof(struct mmc_request));
1762    mrq.cmd = &cmd; cmd.mrq = &mrq;
1763    cmd.data = NULL;        
1764
1765    err = msdc_do_command(host, &cmd, 0, CMD_TIMEOUT);     
1766    return err;         
1767}
1768
1769static int msdc_tune_cmdrsp(struct msdc_host*host, struct mmc_command *cmd)
1770{
1771    int result = -1;
1772    u32 base = host->base;
1773    u32 rsmpl, cur_rsmpl, orig_rsmpl;
1774    u32 rrdly, cur_rrdly = 0xffffffff, orig_rrdly;
1775    u32 skip = 1;
1776    
1777    /* ==== don't support 3.0 now ====
1778           1: R_SMPL[1] 
1779           2: PAD_CMD_RESP_RXDLY[26:22] 
1780            ==========================*/
1781
1782    // save the previous tune result 
1783    sdr_get_field(MSDC_IOCON,    MSDC_IOCON_RSPL,        orig_rsmpl);
1784    sdr_get_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_CMDRRDLY, orig_rrdly);
1785
1786    rrdly = 0; 
1787    do {
1788        for (rsmpl = 0; rsmpl < 2; rsmpl++) {
1789            /* Lv1: R_SMPL[1] */        
1790            cur_rsmpl = (orig_rsmpl + rsmpl) % 2;         
1791            if (skip == 1) {
1792                skip = 0;       
1793                continue;       
1794            }
1795            sdr_set_field(MSDC_IOCON, MSDC_IOCON_RSPL, cur_rsmpl); 
1796
1797            if (host->app_cmd) {
1798                result = msdc_app_cmd(host->mmc, host); 
1799                if (result) {
1800                    ERR_MSG("TUNE_CMD app_cmd<%d> failed: RESP_RXDLY<%d>,R_SMPL<%d>", 
1801                         host->mrq->cmd->opcode, cur_rrdly, cur_rsmpl);
1802                    continue;
1803                } 
1804            }          
1805            result = msdc_do_command(host, cmd, 0, CMD_TIMEOUT); // not tune.             
1806            ERR_MSG("TUNE_CMD<%d> %s PAD_CMD_RESP_RXDLY[26:22]<%d> R_SMPL[1]<%d>", cmd->opcode,
1807                       (result == 0) ? "PASS" : "FAIL", cur_rrdly, cur_rsmpl);
1808                        
1809            if (result == 0) {
1810                return 0;       
1811            }                           
1812            if (result != (unsigned int)(-EIO)) { 
1813                ERR_MSG("TUNE_CMD<%d> Error<%d> not -EIO", cmd->opcode, result);        
1814                return result;   
1815            }
1816
1817            /* should be EIO */
1818            if (sdr_read32(SDC_CMD) & 0x1800) { /* check if has data phase */ 
1819                msdc_abort_data(host);
1820            }
1821        }
1822                
1823        /* Lv2: PAD_CMD_RESP_RXDLY[26:22] */                    
1824        cur_rrdly = (orig_rrdly + rrdly + 1) % 32;
1825        sdr_set_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_CMDRRDLY, cur_rrdly);                
1826    }while (++rrdly < 32);
1827        
1828    return result;
1829}
1830
1831/* Support SD2.0 Only */
1832static int msdc_tune_bread(struct mmc_host *mmc, struct mmc_request *mrq)
1833{
1834    struct msdc_host *host = mmc_priv(mmc);
1835    u32 base = host->base;
1836    u32 ddr=0;  
1837    u32 dcrc=0;
1838    u32 rxdly, cur_rxdly0, cur_rxdly1;
1839    u32 dsmpl, cur_dsmpl,  orig_dsmpl;
1840    u32 cur_dat0,  cur_dat1,  cur_dat2,  cur_dat3;
1841    u32 cur_dat4,  cur_dat5,  cur_dat6,  cur_dat7;
1842    u32 orig_dat0, orig_dat1, orig_dat2, orig_dat3;
1843    u32 orig_dat4, orig_dat5, orig_dat6, orig_dat7;
1844    int result = -1;
1845    u32 skip = 1;
1846
1847    sdr_get_field(MSDC_IOCON, MSDC_IOCON_DSPL, orig_dsmpl);
1848        
1849    /* Tune Method 2. */
1850    sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
1851
1852    rxdly = 0; 
1853    do {
1854        for (dsmpl = 0; dsmpl < 2; dsmpl++) {
1855            cur_dsmpl = (orig_dsmpl + dsmpl) % 2;
1856            if (skip == 1) {
1857                skip = 0;       
1858                continue;       
1859            }             
1860            sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL, cur_dsmpl);
1861
1862            if (host->app_cmd) {
1863                result = msdc_app_cmd(host->mmc, host); 
1864                if (result) {
1865                    ERR_MSG("TUNE_BREAD app_cmd<%d> failed", host->mrq->cmd->opcode);   
1866                    continue;
1867                } 
1868            } 
1869            result = msdc_do_request(mmc,mrq);
1870            
1871            sdr_get_field(SDC_DCRC_STS, SDC_DCRC_STS_POS|SDC_DCRC_STS_NEG, dcrc); /* RO */
1872            if (!ddr) dcrc &= ~SDC_DCRC_STS_NEG;
1873            ERR_MSG("TUNE_BREAD<%s> dcrc<0x%x> DATRDDLY0/1<0x%x><0x%x> dsmpl<0x%x>",
1874                        (result == 0 && dcrc == 0) ? "PASS" : "FAIL", dcrc,
1875                        sdr_read32(MSDC_DAT_RDDLY0), sdr_read32(MSDC_DAT_RDDLY1), cur_dsmpl);
1876
1877            /* Fix me: result is 0, but dcrc is still exist */
1878            if (result == 0 && dcrc == 0) {
1879                goto done;
1880            } else {
1881                /* there is a case: command timeout, and data phase not processed */
1882                if (mrq->data->error != 0 && mrq->data->error != (unsigned int)(-EIO)) {
1883                    ERR_MSG("TUNE_READ: result<0x%x> cmd_error<%d> data_error<%d>", 
1884                               result, mrq->cmd->error, mrq->data->error);      
1885                    goto done;          
1886                }
1887            }
1888        }    
1889
1890        cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
1891        cur_rxdly1 = sdr_read32(MSDC_DAT_RDDLY1);
1892
1893        /* E1 ECO. YD: Reverse */
1894        if (sdr_read32(MSDC_ECO_VER) >= 4) {
1895            orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
1896            orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
1897            orig_dat2 = (cur_rxdly0 >>  8) & 0x1F;
1898            orig_dat3 = (cur_rxdly0 >>  0) & 0x1F;
1899            orig_dat4 = (cur_rxdly1 >> 24) & 0x1F;
1900            orig_dat5 = (cur_rxdly1 >> 16) & 0x1F;
1901            orig_dat6 = (cur_rxdly1 >>  8) & 0x1F;
1902            orig_dat7 = (cur_rxdly1 >>  0) & 0x1F;
1903        } else {   
1904            orig_dat0 = (cur_rxdly0 >>  0) & 0x1F;
1905            orig_dat1 = (cur_rxdly0 >>  8) & 0x1F;
1906            orig_dat2 = (cur_rxdly0 >> 16) & 0x1F;
1907            orig_dat3 = (cur_rxdly0 >> 24) & 0x1F;
1908            orig_dat4 = (cur_rxdly1 >>  0) & 0x1F;
1909            orig_dat5 = (cur_rxdly1 >>  8) & 0x1F;
1910            orig_dat6 = (cur_rxdly1 >> 16) & 0x1F;
1911            orig_dat7 = (cur_rxdly1 >> 24) & 0x1F;
1912        }
1913                
1914        if (ddr) {
1915            cur_dat0 = (dcrc & (1 << 0) || dcrc & (1 << 8))  ? ((orig_dat0 + 1) % 32) : orig_dat0;
1916            cur_dat1 = (dcrc & (1 << 1) || dcrc & (1 << 9))  ? ((orig_dat1 + 1) % 32) : orig_dat1;
1917            cur_dat2 = (dcrc & (1 << 2) || dcrc & (1 << 10)) ? ((orig_dat2 + 1) % 32) : orig_dat2;
1918            cur_dat3 = (dcrc & (1 << 3) || dcrc & (1 << 11)) ? ((orig_dat3 + 1) % 32) : orig_dat3;
1919        } else {
1920            cur_dat0 = (dcrc & (1 << 0)) ? ((orig_dat0 + 1) % 32) : orig_dat0;
1921            cur_dat1 = (dcrc & (1 << 1)) ? ((orig_dat1 + 1) % 32) : orig_dat1;
1922            cur_dat2 = (dcrc & (1 << 2)) ? ((orig_dat2 + 1) % 32) : orig_dat2;
1923            cur_dat3 = (dcrc & (1 << 3)) ? ((orig_dat3 + 1) % 32) : orig_dat3;
1924        }
1925        cur_dat4 = (dcrc & (1 << 4)) ? ((orig_dat4 + 1) % 32) : orig_dat4;
1926        cur_dat5 = (dcrc & (1 << 5)) ? ((orig_dat5 + 1) % 32) : orig_dat5;
1927        cur_dat6 = (dcrc & (1 << 6)) ? ((orig_dat6 + 1) % 32) : orig_dat6;
1928        cur_dat7 = (dcrc & (1 << 7)) ? ((orig_dat7 + 1) % 32) : orig_dat7;
1929
1930        cur_rxdly0 = (cur_dat0 << 24) | (cur_dat1 << 16) | (cur_dat2 << 8) | (cur_dat3 << 0);
1931        cur_rxdly1 = (cur_dat4 << 24) | (cur_dat5 << 16) | (cur_dat6 << 8) | (cur_dat7 << 0);
1932
1933        sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
1934        sdr_write32(MSDC_DAT_RDDLY1, cur_rxdly1);
1935
1936    } while (++rxdly < 32);   
1937          
1938done:
1939    return result;
1940}
1941
1942static int msdc_tune_bwrite(struct mmc_host *mmc,struct mmc_request *mrq)
1943{
1944          struct msdc_host *host = mmc_priv(mmc);
1945    u32 base = host->base;
1946
1947    u32 wrrdly, cur_wrrdly = 0xffffffff, orig_wrrdly;
1948    u32 dsmpl,  cur_dsmpl,  orig_dsmpl;
1949    u32 rxdly,  cur_rxdly0;
1950    u32 orig_dat0, orig_dat1, orig_dat2, orig_dat3;
1951    u32 cur_dat0,  cur_dat1,  cur_dat2,  cur_dat3;
1952    int result = -1;
1953    u32 skip = 1;
1954
1955    // MSDC_IOCON_DDR50CKD need to check. [Fix me] 
1956    
1957    sdr_get_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_DATWRDLY, orig_wrrdly);
1958    sdr_get_field(MSDC_IOCON,    MSDC_IOCON_DSPL,        orig_dsmpl );
1959
1960    /* Tune Method 2. just DAT0 */  
1961    sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
1962    cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
1963    
1964    /* E1 ECO. YD: Reverse */
1965    if (sdr_read32(MSDC_ECO_VER) >= 4) {
1966        orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
1967        orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
1968        orig_dat2 = (cur_rxdly0 >>  8) & 0x1F;
1969        orig_dat3 = (cur_rxdly0 >>  0) & 0x1F;
1970    } else {
1971        orig_dat0 = (cur_rxdly0 >>  0) & 0x1F;
1972        orig_dat1 = (cur_rxdly0 >>  8) & 0x1F;
1973        orig_dat2 = (cur_rxdly0 >> 16) & 0x1F;
1974        orig_dat3 = (cur_rxdly0 >> 24) & 0x1F;
1975    }
1976
1977    rxdly = 0;
1978    do {
1979        wrrdly = 0;
1980        do {    
1981            for (dsmpl = 0; dsmpl < 2; dsmpl++) {
1982                cur_dsmpl = (orig_dsmpl + dsmpl) % 2;
1983                if (skip == 1) {
1984                    skip = 0;
1985                    continue;   
1986                }    
1987                sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL, cur_dsmpl);
1988                
1989                if (host->app_cmd) {
1990                    result = msdc_app_cmd(host->mmc, host);     
1991                    if (result) {
1992                        ERR_MSG("TUNE_BWRITE app_cmd<%d> failed", host->mrq->cmd->opcode);      
1993                        continue;
1994                    } 
1995                }             
1996                result = msdc_do_request(mmc,mrq);
1997            
1998                ERR_MSG("TUNE_BWRITE<%s> DSPL<%d> DATWRDLY<%d> MSDC_DAT_RDDLY0<0x%x>", 
1999                          result == 0 ? "PASS" : "FAIL", 
2000                          cur_dsmpl, cur_wrrdly, cur_rxdly0);
2001            
2002                if (result == 0) {
2003                    goto done;
2004                }
2005                else {
2006                    /* there is a case: command timeout, and data phase not processed */
2007                    if (mrq->data->error != (unsigned int)(-EIO)) {
2008                        ERR_MSG("TUNE_READ: result<0x%x> cmd_error<%d> data_error<%d>", 
2009                                   result, mrq->cmd->error, mrq->data->error);  
2010                        goto done;      
2011                    }
2012                }       
2013            }
2014            cur_wrrdly = (orig_wrrdly + wrrdly + 1) % 32;
2015            sdr_set_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_DATWRDLY, cur_wrrdly);             
2016        } while (++wrrdly < 32); 
2017        
2018        cur_dat0 = (orig_dat0 + rxdly) % 32; /* only adjust bit-1 for crc */
2019        cur_dat1 = orig_dat1;
2020        cur_dat2 = orig_dat2;
2021        cur_dat3 = orig_dat3;                    
2022    
2023        cur_rxdly0 = (cur_dat0 << 24) | (cur_dat1 << 16) | (cur_dat2 << 8) | (cur_dat3 << 0);       
2024        sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);    
2025    } while (++rxdly < 32); 
2026
2027done:
2028    return result;
2029}
2030
2031static int msdc_get_card_status(struct mmc_host *mmc, struct msdc_host *host, u32 *status)
2032{
2033    struct mmc_command cmd;    
2034    struct mmc_request mrq;
2035    u32 err; 
2036
2037    memset(&cmd, 0, sizeof(struct mmc_command));    
2038    cmd.opcode = MMC_SEND_STATUS;    
2039    if (mmc->card) {
2040        cmd.arg = mmc->card->rca << 16;
2041    } else {
2042        ERR_MSG("cmd13 mmc card is null");              
2043        cmd.arg = host->app_cmd_arg;            
2044    }
2045    cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
2046
2047    memset(&mrq, 0, sizeof(struct mmc_request));
2048    mrq.cmd = &cmd; cmd.mrq = &mrq;
2049    cmd.data = NULL;        
2050
2051    err = msdc_do_command(host, &cmd, 1, CMD_TIMEOUT);        
2052    
2053    if (status) {
2054        *status = cmd.resp[0];
2055    }    
2056    
2057    return err;                 
2058}
2059
2060static int msdc_check_busy(struct mmc_host *mmc, struct msdc_host *host)
2061{
2062    u32 err = 0; 
2063    u32 status = 0;
2064    
2065    do {
2066        err = msdc_get_card_status(mmc, host, &status);
2067        if (err) return err;
2068        /* need cmd12? */        
2069        ERR_MSG("cmd<13> resp<0x%x>", status);
2070    } while (R1_CURRENT_STATE(status) == 7);   
2071    
2072    return err;         
2073}
2074
2075/* failed when msdc_do_request */
2076static int msdc_tune_request(struct mmc_host *mmc, struct mmc_request *mrq)
2077{
2078    struct msdc_host *host = mmc_priv(mmc);
2079    struct mmc_command *cmd;
2080    struct mmc_data *data;
2081    //u32 base = host->base;
2082          int ret=0, read; 
2083          
2084    cmd  = mrq->cmd;
2085    data = mrq->cmd->data;
2086    
2087    read = data->flags & MMC_DATA_READ ? 1 : 0;
2088
2089    if (read) {
2090        if (data->error == (unsigned int)(-EIO)) {              
2091            ret = msdc_tune_bread(mmc,mrq);     
2092        }
2093    } else {
2094        ret = msdc_check_busy(mmc, host);       
2095        if (ret){
2096            ERR_MSG("XXX cmd13 wait program done failed");
2097            return ret;
2098        }
2099        /* CRC and TO */        
2100        /* Fix me: don't care card status? */
2101        ret = msdc_tune_bwrite(mmc,mrq);        
2102    }
2103
2104    return ret;
2105}
2106
2107/* ops.request */
2108static void msdc_ops_request(struct mmc_host *mmc,struct mmc_request *mrq)
2109{   
2110    struct msdc_host *host = mmc_priv(mmc);
2111
2112    //=== for sdio profile ===
2113#if 0 /* --- by chhung */
2114    u32 old_H32, old_L32, new_H32, new_L32;
2115    u32 ticks = 0, opcode = 0, sizes = 0, bRx = 0; 
2116#endif /* end of --- */
2117      
2118    if(host->mrq){
2119        ERR_MSG("XXX host->mrq<0x%.8x>", (int)host->mrq);   
2120        BUG();  
2121    }            
2122      
2123    if (!is_card_present(host) || host->power_mode == MMC_POWER_OFF) {
2124        ERR_MSG("cmd<%d> card<%d> power<%d>", mrq->cmd->opcode, is_card_present(host), host->power_mode);
2125        mrq->cmd->error = (unsigned int)-ENOMEDIUM; 
2126        
2127#if 1        
2128        mrq->done(mrq);         // call done directly.
2129#else
2130        mrq->cmd->retries = 0;  // please don't retry.
2131        mmc_request_done(mmc, mrq);
2132#endif
2133
2134        return;
2135    }
2136    
2137    /* start to process */
2138    spin_lock(&host->lock); 
2139#if 0 /* --- by chhung */
2140    if (sdio_pro_enable) {  //=== for sdio profile ===  
2141        if (mrq->cmd->opcode == 52 || mrq->cmd->opcode == 53) {    
2142            GPT_GetCounter64(&old_L32, &old_H32); 
2143        }
2144    }
2145#endif /* end of --- */
2146    
2147    host->mrq = mrq;    
2148
2149    if (msdc_do_request(mmc,mrq)) {     
2150        if(host->hw->flags & MSDC_REMOVABLE && ralink_soc == MT762X_SOC_MT7621AT && mrq->data && mrq->data->error) {
2151            msdc_tune_request(mmc,mrq);                                         
2152        }               
2153    }
2154
2155    /* ==== when request done, check if app_cmd ==== */
2156    if (mrq->cmd->opcode == MMC_APP_CMD) {
2157        host->app_cmd = 1;        
2158        host->app_cmd_arg = mrq->cmd->arg;  /* save the RCA */
2159    } else {
2160        host->app_cmd = 0;       
2161        //host->app_cmd_arg = 0;        
2162    }
2163        
2164    host->mrq = NULL; 
2165
2166#if 0 /* --- by chhung */
2167    //=== for sdio profile ===
2168    if (sdio_pro_enable) {  
2169        if (mrq->cmd->opcode == 52 || mrq->cmd->opcode == 53) {     
2170            GPT_GetCounter64(&new_L32, &new_H32);
2171            ticks = msdc_time_calc(old_L32, old_H32, new_L32, new_H32);
2172            
2173            opcode = mrq->cmd->opcode;    
2174            if (mrq->cmd->data) {
2175                sizes = mrq->cmd->data->blocks * mrq->cmd->data->blksz;         
2176                bRx = mrq->cmd->data->flags & MMC_DATA_READ ? 1 : 0 ;
2177            } else {
2178                bRx = mrq->cmd->arg     & 0x80000000 ? 1 : 0;  
2179            }
2180            
2181            if (!mrq->cmd->error) {
2182                msdc_performance(opcode, sizes, bRx, ticks);
2183            }
2184        }    
2185    } 
2186#endif /* end of --- */
2187    spin_unlock(&host->lock);
2188        
2189    mmc_request_done(mmc, mrq);
2190     
2191   return;
2192}
2193
2194/* called by ops.set_ios */
2195static void msdc_set_buswidth(struct msdc_host *host, u32 width)
2196{
2197    u32 base = host->base;
2198    u32 val = sdr_read32(SDC_CFG);
2199    
2200    val &= ~SDC_CFG_BUSWIDTH;
2201    
2202    switch (width) {
2203    default:
2204    case MMC_BUS_WIDTH_1:
2205        width = 1;
2206        val |= (MSDC_BUS_1BITS << 16);
2207        break;
2208    case MMC_BUS_WIDTH_4:
2209        val |= (MSDC_BUS_4BITS << 16);
2210        break;
2211    case MMC_BUS_WIDTH_8:
2212        val |= (MSDC_BUS_8BITS << 16);
2213        break;
2214    }
2215    
2216    sdr_write32(SDC_CFG, val);
2217
2218    N_MSG(CFG, "Bus Width = %d", width);
2219}
2220
2221/* ops.set_ios */
2222static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
2223{
2224    struct msdc_host *host = mmc_priv(mmc);
2225    struct msdc_hw *hw=host->hw;
2226    u32 base = host->base;
2227    u32 ddr = 0;
2228
2229#ifdef MT6575_SD_DEBUG
2230    static char *vdd[] = {
2231        "1.50v", "1.55v", "1.60v", "1.65v", "1.70v", "1.80v", "1.90v",
2232        "2.00v", "2.10v", "2.20v", "2.30v", "2.40v", "2.50v", "2.60v",
2233        "2.70v", "2.80v", "2.90v", "3.00v", "3.10v", "3.20v", "3.30v",
2234        "3.40v", "3.50v", "3.60v"               
2235    };
2236    static char *power_mode[] = {
2237        "OFF", "UP", "ON"
2238    };
2239    static char *bus_mode[] = {
2240        "UNKNOWN", "OPENDRAIN", "PUSHPULL"
2241    };
2242    static char *timing[] = {
2243        "LEGACY", "MMC_HS", "SD_HS"
2244    };
2245
2246    printk("SET_IOS: CLK(%dkHz), BUS(%s), BW(%u), PWR(%s), VDD(%s), TIMING(%s)",
2247        ios->clock / 1000, bus_mode[ios->bus_mode],
2248        (ios->bus_width == MMC_BUS_WIDTH_4) ? 4 : 1,
2249        power_mode[ios->power_mode], vdd[ios->vdd], timing[ios->timing]);
2250#endif
2251
2252    msdc_set_buswidth(host, ios->bus_width);
2253    
2254    /* Power control ??? */
2255    switch (ios->power_mode) {
2256    case MMC_POWER_OFF:
2257    case MMC_POWER_UP:
2258    // msdc_set_power_mode(host, ios->power_mode); /* --- by chhung */
2259        break;
2260    case MMC_POWER_ON:
2261        host->power_mode = MMC_POWER_ON;
2262        break;
2263    default:
2264        break;
2265    }
2266
2267    /* Clock control */
2268    if (host->mclk != ios->clock) {
2269        if(ios->clock > 25000000) {     
2270            //if (!(host->hw->flags & MSDC_REMOVABLE)) {        
2271            INIT_MSG("SD data latch edge<%d>", hw->data_edge);            
2272            sdr_set_field(MSDC_IOCON, MSDC_IOCON_RSPL, hw->cmd_edge);
2273            sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL, hw->data_edge);
2274            //} /* for tuning debug */
2275        } else { /* default value */
2276            sdr_write32(MSDC_IOCON,      0x00000000);
2277            // sdr_write32(MSDC_DAT_RDDLY0, 0x00000000);
2278            sdr_write32(MSDC_DAT_RDDLY0, 0x10101010);           // for MT7620 E2 and afterward
2279            sdr_write32(MSDC_DAT_RDDLY1, 0x00000000);            
2280            // sdr_write32(MSDC_PAD_TUNE,   0x00000000);
2281            sdr_write32(MSDC_PAD_TUNE,   0x84101010);           // for MT7620 E2 and afterward
2282        }
2283        msdc_set_mclk(host, ddr, ios->clock);
2284    }
2285}
2286
2287/* ops.get_ro */
2288static int msdc_ops_get_ro(struct mmc_host *mmc)
2289{
2290    struct msdc_host *host = mmc_priv(mmc);
2291    u32 base = host->base;
2292    unsigned long flags;
2293    int ro = 0;
2294
2295    if (host->hw->flags & MSDC_WP_PIN_EN) { /* set for card */
2296        spin_lock_irqsave(&host->lock, flags);
2297        ro = (sdr_read32(MSDC_PS) >> 31);
2298        spin_unlock_irqrestore(&host->lock, flags);
2299    }
2300    return ro;
2301}
2302
2303/* ops.get_cd */
2304static int msdc_ops_get_cd(struct mmc_host *mmc)
2305{
2306    struct msdc_host *host = mmc_priv(mmc);
2307    u32 base = host->base;    
2308    unsigned long flags;
2309    int present = 1;
2310
2311    /* for sdio, MSDC_REMOVABLE not set, always return 1 */
2312    if (!(host->hw->flags & MSDC_REMOVABLE)) {
2313        /* For sdio, read H/W always get<1>, but may timeout some times */                      
2314#if 1
2315        host->card_inserted = 1;       
2316        return 1;
2317#else
2318        host->card_inserted = (host->pm_state.event == PM_EVENT_USER_RESUME) ? 1 : 0; 
2319        INIT_MSG("sdio ops_get_cd<%d>", host->card_inserted);
2320        return host->card_inserted; 
2321#endif
2322    }
2323
2324    /* MSDC_CD_PIN_EN set for card */
2325    if (host->hw->flags & MSDC_CD_PIN_EN) {
2326        spin_lock_irqsave(&host->lock, flags);
2327#if 0        
2328        present = host->card_inserted;  /* why not read from H/W: Fix me*/
2329#else
2330        // CD
2331        if (cd_active_low)
2332                present = (sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 0 : 1; 
2333        else
2334                present = (sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 1 : 0; 
2335        host->card_inserted = present;  
2336#endif        
2337        spin_unlock_irqrestore(&host->lock, flags);
2338    } else {
2339        present = 0; /* TODO? Check DAT3 pins for card detection */
2340    }
2341
2342    INIT_MSG("ops_get_cd return<%d>", present);
2343    return present;
2344}
2345
2346/* ops.enable_sdio_irq */
2347static void msdc_ops_enable_sdio_irq(struct mmc_host *mmc, int enable)
2348{
2349    struct msdc_host *host = mmc_priv(mmc);
2350    struct msdc_hw *hw = host->hw;
2351    u32 base = host->base;
2352    u32 tmp;
2353
2354    if (hw->flags & MSDC_EXT_SDIO_IRQ) { /* yes for sdio */
2355        if (enable) {
2356            hw->enable_sdio_eirq();  /* combo_sdio_enable_eirq */
2357        } else {
2358            hw->disable_sdio_eirq(); /* combo_sdio_disable_eirq */
2359        }
2360    } else { 
2361          ERR_MSG("XXX ");  /* so never enter here */
2362        tmp = sdr_read32(SDC_CFG);
2363        /* FIXME. Need to interrupt gap detection */
2364        if (enable) {
2365            tmp |= (SDC_CFG_SDIOIDE | SDC_CFG_SDIOINTWKUP);           
2366        } else {
2367            tmp &= ~(SDC_CFG_SDIOIDE | SDC_CFG_SDIOINTWKUP);
2368        }
2369        sdr_write32(SDC_CFG, tmp);      
2370    }
2371}
2372
2373static struct mmc_host_ops mt_msdc_ops = {
2374    .request         = msdc_ops_request,
2375    .set_ios         = msdc_ops_set_ios,
2376    .get_ro          = msdc_ops_get_ro,
2377    .get_cd          = msdc_ops_get_cd,
2378    .enable_sdio_irq = msdc_ops_enable_sdio_irq,
2379};
2380
2381/*--------------------------------------------------------------------------*/
2382/* interrupt handler                                                    */
2383/*--------------------------------------------------------------------------*/
2384static irqreturn_t msdc_irq(int irq, void *dev_id)
2385{
2386    struct msdc_host  *host = (struct msdc_host *)dev_id;
2387    struct mmc_data   *data = host->data;
2388    struct mmc_command *cmd = host->cmd;
2389    u32 base = host->base;
2390        
2391    u32 cmdsts = MSDC_INT_RSPCRCERR  | MSDC_INT_CMDTMO  | MSDC_INT_CMDRDY  |
2392                 MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO | MSDC_INT_ACMDRDY |
2393                 MSDC_INT_ACMD19_DONE;                 
2394    u32 datsts = MSDC_INT_DATCRCERR  |MSDC_INT_DATTMO;
2395
2396    u32 intsts = sdr_read32(MSDC_INT);
2397    u32 inten  = sdr_read32(MSDC_INTEN); inten &= intsts; 
2398
2399    sdr_write32(MSDC_INT, intsts);  /* clear interrupts */
2400    /* MSG will cause fatal error */
2401        
2402    /* card change interrupt */
2403    if (intsts & MSDC_INT_CDSC){
2404        if (mtk_sw_poll)
2405                return IRQ_HANDLED;
2406        IRQ_MSG("MSDC_INT_CDSC irq<0x%.8x>", intsts); 
2407#if 0 /* ---/+++ by chhung: fix slot mechanical bounce issue */
2408        tasklet_hi_schedule(&host->card_tasklet);
2409#else
2410        schedule_delayed_work(&host->card_delaywork, HZ);
2411#endif
2412        /* tuning when plug card ? */
2413    }
2414    
2415    /* sdio interrupt */
2416    if (intsts & MSDC_INT_SDIOIRQ){
2417        IRQ_MSG("XXX MSDC_INT_SDIOIRQ");  /* seems not sdio irq */
2418        //mmc_signal_sdio_irq(host->mmc);
2419    }
2420
2421    /* transfer complete interrupt */
2422    if (data != NULL) {
2423        if (inten & MSDC_INT_XFER_COMPL) {              
2424            data->bytes_xfered = host->dma.xfersz;
2425            complete(&host->xfer_done);           
2426        } 
2427        
2428        if (intsts & datsts) {         
2429            /* do basic reset, or stop command will sdc_busy */
2430            msdc_reset();
2431            msdc_clr_fifo();        
2432            msdc_clr_int();             
2433            atomic_set(&host->abort, 1);  /* For PIO mode exit */
2434            
2435            if (intsts & MSDC_INT_DATTMO){
2436                IRQ_MSG("XXX CMD<%d> MSDC_INT_DATTMO", host->mrq->cmd->opcode);
2437                data->error = (unsigned int)-ETIMEDOUT;
2438            }
2439            else if (intsts & MSDC_INT_DATCRCERR){
2440                IRQ_MSG("XXX CMD<%d> MSDC_INT_DATCRCERR, SDC_DCRC_STS<0x%x>", host->mrq->cmd->opcode, sdr_read32(SDC_DCRC_STS));
2441                data->error = (unsigned int)-EIO;
2442            }
2443                                    
2444            //if(sdr_read32(MSDC_INTEN) & MSDC_INT_XFER_COMPL) {  
2445            if (host->dma_xfer) {
2446                complete(&host->xfer_done); /* Read CRC come fast, XFER_COMPL not enabled */
2447            } /* PIO mode can't do complete, because not init */
2448        }
2449    }
2450
2451    /* command interrupts */
2452    if ((cmd != NULL) && (intsts & cmdsts)) {
2453        if ((intsts & MSDC_INT_CMDRDY) || (intsts & MSDC_INT_ACMDRDY) || 
2454            (intsts & MSDC_INT_ACMD19_DONE)) {
2455            u32 *rsp = &cmd->resp[0];
2456            
2457            switch (host->cmd_rsp) {
2458            case RESP_NONE:
2459                break;
2460            case RESP_R2:
2461                *rsp++ = sdr_read32(SDC_RESP3); *rsp++ = sdr_read32(SDC_RESP2);
2462                *rsp++ = sdr_read32(SDC_RESP1); *rsp++ = sdr_read32(SDC_RESP0);
2463                break;
2464            default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
2465                if ((intsts & MSDC_INT_ACMDRDY) || (intsts & MSDC_INT_ACMD19_DONE)) {
2466                    *rsp = sdr_read32(SDC_ACMD_RESP);
2467                } else {
2468                    *rsp = sdr_read32(SDC_RESP0);    
2469                }
2470                break;
2471            }
2472        } else if ((intsts & MSDC_INT_RSPCRCERR) || (intsts & MSDC_INT_ACMDCRCERR)) {
2473            if(intsts & MSDC_INT_ACMDCRCERR){
2474                IRQ_MSG("XXX CMD<%d> MSDC_INT_ACMDCRCERR",cmd->opcode);
2475            } 
2476            else {
2477                IRQ_MSG("XXX CMD<%d> MSDC_INT_RSPCRCERR",cmd->opcode);
2478            }
2479            cmd->error = (unsigned int)-EIO;
2480        } else if ((intsts & MSDC_INT_CMDTMO) || (intsts & MSDC_INT_ACMDTMO)) {
2481            if(intsts & MSDC_INT_ACMDTMO){
2482                IRQ_MSG("XXX CMD<%d> MSDC_INT_ACMDTMO",cmd->opcode);
2483            }
2484            else {
2485                IRQ_MSG("XXX CMD<%d> MSDC_INT_CMDTMO",cmd->opcode);
2486            }
2487            cmd->error = (unsigned int)-ETIMEDOUT;
2488            msdc_reset();
2489            msdc_clr_fifo();        
2490            msdc_clr_int();            
2491        }
2492        complete(&host->cmd_done);
2493    }
2494
2495    /* mmc irq interrupts */
2496    if (intsts & MSDC_INT_MMCIRQ) {
2497        printk(KERN_INFO "msdc[%d] MMCIRQ: SDC_CSTS=0x%.8x\r\n", host->id, sdr_read32(SDC_CSTS));    
2498    }
2499    
2500#ifdef MT6575_SD_DEBUG
2501    {
2502/*        msdc_int_reg *int_reg = (msdc_int_reg*)&intsts;*/
2503        N_MSG(INT, "IRQ_EVT(0x%x): MMCIRQ(%d) CDSC(%d), ACRDY(%d), ACTMO(%d), ACCRE(%d) AC19DN(%d)", 
2504            intsts,
2505            int_reg->mmcirq,
2506            int_reg->cdsc,
2507            int_reg->atocmdrdy,
2508            int_reg->atocmdtmo,
2509            int_reg->atocmdcrc,
2510            int_reg->atocmd19done);
2511        N_MSG(INT, "IRQ_EVT(0x%x): SDIO(%d) CMDRDY(%d), CMDTMO(%d), RSPCRC(%d), CSTA(%d)", 
2512            intsts,
2513            int_reg->sdioirq,
2514            int_reg->cmdrdy,
2515            int_reg->cmdtmo,
2516            int_reg->rspcrc,
2517            int_reg->csta);
2518        N_MSG(INT, "IRQ_EVT(0x%x): XFCMP(%d) DXDONE(%d), DATTMO(%d), DATCRC(%d), DMAEMP(%d)", 
2519            intsts,
2520            int_reg->xfercomp,
2521            int_reg->dxferdone,
2522            int_reg->dattmo,
2523            int_reg->datcrc,
2524            int_reg->dmaqempty);
2525
2526    }
2527#endif
2528    
2529    return IRQ_HANDLED;
2530}
2531
2532/*--------------------------------------------------------------------------*/
2533/* platform_driver members                                                      */
2534/*--------------------------------------------------------------------------*/
2535/* called by msdc_drv_probe/remove */
2536static void msdc_enable_cd_irq(struct msdc_host *host, int enable)
2537{
2538        struct msdc_hw *hw = host->hw;
2539        u32 base = host->base;
2540
2541        /* for sdio, not set */
2542        if ((hw->flags & MSDC_CD_PIN_EN) == 0) {
2543                /* Pull down card detection pin since it is not avaiable */
2544                /*
2545                   if (hw->config_gpio_pin) 
2546                   hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_DOWN);
2547                   */
2548                sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
2549                sdr_clr_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
2550                sdr_clr_bits(SDC_CFG, SDC_CFG_INSWKUP);
2551                return;
2552        }
2553
2554        N_MSG(CFG, "CD IRQ Eanable(%d)", enable);
2555
2556        if (enable) {
2557            if (hw->enable_cd_eirq) { /* not set, never enter */
2558                    hw->enable_cd_eirq();
2559            } else {
2560                    /* card detection circuit relies on the core power so that the core power 
2561                     * shouldn't be turned off. Here adds a reference count to keep 
2562                     * the core power alive.
2563                     */
2564                    //msdc_vcore_on(host); //did in msdc_init_hw()
2565
2566                    if (hw->config_gpio_pin) /* NULL */
2567                            hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_UP);
2568
2569                    sdr_set_field(MSDC_PS, MSDC_PS_CDDEBOUNCE, DEFAULT_DEBOUNCE);
2570                    sdr_set_bits(MSDC_PS, MSDC_PS_CDEN);
2571                    sdr_set_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
2572                    sdr_set_bits(SDC_CFG, SDC_CFG_INSWKUP);  /* not in document! Fix me */
2573            }
2574    } else {
2575            if (hw->disable_cd_eirq) {
2576                    hw->disable_cd_eirq();
2577            } else {
2578                    if (hw->config_gpio_pin) /* NULL */
2579                            hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_DOWN);
2580
2581                    sdr_clr_bits(SDC_CFG, SDC_CFG_INSWKUP);
2582                    sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
2583                    sdr_clr_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
2584
2585                    /* Here decreases a reference count to core power since card 
2586                     * detection circuit is shutdown.
2587                     */
2588                    //msdc_vcore_off(host);
2589            }
2590    }
2591}
2592
2593/* called by msdc_drv_probe */
2594static void msdc_init_hw(struct msdc_host *host)
2595{
2596    u32 base = host->base;
2597    struct msdc_hw *hw = host->hw;
2598
2599#ifdef MT6575_SD_DEBUG  
2600    msdc_reg[host->id] = (struct msdc_regs *)host->base;
2601#endif
2602
2603    /* Power on */
2604#if 0 /* --- by chhung */
2605    msdc_vcore_on(host);
2606    msdc_pin_reset(host, MSDC_PIN_PULL_UP);
2607    msdc_select_clksrc(host, hw->clk_src);
2608    enable_clock(PERI_MSDC0_PDN + host->id, "SD");
2609    msdc_vdd_on(host);
2610#endif /* end of --- */
2611    /* Configure to MMC/SD mode */
2612    sdr_set_field(MSDC_CFG, MSDC_CFG_MODE, MSDC_SDMMC); 
2613       
2614    /* Reset */
2615    msdc_reset();
2616    msdc_clr_fifo();
2617
2618    /* Disable card detection */
2619    sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
2620
2621    /* Disable and clear all interrupts */
2622    sdr_clr_bits(MSDC_INTEN, sdr_read32(MSDC_INTEN));
2623    sdr_write32(MSDC_INT, sdr_read32(MSDC_INT));
2624    
2625#if 1
2626        /* reset tuning parameter */
2627    sdr_write32(MSDC_PAD_CTL0,   0x00090000);
2628    sdr_write32(MSDC_PAD_CTL1,   0x000A0000);
2629    sdr_write32(MSDC_PAD_CTL2,   0x000A0000);
2630    // sdr_write32(MSDC_PAD_TUNE,   0x00000000);
2631    sdr_write32(MSDC_PAD_TUNE,   0x84101010);           // for MT7620 E2 and afterward
2632    // sdr_write32(MSDC_DAT_RDDLY0, 0x00000000);
2633    sdr_write32(MSDC_DAT_RDDLY0, 0x10101010);           // for MT7620 E2 and afterward
2634    sdr_write32(MSDC_DAT_RDDLY1, 0x00000000);
2635    sdr_write32(MSDC_IOCON,      0x00000000);
2636#if 0 // use MT7620 default value: 0x403c004f
2637    sdr_write32(MSDC_PATCH_BIT0, 0x003C000F); /* bit0 modified: Rx Data Clock Source: 1 -> 2.0*/
2638#endif
2639
2640    if (sdr_read32(MSDC_ECO_VER) >= 4) { 
2641        if (host->id == 1) {    
2642            sdr_set_field(MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_WRDAT_CRCS, 1); 
2643            sdr_set_field(MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_CMD_RSP,    1);
2644            
2645            /* internal clock: latch read data */  
2646            sdr_set_bits(MSDC_PATCH_BIT0, MSDC_PATCH_BIT_CKGEN_CK);  
2647        }               
2648    }   
2649#endif    
2650
2651    /* for safety, should clear SDC_CFG.SDIO_INT_DET_EN & set SDC_CFG.SDIO in 
2652       pre-loader,uboot,kernel drivers. and SDC_CFG.SDIO_INT_DET_EN will be only
2653       set when kernel driver wants to use SDIO bus interrupt */
2654    /* Configure to enable SDIO mode. it's must otherwise sdio cmd5 failed */
2655    sdr_set_bits(SDC_CFG, SDC_CFG_SDIO);
2656
2657    /* disable detect SDIO device interupt function */
2658    sdr_clr_bits(SDC_CFG, SDC_CFG_SDIOIDE);
2659
2660    /* eneable SMT for glitch filter */
2661    sdr_set_bits(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKSMT);
2662    sdr_set_bits(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDSMT);
2663    sdr_set_bits(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATSMT);
2664
2665#if 1
2666    /* set clk, cmd, dat pad driving */
2667    sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVN, hw->clk_drv);
2668    sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVP, hw->clk_drv);
2669    sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVN, hw->cmd_drv);
2670    sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVP, hw->cmd_drv);
2671    sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVN, hw->dat_drv);
2672    sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVP, hw->dat_drv);
2673#else 
2674    sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVN, 0);
2675    sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVP, 0);
2676    sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVN, 0);
2677    sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVP, 0);
2678    sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVN, 0);
2679    sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVP, 0);
2680#endif
2681
2682    /* set sampling edge */
2683
2684    /* write crc timeout detection */
2685    sdr_set_field(MSDC_PATCH_BIT0, 1 << 30, 1);
2686
2687    /* Configure to default data timeout */
2688    sdr_set_field(SDC_CFG, SDC_CFG_DTOC, DEFAULT_DTOC);
2689
2690    msdc_set_buswidth(host, MMC_BUS_WIDTH_1);
2691
2692    N_MSG(FUC, "init hardware done!");
2693}
2694
2695/* called by msdc_drv_remove */
2696static void msdc_deinit_hw(struct msdc_host *host)
2697{
2698    u32 base = host->base;
2699
2700    /* Disable and clear all interrupts */
2701    sdr_clr_bits(MSDC_INTEN, sdr_read32(MSDC_INTEN));
2702    sdr_write32(MSDC_INT, sdr_read32(MSDC_INT));
2703
2704    /* Disable card detection */
2705    msdc_enable_cd_irq(host, 0);
2706    // msdc_set_power_mode(host, MMC_POWER_OFF);   /* make sure power down */ /* --- by chhung */
2707}
2708
2709/* init gpd and bd list in msdc_drv_probe */
2710static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma)
2711{
2712    gpd_t *gpd = dma->gpd; 
2713    bd_t  *bd  = dma->bd;       
2714    bd_t  *ptr, *prev;
2715    
2716    /* we just support one gpd */     
2717    int bdlen = MAX_BD_PER_GPD;         
2718
2719    /* init the 2 gpd */
2720    memset(gpd, 0, sizeof(gpd_t) * 2);
2721    //gpd->next = (void *)virt_to_phys(gpd + 1); /* pointer to a null gpd, bug! kmalloc <-> virt_to_phys */  
2722    //gpd->next = (dma->gpd_addr + 1);    /* bug */
2723    gpd->next = (void *)((u32)dma->gpd_addr + sizeof(gpd_t));    
2724
2725    //gpd->intr = 0;
2726    gpd->bdp  = 1;   /* hwo, cs, bd pointer */      
2727    //gpd->ptr  = (void*)virt_to_phys(bd); 
2728    gpd->ptr = (void *)dma->bd_addr; /* physical address */
2729    
2730    memset(bd, 0, sizeof(bd_t) * bdlen);
2731    ptr = bd + bdlen - 1;
2732    //ptr->eol  = 1;  /* 0 or 1 [Fix me]*/
2733    //ptr->next = 0;    
2734    
2735    while (ptr != bd) {
2736        prev = ptr - 1;
2737        prev->next = (void *)(dma->bd_addr + sizeof(bd_t) *(ptr - bd));
2738        ptr = prev;
2739    }
2740}
2741
2742static int msdc_drv_probe(struct platform_device *pdev)
2743{
2744    struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2745    __iomem void *base;
2746    struct mmc_host *mmc;
2747    struct resource *mem;
2748    struct msdc_host *host;
2749    struct msdc_hw *hw;
2750    int ret, irq;
2751 
2752    pdev->dev.platform_data = &msdc0_hw;
2753 
2754   if (of_property_read_bool(pdev->dev.of_node, "mtk,wp-en"))
2755        msdc0_hw.flags |= MSDC_WP_PIN_EN;
2756  
2757    /* Allocate MMC host for this device */
2758    mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev);
2759    if (!mmc) return -ENOMEM;
2760
2761    hw   = (struct msdc_hw*)pdev->dev.platform_data;
2762    mem  = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2763    irq  = platform_get_irq(pdev, 0);
2764
2765    //BUG_ON((!hw) || (!mem) || (irq < 0)); /* --- by chhung */
2766    
2767    base = devm_ioremap_resource(&pdev->dev, res);
2768    if (IS_ERR(base))
2769        return PTR_ERR(base);
2770
2771    /* Set host parameters to mmc */
2772    mmc->ops        = &mt_msdc_ops;
2773    mmc->f_min      = HOST_MIN_MCLK;
2774    mmc->f_max      = HOST_MAX_MCLK;
2775    mmc->ocr_avail  = MSDC_OCR_AVAIL;
2776    
2777    /* For sd card: MSDC_SYS_SUSPEND | MSDC_WP_PIN_EN | MSDC_CD_PIN_EN | MSDC_REMOVABLE | MSDC_HIGHSPEED, 
2778       For sdio   : MSDC_EXT_SDIO_IRQ | MSDC_HIGHSPEED */
2779    if (hw->flags & MSDC_HIGHSPEED) {
2780        mmc->caps   = MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
2781    }
2782    if (hw->data_pins == 4) { /* current data_pins are all 4*/
2783        mmc->caps  |= MMC_CAP_4_BIT_DATA;
2784    } else if (hw->data_pins == 8) {
2785        mmc->caps  |= MMC_CAP_8_BIT_DATA;
2786    }
2787    if ((hw->flags & MSDC_SDIO_IRQ) || (hw->flags & MSDC_EXT_SDIO_IRQ))
2788        mmc->caps |= MMC_CAP_SDIO_IRQ;  /* yes for sdio */
2789
2790        cd_active_low = !of_property_read_bool(pdev->dev.of_node, "mediatek,cd-high");
2791        mtk_sw_poll = of_property_read_bool(pdev->dev.of_node, "mediatek,cd-poll");
2792
2793        if (mtk_sw_poll)
2794                mmc->caps |= MMC_CAP_NEEDS_POLL;
2795
2796    /* MMC core transfer sizes tunable parameters */
2797#if LINUX_VERSION_CODE > KERNEL_VERSION(3,10,0)
2798    mmc->max_segs      = MAX_HW_SGMTS;
2799#else
2800    mmc->max_hw_segs   = MAX_HW_SGMTS;
2801    mmc->max_phys_segs = MAX_PHY_SGMTS;
2802#endif
2803    mmc->max_seg_size  = MAX_SGMT_SZ;
2804    mmc->max_blk_size  = HOST_MAX_BLKSZ;
2805    mmc->max_req_size  = MAX_REQ_SZ; 
2806    mmc->max_blk_count = mmc->max_req_size;
2807
2808    host = mmc_priv(mmc);
2809    host->hw        = hw;
2810    host->mmc       = mmc;
2811    host->id        = pdev->id;
2812    if (host->id < 0 || host->id >= 4)
2813            host->id = 0;
2814    host->error     = 0;
2815    host->irq       = irq;    
2816    host->base      = (unsigned long) base;
2817    host->mclk      = 0;                   /* mclk: the request clock of mmc sub-system */
2818    host->hclk      = hclks[hw->clk_src];  /* hclk: clock of clock source to msdc controller */
2819    host->sclk      = 0;                   /* sclk: the really clock after divition */
2820    host->pm_state  = PMSG_RESUME;
2821    host->suspend   = 0;
2822    host->core_clkon = 0;
2823    host->card_clkon = 0;    
2824    host->core_power = 0;
2825    host->power_mode = MMC_POWER_OFF;
2826//    host->card_inserted = hw->flags & MSDC_REMOVABLE ? 0 : 1;
2827    host->timeout_ns = 0;
2828    host->timeout_clks = DEFAULT_DTOC * 65536;
2829  
2830    host->mrq = NULL; 
2831    //init_MUTEX(&host->sem); /* we don't need to support multiple threads access */
2832   
2833    host->dma.used_gpd = 0;
2834    host->dma.used_bd = 0;
2835    mmc_dev(mmc)->dma_mask = NULL;
2836
2837    /* using dma_alloc_coherent*/  /* todo: using 1, for all 4 slots */
2838    host->dma.gpd = dma_alloc_coherent(NULL, MAX_GPD_NUM * sizeof(gpd_t), &host->dma.gpd_addr, GFP_KERNEL); 
2839    host->dma.bd =  dma_alloc_coherent(NULL, MAX_BD_NUM  * sizeof(bd_t),  &host->dma.bd_addr,  GFP_KERNEL); 
2840    BUG_ON((!host->dma.gpd) || (!host->dma.bd));    
2841    msdc_init_gpd_bd(host, &host->dma);
2842    /*for emmc*/
2843    msdc_6575_host[pdev->id] = host;
2844    
2845#if 0
2846    tasklet_init(&host->card_tasklet, msdc_tasklet_card, (ulong)host);
2847#else
2848    INIT_DELAYED_WORK(&host->card_delaywork, msdc_tasklet_card);
2849#endif
2850    spin_lock_init(&host->lock);
2851    msdc_init_hw(host);
2852
2853        if (ralink_soc == MT762X_SOC_MT7621AT)
2854                ret = request_irq((unsigned int)irq, msdc_irq, 0, dev_name(&pdev->dev), host);
2855        else 
2856                ret = request_irq((unsigned int)irq, msdc_irq, IRQF_TRIGGER_LOW, dev_name(&pdev->dev), host);
2857
2858    if (ret) goto release;
2859    // mt65xx_irq_unmask(irq); /* --- by chhung */
2860    
2861    if (hw->flags & MSDC_CD_PIN_EN) { /* not set for sdio */
2862        if (hw->request_cd_eirq) { /* not set for MT6575 */
2863            hw->request_cd_eirq(msdc_eirq_cd, (void*)host); /* msdc_eirq_cd will not be used! */
2864        }
2865    }
2866
2867    if (hw->request_sdio_eirq) /* set to combo_sdio_request_eirq() for WIFI */
2868        hw->request_sdio_eirq(msdc_eirq_sdio, (void*)host); /* msdc_eirq_sdio() will be called when EIRQ */
2869
2870    if (hw->register_pm) {/* yes for sdio */
2871#ifdef CONFIG_PM
2872        hw->register_pm(msdc_pm, (void*)host);  /* combo_sdio_register_pm() */
2873#endif
2874        if(hw->flags & MSDC_SYS_SUSPEND) { /* will not set for WIFI */
2875            ERR_MSG("MSDC_SYS_SUSPEND and register_pm both set");
2876        }
2877        //mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY; /* pm not controlled by system but by client. */ /* --- by chhung */
2878    }
2879    
2880    platform_set_drvdata(pdev, mmc);
2881
2882    ret = mmc_add_host(mmc);
2883    if (ret) goto free_irq;
2884
2885    /* Config card detection pin and enable interrupts */
2886    if (hw->flags & MSDC_CD_PIN_EN) {  /* set for card */
2887        msdc_enable_cd_irq(host, 1);
2888    } else {
2889        msdc_enable_cd_irq(host, 0);
2890    }  
2891
2892    return 0;
2893
2894free_irq:
2895    free_irq(irq, host);
2896release:
2897    platform_set_drvdata(pdev, NULL);
2898    msdc_deinit_hw(host);
2899
2900#if 0
2901    tasklet_kill(&host->card_tasklet);
2902#else
2903    cancel_delayed_work_sync(&host->card_delaywork);
2904#endif
2905
2906    if (mem)
2907        release_mem_region(mem->start, mem->end - mem->start + 1);
2908
2909    mmc_free_host(mmc);
2910
2911    return ret;
2912}
2913
2914/* 4 device share one driver, using "drvdata" to show difference */
2915static int msdc_drv_remove(struct platform_device *pdev)
2916{
2917    struct mmc_host *mmc;
2918    struct msdc_host *host;
2919    struct resource *mem;
2920
2921    mmc  = platform_get_drvdata(pdev);
2922    BUG_ON(!mmc);
2923    
2924    host = mmc_priv(mmc);   
2925    BUG_ON(!host);
2926
2927    ERR_MSG("removed !!!");
2928
2929    platform_set_drvdata(pdev, NULL);
2930    mmc_remove_host(host->mmc);
2931    msdc_deinit_hw(host);
2932
2933#if 0
2934    tasklet_kill(&host->card_tasklet);
2935#else
2936    cancel_delayed_work_sync(&host->card_delaywork);
2937#endif
2938    free_irq(host->irq, host);
2939
2940    dma_free_coherent(NULL, MAX_GPD_NUM * sizeof(gpd_t), host->dma.gpd, host->dma.gpd_addr);
2941    dma_free_coherent(NULL, MAX_BD_NUM  * sizeof(bd_t),  host->dma.bd,  host->dma.bd_addr);
2942
2943    mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2944
2945    if (mem)
2946        release_mem_region(mem->start, mem->end - mem->start + 1);
2947
2948    mmc_free_host(host->mmc);
2949
2950    return 0;
2951}
2952
2953/* Fix me: Power Flow */
2954#ifdef CONFIG_PM
2955static int msdc_drv_suspend(struct platform_device *pdev, pm_message_t state)
2956{
2957    int ret = 0;
2958    struct mmc_host *mmc = platform_get_drvdata(pdev);
2959    struct msdc_host *host = mmc_priv(mmc);
2960
2961    if (mmc && state.event == PM_EVENT_SUSPEND && (host->hw->flags & MSDC_SYS_SUSPEND)) { /* will set for card */
2962        msdc_pm(state, (void*)host);
2963    }
2964    
2965    return ret;
2966}
2967
2968static int msdc_drv_resume(struct platform_device *pdev)
2969{
2970    int ret = 0;
2971    struct mmc_host *mmc = platform_get_drvdata(pdev);
2972    struct msdc_host *host = mmc_priv(mmc);
2973    struct pm_message state;
2974
2975    state.event = PM_EVENT_RESUME;
2976    if (mmc && (host->hw->flags & MSDC_SYS_SUSPEND)) {/* will set for card */
2977        msdc_pm(state, (void*)host);
2978    }
2979
2980    /* This mean WIFI not controller by PM */
2981    
2982    return ret;
2983}
2984#endif
2985
2986static const struct of_device_id mt7620_sdhci_match[] = {
2987        { .compatible = "ralink,mt7620-sdhci" },
2988        {},
2989};
2990MODULE_DEVICE_TABLE(of, mt7620_sdhci_match);
2991
2992static struct platform_driver mt_msdc_driver = {
2993    .probe   = msdc_drv_probe,
2994    .remove  = msdc_drv_remove,
2995#ifdef CONFIG_PM
2996    .suspend = msdc_drv_suspend,
2997    .resume  = msdc_drv_resume,
2998#endif
2999    .driver  = {
3000        .name  = DRV_NAME,
3001        .owner = THIS_MODULE,
3002        .of_match_table = mt7620_sdhci_match,
3003    },
3004};
3005
3006/*--------------------------------------------------------------------------*/
3007/* module init/exit                                                      */
3008/*--------------------------------------------------------------------------*/
3009static int __init mt_msdc_init(void)
3010{
3011    int ret;
3012/* +++ by chhung */
3013    u32 reg;
3014
3015#if defined (CONFIG_MTD_ANY_RALINK)
3016    extern int ra_check_flash_type(void);
3017    if(ra_check_flash_type() == 2) { /* NAND */
3018            printk("%s: !!!!! SDXC Module Initialize Fail !!!!!", __func__);
3019            return 0;
3020    }
3021#endif
3022    printk("MTK MSDC device init.\n");
3023    mtk_sd_device.dev.platform_data = &msdc0_hw;
3024if (ralink_soc == MT762X_SOC_MT7620A || ralink_soc == MT762X_SOC_MT7621AT) {
3025//#if defined (CONFIG_RALINK_MT7620) || defined (CONFIG_RALINK_MT7621)
3026    reg = sdr_read32((volatile u32*)(RALINK_SYSCTL_BASE + 0x60)) & ~(0x3<<18);
3027//#if defined (CONFIG_RALINK_MT7620)
3028        if (ralink_soc == MT762X_SOC_MT7620A)
3029                reg |= 0x1<<18;
3030//#endif
3031} else {
3032//#elif defined (CONFIG_RALINK_MT7628)
3033    /* TODO: maybe omitted when RAether already toggle AGPIO_CFG */
3034    reg = sdr_read32((volatile u32*)(RALINK_SYSCTL_BASE + 0x3c));
3035    reg |= 0x1e << 16;
3036    sdr_write32((volatile u32*)(RALINK_SYSCTL_BASE + 0x3c), reg);
3037
3038    reg = sdr_read32((volatile u32*)(RALINK_SYSCTL_BASE + 0x60)) & ~(0x3<<10);
3039#if defined (CONFIG_MTK_MMC_EMMC_8BIT)
3040    reg |= 0x3<<26 | 0x3<<28 | 0x3<<30;
3041    msdc0_hw.data_pins      = 8,
3042#endif
3043//#endif
3044}
3045    sdr_write32((volatile u32*)(RALINK_SYSCTL_BASE + 0x60), reg);
3046    //platform_device_register(&mtk_sd_device);
3047/* end of +++ */
3048
3049    ret = platform_driver_register(&mt_msdc_driver);
3050    if (ret) {
3051        printk(KERN_ERR DRV_NAME ": Can't register driver");
3052        return ret;
3053    }
3054    printk(KERN_INFO DRV_NAME ": MediaTek MT6575 MSDC Driver\n");
3055
3056#if defined (MT6575_SD_DEBUG)
3057    msdc_debug_proc_init();
3058#endif
3059    return 0;
3060}
3061
3062static void __exit mt_msdc_exit(void)
3063{
3064//    platform_device_unregister(&mtk_sd_device);
3065    platform_driver_unregister(&mt_msdc_driver);
3066}
3067
3068module_init(mt_msdc_init);
3069module_exit(mt_msdc_exit);
3070MODULE_LICENSE("GPL");
3071MODULE_DESCRIPTION("MediaTek MT6575 SD/MMC Card Driver");
3072MODULE_AUTHOR("Infinity Chen <infinity.chen@mediatek.com>");
3073
3074EXPORT_SYMBOL(msdc_6575_host);
3075