b328e237a5867c0149fa4d587ab359b43388d252
[platform/kernel/linux-starfive.git] / drivers / mmc / host / dw_mmc-starfive.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2022 StarFive, Inc <clivia.cai@starfivetech.com>
4  *
5  * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING
6  * CUSTOMERS WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER
7  * FOR THEM TO SAVE TIME. AS A RESULT, STARFIVE SHALL NOT BE HELD LIABLE
8  * FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY
9  * CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE
10  * BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONNECTION
11  * WITH THEIR PRODUCTS.
12  */
13
14
15 #include <linux/clk.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/mmc/host.h>
18 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/gpio.h>
25 #include <linux/delay.h>
26
27 #include "dw_mmc.h"
28 #include "dw_mmc-pltfm.h"
29
30 #define ALL_INT_CLR             0x1ffff
31 #define MAX_DELAY_CHAIN         32
32 struct starfive_priv {
33         struct device *dev;
34         struct regmap *reg_syscon;
35         u32 syscon_offset;
36         u32 syscon_shift;
37         u32 syscon_mask;
38 };
39
40 static unsigned long dw_mci_starfive_caps[] = {
41         MMC_CAP_CMD23,
42         MMC_CAP_CMD23,
43         MMC_CAP_CMD23
44 };
45
46 static void dw_mci_starfive_set_ios(struct dw_mci *host, struct mmc_ios *ios)
47 {
48         int ret;
49         unsigned int clock;
50
51         if (ios->timing == MMC_TIMING_MMC_DDR52 || ios->timing == MMC_TIMING_UHS_DDR50) {
52                 clock = (ios->clock > 50000000 && ios->clock <= 52000000) ? 100000000 : ios->clock;
53                 ret = clk_set_rate(host->ciu_clk, clock);
54                 if (ret)
55                         dev_dbg(host->dev, "Use an external frequency divider %uHz\n", ios->clock);
56                 host->bus_hz = clk_get_rate(host->ciu_clk);
57         } else {
58                 dev_dbg(host->dev, "Using the internal divider\n");
59         }
60 }
61
62 static int dw_mci_starfive_execute_tuning(struct dw_mci_slot *slot,
63                                              u32 opcode)
64 {
65         static const int grade  = MAX_DELAY_CHAIN;
66         struct dw_mci *host = slot->host;
67         struct starfive_priv *priv = host->priv;
68         int raise_point = -1, fall_point = -1;
69         int err, prev_err = -1;
70         int found = 0;
71         int i;
72         u32 regval;
73
74         for (i = 0; i < grade; i++) {
75                 regval = i << priv->syscon_shift;
76                 err = regmap_update_bits(priv->reg_syscon, priv->syscon_offset, priv->syscon_mask, regval);
77                 if (err)
78                         return err;
79                 mci_writel(host, RINTSTS, ALL_INT_CLR);
80
81                 err = mmc_send_tuning(slot->mmc, opcode, NULL);
82                 if (!err)
83                         found = 1;
84
85                 if (i > 0) {
86                         if (err && !prev_err)
87                                 fall_point = i - 1;
88                         if (!err && prev_err)
89                                 raise_point = i;
90                 }
91
92                 if (raise_point != -1 && fall_point != -1)
93                         goto tuning_out;
94
95                 prev_err = err;
96                 err = 0;
97         }
98
99 tuning_out:
100         if (found) {
101                 if (raise_point == -1)
102                         raise_point = 0;
103                 if (fall_point == -1)
104                         fall_point = grade - 1;
105                 if (fall_point < raise_point) {
106                         if ((raise_point + fall_point) >
107                             (grade - 1))
108                                 i = fall_point / 2;
109                         else
110                                 i = (raise_point + grade - 1) / 2;
111                 } else {
112                         i = (raise_point + fall_point) / 2;
113                 }
114
115                 regval = i << priv->syscon_shift;
116                 err = regmap_update_bits(priv->reg_syscon, priv->syscon_offset, priv->syscon_mask, regval);
117                 if (err)
118                         return err;
119                 dev_dbg(host->dev, "Found valid delay chain! use it [delay=%d]\n", i);
120         } else {
121                 dev_err(host->dev, "No valid delay chain! use default\n");
122                 err = -EINVAL;
123         }
124
125         mci_writel(host, RINTSTS, ALL_INT_CLR);
126         return err;
127 }
128
129 static int dw_mci_starfive_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
130 {
131
132         struct dw_mci_slot *slot = mmc_priv(mmc);
133         struct dw_mci *host = slot->host;
134         u32 ret;
135
136         if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
137                 ret = gpio_direction_output(25, 0);
138         else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
139                 ret = gpio_direction_output(25, 1);
140         if (ret)
141                 return ret;
142
143         if (!IS_ERR(mmc->supply.vqmmc)) {
144                 ret = mmc_regulator_set_vqmmc(mmc, ios);
145                 if (ret < 0) {
146                         dev_err(host->dev, "Regulator set error %d\n", ret);
147                         return ret;
148                 }
149         }
150
151         /* We should delay 20ms wait for timing setting finished. */
152         mdelay(20);
153         return 0;
154 }
155
156 static int dw_mci_starfive_parse_dt(struct dw_mci *host)
157 {
158         struct of_phandle_args args;
159         struct starfive_priv *priv;
160         int ret;
161
162         priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
163         if (!priv)
164                 return -ENOMEM;
165
166         ret = of_parse_phandle_with_fixed_args(host->dev->of_node,
167                                                 "starfive,sys-syscon", 3, 0, &args);
168         if (ret) {
169                 dev_err(host->dev, "Failed to parse starfive,sys-syscon\n");
170                 return -EINVAL;
171         }
172
173         priv->reg_syscon = syscon_node_to_regmap(args.np);
174         of_node_put(args.np);
175         if (IS_ERR(priv->reg_syscon))
176                 return PTR_ERR(priv->reg_syscon);
177
178         priv->syscon_offset = args.args[0];
179         priv->syscon_shift  = args.args[1];
180         priv->syscon_mask   = args.args[2];
181
182         host->priv = priv;
183
184         return 0;
185 }
186
187 static const struct dw_mci_drv_data starfive_data = {
188         .caps = dw_mci_starfive_caps,
189         .num_caps = ARRAY_SIZE(dw_mci_starfive_caps),
190         .set_ios = dw_mci_starfive_set_ios,
191         .parse_dt = dw_mci_starfive_parse_dt,
192         .execute_tuning = dw_mci_starfive_execute_tuning,
193         .switch_voltage  = dw_mci_starfive_switch_voltage,
194 };
195
196 static int dw_mci_starfive_probe(struct platform_device *pdev)
197 {
198         return dw_mci_pltfm_register(pdev, &starfive_data);
199 }
200
201 static int dw_mci_starfive_remove(struct platform_device *pdev)
202 {
203         return dw_mci_pltfm_remove(pdev);
204 }
205
206 static const struct of_device_id dw_mci_starfive_match[] = {
207         { .compatible = "starfive,jh7110-sdio", },
208         {},
209 };
210
211 MODULE_DEVICE_TABLE(of, dw_mci_starfive_match);
212 static struct platform_driver dw_mci_starfive_driver = {
213         .probe = dw_mci_starfive_probe,
214         .remove = dw_mci_starfive_remove,
215         .driver = {
216                 .name = "dwmmc_starfive",
217                 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
218                 .of_match_table = dw_mci_starfive_match,
219         },
220 };
221 module_platform_driver(dw_mci_starfive_driver);
222
223 MODULE_DESCRIPTION("StarFive JH7110 Specific DW-MSHC Driver Extension");
224 MODULE_LICENSE("GPL v2");
225 MODULE_ALIAS("platform:dwmmc_starfive");