blob: 03e33753fcf915eafff4fd8248925d5b157beaab [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>
Andre Przywaraf944a612022-09-06 10:36:38 +010030#include <sunxi_gpio.h>
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010031
Andre Przywara3f23aa62021-05-05 09:57:47 +010032#ifndef CCM_MMC_CTRL_MODE_SEL_NEW
33#define CCM_MMC_CTRL_MODE_SEL_NEW 0
34#endif
35
Simon Glass7484ae72017-07-04 13:31:27 -060036struct sunxi_mmc_plat {
37 struct mmc_config cfg;
38 struct mmc mmc;
39};
40
Simon Glass3f19fbf2017-07-04 13:31:23 -060041struct sunxi_mmc_priv {
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010042 unsigned mmc_no;
43 uint32_t *mclkreg;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010044 unsigned fatal_err;
Simon Glass7484ae72017-07-04 13:31:27 -060045 struct gpio_desc cd_gpio; /* Change Detect GPIO */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +010046 struct sunxi_mmc *reg;
47 struct mmc_config cfg;
48};
49
Andre Przywara8c93a9c2021-05-05 10:06:24 +010050/*
51 * All A64 and later MMC controllers feature auto-calibration. This would
52 * normally be detected via the compatible string, but we need something
53 * which works in the SPL as well.
54 */
55static bool sunxi_mmc_can_calibrate(void)
56{
57 return IS_ENABLED(CONFIG_MACH_SUN50I) ||
58 IS_ENABLED(CONFIG_MACH_SUN50I_H5) ||
59 IS_ENABLED(CONFIG_SUN50I_GEN_H6) ||
60 IS_ENABLED(CONFIG_MACH_SUN8I_R40);
61}
62
Simon Glass8e659a22017-07-04 13:31:24 -060063static int mmc_set_mod_clk(struct sunxi_mmc_priv *priv, unsigned int hz)
Hans de Goede06bfab02014-12-07 20:55:10 +010064{
65 unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly;
Andre Przywara3f23aa62021-05-05 09:57:47 +010066 bool new_mode = IS_ENABLED(CONFIG_MMC_SUNXI_HAS_NEW_MODE);
Maxime Ripard95e34702017-08-23 12:03:41 +020067 u32 val = 0;
68
Vasily Khoruzhicka4e8dd92018-11-09 20:41:46 -080069 /* A83T support new mode only on eMMC */
70 if (IS_ENABLED(CONFIG_MACH_SUN8I_A83T) && priv->mmc_no != 2)
71 new_mode = false;
Maxime Ripard95e34702017-08-23 12:03:41 +020072
Hans de Goede06bfab02014-12-07 20:55:10 +010073 if (hz <= 24000000) {
74 pll = CCM_MMC_CTRL_OSCM24;
75 pll_hz = 24000000;
76 } else {
Hans de Goedef1865db2015-01-14 19:05:03 +010077#ifdef CONFIG_MACH_SUN9I
78 pll = CCM_MMC_CTRL_PLL_PERIPH0;
79 pll_hz = clock_get_pll4_periph0();
80#else
Andre Przywaradd505d12021-05-05 09:57:47 +010081 /*
82 * SoCs since the A64 (H5, H6, H616) actually use the doubled
83 * rate of PLL6/PERIPH0 as an input clock, but compensate for
84 * that with a fixed post-divider of 2 in the mod clock.
85 * This cancels each other out, so for simplicity we just
86 * pretend it's always PLL6 without a post divider here.
87 */
Hans de Goede06bfab02014-12-07 20:55:10 +010088 pll = CCM_MMC_CTRL_PLL6;
89 pll_hz = clock_get_pll6();
Hans de Goedef1865db2015-01-14 19:05:03 +010090#endif
Hans de Goede06bfab02014-12-07 20:55:10 +010091 }
92
93 div = pll_hz / hz;
94 if (pll_hz % hz)
95 div++;
96
97 n = 0;
98 while (div > 16) {
99 n++;
100 div = (div + 1) / 2;
101 }
102
103 if (n > 3) {
Simon Glass8e659a22017-07-04 13:31:24 -0600104 printf("mmc %u error cannot set clock to %u\n", priv->mmc_no,
105 hz);
Hans de Goede06bfab02014-12-07 20:55:10 +0100106 return -1;
107 }
108
109 /* determine delays */
110 if (hz <= 400000) {
111 oclk_dly = 0;
Hans de Goede5192ba22015-09-23 16:13:10 +0200112 sclk_dly = 0;
Hans de Goede06bfab02014-12-07 20:55:10 +0100113 } else if (hz <= 25000000) {
114 oclk_dly = 0;
115 sclk_dly = 5;
Hans de Goede06bfab02014-12-07 20:55:10 +0100116 } else {
Andre Przywaraf2f3a592020-12-18 22:02:11 +0000117 if (IS_ENABLED(CONFIG_MACH_SUN9I)) {
118 if (hz <= 52000000)
119 oclk_dly = 5;
120 else
121 oclk_dly = 2;
122 } else {
123 if (hz <= 52000000)
124 oclk_dly = 3;
125 else
126 oclk_dly = 1;
127 }
Hans de Goede5192ba22015-09-23 16:13:10 +0200128 sclk_dly = 4;
Maxime Ripard95e34702017-08-23 12:03:41 +0200129 }
130
131 if (new_mode) {
Andre Przywara3f23aa62021-05-05 09:57:47 +0100132 val |= CCM_MMC_CTRL_MODE_SEL_NEW;
Chen-Yu Tsaie76f0062017-08-31 21:57:48 +0800133 setbits_le32(&priv->reg->ntsr, SUNXI_MMC_NTSR_MODE_SEL_NEW);
Andre Przywara8c93a9c2021-05-05 10:06:24 +0100134 }
135
136 if (!sunxi_mmc_can_calibrate()) {
Vasily Khoruzhick57789d62018-11-05 20:24:28 -0800137 /*
138 * Use hardcoded delay values if controller doesn't support
139 * calibration
140 */
Maxime Ripard95e34702017-08-23 12:03:41 +0200141 val = CCM_MMC_CTRL_OCLK_DLY(oclk_dly) |
142 CCM_MMC_CTRL_SCLK_DLY(sclk_dly);
Hans de Goede06bfab02014-12-07 20:55:10 +0100143 }
144
Maxime Ripard95e34702017-08-23 12:03:41 +0200145 writel(CCM_MMC_CTRL_ENABLE| pll | CCM_MMC_CTRL_N(n) |
146 CCM_MMC_CTRL_M(div) | val, priv->mclkreg);
Hans de Goede06bfab02014-12-07 20:55:10 +0100147
148 debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n",
Simon Glass8e659a22017-07-04 13:31:24 -0600149 priv->mmc_no, hz, pll_hz, 1u << n, div, pll_hz / (1u << n) / div);
Hans de Goede06bfab02014-12-07 20:55:10 +0100150
151 return 0;
152}
153
Simon Glass87ff0f72017-07-04 13:31:25 -0600154static int mmc_update_clk(struct sunxi_mmc_priv *priv)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100155{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100156 unsigned int cmd;
157 unsigned timeout_msecs = 2000;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100158 unsigned long start = get_timer(0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100159
160 cmd = SUNXI_MMC_CMD_START |
161 SUNXI_MMC_CMD_UPCLK_ONLY |
162 SUNXI_MMC_CMD_WAIT_PRE_OVER;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100163
Simon Glass8e659a22017-07-04 13:31:24 -0600164 writel(cmd, &priv->reg->cmd);
165 while (readl(&priv->reg->cmd) & SUNXI_MMC_CMD_START) {
Philipp Tomsich1721b002018-03-21 12:18:58 +0100166 if (get_timer(start) > timeout_msecs)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100167 return -1;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100168 }
169
170 /* clock update sets various irq status bits, clear these */
Simon Glass8e659a22017-07-04 13:31:24 -0600171 writel(readl(&priv->reg->rint), &priv->reg->rint);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100172
173 return 0;
174}
175
Simon Glass87ff0f72017-07-04 13:31:25 -0600176static int mmc_config_clock(struct sunxi_mmc_priv *priv, struct mmc *mmc)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100177{
Simon Glass8e659a22017-07-04 13:31:24 -0600178 unsigned rval = readl(&priv->reg->clkcr);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100179
180 /* Disable Clock */
181 rval &= ~SUNXI_MMC_CLK_ENABLE;
Simon Glass8e659a22017-07-04 13:31:24 -0600182 writel(rval, &priv->reg->clkcr);
Simon Glass87ff0f72017-07-04 13:31:25 -0600183 if (mmc_update_clk(priv))
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100184 return -1;
185
Hans de Goede06bfab02014-12-07 20:55:10 +0100186 /* Set mod_clk to new rate */
Simon Glass8e659a22017-07-04 13:31:24 -0600187 if (mmc_set_mod_clk(priv, mmc->clock))
Hans de Goede06bfab02014-12-07 20:55:10 +0100188 return -1;
189
190 /* Clear internal divider */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100191 rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK;
Simon Glass8e659a22017-07-04 13:31:24 -0600192 writel(rval, &priv->reg->clkcr);
Hans de Goede06bfab02014-12-07 20:55:10 +0100193
Andre Przywara8c93a9c2021-05-05 10:06:24 +0100194#if defined(CONFIG_SUNXI_GEN_SUN6I) || defined(CONFIG_SUN50I_GEN_H6)
Vasily Khoruzhick57789d62018-11-05 20:24:28 -0800195 /* A64 supports calibration of delays on MMC controller and we
196 * have to set delay of zero before starting calibration.
197 * Allwinner BSP driver sets a delay only in the case of
198 * using HS400 which is not supported by mainline U-Boot or
199 * Linux at the moment
200 */
Andre Przywara8c93a9c2021-05-05 10:06:24 +0100201 if (sunxi_mmc_can_calibrate())
202 writel(SUNXI_MMC_CAL_DL_SW_EN, &priv->reg->samp_dl);
Vasily Khoruzhick57789d62018-11-05 20:24:28 -0800203#endif
204
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100205 /* Re-enable Clock */
206 rval |= SUNXI_MMC_CLK_ENABLE;
Simon Glass8e659a22017-07-04 13:31:24 -0600207 writel(rval, &priv->reg->clkcr);
Simon Glass87ff0f72017-07-04 13:31:25 -0600208 if (mmc_update_clk(priv))
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100209 return -1;
210
211 return 0;
212}
213
Simon Glass87ff0f72017-07-04 13:31:25 -0600214static int sunxi_mmc_set_ios_common(struct sunxi_mmc_priv *priv,
215 struct mmc *mmc)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100216{
Hans de Goede06bfab02014-12-07 20:55:10 +0100217 debug("set ios: bus_width: %x, clock: %d\n",
218 mmc->bus_width, mmc->clock);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100219
220 /* Change clock first */
Simon Glass87ff0f72017-07-04 13:31:25 -0600221 if (mmc->clock && mmc_config_clock(priv, mmc) != 0) {
Simon Glass8e659a22017-07-04 13:31:24 -0600222 priv->fatal_err = 1;
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900223 return -EINVAL;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100224 }
225
226 /* Change bus width */
227 if (mmc->bus_width == 8)
Simon Glass8e659a22017-07-04 13:31:24 -0600228 writel(0x2, &priv->reg->width);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100229 else if (mmc->bus_width == 4)
Simon Glass8e659a22017-07-04 13:31:24 -0600230 writel(0x1, &priv->reg->width);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100231 else
Simon Glass8e659a22017-07-04 13:31:24 -0600232 writel(0x0, &priv->reg->width);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900233
234 return 0;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100235}
236
Simon Glass87ff0f72017-07-04 13:31:25 -0600237static int mmc_trans_data_by_cpu(struct sunxi_mmc_priv *priv, struct mmc *mmc,
238 struct mmc_data *data)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100239{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100240 const int reading = !!(data->flags & MMC_DATA_READ);
241 const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY :
242 SUNXI_MMC_STATUS_FIFO_FULL;
243 unsigned i;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100244 unsigned *buff = (unsigned int *)(reading ? data->dest : data->src);
Andre Przywara56086a42021-05-05 11:33:40 +0100245 unsigned word_cnt = (data->blocksize * data->blocks) >> 2;
246 unsigned timeout_msecs = word_cnt >> 6;
247 uint32_t status;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100248 unsigned long start;
249
250 if (timeout_msecs < 2000)
251 timeout_msecs = 2000;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100252
Hans de Goede411dc872014-06-09 11:36:55 +0200253 /* Always read / write data through the CPU */
Simon Glass8e659a22017-07-04 13:31:24 -0600254 setbits_le32(&priv->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB);
Hans de Goede411dc872014-06-09 11:36:55 +0200255
Philipp Tomsich1721b002018-03-21 12:18:58 +0100256 start = get_timer(0);
257
Andre Przywara56086a42021-05-05 11:33:40 +0100258 for (i = 0; i < word_cnt;) {
259 unsigned int in_fifo;
260
261 while ((status = readl(&priv->reg->status)) & status_bit) {
Philipp Tomsich1721b002018-03-21 12:18:58 +0100262 if (get_timer(start) > timeout_msecs)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100263 return -1;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100264 }
265
Andre Przywara56086a42021-05-05 11:33:40 +0100266 /*
267 * For writing we do not easily know the FIFO size, so have
268 * to check the FIFO status after every word written.
269 * TODO: For optimisation we could work out a minimum FIFO
270 * size across all SoCs, and use that together with the current
271 * fill level to write chunks of words.
272 */
273 if (!reading) {
274 writel(buff[i++], &priv->reg->fifo);
275 continue;
276 }
277
278 /*
279 * The status register holds the current FIFO level, so we
280 * can be sure to collect as many words from the FIFO
281 * register without checking the status register after every
282 * read. That saves half of the costly MMIO reads, effectively
283 * doubling the read performance.
Andre Przywaraf5020702021-09-03 16:49:16 +0100284 * Some SoCs (A20) report a level of 0 if the FIFO is
285 * completely full (value masked out?). Use a safe minimal
286 * FIFO size in this case.
Andre Przywara56086a42021-05-05 11:33:40 +0100287 */
Andre Przywaraf5020702021-09-03 16:49:16 +0100288 in_fifo = SUNXI_MMC_STATUS_FIFO_LEVEL(status);
289 if (in_fifo == 0 && (status & SUNXI_MMC_STATUS_FIFO_FULL))
290 in_fifo = 32;
291 for (; in_fifo > 0; in_fifo--)
Andre Przywara56086a42021-05-05 11:33:40 +0100292 buff[i++] = readl_relaxed(&priv->reg->fifo);
293 dmb();
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100294 }
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100295
296 return 0;
297}
298
Simon Glass87ff0f72017-07-04 13:31:25 -0600299static int mmc_rint_wait(struct sunxi_mmc_priv *priv, struct mmc *mmc,
300 uint timeout_msecs, uint done_bit, const char *what)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100301{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100302 unsigned int status;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100303 unsigned long start = get_timer(0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100304
305 do {
Simon Glass8e659a22017-07-04 13:31:24 -0600306 status = readl(&priv->reg->rint);
Philipp Tomsich1721b002018-03-21 12:18:58 +0100307 if ((get_timer(start) > timeout_msecs) ||
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100308 (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) {
309 debug("%s timeout %x\n", what,
310 status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900311 return -ETIMEDOUT;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100312 }
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100313 } while (!(status & done_bit));
314
315 return 0;
316}
317
Simon Glass87ff0f72017-07-04 13:31:25 -0600318static int sunxi_mmc_send_cmd_common(struct sunxi_mmc_priv *priv,
319 struct mmc *mmc, struct mmc_cmd *cmd,
320 struct mmc_data *data)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100321{
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100322 unsigned int cmdval = SUNXI_MMC_CMD_START;
323 unsigned int timeout_msecs;
324 int error = 0;
325 unsigned int status = 0;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100326 unsigned int bytecnt = 0;
327
Simon Glass8e659a22017-07-04 13:31:24 -0600328 if (priv->fatal_err)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100329 return -1;
330 if (cmd->resp_type & MMC_RSP_BUSY)
331 debug("mmc cmd %d check rsp busy\n", cmd->cmdidx);
332 if (cmd->cmdidx == 12)
333 return 0;
334
335 if (!cmd->cmdidx)
336 cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
337 if (cmd->resp_type & MMC_RSP_PRESENT)
338 cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE;
339 if (cmd->resp_type & MMC_RSP_136)
340 cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE;
341 if (cmd->resp_type & MMC_RSP_CRC)
342 cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC;
343
344 if (data) {
Alexander Grafee1d8252016-03-29 17:29:09 +0200345 if ((u32)(long)data->dest & 0x3) {
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100346 error = -1;
347 goto out;
348 }
349
350 cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER;
351 if (data->flags & MMC_DATA_WRITE)
352 cmdval |= SUNXI_MMC_CMD_WRITE;
353 if (data->blocks > 1)
354 cmdval |= SUNXI_MMC_CMD_AUTO_STOP;
Simon Glass8e659a22017-07-04 13:31:24 -0600355 writel(data->blocksize, &priv->reg->blksz);
356 writel(data->blocks * data->blocksize, &priv->reg->bytecnt);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100357 }
358
Simon Glass8e659a22017-07-04 13:31:24 -0600359 debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", priv->mmc_no,
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100360 cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg);
Simon Glass8e659a22017-07-04 13:31:24 -0600361 writel(cmd->cmdarg, &priv->reg->arg);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100362
363 if (!data)
Simon Glass8e659a22017-07-04 13:31:24 -0600364 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100365
366 /*
367 * transfer data and check status
368 * STATREG[2] : FIFO empty
369 * STATREG[3] : FIFO full
370 */
371 if (data) {
372 int ret = 0;
373
374 bytecnt = data->blocksize * data->blocks;
375 debug("trans data %d bytes\n", bytecnt);
Simon Glass8e659a22017-07-04 13:31:24 -0600376 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
Simon Glass87ff0f72017-07-04 13:31:25 -0600377 ret = mmc_trans_data_by_cpu(priv, mmc, data);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100378 if (ret) {
Simon Glass8e659a22017-07-04 13:31:24 -0600379 error = readl(&priv->reg->rint) &
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100380 SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT;
Jaehoon Chung7825d202016-07-19 16:33:36 +0900381 error = -ETIMEDOUT;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100382 goto out;
383 }
384 }
385
Simon Glass87ff0f72017-07-04 13:31:25 -0600386 error = mmc_rint_wait(priv, mmc, 1000, SUNXI_MMC_RINT_COMMAND_DONE,
387 "cmd");
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100388 if (error)
389 goto out;
390
391 if (data) {
Hans de Goede411dc872014-06-09 11:36:55 +0200392 timeout_msecs = 120;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100393 debug("cacl timeout %x msec\n", timeout_msecs);
Simon Glass87ff0f72017-07-04 13:31:25 -0600394 error = mmc_rint_wait(priv, mmc, timeout_msecs,
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100395 data->blocks > 1 ?
396 SUNXI_MMC_RINT_AUTO_COMMAND_DONE :
397 SUNXI_MMC_RINT_DATA_OVER,
398 "data");
399 if (error)
400 goto out;
401 }
402
403 if (cmd->resp_type & MMC_RSP_BUSY) {
Philipp Tomsich1721b002018-03-21 12:18:58 +0100404 unsigned long start = get_timer(0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100405 timeout_msecs = 2000;
Philipp Tomsich1721b002018-03-21 12:18:58 +0100406
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100407 do {
Simon Glass8e659a22017-07-04 13:31:24 -0600408 status = readl(&priv->reg->status);
Philipp Tomsich1721b002018-03-21 12:18:58 +0100409 if (get_timer(start) > timeout_msecs) {
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100410 debug("busy timeout\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900411 error = -ETIMEDOUT;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100412 goto out;
413 }
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100414 } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY);
415 }
416
417 if (cmd->resp_type & MMC_RSP_136) {
Simon Glass8e659a22017-07-04 13:31:24 -0600418 cmd->response[0] = readl(&priv->reg->resp3);
419 cmd->response[1] = readl(&priv->reg->resp2);
420 cmd->response[2] = readl(&priv->reg->resp1);
421 cmd->response[3] = readl(&priv->reg->resp0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100422 debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
423 cmd->response[3], cmd->response[2],
424 cmd->response[1], cmd->response[0]);
425 } else {
Simon Glass8e659a22017-07-04 13:31:24 -0600426 cmd->response[0] = readl(&priv->reg->resp0);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100427 debug("mmc resp 0x%08x\n", cmd->response[0]);
428 }
429out:
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100430 if (error < 0) {
Simon Glass8e659a22017-07-04 13:31:24 -0600431 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
Simon Glass87ff0f72017-07-04 13:31:25 -0600432 mmc_update_clk(priv);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100433 }
Simon Glass8e659a22017-07-04 13:31:24 -0600434 writel(0xffffffff, &priv->reg->rint);
435 writel(readl(&priv->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET,
436 &priv->reg->gctrl);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100437
438 return error;
439}
440
Andre Przywaraf5032592022-07-13 17:21:44 +0100441/* non-DM code here is used by the (ARM) SPL only */
442
Simon Glass7484ae72017-07-04 13:31:27 -0600443#if !CONFIG_IS_ENABLED(DM_MMC)
Andre Przywaraf5032592022-07-13 17:21:44 +0100444/* support 4 mmc hosts */
445struct sunxi_mmc_priv mmc_host[4];
446
447static int mmc_resource_init(int sdc_no)
448{
449 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
450 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
451
452 debug("init mmc %d resource\n", sdc_no);
453
454 switch (sdc_no) {
455 case 0:
456 priv->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE;
457 priv->mclkreg = &ccm->sd0_clk_cfg;
458 break;
459 case 1:
460 priv->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE;
461 priv->mclkreg = &ccm->sd1_clk_cfg;
462 break;
463#ifdef SUNXI_MMC2_BASE
464 case 2:
465 priv->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE;
466 priv->mclkreg = &ccm->sd2_clk_cfg;
467 break;
468#endif
469#ifdef SUNXI_MMC3_BASE
470 case 3:
471 priv->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE;
472 priv->mclkreg = &ccm->sd3_clk_cfg;
473 break;
474#endif
475 default:
476 printf("Wrong mmc number %d\n", sdc_no);
477 return -1;
478 }
479 priv->mmc_no = sdc_no;
480
481 return 0;
482}
483
484static int sunxi_mmc_core_init(struct mmc *mmc)
485{
486 struct sunxi_mmc_priv *priv = mmc->priv;
487
488 /* Reset controller */
489 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
490 udelay(1000);
491
492 return 0;
493}
494
Simon Glass87ff0f72017-07-04 13:31:25 -0600495static int sunxi_mmc_set_ios_legacy(struct mmc *mmc)
496{
497 struct sunxi_mmc_priv *priv = mmc->priv;
498
499 return sunxi_mmc_set_ios_common(priv, mmc);
500}
501
502static int sunxi_mmc_send_cmd_legacy(struct mmc *mmc, struct mmc_cmd *cmd,
503 struct mmc_data *data)
504{
505 struct sunxi_mmc_priv *priv = mmc->priv;
506
507 return sunxi_mmc_send_cmd_common(priv, mmc, cmd, data);
508}
509
Andre Przywaradad8a8d2022-07-13 17:21:43 +0100510/* .getcd is not needed by the SPL */
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100511static const struct mmc_ops sunxi_mmc_ops = {
Simon Glass87ff0f72017-07-04 13:31:25 -0600512 .send_cmd = sunxi_mmc_send_cmd_legacy,
513 .set_ios = sunxi_mmc_set_ios_legacy,
Siarhei Siamashka253d77d2015-02-01 00:42:14 +0200514 .init = sunxi_mmc_core_init,
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100515};
516
Hans de Goede63deaa82014-10-02 21:13:54 +0200517struct mmc *sunxi_mmc_init(int sdc_no)
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100518{
Simon Glass3a654152017-07-04 13:31:26 -0600519 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Simon Glass87ff0f72017-07-04 13:31:25 -0600520 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
521 struct mmc_config *cfg = &priv->cfg;
Simon Glass3a654152017-07-04 13:31:26 -0600522 int ret;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100523
Simon Glass87ff0f72017-07-04 13:31:25 -0600524 memset(priv, '\0', sizeof(struct sunxi_mmc_priv));
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100525
526 cfg->name = "SUNXI SD/MMC";
527 cfg->ops = &sunxi_mmc_ops;
528
529 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
530 cfg->host_caps = MMC_MODE_4BIT;
Andre Przywaraf2f3a592020-12-18 22:02:11 +0000531
532 if ((IS_ENABLED(CONFIG_MACH_SUN50I) || IS_ENABLED(CONFIG_MACH_SUN8I) ||
533 IS_ENABLED(CONFIG_SUN50I_GEN_H6)) && (sdc_no == 2))
Siarhei Siamashka26c50fb2016-03-29 17:29:10 +0200534 cfg->host_caps = MMC_MODE_8BIT;
Andre Przywaraf2f3a592020-12-18 22:02:11 +0000535
Rob Herring5fd3edd2015-03-23 17:56:59 -0500536 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100537 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
538
539 cfg->f_min = 400000;
540 cfg->f_max = 52000000;
541
Hans de Goede3d1095f2014-10-31 16:55:02 +0100542 if (mmc_resource_init(sdc_no) != 0)
543 return NULL;
544
Simon Glass3a654152017-07-04 13:31:26 -0600545 /* config ahb clock */
546 debug("init mmc %d clock and io\n", sdc_no);
Jernej Skrabecd6da7ab2021-01-11 21:11:35 +0100547#if !defined(CONFIG_SUN50I_GEN_H6)
Simon Glass3a654152017-07-04 13:31:26 -0600548 setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no));
549
550#ifdef CONFIG_SUNXI_GEN_SUN6I
551 /* unassert reset */
552 setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no));
553#endif
554#if defined(CONFIG_MACH_SUN9I)
555 /* sun9i has a mmc-common module, also set the gate and reset there */
556 writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET,
557 SUNXI_MMC_COMMON_BASE + 4 * sdc_no);
558#endif
Jernej Skrabecd6da7ab2021-01-11 21:11:35 +0100559#else /* CONFIG_SUN50I_GEN_H6 */
Icenowy Zhenga838a152018-07-21 16:20:29 +0800560 setbits_le32(&ccm->sd_gate_reset, 1 << sdc_no);
561 /* unassert reset */
562 setbits_le32(&ccm->sd_gate_reset, 1 << (RESET_SHIFT + sdc_no));
563#endif
Simon Glass3a654152017-07-04 13:31:26 -0600564 ret = mmc_set_mod_clk(priv, 24000000);
565 if (ret)
566 return NULL;
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100567
Maxime Ripard0cc228e2017-08-23 13:41:33 +0200568 return mmc_create(cfg, priv);
Ian Campbellb4e9f2f2014-05-05 14:42:31 +0100569}
Andre Przywaraf5032592022-07-13 17:21:44 +0100570
571#else /* CONFIG_DM_MMC code below, as used by U-Boot proper */
Simon Glass7484ae72017-07-04 13:31:27 -0600572
573static int sunxi_mmc_set_ios(struct udevice *dev)
574{
Simon Glassfa20e932020-12-03 16:55:20 -0700575 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600576 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
577
578 return sunxi_mmc_set_ios_common(priv, &plat->mmc);
579}
580
581static int sunxi_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
582 struct mmc_data *data)
583{
Simon Glassfa20e932020-12-03 16:55:20 -0700584 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600585 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
586
587 return sunxi_mmc_send_cmd_common(priv, &plat->mmc, cmd, data);
588}
589
590static int sunxi_mmc_getcd(struct udevice *dev)
591{
Andre Przywarad8a29602021-04-21 09:33:04 +0100592 struct mmc *mmc = mmc_get_mmc_dev(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600593 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
594
Andre Przywarad8a29602021-04-21 09:33:04 +0100595 /* If polling, assume that the card is always present. */
596 if ((mmc->cfg->host_caps & MMC_CAP_NONREMOVABLE) ||
597 (mmc->cfg->host_caps & MMC_CAP_NEEDS_POLL))
598 return 1;
599
Heinrich Schuchardt8dc0a992018-02-01 23:39:19 +0100600 if (dm_gpio_is_valid(&priv->cd_gpio)) {
601 int cd_state = dm_gpio_get_value(&priv->cd_gpio);
Simon Glass7484ae72017-07-04 13:31:27 -0600602
Andre Przywarad8a29602021-04-21 09:33:04 +0100603 if (mmc->cfg->host_caps & MMC_CAP_CD_ACTIVE_HIGH)
604 return !cd_state;
605 else
606 return cd_state;
Heinrich Schuchardt8dc0a992018-02-01 23:39:19 +0100607 }
Simon Glass7484ae72017-07-04 13:31:27 -0600608 return 1;
609}
610
611static const struct dm_mmc_ops sunxi_mmc_ops = {
612 .send_cmd = sunxi_mmc_send_cmd,
613 .set_ios = sunxi_mmc_set_ios,
614 .get_cd = sunxi_mmc_getcd,
615};
616
Andre Przywara6b12ad82021-01-11 21:11:44 +0100617static unsigned get_mclk_offset(void)
618{
619 if (IS_ENABLED(CONFIG_MACH_SUN9I_A80))
620 return 0x410;
621
622 if (IS_ENABLED(CONFIG_SUN50I_GEN_H6))
623 return 0x830;
624
625 return 0x88;
626};
627
Simon Glass7484ae72017-07-04 13:31:27 -0600628static int sunxi_mmc_probe(struct udevice *dev)
629{
630 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700631 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600632 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
Andre Przywara29b533c2019-01-29 15:54:13 +0000633 struct reset_ctl_bulk reset_bulk;
634 struct clk gate_clk;
Simon Glass7484ae72017-07-04 13:31:27 -0600635 struct mmc_config *cfg = &plat->cfg;
636 struct ofnode_phandle_args args;
Andre Przywara29b533c2019-01-29 15:54:13 +0000637 u32 *ccu_reg;
Andre Przywarad8a29602021-04-21 09:33:04 +0100638 int ret;
Simon Glass7484ae72017-07-04 13:31:27 -0600639
640 cfg->name = dev->name;
Simon Glass7484ae72017-07-04 13:31:27 -0600641
642 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Andre Przywarad8a29602021-04-21 09:33:04 +0100643 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
Simon Glass7484ae72017-07-04 13:31:27 -0600644 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
645
646 cfg->f_min = 400000;
647 cfg->f_max = 52000000;
648
Andre Przywarad8a29602021-04-21 09:33:04 +0100649 ret = mmc_of_parse(dev, cfg);
650 if (ret)
651 return ret;
652
Andre Przywara70bbb412021-04-29 09:31:58 +0100653 priv->reg = dev_read_addr_ptr(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600654
655 /* We don't have a sunxi clock driver so find the clock address here */
656 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
657 1, &args);
658 if (ret)
659 return ret;
Andre Przywara70bbb412021-04-29 09:31:58 +0100660 ccu_reg = (u32 *)(uintptr_t)ofnode_get_addr(args.node);
Simon Glass7484ae72017-07-04 13:31:27 -0600661
Jagan Teki2002b752019-01-09 16:58:39 +0530662 priv->mmc_no = ((uintptr_t)priv->reg - SUNXI_MMC0_BASE) / 0x1000;
Andre Przywara6b12ad82021-01-11 21:11:44 +0100663 priv->mclkreg = (void *)ccu_reg + get_mclk_offset() + priv->mmc_no * 4;
Andre Przywara29b533c2019-01-29 15:54:13 +0000664
665 ret = clk_get_by_name(dev, "ahb", &gate_clk);
666 if (!ret)
667 clk_enable(&gate_clk);
668
669 ret = reset_get_bulk(dev, &reset_bulk);
670 if (!ret)
671 reset_deassert_bulk(&reset_bulk);
Simon Glass7484ae72017-07-04 13:31:27 -0600672
673 ret = mmc_set_mod_clk(priv, 24000000);
674 if (ret)
675 return ret;
676
677 /* This GPIO is optional */
Samuel Hollandb6b35572021-10-20 23:52:57 -0500678 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
679 GPIOD_IS_IN | GPIOD_PULL_UP);
Simon Glass7484ae72017-07-04 13:31:27 -0600680
681 upriv->mmc = &plat->mmc;
682
683 /* Reset controller */
684 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
685 udelay(1000);
686
687 return 0;
688}
689
690static int sunxi_mmc_bind(struct udevice *dev)
691{
Simon Glassfa20e932020-12-03 16:55:20 -0700692 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glass7484ae72017-07-04 13:31:27 -0600693
694 return mmc_bind(dev, &plat->mmc, &plat->cfg);
695}
696
697static const struct udevice_id sunxi_mmc_ids[] = {
Andre Przywara6b12ad82021-01-11 21:11:44 +0100698 { .compatible = "allwinner,sun4i-a10-mmc" },
699 { .compatible = "allwinner,sun5i-a13-mmc" },
700 { .compatible = "allwinner,sun7i-a20-mmc" },
701 { .compatible = "allwinner,sun8i-a83t-emmc" },
702 { .compatible = "allwinner,sun9i-a80-mmc" },
703 { .compatible = "allwinner,sun50i-a64-mmc" },
704 { .compatible = "allwinner,sun50i-a64-emmc" },
705 { .compatible = "allwinner,sun50i-h6-mmc" },
706 { .compatible = "allwinner,sun50i-h6-emmc" },
707 { .compatible = "allwinner,sun50i-a100-mmc" },
708 { .compatible = "allwinner,sun50i-a100-emmc" },
Jagan Teki2002b752019-01-09 16:58:39 +0530709 { /* sentinel */ }
Simon Glass7484ae72017-07-04 13:31:27 -0600710};
711
712U_BOOT_DRIVER(sunxi_mmc_drv) = {
713 .name = "sunxi_mmc",
714 .id = UCLASS_MMC,
715 .of_match = sunxi_mmc_ids,
716 .bind = sunxi_mmc_bind,
717 .probe = sunxi_mmc_probe,
718 .ops = &sunxi_mmc_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700719 .plat_auto = sizeof(struct sunxi_mmc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700720 .priv_auto = sizeof(struct sunxi_mmc_priv),
Simon Glass7484ae72017-07-04 13:31:27 -0600721};
722#endif