Debian Codegraph @

drivers/mmc/host/sdhci-of-k1.c

raw ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2023-2025 SpacemiT (Hangzhou) Technology Co. Ltd
4 * Copyright (c) 2025 Yixun Lan <[email protected]>
5 */
6
7 #include <linux/bitfield.h>
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/iopoll.h>
11 #include <linux/init.h>
12 #include <linux/mmc/card.h>
13 #include <linux/mmc/host.h>
14 #include <linux/mmc/mmc.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_device.h>
18 #include <linux/reset.h>
19 #include <linux/platform_device.h>
20
21 #include "sdhci.h"
22 #include "sdhci-pltfm.h"
23
24 #define SPACEMIT_SDHC_OP_EXT_REG 0x108
25 #define SDHC_OVRRD_CLK_OEN BIT(11)
26 #define SDHC_FORCE_CLK_ON BIT(12)
27
28 #define SPACEMIT_SDHC_LEGACY_CTRL_REG 0x10C
29 #define SDHC_GEN_PAD_CLK_ON BIT(6)
30
31 #define SPACEMIT_SDHC_MMC_CTRL_REG 0x114
32 #define SDHC_MISC_INT_EN BIT(1)
33 #define SDHC_MISC_INT BIT(2)
34 #define SDHC_ENHANCE_STROBE_EN BIT(8)
35 #define SDHC_MMC_HS400 BIT(9)
36 #define SDHC_MMC_HS200 BIT(10)
37 #define SDHC_MMC_CARD_MODE BIT(12)
38
39 #define SPACEMIT_SDHC_TX_CFG_REG 0x11C
40 #define SDHC_TX_INT_CLK_SEL BIT(30)
41 #define SDHC_TX_MUX_SEL BIT(31)
42
43 #define SPACEMIT_SDHC_PHY_CTRL_REG 0x160
44 #define SDHC_PHY_FUNC_EN BIT(0)
45 #define SDHC_PHY_PLL_LOCK BIT(1)
46 #define SDHC_HOST_LEGACY_MODE BIT(31)
47
48 #define SPACEMIT_SDHC_PHY_FUNC_REG 0x164
49 #define SDHC_PHY_TEST_EN BIT(7)
50 #define SDHC_HS200_USE_RFIFO BIT(15)
51
52 #define SPACEMIT_SDHC_PHY_DLLCFG 0x168
53 #define SDHC_DLL_PREDLY_NUM GENMASK(3, 2)
54 #define SDHC_DLL_FULLDLY_RANGE GENMASK(5, 4)
55 #define SDHC_DLL_VREG_CTRL GENMASK(7, 6)
56 #define SDHC_DLL_ENABLE BIT(31)
57
58 #define SPACEMIT_SDHC_PHY_DLLCFG1 0x16C
59 #define SDHC_DLL_REG1_CTRL GENMASK(7, 0)
60 #define SDHC_DLL_REG2_CTRL GENMASK(15, 8)
61 #define SDHC_DLL_REG3_CTRL GENMASK(23, 16)
62 #define SDHC_DLL_REG4_CTRL GENMASK(31, 24)
63
64 #define SPACEMIT_SDHC_PHY_DLLSTS 0x170
65 #define SDHC_DLL_LOCK_STATE BIT(0)
66
67 #define SPACEMIT_SDHC_PHY_PADCFG_REG 0x178
68 #define SDHC_PHY_DRIVE_SEL GENMASK(2, 0)
69 #define SDHC_RX_BIAS_CTRL BIT(5)
70
71 struct spacemit_sdhci_host {
72 struct clk *clk_core;
73 struct clk *clk_io;
74 };
75
76 /* All helper functions will update clr/set while preserve rest bits */
77 static inline void spacemit_sdhci_setbits(struct sdhci_host *host, u32 val, int reg)
78 {
79 sdhci_writel(host, sdhci_readl(host, reg) | val, reg);
80 }
81
82 static inline void spacemit_sdhci_clrbits(struct sdhci_host *host, u32 val, int reg)
83 {
84 sdhci_writel(host, sdhci_readl(host, reg) & ~val, reg);
85 }
86
87 static inline void spacemit_sdhci_clrsetbits(struct sdhci_host *host, u32 clr, u32 set, int reg)
88 {
89 u32 val = sdhci_readl(host, reg);
90
91 val = (val & ~clr) | set;
92 sdhci_writel(host, val, reg);
93 }
94
95 static void spacemit_sdhci_reset(struct sdhci_host *host, u8 mask)
96 {
97 sdhci_reset(host, mask);
98
99 if (mask != SDHCI_RESET_ALL)
100 return;
101
102 spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK,
103 SPACEMIT_SDHC_PHY_CTRL_REG);
104
105 spacemit_sdhci_clrsetbits(host, SDHC_PHY_DRIVE_SEL,
106 SDHC_RX_BIAS_CTRL | FIELD_PREP(SDHC_PHY_DRIVE_SEL, 4),
107 SPACEMIT_SDHC_PHY_PADCFG_REG);
108
109 if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC))
110 spacemit_sdhci_setbits(host, SDHC_MMC_CARD_MODE, SPACEMIT_SDHC_MMC_CTRL_REG);
111
112 spacemit_sdhci_setbits(host, SDHC_GEN_PAD_CLK_ON, SPACEMIT_SDHC_LEGACY_CTRL_REG);
113
114 if (host->mmc->caps2 & MMC_CAP2_NO_MMC)
115 spacemit_sdhci_setbits(host, SDHC_OVRRD_CLK_OEN | SDHC_FORCE_CLK_ON,
116 SPACEMIT_SDHC_OP_EXT_REG);
117 }
118
119 static void spacemit_sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned int timing)
120 {
121 if (timing == MMC_TIMING_MMC_HS200)
122 spacemit_sdhci_setbits(host, SDHC_MMC_HS200, SPACEMIT_SDHC_MMC_CTRL_REG);
123
124 if (timing == MMC_TIMING_MMC_HS400)
125 spacemit_sdhci_setbits(host, SDHC_MMC_HS400, SPACEMIT_SDHC_MMC_CTRL_REG);
126
127 sdhci_set_uhs_signaling(host, timing);
128
129 if (!(host->mmc->caps2 & MMC_CAP2_NO_SDIO))
130 spacemit_sdhci_setbits(host, SDHCI_CTRL_VDD_180, SDHCI_HOST_CONTROL2);
131 }
132
133 static void spacemit_sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
134 {
135 struct mmc_host *mmc = host->mmc;
136
137 if (mmc->ios.timing <= MMC_TIMING_UHS_SDR50)
138 spacemit_sdhci_setbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG);
139 else
140 spacemit_sdhci_clrbits(host, SDHC_TX_INT_CLK_SEL, SPACEMIT_SDHC_TX_CFG_REG);
141
142 sdhci_set_clock(host, clock);
143 };
144
145 static void spacemit_sdhci_phy_dll_init(struct sdhci_host *host)
146 {
147 u32 state;
148 int ret;
149
150 spacemit_sdhci_clrsetbits(host, SDHC_DLL_PREDLY_NUM |
151 SDHC_DLL_FULLDLY_RANGE |
152 SDHC_DLL_VREG_CTRL,
153 FIELD_PREP(SDHC_DLL_PREDLY_NUM, 1) |
154 FIELD_PREP(SDHC_DLL_FULLDLY_RANGE, 1) |
155 FIELD_PREP(SDHC_DLL_VREG_CTRL, 1),
156 SPACEMIT_SDHC_PHY_DLLCFG);
157
158 spacemit_sdhci_clrsetbits(host, SDHC_DLL_REG1_CTRL,
159 FIELD_PREP(SDHC_DLL_REG1_CTRL, 0x92),
160 SPACEMIT_SDHC_PHY_DLLCFG1);
161
162 spacemit_sdhci_setbits(host, SDHC_DLL_ENABLE, SPACEMIT_SDHC_PHY_DLLCFG);
163
164 ret = readl_poll_timeout(host->ioaddr + SPACEMIT_SDHC_PHY_DLLSTS, state,
165 state & SDHC_DLL_LOCK_STATE, 2, 100);
166 if (ret == -ETIMEDOUT)
167 dev_warn(mmc_dev(host->mmc), "fail to lock phy dll in 100us!\n");
168 }
169
170 static void spacemit_sdhci_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios)
171 {
172 struct sdhci_host *host = mmc_priv(mmc);
173
174 if (!ios->enhanced_strobe) {
175 spacemit_sdhci_clrbits(host, SDHC_ENHANCE_STROBE_EN, SPACEMIT_SDHC_MMC_CTRL_REG);
176 return;
177 }
178
179 spacemit_sdhci_setbits(host, SDHC_ENHANCE_STROBE_EN, SPACEMIT_SDHC_MMC_CTRL_REG);
180 spacemit_sdhci_phy_dll_init(host);
181 }
182
183 static unsigned int spacemit_sdhci_clk_get_max_clock(struct sdhci_host *host)
184 {
185 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
186
187 return clk_get_rate(pltfm_host->clk);
188 }
189
190 static int spacemit_sdhci_pre_select_hs400(struct mmc_host *mmc)
191 {
192 struct sdhci_host *host = mmc_priv(mmc);
193
194 spacemit_sdhci_setbits(host, SDHC_MMC_HS400, SPACEMIT_SDHC_MMC_CTRL_REG);
195
196 return 0;
197 }
198
199 static void spacemit_sdhci_post_select_hs400(struct mmc_host *mmc)
200 {
201 struct sdhci_host *host = mmc_priv(mmc);
202
203 spacemit_sdhci_phy_dll_init(host);
204 }
205
206 static void spacemit_sdhci_pre_hs400_to_hs200(struct mmc_host *mmc)
207 {
208 struct sdhci_host *host = mmc_priv(mmc);
209
210 spacemit_sdhci_clrbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK,
211 SPACEMIT_SDHC_PHY_CTRL_REG);
212 spacemit_sdhci_clrbits(host, SDHC_MMC_HS400 | SDHC_MMC_HS200 | SDHC_ENHANCE_STROBE_EN,
213 SPACEMIT_SDHC_MMC_CTRL_REG);
214 spacemit_sdhci_clrbits(host, SDHC_HS200_USE_RFIFO, SPACEMIT_SDHC_PHY_FUNC_REG);
215
216 udelay(5);
217
218 spacemit_sdhci_setbits(host, SDHC_PHY_FUNC_EN | SDHC_PHY_PLL_LOCK,
219 SPACEMIT_SDHC_PHY_CTRL_REG);
220 }
221
222 static inline int spacemit_sdhci_get_clocks(struct device *dev,
223 struct sdhci_pltfm_host *pltfm_host)
224 {
225 struct spacemit_sdhci_host *sdhst = sdhci_pltfm_priv(pltfm_host);
226
227 sdhst->clk_core = devm_clk_get_enabled(dev, "core");
228 if (IS_ERR(sdhst->clk_core))
229 return -EINVAL;
230
231 sdhst->clk_io = devm_clk_get_enabled(dev, "io");
232 if (IS_ERR(sdhst->clk_io))
233 return -EINVAL;
234
235 pltfm_host->clk = sdhst->clk_io;
236
237 return 0;
238 }
239
240 static inline int spacemit_sdhci_get_resets(struct device *dev)
241 {
242 struct reset_control *rst;
243
244 rst = devm_reset_control_get_optional_shared_deasserted(dev, "axi");
245 if (IS_ERR(rst))
246 return PTR_ERR(rst);
247
248 rst = devm_reset_control_get_optional_exclusive_deasserted(dev, "sdh");
249 if (IS_ERR(rst))
250 return PTR_ERR(rst);
251
252 return 0;
253 }
254
255 static const struct sdhci_ops spacemit_sdhci_ops = {
256 .get_max_clock = spacemit_sdhci_clk_get_max_clock,
257 .reset = spacemit_sdhci_reset,
258 .set_bus_width = sdhci_set_bus_width,
259 .set_clock = spacemit_sdhci_set_clock,
260 .set_uhs_signaling = spacemit_sdhci_set_uhs_signaling,
261 };
262
263 static const struct sdhci_pltfm_data spacemit_sdhci_k1_pdata = {
264 .ops = &spacemit_sdhci_ops,
265 .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
266 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
267 SDHCI_QUIRK_32BIT_ADMA_SIZE |
268 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
269 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
270 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
271 .quirks2 = SDHCI_QUIRK2_BROKEN_64_BIT_DMA |
272 SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
273 };
274
275 static const struct sdhci_pltfm_data spacemit_sdhci_k3_pdata = {
276 .ops = &spacemit_sdhci_ops,
277 .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
278 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
279 SDHCI_QUIRK_32BIT_ADMA_SIZE |
280 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
281 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
282 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
283 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
284 };
285
286 static const struct of_device_id spacemit_sdhci_of_match[] = {
287 { .compatible = "spacemit,k1-sdhci", .data = &spacemit_sdhci_k1_pdata },
288 { .compatible = "spacemit,k3-sdhci", .data = &spacemit_sdhci_k3_pdata },
289 { /* sentinel */ }
290 };
291 MODULE_DEVICE_TABLE(of, spacemit_sdhci_of_match);
292
293 static int spacemit_sdhci_probe(struct platform_device *pdev)
294 {
295 struct device *dev = &pdev->dev;
296 struct spacemit_sdhci_host *sdhst;
297 struct sdhci_pltfm_host *pltfm_host;
298 struct sdhci_host *host;
299 const struct sdhci_pltfm_data *data;
300 struct mmc_host_ops *mops;
301 int ret;
302
303 data = of_device_get_match_data(&pdev->dev);
304
305 host = sdhci_pltfm_init(pdev, data, sizeof(*sdhst));
306 if (IS_ERR(host))
307 return PTR_ERR(host);
308
309 pltfm_host = sdhci_priv(host);
310
311 ret = mmc_of_parse(host->mmc);
312 if (ret)
313 goto err_pltfm;
314
315 sdhci_get_of_property(pdev);
316
317 if (!(host->mmc->caps2 & MMC_CAP2_NO_MMC)) {
318 mops = &host->mmc_host_ops;
319 mops->hs400_prepare_ddr = spacemit_sdhci_pre_select_hs400;
320 mops->hs400_complete = spacemit_sdhci_post_select_hs400;
321 mops->hs400_downgrade = spacemit_sdhci_pre_hs400_to_hs200;
322 mops->hs400_enhanced_strobe = spacemit_sdhci_hs400_enhanced_strobe;
323 }
324
325 host->mmc->caps |= MMC_CAP_NEED_RSP_BUSY;
326
327 ret = spacemit_sdhci_get_clocks(dev, pltfm_host);
328 if (ret)
329 goto err_pltfm;
330
331 ret = spacemit_sdhci_get_resets(dev);
332 if (ret)
333 goto err_pltfm;
334
335 ret = sdhci_add_host(host);
336 if (ret)
337 goto err_pltfm;
338
339 return 0;
340
341 err_pltfm:
342 return ret;
343 }
344
345 static struct platform_driver spacemit_sdhci_driver = {
346 .driver = {
347 .name = "sdhci-spacemit",
348 .of_match_table = spacemit_sdhci_of_match,
349 },
350 .probe = spacemit_sdhci_probe,
351 .remove = sdhci_pltfm_remove,
352 };
353 module_platform_driver(spacemit_sdhci_driver);
354
355 MODULE_DESCRIPTION("SpacemiT SDHCI platform driver");
356 MODULE_LICENSE("GPL");