arasan_sdhc_fdt.c revision 1.6 1 1.6 thorpej /* $NetBSD: arasan_sdhc_fdt.c,v 1.6 2021/01/27 03:10:21 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.6 thorpej __KERNEL_RCSID(0, "$NetBSD: arasan_sdhc_fdt.c,v 1.6 2021/01/27 03:10:21 thorpej Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/systm.h>
36 1.1 jmcneill #include <sys/sysctl.h>
37 1.1 jmcneill #include <sys/kmem.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <dev/sdmmc/sdhcreg.h>
40 1.1 jmcneill #include <dev/sdmmc/sdhcvar.h>
41 1.1 jmcneill #include <dev/sdmmc/sdmmcvar.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <dev/clk/clk_backend.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/fdt/fdtvar.h>
46 1.1 jmcneill #include <dev/fdt/syscon.h>
47 1.1 jmcneill
48 1.1 jmcneill #define RK3399_GRF_EMMCCORE_CON0 0xf000
49 1.1 jmcneill #define RK3399_CORECFG_BASECLKFREQ __BITS(15,8)
50 1.1 jmcneill #define RK3399_CORECFG_TIMEOUTCLKUNIT __BIT(7)
51 1.1 jmcneill #define RK3399_CORECFG_TUNINGCOUNT __BITS(5,0)
52 1.1 jmcneill #define RK3399_GRF_EMMCCORE_CON11 0xf02c
53 1.1 jmcneill #define RK3399_CORECFG_CLOCKMULTIPLIER __BITS(7,0)
54 1.1 jmcneill
55 1.1 jmcneill enum arasan_sdhc_type {
56 1.1 jmcneill AS_TYPE_RK3399 = 1,
57 1.1 jmcneill };
58 1.1 jmcneill
59 1.1 jmcneill struct arasan_sdhc_softc {
60 1.1 jmcneill struct sdhc_softc sc_base;
61 1.1 jmcneill struct sdhc_host *sc_host[1];
62 1.1 jmcneill bus_space_tag_t sc_bst;
63 1.1 jmcneill bus_space_handle_t sc_bsh;
64 1.1 jmcneill bus_size_t sc_bsz;
65 1.1 jmcneill int sc_phandle;
66 1.1 jmcneill struct fdtbus_phy *sc_phy;
67 1.1 jmcneill struct syscon *sc_syscon;
68 1.1 jmcneill struct clk *sc_clk_xin;
69 1.1 jmcneill struct clk *sc_clk_ahb;
70 1.1 jmcneill enum arasan_sdhc_type sc_type;
71 1.1 jmcneill struct clk_domain sc_clkdom;
72 1.1 jmcneill struct clk sc_clk_card;
73 1.1 jmcneill };
74 1.1 jmcneill
75 1.5 thorpej static const struct device_compatible_entry compat_data[] = {
76 1.5 thorpej { .compat = "rockchip,rk3399-sdhci-5.1",
77 1.5 thorpej .value = AS_TYPE_RK3399 },
78 1.5 thorpej
79 1.6 thorpej DEVICE_COMPAT_EOL
80 1.6 thorpej };
81 1.6 thorpej
82 1.6 thorpej static const struct device_compatible_entry sdhci_5_1_compat[] = {
83 1.6 thorpej { .compat = "arasan,sdhci-5.1" },
84 1.6 thorpej DEVICE_COMPAT_EOL
85 1.1 jmcneill };
86 1.1 jmcneill
87 1.1 jmcneill static struct clk *
88 1.1 jmcneill arasan_sdhc_clk_decode(device_t dev, int cc_phandle, const void *data, size_t len)
89 1.1 jmcneill {
90 1.1 jmcneill struct arasan_sdhc_softc * const sc = device_private(dev);
91 1.1 jmcneill
92 1.1 jmcneill if (len != 0)
93 1.1 jmcneill return NULL;
94 1.1 jmcneill
95 1.1 jmcneill return &sc->sc_clk_card;
96 1.1 jmcneill }
97 1.1 jmcneill
98 1.1 jmcneill static const struct fdtbus_clock_controller_func arasan_sdhc_fdt_clk_funcs = {
99 1.1 jmcneill .decode = arasan_sdhc_clk_decode,
100 1.1 jmcneill };
101 1.1 jmcneill
102 1.1 jmcneill static struct clk *
103 1.1 jmcneill arasan_sdhc_clk_get(void *priv, const char *name)
104 1.1 jmcneill {
105 1.1 jmcneill struct arasan_sdhc_softc * const sc = priv;
106 1.1 jmcneill
107 1.1 jmcneill if (strcmp(name, sc->sc_clk_card.name) != 0)
108 1.1 jmcneill return NULL;
109 1.1 jmcneill
110 1.1 jmcneill return &sc->sc_clk_card;
111 1.1 jmcneill }
112 1.1 jmcneill
113 1.1 jmcneill static u_int
114 1.1 jmcneill arasan_sdhc_clk_get_rate(void *priv, struct clk *clk)
115 1.1 jmcneill {
116 1.1 jmcneill struct arasan_sdhc_softc * const sc = priv;
117 1.1 jmcneill
118 1.1 jmcneill return clk_get_rate(sc->sc_clk_xin);
119 1.1 jmcneill }
120 1.1 jmcneill
121 1.1 jmcneill static const struct clk_funcs arasan_sdhc_clk_funcs = {
122 1.1 jmcneill .get = arasan_sdhc_clk_get,
123 1.1 jmcneill .get_rate = arasan_sdhc_clk_get_rate,
124 1.1 jmcneill };
125 1.1 jmcneill
126 1.1 jmcneill static int
127 1.1 jmcneill arasan_sdhc_signal_voltage(struct sdhc_softc *sdhc, int signal_voltage)
128 1.1 jmcneill {
129 1.1 jmcneill if (signal_voltage == SDMMC_SIGNAL_VOLTAGE_180)
130 1.1 jmcneill return 0;
131 1.1 jmcneill
132 1.1 jmcneill return EINVAL;
133 1.1 jmcneill }
134 1.1 jmcneill
135 1.1 jmcneill static int
136 1.1 jmcneill arasan_sdhc_bus_clock_pre(struct sdhc_softc *sdhc, int freq)
137 1.1 jmcneill {
138 1.1 jmcneill struct arasan_sdhc_softc * const sc = device_private(sdhc->sc_dev);
139 1.1 jmcneill int error;
140 1.1 jmcneill
141 1.1 jmcneill if (sc->sc_phy != NULL) {
142 1.1 jmcneill error = fdtbus_phy_enable(sc->sc_phy, false);
143 1.1 jmcneill if (error != 0)
144 1.1 jmcneill return error;
145 1.1 jmcneill }
146 1.1 jmcneill
147 1.1 jmcneill return 0;
148 1.1 jmcneill }
149 1.1 jmcneill
150 1.1 jmcneill static int
151 1.1 jmcneill arasan_sdhc_bus_clock_post(struct sdhc_softc *sdhc, int freq)
152 1.1 jmcneill {
153 1.1 jmcneill struct arasan_sdhc_softc * const sc = device_private(sdhc->sc_dev);
154 1.1 jmcneill int error;
155 1.1 jmcneill
156 1.1 jmcneill if (sc->sc_phy != NULL) {
157 1.1 jmcneill error = fdtbus_phy_enable(sc->sc_phy, true);
158 1.1 jmcneill if (error != 0)
159 1.1 jmcneill return error;
160 1.1 jmcneill }
161 1.1 jmcneill
162 1.1 jmcneill return 0;
163 1.1 jmcneill }
164 1.1 jmcneill
165 1.1 jmcneill static void
166 1.1 jmcneill arasan_sdhc_init_rk3399(struct arasan_sdhc_softc *sc)
167 1.1 jmcneill {
168 1.1 jmcneill uint32_t mask, val;
169 1.1 jmcneill
170 1.1 jmcneill if (sc->sc_syscon == NULL)
171 1.1 jmcneill return;
172 1.1 jmcneill
173 1.1 jmcneill syscon_lock(sc->sc_syscon);
174 1.1 jmcneill
175 1.1 jmcneill /* Disable clock multiplier */
176 1.1 jmcneill mask = RK3399_CORECFG_CLOCKMULTIPLIER;
177 1.1 jmcneill val = 0;
178 1.1 jmcneill syscon_write_4(sc->sc_syscon, RK3399_GRF_EMMCCORE_CON11, (mask << 16) | val);
179 1.1 jmcneill
180 1.1 jmcneill /* Set base clock frequency */
181 1.1 jmcneill const u_int xin_rate = clk_get_rate(sc->sc_clk_xin);
182 1.1 jmcneill mask = RK3399_CORECFG_BASECLKFREQ;
183 1.1 jmcneill val = __SHIFTIN((xin_rate + (1000000 / 2)) / 1000000, RK3399_CORECFG_BASECLKFREQ);
184 1.1 jmcneill syscon_write_4(sc->sc_syscon, RK3399_GRF_EMMCCORE_CON0, (mask << 16) | val);
185 1.1 jmcneill
186 1.1 jmcneill syscon_unlock(sc->sc_syscon);
187 1.1 jmcneill }
188 1.1 jmcneill
189 1.1 jmcneill static void
190 1.1 jmcneill arasan_sdhc_init(device_t dev)
191 1.1 jmcneill {
192 1.1 jmcneill struct arasan_sdhc_softc * const sc = device_private(dev);
193 1.1 jmcneill int error;
194 1.1 jmcneill
195 1.1 jmcneill if (sc->sc_type == AS_TYPE_RK3399)
196 1.1 jmcneill arasan_sdhc_init_rk3399(sc);
197 1.1 jmcneill
198 1.6 thorpej if (of_compatible_match(sc->sc_phandle, sdhci_5_1_compat)) {
199 1.1 jmcneill sc->sc_phy = fdtbus_phy_get(sc->sc_phandle, "phy_arasan");
200 1.1 jmcneill if (sc->sc_phy == NULL) {
201 1.1 jmcneill aprint_error_dev(dev, "couldn't get PHY\n");
202 1.1 jmcneill return;
203 1.1 jmcneill }
204 1.1 jmcneill sc->sc_base.sc_vendor_signal_voltage = arasan_sdhc_signal_voltage;
205 1.1 jmcneill }
206 1.1 jmcneill
207 1.1 jmcneill error = sdhc_host_found(&sc->sc_base, sc->sc_bst, sc->sc_bsh, sc->sc_bsz);
208 1.1 jmcneill if (error != 0) {
209 1.1 jmcneill aprint_error_dev(dev, "couldn't initialize host, error = %d\n", error);
210 1.1 jmcneill return;
211 1.1 jmcneill }
212 1.1 jmcneill }
213 1.1 jmcneill
214 1.1 jmcneill static int
215 1.1 jmcneill arasan_sdhc_match(device_t parent, cfdata_t cf, void *aux)
216 1.1 jmcneill {
217 1.1 jmcneill struct fdt_attach_args * const faa = aux;
218 1.1 jmcneill
219 1.6 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
220 1.1 jmcneill }
221 1.1 jmcneill
222 1.1 jmcneill static void
223 1.1 jmcneill arasan_sdhc_attach(device_t parent, device_t self, void *aux)
224 1.1 jmcneill {
225 1.1 jmcneill struct arasan_sdhc_softc * const sc = device_private(self);
226 1.1 jmcneill struct fdt_attach_args * const faa = aux;
227 1.1 jmcneill const int phandle = faa->faa_phandle;
228 1.1 jmcneill char intrstr[128];
229 1.1 jmcneill const char *clkname;
230 1.1 jmcneill bus_addr_t addr;
231 1.1 jmcneill bus_size_t size;
232 1.1 jmcneill u_int bus_width;
233 1.2 jmcneill int error;
234 1.1 jmcneill void *ih;
235 1.1 jmcneill
236 1.1 jmcneill fdtbus_clock_assign(phandle);
237 1.1 jmcneill
238 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
239 1.1 jmcneill aprint_error(": couldn't get registers\n");
240 1.1 jmcneill return;
241 1.1 jmcneill }
242 1.1 jmcneill
243 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
244 1.1 jmcneill aprint_error(": couldn't decode interrupt\n");
245 1.1 jmcneill return;
246 1.1 jmcneill }
247 1.1 jmcneill
248 1.1 jmcneill sc->sc_clk_xin = fdtbus_clock_get(phandle, "clk_xin");
249 1.1 jmcneill sc->sc_clk_ahb = fdtbus_clock_get(phandle, "clk_ahb");
250 1.1 jmcneill if (sc->sc_clk_xin == NULL || sc->sc_clk_ahb == NULL) {
251 1.1 jmcneill aprint_error(": couldn't get clocks\n");
252 1.1 jmcneill return;
253 1.1 jmcneill }
254 1.1 jmcneill if (clk_enable(sc->sc_clk_xin) != 0 || clk_enable(sc->sc_clk_ahb) != 0) {
255 1.1 jmcneill aprint_error(": couldn't enable clocks\n");
256 1.1 jmcneill return;
257 1.1 jmcneill }
258 1.1 jmcneill
259 1.1 jmcneill sc->sc_syscon = fdtbus_syscon_acquire(phandle, "arasan,soc-ctl-syscon");
260 1.1 jmcneill
261 1.1 jmcneill if (of_getprop_uint32(phandle, "bus-width", &bus_width) != 0)
262 1.1 jmcneill bus_width = 4;
263 1.1 jmcneill
264 1.1 jmcneill sc->sc_phandle = phandle;
265 1.1 jmcneill sc->sc_bst = faa->faa_bst;
266 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
267 1.1 jmcneill aprint_error(": couldn't map registers\n");
268 1.1 jmcneill return;
269 1.1 jmcneill }
270 1.1 jmcneill sc->sc_bsz = size;
271 1.6 thorpej sc->sc_type = of_compatible_lookup(phandle, compat_data)->value;
272 1.1 jmcneill
273 1.2 jmcneill const uint32_t caps = bus_space_read_4(sc->sc_bst, sc->sc_bsh, SDHC_CAPABILITIES);
274 1.2 jmcneill if ((caps & (SDHC_ADMA2_SUPP|SDHC_64BIT_SYS_BUS)) == SDHC_ADMA2_SUPP) {
275 1.2 jmcneill error = bus_dmatag_subregion(faa->faa_dmat, 0, 0xffffffff,
276 1.2 jmcneill &sc->sc_base.sc_dmat, BUS_DMA_WAITOK);
277 1.2 jmcneill if (error != 0) {
278 1.2 jmcneill aprint_error(": couldn't create DMA tag: %d\n", error);
279 1.2 jmcneill return;
280 1.2 jmcneill }
281 1.2 jmcneill } else {
282 1.2 jmcneill sc->sc_base.sc_dmat = faa->faa_dmat;
283 1.2 jmcneill }
284 1.2 jmcneill
285 1.1 jmcneill sc->sc_base.sc_dev = self;
286 1.1 jmcneill sc->sc_base.sc_host = sc->sc_host;
287 1.1 jmcneill sc->sc_base.sc_flags = SDHC_FLAG_NO_CLKBASE |
288 1.3 jmcneill SDHC_FLAG_SINGLE_POWER_WRITE |
289 1.3 jmcneill SDHC_FLAG_32BIT_ACCESS |
290 1.1 jmcneill SDHC_FLAG_USE_DMA |
291 1.1 jmcneill SDHC_FLAG_USE_ADMA2 |
292 1.1 jmcneill SDHC_FLAG_STOP_WITH_TC;
293 1.1 jmcneill if (bus_width == 8)
294 1.1 jmcneill sc->sc_base.sc_flags |= SDHC_FLAG_8BIT_MODE;
295 1.1 jmcneill sc->sc_base.sc_clkbase = clk_get_rate(sc->sc_clk_xin) / 1000;
296 1.1 jmcneill sc->sc_base.sc_vendor_bus_clock = arasan_sdhc_bus_clock_pre;
297 1.1 jmcneill sc->sc_base.sc_vendor_bus_clock_post = arasan_sdhc_bus_clock_post;
298 1.1 jmcneill
299 1.1 jmcneill aprint_naive("\n");
300 1.1 jmcneill aprint_normal(": Arasan SDHCI controller\n");
301 1.1 jmcneill
302 1.1 jmcneill clkname = fdtbus_get_string(phandle, "clock-output-names");
303 1.1 jmcneill if (clkname == NULL)
304 1.1 jmcneill clkname = faa->faa_name;
305 1.1 jmcneill
306 1.1 jmcneill sc->sc_clkdom.name = device_xname(self);
307 1.1 jmcneill sc->sc_clkdom.funcs = &arasan_sdhc_clk_funcs;
308 1.1 jmcneill sc->sc_clkdom.priv = sc;
309 1.1 jmcneill sc->sc_clk_card.domain = &sc->sc_clkdom;
310 1.1 jmcneill sc->sc_clk_card.name = kmem_asprintf("%s", clkname);
311 1.1 jmcneill clk_attach(&sc->sc_clk_card);
312 1.1 jmcneill
313 1.1 jmcneill fdtbus_register_clock_controller(self, phandle, &arasan_sdhc_fdt_clk_funcs);
314 1.1 jmcneill
315 1.4 ryo ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SDMMC, 0,
316 1.4 ryo sdhc_intr, &sc->sc_base, device_xname(self));
317 1.1 jmcneill if (ih == NULL) {
318 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n", intrstr);
319 1.1 jmcneill return;
320 1.1 jmcneill }
321 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
322 1.1 jmcneill
323 1.1 jmcneill arasan_sdhc_init(self);
324 1.1 jmcneill }
325 1.1 jmcneill
326 1.1 jmcneill CFATTACH_DECL_NEW(arasan_sdhc_fdt, sizeof(struct arasan_sdhc_softc),
327 1.1 jmcneill arasan_sdhc_match, arasan_sdhc_attach, NULL, NULL);
328