arasan_sdhc_fdt.c revision 1.5 1 1.5 thorpej /* $NetBSD: arasan_sdhc_fdt.c,v 1.5 2021/01/18 02:35:49 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.5 thorpej __KERNEL_RCSID(0, "$NetBSD: arasan_sdhc_fdt.c,v 1.5 2021/01/18 02:35:49 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.5 thorpej { 0 }
80 1.1 jmcneill };
81 1.1 jmcneill
82 1.1 jmcneill static struct clk *
83 1.1 jmcneill arasan_sdhc_clk_decode(device_t dev, int cc_phandle, const void *data, size_t len)
84 1.1 jmcneill {
85 1.1 jmcneill struct arasan_sdhc_softc * const sc = device_private(dev);
86 1.1 jmcneill
87 1.1 jmcneill if (len != 0)
88 1.1 jmcneill return NULL;
89 1.1 jmcneill
90 1.1 jmcneill return &sc->sc_clk_card;
91 1.1 jmcneill }
92 1.1 jmcneill
93 1.1 jmcneill static const struct fdtbus_clock_controller_func arasan_sdhc_fdt_clk_funcs = {
94 1.1 jmcneill .decode = arasan_sdhc_clk_decode,
95 1.1 jmcneill };
96 1.1 jmcneill
97 1.1 jmcneill static struct clk *
98 1.1 jmcneill arasan_sdhc_clk_get(void *priv, const char *name)
99 1.1 jmcneill {
100 1.1 jmcneill struct arasan_sdhc_softc * const sc = priv;
101 1.1 jmcneill
102 1.1 jmcneill if (strcmp(name, sc->sc_clk_card.name) != 0)
103 1.1 jmcneill return NULL;
104 1.1 jmcneill
105 1.1 jmcneill return &sc->sc_clk_card;
106 1.1 jmcneill }
107 1.1 jmcneill
108 1.1 jmcneill static u_int
109 1.1 jmcneill arasan_sdhc_clk_get_rate(void *priv, struct clk *clk)
110 1.1 jmcneill {
111 1.1 jmcneill struct arasan_sdhc_softc * const sc = priv;
112 1.1 jmcneill
113 1.1 jmcneill return clk_get_rate(sc->sc_clk_xin);
114 1.1 jmcneill }
115 1.1 jmcneill
116 1.1 jmcneill static const struct clk_funcs arasan_sdhc_clk_funcs = {
117 1.1 jmcneill .get = arasan_sdhc_clk_get,
118 1.1 jmcneill .get_rate = arasan_sdhc_clk_get_rate,
119 1.1 jmcneill };
120 1.1 jmcneill
121 1.1 jmcneill static int
122 1.1 jmcneill arasan_sdhc_signal_voltage(struct sdhc_softc *sdhc, int signal_voltage)
123 1.1 jmcneill {
124 1.1 jmcneill if (signal_voltage == SDMMC_SIGNAL_VOLTAGE_180)
125 1.1 jmcneill return 0;
126 1.1 jmcneill
127 1.1 jmcneill return EINVAL;
128 1.1 jmcneill }
129 1.1 jmcneill
130 1.1 jmcneill static int
131 1.1 jmcneill arasan_sdhc_bus_clock_pre(struct sdhc_softc *sdhc, int freq)
132 1.1 jmcneill {
133 1.1 jmcneill struct arasan_sdhc_softc * const sc = device_private(sdhc->sc_dev);
134 1.1 jmcneill int error;
135 1.1 jmcneill
136 1.1 jmcneill if (sc->sc_phy != NULL) {
137 1.1 jmcneill error = fdtbus_phy_enable(sc->sc_phy, false);
138 1.1 jmcneill if (error != 0)
139 1.1 jmcneill return error;
140 1.1 jmcneill }
141 1.1 jmcneill
142 1.1 jmcneill return 0;
143 1.1 jmcneill }
144 1.1 jmcneill
145 1.1 jmcneill static int
146 1.1 jmcneill arasan_sdhc_bus_clock_post(struct sdhc_softc *sdhc, int freq)
147 1.1 jmcneill {
148 1.1 jmcneill struct arasan_sdhc_softc * const sc = device_private(sdhc->sc_dev);
149 1.1 jmcneill int error;
150 1.1 jmcneill
151 1.1 jmcneill if (sc->sc_phy != NULL) {
152 1.1 jmcneill error = fdtbus_phy_enable(sc->sc_phy, true);
153 1.1 jmcneill if (error != 0)
154 1.1 jmcneill return error;
155 1.1 jmcneill }
156 1.1 jmcneill
157 1.1 jmcneill return 0;
158 1.1 jmcneill }
159 1.1 jmcneill
160 1.1 jmcneill static void
161 1.1 jmcneill arasan_sdhc_init_rk3399(struct arasan_sdhc_softc *sc)
162 1.1 jmcneill {
163 1.1 jmcneill uint32_t mask, val;
164 1.1 jmcneill
165 1.1 jmcneill if (sc->sc_syscon == NULL)
166 1.1 jmcneill return;
167 1.1 jmcneill
168 1.1 jmcneill syscon_lock(sc->sc_syscon);
169 1.1 jmcneill
170 1.1 jmcneill /* Disable clock multiplier */
171 1.1 jmcneill mask = RK3399_CORECFG_CLOCKMULTIPLIER;
172 1.1 jmcneill val = 0;
173 1.1 jmcneill syscon_write_4(sc->sc_syscon, RK3399_GRF_EMMCCORE_CON11, (mask << 16) | val);
174 1.1 jmcneill
175 1.1 jmcneill /* Set base clock frequency */
176 1.1 jmcneill const u_int xin_rate = clk_get_rate(sc->sc_clk_xin);
177 1.1 jmcneill mask = RK3399_CORECFG_BASECLKFREQ;
178 1.1 jmcneill val = __SHIFTIN((xin_rate + (1000000 / 2)) / 1000000, RK3399_CORECFG_BASECLKFREQ);
179 1.1 jmcneill syscon_write_4(sc->sc_syscon, RK3399_GRF_EMMCCORE_CON0, (mask << 16) | val);
180 1.1 jmcneill
181 1.1 jmcneill syscon_unlock(sc->sc_syscon);
182 1.1 jmcneill }
183 1.1 jmcneill
184 1.1 jmcneill static void
185 1.1 jmcneill arasan_sdhc_init(device_t dev)
186 1.1 jmcneill {
187 1.1 jmcneill struct arasan_sdhc_softc * const sc = device_private(dev);
188 1.1 jmcneill const char * sdhci_5_1_compat[] = { "arasan,sdhci-5.1", NULL };
189 1.1 jmcneill int error;
190 1.1 jmcneill
191 1.1 jmcneill if (sc->sc_type == AS_TYPE_RK3399)
192 1.1 jmcneill arasan_sdhc_init_rk3399(sc);
193 1.1 jmcneill
194 1.1 jmcneill if (of_match_compatible(sc->sc_phandle, sdhci_5_1_compat)) {
195 1.1 jmcneill sc->sc_phy = fdtbus_phy_get(sc->sc_phandle, "phy_arasan");
196 1.1 jmcneill if (sc->sc_phy == NULL) {
197 1.1 jmcneill aprint_error_dev(dev, "couldn't get PHY\n");
198 1.1 jmcneill return;
199 1.1 jmcneill }
200 1.1 jmcneill sc->sc_base.sc_vendor_signal_voltage = arasan_sdhc_signal_voltage;
201 1.1 jmcneill }
202 1.1 jmcneill
203 1.1 jmcneill error = sdhc_host_found(&sc->sc_base, sc->sc_bst, sc->sc_bsh, sc->sc_bsz);
204 1.1 jmcneill if (error != 0) {
205 1.1 jmcneill aprint_error_dev(dev, "couldn't initialize host, error = %d\n", error);
206 1.1 jmcneill return;
207 1.1 jmcneill }
208 1.1 jmcneill }
209 1.1 jmcneill
210 1.1 jmcneill static int
211 1.1 jmcneill arasan_sdhc_match(device_t parent, cfdata_t cf, void *aux)
212 1.1 jmcneill {
213 1.1 jmcneill struct fdt_attach_args * const faa = aux;
214 1.1 jmcneill
215 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
216 1.1 jmcneill }
217 1.1 jmcneill
218 1.1 jmcneill static void
219 1.1 jmcneill arasan_sdhc_attach(device_t parent, device_t self, void *aux)
220 1.1 jmcneill {
221 1.1 jmcneill struct arasan_sdhc_softc * const sc = device_private(self);
222 1.1 jmcneill struct fdt_attach_args * const faa = aux;
223 1.1 jmcneill const int phandle = faa->faa_phandle;
224 1.1 jmcneill char intrstr[128];
225 1.1 jmcneill const char *clkname;
226 1.1 jmcneill bus_addr_t addr;
227 1.1 jmcneill bus_size_t size;
228 1.1 jmcneill u_int bus_width;
229 1.2 jmcneill int error;
230 1.1 jmcneill void *ih;
231 1.1 jmcneill
232 1.1 jmcneill fdtbus_clock_assign(phandle);
233 1.1 jmcneill
234 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
235 1.1 jmcneill aprint_error(": couldn't get registers\n");
236 1.1 jmcneill return;
237 1.1 jmcneill }
238 1.1 jmcneill
239 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
240 1.1 jmcneill aprint_error(": couldn't decode interrupt\n");
241 1.1 jmcneill return;
242 1.1 jmcneill }
243 1.1 jmcneill
244 1.1 jmcneill sc->sc_clk_xin = fdtbus_clock_get(phandle, "clk_xin");
245 1.1 jmcneill sc->sc_clk_ahb = fdtbus_clock_get(phandle, "clk_ahb");
246 1.1 jmcneill if (sc->sc_clk_xin == NULL || sc->sc_clk_ahb == NULL) {
247 1.1 jmcneill aprint_error(": couldn't get clocks\n");
248 1.1 jmcneill return;
249 1.1 jmcneill }
250 1.1 jmcneill if (clk_enable(sc->sc_clk_xin) != 0 || clk_enable(sc->sc_clk_ahb) != 0) {
251 1.1 jmcneill aprint_error(": couldn't enable clocks\n");
252 1.1 jmcneill return;
253 1.1 jmcneill }
254 1.1 jmcneill
255 1.1 jmcneill sc->sc_syscon = fdtbus_syscon_acquire(phandle, "arasan,soc-ctl-syscon");
256 1.1 jmcneill
257 1.1 jmcneill if (of_getprop_uint32(phandle, "bus-width", &bus_width) != 0)
258 1.1 jmcneill bus_width = 4;
259 1.1 jmcneill
260 1.1 jmcneill sc->sc_phandle = phandle;
261 1.1 jmcneill sc->sc_bst = faa->faa_bst;
262 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
263 1.1 jmcneill aprint_error(": couldn't map registers\n");
264 1.1 jmcneill return;
265 1.1 jmcneill }
266 1.1 jmcneill sc->sc_bsz = size;
267 1.5 thorpej sc->sc_type = of_search_compatible(phandle, compat_data)->value;
268 1.1 jmcneill
269 1.2 jmcneill const uint32_t caps = bus_space_read_4(sc->sc_bst, sc->sc_bsh, SDHC_CAPABILITIES);
270 1.2 jmcneill if ((caps & (SDHC_ADMA2_SUPP|SDHC_64BIT_SYS_BUS)) == SDHC_ADMA2_SUPP) {
271 1.2 jmcneill error = bus_dmatag_subregion(faa->faa_dmat, 0, 0xffffffff,
272 1.2 jmcneill &sc->sc_base.sc_dmat, BUS_DMA_WAITOK);
273 1.2 jmcneill if (error != 0) {
274 1.2 jmcneill aprint_error(": couldn't create DMA tag: %d\n", error);
275 1.2 jmcneill return;
276 1.2 jmcneill }
277 1.2 jmcneill } else {
278 1.2 jmcneill sc->sc_base.sc_dmat = faa->faa_dmat;
279 1.2 jmcneill }
280 1.2 jmcneill
281 1.1 jmcneill sc->sc_base.sc_dev = self;
282 1.1 jmcneill sc->sc_base.sc_host = sc->sc_host;
283 1.1 jmcneill sc->sc_base.sc_flags = SDHC_FLAG_NO_CLKBASE |
284 1.3 jmcneill SDHC_FLAG_SINGLE_POWER_WRITE |
285 1.3 jmcneill SDHC_FLAG_32BIT_ACCESS |
286 1.1 jmcneill SDHC_FLAG_USE_DMA |
287 1.1 jmcneill SDHC_FLAG_USE_ADMA2 |
288 1.1 jmcneill SDHC_FLAG_STOP_WITH_TC;
289 1.1 jmcneill if (bus_width == 8)
290 1.1 jmcneill sc->sc_base.sc_flags |= SDHC_FLAG_8BIT_MODE;
291 1.1 jmcneill sc->sc_base.sc_clkbase = clk_get_rate(sc->sc_clk_xin) / 1000;
292 1.1 jmcneill sc->sc_base.sc_vendor_bus_clock = arasan_sdhc_bus_clock_pre;
293 1.1 jmcneill sc->sc_base.sc_vendor_bus_clock_post = arasan_sdhc_bus_clock_post;
294 1.1 jmcneill
295 1.1 jmcneill aprint_naive("\n");
296 1.1 jmcneill aprint_normal(": Arasan SDHCI controller\n");
297 1.1 jmcneill
298 1.1 jmcneill clkname = fdtbus_get_string(phandle, "clock-output-names");
299 1.1 jmcneill if (clkname == NULL)
300 1.1 jmcneill clkname = faa->faa_name;
301 1.1 jmcneill
302 1.1 jmcneill sc->sc_clkdom.name = device_xname(self);
303 1.1 jmcneill sc->sc_clkdom.funcs = &arasan_sdhc_clk_funcs;
304 1.1 jmcneill sc->sc_clkdom.priv = sc;
305 1.1 jmcneill sc->sc_clk_card.domain = &sc->sc_clkdom;
306 1.1 jmcneill sc->sc_clk_card.name = kmem_asprintf("%s", clkname);
307 1.1 jmcneill clk_attach(&sc->sc_clk_card);
308 1.1 jmcneill
309 1.1 jmcneill fdtbus_register_clock_controller(self, phandle, &arasan_sdhc_fdt_clk_funcs);
310 1.1 jmcneill
311 1.4 ryo ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SDMMC, 0,
312 1.4 ryo sdhc_intr, &sc->sc_base, device_xname(self));
313 1.1 jmcneill if (ih == NULL) {
314 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n", intrstr);
315 1.1 jmcneill return;
316 1.1 jmcneill }
317 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
318 1.1 jmcneill
319 1.1 jmcneill arasan_sdhc_init(self);
320 1.1 jmcneill }
321 1.1 jmcneill
322 1.1 jmcneill CFATTACH_DECL_NEW(arasan_sdhc_fdt, sizeof(struct arasan_sdhc_softc),
323 1.1 jmcneill arasan_sdhc_match, arasan_sdhc_attach, NULL, NULL);
324