2 * Copyright (C) 2016 Socionext Inc.
3 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/bitfield.h>
17 #include <linux/bitops.h>
18 #include <linux/iopoll.h>
19 #include <linux/module.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/mmc.h>
24 #include "sdhci-pltfm.h"
26 /* HRS - Host Register Set (specific to Cadence) */
27 #define SDHCI_CDNS_HRS04 0x10 /* PHY access port */
28 #define SDHCI_CDNS_HRS04_ACK BIT(26)
29 #define SDHCI_CDNS_HRS04_RD BIT(25)
30 #define SDHCI_CDNS_HRS04_WR BIT(24)
31 #define SDHCI_CDNS_HRS04_RDATA GENMASK(23, 16)
32 #define SDHCI_CDNS_HRS04_WDATA GENMASK(15, 8)
33 #define SDHCI_CDNS_HRS04_ADDR GENMASK(5, 0)
35 #define SDHCI_CDNS_HRS06 0x18 /* eMMC control */
36 #define SDHCI_CDNS_HRS06_TUNE_UP BIT(15)
37 #define SDHCI_CDNS_HRS06_TUNE GENMASK(13, 8)
38 #define SDHCI_CDNS_HRS06_MODE GENMASK(2, 0)
39 #define SDHCI_CDNS_HRS06_MODE_SD 0x0
40 #define SDHCI_CDNS_HRS06_MODE_MMC_SDR 0x2
41 #define SDHCI_CDNS_HRS06_MODE_MMC_DDR 0x3
42 #define SDHCI_CDNS_HRS06_MODE_MMC_HS200 0x4
43 #define SDHCI_CDNS_HRS06_MODE_MMC_HS400 0x5
44 #define SDHCI_CDNS_HRS06_MODE_MMC_HS400ES 0x6
46 /* SRS - Slot Register Set (SDHCI-compatible) */
47 #define SDHCI_CDNS_SRS_BASE 0x200
50 #define SDHCI_CDNS_PHY_DLY_SD_HS 0x00
51 #define SDHCI_CDNS_PHY_DLY_SD_DEFAULT 0x01
52 #define SDHCI_CDNS_PHY_DLY_UHS_SDR12 0x02
53 #define SDHCI_CDNS_PHY_DLY_UHS_SDR25 0x03
54 #define SDHCI_CDNS_PHY_DLY_UHS_SDR50 0x04
55 #define SDHCI_CDNS_PHY_DLY_UHS_DDR50 0x05
56 #define SDHCI_CDNS_PHY_DLY_EMMC_LEGACY 0x06
57 #define SDHCI_CDNS_PHY_DLY_EMMC_SDR 0x07
58 #define SDHCI_CDNS_PHY_DLY_EMMC_DDR 0x08
59 #define SDHCI_CDNS_PHY_DLY_SDCLK 0x0b
60 #define SDHCI_CDNS_PHY_DLY_HSMMC 0x0c
61 #define SDHCI_CDNS_PHY_DLY_STROBE 0x0d
64 * The tuned val register is 6 bit-wide, but not the whole of the range is
65 * available. The range 0-42 seems to be available (then 43 wraps around to 0)
66 * but I am not quite sure if it is official. Use only 0 to 39 for safety.
68 #define SDHCI_CDNS_MAX_TUNING_LOOP 40
70 struct sdhci_cdns_phy_param
{
75 struct sdhci_cdns_priv
{
76 void __iomem
*hrs_addr
;
78 unsigned int nr_phy_params
;
79 struct sdhci_cdns_phy_param phy_params
[0];
82 struct sdhci_cdns_phy_cfg
{
87 static const struct sdhci_cdns_phy_cfg sdhci_cdns_phy_cfgs
[] = {
88 { "cdns,phy-input-delay-sd-highspeed", SDHCI_CDNS_PHY_DLY_SD_HS
, },
89 { "cdns,phy-input-delay-legacy", SDHCI_CDNS_PHY_DLY_SD_DEFAULT
, },
90 { "cdns,phy-input-delay-sd-uhs-sdr12", SDHCI_CDNS_PHY_DLY_UHS_SDR12
, },
91 { "cdns,phy-input-delay-sd-uhs-sdr25", SDHCI_CDNS_PHY_DLY_UHS_SDR25
, },
92 { "cdns,phy-input-delay-sd-uhs-sdr50", SDHCI_CDNS_PHY_DLY_UHS_SDR50
, },
93 { "cdns,phy-input-delay-sd-uhs-ddr50", SDHCI_CDNS_PHY_DLY_UHS_DDR50
, },
94 { "cdns,phy-input-delay-mmc-highspeed", SDHCI_CDNS_PHY_DLY_EMMC_SDR
, },
95 { "cdns,phy-input-delay-mmc-ddr", SDHCI_CDNS_PHY_DLY_EMMC_DDR
, },
96 { "cdns,phy-dll-delay-sdclk", SDHCI_CDNS_PHY_DLY_SDCLK
, },
97 { "cdns,phy-dll-delay-sdclk-hsmmc", SDHCI_CDNS_PHY_DLY_HSMMC
, },
98 { "cdns,phy-dll-delay-strobe", SDHCI_CDNS_PHY_DLY_STROBE
, },
101 static int sdhci_cdns_write_phy_reg(struct sdhci_cdns_priv
*priv
,
104 void __iomem
*reg
= priv
->hrs_addr
+ SDHCI_CDNS_HRS04
;
108 tmp
= FIELD_PREP(SDHCI_CDNS_HRS04_WDATA
, data
) |
109 FIELD_PREP(SDHCI_CDNS_HRS04_ADDR
, addr
);
112 tmp
|= SDHCI_CDNS_HRS04_WR
;
115 ret
= readl_poll_timeout(reg
, tmp
, tmp
& SDHCI_CDNS_HRS04_ACK
, 0, 10);
119 tmp
&= ~SDHCI_CDNS_HRS04_WR
;
125 static unsigned int sdhci_cdns_phy_param_count(struct device_node
*np
)
127 unsigned int count
= 0;
130 for (i
= 0; i
< ARRAY_SIZE(sdhci_cdns_phy_cfgs
); i
++)
131 if (of_property_read_bool(np
, sdhci_cdns_phy_cfgs
[i
].property
))
137 static void sdhci_cdns_phy_param_parse(struct device_node
*np
,
138 struct sdhci_cdns_priv
*priv
)
140 struct sdhci_cdns_phy_param
*p
= priv
->phy_params
;
144 for (i
= 0; i
< ARRAY_SIZE(sdhci_cdns_phy_cfgs
); i
++) {
145 ret
= of_property_read_u32(np
, sdhci_cdns_phy_cfgs
[i
].property
,
150 p
->addr
= sdhci_cdns_phy_cfgs
[i
].addr
;
156 static int sdhci_cdns_phy_init(struct sdhci_cdns_priv
*priv
)
160 for (i
= 0; i
< priv
->nr_phy_params
; i
++) {
161 ret
= sdhci_cdns_write_phy_reg(priv
, priv
->phy_params
[i
].addr
,
162 priv
->phy_params
[i
].data
);
170 static inline void *sdhci_cdns_priv(struct sdhci_host
*host
)
172 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
174 return sdhci_pltfm_priv(pltfm_host
);
177 static unsigned int sdhci_cdns_get_timeout_clock(struct sdhci_host
*host
)
180 * Cadence's spec says the Timeout Clock Frequency is the same as the
181 * Base Clock Frequency.
183 return host
->max_clk
;
186 static void sdhci_cdns_set_emmc_mode(struct sdhci_cdns_priv
*priv
, u32 mode
)
190 /* The speed mode for eMMC is selected by HRS06 register */
191 tmp
= readl(priv
->hrs_addr
+ SDHCI_CDNS_HRS06
);
192 tmp
&= ~SDHCI_CDNS_HRS06_MODE
;
193 tmp
|= FIELD_PREP(SDHCI_CDNS_HRS06_MODE
, mode
);
194 writel(tmp
, priv
->hrs_addr
+ SDHCI_CDNS_HRS06
);
197 static u32
sdhci_cdns_get_emmc_mode(struct sdhci_cdns_priv
*priv
)
201 tmp
= readl(priv
->hrs_addr
+ SDHCI_CDNS_HRS06
);
202 return FIELD_GET(SDHCI_CDNS_HRS06_MODE
, tmp
);
205 static void sdhci_cdns_set_uhs_signaling(struct sdhci_host
*host
,
208 struct sdhci_cdns_priv
*priv
= sdhci_cdns_priv(host
);
212 case MMC_TIMING_MMC_HS
:
213 mode
= SDHCI_CDNS_HRS06_MODE_MMC_SDR
;
215 case MMC_TIMING_MMC_DDR52
:
216 mode
= SDHCI_CDNS_HRS06_MODE_MMC_DDR
;
218 case MMC_TIMING_MMC_HS200
:
219 mode
= SDHCI_CDNS_HRS06_MODE_MMC_HS200
;
221 case MMC_TIMING_MMC_HS400
:
222 if (priv
->enhanced_strobe
)
223 mode
= SDHCI_CDNS_HRS06_MODE_MMC_HS400ES
;
225 mode
= SDHCI_CDNS_HRS06_MODE_MMC_HS400
;
228 mode
= SDHCI_CDNS_HRS06_MODE_SD
;
232 sdhci_cdns_set_emmc_mode(priv
, mode
);
234 /* For SD, fall back to the default handler */
235 if (mode
== SDHCI_CDNS_HRS06_MODE_SD
)
236 sdhci_set_uhs_signaling(host
, timing
);
239 static const struct sdhci_ops sdhci_cdns_ops
= {
240 .set_clock
= sdhci_set_clock
,
241 .get_timeout_clock
= sdhci_cdns_get_timeout_clock
,
242 .set_bus_width
= sdhci_set_bus_width
,
243 .reset
= sdhci_reset
,
244 .set_uhs_signaling
= sdhci_cdns_set_uhs_signaling
,
247 static const struct sdhci_pltfm_data sdhci_cdns_pltfm_data
= {
248 .ops
= &sdhci_cdns_ops
,
251 static int sdhci_cdns_set_tune_val(struct sdhci_host
*host
, unsigned int val
)
253 struct sdhci_cdns_priv
*priv
= sdhci_cdns_priv(host
);
254 void __iomem
*reg
= priv
->hrs_addr
+ SDHCI_CDNS_HRS06
;
258 if (WARN_ON(!FIELD_FIT(SDHCI_CDNS_HRS06_TUNE
, val
)))
262 tmp
&= ~SDHCI_CDNS_HRS06_TUNE
;
263 tmp
|= FIELD_PREP(SDHCI_CDNS_HRS06_TUNE
, val
);
266 * Workaround for IP errata:
267 * The IP6116 SD/eMMC PHY design has a timing issue on receive data
268 * path. Send tune request twice.
270 for (i
= 0; i
< 2; i
++) {
271 tmp
|= SDHCI_CDNS_HRS06_TUNE_UP
;
274 ret
= readl_poll_timeout(reg
, tmp
,
275 !(tmp
& SDHCI_CDNS_HRS06_TUNE_UP
),
284 static int sdhci_cdns_execute_tuning(struct mmc_host
*mmc
, u32 opcode
)
286 struct sdhci_host
*host
= mmc_priv(mmc
);
289 int end_of_streak
= 0;
293 * This handler only implements the eMMC tuning that is specific to
294 * this controller. Fall back to the standard method for SD timing.
296 if (host
->timing
!= MMC_TIMING_MMC_HS200
)
297 return sdhci_execute_tuning(mmc
, opcode
);
299 if (WARN_ON(opcode
!= MMC_SEND_TUNING_BLOCK_HS200
))
302 for (i
= 0; i
< SDHCI_CDNS_MAX_TUNING_LOOP
; i
++) {
303 if (sdhci_cdns_set_tune_val(host
, i
) ||
304 mmc_send_tuning(host
->mmc
, opcode
, NULL
)) { /* bad */
308 if (cur_streak
> max_streak
) {
309 max_streak
= cur_streak
;
316 dev_err(mmc_dev(host
->mmc
), "no tuning point found\n");
320 return sdhci_cdns_set_tune_val(host
, end_of_streak
- max_streak
/ 2);
323 static void sdhci_cdns_hs400_enhanced_strobe(struct mmc_host
*mmc
,
326 struct sdhci_host
*host
= mmc_priv(mmc
);
327 struct sdhci_cdns_priv
*priv
= sdhci_cdns_priv(host
);
330 priv
->enhanced_strobe
= ios
->enhanced_strobe
;
332 mode
= sdhci_cdns_get_emmc_mode(priv
);
334 if (mode
== SDHCI_CDNS_HRS06_MODE_MMC_HS400
&& ios
->enhanced_strobe
)
335 sdhci_cdns_set_emmc_mode(priv
,
336 SDHCI_CDNS_HRS06_MODE_MMC_HS400ES
);
338 if (mode
== SDHCI_CDNS_HRS06_MODE_MMC_HS400ES
&& !ios
->enhanced_strobe
)
339 sdhci_cdns_set_emmc_mode(priv
,
340 SDHCI_CDNS_HRS06_MODE_MMC_HS400
);
343 static int sdhci_cdns_probe(struct platform_device
*pdev
)
345 struct sdhci_host
*host
;
346 struct sdhci_pltfm_host
*pltfm_host
;
347 struct sdhci_cdns_priv
*priv
;
350 unsigned int nr_phy_params
;
352 struct device
*dev
= &pdev
->dev
;
354 clk
= devm_clk_get(dev
, NULL
);
358 ret
= clk_prepare_enable(clk
);
362 nr_phy_params
= sdhci_cdns_phy_param_count(dev
->of_node
);
363 priv_size
= sizeof(*priv
) + sizeof(priv
->phy_params
[0]) * nr_phy_params
;
364 host
= sdhci_pltfm_init(pdev
, &sdhci_cdns_pltfm_data
, priv_size
);
370 pltfm_host
= sdhci_priv(host
);
371 pltfm_host
->clk
= clk
;
373 priv
= sdhci_pltfm_priv(pltfm_host
);
374 priv
->nr_phy_params
= nr_phy_params
;
375 priv
->hrs_addr
= host
->ioaddr
;
376 priv
->enhanced_strobe
= false;
377 host
->ioaddr
+= SDHCI_CDNS_SRS_BASE
;
378 host
->mmc_host_ops
.execute_tuning
= sdhci_cdns_execute_tuning
;
379 host
->mmc_host_ops
.hs400_enhanced_strobe
=
380 sdhci_cdns_hs400_enhanced_strobe
;
382 sdhci_get_of_property(pdev
);
384 ret
= mmc_of_parse(host
->mmc
);
388 sdhci_cdns_phy_param_parse(dev
->of_node
, priv
);
390 ret
= sdhci_cdns_phy_init(priv
);
394 ret
= sdhci_add_host(host
);
400 sdhci_pltfm_free(pdev
);
402 clk_disable_unprepare(clk
);
407 #ifdef CONFIG_PM_SLEEP
408 static int sdhci_cdns_resume(struct device
*dev
)
410 struct sdhci_host
*host
= dev_get_drvdata(dev
);
411 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
412 struct sdhci_cdns_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
415 ret
= clk_prepare_enable(pltfm_host
->clk
);
419 ret
= sdhci_cdns_phy_init(priv
);
423 ret
= sdhci_resume_host(host
);
430 clk_disable_unprepare(pltfm_host
->clk
);
436 static const struct dev_pm_ops sdhci_cdns_pm_ops
= {
437 SET_SYSTEM_SLEEP_PM_OPS(sdhci_pltfm_suspend
, sdhci_cdns_resume
)
440 static const struct of_device_id sdhci_cdns_match
[] = {
441 { .compatible
= "socionext,uniphier-sd4hc" },
442 { .compatible
= "cdns,sd4hc" },
445 MODULE_DEVICE_TABLE(of
, sdhci_cdns_match
);
447 static struct platform_driver sdhci_cdns_driver
= {
449 .name
= "sdhci-cdns",
450 .pm
= &sdhci_cdns_pm_ops
,
451 .of_match_table
= sdhci_cdns_match
,
453 .probe
= sdhci_cdns_probe
,
454 .remove
= sdhci_pltfm_unregister
,
456 module_platform_driver(sdhci_cdns_driver
);
458 MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
459 MODULE_DESCRIPTION("Cadence SD/SDIO/eMMC Host Controller Driver");
460 MODULE_LICENSE("GPL");