Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Carlo Caione <carlo@caione.org> |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 8 | #include <fdtdec.h> |
| 9 | #include <malloc.h> |
Neil Armstrong | a10388d | 2019-10-11 17:33:52 +0200 | [diff] [blame] | 10 | #include <pwrseq.h> |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 11 | #include <mmc.h> |
| 12 | #include <asm/io.h> |
Neil Armstrong | a10388d | 2019-10-11 17:33:52 +0200 | [diff] [blame] | 13 | #include <asm/gpio.h> |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 14 | #include <asm/arch/sd_emmc.h> |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 15 | #include <linux/log2.h> |
| 16 | |
| 17 | static inline void *get_regbase(const struct mmc *mmc) |
| 18 | { |
| 19 | struct meson_mmc_platdata *pdata = mmc->priv; |
| 20 | |
| 21 | return pdata->regbase; |
| 22 | } |
| 23 | |
| 24 | static inline uint32_t meson_read(struct mmc *mmc, int offset) |
| 25 | { |
| 26 | return readl(get_regbase(mmc) + offset); |
| 27 | } |
| 28 | |
| 29 | static inline void meson_write(struct mmc *mmc, uint32_t val, int offset) |
| 30 | { |
| 31 | writel(val, get_regbase(mmc) + offset); |
| 32 | } |
| 33 | |
| 34 | static void meson_mmc_config_clock(struct mmc *mmc) |
| 35 | { |
| 36 | uint32_t meson_mmc_clk = 0; |
| 37 | unsigned int clk, clk_src, clk_div; |
| 38 | |
Heinrich Schuchardt | f6549c8 | 2018-03-17 22:49:36 +0000 | [diff] [blame] | 39 | if (!mmc->clock) |
| 40 | return; |
| 41 | |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 42 | /* 1GHz / CLK_MAX_DIV = 15,9 MHz */ |
| 43 | if (mmc->clock > 16000000) { |
| 44 | clk = SD_EMMC_CLKSRC_DIV2; |
| 45 | clk_src = CLK_SRC_DIV2; |
| 46 | } else { |
| 47 | clk = SD_EMMC_CLKSRC_24M; |
| 48 | clk_src = CLK_SRC_24M; |
| 49 | } |
| 50 | clk_div = DIV_ROUND_UP(clk, mmc->clock); |
| 51 | |
| 52 | /* 180 phase core clock */ |
| 53 | meson_mmc_clk |= CLK_CO_PHASE_180; |
| 54 | |
| 55 | /* 180 phase tx clock */ |
| 56 | meson_mmc_clk |= CLK_TX_PHASE_000; |
| 57 | |
| 58 | /* clock settings */ |
| 59 | meson_mmc_clk |= clk_src; |
| 60 | meson_mmc_clk |= clk_div; |
| 61 | |
| 62 | meson_write(mmc, meson_mmc_clk, MESON_SD_EMMC_CLOCK); |
| 63 | } |
| 64 | |
| 65 | static int meson_dm_mmc_set_ios(struct udevice *dev) |
| 66 | { |
| 67 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 68 | uint32_t meson_mmc_cfg; |
| 69 | |
| 70 | meson_mmc_config_clock(mmc); |
| 71 | |
| 72 | meson_mmc_cfg = meson_read(mmc, MESON_SD_EMMC_CFG); |
| 73 | |
| 74 | meson_mmc_cfg &= ~CFG_BUS_WIDTH_MASK; |
| 75 | if (mmc->bus_width == 1) |
| 76 | meson_mmc_cfg |= CFG_BUS_WIDTH_1; |
| 77 | else if (mmc->bus_width == 4) |
| 78 | meson_mmc_cfg |= CFG_BUS_WIDTH_4; |
| 79 | else if (mmc->bus_width == 8) |
| 80 | meson_mmc_cfg |= CFG_BUS_WIDTH_8; |
| 81 | else |
| 82 | return -EINVAL; |
| 83 | |
| 84 | /* 512 bytes block length */ |
| 85 | meson_mmc_cfg &= ~CFG_BL_LEN_MASK; |
| 86 | meson_mmc_cfg |= CFG_BL_LEN_512; |
| 87 | |
| 88 | /* Response timeout 256 clk */ |
| 89 | meson_mmc_cfg &= ~CFG_RESP_TIMEOUT_MASK; |
| 90 | meson_mmc_cfg |= CFG_RESP_TIMEOUT_256; |
| 91 | |
| 92 | /* Command-command gap 16 clk */ |
| 93 | meson_mmc_cfg &= ~CFG_RC_CC_MASK; |
| 94 | meson_mmc_cfg |= CFG_RC_CC_16; |
| 95 | |
| 96 | meson_write(mmc, meson_mmc_cfg, MESON_SD_EMMC_CFG); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void meson_mmc_setup_cmd(struct mmc *mmc, struct mmc_data *data, |
| 102 | struct mmc_cmd *cmd) |
| 103 | { |
| 104 | uint32_t meson_mmc_cmd = 0, cfg; |
| 105 | |
| 106 | meson_mmc_cmd |= cmd->cmdidx << CMD_CFG_CMD_INDEX_SHIFT; |
| 107 | |
| 108 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 109 | if (cmd->resp_type & MMC_RSP_136) |
| 110 | meson_mmc_cmd |= CMD_CFG_RESP_128; |
| 111 | |
| 112 | if (cmd->resp_type & MMC_RSP_BUSY) |
| 113 | meson_mmc_cmd |= CMD_CFG_R1B; |
| 114 | |
| 115 | if (!(cmd->resp_type & MMC_RSP_CRC)) |
| 116 | meson_mmc_cmd |= CMD_CFG_RESP_NOCRC; |
| 117 | } else { |
| 118 | meson_mmc_cmd |= CMD_CFG_NO_RESP; |
| 119 | } |
| 120 | |
| 121 | if (data) { |
| 122 | cfg = meson_read(mmc, MESON_SD_EMMC_CFG); |
| 123 | cfg &= ~CFG_BL_LEN_MASK; |
| 124 | cfg |= ilog2(data->blocksize) << CFG_BL_LEN_SHIFT; |
| 125 | meson_write(mmc, cfg, MESON_SD_EMMC_CFG); |
| 126 | |
| 127 | if (data->flags == MMC_DATA_WRITE) |
| 128 | meson_mmc_cmd |= CMD_CFG_DATA_WR; |
| 129 | |
| 130 | meson_mmc_cmd |= CMD_CFG_DATA_IO | CMD_CFG_BLOCK_MODE | |
| 131 | data->blocks; |
| 132 | } |
| 133 | |
| 134 | meson_mmc_cmd |= CMD_CFG_TIMEOUT_4S | CMD_CFG_OWNER | |
| 135 | CMD_CFG_END_OF_CHAIN; |
| 136 | |
| 137 | meson_write(mmc, meson_mmc_cmd, MESON_SD_EMMC_CMD_CFG); |
| 138 | } |
| 139 | |
| 140 | static void meson_mmc_setup_addr(struct mmc *mmc, struct mmc_data *data) |
| 141 | { |
| 142 | struct meson_mmc_platdata *pdata = mmc->priv; |
| 143 | unsigned int data_size; |
| 144 | uint32_t data_addr = 0; |
| 145 | |
| 146 | if (data) { |
| 147 | data_size = data->blocks * data->blocksize; |
| 148 | |
| 149 | if (data->flags == MMC_DATA_READ) { |
| 150 | data_addr = (ulong) data->dest; |
| 151 | invalidate_dcache_range(data_addr, |
| 152 | data_addr + data_size); |
| 153 | } else { |
| 154 | pdata->w_buf = calloc(data_size, sizeof(char)); |
| 155 | data_addr = (ulong) pdata->w_buf; |
| 156 | memcpy(pdata->w_buf, data->src, data_size); |
| 157 | flush_dcache_range(data_addr, data_addr + data_size); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | meson_write(mmc, data_addr, MESON_SD_EMMC_CMD_DAT); |
| 162 | } |
| 163 | |
| 164 | static void meson_mmc_read_response(struct mmc *mmc, struct mmc_cmd *cmd) |
| 165 | { |
| 166 | if (cmd->resp_type & MMC_RSP_136) { |
| 167 | cmd->response[0] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP3); |
| 168 | cmd->response[1] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP2); |
| 169 | cmd->response[2] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP1); |
| 170 | cmd->response[3] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP); |
| 171 | } else { |
| 172 | cmd->response[0] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | static int meson_dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 177 | struct mmc_data *data) |
| 178 | { |
| 179 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 180 | struct meson_mmc_platdata *pdata = mmc->priv; |
| 181 | uint32_t status; |
| 182 | ulong start; |
| 183 | int ret = 0; |
| 184 | |
| 185 | /* max block size supported by chip is 512 byte */ |
| 186 | if (data && data->blocksize > 512) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | meson_mmc_setup_cmd(mmc, data, cmd); |
| 190 | meson_mmc_setup_addr(mmc, data); |
| 191 | |
| 192 | meson_write(mmc, cmd->cmdarg, MESON_SD_EMMC_CMD_ARG); |
| 193 | |
| 194 | /* use 10s timeout */ |
| 195 | start = get_timer(0); |
| 196 | do { |
| 197 | status = meson_read(mmc, MESON_SD_EMMC_STATUS); |
| 198 | } while(!(status & STATUS_END_OF_CHAIN) && get_timer(start) < 10000); |
| 199 | |
| 200 | if (!(status & STATUS_END_OF_CHAIN)) |
| 201 | ret = -ETIMEDOUT; |
| 202 | else if (status & STATUS_RESP_TIMEOUT) |
| 203 | ret = -ETIMEDOUT; |
| 204 | else if (status & STATUS_ERR_MASK) |
| 205 | ret = -EIO; |
| 206 | |
| 207 | meson_mmc_read_response(mmc, cmd); |
| 208 | |
| 209 | if (data && data->flags == MMC_DATA_WRITE) |
| 210 | free(pdata->w_buf); |
| 211 | |
| 212 | /* reset status bits */ |
| 213 | meson_write(mmc, STATUS_MASK, MESON_SD_EMMC_STATUS); |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | static const struct dm_mmc_ops meson_dm_mmc_ops = { |
| 219 | .send_cmd = meson_dm_mmc_send_cmd, |
| 220 | .set_ios = meson_dm_mmc_set_ios, |
| 221 | }; |
| 222 | |
| 223 | static int meson_mmc_ofdata_to_platdata(struct udevice *dev) |
| 224 | { |
| 225 | struct meson_mmc_platdata *pdata = dev_get_platdata(dev); |
| 226 | fdt_addr_t addr; |
| 227 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 228 | addr = devfdt_get_addr(dev); |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 229 | if (addr == FDT_ADDR_T_NONE) |
| 230 | return -EINVAL; |
| 231 | |
| 232 | pdata->regbase = (void *)addr; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static int meson_mmc_probe(struct udevice *dev) |
| 238 | { |
| 239 | struct meson_mmc_platdata *pdata = dev_get_platdata(dev); |
| 240 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 241 | struct mmc *mmc = &pdata->mmc; |
| 242 | struct mmc_config *cfg = &pdata->cfg; |
| 243 | uint32_t val; |
Neil Armstrong | a10388d | 2019-10-11 17:33:52 +0200 | [diff] [blame] | 244 | #ifdef CONFIG_PWRSEQ |
| 245 | struct udevice *pwr_dev; |
| 246 | int ret; |
| 247 | #endif |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 248 | |
| 249 | cfg->voltages = MMC_VDD_33_34 | MMC_VDD_32_33 | |
| 250 | MMC_VDD_31_32 | MMC_VDD_165_195; |
| 251 | cfg->host_caps = MMC_MODE_8BIT | MMC_MODE_4BIT | |
| 252 | MMC_MODE_HS_52MHz | MMC_MODE_HS; |
| 253 | cfg->f_min = DIV_ROUND_UP(SD_EMMC_CLKSRC_24M, CLK_MAX_DIV); |
| 254 | cfg->f_max = 100000000; /* 100 MHz */ |
Heiner Kallweit | f98205c | 2017-04-14 10:10:19 +0200 | [diff] [blame] | 255 | cfg->b_max = 511; /* max 512 - 1 blocks */ |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 256 | cfg->name = dev->name; |
| 257 | |
| 258 | mmc->priv = pdata; |
| 259 | upriv->mmc = mmc; |
| 260 | |
Jaehoon Chung | 6511718 | 2018-01-26 19:25:29 +0900 | [diff] [blame] | 261 | mmc_set_clock(mmc, cfg->f_min, MMC_CLK_ENABLE); |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 262 | |
Neil Armstrong | a10388d | 2019-10-11 17:33:52 +0200 | [diff] [blame] | 263 | #ifdef CONFIG_PWRSEQ |
| 264 | /* Enable power if needed */ |
| 265 | ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq", |
| 266 | &pwr_dev); |
| 267 | if (!ret) { |
| 268 | ret = pwrseq_set_power(pwr_dev, true); |
| 269 | if (ret) |
| 270 | return ret; |
| 271 | } |
| 272 | #endif |
| 273 | |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 274 | /* reset all status bits */ |
| 275 | meson_write(mmc, STATUS_MASK, MESON_SD_EMMC_STATUS); |
| 276 | |
| 277 | /* disable interrupts */ |
| 278 | meson_write(mmc, 0, MESON_SD_EMMC_IRQ_EN); |
| 279 | |
| 280 | /* enable auto clock mode */ |
| 281 | val = meson_read(mmc, MESON_SD_EMMC_CFG); |
| 282 | val &= ~CFG_SDCLK_ALWAYS_ON; |
| 283 | val |= CFG_AUTO_CLK; |
| 284 | meson_write(mmc, val, MESON_SD_EMMC_CFG); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | int meson_mmc_bind(struct udevice *dev) |
| 290 | { |
| 291 | struct meson_mmc_platdata *pdata = dev_get_platdata(dev); |
| 292 | |
| 293 | return mmc_bind(dev, &pdata->mmc, &pdata->cfg); |
| 294 | } |
| 295 | |
| 296 | static const struct udevice_id meson_mmc_match[] = { |
| 297 | { .compatible = "amlogic,meson-gx-mmc" }, |
Neil Armstrong | 3e3f701 | 2018-09-10 16:43:46 +0200 | [diff] [blame] | 298 | { .compatible = "amlogic,meson-axg-mmc" }, |
Carlo Caione | 9373862 | 2017-04-12 20:30:42 +0200 | [diff] [blame] | 299 | { /* sentinel */ } |
| 300 | }; |
| 301 | |
| 302 | U_BOOT_DRIVER(meson_mmc) = { |
| 303 | .name = "meson_gx_mmc", |
| 304 | .id = UCLASS_MMC, |
| 305 | .of_match = meson_mmc_match, |
| 306 | .ops = &meson_dm_mmc_ops, |
| 307 | .probe = meson_mmc_probe, |
| 308 | .bind = meson_mmc_bind, |
| 309 | .ofdata_to_platdata = meson_mmc_ofdata_to_platdata, |
| 310 | .platdata_auto_alloc_size = sizeof(struct meson_mmc_platdata), |
| 311 | }; |
Neil Armstrong | a10388d | 2019-10-11 17:33:52 +0200 | [diff] [blame] | 312 | |
| 313 | #ifdef CONFIG_PWRSEQ |
| 314 | static int meson_mmc_pwrseq_set_power(struct udevice *dev, bool enable) |
| 315 | { |
| 316 | struct gpio_desc reset; |
| 317 | int ret; |
| 318 | |
| 319 | ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT); |
| 320 | if (ret) |
| 321 | return ret; |
| 322 | dm_gpio_set_value(&reset, 1); |
| 323 | udelay(1); |
| 324 | dm_gpio_set_value(&reset, 0); |
| 325 | udelay(200); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static const struct pwrseq_ops meson_mmc_pwrseq_ops = { |
| 331 | .set_power = meson_mmc_pwrseq_set_power, |
| 332 | }; |
| 333 | |
| 334 | static const struct udevice_id meson_mmc_pwrseq_ids[] = { |
| 335 | { .compatible = "mmc-pwrseq-emmc" }, |
| 336 | { } |
| 337 | }; |
| 338 | |
| 339 | U_BOOT_DRIVER(meson_mmc_pwrseq_drv) = { |
| 340 | .name = "mmc_pwrseq_emmc", |
| 341 | .id = UCLASS_PWRSEQ, |
| 342 | .of_match = meson_mmc_pwrseq_ids, |
| 343 | .ops = &meson_mmc_pwrseq_ops, |
| 344 | }; |
| 345 | #endif |