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
|
/* $OpenBSD: sdhc_fdt.c,v 1.6 2020/04/19 16:17:03 kettenis Exp $ */
/*
* Copyright (c) 2017 Mark Kettenis
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <machine/fdt.h>
#include <machine/intr.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_clock.h>
#include <dev/ofw/ofw_misc.h>
#include <dev/ofw/ofw_pinctrl.h>
#include <dev/ofw/fdt.h>
#include <dev/sdmmc/sdhcreg.h>
#include <dev/sdmmc/sdhcvar.h>
#include <dev/sdmmc/sdmmcvar.h>
/* RK3399 */
#define GRF_EMMCCORE_CON0_BASECLOCK 0xf000
#define GRF_EMMCCORE_CON0_BASECLOCK_CLR (0xff << 24)
#define GRF_EMMCCORE_CON0_BASECLOCK_VAL(x) (((x) & 0xff) << 8)
#define GRF_EMMCCORE_CON11 0xf02c
#define GRF_EMMCCORE_CON11_CLOCKMULT_CLR (0xff << 16)
#define GRF_EMMCCORE_CON11_CLOCKMULT_VAL(x) (((x) & 0xff) << 0)
struct sdhc_fdt_softc {
struct sdhc_softc sc;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
bus_size_t sc_size;
void *sc_ih;
struct sdhc_host *sc_host;
struct clock_device sc_cd;
};
int sdhc_fdt_match(struct device *, void *, void *);
void sdhc_fdt_attach(struct device *, struct device *, void *);
struct cfattach sdhc_fdt_ca = {
sizeof(struct sdhc_fdt_softc), sdhc_fdt_match, sdhc_fdt_attach
};
int sdhc_fdt_signal_voltage(struct sdhc_softc *, int);
uint32_t sdhc_fdt_get_frequency(void *, uint32_t *);
int
sdhc_fdt_match(struct device *parent, void *match, void *aux)
{
struct fdt_attach_args *faa = aux;
return (OF_is_compatible(faa->fa_node, "arasan,sdhci-5.1") ||
OF_is_compatible(faa->fa_node, "brcm,bcm2835-sdhci"));
}
void
sdhc_fdt_attach(struct device *parent, struct device *self, void *aux)
{
struct sdhc_fdt_softc *sc = (struct sdhc_fdt_softc *)self;
struct fdt_attach_args *faa = aux;
struct regmap *rm = NULL;
uint32_t phandle, freq, cap = 0;
if (faa->fa_nreg < 1) {
printf(": no registers\n");
return;
}
sc->sc_iot = faa->fa_iot;
sc->sc_size = faa->fa_reg[0].size;
if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
printf(": can't map registers\n");
return;
}
pinctrl_byname(faa->fa_node, "default");
clock_set_assigned(faa->fa_node);
clock_enable_all(faa->fa_node);
reset_deassert_all(faa->fa_node);
sc->sc_ih = fdt_intr_establish(faa->fa_node, IPL_BIO,
sdhc_intr, sc, sc->sc.sc_dev.dv_xname);
if (sc->sc_ih == NULL) {
printf(": can't establish interrupt\n");
goto unmap;
}
printf("\n");
sc->sc.sc_host = &sc->sc_host;
sc->sc.sc_dmat = faa->fa_dmat;
/*
* Arasan controller always uses 1.8V and doesn't like an
* explicit switch.
*/
if (OF_is_compatible(faa->fa_node, "arasan,sdhci-5.1"))
sc->sc.sc_signal_voltage = sdhc_fdt_signal_voltage;
/*
* Rockchip RK3399 PHY doesn't like being powered down at low
* clock speeds and needs to be powered up explicitly.
*/
if (OF_is_compatible(faa->fa_node, "rockchip,rk3399-sdhci-5.1")) {
/*
* The eMMC core's clock multiplier is of no use, so we just
* clear it. Also make sure to set the base clock frequency.
*/
freq = clock_get_frequency(faa->fa_node, "clk_xin");
freq /= 1000 * 1000; /* in MHz */
phandle = OF_getpropint(faa->fa_node,
"arasan,soc-ctl-syscon", 0);
if (phandle)
rm = regmap_byphandle(phandle);
if (rm) {
regmap_write_4(rm, GRF_EMMCCORE_CON11,
GRF_EMMCCORE_CON11_CLOCKMULT_CLR |
GRF_EMMCCORE_CON11_CLOCKMULT_VAL(0));
regmap_write_4(rm, GRF_EMMCCORE_CON0_BASECLOCK,
GRF_EMMCCORE_CON0_BASECLOCK_CLR |
GRF_EMMCCORE_CON0_BASECLOCK_VAL(freq));
}
/* Provide base clock frequency for the PHY driver. */
sc->sc_cd.cd_node = faa->fa_node;
sc->sc_cd.cd_cookie = sc;
sc->sc_cd.cd_get_frequency = sdhc_fdt_get_frequency;
clock_register(&sc->sc_cd);
/*
* Enable the PHY. The PHY should be powered on/off in
* the bus_clock function, but it's good enough to just
* enable it here right away and to keep it powered on.
*/
phy_enable(faa->fa_node, "phy_arasan");
sc->sc.sc_flags |= SDHC_F_NOPWR0;
/* XXX Doesn't work on Rockchip RK3399. */
sc->sc.sc_flags |= SDHC_F_NODDR50;
}
if (OF_is_compatible(faa->fa_node, "brcm,bcm2835-sdhci")) {
cap = SDHC_VOLTAGE_SUPP_3_3V | SDHC_HIGH_SPEED_SUPP;
cap |= SDHC_MAX_BLK_LEN_1024 << SDHC_MAX_BLK_LEN_SHIFT;
freq = clock_get_frequency(faa->fa_node, NULL);
sc->sc.sc_clkbase = freq / 1000;
sc->sc.sc_flags |= SDHC_F_32BIT_ACCESS;
}
sdhc_host_found(&sc->sc, sc->sc_iot, sc->sc_ioh, sc->sc_size, 1, cap);
return;
unmap:
bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
}
int
sdhc_fdt_signal_voltage(struct sdhc_softc *sc, int signal_voltage)
{
switch (signal_voltage) {
case SDMMC_SIGNAL_VOLTAGE_180:
return 0;
default:
return EINVAL;
}
}
uint32_t
sdhc_fdt_get_frequency(void *cookie, uint32_t *cells)
{
struct sdhc_fdt_softc *sc = cookie;
return clock_get_frequency(sc->sc_cd.cd_node, "clk_xin");
}
|