162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Atmel SDMMC controller driver. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2015 Atmel, 662306a36Sopenharmony_ci * 2015 Ludovic Desroches <ludovic.desroches@atmel.com> 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <linux/bitfield.h> 1062306a36Sopenharmony_ci#include <linux/clk.h> 1162306a36Sopenharmony_ci#include <linux/delay.h> 1262306a36Sopenharmony_ci#include <linux/err.h> 1362306a36Sopenharmony_ci#include <linux/io.h> 1462306a36Sopenharmony_ci#include <linux/iopoll.h> 1562306a36Sopenharmony_ci#include <linux/kernel.h> 1662306a36Sopenharmony_ci#include <linux/mmc/host.h> 1762306a36Sopenharmony_ci#include <linux/mmc/slot-gpio.h> 1862306a36Sopenharmony_ci#include <linux/module.h> 1962306a36Sopenharmony_ci#include <linux/of.h> 2062306a36Sopenharmony_ci#include <linux/platform_device.h> 2162306a36Sopenharmony_ci#include <linux/pm.h> 2262306a36Sopenharmony_ci#include <linux/pm_runtime.h> 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci#include "sdhci-pltfm.h" 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci#define SDMMC_MC1R 0x204 2762306a36Sopenharmony_ci#define SDMMC_MC1R_DDR BIT(3) 2862306a36Sopenharmony_ci#define SDMMC_MC1R_FCD BIT(7) 2962306a36Sopenharmony_ci#define SDMMC_CACR 0x230 3062306a36Sopenharmony_ci#define SDMMC_CACR_CAPWREN BIT(0) 3162306a36Sopenharmony_ci#define SDMMC_CACR_KEY (0x46 << 8) 3262306a36Sopenharmony_ci#define SDMMC_CALCR 0x240 3362306a36Sopenharmony_ci#define SDMMC_CALCR_EN BIT(0) 3462306a36Sopenharmony_ci#define SDMMC_CALCR_ALWYSON BIT(4) 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci#define SDHCI_AT91_PRESET_COMMON_CONF 0x400 /* drv type B, programmable clock mode */ 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_cistruct sdhci_at91_soc_data { 3962306a36Sopenharmony_ci const struct sdhci_pltfm_data *pdata; 4062306a36Sopenharmony_ci bool baseclk_is_generated_internally; 4162306a36Sopenharmony_ci unsigned int divider_for_baseclk; 4262306a36Sopenharmony_ci}; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_cistruct sdhci_at91_priv { 4562306a36Sopenharmony_ci const struct sdhci_at91_soc_data *soc_data; 4662306a36Sopenharmony_ci struct clk *hclock; 4762306a36Sopenharmony_ci struct clk *gck; 4862306a36Sopenharmony_ci struct clk *mainck; 4962306a36Sopenharmony_ci bool restore_needed; 5062306a36Sopenharmony_ci bool cal_always_on; 5162306a36Sopenharmony_ci}; 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_cistatic void sdhci_at91_set_force_card_detect(struct sdhci_host *host) 5462306a36Sopenharmony_ci{ 5562306a36Sopenharmony_ci u8 mc1r; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci mc1r = readb(host->ioaddr + SDMMC_MC1R); 5862306a36Sopenharmony_ci mc1r |= SDMMC_MC1R_FCD; 5962306a36Sopenharmony_ci writeb(mc1r, host->ioaddr + SDMMC_MC1R); 6062306a36Sopenharmony_ci} 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_cistatic void sdhci_at91_set_clock(struct sdhci_host *host, unsigned int clock) 6362306a36Sopenharmony_ci{ 6462306a36Sopenharmony_ci u16 clk; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci host->mmc->actual_clock = 0; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci /* 6962306a36Sopenharmony_ci * There is no requirement to disable the internal clock before 7062306a36Sopenharmony_ci * changing the SD clock configuration. Moreover, disabling the 7162306a36Sopenharmony_ci * internal clock, changing the configuration and re-enabling the 7262306a36Sopenharmony_ci * internal clock causes some bugs. It can prevent to get the internal 7362306a36Sopenharmony_ci * clock stable flag ready and an unexpected switch to the base clock 7462306a36Sopenharmony_ci * when using presets. 7562306a36Sopenharmony_ci */ 7662306a36Sopenharmony_ci clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); 7762306a36Sopenharmony_ci clk &= SDHCI_CLOCK_INT_EN; 7862306a36Sopenharmony_ci sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci if (clock == 0) 8162306a36Sopenharmony_ci return; 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock); 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci clk |= SDHCI_CLOCK_INT_EN; 8662306a36Sopenharmony_ci sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci /* Wait max 20 ms */ 8962306a36Sopenharmony_ci if (read_poll_timeout(sdhci_readw, clk, (clk & SDHCI_CLOCK_INT_STABLE), 9062306a36Sopenharmony_ci 1000, 20000, false, host, SDHCI_CLOCK_CONTROL)) { 9162306a36Sopenharmony_ci pr_err("%s: Internal clock never stabilised.\n", 9262306a36Sopenharmony_ci mmc_hostname(host->mmc)); 9362306a36Sopenharmony_ci return; 9462306a36Sopenharmony_ci } 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci clk |= SDHCI_CLOCK_CARD_EN; 9762306a36Sopenharmony_ci sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); 9862306a36Sopenharmony_ci} 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_cistatic void sdhci_at91_set_uhs_signaling(struct sdhci_host *host, 10162306a36Sopenharmony_ci unsigned int timing) 10262306a36Sopenharmony_ci{ 10362306a36Sopenharmony_ci u8 mc1r; 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci if (timing == MMC_TIMING_MMC_DDR52) { 10662306a36Sopenharmony_ci mc1r = sdhci_readb(host, SDMMC_MC1R); 10762306a36Sopenharmony_ci mc1r |= SDMMC_MC1R_DDR; 10862306a36Sopenharmony_ci sdhci_writeb(host, mc1r, SDMMC_MC1R); 10962306a36Sopenharmony_ci } 11062306a36Sopenharmony_ci sdhci_set_uhs_signaling(host, timing); 11162306a36Sopenharmony_ci} 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_cistatic void sdhci_at91_reset(struct sdhci_host *host, u8 mask) 11462306a36Sopenharmony_ci{ 11562306a36Sopenharmony_ci struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 11662306a36Sopenharmony_ci struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); 11762306a36Sopenharmony_ci unsigned int tmp; 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci sdhci_reset(host, mask); 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) 12262306a36Sopenharmony_ci || mmc_gpio_get_cd(host->mmc) >= 0) 12362306a36Sopenharmony_ci sdhci_at91_set_force_card_detect(host); 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci if (priv->cal_always_on && (mask & SDHCI_RESET_ALL)) { 12662306a36Sopenharmony_ci u32 calcr = sdhci_readl(host, SDMMC_CALCR); 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci sdhci_writel(host, calcr | SDMMC_CALCR_ALWYSON | SDMMC_CALCR_EN, 12962306a36Sopenharmony_ci SDMMC_CALCR); 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci if (read_poll_timeout(sdhci_readl, tmp, !(tmp & SDMMC_CALCR_EN), 13262306a36Sopenharmony_ci 10, 20000, false, host, SDMMC_CALCR)) 13362306a36Sopenharmony_ci dev_err(mmc_dev(host->mmc), "Failed to calibrate\n"); 13462306a36Sopenharmony_ci } 13562306a36Sopenharmony_ci} 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_cistatic const struct sdhci_ops sdhci_at91_sama5d2_ops = { 13862306a36Sopenharmony_ci .set_clock = sdhci_at91_set_clock, 13962306a36Sopenharmony_ci .set_bus_width = sdhci_set_bus_width, 14062306a36Sopenharmony_ci .reset = sdhci_at91_reset, 14162306a36Sopenharmony_ci .set_uhs_signaling = sdhci_at91_set_uhs_signaling, 14262306a36Sopenharmony_ci .set_power = sdhci_set_power_and_bus_voltage, 14362306a36Sopenharmony_ci}; 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_cistatic const struct sdhci_pltfm_data sdhci_sama5d2_pdata = { 14662306a36Sopenharmony_ci .ops = &sdhci_at91_sama5d2_ops, 14762306a36Sopenharmony_ci}; 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_cistatic const struct sdhci_at91_soc_data soc_data_sama5d2 = { 15062306a36Sopenharmony_ci .pdata = &sdhci_sama5d2_pdata, 15162306a36Sopenharmony_ci .baseclk_is_generated_internally = false, 15262306a36Sopenharmony_ci}; 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_cistatic const struct sdhci_at91_soc_data soc_data_sam9x60 = { 15562306a36Sopenharmony_ci .pdata = &sdhci_sama5d2_pdata, 15662306a36Sopenharmony_ci .baseclk_is_generated_internally = true, 15762306a36Sopenharmony_ci .divider_for_baseclk = 2, 15862306a36Sopenharmony_ci}; 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_cistatic const struct of_device_id sdhci_at91_dt_match[] = { 16162306a36Sopenharmony_ci { .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 }, 16262306a36Sopenharmony_ci { .compatible = "microchip,sam9x60-sdhci", .data = &soc_data_sam9x60 }, 16362306a36Sopenharmony_ci {} 16462306a36Sopenharmony_ci}; 16562306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, sdhci_at91_dt_match); 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_cistatic int sdhci_at91_set_clks_presets(struct device *dev) 16862306a36Sopenharmony_ci{ 16962306a36Sopenharmony_ci struct sdhci_host *host = dev_get_drvdata(dev); 17062306a36Sopenharmony_ci struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 17162306a36Sopenharmony_ci struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); 17262306a36Sopenharmony_ci unsigned int caps0, caps1; 17362306a36Sopenharmony_ci unsigned int clk_base, clk_mul; 17462306a36Sopenharmony_ci unsigned int gck_rate, clk_base_rate; 17562306a36Sopenharmony_ci unsigned int preset_div; 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci clk_prepare_enable(priv->hclock); 17862306a36Sopenharmony_ci caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES); 17962306a36Sopenharmony_ci caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1); 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci gck_rate = clk_get_rate(priv->gck); 18262306a36Sopenharmony_ci if (priv->soc_data->baseclk_is_generated_internally) 18362306a36Sopenharmony_ci clk_base_rate = gck_rate / priv->soc_data->divider_for_baseclk; 18462306a36Sopenharmony_ci else 18562306a36Sopenharmony_ci clk_base_rate = clk_get_rate(priv->mainck); 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci clk_base = clk_base_rate / 1000000; 18862306a36Sopenharmony_ci clk_mul = gck_rate / clk_base_rate - 1; 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci caps0 &= ~SDHCI_CLOCK_V3_BASE_MASK; 19162306a36Sopenharmony_ci caps0 |= FIELD_PREP(SDHCI_CLOCK_V3_BASE_MASK, clk_base); 19262306a36Sopenharmony_ci caps1 &= ~SDHCI_CLOCK_MUL_MASK; 19362306a36Sopenharmony_ci caps1 |= FIELD_PREP(SDHCI_CLOCK_MUL_MASK, clk_mul); 19462306a36Sopenharmony_ci /* Set capabilities in r/w mode. */ 19562306a36Sopenharmony_ci writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR); 19662306a36Sopenharmony_ci writel(caps0, host->ioaddr + SDHCI_CAPABILITIES); 19762306a36Sopenharmony_ci writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1); 19862306a36Sopenharmony_ci /* Set capabilities in ro mode. */ 19962306a36Sopenharmony_ci writel(0, host->ioaddr + SDMMC_CACR); 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci dev_dbg(dev, "update clk mul to %u as gck rate is %u Hz and clk base is %u Hz\n", 20262306a36Sopenharmony_ci clk_mul, gck_rate, clk_base_rate); 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci /* 20562306a36Sopenharmony_ci * We have to set preset values because it depends on the clk_mul 20662306a36Sopenharmony_ci * value. Moreover, SDR104 is supported in a degraded mode since the 20762306a36Sopenharmony_ci * maximum sd clock value is 120 MHz instead of 208 MHz. For that 20862306a36Sopenharmony_ci * reason, we need to use presets to support SDR104. 20962306a36Sopenharmony_ci */ 21062306a36Sopenharmony_ci preset_div = DIV_ROUND_UP(gck_rate, 24000000) - 1; 21162306a36Sopenharmony_ci writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, 21262306a36Sopenharmony_ci host->ioaddr + SDHCI_PRESET_FOR_SDR12); 21362306a36Sopenharmony_ci preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1; 21462306a36Sopenharmony_ci writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, 21562306a36Sopenharmony_ci host->ioaddr + SDHCI_PRESET_FOR_SDR25); 21662306a36Sopenharmony_ci preset_div = DIV_ROUND_UP(gck_rate, 100000000) - 1; 21762306a36Sopenharmony_ci writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, 21862306a36Sopenharmony_ci host->ioaddr + SDHCI_PRESET_FOR_SDR50); 21962306a36Sopenharmony_ci preset_div = DIV_ROUND_UP(gck_rate, 120000000) - 1; 22062306a36Sopenharmony_ci writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, 22162306a36Sopenharmony_ci host->ioaddr + SDHCI_PRESET_FOR_SDR104); 22262306a36Sopenharmony_ci preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1; 22362306a36Sopenharmony_ci writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, 22462306a36Sopenharmony_ci host->ioaddr + SDHCI_PRESET_FOR_DDR50); 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci clk_prepare_enable(priv->mainck); 22762306a36Sopenharmony_ci clk_prepare_enable(priv->gck); 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci return 0; 23062306a36Sopenharmony_ci} 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 23362306a36Sopenharmony_cistatic int sdhci_at91_suspend(struct device *dev) 23462306a36Sopenharmony_ci{ 23562306a36Sopenharmony_ci struct sdhci_host *host = dev_get_drvdata(dev); 23662306a36Sopenharmony_ci struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 23762306a36Sopenharmony_ci struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); 23862306a36Sopenharmony_ci int ret; 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci ret = pm_runtime_force_suspend(dev); 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci priv->restore_needed = true; 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci return ret; 24562306a36Sopenharmony_ci} 24662306a36Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */ 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci#ifdef CONFIG_PM 24962306a36Sopenharmony_cistatic int sdhci_at91_runtime_suspend(struct device *dev) 25062306a36Sopenharmony_ci{ 25162306a36Sopenharmony_ci struct sdhci_host *host = dev_get_drvdata(dev); 25262306a36Sopenharmony_ci struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 25362306a36Sopenharmony_ci struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); 25462306a36Sopenharmony_ci int ret; 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci ret = sdhci_runtime_suspend_host(host); 25762306a36Sopenharmony_ci 25862306a36Sopenharmony_ci if (host->tuning_mode != SDHCI_TUNING_MODE_3) 25962306a36Sopenharmony_ci mmc_retune_needed(host->mmc); 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci clk_disable_unprepare(priv->gck); 26262306a36Sopenharmony_ci clk_disable_unprepare(priv->hclock); 26362306a36Sopenharmony_ci clk_disable_unprepare(priv->mainck); 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci return ret; 26662306a36Sopenharmony_ci} 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_cistatic int sdhci_at91_runtime_resume(struct device *dev) 26962306a36Sopenharmony_ci{ 27062306a36Sopenharmony_ci struct sdhci_host *host = dev_get_drvdata(dev); 27162306a36Sopenharmony_ci struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 27262306a36Sopenharmony_ci struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); 27362306a36Sopenharmony_ci int ret; 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci if (priv->restore_needed) { 27662306a36Sopenharmony_ci ret = sdhci_at91_set_clks_presets(dev); 27762306a36Sopenharmony_ci if (ret) 27862306a36Sopenharmony_ci return ret; 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci priv->restore_needed = false; 28162306a36Sopenharmony_ci goto out; 28262306a36Sopenharmony_ci } 28362306a36Sopenharmony_ci 28462306a36Sopenharmony_ci ret = clk_prepare_enable(priv->mainck); 28562306a36Sopenharmony_ci if (ret) { 28662306a36Sopenharmony_ci dev_err(dev, "can't enable mainck\n"); 28762306a36Sopenharmony_ci return ret; 28862306a36Sopenharmony_ci } 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ci ret = clk_prepare_enable(priv->hclock); 29162306a36Sopenharmony_ci if (ret) { 29262306a36Sopenharmony_ci dev_err(dev, "can't enable hclock\n"); 29362306a36Sopenharmony_ci return ret; 29462306a36Sopenharmony_ci } 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ci ret = clk_prepare_enable(priv->gck); 29762306a36Sopenharmony_ci if (ret) { 29862306a36Sopenharmony_ci dev_err(dev, "can't enable gck\n"); 29962306a36Sopenharmony_ci return ret; 30062306a36Sopenharmony_ci } 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ciout: 30362306a36Sopenharmony_ci return sdhci_runtime_resume_host(host, 0); 30462306a36Sopenharmony_ci} 30562306a36Sopenharmony_ci#endif /* CONFIG_PM */ 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_cistatic const struct dev_pm_ops sdhci_at91_dev_pm_ops = { 30862306a36Sopenharmony_ci SET_SYSTEM_SLEEP_PM_OPS(sdhci_at91_suspend, pm_runtime_force_resume) 30962306a36Sopenharmony_ci SET_RUNTIME_PM_OPS(sdhci_at91_runtime_suspend, 31062306a36Sopenharmony_ci sdhci_at91_runtime_resume, 31162306a36Sopenharmony_ci NULL) 31262306a36Sopenharmony_ci}; 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_cistatic int sdhci_at91_probe(struct platform_device *pdev) 31562306a36Sopenharmony_ci{ 31662306a36Sopenharmony_ci const struct sdhci_at91_soc_data *soc_data; 31762306a36Sopenharmony_ci struct sdhci_host *host; 31862306a36Sopenharmony_ci struct sdhci_pltfm_host *pltfm_host; 31962306a36Sopenharmony_ci struct sdhci_at91_priv *priv; 32062306a36Sopenharmony_ci int ret; 32162306a36Sopenharmony_ci 32262306a36Sopenharmony_ci soc_data = of_device_get_match_data(&pdev->dev); 32362306a36Sopenharmony_ci if (!soc_data) 32462306a36Sopenharmony_ci return -EINVAL; 32562306a36Sopenharmony_ci 32662306a36Sopenharmony_ci host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*priv)); 32762306a36Sopenharmony_ci if (IS_ERR(host)) 32862306a36Sopenharmony_ci return PTR_ERR(host); 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ci pltfm_host = sdhci_priv(host); 33162306a36Sopenharmony_ci priv = sdhci_pltfm_priv(pltfm_host); 33262306a36Sopenharmony_ci priv->soc_data = soc_data; 33362306a36Sopenharmony_ci 33462306a36Sopenharmony_ci priv->mainck = devm_clk_get(&pdev->dev, "baseclk"); 33562306a36Sopenharmony_ci if (IS_ERR(priv->mainck)) { 33662306a36Sopenharmony_ci if (soc_data->baseclk_is_generated_internally) { 33762306a36Sopenharmony_ci priv->mainck = NULL; 33862306a36Sopenharmony_ci } else { 33962306a36Sopenharmony_ci dev_err(&pdev->dev, "failed to get baseclk\n"); 34062306a36Sopenharmony_ci ret = PTR_ERR(priv->mainck); 34162306a36Sopenharmony_ci goto sdhci_pltfm_free; 34262306a36Sopenharmony_ci } 34362306a36Sopenharmony_ci } 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ci priv->hclock = devm_clk_get(&pdev->dev, "hclock"); 34662306a36Sopenharmony_ci if (IS_ERR(priv->hclock)) { 34762306a36Sopenharmony_ci dev_err(&pdev->dev, "failed to get hclock\n"); 34862306a36Sopenharmony_ci ret = PTR_ERR(priv->hclock); 34962306a36Sopenharmony_ci goto sdhci_pltfm_free; 35062306a36Sopenharmony_ci } 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_ci priv->gck = devm_clk_get(&pdev->dev, "multclk"); 35362306a36Sopenharmony_ci if (IS_ERR(priv->gck)) { 35462306a36Sopenharmony_ci dev_err(&pdev->dev, "failed to get multclk\n"); 35562306a36Sopenharmony_ci ret = PTR_ERR(priv->gck); 35662306a36Sopenharmony_ci goto sdhci_pltfm_free; 35762306a36Sopenharmony_ci } 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci ret = sdhci_at91_set_clks_presets(&pdev->dev); 36062306a36Sopenharmony_ci if (ret) 36162306a36Sopenharmony_ci goto sdhci_pltfm_free; 36262306a36Sopenharmony_ci 36362306a36Sopenharmony_ci priv->restore_needed = false; 36462306a36Sopenharmony_ci 36562306a36Sopenharmony_ci /* 36662306a36Sopenharmony_ci * if SDCAL pin is wrongly connected, we must enable 36762306a36Sopenharmony_ci * the analog calibration cell permanently. 36862306a36Sopenharmony_ci */ 36962306a36Sopenharmony_ci priv->cal_always_on = 37062306a36Sopenharmony_ci device_property_read_bool(&pdev->dev, 37162306a36Sopenharmony_ci "microchip,sdcal-inverted"); 37262306a36Sopenharmony_ci 37362306a36Sopenharmony_ci ret = mmc_of_parse(host->mmc); 37462306a36Sopenharmony_ci if (ret) 37562306a36Sopenharmony_ci goto clocks_disable_unprepare; 37662306a36Sopenharmony_ci 37762306a36Sopenharmony_ci sdhci_get_of_property(pdev); 37862306a36Sopenharmony_ci 37962306a36Sopenharmony_ci pm_runtime_get_noresume(&pdev->dev); 38062306a36Sopenharmony_ci pm_runtime_set_active(&pdev->dev); 38162306a36Sopenharmony_ci pm_runtime_enable(&pdev->dev); 38262306a36Sopenharmony_ci pm_runtime_set_autosuspend_delay(&pdev->dev, 50); 38362306a36Sopenharmony_ci pm_runtime_use_autosuspend(&pdev->dev); 38462306a36Sopenharmony_ci 38562306a36Sopenharmony_ci /* HS200 is broken at this moment */ 38662306a36Sopenharmony_ci host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200; 38762306a36Sopenharmony_ci 38862306a36Sopenharmony_ci ret = sdhci_add_host(host); 38962306a36Sopenharmony_ci if (ret) 39062306a36Sopenharmony_ci goto pm_runtime_disable; 39162306a36Sopenharmony_ci 39262306a36Sopenharmony_ci /* 39362306a36Sopenharmony_ci * When calling sdhci_runtime_suspend_host(), the sdhci layer makes 39462306a36Sopenharmony_ci * the assumption that all the clocks of the controller are disabled. 39562306a36Sopenharmony_ci * It means we can't get irq from it when it is runtime suspended. 39662306a36Sopenharmony_ci * For that reason, it is not planned to wake-up on a card detect irq 39762306a36Sopenharmony_ci * from the controller. 39862306a36Sopenharmony_ci * If we want to use runtime PM and to be able to wake-up on card 39962306a36Sopenharmony_ci * insertion, we have to use a GPIO for the card detection or we can 40062306a36Sopenharmony_ci * use polling. Be aware that using polling will resume/suspend the 40162306a36Sopenharmony_ci * controller between each attempt. 40262306a36Sopenharmony_ci * Disable SDHCI_QUIRK_BROKEN_CARD_DETECTION to be sure nobody tries 40362306a36Sopenharmony_ci * to enable polling via device tree with broken-cd property. 40462306a36Sopenharmony_ci */ 40562306a36Sopenharmony_ci if (mmc_card_is_removable(host->mmc) && 40662306a36Sopenharmony_ci mmc_gpio_get_cd(host->mmc) < 0) { 40762306a36Sopenharmony_ci host->mmc->caps |= MMC_CAP_NEEDS_POLL; 40862306a36Sopenharmony_ci host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; 40962306a36Sopenharmony_ci } 41062306a36Sopenharmony_ci 41162306a36Sopenharmony_ci /* 41262306a36Sopenharmony_ci * If the device attached to the MMC bus is not removable, it is safer 41362306a36Sopenharmony_ci * to set the Force Card Detect bit. People often don't connect the 41462306a36Sopenharmony_ci * card detect signal and use this pin for another purpose. If the card 41562306a36Sopenharmony_ci * detect pin is not muxed to SDHCI controller, a default value is 41662306a36Sopenharmony_ci * used. This value can be different from a SoC revision to another 41762306a36Sopenharmony_ci * one. Problems come when this default value is not card present. To 41862306a36Sopenharmony_ci * avoid this case, if the device is non removable then the card 41962306a36Sopenharmony_ci * detection procedure using the SDMCC_CD signal is bypassed. 42062306a36Sopenharmony_ci * This bit is reset when a software reset for all command is performed 42162306a36Sopenharmony_ci * so we need to implement our own reset function to set back this bit. 42262306a36Sopenharmony_ci * 42362306a36Sopenharmony_ci * WA: SAMA5D2 doesn't drive CMD if using CD GPIO line. 42462306a36Sopenharmony_ci */ 42562306a36Sopenharmony_ci if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) 42662306a36Sopenharmony_ci || mmc_gpio_get_cd(host->mmc) >= 0) 42762306a36Sopenharmony_ci sdhci_at91_set_force_card_detect(host); 42862306a36Sopenharmony_ci 42962306a36Sopenharmony_ci pm_runtime_put_autosuspend(&pdev->dev); 43062306a36Sopenharmony_ci 43162306a36Sopenharmony_ci return 0; 43262306a36Sopenharmony_ci 43362306a36Sopenharmony_cipm_runtime_disable: 43462306a36Sopenharmony_ci pm_runtime_disable(&pdev->dev); 43562306a36Sopenharmony_ci pm_runtime_set_suspended(&pdev->dev); 43662306a36Sopenharmony_ci pm_runtime_put_noidle(&pdev->dev); 43762306a36Sopenharmony_ciclocks_disable_unprepare: 43862306a36Sopenharmony_ci clk_disable_unprepare(priv->gck); 43962306a36Sopenharmony_ci clk_disable_unprepare(priv->mainck); 44062306a36Sopenharmony_ci clk_disable_unprepare(priv->hclock); 44162306a36Sopenharmony_cisdhci_pltfm_free: 44262306a36Sopenharmony_ci sdhci_pltfm_free(pdev); 44362306a36Sopenharmony_ci return ret; 44462306a36Sopenharmony_ci} 44562306a36Sopenharmony_ci 44662306a36Sopenharmony_cistatic void sdhci_at91_remove(struct platform_device *pdev) 44762306a36Sopenharmony_ci{ 44862306a36Sopenharmony_ci struct sdhci_host *host = platform_get_drvdata(pdev); 44962306a36Sopenharmony_ci struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 45062306a36Sopenharmony_ci struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); 45162306a36Sopenharmony_ci struct clk *gck = priv->gck; 45262306a36Sopenharmony_ci struct clk *hclock = priv->hclock; 45362306a36Sopenharmony_ci struct clk *mainck = priv->mainck; 45462306a36Sopenharmony_ci 45562306a36Sopenharmony_ci pm_runtime_get_sync(&pdev->dev); 45662306a36Sopenharmony_ci pm_runtime_disable(&pdev->dev); 45762306a36Sopenharmony_ci pm_runtime_put_noidle(&pdev->dev); 45862306a36Sopenharmony_ci 45962306a36Sopenharmony_ci sdhci_pltfm_remove(pdev); 46062306a36Sopenharmony_ci 46162306a36Sopenharmony_ci clk_disable_unprepare(gck); 46262306a36Sopenharmony_ci clk_disable_unprepare(hclock); 46362306a36Sopenharmony_ci clk_disable_unprepare(mainck); 46462306a36Sopenharmony_ci} 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_cistatic struct platform_driver sdhci_at91_driver = { 46762306a36Sopenharmony_ci .driver = { 46862306a36Sopenharmony_ci .name = "sdhci-at91", 46962306a36Sopenharmony_ci .probe_type = PROBE_PREFER_ASYNCHRONOUS, 47062306a36Sopenharmony_ci .of_match_table = sdhci_at91_dt_match, 47162306a36Sopenharmony_ci .pm = &sdhci_at91_dev_pm_ops, 47262306a36Sopenharmony_ci }, 47362306a36Sopenharmony_ci .probe = sdhci_at91_probe, 47462306a36Sopenharmony_ci .remove_new = sdhci_at91_remove, 47562306a36Sopenharmony_ci}; 47662306a36Sopenharmony_ci 47762306a36Sopenharmony_cimodule_platform_driver(sdhci_at91_driver); 47862306a36Sopenharmony_ci 47962306a36Sopenharmony_ciMODULE_DESCRIPTION("SDHCI driver for at91"); 48062306a36Sopenharmony_ciMODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>"); 48162306a36Sopenharmony_ciMODULE_LICENSE("GPL v2"); 482