meson_sdio.c revision 1.1.6.2 1 1.1.6.2 christos /* $NetBSD: meson_sdio.c,v 1.1.6.2 2019/06/10 22:05:51 christos Exp $ */
2 1.1.6.2 christos
3 1.1.6.2 christos /*-
4 1.1.6.2 christos * Copyright (c) 2015-2019 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1.6.2 christos * All rights reserved.
6 1.1.6.2 christos *
7 1.1.6.2 christos * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 christos * modification, are permitted provided that the following conditions
9 1.1.6.2 christos * are met:
10 1.1.6.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 christos * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 christos * documentation and/or other materials provided with the distribution.
15 1.1.6.2 christos *
16 1.1.6.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.6.2 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.6.2 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.6.2 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.6.2 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1.6.2 christos * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1.6.2 christos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1.6.2 christos * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1.6.2 christos * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.6.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.6.2 christos * SUCH DAMAGE.
27 1.1.6.2 christos */
28 1.1.6.2 christos
29 1.1.6.2 christos #include <sys/cdefs.h>
30 1.1.6.2 christos __KERNEL_RCSID(0, "$NetBSD: meson_sdio.c,v 1.1.6.2 2019/06/10 22:05:51 christos Exp $");
31 1.1.6.2 christos
32 1.1.6.2 christos #include <sys/param.h>
33 1.1.6.2 christos #include <sys/bus.h>
34 1.1.6.2 christos #include <sys/device.h>
35 1.1.6.2 christos #include <sys/intr.h>
36 1.1.6.2 christos #include <sys/systm.h>
37 1.1.6.2 christos #include <sys/kernel.h>
38 1.1.6.2 christos #include <sys/gpio.h>
39 1.1.6.2 christos
40 1.1.6.2 christos #include <dev/sdmmc/sdmmcvar.h>
41 1.1.6.2 christos #include <dev/sdmmc/sdmmcchip.h>
42 1.1.6.2 christos #include <dev/sdmmc/sdmmc_ioreg.h>
43 1.1.6.2 christos
44 1.1.6.2 christos #include <dev/fdt/fdtvar.h>
45 1.1.6.2 christos
46 1.1.6.2 christos #include <arm/amlogic/meson_sdioreg.h>
47 1.1.6.2 christos
48 1.1.6.2 christos static int meson_sdio_match(device_t, cfdata_t, void *);
49 1.1.6.2 christos static void meson_sdio_attach(device_t, device_t, void *);
50 1.1.6.2 christos static void meson_sdio_attach_i(device_t);
51 1.1.6.2 christos
52 1.1.6.2 christos static int meson_sdio_intr(void *);
53 1.1.6.2 christos
54 1.1.6.2 christos struct meson_sdio_softc {
55 1.1.6.2 christos device_t sc_dev;
56 1.1.6.2 christos bus_space_tag_t sc_bst;
57 1.1.6.2 christos bus_space_handle_t sc_bsh;
58 1.1.6.2 christos bus_dma_tag_t sc_dmat;
59 1.1.6.2 christos void *sc_ih;
60 1.1.6.2 christos
61 1.1.6.2 christos int sc_slot_phandle;
62 1.1.6.2 christos
63 1.1.6.2 christos uint32_t sc_bus_freq;
64 1.1.6.2 christos u_int sc_cur_width;
65 1.1.6.2 christos int sc_cur_port;
66 1.1.6.2 christos
67 1.1.6.2 christos struct fdtbus_gpio_pin *sc_gpio_cd;
68 1.1.6.2 christos int sc_gpio_cd_inverted;
69 1.1.6.2 christos struct fdtbus_gpio_pin *sc_gpio_wp;
70 1.1.6.2 christos int sc_gpio_wp_inverted;
71 1.1.6.2 christos
72 1.1.6.2 christos struct fdtbus_regulator *sc_reg_vmmc;
73 1.1.6.2 christos struct fdtbus_regulator *sc_reg_vqmmc;
74 1.1.6.2 christos
75 1.1.6.2 christos bool sc_non_removable;
76 1.1.6.2 christos bool sc_broken_cd;
77 1.1.6.2 christos
78 1.1.6.2 christos device_t sc_sdmmc_dev;
79 1.1.6.2 christos kmutex_t sc_intr_lock;
80 1.1.6.2 christos kcondvar_t sc_intr_cv;
81 1.1.6.2 christos
82 1.1.6.2 christos uint32_t sc_intr_irqs;
83 1.1.6.2 christos
84 1.1.6.2 christos bus_dmamap_t sc_dmamap;
85 1.1.6.2 christos bus_dma_segment_t sc_segs[1];
86 1.1.6.2 christos void *sc_bbuf;
87 1.1.6.2 christos };
88 1.1.6.2 christos
89 1.1.6.2 christos CFATTACH_DECL_NEW(meson_sdio, sizeof(struct meson_sdio_softc),
90 1.1.6.2 christos meson_sdio_match, meson_sdio_attach, NULL, NULL);
91 1.1.6.2 christos
92 1.1.6.2 christos static int meson_sdio_host_reset(sdmmc_chipset_handle_t);
93 1.1.6.2 christos static uint32_t meson_sdio_host_ocr(sdmmc_chipset_handle_t);
94 1.1.6.2 christos static int meson_sdio_host_maxblklen(sdmmc_chipset_handle_t);
95 1.1.6.2 christos static int meson_sdio_card_detect(sdmmc_chipset_handle_t);
96 1.1.6.2 christos static int meson_sdio_write_protect(sdmmc_chipset_handle_t);
97 1.1.6.2 christos static int meson_sdio_bus_power(sdmmc_chipset_handle_t, uint32_t);
98 1.1.6.2 christos static int meson_sdio_bus_clock(sdmmc_chipset_handle_t, int);
99 1.1.6.2 christos static int meson_sdio_bus_width(sdmmc_chipset_handle_t, int);
100 1.1.6.2 christos static int meson_sdio_bus_rod(sdmmc_chipset_handle_t, int);
101 1.1.6.2 christos static int meson_sdio_signal_voltage(sdmmc_chipset_handle_t, int);
102 1.1.6.2 christos static void meson_sdio_exec_command(sdmmc_chipset_handle_t,
103 1.1.6.2 christos struct sdmmc_command *);
104 1.1.6.2 christos static void meson_sdio_card_enable_intr(sdmmc_chipset_handle_t, int);
105 1.1.6.2 christos static void meson_sdio_card_intr_ack(sdmmc_chipset_handle_t);
106 1.1.6.2 christos
107 1.1.6.2 christos static int meson_sdio_set_clock(struct meson_sdio_softc *, u_int);
108 1.1.6.2 christos static int meson_sdio_wait_irqs(struct meson_sdio_softc *, uint32_t, int);
109 1.1.6.2 christos
110 1.1.6.2 christos static void meson_sdio_dmainit(struct meson_sdio_softc *);
111 1.1.6.2 christos
112 1.1.6.2 christos static struct sdmmc_chip_functions meson_sdio_chip_functions = {
113 1.1.6.2 christos .host_reset = meson_sdio_host_reset,
114 1.1.6.2 christos .host_ocr = meson_sdio_host_ocr,
115 1.1.6.2 christos .host_maxblklen = meson_sdio_host_maxblklen,
116 1.1.6.2 christos .card_detect = meson_sdio_card_detect,
117 1.1.6.2 christos .write_protect = meson_sdio_write_protect,
118 1.1.6.2 christos .bus_power = meson_sdio_bus_power,
119 1.1.6.2 christos .bus_clock = meson_sdio_bus_clock,
120 1.1.6.2 christos .bus_width = meson_sdio_bus_width,
121 1.1.6.2 christos .bus_rod = meson_sdio_bus_rod,
122 1.1.6.2 christos .signal_voltage = meson_sdio_signal_voltage,
123 1.1.6.2 christos .exec_command = meson_sdio_exec_command,
124 1.1.6.2 christos .card_enable_intr = meson_sdio_card_enable_intr,
125 1.1.6.2 christos .card_intr_ack = meson_sdio_card_intr_ack,
126 1.1.6.2 christos };
127 1.1.6.2 christos
128 1.1.6.2 christos #define SDIO_WRITE(sc, reg, val) \
129 1.1.6.2 christos bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
130 1.1.6.2 christos #define SDIO_READ(sc, reg) \
131 1.1.6.2 christos bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
132 1.1.6.2 christos
133 1.1.6.2 christos static const char * const compatible[] = {
134 1.1.6.2 christos "amlogic,meson8b-sdio",
135 1.1.6.2 christos NULL
136 1.1.6.2 christos };
137 1.1.6.2 christos
138 1.1.6.2 christos static const char * const slot_compatible[] = {
139 1.1.6.2 christos "mmc-slot",
140 1.1.6.2 christos NULL
141 1.1.6.2 christos };
142 1.1.6.2 christos
143 1.1.6.2 christos static int
144 1.1.6.2 christos meson_sdio_match(device_t parent, cfdata_t cf, void *aux)
145 1.1.6.2 christos {
146 1.1.6.2 christos struct fdt_attach_args * const faa = aux;
147 1.1.6.2 christos
148 1.1.6.2 christos return of_match_compatible(faa->faa_phandle, compatible);
149 1.1.6.2 christos }
150 1.1.6.2 christos
151 1.1.6.2 christos static void
152 1.1.6.2 christos meson_sdio_attach(device_t parent, device_t self, void *aux)
153 1.1.6.2 christos {
154 1.1.6.2 christos struct meson_sdio_softc * const sc = device_private(self);
155 1.1.6.2 christos struct fdt_attach_args * const faa = aux;
156 1.1.6.2 christos const int phandle = faa->faa_phandle;
157 1.1.6.2 christos char intrstr[128];
158 1.1.6.2 christos struct clk *clk_clkin, *clk_core;
159 1.1.6.2 christos bus_addr_t addr, port;
160 1.1.6.2 christos bus_size_t size;
161 1.1.6.2 christos int child;
162 1.1.6.2 christos
163 1.1.6.2 christos if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
164 1.1.6.2 christos aprint_error(": couldn't get registers\n");
165 1.1.6.2 christos return;
166 1.1.6.2 christos }
167 1.1.6.2 christos
168 1.1.6.2 christos if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
169 1.1.6.2 christos aprint_error(": failed to decode interrupt\n");
170 1.1.6.2 christos return;
171 1.1.6.2 christos }
172 1.1.6.2 christos
173 1.1.6.2 christos clk_core = fdtbus_clock_get(phandle, "core");
174 1.1.6.2 christos if (clk_core == NULL || clk_enable(clk_core) != 0) {
175 1.1.6.2 christos aprint_error(": failed to enable core clock\n");
176 1.1.6.2 christos return;
177 1.1.6.2 christos }
178 1.1.6.2 christos
179 1.1.6.2 christos clk_clkin = fdtbus_clock_get(phandle, "clkin");
180 1.1.6.2 christos if (clk_clkin == NULL) {
181 1.1.6.2 christos aprint_error(": failed to get clkin clock\n");
182 1.1.6.2 christos return;
183 1.1.6.2 christos }
184 1.1.6.2 christos
185 1.1.6.2 christos sc->sc_dev = self;
186 1.1.6.2 christos sc->sc_bst = faa->faa_bst;
187 1.1.6.2 christos sc->sc_dmat = faa->faa_dmat;
188 1.1.6.2 christos if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
189 1.1.6.2 christos aprint_error(": failed to map registers\n");
190 1.1.6.2 christos return;
191 1.1.6.2 christos }
192 1.1.6.2 christos
193 1.1.6.2 christos mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
194 1.1.6.2 christos cv_init(&sc->sc_intr_cv, "sdiointr");
195 1.1.6.2 christos
196 1.1.6.2 christos sc->sc_cur_port = -1;
197 1.1.6.2 christos for (child = OF_child(phandle); child; child = OF_peer(child))
198 1.1.6.2 christos if (of_match_compatible(child, slot_compatible) > 0) {
199 1.1.6.2 christos if (fdtbus_get_reg(child, 0, &port, NULL) == 0) {
200 1.1.6.2 christos sc->sc_slot_phandle = child;
201 1.1.6.2 christos sc->sc_cur_port = port;
202 1.1.6.2 christos }
203 1.1.6.2 christos break;
204 1.1.6.2 christos }
205 1.1.6.2 christos if (sc->sc_cur_port == -1) {
206 1.1.6.2 christos aprint_error(": couldn't get mmc slot\n");
207 1.1.6.2 christos return;
208 1.1.6.2 christos }
209 1.1.6.2 christos
210 1.1.6.2 christos aprint_naive("\n");
211 1.1.6.2 christos aprint_normal(": SDIO controller (port %c)\n", sc->sc_cur_port + 'A');
212 1.1.6.2 christos
213 1.1.6.2 christos sc->sc_reg_vmmc = fdtbus_regulator_acquire(sc->sc_slot_phandle, "vmmc-supply");
214 1.1.6.2 christos sc->sc_reg_vqmmc = fdtbus_regulator_acquire(sc->sc_slot_phandle, "vqmmc-supply");
215 1.1.6.2 christos
216 1.1.6.2 christos sc->sc_gpio_cd = fdtbus_gpio_acquire(sc->sc_slot_phandle, "cd-gpios",
217 1.1.6.2 christos GPIO_PIN_INPUT);
218 1.1.6.2 christos sc->sc_gpio_wp = fdtbus_gpio_acquire(sc->sc_slot_phandle, "wp-gpios",
219 1.1.6.2 christos GPIO_PIN_INPUT);
220 1.1.6.2 christos
221 1.1.6.2 christos sc->sc_gpio_cd_inverted = of_hasprop(sc->sc_slot_phandle, "cd-inverted");
222 1.1.6.2 christos sc->sc_gpio_wp_inverted = of_hasprop(sc->sc_slot_phandle, "wp-inverted");
223 1.1.6.2 christos
224 1.1.6.2 christos sc->sc_non_removable = of_hasprop(sc->sc_slot_phandle, "non-removable");
225 1.1.6.2 christos sc->sc_broken_cd = of_hasprop(sc->sc_slot_phandle, "broken-cd");
226 1.1.6.2 christos
227 1.1.6.2 christos sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, 0, meson_sdio_intr, sc);
228 1.1.6.2 christos if (sc->sc_ih == NULL) {
229 1.1.6.2 christos aprint_error_dev(self, "couldn't establish interrupt on %s\n",
230 1.1.6.2 christos intrstr);
231 1.1.6.2 christos return;
232 1.1.6.2 christos }
233 1.1.6.2 christos aprint_normal_dev(self, "interrupting on %s\n", intrstr);
234 1.1.6.2 christos
235 1.1.6.2 christos sc->sc_bus_freq = clk_get_rate(clk_clkin);
236 1.1.6.2 christos
237 1.1.6.2 christos aprint_normal_dev(self, "core %u Hz, clkin %u Hz\n", clk_get_rate(clk_core), clk_get_rate(clk_clkin));
238 1.1.6.2 christos
239 1.1.6.2 christos meson_sdio_dmainit(sc);
240 1.1.6.2 christos
241 1.1.6.2 christos config_interrupts(self, meson_sdio_attach_i);
242 1.1.6.2 christos }
243 1.1.6.2 christos
244 1.1.6.2 christos static void
245 1.1.6.2 christos meson_sdio_attach_i(device_t self)
246 1.1.6.2 christos {
247 1.1.6.2 christos struct meson_sdio_softc *sc = device_private(self);
248 1.1.6.2 christos struct sdmmcbus_attach_args saa;
249 1.1.6.2 christos
250 1.1.6.2 christos meson_sdio_signal_voltage(sc, SDMMC_SIGNAL_VOLTAGE_330);
251 1.1.6.2 christos meson_sdio_host_reset(sc);
252 1.1.6.2 christos meson_sdio_bus_clock(sc, 400);
253 1.1.6.2 christos meson_sdio_bus_width(sc, 1);
254 1.1.6.2 christos
255 1.1.6.2 christos memset(&saa, 0, sizeof(saa));
256 1.1.6.2 christos saa.saa_busname = "sdmmc";
257 1.1.6.2 christos saa.saa_sct = &meson_sdio_chip_functions;
258 1.1.6.2 christos saa.saa_dmat = sc->sc_dmat;
259 1.1.6.2 christos saa.saa_sch = sc;
260 1.1.6.2 christos saa.saa_clkmin = 400;
261 1.1.6.2 christos saa.saa_clkmax = sc->sc_bus_freq;
262 1.1.6.2 christos /* Do not advertise DMA capabilities, we handle DMA ourselves */
263 1.1.6.2 christos saa.saa_caps = SMC_CAPS_4BIT_MODE|
264 1.1.6.2 christos SMC_CAPS_SD_HIGHSPEED|
265 1.1.6.2 christos SMC_CAPS_MMC_HIGHSPEED;
266 1.1.6.2 christos
267 1.1.6.2 christos sc->sc_sdmmc_dev = config_found(self, &saa, NULL);
268 1.1.6.2 christos }
269 1.1.6.2 christos
270 1.1.6.2 christos static int
271 1.1.6.2 christos meson_sdio_intr(void *priv)
272 1.1.6.2 christos {
273 1.1.6.2 christos struct meson_sdio_softc *sc = priv;
274 1.1.6.2 christos
275 1.1.6.2 christos mutex_enter(&sc->sc_intr_lock);
276 1.1.6.2 christos const u_int irqs = SDIO_READ(sc, SDIO_IRQS_REG);
277 1.1.6.2 christos if (irqs & SDIO_IRQS_CLEAR) {
278 1.1.6.2 christos SDIO_WRITE(sc, SDIO_IRQS_REG, irqs);
279 1.1.6.2 christos sc->sc_intr_irqs |= irqs;
280 1.1.6.2 christos cv_broadcast(&sc->sc_intr_cv);
281 1.1.6.2 christos }
282 1.1.6.2 christos mutex_exit(&sc->sc_intr_lock);
283 1.1.6.2 christos
284 1.1.6.2 christos return 1;
285 1.1.6.2 christos }
286 1.1.6.2 christos
287 1.1.6.2 christos static void
288 1.1.6.2 christos meson_sdio_dmainit(struct meson_sdio_softc *sc)
289 1.1.6.2 christos {
290 1.1.6.2 christos int error, rseg;
291 1.1.6.2 christos
292 1.1.6.2 christos error = bus_dmamem_alloc(sc->sc_dmat, MAXPHYS, PAGE_SIZE, MAXPHYS,
293 1.1.6.2 christos sc->sc_segs, 1, &rseg, BUS_DMA_WAITOK);
294 1.1.6.2 christos if (error) {
295 1.1.6.2 christos device_printf(sc->sc_dev, "bus_dmamem_alloc failed\n");
296 1.1.6.2 christos return;
297 1.1.6.2 christos }
298 1.1.6.2 christos KASSERT(rseg == 1);
299 1.1.6.2 christos
300 1.1.6.2 christos error = bus_dmamem_map(sc->sc_dmat, sc->sc_segs, rseg, MAXPHYS,
301 1.1.6.2 christos &sc->sc_bbuf, BUS_DMA_WAITOK);
302 1.1.6.2 christos if (error) {
303 1.1.6.2 christos device_printf(sc->sc_dev, "bus_dmamem_map failed\n");
304 1.1.6.2 christos return;
305 1.1.6.2 christos }
306 1.1.6.2 christos
307 1.1.6.2 christos error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, 1, MAXPHYS, 0,
308 1.1.6.2 christos BUS_DMA_WAITOK, &sc->sc_dmamap);
309 1.1.6.2 christos if (error) {
310 1.1.6.2 christos device_printf(sc->sc_dev, "bus_dmamap_create failed\n");
311 1.1.6.2 christos return;
312 1.1.6.2 christos }
313 1.1.6.2 christos }
314 1.1.6.2 christos
315 1.1.6.2 christos static int
316 1.1.6.2 christos meson_sdio_set_clock(struct meson_sdio_softc *sc, u_int freq)
317 1.1.6.2 christos {
318 1.1.6.2 christos const u_int pll_freq = sc->sc_bus_freq / 2000;
319 1.1.6.2 christos uint32_t conf;
320 1.1.6.2 christos int clk_div;
321 1.1.6.2 christos
322 1.1.6.2 christos if (freq == 0)
323 1.1.6.2 christos return 0;
324 1.1.6.2 christos
325 1.1.6.2 christos clk_div = howmany(pll_freq, freq);
326 1.1.6.2 christos
327 1.1.6.2 christos conf = SDIO_READ(sc, SDIO_CONF_REG);
328 1.1.6.2 christos conf &= ~SDIO_CONF_COMMAND_CLK_DIV;
329 1.1.6.2 christos conf |= __SHIFTIN(clk_div - 1, SDIO_CONF_COMMAND_CLK_DIV);
330 1.1.6.2 christos SDIO_WRITE(sc, SDIO_CONF_REG, conf);
331 1.1.6.2 christos
332 1.1.6.2 christos return 0;
333 1.1.6.2 christos }
334 1.1.6.2 christos
335 1.1.6.2 christos static int
336 1.1.6.2 christos meson_sdio_wait_irqs(struct meson_sdio_softc *sc, uint32_t mask, int timeout)
337 1.1.6.2 christos {
338 1.1.6.2 christos int retry, error;
339 1.1.6.2 christos
340 1.1.6.2 christos KASSERT(mutex_owned(&sc->sc_intr_lock));
341 1.1.6.2 christos
342 1.1.6.2 christos if (sc->sc_intr_irqs & mask)
343 1.1.6.2 christos return 0;
344 1.1.6.2 christos
345 1.1.6.2 christos retry = timeout / hz;
346 1.1.6.2 christos
347 1.1.6.2 christos while (retry > 0) {
348 1.1.6.2 christos error = cv_timedwait(&sc->sc_intr_cv, &sc->sc_intr_lock, hz);
349 1.1.6.2 christos if (error && error != EWOULDBLOCK)
350 1.1.6.2 christos return error;
351 1.1.6.2 christos if (sc->sc_intr_irqs & mask)
352 1.1.6.2 christos return 0;
353 1.1.6.2 christos --retry;
354 1.1.6.2 christos }
355 1.1.6.2 christos
356 1.1.6.2 christos return ETIMEDOUT;
357 1.1.6.2 christos }
358 1.1.6.2 christos
359 1.1.6.2 christos static int
360 1.1.6.2 christos meson_sdio_host_reset(sdmmc_chipset_handle_t sch)
361 1.1.6.2 christos {
362 1.1.6.2 christos struct meson_sdio_softc *sc = sch;
363 1.1.6.2 christos
364 1.1.6.2 christos SDIO_WRITE(sc, SDIO_IRQC_REG, SDIO_IRQC_SOFT_RESET);
365 1.1.6.2 christos
366 1.1.6.2 christos delay(2);
367 1.1.6.2 christos
368 1.1.6.2 christos SDIO_WRITE(sc, SDIO_IRQS_REG, SDIO_IRQS_CLEAR);
369 1.1.6.2 christos SDIO_WRITE(sc, SDIO_CONF_REG,
370 1.1.6.2 christos __SHIFTIN(2, SDIO_CONF_WRITE_CRC_OK_STATUS) |
371 1.1.6.2 christos __SHIFTIN(2, SDIO_CONF_WRITE_NWR) |
372 1.1.6.2 christos __SHIFTIN(3, SDIO_CONF_M_ENDIAN) |
373 1.1.6.2 christos __SHIFTIN(39, SDIO_CONF_COMMAND_ARG_BITS) |
374 1.1.6.2 christos __SHIFTIN(0x1f4, SDIO_CONF_COMMAND_CLK_DIV));
375 1.1.6.2 christos
376 1.1.6.2 christos SDIO_WRITE(sc, SDIO_MULT_REG,
377 1.1.6.2 christos __SHIFTIN(sc->sc_cur_port, SDIO_MULT_PORT_SEL));
378 1.1.6.2 christos
379 1.1.6.2 christos return 0;
380 1.1.6.2 christos }
381 1.1.6.2 christos
382 1.1.6.2 christos static uint32_t
383 1.1.6.2 christos meson_sdio_host_ocr(sdmmc_chipset_handle_t sch)
384 1.1.6.2 christos {
385 1.1.6.2 christos return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V;
386 1.1.6.2 christos }
387 1.1.6.2 christos
388 1.1.6.2 christos static int
389 1.1.6.2 christos meson_sdio_host_maxblklen(sdmmc_chipset_handle_t sch)
390 1.1.6.2 christos {
391 1.1.6.2 christos return 512;
392 1.1.6.2 christos }
393 1.1.6.2 christos
394 1.1.6.2 christos static int
395 1.1.6.2 christos meson_sdio_card_detect(sdmmc_chipset_handle_t sch)
396 1.1.6.2 christos {
397 1.1.6.2 christos struct meson_sdio_softc *sc = sch;
398 1.1.6.2 christos int val;
399 1.1.6.2 christos
400 1.1.6.2 christos if (sc->sc_non_removable || sc->sc_broken_cd) {
401 1.1.6.2 christos return 1;
402 1.1.6.2 christos } else if (sc->sc_gpio_cd != NULL) {
403 1.1.6.2 christos val = fdtbus_gpio_read(sc->sc_gpio_cd);
404 1.1.6.2 christos if (sc->sc_gpio_cd_inverted)
405 1.1.6.2 christos val = !val;
406 1.1.6.2 christos return val;
407 1.1.6.2 christos } else {
408 1.1.6.2 christos return 1;
409 1.1.6.2 christos }
410 1.1.6.2 christos }
411 1.1.6.2 christos
412 1.1.6.2 christos static int
413 1.1.6.2 christos meson_sdio_write_protect(sdmmc_chipset_handle_t sch)
414 1.1.6.2 christos {
415 1.1.6.2 christos struct meson_sdio_softc *sc = sch;
416 1.1.6.2 christos int val;
417 1.1.6.2 christos
418 1.1.6.2 christos if (sc->sc_gpio_wp != NULL) {
419 1.1.6.2 christos val = fdtbus_gpio_read(sc->sc_gpio_wp);
420 1.1.6.2 christos if (sc->sc_gpio_wp_inverted)
421 1.1.6.2 christos val = !val;
422 1.1.6.2 christos return val;
423 1.1.6.2 christos }
424 1.1.6.2 christos
425 1.1.6.2 christos return 0;
426 1.1.6.2 christos }
427 1.1.6.2 christos
428 1.1.6.2 christos static int
429 1.1.6.2 christos meson_sdio_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
430 1.1.6.2 christos {
431 1.1.6.2 christos return 0;
432 1.1.6.2 christos }
433 1.1.6.2 christos
434 1.1.6.2 christos static int
435 1.1.6.2 christos meson_sdio_bus_clock(sdmmc_chipset_handle_t sch, int freq)
436 1.1.6.2 christos {
437 1.1.6.2 christos struct meson_sdio_softc *sc = sch;
438 1.1.6.2 christos
439 1.1.6.2 christos return meson_sdio_set_clock(sc, freq);
440 1.1.6.2 christos }
441 1.1.6.2 christos
442 1.1.6.2 christos static int
443 1.1.6.2 christos meson_sdio_bus_width(sdmmc_chipset_handle_t sch, int width)
444 1.1.6.2 christos {
445 1.1.6.2 christos struct meson_sdio_softc *sc = sch;
446 1.1.6.2 christos uint32_t conf;
447 1.1.6.2 christos
448 1.1.6.2 christos conf = SDIO_READ(sc, SDIO_CONF_REG);
449 1.1.6.2 christos if (width == 1) {
450 1.1.6.2 christos conf &= ~SDIO_CONF_BUS_WIDTH;
451 1.1.6.2 christos } else if (width == 4) {
452 1.1.6.2 christos conf |= SDIO_CONF_BUS_WIDTH;
453 1.1.6.2 christos } else {
454 1.1.6.2 christos return EINVAL;
455 1.1.6.2 christos }
456 1.1.6.2 christos SDIO_WRITE(sc, SDIO_CONF_REG, conf);
457 1.1.6.2 christos
458 1.1.6.2 christos sc->sc_cur_width = width;
459 1.1.6.2 christos
460 1.1.6.2 christos return 0;
461 1.1.6.2 christos }
462 1.1.6.2 christos
463 1.1.6.2 christos static int
464 1.1.6.2 christos meson_sdio_bus_rod(sdmmc_chipset_handle_t sch, int on)
465 1.1.6.2 christos {
466 1.1.6.2 christos return ENOTSUP;
467 1.1.6.2 christos }
468 1.1.6.2 christos
469 1.1.6.2 christos static int
470 1.1.6.2 christos meson_sdio_signal_voltage(sdmmc_chipset_handle_t sch, int signal_voltage)
471 1.1.6.2 christos {
472 1.1.6.2 christos struct meson_sdio_softc *sc = sch;
473 1.1.6.2 christos u_int uvol;
474 1.1.6.2 christos int error;
475 1.1.6.2 christos
476 1.1.6.2 christos if (sc->sc_reg_vqmmc == NULL)
477 1.1.6.2 christos return 0;
478 1.1.6.2 christos
479 1.1.6.2 christos switch (signal_voltage) {
480 1.1.6.2 christos case SDMMC_SIGNAL_VOLTAGE_330:
481 1.1.6.2 christos uvol = 3300000;
482 1.1.6.2 christos break;
483 1.1.6.2 christos case SDMMC_SIGNAL_VOLTAGE_180:
484 1.1.6.2 christos uvol = 1800000;
485 1.1.6.2 christos break;
486 1.1.6.2 christos default:
487 1.1.6.2 christos return EINVAL;
488 1.1.6.2 christos }
489 1.1.6.2 christos
490 1.1.6.2 christos error = fdtbus_regulator_supports_voltage(sc->sc_reg_vqmmc, uvol, uvol);
491 1.1.6.2 christos if (error != 0)
492 1.1.6.2 christos return 0;
493 1.1.6.2 christos
494 1.1.6.2 christos error = fdtbus_regulator_set_voltage(sc->sc_reg_vqmmc, uvol, uvol);
495 1.1.6.2 christos if (error != 0)
496 1.1.6.2 christos return error;
497 1.1.6.2 christos
498 1.1.6.2 christos return fdtbus_regulator_enable(sc->sc_reg_vqmmc);
499 1.1.6.2 christos }
500 1.1.6.2 christos
501 1.1.6.2 christos static void
502 1.1.6.2 christos meson_sdio_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
503 1.1.6.2 christos {
504 1.1.6.2 christos struct meson_sdio_softc *sc = sch;
505 1.1.6.2 christos uint32_t send, ext, mult, addr;
506 1.1.6.2 christos bool use_bbuf = false;
507 1.1.6.2 christos int i;
508 1.1.6.2 christos
509 1.1.6.2 christos KASSERT(cmd->c_blklen <= 512);
510 1.1.6.2 christos
511 1.1.6.2 christos send = ext = mult = addr = 0;
512 1.1.6.2 christos
513 1.1.6.2 christos mutex_enter(&sc->sc_intr_lock);
514 1.1.6.2 christos
515 1.1.6.2 christos if (cmd->c_opcode == SD_IO_SEND_OP_COND ||
516 1.1.6.2 christos cmd->c_opcode == SD_IO_RW_DIRECT ||
517 1.1.6.2 christos cmd->c_opcode == SD_IO_RW_EXTENDED) {
518 1.1.6.2 christos cmd->c_error = EINVAL;
519 1.1.6.2 christos goto done;
520 1.1.6.2 christos }
521 1.1.6.2 christos
522 1.1.6.2 christos sc->sc_intr_irqs = 0;
523 1.1.6.2 christos
524 1.1.6.2 christos if (cmd->c_flags & SCF_RSP_PRESENT) {
525 1.1.6.2 christos if (cmd->c_flags & SCF_RSP_136) {
526 1.1.6.2 christos send |= __SHIFTIN(133, SDIO_SEND_RESPONSE_BITS);
527 1.1.6.2 christos send |= SDIO_SEND_RESPONSE_CRC7_FROM_8;
528 1.1.6.2 christos } else {
529 1.1.6.2 christos send |= __SHIFTIN(45, SDIO_SEND_RESPONSE_BITS);
530 1.1.6.2 christos }
531 1.1.6.2 christos }
532 1.1.6.2 christos if ((cmd->c_flags & SCF_RSP_CRC) == 0) {
533 1.1.6.2 christos send |= SDIO_SEND_RESPONSE_NO_CRC;
534 1.1.6.2 christos }
535 1.1.6.2 christos if (cmd->c_flags & SCF_RSP_BSY) {
536 1.1.6.2 christos send |= SDIO_SEND_CHECK_BUSY_DAT0;
537 1.1.6.2 christos }
538 1.1.6.2 christos
539 1.1.6.2 christos if (cmd->c_datalen > 0) {
540 1.1.6.2 christos unsigned int nblks, packlen;
541 1.1.6.2 christos
542 1.1.6.2 christos nblks = cmd->c_datalen / cmd->c_blklen;
543 1.1.6.2 christos if (nblks == 0 || (cmd->c_datalen % cmd->c_blklen) != 0)
544 1.1.6.2 christos ++nblks;
545 1.1.6.2 christos packlen = (cmd->c_blklen * 8) + (0xf * sc->sc_cur_width);
546 1.1.6.2 christos
547 1.1.6.2 christos send |= __SHIFTIN(nblks - 1, SDIO_SEND_REPEAT_PACKAGE);
548 1.1.6.2 christos ext |= __SHIFTIN(packlen, SDIO_EXT_DATA_RW_NUMBER);
549 1.1.6.2 christos
550 1.1.6.2 christos if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
551 1.1.6.2 christos send |= SDIO_SEND_RESPONSE_DATA;
552 1.1.6.2 christos } else {
553 1.1.6.2 christos send |= SDIO_SEND_COMMAND_HAS_DATA;
554 1.1.6.2 christos }
555 1.1.6.2 christos
556 1.1.6.2 christos cmd->c_error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
557 1.1.6.2 christos sc->sc_bbuf, MAXPHYS, NULL, BUS_DMA_WAITOK);
558 1.1.6.2 christos if (cmd->c_error) {
559 1.1.6.2 christos device_printf(sc->sc_dev, "bus_dmamap_load failed\n");
560 1.1.6.2 christos goto done;
561 1.1.6.2 christos }
562 1.1.6.2 christos if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
563 1.1.6.2 christos bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
564 1.1.6.2 christos MAXPHYS, BUS_DMASYNC_PREREAD);
565 1.1.6.2 christos } else {
566 1.1.6.2 christos memcpy(sc->sc_bbuf, cmd->c_data, cmd->c_datalen);
567 1.1.6.2 christos bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
568 1.1.6.2 christos MAXPHYS, BUS_DMASYNC_PREWRITE);
569 1.1.6.2 christos }
570 1.1.6.2 christos addr = sc->sc_dmamap->dm_segs[0].ds_addr;
571 1.1.6.2 christos use_bbuf = true;
572 1.1.6.2 christos }
573 1.1.6.2 christos send |= __SHIFTIN(cmd->c_opcode | 0x40, SDIO_SEND_COMMAND_INDEX);
574 1.1.6.2 christos
575 1.1.6.2 christos mult |= __SHIFTIN(sc->sc_cur_port, SDIO_MULT_PORT_SEL);
576 1.1.6.2 christos
577 1.1.6.2 christos SDIO_WRITE(sc, SDIO_IRQC_REG, SDIO_IRQC_SOFT_RESET);
578 1.1.6.2 christos delay(2);
579 1.1.6.2 christos
580 1.1.6.2 christos SDIO_WRITE(sc, SDIO_IRQC_REG, SDIO_IRQC_ARC_CMD_INTEN);
581 1.1.6.2 christos SDIO_WRITE(sc, SDIO_IRQS_REG, SDIO_IRQS_CLEAR);
582 1.1.6.2 christos
583 1.1.6.2 christos SDIO_WRITE(sc, SDIO_ARGU_REG, cmd->c_arg);
584 1.1.6.2 christos SDIO_WRITE(sc, SDIO_MULT_REG, mult);
585 1.1.6.2 christos SDIO_WRITE(sc, SDIO_EXT_REG, ext);
586 1.1.6.2 christos SDIO_WRITE(sc, SDIO_ADDR_REG, addr);
587 1.1.6.2 christos SDIO_WRITE(sc, SDIO_SEND_REG, send);
588 1.1.6.2 christos
589 1.1.6.2 christos cmd->c_error = meson_sdio_wait_irqs(sc, SDIO_IRQS_CMD_INT, hz * 3);
590 1.1.6.2 christos if (cmd->c_error) {
591 1.1.6.2 christos goto done;
592 1.1.6.2 christos }
593 1.1.6.2 christos
594 1.1.6.2 christos if (SDIO_READ(sc, SDIO_IRQS_REG) & SDIO_IRQS_CMD_BUSY) {
595 1.1.6.2 christos int retry;
596 1.1.6.2 christos for (retry = 10000; retry > 0; retry--) {
597 1.1.6.2 christos const uint32_t irqs = SDIO_READ(sc, SDIO_IRQS_REG);
598 1.1.6.2 christos if ((irqs & SDIO_IRQS_CMD_BUSY) == 0)
599 1.1.6.2 christos break;
600 1.1.6.2 christos delay(100);
601 1.1.6.2 christos }
602 1.1.6.2 christos if (retry == 0) {
603 1.1.6.2 christos aprint_debug_dev(sc->sc_dev,
604 1.1.6.2 christos "busy timeout, opcode %d flags %#x datalen %d\n",
605 1.1.6.2 christos cmd->c_opcode, cmd->c_flags, cmd->c_datalen);
606 1.1.6.2 christos cmd->c_error = ETIMEDOUT;
607 1.1.6.2 christos goto done;
608 1.1.6.2 christos }
609 1.1.6.2 christos }
610 1.1.6.2 christos
611 1.1.6.2 christos const uint32_t irqs = SDIO_READ(sc, SDIO_IRQS_REG);
612 1.1.6.2 christos if (cmd->c_flags & SCF_RSP_CRC) {
613 1.1.6.2 christos if ((irqs & SDIO_IRQS_RESPONSE_CRC7_OK) == 0) {
614 1.1.6.2 christos device_printf(sc->sc_dev, "response crc error\n");
615 1.1.6.2 christos cmd->c_error = EIO;
616 1.1.6.2 christos goto done;
617 1.1.6.2 christos }
618 1.1.6.2 christos }
619 1.1.6.2 christos if (cmd->c_datalen > 0) {
620 1.1.6.2 christos uint32_t crcmask = SDIO_IRQS_DATA_READ_CRC16_OK|
621 1.1.6.2 christos SDIO_IRQS_DATA_WRITE_CRC16_OK;
622 1.1.6.2 christos if ((irqs & crcmask) == 0) {
623 1.1.6.2 christos device_printf(sc->sc_dev, "data crc error\n");
624 1.1.6.2 christos cmd->c_error = EIO;
625 1.1.6.2 christos goto done;
626 1.1.6.2 christos }
627 1.1.6.2 christos }
628 1.1.6.2 christos
629 1.1.6.2 christos if (cmd->c_flags & SCF_RSP_PRESENT) {
630 1.1.6.2 christos mult |= SDIO_MULT_WRITE_READ_OUT_INDEX;
631 1.1.6.2 christos mult &= ~SDIO_MULT_RESPONSE_READ_INDEX;
632 1.1.6.2 christos SDIO_WRITE(sc, SDIO_MULT_REG, mult);
633 1.1.6.2 christos
634 1.1.6.2 christos if (cmd->c_flags & SCF_RSP_136) {
635 1.1.6.2 christos for (i = 0; i < 4; i++) {
636 1.1.6.2 christos cmd->c_resp[i] = SDIO_READ(sc, SDIO_ARGU_REG);
637 1.1.6.2 christos }
638 1.1.6.2 christos } else {
639 1.1.6.2 christos cmd->c_resp[0] = SDIO_READ(sc, SDIO_ARGU_REG);
640 1.1.6.2 christos }
641 1.1.6.2 christos }
642 1.1.6.2 christos
643 1.1.6.2 christos done:
644 1.1.6.2 christos if (use_bbuf) {
645 1.1.6.2 christos if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
646 1.1.6.2 christos bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
647 1.1.6.2 christos MAXPHYS, BUS_DMASYNC_POSTREAD);
648 1.1.6.2 christos } else {
649 1.1.6.2 christos bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
650 1.1.6.2 christos MAXPHYS, BUS_DMASYNC_POSTWRITE);
651 1.1.6.2 christos }
652 1.1.6.2 christos bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
653 1.1.6.2 christos if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
654 1.1.6.2 christos memcpy(cmd->c_data, sc->sc_bbuf, cmd->c_datalen);
655 1.1.6.2 christos }
656 1.1.6.2 christos }
657 1.1.6.2 christos cmd->c_flags |= SCF_ITSDONE;
658 1.1.6.2 christos
659 1.1.6.2 christos SDIO_WRITE(sc, SDIO_IRQC_REG, 0);
660 1.1.6.2 christos SDIO_WRITE(sc, SDIO_IRQS_REG, SDIO_IRQS_CLEAR);
661 1.1.6.2 christos
662 1.1.6.2 christos mutex_exit(&sc->sc_intr_lock);
663 1.1.6.2 christos }
664 1.1.6.2 christos
665 1.1.6.2 christos static void
666 1.1.6.2 christos meson_sdio_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
667 1.1.6.2 christos {
668 1.1.6.2 christos struct meson_sdio_softc *sc = sch;
669 1.1.6.2 christos uint32_t irqc;
670 1.1.6.2 christos
671 1.1.6.2 christos mutex_enter(&sc->sc_intr_lock);
672 1.1.6.2 christos irqc = SDIO_READ(sc, SDIO_IRQC_REG);
673 1.1.6.2 christos if (enable) {
674 1.1.6.2 christos irqc |= SDIO_IRQC_ARC_IF_INTEN;
675 1.1.6.2 christos } else {
676 1.1.6.2 christos irqc &= ~SDIO_IRQC_ARC_IF_INTEN;
677 1.1.6.2 christos }
678 1.1.6.2 christos SDIO_WRITE(sc, SDIO_IRQC_REG, irqc);
679 1.1.6.2 christos mutex_exit(&sc->sc_intr_lock);
680 1.1.6.2 christos }
681 1.1.6.2 christos
682 1.1.6.2 christos static void
683 1.1.6.2 christos meson_sdio_card_intr_ack(sdmmc_chipset_handle_t sch)
684 1.1.6.2 christos {
685 1.1.6.2 christos struct meson_sdio_softc *sc = sch;
686 1.1.6.2 christos
687 1.1.6.2 christos mutex_enter(&sc->sc_intr_lock);
688 1.1.6.2 christos SDIO_WRITE(sc, SDIO_IRQS_REG, SDIO_IRQS_IF_INT);
689 1.1.6.2 christos mutex_exit(&sc->sc_intr_lock);
690 1.1.6.2 christos }
691