blob: c170c16d5ab73d19b64e7efca94589390ea4e1e5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbelle24ea552014-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.
Ian Campbelle24ea552014-05-05 14:42:31 +01008 */
9
10#include <common.h>
Simon Glassdd279182017-07-04 13:31:27 -060011#include <dm.h>
Hans de Goede90641f82015-04-22 17:03:17 +020012#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010014#include <malloc.h>
15#include <mmc.h>
Andre Przywarac57572e2019-01-29 15:54:13 +000016#include <clk.h>
17#include <reset.h>
Samuel Holland42508462021-09-11 16:50:47 -050018#include <asm/gpio.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010019#include <asm/io.h>
20#include <asm/arch/clock.h>
21#include <asm/arch/cpu.h>
22#include <asm/arch/mmc.h>
Simon Glassc05ed002020-05-10 11:40:11 -060023#include <linux/delay.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010024
Andre Przywaraf85c0912021-05-05 09:57:47 +010025#ifndef CCM_MMC_CTRL_MODE_SEL_NEW
26#define CCM_MMC_CTRL_MODE_SEL_NEW 0
27#endif
28
Simon Glassdd279182017-07-04 13:31:27 -060029struct sunxi_mmc_plat {
30 struct mmc_config cfg;
31 struct mmc mmc;
32};
33
Simon Glasse3c794e2017-07-04 13:31:23 -060034struct sunxi_mmc_priv {
Ian Campbelle24ea552014-05-05 14:42:31 +010035 unsigned mmc_no;
36 uint32_t *mclkreg;
Ian Campbelle24ea552014-05-05 14:42:31 +010037 unsigned fatal_err;
Simon Glassdd279182017-07-04 13:31:27 -060038 struct gpio_desc cd_gpio; /* Change Detect GPIO */
Ian Campbelle24ea552014-05-05 14:42:31 +010039 struct sunxi_mmc *reg;
40 struct mmc_config cfg;
41};
42
Simon Glassdd279182017-07-04 13:31:27 -060043#if !CONFIG_IS_ENABLED(DM_MMC)
Ian Campbelle24ea552014-05-05 14:42:31 +010044/* support 4 mmc hosts */
Simon Glasse3c794e2017-07-04 13:31:23 -060045struct sunxi_mmc_priv mmc_host[4];
Ian Campbelle24ea552014-05-05 14:42:31 +010046
Hans de Goede967325f2014-10-31 16:55:02 +010047static int sunxi_mmc_getcd_gpio(int sdc_no)
48{
49 switch (sdc_no) {
50 case 0: return sunxi_name_to_gpio(CONFIG_MMC0_CD_PIN);
51 case 1: return sunxi_name_to_gpio(CONFIG_MMC1_CD_PIN);
52 case 2: return sunxi_name_to_gpio(CONFIG_MMC2_CD_PIN);
53 case 3: return sunxi_name_to_gpio(CONFIG_MMC3_CD_PIN);
54 }
Hans de Goede90641f82015-04-22 17:03:17 +020055 return -EINVAL;
Hans de Goede967325f2014-10-31 16:55:02 +010056}
57
Ian Campbelle24ea552014-05-05 14:42:31 +010058static int mmc_resource_init(int sdc_no)
59{
Simon Glass3f5af122017-07-04 13:31:24 -060060 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
Ian Campbelle24ea552014-05-05 14:42:31 +010061 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede967325f2014-10-31 16:55:02 +010062 int cd_pin, ret = 0;
Ian Campbelle24ea552014-05-05 14:42:31 +010063
64 debug("init mmc %d resource\n", sdc_no);
65
66 switch (sdc_no) {
67 case 0:
Simon Glass3f5af122017-07-04 13:31:24 -060068 priv->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE;
69 priv->mclkreg = &ccm->sd0_clk_cfg;
Ian Campbelle24ea552014-05-05 14:42:31 +010070 break;
71 case 1:
Simon Glass3f5af122017-07-04 13:31:24 -060072 priv->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE;
73 priv->mclkreg = &ccm->sd1_clk_cfg;
Ian Campbelle24ea552014-05-05 14:42:31 +010074 break;
75 case 2:
Simon Glass3f5af122017-07-04 13:31:24 -060076 priv->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE;
77 priv->mclkreg = &ccm->sd2_clk_cfg;
Ian Campbelle24ea552014-05-05 14:42:31 +010078 break;
Icenowy Zheng42956f12018-07-21 16:20:29 +080079#ifdef SUNXI_MMC3_BASE
Ian Campbelle24ea552014-05-05 14:42:31 +010080 case 3:
Simon Glass3f5af122017-07-04 13:31:24 -060081 priv->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE;
82 priv->mclkreg = &ccm->sd3_clk_cfg;
Ian Campbelle24ea552014-05-05 14:42:31 +010083 break;
Icenowy Zheng42956f12018-07-21 16:20:29 +080084#endif
Ian Campbelle24ea552014-05-05 14:42:31 +010085 default:
86 printf("Wrong mmc number %d\n", sdc_no);
87 return -1;
88 }
Simon Glass3f5af122017-07-04 13:31:24 -060089 priv->mmc_no = sdc_no;
Ian Campbelle24ea552014-05-05 14:42:31 +010090
Hans de Goede967325f2014-10-31 16:55:02 +010091 cd_pin = sunxi_mmc_getcd_gpio(sdc_no);
Hans de Goede90641f82015-04-22 17:03:17 +020092 if (cd_pin >= 0) {
Hans de Goede967325f2014-10-31 16:55:02 +010093 ret = gpio_request(cd_pin, "mmc_cd");
Hans de Goede1c09fa32015-05-30 16:39:10 +020094 if (!ret) {
95 sunxi_gpio_set_pull(cd_pin, SUNXI_GPIO_PULL_UP);
Axel Linb0c4ae12014-12-20 11:41:25 +080096 ret = gpio_direction_input(cd_pin);
Hans de Goede1c09fa32015-05-30 16:39:10 +020097 }
Axel Linb0c4ae12014-12-20 11:41:25 +080098 }
Hans de Goede967325f2014-10-31 16:55:02 +010099
100 return ret;
Ian Campbelle24ea552014-05-05 14:42:31 +0100101}
Simon Glassdd279182017-07-04 13:31:27 -0600102#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100103
Andre Przywarab5dd39c2021-05-05 10:06:24 +0100104/*
105 * All A64 and later MMC controllers feature auto-calibration. This would
106 * normally be detected via the compatible string, but we need something
107 * which works in the SPL as well.
108 */
109static bool sunxi_mmc_can_calibrate(void)
110{
111 return IS_ENABLED(CONFIG_MACH_SUN50I) ||
112 IS_ENABLED(CONFIG_MACH_SUN50I_H5) ||
113 IS_ENABLED(CONFIG_SUN50I_GEN_H6) ||
114 IS_ENABLED(CONFIG_MACH_SUN8I_R40);
115}
116
Simon Glass3f5af122017-07-04 13:31:24 -0600117static int mmc_set_mod_clk(struct sunxi_mmc_priv *priv, unsigned int hz)
Hans de Goedefc3a8322014-12-07 20:55:10 +0100118{
119 unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly;
Andre Przywaraf85c0912021-05-05 09:57:47 +0100120 bool new_mode = IS_ENABLED(CONFIG_MMC_SUNXI_HAS_NEW_MODE);
Maxime Ripardde9b1772017-08-23 12:03:41 +0200121 u32 val = 0;
122
Vasily Khoruzhick0e21a2f2018-11-09 20:41:46 -0800123 /* A83T support new mode only on eMMC */
124 if (IS_ENABLED(CONFIG_MACH_SUN8I_A83T) && priv->mmc_no != 2)
125 new_mode = false;
Maxime Ripardde9b1772017-08-23 12:03:41 +0200126
Hans de Goedefc3a8322014-12-07 20:55:10 +0100127 if (hz <= 24000000) {
128 pll = CCM_MMC_CTRL_OSCM24;
129 pll_hz = 24000000;
130 } else {
Hans de Goededaf22632015-01-14 19:05:03 +0100131#ifdef CONFIG_MACH_SUN9I
132 pll = CCM_MMC_CTRL_PLL_PERIPH0;
133 pll_hz = clock_get_pll4_periph0();
134#else
Andre Przywara937ee312021-05-05 09:57:47 +0100135 /*
136 * SoCs since the A64 (H5, H6, H616) actually use the doubled
137 * rate of PLL6/PERIPH0 as an input clock, but compensate for
138 * that with a fixed post-divider of 2 in the mod clock.
139 * This cancels each other out, so for simplicity we just
140 * pretend it's always PLL6 without a post divider here.
141 */
Hans de Goedefc3a8322014-12-07 20:55:10 +0100142 pll = CCM_MMC_CTRL_PLL6;
143 pll_hz = clock_get_pll6();
Hans de Goededaf22632015-01-14 19:05:03 +0100144#endif
Hans de Goedefc3a8322014-12-07 20:55:10 +0100145 }
146
147 div = pll_hz / hz;
148 if (pll_hz % hz)
149 div++;
150
151 n = 0;
152 while (div > 16) {
153 n++;
154 div = (div + 1) / 2;
155 }
156
157 if (n > 3) {
Simon Glass3f5af122017-07-04 13:31:24 -0600158 printf("mmc %u error cannot set clock to %u\n", priv->mmc_no,
159 hz);
Hans de Goedefc3a8322014-12-07 20:55:10 +0100160 return -1;
161 }
162
163 /* determine delays */
164 if (hz <= 400000) {
165 oclk_dly = 0;
Hans de Goedebe909742015-09-23 16:13:10 +0200166 sclk_dly = 0;
Hans de Goedefc3a8322014-12-07 20:55:10 +0100167 } else if (hz <= 25000000) {
168 oclk_dly = 0;
169 sclk_dly = 5;
Hans de Goedefc3a8322014-12-07 20:55:10 +0100170 } else {
Andre Przywaraf4826fb2020-12-18 22:02:11 +0000171 if (IS_ENABLED(CONFIG_MACH_SUN9I)) {
172 if (hz <= 52000000)
173 oclk_dly = 5;
174 else
175 oclk_dly = 2;
176 } else {
177 if (hz <= 52000000)
178 oclk_dly = 3;
179 else
180 oclk_dly = 1;
181 }
Hans de Goedefc3a8322014-12-07 20:55:10 +0100182 sclk_dly = 4;
183 }
184
Maxime Ripardde9b1772017-08-23 12:03:41 +0200185 if (new_mode) {
Andre Przywaraf85c0912021-05-05 09:57:47 +0100186 val |= CCM_MMC_CTRL_MODE_SEL_NEW;
Chen-Yu Tsai8a647fc2017-08-31 21:57:48 +0800187 setbits_le32(&priv->reg->ntsr, SUNXI_MMC_NTSR_MODE_SEL_NEW);
Andre Przywarab5dd39c2021-05-05 10:06:24 +0100188 }
189
190 if (!sunxi_mmc_can_calibrate()) {
Vasily Khoruzhick20940ef2018-11-05 20:24:28 -0800191 /*
192 * Use hardcoded delay values if controller doesn't support
193 * calibration
194 */
Maxime Ripardde9b1772017-08-23 12:03:41 +0200195 val = CCM_MMC_CTRL_OCLK_DLY(oclk_dly) |
196 CCM_MMC_CTRL_SCLK_DLY(sclk_dly);
197 }
198
199 writel(CCM_MMC_CTRL_ENABLE| pll | CCM_MMC_CTRL_N(n) |
200 CCM_MMC_CTRL_M(div) | val, priv->mclkreg);
Hans de Goedefc3a8322014-12-07 20:55:10 +0100201
202 debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n",
Simon Glass3f5af122017-07-04 13:31:24 -0600203 priv->mmc_no, hz, pll_hz, 1u << n, div, pll_hz / (1u << n) / div);
Hans de Goedefc3a8322014-12-07 20:55:10 +0100204
205 return 0;
206}
207
Simon Glass034e2262017-07-04 13:31:25 -0600208static int mmc_update_clk(struct sunxi_mmc_priv *priv)
Ian Campbelle24ea552014-05-05 14:42:31 +0100209{
Ian Campbelle24ea552014-05-05 14:42:31 +0100210 unsigned int cmd;
211 unsigned timeout_msecs = 2000;
Philipp Tomsich5ff8e542018-03-21 12:18:58 +0100212 unsigned long start = get_timer(0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100213
214 cmd = SUNXI_MMC_CMD_START |
215 SUNXI_MMC_CMD_UPCLK_ONLY |
216 SUNXI_MMC_CMD_WAIT_PRE_OVER;
Philipp Tomsich5ff8e542018-03-21 12:18:58 +0100217
Simon Glass3f5af122017-07-04 13:31:24 -0600218 writel(cmd, &priv->reg->cmd);
219 while (readl(&priv->reg->cmd) & SUNXI_MMC_CMD_START) {
Philipp Tomsich5ff8e542018-03-21 12:18:58 +0100220 if (get_timer(start) > timeout_msecs)
Ian Campbelle24ea552014-05-05 14:42:31 +0100221 return -1;
Ian Campbelle24ea552014-05-05 14:42:31 +0100222 }
223
224 /* clock update sets various irq status bits, clear these */
Simon Glass3f5af122017-07-04 13:31:24 -0600225 writel(readl(&priv->reg->rint), &priv->reg->rint);
Ian Campbelle24ea552014-05-05 14:42:31 +0100226
227 return 0;
228}
229
Simon Glass034e2262017-07-04 13:31:25 -0600230static int mmc_config_clock(struct sunxi_mmc_priv *priv, struct mmc *mmc)
Ian Campbelle24ea552014-05-05 14:42:31 +0100231{
Simon Glass3f5af122017-07-04 13:31:24 -0600232 unsigned rval = readl(&priv->reg->clkcr);
Ian Campbelle24ea552014-05-05 14:42:31 +0100233
234 /* Disable Clock */
235 rval &= ~SUNXI_MMC_CLK_ENABLE;
Simon Glass3f5af122017-07-04 13:31:24 -0600236 writel(rval, &priv->reg->clkcr);
Simon Glass034e2262017-07-04 13:31:25 -0600237 if (mmc_update_clk(priv))
Ian Campbelle24ea552014-05-05 14:42:31 +0100238 return -1;
239
Hans de Goedefc3a8322014-12-07 20:55:10 +0100240 /* Set mod_clk to new rate */
Simon Glass3f5af122017-07-04 13:31:24 -0600241 if (mmc_set_mod_clk(priv, mmc->clock))
Ian Campbelle24ea552014-05-05 14:42:31 +0100242 return -1;
Hans de Goedefc3a8322014-12-07 20:55:10 +0100243
244 /* Clear internal divider */
245 rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK;
Simon Glass3f5af122017-07-04 13:31:24 -0600246 writel(rval, &priv->reg->clkcr);
Hans de Goedefc3a8322014-12-07 20:55:10 +0100247
Andre Przywarab5dd39c2021-05-05 10:06:24 +0100248#if defined(CONFIG_SUNXI_GEN_SUN6I) || defined(CONFIG_SUN50I_GEN_H6)
Vasily Khoruzhick20940ef2018-11-05 20:24:28 -0800249 /* A64 supports calibration of delays on MMC controller and we
250 * have to set delay of zero before starting calibration.
251 * Allwinner BSP driver sets a delay only in the case of
252 * using HS400 which is not supported by mainline U-Boot or
253 * Linux at the moment
254 */
Andre Przywarab5dd39c2021-05-05 10:06:24 +0100255 if (sunxi_mmc_can_calibrate())
256 writel(SUNXI_MMC_CAL_DL_SW_EN, &priv->reg->samp_dl);
Vasily Khoruzhick20940ef2018-11-05 20:24:28 -0800257#endif
258
Ian Campbelle24ea552014-05-05 14:42:31 +0100259 /* Re-enable Clock */
260 rval |= SUNXI_MMC_CLK_ENABLE;
Simon Glass3f5af122017-07-04 13:31:24 -0600261 writel(rval, &priv->reg->clkcr);
Simon Glass034e2262017-07-04 13:31:25 -0600262 if (mmc_update_clk(priv))
Ian Campbelle24ea552014-05-05 14:42:31 +0100263 return -1;
264
265 return 0;
266}
267
Simon Glass034e2262017-07-04 13:31:25 -0600268static int sunxi_mmc_set_ios_common(struct sunxi_mmc_priv *priv,
269 struct mmc *mmc)
Ian Campbelle24ea552014-05-05 14:42:31 +0100270{
Hans de Goedefc3a8322014-12-07 20:55:10 +0100271 debug("set ios: bus_width: %x, clock: %d\n",
272 mmc->bus_width, mmc->clock);
Ian Campbelle24ea552014-05-05 14:42:31 +0100273
274 /* Change clock first */
Simon Glass034e2262017-07-04 13:31:25 -0600275 if (mmc->clock && mmc_config_clock(priv, mmc) != 0) {
Simon Glass3f5af122017-07-04 13:31:24 -0600276 priv->fatal_err = 1;
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900277 return -EINVAL;
Ian Campbelle24ea552014-05-05 14:42:31 +0100278 }
279
280 /* Change bus width */
281 if (mmc->bus_width == 8)
Simon Glass3f5af122017-07-04 13:31:24 -0600282 writel(0x2, &priv->reg->width);
Ian Campbelle24ea552014-05-05 14:42:31 +0100283 else if (mmc->bus_width == 4)
Simon Glass3f5af122017-07-04 13:31:24 -0600284 writel(0x1, &priv->reg->width);
Ian Campbelle24ea552014-05-05 14:42:31 +0100285 else
Simon Glass3f5af122017-07-04 13:31:24 -0600286 writel(0x0, &priv->reg->width);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900287
288 return 0;
Ian Campbelle24ea552014-05-05 14:42:31 +0100289}
290
Simon Glassdd279182017-07-04 13:31:27 -0600291#if !CONFIG_IS_ENABLED(DM_MMC)
Siarhei Siamashka5abdb152015-02-01 00:42:14 +0200292static int sunxi_mmc_core_init(struct mmc *mmc)
Ian Campbelle24ea552014-05-05 14:42:31 +0100293{
Simon Glass3f5af122017-07-04 13:31:24 -0600294 struct sunxi_mmc_priv *priv = mmc->priv;
Ian Campbelle24ea552014-05-05 14:42:31 +0100295
296 /* Reset controller */
Simon Glass3f5af122017-07-04 13:31:24 -0600297 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
Hans de Goedeb6ae6762014-06-09 11:36:55 +0200298 udelay(1000);
Ian Campbelle24ea552014-05-05 14:42:31 +0100299
300 return 0;
301}
Simon Glassdd279182017-07-04 13:31:27 -0600302#endif
Ian Campbelle24ea552014-05-05 14:42:31 +0100303
Simon Glass034e2262017-07-04 13:31:25 -0600304static int mmc_trans_data_by_cpu(struct sunxi_mmc_priv *priv, struct mmc *mmc,
305 struct mmc_data *data)
Ian Campbelle24ea552014-05-05 14:42:31 +0100306{
Ian Campbelle24ea552014-05-05 14:42:31 +0100307 const int reading = !!(data->flags & MMC_DATA_READ);
308 const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY :
309 SUNXI_MMC_STATUS_FIFO_FULL;
310 unsigned i;
Ian Campbelle24ea552014-05-05 14:42:31 +0100311 unsigned *buff = (unsigned int *)(reading ? data->dest : data->src);
Andre Przywara9faae542021-05-05 11:33:40 +0100312 unsigned word_cnt = (data->blocksize * data->blocks) >> 2;
313 unsigned timeout_msecs = word_cnt >> 6;
314 uint32_t status;
Philipp Tomsich5ff8e542018-03-21 12:18:58 +0100315 unsigned long start;
316
317 if (timeout_msecs < 2000)
318 timeout_msecs = 2000;
Ian Campbelle24ea552014-05-05 14:42:31 +0100319
Hans de Goedeb6ae6762014-06-09 11:36:55 +0200320 /* Always read / write data through the CPU */
Simon Glass3f5af122017-07-04 13:31:24 -0600321 setbits_le32(&priv->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB);
Hans de Goedeb6ae6762014-06-09 11:36:55 +0200322
Philipp Tomsich5ff8e542018-03-21 12:18:58 +0100323 start = get_timer(0);
324
Andre Przywara9faae542021-05-05 11:33:40 +0100325 for (i = 0; i < word_cnt;) {
326 unsigned int in_fifo;
327
328 while ((status = readl(&priv->reg->status)) & status_bit) {
Philipp Tomsich5ff8e542018-03-21 12:18:58 +0100329 if (get_timer(start) > timeout_msecs)
Ian Campbelle24ea552014-05-05 14:42:31 +0100330 return -1;
Ian Campbelle24ea552014-05-05 14:42:31 +0100331 }
332
Andre Przywara9faae542021-05-05 11:33:40 +0100333 /*
334 * For writing we do not easily know the FIFO size, so have
335 * to check the FIFO status after every word written.
336 * TODO: For optimisation we could work out a minimum FIFO
337 * size across all SoCs, and use that together with the current
338 * fill level to write chunks of words.
339 */
340 if (!reading) {
341 writel(buff[i++], &priv->reg->fifo);
342 continue;
343 }
344
345 /*
346 * The status register holds the current FIFO level, so we
347 * can be sure to collect as many words from the FIFO
348 * register without checking the status register after every
349 * read. That saves half of the costly MMIO reads, effectively
350 * doubling the read performance.
Andre Przywara0b508ca2021-09-03 16:49:16 +0100351 * Some SoCs (A20) report a level of 0 if the FIFO is
352 * completely full (value masked out?). Use a safe minimal
353 * FIFO size in this case.
Andre Przywara9faae542021-05-05 11:33:40 +0100354 */
Andre Przywara0b508ca2021-09-03 16:49:16 +0100355 in_fifo = SUNXI_MMC_STATUS_FIFO_LEVEL(status);
356 if (in_fifo == 0 && (status & SUNXI_MMC_STATUS_FIFO_FULL))
357 in_fifo = 32;
358 for (; in_fifo > 0; in_fifo--)
Andre Przywara9faae542021-05-05 11:33:40 +0100359 buff[i++] = readl_relaxed(&priv->reg->fifo);
360 dmb();
Ian Campbelle24ea552014-05-05 14:42:31 +0100361 }
362
363 return 0;
364}
365
Simon Glass034e2262017-07-04 13:31:25 -0600366static int mmc_rint_wait(struct sunxi_mmc_priv *priv, struct mmc *mmc,
367 uint timeout_msecs, uint done_bit, const char *what)
Ian Campbelle24ea552014-05-05 14:42:31 +0100368{
Ian Campbelle24ea552014-05-05 14:42:31 +0100369 unsigned int status;
Philipp Tomsich5ff8e542018-03-21 12:18:58 +0100370 unsigned long start = get_timer(0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100371
372 do {
Simon Glass3f5af122017-07-04 13:31:24 -0600373 status = readl(&priv->reg->rint);
Philipp Tomsich5ff8e542018-03-21 12:18:58 +0100374 if ((get_timer(start) > timeout_msecs) ||
Ian Campbelle24ea552014-05-05 14:42:31 +0100375 (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) {
376 debug("%s timeout %x\n", what,
377 status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900378 return -ETIMEDOUT;
Ian Campbelle24ea552014-05-05 14:42:31 +0100379 }
Ian Campbelle24ea552014-05-05 14:42:31 +0100380 } while (!(status & done_bit));
381
382 return 0;
383}
384
Simon Glass034e2262017-07-04 13:31:25 -0600385static int sunxi_mmc_send_cmd_common(struct sunxi_mmc_priv *priv,
386 struct mmc *mmc, struct mmc_cmd *cmd,
387 struct mmc_data *data)
Ian Campbelle24ea552014-05-05 14:42:31 +0100388{
Ian Campbelle24ea552014-05-05 14:42:31 +0100389 unsigned int cmdval = SUNXI_MMC_CMD_START;
390 unsigned int timeout_msecs;
391 int error = 0;
392 unsigned int status = 0;
Ian Campbelle24ea552014-05-05 14:42:31 +0100393 unsigned int bytecnt = 0;
394
Simon Glass3f5af122017-07-04 13:31:24 -0600395 if (priv->fatal_err)
Ian Campbelle24ea552014-05-05 14:42:31 +0100396 return -1;
397 if (cmd->resp_type & MMC_RSP_BUSY)
398 debug("mmc cmd %d check rsp busy\n", cmd->cmdidx);
399 if (cmd->cmdidx == 12)
400 return 0;
401
402 if (!cmd->cmdidx)
403 cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
404 if (cmd->resp_type & MMC_RSP_PRESENT)
405 cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE;
406 if (cmd->resp_type & MMC_RSP_136)
407 cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE;
408 if (cmd->resp_type & MMC_RSP_CRC)
409 cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC;
410
411 if (data) {
Alexander Graf0ea5a042016-03-29 17:29:09 +0200412 if ((u32)(long)data->dest & 0x3) {
Ian Campbelle24ea552014-05-05 14:42:31 +0100413 error = -1;
414 goto out;
415 }
416
417 cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER;
418 if (data->flags & MMC_DATA_WRITE)
419 cmdval |= SUNXI_MMC_CMD_WRITE;
420 if (data->blocks > 1)
421 cmdval |= SUNXI_MMC_CMD_AUTO_STOP;
Simon Glass3f5af122017-07-04 13:31:24 -0600422 writel(data->blocksize, &priv->reg->blksz);
423 writel(data->blocks * data->blocksize, &priv->reg->bytecnt);
Ian Campbelle24ea552014-05-05 14:42:31 +0100424 }
425
Simon Glass3f5af122017-07-04 13:31:24 -0600426 debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", priv->mmc_no,
Ian Campbelle24ea552014-05-05 14:42:31 +0100427 cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg);
Simon Glass3f5af122017-07-04 13:31:24 -0600428 writel(cmd->cmdarg, &priv->reg->arg);
Ian Campbelle24ea552014-05-05 14:42:31 +0100429
430 if (!data)
Simon Glass3f5af122017-07-04 13:31:24 -0600431 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
Ian Campbelle24ea552014-05-05 14:42:31 +0100432
433 /*
434 * transfer data and check status
435 * STATREG[2] : FIFO empty
436 * STATREG[3] : FIFO full
437 */
438 if (data) {
439 int ret = 0;
440
441 bytecnt = data->blocksize * data->blocks;
442 debug("trans data %d bytes\n", bytecnt);
Simon Glass3f5af122017-07-04 13:31:24 -0600443 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
Simon Glass034e2262017-07-04 13:31:25 -0600444 ret = mmc_trans_data_by_cpu(priv, mmc, data);
Ian Campbelle24ea552014-05-05 14:42:31 +0100445 if (ret) {
Simon Glass3f5af122017-07-04 13:31:24 -0600446 error = readl(&priv->reg->rint) &
Ian Campbelle24ea552014-05-05 14:42:31 +0100447 SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT;
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900448 error = -ETIMEDOUT;
Ian Campbelle24ea552014-05-05 14:42:31 +0100449 goto out;
450 }
451 }
452
Simon Glass034e2262017-07-04 13:31:25 -0600453 error = mmc_rint_wait(priv, mmc, 1000, SUNXI_MMC_RINT_COMMAND_DONE,
454 "cmd");
Ian Campbelle24ea552014-05-05 14:42:31 +0100455 if (error)
456 goto out;
457
458 if (data) {
Hans de Goedeb6ae6762014-06-09 11:36:55 +0200459 timeout_msecs = 120;
Ian Campbelle24ea552014-05-05 14:42:31 +0100460 debug("cacl timeout %x msec\n", timeout_msecs);
Simon Glass034e2262017-07-04 13:31:25 -0600461 error = mmc_rint_wait(priv, mmc, timeout_msecs,
Ian Campbelle24ea552014-05-05 14:42:31 +0100462 data->blocks > 1 ?
463 SUNXI_MMC_RINT_AUTO_COMMAND_DONE :
464 SUNXI_MMC_RINT_DATA_OVER,
465 "data");
466 if (error)
467 goto out;
468 }
469
470 if (cmd->resp_type & MMC_RSP_BUSY) {
Philipp Tomsich5ff8e542018-03-21 12:18:58 +0100471 unsigned long start = get_timer(0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100472 timeout_msecs = 2000;
Philipp Tomsich5ff8e542018-03-21 12:18:58 +0100473
Ian Campbelle24ea552014-05-05 14:42:31 +0100474 do {
Simon Glass3f5af122017-07-04 13:31:24 -0600475 status = readl(&priv->reg->status);
Philipp Tomsich5ff8e542018-03-21 12:18:58 +0100476 if (get_timer(start) > timeout_msecs) {
Ian Campbelle24ea552014-05-05 14:42:31 +0100477 debug("busy timeout\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900478 error = -ETIMEDOUT;
Ian Campbelle24ea552014-05-05 14:42:31 +0100479 goto out;
480 }
Ian Campbelle24ea552014-05-05 14:42:31 +0100481 } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY);
482 }
483
484 if (cmd->resp_type & MMC_RSP_136) {
Simon Glass3f5af122017-07-04 13:31:24 -0600485 cmd->response[0] = readl(&priv->reg->resp3);
486 cmd->response[1] = readl(&priv->reg->resp2);
487 cmd->response[2] = readl(&priv->reg->resp1);
488 cmd->response[3] = readl(&priv->reg->resp0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100489 debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
490 cmd->response[3], cmd->response[2],
491 cmd->response[1], cmd->response[0]);
492 } else {
Simon Glass3f5af122017-07-04 13:31:24 -0600493 cmd->response[0] = readl(&priv->reg->resp0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100494 debug("mmc resp 0x%08x\n", cmd->response[0]);
495 }
496out:
Ian Campbelle24ea552014-05-05 14:42:31 +0100497 if (error < 0) {
Simon Glass3f5af122017-07-04 13:31:24 -0600498 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
Simon Glass034e2262017-07-04 13:31:25 -0600499 mmc_update_clk(priv);
Ian Campbelle24ea552014-05-05 14:42:31 +0100500 }
Simon Glass3f5af122017-07-04 13:31:24 -0600501 writel(0xffffffff, &priv->reg->rint);
502 writel(readl(&priv->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET,
503 &priv->reg->gctrl);
Ian Campbelle24ea552014-05-05 14:42:31 +0100504
505 return error;
506}
507
Simon Glassdd279182017-07-04 13:31:27 -0600508#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass034e2262017-07-04 13:31:25 -0600509static int sunxi_mmc_set_ios_legacy(struct mmc *mmc)
510{
511 struct sunxi_mmc_priv *priv = mmc->priv;
512
513 return sunxi_mmc_set_ios_common(priv, mmc);
514}
515
516static int sunxi_mmc_send_cmd_legacy(struct mmc *mmc, struct mmc_cmd *cmd,
517 struct mmc_data *data)
518{
519 struct sunxi_mmc_priv *priv = mmc->priv;
520
521 return sunxi_mmc_send_cmd_common(priv, mmc, cmd, data);
522}
523
524static int sunxi_mmc_getcd_legacy(struct mmc *mmc)
Hans de Goedecd821132014-10-02 20:29:26 +0200525{
Simon Glass3f5af122017-07-04 13:31:24 -0600526 struct sunxi_mmc_priv *priv = mmc->priv;
Hans de Goede967325f2014-10-31 16:55:02 +0100527 int cd_pin;
Hans de Goedecd821132014-10-02 20:29:26 +0200528
Simon Glass3f5af122017-07-04 13:31:24 -0600529 cd_pin = sunxi_mmc_getcd_gpio(priv->mmc_no);
Hans de Goede90641f82015-04-22 17:03:17 +0200530 if (cd_pin < 0)
Hans de Goedecd821132014-10-02 20:29:26 +0200531 return 1;
532
Axel Linb0c4ae12014-12-20 11:41:25 +0800533 return !gpio_get_value(cd_pin);
Hans de Goedecd821132014-10-02 20:29:26 +0200534}
535
Ian Campbelle24ea552014-05-05 14:42:31 +0100536static const struct mmc_ops sunxi_mmc_ops = {
Simon Glass034e2262017-07-04 13:31:25 -0600537 .send_cmd = sunxi_mmc_send_cmd_legacy,
538 .set_ios = sunxi_mmc_set_ios_legacy,
Siarhei Siamashka5abdb152015-02-01 00:42:14 +0200539 .init = sunxi_mmc_core_init,
Simon Glass034e2262017-07-04 13:31:25 -0600540 .getcd = sunxi_mmc_getcd_legacy,
Ian Campbelle24ea552014-05-05 14:42:31 +0100541};
542
Hans de Goedee79c7c82014-10-02 21:13:54 +0200543struct mmc *sunxi_mmc_init(int sdc_no)
Ian Campbelle24ea552014-05-05 14:42:31 +0100544{
Simon Glassec73d962017-07-04 13:31:26 -0600545 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Simon Glass034e2262017-07-04 13:31:25 -0600546 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
547 struct mmc_config *cfg = &priv->cfg;
Simon Glassec73d962017-07-04 13:31:26 -0600548 int ret;
Ian Campbelle24ea552014-05-05 14:42:31 +0100549
Simon Glass034e2262017-07-04 13:31:25 -0600550 memset(priv, '\0', sizeof(struct sunxi_mmc_priv));
Ian Campbelle24ea552014-05-05 14:42:31 +0100551
552 cfg->name = "SUNXI SD/MMC";
553 cfg->ops = &sunxi_mmc_ops;
554
555 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
556 cfg->host_caps = MMC_MODE_4BIT;
Andre Przywaraf4826fb2020-12-18 22:02:11 +0000557
558 if ((IS_ENABLED(CONFIG_MACH_SUN50I) || IS_ENABLED(CONFIG_MACH_SUN8I) ||
559 IS_ENABLED(CONFIG_SUN50I_GEN_H6)) && (sdc_no == 2))
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200560 cfg->host_caps = MMC_MODE_8BIT;
Andre Przywaraf4826fb2020-12-18 22:02:11 +0000561
Rob Herring5a203972015-03-23 17:56:59 -0500562 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Ian Campbelle24ea552014-05-05 14:42:31 +0100563 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
564
565 cfg->f_min = 400000;
566 cfg->f_max = 52000000;
567
Hans de Goede967325f2014-10-31 16:55:02 +0100568 if (mmc_resource_init(sdc_no) != 0)
569 return NULL;
570
Simon Glassec73d962017-07-04 13:31:26 -0600571 /* config ahb clock */
572 debug("init mmc %d clock and io\n", sdc_no);
Jernej Skrabecaaebb902021-01-11 21:11:35 +0100573#if !defined(CONFIG_SUN50I_GEN_H6)
Simon Glassec73d962017-07-04 13:31:26 -0600574 setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no));
575
576#ifdef CONFIG_SUNXI_GEN_SUN6I
577 /* unassert reset */
578 setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no));
579#endif
580#if defined(CONFIG_MACH_SUN9I)
581 /* sun9i has a mmc-common module, also set the gate and reset there */
582 writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET,
583 SUNXI_MMC_COMMON_BASE + 4 * sdc_no);
584#endif
Jernej Skrabecaaebb902021-01-11 21:11:35 +0100585#else /* CONFIG_SUN50I_GEN_H6 */
Icenowy Zheng42956f12018-07-21 16:20:29 +0800586 setbits_le32(&ccm->sd_gate_reset, 1 << sdc_no);
587 /* unassert reset */
588 setbits_le32(&ccm->sd_gate_reset, 1 << (RESET_SHIFT + sdc_no));
589#endif
Simon Glassec73d962017-07-04 13:31:26 -0600590 ret = mmc_set_mod_clk(priv, 24000000);
591 if (ret)
592 return NULL;
Ian Campbelle24ea552014-05-05 14:42:31 +0100593
Maxime Ripardead36972017-08-23 13:41:33 +0200594 return mmc_create(cfg, priv);
Ian Campbelle24ea552014-05-05 14:42:31 +0100595}
Simon Glassdd279182017-07-04 13:31:27 -0600596#else
597
598static int sunxi_mmc_set_ios(struct udevice *dev)
599{
Simon Glassc69cda22020-12-03 16:55:20 -0700600 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glassdd279182017-07-04 13:31:27 -0600601 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
602
603 return sunxi_mmc_set_ios_common(priv, &plat->mmc);
604}
605
606static int sunxi_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
607 struct mmc_data *data)
608{
Simon Glassc69cda22020-12-03 16:55:20 -0700609 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glassdd279182017-07-04 13:31:27 -0600610 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
611
612 return sunxi_mmc_send_cmd_common(priv, &plat->mmc, cmd, data);
613}
614
615static int sunxi_mmc_getcd(struct udevice *dev)
616{
Andre Przywaraac62dad2021-04-21 09:33:04 +0100617 struct mmc *mmc = mmc_get_mmc_dev(dev);
Simon Glassdd279182017-07-04 13:31:27 -0600618 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
619
Andre Przywaraac62dad2021-04-21 09:33:04 +0100620 /* If polling, assume that the card is always present. */
621 if ((mmc->cfg->host_caps & MMC_CAP_NONREMOVABLE) ||
622 (mmc->cfg->host_caps & MMC_CAP_NEEDS_POLL))
623 return 1;
624
Heinrich Schuchardt8be4e612018-02-01 23:39:19 +0100625 if (dm_gpio_is_valid(&priv->cd_gpio)) {
626 int cd_state = dm_gpio_get_value(&priv->cd_gpio);
Simon Glassdd279182017-07-04 13:31:27 -0600627
Andre Przywaraac62dad2021-04-21 09:33:04 +0100628 if (mmc->cfg->host_caps & MMC_CAP_CD_ACTIVE_HIGH)
629 return !cd_state;
630 else
631 return cd_state;
Heinrich Schuchardt8be4e612018-02-01 23:39:19 +0100632 }
Simon Glassdd279182017-07-04 13:31:27 -0600633 return 1;
634}
635
636static const struct dm_mmc_ops sunxi_mmc_ops = {
637 .send_cmd = sunxi_mmc_send_cmd,
638 .set_ios = sunxi_mmc_set_ios,
639 .get_cd = sunxi_mmc_getcd,
640};
641
Andre Przywara0237b302021-01-11 21:11:44 +0100642static unsigned get_mclk_offset(void)
643{
644 if (IS_ENABLED(CONFIG_MACH_SUN9I_A80))
645 return 0x410;
646
647 if (IS_ENABLED(CONFIG_SUN50I_GEN_H6))
648 return 0x830;
649
650 return 0x88;
651};
652
Simon Glassdd279182017-07-04 13:31:27 -0600653static int sunxi_mmc_probe(struct udevice *dev)
654{
655 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700656 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glassdd279182017-07-04 13:31:27 -0600657 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
Andre Przywarac57572e2019-01-29 15:54:13 +0000658 struct reset_ctl_bulk reset_bulk;
659 struct clk gate_clk;
Simon Glassdd279182017-07-04 13:31:27 -0600660 struct mmc_config *cfg = &plat->cfg;
661 struct ofnode_phandle_args args;
Andre Przywarac57572e2019-01-29 15:54:13 +0000662 u32 *ccu_reg;
Andre Przywaraac62dad2021-04-21 09:33:04 +0100663 int ret;
Simon Glassdd279182017-07-04 13:31:27 -0600664
665 cfg->name = dev->name;
Simon Glassdd279182017-07-04 13:31:27 -0600666
667 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Andre Przywaraac62dad2021-04-21 09:33:04 +0100668 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
Simon Glassdd279182017-07-04 13:31:27 -0600669 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
670
671 cfg->f_min = 400000;
672 cfg->f_max = 52000000;
673
Andre Przywaraac62dad2021-04-21 09:33:04 +0100674 ret = mmc_of_parse(dev, cfg);
675 if (ret)
676 return ret;
677
Andre Przywaraca496ba2021-04-29 09:31:58 +0100678 priv->reg = dev_read_addr_ptr(dev);
Simon Glassdd279182017-07-04 13:31:27 -0600679
680 /* We don't have a sunxi clock driver so find the clock address here */
681 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
682 1, &args);
683 if (ret)
684 return ret;
Andre Przywaraca496ba2021-04-29 09:31:58 +0100685 ccu_reg = (u32 *)(uintptr_t)ofnode_get_addr(args.node);
Simon Glassdd279182017-07-04 13:31:27 -0600686
Jagan Tekie8f37f42019-01-09 16:58:39 +0530687 priv->mmc_no = ((uintptr_t)priv->reg - SUNXI_MMC0_BASE) / 0x1000;
Andre Przywara0237b302021-01-11 21:11:44 +0100688 priv->mclkreg = (void *)ccu_reg + get_mclk_offset() + priv->mmc_no * 4;
Andre Przywarac57572e2019-01-29 15:54:13 +0000689
690 ret = clk_get_by_name(dev, "ahb", &gate_clk);
691 if (!ret)
692 clk_enable(&gate_clk);
693
694 ret = reset_get_bulk(dev, &reset_bulk);
695 if (!ret)
696 reset_deassert_bulk(&reset_bulk);
Simon Glassdd279182017-07-04 13:31:27 -0600697
698 ret = mmc_set_mod_clk(priv, 24000000);
699 if (ret)
700 return ret;
701
702 /* This GPIO is optional */
Andre Przywaraac62dad2021-04-21 09:33:04 +0100703 if (!gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
Simon Glassdd279182017-07-04 13:31:27 -0600704 GPIOD_IS_IN)) {
705 int cd_pin = gpio_get_number(&priv->cd_gpio);
706
707 sunxi_gpio_set_pull(cd_pin, SUNXI_GPIO_PULL_UP);
708 }
709
710 upriv->mmc = &plat->mmc;
711
712 /* Reset controller */
713 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
714 udelay(1000);
715
716 return 0;
717}
718
719static int sunxi_mmc_bind(struct udevice *dev)
720{
Simon Glassc69cda22020-12-03 16:55:20 -0700721 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
Simon Glassdd279182017-07-04 13:31:27 -0600722
723 return mmc_bind(dev, &plat->mmc, &plat->cfg);
724}
725
726static const struct udevice_id sunxi_mmc_ids[] = {
Andre Przywara0237b302021-01-11 21:11:44 +0100727 { .compatible = "allwinner,sun4i-a10-mmc" },
728 { .compatible = "allwinner,sun5i-a13-mmc" },
729 { .compatible = "allwinner,sun7i-a20-mmc" },
730 { .compatible = "allwinner,sun8i-a83t-emmc" },
731 { .compatible = "allwinner,sun9i-a80-mmc" },
732 { .compatible = "allwinner,sun50i-a64-mmc" },
733 { .compatible = "allwinner,sun50i-a64-emmc" },
734 { .compatible = "allwinner,sun50i-h6-mmc" },
735 { .compatible = "allwinner,sun50i-h6-emmc" },
736 { .compatible = "allwinner,sun50i-a100-mmc" },
737 { .compatible = "allwinner,sun50i-a100-emmc" },
Jagan Tekie8f37f42019-01-09 16:58:39 +0530738 { /* sentinel */ }
Simon Glassdd279182017-07-04 13:31:27 -0600739};
740
741U_BOOT_DRIVER(sunxi_mmc_drv) = {
742 .name = "sunxi_mmc",
743 .id = UCLASS_MMC,
744 .of_match = sunxi_mmc_ids,
745 .bind = sunxi_mmc_bind,
746 .probe = sunxi_mmc_probe,
747 .ops = &sunxi_mmc_ops,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700748 .plat_auto = sizeof(struct sunxi_mmc_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700749 .priv_auto = sizeof(struct sunxi_mmc_priv),
Simon Glassdd279182017-07-04 13:31:27 -0600750};
751#endif