blob: c79bff5e2280a0dfd3abd28845c795a835e10e50 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +02002/*
3 * Atmel SDMMC controller driver.
4 *
5 * Copyright (C) 2015 Atmel,
6 * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +02007 */
8
9#include <linux/clk.h>
Ludovic Desroches4e289a72016-04-07 11:13:09 +020010#include <linux/delay.h>
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +020011#include <linux/err.h>
12#include <linux/io.h>
Ludovic Desroches44064332016-04-28 14:59:26 +020013#include <linux/kernel.h>
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +020014#include <linux/mmc/host.h>
ludovic.desroches@atmel.com64e5cd72016-03-17 14:54:34 +010015#include <linux/mmc/slot-gpio.h>
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +020016#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/of_device.h>
ludovic.desroches@atmel.comf5f17812015-11-11 19:11:48 +010019#include <linux/pm.h>
20#include <linux/pm_runtime.h>
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +020021
22#include "sdhci-pltfm.h"
23
Ludovic Desrochesd0918762017-03-28 11:00:45 +020024#define SDMMC_MC1R 0x204
25#define SDMMC_MC1R_DDR BIT(3)
Ludovic Desroches7a1e3f12017-07-26 16:02:46 +020026#define SDMMC_MC1R_FCD BIT(7)
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +020027#define SDMMC_CACR 0x230
28#define SDMMC_CACR_CAPWREN BIT(0)
29#define SDMMC_CACR_KEY (0x46 << 8)
Nicolas Ferre727d8362019-10-08 14:34:32 +020030#define SDMMC_CALCR 0x240
31#define SDMMC_CALCR_EN BIT(0)
32#define SDMMC_CALCR_ALWYSON BIT(4)
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +020033
Ludovic Desroches44064332016-04-28 14:59:26 +020034#define SDHCI_AT91_PRESET_COMMON_CONF 0x400 /* drv type B, programmable clock mode */
35
Ludovic Desroches39766562019-11-28 08:45:21 +010036struct 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.combb5f8ea2015-07-29 16:22:47 +020042struct sdhci_at91_priv {
Ludovic Desroches39766562019-11-28 08:45:21 +010043 const struct sdhci_at91_soc_data *soc_data;
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +020044 struct clk *hclock;
45 struct clk *gck;
46 struct clk *mainck;
Quentin Schulze2b372e2017-07-13 10:04:18 +020047 bool restore_needed;
Nicolas Ferre727d8362019-10-08 14:34:32 +020048 bool cal_always_on;
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +020049};
50
Ludovic Desroches7a1e3f12017-07-26 16:02:46 +020051static 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 Desroches4e289a72016-04-07 11:13:09 +020060static 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 King519c51a2017-10-03 11:06:36 +0100104static void sdhci_at91_set_uhs_signaling(struct sdhci_host *host,
105 unsigned int timing)
Ludovic Desrochesd0918762017-03-28 11:00:45 +0200106{
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 Desroches7a1e3f12017-07-26 16:02:46 +0200112static void sdhci_at91_reset(struct sdhci_host *host, u8 mask)
113{
Nicolas Ferre727d8362019-10-08 14:34:32 +0200114 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
115 struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
116
Ludovic Desroches7a1e3f12017-07-26 16:02:46 +0200117 sdhci_reset(host, mask);
118
Michał Mirosław53dd0a72020-03-15 17:44:25 +0100119 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE)
120 || mmc_gpio_get_cd(host->mmc) >= 0)
Ludovic Desroches7a1e3f12017-07-26 16:02:46 +0200121 sdhci_at91_set_force_card_detect(host);
Nicolas Ferre727d8362019-10-08 14:34:32 +0200122
123 if (priv->cal_always_on && (mask & SDHCI_RESET_ALL))
124 sdhci_writel(host, SDMMC_CALCR_ALWYSON | SDMMC_CALCR_EN,
125 SDMMC_CALCR);
Ludovic Desroches7a1e3f12017-07-26 16:02:46 +0200126}
127
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200128static const struct sdhci_ops sdhci_at91_sama5d2_ops = {
Ludovic Desroches4e289a72016-04-07 11:13:09 +0200129 .set_clock = sdhci_at91_set_clock,
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200130 .set_bus_width = sdhci_set_bus_width,
Ludovic Desroches7a1e3f12017-07-26 16:02:46 +0200131 .reset = sdhci_at91_reset,
Ludovic Desrochesd0918762017-03-28 11:00:45 +0200132 .set_uhs_signaling = sdhci_at91_set_uhs_signaling,
Nicolas Saenz Julienne98160562020-03-06 18:44:06 +0100133 .set_power = sdhci_set_power_and_bus_voltage,
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200134};
135
Ludovic Desroches39766562019-11-28 08:45:21 +0100136static const struct sdhci_pltfm_data sdhci_sama5d2_pdata = {
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200137 .ops = &sdhci_at91_sama5d2_ops,
138};
139
Ludovic Desroches39766562019-11-28 08:45:21 +0100140static const struct sdhci_at91_soc_data soc_data_sama5d2 = {
141 .pdata = &sdhci_sama5d2_pdata,
142 .baseclk_is_generated_internally = false,
143};
144
145static 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.combb5f8ea2015-07-29 16:22:47 +0200151static const struct of_device_id sdhci_at91_dt_match[] = {
152 { .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 },
Ludovic Desroches39766562019-11-28 08:45:21 +0100153 { .compatible = "microchip,sam9x60-sdhci", .data = &soc_data_sam9x60 },
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200154 {}
155};
Javier Martinez Canillasd9943c62016-10-17 13:13:45 -0300156MODULE_DEVICE_TABLE(of, sdhci_at91_dt_match);
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200157
Quentin Schulzc8a019e2017-07-13 10:04:17 +0200158static 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 Schulzc8a019e2017-07-13 10:04:17 +0200163 unsigned int caps0, caps1;
164 unsigned int clk_base, clk_mul;
Ludovic Desroches39766562019-11-28 08:45:21 +0100165 unsigned int gck_rate, clk_base_rate;
Quentin Schulzc8a019e2017-07-13 10:04:17 +0200166 unsigned int preset_div;
167
Quentin Schulzc8a019e2017-07-13 10:04:17 +0200168 clk_prepare_enable(priv->hclock);
169 caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES);
170 caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1);
Ludovic Desroches39766562019-11-28 08:45:21 +0100171
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 Birsanfdd8eef2020-03-12 14:29:24 +0000192 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 Schulzc8a019e2017-07-13 10:04:17 +0200194
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 Desroches39766562019-11-28 08:45:21 +0100201 preset_div = DIV_ROUND_UP(gck_rate, 24000000) - 1;
Quentin Schulzc8a019e2017-07-13 10:04:17 +0200202 writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
203 host->ioaddr + SDHCI_PRESET_FOR_SDR12);
Ludovic Desroches39766562019-11-28 08:45:21 +0100204 preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1;
Quentin Schulzc8a019e2017-07-13 10:04:17 +0200205 writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
206 host->ioaddr + SDHCI_PRESET_FOR_SDR25);
Ludovic Desroches39766562019-11-28 08:45:21 +0100207 preset_div = DIV_ROUND_UP(gck_rate, 100000000) - 1;
Quentin Schulzc8a019e2017-07-13 10:04:17 +0200208 writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
209 host->ioaddr + SDHCI_PRESET_FOR_SDR50);
Ludovic Desroches39766562019-11-28 08:45:21 +0100210 preset_div = DIV_ROUND_UP(gck_rate, 120000000) - 1;
Quentin Schulzc8a019e2017-07-13 10:04:17 +0200211 writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div,
212 host->ioaddr + SDHCI_PRESET_FOR_SDR104);
Ludovic Desroches39766562019-11-28 08:45:21 +0100213 preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1;
Quentin Schulzc8a019e2017-07-13 10:04:17 +0200214 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 Schulze2b372e2017-07-13 10:04:18 +0200223#ifdef CONFIG_PM_SLEEP
224static 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.comf5f17812015-11-11 19:11:48 +0100239#ifdef CONFIG_PM
240static 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 Zhang10f1c132016-02-16 21:08:25 +0800244 struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
ludovic.desroches@atmel.comf5f17812015-11-11 19:11:48 +0100245 int ret;
246
247 ret = sdhci_runtime_suspend_host(host);
248
Adrian Hunterd38dcad2017-03-20 19:50:32 +0200249 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
250 mmc_retune_needed(host->mmc);
251
ludovic.desroches@atmel.comf5f17812015-11-11 19:11:48 +0100252 clk_disable_unprepare(priv->gck);
253 clk_disable_unprepare(priv->hclock);
254 clk_disable_unprepare(priv->mainck);
255
256 return ret;
257}
258
259static 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 Zhang10f1c132016-02-16 21:08:25 +0800263 struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host);
ludovic.desroches@atmel.comf5f17812015-11-11 19:11:48 +0100264 int ret;
265
Quentin Schulze2b372e2017-07-13 10:04:18 +0200266 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.comf5f17812015-11-11 19:11:48 +0100275 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 Schulze2b372e2017-07-13 10:04:18 +0200293out:
Baolin Wangc6303c52019-07-25 11:14:22 +0800294 return sdhci_runtime_resume_host(host, 0);
ludovic.desroches@atmel.comf5f17812015-11-11 19:11:48 +0100295}
296#endif /* CONFIG_PM */
297
298static const struct dev_pm_ops sdhci_at91_dev_pm_ops = {
Quentin Schulze2b372e2017-07-13 10:04:18 +0200299 SET_SYSTEM_SLEEP_PM_OPS(sdhci_at91_suspend, pm_runtime_force_resume)
ludovic.desroches@atmel.comf5f17812015-11-11 19:11:48 +0100300 SET_RUNTIME_PM_OPS(sdhci_at91_runtime_suspend,
301 sdhci_at91_runtime_resume,
302 NULL)
303};
304
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200305static int sdhci_at91_probe(struct platform_device *pdev)
306{
307 const struct of_device_id *match;
Ludovic Desroches39766562019-11-28 08:45:21 +0100308 const struct sdhci_at91_soc_data *soc_data;
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200309 struct sdhci_host *host;
310 struct sdhci_pltfm_host *pltfm_host;
311 struct sdhci_at91_priv *priv;
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200312 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 Desroches39766562019-11-28 08:45:21 +0100319 host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*priv));
Jisheng Zhang10f1c132016-02-16 21:08:25 +0800320 if (IS_ERR(host))
321 return PTR_ERR(host);
322
323 pltfm_host = sdhci_priv(host);
324 priv = sdhci_pltfm_priv(pltfm_host);
Ludovic Desroches39766562019-11-28 08:45:21 +0100325 priv->soc_data = soc_data;
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200326
327 priv->mainck = devm_clk_get(&pdev->dev, "baseclk");
328 if (IS_ERR(priv->mainck)) {
Ludovic Desroches39766562019-11-28 08:45:21 +0100329 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ława04184c2020-01-02 11:42:16 +0100333 ret = PTR_ERR(priv->mainck);
334 goto sdhci_pltfm_free;
Ludovic Desroches39766562019-11-28 08:45:21 +0100335 }
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200336 }
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ława04184c2020-01-02 11:42:16 +0100341 ret = PTR_ERR(priv->hclock);
342 goto sdhci_pltfm_free;
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200343 }
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ława04184c2020-01-02 11:42:16 +0100348 ret = PTR_ERR(priv->gck);
349 goto sdhci_pltfm_free;
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200350 }
351
Quentin Schulzc8a019e2017-07-13 10:04:17 +0200352 ret = sdhci_at91_set_clks_presets(&pdev->dev);
353 if (ret)
354 goto sdhci_pltfm_free;
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200355
Quentin Schulze2b372e2017-07-13 10:04:18 +0200356 priv->restore_needed = false;
357
Nicolas Ferre727d8362019-10-08 14:34:32 +0200358 /*
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.combb5f8ea2015-07-29 16:22:47 +0200366 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.comf5f17812015-11-11 19:11:48 +0100372 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 Hristev7871aa62019-08-08 08:35:40 +0000378 /* HS200 is broken at this moment */
Eugen Hristevfed23c52019-11-14 12:59:26 +0000379 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
Eugen Hristev7871aa62019-08-08 08:35:40 +0000380
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200381 ret = sdhci_add_host(host);
382 if (ret)
ludovic.desroches@atmel.comf5f17812015-11-11 19:11:48 +0100383 goto pm_runtime_disable;
384
ludovic.desroches@atmel.com64e5cd72016-03-17 14:54:34 +0100385 /*
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 Chung860951c2016-06-21 10:13:26 +0900398 if (mmc_card_is_removable(host->mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +0200399 mmc_gpio_get_cd(host->mmc) < 0) {
ludovic.desroches@atmel.com64e5cd72016-03-17 14:54:34 +0100400 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
401 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
402 }
403
Ludovic Desroches7a1e3f12017-07-26 16:02:46 +0200404 /*
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ław53dd0a72020-03-15 17:44:25 +0100415 *
416 * WA: SAMA5D2 doesn't drive CMD if using CD GPIO line.
Ludovic Desroches7a1e3f12017-07-26 16:02:46 +0200417 */
Michał Mirosław53dd0a72020-03-15 17:44:25 +0100418 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE)
419 || mmc_gpio_get_cd(host->mmc) >= 0)
Ludovic Desroches7a1e3f12017-07-26 16:02:46 +0200420 sdhci_at91_set_force_card_detect(host);
421
ludovic.desroches@atmel.comf5f17812015-11-11 19:11:48 +0100422 pm_runtime_put_autosuspend(&pdev->dev);
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200423
424 return 0;
425
ludovic.desroches@atmel.comf5f17812015-11-11 19:11:48 +0100426pm_runtime_disable:
427 pm_runtime_disable(&pdev->dev);
428 pm_runtime_set_suspended(&pdev->dev);
Jisheng Zhang2df9d582016-02-02 19:55:06 +0800429 pm_runtime_put_noidle(&pdev->dev);
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200430clocks_disable_unprepare:
431 clk_disable_unprepare(priv->gck);
432 clk_disable_unprepare(priv->mainck);
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200433 clk_disable_unprepare(priv->hclock);
Quentin Schulzc8a019e2017-07-13 10:04:17 +0200434sdhci_pltfm_free:
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200435 sdhci_pltfm_free(pdev);
436 return ret;
437}
438
439static 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 Zhang10f1c132016-02-16 21:08:25 +0800443 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.combb5f8ea2015-07-29 16:22:47 +0200447
ludovic.desroches@atmel.comf5f17812015-11-11 19:11:48 +0100448 pm_runtime_get_sync(&pdev->dev);
449 pm_runtime_disable(&pdev->dev);
450 pm_runtime_put_noidle(&pdev->dev);
451
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200452 sdhci_pltfm_unregister(pdev);
453
Jisheng Zhang10f1c132016-02-16 21:08:25 +0800454 clk_disable_unprepare(gck);
455 clk_disable_unprepare(hclock);
456 clk_disable_unprepare(mainck);
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200457
458 return 0;
459}
460
461static struct platform_driver sdhci_at91_driver = {
462 .driver = {
463 .name = "sdhci-at91",
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200464 .of_match_table = sdhci_at91_dt_match,
ludovic.desroches@atmel.comf5f17812015-11-11 19:11:48 +0100465 .pm = &sdhci_at91_dev_pm_ops,
ludovic.desroches@atmel.combb5f8ea2015-07-29 16:22:47 +0200466 },
467 .probe = sdhci_at91_probe,
468 .remove = sdhci_at91_remove,
469};
470
471module_platform_driver(sdhci_at91_driver);
472
473MODULE_DESCRIPTION("SDHCI driver for at91");
474MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
475MODULE_LICENSE("GPL v2");