1 /* Copyright Statement:
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.
10 * MediaTek Inc. (C) 2010. All rights reserved.
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.
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.
36 #include <linux/module.h>
37 #include <linux/delay.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/spinlock.h>
40 #include <linux/platform_device.h>
42 #include <linux/mmc/host.h>
43 #include <linux/mmc/mmc.h>
44 #include <linux/mmc/sd.h>
45 #include <linux/mmc/sdio.h>
47 #include <asm/mach-ralink/ralink_regs.h>
51 #include "mt6575_sd.h"
53 //#define IRQ_SDC 14 //MT7620 /*FIXME*/
54 #ifdef CONFIG_SOC_MT7621
55 #define RALINK_SYSCTL_BASE 0xbe000000
56 #define RALINK_MSDC_BASE 0xbe130000
58 #define RALINK_SYSCTL_BASE 0xb0000000
59 #define RALINK_MSDC_BASE 0xb0130000
61 #define IRQ_SDC 22 /*FIXME*/
63 #define DRV_NAME "mtk-sd"
65 #if defined(CONFIG_SOC_MT7620)
66 #define HOST_MAX_MCLK (48000000) /* +/- by chhung */
67 #elif defined(CONFIG_SOC_MT7621)
68 #define HOST_MAX_MCLK (50000000) /* +/- by chhung */
70 #define HOST_MIN_MCLK (260000)
72 #define HOST_MAX_BLKSZ (2048)
74 #define MSDC_OCR_AVAIL (MMC_VDD_28_29 | MMC_VDD_29_30 | MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33)
76 #define GPIO_PULL_DOWN (0)
77 #define GPIO_PULL_UP (1)
79 #if 0 /* --- by chhung */
80 #define MSDC_CLKSRC_REG (0xf100000C)
81 #define PDN_REG (0xF1000010)
82 #endif /* end of --- */
84 #define DEFAULT_DEBOUNCE (8) /* 8 cycles */
85 #define DEFAULT_DTOC (40) /* data timeout counter. 65536x40 sclk. */
87 #define CMD_TIMEOUT (HZ / 10) /* 100ms */
88 #define DAT_TIMEOUT (HZ / 2 * 5) /* 500ms x5 */
90 #define MAX_DMA_CNT (64 * 1024 - 512) /* a single transaction for WIFI may be 50K*/
92 #define MAX_GPD_NUM (1 + 1) /* one null gpd */
93 #define MAX_BD_NUM (1024)
94 #define MAX_BD_PER_GPD (MAX_BD_NUM)
96 #define MAX_HW_SGMTS (MAX_BD_NUM)
97 #define MAX_PHY_SGMTS (MAX_BD_NUM)
98 #define MAX_SGMT_SZ (MAX_DMA_CNT)
99 #define MAX_REQ_SZ (MAX_SGMT_SZ * 8)
101 static int cd_active_low = 1;
103 //=================================
104 #define PERI_MSDC0_PDN (15)
105 //#define PERI_MSDC1_PDN (16)
106 //#define PERI_MSDC2_PDN (17)
107 //#define PERI_MSDC3_PDN (18)
109 #if 0 /* --- by chhung */
110 /* gate means clock power down */
111 static int g_clk_gate = 0;
112 #define msdc_gate_clock(id) \
114 g_clk_gate &= ~(1 << ((id) + PERI_MSDC0_PDN)); \
116 /* not like power down register. 1 means clock on. */
117 #define msdc_ungate_clock(id) \
119 g_clk_gate |= 1 << ((id) + PERI_MSDC0_PDN); \
122 // do we need sync object or not
123 void msdc_clk_status(int *status)
125 *status = g_clk_gate;
127 #endif /* end of --- */
130 struct msdc_hw msdc0_hw = {
132 .flags = MSDC_CD_PIN_EN | MSDC_REMOVABLE,
133 // .flags = MSDC_WP_PIN_EN | MSDC_CD_PIN_EN | MSDC_REMOVABLE,
138 static int msdc_rsp[] = {
150 #define msdc_txfifocnt() ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16)
151 #define msdc_rxfifocnt() ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) >> 0)
152 #define msdc_fifo_write32(v) sdr_write32(MSDC_TXDATA, (v))
153 #define msdc_fifo_write8(v) sdr_write8(MSDC_TXDATA, (v))
154 #define msdc_fifo_read32() sdr_read32(MSDC_RXDATA)
155 #define msdc_fifo_read8() sdr_read8(MSDC_RXDATA)
157 #define msdc_dma_on() sdr_clr_bits(MSDC_CFG, MSDC_CFG_PIO)
159 #define msdc_retry(expr, retry, cnt) \
166 retry--; mdelay(1); cnt = backup; \
169 WARN_ON(retry == 0); \
172 static void msdc_reset_hw(struct msdc_host *host)
174 void __iomem *base = host->base;
176 sdr_set_bits(MSDC_CFG, MSDC_CFG_RST);
177 while (sdr_read32(MSDC_CFG) & MSDC_CFG_RST)
181 #define msdc_clr_int() \
183 volatile u32 val = sdr_read32(MSDC_INT); \
184 sdr_write32(MSDC_INT, val); \
187 #define msdc_clr_fifo() \
189 int retry = 3, cnt = 1000; \
190 sdr_set_bits(MSDC_FIFOCS, MSDC_FIFOCS_CLR); \
191 msdc_retry(sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_CLR, retry, cnt); \
194 #define msdc_irq_save(val) \
196 val = sdr_read32(MSDC_INTEN); \
197 sdr_clr_bits(MSDC_INTEN, val); \
200 #define msdc_irq_restore(val) \
202 sdr_set_bits(MSDC_INTEN, val); \
205 /* clock source for host: global */
206 #if defined(CONFIG_SOC_MT7620)
207 static u32 hclks[] = {48000000}; /* +/- by chhung */
208 #elif defined(CONFIG_SOC_MT7621)
209 static u32 hclks[] = {50000000}; /* +/- by chhung */
212 //============================================
213 // the power for msdc host controller: global
214 // always keep the VMC on.
215 //============================================
216 #define msdc_vcore_on(host) \
218 INIT_MSG("[+]VMC ref. count<%d>", ++host->pwr_ref); \
219 (void)hwPowerOn(MT65XX_POWER_LDO_VMC, VOL_3300, "SD"); \
221 #define msdc_vcore_off(host) \
223 INIT_MSG("[-]VMC ref. count<%d>", --host->pwr_ref); \
224 (void)hwPowerDown(MT65XX_POWER_LDO_VMC, "SD"); \
227 //====================================
228 // the vdd output for card: global
229 // always keep the VMCH on.
230 //====================================
231 #define msdc_vdd_on(host) \
233 (void)hwPowerOn(MT65XX_POWER_LDO_VMCH, VOL_3300, "SD"); \
235 #define msdc_vdd_off(host) \
237 (void)hwPowerDown(MT65XX_POWER_LDO_VMCH, "SD"); \
240 #define sdc_is_busy() (sdr_read32(SDC_STS) & SDC_STS_SDCBUSY)
241 #define sdc_is_cmd_busy() (sdr_read32(SDC_STS) & SDC_STS_CMDBUSY)
243 #define sdc_send_cmd(cmd, arg) \
245 sdr_write32(SDC_ARG, (arg)); \
246 sdr_write32(SDC_CMD, (cmd)); \
249 // can modify to read h/w register.
250 //#define is_card_present(h) ((sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 0 : 1);
251 #define is_card_present(h) (((struct msdc_host *)(h))->card_inserted)
255 #define PHYSADDR(a) (((unsigned long)(a)) & 0x1fffffff)
257 #define PHYSADDR(a) ((a) & 0x1fffffff)
260 static unsigned int msdc_do_command(struct msdc_host *host,
261 struct mmc_command *cmd,
263 unsigned long timeout);
265 static int msdc_tune_cmdrsp(struct msdc_host *host, struct mmc_command *cmd);
267 #ifdef MT6575_SD_DEBUG
268 static void msdc_dump_card_status(struct msdc_host *host, u32 status)
270 /* N_MSG is currently a no-op */
272 static char *state[] = {
291 if (status & R1_OUT_OF_RANGE)
292 N_MSG(RSP, "[CARD_STATUS] Out of Range");
293 if (status & R1_ADDRESS_ERROR)
294 N_MSG(RSP, "[CARD_STATUS] Address Error");
295 if (status & R1_BLOCK_LEN_ERROR)
296 N_MSG(RSP, "[CARD_STATUS] Block Len Error");
297 if (status & R1_ERASE_SEQ_ERROR)
298 N_MSG(RSP, "[CARD_STATUS] Erase Seq Error");
299 if (status & R1_ERASE_PARAM)
300 N_MSG(RSP, "[CARD_STATUS] Erase Param");
301 if (status & R1_WP_VIOLATION)
302 N_MSG(RSP, "[CARD_STATUS] WP Violation");
303 if (status & R1_CARD_IS_LOCKED)
304 N_MSG(RSP, "[CARD_STATUS] Card is Locked");
305 if (status & R1_LOCK_UNLOCK_FAILED)
306 N_MSG(RSP, "[CARD_STATUS] Lock/Unlock Failed");
307 if (status & R1_COM_CRC_ERROR)
308 N_MSG(RSP, "[CARD_STATUS] Command CRC Error");
309 if (status & R1_ILLEGAL_COMMAND)
310 N_MSG(RSP, "[CARD_STATUS] Illegal Command");
311 if (status & R1_CARD_ECC_FAILED)
312 N_MSG(RSP, "[CARD_STATUS] Card ECC Failed");
313 if (status & R1_CC_ERROR)
314 N_MSG(RSP, "[CARD_STATUS] CC Error");
315 if (status & R1_ERROR)
316 N_MSG(RSP, "[CARD_STATUS] Error");
317 if (status & R1_UNDERRUN)
318 N_MSG(RSP, "[CARD_STATUS] Underrun");
319 if (status & R1_OVERRUN)
320 N_MSG(RSP, "[CARD_STATUS] Overrun");
321 if (status & R1_CID_CSD_OVERWRITE)
322 N_MSG(RSP, "[CARD_STATUS] CID/CSD Overwrite");
323 if (status & R1_WP_ERASE_SKIP)
324 N_MSG(RSP, "[CARD_STATUS] WP Eraser Skip");
325 if (status & R1_CARD_ECC_DISABLED)
326 N_MSG(RSP, "[CARD_STATUS] Card ECC Disabled");
327 if (status & R1_ERASE_RESET)
328 N_MSG(RSP, "[CARD_STATUS] Erase Reset");
329 if (status & R1_READY_FOR_DATA)
330 N_MSG(RSP, "[CARD_STATUS] Ready for Data");
331 if (status & R1_SWITCH_ERROR)
332 N_MSG(RSP, "[CARD_STATUS] Switch error");
333 if (status & R1_APP_CMD)
334 N_MSG(RSP, "[CARD_STATUS] App Command");
336 N_MSG(RSP, "[CARD_STATUS] '%s' State", state[R1_CURRENT_STATE(status)]);
339 static void msdc_dump_ocr_reg(struct msdc_host *host, u32 resp)
342 N_MSG(RSP, "[OCR] Low Voltage Range");
343 if (resp & (1 << 15))
344 N_MSG(RSP, "[OCR] 2.7-2.8 volt");
345 if (resp & (1 << 16))
346 N_MSG(RSP, "[OCR] 2.8-2.9 volt");
347 if (resp & (1 << 17))
348 N_MSG(RSP, "[OCR] 2.9-3.0 volt");
349 if (resp & (1 << 18))
350 N_MSG(RSP, "[OCR] 3.0-3.1 volt");
351 if (resp & (1 << 19))
352 N_MSG(RSP, "[OCR] 3.1-3.2 volt");
353 if (resp & (1 << 20))
354 N_MSG(RSP, "[OCR] 3.2-3.3 volt");
355 if (resp & (1 << 21))
356 N_MSG(RSP, "[OCR] 3.3-3.4 volt");
357 if (resp & (1 << 22))
358 N_MSG(RSP, "[OCR] 3.4-3.5 volt");
359 if (resp & (1 << 23))
360 N_MSG(RSP, "[OCR] 3.5-3.6 volt");
361 if (resp & (1 << 24))
362 N_MSG(RSP, "[OCR] Switching to 1.8V Accepted (S18A)");
363 if (resp & (1 << 30))
364 N_MSG(RSP, "[OCR] Card Capacity Status (CCS)");
365 if (resp & (1 << 31))
366 N_MSG(RSP, "[OCR] Card Power Up Status (Idle)");
368 N_MSG(RSP, "[OCR] Card Power Up Status (Busy)");
371 static void msdc_dump_rca_resp(struct msdc_host *host, u32 resp)
373 u32 status = (((resp >> 15) & 0x1) << 23) |
374 (((resp >> 14) & 0x1) << 22) |
375 (((resp >> 13) & 0x1) << 19) |
378 N_MSG(RSP, "[RCA] 0x%.4x", resp >> 16);
379 msdc_dump_card_status(host, status);
382 static void msdc_dump_io_resp(struct msdc_host *host, u32 resp)
384 u32 flags = (resp >> 8) & 0xFF;
386 char *state[] = {"DIS", "CMD", "TRN", "RFU"};
388 if (flags & (1 << 7))
389 N_MSG(RSP, "[IO] COM_CRC_ERR");
390 if (flags & (1 << 6))
391 N_MSG(RSP, "[IO] Illegal command");
392 if (flags & (1 << 3))
393 N_MSG(RSP, "[IO] Error");
394 if (flags & (1 << 2))
395 N_MSG(RSP, "[IO] RFU");
396 if (flags & (1 << 1))
397 N_MSG(RSP, "[IO] Function number error");
398 if (flags & (1 << 0))
399 N_MSG(RSP, "[IO] Out of range");
401 N_MSG(RSP, "[IO] State: %s, Data:0x%x", state[(resp >> 12) & 0x3], resp & 0xFF);
405 static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
407 void __iomem *base = host->base;
410 host->timeout_ns = ns;
411 host->timeout_clks = clks;
413 clk_ns = 1000000000UL / host->sclk;
414 timeout = ns / clk_ns + clks;
415 timeout = timeout >> 16; /* in 65536 sclk cycle unit */
416 timeout = timeout > 1 ? timeout - 1 : 0;
417 timeout = timeout > 255 ? 255 : timeout;
419 sdr_set_field(SDC_CFG, SDC_CFG_DTOC, timeout);
421 N_MSG(OPS, "Set read data timeout: %dns %dclks -> %d x 65536 cycles",
422 ns, clks, timeout + 1);
425 static void msdc_tasklet_card(struct work_struct *work)
427 struct msdc_host *host = (struct msdc_host *)container_of(work,
428 struct msdc_host, card_delaywork.work);
429 void __iomem *base = host->base;
434 spin_lock(&host->lock);
436 status = sdr_read32(MSDC_PS);
438 inserted = (status & MSDC_PS_CDSTS) ? 0 : 1;
440 inserted = (status & MSDC_PS_CDSTS) ? 1 : 0;
443 change = host->card_inserted ^ inserted;
444 host->card_inserted = inserted;
446 if (change && !host->suspend) {
448 host->mmc->f_max = HOST_MAX_MCLK; // work around
449 mmc_detect_change(host->mmc, msecs_to_jiffies(20));
451 #else /* Make sure: handle the last interrupt */
452 host->card_inserted = inserted;
454 if (!host->suspend) {
455 host->mmc->f_max = HOST_MAX_MCLK;
456 mmc_detect_change(host->mmc, msecs_to_jiffies(20));
459 IRQ_MSG("card found<%s>", inserted ? "inserted" : "removed");
462 spin_unlock(&host->lock);
465 #if 0 /* --- by chhung */
467 static u8 clk_src_bit[4] = {
471 static void msdc_select_clksrc(struct msdc_host *host, unsigned char clksrc)
474 void __iomem *base = host->base;
477 INIT_MSG("set clock source to <%d>", clksrc);
479 val = sdr_read32(MSDC_CLKSRC_REG);
480 if (sdr_read32(MSDC_ECO_VER) >= 4) {
481 val &= ~(0x3 << clk_src_bit[host->id]);
482 val |= clksrc << clk_src_bit[host->id];
484 val &= ~0x3; val |= clksrc;
486 sdr_write32(MSDC_CLKSRC_REG, val);
488 host->hclk = hclks[clksrc];
489 host->hw->clk_src = clksrc;
491 #endif /* end of --- */
493 static void msdc_set_mclk(struct msdc_host *host, int ddr, unsigned int hz)
495 //struct msdc_hw *hw = host->hw;
496 void __iomem *base = host->base;
501 u32 hclk = host->hclk;
502 //u8 clksrc = hw->clk_src;
504 if (!hz) { // set mmc system clock to 0 ?
505 //ERR_MSG("set mclk to 0!!!");
510 msdc_irq_save(flags);
513 mode = 0x2; /* ddr mode and use divisor */
514 if (hz >= (hclk >> 2)) {
515 div = 1; /* mean div = 1/4 */
516 sclk = hclk >> 2; /* sclk = clk / 4 */
518 div = (hclk + ((hz << 2) - 1)) / (hz << 2);
519 sclk = (hclk >> 2) / div;
521 } else if (hz >= hclk) { /* bug fix */
522 mode = 0x1; /* no divisor and divisor is ignored */
526 mode = 0x0; /* use divisor */
527 if (hz >= (hclk >> 1)) {
528 div = 0; /* mean div = 1/2 */
529 sclk = hclk >> 1; /* sclk = clk / 2 */
531 div = (hclk + ((hz << 2) - 1)) / (hz << 2);
532 sclk = (hclk >> 2) / div;
536 /* set clock mode and divisor */
537 sdr_set_field(MSDC_CFG, MSDC_CFG_CKMOD, mode);
538 sdr_set_field(MSDC_CFG, MSDC_CFG_CKDIV, div);
540 /* wait clock stable */
541 while (!(sdr_read32(MSDC_CFG) & MSDC_CFG_CKSTB))
546 msdc_set_timeout(host, host->timeout_ns, host->timeout_clks); // need?
548 INIT_MSG("================");
549 INIT_MSG("!!! Set<%dKHz> Source<%dKHz> -> sclk<%dKHz>", hz / 1000, hclk / 1000, sclk / 1000);
550 INIT_MSG("================");
552 msdc_irq_restore(flags);
555 /* Fix me. when need to abort */
556 static void msdc_abort_data(struct msdc_host *host)
558 void __iomem *base = host->base;
559 struct mmc_command *stop = host->mrq->stop;
561 ERR_MSG("Need to Abort.");
567 // need to check FIFO count 0 ?
569 if (stop) { /* try to stop, but may not success */
570 ERR_MSG("stop when abort CMD<%d>", stop->opcode);
571 (void)msdc_do_command(host, stop, 0, CMD_TIMEOUT);
574 //if (host->mclk >= 25000000) {
575 // msdc_set_mclk(host, 0, host->mclk >> 1);
579 #if 0 /* --- by chhung */
580 static void msdc_pin_config(struct msdc_host *host, int mode)
582 struct msdc_hw *hw = host->hw;
583 void __iomem *base = host->base;
584 int pull = (mode == MSDC_PIN_PULL_UP) ? GPIO_PULL_UP : GPIO_PULL_DOWN;
587 if (hw->flags & MSDC_WP_PIN_EN) {
588 if (hw->config_gpio_pin) /* NULL */
589 hw->config_gpio_pin(MSDC_WP_PIN, pull);
593 case MSDC_PIN_PULL_UP:
594 //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 1); /* Check & FIXME */
595 //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 0); /* Check & FIXME */
596 sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 1);
597 sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 0);
598 sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 1);
599 sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 0);
601 case MSDC_PIN_PULL_DOWN:
602 //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 0); /* Check & FIXME */
603 //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 1); /* Check & FIXME */
604 sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 0);
605 sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 1);
606 sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 0);
607 sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 1);
609 case MSDC_PIN_PULL_NONE:
611 //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 0); /* Check & FIXME */
612 //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 0); /* Check & FIXME */
613 sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 0);
614 sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 0);
615 sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 0);
616 sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 0);
620 N_MSG(CFG, "Pins mode(%d), down(%d), up(%d)",
621 mode, MSDC_PIN_PULL_DOWN, MSDC_PIN_PULL_UP);
624 void msdc_pin_reset(struct msdc_host *host, int mode)
626 struct msdc_hw *hw = (struct msdc_hw *)host->hw;
627 void __iomem *base = host->base;
628 int pull = (mode == MSDC_PIN_PULL_UP) ? GPIO_PULL_UP : GPIO_PULL_DOWN;
630 /* Config reset pin */
631 if (hw->flags & MSDC_RST_PIN_EN) {
632 if (hw->config_gpio_pin) /* NULL */
633 hw->config_gpio_pin(MSDC_RST_PIN, pull);
635 if (mode == MSDC_PIN_PULL_UP)
636 sdr_clr_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
638 sdr_set_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
642 static void msdc_core_power(struct msdc_host *host, int on)
644 N_MSG(CFG, "Turn %s %s power (copower: %d -> %d)",
645 on ? "on" : "off", "core", host->core_power, on);
647 if (on && host->core_power == 0) {
649 host->core_power = 1;
651 } else if (!on && host->core_power == 1) {
652 msdc_vcore_off(host);
653 host->core_power = 0;
658 static void msdc_host_power(struct msdc_host *host, int on)
660 N_MSG(CFG, "Turn %s %s power ", on ? "on" : "off", "host");
663 //msdc_core_power(host, 1); // need do card detection.
664 msdc_pin_reset(host, MSDC_PIN_PULL_UP);
666 msdc_pin_reset(host, MSDC_PIN_PULL_DOWN);
667 //msdc_core_power(host, 0);
671 static void msdc_card_power(struct msdc_host *host, int on)
673 N_MSG(CFG, "Turn %s %s power ", on ? "on" : "off", "card");
676 msdc_pin_config(host, MSDC_PIN_PULL_UP);
677 //msdc_vdd_on(host); // need todo card detection.
680 //msdc_vdd_off(host);
681 msdc_pin_config(host, MSDC_PIN_PULL_DOWN);
686 static void msdc_set_power_mode(struct msdc_host *host, u8 mode)
688 N_MSG(CFG, "Set power mode(%d)", mode);
690 if (host->power_mode == MMC_POWER_OFF && mode != MMC_POWER_OFF) {
691 msdc_host_power(host, 1);
692 msdc_card_power(host, 1);
693 } else if (host->power_mode != MMC_POWER_OFF && mode == MMC_POWER_OFF) {
694 msdc_card_power(host, 0);
695 msdc_host_power(host, 0);
697 host->power_mode = mode;
699 #endif /* end of --- */
703 register as callback function of WIFI(combo_sdio_register_pm) .
704 can called by msdc_drv_suspend/resume too.
706 static void msdc_pm(pm_message_t state, void *data)
708 struct msdc_host *host = (struct msdc_host *)data;
709 int evt = state.event;
711 if (evt == PM_EVENT_USER_RESUME || evt == PM_EVENT_USER_SUSPEND) {
712 INIT_MSG("USR_%s: suspend<%d> power<%d>",
713 evt == PM_EVENT_USER_RESUME ? "EVENT_USER_RESUME" : "EVENT_USER_SUSPEND",
714 host->suspend, host->power_mode);
717 if (evt == PM_EVENT_SUSPEND || evt == PM_EVENT_USER_SUSPEND) {
718 if (host->suspend) /* already suspend */ /* default 0*/
721 /* for memory card. already power off by mmc */
722 if (evt == PM_EVENT_SUSPEND && host->power_mode == MMC_POWER_OFF)
726 host->pm_state = state; /* default PMSG_RESUME */
728 } else if (evt == PM_EVENT_RESUME || evt == PM_EVENT_USER_RESUME) {
729 if (!host->suspend) {
730 //ERR_MSG("warning: already resume");
734 /* No PM resume when USR suspend */
735 if (evt == PM_EVENT_RESUME && host->pm_state.event == PM_EVENT_USER_SUSPEND) {
736 ERR_MSG("PM Resume when in USR Suspend"); /* won't happen. */
741 host->pm_state = state;
747 /*--------------------------------------------------------------------------*/
748 /* mmc_host_ops members */
749 /*--------------------------------------------------------------------------*/
750 static unsigned int msdc_command_start(struct msdc_host *host,
751 struct mmc_command *cmd,
752 int tune, /* not used */
753 unsigned long timeout)
755 void __iomem *base = host->base;
756 u32 opcode = cmd->opcode;
758 u32 wints = MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO |
759 MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO |
760 MSDC_INT_ACMD19_DONE;
765 /* Protocol layer does not provide response type, but our hardware needs
766 * to know exact type, not just size!
768 if (opcode == MMC_SEND_OP_COND || opcode == SD_APP_OP_COND) {
770 } else if (opcode == MMC_SET_RELATIVE_ADDR) {
771 resp = (mmc_cmd_type(cmd) == MMC_CMD_BCR) ? RESP_R6 : RESP_R1;
772 } else if (opcode == MMC_FAST_IO) {
774 } else if (opcode == MMC_GO_IRQ_STATE) {
776 } else if (opcode == MMC_SELECT_CARD) {
777 resp = (cmd->arg != 0) ? RESP_R1B : RESP_NONE;
778 } else if (opcode == SD_IO_RW_DIRECT || opcode == SD_IO_RW_EXTENDED) {
779 resp = RESP_R1; /* SDIO workaround. */
780 } else if (opcode == SD_SEND_IF_COND && (mmc_cmd_type(cmd) == MMC_CMD_BCR)) {
783 switch (mmc_resp_type(cmd)) {
805 * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 |
806 * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
808 rawcmd = opcode | msdc_rsp[resp] << 7 | host->blksz << 16;
810 if (opcode == MMC_READ_MULTIPLE_BLOCK) {
812 } else if (opcode == MMC_READ_SINGLE_BLOCK) {
814 } else if (opcode == MMC_WRITE_MULTIPLE_BLOCK) {
815 rawcmd |= ((2 << 11) | (1 << 13));
816 } else if (opcode == MMC_WRITE_BLOCK) {
817 rawcmd |= ((1 << 11) | (1 << 13));
818 } else if (opcode == SD_IO_RW_EXTENDED) {
819 if (cmd->data->flags & MMC_DATA_WRITE)
821 if (cmd->data->blocks > 1)
825 } else if (opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int)-1) {
827 } else if ((opcode == SD_APP_SEND_SCR) ||
828 (opcode == SD_APP_SEND_NUM_WR_BLKS) ||
829 (opcode == SD_SWITCH && (mmc_cmd_type(cmd) == MMC_CMD_ADTC)) ||
830 (opcode == SD_APP_SD_STATUS && (mmc_cmd_type(cmd) == MMC_CMD_ADTC)) ||
831 (opcode == MMC_SEND_EXT_CSD && (mmc_cmd_type(cmd) == MMC_CMD_ADTC))) {
833 } else if (opcode == MMC_STOP_TRANSMISSION) {
835 rawcmd &= ~(0x0FFF << 16);
838 N_MSG(CMD, "CMD<%d><0x%.8x> Arg<0x%.8x>", opcode, rawcmd, cmd->arg);
840 tmo = jiffies + timeout;
842 if (opcode == MMC_SEND_STATUS) {
844 if (!sdc_is_cmd_busy())
847 if (time_after(jiffies, tmo)) {
848 ERR_MSG("XXX cmd_busy timeout: before CMD<%d>", opcode);
849 cmd->error = -ETIMEDOUT;
858 if (time_after(jiffies, tmo)) {
859 ERR_MSG("XXX sdc_busy timeout: before CMD<%d>", opcode);
860 cmd->error = -ETIMEDOUT;
867 //BUG_ON(in_interrupt());
869 host->cmd_rsp = resp;
871 init_completion(&host->cmd_done);
873 sdr_set_bits(MSDC_INTEN, wints);
874 sdc_send_cmd(rawcmd, cmd->arg);
880 static unsigned int msdc_command_resp(struct msdc_host *host,
881 struct mmc_command *cmd,
883 unsigned long timeout)
884 __must_hold(&host->lock)
886 void __iomem *base = host->base;
887 u32 opcode = cmd->opcode;
890 u32 wints = MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO |
891 MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO |
892 MSDC_INT_ACMD19_DONE;
894 resp = host->cmd_rsp;
896 BUG_ON(in_interrupt());
897 //init_completion(&host->cmd_done);
898 //sdr_set_bits(MSDC_INTEN, wints);
900 spin_unlock(&host->lock);
901 if (!wait_for_completion_timeout(&host->cmd_done, 10 * timeout)) {
902 ERR_MSG("XXX CMD<%d> wait_for_completion timeout ARG<0x%.8x>", opcode, cmd->arg);
903 cmd->error = -ETIMEDOUT;
906 spin_lock(&host->lock);
908 sdr_clr_bits(MSDC_INTEN, wints);
912 #ifdef MT6575_SD_DEBUG
915 N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)", opcode, cmd->error, resp);
918 N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)= %.8x %.8x %.8x %.8x",
919 opcode, cmd->error, resp, cmd->resp[0], cmd->resp[1],
920 cmd->resp[2], cmd->resp[3]);
922 default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
923 N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)= 0x%.8x",
924 opcode, cmd->error, resp, cmd->resp[0]);
925 if (cmd->error == 0) {
929 msdc_dump_card_status(host, cmd->resp[0]);
932 msdc_dump_ocr_reg(host, cmd->resp[0]);
935 msdc_dump_io_resp(host, cmd->resp[0]);
938 msdc_dump_rca_resp(host, cmd->resp[0]);
946 /* do we need to save card's RCA when SD_SEND_RELATIVE_ADDR */
951 /* memory card CRC */
952 if (host->hw->flags & MSDC_REMOVABLE && cmd->error == -EIO) {
953 if (sdr_read32(SDC_CMD) & 0x1800) { /* check if has data phase */
954 msdc_abort_data(host);
961 cmd->error = msdc_tune_cmdrsp(host, cmd);
965 /* if (resp == RESP_R1B) {
966 while ((sdr_read32(MSDC_PS) & 0x10000) != 0x10000);
968 /* CMD12 Error Handle */
973 static unsigned int msdc_do_command(struct msdc_host *host,
974 struct mmc_command *cmd,
976 unsigned long timeout)
978 if (msdc_command_start(host, cmd, tune, timeout))
981 if (msdc_command_resp(host, cmd, tune, timeout))
986 N_MSG(CMD, " return<%d> resp<0x%.8x>", cmd->error, cmd->resp[0]);
990 #if 0 /* --- by chhung */
991 // DMA resume / start / stop
992 static void msdc_dma_resume(struct msdc_host *host)
994 void __iomem *base = host->base;
996 sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_RESUME, 1);
998 N_MSG(DMA, "DMA resume");
1000 #endif /* end of --- */
1002 static void msdc_dma_start(struct msdc_host *host)
1004 void __iomem *base = host->base;
1005 u32 wints = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR;
1007 sdr_set_bits(MSDC_INTEN, wints);
1008 //dsb(); /* --- by chhung */
1009 sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1);
1011 N_MSG(DMA, "DMA start");
1014 static void msdc_dma_stop(struct msdc_host *host)
1016 void __iomem *base = host->base;
1018 u32 wints = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR;
1020 N_MSG(DMA, "DMA status: 0x%.8x", sdr_read32(MSDC_DMA_CFG));
1021 //while (sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS);
1023 sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, 1);
1024 while (sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS)
1027 //dsb(); /* --- by chhung */
1028 sdr_clr_bits(MSDC_INTEN, wints); /* Not just xfer_comp */
1030 N_MSG(DMA, "DMA stop");
1034 static u8 msdc_dma_calcs(u8 *buf, u32 len)
1038 for (i = 0; i < len; i++)
1040 return 0xFF - (u8)sum;
1043 /* gpd bd setup + dma registers */
1044 static void msdc_dma_config(struct msdc_host *host, struct msdc_dma *dma)
1046 void __iomem *base = host->base;
1047 //u32 i, j, num, bdlen, arg, xfersz;
1049 struct scatterlist *sg;
1053 switch (dma->mode) {
1054 case MSDC_MODE_DMA_BASIC:
1055 BUG_ON(host->xfer_size > 65535);
1056 BUG_ON(dma->sglen != 1);
1057 sdr_write32(MSDC_DMA_SA, PHYSADDR(sg_dma_address(sg)));
1058 sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_LASTBUF, 1);
1059 //#if defined (CONFIG_RALINK_MT7620)
1060 if (ralink_soc == MT762X_SOC_MT7620A)
1061 sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_XFERSZ, sg_dma_len(sg));
1062 //#elif defined (CONFIG_RALINK_MT7621) || defined (CONFIG_RALINK_MT7628)
1064 sdr_write32((void __iomem *)(RALINK_MSDC_BASE + 0xa8), sg_dma_len(sg));
1066 sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_BRUSTSZ,
1068 sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_MODE, 0);
1070 case MSDC_MODE_DMA_DESC:
1072 /* calculate the required number of gpd */
1073 num = (dma->sglen + MAX_BD_PER_GPD - 1) / MAX_BD_PER_GPD;
1081 gpd->hwo = 1; /* hw will clear it */
1083 gpd->chksum = 0; /* need to clear first. */
1084 gpd->chksum = msdc_dma_calcs((u8 *)gpd, 16);
1087 for_each_sg(dma->sg, sg, dma->sglen, j) {
1090 bd[j].ptr = (void *)sg_dma_address(sg);
1091 bd[j].buflen = sg_dma_len(sg);
1093 if (j == dma->sglen - 1)
1094 bd[j].eol = 1; /* the last bd */
1098 bd[j].chksum = 0; /* checksume need to clear first */
1099 bd[j].chksum = msdc_dma_calcs((u8 *)(&bd[j]), 16);
1102 sdr_set_field(MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, 1);
1103 sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_BRUSTSZ,
1105 sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_MODE, 1);
1107 sdr_write32(MSDC_DMA_SA, PHYSADDR((u32)dma->gpd_addr));
1114 N_MSG(DMA, "DMA_CTRL = 0x%x", sdr_read32(MSDC_DMA_CTRL));
1115 N_MSG(DMA, "DMA_CFG = 0x%x", sdr_read32(MSDC_DMA_CFG));
1116 N_MSG(DMA, "DMA_SA = 0x%x", sdr_read32(MSDC_DMA_SA));
1120 static void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma,
1121 struct scatterlist *sg, unsigned int sglen)
1123 BUG_ON(sglen > MAX_BD_NUM); /* not support currently */
1128 dma->mode = MSDC_MODE_DMA_DESC;
1130 N_MSG(DMA, "DMA mode<%d> sglen<%d> xfersz<%d>", dma->mode, dma->sglen,
1133 msdc_dma_config(host, dma);
1136 static int msdc_do_request(struct mmc_host *mmc, struct mmc_request *mrq)
1137 __must_hold(&host->lock)
1139 struct msdc_host *host = mmc_priv(mmc);
1140 struct mmc_command *cmd;
1141 struct mmc_data *data;
1142 void __iomem *base = host->base;
1144 int read = 1, send_type = 0;
1149 BUG_ON(mmc == NULL);
1150 BUG_ON(mrq == NULL);
1155 data = mrq->cmd->data;
1157 #if 0 /* --- by chhung */
1159 N_MSG(OPS, "enable clock!");
1160 msdc_ungate_clock(host->id);
1162 #endif /* end of --- */
1165 send_type = SND_CMD;
1166 if (msdc_do_command(host, cmd, 1, CMD_TIMEOUT) != 0)
1169 BUG_ON(data->blksz > HOST_MAX_BLKSZ);
1170 send_type = SND_DAT;
1173 read = data->flags & MMC_DATA_READ ? 1 : 0;
1175 host->xfer_size = data->blocks * data->blksz;
1176 host->blksz = data->blksz;
1179 if ((host->timeout_ns != data->timeout_ns) ||
1180 (host->timeout_clks != data->timeout_clks)) {
1181 msdc_set_timeout(host, data->timeout_ns, data->timeout_clks);
1185 sdr_write32(SDC_BLK_NUM, data->blocks);
1186 //msdc_clr_fifo(); /* no need */
1188 msdc_dma_on(); /* enable DMA mode first!! */
1189 init_completion(&host->xfer_done);
1191 /* start the command first*/
1192 if (msdc_command_start(host, cmd, 1, CMD_TIMEOUT) != 0)
1195 data->sg_count = dma_map_sg(mmc_dev(mmc), data->sg,
1197 mmc_get_dma_dir(data));
1198 msdc_dma_setup(host, &host->dma, data->sg,
1201 /* then wait command done */
1202 if (msdc_command_resp(host, cmd, 1, CMD_TIMEOUT) != 0)
1205 /* for read, the data coming too fast, then CRC error
1206 start DMA no business with CRC. */
1207 //init_completion(&host->xfer_done);
1208 msdc_dma_start(host);
1210 spin_unlock(&host->lock);
1211 if (!wait_for_completion_timeout(&host->xfer_done, DAT_TIMEOUT)) {
1212 ERR_MSG("XXX CMD<%d> wait xfer_done<%d> timeout!!", cmd->opcode, data->blocks * data->blksz);
1213 ERR_MSG(" DMA_SA = 0x%x", sdr_read32(MSDC_DMA_SA));
1214 ERR_MSG(" DMA_CA = 0x%x", sdr_read32(MSDC_DMA_CA));
1215 ERR_MSG(" DMA_CTRL = 0x%x", sdr_read32(MSDC_DMA_CTRL));
1216 ERR_MSG(" DMA_CFG = 0x%x", sdr_read32(MSDC_DMA_CFG));
1217 data->error = -ETIMEDOUT;
1219 msdc_reset_hw(host);
1223 spin_lock(&host->lock);
1224 msdc_dma_stop(host);
1226 /* Last: stop transfer */
1228 if (msdc_do_command(host, data->stop, 0, CMD_TIMEOUT) != 0)
1236 dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len,
1237 mmc_get_dma_dir(data));
1240 #if 0 // don't stop twice!
1241 if (host->hw->flags & MSDC_REMOVABLE && data->error) {
1242 msdc_abort_data(host);
1243 /* reset in IRQ, stop command has issued. -> No need */
1247 N_MSG(OPS, "CMD<%d> data<%s %s> blksz<%d> block<%d> error<%d>", cmd->opcode, (dma ? "dma" : "pio"),
1248 (read ? "read " : "write"), data->blksz, data->blocks, data->error);
1251 #if 0 /* --- by chhung */
1254 if (send_type == SND_CMD) {
1255 if (cmd->opcode == MMC_SEND_STATUS) {
1256 if ((cmd->resp[0] & CARD_READY_FOR_DATA) || (CARD_CURRENT_STATE(cmd->resp[0]) != 7)) {
1257 N_MSG(OPS, "disable clock, CMD13 IDLE");
1258 msdc_gate_clock(host->id);
1261 N_MSG(OPS, "disable clock, CMD<%d>", cmd->opcode);
1262 msdc_gate_clock(host->id);
1266 N_MSG(OPS, "disable clock!!! Read CMD<%d>", cmd->opcode);
1267 msdc_gate_clock(host->id);
1272 msdc_gate_clock(host->id);
1274 #endif /* end of --- */
1276 if (mrq->cmd->error)
1277 host->error = 0x001;
1278 if (mrq->data && mrq->data->error)
1279 host->error |= 0x010;
1280 if (mrq->stop && mrq->stop->error)
1281 host->error |= 0x100;
1283 //if (host->error) ERR_MSG("host->error<%d>", host->error);
1288 static int msdc_app_cmd(struct mmc_host *mmc, struct msdc_host *host)
1290 struct mmc_command cmd;
1291 struct mmc_request mrq;
1294 memset(&cmd, 0, sizeof(struct mmc_command));
1295 cmd.opcode = MMC_APP_CMD;
1296 #if 0 /* bug: we meet mmc->card is null when ACMD6 */
1297 cmd.arg = mmc->card->rca << 16;
1299 cmd.arg = host->app_cmd_arg;
1301 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1303 memset(&mrq, 0, sizeof(struct mmc_request));
1304 mrq.cmd = &cmd; cmd.mrq = &mrq;
1307 err = msdc_do_command(host, &cmd, 0, CMD_TIMEOUT);
1311 static int msdc_tune_cmdrsp(struct msdc_host *host, struct mmc_command *cmd)
1314 void __iomem *base = host->base;
1315 u32 rsmpl, cur_rsmpl, orig_rsmpl;
1316 u32 rrdly, cur_rrdly = 0xffffffff, orig_rrdly;
1319 /* ==== don't support 3.0 now ====
1321 2: PAD_CMD_RESP_RXDLY[26:22]
1322 ==========================*/
1324 // save the previous tune result
1325 sdr_get_field(MSDC_IOCON, MSDC_IOCON_RSPL, &orig_rsmpl);
1326 sdr_get_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_CMDRRDLY, &orig_rrdly);
1330 for (rsmpl = 0; rsmpl < 2; rsmpl++) {
1331 /* Lv1: R_SMPL[1] */
1332 cur_rsmpl = (orig_rsmpl + rsmpl) % 2;
1337 sdr_set_field(MSDC_IOCON, MSDC_IOCON_RSPL, cur_rsmpl);
1339 if (host->app_cmd) {
1340 result = msdc_app_cmd(host->mmc, host);
1342 ERR_MSG("TUNE_CMD app_cmd<%d> failed: RESP_RXDLY<%d>,R_SMPL<%d>",
1343 host->mrq->cmd->opcode, cur_rrdly, cur_rsmpl);
1347 result = msdc_do_command(host, cmd, 0, CMD_TIMEOUT); // not tune.
1348 ERR_MSG("TUNE_CMD<%d> %s PAD_CMD_RESP_RXDLY[26:22]<%d> R_SMPL[1]<%d>", cmd->opcode,
1349 (result == 0) ? "PASS" : "FAIL", cur_rrdly, cur_rsmpl);
1353 if (result != -EIO) {
1354 ERR_MSG("TUNE_CMD<%d> Error<%d> not -EIO", cmd->opcode, result);
1359 if (sdr_read32(SDC_CMD) & 0x1800) { /* check if has data phase */
1360 msdc_abort_data(host);
1364 /* Lv2: PAD_CMD_RESP_RXDLY[26:22] */
1365 cur_rrdly = (orig_rrdly + rrdly + 1) % 32;
1366 sdr_set_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_CMDRRDLY, cur_rrdly);
1367 } while (++rrdly < 32);
1372 /* Support SD2.0 Only */
1373 static int msdc_tune_bread(struct mmc_host *mmc, struct mmc_request *mrq)
1375 struct msdc_host *host = mmc_priv(mmc);
1376 void __iomem *base = host->base;
1379 u32 rxdly, cur_rxdly0, cur_rxdly1;
1380 u32 dsmpl, cur_dsmpl, orig_dsmpl;
1381 u32 cur_dat0, cur_dat1, cur_dat2, cur_dat3;
1382 u32 cur_dat4, cur_dat5, cur_dat6, cur_dat7;
1383 u32 orig_dat0, orig_dat1, orig_dat2, orig_dat3;
1384 u32 orig_dat4, orig_dat5, orig_dat6, orig_dat7;
1388 sdr_get_field(MSDC_IOCON, MSDC_IOCON_DSPL, &orig_dsmpl);
1390 /* Tune Method 2. */
1391 sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
1395 for (dsmpl = 0; dsmpl < 2; dsmpl++) {
1396 cur_dsmpl = (orig_dsmpl + dsmpl) % 2;
1401 sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL, cur_dsmpl);
1403 if (host->app_cmd) {
1404 result = msdc_app_cmd(host->mmc, host);
1406 ERR_MSG("TUNE_BREAD app_cmd<%d> failed", host->mrq->cmd->opcode);
1410 result = msdc_do_request(mmc, mrq);
1412 sdr_get_field(SDC_DCRC_STS,
1413 SDC_DCRC_STS_POS | SDC_DCRC_STS_NEG,
1416 dcrc &= ~SDC_DCRC_STS_NEG;
1417 ERR_MSG("TUNE_BREAD<%s> dcrc<0x%x> DATRDDLY0/1<0x%x><0x%x> dsmpl<0x%x>",
1418 (result == 0 && dcrc == 0) ? "PASS" : "FAIL", dcrc,
1419 sdr_read32(MSDC_DAT_RDDLY0), sdr_read32(MSDC_DAT_RDDLY1), cur_dsmpl);
1421 /* Fix me: result is 0, but dcrc is still exist */
1422 if (result == 0 && dcrc == 0) {
1425 /* there is a case: command timeout, and data phase not processed */
1426 if (mrq->data->error != 0 &&
1427 mrq->data->error != -EIO) {
1428 ERR_MSG("TUNE_READ: result<0x%x> cmd_error<%d> data_error<%d>",
1429 result, mrq->cmd->error, mrq->data->error);
1435 cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
1436 cur_rxdly1 = sdr_read32(MSDC_DAT_RDDLY1);
1438 /* E1 ECO. YD: Reverse */
1439 if (sdr_read32(MSDC_ECO_VER) >= 4) {
1440 orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
1441 orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
1442 orig_dat2 = (cur_rxdly0 >> 8) & 0x1F;
1443 orig_dat3 = (cur_rxdly0 >> 0) & 0x1F;
1444 orig_dat4 = (cur_rxdly1 >> 24) & 0x1F;
1445 orig_dat5 = (cur_rxdly1 >> 16) & 0x1F;
1446 orig_dat6 = (cur_rxdly1 >> 8) & 0x1F;
1447 orig_dat7 = (cur_rxdly1 >> 0) & 0x1F;
1449 orig_dat0 = (cur_rxdly0 >> 0) & 0x1F;
1450 orig_dat1 = (cur_rxdly0 >> 8) & 0x1F;
1451 orig_dat2 = (cur_rxdly0 >> 16) & 0x1F;
1452 orig_dat3 = (cur_rxdly0 >> 24) & 0x1F;
1453 orig_dat4 = (cur_rxdly1 >> 0) & 0x1F;
1454 orig_dat5 = (cur_rxdly1 >> 8) & 0x1F;
1455 orig_dat6 = (cur_rxdly1 >> 16) & 0x1F;
1456 orig_dat7 = (cur_rxdly1 >> 24) & 0x1F;
1460 cur_dat0 = (dcrc & (1 << 0) || dcrc & (1 << 8)) ? ((orig_dat0 + 1) % 32) : orig_dat0;
1461 cur_dat1 = (dcrc & (1 << 1) || dcrc & (1 << 9)) ? ((orig_dat1 + 1) % 32) : orig_dat1;
1462 cur_dat2 = (dcrc & (1 << 2) || dcrc & (1 << 10)) ? ((orig_dat2 + 1) % 32) : orig_dat2;
1463 cur_dat3 = (dcrc & (1 << 3) || dcrc & (1 << 11)) ? ((orig_dat3 + 1) % 32) : orig_dat3;
1465 cur_dat0 = (dcrc & (1 << 0)) ? ((orig_dat0 + 1) % 32) : orig_dat0;
1466 cur_dat1 = (dcrc & (1 << 1)) ? ((orig_dat1 + 1) % 32) : orig_dat1;
1467 cur_dat2 = (dcrc & (1 << 2)) ? ((orig_dat2 + 1) % 32) : orig_dat2;
1468 cur_dat3 = (dcrc & (1 << 3)) ? ((orig_dat3 + 1) % 32) : orig_dat3;
1470 cur_dat4 = (dcrc & (1 << 4)) ? ((orig_dat4 + 1) % 32) : orig_dat4;
1471 cur_dat5 = (dcrc & (1 << 5)) ? ((orig_dat5 + 1) % 32) : orig_dat5;
1472 cur_dat6 = (dcrc & (1 << 6)) ? ((orig_dat6 + 1) % 32) : orig_dat6;
1473 cur_dat7 = (dcrc & (1 << 7)) ? ((orig_dat7 + 1) % 32) : orig_dat7;
1475 cur_rxdly0 = (cur_dat0 << 24) | (cur_dat1 << 16) | (cur_dat2 << 8) | (cur_dat3 << 0);
1476 cur_rxdly1 = (cur_dat4 << 24) | (cur_dat5 << 16) | (cur_dat6 << 8) | (cur_dat7 << 0);
1478 sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
1479 sdr_write32(MSDC_DAT_RDDLY1, cur_rxdly1);
1481 } while (++rxdly < 32);
1487 static int msdc_tune_bwrite(struct mmc_host *mmc, struct mmc_request *mrq)
1489 struct msdc_host *host = mmc_priv(mmc);
1490 void __iomem *base = host->base;
1492 u32 wrrdly, cur_wrrdly = 0xffffffff, orig_wrrdly;
1493 u32 dsmpl, cur_dsmpl, orig_dsmpl;
1494 u32 rxdly, cur_rxdly0;
1495 u32 orig_dat0, orig_dat1, orig_dat2, orig_dat3;
1496 u32 cur_dat0, cur_dat1, cur_dat2, cur_dat3;
1500 // MSDC_IOCON_DDR50CKD need to check. [Fix me]
1502 sdr_get_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_DATWRDLY, &orig_wrrdly);
1503 sdr_get_field(MSDC_IOCON, MSDC_IOCON_DSPL, &orig_dsmpl);
1505 /* Tune Method 2. just DAT0 */
1506 sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
1507 cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
1509 /* E1 ECO. YD: Reverse */
1510 if (sdr_read32(MSDC_ECO_VER) >= 4) {
1511 orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
1512 orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
1513 orig_dat2 = (cur_rxdly0 >> 8) & 0x1F;
1514 orig_dat3 = (cur_rxdly0 >> 0) & 0x1F;
1516 orig_dat0 = (cur_rxdly0 >> 0) & 0x1F;
1517 orig_dat1 = (cur_rxdly0 >> 8) & 0x1F;
1518 orig_dat2 = (cur_rxdly0 >> 16) & 0x1F;
1519 orig_dat3 = (cur_rxdly0 >> 24) & 0x1F;
1526 for (dsmpl = 0; dsmpl < 2; dsmpl++) {
1527 cur_dsmpl = (orig_dsmpl + dsmpl) % 2;
1532 sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL, cur_dsmpl);
1534 if (host->app_cmd) {
1535 result = msdc_app_cmd(host->mmc, host);
1537 ERR_MSG("TUNE_BWRITE app_cmd<%d> failed", host->mrq->cmd->opcode);
1541 result = msdc_do_request(mmc, mrq);
1543 ERR_MSG("TUNE_BWRITE<%s> DSPL<%d> DATWRDLY<%d> MSDC_DAT_RDDLY0<0x%x>",
1544 result == 0 ? "PASS" : "FAIL",
1545 cur_dsmpl, cur_wrrdly, cur_rxdly0);
1550 /* there is a case: command timeout, and data phase not processed */
1551 if (mrq->data->error != -EIO) {
1552 ERR_MSG("TUNE_READ: result<0x%x> cmd_error<%d> data_error<%d>",
1553 result, mrq->cmd->error, mrq->data->error);
1558 cur_wrrdly = (orig_wrrdly + wrrdly + 1) % 32;
1559 sdr_set_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_DATWRDLY, cur_wrrdly);
1560 } while (++wrrdly < 32);
1562 cur_dat0 = (orig_dat0 + rxdly) % 32; /* only adjust bit-1 for crc */
1563 cur_dat1 = orig_dat1;
1564 cur_dat2 = orig_dat2;
1565 cur_dat3 = orig_dat3;
1567 cur_rxdly0 = (cur_dat0 << 24) | (cur_dat1 << 16) | (cur_dat2 << 8) | (cur_dat3 << 0);
1568 sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
1569 } while (++rxdly < 32);
1575 static int msdc_get_card_status(struct mmc_host *mmc, struct msdc_host *host, u32 *status)
1577 struct mmc_command cmd;
1578 struct mmc_request mrq;
1581 memset(&cmd, 0, sizeof(struct mmc_command));
1582 cmd.opcode = MMC_SEND_STATUS;
1584 cmd.arg = mmc->card->rca << 16;
1586 ERR_MSG("cmd13 mmc card is null");
1587 cmd.arg = host->app_cmd_arg;
1589 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
1591 memset(&mrq, 0, sizeof(struct mmc_request));
1592 mrq.cmd = &cmd; cmd.mrq = &mrq;
1595 err = msdc_do_command(host, &cmd, 1, CMD_TIMEOUT);
1598 *status = cmd.resp[0];
1603 static int msdc_check_busy(struct mmc_host *mmc, struct msdc_host *host)
1609 err = msdc_get_card_status(mmc, host, &status);
1613 ERR_MSG("cmd<13> resp<0x%x>", status);
1614 } while (R1_CURRENT_STATE(status) == 7);
1619 /* failed when msdc_do_request */
1620 static int msdc_tune_request(struct mmc_host *mmc, struct mmc_request *mrq)
1622 struct msdc_host *host = mmc_priv(mmc);
1623 struct mmc_command *cmd;
1624 struct mmc_data *data;
1625 //u32 base = host->base;
1629 data = mrq->cmd->data;
1631 read = data->flags & MMC_DATA_READ ? 1 : 0;
1634 if (data->error == -EIO)
1635 ret = msdc_tune_bread(mmc, mrq);
1637 ret = msdc_check_busy(mmc, host);
1639 ERR_MSG("XXX cmd13 wait program done failed");
1643 /* Fix me: don't care card status? */
1644 ret = msdc_tune_bwrite(mmc, mrq);
1651 static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
1653 struct msdc_host *host = mmc_priv(mmc);
1655 //=== for sdio profile ===
1656 #if 0 /* --- by chhung */
1657 u32 old_H32, old_L32, new_H32, new_L32;
1658 u32 ticks = 0, opcode = 0, sizes = 0, bRx = 0;
1659 #endif /* end of --- */
1663 /* start to process */
1664 spin_lock(&host->lock);
1665 #if 0 /* --- by chhung */
1666 if (sdio_pro_enable) { //=== for sdio profile ===
1667 if (mrq->cmd->opcode == 52 || mrq->cmd->opcode == 53)
1668 GPT_GetCounter64(&old_L32, &old_H32);
1670 #endif /* end of --- */
1674 if (msdc_do_request(mmc, mrq)) {
1675 if (host->hw->flags & MSDC_REMOVABLE && ralink_soc == MT762X_SOC_MT7621AT && mrq->data && mrq->data->error)
1676 msdc_tune_request(mmc, mrq);
1679 /* ==== when request done, check if app_cmd ==== */
1680 if (mrq->cmd->opcode == MMC_APP_CMD) {
1682 host->app_cmd_arg = mrq->cmd->arg; /* save the RCA */
1685 //host->app_cmd_arg = 0;
1690 #if 0 /* --- by chhung */
1691 //=== for sdio profile ===
1692 if (sdio_pro_enable) {
1693 if (mrq->cmd->opcode == 52 || mrq->cmd->opcode == 53) {
1694 GPT_GetCounter64(&new_L32, &new_H32);
1695 ticks = msdc_time_calc(old_L32, old_H32, new_L32, new_H32);
1697 opcode = mrq->cmd->opcode;
1698 if (mrq->cmd->data) {
1699 sizes = mrq->cmd->data->blocks * mrq->cmd->data->blksz;
1700 bRx = mrq->cmd->data->flags & MMC_DATA_READ ? 1 : 0;
1702 bRx = mrq->cmd->arg & 0x80000000 ? 1 : 0;
1705 if (!mrq->cmd->error)
1706 msdc_performance(opcode, sizes, bRx, ticks);
1709 #endif /* end of --- */
1710 spin_unlock(&host->lock);
1712 mmc_request_done(mmc, mrq);
1717 /* called by ops.set_ios */
1718 static void msdc_set_buswidth(struct msdc_host *host, u32 width)
1720 void __iomem *base = host->base;
1721 u32 val = sdr_read32(SDC_CFG);
1723 val &= ~SDC_CFG_BUSWIDTH;
1727 case MMC_BUS_WIDTH_1:
1729 val |= (MSDC_BUS_1BITS << 16);
1731 case MMC_BUS_WIDTH_4:
1732 val |= (MSDC_BUS_4BITS << 16);
1734 case MMC_BUS_WIDTH_8:
1735 val |= (MSDC_BUS_8BITS << 16);
1739 sdr_write32(SDC_CFG, val);
1741 N_MSG(CFG, "Bus Width = %d", width);
1745 static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1747 struct msdc_host *host = mmc_priv(mmc);
1748 void __iomem *base = host->base;
1751 #ifdef MT6575_SD_DEBUG
1752 static char *vdd[] = {
1753 "1.50v", "1.55v", "1.60v", "1.65v", "1.70v", "1.80v", "1.90v",
1754 "2.00v", "2.10v", "2.20v", "2.30v", "2.40v", "2.50v", "2.60v",
1755 "2.70v", "2.80v", "2.90v", "3.00v", "3.10v", "3.20v", "3.30v",
1756 "3.40v", "3.50v", "3.60v"
1758 static char *power_mode[] = {
1761 static char *bus_mode[] = {
1762 "UNKNOWN", "OPENDRAIN", "PUSHPULL"
1764 static char *timing[] = {
1765 "LEGACY", "MMC_HS", "SD_HS"
1768 printk("SET_IOS: CLK(%dkHz), BUS(%s), BW(%u), PWR(%s), VDD(%s), TIMING(%s)",
1769 ios->clock / 1000, bus_mode[ios->bus_mode],
1770 (ios->bus_width == MMC_BUS_WIDTH_4) ? 4 : 1,
1771 power_mode[ios->power_mode], vdd[ios->vdd], timing[ios->timing]);
1774 msdc_set_buswidth(host, ios->bus_width);
1776 /* Power control ??? */
1777 switch (ios->power_mode) {
1780 // msdc_set_power_mode(host, ios->power_mode); /* --- by chhung */
1783 host->power_mode = MMC_POWER_ON;
1790 if (host->mclk != ios->clock) {
1791 if (ios->clock > 25000000) {
1792 //if (!(host->hw->flags & MSDC_REMOVABLE)) {
1793 INIT_MSG("SD data latch edge<%d>", MSDC_SMPL_FALLING);
1794 sdr_set_field(MSDC_IOCON, MSDC_IOCON_RSPL,
1796 sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL,
1798 //} /* for tuning debug */
1799 } else { /* default value */
1800 sdr_write32(MSDC_IOCON, 0x00000000);
1801 // sdr_write32(MSDC_DAT_RDDLY0, 0x00000000);
1802 sdr_write32(MSDC_DAT_RDDLY0, 0x10101010); // for MT7620 E2 and afterward
1803 sdr_write32(MSDC_DAT_RDDLY1, 0x00000000);
1804 // sdr_write32(MSDC_PAD_TUNE, 0x00000000);
1805 sdr_write32(MSDC_PAD_TUNE, 0x84101010); // for MT7620 E2 and afterward
1807 msdc_set_mclk(host, ddr, ios->clock);
1812 static int msdc_ops_get_ro(struct mmc_host *mmc)
1814 struct msdc_host *host = mmc_priv(mmc);
1815 void __iomem *base = host->base;
1816 unsigned long flags;
1819 if (host->hw->flags & MSDC_WP_PIN_EN) { /* set for card */
1820 spin_lock_irqsave(&host->lock, flags);
1821 ro = (sdr_read32(MSDC_PS) >> 31);
1822 spin_unlock_irqrestore(&host->lock, flags);
1828 static int msdc_ops_get_cd(struct mmc_host *mmc)
1830 struct msdc_host *host = mmc_priv(mmc);
1831 void __iomem *base = host->base;
1832 unsigned long flags;
1835 /* for sdio, MSDC_REMOVABLE not set, always return 1 */
1836 if (!(host->hw->flags & MSDC_REMOVABLE)) {
1837 /* For sdio, read H/W always get<1>, but may timeout some times */
1839 host->card_inserted = 1;
1842 host->card_inserted = (host->pm_state.event == PM_EVENT_USER_RESUME) ? 1 : 0;
1843 INIT_MSG("sdio ops_get_cd<%d>", host->card_inserted);
1844 return host->card_inserted;
1848 /* MSDC_CD_PIN_EN set for card */
1849 if (host->hw->flags & MSDC_CD_PIN_EN) {
1850 spin_lock_irqsave(&host->lock, flags);
1852 present = host->card_inserted; /* why not read from H/W: Fix me*/
1856 present = (sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 0 : 1;
1858 present = (sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 1 : 0;
1859 host->card_inserted = present;
1861 spin_unlock_irqrestore(&host->lock, flags);
1863 present = 0; /* TODO? Check DAT3 pins for card detection */
1866 INIT_MSG("ops_get_cd return<%d>", present);
1870 static struct mmc_host_ops mt_msdc_ops = {
1871 .request = msdc_ops_request,
1872 .set_ios = msdc_ops_set_ios,
1873 .get_ro = msdc_ops_get_ro,
1874 .get_cd = msdc_ops_get_cd,
1877 /*--------------------------------------------------------------------------*/
1878 /* interrupt handler */
1879 /*--------------------------------------------------------------------------*/
1880 static irqreturn_t msdc_irq(int irq, void *dev_id)
1882 struct msdc_host *host = (struct msdc_host *)dev_id;
1883 struct mmc_data *data = host->data;
1884 struct mmc_command *cmd = host->cmd;
1885 void __iomem *base = host->base;
1887 u32 cmdsts = MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO | MSDC_INT_CMDRDY |
1888 MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO | MSDC_INT_ACMDRDY |
1889 MSDC_INT_ACMD19_DONE;
1890 u32 datsts = MSDC_INT_DATCRCERR | MSDC_INT_DATTMO;
1892 u32 intsts = sdr_read32(MSDC_INT);
1893 u32 inten = sdr_read32(MSDC_INTEN); inten &= intsts;
1895 sdr_write32(MSDC_INT, intsts); /* clear interrupts */
1896 /* MSG will cause fatal error */
1898 /* card change interrupt */
1899 if (intsts & MSDC_INT_CDSC) {
1900 if (host->mmc->caps & MMC_CAP_NEEDS_POLL)
1902 IRQ_MSG("MSDC_INT_CDSC irq<0x%.8x>", intsts);
1903 schedule_delayed_work(&host->card_delaywork, HZ);
1904 /* tuning when plug card ? */
1907 /* sdio interrupt */
1908 if (intsts & MSDC_INT_SDIOIRQ) {
1909 IRQ_MSG("XXX MSDC_INT_SDIOIRQ"); /* seems not sdio irq */
1910 //mmc_signal_sdio_irq(host->mmc);
1913 /* transfer complete interrupt */
1915 if (inten & MSDC_INT_XFER_COMPL) {
1916 data->bytes_xfered = host->xfer_size;
1917 complete(&host->xfer_done);
1920 if (intsts & datsts) {
1921 /* do basic reset, or stop command will sdc_busy */
1922 msdc_reset_hw(host);
1926 if (intsts & MSDC_INT_DATTMO) {
1927 IRQ_MSG("XXX CMD<%d> MSDC_INT_DATTMO", host->mrq->cmd->opcode);
1928 data->error = -ETIMEDOUT;
1929 } else if (intsts & MSDC_INT_DATCRCERR) {
1930 IRQ_MSG("XXX CMD<%d> MSDC_INT_DATCRCERR, SDC_DCRC_STS<0x%x>", host->mrq->cmd->opcode, sdr_read32(SDC_DCRC_STS));
1934 //if(sdr_read32(MSDC_INTEN) & MSDC_INT_XFER_COMPL) {
1935 complete(&host->xfer_done); /* Read CRC come fast, XFER_COMPL not enabled */
1939 /* command interrupts */
1940 if ((cmd != NULL) && (intsts & cmdsts)) {
1941 if ((intsts & MSDC_INT_CMDRDY) || (intsts & MSDC_INT_ACMDRDY) ||
1942 (intsts & MSDC_INT_ACMD19_DONE)) {
1943 u32 *rsp = &cmd->resp[0];
1945 switch (host->cmd_rsp) {
1949 *rsp++ = sdr_read32(SDC_RESP3); *rsp++ = sdr_read32(SDC_RESP2);
1950 *rsp++ = sdr_read32(SDC_RESP1); *rsp++ = sdr_read32(SDC_RESP0);
1952 default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
1953 if ((intsts & MSDC_INT_ACMDRDY) || (intsts & MSDC_INT_ACMD19_DONE))
1954 *rsp = sdr_read32(SDC_ACMD_RESP);
1956 *rsp = sdr_read32(SDC_RESP0);
1959 } else if ((intsts & MSDC_INT_RSPCRCERR) || (intsts & MSDC_INT_ACMDCRCERR)) {
1960 if (intsts & MSDC_INT_ACMDCRCERR)
1961 IRQ_MSG("XXX CMD<%d> MSDC_INT_ACMDCRCERR", cmd->opcode);
1963 IRQ_MSG("XXX CMD<%d> MSDC_INT_RSPCRCERR", cmd->opcode);
1965 } else if ((intsts & MSDC_INT_CMDTMO) || (intsts & MSDC_INT_ACMDTMO)) {
1966 if (intsts & MSDC_INT_ACMDTMO)
1967 IRQ_MSG("XXX CMD<%d> MSDC_INT_ACMDTMO", cmd->opcode);
1969 IRQ_MSG("XXX CMD<%d> MSDC_INT_CMDTMO", cmd->opcode);
1970 cmd->error = -ETIMEDOUT;
1971 msdc_reset_hw(host);
1975 complete(&host->cmd_done);
1978 /* mmc irq interrupts */
1979 if (intsts & MSDC_INT_MMCIRQ)
1980 printk(KERN_INFO "msdc[%d] MMCIRQ: SDC_CSTS=0x%.8x\r\n", host->id, sdr_read32(SDC_CSTS));
1982 #ifdef MT6575_SD_DEBUG
1984 /* msdc_int_reg *int_reg = (msdc_int_reg*)&intsts;*/
1985 N_MSG(INT, "IRQ_EVT(0x%x): MMCIRQ(%d) CDSC(%d), ACRDY(%d), ACTMO(%d), ACCRE(%d) AC19DN(%d)",
1992 int_reg->atocmd19done);
1993 N_MSG(INT, "IRQ_EVT(0x%x): SDIO(%d) CMDRDY(%d), CMDTMO(%d), RSPCRC(%d), CSTA(%d)",
2000 N_MSG(INT, "IRQ_EVT(0x%x): XFCMP(%d) DXDONE(%d), DATTMO(%d), DATCRC(%d), DMAEMP(%d)",
2006 int_reg->dmaqempty);
2013 /*--------------------------------------------------------------------------*/
2014 /* platform_driver members */
2015 /*--------------------------------------------------------------------------*/
2016 /* called by msdc_drv_probe/remove */
2017 static void msdc_enable_cd_irq(struct msdc_host *host, int enable)
2019 struct msdc_hw *hw = host->hw;
2020 void __iomem *base = host->base;
2022 /* for sdio, not set */
2023 if ((hw->flags & MSDC_CD_PIN_EN) == 0) {
2024 /* Pull down card detection pin since it is not avaiable */
2026 if (hw->config_gpio_pin)
2027 hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_DOWN);
2029 sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
2030 sdr_clr_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
2031 sdr_clr_bits(SDC_CFG, SDC_CFG_INSWKUP);
2035 N_MSG(CFG, "CD IRQ Enable(%d)", enable);
2038 /* card detection circuit relies on the core power so that the core power
2039 * shouldn't be turned off. Here adds a reference count to keep
2040 * the core power alive.
2042 //msdc_vcore_on(host); //did in msdc_init_hw()
2044 if (hw->config_gpio_pin) /* NULL */
2045 hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_UP);
2047 sdr_set_field(MSDC_PS, MSDC_PS_CDDEBOUNCE, DEFAULT_DEBOUNCE);
2048 sdr_set_bits(MSDC_PS, MSDC_PS_CDEN);
2049 sdr_set_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
2050 sdr_set_bits(SDC_CFG, SDC_CFG_INSWKUP); /* not in document! Fix me */
2052 if (hw->config_gpio_pin) /* NULL */
2053 hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_DOWN);
2055 sdr_clr_bits(SDC_CFG, SDC_CFG_INSWKUP);
2056 sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
2057 sdr_clr_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
2059 /* Here decreases a reference count to core power since card
2060 * detection circuit is shutdown.
2062 //msdc_vcore_off(host);
2066 /* called by msdc_drv_probe */
2067 static void msdc_init_hw(struct msdc_host *host)
2069 void __iomem *base = host->base;
2072 #if 0 /* --- by chhung */
2073 msdc_vcore_on(host);
2074 msdc_pin_reset(host, MSDC_PIN_PULL_UP);
2075 msdc_select_clksrc(host, hw->clk_src);
2076 enable_clock(PERI_MSDC0_PDN + host->id, "SD");
2078 #endif /* end of --- */
2079 /* Configure to MMC/SD mode */
2080 sdr_set_field(MSDC_CFG, MSDC_CFG_MODE, MSDC_SDMMC);
2083 msdc_reset_hw(host);
2086 /* Disable card detection */
2087 sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
2089 /* Disable and clear all interrupts */
2090 sdr_clr_bits(MSDC_INTEN, sdr_read32(MSDC_INTEN));
2091 sdr_write32(MSDC_INT, sdr_read32(MSDC_INT));
2094 /* reset tuning parameter */
2095 sdr_write32(MSDC_PAD_CTL0, 0x00090000);
2096 sdr_write32(MSDC_PAD_CTL1, 0x000A0000);
2097 sdr_write32(MSDC_PAD_CTL2, 0x000A0000);
2098 // sdr_write32(MSDC_PAD_TUNE, 0x00000000);
2099 sdr_write32(MSDC_PAD_TUNE, 0x84101010); // for MT7620 E2 and afterward
2100 // sdr_write32(MSDC_DAT_RDDLY0, 0x00000000);
2101 sdr_write32(MSDC_DAT_RDDLY0, 0x10101010); // for MT7620 E2 and afterward
2102 sdr_write32(MSDC_DAT_RDDLY1, 0x00000000);
2103 sdr_write32(MSDC_IOCON, 0x00000000);
2104 #if 0 // use MT7620 default value: 0x403c004f
2105 sdr_write32(MSDC_PATCH_BIT0, 0x003C000F); /* bit0 modified: Rx Data Clock Source: 1 -> 2.0*/
2108 if (sdr_read32(MSDC_ECO_VER) >= 4) {
2109 if (host->id == 1) {
2110 sdr_set_field(MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_WRDAT_CRCS, 1);
2111 sdr_set_field(MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_CMD_RSP, 1);
2113 /* internal clock: latch read data */
2114 sdr_set_bits(MSDC_PATCH_BIT0, MSDC_PATCH_BIT_CKGEN_CK);
2119 /* for safety, should clear SDC_CFG.SDIO_INT_DET_EN & set SDC_CFG.SDIO in
2120 pre-loader,uboot,kernel drivers. and SDC_CFG.SDIO_INT_DET_EN will be only
2121 set when kernel driver wants to use SDIO bus interrupt */
2122 /* Configure to enable SDIO mode. it's must otherwise sdio cmd5 failed */
2123 sdr_set_bits(SDC_CFG, SDC_CFG_SDIO);
2125 /* disable detect SDIO device interupt function */
2126 sdr_clr_bits(SDC_CFG, SDC_CFG_SDIOIDE);
2128 /* eneable SMT for glitch filter */
2129 sdr_set_bits(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKSMT);
2130 sdr_set_bits(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDSMT);
2131 sdr_set_bits(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATSMT);
2134 /* set clk, cmd, dat pad driving */
2135 sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVN, 4);
2136 sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVP, 4);
2137 sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVN, 4);
2138 sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVP, 4);
2139 sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVN, 4);
2140 sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVP, 4);
2142 sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVN, 0);
2143 sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVP, 0);
2144 sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVN, 0);
2145 sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVP, 0);
2146 sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVN, 0);
2147 sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVP, 0);
2150 /* set sampling edge */
2152 /* write crc timeout detection */
2153 sdr_set_field(MSDC_PATCH_BIT0, 1 << 30, 1);
2155 /* Configure to default data timeout */
2156 sdr_set_field(SDC_CFG, SDC_CFG_DTOC, DEFAULT_DTOC);
2158 msdc_set_buswidth(host, MMC_BUS_WIDTH_1);
2160 N_MSG(FUC, "init hardware done!");
2163 /* called by msdc_drv_remove */
2164 static void msdc_deinit_hw(struct msdc_host *host)
2166 void __iomem *base = host->base;
2168 /* Disable and clear all interrupts */
2169 sdr_clr_bits(MSDC_INTEN, sdr_read32(MSDC_INTEN));
2170 sdr_write32(MSDC_INT, sdr_read32(MSDC_INT));
2172 /* Disable card detection */
2173 msdc_enable_cd_irq(host, 0);
2174 // msdc_set_power_mode(host, MMC_POWER_OFF); /* make sure power down */ /* --- by chhung */
2177 /* init gpd and bd list in msdc_drv_probe */
2178 static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma)
2180 struct gpd *gpd = dma->gpd;
2181 struct bd *bd = dma->bd;
2184 /* we just support one gpd, but gpd->next must be set for desc
2185 * DMA. That's why we alloc 2 gpd structurs.
2188 memset(gpd, 0, sizeof(struct gpd) * 2);
2190 gpd->bdp = 1; /* hwo, cs, bd pointer */
2191 gpd->ptr = (void *)dma->bd_addr; /* physical address */
2192 gpd->next = (void *)((u32)dma->gpd_addr + sizeof(struct gpd));
2194 memset(bd, 0, sizeof(struct bd) * MAX_BD_NUM);
2195 for (i = 0; i < (MAX_BD_NUM - 1); i++)
2196 bd[i].next = (void *)(dma->bd_addr + sizeof(*bd) * (i + 1));
2199 static int msdc_drv_probe(struct platform_device *pdev)
2201 struct resource *res;
2203 struct mmc_host *mmc;
2204 struct msdc_host *host;
2210 if (of_property_read_bool(pdev->dev.of_node, "mtk,wp-en"))
2211 msdc0_hw.flags |= MSDC_WP_PIN_EN;
2213 /* Allocate MMC host for this device */
2214 mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev);
2218 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2219 base = devm_ioremap_resource(&pdev->dev, res);
2221 ret = PTR_ERR(base);
2225 /* Set host parameters to mmc */
2226 mmc->ops = &mt_msdc_ops;
2227 mmc->f_min = HOST_MIN_MCLK;
2228 mmc->f_max = HOST_MAX_MCLK;
2229 mmc->ocr_avail = MSDC_OCR_AVAIL;
2231 mmc->caps = MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
2233 //TODO: read this as bus-width from dt (via mmc_of_parse)
2234 mmc->caps |= MMC_CAP_4_BIT_DATA;
2236 cd_active_low = !of_property_read_bool(pdev->dev.of_node, "mediatek,cd-high");
2238 if (of_property_read_bool(pdev->dev.of_node, "mediatek,cd-poll"))
2239 mmc->caps |= MMC_CAP_NEEDS_POLL;
2241 /* MMC core transfer sizes tunable parameters */
2242 mmc->max_segs = MAX_HW_SGMTS;
2244 mmc->max_seg_size = MAX_SGMT_SZ;
2245 mmc->max_blk_size = HOST_MAX_BLKSZ;
2246 mmc->max_req_size = MAX_REQ_SZ;
2247 mmc->max_blk_count = mmc->max_req_size;
2249 host = mmc_priv(mmc);
2252 host->id = pdev->id;
2253 if (host->id < 0 || host->id >= 4)
2257 host->irq = platform_get_irq(pdev, 0);
2258 if (host->irq < 0) {
2264 host->mclk = 0; /* mclk: the request clock of mmc sub-system */
2265 host->hclk = hclks[hw->clk_src]; /* hclk: clock of clock source to msdc controller */
2266 host->sclk = 0; /* sclk: the really clock after divition */
2267 host->pm_state = PMSG_RESUME;
2269 host->core_clkon = 0;
2270 host->card_clkon = 0;
2271 host->core_power = 0;
2272 host->power_mode = MMC_POWER_OFF;
2273 // host->card_inserted = hw->flags & MSDC_REMOVABLE ? 0 : 1;
2274 host->timeout_ns = 0;
2275 host->timeout_clks = DEFAULT_DTOC * 65536;
2278 //init_MUTEX(&host->sem); /* we don't need to support multiple threads access */
2280 mmc_dev(mmc)->dma_mask = NULL;
2282 /* using dma_alloc_coherent*/ /* todo: using 1, for all 4 slots */
2283 host->dma.gpd = dma_alloc_coherent(&pdev->dev,
2284 MAX_GPD_NUM * sizeof(struct gpd),
2285 &host->dma.gpd_addr, GFP_KERNEL);
2286 host->dma.bd = dma_alloc_coherent(&pdev->dev,
2287 MAX_BD_NUM * sizeof(struct bd),
2288 &host->dma.bd_addr, GFP_KERNEL);
2289 if (!host->dma.gpd || !host->dma.bd) {
2293 msdc_init_gpd_bd(host, &host->dma);
2295 INIT_DELAYED_WORK(&host->card_delaywork, msdc_tasklet_card);
2296 spin_lock_init(&host->lock);
2299 /* TODO check weather flags 0 is correct, the mtk-sd driver uses
2300 * IRQF_TRIGGER_LOW | IRQF_ONESHOT for flags
2302 * for flags 0 the trigger polarity is determined by the
2303 * device tree, but not the oneshot flag, but maybe it is also
2304 * not needed because the soc could be oneshot safe.
2306 ret = devm_request_irq(&pdev->dev, host->irq, msdc_irq, 0, pdev->name,
2311 platform_set_drvdata(pdev, mmc);
2313 ret = mmc_add_host(mmc);
2317 /* Config card detection pin and enable interrupts */
2318 if (hw->flags & MSDC_CD_PIN_EN) { /* set for card */
2319 msdc_enable_cd_irq(host, 1);
2321 msdc_enable_cd_irq(host, 0);
2327 platform_set_drvdata(pdev, NULL);
2328 msdc_deinit_hw(host);
2329 cancel_delayed_work_sync(&host->card_delaywork);
2333 dma_free_coherent(&pdev->dev, MAX_GPD_NUM * sizeof(struct gpd),
2334 host->dma.gpd, host->dma.gpd_addr);
2336 dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct bd),
2337 host->dma.bd, host->dma.bd_addr);
2344 /* 4 device share one driver, using "drvdata" to show difference */
2345 static int msdc_drv_remove(struct platform_device *pdev)
2347 struct mmc_host *mmc;
2348 struct msdc_host *host;
2350 mmc = platform_get_drvdata(pdev);
2353 host = mmc_priv(mmc);
2356 ERR_MSG("removed !!!");
2358 platform_set_drvdata(pdev, NULL);
2359 mmc_remove_host(host->mmc);
2360 msdc_deinit_hw(host);
2362 cancel_delayed_work_sync(&host->card_delaywork);
2364 dma_free_coherent(&pdev->dev, MAX_GPD_NUM * sizeof(struct gpd),
2365 host->dma.gpd, host->dma.gpd_addr);
2366 dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct bd),
2367 host->dma.bd, host->dma.bd_addr);
2369 mmc_free_host(host->mmc);
2374 /* Fix me: Power Flow */
2377 static void msdc_drv_pm(struct platform_device *pdev, pm_message_t state)
2379 struct mmc_host *mmc = platform_get_drvdata(pdev);
2381 struct msdc_host *host = mmc_priv(mmc);
2382 msdc_pm(state, (void *)host);
2386 static int msdc_drv_suspend(struct platform_device *pdev, pm_message_t state)
2388 if (state.event == PM_EVENT_SUSPEND)
2389 msdc_drv_pm(pdev, state);
2393 static int msdc_drv_resume(struct platform_device *pdev)
2395 struct pm_message state;
2397 state.event = PM_EVENT_RESUME;
2398 msdc_drv_pm(pdev, state);
2403 static const struct of_device_id mt7620_sdhci_match[] = {
2404 { .compatible = "ralink,mt7620-sdhci" },
2407 MODULE_DEVICE_TABLE(of, mt7620_sdhci_match);
2409 static struct platform_driver mt_msdc_driver = {
2410 .probe = msdc_drv_probe,
2411 .remove = msdc_drv_remove,
2413 .suspend = msdc_drv_suspend,
2414 .resume = msdc_drv_resume,
2418 .of_match_table = mt7620_sdhci_match,
2422 /*--------------------------------------------------------------------------*/
2423 /* module init/exit */
2424 /*--------------------------------------------------------------------------*/
2425 static int __init mt_msdc_init(void)
2430 // Set the pins for sdxc to sdxc mode
2431 //FIXME: this should be done by pinctl and not by the sd driver
2432 reg = sdr_read32((void __iomem *)(RALINK_SYSCTL_BASE + 0x60)) & ~(0x3 << 18);
2433 sdr_write32((void __iomem *)(RALINK_SYSCTL_BASE + 0x60), reg);
2435 ret = platform_driver_register(&mt_msdc_driver);
2437 printk(KERN_ERR DRV_NAME ": Can't register driver");
2441 #if defined(MT6575_SD_DEBUG)
2442 msdc_debug_proc_init();
2447 static void __exit mt_msdc_exit(void)
2449 platform_driver_unregister(&mt_msdc_driver);
2452 module_init(mt_msdc_init);
2453 module_exit(mt_msdc_exit);
2454 MODULE_LICENSE("GPL");
2455 MODULE_DESCRIPTION("MediaTek MT6575 SD/MMC Card Driver");