1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* Copyright (c) 2006-2024 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-10-28 zhujiale The first version
*/
#include <sdhci.h>
#include <rtdevice.h>
#include "drv_sd.h"
#define DBG_TAG "SDHCI"
#ifdef DRV_DEBUG
#define DBG_LVL DBG_LOG
#else
#define DBG_LVL DBG_INFO
#endif /* DRV_DEBUG */
#include <rtdbg.h>
#define REG_OFFSET_IN_BITS(reg) ((reg) << 3 & 0x18)
struct sdhci_iproc_data {
const struct sdhci_pltfm_data *pdata;
rt_uint32_t caps;
rt_uint32_t caps1;
rt_uint32_t mmc_caps;
rt_bool_t missing_caps;
};
struct sdhci_iproc_host {
const struct sdhci_iproc_data *data;
rt_uint32_t shadow_cmd;
rt_uint32_t shadow_blk;
rt_bool_t is_cmd_shadowed;
rt_bool_t is_blk_shadowed;
};
static inline rt_uint32_t sdhci_iproc_readl(struct sdhci_host *host, int reg)
{
rt_uint32_t val = readl(host->ioaddr + reg);
return val;
}
static rt_uint16_t sdhci_iproc_readw(struct sdhci_host *host, int reg)
{
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
struct sdhci_iproc_host *iproc_host = sdhci_pltfm_priv(pltfm_host);
rt_uint32_t val;
rt_uint16_t word;
if ((reg == SDHCI_TRANSFER_MODE) && iproc_host->is_cmd_shadowed)
{
/* Get the saved transfer mode */
val = iproc_host->shadow_cmd;
} else if ((reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT) &&
iproc_host->is_blk_shadowed)
{
/* Get the saved block info */
val = iproc_host->shadow_blk;
} else {
val = sdhci_iproc_readl(host, (reg & ~3));
}
word = val >> REG_OFFSET_IN_BITS(reg) & 0xffff;
return word;
}
static rt_uint8_t sdhci_iproc_readb(struct sdhci_host *host, int reg)
{
rt_uint32_t val = sdhci_iproc_readl(host, (reg & ~3));
rt_uint8_t byte = val >> REG_OFFSET_IN_BITS(reg) & 0xff;
return byte;
}
static inline void sdhci_iproc_writel(struct sdhci_host *host, rt_uint32_t val, int reg)
{
LOG_D("%s: writel [0x%02x] 0x%08x\n",
mmc_hostname(host->mmc), reg, val);
writel(val, host->ioaddr + reg);
if (host->clock <= 400000)
{
/* Round up to micro-second four SD clock delay */
if (host->clock)
rt_hw_us_delay((4 * 1000000 + host->clock - 1) / host->clock);
else
rt_hw_us_delay(10);
}
}
static void sdhci_iproc_writew(struct sdhci_host *host, rt_uint16_t val, int reg)
{
struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
struct sdhci_iproc_host *iproc_host = sdhci_pltfm_priv(pltfm_host);
rt_uint32_t word_shift = REG_OFFSET_IN_BITS(reg);
rt_uint32_t mask = 0xffff << word_shift;
rt_uint32_t oldval, newval;
if (reg == SDHCI_COMMAND)
{
/* Write the block now as we are issuing a command */
if (iproc_host->is_blk_shadowed)
{
sdhci_iproc_writel(host, iproc_host->shadow_blk,
SDHCI_BLOCK_SIZE);
iproc_host->is_blk_shadowed = false;
}
oldval = iproc_host->shadow_cmd;
iproc_host->is_cmd_shadowed = false;
} else if ((reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT) &&
iproc_host->is_blk_shadowed)
{
/* Block size and count are stored in shadow reg */
oldval = iproc_host->shadow_blk;
} else {
/* Read reg, all other registers are not shadowed */
oldval = sdhci_iproc_readl(host, (reg & ~3));
}
newval = (oldval & ~mask) | (val << word_shift);
if (reg == SDHCI_TRANSFER_MODE)
{
/* Save the transfer mode until the command is issued */
iproc_host->shadow_cmd = newval;
iproc_host->is_cmd_shadowed = true;
} else if (reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT)
{
/* Save the block info until the command is issued */
iproc_host->shadow_blk = newval;
iproc_host->is_blk_shadowed = true;
} else {
/* Command or other regular 32-bit write */
sdhci_iproc_writel(host, newval, reg & ~3);
}
}
static void sdhci_iproc_writeb(struct sdhci_host *host, rt_uint8_t val, int reg)
{
rt_uint32_t oldval = sdhci_iproc_readl(host, (reg & ~3));
rt_uint32_t byte_shift = REG_OFFSET_IN_BITS(reg);
rt_uint32_t mask = 0xff << byte_shift;
rt_uint32_t newval = (oldval & ~mask) | (val << byte_shift);
sdhci_iproc_writel(host, newval, reg & ~3);
}
static const struct sdhci_ops sdhci_iproc_bcm2711_ops = {
.read_l = sdhci_iproc_readl,
.read_w = sdhci_iproc_readw,
.read_b = sdhci_iproc_readb,
.write_l = sdhci_iproc_writel,
.write_w = sdhci_iproc_writew,
.write_b = sdhci_iproc_writeb,
.set_clock = sdhci_set_clock,
.set_bus_width = sdhci_set_bus_width,
.reset = sdhci_reset,
.set_uhs_signaling = sdhci_set_uhs_signaling,
};
static const struct sdhci_pltfm_data sdhci_bcm2711_pltfm_data = {
.quirks = SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,
.ops = &sdhci_iproc_bcm2711_ops,
};
static const struct sdhci_iproc_data bcm2711_data = {
.pdata = &sdhci_bcm2711_pltfm_data,
.mmc_caps = MMC_CAP_3_3V_DDR,
};
rt_err_t bcm2835_probe(struct rt_platform_device *pdev)
{
struct sdhci_iproc_host *iproc_host;
const struct sdhci_iproc_data *iproc_data = NULL;
struct sdhci_host *host;
struct sdhci_pltfm_host *pltfm_host;
int ret;
iproc_data = &bcm2711_data;
host = sdhci_pltfm_init(pdev, iproc_data->pdata, sizeof(*iproc_host));
pltfm_host = sdhci_priv(host);
iproc_host = sdhci_pltfm_priv(pltfm_host);
iproc_host->data = iproc_data;
ret = mmc_of_parse(host->mmc);
if (ret)
goto err;
sdhci_get_property(pdev);
host->mmc->caps |= iproc_host->data->mmc_caps;
ret = sdhci_add_host(host);
if (ret)
goto err;
return 0;
err:
return ret;
}
static const struct rt_ofw_node_id bcm2835_ofw_ids[] =
{
/* { .compatible = "brcm,bcm2711-emmc2"},*/
{ .compatible = "brcm,bcm2835-mmc"},
{ /* sentinel */ }
};
static struct rt_platform_driver bcm2835_driver =
{
.name = "bcm2835",
.ids = bcm2835_ofw_ids,
.probe = bcm2835_probe,
/* .remove = pl011_remove,*/
};
RT_PLATFORM_DRIVER_EXPORT(bcm2835_driver);
|