Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007-2011 |
| 3 | * Allwinner Technology Co., Ltd. <www.allwinnertech.com> |
| 4 | * Aaron <leafy.myeh@allwinnertech.com> |
| 5 | * |
| 6 | * MMC driver for allwinner sunxi platform. |
| 7 | * |
| 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Hans de Goede | 90641f8 | 2015-04-22 17:03:17 +0200 | [diff] [blame] | 13 | #include <errno.h> |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 14 | #include <malloc.h> |
| 15 | #include <mmc.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <asm/arch/clock.h> |
| 18 | #include <asm/arch/cpu.h> |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 19 | #include <asm/arch/gpio.h> |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 20 | #include <asm/arch/mmc.h> |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 21 | #include <asm-generic/gpio.h> |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 22 | |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 23 | struct sunxi_mmc_plat { |
| 24 | struct mmc_config cfg; |
| 25 | struct mmc mmc; |
| 26 | }; |
| 27 | |
Simon Glass | e3c794e | 2017-07-04 13:31:23 -0600 | [diff] [blame] | 28 | struct sunxi_mmc_priv { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 29 | unsigned mmc_no; |
| 30 | uint32_t *mclkreg; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 31 | unsigned fatal_err; |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 32 | struct gpio_desc cd_gpio; /* Change Detect GPIO */ |
Heinrich Schuchardt | 8be4e61 | 2018-02-01 23:39:19 +0100 | [diff] [blame^] | 33 | int cd_inverted; /* Inverted Card Detect */ |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 34 | struct sunxi_mmc *reg; |
| 35 | struct mmc_config cfg; |
| 36 | }; |
| 37 | |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 38 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 39 | /* support 4 mmc hosts */ |
Simon Glass | e3c794e | 2017-07-04 13:31:23 -0600 | [diff] [blame] | 40 | struct sunxi_mmc_priv mmc_host[4]; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 41 | |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 42 | static int sunxi_mmc_getcd_gpio(int sdc_no) |
| 43 | { |
| 44 | switch (sdc_no) { |
| 45 | case 0: return sunxi_name_to_gpio(CONFIG_MMC0_CD_PIN); |
| 46 | case 1: return sunxi_name_to_gpio(CONFIG_MMC1_CD_PIN); |
| 47 | case 2: return sunxi_name_to_gpio(CONFIG_MMC2_CD_PIN); |
| 48 | case 3: return sunxi_name_to_gpio(CONFIG_MMC3_CD_PIN); |
| 49 | } |
Hans de Goede | 90641f8 | 2015-04-22 17:03:17 +0200 | [diff] [blame] | 50 | return -EINVAL; |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 53 | static int mmc_resource_init(int sdc_no) |
| 54 | { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 55 | struct sunxi_mmc_priv *priv = &mmc_host[sdc_no]; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 56 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 57 | int cd_pin, ret = 0; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 58 | |
| 59 | debug("init mmc %d resource\n", sdc_no); |
| 60 | |
| 61 | switch (sdc_no) { |
| 62 | case 0: |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 63 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE; |
| 64 | priv->mclkreg = &ccm->sd0_clk_cfg; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 65 | break; |
| 66 | case 1: |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 67 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE; |
| 68 | priv->mclkreg = &ccm->sd1_clk_cfg; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 69 | break; |
| 70 | case 2: |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 71 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE; |
| 72 | priv->mclkreg = &ccm->sd2_clk_cfg; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 73 | break; |
| 74 | case 3: |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 75 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE; |
| 76 | priv->mclkreg = &ccm->sd3_clk_cfg; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 77 | break; |
| 78 | default: |
| 79 | printf("Wrong mmc number %d\n", sdc_no); |
| 80 | return -1; |
| 81 | } |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 82 | priv->mmc_no = sdc_no; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 83 | |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 84 | cd_pin = sunxi_mmc_getcd_gpio(sdc_no); |
Hans de Goede | 90641f8 | 2015-04-22 17:03:17 +0200 | [diff] [blame] | 85 | if (cd_pin >= 0) { |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 86 | ret = gpio_request(cd_pin, "mmc_cd"); |
Hans de Goede | 1c09fa3 | 2015-05-30 16:39:10 +0200 | [diff] [blame] | 87 | if (!ret) { |
| 88 | sunxi_gpio_set_pull(cd_pin, SUNXI_GPIO_PULL_UP); |
Axel Lin | b0c4ae1 | 2014-12-20 11:41:25 +0800 | [diff] [blame] | 89 | ret = gpio_direction_input(cd_pin); |
Hans de Goede | 1c09fa3 | 2015-05-30 16:39:10 +0200 | [diff] [blame] | 90 | } |
Axel Lin | b0c4ae1 | 2014-12-20 11:41:25 +0800 | [diff] [blame] | 91 | } |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 92 | |
| 93 | return ret; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 94 | } |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 95 | #endif |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 96 | |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 97 | static int mmc_set_mod_clk(struct sunxi_mmc_priv *priv, unsigned int hz) |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 98 | { |
| 99 | unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly; |
Maxime Ripard | de9b177 | 2017-08-23 12:03:41 +0200 | [diff] [blame] | 100 | bool new_mode = false; |
| 101 | u32 val = 0; |
| 102 | |
| 103 | if (IS_ENABLED(CONFIG_MMC_SUNXI_HAS_NEW_MODE) && (priv->mmc_no == 2)) |
| 104 | new_mode = true; |
| 105 | |
| 106 | /* |
| 107 | * The MMC clock has an extra /2 post-divider when operating in the new |
| 108 | * mode. |
| 109 | */ |
| 110 | if (new_mode) |
| 111 | hz = hz * 2; |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 112 | |
| 113 | if (hz <= 24000000) { |
| 114 | pll = CCM_MMC_CTRL_OSCM24; |
| 115 | pll_hz = 24000000; |
| 116 | } else { |
Hans de Goede | daf2263 | 2015-01-14 19:05:03 +0100 | [diff] [blame] | 117 | #ifdef CONFIG_MACH_SUN9I |
| 118 | pll = CCM_MMC_CTRL_PLL_PERIPH0; |
| 119 | pll_hz = clock_get_pll4_periph0(); |
| 120 | #else |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 121 | pll = CCM_MMC_CTRL_PLL6; |
| 122 | pll_hz = clock_get_pll6(); |
Hans de Goede | daf2263 | 2015-01-14 19:05:03 +0100 | [diff] [blame] | 123 | #endif |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | div = pll_hz / hz; |
| 127 | if (pll_hz % hz) |
| 128 | div++; |
| 129 | |
| 130 | n = 0; |
| 131 | while (div > 16) { |
| 132 | n++; |
| 133 | div = (div + 1) / 2; |
| 134 | } |
| 135 | |
| 136 | if (n > 3) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 137 | printf("mmc %u error cannot set clock to %u\n", priv->mmc_no, |
| 138 | hz); |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | /* determine delays */ |
| 143 | if (hz <= 400000) { |
| 144 | oclk_dly = 0; |
Hans de Goede | be90974 | 2015-09-23 16:13:10 +0200 | [diff] [blame] | 145 | sclk_dly = 0; |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 146 | } else if (hz <= 25000000) { |
| 147 | oclk_dly = 0; |
| 148 | sclk_dly = 5; |
Hans de Goede | be90974 | 2015-09-23 16:13:10 +0200 | [diff] [blame] | 149 | #ifdef CONFIG_MACH_SUN9I |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 150 | } else if (hz <= 50000000) { |
Hans de Goede | be90974 | 2015-09-23 16:13:10 +0200 | [diff] [blame] | 151 | oclk_dly = 5; |
| 152 | sclk_dly = 4; |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 153 | } else { |
| 154 | /* hz > 50000000 */ |
| 155 | oclk_dly = 2; |
| 156 | sclk_dly = 4; |
Hans de Goede | be90974 | 2015-09-23 16:13:10 +0200 | [diff] [blame] | 157 | #else |
| 158 | } else if (hz <= 50000000) { |
| 159 | oclk_dly = 3; |
| 160 | sclk_dly = 4; |
| 161 | } else { |
| 162 | /* hz > 50000000 */ |
| 163 | oclk_dly = 1; |
| 164 | sclk_dly = 4; |
| 165 | #endif |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Maxime Ripard | de9b177 | 2017-08-23 12:03:41 +0200 | [diff] [blame] | 168 | if (new_mode) { |
| 169 | #ifdef CONFIG_MMC_SUNXI_HAS_NEW_MODE |
| 170 | val = CCM_MMC_CTRL_MODE_SEL_NEW; |
Chen-Yu Tsai | 8a647fc | 2017-08-31 21:57:48 +0800 | [diff] [blame] | 171 | setbits_le32(&priv->reg->ntsr, SUNXI_MMC_NTSR_MODE_SEL_NEW); |
Maxime Ripard | de9b177 | 2017-08-23 12:03:41 +0200 | [diff] [blame] | 172 | #endif |
| 173 | } else { |
| 174 | val = CCM_MMC_CTRL_OCLK_DLY(oclk_dly) | |
| 175 | CCM_MMC_CTRL_SCLK_DLY(sclk_dly); |
| 176 | } |
| 177 | |
| 178 | writel(CCM_MMC_CTRL_ENABLE| pll | CCM_MMC_CTRL_N(n) | |
| 179 | CCM_MMC_CTRL_M(div) | val, priv->mclkreg); |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 180 | |
| 181 | debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n", |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 182 | priv->mmc_no, hz, pll_hz, 1u << n, div, pll_hz / (1u << n) / div); |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 187 | static int mmc_update_clk(struct sunxi_mmc_priv *priv) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 188 | { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 189 | unsigned int cmd; |
| 190 | unsigned timeout_msecs = 2000; |
| 191 | |
| 192 | cmd = SUNXI_MMC_CMD_START | |
| 193 | SUNXI_MMC_CMD_UPCLK_ONLY | |
| 194 | SUNXI_MMC_CMD_WAIT_PRE_OVER; |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 195 | writel(cmd, &priv->reg->cmd); |
| 196 | while (readl(&priv->reg->cmd) & SUNXI_MMC_CMD_START) { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 197 | if (!timeout_msecs--) |
| 198 | return -1; |
| 199 | udelay(1000); |
| 200 | } |
| 201 | |
| 202 | /* clock update sets various irq status bits, clear these */ |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 203 | writel(readl(&priv->reg->rint), &priv->reg->rint); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 208 | static int mmc_config_clock(struct sunxi_mmc_priv *priv, struct mmc *mmc) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 209 | { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 210 | unsigned rval = readl(&priv->reg->clkcr); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 211 | |
| 212 | /* Disable Clock */ |
| 213 | rval &= ~SUNXI_MMC_CLK_ENABLE; |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 214 | writel(rval, &priv->reg->clkcr); |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 215 | if (mmc_update_clk(priv)) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 216 | return -1; |
| 217 | |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 218 | /* Set mod_clk to new rate */ |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 219 | if (mmc_set_mod_clk(priv, mmc->clock)) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 220 | return -1; |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 221 | |
| 222 | /* Clear internal divider */ |
| 223 | rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK; |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 224 | writel(rval, &priv->reg->clkcr); |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 225 | |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 226 | /* Re-enable Clock */ |
| 227 | rval |= SUNXI_MMC_CLK_ENABLE; |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 228 | writel(rval, &priv->reg->clkcr); |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 229 | if (mmc_update_clk(priv)) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 230 | return -1; |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 235 | static int sunxi_mmc_set_ios_common(struct sunxi_mmc_priv *priv, |
| 236 | struct mmc *mmc) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 237 | { |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 238 | debug("set ios: bus_width: %x, clock: %d\n", |
| 239 | mmc->bus_width, mmc->clock); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 240 | |
| 241 | /* Change clock first */ |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 242 | if (mmc->clock && mmc_config_clock(priv, mmc) != 0) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 243 | priv->fatal_err = 1; |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 244 | return -EINVAL; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* Change bus width */ |
| 248 | if (mmc->bus_width == 8) |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 249 | writel(0x2, &priv->reg->width); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 250 | else if (mmc->bus_width == 4) |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 251 | writel(0x1, &priv->reg->width); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 252 | else |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 253 | writel(0x0, &priv->reg->width); |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 254 | |
| 255 | return 0; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 258 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Siarhei Siamashka | 5abdb15 | 2015-02-01 00:42:14 +0200 | [diff] [blame] | 259 | static int sunxi_mmc_core_init(struct mmc *mmc) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 260 | { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 261 | struct sunxi_mmc_priv *priv = mmc->priv; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 262 | |
| 263 | /* Reset controller */ |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 264 | writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl); |
Hans de Goede | b6ae676 | 2014-06-09 11:36:55 +0200 | [diff] [blame] | 265 | udelay(1000); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 266 | |
| 267 | return 0; |
| 268 | } |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 269 | #endif |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 270 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 271 | static int mmc_trans_data_by_cpu(struct sunxi_mmc_priv *priv, struct mmc *mmc, |
| 272 | struct mmc_data *data) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 273 | { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 274 | const int reading = !!(data->flags & MMC_DATA_READ); |
| 275 | const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY : |
| 276 | SUNXI_MMC_STATUS_FIFO_FULL; |
| 277 | unsigned i; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 278 | unsigned *buff = (unsigned int *)(reading ? data->dest : data->src); |
Yousong Zhou | 28f69b9 | 2015-08-29 21:26:11 +0800 | [diff] [blame] | 279 | unsigned byte_cnt = data->blocksize * data->blocks; |
Tobias Doerffel | 26c0c15 | 2016-07-08 12:40:14 +0200 | [diff] [blame] | 280 | unsigned timeout_usecs = (byte_cnt >> 8) * 1000; |
| 281 | if (timeout_usecs < 2000000) |
| 282 | timeout_usecs = 2000000; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 283 | |
Hans de Goede | b6ae676 | 2014-06-09 11:36:55 +0200 | [diff] [blame] | 284 | /* Always read / write data through the CPU */ |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 285 | setbits_le32(&priv->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB); |
Hans de Goede | b6ae676 | 2014-06-09 11:36:55 +0200 | [diff] [blame] | 286 | |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 287 | for (i = 0; i < (byte_cnt >> 2); i++) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 288 | while (readl(&priv->reg->status) & status_bit) { |
Tobias Doerffel | 26c0c15 | 2016-07-08 12:40:14 +0200 | [diff] [blame] | 289 | if (!timeout_usecs--) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 290 | return -1; |
Tobias Doerffel | 26c0c15 | 2016-07-08 12:40:14 +0200 | [diff] [blame] | 291 | udelay(1); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | if (reading) |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 295 | buff[i] = readl(&priv->reg->fifo); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 296 | else |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 297 | writel(buff[i], &priv->reg->fifo); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
Simon Glass | 034e226 | 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 | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 305 | { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 306 | unsigned int status; |
| 307 | |
| 308 | do { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 309 | status = readl(&priv->reg->rint); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 310 | if (!timeout_msecs-- || |
| 311 | (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) { |
| 312 | debug("%s timeout %x\n", what, |
| 313 | status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 314 | return -ETIMEDOUT; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 315 | } |
| 316 | udelay(1000); |
| 317 | } while (!(status & done_bit)); |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Simon Glass | 034e226 | 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 | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 325 | { |
Ian Campbell | e24ea55 | 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 | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 330 | unsigned int bytecnt = 0; |
| 331 | |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 332 | if (priv->fatal_err) |
Ian Campbell | e24ea55 | 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 | 0ea5a04 | 2016-03-29 17:29:09 +0200 | [diff] [blame] | 349 | if ((u32)(long)data->dest & 0x3) { |
Ian Campbell | e24ea55 | 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 | 3f5af12 | 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 | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 361 | } |
| 362 | |
Simon Glass | 3f5af12 | 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 | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 364 | cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg); |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 365 | writel(cmd->cmdarg, &priv->reg->arg); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 366 | |
| 367 | if (!data) |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 368 | writel(cmdval | cmd->cmdidx, &priv->reg->cmd); |
Ian Campbell | e24ea55 | 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 | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 380 | writel(cmdval | cmd->cmdidx, &priv->reg->cmd); |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 381 | ret = mmc_trans_data_by_cpu(priv, mmc, data); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 382 | if (ret) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 383 | error = readl(&priv->reg->rint) & |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 384 | SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT; |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 385 | error = -ETIMEDOUT; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 386 | goto out; |
| 387 | } |
| 388 | } |
| 389 | |
Simon Glass | 034e226 | 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 | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 392 | if (error) |
| 393 | goto out; |
| 394 | |
| 395 | if (data) { |
Hans de Goede | b6ae676 | 2014-06-09 11:36:55 +0200 | [diff] [blame] | 396 | timeout_msecs = 120; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 397 | debug("cacl timeout %x msec\n", timeout_msecs); |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 398 | error = mmc_rint_wait(priv, mmc, timeout_msecs, |
Ian Campbell | e24ea55 | 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) { |
| 408 | timeout_msecs = 2000; |
| 409 | do { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 410 | status = readl(&priv->reg->status); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 411 | if (!timeout_msecs--) { |
| 412 | debug("busy timeout\n"); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 413 | error = -ETIMEDOUT; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 414 | goto out; |
| 415 | } |
| 416 | udelay(1000); |
| 417 | } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY); |
| 418 | } |
| 419 | |
| 420 | if (cmd->resp_type & MMC_RSP_136) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 421 | cmd->response[0] = readl(&priv->reg->resp3); |
| 422 | cmd->response[1] = readl(&priv->reg->resp2); |
| 423 | cmd->response[2] = readl(&priv->reg->resp1); |
| 424 | cmd->response[3] = readl(&priv->reg->resp0); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 425 | debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n", |
| 426 | cmd->response[3], cmd->response[2], |
| 427 | cmd->response[1], cmd->response[0]); |
| 428 | } else { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 429 | cmd->response[0] = readl(&priv->reg->resp0); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 430 | debug("mmc resp 0x%08x\n", cmd->response[0]); |
| 431 | } |
| 432 | out: |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 433 | if (error < 0) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 434 | writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl); |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 435 | mmc_update_clk(priv); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 436 | } |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 437 | writel(0xffffffff, &priv->reg->rint); |
| 438 | writel(readl(&priv->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET, |
| 439 | &priv->reg->gctrl); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 440 | |
| 441 | return error; |
| 442 | } |
| 443 | |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 444 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 445 | static int sunxi_mmc_set_ios_legacy(struct mmc *mmc) |
| 446 | { |
| 447 | struct sunxi_mmc_priv *priv = mmc->priv; |
| 448 | |
| 449 | return sunxi_mmc_set_ios_common(priv, mmc); |
| 450 | } |
| 451 | |
| 452 | static int sunxi_mmc_send_cmd_legacy(struct mmc *mmc, struct mmc_cmd *cmd, |
| 453 | struct mmc_data *data) |
| 454 | { |
| 455 | struct sunxi_mmc_priv *priv = mmc->priv; |
| 456 | |
| 457 | return sunxi_mmc_send_cmd_common(priv, mmc, cmd, data); |
| 458 | } |
| 459 | |
| 460 | static int sunxi_mmc_getcd_legacy(struct mmc *mmc) |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 461 | { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 462 | struct sunxi_mmc_priv *priv = mmc->priv; |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 463 | int cd_pin; |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 464 | |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 465 | cd_pin = sunxi_mmc_getcd_gpio(priv->mmc_no); |
Hans de Goede | 90641f8 | 2015-04-22 17:03:17 +0200 | [diff] [blame] | 466 | if (cd_pin < 0) |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 467 | return 1; |
| 468 | |
Axel Lin | b0c4ae1 | 2014-12-20 11:41:25 +0800 | [diff] [blame] | 469 | return !gpio_get_value(cd_pin); |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 472 | static const struct mmc_ops sunxi_mmc_ops = { |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 473 | .send_cmd = sunxi_mmc_send_cmd_legacy, |
| 474 | .set_ios = sunxi_mmc_set_ios_legacy, |
Siarhei Siamashka | 5abdb15 | 2015-02-01 00:42:14 +0200 | [diff] [blame] | 475 | .init = sunxi_mmc_core_init, |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 476 | .getcd = sunxi_mmc_getcd_legacy, |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 477 | }; |
| 478 | |
Hans de Goede | e79c7c8 | 2014-10-02 21:13:54 +0200 | [diff] [blame] | 479 | struct mmc *sunxi_mmc_init(int sdc_no) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 480 | { |
Simon Glass | ec73d96 | 2017-07-04 13:31:26 -0600 | [diff] [blame] | 481 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 482 | struct sunxi_mmc_priv *priv = &mmc_host[sdc_no]; |
| 483 | struct mmc_config *cfg = &priv->cfg; |
Simon Glass | ec73d96 | 2017-07-04 13:31:26 -0600 | [diff] [blame] | 484 | int ret; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 485 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame] | 486 | memset(priv, '\0', sizeof(struct sunxi_mmc_priv)); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 487 | |
| 488 | cfg->name = "SUNXI SD/MMC"; |
| 489 | cfg->ops = &sunxi_mmc_ops; |
| 490 | |
| 491 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 492 | cfg->host_caps = MMC_MODE_4BIT; |
Maxime Ripard | fb01318 | 2016-11-04 16:18:09 +0100 | [diff] [blame] | 493 | #if defined(CONFIG_MACH_SUN50I) || defined(CONFIG_MACH_SUN8I) |
Siarhei Siamashka | d96ebc4 | 2016-03-29 17:29:10 +0200 | [diff] [blame] | 494 | if (sdc_no == 2) |
| 495 | cfg->host_caps = MMC_MODE_8BIT; |
| 496 | #endif |
Rob Herring | 5a20397 | 2015-03-23 17:56:59 -0500 | [diff] [blame] | 497 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 498 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 499 | |
| 500 | cfg->f_min = 400000; |
| 501 | cfg->f_max = 52000000; |
| 502 | |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 503 | if (mmc_resource_init(sdc_no) != 0) |
| 504 | return NULL; |
| 505 | |
Simon Glass | ec73d96 | 2017-07-04 13:31:26 -0600 | [diff] [blame] | 506 | /* config ahb clock */ |
| 507 | debug("init mmc %d clock and io\n", sdc_no); |
| 508 | setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no)); |
| 509 | |
| 510 | #ifdef CONFIG_SUNXI_GEN_SUN6I |
| 511 | /* unassert reset */ |
| 512 | setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no)); |
| 513 | #endif |
| 514 | #if defined(CONFIG_MACH_SUN9I) |
| 515 | /* sun9i has a mmc-common module, also set the gate and reset there */ |
| 516 | writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET, |
| 517 | SUNXI_MMC_COMMON_BASE + 4 * sdc_no); |
| 518 | #endif |
| 519 | ret = mmc_set_mod_clk(priv, 24000000); |
| 520 | if (ret) |
| 521 | return NULL; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 522 | |
Maxime Ripard | ead3697 | 2017-08-23 13:41:33 +0200 | [diff] [blame] | 523 | return mmc_create(cfg, priv); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 524 | } |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 525 | #else |
| 526 | |
| 527 | static int sunxi_mmc_set_ios(struct udevice *dev) |
| 528 | { |
| 529 | struct sunxi_mmc_plat *plat = dev_get_platdata(dev); |
| 530 | struct sunxi_mmc_priv *priv = dev_get_priv(dev); |
| 531 | |
| 532 | return sunxi_mmc_set_ios_common(priv, &plat->mmc); |
| 533 | } |
| 534 | |
| 535 | static int sunxi_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 536 | struct mmc_data *data) |
| 537 | { |
| 538 | struct sunxi_mmc_plat *plat = dev_get_platdata(dev); |
| 539 | struct sunxi_mmc_priv *priv = dev_get_priv(dev); |
| 540 | |
| 541 | return sunxi_mmc_send_cmd_common(priv, &plat->mmc, cmd, data); |
| 542 | } |
| 543 | |
| 544 | static int sunxi_mmc_getcd(struct udevice *dev) |
| 545 | { |
| 546 | struct sunxi_mmc_priv *priv = dev_get_priv(dev); |
| 547 | |
Heinrich Schuchardt | 8be4e61 | 2018-02-01 23:39:19 +0100 | [diff] [blame^] | 548 | if (dm_gpio_is_valid(&priv->cd_gpio)) { |
| 549 | int cd_state = dm_gpio_get_value(&priv->cd_gpio); |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 550 | |
Heinrich Schuchardt | 8be4e61 | 2018-02-01 23:39:19 +0100 | [diff] [blame^] | 551 | return cd_state ^ priv->cd_inverted; |
| 552 | } |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 553 | return 1; |
| 554 | } |
| 555 | |
| 556 | static const struct dm_mmc_ops sunxi_mmc_ops = { |
| 557 | .send_cmd = sunxi_mmc_send_cmd, |
| 558 | .set_ios = sunxi_mmc_set_ios, |
| 559 | .get_cd = sunxi_mmc_getcd, |
| 560 | }; |
| 561 | |
| 562 | static int sunxi_mmc_probe(struct udevice *dev) |
| 563 | { |
| 564 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 565 | struct sunxi_mmc_plat *plat = dev_get_platdata(dev); |
| 566 | struct sunxi_mmc_priv *priv = dev_get_priv(dev); |
| 567 | struct mmc_config *cfg = &plat->cfg; |
| 568 | struct ofnode_phandle_args args; |
| 569 | u32 *gate_reg; |
| 570 | int bus_width, ret; |
| 571 | |
| 572 | cfg->name = dev->name; |
| 573 | bus_width = dev_read_u32_default(dev, "bus-width", 1); |
| 574 | |
| 575 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 576 | cfg->host_caps = 0; |
| 577 | if (bus_width == 8) |
| 578 | cfg->host_caps |= MMC_MODE_8BIT; |
| 579 | if (bus_width >= 4) |
| 580 | cfg->host_caps |= MMC_MODE_4BIT; |
| 581 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
| 582 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 583 | |
| 584 | cfg->f_min = 400000; |
| 585 | cfg->f_max = 52000000; |
| 586 | |
| 587 | priv->reg = (void *)dev_read_addr(dev); |
| 588 | |
| 589 | /* We don't have a sunxi clock driver so find the clock address here */ |
| 590 | ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0, |
| 591 | 1, &args); |
| 592 | if (ret) |
| 593 | return ret; |
| 594 | priv->mclkreg = (u32 *)ofnode_get_addr(args.node); |
| 595 | |
| 596 | ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0, |
| 597 | 0, &args); |
| 598 | if (ret) |
| 599 | return ret; |
| 600 | gate_reg = (u32 *)ofnode_get_addr(args.node); |
| 601 | setbits_le32(gate_reg, 1 << args.args[0]); |
| 602 | priv->mmc_no = args.args[0] - 8; |
| 603 | |
| 604 | ret = mmc_set_mod_clk(priv, 24000000); |
| 605 | if (ret) |
| 606 | return ret; |
| 607 | |
| 608 | /* This GPIO is optional */ |
| 609 | if (!gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, |
| 610 | GPIOD_IS_IN)) { |
| 611 | int cd_pin = gpio_get_number(&priv->cd_gpio); |
| 612 | |
| 613 | sunxi_gpio_set_pull(cd_pin, SUNXI_GPIO_PULL_UP); |
| 614 | } |
| 615 | |
Heinrich Schuchardt | 8be4e61 | 2018-02-01 23:39:19 +0100 | [diff] [blame^] | 616 | /* Check if card detect is inverted */ |
| 617 | priv->cd_inverted = dev_read_bool(dev, "cd-inverted"); |
| 618 | |
Simon Glass | dd27918 | 2017-07-04 13:31:27 -0600 | [diff] [blame] | 619 | upriv->mmc = &plat->mmc; |
| 620 | |
| 621 | /* Reset controller */ |
| 622 | writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl); |
| 623 | udelay(1000); |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | static int sunxi_mmc_bind(struct udevice *dev) |
| 629 | { |
| 630 | struct sunxi_mmc_plat *plat = dev_get_platdata(dev); |
| 631 | |
| 632 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 633 | } |
| 634 | |
| 635 | static const struct udevice_id sunxi_mmc_ids[] = { |
| 636 | { .compatible = "allwinner,sun5i-a13-mmc" }, |
| 637 | { } |
| 638 | }; |
| 639 | |
| 640 | U_BOOT_DRIVER(sunxi_mmc_drv) = { |
| 641 | .name = "sunxi_mmc", |
| 642 | .id = UCLASS_MMC, |
| 643 | .of_match = sunxi_mmc_ids, |
| 644 | .bind = sunxi_mmc_bind, |
| 645 | .probe = sunxi_mmc_probe, |
| 646 | .ops = &sunxi_mmc_ops, |
| 647 | .platdata_auto_alloc_size = sizeof(struct sunxi_mmc_plat), |
| 648 | .priv_auto_alloc_size = sizeof(struct sunxi_mmc_priv), |
| 649 | }; |
| 650 | #endif |