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