sunxi_mmc.c revision 1.34 1 1.34 jmcneill /* $NetBSD: sunxi_mmc.c,v 1.34 2019/09/01 11:44:23 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2014-2017 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.13 jmcneill #include "opt_sunximmc.h"
30 1.13 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.34 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_mmc.c,v 1.34 2019/09/01 11:44:23 jmcneill Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/bus.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/intr.h>
38 1.1 jmcneill #include <sys/systm.h>
39 1.1 jmcneill #include <sys/kernel.h>
40 1.1 jmcneill #include <sys/gpio.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/sdmmc/sdmmcvar.h>
43 1.1 jmcneill #include <dev/sdmmc/sdmmcchip.h>
44 1.1 jmcneill #include <dev/sdmmc/sdmmc_ioreg.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <dev/fdt/fdtvar.h>
47 1.1 jmcneill
48 1.1 jmcneill #include <arm/sunxi/sunxi_mmc.h>
49 1.1 jmcneill
50 1.13 jmcneill #ifdef SUNXI_MMC_DEBUG
51 1.13 jmcneill static int sunxi_mmc_debug = SUNXI_MMC_DEBUG;
52 1.13 jmcneill #define DPRINTF(dev, fmt, ...) \
53 1.13 jmcneill do { \
54 1.13 jmcneill if (sunxi_mmc_debug & __BIT(device_unit(dev))) \
55 1.13 jmcneill device_printf((dev), fmt, ##__VA_ARGS__); \
56 1.13 jmcneill } while (0)
57 1.13 jmcneill #else
58 1.13 jmcneill #define DPRINTF(dev, fmt, ...) ((void)0)
59 1.13 jmcneill #endif
60 1.13 jmcneill
61 1.3 jmcneill enum sunxi_mmc_timing {
62 1.3 jmcneill SUNXI_MMC_TIMING_400K,
63 1.3 jmcneill SUNXI_MMC_TIMING_25M,
64 1.3 jmcneill SUNXI_MMC_TIMING_50M,
65 1.3 jmcneill SUNXI_MMC_TIMING_50M_DDR,
66 1.3 jmcneill SUNXI_MMC_TIMING_50M_DDR_8BIT,
67 1.3 jmcneill };
68 1.3 jmcneill
69 1.3 jmcneill struct sunxi_mmc_delay {
70 1.3 jmcneill u_int output_phase;
71 1.3 jmcneill u_int sample_phase;
72 1.3 jmcneill };
73 1.3 jmcneill
74 1.10 jmcneill static const struct sunxi_mmc_delay sun7i_mmc_delays[] = {
75 1.3 jmcneill [SUNXI_MMC_TIMING_400K] = { 180, 180 },
76 1.3 jmcneill [SUNXI_MMC_TIMING_25M] = { 180, 75 },
77 1.3 jmcneill [SUNXI_MMC_TIMING_50M] = { 90, 120 },
78 1.3 jmcneill [SUNXI_MMC_TIMING_50M_DDR] = { 60, 120 },
79 1.3 jmcneill [SUNXI_MMC_TIMING_50M_DDR_8BIT] = { 90, 180 },
80 1.3 jmcneill };
81 1.3 jmcneill
82 1.10 jmcneill static const struct sunxi_mmc_delay sun9i_mmc_delays[] = {
83 1.10 jmcneill [SUNXI_MMC_TIMING_400K] = { 180, 180 },
84 1.10 jmcneill [SUNXI_MMC_TIMING_25M] = { 180, 75 },
85 1.10 jmcneill [SUNXI_MMC_TIMING_50M] = { 150, 120 },
86 1.10 jmcneill [SUNXI_MMC_TIMING_50M_DDR] = { 54, 36 },
87 1.10 jmcneill [SUNXI_MMC_TIMING_50M_DDR_8BIT] = { 72, 72 },
88 1.10 jmcneill };
89 1.10 jmcneill
90 1.21 ryo #define SUNXI_MMC_NDESC 64
91 1.1 jmcneill
92 1.1 jmcneill struct sunxi_mmc_softc;
93 1.1 jmcneill
94 1.1 jmcneill static int sunxi_mmc_match(device_t, cfdata_t, void *);
95 1.1 jmcneill static void sunxi_mmc_attach(device_t, device_t, void *);
96 1.1 jmcneill static void sunxi_mmc_attach_i(device_t);
97 1.1 jmcneill
98 1.1 jmcneill static int sunxi_mmc_intr(void *);
99 1.14 jmcneill static int sunxi_mmc_dmabounce_setup(struct sunxi_mmc_softc *);
100 1.1 jmcneill static int sunxi_mmc_idma_setup(struct sunxi_mmc_softc *);
101 1.1 jmcneill
102 1.1 jmcneill static int sunxi_mmc_host_reset(sdmmc_chipset_handle_t);
103 1.1 jmcneill static uint32_t sunxi_mmc_host_ocr(sdmmc_chipset_handle_t);
104 1.1 jmcneill static int sunxi_mmc_host_maxblklen(sdmmc_chipset_handle_t);
105 1.1 jmcneill static int sunxi_mmc_card_detect(sdmmc_chipset_handle_t);
106 1.1 jmcneill static int sunxi_mmc_write_protect(sdmmc_chipset_handle_t);
107 1.1 jmcneill static int sunxi_mmc_bus_power(sdmmc_chipset_handle_t, uint32_t);
108 1.3 jmcneill static int sunxi_mmc_bus_clock(sdmmc_chipset_handle_t, int, bool);
109 1.1 jmcneill static int sunxi_mmc_bus_width(sdmmc_chipset_handle_t, int);
110 1.1 jmcneill static int sunxi_mmc_bus_rod(sdmmc_chipset_handle_t, int);
111 1.3 jmcneill static int sunxi_mmc_signal_voltage(sdmmc_chipset_handle_t, int);
112 1.23 jmcneill static int sunxi_mmc_execute_tuning(sdmmc_chipset_handle_t, int);
113 1.1 jmcneill static void sunxi_mmc_exec_command(sdmmc_chipset_handle_t,
114 1.1 jmcneill struct sdmmc_command *);
115 1.1 jmcneill static void sunxi_mmc_card_enable_intr(sdmmc_chipset_handle_t, int);
116 1.1 jmcneill static void sunxi_mmc_card_intr_ack(sdmmc_chipset_handle_t);
117 1.1 jmcneill
118 1.1 jmcneill static struct sdmmc_chip_functions sunxi_mmc_chip_functions = {
119 1.1 jmcneill .host_reset = sunxi_mmc_host_reset,
120 1.1 jmcneill .host_ocr = sunxi_mmc_host_ocr,
121 1.1 jmcneill .host_maxblklen = sunxi_mmc_host_maxblklen,
122 1.1 jmcneill .card_detect = sunxi_mmc_card_detect,
123 1.1 jmcneill .write_protect = sunxi_mmc_write_protect,
124 1.1 jmcneill .bus_power = sunxi_mmc_bus_power,
125 1.3 jmcneill .bus_clock_ddr = sunxi_mmc_bus_clock,
126 1.1 jmcneill .bus_width = sunxi_mmc_bus_width,
127 1.1 jmcneill .bus_rod = sunxi_mmc_bus_rod,
128 1.3 jmcneill .signal_voltage = sunxi_mmc_signal_voltage,
129 1.23 jmcneill .execute_tuning = sunxi_mmc_execute_tuning,
130 1.1 jmcneill .exec_command = sunxi_mmc_exec_command,
131 1.1 jmcneill .card_enable_intr = sunxi_mmc_card_enable_intr,
132 1.1 jmcneill .card_intr_ack = sunxi_mmc_card_intr_ack,
133 1.1 jmcneill };
134 1.1 jmcneill
135 1.7 jmcneill struct sunxi_mmc_config {
136 1.7 jmcneill u_int idma_xferlen;
137 1.7 jmcneill u_int flags;
138 1.7 jmcneill #define SUNXI_MMC_FLAG_CALIB_REG 0x01
139 1.7 jmcneill #define SUNXI_MMC_FLAG_NEW_TIMINGS 0x02
140 1.7 jmcneill #define SUNXI_MMC_FLAG_MASK_DATA0 0x04
141 1.23 jmcneill #define SUNXI_MMC_FLAG_HS200 0x08
142 1.7 jmcneill const struct sunxi_mmc_delay *delays;
143 1.7 jmcneill uint32_t dma_ftrglevel;
144 1.7 jmcneill };
145 1.7 jmcneill
146 1.1 jmcneill struct sunxi_mmc_softc {
147 1.1 jmcneill device_t sc_dev;
148 1.1 jmcneill bus_space_tag_t sc_bst;
149 1.1 jmcneill bus_space_handle_t sc_bsh;
150 1.1 jmcneill bus_dma_tag_t sc_dmat;
151 1.1 jmcneill int sc_phandle;
152 1.1 jmcneill
153 1.1 jmcneill void *sc_ih;
154 1.1 jmcneill kmutex_t sc_intr_lock;
155 1.1 jmcneill kcondvar_t sc_intr_cv;
156 1.1 jmcneill kcondvar_t sc_idst_cv;
157 1.1 jmcneill
158 1.1 jmcneill int sc_mmc_width;
159 1.1 jmcneill int sc_mmc_present;
160 1.1 jmcneill
161 1.23 jmcneill u_int sc_max_frequency;
162 1.23 jmcneill
163 1.1 jmcneill device_t sc_sdmmc_dev;
164 1.1 jmcneill
165 1.7 jmcneill struct sunxi_mmc_config *sc_config;
166 1.1 jmcneill
167 1.1 jmcneill bus_dma_segment_t sc_idma_segs[1];
168 1.1 jmcneill int sc_idma_nsegs;
169 1.1 jmcneill bus_size_t sc_idma_size;
170 1.1 jmcneill bus_dmamap_t sc_idma_map;
171 1.1 jmcneill int sc_idma_ndesc;
172 1.1 jmcneill void *sc_idma_desc;
173 1.1 jmcneill
174 1.14 jmcneill bus_dmamap_t sc_dmabounce_map;
175 1.14 jmcneill void *sc_dmabounce_buf;
176 1.14 jmcneill size_t sc_dmabounce_buflen;
177 1.14 jmcneill
178 1.1 jmcneill uint32_t sc_intr_rint;
179 1.1 jmcneill uint32_t sc_idma_idst;
180 1.1 jmcneill
181 1.1 jmcneill struct clk *sc_clk_ahb;
182 1.1 jmcneill struct clk *sc_clk_mmc;
183 1.1 jmcneill struct clk *sc_clk_output;
184 1.1 jmcneill struct clk *sc_clk_sample;
185 1.1 jmcneill
186 1.1 jmcneill struct fdtbus_reset *sc_rst_ahb;
187 1.1 jmcneill
188 1.1 jmcneill struct fdtbus_gpio_pin *sc_gpio_cd;
189 1.1 jmcneill int sc_gpio_cd_inverted;
190 1.1 jmcneill struct fdtbus_gpio_pin *sc_gpio_wp;
191 1.1 jmcneill int sc_gpio_wp_inverted;
192 1.3 jmcneill
193 1.29 jmcneill struct fdtbus_regulator *sc_reg_vmmc;
194 1.3 jmcneill struct fdtbus_regulator *sc_reg_vqmmc;
195 1.12 jmcneill
196 1.12 jmcneill struct fdtbus_mmc_pwrseq *sc_pwrseq;
197 1.17 jmcneill
198 1.17 jmcneill bool sc_non_removable;
199 1.17 jmcneill bool sc_broken_cd;
200 1.1 jmcneill };
201 1.1 jmcneill
202 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_mmc, sizeof(struct sunxi_mmc_softc),
203 1.1 jmcneill sunxi_mmc_match, sunxi_mmc_attach, NULL, NULL);
204 1.1 jmcneill
205 1.1 jmcneill #define MMC_WRITE(sc, reg, val) \
206 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
207 1.1 jmcneill #define MMC_READ(sc, reg) \
208 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
209 1.1 jmcneill
210 1.9 jmcneill static const struct sunxi_mmc_config sun4i_a10_mmc_config = {
211 1.9 jmcneill .idma_xferlen = 0x2000,
212 1.9 jmcneill .dma_ftrglevel = 0x20070008,
213 1.9 jmcneill .delays = NULL,
214 1.9 jmcneill .flags = 0,
215 1.9 jmcneill };
216 1.9 jmcneill
217 1.7 jmcneill static const struct sunxi_mmc_config sun5i_a13_mmc_config = {
218 1.7 jmcneill .idma_xferlen = 0x10000,
219 1.7 jmcneill .dma_ftrglevel = 0x20070008,
220 1.7 jmcneill .delays = NULL,
221 1.7 jmcneill .flags = 0,
222 1.7 jmcneill };
223 1.7 jmcneill
224 1.7 jmcneill static const struct sunxi_mmc_config sun7i_a20_mmc_config = {
225 1.8 jmcneill .idma_xferlen = 0x2000,
226 1.7 jmcneill .dma_ftrglevel = 0x20070008,
227 1.10 jmcneill .delays = sun7i_mmc_delays,
228 1.10 jmcneill .flags = 0,
229 1.10 jmcneill };
230 1.10 jmcneill
231 1.16 jmcneill static const struct sunxi_mmc_config sun8i_a83t_emmc_config = {
232 1.16 jmcneill .idma_xferlen = 0x10000,
233 1.16 jmcneill .dma_ftrglevel = 0x20070008,
234 1.16 jmcneill .delays = NULL,
235 1.16 jmcneill .flags = SUNXI_MMC_FLAG_NEW_TIMINGS,
236 1.16 jmcneill };
237 1.16 jmcneill
238 1.10 jmcneill static const struct sunxi_mmc_config sun9i_a80_mmc_config = {
239 1.10 jmcneill .idma_xferlen = 0x10000,
240 1.10 jmcneill .dma_ftrglevel = 0x200f0010,
241 1.10 jmcneill .delays = sun9i_mmc_delays,
242 1.7 jmcneill .flags = 0,
243 1.7 jmcneill };
244 1.7 jmcneill
245 1.7 jmcneill static const struct sunxi_mmc_config sun50i_a64_mmc_config = {
246 1.7 jmcneill .idma_xferlen = 0x10000,
247 1.7 jmcneill .dma_ftrglevel = 0x20070008,
248 1.7 jmcneill .delays = NULL,
249 1.7 jmcneill .flags = SUNXI_MMC_FLAG_CALIB_REG |
250 1.7 jmcneill SUNXI_MMC_FLAG_NEW_TIMINGS |
251 1.7 jmcneill SUNXI_MMC_FLAG_MASK_DATA0,
252 1.7 jmcneill };
253 1.7 jmcneill
254 1.18 jmcneill static const struct sunxi_mmc_config sun50i_a64_emmc_config = {
255 1.19 jakllsch .idma_xferlen = 0x2000,
256 1.18 jmcneill .dma_ftrglevel = 0x20070008,
257 1.18 jmcneill .delays = NULL,
258 1.28 jmcneill .flags = SUNXI_MMC_FLAG_CALIB_REG |
259 1.28 jmcneill SUNXI_MMC_FLAG_NEW_TIMINGS |
260 1.28 jmcneill SUNXI_MMC_FLAG_HS200,
261 1.18 jmcneill };
262 1.18 jmcneill
263 1.20 jmcneill static const struct sunxi_mmc_config sun50i_h6_mmc_config = {
264 1.20 jmcneill .idma_xferlen = 0x10000,
265 1.20 jmcneill .dma_ftrglevel = 0x20070008,
266 1.20 jmcneill .delays = NULL,
267 1.20 jmcneill .flags = SUNXI_MMC_FLAG_CALIB_REG |
268 1.20 jmcneill SUNXI_MMC_FLAG_NEW_TIMINGS |
269 1.20 jmcneill SUNXI_MMC_FLAG_MASK_DATA0,
270 1.20 jmcneill };
271 1.20 jmcneill
272 1.20 jmcneill static const struct sunxi_mmc_config sun50i_h6_emmc_config = {
273 1.20 jmcneill .idma_xferlen = 0x2000,
274 1.20 jmcneill .dma_ftrglevel = 0x20070008,
275 1.20 jmcneill .delays = NULL,
276 1.20 jmcneill .flags = SUNXI_MMC_FLAG_CALIB_REG,
277 1.20 jmcneill };
278 1.20 jmcneill
279 1.7 jmcneill static const struct of_compat_data compat_data[] = {
280 1.9 jmcneill { "allwinner,sun4i-a10-mmc", (uintptr_t)&sun4i_a10_mmc_config },
281 1.7 jmcneill { "allwinner,sun5i-a13-mmc", (uintptr_t)&sun5i_a13_mmc_config },
282 1.7 jmcneill { "allwinner,sun7i-a20-mmc", (uintptr_t)&sun7i_a20_mmc_config },
283 1.16 jmcneill { "allwinner,sun8i-a83t-emmc", (uintptr_t)&sun8i_a83t_emmc_config },
284 1.10 jmcneill { "allwinner,sun9i-a80-mmc", (uintptr_t)&sun9i_a80_mmc_config },
285 1.7 jmcneill { "allwinner,sun50i-a64-mmc", (uintptr_t)&sun50i_a64_mmc_config },
286 1.18 jmcneill { "allwinner,sun50i-a64-emmc", (uintptr_t)&sun50i_a64_emmc_config },
287 1.20 jmcneill { "allwinner,sun50i-h6-mmc", (uintptr_t)&sun50i_h6_mmc_config },
288 1.20 jmcneill { "allwinner,sun50i-h6-emmc", (uintptr_t)&sun50i_h6_emmc_config },
289 1.7 jmcneill { NULL }
290 1.1 jmcneill };
291 1.1 jmcneill
292 1.1 jmcneill static int
293 1.1 jmcneill sunxi_mmc_match(device_t parent, cfdata_t cf, void *aux)
294 1.1 jmcneill {
295 1.1 jmcneill struct fdt_attach_args * const faa = aux;
296 1.1 jmcneill
297 1.7 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
298 1.1 jmcneill }
299 1.1 jmcneill
300 1.1 jmcneill static void
301 1.1 jmcneill sunxi_mmc_attach(device_t parent, device_t self, void *aux)
302 1.1 jmcneill {
303 1.1 jmcneill struct sunxi_mmc_softc * const sc = device_private(self);
304 1.1 jmcneill struct fdt_attach_args * const faa = aux;
305 1.1 jmcneill const int phandle = faa->faa_phandle;
306 1.1 jmcneill char intrstr[128];
307 1.1 jmcneill bus_addr_t addr;
308 1.1 jmcneill bus_size_t size;
309 1.1 jmcneill
310 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
311 1.1 jmcneill aprint_error(": couldn't get registers\n");
312 1.1 jmcneill return;
313 1.1 jmcneill }
314 1.1 jmcneill
315 1.1 jmcneill sc->sc_clk_ahb = fdtbus_clock_get(phandle, "ahb");
316 1.1 jmcneill sc->sc_clk_mmc = fdtbus_clock_get(phandle, "mmc");
317 1.1 jmcneill sc->sc_clk_output = fdtbus_clock_get(phandle, "output");
318 1.1 jmcneill sc->sc_clk_sample = fdtbus_clock_get(phandle, "sample");
319 1.1 jmcneill
320 1.1 jmcneill #if notyet
321 1.1 jmcneill if (sc->sc_clk_ahb == NULL || sc->sc_clk_mmc == NULL ||
322 1.1 jmcneill sc->sc_clk_output == NULL || sc->sc_clk_sample == NULL) {
323 1.1 jmcneill #else
324 1.1 jmcneill if (sc->sc_clk_ahb == NULL || sc->sc_clk_mmc == NULL) {
325 1.1 jmcneill #endif
326 1.1 jmcneill aprint_error(": couldn't get clocks\n");
327 1.1 jmcneill return;
328 1.1 jmcneill }
329 1.1 jmcneill
330 1.1 jmcneill sc->sc_rst_ahb = fdtbus_reset_get(phandle, "ahb");
331 1.1 jmcneill
332 1.12 jmcneill sc->sc_pwrseq = fdtbus_mmc_pwrseq_get(phandle);
333 1.12 jmcneill
334 1.1 jmcneill if (clk_enable(sc->sc_clk_ahb) != 0 ||
335 1.1 jmcneill clk_enable(sc->sc_clk_mmc) != 0) {
336 1.1 jmcneill aprint_error(": couldn't enable clocks\n");
337 1.1 jmcneill return;
338 1.1 jmcneill }
339 1.1 jmcneill
340 1.5 jmcneill if (sc->sc_rst_ahb != NULL) {
341 1.5 jmcneill if (fdtbus_reset_deassert(sc->sc_rst_ahb) != 0) {
342 1.5 jmcneill aprint_error(": couldn't de-assert resets\n");
343 1.5 jmcneill return;
344 1.5 jmcneill }
345 1.1 jmcneill }
346 1.1 jmcneill
347 1.1 jmcneill sc->sc_dev = self;
348 1.1 jmcneill sc->sc_phandle = phandle;
349 1.7 jmcneill sc->sc_config = (void *)of_search_compatible(phandle, compat_data)->data;
350 1.1 jmcneill sc->sc_bst = faa->faa_bst;
351 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
352 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
353 1.1 jmcneill cv_init(&sc->sc_intr_cv, "awinmmcirq");
354 1.1 jmcneill cv_init(&sc->sc_idst_cv, "awinmmcdma");
355 1.1 jmcneill
356 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
357 1.1 jmcneill aprint_error(": couldn't map registers\n");
358 1.1 jmcneill return;
359 1.1 jmcneill }
360 1.1 jmcneill
361 1.33 jmcneill sc->sc_reg_vmmc = fdtbus_regulator_acquire(phandle, "vmmc-supply");
362 1.33 jmcneill if (sc->sc_reg_vmmc != NULL && fdtbus_regulator_enable(sc->sc_reg_vmmc)) {
363 1.33 jmcneill aprint_error(": couldn't enable vmmc-supply\n");
364 1.33 jmcneill return;
365 1.33 jmcneill }
366 1.33 jmcneill
367 1.1 jmcneill aprint_naive("\n");
368 1.1 jmcneill aprint_normal(": SD/MMC controller\n");
369 1.1 jmcneill
370 1.29 jmcneill sc->sc_reg_vqmmc = fdtbus_regulator_acquire(phandle, "vqmmc-supply");
371 1.29 jmcneill
372 1.1 jmcneill sc->sc_gpio_cd = fdtbus_gpio_acquire(phandle, "cd-gpios",
373 1.1 jmcneill GPIO_PIN_INPUT);
374 1.1 jmcneill sc->sc_gpio_wp = fdtbus_gpio_acquire(phandle, "wp-gpios",
375 1.1 jmcneill GPIO_PIN_INPUT);
376 1.1 jmcneill
377 1.1 jmcneill sc->sc_gpio_cd_inverted = of_hasprop(phandle, "cd-inverted") ? 0 : 1;
378 1.1 jmcneill sc->sc_gpio_wp_inverted = of_hasprop(phandle, "wp-inverted") ? 0 : 1;
379 1.1 jmcneill
380 1.17 jmcneill sc->sc_non_removable = of_hasprop(phandle, "non-removable");
381 1.17 jmcneill sc->sc_broken_cd = of_hasprop(phandle, "broken-cd");
382 1.17 jmcneill
383 1.23 jmcneill if (of_getprop_uint32(phandle, "max-frequency", &sc->sc_max_frequency))
384 1.23 jmcneill sc->sc_max_frequency = 52000000;
385 1.23 jmcneill
386 1.14 jmcneill if (sunxi_mmc_dmabounce_setup(sc) != 0 ||
387 1.14 jmcneill sunxi_mmc_idma_setup(sc) != 0) {
388 1.1 jmcneill aprint_error_dev(self, "failed to setup DMA\n");
389 1.1 jmcneill return;
390 1.1 jmcneill }
391 1.1 jmcneill
392 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
393 1.1 jmcneill aprint_error_dev(self, "failed to decode interrupt\n");
394 1.1 jmcneill return;
395 1.1 jmcneill }
396 1.1 jmcneill
397 1.1 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, FDT_INTR_MPSAFE,
398 1.1 jmcneill sunxi_mmc_intr, sc);
399 1.1 jmcneill if (sc->sc_ih == NULL) {
400 1.1 jmcneill aprint_error_dev(self, "failed to establish interrupt on %s\n",
401 1.1 jmcneill intrstr);
402 1.1 jmcneill return;
403 1.1 jmcneill }
404 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
405 1.1 jmcneill
406 1.1 jmcneill config_interrupts(self, sunxi_mmc_attach_i);
407 1.1 jmcneill }
408 1.1 jmcneill
409 1.1 jmcneill static int
410 1.14 jmcneill sunxi_mmc_dmabounce_setup(struct sunxi_mmc_softc *sc)
411 1.14 jmcneill {
412 1.14 jmcneill bus_dma_segment_t ds[1];
413 1.14 jmcneill int error, rseg;
414 1.14 jmcneill
415 1.14 jmcneill sc->sc_dmabounce_buflen = sunxi_mmc_host_maxblklen(sc);
416 1.14 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_dmabounce_buflen, 0,
417 1.14 jmcneill sc->sc_dmabounce_buflen, ds, 1, &rseg, BUS_DMA_WAITOK);
418 1.14 jmcneill if (error)
419 1.14 jmcneill return error;
420 1.14 jmcneill error = bus_dmamem_map(sc->sc_dmat, ds, 1, sc->sc_dmabounce_buflen,
421 1.14 jmcneill &sc->sc_dmabounce_buf, BUS_DMA_WAITOK);
422 1.14 jmcneill if (error)
423 1.14 jmcneill goto free;
424 1.14 jmcneill error = bus_dmamap_create(sc->sc_dmat, sc->sc_dmabounce_buflen, 1,
425 1.14 jmcneill sc->sc_dmabounce_buflen, 0, BUS_DMA_WAITOK, &sc->sc_dmabounce_map);
426 1.14 jmcneill if (error)
427 1.14 jmcneill goto unmap;
428 1.14 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmabounce_map,
429 1.14 jmcneill sc->sc_dmabounce_buf, sc->sc_dmabounce_buflen, NULL,
430 1.14 jmcneill BUS_DMA_WAITOK);
431 1.14 jmcneill if (error)
432 1.14 jmcneill goto destroy;
433 1.14 jmcneill return 0;
434 1.14 jmcneill
435 1.14 jmcneill destroy:
436 1.14 jmcneill bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmabounce_map);
437 1.14 jmcneill unmap:
438 1.14 jmcneill bus_dmamem_unmap(sc->sc_dmat, sc->sc_dmabounce_buf,
439 1.14 jmcneill sc->sc_dmabounce_buflen);
440 1.14 jmcneill free:
441 1.14 jmcneill bus_dmamem_free(sc->sc_dmat, ds, rseg);
442 1.14 jmcneill return error;
443 1.14 jmcneill }
444 1.14 jmcneill
445 1.14 jmcneill static int
446 1.1 jmcneill sunxi_mmc_idma_setup(struct sunxi_mmc_softc *sc)
447 1.1 jmcneill {
448 1.1 jmcneill int error;
449 1.1 jmcneill
450 1.1 jmcneill sc->sc_idma_ndesc = SUNXI_MMC_NDESC;
451 1.1 jmcneill sc->sc_idma_size = sizeof(struct sunxi_mmc_idma_descriptor) *
452 1.1 jmcneill sc->sc_idma_ndesc;
453 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_idma_size, 0,
454 1.1 jmcneill sc->sc_idma_size, sc->sc_idma_segs, 1,
455 1.1 jmcneill &sc->sc_idma_nsegs, BUS_DMA_WAITOK);
456 1.1 jmcneill if (error)
457 1.1 jmcneill return error;
458 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, sc->sc_idma_segs,
459 1.1 jmcneill sc->sc_idma_nsegs, sc->sc_idma_size,
460 1.1 jmcneill &sc->sc_idma_desc, BUS_DMA_WAITOK);
461 1.1 jmcneill if (error)
462 1.1 jmcneill goto free;
463 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, sc->sc_idma_size, 1,
464 1.1 jmcneill sc->sc_idma_size, 0, BUS_DMA_WAITOK, &sc->sc_idma_map);
465 1.1 jmcneill if (error)
466 1.1 jmcneill goto unmap;
467 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->sc_idma_map,
468 1.1 jmcneill sc->sc_idma_desc, sc->sc_idma_size, NULL, BUS_DMA_WAITOK);
469 1.1 jmcneill if (error)
470 1.1 jmcneill goto destroy;
471 1.1 jmcneill return 0;
472 1.1 jmcneill
473 1.1 jmcneill destroy:
474 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, sc->sc_idma_map);
475 1.1 jmcneill unmap:
476 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, sc->sc_idma_desc, sc->sc_idma_size);
477 1.1 jmcneill free:
478 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, sc->sc_idma_segs, sc->sc_idma_nsegs);
479 1.1 jmcneill return error;
480 1.1 jmcneill }
481 1.1 jmcneill
482 1.1 jmcneill static int
483 1.3 jmcneill sunxi_mmc_set_clock(struct sunxi_mmc_softc *sc, u_int freq, bool ddr)
484 1.1 jmcneill {
485 1.3 jmcneill const struct sunxi_mmc_delay *delays;
486 1.24 jmcneill int error, timing = SUNXI_MMC_TIMING_400K;
487 1.3 jmcneill
488 1.23 jmcneill if (sc->sc_config->delays) {
489 1.23 jmcneill if (freq <= 400) {
490 1.23 jmcneill timing = SUNXI_MMC_TIMING_400K;
491 1.23 jmcneill } else if (freq <= 25000) {
492 1.23 jmcneill timing = SUNXI_MMC_TIMING_25M;
493 1.23 jmcneill } else if (freq <= 52000) {
494 1.23 jmcneill if (ddr) {
495 1.23 jmcneill timing = sc->sc_mmc_width == 8 ?
496 1.23 jmcneill SUNXI_MMC_TIMING_50M_DDR_8BIT :
497 1.23 jmcneill SUNXI_MMC_TIMING_50M_DDR;
498 1.23 jmcneill } else {
499 1.23 jmcneill timing = SUNXI_MMC_TIMING_50M;
500 1.23 jmcneill }
501 1.23 jmcneill } else
502 1.23 jmcneill return EINVAL;
503 1.23 jmcneill }
504 1.23 jmcneill if (sc->sc_max_frequency) {
505 1.23 jmcneill if (freq * 1000 > sc->sc_max_frequency)
506 1.23 jmcneill return EINVAL;
507 1.23 jmcneill }
508 1.3 jmcneill
509 1.3 jmcneill error = clk_set_rate(sc->sc_clk_mmc, (freq * 1000) << ddr);
510 1.3 jmcneill if (error != 0)
511 1.3 jmcneill return error;
512 1.3 jmcneill
513 1.7 jmcneill if (sc->sc_config->delays == NULL)
514 1.7 jmcneill return 0;
515 1.7 jmcneill
516 1.7 jmcneill delays = &sc->sc_config->delays[timing];
517 1.7 jmcneill
518 1.3 jmcneill if (sc->sc_clk_sample) {
519 1.3 jmcneill error = clk_set_rate(sc->sc_clk_sample, delays->sample_phase);
520 1.3 jmcneill if (error != 0)
521 1.3 jmcneill return error;
522 1.3 jmcneill }
523 1.3 jmcneill if (sc->sc_clk_output) {
524 1.3 jmcneill error = clk_set_rate(sc->sc_clk_output, delays->output_phase);
525 1.3 jmcneill if (error != 0)
526 1.3 jmcneill return error;
527 1.3 jmcneill }
528 1.3 jmcneill
529 1.3 jmcneill return 0;
530 1.1 jmcneill }
531 1.1 jmcneill
532 1.1 jmcneill static void
533 1.32 jmcneill sunxi_mmc_hw_reset(struct sunxi_mmc_softc *sc)
534 1.32 jmcneill {
535 1.32 jmcneill MMC_WRITE(sc, SUNXI_MMC_HWRST, 0);
536 1.32 jmcneill delay(1000);
537 1.32 jmcneill MMC_WRITE(sc, SUNXI_MMC_HWRST, 1);
538 1.32 jmcneill delay(1000);
539 1.32 jmcneill }
540 1.32 jmcneill
541 1.32 jmcneill static void
542 1.1 jmcneill sunxi_mmc_attach_i(device_t self)
543 1.1 jmcneill {
544 1.1 jmcneill struct sunxi_mmc_softc *sc = device_private(self);
545 1.23 jmcneill const u_int flags = sc->sc_config->flags;
546 1.1 jmcneill struct sdmmcbus_attach_args saa;
547 1.1 jmcneill uint32_t width;
548 1.1 jmcneill
549 1.12 jmcneill if (sc->sc_pwrseq)
550 1.12 jmcneill fdtbus_mmc_pwrseq_pre_power_on(sc->sc_pwrseq);
551 1.12 jmcneill
552 1.32 jmcneill if (of_hasprop(sc->sc_phandle, "cap-mmc-hw-reset"))
553 1.32 jmcneill sunxi_mmc_hw_reset(sc);
554 1.32 jmcneill
555 1.1 jmcneill sunxi_mmc_host_reset(sc);
556 1.1 jmcneill sunxi_mmc_bus_width(sc, 1);
557 1.3 jmcneill sunxi_mmc_set_clock(sc, 400, false);
558 1.1 jmcneill
559 1.12 jmcneill if (sc->sc_pwrseq)
560 1.12 jmcneill fdtbus_mmc_pwrseq_post_power_on(sc->sc_pwrseq);
561 1.12 jmcneill
562 1.1 jmcneill if (of_getprop_uint32(sc->sc_phandle, "bus-width", &width) != 0)
563 1.1 jmcneill width = 4;
564 1.1 jmcneill
565 1.1 jmcneill memset(&saa, 0, sizeof(saa));
566 1.1 jmcneill saa.saa_busname = "sdmmc";
567 1.1 jmcneill saa.saa_sct = &sunxi_mmc_chip_functions;
568 1.1 jmcneill saa.saa_sch = sc;
569 1.1 jmcneill saa.saa_dmat = sc->sc_dmat;
570 1.1 jmcneill saa.saa_clkmin = 400;
571 1.23 jmcneill saa.saa_clkmax = sc->sc_max_frequency / 1000;
572 1.1 jmcneill saa.saa_caps = SMC_CAPS_DMA |
573 1.1 jmcneill SMC_CAPS_MULTI_SEG_DMA |
574 1.1 jmcneill SMC_CAPS_AUTO_STOP |
575 1.1 jmcneill SMC_CAPS_SD_HIGHSPEED |
576 1.2 jmcneill SMC_CAPS_MMC_HIGHSPEED |
577 1.2 jmcneill SMC_CAPS_POLLING;
578 1.25 jmcneill
579 1.25 jmcneill if (sc->sc_config->delays || (flags & SUNXI_MMC_FLAG_NEW_TIMINGS))
580 1.25 jmcneill saa.saa_caps |= SMC_CAPS_MMC_DDR52;
581 1.25 jmcneill
582 1.23 jmcneill if (flags & SUNXI_MMC_FLAG_HS200)
583 1.23 jmcneill saa.saa_caps |= SMC_CAPS_MMC_HS200;
584 1.25 jmcneill
585 1.1 jmcneill if (width == 4)
586 1.1 jmcneill saa.saa_caps |= SMC_CAPS_4BIT_MODE;
587 1.1 jmcneill if (width == 8)
588 1.1 jmcneill saa.saa_caps |= SMC_CAPS_8BIT_MODE;
589 1.1 jmcneill
590 1.1 jmcneill if (sc->sc_gpio_cd)
591 1.1 jmcneill saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
592 1.1 jmcneill
593 1.1 jmcneill sc->sc_sdmmc_dev = config_found(self, &saa, NULL);
594 1.1 jmcneill }
595 1.1 jmcneill
596 1.1 jmcneill static int
597 1.1 jmcneill sunxi_mmc_intr(void *priv)
598 1.1 jmcneill {
599 1.1 jmcneill struct sunxi_mmc_softc *sc = priv;
600 1.22 jmcneill uint32_t idst, rint, imask;
601 1.1 jmcneill
602 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
603 1.1 jmcneill idst = MMC_READ(sc, SUNXI_MMC_IDST);
604 1.1 jmcneill rint = MMC_READ(sc, SUNXI_MMC_RINT);
605 1.11 jmcneill if (!idst && !rint) {
606 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
607 1.1 jmcneill return 0;
608 1.1 jmcneill }
609 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_IDST, idst);
610 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_RINT, rint & ~SUNXI_MMC_INT_SDIO_INT);
611 1.1 jmcneill
612 1.13 jmcneill DPRINTF(sc->sc_dev, "mmc intr idst=%08X rint=%08X\n",
613 1.11 jmcneill idst, rint);
614 1.1 jmcneill
615 1.11 jmcneill if (idst != 0) {
616 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_IDIE, 0);
617 1.1 jmcneill sc->sc_idma_idst |= idst;
618 1.1 jmcneill cv_broadcast(&sc->sc_idst_cv);
619 1.1 jmcneill }
620 1.1 jmcneill
621 1.11 jmcneill if ((rint & ~SUNXI_MMC_INT_SDIO_INT) != 0) {
622 1.22 jmcneill imask = MMC_READ(sc, SUNXI_MMC_IMASK);
623 1.34 jmcneill MMC_WRITE(sc, SUNXI_MMC_IMASK, imask & SUNXI_MMC_INT_SDIO_INT);
624 1.11 jmcneill sc->sc_intr_rint |= (rint & ~SUNXI_MMC_INT_SDIO_INT);
625 1.1 jmcneill cv_broadcast(&sc->sc_intr_cv);
626 1.1 jmcneill }
627 1.1 jmcneill
628 1.11 jmcneill if ((rint & SUNXI_MMC_INT_SDIO_INT) != 0) {
629 1.11 jmcneill sdmmc_card_intr(sc->sc_sdmmc_dev);
630 1.11 jmcneill }
631 1.11 jmcneill
632 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
633 1.1 jmcneill
634 1.1 jmcneill return 1;
635 1.1 jmcneill }
636 1.1 jmcneill
637 1.1 jmcneill static int
638 1.2 jmcneill sunxi_mmc_wait_rint(struct sunxi_mmc_softc *sc, uint32_t mask,
639 1.2 jmcneill int timeout, bool poll)
640 1.1 jmcneill {
641 1.1 jmcneill int retry;
642 1.1 jmcneill int error;
643 1.1 jmcneill
644 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_intr_lock));
645 1.1 jmcneill
646 1.1 jmcneill if (sc->sc_intr_rint & mask)
647 1.1 jmcneill return 0;
648 1.1 jmcneill
649 1.2 jmcneill if (poll)
650 1.2 jmcneill retry = timeout / hz * 1000;
651 1.2 jmcneill else
652 1.2 jmcneill retry = timeout / hz;
653 1.1 jmcneill
654 1.1 jmcneill while (retry > 0) {
655 1.2 jmcneill if (poll) {
656 1.2 jmcneill sc->sc_intr_rint |= MMC_READ(sc, SUNXI_MMC_RINT);
657 1.2 jmcneill } else {
658 1.2 jmcneill error = cv_timedwait(&sc->sc_intr_cv,
659 1.2 jmcneill &sc->sc_intr_lock, hz);
660 1.2 jmcneill if (error && error != EWOULDBLOCK)
661 1.2 jmcneill return error;
662 1.2 jmcneill }
663 1.1 jmcneill if (sc->sc_intr_rint & mask)
664 1.1 jmcneill return 0;
665 1.2 jmcneill if (poll)
666 1.2 jmcneill delay(1000);
667 1.1 jmcneill --retry;
668 1.1 jmcneill }
669 1.1 jmcneill
670 1.1 jmcneill return ETIMEDOUT;
671 1.1 jmcneill }
672 1.1 jmcneill
673 1.1 jmcneill static int
674 1.1 jmcneill sunxi_mmc_host_reset(sdmmc_chipset_handle_t sch)
675 1.1 jmcneill {
676 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
677 1.22 jmcneill uint32_t gctrl;
678 1.1 jmcneill int retry = 1000;
679 1.1 jmcneill
680 1.13 jmcneill DPRINTF(sc->sc_dev, "host reset\n");
681 1.1 jmcneill
682 1.22 jmcneill gctrl = MMC_READ(sc, SUNXI_MMC_GCTRL);
683 1.22 jmcneill gctrl |= SUNXI_MMC_GCTRL_RESET;
684 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL, gctrl);
685 1.1 jmcneill while (--retry > 0) {
686 1.1 jmcneill if (!(MMC_READ(sc, SUNXI_MMC_GCTRL) & SUNXI_MMC_GCTRL_RESET))
687 1.1 jmcneill break;
688 1.1 jmcneill delay(100);
689 1.1 jmcneill }
690 1.1 jmcneill
691 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_TIMEOUT, 0xffffffff);
692 1.1 jmcneill
693 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_IMASK, 0);
694 1.22 jmcneill
695 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_RINT, 0xffffffff);
696 1.1 jmcneill
697 1.22 jmcneill gctrl = MMC_READ(sc, SUNXI_MMC_GCTRL);
698 1.22 jmcneill gctrl |= SUNXI_MMC_GCTRL_INTEN;
699 1.22 jmcneill gctrl &= ~SUNXI_MMC_GCTRL_WAIT_MEM_ACCESS_DONE;
700 1.22 jmcneill gctrl &= ~SUNXI_MMC_GCTRL_ACCESS_BY_AHB;
701 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL, gctrl);
702 1.1 jmcneill
703 1.1 jmcneill return 0;
704 1.1 jmcneill }
705 1.1 jmcneill
706 1.1 jmcneill static uint32_t
707 1.1 jmcneill sunxi_mmc_host_ocr(sdmmc_chipset_handle_t sch)
708 1.1 jmcneill {
709 1.1 jmcneill return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V | MMC_OCR_HCS;
710 1.1 jmcneill }
711 1.1 jmcneill
712 1.1 jmcneill static int
713 1.1 jmcneill sunxi_mmc_host_maxblklen(sdmmc_chipset_handle_t sch)
714 1.1 jmcneill {
715 1.1 jmcneill return 8192;
716 1.1 jmcneill }
717 1.1 jmcneill
718 1.1 jmcneill static int
719 1.1 jmcneill sunxi_mmc_card_detect(sdmmc_chipset_handle_t sch)
720 1.1 jmcneill {
721 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
722 1.1 jmcneill
723 1.17 jmcneill if (sc->sc_non_removable || sc->sc_broken_cd) {
724 1.17 jmcneill /*
725 1.17 jmcneill * Non-removable or broken card detect flag set in
726 1.17 jmcneill * DT, assume always present
727 1.17 jmcneill */
728 1.17 jmcneill return 1;
729 1.17 jmcneill } else if (sc->sc_gpio_cd != NULL) {
730 1.17 jmcneill /* Use card detect GPIO */
731 1.1 jmcneill int v = 0, i;
732 1.1 jmcneill for (i = 0; i < 5; i++) {
733 1.1 jmcneill v += (fdtbus_gpio_read(sc->sc_gpio_cd) ^
734 1.1 jmcneill sc->sc_gpio_cd_inverted);
735 1.1 jmcneill delay(1000);
736 1.1 jmcneill }
737 1.1 jmcneill if (v == 5)
738 1.1 jmcneill sc->sc_mmc_present = 0;
739 1.1 jmcneill else if (v == 0)
740 1.1 jmcneill sc->sc_mmc_present = 1;
741 1.1 jmcneill return sc->sc_mmc_present;
742 1.17 jmcneill } else {
743 1.17 jmcneill /* Use CARD_PRESENT field of SD_STATUS register */
744 1.17 jmcneill const uint32_t present = MMC_READ(sc, SUNXI_MMC_STATUS) &
745 1.17 jmcneill SUNXI_MMC_STATUS_CARD_PRESENT;
746 1.17 jmcneill return present != 0;
747 1.1 jmcneill }
748 1.1 jmcneill }
749 1.1 jmcneill
750 1.1 jmcneill static int
751 1.1 jmcneill sunxi_mmc_write_protect(sdmmc_chipset_handle_t sch)
752 1.1 jmcneill {
753 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
754 1.1 jmcneill
755 1.1 jmcneill if (sc->sc_gpio_wp == NULL) {
756 1.1 jmcneill return 0; /* no write protect pin, assume rw */
757 1.1 jmcneill } else {
758 1.1 jmcneill return fdtbus_gpio_read(sc->sc_gpio_wp) ^
759 1.1 jmcneill sc->sc_gpio_wp_inverted;
760 1.1 jmcneill }
761 1.1 jmcneill }
762 1.1 jmcneill
763 1.1 jmcneill static int
764 1.1 jmcneill sunxi_mmc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
765 1.1 jmcneill {
766 1.1 jmcneill return 0;
767 1.1 jmcneill }
768 1.1 jmcneill
769 1.1 jmcneill static int
770 1.1 jmcneill sunxi_mmc_update_clock(struct sunxi_mmc_softc *sc)
771 1.1 jmcneill {
772 1.1 jmcneill uint32_t cmd;
773 1.1 jmcneill int retry;
774 1.1 jmcneill
775 1.13 jmcneill DPRINTF(sc->sc_dev, "update clock\n");
776 1.1 jmcneill
777 1.1 jmcneill cmd = SUNXI_MMC_CMD_START |
778 1.1 jmcneill SUNXI_MMC_CMD_UPCLK_ONLY |
779 1.1 jmcneill SUNXI_MMC_CMD_WAIT_PRE_OVER;
780 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CMD, cmd);
781 1.31 jmcneill retry = 100000;
782 1.1 jmcneill while (--retry > 0) {
783 1.1 jmcneill if (!(MMC_READ(sc, SUNXI_MMC_CMD) & SUNXI_MMC_CMD_START))
784 1.1 jmcneill break;
785 1.1 jmcneill delay(10);
786 1.1 jmcneill }
787 1.1 jmcneill
788 1.1 jmcneill if (retry == 0) {
789 1.1 jmcneill aprint_error_dev(sc->sc_dev, "timeout updating clock\n");
790 1.13 jmcneill DPRINTF(sc->sc_dev, "GCTRL: 0x%08x\n",
791 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_GCTRL));
792 1.13 jmcneill DPRINTF(sc->sc_dev, "CLKCR: 0x%08x\n",
793 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_CLKCR));
794 1.13 jmcneill DPRINTF(sc->sc_dev, "TIMEOUT: 0x%08x\n",
795 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_TIMEOUT));
796 1.13 jmcneill DPRINTF(sc->sc_dev, "WIDTH: 0x%08x\n",
797 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_WIDTH));
798 1.13 jmcneill DPRINTF(sc->sc_dev, "CMD: 0x%08x\n",
799 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_CMD));
800 1.13 jmcneill DPRINTF(sc->sc_dev, "MINT: 0x%08x\n",
801 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_MINT));
802 1.13 jmcneill DPRINTF(sc->sc_dev, "RINT: 0x%08x\n",
803 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_RINT));
804 1.13 jmcneill DPRINTF(sc->sc_dev, "STATUS: 0x%08x\n",
805 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_STATUS));
806 1.1 jmcneill return ETIMEDOUT;
807 1.1 jmcneill }
808 1.1 jmcneill
809 1.1 jmcneill return 0;
810 1.1 jmcneill }
811 1.1 jmcneill
812 1.1 jmcneill static int
813 1.3 jmcneill sunxi_mmc_bus_clock(sdmmc_chipset_handle_t sch, int freq, bool ddr)
814 1.1 jmcneill {
815 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
816 1.7 jmcneill uint32_t clkcr, gctrl, ntsr;
817 1.7 jmcneill const u_int flags = sc->sc_config->flags;
818 1.1 jmcneill
819 1.1 jmcneill clkcr = MMC_READ(sc, SUNXI_MMC_CLKCR);
820 1.1 jmcneill if (clkcr & SUNXI_MMC_CLKCR_CARDCLKON) {
821 1.1 jmcneill clkcr &= ~SUNXI_MMC_CLKCR_CARDCLKON;
822 1.7 jmcneill if (flags & SUNXI_MMC_CLKCR_MASK_DATA0)
823 1.7 jmcneill clkcr |= SUNXI_MMC_CLKCR_MASK_DATA0;
824 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
825 1.1 jmcneill if (sunxi_mmc_update_clock(sc) != 0)
826 1.1 jmcneill return 1;
827 1.7 jmcneill if (flags & SUNXI_MMC_CLKCR_MASK_DATA0) {
828 1.7 jmcneill clkcr = MMC_READ(sc, SUNXI_MMC_CLKCR);
829 1.7 jmcneill clkcr &= ~SUNXI_MMC_CLKCR_MASK_DATA0;
830 1.7 jmcneill MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
831 1.7 jmcneill }
832 1.1 jmcneill }
833 1.1 jmcneill
834 1.1 jmcneill if (freq) {
835 1.1 jmcneill
836 1.1 jmcneill clkcr &= ~SUNXI_MMC_CLKCR_DIV;
837 1.3 jmcneill clkcr |= __SHIFTIN(ddr, SUNXI_MMC_CLKCR_DIV);
838 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
839 1.7 jmcneill
840 1.7 jmcneill if (flags & SUNXI_MMC_FLAG_NEW_TIMINGS) {
841 1.7 jmcneill ntsr = MMC_READ(sc, SUNXI_MMC_NTSR);
842 1.7 jmcneill ntsr |= SUNXI_MMC_NTSR_MODE_SELECT;
843 1.7 jmcneill MMC_WRITE(sc, SUNXI_MMC_NTSR, ntsr);
844 1.7 jmcneill }
845 1.7 jmcneill
846 1.7 jmcneill if (flags & SUNXI_MMC_FLAG_CALIB_REG)
847 1.7 jmcneill MMC_WRITE(sc, SUNXI_MMC_SAMP_DL, SUNXI_MMC_SAMP_DL_SW_EN);
848 1.7 jmcneill
849 1.1 jmcneill if (sunxi_mmc_update_clock(sc) != 0)
850 1.1 jmcneill return 1;
851 1.1 jmcneill
852 1.3 jmcneill gctrl = MMC_READ(sc, SUNXI_MMC_GCTRL);
853 1.3 jmcneill if (ddr)
854 1.3 jmcneill gctrl |= SUNXI_MMC_GCTRL_DDR_MODE;
855 1.3 jmcneill else
856 1.3 jmcneill gctrl &= ~SUNXI_MMC_GCTRL_DDR_MODE;
857 1.3 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL, gctrl);
858 1.3 jmcneill
859 1.3 jmcneill if (sunxi_mmc_set_clock(sc, freq, ddr) != 0)
860 1.1 jmcneill return 1;
861 1.1 jmcneill
862 1.1 jmcneill clkcr |= SUNXI_MMC_CLKCR_CARDCLKON;
863 1.7 jmcneill if (flags & SUNXI_MMC_CLKCR_MASK_DATA0)
864 1.7 jmcneill clkcr |= SUNXI_MMC_CLKCR_MASK_DATA0;
865 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
866 1.1 jmcneill if (sunxi_mmc_update_clock(sc) != 0)
867 1.1 jmcneill return 1;
868 1.7 jmcneill if (flags & SUNXI_MMC_CLKCR_MASK_DATA0) {
869 1.7 jmcneill clkcr = MMC_READ(sc, SUNXI_MMC_CLKCR);
870 1.7 jmcneill clkcr &= ~SUNXI_MMC_CLKCR_MASK_DATA0;
871 1.7 jmcneill MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
872 1.7 jmcneill }
873 1.1 jmcneill }
874 1.1 jmcneill
875 1.1 jmcneill return 0;
876 1.1 jmcneill }
877 1.1 jmcneill
878 1.1 jmcneill static int
879 1.1 jmcneill sunxi_mmc_bus_width(sdmmc_chipset_handle_t sch, int width)
880 1.1 jmcneill {
881 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
882 1.1 jmcneill
883 1.13 jmcneill DPRINTF(sc->sc_dev, "width = %d\n", width);
884 1.1 jmcneill
885 1.1 jmcneill switch (width) {
886 1.1 jmcneill case 1:
887 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_WIDTH, SUNXI_MMC_WIDTH_1);
888 1.1 jmcneill break;
889 1.1 jmcneill case 4:
890 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_WIDTH, SUNXI_MMC_WIDTH_4);
891 1.1 jmcneill break;
892 1.1 jmcneill case 8:
893 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_WIDTH, SUNXI_MMC_WIDTH_8);
894 1.1 jmcneill break;
895 1.1 jmcneill default:
896 1.1 jmcneill return 1;
897 1.1 jmcneill }
898 1.1 jmcneill
899 1.1 jmcneill sc->sc_mmc_width = width;
900 1.1 jmcneill
901 1.1 jmcneill return 0;
902 1.1 jmcneill }
903 1.1 jmcneill
904 1.1 jmcneill static int
905 1.1 jmcneill sunxi_mmc_bus_rod(sdmmc_chipset_handle_t sch, int on)
906 1.1 jmcneill {
907 1.1 jmcneill return -1;
908 1.1 jmcneill }
909 1.1 jmcneill
910 1.1 jmcneill static int
911 1.3 jmcneill sunxi_mmc_signal_voltage(sdmmc_chipset_handle_t sch, int signal_voltage)
912 1.3 jmcneill {
913 1.3 jmcneill struct sunxi_mmc_softc *sc = sch;
914 1.3 jmcneill u_int uvol;
915 1.3 jmcneill int error;
916 1.3 jmcneill
917 1.3 jmcneill if (sc->sc_reg_vqmmc == NULL)
918 1.3 jmcneill return 0;
919 1.3 jmcneill
920 1.3 jmcneill switch (signal_voltage) {
921 1.3 jmcneill case SDMMC_SIGNAL_VOLTAGE_330:
922 1.3 jmcneill uvol = 3300000;
923 1.3 jmcneill break;
924 1.3 jmcneill case SDMMC_SIGNAL_VOLTAGE_180:
925 1.3 jmcneill uvol = 1800000;
926 1.3 jmcneill break;
927 1.3 jmcneill default:
928 1.3 jmcneill return EINVAL;
929 1.3 jmcneill }
930 1.3 jmcneill
931 1.30 jmcneill error = fdtbus_regulator_supports_voltage(sc->sc_reg_vqmmc, uvol, uvol);
932 1.30 jmcneill if (error != 0)
933 1.30 jmcneill return 0;
934 1.30 jmcneill
935 1.3 jmcneill error = fdtbus_regulator_set_voltage(sc->sc_reg_vqmmc, uvol, uvol);
936 1.3 jmcneill if (error != 0)
937 1.3 jmcneill return error;
938 1.3 jmcneill
939 1.3 jmcneill return fdtbus_regulator_enable(sc->sc_reg_vqmmc);
940 1.3 jmcneill }
941 1.3 jmcneill
942 1.3 jmcneill static int
943 1.23 jmcneill sunxi_mmc_execute_tuning(sdmmc_chipset_handle_t sch, int timing)
944 1.23 jmcneill {
945 1.23 jmcneill switch (timing) {
946 1.23 jmcneill case SDMMC_TIMING_MMC_HS200:
947 1.23 jmcneill break;
948 1.23 jmcneill default:
949 1.23 jmcneill return EINVAL;
950 1.23 jmcneill }
951 1.23 jmcneill
952 1.23 jmcneill return 0;
953 1.23 jmcneill }
954 1.23 jmcneill
955 1.23 jmcneill static int
956 1.1 jmcneill sunxi_mmc_dma_prepare(struct sunxi_mmc_softc *sc, struct sdmmc_command *cmd)
957 1.1 jmcneill {
958 1.1 jmcneill struct sunxi_mmc_idma_descriptor *dma = sc->sc_idma_desc;
959 1.1 jmcneill bus_addr_t desc_paddr = sc->sc_idma_map->dm_segs[0].ds_addr;
960 1.14 jmcneill bus_dmamap_t map;
961 1.1 jmcneill bus_size_t off;
962 1.1 jmcneill int desc, resid, seg;
963 1.1 jmcneill uint32_t val;
964 1.1 jmcneill
965 1.14 jmcneill /*
966 1.14 jmcneill * If the command includes a dma map use it, otherwise we need to
967 1.14 jmcneill * bounce. This can happen for SDIO IO_RW_EXTENDED (CMD53) commands.
968 1.14 jmcneill */
969 1.14 jmcneill if (cmd->c_dmamap) {
970 1.14 jmcneill map = cmd->c_dmamap;
971 1.14 jmcneill } else {
972 1.14 jmcneill if (cmd->c_datalen > sc->sc_dmabounce_buflen)
973 1.14 jmcneill return E2BIG;
974 1.14 jmcneill map = sc->sc_dmabounce_map;
975 1.14 jmcneill
976 1.15 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
977 1.15 jmcneill memset(sc->sc_dmabounce_buf, 0, cmd->c_datalen);
978 1.15 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
979 1.15 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_PREREAD);
980 1.15 jmcneill } else {
981 1.14 jmcneill memcpy(sc->sc_dmabounce_buf, cmd->c_data,
982 1.14 jmcneill cmd->c_datalen);
983 1.14 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
984 1.14 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_PREWRITE);
985 1.14 jmcneill }
986 1.14 jmcneill }
987 1.14 jmcneill
988 1.1 jmcneill desc = 0;
989 1.14 jmcneill for (seg = 0; seg < map->dm_nsegs; seg++) {
990 1.14 jmcneill bus_addr_t paddr = map->dm_segs[seg].ds_addr;
991 1.14 jmcneill bus_size_t len = map->dm_segs[seg].ds_len;
992 1.27 riastrad resid = uimin(len, cmd->c_resid);
993 1.1 jmcneill off = 0;
994 1.1 jmcneill while (resid > 0) {
995 1.1 jmcneill if (desc == sc->sc_idma_ndesc)
996 1.1 jmcneill break;
997 1.27 riastrad len = uimin(sc->sc_config->idma_xferlen, resid);
998 1.26 jmcneill dma[desc].dma_buf_size = htole32(len);
999 1.1 jmcneill dma[desc].dma_buf_addr = htole32(paddr + off);
1000 1.1 jmcneill dma[desc].dma_config = htole32(SUNXI_MMC_IDMA_CONFIG_CH |
1001 1.1 jmcneill SUNXI_MMC_IDMA_CONFIG_OWN);
1002 1.1 jmcneill cmd->c_resid -= len;
1003 1.1 jmcneill resid -= len;
1004 1.1 jmcneill off += len;
1005 1.1 jmcneill if (desc == 0) {
1006 1.1 jmcneill dma[desc].dma_config |= htole32(SUNXI_MMC_IDMA_CONFIG_FD);
1007 1.1 jmcneill }
1008 1.1 jmcneill if (cmd->c_resid == 0) {
1009 1.1 jmcneill dma[desc].dma_config |= htole32(SUNXI_MMC_IDMA_CONFIG_LD);
1010 1.1 jmcneill dma[desc].dma_config |= htole32(SUNXI_MMC_IDMA_CONFIG_ER);
1011 1.1 jmcneill dma[desc].dma_next = 0;
1012 1.1 jmcneill } else {
1013 1.1 jmcneill dma[desc].dma_config |=
1014 1.1 jmcneill htole32(SUNXI_MMC_IDMA_CONFIG_DIC);
1015 1.1 jmcneill dma[desc].dma_next = htole32(
1016 1.1 jmcneill desc_paddr + ((desc+1) *
1017 1.1 jmcneill sizeof(struct sunxi_mmc_idma_descriptor)));
1018 1.1 jmcneill }
1019 1.1 jmcneill ++desc;
1020 1.1 jmcneill }
1021 1.1 jmcneill }
1022 1.1 jmcneill if (desc == sc->sc_idma_ndesc) {
1023 1.1 jmcneill aprint_error_dev(sc->sc_dev,
1024 1.21 ryo "not enough descriptors for %d byte transfer! "
1025 1.21 ryo "there are %u segments with a max xfer length of %u\n",
1026 1.21 ryo cmd->c_datalen, map->dm_nsegs, sc->sc_config->idma_xferlen);
1027 1.1 jmcneill return EIO;
1028 1.1 jmcneill }
1029 1.1 jmcneill
1030 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_idma_map, 0,
1031 1.1 jmcneill sc->sc_idma_size, BUS_DMASYNC_PREWRITE);
1032 1.1 jmcneill
1033 1.1 jmcneill sc->sc_idma_idst = 0;
1034 1.1 jmcneill
1035 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_DLBA, desc_paddr);
1036 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_FTRGLEVEL, sc->sc_config->dma_ftrglevel);
1037 1.22 jmcneill
1038 1.1 jmcneill val = MMC_READ(sc, SUNXI_MMC_GCTRL);
1039 1.1 jmcneill val |= SUNXI_MMC_GCTRL_DMAEN;
1040 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL, val);
1041 1.1 jmcneill val |= SUNXI_MMC_GCTRL_DMARESET;
1042 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL, val);
1043 1.22 jmcneill
1044 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_DMAC, SUNXI_MMC_DMAC_SOFTRESET);
1045 1.14 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ))
1046 1.22 jmcneill val = SUNXI_MMC_IDST_RECEIVE_INT;
1047 1.1 jmcneill else
1048 1.22 jmcneill val = 0;
1049 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_IDIE, val);
1050 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_DMAC,
1051 1.22 jmcneill SUNXI_MMC_DMAC_IDMA_ON|SUNXI_MMC_DMAC_FIX_BURST);
1052 1.1 jmcneill
1053 1.1 jmcneill return 0;
1054 1.1 jmcneill }
1055 1.1 jmcneill
1056 1.1 jmcneill static void
1057 1.14 jmcneill sunxi_mmc_dma_complete(struct sunxi_mmc_softc *sc, struct sdmmc_command *cmd)
1058 1.1 jmcneill {
1059 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_DMAC, 0);
1060 1.22 jmcneill
1061 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_idma_map, 0,
1062 1.1 jmcneill sc->sc_idma_size, BUS_DMASYNC_POSTWRITE);
1063 1.14 jmcneill
1064 1.14 jmcneill if (cmd->c_dmamap == NULL) {
1065 1.14 jmcneill if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
1066 1.14 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
1067 1.15 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_POSTREAD);
1068 1.14 jmcneill memcpy(cmd->c_data, sc->sc_dmabounce_buf,
1069 1.14 jmcneill cmd->c_datalen);
1070 1.14 jmcneill } else {
1071 1.14 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dmabounce_map,
1072 1.15 jmcneill 0, cmd->c_datalen, BUS_DMASYNC_POSTWRITE);
1073 1.14 jmcneill }
1074 1.14 jmcneill }
1075 1.1 jmcneill }
1076 1.1 jmcneill
1077 1.1 jmcneill static void
1078 1.1 jmcneill sunxi_mmc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
1079 1.1 jmcneill {
1080 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
1081 1.1 jmcneill uint32_t cmdval = SUNXI_MMC_CMD_START;
1082 1.22 jmcneill uint32_t imask, oimask;
1083 1.2 jmcneill const bool poll = (cmd->c_flags & SCF_POLL) != 0;
1084 1.1 jmcneill int retry;
1085 1.1 jmcneill
1086 1.13 jmcneill DPRINTF(sc->sc_dev,
1087 1.2 jmcneill "opcode %d flags 0x%x data %p datalen %d blklen %d poll %d\n",
1088 1.1 jmcneill cmd->c_opcode, cmd->c_flags, cmd->c_data, cmd->c_datalen,
1089 1.2 jmcneill cmd->c_blklen, poll);
1090 1.1 jmcneill
1091 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
1092 1.1 jmcneill
1093 1.1 jmcneill if (cmd->c_opcode == 0)
1094 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
1095 1.1 jmcneill if (cmd->c_flags & SCF_RSP_PRESENT)
1096 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_RSP_EXP;
1097 1.1 jmcneill if (cmd->c_flags & SCF_RSP_136)
1098 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_LONG_RSP;
1099 1.1 jmcneill if (cmd->c_flags & SCF_RSP_CRC)
1100 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_CHECK_RSP_CRC;
1101 1.1 jmcneill
1102 1.22 jmcneill imask = oimask = MMC_READ(sc, SUNXI_MMC_IMASK);
1103 1.22 jmcneill imask |= SUNXI_MMC_INT_ERROR;
1104 1.22 jmcneill
1105 1.1 jmcneill if (cmd->c_datalen > 0) {
1106 1.1 jmcneill unsigned int nblks;
1107 1.1 jmcneill
1108 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_DATA_EXP | SUNXI_MMC_CMD_WAIT_PRE_OVER;
1109 1.1 jmcneill if (!ISSET(cmd->c_flags, SCF_CMD_READ)) {
1110 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_WRITE;
1111 1.1 jmcneill }
1112 1.1 jmcneill
1113 1.1 jmcneill nblks = cmd->c_datalen / cmd->c_blklen;
1114 1.1 jmcneill if (nblks == 0 || (cmd->c_datalen % cmd->c_blklen) != 0)
1115 1.1 jmcneill ++nblks;
1116 1.1 jmcneill
1117 1.1 jmcneill if (nblks > 1) {
1118 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_SEND_AUTO_STOP;
1119 1.22 jmcneill imask |= SUNXI_MMC_INT_AUTO_CMD_DONE;
1120 1.22 jmcneill } else {
1121 1.22 jmcneill imask |= SUNXI_MMC_INT_DATA_OVER;
1122 1.1 jmcneill }
1123 1.1 jmcneill
1124 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_BLKSZ, cmd->c_blklen);
1125 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_BYTECNT, nblks * cmd->c_blklen);
1126 1.22 jmcneill } else {
1127 1.22 jmcneill imask |= SUNXI_MMC_INT_CMD_DONE;
1128 1.1 jmcneill }
1129 1.1 jmcneill
1130 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_IMASK, imask);
1131 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_RINT, 0xffff);
1132 1.22 jmcneill
1133 1.1 jmcneill sc->sc_intr_rint = 0;
1134 1.1 jmcneill
1135 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_A12A,
1136 1.1 jmcneill (cmdval & SUNXI_MMC_CMD_SEND_AUTO_STOP) ? 0 : 0xffff);
1137 1.1 jmcneill
1138 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_ARG, cmd->c_arg);
1139 1.1 jmcneill
1140 1.13 jmcneill DPRINTF(sc->sc_dev, "cmdval = %08x\n", cmdval);
1141 1.1 jmcneill
1142 1.1 jmcneill if (cmd->c_datalen == 0) {
1143 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CMD, cmdval | cmd->c_opcode);
1144 1.1 jmcneill } else {
1145 1.1 jmcneill cmd->c_resid = cmd->c_datalen;
1146 1.1 jmcneill cmd->c_error = sunxi_mmc_dma_prepare(sc, cmd);
1147 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CMD, cmdval | cmd->c_opcode);
1148 1.22 jmcneill if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_CMD_READ)) {
1149 1.22 jmcneill const uint32_t idst_mask = SUNXI_MMC_IDST_RECEIVE_INT;
1150 1.22 jmcneill
1151 1.1 jmcneill retry = 10;
1152 1.1 jmcneill while ((sc->sc_idma_idst & idst_mask) == 0) {
1153 1.1 jmcneill if (retry-- == 0) {
1154 1.1 jmcneill cmd->c_error = ETIMEDOUT;
1155 1.1 jmcneill break;
1156 1.1 jmcneill }
1157 1.1 jmcneill cv_timedwait(&sc->sc_idst_cv,
1158 1.1 jmcneill &sc->sc_intr_lock, hz);
1159 1.1 jmcneill }
1160 1.1 jmcneill }
1161 1.1 jmcneill }
1162 1.1 jmcneill
1163 1.1 jmcneill cmd->c_error = sunxi_mmc_wait_rint(sc,
1164 1.32 jmcneill SUNXI_MMC_INT_ERROR|SUNXI_MMC_INT_CMD_DONE, hz * 3, poll);
1165 1.1 jmcneill if (cmd->c_error == 0 && (sc->sc_intr_rint & SUNXI_MMC_INT_ERROR)) {
1166 1.1 jmcneill if (sc->sc_intr_rint & SUNXI_MMC_INT_RESP_TIMEOUT) {
1167 1.1 jmcneill cmd->c_error = ETIMEDOUT;
1168 1.1 jmcneill } else {
1169 1.1 jmcneill cmd->c_error = EIO;
1170 1.1 jmcneill }
1171 1.1 jmcneill }
1172 1.1 jmcneill if (cmd->c_error) {
1173 1.13 jmcneill DPRINTF(sc->sc_dev,
1174 1.1 jmcneill "cmd failed, error %d\n", cmd->c_error);
1175 1.1 jmcneill goto done;
1176 1.1 jmcneill }
1177 1.1 jmcneill
1178 1.1 jmcneill if (cmd->c_datalen > 0) {
1179 1.22 jmcneill sunxi_mmc_dma_complete(sc, cmd);
1180 1.22 jmcneill
1181 1.1 jmcneill cmd->c_error = sunxi_mmc_wait_rint(sc,
1182 1.1 jmcneill SUNXI_MMC_INT_ERROR|
1183 1.1 jmcneill SUNXI_MMC_INT_AUTO_CMD_DONE|
1184 1.1 jmcneill SUNXI_MMC_INT_DATA_OVER,
1185 1.32 jmcneill hz*3, poll);
1186 1.1 jmcneill if (cmd->c_error == 0 &&
1187 1.1 jmcneill (sc->sc_intr_rint & SUNXI_MMC_INT_ERROR)) {
1188 1.1 jmcneill cmd->c_error = ETIMEDOUT;
1189 1.1 jmcneill }
1190 1.1 jmcneill if (cmd->c_error) {
1191 1.13 jmcneill DPRINTF(sc->sc_dev,
1192 1.1 jmcneill "data timeout, rint = %08x\n",
1193 1.1 jmcneill sc->sc_intr_rint);
1194 1.1 jmcneill cmd->c_error = ETIMEDOUT;
1195 1.1 jmcneill goto done;
1196 1.1 jmcneill }
1197 1.1 jmcneill }
1198 1.1 jmcneill
1199 1.1 jmcneill if (cmd->c_flags & SCF_RSP_PRESENT) {
1200 1.1 jmcneill if (cmd->c_flags & SCF_RSP_136) {
1201 1.1 jmcneill cmd->c_resp[0] = MMC_READ(sc, SUNXI_MMC_RESP0);
1202 1.1 jmcneill cmd->c_resp[1] = MMC_READ(sc, SUNXI_MMC_RESP1);
1203 1.1 jmcneill cmd->c_resp[2] = MMC_READ(sc, SUNXI_MMC_RESP2);
1204 1.1 jmcneill cmd->c_resp[3] = MMC_READ(sc, SUNXI_MMC_RESP3);
1205 1.1 jmcneill if (cmd->c_flags & SCF_RSP_CRC) {
1206 1.1 jmcneill cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
1207 1.1 jmcneill (cmd->c_resp[1] << 24);
1208 1.1 jmcneill cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
1209 1.1 jmcneill (cmd->c_resp[2] << 24);
1210 1.1 jmcneill cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
1211 1.1 jmcneill (cmd->c_resp[3] << 24);
1212 1.1 jmcneill cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
1213 1.1 jmcneill }
1214 1.1 jmcneill } else {
1215 1.1 jmcneill cmd->c_resp[0] = MMC_READ(sc, SUNXI_MMC_RESP0);
1216 1.1 jmcneill }
1217 1.1 jmcneill }
1218 1.1 jmcneill
1219 1.1 jmcneill done:
1220 1.1 jmcneill cmd->c_flags |= SCF_ITSDONE;
1221 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_IMASK, oimask);
1222 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_RINT, 0xffff);
1223 1.22 jmcneill MMC_WRITE(sc, SUNXI_MMC_IDST, 0x337);
1224 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
1225 1.1 jmcneill
1226 1.1 jmcneill if (cmd->c_error) {
1227 1.13 jmcneill DPRINTF(sc->sc_dev, "i/o error %d\n", cmd->c_error);
1228 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL,
1229 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_GCTRL) |
1230 1.1 jmcneill SUNXI_MMC_GCTRL_DMARESET | SUNXI_MMC_GCTRL_FIFORESET);
1231 1.1 jmcneill for (retry = 0; retry < 1000; retry++) {
1232 1.1 jmcneill if (!(MMC_READ(sc, SUNXI_MMC_GCTRL) & SUNXI_MMC_GCTRL_RESET))
1233 1.1 jmcneill break;
1234 1.1 jmcneill delay(10);
1235 1.1 jmcneill }
1236 1.1 jmcneill sunxi_mmc_update_clock(sc);
1237 1.1 jmcneill }
1238 1.1 jmcneill
1239 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL,
1240 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_GCTRL) | SUNXI_MMC_GCTRL_FIFORESET);
1241 1.1 jmcneill }
1242 1.1 jmcneill
1243 1.1 jmcneill static void
1244 1.1 jmcneill sunxi_mmc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
1245 1.1 jmcneill {
1246 1.11 jmcneill struct sunxi_mmc_softc *sc = sch;
1247 1.11 jmcneill uint32_t imask;
1248 1.11 jmcneill
1249 1.11 jmcneill imask = MMC_READ(sc, SUNXI_MMC_IMASK);
1250 1.11 jmcneill if (enable)
1251 1.11 jmcneill imask |= SUNXI_MMC_INT_SDIO_INT;
1252 1.11 jmcneill else
1253 1.11 jmcneill imask &= ~SUNXI_MMC_INT_SDIO_INT;
1254 1.11 jmcneill MMC_WRITE(sc, SUNXI_MMC_IMASK, imask);
1255 1.1 jmcneill }
1256 1.1 jmcneill
1257 1.1 jmcneill static void
1258 1.1 jmcneill sunxi_mmc_card_intr_ack(sdmmc_chipset_handle_t sch)
1259 1.1 jmcneill {
1260 1.11 jmcneill struct sunxi_mmc_softc *sc = sch;
1261 1.11 jmcneill
1262 1.11 jmcneill MMC_WRITE(sc, SUNXI_MMC_RINT, SUNXI_MMC_INT_SDIO_INT);
1263 1.1 jmcneill }
1264