1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2022 StarFive, Inc <clivia.cai@starfivetech.com>
8 #include <linux/delay.h>
9 #include <linux/gpio.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/mmc/host.h>
12 #include <linux/module.h>
13 #include <linux/of_address.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/consumer.h>
20 #include "dw_mmc-pltfm.h"
22 #define ALL_INT_CLR 0x1ffff
23 #define MAX_DELAY_CHAIN 32
25 struct starfive_priv {
27 struct regmap *reg_syscon;
33 static unsigned long dw_mci_starfive_caps[] = {
39 static int dw_mci_starfive_execute_tuning(struct dw_mci_slot *slot,
42 static const int grade = MAX_DELAY_CHAIN;
43 struct dw_mci *host = slot->host;
44 struct starfive_priv *priv = host->priv;
45 int raise_point = -1, fall_point = -1;
46 int err, prev_err = -1;
51 for (i = 0; i < grade; i++) {
52 regval = i << priv->syscon_shift;
53 err = regmap_update_bits(priv->reg_syscon, priv->syscon_offset,
54 priv->syscon_mask, regval);
57 mci_writel(host, RINTSTS, ALL_INT_CLR);
59 err = mmc_send_tuning(slot->mmc, opcode, NULL);
70 if (raise_point != -1 && fall_point != -1)
79 if (raise_point == -1)
82 fall_point = grade - 1;
83 if (fall_point < raise_point) {
84 if ((raise_point + fall_point) >
88 i = (raise_point + grade - 1) / 2;
90 i = (raise_point + fall_point) / 2;
93 regval = i << priv->syscon_shift;
94 err = regmap_update_bits(priv->reg_syscon, priv->syscon_offset,
95 priv->syscon_mask, regval);
98 mci_writel(host, RINTSTS, ALL_INT_CLR);
100 dev_dbg(host->dev, "Found valid delay chain! use it [delay=%d]\n", i);
102 dev_err(host->dev, "No valid delay chain! use default\n");
106 mci_writel(host, RINTSTS, ALL_INT_CLR);
110 static int dw_mci_starfive_parse_dt(struct dw_mci *host)
112 struct of_phandle_args args;
113 struct starfive_priv *priv;
116 priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
120 ret = of_parse_phandle_with_fixed_args(host->dev->of_node,
121 "starfive,sys-syscon", 3, 0, &args);
123 dev_err(host->dev, "Failed to parse starfive,sys-syscon\n");
127 priv->reg_syscon = syscon_node_to_regmap(args.np);
128 of_node_put(args.np);
129 if (IS_ERR(priv->reg_syscon))
130 return PTR_ERR(priv->reg_syscon);
132 priv->syscon_offset = args.args[0];
133 priv->syscon_shift = args.args[1];
134 priv->syscon_mask = args.args[2];
141 static const struct dw_mci_drv_data starfive_data = {
142 .caps = dw_mci_starfive_caps,
143 .num_caps = ARRAY_SIZE(dw_mci_starfive_caps),
144 .parse_dt = dw_mci_starfive_parse_dt,
145 .execute_tuning = dw_mci_starfive_execute_tuning,
148 static int dw_mci_starfive_probe(struct platform_device *pdev)
150 return dw_mci_pltfm_register(pdev, &starfive_data);
153 static int dw_mci_starfive_remove(struct platform_device *pdev)
155 return dw_mci_pltfm_remove(pdev);
158 static const struct of_device_id dw_mci_starfive_match[] = {
159 { .compatible = "starfive,jh7110-sdio", },
163 MODULE_DEVICE_TABLE(of, dw_mci_starfive_match);
164 static struct platform_driver dw_mci_starfive_driver = {
165 .probe = dw_mci_starfive_probe,
166 .remove = dw_mci_starfive_remove,
168 .name = "dwmmc_starfive",
169 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
170 .of_match_table = dw_mci_starfive_match,
173 module_platform_driver(dw_mci_starfive_driver);
175 MODULE_DESCRIPTION("StarFive JH7110 Specific DW-MSHC Driver Extension");
176 MODULE_LICENSE("GPL");
177 MODULE_ALIAS("platform:dwmmc_starfive");