blob: 7d169efa476caa83ea8c1a04c759e8187211ca67 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadadcbc4432016-12-30 22:41:46 +09002/*
3 * Copyright (C) 2016 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadadcbc4432016-12-30 22:41:46 +09005 */
6
Simon Glass11c89f32017-05-17 17:18:03 -06007#include <dm.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06008#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <dm/device_compat.h>
Masahiro Yamada053ceeb2017-12-30 02:00:09 +090010#include <linux/bitfield.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060012#include <linux/bug.h>
Masahiro Yamadadcbc4432016-12-30 22:41:46 +090013#include <linux/io.h>
Masahiro Yamada89b93912017-05-09 15:52:04 +090014#include <linux/iopoll.h>
Masahiro Yamadadcbc4432016-12-30 22:41:46 +090015#include <linux/sizes.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090016#include <linux/libfdt.h>
Masahiro Yamadadcbc4432016-12-30 22:41:46 +090017#include <mmc.h>
18#include <sdhci.h>
Kuan Lim Lee93986cb2023-11-28 14:38:30 +080019#include "sdhci-cadence.h"
Masahiro Yamadadcbc4432016-12-30 22:41:46 +090020
Masahiro Yamada89b93912017-05-09 15:52:04 +090021struct sdhci_cdns_phy_cfg {
22 const char *property;
23 u8 addr;
24};
25
26static const struct sdhci_cdns_phy_cfg sdhci_cdns_phy_cfgs[] = {
27 { "cdns,phy-input-delay-sd-highspeed", SDHCI_CDNS_PHY_DLY_SD_HS, },
28 { "cdns,phy-input-delay-legacy", SDHCI_CDNS_PHY_DLY_SD_DEFAULT, },
29 { "cdns,phy-input-delay-sd-uhs-sdr12", SDHCI_CDNS_PHY_DLY_UHS_SDR12, },
30 { "cdns,phy-input-delay-sd-uhs-sdr25", SDHCI_CDNS_PHY_DLY_UHS_SDR25, },
31 { "cdns,phy-input-delay-sd-uhs-sdr50", SDHCI_CDNS_PHY_DLY_UHS_SDR50, },
32 { "cdns,phy-input-delay-sd-uhs-ddr50", SDHCI_CDNS_PHY_DLY_UHS_DDR50, },
33 { "cdns,phy-input-delay-mmc-highspeed", SDHCI_CDNS_PHY_DLY_EMMC_SDR, },
34 { "cdns,phy-input-delay-mmc-ddr", SDHCI_CDNS_PHY_DLY_EMMC_DDR, },
35 { "cdns,phy-dll-delay-sdclk", SDHCI_CDNS_PHY_DLY_SDCLK, },
36 { "cdns,phy-dll-delay-sdclk-hsmmc", SDHCI_CDNS_PHY_DLY_HSMMC, },
37 { "cdns,phy-dll-delay-strobe", SDHCI_CDNS_PHY_DLY_STROBE, },
38};
39
40static int sdhci_cdns_write_phy_reg(struct sdhci_cdns_plat *plat,
41 u8 addr, u8 data)
Masahiro Yamadadcbc4432016-12-30 22:41:46 +090042{
43 void __iomem *reg = plat->hrs_addr + SDHCI_CDNS_HRS04;
44 u32 tmp;
Masahiro Yamada89b93912017-05-09 15:52:04 +090045 int ret;
Masahiro Yamadadcbc4432016-12-30 22:41:46 +090046
Masahiro Yamada053ceeb2017-12-30 02:00:09 +090047 tmp = FIELD_PREP(SDHCI_CDNS_HRS04_WDATA, data) |
48 FIELD_PREP(SDHCI_CDNS_HRS04_ADDR, addr);
Masahiro Yamadadcbc4432016-12-30 22:41:46 +090049 writel(tmp, reg);
50
51 tmp |= SDHCI_CDNS_HRS04_WR;
52 writel(tmp, reg);
53
Masahiro Yamada89b93912017-05-09 15:52:04 +090054 ret = readl_poll_timeout(reg, tmp, tmp & SDHCI_CDNS_HRS04_ACK, 10);
55 if (ret)
56 return ret;
57
Masahiro Yamadadcbc4432016-12-30 22:41:46 +090058 tmp &= ~SDHCI_CDNS_HRS04_WR;
59 writel(tmp, reg);
Masahiro Yamada89b93912017-05-09 15:52:04 +090060
61 return 0;
Masahiro Yamadadcbc4432016-12-30 22:41:46 +090062}
63
Masahiro Yamada89b93912017-05-09 15:52:04 +090064static int sdhci_cdns_phy_init(struct sdhci_cdns_plat *plat,
65 const void *fdt, int nodeoffset)
Masahiro Yamadadcbc4432016-12-30 22:41:46 +090066{
Masahiro Yamada959e9072017-06-22 17:58:09 +090067 const fdt32_t *prop;
Masahiro Yamada89b93912017-05-09 15:52:04 +090068 int ret, i;
69
70 for (i = 0; i < ARRAY_SIZE(sdhci_cdns_phy_cfgs); i++) {
71 prop = fdt_getprop(fdt, nodeoffset,
72 sdhci_cdns_phy_cfgs[i].property, NULL);
73 if (!prop)
74 continue;
75
76 ret = sdhci_cdns_write_phy_reg(plat,
77 sdhci_cdns_phy_cfgs[i].addr,
78 fdt32_to_cpu(*prop));
79 if (ret)
80 return ret;
81 }
82
83 return 0;
Masahiro Yamadadcbc4432016-12-30 22:41:46 +090084}
85
Masahiro Yamadaa1c702b2017-09-28 21:13:10 +090086static void sdhci_cdns_set_control_reg(struct sdhci_host *host)
87{
88 struct mmc *mmc = host->mmc;
Simon Glassfa20e932020-12-03 16:55:20 -070089 struct sdhci_cdns_plat *plat = dev_get_plat(mmc->dev);
Masahiro Yamadaa1c702b2017-09-28 21:13:10 +090090 unsigned int clock = mmc->clock;
91 u32 mode, tmp;
92
93 /*
94 * REVISIT:
95 * The mode should be decided by MMC_TIMING_* like Linux, but
96 * U-Boot does not support timing. Use the clock frequency instead.
97 */
Masahiro Yamada1a0cb672018-01-12 18:10:38 +090098 if (clock <= 26000000) {
Masahiro Yamadaa1c702b2017-09-28 21:13:10 +090099 mode = SDHCI_CDNS_HRS06_MODE_SD; /* use this for Legacy */
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900100 } else if (clock <= 52000000) {
Masahiro Yamadaa1c702b2017-09-28 21:13:10 +0900101 if (mmc->ddr_mode)
102 mode = SDHCI_CDNS_HRS06_MODE_MMC_DDR;
103 else
104 mode = SDHCI_CDNS_HRS06_MODE_MMC_SDR;
105 } else {
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900106 if (mmc->ddr_mode)
107 mode = SDHCI_CDNS_HRS06_MODE_MMC_HS400;
108 else
109 mode = SDHCI_CDNS_HRS06_MODE_MMC_HS200;
Masahiro Yamadaa1c702b2017-09-28 21:13:10 +0900110 }
111
112 tmp = readl(plat->hrs_addr + SDHCI_CDNS_HRS06);
Masahiro Yamada053ceeb2017-12-30 02:00:09 +0900113 tmp &= ~SDHCI_CDNS_HRS06_MODE;
114 tmp |= FIELD_PREP(SDHCI_CDNS_HRS06_MODE, mode);
Masahiro Yamadaa1c702b2017-09-28 21:13:10 +0900115 writel(tmp, plat->hrs_addr + SDHCI_CDNS_HRS06);
Kuan Lim Lee93986cb2023-11-28 14:38:30 +0800116
117 if (device_is_compatible(mmc->dev, "cdns,sd6hc"))
118 sdhci_cdns6_phy_adj(mmc->dev, plat, mode);
Masahiro Yamadaa1c702b2017-09-28 21:13:10 +0900119}
120
121static const struct sdhci_ops sdhci_cdns_ops = {
122 .set_control_reg = sdhci_cdns_set_control_reg,
123};
124
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900125static int sdhci_cdns_set_tune_val(struct sdhci_cdns_plat *plat,
126 unsigned int val)
127{
128 void __iomem *reg = plat->hrs_addr + SDHCI_CDNS_HRS06;
129 u32 tmp;
Masahiro Yamada55f7b002020-01-21 18:42:05 +0900130 int i, ret;
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900131
Kuan Lim Lee93986cb2023-11-28 14:38:30 +0800132 if (device_is_compatible(plat->mmc.dev, "cdns,sd6hc"))
133 return sdhci_cdns6_set_tune_val(plat, val);
134
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900135 if (WARN_ON(!FIELD_FIT(SDHCI_CDNS_HRS06_TUNE, val)))
136 return -EINVAL;
137
138 tmp = readl(reg);
139 tmp &= ~SDHCI_CDNS_HRS06_TUNE;
140 tmp |= FIELD_PREP(SDHCI_CDNS_HRS06_TUNE, val);
Masahiro Yamada55f7b002020-01-21 18:42:05 +0900141
142 /*
143 * Workaround for IP errata:
144 * The IP6116 SD/eMMC PHY design has a timing issue on receive data
145 * path. Send tune request twice.
146 */
147 for (i = 0; i < 2; i++) {
148 tmp |= SDHCI_CDNS_HRS06_TUNE_UP;
149 writel(tmp, reg);
150
151 ret = readl_poll_timeout(reg, tmp,
152 !(tmp & SDHCI_CDNS_HRS06_TUNE_UP), 1);
153 if (ret)
154 return ret;
155 }
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900156
Masahiro Yamada55f7b002020-01-21 18:42:05 +0900157 return 0;
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900158}
159
160static int __maybe_unused sdhci_cdns_execute_tuning(struct udevice *dev,
161 unsigned int opcode)
162{
Simon Glassfa20e932020-12-03 16:55:20 -0700163 struct sdhci_cdns_plat *plat = dev_get_plat(dev);
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900164 struct mmc *mmc = &plat->mmc;
165 int cur_streak = 0;
166 int max_streak = 0;
167 int end_of_streak = 0;
168 int i;
169
170 /*
171 * This handler only implements the eMMC tuning that is specific to
172 * this controller. The tuning for SD timing should be handled by the
173 * SDHCI core.
174 */
175 if (!IS_MMC(mmc))
176 return -ENOTSUPP;
177
178 if (WARN_ON(opcode != MMC_CMD_SEND_TUNING_BLOCK_HS200))
179 return -EINVAL;
180
181 for (i = 0; i < SDHCI_CDNS_MAX_TUNING_LOOP; i++) {
182 if (sdhci_cdns_set_tune_val(plat, i) ||
Marek Vasutdad81fb2024-02-20 09:36:23 +0100183 mmc_send_tuning(mmc, opcode)) { /* bad */
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900184 cur_streak = 0;
185 } else { /* good */
186 cur_streak++;
187 if (cur_streak > max_streak) {
188 max_streak = cur_streak;
189 end_of_streak = i;
190 }
191 }
192 }
193
194 if (!max_streak) {
195 dev_err(dev, "no tuning point found\n");
196 return -EIO;
197 }
198
199 return sdhci_cdns_set_tune_val(plat, end_of_streak - max_streak / 2);
200}
201
202static struct dm_mmc_ops sdhci_cdns_mmc_ops;
203
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900204static int sdhci_cdns_bind(struct udevice *dev)
205{
Simon Glassfa20e932020-12-03 16:55:20 -0700206 struct sdhci_cdns_plat *plat = dev_get_plat(dev);
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900207
208 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
209}
210
211static int sdhci_cdns_probe(struct udevice *dev)
212{
Masahiro Yamada89b93912017-05-09 15:52:04 +0900213 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900214 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700215 struct sdhci_cdns_plat *plat = dev_get_plat(dev);
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900216 struct sdhci_host *host = dev_get_priv(dev);
217 fdt_addr_t base;
218 int ret;
219
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900220 base = dev_read_addr(dev);
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900221 if (base == FDT_ADDR_T_NONE)
222 return -EINVAL;
223
224 plat->hrs_addr = devm_ioremap(dev, base, SZ_1K);
225 if (!plat->hrs_addr)
226 return -ENOMEM;
227
228 host->name = dev->name;
229 host->ioaddr = plat->hrs_addr + SDHCI_CDNS_SRS_BASE;
Masahiro Yamadaa1c702b2017-09-28 21:13:10 +0900230 host->ops = &sdhci_cdns_ops;
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900231 host->quirks |= SDHCI_QUIRK_WAIT_SEND_CMD;
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900232 sdhci_cdns_mmc_ops = sdhci_ops;
Tom Rinidec7ea02024-05-20 13:35:03 -0600233#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900234 sdhci_cdns_mmc_ops.execute_tuning = sdhci_cdns_execute_tuning;
235#endif
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900236
Masahiro Yamadad80ca272017-12-30 02:00:10 +0900237 ret = mmc_of_parse(dev, &plat->cfg);
238 if (ret)
239 return ret;
240
Kuan Lim Lee93986cb2023-11-28 14:38:30 +0800241 if (device_is_compatible(dev, "cdns,sd6hc"))
242 ret = sdhci_cdns6_phy_init(dev, plat);
243 else
244 ret = sdhci_cdns_phy_init(plat, gd->fdt_blob, dev_of_offset(dev));
Masahiro Yamada89b93912017-05-09 15:52:04 +0900245 if (ret)
246 return ret;
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900247
Peng Fan41ec1c12019-08-06 02:48:02 +0000248 host->mmc = &plat->mmc;
249 host->mmc->dev = dev;
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900250 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
251 if (ret)
252 return ret;
253
254 upriv->mmc = &plat->mmc;
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900255 host->mmc->priv = host;
256
257 return sdhci_probe(dev);
258}
259
260static const struct udevice_id sdhci_cdns_match[] = {
261 { .compatible = "socionext,uniphier-sd4hc" },
262 { .compatible = "cdns,sd4hc" },
Kuan Lim Lee93986cb2023-11-28 14:38:30 +0800263 { .compatible = "cdns,sd6hc" },
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900264 { /* sentinel */ }
265};
266
267U_BOOT_DRIVER(sdhci_cdns) = {
268 .name = "sdhci-cdns",
269 .id = UCLASS_MMC,
270 .of_match = sdhci_cdns_match,
271 .bind = sdhci_cdns_bind,
272 .probe = sdhci_cdns_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700273 .priv_auto = sizeof(struct sdhci_host),
Simon Glass71fa5b42020-12-03 16:55:18 -0700274 .plat_auto = sizeof(struct sdhci_cdns_plat),
Masahiro Yamada1a0cb672018-01-12 18:10:38 +0900275 .ops = &sdhci_cdns_mmc_ops,
Masahiro Yamadadcbc4432016-12-30 22:41:46 +0900276};