1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * sdhci-pltfm.c Support for SDHCI platform devices
4  * Copyright (c) 2009 Intel Corporation
5  *
6  * Copyright (c) 2007, 2011 Freescale Semiconductor, Inc.
7  * Copyright (c) 2009 MontaVista Software, Inc.
8  *
9  * Authors: Xiaobo Xie <X.Xie@freescale.com>
10  *	    Anton Vorontsov <avorontsov@ru.mvista.com>
11  */
12 
13 /* Supports:
14  * SDHCI platform devices
15  *
16  * Inspired by sdhci-pci.c, by Pierre Ossman
17  */
18 
19 #include <linux/err.h>
20 #include <linux/module.h>
21 #include <linux/property.h>
22 #include <linux/of.h>
23 #ifdef CONFIG_PPC
24 #include <asm/machdep.h>
25 #endif
26 #include "sdhci-pltfm.h"
27 
sdhci_pltfm_clk_get_max_clock(struct sdhci_host * host)28 unsigned int sdhci_pltfm_clk_get_max_clock(struct sdhci_host *host)
29 {
30 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
31 
32 	return clk_get_rate(pltfm_host->clk);
33 }
34 EXPORT_SYMBOL_GPL(sdhci_pltfm_clk_get_max_clock);
35 
36 static const struct sdhci_ops sdhci_pltfm_ops = {
37 	.set_clock = sdhci_set_clock,
38 	.set_bus_width = sdhci_set_bus_width,
39 	.reset = sdhci_reset,
40 	.set_uhs_signaling = sdhci_set_uhs_signaling,
41 };
42 
sdhci_wp_inverted(struct device * dev)43 static bool sdhci_wp_inverted(struct device *dev)
44 {
45 	if (device_property_present(dev, "sdhci,wp-inverted") ||
46 	    device_property_present(dev, "wp-inverted"))
47 		return true;
48 
49 	/* Old device trees don't have the wp-inverted property. */
50 #ifdef CONFIG_PPC
51 	return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
52 #else
53 	return false;
54 #endif /* CONFIG_PPC */
55 }
56 
sdhci_get_compatibility(struct platform_device * pdev)57 static void sdhci_get_compatibility(struct platform_device *pdev)
58 {
59 	struct sdhci_host *host = platform_get_drvdata(pdev);
60 	struct device_node *np = pdev->dev.of_node;
61 
62 	if (!np)
63 		return;
64 
65 	if (of_device_is_compatible(np, "fsl,p2020-rev1-esdhc"))
66 		host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
67 
68 	if (of_device_is_compatible(np, "fsl,p2020-esdhc") ||
69 	    of_device_is_compatible(np, "fsl,p1010-esdhc") ||
70 	    of_device_is_compatible(np, "fsl,t4240-esdhc") ||
71 	    of_device_is_compatible(np, "fsl,mpc8536-esdhc"))
72 		host->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
73 }
74 
sdhci_get_property(struct platform_device * pdev)75 void sdhci_get_property(struct platform_device *pdev)
76 {
77 	struct device *dev = &pdev->dev;
78 	struct sdhci_host *host = platform_get_drvdata(pdev);
79 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
80 	u32 bus_width;
81 
82 	if (device_property_present(dev, "sdhci,auto-cmd12"))
83 		host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
84 
85 	if (device_property_present(dev, "sdhci,1-bit-only") ||
86 	    (device_property_read_u32(dev, "bus-width", &bus_width) == 0 &&
87 	    bus_width == 1))
88 		host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
89 
90 	if (sdhci_wp_inverted(dev))
91 		host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
92 
93 	if (device_property_present(dev, "broken-cd"))
94 		host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
95 
96 	if (device_property_present(dev, "no-1-8-v"))
97 		host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
98 
99 	sdhci_get_compatibility(pdev);
100 
101 	device_property_read_u32(dev, "clock-frequency", &pltfm_host->clock);
102 
103 	if (device_property_present(dev, "keep-power-in-suspend"))
104 		host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
105 
106 	if (device_property_read_bool(dev, "wakeup-source") ||
107 	    device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
108 		host->mmc->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
109 }
110 EXPORT_SYMBOL_GPL(sdhci_get_property);
111 
sdhci_pltfm_init(struct platform_device * pdev,const struct sdhci_pltfm_data * pdata,size_t priv_size)112 struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
113 				    const struct sdhci_pltfm_data *pdata,
114 				    size_t priv_size)
115 {
116 	struct sdhci_host *host;
117 	void __iomem *ioaddr;
118 	int irq, ret;
119 
120 	ioaddr = devm_platform_ioremap_resource(pdev, 0);
121 	if (IS_ERR(ioaddr)) {
122 		ret = PTR_ERR(ioaddr);
123 		goto err;
124 	}
125 
126 	irq = platform_get_irq(pdev, 0);
127 	if (irq < 0) {
128 		ret = irq;
129 		goto err;
130 	}
131 
132 	host = sdhci_alloc_host(&pdev->dev,
133 		sizeof(struct sdhci_pltfm_host) + priv_size);
134 
135 	if (IS_ERR(host)) {
136 		ret = PTR_ERR(host);
137 		goto err;
138 	}
139 
140 	host->ioaddr = ioaddr;
141 	host->irq = irq;
142 	host->hw_name = dev_name(&pdev->dev);
143 	if (pdata && pdata->ops)
144 		host->ops = pdata->ops;
145 	else
146 		host->ops = &sdhci_pltfm_ops;
147 	if (pdata) {
148 		host->quirks = pdata->quirks;
149 		host->quirks2 = pdata->quirks2;
150 	}
151 
152 	platform_set_drvdata(pdev, host);
153 
154 	return host;
155 err:
156 	dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
157 	return ERR_PTR(ret);
158 }
159 EXPORT_SYMBOL_GPL(sdhci_pltfm_init);
160 
sdhci_pltfm_free(struct platform_device * pdev)161 void sdhci_pltfm_free(struct platform_device *pdev)
162 {
163 	struct sdhci_host *host = platform_get_drvdata(pdev);
164 
165 	sdhci_free_host(host);
166 }
167 EXPORT_SYMBOL_GPL(sdhci_pltfm_free);
168 
sdhci_pltfm_init_and_add_host(struct platform_device * pdev,const struct sdhci_pltfm_data * pdata,size_t priv_size)169 int sdhci_pltfm_init_and_add_host(struct platform_device *pdev,
170 				  const struct sdhci_pltfm_data *pdata,
171 				  size_t priv_size)
172 {
173 	struct sdhci_host *host;
174 	int ret = 0;
175 
176 	host = sdhci_pltfm_init(pdev, pdata, priv_size);
177 	if (IS_ERR(host))
178 		return PTR_ERR(host);
179 
180 	sdhci_get_property(pdev);
181 
182 	ret = sdhci_add_host(host);
183 	if (ret)
184 		sdhci_pltfm_free(pdev);
185 
186 	return ret;
187 }
188 EXPORT_SYMBOL_GPL(sdhci_pltfm_init_and_add_host);
189 
sdhci_pltfm_remove(struct platform_device * pdev)190 void sdhci_pltfm_remove(struct platform_device *pdev)
191 {
192 	struct sdhci_host *host = platform_get_drvdata(pdev);
193 	int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
194 
195 	sdhci_remove_host(host, dead);
196 	sdhci_pltfm_free(pdev);
197 }
198 EXPORT_SYMBOL_GPL(sdhci_pltfm_remove);
199 
200 #ifdef CONFIG_PM_SLEEP
sdhci_pltfm_suspend(struct device * dev)201 int sdhci_pltfm_suspend(struct device *dev)
202 {
203 	struct sdhci_host *host = dev_get_drvdata(dev);
204 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
205 	int ret;
206 
207 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
208 		mmc_retune_needed(host->mmc);
209 
210 	ret = sdhci_suspend_host(host);
211 	if (ret)
212 		return ret;
213 
214 	clk_disable_unprepare(pltfm_host->clk);
215 
216 	return 0;
217 }
218 EXPORT_SYMBOL_GPL(sdhci_pltfm_suspend);
219 
sdhci_pltfm_resume(struct device * dev)220 int sdhci_pltfm_resume(struct device *dev)
221 {
222 	struct sdhci_host *host = dev_get_drvdata(dev);
223 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
224 	int ret;
225 
226 	ret = clk_prepare_enable(pltfm_host->clk);
227 	if (ret)
228 		return ret;
229 
230 	ret = sdhci_resume_host(host);
231 	if (ret)
232 		clk_disable_unprepare(pltfm_host->clk);
233 
234 	return ret;
235 }
236 EXPORT_SYMBOL_GPL(sdhci_pltfm_resume);
237 #endif
238 
239 const struct dev_pm_ops sdhci_pltfm_pmops = {
240 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_pltfm_suspend, sdhci_pltfm_resume)
241 };
242 EXPORT_SYMBOL_GPL(sdhci_pltfm_pmops);
243 
sdhci_pltfm_drv_init(void)244 static int __init sdhci_pltfm_drv_init(void)
245 {
246 	pr_info("sdhci-pltfm: SDHCI platform and OF driver helper\n");
247 
248 	return 0;
249 }
250 module_init(sdhci_pltfm_drv_init);
251 
sdhci_pltfm_drv_exit(void)252 static void __exit sdhci_pltfm_drv_exit(void)
253 {
254 }
255 module_exit(sdhci_pltfm_drv_exit);
256 
257 MODULE_DESCRIPTION("SDHCI platform and OF driver helper");
258 MODULE_AUTHOR("Intel Corporation");
259 MODULE_LICENSE("GPL v2");
260