1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Driver for Synopsys DesignWare Cores Mobile Storage Host Controller
4 *
5 * Copyright (C) 2018 Synaptics Incorporated
6 *
7 * Author: Jisheng Zhang <jszhang@kernel.org>
8 */
9
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/sizes.h>
16
17 #include "sdhci-pltfm.h"
18
19 #define BOUNDARY_OK(addr, len) \
20 ((addr | (SZ_128M - 1)) == ((addr + len - 1) | (SZ_128M - 1)))
21
22 struct dwcmshc_priv {
23 struct clk *bus_clk;
24 };
25
26 /*
27 * If DMA addr spans 128MB boundary, we split the DMA transfer into two
28 * so that each DMA transfer doesn't exceed the boundary.
29 */
dwcmshc_adma_write_desc(struct sdhci_host * host,void ** desc,dma_addr_t addr,int len,unsigned int cmd)30 static void dwcmshc_adma_write_desc(struct sdhci_host *host, void **desc,
31 dma_addr_t addr, int len, unsigned int cmd)
32 {
33 int tmplen, offset;
34
35 if (likely(!len || BOUNDARY_OK(addr, len))) {
36 sdhci_adma_write_desc(host, desc, addr, len, cmd);
37 return;
38 }
39
40 offset = addr & (SZ_128M - 1);
41 tmplen = SZ_128M - offset;
42 sdhci_adma_write_desc(host, desc, addr, tmplen, cmd);
43
44 addr += tmplen;
45 len -= tmplen;
46 sdhci_adma_write_desc(host, desc, addr, len, cmd);
47 }
48
49 static const struct sdhci_ops sdhci_dwcmshc_ops = {
50 .set_clock = sdhci_set_clock,
51 .set_bus_width = sdhci_set_bus_width,
52 .set_uhs_signaling = sdhci_set_uhs_signaling,
53 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
54 .reset = sdhci_reset,
55 .adma_write_desc = dwcmshc_adma_write_desc,
56 };
57
58 static const struct sdhci_pltfm_data sdhci_dwcmshc_pdata = {
59 .ops = &sdhci_dwcmshc_ops,
60 .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
61 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
62 };
63
dwcmshc_probe(struct platform_device * pdev)64 static int dwcmshc_probe(struct platform_device *pdev)
65 {
66 struct sdhci_pltfm_host *pltfm_host;
67 struct sdhci_host *host;
68 struct dwcmshc_priv *priv;
69 int err;
70 u32 extra;
71
72 host = sdhci_pltfm_init(pdev, &sdhci_dwcmshc_pdata,
73 sizeof(struct dwcmshc_priv));
74 if (IS_ERR(host))
75 return PTR_ERR(host);
76
77 /*
78 * extra adma table cnt for cross 128M boundary handling.
79 */
80 extra = DIV_ROUND_UP_ULL(dma_get_required_mask(&pdev->dev), SZ_128M);
81 if (extra > SDHCI_MAX_SEGS)
82 extra = SDHCI_MAX_SEGS;
83 host->adma_table_cnt += extra;
84
85 pltfm_host = sdhci_priv(host);
86 priv = sdhci_pltfm_priv(pltfm_host);
87
88 pltfm_host->clk = devm_clk_get(&pdev->dev, "core");
89 if (IS_ERR(pltfm_host->clk)) {
90 err = PTR_ERR(pltfm_host->clk);
91 dev_err(&pdev->dev, "failed to get core clk: %d\n", err);
92 goto free_pltfm;
93 }
94 err = clk_prepare_enable(pltfm_host->clk);
95 if (err)
96 goto free_pltfm;
97
98 priv->bus_clk = devm_clk_get(&pdev->dev, "bus");
99 if (!IS_ERR(priv->bus_clk))
100 clk_prepare_enable(priv->bus_clk);
101
102 err = mmc_of_parse(host->mmc);
103 if (err)
104 goto err_clk;
105
106 sdhci_get_of_property(pdev);
107
108 err = sdhci_add_host(host);
109 if (err)
110 goto err_clk;
111
112 return 0;
113
114 err_clk:
115 clk_disable_unprepare(pltfm_host->clk);
116 clk_disable_unprepare(priv->bus_clk);
117 free_pltfm:
118 sdhci_pltfm_free(pdev);
119 return err;
120 }
121
dwcmshc_remove(struct platform_device * pdev)122 static int dwcmshc_remove(struct platform_device *pdev)
123 {
124 struct sdhci_host *host = platform_get_drvdata(pdev);
125 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
126 struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
127
128 sdhci_remove_host(host, 0);
129
130 clk_disable_unprepare(pltfm_host->clk);
131 clk_disable_unprepare(priv->bus_clk);
132
133 sdhci_pltfm_free(pdev);
134
135 return 0;
136 }
137
138 static const struct of_device_id sdhci_dwcmshc_dt_ids[] = {
139 { .compatible = "snps,dwcmshc-sdhci" },
140 {}
141 };
142 MODULE_DEVICE_TABLE(of, sdhci_dwcmshc_dt_ids);
143
144 static struct platform_driver sdhci_dwcmshc_driver = {
145 .driver = {
146 .name = "sdhci-dwcmshc",
147 .of_match_table = sdhci_dwcmshc_dt_ids,
148 },
149 .probe = dwcmshc_probe,
150 .remove = dwcmshc_remove,
151 };
152 module_platform_driver(sdhci_dwcmshc_driver);
153
154 MODULE_DESCRIPTION("SDHCI platform driver for Synopsys DWC MSHC");
155 MODULE_AUTHOR("Jisheng Zhang <jszhang@kernel.org>");
156 MODULE_LICENSE("GPL v2");
157