blob: 23bc7da917a96f2ce6fec9c64563734abe137bfb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbellb4e9f2f2014-05-05 14:42:31 +01002/*
3 * (C) Copyright 2007-2011
4 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
5 * Aaron <leafy.myeh@allwinnertech.com>
6 *
7 * MMC driver for allwinner sunxi platform.
Andre Przywaraf5032592022-07-13 17:21:44 +01008 *
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.
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010014 */
15
16#include <common.h>
Simon Glass7484ae72017-07-04 13:31:27 -060017#include <dm.h>
Hans de Goedeb1e107a2015-04-22 17:03:17 +020018#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010020#include <malloc.h>
21#include <mmc.h>
Andre Przywara29b533c2019-01-29 15:54:13 +000022#include <clk.h>
23#include <reset.h>
Samuel Holland06feb812021-09-11 16:50:47 -050024#include <asm/gpio.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010025#include <asm/io.h>
26#include <asm/arch/clock.h>
27#include <asm/arch/cpu.h>
28#include <asm/arch/mmc.h>
Simon Glassdbd79542020-05-10 11:40:11 -060029#include <linux/delay.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010030
Andre Przywara3f23aa62021-05-05 09:57:47 +010031#ifndef CCM_MMC_CTRL_MODE_SEL_NEW
32#define CCM_MMC_CTRL_MODE_SEL_NEW 0
33#endif
34
Simon Glass7484ae72017-07-04 13:31:27 -060035struct sunxi_mmc_plat {
36 struct mmc_config cfg;
37 struct mmc mmc;
38};
39
Simon Glass3f19fbf2017-07-04 13:31:23 -060040struct sunxi_mmc_priv {
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010041 unsigned mmc_no;
42 uint32_t *mclkreg;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010043 unsigned fatal_err;
Simon Glass7484ae72017-07-04 13:31:27 -060044 struct gpio_desc cd_gpio; /* Change Detect GPIO */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010045 struct sunxi_mmc *reg;
46 struct mmc_config cfg;
47};
48
Andre Przywara8c93a9c2021-05-05 10:06:24 +010049/*
50 * All A64 and later MMC controllers feature auto-calibration. This would
51 * normally be detected via the compatible string, but we need something
52 * which works in the SPL as well.
53 */
54static bool sunxi_mmc_can_calibrate(void)
55{
56 return IS_ENABLED(CONFIG_MACH_SUN50I) ||
57 IS_ENABLED(CONFIG_MACH_SUN50I_H5) ||
58 IS_ENABLED(CONFIG_SUN50I_GEN_H6) ||
59 IS_ENABLED(CONFIG_MACH_SUN8I_R40);
60}
61
Simon Glass8e659a22017-07-04 13:31:24 -060062static int mmc_set_mod_clk(struct sunxi_mmc_priv *priv, unsigned int hz)
Hans de Goede06bfab02014-12-07 20:55:10 +010063{
64 unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly;
Andre Przywara3f23aa62021-05-05 09:57:47 +010065 bool new_mode = IS_ENABLED(CONFIG_MMC_SUNXI_HAS_NEW_MODE);
Maxime Ripard95e34702017-08-23 12:03:41 +020066 u32 val = 0;
67
Vasily Khoruzhicka4e8dd92018-11-09 20:41:46 -080068 /* A83T support new mode only on eMMC */
69 if (IS_ENABLED(CONFIG_MACH_SUN8I_A83T) && priv->mmc_no != 2)
70 new_mode = false;
Maxime Ripard95e34702017-08-23 12:03:41 +020071
Hans de Goede06bfab02014-12-07 20:55:10 +010072 if (hz <= 24000000) {
73 pll = CCM_MMC_CTRL_OSCM24;
74 pll_hz = 24000000;
75 } else {
Hans de Goedef1865db2015-01-14 19:05:03 +010076#ifdef CONFIG_MACH_SUN9I
77 pll = CCM_MMC_CTRL_PLL_PERIPH0;
78 pll_hz = clock_get_pll4_periph0();
79#else
Andre Przywaradd505d12021-05-05 09:57:47 +010080 /*
81 * SoCs since the A64 (H5, H6, H616) actually use the doubled
82 * rate of PLL6/PERIPH0 as an input clock, but compensate for
83 * that with a fixed post-divider of 2 in the mod clock.
84 * This cancels each other out, so for simplicity we just
85 * pretend it's always PLL6 without a post divider here.
86 */
Hans de Goede06bfab02014-12-07 20:55:10 +010087 pll = CCM_MMC_CTRL_PLL6;
88 pll_hz = clock_get_pll6();
Hans de Goedef1865db2015-01-14 19:05:03 +010089#endif
Hans de Goede06bfab02014-12-07 20:55:10 +010090 }
91
92 div = pll_hz / hz;
93 if (pll_hz % hz)
94 div++;
95
96 n = 0;
97 while (div > 16) {
98 n++;
99 div = (div + 1) / 2;
100 }
101
102 if (n > 3) {
Simon Glass8e659a22017-07-04 13:31:24 -0600103 printf("mmc %u error cannot set clock to %u\n", priv->mmc_no,
104 hz);
Hans de Goede06bfab02014-12-07 20:55:10 +0100105 return -1;
106 }
107
108 /* determine delays */
109 if (hz <= 400000) {
110 oclk_dly = 0;
Hans de Goede5192ba22015-09-23 16:13:10 +0200111 sclk_dly = 0;
Hans de Goede06bfab02014-12-07 20:55:10 +0100112 } else if (hz <= 25000000) {
113 oclk_dly = 0;
114 sclk_dly = 5;
Hans de Goede06bfab02014-12-07 20:55:10 +0100115 } else {
Andre Przywaraf2f3a592020-12-18 22:02:11 +0000116 if (IS_ENABLED(CONFIG_MACH_SUN9I)) {
117 if (hz <= 52000000)
118 oclk_dly = 5;
119 else
120 oclk_dly = 2;
121 } else {
122 if (hz <= 52000000)
123 oclk_dly = 3;
124 else
125 oclk_dly = 1;
126 }
Hans de Goede5192ba22015-09-23 16:13:10 +0200127 sclk_dly = 4;
Maxime Ripard95e34702017-08-23 12:03:41 +0200128 }
129
130 if (new_mode) {
Andre Przywara3f23aa62021-05-05 09:57:47 +0100131 val |= CCM_MMC_CTRL_MODE_SEL_NEW;
Chen-Yu Tsaie76f0062017-08-31 21:57:48 +0800132 setbits_le32(&priv->reg->ntsr, SUNXI_MMC_NTSR_MODE_SEL_NEW);
Andre Przywara8c93a9c2021-05-05 10:06:24 +0100133 }
134
135 if (!sunxi_mmc_can_calibrate()) {
Vasily Khoruzhick57789d62018-11-05 20:24:28 -0800136 /*
137 * Use hardcoded delay values if controller doesn't support
138 * calibration
139 */
Maxime Ripard95e34702017-08-23 12:03:41 +0200140 val = CCM_MMC_CTRL_OCLK_DLY(oclk_dly) |
141 CCM_MMC_CTRL_SCLK_DLY(sclk_dly);
Hans de Goede06bfab02014-12-07 20:55:10 +0100142 }
143
Maxime Ripard95e34702017-08-23 12:03:41 +0200144 writel(CCM_MMC_CTRL_ENABLE| pll | CCM_MMC_CTRL_N(n) |
145 CCM_MMC_CTRL_M(div) | val, priv->mclkreg);
Hans de Goede06bfab02014-12-07 20:55:10 +0100146
147 debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n",
Simon Glass8e659a22017-07-04 13:31:24 -0600148 priv->mmc_no, hz, pll_hz, 1u << n, div, pll_hz / (1u << n) / div);
Hans de Goede06bfab02014-12-07 20:55:10 +0100149
150 return 0;
151}
152
Simon Glass87ff0f72017-07-04 13:31:25 -0600153static int mmc_update_clk(struct sunxi_mmc_priv *priv)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100154{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100155 unsigned int cmd;
156 unsigned timeout_msecs = 2000;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100157 unsigned long start = get_timer(0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100158
159 cmd = SUNXI_MMC_CMD_START |
160 SUNXI_MMC_CMD_UPCLK_ONLY |
161 SUNXI_MMC_CMD_WAIT_PRE_OVER;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100162
Simon Glass8e659a22017-07-04 13:31:24 -0600163 writel(cmd, &priv->reg->cmd);
164 while (readl(&priv->reg->cmd) & SUNXI_MMC_CMD_START) {
Philipp Tomsich1721b002018-03-21 12:18:58 +0100165 if (get_timer(start) > timeout_msecs)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100166 return -1;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100167 }
168
169 /* clock update sets various irq status bits, clear these */
Simon Glass8e659a22017-07-04 13:31:24 -0600170 writel(readl(&priv->reg->rint), &priv->reg->rint);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100171
172 return 0;
173}
174
Simon Glass87ff0f72017-07-04 13:31:25 -0600175static int mmc_config_clock(struct sunxi_mmc_priv *priv, struct mmc *mmc)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100176{
Simon Glass8e659a22017-07-04 13:31:24 -0600177 unsigned rval = readl(&priv->reg->clkcr);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100178
179 /* Disable Clock */
180 rval &= ~SUNXI_MMC_CLK_ENABLE;
Simon Glass8e659a22017-07-04 13:31:24 -0600181 writel(rval, &priv->reg->clkcr);
Simon Glass87ff0f72017-07-04 13:31:25 -0600182 if (mmc_update_clk(priv))
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100183 return -1;
184
Hans de Goede06bfab02014-12-07 20:55:10 +0100185 /* Set mod_clk to new rate */
Simon Glass8e659a22017-07-04 13:31:24 -0600186 if (mmc_set_mod_clk(priv, mmc->clock))
Hans de Goede06bfab02014-12-07 20:55:10 +0100187 return -1;
188
189 /* Clear internal divider */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100190 rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK;
Simon Glass8e659a22017-07-04 13:31:24 -0600191 writel(rval, &priv->reg->clkcr);
Hans de Goede06bfab02014-12-07 20:55:10 +0100192
Andre Przywara8c93a9c2021-05-05 10:06:24 +0100193#if defined(CONFIG_SUNXI_GEN_SUN6I) || defined(CONFIG_SUN50I_GEN_H6)
Vasily Khoruzhick57789d62018-11-05 20:24:28 -0800194 /* A64 supports calibration of delays on MMC controller and we
195 * have to set delay of zero before starting calibration.
196 * Allwinner BSP driver sets a delay only in the case of
197 * using HS400 which is not supported by mainline U-Boot or
198 * Linux at the moment
199 */
Andre Przywara8c93a9c2021-05-05 10:06:24 +0100200 if (sunxi_mmc_can_calibrate())
201 writel(SUNXI_MMC_CAL_DL_SW_EN, &priv->reg->samp_dl);
Vasily Khoruzhick57789d62018-11-05 20:24:28 -0800202#endif
203
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100204 /* Re-enable Clock */
205 rval |= SUNXI_MMC_CLK_ENABLE;
Simon Glass8e659a22017-07-04 13:31:24 -0600206 writel(rval, &priv->reg->clkcr);
Simon Glass87ff0f72017-07-04 13:31:25 -0600207 if (mmc_update_clk(priv))
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100208 return -1;
209
210 return 0;
211}
212
Simon Glass87ff0f72017-07-04 13:31:25 -0600213static int sunxi_mmc_set_ios_common(struct sunxi_mmc_priv *priv,
214 struct mmc *mmc)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100215{
Hans de Goede06bfab02014-12-07 20:55:10 +0100216 debug("set ios: bus_width: %x, clock: %d\n",
217 mmc->bus_width, mmc->clock);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100218
219 /* Change clock first */
Simon Glass87ff0f72017-07-04 13:31:25 -0600220 if (mmc->clock && mmc_config_clock(priv, mmc) != 0) {
Simon Glass8e659a22017-07-04 13:31:24 -0600221 priv->fatal_err = 1;
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900222 return -EINVAL;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100223 }
224
225 /* Change bus width */
226 if (mmc->bus_width == 8)
Simon Glass8e659a22017-07-04 13:31:24 -0600227 writel(0x2, &priv->reg->width);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100228 else if (mmc->bus_width == 4)
Simon Glass8e659a22017-07-04 13:31:24 -0600229 writel(0x1, &priv->reg->width);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100230 else
Simon Glass8e659a22017-07-04 13:31:24 -0600231 writel(0x0, &priv->reg->width);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900232
233 return 0;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100234}
235
Simon Glass87ff0f72017-07-04 13:31:25 -0600236static int mmc_trans_data_by_cpu(struct sunxi_mmc_priv *priv, struct mmc *mmc,
237 struct mmc_data *data)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100238{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100239 const int reading = !!(data->flags & MMC_DATA_READ);
240 const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY :
241 SUNXI_MMC_STATUS_FIFO_FULL;
242 unsigned i;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100243 unsigned *buff = (unsigned int *)(reading ? data->dest : data->src);
Andre Przywara56086a42021-05-05 11:33:40 +0100244 unsigned word_cnt = (data->blocksize * data->blocks) >> 2;
245 unsigned timeout_msecs = word_cnt >> 6;
246 uint32_t status;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100247 unsigned long start;
248
249 if (timeout_msecs < 2000)
250 timeout_msecs = 2000;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100251
Hans de Goede411dc872014-06-09 11:36:55 +0200252 /* Always read / write data through the CPU */
Simon Glass8e659a22017-07-04 13:31:24 -0600253 setbits_le32(&priv->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB);
Hans de Goede411dc872014-06-09 11:36:55 +0200254
Philipp Tomsich1721b002018-03-21 12:18:58 +0100255 start = get_timer(0);
256
Andre Przywara56086a42021-05-05 11:33:40 +0100257 for (i = 0; i < word_cnt;) {
258 unsigned int in_fifo;
259
260 while ((status = readl(&priv->reg->status)) & status_bit) {
Philipp Tomsich1721b002018-03-21 12:18:58 +0100261 if (get_timer(start) > timeout_msecs)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100262 return -1;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100263 }
264
Andre Przywara56086a42021-05-05 11:33:40 +0100265 /*
266 * For writing we do not easily know the FIFO size, so have
267 * to check the FIFO status after every word written.
268 * TODO: For optimisation we could work out a minimum FIFO
269 * size across all SoCs, and use that together with the current
270 * fill level to write chunks of words.
271 */
272 if (!reading) {
273 writel(buff[i++], &priv->reg->fifo);
274 continue;
275 }
276
277 /*
278 * The status register holds the current FIFO level, so we
279 * can be sure to collect as many words from the FIFO
280 * register without checking the status register after every
281 * read. That saves half of the costly MMIO reads, effectively
282 * doubling the read performance.
Andre Przywaraf5020702021-09-03 16:49:16 +0100283 * Some SoCs (A20) report a level of 0 if the FIFO is
284 * completely full (value masked out?). Use a safe minimal
285 * FIFO size in this case.
Andre Przywara56086a42021-05-05 11:33:40 +0100286 */
Andre Przywaraf5020702021-09-03 16:49:16 +0100287 in_fifo = SUNXI_MMC_STATUS_FIFO_LEVEL(status);
288 if (in_fifo == 0 && (status & SUNXI_MMC_STATUS_FIFO_FULL))
289 in_fifo = 32;
290 for (; in_fifo > 0; in_fifo--)
Andre Przywara56086a42021-05-05 11:33:40 +0100291 buff[i++] = readl_relaxed(&priv->reg->fifo);
292 dmb();
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100293 }
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100294
295 return 0;
296}
297
Simon Glass87ff0f72017-07-04 13:31:25 -0600298static int mmc_rint_wait(struct sunxi_mmc_priv *priv, struct mmc *mmc,
299 uint timeout_msecs, uint done_bit, const char *what)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100300{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100301 unsigned int status;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100302 unsigned long start = get_timer(0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100303
304 do {
Simon Glass8e659a22017-07-04 13:31:24 -0600305 status = readl(&priv->reg->rint);
Philipp Tomsich1721b002018-03-21 12:18:58 +0100306 if ((get_timer(start) > timeout_msecs) ||
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100307 (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) {
308 debug("%s timeout %x\n", what,
309 status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900310 return -ETIMEDOUT;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100311 }
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100312 } while (!(status & done_bit));
313
314 return 0;
315}
316
Simon Glass87ff0f72017-07-04 13:31:25 -0600317static int sunxi_mmc_send_cmd_common(struct sunxi_mmc_priv *priv,
318 struct mmc *mmc, struct mmc_cmd *cmd,
319 struct mmc_data *data)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100320{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100321 unsigned int cmdval = SUNXI_MMC_CMD_START;
322 unsigned int timeout_msecs;
323 int error = 0;
324 unsigned int status = 0;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100325 unsigned int bytecnt = 0;
326
Simon Glass8e659a22017-07-04 13:31:24 -0600327 if (priv->fatal_err)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100328 return -1;
329 if (cmd->resp_type & MMC_RSP_BUSY)
330 debug("mmc cmd %d check rsp busy\n", cmd->cmdidx);
331 if (cmd->cmdidx == 12)
332 return 0;
333
334 if (!cmd->cmdidx)
335 cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
336 if (cmd->resp_type & MMC_RSP_PRESENT)
337 cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE;
338 if (cmd->resp_type & MMC_RSP_136)
339 cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE;
340 if (cmd->resp_type & MMC_RSP_CRC)
341 cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC;
342
343 if (data) {
Alexander Grafee1d8252016-03-29 17:29:09 +0200344 if ((u32)(long)data->dest & 0x3) {
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100345 error = -1;
346 goto out;
347 }
348
349 cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER;
350 if (data->flags & MMC_DATA_WRITE)
351 cmdval |= SUNXI_MMC_CMD_WRITE;
352 if (data->blocks > 1)
353 cmdval |= SUNXI_MMC_CMD_AUTO_STOP;
Simon Glass8e659a22017-07-04 13:31:24 -0600354 writel(data->blocksize, &priv->reg->blksz);
355 writel(data->blocks * data->blocksize, &priv->reg->bytecnt);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100356 }
357
Simon Glass8e659a22017-07-04 13:31:24 -0600358 debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", priv->mmc_no,
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100359 cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg);
Simon Glass8e659a22017-07-04 13:31:24 -0600360 writel(cmd->cmdarg, &priv->reg->arg);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100361
362 if (!data)
Simon Glass8e659a22017-07-04 13:31:24 -0600363 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100364
365 /*
366 * transfer data and check status
367 * STATREG[2] : FIFO empty
368 * STATREG[3] : FIFO full
369 */
370 if (data) {
371 int ret = 0;
372
373 bytecnt = data->blocksize * data->blocks;
374 debug("trans data %d bytes\n", bytecnt);
Simon Glass8e659a22017-07-04 13:31:24 -0600375 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
Simon Glass87ff0f72017-07-04 13:31:25 -0600376 ret = mmc_trans_data_by_cpu(priv, mmc, data);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100377 if (ret) {
Simon Glass8e659a22017-07-04 13:31:24 -0600378 error = readl(&priv->reg->rint) &
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100379 SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT;
Jaehoon Chung7825d202016-07-19 16:33:36 +0900380 error = -ETIMEDOUT;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100381 goto out;
382 }
383 }
384
Simon Glass87ff0f72017-07-04 13:31:25 -0600385 error = mmc_rint_wait(priv, mmc, 1000, SUNXI_MMC_RINT_COMMAND_DONE,
386 "cmd");
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100387 if (error)
388 goto out;
389
390 if (data) {
Hans de Goede411dc872014-06-09 11:36:55 +0200391 timeout_msecs = 120;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100392 debug("cacl timeout %x msec\n", timeout_msecs);
Simon Glass87ff0f72017-07-04 13:31:25 -0600393 error = mmc_rint_wait(priv, mmc, timeout_msecs,
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100394 data->blocks > 1 ?
395 SUNXI_MMC_RINT_AUTO_COMMAND_DONE :
396 SUNXI_MMC_RINT_DATA_OVER,
397 "data");
398 if (error)
399 goto out;
400 }
401
402 if (cmd->resp_type & MMC_RSP_BUSY) {
Philipp Tomsich1721b002018-03-21 12:18:58 +0100403 unsigned long start = get_timer(0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100404 timeout_msecs = 2000;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100405
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100406 do {
Simon Glass8e659a22017-07-04 13:31:24 -0600407 status = readl(&priv->reg->status);
Philipp Tomsich1721b002018-03-21 12:18:58 +0100408 if (get_timer(start) > timeout_msecs) {
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100409 debug("busy timeout\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900410 error = -ETIMEDOUT;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100411 goto out;
412 }
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100413 } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY);
414 }
415
416 if (cmd->resp_type & MMC_RSP_136) {
Simon Glass8e659a22017-07-04 13:31:24 -0600417 cmd->response[0] = readl(&priv->reg->resp3);
418 cmd->response[1] = readl(&priv->reg->resp2);
419 cmd->response[2] = readl(&priv->reg->resp1);
420 cmd->response[3] = readl(&priv->reg->resp0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100421 debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
422 cmd->response[3], cmd->response[2],
423 cmd->response[1], cmd->response[0]);
424 } else {
Simon Glass8e659a22017-07-04 13:31:24 -0600425 cmd->response[0] = readl(&priv->reg->resp0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100426 debug("mmc resp 0x%08x\n", cmd->response[0]);
427 }
428out:
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100429 if (error < 0) {
Simon Glass8e659a22017-07-04 13:31:24 -0600430 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
Simon Glass87ff0f72017-07-04 13:31:25 -0600431 mmc_update_clk(priv);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100432 }
Simon Glass8e659a22017-07-04 13:31:24 -0600433 writel(0xffffffff, &priv->reg->rint);
434 writel(readl(&priv->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET,
435 &priv->reg->gctrl);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100436
437 return error;
438}
439
Andre Przywaraf5032592022-07-13 17:21:44 +0100440/* non-DM code here is used by the (ARM) SPL only */
441
Simon Glass7484ae72017-07-04 13:31:27 -0600442#if !CONFIG_IS_ENABLED(DM_MMC)
Andre Przywaraf5032592022-07-13 17:21:44 +0100443/* support 4 mmc hosts */
444struct sunxi_mmc_priv mmc_host[4];
445
446static int mmc_resource_init(int sdc_no)
447{
448 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
449 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
450
451 debug("init mmc %d resource\n", sdc_no);
452
453 switch (sdc_no) {
454 case 0:
455 priv->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE;
456 priv->mclkreg = &ccm->sd0_clk_cfg;
457 break;
458 case 1:
459 priv->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE;
460 priv->mclkreg = &ccm->sd1_clk_cfg;
461 break;
462#ifdef SUNXI_MMC2_BASE
463 case 2:
464 priv->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE;
465 priv->mclkreg = &ccm->sd2_clk_cfg;
466 break;
467#endif
468#ifdef SUNXI_MMC3_BASE
469 case 3:
470 priv->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE;
471 priv->mclkreg = &ccm->sd3_clk_cfg;
472 break;
473#endif
474 default:
475 printf("Wrong mmc number %d\n", sdc_no);
476 return -1;
477 }
478 priv->mmc_no = sdc_no;
479
480 return 0;
481}
482
483static int sunxi_mmc_core_init(struct mmc *mmc)
484{
485 struct sunxi_mmc_priv *priv = mmc->priv;
486
487 /* Reset controller */
488 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
489 udelay(1000);
490
491 return 0;
492}
493
Simon Glass87ff0f72017-07-04 13:31:25 -0600494static int sunxi_mmc_set_ios_legacy(struct mmc *mmc)
495{
496 struct sunxi_mmc_priv *priv = mmc->priv;
497
498 return sunxi_mmc_set_ios_common(priv, mmc);
499}
500
501static int sunxi_mmc_send_cmd_legacy(struct mmc *mmc, struct mmc_cmd *cmd,
502 struct mmc_data *data)
503{
504 struct sunxi_mmc_priv *priv = mmc->priv;
505
506 return sunxi_mmc_send_cmd_common(priv, mmc, cmd, data);
507}
508
Andre Przywaradad8a8d2022-07-13 17:21:43 +0100509/* .getcd is not needed by the SPL */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100510static const struct mmc_ops sunxi_mmc_ops = {
Simon Glass87ff0f72017-07-04 13:31:25 -0600511 .send_cmd = sunxi_mmc_send_cmd_legacy,
512 .set_ios = sunxi_mmc_set_ios_legacy,
Siarhei Siamashka253d77d2015-02-01 00:42:14 +0200513 .init = sunxi_mmc_core_init,
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100514};
515
Hans de Goede63deaa82014-10-02 21:13:54 +0200516struct mmc *sunxi_mmc_init(int sdc_no)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100517{
Simon Glass3a654152017-07-04 13:31:26 -0600518 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Simon Glass87ff0f72017-07-04 13:31:25 -0600519 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
520 struct mmc_config *cfg = &priv->cfg;
Simon Glass3a654152017-07-04 13:31:26 -0600521 int ret;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100522
Simon Glass87ff0f72017-07-04 13:31:25 -0600523 memset(priv, '\0', sizeof(struct sunxi_mmc_priv));
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100524
525 cfg->name = "SUNXI SD/MMC";
526 cfg->ops = &sunxi_mmc_ops;
527
528 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
529 cfg->host_caps = MMC_MODE_4BIT;
Andre Przywaraf2f3a592020-12-18 22:02:11 +0000530
531 if ((IS_ENABLED(CONFIG_MACH_SUN50I) || IS_ENABLED(CONFIG_MACH_SUN8I) ||
532 IS_ENABLED(CONFIG_SUN50I_GEN_H6)) && (sdc_no == 2))
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200533 cfg->host_caps = MMC_MODE_8BIT;
Andre Przywaraf2f3a592020-12-18 22:02:11 +0000534
Rob Herring5fd3edd2015-03-23 17:56:59 -0500535 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100536 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
537
538 cfg->f_min = 400000;
539 cfg->f_max = 52000000;
540
Hans de Goede3d1095f2014-10-31 16:55:02 +0100541 if (mmc_resource_init(sdc_no) != 0)
542 return NULL;
543
Simon Glass3a654152017-07-04 13:31:26 -0600544 /* config ahb clock */
545 debug("init mmc %d clock and io\n", sdc_no);
Jernej Skrabecd6da7ab2021-01-11 21:11:35 +0100546#if !defined(CONFIG_SUN50I_GEN_H6)
Simon Glass3a654152017-07-04 13:31:26 -0600547 setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no));
548
549#ifdef CONFIG_SUNXI_GEN_SUN6I
550 /* unassert reset */
551 setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no));
552#endif
553#if defined(CONFIG_MACH_SUN9I)
554 /* sun9i has a mmc-common module, also set the gate and reset there */
555 writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET,
556 SUNXI_MMC_COMMON_BASE + 4 * sdc_no);
557#endif
Jernej Skrabecd6da7ab2021-01-11 21:11:35 +0100558#else /* CONFIG_SUN50I_GEN_H6 */
Icenowy Zhenga838a152018-07-21 16:20:29 +0800559 setbits_le32(&ccm->sd_gate_reset, 1 << sdc_no);
560 /* unassert reset */
561 setbits_le32(&ccm->sd_gate_reset, 1 << (RESET_SHIFT + sdc_no));
562#endif
Simon Glass3a654152017-07-04 13:31:26 -0600563 ret = mmc_set_mod_clk(priv, 24000000);
564 if (ret)
565 return NULL;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100566
Maxime Ripard0cc228e2017-08-23 13:41:33 +0200567 return mmc_create(cfg, priv);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100568}
Andre Przywaraf5032592022-07-13 17:21:44 +0100569
570#else /* CONFIG_DM_MMC code below, as used by U-Boot proper */
Simon Glass7484ae72017-07-04 13:31:27 -0600571
572static int sunxi_mmc_set_ios(struct udevice *dev)
573{
Simon Glassfa20e932020-12-03 16:55:20 -0700574 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600575 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
576
577 return sunxi_mmc_set_ios_common(priv, &plat->mmc);
578}
579
580static int sunxi_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
581 struct mmc_data *data)
582{
Simon Glassfa20e932020-12-03 16:55:20 -0700583 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600584 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
585
586 return sunxi_mmc_send_cmd_common(priv, &plat->mmc, cmd, data);
587}
588
589static int sunxi_mmc_getcd(struct udevice *dev)
590{
Andre Przywarad8a29602021-04-21 09:33:04 +0100591 struct mmc *mmc = mmc_get_mmc_dev(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600592 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
593
Andre Przywarad8a29602021-04-21 09:33:04 +0100594 /* If polling, assume that the card is always present. */
595 if ((mmc->cfg->host_caps & MMC_CAP_NONREMOVABLE) ||
596 (mmc->cfg->host_caps & MMC_CAP_NEEDS_POLL))
597 return 1;
598
Heinrich Schuchardt8dc0a992018-02-01 23:39:19 +0100599 if (dm_gpio_is_valid(&priv->cd_gpio)) {
600 int cd_state = dm_gpio_get_value(&priv->cd_gpio);
Simon Glass7484ae72017-07-04 13:31:27 -0600601
Andre Przywarad8a29602021-04-21 09:33:04 +0100602 if (mmc->cfg->host_caps & MMC_CAP_CD_ACTIVE_HIGH)
603 return !cd_state;
604 else
605 return cd_state;
Heinrich Schuchardt8dc0a992018-02-01 23:39:19 +0100606 }
Simon Glass7484ae72017-07-04 13:31:27 -0600607 return 1;
608}
609
610static const struct dm_mmc_ops sunxi_mmc_ops = {
611 .send_cmd = sunxi_mmc_send_cmd,
612 .set_ios = sunxi_mmc_set_ios,
613 .get_cd = sunxi_mmc_getcd,
614};
615
Andre Przywara6b12ad82021-01-11 21:11:44 +0100616static unsigned get_mclk_offset(void)
617{
618 if (IS_ENABLED(CONFIG_MACH_SUN9I_A80))
619 return 0x410;
620
621 if (IS_ENABLED(CONFIG_SUN50I_GEN_H6))
622 return 0x830;
623
624 return 0x88;
625};
626
Simon Glass7484ae72017-07-04 13:31:27 -0600627static int sunxi_mmc_probe(struct udevice *dev)
628{
629 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700630 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600631 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
Andre Przywara29b533c2019-01-29 15:54:13 +0000632 struct reset_ctl_bulk reset_bulk;
633 struct clk gate_clk;
Simon Glass7484ae72017-07-04 13:31:27 -0600634 struct mmc_config *cfg = &plat->cfg;
635 struct ofnode_phandle_args args;
Andre Przywara29b533c2019-01-29 15:54:13 +0000636 u32 *ccu_reg;
Andre Przywarad8a29602021-04-21 09:33:04 +0100637 int ret;
Simon Glass7484ae72017-07-04 13:31:27 -0600638
639 cfg->name = dev->name;
Simon Glass7484ae72017-07-04 13:31:27 -0600640
641 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Andre Przywarad8a29602021-04-21 09:33:04 +0100642 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
Simon Glass7484ae72017-07-04 13:31:27 -0600643 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
644
645 cfg->f_min = 400000;
646 cfg->f_max = 52000000;
647
Andre Przywarad8a29602021-04-21 09:33:04 +0100648 ret = mmc_of_parse(dev, cfg);
649 if (ret)
650 return ret;
651
Andre Przywara70bbb412021-04-29 09:31:58 +0100652 priv->reg = dev_read_addr_ptr(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600653
654 /* We don't have a sunxi clock driver so find the clock address here */
655 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
656 1, &args);
657 if (ret)
658 return ret;
Andre Przywara70bbb412021-04-29 09:31:58 +0100659 ccu_reg = (u32 *)(uintptr_t)ofnode_get_addr(args.node);
Simon Glass7484ae72017-07-04 13:31:27 -0600660
Jagan Teki2002b752019-01-09 16:58:39 +0530661 priv->mmc_no = ((uintptr_t)priv->reg - SUNXI_MMC0_BASE) / 0x1000;
Andre Przywara6b12ad82021-01-11 21:11:44 +0100662 priv->mclkreg = (void *)ccu_reg + get_mclk_offset() + priv->mmc_no * 4;
Andre Przywara29b533c2019-01-29 15:54:13 +0000663
664 ret = clk_get_by_name(dev, "ahb", &gate_clk);
665 if (!ret)
666 clk_enable(&gate_clk);
667
668 ret = reset_get_bulk(dev, &reset_bulk);
669 if (!ret)
670 reset_deassert_bulk(&reset_bulk);
Simon Glass7484ae72017-07-04 13:31:27 -0600671
672 ret = mmc_set_mod_clk(priv, 24000000);
673 if (ret)
674 return ret;
675
676 /* This GPIO is optional */
Samuel Hollandb6b35572021-10-20 23:52:57 -0500677 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
678 GPIOD_IS_IN | GPIOD_PULL_UP);
Simon Glass7484ae72017-07-04 13:31:27 -0600679
680 upriv->mmc = &plat->mmc;
681
682 /* Reset controller */
683 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
684 udelay(1000);
685
686 return 0;
687}
688
689static int sunxi_mmc_bind(struct udevice *dev)
690{
Simon Glassfa20e932020-12-03 16:55:20 -0700691 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600692
693 return mmc_bind(dev, &plat->mmc, &plat->cfg);
694}
695
696static const struct udevice_id sunxi_mmc_ids[] = {
Andre Przywara6b12ad82021-01-11 21:11:44 +0100697 { .compatible = "allwinner,sun4i-a10-mmc" },
698 { .compatible = "allwinner,sun5i-a13-mmc" },
699 { .compatible = "allwinner,sun7i-a20-mmc" },
700 { .compatible = "allwinner,sun8i-a83t-emmc" },
701 { .compatible = "allwinner,sun9i-a80-mmc" },
702 { .compatible = "allwinner,sun50i-a64-mmc" },
703 { .compatible = "allwinner,sun50i-a64-emmc" },
704 { .compatible = "allwinner,sun50i-h6-mmc" },
705 { .compatible = "allwinner,sun50i-h6-emmc" },
706 { .compatible = "allwinner,sun50i-a100-mmc" },
707 { .compatible = "allwinner,sun50i-a100-emmc" },
Jagan Teki2002b752019-01-09 16:58:39 +0530708 { /* sentinel */ }
Simon Glass7484ae72017-07-04 13:31:27 -0600709};
710
711U_BOOT_DRIVER(sunxi_mmc_drv) = {
712 .name = "sunxi_mmc",
713 .id = UCLASS_MMC,
714 .of_match = sunxi_mmc_ids,
715 .bind = sunxi_mmc_bind,
716 .probe = sunxi_mmc_probe,
717 .ops = &sunxi_mmc_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700718 .plat_auto = sizeof(struct sunxi_mmc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700719 .priv_auto = sizeof(struct sunxi_mmc_priv),
Simon Glass7484ae72017-07-04 13:31:27 -0600720};
721#endif