1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/delay.h>
23#include <linux/module.h>
24#include <linux/mmc/host.h>
25#include "sdhci-pltfm.h"
26
27
28
29
30
31#define MIN_FREQ 400000
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#define BCM2835_SDHCI_WRITE_DELAY (((2 * 1000000) / MIN_FREQ) + 1)
52
53struct bcm2835_sdhci {
54 u32 shadow;
55};
56
57static void bcm2835_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
58{
59 writel(val, host->ioaddr + reg);
60
61 udelay(BCM2835_SDHCI_WRITE_DELAY);
62}
63
64static inline u32 bcm2835_sdhci_readl(struct sdhci_host *host, int reg)
65{
66 u32 val = readl(host->ioaddr + reg);
67
68 if (reg == SDHCI_CAPABILITIES)
69 val |= SDHCI_CAN_VDD_330;
70
71 return val;
72}
73
74static void bcm2835_sdhci_writew(struct sdhci_host *host, u16 val, int reg)
75{
76 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
77 struct bcm2835_sdhci *bcm2835_host = pltfm_host->priv;
78 u32 oldval = (reg == SDHCI_COMMAND) ? bcm2835_host->shadow :
79 bcm2835_sdhci_readl(host, reg & ~3);
80 u32 word_num = (reg >> 1) & 1;
81 u32 word_shift = word_num * 16;
82 u32 mask = 0xffff << word_shift;
83 u32 newval = (oldval & ~mask) | (val << word_shift);
84
85 if (reg == SDHCI_TRANSFER_MODE)
86 bcm2835_host->shadow = newval;
87 else
88 bcm2835_sdhci_writel(host, newval, reg & ~3);
89}
90
91static u16 bcm2835_sdhci_readw(struct sdhci_host *host, int reg)
92{
93 u32 val = bcm2835_sdhci_readl(host, (reg & ~3));
94 u32 word_num = (reg >> 1) & 1;
95 u32 word_shift = word_num * 16;
96 u32 word = (val >> word_shift) & 0xffff;
97
98 return word;
99}
100
101static void bcm2835_sdhci_writeb(struct sdhci_host *host, u8 val, int reg)
102{
103 u32 oldval = bcm2835_sdhci_readl(host, reg & ~3);
104 u32 byte_num = reg & 3;
105 u32 byte_shift = byte_num * 8;
106 u32 mask = 0xff << byte_shift;
107 u32 newval = (oldval & ~mask) | (val << byte_shift);
108
109 bcm2835_sdhci_writel(host, newval, reg & ~3);
110}
111
112static u8 bcm2835_sdhci_readb(struct sdhci_host *host, int reg)
113{
114 u32 val = bcm2835_sdhci_readl(host, (reg & ~3));
115 u32 byte_num = reg & 3;
116 u32 byte_shift = byte_num * 8;
117 u32 byte = (val >> byte_shift) & 0xff;
118
119 return byte;
120}
121
122unsigned int bcm2835_sdhci_get_min_clock(struct sdhci_host *host)
123{
124 return MIN_FREQ;
125}
126
127static const struct sdhci_ops bcm2835_sdhci_ops = {
128 .write_l = bcm2835_sdhci_writel,
129 .write_w = bcm2835_sdhci_writew,
130 .write_b = bcm2835_sdhci_writeb,
131 .read_l = bcm2835_sdhci_readl,
132 .read_w = bcm2835_sdhci_readw,
133 .read_b = bcm2835_sdhci_readb,
134 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
135 .get_min_clock = bcm2835_sdhci_get_min_clock,
136};
137
138static const struct sdhci_pltfm_data bcm2835_sdhci_pdata = {
139 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
140 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK,
141 .ops = &bcm2835_sdhci_ops,
142};
143
144static int bcm2835_sdhci_probe(struct platform_device *pdev)
145{
146 struct sdhci_host *host;
147 struct bcm2835_sdhci *bcm2835_host;
148 struct sdhci_pltfm_host *pltfm_host;
149 int ret;
150
151 host = sdhci_pltfm_init(pdev, &bcm2835_sdhci_pdata, 0);
152 if (IS_ERR(host))
153 return PTR_ERR(host);
154
155 bcm2835_host = devm_kzalloc(&pdev->dev, sizeof(*bcm2835_host),
156 GFP_KERNEL);
157 if (!bcm2835_host) {
158 dev_err(mmc_dev(host->mmc),
159 "failed to allocate bcm2835_sdhci\n");
160 return -ENOMEM;
161 }
162
163 pltfm_host = sdhci_priv(host);
164 pltfm_host->priv = bcm2835_host;
165
166 pltfm_host->clk = devm_clk_get(&pdev->dev, NULL);
167 if (IS_ERR(pltfm_host->clk)) {
168 ret = PTR_ERR(pltfm_host->clk);
169 goto err;
170 }
171
172 return sdhci_add_host(host);
173
174err:
175 sdhci_pltfm_free(pdev);
176 return ret;
177}
178
179static int bcm2835_sdhci_remove(struct platform_device *pdev)
180{
181 struct sdhci_host *host = platform_get_drvdata(pdev);
182 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
183
184 sdhci_remove_host(host, dead);
185 sdhci_pltfm_free(pdev);
186
187 return 0;
188}
189
190static const struct of_device_id bcm2835_sdhci_of_match[] = {
191 { .compatible = "brcm,bcm2835-sdhci" },
192 { }
193};
194MODULE_DEVICE_TABLE(of, bcm2835_sdhci_of_match);
195
196static struct platform_driver bcm2835_sdhci_driver = {
197 .driver = {
198 .name = "sdhci-bcm2835",
199 .owner = THIS_MODULE,
200 .of_match_table = bcm2835_sdhci_of_match,
201 .pm = SDHCI_PLTFM_PMOPS,
202 },
203 .probe = bcm2835_sdhci_probe,
204 .remove = bcm2835_sdhci_remove,
205};
206module_platform_driver(bcm2835_sdhci_driver);
207
208MODULE_DESCRIPTION("BCM2835 SDHCI driver");
209MODULE_AUTHOR("Stephen Warren");
210MODULE_LICENSE("GPL v2");
211