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