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