2019-05-29 22:17:56 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-07-29 22:22:47 +08:00
|
|
|
/*
|
|
|
|
* Atmel SDMMC controller driver.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Atmel,
|
|
|
|
* 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
|
|
|
|
*/
|
|
|
|
|
2020-04-08 15:21:05 +08:00
|
|
|
#include <linux/bitfield.h>
|
2015-07-29 22:22:47 +08:00
|
|
|
#include <linux/clk.h>
|
2016-04-07 17:13:09 +08:00
|
|
|
#include <linux/delay.h>
|
2015-07-29 22:22:47 +08:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
2021-09-24 16:28:50 +08:00
|
|
|
#include <linux/iopoll.h>
|
2016-04-28 20:59:26 +08:00
|
|
|
#include <linux/kernel.h>
|
2015-07-29 22:22:47 +08:00
|
|
|
#include <linux/mmc/host.h>
|
2016-03-17 21:54:34 +08:00
|
|
|
#include <linux/mmc/slot-gpio.h>
|
2015-07-29 22:22:47 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
2015-11-12 02:11:48 +08:00
|
|
|
#include <linux/pm.h>
|
|
|
|
#include <linux/pm_runtime.h>
|
2015-07-29 22:22:47 +08:00
|
|
|
|
|
|
|
#include "sdhci-pltfm.h"
|
|
|
|
|
2017-03-28 17:00:45 +08:00
|
|
|
#define SDMMC_MC1R 0x204
|
|
|
|
#define SDMMC_MC1R_DDR BIT(3)
|
2017-07-26 22:02:46 +08:00
|
|
|
#define SDMMC_MC1R_FCD BIT(7)
|
2015-07-29 22:22:47 +08:00
|
|
|
#define SDMMC_CACR 0x230
|
|
|
|
#define SDMMC_CACR_CAPWREN BIT(0)
|
|
|
|
#define SDMMC_CACR_KEY (0x46 << 8)
|
2019-10-08 20:34:32 +08:00
|
|
|
#define SDMMC_CALCR 0x240
|
|
|
|
#define SDMMC_CALCR_EN BIT(0)
|
|
|
|
#define SDMMC_CALCR_ALWYSON BIT(4)
|
2015-07-29 22:22:47 +08:00
|
|
|
|
2016-04-28 20:59:26 +08:00
|
|
|
#define SDHCI_AT91_PRESET_COMMON_CONF 0x400 /* drv type B, programmable clock mode */
|
|
|
|
|
2019-11-28 15:45:21 +08:00
|
|
|
struct sdhci_at91_soc_data {
|
|
|
|
const struct sdhci_pltfm_data *pdata;
|
|
|
|
bool baseclk_is_generated_internally;
|
|
|
|
unsigned int divider_for_baseclk;
|
|
|
|
};
|
|
|
|
|
2015-07-29 22:22:47 +08:00
|
|
|
struct sdhci_at91_priv {
|
2019-11-28 15:45:21 +08:00
|
|
|
const struct sdhci_at91_soc_data *soc_data;
|
2015-07-29 22:22:47 +08:00
|
|
|
struct clk *hclock;
|
|
|
|
struct clk *gck;
|
|
|
|
struct clk *mainck;
|
2017-07-13 16:04:18 +08:00
|
|
|
bool restore_needed;
|
2019-10-08 20:34:32 +08:00
|
|
|
bool cal_always_on;
|
2015-07-29 22:22:47 +08:00
|
|
|
};
|
|
|
|
|
2017-07-26 22:02:46 +08:00
|
|
|
static void sdhci_at91_set_force_card_detect(struct sdhci_host *host)
|
|
|
|
{
|
|
|
|
u8 mc1r;
|
|
|
|
|
|
|
|
mc1r = readb(host->ioaddr + SDMMC_MC1R);
|
|
|
|
mc1r |= SDMMC_MC1R_FCD;
|
|
|
|
writeb(mc1r, host->ioaddr + SDMMC_MC1R);
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:13:09 +08:00
|
|
|
static void sdhci_at91_set_clock(struct sdhci_host *host, unsigned int clock)
|
|
|
|
{
|
|
|
|
u16 clk;
|
|
|
|
|
|
|
|
host->mmc->actual_clock = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is no requirement to disable the internal clock before
|
|
|
|
* changing the SD clock configuration. Moreover, disabling the
|
|
|
|
* internal clock, changing the configuration and re-enabling the
|
|
|
|
* internal clock causes some bugs. It can prevent to get the internal
|
|
|
|
* clock stable flag ready and an unexpected switch to the base clock
|
|
|
|
* when using presets.
|
|
|
|
*/
|
|
|
|
clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
|
|
|
|
clk &= SDHCI_CLOCK_INT_EN;
|
|
|
|
sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
|
|
|
|
|
|
|
|
if (clock == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
|
|
|
|
|
|
|
|
clk |= SDHCI_CLOCK_INT_EN;
|
|
|
|
sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
|
|
|
|
|
|
|
|
/* Wait max 20 ms */
|
2021-09-24 16:28:51 +08:00
|
|
|
if (read_poll_timeout(sdhci_readw, clk, (clk & SDHCI_CLOCK_INT_STABLE),
|
|
|
|
1000, 20000, false, host, SDHCI_CLOCK_CONTROL)) {
|
|
|
|
pr_err("%s: Internal clock never stabilised.\n",
|
|
|
|
mmc_hostname(host->mmc));
|
|
|
|
return;
|
2016-04-07 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
clk |= SDHCI_CLOCK_CARD_EN;
|
|
|
|
sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
|
|
|
|
}
|
|
|
|
|
2017-10-03 18:06:36 +08:00
|
|
|
static void sdhci_at91_set_uhs_signaling(struct sdhci_host *host,
|
|
|
|
unsigned int timing)
|
2017-03-28 17:00:45 +08:00
|
|
|
{
|
2022-06-30 17:09:26 +08:00
|
|
|
u8 mc1r;
|
|
|
|
|
|
|
|
if (timing == MMC_TIMING_MMC_DDR52) {
|
|
|
|
mc1r = sdhci_readb(host, SDMMC_MC1R);
|
|
|
|
mc1r |= SDMMC_MC1R_DDR;
|
|
|
|
sdhci_writeb(host, mc1r, SDMMC_MC1R);
|
|
|
|
}
|
2017-03-28 17:00:45 +08:00
|
|
|
sdhci_set_uhs_signaling(host, timing);
|
|
|
|
}
|
|
|
|
|
2017-07-26 22:02:46 +08:00
|
|
|
static void sdhci_at91_reset(struct sdhci_host *host, u8 mask)
|
|
|
|
{
|
2019-10-08 20:34:32 +08:00
|
|
|
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
|
|
|
|
struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
|
2021-09-24 16:28:50 +08:00
|
|
|
unsigned int tmp;
|
2019-10-08 20:34:32 +08:00
|
|
|
|
2017-07-26 22:02:46 +08:00
|
|
|
sdhci_reset(host, mask);
|
|
|
|
|
2020-03-16 00:44:25 +08:00
|
|
|
if ((host->mmc->caps & MMC_CAP_NONREMOVABLE)
|
|
|
|
|| mmc_gpio_get_cd(host->mmc) >= 0)
|
2017-07-26 22:02:46 +08:00
|
|
|
sdhci_at91_set_force_card_detect(host);
|
2019-10-08 20:34:32 +08:00
|
|
|
|
2020-05-27 18:56:59 +08:00
|
|
|
if (priv->cal_always_on && (mask & SDHCI_RESET_ALL)) {
|
|
|
|
u32 calcr = sdhci_readl(host, SDMMC_CALCR);
|
|
|
|
|
|
|
|
sdhci_writel(host, calcr | SDMMC_CALCR_ALWYSON | SDMMC_CALCR_EN,
|
2019-10-08 20:34:32 +08:00
|
|
|
SDMMC_CALCR);
|
2021-09-24 16:28:50 +08:00
|
|
|
|
|
|
|
if (read_poll_timeout(sdhci_readl, tmp, !(tmp & SDMMC_CALCR_EN),
|
|
|
|
10, 20000, false, host, SDMMC_CALCR))
|
|
|
|
dev_err(mmc_dev(host->mmc), "Failed to calibrate\n");
|
2020-05-27 18:56:59 +08:00
|
|
|
}
|
2017-07-26 22:02:46 +08:00
|
|
|
}
|
|
|
|
|
2015-07-29 22:22:47 +08:00
|
|
|
static const struct sdhci_ops sdhci_at91_sama5d2_ops = {
|
2016-04-07 17:13:09 +08:00
|
|
|
.set_clock = sdhci_at91_set_clock,
|
2015-07-29 22:22:47 +08:00
|
|
|
.set_bus_width = sdhci_set_bus_width,
|
2017-07-26 22:02:46 +08:00
|
|
|
.reset = sdhci_at91_reset,
|
2017-03-28 17:00:45 +08:00
|
|
|
.set_uhs_signaling = sdhci_at91_set_uhs_signaling,
|
2020-03-07 01:44:06 +08:00
|
|
|
.set_power = sdhci_set_power_and_bus_voltage,
|
2015-07-29 22:22:47 +08:00
|
|
|
};
|
|
|
|
|
2019-11-28 15:45:21 +08:00
|
|
|
static const struct sdhci_pltfm_data sdhci_sama5d2_pdata = {
|
2015-07-29 22:22:47 +08:00
|
|
|
.ops = &sdhci_at91_sama5d2_ops,
|
|
|
|
};
|
|
|
|
|
2019-11-28 15:45:21 +08:00
|
|
|
static const struct sdhci_at91_soc_data soc_data_sama5d2 = {
|
|
|
|
.pdata = &sdhci_sama5d2_pdata,
|
|
|
|
.baseclk_is_generated_internally = false,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct sdhci_at91_soc_data soc_data_sam9x60 = {
|
|
|
|
.pdata = &sdhci_sama5d2_pdata,
|
|
|
|
.baseclk_is_generated_internally = true,
|
|
|
|
.divider_for_baseclk = 2,
|
|
|
|
};
|
|
|
|
|
2015-07-29 22:22:47 +08:00
|
|
|
static const struct of_device_id sdhci_at91_dt_match[] = {
|
|
|
|
{ .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 },
|
2019-11-28 15:45:21 +08:00
|
|
|
{ .compatible = "microchip,sam9x60-sdhci", .data = &soc_data_sam9x60 },
|
2015-07-29 22:22:47 +08:00
|
|
|
{}
|
|
|
|
};
|
2016-10-18 00:13:45 +08:00
|
|
|
MODULE_DEVICE_TABLE(of, sdhci_at91_dt_match);
|
2015-07-29 22:22:47 +08:00
|
|
|
|
2017-07-13 16:04:17 +08:00
|
|
|
static int sdhci_at91_set_clks_presets(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sdhci_host *host = dev_get_drvdata(dev);
|
|
|
|
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
|
|
|
|
struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
|
|
|
|
unsigned int caps0, caps1;
|
|
|
|
unsigned int clk_base, clk_mul;
|
2019-11-28 15:45:21 +08:00
|
|
|
unsigned int gck_rate, clk_base_rate;
|
2017-07-13 16:04:17 +08:00
|
|
|
unsigned int preset_div;
|
|
|
|
|
|
|
|
clk_prepare_enable(priv->hclock);
|
|
|
|
caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES);
|
|
|
|
caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1);
|
2019-11-28 15:45:21 +08:00
|
|
|
|
|
|
|
gck_rate = clk_get_rate(priv->gck);
|
|
|
|
if (priv->soc_data->baseclk_is_generated_internally)
|
|
|
|
clk_base_rate = gck_rate / priv->soc_data->divider_for_baseclk;
|
|
|
|
else
|
|
|
|
clk_base_rate = clk_get_rate(priv->mainck);
|
|
|
|
|
|
|
|
clk_base = clk_base_rate / 1000000;
|
|
|
|
clk_mul = gck_rate / clk_base_rate - 1;
|
|
|
|
|
|
|
|
caps0 &= ~SDHCI_CLOCK_V3_BASE_MASK;
|
2020-04-08 15:21:05 +08:00
|
|
|
caps0 |= FIELD_PREP(SDHCI_CLOCK_V3_BASE_MASK, clk_base);
|
2019-11-28 15:45:21 +08:00
|
|
|
caps1 &= ~SDHCI_CLOCK_MUL_MASK;
|
2020-04-08 15:21:05 +08:00
|
|
|
caps1 |= FIELD_PREP(SDHCI_CLOCK_MUL_MASK, clk_mul);
|
2019-11-28 15:45:21 +08:00
|
|
|
/* Set capabilities in r/w mode. */
|
|
|
|
writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR);
|
|
|
|
writel(caps0, host->ioaddr + SDHCI_CAPABILITIES);
|
|
|
|
writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1);
|
|
|
|
/* Set capabilities in ro mode. */
|
|
|
|
writel(0, host->ioaddr + SDMMC_CACR);
|
|
|
|
|
2020-03-12 22:29:24 +08:00
|
|
|
dev_dbg(dev, "update clk mul to %u as gck rate is %u Hz and clk base is %u Hz\n",
|
|
|
|
clk_mul, gck_rate, clk_base_rate);
|
2017-07-13 16:04:17 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We have to set preset values because it depends on the clk_mul
|
|
|
|
* value. Moreover, SDR104 is supported in a degraded mode since the
|
|
|
|
* maximum sd clock value is 120 MHz instead of 208 MHz. For that
|
|
|
|
* reason, we need to use presets to support SDR104.
|
|
|
|
*/
|
2019-11-28 15:45:21 +08:00
|
|
|
preset_div = DIV_ROUND_UP(gck_rate, 24000000) - 1;
|
2017-07-13 16:04:17 +08:00
|
|
|
writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
|
|
|
|
host->ioaddr + SDHCI_PRESET_FOR_SDR12);
|
2019-11-28 15:45:21 +08:00
|
|
|
preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1;
|
2017-07-13 16:04:17 +08:00
|
|
|
writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
|
|
|
|
host->ioaddr + SDHCI_PRESET_FOR_SDR25);
|
2019-11-28 15:45:21 +08:00
|
|
|
preset_div = DIV_ROUND_UP(gck_rate, 100000000) - 1;
|
2017-07-13 16:04:17 +08:00
|
|
|
writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
|
|
|
|
host->ioaddr + SDHCI_PRESET_FOR_SDR50);
|
2019-11-28 15:45:21 +08:00
|
|
|
preset_div = DIV_ROUND_UP(gck_rate, 120000000) - 1;
|
2017-07-13 16:04:17 +08:00
|
|
|
writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
|
|
|
|
host->ioaddr + SDHCI_PRESET_FOR_SDR104);
|
2019-11-28 15:45:21 +08:00
|
|
|
preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1;
|
2017-07-13 16:04:17 +08:00
|
|
|
writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
|
|
|
|
host->ioaddr + SDHCI_PRESET_FOR_DDR50);
|
|
|
|
|
|
|
|
clk_prepare_enable(priv->mainck);
|
|
|
|
clk_prepare_enable(priv->gck);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-13 16:04:18 +08:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
|
|
|
static int sdhci_at91_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sdhci_host *host = dev_get_drvdata(dev);
|
|
|
|
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
|
|
|
|
struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pm_runtime_force_suspend(dev);
|
|
|
|
|
|
|
|
priv->restore_needed = true;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_PM_SLEEP */
|
|
|
|
|
2015-11-12 02:11:48 +08:00
|
|
|
#ifdef CONFIG_PM
|
|
|
|
static int sdhci_at91_runtime_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sdhci_host *host = dev_get_drvdata(dev);
|
|
|
|
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
|
2016-02-16 21:08:25 +08:00
|
|
|
struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
|
2015-11-12 02:11:48 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sdhci_runtime_suspend_host(host);
|
|
|
|
|
2017-03-21 01:50:32 +08:00
|
|
|
if (host->tuning_mode != SDHCI_TUNING_MODE_3)
|
|
|
|
mmc_retune_needed(host->mmc);
|
|
|
|
|
2015-11-12 02:11:48 +08:00
|
|
|
clk_disable_unprepare(priv->gck);
|
|
|
|
clk_disable_unprepare(priv->hclock);
|
|
|
|
clk_disable_unprepare(priv->mainck);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sdhci_at91_runtime_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sdhci_host *host = dev_get_drvdata(dev);
|
|
|
|
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
|
2016-02-16 21:08:25 +08:00
|
|
|
struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
|
2015-11-12 02:11:48 +08:00
|
|
|
int ret;
|
|
|
|
|
2017-07-13 16:04:18 +08:00
|
|
|
if (priv->restore_needed) {
|
|
|
|
ret = sdhci_at91_set_clks_presets(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
priv->restore_needed = false;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-11-12 02:11:48 +08:00
|
|
|
ret = clk_prepare_enable(priv->mainck);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "can't enable mainck\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(priv->hclock);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "can't enable hclock\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(priv->gck);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "can't enable gck\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-13 16:04:18 +08:00
|
|
|
out:
|
2019-07-25 11:14:22 +08:00
|
|
|
return sdhci_runtime_resume_host(host, 0);
|
2015-11-12 02:11:48 +08:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_PM */
|
|
|
|
|
|
|
|
static const struct dev_pm_ops sdhci_at91_dev_pm_ops = {
|
2017-07-13 16:04:18 +08:00
|
|
|
SET_SYSTEM_SLEEP_PM_OPS(sdhci_at91_suspend, pm_runtime_force_resume)
|
2015-11-12 02:11:48 +08:00
|
|
|
SET_RUNTIME_PM_OPS(sdhci_at91_runtime_suspend,
|
|
|
|
sdhci_at91_runtime_resume,
|
|
|
|
NULL)
|
|
|
|
};
|
|
|
|
|
2015-07-29 22:22:47 +08:00
|
|
|
static int sdhci_at91_probe(struct platform_device *pdev)
|
|
|
|
{
|
2019-11-28 15:45:21 +08:00
|
|
|
const struct sdhci_at91_soc_data *soc_data;
|
2015-07-29 22:22:47 +08:00
|
|
|
struct sdhci_host *host;
|
|
|
|
struct sdhci_pltfm_host *pltfm_host;
|
|
|
|
struct sdhci_at91_priv *priv;
|
|
|
|
int ret;
|
|
|
|
|
2022-02-03 02:06:47 +08:00
|
|
|
soc_data = of_device_get_match_data(&pdev->dev);
|
|
|
|
if (!soc_data)
|
2015-07-29 22:22:47 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-11-28 15:45:21 +08:00
|
|
|
host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*priv));
|
2016-02-16 21:08:25 +08:00
|
|
|
if (IS_ERR(host))
|
|
|
|
return PTR_ERR(host);
|
|
|
|
|
|
|
|
pltfm_host = sdhci_priv(host);
|
|
|
|
priv = sdhci_pltfm_priv(pltfm_host);
|
2019-11-28 15:45:21 +08:00
|
|
|
priv->soc_data = soc_data;
|
2015-07-29 22:22:47 +08:00
|
|
|
|
|
|
|
priv->mainck = devm_clk_get(&pdev->dev, "baseclk");
|
|
|
|
if (IS_ERR(priv->mainck)) {
|
2019-11-28 15:45:21 +08:00
|
|
|
if (soc_data->baseclk_is_generated_internally) {
|
|
|
|
priv->mainck = NULL;
|
|
|
|
} else {
|
|
|
|
dev_err(&pdev->dev, "failed to get baseclk\n");
|
2020-01-02 18:42:16 +08:00
|
|
|
ret = PTR_ERR(priv->mainck);
|
|
|
|
goto sdhci_pltfm_free;
|
2019-11-28 15:45:21 +08:00
|
|
|
}
|
2015-07-29 22:22:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
priv->hclock = devm_clk_get(&pdev->dev, "hclock");
|
|
|
|
if (IS_ERR(priv->hclock)) {
|
|
|
|
dev_err(&pdev->dev, "failed to get hclock\n");
|
2020-01-02 18:42:16 +08:00
|
|
|
ret = PTR_ERR(priv->hclock);
|
|
|
|
goto sdhci_pltfm_free;
|
2015-07-29 22:22:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
priv->gck = devm_clk_get(&pdev->dev, "multclk");
|
|
|
|
if (IS_ERR(priv->gck)) {
|
|
|
|
dev_err(&pdev->dev, "failed to get multclk\n");
|
2020-01-02 18:42:16 +08:00
|
|
|
ret = PTR_ERR(priv->gck);
|
|
|
|
goto sdhci_pltfm_free;
|
2015-07-29 22:22:47 +08:00
|
|
|
}
|
|
|
|
|
2017-07-13 16:04:17 +08:00
|
|
|
ret = sdhci_at91_set_clks_presets(&pdev->dev);
|
|
|
|
if (ret)
|
|
|
|
goto sdhci_pltfm_free;
|
2015-07-29 22:22:47 +08:00
|
|
|
|
2017-07-13 16:04:18 +08:00
|
|
|
priv->restore_needed = false;
|
|
|
|
|
2019-10-08 20:34:32 +08:00
|
|
|
/*
|
|
|
|
* if SDCAL pin is wrongly connected, we must enable
|
|
|
|
* the analog calibration cell permanently.
|
|
|
|
*/
|
|
|
|
priv->cal_always_on =
|
|
|
|
device_property_read_bool(&pdev->dev,
|
|
|
|
"microchip,sdcal-inverted");
|
|
|
|
|
2015-07-29 22:22:47 +08:00
|
|
|
ret = mmc_of_parse(host->mmc);
|
|
|
|
if (ret)
|
|
|
|
goto clocks_disable_unprepare;
|
|
|
|
|
|
|
|
sdhci_get_of_property(pdev);
|
|
|
|
|
2015-11-12 02:11:48 +08:00
|
|
|
pm_runtime_get_noresume(&pdev->dev);
|
|
|
|
pm_runtime_set_active(&pdev->dev);
|
|
|
|
pm_runtime_enable(&pdev->dev);
|
|
|
|
pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
|
|
|
|
pm_runtime_use_autosuspend(&pdev->dev);
|
|
|
|
|
2019-08-08 16:35:40 +08:00
|
|
|
/* HS200 is broken at this moment */
|
2019-11-14 20:59:26 +08:00
|
|
|
host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
|
2019-08-08 16:35:40 +08:00
|
|
|
|
2015-07-29 22:22:47 +08:00
|
|
|
ret = sdhci_add_host(host);
|
|
|
|
if (ret)
|
2015-11-12 02:11:48 +08:00
|
|
|
goto pm_runtime_disable;
|
|
|
|
|
2016-03-17 21:54:34 +08:00
|
|
|
/*
|
|
|
|
* When calling sdhci_runtime_suspend_host(), the sdhci layer makes
|
|
|
|
* the assumption that all the clocks of the controller are disabled.
|
|
|
|
* It means we can't get irq from it when it is runtime suspended.
|
|
|
|
* For that reason, it is not planned to wake-up on a card detect irq
|
|
|
|
* from the controller.
|
|
|
|
* If we want to use runtime PM and to be able to wake-up on card
|
|
|
|
* insertion, we have to use a GPIO for the card detection or we can
|
|
|
|
* use polling. Be aware that using polling will resume/suspend the
|
|
|
|
* controller between each attempt.
|
|
|
|
* Disable SDHCI_QUIRK_BROKEN_CARD_DETECTION to be sure nobody tries
|
|
|
|
* to enable polling via device tree with broken-cd property.
|
|
|
|
*/
|
2016-06-21 09:13:26 +08:00
|
|
|
if (mmc_card_is_removable(host->mmc) &&
|
remove lots of IS_ERR_VALUE abuses
Most users of IS_ERR_VALUE() in the kernel are wrong, as they
pass an 'int' into a function that takes an 'unsigned long'
argument. This happens to work because the type is sign-extended
on 64-bit architectures before it gets converted into an
unsigned type.
However, anything that passes an 'unsigned short' or 'unsigned int'
argument into IS_ERR_VALUE() is guaranteed to be broken, as are
8-bit integers and types that are wider than 'unsigned long'.
Andrzej Hajda has already fixed a lot of the worst abusers that
were causing actual bugs, but it would be nice to prevent any
users that are not passing 'unsigned long' arguments.
This patch changes all users of IS_ERR_VALUE() that I could find
on 32-bit ARM randconfig builds and x86 allmodconfig. For the
moment, this doesn't change the definition of IS_ERR_VALUE()
because there are probably still architecture specific users
elsewhere.
Almost all the warnings I got are for files that are better off
using 'if (err)' or 'if (err < 0)'.
The only legitimate user I could find that we get a warning for
is the (32-bit only) freescale fman driver, so I did not remove
the IS_ERR_VALUE() there but changed the type to 'unsigned long'.
For 9pfs, I just worked around one user whose calling conventions
are so obscure that I did not dare change the behavior.
I was using this definition for testing:
#define IS_ERR_VALUE(x) ((unsigned long*)NULL == (typeof (x)*)NULL && \
unlikely((unsigned long long)(x) >= (unsigned long long)(typeof(x))-MAX_ERRNO))
which ends up making all 16-bit or wider types work correctly with
the most plausible interpretation of what IS_ERR_VALUE() was supposed
to return according to its users, but also causes a compile-time
warning for any users that do not pass an 'unsigned long' argument.
I suggested this approach earlier this year, but back then we ended
up deciding to just fix the users that are obviously broken. After
the initial warning that caused me to get involved in the discussion
(fs/gfs2/dir.c) showed up again in the mainline kernel, Linus
asked me to send the whole thing again.
[ Updated the 9p parts as per Al Viro - Linus ]
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Andrzej Hajda <a.hajda@samsung.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Link: https://lkml.org/lkml/2016/1/7/363
Link: https://lkml.org/lkml/2016/5/27/486
Acked-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org> # For nvmem part
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-05-28 05:23:25 +08:00
|
|
|
mmc_gpio_get_cd(host->mmc) < 0) {
|
2016-03-17 21:54:34 +08:00
|
|
|
host->mmc->caps |= MMC_CAP_NEEDS_POLL;
|
|
|
|
host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
|
|
|
|
}
|
|
|
|
|
2017-07-26 22:02:46 +08:00
|
|
|
/*
|
|
|
|
* If the device attached to the MMC bus is not removable, it is safer
|
|
|
|
* to set the Force Card Detect bit. People often don't connect the
|
|
|
|
* card detect signal and use this pin for another purpose. If the card
|
|
|
|
* detect pin is not muxed to SDHCI controller, a default value is
|
|
|
|
* used. This value can be different from a SoC revision to another
|
|
|
|
* one. Problems come when this default value is not card present. To
|
|
|
|
* avoid this case, if the device is non removable then the card
|
|
|
|
* detection procedure using the SDMCC_CD signal is bypassed.
|
|
|
|
* This bit is reset when a software reset for all command is performed
|
|
|
|
* so we need to implement our own reset function to set back this bit.
|
2020-03-16 00:44:25 +08:00
|
|
|
*
|
|
|
|
* WA: SAMA5D2 doesn't drive CMD if using CD GPIO line.
|
2017-07-26 22:02:46 +08:00
|
|
|
*/
|
2020-03-16 00:44:25 +08:00
|
|
|
if ((host->mmc->caps & MMC_CAP_NONREMOVABLE)
|
|
|
|
|| mmc_gpio_get_cd(host->mmc) >= 0)
|
2017-07-26 22:02:46 +08:00
|
|
|
sdhci_at91_set_force_card_detect(host);
|
|
|
|
|
2015-11-12 02:11:48 +08:00
|
|
|
pm_runtime_put_autosuspend(&pdev->dev);
|
2015-07-29 22:22:47 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2015-11-12 02:11:48 +08:00
|
|
|
pm_runtime_disable:
|
|
|
|
pm_runtime_disable(&pdev->dev);
|
|
|
|
pm_runtime_set_suspended(&pdev->dev);
|
2016-02-02 19:55:06 +08:00
|
|
|
pm_runtime_put_noidle(&pdev->dev);
|
2015-07-29 22:22:47 +08:00
|
|
|
clocks_disable_unprepare:
|
|
|
|
clk_disable_unprepare(priv->gck);
|
|
|
|
clk_disable_unprepare(priv->mainck);
|
|
|
|
clk_disable_unprepare(priv->hclock);
|
2017-07-13 16:04:17 +08:00
|
|
|
sdhci_pltfm_free:
|
2015-07-29 22:22:47 +08:00
|
|
|
sdhci_pltfm_free(pdev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sdhci_at91_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct sdhci_host *host = platform_get_drvdata(pdev);
|
|
|
|
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
|
2016-02-16 21:08:25 +08:00
|
|
|
struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
|
|
|
|
struct clk *gck = priv->gck;
|
|
|
|
struct clk *hclock = priv->hclock;
|
|
|
|
struct clk *mainck = priv->mainck;
|
2015-07-29 22:22:47 +08:00
|
|
|
|
2015-11-12 02:11:48 +08:00
|
|
|
pm_runtime_get_sync(&pdev->dev);
|
|
|
|
pm_runtime_disable(&pdev->dev);
|
|
|
|
pm_runtime_put_noidle(&pdev->dev);
|
|
|
|
|
2015-07-29 22:22:47 +08:00
|
|
|
sdhci_pltfm_unregister(pdev);
|
|
|
|
|
2016-02-16 21:08:25 +08:00
|
|
|
clk_disable_unprepare(gck);
|
|
|
|
clk_disable_unprepare(hclock);
|
|
|
|
clk_disable_unprepare(mainck);
|
2015-07-29 22:22:47 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver sdhci_at91_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "sdhci-at91",
|
2020-09-04 07:24:36 +08:00
|
|
|
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
2015-07-29 22:22:47 +08:00
|
|
|
.of_match_table = sdhci_at91_dt_match,
|
2015-11-12 02:11:48 +08:00
|
|
|
.pm = &sdhci_at91_dev_pm_ops,
|
2015-07-29 22:22:47 +08:00
|
|
|
},
|
|
|
|
.probe = sdhci_at91_probe,
|
|
|
|
.remove = sdhci_at91_remove,
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(sdhci_at91_driver);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("SDHCI driver for at91");
|
|
|
|
MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
|
|
|
|
MODULE_LICENSE("GPL v2");
|