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