1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
7 #include <linux/bitfield.h>
8 #include <linux/bits.h>
9 #include <linux/iopoll.h>
10 #include <linux/module.h>
11 #include <linux/mmc/host.h>
12 #include <linux/mmc/mmc.h>
15 #include "sdhci-pltfm.h"
17 /* HRS - Host Register Set (specific to Cadence) */
18 #define SDHCI_CDNS_HRS04 0x10 /* PHY access port */
19 #define SDHCI_CDNS_HRS04_ACK BIT(26)
20 #define SDHCI_CDNS_HRS04_RD BIT(25)
21 #define SDHCI_CDNS_HRS04_WR BIT(24)
22 #define SDHCI_CDNS_HRS04_RDATA GENMASK(23, 16)
23 #define SDHCI_CDNS_HRS04_WDATA GENMASK(15, 8)
24 #define SDHCI_CDNS_HRS04_ADDR GENMASK(5, 0)
26 #define SDHCI_CDNS_HRS06 0x18 /* eMMC control */
27 #define SDHCI_CDNS_HRS06_TUNE_UP BIT(15)
28 #define SDHCI_CDNS_HRS06_TUNE GENMASK(13, 8)
29 #define SDHCI_CDNS_HRS06_MODE GENMASK(2, 0)
30 #define SDHCI_CDNS_HRS06_MODE_SD 0x0
31 #define SDHCI_CDNS_HRS06_MODE_MMC_SDR 0x2
32 #define SDHCI_CDNS_HRS06_MODE_MMC_DDR 0x3
33 #define SDHCI_CDNS_HRS06_MODE_MMC_HS200 0x4
34 #define SDHCI_CDNS_HRS06_MODE_MMC_HS400 0x5
35 #define SDHCI_CDNS_HRS06_MODE_MMC_HS400ES 0x6
37 /* SRS - Slot Register Set (SDHCI-compatible) */
38 #define SDHCI_CDNS_SRS_BASE 0x200
41 #define SDHCI_CDNS_PHY_DLY_SD_HS 0x00
42 #define SDHCI_CDNS_PHY_DLY_SD_DEFAULT 0x01
43 #define SDHCI_CDNS_PHY_DLY_UHS_SDR12 0x02
44 #define SDHCI_CDNS_PHY_DLY_UHS_SDR25 0x03
45 #define SDHCI_CDNS_PHY_DLY_UHS_SDR50 0x04
46 #define SDHCI_CDNS_PHY_DLY_UHS_DDR50 0x05
47 #define SDHCI_CDNS_PHY_DLY_EMMC_LEGACY 0x06
48 #define SDHCI_CDNS_PHY_DLY_EMMC_SDR 0x07
49 #define SDHCI_CDNS_PHY_DLY_EMMC_DDR 0x08
50 #define SDHCI_CDNS_PHY_DLY_SDCLK 0x0b
51 #define SDHCI_CDNS_PHY_DLY_HSMMC 0x0c
52 #define SDHCI_CDNS_PHY_DLY_STROBE 0x0d
55 * The tuned val register is 6 bit-wide, but not the whole of the range is
56 * available. The range 0-42 seems to be available (then 43 wraps around to 0)
57 * but I am not quite sure if it is official. Use only 0 to 39 for safety.
59 #define SDHCI_CDNS_MAX_TUNING_LOOP 40
61 struct sdhci_cdns_phy_param
{
66 struct sdhci_cdns_priv
{
67 void __iomem
*hrs_addr
;
69 unsigned int nr_phy_params
;
70 struct sdhci_cdns_phy_param phy_params
[0];
73 struct sdhci_cdns_phy_cfg
{
78 static const struct sdhci_cdns_phy_cfg sdhci_cdns_phy_cfgs
[] = {
79 { "cdns,phy-input-delay-sd-highspeed", SDHCI_CDNS_PHY_DLY_SD_HS
, },
80 { "cdns,phy-input-delay-legacy", SDHCI_CDNS_PHY_DLY_SD_DEFAULT
, },
81 { "cdns,phy-input-delay-sd-uhs-sdr12", SDHCI_CDNS_PHY_DLY_UHS_SDR12
, },
82 { "cdns,phy-input-delay-sd-uhs-sdr25", SDHCI_CDNS_PHY_DLY_UHS_SDR25
, },
83 { "cdns,phy-input-delay-sd-uhs-sdr50", SDHCI_CDNS_PHY_DLY_UHS_SDR50
, },
84 { "cdns,phy-input-delay-sd-uhs-ddr50", SDHCI_CDNS_PHY_DLY_UHS_DDR50
, },
85 { "cdns,phy-input-delay-mmc-highspeed", SDHCI_CDNS_PHY_DLY_EMMC_SDR
, },
86 { "cdns,phy-input-delay-mmc-ddr", SDHCI_CDNS_PHY_DLY_EMMC_DDR
, },
87 { "cdns,phy-dll-delay-sdclk", SDHCI_CDNS_PHY_DLY_SDCLK
, },
88 { "cdns,phy-dll-delay-sdclk-hsmmc", SDHCI_CDNS_PHY_DLY_HSMMC
, },
89 { "cdns,phy-dll-delay-strobe", SDHCI_CDNS_PHY_DLY_STROBE
, },
92 static int sdhci_cdns_write_phy_reg(struct sdhci_cdns_priv
*priv
,
95 void __iomem
*reg
= priv
->hrs_addr
+ SDHCI_CDNS_HRS04
;
99 tmp
= FIELD_PREP(SDHCI_CDNS_HRS04_WDATA
, data
) |
100 FIELD_PREP(SDHCI_CDNS_HRS04_ADDR
, addr
);
103 tmp
|= SDHCI_CDNS_HRS04_WR
;
106 ret
= readl_poll_timeout(reg
, tmp
, tmp
& SDHCI_CDNS_HRS04_ACK
, 0, 10);
110 tmp
&= ~SDHCI_CDNS_HRS04_WR
;
116 static unsigned int sdhci_cdns_phy_param_count(struct device_node
*np
)
118 unsigned int count
= 0;
121 for (i
= 0; i
< ARRAY_SIZE(sdhci_cdns_phy_cfgs
); i
++)
122 if (of_property_read_bool(np
, sdhci_cdns_phy_cfgs
[i
].property
))
128 static void sdhci_cdns_phy_param_parse(struct device_node
*np
,
129 struct sdhci_cdns_priv
*priv
)
131 struct sdhci_cdns_phy_param
*p
= priv
->phy_params
;
135 for (i
= 0; i
< ARRAY_SIZE(sdhci_cdns_phy_cfgs
); i
++) {
136 ret
= of_property_read_u32(np
, sdhci_cdns_phy_cfgs
[i
].property
,
141 p
->addr
= sdhci_cdns_phy_cfgs
[i
].addr
;
147 static int sdhci_cdns_phy_init(struct sdhci_cdns_priv
*priv
)
151 for (i
= 0; i
< priv
->nr_phy_params
; i
++) {
152 ret
= sdhci_cdns_write_phy_reg(priv
, priv
->phy_params
[i
].addr
,
153 priv
->phy_params
[i
].data
);
161 static void *sdhci_cdns_priv(struct sdhci_host
*host
)
163 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
165 return sdhci_pltfm_priv(pltfm_host
);
168 static unsigned int sdhci_cdns_get_timeout_clock(struct sdhci_host
*host
)
171 * Cadence's spec says the Timeout Clock Frequency is the same as the
172 * Base Clock Frequency.
174 return host
->max_clk
;
177 static void sdhci_cdns_set_emmc_mode(struct sdhci_cdns_priv
*priv
, u32 mode
)
181 /* The speed mode for eMMC is selected by HRS06 register */
182 tmp
= readl(priv
->hrs_addr
+ SDHCI_CDNS_HRS06
);
183 tmp
&= ~SDHCI_CDNS_HRS06_MODE
;
184 tmp
|= FIELD_PREP(SDHCI_CDNS_HRS06_MODE
, mode
);
185 writel(tmp
, priv
->hrs_addr
+ SDHCI_CDNS_HRS06
);
188 static u32
sdhci_cdns_get_emmc_mode(struct sdhci_cdns_priv
*priv
)
192 tmp
= readl(priv
->hrs_addr
+ SDHCI_CDNS_HRS06
);
193 return FIELD_GET(SDHCI_CDNS_HRS06_MODE
, tmp
);
196 static void sdhci_cdns_set_uhs_signaling(struct sdhci_host
*host
,
199 struct sdhci_cdns_priv
*priv
= sdhci_cdns_priv(host
);
203 case MMC_TIMING_MMC_HS
:
204 mode
= SDHCI_CDNS_HRS06_MODE_MMC_SDR
;
206 case MMC_TIMING_MMC_DDR52
:
207 mode
= SDHCI_CDNS_HRS06_MODE_MMC_DDR
;
209 case MMC_TIMING_MMC_HS200
:
210 mode
= SDHCI_CDNS_HRS06_MODE_MMC_HS200
;
212 case MMC_TIMING_MMC_HS400
:
213 if (priv
->enhanced_strobe
)
214 mode
= SDHCI_CDNS_HRS06_MODE_MMC_HS400ES
;
216 mode
= SDHCI_CDNS_HRS06_MODE_MMC_HS400
;
219 mode
= SDHCI_CDNS_HRS06_MODE_SD
;
223 sdhci_cdns_set_emmc_mode(priv
, mode
);
225 /* For SD, fall back to the default handler */
226 if (mode
== SDHCI_CDNS_HRS06_MODE_SD
)
227 sdhci_set_uhs_signaling(host
, timing
);
230 static const struct sdhci_ops sdhci_cdns_ops
= {
231 .set_clock
= sdhci_set_clock
,
232 .get_timeout_clock
= sdhci_cdns_get_timeout_clock
,
233 .set_bus_width
= sdhci_set_bus_width
,
234 .reset
= sdhci_reset
,
235 .set_uhs_signaling
= sdhci_cdns_set_uhs_signaling
,
238 static const struct sdhci_pltfm_data sdhci_cdns_pltfm_data
= {
239 .ops
= &sdhci_cdns_ops
,
242 static int sdhci_cdns_set_tune_val(struct sdhci_host
*host
, unsigned int val
)
244 struct sdhci_cdns_priv
*priv
= sdhci_cdns_priv(host
);
245 void __iomem
*reg
= priv
->hrs_addr
+ SDHCI_CDNS_HRS06
;
249 if (WARN_ON(!FIELD_FIT(SDHCI_CDNS_HRS06_TUNE
, val
)))
253 tmp
&= ~SDHCI_CDNS_HRS06_TUNE
;
254 tmp
|= FIELD_PREP(SDHCI_CDNS_HRS06_TUNE
, val
);
257 * Workaround for IP errata:
258 * The IP6116 SD/eMMC PHY design has a timing issue on receive data
259 * path. Send tune request twice.
261 for (i
= 0; i
< 2; i
++) {
262 tmp
|= SDHCI_CDNS_HRS06_TUNE_UP
;
265 ret
= readl_poll_timeout(reg
, tmp
,
266 !(tmp
& SDHCI_CDNS_HRS06_TUNE_UP
),
275 static int sdhci_cdns_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
277 struct sdhci_host
*host
= mmc_priv(mmc
);
280 int end_of_streak
= 0;
284 * This handler only implements the eMMC tuning that is specific to
285 * this controller. Fall back to the standard method for SD timing.
287 if (host
->timing
!= MMC_TIMING_MMC_HS200
)
288 return sdhci_execute_tuning(mmc
, opcode
);
290 if (WARN_ON(opcode
!= MMC_SEND_TUNING_BLOCK_HS200
))
293 for (i
= 0; i
< SDHCI_CDNS_MAX_TUNING_LOOP
; i
++) {
294 if (sdhci_cdns_set_tune_val(host
, i
) ||
295 mmc_send_tuning(host
->mmc
, opcode
, NULL
)) { /* bad */
299 if (cur_streak
> max_streak
) {
300 max_streak
= cur_streak
;
307 dev_err(mmc_dev(host
->mmc
), "no tuning point found\n");
311 return sdhci_cdns_set_tune_val(host
, end_of_streak
- max_streak
/ 2);
314 static void sdhci_cdns_hs400_enhanced_strobe(struct mmc_host
*mmc
,
317 struct sdhci_host
*host
= mmc_priv(mmc
);
318 struct sdhci_cdns_priv
*priv
= sdhci_cdns_priv(host
);
321 priv
->enhanced_strobe
= ios
->enhanced_strobe
;
323 mode
= sdhci_cdns_get_emmc_mode(priv
);
325 if (mode
== SDHCI_CDNS_HRS06_MODE_MMC_HS400
&& ios
->enhanced_strobe
)
326 sdhci_cdns_set_emmc_mode(priv
,
327 SDHCI_CDNS_HRS06_MODE_MMC_HS400ES
);
329 if (mode
== SDHCI_CDNS_HRS06_MODE_MMC_HS400ES
&& !ios
->enhanced_strobe
)
330 sdhci_cdns_set_emmc_mode(priv
,
331 SDHCI_CDNS_HRS06_MODE_MMC_HS400
);
334 static int sdhci_cdns_probe(struct platform_device
*pdev
)
336 struct sdhci_host
*host
;
337 struct sdhci_pltfm_host
*pltfm_host
;
338 struct sdhci_cdns_priv
*priv
;
340 unsigned int nr_phy_params
;
342 struct device
*dev
= &pdev
->dev
;
343 static const u16 version
= SDHCI_SPEC_400
<< SDHCI_SPEC_VER_SHIFT
;
345 clk
= devm_clk_get(dev
, NULL
);
349 ret
= clk_prepare_enable(clk
);
353 nr_phy_params
= sdhci_cdns_phy_param_count(dev
->of_node
);
354 host
= sdhci_pltfm_init(pdev
, &sdhci_cdns_pltfm_data
,
355 struct_size(priv
, phy_params
, nr_phy_params
));
361 pltfm_host
= sdhci_priv(host
);
362 pltfm_host
->clk
= clk
;
364 priv
= sdhci_pltfm_priv(pltfm_host
);
365 priv
->nr_phy_params
= nr_phy_params
;
366 priv
->hrs_addr
= host
->ioaddr
;
367 priv
->enhanced_strobe
= false;
368 host
->ioaddr
+= SDHCI_CDNS_SRS_BASE
;
369 host
->mmc_host_ops
.execute_tuning
= sdhci_cdns_execute_tuning
;
370 host
->mmc_host_ops
.hs400_enhanced_strobe
=
371 sdhci_cdns_hs400_enhanced_strobe
;
372 sdhci_enable_v4_mode(host
);
373 __sdhci_read_caps(host
, &version
, NULL
, NULL
);
375 sdhci_get_of_property(pdev
);
377 ret
= mmc_of_parse(host
->mmc
);
381 sdhci_cdns_phy_param_parse(dev
->of_node
, priv
);
383 ret
= sdhci_cdns_phy_init(priv
);
387 ret
= sdhci_add_host(host
);
393 sdhci_pltfm_free(pdev
);
395 clk_disable_unprepare(clk
);
400 #ifdef CONFIG_PM_SLEEP
401 static int sdhci_cdns_resume(struct device
*dev
)
403 struct sdhci_host
*host
= dev_get_drvdata(dev
);
404 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
405 struct sdhci_cdns_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
408 ret
= clk_prepare_enable(pltfm_host
->clk
);
412 ret
= sdhci_cdns_phy_init(priv
);
416 ret
= sdhci_resume_host(host
);
423 clk_disable_unprepare(pltfm_host
->clk
);
429 static const struct dev_pm_ops sdhci_cdns_pm_ops
= {
430 SET_SYSTEM_SLEEP_PM_OPS(sdhci_pltfm_suspend
, sdhci_cdns_resume
)
433 static const struct of_device_id sdhci_cdns_match
[] = {
434 { .compatible
= "socionext,uniphier-sd4hc" },
435 { .compatible
= "cdns,sd4hc" },
438 MODULE_DEVICE_TABLE(of
, sdhci_cdns_match
);
440 static struct platform_driver sdhci_cdns_driver
= {
442 .name
= "sdhci-cdns",
443 .pm
= &sdhci_cdns_pm_ops
,
444 .of_match_table
= sdhci_cdns_match
,
446 .probe
= sdhci_cdns_probe
,
447 .remove
= sdhci_pltfm_unregister
,
449 module_platform_driver(sdhci_cdns_driver
);
451 MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
452 MODULE_DESCRIPTION("Cadence SD/SDIO/eMMC Host Controller Driver");
453 MODULE_LICENSE("GPL");