Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Atmel SDMMC controller driver. |
| 4 | * |
| 5 | * Copyright (C) 2015 Atmel, |
| 6 | * 2015 Ludovic Desroches <ludovic.desroches@atmel.com> |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/clk.h> |
Ludovic Desroches | 4e289a7 | 2016-04-07 11:13:09 +0200 | [diff] [blame] | 10 | #include <linux/delay.h> |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 11 | #include <linux/err.h> |
| 12 | #include <linux/io.h> |
Ludovic Desroches | 4406433 | 2016-04-28 14:59:26 +0200 | [diff] [blame] | 13 | #include <linux/kernel.h> |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 14 | #include <linux/mmc/host.h> |
ludovic.desroches@atmel.com | 64e5cd7 | 2016-03-17 14:54:34 +0100 | [diff] [blame] | 15 | #include <linux/mmc/slot-gpio.h> |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/of_device.h> |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 19 | #include <linux/pm.h> |
| 20 | #include <linux/pm_runtime.h> |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 21 | |
| 22 | #include "sdhci-pltfm.h" |
| 23 | |
Ludovic Desroches | d091876 | 2017-03-28 11:00:45 +0200 | [diff] [blame] | 24 | #define SDMMC_MC1R 0x204 |
| 25 | #define SDMMC_MC1R_DDR BIT(3) |
Ludovic Desroches | 7a1e3f1 | 2017-07-26 16:02:46 +0200 | [diff] [blame] | 26 | #define SDMMC_MC1R_FCD BIT(7) |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 27 | #define SDMMC_CACR 0x230 |
| 28 | #define SDMMC_CACR_CAPWREN BIT(0) |
| 29 | #define SDMMC_CACR_KEY (0x46 << 8) |
Nicolas Ferre | 727d836 | 2019-10-08 14:34:32 +0200 | [diff] [blame] | 30 | #define SDMMC_CALCR 0x240 |
| 31 | #define SDMMC_CALCR_EN BIT(0) |
| 32 | #define SDMMC_CALCR_ALWYSON BIT(4) |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 33 | |
Ludovic Desroches | 4406433 | 2016-04-28 14:59:26 +0200 | [diff] [blame] | 34 | #define SDHCI_AT91_PRESET_COMMON_CONF 0x400 /* drv type B, programmable clock mode */ |
| 35 | |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 36 | struct sdhci_at91_soc_data { |
| 37 | const struct sdhci_pltfm_data *pdata; |
| 38 | bool baseclk_is_generated_internally; |
| 39 | unsigned int divider_for_baseclk; |
| 40 | }; |
| 41 | |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 42 | struct sdhci_at91_priv { |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 43 | const struct sdhci_at91_soc_data *soc_data; |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 44 | struct clk *hclock; |
| 45 | struct clk *gck; |
| 46 | struct clk *mainck; |
Quentin Schulz | e2b372e | 2017-07-13 10:04:18 +0200 | [diff] [blame] | 47 | bool restore_needed; |
Nicolas Ferre | 727d836 | 2019-10-08 14:34:32 +0200 | [diff] [blame] | 48 | bool cal_always_on; |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
Ludovic Desroches | 7a1e3f1 | 2017-07-26 16:02:46 +0200 | [diff] [blame] | 51 | static void sdhci_at91_set_force_card_detect(struct sdhci_host *host) |
| 52 | { |
| 53 | u8 mc1r; |
| 54 | |
| 55 | mc1r = readb(host->ioaddr + SDMMC_MC1R); |
| 56 | mc1r |= SDMMC_MC1R_FCD; |
| 57 | writeb(mc1r, host->ioaddr + SDMMC_MC1R); |
| 58 | } |
| 59 | |
Ludovic Desroches | 4e289a7 | 2016-04-07 11:13:09 +0200 | [diff] [blame] | 60 | static void sdhci_at91_set_clock(struct sdhci_host *host, unsigned int clock) |
| 61 | { |
| 62 | u16 clk; |
| 63 | unsigned long timeout; |
| 64 | |
| 65 | host->mmc->actual_clock = 0; |
| 66 | |
| 67 | /* |
| 68 | * There is no requirement to disable the internal clock before |
| 69 | * changing the SD clock configuration. Moreover, disabling the |
| 70 | * internal clock, changing the configuration and re-enabling the |
| 71 | * internal clock causes some bugs. It can prevent to get the internal |
| 72 | * clock stable flag ready and an unexpected switch to the base clock |
| 73 | * when using presets. |
| 74 | */ |
| 75 | clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 76 | clk &= SDHCI_CLOCK_INT_EN; |
| 77 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 78 | |
| 79 | if (clock == 0) |
| 80 | return; |
| 81 | |
| 82 | clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock); |
| 83 | |
| 84 | clk |= SDHCI_CLOCK_INT_EN; |
| 85 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 86 | |
| 87 | /* Wait max 20 ms */ |
| 88 | timeout = 20; |
| 89 | while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL)) |
| 90 | & SDHCI_CLOCK_INT_STABLE)) { |
| 91 | if (timeout == 0) { |
| 92 | pr_err("%s: Internal clock never stabilised.\n", |
| 93 | mmc_hostname(host->mmc)); |
| 94 | return; |
| 95 | } |
| 96 | timeout--; |
| 97 | mdelay(1); |
| 98 | } |
| 99 | |
| 100 | clk |= SDHCI_CLOCK_CARD_EN; |
| 101 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 102 | } |
| 103 | |
Colin Ian King | 519c51a | 2017-10-03 11:06:36 +0100 | [diff] [blame] | 104 | static void sdhci_at91_set_uhs_signaling(struct sdhci_host *host, |
| 105 | unsigned int timing) |
Ludovic Desroches | d091876 | 2017-03-28 11:00:45 +0200 | [diff] [blame] | 106 | { |
| 107 | if (timing == MMC_TIMING_MMC_DDR52) |
| 108 | sdhci_writeb(host, SDMMC_MC1R_DDR, SDMMC_MC1R); |
| 109 | sdhci_set_uhs_signaling(host, timing); |
| 110 | } |
| 111 | |
Ludovic Desroches | 7a1e3f1 | 2017-07-26 16:02:46 +0200 | [diff] [blame] | 112 | static void sdhci_at91_reset(struct sdhci_host *host, u8 mask) |
| 113 | { |
Nicolas Ferre | 727d836 | 2019-10-08 14:34:32 +0200 | [diff] [blame] | 114 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 115 | struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 116 | |
Ludovic Desroches | 7a1e3f1 | 2017-07-26 16:02:46 +0200 | [diff] [blame] | 117 | sdhci_reset(host, mask); |
| 118 | |
Michał Mirosław | 53dd0a7 | 2020-03-15 17:44:25 +0100 | [diff] [blame] | 119 | if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) |
| 120 | || mmc_gpio_get_cd(host->mmc) >= 0) |
Ludovic Desroches | 7a1e3f1 | 2017-07-26 16:02:46 +0200 | [diff] [blame] | 121 | sdhci_at91_set_force_card_detect(host); |
Nicolas Ferre | 727d836 | 2019-10-08 14:34:32 +0200 | [diff] [blame] | 122 | |
| 123 | if (priv->cal_always_on && (mask & SDHCI_RESET_ALL)) |
| 124 | sdhci_writel(host, SDMMC_CALCR_ALWYSON | SDMMC_CALCR_EN, |
| 125 | SDMMC_CALCR); |
Ludovic Desroches | 7a1e3f1 | 2017-07-26 16:02:46 +0200 | [diff] [blame] | 126 | } |
| 127 | |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 128 | static const struct sdhci_ops sdhci_at91_sama5d2_ops = { |
Ludovic Desroches | 4e289a7 | 2016-04-07 11:13:09 +0200 | [diff] [blame] | 129 | .set_clock = sdhci_at91_set_clock, |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 130 | .set_bus_width = sdhci_set_bus_width, |
Ludovic Desroches | 7a1e3f1 | 2017-07-26 16:02:46 +0200 | [diff] [blame] | 131 | .reset = sdhci_at91_reset, |
Ludovic Desroches | d091876 | 2017-03-28 11:00:45 +0200 | [diff] [blame] | 132 | .set_uhs_signaling = sdhci_at91_set_uhs_signaling, |
Nicolas Saenz Julienne | 9816056 | 2020-03-06 18:44:06 +0100 | [diff] [blame] | 133 | .set_power = sdhci_set_power_and_bus_voltage, |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 134 | }; |
| 135 | |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 136 | static const struct sdhci_pltfm_data sdhci_sama5d2_pdata = { |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 137 | .ops = &sdhci_at91_sama5d2_ops, |
| 138 | }; |
| 139 | |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 140 | static const struct sdhci_at91_soc_data soc_data_sama5d2 = { |
| 141 | .pdata = &sdhci_sama5d2_pdata, |
| 142 | .baseclk_is_generated_internally = false, |
| 143 | }; |
| 144 | |
| 145 | static const struct sdhci_at91_soc_data soc_data_sam9x60 = { |
| 146 | .pdata = &sdhci_sama5d2_pdata, |
| 147 | .baseclk_is_generated_internally = true, |
| 148 | .divider_for_baseclk = 2, |
| 149 | }; |
| 150 | |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 151 | static const struct of_device_id sdhci_at91_dt_match[] = { |
| 152 | { .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 }, |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 153 | { .compatible = "microchip,sam9x60-sdhci", .data = &soc_data_sam9x60 }, |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 154 | {} |
| 155 | }; |
Javier Martinez Canillas | d9943c6 | 2016-10-17 13:13:45 -0300 | [diff] [blame] | 156 | MODULE_DEVICE_TABLE(of, sdhci_at91_dt_match); |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 157 | |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 158 | static int sdhci_at91_set_clks_presets(struct device *dev) |
| 159 | { |
| 160 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 161 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 162 | struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 163 | unsigned int caps0, caps1; |
| 164 | unsigned int clk_base, clk_mul; |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 165 | unsigned int gck_rate, clk_base_rate; |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 166 | unsigned int preset_div; |
| 167 | |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 168 | clk_prepare_enable(priv->hclock); |
| 169 | caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES); |
| 170 | caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1); |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 171 | |
| 172 | gck_rate = clk_get_rate(priv->gck); |
| 173 | if (priv->soc_data->baseclk_is_generated_internally) |
| 174 | clk_base_rate = gck_rate / priv->soc_data->divider_for_baseclk; |
| 175 | else |
| 176 | clk_base_rate = clk_get_rate(priv->mainck); |
| 177 | |
| 178 | clk_base = clk_base_rate / 1000000; |
| 179 | clk_mul = gck_rate / clk_base_rate - 1; |
| 180 | |
| 181 | caps0 &= ~SDHCI_CLOCK_V3_BASE_MASK; |
| 182 | caps0 |= (clk_base << SDHCI_CLOCK_BASE_SHIFT) & SDHCI_CLOCK_V3_BASE_MASK; |
| 183 | caps1 &= ~SDHCI_CLOCK_MUL_MASK; |
| 184 | caps1 |= (clk_mul << SDHCI_CLOCK_MUL_SHIFT) & SDHCI_CLOCK_MUL_MASK; |
| 185 | /* Set capabilities in r/w mode. */ |
| 186 | writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR); |
| 187 | writel(caps0, host->ioaddr + SDHCI_CAPABILITIES); |
| 188 | writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1); |
| 189 | /* Set capabilities in ro mode. */ |
| 190 | writel(0, host->ioaddr + SDMMC_CACR); |
| 191 | |
Cristian Birsan | fdd8eef | 2020-03-12 14:29:24 +0000 | [diff] [blame] | 192 | dev_dbg(dev, "update clk mul to %u as gck rate is %u Hz and clk base is %u Hz\n", |
| 193 | clk_mul, gck_rate, clk_base_rate); |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * We have to set preset values because it depends on the clk_mul |
| 197 | * value. Moreover, SDR104 is supported in a degraded mode since the |
| 198 | * maximum sd clock value is 120 MHz instead of 208 MHz. For that |
| 199 | * reason, we need to use presets to support SDR104. |
| 200 | */ |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 201 | preset_div = DIV_ROUND_UP(gck_rate, 24000000) - 1; |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 202 | writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, |
| 203 | host->ioaddr + SDHCI_PRESET_FOR_SDR12); |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 204 | preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1; |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 205 | writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, |
| 206 | host->ioaddr + SDHCI_PRESET_FOR_SDR25); |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 207 | preset_div = DIV_ROUND_UP(gck_rate, 100000000) - 1; |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 208 | writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, |
| 209 | host->ioaddr + SDHCI_PRESET_FOR_SDR50); |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 210 | preset_div = DIV_ROUND_UP(gck_rate, 120000000) - 1; |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 211 | writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, |
| 212 | host->ioaddr + SDHCI_PRESET_FOR_SDR104); |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 213 | preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1; |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 214 | writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, |
| 215 | host->ioaddr + SDHCI_PRESET_FOR_DDR50); |
| 216 | |
| 217 | clk_prepare_enable(priv->mainck); |
| 218 | clk_prepare_enable(priv->gck); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Quentin Schulz | e2b372e | 2017-07-13 10:04:18 +0200 | [diff] [blame] | 223 | #ifdef CONFIG_PM_SLEEP |
| 224 | static int sdhci_at91_suspend(struct device *dev) |
| 225 | { |
| 226 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 227 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 228 | struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 229 | int ret; |
| 230 | |
| 231 | ret = pm_runtime_force_suspend(dev); |
| 232 | |
| 233 | priv->restore_needed = true; |
| 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | #endif /* CONFIG_PM_SLEEP */ |
| 238 | |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 239 | #ifdef CONFIG_PM |
| 240 | static int sdhci_at91_runtime_suspend(struct device *dev) |
| 241 | { |
| 242 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 243 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 10f1c13 | 2016-02-16 21:08:25 +0800 | [diff] [blame] | 244 | struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 245 | int ret; |
| 246 | |
| 247 | ret = sdhci_runtime_suspend_host(host); |
| 248 | |
Adrian Hunter | d38dcad | 2017-03-20 19:50:32 +0200 | [diff] [blame] | 249 | if (host->tuning_mode != SDHCI_TUNING_MODE_3) |
| 250 | mmc_retune_needed(host->mmc); |
| 251 | |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 252 | clk_disable_unprepare(priv->gck); |
| 253 | clk_disable_unprepare(priv->hclock); |
| 254 | clk_disable_unprepare(priv->mainck); |
| 255 | |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | static int sdhci_at91_runtime_resume(struct device *dev) |
| 260 | { |
| 261 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 262 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 10f1c13 | 2016-02-16 21:08:25 +0800 | [diff] [blame] | 263 | struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 264 | int ret; |
| 265 | |
Quentin Schulz | e2b372e | 2017-07-13 10:04:18 +0200 | [diff] [blame] | 266 | if (priv->restore_needed) { |
| 267 | ret = sdhci_at91_set_clks_presets(dev); |
| 268 | if (ret) |
| 269 | return ret; |
| 270 | |
| 271 | priv->restore_needed = false; |
| 272 | goto out; |
| 273 | } |
| 274 | |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 275 | ret = clk_prepare_enable(priv->mainck); |
| 276 | if (ret) { |
| 277 | dev_err(dev, "can't enable mainck\n"); |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | ret = clk_prepare_enable(priv->hclock); |
| 282 | if (ret) { |
| 283 | dev_err(dev, "can't enable hclock\n"); |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | ret = clk_prepare_enable(priv->gck); |
| 288 | if (ret) { |
| 289 | dev_err(dev, "can't enable gck\n"); |
| 290 | return ret; |
| 291 | } |
| 292 | |
Quentin Schulz | e2b372e | 2017-07-13 10:04:18 +0200 | [diff] [blame] | 293 | out: |
Baolin Wang | c6303c5 | 2019-07-25 11:14:22 +0800 | [diff] [blame] | 294 | return sdhci_runtime_resume_host(host, 0); |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 295 | } |
| 296 | #endif /* CONFIG_PM */ |
| 297 | |
| 298 | static const struct dev_pm_ops sdhci_at91_dev_pm_ops = { |
Quentin Schulz | e2b372e | 2017-07-13 10:04:18 +0200 | [diff] [blame] | 299 | SET_SYSTEM_SLEEP_PM_OPS(sdhci_at91_suspend, pm_runtime_force_resume) |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 300 | SET_RUNTIME_PM_OPS(sdhci_at91_runtime_suspend, |
| 301 | sdhci_at91_runtime_resume, |
| 302 | NULL) |
| 303 | }; |
| 304 | |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 305 | static int sdhci_at91_probe(struct platform_device *pdev) |
| 306 | { |
| 307 | const struct of_device_id *match; |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 308 | const struct sdhci_at91_soc_data *soc_data; |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 309 | struct sdhci_host *host; |
| 310 | struct sdhci_pltfm_host *pltfm_host; |
| 311 | struct sdhci_at91_priv *priv; |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 312 | int ret; |
| 313 | |
| 314 | match = of_match_device(sdhci_at91_dt_match, &pdev->dev); |
| 315 | if (!match) |
| 316 | return -EINVAL; |
| 317 | soc_data = match->data; |
| 318 | |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 319 | host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*priv)); |
Jisheng Zhang | 10f1c13 | 2016-02-16 21:08:25 +0800 | [diff] [blame] | 320 | if (IS_ERR(host)) |
| 321 | return PTR_ERR(host); |
| 322 | |
| 323 | pltfm_host = sdhci_priv(host); |
| 324 | priv = sdhci_pltfm_priv(pltfm_host); |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 325 | priv->soc_data = soc_data; |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 326 | |
| 327 | priv->mainck = devm_clk_get(&pdev->dev, "baseclk"); |
| 328 | if (IS_ERR(priv->mainck)) { |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 329 | if (soc_data->baseclk_is_generated_internally) { |
| 330 | priv->mainck = NULL; |
| 331 | } else { |
| 332 | dev_err(&pdev->dev, "failed to get baseclk\n"); |
Michał Mirosław | a04184c | 2020-01-02 11:42:16 +0100 | [diff] [blame] | 333 | ret = PTR_ERR(priv->mainck); |
| 334 | goto sdhci_pltfm_free; |
Ludovic Desroches | 3976656 | 2019-11-28 08:45:21 +0100 | [diff] [blame] | 335 | } |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | priv->hclock = devm_clk_get(&pdev->dev, "hclock"); |
| 339 | if (IS_ERR(priv->hclock)) { |
| 340 | dev_err(&pdev->dev, "failed to get hclock\n"); |
Michał Mirosław | a04184c | 2020-01-02 11:42:16 +0100 | [diff] [blame] | 341 | ret = PTR_ERR(priv->hclock); |
| 342 | goto sdhci_pltfm_free; |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | priv->gck = devm_clk_get(&pdev->dev, "multclk"); |
| 346 | if (IS_ERR(priv->gck)) { |
| 347 | dev_err(&pdev->dev, "failed to get multclk\n"); |
Michał Mirosław | a04184c | 2020-01-02 11:42:16 +0100 | [diff] [blame] | 348 | ret = PTR_ERR(priv->gck); |
| 349 | goto sdhci_pltfm_free; |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 352 | ret = sdhci_at91_set_clks_presets(&pdev->dev); |
| 353 | if (ret) |
| 354 | goto sdhci_pltfm_free; |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 355 | |
Quentin Schulz | e2b372e | 2017-07-13 10:04:18 +0200 | [diff] [blame] | 356 | priv->restore_needed = false; |
| 357 | |
Nicolas Ferre | 727d836 | 2019-10-08 14:34:32 +0200 | [diff] [blame] | 358 | /* |
| 359 | * if SDCAL pin is wrongly connected, we must enable |
| 360 | * the analog calibration cell permanently. |
| 361 | */ |
| 362 | priv->cal_always_on = |
| 363 | device_property_read_bool(&pdev->dev, |
| 364 | "microchip,sdcal-inverted"); |
| 365 | |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 366 | ret = mmc_of_parse(host->mmc); |
| 367 | if (ret) |
| 368 | goto clocks_disable_unprepare; |
| 369 | |
| 370 | sdhci_get_of_property(pdev); |
| 371 | |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 372 | pm_runtime_get_noresume(&pdev->dev); |
| 373 | pm_runtime_set_active(&pdev->dev); |
| 374 | pm_runtime_enable(&pdev->dev); |
| 375 | pm_runtime_set_autosuspend_delay(&pdev->dev, 50); |
| 376 | pm_runtime_use_autosuspend(&pdev->dev); |
| 377 | |
Eugen Hristev | 7871aa6 | 2019-08-08 08:35:40 +0000 | [diff] [blame] | 378 | /* HS200 is broken at this moment */ |
Eugen Hristev | fed23c5 | 2019-11-14 12:59:26 +0000 | [diff] [blame] | 379 | host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200; |
Eugen Hristev | 7871aa6 | 2019-08-08 08:35:40 +0000 | [diff] [blame] | 380 | |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 381 | ret = sdhci_add_host(host); |
| 382 | if (ret) |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 383 | goto pm_runtime_disable; |
| 384 | |
ludovic.desroches@atmel.com | 64e5cd7 | 2016-03-17 14:54:34 +0100 | [diff] [blame] | 385 | /* |
| 386 | * When calling sdhci_runtime_suspend_host(), the sdhci layer makes |
| 387 | * the assumption that all the clocks of the controller are disabled. |
| 388 | * It means we can't get irq from it when it is runtime suspended. |
| 389 | * For that reason, it is not planned to wake-up on a card detect irq |
| 390 | * from the controller. |
| 391 | * If we want to use runtime PM and to be able to wake-up on card |
| 392 | * insertion, we have to use a GPIO for the card detection or we can |
| 393 | * use polling. Be aware that using polling will resume/suspend the |
| 394 | * controller between each attempt. |
| 395 | * Disable SDHCI_QUIRK_BROKEN_CARD_DETECTION to be sure nobody tries |
| 396 | * to enable polling via device tree with broken-cd property. |
| 397 | */ |
Jaehoon Chung | 860951c | 2016-06-21 10:13:26 +0900 | [diff] [blame] | 398 | if (mmc_card_is_removable(host->mmc) && |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 399 | mmc_gpio_get_cd(host->mmc) < 0) { |
ludovic.desroches@atmel.com | 64e5cd7 | 2016-03-17 14:54:34 +0100 | [diff] [blame] | 400 | host->mmc->caps |= MMC_CAP_NEEDS_POLL; |
| 401 | host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION; |
| 402 | } |
| 403 | |
Ludovic Desroches | 7a1e3f1 | 2017-07-26 16:02:46 +0200 | [diff] [blame] | 404 | /* |
| 405 | * If the device attached to the MMC bus is not removable, it is safer |
| 406 | * to set the Force Card Detect bit. People often don't connect the |
| 407 | * card detect signal and use this pin for another purpose. If the card |
| 408 | * detect pin is not muxed to SDHCI controller, a default value is |
| 409 | * used. This value can be different from a SoC revision to another |
| 410 | * one. Problems come when this default value is not card present. To |
| 411 | * avoid this case, if the device is non removable then the card |
| 412 | * detection procedure using the SDMCC_CD signal is bypassed. |
| 413 | * This bit is reset when a software reset for all command is performed |
| 414 | * so we need to implement our own reset function to set back this bit. |
Michał Mirosław | 53dd0a7 | 2020-03-15 17:44:25 +0100 | [diff] [blame] | 415 | * |
| 416 | * WA: SAMA5D2 doesn't drive CMD if using CD GPIO line. |
Ludovic Desroches | 7a1e3f1 | 2017-07-26 16:02:46 +0200 | [diff] [blame] | 417 | */ |
Michał Mirosław | 53dd0a7 | 2020-03-15 17:44:25 +0100 | [diff] [blame] | 418 | if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) |
| 419 | || mmc_gpio_get_cd(host->mmc) >= 0) |
Ludovic Desroches | 7a1e3f1 | 2017-07-26 16:02:46 +0200 | [diff] [blame] | 420 | sdhci_at91_set_force_card_detect(host); |
| 421 | |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 422 | pm_runtime_put_autosuspend(&pdev->dev); |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 423 | |
| 424 | return 0; |
| 425 | |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 426 | pm_runtime_disable: |
| 427 | pm_runtime_disable(&pdev->dev); |
| 428 | pm_runtime_set_suspended(&pdev->dev); |
Jisheng Zhang | 2df9d58 | 2016-02-02 19:55:06 +0800 | [diff] [blame] | 429 | pm_runtime_put_noidle(&pdev->dev); |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 430 | clocks_disable_unprepare: |
| 431 | clk_disable_unprepare(priv->gck); |
| 432 | clk_disable_unprepare(priv->mainck); |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 433 | clk_disable_unprepare(priv->hclock); |
Quentin Schulz | c8a019e | 2017-07-13 10:04:17 +0200 | [diff] [blame] | 434 | sdhci_pltfm_free: |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 435 | sdhci_pltfm_free(pdev); |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static int sdhci_at91_remove(struct platform_device *pdev) |
| 440 | { |
| 441 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 442 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
Jisheng Zhang | 10f1c13 | 2016-02-16 21:08:25 +0800 | [diff] [blame] | 443 | struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); |
| 444 | struct clk *gck = priv->gck; |
| 445 | struct clk *hclock = priv->hclock; |
| 446 | struct clk *mainck = priv->mainck; |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 447 | |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 448 | pm_runtime_get_sync(&pdev->dev); |
| 449 | pm_runtime_disable(&pdev->dev); |
| 450 | pm_runtime_put_noidle(&pdev->dev); |
| 451 | |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 452 | sdhci_pltfm_unregister(pdev); |
| 453 | |
Jisheng Zhang | 10f1c13 | 2016-02-16 21:08:25 +0800 | [diff] [blame] | 454 | clk_disable_unprepare(gck); |
| 455 | clk_disable_unprepare(hclock); |
| 456 | clk_disable_unprepare(mainck); |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static struct platform_driver sdhci_at91_driver = { |
| 462 | .driver = { |
| 463 | .name = "sdhci-at91", |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 464 | .of_match_table = sdhci_at91_dt_match, |
ludovic.desroches@atmel.com | f5f1781 | 2015-11-11 19:11:48 +0100 | [diff] [blame] | 465 | .pm = &sdhci_at91_dev_pm_ops, |
ludovic.desroches@atmel.com | bb5f8ea | 2015-07-29 16:22:47 +0200 | [diff] [blame] | 466 | }, |
| 467 | .probe = sdhci_at91_probe, |
| 468 | .remove = sdhci_at91_remove, |
| 469 | }; |
| 470 | |
| 471 | module_platform_driver(sdhci_at91_driver); |
| 472 | |
| 473 | MODULE_DESCRIPTION("SDHCI driver for at91"); |
| 474 | MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>"); |
| 475 | MODULE_LICENSE("GPL v2"); |