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