.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Atmel SDMMC controller driver. |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2015 Atmel, |
---|
5 | 6 | * 2015 Ludovic Desroches <ludovic.desroches@atmel.com> |
---|
6 | | - * |
---|
7 | | - * This software is licensed under the terms of the GNU General Public |
---|
8 | | - * License version 2, as published by the Free Software Foundation, and |
---|
9 | | - * may be copied, distributed, and modified under those terms. |
---|
10 | | - * |
---|
11 | | - * This program is distributed in the hope that it will be useful, |
---|
12 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | | - * GNU General Public License for more details. |
---|
15 | 7 | */ |
---|
16 | 8 | |
---|
| 9 | +#include <linux/bitfield.h> |
---|
17 | 10 | #include <linux/clk.h> |
---|
18 | 11 | #include <linux/delay.h> |
---|
19 | 12 | #include <linux/err.h> |
---|
20 | 13 | #include <linux/io.h> |
---|
| 14 | +#include <linux/iopoll.h> |
---|
21 | 15 | #include <linux/kernel.h> |
---|
22 | 16 | #include <linux/mmc/host.h> |
---|
23 | 17 | #include <linux/mmc/slot-gpio.h> |
---|
.. | .. |
---|
35 | 29 | #define SDMMC_CACR 0x230 |
---|
36 | 30 | #define SDMMC_CACR_CAPWREN BIT(0) |
---|
37 | 31 | #define SDMMC_CACR_KEY (0x46 << 8) |
---|
| 32 | +#define SDMMC_CALCR 0x240 |
---|
| 33 | +#define SDMMC_CALCR_EN BIT(0) |
---|
| 34 | +#define SDMMC_CALCR_ALWYSON BIT(4) |
---|
38 | 35 | |
---|
39 | 36 | #define SDHCI_AT91_PRESET_COMMON_CONF 0x400 /* drv type B, programmable clock mode */ |
---|
40 | 37 | |
---|
| 38 | +struct sdhci_at91_soc_data { |
---|
| 39 | + const struct sdhci_pltfm_data *pdata; |
---|
| 40 | + bool baseclk_is_generated_internally; |
---|
| 41 | + unsigned int divider_for_baseclk; |
---|
| 42 | +}; |
---|
| 43 | + |
---|
41 | 44 | struct sdhci_at91_priv { |
---|
| 45 | + const struct sdhci_at91_soc_data *soc_data; |
---|
42 | 46 | struct clk *hclock; |
---|
43 | 47 | struct clk *gck; |
---|
44 | 48 | struct clk *mainck; |
---|
45 | 49 | bool restore_needed; |
---|
| 50 | + bool cal_always_on; |
---|
46 | 51 | }; |
---|
47 | 52 | |
---|
48 | 53 | static void sdhci_at91_set_force_card_detect(struct sdhci_host *host) |
---|
.. | .. |
---|
57 | 62 | static void sdhci_at91_set_clock(struct sdhci_host *host, unsigned int clock) |
---|
58 | 63 | { |
---|
59 | 64 | u16 clk; |
---|
60 | | - unsigned long timeout; |
---|
61 | 65 | |
---|
62 | 66 | host->mmc->actual_clock = 0; |
---|
63 | 67 | |
---|
.. | .. |
---|
82 | 86 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
---|
83 | 87 | |
---|
84 | 88 | /* Wait max 20 ms */ |
---|
85 | | - timeout = 20; |
---|
86 | | - while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL)) |
---|
87 | | - & SDHCI_CLOCK_INT_STABLE)) { |
---|
88 | | - if (timeout == 0) { |
---|
89 | | - pr_err("%s: Internal clock never stabilised.\n", |
---|
90 | | - mmc_hostname(host->mmc)); |
---|
91 | | - return; |
---|
92 | | - } |
---|
93 | | - timeout--; |
---|
94 | | - mdelay(1); |
---|
| 89 | + if (read_poll_timeout(sdhci_readw, clk, (clk & SDHCI_CLOCK_INT_STABLE), |
---|
| 90 | + 1000, 20000, false, host, SDHCI_CLOCK_CONTROL)) { |
---|
| 91 | + pr_err("%s: Internal clock never stabilised.\n", |
---|
| 92 | + mmc_hostname(host->mmc)); |
---|
| 93 | + return; |
---|
95 | 94 | } |
---|
96 | 95 | |
---|
97 | 96 | clk |= SDHCI_CLOCK_CARD_EN; |
---|
98 | 97 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
---|
99 | 98 | } |
---|
100 | 99 | |
---|
101 | | -/* |
---|
102 | | - * In this specific implementation of the SDHCI controller, the power register |
---|
103 | | - * needs to have a valid voltage set even when the power supply is managed by |
---|
104 | | - * an external regulator. |
---|
105 | | - */ |
---|
106 | | -static void sdhci_at91_set_power(struct sdhci_host *host, unsigned char mode, |
---|
107 | | - unsigned short vdd) |
---|
108 | | -{ |
---|
109 | | - if (!IS_ERR(host->mmc->supply.vmmc)) { |
---|
110 | | - struct mmc_host *mmc = host->mmc; |
---|
111 | | - |
---|
112 | | - mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd); |
---|
113 | | - } |
---|
114 | | - sdhci_set_power_noreg(host, mode, vdd); |
---|
115 | | -} |
---|
116 | | - |
---|
117 | 100 | static void sdhci_at91_set_uhs_signaling(struct sdhci_host *host, |
---|
118 | 101 | unsigned int timing) |
---|
119 | 102 | { |
---|
120 | | - if (timing == MMC_TIMING_MMC_DDR52) |
---|
121 | | - sdhci_writeb(host, SDMMC_MC1R_DDR, SDMMC_MC1R); |
---|
| 103 | + u8 mc1r; |
---|
| 104 | + |
---|
| 105 | + if (timing == MMC_TIMING_MMC_DDR52) { |
---|
| 106 | + mc1r = sdhci_readb(host, SDMMC_MC1R); |
---|
| 107 | + mc1r |= SDMMC_MC1R_DDR; |
---|
| 108 | + sdhci_writeb(host, mc1r, SDMMC_MC1R); |
---|
| 109 | + } |
---|
122 | 110 | sdhci_set_uhs_signaling(host, timing); |
---|
123 | 111 | } |
---|
124 | 112 | |
---|
125 | 113 | static void sdhci_at91_reset(struct sdhci_host *host, u8 mask) |
---|
126 | 114 | { |
---|
| 115 | + struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
---|
| 116 | + struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); |
---|
| 117 | + unsigned int tmp; |
---|
| 118 | + |
---|
127 | 119 | sdhci_reset(host, mask); |
---|
128 | 120 | |
---|
129 | 121 | if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) |
---|
130 | 122 | || mmc_gpio_get_cd(host->mmc) >= 0) |
---|
131 | 123 | sdhci_at91_set_force_card_detect(host); |
---|
| 124 | + |
---|
| 125 | + if (priv->cal_always_on && (mask & SDHCI_RESET_ALL)) { |
---|
| 126 | + u32 calcr = sdhci_readl(host, SDMMC_CALCR); |
---|
| 127 | + |
---|
| 128 | + sdhci_writel(host, calcr | SDMMC_CALCR_ALWYSON | SDMMC_CALCR_EN, |
---|
| 129 | + SDMMC_CALCR); |
---|
| 130 | + |
---|
| 131 | + if (read_poll_timeout(sdhci_readl, tmp, !(tmp & SDMMC_CALCR_EN), |
---|
| 132 | + 10, 20000, false, host, SDMMC_CALCR)) |
---|
| 133 | + dev_err(mmc_dev(host->mmc), "Failed to calibrate\n"); |
---|
| 134 | + } |
---|
132 | 135 | } |
---|
133 | 136 | |
---|
134 | 137 | static const struct sdhci_ops sdhci_at91_sama5d2_ops = { |
---|
.. | .. |
---|
136 | 139 | .set_bus_width = sdhci_set_bus_width, |
---|
137 | 140 | .reset = sdhci_at91_reset, |
---|
138 | 141 | .set_uhs_signaling = sdhci_at91_set_uhs_signaling, |
---|
139 | | - .set_power = sdhci_at91_set_power, |
---|
| 142 | + .set_power = sdhci_set_power_and_bus_voltage, |
---|
140 | 143 | }; |
---|
141 | 144 | |
---|
142 | | -static const struct sdhci_pltfm_data soc_data_sama5d2 = { |
---|
| 145 | +static const struct sdhci_pltfm_data sdhci_sama5d2_pdata = { |
---|
143 | 146 | .ops = &sdhci_at91_sama5d2_ops, |
---|
| 147 | +}; |
---|
| 148 | + |
---|
| 149 | +static const struct sdhci_at91_soc_data soc_data_sama5d2 = { |
---|
| 150 | + .pdata = &sdhci_sama5d2_pdata, |
---|
| 151 | + .baseclk_is_generated_internally = false, |
---|
| 152 | +}; |
---|
| 153 | + |
---|
| 154 | +static const struct sdhci_at91_soc_data soc_data_sam9x60 = { |
---|
| 155 | + .pdata = &sdhci_sama5d2_pdata, |
---|
| 156 | + .baseclk_is_generated_internally = true, |
---|
| 157 | + .divider_for_baseclk = 2, |
---|
144 | 158 | }; |
---|
145 | 159 | |
---|
146 | 160 | static const struct of_device_id sdhci_at91_dt_match[] = { |
---|
147 | 161 | { .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 }, |
---|
| 162 | + { .compatible = "microchip,sam9x60-sdhci", .data = &soc_data_sam9x60 }, |
---|
148 | 163 | {} |
---|
149 | 164 | }; |
---|
150 | 165 | MODULE_DEVICE_TABLE(of, sdhci_at91_dt_match); |
---|
.. | .. |
---|
154 | 169 | struct sdhci_host *host = dev_get_drvdata(dev); |
---|
155 | 170 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
---|
156 | 171 | struct sdhci_at91_priv *priv = sdhci_pltfm_priv(pltfm_host); |
---|
157 | | - int ret; |
---|
158 | 172 | unsigned int caps0, caps1; |
---|
159 | 173 | unsigned int clk_base, clk_mul; |
---|
160 | | - unsigned int gck_rate, real_gck_rate; |
---|
| 174 | + unsigned int gck_rate, clk_base_rate; |
---|
161 | 175 | unsigned int preset_div; |
---|
162 | 176 | |
---|
163 | | - /* |
---|
164 | | - * The mult clock is provided by as a generated clock by the PMC |
---|
165 | | - * controller. In order to set the rate of gck, we have to get the |
---|
166 | | - * base clock rate and the clock mult from capabilities. |
---|
167 | | - */ |
---|
168 | 177 | clk_prepare_enable(priv->hclock); |
---|
169 | 178 | caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES); |
---|
170 | 179 | caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1); |
---|
171 | | - clk_base = (caps0 & SDHCI_CLOCK_V3_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT; |
---|
172 | | - clk_mul = (caps1 & SDHCI_CLOCK_MUL_MASK) >> SDHCI_CLOCK_MUL_SHIFT; |
---|
173 | | - gck_rate = clk_base * 1000000 * (clk_mul + 1); |
---|
174 | | - ret = clk_set_rate(priv->gck, gck_rate); |
---|
175 | | - if (ret < 0) { |
---|
176 | | - dev_err(dev, "failed to set gck"); |
---|
177 | | - clk_disable_unprepare(priv->hclock); |
---|
178 | | - return ret; |
---|
179 | | - } |
---|
180 | | - /* |
---|
181 | | - * We need to check if we have the requested rate for gck because in |
---|
182 | | - * some cases this rate could be not supported. If it happens, the rate |
---|
183 | | - * is the closest one gck can provide. We have to update the value |
---|
184 | | - * of clk mul. |
---|
185 | | - */ |
---|
186 | | - real_gck_rate = clk_get_rate(priv->gck); |
---|
187 | | - if (real_gck_rate != gck_rate) { |
---|
188 | | - clk_mul = real_gck_rate / (clk_base * 1000000) - 1; |
---|
189 | | - caps1 &= (~SDHCI_CLOCK_MUL_MASK); |
---|
190 | | - caps1 |= ((clk_mul << SDHCI_CLOCK_MUL_SHIFT) & |
---|
191 | | - SDHCI_CLOCK_MUL_MASK); |
---|
192 | | - /* Set capabilities in r/w mode. */ |
---|
193 | | - writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, |
---|
194 | | - host->ioaddr + SDMMC_CACR); |
---|
195 | | - writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1); |
---|
196 | | - /* Set capabilities in ro mode. */ |
---|
197 | | - writel(0, host->ioaddr + SDMMC_CACR); |
---|
198 | | - dev_info(dev, "update clk mul to %u as gck rate is %u Hz\n", |
---|
199 | | - clk_mul, real_gck_rate); |
---|
200 | | - } |
---|
| 180 | + |
---|
| 181 | + gck_rate = clk_get_rate(priv->gck); |
---|
| 182 | + if (priv->soc_data->baseclk_is_generated_internally) |
---|
| 183 | + clk_base_rate = gck_rate / priv->soc_data->divider_for_baseclk; |
---|
| 184 | + else |
---|
| 185 | + clk_base_rate = clk_get_rate(priv->mainck); |
---|
| 186 | + |
---|
| 187 | + clk_base = clk_base_rate / 1000000; |
---|
| 188 | + clk_mul = gck_rate / clk_base_rate - 1; |
---|
| 189 | + |
---|
| 190 | + caps0 &= ~SDHCI_CLOCK_V3_BASE_MASK; |
---|
| 191 | + caps0 |= FIELD_PREP(SDHCI_CLOCK_V3_BASE_MASK, clk_base); |
---|
| 192 | + caps1 &= ~SDHCI_CLOCK_MUL_MASK; |
---|
| 193 | + caps1 |= FIELD_PREP(SDHCI_CLOCK_MUL_MASK, clk_mul); |
---|
| 194 | + /* Set capabilities in r/w mode. */ |
---|
| 195 | + writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR); |
---|
| 196 | + writel(caps0, host->ioaddr + SDHCI_CAPABILITIES); |
---|
| 197 | + writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1); |
---|
| 198 | + /* Set capabilities in ro mode. */ |
---|
| 199 | + writel(0, host->ioaddr + SDMMC_CACR); |
---|
| 200 | + |
---|
| 201 | + dev_dbg(dev, "update clk mul to %u as gck rate is %u Hz and clk base is %u Hz\n", |
---|
| 202 | + clk_mul, gck_rate, clk_base_rate); |
---|
201 | 203 | |
---|
202 | 204 | /* |
---|
203 | 205 | * We have to set preset values because it depends on the clk_mul |
---|
.. | .. |
---|
205 | 207 | * maximum sd clock value is 120 MHz instead of 208 MHz. For that |
---|
206 | 208 | * reason, we need to use presets to support SDR104. |
---|
207 | 209 | */ |
---|
208 | | - preset_div = DIV_ROUND_UP(real_gck_rate, 24000000) - 1; |
---|
| 210 | + preset_div = DIV_ROUND_UP(gck_rate, 24000000) - 1; |
---|
209 | 211 | writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, |
---|
210 | 212 | host->ioaddr + SDHCI_PRESET_FOR_SDR12); |
---|
211 | | - preset_div = DIV_ROUND_UP(real_gck_rate, 50000000) - 1; |
---|
| 213 | + preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1; |
---|
212 | 214 | writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, |
---|
213 | 215 | host->ioaddr + SDHCI_PRESET_FOR_SDR25); |
---|
214 | | - preset_div = DIV_ROUND_UP(real_gck_rate, 100000000) - 1; |
---|
| 216 | + preset_div = DIV_ROUND_UP(gck_rate, 100000000) - 1; |
---|
215 | 217 | writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, |
---|
216 | 218 | host->ioaddr + SDHCI_PRESET_FOR_SDR50); |
---|
217 | | - preset_div = DIV_ROUND_UP(real_gck_rate, 120000000) - 1; |
---|
| 219 | + preset_div = DIV_ROUND_UP(gck_rate, 120000000) - 1; |
---|
218 | 220 | writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, |
---|
219 | 221 | host->ioaddr + SDHCI_PRESET_FOR_SDR104); |
---|
220 | | - preset_div = DIV_ROUND_UP(real_gck_rate, 50000000) - 1; |
---|
| 222 | + preset_div = DIV_ROUND_UP(gck_rate, 50000000) - 1; |
---|
221 | 223 | writew(SDHCI_AT91_PRESET_COMMON_CONF | preset_div, |
---|
222 | 224 | host->ioaddr + SDHCI_PRESET_FOR_DDR50); |
---|
223 | 225 | |
---|
.. | .. |
---|
298 | 300 | } |
---|
299 | 301 | |
---|
300 | 302 | out: |
---|
301 | | - return sdhci_runtime_resume_host(host); |
---|
| 303 | + return sdhci_runtime_resume_host(host, 0); |
---|
302 | 304 | } |
---|
303 | 305 | #endif /* CONFIG_PM */ |
---|
304 | 306 | |
---|
.. | .. |
---|
312 | 314 | static int sdhci_at91_probe(struct platform_device *pdev) |
---|
313 | 315 | { |
---|
314 | 316 | const struct of_device_id *match; |
---|
315 | | - const struct sdhci_pltfm_data *soc_data; |
---|
| 317 | + const struct sdhci_at91_soc_data *soc_data; |
---|
316 | 318 | struct sdhci_host *host; |
---|
317 | 319 | struct sdhci_pltfm_host *pltfm_host; |
---|
318 | 320 | struct sdhci_at91_priv *priv; |
---|
.. | .. |
---|
323 | 325 | return -EINVAL; |
---|
324 | 326 | soc_data = match->data; |
---|
325 | 327 | |
---|
326 | | - host = sdhci_pltfm_init(pdev, soc_data, sizeof(*priv)); |
---|
| 328 | + host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*priv)); |
---|
327 | 329 | if (IS_ERR(host)) |
---|
328 | 330 | return PTR_ERR(host); |
---|
329 | 331 | |
---|
330 | 332 | pltfm_host = sdhci_priv(host); |
---|
331 | 333 | priv = sdhci_pltfm_priv(pltfm_host); |
---|
| 334 | + priv->soc_data = soc_data; |
---|
332 | 335 | |
---|
333 | 336 | priv->mainck = devm_clk_get(&pdev->dev, "baseclk"); |
---|
334 | 337 | if (IS_ERR(priv->mainck)) { |
---|
335 | | - dev_err(&pdev->dev, "failed to get baseclk\n"); |
---|
336 | | - ret = PTR_ERR(priv->mainck); |
---|
337 | | - goto sdhci_pltfm_free; |
---|
| 338 | + if (soc_data->baseclk_is_generated_internally) { |
---|
| 339 | + priv->mainck = NULL; |
---|
| 340 | + } else { |
---|
| 341 | + dev_err(&pdev->dev, "failed to get baseclk\n"); |
---|
| 342 | + ret = PTR_ERR(priv->mainck); |
---|
| 343 | + goto sdhci_pltfm_free; |
---|
| 344 | + } |
---|
338 | 345 | } |
---|
339 | 346 | |
---|
340 | 347 | priv->hclock = devm_clk_get(&pdev->dev, "hclock"); |
---|
.. | .. |
---|
356 | 363 | goto sdhci_pltfm_free; |
---|
357 | 364 | |
---|
358 | 365 | priv->restore_needed = false; |
---|
| 366 | + |
---|
| 367 | + /* |
---|
| 368 | + * if SDCAL pin is wrongly connected, we must enable |
---|
| 369 | + * the analog calibration cell permanently. |
---|
| 370 | + */ |
---|
| 371 | + priv->cal_always_on = |
---|
| 372 | + device_property_read_bool(&pdev->dev, |
---|
| 373 | + "microchip,sdcal-inverted"); |
---|
359 | 374 | |
---|
360 | 375 | ret = mmc_of_parse(host->mmc); |
---|
361 | 376 | if (ret) |
---|
.. | .. |
---|
455 | 470 | static struct platform_driver sdhci_at91_driver = { |
---|
456 | 471 | .driver = { |
---|
457 | 472 | .name = "sdhci-at91", |
---|
| 473 | + .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
---|
458 | 474 | .of_match_table = sdhci_at91_dt_match, |
---|
459 | 475 | .pm = &sdhci_at91_dev_pm_ops, |
---|
460 | 476 | }, |
---|