Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-or-later
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>
16 */
17
18#include <linux/clk-provider.h>
19#include <linux/mfd/syscon.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/platform_device.h>
23#include <linux/phy/phy.h>
24#include <linux/regmap.h>
25#include <linux/reset.h>
26#include <linux/firmware/xlnx-zynqmp.h>
27
28#include "cqhci.h"
29#include "sdhci-cqhci.h"
30#include "sdhci-pltfm.h"
31
32#define SDHCI_ARASAN_VENDOR_REGISTER 0x78
33
34#define SDHCI_ARASAN_ITAPDLY_REGISTER 0xF0F8
35#define SDHCI_ARASAN_ITAPDLY_SEL_MASK 0xFF
36
37#define SDHCI_ARASAN_OTAPDLY_REGISTER 0xF0FC
38#define SDHCI_ARASAN_OTAPDLY_SEL_MASK 0x3F
39
40#define SDHCI_ARASAN_CQE_BASE_ADDR 0x200
41#define VENDOR_ENHANCED_STROBE BIT(0)
42
43#define PHY_CLK_TOO_SLOW_HZ 400000
44#define MIN_PHY_CLK_HZ 50000000
45
46#define SDHCI_ITAPDLY_CHGWIN 0x200
47#define SDHCI_ITAPDLY_ENABLE 0x100
48#define SDHCI_OTAPDLY_ENABLE 0x40
49
50#define PHY_CTRL_REG1 0x270
51#define PHY_CTRL_ITAPDLY_ENA_MASK BIT(0)
52#define PHY_CTRL_ITAPDLY_SEL_MASK GENMASK(5, 1)
53#define PHY_CTRL_ITAPDLY_SEL_SHIFT 1
54#define PHY_CTRL_ITAP_CHG_WIN_MASK BIT(6)
55#define PHY_CTRL_OTAPDLY_ENA_MASK BIT(8)
56#define PHY_CTRL_OTAPDLY_SEL_MASK GENMASK(15, 12)
57#define PHY_CTRL_OTAPDLY_SEL_SHIFT 12
58#define PHY_CTRL_STRB_SEL_MASK GENMASK(23, 16)
59#define PHY_CTRL_STRB_SEL_SHIFT 16
60#define PHY_CTRL_TEST_CTRL_MASK GENMASK(31, 24)
61
62#define PHY_CTRL_REG2 0x274
63#define PHY_CTRL_EN_DLL_MASK BIT(0)
64#define PHY_CTRL_DLL_RDY_MASK BIT(1)
65#define PHY_CTRL_FREQ_SEL_MASK GENMASK(6, 4)
66#define PHY_CTRL_FREQ_SEL_SHIFT 4
67#define PHY_CTRL_SEL_DLY_TX_MASK BIT(16)
68#define PHY_CTRL_SEL_DLY_RX_MASK BIT(17)
69#define FREQSEL_200M_170M 0x0
70#define FREQSEL_170M_140M 0x1
71#define FREQSEL_140M_110M 0x2
72#define FREQSEL_110M_80M 0x3
73#define FREQSEL_80M_50M 0x4
74#define FREQSEL_275M_250M 0x5
75#define FREQSEL_250M_225M 0x6
76#define FREQSEL_225M_200M 0x7
77#define PHY_DLL_TIMEOUT_MS 100
78
79#define SDHCI_HW_RST_EN BIT(4)
80
81/* Default settings for ZynqMP Clock Phases */
82#define ZYNQMP_ICLK_PHASE {0, 63, 63, 0, 63, 0, 0, 183, 54, 0, 0}
83#define ZYNQMP_OCLK_PHASE {0, 72, 60, 0, 60, 72, 135, 48, 72, 135, 0}
84
85#define VERSAL_ICLK_PHASE {0, 132, 132, 0, 132, 0, 0, 162, 90, 0, 0}
86#define VERSAL_OCLK_PHASE {0, 60, 48, 0, 48, 72, 90, 36, 60, 90, 0}
87
88#define VERSAL_NET_EMMC_ICLK_PHASE {0, 0, 0, 0, 0, 0, 0, 0, 39, 0, 0}
89#define VERSAL_NET_EMMC_OCLK_PHASE {0, 113, 0, 0, 0, 0, 0, 0, 113, 79, 45}
90
91#define VERSAL_NET_PHY_CTRL_STRB90_STRB180_VAL 0X77
92
93/*
94 * On some SoCs the syscon area has a feature where the upper 16-bits of
95 * each 32-bit register act as a write mask for the lower 16-bits. This allows
96 * atomic updates of the register without locking. This macro is used on SoCs
97 * that have that feature.
98 */
99#define HIWORD_UPDATE(val, mask, shift) \
100 ((val) << (shift) | (mask) << ((shift) + 16))
101
102#define CD_STABLE_TIMEOUT_US 1000000
103#define CD_STABLE_MAX_SLEEP_US 10
104
105/**
106 * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
107 *
108 * @reg: Offset within the syscon of the register containing this field
109 * @width: Number of bits for this field
110 * @shift: Bit offset within @reg of this field (or -1 if not avail)
111 */
112struct sdhci_arasan_soc_ctl_field {
113 u32 reg;
114 u16 width;
115 s16 shift;
116};
117
118/**
119 * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
120 *
121 * @baseclkfreq: Where to find corecfg_baseclkfreq
122 * @clockmultiplier: Where to find corecfg_clockmultiplier
123 * @support64b: Where to find SUPPORT64B bit
124 * @hiword_update: If true, use HIWORD_UPDATE to access the syscon
125 *
126 * It's up to the licensee of the Arsan IP block to make these available
127 * somewhere if needed. Presumably these will be scattered somewhere that's
128 * accessible via the syscon API.
129 */
130struct sdhci_arasan_soc_ctl_map {
131 struct sdhci_arasan_soc_ctl_field baseclkfreq;
132 struct sdhci_arasan_soc_ctl_field clockmultiplier;
133 struct sdhci_arasan_soc_ctl_field support64b;
134 bool hiword_update;
135};
136
137/**
138 * struct sdhci_arasan_clk_ops - Clock Operations for Arasan SD controller
139 *
140 * @sdcardclk_ops: The output clock related operations
141 * @sampleclk_ops: The sample clock related operations
142 */
143struct sdhci_arasan_clk_ops {
144 const struct clk_ops *sdcardclk_ops;
145 const struct clk_ops *sampleclk_ops;
146};
147
148/**
149 * struct sdhci_arasan_clk_data - Arasan Controller Clock Data.
150 *
151 * @sdcardclk_hw: Struct for the clock we might provide to a PHY.
152 * @sdcardclk: Pointer to normal 'struct clock' for sdcardclk_hw.
153 * @sampleclk_hw: Struct for the clock we might provide to a PHY.
154 * @sampleclk: Pointer to normal 'struct clock' for sampleclk_hw.
155 * @clk_phase_in: Array of Input Clock Phase Delays for all speed modes
156 * @clk_phase_out: Array of Output Clock Phase Delays for all speed modes
157 * @set_clk_delays: Function pointer for setting Clock Delays
158 * @clk_of_data: Platform specific runtime clock data storage pointer
159 */
160struct sdhci_arasan_clk_data {
161 struct clk_hw sdcardclk_hw;
162 struct clk *sdcardclk;
163 struct clk_hw sampleclk_hw;
164 struct clk *sampleclk;
165 int clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
166 int clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
167 void (*set_clk_delays)(struct sdhci_host *host);
168 void *clk_of_data;
169};
170
171/**
172 * struct sdhci_arasan_data - Arasan Controller Data
173 *
174 * @host: Pointer to the main SDHCI host structure.
175 * @clk_ahb: Pointer to the AHB clock
176 * @phy: Pointer to the generic phy
177 * @is_phy_on: True if the PHY is on; false if not.
178 * @internal_phy_reg: True if the PHY is within the Host controller.
179 * @has_cqe: True if controller has command queuing engine.
180 * @clk_data: Struct for the Arasan Controller Clock Data.
181 * @clk_ops: Struct for the Arasan Controller Clock Operations.
182 * @soc_ctl_base: Pointer to regmap for syscon for soc_ctl registers.
183 * @soc_ctl_map: Map to get offsets into soc_ctl registers.
184 * @quirks: Arasan deviations from spec.
185 */
186struct sdhci_arasan_data {
187 struct sdhci_host *host;
188 struct clk *clk_ahb;
189 struct phy *phy;
190 bool is_phy_on;
191 bool internal_phy_reg;
192
193 bool has_cqe;
194 struct sdhci_arasan_clk_data clk_data;
195 const struct sdhci_arasan_clk_ops *clk_ops;
196
197 struct regmap *soc_ctl_base;
198 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
199 unsigned int quirks;
200
201/* Controller does not have CD wired and will not function normally without */
202#define SDHCI_ARASAN_QUIRK_FORCE_CDTEST BIT(0)
203/* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the
204 * internal clock even when the clock isn't stable */
205#define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1)
206/*
207 * Some of the Arasan variations might not have timing requirements
208 * met at 25MHz for Default Speed mode, those controllers work at
209 * 19MHz instead
210 */
211#define SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN BIT(2)
212/* Enable CD stable check before power-up */
213#define SDHCI_ARASAN_QUIRK_ENSURE_CD_STABLE BIT(3)
214};
215
216struct sdhci_arasan_of_data {
217 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
218 const struct sdhci_pltfm_data *pdata;
219 const struct sdhci_arasan_clk_ops *clk_ops;
220 u32 quirks;
221};
222
223static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
224 .baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
225 .clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
226 .hiword_update = true,
227};
228
229static const struct sdhci_arasan_soc_ctl_map intel_lgm_emmc_soc_ctl_map = {
230 .baseclkfreq = { .reg = 0xa0, .width = 8, .shift = 2 },
231 .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
232 .hiword_update = false,
233};
234
235static const struct sdhci_arasan_soc_ctl_map intel_lgm_sdxc_soc_ctl_map = {
236 .baseclkfreq = { .reg = 0x80, .width = 8, .shift = 2 },
237 .clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
238 .hiword_update = false,
239};
240
241static const struct sdhci_arasan_soc_ctl_map intel_keembay_soc_ctl_map = {
242 .baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 },
243 .clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 },
244 .support64b = { .reg = 0x4, .width = 1, .shift = 24 },
245 .hiword_update = false,
246};
247
248static void sdhci_arasan_phy_set_delaychain(struct sdhci_host *host, bool enable)
249{
250 u32 reg;
251
252 reg = readl(host->ioaddr + PHY_CTRL_REG2);
253 if (enable)
254 reg |= (PHY_CTRL_SEL_DLY_TX_MASK | PHY_CTRL_SEL_DLY_RX_MASK);
255 else
256 reg &= ~(PHY_CTRL_SEL_DLY_TX_MASK | PHY_CTRL_SEL_DLY_RX_MASK);
257
258 writel(reg, host->ioaddr + PHY_CTRL_REG2);
259}
260
261static int sdhci_arasan_phy_set_dll(struct sdhci_host *host, bool enable)
262{
263 u32 reg;
264
265 reg = readl(host->ioaddr + PHY_CTRL_REG2);
266 if (enable)
267 reg |= PHY_CTRL_EN_DLL_MASK;
268 else
269 reg &= ~PHY_CTRL_EN_DLL_MASK;
270
271 writel(reg, host->ioaddr + PHY_CTRL_REG2);
272
273 if (!enable)
274 return 0;
275
276 return readl_relaxed_poll_timeout(host->ioaddr + PHY_CTRL_REG2, reg,
277 (reg & PHY_CTRL_DLL_RDY_MASK), 10,
278 1000 * PHY_DLL_TIMEOUT_MS);
279}
280
281static void sdhci_arasan_phy_dll_set_freq(struct sdhci_host *host, int clock)
282{
283 u32 reg, freq_sel, freq;
284
285 freq = DIV_ROUND_CLOSEST(clock, 1000000);
286 if (freq <= 200 && freq > 170)
287 freq_sel = FREQSEL_200M_170M;
288 else if (freq <= 170 && freq > 140)
289 freq_sel = FREQSEL_170M_140M;
290 else if (freq <= 140 && freq > 110)
291 freq_sel = FREQSEL_140M_110M;
292 else if (freq <= 110 && freq > 80)
293 freq_sel = FREQSEL_110M_80M;
294 else
295 freq_sel = FREQSEL_80M_50M;
296
297 reg = readl(host->ioaddr + PHY_CTRL_REG2);
298 reg &= ~PHY_CTRL_FREQ_SEL_MASK;
299 reg |= (freq_sel << PHY_CTRL_FREQ_SEL_SHIFT);
300 writel(reg, host->ioaddr + PHY_CTRL_REG2);
301}
302
303/**
304 * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
305 *
306 * @host: The sdhci_host
307 * @fld: The field to write to
308 * @val: The value to write
309 *
310 * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
311 * Note that if a field is specified as not available (shift < 0) then
312 * this function will silently return an error code. It will be noisy
313 * and print errors for any other (unexpected) errors.
314 *
315 * Return: 0 on success and error value on error
316 */
317static int sdhci_arasan_syscon_write(struct sdhci_host *host,
318 const struct sdhci_arasan_soc_ctl_field *fld,
319 u32 val)
320{
321 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
322 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
323 struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
324 u32 reg = fld->reg;
325 u16 width = fld->width;
326 s16 shift = fld->shift;
327 int ret;
328
329 /*
330 * Silently return errors for shift < 0 so caller doesn't have
331 * to check for fields which are optional. For fields that
332 * are required then caller needs to do something special
333 * anyway.
334 */
335 if (shift < 0)
336 return -EINVAL;
337
338 if (sdhci_arasan->soc_ctl_map->hiword_update)
339 ret = regmap_write(soc_ctl_base, reg,
340 HIWORD_UPDATE(val, GENMASK(width, 0),
341 shift));
342 else
343 ret = regmap_update_bits(soc_ctl_base, reg,
344 GENMASK(shift + width, shift),
345 val << shift);
346
347 /* Yell about (unexpected) regmap errors */
348 if (ret)
349 pr_warn("%s: Regmap write fail: %d\n",
350 mmc_hostname(host->mmc), ret);
351
352 return ret;
353}
354
355static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
356{
357 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
358 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
359 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
360 bool ctrl_phy = false;
361
362 if (!IS_ERR(sdhci_arasan->phy)) {
363 if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
364 /*
365 * If PHY off, set clock to max speed and power PHY on.
366 *
367 * Although PHY docs apparently suggest power cycling
368 * when changing the clock the PHY doesn't like to be
369 * powered on while at low speeds like those used in ID
370 * mode. Even worse is powering the PHY on while the
371 * clock is off.
372 *
373 * To workaround the PHY limitations, the best we can
374 * do is to power it on at a faster speed and then slam
375 * through low speeds without power cycling.
376 */
377 sdhci_set_clock(host, host->max_clk);
378 if (phy_power_on(sdhci_arasan->phy)) {
379 pr_err("%s: Cannot power on phy.\n",
380 mmc_hostname(host->mmc));
381 return;
382 }
383
384 sdhci_arasan->is_phy_on = true;
385
386 /*
387 * We'll now fall through to the below case with
388 * ctrl_phy = false (so we won't turn off/on). The
389 * sdhci_set_clock() will set the real clock.
390 */
391 } else if (clock > PHY_CLK_TOO_SLOW_HZ) {
392 /*
393 * At higher clock speeds the PHY is fine being power
394 * cycled and docs say you _should_ power cycle when
395 * changing clock speeds.
396 */
397 ctrl_phy = true;
398 }
399 }
400
401 if (ctrl_phy && sdhci_arasan->is_phy_on) {
402 phy_power_off(sdhci_arasan->phy);
403 sdhci_arasan->is_phy_on = false;
404 }
405
406 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN) {
407 /*
408 * Some of the Arasan variations might not have timing
409 * requirements met at 25MHz for Default Speed mode,
410 * those controllers work at 19MHz instead.
411 */
412 if (clock == DEFAULT_SPEED_MAX_DTR)
413 clock = (DEFAULT_SPEED_MAX_DTR * 19) / 25;
414 }
415
416 /* Set the Input and Output Clock Phase Delays */
417 if (clk_data->set_clk_delays && clock > PHY_CLK_TOO_SLOW_HZ)
418 clk_data->set_clk_delays(host);
419
420 if (sdhci_arasan->internal_phy_reg && clock >= MIN_PHY_CLK_HZ) {
421 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
422 sdhci_arasan_phy_set_dll(host, 0);
423 sdhci_arasan_phy_set_delaychain(host, 0);
424 sdhci_arasan_phy_dll_set_freq(host, clock);
425 } else if (sdhci_arasan->internal_phy_reg) {
426 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
427 sdhci_arasan_phy_set_delaychain(host, 1);
428 }
429
430 sdhci_set_clock(host, clock);
431
432 if (sdhci_arasan->internal_phy_reg && clock >= MIN_PHY_CLK_HZ)
433 sdhci_arasan_phy_set_dll(host, 1);
434
435 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
436 /*
437 * Some controllers immediately report SDHCI_CLOCK_INT_STABLE
438 * after enabling the clock even though the clock is not
439 * stable. Trying to use a clock without waiting here results
440 * in EILSEQ while detecting some older/slower cards. The
441 * chosen delay is the maximum delay from sdhci_set_clock.
442 */
443 msleep(20);
444
445 if (ctrl_phy) {
446 if (phy_power_on(sdhci_arasan->phy)) {
447 pr_err("%s: Cannot power on phy.\n",
448 mmc_hostname(host->mmc));
449 return;
450 }
451
452 sdhci_arasan->is_phy_on = true;
453 }
454}
455
456static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
457 struct mmc_ios *ios)
458{
459 u32 vendor;
460 struct sdhci_host *host = mmc_priv(mmc);
461
462 vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
463 if (ios->enhanced_strobe)
464 vendor |= VENDOR_ENHANCED_STROBE;
465 else
466 vendor &= ~VENDOR_ENHANCED_STROBE;
467
468 sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
469}
470
471static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
472{
473 u8 ctrl;
474 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
475 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
476
477 sdhci_and_cqhci_reset(host, mask);
478
479 if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
480 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
481 ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
482 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
483 }
484}
485
486static void sdhci_arasan_hw_reset(struct sdhci_host *host)
487{
488 u8 reg;
489
490 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
491 reg |= SDHCI_HW_RST_EN;
492 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
493 /* As per eMMC spec, minimum 1us is required but give it 2us for good measure */
494 usleep_range(2, 5);
495 reg &= ~SDHCI_HW_RST_EN;
496 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
497 /* As per eMMC spec, minimum 200us is required but give it 300us for good measure */
498 usleep_range(300, 500);
499}
500
501static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
502 struct mmc_ios *ios)
503{
504 switch (ios->signal_voltage) {
505 case MMC_SIGNAL_VOLTAGE_180:
506 /*
507 * Plese don't switch to 1V8 as arasan,5.1 doesn't
508 * actually refer to this setting to indicate the
509 * signal voltage and the state machine will be broken
510 * actually if we force to enable 1V8. That's something
511 * like broken quirk but we could work around here.
512 */
513 return 0;
514 case MMC_SIGNAL_VOLTAGE_330:
515 case MMC_SIGNAL_VOLTAGE_120:
516 /* We don't support 3V3 and 1V2 */
517 break;
518 }
519
520 return -EINVAL;
521}
522
523static void sdhci_arasan_set_power_and_bus_voltage(struct sdhci_host *host, unsigned char mode,
524 unsigned short vdd)
525{
526 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
527 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
528 u32 reg;
529
530 /*
531 * Ensure that the card detect logic has stabilized before powering up, this is
532 * necessary after a host controller reset.
533 */
534 if (mode == MMC_POWER_UP && sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_ENSURE_CD_STABLE)
535 read_poll_timeout(sdhci_readl, reg, reg & SDHCI_CD_STABLE, CD_STABLE_MAX_SLEEP_US,
536 CD_STABLE_TIMEOUT_US, false, host, SDHCI_PRESENT_STATE);
537
538 sdhci_set_power_and_bus_voltage(host, mode, vdd);
539}
540
541static const struct sdhci_ops sdhci_arasan_ops = {
542 .set_clock = sdhci_arasan_set_clock,
543 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
544 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
545 .set_bus_width = sdhci_set_bus_width,
546 .reset = sdhci_arasan_reset,
547 .set_uhs_signaling = sdhci_set_uhs_signaling,
548 .set_power = sdhci_arasan_set_power_and_bus_voltage,
549 .hw_reset = sdhci_arasan_hw_reset,
550};
551
552static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
553{
554 int cmd_error = 0;
555 int data_error = 0;
556
557 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
558 return intmask;
559
560 cqhci_irq(host->mmc, intmask, cmd_error, data_error);
561
562 return 0;
563}
564
565static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
566{
567 sdhci_dumpregs(mmc_priv(mmc));
568}
569
570static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
571{
572 struct sdhci_host *host = mmc_priv(mmc);
573 u32 reg;
574
575 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
576 while (reg & SDHCI_DATA_AVAILABLE) {
577 sdhci_readl(host, SDHCI_BUFFER);
578 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
579 }
580
581 sdhci_cqe_enable(mmc);
582}
583
584static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
585 .enable = sdhci_arasan_cqe_enable,
586 .disable = sdhci_cqe_disable,
587 .dumpregs = sdhci_arasan_dumpregs,
588};
589
590static const struct sdhci_ops sdhci_arasan_cqe_ops = {
591 .set_clock = sdhci_arasan_set_clock,
592 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
593 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
594 .set_bus_width = sdhci_set_bus_width,
595 .reset = sdhci_arasan_reset,
596 .set_uhs_signaling = sdhci_set_uhs_signaling,
597 .set_power = sdhci_arasan_set_power_and_bus_voltage,
598 .irq = sdhci_arasan_cqhci_irq,
599};
600
601static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
602 .ops = &sdhci_arasan_cqe_ops,
603 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
604 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
605 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
606};
607
608#ifdef CONFIG_PM_SLEEP
609/**
610 * sdhci_arasan_suspend - Suspend method for the driver
611 * @dev: Address of the device structure
612 *
613 * Put the device in a low power state.
614 *
615 * Return: 0 on success and error value on error
616 */
617static int sdhci_arasan_suspend(struct device *dev)
618{
619 struct sdhci_host *host = dev_get_drvdata(dev);
620 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
621 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
622 int ret;
623
624 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
625 mmc_retune_needed(host->mmc);
626
627 if (sdhci_arasan->has_cqe) {
628 ret = cqhci_suspend(host->mmc);
629 if (ret)
630 return ret;
631 }
632
633 ret = sdhci_suspend_host(host);
634 if (ret)
635 return ret;
636
637 if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
638 ret = phy_power_off(sdhci_arasan->phy);
639 if (ret) {
640 dev_err(dev, "Cannot power off phy.\n");
641 if (sdhci_resume_host(host))
642 dev_err(dev, "Cannot resume host.\n");
643
644 return ret;
645 }
646 sdhci_arasan->is_phy_on = false;
647 }
648
649 clk_disable(pltfm_host->clk);
650 clk_disable(sdhci_arasan->clk_ahb);
651
652 return 0;
653}
654
655/**
656 * sdhci_arasan_resume - Resume method for the driver
657 * @dev: Address of the device structure
658 *
659 * Resume operation after suspend
660 *
661 * Return: 0 on success and error value on error
662 */
663static int sdhci_arasan_resume(struct device *dev)
664{
665 struct sdhci_host *host = dev_get_drvdata(dev);
666 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
667 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
668 int ret;
669
670 ret = clk_enable(sdhci_arasan->clk_ahb);
671 if (ret) {
672 dev_err(dev, "Cannot enable AHB clock.\n");
673 return ret;
674 }
675
676 ret = clk_enable(pltfm_host->clk);
677 if (ret) {
678 dev_err(dev, "Cannot enable SD clock.\n");
679 return ret;
680 }
681
682 if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
683 ret = phy_power_on(sdhci_arasan->phy);
684 if (ret) {
685 dev_err(dev, "Cannot power on phy.\n");
686 return ret;
687 }
688 sdhci_arasan->is_phy_on = true;
689 }
690
691 ret = sdhci_resume_host(host);
692 if (ret) {
693 dev_err(dev, "Cannot resume host.\n");
694 return ret;
695 }
696
697 if (sdhci_arasan->has_cqe)
698 return cqhci_resume(host->mmc);
699
700 return 0;
701}
702#endif /* ! CONFIG_PM_SLEEP */
703
704static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
705 sdhci_arasan_resume);
706
707/**
708 * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
709 *
710 * @hw: Pointer to the hardware clock structure.
711 * @parent_rate: The parent rate (should be rate of clk_xin).
712 *
713 * Return the current actual rate of the SD card clock. This can be used
714 * to communicate with out PHY.
715 *
716 * Return: The card clock rate.
717 */
718static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
719 unsigned long parent_rate)
720{
721 struct sdhci_arasan_clk_data *clk_data =
722 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
723 struct sdhci_arasan_data *sdhci_arasan =
724 container_of(clk_data, struct sdhci_arasan_data, clk_data);
725 struct sdhci_host *host = sdhci_arasan->host;
726
727 return host->mmc->actual_clock;
728}
729
730static const struct clk_ops arasan_sdcardclk_ops = {
731 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
732};
733
734/**
735 * sdhci_arasan_sampleclk_recalc_rate - Return the sampling clock rate
736 *
737 * @hw: Pointer to the hardware clock structure.
738 * @parent_rate: The parent rate (should be rate of clk_xin).
739 *
740 * Return the current actual rate of the sampling clock. This can be used
741 * to communicate with out PHY.
742 *
743 * Return: The sample clock rate.
744 */
745static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw,
746 unsigned long parent_rate)
747{
748 struct sdhci_arasan_clk_data *clk_data =
749 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
750 struct sdhci_arasan_data *sdhci_arasan =
751 container_of(clk_data, struct sdhci_arasan_data, clk_data);
752 struct sdhci_host *host = sdhci_arasan->host;
753
754 return host->mmc->actual_clock;
755}
756
757static const struct clk_ops arasan_sampleclk_ops = {
758 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
759};
760
761/**
762 * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
763 *
764 * @hw: Pointer to the hardware clock structure.
765 * @degrees: The clock phase shift between 0 - 359.
766 *
767 * Set the SD Output Clock Tap Delays for Output path
768 *
769 * Return: 0 on success and error value on error
770 */
771static int sdhci_zynqmp_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
772{
773 struct sdhci_arasan_clk_data *clk_data =
774 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
775 struct sdhci_arasan_data *sdhci_arasan =
776 container_of(clk_data, struct sdhci_arasan_data, clk_data);
777 struct sdhci_host *host = sdhci_arasan->host;
778 const char *clk_name = clk_hw_get_name(hw);
779 u32 node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1;
780 u8 tap_delay, tap_max = 0;
781 int ret;
782
783 /* This is applicable for SDHCI_SPEC_300 and above */
784 if (host->version < SDHCI_SPEC_300)
785 return 0;
786
787 switch (host->timing) {
788 case MMC_TIMING_MMC_HS:
789 case MMC_TIMING_SD_HS:
790 case MMC_TIMING_UHS_SDR25:
791 case MMC_TIMING_UHS_DDR50:
792 case MMC_TIMING_MMC_DDR52:
793 /* For 50MHz clock, 30 Taps are available */
794 tap_max = 30;
795 break;
796 case MMC_TIMING_UHS_SDR50:
797 /* For 100MHz clock, 15 Taps are available */
798 tap_max = 15;
799 break;
800 case MMC_TIMING_UHS_SDR104:
801 case MMC_TIMING_MMC_HS200:
802 /* For 200MHz clock, 8 Taps are available */
803 tap_max = 8;
804 break;
805 default:
806 break;
807 }
808
809 tap_delay = (degrees * tap_max) / 360;
810
811 /* Set the Clock Phase */
812 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay);
813 if (ret)
814 pr_err("Error setting Output Tap Delay\n");
815
816 /* Release DLL Reset */
817 zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_RELEASE);
818
819 return ret;
820}
821
822static const struct clk_ops zynqmp_sdcardclk_ops = {
823 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
824 .set_phase = sdhci_zynqmp_sdcardclk_set_phase,
825};
826
827/**
828 * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
829 *
830 * @hw: Pointer to the hardware clock structure.
831 * @degrees: The clock phase shift between 0 - 359.
832 *
833 * Set the SD Input Clock Tap Delays for Input path
834 *
835 * Return: 0 on success and error value on error
836 */
837static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees)
838{
839 struct sdhci_arasan_clk_data *clk_data =
840 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
841 struct sdhci_arasan_data *sdhci_arasan =
842 container_of(clk_data, struct sdhci_arasan_data, clk_data);
843 struct sdhci_host *host = sdhci_arasan->host;
844 const char *clk_name = clk_hw_get_name(hw);
845 u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1;
846 u8 tap_delay, tap_max = 0;
847 int ret;
848
849 /* This is applicable for SDHCI_SPEC_300 and above */
850 if (host->version < SDHCI_SPEC_300)
851 return 0;
852
853 /* Assert DLL Reset */
854 zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_ASSERT);
855
856 switch (host->timing) {
857 case MMC_TIMING_MMC_HS:
858 case MMC_TIMING_SD_HS:
859 case MMC_TIMING_UHS_SDR25:
860 case MMC_TIMING_UHS_DDR50:
861 case MMC_TIMING_MMC_DDR52:
862 /* For 50MHz clock, 120 Taps are available */
863 tap_max = 120;
864 break;
865 case MMC_TIMING_UHS_SDR50:
866 /* For 100MHz clock, 60 Taps are available */
867 tap_max = 60;
868 break;
869 case MMC_TIMING_UHS_SDR104:
870 case MMC_TIMING_MMC_HS200:
871 /* For 200MHz clock, 30 Taps are available */
872 tap_max = 30;
873 break;
874 default:
875 break;
876 }
877
878 tap_delay = (degrees * tap_max) / 360;
879
880 /* Set the Clock Phase */
881 ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay);
882 if (ret)
883 pr_err("Error setting Input Tap Delay\n");
884
885 return ret;
886}
887
888static const struct clk_ops zynqmp_sampleclk_ops = {
889 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
890 .set_phase = sdhci_zynqmp_sampleclk_set_phase,
891};
892
893/**
894 * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
895 *
896 * @hw: Pointer to the hardware clock structure.
897 * @degrees: The clock phase shift between 0 - 359.
898 *
899 * Set the SD Output Clock Tap Delays for Output path
900 *
901 * Return: 0 on success and error value on error
902 */
903static int sdhci_versal_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
904{
905 struct sdhci_arasan_clk_data *clk_data =
906 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
907 struct sdhci_arasan_data *sdhci_arasan =
908 container_of(clk_data, struct sdhci_arasan_data, clk_data);
909 struct sdhci_host *host = sdhci_arasan->host;
910 u8 tap_delay, tap_max = 0;
911
912 /* This is applicable for SDHCI_SPEC_300 and above */
913 if (host->version < SDHCI_SPEC_300)
914 return 0;
915
916 switch (host->timing) {
917 case MMC_TIMING_MMC_HS:
918 case MMC_TIMING_SD_HS:
919 case MMC_TIMING_UHS_SDR25:
920 case MMC_TIMING_UHS_DDR50:
921 case MMC_TIMING_MMC_DDR52:
922 /* For 50MHz clock, 30 Taps are available */
923 tap_max = 30;
924 break;
925 case MMC_TIMING_UHS_SDR50:
926 /* For 100MHz clock, 15 Taps are available */
927 tap_max = 15;
928 break;
929 case MMC_TIMING_UHS_SDR104:
930 case MMC_TIMING_MMC_HS200:
931 /* For 200MHz clock, 8 Taps are available */
932 tap_max = 8;
933 break;
934 default:
935 break;
936 }
937
938 tap_delay = (degrees * tap_max) / 360;
939
940 /* Set the Clock Phase */
941 if (tap_delay) {
942 u32 regval;
943
944 regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
945 regval |= SDHCI_OTAPDLY_ENABLE;
946 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
947 regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
948 regval |= tap_delay;
949 sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
950 }
951
952 return 0;
953}
954
955static const struct clk_ops versal_sdcardclk_ops = {
956 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
957 .set_phase = sdhci_versal_sdcardclk_set_phase,
958};
959
960/**
961 * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
962 *
963 * @hw: Pointer to the hardware clock structure.
964 * @degrees: The clock phase shift between 0 - 359.
965 *
966 * Set the SD Input Clock Tap Delays for Input path
967 *
968 * Return: 0 on success and error value on error
969 */
970static int sdhci_versal_sampleclk_set_phase(struct clk_hw *hw, int degrees)
971{
972 struct sdhci_arasan_clk_data *clk_data =
973 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
974 struct sdhci_arasan_data *sdhci_arasan =
975 container_of(clk_data, struct sdhci_arasan_data, clk_data);
976 struct sdhci_host *host = sdhci_arasan->host;
977 u8 tap_delay, tap_max = 0;
978
979 /* This is applicable for SDHCI_SPEC_300 and above */
980 if (host->version < SDHCI_SPEC_300)
981 return 0;
982
983 switch (host->timing) {
984 case MMC_TIMING_MMC_HS:
985 case MMC_TIMING_SD_HS:
986 case MMC_TIMING_UHS_SDR25:
987 case MMC_TIMING_UHS_DDR50:
988 case MMC_TIMING_MMC_DDR52:
989 /* For 50MHz clock, 120 Taps are available */
990 tap_max = 120;
991 break;
992 case MMC_TIMING_UHS_SDR50:
993 /* For 100MHz clock, 60 Taps are available */
994 tap_max = 60;
995 break;
996 case MMC_TIMING_UHS_SDR104:
997 case MMC_TIMING_MMC_HS200:
998 /* For 200MHz clock, 30 Taps are available */
999 tap_max = 30;
1000 break;
1001 default:
1002 break;
1003 }
1004
1005 tap_delay = (degrees * tap_max) / 360;
1006
1007 /* Set the Clock Phase */
1008 if (tap_delay) {
1009 u32 regval;
1010
1011 regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
1012 regval |= SDHCI_ITAPDLY_CHGWIN;
1013 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
1014 regval |= SDHCI_ITAPDLY_ENABLE;
1015 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
1016 regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
1017 regval |= tap_delay;
1018 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
1019 regval &= ~SDHCI_ITAPDLY_CHGWIN;
1020 sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
1021 }
1022
1023 return 0;
1024}
1025
1026static const struct clk_ops versal_sampleclk_ops = {
1027 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
1028 .set_phase = sdhci_versal_sampleclk_set_phase,
1029};
1030
1031static int sdhci_versal_net_emmc_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
1032{
1033 struct sdhci_arasan_clk_data *clk_data =
1034 container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
1035 struct sdhci_arasan_data *sdhci_arasan =
1036 container_of(clk_data, struct sdhci_arasan_data, clk_data);
1037 struct sdhci_host *host = sdhci_arasan->host;
1038 u8 tap_delay, tap_max = 0;
1039
1040 switch (host->timing) {
1041 case MMC_TIMING_MMC_HS:
1042 case MMC_TIMING_MMC_DDR52:
1043 tap_max = 16;
1044 break;
1045 case MMC_TIMING_MMC_HS200:
1046 case MMC_TIMING_MMC_HS400:
1047 /* For 200MHz clock, 32 Taps are available */
1048 tap_max = 32;
1049 break;
1050 default:
1051 break;
1052 }
1053
1054 tap_delay = (degrees * tap_max) / 360;
1055
1056 /* Set the Clock Phase */
1057 if (tap_delay) {
1058 u32 regval;
1059
1060 regval = sdhci_readl(host, PHY_CTRL_REG1);
1061 regval |= PHY_CTRL_OTAPDLY_ENA_MASK;
1062 sdhci_writel(host, regval, PHY_CTRL_REG1);
1063 regval &= ~PHY_CTRL_OTAPDLY_SEL_MASK;
1064 regval |= tap_delay << PHY_CTRL_OTAPDLY_SEL_SHIFT;
1065 sdhci_writel(host, regval, PHY_CTRL_REG1);
1066 }
1067
1068 return 0;
1069}
1070
1071static const struct clk_ops versal_net_sdcardclk_ops = {
1072 .recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
1073 .set_phase = sdhci_versal_net_emmc_sdcardclk_set_phase,
1074};
1075
1076static int sdhci_versal_net_emmc_sampleclk_set_phase(struct clk_hw *hw, int degrees)
1077{
1078 struct sdhci_arasan_clk_data *clk_data =
1079 container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
1080 struct sdhci_arasan_data *sdhci_arasan =
1081 container_of(clk_data, struct sdhci_arasan_data, clk_data);
1082 struct sdhci_host *host = sdhci_arasan->host;
1083 u8 tap_delay, tap_max = 0;
1084 u32 regval;
1085
1086 switch (host->timing) {
1087 case MMC_TIMING_MMC_HS:
1088 case MMC_TIMING_MMC_DDR52:
1089 tap_max = 32;
1090 break;
1091 case MMC_TIMING_MMC_HS400:
1092 /* Strobe select tap point for strb90 and strb180 */
1093 regval = sdhci_readl(host, PHY_CTRL_REG1);
1094 regval &= ~PHY_CTRL_STRB_SEL_MASK;
1095 regval |= VERSAL_NET_PHY_CTRL_STRB90_STRB180_VAL << PHY_CTRL_STRB_SEL_SHIFT;
1096 sdhci_writel(host, regval, PHY_CTRL_REG1);
1097 break;
1098 default:
1099 break;
1100 }
1101
1102 tap_delay = (degrees * tap_max) / 360;
1103
1104 /* Set the Clock Phase */
1105 if (tap_delay) {
1106 regval = sdhci_readl(host, PHY_CTRL_REG1);
1107 regval |= PHY_CTRL_ITAP_CHG_WIN_MASK;
1108 sdhci_writel(host, regval, PHY_CTRL_REG1);
1109 regval |= PHY_CTRL_ITAPDLY_ENA_MASK;
1110 sdhci_writel(host, regval, PHY_CTRL_REG1);
1111 regval &= ~PHY_CTRL_ITAPDLY_SEL_MASK;
1112 regval |= tap_delay << PHY_CTRL_ITAPDLY_SEL_SHIFT;
1113 sdhci_writel(host, regval, PHY_CTRL_REG1);
1114 regval &= ~PHY_CTRL_ITAP_CHG_WIN_MASK;
1115 sdhci_writel(host, regval, PHY_CTRL_REG1);
1116 }
1117
1118 return 0;
1119}
1120
1121static const struct clk_ops versal_net_sampleclk_ops = {
1122 .recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
1123 .set_phase = sdhci_versal_net_emmc_sampleclk_set_phase,
1124};
1125
1126static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid)
1127{
1128 u16 clk;
1129
1130 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1131 clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN);
1132 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1133
1134 /* Issue DLL Reset */
1135 zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE);
1136
1137 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1138
1139 sdhci_enable_clk(host, clk);
1140}
1141
1142static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode)
1143{
1144 struct sdhci_host *host = mmc_priv(mmc);
1145 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1146 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1147 struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw;
1148 const char *clk_name = clk_hw_get_name(hw);
1149 u32 device_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 :
1150 NODE_SD_1;
1151 int err;
1152
1153 /* ZynqMP SD controller does not perform auto tuning in DDR50 mode */
1154 if (mmc->ios.timing == MMC_TIMING_UHS_DDR50)
1155 return 0;
1156
1157 arasan_zynqmp_dll_reset(host, device_id);
1158
1159 err = sdhci_execute_tuning(mmc, opcode);
1160 if (err)
1161 return err;
1162
1163 arasan_zynqmp_dll_reset(host, device_id);
1164
1165 return 0;
1166}
1167
1168/**
1169 * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
1170 *
1171 * @host: The sdhci_host
1172 * @value: The value to write
1173 *
1174 * The corecfg_clockmultiplier is supposed to contain clock multiplier
1175 * value of programmable clock generator.
1176 *
1177 * NOTES:
1178 * - Many existing devices don't seem to do this and work fine. To keep
1179 * compatibility for old hardware where the device tree doesn't provide a
1180 * register map, this function is a noop if a soc_ctl_map hasn't been provided
1181 * for this platform.
1182 * - The value of corecfg_clockmultiplier should sync with that of corresponding
1183 * value reading from sdhci_capability_register. So this function is called
1184 * once at probe time and never called again.
1185 */
1186static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
1187 u32 value)
1188{
1189 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1190 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1191 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
1192 sdhci_arasan->soc_ctl_map;
1193
1194 /* Having a map is optional */
1195 if (!soc_ctl_map)
1196 return;
1197
1198 /* If we have a map, we expect to have a syscon */
1199 if (!sdhci_arasan->soc_ctl_base) {
1200 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1201 mmc_hostname(host->mmc));
1202 return;
1203 }
1204
1205 sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
1206}
1207
1208/**
1209 * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
1210 *
1211 * @host: The sdhci_host
1212 *
1213 * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin. This
1214 * function can be used to make that happen.
1215 *
1216 * NOTES:
1217 * - Many existing devices don't seem to do this and work fine. To keep
1218 * compatibility for old hardware where the device tree doesn't provide a
1219 * register map, this function is a noop if a soc_ctl_map hasn't been provided
1220 * for this platform.
1221 * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
1222 * to achieve lower clock rates. That means that this function is called once
1223 * at probe time and never called again.
1224 */
1225static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
1226{
1227 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1228 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1229 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
1230 sdhci_arasan->soc_ctl_map;
1231 u32 mhz = DIV_ROUND_CLOSEST_ULL(clk_get_rate(pltfm_host->clk), 1000000);
1232
1233 /* Having a map is optional */
1234 if (!soc_ctl_map)
1235 return;
1236
1237 /* If we have a map, we expect to have a syscon */
1238 if (!sdhci_arasan->soc_ctl_base) {
1239 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1240 mmc_hostname(host->mmc));
1241 return;
1242 }
1243
1244 sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
1245}
1246
1247static void sdhci_arasan_set_clk_delays(struct sdhci_host *host)
1248{
1249 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1250 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1251 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1252
1253 clk_set_phase(clk_data->sampleclk,
1254 clk_data->clk_phase_in[host->timing]);
1255 clk_set_phase(clk_data->sdcardclk,
1256 clk_data->clk_phase_out[host->timing]);
1257}
1258
1259static void arasan_dt_read_clk_phase(struct device *dev,
1260 struct sdhci_arasan_clk_data *clk_data,
1261 unsigned int timing, const char *prop)
1262{
1263 struct device_node *np = dev->of_node;
1264
1265 u32 clk_phase[2] = {0};
1266 int ret;
1267
1268 /*
1269 * Read Tap Delay values from DT, if the DT does not contain the
1270 * Tap Values then use the pre-defined values.
1271 */
1272 ret = of_property_read_variable_u32_array(np, prop, &clk_phase[0],
1273 2, 0);
1274 if (ret < 0) {
1275 dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
1276 prop, clk_data->clk_phase_in[timing],
1277 clk_data->clk_phase_out[timing]);
1278 return;
1279 }
1280
1281 /* The values read are Input and Output Clock Delays in order */
1282 clk_data->clk_phase_in[timing] = clk_phase[0];
1283 clk_data->clk_phase_out[timing] = clk_phase[1];
1284}
1285
1286/**
1287 * arasan_dt_parse_clk_phases - Read Clock Delay values from DT
1288 *
1289 * @dev: Pointer to our struct device.
1290 * @clk_data: Pointer to the Clock Data structure
1291 *
1292 * Called at initialization to parse the values of Clock Delays.
1293 */
1294static void arasan_dt_parse_clk_phases(struct device *dev,
1295 struct sdhci_arasan_clk_data *clk_data)
1296{
1297 u32 mio_bank = 0;
1298 int i;
1299
1300 /*
1301 * This has been kept as a pointer and is assigned a function here.
1302 * So that different controller variants can assign their own handling
1303 * function.
1304 */
1305 clk_data->set_clk_delays = sdhci_arasan_set_clk_delays;
1306
1307 if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) {
1308 u32 zynqmp_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1309 ZYNQMP_ICLK_PHASE;
1310 u32 zynqmp_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1311 ZYNQMP_OCLK_PHASE;
1312
1313 of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank);
1314 if (mio_bank == 2) {
1315 zynqmp_oclk_phase[MMC_TIMING_UHS_SDR104] = 90;
1316 zynqmp_oclk_phase[MMC_TIMING_MMC_HS200] = 90;
1317 }
1318
1319 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1320 clk_data->clk_phase_in[i] = zynqmp_iclk_phase[i];
1321 clk_data->clk_phase_out[i] = zynqmp_oclk_phase[i];
1322 }
1323 }
1324
1325 if (of_device_is_compatible(dev->of_node, "xlnx,versal-8.9a")) {
1326 u32 versal_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1327 VERSAL_ICLK_PHASE;
1328 u32 versal_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1329 VERSAL_OCLK_PHASE;
1330
1331 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1332 clk_data->clk_phase_in[i] = versal_iclk_phase[i];
1333 clk_data->clk_phase_out[i] = versal_oclk_phase[i];
1334 }
1335 }
1336 if (of_device_is_compatible(dev->of_node, "xlnx,versal-net-emmc")) {
1337 u32 versal_net_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1338 VERSAL_NET_EMMC_ICLK_PHASE;
1339 u32 versal_net_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1340 VERSAL_NET_EMMC_OCLK_PHASE;
1341
1342 for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1343 clk_data->clk_phase_in[i] = versal_net_iclk_phase[i];
1344 clk_data->clk_phase_out[i] = versal_net_oclk_phase[i];
1345 }
1346 }
1347 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY,
1348 "clk-phase-legacy");
1349 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS,
1350 "clk-phase-mmc-hs");
1351 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS,
1352 "clk-phase-sd-hs");
1353 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12,
1354 "clk-phase-uhs-sdr12");
1355 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25,
1356 "clk-phase-uhs-sdr25");
1357 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50,
1358 "clk-phase-uhs-sdr50");
1359 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104,
1360 "clk-phase-uhs-sdr104");
1361 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50,
1362 "clk-phase-uhs-ddr50");
1363 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52,
1364 "clk-phase-mmc-ddr52");
1365 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200,
1366 "clk-phase-mmc-hs200");
1367 arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400,
1368 "clk-phase-mmc-hs400");
1369}
1370
1371static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
1372 .ops = &sdhci_arasan_ops,
1373 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1374 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1375 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1376 SDHCI_QUIRK2_STOP_WITH_TC,
1377};
1378
1379static const struct sdhci_arasan_clk_ops arasan_clk_ops = {
1380 .sdcardclk_ops = &arasan_sdcardclk_ops,
1381 .sampleclk_ops = &arasan_sampleclk_ops,
1382};
1383
1384static struct sdhci_arasan_of_data sdhci_arasan_generic_data = {
1385 .pdata = &sdhci_arasan_pdata,
1386 .clk_ops = &arasan_clk_ops,
1387};
1388
1389static const struct sdhci_pltfm_data sdhci_keembay_emmc_pdata = {
1390 .ops = &sdhci_arasan_cqe_ops,
1391 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1392 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1393 SDHCI_QUIRK_NO_LED |
1394 SDHCI_QUIRK_32BIT_DMA_ADDR |
1395 SDHCI_QUIRK_32BIT_DMA_SIZE |
1396 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1397 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1398 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1399 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
1400 SDHCI_QUIRK2_STOP_WITH_TC |
1401 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1402};
1403
1404static const struct sdhci_pltfm_data sdhci_keembay_sd_pdata = {
1405 .ops = &sdhci_arasan_ops,
1406 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1407 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1408 SDHCI_QUIRK_NO_LED |
1409 SDHCI_QUIRK_32BIT_DMA_ADDR |
1410 SDHCI_QUIRK_32BIT_DMA_SIZE |
1411 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1412 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1413 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1414 SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
1415 SDHCI_QUIRK2_STOP_WITH_TC |
1416 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1417};
1418
1419static const struct sdhci_pltfm_data sdhci_keembay_sdio_pdata = {
1420 .ops = &sdhci_arasan_ops,
1421 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1422 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1423 SDHCI_QUIRK_NO_LED |
1424 SDHCI_QUIRK_32BIT_DMA_ADDR |
1425 SDHCI_QUIRK_32BIT_DMA_SIZE |
1426 SDHCI_QUIRK_32BIT_ADMA_SIZE,
1427 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1428 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1429 SDHCI_QUIRK2_HOST_OFF_CARD_ON |
1430 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1431};
1432
1433static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
1434 .soc_ctl_map = &rk3399_soc_ctl_map,
1435 .pdata = &sdhci_arasan_cqe_pdata,
1436 .clk_ops = &arasan_clk_ops,
1437};
1438
1439static struct sdhci_arasan_of_data intel_lgm_emmc_data = {
1440 .soc_ctl_map = &intel_lgm_emmc_soc_ctl_map,
1441 .pdata = &sdhci_arasan_cqe_pdata,
1442 .clk_ops = &arasan_clk_ops,
1443};
1444
1445static struct sdhci_arasan_of_data intel_lgm_sdxc_data = {
1446 .soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map,
1447 .pdata = &sdhci_arasan_cqe_pdata,
1448 .clk_ops = &arasan_clk_ops,
1449};
1450
1451static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = {
1452 .ops = &sdhci_arasan_ops,
1453 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1454 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1455 SDHCI_QUIRK2_STOP_WITH_TC,
1456};
1457
1458static const struct sdhci_pltfm_data sdhci_arasan_versal_net_pdata = {
1459 .ops = &sdhci_arasan_ops,
1460 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1461 SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1462 SDHCI_QUIRK2_STOP_WITH_TC |
1463 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
1464};
1465
1466static const struct sdhci_arasan_clk_ops zynqmp_clk_ops = {
1467 .sdcardclk_ops = &zynqmp_sdcardclk_ops,
1468 .sampleclk_ops = &zynqmp_sampleclk_ops,
1469};
1470
1471static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = {
1472 .pdata = &sdhci_arasan_zynqmp_pdata,
1473 .clk_ops = &zynqmp_clk_ops,
1474 .quirks = SDHCI_ARASAN_QUIRK_ENSURE_CD_STABLE,
1475};
1476
1477static const struct sdhci_arasan_clk_ops versal_clk_ops = {
1478 .sdcardclk_ops = &versal_sdcardclk_ops,
1479 .sampleclk_ops = &versal_sampleclk_ops,
1480};
1481
1482static struct sdhci_arasan_of_data sdhci_arasan_versal_data = {
1483 .pdata = &sdhci_arasan_zynqmp_pdata,
1484 .clk_ops = &versal_clk_ops,
1485 .quirks = SDHCI_ARASAN_QUIRK_ENSURE_CD_STABLE,
1486};
1487
1488static const struct sdhci_arasan_clk_ops versal_net_clk_ops = {
1489 .sdcardclk_ops = &versal_net_sdcardclk_ops,
1490 .sampleclk_ops = &versal_net_sampleclk_ops,
1491};
1492
1493static struct sdhci_arasan_of_data sdhci_arasan_versal_net_data = {
1494 .pdata = &sdhci_arasan_versal_net_pdata,
1495 .clk_ops = &versal_net_clk_ops,
1496 .quirks = SDHCI_ARASAN_QUIRK_ENSURE_CD_STABLE,
1497};
1498
1499static struct sdhci_arasan_of_data intel_keembay_emmc_data = {
1500 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1501 .pdata = &sdhci_keembay_emmc_pdata,
1502 .clk_ops = &arasan_clk_ops,
1503};
1504
1505static struct sdhci_arasan_of_data intel_keembay_sd_data = {
1506 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1507 .pdata = &sdhci_keembay_sd_pdata,
1508 .clk_ops = &arasan_clk_ops,
1509};
1510
1511static struct sdhci_arasan_of_data intel_keembay_sdio_data = {
1512 .soc_ctl_map = &intel_keembay_soc_ctl_map,
1513 .pdata = &sdhci_keembay_sdio_pdata,
1514 .clk_ops = &arasan_clk_ops,
1515};
1516
1517static const struct of_device_id sdhci_arasan_of_match[] = {
1518 /* SoC-specific compatible strings w/ soc_ctl_map */
1519 {
1520 .compatible = "rockchip,rk3399-sdhci-5.1",
1521 .data = &sdhci_arasan_rk3399_data,
1522 },
1523 {
1524 .compatible = "intel,lgm-sdhci-5.1-emmc",
1525 .data = &intel_lgm_emmc_data,
1526 },
1527 {
1528 .compatible = "intel,lgm-sdhci-5.1-sdxc",
1529 .data = &intel_lgm_sdxc_data,
1530 },
1531 {
1532 .compatible = "intel,keembay-sdhci-5.1-emmc",
1533 .data = &intel_keembay_emmc_data,
1534 },
1535 {
1536 .compatible = "intel,keembay-sdhci-5.1-sd",
1537 .data = &intel_keembay_sd_data,
1538 },
1539 {
1540 .compatible = "intel,keembay-sdhci-5.1-sdio",
1541 .data = &intel_keembay_sdio_data,
1542 },
1543 /* Generic compatible below here */
1544 {
1545 .compatible = "arasan,sdhci-8.9a",
1546 .data = &sdhci_arasan_generic_data,
1547 },
1548 {
1549 .compatible = "arasan,sdhci-5.1",
1550 .data = &sdhci_arasan_generic_data,
1551 },
1552 {
1553 .compatible = "arasan,sdhci-4.9a",
1554 .data = &sdhci_arasan_generic_data,
1555 },
1556 {
1557 .compatible = "xlnx,zynqmp-8.9a",
1558 .data = &sdhci_arasan_zynqmp_data,
1559 },
1560 {
1561 .compatible = "xlnx,versal-8.9a",
1562 .data = &sdhci_arasan_versal_data,
1563 },
1564 {
1565 .compatible = "xlnx,versal-net-emmc",
1566 .data = &sdhci_arasan_versal_net_data,
1567 },
1568 { /* sentinel */ }
1569};
1570MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
1571
1572/**
1573 * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use
1574 *
1575 * @sdhci_arasan: Our private data structure.
1576 * @clk_xin: Pointer to the functional clock
1577 * @dev: Pointer to our struct device.
1578 *
1579 * Some PHY devices need to know what the actual card clock is. In order for
1580 * them to find out, we'll provide a clock through the common clock framework
1581 * for them to query.
1582 *
1583 * Return: 0 on success and error value on error
1584 */
1585static int
1586sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan,
1587 struct clk *clk_xin,
1588 struct device *dev)
1589{
1590 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1591 struct device_node *np = dev->of_node;
1592 struct clk_init_data sdcardclk_init;
1593 const char *parent_clk_name;
1594 int ret;
1595
1596 ret = of_property_read_string_index(np, "clock-output-names", 0,
1597 &sdcardclk_init.name);
1598 if (ret) {
1599 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1600 return ret;
1601 }
1602
1603 parent_clk_name = __clk_get_name(clk_xin);
1604 sdcardclk_init.parent_names = &parent_clk_name;
1605 sdcardclk_init.num_parents = 1;
1606 sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
1607 sdcardclk_init.ops = sdhci_arasan->clk_ops->sdcardclk_ops;
1608
1609 clk_data->sdcardclk_hw.init = &sdcardclk_init;
1610 clk_data->sdcardclk =
1611 devm_clk_register(dev, &clk_data->sdcardclk_hw);
1612 if (IS_ERR(clk_data->sdcardclk))
1613 return PTR_ERR(clk_data->sdcardclk);
1614 clk_data->sdcardclk_hw.init = NULL;
1615
1616 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1617 clk_data->sdcardclk);
1618 if (ret)
1619 dev_err(dev, "Failed to add sdcard clock provider\n");
1620
1621 return ret;
1622}
1623
1624/**
1625 * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use
1626 *
1627 * @sdhci_arasan: Our private data structure.
1628 * @clk_xin: Pointer to the functional clock
1629 * @dev: Pointer to our struct device.
1630 *
1631 * Some PHY devices need to know what the actual card clock is. In order for
1632 * them to find out, we'll provide a clock through the common clock framework
1633 * for them to query.
1634 *
1635 * Return: 0 on success and error value on error
1636 */
1637static int
1638sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan,
1639 struct clk *clk_xin,
1640 struct device *dev)
1641{
1642 struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1643 struct device_node *np = dev->of_node;
1644 struct clk_init_data sampleclk_init;
1645 const char *parent_clk_name;
1646 int ret;
1647
1648 ret = of_property_read_string_index(np, "clock-output-names", 1,
1649 &sampleclk_init.name);
1650 if (ret) {
1651 dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1652 return ret;
1653 }
1654
1655 parent_clk_name = __clk_get_name(clk_xin);
1656 sampleclk_init.parent_names = &parent_clk_name;
1657 sampleclk_init.num_parents = 1;
1658 sampleclk_init.flags = CLK_GET_RATE_NOCACHE;
1659 sampleclk_init.ops = sdhci_arasan->clk_ops->sampleclk_ops;
1660
1661 clk_data->sampleclk_hw.init = &sampleclk_init;
1662 clk_data->sampleclk =
1663 devm_clk_register(dev, &clk_data->sampleclk_hw);
1664 if (IS_ERR(clk_data->sampleclk))
1665 return PTR_ERR(clk_data->sampleclk);
1666 clk_data->sampleclk_hw.init = NULL;
1667
1668 ret = of_clk_add_provider(np, of_clk_src_simple_get,
1669 clk_data->sampleclk);
1670 if (ret)
1671 dev_err(dev, "Failed to add sample clock provider\n");
1672
1673 return ret;
1674}
1675
1676/**
1677 * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
1678 *
1679 * @dev: Pointer to our struct device.
1680 *
1681 * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
1682 * returned success.
1683 */
1684static void sdhci_arasan_unregister_sdclk(struct device *dev)
1685{
1686 struct device_node *np = dev->of_node;
1687
1688 if (!of_property_present(np, "#clock-cells"))
1689 return;
1690
1691 of_clk_del_provider(dev->of_node);
1692}
1693
1694/**
1695 * sdhci_arasan_update_support64b - Set SUPPORT_64B (64-bit System Bus Support)
1696 * @host: The sdhci_host
1697 * @value: The value to write
1698 *
1699 * This should be set based on the System Address Bus.
1700 * 0: the Core supports only 32-bit System Address Bus.
1701 * 1: the Core supports 64-bit System Address Bus.
1702 *
1703 * NOTE:
1704 * For Keem Bay, it is required to clear this bit. Its default value is 1'b1.
1705 * Keem Bay does not support 64-bit access.
1706 */
1707static void sdhci_arasan_update_support64b(struct sdhci_host *host, u32 value)
1708{
1709 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1710 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1711 const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
1712
1713 /* Having a map is optional */
1714 soc_ctl_map = sdhci_arasan->soc_ctl_map;
1715 if (!soc_ctl_map)
1716 return;
1717
1718 /* If we have a map, we expect to have a syscon */
1719 if (!sdhci_arasan->soc_ctl_base) {
1720 pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1721 mmc_hostname(host->mmc));
1722 return;
1723 }
1724
1725 sdhci_arasan_syscon_write(host, &soc_ctl_map->support64b, value);
1726}
1727
1728/**
1729 * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use
1730 *
1731 * @sdhci_arasan: Our private data structure.
1732 * @clk_xin: Pointer to the functional clock
1733 * @dev: Pointer to our struct device.
1734 *
1735 * Some PHY devices need to know what the actual card clock is. In order for
1736 * them to find out, we'll provide a clock through the common clock framework
1737 * for them to query.
1738 *
1739 * Note: without seriously re-architecting SDHCI's clock code and testing on
1740 * all platforms, there's no way to create a totally beautiful clock here
1741 * with all clock ops implemented. Instead, we'll just create a clock that can
1742 * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
1743 * framework that we're doing things behind its back. This should be sufficient
1744 * to create nice clean device tree bindings and later (if needed) we can try
1745 * re-architecting SDHCI if we see some benefit to it.
1746 *
1747 * Return: 0 on success and error value on error
1748 */
1749static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
1750 struct clk *clk_xin,
1751 struct device *dev)
1752{
1753 struct device_node *np = dev->of_node;
1754 u32 num_clks = 0;
1755 int ret;
1756
1757 /* Providing a clock to the PHY is optional; no error if missing */
1758 if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0)
1759 return 0;
1760
1761 ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev);
1762 if (ret)
1763 return ret;
1764
1765 if (num_clks) {
1766 ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin,
1767 dev);
1768 if (ret) {
1769 sdhci_arasan_unregister_sdclk(dev);
1770 return ret;
1771 }
1772 }
1773
1774 return 0;
1775}
1776
1777static int sdhci_zynqmp_set_dynamic_config(struct device *dev,
1778 struct sdhci_arasan_data *sdhci_arasan)
1779{
1780 struct sdhci_host *host = sdhci_arasan->host;
1781 struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw;
1782 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1783 const char *clk_name = clk_hw_get_name(hw);
1784 u32 mhz, node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1;
1785 struct reset_control *rstc;
1786 int ret;
1787
1788 /* Obtain SDHC reset control */
1789 rstc = devm_reset_control_get_optional_exclusive(dev, NULL);
1790 if (IS_ERR(rstc)) {
1791 dev_err(dev, "Cannot get SDHC reset.\n");
1792 return PTR_ERR(rstc);
1793 }
1794
1795 ret = reset_control_assert(rstc);
1796 if (ret)
1797 return ret;
1798
1799 ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_FIXED, 0);
1800 if (ret)
1801 return ret;
1802
1803 ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_EMMC_SEL,
1804 !!(host->mmc->caps & MMC_CAP_NONREMOVABLE));
1805 if (ret)
1806 return ret;
1807
1808 mhz = DIV_ROUND_CLOSEST_ULL(clk_get_rate(pltfm_host->clk), 1000000);
1809 if (mhz > 100 && mhz <= 200)
1810 mhz = 200;
1811 else if (mhz > 50 && mhz <= 100)
1812 mhz = 100;
1813 else if (mhz > 25 && mhz <= 50)
1814 mhz = 50;
1815 else
1816 mhz = 25;
1817
1818 ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_BASECLK, mhz);
1819 if (ret)
1820 return ret;
1821
1822 ret = zynqmp_pm_set_sd_config(node_id, SD_CONFIG_8BIT,
1823 !!(host->mmc->caps & MMC_CAP_8_BIT_DATA));
1824 if (ret)
1825 return ret;
1826
1827 ret = reset_control_deassert(rstc);
1828 if (ret)
1829 return ret;
1830
1831 usleep_range(1000, 1500);
1832
1833 return 0;
1834}
1835
1836static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
1837{
1838 struct sdhci_host *host = sdhci_arasan->host;
1839 struct cqhci_host *cq_host;
1840 bool dma64;
1841 int ret;
1842
1843 if (!sdhci_arasan->has_cqe)
1844 return sdhci_add_host(host);
1845
1846 ret = sdhci_setup_host(host);
1847 if (ret)
1848 return ret;
1849
1850 cq_host = devm_kzalloc(host->mmc->parent,
1851 sizeof(*cq_host), GFP_KERNEL);
1852 if (!cq_host) {
1853 ret = -ENOMEM;
1854 goto cleanup;
1855 }
1856
1857 cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
1858 cq_host->ops = &sdhci_arasan_cqhci_ops;
1859
1860 dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1861 if (dma64)
1862 cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1863
1864 ret = cqhci_init(cq_host, host->mmc, dma64);
1865 if (ret)
1866 goto cleanup;
1867
1868 ret = __sdhci_add_host(host);
1869 if (ret)
1870 goto cleanup;
1871
1872 return 0;
1873
1874cleanup:
1875 sdhci_cleanup_host(host);
1876 return ret;
1877}
1878
1879static int sdhci_arasan_probe(struct platform_device *pdev)
1880{
1881 int ret;
1882 struct device_node *node;
1883 struct clk *clk_xin;
1884 struct clk *clk_dll;
1885 struct sdhci_host *host;
1886 struct sdhci_pltfm_host *pltfm_host;
1887 struct device *dev = &pdev->dev;
1888 struct device_node *np = dev->of_node;
1889 struct sdhci_arasan_data *sdhci_arasan;
1890 const struct sdhci_arasan_of_data *data;
1891
1892 data = of_device_get_match_data(dev);
1893 if (!data)
1894 return -EINVAL;
1895
1896 host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
1897
1898 if (IS_ERR(host))
1899 return PTR_ERR(host);
1900
1901 pltfm_host = sdhci_priv(host);
1902 sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1903 sdhci_arasan->host = host;
1904
1905 sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
1906 sdhci_arasan->clk_ops = data->clk_ops;
1907
1908 node = of_parse_phandle(np, "arasan,soc-ctl-syscon", 0);
1909 if (node) {
1910 sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
1911 of_node_put(node);
1912
1913 if (IS_ERR(sdhci_arasan->soc_ctl_base))
1914 return dev_err_probe(dev,
1915 PTR_ERR(sdhci_arasan->soc_ctl_base),
1916 "Can't get syscon\n");
1917 }
1918
1919 sdhci_get_of_property(pdev);
1920
1921 sdhci_arasan->clk_ahb = devm_clk_get(dev, "clk_ahb");
1922 if (IS_ERR(sdhci_arasan->clk_ahb))
1923 return dev_err_probe(dev, PTR_ERR(sdhci_arasan->clk_ahb),
1924 "clk_ahb clock not found.\n");
1925
1926 clk_xin = devm_clk_get(dev, "clk_xin");
1927 if (IS_ERR(clk_xin))
1928 return dev_err_probe(dev, PTR_ERR(clk_xin), "clk_xin clock not found.\n");
1929
1930 ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
1931 if (ret)
1932 return dev_err_probe(dev, ret, "Unable to enable AHB clock.\n");
1933
1934 /* If clock-frequency property is set, use the provided value */
1935 if (pltfm_host->clock &&
1936 pltfm_host->clock != clk_get_rate(clk_xin)) {
1937 ret = clk_set_rate(clk_xin, pltfm_host->clock);
1938 if (ret) {
1939 dev_err(&pdev->dev, "Failed to set SD clock rate\n");
1940 goto clk_dis_ahb;
1941 }
1942 }
1943
1944 ret = clk_prepare_enable(clk_xin);
1945 if (ret) {
1946 dev_err(dev, "Unable to enable SD clock.\n");
1947 goto clk_dis_ahb;
1948 }
1949
1950 clk_dll = devm_clk_get_optional_enabled(dev, "gate");
1951 if (IS_ERR(clk_dll)) {
1952 ret = dev_err_probe(dev, PTR_ERR(clk_dll), "failed to get dll clk\n");
1953 goto clk_disable_all;
1954 }
1955
1956 if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
1957 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
1958
1959 if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
1960 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
1961
1962 pltfm_host->clk = clk_xin;
1963
1964 if (of_device_is_compatible(np, "rockchip,rk3399-sdhci-5.1"))
1965 sdhci_arasan_update_clockmultiplier(host, 0x0);
1966
1967 sdhci_arasan->quirks |= data->quirks;
1968
1969 if (of_device_is_compatible(np, "intel,keembay-sdhci-5.1-emmc") ||
1970 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sd") ||
1971 of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sdio")) {
1972 sdhci_arasan_update_clockmultiplier(host, 0x0);
1973 sdhci_arasan_update_support64b(host, 0x0);
1974
1975 host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1976 }
1977
1978 sdhci_arasan_update_baseclkfreq(host);
1979
1980 ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, dev);
1981 if (ret)
1982 goto clk_disable_all;
1983
1984 if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
1985 host->mmc_host_ops.execute_tuning =
1986 arasan_zynqmp_execute_tuning;
1987
1988 sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN;
1989 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
1990 }
1991
1992 arasan_dt_parse_clk_phases(dev, &sdhci_arasan->clk_data);
1993
1994 ret = mmc_of_parse(host->mmc);
1995 if (ret) {
1996 ret = dev_err_probe(dev, ret, "parsing dt failed.\n");
1997 goto unreg_clk;
1998 }
1999
2000 if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
2001 ret = zynqmp_pm_is_function_supported(PM_IOCTL, IOCTL_SET_SD_CONFIG);
2002 if (!ret) {
2003 ret = sdhci_zynqmp_set_dynamic_config(dev, sdhci_arasan);
2004 if (ret)
2005 goto unreg_clk;
2006 }
2007 }
2008
2009 sdhci_arasan->phy = ERR_PTR(-ENODEV);
2010 if (of_device_is_compatible(np, "arasan,sdhci-5.1")) {
2011 sdhci_arasan->phy = devm_phy_get(dev, "phy_arasan");
2012 if (IS_ERR(sdhci_arasan->phy)) {
2013 ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->phy),
2014 "No phy for arasan,sdhci-5.1.\n");
2015 goto unreg_clk;
2016 }
2017
2018 ret = phy_init(sdhci_arasan->phy);
2019 if (ret < 0) {
2020 dev_err(dev, "phy_init err.\n");
2021 goto unreg_clk;
2022 }
2023
2024 host->mmc_host_ops.hs400_enhanced_strobe =
2025 sdhci_arasan_hs400_enhanced_strobe;
2026 host->mmc_host_ops.start_signal_voltage_switch =
2027 sdhci_arasan_voltage_switch;
2028 sdhci_arasan->has_cqe = true;
2029 host->mmc->caps2 |= MMC_CAP2_CQE;
2030
2031 if (!of_property_read_bool(np, "disable-cqe-dcmd"))
2032 host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
2033 }
2034
2035 if (of_device_is_compatible(np, "xlnx,versal-net-emmc"))
2036 sdhci_arasan->internal_phy_reg = true;
2037
2038 ret = sdhci_arasan_add_host(sdhci_arasan);
2039 if (ret)
2040 goto err_add_host;
2041
2042 return 0;
2043
2044err_add_host:
2045 if (!IS_ERR(sdhci_arasan->phy))
2046 phy_exit(sdhci_arasan->phy);
2047unreg_clk:
2048 sdhci_arasan_unregister_sdclk(dev);
2049clk_disable_all:
2050 clk_disable_unprepare(clk_xin);
2051clk_dis_ahb:
2052 clk_disable_unprepare(sdhci_arasan->clk_ahb);
2053 return ret;
2054}
2055
2056static void sdhci_arasan_remove(struct platform_device *pdev)
2057{
2058 struct sdhci_host *host = platform_get_drvdata(pdev);
2059 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2060 struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
2061 struct clk *clk_ahb = sdhci_arasan->clk_ahb;
2062 struct clk *clk_xin = pltfm_host->clk;
2063
2064 if (!IS_ERR(sdhci_arasan->phy)) {
2065 if (sdhci_arasan->is_phy_on)
2066 phy_power_off(sdhci_arasan->phy);
2067 phy_exit(sdhci_arasan->phy);
2068 }
2069
2070 sdhci_arasan_unregister_sdclk(&pdev->dev);
2071
2072 sdhci_pltfm_remove(pdev);
2073
2074 clk_disable_unprepare(clk_xin);
2075 clk_disable_unprepare(clk_ahb);
2076}
2077
2078static struct platform_driver sdhci_arasan_driver = {
2079 .driver = {
2080 .name = "sdhci-arasan",
2081 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
2082 .of_match_table = sdhci_arasan_of_match,
2083 .pm = &sdhci_arasan_dev_pm_ops,
2084 },
2085 .probe = sdhci_arasan_probe,
2086 .remove = sdhci_arasan_remove,
2087};
2088
2089module_platform_driver(sdhci_arasan_driver);
2090
2091MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
2092MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
2093MODULE_LICENSE("GPL");