Thu Jul 23 23:52:54 2015 UTC ()
Add a SDHC_FLAG_NO_TIMEOUT quirk to handle spurious timeouts on Tegra K1
during data transfers. While here, increase the soft timeout for DMA
transfers from 1s to 3s.


(jmcneill)
diff -r1.60 -r1.61 src/sys/dev/sdmmc/sdhc.c
diff -r1.18 -r1.19 src/sys/dev/sdmmc/sdhcvar.h

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

--- src/sys/dev/sdmmc/sdhc.c 2015/07/22 09:54:42 1.60
+++ src/sys/dev/sdmmc/sdhc.c 2015/07/23 23:52:54 1.61
@@ -1,1844 +1,1855 @@ @@ -1,1844 +1,1855 @@
1/* $NetBSD: sdhc.c,v 1.60 2015/07/22 09:54:42 skrll Exp $ */ 1/* $NetBSD: sdhc.c,v 1.61 2015/07/23 23:52:54 jmcneill 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.60 2015/07/22 09:54:42 skrll Exp $"); 26__KERNEL_RCSID(0, "$NetBSD: sdhc.c,v 1.61 2015/07/23 23:52:54 jmcneill Exp $");
27 27
28#ifdef _KERNEL_OPT 28#ifdef _KERNEL_OPT
29#include "opt_sdmmc.h" 29#include "opt_sdmmc.h"
30#endif 30#endif
31 31
32#include <sys/param.h> 32#include <sys/param.h>
33#include <sys/device.h> 33#include <sys/device.h>
34#include <sys/kernel.h> 34#include <sys/kernel.h>
35#include <sys/kthread.h> 35#include <sys/kthread.h>
36#include <sys/malloc.h> 36#include <sys/malloc.h>
37#include <sys/systm.h> 37#include <sys/systm.h>
38#include <sys/mutex.h> 38#include <sys/mutex.h>
39#include <sys/condvar.h> 39#include <sys/condvar.h>
40 40
41#include <dev/sdmmc/sdhcreg.h> 41#include <dev/sdmmc/sdhcreg.h>
42#include <dev/sdmmc/sdhcvar.h> 42#include <dev/sdmmc/sdhcvar.h>
43#include <dev/sdmmc/sdmmcchip.h> 43#include <dev/sdmmc/sdmmcchip.h>
44#include <dev/sdmmc/sdmmcreg.h> 44#include <dev/sdmmc/sdmmcreg.h>
45#include <dev/sdmmc/sdmmcvar.h> 45#include <dev/sdmmc/sdmmcvar.h>
46 46
47#ifdef SDHC_DEBUG 47#ifdef SDHC_DEBUG
48int sdhcdebug = 1; 48int sdhcdebug = 1;
49#define DPRINTF(n,s) do { if ((n) <= sdhcdebug) printf s; } while (0) 49#define DPRINTF(n,s) do { if ((n) <= sdhcdebug) printf s; } while (0)
50void sdhc_dump_regs(struct sdhc_host *); 50void sdhc_dump_regs(struct sdhc_host *);
51#else 51#else
52#define DPRINTF(n,s) do {} while (0) 52#define DPRINTF(n,s) do {} while (0)
53#endif 53#endif
54 54
55#define SDHC_COMMAND_TIMEOUT hz 55#define SDHC_COMMAND_TIMEOUT hz
56#define SDHC_BUFFER_TIMEOUT hz 56#define SDHC_BUFFER_TIMEOUT hz
57#define SDHC_TRANSFER_TIMEOUT hz 57#define SDHC_TRANSFER_TIMEOUT hz
58#define SDHC_DMA_TIMEOUT hz 58#define SDHC_DMA_TIMEOUT (hz*3)
59 59
60struct sdhc_host { 60struct sdhc_host {
61 struct sdhc_softc *sc; /* host controller device */ 61 struct sdhc_softc *sc; /* host controller device */
62 62
63 bus_space_tag_t iot; /* host register set tag */ 63 bus_space_tag_t iot; /* host register set tag */
64 bus_space_handle_t ioh; /* host register set handle */ 64 bus_space_handle_t ioh; /* host register set handle */
65 bus_size_t ios; /* host register space size */ 65 bus_size_t ios; /* host register space size */
66 bus_dma_tag_t dmat; /* host DMA tag */ 66 bus_dma_tag_t dmat; /* host DMA tag */
67 67
68 device_t sdmmc; /* generic SD/MMC device */ 68 device_t sdmmc; /* generic SD/MMC device */
69 69
70 struct kmutex host_mtx; 70 struct kmutex host_mtx;
71 71
72 u_int clkbase; /* base clock frequency in KHz */ 72 u_int clkbase; /* base clock frequency in KHz */
73 int maxblklen; /* maximum block length */ 73 int maxblklen; /* maximum block length */
74 uint32_t ocr; /* OCR value from capabilities */ 74 uint32_t ocr; /* OCR value from capabilities */
75 75
76 uint8_t regs[14]; /* host controller state */ 76 uint8_t regs[14]; /* host controller state */
77 77
78 uint16_t intr_status; /* soft interrupt status */ 78 uint16_t intr_status; /* soft interrupt status */
79 uint16_t intr_error_status; /* soft error status */ 79 uint16_t intr_error_status; /* soft error status */
80 struct kmutex intr_mtx; 80 struct kmutex intr_mtx;
81 struct kcondvar intr_cv; 81 struct kcondvar intr_cv;
82 82
83 int specver; /* spec. version */ 83 int specver; /* spec. version */
84 84
85 uint32_t flags; /* flags for this host */ 85 uint32_t flags; /* flags for this host */
86#define SHF_USE_DMA 0x0001 86#define SHF_USE_DMA 0x0001
87#define SHF_USE_4BIT_MODE 0x0002 87#define SHF_USE_4BIT_MODE 0x0002
88#define SHF_USE_8BIT_MODE 0x0004 88#define SHF_USE_8BIT_MODE 0x0004
89#define SHF_MODE_DMAEN 0x0008 /* needs SDHC_DMA_ENABLE in mode */ 89#define SHF_MODE_DMAEN 0x0008 /* needs SDHC_DMA_ENABLE in mode */
90}; 90};
91 91
92#define HDEVNAME(hp) (device_xname((hp)->sc->sc_dev)) 92#define HDEVNAME(hp) (device_xname((hp)->sc->sc_dev))
93 93
94static uint8_t 94static uint8_t
95hread1(struct sdhc_host *hp, bus_size_t reg) 95hread1(struct sdhc_host *hp, bus_size_t reg)
96{ 96{
97 97
98 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) 98 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
99 return bus_space_read_1(hp->iot, hp->ioh, reg); 99 return bus_space_read_1(hp->iot, hp->ioh, reg);
100 return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 3)); 100 return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 3));
101} 101}
102 102
103static uint16_t 103static uint16_t
104hread2(struct sdhc_host *hp, bus_size_t reg) 104hread2(struct sdhc_host *hp, bus_size_t reg)
105{ 105{
106 106
107 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) 107 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
108 return bus_space_read_2(hp->iot, hp->ioh, reg); 108 return bus_space_read_2(hp->iot, hp->ioh, reg);
109 return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 2)); 109 return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 2));
110} 110}
111 111
112#define HREAD1(hp, reg) hread1(hp, reg) 112#define HREAD1(hp, reg) hread1(hp, reg)
113#define HREAD2(hp, reg) hread2(hp, reg) 113#define HREAD2(hp, reg) hread2(hp, reg)
114#define HREAD4(hp, reg) \ 114#define HREAD4(hp, reg) \
115 (bus_space_read_4((hp)->iot, (hp)->ioh, (reg))) 115 (bus_space_read_4((hp)->iot, (hp)->ioh, (reg)))
116 116
117 117
118static void 118static void
119hwrite1(struct sdhc_host *hp, bus_size_t o, uint8_t val) 119hwrite1(struct sdhc_host *hp, bus_size_t o, uint8_t val)
120{ 120{
121 121
122 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) { 122 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
123 bus_space_write_1(hp->iot, hp->ioh, o, val); 123 bus_space_write_1(hp->iot, hp->ioh, o, val);
124 } else { 124 } else {
125 const size_t shift = 8 * (o & 3); 125 const size_t shift = 8 * (o & 3);
126 o &= -4; 126 o &= -4;
127 uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o); 127 uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
128 tmp = (val << shift) | (tmp & ~(0xff << shift)); 128 tmp = (val << shift) | (tmp & ~(0xff << shift));
129 bus_space_write_4(hp->iot, hp->ioh, o, tmp); 129 bus_space_write_4(hp->iot, hp->ioh, o, tmp);
130 } 130 }
131} 131}
132 132
133static void 133static void
134hwrite2(struct sdhc_host *hp, bus_size_t o, uint16_t val) 134hwrite2(struct sdhc_host *hp, bus_size_t o, uint16_t val)
135{ 135{
136 136
137 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) { 137 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
138 bus_space_write_2(hp->iot, hp->ioh, o, val); 138 bus_space_write_2(hp->iot, hp->ioh, o, val);
139 } else { 139 } else {
140 const size_t shift = 8 * (o & 2); 140 const size_t shift = 8 * (o & 2);
141 o &= -4; 141 o &= -4;
142 uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o); 142 uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
143 tmp = (val << shift) | (tmp & ~(0xffff << shift)); 143 tmp = (val << shift) | (tmp & ~(0xffff << shift));
144 bus_space_write_4(hp->iot, hp->ioh, o, tmp); 144 bus_space_write_4(hp->iot, hp->ioh, o, tmp);
145 } 145 }
146} 146}
147 147
148#define HWRITE1(hp, reg, val) hwrite1(hp, reg, val) 148#define HWRITE1(hp, reg, val) hwrite1(hp, reg, val)
149#define HWRITE2(hp, reg, val) hwrite2(hp, reg, val) 149#define HWRITE2(hp, reg, val) hwrite2(hp, reg, val)
150#define HWRITE4(hp, reg, val) \ 150#define HWRITE4(hp, reg, val) \
151 bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val)) 151 bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val))
152 152
153#define HCLR1(hp, reg, bits) \ 153#define HCLR1(hp, reg, bits) \
154 do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits)); while (0) 154 do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits)); while (0)
155#define HCLR2(hp, reg, bits) \ 155#define HCLR2(hp, reg, bits) \
156 do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits)); while (0) 156 do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits)); while (0)
157#define HCLR4(hp, reg, bits) \ 157#define HCLR4(hp, reg, bits) \
158 do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) & ~(bits)); while (0) 158 do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) & ~(bits)); while (0)
159#define HSET1(hp, reg, bits) \ 159#define HSET1(hp, reg, bits) \
160 do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits)); while (0) 160 do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits)); while (0)
161#define HSET2(hp, reg, bits) \ 161#define HSET2(hp, reg, bits) \
162 do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits)); while (0) 162 do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits)); while (0)
163#define HSET4(hp, reg, bits) \ 163#define HSET4(hp, reg, bits) \
164 do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) | (bits)); while (0) 164 do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) | (bits)); while (0)
165 165
166static int sdhc_host_reset(sdmmc_chipset_handle_t); 166static int sdhc_host_reset(sdmmc_chipset_handle_t);
167static int sdhc_host_reset1(sdmmc_chipset_handle_t); 167static int sdhc_host_reset1(sdmmc_chipset_handle_t);
168static uint32_t sdhc_host_ocr(sdmmc_chipset_handle_t); 168static uint32_t sdhc_host_ocr(sdmmc_chipset_handle_t);
169static int sdhc_host_maxblklen(sdmmc_chipset_handle_t); 169static int sdhc_host_maxblklen(sdmmc_chipset_handle_t);
170static int sdhc_card_detect(sdmmc_chipset_handle_t); 170static int sdhc_card_detect(sdmmc_chipset_handle_t);
171static int sdhc_write_protect(sdmmc_chipset_handle_t); 171static int sdhc_write_protect(sdmmc_chipset_handle_t);
172static int sdhc_bus_power(sdmmc_chipset_handle_t, uint32_t); 172static int sdhc_bus_power(sdmmc_chipset_handle_t, uint32_t);
173static int sdhc_bus_clock(sdmmc_chipset_handle_t, int); 173static int sdhc_bus_clock(sdmmc_chipset_handle_t, int);
174static int sdhc_bus_width(sdmmc_chipset_handle_t, int); 174static int sdhc_bus_width(sdmmc_chipset_handle_t, int);
175static int sdhc_bus_rod(sdmmc_chipset_handle_t, int); 175static int sdhc_bus_rod(sdmmc_chipset_handle_t, int);
176static void sdhc_card_enable_intr(sdmmc_chipset_handle_t, int); 176static void sdhc_card_enable_intr(sdmmc_chipset_handle_t, int);
177static void sdhc_card_intr_ack(sdmmc_chipset_handle_t); 177static void sdhc_card_intr_ack(sdmmc_chipset_handle_t);
178static void sdhc_exec_command(sdmmc_chipset_handle_t, 178static void sdhc_exec_command(sdmmc_chipset_handle_t,
179 struct sdmmc_command *); 179 struct sdmmc_command *);
180static int sdhc_start_command(struct sdhc_host *, struct sdmmc_command *); 180static int sdhc_start_command(struct sdhc_host *, struct sdmmc_command *);
181static int sdhc_wait_state(struct sdhc_host *, uint32_t, uint32_t); 181static int sdhc_wait_state(struct sdhc_host *, uint32_t, uint32_t);
182static int sdhc_soft_reset(struct sdhc_host *, int); 182static int sdhc_soft_reset(struct sdhc_host *, int);
183static int sdhc_wait_intr(struct sdhc_host *, int, int); 183static int sdhc_wait_intr(struct sdhc_host *, int, int);
184static void sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *); 184static void sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *);
185static int sdhc_transfer_data_dma(struct sdhc_host *, struct sdmmc_command *); 185static int sdhc_transfer_data_dma(struct sdhc_host *, struct sdmmc_command *);
186static int sdhc_transfer_data_pio(struct sdhc_host *, struct sdmmc_command *); 186static int sdhc_transfer_data_pio(struct sdhc_host *, struct sdmmc_command *);
187static void sdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int); 187static void sdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
188static void sdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int); 188static void sdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
189static void esdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int); 189static void esdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
190static void esdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int); 190static void esdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
191 191
192static struct sdmmc_chip_functions sdhc_functions = { 192static struct sdmmc_chip_functions sdhc_functions = {
193 /* host controller reset */ 193 /* host controller reset */
194 .host_reset = sdhc_host_reset, 194 .host_reset = sdhc_host_reset,
195 195
196 /* host controller capabilities */ 196 /* host controller capabilities */
197 .host_ocr = sdhc_host_ocr, 197 .host_ocr = sdhc_host_ocr,
198 .host_maxblklen = sdhc_host_maxblklen, 198 .host_maxblklen = sdhc_host_maxblklen,
199 199
200 /* card detection */ 200 /* card detection */
201 .card_detect = sdhc_card_detect, 201 .card_detect = sdhc_card_detect,
202 202
203 /* write protect */ 203 /* write protect */
204 .write_protect = sdhc_write_protect, 204 .write_protect = sdhc_write_protect,
205 205
206 /* bus power, clock frequency, width and ROD(OpenDrain/PushPull) */ 206 /* bus power, clock frequency, width and ROD(OpenDrain/PushPull) */
207 .bus_power = sdhc_bus_power, 207 .bus_power = sdhc_bus_power,
208 .bus_clock = sdhc_bus_clock, 208 .bus_clock = sdhc_bus_clock,
209 .bus_width = sdhc_bus_width, 209 .bus_width = sdhc_bus_width,
210 .bus_rod = sdhc_bus_rod, 210 .bus_rod = sdhc_bus_rod,
211 211
212 /* command execution */ 212 /* command execution */
213 .exec_command = sdhc_exec_command, 213 .exec_command = sdhc_exec_command,
214 214
215 /* card interrupt */ 215 /* card interrupt */
216 .card_enable_intr = sdhc_card_enable_intr, 216 .card_enable_intr = sdhc_card_enable_intr,
217 .card_intr_ack = sdhc_card_intr_ack 217 .card_intr_ack = sdhc_card_intr_ack
218}; 218};
219 219
220static int 220static int
221sdhc_cfprint(void *aux, const char *pnp) 221sdhc_cfprint(void *aux, const char *pnp)
222{ 222{
223 const struct sdmmcbus_attach_args * const saa = aux; 223 const struct sdmmcbus_attach_args * const saa = aux;
224 const struct sdhc_host * const hp = saa->saa_sch; 224 const struct sdhc_host * const hp = saa->saa_sch;
225 225
226 if (pnp) { 226 if (pnp) {
227 aprint_normal("sdmmc at %s", pnp); 227 aprint_normal("sdmmc at %s", pnp);
228 } 228 }
229 for (size_t host = 0; host < hp->sc->sc_nhosts; host++) { 229 for (size_t host = 0; host < hp->sc->sc_nhosts; host++) {
230 if (hp->sc->sc_host[host] == hp) { 230 if (hp->sc->sc_host[host] == hp) {
231 aprint_normal(" slot %zu", host); 231 aprint_normal(" slot %zu", host);
232 } 232 }
233 } 233 }
234 234
235 return UNCONF; 235 return UNCONF;
236} 236}
237 237
238/* 238/*
239 * Called by attachment driver. For each SD card slot there is one SD 239 * Called by attachment driver. For each SD card slot there is one SD
240 * host controller standard register set. (1.3) 240 * host controller standard register set. (1.3)
241 */ 241 */
242int 242int
243sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot, 243sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot,
244 bus_space_handle_t ioh, bus_size_t iosize) 244 bus_space_handle_t ioh, bus_size_t iosize)
245{ 245{
246 struct sdmmcbus_attach_args saa; 246 struct sdmmcbus_attach_args saa;
247 struct sdhc_host *hp; 247 struct sdhc_host *hp;
248 uint32_t caps; 248 uint32_t caps;
249 uint16_t sdhcver; 249 uint16_t sdhcver;
250 250
251 /* Allocate one more host structure. */ 251 /* Allocate one more host structure. */
252 hp = malloc(sizeof(struct sdhc_host), M_DEVBUF, M_WAITOK|M_ZERO); 252 hp = malloc(sizeof(struct sdhc_host), M_DEVBUF, M_WAITOK|M_ZERO);
253 if (hp == NULL) { 253 if (hp == NULL) {
254 aprint_error_dev(sc->sc_dev, 254 aprint_error_dev(sc->sc_dev,
255 "couldn't alloc memory (sdhc host)\n"); 255 "couldn't alloc memory (sdhc host)\n");
256 goto err1; 256 goto err1;
257 } 257 }
258 sc->sc_host[sc->sc_nhosts++] = hp; 258 sc->sc_host[sc->sc_nhosts++] = hp;
259 259
260 /* Fill in the new host structure. */ 260 /* Fill in the new host structure. */
261 hp->sc = sc; 261 hp->sc = sc;
262 hp->iot = iot; 262 hp->iot = iot;
263 hp->ioh = ioh; 263 hp->ioh = ioh;
264 hp->ios = iosize; 264 hp->ios = iosize;
265 hp->dmat = sc->sc_dmat; 265 hp->dmat = sc->sc_dmat;
266 266
267 mutex_init(&hp->host_mtx, MUTEX_DEFAULT, IPL_SDMMC); 267 mutex_init(&hp->host_mtx, MUTEX_DEFAULT, IPL_SDMMC);
268 mutex_init(&hp->intr_mtx, MUTEX_DEFAULT, IPL_SDMMC); 268 mutex_init(&hp->intr_mtx, MUTEX_DEFAULT, IPL_SDMMC);
269 cv_init(&hp->intr_cv, "sdhcintr"); 269 cv_init(&hp->intr_cv, "sdhcintr");
270 270
271 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 271 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
272 sdhcver = HREAD4(hp, SDHC_ESDHC_HOST_CTL_VERSION); 272 sdhcver = HREAD4(hp, SDHC_ESDHC_HOST_CTL_VERSION);
273 } else { 273 } else {
274 sdhcver = HREAD2(hp, SDHC_HOST_CTL_VERSION); 274 sdhcver = HREAD2(hp, SDHC_HOST_CTL_VERSION);
275 } 275 }
276 aprint_normal_dev(sc->sc_dev, "SDHC "); 276 aprint_normal_dev(sc->sc_dev, "SDHC ");
277 hp->specver = SDHC_SPEC_VERSION(sdhcver); 277 hp->specver = SDHC_SPEC_VERSION(sdhcver);
278 switch (SDHC_SPEC_VERSION(sdhcver)) { 278 switch (SDHC_SPEC_VERSION(sdhcver)) {
279 case SDHC_SPEC_VERS_100: 279 case SDHC_SPEC_VERS_100:
280 aprint_normal("1.0"); 280 aprint_normal("1.0");
281 break; 281 break;
282 282
283 case SDHC_SPEC_VERS_200: 283 case SDHC_SPEC_VERS_200:
284 aprint_normal("2.0"); 284 aprint_normal("2.0");
285 break; 285 break;
286 286
287 case SDHC_SPEC_VERS_300: 287 case SDHC_SPEC_VERS_300:
288 aprint_normal("3.0"); 288 aprint_normal("3.0");
289 break; 289 break;
290 290
291 case SDHC_SPEC_VERS_400: 291 case SDHC_SPEC_VERS_400:
292 aprint_normal("4.0"); 292 aprint_normal("4.0");
293 break; 293 break;
294 294
295 default: 295 default:
296 aprint_normal("unknown version(0x%x)", 296 aprint_normal("unknown version(0x%x)",
297 SDHC_SPEC_VERSION(sdhcver)); 297 SDHC_SPEC_VERSION(sdhcver));
298 break; 298 break;
299 } 299 }
300 aprint_normal(", rev %u", SDHC_VENDOR_VERSION(sdhcver)); 300 aprint_normal(", rev %u", SDHC_VENDOR_VERSION(sdhcver));
301 301
302 /* 302 /*
303 * Reset the host controller and enable interrupts. 303 * Reset the host controller and enable interrupts.
304 */ 304 */
305 (void)sdhc_host_reset(hp); 305 (void)sdhc_host_reset(hp);
306 306
307 /* Determine host capabilities. */ 307 /* Determine host capabilities. */
308 if (ISSET(sc->sc_flags, SDHC_FLAG_HOSTCAPS)) { 308 if (ISSET(sc->sc_flags, SDHC_FLAG_HOSTCAPS)) {
309 caps = sc->sc_caps; 309 caps = sc->sc_caps;
310 } else { 310 } else {
311 mutex_enter(&hp->host_mtx); 311 mutex_enter(&hp->host_mtx);
312 caps = HREAD4(hp, SDHC_CAPABILITIES); 312 caps = HREAD4(hp, SDHC_CAPABILITIES);
313 mutex_exit(&hp->host_mtx); 313 mutex_exit(&hp->host_mtx);
314 } 314 }
315 315
316 /* 316 /*
317 * Use DMA if the host system and the controller support it. 317 * Use DMA if the host system and the controller support it.
318 * Suports integrated or external DMA egine, with or without 318 * Suports integrated or external DMA egine, with or without
319 * SDHC_DMA_ENABLE in the command. 319 * SDHC_DMA_ENABLE in the command.
320 */ 320 */
321 if (ISSET(sc->sc_flags, SDHC_FLAG_FORCE_DMA) || 321 if (ISSET(sc->sc_flags, SDHC_FLAG_FORCE_DMA) ||
322 (ISSET(sc->sc_flags, SDHC_FLAG_USE_DMA && 322 (ISSET(sc->sc_flags, SDHC_FLAG_USE_DMA &&
323 ISSET(caps, SDHC_DMA_SUPPORT)))) { 323 ISSET(caps, SDHC_DMA_SUPPORT)))) {
324 SET(hp->flags, SHF_USE_DMA); 324 SET(hp->flags, SHF_USE_DMA);
325 if (!ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA) || 325 if (!ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA) ||
326 ISSET(sc->sc_flags, SDHC_FLAG_EXTDMA_DMAEN)) 326 ISSET(sc->sc_flags, SDHC_FLAG_EXTDMA_DMAEN))
327 SET(hp->flags, SHF_MODE_DMAEN); 327 SET(hp->flags, SHF_MODE_DMAEN);
328 328
329 aprint_normal(", DMA"); 329 aprint_normal(", DMA");
330 } else { 330 } else {
331 aprint_normal(", PIO"); 331 aprint_normal(", PIO");
332 } 332 }
333 333
334 /* 334 /*
335 * Determine the base clock frequency. (2.2.24) 335 * Determine the base clock frequency. (2.2.24)
336 */ 336 */
337 if (hp->specver >= SDHC_SPEC_VERS_300) { 337 if (hp->specver >= SDHC_SPEC_VERS_300) {
338 hp->clkbase = SDHC_BASE_V3_FREQ_KHZ(caps); 338 hp->clkbase = SDHC_BASE_V3_FREQ_KHZ(caps);
339 } else { 339 } else {
340 hp->clkbase = SDHC_BASE_FREQ_KHZ(caps); 340 hp->clkbase = SDHC_BASE_FREQ_KHZ(caps);
341 } 341 }
342 if (hp->clkbase == 0 || 342 if (hp->clkbase == 0 ||
343 ISSET(sc->sc_flags, SDHC_FLAG_NO_CLKBASE)) { 343 ISSET(sc->sc_flags, SDHC_FLAG_NO_CLKBASE)) {
344 if (sc->sc_clkbase == 0) { 344 if (sc->sc_clkbase == 0) {
345 /* The attachment driver must tell us. */ 345 /* The attachment driver must tell us. */
346 aprint_error_dev(sc->sc_dev, 346 aprint_error_dev(sc->sc_dev,
347 "unknown base clock frequency\n"); 347 "unknown base clock frequency\n");
348 goto err; 348 goto err;
349 } 349 }
350 hp->clkbase = sc->sc_clkbase; 350 hp->clkbase = sc->sc_clkbase;
351 } 351 }
352 if (hp->clkbase < 10000 || hp->clkbase > 10000 * 256) { 352 if (hp->clkbase < 10000 || hp->clkbase > 10000 * 256) {
353 /* SDHC 1.0 supports only 10-63 MHz. */ 353 /* SDHC 1.0 supports only 10-63 MHz. */
354 aprint_error_dev(sc->sc_dev, 354 aprint_error_dev(sc->sc_dev,
355 "base clock frequency out of range: %u MHz\n", 355 "base clock frequency out of range: %u MHz\n",
356 hp->clkbase / 1000); 356 hp->clkbase / 1000);
357 goto err; 357 goto err;
358 } 358 }
359 aprint_normal(", %u kHz", hp->clkbase); 359 aprint_normal(", %u kHz", hp->clkbase);
360 360
361 /* 361 /*
362 * XXX Set the data timeout counter value according to 362 * XXX Set the data timeout counter value according to
363 * capabilities. (2.2.15) 363 * capabilities. (2.2.15)
364 */ 364 */
365 HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX); 365 HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
366#if 1 366#if 1
367 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) 367 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
368 HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16); 368 HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
369#endif 369#endif
370 370
371 if (ISSET(caps, SDHC_EMBEDDED_SLOT)) 371 if (ISSET(caps, SDHC_EMBEDDED_SLOT))
372 aprint_normal(", embedded slot"); 372 aprint_normal(", embedded slot");
373 373
374 /* 374 /*
375 * Determine SD bus voltage levels supported by the controller. 375 * Determine SD bus voltage levels supported by the controller.
376 */ 376 */
377 aprint_normal(","); 377 aprint_normal(",");
378 if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V) && 378 if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V) &&
379 (hp->specver < SDHC_SPEC_VERS_300 || 379 (hp->specver < SDHC_SPEC_VERS_300 ||
380 ISSET(caps, SDHC_EMBEDDED_SLOT))) { 380 ISSET(caps, SDHC_EMBEDDED_SLOT))) {
381 SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V); 381 SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V);
382 aprint_normal(" 1.8V"); 382 aprint_normal(" 1.8V");
383 } 383 }
384 if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V)) { 384 if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V)) {
385 SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V); 385 SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V);
386 aprint_normal(" 3.0V"); 386 aprint_normal(" 3.0V");
387 } 387 }
388 if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V)) { 388 if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V)) {
389 SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V); 389 SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V);
390 aprint_normal(" 3.3V"); 390 aprint_normal(" 3.3V");
391 } 391 }
392 392
393 /* 393 /*
394 * Determine the maximum block length supported by the host 394 * Determine the maximum block length supported by the host
395 * controller. (2.2.24) 395 * controller. (2.2.24)
396 */ 396 */
397 switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) { 397 switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) {
398 case SDHC_MAX_BLK_LEN_512: 398 case SDHC_MAX_BLK_LEN_512:
399 hp->maxblklen = 512; 399 hp->maxblklen = 512;
400 break; 400 break;
401 401
402 case SDHC_MAX_BLK_LEN_1024: 402 case SDHC_MAX_BLK_LEN_1024:
403 hp->maxblklen = 1024; 403 hp->maxblklen = 1024;
404 break; 404 break;
405 405
406 case SDHC_MAX_BLK_LEN_2048: 406 case SDHC_MAX_BLK_LEN_2048:
407 hp->maxblklen = 2048; 407 hp->maxblklen = 2048;
408 break; 408 break;
409 409
410 case SDHC_MAX_BLK_LEN_4096: 410 case SDHC_MAX_BLK_LEN_4096:
411 hp->maxblklen = 4096; 411 hp->maxblklen = 4096;
412 break; 412 break;
413 413
414 default: 414 default:
415 aprint_error_dev(sc->sc_dev, "max block length unknown\n"); 415 aprint_error_dev(sc->sc_dev, "max block length unknown\n");
416 goto err; 416 goto err;
417 } 417 }
418 aprint_normal(", %u byte blocks", hp->maxblklen); 418 aprint_normal(", %u byte blocks", hp->maxblklen);
419 aprint_normal("\n"); 419 aprint_normal("\n");
420 420
421 /* 421 /*
422 * Attach the generic SD/MMC bus driver. (The bus driver must 422 * Attach the generic SD/MMC bus driver. (The bus driver must
423 * not invoke any chipset functions before it is attached.) 423 * not invoke any chipset functions before it is attached.)
424 */ 424 */
425 memset(&saa, 0, sizeof(saa)); 425 memset(&saa, 0, sizeof(saa));
426 saa.saa_busname = "sdmmc"; 426 saa.saa_busname = "sdmmc";
427 saa.saa_sct = &sdhc_functions; 427 saa.saa_sct = &sdhc_functions;
428 saa.saa_sch = hp; 428 saa.saa_sch = hp;
429 saa.saa_dmat = hp->dmat; 429 saa.saa_dmat = hp->dmat;
430 saa.saa_clkmax = hp->clkbase; 430 saa.saa_clkmax = hp->clkbase;
431 if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_CGM)) 431 if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_CGM))
432 saa.saa_clkmin = hp->clkbase / 256 / 2046; 432 saa.saa_clkmin = hp->clkbase / 256 / 2046;
433 else if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_DVS)) 433 else if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_DVS))
434 saa.saa_clkmin = hp->clkbase / 256 / 16; 434 saa.saa_clkmin = hp->clkbase / 256 / 16;
435 else if (hp->sc->sc_clkmsk != 0) 435 else if (hp->sc->sc_clkmsk != 0)
436 saa.saa_clkmin = hp->clkbase / (hp->sc->sc_clkmsk >> 436 saa.saa_clkmin = hp->clkbase / (hp->sc->sc_clkmsk >>
437 (ffs(hp->sc->sc_clkmsk) - 1)); 437 (ffs(hp->sc->sc_clkmsk) - 1));
438 else if (hp->specver >= SDHC_SPEC_VERS_300) 438 else if (hp->specver >= SDHC_SPEC_VERS_300)
439 saa.saa_clkmin = hp->clkbase / 0x3ff; 439 saa.saa_clkmin = hp->clkbase / 0x3ff;
440 else 440 else
441 saa.saa_clkmin = hp->clkbase / 256; 441 saa.saa_clkmin = hp->clkbase / 256;
442 saa.saa_caps = SMC_CAPS_4BIT_MODE|SMC_CAPS_AUTO_STOP; 442 saa.saa_caps = SMC_CAPS_4BIT_MODE|SMC_CAPS_AUTO_STOP;
443 if (ISSET(sc->sc_flags, SDHC_FLAG_8BIT_MODE)) 443 if (ISSET(sc->sc_flags, SDHC_FLAG_8BIT_MODE))
444 saa.saa_caps |= SMC_CAPS_8BIT_MODE; 444 saa.saa_caps |= SMC_CAPS_8BIT_MODE;
445 if (ISSET(caps, SDHC_HIGH_SPEED_SUPP)) 445 if (ISSET(caps, SDHC_HIGH_SPEED_SUPP))
446 saa.saa_caps |= SMC_CAPS_SD_HIGHSPEED; 446 saa.saa_caps |= SMC_CAPS_SD_HIGHSPEED;
447 if (ISSET(hp->flags, SHF_USE_DMA)) { 447 if (ISSET(hp->flags, SHF_USE_DMA)) {
448 saa.saa_caps |= SMC_CAPS_DMA; 448 saa.saa_caps |= SMC_CAPS_DMA;
449 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) 449 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
450 saa.saa_caps |= SMC_CAPS_MULTI_SEG_DMA; 450 saa.saa_caps |= SMC_CAPS_MULTI_SEG_DMA;
451 } 451 }
452 if (ISSET(sc->sc_flags, SDHC_FLAG_SINGLE_ONLY)) 452 if (ISSET(sc->sc_flags, SDHC_FLAG_SINGLE_ONLY))
453 saa.saa_caps |= SMC_CAPS_SINGLE_ONLY; 453 saa.saa_caps |= SMC_CAPS_SINGLE_ONLY;
454 hp->sdmmc = config_found(sc->sc_dev, &saa, sdhc_cfprint); 454 hp->sdmmc = config_found(sc->sc_dev, &saa, sdhc_cfprint);
455 455
456 return 0; 456 return 0;
457 457
458err: 458err:
459 cv_destroy(&hp->intr_cv); 459 cv_destroy(&hp->intr_cv);
460 mutex_destroy(&hp->intr_mtx); 460 mutex_destroy(&hp->intr_mtx);
461 mutex_destroy(&hp->host_mtx); 461 mutex_destroy(&hp->host_mtx);
462 free(hp, M_DEVBUF); 462 free(hp, M_DEVBUF);
463 sc->sc_host[--sc->sc_nhosts] = NULL; 463 sc->sc_host[--sc->sc_nhosts] = NULL;
464err1: 464err1:
465 return 1; 465 return 1;
466} 466}
467 467
468int 468int
469sdhc_detach(struct sdhc_softc *sc, int flags) 469sdhc_detach(struct sdhc_softc *sc, int flags)
470{ 470{
471 struct sdhc_host *hp; 471 struct sdhc_host *hp;
472 int rv = 0; 472 int rv = 0;
473 473
474 for (size_t n = 0; n < sc->sc_nhosts; n++) { 474 for (size_t n = 0; n < sc->sc_nhosts; n++) {
475 hp = sc->sc_host[n]; 475 hp = sc->sc_host[n];
476 if (hp == NULL) 476 if (hp == NULL)
477 continue; 477 continue;
478 if (hp->sdmmc != NULL) { 478 if (hp->sdmmc != NULL) {
479 rv = config_detach(hp->sdmmc, flags); 479 rv = config_detach(hp->sdmmc, flags);
480 if (rv) 480 if (rv)
481 break; 481 break;
482 hp->sdmmc = NULL; 482 hp->sdmmc = NULL;
483 } 483 }
484 /* disable interrupts */ 484 /* disable interrupts */
485 if ((flags & DETACH_FORCE) == 0) { 485 if ((flags & DETACH_FORCE) == 0) {
486 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) { 486 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
487 HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0); 487 HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
488 } else { 488 } else {
489 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0); 489 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
490 } 490 }
491 sdhc_soft_reset(hp, SDHC_RESET_ALL); 491 sdhc_soft_reset(hp, SDHC_RESET_ALL);
492 } 492 }
493 cv_destroy(&hp->intr_cv); 493 cv_destroy(&hp->intr_cv);
494 mutex_destroy(&hp->intr_mtx); 494 mutex_destroy(&hp->intr_mtx);
495 mutex_destroy(&hp->host_mtx); 495 mutex_destroy(&hp->host_mtx);
496 if (hp->ios > 0) { 496 if (hp->ios > 0) {
497 bus_space_unmap(hp->iot, hp->ioh, hp->ios); 497 bus_space_unmap(hp->iot, hp->ioh, hp->ios);
498 hp->ios = 0; 498 hp->ios = 0;
499 } 499 }
500 free(hp, M_DEVBUF); 500 free(hp, M_DEVBUF);
501 sc->sc_host[n] = NULL; 501 sc->sc_host[n] = NULL;
502 } 502 }
503 503
504 return rv; 504 return rv;
505} 505}
506 506
507bool 507bool
508sdhc_suspend(device_t dev, const pmf_qual_t *qual) 508sdhc_suspend(device_t dev, const pmf_qual_t *qual)
509{ 509{
510 struct sdhc_softc *sc = device_private(dev); 510 struct sdhc_softc *sc = device_private(dev);
511 struct sdhc_host *hp; 511 struct sdhc_host *hp;
512 size_t i; 512 size_t i;
513 513
514 /* XXX poll for command completion or suspend command 514 /* XXX poll for command completion or suspend command
515 * in progress */ 515 * in progress */
516 516
517 /* Save the host controller state. */ 517 /* Save the host controller state. */
518 for (size_t n = 0; n < sc->sc_nhosts; n++) { 518 for (size_t n = 0; n < sc->sc_nhosts; n++) {
519 hp = sc->sc_host[n]; 519 hp = sc->sc_host[n];
520 if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) { 520 if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
521 for (i = 0; i < sizeof hp->regs; i += 4) { 521 for (i = 0; i < sizeof hp->regs; i += 4) {
522 uint32_t v = HREAD4(hp, i); 522 uint32_t v = HREAD4(hp, i);
523 hp->regs[i + 0] = (v >> 0); 523 hp->regs[i + 0] = (v >> 0);
524 hp->regs[i + 1] = (v >> 8); 524 hp->regs[i + 1] = (v >> 8);
525 if (i + 3 < sizeof hp->regs) { 525 if (i + 3 < sizeof hp->regs) {
526 hp->regs[i + 2] = (v >> 16); 526 hp->regs[i + 2] = (v >> 16);
527 hp->regs[i + 3] = (v >> 24); 527 hp->regs[i + 3] = (v >> 24);
528 } 528 }
529 } 529 }
530 } else { 530 } else {
531 for (i = 0; i < sizeof hp->regs; i++) { 531 for (i = 0; i < sizeof hp->regs; i++) {
532 hp->regs[i] = HREAD1(hp, i); 532 hp->regs[i] = HREAD1(hp, i);
533 } 533 }
534 } 534 }
535 } 535 }
536 return true; 536 return true;
537} 537}
538 538
539bool 539bool
540sdhc_resume(device_t dev, const pmf_qual_t *qual) 540sdhc_resume(device_t dev, const pmf_qual_t *qual)
541{ 541{
542 struct sdhc_softc *sc = device_private(dev); 542 struct sdhc_softc *sc = device_private(dev);
543 struct sdhc_host *hp; 543 struct sdhc_host *hp;
544 size_t i; 544 size_t i;
545 545
546 /* Restore the host controller state. */ 546 /* Restore the host controller state. */
547 for (size_t n = 0; n < sc->sc_nhosts; n++) { 547 for (size_t n = 0; n < sc->sc_nhosts; n++) {
548 hp = sc->sc_host[n]; 548 hp = sc->sc_host[n];
549 (void)sdhc_host_reset(hp); 549 (void)sdhc_host_reset(hp);
550 if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) { 550 if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
551 for (i = 0; i < sizeof hp->regs; i += 4) { 551 for (i = 0; i < sizeof hp->regs; i += 4) {
552 if (i + 3 < sizeof hp->regs) { 552 if (i + 3 < sizeof hp->regs) {
553 HWRITE4(hp, i, 553 HWRITE4(hp, i,
554 (hp->regs[i + 0] << 0) 554 (hp->regs[i + 0] << 0)
555 | (hp->regs[i + 1] << 8) 555 | (hp->regs[i + 1] << 8)
556 | (hp->regs[i + 2] << 16) 556 | (hp->regs[i + 2] << 16)
557 | (hp->regs[i + 3] << 24)); 557 | (hp->regs[i + 3] << 24));
558 } else { 558 } else {
559 HWRITE4(hp, i, 559 HWRITE4(hp, i,
560 (hp->regs[i + 0] << 0) 560 (hp->regs[i + 0] << 0)
561 | (hp->regs[i + 1] << 8)); 561 | (hp->regs[i + 1] << 8));
562 } 562 }
563 } 563 }
564 } else { 564 } else {
565 for (i = 0; i < sizeof hp->regs; i++) { 565 for (i = 0; i < sizeof hp->regs; i++) {
566 HWRITE1(hp, i, hp->regs[i]); 566 HWRITE1(hp, i, hp->regs[i]);
567 } 567 }
568 } 568 }
569 } 569 }
570 return true; 570 return true;
571} 571}
572 572
573bool 573bool
574sdhc_shutdown(device_t dev, int flags) 574sdhc_shutdown(device_t dev, int flags)
575{ 575{
576 struct sdhc_softc *sc = device_private(dev); 576 struct sdhc_softc *sc = device_private(dev);
577 struct sdhc_host *hp; 577 struct sdhc_host *hp;
578 578
579 /* XXX chip locks up if we don't disable it before reboot. */ 579 /* XXX chip locks up if we don't disable it before reboot. */
580 for (size_t i = 0; i < sc->sc_nhosts; i++) { 580 for (size_t i = 0; i < sc->sc_nhosts; i++) {
581 hp = sc->sc_host[i]; 581 hp = sc->sc_host[i];
582 (void)sdhc_host_reset(hp); 582 (void)sdhc_host_reset(hp);
583 } 583 }
584 return true; 584 return true;
585} 585}
586 586
587/* 587/*
588 * Reset the host controller. Called during initialization, when 588 * Reset the host controller. Called during initialization, when
589 * cards are removed, upon resume, and during error recovery. 589 * cards are removed, upon resume, and during error recovery.
590 */ 590 */
591static int 591static int
592sdhc_host_reset1(sdmmc_chipset_handle_t sch) 592sdhc_host_reset1(sdmmc_chipset_handle_t sch)
593{ 593{
594 struct sdhc_host *hp = (struct sdhc_host *)sch; 594 struct sdhc_host *hp = (struct sdhc_host *)sch;
595 uint32_t sdhcimask; 595 uint32_t sdhcimask;
596 int error; 596 int error;
597 597
598 /* Don't lock. */ 598 /* Don't lock. */
599 599
600 /* Disable all interrupts. */ 600 /* Disable all interrupts. */
601 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) { 601 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
602 HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0); 602 HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
603 } else { 603 } else {
604 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0); 604 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
605 } 605 }
606 606
607 /* 607 /*
608 * Reset the entire host controller and wait up to 100ms for 608 * Reset the entire host controller and wait up to 100ms for
609 * the controller to clear the reset bit. 609 * the controller to clear the reset bit.
610 */ 610 */
611 error = sdhc_soft_reset(hp, SDHC_RESET_ALL); 611 error = sdhc_soft_reset(hp, SDHC_RESET_ALL);
612 if (error) 612 if (error)
613 goto out; 613 goto out;
614 614
615 /* Set data timeout counter value to max for now. */ 615 /* Set data timeout counter value to max for now. */
616 HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX); 616 HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
617#if 1 617#if 1
618 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) 618 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
619 HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16); 619 HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
620#endif 620#endif
621 621
622 /* Enable interrupts. */ 622 /* Enable interrupts. */
623 mutex_enter(&hp->intr_mtx); 623 mutex_enter(&hp->intr_mtx);
624 sdhcimask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION | 624 sdhcimask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION |
625 SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY | 625 SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY |
626 SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT | 626 SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT |
627 SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE; 627 SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE;
628 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) { 628 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
629 sdhcimask |= SDHC_EINTR_STATUS_MASK << 16; 629 sdhcimask |= SDHC_EINTR_STATUS_MASK << 16;
630 HWRITE4(hp, SDHC_NINTR_STATUS_EN, sdhcimask); 630 HWRITE4(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
631 sdhcimask ^= 631 sdhcimask ^=
632 (SDHC_EINTR_STATUS_MASK ^ SDHC_EINTR_SIGNAL_MASK) << 16; 632 (SDHC_EINTR_STATUS_MASK ^ SDHC_EINTR_SIGNAL_MASK) << 16;
633 sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY; 633 sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
634 HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask); 634 HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
635 } else { 635 } else {
636 HWRITE2(hp, SDHC_NINTR_STATUS_EN, sdhcimask); 636 HWRITE2(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
637 HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK); 637 HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK);
638 sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY; 638 sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
639 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask); 639 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
640 HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK); 640 HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK);
641 } 641 }
642 mutex_exit(&hp->intr_mtx); 642 mutex_exit(&hp->intr_mtx);
643 643
644out: 644out:
645 return error; 645 return error;
646} 646}
647 647
648static int 648static int
649sdhc_host_reset(sdmmc_chipset_handle_t sch) 649sdhc_host_reset(sdmmc_chipset_handle_t sch)
650{ 650{
651 struct sdhc_host *hp = (struct sdhc_host *)sch; 651 struct sdhc_host *hp = (struct sdhc_host *)sch;
652 int error; 652 int error;
653 653
654 mutex_enter(&hp->host_mtx); 654 mutex_enter(&hp->host_mtx);
655 error = sdhc_host_reset1(sch); 655 error = sdhc_host_reset1(sch);
656 mutex_exit(&hp->host_mtx); 656 mutex_exit(&hp->host_mtx);
657 657
658 return error; 658 return error;
659} 659}
660 660
661static uint32_t 661static uint32_t
662sdhc_host_ocr(sdmmc_chipset_handle_t sch) 662sdhc_host_ocr(sdmmc_chipset_handle_t sch)
663{ 663{
664 struct sdhc_host *hp = (struct sdhc_host *)sch; 664 struct sdhc_host *hp = (struct sdhc_host *)sch;
665 665
666 return hp->ocr; 666 return hp->ocr;
667} 667}
668 668
669static int 669static int
670sdhc_host_maxblklen(sdmmc_chipset_handle_t sch) 670sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)
671{ 671{
672 struct sdhc_host *hp = (struct sdhc_host *)sch; 672 struct sdhc_host *hp = (struct sdhc_host *)sch;
673 673
674 return hp->maxblklen; 674 return hp->maxblklen;
675} 675}
676 676
677/* 677/*
678 * Return non-zero if the card is currently inserted. 678 * Return non-zero if the card is currently inserted.
679 */ 679 */
680static int 680static int
681sdhc_card_detect(sdmmc_chipset_handle_t sch) 681sdhc_card_detect(sdmmc_chipset_handle_t sch)
682{ 682{
683 struct sdhc_host *hp = (struct sdhc_host *)sch; 683 struct sdhc_host *hp = (struct sdhc_host *)sch;
684 int r; 684 int r;
685 685
686 if (hp->sc->sc_vendor_card_detect) 686 if (hp->sc->sc_vendor_card_detect)
687 return (*hp->sc->sc_vendor_card_detect)(hp->sc); 687 return (*hp->sc->sc_vendor_card_detect)(hp->sc);
688 688
689 mutex_enter(&hp->host_mtx); 689 mutex_enter(&hp->host_mtx);
690 r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED); 690 r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED);
691 mutex_exit(&hp->host_mtx); 691 mutex_exit(&hp->host_mtx);
692 692
693 return r ? 1 : 0; 693 return r ? 1 : 0;
694} 694}
695 695
696/* 696/*
697 * Return non-zero if the card is currently write-protected. 697 * Return non-zero if the card is currently write-protected.
698 */ 698 */
699static int 699static int
700sdhc_write_protect(sdmmc_chipset_handle_t sch) 700sdhc_write_protect(sdmmc_chipset_handle_t sch)
701{ 701{
702 struct sdhc_host *hp = (struct sdhc_host *)sch; 702 struct sdhc_host *hp = (struct sdhc_host *)sch;
703 int r; 703 int r;
704 704
705 if (hp->sc->sc_vendor_write_protect) 705 if (hp->sc->sc_vendor_write_protect)
706 return (*hp->sc->sc_vendor_write_protect)(hp->sc); 706 return (*hp->sc->sc_vendor_write_protect)(hp->sc);
707 707
708 mutex_enter(&hp->host_mtx); 708 mutex_enter(&hp->host_mtx);
709 r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_WRITE_PROTECT_SWITCH); 709 r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_WRITE_PROTECT_SWITCH);
710 mutex_exit(&hp->host_mtx); 710 mutex_exit(&hp->host_mtx);
711 711
712 return r ? 0 : 1; 712 return r ? 0 : 1;
713} 713}
714 714
715/* 715/*
716 * Set or change SD bus voltage and enable or disable SD bus power. 716 * Set or change SD bus voltage and enable or disable SD bus power.
717 * Return zero on success. 717 * Return zero on success.
718 */ 718 */
719static int 719static int
720sdhc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr) 720sdhc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
721{ 721{
722 struct sdhc_host *hp = (struct sdhc_host *)sch; 722 struct sdhc_host *hp = (struct sdhc_host *)sch;
723 uint8_t vdd; 723 uint8_t vdd;
724 int error = 0; 724 int error = 0;
725 const uint32_t pcmask = 725 const uint32_t pcmask =
726 ~(SDHC_BUS_POWER | (SDHC_VOLTAGE_MASK << SDHC_VOLTAGE_SHIFT)); 726 ~(SDHC_BUS_POWER | (SDHC_VOLTAGE_MASK << SDHC_VOLTAGE_SHIFT));
727 727
728 mutex_enter(&hp->host_mtx); 728 mutex_enter(&hp->host_mtx);
729 729
730 /* 730 /*
731 * Disable bus power before voltage change. 731 * Disable bus power before voltage change.
732 */ 732 */
733 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS) 733 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)
734 && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_PWR0)) 734 && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_PWR0))
735 HWRITE1(hp, SDHC_POWER_CTL, 0); 735 HWRITE1(hp, SDHC_POWER_CTL, 0);
736 736
737 /* If power is disabled, reset the host and return now. */ 737 /* If power is disabled, reset the host and return now. */
738 if (ocr == 0) { 738 if (ocr == 0) {
739 (void)sdhc_host_reset1(hp); 739 (void)sdhc_host_reset1(hp);
740 goto out; 740 goto out;
741 } 741 }
742 742
743 /* 743 /*
744 * Select the lowest voltage according to capabilities. 744 * Select the lowest voltage according to capabilities.
745 */ 745 */
746 ocr &= hp->ocr; 746 ocr &= hp->ocr;
747 if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V)) { 747 if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V)) {
748 vdd = SDHC_VOLTAGE_1_8V; 748 vdd = SDHC_VOLTAGE_1_8V;
749 } else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V)) { 749 } else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V)) {
750 vdd = SDHC_VOLTAGE_3_0V; 750 vdd = SDHC_VOLTAGE_3_0V;
751 } else if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) { 751 } else if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) {
752 vdd = SDHC_VOLTAGE_3_3V; 752 vdd = SDHC_VOLTAGE_3_3V;
753 } else { 753 } else {
754 /* Unsupported voltage level requested. */ 754 /* Unsupported voltage level requested. */
755 error = EINVAL; 755 error = EINVAL;
756 goto out; 756 goto out;
757 } 757 }
758 758
759 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 759 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
760 /* 760 /*
761 * Enable bus power. Wait at least 1 ms (or 74 clocks) plus 761 * Enable bus power. Wait at least 1 ms (or 74 clocks) plus
762 * voltage ramp until power rises. 762 * voltage ramp until power rises.
763 */ 763 */
764 764
765 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_SINGLE_POWER_WRITE)) { 765 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_SINGLE_POWER_WRITE)) {
766 HWRITE1(hp, SDHC_POWER_CTL, 766 HWRITE1(hp, SDHC_POWER_CTL,
767 (vdd << SDHC_VOLTAGE_SHIFT) | SDHC_BUS_POWER); 767 (vdd << SDHC_VOLTAGE_SHIFT) | SDHC_BUS_POWER);
768 } else { 768 } else {
769 HWRITE1(hp, SDHC_POWER_CTL, 769 HWRITE1(hp, SDHC_POWER_CTL,
770 HREAD1(hp, SDHC_POWER_CTL) & pcmask); 770 HREAD1(hp, SDHC_POWER_CTL) & pcmask);
771 sdmmc_delay(1); 771 sdmmc_delay(1);
772 HWRITE1(hp, SDHC_POWER_CTL, 772 HWRITE1(hp, SDHC_POWER_CTL,
773 (vdd << SDHC_VOLTAGE_SHIFT)); 773 (vdd << SDHC_VOLTAGE_SHIFT));
774 sdmmc_delay(1); 774 sdmmc_delay(1);
775 HSET1(hp, SDHC_POWER_CTL, SDHC_BUS_POWER); 775 HSET1(hp, SDHC_POWER_CTL, SDHC_BUS_POWER);
776 sdmmc_delay(10000); 776 sdmmc_delay(10000);
777 } 777 }
778 778
779 /* 779 /*
780 * The host system may not power the bus due to battery low, 780 * The host system may not power the bus due to battery low,
781 * etc. In that case, the host controller should clear the 781 * etc. In that case, the host controller should clear the
782 * bus power bit. 782 * bus power bit.
783 */ 783 */
784 if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) { 784 if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) {
785 error = ENXIO; 785 error = ENXIO;
786 goto out; 786 goto out;
787 } 787 }
788 } 788 }
789 789
790out: 790out:
791 mutex_exit(&hp->host_mtx); 791 mutex_exit(&hp->host_mtx);
792 792
793 return error; 793 return error;
794} 794}
795 795
796/* 796/*
797 * Return the smallest possible base clock frequency divisor value 797 * Return the smallest possible base clock frequency divisor value
798 * for the CLOCK_CTL register to produce `freq' (KHz). 798 * for the CLOCK_CTL register to produce `freq' (KHz).
799 */ 799 */
800static bool 800static bool
801sdhc_clock_divisor(struct sdhc_host *hp, u_int freq, u_int *divp) 801sdhc_clock_divisor(struct sdhc_host *hp, u_int freq, u_int *divp)
802{ 802{
803 u_int div; 803 u_int div;
804 804
805 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_CGM)) { 805 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_CGM)) {
806 for (div = hp->clkbase / freq; div <= 0x3ff; div++) { 806 for (div = hp->clkbase / freq; div <= 0x3ff; div++) {
807 if ((hp->clkbase / div) <= freq) { 807 if ((hp->clkbase / div) <= freq) {
808 *divp = SDHC_SDCLK_CGM 808 *divp = SDHC_SDCLK_CGM
809 | ((div & 0x300) << SDHC_SDCLK_XDIV_SHIFT) 809 | ((div & 0x300) << SDHC_SDCLK_XDIV_SHIFT)
810 | ((div & 0x0ff) << SDHC_SDCLK_DIV_SHIFT); 810 | ((div & 0x0ff) << SDHC_SDCLK_DIV_SHIFT);
811 //freq = hp->clkbase / div; 811 //freq = hp->clkbase / div;
812 return true; 812 return true;
813 } 813 }
814 } 814 }
815 /* No divisor found. */ 815 /* No divisor found. */
816 return false; 816 return false;
817 } 817 }
818 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_DVS)) { 818 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_DVS)) {
819 u_int dvs = (hp->clkbase + freq - 1) / freq; 819 u_int dvs = (hp->clkbase + freq - 1) / freq;
820 u_int roundup = dvs & 1; 820 u_int roundup = dvs & 1;
821 for (dvs >>= 1, div = 1; div <= 256; div <<= 1, dvs >>= 1) { 821 for (dvs >>= 1, div = 1; div <= 256; div <<= 1, dvs >>= 1) {
822 if (dvs + roundup <= 16) { 822 if (dvs + roundup <= 16) {
823 dvs += roundup - 1; 823 dvs += roundup - 1;
824 *divp = (div << SDHC_SDCLK_DIV_SHIFT) 824 *divp = (div << SDHC_SDCLK_DIV_SHIFT)
825 | (dvs << SDHC_SDCLK_DVS_SHIFT); 825 | (dvs << SDHC_SDCLK_DVS_SHIFT);
826 DPRINTF(2, 826 DPRINTF(2,
827 ("%s: divisor for freq %u is %u * %u\n", 827 ("%s: divisor for freq %u is %u * %u\n",
828 HDEVNAME(hp), freq, div * 2, dvs + 1)); 828 HDEVNAME(hp), freq, div * 2, dvs + 1));
829 //freq = hp->clkbase / (div * 2) * (dvs + 1); 829 //freq = hp->clkbase / (div * 2) * (dvs + 1);
830 return true; 830 return true;
831 } 831 }
832 /* 832 /*
833 * If we drop bits, we need to round up the divisor. 833 * If we drop bits, we need to round up the divisor.
834 */ 834 */
835 roundup |= dvs & 1; 835 roundup |= dvs & 1;
836 } 836 }
837 /* No divisor found. */ 837 /* No divisor found. */
838 return false; 838 return false;
839 } 839 }
840 if (hp->sc->sc_clkmsk != 0) { 840 if (hp->sc->sc_clkmsk != 0) {
841 div = howmany(hp->clkbase, freq); 841 div = howmany(hp->clkbase, freq);
842 if (div > (hp->sc->sc_clkmsk >> (ffs(hp->sc->sc_clkmsk) - 1))) 842 if (div > (hp->sc->sc_clkmsk >> (ffs(hp->sc->sc_clkmsk) - 1)))
843 return false; 843 return false;
844 *divp = div << (ffs(hp->sc->sc_clkmsk) - 1); 844 *divp = div << (ffs(hp->sc->sc_clkmsk) - 1);
845 //freq = hp->clkbase / div; 845 //freq = hp->clkbase / div;
846 return true; 846 return true;
847 } 847 }
848 if (hp->specver >= SDHC_SPEC_VERS_300) { 848 if (hp->specver >= SDHC_SPEC_VERS_300) {
849 div = howmany(hp->clkbase, freq); 849 div = howmany(hp->clkbase, freq);
850 div = div > 1 ? howmany(div, 2) : 0; 850 div = div > 1 ? howmany(div, 2) : 0;
851 if (div > 0x3ff) 851 if (div > 0x3ff)
852 return false; 852 return false;
853 *divp = (((div >> 8) & SDHC_SDCLK_XDIV_MASK) 853 *divp = (((div >> 8) & SDHC_SDCLK_XDIV_MASK)
854 << SDHC_SDCLK_XDIV_SHIFT) | 854 << SDHC_SDCLK_XDIV_SHIFT) |
855 (((div >> 0) & SDHC_SDCLK_DIV_MASK) 855 (((div >> 0) & SDHC_SDCLK_DIV_MASK)
856 << SDHC_SDCLK_DIV_SHIFT); 856 << SDHC_SDCLK_DIV_SHIFT);
857 //freq = hp->clkbase / div; 857 //freq = hp->clkbase / div;
858 return true; 858 return true;
859 } else { 859 } else {
860 for (div = 1; div <= 256; div *= 2) { 860 for (div = 1; div <= 256; div *= 2) {
861 if ((hp->clkbase / div) <= freq) { 861 if ((hp->clkbase / div) <= freq) {
862 *divp = (div / 2) << SDHC_SDCLK_DIV_SHIFT; 862 *divp = (div / 2) << SDHC_SDCLK_DIV_SHIFT;
863 //freq = hp->clkbase / div; 863 //freq = hp->clkbase / div;
864 return true; 864 return true;
865 } 865 }
866 } 866 }
867 /* No divisor found. */ 867 /* No divisor found. */
868 return false; 868 return false;
869 } 869 }
870 /* No divisor found. */ 870 /* No divisor found. */
871 return false; 871 return false;
872} 872}
873 873
874/* 874/*
875 * Set or change SDCLK frequency or disable the SD clock. 875 * Set or change SDCLK frequency or disable the SD clock.
876 * Return zero on success. 876 * Return zero on success.
877 */ 877 */
878static int 878static int
879sdhc_bus_clock(sdmmc_chipset_handle_t sch, int freq) 879sdhc_bus_clock(sdmmc_chipset_handle_t sch, int freq)
880{ 880{
881 struct sdhc_host *hp = (struct sdhc_host *)sch; 881 struct sdhc_host *hp = (struct sdhc_host *)sch;
882 u_int div; 882 u_int div;
883 u_int timo; 883 u_int timo;
884 int16_t reg; 884 int16_t reg;
885 int error = 0; 885 int error = 0;
886#ifdef DIAGNOSTIC 886#ifdef DIAGNOSTIC
887 bool present; 887 bool present;
888 888
889 mutex_enter(&hp->host_mtx); 889 mutex_enter(&hp->host_mtx);
890 present = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK); 890 present = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK);
891 mutex_exit(&hp->host_mtx); 891 mutex_exit(&hp->host_mtx);
892 892
893 /* Must not stop the clock if commands are in progress. */ 893 /* Must not stop the clock if commands are in progress. */
894 if (present && sdhc_card_detect(hp)) { 894 if (present && sdhc_card_detect(hp)) {
895 aprint_normal_dev(hp->sc->sc_dev, 895 aprint_normal_dev(hp->sc->sc_dev,
896 "%s: command in progress\n", __func__); 896 "%s: command in progress\n", __func__);
897 } 897 }
898#endif 898#endif
899 899
900 mutex_enter(&hp->host_mtx); 900 mutex_enter(&hp->host_mtx);
901 901
902 if (hp->sc->sc_vendor_bus_clock) { 902 if (hp->sc->sc_vendor_bus_clock) {
903 error = (*hp->sc->sc_vendor_bus_clock)(hp->sc, freq); 903 error = (*hp->sc->sc_vendor_bus_clock)(hp->sc, freq);
904 if (error != 0) 904 if (error != 0)
905 goto out; 905 goto out;
906 } 906 }
907 907
908 /* 908 /*
909 * Stop SD clock before changing the frequency. 909 * Stop SD clock before changing the frequency.
910 */ 910 */
911 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 911 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
912 HCLR4(hp, SDHC_CLOCK_CTL, 0xfff8); 912 HCLR4(hp, SDHC_CLOCK_CTL, 0xfff8);
913 if (freq == SDMMC_SDCLK_OFF) { 913 if (freq == SDMMC_SDCLK_OFF) {
914 HSET4(hp, SDHC_CLOCK_CTL, 0x80f0); 914 HSET4(hp, SDHC_CLOCK_CTL, 0x80f0);
915 goto out; 915 goto out;
916 } 916 }
917 } else { 917 } else {
918 HCLR2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE); 918 HCLR2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
919 if (freq == SDMMC_SDCLK_OFF) 919 if (freq == SDMMC_SDCLK_OFF)
920 goto out; 920 goto out;
921 } 921 }
922 922
923 /* 923 /*
924 * Set the minimum base clock frequency divisor. 924 * Set the minimum base clock frequency divisor.
925 */ 925 */
926 if (!sdhc_clock_divisor(hp, freq, &div)) { 926 if (!sdhc_clock_divisor(hp, freq, &div)) {
927 /* Invalid base clock frequency or `freq' value. */ 927 /* Invalid base clock frequency or `freq' value. */
928 error = EINVAL; 928 error = EINVAL;
929 goto out; 929 goto out;
930 } 930 }
931 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 931 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
932 HWRITE4(hp, SDHC_CLOCK_CTL, 932 HWRITE4(hp, SDHC_CLOCK_CTL,
933 div | (SDHC_TIMEOUT_MAX << 16)); 933 div | (SDHC_TIMEOUT_MAX << 16));
934 } else { 934 } else {
935 reg = HREAD2(hp, SDHC_CLOCK_CTL); 935 reg = HREAD2(hp, SDHC_CLOCK_CTL);
936 reg &= (SDHC_INTCLK_STABLE | SDHC_INTCLK_ENABLE); 936 reg &= (SDHC_INTCLK_STABLE | SDHC_INTCLK_ENABLE);
937 HWRITE2(hp, SDHC_CLOCK_CTL, reg | div); 937 HWRITE2(hp, SDHC_CLOCK_CTL, reg | div);
938 } 938 }
939 939
940 /* 940 /*
941 * Start internal clock. Wait 10ms for stabilization. 941 * Start internal clock. Wait 10ms for stabilization.
942 */ 942 */
943 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 943 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
944 sdmmc_delay(10000); 944 sdmmc_delay(10000);
945 HSET4(hp, SDHC_CLOCK_CTL, 945 HSET4(hp, SDHC_CLOCK_CTL,
946 8 | SDHC_INTCLK_ENABLE | SDHC_INTCLK_STABLE); 946 8 | SDHC_INTCLK_ENABLE | SDHC_INTCLK_STABLE);
947 } else { 947 } else {
948 HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE); 948 HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE);
949 for (timo = 1000; timo > 0; timo--) { 949 for (timo = 1000; timo > 0; timo--) {
950 if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL), 950 if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL),
951 SDHC_INTCLK_STABLE)) 951 SDHC_INTCLK_STABLE))
952 break; 952 break;
953 sdmmc_delay(10); 953 sdmmc_delay(10);
954 } 954 }
955 if (timo == 0) { 955 if (timo == 0) {
956 error = ETIMEDOUT; 956 error = ETIMEDOUT;
957 goto out; 957 goto out;
958 } 958 }
959 } 959 }
960 960
961 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 961 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
962 HSET1(hp, SDHC_SOFTWARE_RESET, SDHC_INIT_ACTIVE); 962 HSET1(hp, SDHC_SOFTWARE_RESET, SDHC_INIT_ACTIVE);
963 /* 963 /*
964 * Sending 80 clocks at 400kHz takes 200us. 964 * Sending 80 clocks at 400kHz takes 200us.
965 * So delay for that time + slop and then 965 * So delay for that time + slop and then
966 * check a few times for completion. 966 * check a few times for completion.
967 */ 967 */
968 sdmmc_delay(210); 968 sdmmc_delay(210);
969 for (timo = 10; timo > 0; timo--) { 969 for (timo = 10; timo > 0; timo--) {
970 if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), 970 if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET),
971 SDHC_INIT_ACTIVE)) 971 SDHC_INIT_ACTIVE))
972 break; 972 break;
973 sdmmc_delay(10); 973 sdmmc_delay(10);
974 } 974 }
975 DPRINTF(2,("%s: %u init spins\n", __func__, 10 - timo)); 975 DPRINTF(2,("%s: %u init spins\n", __func__, 10 - timo));
976 976
977 /* 977 /*
978 * Enable SD clock. 978 * Enable SD clock.
979 */ 979 */
980 HSET4(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE); 980 HSET4(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
981 } else { 981 } else {
982 /* 982 /*
983 * Enable SD clock. 983 * Enable SD clock.
984 */ 984 */
985 HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE); 985 HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
986 986
987 if (freq > 25000 && 987 if (freq > 25000 &&
988 !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_HS_BIT)) 988 !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_HS_BIT))
989 HSET1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED); 989 HSET1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
990 else 990 else
991 HCLR1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED); 991 HCLR1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
992 } 992 }
993 993
994out: 994out:
995 mutex_exit(&hp->host_mtx); 995 mutex_exit(&hp->host_mtx);
996 996
997 return error; 997 return error;
998} 998}
999 999
1000static int 1000static int
1001sdhc_bus_width(sdmmc_chipset_handle_t sch, int width) 1001sdhc_bus_width(sdmmc_chipset_handle_t sch, int width)
1002{ 1002{
1003 struct sdhc_host *hp = (struct sdhc_host *)sch; 1003 struct sdhc_host *hp = (struct sdhc_host *)sch;
1004 int reg; 1004 int reg;
1005 1005
1006 switch (width) { 1006 switch (width) {
1007 case 1: 1007 case 1:
1008 case 4: 1008 case 4:
1009 break; 1009 break;
1010 1010
1011 case 8: 1011 case 8:
1012 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_8BIT_MODE)) 1012 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_8BIT_MODE))
1013 break; 1013 break;
1014 /* FALLTHROUGH */ 1014 /* FALLTHROUGH */
1015 default: 1015 default:
1016 DPRINTF(0,("%s: unsupported bus width (%d)\n", 1016 DPRINTF(0,("%s: unsupported bus width (%d)\n",
1017 HDEVNAME(hp), width)); 1017 HDEVNAME(hp), width));
1018 return 1; 1018 return 1;
1019 } 1019 }
1020 1020
1021 mutex_enter(&hp->host_mtx); 1021 mutex_enter(&hp->host_mtx);
1022 reg = HREAD1(hp, SDHC_HOST_CTL); 1022 reg = HREAD1(hp, SDHC_HOST_CTL);
1023 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1023 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1024 reg &= ~(SDHC_4BIT_MODE|SDHC_ESDHC_8BIT_MODE); 1024 reg &= ~(SDHC_4BIT_MODE|SDHC_ESDHC_8BIT_MODE);
1025 if (width == 4) 1025 if (width == 4)
1026 reg |= SDHC_4BIT_MODE; 1026 reg |= SDHC_4BIT_MODE;
1027 else if (width == 8) 1027 else if (width == 8)
1028 reg |= SDHC_ESDHC_8BIT_MODE; 1028 reg |= SDHC_ESDHC_8BIT_MODE;
1029 } else { 1029 } else {
1030 reg &= ~SDHC_4BIT_MODE; 1030 reg &= ~SDHC_4BIT_MODE;
1031 if (hp->specver >= SDHC_SPEC_VERS_300) { 1031 if (hp->specver >= SDHC_SPEC_VERS_300) {
1032 reg &= ~SDHC_8BIT_MODE; 1032 reg &= ~SDHC_8BIT_MODE;
1033 } 1033 }
1034 if (width == 4) { 1034 if (width == 4) {
1035 reg |= SDHC_4BIT_MODE; 1035 reg |= SDHC_4BIT_MODE;
1036 } else if (width == 8 && hp->specver >= SDHC_SPEC_VERS_300) { 1036 } else if (width == 8 && hp->specver >= SDHC_SPEC_VERS_300) {
1037 reg |= SDHC_8BIT_MODE; 1037 reg |= SDHC_8BIT_MODE;
1038 } 1038 }
1039 } 1039 }
1040 HWRITE1(hp, SDHC_HOST_CTL, reg); 1040 HWRITE1(hp, SDHC_HOST_CTL, reg);
1041 mutex_exit(&hp->host_mtx); 1041 mutex_exit(&hp->host_mtx);
1042 1042
1043 return 0; 1043 return 0;
1044} 1044}
1045 1045
1046static int 1046static int
1047sdhc_bus_rod(sdmmc_chipset_handle_t sch, int on) 1047sdhc_bus_rod(sdmmc_chipset_handle_t sch, int on)
1048{ 1048{
1049 struct sdhc_host *hp = (struct sdhc_host *)sch; 1049 struct sdhc_host *hp = (struct sdhc_host *)sch;
1050 1050
1051 if (hp->sc->sc_vendor_rod) 1051 if (hp->sc->sc_vendor_rod)
1052 return (*hp->sc->sc_vendor_rod)(hp->sc, on); 1052 return (*hp->sc->sc_vendor_rod)(hp->sc, on);
1053 1053
1054 return 0; 1054 return 0;
1055} 1055}
1056 1056
1057static void 1057static void
1058sdhc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable) 1058sdhc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
1059{ 1059{
1060 struct sdhc_host *hp = (struct sdhc_host *)sch; 1060 struct sdhc_host *hp = (struct sdhc_host *)sch;
1061 1061
1062 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1062 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1063 mutex_enter(&hp->intr_mtx); 1063 mutex_enter(&hp->intr_mtx);
1064 if (enable) { 1064 if (enable) {
1065 HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT); 1065 HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
1066 HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT); 1066 HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
1067 } else { 1067 } else {
1068 HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT); 1068 HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
1069 HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT); 1069 HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
1070 } 1070 }
1071 mutex_exit(&hp->intr_mtx); 1071 mutex_exit(&hp->intr_mtx);
1072 } 1072 }
1073} 1073}
1074 1074
1075static void 1075static void
1076sdhc_card_intr_ack(sdmmc_chipset_handle_t sch) 1076sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)
1077{ 1077{
1078 struct sdhc_host *hp = (struct sdhc_host *)sch; 1078 struct sdhc_host *hp = (struct sdhc_host *)sch;
1079 1079
1080 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1080 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1081 mutex_enter(&hp->intr_mtx); 1081 mutex_enter(&hp->intr_mtx);
1082 HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT); 1082 HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
1083 mutex_exit(&hp->intr_mtx); 1083 mutex_exit(&hp->intr_mtx);
1084 } 1084 }
1085} 1085}
1086 1086
1087static int 1087static int
1088sdhc_wait_state(struct sdhc_host *hp, uint32_t mask, uint32_t value) 1088sdhc_wait_state(struct sdhc_host *hp, uint32_t mask, uint32_t value)
1089{ 1089{
1090 uint32_t state; 1090 uint32_t state;
1091 int timeout; 1091 int timeout;
1092 1092
1093 for (timeout = 10; timeout > 0; timeout--) { 1093 for (timeout = 10; timeout > 0; timeout--) {
1094 if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask) == value) 1094 if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask) == value)
1095 return 0; 1095 return 0;
1096 sdmmc_delay(10000); 1096 sdmmc_delay(10000);
1097 } 1097 }
1098 DPRINTF(0,("%s: timeout waiting for %x (state=%x)\n", HDEVNAME(hp), 1098 DPRINTF(0,("%s: timeout waiting for %x (state=%x)\n", HDEVNAME(hp),
1099 value, state)); 1099 value, state));
1100 return ETIMEDOUT; 1100 return ETIMEDOUT;
1101} 1101}
1102 1102
1103static void 1103static void
1104sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd) 1104sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
1105{ 1105{
1106 struct sdhc_host *hp = (struct sdhc_host *)sch; 1106 struct sdhc_host *hp = (struct sdhc_host *)sch;
1107 int error; 1107 int error;
1108 1108
1109 if (cmd->c_data && ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1109 if (cmd->c_data && ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1110 const uint16_t ready = SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY; 1110 const uint16_t ready = SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY;
1111 mutex_enter(&hp->intr_mtx); 1111 mutex_enter(&hp->intr_mtx);
1112 if (ISSET(hp->flags, SHF_USE_DMA)) { 1112 if (ISSET(hp->flags, SHF_USE_DMA)) {
1113 HCLR2(hp, SDHC_NINTR_SIGNAL_EN, ready); 1113 HCLR2(hp, SDHC_NINTR_SIGNAL_EN, ready);
1114 HCLR2(hp, SDHC_NINTR_STATUS_EN, ready); 1114 HCLR2(hp, SDHC_NINTR_STATUS_EN, ready);
1115 } else { 1115 } else {
1116 HSET2(hp, SDHC_NINTR_SIGNAL_EN, ready); 1116 HSET2(hp, SDHC_NINTR_SIGNAL_EN, ready);
1117 HSET2(hp, SDHC_NINTR_STATUS_EN, ready); 1117 HSET2(hp, SDHC_NINTR_STATUS_EN, ready);
1118 } 1118 }
1119 mutex_exit(&hp->intr_mtx); 1119 mutex_exit(&hp->intr_mtx);
1120 } 1120 }
1121 1121
 1122 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_TIMEOUT)) {
 1123 const uint16_t eintr = SDHC_CMD_TIMEOUT_ERROR;
 1124 if (cmd->c_data != NULL) {
 1125 HCLR2(hp, SDHC_EINTR_SIGNAL_EN, eintr);
 1126 HCLR2(hp, SDHC_EINTR_STATUS_EN, eintr);
 1127 } else {
 1128 HSET2(hp, SDHC_EINTR_SIGNAL_EN, eintr);
 1129 HSET2(hp, SDHC_EINTR_STATUS_EN, eintr);
 1130 }
 1131 }
 1132
1122 /* 1133 /*
1123 * Start the MMC command, or mark `cmd' as failed and return. 1134 * Start the MMC command, or mark `cmd' as failed and return.
1124 */ 1135 */
1125 error = sdhc_start_command(hp, cmd); 1136 error = sdhc_start_command(hp, cmd);
1126 if (error) { 1137 if (error) {
1127 cmd->c_error = error; 1138 cmd->c_error = error;
1128 goto out; 1139 goto out;
1129 } 1140 }
1130 1141
1131 /* 1142 /*
1132 * Wait until the command phase is done, or until the command 1143 * Wait until the command phase is done, or until the command
1133 * is marked done for any other reason. 1144 * is marked done for any other reason.
1134 */ 1145 */
1135 if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE, SDHC_COMMAND_TIMEOUT)) { 1146 if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE, SDHC_COMMAND_TIMEOUT)) {
1136 cmd->c_error = ETIMEDOUT; 1147 cmd->c_error = ETIMEDOUT;
1137 goto out; 1148 goto out;
1138 } 1149 }
1139 1150
1140 /* 1151 /*
1141 * The host controller removes bits [0:7] from the response 1152 * The host controller removes bits [0:7] from the response
1142 * data (CRC) and we pass the data up unchanged to the bus 1153 * data (CRC) and we pass the data up unchanged to the bus
1143 * driver (without padding). 1154 * driver (without padding).
1144 */ 1155 */
1145 mutex_enter(&hp->host_mtx); 1156 mutex_enter(&hp->host_mtx);
1146 if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) { 1157 if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
1147 cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE + 0); 1158 cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE + 0);
1148 if (ISSET(cmd->c_flags, SCF_RSP_136)) { 1159 if (ISSET(cmd->c_flags, SCF_RSP_136)) {
1149 cmd->c_resp[1] = HREAD4(hp, SDHC_RESPONSE + 4); 1160 cmd->c_resp[1] = HREAD4(hp, SDHC_RESPONSE + 4);
1150 cmd->c_resp[2] = HREAD4(hp, SDHC_RESPONSE + 8); 1161 cmd->c_resp[2] = HREAD4(hp, SDHC_RESPONSE + 8);
1151 cmd->c_resp[3] = HREAD4(hp, SDHC_RESPONSE + 12); 1162 cmd->c_resp[3] = HREAD4(hp, SDHC_RESPONSE + 12);
1152 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_RSP136_CRC)) { 1163 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_RSP136_CRC)) {
1153 cmd->c_resp[0] = (cmd->c_resp[0] >> 8) | 1164 cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
1154 (cmd->c_resp[1] << 24); 1165 (cmd->c_resp[1] << 24);
1155 cmd->c_resp[1] = (cmd->c_resp[1] >> 8) | 1166 cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
1156 (cmd->c_resp[2] << 24); 1167 (cmd->c_resp[2] << 24);
1157 cmd->c_resp[2] = (cmd->c_resp[2] >> 8) | 1168 cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
1158 (cmd->c_resp[3] << 24); 1169 (cmd->c_resp[3] << 24);
1159 cmd->c_resp[3] = (cmd->c_resp[3] >> 8); 1170 cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
1160 } 1171 }
1161 } 1172 }
1162 } 1173 }
1163 mutex_exit(&hp->host_mtx); 1174 mutex_exit(&hp->host_mtx);
1164 DPRINTF(1,("%s: resp = %08x\n", HDEVNAME(hp), cmd->c_resp[0])); 1175 DPRINTF(1,("%s: resp = %08x\n", HDEVNAME(hp), cmd->c_resp[0]));
1165 1176
1166 /* 1177 /*
1167 * If the command has data to transfer in any direction, 1178 * If the command has data to transfer in any direction,
1168 * execute the transfer now. 1179 * execute the transfer now.
1169 */ 1180 */
1170 if (cmd->c_error == 0 && cmd->c_data != NULL) 1181 if (cmd->c_error == 0 && cmd->c_data != NULL)
1171 sdhc_transfer_data(hp, cmd); 1182 sdhc_transfer_data(hp, cmd);
1172 else if (ISSET(cmd->c_flags, SCF_RSP_BSY)) { 1183 else if (ISSET(cmd->c_flags, SCF_RSP_BSY)) {
1173 if (!sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE, hz * 10)) { 1184 if (!sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE, hz * 10)) {
1174 cmd->c_error = ETIMEDOUT; 1185 cmd->c_error = ETIMEDOUT;
1175 goto out; 1186 goto out;
1176 } 1187 }
1177 } 1188 }
1178 1189
1179out: 1190out:
1180 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED) 1191 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)
1181 && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_LED_ON)) { 1192 && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_LED_ON)) {
1182 mutex_enter(&hp->host_mtx); 1193 mutex_enter(&hp->host_mtx);
1183 /* Turn off the LED. */ 1194 /* Turn off the LED. */
1184 HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON); 1195 HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
1185 mutex_exit(&hp->host_mtx); 1196 mutex_exit(&hp->host_mtx);
1186 } 1197 }
1187 SET(cmd->c_flags, SCF_ITSDONE); 1198 SET(cmd->c_flags, SCF_ITSDONE);
1188 1199
1189 DPRINTF(1,("%s: cmd %d %s (flags=%08x error=%d)\n", HDEVNAME(hp), 1200 DPRINTF(1,("%s: cmd %d %s (flags=%08x error=%d)\n", HDEVNAME(hp),
1190 cmd->c_opcode, (cmd->c_error == 0) ? "done" : "abort", 1201 cmd->c_opcode, (cmd->c_error == 0) ? "done" : "abort",
1191 cmd->c_flags, cmd->c_error)); 1202 cmd->c_flags, cmd->c_error));
1192} 1203}
1193 1204
1194static int 1205static int
1195sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd) 1206sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd)
1196{ 1207{
1197 struct sdhc_softc * const sc = hp->sc; 1208 struct sdhc_softc * const sc = hp->sc;
1198 uint16_t blksize = 0; 1209 uint16_t blksize = 0;
1199 uint16_t blkcount = 0; 1210 uint16_t blkcount = 0;
1200 uint16_t mode; 1211 uint16_t mode;
1201 uint16_t command; 1212 uint16_t command;
1202 int error; 1213 int error;
1203 1214
1204 DPRINTF(1,("%s: start cmd %d arg=%08x data=%p dlen=%d flags=%08x, status=%#x\n", 1215 DPRINTF(1,("%s: start cmd %d arg=%08x data=%p dlen=%d flags=%08x, status=%#x\n",
1205 HDEVNAME(hp), cmd->c_opcode, cmd->c_arg, cmd->c_data, 1216 HDEVNAME(hp), cmd->c_opcode, cmd->c_arg, cmd->c_data,
1206 cmd->c_datalen, cmd->c_flags, HREAD4(hp, SDHC_NINTR_STATUS))); 1217 cmd->c_datalen, cmd->c_flags, HREAD4(hp, SDHC_NINTR_STATUS)));
1207 1218
1208 /* 1219 /*
1209 * The maximum block length for commands should be the minimum 1220 * The maximum block length for commands should be the minimum
1210 * of the host buffer size and the card buffer size. (1.7.2) 1221 * of the host buffer size and the card buffer size. (1.7.2)
1211 */ 1222 */
1212 1223
1213 /* Fragment the data into proper blocks. */ 1224 /* Fragment the data into proper blocks. */
1214 if (cmd->c_datalen > 0) { 1225 if (cmd->c_datalen > 0) {
1215 blksize = MIN(cmd->c_datalen, cmd->c_blklen); 1226 blksize = MIN(cmd->c_datalen, cmd->c_blklen);
1216 blkcount = cmd->c_datalen / blksize; 1227 blkcount = cmd->c_datalen / blksize;
1217 if (cmd->c_datalen % blksize > 0) { 1228 if (cmd->c_datalen % blksize > 0) {
1218 /* XXX: Split this command. (1.7.4) */ 1229 /* XXX: Split this command. (1.7.4) */
1219 aprint_error_dev(sc->sc_dev, 1230 aprint_error_dev(sc->sc_dev,
1220 "data not a multiple of %u bytes\n", blksize); 1231 "data not a multiple of %u bytes\n", blksize);
1221 return EINVAL; 1232 return EINVAL;
1222 } 1233 }
1223 } 1234 }
1224 1235
1225 /* Check limit imposed by 9-bit block count. (1.7.2) */ 1236 /* Check limit imposed by 9-bit block count. (1.7.2) */
1226 if (blkcount > SDHC_BLOCK_COUNT_MAX) { 1237 if (blkcount > SDHC_BLOCK_COUNT_MAX) {
1227 aprint_error_dev(sc->sc_dev, "too much data\n"); 1238 aprint_error_dev(sc->sc_dev, "too much data\n");
1228 return EINVAL; 1239 return EINVAL;
1229 } 1240 }
1230 1241
1231 /* Prepare transfer mode register value. (2.2.5) */ 1242 /* Prepare transfer mode register value. (2.2.5) */
1232 mode = SDHC_BLOCK_COUNT_ENABLE; 1243 mode = SDHC_BLOCK_COUNT_ENABLE;
1233 if (ISSET(cmd->c_flags, SCF_CMD_READ)) 1244 if (ISSET(cmd->c_flags, SCF_CMD_READ))
1234 mode |= SDHC_READ_MODE; 1245 mode |= SDHC_READ_MODE;
1235 if (blkcount > 1) { 1246 if (blkcount > 1) {
1236 mode |= SDHC_MULTI_BLOCK_MODE; 1247 mode |= SDHC_MULTI_BLOCK_MODE;
1237 /* XXX only for memory commands? */ 1248 /* XXX only for memory commands? */
1238 mode |= SDHC_AUTO_CMD12_ENABLE; 1249 mode |= SDHC_AUTO_CMD12_ENABLE;
1239 } 1250 }
1240 if (cmd->c_dmamap != NULL && cmd->c_datalen > 0 && 1251 if (cmd->c_dmamap != NULL && cmd->c_datalen > 0 &&
1241 ISSET(hp->flags, SHF_MODE_DMAEN)) { 1252 ISSET(hp->flags, SHF_MODE_DMAEN)) {
1242 mode |= SDHC_DMA_ENABLE; 1253 mode |= SDHC_DMA_ENABLE;
1243 } 1254 }
1244 1255
1245 /* 1256 /*
1246 * Prepare command register value. (2.2.6) 1257 * Prepare command register value. (2.2.6)
1247 */ 1258 */
1248 command = (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) << SDHC_COMMAND_INDEX_SHIFT; 1259 command = (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) << SDHC_COMMAND_INDEX_SHIFT;
1249 1260
1250 if (ISSET(cmd->c_flags, SCF_RSP_CRC)) 1261 if (ISSET(cmd->c_flags, SCF_RSP_CRC))
1251 command |= SDHC_CRC_CHECK_ENABLE; 1262 command |= SDHC_CRC_CHECK_ENABLE;
1252 if (ISSET(cmd->c_flags, SCF_RSP_IDX)) 1263 if (ISSET(cmd->c_flags, SCF_RSP_IDX))
1253 command |= SDHC_INDEX_CHECK_ENABLE; 1264 command |= SDHC_INDEX_CHECK_ENABLE;
1254 if (cmd->c_data != NULL) 1265 if (cmd->c_data != NULL)
1255 command |= SDHC_DATA_PRESENT_SELECT; 1266 command |= SDHC_DATA_PRESENT_SELECT;
1256 1267
1257 if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT)) 1268 if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
1258 command |= SDHC_NO_RESPONSE; 1269 command |= SDHC_NO_RESPONSE;
1259 else if (ISSET(cmd->c_flags, SCF_RSP_136)) 1270 else if (ISSET(cmd->c_flags, SCF_RSP_136))
1260 command |= SDHC_RESP_LEN_136; 1271 command |= SDHC_RESP_LEN_136;
1261 else if (ISSET(cmd->c_flags, SCF_RSP_BSY)) 1272 else if (ISSET(cmd->c_flags, SCF_RSP_BSY))
1262 command |= SDHC_RESP_LEN_48_CHK_BUSY; 1273 command |= SDHC_RESP_LEN_48_CHK_BUSY;
1263 else 1274 else
1264 command |= SDHC_RESP_LEN_48; 1275 command |= SDHC_RESP_LEN_48;
1265 1276
1266 /* Wait until command and data inhibit bits are clear. (1.5) */ 1277 /* Wait until command and data inhibit bits are clear. (1.5) */
1267 error = sdhc_wait_state(hp, SDHC_CMD_INHIBIT_MASK, 0); 1278 error = sdhc_wait_state(hp, SDHC_CMD_INHIBIT_MASK, 0);
1268 if (error) 1279 if (error)
1269 return error; 1280 return error;
1270 1281
1271 DPRINTF(1,("%s: writing cmd: blksize=%d blkcnt=%d mode=%04x cmd=%04x\n", 1282 DPRINTF(1,("%s: writing cmd: blksize=%d blkcnt=%d mode=%04x cmd=%04x\n",
1272 HDEVNAME(hp), blksize, blkcount, mode, command)); 1283 HDEVNAME(hp), blksize, blkcount, mode, command));
1273 1284
1274 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1285 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1275 blksize |= (MAX(0, PAGE_SHIFT - 12) & SDHC_DMA_BOUNDARY_MASK) << 1286 blksize |= (MAX(0, PAGE_SHIFT - 12) & SDHC_DMA_BOUNDARY_MASK) <<
1276 SDHC_DMA_BOUNDARY_SHIFT; /* PAGE_SIZE DMA boundary */ 1287 SDHC_DMA_BOUNDARY_SHIFT; /* PAGE_SIZE DMA boundary */
1277 } 1288 }
1278 1289
1279 mutex_enter(&hp->host_mtx); 1290 mutex_enter(&hp->host_mtx);
1280 1291
1281 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1292 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1282 /* Alert the user not to remove the card. */ 1293 /* Alert the user not to remove the card. */
1283 HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON); 1294 HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
1284 } 1295 }
1285 1296
1286 /* Set DMA start address. */ 1297 /* Set DMA start address. */
1287 if (ISSET(mode, SDHC_DMA_ENABLE) && 1298 if (ISSET(mode, SDHC_DMA_ENABLE) &&
1288 !ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA)) 1299 !ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA))
1289 HWRITE4(hp, SDHC_DMA_ADDR, cmd->c_dmamap->dm_segs[0].ds_addr); 1300 HWRITE4(hp, SDHC_DMA_ADDR, cmd->c_dmamap->dm_segs[0].ds_addr);
1290 1301
1291 /* 1302 /*
1292 * Start a CPU data transfer. Writing to the high order byte 1303 * Start a CPU data transfer. Writing to the high order byte
1293 * of the SDHC_COMMAND register triggers the SD command. (1.5) 1304 * of the SDHC_COMMAND register triggers the SD command. (1.5)
1294 */ 1305 */
1295 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) { 1306 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
1296 HWRITE4(hp, SDHC_BLOCK_SIZE, blksize | (blkcount << 16)); 1307 HWRITE4(hp, SDHC_BLOCK_SIZE, blksize | (blkcount << 16));
1297 HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg); 1308 HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
1298 HWRITE4(hp, SDHC_TRANSFER_MODE, mode | (command << 16)); 1309 HWRITE4(hp, SDHC_TRANSFER_MODE, mode | (command << 16));
1299 } else { 1310 } else {
1300 HWRITE2(hp, SDHC_BLOCK_SIZE, blksize); 1311 HWRITE2(hp, SDHC_BLOCK_SIZE, blksize);
1301 HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount); 1312 HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount);
1302 HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg); 1313 HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
1303 HWRITE2(hp, SDHC_TRANSFER_MODE, mode); 1314 HWRITE2(hp, SDHC_TRANSFER_MODE, mode);
1304 HWRITE2(hp, SDHC_COMMAND, command); 1315 HWRITE2(hp, SDHC_COMMAND, command);
1305 } 1316 }
1306 1317
1307 mutex_exit(&hp->host_mtx); 1318 mutex_exit(&hp->host_mtx);
1308 1319
1309 return 0; 1320 return 0;
1310} 1321}
1311 1322
1312static void 1323static void
1313sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd) 1324sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd)
1314{ 1325{
1315 struct sdhc_softc *sc = hp->sc; 1326 struct sdhc_softc *sc = hp->sc;
1316 int error; 1327 int error;
1317 1328
1318 DPRINTF(1,("%s: data transfer: resp=%08x datalen=%u\n", HDEVNAME(hp), 1329 DPRINTF(1,("%s: data transfer: resp=%08x datalen=%u\n", HDEVNAME(hp),
1319 MMC_R1(cmd->c_resp), cmd->c_datalen)); 1330 MMC_R1(cmd->c_resp), cmd->c_datalen));
1320 1331
1321#ifdef SDHC_DEBUG 1332#ifdef SDHC_DEBUG
1322 /* XXX I forgot why I wanted to know when this happens :-( */ 1333 /* XXX I forgot why I wanted to know when this happens :-( */
1323 if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) && 1334 if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) &&
1324 ISSET(MMC_R1(cmd->c_resp), 0xcb00)) { 1335 ISSET(MMC_R1(cmd->c_resp), 0xcb00)) {
1325 aprint_error_dev(hp->sc->sc_dev, 1336 aprint_error_dev(hp->sc->sc_dev,
1326 "CMD52/53 error response flags %#x\n", 1337 "CMD52/53 error response flags %#x\n",
1327 MMC_R1(cmd->c_resp) & 0xff00); 1338 MMC_R1(cmd->c_resp) & 0xff00);
1328 } 1339 }
1329#endif 1340#endif
1330 1341
1331 if (cmd->c_dmamap != NULL) { 1342 if (cmd->c_dmamap != NULL) {
1332 if (hp->sc->sc_vendor_transfer_data_dma != NULL) { 1343 if (hp->sc->sc_vendor_transfer_data_dma != NULL) {
1333 error = hp->sc->sc_vendor_transfer_data_dma(sc, cmd); 1344 error = hp->sc->sc_vendor_transfer_data_dma(sc, cmd);
1334 if (error == 0 && !sdhc_wait_intr(hp, 1345 if (error == 0 && !sdhc_wait_intr(hp,
1335 SDHC_TRANSFER_COMPLETE, SDHC_TRANSFER_TIMEOUT)) { 1346 SDHC_TRANSFER_COMPLETE, SDHC_DMA_TIMEOUT)) {
1336 error = ETIMEDOUT; 1347 error = ETIMEDOUT;
1337 } 1348 }
1338 } else { 1349 } else {
1339 error = sdhc_transfer_data_dma(hp, cmd); 1350 error = sdhc_transfer_data_dma(hp, cmd);
1340 } 1351 }
1341 } else 1352 } else
1342 error = sdhc_transfer_data_pio(hp, cmd); 1353 error = sdhc_transfer_data_pio(hp, cmd);
1343 if (error) 1354 if (error)
1344 cmd->c_error = error; 1355 cmd->c_error = error;
1345 SET(cmd->c_flags, SCF_ITSDONE); 1356 SET(cmd->c_flags, SCF_ITSDONE);
1346 1357
1347 DPRINTF(1,("%s: data transfer done (error=%d)\n", 1358 DPRINTF(1,("%s: data transfer done (error=%d)\n",
1348 HDEVNAME(hp), cmd->c_error)); 1359 HDEVNAME(hp), cmd->c_error));
1349} 1360}
1350 1361
1351static int 1362static int
1352sdhc_transfer_data_dma(struct sdhc_host *hp, struct sdmmc_command *cmd) 1363sdhc_transfer_data_dma(struct sdhc_host *hp, struct sdmmc_command *cmd)
1353{ 1364{
1354 bus_dma_segment_t *dm_segs = cmd->c_dmamap->dm_segs; 1365 bus_dma_segment_t *dm_segs = cmd->c_dmamap->dm_segs;
1355 bus_addr_t posaddr; 1366 bus_addr_t posaddr;
1356 bus_addr_t segaddr; 1367 bus_addr_t segaddr;
1357 bus_size_t seglen; 1368 bus_size_t seglen;
1358 u_int seg = 0; 1369 u_int seg = 0;
1359 int error = 0; 1370 int error = 0;
1360 int status; 1371 int status;
1361 1372
1362 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_DMA_INTERRUPT); 1373 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_DMA_INTERRUPT);
1363 KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_DMA_INTERRUPT); 1374 KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_DMA_INTERRUPT);
1364 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE); 1375 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
1365 KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE); 1376 KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
1366 1377
1367 for (;;) { 1378 for (;;) {
1368 status = sdhc_wait_intr(hp, 1379 status = sdhc_wait_intr(hp,
1369 SDHC_DMA_INTERRUPT|SDHC_TRANSFER_COMPLETE, 1380 SDHC_DMA_INTERRUPT|SDHC_TRANSFER_COMPLETE,
1370 SDHC_DMA_TIMEOUT); 1381 SDHC_DMA_TIMEOUT);
1371 1382
1372 if (status & SDHC_TRANSFER_COMPLETE) { 1383 if (status & SDHC_TRANSFER_COMPLETE) {
1373 break; 1384 break;
1374 } 1385 }
1375 if (!status) { 1386 if (!status) {
1376 error = ETIMEDOUT; 1387 error = ETIMEDOUT;
1377 break; 1388 break;
1378 } 1389 }
1379 if ((status & SDHC_DMA_INTERRUPT) == 0) { 1390 if ((status & SDHC_DMA_INTERRUPT) == 0) {
1380 continue; 1391 continue;
1381 } 1392 }
1382 1393
1383 /* DMA Interrupt (boundary crossing) */ 1394 /* DMA Interrupt (boundary crossing) */
1384 1395
1385 segaddr = dm_segs[seg].ds_addr; 1396 segaddr = dm_segs[seg].ds_addr;
1386 seglen = dm_segs[seg].ds_len; 1397 seglen = dm_segs[seg].ds_len;
1387 mutex_enter(&hp->host_mtx); 1398 mutex_enter(&hp->host_mtx);
1388 posaddr = HREAD4(hp, SDHC_DMA_ADDR); 1399 posaddr = HREAD4(hp, SDHC_DMA_ADDR);
1389 mutex_exit(&hp->host_mtx); 1400 mutex_exit(&hp->host_mtx);
1390 1401
1391 if ((seg == (cmd->c_dmamap->dm_nsegs-1)) && (posaddr == (segaddr + seglen))) { 1402 if ((seg == (cmd->c_dmamap->dm_nsegs-1)) && (posaddr == (segaddr + seglen))) {
1392 continue; 1403 continue;
1393 } 1404 }
1394 mutex_enter(&hp->host_mtx); 1405 mutex_enter(&hp->host_mtx);
1395 if ((posaddr >= segaddr) && (posaddr < (segaddr + seglen))) 1406 if ((posaddr >= segaddr) && (posaddr < (segaddr + seglen)))
1396 HWRITE4(hp, SDHC_DMA_ADDR, posaddr); 1407 HWRITE4(hp, SDHC_DMA_ADDR, posaddr);
1397 else if ((posaddr >= segaddr) && (posaddr == (segaddr + seglen)) && (seg + 1) < cmd->c_dmamap->dm_nsegs) 1408 else if ((posaddr >= segaddr) && (posaddr == (segaddr + seglen)) && (seg + 1) < cmd->c_dmamap->dm_nsegs)
1398 HWRITE4(hp, SDHC_DMA_ADDR, dm_segs[++seg].ds_addr); 1409 HWRITE4(hp, SDHC_DMA_ADDR, dm_segs[++seg].ds_addr);
1399 mutex_exit(&hp->host_mtx); 1410 mutex_exit(&hp->host_mtx);
1400 KASSERT(seg < cmd->c_dmamap->dm_nsegs); 1411 KASSERT(seg < cmd->c_dmamap->dm_nsegs);
1401 } 1412 }
1402 1413
1403 return error; 1414 return error;
1404} 1415}
1405 1416
1406static int 1417static int
1407sdhc_transfer_data_pio(struct sdhc_host *hp, struct sdmmc_command *cmd) 1418sdhc_transfer_data_pio(struct sdhc_host *hp, struct sdmmc_command *cmd)
1408{ 1419{
1409 uint8_t *data = cmd->c_data; 1420 uint8_t *data = cmd->c_data;
1410 void (*pio_func)(struct sdhc_host *, uint8_t *, u_int); 1421 void (*pio_func)(struct sdhc_host *, uint8_t *, u_int);
1411 u_int len, datalen; 1422 u_int len, datalen;
1412 u_int imask; 1423 u_int imask;
1413 u_int pmask; 1424 u_int pmask;
1414 int error = 0; 1425 int error = 0;
1415 1426
1416 if (ISSET(cmd->c_flags, SCF_CMD_READ)) { 1427 if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
1417 imask = SDHC_BUFFER_READ_READY; 1428 imask = SDHC_BUFFER_READ_READY;
1418 pmask = SDHC_BUFFER_READ_ENABLE; 1429 pmask = SDHC_BUFFER_READ_ENABLE;
1419 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1430 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1420 pio_func = esdhc_read_data_pio; 1431 pio_func = esdhc_read_data_pio;
1421 } else { 1432 } else {
1422 pio_func = sdhc_read_data_pio; 1433 pio_func = sdhc_read_data_pio;
1423 } 1434 }
1424 } else { 1435 } else {
1425 imask = SDHC_BUFFER_WRITE_READY; 1436 imask = SDHC_BUFFER_WRITE_READY;
1426 pmask = SDHC_BUFFER_WRITE_ENABLE; 1437 pmask = SDHC_BUFFER_WRITE_ENABLE;
1427 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1438 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1428 pio_func = esdhc_write_data_pio; 1439 pio_func = esdhc_write_data_pio;
1429 } else { 1440 } else {
1430 pio_func = sdhc_write_data_pio; 1441 pio_func = sdhc_write_data_pio;
1431 } 1442 }
1432 } 1443 }
1433 datalen = cmd->c_datalen; 1444 datalen = cmd->c_datalen;
1434 1445
1435 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & imask); 1446 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & imask);
1436 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE); 1447 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
1437 KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE); 1448 KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
1438 1449
1439 while (datalen > 0) { 1450 while (datalen > 0) {
1440 if (!ISSET(HREAD4(hp, SDHC_PRESENT_STATE), imask)) { 1451 if (!ISSET(HREAD4(hp, SDHC_PRESENT_STATE), imask)) {
1441 mutex_enter(&hp->intr_mtx); 1452 mutex_enter(&hp->intr_mtx);
1442 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) { 1453 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
1443 HSET4(hp, SDHC_NINTR_SIGNAL_EN, imask); 1454 HSET4(hp, SDHC_NINTR_SIGNAL_EN, imask);
1444 } else { 1455 } else {
1445 HSET2(hp, SDHC_NINTR_SIGNAL_EN, imask); 1456 HSET2(hp, SDHC_NINTR_SIGNAL_EN, imask);
1446 } 1457 }
1447 mutex_exit(&hp->intr_mtx); 1458 mutex_exit(&hp->intr_mtx);
1448 if (!sdhc_wait_intr(hp, imask, SDHC_BUFFER_TIMEOUT)) { 1459 if (!sdhc_wait_intr(hp, imask, SDHC_BUFFER_TIMEOUT)) {
1449 error = ETIMEDOUT; 1460 error = ETIMEDOUT;
1450 break; 1461 break;
1451 } 1462 }
1452 1463
1453 error = sdhc_wait_state(hp, pmask, pmask); 1464 error = sdhc_wait_state(hp, pmask, pmask);
1454 if (error) 1465 if (error)
1455 break; 1466 break;
1456 } 1467 }
1457 1468
1458 len = MIN(datalen, cmd->c_blklen); 1469 len = MIN(datalen, cmd->c_blklen);
1459 (*pio_func)(hp, data, len); 1470 (*pio_func)(hp, data, len);
1460 DPRINTF(2,("%s: pio data transfer %u @ %p\n", 1471 DPRINTF(2,("%s: pio data transfer %u @ %p\n",
1461 HDEVNAME(hp), len, data)); 1472 HDEVNAME(hp), len, data));
1462 1473
1463 data += len; 1474 data += len;
1464 datalen -= len; 1475 datalen -= len;
1465 } 1476 }
1466 1477
1467 if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE, 1478 if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
1468 SDHC_TRANSFER_TIMEOUT)) 1479 SDHC_TRANSFER_TIMEOUT))
1469 error = ETIMEDOUT; 1480 error = ETIMEDOUT;
1470 1481
1471 return error; 1482 return error;
1472} 1483}
1473 1484
1474static void 1485static void
1475sdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen) 1486sdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
1476{ 1487{
1477 1488
1478 if (((__uintptr_t)data & 3) == 0) { 1489 if (((__uintptr_t)data & 3) == 0) {
1479 while (datalen > 3) { 1490 while (datalen > 3) {
1480 *(uint32_t *)data = le32toh(HREAD4(hp, SDHC_DATA)); 1491 *(uint32_t *)data = le32toh(HREAD4(hp, SDHC_DATA));
1481 data += 4; 1492 data += 4;
1482 datalen -= 4; 1493 datalen -= 4;
1483 } 1494 }
1484 if (datalen > 1) { 1495 if (datalen > 1) {
1485 *(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA)); 1496 *(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
1486 data += 2; 1497 data += 2;
1487 datalen -= 2; 1498 datalen -= 2;
1488 } 1499 }
1489 if (datalen > 0) { 1500 if (datalen > 0) {
1490 *data = HREAD1(hp, SDHC_DATA); 1501 *data = HREAD1(hp, SDHC_DATA);
1491 data += 1; 1502 data += 1;
1492 datalen -= 1; 1503 datalen -= 1;
1493 } 1504 }
1494 } else if (((__uintptr_t)data & 1) == 0) { 1505 } else if (((__uintptr_t)data & 1) == 0) {
1495 while (datalen > 1) { 1506 while (datalen > 1) {
1496 *(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA)); 1507 *(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
1497 data += 2; 1508 data += 2;
1498 datalen -= 2; 1509 datalen -= 2;
1499 } 1510 }
1500 if (datalen > 0) { 1511 if (datalen > 0) {
1501 *data = HREAD1(hp, SDHC_DATA); 1512 *data = HREAD1(hp, SDHC_DATA);
1502 data += 1; 1513 data += 1;
1503 datalen -= 1; 1514 datalen -= 1;
1504 } 1515 }
1505 } else { 1516 } else {
1506 while (datalen > 0) { 1517 while (datalen > 0) {
1507 *data = HREAD1(hp, SDHC_DATA); 1518 *data = HREAD1(hp, SDHC_DATA);
1508 data += 1; 1519 data += 1;
1509 datalen -= 1; 1520 datalen -= 1;
1510 } 1521 }
1511 } 1522 }
1512} 1523}
1513 1524
1514static void 1525static void
1515sdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen) 1526sdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
1516{ 1527{
1517 1528
1518 if (((__uintptr_t)data & 3) == 0) { 1529 if (((__uintptr_t)data & 3) == 0) {
1519 while (datalen > 3) { 1530 while (datalen > 3) {
1520 HWRITE4(hp, SDHC_DATA, htole32(*(uint32_t *)data)); 1531 HWRITE4(hp, SDHC_DATA, htole32(*(uint32_t *)data));
1521 data += 4; 1532 data += 4;
1522 datalen -= 4; 1533 datalen -= 4;
1523 } 1534 }
1524 if (datalen > 1) { 1535 if (datalen > 1) {
1525 HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data)); 1536 HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
1526 data += 2; 1537 data += 2;
1527 datalen -= 2; 1538 datalen -= 2;
1528 } 1539 }
1529 if (datalen > 0) { 1540 if (datalen > 0) {
1530 HWRITE1(hp, SDHC_DATA, *data); 1541 HWRITE1(hp, SDHC_DATA, *data);
1531 data += 1; 1542 data += 1;
1532 datalen -= 1; 1543 datalen -= 1;
1533 } 1544 }
1534 } else if (((__uintptr_t)data & 1) == 0) { 1545 } else if (((__uintptr_t)data & 1) == 0) {
1535 while (datalen > 1) { 1546 while (datalen > 1) {
1536 HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data)); 1547 HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
1537 data += 2; 1548 data += 2;
1538 datalen -= 2; 1549 datalen -= 2;
1539 } 1550 }
1540 if (datalen > 0) { 1551 if (datalen > 0) {
1541 HWRITE1(hp, SDHC_DATA, *data); 1552 HWRITE1(hp, SDHC_DATA, *data);
1542 data += 1; 1553 data += 1;
1543 datalen -= 1; 1554 datalen -= 1;
1544 } 1555 }
1545 } else { 1556 } else {
1546 while (datalen > 0) { 1557 while (datalen > 0) {
1547 HWRITE1(hp, SDHC_DATA, *data); 1558 HWRITE1(hp, SDHC_DATA, *data);
1548 data += 1; 1559 data += 1;
1549 datalen -= 1; 1560 datalen -= 1;
1550 } 1561 }
1551 } 1562 }
1552} 1563}
1553 1564
1554static void 1565static void
1555esdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen) 1566esdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
1556{ 1567{
1557 uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS); 1568 uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
1558 uint32_t v; 1569 uint32_t v;
1559 1570
1560 const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_READ_SHIFT) & SDHC_WATERMARK_READ_MASK; 1571 const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_READ_SHIFT) & SDHC_WATERMARK_READ_MASK;
1561 size_t count = 0; 1572 size_t count = 0;
1562 1573
1563 while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) { 1574 while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
1564 if (count == 0) { 1575 if (count == 0) {
1565 /* 1576 /*
1566 * If we've drained "watermark" words, we need to wait 1577 * If we've drained "watermark" words, we need to wait
1567 * a little bit so the read FIFO can refill. 1578 * a little bit so the read FIFO can refill.
1568 */ 1579 */
1569 sdmmc_delay(10); 1580 sdmmc_delay(10);
1570 count = watermark; 1581 count = watermark;
1571 } 1582 }
1572 v = HREAD4(hp, SDHC_DATA); 1583 v = HREAD4(hp, SDHC_DATA);
1573 v = le32toh(v); 1584 v = le32toh(v);
1574 *(uint32_t *)data = v; 1585 *(uint32_t *)data = v;
1575 data += 4; 1586 data += 4;
1576 datalen -= 4; 1587 datalen -= 4;
1577 status = HREAD2(hp, SDHC_NINTR_STATUS); 1588 status = HREAD2(hp, SDHC_NINTR_STATUS);
1578 count--; 1589 count--;
1579 } 1590 }
1580 if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) { 1591 if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
1581 if (count == 0) { 1592 if (count == 0) {
1582 sdmmc_delay(10); 1593 sdmmc_delay(10);
1583 } 1594 }
1584 v = HREAD4(hp, SDHC_DATA); 1595 v = HREAD4(hp, SDHC_DATA);
1585 v = le32toh(v); 1596 v = le32toh(v);
1586 do { 1597 do {
1587 *data++ = v; 1598 *data++ = v;
1588 v >>= 8; 1599 v >>= 8;
1589 } while (--datalen > 0); 1600 } while (--datalen > 0);
1590 } 1601 }
1591} 1602}
1592 1603
1593static void 1604static void
1594esdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen) 1605esdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
1595{ 1606{
1596 uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS); 1607 uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
1597 uint32_t v; 1608 uint32_t v;
1598 1609
1599 const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_WRITE_SHIFT) & SDHC_WATERMARK_WRITE_MASK; 1610 const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_WRITE_SHIFT) & SDHC_WATERMARK_WRITE_MASK;
1600 size_t count = watermark; 1611 size_t count = watermark;
1601 1612
1602 while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) { 1613 while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
1603 if (count == 0) { 1614 if (count == 0) {
1604 sdmmc_delay(10); 1615 sdmmc_delay(10);
1605 count = watermark; 1616 count = watermark;
1606 } 1617 }
1607 v = *(uint32_t *)data; 1618 v = *(uint32_t *)data;
1608 v = htole32(v); 1619 v = htole32(v);
1609 HWRITE4(hp, SDHC_DATA, v); 1620 HWRITE4(hp, SDHC_DATA, v);
1610 data += 4; 1621 data += 4;
1611 datalen -= 4; 1622 datalen -= 4;
1612 status = HREAD2(hp, SDHC_NINTR_STATUS); 1623 status = HREAD2(hp, SDHC_NINTR_STATUS);
1613 count--; 1624 count--;
1614 } 1625 }
1615 if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) { 1626 if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
1616 if (count == 0) { 1627 if (count == 0) {
1617 sdmmc_delay(10); 1628 sdmmc_delay(10);
1618 } 1629 }
1619 v = *(uint32_t *)data; 1630 v = *(uint32_t *)data;
1620 v = htole32(v); 1631 v = htole32(v);
1621 HWRITE4(hp, SDHC_DATA, v); 1632 HWRITE4(hp, SDHC_DATA, v);
1622 } 1633 }
1623} 1634}
1624 1635
1625/* Prepare for another command. */ 1636/* Prepare for another command. */
1626static int 1637static int
1627sdhc_soft_reset(struct sdhc_host *hp, int mask) 1638sdhc_soft_reset(struct sdhc_host *hp, int mask)
1628{ 1639{
1629 int timo; 1640 int timo;
1630 1641
1631 DPRINTF(1,("%s: software reset reg=%08x\n", HDEVNAME(hp), mask)); 1642 DPRINTF(1,("%s: software reset reg=%08x\n", HDEVNAME(hp), mask));
1632 1643
1633 /* Request the reset. */ 1644 /* Request the reset. */
1634 HWRITE1(hp, SDHC_SOFTWARE_RESET, mask); 1645 HWRITE1(hp, SDHC_SOFTWARE_RESET, mask);
1635 1646
1636 /* 1647 /*
1637 * If necessary, wait for the controller to set the bits to 1648 * If necessary, wait for the controller to set the bits to
1638 * acknowledge the reset. 1649 * acknowledge the reset.
1639 */ 1650 */
1640 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_WAIT_RESET) && 1651 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_WAIT_RESET) &&
1641 ISSET(mask, (SDHC_RESET_DAT | SDHC_RESET_CMD))) { 1652 ISSET(mask, (SDHC_RESET_DAT | SDHC_RESET_CMD))) {
1642 for (timo = 10000; timo > 0; timo--) { 1653 for (timo = 10000; timo > 0; timo--) {
1643 if (ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask)) 1654 if (ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
1644 break; 1655 break;
1645 /* Short delay because I worry we may miss it... */ 1656 /* Short delay because I worry we may miss it... */
1646 sdmmc_delay(1); 1657 sdmmc_delay(1);
1647 } 1658 }
1648 if (timo == 0) 1659 if (timo == 0)
1649 return ETIMEDOUT; 1660 return ETIMEDOUT;
1650 } 1661 }
1651 1662
1652 /* 1663 /*
1653 * Wait for the controller to clear the bits to indicate that 1664 * Wait for the controller to clear the bits to indicate that
1654 * the reset has completed. 1665 * the reset has completed.
1655 */ 1666 */
1656 for (timo = 10; timo > 0; timo--) { 1667 for (timo = 10; timo > 0; timo--) {
1657 if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask)) 1668 if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
1658 break; 1669 break;
1659 sdmmc_delay(10000); 1670 sdmmc_delay(10000);
1660 } 1671 }
1661 if (timo == 0) { 1672 if (timo == 0) {
1662 DPRINTF(1,("%s: timeout reg=%08x\n", HDEVNAME(hp), 1673 DPRINTF(1,("%s: timeout reg=%08x\n", HDEVNAME(hp),
1663 HREAD1(hp, SDHC_SOFTWARE_RESET))); 1674 HREAD1(hp, SDHC_SOFTWARE_RESET)));
1664 return ETIMEDOUT; 1675 return ETIMEDOUT;
1665 } 1676 }
1666 1677
1667 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1678 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1668 HSET4(hp, SDHC_DMA_CTL, SDHC_DMA_SNOOP); 1679 HSET4(hp, SDHC_DMA_CTL, SDHC_DMA_SNOOP);
1669 } 1680 }
1670 1681
1671 return 0; 1682 return 0;
1672} 1683}
1673 1684
1674static int 1685static int
1675sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo) 1686sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo)
1676{ 1687{
1677 int status; 1688 int status;
1678 1689
1679 mask |= SDHC_ERROR_INTERRUPT; 1690 mask |= SDHC_ERROR_INTERRUPT;
1680 1691
1681 mutex_enter(&hp->intr_mtx); 1692 mutex_enter(&hp->intr_mtx);
1682 status = hp->intr_status & mask; 1693 status = hp->intr_status & mask;
1683 while (status == 0) { 1694 while (status == 0) {
1684 if (cv_timedwait(&hp->intr_cv, &hp->intr_mtx, timo) 1695 if (cv_timedwait(&hp->intr_cv, &hp->intr_mtx, timo)
1685 == EWOULDBLOCK) { 1696 == EWOULDBLOCK) {
1686 status |= SDHC_ERROR_INTERRUPT; 1697 status |= SDHC_ERROR_INTERRUPT;
1687 break; 1698 break;
1688 } 1699 }
1689 status = hp->intr_status & mask; 1700 status = hp->intr_status & mask;
1690 } 1701 }
1691 hp->intr_status &= ~status; 1702 hp->intr_status &= ~status;
1692 1703
1693 DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status, 1704 DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status,
1694 hp->intr_error_status)); 1705 hp->intr_error_status));
1695 1706
1696 /* Command timeout has higher priority than command complete. */ 1707 /* Command timeout has higher priority than command complete. */
1697 if (ISSET(status, SDHC_ERROR_INTERRUPT) || hp->intr_error_status) { 1708 if (ISSET(status, SDHC_ERROR_INTERRUPT) || hp->intr_error_status) {
1698 hp->intr_error_status = 0; 1709 hp->intr_error_status = 0;
1699 hp->intr_status &= ~SDHC_ERROR_INTERRUPT; 1710 hp->intr_status &= ~SDHC_ERROR_INTERRUPT;
1700 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1711 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1701 (void)sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD); 1712 (void)sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
1702 } 1713 }
1703 status = 0; 1714 status = 0;
1704 } 1715 }
1705 mutex_exit(&hp->intr_mtx); 1716 mutex_exit(&hp->intr_mtx);
1706 1717
1707 return status; 1718 return status;
1708} 1719}
1709 1720
1710/* 1721/*
1711 * Established by attachment driver at interrupt priority IPL_SDMMC. 1722 * Established by attachment driver at interrupt priority IPL_SDMMC.
1712 */ 1723 */
1713int 1724int
1714sdhc_intr(void *arg) 1725sdhc_intr(void *arg)
1715{ 1726{
1716 struct sdhc_softc *sc = (struct sdhc_softc *)arg; 1727 struct sdhc_softc *sc = (struct sdhc_softc *)arg;
1717 struct sdhc_host *hp; 1728 struct sdhc_host *hp;
1718 int done = 0; 1729 int done = 0;
1719 uint16_t status; 1730 uint16_t status;
1720 uint16_t error; 1731 uint16_t error;
1721 1732
1722 /* We got an interrupt, but we don't know from which slot. */ 1733 /* We got an interrupt, but we don't know from which slot. */
1723 for (size_t host = 0; host < sc->sc_nhosts; host++) { 1734 for (size_t host = 0; host < sc->sc_nhosts; host++) {
1724 hp = sc->sc_host[host]; 1735 hp = sc->sc_host[host];
1725 if (hp == NULL) 1736 if (hp == NULL)
1726 continue; 1737 continue;
1727 1738
1728 if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) { 1739 if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
1729 /* Find out which interrupts are pending. */ 1740 /* Find out which interrupts are pending. */
1730 uint32_t xstatus = HREAD4(hp, SDHC_NINTR_STATUS); 1741 uint32_t xstatus = HREAD4(hp, SDHC_NINTR_STATUS);
1731 status = xstatus; 1742 status = xstatus;
1732 error = xstatus >> 16; 1743 error = xstatus >> 16;
1733 if (error) 1744 if (error)
1734 xstatus |= SDHC_ERROR_INTERRUPT; 1745 xstatus |= SDHC_ERROR_INTERRUPT;
1735 else if (!ISSET(status, SDHC_NINTR_STATUS_MASK)) 1746 else if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
1736 continue; /* no interrupt for us */ 1747 continue; /* no interrupt for us */
1737 /* Acknowledge the interrupts we are about to handle. */ 1748 /* Acknowledge the interrupts we are about to handle. */
1738 HWRITE4(hp, SDHC_NINTR_STATUS, xstatus); 1749 HWRITE4(hp, SDHC_NINTR_STATUS, xstatus);
1739 } else { 1750 } else {
1740 /* Find out which interrupts are pending. */ 1751 /* Find out which interrupts are pending. */
1741 error = 0; 1752 error = 0;
1742 status = HREAD2(hp, SDHC_NINTR_STATUS); 1753 status = HREAD2(hp, SDHC_NINTR_STATUS);
1743 if (!ISSET(status, SDHC_NINTR_STATUS_MASK)) 1754 if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
1744 continue; /* no interrupt for us */ 1755 continue; /* no interrupt for us */
1745 /* Acknowledge the interrupts we are about to handle. */ 1756 /* Acknowledge the interrupts we are about to handle. */
1746 HWRITE2(hp, SDHC_NINTR_STATUS, status); 1757 HWRITE2(hp, SDHC_NINTR_STATUS, status);
1747 if (ISSET(status, SDHC_ERROR_INTERRUPT)) { 1758 if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
1748 /* Acknowledge error interrupts. */ 1759 /* Acknowledge error interrupts. */
1749 error = HREAD2(hp, SDHC_EINTR_STATUS); 1760 error = HREAD2(hp, SDHC_EINTR_STATUS);
1750 HWRITE2(hp, SDHC_EINTR_STATUS, error); 1761 HWRITE2(hp, SDHC_EINTR_STATUS, error);
1751 } 1762 }
1752 } 1763 }
1753 1764
1754 DPRINTF(2,("%s: interrupt status=%x error=%x\n", HDEVNAME(hp), 1765 DPRINTF(2,("%s: interrupt status=%x error=%x\n", HDEVNAME(hp),
1755 status, error)); 1766 status, error));
1756 1767
1757 mutex_enter(&hp->intr_mtx); 1768 mutex_enter(&hp->intr_mtx);
1758 1769
1759 /* Claim this interrupt. */ 1770 /* Claim this interrupt. */
1760 done = 1; 1771 done = 1;
1761 1772
1762 /* 1773 /*
1763 * Service error interrupts. 1774 * Service error interrupts.
1764 */ 1775 */
1765 if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR| 1776 if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR|
1766 SDHC_DATA_TIMEOUT_ERROR)) { 1777 SDHC_DATA_TIMEOUT_ERROR)) {
1767 hp->intr_error_status |= error; 1778 hp->intr_error_status |= error;
1768 hp->intr_status |= status; 1779 hp->intr_status |= status;
1769 cv_broadcast(&hp->intr_cv); 1780 cv_broadcast(&hp->intr_cv);
1770 } 1781 }
1771 1782
1772 /* 1783 /*
1773 * Wake up the sdmmc event thread to scan for cards. 1784 * Wake up the sdmmc event thread to scan for cards.
1774 */ 1785 */
1775 if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION)) { 1786 if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION)) {
1776 if (hp->sdmmc != NULL) { 1787 if (hp->sdmmc != NULL) {
1777 sdmmc_needs_discover(hp->sdmmc); 1788 sdmmc_needs_discover(hp->sdmmc);
1778 } 1789 }
1779 if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1790 if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1780 HCLR4(hp, SDHC_NINTR_STATUS_EN, 1791 HCLR4(hp, SDHC_NINTR_STATUS_EN,
1781 status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION)); 1792 status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
1782 HCLR4(hp, SDHC_NINTR_SIGNAL_EN, 1793 HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
1783 status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION)); 1794 status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
1784 } 1795 }
1785 } 1796 }
1786 1797
1787 /* 1798 /*
1788 * Wake up the blocking process to service command 1799 * Wake up the blocking process to service command
1789 * related interrupt(s). 1800 * related interrupt(s).
1790 */ 1801 */
1791 if (ISSET(status, SDHC_COMMAND_COMPLETE| 1802 if (ISSET(status, SDHC_COMMAND_COMPLETE|
1792 SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY| 1803 SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY|
1793 SDHC_TRANSFER_COMPLETE|SDHC_DMA_INTERRUPT)) { 1804 SDHC_TRANSFER_COMPLETE|SDHC_DMA_INTERRUPT)) {
1794 hp->intr_status |= status; 1805 hp->intr_status |= status;
1795 if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) { 1806 if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1796 HCLR4(hp, SDHC_NINTR_SIGNAL_EN, 1807 HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
1797 status & (SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY)); 1808 status & (SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY));
1798 } 1809 }
1799 cv_broadcast(&hp->intr_cv); 1810 cv_broadcast(&hp->intr_cv);
1800 } 1811 }
1801 1812
1802 /* 1813 /*
1803 * Service SD card interrupts. 1814 * Service SD card interrupts.
1804 */ 1815 */
1805 if (!ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED) 1816 if (!ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)
1806 && ISSET(status, SDHC_CARD_INTERRUPT)) { 1817 && ISSET(status, SDHC_CARD_INTERRUPT)) {
1807 DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp))); 1818 DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp)));
1808 HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT); 1819 HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
1809 sdmmc_card_intr(hp->sdmmc); 1820 sdmmc_card_intr(hp->sdmmc);
1810 } 1821 }
1811 mutex_exit(&hp->intr_mtx); 1822 mutex_exit(&hp->intr_mtx);
1812 } 1823 }
1813 1824
1814 return done; 1825 return done;
1815} 1826}
1816 1827
1817#ifdef SDHC_DEBUG 1828#ifdef SDHC_DEBUG
1818void 1829void
1819sdhc_dump_regs(struct sdhc_host *hp) 1830sdhc_dump_regs(struct sdhc_host *hp)
1820{ 1831{
1821 1832
1822 printf("0x%02x PRESENT_STATE: %x\n", SDHC_PRESENT_STATE, 1833 printf("0x%02x PRESENT_STATE: %x\n", SDHC_PRESENT_STATE,
1823 HREAD4(hp, SDHC_PRESENT_STATE)); 1834 HREAD4(hp, SDHC_PRESENT_STATE));
1824 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) 1835 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
1825 printf("0x%02x POWER_CTL: %x\n", SDHC_POWER_CTL, 1836 printf("0x%02x POWER_CTL: %x\n", SDHC_POWER_CTL,
1826 HREAD1(hp, SDHC_POWER_CTL)); 1837 HREAD1(hp, SDHC_POWER_CTL));
1827 printf("0x%02x NINTR_STATUS: %x\n", SDHC_NINTR_STATUS, 1838 printf("0x%02x NINTR_STATUS: %x\n", SDHC_NINTR_STATUS,
1828 HREAD2(hp, SDHC_NINTR_STATUS)); 1839 HREAD2(hp, SDHC_NINTR_STATUS));
1829 printf("0x%02x EINTR_STATUS: %x\n", SDHC_EINTR_STATUS, 1840 printf("0x%02x EINTR_STATUS: %x\n", SDHC_EINTR_STATUS,
1830 HREAD2(hp, SDHC_EINTR_STATUS)); 1841 HREAD2(hp, SDHC_EINTR_STATUS));
1831 printf("0x%02x NINTR_STATUS_EN: %x\n", SDHC_NINTR_STATUS_EN, 1842 printf("0x%02x NINTR_STATUS_EN: %x\n", SDHC_NINTR_STATUS_EN,
1832 HREAD2(hp, SDHC_NINTR_STATUS_EN)); 1843 HREAD2(hp, SDHC_NINTR_STATUS_EN));
1833 printf("0x%02x EINTR_STATUS_EN: %x\n", SDHC_EINTR_STATUS_EN, 1844 printf("0x%02x EINTR_STATUS_EN: %x\n", SDHC_EINTR_STATUS_EN,
1834 HREAD2(hp, SDHC_EINTR_STATUS_EN)); 1845 HREAD2(hp, SDHC_EINTR_STATUS_EN));
1835 printf("0x%02x NINTR_SIGNAL_EN: %x\n", SDHC_NINTR_SIGNAL_EN, 1846 printf("0x%02x NINTR_SIGNAL_EN: %x\n", SDHC_NINTR_SIGNAL_EN,
1836 HREAD2(hp, SDHC_NINTR_SIGNAL_EN)); 1847 HREAD2(hp, SDHC_NINTR_SIGNAL_EN));
1837 printf("0x%02x EINTR_SIGNAL_EN: %x\n", SDHC_EINTR_SIGNAL_EN, 1848 printf("0x%02x EINTR_SIGNAL_EN: %x\n", SDHC_EINTR_SIGNAL_EN,
1838 HREAD2(hp, SDHC_EINTR_SIGNAL_EN)); 1849 HREAD2(hp, SDHC_EINTR_SIGNAL_EN));
1839 printf("0x%02x CAPABILITIES: %x\n", SDHC_CAPABILITIES, 1850 printf("0x%02x CAPABILITIES: %x\n", SDHC_CAPABILITIES,
1840 HREAD4(hp, SDHC_CAPABILITIES)); 1851 HREAD4(hp, SDHC_CAPABILITIES));
1841 printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES, 1852 printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES,
1842 HREAD4(hp, SDHC_MAX_CAPABILITIES)); 1853 HREAD4(hp, SDHC_MAX_CAPABILITIES));
1843} 1854}
1844#endif 1855#endif

cvs diff -r1.18 -r1.19 src/sys/dev/sdmmc/sdhcvar.h (switch to unified diff)

--- src/sys/dev/sdmmc/sdhcvar.h 2015/05/03 11:46:25 1.18
+++ src/sys/dev/sdmmc/sdhcvar.h 2015/07/23 23:52:54 1.19
@@ -1,78 +1,79 @@ @@ -1,78 +1,79 @@
1/* $NetBSD: sdhcvar.h,v 1.18 2015/05/03 11:46:25 jmcneill Exp $ */ 1/* $NetBSD: sdhcvar.h,v 1.19 2015/07/23 23:52:54 jmcneill Exp $ */
2/* $OpenBSD: sdhcvar.h,v 1.3 2007/09/06 08:01:01 jsg Exp $ */ 2/* $OpenBSD: sdhcvar.h,v 1.3 2007/09/06 08:01:01 jsg 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#ifndef _SDHCVAR_H_ 20#ifndef _SDHCVAR_H_
21#define _SDHCVAR_H_ 21#define _SDHCVAR_H_
22 22
23#include <sys/bus.h> 23#include <sys/bus.h>
24#include <sys/device.h> 24#include <sys/device.h>
25#include <sys/pmf.h> 25#include <sys/pmf.h>
26 26
27struct sdhc_host; 27struct sdhc_host;
28struct sdmmc_command; 28struct sdmmc_command;
29 29
30struct sdhc_softc { 30struct sdhc_softc {
31 device_t sc_dev; 31 device_t sc_dev;
32 32
33 struct sdhc_host **sc_host; 33 struct sdhc_host **sc_host;
34 int sc_nhosts; 34 int sc_nhosts;
35 35
36 bus_dma_tag_t sc_dmat; 36 bus_dma_tag_t sc_dmat;
37 37
38 uint32_t sc_flags; 38 uint32_t sc_flags;
39#define SDHC_FLAG_USE_DMA 0x00000001 39#define SDHC_FLAG_USE_DMA 0x00000001
40#define SDHC_FLAG_FORCE_DMA 0x00000002 40#define SDHC_FLAG_FORCE_DMA 0x00000002
41#define SDHC_FLAG_NO_PWR0 0x00000004 /* Freescale ESDHC */ 41#define SDHC_FLAG_NO_PWR0 0x00000004 /* Freescale ESDHC */
42#define SDHC_FLAG_HAVE_DVS 0x00000008 /* Freescale ESDHC */ 42#define SDHC_FLAG_HAVE_DVS 0x00000008 /* Freescale ESDHC */
43#define SDHC_FLAG_32BIT_ACCESS 0x00000010 /* Freescale ESDHC */ 43#define SDHC_FLAG_32BIT_ACCESS 0x00000010 /* Freescale ESDHC */
44#define SDHC_FLAG_ENHANCED 0x00000020 /* Freescale ESDHC */ 44#define SDHC_FLAG_ENHANCED 0x00000020 /* Freescale ESDHC */
45#define SDHC_FLAG_8BIT_MODE 0x00000040 /* MMC 8bit mode is supported */ 45#define SDHC_FLAG_8BIT_MODE 0x00000040 /* MMC 8bit mode is supported */
46#define SDHC_FLAG_HAVE_CGM 0x00000080 /* Netlogic XLP */ 46#define SDHC_FLAG_HAVE_CGM 0x00000080 /* Netlogic XLP */
47#define SDHC_FLAG_NO_LED_ON 0x00000100 /* LED_ON unsupported in HOST_CTL */ 47#define SDHC_FLAG_NO_LED_ON 0x00000100 /* LED_ON unsupported in HOST_CTL */
48#define SDHC_FLAG_HOSTCAPS 0x00000200 /* No device provided capabilities */ 48#define SDHC_FLAG_HOSTCAPS 0x00000200 /* No device provided capabilities */
49#define SDHC_FLAG_RSP136_CRC 0x00000400 /* Resp 136 with CRC and end-bit */ 49#define SDHC_FLAG_RSP136_CRC 0x00000400 /* Resp 136 with CRC and end-bit */
50#define SDHC_FLAG_SINGLE_ONLY 0x00000800 /* Single transfer only */ 50#define SDHC_FLAG_SINGLE_ONLY 0x00000800 /* Single transfer only */
51#define SDHC_FLAG_WAIT_RESET 0x00001000 /* Wait for soft resets to start */ 51#define SDHC_FLAG_WAIT_RESET 0x00001000 /* Wait for soft resets to start */
52#define SDHC_FLAG_NO_HS_BIT 0x00002000 /* Don't set SDHC_HIGH_SPEED bit */ 52#define SDHC_FLAG_NO_HS_BIT 0x00002000 /* Don't set SDHC_HIGH_SPEED bit */
53#define SDHC_FLAG_EXTERNAL_DMA 0x00004000 53#define SDHC_FLAG_EXTERNAL_DMA 0x00004000
54#define SDHC_FLAG_EXTDMA_DMAEN 0x00008000 /* ext. dma need SDHC_DMA_ENABLE */ 54#define SDHC_FLAG_EXTDMA_DMAEN 0x00008000 /* ext. dma need SDHC_DMA_ENABLE */
55#define SDHC_FLAG_NO_CLKBASE 0x00020000 /* ignore clkbase register */ 55#define SDHC_FLAG_NO_CLKBASE 0x00020000 /* ignore clkbase register */
56#define SDHC_FLAG_SINGLE_POWER_WRITE 0x00040000 56#define SDHC_FLAG_SINGLE_POWER_WRITE 0x00040000
 57#define SDHC_FLAG_NO_TIMEOUT 0x00080000 /* ignore timeout interrupts */
57 58
58 uint32_t sc_clkbase; 59 uint32_t sc_clkbase;
59 int sc_clkmsk; /* Mask for SDCLK */ 60 int sc_clkmsk; /* Mask for SDCLK */
60 uint32_t sc_caps;/* attachment provided capabilities */ 61 uint32_t sc_caps;/* attachment provided capabilities */
61 62
62 int (*sc_vendor_rod)(struct sdhc_softc *, int); 63 int (*sc_vendor_rod)(struct sdhc_softc *, int);
63 int (*sc_vendor_write_protect)(struct sdhc_softc *); 64 int (*sc_vendor_write_protect)(struct sdhc_softc *);
64 int (*sc_vendor_card_detect)(struct sdhc_softc *); 65 int (*sc_vendor_card_detect)(struct sdhc_softc *);
65 int (*sc_vendor_bus_clock)(struct sdhc_softc *, int); 66 int (*sc_vendor_bus_clock)(struct sdhc_softc *, int);
66 int (*sc_vendor_transfer_data_dma)(struct sdhc_softc *, struct sdmmc_command *); 67 int (*sc_vendor_transfer_data_dma)(struct sdhc_softc *, struct sdmmc_command *);
67}; 68};
68 69
69/* Host controller functions called by the attachment driver. */ 70/* Host controller functions called by the attachment driver. */
70int sdhc_host_found(struct sdhc_softc *, bus_space_tag_t, 71int sdhc_host_found(struct sdhc_softc *, bus_space_tag_t,
71 bus_space_handle_t, bus_size_t); 72 bus_space_handle_t, bus_size_t);
72int sdhc_intr(void *); 73int sdhc_intr(void *);
73int sdhc_detach(struct sdhc_softc *, int); 74int sdhc_detach(struct sdhc_softc *, int);
74bool sdhc_suspend(device_t, const pmf_qual_t *); 75bool sdhc_suspend(device_t, const pmf_qual_t *);
75bool sdhc_resume(device_t, const pmf_qual_t *); 76bool sdhc_resume(device_t, const pmf_qual_t *);
76bool sdhc_shutdown(device_t, int); 77bool sdhc_shutdown(device_t, int);
77 78
78#endif /* _SDHCVAR_H_ */ 79#endif /* _SDHCVAR_H_ */