1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ZX Specific Extensions for Synopsys DW Multimedia Card Interface driver
5 * Copyright (C) 2016, Linaro Ltd.
6 * Copyright (C) 2016, ZTE Corp.
10 #include <linux/mfd/syscon.h>
11 #include <linux/mmc/host.h>
12 #include <linux/mmc/mmc.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
21 #include "dw_mmc-pltfm.h"
22 #include "dw_mmc-zx.h"
24 struct dw_mci_zx_priv_data
{
25 struct regmap
*sysc_base
;
29 DELAY_TYPE_READ
, /* read dqs delay */
30 DELAY_TYPE_CLK
, /* clk sample delay */
33 static int dw_mci_zx_emmc_set_delay(struct dw_mci
*host
, unsigned int delay
,
34 enum delay_type dflag
)
36 struct dw_mci_zx_priv_data
*priv
= host
->priv
;
37 struct regmap
*sysc_base
= priv
->sysc_base
;
39 unsigned int loop
= 1000;
45 ret
= regmap_update_bits(sysc_base
, LB_AON_EMMC_CFG_REG0
,
46 PARA_HALF_CLK_MODE
| PARA_DLL_BYPASS_MODE
|
47 PARA_PHASE_DET_SEL_MASK
|
48 PARA_DLL_LOCK_NUM_MASK
|
49 DLL_REG_SET
| PARA_DLL_START_MASK
,
50 PARA_DLL_START(4) | PARA_DLL_LOCK_NUM(4));
54 ret
= regmap_read(sysc_base
, LB_AON_EMMC_CFG_REG1
, &clksel
);
58 if (dflag
== DELAY_TYPE_CLK
) {
59 clksel
&= ~CLK_SAMP_DELAY_MASK
;
60 clksel
|= CLK_SAMP_DELAY(delay
);
62 clksel
&= ~READ_DQS_DELAY_MASK
;
63 clksel
|= READ_DQS_DELAY(delay
);
66 regmap_write(sysc_base
, LB_AON_EMMC_CFG_REG1
, clksel
);
67 regmap_update_bits(sysc_base
, LB_AON_EMMC_CFG_REG0
,
68 PARA_DLL_START_MASK
| PARA_DLL_LOCK_NUM_MASK
|
70 PARA_DLL_START(4) | PARA_DLL_LOCK_NUM(4) |
74 ret
= regmap_read(sysc_base
, LB_AON_EMMC_CFG_REG2
, &clksel
);
78 } while (--loop
&& !(clksel
& ZX_DLL_LOCKED
));
81 dev_err(host
->dev
, "Error: %s dll lock fail\n", __func__
);
88 static int dw_mci_zx_emmc_execute_tuning(struct dw_mci_slot
*slot
, u32 opcode
)
90 struct dw_mci
*host
= slot
->host
;
91 struct mmc_host
*mmc
= slot
->mmc
;
92 int ret
, len
= 0, start
= 0, end
= 0, delay
, best
= 0;
94 for (delay
= 1; delay
< 128; delay
++) {
95 ret
= dw_mci_zx_emmc_set_delay(host
, delay
, DELAY_TYPE_CLK
);
96 if (!ret
&& mmc_send_tuning(mmc
, opcode
, NULL
)) {
99 /* check and update longest good range */
100 if ((end
- start
) > len
) {
101 best
= (start
+ end
) >> 1;
115 if ((end
- start
) > len
) {
116 best
= (start
+ end
) >> 1;
123 dev_info(host
->dev
, "%s best range: start %d end %d\n", __func__
,
125 return dw_mci_zx_emmc_set_delay(host
, best
, DELAY_TYPE_CLK
);
128 static int dw_mci_zx_prepare_hs400_tuning(struct dw_mci
*host
,
133 /* config phase shift as 90 degree */
134 ret
= dw_mci_zx_emmc_set_delay(host
, 32, DELAY_TYPE_READ
);
141 static int dw_mci_zx_execute_tuning(struct dw_mci_slot
*slot
, u32 opcode
)
143 struct dw_mci
*host
= slot
->host
;
145 if (host
->verid
== 0x290a) /* only for emmc */
146 return dw_mci_zx_emmc_execute_tuning(slot
, opcode
);
147 /* TODO: Add 0x210a dedicated tuning for sd/sdio */
152 static int dw_mci_zx_parse_dt(struct dw_mci
*host
)
154 struct device_node
*np
= host
->dev
->of_node
;
155 struct device_node
*node
;
156 struct dw_mci_zx_priv_data
*priv
;
157 struct regmap
*sysc_base
;
160 /* syscon is needed only by emmc */
161 node
= of_parse_phandle(np
, "zte,aon-syscon", 0);
163 sysc_base
= syscon_node_to_regmap(node
);
166 if (IS_ERR(sysc_base
)) {
167 ret
= PTR_ERR(sysc_base
);
168 if (ret
!= -EPROBE_DEFER
)
169 dev_err(host
->dev
, "Can't get syscon: %d\n",
177 priv
= devm_kzalloc(host
->dev
, sizeof(*priv
), GFP_KERNEL
);
180 priv
->sysc_base
= sysc_base
;
186 static unsigned long zx_dwmmc_caps
[3] = {
192 static const struct dw_mci_drv_data zx_drv_data
= {
193 .caps
= zx_dwmmc_caps
,
194 .num_caps
= ARRAY_SIZE(zx_dwmmc_caps
),
195 .execute_tuning
= dw_mci_zx_execute_tuning
,
196 .prepare_hs400_tuning
= dw_mci_zx_prepare_hs400_tuning
,
197 .parse_dt
= dw_mci_zx_parse_dt
,
200 static const struct of_device_id dw_mci_zx_match
[] = {
201 { .compatible
= "zte,zx296718-dw-mshc", .data
= &zx_drv_data
},
204 MODULE_DEVICE_TABLE(of
, dw_mci_zx_match
);
206 static int dw_mci_zx_probe(struct platform_device
*pdev
)
208 const struct dw_mci_drv_data
*drv_data
;
209 const struct of_device_id
*match
;
211 match
= of_match_node(dw_mci_zx_match
, pdev
->dev
.of_node
);
212 drv_data
= match
->data
;
214 return dw_mci_pltfm_register(pdev
, drv_data
);
217 static const struct dev_pm_ops dw_mci_zx_dev_pm_ops
= {
218 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
219 pm_runtime_force_resume
)
220 SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend
,
221 dw_mci_runtime_resume
,
225 static struct platform_driver dw_mci_zx_pltfm_driver
= {
226 .probe
= dw_mci_zx_probe
,
227 .remove
= dw_mci_pltfm_remove
,
230 .of_match_table
= dw_mci_zx_match
,
231 .pm
= &dw_mci_zx_dev_pm_ops
,
235 module_platform_driver(dw_mci_zx_pltfm_driver
);
237 MODULE_DESCRIPTION("ZTE emmc/sd driver");
238 MODULE_LICENSE("GPL v2");