xref: /linux/drivers/mmc/host/dw_mmc-hi3798cv200.c (revision e382ab741252471383e9990583258e3054f66963)
1*e382ab74Stianshuliang // SPDX-License-Identifier: GPL-2.0
2*e382ab74Stianshuliang /*
3*e382ab74Stianshuliang  * Copyright (c) 2018 HiSilicon Technologies Co., Ltd.
4*e382ab74Stianshuliang  */
5*e382ab74Stianshuliang 
6*e382ab74Stianshuliang #include <linux/clk.h>
7*e382ab74Stianshuliang #include <linux/mfd/syscon.h>
8*e382ab74Stianshuliang #include <linux/mmc/host.h>
9*e382ab74Stianshuliang #include <linux/module.h>
10*e382ab74Stianshuliang #include <linux/of_address.h>
11*e382ab74Stianshuliang #include <linux/platform_device.h>
12*e382ab74Stianshuliang #include <linux/pm_runtime.h>
13*e382ab74Stianshuliang #include <linux/regmap.h>
14*e382ab74Stianshuliang #include <linux/regulator/consumer.h>
15*e382ab74Stianshuliang 
16*e382ab74Stianshuliang #include "dw_mmc.h"
17*e382ab74Stianshuliang #include "dw_mmc-pltfm.h"
18*e382ab74Stianshuliang 
19*e382ab74Stianshuliang #define ALL_INT_CLR		0x1ffff
20*e382ab74Stianshuliang 
21*e382ab74Stianshuliang struct hi3798cv200_priv {
22*e382ab74Stianshuliang 	struct clk *sample_clk;
23*e382ab74Stianshuliang 	struct clk *drive_clk;
24*e382ab74Stianshuliang };
25*e382ab74Stianshuliang 
26*e382ab74Stianshuliang static void dw_mci_hi3798cv200_set_ios(struct dw_mci *host, struct mmc_ios *ios)
27*e382ab74Stianshuliang {
28*e382ab74Stianshuliang 	struct hi3798cv200_priv *priv = host->priv;
29*e382ab74Stianshuliang 	u32 val;
30*e382ab74Stianshuliang 
31*e382ab74Stianshuliang 	val = mci_readl(host, UHS_REG);
32*e382ab74Stianshuliang 	if (ios->timing == MMC_TIMING_MMC_DDR52 ||
33*e382ab74Stianshuliang 	    ios->timing == MMC_TIMING_UHS_DDR50)
34*e382ab74Stianshuliang 		val |= SDMMC_UHS_DDR;
35*e382ab74Stianshuliang 	else
36*e382ab74Stianshuliang 		val &= ~SDMMC_UHS_DDR;
37*e382ab74Stianshuliang 	mci_writel(host, UHS_REG, val);
38*e382ab74Stianshuliang 
39*e382ab74Stianshuliang 	val = mci_readl(host, ENABLE_SHIFT);
40*e382ab74Stianshuliang 	if (ios->timing == MMC_TIMING_MMC_DDR52)
41*e382ab74Stianshuliang 		val |= SDMMC_ENABLE_PHASE;
42*e382ab74Stianshuliang 	else
43*e382ab74Stianshuliang 		val &= ~SDMMC_ENABLE_PHASE;
44*e382ab74Stianshuliang 	mci_writel(host, ENABLE_SHIFT, val);
45*e382ab74Stianshuliang 
46*e382ab74Stianshuliang 	val = mci_readl(host, DDR_REG);
47*e382ab74Stianshuliang 	if (ios->timing == MMC_TIMING_MMC_HS400)
48*e382ab74Stianshuliang 		val |= SDMMC_DDR_HS400;
49*e382ab74Stianshuliang 	else
50*e382ab74Stianshuliang 		val &= ~SDMMC_DDR_HS400;
51*e382ab74Stianshuliang 	mci_writel(host, DDR_REG, val);
52*e382ab74Stianshuliang 
53*e382ab74Stianshuliang 	if (ios->timing == MMC_TIMING_MMC_HS ||
54*e382ab74Stianshuliang 	    ios->timing == MMC_TIMING_LEGACY)
55*e382ab74Stianshuliang 		clk_set_phase(priv->drive_clk, 180);
56*e382ab74Stianshuliang 	else if (ios->timing == MMC_TIMING_MMC_HS200)
57*e382ab74Stianshuliang 		clk_set_phase(priv->drive_clk, 135);
58*e382ab74Stianshuliang }
59*e382ab74Stianshuliang 
60*e382ab74Stianshuliang static int dw_mci_hi3798cv200_execute_tuning(struct dw_mci_slot *slot,
61*e382ab74Stianshuliang 					     u32 opcode)
62*e382ab74Stianshuliang {
63*e382ab74Stianshuliang 	int degrees[] = { 0, 45, 90, 135, 180, 225, 270, 315 };
64*e382ab74Stianshuliang 	struct dw_mci *host = slot->host;
65*e382ab74Stianshuliang 	struct hi3798cv200_priv *priv = host->priv;
66*e382ab74Stianshuliang 	int raise_point = -1, fall_point = -1;
67*e382ab74Stianshuliang 	int err, prev_err = -1;
68*e382ab74Stianshuliang 	int found = 0;
69*e382ab74Stianshuliang 	int i;
70*e382ab74Stianshuliang 
71*e382ab74Stianshuliang 	for (i = 0; i < ARRAY_SIZE(degrees); i++) {
72*e382ab74Stianshuliang 		clk_set_phase(priv->sample_clk, degrees[i]);
73*e382ab74Stianshuliang 		mci_writel(host, RINTSTS, ALL_INT_CLR);
74*e382ab74Stianshuliang 
75*e382ab74Stianshuliang 		err = mmc_send_tuning(slot->mmc, opcode, NULL);
76*e382ab74Stianshuliang 		if (!err)
77*e382ab74Stianshuliang 			found = 1;
78*e382ab74Stianshuliang 
79*e382ab74Stianshuliang 		if (i > 0) {
80*e382ab74Stianshuliang 			if (err && !prev_err)
81*e382ab74Stianshuliang 				fall_point = i - 1;
82*e382ab74Stianshuliang 			if (!err && prev_err)
83*e382ab74Stianshuliang 				raise_point = i;
84*e382ab74Stianshuliang 		}
85*e382ab74Stianshuliang 
86*e382ab74Stianshuliang 		if (raise_point != -1 && fall_point != -1)
87*e382ab74Stianshuliang 			goto tuning_out;
88*e382ab74Stianshuliang 
89*e382ab74Stianshuliang 		prev_err = err;
90*e382ab74Stianshuliang 		err = 0;
91*e382ab74Stianshuliang 	}
92*e382ab74Stianshuliang 
93*e382ab74Stianshuliang tuning_out:
94*e382ab74Stianshuliang 	if (found) {
95*e382ab74Stianshuliang 		if (raise_point == -1)
96*e382ab74Stianshuliang 			raise_point = 0;
97*e382ab74Stianshuliang 		if (fall_point == -1)
98*e382ab74Stianshuliang 			fall_point = ARRAY_SIZE(degrees) - 1;
99*e382ab74Stianshuliang 		if (fall_point < raise_point) {
100*e382ab74Stianshuliang 			if ((raise_point + fall_point) >
101*e382ab74Stianshuliang 			    (ARRAY_SIZE(degrees) - 1))
102*e382ab74Stianshuliang 				i = fall_point / 2;
103*e382ab74Stianshuliang 			else
104*e382ab74Stianshuliang 				i = (raise_point + ARRAY_SIZE(degrees) - 1) / 2;
105*e382ab74Stianshuliang 		} else {
106*e382ab74Stianshuliang 			i = (raise_point + fall_point) / 2;
107*e382ab74Stianshuliang 		}
108*e382ab74Stianshuliang 
109*e382ab74Stianshuliang 		clk_set_phase(priv->sample_clk, degrees[i]);
110*e382ab74Stianshuliang 		dev_dbg(host->dev, "Tuning clk_sample[%d, %d], set[%d]\n",
111*e382ab74Stianshuliang 			raise_point, fall_point, degrees[i]);
112*e382ab74Stianshuliang 	} else {
113*e382ab74Stianshuliang 		dev_err(host->dev, "No valid clk_sample shift! use default\n");
114*e382ab74Stianshuliang 		err = -EINVAL;
115*e382ab74Stianshuliang 	}
116*e382ab74Stianshuliang 
117*e382ab74Stianshuliang 	mci_writel(host, RINTSTS, ALL_INT_CLR);
118*e382ab74Stianshuliang 	return err;
119*e382ab74Stianshuliang }
120*e382ab74Stianshuliang 
121*e382ab74Stianshuliang static int dw_mci_hi3798cv200_init(struct dw_mci *host)
122*e382ab74Stianshuliang {
123*e382ab74Stianshuliang 	struct hi3798cv200_priv *priv;
124*e382ab74Stianshuliang 	int ret;
125*e382ab74Stianshuliang 
126*e382ab74Stianshuliang 	priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
127*e382ab74Stianshuliang 	if (!priv)
128*e382ab74Stianshuliang 		return -ENOMEM;
129*e382ab74Stianshuliang 
130*e382ab74Stianshuliang 	priv->sample_clk = devm_clk_get(host->dev, "ciu-sample");
131*e382ab74Stianshuliang 	if (IS_ERR(priv->sample_clk)) {
132*e382ab74Stianshuliang 		dev_err(host->dev, "failed to get ciu-sample clock\n");
133*e382ab74Stianshuliang 		return PTR_ERR(priv->sample_clk);
134*e382ab74Stianshuliang 	}
135*e382ab74Stianshuliang 
136*e382ab74Stianshuliang 	priv->drive_clk = devm_clk_get(host->dev, "ciu-drive");
137*e382ab74Stianshuliang 	if (IS_ERR(priv->drive_clk)) {
138*e382ab74Stianshuliang 		dev_err(host->dev, "failed to get ciu-drive clock\n");
139*e382ab74Stianshuliang 		return PTR_ERR(priv->drive_clk);
140*e382ab74Stianshuliang 	}
141*e382ab74Stianshuliang 
142*e382ab74Stianshuliang 	ret = clk_prepare_enable(priv->sample_clk);
143*e382ab74Stianshuliang 	if (ret) {
144*e382ab74Stianshuliang 		dev_err(host->dev, "failed to enable ciu-sample clock\n");
145*e382ab74Stianshuliang 		return ret;
146*e382ab74Stianshuliang 	}
147*e382ab74Stianshuliang 
148*e382ab74Stianshuliang 	ret = clk_prepare_enable(priv->drive_clk);
149*e382ab74Stianshuliang 	if (ret) {
150*e382ab74Stianshuliang 		dev_err(host->dev, "failed to enable ciu-drive clock\n");
151*e382ab74Stianshuliang 		goto disable_sample_clk;
152*e382ab74Stianshuliang 	}
153*e382ab74Stianshuliang 
154*e382ab74Stianshuliang 	host->priv = priv;
155*e382ab74Stianshuliang 	return 0;
156*e382ab74Stianshuliang 
157*e382ab74Stianshuliang disable_sample_clk:
158*e382ab74Stianshuliang 	clk_disable_unprepare(priv->sample_clk);
159*e382ab74Stianshuliang 	return ret;
160*e382ab74Stianshuliang }
161*e382ab74Stianshuliang 
162*e382ab74Stianshuliang static const struct dw_mci_drv_data hi3798cv200_data = {
163*e382ab74Stianshuliang 	.init = dw_mci_hi3798cv200_init,
164*e382ab74Stianshuliang 	.set_ios = dw_mci_hi3798cv200_set_ios,
165*e382ab74Stianshuliang 	.execute_tuning = dw_mci_hi3798cv200_execute_tuning,
166*e382ab74Stianshuliang };
167*e382ab74Stianshuliang 
168*e382ab74Stianshuliang static int dw_mci_hi3798cv200_probe(struct platform_device *pdev)
169*e382ab74Stianshuliang {
170*e382ab74Stianshuliang 	return dw_mci_pltfm_register(pdev, &hi3798cv200_data);
171*e382ab74Stianshuliang }
172*e382ab74Stianshuliang 
173*e382ab74Stianshuliang static int dw_mci_hi3798cv200_remove(struct platform_device *pdev)
174*e382ab74Stianshuliang {
175*e382ab74Stianshuliang 	struct dw_mci *host = platform_get_drvdata(pdev);
176*e382ab74Stianshuliang 	struct hi3798cv200_priv *priv = host->priv;
177*e382ab74Stianshuliang 
178*e382ab74Stianshuliang 	clk_disable_unprepare(priv->drive_clk);
179*e382ab74Stianshuliang 	clk_disable_unprepare(priv->sample_clk);
180*e382ab74Stianshuliang 
181*e382ab74Stianshuliang 	return dw_mci_pltfm_remove(pdev);
182*e382ab74Stianshuliang }
183*e382ab74Stianshuliang 
184*e382ab74Stianshuliang static const struct of_device_id dw_mci_hi3798cv200_match[] = {
185*e382ab74Stianshuliang 	{ .compatible = "hisilicon,hi3798cv200-dw-mshc", },
186*e382ab74Stianshuliang 	{},
187*e382ab74Stianshuliang };
188*e382ab74Stianshuliang 
189*e382ab74Stianshuliang MODULE_DEVICE_TABLE(of, dw_mci_hi3798cv200_match);
190*e382ab74Stianshuliang static struct platform_driver dw_mci_hi3798cv200_driver = {
191*e382ab74Stianshuliang 	.probe = dw_mci_hi3798cv200_probe,
192*e382ab74Stianshuliang 	.remove = dw_mci_hi3798cv200_remove,
193*e382ab74Stianshuliang 	.driver = {
194*e382ab74Stianshuliang 		.name = "dwmmc_hi3798cv200",
195*e382ab74Stianshuliang 		.of_match_table = dw_mci_hi3798cv200_match,
196*e382ab74Stianshuliang 	},
197*e382ab74Stianshuliang };
198*e382ab74Stianshuliang module_platform_driver(dw_mci_hi3798cv200_driver);
199*e382ab74Stianshuliang 
200*e382ab74Stianshuliang MODULE_DESCRIPTION("HiSilicon Hi3798CV200 Specific DW-MSHC Driver Extension");
201*e382ab74Stianshuliang MODULE_LICENSE("GPL v2");
202*e382ab74Stianshuliang MODULE_ALIAS("platform:dwmmc_hi3798cv200");
203