1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Arasan Secure Digital Host Controller Interface. 4 * Copyright (C) 2011 - 2012 Michal Simek <monstr@monstr.eu> 5 * Copyright (c) 2012 Wind River Systems, Inc. 6 * Copyright (C) 2013 Pengutronix e.K. 7 * Copyright (C) 2013 Xilinx Inc. 8 * 9 * Based on sdhci-of-esdhc.c 10 * 11 * Copyright (c) 2007 Freescale Semiconductor, Inc. 12 * Copyright (c) 2009 MontaVista Software, Inc. 13 * 14 * Authors: Xiaobo Xie <X.Xie@freescale.com> 15 * Anton Vorontsov <avorontsov@ru.mvista.com> 16 */ 17 18 #include <linux/clk-provider.h> 19 #include <linux/mfd/syscon.h> 20 #include <linux/module.h> 21 #include <linux/of_device.h> 22 #include <linux/phy/phy.h> 23 #include <linux/regmap.h> 24 #include <linux/of.h> 25 #include <linux/firmware/xlnx-zynqmp.h> 26 27 #include "cqhci.h" 28 #include "sdhci-pltfm.h" 29 30 #define SDHCI_ARASAN_VENDOR_REGISTER 0x78 31 32 #define SDHCI_ARASAN_ITAPDLY_REGISTER 0xF0F8 33 #define SDHCI_ARASAN_OTAPDLY_REGISTER 0xF0FC 34 35 #define SDHCI_ARASAN_CQE_BASE_ADDR 0x200 36 #define VENDOR_ENHANCED_STROBE BIT(0) 37 38 #define PHY_CLK_TOO_SLOW_HZ 400000 39 40 #define SDHCI_ITAPDLY_CHGWIN 0x200 41 #define SDHCI_ITAPDLY_ENABLE 0x100 42 #define SDHCI_OTAPDLY_ENABLE 0x40 43 44 /* Default settings for ZynqMP Clock Phases */ 45 #define ZYNQMP_ICLK_PHASE {0, 63, 63, 0, 63, 0, 0, 183, 54, 0, 0} 46 #define ZYNQMP_OCLK_PHASE {0, 72, 60, 0, 60, 72, 135, 48, 72, 135, 0} 47 48 #define VERSAL_ICLK_PHASE {0, 132, 132, 0, 132, 0, 0, 162, 90, 0, 0} 49 #define VERSAL_OCLK_PHASE {0, 60, 48, 0, 48, 72, 90, 36, 60, 90, 0} 50 51 /* 52 * On some SoCs the syscon area has a feature where the upper 16-bits of 53 * each 32-bit register act as a write mask for the lower 16-bits. This allows 54 * atomic updates of the register without locking. This macro is used on SoCs 55 * that have that feature. 56 */ 57 #define HIWORD_UPDATE(val, mask, shift) \ 58 ((val) << (shift) | (mask) << ((shift) + 16)) 59 60 /** 61 * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map 62 * 63 * @reg: Offset within the syscon of the register containing this field 64 * @width: Number of bits for this field 65 * @shift: Bit offset within @reg of this field (or -1 if not avail) 66 */ 67 struct sdhci_arasan_soc_ctl_field { 68 u32 reg; 69 u16 width; 70 s16 shift; 71 }; 72 73 /** 74 * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers 75 * 76 * @baseclkfreq: Where to find corecfg_baseclkfreq 77 * @clockmultiplier: Where to find corecfg_clockmultiplier 78 * @support64b: Where to find SUPPORT64B bit 79 * @hiword_update: If true, use HIWORD_UPDATE to access the syscon 80 * 81 * It's up to the licensee of the Arsan IP block to make these available 82 * somewhere if needed. Presumably these will be scattered somewhere that's 83 * accessible via the syscon API. 84 */ 85 struct sdhci_arasan_soc_ctl_map { 86 struct sdhci_arasan_soc_ctl_field baseclkfreq; 87 struct sdhci_arasan_soc_ctl_field clockmultiplier; 88 struct sdhci_arasan_soc_ctl_field support64b; 89 bool hiword_update; 90 }; 91 92 /** 93 * struct sdhci_arasan_clk_ops - Clock Operations for Arasan SD controller 94 * 95 * @sdcardclk_ops: The output clock related operations 96 * @sampleclk_ops: The sample clock related operations 97 */ 98 struct sdhci_arasan_clk_ops { 99 const struct clk_ops *sdcardclk_ops; 100 const struct clk_ops *sampleclk_ops; 101 }; 102 103 /** 104 * struct sdhci_arasan_clk_data - Arasan Controller Clock Data. 105 * 106 * @sdcardclk_hw: Struct for the clock we might provide to a PHY. 107 * @sdcardclk: Pointer to normal 'struct clock' for sdcardclk_hw. 108 * @sampleclk_hw: Struct for the clock we might provide to a PHY. 109 * @sampleclk: Pointer to normal 'struct clock' for sampleclk_hw. 110 * @clk_phase_in: Array of Input Clock Phase Delays for all speed modes 111 * @clk_phase_out: Array of Output Clock Phase Delays for all speed modes 112 * @set_clk_delays: Function pointer for setting Clock Delays 113 * @clk_of_data: Platform specific runtime clock data storage pointer 114 */ 115 struct sdhci_arasan_clk_data { 116 struct clk_hw sdcardclk_hw; 117 struct clk *sdcardclk; 118 struct clk_hw sampleclk_hw; 119 struct clk *sampleclk; 120 int clk_phase_in[MMC_TIMING_MMC_HS400 + 1]; 121 int clk_phase_out[MMC_TIMING_MMC_HS400 + 1]; 122 void (*set_clk_delays)(struct sdhci_host *host); 123 void *clk_of_data; 124 }; 125 126 /** 127 * struct sdhci_arasan_data - Arasan Controller Data 128 * 129 * @host: Pointer to the main SDHCI host structure. 130 * @clk_ahb: Pointer to the AHB clock 131 * @phy: Pointer to the generic phy 132 * @is_phy_on: True if the PHY is on; false if not. 133 * @has_cqe: True if controller has command queuing engine. 134 * @clk_data: Struct for the Arasan Controller Clock Data. 135 * @clk_ops: Struct for the Arasan Controller Clock Operations. 136 * @soc_ctl_base: Pointer to regmap for syscon for soc_ctl registers. 137 * @soc_ctl_map: Map to get offsets into soc_ctl registers. 138 * @quirks: Arasan deviations from spec. 139 */ 140 struct sdhci_arasan_data { 141 struct sdhci_host *host; 142 struct clk *clk_ahb; 143 struct phy *phy; 144 bool is_phy_on; 145 146 bool has_cqe; 147 struct sdhci_arasan_clk_data clk_data; 148 const struct sdhci_arasan_clk_ops *clk_ops; 149 150 struct regmap *soc_ctl_base; 151 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map; 152 unsigned int quirks; 153 154 /* Controller does not have CD wired and will not function normally without */ 155 #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0) 156 /* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the 157 * internal clock even when the clock isn't stable */ 158 #define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1) 159 }; 160 161 struct sdhci_arasan_of_data { 162 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map; 163 const struct sdhci_pltfm_data *pdata; 164 const struct sdhci_arasan_clk_ops *clk_ops; 165 }; 166 167 static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = { 168 .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 }, 169 .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0}, 170 .hiword_update = true, 171 }; 172 173 static const struct sdhci_arasan_soc_ctl_map intel_lgm_emmc_soc_ctl_map = { 174 .baseclkfreq = { .reg = 0xa0, .width = 8, .shift = 2 }, 175 .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 }, 176 .hiword_update = false, 177 }; 178 179 static const struct sdhci_arasan_soc_ctl_map intel_lgm_sdxc_soc_ctl_map = { 180 .baseclkfreq = { .reg = 0x80, .width = 8, .shift = 2 }, 181 .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 }, 182 .hiword_update = false, 183 }; 184 185 static const struct sdhci_arasan_soc_ctl_map intel_keembay_soc_ctl_map = { 186 .baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 }, 187 .clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 }, 188 .support64b = { .reg = 0x4, .width = 1, .shift = 24 }, 189 .hiword_update = false, 190 }; 191 192 /** 193 * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers 194 * 195 * @host: The sdhci_host 196 * @fld: The field to write to 197 * @val: The value to write 198 * 199 * This function allows writing to fields in sdhci_arasan_soc_ctl_map. 200 * Note that if a field is specified as not available (shift < 0) then 201 * this function will silently return an error code. It will be noisy 202 * and print errors for any other (unexpected) errors. 203 * 204 * Return: 0 on success and error value on error 205 */ 206 static int sdhci_arasan_syscon_write(struct sdhci_host *host, 207 const struct sdhci_arasan_soc_ctl_field *fld, 208 u32 val) 209 { 210 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 211 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 212 struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base; 213 u32 reg = fld->reg; 214 u16 width = fld->width; 215 s16 shift = fld->shift; 216 int ret; 217 218 /* 219 * Silently return errors for shift < 0 so caller doesn't have 220 * to check for fields which are optional. For fields that 221 * are required then caller needs to do something special 222 * anyway. 223 */ 224 if (shift < 0) 225 return -EINVAL; 226 227 if (sdhci_arasan->soc_ctl_map->hiword_update) 228 ret = regmap_write(soc_ctl_base, reg, 229 HIWORD_UPDATE(val, GENMASK(width, 0), 230 shift)); 231 else 232 ret = regmap_update_bits(soc_ctl_base, reg, 233 GENMASK(shift + width, shift), 234 val << shift); 235 236 /* Yell about (unexpected) regmap errors */ 237 if (ret) 238 pr_warn("%s: Regmap write fail: %d\n", 239 mmc_hostname(host->mmc), ret); 240 241 return ret; 242 } 243 244 static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock) 245 { 246 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 247 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 248 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data; 249 bool ctrl_phy = false; 250 251 if (!IS_ERR(sdhci_arasan->phy)) { 252 if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) { 253 /* 254 * If PHY off, set clock to max speed and power PHY on. 255 * 256 * Although PHY docs apparently suggest power cycling 257 * when changing the clock the PHY doesn't like to be 258 * powered on while at low speeds like those used in ID 259 * mode. Even worse is powering the PHY on while the 260 * clock is off. 261 * 262 * To workaround the PHY limitations, the best we can 263 * do is to power it on at a faster speed and then slam 264 * through low speeds without power cycling. 265 */ 266 sdhci_set_clock(host, host->max_clk); 267 phy_power_on(sdhci_arasan->phy); 268 sdhci_arasan->is_phy_on = true; 269 270 /* 271 * We'll now fall through to the below case with 272 * ctrl_phy = false (so we won't turn off/on). The 273 * sdhci_set_clock() will set the real clock. 274 */ 275 } else if (clock > PHY_CLK_TOO_SLOW_HZ) { 276 /* 277 * At higher clock speeds the PHY is fine being power 278 * cycled and docs say you _should_ power cycle when 279 * changing clock speeds. 280 */ 281 ctrl_phy = true; 282 } 283 } 284 285 if (ctrl_phy && sdhci_arasan->is_phy_on) { 286 phy_power_off(sdhci_arasan->phy); 287 sdhci_arasan->is_phy_on = false; 288 } 289 290 /* Set the Input and Output Clock Phase Delays */ 291 if (clk_data->set_clk_delays) 292 clk_data->set_clk_delays(host); 293 294 sdhci_set_clock(host, clock); 295 296 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE) 297 /* 298 * Some controllers immediately report SDHCI_CLOCK_INT_STABLE 299 * after enabling the clock even though the clock is not 300 * stable. Trying to use a clock without waiting here results 301 * in EILSEQ while detecting some older/slower cards. The 302 * chosen delay is the maximum delay from sdhci_set_clock. 303 */ 304 msleep(20); 305 306 if (ctrl_phy) { 307 phy_power_on(sdhci_arasan->phy); 308 sdhci_arasan->is_phy_on = true; 309 } 310 } 311 312 static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc, 313 struct mmc_ios *ios) 314 { 315 u32 vendor; 316 struct sdhci_host *host = mmc_priv(mmc); 317 318 vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER); 319 if (ios->enhanced_strobe) 320 vendor |= VENDOR_ENHANCED_STROBE; 321 else 322 vendor &= ~VENDOR_ENHANCED_STROBE; 323 324 sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER); 325 } 326 327 static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask) 328 { 329 u8 ctrl; 330 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 331 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 332 333 sdhci_reset(host, mask); 334 335 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) { 336 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); 337 ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN; 338 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); 339 } 340 } 341 342 static int sdhci_arasan_voltage_switch(struct mmc_host *mmc, 343 struct mmc_ios *ios) 344 { 345 switch (ios->signal_voltage) { 346 case MMC_SIGNAL_VOLTAGE_180: 347 /* 348 * Plese don't switch to 1V8 as arasan,5.1 doesn't 349 * actually refer to this setting to indicate the 350 * signal voltage and the state machine will be broken 351 * actually if we force to enable 1V8. That's something 352 * like broken quirk but we could work around here. 353 */ 354 return 0; 355 case MMC_SIGNAL_VOLTAGE_330: 356 case MMC_SIGNAL_VOLTAGE_120: 357 /* We don't support 3V3 and 1V2 */ 358 break; 359 } 360 361 return -EINVAL; 362 } 363 364 static const struct sdhci_ops sdhci_arasan_ops = { 365 .set_clock = sdhci_arasan_set_clock, 366 .get_max_clock = sdhci_pltfm_clk_get_max_clock, 367 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock, 368 .set_bus_width = sdhci_set_bus_width, 369 .reset = sdhci_arasan_reset, 370 .set_uhs_signaling = sdhci_set_uhs_signaling, 371 .set_power = sdhci_set_power_and_bus_voltage, 372 }; 373 374 static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask) 375 { 376 int cmd_error = 0; 377 int data_error = 0; 378 379 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error)) 380 return intmask; 381 382 cqhci_irq(host->mmc, intmask, cmd_error, data_error); 383 384 return 0; 385 } 386 387 static void sdhci_arasan_dumpregs(struct mmc_host *mmc) 388 { 389 sdhci_dumpregs(mmc_priv(mmc)); 390 } 391 392 static void sdhci_arasan_cqe_enable(struct mmc_host *mmc) 393 { 394 struct sdhci_host *host = mmc_priv(mmc); 395 u32 reg; 396 397 reg = sdhci_readl(host, SDHCI_PRESENT_STATE); 398 while (reg & SDHCI_DATA_AVAILABLE) { 399 sdhci_readl(host, SDHCI_BUFFER); 400 reg = sdhci_readl(host, SDHCI_PRESENT_STATE); 401 } 402 403 sdhci_cqe_enable(mmc); 404 } 405 406 static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = { 407 .enable = sdhci_arasan_cqe_enable, 408 .disable = sdhci_cqe_disable, 409 .dumpregs = sdhci_arasan_dumpregs, 410 }; 411 412 static const struct sdhci_ops sdhci_arasan_cqe_ops = { 413 .set_clock = sdhci_arasan_set_clock, 414 .get_max_clock = sdhci_pltfm_clk_get_max_clock, 415 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock, 416 .set_bus_width = sdhci_set_bus_width, 417 .reset = sdhci_arasan_reset, 418 .set_uhs_signaling = sdhci_set_uhs_signaling, 419 .set_power = sdhci_set_power_and_bus_voltage, 420 .irq = sdhci_arasan_cqhci_irq, 421 }; 422 423 static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = { 424 .ops = &sdhci_arasan_cqe_ops, 425 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, 426 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | 427 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN, 428 }; 429 430 #ifdef CONFIG_PM_SLEEP 431 /** 432 * sdhci_arasan_suspend - Suspend method for the driver 433 * @dev: Address of the device structure 434 * 435 * Put the device in a low power state. 436 * 437 * Return: 0 on success and error value on error 438 */ 439 static int sdhci_arasan_suspend(struct device *dev) 440 { 441 struct sdhci_host *host = dev_get_drvdata(dev); 442 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 443 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 444 int ret; 445 446 if (host->tuning_mode != SDHCI_TUNING_MODE_3) 447 mmc_retune_needed(host->mmc); 448 449 if (sdhci_arasan->has_cqe) { 450 ret = cqhci_suspend(host->mmc); 451 if (ret) 452 return ret; 453 } 454 455 ret = sdhci_suspend_host(host); 456 if (ret) 457 return ret; 458 459 if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) { 460 ret = phy_power_off(sdhci_arasan->phy); 461 if (ret) { 462 dev_err(dev, "Cannot power off phy.\n"); 463 sdhci_resume_host(host); 464 return ret; 465 } 466 sdhci_arasan->is_phy_on = false; 467 } 468 469 clk_disable(pltfm_host->clk); 470 clk_disable(sdhci_arasan->clk_ahb); 471 472 return 0; 473 } 474 475 /** 476 * sdhci_arasan_resume - Resume method for the driver 477 * @dev: Address of the device structure 478 * 479 * Resume operation after suspend 480 * 481 * Return: 0 on success and error value on error 482 */ 483 static int sdhci_arasan_resume(struct device *dev) 484 { 485 struct sdhci_host *host = dev_get_drvdata(dev); 486 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 487 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 488 int ret; 489 490 ret = clk_enable(sdhci_arasan->clk_ahb); 491 if (ret) { 492 dev_err(dev, "Cannot enable AHB clock.\n"); 493 return ret; 494 } 495 496 ret = clk_enable(pltfm_host->clk); 497 if (ret) { 498 dev_err(dev, "Cannot enable SD clock.\n"); 499 return ret; 500 } 501 502 if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) { 503 ret = phy_power_on(sdhci_arasan->phy); 504 if (ret) { 505 dev_err(dev, "Cannot power on phy.\n"); 506 return ret; 507 } 508 sdhci_arasan->is_phy_on = true; 509 } 510 511 ret = sdhci_resume_host(host); 512 if (ret) { 513 dev_err(dev, "Cannot resume host.\n"); 514 return ret; 515 } 516 517 if (sdhci_arasan->has_cqe) 518 return cqhci_resume(host->mmc); 519 520 return 0; 521 } 522 #endif /* ! CONFIG_PM_SLEEP */ 523 524 static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend, 525 sdhci_arasan_resume); 526 527 /** 528 * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate 529 * 530 * @hw: Pointer to the hardware clock structure. 531 * @parent_rate: The parent rate (should be rate of clk_xin). 532 * 533 * Return the current actual rate of the SD card clock. This can be used 534 * to communicate with out PHY. 535 * 536 * Return: The card clock rate. 537 */ 538 static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw, 539 unsigned long parent_rate) 540 { 541 struct sdhci_arasan_clk_data *clk_data = 542 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw); 543 struct sdhci_arasan_data *sdhci_arasan = 544 container_of(clk_data, struct sdhci_arasan_data, clk_data); 545 struct sdhci_host *host = sdhci_arasan->host; 546 547 return host->mmc->actual_clock; 548 } 549 550 static const struct clk_ops arasan_sdcardclk_ops = { 551 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate, 552 }; 553 554 /** 555 * sdhci_arasan_sampleclk_recalc_rate - Return the sampling clock rate 556 * 557 * @hw: Pointer to the hardware clock structure. 558 * @parent_rate: The parent rate (should be rate of clk_xin). 559 * 560 * Return the current actual rate of the sampling clock. This can be used 561 * to communicate with out PHY. 562 * 563 * Return: The sample clock rate. 564 */ 565 static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw, 566 unsigned long parent_rate) 567 { 568 struct sdhci_arasan_clk_data *clk_data = 569 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw); 570 struct sdhci_arasan_data *sdhci_arasan = 571 container_of(clk_data, struct sdhci_arasan_data, clk_data); 572 struct sdhci_host *host = sdhci_arasan->host; 573 574 return host->mmc->actual_clock; 575 } 576 577 static const struct clk_ops arasan_sampleclk_ops = { 578 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate, 579 }; 580 581 /** 582 * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays 583 * 584 * @hw: Pointer to the hardware clock structure. 585 * @degrees: The clock phase shift between 0 - 359. 586 * 587 * Set the SD Output Clock Tap Delays for Output path 588 * 589 * Return: 0 on success and error value on error 590 */ 591 static int sdhci_zynqmp_sdcardclk_set_phase(struct clk_hw *hw, int degrees) 592 { 593 struct sdhci_arasan_clk_data *clk_data = 594 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw); 595 struct sdhci_arasan_data *sdhci_arasan = 596 container_of(clk_data, struct sdhci_arasan_data, clk_data); 597 struct sdhci_host *host = sdhci_arasan->host; 598 const char *clk_name = clk_hw_get_name(hw); 599 u32 node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1; 600 u8 tap_delay, tap_max = 0; 601 int ret; 602 603 /* 604 * This is applicable for SDHCI_SPEC_300 and above 605 * ZynqMP does not set phase for <=25MHz clock. 606 * If degrees is zero, no need to do anything. 607 */ 608 if (host->version < SDHCI_SPEC_300 || 609 host->timing == MMC_TIMING_LEGACY || 610 host->timing == MMC_TIMING_UHS_SDR12 || !degrees) 611 return 0; 612 613 switch (host->timing) { 614 case MMC_TIMING_MMC_HS: 615 case MMC_TIMING_SD_HS: 616 case MMC_TIMING_UHS_SDR25: 617 case MMC_TIMING_UHS_DDR50: 618 case MMC_TIMING_MMC_DDR52: 619 /* For 50MHz clock, 30 Taps are available */ 620 tap_max = 30; 621 break; 622 case MMC_TIMING_UHS_SDR50: 623 /* For 100MHz clock, 15 Taps are available */ 624 tap_max = 15; 625 break; 626 case MMC_TIMING_UHS_SDR104: 627 case MMC_TIMING_MMC_HS200: 628 /* For 200MHz clock, 8 Taps are available */ 629 tap_max = 8; 630 default: 631 break; 632 } 633 634 tap_delay = (degrees * tap_max) / 360; 635 636 /* Set the Clock Phase */ 637 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay); 638 if (ret) 639 pr_err("Error setting Output Tap Delay\n"); 640 641 return ret; 642 } 643 644 static const struct clk_ops zynqmp_sdcardclk_ops = { 645 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate, 646 .set_phase = sdhci_zynqmp_sdcardclk_set_phase, 647 }; 648 649 /** 650 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays 651 * 652 * @hw: Pointer to the hardware clock structure. 653 * @degrees: The clock phase shift between 0 - 359. 654 * 655 * Set the SD Input Clock Tap Delays for Input path 656 * 657 * Return: 0 on success and error value on error 658 */ 659 static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees) 660 { 661 struct sdhci_arasan_clk_data *clk_data = 662 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw); 663 struct sdhci_arasan_data *sdhci_arasan = 664 container_of(clk_data, struct sdhci_arasan_data, clk_data); 665 struct sdhci_host *host = sdhci_arasan->host; 666 const char *clk_name = clk_hw_get_name(hw); 667 u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1; 668 u8 tap_delay, tap_max = 0; 669 int ret; 670 671 /* 672 * This is applicable for SDHCI_SPEC_300 and above 673 * ZynqMP does not set phase for <=25MHz clock. 674 * If degrees is zero, no need to do anything. 675 */ 676 if (host->version < SDHCI_SPEC_300 || 677 host->timing == MMC_TIMING_LEGACY || 678 host->timing == MMC_TIMING_UHS_SDR12 || !degrees) 679 return 0; 680 681 switch (host->timing) { 682 case MMC_TIMING_MMC_HS: 683 case MMC_TIMING_SD_HS: 684 case MMC_TIMING_UHS_SDR25: 685 case MMC_TIMING_UHS_DDR50: 686 case MMC_TIMING_MMC_DDR52: 687 /* For 50MHz clock, 120 Taps are available */ 688 tap_max = 120; 689 break; 690 case MMC_TIMING_UHS_SDR50: 691 /* For 100MHz clock, 60 Taps are available */ 692 tap_max = 60; 693 break; 694 case MMC_TIMING_UHS_SDR104: 695 case MMC_TIMING_MMC_HS200: 696 /* For 200MHz clock, 30 Taps are available */ 697 tap_max = 30; 698 default: 699 break; 700 } 701 702 tap_delay = (degrees * tap_max) / 360; 703 704 /* Set the Clock Phase */ 705 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay); 706 if (ret) 707 pr_err("Error setting Input Tap Delay\n"); 708 709 return ret; 710 } 711 712 static const struct clk_ops zynqmp_sampleclk_ops = { 713 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate, 714 .set_phase = sdhci_zynqmp_sampleclk_set_phase, 715 }; 716 717 /** 718 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays 719 * 720 * @hw: Pointer to the hardware clock structure. 721 * @degrees: The clock phase shift between 0 - 359. 722 * 723 * Set the SD Output Clock Tap Delays for Output path 724 * 725 * Return: 0 on success and error value on error 726 */ 727 static int sdhci_versal_sdcardclk_set_phase(struct clk_hw *hw, int degrees) 728 { 729 struct sdhci_arasan_clk_data *clk_data = 730 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw); 731 struct sdhci_arasan_data *sdhci_arasan = 732 container_of(clk_data, struct sdhci_arasan_data, clk_data); 733 struct sdhci_host *host = sdhci_arasan->host; 734 u8 tap_delay, tap_max = 0; 735 736 /* 737 * This is applicable for SDHCI_SPEC_300 and above 738 * Versal does not set phase for <=25MHz clock. 739 * If degrees is zero, no need to do anything. 740 */ 741 if (host->version < SDHCI_SPEC_300 || 742 host->timing == MMC_TIMING_LEGACY || 743 host->timing == MMC_TIMING_UHS_SDR12 || !degrees) 744 return 0; 745 746 switch (host->timing) { 747 case MMC_TIMING_MMC_HS: 748 case MMC_TIMING_SD_HS: 749 case MMC_TIMING_UHS_SDR25: 750 case MMC_TIMING_UHS_DDR50: 751 case MMC_TIMING_MMC_DDR52: 752 /* For 50MHz clock, 30 Taps are available */ 753 tap_max = 30; 754 break; 755 case MMC_TIMING_UHS_SDR50: 756 /* For 100MHz clock, 15 Taps are available */ 757 tap_max = 15; 758 break; 759 case MMC_TIMING_UHS_SDR104: 760 case MMC_TIMING_MMC_HS200: 761 /* For 200MHz clock, 8 Taps are available */ 762 tap_max = 8; 763 default: 764 break; 765 } 766 767 tap_delay = (degrees * tap_max) / 360; 768 769 /* Set the Clock Phase */ 770 if (tap_delay) { 771 u32 regval; 772 773 regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER); 774 regval |= SDHCI_OTAPDLY_ENABLE; 775 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER); 776 regval |= tap_delay; 777 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER); 778 } 779 780 return 0; 781 } 782 783 static const struct clk_ops versal_sdcardclk_ops = { 784 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate, 785 .set_phase = sdhci_versal_sdcardclk_set_phase, 786 }; 787 788 /** 789 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays 790 * 791 * @hw: Pointer to the hardware clock structure. 792 * @degrees: The clock phase shift between 0 - 359. 793 * 794 * Set the SD Input Clock Tap Delays for Input path 795 * 796 * Return: 0 on success and error value on error 797 */ 798 static int sdhci_versal_sampleclk_set_phase(struct clk_hw *hw, int degrees) 799 { 800 struct sdhci_arasan_clk_data *clk_data = 801 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw); 802 struct sdhci_arasan_data *sdhci_arasan = 803 container_of(clk_data, struct sdhci_arasan_data, clk_data); 804 struct sdhci_host *host = sdhci_arasan->host; 805 u8 tap_delay, tap_max = 0; 806 807 /* 808 * This is applicable for SDHCI_SPEC_300 and above 809 * Versal does not set phase for <=25MHz clock. 810 * If degrees is zero, no need to do anything. 811 */ 812 if (host->version < SDHCI_SPEC_300 || 813 host->timing == MMC_TIMING_LEGACY || 814 host->timing == MMC_TIMING_UHS_SDR12 || !degrees) 815 return 0; 816 817 switch (host->timing) { 818 case MMC_TIMING_MMC_HS: 819 case MMC_TIMING_SD_HS: 820 case MMC_TIMING_UHS_SDR25: 821 case MMC_TIMING_UHS_DDR50: 822 case MMC_TIMING_MMC_DDR52: 823 /* For 50MHz clock, 120 Taps are available */ 824 tap_max = 120; 825 break; 826 case MMC_TIMING_UHS_SDR50: 827 /* For 100MHz clock, 60 Taps are available */ 828 tap_max = 60; 829 break; 830 case MMC_TIMING_UHS_SDR104: 831 case MMC_TIMING_MMC_HS200: 832 /* For 200MHz clock, 30 Taps are available */ 833 tap_max = 30; 834 default: 835 break; 836 } 837 838 tap_delay = (degrees * tap_max) / 360; 839 840 /* Set the Clock Phase */ 841 if (tap_delay) { 842 u32 regval; 843 844 regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER); 845 regval |= SDHCI_ITAPDLY_CHGWIN; 846 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER); 847 regval |= SDHCI_ITAPDLY_ENABLE; 848 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER); 849 regval |= tap_delay; 850 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER); 851 regval &= ~SDHCI_ITAPDLY_CHGWIN; 852 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER); 853 } 854 855 return 0; 856 } 857 858 static const struct clk_ops versal_sampleclk_ops = { 859 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate, 860 .set_phase = sdhci_versal_sampleclk_set_phase, 861 }; 862 863 static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid) 864 { 865 u16 clk; 866 867 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); 868 clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN); 869 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 870 871 /* Issue DLL Reset */ 872 zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE); 873 874 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); 875 876 sdhci_enable_clk(host, clk); 877 } 878 879 static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode) 880 { 881 struct sdhci_host *host = mmc_priv(mmc); 882 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 883 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 884 struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw; 885 const char *clk_name = clk_hw_get_name(hw); 886 u32 device_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : 887 NODE_SD_1; 888 int err; 889 890 arasan_zynqmp_dll_reset(host, device_id); 891 892 err = sdhci_execute_tuning(mmc, opcode); 893 if (err) 894 return err; 895 896 arasan_zynqmp_dll_reset(host, device_id); 897 898 return 0; 899 } 900 901 /** 902 * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier 903 * 904 * @host: The sdhci_host 905 * @value: The value to write 906 * 907 * The corecfg_clockmultiplier is supposed to contain clock multiplier 908 * value of programmable clock generator. 909 * 910 * NOTES: 911 * - Many existing devices don't seem to do this and work fine. To keep 912 * compatibility for old hardware where the device tree doesn't provide a 913 * register map, this function is a noop if a soc_ctl_map hasn't been provided 914 * for this platform. 915 * - The value of corecfg_clockmultiplier should sync with that of corresponding 916 * value reading from sdhci_capability_register. So this function is called 917 * once at probe time and never called again. 918 */ 919 static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host, 920 u32 value) 921 { 922 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 923 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 924 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map = 925 sdhci_arasan->soc_ctl_map; 926 927 /* Having a map is optional */ 928 if (!soc_ctl_map) 929 return; 930 931 /* If we have a map, we expect to have a syscon */ 932 if (!sdhci_arasan->soc_ctl_base) { 933 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n", 934 mmc_hostname(host->mmc)); 935 return; 936 } 937 938 sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value); 939 } 940 941 /** 942 * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq 943 * 944 * @host: The sdhci_host 945 * 946 * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This 947 * function can be used to make that happen. 948 * 949 * NOTES: 950 * - Many existing devices don't seem to do this and work fine. To keep 951 * compatibility for old hardware where the device tree doesn't provide a 952 * register map, this function is a noop if a soc_ctl_map hasn't been provided 953 * for this platform. 954 * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider 955 * to achieve lower clock rates. That means that this function is called once 956 * at probe time and never called again. 957 */ 958 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host) 959 { 960 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 961 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 962 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map = 963 sdhci_arasan->soc_ctl_map; 964 u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000); 965 966 /* Having a map is optional */ 967 if (!soc_ctl_map) 968 return; 969 970 /* If we have a map, we expect to have a syscon */ 971 if (!sdhci_arasan->soc_ctl_base) { 972 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n", 973 mmc_hostname(host->mmc)); 974 return; 975 } 976 977 sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz); 978 } 979 980 static void sdhci_arasan_set_clk_delays(struct sdhci_host *host) 981 { 982 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 983 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 984 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data; 985 986 clk_set_phase(clk_data->sampleclk, 987 clk_data->clk_phase_in[host->timing]); 988 clk_set_phase(clk_data->sdcardclk, 989 clk_data->clk_phase_out[host->timing]); 990 } 991 992 static void arasan_dt_read_clk_phase(struct device *dev, 993 struct sdhci_arasan_clk_data *clk_data, 994 unsigned int timing, const char *prop) 995 { 996 struct device_node *np = dev->of_node; 997 998 int clk_phase[2] = {0}; 999 1000 /* 1001 * Read Tap Delay values from DT, if the DT does not contain the 1002 * Tap Values then use the pre-defined values. 1003 */ 1004 if (of_property_read_variable_u32_array(np, prop, &clk_phase[0], 1005 2, 0)) { 1006 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n", 1007 prop, clk_data->clk_phase_in[timing], 1008 clk_data->clk_phase_out[timing]); 1009 return; 1010 } 1011 1012 /* The values read are Input and Output Clock Delays in order */ 1013 clk_data->clk_phase_in[timing] = clk_phase[0]; 1014 clk_data->clk_phase_out[timing] = clk_phase[1]; 1015 } 1016 1017 /** 1018 * arasan_dt_parse_clk_phases - Read Clock Delay values from DT 1019 * 1020 * @dev: Pointer to our struct device. 1021 * @clk_data: Pointer to the Clock Data structure 1022 * 1023 * Called at initialization to parse the values of Clock Delays. 1024 */ 1025 static void arasan_dt_parse_clk_phases(struct device *dev, 1026 struct sdhci_arasan_clk_data *clk_data) 1027 { 1028 int *iclk_phase, *oclk_phase; 1029 u32 mio_bank = 0; 1030 int i; 1031 1032 /* 1033 * This has been kept as a pointer and is assigned a function here. 1034 * So that different controller variants can assign their own handling 1035 * function. 1036 */ 1037 clk_data->set_clk_delays = sdhci_arasan_set_clk_delays; 1038 1039 if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) { 1040 iclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) ZYNQMP_ICLK_PHASE; 1041 oclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) ZYNQMP_OCLK_PHASE; 1042 1043 of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank); 1044 if (mio_bank == 2) { 1045 oclk_phase[MMC_TIMING_UHS_SDR104] = 90; 1046 oclk_phase[MMC_TIMING_MMC_HS200] = 90; 1047 } 1048 1049 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) { 1050 clk_data->clk_phase_in[i] = iclk_phase[i]; 1051 clk_data->clk_phase_out[i] = oclk_phase[i]; 1052 } 1053 } 1054 1055 if (of_device_is_compatible(dev->of_node, "xlnx,versal-8.9a")) { 1056 iclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) VERSAL_ICLK_PHASE; 1057 oclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) VERSAL_OCLK_PHASE; 1058 1059 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) { 1060 clk_data->clk_phase_in[i] = iclk_phase[i]; 1061 clk_data->clk_phase_out[i] = oclk_phase[i]; 1062 } 1063 } 1064 1065 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY, 1066 "clk-phase-legacy"); 1067 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS, 1068 "clk-phase-mmc-hs"); 1069 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS, 1070 "clk-phase-sd-hs"); 1071 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12, 1072 "clk-phase-uhs-sdr12"); 1073 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25, 1074 "clk-phase-uhs-sdr25"); 1075 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50, 1076 "clk-phase-uhs-sdr50"); 1077 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104, 1078 "clk-phase-uhs-sdr104"); 1079 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50, 1080 "clk-phase-uhs-ddr50"); 1081 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52, 1082 "clk-phase-mmc-ddr52"); 1083 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200, 1084 "clk-phase-mmc-hs200"); 1085 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400, 1086 "clk-phase-mmc-hs400"); 1087 } 1088 1089 static const struct sdhci_pltfm_data sdhci_arasan_pdata = { 1090 .ops = &sdhci_arasan_ops, 1091 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, 1092 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | 1093 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN | 1094 SDHCI_QUIRK2_STOP_WITH_TC, 1095 }; 1096 1097 static const struct sdhci_arasan_clk_ops arasan_clk_ops = { 1098 .sdcardclk_ops = &arasan_sdcardclk_ops, 1099 .sampleclk_ops = &arasan_sampleclk_ops, 1100 }; 1101 1102 static struct sdhci_arasan_of_data sdhci_arasan_generic_data = { 1103 .pdata = &sdhci_arasan_pdata, 1104 .clk_ops = &arasan_clk_ops, 1105 }; 1106 1107 static const struct sdhci_pltfm_data sdhci_keembay_emmc_pdata = { 1108 .ops = &sdhci_arasan_cqe_ops, 1109 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | 1110 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | 1111 SDHCI_QUIRK_NO_LED | 1112 SDHCI_QUIRK_32BIT_DMA_ADDR | 1113 SDHCI_QUIRK_32BIT_DMA_SIZE | 1114 SDHCI_QUIRK_32BIT_ADMA_SIZE, 1115 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | 1116 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN | 1117 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 | 1118 SDHCI_QUIRK2_STOP_WITH_TC | 1119 SDHCI_QUIRK2_BROKEN_64_BIT_DMA, 1120 }; 1121 1122 static const struct sdhci_pltfm_data sdhci_keembay_sd_pdata = { 1123 .ops = &sdhci_arasan_ops, 1124 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | 1125 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | 1126 SDHCI_QUIRK_NO_LED | 1127 SDHCI_QUIRK_32BIT_DMA_ADDR | 1128 SDHCI_QUIRK_32BIT_DMA_SIZE | 1129 SDHCI_QUIRK_32BIT_ADMA_SIZE, 1130 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | 1131 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN | 1132 SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON | 1133 SDHCI_QUIRK2_STOP_WITH_TC | 1134 SDHCI_QUIRK2_BROKEN_64_BIT_DMA, 1135 }; 1136 1137 static const struct sdhci_pltfm_data sdhci_keembay_sdio_pdata = { 1138 .ops = &sdhci_arasan_ops, 1139 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | 1140 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | 1141 SDHCI_QUIRK_NO_LED | 1142 SDHCI_QUIRK_32BIT_DMA_ADDR | 1143 SDHCI_QUIRK_32BIT_DMA_SIZE | 1144 SDHCI_QUIRK_32BIT_ADMA_SIZE, 1145 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | 1146 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN | 1147 SDHCI_QUIRK2_HOST_OFF_CARD_ON | 1148 SDHCI_QUIRK2_BROKEN_64_BIT_DMA, 1149 }; 1150 1151 static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = { 1152 .soc_ctl_map = &rk3399_soc_ctl_map, 1153 .pdata = &sdhci_arasan_cqe_pdata, 1154 .clk_ops = &arasan_clk_ops, 1155 }; 1156 1157 static struct sdhci_arasan_of_data intel_lgm_emmc_data = { 1158 .soc_ctl_map = &intel_lgm_emmc_soc_ctl_map, 1159 .pdata = &sdhci_arasan_cqe_pdata, 1160 .clk_ops = &arasan_clk_ops, 1161 }; 1162 1163 static struct sdhci_arasan_of_data intel_lgm_sdxc_data = { 1164 .soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map, 1165 .pdata = &sdhci_arasan_cqe_pdata, 1166 .clk_ops = &arasan_clk_ops, 1167 }; 1168 1169 static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = { 1170 .ops = &sdhci_arasan_ops, 1171 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | 1172 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN | 1173 SDHCI_QUIRK2_STOP_WITH_TC, 1174 }; 1175 1176 static const struct sdhci_arasan_clk_ops zynqmp_clk_ops = { 1177 .sdcardclk_ops = &zynqmp_sdcardclk_ops, 1178 .sampleclk_ops = &zynqmp_sampleclk_ops, 1179 }; 1180 1181 static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = { 1182 .pdata = &sdhci_arasan_zynqmp_pdata, 1183 .clk_ops = &zynqmp_clk_ops, 1184 }; 1185 1186 static const struct sdhci_arasan_clk_ops versal_clk_ops = { 1187 .sdcardclk_ops = &versal_sdcardclk_ops, 1188 .sampleclk_ops = &versal_sampleclk_ops, 1189 }; 1190 1191 static struct sdhci_arasan_of_data sdhci_arasan_versal_data = { 1192 .pdata = &sdhci_arasan_zynqmp_pdata, 1193 .clk_ops = &versal_clk_ops, 1194 }; 1195 1196 static struct sdhci_arasan_of_data intel_keembay_emmc_data = { 1197 .soc_ctl_map = &intel_keembay_soc_ctl_map, 1198 .pdata = &sdhci_keembay_emmc_pdata, 1199 }; 1200 1201 static struct sdhci_arasan_of_data intel_keembay_sd_data = { 1202 .soc_ctl_map = &intel_keembay_soc_ctl_map, 1203 .pdata = &sdhci_keembay_sd_pdata, 1204 }; 1205 1206 static struct sdhci_arasan_of_data intel_keembay_sdio_data = { 1207 .soc_ctl_map = &intel_keembay_soc_ctl_map, 1208 .pdata = &sdhci_keembay_sdio_pdata, 1209 }; 1210 1211 static const struct of_device_id sdhci_arasan_of_match[] = { 1212 /* SoC-specific compatible strings w/ soc_ctl_map */ 1213 { 1214 .compatible = "rockchip,rk3399-sdhci-5.1", 1215 .data = &sdhci_arasan_rk3399_data, 1216 }, 1217 { 1218 .compatible = "intel,lgm-sdhci-5.1-emmc", 1219 .data = &intel_lgm_emmc_data, 1220 }, 1221 { 1222 .compatible = "intel,lgm-sdhci-5.1-sdxc", 1223 .data = &intel_lgm_sdxc_data, 1224 }, 1225 { 1226 .compatible = "intel,keembay-sdhci-5.1-emmc", 1227 .data = &intel_keembay_emmc_data, 1228 }, 1229 { 1230 .compatible = "intel,keembay-sdhci-5.1-sd", 1231 .data = &intel_keembay_sd_data, 1232 }, 1233 { 1234 .compatible = "intel,keembay-sdhci-5.1-sdio", 1235 .data = &intel_keembay_sdio_data, 1236 }, 1237 /* Generic compatible below here */ 1238 { 1239 .compatible = "arasan,sdhci-8.9a", 1240 .data = &sdhci_arasan_generic_data, 1241 }, 1242 { 1243 .compatible = "arasan,sdhci-5.1", 1244 .data = &sdhci_arasan_generic_data, 1245 }, 1246 { 1247 .compatible = "arasan,sdhci-4.9a", 1248 .data = &sdhci_arasan_generic_data, 1249 }, 1250 { 1251 .compatible = "xlnx,zynqmp-8.9a", 1252 .data = &sdhci_arasan_zynqmp_data, 1253 }, 1254 { 1255 .compatible = "xlnx,versal-8.9a", 1256 .data = &sdhci_arasan_versal_data, 1257 }, 1258 { /* sentinel */ } 1259 }; 1260 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match); 1261 1262 /** 1263 * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use 1264 * 1265 * @sdhci_arasan: Our private data structure. 1266 * @clk_xin: Pointer to the functional clock 1267 * @dev: Pointer to our struct device. 1268 * 1269 * Some PHY devices need to know what the actual card clock is. In order for 1270 * them to find out, we'll provide a clock through the common clock framework 1271 * for them to query. 1272 * 1273 * Return: 0 on success and error value on error 1274 */ 1275 static int 1276 sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan, 1277 struct clk *clk_xin, 1278 struct device *dev) 1279 { 1280 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data; 1281 struct device_node *np = dev->of_node; 1282 struct clk_init_data sdcardclk_init; 1283 const char *parent_clk_name; 1284 int ret; 1285 1286 ret = of_property_read_string_index(np, "clock-output-names", 0, 1287 &sdcardclk_init.name); 1288 if (ret) { 1289 dev_err(dev, "DT has #clock-cells but no clock-output-names\n"); 1290 return ret; 1291 } 1292 1293 parent_clk_name = __clk_get_name(clk_xin); 1294 sdcardclk_init.parent_names = &parent_clk_name; 1295 sdcardclk_init.num_parents = 1; 1296 sdcardclk_init.flags = CLK_GET_RATE_NOCACHE; 1297 sdcardclk_init.ops = sdhci_arasan->clk_ops->sdcardclk_ops; 1298 1299 clk_data->sdcardclk_hw.init = &sdcardclk_init; 1300 clk_data->sdcardclk = 1301 devm_clk_register(dev, &clk_data->sdcardclk_hw); 1302 clk_data->sdcardclk_hw.init = NULL; 1303 1304 ret = of_clk_add_provider(np, of_clk_src_simple_get, 1305 clk_data->sdcardclk); 1306 if (ret) 1307 dev_err(dev, "Failed to add sdcard clock provider\n"); 1308 1309 return ret; 1310 } 1311 1312 /** 1313 * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use 1314 * 1315 * @sdhci_arasan: Our private data structure. 1316 * @clk_xin: Pointer to the functional clock 1317 * @dev: Pointer to our struct device. 1318 * 1319 * Some PHY devices need to know what the actual card clock is. In order for 1320 * them to find out, we'll provide a clock through the common clock framework 1321 * for them to query. 1322 * 1323 * Return: 0 on success and error value on error 1324 */ 1325 static int 1326 sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan, 1327 struct clk *clk_xin, 1328 struct device *dev) 1329 { 1330 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data; 1331 struct device_node *np = dev->of_node; 1332 struct clk_init_data sampleclk_init; 1333 const char *parent_clk_name; 1334 int ret; 1335 1336 ret = of_property_read_string_index(np, "clock-output-names", 1, 1337 &sampleclk_init.name); 1338 if (ret) { 1339 dev_err(dev, "DT has #clock-cells but no clock-output-names\n"); 1340 return ret; 1341 } 1342 1343 parent_clk_name = __clk_get_name(clk_xin); 1344 sampleclk_init.parent_names = &parent_clk_name; 1345 sampleclk_init.num_parents = 1; 1346 sampleclk_init.flags = CLK_GET_RATE_NOCACHE; 1347 sampleclk_init.ops = sdhci_arasan->clk_ops->sampleclk_ops; 1348 1349 clk_data->sampleclk_hw.init = &sampleclk_init; 1350 clk_data->sampleclk = 1351 devm_clk_register(dev, &clk_data->sampleclk_hw); 1352 clk_data->sampleclk_hw.init = NULL; 1353 1354 ret = of_clk_add_provider(np, of_clk_src_simple_get, 1355 clk_data->sampleclk); 1356 if (ret) 1357 dev_err(dev, "Failed to add sample clock provider\n"); 1358 1359 return ret; 1360 } 1361 1362 /** 1363 * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk() 1364 * 1365 * @dev: Pointer to our struct device. 1366 * 1367 * Should be called any time we're exiting and sdhci_arasan_register_sdclk() 1368 * returned success. 1369 */ 1370 static void sdhci_arasan_unregister_sdclk(struct device *dev) 1371 { 1372 struct device_node *np = dev->of_node; 1373 1374 if (!of_find_property(np, "#clock-cells", NULL)) 1375 return; 1376 1377 of_clk_del_provider(dev->of_node); 1378 } 1379 1380 /** 1381 * sdhci_arasan_update_support64b - Set SUPPORT_64B (64-bit System Bus Support) 1382 * 1383 * This should be set based on the System Address Bus. 1384 * 0: the Core supports only 32-bit System Address Bus. 1385 * 1: the Core supports 64-bit System Address Bus. 1386 * 1387 * NOTES: 1388 * - For Keem Bay, it is required to clear this bit. Its default value is 1'b1. 1389 * Keem Bay does not support 64-bit access. 1390 * 1391 * @host The sdhci_host 1392 */ 1393 static void sdhci_arasan_update_support64b(struct sdhci_host *host, u32 value) 1394 { 1395 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1396 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 1397 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map = 1398 sdhci_arasan->soc_ctl_map; 1399 1400 /* Having a map is optional */ 1401 if (!soc_ctl_map) 1402 return; 1403 1404 /* If we have a map, we expect to have a syscon */ 1405 if (!sdhci_arasan->soc_ctl_base) { 1406 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n", 1407 mmc_hostname(host->mmc)); 1408 return; 1409 } 1410 1411 sdhci_arasan_syscon_write(host, &soc_ctl_map->support64b, value); 1412 } 1413 1414 /** 1415 * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use 1416 * 1417 * @sdhci_arasan: Our private data structure. 1418 * @clk_xin: Pointer to the functional clock 1419 * @dev: Pointer to our struct device. 1420 * 1421 * Some PHY devices need to know what the actual card clock is. In order for 1422 * them to find out, we'll provide a clock through the common clock framework 1423 * for them to query. 1424 * 1425 * Note: without seriously re-architecting SDHCI's clock code and testing on 1426 * all platforms, there's no way to create a totally beautiful clock here 1427 * with all clock ops implemented. Instead, we'll just create a clock that can 1428 * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock 1429 * framework that we're doing things behind its back. This should be sufficient 1430 * to create nice clean device tree bindings and later (if needed) we can try 1431 * re-architecting SDHCI if we see some benefit to it. 1432 * 1433 * Return: 0 on success and error value on error 1434 */ 1435 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan, 1436 struct clk *clk_xin, 1437 struct device *dev) 1438 { 1439 struct device_node *np = dev->of_node; 1440 u32 num_clks = 0; 1441 int ret; 1442 1443 /* Providing a clock to the PHY is optional; no error if missing */ 1444 if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0) 1445 return 0; 1446 1447 ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev); 1448 if (ret) 1449 return ret; 1450 1451 if (num_clks) { 1452 ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin, 1453 dev); 1454 if (ret) { 1455 sdhci_arasan_unregister_sdclk(dev); 1456 return ret; 1457 } 1458 } 1459 1460 return 0; 1461 } 1462 1463 static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan) 1464 { 1465 struct sdhci_host *host = sdhci_arasan->host; 1466 struct cqhci_host *cq_host; 1467 bool dma64; 1468 int ret; 1469 1470 if (!sdhci_arasan->has_cqe) 1471 return sdhci_add_host(host); 1472 1473 ret = sdhci_setup_host(host); 1474 if (ret) 1475 return ret; 1476 1477 cq_host = devm_kzalloc(host->mmc->parent, 1478 sizeof(*cq_host), GFP_KERNEL); 1479 if (!cq_host) { 1480 ret = -ENOMEM; 1481 goto cleanup; 1482 } 1483 1484 cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR; 1485 cq_host->ops = &sdhci_arasan_cqhci_ops; 1486 1487 dma64 = host->flags & SDHCI_USE_64_BIT_DMA; 1488 if (dma64) 1489 cq_host->caps |= CQHCI_TASK_DESC_SZ_128; 1490 1491 ret = cqhci_init(cq_host, host->mmc, dma64); 1492 if (ret) 1493 goto cleanup; 1494 1495 ret = __sdhci_add_host(host); 1496 if (ret) 1497 goto cleanup; 1498 1499 return 0; 1500 1501 cleanup: 1502 sdhci_cleanup_host(host); 1503 return ret; 1504 } 1505 1506 static int sdhci_arasan_probe(struct platform_device *pdev) 1507 { 1508 int ret; 1509 const struct of_device_id *match; 1510 struct device_node *node; 1511 struct clk *clk_xin; 1512 struct sdhci_host *host; 1513 struct sdhci_pltfm_host *pltfm_host; 1514 struct sdhci_arasan_data *sdhci_arasan; 1515 struct device_node *np = pdev->dev.of_node; 1516 const struct sdhci_arasan_of_data *data; 1517 1518 match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node); 1519 data = match->data; 1520 host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan)); 1521 1522 if (IS_ERR(host)) 1523 return PTR_ERR(host); 1524 1525 pltfm_host = sdhci_priv(host); 1526 sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 1527 sdhci_arasan->host = host; 1528 1529 sdhci_arasan->soc_ctl_map = data->soc_ctl_map; 1530 sdhci_arasan->clk_ops = data->clk_ops; 1531 1532 node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0); 1533 if (node) { 1534 sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node); 1535 of_node_put(node); 1536 1537 if (IS_ERR(sdhci_arasan->soc_ctl_base)) { 1538 ret = PTR_ERR(sdhci_arasan->soc_ctl_base); 1539 if (ret != -EPROBE_DEFER) 1540 dev_err(&pdev->dev, "Can't get syscon: %d\n", 1541 ret); 1542 goto err_pltfm_free; 1543 } 1544 } 1545 1546 sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb"); 1547 if (IS_ERR(sdhci_arasan->clk_ahb)) { 1548 dev_err(&pdev->dev, "clk_ahb clock not found.\n"); 1549 ret = PTR_ERR(sdhci_arasan->clk_ahb); 1550 goto err_pltfm_free; 1551 } 1552 1553 clk_xin = devm_clk_get(&pdev->dev, "clk_xin"); 1554 if (IS_ERR(clk_xin)) { 1555 dev_err(&pdev->dev, "clk_xin clock not found.\n"); 1556 ret = PTR_ERR(clk_xin); 1557 goto err_pltfm_free; 1558 } 1559 1560 ret = clk_prepare_enable(sdhci_arasan->clk_ahb); 1561 if (ret) { 1562 dev_err(&pdev->dev, "Unable to enable AHB clock.\n"); 1563 goto err_pltfm_free; 1564 } 1565 1566 ret = clk_prepare_enable(clk_xin); 1567 if (ret) { 1568 dev_err(&pdev->dev, "Unable to enable SD clock.\n"); 1569 goto clk_dis_ahb; 1570 } 1571 1572 sdhci_get_of_property(pdev); 1573 1574 if (of_property_read_bool(np, "xlnx,fails-without-test-cd")) 1575 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST; 1576 1577 if (of_property_read_bool(np, "xlnx,int-clock-stable-broken")) 1578 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE; 1579 1580 pltfm_host->clk = clk_xin; 1581 1582 if (of_device_is_compatible(pdev->dev.of_node, 1583 "rockchip,rk3399-sdhci-5.1")) 1584 sdhci_arasan_update_clockmultiplier(host, 0x0); 1585 1586 if (of_device_is_compatible(np, "intel,keembay-sdhci-5.1-emmc") || 1587 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sd") || 1588 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sdio")) { 1589 sdhci_arasan_update_clockmultiplier(host, 0x0); 1590 sdhci_arasan_update_support64b(host, 0x0); 1591 1592 host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY; 1593 } 1594 1595 sdhci_arasan_update_baseclkfreq(host); 1596 1597 ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev); 1598 if (ret) 1599 goto clk_disable_all; 1600 1601 if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) { 1602 host->mmc_host_ops.execute_tuning = 1603 arasan_zynqmp_execute_tuning; 1604 } 1605 1606 arasan_dt_parse_clk_phases(&pdev->dev, &sdhci_arasan->clk_data); 1607 1608 ret = mmc_of_parse(host->mmc); 1609 if (ret) { 1610 if (ret != -EPROBE_DEFER) 1611 dev_err(&pdev->dev, "parsing dt failed (%d)\n", ret); 1612 goto unreg_clk; 1613 } 1614 1615 sdhci_arasan->phy = ERR_PTR(-ENODEV); 1616 if (of_device_is_compatible(pdev->dev.of_node, 1617 "arasan,sdhci-5.1")) { 1618 sdhci_arasan->phy = devm_phy_get(&pdev->dev, 1619 "phy_arasan"); 1620 if (IS_ERR(sdhci_arasan->phy)) { 1621 ret = PTR_ERR(sdhci_arasan->phy); 1622 dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n"); 1623 goto unreg_clk; 1624 } 1625 1626 ret = phy_init(sdhci_arasan->phy); 1627 if (ret < 0) { 1628 dev_err(&pdev->dev, "phy_init err.\n"); 1629 goto unreg_clk; 1630 } 1631 1632 host->mmc_host_ops.hs400_enhanced_strobe = 1633 sdhci_arasan_hs400_enhanced_strobe; 1634 host->mmc_host_ops.start_signal_voltage_switch = 1635 sdhci_arasan_voltage_switch; 1636 sdhci_arasan->has_cqe = true; 1637 host->mmc->caps2 |= MMC_CAP2_CQE; 1638 1639 if (!of_property_read_bool(np, "disable-cqe-dcmd")) 1640 host->mmc->caps2 |= MMC_CAP2_CQE_DCMD; 1641 } 1642 1643 ret = sdhci_arasan_add_host(sdhci_arasan); 1644 if (ret) 1645 goto err_add_host; 1646 1647 return 0; 1648 1649 err_add_host: 1650 if (!IS_ERR(sdhci_arasan->phy)) 1651 phy_exit(sdhci_arasan->phy); 1652 unreg_clk: 1653 sdhci_arasan_unregister_sdclk(&pdev->dev); 1654 clk_disable_all: 1655 clk_disable_unprepare(clk_xin); 1656 clk_dis_ahb: 1657 clk_disable_unprepare(sdhci_arasan->clk_ahb); 1658 err_pltfm_free: 1659 sdhci_pltfm_free(pdev); 1660 return ret; 1661 } 1662 1663 static int sdhci_arasan_remove(struct platform_device *pdev) 1664 { 1665 int ret; 1666 struct sdhci_host *host = platform_get_drvdata(pdev); 1667 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 1668 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); 1669 struct clk *clk_ahb = sdhci_arasan->clk_ahb; 1670 1671 if (!IS_ERR(sdhci_arasan->phy)) { 1672 if (sdhci_arasan->is_phy_on) 1673 phy_power_off(sdhci_arasan->phy); 1674 phy_exit(sdhci_arasan->phy); 1675 } 1676 1677 sdhci_arasan_unregister_sdclk(&pdev->dev); 1678 1679 ret = sdhci_pltfm_unregister(pdev); 1680 1681 clk_disable_unprepare(clk_ahb); 1682 1683 return ret; 1684 } 1685 1686 static struct platform_driver sdhci_arasan_driver = { 1687 .driver = { 1688 .name = "sdhci-arasan", 1689 .of_match_table = sdhci_arasan_of_match, 1690 .pm = &sdhci_arasan_dev_pm_ops, 1691 }, 1692 .probe = sdhci_arasan_probe, 1693 .remove = sdhci_arasan_remove, 1694 }; 1695 1696 module_platform_driver(sdhci_arasan_driver); 1697 1698 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller"); 1699 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>"); 1700 MODULE_LICENSE("GPL"); 1701