blob: 951e6acd34d496773bcd36d913f97c340f131c6c [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
Simon Glass7484ae72017-07-04 13:31:27 -060016#include <dm.h>
Hans de Goedeb1e107a2015-04-22 17:03:17 +020017#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010019#include <malloc.h>
20#include <mmc.h>
Andre Przywara29b533c2019-01-29 15:54:13 +000021#include <clk.h>
22#include <reset.h>
Samuel Holland06feb812021-09-11 16:50:47 -050023#include <asm/gpio.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010024#include <asm/io.h>
25#include <asm/arch/clock.h>
26#include <asm/arch/cpu.h>
Samuel Holland1e7c7972023-10-31 00:22:35 -050027#if !CONFIG_IS_ENABLED(DM_MMC)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010028#include <asm/arch/mmc.h>
Samuel Holland1e7c7972023-10-31 00:22:35 -050029#endif
Simon Glassdbd79542020-05-10 11:40:11 -060030#include <linux/delay.h>
Andre Przywaraf944a612022-09-06 10:36:38 +010031#include <sunxi_gpio.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010032
Samuel Holland1e7c7972023-10-31 00:22:35 -050033#include "sunxi_mmc.h"
34
Andre Przywara3f23aa62021-05-05 09:57:47 +010035#ifndef CCM_MMC_CTRL_MODE_SEL_NEW
36#define CCM_MMC_CTRL_MODE_SEL_NEW 0
37#endif
38
Simon Glass7484ae72017-07-04 13:31:27 -060039struct sunxi_mmc_plat {
40 struct mmc_config cfg;
41 struct mmc mmc;
42};
43
Simon Glass3f19fbf2017-07-04 13:31:23 -060044struct sunxi_mmc_priv {
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010045 unsigned mmc_no;
46 uint32_t *mclkreg;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010047 unsigned fatal_err;
Simon Glass7484ae72017-07-04 13:31:27 -060048 struct gpio_desc cd_gpio; /* Change Detect GPIO */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010049 struct sunxi_mmc *reg;
50 struct mmc_config cfg;
51};
52
Andre Przywara8c93a9c2021-05-05 10:06:24 +010053/*
54 * All A64 and later MMC controllers feature auto-calibration. This would
55 * normally be detected via the compatible string, but we need something
56 * which works in the SPL as well.
57 */
58static bool sunxi_mmc_can_calibrate(void)
59{
60 return IS_ENABLED(CONFIG_MACH_SUN50I) ||
61 IS_ENABLED(CONFIG_MACH_SUN50I_H5) ||
62 IS_ENABLED(CONFIG_SUN50I_GEN_H6) ||
Andre Przywara068962b2022-10-05 17:54:19 +010063 IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2) ||
Andre Przywara8c93a9c2021-05-05 10:06:24 +010064 IS_ENABLED(CONFIG_MACH_SUN8I_R40);
65}
66
Simon Glass8e659a22017-07-04 13:31:24 -060067static int mmc_set_mod_clk(struct sunxi_mmc_priv *priv, unsigned int hz)
Hans de Goede06bfab02014-12-07 20:55:10 +010068{
69 unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly;
Andre Przywara3f23aa62021-05-05 09:57:47 +010070 bool new_mode = IS_ENABLED(CONFIG_MMC_SUNXI_HAS_NEW_MODE);
Maxime Ripard95e34702017-08-23 12:03:41 +020071 u32 val = 0;
72
Vasily Khoruzhicka4e8dd92018-11-09 20:41:46 -080073 /* A83T support new mode only on eMMC */
74 if (IS_ENABLED(CONFIG_MACH_SUN8I_A83T) && priv->mmc_no != 2)
75 new_mode = false;
Maxime Ripard95e34702017-08-23 12:03:41 +020076
Hans de Goede06bfab02014-12-07 20:55:10 +010077 if (hz <= 24000000) {
78 pll = CCM_MMC_CTRL_OSCM24;
79 pll_hz = 24000000;
80 } else {
Hans de Goedef1865db2015-01-14 19:05:03 +010081#ifdef CONFIG_MACH_SUN9I
82 pll = CCM_MMC_CTRL_PLL_PERIPH0;
83 pll_hz = clock_get_pll4_periph0();
84#else
Andre Przywaradd505d12021-05-05 09:57:47 +010085 /*
86 * SoCs since the A64 (H5, H6, H616) actually use the doubled
87 * rate of PLL6/PERIPH0 as an input clock, but compensate for
88 * that with a fixed post-divider of 2 in the mod clock.
89 * This cancels each other out, so for simplicity we just
90 * pretend it's always PLL6 without a post divider here.
91 */
Hans de Goede06bfab02014-12-07 20:55:10 +010092 pll = CCM_MMC_CTRL_PLL6;
93 pll_hz = clock_get_pll6();
Hans de Goedef1865db2015-01-14 19:05:03 +010094#endif
Andre Przywaraf167f3f2025-02-26 11:37:11 +000095 /*
96 * On the D1/R528/T113 mux source 1 refers to PLL_PERIPH0(1x),
97 * like for the older SoCs. However we still have the hidden
98 * divider of 2x, so compensate for that here.
99 */
100 if (IS_ENABLED(CONFIG_MACH_SUN8I_R528))
101 pll_hz /= 2;
Hans de Goede06bfab02014-12-07 20:55:10 +0100102 }
103
104 div = pll_hz / hz;
105 if (pll_hz % hz)
106 div++;
107
108 n = 0;
109 while (div > 16) {
110 n++;
111 div = (div + 1) / 2;
112 }
113
114 if (n > 3) {
Simon Glass8e659a22017-07-04 13:31:24 -0600115 printf("mmc %u error cannot set clock to %u\n", priv->mmc_no,
116 hz);
Hans de Goede06bfab02014-12-07 20:55:10 +0100117 return -1;
118 }
119
120 /* determine delays */
121 if (hz <= 400000) {
122 oclk_dly = 0;
Hans de Goede5192ba22015-09-23 16:13:10 +0200123 sclk_dly = 0;
Hans de Goede06bfab02014-12-07 20:55:10 +0100124 } else if (hz <= 25000000) {
125 oclk_dly = 0;
126 sclk_dly = 5;
Hans de Goede06bfab02014-12-07 20:55:10 +0100127 } else {
Andre Przywaraf2f3a592020-12-18 22:02:11 +0000128 if (IS_ENABLED(CONFIG_MACH_SUN9I)) {
129 if (hz <= 52000000)
130 oclk_dly = 5;
131 else
132 oclk_dly = 2;
133 } else {
134 if (hz <= 52000000)
135 oclk_dly = 3;
136 else
137 oclk_dly = 1;
138 }
Hans de Goede5192ba22015-09-23 16:13:10 +0200139 sclk_dly = 4;
Maxime Ripard95e34702017-08-23 12:03:41 +0200140 }
141
142 if (new_mode) {
Andre Przywara3f23aa62021-05-05 09:57:47 +0100143 val |= CCM_MMC_CTRL_MODE_SEL_NEW;
Chen-Yu Tsaie76f0062017-08-31 21:57:48 +0800144 setbits_le32(&priv->reg->ntsr, SUNXI_MMC_NTSR_MODE_SEL_NEW);
Andre Przywara8c93a9c2021-05-05 10:06:24 +0100145 }
146
147 if (!sunxi_mmc_can_calibrate()) {
Vasily Khoruzhick57789d62018-11-05 20:24:28 -0800148 /*
149 * Use hardcoded delay values if controller doesn't support
150 * calibration
151 */
Maxime Ripard95e34702017-08-23 12:03:41 +0200152 val = CCM_MMC_CTRL_OCLK_DLY(oclk_dly) |
153 CCM_MMC_CTRL_SCLK_DLY(sclk_dly);
Hans de Goede06bfab02014-12-07 20:55:10 +0100154 }
155
Maxime Ripard95e34702017-08-23 12:03:41 +0200156 writel(CCM_MMC_CTRL_ENABLE| pll | CCM_MMC_CTRL_N(n) |
157 CCM_MMC_CTRL_M(div) | val, priv->mclkreg);
Hans de Goede06bfab02014-12-07 20:55:10 +0100158
159 debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n",
Simon Glass8e659a22017-07-04 13:31:24 -0600160 priv->mmc_no, hz, pll_hz, 1u << n, div, pll_hz / (1u << n) / div);
Hans de Goede06bfab02014-12-07 20:55:10 +0100161
162 return 0;
163}
164
Simon Glass87ff0f72017-07-04 13:31:25 -0600165static int mmc_update_clk(struct sunxi_mmc_priv *priv)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100166{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100167 unsigned int cmd;
168 unsigned timeout_msecs = 2000;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100169 unsigned long start = get_timer(0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100170
171 cmd = SUNXI_MMC_CMD_START |
172 SUNXI_MMC_CMD_UPCLK_ONLY |
173 SUNXI_MMC_CMD_WAIT_PRE_OVER;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100174
Simon Glass8e659a22017-07-04 13:31:24 -0600175 writel(cmd, &priv->reg->cmd);
176 while (readl(&priv->reg->cmd) & SUNXI_MMC_CMD_START) {
Philipp Tomsich1721b002018-03-21 12:18:58 +0100177 if (get_timer(start) > timeout_msecs)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100178 return -1;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100179 }
180
181 /* clock update sets various irq status bits, clear these */
Simon Glass8e659a22017-07-04 13:31:24 -0600182 writel(readl(&priv->reg->rint), &priv->reg->rint);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100183
184 return 0;
185}
186
Simon Glass87ff0f72017-07-04 13:31:25 -0600187static int mmc_config_clock(struct sunxi_mmc_priv *priv, struct mmc *mmc)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100188{
Simon Glass8e659a22017-07-04 13:31:24 -0600189 unsigned rval = readl(&priv->reg->clkcr);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100190
191 /* Disable Clock */
192 rval &= ~SUNXI_MMC_CLK_ENABLE;
Simon Glass8e659a22017-07-04 13:31:24 -0600193 writel(rval, &priv->reg->clkcr);
Simon Glass87ff0f72017-07-04 13:31:25 -0600194 if (mmc_update_clk(priv))
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100195 return -1;
196
Hans de Goede06bfab02014-12-07 20:55:10 +0100197 /* Set mod_clk to new rate */
Simon Glass8e659a22017-07-04 13:31:24 -0600198 if (mmc_set_mod_clk(priv, mmc->clock))
Hans de Goede06bfab02014-12-07 20:55:10 +0100199 return -1;
200
201 /* Clear internal divider */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100202 rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK;
Simon Glass8e659a22017-07-04 13:31:24 -0600203 writel(rval, &priv->reg->clkcr);
Hans de Goede06bfab02014-12-07 20:55:10 +0100204
Andre Przywara068962b2022-10-05 17:54:19 +0100205#if defined(CONFIG_SUNXI_GEN_SUN6I) || defined(CONFIG_SUN50I_GEN_H6) || defined(CONFIG_SUNXI_GEN_NCAT2)
Vasily Khoruzhick57789d62018-11-05 20:24:28 -0800206 /* A64 supports calibration of delays on MMC controller and we
207 * have to set delay of zero before starting calibration.
208 * Allwinner BSP driver sets a delay only in the case of
209 * using HS400 which is not supported by mainline U-Boot or
210 * Linux at the moment
211 */
Andre Przywara8c93a9c2021-05-05 10:06:24 +0100212 if (sunxi_mmc_can_calibrate())
213 writel(SUNXI_MMC_CAL_DL_SW_EN, &priv->reg->samp_dl);
Vasily Khoruzhick57789d62018-11-05 20:24:28 -0800214#endif
215
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100216 /* Re-enable Clock */
217 rval |= SUNXI_MMC_CLK_ENABLE;
Simon Glass8e659a22017-07-04 13:31:24 -0600218 writel(rval, &priv->reg->clkcr);
Simon Glass87ff0f72017-07-04 13:31:25 -0600219 if (mmc_update_clk(priv))
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100220 return -1;
221
222 return 0;
223}
224
Simon Glass87ff0f72017-07-04 13:31:25 -0600225static int sunxi_mmc_set_ios_common(struct sunxi_mmc_priv *priv,
226 struct mmc *mmc)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100227{
Hans de Goede06bfab02014-12-07 20:55:10 +0100228 debug("set ios: bus_width: %x, clock: %d\n",
229 mmc->bus_width, mmc->clock);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100230
231 /* Change clock first */
Simon Glass87ff0f72017-07-04 13:31:25 -0600232 if (mmc->clock && mmc_config_clock(priv, mmc) != 0) {
Simon Glass8e659a22017-07-04 13:31:24 -0600233 priv->fatal_err = 1;
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900234 return -EINVAL;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100235 }
236
237 /* Change bus width */
238 if (mmc->bus_width == 8)
Simon Glass8e659a22017-07-04 13:31:24 -0600239 writel(0x2, &priv->reg->width);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100240 else if (mmc->bus_width == 4)
Simon Glass8e659a22017-07-04 13:31:24 -0600241 writel(0x1, &priv->reg->width);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100242 else
Simon Glass8e659a22017-07-04 13:31:24 -0600243 writel(0x0, &priv->reg->width);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900244
245 return 0;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100246}
247
Simon Glass87ff0f72017-07-04 13:31:25 -0600248static int mmc_trans_data_by_cpu(struct sunxi_mmc_priv *priv, struct mmc *mmc,
249 struct mmc_data *data)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100250{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100251 const int reading = !!(data->flags & MMC_DATA_READ);
252 const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY :
253 SUNXI_MMC_STATUS_FIFO_FULL;
254 unsigned i;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100255 unsigned *buff = (unsigned int *)(reading ? data->dest : data->src);
Andre Przywara56086a42021-05-05 11:33:40 +0100256 unsigned word_cnt = (data->blocksize * data->blocks) >> 2;
257 unsigned timeout_msecs = word_cnt >> 6;
258 uint32_t status;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100259 unsigned long start;
260
261 if (timeout_msecs < 2000)
262 timeout_msecs = 2000;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100263
Hans de Goede411dc872014-06-09 11:36:55 +0200264 /* Always read / write data through the CPU */
Simon Glass8e659a22017-07-04 13:31:24 -0600265 setbits_le32(&priv->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB);
Hans de Goede411dc872014-06-09 11:36:55 +0200266
Philipp Tomsich1721b002018-03-21 12:18:58 +0100267 start = get_timer(0);
268
Andre Przywara56086a42021-05-05 11:33:40 +0100269 for (i = 0; i < word_cnt;) {
270 unsigned int in_fifo;
271
272 while ((status = readl(&priv->reg->status)) & status_bit) {
Philipp Tomsich1721b002018-03-21 12:18:58 +0100273 if (get_timer(start) > timeout_msecs)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100274 return -1;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100275 }
276
Andre Przywara56086a42021-05-05 11:33:40 +0100277 /*
278 * For writing we do not easily know the FIFO size, so have
279 * to check the FIFO status after every word written.
280 * TODO: For optimisation we could work out a minimum FIFO
281 * size across all SoCs, and use that together with the current
282 * fill level to write chunks of words.
283 */
284 if (!reading) {
285 writel(buff[i++], &priv->reg->fifo);
286 continue;
287 }
288
289 /*
290 * The status register holds the current FIFO level, so we
291 * can be sure to collect as many words from the FIFO
292 * register without checking the status register after every
293 * read. That saves half of the costly MMIO reads, effectively
294 * doubling the read performance.
Andre Przywaraf5020702021-09-03 16:49:16 +0100295 * Some SoCs (A20) report a level of 0 if the FIFO is
296 * completely full (value masked out?). Use a safe minimal
297 * FIFO size in this case.
Andre Przywara56086a42021-05-05 11:33:40 +0100298 */
Andre Przywaraf5020702021-09-03 16:49:16 +0100299 in_fifo = SUNXI_MMC_STATUS_FIFO_LEVEL(status);
300 if (in_fifo == 0 && (status & SUNXI_MMC_STATUS_FIFO_FULL))
301 in_fifo = 32;
302 for (; in_fifo > 0; in_fifo--)
Andre Przywara56086a42021-05-05 11:33:40 +0100303 buff[i++] = readl_relaxed(&priv->reg->fifo);
304 dmb();
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100305 }
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100306
307 return 0;
308}
309
Simon Glass87ff0f72017-07-04 13:31:25 -0600310static int mmc_rint_wait(struct sunxi_mmc_priv *priv, struct mmc *mmc,
311 uint timeout_msecs, uint done_bit, const char *what)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100312{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100313 unsigned int status;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100314 unsigned long start = get_timer(0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100315
316 do {
Simon Glass8e659a22017-07-04 13:31:24 -0600317 status = readl(&priv->reg->rint);
Philipp Tomsich1721b002018-03-21 12:18:58 +0100318 if ((get_timer(start) > timeout_msecs) ||
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100319 (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) {
320 debug("%s timeout %x\n", what,
321 status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900322 return -ETIMEDOUT;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100323 }
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100324 } while (!(status & done_bit));
325
326 return 0;
327}
328
Simon Glass87ff0f72017-07-04 13:31:25 -0600329static int sunxi_mmc_send_cmd_common(struct sunxi_mmc_priv *priv,
330 struct mmc *mmc, struct mmc_cmd *cmd,
331 struct mmc_data *data)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100332{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100333 unsigned int cmdval = SUNXI_MMC_CMD_START;
334 unsigned int timeout_msecs;
335 int error = 0;
336 unsigned int status = 0;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100337 unsigned int bytecnt = 0;
338
Simon Glass8e659a22017-07-04 13:31:24 -0600339 if (priv->fatal_err)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100340 return -1;
341 if (cmd->resp_type & MMC_RSP_BUSY)
342 debug("mmc cmd %d check rsp busy\n", cmd->cmdidx);
343 if (cmd->cmdidx == 12)
344 return 0;
345
346 if (!cmd->cmdidx)
347 cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
348 if (cmd->resp_type & MMC_RSP_PRESENT)
349 cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE;
350 if (cmd->resp_type & MMC_RSP_136)
351 cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE;
352 if (cmd->resp_type & MMC_RSP_CRC)
353 cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC;
354
355 if (data) {
Alexander Grafee1d8252016-03-29 17:29:09 +0200356 if ((u32)(long)data->dest & 0x3) {
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100357 error = -1;
358 goto out;
359 }
360
361 cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER;
362 if (data->flags & MMC_DATA_WRITE)
363 cmdval |= SUNXI_MMC_CMD_WRITE;
364 if (data->blocks > 1)
365 cmdval |= SUNXI_MMC_CMD_AUTO_STOP;
Simon Glass8e659a22017-07-04 13:31:24 -0600366 writel(data->blocksize, &priv->reg->blksz);
367 writel(data->blocks * data->blocksize, &priv->reg->bytecnt);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100368 }
369
Simon Glass8e659a22017-07-04 13:31:24 -0600370 debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", priv->mmc_no,
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100371 cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg);
Simon Glass8e659a22017-07-04 13:31:24 -0600372 writel(cmd->cmdarg, &priv->reg->arg);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100373
374 if (!data)
Simon Glass8e659a22017-07-04 13:31:24 -0600375 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100376
377 /*
378 * transfer data and check status
379 * STATREG[2] : FIFO empty
380 * STATREG[3] : FIFO full
381 */
382 if (data) {
383 int ret = 0;
384
385 bytecnt = data->blocksize * data->blocks;
386 debug("trans data %d bytes\n", bytecnt);
Simon Glass8e659a22017-07-04 13:31:24 -0600387 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
Simon Glass87ff0f72017-07-04 13:31:25 -0600388 ret = mmc_trans_data_by_cpu(priv, mmc, data);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100389 if (ret) {
Simon Glass8e659a22017-07-04 13:31:24 -0600390 error = readl(&priv->reg->rint) &
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100391 SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT;
Jaehoon Chung7825d202016-07-19 16:33:36 +0900392 error = -ETIMEDOUT;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100393 goto out;
394 }
395 }
396
Simon Glass87ff0f72017-07-04 13:31:25 -0600397 error = mmc_rint_wait(priv, mmc, 1000, SUNXI_MMC_RINT_COMMAND_DONE,
398 "cmd");
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100399 if (error)
400 goto out;
401
402 if (data) {
Hans de Goede411dc872014-06-09 11:36:55 +0200403 timeout_msecs = 120;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100404 debug("cacl timeout %x msec\n", timeout_msecs);
Simon Glass87ff0f72017-07-04 13:31:25 -0600405 error = mmc_rint_wait(priv, mmc, timeout_msecs,
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100406 data->blocks > 1 ?
407 SUNXI_MMC_RINT_AUTO_COMMAND_DONE :
408 SUNXI_MMC_RINT_DATA_OVER,
409 "data");
410 if (error)
411 goto out;
412 }
413
414 if (cmd->resp_type & MMC_RSP_BUSY) {
Philipp Tomsich1721b002018-03-21 12:18:58 +0100415 unsigned long start = get_timer(0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100416 timeout_msecs = 2000;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100417
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100418 do {
Simon Glass8e659a22017-07-04 13:31:24 -0600419 status = readl(&priv->reg->status);
Philipp Tomsich1721b002018-03-21 12:18:58 +0100420 if (get_timer(start) > timeout_msecs) {
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100421 debug("busy timeout\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900422 error = -ETIMEDOUT;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100423 goto out;
424 }
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100425 } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY);
426 }
427
428 if (cmd->resp_type & MMC_RSP_136) {
Simon Glass8e659a22017-07-04 13:31:24 -0600429 cmd->response[0] = readl(&priv->reg->resp3);
430 cmd->response[1] = readl(&priv->reg->resp2);
431 cmd->response[2] = readl(&priv->reg->resp1);
432 cmd->response[3] = readl(&priv->reg->resp0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100433 debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
434 cmd->response[3], cmd->response[2],
435 cmd->response[1], cmd->response[0]);
436 } else {
Simon Glass8e659a22017-07-04 13:31:24 -0600437 cmd->response[0] = readl(&priv->reg->resp0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100438 debug("mmc resp 0x%08x\n", cmd->response[0]);
439 }
440out:
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100441 if (error < 0) {
Simon Glass8e659a22017-07-04 13:31:24 -0600442 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
Simon Glass87ff0f72017-07-04 13:31:25 -0600443 mmc_update_clk(priv);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100444 }
Simon Glass8e659a22017-07-04 13:31:24 -0600445 writel(0xffffffff, &priv->reg->rint);
446 writel(readl(&priv->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET,
447 &priv->reg->gctrl);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100448
449 return error;
450}
451
Jernej Skrabec19d17342025-03-09 07:12:41 +0100452static void sunxi_mmc_reset(void *regs)
453{
454 /* Reset controller */
455 writel(SUNXI_MMC_GCTRL_RESET, regs + SUNXI_MMC_GCTRL);
456 udelay(1000);
457
458 if (IS_ENABLED(CONFIG_SUN50I_GEN_H6) || IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2)) {
459 /* Reset card */
460 writel(SUNXI_MMC_HWRST_ASSERT, regs + SUNXI_MMC_HWRST);
461 udelay(10);
462 writel(SUNXI_MMC_HWRST_DEASSERT, regs + SUNXI_MMC_HWRST);
463 udelay(300);
464
465 /* Setup FIFO R/W threshold. Needed on H616. */
466 writel(SUNXI_MMC_THLDC_READ_THLD(512) |
467 SUNXI_MMC_THLDC_WRITE_EN |
468 SUNXI_MMC_THLDC_READ_EN, regs + SUNXI_MMC_THLDC);
469 }
470}
471
Andre Przywaraf5032592022-07-13 17:21:44 +0100472/* non-DM code here is used by the (ARM) SPL only */
473
Simon Glass7484ae72017-07-04 13:31:27 -0600474#if !CONFIG_IS_ENABLED(DM_MMC)
Andre Przywaraf5032592022-07-13 17:21:44 +0100475/* support 4 mmc hosts */
476struct sunxi_mmc_priv mmc_host[4];
477
478static int mmc_resource_init(int sdc_no)
479{
480 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
481 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
482
483 debug("init mmc %d resource\n", sdc_no);
484
485 switch (sdc_no) {
486 case 0:
487 priv->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE;
488 priv->mclkreg = &ccm->sd0_clk_cfg;
489 break;
490 case 1:
491 priv->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE;
492 priv->mclkreg = &ccm->sd1_clk_cfg;
493 break;
494#ifdef SUNXI_MMC2_BASE
495 case 2:
496 priv->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE;
497 priv->mclkreg = &ccm->sd2_clk_cfg;
498 break;
499#endif
500#ifdef SUNXI_MMC3_BASE
501 case 3:
502 priv->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE;
503 priv->mclkreg = &ccm->sd3_clk_cfg;
504 break;
505#endif
506 default:
507 printf("Wrong mmc number %d\n", sdc_no);
508 return -1;
509 }
510 priv->mmc_no = sdc_no;
511
512 return 0;
513}
514
515static int sunxi_mmc_core_init(struct mmc *mmc)
516{
517 struct sunxi_mmc_priv *priv = mmc->priv;
518
Jernej Skrabec19d17342025-03-09 07:12:41 +0100519 sunxi_mmc_reset(priv->reg);
Andre Przywaraf5032592022-07-13 17:21:44 +0100520
521 return 0;
522}
523
Simon Glass87ff0f72017-07-04 13:31:25 -0600524static int sunxi_mmc_set_ios_legacy(struct mmc *mmc)
525{
526 struct sunxi_mmc_priv *priv = mmc->priv;
527
528 return sunxi_mmc_set_ios_common(priv, mmc);
529}
530
531static int sunxi_mmc_send_cmd_legacy(struct mmc *mmc, struct mmc_cmd *cmd,
532 struct mmc_data *data)
533{
534 struct sunxi_mmc_priv *priv = mmc->priv;
535
536 return sunxi_mmc_send_cmd_common(priv, mmc, cmd, data);
537}
538
Andre Przywaradad8a8d2022-07-13 17:21:43 +0100539/* .getcd is not needed by the SPL */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100540static const struct mmc_ops sunxi_mmc_ops = {
Simon Glass87ff0f72017-07-04 13:31:25 -0600541 .send_cmd = sunxi_mmc_send_cmd_legacy,
542 .set_ios = sunxi_mmc_set_ios_legacy,
Siarhei Siamashka253d77d2015-02-01 00:42:14 +0200543 .init = sunxi_mmc_core_init,
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100544};
545
Hans de Goede63deaa82014-10-02 21:13:54 +0200546struct mmc *sunxi_mmc_init(int sdc_no)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100547{
Simon Glass3a654152017-07-04 13:31:26 -0600548 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Simon Glass87ff0f72017-07-04 13:31:25 -0600549 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
550 struct mmc_config *cfg = &priv->cfg;
Simon Glass3a654152017-07-04 13:31:26 -0600551 int ret;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100552
Simon Glass87ff0f72017-07-04 13:31:25 -0600553 memset(priv, '\0', sizeof(struct sunxi_mmc_priv));
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100554
555 cfg->name = "SUNXI SD/MMC";
556 cfg->ops = &sunxi_mmc_ops;
557
558 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
559 cfg->host_caps = MMC_MODE_4BIT;
Andre Przywaraf2f3a592020-12-18 22:02:11 +0000560
561 if ((IS_ENABLED(CONFIG_MACH_SUN50I) || IS_ENABLED(CONFIG_MACH_SUN8I) ||
562 IS_ENABLED(CONFIG_SUN50I_GEN_H6)) && (sdc_no == 2))
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200563 cfg->host_caps = MMC_MODE_8BIT;
Andre Przywaraf2f3a592020-12-18 22:02:11 +0000564
Rob Herring5fd3edd2015-03-23 17:56:59 -0500565 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100566 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
567
568 cfg->f_min = 400000;
569 cfg->f_max = 52000000;
570
Hans de Goede3d1095f2014-10-31 16:55:02 +0100571 if (mmc_resource_init(sdc_no) != 0)
572 return NULL;
573
Simon Glass3a654152017-07-04 13:31:26 -0600574 /* config ahb clock */
575 debug("init mmc %d clock and io\n", sdc_no);
Andre Przywara068962b2022-10-05 17:54:19 +0100576#if !defined(CONFIG_SUN50I_GEN_H6) && !defined(CONFIG_SUNXI_GEN_NCAT2)
Simon Glass3a654152017-07-04 13:31:26 -0600577 setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no));
578
579#ifdef CONFIG_SUNXI_GEN_SUN6I
580 /* unassert reset */
581 setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no));
582#endif
583#if defined(CONFIG_MACH_SUN9I)
584 /* sun9i has a mmc-common module, also set the gate and reset there */
585 writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET,
586 SUNXI_MMC_COMMON_BASE + 4 * sdc_no);
587#endif
Jernej Skrabecd6da7ab2021-01-11 21:11:35 +0100588#else /* CONFIG_SUN50I_GEN_H6 */
Icenowy Zhenga838a152018-07-21 16:20:29 +0800589 setbits_le32(&ccm->sd_gate_reset, 1 << sdc_no);
590 /* unassert reset */
591 setbits_le32(&ccm->sd_gate_reset, 1 << (RESET_SHIFT + sdc_no));
592#endif
Simon Glass3a654152017-07-04 13:31:26 -0600593 ret = mmc_set_mod_clk(priv, 24000000);
594 if (ret)
595 return NULL;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100596
Maxime Ripard0cc228e2017-08-23 13:41:33 +0200597 return mmc_create(cfg, priv);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100598}
Andre Przywaraf5032592022-07-13 17:21:44 +0100599
600#else /* CONFIG_DM_MMC code below, as used by U-Boot proper */
Simon Glass7484ae72017-07-04 13:31:27 -0600601
602static int sunxi_mmc_set_ios(struct udevice *dev)
603{
Simon Glassfa20e932020-12-03 16:55:20 -0700604 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600605 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
606
607 return sunxi_mmc_set_ios_common(priv, &plat->mmc);
608}
609
610static int sunxi_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
611 struct mmc_data *data)
612{
Simon Glassfa20e932020-12-03 16:55:20 -0700613 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600614 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
615
616 return sunxi_mmc_send_cmd_common(priv, &plat->mmc, cmd, data);
617}
618
619static int sunxi_mmc_getcd(struct udevice *dev)
620{
Andre Przywarad8a29602021-04-21 09:33:04 +0100621 struct mmc *mmc = mmc_get_mmc_dev(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600622 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
623
Andre Przywarad8a29602021-04-21 09:33:04 +0100624 /* If polling, assume that the card is always present. */
625 if ((mmc->cfg->host_caps & MMC_CAP_NONREMOVABLE) ||
626 (mmc->cfg->host_caps & MMC_CAP_NEEDS_POLL))
627 return 1;
628
Heinrich Schuchardt8dc0a992018-02-01 23:39:19 +0100629 if (dm_gpio_is_valid(&priv->cd_gpio)) {
630 int cd_state = dm_gpio_get_value(&priv->cd_gpio);
Simon Glass7484ae72017-07-04 13:31:27 -0600631
Andre Przywarad8a29602021-04-21 09:33:04 +0100632 if (mmc->cfg->host_caps & MMC_CAP_CD_ACTIVE_HIGH)
633 return !cd_state;
634 else
635 return cd_state;
Heinrich Schuchardt8dc0a992018-02-01 23:39:19 +0100636 }
Simon Glass7484ae72017-07-04 13:31:27 -0600637 return 1;
638}
639
640static const struct dm_mmc_ops sunxi_mmc_ops = {
641 .send_cmd = sunxi_mmc_send_cmd,
642 .set_ios = sunxi_mmc_set_ios,
643 .get_cd = sunxi_mmc_getcd,
644};
645
Andre Przywara6b12ad82021-01-11 21:11:44 +0100646static unsigned get_mclk_offset(void)
647{
648 if (IS_ENABLED(CONFIG_MACH_SUN9I_A80))
649 return 0x410;
650
Andre Przywara068962b2022-10-05 17:54:19 +0100651 if (IS_ENABLED(CONFIG_SUN50I_GEN_H6) || IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2))
Andre Przywara6b12ad82021-01-11 21:11:44 +0100652 return 0x830;
653
654 return 0x88;
655};
656
Simon Glass7484ae72017-07-04 13:31:27 -0600657static int sunxi_mmc_probe(struct udevice *dev)
658{
659 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700660 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600661 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
Andre Przywara29b533c2019-01-29 15:54:13 +0000662 struct reset_ctl_bulk reset_bulk;
663 struct clk gate_clk;
Simon Glass7484ae72017-07-04 13:31:27 -0600664 struct mmc_config *cfg = &plat->cfg;
665 struct ofnode_phandle_args args;
Andre Przywara29b533c2019-01-29 15:54:13 +0000666 u32 *ccu_reg;
Andre Przywarad8a29602021-04-21 09:33:04 +0100667 int ret;
Simon Glass7484ae72017-07-04 13:31:27 -0600668
669 cfg->name = dev->name;
Simon Glass7484ae72017-07-04 13:31:27 -0600670
671 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Andre Przywarad8a29602021-04-21 09:33:04 +0100672 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
Simon Glass7484ae72017-07-04 13:31:27 -0600673 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
674
675 cfg->f_min = 400000;
676 cfg->f_max = 52000000;
677
Andre Przywarad8a29602021-04-21 09:33:04 +0100678 ret = mmc_of_parse(dev, cfg);
679 if (ret)
680 return ret;
681
Andre Przywara70bbb412021-04-29 09:31:58 +0100682 priv->reg = dev_read_addr_ptr(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600683
684 /* We don't have a sunxi clock driver so find the clock address here */
685 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
686 1, &args);
687 if (ret)
688 return ret;
Andre Przywara70bbb412021-04-29 09:31:58 +0100689 ccu_reg = (u32 *)(uintptr_t)ofnode_get_addr(args.node);
Simon Glass7484ae72017-07-04 13:31:27 -0600690
Jagan Teki2002b752019-01-09 16:58:39 +0530691 priv->mmc_no = ((uintptr_t)priv->reg - SUNXI_MMC0_BASE) / 0x1000;
Andre Przywara6b12ad82021-01-11 21:11:44 +0100692 priv->mclkreg = (void *)ccu_reg + get_mclk_offset() + priv->mmc_no * 4;
Andre Przywara29b533c2019-01-29 15:54:13 +0000693
694 ret = clk_get_by_name(dev, "ahb", &gate_clk);
695 if (!ret)
696 clk_enable(&gate_clk);
697
698 ret = reset_get_bulk(dev, &reset_bulk);
699 if (!ret)
700 reset_deassert_bulk(&reset_bulk);
Simon Glass7484ae72017-07-04 13:31:27 -0600701
702 ret = mmc_set_mod_clk(priv, 24000000);
703 if (ret)
704 return ret;
705
706 /* This GPIO is optional */
Samuel Hollandb6b35572021-10-20 23:52:57 -0500707 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
708 GPIOD_IS_IN | GPIOD_PULL_UP);
Simon Glass7484ae72017-07-04 13:31:27 -0600709
710 upriv->mmc = &plat->mmc;
711
Jernej Skrabec19d17342025-03-09 07:12:41 +0100712 sunxi_mmc_reset(priv->reg);
Simon Glass7484ae72017-07-04 13:31:27 -0600713
714 return 0;
715}
716
717static int sunxi_mmc_bind(struct udevice *dev)
718{
Simon Glassfa20e932020-12-03 16:55:20 -0700719 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600720
721 return mmc_bind(dev, &plat->mmc, &plat->cfg);
722}
723
724static const struct udevice_id sunxi_mmc_ids[] = {
Andre Przywara6b12ad82021-01-11 21:11:44 +0100725 { .compatible = "allwinner,sun4i-a10-mmc" },
726 { .compatible = "allwinner,sun5i-a13-mmc" },
727 { .compatible = "allwinner,sun7i-a20-mmc" },
728 { .compatible = "allwinner,sun8i-a83t-emmc" },
729 { .compatible = "allwinner,sun9i-a80-mmc" },
Samuel Hollanddc64e442023-10-31 00:22:34 -0500730 { .compatible = "allwinner,sun20i-d1-mmc" },
Andre Przywara6b12ad82021-01-11 21:11:44 +0100731 { .compatible = "allwinner,sun50i-a64-mmc" },
732 { .compatible = "allwinner,sun50i-a64-emmc" },
733 { .compatible = "allwinner,sun50i-h6-mmc" },
734 { .compatible = "allwinner,sun50i-h6-emmc" },
735 { .compatible = "allwinner,sun50i-a100-mmc" },
736 { .compatible = "allwinner,sun50i-a100-emmc" },
Jagan Teki2002b752019-01-09 16:58:39 +0530737 { /* sentinel */ }
Simon Glass7484ae72017-07-04 13:31:27 -0600738};
739
740U_BOOT_DRIVER(sunxi_mmc_drv) = {
741 .name = "sunxi_mmc",
742 .id = UCLASS_MMC,
743 .of_match = sunxi_mmc_ids,
744 .bind = sunxi_mmc_bind,
745 .probe = sunxi_mmc_probe,
746 .ops = &sunxi_mmc_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700747 .plat_auto = sizeof(struct sunxi_mmc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700748 .priv_auto = sizeof(struct sunxi_mmc_priv),
Simon Glass7484ae72017-07-04 13:31:27 -0600749};
750#endif