sdhc.c revision 1.7.2.6 1 1.7.2.6 matt /* $NetBSD: sdhc.c,v 1.7.2.6 2014/02/15 03:33:40 matt Exp $ */
2 1.7.2.2 matt /* $OpenBSD: sdhc.c,v 1.25 2009/01/13 19:44:20 grange Exp $ */
3 1.7.2.2 matt
4 1.7.2.2 matt /*
5 1.7.2.2 matt * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
6 1.7.2.2 matt *
7 1.7.2.2 matt * Permission to use, copy, modify, and distribute this software for any
8 1.7.2.2 matt * purpose with or without fee is hereby granted, provided that the above
9 1.7.2.2 matt * copyright notice and this permission notice appear in all copies.
10 1.7.2.2 matt *
11 1.7.2.2 matt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.7.2.2 matt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.7.2.2 matt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.7.2.2 matt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.7.2.2 matt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.7.2.2 matt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.7.2.2 matt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.7.2.2 matt */
19 1.7.2.2 matt
20 1.7.2.2 matt /*
21 1.7.2.2 matt * SD Host Controller driver based on the SD Host Controller Standard
22 1.7.2.2 matt * Simplified Specification Version 1.00 (www.sdcard.com).
23 1.7.2.2 matt */
24 1.7.2.2 matt
25 1.7.2.2 matt #include <sys/cdefs.h>
26 1.7.2.6 matt __KERNEL_RCSID(0, "$NetBSD: sdhc.c,v 1.7.2.6 2014/02/15 03:33:40 matt Exp $");
27 1.7.2.2 matt
28 1.7.2.2 matt #include <sys/param.h>
29 1.7.2.2 matt #include <sys/device.h>
30 1.7.2.2 matt #include <sys/kernel.h>
31 1.7.2.2 matt #include <sys/kthread.h>
32 1.7.2.2 matt #include <sys/malloc.h>
33 1.7.2.2 matt #include <sys/systm.h>
34 1.7.2.2 matt #include <sys/mutex.h>
35 1.7.2.2 matt #include <sys/condvar.h>
36 1.7.2.2 matt
37 1.7.2.2 matt #include <dev/sdmmc/sdhcreg.h>
38 1.7.2.2 matt #include <dev/sdmmc/sdhcvar.h>
39 1.7.2.2 matt #include <dev/sdmmc/sdmmcchip.h>
40 1.7.2.2 matt #include <dev/sdmmc/sdmmcreg.h>
41 1.7.2.2 matt #include <dev/sdmmc/sdmmcvar.h>
42 1.7.2.2 matt
43 1.7.2.2 matt #ifdef SDHC_DEBUG
44 1.7.2.5 matt int sdhcdebug = 0;
45 1.7.2.2 matt #define DPRINTF(n,s) do { if ((n) <= sdhcdebug) printf s; } while (0)
46 1.7.2.2 matt void sdhc_dump_regs(struct sdhc_host *);
47 1.7.2.2 matt #else
48 1.7.2.2 matt #define DPRINTF(n,s) do {} while (0)
49 1.7.2.2 matt #endif
50 1.7.2.2 matt
51 1.7.2.2 matt #define SDHC_COMMAND_TIMEOUT hz
52 1.7.2.2 matt #define SDHC_BUFFER_TIMEOUT hz
53 1.7.2.2 matt #define SDHC_TRANSFER_TIMEOUT hz
54 1.7.2.2 matt #define SDHC_DMA_TIMEOUT hz
55 1.7.2.2 matt
56 1.7.2.2 matt struct sdhc_host {
57 1.7.2.2 matt struct sdhc_softc *sc; /* host controller device */
58 1.7.2.2 matt
59 1.7.2.2 matt bus_space_tag_t iot; /* host register set tag */
60 1.7.2.2 matt bus_space_handle_t ioh; /* host register set handle */
61 1.7.2.2 matt bus_dma_tag_t dmat; /* host DMA tag */
62 1.7.2.2 matt
63 1.7.2.2 matt device_t sdmmc; /* generic SD/MMC device */
64 1.7.2.2 matt
65 1.7.2.2 matt struct kmutex host_mtx;
66 1.7.2.2 matt
67 1.7.2.2 matt u_int clkbase; /* base clock frequency in KHz */
68 1.7.2.2 matt int maxblklen; /* maximum block length */
69 1.7.2.2 matt uint32_t ocr; /* OCR value from capabilities */
70 1.7.2.2 matt
71 1.7.2.2 matt uint8_t regs[14]; /* host controller state */
72 1.7.2.2 matt
73 1.7.2.2 matt uint16_t intr_status; /* soft interrupt status */
74 1.7.2.2 matt uint16_t intr_error_status; /* soft error status */
75 1.7.2.2 matt struct kmutex intr_mtx;
76 1.7.2.2 matt struct kcondvar intr_cv;
77 1.7.2.2 matt
78 1.7.2.2 matt uint32_t flags; /* flags for this host */
79 1.7.2.2 matt #define SHF_USE_DMA 0x0001
80 1.7.2.2 matt #define SHF_USE_4BIT_MODE 0x0002
81 1.7.2.2 matt };
82 1.7.2.2 matt
83 1.7.2.2 matt #define HDEVNAME(hp) (device_xname((hp)->sc->sc_dev))
84 1.7.2.2 matt
85 1.7.2.2 matt #define HREAD1(hp, reg) \
86 1.7.2.2 matt (bus_space_read_1((hp)->iot, (hp)->ioh, (reg)))
87 1.7.2.2 matt #define HREAD2(hp, reg) \
88 1.7.2.2 matt (bus_space_read_2((hp)->iot, (hp)->ioh, (reg)))
89 1.7.2.2 matt #define HREAD4(hp, reg) \
90 1.7.2.2 matt (bus_space_read_4((hp)->iot, (hp)->ioh, (reg)))
91 1.7.2.2 matt #define HWRITE1(hp, reg, val) \
92 1.7.2.2 matt bus_space_write_1((hp)->iot, (hp)->ioh, (reg), (val))
93 1.7.2.2 matt #define HWRITE2(hp, reg, val) \
94 1.7.2.2 matt bus_space_write_2((hp)->iot, (hp)->ioh, (reg), (val))
95 1.7.2.2 matt #define HWRITE4(hp, reg, val) \
96 1.7.2.2 matt bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val))
97 1.7.2.2 matt #define HCLR1(hp, reg, bits) \
98 1.7.2.2 matt HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits))
99 1.7.2.2 matt #define HCLR2(hp, reg, bits) \
100 1.7.2.2 matt HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits))
101 1.7.2.2 matt #define HSET1(hp, reg, bits) \
102 1.7.2.2 matt HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits))
103 1.7.2.2 matt #define HSET2(hp, reg, bits) \
104 1.7.2.2 matt HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits))
105 1.7.2.2 matt
106 1.7.2.2 matt static int sdhc_host_reset(sdmmc_chipset_handle_t);
107 1.7.2.2 matt static int sdhc_host_reset1(sdmmc_chipset_handle_t);
108 1.7.2.2 matt static uint32_t sdhc_host_ocr(sdmmc_chipset_handle_t);
109 1.7.2.2 matt static int sdhc_host_maxblklen(sdmmc_chipset_handle_t);
110 1.7.2.2 matt static int sdhc_card_detect(sdmmc_chipset_handle_t);
111 1.7.2.2 matt static int sdhc_write_protect(sdmmc_chipset_handle_t);
112 1.7.2.2 matt static int sdhc_bus_power(sdmmc_chipset_handle_t, uint32_t);
113 1.7.2.2 matt static int sdhc_bus_clock(sdmmc_chipset_handle_t, int);
114 1.7.2.2 matt static int sdhc_bus_width(sdmmc_chipset_handle_t, int);
115 1.7.2.2 matt static void sdhc_card_enable_intr(sdmmc_chipset_handle_t, int);
116 1.7.2.2 matt static void sdhc_card_intr_ack(sdmmc_chipset_handle_t);
117 1.7.2.2 matt static void sdhc_exec_command(sdmmc_chipset_handle_t,
118 1.7.2.2 matt struct sdmmc_command *);
119 1.7.2.2 matt static int sdhc_start_command(struct sdhc_host *, struct sdmmc_command *);
120 1.7.2.2 matt static int sdhc_wait_state(struct sdhc_host *, uint32_t, uint32_t);
121 1.7.2.2 matt static int sdhc_soft_reset(struct sdhc_host *, int);
122 1.7.2.2 matt static int sdhc_wait_intr(struct sdhc_host *, int, int);
123 1.7.2.2 matt static void sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *);
124 1.7.2.2 matt static int sdhc_transfer_data_pio(struct sdhc_host *, struct sdmmc_command *);
125 1.7.2.2 matt static void sdhc_read_data_pio(struct sdhc_host *, uint8_t *, int);
126 1.7.2.2 matt static void sdhc_write_data_pio(struct sdhc_host *, uint8_t *, int);
127 1.7.2.2 matt
128 1.7.2.2 matt static struct sdmmc_chip_functions sdhc_functions = {
129 1.7.2.2 matt /* host controller reset */
130 1.7.2.2 matt sdhc_host_reset,
131 1.7.2.2 matt
132 1.7.2.2 matt /* host controller capabilities */
133 1.7.2.2 matt sdhc_host_ocr,
134 1.7.2.2 matt sdhc_host_maxblklen,
135 1.7.2.2 matt
136 1.7.2.2 matt /* card detection */
137 1.7.2.2 matt sdhc_card_detect,
138 1.7.2.2 matt
139 1.7.2.2 matt /* write protect */
140 1.7.2.2 matt sdhc_write_protect,
141 1.7.2.2 matt
142 1.7.2.2 matt /* bus power, clock frequency and width */
143 1.7.2.2 matt sdhc_bus_power,
144 1.7.2.2 matt sdhc_bus_clock,
145 1.7.2.2 matt sdhc_bus_width,
146 1.7.2.2 matt
147 1.7.2.2 matt /* command execution */
148 1.7.2.2 matt sdhc_exec_command,
149 1.7.2.2 matt
150 1.7.2.2 matt /* card interrupt */
151 1.7.2.2 matt sdhc_card_enable_intr,
152 1.7.2.2 matt sdhc_card_intr_ack
153 1.7.2.2 matt };
154 1.7.2.2 matt
155 1.7.2.2 matt /*
156 1.7.2.2 matt * Called by attachment driver. For each SD card slot there is one SD
157 1.7.2.2 matt * host controller standard register set. (1.3)
158 1.7.2.2 matt */
159 1.7.2.2 matt int
160 1.7.2.2 matt sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot,
161 1.7.2.2 matt bus_space_handle_t ioh, bus_size_t iosize)
162 1.7.2.2 matt {
163 1.7.2.2 matt struct sdmmcbus_attach_args saa;
164 1.7.2.2 matt struct sdhc_host *hp;
165 1.7.2.2 matt uint32_t caps;
166 1.7.2.2 matt #ifdef SDHC_DEBUG
167 1.7.2.2 matt uint16_t sdhcver;
168 1.7.2.2 matt
169 1.7.2.2 matt sdhcver = bus_space_read_2(iot, ioh, SDHC_HOST_CTL_VERSION);
170 1.7.2.2 matt aprint_normal_dev(sc->sc_dev, "SD Host Specification/Vendor Version ");
171 1.7.2.2 matt switch (SDHC_SPEC_VERSION(sdhcver)) {
172 1.7.2.2 matt case 0x00:
173 1.7.2.2 matt aprint_normal("1.0/%u\n", SDHC_VENDOR_VERSION(sdhcver));
174 1.7.2.2 matt break;
175 1.7.2.2 matt
176 1.7.2.2 matt default:
177 1.7.2.2 matt aprint_normal(">1.0/%u\n", SDHC_VENDOR_VERSION(sdhcver));
178 1.7.2.2 matt break;
179 1.7.2.2 matt }
180 1.7.2.2 matt #endif
181 1.7.2.2 matt
182 1.7.2.2 matt /* Allocate one more host structure. */
183 1.7.2.2 matt hp = malloc(sizeof(struct sdhc_host), M_DEVBUF, M_WAITOK|M_ZERO);
184 1.7.2.2 matt if (hp == NULL) {
185 1.7.2.2 matt aprint_error_dev(sc->sc_dev,
186 1.7.2.2 matt "couldn't alloc memory (sdhc host)\n");
187 1.7.2.2 matt goto err1;
188 1.7.2.2 matt }
189 1.7.2.2 matt sc->sc_host[sc->sc_nhosts++] = hp;
190 1.7.2.2 matt
191 1.7.2.2 matt /* Fill in the new host structure. */
192 1.7.2.2 matt hp->sc = sc;
193 1.7.2.2 matt hp->iot = iot;
194 1.7.2.2 matt hp->ioh = ioh;
195 1.7.2.2 matt hp->dmat = sc->sc_dmat;
196 1.7.2.2 matt
197 1.7.2.2 matt mutex_init(&hp->host_mtx, MUTEX_DEFAULT, IPL_SDMMC);
198 1.7.2.2 matt mutex_init(&hp->intr_mtx, MUTEX_DEFAULT, IPL_SDMMC);
199 1.7.2.2 matt cv_init(&hp->intr_cv, "sdhcintr");
200 1.7.2.2 matt
201 1.7.2.2 matt /*
202 1.7.2.2 matt * Reset the host controller and enable interrupts.
203 1.7.2.2 matt */
204 1.7.2.2 matt (void)sdhc_host_reset(hp);
205 1.7.2.2 matt
206 1.7.2.2 matt /* Determine host capabilities. */
207 1.7.2.2 matt mutex_enter(&hp->host_mtx);
208 1.7.2.2 matt caps = HREAD4(hp, SDHC_CAPABILITIES);
209 1.7.2.2 matt mutex_exit(&hp->host_mtx);
210 1.7.2.2 matt
211 1.7.2.3 matt DPRINTF(1,("%s: caps=0x%08x\n", device_xname(sc->sc_dev), caps));
212 1.7.2.3 matt
213 1.7.2.2 matt #if notyet
214 1.7.2.2 matt /* Use DMA if the host system and the controller support it. */
215 1.7.2.2 matt if (ISSET(sc->sc_flags, SDHC_FLAG_FORCE_DMA)
216 1.7.2.2 matt || ((ISSET(sc->sc_flags, SDHC_FLAG_USE_DMA)
217 1.7.2.2 matt && ISSET(caps, SDHC_DMA_SUPPORT)))) {
218 1.7.2.2 matt SET(hp->flags, SHF_USE_DMA);
219 1.7.2.2 matt aprint_normal_dev(sc->sc_dev, "using DMA transfer\n");
220 1.7.2.2 matt }
221 1.7.2.2 matt #endif
222 1.7.2.2 matt
223 1.7.2.2 matt /*
224 1.7.2.2 matt * Determine the base clock frequency. (2.2.24)
225 1.7.2.2 matt */
226 1.7.2.2 matt if (SDHC_BASE_FREQ_KHZ(caps) != 0)
227 1.7.2.2 matt hp->clkbase = SDHC_BASE_FREQ_KHZ(caps);
228 1.7.2.2 matt if (hp->clkbase == 0) {
229 1.7.2.4 matt if (sc->sc_clkbase == 0) {
230 1.7.2.4 matt /* The attachment driver must tell us. */
231 1.7.2.4 matt aprint_error_dev(sc->sc_dev,"unknown base clock frequency\n");
232 1.7.2.4 matt goto err;
233 1.7.2.4 matt }
234 1.7.2.4 matt hp->clkbase = sc->sc_clkbase;
235 1.7.2.4 matt }
236 1.7.2.4 matt if (hp->clkbase < 10000 || hp->clkbase > 10000 * 256) {
237 1.7.2.2 matt /* SDHC 1.0 supports only 10-63 MHz. */
238 1.7.2.3 matt /* SDHC 1.0 supports only 10-255 MHz. */
239 1.7.2.2 matt aprint_error_dev(sc->sc_dev,
240 1.7.2.2 matt "base clock frequency out of range: %u MHz\n",
241 1.7.2.2 matt hp->clkbase / 1000);
242 1.7.2.2 matt goto err;
243 1.7.2.2 matt }
244 1.7.2.2 matt DPRINTF(1,("%s: base clock frequency %u MHz\n",
245 1.7.2.2 matt device_xname(sc->sc_dev), hp->clkbase / 1000));
246 1.7.2.2 matt
247 1.7.2.2 matt /*
248 1.7.2.2 matt * XXX Set the data timeout counter value according to
249 1.7.2.2 matt * capabilities. (2.2.15)
250 1.7.2.2 matt */
251 1.7.2.2 matt HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
252 1.7.2.2 matt
253 1.7.2.2 matt /*
254 1.7.2.2 matt * Determine SD bus voltage levels supported by the controller.
255 1.7.2.2 matt */
256 1.7.2.2 matt if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V))
257 1.7.2.2 matt SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V);
258 1.7.2.2 matt if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V))
259 1.7.2.2 matt SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V);
260 1.7.2.2 matt if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V))
261 1.7.2.2 matt SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V);
262 1.7.2.2 matt
263 1.7.2.2 matt /*
264 1.7.2.2 matt * Determine the maximum block length supported by the host
265 1.7.2.2 matt * controller. (2.2.24)
266 1.7.2.2 matt */
267 1.7.2.2 matt switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) {
268 1.7.2.2 matt case SDHC_MAX_BLK_LEN_512:
269 1.7.2.2 matt hp->maxblklen = 512;
270 1.7.2.2 matt break;
271 1.7.2.2 matt
272 1.7.2.2 matt case SDHC_MAX_BLK_LEN_1024:
273 1.7.2.2 matt hp->maxblklen = 1024;
274 1.7.2.2 matt break;
275 1.7.2.2 matt
276 1.7.2.2 matt case SDHC_MAX_BLK_LEN_2048:
277 1.7.2.2 matt hp->maxblklen = 2048;
278 1.7.2.2 matt break;
279 1.7.2.2 matt
280 1.7.2.3 matt case SDHC_MAX_BLK_LEN_4096:
281 1.7.2.3 matt hp->maxblklen = 4096;
282 1.7.2.3 matt break;
283 1.7.2.3 matt
284 1.7.2.2 matt default:
285 1.7.2.2 matt aprint_error_dev(sc->sc_dev, "max block length unknown\n");
286 1.7.2.2 matt goto err;
287 1.7.2.2 matt }
288 1.7.2.2 matt DPRINTF(1, ("%s: max block length %u byte%s\n",
289 1.7.2.2 matt device_xname(sc->sc_dev), hp->maxblklen,
290 1.7.2.2 matt hp->maxblklen > 1 ? "s" : ""));
291 1.7.2.2 matt
292 1.7.2.3 matt #if 0
293 1.7.2.6 matt if (sc->sc_flags & SDHC_FLAG_HAVE_CGM) {
294 1.7.2.3 matt uint16_t clk = HREAD2(hp, SDHC_CLOCK_CTL);
295 1.7.2.3 matt clk |= SDHC_SDCLK_CGM;
296 1.7.2.3 matt HWRITE2(hp, SDHC_CLOCK_CTL, clk);
297 1.7.2.3 matt clk = HREAD2(hp, SDHC_CLOCK_CTL);
298 1.7.2.3 matt if ((clk & SDHC_SDCLK_CGM) == 0) {
299 1.7.2.3 matt sc->sc_flags &= ~SDHC_FLAG_HAS_CGM;
300 1.7.2.3 matt DPRINTF(1, ("%s: CGM indicated but not supported\n",
301 1.7.2.3 matt device_xname(sc->sc_dev)));
302 1.7.2.3 matt }
303 1.7.2.3 matt }
304 1.7.2.3 matt #endif
305 1.7.2.3 matt
306 1.7.2.2 matt /*
307 1.7.2.2 matt * Attach the generic SD/MMC bus driver. (The bus driver must
308 1.7.2.2 matt * not invoke any chipset functions before it is attached.)
309 1.7.2.2 matt */
310 1.7.2.2 matt memset(&saa, 0, sizeof(saa));
311 1.7.2.2 matt saa.saa_busname = "sdmmc";
312 1.7.2.2 matt saa.saa_sct = &sdhc_functions;
313 1.7.2.2 matt saa.saa_sch = hp;
314 1.7.2.2 matt saa.saa_dmat = hp->dmat;
315 1.7.2.6 matt if (sc->sc_flags & SDHC_FLAG_HAVE_CGM) {
316 1.7.2.3 matt saa.saa_clkmin = hp->clkbase / 2046;
317 1.7.2.3 matt } else {
318 1.7.2.3 matt saa.saa_clkmin = hp->clkbase / 256;
319 1.7.2.3 matt }
320 1.7.2.2 matt saa.saa_clkmax = hp->clkbase;
321 1.7.2.4 matt if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_DVS))
322 1.7.2.4 matt saa.saa_clkmin /= 16;
323 1.7.2.2 matt saa.saa_caps = SMC_CAPS_4BIT_MODE|SMC_CAPS_AUTO_STOP;
324 1.7.2.3 matt DPRINTF(1, ("%s: clkmin=%d clkmax=%d\n",
325 1.7.2.3 matt device_xname(sc->sc_dev), saa.saa_clkmin, saa.saa_clkmax));
326 1.7.2.2 matt #if notyet
327 1.7.2.2 matt if (ISSET(hp->flags, SHF_USE_DMA))
328 1.7.2.2 matt saa.saa_caps |= SMC_CAPS_DMA;
329 1.7.2.2 matt #endif
330 1.7.2.2 matt
331 1.7.2.2 matt hp->sdmmc = config_found(sc->sc_dev, &saa, NULL);
332 1.7.2.2 matt
333 1.7.2.2 matt return 0;
334 1.7.2.2 matt
335 1.7.2.2 matt err:
336 1.7.2.2 matt cv_destroy(&hp->intr_cv);
337 1.7.2.2 matt mutex_destroy(&hp->intr_mtx);
338 1.7.2.2 matt mutex_destroy(&hp->host_mtx);
339 1.7.2.2 matt free(hp, M_DEVBUF);
340 1.7.2.2 matt sc->sc_host[--sc->sc_nhosts] = NULL;
341 1.7.2.2 matt err1:
342 1.7.2.2 matt return 1;
343 1.7.2.2 matt }
344 1.7.2.2 matt
345 1.7.2.2 matt bool
346 1.7.2.2 matt sdhc_suspend(device_t dev PMF_FN_ARGS)
347 1.7.2.2 matt {
348 1.7.2.2 matt struct sdhc_softc *sc = device_private(dev);
349 1.7.2.2 matt struct sdhc_host *hp;
350 1.7.2.2 matt int n, i;
351 1.7.2.2 matt
352 1.7.2.2 matt /* XXX poll for command completion or suspend command
353 1.7.2.2 matt * in progress */
354 1.7.2.2 matt
355 1.7.2.2 matt /* Save the host controller state. */
356 1.7.2.2 matt for (n = 0; n < sc->sc_nhosts; n++) {
357 1.7.2.2 matt hp = sc->sc_host[n];
358 1.7.2.2 matt for (i = 0; i < sizeof hp->regs; i++)
359 1.7.2.2 matt hp->regs[i] = HREAD1(hp, i);
360 1.7.2.2 matt }
361 1.7.2.2 matt return true;
362 1.7.2.2 matt }
363 1.7.2.2 matt
364 1.7.2.2 matt bool
365 1.7.2.2 matt sdhc_resume(device_t dev PMF_FN_ARGS)
366 1.7.2.2 matt {
367 1.7.2.2 matt struct sdhc_softc *sc = device_private(dev);
368 1.7.2.2 matt struct sdhc_host *hp;
369 1.7.2.2 matt int n, i;
370 1.7.2.2 matt
371 1.7.2.2 matt /* Restore the host controller state. */
372 1.7.2.2 matt for (n = 0; n < sc->sc_nhosts; n++) {
373 1.7.2.2 matt hp = sc->sc_host[n];
374 1.7.2.2 matt (void)sdhc_host_reset(hp);
375 1.7.2.2 matt for (i = 0; i < sizeof hp->regs; i++)
376 1.7.2.2 matt HWRITE1(hp, i, hp->regs[i]);
377 1.7.2.2 matt }
378 1.7.2.2 matt return true;
379 1.7.2.2 matt }
380 1.7.2.2 matt
381 1.7.2.2 matt bool
382 1.7.2.2 matt sdhc_shutdown(device_t dev, int flags)
383 1.7.2.2 matt {
384 1.7.2.2 matt struct sdhc_softc *sc = device_private(dev);
385 1.7.2.2 matt struct sdhc_host *hp;
386 1.7.2.2 matt int i;
387 1.7.2.2 matt
388 1.7.2.2 matt /* XXX chip locks up if we don't disable it before reboot. */
389 1.7.2.2 matt for (i = 0; i < sc->sc_nhosts; i++) {
390 1.7.2.2 matt hp = sc->sc_host[i];
391 1.7.2.2 matt (void)sdhc_host_reset(hp);
392 1.7.2.2 matt }
393 1.7.2.2 matt return true;
394 1.7.2.2 matt }
395 1.7.2.2 matt
396 1.7.2.2 matt /*
397 1.7.2.2 matt * Reset the host controller. Called during initialization, when
398 1.7.2.2 matt * cards are removed, upon resume, and during error recovery.
399 1.7.2.2 matt */
400 1.7.2.2 matt static int
401 1.7.2.2 matt sdhc_host_reset1(sdmmc_chipset_handle_t sch)
402 1.7.2.2 matt {
403 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
404 1.7.2.2 matt uint16_t sdhcimask;
405 1.7.2.2 matt int error;
406 1.7.2.2 matt
407 1.7.2.2 matt /* Don't lock. */
408 1.7.2.2 matt
409 1.7.2.2 matt /* Disable all interrupts. */
410 1.7.2.2 matt HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
411 1.7.2.2 matt
412 1.7.2.2 matt /*
413 1.7.2.2 matt * Reset the entire host controller and wait up to 100ms for
414 1.7.2.2 matt * the controller to clear the reset bit.
415 1.7.2.2 matt */
416 1.7.2.2 matt error = sdhc_soft_reset(hp, SDHC_RESET_ALL);
417 1.7.2.2 matt if (error)
418 1.7.2.2 matt goto out;
419 1.7.2.2 matt
420 1.7.2.2 matt /* Set data timeout counter value to max for now. */
421 1.7.2.2 matt HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
422 1.7.2.2 matt
423 1.7.2.2 matt /* Enable interrupts. */
424 1.7.2.2 matt sdhcimask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION |
425 1.7.2.2 matt SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY |
426 1.7.2.2 matt SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT |
427 1.7.2.2 matt SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE;
428 1.7.2.2 matt HWRITE2(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
429 1.7.2.2 matt HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK);
430 1.7.2.2 matt HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
431 1.7.2.2 matt HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK);
432 1.7.2.2 matt
433 1.7.2.2 matt out:
434 1.7.2.2 matt return error;
435 1.7.2.2 matt }
436 1.7.2.2 matt
437 1.7.2.2 matt static int
438 1.7.2.2 matt sdhc_host_reset(sdmmc_chipset_handle_t sch)
439 1.7.2.2 matt {
440 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
441 1.7.2.2 matt int error;
442 1.7.2.2 matt
443 1.7.2.2 matt mutex_enter(&hp->host_mtx);
444 1.7.2.2 matt error = sdhc_host_reset1(sch);
445 1.7.2.2 matt mutex_exit(&hp->host_mtx);
446 1.7.2.2 matt
447 1.7.2.2 matt return error;
448 1.7.2.2 matt }
449 1.7.2.2 matt
450 1.7.2.2 matt static uint32_t
451 1.7.2.2 matt sdhc_host_ocr(sdmmc_chipset_handle_t sch)
452 1.7.2.2 matt {
453 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
454 1.7.2.2 matt
455 1.7.2.2 matt return hp->ocr;
456 1.7.2.2 matt }
457 1.7.2.2 matt
458 1.7.2.2 matt static int
459 1.7.2.2 matt sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)
460 1.7.2.2 matt {
461 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
462 1.7.2.2 matt
463 1.7.2.2 matt return hp->maxblklen;
464 1.7.2.2 matt }
465 1.7.2.2 matt
466 1.7.2.2 matt /*
467 1.7.2.2 matt * Return non-zero if the card is currently inserted.
468 1.7.2.2 matt */
469 1.7.2.2 matt static int
470 1.7.2.2 matt sdhc_card_detect(sdmmc_chipset_handle_t sch)
471 1.7.2.2 matt {
472 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
473 1.7.2.2 matt int r;
474 1.7.2.2 matt
475 1.7.2.2 matt mutex_enter(&hp->host_mtx);
476 1.7.2.2 matt r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED);
477 1.7.2.2 matt mutex_exit(&hp->host_mtx);
478 1.7.2.2 matt
479 1.7.2.2 matt if (r)
480 1.7.2.2 matt return 1;
481 1.7.2.2 matt return 0;
482 1.7.2.2 matt }
483 1.7.2.2 matt
484 1.7.2.2 matt /*
485 1.7.2.2 matt * Return non-zero if the card is currently write-protected.
486 1.7.2.2 matt */
487 1.7.2.2 matt static int
488 1.7.2.2 matt sdhc_write_protect(sdmmc_chipset_handle_t sch)
489 1.7.2.2 matt {
490 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
491 1.7.2.2 matt int r;
492 1.7.2.2 matt
493 1.7.2.2 matt mutex_enter(&hp->host_mtx);
494 1.7.2.2 matt r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_WRITE_PROTECT_SWITCH);
495 1.7.2.2 matt mutex_exit(&hp->host_mtx);
496 1.7.2.2 matt
497 1.7.2.2 matt if (!r)
498 1.7.2.2 matt return 1;
499 1.7.2.2 matt return 0;
500 1.7.2.2 matt }
501 1.7.2.2 matt
502 1.7.2.2 matt /*
503 1.7.2.2 matt * Set or change SD bus voltage and enable or disable SD bus power.
504 1.7.2.2 matt * Return zero on success.
505 1.7.2.2 matt */
506 1.7.2.2 matt static int
507 1.7.2.2 matt sdhc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
508 1.7.2.2 matt {
509 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
510 1.7.2.2 matt uint8_t vdd;
511 1.7.2.2 matt int error = 0;
512 1.7.2.2 matt
513 1.7.2.2 matt mutex_enter(&hp->host_mtx);
514 1.7.2.2 matt
515 1.7.2.2 matt /*
516 1.7.2.2 matt * Disable bus power before voltage change.
517 1.7.2.2 matt */
518 1.7.2.2 matt if (!(hp->sc->sc_flags & SDHC_FLAG_NO_PWR0))
519 1.7.2.2 matt HWRITE1(hp, SDHC_POWER_CTL, 0);
520 1.7.2.2 matt
521 1.7.2.2 matt /* If power is disabled, reset the host and return now. */
522 1.7.2.2 matt if (ocr == 0) {
523 1.7.2.2 matt (void)sdhc_host_reset1(hp);
524 1.7.2.2 matt goto out;
525 1.7.2.2 matt }
526 1.7.2.2 matt
527 1.7.2.2 matt /*
528 1.7.2.2 matt * Select the lowest voltage according to capabilities.
529 1.7.2.2 matt */
530 1.7.2.2 matt ocr &= hp->ocr;
531 1.7.2.2 matt if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V))
532 1.7.2.2 matt vdd = SDHC_VOLTAGE_1_8V;
533 1.7.2.2 matt else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V))
534 1.7.2.2 matt vdd = SDHC_VOLTAGE_3_0V;
535 1.7.2.2 matt else if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V))
536 1.7.2.2 matt vdd = SDHC_VOLTAGE_3_3V;
537 1.7.2.2 matt else {
538 1.7.2.2 matt /* Unsupported voltage level requested. */
539 1.7.2.2 matt error = EINVAL;
540 1.7.2.2 matt goto out;
541 1.7.2.2 matt }
542 1.7.2.2 matt
543 1.7.2.2 matt /*
544 1.7.2.2 matt * Enable bus power. Wait at least 1 ms (or 74 clocks) plus
545 1.7.2.2 matt * voltage ramp until power rises.
546 1.7.2.2 matt */
547 1.7.2.2 matt HWRITE1(hp, SDHC_POWER_CTL,
548 1.7.2.2 matt (vdd << SDHC_VOLTAGE_SHIFT) | SDHC_BUS_POWER);
549 1.7.2.2 matt sdmmc_delay(10000);
550 1.7.2.2 matt
551 1.7.2.2 matt /*
552 1.7.2.2 matt * The host system may not power the bus due to battery low,
553 1.7.2.2 matt * etc. In that case, the host controller should clear the
554 1.7.2.2 matt * bus power bit.
555 1.7.2.2 matt */
556 1.7.2.2 matt if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) {
557 1.7.2.2 matt error = ENXIO;
558 1.7.2.2 matt goto out;
559 1.7.2.2 matt }
560 1.7.2.2 matt
561 1.7.2.2 matt out:
562 1.7.2.2 matt mutex_exit(&hp->host_mtx);
563 1.7.2.2 matt
564 1.7.2.2 matt return error;
565 1.7.2.2 matt }
566 1.7.2.2 matt
567 1.7.2.2 matt /*
568 1.7.2.2 matt * Return the smallest possible base clock frequency divisor value
569 1.7.2.2 matt * for the CLOCK_CTL register to produce `freq' (KHz).
570 1.7.2.2 matt */
571 1.7.2.2 matt static int
572 1.7.2.3 matt sdhc_clock_divisor(struct sdhc_host *hp, u_int freq, int *divp)
573 1.7.2.2 matt {
574 1.7.2.2 matt int div;
575 1.7.2.2 matt
576 1.7.2.6 matt if (hp->sc->sc_flags & SDHC_FLAG_HAVE_CGM) {
577 1.7.2.3 matt for (div = hp->clkbase / freq; div <= 0x3ff; div++) {
578 1.7.2.3 matt if ((hp->clkbase / div) <= freq) {
579 1.7.2.3 matt *divp = SDHC_SDCLK_CGM
580 1.7.2.3 matt | ((div & 0x300) << SDHC_SDCLK_XDIV_SHIFT)
581 1.7.2.3 matt | ((div & 0x0ff) << SDHC_SDCLK_DIV_SHIFT);
582 1.7.2.3 matt return false;
583 1.7.2.3 matt }
584 1.7.2.3 matt }
585 1.7.2.3 matt /* No divisor found. */
586 1.7.2.3 matt return true;
587 1.7.2.3 matt }
588 1.7.2.4 matt if (hp->sc->sc_flags & SDHC_FLAG_HAVE_DVS) {
589 1.7.2.4 matt int dvs = (hp->clkbase + freq - 1) / freq;
590 1.7.2.4 matt div = 1;
591 1.7.2.4 matt for (div = 1; div <= 256; div <<= 1, dvs >>= 1) {
592 1.7.2.4 matt if (dvs <= 16) {
593 1.7.2.4 matt div <<= SDHC_SDCLK_DIV_SHIFT;
594 1.7.2.4 matt div |= (dvs - 1) << SDHC_SDCLK_DVS_SHIFT;
595 1.7.2.4 matt *divp = div;
596 1.7.2.4 matt return false;
597 1.7.2.4 matt }
598 1.7.2.4 matt }
599 1.7.2.4 matt /* No divisor found. */
600 1.7.2.4 matt return true;
601 1.7.2.4 matt }
602 1.7.2.3 matt for (div = 1; div <= 256; div *= 2) {
603 1.7.2.3 matt if ((hp->clkbase / div) <= freq) {
604 1.7.2.3 matt *divp = (div / 2) << SDHC_SDCLK_DIV_SHIFT;
605 1.7.2.3 matt return false;
606 1.7.2.3 matt }
607 1.7.2.3 matt }
608 1.7.2.3 matt
609 1.7.2.2 matt /* No divisor found. */
610 1.7.2.3 matt return true;
611 1.7.2.2 matt }
612 1.7.2.2 matt
613 1.7.2.2 matt /*
614 1.7.2.2 matt * Set or change SDCLK frequency or disable the SD clock.
615 1.7.2.2 matt * Return zero on success.
616 1.7.2.2 matt */
617 1.7.2.2 matt static int
618 1.7.2.2 matt sdhc_bus_clock(sdmmc_chipset_handle_t sch, int freq)
619 1.7.2.2 matt {
620 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
621 1.7.2.2 matt int div;
622 1.7.2.2 matt int timo;
623 1.7.2.2 matt int error = 0;
624 1.7.2.2 matt #ifdef DIAGNOSTIC
625 1.7.2.2 matt int ispresent;
626 1.7.2.2 matt #endif
627 1.7.2.2 matt
628 1.7.2.2 matt #ifdef DIAGNOSTIC
629 1.7.2.2 matt mutex_enter(&hp->host_mtx);
630 1.7.2.2 matt ispresent = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK);
631 1.7.2.2 matt mutex_exit(&hp->host_mtx);
632 1.7.2.2 matt
633 1.7.2.2 matt /* Must not stop the clock if commands are in progress. */
634 1.7.2.2 matt if (ispresent && sdhc_card_detect(hp))
635 1.7.2.2 matt printf("%s: sdhc_sdclk_frequency_select: command in progress\n",
636 1.7.2.2 matt device_xname(hp->sc->sc_dev));
637 1.7.2.2 matt #endif
638 1.7.2.2 matt
639 1.7.2.2 matt mutex_enter(&hp->host_mtx);
640 1.7.2.2 matt
641 1.7.2.2 matt /*
642 1.7.2.2 matt * Stop SD clock before changing the frequency.
643 1.7.2.2 matt */
644 1.7.2.2 matt HWRITE2(hp, SDHC_CLOCK_CTL, 0);
645 1.7.2.2 matt if (freq == SDMMC_SDCLK_OFF)
646 1.7.2.2 matt goto out;
647 1.7.2.2 matt
648 1.7.2.2 matt /*
649 1.7.2.2 matt * Set the minimum base clock frequency divisor.
650 1.7.2.2 matt */
651 1.7.2.3 matt if (sdhc_clock_divisor(hp, freq, &div)) {
652 1.7.2.2 matt /* Invalid base clock frequency or `freq' value. */
653 1.7.2.2 matt error = EINVAL;
654 1.7.2.2 matt goto out;
655 1.7.2.2 matt }
656 1.7.2.4 matt HWRITE2(hp, SDHC_CLOCK_CTL, div);
657 1.7.2.2 matt
658 1.7.2.2 matt /*
659 1.7.2.2 matt * Start internal clock. Wait 10ms for stabilization.
660 1.7.2.2 matt */
661 1.7.2.2 matt HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE);
662 1.7.2.2 matt for (timo = 1000; timo > 0; timo--) {
663 1.7.2.2 matt if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL), SDHC_INTCLK_STABLE))
664 1.7.2.2 matt break;
665 1.7.2.2 matt sdmmc_delay(10);
666 1.7.2.2 matt }
667 1.7.2.2 matt if (timo == 0) {
668 1.7.2.2 matt error = ETIMEDOUT;
669 1.7.2.2 matt goto out;
670 1.7.2.2 matt }
671 1.7.2.2 matt
672 1.7.2.2 matt /*
673 1.7.2.2 matt * Enable SD clock.
674 1.7.2.2 matt */
675 1.7.2.2 matt HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
676 1.7.2.2 matt
677 1.7.2.2 matt out:
678 1.7.2.2 matt mutex_exit(&hp->host_mtx);
679 1.7.2.2 matt
680 1.7.2.2 matt return error;
681 1.7.2.2 matt }
682 1.7.2.2 matt
683 1.7.2.2 matt static int
684 1.7.2.2 matt sdhc_bus_width(sdmmc_chipset_handle_t sch, int width)
685 1.7.2.2 matt {
686 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
687 1.7.2.2 matt int reg;
688 1.7.2.2 matt
689 1.7.2.2 matt switch (width) {
690 1.7.2.2 matt case 1:
691 1.7.2.2 matt case 4:
692 1.7.2.2 matt break;
693 1.7.2.2 matt
694 1.7.2.2 matt default:
695 1.7.2.2 matt DPRINTF(0,("%s: unsupported bus width (%d)\n",
696 1.7.2.2 matt HDEVNAME(hp), width));
697 1.7.2.2 matt return 1;
698 1.7.2.2 matt }
699 1.7.2.2 matt
700 1.7.2.2 matt mutex_enter(&hp->host_mtx);
701 1.7.2.2 matt reg = HREAD1(hp, SDHC_POWER_CTL);
702 1.7.2.2 matt reg &= ~SDHC_4BIT_MODE;
703 1.7.2.2 matt if (width == 4)
704 1.7.2.2 matt reg |= SDHC_4BIT_MODE;
705 1.7.2.2 matt HWRITE1(hp, SDHC_POWER_CTL, reg);
706 1.7.2.2 matt mutex_exit(&hp->host_mtx);
707 1.7.2.2 matt
708 1.7.2.2 matt return 0;
709 1.7.2.2 matt }
710 1.7.2.2 matt
711 1.7.2.2 matt static void
712 1.7.2.2 matt sdhc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
713 1.7.2.2 matt {
714 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
715 1.7.2.2 matt
716 1.7.2.2 matt mutex_enter(&hp->host_mtx);
717 1.7.2.2 matt if (enable) {
718 1.7.2.2 matt HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
719 1.7.2.2 matt HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
720 1.7.2.2 matt } else {
721 1.7.2.2 matt HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
722 1.7.2.2 matt HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
723 1.7.2.2 matt }
724 1.7.2.2 matt mutex_exit(&hp->host_mtx);
725 1.7.2.2 matt }
726 1.7.2.2 matt
727 1.7.2.2 matt static void
728 1.7.2.2 matt sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)
729 1.7.2.2 matt {
730 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
731 1.7.2.2 matt
732 1.7.2.2 matt mutex_enter(&hp->host_mtx);
733 1.7.2.2 matt HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
734 1.7.2.2 matt mutex_exit(&hp->host_mtx);
735 1.7.2.2 matt }
736 1.7.2.2 matt
737 1.7.2.2 matt static int
738 1.7.2.2 matt sdhc_wait_state(struct sdhc_host *hp, uint32_t mask, uint32_t value)
739 1.7.2.2 matt {
740 1.7.2.2 matt uint32_t state;
741 1.7.2.2 matt int timeout;
742 1.7.2.2 matt
743 1.7.2.2 matt for (timeout = 10; timeout > 0; timeout--) {
744 1.7.2.2 matt if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask) == value)
745 1.7.2.2 matt return 0;
746 1.7.2.2 matt sdmmc_delay(10000);
747 1.7.2.2 matt }
748 1.7.2.2 matt DPRINTF(0,("%s: timeout waiting for %x (state=%x)\n", HDEVNAME(hp),
749 1.7.2.2 matt value, state));
750 1.7.2.2 matt return ETIMEDOUT;
751 1.7.2.2 matt }
752 1.7.2.2 matt
753 1.7.2.2 matt static void
754 1.7.2.2 matt sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
755 1.7.2.2 matt {
756 1.7.2.2 matt struct sdhc_host *hp = (struct sdhc_host *)sch;
757 1.7.2.2 matt int error;
758 1.7.2.2 matt
759 1.7.2.2 matt /*
760 1.7.2.2 matt * Start the MMC command, or mark `cmd' as failed and return.
761 1.7.2.2 matt */
762 1.7.2.2 matt error = sdhc_start_command(hp, cmd);
763 1.7.2.2 matt if (error) {
764 1.7.2.2 matt cmd->c_error = error;
765 1.7.2.2 matt goto out;
766 1.7.2.2 matt }
767 1.7.2.2 matt
768 1.7.2.2 matt /*
769 1.7.2.2 matt * Wait until the command phase is done, or until the command
770 1.7.2.2 matt * is marked done for any other reason.
771 1.7.2.2 matt */
772 1.7.2.2 matt if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE, SDHC_COMMAND_TIMEOUT)) {
773 1.7.2.2 matt cmd->c_error = ETIMEDOUT;
774 1.7.2.2 matt goto out;
775 1.7.2.2 matt }
776 1.7.2.2 matt
777 1.7.2.2 matt /*
778 1.7.2.2 matt * The host controller removes bits [0:7] from the response
779 1.7.2.2 matt * data (CRC) and we pass the data up unchanged to the bus
780 1.7.2.2 matt * driver (without padding).
781 1.7.2.2 matt */
782 1.7.2.2 matt mutex_enter(&hp->host_mtx);
783 1.7.2.2 matt if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
784 1.7.2.2 matt if (ISSET(cmd->c_flags, SCF_RSP_136)) {
785 1.7.2.2 matt uint8_t *p = (uint8_t *)cmd->c_resp;
786 1.7.2.2 matt int i;
787 1.7.2.2 matt
788 1.7.2.2 matt for (i = 0; i < 15; i++)
789 1.7.2.2 matt *p++ = HREAD1(hp, SDHC_RESPONSE + i);
790 1.7.2.2 matt } else {
791 1.7.2.2 matt cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE);
792 1.7.2.2 matt }
793 1.7.2.2 matt }
794 1.7.2.2 matt mutex_exit(&hp->host_mtx);
795 1.7.2.2 matt DPRINTF(1,("%s: resp = %08x\n", HDEVNAME(hp), cmd->c_resp[0]));
796 1.7.2.2 matt
797 1.7.2.2 matt /*
798 1.7.2.2 matt * If the command has data to transfer in any direction,
799 1.7.2.2 matt * execute the transfer now.
800 1.7.2.2 matt */
801 1.7.2.2 matt if (cmd->c_error == 0 && cmd->c_data != NULL)
802 1.7.2.2 matt sdhc_transfer_data(hp, cmd);
803 1.7.2.2 matt
804 1.7.2.2 matt out:
805 1.7.2.2 matt mutex_enter(&hp->host_mtx);
806 1.7.2.2 matt /* Turn off the LED. */
807 1.7.2.2 matt HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
808 1.7.2.2 matt mutex_exit(&hp->host_mtx);
809 1.7.2.2 matt SET(cmd->c_flags, SCF_ITSDONE);
810 1.7.2.2 matt
811 1.7.2.2 matt DPRINTF(1,("%s: cmd %d %s (flags=%08x error=%d)\n", HDEVNAME(hp),
812 1.7.2.2 matt cmd->c_opcode, (cmd->c_error == 0) ? "done" : "abort",
813 1.7.2.2 matt cmd->c_flags, cmd->c_error));
814 1.7.2.2 matt }
815 1.7.2.2 matt
816 1.7.2.2 matt static int
817 1.7.2.2 matt sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd)
818 1.7.2.2 matt {
819 1.7.2.2 matt uint16_t blksize = 0;
820 1.7.2.2 matt uint16_t blkcount = 0;
821 1.7.2.2 matt uint16_t mode;
822 1.7.2.2 matt uint16_t command;
823 1.7.2.2 matt int error;
824 1.7.2.2 matt
825 1.7.2.2 matt DPRINTF(1,("%s: start cmd %d arg=%08x data=%p dlen=%d flags=%08x "
826 1.7.2.2 matt "proc=%p \"%s\"\n", HDEVNAME(hp), cmd->c_opcode, cmd->c_arg,
827 1.7.2.2 matt cmd->c_data, cmd->c_datalen, cmd->c_flags, curproc,
828 1.7.2.2 matt curproc ? curproc->p_comm : ""));
829 1.7.2.2 matt
830 1.7.2.2 matt /*
831 1.7.2.2 matt * The maximum block length for commands should be the minimum
832 1.7.2.2 matt * of the host buffer size and the card buffer size. (1.7.2)
833 1.7.2.2 matt */
834 1.7.2.2 matt
835 1.7.2.2 matt /* Fragment the data into proper blocks. */
836 1.7.2.2 matt if (cmd->c_datalen > 0) {
837 1.7.2.2 matt blksize = MIN(cmd->c_datalen, cmd->c_blklen);
838 1.7.2.2 matt blkcount = cmd->c_datalen / blksize;
839 1.7.2.2 matt if (cmd->c_datalen % blksize > 0) {
840 1.7.2.2 matt /* XXX: Split this command. (1.7.4) */
841 1.7.2.2 matt aprint_error_dev(hp->sc->sc_dev,
842 1.7.2.2 matt "data not a multiple of %u bytes\n", blksize);
843 1.7.2.2 matt return EINVAL;
844 1.7.2.2 matt }
845 1.7.2.2 matt }
846 1.7.2.2 matt
847 1.7.2.2 matt /* Check limit imposed by 9-bit block count. (1.7.2) */
848 1.7.2.2 matt if (blkcount > SDHC_BLOCK_COUNT_MAX) {
849 1.7.2.2 matt aprint_error_dev(hp->sc->sc_dev, "too much data\n");
850 1.7.2.2 matt return EINVAL;
851 1.7.2.2 matt }
852 1.7.2.2 matt
853 1.7.2.2 matt /* Prepare transfer mode register value. (2.2.5) */
854 1.7.2.2 matt mode = 0;
855 1.7.2.2 matt if (ISSET(cmd->c_flags, SCF_CMD_READ))
856 1.7.2.2 matt mode |= SDHC_READ_MODE;
857 1.7.2.2 matt if (blkcount > 0) {
858 1.7.2.2 matt mode |= SDHC_BLOCK_COUNT_ENABLE;
859 1.7.2.2 matt if (blkcount > 1) {
860 1.7.2.2 matt mode |= SDHC_MULTI_BLOCK_MODE;
861 1.7.2.2 matt /* XXX only for memory commands? */
862 1.7.2.2 matt mode |= SDHC_AUTO_CMD12_ENABLE;
863 1.7.2.2 matt }
864 1.7.2.2 matt }
865 1.7.2.2 matt #if notyet
866 1.7.2.2 matt if (cmd->c_dmap != NULL && cmd->c_datalen > 0)
867 1.7.2.2 matt mode |= SDHC_DMA_ENABLE;
868 1.7.2.2 matt #endif
869 1.7.2.2 matt
870 1.7.2.2 matt /*
871 1.7.2.2 matt * Prepare command register value. (2.2.6)
872 1.7.2.2 matt */
873 1.7.2.2 matt command =
874 1.7.2.2 matt (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) << SDHC_COMMAND_INDEX_SHIFT;
875 1.7.2.2 matt
876 1.7.2.2 matt if (ISSET(cmd->c_flags, SCF_RSP_CRC))
877 1.7.2.2 matt command |= SDHC_CRC_CHECK_ENABLE;
878 1.7.2.2 matt if (ISSET(cmd->c_flags, SCF_RSP_IDX))
879 1.7.2.2 matt command |= SDHC_INDEX_CHECK_ENABLE;
880 1.7.2.2 matt if (cmd->c_data != NULL)
881 1.7.2.2 matt command |= SDHC_DATA_PRESENT_SELECT;
882 1.7.2.2 matt
883 1.7.2.2 matt if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
884 1.7.2.2 matt command |= SDHC_NO_RESPONSE;
885 1.7.2.2 matt else if (ISSET(cmd->c_flags, SCF_RSP_136))
886 1.7.2.2 matt command |= SDHC_RESP_LEN_136;
887 1.7.2.2 matt else if (ISSET(cmd->c_flags, SCF_RSP_BSY))
888 1.7.2.2 matt command |= SDHC_RESP_LEN_48_CHK_BUSY;
889 1.7.2.2 matt else
890 1.7.2.2 matt command |= SDHC_RESP_LEN_48;
891 1.7.2.2 matt
892 1.7.2.2 matt /* Wait until command and data inhibit bits are clear. (1.5) */
893 1.7.2.2 matt error = sdhc_wait_state(hp, SDHC_CMD_INHIBIT_MASK, 0);
894 1.7.2.2 matt if (error)
895 1.7.2.2 matt return error;
896 1.7.2.2 matt
897 1.7.2.2 matt DPRINTF(1,("%s: writing cmd: blksize=%d blkcnt=%d mode=%04x cmd=%04x\n",
898 1.7.2.2 matt HDEVNAME(hp), blksize, blkcount, mode, command));
899 1.7.2.2 matt
900 1.7.2.2 matt mutex_enter(&hp->host_mtx);
901 1.7.2.2 matt
902 1.7.2.2 matt /* Alert the user not to remove the card. */
903 1.7.2.2 matt HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
904 1.7.2.2 matt
905 1.7.2.2 matt /*
906 1.7.2.2 matt * Start a CPU data transfer. Writing to the high order byte
907 1.7.2.2 matt * of the SDHC_COMMAND register triggers the SD command. (1.5)
908 1.7.2.2 matt */
909 1.7.2.2 matt HWRITE2(hp, SDHC_TRANSFER_MODE, mode);
910 1.7.2.2 matt HWRITE2(hp, SDHC_BLOCK_SIZE, blksize);
911 1.7.2.2 matt if (blkcount > 1)
912 1.7.2.2 matt HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount);
913 1.7.2.2 matt HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
914 1.7.2.2 matt HWRITE2(hp, SDHC_COMMAND, command);
915 1.7.2.2 matt
916 1.7.2.2 matt mutex_exit(&hp->host_mtx);
917 1.7.2.2 matt
918 1.7.2.2 matt return 0;
919 1.7.2.2 matt }
920 1.7.2.2 matt
921 1.7.2.2 matt static void
922 1.7.2.2 matt sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd)
923 1.7.2.2 matt {
924 1.7.2.2 matt int error;
925 1.7.2.2 matt
926 1.7.2.2 matt DPRINTF(1,("%s: data transfer: resp=%08x datalen=%u\n", HDEVNAME(hp),
927 1.7.2.2 matt MMC_R1(cmd->c_resp), cmd->c_datalen));
928 1.7.2.2 matt
929 1.7.2.2 matt #ifdef SDHC_DEBUG
930 1.7.2.2 matt /* XXX I forgot why I wanted to know when this happens :-( */
931 1.7.2.2 matt if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) &&
932 1.7.2.2 matt ISSET(MMC_R1(cmd->c_resp), 0xcb00)) {
933 1.7.2.2 matt aprint_error_dev(hp->sc->sc_dev,
934 1.7.2.2 matt "CMD52/53 error response flags %#x\n",
935 1.7.2.2 matt MMC_R1(cmd->c_resp) & 0xff00);
936 1.7.2.2 matt }
937 1.7.2.2 matt #endif
938 1.7.2.2 matt
939 1.7.2.2 matt error = sdhc_transfer_data_pio(hp, cmd);
940 1.7.2.2 matt if (error)
941 1.7.2.2 matt cmd->c_error = error;
942 1.7.2.2 matt SET(cmd->c_flags, SCF_ITSDONE);
943 1.7.2.2 matt
944 1.7.2.2 matt DPRINTF(1,("%s: data transfer done (error=%d)\n",
945 1.7.2.2 matt HDEVNAME(hp), cmd->c_error));
946 1.7.2.2 matt }
947 1.7.2.2 matt
948 1.7.2.2 matt static int
949 1.7.2.2 matt sdhc_transfer_data_pio(struct sdhc_host *hp, struct sdmmc_command *cmd)
950 1.7.2.2 matt {
951 1.7.2.2 matt uint8_t *data = cmd->c_data;
952 1.7.2.2 matt int len, datalen;
953 1.7.2.2 matt int mask;
954 1.7.2.2 matt int error = 0;
955 1.7.2.2 matt
956 1.7.2.2 matt mask = ISSET(cmd->c_flags, SCF_CMD_READ) ?
957 1.7.2.2 matt SDHC_BUFFER_READ_ENABLE : SDHC_BUFFER_WRITE_ENABLE;
958 1.7.2.2 matt datalen = cmd->c_datalen;
959 1.7.2.2 matt
960 1.7.2.2 matt while (datalen > 0) {
961 1.7.2.2 matt if (!sdhc_wait_intr(hp,
962 1.7.2.2 matt SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY,
963 1.7.2.2 matt SDHC_BUFFER_TIMEOUT)) {
964 1.7.2.2 matt error = ETIMEDOUT;
965 1.7.2.2 matt break;
966 1.7.2.2 matt }
967 1.7.2.2 matt
968 1.7.2.2 matt error = sdhc_wait_state(hp, mask, mask);
969 1.7.2.2 matt if (error)
970 1.7.2.2 matt break;
971 1.7.2.2 matt
972 1.7.2.2 matt len = MIN(datalen, cmd->c_blklen);
973 1.7.2.2 matt if (ISSET(cmd->c_flags, SCF_CMD_READ))
974 1.7.2.2 matt sdhc_read_data_pio(hp, data, len);
975 1.7.2.2 matt else
976 1.7.2.2 matt sdhc_write_data_pio(hp, data, len);
977 1.7.2.2 matt
978 1.7.2.2 matt data += len;
979 1.7.2.2 matt datalen -= len;
980 1.7.2.2 matt }
981 1.7.2.2 matt
982 1.7.2.2 matt if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
983 1.7.2.2 matt SDHC_TRANSFER_TIMEOUT))
984 1.7.2.2 matt error = ETIMEDOUT;
985 1.7.2.2 matt
986 1.7.2.2 matt return error;
987 1.7.2.2 matt }
988 1.7.2.2 matt
989 1.7.2.2 matt static void
990 1.7.2.2 matt sdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, int datalen)
991 1.7.2.2 matt {
992 1.7.2.2 matt
993 1.7.2.2 matt if (((__uintptr_t)data & 3) == 0) {
994 1.7.2.2 matt while (datalen > 3) {
995 1.7.2.2 matt *(uint32_t *)data = HREAD4(hp, SDHC_DATA);
996 1.7.2.2 matt data += 4;
997 1.7.2.2 matt datalen -= 4;
998 1.7.2.2 matt }
999 1.7.2.2 matt if (datalen > 1) {
1000 1.7.2.2 matt *(uint16_t *)data = HREAD2(hp, SDHC_DATA);
1001 1.7.2.2 matt data += 2;
1002 1.7.2.2 matt datalen -= 2;
1003 1.7.2.2 matt }
1004 1.7.2.2 matt if (datalen > 0) {
1005 1.7.2.2 matt *data = HREAD1(hp, SDHC_DATA);
1006 1.7.2.2 matt data += 1;
1007 1.7.2.2 matt datalen -= 1;
1008 1.7.2.2 matt }
1009 1.7.2.2 matt } else if (((__uintptr_t)data & 1) == 0) {
1010 1.7.2.2 matt while (datalen > 1) {
1011 1.7.2.2 matt *(uint16_t *)data = HREAD2(hp, SDHC_DATA);
1012 1.7.2.2 matt data += 2;
1013 1.7.2.2 matt datalen -= 2;
1014 1.7.2.2 matt }
1015 1.7.2.2 matt if (datalen > 0) {
1016 1.7.2.2 matt *data = HREAD1(hp, SDHC_DATA);
1017 1.7.2.2 matt data += 1;
1018 1.7.2.2 matt datalen -= 1;
1019 1.7.2.2 matt }
1020 1.7.2.2 matt } else {
1021 1.7.2.2 matt while (datalen > 0) {
1022 1.7.2.2 matt *data = HREAD1(hp, SDHC_DATA);
1023 1.7.2.2 matt data += 1;
1024 1.7.2.2 matt datalen -= 1;
1025 1.7.2.2 matt }
1026 1.7.2.2 matt }
1027 1.7.2.2 matt }
1028 1.7.2.2 matt
1029 1.7.2.2 matt static void
1030 1.7.2.2 matt sdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, int datalen)
1031 1.7.2.2 matt {
1032 1.7.2.2 matt
1033 1.7.2.2 matt if (((__uintptr_t)data & 3) == 0) {
1034 1.7.2.2 matt while (datalen > 3) {
1035 1.7.2.2 matt HWRITE4(hp, SDHC_DATA, *(uint32_t *)data);
1036 1.7.2.2 matt data += 4;
1037 1.7.2.2 matt datalen -= 4;
1038 1.7.2.2 matt }
1039 1.7.2.2 matt if (datalen > 1) {
1040 1.7.2.2 matt HWRITE2(hp, SDHC_DATA, *(uint16_t *)data);
1041 1.7.2.2 matt data += 2;
1042 1.7.2.2 matt datalen -= 2;
1043 1.7.2.2 matt }
1044 1.7.2.2 matt if (datalen > 0) {
1045 1.7.2.2 matt HWRITE1(hp, SDHC_DATA, *data);
1046 1.7.2.2 matt data += 1;
1047 1.7.2.2 matt datalen -= 1;
1048 1.7.2.2 matt }
1049 1.7.2.2 matt } else if (((__uintptr_t)data & 1) == 0) {
1050 1.7.2.2 matt while (datalen > 1) {
1051 1.7.2.2 matt HWRITE2(hp, SDHC_DATA, *(uint16_t *)data);
1052 1.7.2.2 matt data += 2;
1053 1.7.2.2 matt datalen -= 2;
1054 1.7.2.2 matt }
1055 1.7.2.2 matt if (datalen > 0) {
1056 1.7.2.2 matt HWRITE1(hp, SDHC_DATA, *data);
1057 1.7.2.2 matt data += 1;
1058 1.7.2.2 matt datalen -= 1;
1059 1.7.2.2 matt }
1060 1.7.2.2 matt } else {
1061 1.7.2.2 matt while (datalen > 0) {
1062 1.7.2.2 matt HWRITE1(hp, SDHC_DATA, *data);
1063 1.7.2.2 matt data += 1;
1064 1.7.2.2 matt datalen -= 1;
1065 1.7.2.2 matt }
1066 1.7.2.2 matt }
1067 1.7.2.2 matt }
1068 1.7.2.2 matt
1069 1.7.2.2 matt /* Prepare for another command. */
1070 1.7.2.2 matt static int
1071 1.7.2.2 matt sdhc_soft_reset(struct sdhc_host *hp, int mask)
1072 1.7.2.2 matt {
1073 1.7.2.2 matt int timo;
1074 1.7.2.2 matt
1075 1.7.2.2 matt DPRINTF(1,("%s: software reset reg=%08x\n", HDEVNAME(hp), mask));
1076 1.7.2.2 matt
1077 1.7.2.2 matt HWRITE1(hp, SDHC_SOFTWARE_RESET, mask);
1078 1.7.2.2 matt for (timo = 10; timo > 0; timo--) {
1079 1.7.2.2 matt if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
1080 1.7.2.2 matt break;
1081 1.7.2.2 matt sdmmc_delay(10000);
1082 1.7.2.2 matt HWRITE1(hp, SDHC_SOFTWARE_RESET, 0);
1083 1.7.2.2 matt }
1084 1.7.2.2 matt if (timo == 0) {
1085 1.7.2.2 matt DPRINTF(1,("%s: timeout reg=%08x\n", HDEVNAME(hp),
1086 1.7.2.2 matt HREAD1(hp, SDHC_SOFTWARE_RESET)));
1087 1.7.2.2 matt HWRITE1(hp, SDHC_SOFTWARE_RESET, 0);
1088 1.7.2.2 matt return ETIMEDOUT;
1089 1.7.2.2 matt }
1090 1.7.2.2 matt
1091 1.7.2.2 matt return 0;
1092 1.7.2.2 matt }
1093 1.7.2.2 matt
1094 1.7.2.2 matt static int
1095 1.7.2.2 matt sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo)
1096 1.7.2.2 matt {
1097 1.7.2.2 matt int status;
1098 1.7.2.2 matt
1099 1.7.2.2 matt mask |= SDHC_ERROR_INTERRUPT;
1100 1.7.2.2 matt
1101 1.7.2.2 matt mutex_enter(&hp->intr_mtx);
1102 1.7.2.2 matt status = hp->intr_status & mask;
1103 1.7.2.2 matt while (status == 0) {
1104 1.7.2.2 matt if (cv_timedwait(&hp->intr_cv, &hp->intr_mtx, timo)
1105 1.7.2.2 matt == EWOULDBLOCK) {
1106 1.7.2.2 matt status |= SDHC_ERROR_INTERRUPT;
1107 1.7.2.2 matt break;
1108 1.7.2.2 matt }
1109 1.7.2.2 matt status = hp->intr_status & mask;
1110 1.7.2.2 matt }
1111 1.7.2.2 matt hp->intr_status &= ~status;
1112 1.7.2.2 matt
1113 1.7.2.2 matt DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status,
1114 1.7.2.2 matt hp->intr_error_status));
1115 1.7.2.2 matt
1116 1.7.2.2 matt /* Command timeout has higher priority than command complete. */
1117 1.7.2.2 matt if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
1118 1.7.2.2 matt hp->intr_error_status = 0;
1119 1.7.2.2 matt (void)sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
1120 1.7.2.2 matt status = 0;
1121 1.7.2.2 matt }
1122 1.7.2.2 matt mutex_exit(&hp->intr_mtx);
1123 1.7.2.2 matt
1124 1.7.2.2 matt return status;
1125 1.7.2.2 matt }
1126 1.7.2.2 matt
1127 1.7.2.2 matt /*
1128 1.7.2.2 matt * Established by attachment driver at interrupt priority IPL_SDMMC.
1129 1.7.2.2 matt */
1130 1.7.2.2 matt int
1131 1.7.2.2 matt sdhc_intr(void *arg)
1132 1.7.2.2 matt {
1133 1.7.2.2 matt struct sdhc_softc *sc = (struct sdhc_softc *)arg;
1134 1.7.2.2 matt struct sdhc_host *hp;
1135 1.7.2.2 matt int host;
1136 1.7.2.2 matt int done = 0;
1137 1.7.2.2 matt uint16_t status;
1138 1.7.2.2 matt uint16_t error;
1139 1.7.2.2 matt
1140 1.7.2.2 matt /* We got an interrupt, but we don't know from which slot. */
1141 1.7.2.2 matt for (host = 0; host < sc->sc_nhosts; host++) {
1142 1.7.2.2 matt hp = sc->sc_host[host];
1143 1.7.2.2 matt if (hp == NULL)
1144 1.7.2.2 matt continue;
1145 1.7.2.2 matt
1146 1.7.2.2 matt /* Find out which interrupts are pending. */
1147 1.7.2.2 matt status = HREAD2(hp, SDHC_NINTR_STATUS);
1148 1.7.2.2 matt if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
1149 1.7.2.2 matt continue; /* no interrupt for us */
1150 1.7.2.2 matt
1151 1.7.2.2 matt /* Acknowledge the interrupts we are about to handle. */
1152 1.7.2.2 matt HWRITE2(hp, SDHC_NINTR_STATUS, status);
1153 1.7.2.2 matt DPRINTF(2,("%s: interrupt status=%x\n", HDEVNAME(hp),
1154 1.7.2.2 matt status));
1155 1.7.2.2 matt
1156 1.7.2.2 matt if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
1157 1.7.2.2 matt continue;
1158 1.7.2.2 matt
1159 1.7.2.2 matt /* Claim this interrupt. */
1160 1.7.2.2 matt done = 1;
1161 1.7.2.2 matt
1162 1.7.2.2 matt /*
1163 1.7.2.2 matt * Service error interrupts.
1164 1.7.2.2 matt */
1165 1.7.2.2 matt if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
1166 1.7.2.2 matt /* Acknowledge error interrupts. */
1167 1.7.2.2 matt error = HREAD2(hp, SDHC_EINTR_STATUS);
1168 1.7.2.2 matt HWRITE2(hp, SDHC_EINTR_STATUS, error);
1169 1.7.2.2 matt DPRINTF(2,("%s: error interrupt, status=%x\n",
1170 1.7.2.2 matt HDEVNAME(hp), error));
1171 1.7.2.2 matt
1172 1.7.2.2 matt if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR|
1173 1.7.2.2 matt SDHC_DATA_TIMEOUT_ERROR)) {
1174 1.7.2.2 matt hp->intr_error_status |= error;
1175 1.7.2.2 matt hp->intr_status |= status;
1176 1.7.2.2 matt cv_broadcast(&hp->intr_cv);
1177 1.7.2.2 matt }
1178 1.7.2.2 matt }
1179 1.7.2.2 matt
1180 1.7.2.2 matt /*
1181 1.7.2.2 matt * Wake up the sdmmc event thread to scan for cards.
1182 1.7.2.2 matt */
1183 1.7.2.2 matt if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION))
1184 1.7.2.2 matt sdmmc_needs_discover(hp->sdmmc);
1185 1.7.2.2 matt
1186 1.7.2.2 matt /*
1187 1.7.2.2 matt * Wake up the blocking process to service command
1188 1.7.2.2 matt * related interrupt(s).
1189 1.7.2.2 matt */
1190 1.7.2.2 matt if (ISSET(status, SDHC_BUFFER_READ_READY|
1191 1.7.2.2 matt SDHC_BUFFER_WRITE_READY|SDHC_COMMAND_COMPLETE|
1192 1.7.2.2 matt SDHC_TRANSFER_COMPLETE|SDHC_DMA_INTERRUPT)) {
1193 1.7.2.2 matt hp->intr_status |= status;
1194 1.7.2.2 matt cv_broadcast(&hp->intr_cv);
1195 1.7.2.2 matt }
1196 1.7.2.2 matt
1197 1.7.2.2 matt /*
1198 1.7.2.2 matt * Service SD card interrupts.
1199 1.7.2.2 matt */
1200 1.7.2.2 matt if (ISSET(status, SDHC_CARD_INTERRUPT)) {
1201 1.7.2.2 matt DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp)));
1202 1.7.2.2 matt HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
1203 1.7.2.2 matt sdmmc_card_intr(hp->sdmmc);
1204 1.7.2.2 matt }
1205 1.7.2.2 matt }
1206 1.7.2.2 matt
1207 1.7.2.2 matt return done;
1208 1.7.2.2 matt }
1209 1.7.2.2 matt
1210 1.7.2.2 matt #ifdef SDHC_DEBUG
1211 1.7.2.2 matt void
1212 1.7.2.2 matt sdhc_dump_regs(struct sdhc_host *hp)
1213 1.7.2.2 matt {
1214 1.7.2.2 matt
1215 1.7.2.2 matt printf("0x%02x PRESENT_STATE: %x\n", SDHC_PRESENT_STATE,
1216 1.7.2.2 matt HREAD4(hp, SDHC_PRESENT_STATE));
1217 1.7.2.2 matt printf("0x%02x POWER_CTL: %x\n", SDHC_POWER_CTL,
1218 1.7.2.2 matt HREAD1(hp, SDHC_POWER_CTL));
1219 1.7.2.2 matt printf("0x%02x NINTR_STATUS: %x\n", SDHC_NINTR_STATUS,
1220 1.7.2.2 matt HREAD2(hp, SDHC_NINTR_STATUS));
1221 1.7.2.2 matt printf("0x%02x EINTR_STATUS: %x\n", SDHC_EINTR_STATUS,
1222 1.7.2.2 matt HREAD2(hp, SDHC_EINTR_STATUS));
1223 1.7.2.2 matt printf("0x%02x NINTR_STATUS_EN: %x\n", SDHC_NINTR_STATUS_EN,
1224 1.7.2.2 matt HREAD2(hp, SDHC_NINTR_STATUS_EN));
1225 1.7.2.2 matt printf("0x%02x EINTR_STATUS_EN: %x\n", SDHC_EINTR_STATUS_EN,
1226 1.7.2.2 matt HREAD2(hp, SDHC_EINTR_STATUS_EN));
1227 1.7.2.2 matt printf("0x%02x NINTR_SIGNAL_EN: %x\n", SDHC_NINTR_SIGNAL_EN,
1228 1.7.2.2 matt HREAD2(hp, SDHC_NINTR_SIGNAL_EN));
1229 1.7.2.2 matt printf("0x%02x EINTR_SIGNAL_EN: %x\n", SDHC_EINTR_SIGNAL_EN,
1230 1.7.2.2 matt HREAD2(hp, SDHC_EINTR_SIGNAL_EN));
1231 1.7.2.2 matt printf("0x%02x CAPABILITIES: %x\n", SDHC_CAPABILITIES,
1232 1.7.2.2 matt HREAD4(hp, SDHC_CAPABILITIES));
1233 1.7.2.2 matt printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES,
1234 1.7.2.2 matt HREAD4(hp, SDHC_MAX_CAPABILITIES));
1235 1.7.2.2 matt }
1236 1.7.2.2 matt #endif
1237