| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 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/pinctrl/consumer.h> |
| 20 |
#include <linux/platform_device.h> |
| 21 |
|
| 22 |
#include "sdhci.h" |
| 23 |
#include "sdhci-pltfm.h" |
| 24 |
|
| •25 |
#define SPACEMIT_SDHC_OP_EXT_REG 0x108 |
| 26 |
#define SDHC_OVRRD_CLK_OEN BIT(11) |
| 27 |
#define SDHC_FORCE_CLK_ON BIT(12) |
| 28 |
|
| 29 |
#define SPACEMIT_SDHC_LEGACY_CTRL_REG 0x10C |
| 30 |
#define SDHC_GEN_PAD_CLK_ON BIT(6) |
| 31 |
|
| 32 |
#define SPACEMIT_SDHC_MMC_CTRL_REG 0x114 |
| 33 |
#define SDHC_MISC_INT_EN BIT(1) |
| 34 |
#define SDHC_MISC_INT BIT(2) |
| 35 |
#define SDHC_ENHANCE_STROBE_EN BIT(8) |
| 36 |
#define SDHC_MMC_HS400 BIT(9) |
| 37 |
#define SDHC_MMC_HS200 BIT(10) |
| 38 |
#define SDHC_MMC_CARD_MODE BIT(12) |
| 39 |
|
| 40 |
#define SPACEMIT_SDHC_TX_CFG_REG 0x11C |
| 41 |
#define SDHC_TX_INT_CLK_SEL BIT(30) |
| 42 |
#define SDHC_TX_MUX_SEL BIT(31) |
| 43 |
|
| 44 |
#define SPACEMIT_SDHC_PHY_CTRL_REG 0x160 |
| 45 |
#define SDHC_PHY_FUNC_EN BIT(0) |
| 46 |
#define SDHC_PHY_PLL_LOCK BIT(1) |
| 47 |
#define SDHC_HOST_LEGACY_MODE BIT(31) |
| 48 |
|
| 49 |
#define SPACEMIT_SDHC_PHY_FUNC_REG 0x164 |
| 50 |
#define SDHC_PHY_TEST_EN BIT(7) |
| 51 |
#define SDHC_HS200_USE_RFIFO BIT(15) |
| 52 |
|
| 53 |
#define SPACEMIT_SDHC_PHY_DLLCFG 0x168 |
| 54 |
#define SDHC_DLL_PREDLY_NUM GENMASK(3, 2) |
| 55 |
#define SDHC_DLL_FULLDLY_RANGE GENMASK(5, 4) |
| 56 |
#define SDHC_DLL_VREG_CTRL GENMASK(7, 6) |
| 57 |
#define SDHC_DLL_ENABLE BIT(31) |
| 58 |
|
| 59 |
#define SPACEMIT_SDHC_PHY_DLLCFG1 0x16C |
| 60 |
#define SDHC_DLL_REG1_CTRL GENMASK(7, 0) |
| 61 |
#define SDHC_DLL_REG2_CTRL GENMASK(15, 8) |
| 62 |
#define SDHC_DLL_REG3_CTRL GENMASK(23, 16) |
| 63 |
#define SDHC_DLL_REG4_CTRL GENMASK(31, 24) |
| 64 |
|
| 65 |
#define SPACEMIT_SDHC_PHY_DLLSTS 0x170 |
| 66 |
#define SDHC_DLL_LOCK_STATE BIT(0) |
| 67 |
|
| 68 |
#define SPACEMIT_SDHC_PHY_PADCFG_REG 0x178 |
| 69 |
#define SDHC_PHY_DRIVE_SEL GENMASK(2, 0) |
| 70 |
#define SDHC_RX_BIAS_CTRL BIT(5) |
| 71 |
|
| •72 |
#define SPACEMIT_SDHC_RX_CFG_REG 0x118 |
| 73 |
#define SDHC_RX_SDCLK_SEL0_MASK GENMASK(1, 0) |
| 74 |
#define SDHC_RX_SDCLK_SEL1_MASK GENMASK(3, 2) |
| 75 |
#define SDHC_RX_SDCLK_SEL1 FIELD_PREP(SDHC_RX_SDCLK_SEL1_MASK, 1) |
| 76 |
|
| 77 |
#define SPACEMIT_SDHC_DLINE_CTRL_REG 0x130 |
| 78 |
#define SDHC_DLINE_PU BIT(0) |
| 79 |
#define SDHC_RX_DLINE_CODE_MASK GENMASK(23, 16) |
| 80 |
#define SDHC_TX_DLINE_CODE_MASK GENMASK(31, 24) |
| 81 |
|
| 82 |
#define SPACEMIT_SDHC_DLINE_CFG_REG 0x134 |
| 83 |
#define SDHC_RX_DLINE_REG_MASK GENMASK(7, 0) |
| 84 |
#define SDHC_RX_DLINE_GAIN BIT(8) |
| 85 |
#define SDHC_TX_DLINE_REG_MASK GENMASK(23, 16) |
| 86 |
|
| 87 |
#define SPACEMIT_RX_DLINE_REG 9 |
| 88 |
#define SPACEMIT_RX_TUNE_DELAY_MIN 0x0 |
| 89 |
#define SPACEMIT_RX_TUNE_DELAY_MAX 0xFF |
| 90 |
|
| 91 |
#define SPACEMIT_TX_TUNING_DLINE_REG 0x00 |
| 92 |
#define SPACEMIT_TX_TUNING_DELAYCODE 127 |
| 93 |
|
| 94 |
struct spacemit_sdhci_host { |
| 95 |
struct clk *clk_core; |
| 96 |
struct clk *clk_io; |
| •97 |
struct pinctrl *pinctrl; |
| 98 |
struct pinctrl_state *pinctrl_default; |
| 99 |
struct pinctrl_state *pinctrl_uhs; |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
static inline void spacemit_sdhci_setbits(struct sdhci_host *host, u32 val, int reg) |
| 104 |
{ |
| 105 |
sdhci_writel(host, sdhci_readl(host, reg) | val, reg); |
| 106 |
} |
| 107 |
|
| 108 |
static inline void spacemit_sdhci_clrbits(struct sdhci_host *host, u32 val, int reg) |
| 109 |
{ |
| 110 |
sdhci_writel(host, sdhci_readl(host, reg) & ~val, reg); |
| 111 |
} |
| 112 |
|
| 113 |
static inline void spacemit_sdhci_clrsetbits(struct sdhci_host *host, u32 clr, u32 set, int reg) |
| 114 |
{ |
| 115 |
u32 val = sdhci_readl(host, reg); |
| 116 |
|
| 117 |
val = (val & ~clr) | set; |
| 118 |
sdhci_writel(host, val, reg); |
| 119 |
} |
| 120 |
|
| •121 |
static void spacemit_sdhci_set_rx_delay(struct sdhci_host *host, u8 delay) |
| 122 |
{ |
| 123 |
spacemit_sdhci_clrsetbits(host, SDHC_RX_DLINE_CODE_MASK, |
| 124 |
FIELD_PREP(SDHC_RX_DLINE_CODE_MASK, delay), |
| 125 |
SPACEMIT_SDHC_DLINE_CTRL_REG); |
| 126 |
} |
| 127 |
|
| 128 |
static void spacemit_sdhci_set_tx_delay(struct sdhci_host *host, u8 delay) |
| 129 |
{ |
| 130 |
spacemit_sdhci_clrsetbits(host, SDHC_TX_DLINE_CODE_MASK, |
| 131 |
FIELD_PREP(SDHC_TX_DLINE_CODE_MASK, delay), |
| 132 |
SPACEMIT_SDHC_DLINE_CTRL_REG); |
| 133 |
} |
| 134 |
|
| 135 |
static void spacemit_sdhci_set_tx_dline_reg(struct sdhci_host *host, u8 dline_reg) |
| 136 |
{ |
| 137 |
spacemit_sdhci_clrsetbits(host, SDHC_TX_DLINE_REG_MASK, |
| 138 |
FIELD_PREP(SDHC_TX_DLINE_REG_MASK, dline_reg), |
| 139 |
SPACEMIT_SDHC_DLINE_CFG_REG); |
| 140 |
} |
| 141 |
|
| 142 |
static void spacemit_sdhci_tx_tuning_prepare(struct sdhci_host *host) |
| 143 |
{ |
| 144 |
spacemit_sdhci_setbits(host, SDHC_TX_MUX_SEL, SPACEMIT_SDHC_TX_CFG_REG); |
| 145 |
spacemit_sdhci_setbits(host, SDHC_DLINE_PU, SPACEMIT_SDHC_DLINE_CTRL_REG); |
| 146 |
udelay(5); |
| 147 |
} |
| 148 |
|
| 149 |
static void spacemit_sdhci_prepare_tuning(struct sdhci_host *host) |
| 150 |
{ |
| 151 |
spacemit_sdhci_clrsetbits(host, SDHC_RX_DLINE_REG_MASK, |
| 152 |
FIELD_PREP(SDHC_RX_DLINE_REG_MASK, SPACEMIT_RX_DLINE_REG), |
| 153 |
SPACEMIT_SDHC_DLINE_CFG_REG); |
| 154 |
|
| 155 |
spacemit_sdhci_setbits(host, SDHC_DLINE_PU, SPACEMIT_SDHC_DLINE_CTRL_REG); |
| 156 |
udelay(5); |
| 157 |
|
| 158 |
spacemit_sdhci_clrsetbits(host, SDHC_RX_SDCLK_SEL1_MASK, SDHC_RX_SDCLK_SEL1, |
| 159 |
SPACEMIT_SDHC_RX_CFG_REG); |
| 160 |
|
| 161 |
if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200) |
| 162 |
spacemit_sdhci_setbits(host, SDHC_HS200_USE_RFIFO, SPACEMIT_SDHC_PHY_FUNC_REG); |
| 163 |
} |
| 164 |
|
| 165 |
static void spacemit_sdhci_reset(struct sdhci_host *host, u8 mask) |
| 166 |
{ |
| 167 |
sdhci_reset(host, mask); |
| 168 |
|
| 169 |
if (mask != SDHCI_RESET_ALL) |
| 170 |
return; |
| 171 |
|
| 172 |
spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK, |
| 173 |
SPACEMIT_SDHC_PHY_CTRL_REG); |
| 174 |
|
| 175 |
spacemit_sdhci_clrsetbits(host, SDHC_PHY_DRIVE_SEL, |
| 176 |
SDHC_RX_BIAS_CTRL | FIELD_PREP(SDHC_PHY_DRIVE_SEL, 4), |
| 177 |
SPACEMIT_SDHC_PHY_PADCFG_REG); |
| 178 |
|
| 179 |
if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC)) |
| 180 |
spacemit_sdhci_setbits(host, SDHC_MMC_CARD_MODE, SPACEMIT_SDHC_MMC_CTRL_REG); |
| •181 |
|
| 182 |
spacemit_sdhci_setbits(host, SDHC_GEN_PAD_CLK_ON, SPACEMIT_SDHC_LEGACY_CTRL_REG); |
| 183 |
|
| 184 |
if (host->mmc->caps2 & MMC_CAP2_NO_MMC) |
| 185 |
spacemit_sdhci_setbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON, |
| 186 |
SPACEMIT_SDHC_OP_EXT_REG); |
| 187 |
} |
| 188 |
|
| 189 |
static void spacemit_sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned int timing) |
| 190 |
{ |
| 191 |
if (timing == MMC_TIMING_MMC_HS200) |
| 192 |
spacemit_sdhci_setbits(host, SDHC_MMC_HS200, SPACEMIT_SDHC_MMC_CTRL_REG); |
| 193 |
|
| 194 |
if (timing == MMC_TIMING_MMC_HS400) |
| 195 |
spacemit_sdhci_setbits(host, SDHC_MMC_HS400, SPACEMIT_SDHC_MMC_CTRL_REG); |
| 196 |
|
| 197 |
sdhci_set_uhs_signaling(host, timing); |
| 198 |
|
| 199 |
if (!(host->mmc->caps2 & MMC_CAP2_NO_SDIO)) |
| 200 |
spacemit_sdhci_setbits(host, SDHCI_CTRL_VDD_180, SDHCI_HOST_CONTROL2); |
| 201 |
} |
| 202 |
|
| 203 |
static void spacemit_sdhci_set_clock(struct sdhci_host *host, unsigned int clock) |
| 204 |
{ |
| 205 |
struct mmc_host *mmc = host->mmc; |
| 206 |
|
| 207 |
if (mmc->ios.timing <= MMC_TIMING_UHS_SDR50) |
| 208 |
spacemit_sdhci_setbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG); |
| 209 |
else |
| 210 |
spacemit_sdhci_clrbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG); |
| 211 |
|
| 212 |
sdhci_set_clock(host, clock); |
| 213 |
}; |
| 214 |
|
| 215 |
static void spacemit_sdhci_phy_dll_init(struct sdhci_host *host) |
| 216 |
{ |
| 217 |
u32 state; |
| 218 |
int ret; |
| 219 |
|
| 220 |
spacemit_sdhci_clrsetbits(host, SDHC_DLL_PREDLY_NUM | |
| 221 |
SDHC_DLL_FULLDLY_RANGE | |
| 222 |
SDHC_DLL_VREG_CTRL, |
| 223 |
FIELD_PREP(SDHC_DLL_PREDLY_NUM, 1) | |
| 224 |
FIELD_PREP(SDHC_DLL_FULLDLY_RANGE, 1) | |
| 225 |
FIELD_PREP(SDHC_DLL_VREG_CTRL, 1), |
| 226 |
SPACEMIT_SDHC_PHY_DLLCFG); |
| 227 |
|
| 228 |
spacemit_sdhci_clrsetbits(host, SDHC_DLL_REG1_CTRL, |
| 229 |
FIELD_PREP(SDHC_DLL_REG1_CTRL, 0x92), |
| 230 |
SPACEMIT_SDHC_PHY_DLLCFG1); |
| 231 |
|
| 232 |
spacemit_sdhci_setbits(host, SDHC_DLL_ENABLE, SPACEMIT_SDHC_PHY_DLLCFG); |
| 233 |
|
| 234 |
ret = readl_poll_timeout(host->ioaddr + SPACEMIT_SDHC_PHY_DLLSTS, state, |
| 235 |
state & SDHC_DLL_LOCK_STATE, 2, 100); |
| 236 |
if (ret == -ETIMEDOUT) |
| 237 |
dev_warn(mmc_dev(host->mmc), "fail to lock phy dll in 100us!\n"); |
| 238 |
} |
| 239 |
|
| 240 |
static void spacemit_sdhci_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios) |
| 241 |
{ |
| 242 |
struct sdhci_host *host = mmc_priv(mmc); |
| 243 |
|
| 244 |
if (!ios->enhanced_strobe) { |
| 245 |
spacemit_sdhci_clrbits(host, SDHC_ENHANCE_STROBE_EN, SPACEMIT_SDHC_MMC_CTRL_REG); |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
spacemit_sdhci_setbits(host, SDHC_ENHANCE_STROBE_EN, SPACEMIT_SDHC_MMC_CTRL_REG); |
| 250 |
spacemit_sdhci_phy_dll_init(host); |
| 251 |
} |
| 252 |
|
| 253 |
static unsigned int spacemit_sdhci_clk_get_max_clock(struct sdhci_host *host) |
| 254 |
{ |
| 255 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 256 |
|
| 257 |
return clk_get_rate(pltfm_host->clk); |
| 258 |
} |
| 259 |
|
| •260 |
static int spacemit_sdhci_execute_tuning(struct sdhci_host *host, u32 opcode) |
| 261 |
{ |
| 262 |
int current_len = 0, current_start = 0; |
| 263 |
int max_pass_len = 0, max_pass_start = 0; |
| 264 |
struct mmc_host *mmc = host->mmc; |
| 265 |
struct mmc_ios ios = mmc->ios; |
| 266 |
u8 final_delay; |
| 267 |
int ret = 0; |
| 268 |
int i; |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
if (host->clock < 100 * 1000 * 1000 || |
| 275 |
!(ios.timing == MMC_TIMING_MMC_HS200 || |
| 276 |
ios.timing == MMC_TIMING_UHS_SDR50 || |
| 277 |
ios.timing == MMC_TIMING_UHS_SDR104)) |
| 278 |
return 0; |
| 279 |
|
| 280 |
if (mmc->caps2 & MMC_CAP2_NO_MMC) { |
| 281 |
spacemit_sdhci_set_tx_dline_reg(host, SPACEMIT_TX_TUNING_DLINE_REG); |
| 282 |
spacemit_sdhci_set_tx_delay(host, SPACEMIT_TX_TUNING_DELAYCODE); |
| 283 |
spacemit_sdhci_tx_tuning_prepare(host); |
| 284 |
|
| 285 |
dev_dbg(mmc_dev(host->mmc), "TX tuning: dline_reg=%d, delaycode=%d\n", |
| 286 |
SPACEMIT_TX_TUNING_DLINE_REG, SPACEMIT_TX_TUNING_DELAYCODE); |
| 287 |
} |
| 288 |
|
| 289 |
spacemit_sdhci_prepare_tuning(host); |
| 290 |
|
| 291 |
for (i = SPACEMIT_RX_TUNE_DELAY_MIN; i <= SPACEMIT_RX_TUNE_DELAY_MAX; i++) { |
| 292 |
spacemit_sdhci_set_rx_delay(host, i); |
| 293 |
ret = mmc_send_tuning(host->mmc, opcode, NULL); |
| 294 |
|
| 295 |
dev_dbg(mmc_dev(host->mmc), "RX delay %d: %s\n", |
| 296 |
i, ret == 0 ? "pass" : "fail"); |
| 297 |
|
| 298 |
if (ret == 0) { |
| 299 |
|
| 300 |
if (current_len == 0) |
| 301 |
current_start = i; |
| 302 |
current_len++; |
| 303 |
} else { |
| 304 |
|
| 305 |
if (current_len > max_pass_len) { |
| 306 |
max_pass_len = current_len; |
| 307 |
max_pass_start = current_start; |
| 308 |
} |
| 309 |
current_len = 0; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if (current_len > max_pass_len) { |
| 314 |
max_pass_len = current_len; |
| 315 |
max_pass_start = current_start; |
| 316 |
} |
| 317 |
|
| 318 |
if (max_pass_len < 3) { |
| 319 |
dev_err(mmc_dev(host->mmc), "Tuning failed: no stable window found\n"); |
| 320 |
return -EIO; |
| 321 |
} |
| 322 |
|
| 323 |
final_delay = max_pass_start + max_pass_len / 2; |
| 324 |
spacemit_sdhci_set_rx_delay(host, final_delay); |
| 325 |
ret = mmc_send_tuning(host->mmc, opcode, NULL); |
| 326 |
if (ret) { |
| 327 |
u8 retry_delays[] = { |
| 328 |
max_pass_start + max_pass_len / 4, |
| 329 |
max_pass_start + (3 * max_pass_len) / 4, |
| 330 |
max_pass_start, |
| 331 |
max_pass_start + max_pass_len - 1 |
| 332 |
}; |
| 333 |
int retry_count = ARRAY_SIZE(retry_delays); |
| 334 |
|
| 335 |
dev_warn(mmc_dev(mmc), "Primary delay %d failed, trying alternatives\n", |
| 336 |
final_delay); |
| 337 |
|
| 338 |
for (i = 0; i < retry_count; i++) { |
| 339 |
if (retry_delays[i] >= SPACEMIT_RX_TUNE_DELAY_MIN && |
| 340 |
retry_delays[i] <= SPACEMIT_RX_TUNE_DELAY_MAX) { |
| 341 |
spacemit_sdhci_set_rx_delay(host, retry_delays[i]); |
| 342 |
ret = mmc_send_tuning(host->mmc, opcode, NULL); |
| 343 |
if (!ret) { |
| 344 |
final_delay = retry_delays[i]; |
| 345 |
dev_info(mmc_dev(mmc), "Retry successful with delay %d\n", |
| 346 |
final_delay); |
| 347 |
break; |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
if (ret) { |
| 353 |
dev_err(mmc_dev(mmc), "All retry attempts failed\n"); |
| 354 |
return -EIO; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
dev_dbg(mmc_dev(host->mmc), |
| 359 |
"Tuning successful: window %d-%d, using delay %d\n", |
| 360 |
max_pass_start, max_pass_start + max_pass_len - 1, final_delay); |
| 361 |
|
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
static int spacemit_sdhci_pre_select_hs400(struct mmc_host *mmc) |
| 366 |
{ |
| 367 |
struct sdhci_host *host = mmc_priv(mmc); |
| 368 |
|
| 369 |
spacemit_sdhci_setbits(host, SDHC_MMC_HS400, SPACEMIT_SDHC_MMC_CTRL_REG); |
| 370 |
|
| 371 |
return 0; |
| 372 |
} |
| 373 |
|
| 374 |
static void spacemit_sdhci_post_select_hs400(struct mmc_host *mmc) |
| 375 |
{ |
| 376 |
struct sdhci_host *host = mmc_priv(mmc); |
| 377 |
|
| 378 |
spacemit_sdhci_phy_dll_init(host); |
| 379 |
} |
| 380 |
|
| 381 |
static void spacemit_sdhci_pre_hs400_to_hs200(struct mmc_host *mmc) |
| 382 |
{ |
| 383 |
struct sdhci_host *host = mmc_priv(mmc); |
| 384 |
|
| 385 |
spacemit_sdhci_clrbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK, |
| 386 |
SPACEMIT_SDHC_PHY_CTRL_REG); |
| 387 |
spacemit_sdhci_clrbits(host, SDHC_MMC_HS400 | SDHC_MMC_HS200 | SDHC_ENHANCE_STROBE_EN, |
| 388 |
SPACEMIT_SDHC_MMC_CTRL_REG); |
| 389 |
spacemit_sdhci_clrbits(host, SDHC_HS200_USE_RFIFO, SPACEMIT_SDHC_PHY_FUNC_REG); |
| 390 |
|
| 391 |
udelay(5); |
| 392 |
|
| 393 |
spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK, |
| 394 |
SPACEMIT_SDHC_PHY_CTRL_REG); |
| 395 |
} |
| 396 |
|
| •397 |
static int spacemit_sdhci_start_signal_voltage_switch(struct mmc_host *mmc, |
| 398 |
struct mmc_ios *ios) |
| 399 |
{ |
| 400 |
struct sdhci_host *host = mmc_priv(mmc); |
| 401 |
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 402 |
struct spacemit_sdhci_host *sdhst = sdhci_pltfm_priv(pltfm_host); |
| 403 |
struct pinctrl_state *state; |
| 404 |
int ret; |
| 405 |
|
| 406 |
ret = sdhci_start_signal_voltage_switch(mmc, ios); |
| 407 |
if (ret) |
| 408 |
return ret; |
| 409 |
|
| 410 |
if (!sdhst->pinctrl) |
| 411 |
return 0; |
| 412 |
|
| 413 |
|
| 414 |
switch (ios->signal_voltage) { |
| 415 |
case MMC_SIGNAL_VOLTAGE_330: |
| 416 |
state = sdhst->pinctrl_default; |
| 417 |
break; |
| 418 |
case MMC_SIGNAL_VOLTAGE_180: |
| 419 |
state = sdhst->pinctrl_uhs; |
| 420 |
break; |
| 421 |
default: |
| 422 |
dev_warn(mmc_dev(mmc), "unsupported voltage %d\n", ios->signal_voltage); |
| 423 |
return 0; |
| 424 |
} |
| 425 |
|
| 426 |
ret = pinctrl_select_state(sdhst->pinctrl, state); |
| 427 |
if (ret) { |
| 428 |
dev_warn(mmc_dev(mmc), "failed to select pinctrl state: %d\n", ret); |
| 429 |
return 0; |
| 430 |
} |
| 431 |
dev_dbg(mmc_dev(mmc), "switched to %s pinctrl state\n", |
| 432 |
ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180 ? "UHS" : "default"); |
| 433 |
|
| 434 |
return 0; |
| 435 |
} |
| 436 |
|
| 437 |
static inline int spacemit_sdhci_get_clocks(struct device *dev, |
| 438 |
struct sdhci_pltfm_host *pltfm_host) |
| 439 |
{ |
| 440 |
struct spacemit_sdhci_host *sdhst = sdhci_pltfm_priv(pltfm_host); |
| 441 |
|
| 442 |
sdhst->clk_core = devm_clk_get_enabled(dev, "core"); |
| 443 |
if (IS_ERR(sdhst->clk_core)) |
| 444 |
return -EINVAL; |
| 445 |
|
| 446 |
sdhst->clk_io = devm_clk_get_enabled(dev, "io"); |
| 447 |
if (IS_ERR(sdhst->clk_io)) |
| 448 |
return -EINVAL; |
| 449 |
|
| 450 |
pltfm_host->clk = sdhst->clk_io; |
| 451 |
|
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
static inline int spacemit_sdhci_get_resets(struct device *dev) |
| 456 |
{ |
| 457 |
struct reset_control *rst; |
| 458 |
|
| 459 |
rst = devm_reset_control_get_optional_shared_deasserted(dev, "axi"); |
| 460 |
if (IS_ERR(rst)) |
| 461 |
return PTR_ERR(rst); |
| 462 |
|
| 463 |
rst = devm_reset_control_get_optional_exclusive_deasserted(dev, "sdh"); |
| 464 |
if (IS_ERR(rst)) |
| 465 |
return PTR_ERR(rst); |
| 466 |
|
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| •470 |
static inline void spacemit_sdhci_get_pins(struct device *dev, |
| 471 |
struct sdhci_pltfm_host *pltfm_host) |
| 472 |
{ |
| 473 |
struct spacemit_sdhci_host *sdhst = sdhci_pltfm_priv(pltfm_host); |
| 474 |
|
| 475 |
sdhst->pinctrl = devm_pinctrl_get(dev); |
| 476 |
if (IS_ERR(sdhst->pinctrl)) { |
| 477 |
sdhst->pinctrl = NULL; |
| 478 |
dev_dbg(dev, "pinctrl not available, voltage switching will work without it\n"); |
| 479 |
return; |
| 480 |
} |
| 481 |
|
| 482 |
sdhst->pinctrl_default = pinctrl_lookup_state(sdhst->pinctrl, "default"); |
| 483 |
if (IS_ERR(sdhst->pinctrl_default)) |
| 484 |
sdhst->pinctrl_default = NULL; |
| 485 |
|
| 486 |
sdhst->pinctrl_uhs = pinctrl_lookup_state(sdhst->pinctrl, "uhs"); |
| 487 |
if (IS_ERR(sdhst->pinctrl_uhs)) |
| 488 |
sdhst->pinctrl_uhs = NULL; |
| 489 |
|
| 490 |
dev_dbg(dev, "pinctrl setup: default=%p, uhs=%p\n", |
| 491 |
sdhst->pinctrl_default, sdhst->pinctrl_uhs); |
| 492 |
} |
| 493 |
|
| 494 |
static const struct sdhci_ops spacemit_sdhci_ops = { |
| 495 |
.get_max_clock = spacemit_sdhci_clk_get_max_clock, |
| 496 |
.reset = spacemit_sdhci_reset, |
| 497 |
.set_bus_width = sdhci_set_bus_width, |
| 498 |
.set_clock = spacemit_sdhci_set_clock, |
| 499 |
.set_uhs_signaling = spacemit_sdhci_set_uhs_signaling, |
| •500 |
.platform_execute_tuning = spacemit_sdhci_execute_tuning, |
| 501 |
}; |
| 502 |
|
| 503 |
static const struct sdhci_pltfm_data spacemit_sdhci_k1_pdata = { |
| 504 |
.ops = &spacemit_sdhci_ops, |
| 505 |
.quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK | |
| 506 |
SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | |
| 507 |
SDHCI_QUIRK_32BIT_ADMA_SIZE | |
| 508 |
SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
| 509 |
SDHCI_QUIRK_BROKEN_CARD_DETECTION | |
| 510 |
SDHCI_QUIRK_BROKEN_TIMEOUT_VAL, |
| 511 |
.quirks2 = SDHCI_QUIRK2_BROKEN_64_BIT_DMA | |
| 512 |
SDHCI_QUIRK2_PRESET_VALUE_BROKEN, |
| 513 |
}; |
| 514 |
|
| 515 |
static const struct sdhci_pltfm_data spacemit_sdhci_k3_pdata = { |
| 516 |
.ops = &spacemit_sdhci_ops, |
| 517 |
.quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK | |
| 518 |
SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | |
| 519 |
SDHCI_QUIRK_32BIT_ADMA_SIZE | |
| 520 |
SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
| 521 |
SDHCI_QUIRK_BROKEN_CARD_DETECTION | |
| 522 |
SDHCI_QUIRK_BROKEN_TIMEOUT_VAL, |
| 523 |
.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN, |
| 524 |
}; |
| 525 |
|
| 526 |
static const struct of_device_id spacemit_sdhci_of_match[] = { |
| 527 |
{ .compatible = "spacemit,k1-sdhci", .data = &spacemit_sdhci_k1_pdata }, |
| 528 |
{ .compatible = "spacemit,k3-sdhci", .data = &spacemit_sdhci_k3_pdata }, |
| 529 |
{ } |
| 530 |
}; |
| 531 |
MODULE_DEVICE_TABLE(of, spacemit_sdhci_of_match); |
| 532 |
|
| 533 |
static int spacemit_sdhci_probe(struct platform_device *pdev) |
| 534 |
{ |
| 535 |
struct device *dev = &pdev->dev; |
| 536 |
struct spacemit_sdhci_host *sdhst; |
| 537 |
struct sdhci_pltfm_host *pltfm_host; |
| 538 |
struct sdhci_host *host; |
| 539 |
const struct sdhci_pltfm_data *data; |
| 540 |
struct mmc_host_ops *mops; |
| 541 |
int ret; |
| 542 |
|
| 543 |
data = of_device_get_match_data(&pdev->dev); |
| 544 |
|
| 545 |
host = sdhci_pltfm_init(pdev, data, sizeof(*sdhst)); |
| 546 |
if (IS_ERR(host)) |
| 547 |
return PTR_ERR(host); |
| 548 |
|
| 549 |
pltfm_host = sdhci_priv(host); |
| 550 |
|
| 551 |
ret = mmc_of_parse(host->mmc); |
| 552 |
if (ret) |
| 553 |
goto err_pltfm; |
| 554 |
|
| 555 |
sdhci_get_of_property(pdev); |
| 556 |
|
| 557 |
if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC)) { |
| 558 |
mops = &host->mmc_host_ops; |
| 559 |
mops->hs400_prepare_ddr = spacemit_sdhci_pre_select_hs400; |
| 560 |
mops->hs400_complete = spacemit_sdhci_post_select_hs400; |
| 561 |
mops->hs400_downgrade = spacemit_sdhci_pre_hs400_to_hs200; |
| 562 |
mops->hs400_enhanced_strobe = spacemit_sdhci_hs400_enhanced_strobe; |
| 563 |
} |
| 564 |
|
| 565 |
host->mmc->caps |= MMC_CAP_NEED_RSP_BUSY; |
| 566 |
|
| •567 |
spacemit_sdhci_get_pins(dev, pltfm_host); |
| 568 |
|
| 569 |
host->mmc_host_ops.start_signal_voltage_switch = spacemit_sdhci_start_signal_voltage_switch; |
| 570 |
|
| 571 |
ret = spacemit_sdhci_get_clocks(dev, pltfm_host); |
| 572 |
if (ret) |
| 573 |
goto err_pltfm; |
| 574 |
|
| 575 |
ret = spacemit_sdhci_get_resets(dev); |
| 576 |
if (ret) |
| 577 |
goto err_pltfm; |
| 578 |
|
| 579 |
ret = sdhci_add_host(host); |
| 580 |
if (ret) |
| 581 |
goto err_pltfm; |
| 582 |
|
| 583 |
return 0; |
| 584 |
|
| 585 |
err_pltfm: |
| 586 |
return ret; |
| 587 |
} |
| 588 |
|
| 589 |
static struct platform_driver spacemit_sdhci_driver = { |
| 590 |
.driver = { |
| 591 |
.name = "sdhci-spacemit", |
| 592 |
.of_match_table = spacemit_sdhci_of_match, |
| 593 |
}, |
| 594 |
.probe = spacemit_sdhci_probe, |
| 595 |
.remove = sdhci_pltfm_remove, |
| 596 |
}; |
| 597 |
module_platform_driver(spacemit_sdhci_driver); |
| 598 |
|
| 599 |
MODULE_DESCRIPTION("SpacemiT SDHCI platform driver"); |
| 600 |
MODULE_LICENSE("GPL"); |