| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (C) 2023-2025 SpacemiT (Hangzhou) Technology Co. Ltd |
| 4 |
* Copyright (c) 2025 Yixun Lan <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/bitfield.h> |
| 8 |
#include <linux/clk.h> |
| 9 |
#include <linux/delay.h> |
| 10 |
#include <linux/iopoll.h> |
| 11 |
#include <linux/init.h> |
| 12 |
#include <linux/mmc/card.h> |
| 13 |
#include <linux/mmc/host.h> |
| 14 |
#include <linux/mmc/mmc.h> |
| 15 |
#include <linux/module.h> |
| 16 |
#include <linux/of.h> |
| 17 |
#include <linux/of_device.h> |
| 18 |
#include <linux/reset.h> |
| 19 |
#include <linux/platform_device.h> |
| 20 |
|
| 21 |
#include "sdhci.h" |
| 22 |
#include "sdhci-pltfm.h" |
| 23 |
|
| 24 |
#define SPACEMIT_SDHC_MMC_CTRL_REG 0x114 |
| 25 |
#define SDHC_MISC_INT_EN BIT(1) |
| 26 |
#define SDHC_MISC_INT BIT(2) |
| 27 |
#define SDHC_ENHANCE_STROBE_EN BIT(8) |
| 28 |
#define SDHC_MMC_HS400 BIT(9) |
| 29 |
#define SDHC_MMC_HS200 BIT(10) |
| 30 |
#define SDHC_MMC_CARD_MODE BIT(12) |
| 31 |
|
| 32 |
#define SPACEMIT_SDHC_TX_CFG_REG 0x11C |
| 33 |
#define SDHC_TX_INT_CLK_SEL BIT(30) |
| 34 |
#define SDHC_TX_MUX_SEL BIT(31) |
| 35 |
|
| 36 |
#define SPACEMIT_SDHC_PHY_CTRL_REG 0x160 |
| 37 |
#define SDHC_PHY_FUNC_EN BIT(0) |
| 38 |
#define SDHC_PHY_PLL_LOCK BIT(1) |
| 39 |
#define SDHC_HOST_LEGACY_MODE BIT(31) |
| 40 |
|
| 41 |
#define SPACEMIT_SDHC_PHY_FUNC_REG 0x164 |
| 42 |
#define SDHC_PHY_TEST_EN BIT(7) |
| 43 |
#define SDHC_HS200_USE_RFIFO BIT(15) |
| 44 |
|
| 45 |
#define SPACEMIT_SDHC_PHY_DLLCFG 0x168 |
| 46 |
#define SDHC_DLL_PREDLY_NUM GENMASK(3, 2) |
| 47 |
#define SDHC_DLL_FULLDLY_RANGE GENMASK(5, 4) |
| 48 |
#define SDHC_DLL_VREG_CTRL GENMASK(7, 6) |
| 49 |
#define SDHC_DLL_ENABLE BIT(31) |
| 50 |
|
| 51 |
#define SPACEMIT_SDHC_PHY_DLLCFG1 0x16C |
| 52 |
#define SDHC_DLL_REG1_CTRL GENMASK(7, 0) |
| 53 |
#define SDHC_DLL_REG2_CTRL GENMASK(15, 8) |
| 54 |
#define SDHC_DLL_REG3_CTRL GENMASK(23, 16) |
| 55 |
#define SDHC_DLL_REG4_CTRL GENMASK(31, 24) |
| 56 |
|
| 57 |
#define SPACEMIT_SDHC_PHY_DLLSTS 0x170 |
| 58 |
#define SDHC_DLL_LOCK_STATE BIT(0) |
| 59 |
|
| 60 |
#define SPACEMIT_SDHC_PHY_PADCFG_REG 0x178 |
| 61 |
#define SDHC_PHY_DRIVE_SEL GENMASK(2, 0) |
| 62 |
#define SDHC_RX_BIAS_CTRL BIT(5) |
| 63 |
|
| 64 |
struct spacemit_sdhci_host { |
| 65 |
struct clk *clk_core; |
| 66 |
struct clk *clk_io; |
| 67 |
}; |
| 68 |
|
| 69 |
/* All helper functions will update clr/set while preserve rest bits */ |
| 70 |
static inline void spacemit_sdhci_setbits(struct sdhci_host *host, u32 val, int reg) |
| 71 |
{ |
| 72 |
sdhci_writel(host, sdhci_readl(host, reg) | val, reg); |
| 73 |
} |
| 74 |
|
| 75 |
static inline void spacemit_sdhci_clrbits(struct sdhci_host *host, u32 val, int reg) |
| 76 |
{ |
| 77 |
sdhci_writel(host, sdhci_readl(host, reg) & ~val, reg); |
| 78 |
} |
| 79 |
|
| 80 |
static inline void spacemit_sdhci_clrsetbits(struct sdhci_host *host, u32 clr, u32 set, int reg) |
| 81 |
{ |
| 82 |
u32 val = sdhci_readl(host, reg); |
| 83 |
|
| 84 |
val = (val & ~clr) | set; |
| 85 |
sdhci_writel(host, val, reg); |
| 86 |
} |
| 87 |
|
| 88 |
static void spacemit_sdhci_reset(struct sdhci_host *host, u8 mask) |
| 89 |
{ |
| 90 |
sdhci_reset(host, mask); |
| 91 |
|
| 92 |
if (mask != SDHCI_RESET_ALL) |
| 93 |
return; |
| 94 |
|
| 95 |
spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK, |
| 96 |
SPACEMIT_SDHC_PHY_CTRL_REG); |
| 97 |
|
| 98 |
spacemit_sdhci_clrsetbits(host, SDHC_PHY_DRIVE_SEL, |
| 99 |
SDHC_RX_BIAS_CTRL | FIELD_PREP(SDHC_PHY_DRIVE_SEL, 4), |
| 100 |
SPACEMIT_SDHC_PHY_PADCFG_REG); |
| 101 |
|
| 102 |
if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC)) |
| 103 |
spacemit_sdhci_setbits(host, SDHC_MMC_CARD_MODE, SPACEMIT_SDHC_MMC_CTRL_REG); |
| 104 |
} |
| 105 |
|
| 106 |
static void spacemit_sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned int timing) |
| 107 |
{ |
| 108 |
if (timing == MMC_TIMING_MMC_HS200) |
| 109 |
spacemit_sdhci_setbits(host, SDHC_MMC_HS200, SPACEMIT_SDHC_MMC_CTRL_REG); |
| 110 |
|
| 111 |
if (timing == MMC_TIMING_MMC_HS400) |
| 112 |
spacemit_sdhci_setbits(host, SDHC_MMC_HS400, SPACEMIT_SDHC_MMC_CTRL_REG); |
| 113 |
|
| 114 |
sdhci_set_uhs_signaling(host, timing); |
| 115 |
|
| 116 |
if (!(host->mmc->caps2 & MMC_CAP2_NO_SDIO)) |
| 117 |
spacemit_sdhci_setbits(host, SDHCI_CTRL_VDD_180, SDHCI_HOST_CONTROL2); |
| 118 |
} |
| 119 |
|
| 120 |
static void spacemit_sdhci_set_clock(struct sdhci_host *host, unsigned int clock) |
| 121 |
{ |
| 122 |
struct mmc_host *mmc = host->mmc; |
| 123 |
|
| 124 |
if (mmc->ios.timing <= MMC_TIMING_UHS_SDR50) |
| 125 |
spacemit_sdhci_setbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG); |
| 126 |
else |
| 127 |
spacemit_sdhci_clrbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG); |
| 128 |
|
| 129 |
sdhci_set_clock(host, clock); |
| 130 |
}; |
| 131 |
|
| 132 |
static void spacemit_sdhci_phy_dll_init(struct sdhci_host *host) |
| 133 |
{ |
| 134 |
u32 state; |
| 135 |
int ret; |
| 136 |
|
| 137 |
spacemit_sdhci_clrsetbits(host, SDHC_DLL_PREDLY_NUM | |
| 138 |
SDHC_DLL_FULLDLY_RANGE | |
| 139 |
SDHC_DLL_VREG_CTRL, |
| 140 |
FIELD_PREP(SDHC_DLL_PREDLY_NUM, 1) | |
| 141 |
FIELD_PREP(SDHC_DLL_FULLDLY_RANGE, 1) | |
| 142 |
FIELD_PREP(SDHC_DLL_VREG_CTRL, 1), |
| 143 |
SPACEMIT_SDHC_PHY_DLLCFG); |
| 144 |
|
| 145 |
spacemit_sdhci_clrsetbits(host, SDHC_DLL_REG1_CTRL, |
| 146 |
FIELD_PREP(SDHC_DLL_REG1_CTRL, 0x92), |
| 147 |
SPACEMIT_SDHC_PHY_DLLCFG1); |
| 148 |
|
| 149 |
spacemit_sdhci_setbits(host, SDHC_DLL_ENABLE, SPACEMIT_SDHC_PHY_DLLCFG); |
| 150 |
|
| 151 |
ret = readl_poll_timeout(host->ioaddr + SPACEMIT_SDHC_PHY_DLLSTS, state, |
| 152 |
state & SDHC_DLL_LOCK_STATE, 2, 100); |
| 153 |
if (ret == -ETIMEDOUT) |
| 154 |
dev_warn(mmc_dev(host->mmc), "fail to lock phy dll in 100us!\n"); |
| 155 |
} |
| 156 |
|
| 157 |
static void spacemit_sdhci_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios) |
| 158 |
{ |
| 159 |
struct sdhci_host *host = mmc_priv(mmc); |
| 160 |
|
| 161 |
if (!ios->enhanced_strobe) { |
| 162 |
spacemit_sdhci_clrbits(host, SDHC_ENHANCE_STROBE_EN, SPACEMIT_SDHC_MMC_CTRL_REG); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
spacemit_sdhci_setbits(host, SDHC_ENHANCE_STROBE_EN, SPACEMIT_SDHC_MMC_CTRL_REG); |
| 167 |
spacemit_sdhci_phy_dll_init(host); |
| 168 |
} |
| 169 |
|
| 170 |
static unsigned int spacemit_sdhci_clk_get_max_clock(struct sdhci_host *host) |
| 171 |
{ |
| 172 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 173 |
|
| 174 |
return clk_get_rate(pltfm_host->clk); |
| 175 |
} |
| 176 |
|
| 177 |
static int spacemit_sdhci_pre_select_hs400(struct mmc_host *mmc) |
| 178 |
{ |
| 179 |
struct sdhci_host *host = mmc_priv(mmc); |
| 180 |
|
| 181 |
spacemit_sdhci_setbits(host, SDHC_MMC_HS400, SPACEMIT_SDHC_MMC_CTRL_REG); |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
static void spacemit_sdhci_post_select_hs400(struct mmc_host *mmc) |
| 187 |
{ |
| 188 |
struct sdhci_host *host = mmc_priv(mmc); |
| 189 |
|
| 190 |
spacemit_sdhci_phy_dll_init(host); |
| 191 |
} |
| 192 |
|
| 193 |
static void spacemit_sdhci_pre_hs400_to_hs200(struct mmc_host *mmc) |
| 194 |
{ |
| 195 |
struct sdhci_host *host = mmc_priv(mmc); |
| 196 |
|
| 197 |
spacemit_sdhci_clrbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK, |
| 198 |
SPACEMIT_SDHC_PHY_CTRL_REG); |
| 199 |
spacemit_sdhci_clrbits(host, SDHC_MMC_HS400 | SDHC_MMC_HS200 | SDHC_ENHANCE_STROBE_EN, |
| 200 |
SPACEMIT_SDHC_MMC_CTRL_REG); |
| 201 |
spacemit_sdhci_clrbits(host, SDHC_HS200_USE_RFIFO, SPACEMIT_SDHC_PHY_FUNC_REG); |
| 202 |
|
| 203 |
udelay(5); |
| 204 |
|
| 205 |
spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK, |
| 206 |
SPACEMIT_SDHC_PHY_CTRL_REG); |
| 207 |
} |
| 208 |
|
| 209 |
static inline int spacemit_sdhci_get_clocks(struct device *dev, |
| 210 |
struct sdhci_pltfm_host *pltfm_host) |
| 211 |
{ |
| 212 |
struct spacemit_sdhci_host *sdhst = sdhci_pltfm_priv(pltfm_host); |
| 213 |
|
| 214 |
sdhst->clk_core = devm_clk_get_enabled(dev, "core"); |
| 215 |
if (IS_ERR(sdhst->clk_core)) |
| 216 |
return -EINVAL; |
| 217 |
|
| 218 |
sdhst->clk_io = devm_clk_get_enabled(dev, "io"); |
| 219 |
if (IS_ERR(sdhst->clk_io)) |
| 220 |
return -EINVAL; |
| 221 |
|
| 222 |
pltfm_host->clk = sdhst->clk_io; |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
static inline int spacemit_sdhci_get_resets(struct device *dev) |
| 228 |
{ |
| 229 |
struct reset_control *rst; |
| 230 |
|
| 231 |
rst = devm_reset_control_get_optional_shared_deasserted(dev, "axi"); |
| 232 |
if (IS_ERR(rst)) |
| 233 |
return PTR_ERR(rst); |
| 234 |
|
| 235 |
rst = devm_reset_control_get_optional_exclusive_deasserted(dev, "sdh"); |
| 236 |
if (IS_ERR(rst)) |
| 237 |
return PTR_ERR(rst); |
| 238 |
|
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
static const struct sdhci_ops spacemit_sdhci_ops = { |
| 243 |
.get_max_clock = spacemit_sdhci_clk_get_max_clock, |
| 244 |
.reset = spacemit_sdhci_reset, |
| 245 |
.set_bus_width = sdhci_set_bus_width, |
| 246 |
.set_clock = spacemit_sdhci_set_clock, |
| 247 |
.set_uhs_signaling = spacemit_sdhci_set_uhs_signaling, |
| 248 |
}; |
| 249 |
|
| 250 |
static const struct sdhci_pltfm_data spacemit_sdhci_k1_pdata = { |
| 251 |
.ops = &spacemit_sdhci_ops, |
| 252 |
.quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK | |
| 253 |
SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | |
| 254 |
SDHCI_QUIRK_32BIT_ADMA_SIZE | |
| 255 |
SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
| 256 |
SDHCI_QUIRK_BROKEN_CARD_DETECTION | |
| 257 |
SDHCI_QUIRK_BROKEN_TIMEOUT_VAL, |
| 258 |
.quirks2 = SDHCI_QUIRK2_BROKEN_64_BIT_DMA | |
| 259 |
SDHCI_QUIRK2_PRESET_VALUE_BROKEN, |
| 260 |
}; |
| 261 |
|
| 262 |
static const struct sdhci_pltfm_data spacemit_sdhci_k3_pdata = { |
| 263 |
.ops = &spacemit_sdhci_ops, |
| 264 |
.quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK | |
| 265 |
SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | |
| 266 |
SDHCI_QUIRK_32BIT_ADMA_SIZE | |
| 267 |
SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
| 268 |
SDHCI_QUIRK_BROKEN_CARD_DETECTION | |
| 269 |
SDHCI_QUIRK_BROKEN_TIMEOUT_VAL, |
| 270 |
.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, |
| 271 |
}; |
| 272 |
|
| 273 |
static const struct of_device_id spacemit_sdhci_of_match[] = { |
| 274 |
{ .compatible = "spacemit,k1-sdhci", .data = &spacemit_sdhci_k1_pdata }, |
| 275 |
{ .compatible = "spacemit,k3-sdhci", .data = &spacemit_sdhci_k3_pdata }, |
| 276 |
{ /* sentinel */ } |
| 277 |
}; |
| 278 |
MODULE_DEVICE_TABLE(of, spacemit_sdhci_of_match); |
| 279 |
|
| 280 |
static int spacemit_sdhci_probe(struct platform_device *pdev) |
| 281 |
{ |
| 282 |
struct device *dev = &pdev->dev; |
| 283 |
struct spacemit_sdhci_host *sdhst; |
| 284 |
struct sdhci_pltfm_host *pltfm_host; |
| 285 |
struct sdhci_host *host; |
| 286 |
const struct sdhci_pltfm_data *data; |
| 287 |
struct mmc_host_ops *mops; |
| 288 |
int ret; |
| 289 |
|
| 290 |
data = of_device_get_match_data(&pdev->dev); |
| 291 |
|
| 292 |
host = sdhci_pltfm_init(pdev, data, sizeof(*sdhst)); |
| 293 |
if (IS_ERR(host)) |
| 294 |
return PTR_ERR(host); |
| 295 |
|
| 296 |
pltfm_host = sdhci_priv(host); |
| 297 |
|
| 298 |
ret = mmc_of_parse(host->mmc); |
| 299 |
if (ret) |
| 300 |
goto err_pltfm; |
| 301 |
|
| 302 |
sdhci_get_of_property(pdev); |
| 303 |
|
| 304 |
if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC)) { |
| 305 |
mops = &host->mmc_host_ops; |
| 306 |
mops->hs400_prepare_ddr = spacemit_sdhci_pre_select_hs400; |
| 307 |
mops->hs400_complete = spacemit_sdhci_post_select_hs400; |
| 308 |
mops->hs400_downgrade = spacemit_sdhci_pre_hs400_to_hs200; |
| 309 |
mops->hs400_enhanced_strobe = spacemit_sdhci_hs400_enhanced_strobe; |
| 310 |
} |
| 311 |
|
| 312 |
host->mmc->caps |= MMC_CAP_NEED_RSP_BUSY; |
| 313 |
|
| 314 |
ret = spacemit_sdhci_get_clocks(dev, pltfm_host); |
| 315 |
if (ret) |
| 316 |
goto err_pltfm; |
| 317 |
|
| 318 |
ret = spacemit_sdhci_get_resets(dev); |
| 319 |
if (ret) |
| 320 |
goto err_pltfm; |
| 321 |
|
| 322 |
ret = sdhci_add_host(host); |
| 323 |
if (ret) |
| 324 |
goto err_pltfm; |
| 325 |
|
| 326 |
return 0; |
| 327 |
|
| 328 |
err_pltfm: |
| 329 |
return ret; |
| 330 |
} |
| 331 |
|
| 332 |
static struct platform_driver spacemit_sdhci_driver = { |
| 333 |
.driver = { |
| 334 |
.name = "sdhci-spacemit", |
| 335 |
.of_match_table = spacemit_sdhci_of_match, |
| 336 |
}, |
| 337 |
.probe = spacemit_sdhci_probe, |
| 338 |
.remove = sdhci_pltfm_remove, |
| 339 |
}; |
| 340 |
module_platform_driver(spacemit_sdhci_driver); |
| 341 |
|
| 342 |
MODULE_DESCRIPTION("SpacemiT SDHCI platform driver"); |
| 343 |
MODULE_LICENSE("GPL"); |