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