2 * (C) Copyright 2007-2011
3 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
4 * Aaron <leafy.myeh@allwinnertech.com>
6 * MMC driver for allwinner sunxi platform.
8 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/arch/clock.h>
16 #include <asm/arch/cpu.h>
17 #include <asm/arch/gpio.h>
18 #include <asm/arch/mmc.h>
19 #include <asm-generic/gpio.h>
21 struct sunxi_mmc_host
{
25 struct sunxi_mmc
*reg
;
26 struct mmc_config cfg
;
29 /* support 4 mmc hosts */
30 struct sunxi_mmc_host mmc_host
[4];
32 static int sunxi_mmc_getcd_gpio(int sdc_no
)
35 case 0: return sunxi_name_to_gpio(CONFIG_MMC0_CD_PIN
);
36 case 1: return sunxi_name_to_gpio(CONFIG_MMC1_CD_PIN
);
37 case 2: return sunxi_name_to_gpio(CONFIG_MMC2_CD_PIN
);
38 case 3: return sunxi_name_to_gpio(CONFIG_MMC3_CD_PIN
);
43 static int mmc_resource_init(int sdc_no
)
45 struct sunxi_mmc_host
*mmchost
= &mmc_host
[sdc_no
];
46 struct sunxi_ccm_reg
*ccm
= (struct sunxi_ccm_reg
*)SUNXI_CCM_BASE
;
49 debug("init mmc %d resource\n", sdc_no
);
53 mmchost
->reg
= (struct sunxi_mmc
*)SUNXI_MMC0_BASE
;
54 mmchost
->mclkreg
= &ccm
->sd0_clk_cfg
;
57 mmchost
->reg
= (struct sunxi_mmc
*)SUNXI_MMC1_BASE
;
58 mmchost
->mclkreg
= &ccm
->sd1_clk_cfg
;
61 mmchost
->reg
= (struct sunxi_mmc
*)SUNXI_MMC2_BASE
;
62 mmchost
->mclkreg
= &ccm
->sd2_clk_cfg
;
65 mmchost
->reg
= (struct sunxi_mmc
*)SUNXI_MMC3_BASE
;
66 mmchost
->mclkreg
= &ccm
->sd3_clk_cfg
;
69 printf("Wrong mmc number %d\n", sdc_no
);
72 mmchost
->mmc_no
= sdc_no
;
74 cd_pin
= sunxi_mmc_getcd_gpio(sdc_no
);
76 ret
= gpio_request(cd_pin
, "mmc_cd");
78 ret
= gpio_direction_input(cd_pin
);
84 static int mmc_set_mod_clk(struct sunxi_mmc_host
*mmchost
, unsigned int hz
)
86 unsigned int pll
, pll_hz
, div
, n
, oclk_dly
, sclk_dly
;
89 pll
= CCM_MMC_CTRL_OSCM24
;
92 #ifdef CONFIG_MACH_SUN9I
93 pll
= CCM_MMC_CTRL_PLL_PERIPH0
;
94 pll_hz
= clock_get_pll4_periph0();
96 pll
= CCM_MMC_CTRL_PLL6
;
97 pll_hz
= clock_get_pll6();
112 printf("mmc %u error cannot set clock to %u\n",
113 mmchost
->mmc_no
, hz
);
117 /* determine delays */
121 } else if (hz
<= 25000000) {
124 } else if (hz
<= 50000000) {
133 writel(CCM_MMC_CTRL_ENABLE
| pll
| CCM_MMC_CTRL_SCLK_DLY(sclk_dly
) |
134 CCM_MMC_CTRL_N(n
) | CCM_MMC_CTRL_OCLK_DLY(oclk_dly
) |
135 CCM_MMC_CTRL_M(div
), mmchost
->mclkreg
);
137 debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n",
138 mmchost
->mmc_no
, hz
, pll_hz
, 1u << n
, div
,
139 pll_hz
/ (1u << n
) / div
);
144 static int mmc_clk_io_on(int sdc_no
)
146 struct sunxi_mmc_host
*mmchost
= &mmc_host
[sdc_no
];
147 struct sunxi_ccm_reg
*ccm
= (struct sunxi_ccm_reg
*)SUNXI_CCM_BASE
;
149 debug("init mmc %d clock and io\n", sdc_no
);
151 /* config ahb clock */
152 setbits_le32(&ccm
->ahb_gate0
, 1 << AHB_GATE_OFFSET_MMC(sdc_no
));
154 #if defined(CONFIG_MACH_SUN6I) || defined(CONFIG_MACH_SUN8I) || \
155 defined(CONFIG_MACH_SUN9I)
157 setbits_le32(&ccm
->ahb_reset0_cfg
, 1 << AHB_RESET_OFFSET_MMC(sdc_no
));
159 #if defined(CONFIG_MACH_SUN9I)
160 /* sun9i has a mmc-common module, also set the gate and reset there */
161 writel(SUNXI_MMC_COMMON_CLK_GATE
| SUNXI_MMC_COMMON_RESET
,
162 SUNXI_MMC_COMMON_BASE
+ 4 * sdc_no
);
165 return mmc_set_mod_clk(mmchost
, 24000000);
168 static int mmc_update_clk(struct mmc
*mmc
)
170 struct sunxi_mmc_host
*mmchost
= mmc
->priv
;
172 unsigned timeout_msecs
= 2000;
174 cmd
= SUNXI_MMC_CMD_START
|
175 SUNXI_MMC_CMD_UPCLK_ONLY
|
176 SUNXI_MMC_CMD_WAIT_PRE_OVER
;
177 writel(cmd
, &mmchost
->reg
->cmd
);
178 while (readl(&mmchost
->reg
->cmd
) & SUNXI_MMC_CMD_START
) {
179 if (!timeout_msecs
--)
184 /* clock update sets various irq status bits, clear these */
185 writel(readl(&mmchost
->reg
->rint
), &mmchost
->reg
->rint
);
190 static int mmc_config_clock(struct mmc
*mmc
)
192 struct sunxi_mmc_host
*mmchost
= mmc
->priv
;
193 unsigned rval
= readl(&mmchost
->reg
->clkcr
);
196 rval
&= ~SUNXI_MMC_CLK_ENABLE
;
197 writel(rval
, &mmchost
->reg
->clkcr
);
198 if (mmc_update_clk(mmc
))
201 /* Set mod_clk to new rate */
202 if (mmc_set_mod_clk(mmchost
, mmc
->clock
))
205 /* Clear internal divider */
206 rval
&= ~SUNXI_MMC_CLK_DIVIDER_MASK
;
207 writel(rval
, &mmchost
->reg
->clkcr
);
209 /* Re-enable Clock */
210 rval
|= SUNXI_MMC_CLK_ENABLE
;
211 writel(rval
, &mmchost
->reg
->clkcr
);
212 if (mmc_update_clk(mmc
))
218 static void mmc_set_ios(struct mmc
*mmc
)
220 struct sunxi_mmc_host
*mmchost
= mmc
->priv
;
222 debug("set ios: bus_width: %x, clock: %d\n",
223 mmc
->bus_width
, mmc
->clock
);
225 /* Change clock first */
226 if (mmc
->clock
&& mmc_config_clock(mmc
) != 0) {
227 mmchost
->fatal_err
= 1;
231 /* Change bus width */
232 if (mmc
->bus_width
== 8)
233 writel(0x2, &mmchost
->reg
->width
);
234 else if (mmc
->bus_width
== 4)
235 writel(0x1, &mmchost
->reg
->width
);
237 writel(0x0, &mmchost
->reg
->width
);
240 static int mmc_core_init(struct mmc
*mmc
)
242 struct sunxi_mmc_host
*mmchost
= mmc
->priv
;
244 /* Reset controller */
245 writel(SUNXI_MMC_GCTRL_RESET
, &mmchost
->reg
->gctrl
);
251 static int mmc_trans_data_by_cpu(struct mmc
*mmc
, struct mmc_data
*data
)
253 struct sunxi_mmc_host
*mmchost
= mmc
->priv
;
254 const int reading
= !!(data
->flags
& MMC_DATA_READ
);
255 const uint32_t status_bit
= reading
? SUNXI_MMC_STATUS_FIFO_EMPTY
:
256 SUNXI_MMC_STATUS_FIFO_FULL
;
258 unsigned byte_cnt
= data
->blocksize
* data
->blocks
;
259 unsigned timeout_msecs
= 2000;
260 unsigned *buff
= (unsigned int *)(reading
? data
->dest
: data
->src
);
262 /* Always read / write data through the CPU */
263 setbits_le32(&mmchost
->reg
->gctrl
, SUNXI_MMC_GCTRL_ACCESS_BY_AHB
);
265 for (i
= 0; i
< (byte_cnt
>> 2); i
++) {
266 while (readl(&mmchost
->reg
->status
) & status_bit
) {
267 if (!timeout_msecs
--)
273 buff
[i
] = readl(&mmchost
->reg
->fifo
);
275 writel(buff
[i
], &mmchost
->reg
->fifo
);
281 static int mmc_rint_wait(struct mmc
*mmc
, unsigned int timeout_msecs
,
282 unsigned int done_bit
, const char *what
)
284 struct sunxi_mmc_host
*mmchost
= mmc
->priv
;
288 status
= readl(&mmchost
->reg
->rint
);
289 if (!timeout_msecs
-- ||
290 (status
& SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT
)) {
291 debug("%s timeout %x\n", what
,
292 status
& SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT
);
296 } while (!(status
& done_bit
));
301 static int mmc_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
,
302 struct mmc_data
*data
)
304 struct sunxi_mmc_host
*mmchost
= mmc
->priv
;
305 unsigned int cmdval
= SUNXI_MMC_CMD_START
;
306 unsigned int timeout_msecs
;
308 unsigned int status
= 0;
309 unsigned int bytecnt
= 0;
311 if (mmchost
->fatal_err
)
313 if (cmd
->resp_type
& MMC_RSP_BUSY
)
314 debug("mmc cmd %d check rsp busy\n", cmd
->cmdidx
);
315 if (cmd
->cmdidx
== 12)
319 cmdval
|= SUNXI_MMC_CMD_SEND_INIT_SEQ
;
320 if (cmd
->resp_type
& MMC_RSP_PRESENT
)
321 cmdval
|= SUNXI_MMC_CMD_RESP_EXPIRE
;
322 if (cmd
->resp_type
& MMC_RSP_136
)
323 cmdval
|= SUNXI_MMC_CMD_LONG_RESPONSE
;
324 if (cmd
->resp_type
& MMC_RSP_CRC
)
325 cmdval
|= SUNXI_MMC_CMD_CHK_RESPONSE_CRC
;
328 if ((u32
) data
->dest
& 0x3) {
333 cmdval
|= SUNXI_MMC_CMD_DATA_EXPIRE
|SUNXI_MMC_CMD_WAIT_PRE_OVER
;
334 if (data
->flags
& MMC_DATA_WRITE
)
335 cmdval
|= SUNXI_MMC_CMD_WRITE
;
336 if (data
->blocks
> 1)
337 cmdval
|= SUNXI_MMC_CMD_AUTO_STOP
;
338 writel(data
->blocksize
, &mmchost
->reg
->blksz
);
339 writel(data
->blocks
* data
->blocksize
, &mmchost
->reg
->bytecnt
);
342 debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", mmchost
->mmc_no
,
343 cmd
->cmdidx
, cmdval
| cmd
->cmdidx
, cmd
->cmdarg
);
344 writel(cmd
->cmdarg
, &mmchost
->reg
->arg
);
347 writel(cmdval
| cmd
->cmdidx
, &mmchost
->reg
->cmd
);
350 * transfer data and check status
351 * STATREG[2] : FIFO empty
352 * STATREG[3] : FIFO full
357 bytecnt
= data
->blocksize
* data
->blocks
;
358 debug("trans data %d bytes\n", bytecnt
);
359 writel(cmdval
| cmd
->cmdidx
, &mmchost
->reg
->cmd
);
360 ret
= mmc_trans_data_by_cpu(mmc
, data
);
362 error
= readl(&mmchost
->reg
->rint
) & \
363 SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT
;
369 error
= mmc_rint_wait(mmc
, 1000, SUNXI_MMC_RINT_COMMAND_DONE
, "cmd");
375 debug("cacl timeout %x msec\n", timeout_msecs
);
376 error
= mmc_rint_wait(mmc
, timeout_msecs
,
378 SUNXI_MMC_RINT_AUTO_COMMAND_DONE
:
379 SUNXI_MMC_RINT_DATA_OVER
,
385 if (cmd
->resp_type
& MMC_RSP_BUSY
) {
386 timeout_msecs
= 2000;
388 status
= readl(&mmchost
->reg
->status
);
389 if (!timeout_msecs
--) {
390 debug("busy timeout\n");
395 } while (status
& SUNXI_MMC_STATUS_CARD_DATA_BUSY
);
398 if (cmd
->resp_type
& MMC_RSP_136
) {
399 cmd
->response
[0] = readl(&mmchost
->reg
->resp3
);
400 cmd
->response
[1] = readl(&mmchost
->reg
->resp2
);
401 cmd
->response
[2] = readl(&mmchost
->reg
->resp1
);
402 cmd
->response
[3] = readl(&mmchost
->reg
->resp0
);
403 debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
404 cmd
->response
[3], cmd
->response
[2],
405 cmd
->response
[1], cmd
->response
[0]);
407 cmd
->response
[0] = readl(&mmchost
->reg
->resp0
);
408 debug("mmc resp 0x%08x\n", cmd
->response
[0]);
412 writel(SUNXI_MMC_GCTRL_RESET
, &mmchost
->reg
->gctrl
);
415 writel(0xffffffff, &mmchost
->reg
->rint
);
416 writel(readl(&mmchost
->reg
->gctrl
) | SUNXI_MMC_GCTRL_FIFO_RESET
,
417 &mmchost
->reg
->gctrl
);
422 static int sunxi_mmc_getcd(struct mmc
*mmc
)
424 struct sunxi_mmc_host
*mmchost
= mmc
->priv
;
427 cd_pin
= sunxi_mmc_getcd_gpio(mmchost
->mmc_no
);
431 return !gpio_get_value(cd_pin
);
434 static const struct mmc_ops sunxi_mmc_ops
= {
435 .send_cmd
= mmc_send_cmd
,
436 .set_ios
= mmc_set_ios
,
437 .init
= mmc_core_init
,
438 .getcd
= sunxi_mmc_getcd
,
441 struct mmc
*sunxi_mmc_init(int sdc_no
)
443 struct mmc_config
*cfg
= &mmc_host
[sdc_no
].cfg
;
445 memset(&mmc_host
[sdc_no
], 0, sizeof(struct sunxi_mmc_host
));
447 cfg
->name
= "SUNXI SD/MMC";
448 cfg
->ops
= &sunxi_mmc_ops
;
450 cfg
->voltages
= MMC_VDD_32_33
| MMC_VDD_33_34
;
451 cfg
->host_caps
= MMC_MODE_4BIT
;
452 cfg
->host_caps
|= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
453 #if defined(CONFIG_MACH_SUN6I) || defined(CONFIG_MACH_SUN7I) || \
454 defined(CONFIG_MACH_SUN8I) || defined(CONFIG_MACH_SUN9I)
455 cfg
->host_caps
|= MMC_MODE_HC
;
457 cfg
->b_max
= CONFIG_SYS_MMC_MAX_BLK_COUNT
;
460 cfg
->f_max
= 52000000;
462 if (mmc_resource_init(sdc_no
) != 0)
465 mmc_clk_io_on(sdc_no
);
467 return mmc_create(cfg
, &mmc_host
[sdc_no
]);