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