perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / mmc / host / sdhci-sprd.c
blob9a822e2e9f0b61967c3ae2c703bcc35527d73a0e
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Secure Digital Host Controller
4 //
5 // Copyright (C) 2018 Spreadtrum, Inc.
6 // Author: Chunyan Zhang <chunyan.zhang@unisoc.com>
8 #include <linux/delay.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/highmem.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
14 #include <linux/of_gpio.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/slab.h>
20 #include "sdhci-pltfm.h"
22 /* SDHCI_ARGUMENT2 register high 16bit */
23 #define SDHCI_SPRD_ARG2_STUFF GENMASK(31, 16)
25 #define SDHCI_SPRD_REG_32_DLL_DLY_OFFSET 0x208
26 #define SDHCIBSPRD_IT_WR_DLY_INV BIT(5)
27 #define SDHCI_SPRD_BIT_CMD_DLY_INV BIT(13)
28 #define SDHCI_SPRD_BIT_POSRD_DLY_INV BIT(21)
29 #define SDHCI_SPRD_BIT_NEGRD_DLY_INV BIT(29)
31 #define SDHCI_SPRD_REG_32_BUSY_POSI 0x250
32 #define SDHCI_SPRD_BIT_OUTR_CLK_AUTO_EN BIT(25)
33 #define SDHCI_SPRD_BIT_INNR_CLK_AUTO_EN BIT(24)
35 #define SDHCI_SPRD_REG_DEBOUNCE 0x28C
36 #define SDHCI_SPRD_BIT_DLL_BAK BIT(0)
37 #define SDHCI_SPRD_BIT_DLL_VAL BIT(1)
39 #define SDHCI_SPRD_INT_SIGNAL_MASK 0x1B7F410B
41 /* SDHCI_HOST_CONTROL2 */
42 #define SDHCI_SPRD_CTRL_HS200 0x0005
43 #define SDHCI_SPRD_CTRL_HS400 0x0006
46 * According to the standard specification, BIT(3) of SDHCI_SOFTWARE_RESET is
47 * reserved, and only used on Spreadtrum's design, the hardware cannot work
48 * if this bit is cleared.
49 * 1 : normal work
50 * 0 : hardware reset
52 #define SDHCI_HW_RESET_CARD BIT(3)
54 #define SDHCI_SPRD_MAX_CUR 0xFFFFFF
55 #define SDHCI_SPRD_CLK_MAX_DIV 1023
57 #define SDHCI_SPRD_CLK_DEF_RATE 26000000
59 struct sdhci_sprd_host {
60 u32 version;
61 struct clk *clk_sdio;
62 struct clk *clk_enable;
63 u32 base_rate;
64 int flags; /* backup of host attribute */
67 #define TO_SPRD_HOST(host) sdhci_pltfm_priv(sdhci_priv(host))
69 static void sdhci_sprd_init_config(struct sdhci_host *host)
71 u16 val;
73 /* set dll backup mode */
74 val = sdhci_readl(host, SDHCI_SPRD_REG_DEBOUNCE);
75 val |= SDHCI_SPRD_BIT_DLL_BAK | SDHCI_SPRD_BIT_DLL_VAL;
76 sdhci_writel(host, val, SDHCI_SPRD_REG_DEBOUNCE);
79 static inline u32 sdhci_sprd_readl(struct sdhci_host *host, int reg)
81 if (unlikely(reg == SDHCI_MAX_CURRENT))
82 return SDHCI_SPRD_MAX_CUR;
84 return readl_relaxed(host->ioaddr + reg);
87 static inline void sdhci_sprd_writel(struct sdhci_host *host, u32 val, int reg)
89 /* SDHCI_MAX_CURRENT is reserved on Spreadtrum's platform */
90 if (unlikely(reg == SDHCI_MAX_CURRENT))
91 return;
93 if (unlikely(reg == SDHCI_SIGNAL_ENABLE || reg == SDHCI_INT_ENABLE))
94 val = val & SDHCI_SPRD_INT_SIGNAL_MASK;
96 writel_relaxed(val, host->ioaddr + reg);
99 static inline void sdhci_sprd_writew(struct sdhci_host *host, u16 val, int reg)
101 /* SDHCI_BLOCK_COUNT is Read Only on Spreadtrum's platform */
102 if (unlikely(reg == SDHCI_BLOCK_COUNT))
103 return;
105 writew_relaxed(val, host->ioaddr + reg);
108 static inline void sdhci_sprd_writeb(struct sdhci_host *host, u8 val, int reg)
111 * Since BIT(3) of SDHCI_SOFTWARE_RESET is reserved according to the
112 * standard specification, sdhci_reset() write this register directly
113 * without checking other reserved bits, that will clear BIT(3) which
114 * is defined as hardware reset on Spreadtrum's platform and clearing
115 * it by mistake will lead the card not work. So here we need to work
116 * around it.
118 if (unlikely(reg == SDHCI_SOFTWARE_RESET)) {
119 if (readb_relaxed(host->ioaddr + reg) & SDHCI_HW_RESET_CARD)
120 val |= SDHCI_HW_RESET_CARD;
123 writeb_relaxed(val, host->ioaddr + reg);
126 static inline void sdhci_sprd_sd_clk_off(struct sdhci_host *host)
128 u16 ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
130 ctrl &= ~SDHCI_CLOCK_CARD_EN;
131 sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
134 static inline void
135 sdhci_sprd_set_dll_invert(struct sdhci_host *host, u32 mask, bool en)
137 u32 dll_dly_offset;
139 dll_dly_offset = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_DLY_OFFSET);
140 if (en)
141 dll_dly_offset |= mask;
142 else
143 dll_dly_offset &= ~mask;
144 sdhci_writel(host, dll_dly_offset, SDHCI_SPRD_REG_32_DLL_DLY_OFFSET);
147 static inline u32 sdhci_sprd_calc_div(u32 base_clk, u32 clk)
149 u32 div;
151 /* select 2x clock source */
152 if (base_clk <= clk * 2)
153 return 0;
155 div = (u32) (base_clk / (clk * 2));
157 if ((base_clk / div) > (clk * 2))
158 div++;
160 if (div > SDHCI_SPRD_CLK_MAX_DIV)
161 div = SDHCI_SPRD_CLK_MAX_DIV;
163 if (div % 2)
164 div = (div + 1) / 2;
165 else
166 div = div / 2;
168 return div;
171 static inline void _sdhci_sprd_set_clock(struct sdhci_host *host,
172 unsigned int clk)
174 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
175 u32 div, val, mask;
177 div = sdhci_sprd_calc_div(sprd_host->base_rate, clk);
179 clk |= ((div & 0x300) >> 2) | ((div & 0xFF) << 8);
180 sdhci_enable_clk(host, clk);
182 /* enable auto gate sdhc_enable_auto_gate */
183 val = sdhci_readl(host, SDHCI_SPRD_REG_32_BUSY_POSI);
184 mask = SDHCI_SPRD_BIT_OUTR_CLK_AUTO_EN |
185 SDHCI_SPRD_BIT_INNR_CLK_AUTO_EN;
186 if (mask != (val & mask)) {
187 val |= mask;
188 sdhci_writel(host, val, SDHCI_SPRD_REG_32_BUSY_POSI);
192 static void sdhci_sprd_set_clock(struct sdhci_host *host, unsigned int clock)
194 bool en = false;
196 if (clock == 0) {
197 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
198 } else if (clock != host->clock) {
199 sdhci_sprd_sd_clk_off(host);
200 _sdhci_sprd_set_clock(host, clock);
202 if (clock <= 400000)
203 en = true;
204 sdhci_sprd_set_dll_invert(host, SDHCI_SPRD_BIT_CMD_DLY_INV |
205 SDHCI_SPRD_BIT_POSRD_DLY_INV, en);
206 } else {
207 _sdhci_sprd_set_clock(host, clock);
211 static unsigned int sdhci_sprd_get_max_clock(struct sdhci_host *host)
213 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
215 return clk_round_rate(sprd_host->clk_sdio, ULONG_MAX);
218 static unsigned int sdhci_sprd_get_min_clock(struct sdhci_host *host)
220 return 400000;
223 static void sdhci_sprd_set_uhs_signaling(struct sdhci_host *host,
224 unsigned int timing)
226 u16 ctrl_2;
228 if (timing == host->timing)
229 return;
231 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
232 /* Select Bus Speed Mode for host */
233 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
234 switch (timing) {
235 case MMC_TIMING_UHS_SDR12:
236 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
237 break;
238 case MMC_TIMING_MMC_HS:
239 case MMC_TIMING_SD_HS:
240 case MMC_TIMING_UHS_SDR25:
241 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
242 break;
243 case MMC_TIMING_UHS_SDR50:
244 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
245 break;
246 case MMC_TIMING_UHS_SDR104:
247 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
248 break;
249 case MMC_TIMING_UHS_DDR50:
250 case MMC_TIMING_MMC_DDR52:
251 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
252 break;
253 case MMC_TIMING_MMC_HS200:
254 ctrl_2 |= SDHCI_SPRD_CTRL_HS200;
255 break;
256 case MMC_TIMING_MMC_HS400:
257 ctrl_2 |= SDHCI_SPRD_CTRL_HS400;
258 break;
259 default:
260 break;
263 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
266 static void sdhci_sprd_hw_reset(struct sdhci_host *host)
268 int val;
271 * Note: don't use sdhci_writeb() API here since it is redirected to
272 * sdhci_sprd_writeb() in which we have a workaround for
273 * SDHCI_SOFTWARE_RESET which would make bit SDHCI_HW_RESET_CARD can
274 * not be cleared.
276 val = readb_relaxed(host->ioaddr + SDHCI_SOFTWARE_RESET);
277 val &= ~SDHCI_HW_RESET_CARD;
278 writeb_relaxed(val, host->ioaddr + SDHCI_SOFTWARE_RESET);
279 /* wait for 10 us */
280 usleep_range(10, 20);
282 val |= SDHCI_HW_RESET_CARD;
283 writeb_relaxed(val, host->ioaddr + SDHCI_SOFTWARE_RESET);
284 usleep_range(300, 500);
287 static struct sdhci_ops sdhci_sprd_ops = {
288 .read_l = sdhci_sprd_readl,
289 .write_l = sdhci_sprd_writel,
290 .write_b = sdhci_sprd_writeb,
291 .set_clock = sdhci_sprd_set_clock,
292 .get_max_clock = sdhci_sprd_get_max_clock,
293 .get_min_clock = sdhci_sprd_get_min_clock,
294 .set_bus_width = sdhci_set_bus_width,
295 .reset = sdhci_reset,
296 .set_uhs_signaling = sdhci_sprd_set_uhs_signaling,
297 .hw_reset = sdhci_sprd_hw_reset,
300 static void sdhci_sprd_request(struct mmc_host *mmc, struct mmc_request *mrq)
302 struct sdhci_host *host = mmc_priv(mmc);
303 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
305 host->flags |= sprd_host->flags & SDHCI_AUTO_CMD23;
308 * From version 4.10 onward, ARGUMENT2 register is also as 32-bit
309 * block count register which doesn't support stuff bits of
310 * CMD23 argument on Spreadtrum's sd host controller.
312 if (host->version >= SDHCI_SPEC_410 &&
313 mrq->sbc && (mrq->sbc->arg & SDHCI_SPRD_ARG2_STUFF) &&
314 (host->flags & SDHCI_AUTO_CMD23))
315 host->flags &= ~SDHCI_AUTO_CMD23;
317 sdhci_request(mmc, mrq);
320 static const struct sdhci_pltfm_data sdhci_sprd_pdata = {
321 .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK,
322 .quirks2 = SDHCI_QUIRK2_BROKEN_HS200 |
323 SDHCI_QUIRK2_USE_32BIT_BLK_CNT,
324 .ops = &sdhci_sprd_ops,
327 static int sdhci_sprd_probe(struct platform_device *pdev)
329 struct sdhci_host *host;
330 struct sdhci_sprd_host *sprd_host;
331 struct clk *clk;
332 int ret = 0;
334 host = sdhci_pltfm_init(pdev, &sdhci_sprd_pdata, sizeof(*sprd_host));
335 if (IS_ERR(host))
336 return PTR_ERR(host);
338 host->dma_mask = DMA_BIT_MASK(64);
339 pdev->dev.dma_mask = &host->dma_mask;
340 host->mmc_host_ops.request = sdhci_sprd_request;
342 host->mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
343 MMC_CAP_ERASE | MMC_CAP_CMD23;
344 ret = mmc_of_parse(host->mmc);
345 if (ret)
346 goto pltfm_free;
348 sprd_host = TO_SPRD_HOST(host);
350 clk = devm_clk_get(&pdev->dev, "sdio");
351 if (IS_ERR(clk)) {
352 ret = PTR_ERR(clk);
353 goto pltfm_free;
355 sprd_host->clk_sdio = clk;
356 sprd_host->base_rate = clk_get_rate(sprd_host->clk_sdio);
357 if (!sprd_host->base_rate)
358 sprd_host->base_rate = SDHCI_SPRD_CLK_DEF_RATE;
360 clk = devm_clk_get(&pdev->dev, "enable");
361 if (IS_ERR(clk)) {
362 ret = PTR_ERR(clk);
363 goto pltfm_free;
365 sprd_host->clk_enable = clk;
367 ret = clk_prepare_enable(sprd_host->clk_sdio);
368 if (ret)
369 goto pltfm_free;
371 clk_prepare_enable(sprd_host->clk_enable);
372 if (ret)
373 goto clk_disable;
375 sdhci_sprd_init_config(host);
376 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
377 sprd_host->version = ((host->version & SDHCI_VENDOR_VER_MASK) >>
378 SDHCI_VENDOR_VER_SHIFT);
380 pm_runtime_get_noresume(&pdev->dev);
381 pm_runtime_set_active(&pdev->dev);
382 pm_runtime_enable(&pdev->dev);
383 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
384 pm_runtime_use_autosuspend(&pdev->dev);
385 pm_suspend_ignore_children(&pdev->dev, 1);
387 sdhci_enable_v4_mode(host);
389 ret = sdhci_setup_host(host);
390 if (ret)
391 goto pm_runtime_disable;
393 sprd_host->flags = host->flags;
395 ret = __sdhci_add_host(host);
396 if (ret)
397 goto err_cleanup_host;
399 pm_runtime_mark_last_busy(&pdev->dev);
400 pm_runtime_put_autosuspend(&pdev->dev);
402 return 0;
404 err_cleanup_host:
405 sdhci_cleanup_host(host);
407 pm_runtime_disable:
408 pm_runtime_disable(&pdev->dev);
409 pm_runtime_set_suspended(&pdev->dev);
411 clk_disable_unprepare(sprd_host->clk_enable);
413 clk_disable:
414 clk_disable_unprepare(sprd_host->clk_sdio);
416 pltfm_free:
417 sdhci_pltfm_free(pdev);
418 return ret;
421 static int sdhci_sprd_remove(struct platform_device *pdev)
423 struct sdhci_host *host = platform_get_drvdata(pdev);
424 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
425 struct mmc_host *mmc = host->mmc;
427 mmc_remove_host(mmc);
428 clk_disable_unprepare(sprd_host->clk_sdio);
429 clk_disable_unprepare(sprd_host->clk_enable);
431 mmc_free_host(mmc);
433 return 0;
436 static const struct of_device_id sdhci_sprd_of_match[] = {
437 { .compatible = "sprd,sdhci-r11", },
440 MODULE_DEVICE_TABLE(of, sdhci_sprd_of_match);
442 #ifdef CONFIG_PM
443 static int sdhci_sprd_runtime_suspend(struct device *dev)
445 struct sdhci_host *host = dev_get_drvdata(dev);
446 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
448 sdhci_runtime_suspend_host(host);
450 clk_disable_unprepare(sprd_host->clk_sdio);
451 clk_disable_unprepare(sprd_host->clk_enable);
453 return 0;
456 static int sdhci_sprd_runtime_resume(struct device *dev)
458 struct sdhci_host *host = dev_get_drvdata(dev);
459 struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
460 int ret;
462 ret = clk_prepare_enable(sprd_host->clk_enable);
463 if (ret)
464 return ret;
466 ret = clk_prepare_enable(sprd_host->clk_sdio);
467 if (ret) {
468 clk_disable_unprepare(sprd_host->clk_enable);
469 return ret;
472 sdhci_runtime_resume_host(host);
474 return 0;
476 #endif
478 static const struct dev_pm_ops sdhci_sprd_pm_ops = {
479 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
480 pm_runtime_force_resume)
481 SET_RUNTIME_PM_OPS(sdhci_sprd_runtime_suspend,
482 sdhci_sprd_runtime_resume, NULL)
485 static struct platform_driver sdhci_sprd_driver = {
486 .probe = sdhci_sprd_probe,
487 .remove = sdhci_sprd_remove,
488 .driver = {
489 .name = "sdhci_sprd_r11",
490 .of_match_table = of_match_ptr(sdhci_sprd_of_match),
491 .pm = &sdhci_sprd_pm_ops,
494 module_platform_driver(sdhci_sprd_driver);
496 MODULE_DESCRIPTION("Spreadtrum sdio host controller r11 driver");
497 MODULE_LICENSE("GPL v2");
498 MODULE_ALIAS("platform:sdhci-sprd-r11");