]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
e24ea55c IC |
2 | /* |
3 | * (C) Copyright 2007-2011 | |
4 | * Allwinner Technology Co., Ltd. <www.allwinnertech.com> | |
5 | * Aaron <[email protected]> | |
6 | * | |
7 | * MMC driver for allwinner sunxi platform. | |
ba16b531 AP |
8 | * |
9 | * This driver is used by the (ARM) SPL with the legacy MMC interface, and | |
10 | * by U-Boot proper using the full DM interface. The actual hardware access | |
11 | * code is common, and comes first in this file. | |
12 | * The legacy MMC interface implementation comes next, followed by the | |
13 | * proper DM_MMC implementation at the end. | |
e24ea55c IC |
14 | */ |
15 | ||
d678a59d | 16 | #include <common.h> |
dd27918c | 17 | #include <dm.h> |
90641f82 | 18 | #include <errno.h> |
f7ae49fc | 19 | #include <log.h> |
e24ea55c IC |
20 | #include <malloc.h> |
21 | #include <mmc.h> | |
c57572eb AP |
22 | #include <clk.h> |
23 | #include <reset.h> | |
42508461 | 24 | #include <asm/gpio.h> |
e24ea55c IC |
25 | #include <asm/io.h> |
26 | #include <asm/arch/clock.h> | |
27 | #include <asm/arch/cpu.h> | |
43b573df | 28 | #if !CONFIG_IS_ENABLED(DM_MMC) |
e24ea55c | 29 | #include <asm/arch/mmc.h> |
43b573df | 30 | #endif |
c05ed00a | 31 | #include <linux/delay.h> |
207ed0a3 | 32 | #include <sunxi_gpio.h> |
e24ea55c | 33 | |
43b573df SH |
34 | #include "sunxi_mmc.h" |
35 | ||
f85c0912 AP |
36 | #ifndef CCM_MMC_CTRL_MODE_SEL_NEW |
37 | #define CCM_MMC_CTRL_MODE_SEL_NEW 0 | |
38 | #endif | |
39 | ||
dd27918c SG |
40 | struct sunxi_mmc_plat { |
41 | struct mmc_config cfg; | |
42 | struct mmc mmc; | |
43 | }; | |
44 | ||
e3c794e2 | 45 | struct sunxi_mmc_priv { |
e24ea55c IC |
46 | unsigned mmc_no; |
47 | uint32_t *mclkreg; | |
e24ea55c | 48 | unsigned fatal_err; |
dd27918c | 49 | struct gpio_desc cd_gpio; /* Change Detect GPIO */ |
e24ea55c IC |
50 | struct sunxi_mmc *reg; |
51 | struct mmc_config cfg; | |
52 | }; | |
53 | ||
b5dd39c9 AP |
54 | /* |
55 | * All A64 and later MMC controllers feature auto-calibration. This would | |
56 | * normally be detected via the compatible string, but we need something | |
57 | * which works in the SPL as well. | |
58 | */ | |
59 | static bool sunxi_mmc_can_calibrate(void) | |
60 | { | |
61 | return IS_ENABLED(CONFIG_MACH_SUN50I) || | |
62 | IS_ENABLED(CONFIG_MACH_SUN50I_H5) || | |
63 | IS_ENABLED(CONFIG_SUN50I_GEN_H6) || | |
4a9e89a3 | 64 | IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2) || |
b5dd39c9 AP |
65 | IS_ENABLED(CONFIG_MACH_SUN8I_R40); |
66 | } | |
67 | ||
3f5af12a | 68 | static int mmc_set_mod_clk(struct sunxi_mmc_priv *priv, unsigned int hz) |
fc3a8325 HG |
69 | { |
70 | unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly; | |
f85c0912 | 71 | bool new_mode = IS_ENABLED(CONFIG_MMC_SUNXI_HAS_NEW_MODE); |
de9b1771 MR |
72 | u32 val = 0; |
73 | ||
0e21a2ff VK |
74 | /* A83T support new mode only on eMMC */ |
75 | if (IS_ENABLED(CONFIG_MACH_SUN8I_A83T) && priv->mmc_no != 2) | |
76 | new_mode = false; | |
de9b1771 | 77 | |
fc3a8325 HG |
78 | if (hz <= 24000000) { |
79 | pll = CCM_MMC_CTRL_OSCM24; | |
80 | pll_hz = 24000000; | |
81 | } else { | |
daf22636 HG |
82 | #ifdef CONFIG_MACH_SUN9I |
83 | pll = CCM_MMC_CTRL_PLL_PERIPH0; | |
84 | pll_hz = clock_get_pll4_periph0(); | |
85 | #else | |
937ee31e AP |
86 | /* |
87 | * SoCs since the A64 (H5, H6, H616) actually use the doubled | |
88 | * rate of PLL6/PERIPH0 as an input clock, but compensate for | |
89 | * that with a fixed post-divider of 2 in the mod clock. | |
90 | * This cancels each other out, so for simplicity we just | |
91 | * pretend it's always PLL6 without a post divider here. | |
92 | */ | |
fc3a8325 HG |
93 | pll = CCM_MMC_CTRL_PLL6; |
94 | pll_hz = clock_get_pll6(); | |
daf22636 | 95 | #endif |
fc3a8325 HG |
96 | } |
97 | ||
98 | div = pll_hz / hz; | |
99 | if (pll_hz % hz) | |
100 | div++; | |
101 | ||
102 | n = 0; | |
103 | while (div > 16) { | |
104 | n++; | |
105 | div = (div + 1) / 2; | |
106 | } | |
107 | ||
108 | if (n > 3) { | |
3f5af12a SG |
109 | printf("mmc %u error cannot set clock to %u\n", priv->mmc_no, |
110 | hz); | |
fc3a8325 HG |
111 | return -1; |
112 | } | |
113 | ||
114 | /* determine delays */ | |
115 | if (hz <= 400000) { | |
116 | oclk_dly = 0; | |
be90974c | 117 | sclk_dly = 0; |
fc3a8325 HG |
118 | } else if (hz <= 25000000) { |
119 | oclk_dly = 0; | |
120 | sclk_dly = 5; | |
be90974c | 121 | } else { |
f4826fb1 AP |
122 | if (IS_ENABLED(CONFIG_MACH_SUN9I)) { |
123 | if (hz <= 52000000) | |
124 | oclk_dly = 5; | |
125 | else | |
126 | oclk_dly = 2; | |
127 | } else { | |
128 | if (hz <= 52000000) | |
129 | oclk_dly = 3; | |
130 | else | |
131 | oclk_dly = 1; | |
132 | } | |
be90974c | 133 | sclk_dly = 4; |
fc3a8325 HG |
134 | } |
135 | ||
de9b1771 | 136 | if (new_mode) { |
f85c0912 | 137 | val |= CCM_MMC_CTRL_MODE_SEL_NEW; |
8a647fc3 | 138 | setbits_le32(&priv->reg->ntsr, SUNXI_MMC_NTSR_MODE_SEL_NEW); |
b5dd39c9 AP |
139 | } |
140 | ||
141 | if (!sunxi_mmc_can_calibrate()) { | |
20940ef2 VK |
142 | /* |
143 | * Use hardcoded delay values if controller doesn't support | |
144 | * calibration | |
145 | */ | |
de9b1771 MR |
146 | val = CCM_MMC_CTRL_OCLK_DLY(oclk_dly) | |
147 | CCM_MMC_CTRL_SCLK_DLY(sclk_dly); | |
148 | } | |
149 | ||
150 | writel(CCM_MMC_CTRL_ENABLE| pll | CCM_MMC_CTRL_N(n) | | |
151 | CCM_MMC_CTRL_M(div) | val, priv->mclkreg); | |
fc3a8325 HG |
152 | |
153 | debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n", | |
3f5af12a | 154 | priv->mmc_no, hz, pll_hz, 1u << n, div, pll_hz / (1u << n) / div); |
fc3a8325 HG |
155 | |
156 | return 0; | |
157 | } | |
158 | ||
034e226b | 159 | static int mmc_update_clk(struct sunxi_mmc_priv *priv) |
e24ea55c | 160 | { |
e24ea55c IC |
161 | unsigned int cmd; |
162 | unsigned timeout_msecs = 2000; | |
5ff8e548 | 163 | unsigned long start = get_timer(0); |
e24ea55c IC |
164 | |
165 | cmd = SUNXI_MMC_CMD_START | | |
166 | SUNXI_MMC_CMD_UPCLK_ONLY | | |
167 | SUNXI_MMC_CMD_WAIT_PRE_OVER; | |
5ff8e548 | 168 | |
3f5af12a SG |
169 | writel(cmd, &priv->reg->cmd); |
170 | while (readl(&priv->reg->cmd) & SUNXI_MMC_CMD_START) { | |
5ff8e548 | 171 | if (get_timer(start) > timeout_msecs) |
e24ea55c | 172 | return -1; |
e24ea55c IC |
173 | } |
174 | ||
175 | /* clock update sets various irq status bits, clear these */ | |
3f5af12a | 176 | writel(readl(&priv->reg->rint), &priv->reg->rint); |
e24ea55c IC |
177 | |
178 | return 0; | |
179 | } | |
180 | ||
034e226b | 181 | static int mmc_config_clock(struct sunxi_mmc_priv *priv, struct mmc *mmc) |
e24ea55c | 182 | { |
3f5af12a | 183 | unsigned rval = readl(&priv->reg->clkcr); |
e24ea55c IC |
184 | |
185 | /* Disable Clock */ | |
186 | rval &= ~SUNXI_MMC_CLK_ENABLE; | |
3f5af12a | 187 | writel(rval, &priv->reg->clkcr); |
034e226b | 188 | if (mmc_update_clk(priv)) |
e24ea55c IC |
189 | return -1; |
190 | ||
fc3a8325 | 191 | /* Set mod_clk to new rate */ |
3f5af12a | 192 | if (mmc_set_mod_clk(priv, mmc->clock)) |
fc3a8325 HG |
193 | return -1; |
194 | ||
195 | /* Clear internal divider */ | |
e24ea55c | 196 | rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK; |
3f5af12a | 197 | writel(rval, &priv->reg->clkcr); |
fc3a8325 | 198 | |
4a9e89a3 | 199 | #if defined(CONFIG_SUNXI_GEN_SUN6I) || defined(CONFIG_SUN50I_GEN_H6) || defined(CONFIG_SUNXI_GEN_NCAT2) |
20940ef2 VK |
200 | /* A64 supports calibration of delays on MMC controller and we |
201 | * have to set delay of zero before starting calibration. | |
202 | * Allwinner BSP driver sets a delay only in the case of | |
203 | * using HS400 which is not supported by mainline U-Boot or | |
204 | * Linux at the moment | |
205 | */ | |
b5dd39c9 AP |
206 | if (sunxi_mmc_can_calibrate()) |
207 | writel(SUNXI_MMC_CAL_DL_SW_EN, &priv->reg->samp_dl); | |
20940ef2 VK |
208 | #endif |
209 | ||
e24ea55c IC |
210 | /* Re-enable Clock */ |
211 | rval |= SUNXI_MMC_CLK_ENABLE; | |
3f5af12a | 212 | writel(rval, &priv->reg->clkcr); |
034e226b | 213 | if (mmc_update_clk(priv)) |
e24ea55c IC |
214 | return -1; |
215 | ||
216 | return 0; | |
217 | } | |
218 | ||
034e226b SG |
219 | static int sunxi_mmc_set_ios_common(struct sunxi_mmc_priv *priv, |
220 | struct mmc *mmc) | |
e24ea55c | 221 | { |
fc3a8325 HG |
222 | debug("set ios: bus_width: %x, clock: %d\n", |
223 | mmc->bus_width, mmc->clock); | |
e24ea55c IC |
224 | |
225 | /* Change clock first */ | |
034e226b | 226 | if (mmc->clock && mmc_config_clock(priv, mmc) != 0) { |
3f5af12a | 227 | priv->fatal_err = 1; |
07b0b9c0 | 228 | return -EINVAL; |
e24ea55c IC |
229 | } |
230 | ||
231 | /* Change bus width */ | |
232 | if (mmc->bus_width == 8) | |
3f5af12a | 233 | writel(0x2, &priv->reg->width); |
e24ea55c | 234 | else if (mmc->bus_width == 4) |
3f5af12a | 235 | writel(0x1, &priv->reg->width); |
e24ea55c | 236 | else |
3f5af12a | 237 | writel(0x0, &priv->reg->width); |
07b0b9c0 JC |
238 | |
239 | return 0; | |
e24ea55c IC |
240 | } |
241 | ||
034e226b SG |
242 | static int mmc_trans_data_by_cpu(struct sunxi_mmc_priv *priv, struct mmc *mmc, |
243 | struct mmc_data *data) | |
e24ea55c | 244 | { |
e24ea55c IC |
245 | const int reading = !!(data->flags & MMC_DATA_READ); |
246 | const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY : | |
247 | SUNXI_MMC_STATUS_FIFO_FULL; | |
248 | unsigned i; | |
e24ea55c | 249 | unsigned *buff = (unsigned int *)(reading ? data->dest : data->src); |
9faae545 AP |
250 | unsigned word_cnt = (data->blocksize * data->blocks) >> 2; |
251 | unsigned timeout_msecs = word_cnt >> 6; | |
252 | uint32_t status; | |
5ff8e548 PT |
253 | unsigned long start; |
254 | ||
255 | if (timeout_msecs < 2000) | |
256 | timeout_msecs = 2000; | |
e24ea55c | 257 | |
b6ae6765 | 258 | /* Always read / write data through the CPU */ |
3f5af12a | 259 | setbits_le32(&priv->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB); |
b6ae6765 | 260 | |
5ff8e548 PT |
261 | start = get_timer(0); |
262 | ||
9faae545 AP |
263 | for (i = 0; i < word_cnt;) { |
264 | unsigned int in_fifo; | |
265 | ||
266 | while ((status = readl(&priv->reg->status)) & status_bit) { | |
5ff8e548 | 267 | if (get_timer(start) > timeout_msecs) |
e24ea55c | 268 | return -1; |
e24ea55c IC |
269 | } |
270 | ||
9faae545 AP |
271 | /* |
272 | * For writing we do not easily know the FIFO size, so have | |
273 | * to check the FIFO status after every word written. | |
274 | * TODO: For optimisation we could work out a minimum FIFO | |
275 | * size across all SoCs, and use that together with the current | |
276 | * fill level to write chunks of words. | |
277 | */ | |
278 | if (!reading) { | |
279 | writel(buff[i++], &priv->reg->fifo); | |
280 | continue; | |
281 | } | |
282 | ||
283 | /* | |
284 | * The status register holds the current FIFO level, so we | |
285 | * can be sure to collect as many words from the FIFO | |
286 | * register without checking the status register after every | |
287 | * read. That saves half of the costly MMIO reads, effectively | |
288 | * doubling the read performance. | |
0b508ca8 AP |
289 | * Some SoCs (A20) report a level of 0 if the FIFO is |
290 | * completely full (value masked out?). Use a safe minimal | |
291 | * FIFO size in this case. | |
9faae545 | 292 | */ |
0b508ca8 AP |
293 | in_fifo = SUNXI_MMC_STATUS_FIFO_LEVEL(status); |
294 | if (in_fifo == 0 && (status & SUNXI_MMC_STATUS_FIFO_FULL)) | |
295 | in_fifo = 32; | |
296 | for (; in_fifo > 0; in_fifo--) | |
9faae545 AP |
297 | buff[i++] = readl_relaxed(&priv->reg->fifo); |
298 | dmb(); | |
e24ea55c IC |
299 | } |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
034e226b SG |
304 | static int mmc_rint_wait(struct sunxi_mmc_priv *priv, struct mmc *mmc, |
305 | uint timeout_msecs, uint done_bit, const char *what) | |
e24ea55c | 306 | { |
e24ea55c | 307 | unsigned int status; |
5ff8e548 | 308 | unsigned long start = get_timer(0); |
e24ea55c IC |
309 | |
310 | do { | |
3f5af12a | 311 | status = readl(&priv->reg->rint); |
5ff8e548 | 312 | if ((get_timer(start) > timeout_msecs) || |
e24ea55c IC |
313 | (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) { |
314 | debug("%s timeout %x\n", what, | |
315 | status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT); | |
915ffa52 | 316 | return -ETIMEDOUT; |
e24ea55c | 317 | } |
e24ea55c IC |
318 | } while (!(status & done_bit)); |
319 | ||
320 | return 0; | |
321 | } | |
322 | ||
034e226b SG |
323 | static int sunxi_mmc_send_cmd_common(struct sunxi_mmc_priv *priv, |
324 | struct mmc *mmc, struct mmc_cmd *cmd, | |
325 | struct mmc_data *data) | |
e24ea55c | 326 | { |
e24ea55c IC |
327 | unsigned int cmdval = SUNXI_MMC_CMD_START; |
328 | unsigned int timeout_msecs; | |
329 | int error = 0; | |
330 | unsigned int status = 0; | |
e24ea55c IC |
331 | unsigned int bytecnt = 0; |
332 | ||
3f5af12a | 333 | if (priv->fatal_err) |
e24ea55c IC |
334 | return -1; |
335 | if (cmd->resp_type & MMC_RSP_BUSY) | |
336 | debug("mmc cmd %d check rsp busy\n", cmd->cmdidx); | |
337 | if (cmd->cmdidx == 12) | |
338 | return 0; | |
339 | ||
340 | if (!cmd->cmdidx) | |
341 | cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ; | |
342 | if (cmd->resp_type & MMC_RSP_PRESENT) | |
343 | cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE; | |
344 | if (cmd->resp_type & MMC_RSP_136) | |
345 | cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE; | |
346 | if (cmd->resp_type & MMC_RSP_CRC) | |
347 | cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC; | |
348 | ||
349 | if (data) { | |
0ea5a04f | 350 | if ((u32)(long)data->dest & 0x3) { |
e24ea55c IC |
351 | error = -1; |
352 | goto out; | |
353 | } | |
354 | ||
355 | cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER; | |
356 | if (data->flags & MMC_DATA_WRITE) | |
357 | cmdval |= SUNXI_MMC_CMD_WRITE; | |
358 | if (data->blocks > 1) | |
359 | cmdval |= SUNXI_MMC_CMD_AUTO_STOP; | |
3f5af12a SG |
360 | writel(data->blocksize, &priv->reg->blksz); |
361 | writel(data->blocks * data->blocksize, &priv->reg->bytecnt); | |
e24ea55c IC |
362 | } |
363 | ||
3f5af12a | 364 | debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", priv->mmc_no, |
e24ea55c | 365 | cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg); |
3f5af12a | 366 | writel(cmd->cmdarg, &priv->reg->arg); |
e24ea55c IC |
367 | |
368 | if (!data) | |
3f5af12a | 369 | writel(cmdval | cmd->cmdidx, &priv->reg->cmd); |
e24ea55c IC |
370 | |
371 | /* | |
372 | * transfer data and check status | |
373 | * STATREG[2] : FIFO empty | |
374 | * STATREG[3] : FIFO full | |
375 | */ | |
376 | if (data) { | |
377 | int ret = 0; | |
378 | ||
379 | bytecnt = data->blocksize * data->blocks; | |
380 | debug("trans data %d bytes\n", bytecnt); | |
3f5af12a | 381 | writel(cmdval | cmd->cmdidx, &priv->reg->cmd); |
034e226b | 382 | ret = mmc_trans_data_by_cpu(priv, mmc, data); |
e24ea55c | 383 | if (ret) { |
3f5af12a | 384 | error = readl(&priv->reg->rint) & |
e24ea55c | 385 | SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT; |
915ffa52 | 386 | error = -ETIMEDOUT; |
e24ea55c IC |
387 | goto out; |
388 | } | |
389 | } | |
390 | ||
034e226b SG |
391 | error = mmc_rint_wait(priv, mmc, 1000, SUNXI_MMC_RINT_COMMAND_DONE, |
392 | "cmd"); | |
e24ea55c IC |
393 | if (error) |
394 | goto out; | |
395 | ||
396 | if (data) { | |
b6ae6765 | 397 | timeout_msecs = 120; |
e24ea55c | 398 | debug("cacl timeout %x msec\n", timeout_msecs); |
034e226b | 399 | error = mmc_rint_wait(priv, mmc, timeout_msecs, |
e24ea55c IC |
400 | data->blocks > 1 ? |
401 | SUNXI_MMC_RINT_AUTO_COMMAND_DONE : | |
402 | SUNXI_MMC_RINT_DATA_OVER, | |
403 | "data"); | |
404 | if (error) | |
405 | goto out; | |
406 | } | |
407 | ||
408 | if (cmd->resp_type & MMC_RSP_BUSY) { | |
5ff8e548 | 409 | unsigned long start = get_timer(0); |
e24ea55c | 410 | timeout_msecs = 2000; |
5ff8e548 | 411 | |
e24ea55c | 412 | do { |
3f5af12a | 413 | status = readl(&priv->reg->status); |
5ff8e548 | 414 | if (get_timer(start) > timeout_msecs) { |
e24ea55c | 415 | debug("busy timeout\n"); |
915ffa52 | 416 | error = -ETIMEDOUT; |
e24ea55c IC |
417 | goto out; |
418 | } | |
e24ea55c IC |
419 | } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY); |
420 | } | |
421 | ||
422 | if (cmd->resp_type & MMC_RSP_136) { | |
3f5af12a SG |
423 | cmd->response[0] = readl(&priv->reg->resp3); |
424 | cmd->response[1] = readl(&priv->reg->resp2); | |
425 | cmd->response[2] = readl(&priv->reg->resp1); | |
426 | cmd->response[3] = readl(&priv->reg->resp0); | |
e24ea55c IC |
427 | debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n", |
428 | cmd->response[3], cmd->response[2], | |
429 | cmd->response[1], cmd->response[0]); | |
430 | } else { | |
3f5af12a | 431 | cmd->response[0] = readl(&priv->reg->resp0); |
e24ea55c IC |
432 | debug("mmc resp 0x%08x\n", cmd->response[0]); |
433 | } | |
434 | out: | |
e24ea55c | 435 | if (error < 0) { |
3f5af12a | 436 | writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl); |
034e226b | 437 | mmc_update_clk(priv); |
e24ea55c | 438 | } |
3f5af12a SG |
439 | writel(0xffffffff, &priv->reg->rint); |
440 | writel(readl(&priv->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET, | |
441 | &priv->reg->gctrl); | |
e24ea55c IC |
442 | |
443 | return error; | |
444 | } | |
445 | ||
ba16b531 AP |
446 | /* non-DM code here is used by the (ARM) SPL only */ |
447 | ||
dd27918c | 448 | #if !CONFIG_IS_ENABLED(DM_MMC) |
ba16b531 AP |
449 | /* support 4 mmc hosts */ |
450 | struct sunxi_mmc_priv mmc_host[4]; | |
451 | ||
452 | static int mmc_resource_init(int sdc_no) | |
453 | { | |
454 | struct sunxi_mmc_priv *priv = &mmc_host[sdc_no]; | |
455 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; | |
456 | ||
457 | debug("init mmc %d resource\n", sdc_no); | |
458 | ||
459 | switch (sdc_no) { | |
460 | case 0: | |
461 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE; | |
462 | priv->mclkreg = &ccm->sd0_clk_cfg; | |
463 | break; | |
464 | case 1: | |
465 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE; | |
466 | priv->mclkreg = &ccm->sd1_clk_cfg; | |
467 | break; | |
468 | #ifdef SUNXI_MMC2_BASE | |
469 | case 2: | |
470 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE; | |
471 | priv->mclkreg = &ccm->sd2_clk_cfg; | |
472 | break; | |
473 | #endif | |
474 | #ifdef SUNXI_MMC3_BASE | |
475 | case 3: | |
476 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE; | |
477 | priv->mclkreg = &ccm->sd3_clk_cfg; | |
478 | break; | |
479 | #endif | |
480 | default: | |
481 | printf("Wrong mmc number %d\n", sdc_no); | |
482 | return -1; | |
483 | } | |
484 | priv->mmc_no = sdc_no; | |
485 | ||
486 | return 0; | |
487 | } | |
488 | ||
489 | static int sunxi_mmc_core_init(struct mmc *mmc) | |
490 | { | |
491 | struct sunxi_mmc_priv *priv = mmc->priv; | |
492 | ||
493 | /* Reset controller */ | |
494 | writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl); | |
495 | udelay(1000); | |
496 | ||
497 | return 0; | |
498 | } | |
499 | ||
034e226b SG |
500 | static int sunxi_mmc_set_ios_legacy(struct mmc *mmc) |
501 | { | |
502 | struct sunxi_mmc_priv *priv = mmc->priv; | |
503 | ||
504 | return sunxi_mmc_set_ios_common(priv, mmc); | |
505 | } | |
506 | ||
507 | static int sunxi_mmc_send_cmd_legacy(struct mmc *mmc, struct mmc_cmd *cmd, | |
508 | struct mmc_data *data) | |
509 | { | |
510 | struct sunxi_mmc_priv *priv = mmc->priv; | |
511 | ||
512 | return sunxi_mmc_send_cmd_common(priv, mmc, cmd, data); | |
513 | } | |
514 | ||
5db81f1c | 515 | /* .getcd is not needed by the SPL */ |
e24ea55c | 516 | static const struct mmc_ops sunxi_mmc_ops = { |
034e226b SG |
517 | .send_cmd = sunxi_mmc_send_cmd_legacy, |
518 | .set_ios = sunxi_mmc_set_ios_legacy, | |
5abdb156 | 519 | .init = sunxi_mmc_core_init, |
e24ea55c IC |
520 | }; |
521 | ||
e79c7c88 | 522 | struct mmc *sunxi_mmc_init(int sdc_no) |
e24ea55c | 523 | { |
ec73d960 | 524 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
034e226b SG |
525 | struct sunxi_mmc_priv *priv = &mmc_host[sdc_no]; |
526 | struct mmc_config *cfg = &priv->cfg; | |
ec73d960 | 527 | int ret; |
e24ea55c | 528 | |
034e226b | 529 | memset(priv, '\0', sizeof(struct sunxi_mmc_priv)); |
e24ea55c IC |
530 | |
531 | cfg->name = "SUNXI SD/MMC"; | |
532 | cfg->ops = &sunxi_mmc_ops; | |
533 | ||
534 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; | |
535 | cfg->host_caps = MMC_MODE_4BIT; | |
f4826fb1 AP |
536 | |
537 | if ((IS_ENABLED(CONFIG_MACH_SUN50I) || IS_ENABLED(CONFIG_MACH_SUN8I) || | |
538 | IS_ENABLED(CONFIG_SUN50I_GEN_H6)) && (sdc_no == 2)) | |
d96ebc46 | 539 | cfg->host_caps = MMC_MODE_8BIT; |
f4826fb1 | 540 | |
5a20397b | 541 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
e24ea55c IC |
542 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
543 | ||
544 | cfg->f_min = 400000; | |
545 | cfg->f_max = 52000000; | |
546 | ||
967325fe HG |
547 | if (mmc_resource_init(sdc_no) != 0) |
548 | return NULL; | |
549 | ||
ec73d960 SG |
550 | /* config ahb clock */ |
551 | debug("init mmc %d clock and io\n", sdc_no); | |
4a9e89a3 | 552 | #if !defined(CONFIG_SUN50I_GEN_H6) && !defined(CONFIG_SUNXI_GEN_NCAT2) |
ec73d960 SG |
553 | setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no)); |
554 | ||
555 | #ifdef CONFIG_SUNXI_GEN_SUN6I | |
556 | /* unassert reset */ | |
557 | setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no)); | |
558 | #endif | |
559 | #if defined(CONFIG_MACH_SUN9I) | |
560 | /* sun9i has a mmc-common module, also set the gate and reset there */ | |
561 | writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET, | |
562 | SUNXI_MMC_COMMON_BASE + 4 * sdc_no); | |
42956f1b | 563 | #endif |
aaebb900 | 564 | #else /* CONFIG_SUN50I_GEN_H6 */ |
42956f1b IZ |
565 | setbits_le32(&ccm->sd_gate_reset, 1 << sdc_no); |
566 | /* unassert reset */ | |
567 | setbits_le32(&ccm->sd_gate_reset, 1 << (RESET_SHIFT + sdc_no)); | |
ec73d960 SG |
568 | #endif |
569 | ret = mmc_set_mod_clk(priv, 24000000); | |
570 | if (ret) | |
571 | return NULL; | |
e24ea55c | 572 | |
ead3697d | 573 | return mmc_create(cfg, priv); |
e24ea55c | 574 | } |
ba16b531 AP |
575 | |
576 | #else /* CONFIG_DM_MMC code below, as used by U-Boot proper */ | |
dd27918c SG |
577 | |
578 | static int sunxi_mmc_set_ios(struct udevice *dev) | |
579 | { | |
c69cda25 | 580 | struct sunxi_mmc_plat *plat = dev_get_plat(dev); |
dd27918c SG |
581 | struct sunxi_mmc_priv *priv = dev_get_priv(dev); |
582 | ||
583 | return sunxi_mmc_set_ios_common(priv, &plat->mmc); | |
584 | } | |
585 | ||
586 | static int sunxi_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, | |
587 | struct mmc_data *data) | |
588 | { | |
c69cda25 | 589 | struct sunxi_mmc_plat *plat = dev_get_plat(dev); |
dd27918c SG |
590 | struct sunxi_mmc_priv *priv = dev_get_priv(dev); |
591 | ||
592 | return sunxi_mmc_send_cmd_common(priv, &plat->mmc, cmd, data); | |
593 | } | |
594 | ||
595 | static int sunxi_mmc_getcd(struct udevice *dev) | |
596 | { | |
ac62dadb | 597 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
dd27918c SG |
598 | struct sunxi_mmc_priv *priv = dev_get_priv(dev); |
599 | ||
ac62dadb AP |
600 | /* If polling, assume that the card is always present. */ |
601 | if ((mmc->cfg->host_caps & MMC_CAP_NONREMOVABLE) || | |
602 | (mmc->cfg->host_caps & MMC_CAP_NEEDS_POLL)) | |
603 | return 1; | |
604 | ||
8be4e61d HS |
605 | if (dm_gpio_is_valid(&priv->cd_gpio)) { |
606 | int cd_state = dm_gpio_get_value(&priv->cd_gpio); | |
dd27918c | 607 | |
ac62dadb AP |
608 | if (mmc->cfg->host_caps & MMC_CAP_CD_ACTIVE_HIGH) |
609 | return !cd_state; | |
610 | else | |
611 | return cd_state; | |
8be4e61d | 612 | } |
dd27918c SG |
613 | return 1; |
614 | } | |
615 | ||
616 | static const struct dm_mmc_ops sunxi_mmc_ops = { | |
617 | .send_cmd = sunxi_mmc_send_cmd, | |
618 | .set_ios = sunxi_mmc_set_ios, | |
619 | .get_cd = sunxi_mmc_getcd, | |
620 | }; | |
621 | ||
0237b304 AP |
622 | static unsigned get_mclk_offset(void) |
623 | { | |
624 | if (IS_ENABLED(CONFIG_MACH_SUN9I_A80)) | |
625 | return 0x410; | |
626 | ||
4a9e89a3 | 627 | if (IS_ENABLED(CONFIG_SUN50I_GEN_H6) || IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2)) |
0237b304 AP |
628 | return 0x830; |
629 | ||
630 | return 0x88; | |
631 | }; | |
632 | ||
dd27918c SG |
633 | static int sunxi_mmc_probe(struct udevice *dev) |
634 | { | |
635 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); | |
c69cda25 | 636 | struct sunxi_mmc_plat *plat = dev_get_plat(dev); |
dd27918c | 637 | struct sunxi_mmc_priv *priv = dev_get_priv(dev); |
c57572eb AP |
638 | struct reset_ctl_bulk reset_bulk; |
639 | struct clk gate_clk; | |
dd27918c SG |
640 | struct mmc_config *cfg = &plat->cfg; |
641 | struct ofnode_phandle_args args; | |
c57572eb | 642 | u32 *ccu_reg; |
ac62dadb | 643 | int ret; |
dd27918c SG |
644 | |
645 | cfg->name = dev->name; | |
dd27918c SG |
646 | |
647 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; | |
ac62dadb | 648 | cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS; |
dd27918c SG |
649 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
650 | ||
651 | cfg->f_min = 400000; | |
652 | cfg->f_max = 52000000; | |
653 | ||
ac62dadb AP |
654 | ret = mmc_of_parse(dev, cfg); |
655 | if (ret) | |
656 | return ret; | |
657 | ||
ca496baf | 658 | priv->reg = dev_read_addr_ptr(dev); |
dd27918c SG |
659 | |
660 | /* We don't have a sunxi clock driver so find the clock address here */ | |
661 | ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0, | |
662 | 1, &args); | |
663 | if (ret) | |
664 | return ret; | |
ca496baf | 665 | ccu_reg = (u32 *)(uintptr_t)ofnode_get_addr(args.node); |
dd27918c | 666 | |
e8f37f42 | 667 | priv->mmc_no = ((uintptr_t)priv->reg - SUNXI_MMC0_BASE) / 0x1000; |
0237b304 | 668 | priv->mclkreg = (void *)ccu_reg + get_mclk_offset() + priv->mmc_no * 4; |
c57572eb AP |
669 | |
670 | ret = clk_get_by_name(dev, "ahb", &gate_clk); | |
671 | if (!ret) | |
672 | clk_enable(&gate_clk); | |
673 | ||
674 | ret = reset_get_bulk(dev, &reset_bulk); | |
675 | if (!ret) | |
676 | reset_deassert_bulk(&reset_bulk); | |
dd27918c SG |
677 | |
678 | ret = mmc_set_mod_clk(priv, 24000000); | |
679 | if (ret) | |
680 | return ret; | |
681 | ||
682 | /* This GPIO is optional */ | |
fb6f6701 SH |
683 | gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, |
684 | GPIOD_IS_IN | GPIOD_PULL_UP); | |
dd27918c SG |
685 | |
686 | upriv->mmc = &plat->mmc; | |
687 | ||
688 | /* Reset controller */ | |
689 | writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl); | |
690 | udelay(1000); | |
691 | ||
692 | return 0; | |
693 | } | |
694 | ||
695 | static int sunxi_mmc_bind(struct udevice *dev) | |
696 | { | |
c69cda25 | 697 | struct sunxi_mmc_plat *plat = dev_get_plat(dev); |
dd27918c SG |
698 | |
699 | return mmc_bind(dev, &plat->mmc, &plat->cfg); | |
700 | } | |
701 | ||
702 | static const struct udevice_id sunxi_mmc_ids[] = { | |
0237b304 AP |
703 | { .compatible = "allwinner,sun4i-a10-mmc" }, |
704 | { .compatible = "allwinner,sun5i-a13-mmc" }, | |
705 | { .compatible = "allwinner,sun7i-a20-mmc" }, | |
706 | { .compatible = "allwinner,sun8i-a83t-emmc" }, | |
707 | { .compatible = "allwinner,sun9i-a80-mmc" }, | |
d379bcbf | 708 | { .compatible = "allwinner,sun20i-d1-mmc" }, |
0237b304 AP |
709 | { .compatible = "allwinner,sun50i-a64-mmc" }, |
710 | { .compatible = "allwinner,sun50i-a64-emmc" }, | |
711 | { .compatible = "allwinner,sun50i-h6-mmc" }, | |
712 | { .compatible = "allwinner,sun50i-h6-emmc" }, | |
713 | { .compatible = "allwinner,sun50i-a100-mmc" }, | |
714 | { .compatible = "allwinner,sun50i-a100-emmc" }, | |
e8f37f42 | 715 | { /* sentinel */ } |
dd27918c SG |
716 | }; |
717 | ||
718 | U_BOOT_DRIVER(sunxi_mmc_drv) = { | |
719 | .name = "sunxi_mmc", | |
720 | .id = UCLASS_MMC, | |
721 | .of_match = sunxi_mmc_ids, | |
722 | .bind = sunxi_mmc_bind, | |
723 | .probe = sunxi_mmc_probe, | |
724 | .ops = &sunxi_mmc_ops, | |
caa4daa2 | 725 | .plat_auto = sizeof(struct sunxi_mmc_plat), |
41575d8e | 726 | .priv_auto = sizeof(struct sunxi_mmc_priv), |
dd27918c SG |
727 | }; |
728 | #endif |