xref: /openbmc/linux/drivers/mmc/host/sdhci_f_sdh30.c (revision c1d3fb8a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/drivers/mmc/host/sdhci_f_sdh30.c
4  *
5  * Copyright (C) 2013 - 2015 Fujitsu Semiconductor, Ltd
6  *              Vincent Yang <vincent.yang@tw.fujitsu.com>
7  * Copyright (C) 2015 Linaro Ltd  Andy Green <andy.green@linaro.org>
8  */
9 
10 #include <linux/acpi.h>
11 #include <linux/err.h>
12 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/property.h>
16 #include <linux/clk.h>
17 
18 #include "sdhci-pltfm.h"
19 #include "sdhci_f_sdh30.h"
20 
21 struct f_sdhost_priv {
22 	struct clk *clk_iface;
23 	struct clk *clk;
24 	u32 vendor_hs200;
25 	struct device *dev;
26 	bool enable_cmd_dat_delay;
27 };
28 
29 static void sdhci_f_sdh30_soft_voltage_switch(struct sdhci_host *host)
30 {
31 	struct f_sdhost_priv *priv = sdhci_priv(host);
32 	u32 ctrl = 0;
33 
34 	usleep_range(2500, 3000);
35 	ctrl = sdhci_readl(host, F_SDH30_IO_CONTROL2);
36 	ctrl |= F_SDH30_CRES_O_DN;
37 	sdhci_writel(host, ctrl, F_SDH30_IO_CONTROL2);
38 	ctrl |= F_SDH30_MSEL_O_1_8;
39 	sdhci_writel(host, ctrl, F_SDH30_IO_CONTROL2);
40 
41 	ctrl &= ~F_SDH30_CRES_O_DN;
42 	sdhci_writel(host, ctrl, F_SDH30_IO_CONTROL2);
43 	usleep_range(2500, 3000);
44 
45 	if (priv->vendor_hs200) {
46 		dev_info(priv->dev, "%s: setting hs200\n", __func__);
47 		ctrl = sdhci_readl(host, F_SDH30_ESD_CONTROL);
48 		ctrl |= priv->vendor_hs200;
49 		sdhci_writel(host, ctrl, F_SDH30_ESD_CONTROL);
50 	}
51 
52 	ctrl = sdhci_readl(host, F_SDH30_TUNING_SETTING);
53 	ctrl |= F_SDH30_CMD_CHK_DIS;
54 	sdhci_writel(host, ctrl, F_SDH30_TUNING_SETTING);
55 }
56 
57 static unsigned int sdhci_f_sdh30_get_min_clock(struct sdhci_host *host)
58 {
59 	return F_SDH30_MIN_CLOCK;
60 }
61 
62 static void sdhci_f_sdh30_reset(struct sdhci_host *host, u8 mask)
63 {
64 	struct f_sdhost_priv *priv = sdhci_priv(host);
65 	u32 ctl;
66 
67 	if (sdhci_readw(host, SDHCI_CLOCK_CONTROL) == 0)
68 		sdhci_writew(host, 0xBC01, SDHCI_CLOCK_CONTROL);
69 
70 	sdhci_reset(host, mask);
71 
72 	if (priv->enable_cmd_dat_delay) {
73 		ctl = sdhci_readl(host, F_SDH30_ESD_CONTROL);
74 		ctl |= F_SDH30_CMD_DAT_DELAY;
75 		sdhci_writel(host, ctl, F_SDH30_ESD_CONTROL);
76 	}
77 }
78 
79 static const struct sdhci_ops sdhci_f_sdh30_ops = {
80 	.voltage_switch = sdhci_f_sdh30_soft_voltage_switch,
81 	.get_min_clock = sdhci_f_sdh30_get_min_clock,
82 	.reset = sdhci_f_sdh30_reset,
83 	.set_clock = sdhci_set_clock,
84 	.set_bus_width = sdhci_set_bus_width,
85 	.set_uhs_signaling = sdhci_set_uhs_signaling,
86 };
87 
88 static int sdhci_f_sdh30_probe(struct platform_device *pdev)
89 {
90 	struct sdhci_host *host;
91 	struct device *dev = &pdev->dev;
92 	struct resource *res;
93 	int irq, ctrl = 0, ret = 0;
94 	struct f_sdhost_priv *priv;
95 	u32 reg = 0;
96 
97 	irq = platform_get_irq(pdev, 0);
98 	if (irq < 0)
99 		return irq;
100 
101 	host = sdhci_alloc_host(dev, sizeof(struct f_sdhost_priv));
102 	if (IS_ERR(host))
103 		return PTR_ERR(host);
104 
105 	priv = sdhci_priv(host);
106 	priv->dev = dev;
107 
108 	host->quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
109 		       SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
110 	host->quirks2 = SDHCI_QUIRK2_SUPPORT_SINGLE |
111 			SDHCI_QUIRK2_TUNING_WORK_AROUND;
112 
113 	priv->enable_cmd_dat_delay = device_property_read_bool(dev,
114 						"fujitsu,cmd-dat-delay-select");
115 
116 	ret = mmc_of_parse(host->mmc);
117 	if (ret)
118 		goto err;
119 
120 	platform_set_drvdata(pdev, host);
121 
122 	host->hw_name = "f_sdh30";
123 	host->ops = &sdhci_f_sdh30_ops;
124 	host->irq = irq;
125 
126 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
127 	host->ioaddr = devm_ioremap_resource(&pdev->dev, res);
128 	if (IS_ERR(host->ioaddr)) {
129 		ret = PTR_ERR(host->ioaddr);
130 		goto err;
131 	}
132 
133 	if (dev_of_node(dev)) {
134 		sdhci_get_of_property(pdev);
135 
136 		priv->clk_iface = devm_clk_get(&pdev->dev, "iface");
137 		if (IS_ERR(priv->clk_iface)) {
138 			ret = PTR_ERR(priv->clk_iface);
139 			goto err;
140 		}
141 
142 		ret = clk_prepare_enable(priv->clk_iface);
143 		if (ret)
144 			goto err;
145 
146 		priv->clk = devm_clk_get(&pdev->dev, "core");
147 		if (IS_ERR(priv->clk)) {
148 			ret = PTR_ERR(priv->clk);
149 			goto err_clk;
150 		}
151 
152 		ret = clk_prepare_enable(priv->clk);
153 		if (ret)
154 			goto err_clk;
155 	}
156 
157 	/* init vendor specific regs */
158 	ctrl = sdhci_readw(host, F_SDH30_AHB_CONFIG);
159 	ctrl |= F_SDH30_SIN | F_SDH30_AHB_INCR_16 | F_SDH30_AHB_INCR_8 |
160 		F_SDH30_AHB_INCR_4;
161 	ctrl &= ~(F_SDH30_AHB_BIGED | F_SDH30_BUSLOCK_EN);
162 	sdhci_writew(host, ctrl, F_SDH30_AHB_CONFIG);
163 
164 	reg = sdhci_readl(host, F_SDH30_ESD_CONTROL);
165 	sdhci_writel(host, reg & ~F_SDH30_EMMC_RST, F_SDH30_ESD_CONTROL);
166 	msleep(20);
167 	sdhci_writel(host, reg | F_SDH30_EMMC_RST, F_SDH30_ESD_CONTROL);
168 
169 	reg = sdhci_readl(host, SDHCI_CAPABILITIES);
170 	if (reg & SDHCI_CAN_DO_8BIT)
171 		priv->vendor_hs200 = F_SDH30_EMMC_HS200;
172 
173 	ret = sdhci_add_host(host);
174 	if (ret)
175 		goto err_add_host;
176 
177 	return 0;
178 
179 err_add_host:
180 	clk_disable_unprepare(priv->clk);
181 err_clk:
182 	clk_disable_unprepare(priv->clk_iface);
183 err:
184 	sdhci_free_host(host);
185 	return ret;
186 }
187 
188 static int sdhci_f_sdh30_remove(struct platform_device *pdev)
189 {
190 	struct sdhci_host *host = platform_get_drvdata(pdev);
191 	struct f_sdhost_priv *priv = sdhci_priv(host);
192 
193 	sdhci_remove_host(host, readl(host->ioaddr + SDHCI_INT_STATUS) ==
194 			  0xffffffff);
195 
196 	clk_disable_unprepare(priv->clk_iface);
197 	clk_disable_unprepare(priv->clk);
198 
199 	sdhci_free_host(host);
200 	platform_set_drvdata(pdev, NULL);
201 
202 	return 0;
203 }
204 
205 #ifdef CONFIG_OF
206 static const struct of_device_id f_sdh30_dt_ids[] = {
207 	{ .compatible = "fujitsu,mb86s70-sdhci-3.0" },
208 	{ /* sentinel */ }
209 };
210 MODULE_DEVICE_TABLE(of, f_sdh30_dt_ids);
211 #endif
212 
213 #ifdef CONFIG_ACPI
214 static const struct acpi_device_id f_sdh30_acpi_ids[] = {
215 	{ "SCX0002" },
216 	{ /* sentinel */ }
217 };
218 MODULE_DEVICE_TABLE(acpi, f_sdh30_acpi_ids);
219 #endif
220 
221 static struct platform_driver sdhci_f_sdh30_driver = {
222 	.driver = {
223 		.name = "f_sdh30",
224 		.of_match_table = of_match_ptr(f_sdh30_dt_ids),
225 		.acpi_match_table = ACPI_PTR(f_sdh30_acpi_ids),
226 		.pm	= &sdhci_pltfm_pmops,
227 	},
228 	.probe	= sdhci_f_sdh30_probe,
229 	.remove	= sdhci_f_sdh30_remove,
230 };
231 
232 module_platform_driver(sdhci_f_sdh30_driver);
233 
234 MODULE_DESCRIPTION("F_SDH30 SD Card Controller driver");
235 MODULE_LICENSE("GPL v2");
236 MODULE_AUTHOR("FUJITSU SEMICONDUCTOR LTD.");
237 MODULE_ALIAS("platform:f_sdh30");
238