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 #ifdef CONFIG_SOC_MT7621
54 #define RALINK_SYSCTL_BASE 0xbe000000
56 #define RALINK_SYSCTL_BASE 0xb0000000
59 #define DRV_NAME "mtk-sd"
61 #if defined(CONFIG_SOC_MT7620)
62 #define HOST_MAX_MCLK (48000000) /* +/- by chhung */
63 #elif defined(CONFIG_SOC_MT7621)
64 #define HOST_MAX_MCLK (50000000) /* +/- by chhung */
66 #define HOST_MIN_MCLK (260000)
68 #define HOST_MAX_BLKSZ (2048)
70 #define MSDC_OCR_AVAIL (MMC_VDD_28_29 | MMC_VDD_29_30 | MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33)
72 #define GPIO_PULL_DOWN (0)
73 #define GPIO_PULL_UP (1)
75 #define DEFAULT_DEBOUNCE (8) /* 8 cycles */
76 #define DEFAULT_DTOC (40) /* data timeout counter. 65536x40 sclk. */
78 #define CMD_TIMEOUT (HZ / 10) /* 100ms */
79 #define DAT_TIMEOUT (HZ / 2 * 5) /* 500ms x5 */
81 #define MAX_DMA_CNT (64 * 1024 - 512) /* a single transaction for WIFI may be 50K*/
83 #define MAX_GPD_NUM (1 + 1) /* one null gpd */
84 #define MAX_BD_NUM (1024)
86 #define MAX_HW_SGMTS (MAX_BD_NUM)
87 #define MAX_SGMT_SZ (MAX_DMA_CNT)
88 #define MAX_REQ_SZ (MAX_SGMT_SZ * 8)
90 static int cd_active_low
= 1;
92 //=================================
93 #define PERI_MSDC0_PDN (15)
94 //#define PERI_MSDC1_PDN (16)
95 //#define PERI_MSDC2_PDN (17)
96 //#define PERI_MSDC3_PDN (18)
99 struct msdc_hw msdc0_hw
= {
101 .flags
= MSDC_CD_PIN_EN
| MSDC_REMOVABLE
,
102 // .flags = MSDC_WP_PIN_EN | MSDC_CD_PIN_EN | MSDC_REMOVABLE,
107 static int msdc_rsp
[] = {
119 #define msdc_dma_on() sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_PIO)
121 static void msdc_reset_hw(struct msdc_host
*host
)
123 sdr_set_bits(host
->base
+ MSDC_CFG
, MSDC_CFG_RST
);
124 while (readl(host
->base
+ MSDC_CFG
) & MSDC_CFG_RST
)
128 #define msdc_clr_int() \
130 volatile u32 val = readl(host->base + MSDC_INT); \
131 writel(val, host->base + MSDC_INT); \
134 static void msdc_clr_fifo(struct msdc_host
*host
)
136 sdr_set_bits(host
->base
+ MSDC_FIFOCS
, MSDC_FIFOCS_CLR
);
137 while (readl(host
->base
+ MSDC_FIFOCS
) & MSDC_FIFOCS_CLR
)
141 #define msdc_irq_save(val) \
143 val = readl(host->base + MSDC_INTEN); \
144 sdr_clr_bits(host->base + MSDC_INTEN, val); \
147 /* clock source for host: global */
148 #if defined(CONFIG_SOC_MT7620)
149 static u32 hclks
[] = {48000000}; /* +/- by chhung */
150 #elif defined(CONFIG_SOC_MT7621)
151 static u32 hclks
[] = {50000000}; /* +/- by chhung */
154 #define sdc_is_busy() (readl(host->base + SDC_STS) & SDC_STS_SDCBUSY)
155 #define sdc_is_cmd_busy() (readl(host->base + SDC_STS) & SDC_STS_CMDBUSY)
157 #define sdc_send_cmd(cmd, arg) \
159 writel((arg), host->base + SDC_ARG); \
160 writel((cmd), host->base + SDC_CMD); \
165 #define PHYSADDR(a) (((unsigned long)(a)) & 0x1fffffff)
167 #define PHYSADDR(a) ((a) & 0x1fffffff)
170 static unsigned int msdc_do_command(struct msdc_host
*host
,
171 struct mmc_command
*cmd
,
173 unsigned long timeout
);
175 static int msdc_tune_cmdrsp(struct msdc_host
*host
, struct mmc_command
*cmd
);
177 static void msdc_set_timeout(struct msdc_host
*host
, u32 ns
, u32 clks
)
181 host
->timeout_ns
= ns
;
182 host
->timeout_clks
= clks
;
184 clk_ns
= 1000000000UL / host
->sclk
;
185 timeout
= ns
/ clk_ns
+ clks
;
186 timeout
= timeout
>> 16; /* in 65536 sclk cycle unit */
187 timeout
= timeout
> 1 ? timeout
- 1 : 0;
188 timeout
= timeout
> 255 ? 255 : timeout
;
190 sdr_set_field(host
->base
+ SDC_CFG
, SDC_CFG_DTOC
, timeout
);
193 static void msdc_tasklet_card(struct work_struct
*work
)
195 struct msdc_host
*host
= (struct msdc_host
*)container_of(work
,
196 struct msdc_host
, card_delaywork
.work
);
200 spin_lock(&host
->lock
);
202 status
= readl(host
->base
+ MSDC_PS
);
204 inserted
= (status
& MSDC_PS_CDSTS
) ? 0 : 1;
206 inserted
= (status
& MSDC_PS_CDSTS
) ? 1 : 0;
208 /* Make sure: handle the last interrupt */
209 host
->card_inserted
= inserted
;
211 if (!host
->suspend
) {
212 host
->mmc
->f_max
= HOST_MAX_MCLK
;
213 mmc_detect_change(host
->mmc
, msecs_to_jiffies(20));
216 spin_unlock(&host
->lock
);
219 static void msdc_select_clksrc(struct msdc_host
*host
, unsigned char clksrc
)
225 val
= readl(host
->base
+ MSDC_CLKSRC_REG
);
226 if (readl(host
->base
+ MSDC_ECO_VER
) >= 4) {
227 val
&= ~(0x3 << clk_src_bit
[host
->id
]);
228 val
|= clksrc
<< clk_src_bit
[host
->id
];
230 val
&= ~0x3; val
|= clksrc
;
232 writel(val
, host
->base
+ MSDC_CLKSRC_REG
);
234 host
->hclk
= hclks
[clksrc
];
235 host
->hw
->clk_src
= clksrc
;
237 #endif /* end of --- */
239 static void msdc_set_mclk(struct msdc_host
*host
, int ddr
, unsigned int hz
)
241 //struct msdc_hw *hw = host->hw;
246 u32 hclk
= host
->hclk
;
247 //u8 clksrc = hw->clk_src;
249 if (!hz
) { // set mmc system clock to 0 ?
254 msdc_irq_save(flags
);
257 mode
= 0x2; /* ddr mode and use divisor */
258 if (hz
>= (hclk
>> 2)) {
259 div
= 1; /* mean div = 1/4 */
260 sclk
= hclk
>> 2; /* sclk = clk / 4 */
262 div
= (hclk
+ ((hz
<< 2) - 1)) / (hz
<< 2);
263 sclk
= (hclk
>> 2) / div
;
265 } else if (hz
>= hclk
) { /* bug fix */
266 mode
= 0x1; /* no divisor and divisor is ignored */
270 mode
= 0x0; /* use divisor */
271 if (hz
>= (hclk
>> 1)) {
272 div
= 0; /* mean div = 1/2 */
273 sclk
= hclk
>> 1; /* sclk = clk / 2 */
275 div
= (hclk
+ ((hz
<< 2) - 1)) / (hz
<< 2);
276 sclk
= (hclk
>> 2) / div
;
280 /* set clock mode and divisor */
281 sdr_set_field(host
->base
+ MSDC_CFG
, MSDC_CFG_CKMOD
, mode
);
282 sdr_set_field(host
->base
+ MSDC_CFG
, MSDC_CFG_CKDIV
, div
);
284 /* wait clock stable */
285 while (!(readl(host
->base
+ MSDC_CFG
) & MSDC_CFG_CKSTB
))
290 msdc_set_timeout(host
, host
->timeout_ns
, host
->timeout_clks
); // need?
292 sdr_set_bits(host
->base
+ MSDC_INTEN
, flags
);
295 /* Fix me. when need to abort */
296 static void msdc_abort_data(struct msdc_host
*host
)
298 struct mmc_command
*stop
= host
->mrq
->stop
;
300 dev_err(mmc_dev(host
->mmc
), "%d -> Need to Abort.\n", host
->id
);
306 // need to check FIFO count 0 ?
308 if (stop
) { /* try to stop, but may not success */
309 dev_err(mmc_dev(host
->mmc
), "%d -> stop when abort CMD<%d>\n",
310 host
->id
, stop
->opcode
);
311 (void)msdc_do_command(host
, stop
, 0, CMD_TIMEOUT
);
314 //if (host->mclk >= 25000000) {
315 // msdc_set_mclk(host, 0, host->mclk >> 1);
321 register as callback function of WIFI(combo_sdio_register_pm) .
322 can called by msdc_drv_suspend/resume too.
324 static void msdc_pm(pm_message_t state
, void *data
)
326 struct msdc_host
*host
= (struct msdc_host
*)data
;
327 int evt
= state
.event
;
329 if (evt
== PM_EVENT_SUSPEND
|| evt
== PM_EVENT_USER_SUSPEND
) {
330 if (host
->suspend
) /* already suspend */ /* default 0*/
333 /* for memory card. already power off by mmc */
334 if (evt
== PM_EVENT_SUSPEND
&& host
->power_mode
== MMC_POWER_OFF
)
338 host
->pm_state
= state
; /* default PMSG_RESUME */
340 } else if (evt
== PM_EVENT_RESUME
|| evt
== PM_EVENT_USER_RESUME
) {
344 /* No PM resume when USR suspend */
345 if (evt
== PM_EVENT_RESUME
&& host
->pm_state
.event
== PM_EVENT_USER_SUSPEND
) {
346 dev_err(mmc_dev(host
->mmc
),
347 "%d -> PM Resume when in USR Suspend\n",
348 host
->id
); /* won't happen. */
353 host
->pm_state
= state
;
359 static inline u32
msdc_cmd_find_resp(struct mmc_command
*cmd
)
361 u32 opcode
= cmd
->opcode
;
364 if (opcode
== MMC_SET_RELATIVE_ADDR
) {
365 resp
= (mmc_cmd_type(cmd
) == MMC_CMD_BCR
) ? RESP_R6
: RESP_R1
;
366 } else if (opcode
== MMC_FAST_IO
) {
368 } else if (opcode
== MMC_GO_IRQ_STATE
) {
370 } else if (opcode
== MMC_SELECT_CARD
) {
371 resp
= (cmd
->arg
!= 0) ? RESP_R1B
: RESP_NONE
;
372 } else if (opcode
== SD_IO_RW_DIRECT
|| opcode
== SD_IO_RW_EXTENDED
) {
373 resp
= RESP_R1
; /* SDIO workaround. */
374 } else if (opcode
== SD_SEND_IF_COND
&& (mmc_cmd_type(cmd
) == MMC_CMD_BCR
)) {
377 switch (mmc_resp_type(cmd
)) {
400 /*--------------------------------------------------------------------------*/
401 /* mmc_host_ops members */
402 /*--------------------------------------------------------------------------*/
403 static unsigned int msdc_command_start(struct msdc_host
*host
,
404 struct mmc_command
*cmd
,
405 unsigned long timeout
)
407 u32 opcode
= cmd
->opcode
;
409 u32 wints
= MSDC_INT_CMDRDY
| MSDC_INT_RSPCRCERR
| MSDC_INT_CMDTMO
|
410 MSDC_INT_ACMDRDY
| MSDC_INT_ACMDCRCERR
| MSDC_INT_ACMDTMO
|
411 MSDC_INT_ACMD19_DONE
;
416 /* Protocol layer does not provide response type, but our hardware needs
417 * to know exact type, not just size!
419 resp
= msdc_cmd_find_resp(cmd
);
423 * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 |
424 * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
426 rawcmd
= opcode
| msdc_rsp
[resp
] << 7 | host
->blksz
<< 16;
428 if (opcode
== MMC_READ_MULTIPLE_BLOCK
) {
430 } else if (opcode
== MMC_READ_SINGLE_BLOCK
) {
432 } else if (opcode
== MMC_WRITE_MULTIPLE_BLOCK
) {
433 rawcmd
|= ((2 << 11) | (1 << 13));
434 } else if (opcode
== MMC_WRITE_BLOCK
) {
435 rawcmd
|= ((1 << 11) | (1 << 13));
436 } else if (opcode
== SD_IO_RW_EXTENDED
) {
437 if (cmd
->data
->flags
& MMC_DATA_WRITE
)
439 if (cmd
->data
->blocks
> 1)
443 } else if (opcode
== SD_IO_RW_DIRECT
&& cmd
->flags
== (unsigned int)-1) {
445 } else if ((opcode
== SD_APP_SEND_SCR
) ||
446 (opcode
== SD_APP_SEND_NUM_WR_BLKS
) ||
447 (opcode
== SD_SWITCH
&& (mmc_cmd_type(cmd
) == MMC_CMD_ADTC
)) ||
448 (opcode
== SD_APP_SD_STATUS
&& (mmc_cmd_type(cmd
) == MMC_CMD_ADTC
)) ||
449 (opcode
== MMC_SEND_EXT_CSD
&& (mmc_cmd_type(cmd
) == MMC_CMD_ADTC
))) {
451 } else if (opcode
== MMC_STOP_TRANSMISSION
) {
453 rawcmd
&= ~(0x0FFF << 16);
456 tmo
= jiffies
+ timeout
;
458 if (opcode
== MMC_SEND_STATUS
) {
460 if (!sdc_is_cmd_busy())
463 if (time_after(jiffies
, tmo
)) {
464 dev_err(mmc_dev(host
->mmc
),
465 "%d -> XXX cmd_busy timeout: before CMD<%d>\n",
467 cmd
->error
= -ETIMEDOUT
;
476 if (time_after(jiffies
, tmo
)) {
477 dev_err(mmc_dev(host
->mmc
),
478 "%d -> XXX sdc_busy timeout: before CMD<%d>\n",
480 cmd
->error
= -ETIMEDOUT
;
487 //BUG_ON(in_interrupt());
489 host
->cmd_rsp
= resp
;
491 init_completion(&host
->cmd_done
);
493 sdr_set_bits(host
->base
+ MSDC_INTEN
, wints
);
494 sdc_send_cmd(rawcmd
, cmd
->arg
);
500 static unsigned int msdc_command_resp(struct msdc_host
*host
,
501 struct mmc_command
*cmd
,
503 unsigned long timeout
)
504 __must_hold(&host
->lock
)
506 u32 opcode
= cmd
->opcode
;
508 u32 wints
= MSDC_INT_CMDRDY
| MSDC_INT_RSPCRCERR
| MSDC_INT_CMDTMO
|
509 MSDC_INT_ACMDRDY
| MSDC_INT_ACMDCRCERR
| MSDC_INT_ACMDTMO
|
510 MSDC_INT_ACMD19_DONE
;
512 BUG_ON(in_interrupt());
513 //init_completion(&host->cmd_done);
514 //sdr_set_bits(host->base + MSDC_INTEN, wints);
516 spin_unlock(&host
->lock
);
517 if (!wait_for_completion_timeout(&host
->cmd_done
, 10 * timeout
)) {
518 dev_err(mmc_dev(host
->mmc
),
519 "%d -> XXX CMD<%d> wait_for_completion timeout ARG<0x%.8x>\n",
520 host
->id
, opcode
, cmd
->arg
);
521 cmd
->error
= -ETIMEDOUT
;
524 spin_lock(&host
->lock
);
526 sdr_clr_bits(host
->base
+ MSDC_INTEN
, wints
);
530 /* do we need to save card's RCA when SD_SEND_RELATIVE_ADDR */
535 /* memory card CRC */
536 if (host
->hw
->flags
& MSDC_REMOVABLE
&& cmd
->error
== -EIO
) {
537 /* check if has data phase */
538 if (readl(host
->base
+ SDC_CMD
) & 0x1800) {
539 msdc_abort_data(host
);
546 cmd
->error
= msdc_tune_cmdrsp(host
, cmd
);
550 /* if (resp == RESP_R1B) {
551 while ((readl(host->base + MSDC_PS) & 0x10000) != 0x10000);
553 /* CMD12 Error Handle */
558 static unsigned int msdc_do_command(struct msdc_host
*host
,
559 struct mmc_command
*cmd
,
561 unsigned long timeout
)
563 if (msdc_command_start(host
, cmd
, timeout
))
566 if (msdc_command_resp(host
, cmd
, tune
, timeout
))
574 static void msdc_dma_start(struct msdc_host
*host
)
576 u32 wints
= MSDC_INTEN_XFER_COMPL
| MSDC_INTEN_DATTMO
| MSDC_INTEN_DATCRCERR
;
578 sdr_set_bits(host
->base
+ MSDC_INTEN
, wints
);
579 //dsb(); /* --- by chhung */
580 sdr_set_field(host
->base
+ MSDC_DMA_CTRL
, MSDC_DMA_CTRL_START
, 1);
583 static void msdc_dma_stop(struct msdc_host
*host
)
586 u32 wints
= MSDC_INTEN_XFER_COMPL
| MSDC_INTEN_DATTMO
| MSDC_INTEN_DATCRCERR
;
588 //while (readl(host->base + MSDC_DMA_CFG) & MSDC_DMA_CFG_STS);
590 sdr_set_field(host
->base
+ MSDC_DMA_CTRL
, MSDC_DMA_CTRL_STOP
, 1);
591 while (readl(host
->base
+ MSDC_DMA_CFG
) & MSDC_DMA_CFG_STS
)
594 //dsb(); /* --- by chhung */
595 sdr_clr_bits(host
->base
+ MSDC_INTEN
, wints
); /* Not just xfer_comp */
599 static u8
msdc_dma_calcs(u8
*buf
, u32 len
)
603 for (i
= 0; i
< len
; i
++)
605 return 0xFF - (u8
)sum
;
608 static void msdc_dma_setup(struct msdc_host
*host
, struct msdc_dma
*dma
,
609 struct scatterlist
*sg_cmd
, unsigned int sglen
)
611 struct scatterlist
*sg
;
616 BUG_ON(sglen
> MAX_BD_NUM
); /* not support currently */
623 gpd
->hwo
= 1; /* hw will clear it */
625 gpd
->chksum
= 0; /* need to clear first. */
626 gpd
->chksum
= msdc_dma_calcs((u8
*)gpd
, 16);
629 for_each_sg(sg_cmd
, sg
, sglen
, j
) {
632 bd
[j
].ptr
= (void *)sg_dma_address(sg
);
633 bd
[j
].buflen
= sg_dma_len(sg
);
636 bd
[j
].eol
= 1; /* the last bd */
640 bd
[j
].chksum
= 0; /* checksume need to clear first */
641 bd
[j
].chksum
= msdc_dma_calcs((u8
*)(&bd
[j
]), 16);
644 sdr_set_field(host
->base
+ MSDC_DMA_CFG
, MSDC_DMA_CFG_DECSEN
, 1);
645 sdr_set_field(host
->base
+ MSDC_DMA_CTRL
, MSDC_DMA_CTRL_BRUSTSZ
,
647 sdr_set_field(host
->base
+ MSDC_DMA_CTRL
, MSDC_DMA_CTRL_MODE
, 1);
649 writel(PHYSADDR((u32
)dma
->gpd_addr
), host
->base
+ MSDC_DMA_SA
);
652 static int msdc_do_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
653 __must_hold(&host
->lock
)
655 struct msdc_host
*host
= mmc_priv(mmc
);
656 struct mmc_command
*cmd
;
657 struct mmc_data
*data
;
659 int read
= 1, send_type
= 0;
670 data
= mrq
->cmd
->data
;
674 if (msdc_do_command(host
, cmd
, 1, CMD_TIMEOUT
) != 0)
677 BUG_ON(data
->blksz
> HOST_MAX_BLKSZ
);
681 read
= data
->flags
& MMC_DATA_READ
? 1 : 0;
683 host
->xfer_size
= data
->blocks
* data
->blksz
;
684 host
->blksz
= data
->blksz
;
687 if ((host
->timeout_ns
!= data
->timeout_ns
) ||
688 (host
->timeout_clks
!= data
->timeout_clks
)) {
689 msdc_set_timeout(host
, data
->timeout_ns
, data
->timeout_clks
);
693 writel(data
->blocks
, host
->base
+ SDC_BLK_NUM
);
694 //msdc_clr_fifo(host); /* no need */
696 msdc_dma_on(); /* enable DMA mode first!! */
697 init_completion(&host
->xfer_done
);
699 /* start the command first*/
700 if (msdc_command_start(host
, cmd
, CMD_TIMEOUT
) != 0)
703 data
->sg_count
= dma_map_sg(mmc_dev(mmc
), data
->sg
,
705 mmc_get_dma_dir(data
));
706 msdc_dma_setup(host
, &host
->dma
, data
->sg
,
709 /* then wait command done */
710 if (msdc_command_resp(host
, cmd
, 1, CMD_TIMEOUT
) != 0)
713 /* for read, the data coming too fast, then CRC error
714 start DMA no business with CRC. */
715 //init_completion(&host->xfer_done);
716 msdc_dma_start(host
);
718 spin_unlock(&host
->lock
);
719 if (!wait_for_completion_timeout(&host
->xfer_done
, DAT_TIMEOUT
)) {
720 dev_err(mmc_dev(host
->mmc
),
721 "%d -> XXX CMD<%d> wait xfer_done<%d> timeout!!\n",
722 host
->id
, cmd
->opcode
,
723 data
->blocks
* data
->blksz
);
724 dev_err(mmc_dev(host
->mmc
),
725 "%d -> DMA_SA = 0x%x\n",
726 host
->id
, readl(host
->base
+ MSDC_DMA_SA
));
727 dev_err(mmc_dev(host
->mmc
),
728 "%d -> DMA_CA = 0x%x\n",
729 host
->id
, readl(host
->base
+ MSDC_DMA_CA
));
730 dev_err(mmc_dev(host
->mmc
),
731 "%d -> DMA_CTRL = 0x%x\n",
732 host
->id
, readl(host
->base
+ MSDC_DMA_CTRL
));
733 dev_err(mmc_dev(host
->mmc
),
734 "%d -> DMA_CFG = 0x%x\n",
735 host
->id
, readl(host
->base
+ MSDC_DMA_CFG
));
736 data
->error
= -ETIMEDOUT
;
742 spin_lock(&host
->lock
);
745 /* Last: stop transfer */
747 if (msdc_do_command(host
, data
->stop
, 0, CMD_TIMEOUT
) != 0)
755 dma_unmap_sg(mmc_dev(mmc
), data
->sg
, data
->sg_len
,
756 mmc_get_dma_dir(data
));
762 if (mrq
->data
&& mrq
->data
->error
)
763 host
->error
|= 0x010;
764 if (mrq
->stop
&& mrq
->stop
->error
)
765 host
->error
|= 0x100;
770 static int msdc_app_cmd(struct mmc_host
*mmc
, struct msdc_host
*host
)
772 struct mmc_command cmd
;
773 struct mmc_request mrq
;
776 memset(&cmd
, 0, sizeof(struct mmc_command
));
777 cmd
.opcode
= MMC_APP_CMD
;
778 cmd
.arg
= host
->app_cmd_arg
;
779 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
781 memset(&mrq
, 0, sizeof(struct mmc_request
));
782 mrq
.cmd
= &cmd
; cmd
.mrq
= &mrq
;
785 err
= msdc_do_command(host
, &cmd
, 0, CMD_TIMEOUT
);
789 static int msdc_tune_cmdrsp(struct msdc_host
*host
, struct mmc_command
*cmd
)
792 u32 rsmpl
, cur_rsmpl
, orig_rsmpl
;
793 u32 rrdly
, cur_rrdly
= 0xffffffff, orig_rrdly
;
796 /* ==== don't support 3.0 now ====
798 2: PAD_CMD_RESP_RXDLY[26:22]
799 ==========================*/
801 // save the previous tune result
802 sdr_get_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
, &orig_rsmpl
);
803 sdr_get_field(host
->base
+ MSDC_PAD_TUNE
, MSDC_PAD_TUNE_CMDRRDLY
,
808 for (rsmpl
= 0; rsmpl
< 2; rsmpl
++) {
810 cur_rsmpl
= (orig_rsmpl
+ rsmpl
) % 2;
815 sdr_set_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
,
819 result
= msdc_app_cmd(host
->mmc
, host
);
821 dev_err(mmc_dev(host
->mmc
),
822 "%d -> TUNE_CMD app_cmd<%d> failed: RESP_RXDLY<%d>,R_SMPL<%d>\n",
824 host
->mrq
->cmd
->opcode
,
825 cur_rrdly
, cur_rsmpl
);
829 result
= msdc_do_command(host
, cmd
, 0, CMD_TIMEOUT
); // not tune.
830 dev_err(mmc_dev(host
->mmc
),
831 "%d -> TUNE_CMD<%d> %s PAD_CMD_RESP_RXDLY[26:22]<%d> R_SMPL[1]<%d>\n",
832 host
->id
, cmd
->opcode
,
833 (result
== 0) ? "PASS" : "FAIL", cur_rrdly
,
838 if (result
!= -EIO
) {
839 dev_err(mmc_dev(host
->mmc
),
840 "%d -> TUNE_CMD<%d> Error<%d> not -EIO\n",
841 host
->id
, cmd
->opcode
, result
);
846 /* check if has data phase */
847 if (readl(host
->base
+ SDC_CMD
) & 0x1800)
848 msdc_abort_data(host
);
851 /* Lv2: PAD_CMD_RESP_RXDLY[26:22] */
852 cur_rrdly
= (orig_rrdly
+ rrdly
+ 1) % 32;
853 sdr_set_field(host
->base
+ MSDC_PAD_TUNE
,
854 MSDC_PAD_TUNE_CMDRRDLY
, cur_rrdly
);
855 } while (++rrdly
< 32);
860 /* Support SD2.0 Only */
861 static int msdc_tune_bread(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
863 struct msdc_host
*host
= mmc_priv(mmc
);
866 u32 rxdly
, cur_rxdly0
, cur_rxdly1
;
867 u32 dsmpl
, cur_dsmpl
, orig_dsmpl
;
868 u32 cur_dat0
, cur_dat1
, cur_dat2
, cur_dat3
;
869 u32 cur_dat4
, cur_dat5
, cur_dat6
, cur_dat7
;
870 u32 orig_dat0
, orig_dat1
, orig_dat2
, orig_dat3
;
871 u32 orig_dat4
, orig_dat5
, orig_dat6
, orig_dat7
;
875 sdr_get_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_DSPL
, &orig_dsmpl
);
878 sdr_set_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_DDLSEL
, 1);
882 for (dsmpl
= 0; dsmpl
< 2; dsmpl
++) {
883 cur_dsmpl
= (orig_dsmpl
+ dsmpl
) % 2;
888 sdr_set_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_DSPL
,
892 result
= msdc_app_cmd(host
->mmc
, host
);
894 dev_err(mmc_dev(host
->mmc
),
895 "%d -> TUNE_BREAD app_cmd<%d> failed\n",
897 host
->mrq
->cmd
->opcode
);
901 result
= msdc_do_request(mmc
, mrq
);
903 sdr_get_field(host
->base
+ SDC_DCRC_STS
,
904 SDC_DCRC_STS_POS
| SDC_DCRC_STS_NEG
,
907 dcrc
&= ~SDC_DCRC_STS_NEG
;
908 dev_err(mmc_dev(host
->mmc
),
909 "%d -> TUNE_BREAD<%s> dcrc<0x%x> DATRDDLY0/1<0x%x><0x%x> dsmpl<0x%x>\n",
911 (result
== 0 && dcrc
== 0) ? "PASS" : "FAIL",
912 dcrc
, readl(host
->base
+ MSDC_DAT_RDDLY0
),
913 readl(host
->base
+ MSDC_DAT_RDDLY1
),
916 /* Fix me: result is 0, but dcrc is still exist */
917 if (result
== 0 && dcrc
== 0) {
920 /* there is a case: command timeout, and data phase not processed */
921 if (mrq
->data
->error
!= 0 &&
922 mrq
->data
->error
!= -EIO
) {
923 dev_err(mmc_dev(host
->mmc
),
924 "%d -> TUNE_READ: result<0x%x> cmd_error<%d> data_error<%d>\n",
933 cur_rxdly0
= readl(host
->base
+ MSDC_DAT_RDDLY0
);
934 cur_rxdly1
= readl(host
->base
+ MSDC_DAT_RDDLY1
);
936 /* E1 ECO. YD: Reverse */
937 if (readl(host
->base
+ MSDC_ECO_VER
) >= 4) {
938 orig_dat0
= (cur_rxdly0
>> 24) & 0x1F;
939 orig_dat1
= (cur_rxdly0
>> 16) & 0x1F;
940 orig_dat2
= (cur_rxdly0
>> 8) & 0x1F;
941 orig_dat3
= (cur_rxdly0
>> 0) & 0x1F;
942 orig_dat4
= (cur_rxdly1
>> 24) & 0x1F;
943 orig_dat5
= (cur_rxdly1
>> 16) & 0x1F;
944 orig_dat6
= (cur_rxdly1
>> 8) & 0x1F;
945 orig_dat7
= (cur_rxdly1
>> 0) & 0x1F;
947 orig_dat0
= (cur_rxdly0
>> 0) & 0x1F;
948 orig_dat1
= (cur_rxdly0
>> 8) & 0x1F;
949 orig_dat2
= (cur_rxdly0
>> 16) & 0x1F;
950 orig_dat3
= (cur_rxdly0
>> 24) & 0x1F;
951 orig_dat4
= (cur_rxdly1
>> 0) & 0x1F;
952 orig_dat5
= (cur_rxdly1
>> 8) & 0x1F;
953 orig_dat6
= (cur_rxdly1
>> 16) & 0x1F;
954 orig_dat7
= (cur_rxdly1
>> 24) & 0x1F;
958 cur_dat0
= (dcrc
& (1 << 0) || dcrc
& (1 << 8)) ? ((orig_dat0
+ 1) % 32) : orig_dat0
;
959 cur_dat1
= (dcrc
& (1 << 1) || dcrc
& (1 << 9)) ? ((orig_dat1
+ 1) % 32) : orig_dat1
;
960 cur_dat2
= (dcrc
& (1 << 2) || dcrc
& (1 << 10)) ? ((orig_dat2
+ 1) % 32) : orig_dat2
;
961 cur_dat3
= (dcrc
& (1 << 3) || dcrc
& (1 << 11)) ? ((orig_dat3
+ 1) % 32) : orig_dat3
;
963 cur_dat0
= (dcrc
& (1 << 0)) ? ((orig_dat0
+ 1) % 32) : orig_dat0
;
964 cur_dat1
= (dcrc
& (1 << 1)) ? ((orig_dat1
+ 1) % 32) : orig_dat1
;
965 cur_dat2
= (dcrc
& (1 << 2)) ? ((orig_dat2
+ 1) % 32) : orig_dat2
;
966 cur_dat3
= (dcrc
& (1 << 3)) ? ((orig_dat3
+ 1) % 32) : orig_dat3
;
968 cur_dat4
= (dcrc
& (1 << 4)) ? ((orig_dat4
+ 1) % 32) : orig_dat4
;
969 cur_dat5
= (dcrc
& (1 << 5)) ? ((orig_dat5
+ 1) % 32) : orig_dat5
;
970 cur_dat6
= (dcrc
& (1 << 6)) ? ((orig_dat6
+ 1) % 32) : orig_dat6
;
971 cur_dat7
= (dcrc
& (1 << 7)) ? ((orig_dat7
+ 1) % 32) : orig_dat7
;
973 cur_rxdly0
= (cur_dat0
<< 24) | (cur_dat1
<< 16) | (cur_dat2
<< 8) | (cur_dat3
<< 0);
974 cur_rxdly1
= (cur_dat4
<< 24) | (cur_dat5
<< 16) | (cur_dat6
<< 8) | (cur_dat7
<< 0);
976 writel(cur_rxdly0
, host
->base
+ MSDC_DAT_RDDLY0
);
977 writel(cur_rxdly1
, host
->base
+ MSDC_DAT_RDDLY1
);
979 } while (++rxdly
< 32);
985 static int msdc_tune_bwrite(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
987 struct msdc_host
*host
= mmc_priv(mmc
);
989 u32 wrrdly
, cur_wrrdly
= 0xffffffff, orig_wrrdly
;
990 u32 dsmpl
, cur_dsmpl
, orig_dsmpl
;
991 u32 rxdly
, cur_rxdly0
;
992 u32 orig_dat0
, orig_dat1
, orig_dat2
, orig_dat3
;
993 u32 cur_dat0
, cur_dat1
, cur_dat2
, cur_dat3
;
997 // MSDC_IOCON_DDR50CKD need to check. [Fix me]
999 sdr_get_field(host
->base
+ MSDC_PAD_TUNE
, MSDC_PAD_TUNE_DATWRDLY
,
1001 sdr_get_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_DSPL
, &orig_dsmpl
);
1003 /* Tune Method 2. just DAT0 */
1004 sdr_set_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_DDLSEL
, 1);
1005 cur_rxdly0
= readl(host
->base
+ MSDC_DAT_RDDLY0
);
1007 /* E1 ECO. YD: Reverse */
1008 if (readl(host
->base
+ MSDC_ECO_VER
) >= 4) {
1009 orig_dat0
= (cur_rxdly0
>> 24) & 0x1F;
1010 orig_dat1
= (cur_rxdly0
>> 16) & 0x1F;
1011 orig_dat2
= (cur_rxdly0
>> 8) & 0x1F;
1012 orig_dat3
= (cur_rxdly0
>> 0) & 0x1F;
1014 orig_dat0
= (cur_rxdly0
>> 0) & 0x1F;
1015 orig_dat1
= (cur_rxdly0
>> 8) & 0x1F;
1016 orig_dat2
= (cur_rxdly0
>> 16) & 0x1F;
1017 orig_dat3
= (cur_rxdly0
>> 24) & 0x1F;
1024 for (dsmpl
= 0; dsmpl
< 2; dsmpl
++) {
1025 cur_dsmpl
= (orig_dsmpl
+ dsmpl
) % 2;
1030 sdr_set_field(host
->base
+ MSDC_IOCON
,
1031 MSDC_IOCON_DSPL
, cur_dsmpl
);
1033 if (host
->app_cmd
) {
1034 result
= msdc_app_cmd(host
->mmc
, host
);
1036 dev_err(mmc_dev(host
->mmc
),
1037 "%d -> TUNE_BWRITE app_cmd<%d> failed\n",
1039 host
->mrq
->cmd
->opcode
);
1043 result
= msdc_do_request(mmc
, mrq
);
1045 dev_err(mmc_dev(host
->mmc
),
1046 "%d -> TUNE_BWRITE<%s> DSPL<%d> DATWRDLY<%d> MSDC_DAT_RDDLY0<0x%x>\n",
1048 result
== 0 ? "PASS" : "FAIL",
1049 cur_dsmpl
, cur_wrrdly
, cur_rxdly0
);
1054 /* there is a case: command timeout, and data phase not processed */
1055 if (mrq
->data
->error
!= -EIO
) {
1056 dev_err(mmc_dev(host
->mmc
),
1057 "%d -> TUNE_READ: result<0x%x> cmd_error<%d> data_error<%d>\n",
1065 cur_wrrdly
= (orig_wrrdly
+ wrrdly
+ 1) % 32;
1066 sdr_set_field(host
->base
+ MSDC_PAD_TUNE
,
1067 MSDC_PAD_TUNE_DATWRDLY
, cur_wrrdly
);
1068 } while (++wrrdly
< 32);
1070 cur_dat0
= (orig_dat0
+ rxdly
) % 32; /* only adjust bit-1 for crc */
1071 cur_dat1
= orig_dat1
;
1072 cur_dat2
= orig_dat2
;
1073 cur_dat3
= orig_dat3
;
1075 cur_rxdly0
= (cur_dat0
<< 24) | (cur_dat1
<< 16) | (cur_dat2
<< 8) | (cur_dat3
<< 0);
1076 writel(cur_rxdly0
, host
->base
+ MSDC_DAT_RDDLY0
);
1077 } while (++rxdly
< 32);
1083 static int msdc_get_card_status(struct mmc_host
*mmc
, struct msdc_host
*host
, u32
*status
)
1085 struct mmc_command cmd
;
1086 struct mmc_request mrq
;
1089 memset(&cmd
, 0, sizeof(struct mmc_command
));
1090 cmd
.opcode
= MMC_SEND_STATUS
;
1092 cmd
.arg
= mmc
->card
->rca
<< 16;
1094 dev_err(mmc_dev(host
->mmc
), "%d -> cmd13 mmc card is null\n",
1096 cmd
.arg
= host
->app_cmd_arg
;
1098 cmd
.flags
= MMC_RSP_SPI_R2
| MMC_RSP_R1
| MMC_CMD_AC
;
1100 memset(&mrq
, 0, sizeof(struct mmc_request
));
1101 mrq
.cmd
= &cmd
; cmd
.mrq
= &mrq
;
1104 err
= msdc_do_command(host
, &cmd
, 1, CMD_TIMEOUT
);
1107 *status
= cmd
.resp
[0];
1112 static int msdc_check_busy(struct mmc_host
*mmc
, struct msdc_host
*host
)
1118 err
= msdc_get_card_status(mmc
, host
, &status
);
1122 dev_err(mmc_dev(host
->mmc
), "%d -> cmd<13> resp<0x%x>\n",
1124 } while (R1_CURRENT_STATE(status
) == 7);
1129 /* failed when msdc_do_request */
1130 static int msdc_tune_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1132 struct msdc_host
*host
= mmc_priv(mmc
);
1133 struct mmc_data
*data
;
1134 //u32 base = host->base;
1137 data
= mrq
->cmd
->data
;
1139 read
= data
->flags
& MMC_DATA_READ
? 1 : 0;
1142 if (data
->error
== -EIO
)
1143 ret
= msdc_tune_bread(mmc
, mrq
);
1145 ret
= msdc_check_busy(mmc
, host
);
1147 dev_err(mmc_dev(host
->mmc
),
1148 "%d -> XXX cmd13 wait program done failed\n",
1153 /* Fix me: don't care card status? */
1154 ret
= msdc_tune_bwrite(mmc
, mrq
);
1161 static void msdc_ops_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
1163 struct msdc_host
*host
= mmc_priv(mmc
);
1167 /* start to process */
1168 spin_lock(&host
->lock
);
1172 if (msdc_do_request(mmc
, mrq
)) {
1173 if (host
->hw
->flags
& MSDC_REMOVABLE
&& ralink_soc
== MT762X_SOC_MT7621AT
&& mrq
->data
&& mrq
->data
->error
)
1174 msdc_tune_request(mmc
, mrq
);
1177 /* ==== when request done, check if app_cmd ==== */
1178 if (mrq
->cmd
->opcode
== MMC_APP_CMD
) {
1180 host
->app_cmd_arg
= mrq
->cmd
->arg
; /* save the RCA */
1183 //host->app_cmd_arg = 0;
1188 spin_unlock(&host
->lock
);
1190 mmc_request_done(mmc
, mrq
);
1195 /* called by ops.set_ios */
1196 static void msdc_set_buswidth(struct msdc_host
*host
, u32 width
)
1198 u32 val
= readl(host
->base
+ SDC_CFG
);
1200 val
&= ~SDC_CFG_BUSWIDTH
;
1204 case MMC_BUS_WIDTH_1
:
1206 val
|= (MSDC_BUS_1BITS
<< 16);
1208 case MMC_BUS_WIDTH_4
:
1209 val
|= (MSDC_BUS_4BITS
<< 16);
1211 case MMC_BUS_WIDTH_8
:
1212 val
|= (MSDC_BUS_8BITS
<< 16);
1216 writel(val
, host
->base
+ SDC_CFG
);
1220 static void msdc_ops_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1222 struct msdc_host
*host
= mmc_priv(mmc
);
1225 #ifdef MT6575_SD_DEBUG
1226 static char *vdd
[] = {
1227 "1.50v", "1.55v", "1.60v", "1.65v", "1.70v", "1.80v", "1.90v",
1228 "2.00v", "2.10v", "2.20v", "2.30v", "2.40v", "2.50v", "2.60v",
1229 "2.70v", "2.80v", "2.90v", "3.00v", "3.10v", "3.20v", "3.30v",
1230 "3.40v", "3.50v", "3.60v"
1232 static char *power_mode
[] = {
1235 static char *bus_mode
[] = {
1236 "UNKNOWN", "OPENDRAIN", "PUSHPULL"
1238 static char *timing
[] = {
1239 "LEGACY", "MMC_HS", "SD_HS"
1242 printk("SET_IOS: CLK(%dkHz), BUS(%s), BW(%u), PWR(%s), VDD(%s), TIMING(%s)",
1243 ios
->clock
/ 1000, bus_mode
[ios
->bus_mode
],
1244 (ios
->bus_width
== MMC_BUS_WIDTH_4
) ? 4 : 1,
1245 power_mode
[ios
->power_mode
], vdd
[ios
->vdd
], timing
[ios
->timing
]);
1248 msdc_set_buswidth(host
, ios
->bus_width
);
1250 /* Power control ??? */
1251 switch (ios
->power_mode
) {
1256 host
->power_mode
= MMC_POWER_ON
;
1263 if (host
->mclk
!= ios
->clock
) {
1264 if (ios
->clock
> 25000000) {
1265 //if (!(host->hw->flags & MSDC_REMOVABLE)) {
1266 sdr_set_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_RSPL
,
1268 sdr_set_field(host
->base
+ MSDC_IOCON
, MSDC_IOCON_DSPL
,
1270 //} /* for tuning debug */
1271 } else { /* default value */
1272 writel(0x00000000, host
->base
+ MSDC_IOCON
);
1273 // writel(0x00000000, host->base + MSDC_DAT_RDDLY0);
1275 // for MT7620 E2 and afterward
1276 writel(0x10101010, host
->base
+ MSDC_DAT_RDDLY0
);
1278 writel(0x00000000, host
->base
+ MSDC_DAT_RDDLY1
);
1279 // writel(0x00000000, host->base + MSDC_PAD_TUNE);
1281 // for MT7620 E2 and afterward
1282 writel(0x84101010, host
->base
+ MSDC_PAD_TUNE
);
1284 msdc_set_mclk(host
, ddr
, ios
->clock
);
1289 static int msdc_ops_get_ro(struct mmc_host
*mmc
)
1291 struct msdc_host
*host
= mmc_priv(mmc
);
1292 unsigned long flags
;
1295 if (host
->hw
->flags
& MSDC_WP_PIN_EN
) { /* set for card */
1296 spin_lock_irqsave(&host
->lock
, flags
);
1297 ro
= (readl(host
->base
+ MSDC_PS
) >> 31);
1298 spin_unlock_irqrestore(&host
->lock
, flags
);
1304 static int msdc_ops_get_cd(struct mmc_host
*mmc
)
1306 struct msdc_host
*host
= mmc_priv(mmc
);
1307 unsigned long flags
;
1310 /* for sdio, MSDC_REMOVABLE not set, always return 1 */
1311 if (!(host
->hw
->flags
& MSDC_REMOVABLE
)) {
1312 /* For sdio, read H/W always get<1>, but may timeout some times */
1314 host
->card_inserted
= 1;
1317 host
->card_inserted
= (host
->pm_state
.event
== PM_EVENT_USER_RESUME
) ? 1 : 0;
1318 return host
->card_inserted
;
1322 /* MSDC_CD_PIN_EN set for card */
1323 if (host
->hw
->flags
& MSDC_CD_PIN_EN
) {
1324 spin_lock_irqsave(&host
->lock
, flags
);
1326 present
= readl(host
->base
+ MSDC_PS
) & MSDC_PS_CDSTS
;
1328 present
= present
? 0 : 1;
1330 present
= present
? 1 : 0;
1331 host
->card_inserted
= present
;
1332 spin_unlock_irqrestore(&host
->lock
, flags
);
1334 present
= 0; /* TODO? Check DAT3 pins for card detection */
1340 static struct mmc_host_ops mt_msdc_ops
= {
1341 .request
= msdc_ops_request
,
1342 .set_ios
= msdc_ops_set_ios
,
1343 .get_ro
= msdc_ops_get_ro
,
1344 .get_cd
= msdc_ops_get_cd
,
1347 /*--------------------------------------------------------------------------*/
1348 /* interrupt handler */
1349 /*--------------------------------------------------------------------------*/
1350 static irqreturn_t
msdc_irq(int irq
, void *dev_id
)
1352 struct msdc_host
*host
= (struct msdc_host
*)dev_id
;
1353 struct mmc_data
*data
= host
->data
;
1354 struct mmc_command
*cmd
= host
->cmd
;
1356 u32 cmdsts
= MSDC_INT_RSPCRCERR
| MSDC_INT_CMDTMO
| MSDC_INT_CMDRDY
|
1357 MSDC_INT_ACMDCRCERR
| MSDC_INT_ACMDTMO
| MSDC_INT_ACMDRDY
|
1358 MSDC_INT_ACMD19_DONE
;
1359 u32 datsts
= MSDC_INT_DATCRCERR
| MSDC_INT_DATTMO
;
1361 u32 intsts
= readl(host
->base
+ MSDC_INT
);
1362 u32 inten
= readl(host
->base
+ MSDC_INTEN
); inten
&= intsts
;
1364 writel(intsts
, host
->base
+ MSDC_INT
); /* clear interrupts */
1365 /* MSG will cause fatal error */
1367 /* card change interrupt */
1368 if (intsts
& MSDC_INT_CDSC
) {
1369 if (host
->mmc
->caps
& MMC_CAP_NEEDS_POLL
)
1371 schedule_delayed_work(&host
->card_delaywork
, HZ
);
1372 /* tuning when plug card ? */
1375 /* transfer complete interrupt */
1377 if (inten
& MSDC_INT_XFER_COMPL
) {
1378 data
->bytes_xfered
= host
->xfer_size
;
1379 complete(&host
->xfer_done
);
1382 if (intsts
& datsts
) {
1383 /* do basic reset, or stop command will sdc_busy */
1384 msdc_reset_hw(host
);
1385 msdc_clr_fifo(host
);
1388 if (intsts
& MSDC_INT_DATTMO
)
1389 data
->error
= -ETIMEDOUT
;
1390 else if (intsts
& MSDC_INT_DATCRCERR
)
1393 //if(readl(MSDC_INTEN) & MSDC_INT_XFER_COMPL) {
1394 complete(&host
->xfer_done
); /* Read CRC come fast, XFER_COMPL not enabled */
1398 /* command interrupts */
1399 if (cmd
&& (intsts
& cmdsts
)) {
1400 if ((intsts
& MSDC_INT_CMDRDY
) || (intsts
& MSDC_INT_ACMDRDY
) ||
1401 (intsts
& MSDC_INT_ACMD19_DONE
)) {
1402 u32
*rsp
= &cmd
->resp
[0];
1404 switch (host
->cmd_rsp
) {
1408 *rsp
++ = readl(host
->base
+ SDC_RESP3
);
1409 *rsp
++ = readl(host
->base
+ SDC_RESP2
);
1410 *rsp
++ = readl(host
->base
+ SDC_RESP1
);
1411 *rsp
++ = readl(host
->base
+ SDC_RESP0
);
1413 default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
1414 if ((intsts
& MSDC_INT_ACMDRDY
) || (intsts
& MSDC_INT_ACMD19_DONE
))
1415 *rsp
= readl(host
->base
+ SDC_ACMD_RESP
);
1417 *rsp
= readl(host
->base
+ SDC_RESP0
);
1420 } else if ((intsts
& MSDC_INT_RSPCRCERR
) || (intsts
& MSDC_INT_ACMDCRCERR
)) {
1422 } else if ((intsts
& MSDC_INT_CMDTMO
) || (intsts
& MSDC_INT_ACMDTMO
)) {
1423 cmd
->error
= -ETIMEDOUT
;
1424 msdc_reset_hw(host
);
1425 msdc_clr_fifo(host
);
1428 complete(&host
->cmd_done
);
1431 /* mmc irq interrupts */
1432 if (intsts
& MSDC_INT_MMCIRQ
)
1433 dev_info(mmc_dev(host
->mmc
), "msdc[%d] MMCIRQ: SDC_CSTS=0x%.8x\r\n",
1434 host
->id
, readl(host
->base
+ SDC_CSTS
));
1439 /*--------------------------------------------------------------------------*/
1440 /* platform_driver members */
1441 /*--------------------------------------------------------------------------*/
1442 /* called by msdc_drv_probe/remove */
1443 static void msdc_enable_cd_irq(struct msdc_host
*host
, int enable
)
1445 struct msdc_hw
*hw
= host
->hw
;
1447 /* for sdio, not set */
1448 if ((hw
->flags
& MSDC_CD_PIN_EN
) == 0) {
1449 /* Pull down card detection pin since it is not avaiable */
1451 if (hw->config_gpio_pin)
1452 hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_DOWN);
1454 sdr_clr_bits(host
->base
+ MSDC_PS
, MSDC_PS_CDEN
);
1455 sdr_clr_bits(host
->base
+ MSDC_INTEN
, MSDC_INTEN_CDSC
);
1456 sdr_clr_bits(host
->base
+ SDC_CFG
, SDC_CFG_INSWKUP
);
1461 /* card detection circuit relies on the core power so that the core power
1462 * shouldn't be turned off. Here adds a reference count to keep
1463 * the core power alive.
1466 if (hw
->config_gpio_pin
) /* NULL */
1467 hw
->config_gpio_pin(MSDC_CD_PIN
, GPIO_PULL_UP
);
1469 sdr_set_field(host
->base
+ MSDC_PS
, MSDC_PS_CDDEBOUNCE
,
1471 sdr_set_bits(host
->base
+ MSDC_PS
, MSDC_PS_CDEN
);
1472 sdr_set_bits(host
->base
+ MSDC_INTEN
, MSDC_INTEN_CDSC
);
1474 /* not in document! Fix me */
1475 sdr_set_bits(host
->base
+ SDC_CFG
, SDC_CFG_INSWKUP
);
1477 if (hw
->config_gpio_pin
) /* NULL */
1478 hw
->config_gpio_pin(MSDC_CD_PIN
, GPIO_PULL_DOWN
);
1480 sdr_clr_bits(host
->base
+ SDC_CFG
, SDC_CFG_INSWKUP
);
1481 sdr_clr_bits(host
->base
+ MSDC_PS
, MSDC_PS_CDEN
);
1482 sdr_clr_bits(host
->base
+ MSDC_INTEN
, MSDC_INTEN_CDSC
);
1484 /* Here decreases a reference count to core power since card
1485 * detection circuit is shutdown.
1490 /* called by msdc_drv_probe */
1491 static void msdc_init_hw(struct msdc_host
*host
)
1494 /* Configure to MMC/SD mode */
1495 sdr_set_field(host
->base
+ MSDC_CFG
, MSDC_CFG_MODE
, MSDC_SDMMC
);
1498 msdc_reset_hw(host
);
1499 msdc_clr_fifo(host
);
1501 /* Disable card detection */
1502 sdr_clr_bits(host
->base
+ MSDC_PS
, MSDC_PS_CDEN
);
1504 /* Disable and clear all interrupts */
1505 sdr_clr_bits(host
->base
+ MSDC_INTEN
, readl(host
->base
+ MSDC_INTEN
));
1506 writel(readl(host
->base
+ MSDC_INT
), host
->base
+ MSDC_INT
);
1509 /* reset tuning parameter */
1510 writel(0x00090000, host
->base
+ MSDC_PAD_CTL0
);
1511 writel(0x000A0000, host
->base
+ MSDC_PAD_CTL1
);
1512 writel(0x000A0000, host
->base
+ MSDC_PAD_CTL2
);
1513 // writel( 0x00000000, host->base + MSDC_PAD_TUNE);
1515 // for MT7620 E2 and afterward
1516 writel(0x84101010, host
->base
+ MSDC_PAD_TUNE
);
1518 // writel(0x00000000, host->base + MSDC_DAT_RDDLY0);
1520 // for MT7620 E2 and afterward
1521 writel(0x10101010, host
->base
+ MSDC_DAT_RDDLY0
);
1523 writel(0x00000000, host
->base
+ MSDC_DAT_RDDLY1
);
1524 writel(0x00000000, host
->base
+ MSDC_IOCON
);
1526 if (readl(host
->base
+ MSDC_ECO_VER
) >= 4) {
1527 if (host
->id
== 1) {
1528 sdr_set_field(host
->base
+ MSDC_PATCH_BIT1
,
1529 MSDC_PATCH_BIT1_WRDAT_CRCS
, 1);
1530 sdr_set_field(host
->base
+ MSDC_PATCH_BIT1
,
1531 MSDC_PATCH_BIT1_CMD_RSP
, 1);
1533 /* internal clock: latch read data */
1534 sdr_set_bits(host
->base
+ MSDC_PATCH_BIT0
,
1535 MSDC_PATCH_BIT_CKGEN_CK
);
1540 /* for safety, should clear SDC_CFG.SDIO_INT_DET_EN & set SDC_CFG.SDIO in
1541 pre-loader,uboot,kernel drivers. and SDC_CFG.SDIO_INT_DET_EN will be only
1542 set when kernel driver wants to use SDIO bus interrupt */
1543 /* Configure to enable SDIO mode. it's must otherwise sdio cmd5 failed */
1544 sdr_set_bits(host
->base
+ SDC_CFG
, SDC_CFG_SDIO
);
1546 /* disable detect SDIO device interupt function */
1547 sdr_clr_bits(host
->base
+ SDC_CFG
, SDC_CFG_SDIOIDE
);
1549 /* eneable SMT for glitch filter */
1550 sdr_set_bits(host
->base
+ MSDC_PAD_CTL0
, MSDC_PAD_CTL0_CLKSMT
);
1551 sdr_set_bits(host
->base
+ MSDC_PAD_CTL1
, MSDC_PAD_CTL1_CMDSMT
);
1552 sdr_set_bits(host
->base
+ MSDC_PAD_CTL2
, MSDC_PAD_CTL2_DATSMT
);
1555 /* set clk, cmd, dat pad driving */
1556 sdr_set_field(host
->base
+ MSDC_PAD_CTL0
, MSDC_PAD_CTL0_CLKDRVN
, 4);
1557 sdr_set_field(host
->base
+ MSDC_PAD_CTL0
, MSDC_PAD_CTL0_CLKDRVP
, 4);
1558 sdr_set_field(host
->base
+ MSDC_PAD_CTL1
, MSDC_PAD_CTL1_CMDDRVN
, 4);
1559 sdr_set_field(host
->base
+ MSDC_PAD_CTL1
, MSDC_PAD_CTL1_CMDDRVP
, 4);
1560 sdr_set_field(host
->base
+ MSDC_PAD_CTL2
, MSDC_PAD_CTL2_DATDRVN
, 4);
1561 sdr_set_field(host
->base
+ MSDC_PAD_CTL2
, MSDC_PAD_CTL2_DATDRVP
, 4);
1563 sdr_set_field(host
->base
+ MSDC_PAD_CTL0
, MSDC_PAD_CTL0_CLKDRVN
, 0);
1564 sdr_set_field(host
->base
+ MSDC_PAD_CTL0
, MSDC_PAD_CTL0_CLKDRVP
, 0);
1565 sdr_set_field(host
->base
+ MSDC_PAD_CTL1
, MSDC_PAD_CTL1_CMDDRVN
, 0);
1566 sdr_set_field(host
->base
+ MSDC_PAD_CTL1
, MSDC_PAD_CTL1_CMDDRVP
, 0);
1567 sdr_set_field(host
->base
+ MSDC_PAD_CTL2
, MSDC_PAD_CTL2_DATDRVN
, 0);
1568 sdr_set_field(host
->base
+ MSDC_PAD_CTL2
, MSDC_PAD_CTL2_DATDRVP
, 0);
1571 /* set sampling edge */
1573 /* write crc timeout detection */
1574 sdr_set_field(host
->base
+ MSDC_PATCH_BIT0
, 1 << 30, 1);
1576 /* Configure to default data timeout */
1577 sdr_set_field(host
->base
+ SDC_CFG
, SDC_CFG_DTOC
, DEFAULT_DTOC
);
1579 msdc_set_buswidth(host
, MMC_BUS_WIDTH_1
);
1582 /* called by msdc_drv_remove */
1583 static void msdc_deinit_hw(struct msdc_host
*host
)
1585 /* Disable and clear all interrupts */
1586 sdr_clr_bits(host
->base
+ MSDC_INTEN
, readl(host
->base
+ MSDC_INTEN
));
1587 writel(readl(host
->base
+ MSDC_INT
), host
->base
+ MSDC_INT
);
1589 /* Disable card detection */
1590 msdc_enable_cd_irq(host
, 0);
1593 /* init gpd and bd list in msdc_drv_probe */
1594 static void msdc_init_gpd_bd(struct msdc_host
*host
, struct msdc_dma
*dma
)
1596 struct gpd
*gpd
= dma
->gpd
;
1597 struct bd
*bd
= dma
->bd
;
1600 /* we just support one gpd, but gpd->next must be set for desc
1601 * DMA. That's why we alloc 2 gpd structurs.
1604 memset(gpd
, 0, sizeof(struct gpd
) * 2);
1606 gpd
->bdp
= 1; /* hwo, cs, bd pointer */
1607 gpd
->ptr
= (void *)dma
->bd_addr
; /* physical address */
1608 gpd
->next
= (void *)((u32
)dma
->gpd_addr
+ sizeof(struct gpd
));
1610 memset(bd
, 0, sizeof(struct bd
) * MAX_BD_NUM
);
1611 for (i
= 0; i
< (MAX_BD_NUM
- 1); i
++)
1612 bd
[i
].next
= (void *)(dma
->bd_addr
+ sizeof(*bd
) * (i
+ 1));
1615 static int msdc_drv_probe(struct platform_device
*pdev
)
1617 struct resource
*res
;
1619 struct mmc_host
*mmc
;
1620 struct msdc_host
*host
;
1626 if (of_property_read_bool(pdev
->dev
.of_node
, "mtk,wp-en"))
1627 msdc0_hw
.flags
|= MSDC_WP_PIN_EN
;
1629 /* Allocate MMC host for this device */
1630 mmc
= mmc_alloc_host(sizeof(struct msdc_host
), &pdev
->dev
);
1634 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1635 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1637 ret
= PTR_ERR(base
);
1641 /* Set host parameters to mmc */
1642 mmc
->ops
= &mt_msdc_ops
;
1643 mmc
->f_min
= HOST_MIN_MCLK
;
1644 mmc
->f_max
= HOST_MAX_MCLK
;
1645 mmc
->ocr_avail
= MSDC_OCR_AVAIL
;
1647 mmc
->caps
= MMC_CAP_MMC_HIGHSPEED
| MMC_CAP_SD_HIGHSPEED
;
1649 //TODO: read this as bus-width from dt (via mmc_of_parse)
1650 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1652 cd_active_low
= !of_property_read_bool(pdev
->dev
.of_node
, "mediatek,cd-high");
1654 if (of_property_read_bool(pdev
->dev
.of_node
, "mediatek,cd-poll"))
1655 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
1657 /* MMC core transfer sizes tunable parameters */
1658 mmc
->max_segs
= MAX_HW_SGMTS
;
1660 mmc
->max_seg_size
= MAX_SGMT_SZ
;
1661 mmc
->max_blk_size
= HOST_MAX_BLKSZ
;
1662 mmc
->max_req_size
= MAX_REQ_SZ
;
1663 mmc
->max_blk_count
= mmc
->max_req_size
;
1665 host
= mmc_priv(mmc
);
1668 host
->id
= pdev
->id
;
1669 if (host
->id
< 0 || host
->id
>= 4)
1673 host
->irq
= platform_get_irq(pdev
, 0);
1674 if (host
->irq
< 0) {
1680 host
->mclk
= 0; /* mclk: the request clock of mmc sub-system */
1681 host
->hclk
= hclks
[hw
->clk_src
]; /* hclk: clock of clock source to msdc controller */
1682 host
->sclk
= 0; /* sclk: the really clock after divition */
1683 host
->pm_state
= PMSG_RESUME
;
1685 host
->core_clkon
= 0;
1686 host
->card_clkon
= 0;
1687 host
->core_power
= 0;
1688 host
->power_mode
= MMC_POWER_OFF
;
1689 // host->card_inserted = hw->flags & MSDC_REMOVABLE ? 0 : 1;
1690 host
->timeout_ns
= 0;
1691 host
->timeout_clks
= DEFAULT_DTOC
* 65536;
1694 //init_MUTEX(&host->sem); /* we don't need to support multiple threads access */
1696 mmc_dev(mmc
)->dma_mask
= NULL
;
1698 /* using dma_alloc_coherent*/ /* todo: using 1, for all 4 slots */
1699 host
->dma
.gpd
= dma_alloc_coherent(&pdev
->dev
,
1700 MAX_GPD_NUM
* sizeof(struct gpd
),
1701 &host
->dma
.gpd_addr
, GFP_KERNEL
);
1702 host
->dma
.bd
= dma_alloc_coherent(&pdev
->dev
,
1703 MAX_BD_NUM
* sizeof(struct bd
),
1704 &host
->dma
.bd_addr
, GFP_KERNEL
);
1705 if (!host
->dma
.gpd
|| !host
->dma
.bd
) {
1709 msdc_init_gpd_bd(host
, &host
->dma
);
1711 INIT_DELAYED_WORK(&host
->card_delaywork
, msdc_tasklet_card
);
1712 spin_lock_init(&host
->lock
);
1715 /* TODO check weather flags 0 is correct, the mtk-sd driver uses
1716 * IRQF_TRIGGER_LOW | IRQF_ONESHOT for flags
1718 * for flags 0 the trigger polarity is determined by the
1719 * device tree, but not the oneshot flag, but maybe it is also
1720 * not needed because the soc could be oneshot safe.
1722 ret
= devm_request_irq(&pdev
->dev
, host
->irq
, msdc_irq
, 0, pdev
->name
,
1727 platform_set_drvdata(pdev
, mmc
);
1729 ret
= mmc_add_host(mmc
);
1733 /* Config card detection pin and enable interrupts */
1734 if (hw
->flags
& MSDC_CD_PIN_EN
) { /* set for card */
1735 msdc_enable_cd_irq(host
, 1);
1737 msdc_enable_cd_irq(host
, 0);
1743 platform_set_drvdata(pdev
, NULL
);
1744 msdc_deinit_hw(host
);
1745 cancel_delayed_work_sync(&host
->card_delaywork
);
1749 dma_free_coherent(&pdev
->dev
, MAX_GPD_NUM
* sizeof(struct gpd
),
1750 host
->dma
.gpd
, host
->dma
.gpd_addr
);
1752 dma_free_coherent(&pdev
->dev
, MAX_BD_NUM
* sizeof(struct bd
),
1753 host
->dma
.bd
, host
->dma
.bd_addr
);
1760 /* 4 device share one driver, using "drvdata" to show difference */
1761 static int msdc_drv_remove(struct platform_device
*pdev
)
1763 struct mmc_host
*mmc
;
1764 struct msdc_host
*host
;
1766 mmc
= platform_get_drvdata(pdev
);
1769 host
= mmc_priv(mmc
);
1772 dev_err(mmc_dev(host
->mmc
), "%d -> removed !!!\n",
1775 platform_set_drvdata(pdev
, NULL
);
1776 mmc_remove_host(host
->mmc
);
1777 msdc_deinit_hw(host
);
1779 cancel_delayed_work_sync(&host
->card_delaywork
);
1781 dma_free_coherent(&pdev
->dev
, MAX_GPD_NUM
* sizeof(struct gpd
),
1782 host
->dma
.gpd
, host
->dma
.gpd_addr
);
1783 dma_free_coherent(&pdev
->dev
, MAX_BD_NUM
* sizeof(struct bd
),
1784 host
->dma
.bd
, host
->dma
.bd_addr
);
1786 mmc_free_host(host
->mmc
);
1791 /* Fix me: Power Flow */
1794 static void msdc_drv_pm(struct platform_device
*pdev
, pm_message_t state
)
1796 struct mmc_host
*mmc
= platform_get_drvdata(pdev
);
1799 struct msdc_host
*host
= mmc_priv(mmc
);
1800 msdc_pm(state
, (void *)host
);
1804 static int msdc_drv_suspend(struct platform_device
*pdev
, pm_message_t state
)
1806 if (state
.event
== PM_EVENT_SUSPEND
)
1807 msdc_drv_pm(pdev
, state
);
1811 static int msdc_drv_resume(struct platform_device
*pdev
)
1813 struct pm_message state
;
1815 state
.event
= PM_EVENT_RESUME
;
1816 msdc_drv_pm(pdev
, state
);
1821 static const struct of_device_id mt7620_sdhci_match
[] = {
1822 { .compatible
= "ralink,mt7620-sdhci" },
1825 MODULE_DEVICE_TABLE(of
, mt7620_sdhci_match
);
1827 static struct platform_driver mt_msdc_driver
= {
1828 .probe
= msdc_drv_probe
,
1829 .remove
= msdc_drv_remove
,
1831 .suspend
= msdc_drv_suspend
,
1832 .resume
= msdc_drv_resume
,
1836 .of_match_table
= mt7620_sdhci_match
,
1840 /*--------------------------------------------------------------------------*/
1841 /* module init/exit */
1842 /*--------------------------------------------------------------------------*/
1843 static int __init
mt_msdc_init(void)
1848 // Set the pins for sdxc to sdxc mode
1849 //FIXME: this should be done by pinctl and not by the sd driver
1850 reg
= readl((void __iomem
*)(RALINK_SYSCTL_BASE
+ 0x60)) & ~(0x3 << 18);
1851 writel(reg
, (void __iomem
*)(RALINK_SYSCTL_BASE
+ 0x60));
1853 ret
= platform_driver_register(&mt_msdc_driver
);
1855 pr_err("%s: Can't register driver", DRV_NAME
);
1859 #if defined(MT6575_SD_DEBUG)
1860 msdc_debug_proc_init();
1865 static void __exit
mt_msdc_exit(void)
1867 platform_driver_unregister(&mt_msdc_driver
);
1870 module_init(mt_msdc_init
);
1871 module_exit(mt_msdc_exit
);
1872 MODULE_LICENSE("GPL");
1873 MODULE_DESCRIPTION("MediaTek MT6575 SD/MMC Card Driver");
1874 MODULE_AUTHOR("Infinity Chen <infinity.chen@mediatek.com>");