Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
e3ec3a3d SB |
2 | /* |
3 | * Arasan Secure Digital Host Controller Interface. | |
4 | * Copyright (C) 2011 - 2012 Michal Simek <monstr@monstr.eu> | |
5 | * Copyright (c) 2012 Wind River Systems, Inc. | |
6 | * Copyright (C) 2013 Pengutronix e.K. | |
7 | * Copyright (C) 2013 Xilinx Inc. | |
8 | * | |
9 | * Based on sdhci-of-esdhc.c | |
10 | * | |
11 | * Copyright (c) 2007 Freescale Semiconductor, Inc. | |
12 | * Copyright (c) 2009 MontaVista Software, Inc. | |
13 | * | |
14 | * Authors: Xiaobo Xie <X.Xie@freescale.com> | |
15 | * Anton Vorontsov <avorontsov@ru.mvista.com> | |
e3ec3a3d SB |
16 | */ |
17 | ||
c390f211 | 18 | #include <linux/clk-provider.h> |
3ea4666e | 19 | #include <linux/mfd/syscon.h> |
e3ec3a3d | 20 | #include <linux/module.h> |
308f3f8d | 21 | #include <linux/of_device.h> |
91aa3661 | 22 | #include <linux/phy/phy.h> |
3ea4666e | 23 | #include <linux/regmap.h> |
3794c542 | 24 | #include <linux/of.h> |
a5c8b2ae | 25 | #include <linux/firmware/xlnx-zynqmp.h> |
e3ec3a3d | 26 | |
84362d79 SL |
27 | #include "cqhci.h" |
28 | #include "sdhci-pltfm.h" | |
e3ec3a3d | 29 | |
84362d79 | 30 | #define SDHCI_ARASAN_VENDOR_REGISTER 0x78 |
1a470721 MN |
31 | |
32 | #define SDHCI_ARASAN_ITAPDLY_REGISTER 0xF0F8 | |
d338c6d0 MN |
33 | #define SDHCI_ARASAN_ITAPDLY_SEL_MASK 0xFF |
34 | ||
1a470721 | 35 | #define SDHCI_ARASAN_OTAPDLY_REGISTER 0xF0FC |
d338c6d0 | 36 | #define SDHCI_ARASAN_OTAPDLY_SEL_MASK 0x3F |
1a470721 | 37 | |
84362d79 | 38 | #define SDHCI_ARASAN_CQE_BASE_ADDR 0x200 |
a05c8465 | 39 | #define VENDOR_ENHANCED_STROBE BIT(0) |
e3ec3a3d | 40 | |
b2db9c67 DA |
41 | #define PHY_CLK_TOO_SLOW_HZ 400000 |
42 | ||
1a470721 MN |
43 | #define SDHCI_ITAPDLY_CHGWIN 0x200 |
44 | #define SDHCI_ITAPDLY_ENABLE 0x100 | |
45 | #define SDHCI_OTAPDLY_ENABLE 0x40 | |
46 | ||
a5c8b2ae MN |
47 | /* Default settings for ZynqMP Clock Phases */ |
48 | #define ZYNQMP_ICLK_PHASE {0, 63, 63, 0, 63, 0, 0, 183, 54, 0, 0} | |
49 | #define ZYNQMP_OCLK_PHASE {0, 72, 60, 0, 60, 72, 135, 48, 72, 135, 0} | |
50 | ||
1a470721 MN |
51 | #define VERSAL_ICLK_PHASE {0, 132, 132, 0, 132, 0, 0, 162, 90, 0, 0} |
52 | #define VERSAL_OCLK_PHASE {0, 60, 48, 0, 48, 72, 90, 36, 60, 90, 0} | |
53 | ||
3ea4666e DA |
54 | /* |
55 | * On some SoCs the syscon area has a feature where the upper 16-bits of | |
56 | * each 32-bit register act as a write mask for the lower 16-bits. This allows | |
57 | * atomic updates of the register without locking. This macro is used on SoCs | |
58 | * that have that feature. | |
59 | */ | |
60 | #define HIWORD_UPDATE(val, mask, shift) \ | |
61 | ((val) << (shift) | (mask) << ((shift) + 16)) | |
62 | ||
63 | /** | |
64 | * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map | |
65 | * | |
66 | * @reg: Offset within the syscon of the register containing this field | |
67 | * @width: Number of bits for this field | |
68 | * @shift: Bit offset within @reg of this field (or -1 if not avail) | |
69 | */ | |
70 | struct sdhci_arasan_soc_ctl_field { | |
71 | u32 reg; | |
72 | u16 width; | |
73 | s16 shift; | |
74 | }; | |
75 | ||
76 | /** | |
77 | * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers | |
78 | * | |
3ea4666e | 79 | * @baseclkfreq: Where to find corecfg_baseclkfreq |
b2ca77c9 | 80 | * @clockmultiplier: Where to find corecfg_clockmultiplier |
36c6aada | 81 | * @support64b: Where to find SUPPORT64B bit |
3ea4666e | 82 | * @hiword_update: If true, use HIWORD_UPDATE to access the syscon |
4908460e MN |
83 | * |
84 | * It's up to the licensee of the Arsan IP block to make these available | |
85 | * somewhere if needed. Presumably these will be scattered somewhere that's | |
86 | * accessible via the syscon API. | |
3ea4666e DA |
87 | */ |
88 | struct sdhci_arasan_soc_ctl_map { | |
89 | struct sdhci_arasan_soc_ctl_field baseclkfreq; | |
b2ca77c9 | 90 | struct sdhci_arasan_soc_ctl_field clockmultiplier; |
36c6aada | 91 | struct sdhci_arasan_soc_ctl_field support64b; |
3ea4666e DA |
92 | bool hiword_update; |
93 | }; | |
94 | ||
16ada730 MN |
95 | /** |
96 | * struct sdhci_arasan_clk_ops - Clock Operations for Arasan SD controller | |
97 | * | |
98 | * @sdcardclk_ops: The output clock related operations | |
99 | * @sampleclk_ops: The sample clock related operations | |
100 | */ | |
101 | struct sdhci_arasan_clk_ops { | |
102 | const struct clk_ops *sdcardclk_ops; | |
103 | const struct clk_ops *sampleclk_ops; | |
104 | }; | |
105 | ||
e1463618 | 106 | /** |
4908460e MN |
107 | * struct sdhci_arasan_clk_data - Arasan Controller Clock Data. |
108 | * | |
e1463618 MN |
109 | * @sdcardclk_hw: Struct for the clock we might provide to a PHY. |
110 | * @sdcardclk: Pointer to normal 'struct clock' for sdcardclk_hw. | |
07a14d1d MN |
111 | * @sampleclk_hw: Struct for the clock we might provide to a PHY. |
112 | * @sampleclk: Pointer to normal 'struct clock' for sampleclk_hw. | |
f3dafc37 MN |
113 | * @clk_phase_in: Array of Input Clock Phase Delays for all speed modes |
114 | * @clk_phase_out: Array of Output Clock Phase Delays for all speed modes | |
115 | * @set_clk_delays: Function pointer for setting Clock Delays | |
a5c8b2ae | 116 | * @clk_of_data: Platform specific runtime clock data storage pointer |
e1463618 MN |
117 | */ |
118 | struct sdhci_arasan_clk_data { | |
119 | struct clk_hw sdcardclk_hw; | |
120 | struct clk *sdcardclk; | |
07a14d1d MN |
121 | struct clk_hw sampleclk_hw; |
122 | struct clk *sampleclk; | |
f3dafc37 MN |
123 | int clk_phase_in[MMC_TIMING_MMC_HS400 + 1]; |
124 | int clk_phase_out[MMC_TIMING_MMC_HS400 + 1]; | |
125 | void (*set_clk_delays)(struct sdhci_host *host); | |
a5c8b2ae MN |
126 | void *clk_of_data; |
127 | }; | |
128 | ||
e3ec3a3d | 129 | /** |
4908460e MN |
130 | * struct sdhci_arasan_data - Arasan Controller Data |
131 | * | |
c390f211 | 132 | * @host: Pointer to the main SDHCI host structure. |
3ea4666e DA |
133 | * @clk_ahb: Pointer to the AHB clock |
134 | * @phy: Pointer to the generic phy | |
b2db9c67 | 135 | * @is_phy_on: True if the PHY is on; false if not. |
4908460e | 136 | * @has_cqe: True if controller has command queuing engine. |
e1463618 | 137 | * @clk_data: Struct for the Arasan Controller Clock Data. |
16ada730 | 138 | * @clk_ops: Struct for the Arasan Controller Clock Operations. |
3ea4666e DA |
139 | * @soc_ctl_base: Pointer to regmap for syscon for soc_ctl registers. |
140 | * @soc_ctl_map: Map to get offsets into soc_ctl registers. | |
4908460e | 141 | * @quirks: Arasan deviations from spec. |
e3ec3a3d SB |
142 | */ |
143 | struct sdhci_arasan_data { | |
c390f211 | 144 | struct sdhci_host *host; |
e3ec3a3d | 145 | struct clk *clk_ahb; |
91aa3661 | 146 | struct phy *phy; |
b2db9c67 | 147 | bool is_phy_on; |
3ea4666e | 148 | |
84362d79 | 149 | bool has_cqe; |
e1463618 | 150 | struct sdhci_arasan_clk_data clk_data; |
16ada730 | 151 | const struct sdhci_arasan_clk_ops *clk_ops; |
c390f211 | 152 | |
3ea4666e DA |
153 | struct regmap *soc_ctl_base; |
154 | const struct sdhci_arasan_soc_ctl_map *soc_ctl_map; | |
4908460e | 155 | unsigned int quirks; |
3794c542 ZB |
156 | |
157 | /* Controller does not have CD wired and will not function normally without */ | |
158 | #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0) | |
3f2c7d5d HG |
159 | /* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the |
160 | * internal clock even when the clock isn't stable */ | |
161 | #define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1) | |
3ea4666e DA |
162 | }; |
163 | ||
06b23ca0 FA |
164 | struct sdhci_arasan_of_data { |
165 | const struct sdhci_arasan_soc_ctl_map *soc_ctl_map; | |
166 | const struct sdhci_pltfm_data *pdata; | |
16ada730 | 167 | const struct sdhci_arasan_clk_ops *clk_ops; |
06b23ca0 FA |
168 | }; |
169 | ||
3ea4666e DA |
170 | static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = { |
171 | .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 }, | |
b2ca77c9 | 172 | .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0}, |
3ea4666e | 173 | .hiword_update = true, |
e3ec3a3d SB |
174 | }; |
175 | ||
5c1a4f40 RVM |
176 | static const struct sdhci_arasan_soc_ctl_map intel_lgm_emmc_soc_ctl_map = { |
177 | .baseclkfreq = { .reg = 0xa0, .width = 8, .shift = 2 }, | |
178 | .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 }, | |
179 | .hiword_update = false, | |
180 | }; | |
181 | ||
d1807ad6 RVM |
182 | static const struct sdhci_arasan_soc_ctl_map intel_lgm_sdxc_soc_ctl_map = { |
183 | .baseclkfreq = { .reg = 0x80, .width = 8, .shift = 2 }, | |
184 | .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 }, | |
185 | .hiword_update = false, | |
186 | }; | |
187 | ||
36c6aada WAZ |
188 | static const struct sdhci_arasan_soc_ctl_map intel_keembay_soc_ctl_map = { |
189 | .baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 }, | |
190 | .clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 }, | |
191 | .support64b = { .reg = 0x4, .width = 1, .shift = 24 }, | |
192 | .hiword_update = false, | |
193 | }; | |
194 | ||
3ea4666e DA |
195 | /** |
196 | * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers | |
197 | * | |
4908460e MN |
198 | * @host: The sdhci_host |
199 | * @fld: The field to write to | |
200 | * @val: The value to write | |
201 | * | |
3ea4666e DA |
202 | * This function allows writing to fields in sdhci_arasan_soc_ctl_map. |
203 | * Note that if a field is specified as not available (shift < 0) then | |
204 | * this function will silently return an error code. It will be noisy | |
205 | * and print errors for any other (unexpected) errors. | |
206 | * | |
4908460e | 207 | * Return: 0 on success and error value on error |
3ea4666e DA |
208 | */ |
209 | static int sdhci_arasan_syscon_write(struct sdhci_host *host, | |
210 | const struct sdhci_arasan_soc_ctl_field *fld, | |
211 | u32 val) | |
212 | { | |
213 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
214 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); | |
215 | struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base; | |
216 | u32 reg = fld->reg; | |
217 | u16 width = fld->width; | |
218 | s16 shift = fld->shift; | |
219 | int ret; | |
220 | ||
221 | /* | |
222 | * Silently return errors for shift < 0 so caller doesn't have | |
223 | * to check for fields which are optional. For fields that | |
224 | * are required then caller needs to do something special | |
225 | * anyway. | |
226 | */ | |
227 | if (shift < 0) | |
228 | return -EINVAL; | |
229 | ||
230 | if (sdhci_arasan->soc_ctl_map->hiword_update) | |
231 | ret = regmap_write(soc_ctl_base, reg, | |
232 | HIWORD_UPDATE(val, GENMASK(width, 0), | |
233 | shift)); | |
234 | else | |
235 | ret = regmap_update_bits(soc_ctl_base, reg, | |
236 | GENMASK(shift + width, shift), | |
237 | val << shift); | |
238 | ||
239 | /* Yell about (unexpected) regmap errors */ | |
240 | if (ret) | |
241 | pr_warn("%s: Regmap write fail: %d\n", | |
242 | mmc_hostname(host->mmc), ret); | |
243 | ||
244 | return ret; | |
245 | } | |
246 | ||
802ac39a SL |
247 | static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock) |
248 | { | |
249 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
250 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); | |
f3dafc37 | 251 | struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data; |
6fc09244 | 252 | bool ctrl_phy = false; |
802ac39a | 253 | |
b2db9c67 DA |
254 | if (!IS_ERR(sdhci_arasan->phy)) { |
255 | if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) { | |
256 | /* | |
257 | * If PHY off, set clock to max speed and power PHY on. | |
258 | * | |
259 | * Although PHY docs apparently suggest power cycling | |
260 | * when changing the clock the PHY doesn't like to be | |
261 | * powered on while at low speeds like those used in ID | |
262 | * mode. Even worse is powering the PHY on while the | |
263 | * clock is off. | |
264 | * | |
265 | * To workaround the PHY limitations, the best we can | |
266 | * do is to power it on at a faster speed and then slam | |
267 | * through low speeds without power cycling. | |
268 | */ | |
269 | sdhci_set_clock(host, host->max_clk); | |
b2db9c67 | 270 | phy_power_on(sdhci_arasan->phy); |
b2db9c67 DA |
271 | sdhci_arasan->is_phy_on = true; |
272 | ||
273 | /* | |
274 | * We'll now fall through to the below case with | |
275 | * ctrl_phy = false (so we won't turn off/on). The | |
276 | * sdhci_set_clock() will set the real clock. | |
277 | */ | |
278 | } else if (clock > PHY_CLK_TOO_SLOW_HZ) { | |
279 | /* | |
280 | * At higher clock speeds the PHY is fine being power | |
281 | * cycled and docs say you _should_ power cycle when | |
282 | * changing clock speeds. | |
283 | */ | |
284 | ctrl_phy = true; | |
285 | } | |
286 | } | |
802ac39a | 287 | |
b2db9c67 | 288 | if (ctrl_phy && sdhci_arasan->is_phy_on) { |
802ac39a | 289 | phy_power_off(sdhci_arasan->phy); |
b2db9c67 | 290 | sdhci_arasan->is_phy_on = false; |
802ac39a SL |
291 | } |
292 | ||
f3dafc37 MN |
293 | /* Set the Input and Output Clock Phase Delays */ |
294 | if (clk_data->set_clk_delays) | |
295 | clk_data->set_clk_delays(host); | |
296 | ||
802ac39a SL |
297 | sdhci_set_clock(host, clock); |
298 | ||
3f2c7d5d HG |
299 | if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE) |
300 | /* | |
301 | * Some controllers immediately report SDHCI_CLOCK_INT_STABLE | |
302 | * after enabling the clock even though the clock is not | |
303 | * stable. Trying to use a clock without waiting here results | |
304 | * in EILSEQ while detecting some older/slower cards. The | |
305 | * chosen delay is the maximum delay from sdhci_set_clock. | |
306 | */ | |
307 | msleep(20); | |
308 | ||
6fc09244 | 309 | if (ctrl_phy) { |
802ac39a | 310 | phy_power_on(sdhci_arasan->phy); |
b2db9c67 | 311 | sdhci_arasan->is_phy_on = true; |
802ac39a SL |
312 | } |
313 | } | |
314 | ||
a05c8465 SL |
315 | static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc, |
316 | struct mmc_ios *ios) | |
317 | { | |
318 | u32 vendor; | |
319 | struct sdhci_host *host = mmc_priv(mmc); | |
320 | ||
0daf72fe | 321 | vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER); |
a05c8465 SL |
322 | if (ios->enhanced_strobe) |
323 | vendor |= VENDOR_ENHANCED_STROBE; | |
324 | else | |
325 | vendor &= ~VENDOR_ENHANCED_STROBE; | |
326 | ||
0daf72fe | 327 | sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER); |
a05c8465 SL |
328 | } |
329 | ||
13d62fd2 | 330 | static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask) |
3794c542 ZB |
331 | { |
332 | u8 ctrl; | |
333 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
334 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); | |
335 | ||
336 | sdhci_reset(host, mask); | |
337 | ||
338 | if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) { | |
339 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); | |
340 | ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN; | |
341 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); | |
342 | } | |
343 | } | |
344 | ||
8a3bee9b SL |
345 | static int sdhci_arasan_voltage_switch(struct mmc_host *mmc, |
346 | struct mmc_ios *ios) | |
347 | { | |
348 | switch (ios->signal_voltage) { | |
349 | case MMC_SIGNAL_VOLTAGE_180: | |
350 | /* | |
351 | * Plese don't switch to 1V8 as arasan,5.1 doesn't | |
352 | * actually refer to this setting to indicate the | |
353 | * signal voltage and the state machine will be broken | |
354 | * actually if we force to enable 1V8. That's something | |
355 | * like broken quirk but we could work around here. | |
356 | */ | |
357 | return 0; | |
358 | case MMC_SIGNAL_VOLTAGE_330: | |
359 | case MMC_SIGNAL_VOLTAGE_120: | |
360 | /* We don't support 3V3 and 1V2 */ | |
361 | break; | |
362 | } | |
363 | ||
364 | return -EINVAL; | |
365 | } | |
366 | ||
a81dae3a | 367 | static const struct sdhci_ops sdhci_arasan_ops = { |
802ac39a | 368 | .set_clock = sdhci_arasan_set_clock, |
e3ec3a3d | 369 | .get_max_clock = sdhci_pltfm_clk_get_max_clock, |
8cc35289 | 370 | .get_timeout_clock = sdhci_pltfm_clk_get_max_clock, |
2317f56c | 371 | .set_bus_width = sdhci_set_bus_width, |
3794c542 | 372 | .reset = sdhci_arasan_reset, |
96d7b78c | 373 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
c2c5252c | 374 | .set_power = sdhci_set_power_and_bus_voltage, |
e3ec3a3d SB |
375 | }; |
376 | ||
84362d79 SL |
377 | static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask) |
378 | { | |
379 | int cmd_error = 0; | |
380 | int data_error = 0; | |
381 | ||
382 | if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error)) | |
383 | return intmask; | |
384 | ||
385 | cqhci_irq(host->mmc, intmask, cmd_error, data_error); | |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
390 | static void sdhci_arasan_dumpregs(struct mmc_host *mmc) | |
391 | { | |
392 | sdhci_dumpregs(mmc_priv(mmc)); | |
393 | } | |
394 | ||
395 | static void sdhci_arasan_cqe_enable(struct mmc_host *mmc) | |
396 | { | |
397 | struct sdhci_host *host = mmc_priv(mmc); | |
398 | u32 reg; | |
399 | ||
400 | reg = sdhci_readl(host, SDHCI_PRESENT_STATE); | |
401 | while (reg & SDHCI_DATA_AVAILABLE) { | |
402 | sdhci_readl(host, SDHCI_BUFFER); | |
403 | reg = sdhci_readl(host, SDHCI_PRESENT_STATE); | |
404 | } | |
405 | ||
406 | sdhci_cqe_enable(mmc); | |
407 | } | |
408 | ||
409 | static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = { | |
410 | .enable = sdhci_arasan_cqe_enable, | |
411 | .disable = sdhci_cqe_disable, | |
412 | .dumpregs = sdhci_arasan_dumpregs, | |
413 | }; | |
414 | ||
415 | static const struct sdhci_ops sdhci_arasan_cqe_ops = { | |
416 | .set_clock = sdhci_arasan_set_clock, | |
417 | .get_max_clock = sdhci_pltfm_clk_get_max_clock, | |
418 | .get_timeout_clock = sdhci_pltfm_clk_get_max_clock, | |
419 | .set_bus_width = sdhci_set_bus_width, | |
420 | .reset = sdhci_arasan_reset, | |
421 | .set_uhs_signaling = sdhci_set_uhs_signaling, | |
c2c5252c | 422 | .set_power = sdhci_set_power_and_bus_voltage, |
84362d79 SL |
423 | .irq = sdhci_arasan_cqhci_irq, |
424 | }; | |
425 | ||
426 | static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = { | |
427 | .ops = &sdhci_arasan_cqe_ops, | |
428 | .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, | |
429 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | | |
430 | SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN, | |
431 | }; | |
432 | ||
e3ec3a3d SB |
433 | #ifdef CONFIG_PM_SLEEP |
434 | /** | |
435 | * sdhci_arasan_suspend - Suspend method for the driver | |
436 | * @dev: Address of the device structure | |
e3ec3a3d SB |
437 | * |
438 | * Put the device in a low power state. | |
4908460e MN |
439 | * |
440 | * Return: 0 on success and error value on error | |
e3ec3a3d SB |
441 | */ |
442 | static int sdhci_arasan_suspend(struct device *dev) | |
443 | { | |
970f2d90 | 444 | struct sdhci_host *host = dev_get_drvdata(dev); |
e3ec3a3d | 445 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
89211418 | 446 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); |
e3ec3a3d SB |
447 | int ret; |
448 | ||
d38dcad4 AH |
449 | if (host->tuning_mode != SDHCI_TUNING_MODE_3) |
450 | mmc_retune_needed(host->mmc); | |
451 | ||
84362d79 SL |
452 | if (sdhci_arasan->has_cqe) { |
453 | ret = cqhci_suspend(host->mmc); | |
454 | if (ret) | |
455 | return ret; | |
456 | } | |
457 | ||
e3ec3a3d SB |
458 | ret = sdhci_suspend_host(host); |
459 | if (ret) | |
460 | return ret; | |
461 | ||
b2db9c67 | 462 | if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) { |
91aa3661 SL |
463 | ret = phy_power_off(sdhci_arasan->phy); |
464 | if (ret) { | |
465 | dev_err(dev, "Cannot power off phy.\n"); | |
466 | sdhci_resume_host(host); | |
467 | return ret; | |
468 | } | |
b2db9c67 | 469 | sdhci_arasan->is_phy_on = false; |
91aa3661 SL |
470 | } |
471 | ||
e3ec3a3d SB |
472 | clk_disable(pltfm_host->clk); |
473 | clk_disable(sdhci_arasan->clk_ahb); | |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
478 | /** | |
479 | * sdhci_arasan_resume - Resume method for the driver | |
480 | * @dev: Address of the device structure | |
e3ec3a3d SB |
481 | * |
482 | * Resume operation after suspend | |
4908460e MN |
483 | * |
484 | * Return: 0 on success and error value on error | |
e3ec3a3d SB |
485 | */ |
486 | static int sdhci_arasan_resume(struct device *dev) | |
487 | { | |
970f2d90 | 488 | struct sdhci_host *host = dev_get_drvdata(dev); |
e3ec3a3d | 489 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
89211418 | 490 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); |
e3ec3a3d SB |
491 | int ret; |
492 | ||
493 | ret = clk_enable(sdhci_arasan->clk_ahb); | |
494 | if (ret) { | |
495 | dev_err(dev, "Cannot enable AHB clock.\n"); | |
496 | return ret; | |
497 | } | |
498 | ||
499 | ret = clk_enable(pltfm_host->clk); | |
500 | if (ret) { | |
501 | dev_err(dev, "Cannot enable SD clock.\n"); | |
e3ec3a3d SB |
502 | return ret; |
503 | } | |
504 | ||
b2db9c67 | 505 | if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) { |
91aa3661 SL |
506 | ret = phy_power_on(sdhci_arasan->phy); |
507 | if (ret) { | |
508 | dev_err(dev, "Cannot power on phy.\n"); | |
509 | return ret; | |
510 | } | |
b2db9c67 | 511 | sdhci_arasan->is_phy_on = true; |
91aa3661 SL |
512 | } |
513 | ||
84362d79 SL |
514 | ret = sdhci_resume_host(host); |
515 | if (ret) { | |
516 | dev_err(dev, "Cannot resume host.\n"); | |
517 | return ret; | |
518 | } | |
519 | ||
520 | if (sdhci_arasan->has_cqe) | |
521 | return cqhci_resume(host->mmc); | |
522 | ||
523 | return 0; | |
e3ec3a3d SB |
524 | } |
525 | #endif /* ! CONFIG_PM_SLEEP */ | |
526 | ||
527 | static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend, | |
528 | sdhci_arasan_resume); | |
529 | ||
c390f211 DA |
530 | /** |
531 | * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate | |
532 | * | |
4908460e MN |
533 | * @hw: Pointer to the hardware clock structure. |
534 | * @parent_rate: The parent rate (should be rate of clk_xin). | |
535 | * | |
c390f211 DA |
536 | * Return the current actual rate of the SD card clock. This can be used |
537 | * to communicate with out PHY. | |
538 | * | |
4908460e | 539 | * Return: The card clock rate. |
c390f211 DA |
540 | */ |
541 | static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw, | |
542 | unsigned long parent_rate) | |
c390f211 | 543 | { |
e1463618 MN |
544 | struct sdhci_arasan_clk_data *clk_data = |
545 | container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw); | |
c390f211 | 546 | struct sdhci_arasan_data *sdhci_arasan = |
e1463618 | 547 | container_of(clk_data, struct sdhci_arasan_data, clk_data); |
c390f211 DA |
548 | struct sdhci_host *host = sdhci_arasan->host; |
549 | ||
550 | return host->mmc->actual_clock; | |
551 | } | |
552 | ||
553 | static const struct clk_ops arasan_sdcardclk_ops = { | |
554 | .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate, | |
555 | }; | |
556 | ||
07a14d1d MN |
557 | /** |
558 | * sdhci_arasan_sampleclk_recalc_rate - Return the sampling clock rate | |
559 | * | |
4908460e MN |
560 | * @hw: Pointer to the hardware clock structure. |
561 | * @parent_rate: The parent rate (should be rate of clk_xin). | |
562 | * | |
07a14d1d MN |
563 | * Return the current actual rate of the sampling clock. This can be used |
564 | * to communicate with out PHY. | |
565 | * | |
4908460e | 566 | * Return: The sample clock rate. |
07a14d1d MN |
567 | */ |
568 | static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw, | |
569 | unsigned long parent_rate) | |
07a14d1d MN |
570 | { |
571 | struct sdhci_arasan_clk_data *clk_data = | |
572 | container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw); | |
573 | struct sdhci_arasan_data *sdhci_arasan = | |
574 | container_of(clk_data, struct sdhci_arasan_data, clk_data); | |
575 | struct sdhci_host *host = sdhci_arasan->host; | |
576 | ||
577 | return host->mmc->actual_clock; | |
578 | } | |
579 | ||
580 | static const struct clk_ops arasan_sampleclk_ops = { | |
581 | .recalc_rate = sdhci_arasan_sampleclk_recalc_rate, | |
582 | }; | |
583 | ||
a5c8b2ae MN |
584 | /** |
585 | * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays | |
586 | * | |
4908460e MN |
587 | * @hw: Pointer to the hardware clock structure. |
588 | * @degrees: The clock phase shift between 0 - 359. | |
589 | * | |
a5c8b2ae MN |
590 | * Set the SD Output Clock Tap Delays for Output path |
591 | * | |
a5c8b2ae MN |
592 | * Return: 0 on success and error value on error |
593 | */ | |
594 | static int sdhci_zynqmp_sdcardclk_set_phase(struct clk_hw *hw, int degrees) | |
a5c8b2ae MN |
595 | { |
596 | struct sdhci_arasan_clk_data *clk_data = | |
597 | container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw); | |
598 | struct sdhci_arasan_data *sdhci_arasan = | |
599 | container_of(clk_data, struct sdhci_arasan_data, clk_data); | |
600 | struct sdhci_host *host = sdhci_arasan->host; | |
a5c8b2ae MN |
601 | const char *clk_name = clk_hw_get_name(hw); |
602 | u32 node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1; | |
603 | u8 tap_delay, tap_max = 0; | |
604 | int ret; | |
605 | ||
9e953432 MN |
606 | /* This is applicable for SDHCI_SPEC_300 and above */ |
607 | if (host->version < SDHCI_SPEC_300) | |
a5c8b2ae MN |
608 | return 0; |
609 | ||
610 | switch (host->timing) { | |
611 | case MMC_TIMING_MMC_HS: | |
612 | case MMC_TIMING_SD_HS: | |
613 | case MMC_TIMING_UHS_SDR25: | |
614 | case MMC_TIMING_UHS_DDR50: | |
615 | case MMC_TIMING_MMC_DDR52: | |
616 | /* For 50MHz clock, 30 Taps are available */ | |
617 | tap_max = 30; | |
618 | break; | |
619 | case MMC_TIMING_UHS_SDR50: | |
620 | /* For 100MHz clock, 15 Taps are available */ | |
621 | tap_max = 15; | |
622 | break; | |
623 | case MMC_TIMING_UHS_SDR104: | |
624 | case MMC_TIMING_MMC_HS200: | |
625 | /* For 200MHz clock, 8 Taps are available */ | |
626 | tap_max = 8; | |
627 | default: | |
628 | break; | |
629 | } | |
630 | ||
631 | tap_delay = (degrees * tap_max) / 360; | |
632 | ||
633 | /* Set the Clock Phase */ | |
426c8d85 | 634 | ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay); |
a5c8b2ae MN |
635 | if (ret) |
636 | pr_err("Error setting Output Tap Delay\n"); | |
637 | ||
d06d60d5 MN |
638 | /* Release DLL Reset */ |
639 | zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_RELEASE); | |
640 | ||
a5c8b2ae MN |
641 | return ret; |
642 | } | |
643 | ||
644 | static const struct clk_ops zynqmp_sdcardclk_ops = { | |
645 | .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate, | |
646 | .set_phase = sdhci_zynqmp_sdcardclk_set_phase, | |
647 | }; | |
648 | ||
649 | /** | |
650 | * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays | |
651 | * | |
4908460e MN |
652 | * @hw: Pointer to the hardware clock structure. |
653 | * @degrees: The clock phase shift between 0 - 359. | |
654 | * | |
a5c8b2ae MN |
655 | * Set the SD Input Clock Tap Delays for Input path |
656 | * | |
a5c8b2ae MN |
657 | * Return: 0 on success and error value on error |
658 | */ | |
659 | static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees) | |
a5c8b2ae MN |
660 | { |
661 | struct sdhci_arasan_clk_data *clk_data = | |
662 | container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw); | |
663 | struct sdhci_arasan_data *sdhci_arasan = | |
664 | container_of(clk_data, struct sdhci_arasan_data, clk_data); | |
665 | struct sdhci_host *host = sdhci_arasan->host; | |
a5c8b2ae MN |
666 | const char *clk_name = clk_hw_get_name(hw); |
667 | u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1; | |
668 | u8 tap_delay, tap_max = 0; | |
669 | int ret; | |
670 | ||
9e953432 MN |
671 | /* This is applicable for SDHCI_SPEC_300 and above */ |
672 | if (host->version < SDHCI_SPEC_300) | |
a5c8b2ae MN |
673 | return 0; |
674 | ||
d06d60d5 MN |
675 | /* Assert DLL Reset */ |
676 | zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_ASSERT); | |
677 | ||
a5c8b2ae MN |
678 | switch (host->timing) { |
679 | case MMC_TIMING_MMC_HS: | |
680 | case MMC_TIMING_SD_HS: | |
681 | case MMC_TIMING_UHS_SDR25: | |
682 | case MMC_TIMING_UHS_DDR50: | |
683 | case MMC_TIMING_MMC_DDR52: | |
684 | /* For 50MHz clock, 120 Taps are available */ | |
685 | tap_max = 120; | |
686 | break; | |
687 | case MMC_TIMING_UHS_SDR50: | |
688 | /* For 100MHz clock, 60 Taps are available */ | |
689 | tap_max = 60; | |
690 | break; | |
691 | case MMC_TIMING_UHS_SDR104: | |
692 | case MMC_TIMING_MMC_HS200: | |
693 | /* For 200MHz clock, 30 Taps are available */ | |
694 | tap_max = 30; | |
695 | default: | |
696 | break; | |
697 | } | |
698 | ||
699 | tap_delay = (degrees * tap_max) / 360; | |
700 | ||
701 | /* Set the Clock Phase */ | |
426c8d85 | 702 | ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay); |
a5c8b2ae MN |
703 | if (ret) |
704 | pr_err("Error setting Input Tap Delay\n"); | |
705 | ||
706 | return ret; | |
707 | } | |
708 | ||
709 | static const struct clk_ops zynqmp_sampleclk_ops = { | |
710 | .recalc_rate = sdhci_arasan_sampleclk_recalc_rate, | |
711 | .set_phase = sdhci_zynqmp_sampleclk_set_phase, | |
712 | }; | |
713 | ||
1a470721 MN |
714 | /** |
715 | * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays | |
716 | * | |
4908460e MN |
717 | * @hw: Pointer to the hardware clock structure. |
718 | * @degrees: The clock phase shift between 0 - 359. | |
719 | * | |
1a470721 MN |
720 | * Set the SD Output Clock Tap Delays for Output path |
721 | * | |
1a470721 MN |
722 | * Return: 0 on success and error value on error |
723 | */ | |
724 | static int sdhci_versal_sdcardclk_set_phase(struct clk_hw *hw, int degrees) | |
725 | { | |
726 | struct sdhci_arasan_clk_data *clk_data = | |
727 | container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw); | |
728 | struct sdhci_arasan_data *sdhci_arasan = | |
729 | container_of(clk_data, struct sdhci_arasan_data, clk_data); | |
730 | struct sdhci_host *host = sdhci_arasan->host; | |
731 | u8 tap_delay, tap_max = 0; | |
1a470721 | 732 | |
9e953432 MN |
733 | /* This is applicable for SDHCI_SPEC_300 and above */ |
734 | if (host->version < SDHCI_SPEC_300) | |
1a470721 MN |
735 | return 0; |
736 | ||
737 | switch (host->timing) { | |
738 | case MMC_TIMING_MMC_HS: | |
739 | case MMC_TIMING_SD_HS: | |
740 | case MMC_TIMING_UHS_SDR25: | |
741 | case MMC_TIMING_UHS_DDR50: | |
742 | case MMC_TIMING_MMC_DDR52: | |
743 | /* For 50MHz clock, 30 Taps are available */ | |
744 | tap_max = 30; | |
745 | break; | |
746 | case MMC_TIMING_UHS_SDR50: | |
747 | /* For 100MHz clock, 15 Taps are available */ | |
748 | tap_max = 15; | |
749 | break; | |
750 | case MMC_TIMING_UHS_SDR104: | |
751 | case MMC_TIMING_MMC_HS200: | |
752 | /* For 200MHz clock, 8 Taps are available */ | |
753 | tap_max = 8; | |
754 | default: | |
755 | break; | |
756 | } | |
757 | ||
758 | tap_delay = (degrees * tap_max) / 360; | |
759 | ||
760 | /* Set the Clock Phase */ | |
761 | if (tap_delay) { | |
762 | u32 regval; | |
763 | ||
764 | regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER); | |
765 | regval |= SDHCI_OTAPDLY_ENABLE; | |
766 | sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER); | |
d338c6d0 | 767 | regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK; |
1a470721 MN |
768 | regval |= tap_delay; |
769 | sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER); | |
770 | } | |
771 | ||
098c408b | 772 | return 0; |
1a470721 MN |
773 | } |
774 | ||
775 | static const struct clk_ops versal_sdcardclk_ops = { | |
776 | .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate, | |
777 | .set_phase = sdhci_versal_sdcardclk_set_phase, | |
778 | }; | |
779 | ||
780 | /** | |
781 | * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays | |
782 | * | |
4908460e MN |
783 | * @hw: Pointer to the hardware clock structure. |
784 | * @degrees: The clock phase shift between 0 - 359. | |
785 | * | |
1a470721 MN |
786 | * Set the SD Input Clock Tap Delays for Input path |
787 | * | |
1a470721 MN |
788 | * Return: 0 on success and error value on error |
789 | */ | |
790 | static int sdhci_versal_sampleclk_set_phase(struct clk_hw *hw, int degrees) | |
791 | { | |
792 | struct sdhci_arasan_clk_data *clk_data = | |
793 | container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw); | |
794 | struct sdhci_arasan_data *sdhci_arasan = | |
795 | container_of(clk_data, struct sdhci_arasan_data, clk_data); | |
796 | struct sdhci_host *host = sdhci_arasan->host; | |
797 | u8 tap_delay, tap_max = 0; | |
1a470721 | 798 | |
9e953432 MN |
799 | /* This is applicable for SDHCI_SPEC_300 and above */ |
800 | if (host->version < SDHCI_SPEC_300) | |
1a470721 MN |
801 | return 0; |
802 | ||
803 | switch (host->timing) { | |
804 | case MMC_TIMING_MMC_HS: | |
805 | case MMC_TIMING_SD_HS: | |
806 | case MMC_TIMING_UHS_SDR25: | |
807 | case MMC_TIMING_UHS_DDR50: | |
808 | case MMC_TIMING_MMC_DDR52: | |
809 | /* For 50MHz clock, 120 Taps are available */ | |
810 | tap_max = 120; | |
811 | break; | |
812 | case MMC_TIMING_UHS_SDR50: | |
813 | /* For 100MHz clock, 60 Taps are available */ | |
814 | tap_max = 60; | |
815 | break; | |
816 | case MMC_TIMING_UHS_SDR104: | |
817 | case MMC_TIMING_MMC_HS200: | |
818 | /* For 200MHz clock, 30 Taps are available */ | |
819 | tap_max = 30; | |
820 | default: | |
821 | break; | |
822 | } | |
823 | ||
824 | tap_delay = (degrees * tap_max) / 360; | |
825 | ||
826 | /* Set the Clock Phase */ | |
827 | if (tap_delay) { | |
828 | u32 regval; | |
829 | ||
830 | regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER); | |
831 | regval |= SDHCI_ITAPDLY_CHGWIN; | |
832 | sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER); | |
833 | regval |= SDHCI_ITAPDLY_ENABLE; | |
834 | sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER); | |
d338c6d0 | 835 | regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK; |
1a470721 MN |
836 | regval |= tap_delay; |
837 | sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER); | |
838 | regval &= ~SDHCI_ITAPDLY_CHGWIN; | |
839 | sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER); | |
840 | } | |
841 | ||
098c408b | 842 | return 0; |
1a470721 MN |
843 | } |
844 | ||
845 | static const struct clk_ops versal_sampleclk_ops = { | |
846 | .recalc_rate = sdhci_arasan_sampleclk_recalc_rate, | |
847 | .set_phase = sdhci_versal_sampleclk_set_phase, | |
848 | }; | |
849 | ||
8d2e3343 MN |
850 | static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid) |
851 | { | |
8d2e3343 MN |
852 | u16 clk; |
853 | ||
854 | clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); | |
855 | clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN); | |
856 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); | |
857 | ||
858 | /* Issue DLL Reset */ | |
426c8d85 | 859 | zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE); |
8d2e3343 MN |
860 | |
861 | clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); | |
862 | ||
863 | sdhci_enable_clk(host, clk); | |
864 | } | |
865 | ||
866 | static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode) | |
867 | { | |
868 | struct sdhci_host *host = mmc_priv(mmc); | |
869 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
870 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); | |
871 | struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw; | |
872 | const char *clk_name = clk_hw_get_name(hw); | |
873 | u32 device_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : | |
874 | NODE_SD_1; | |
875 | int err; | |
876 | ||
877 | arasan_zynqmp_dll_reset(host, device_id); | |
878 | ||
879 | err = sdhci_execute_tuning(mmc, opcode); | |
880 | if (err) | |
881 | return err; | |
882 | ||
883 | arasan_zynqmp_dll_reset(host, device_id); | |
884 | ||
885 | return 0; | |
886 | } | |
887 | ||
b2ca77c9 SL |
888 | /** |
889 | * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier | |
890 | * | |
4908460e MN |
891 | * @host: The sdhci_host |
892 | * @value: The value to write | |
893 | * | |
b2ca77c9 SL |
894 | * The corecfg_clockmultiplier is supposed to contain clock multiplier |
895 | * value of programmable clock generator. | |
896 | * | |
897 | * NOTES: | |
898 | * - Many existing devices don't seem to do this and work fine. To keep | |
899 | * compatibility for old hardware where the device tree doesn't provide a | |
900 | * register map, this function is a noop if a soc_ctl_map hasn't been provided | |
901 | * for this platform. | |
902 | * - The value of corecfg_clockmultiplier should sync with that of corresponding | |
903 | * value reading from sdhci_capability_register. So this function is called | |
904 | * once at probe time and never called again. | |
b2ca77c9 SL |
905 | */ |
906 | static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host, | |
907 | u32 value) | |
908 | { | |
909 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
910 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); | |
911 | const struct sdhci_arasan_soc_ctl_map *soc_ctl_map = | |
912 | sdhci_arasan->soc_ctl_map; | |
913 | ||
914 | /* Having a map is optional */ | |
915 | if (!soc_ctl_map) | |
916 | return; | |
917 | ||
918 | /* If we have a map, we expect to have a syscon */ | |
919 | if (!sdhci_arasan->soc_ctl_base) { | |
920 | pr_warn("%s: Have regmap, but no soc-ctl-syscon\n", | |
921 | mmc_hostname(host->mmc)); | |
922 | return; | |
923 | } | |
924 | ||
925 | sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value); | |
926 | } | |
927 | ||
3ea4666e DA |
928 | /** |
929 | * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq | |
930 | * | |
4908460e MN |
931 | * @host: The sdhci_host |
932 | * | |
3ea4666e DA |
933 | * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This |
934 | * function can be used to make that happen. | |
935 | * | |
936 | * NOTES: | |
937 | * - Many existing devices don't seem to do this and work fine. To keep | |
938 | * compatibility for old hardware where the device tree doesn't provide a | |
939 | * register map, this function is a noop if a soc_ctl_map hasn't been provided | |
940 | * for this platform. | |
941 | * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider | |
942 | * to achieve lower clock rates. That means that this function is called once | |
943 | * at probe time and never called again. | |
3ea4666e DA |
944 | */ |
945 | static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host) | |
946 | { | |
947 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
948 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); | |
949 | const struct sdhci_arasan_soc_ctl_map *soc_ctl_map = | |
950 | sdhci_arasan->soc_ctl_map; | |
951 | u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000); | |
952 | ||
953 | /* Having a map is optional */ | |
954 | if (!soc_ctl_map) | |
955 | return; | |
956 | ||
957 | /* If we have a map, we expect to have a syscon */ | |
958 | if (!sdhci_arasan->soc_ctl_base) { | |
959 | pr_warn("%s: Have regmap, but no soc-ctl-syscon\n", | |
960 | mmc_hostname(host->mmc)); | |
961 | return; | |
962 | } | |
963 | ||
964 | sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz); | |
965 | } | |
966 | ||
f3dafc37 MN |
967 | static void sdhci_arasan_set_clk_delays(struct sdhci_host *host) |
968 | { | |
969 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
970 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); | |
971 | struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data; | |
972 | ||
973 | clk_set_phase(clk_data->sampleclk, | |
974 | clk_data->clk_phase_in[host->timing]); | |
975 | clk_set_phase(clk_data->sdcardclk, | |
976 | clk_data->clk_phase_out[host->timing]); | |
977 | } | |
978 | ||
979 | static void arasan_dt_read_clk_phase(struct device *dev, | |
980 | struct sdhci_arasan_clk_data *clk_data, | |
981 | unsigned int timing, const char *prop) | |
982 | { | |
983 | struct device_node *np = dev->of_node; | |
984 | ||
985 | int clk_phase[2] = {0}; | |
986 | ||
987 | /* | |
988 | * Read Tap Delay values from DT, if the DT does not contain the | |
989 | * Tap Values then use the pre-defined values. | |
990 | */ | |
991 | if (of_property_read_variable_u32_array(np, prop, &clk_phase[0], | |
992 | 2, 0)) { | |
993 | dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n", | |
994 | prop, clk_data->clk_phase_in[timing], | |
995 | clk_data->clk_phase_out[timing]); | |
996 | return; | |
997 | } | |
998 | ||
999 | /* The values read are Input and Output Clock Delays in order */ | |
1000 | clk_data->clk_phase_in[timing] = clk_phase[0]; | |
1001 | clk_data->clk_phase_out[timing] = clk_phase[1]; | |
1002 | } | |
1003 | ||
1004 | /** | |
1005 | * arasan_dt_parse_clk_phases - Read Clock Delay values from DT | |
1006 | * | |
f3dafc37 MN |
1007 | * @dev: Pointer to our struct device. |
1008 | * @clk_data: Pointer to the Clock Data structure | |
4908460e MN |
1009 | * |
1010 | * Called at initialization to parse the values of Clock Delays. | |
f3dafc37 MN |
1011 | */ |
1012 | static void arasan_dt_parse_clk_phases(struct device *dev, | |
1013 | struct sdhci_arasan_clk_data *clk_data) | |
1014 | { | |
a5c8b2ae MN |
1015 | u32 mio_bank = 0; |
1016 | int i; | |
1017 | ||
f3dafc37 MN |
1018 | /* |
1019 | * This has been kept as a pointer and is assigned a function here. | |
1020 | * So that different controller variants can assign their own handling | |
1021 | * function. | |
1022 | */ | |
1023 | clk_data->set_clk_delays = sdhci_arasan_set_clk_delays; | |
1024 | ||
a5c8b2ae | 1025 | if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) { |
88e1d0b1 MN |
1026 | u32 zynqmp_iclk_phase[MMC_TIMING_MMC_HS400 + 1] = |
1027 | ZYNQMP_ICLK_PHASE; | |
1028 | u32 zynqmp_oclk_phase[MMC_TIMING_MMC_HS400 + 1] = | |
1029 | ZYNQMP_OCLK_PHASE; | |
a5c8b2ae MN |
1030 | |
1031 | of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank); | |
1032 | if (mio_bank == 2) { | |
88e1d0b1 MN |
1033 | zynqmp_oclk_phase[MMC_TIMING_UHS_SDR104] = 90; |
1034 | zynqmp_oclk_phase[MMC_TIMING_MMC_HS200] = 90; | |
a5c8b2ae MN |
1035 | } |
1036 | ||
1037 | for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) { | |
88e1d0b1 MN |
1038 | clk_data->clk_phase_in[i] = zynqmp_iclk_phase[i]; |
1039 | clk_data->clk_phase_out[i] = zynqmp_oclk_phase[i]; | |
a5c8b2ae MN |
1040 | } |
1041 | } | |
1042 | ||
1a470721 | 1043 | if (of_device_is_compatible(dev->of_node, "xlnx,versal-8.9a")) { |
88e1d0b1 MN |
1044 | u32 versal_iclk_phase[MMC_TIMING_MMC_HS400 + 1] = |
1045 | VERSAL_ICLK_PHASE; | |
1046 | u32 versal_oclk_phase[MMC_TIMING_MMC_HS400 + 1] = | |
1047 | VERSAL_OCLK_PHASE; | |
1a470721 MN |
1048 | |
1049 | for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) { | |
88e1d0b1 MN |
1050 | clk_data->clk_phase_in[i] = versal_iclk_phase[i]; |
1051 | clk_data->clk_phase_out[i] = versal_oclk_phase[i]; | |
1a470721 MN |
1052 | } |
1053 | } | |
1054 | ||
f3dafc37 MN |
1055 | arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY, |
1056 | "clk-phase-legacy"); | |
1057 | arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS, | |
1058 | "clk-phase-mmc-hs"); | |
1059 | arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS, | |
1060 | "clk-phase-sd-hs"); | |
1061 | arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12, | |
1062 | "clk-phase-uhs-sdr12"); | |
1063 | arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25, | |
1064 | "clk-phase-uhs-sdr25"); | |
1065 | arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50, | |
1066 | "clk-phase-uhs-sdr50"); | |
1067 | arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104, | |
1068 | "clk-phase-uhs-sdr104"); | |
1069 | arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50, | |
1070 | "clk-phase-uhs-ddr50"); | |
1071 | arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52, | |
1072 | "clk-phase-mmc-ddr52"); | |
1073 | arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200, | |
1074 | "clk-phase-mmc-hs200"); | |
1075 | arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400, | |
1076 | "clk-phase-mmc-hs400"); | |
1077 | } | |
1078 | ||
37d3ee7c MN |
1079 | static const struct sdhci_pltfm_data sdhci_arasan_pdata = { |
1080 | .ops = &sdhci_arasan_ops, | |
1081 | .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, | |
1082 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | | |
1083 | SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN | | |
1084 | SDHCI_QUIRK2_STOP_WITH_TC, | |
1085 | }; | |
1086 | ||
16ada730 MN |
1087 | static const struct sdhci_arasan_clk_ops arasan_clk_ops = { |
1088 | .sdcardclk_ops = &arasan_sdcardclk_ops, | |
1089 | .sampleclk_ops = &arasan_sampleclk_ops, | |
1090 | }; | |
1091 | ||
37d3ee7c MN |
1092 | static struct sdhci_arasan_of_data sdhci_arasan_generic_data = { |
1093 | .pdata = &sdhci_arasan_pdata, | |
16ada730 | 1094 | .clk_ops = &arasan_clk_ops, |
37d3ee7c MN |
1095 | }; |
1096 | ||
36c6aada WAZ |
1097 | static const struct sdhci_pltfm_data sdhci_keembay_emmc_pdata = { |
1098 | .ops = &sdhci_arasan_cqe_ops, | |
1099 | .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | | |
1100 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | | |
1101 | SDHCI_QUIRK_NO_LED | | |
1102 | SDHCI_QUIRK_32BIT_DMA_ADDR | | |
1103 | SDHCI_QUIRK_32BIT_DMA_SIZE | | |
1104 | SDHCI_QUIRK_32BIT_ADMA_SIZE, | |
1105 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | | |
1106 | SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN | | |
1107 | SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 | | |
1108 | SDHCI_QUIRK2_STOP_WITH_TC | | |
1109 | SDHCI_QUIRK2_BROKEN_64_BIT_DMA, | |
1110 | }; | |
1111 | ||
1112 | static const struct sdhci_pltfm_data sdhci_keembay_sd_pdata = { | |
1113 | .ops = &sdhci_arasan_ops, | |
1114 | .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | | |
1115 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | | |
1116 | SDHCI_QUIRK_NO_LED | | |
1117 | SDHCI_QUIRK_32BIT_DMA_ADDR | | |
1118 | SDHCI_QUIRK_32BIT_DMA_SIZE | | |
1119 | SDHCI_QUIRK_32BIT_ADMA_SIZE, | |
1120 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | | |
1121 | SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN | | |
1122 | SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON | | |
1123 | SDHCI_QUIRK2_STOP_WITH_TC | | |
1124 | SDHCI_QUIRK2_BROKEN_64_BIT_DMA, | |
1125 | }; | |
1126 | ||
1127 | static const struct sdhci_pltfm_data sdhci_keembay_sdio_pdata = { | |
1128 | .ops = &sdhci_arasan_ops, | |
1129 | .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | | |
1130 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | | |
1131 | SDHCI_QUIRK_NO_LED | | |
1132 | SDHCI_QUIRK_32BIT_DMA_ADDR | | |
1133 | SDHCI_QUIRK_32BIT_DMA_SIZE | | |
1134 | SDHCI_QUIRK_32BIT_ADMA_SIZE, | |
1135 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | | |
1136 | SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN | | |
1137 | SDHCI_QUIRK2_HOST_OFF_CARD_ON | | |
1138 | SDHCI_QUIRK2_BROKEN_64_BIT_DMA, | |
1139 | }; | |
1140 | ||
37d3ee7c MN |
1141 | static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = { |
1142 | .soc_ctl_map = &rk3399_soc_ctl_map, | |
1143 | .pdata = &sdhci_arasan_cqe_pdata, | |
16ada730 | 1144 | .clk_ops = &arasan_clk_ops, |
37d3ee7c MN |
1145 | }; |
1146 | ||
1147 | static struct sdhci_arasan_of_data intel_lgm_emmc_data = { | |
1148 | .soc_ctl_map = &intel_lgm_emmc_soc_ctl_map, | |
1149 | .pdata = &sdhci_arasan_cqe_pdata, | |
16ada730 | 1150 | .clk_ops = &arasan_clk_ops, |
37d3ee7c MN |
1151 | }; |
1152 | ||
1153 | static struct sdhci_arasan_of_data intel_lgm_sdxc_data = { | |
1154 | .soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map, | |
1155 | .pdata = &sdhci_arasan_cqe_pdata, | |
16ada730 | 1156 | .clk_ops = &arasan_clk_ops, |
37d3ee7c MN |
1157 | }; |
1158 | ||
1159 | static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = { | |
1160 | .ops = &sdhci_arasan_ops, | |
1161 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | | |
1162 | SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN | | |
1163 | SDHCI_QUIRK2_STOP_WITH_TC, | |
1164 | }; | |
1165 | ||
16ada730 MN |
1166 | static const struct sdhci_arasan_clk_ops zynqmp_clk_ops = { |
1167 | .sdcardclk_ops = &zynqmp_sdcardclk_ops, | |
1168 | .sampleclk_ops = &zynqmp_sampleclk_ops, | |
1169 | }; | |
1170 | ||
37d3ee7c MN |
1171 | static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = { |
1172 | .pdata = &sdhci_arasan_zynqmp_pdata, | |
16ada730 MN |
1173 | .clk_ops = &zynqmp_clk_ops, |
1174 | }; | |
1175 | ||
1176 | static const struct sdhci_arasan_clk_ops versal_clk_ops = { | |
1177 | .sdcardclk_ops = &versal_sdcardclk_ops, | |
1178 | .sampleclk_ops = &versal_sampleclk_ops, | |
37d3ee7c MN |
1179 | }; |
1180 | ||
1181 | static struct sdhci_arasan_of_data sdhci_arasan_versal_data = { | |
1182 | .pdata = &sdhci_arasan_zynqmp_pdata, | |
16ada730 | 1183 | .clk_ops = &versal_clk_ops, |
37d3ee7c MN |
1184 | }; |
1185 | ||
36c6aada WAZ |
1186 | static struct sdhci_arasan_of_data intel_keembay_emmc_data = { |
1187 | .soc_ctl_map = &intel_keembay_soc_ctl_map, | |
1188 | .pdata = &sdhci_keembay_emmc_pdata, | |
a42a7ec9 | 1189 | .clk_ops = &arasan_clk_ops, |
36c6aada WAZ |
1190 | }; |
1191 | ||
1192 | static struct sdhci_arasan_of_data intel_keembay_sd_data = { | |
1193 | .soc_ctl_map = &intel_keembay_soc_ctl_map, | |
1194 | .pdata = &sdhci_keembay_sd_pdata, | |
a42a7ec9 | 1195 | .clk_ops = &arasan_clk_ops, |
36c6aada WAZ |
1196 | }; |
1197 | ||
1198 | static struct sdhci_arasan_of_data intel_keembay_sdio_data = { | |
1199 | .soc_ctl_map = &intel_keembay_soc_ctl_map, | |
1200 | .pdata = &sdhci_keembay_sdio_pdata, | |
a42a7ec9 | 1201 | .clk_ops = &arasan_clk_ops, |
36c6aada WAZ |
1202 | }; |
1203 | ||
37d3ee7c MN |
1204 | static const struct of_device_id sdhci_arasan_of_match[] = { |
1205 | /* SoC-specific compatible strings w/ soc_ctl_map */ | |
1206 | { | |
1207 | .compatible = "rockchip,rk3399-sdhci-5.1", | |
1208 | .data = &sdhci_arasan_rk3399_data, | |
1209 | }, | |
1210 | { | |
1211 | .compatible = "intel,lgm-sdhci-5.1-emmc", | |
1212 | .data = &intel_lgm_emmc_data, | |
1213 | }, | |
1214 | { | |
1215 | .compatible = "intel,lgm-sdhci-5.1-sdxc", | |
1216 | .data = &intel_lgm_sdxc_data, | |
1217 | }, | |
36c6aada WAZ |
1218 | { |
1219 | .compatible = "intel,keembay-sdhci-5.1-emmc", | |
1220 | .data = &intel_keembay_emmc_data, | |
1221 | }, | |
1222 | { | |
1223 | .compatible = "intel,keembay-sdhci-5.1-sd", | |
1224 | .data = &intel_keembay_sd_data, | |
1225 | }, | |
1226 | { | |
1227 | .compatible = "intel,keembay-sdhci-5.1-sdio", | |
1228 | .data = &intel_keembay_sdio_data, | |
1229 | }, | |
37d3ee7c MN |
1230 | /* Generic compatible below here */ |
1231 | { | |
1232 | .compatible = "arasan,sdhci-8.9a", | |
1233 | .data = &sdhci_arasan_generic_data, | |
1234 | }, | |
1235 | { | |
1236 | .compatible = "arasan,sdhci-5.1", | |
1237 | .data = &sdhci_arasan_generic_data, | |
1238 | }, | |
1239 | { | |
1240 | .compatible = "arasan,sdhci-4.9a", | |
1241 | .data = &sdhci_arasan_generic_data, | |
1242 | }, | |
1243 | { | |
1244 | .compatible = "xlnx,zynqmp-8.9a", | |
1245 | .data = &sdhci_arasan_zynqmp_data, | |
1246 | }, | |
1247 | { | |
1248 | .compatible = "xlnx,versal-8.9a", | |
1249 | .data = &sdhci_arasan_versal_data, | |
1250 | }, | |
1251 | { /* sentinel */ } | |
1252 | }; | |
1253 | MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match); | |
1254 | ||
c390f211 | 1255 | /** |
07a14d1d | 1256 | * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use |
c390f211 | 1257 | * |
4908460e MN |
1258 | * @sdhci_arasan: Our private data structure. |
1259 | * @clk_xin: Pointer to the functional clock | |
1260 | * @dev: Pointer to our struct device. | |
1261 | * | |
c390f211 DA |
1262 | * Some PHY devices need to know what the actual card clock is. In order for |
1263 | * them to find out, we'll provide a clock through the common clock framework | |
1264 | * for them to query. | |
1265 | * | |
4908460e | 1266 | * Return: 0 on success and error value on error |
c390f211 | 1267 | */ |
07a14d1d MN |
1268 | static int |
1269 | sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan, | |
1270 | struct clk *clk_xin, | |
1271 | struct device *dev) | |
c390f211 | 1272 | { |
e1463618 | 1273 | struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data; |
c390f211 DA |
1274 | struct device_node *np = dev->of_node; |
1275 | struct clk_init_data sdcardclk_init; | |
1276 | const char *parent_clk_name; | |
1277 | int ret; | |
1278 | ||
c390f211 DA |
1279 | ret = of_property_read_string_index(np, "clock-output-names", 0, |
1280 | &sdcardclk_init.name); | |
1281 | if (ret) { | |
1282 | dev_err(dev, "DT has #clock-cells but no clock-output-names\n"); | |
1283 | return ret; | |
1284 | } | |
1285 | ||
1286 | parent_clk_name = __clk_get_name(clk_xin); | |
1287 | sdcardclk_init.parent_names = &parent_clk_name; | |
1288 | sdcardclk_init.num_parents = 1; | |
1289 | sdcardclk_init.flags = CLK_GET_RATE_NOCACHE; | |
16ada730 | 1290 | sdcardclk_init.ops = sdhci_arasan->clk_ops->sdcardclk_ops; |
c390f211 | 1291 | |
e1463618 MN |
1292 | clk_data->sdcardclk_hw.init = &sdcardclk_init; |
1293 | clk_data->sdcardclk = | |
1294 | devm_clk_register(dev, &clk_data->sdcardclk_hw); | |
c99e1d0c CY |
1295 | if (IS_ERR(clk_data->sdcardclk)) |
1296 | return PTR_ERR(clk_data->sdcardclk); | |
e1463618 | 1297 | clk_data->sdcardclk_hw.init = NULL; |
c390f211 DA |
1298 | |
1299 | ret = of_clk_add_provider(np, of_clk_src_simple_get, | |
e1463618 | 1300 | clk_data->sdcardclk); |
c390f211 | 1301 | if (ret) |
07a14d1d MN |
1302 | dev_err(dev, "Failed to add sdcard clock provider\n"); |
1303 | ||
1304 | return ret; | |
1305 | } | |
1306 | ||
1307 | /** | |
1308 | * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use | |
1309 | * | |
4908460e MN |
1310 | * @sdhci_arasan: Our private data structure. |
1311 | * @clk_xin: Pointer to the functional clock | |
1312 | * @dev: Pointer to our struct device. | |
1313 | * | |
07a14d1d MN |
1314 | * Some PHY devices need to know what the actual card clock is. In order for |
1315 | * them to find out, we'll provide a clock through the common clock framework | |
1316 | * for them to query. | |
1317 | * | |
4908460e | 1318 | * Return: 0 on success and error value on error |
07a14d1d MN |
1319 | */ |
1320 | static int | |
1321 | sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan, | |
1322 | struct clk *clk_xin, | |
1323 | struct device *dev) | |
1324 | { | |
1325 | struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data; | |
1326 | struct device_node *np = dev->of_node; | |
1327 | struct clk_init_data sampleclk_init; | |
1328 | const char *parent_clk_name; | |
1329 | int ret; | |
1330 | ||
1331 | ret = of_property_read_string_index(np, "clock-output-names", 1, | |
1332 | &sampleclk_init.name); | |
1333 | if (ret) { | |
1334 | dev_err(dev, "DT has #clock-cells but no clock-output-names\n"); | |
1335 | return ret; | |
1336 | } | |
1337 | ||
1338 | parent_clk_name = __clk_get_name(clk_xin); | |
1339 | sampleclk_init.parent_names = &parent_clk_name; | |
1340 | sampleclk_init.num_parents = 1; | |
1341 | sampleclk_init.flags = CLK_GET_RATE_NOCACHE; | |
16ada730 | 1342 | sampleclk_init.ops = sdhci_arasan->clk_ops->sampleclk_ops; |
07a14d1d MN |
1343 | |
1344 | clk_data->sampleclk_hw.init = &sampleclk_init; | |
1345 | clk_data->sampleclk = | |
1346 | devm_clk_register(dev, &clk_data->sampleclk_hw); | |
c99e1d0c CY |
1347 | if (IS_ERR(clk_data->sampleclk)) |
1348 | return PTR_ERR(clk_data->sampleclk); | |
07a14d1d MN |
1349 | clk_data->sampleclk_hw.init = NULL; |
1350 | ||
1351 | ret = of_clk_add_provider(np, of_clk_src_simple_get, | |
1352 | clk_data->sampleclk); | |
1353 | if (ret) | |
1354 | dev_err(dev, "Failed to add sample clock provider\n"); | |
c390f211 DA |
1355 | |
1356 | return ret; | |
1357 | } | |
1358 | ||
1359 | /** | |
1360 | * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk() | |
1361 | * | |
4908460e MN |
1362 | * @dev: Pointer to our struct device. |
1363 | * | |
c390f211 DA |
1364 | * Should be called any time we're exiting and sdhci_arasan_register_sdclk() |
1365 | * returned success. | |
c390f211 DA |
1366 | */ |
1367 | static void sdhci_arasan_unregister_sdclk(struct device *dev) | |
1368 | { | |
1369 | struct device_node *np = dev->of_node; | |
1370 | ||
1371 | if (!of_find_property(np, "#clock-cells", NULL)) | |
1372 | return; | |
1373 | ||
1374 | of_clk_del_provider(dev->of_node); | |
1375 | } | |
1376 | ||
36c6aada WAZ |
1377 | /** |
1378 | * sdhci_arasan_update_support64b - Set SUPPORT_64B (64-bit System Bus Support) | |
1379 | * | |
1380 | * This should be set based on the System Address Bus. | |
1381 | * 0: the Core supports only 32-bit System Address Bus. | |
1382 | * 1: the Core supports 64-bit System Address Bus. | |
1383 | * | |
1384 | * NOTES: | |
1385 | * - For Keem Bay, it is required to clear this bit. Its default value is 1'b1. | |
1386 | * Keem Bay does not support 64-bit access. | |
1387 | * | |
654e8750 LJ |
1388 | * @host: The sdhci_host |
1389 | * @value: The value to write | |
36c6aada WAZ |
1390 | */ |
1391 | static void sdhci_arasan_update_support64b(struct sdhci_host *host, u32 value) | |
1392 | { | |
1393 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
1394 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); | |
1395 | const struct sdhci_arasan_soc_ctl_map *soc_ctl_map = | |
1396 | sdhci_arasan->soc_ctl_map; | |
1397 | ||
1398 | /* Having a map is optional */ | |
1399 | if (!soc_ctl_map) | |
1400 | return; | |
1401 | ||
1402 | /* If we have a map, we expect to have a syscon */ | |
1403 | if (!sdhci_arasan->soc_ctl_base) { | |
1404 | pr_warn("%s: Have regmap, but no soc-ctl-syscon\n", | |
1405 | mmc_hostname(host->mmc)); | |
1406 | return; | |
1407 | } | |
1408 | ||
1409 | sdhci_arasan_syscon_write(host, &soc_ctl_map->support64b, value); | |
1410 | } | |
1411 | ||
07a14d1d MN |
1412 | /** |
1413 | * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use | |
1414 | * | |
4908460e MN |
1415 | * @sdhci_arasan: Our private data structure. |
1416 | * @clk_xin: Pointer to the functional clock | |
1417 | * @dev: Pointer to our struct device. | |
1418 | * | |
07a14d1d MN |
1419 | * Some PHY devices need to know what the actual card clock is. In order for |
1420 | * them to find out, we'll provide a clock through the common clock framework | |
1421 | * for them to query. | |
1422 | * | |
1423 | * Note: without seriously re-architecting SDHCI's clock code and testing on | |
1424 | * all platforms, there's no way to create a totally beautiful clock here | |
1425 | * with all clock ops implemented. Instead, we'll just create a clock that can | |
1426 | * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock | |
1427 | * framework that we're doing things behind its back. This should be sufficient | |
1428 | * to create nice clean device tree bindings and later (if needed) we can try | |
1429 | * re-architecting SDHCI if we see some benefit to it. | |
1430 | * | |
4908460e | 1431 | * Return: 0 on success and error value on error |
07a14d1d MN |
1432 | */ |
1433 | static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan, | |
1434 | struct clk *clk_xin, | |
1435 | struct device *dev) | |
1436 | { | |
1437 | struct device_node *np = dev->of_node; | |
1438 | u32 num_clks = 0; | |
1439 | int ret; | |
1440 | ||
1441 | /* Providing a clock to the PHY is optional; no error if missing */ | |
1442 | if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0) | |
1443 | return 0; | |
1444 | ||
1445 | ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev); | |
1446 | if (ret) | |
1447 | return ret; | |
1448 | ||
1449 | if (num_clks) { | |
1450 | ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin, | |
1451 | dev); | |
1452 | if (ret) { | |
1453 | sdhci_arasan_unregister_sdclk(dev); | |
1454 | return ret; | |
1455 | } | |
1456 | } | |
1457 | ||
1458 | return 0; | |
1459 | } | |
1460 | ||
84362d79 SL |
1461 | static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan) |
1462 | { | |
1463 | struct sdhci_host *host = sdhci_arasan->host; | |
1464 | struct cqhci_host *cq_host; | |
1465 | bool dma64; | |
1466 | int ret; | |
1467 | ||
1468 | if (!sdhci_arasan->has_cqe) | |
1469 | return sdhci_add_host(host); | |
1470 | ||
1471 | ret = sdhci_setup_host(host); | |
1472 | if (ret) | |
1473 | return ret; | |
1474 | ||
1475 | cq_host = devm_kzalloc(host->mmc->parent, | |
1476 | sizeof(*cq_host), GFP_KERNEL); | |
1477 | if (!cq_host) { | |
1478 | ret = -ENOMEM; | |
1479 | goto cleanup; | |
1480 | } | |
1481 | ||
1482 | cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR; | |
1483 | cq_host->ops = &sdhci_arasan_cqhci_ops; | |
1484 | ||
1485 | dma64 = host->flags & SDHCI_USE_64_BIT_DMA; | |
1486 | if (dma64) | |
1487 | cq_host->caps |= CQHCI_TASK_DESC_SZ_128; | |
1488 | ||
1489 | ret = cqhci_init(cq_host, host->mmc, dma64); | |
1490 | if (ret) | |
1491 | goto cleanup; | |
1492 | ||
1493 | ret = __sdhci_add_host(host); | |
1494 | if (ret) | |
1495 | goto cleanup; | |
1496 | ||
1497 | return 0; | |
1498 | ||
1499 | cleanup: | |
1500 | sdhci_cleanup_host(host); | |
1501 | return ret; | |
1502 | } | |
1503 | ||
e3ec3a3d SB |
1504 | static int sdhci_arasan_probe(struct platform_device *pdev) |
1505 | { | |
1506 | int ret; | |
3ea4666e DA |
1507 | const struct of_device_id *match; |
1508 | struct device_node *node; | |
e3ec3a3d SB |
1509 | struct clk *clk_xin; |
1510 | struct sdhci_host *host; | |
1511 | struct sdhci_pltfm_host *pltfm_host; | |
1512 | struct sdhci_arasan_data *sdhci_arasan; | |
3794c542 | 1513 | struct device_node *np = pdev->dev.of_node; |
06b23ca0 | 1514 | const struct sdhci_arasan_of_data *data; |
84362d79 | 1515 | |
06b23ca0 FA |
1516 | match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node); |
1517 | data = match->data; | |
1518 | host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan)); | |
e3ec3a3d | 1519 | |
89211418 JZ |
1520 | if (IS_ERR(host)) |
1521 | return PTR_ERR(host); | |
1522 | ||
1523 | pltfm_host = sdhci_priv(host); | |
1524 | sdhci_arasan = sdhci_pltfm_priv(pltfm_host); | |
c390f211 | 1525 | sdhci_arasan->host = host; |
e3ec3a3d | 1526 | |
06b23ca0 | 1527 | sdhci_arasan->soc_ctl_map = data->soc_ctl_map; |
16ada730 | 1528 | sdhci_arasan->clk_ops = data->clk_ops; |
3ea4666e DA |
1529 | |
1530 | node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0); | |
1531 | if (node) { | |
1532 | sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node); | |
1533 | of_node_put(node); | |
1534 | ||
1535 | if (IS_ERR(sdhci_arasan->soc_ctl_base)) { | |
72ea817d KK |
1536 | ret = dev_err_probe(&pdev->dev, |
1537 | PTR_ERR(sdhci_arasan->soc_ctl_base), | |
1538 | "Can't get syscon\n"); | |
3ea4666e DA |
1539 | goto err_pltfm_free; |
1540 | } | |
1541 | } | |
1542 | ||
e3ec3a3d SB |
1543 | sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb"); |
1544 | if (IS_ERR(sdhci_arasan->clk_ahb)) { | |
1545 | dev_err(&pdev->dev, "clk_ahb clock not found.\n"); | |
278d0962 SL |
1546 | ret = PTR_ERR(sdhci_arasan->clk_ahb); |
1547 | goto err_pltfm_free; | |
e3ec3a3d SB |
1548 | } |
1549 | ||
1550 | clk_xin = devm_clk_get(&pdev->dev, "clk_xin"); | |
1551 | if (IS_ERR(clk_xin)) { | |
1552 | dev_err(&pdev->dev, "clk_xin clock not found.\n"); | |
278d0962 SL |
1553 | ret = PTR_ERR(clk_xin); |
1554 | goto err_pltfm_free; | |
e3ec3a3d SB |
1555 | } |
1556 | ||
1557 | ret = clk_prepare_enable(sdhci_arasan->clk_ahb); | |
1558 | if (ret) { | |
1559 | dev_err(&pdev->dev, "Unable to enable AHB clock.\n"); | |
278d0962 | 1560 | goto err_pltfm_free; |
e3ec3a3d SB |
1561 | } |
1562 | ||
1563 | ret = clk_prepare_enable(clk_xin); | |
1564 | if (ret) { | |
1565 | dev_err(&pdev->dev, "Unable to enable SD clock.\n"); | |
1566 | goto clk_dis_ahb; | |
1567 | } | |
1568 | ||
e3ec3a3d | 1569 | sdhci_get_of_property(pdev); |
3794c542 ZB |
1570 | |
1571 | if (of_property_read_bool(np, "xlnx,fails-without-test-cd")) | |
1572 | sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST; | |
1573 | ||
3f2c7d5d HG |
1574 | if (of_property_read_bool(np, "xlnx,int-clock-stable-broken")) |
1575 | sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE; | |
1576 | ||
e3ec3a3d SB |
1577 | pltfm_host->clk = clk_xin; |
1578 | ||
b2ca77c9 SL |
1579 | if (of_device_is_compatible(pdev->dev.of_node, |
1580 | "rockchip,rk3399-sdhci-5.1")) | |
1581 | sdhci_arasan_update_clockmultiplier(host, 0x0); | |
1582 | ||
36c6aada WAZ |
1583 | if (of_device_is_compatible(np, "intel,keembay-sdhci-5.1-emmc") || |
1584 | of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sd") || | |
1585 | of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sdio")) { | |
1586 | sdhci_arasan_update_clockmultiplier(host, 0x0); | |
1587 | sdhci_arasan_update_support64b(host, 0x0); | |
1588 | ||
1589 | host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY; | |
1590 | } | |
1591 | ||
3ea4666e DA |
1592 | sdhci_arasan_update_baseclkfreq(host); |
1593 | ||
c390f211 DA |
1594 | ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev); |
1595 | if (ret) | |
1596 | goto clk_disable_all; | |
1597 | ||
a5c8b2ae | 1598 | if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) { |
8d2e3343 MN |
1599 | host->mmc_host_ops.execute_tuning = |
1600 | arasan_zynqmp_execute_tuning; | |
a5c8b2ae MN |
1601 | } |
1602 | ||
f3dafc37 MN |
1603 | arasan_dt_parse_clk_phases(&pdev->dev, &sdhci_arasan->clk_data); |
1604 | ||
16b23787 MS |
1605 | ret = mmc_of_parse(host->mmc); |
1606 | if (ret) { | |
60208a26 MS |
1607 | if (ret != -EPROBE_DEFER) |
1608 | dev_err(&pdev->dev, "parsing dt failed (%d)\n", ret); | |
c390f211 | 1609 | goto unreg_clk; |
16b23787 MS |
1610 | } |
1611 | ||
91aa3661 SL |
1612 | sdhci_arasan->phy = ERR_PTR(-ENODEV); |
1613 | if (of_device_is_compatible(pdev->dev.of_node, | |
1614 | "arasan,sdhci-5.1")) { | |
1615 | sdhci_arasan->phy = devm_phy_get(&pdev->dev, | |
1616 | "phy_arasan"); | |
1617 | if (IS_ERR(sdhci_arasan->phy)) { | |
1618 | ret = PTR_ERR(sdhci_arasan->phy); | |
1619 | dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n"); | |
c390f211 | 1620 | goto unreg_clk; |
91aa3661 SL |
1621 | } |
1622 | ||
1623 | ret = phy_init(sdhci_arasan->phy); | |
1624 | if (ret < 0) { | |
1625 | dev_err(&pdev->dev, "phy_init err.\n"); | |
c390f211 | 1626 | goto unreg_clk; |
91aa3661 SL |
1627 | } |
1628 | ||
a05c8465 SL |
1629 | host->mmc_host_ops.hs400_enhanced_strobe = |
1630 | sdhci_arasan_hs400_enhanced_strobe; | |
8a3bee9b SL |
1631 | host->mmc_host_ops.start_signal_voltage_switch = |
1632 | sdhci_arasan_voltage_switch; | |
84362d79 | 1633 | sdhci_arasan->has_cqe = true; |
7bda9482 CM |
1634 | host->mmc->caps2 |= MMC_CAP2_CQE; |
1635 | ||
1636 | if (!of_property_read_bool(np, "disable-cqe-dcmd")) | |
1637 | host->mmc->caps2 |= MMC_CAP2_CQE_DCMD; | |
91aa3661 SL |
1638 | } |
1639 | ||
84362d79 | 1640 | ret = sdhci_arasan_add_host(sdhci_arasan); |
b1df9de7 | 1641 | if (ret) |
91aa3661 | 1642 | goto err_add_host; |
e3ec3a3d SB |
1643 | |
1644 | return 0; | |
1645 | ||
91aa3661 | 1646 | err_add_host: |
91aa3661 SL |
1647 | if (!IS_ERR(sdhci_arasan->phy)) |
1648 | phy_exit(sdhci_arasan->phy); | |
c390f211 DA |
1649 | unreg_clk: |
1650 | sdhci_arasan_unregister_sdclk(&pdev->dev); | |
e3ec3a3d SB |
1651 | clk_disable_all: |
1652 | clk_disable_unprepare(clk_xin); | |
1653 | clk_dis_ahb: | |
1654 | clk_disable_unprepare(sdhci_arasan->clk_ahb); | |
278d0962 SL |
1655 | err_pltfm_free: |
1656 | sdhci_pltfm_free(pdev); | |
e3ec3a3d SB |
1657 | return ret; |
1658 | } | |
1659 | ||
1660 | static int sdhci_arasan_remove(struct platform_device *pdev) | |
1661 | { | |
0c7fe32e | 1662 | int ret; |
e3ec3a3d SB |
1663 | struct sdhci_host *host = platform_get_drvdata(pdev); |
1664 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
89211418 JZ |
1665 | struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host); |
1666 | struct clk *clk_ahb = sdhci_arasan->clk_ahb; | |
e3ec3a3d | 1667 | |
91aa3661 | 1668 | if (!IS_ERR(sdhci_arasan->phy)) { |
b2db9c67 DA |
1669 | if (sdhci_arasan->is_phy_on) |
1670 | phy_power_off(sdhci_arasan->phy); | |
91aa3661 SL |
1671 | phy_exit(sdhci_arasan->phy); |
1672 | } | |
1673 | ||
c390f211 DA |
1674 | sdhci_arasan_unregister_sdclk(&pdev->dev); |
1675 | ||
0c7fe32e JZ |
1676 | ret = sdhci_pltfm_unregister(pdev); |
1677 | ||
89211418 | 1678 | clk_disable_unprepare(clk_ahb); |
e3ec3a3d | 1679 | |
0c7fe32e | 1680 | return ret; |
e3ec3a3d SB |
1681 | } |
1682 | ||
e3ec3a3d SB |
1683 | static struct platform_driver sdhci_arasan_driver = { |
1684 | .driver = { | |
1685 | .name = "sdhci-arasan", | |
21b2cec6 | 1686 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
e3ec3a3d SB |
1687 | .of_match_table = sdhci_arasan_of_match, |
1688 | .pm = &sdhci_arasan_dev_pm_ops, | |
1689 | }, | |
1690 | .probe = sdhci_arasan_probe, | |
1691 | .remove = sdhci_arasan_remove, | |
1692 | }; | |
1693 | ||
1694 | module_platform_driver(sdhci_arasan_driver); | |
1695 | ||
1696 | MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller"); | |
1697 | MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>"); | |
1698 | MODULE_LICENSE("GPL"); |