sunxi_mmc.c revision 1.13 1 1.13 jmcneill /* $NetBSD: sunxi_mmc.c,v 1.13 2017/10/23 11:06:31 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.13 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_mmc.c,v 1.13 2017/10/23 11:06:31 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.1 jmcneill #define SUNXI_MMC_NDESC 16
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.1 jmcneill static int sunxi_mmc_idma_setup(struct sunxi_mmc_softc *);
100 1.1 jmcneill
101 1.1 jmcneill static int sunxi_mmc_host_reset(sdmmc_chipset_handle_t);
102 1.1 jmcneill static uint32_t sunxi_mmc_host_ocr(sdmmc_chipset_handle_t);
103 1.1 jmcneill static int sunxi_mmc_host_maxblklen(sdmmc_chipset_handle_t);
104 1.1 jmcneill static int sunxi_mmc_card_detect(sdmmc_chipset_handle_t);
105 1.1 jmcneill static int sunxi_mmc_write_protect(sdmmc_chipset_handle_t);
106 1.1 jmcneill static int sunxi_mmc_bus_power(sdmmc_chipset_handle_t, uint32_t);
107 1.3 jmcneill static int sunxi_mmc_bus_clock(sdmmc_chipset_handle_t, int, bool);
108 1.1 jmcneill static int sunxi_mmc_bus_width(sdmmc_chipset_handle_t, int);
109 1.1 jmcneill static int sunxi_mmc_bus_rod(sdmmc_chipset_handle_t, int);
110 1.3 jmcneill static int sunxi_mmc_signal_voltage(sdmmc_chipset_handle_t, int);
111 1.1 jmcneill static void sunxi_mmc_exec_command(sdmmc_chipset_handle_t,
112 1.1 jmcneill struct sdmmc_command *);
113 1.1 jmcneill static void sunxi_mmc_card_enable_intr(sdmmc_chipset_handle_t, int);
114 1.1 jmcneill static void sunxi_mmc_card_intr_ack(sdmmc_chipset_handle_t);
115 1.1 jmcneill
116 1.1 jmcneill static struct sdmmc_chip_functions sunxi_mmc_chip_functions = {
117 1.1 jmcneill .host_reset = sunxi_mmc_host_reset,
118 1.1 jmcneill .host_ocr = sunxi_mmc_host_ocr,
119 1.1 jmcneill .host_maxblklen = sunxi_mmc_host_maxblklen,
120 1.1 jmcneill .card_detect = sunxi_mmc_card_detect,
121 1.1 jmcneill .write_protect = sunxi_mmc_write_protect,
122 1.1 jmcneill .bus_power = sunxi_mmc_bus_power,
123 1.3 jmcneill .bus_clock_ddr = sunxi_mmc_bus_clock,
124 1.1 jmcneill .bus_width = sunxi_mmc_bus_width,
125 1.1 jmcneill .bus_rod = sunxi_mmc_bus_rod,
126 1.3 jmcneill .signal_voltage = sunxi_mmc_signal_voltage,
127 1.1 jmcneill .exec_command = sunxi_mmc_exec_command,
128 1.1 jmcneill .card_enable_intr = sunxi_mmc_card_enable_intr,
129 1.1 jmcneill .card_intr_ack = sunxi_mmc_card_intr_ack,
130 1.1 jmcneill };
131 1.1 jmcneill
132 1.7 jmcneill struct sunxi_mmc_config {
133 1.7 jmcneill u_int idma_xferlen;
134 1.7 jmcneill u_int flags;
135 1.7 jmcneill #define SUNXI_MMC_FLAG_CALIB_REG 0x01
136 1.7 jmcneill #define SUNXI_MMC_FLAG_NEW_TIMINGS 0x02
137 1.7 jmcneill #define SUNXI_MMC_FLAG_MASK_DATA0 0x04
138 1.7 jmcneill const struct sunxi_mmc_delay *delays;
139 1.7 jmcneill uint32_t dma_ftrglevel;
140 1.7 jmcneill };
141 1.7 jmcneill
142 1.1 jmcneill struct sunxi_mmc_softc {
143 1.1 jmcneill device_t sc_dev;
144 1.1 jmcneill bus_space_tag_t sc_bst;
145 1.1 jmcneill bus_space_handle_t sc_bsh;
146 1.1 jmcneill bus_dma_tag_t sc_dmat;
147 1.1 jmcneill int sc_phandle;
148 1.1 jmcneill
149 1.1 jmcneill void *sc_ih;
150 1.1 jmcneill kmutex_t sc_intr_lock;
151 1.1 jmcneill kcondvar_t sc_intr_cv;
152 1.1 jmcneill kcondvar_t sc_idst_cv;
153 1.1 jmcneill
154 1.1 jmcneill int sc_mmc_width;
155 1.1 jmcneill int sc_mmc_present;
156 1.1 jmcneill
157 1.1 jmcneill device_t sc_sdmmc_dev;
158 1.1 jmcneill
159 1.7 jmcneill struct sunxi_mmc_config *sc_config;
160 1.1 jmcneill
161 1.1 jmcneill bus_dma_segment_t sc_idma_segs[1];
162 1.1 jmcneill int sc_idma_nsegs;
163 1.1 jmcneill bus_size_t sc_idma_size;
164 1.1 jmcneill bus_dmamap_t sc_idma_map;
165 1.1 jmcneill int sc_idma_ndesc;
166 1.1 jmcneill void *sc_idma_desc;
167 1.1 jmcneill
168 1.1 jmcneill uint32_t sc_intr_rint;
169 1.1 jmcneill uint32_t sc_idma_idst;
170 1.1 jmcneill
171 1.1 jmcneill struct clk *sc_clk_ahb;
172 1.1 jmcneill struct clk *sc_clk_mmc;
173 1.1 jmcneill struct clk *sc_clk_output;
174 1.1 jmcneill struct clk *sc_clk_sample;
175 1.1 jmcneill
176 1.1 jmcneill struct fdtbus_reset *sc_rst_ahb;
177 1.1 jmcneill
178 1.1 jmcneill struct fdtbus_gpio_pin *sc_gpio_cd;
179 1.1 jmcneill int sc_gpio_cd_inverted;
180 1.1 jmcneill struct fdtbus_gpio_pin *sc_gpio_wp;
181 1.1 jmcneill int sc_gpio_wp_inverted;
182 1.3 jmcneill
183 1.3 jmcneill struct fdtbus_regulator *sc_reg_vqmmc;
184 1.12 jmcneill
185 1.12 jmcneill struct fdtbus_mmc_pwrseq *sc_pwrseq;
186 1.1 jmcneill };
187 1.1 jmcneill
188 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_mmc, sizeof(struct sunxi_mmc_softc),
189 1.1 jmcneill sunxi_mmc_match, sunxi_mmc_attach, NULL, NULL);
190 1.1 jmcneill
191 1.1 jmcneill #define MMC_WRITE(sc, reg, val) \
192 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
193 1.1 jmcneill #define MMC_READ(sc, reg) \
194 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
195 1.1 jmcneill
196 1.9 jmcneill static const struct sunxi_mmc_config sun4i_a10_mmc_config = {
197 1.9 jmcneill .idma_xferlen = 0x2000,
198 1.9 jmcneill .dma_ftrglevel = 0x20070008,
199 1.9 jmcneill .delays = NULL,
200 1.9 jmcneill .flags = 0,
201 1.9 jmcneill };
202 1.9 jmcneill
203 1.7 jmcneill static const struct sunxi_mmc_config sun5i_a13_mmc_config = {
204 1.7 jmcneill .idma_xferlen = 0x10000,
205 1.7 jmcneill .dma_ftrglevel = 0x20070008,
206 1.7 jmcneill .delays = NULL,
207 1.7 jmcneill .flags = 0,
208 1.7 jmcneill };
209 1.7 jmcneill
210 1.7 jmcneill static const struct sunxi_mmc_config sun7i_a20_mmc_config = {
211 1.8 jmcneill .idma_xferlen = 0x2000,
212 1.7 jmcneill .dma_ftrglevel = 0x20070008,
213 1.10 jmcneill .delays = sun7i_mmc_delays,
214 1.10 jmcneill .flags = 0,
215 1.10 jmcneill };
216 1.10 jmcneill
217 1.10 jmcneill static const struct sunxi_mmc_config sun9i_a80_mmc_config = {
218 1.10 jmcneill .idma_xferlen = 0x10000,
219 1.10 jmcneill .dma_ftrglevel = 0x200f0010,
220 1.10 jmcneill .delays = sun9i_mmc_delays,
221 1.7 jmcneill .flags = 0,
222 1.7 jmcneill };
223 1.7 jmcneill
224 1.7 jmcneill static const struct sunxi_mmc_config sun50i_a64_mmc_config = {
225 1.7 jmcneill .idma_xferlen = 0x10000,
226 1.7 jmcneill .dma_ftrglevel = 0x20070008,
227 1.7 jmcneill .delays = NULL,
228 1.7 jmcneill .flags = SUNXI_MMC_FLAG_CALIB_REG |
229 1.7 jmcneill SUNXI_MMC_FLAG_NEW_TIMINGS |
230 1.7 jmcneill SUNXI_MMC_FLAG_MASK_DATA0,
231 1.7 jmcneill };
232 1.7 jmcneill
233 1.7 jmcneill static const struct of_compat_data compat_data[] = {
234 1.9 jmcneill { "allwinner,sun4i-a10-mmc", (uintptr_t)&sun4i_a10_mmc_config },
235 1.7 jmcneill { "allwinner,sun5i-a13-mmc", (uintptr_t)&sun5i_a13_mmc_config },
236 1.7 jmcneill { "allwinner,sun7i-a20-mmc", (uintptr_t)&sun7i_a20_mmc_config },
237 1.10 jmcneill { "allwinner,sun9i-a80-mmc", (uintptr_t)&sun9i_a80_mmc_config },
238 1.7 jmcneill { "allwinner,sun50i-a64-mmc", (uintptr_t)&sun50i_a64_mmc_config },
239 1.7 jmcneill { NULL }
240 1.1 jmcneill };
241 1.1 jmcneill
242 1.1 jmcneill static int
243 1.1 jmcneill sunxi_mmc_match(device_t parent, cfdata_t cf, void *aux)
244 1.1 jmcneill {
245 1.1 jmcneill struct fdt_attach_args * const faa = aux;
246 1.1 jmcneill
247 1.7 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
248 1.1 jmcneill }
249 1.1 jmcneill
250 1.1 jmcneill static void
251 1.1 jmcneill sunxi_mmc_attach(device_t parent, device_t self, void *aux)
252 1.1 jmcneill {
253 1.1 jmcneill struct sunxi_mmc_softc * const sc = device_private(self);
254 1.1 jmcneill struct fdt_attach_args * const faa = aux;
255 1.1 jmcneill const int phandle = faa->faa_phandle;
256 1.1 jmcneill char intrstr[128];
257 1.1 jmcneill bus_addr_t addr;
258 1.1 jmcneill bus_size_t size;
259 1.1 jmcneill
260 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
261 1.1 jmcneill aprint_error(": couldn't get registers\n");
262 1.1 jmcneill return;
263 1.1 jmcneill }
264 1.1 jmcneill
265 1.1 jmcneill sc->sc_clk_ahb = fdtbus_clock_get(phandle, "ahb");
266 1.1 jmcneill sc->sc_clk_mmc = fdtbus_clock_get(phandle, "mmc");
267 1.1 jmcneill sc->sc_clk_output = fdtbus_clock_get(phandle, "output");
268 1.1 jmcneill sc->sc_clk_sample = fdtbus_clock_get(phandle, "sample");
269 1.1 jmcneill
270 1.1 jmcneill #if notyet
271 1.1 jmcneill if (sc->sc_clk_ahb == NULL || sc->sc_clk_mmc == NULL ||
272 1.1 jmcneill sc->sc_clk_output == NULL || sc->sc_clk_sample == NULL) {
273 1.1 jmcneill #else
274 1.1 jmcneill if (sc->sc_clk_ahb == NULL || sc->sc_clk_mmc == NULL) {
275 1.1 jmcneill #endif
276 1.1 jmcneill aprint_error(": couldn't get clocks\n");
277 1.1 jmcneill return;
278 1.1 jmcneill }
279 1.1 jmcneill
280 1.1 jmcneill sc->sc_rst_ahb = fdtbus_reset_get(phandle, "ahb");
281 1.1 jmcneill
282 1.3 jmcneill sc->sc_reg_vqmmc = fdtbus_regulator_acquire(phandle, "vqmmc-supply");
283 1.3 jmcneill
284 1.12 jmcneill sc->sc_pwrseq = fdtbus_mmc_pwrseq_get(phandle);
285 1.12 jmcneill
286 1.1 jmcneill if (clk_enable(sc->sc_clk_ahb) != 0 ||
287 1.1 jmcneill clk_enable(sc->sc_clk_mmc) != 0) {
288 1.1 jmcneill aprint_error(": couldn't enable clocks\n");
289 1.1 jmcneill return;
290 1.1 jmcneill }
291 1.1 jmcneill
292 1.5 jmcneill if (sc->sc_rst_ahb != NULL) {
293 1.5 jmcneill if (fdtbus_reset_deassert(sc->sc_rst_ahb) != 0) {
294 1.5 jmcneill aprint_error(": couldn't de-assert resets\n");
295 1.5 jmcneill return;
296 1.5 jmcneill }
297 1.1 jmcneill }
298 1.1 jmcneill
299 1.1 jmcneill sc->sc_dev = self;
300 1.1 jmcneill sc->sc_phandle = phandle;
301 1.7 jmcneill sc->sc_config = (void *)of_search_compatible(phandle, compat_data)->data;
302 1.1 jmcneill sc->sc_bst = faa->faa_bst;
303 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
304 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_BIO);
305 1.1 jmcneill cv_init(&sc->sc_intr_cv, "awinmmcirq");
306 1.1 jmcneill cv_init(&sc->sc_idst_cv, "awinmmcdma");
307 1.1 jmcneill
308 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
309 1.1 jmcneill aprint_error(": couldn't map registers\n");
310 1.1 jmcneill return;
311 1.1 jmcneill }
312 1.1 jmcneill
313 1.1 jmcneill aprint_naive("\n");
314 1.1 jmcneill aprint_normal(": SD/MMC controller\n");
315 1.1 jmcneill
316 1.1 jmcneill sc->sc_gpio_cd = fdtbus_gpio_acquire(phandle, "cd-gpios",
317 1.1 jmcneill GPIO_PIN_INPUT);
318 1.1 jmcneill sc->sc_gpio_wp = fdtbus_gpio_acquire(phandle, "wp-gpios",
319 1.1 jmcneill GPIO_PIN_INPUT);
320 1.1 jmcneill
321 1.1 jmcneill sc->sc_gpio_cd_inverted = of_hasprop(phandle, "cd-inverted") ? 0 : 1;
322 1.1 jmcneill sc->sc_gpio_wp_inverted = of_hasprop(phandle, "wp-inverted") ? 0 : 1;
323 1.1 jmcneill
324 1.1 jmcneill if (sunxi_mmc_idma_setup(sc) != 0) {
325 1.1 jmcneill aprint_error_dev(self, "failed to setup DMA\n");
326 1.1 jmcneill return;
327 1.1 jmcneill }
328 1.1 jmcneill
329 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
330 1.1 jmcneill aprint_error_dev(self, "failed to decode interrupt\n");
331 1.1 jmcneill return;
332 1.1 jmcneill }
333 1.1 jmcneill
334 1.1 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, FDT_INTR_MPSAFE,
335 1.1 jmcneill sunxi_mmc_intr, sc);
336 1.1 jmcneill if (sc->sc_ih == NULL) {
337 1.1 jmcneill aprint_error_dev(self, "failed to establish interrupt on %s\n",
338 1.1 jmcneill intrstr);
339 1.1 jmcneill return;
340 1.1 jmcneill }
341 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
342 1.1 jmcneill
343 1.1 jmcneill config_interrupts(self, sunxi_mmc_attach_i);
344 1.1 jmcneill }
345 1.1 jmcneill
346 1.1 jmcneill static int
347 1.1 jmcneill sunxi_mmc_idma_setup(struct sunxi_mmc_softc *sc)
348 1.1 jmcneill {
349 1.1 jmcneill int error;
350 1.1 jmcneill
351 1.1 jmcneill sc->sc_idma_ndesc = SUNXI_MMC_NDESC;
352 1.1 jmcneill sc->sc_idma_size = sizeof(struct sunxi_mmc_idma_descriptor) *
353 1.1 jmcneill sc->sc_idma_ndesc;
354 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_idma_size, 0,
355 1.1 jmcneill sc->sc_idma_size, sc->sc_idma_segs, 1,
356 1.1 jmcneill &sc->sc_idma_nsegs, BUS_DMA_WAITOK);
357 1.1 jmcneill if (error)
358 1.1 jmcneill return error;
359 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, sc->sc_idma_segs,
360 1.1 jmcneill sc->sc_idma_nsegs, sc->sc_idma_size,
361 1.1 jmcneill &sc->sc_idma_desc, BUS_DMA_WAITOK);
362 1.1 jmcneill if (error)
363 1.1 jmcneill goto free;
364 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, sc->sc_idma_size, 1,
365 1.1 jmcneill sc->sc_idma_size, 0, BUS_DMA_WAITOK, &sc->sc_idma_map);
366 1.1 jmcneill if (error)
367 1.1 jmcneill goto unmap;
368 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->sc_idma_map,
369 1.1 jmcneill sc->sc_idma_desc, sc->sc_idma_size, NULL, BUS_DMA_WAITOK);
370 1.1 jmcneill if (error)
371 1.1 jmcneill goto destroy;
372 1.1 jmcneill return 0;
373 1.1 jmcneill
374 1.1 jmcneill destroy:
375 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, sc->sc_idma_map);
376 1.1 jmcneill unmap:
377 1.1 jmcneill bus_dmamem_unmap(sc->sc_dmat, sc->sc_idma_desc, sc->sc_idma_size);
378 1.1 jmcneill free:
379 1.1 jmcneill bus_dmamem_free(sc->sc_dmat, sc->sc_idma_segs, sc->sc_idma_nsegs);
380 1.1 jmcneill return error;
381 1.1 jmcneill }
382 1.1 jmcneill
383 1.1 jmcneill static int
384 1.3 jmcneill sunxi_mmc_set_clock(struct sunxi_mmc_softc *sc, u_int freq, bool ddr)
385 1.1 jmcneill {
386 1.3 jmcneill const struct sunxi_mmc_delay *delays;
387 1.3 jmcneill int error, timing;
388 1.3 jmcneill
389 1.3 jmcneill if (freq <= 400) {
390 1.3 jmcneill timing = SUNXI_MMC_TIMING_400K;
391 1.3 jmcneill } else if (freq <= 25000) {
392 1.3 jmcneill timing = SUNXI_MMC_TIMING_25M;
393 1.3 jmcneill } else if (freq <= 52000) {
394 1.3 jmcneill if (ddr) {
395 1.3 jmcneill timing = sc->sc_mmc_width == 8 ?
396 1.3 jmcneill SUNXI_MMC_TIMING_50M_DDR_8BIT :
397 1.3 jmcneill SUNXI_MMC_TIMING_50M_DDR;
398 1.3 jmcneill } else {
399 1.3 jmcneill timing = SUNXI_MMC_TIMING_50M;
400 1.3 jmcneill }
401 1.3 jmcneill } else
402 1.3 jmcneill return EINVAL;
403 1.3 jmcneill
404 1.3 jmcneill error = clk_set_rate(sc->sc_clk_mmc, (freq * 1000) << ddr);
405 1.3 jmcneill if (error != 0)
406 1.3 jmcneill return error;
407 1.3 jmcneill
408 1.7 jmcneill if (sc->sc_config->delays == NULL)
409 1.7 jmcneill return 0;
410 1.7 jmcneill
411 1.7 jmcneill delays = &sc->sc_config->delays[timing];
412 1.7 jmcneill
413 1.3 jmcneill if (sc->sc_clk_sample) {
414 1.3 jmcneill error = clk_set_rate(sc->sc_clk_sample, delays->sample_phase);
415 1.3 jmcneill if (error != 0)
416 1.3 jmcneill return error;
417 1.3 jmcneill }
418 1.3 jmcneill if (sc->sc_clk_output) {
419 1.3 jmcneill error = clk_set_rate(sc->sc_clk_output, delays->output_phase);
420 1.3 jmcneill if (error != 0)
421 1.3 jmcneill return error;
422 1.3 jmcneill }
423 1.3 jmcneill
424 1.3 jmcneill return 0;
425 1.1 jmcneill }
426 1.1 jmcneill
427 1.1 jmcneill static void
428 1.1 jmcneill sunxi_mmc_attach_i(device_t self)
429 1.1 jmcneill {
430 1.1 jmcneill struct sunxi_mmc_softc *sc = device_private(self);
431 1.1 jmcneill struct sdmmcbus_attach_args saa;
432 1.1 jmcneill uint32_t width;
433 1.1 jmcneill
434 1.12 jmcneill if (sc->sc_pwrseq)
435 1.12 jmcneill fdtbus_mmc_pwrseq_pre_power_on(sc->sc_pwrseq);
436 1.12 jmcneill
437 1.1 jmcneill sunxi_mmc_host_reset(sc);
438 1.1 jmcneill sunxi_mmc_bus_width(sc, 1);
439 1.3 jmcneill sunxi_mmc_set_clock(sc, 400, false);
440 1.1 jmcneill
441 1.12 jmcneill if (sc->sc_pwrseq)
442 1.12 jmcneill fdtbus_mmc_pwrseq_post_power_on(sc->sc_pwrseq);
443 1.12 jmcneill
444 1.1 jmcneill if (of_getprop_uint32(sc->sc_phandle, "bus-width", &width) != 0)
445 1.1 jmcneill width = 4;
446 1.1 jmcneill
447 1.1 jmcneill memset(&saa, 0, sizeof(saa));
448 1.1 jmcneill saa.saa_busname = "sdmmc";
449 1.1 jmcneill saa.saa_sct = &sunxi_mmc_chip_functions;
450 1.1 jmcneill saa.saa_sch = sc;
451 1.1 jmcneill saa.saa_dmat = sc->sc_dmat;
452 1.1 jmcneill saa.saa_clkmin = 400;
453 1.1 jmcneill saa.saa_clkmax = 52000;
454 1.1 jmcneill saa.saa_caps = SMC_CAPS_DMA |
455 1.1 jmcneill SMC_CAPS_MULTI_SEG_DMA |
456 1.1 jmcneill SMC_CAPS_AUTO_STOP |
457 1.1 jmcneill SMC_CAPS_SD_HIGHSPEED |
458 1.2 jmcneill SMC_CAPS_MMC_HIGHSPEED |
459 1.3 jmcneill SMC_CAPS_MMC_DDR52 |
460 1.2 jmcneill SMC_CAPS_POLLING;
461 1.1 jmcneill if (width == 4)
462 1.1 jmcneill saa.saa_caps |= SMC_CAPS_4BIT_MODE;
463 1.1 jmcneill if (width == 8)
464 1.1 jmcneill saa.saa_caps |= SMC_CAPS_8BIT_MODE;
465 1.1 jmcneill
466 1.1 jmcneill if (sc->sc_gpio_cd)
467 1.1 jmcneill saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
468 1.1 jmcneill
469 1.1 jmcneill sc->sc_sdmmc_dev = config_found(self, &saa, NULL);
470 1.1 jmcneill }
471 1.1 jmcneill
472 1.1 jmcneill static int
473 1.1 jmcneill sunxi_mmc_intr(void *priv)
474 1.1 jmcneill {
475 1.1 jmcneill struct sunxi_mmc_softc *sc = priv;
476 1.11 jmcneill uint32_t idst, rint;
477 1.1 jmcneill
478 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
479 1.1 jmcneill idst = MMC_READ(sc, SUNXI_MMC_IDST);
480 1.1 jmcneill rint = MMC_READ(sc, SUNXI_MMC_RINT);
481 1.11 jmcneill if (!idst && !rint) {
482 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
483 1.1 jmcneill return 0;
484 1.1 jmcneill }
485 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_IDST, idst);
486 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_RINT, rint);
487 1.1 jmcneill
488 1.13 jmcneill DPRINTF(sc->sc_dev, "mmc intr idst=%08X rint=%08X\n",
489 1.11 jmcneill idst, rint);
490 1.1 jmcneill
491 1.11 jmcneill if (idst != 0) {
492 1.1 jmcneill sc->sc_idma_idst |= idst;
493 1.1 jmcneill cv_broadcast(&sc->sc_idst_cv);
494 1.1 jmcneill }
495 1.1 jmcneill
496 1.11 jmcneill if ((rint & ~SUNXI_MMC_INT_SDIO_INT) != 0) {
497 1.11 jmcneill sc->sc_intr_rint |= (rint & ~SUNXI_MMC_INT_SDIO_INT);
498 1.1 jmcneill cv_broadcast(&sc->sc_intr_cv);
499 1.1 jmcneill }
500 1.1 jmcneill
501 1.11 jmcneill if ((rint & SUNXI_MMC_INT_SDIO_INT) != 0) {
502 1.11 jmcneill sdmmc_card_intr(sc->sc_sdmmc_dev);
503 1.11 jmcneill }
504 1.11 jmcneill
505 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
506 1.1 jmcneill
507 1.1 jmcneill return 1;
508 1.1 jmcneill }
509 1.1 jmcneill
510 1.1 jmcneill static int
511 1.2 jmcneill sunxi_mmc_wait_rint(struct sunxi_mmc_softc *sc, uint32_t mask,
512 1.2 jmcneill int timeout, bool poll)
513 1.1 jmcneill {
514 1.1 jmcneill int retry;
515 1.1 jmcneill int error;
516 1.1 jmcneill
517 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_intr_lock));
518 1.1 jmcneill
519 1.1 jmcneill if (sc->sc_intr_rint & mask)
520 1.1 jmcneill return 0;
521 1.1 jmcneill
522 1.2 jmcneill if (poll)
523 1.2 jmcneill retry = timeout / hz * 1000;
524 1.2 jmcneill else
525 1.2 jmcneill retry = timeout / hz;
526 1.1 jmcneill
527 1.1 jmcneill while (retry > 0) {
528 1.2 jmcneill if (poll) {
529 1.2 jmcneill sc->sc_intr_rint |= MMC_READ(sc, SUNXI_MMC_RINT);
530 1.2 jmcneill } else {
531 1.2 jmcneill error = cv_timedwait(&sc->sc_intr_cv,
532 1.2 jmcneill &sc->sc_intr_lock, hz);
533 1.2 jmcneill if (error && error != EWOULDBLOCK)
534 1.2 jmcneill return error;
535 1.2 jmcneill }
536 1.1 jmcneill if (sc->sc_intr_rint & mask)
537 1.1 jmcneill return 0;
538 1.2 jmcneill if (poll)
539 1.2 jmcneill delay(1000);
540 1.1 jmcneill --retry;
541 1.1 jmcneill }
542 1.1 jmcneill
543 1.1 jmcneill return ETIMEDOUT;
544 1.1 jmcneill }
545 1.1 jmcneill
546 1.1 jmcneill static int
547 1.1 jmcneill sunxi_mmc_host_reset(sdmmc_chipset_handle_t sch)
548 1.1 jmcneill {
549 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
550 1.1 jmcneill int retry = 1000;
551 1.1 jmcneill
552 1.13 jmcneill DPRINTF(sc->sc_dev, "host reset\n");
553 1.1 jmcneill
554 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL,
555 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_GCTRL) | SUNXI_MMC_GCTRL_RESET);
556 1.1 jmcneill while (--retry > 0) {
557 1.1 jmcneill if (!(MMC_READ(sc, SUNXI_MMC_GCTRL) & SUNXI_MMC_GCTRL_RESET))
558 1.1 jmcneill break;
559 1.1 jmcneill delay(100);
560 1.1 jmcneill }
561 1.1 jmcneill
562 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_TIMEOUT, 0xffffffff);
563 1.1 jmcneill
564 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_IMASK,
565 1.1 jmcneill SUNXI_MMC_INT_CMD_DONE | SUNXI_MMC_INT_ERROR |
566 1.1 jmcneill SUNXI_MMC_INT_DATA_OVER | SUNXI_MMC_INT_AUTO_CMD_DONE);
567 1.1 jmcneill
568 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL,
569 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_GCTRL) | SUNXI_MMC_GCTRL_INTEN);
570 1.1 jmcneill
571 1.1 jmcneill return 0;
572 1.1 jmcneill }
573 1.1 jmcneill
574 1.1 jmcneill static uint32_t
575 1.1 jmcneill sunxi_mmc_host_ocr(sdmmc_chipset_handle_t sch)
576 1.1 jmcneill {
577 1.1 jmcneill return MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V | MMC_OCR_HCS;
578 1.1 jmcneill }
579 1.1 jmcneill
580 1.1 jmcneill static int
581 1.1 jmcneill sunxi_mmc_host_maxblklen(sdmmc_chipset_handle_t sch)
582 1.1 jmcneill {
583 1.1 jmcneill return 8192;
584 1.1 jmcneill }
585 1.1 jmcneill
586 1.1 jmcneill static int
587 1.1 jmcneill sunxi_mmc_card_detect(sdmmc_chipset_handle_t sch)
588 1.1 jmcneill {
589 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
590 1.1 jmcneill
591 1.1 jmcneill if (sc->sc_gpio_cd == NULL) {
592 1.1 jmcneill return 1; /* no card detect pin, assume present */
593 1.1 jmcneill } else {
594 1.1 jmcneill int v = 0, i;
595 1.1 jmcneill for (i = 0; i < 5; i++) {
596 1.1 jmcneill v += (fdtbus_gpio_read(sc->sc_gpio_cd) ^
597 1.1 jmcneill sc->sc_gpio_cd_inverted);
598 1.1 jmcneill delay(1000);
599 1.1 jmcneill }
600 1.1 jmcneill if (v == 5)
601 1.1 jmcneill sc->sc_mmc_present = 0;
602 1.1 jmcneill else if (v == 0)
603 1.1 jmcneill sc->sc_mmc_present = 1;
604 1.1 jmcneill return sc->sc_mmc_present;
605 1.1 jmcneill }
606 1.1 jmcneill }
607 1.1 jmcneill
608 1.1 jmcneill static int
609 1.1 jmcneill sunxi_mmc_write_protect(sdmmc_chipset_handle_t sch)
610 1.1 jmcneill {
611 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
612 1.1 jmcneill
613 1.1 jmcneill if (sc->sc_gpio_wp == NULL) {
614 1.1 jmcneill return 0; /* no write protect pin, assume rw */
615 1.1 jmcneill } else {
616 1.1 jmcneill return fdtbus_gpio_read(sc->sc_gpio_wp) ^
617 1.1 jmcneill sc->sc_gpio_wp_inverted;
618 1.1 jmcneill }
619 1.1 jmcneill }
620 1.1 jmcneill
621 1.1 jmcneill static int
622 1.1 jmcneill sunxi_mmc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
623 1.1 jmcneill {
624 1.1 jmcneill return 0;
625 1.1 jmcneill }
626 1.1 jmcneill
627 1.1 jmcneill static int
628 1.1 jmcneill sunxi_mmc_update_clock(struct sunxi_mmc_softc *sc)
629 1.1 jmcneill {
630 1.1 jmcneill uint32_t cmd;
631 1.1 jmcneill int retry;
632 1.1 jmcneill
633 1.13 jmcneill DPRINTF(sc->sc_dev, "update clock\n");
634 1.1 jmcneill
635 1.1 jmcneill cmd = SUNXI_MMC_CMD_START |
636 1.1 jmcneill SUNXI_MMC_CMD_UPCLK_ONLY |
637 1.1 jmcneill SUNXI_MMC_CMD_WAIT_PRE_OVER;
638 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CMD, cmd);
639 1.1 jmcneill retry = 0xfffff;
640 1.1 jmcneill while (--retry > 0) {
641 1.1 jmcneill if (!(MMC_READ(sc, SUNXI_MMC_CMD) & SUNXI_MMC_CMD_START))
642 1.1 jmcneill break;
643 1.1 jmcneill delay(10);
644 1.1 jmcneill }
645 1.1 jmcneill
646 1.1 jmcneill if (retry == 0) {
647 1.1 jmcneill aprint_error_dev(sc->sc_dev, "timeout updating clock\n");
648 1.13 jmcneill DPRINTF(sc->sc_dev, "GCTRL: 0x%08x\n",
649 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_GCTRL));
650 1.13 jmcneill DPRINTF(sc->sc_dev, "CLKCR: 0x%08x\n",
651 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_CLKCR));
652 1.13 jmcneill DPRINTF(sc->sc_dev, "TIMEOUT: 0x%08x\n",
653 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_TIMEOUT));
654 1.13 jmcneill DPRINTF(sc->sc_dev, "WIDTH: 0x%08x\n",
655 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_WIDTH));
656 1.13 jmcneill DPRINTF(sc->sc_dev, "CMD: 0x%08x\n",
657 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_CMD));
658 1.13 jmcneill DPRINTF(sc->sc_dev, "MINT: 0x%08x\n",
659 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_MINT));
660 1.13 jmcneill DPRINTF(sc->sc_dev, "RINT: 0x%08x\n",
661 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_RINT));
662 1.13 jmcneill DPRINTF(sc->sc_dev, "STATUS: 0x%08x\n",
663 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_STATUS));
664 1.1 jmcneill return ETIMEDOUT;
665 1.1 jmcneill }
666 1.1 jmcneill
667 1.1 jmcneill return 0;
668 1.1 jmcneill }
669 1.1 jmcneill
670 1.1 jmcneill static int
671 1.3 jmcneill sunxi_mmc_bus_clock(sdmmc_chipset_handle_t sch, int freq, bool ddr)
672 1.1 jmcneill {
673 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
674 1.7 jmcneill uint32_t clkcr, gctrl, ntsr;
675 1.7 jmcneill const u_int flags = sc->sc_config->flags;
676 1.1 jmcneill
677 1.1 jmcneill clkcr = MMC_READ(sc, SUNXI_MMC_CLKCR);
678 1.1 jmcneill if (clkcr & SUNXI_MMC_CLKCR_CARDCLKON) {
679 1.1 jmcneill clkcr &= ~SUNXI_MMC_CLKCR_CARDCLKON;
680 1.7 jmcneill if (flags & SUNXI_MMC_CLKCR_MASK_DATA0)
681 1.7 jmcneill clkcr |= SUNXI_MMC_CLKCR_MASK_DATA0;
682 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
683 1.1 jmcneill if (sunxi_mmc_update_clock(sc) != 0)
684 1.1 jmcneill return 1;
685 1.7 jmcneill if (flags & SUNXI_MMC_CLKCR_MASK_DATA0) {
686 1.7 jmcneill clkcr = MMC_READ(sc, SUNXI_MMC_CLKCR);
687 1.7 jmcneill clkcr &= ~SUNXI_MMC_CLKCR_MASK_DATA0;
688 1.7 jmcneill MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
689 1.7 jmcneill }
690 1.1 jmcneill }
691 1.1 jmcneill
692 1.1 jmcneill if (freq) {
693 1.1 jmcneill
694 1.1 jmcneill clkcr &= ~SUNXI_MMC_CLKCR_DIV;
695 1.3 jmcneill clkcr |= __SHIFTIN(ddr, SUNXI_MMC_CLKCR_DIV);
696 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
697 1.7 jmcneill
698 1.7 jmcneill if (flags & SUNXI_MMC_FLAG_NEW_TIMINGS) {
699 1.7 jmcneill ntsr = MMC_READ(sc, SUNXI_MMC_NTSR);
700 1.7 jmcneill ntsr |= SUNXI_MMC_NTSR_MODE_SELECT;
701 1.7 jmcneill MMC_WRITE(sc, SUNXI_MMC_NTSR, ntsr);
702 1.7 jmcneill }
703 1.7 jmcneill
704 1.7 jmcneill if (flags & SUNXI_MMC_FLAG_CALIB_REG)
705 1.7 jmcneill MMC_WRITE(sc, SUNXI_MMC_SAMP_DL, SUNXI_MMC_SAMP_DL_SW_EN);
706 1.7 jmcneill
707 1.1 jmcneill if (sunxi_mmc_update_clock(sc) != 0)
708 1.1 jmcneill return 1;
709 1.1 jmcneill
710 1.3 jmcneill gctrl = MMC_READ(sc, SUNXI_MMC_GCTRL);
711 1.3 jmcneill if (ddr)
712 1.3 jmcneill gctrl |= SUNXI_MMC_GCTRL_DDR_MODE;
713 1.3 jmcneill else
714 1.3 jmcneill gctrl &= ~SUNXI_MMC_GCTRL_DDR_MODE;
715 1.3 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL, gctrl);
716 1.3 jmcneill
717 1.3 jmcneill if (sunxi_mmc_set_clock(sc, freq, ddr) != 0)
718 1.1 jmcneill return 1;
719 1.1 jmcneill
720 1.1 jmcneill clkcr |= SUNXI_MMC_CLKCR_CARDCLKON;
721 1.7 jmcneill if (flags & SUNXI_MMC_CLKCR_MASK_DATA0)
722 1.7 jmcneill clkcr |= SUNXI_MMC_CLKCR_MASK_DATA0;
723 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
724 1.1 jmcneill if (sunxi_mmc_update_clock(sc) != 0)
725 1.1 jmcneill return 1;
726 1.7 jmcneill if (flags & SUNXI_MMC_CLKCR_MASK_DATA0) {
727 1.7 jmcneill clkcr = MMC_READ(sc, SUNXI_MMC_CLKCR);
728 1.7 jmcneill clkcr &= ~SUNXI_MMC_CLKCR_MASK_DATA0;
729 1.7 jmcneill MMC_WRITE(sc, SUNXI_MMC_CLKCR, clkcr);
730 1.7 jmcneill }
731 1.1 jmcneill }
732 1.1 jmcneill
733 1.1 jmcneill return 0;
734 1.1 jmcneill }
735 1.1 jmcneill
736 1.1 jmcneill static int
737 1.1 jmcneill sunxi_mmc_bus_width(sdmmc_chipset_handle_t sch, int width)
738 1.1 jmcneill {
739 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
740 1.1 jmcneill
741 1.13 jmcneill DPRINTF(sc->sc_dev, "width = %d\n", width);
742 1.1 jmcneill
743 1.1 jmcneill switch (width) {
744 1.1 jmcneill case 1:
745 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_WIDTH, SUNXI_MMC_WIDTH_1);
746 1.1 jmcneill break;
747 1.1 jmcneill case 4:
748 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_WIDTH, SUNXI_MMC_WIDTH_4);
749 1.1 jmcneill break;
750 1.1 jmcneill case 8:
751 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_WIDTH, SUNXI_MMC_WIDTH_8);
752 1.1 jmcneill break;
753 1.1 jmcneill default:
754 1.1 jmcneill return 1;
755 1.1 jmcneill }
756 1.1 jmcneill
757 1.1 jmcneill sc->sc_mmc_width = width;
758 1.1 jmcneill
759 1.1 jmcneill return 0;
760 1.1 jmcneill }
761 1.1 jmcneill
762 1.1 jmcneill static int
763 1.1 jmcneill sunxi_mmc_bus_rod(sdmmc_chipset_handle_t sch, int on)
764 1.1 jmcneill {
765 1.1 jmcneill return -1;
766 1.1 jmcneill }
767 1.1 jmcneill
768 1.1 jmcneill static int
769 1.3 jmcneill sunxi_mmc_signal_voltage(sdmmc_chipset_handle_t sch, int signal_voltage)
770 1.3 jmcneill {
771 1.3 jmcneill struct sunxi_mmc_softc *sc = sch;
772 1.3 jmcneill u_int uvol;
773 1.3 jmcneill int error;
774 1.3 jmcneill
775 1.3 jmcneill if (sc->sc_reg_vqmmc == NULL)
776 1.3 jmcneill return 0;
777 1.3 jmcneill
778 1.3 jmcneill switch (signal_voltage) {
779 1.3 jmcneill case SDMMC_SIGNAL_VOLTAGE_330:
780 1.3 jmcneill uvol = 3300000;
781 1.3 jmcneill break;
782 1.3 jmcneill case SDMMC_SIGNAL_VOLTAGE_180:
783 1.3 jmcneill uvol = 1800000;
784 1.3 jmcneill break;
785 1.3 jmcneill default:
786 1.3 jmcneill return EINVAL;
787 1.3 jmcneill }
788 1.3 jmcneill
789 1.3 jmcneill error = fdtbus_regulator_set_voltage(sc->sc_reg_vqmmc, uvol, uvol);
790 1.3 jmcneill if (error != 0)
791 1.3 jmcneill return error;
792 1.3 jmcneill
793 1.3 jmcneill return fdtbus_regulator_enable(sc->sc_reg_vqmmc);
794 1.3 jmcneill }
795 1.3 jmcneill
796 1.3 jmcneill static int
797 1.1 jmcneill sunxi_mmc_dma_prepare(struct sunxi_mmc_softc *sc, struct sdmmc_command *cmd)
798 1.1 jmcneill {
799 1.1 jmcneill struct sunxi_mmc_idma_descriptor *dma = sc->sc_idma_desc;
800 1.1 jmcneill bus_addr_t desc_paddr = sc->sc_idma_map->dm_segs[0].ds_addr;
801 1.1 jmcneill bus_size_t off;
802 1.1 jmcneill int desc, resid, seg;
803 1.1 jmcneill uint32_t val;
804 1.1 jmcneill
805 1.1 jmcneill desc = 0;
806 1.1 jmcneill for (seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
807 1.1 jmcneill bus_addr_t paddr = cmd->c_dmamap->dm_segs[seg].ds_addr;
808 1.1 jmcneill bus_size_t len = cmd->c_dmamap->dm_segs[seg].ds_len;
809 1.1 jmcneill resid = min(len, cmd->c_resid);
810 1.1 jmcneill off = 0;
811 1.1 jmcneill while (resid > 0) {
812 1.1 jmcneill if (desc == sc->sc_idma_ndesc)
813 1.1 jmcneill break;
814 1.7 jmcneill len = min(sc->sc_config->idma_xferlen, resid);
815 1.1 jmcneill dma[desc].dma_buf_size = htole32(len);
816 1.1 jmcneill dma[desc].dma_buf_addr = htole32(paddr + off);
817 1.1 jmcneill dma[desc].dma_config = htole32(SUNXI_MMC_IDMA_CONFIG_CH |
818 1.1 jmcneill SUNXI_MMC_IDMA_CONFIG_OWN);
819 1.1 jmcneill cmd->c_resid -= len;
820 1.1 jmcneill resid -= len;
821 1.1 jmcneill off += len;
822 1.1 jmcneill if (desc == 0) {
823 1.1 jmcneill dma[desc].dma_config |= htole32(SUNXI_MMC_IDMA_CONFIG_FD);
824 1.1 jmcneill }
825 1.1 jmcneill if (cmd->c_resid == 0) {
826 1.1 jmcneill dma[desc].dma_config |= htole32(SUNXI_MMC_IDMA_CONFIG_LD);
827 1.1 jmcneill dma[desc].dma_config |= htole32(SUNXI_MMC_IDMA_CONFIG_ER);
828 1.1 jmcneill dma[desc].dma_next = 0;
829 1.1 jmcneill } else {
830 1.1 jmcneill dma[desc].dma_config |=
831 1.1 jmcneill htole32(SUNXI_MMC_IDMA_CONFIG_DIC);
832 1.1 jmcneill dma[desc].dma_next = htole32(
833 1.1 jmcneill desc_paddr + ((desc+1) *
834 1.1 jmcneill sizeof(struct sunxi_mmc_idma_descriptor)));
835 1.1 jmcneill }
836 1.1 jmcneill ++desc;
837 1.1 jmcneill }
838 1.1 jmcneill }
839 1.1 jmcneill if (desc == sc->sc_idma_ndesc) {
840 1.1 jmcneill aprint_error_dev(sc->sc_dev,
841 1.1 jmcneill "not enough descriptors for %d byte transfer!\n",
842 1.1 jmcneill cmd->c_datalen);
843 1.1 jmcneill return EIO;
844 1.1 jmcneill }
845 1.1 jmcneill
846 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_idma_map, 0,
847 1.1 jmcneill sc->sc_idma_size, BUS_DMASYNC_PREWRITE);
848 1.1 jmcneill
849 1.1 jmcneill sc->sc_idma_idst = 0;
850 1.1 jmcneill
851 1.1 jmcneill val = MMC_READ(sc, SUNXI_MMC_GCTRL);
852 1.1 jmcneill val |= SUNXI_MMC_GCTRL_DMAEN;
853 1.1 jmcneill val |= SUNXI_MMC_GCTRL_INTEN;
854 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL, val);
855 1.1 jmcneill val |= SUNXI_MMC_GCTRL_DMARESET;
856 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL, val);
857 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_DMAC, SUNXI_MMC_DMAC_SOFTRESET);
858 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_DMAC,
859 1.1 jmcneill SUNXI_MMC_DMAC_IDMA_ON|SUNXI_MMC_DMAC_FIX_BURST);
860 1.1 jmcneill val = MMC_READ(sc, SUNXI_MMC_IDIE);
861 1.1 jmcneill val &= ~(SUNXI_MMC_IDST_RECEIVE_INT|SUNXI_MMC_IDST_TRANSMIT_INT);
862 1.1 jmcneill if (cmd->c_flags & SCF_CMD_READ)
863 1.1 jmcneill val |= SUNXI_MMC_IDST_RECEIVE_INT;
864 1.1 jmcneill else
865 1.1 jmcneill val |= SUNXI_MMC_IDST_TRANSMIT_INT;
866 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_IDIE, val);
867 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_DLBA, desc_paddr);
868 1.7 jmcneill MMC_WRITE(sc, SUNXI_MMC_FTRGLEVEL, sc->sc_config->dma_ftrglevel);
869 1.1 jmcneill
870 1.1 jmcneill return 0;
871 1.1 jmcneill }
872 1.1 jmcneill
873 1.1 jmcneill static void
874 1.1 jmcneill sunxi_mmc_dma_complete(struct sunxi_mmc_softc *sc)
875 1.1 jmcneill {
876 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_idma_map, 0,
877 1.1 jmcneill sc->sc_idma_size, BUS_DMASYNC_POSTWRITE);
878 1.1 jmcneill }
879 1.1 jmcneill
880 1.1 jmcneill static void
881 1.1 jmcneill sunxi_mmc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
882 1.1 jmcneill {
883 1.1 jmcneill struct sunxi_mmc_softc *sc = sch;
884 1.1 jmcneill uint32_t cmdval = SUNXI_MMC_CMD_START;
885 1.2 jmcneill const bool poll = (cmd->c_flags & SCF_POLL) != 0;
886 1.1 jmcneill int retry;
887 1.1 jmcneill
888 1.13 jmcneill DPRINTF(sc->sc_dev,
889 1.2 jmcneill "opcode %d flags 0x%x data %p datalen %d blklen %d poll %d\n",
890 1.1 jmcneill cmd->c_opcode, cmd->c_flags, cmd->c_data, cmd->c_datalen,
891 1.2 jmcneill cmd->c_blklen, poll);
892 1.1 jmcneill
893 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
894 1.1 jmcneill
895 1.1 jmcneill if (cmd->c_opcode == 0)
896 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
897 1.1 jmcneill if (cmd->c_flags & SCF_RSP_PRESENT)
898 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_RSP_EXP;
899 1.1 jmcneill if (cmd->c_flags & SCF_RSP_136)
900 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_LONG_RSP;
901 1.1 jmcneill if (cmd->c_flags & SCF_RSP_CRC)
902 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_CHECK_RSP_CRC;
903 1.1 jmcneill
904 1.1 jmcneill if (cmd->c_datalen > 0) {
905 1.1 jmcneill unsigned int nblks;
906 1.1 jmcneill
907 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_DATA_EXP | SUNXI_MMC_CMD_WAIT_PRE_OVER;
908 1.1 jmcneill if (!ISSET(cmd->c_flags, SCF_CMD_READ)) {
909 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_WRITE;
910 1.1 jmcneill }
911 1.1 jmcneill
912 1.1 jmcneill nblks = cmd->c_datalen / cmd->c_blklen;
913 1.1 jmcneill if (nblks == 0 || (cmd->c_datalen % cmd->c_blklen) != 0)
914 1.1 jmcneill ++nblks;
915 1.1 jmcneill
916 1.1 jmcneill if (nblks > 1) {
917 1.1 jmcneill cmdval |= SUNXI_MMC_CMD_SEND_AUTO_STOP;
918 1.1 jmcneill }
919 1.1 jmcneill
920 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_BLKSZ, cmd->c_blklen);
921 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_BYTECNT, nblks * cmd->c_blklen);
922 1.1 jmcneill }
923 1.1 jmcneill
924 1.1 jmcneill sc->sc_intr_rint = 0;
925 1.1 jmcneill
926 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_A12A,
927 1.1 jmcneill (cmdval & SUNXI_MMC_CMD_SEND_AUTO_STOP) ? 0 : 0xffff);
928 1.1 jmcneill
929 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_ARG, cmd->c_arg);
930 1.1 jmcneill
931 1.13 jmcneill DPRINTF(sc->sc_dev, "cmdval = %08x\n", cmdval);
932 1.1 jmcneill
933 1.1 jmcneill if (cmd->c_datalen == 0) {
934 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CMD, cmdval | cmd->c_opcode);
935 1.1 jmcneill } else {
936 1.1 jmcneill cmd->c_resid = cmd->c_datalen;
937 1.1 jmcneill cmd->c_error = sunxi_mmc_dma_prepare(sc, cmd);
938 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_CMD, cmdval | cmd->c_opcode);
939 1.1 jmcneill if (cmd->c_error == 0) {
940 1.1 jmcneill const uint32_t idst_mask =
941 1.1 jmcneill SUNXI_MMC_IDST_ERROR | SUNXI_MMC_IDST_COMPLETE;
942 1.1 jmcneill retry = 10;
943 1.1 jmcneill while ((sc->sc_idma_idst & idst_mask) == 0) {
944 1.1 jmcneill if (retry-- == 0) {
945 1.1 jmcneill cmd->c_error = ETIMEDOUT;
946 1.1 jmcneill break;
947 1.1 jmcneill }
948 1.1 jmcneill cv_timedwait(&sc->sc_idst_cv,
949 1.1 jmcneill &sc->sc_intr_lock, hz);
950 1.1 jmcneill }
951 1.1 jmcneill }
952 1.1 jmcneill sunxi_mmc_dma_complete(sc);
953 1.1 jmcneill if (sc->sc_idma_idst & SUNXI_MMC_IDST_ERROR) {
954 1.1 jmcneill cmd->c_error = EIO;
955 1.1 jmcneill } else if (!(sc->sc_idma_idst & SUNXI_MMC_IDST_COMPLETE)) {
956 1.1 jmcneill cmd->c_error = ETIMEDOUT;
957 1.1 jmcneill }
958 1.1 jmcneill if (cmd->c_error) {
959 1.13 jmcneill DPRINTF(sc->sc_dev,
960 1.1 jmcneill "xfer failed, error %d\n", cmd->c_error);
961 1.1 jmcneill goto done;
962 1.1 jmcneill }
963 1.1 jmcneill }
964 1.1 jmcneill
965 1.1 jmcneill cmd->c_error = sunxi_mmc_wait_rint(sc,
966 1.2 jmcneill SUNXI_MMC_INT_ERROR|SUNXI_MMC_INT_CMD_DONE, hz * 10, poll);
967 1.1 jmcneill if (cmd->c_error == 0 && (sc->sc_intr_rint & SUNXI_MMC_INT_ERROR)) {
968 1.1 jmcneill if (sc->sc_intr_rint & SUNXI_MMC_INT_RESP_TIMEOUT) {
969 1.1 jmcneill cmd->c_error = ETIMEDOUT;
970 1.1 jmcneill } else {
971 1.1 jmcneill cmd->c_error = EIO;
972 1.1 jmcneill }
973 1.1 jmcneill }
974 1.1 jmcneill if (cmd->c_error) {
975 1.13 jmcneill DPRINTF(sc->sc_dev,
976 1.1 jmcneill "cmd failed, error %d\n", cmd->c_error);
977 1.1 jmcneill goto done;
978 1.1 jmcneill }
979 1.1 jmcneill
980 1.1 jmcneill if (cmd->c_datalen > 0) {
981 1.1 jmcneill cmd->c_error = sunxi_mmc_wait_rint(sc,
982 1.1 jmcneill SUNXI_MMC_INT_ERROR|
983 1.1 jmcneill SUNXI_MMC_INT_AUTO_CMD_DONE|
984 1.1 jmcneill SUNXI_MMC_INT_DATA_OVER,
985 1.2 jmcneill hz*10, poll);
986 1.1 jmcneill if (cmd->c_error == 0 &&
987 1.1 jmcneill (sc->sc_intr_rint & SUNXI_MMC_INT_ERROR)) {
988 1.1 jmcneill cmd->c_error = ETIMEDOUT;
989 1.1 jmcneill }
990 1.1 jmcneill if (cmd->c_error) {
991 1.13 jmcneill DPRINTF(sc->sc_dev,
992 1.1 jmcneill "data timeout, rint = %08x\n",
993 1.1 jmcneill sc->sc_intr_rint);
994 1.1 jmcneill cmd->c_error = ETIMEDOUT;
995 1.1 jmcneill goto done;
996 1.1 jmcneill }
997 1.1 jmcneill }
998 1.1 jmcneill
999 1.1 jmcneill if (cmd->c_flags & SCF_RSP_PRESENT) {
1000 1.1 jmcneill if (cmd->c_flags & SCF_RSP_136) {
1001 1.1 jmcneill cmd->c_resp[0] = MMC_READ(sc, SUNXI_MMC_RESP0);
1002 1.1 jmcneill cmd->c_resp[1] = MMC_READ(sc, SUNXI_MMC_RESP1);
1003 1.1 jmcneill cmd->c_resp[2] = MMC_READ(sc, SUNXI_MMC_RESP2);
1004 1.1 jmcneill cmd->c_resp[3] = MMC_READ(sc, SUNXI_MMC_RESP3);
1005 1.1 jmcneill if (cmd->c_flags & SCF_RSP_CRC) {
1006 1.1 jmcneill cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
1007 1.1 jmcneill (cmd->c_resp[1] << 24);
1008 1.1 jmcneill cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
1009 1.1 jmcneill (cmd->c_resp[2] << 24);
1010 1.1 jmcneill cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
1011 1.1 jmcneill (cmd->c_resp[3] << 24);
1012 1.1 jmcneill cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
1013 1.1 jmcneill }
1014 1.1 jmcneill } else {
1015 1.1 jmcneill cmd->c_resp[0] = MMC_READ(sc, SUNXI_MMC_RESP0);
1016 1.1 jmcneill }
1017 1.1 jmcneill }
1018 1.1 jmcneill
1019 1.1 jmcneill done:
1020 1.1 jmcneill cmd->c_flags |= SCF_ITSDONE;
1021 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
1022 1.1 jmcneill
1023 1.1 jmcneill if (cmd->c_error) {
1024 1.13 jmcneill DPRINTF(sc->sc_dev, "i/o error %d\n", cmd->c_error);
1025 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL,
1026 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_GCTRL) |
1027 1.1 jmcneill SUNXI_MMC_GCTRL_DMARESET | SUNXI_MMC_GCTRL_FIFORESET);
1028 1.1 jmcneill for (retry = 0; retry < 1000; retry++) {
1029 1.1 jmcneill if (!(MMC_READ(sc, SUNXI_MMC_GCTRL) & SUNXI_MMC_GCTRL_RESET))
1030 1.1 jmcneill break;
1031 1.1 jmcneill delay(10);
1032 1.1 jmcneill }
1033 1.1 jmcneill sunxi_mmc_update_clock(sc);
1034 1.1 jmcneill }
1035 1.1 jmcneill
1036 1.1 jmcneill MMC_WRITE(sc, SUNXI_MMC_GCTRL,
1037 1.1 jmcneill MMC_READ(sc, SUNXI_MMC_GCTRL) | SUNXI_MMC_GCTRL_FIFORESET);
1038 1.1 jmcneill }
1039 1.1 jmcneill
1040 1.1 jmcneill static void
1041 1.1 jmcneill sunxi_mmc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
1042 1.1 jmcneill {
1043 1.11 jmcneill struct sunxi_mmc_softc *sc = sch;
1044 1.11 jmcneill uint32_t imask;
1045 1.11 jmcneill
1046 1.11 jmcneill imask = MMC_READ(sc, SUNXI_MMC_IMASK);
1047 1.11 jmcneill if (enable)
1048 1.11 jmcneill imask |= SUNXI_MMC_INT_SDIO_INT;
1049 1.11 jmcneill else
1050 1.11 jmcneill imask &= ~SUNXI_MMC_INT_SDIO_INT;
1051 1.11 jmcneill MMC_WRITE(sc, SUNXI_MMC_IMASK, imask);
1052 1.1 jmcneill }
1053 1.1 jmcneill
1054 1.1 jmcneill static void
1055 1.1 jmcneill sunxi_mmc_card_intr_ack(sdmmc_chipset_handle_t sch)
1056 1.1 jmcneill {
1057 1.11 jmcneill struct sunxi_mmc_softc *sc = sch;
1058 1.11 jmcneill
1059 1.11 jmcneill MMC_WRITE(sc, SUNXI_MMC_RINT, SUNXI_MMC_INT_SDIO_INT);
1060 1.1 jmcneill }
1061