Fri Oct 2 04:33:59 2009 UTC ()
Typo in comment.


(uebayasi)
diff -r1.2 -r1.3 src/sys/dev/sdmmc/sdhc.c

cvs diff -r1.2 -r1.3 src/sys/dev/sdmmc/sdhc.c (switch to unified diff)

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