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