1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2022 StarFive, Inc <clivia.cai@starfivetech.com>
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.
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>
28 #include "dw_mmc-pltfm.h"
30 #define ALL_INT_CLR 0x1ffff
31 #define MAX_DELAY_CHAIN 32
32 struct starfive_priv {
34 struct regmap *reg_syscon;
40 static unsigned long dw_mci_starfive_caps[] = {
46 static void dw_mci_starfive_set_ios(struct dw_mci *host, struct mmc_ios *ios)
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);
55 dev_dbg(host->dev, "Use an external frequency divider %uHz\n", ios->clock);
56 host->bus_hz = clk_get_rate(host->ciu_clk);
58 dev_dbg(host->dev, "Using the internal divider\n");
62 static int dw_mci_starfive_execute_tuning(struct dw_mci_slot *slot,
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;
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);
79 mci_writel(host, RINTSTS, ALL_INT_CLR);
81 err = mmc_send_tuning(slot->mmc, opcode, NULL);
92 if (raise_point != -1 && fall_point != -1)
101 if (raise_point == -1)
103 if (fall_point == -1)
104 fall_point = grade - 1;
105 if (fall_point < raise_point) {
106 if ((raise_point + fall_point) >
110 i = (raise_point + grade - 1) / 2;
112 i = (raise_point + fall_point) / 2;
115 regval = i << priv->syscon_shift;
116 err = regmap_update_bits(priv->reg_syscon, priv->syscon_offset, priv->syscon_mask, regval);
119 dev_dbg(host->dev, "Found valid delay chain! use it [delay=%d]\n", i);
121 dev_err(host->dev, "No valid delay chain! use default\n");
125 mci_writel(host, RINTSTS, ALL_INT_CLR);
129 static int dw_mci_starfive_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
132 struct dw_mci_slot *slot = mmc_priv(mmc);
133 struct dw_mci *host = slot->host;
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);
143 if (!IS_ERR(mmc->supply.vqmmc)) {
144 ret = mmc_regulator_set_vqmmc(mmc, ios);
146 dev_err(host->dev, "Regulator set error %d\n", ret);
151 /* We should delay 20ms wait for timing setting finished. */
156 static int dw_mci_starfive_parse_dt(struct dw_mci *host)
158 struct of_phandle_args args;
159 struct starfive_priv *priv;
162 priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
166 ret = of_parse_phandle_with_fixed_args(host->dev->of_node,
167 "starfive,sys-syscon", 3, 0, &args);
169 dev_err(host->dev, "Failed to parse starfive,sys-syscon\n");
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);
178 priv->syscon_offset = args.args[0];
179 priv->syscon_shift = args.args[1];
180 priv->syscon_mask = args.args[2];
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,
196 static int dw_mci_starfive_probe(struct platform_device *pdev)
198 return dw_mci_pltfm_register(pdev, &starfive_data);
201 static int dw_mci_starfive_remove(struct platform_device *pdev)
203 return dw_mci_pltfm_remove(pdev);
206 static const struct of_device_id dw_mci_starfive_match[] = {
207 { .compatible = "starfive,jh7110-sdio", },
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,
216 .name = "dwmmc_starfive",
217 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
218 .of_match_table = dw_mci_starfive_match,
221 module_platform_driver(dw_mci_starfive_driver);
223 MODULE_DESCRIPTION("StarFive JH7110 Specific DW-MSHC Driver Extension");
224 MODULE_LICENSE("GPL v2");
225 MODULE_ALIAS("platform:dwmmc_starfive");