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> |
Hans de Goede | 90641f8 | 2015-04-22 17:03:17 +0200 | [diff] [blame] | 12 | #include <errno.h> |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <mmc.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <asm/arch/clock.h> |
| 17 | #include <asm/arch/cpu.h> |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 18 | #include <asm/arch/gpio.h> |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 19 | #include <asm/arch/mmc.h> |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 20 | #include <asm-generic/gpio.h> |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 21 | |
Simon Glass | e3c794e | 2017-07-04 13:31:23 -0600 | [diff] [blame] | 22 | struct sunxi_mmc_priv { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 23 | unsigned mmc_no; |
| 24 | uint32_t *mclkreg; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 25 | unsigned fatal_err; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 26 | struct sunxi_mmc *reg; |
| 27 | struct mmc_config cfg; |
| 28 | }; |
| 29 | |
| 30 | /* support 4 mmc hosts */ |
Simon Glass | e3c794e | 2017-07-04 13:31:23 -0600 | [diff] [blame] | 31 | struct sunxi_mmc_priv mmc_host[4]; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 32 | |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 33 | static int sunxi_mmc_getcd_gpio(int sdc_no) |
| 34 | { |
| 35 | switch (sdc_no) { |
| 36 | case 0: return sunxi_name_to_gpio(CONFIG_MMC0_CD_PIN); |
| 37 | case 1: return sunxi_name_to_gpio(CONFIG_MMC1_CD_PIN); |
| 38 | case 2: return sunxi_name_to_gpio(CONFIG_MMC2_CD_PIN); |
| 39 | case 3: return sunxi_name_to_gpio(CONFIG_MMC3_CD_PIN); |
| 40 | } |
Hans de Goede | 90641f8 | 2015-04-22 17:03:17 +0200 | [diff] [blame] | 41 | return -EINVAL; |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 44 | static int mmc_resource_init(int sdc_no) |
| 45 | { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 46 | struct sunxi_mmc_priv *priv = &mmc_host[sdc_no]; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 47 | 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] | 48 | int cd_pin, ret = 0; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 49 | |
| 50 | debug("init mmc %d resource\n", sdc_no); |
| 51 | |
| 52 | switch (sdc_no) { |
| 53 | case 0: |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 54 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE; |
| 55 | priv->mclkreg = &ccm->sd0_clk_cfg; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 56 | break; |
| 57 | case 1: |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 58 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE; |
| 59 | priv->mclkreg = &ccm->sd1_clk_cfg; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 60 | break; |
| 61 | case 2: |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 62 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE; |
| 63 | priv->mclkreg = &ccm->sd2_clk_cfg; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 64 | break; |
| 65 | case 3: |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 66 | priv->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE; |
| 67 | priv->mclkreg = &ccm->sd3_clk_cfg; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 68 | break; |
| 69 | default: |
| 70 | printf("Wrong mmc number %d\n", sdc_no); |
| 71 | return -1; |
| 72 | } |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 73 | priv->mmc_no = sdc_no; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 74 | |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 75 | cd_pin = sunxi_mmc_getcd_gpio(sdc_no); |
Hans de Goede | 90641f8 | 2015-04-22 17:03:17 +0200 | [diff] [blame] | 76 | if (cd_pin >= 0) { |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 77 | ret = gpio_request(cd_pin, "mmc_cd"); |
Hans de Goede | 1c09fa3 | 2015-05-30 16:39:10 +0200 | [diff] [blame] | 78 | if (!ret) { |
| 79 | sunxi_gpio_set_pull(cd_pin, SUNXI_GPIO_PULL_UP); |
Axel Lin | b0c4ae1 | 2014-12-20 11:41:25 +0800 | [diff] [blame] | 80 | ret = gpio_direction_input(cd_pin); |
Hans de Goede | 1c09fa3 | 2015-05-30 16:39:10 +0200 | [diff] [blame] | 81 | } |
Axel Lin | b0c4ae1 | 2014-12-20 11:41:25 +0800 | [diff] [blame] | 82 | } |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 83 | |
| 84 | return ret; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 87 | 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] | 88 | { |
| 89 | unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly; |
| 90 | |
| 91 | if (hz <= 24000000) { |
| 92 | pll = CCM_MMC_CTRL_OSCM24; |
| 93 | pll_hz = 24000000; |
| 94 | } else { |
Hans de Goede | daf2263 | 2015-01-14 19:05:03 +0100 | [diff] [blame] | 95 | #ifdef CONFIG_MACH_SUN9I |
| 96 | pll = CCM_MMC_CTRL_PLL_PERIPH0; |
| 97 | pll_hz = clock_get_pll4_periph0(); |
| 98 | #else |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 99 | pll = CCM_MMC_CTRL_PLL6; |
| 100 | pll_hz = clock_get_pll6(); |
Hans de Goede | daf2263 | 2015-01-14 19:05:03 +0100 | [diff] [blame] | 101 | #endif |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | div = pll_hz / hz; |
| 105 | if (pll_hz % hz) |
| 106 | div++; |
| 107 | |
| 108 | n = 0; |
| 109 | while (div > 16) { |
| 110 | n++; |
| 111 | div = (div + 1) / 2; |
| 112 | } |
| 113 | |
| 114 | if (n > 3) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 115 | printf("mmc %u error cannot set clock to %u\n", priv->mmc_no, |
| 116 | hz); |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | /* determine delays */ |
| 121 | if (hz <= 400000) { |
| 122 | oclk_dly = 0; |
Hans de Goede | be90974 | 2015-09-23 16:13:10 +0200 | [diff] [blame] | 123 | sclk_dly = 0; |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 124 | } else if (hz <= 25000000) { |
| 125 | oclk_dly = 0; |
| 126 | sclk_dly = 5; |
Hans de Goede | be90974 | 2015-09-23 16:13:10 +0200 | [diff] [blame] | 127 | #ifdef CONFIG_MACH_SUN9I |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 128 | } else if (hz <= 50000000) { |
Hans de Goede | be90974 | 2015-09-23 16:13:10 +0200 | [diff] [blame] | 129 | oclk_dly = 5; |
| 130 | sclk_dly = 4; |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 131 | } else { |
| 132 | /* hz > 50000000 */ |
| 133 | oclk_dly = 2; |
| 134 | sclk_dly = 4; |
Hans de Goede | be90974 | 2015-09-23 16:13:10 +0200 | [diff] [blame] | 135 | #else |
| 136 | } else if (hz <= 50000000) { |
| 137 | oclk_dly = 3; |
| 138 | sclk_dly = 4; |
| 139 | } else { |
| 140 | /* hz > 50000000 */ |
| 141 | oclk_dly = 1; |
| 142 | sclk_dly = 4; |
| 143 | #endif |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | writel(CCM_MMC_CTRL_ENABLE | pll | CCM_MMC_CTRL_SCLK_DLY(sclk_dly) | |
| 147 | CCM_MMC_CTRL_N(n) | CCM_MMC_CTRL_OCLK_DLY(oclk_dly) | |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 148 | CCM_MMC_CTRL_M(div), priv->mclkreg); |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 149 | |
| 150 | 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] | 151 | 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] | 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 156 | static int mmc_clk_io_on(int sdc_no) |
| 157 | { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 158 | struct sunxi_mmc_priv *priv = &mmc_host[sdc_no]; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 159 | struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 160 | |
| 161 | debug("init mmc %d clock and io\n", sdc_no); |
| 162 | |
| 163 | /* config ahb clock */ |
| 164 | setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no)); |
| 165 | |
Hans de Goede | 44d8ae5 | 2015-04-06 20:33:34 +0200 | [diff] [blame] | 166 | #ifdef CONFIG_SUNXI_GEN_SUN6I |
Hans de Goede | 1d1bd42 | 2014-10-03 20:16:26 +0800 | [diff] [blame] | 167 | /* unassert reset */ |
| 168 | setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no)); |
| 169 | #endif |
Hans de Goede | daf2263 | 2015-01-14 19:05:03 +0100 | [diff] [blame] | 170 | #if defined(CONFIG_MACH_SUN9I) |
| 171 | /* sun9i has a mmc-common module, also set the gate and reset there */ |
| 172 | writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET, |
| 173 | SUNXI_MMC_COMMON_BASE + 4 * sdc_no); |
| 174 | #endif |
Hans de Goede | 1d1bd42 | 2014-10-03 20:16:26 +0800 | [diff] [blame] | 175 | |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 176 | return mmc_set_mod_clk(priv, 24000000); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 179 | static int mmc_update_clk(struct sunxi_mmc_priv *priv) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 180 | { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 181 | unsigned int cmd; |
| 182 | unsigned timeout_msecs = 2000; |
| 183 | |
| 184 | cmd = SUNXI_MMC_CMD_START | |
| 185 | SUNXI_MMC_CMD_UPCLK_ONLY | |
| 186 | SUNXI_MMC_CMD_WAIT_PRE_OVER; |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 187 | writel(cmd, &priv->reg->cmd); |
| 188 | while (readl(&priv->reg->cmd) & SUNXI_MMC_CMD_START) { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 189 | if (!timeout_msecs--) |
| 190 | return -1; |
| 191 | udelay(1000); |
| 192 | } |
| 193 | |
| 194 | /* clock update sets various irq status bits, clear these */ |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 195 | writel(readl(&priv->reg->rint), &priv->reg->rint); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 200 | 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] | 201 | { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 202 | unsigned rval = readl(&priv->reg->clkcr); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 203 | |
| 204 | /* Disable Clock */ |
| 205 | rval &= ~SUNXI_MMC_CLK_ENABLE; |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 206 | writel(rval, &priv->reg->clkcr); |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 207 | if (mmc_update_clk(priv)) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 208 | return -1; |
| 209 | |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 210 | /* Set mod_clk to new rate */ |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 211 | if (mmc_set_mod_clk(priv, mmc->clock)) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 212 | return -1; |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 213 | |
| 214 | /* Clear internal divider */ |
| 215 | rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK; |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 216 | writel(rval, &priv->reg->clkcr); |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 217 | |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 218 | /* Re-enable Clock */ |
| 219 | rval |= SUNXI_MMC_CLK_ENABLE; |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 220 | writel(rval, &priv->reg->clkcr); |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 221 | if (mmc_update_clk(priv)) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 222 | return -1; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 227 | static int sunxi_mmc_set_ios_common(struct sunxi_mmc_priv *priv, |
| 228 | struct mmc *mmc) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 229 | { |
Hans de Goede | fc3a832 | 2014-12-07 20:55:10 +0100 | [diff] [blame] | 230 | debug("set ios: bus_width: %x, clock: %d\n", |
| 231 | mmc->bus_width, mmc->clock); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 232 | |
| 233 | /* Change clock first */ |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 234 | if (mmc->clock && mmc_config_clock(priv, mmc) != 0) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 235 | priv->fatal_err = 1; |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 236 | return -EINVAL; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* Change bus width */ |
| 240 | if (mmc->bus_width == 8) |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 241 | writel(0x2, &priv->reg->width); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 242 | else if (mmc->bus_width == 4) |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 243 | writel(0x1, &priv->reg->width); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 244 | else |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 245 | writel(0x0, &priv->reg->width); |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 246 | |
| 247 | return 0; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Siarhei Siamashka | 5abdb15 | 2015-02-01 00:42:14 +0200 | [diff] [blame] | 250 | static int sunxi_mmc_core_init(struct mmc *mmc) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 251 | { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 252 | struct sunxi_mmc_priv *priv = mmc->priv; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 253 | |
| 254 | /* Reset controller */ |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 255 | writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl); |
Hans de Goede | b6ae676 | 2014-06-09 11:36:55 +0200 | [diff] [blame] | 256 | udelay(1000); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 261 | static int mmc_trans_data_by_cpu(struct sunxi_mmc_priv *priv, struct mmc *mmc, |
| 262 | struct mmc_data *data) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 263 | { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 264 | const int reading = !!(data->flags & MMC_DATA_READ); |
| 265 | const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY : |
| 266 | SUNXI_MMC_STATUS_FIFO_FULL; |
| 267 | unsigned i; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 268 | unsigned *buff = (unsigned int *)(reading ? data->dest : data->src); |
Yousong Zhou | 28f69b9 | 2015-08-29 21:26:11 +0800 | [diff] [blame] | 269 | unsigned byte_cnt = data->blocksize * data->blocks; |
Tobias Doerffel | 26c0c15 | 2016-07-08 12:40:14 +0200 | [diff] [blame] | 270 | unsigned timeout_usecs = (byte_cnt >> 8) * 1000; |
| 271 | if (timeout_usecs < 2000000) |
| 272 | timeout_usecs = 2000000; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 273 | |
Hans de Goede | b6ae676 | 2014-06-09 11:36:55 +0200 | [diff] [blame] | 274 | /* Always read / write data through the CPU */ |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 275 | setbits_le32(&priv->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB); |
Hans de Goede | b6ae676 | 2014-06-09 11:36:55 +0200 | [diff] [blame] | 276 | |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 277 | for (i = 0; i < (byte_cnt >> 2); i++) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 278 | while (readl(&priv->reg->status) & status_bit) { |
Tobias Doerffel | 26c0c15 | 2016-07-08 12:40:14 +0200 | [diff] [blame] | 279 | if (!timeout_usecs--) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 280 | return -1; |
Tobias Doerffel | 26c0c15 | 2016-07-08 12:40:14 +0200 | [diff] [blame] | 281 | udelay(1); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | if (reading) |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 285 | buff[i] = readl(&priv->reg->fifo); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 286 | else |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 287 | writel(buff[i], &priv->reg->fifo); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 293 | static int mmc_rint_wait(struct sunxi_mmc_priv *priv, struct mmc *mmc, |
| 294 | uint timeout_msecs, uint done_bit, const char *what) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 295 | { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 296 | unsigned int status; |
| 297 | |
| 298 | do { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 299 | status = readl(&priv->reg->rint); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 300 | if (!timeout_msecs-- || |
| 301 | (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) { |
| 302 | debug("%s timeout %x\n", what, |
| 303 | status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 304 | return -ETIMEDOUT; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 305 | } |
| 306 | udelay(1000); |
| 307 | } while (!(status & done_bit)); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 312 | static int sunxi_mmc_send_cmd_common(struct sunxi_mmc_priv *priv, |
| 313 | struct mmc *mmc, struct mmc_cmd *cmd, |
| 314 | struct mmc_data *data) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 315 | { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 316 | unsigned int cmdval = SUNXI_MMC_CMD_START; |
| 317 | unsigned int timeout_msecs; |
| 318 | int error = 0; |
| 319 | unsigned int status = 0; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 320 | unsigned int bytecnt = 0; |
| 321 | |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 322 | if (priv->fatal_err) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 323 | return -1; |
| 324 | if (cmd->resp_type & MMC_RSP_BUSY) |
| 325 | debug("mmc cmd %d check rsp busy\n", cmd->cmdidx); |
| 326 | if (cmd->cmdidx == 12) |
| 327 | return 0; |
| 328 | |
| 329 | if (!cmd->cmdidx) |
| 330 | cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ; |
| 331 | if (cmd->resp_type & MMC_RSP_PRESENT) |
| 332 | cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE; |
| 333 | if (cmd->resp_type & MMC_RSP_136) |
| 334 | cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE; |
| 335 | if (cmd->resp_type & MMC_RSP_CRC) |
| 336 | cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC; |
| 337 | |
| 338 | if (data) { |
Alexander Graf | 0ea5a04 | 2016-03-29 17:29:09 +0200 | [diff] [blame] | 339 | if ((u32)(long)data->dest & 0x3) { |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 340 | error = -1; |
| 341 | goto out; |
| 342 | } |
| 343 | |
| 344 | cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER; |
| 345 | if (data->flags & MMC_DATA_WRITE) |
| 346 | cmdval |= SUNXI_MMC_CMD_WRITE; |
| 347 | if (data->blocks > 1) |
| 348 | cmdval |= SUNXI_MMC_CMD_AUTO_STOP; |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 349 | writel(data->blocksize, &priv->reg->blksz); |
| 350 | writel(data->blocks * data->blocksize, &priv->reg->bytecnt); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 353 | 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] | 354 | cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg); |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 355 | writel(cmd->cmdarg, &priv->reg->arg); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 356 | |
| 357 | if (!data) |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 358 | writel(cmdval | cmd->cmdidx, &priv->reg->cmd); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * transfer data and check status |
| 362 | * STATREG[2] : FIFO empty |
| 363 | * STATREG[3] : FIFO full |
| 364 | */ |
| 365 | if (data) { |
| 366 | int ret = 0; |
| 367 | |
| 368 | bytecnt = data->blocksize * data->blocks; |
| 369 | debug("trans data %d bytes\n", bytecnt); |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 370 | writel(cmdval | cmd->cmdidx, &priv->reg->cmd); |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 371 | ret = mmc_trans_data_by_cpu(priv, mmc, data); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 372 | if (ret) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 373 | error = readl(&priv->reg->rint) & |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 374 | SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT; |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 375 | error = -ETIMEDOUT; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 376 | goto out; |
| 377 | } |
| 378 | } |
| 379 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 380 | error = mmc_rint_wait(priv, mmc, 1000, SUNXI_MMC_RINT_COMMAND_DONE, |
| 381 | "cmd"); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 382 | if (error) |
| 383 | goto out; |
| 384 | |
| 385 | if (data) { |
Hans de Goede | b6ae676 | 2014-06-09 11:36:55 +0200 | [diff] [blame] | 386 | timeout_msecs = 120; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 387 | debug("cacl timeout %x msec\n", timeout_msecs); |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 388 | error = mmc_rint_wait(priv, mmc, timeout_msecs, |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 389 | data->blocks > 1 ? |
| 390 | SUNXI_MMC_RINT_AUTO_COMMAND_DONE : |
| 391 | SUNXI_MMC_RINT_DATA_OVER, |
| 392 | "data"); |
| 393 | if (error) |
| 394 | goto out; |
| 395 | } |
| 396 | |
| 397 | if (cmd->resp_type & MMC_RSP_BUSY) { |
| 398 | timeout_msecs = 2000; |
| 399 | do { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 400 | status = readl(&priv->reg->status); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 401 | if (!timeout_msecs--) { |
| 402 | debug("busy timeout\n"); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 403 | error = -ETIMEDOUT; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 404 | goto out; |
| 405 | } |
| 406 | udelay(1000); |
| 407 | } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY); |
| 408 | } |
| 409 | |
| 410 | if (cmd->resp_type & MMC_RSP_136) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 411 | cmd->response[0] = readl(&priv->reg->resp3); |
| 412 | cmd->response[1] = readl(&priv->reg->resp2); |
| 413 | cmd->response[2] = readl(&priv->reg->resp1); |
| 414 | cmd->response[3] = readl(&priv->reg->resp0); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 415 | debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n", |
| 416 | cmd->response[3], cmd->response[2], |
| 417 | cmd->response[1], cmd->response[0]); |
| 418 | } else { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 419 | cmd->response[0] = readl(&priv->reg->resp0); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 420 | debug("mmc resp 0x%08x\n", cmd->response[0]); |
| 421 | } |
| 422 | out: |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 423 | if (error < 0) { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 424 | writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl); |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 425 | mmc_update_clk(priv); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 426 | } |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 427 | writel(0xffffffff, &priv->reg->rint); |
| 428 | writel(readl(&priv->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET, |
| 429 | &priv->reg->gctrl); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 430 | |
| 431 | return error; |
| 432 | } |
| 433 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 434 | static int sunxi_mmc_set_ios_legacy(struct mmc *mmc) |
| 435 | { |
| 436 | struct sunxi_mmc_priv *priv = mmc->priv; |
| 437 | |
| 438 | return sunxi_mmc_set_ios_common(priv, mmc); |
| 439 | } |
| 440 | |
| 441 | static int sunxi_mmc_send_cmd_legacy(struct mmc *mmc, struct mmc_cmd *cmd, |
| 442 | struct mmc_data *data) |
| 443 | { |
| 444 | struct sunxi_mmc_priv *priv = mmc->priv; |
| 445 | |
| 446 | return sunxi_mmc_send_cmd_common(priv, mmc, cmd, data); |
| 447 | } |
| 448 | |
| 449 | static int sunxi_mmc_getcd_legacy(struct mmc *mmc) |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 450 | { |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 451 | struct sunxi_mmc_priv *priv = mmc->priv; |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 452 | int cd_pin; |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 453 | |
Simon Glass | 3f5af12 | 2017-07-04 13:31:24 -0600 | [diff] [blame] | 454 | cd_pin = sunxi_mmc_getcd_gpio(priv->mmc_no); |
Hans de Goede | 90641f8 | 2015-04-22 17:03:17 +0200 | [diff] [blame] | 455 | if (cd_pin < 0) |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 456 | return 1; |
| 457 | |
Axel Lin | b0c4ae1 | 2014-12-20 11:41:25 +0800 | [diff] [blame] | 458 | return !gpio_get_value(cd_pin); |
Hans de Goede | cd82113 | 2014-10-02 20:29:26 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 461 | static const struct mmc_ops sunxi_mmc_ops = { |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 462 | .send_cmd = sunxi_mmc_send_cmd_legacy, |
| 463 | .set_ios = sunxi_mmc_set_ios_legacy, |
Siarhei Siamashka | 5abdb15 | 2015-02-01 00:42:14 +0200 | [diff] [blame] | 464 | .init = sunxi_mmc_core_init, |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 465 | .getcd = sunxi_mmc_getcd_legacy, |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 466 | }; |
| 467 | |
Hans de Goede | e79c7c8 | 2014-10-02 21:13:54 +0200 | [diff] [blame] | 468 | struct mmc *sunxi_mmc_init(int sdc_no) |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 469 | { |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 470 | struct sunxi_mmc_priv *priv = &mmc_host[sdc_no]; |
| 471 | struct mmc_config *cfg = &priv->cfg; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 472 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 473 | memset(priv, '\0', sizeof(struct sunxi_mmc_priv)); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 474 | |
| 475 | cfg->name = "SUNXI SD/MMC"; |
| 476 | cfg->ops = &sunxi_mmc_ops; |
| 477 | |
| 478 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 479 | cfg->host_caps = MMC_MODE_4BIT; |
Maxime Ripard | fb01318 | 2016-11-04 16:18:09 +0100 | [diff] [blame] | 480 | #if defined(CONFIG_MACH_SUN50I) || defined(CONFIG_MACH_SUN8I) |
Siarhei Siamashka | d96ebc4 | 2016-03-29 17:29:10 +0200 | [diff] [blame] | 481 | if (sdc_no == 2) |
| 482 | cfg->host_caps = MMC_MODE_8BIT; |
| 483 | #endif |
Rob Herring | 5a20397 | 2015-03-23 17:56:59 -0500 | [diff] [blame] | 484 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 485 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 486 | |
| 487 | cfg->f_min = 400000; |
| 488 | cfg->f_max = 52000000; |
| 489 | |
Hans de Goede | 967325f | 2014-10-31 16:55:02 +0100 | [diff] [blame] | 490 | if (mmc_resource_init(sdc_no) != 0) |
| 491 | return NULL; |
| 492 | |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 493 | mmc_clk_io_on(sdc_no); |
| 494 | |
Simon Glass | 034e226 | 2017-07-04 13:31:25 -0600 | [diff] [blame^] | 495 | return mmc_create(cfg, mmc_host); |
Ian Campbell | e24ea55 | 2014-05-05 14:42:31 +0100 | [diff] [blame] | 496 | } |