xref: /freebsd/sys/dev/sdhci/sdhci.c (revision 3f05af05ace08ae28892ecfd28b000822a5d7ae0)
1 /*-
2  * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/callout.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/resource.h>
39 #include <sys/rman.h>
40 #include <sys/sysctl.h>
41 #include <sys/taskqueue.h>
42 
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <machine/stdarg.h>
46 
47 #include <dev/mmc/bridge.h>
48 #include <dev/mmc/mmcreg.h>
49 #include <dev/mmc/mmcbrvar.h>
50 
51 #include "mmcbr_if.h"
52 #include "sdhci.h"
53 #include "sdhci_if.h"
54 
55 SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver");
56 
57 static int sdhci_debug;
58 SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, "Debug level");
59 
60 #define RD1(slot, off)	SDHCI_READ_1((slot)->bus, (slot), (off))
61 #define RD2(slot, off)	SDHCI_READ_2((slot)->bus, (slot), (off))
62 #define RD4(slot, off)	SDHCI_READ_4((slot)->bus, (slot), (off))
63 #define RD_MULTI_4(slot, off, ptr, count)	\
64     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
65 
66 #define WR1(slot, off, val)	SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
67 #define WR2(slot, off, val)	SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
68 #define WR4(slot, off, val)	SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
69 #define WR_MULTI_4(slot, off, ptr, count)	\
70     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
71 
72 static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
73 static void sdhci_start(struct sdhci_slot *slot);
74 static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data);
75 
76 static void sdhci_card_task(void *, int);
77 
78 /* helper routines */
79 #define SDHCI_LOCK(_slot)		mtx_lock(&(_slot)->mtx)
80 #define	SDHCI_UNLOCK(_slot)		mtx_unlock(&(_slot)->mtx)
81 #define SDHCI_LOCK_INIT(_slot) \
82 	mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
83 #define SDHCI_LOCK_DESTROY(_slot)	mtx_destroy(&_slot->mtx);
84 #define SDHCI_ASSERT_LOCKED(_slot)	mtx_assert(&_slot->mtx, MA_OWNED);
85 #define SDHCI_ASSERT_UNLOCKED(_slot)	mtx_assert(&_slot->mtx, MA_NOTOWNED);
86 
87 #define	SDHCI_DEFAULT_MAX_FREQ	50
88 
89 #define	SDHCI_200_MAX_DIVIDER	256
90 #define	SDHCI_300_MAX_DIVIDER	2046
91 
92 static void
93 sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
94 {
95 	if (error != 0) {
96 		printf("getaddr: error %d\n", error);
97 		return;
98 	}
99 	*(bus_addr_t *)arg = segs[0].ds_addr;
100 }
101 
102 static int
103 slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
104 {
105 	va_list ap;
106 	int retval;
107 
108     	retval = printf("%s-slot%d: ",
109 	    device_get_nameunit(slot->bus), slot->num);
110 
111 	va_start(ap, fmt);
112 	retval += vprintf(fmt, ap);
113 	va_end(ap);
114 	return (retval);
115 }
116 
117 static void
118 sdhci_dumpregs(struct sdhci_slot *slot)
119 {
120 	slot_printf(slot,
121 	    "============== REGISTER DUMP ==============\n");
122 
123 	slot_printf(slot, "Sys addr: 0x%08x | Version:  0x%08x\n",
124 	    RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
125 	slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
126 	    RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
127 	slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n",
128 	    RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
129 	slot_printf(slot, "Present:  0x%08x | Host ctl: 0x%08x\n",
130 	    RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
131 	slot_printf(slot, "Power:    0x%08x | Blk gap:  0x%08x\n",
132 	    RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
133 	slot_printf(slot, "Wake-up:  0x%08x | Clock:    0x%08x\n",
134 	    RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
135 	slot_printf(slot, "Timeout:  0x%08x | Int stat: 0x%08x\n",
136 	    RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
137 	slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
138 	    RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
139 	slot_printf(slot, "AC12 err: 0x%08x | Slot int: 0x%08x\n",
140 	    RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_SLOT_INT_STATUS));
141 	slot_printf(slot, "Caps:     0x%08x | Max curr: 0x%08x\n",
142 	    RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_MAX_CURRENT));
143 
144 	slot_printf(slot,
145 	    "===========================================\n");
146 }
147 
148 static void
149 sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
150 {
151 	int timeout;
152 
153 	if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
154 		if (!(RD4(slot, SDHCI_PRESENT_STATE) &
155 			SDHCI_CARD_PRESENT))
156 			return;
157 	}
158 
159 	/* Some controllers need this kick or reset won't work. */
160 	if ((mask & SDHCI_RESET_ALL) == 0 &&
161 	    (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
162 		uint32_t clock;
163 
164 		/* This is to force an update */
165 		clock = slot->clock;
166 		slot->clock = 0;
167 		sdhci_set_clock(slot, clock);
168 	}
169 
170 	if (mask & SDHCI_RESET_ALL) {
171 		slot->clock = 0;
172 		slot->power = 0;
173 	}
174 
175 	WR1(slot, SDHCI_SOFTWARE_RESET, mask);
176 
177 	if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
178 		/*
179 		 * Resets on TI OMAPs and AM335x are incompatible with SDHCI
180 		 * specification.  The reset bit has internal propagation delay,
181 		 * so a fast read after write returns 0 even if reset process is
182 		 * in progress. The workaround is to poll for 1 before polling
183 		 * for 0.  In the worst case, if we miss seeing it asserted the
184 		 * time we spent waiting is enough to ensure the reset finishes.
185 		 */
186 		timeout = 10000;
187 		while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
188 			if (timeout <= 0)
189 				break;
190 			timeout--;
191 			DELAY(1);
192 		}
193 	}
194 
195 	/* Wait max 100 ms */
196 	timeout = 10000;
197 	/* Controller clears the bits when it's done */
198 	while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
199 		if (timeout <= 0) {
200 			slot_printf(slot, "Reset 0x%x never completed.\n",
201 			    mask);
202 			sdhci_dumpregs(slot);
203 			return;
204 		}
205 		timeout--;
206 		DELAY(10);
207 	}
208 }
209 
210 static void
211 sdhci_init(struct sdhci_slot *slot)
212 {
213 
214 	sdhci_reset(slot, SDHCI_RESET_ALL);
215 
216 	/* Enable interrupts. */
217 	slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
218 	    SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
219 	    SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
220 	    SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
221 	    SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
222 	    SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
223 	    SDHCI_INT_ACMD12ERR;
224 	WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
225 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
226 }
227 
228 static void
229 sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
230 {
231 	uint32_t res;
232 	uint16_t clk;
233 	uint16_t div;
234 	int timeout;
235 
236 	if (clock == slot->clock)
237 		return;
238 	slot->clock = clock;
239 
240 	/* Turn off the clock. */
241 	clk = RD2(slot, SDHCI_CLOCK_CONTROL);
242 	WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
243 	/* If no clock requested - left it so. */
244 	if (clock == 0)
245 		return;
246 
247 	/* Recalculate timeout clock frequency based on the new sd clock. */
248 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
249 		slot->timeout_clk = slot->clock / 1000;
250 
251 	if (slot->version < SDHCI_SPEC_300) {
252 		/* Looking for highest freq <= clock. */
253 		res = slot->max_clk;
254 		for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
255 			if (res <= clock)
256 				break;
257 			res >>= 1;
258 		}
259 		/* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
260 		div >>= 1;
261 	}
262 	else {
263 		/* Version 3.0 divisors are multiples of two up to 1023*2 */
264 		if (clock >= slot->max_clk)
265 			div = 0;
266 		else {
267 			for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
268 				if ((slot->max_clk / div) <= clock)
269 					break;
270 			}
271 		}
272 		div >>= 1;
273 	}
274 
275 	if (bootverbose || sdhci_debug)
276 		slot_printf(slot, "Divider %d for freq %d (max %d)\n",
277 			div, clock, slot->max_clk);
278 
279 	/* Now we have got divider, set it. */
280 	clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
281 	clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
282 		<< SDHCI_DIVIDER_HI_SHIFT;
283 
284 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
285 	/* Enable clock. */
286 	clk |= SDHCI_CLOCK_INT_EN;
287 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
288 	/* Wait up to 10 ms until it stabilize. */
289 	timeout = 10;
290 	while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
291 		& SDHCI_CLOCK_INT_STABLE)) {
292 		if (timeout == 0) {
293 			slot_printf(slot,
294 			    "Internal clock never stabilised.\n");
295 			sdhci_dumpregs(slot);
296 			return;
297 		}
298 		timeout--;
299 		DELAY(1000);
300 	}
301 	/* Pass clock signal to the bus. */
302 	clk |= SDHCI_CLOCK_CARD_EN;
303 	WR2(slot, SDHCI_CLOCK_CONTROL, clk);
304 }
305 
306 static void
307 sdhci_set_power(struct sdhci_slot *slot, u_char power)
308 {
309 	uint8_t pwr;
310 
311 	if (slot->power == power)
312 		return;
313 
314 	slot->power = power;
315 
316 	/* Turn off the power. */
317 	pwr = 0;
318 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
319 	/* If power down requested - left it so. */
320 	if (power == 0)
321 		return;
322 	/* Set voltage. */
323 	switch (1 << power) {
324 	case MMC_OCR_LOW_VOLTAGE:
325 		pwr |= SDHCI_POWER_180;
326 		break;
327 	case MMC_OCR_290_300:
328 	case MMC_OCR_300_310:
329 		pwr |= SDHCI_POWER_300;
330 		break;
331 	case MMC_OCR_320_330:
332 	case MMC_OCR_330_340:
333 		pwr |= SDHCI_POWER_330;
334 		break;
335 	}
336 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
337 	/* Turn on the power. */
338 	pwr |= SDHCI_POWER_ON;
339 	WR1(slot, SDHCI_POWER_CONTROL, pwr);
340 }
341 
342 static void
343 sdhci_read_block_pio(struct sdhci_slot *slot)
344 {
345 	uint32_t data;
346 	char *buffer;
347 	size_t left;
348 
349 	buffer = slot->curcmd->data->data;
350 	buffer += slot->offset;
351 	/* Transfer one block at a time. */
352 	left = min(512, slot->curcmd->data->len - slot->offset);
353 	slot->offset += left;
354 
355 	/* If we are too fast, broken controllers return zeroes. */
356 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
357 		DELAY(10);
358 	/* Handle unaligned and aligned buffer cases. */
359 	if ((intptr_t)buffer & 3) {
360 		while (left > 3) {
361 			data = RD4(slot, SDHCI_BUFFER);
362 			buffer[0] = data;
363 			buffer[1] = (data >> 8);
364 			buffer[2] = (data >> 16);
365 			buffer[3] = (data >> 24);
366 			buffer += 4;
367 			left -= 4;
368 		}
369 	} else {
370 		RD_MULTI_4(slot, SDHCI_BUFFER,
371 		    (uint32_t *)buffer, left >> 2);
372 		left &= 3;
373 	}
374 	/* Handle uneven size case. */
375 	if (left > 0) {
376 		data = RD4(slot, SDHCI_BUFFER);
377 		while (left > 0) {
378 			*(buffer++) = data;
379 			data >>= 8;
380 			left--;
381 		}
382 	}
383 }
384 
385 static void
386 sdhci_write_block_pio(struct sdhci_slot *slot)
387 {
388 	uint32_t data = 0;
389 	char *buffer;
390 	size_t left;
391 
392 	buffer = slot->curcmd->data->data;
393 	buffer += slot->offset;
394 	/* Transfer one block at a time. */
395 	left = min(512, slot->curcmd->data->len - slot->offset);
396 	slot->offset += left;
397 
398 	/* Handle unaligned and aligned buffer cases. */
399 	if ((intptr_t)buffer & 3) {
400 		while (left > 3) {
401 			data = buffer[0] +
402 			    (buffer[1] << 8) +
403 			    (buffer[2] << 16) +
404 			    (buffer[3] << 24);
405 			left -= 4;
406 			buffer += 4;
407 			WR4(slot, SDHCI_BUFFER, data);
408 		}
409 	} else {
410 		WR_MULTI_4(slot, SDHCI_BUFFER,
411 		    (uint32_t *)buffer, left >> 2);
412 		left &= 3;
413 	}
414 	/* Handle uneven size case. */
415 	if (left > 0) {
416 		while (left > 0) {
417 			data <<= 8;
418 			data += *(buffer++);
419 			left--;
420 		}
421 		WR4(slot, SDHCI_BUFFER, data);
422 	}
423 }
424 
425 static void
426 sdhci_transfer_pio(struct sdhci_slot *slot)
427 {
428 
429 	/* Read as many blocks as possible. */
430 	if (slot->curcmd->data->flags & MMC_DATA_READ) {
431 		while (RD4(slot, SDHCI_PRESENT_STATE) &
432 		    SDHCI_DATA_AVAILABLE) {
433 			sdhci_read_block_pio(slot);
434 			if (slot->offset >= slot->curcmd->data->len)
435 				break;
436 		}
437 	} else {
438 		while (RD4(slot, SDHCI_PRESENT_STATE) &
439 		    SDHCI_SPACE_AVAILABLE) {
440 			sdhci_write_block_pio(slot);
441 			if (slot->offset >= slot->curcmd->data->len)
442 				break;
443 		}
444 	}
445 }
446 
447 static void
448 sdhci_card_delay(void *arg)
449 {
450 	struct sdhci_slot *slot = arg;
451 
452 	taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
453 }
454 
455 static void
456 sdhci_card_task(void *arg, int pending)
457 {
458 	struct sdhci_slot *slot = arg;
459 
460 	SDHCI_LOCK(slot);
461 	if (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT) {
462 		if (slot->dev == NULL) {
463 			/* If card is present - attach mmc bus. */
464 			slot->dev = device_add_child(slot->bus, "mmc", -1);
465 			device_set_ivars(slot->dev, slot);
466 			SDHCI_UNLOCK(slot);
467 			device_probe_and_attach(slot->dev);
468 		} else
469 			SDHCI_UNLOCK(slot);
470 	} else {
471 		if (slot->dev != NULL) {
472 			/* If no card present - detach mmc bus. */
473 			device_t d = slot->dev;
474 			slot->dev = NULL;
475 			SDHCI_UNLOCK(slot);
476 			device_delete_child(slot->bus, d);
477 		} else
478 			SDHCI_UNLOCK(slot);
479 	}
480 }
481 
482 int
483 sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
484 {
485 	uint32_t caps, freq;
486 	int err;
487 
488 	SDHCI_LOCK_INIT(slot);
489 	slot->num = num;
490 	slot->bus = dev;
491 
492 	/* Allocate DMA tag. */
493 	err = bus_dma_tag_create(bus_get_dma_tag(dev),
494 	    DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
495 	    BUS_SPACE_MAXADDR, NULL, NULL,
496 	    DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE,
497 	    BUS_DMA_ALLOCNOW, NULL, NULL,
498 	    &slot->dmatag);
499 	if (err != 0) {
500 		device_printf(dev, "Can't create DMA tag\n");
501 		SDHCI_LOCK_DESTROY(slot);
502 		return (err);
503 	}
504 	/* Allocate DMA memory. */
505 	err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
506 	    BUS_DMA_NOWAIT, &slot->dmamap);
507 	if (err != 0) {
508 		device_printf(dev, "Can't alloc DMA memory\n");
509 		SDHCI_LOCK_DESTROY(slot);
510 		return (err);
511 	}
512 	/* Map the memory. */
513 	err = bus_dmamap_load(slot->dmatag, slot->dmamap,
514 	    (void *)slot->dmamem, DMA_BLOCK_SIZE,
515 	    sdhci_getaddr, &slot->paddr, 0);
516 	if (err != 0 || slot->paddr == 0) {
517 		device_printf(dev, "Can't load DMA memory\n");
518 		SDHCI_LOCK_DESTROY(slot);
519 		if(err)
520 			return (err);
521 		else
522 			return (EFAULT);
523 	}
524 
525 	/* Initialize slot. */
526 	sdhci_init(slot);
527 	slot->version = (RD2(slot, SDHCI_HOST_VERSION)
528 		>> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
529 	if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS)
530 		caps = slot->caps;
531 	else
532 		caps = RD4(slot, SDHCI_CAPABILITIES);
533 	/* Calculate base clock frequency. */
534 	if (slot->version >= SDHCI_SPEC_300)
535 		freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
536 		    SDHCI_CLOCK_BASE_SHIFT;
537 	else
538 		freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
539 		    SDHCI_CLOCK_BASE_SHIFT;
540 	if (freq != 0)
541 		slot->max_clk = freq * 1000000;
542 	/*
543 	 * If the frequency wasn't in the capabilities and the hardware driver
544 	 * hasn't already set max_clk we're probably not going to work right
545 	 * with an assumption, so complain about it.
546 	 */
547 	if (slot->max_clk == 0) {
548 		slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
549 		device_printf(dev, "Hardware doesn't specify base clock "
550 		    "frequency, using %dMHz as default.\n", SDHCI_DEFAULT_MAX_FREQ);
551 	}
552 	/* Calculate timeout clock frequency. */
553 	if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
554 		slot->timeout_clk = slot->max_clk / 1000;
555 	} else {
556 		slot->timeout_clk =
557 			(caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
558 		if (caps & SDHCI_TIMEOUT_CLK_UNIT)
559 			slot->timeout_clk *= 1000;
560 	}
561 	/*
562 	 * If the frequency wasn't in the capabilities and the hardware driver
563 	 * hasn't already set timeout_clk we'll probably work okay using the
564 	 * max timeout, but still mention it.
565 	 */
566 	if (slot->timeout_clk == 0) {
567 		device_printf(dev, "Hardware doesn't specify timeout clock "
568 		    "frequency, setting BROKEN_TIMEOUT quirk.\n");
569 		slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
570 	}
571 
572 	slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
573 	slot->host.f_max = slot->max_clk;
574 	slot->host.host_ocr = 0;
575 	if (caps & SDHCI_CAN_VDD_330)
576 	    slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
577 	if (caps & SDHCI_CAN_VDD_300)
578 	    slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
579 	if (caps & SDHCI_CAN_VDD_180)
580 	    slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
581 	if (slot->host.host_ocr == 0) {
582 		device_printf(dev, "Hardware doesn't report any "
583 		    "support voltages.\n");
584 	}
585 	slot->host.caps = MMC_CAP_4_BIT_DATA;
586 	if (caps & SDHCI_CAN_DO_8BITBUS)
587 		slot->host.caps |= MMC_CAP_8_BIT_DATA;
588 	if (caps & SDHCI_CAN_DO_HISPD)
589 		slot->host.caps |= MMC_CAP_HSPEED;
590 	/* Decide if we have usable DMA. */
591 	if (caps & SDHCI_CAN_DO_DMA)
592 		slot->opt |= SDHCI_HAVE_DMA;
593 
594 	if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
595 		slot->opt &= ~SDHCI_HAVE_DMA;
596 	if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
597 		slot->opt |= SDHCI_HAVE_DMA;
598 
599 	/*
600 	 * Use platform-provided transfer backend
601 	 * with PIO as a fallback mechanism
602 	 */
603 	if (slot->opt & SDHCI_PLATFORM_TRANSFER)
604 		slot->opt &= ~SDHCI_HAVE_DMA;
605 
606 	if (bootverbose || sdhci_debug) {
607 		slot_printf(slot, "%uMHz%s %s%s%s%s %s\n",
608 		    slot->max_clk / 1000000,
609 		    (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
610 		    (caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
611 			((caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
612 		    (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
613 		    (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
614 		    (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "",
615 		    (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO");
616 		sdhci_dumpregs(slot);
617 	}
618 
619 	TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
620 	callout_init(&slot->card_callout, 1);
621 	callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
622 	return (0);
623 }
624 
625 void
626 sdhci_start_slot(struct sdhci_slot *slot)
627 {
628 	sdhci_card_task(slot, 0);
629 }
630 
631 int
632 sdhci_cleanup_slot(struct sdhci_slot *slot)
633 {
634 	device_t d;
635 
636 	callout_drain(&slot->timeout_callout);
637 	callout_drain(&slot->card_callout);
638 	taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
639 
640 	SDHCI_LOCK(slot);
641 	d = slot->dev;
642 	slot->dev = NULL;
643 	SDHCI_UNLOCK(slot);
644 	if (d != NULL)
645 		device_delete_child(slot->bus, d);
646 
647 	SDHCI_LOCK(slot);
648 	sdhci_reset(slot, SDHCI_RESET_ALL);
649 	SDHCI_UNLOCK(slot);
650 	bus_dmamap_unload(slot->dmatag, slot->dmamap);
651 	bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
652 	bus_dma_tag_destroy(slot->dmatag);
653 
654 	SDHCI_LOCK_DESTROY(slot);
655 
656 	return (0);
657 }
658 
659 int
660 sdhci_generic_suspend(struct sdhci_slot *slot)
661 {
662 	sdhci_reset(slot, SDHCI_RESET_ALL);
663 
664 	return (0);
665 }
666 
667 int
668 sdhci_generic_resume(struct sdhci_slot *slot)
669 {
670 	sdhci_init(slot);
671 
672 	return (0);
673 }
674 
675 uint32_t
676 sdhci_generic_min_freq(device_t brdev, struct sdhci_slot *slot)
677 {
678 	if (slot->version >= SDHCI_SPEC_300)
679 		return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
680 	else
681 		return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
682 }
683 
684 int
685 sdhci_generic_update_ios(device_t brdev, device_t reqdev)
686 {
687 	struct sdhci_slot *slot = device_get_ivars(reqdev);
688 	struct mmc_ios *ios = &slot->host.ios;
689 
690 	SDHCI_LOCK(slot);
691 	/* Do full reset on bus power down to clear from any state. */
692 	if (ios->power_mode == power_off) {
693 		WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
694 		sdhci_init(slot);
695 	}
696 	/* Configure the bus. */
697 	sdhci_set_clock(slot, ios->clock);
698 	sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
699 	if (ios->bus_width == bus_width_8) {
700 		slot->hostctrl |= SDHCI_CTRL_8BITBUS;
701 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
702 	} else if (ios->bus_width == bus_width_4) {
703 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
704 		slot->hostctrl |= SDHCI_CTRL_4BITBUS;
705 	} else if (ios->bus_width == bus_width_1) {
706 		slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
707 		slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
708 	} else {
709 		panic("Invalid bus width: %d", ios->bus_width);
710 	}
711 	if (ios->timing == bus_timing_hs &&
712 	    !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
713 		slot->hostctrl |= SDHCI_CTRL_HISPD;
714 	else
715 		slot->hostctrl &= ~SDHCI_CTRL_HISPD;
716 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
717 	/* Some controllers like reset after bus changes. */
718 	if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
719 		sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
720 
721 	SDHCI_UNLOCK(slot);
722 	return (0);
723 }
724 
725 static void
726 sdhci_req_done(struct sdhci_slot *slot)
727 {
728 	struct mmc_request *req;
729 
730 	if (slot->req != NULL && slot->curcmd != NULL) {
731 		callout_stop(&slot->timeout_callout);
732 		req = slot->req;
733 		slot->req = NULL;
734 		slot->curcmd = NULL;
735 		req->done(req);
736 	}
737 }
738 
739 static void
740 sdhci_timeout(void *arg)
741 {
742 	struct sdhci_slot *slot = arg;
743 
744 	if (slot->curcmd != NULL) {
745 		slot_printf(slot, " Controller timeout\n");
746 		sdhci_dumpregs(slot);
747 		sdhci_reset(slot, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
748 		slot->curcmd->error = MMC_ERR_TIMEOUT;
749 		sdhci_req_done(slot);
750 	} else {
751 		slot_printf(slot, " Spurious timeout - no active command\n");
752 	}
753 }
754 
755 static void
756 sdhci_set_transfer_mode(struct sdhci_slot *slot,
757 	struct mmc_data *data)
758 {
759 	uint16_t mode;
760 
761 	if (data == NULL)
762 		return;
763 
764 	mode = SDHCI_TRNS_BLK_CNT_EN;
765 	if (data->len > 512)
766 		mode |= SDHCI_TRNS_MULTI;
767 	if (data->flags & MMC_DATA_READ)
768 		mode |= SDHCI_TRNS_READ;
769 	if (slot->req->stop)
770 		mode |= SDHCI_TRNS_ACMD12;
771 	if (slot->flags & SDHCI_USE_DMA)
772 		mode |= SDHCI_TRNS_DMA;
773 
774 	WR2(slot, SDHCI_TRANSFER_MODE, mode);
775 }
776 
777 static void
778 sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
779 {
780 	int flags, timeout;
781 	uint32_t mask, state;
782 
783 	slot->curcmd = cmd;
784 	slot->cmd_done = 0;
785 
786 	cmd->error = MMC_ERR_NONE;
787 
788 	/* This flags combination is not supported by controller. */
789 	if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
790 		slot_printf(slot, "Unsupported response type!\n");
791 		cmd->error = MMC_ERR_FAILED;
792 		sdhci_req_done(slot);
793 		return;
794 	}
795 
796 	/* Read controller present state. */
797 	state = RD4(slot, SDHCI_PRESENT_STATE);
798 	/* Do not issue command if there is no card, clock or power.
799 	 * Controller will not detect timeout without clock active. */
800 	if ((state & SDHCI_CARD_PRESENT) == 0 ||
801 	    slot->power == 0 ||
802 	    slot->clock == 0) {
803 		cmd->error = MMC_ERR_FAILED;
804 		sdhci_req_done(slot);
805 		return;
806 	}
807 	/* Always wait for free CMD bus. */
808 	mask = SDHCI_CMD_INHIBIT;
809 	/* Wait for free DAT if we have data or busy signal. */
810 	if (cmd->data || (cmd->flags & MMC_RSP_BUSY))
811 		mask |= SDHCI_DAT_INHIBIT;
812 	/* We shouldn't wait for DAT for stop commands. */
813 	if (cmd == slot->req->stop)
814 		mask &= ~SDHCI_DAT_INHIBIT;
815 	/*
816 	 *  Wait for bus no more then 250 ms.  Typically there will be no wait
817 	 *  here at all, but when writing a crash dump we may be bypassing the
818 	 *  host platform's interrupt handler, and in some cases that handler
819 	 *  may be working around hardware quirks such as not respecting r1b
820 	 *  busy indications.  In those cases, this wait-loop serves the purpose
821 	 *  of waiting for the prior command and data transfers to be done, and
822 	 *  SD cards are allowed to take up to 250ms for write and erase ops.
823 	 *  (It's usually more like 20-30ms in the real world.)
824 	 */
825 	timeout = 250;
826 	while (state & mask) {
827 		if (timeout == 0) {
828 			slot_printf(slot, "Controller never released "
829 			    "inhibit bit(s).\n");
830 			sdhci_dumpregs(slot);
831 			cmd->error = MMC_ERR_FAILED;
832 			sdhci_req_done(slot);
833 			return;
834 		}
835 		timeout--;
836 		DELAY(1000);
837 		state = RD4(slot, SDHCI_PRESENT_STATE);
838 	}
839 
840 	/* Prepare command flags. */
841 	if (!(cmd->flags & MMC_RSP_PRESENT))
842 		flags = SDHCI_CMD_RESP_NONE;
843 	else if (cmd->flags & MMC_RSP_136)
844 		flags = SDHCI_CMD_RESP_LONG;
845 	else if (cmd->flags & MMC_RSP_BUSY)
846 		flags = SDHCI_CMD_RESP_SHORT_BUSY;
847 	else
848 		flags = SDHCI_CMD_RESP_SHORT;
849 	if (cmd->flags & MMC_RSP_CRC)
850 		flags |= SDHCI_CMD_CRC;
851 	if (cmd->flags & MMC_RSP_OPCODE)
852 		flags |= SDHCI_CMD_INDEX;
853 	if (cmd->data)
854 		flags |= SDHCI_CMD_DATA;
855 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
856 		flags |= SDHCI_CMD_TYPE_ABORT;
857 	/* Prepare data. */
858 	sdhci_start_data(slot, cmd->data);
859 	/*
860 	 * Interrupt aggregation: To reduce total number of interrupts
861 	 * group response interrupt with data interrupt when possible.
862 	 * If there going to be data interrupt, mask response one.
863 	 */
864 	if (slot->data_done == 0) {
865 		WR4(slot, SDHCI_SIGNAL_ENABLE,
866 		    slot->intmask &= ~SDHCI_INT_RESPONSE);
867 	}
868 	/* Set command argument. */
869 	WR4(slot, SDHCI_ARGUMENT, cmd->arg);
870 	/* Set data transfer mode. */
871 	sdhci_set_transfer_mode(slot, cmd->data);
872 	/* Start command. */
873 	WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
874 	/* Start timeout callout. */
875 	callout_reset(&slot->timeout_callout, 2*hz, sdhci_timeout, slot);
876 }
877 
878 static void
879 sdhci_finish_command(struct sdhci_slot *slot)
880 {
881 	int i;
882 
883 	slot->cmd_done = 1;
884 	/* Interrupt aggregation: Restore command interrupt.
885 	 * Main restore point for the case when command interrupt
886 	 * happened first. */
887 	WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE);
888 	/* In case of error - reset host and return. */
889 	if (slot->curcmd->error) {
890 		sdhci_reset(slot, SDHCI_RESET_CMD);
891 		sdhci_reset(slot, SDHCI_RESET_DATA);
892 		sdhci_start(slot);
893 		return;
894 	}
895 	/* If command has response - fetch it. */
896 	if (slot->curcmd->flags & MMC_RSP_PRESENT) {
897 		if (slot->curcmd->flags & MMC_RSP_136) {
898 			/* CRC is stripped so we need one byte shift. */
899 			uint8_t extra = 0;
900 			for (i = 0; i < 4; i++) {
901 				uint32_t val = RD4(slot, SDHCI_RESPONSE + i * 4);
902 				if (slot->quirks & SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
903 					slot->curcmd->resp[3 - i] = val;
904 				else {
905 					slot->curcmd->resp[3 - i] =
906 					    (val << 8) | extra;
907 					extra = val >> 24;
908 				}
909 			}
910 		} else
911 			slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
912 	}
913 	/* If data ready - finish. */
914 	if (slot->data_done)
915 		sdhci_start(slot);
916 }
917 
918 static void
919 sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data)
920 {
921 	uint32_t target_timeout, current_timeout;
922 	uint8_t div;
923 
924 	if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
925 		slot->data_done = 1;
926 		return;
927 	}
928 
929 	slot->data_done = 0;
930 
931 	/* Calculate and set data timeout.*/
932 	/* XXX: We should have this from mmc layer, now assume 1 sec. */
933 	if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
934 		div = 0xE;
935 	} else {
936 		target_timeout = 1000000;
937 		div = 0;
938 		current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
939 		while (current_timeout < target_timeout && div < 0xE) {
940 			++div;
941 			current_timeout <<= 1;
942 		}
943 		/* Compensate for an off-by-one error in the CaFe chip.*/
944 		if (div < 0xE &&
945 		    (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
946 			++div;
947 		}
948 	}
949 	WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
950 
951 	if (data == NULL)
952 		return;
953 
954 	/* Use DMA if possible. */
955 	if ((slot->opt & SDHCI_HAVE_DMA))
956 		slot->flags |= SDHCI_USE_DMA;
957 	/* If data is small, broken DMA may return zeroes instead of data, */
958 	if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
959 	    (data->len <= 512))
960 		slot->flags &= ~SDHCI_USE_DMA;
961 	/* Some controllers require even block sizes. */
962 	if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
963 	    ((data->len) & 0x3))
964 		slot->flags &= ~SDHCI_USE_DMA;
965 	/* Load DMA buffer. */
966 	if (slot->flags & SDHCI_USE_DMA) {
967 		if (data->flags & MMC_DATA_READ)
968 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
969 			    BUS_DMASYNC_PREREAD);
970 		else {
971 			memcpy(slot->dmamem, data->data,
972 			    (data->len < DMA_BLOCK_SIZE) ?
973 			    data->len : DMA_BLOCK_SIZE);
974 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
975 			    BUS_DMASYNC_PREWRITE);
976 		}
977 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
978 		/* Interrupt aggregation: Mask border interrupt
979 		 * for the last page and unmask else. */
980 		if (data->len == DMA_BLOCK_SIZE)
981 			slot->intmask &= ~SDHCI_INT_DMA_END;
982 		else
983 			slot->intmask |= SDHCI_INT_DMA_END;
984 		WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
985 	}
986 	/* Current data offset for both PIO and DMA. */
987 	slot->offset = 0;
988 	/* Set block size and request IRQ on 4K border. */
989 	WR2(slot, SDHCI_BLOCK_SIZE,
990 	    SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512)?data->len:512));
991 	/* Set block count. */
992 	WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512);
993 }
994 
995 void
996 sdhci_finish_data(struct sdhci_slot *slot)
997 {
998 	struct mmc_data *data = slot->curcmd->data;
999 
1000 	/* Interrupt aggregation: Restore command interrupt.
1001 	 * Auxiliary restore point for the case when data interrupt
1002 	 * happened first. */
1003 	if (!slot->cmd_done) {
1004 		WR4(slot, SDHCI_SIGNAL_ENABLE,
1005 		    slot->intmask |= SDHCI_INT_RESPONSE);
1006 	}
1007 	/* Unload rest of data from DMA buffer. */
1008 	if (!slot->data_done && (slot->flags & SDHCI_USE_DMA)) {
1009 		if (data->flags & MMC_DATA_READ) {
1010 			size_t left = data->len - slot->offset;
1011 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1012 			    BUS_DMASYNC_POSTREAD);
1013 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1014 			    (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
1015 		} else
1016 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1017 			    BUS_DMASYNC_POSTWRITE);
1018 	}
1019 	slot->data_done = 1;
1020 	/* If there was error - reset the host. */
1021 	if (slot->curcmd->error) {
1022 		sdhci_reset(slot, SDHCI_RESET_CMD);
1023 		sdhci_reset(slot, SDHCI_RESET_DATA);
1024 		sdhci_start(slot);
1025 		return;
1026 	}
1027 	/* If we already have command response - finish. */
1028 	if (slot->cmd_done)
1029 		sdhci_start(slot);
1030 }
1031 
1032 static void
1033 sdhci_start(struct sdhci_slot *slot)
1034 {
1035 	struct mmc_request *req;
1036 
1037 	req = slot->req;
1038 	if (req == NULL)
1039 		return;
1040 
1041 	if (!(slot->flags & CMD_STARTED)) {
1042 		slot->flags |= CMD_STARTED;
1043 		sdhci_start_command(slot, req->cmd);
1044 		return;
1045 	}
1046 /* 	We don't need this until using Auto-CMD12 feature
1047 	if (!(slot->flags & STOP_STARTED) && req->stop) {
1048 		slot->flags |= STOP_STARTED;
1049 		sdhci_start_command(slot, req->stop);
1050 		return;
1051 	}
1052 */
1053 	if (sdhci_debug > 1)
1054 		slot_printf(slot, "result: %d\n", req->cmd->error);
1055 	if (!req->cmd->error &&
1056 	    (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
1057 		sdhci_reset(slot, SDHCI_RESET_CMD);
1058 		sdhci_reset(slot, SDHCI_RESET_DATA);
1059 	}
1060 
1061 	sdhci_req_done(slot);
1062 }
1063 
1064 int
1065 sdhci_generic_request(device_t brdev, device_t reqdev, struct mmc_request *req)
1066 {
1067 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1068 
1069 	SDHCI_LOCK(slot);
1070 	if (slot->req != NULL) {
1071 		SDHCI_UNLOCK(slot);
1072 		return (EBUSY);
1073 	}
1074 	if (sdhci_debug > 1) {
1075 		slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
1076     		    req->cmd->opcode, req->cmd->arg, req->cmd->flags,
1077     		    (req->cmd->data)?(u_int)req->cmd->data->len:0,
1078 		    (req->cmd->data)?req->cmd->data->flags:0);
1079 	}
1080 	slot->req = req;
1081 	slot->flags = 0;
1082 	sdhci_start(slot);
1083 	SDHCI_UNLOCK(slot);
1084 	if (dumping) {
1085 		while (slot->req != NULL) {
1086 			sdhci_generic_intr(slot);
1087 			DELAY(10);
1088 		}
1089 	}
1090 	return (0);
1091 }
1092 
1093 int
1094 sdhci_generic_get_ro(device_t brdev, device_t reqdev)
1095 {
1096 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1097 	uint32_t val;
1098 
1099 	SDHCI_LOCK(slot);
1100 	val = RD4(slot, SDHCI_PRESENT_STATE);
1101 	SDHCI_UNLOCK(slot);
1102 	return (!(val & SDHCI_WRITE_PROTECT));
1103 }
1104 
1105 int
1106 sdhci_generic_acquire_host(device_t brdev, device_t reqdev)
1107 {
1108 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1109 	int err = 0;
1110 
1111 	SDHCI_LOCK(slot);
1112 	while (slot->bus_busy)
1113 		msleep(slot, &slot->mtx, 0, "sdhciah", 0);
1114 	slot->bus_busy++;
1115 	/* Activate led. */
1116 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
1117 	SDHCI_UNLOCK(slot);
1118 	return (err);
1119 }
1120 
1121 int
1122 sdhci_generic_release_host(device_t brdev, device_t reqdev)
1123 {
1124 	struct sdhci_slot *slot = device_get_ivars(reqdev);
1125 
1126 	SDHCI_LOCK(slot);
1127 	/* Deactivate led. */
1128 	WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
1129 	slot->bus_busy--;
1130 	SDHCI_UNLOCK(slot);
1131 	wakeup(slot);
1132 	return (0);
1133 }
1134 
1135 static void
1136 sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
1137 {
1138 
1139 	if (!slot->curcmd) {
1140 		slot_printf(slot, "Got command interrupt 0x%08x, but "
1141 		    "there is no active command.\n", intmask);
1142 		sdhci_dumpregs(slot);
1143 		return;
1144 	}
1145 	if (intmask & SDHCI_INT_TIMEOUT)
1146 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1147 	else if (intmask & SDHCI_INT_CRC)
1148 		slot->curcmd->error = MMC_ERR_BADCRC;
1149 	else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
1150 		slot->curcmd->error = MMC_ERR_FIFO;
1151 
1152 	sdhci_finish_command(slot);
1153 }
1154 
1155 static void
1156 sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
1157 {
1158 
1159 	if (!slot->curcmd) {
1160 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1161 		    "there is no active command.\n", intmask);
1162 		sdhci_dumpregs(slot);
1163 		return;
1164 	}
1165 	if (slot->curcmd->data == NULL &&
1166 	    (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1167 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1168 		    "there is no active data operation.\n",
1169 		    intmask);
1170 		sdhci_dumpregs(slot);
1171 		return;
1172 	}
1173 	if (intmask & SDHCI_INT_DATA_TIMEOUT)
1174 		slot->curcmd->error = MMC_ERR_TIMEOUT;
1175 	else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1176 		slot->curcmd->error = MMC_ERR_BADCRC;
1177 	if (slot->curcmd->data == NULL &&
1178 	    (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
1179 	    SDHCI_INT_DMA_END))) {
1180 		slot_printf(slot, "Got data interrupt 0x%08x, but "
1181 		    "there is busy-only command.\n", intmask);
1182 		sdhci_dumpregs(slot);
1183 		slot->curcmd->error = MMC_ERR_INVALID;
1184 	}
1185 	if (slot->curcmd->error) {
1186 		/* No need to continue after any error. */
1187 		goto done;
1188 	}
1189 
1190 	/* Handle PIO interrupt. */
1191 	if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
1192 		if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
1193 		    SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
1194 			SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, &intmask);
1195 			slot->flags |= PLATFORM_DATA_STARTED;
1196 		} else
1197 			sdhci_transfer_pio(slot);
1198 	}
1199 	/* Handle DMA border. */
1200 	if (intmask & SDHCI_INT_DMA_END) {
1201 		struct mmc_data *data = slot->curcmd->data;
1202 		size_t left;
1203 
1204 		/* Unload DMA buffer... */
1205 		left = data->len - slot->offset;
1206 		if (data->flags & MMC_DATA_READ) {
1207 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1208 			    BUS_DMASYNC_POSTREAD);
1209 			memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1210 			    (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
1211 		} else {
1212 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1213 			    BUS_DMASYNC_POSTWRITE);
1214 		}
1215 		/* ... and reload it again. */
1216 		slot->offset += DMA_BLOCK_SIZE;
1217 		left = data->len - slot->offset;
1218 		if (data->flags & MMC_DATA_READ) {
1219 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1220 			    BUS_DMASYNC_PREREAD);
1221 		} else {
1222 			memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
1223 			    (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE);
1224 			bus_dmamap_sync(slot->dmatag, slot->dmamap,
1225 			    BUS_DMASYNC_PREWRITE);
1226 		}
1227 		/* Interrupt aggregation: Mask border interrupt
1228 		 * for the last page. */
1229 		if (left == DMA_BLOCK_SIZE) {
1230 			slot->intmask &= ~SDHCI_INT_DMA_END;
1231 			WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1232 		}
1233 		/* Restart DMA. */
1234 		WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1235 	}
1236 	/* We have got all data. */
1237 	if (intmask & SDHCI_INT_DATA_END) {
1238 		if (slot->flags & PLATFORM_DATA_STARTED) {
1239 			slot->flags &= ~PLATFORM_DATA_STARTED;
1240 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1241 		} else
1242 			sdhci_finish_data(slot);
1243 	}
1244 done:
1245 	if (slot->curcmd != NULL && slot->curcmd->error != 0) {
1246 		if (slot->flags & PLATFORM_DATA_STARTED) {
1247 			slot->flags &= ~PLATFORM_DATA_STARTED;
1248 			SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1249 		} else
1250 			sdhci_finish_data(slot);
1251 		return;
1252 	}
1253 }
1254 
1255 static void
1256 sdhci_acmd_irq(struct sdhci_slot *slot)
1257 {
1258 	uint16_t err;
1259 
1260 	err = RD4(slot, SDHCI_ACMD12_ERR);
1261 	if (!slot->curcmd) {
1262 		slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
1263 		    "there is no active command.\n", err);
1264 		sdhci_dumpregs(slot);
1265 		return;
1266 	}
1267 	slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err);
1268 	sdhci_reset(slot, SDHCI_RESET_CMD);
1269 }
1270 
1271 void
1272 sdhci_generic_intr(struct sdhci_slot *slot)
1273 {
1274 	uint32_t intmask;
1275 
1276 	SDHCI_LOCK(slot);
1277 	/* Read slot interrupt status. */
1278 	intmask = RD4(slot, SDHCI_INT_STATUS);
1279 	if (intmask == 0 || intmask == 0xffffffff) {
1280 		SDHCI_UNLOCK(slot);
1281 		return;
1282 	}
1283 	if (sdhci_debug > 2)
1284 		slot_printf(slot, "Interrupt %#x\n", intmask);
1285 
1286 	/* Handle card presence interrupts. */
1287 	if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1288 		WR4(slot, SDHCI_INT_STATUS, intmask &
1289 		    (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
1290 
1291 		if (intmask & SDHCI_INT_CARD_REMOVE) {
1292 			if (bootverbose || sdhci_debug)
1293 				slot_printf(slot, "Card removed\n");
1294 			callout_stop(&slot->card_callout);
1295 			taskqueue_enqueue(taskqueue_swi_giant,
1296 			    &slot->card_task);
1297 		}
1298 		if (intmask & SDHCI_INT_CARD_INSERT) {
1299 			if (bootverbose || sdhci_debug)
1300 				slot_printf(slot, "Card inserted\n");
1301 			callout_reset(&slot->card_callout, hz / 2,
1302 			    sdhci_card_delay, slot);
1303 		}
1304 		intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1305 	}
1306 	/* Handle command interrupts. */
1307 	if (intmask & SDHCI_INT_CMD_MASK) {
1308 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
1309 		sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
1310 	}
1311 	/* Handle data interrupts. */
1312 	if (intmask & SDHCI_INT_DATA_MASK) {
1313 		WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
1314 		/* Dont call data_irq in case of errored command */
1315 		if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
1316 			sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
1317 	}
1318 	/* Handle AutoCMD12 error interrupt. */
1319 	if (intmask & SDHCI_INT_ACMD12ERR) {
1320 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
1321 		sdhci_acmd_irq(slot);
1322 	}
1323 	intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1324 	intmask &= ~SDHCI_INT_ACMD12ERR;
1325 	intmask &= ~SDHCI_INT_ERROR;
1326 	/* Handle bus power interrupt. */
1327 	if (intmask & SDHCI_INT_BUS_POWER) {
1328 		WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
1329 		slot_printf(slot,
1330 		    "Card is consuming too much power!\n");
1331 		intmask &= ~SDHCI_INT_BUS_POWER;
1332 	}
1333 	/* The rest is unknown. */
1334 	if (intmask) {
1335 		WR4(slot, SDHCI_INT_STATUS, intmask);
1336 		slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
1337 		    intmask);
1338 		sdhci_dumpregs(slot);
1339 	}
1340 
1341 	SDHCI_UNLOCK(slot);
1342 }
1343 
1344 int
1345 sdhci_generic_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1346 {
1347 	struct sdhci_slot *slot = device_get_ivars(child);
1348 
1349 	switch (which) {
1350 	default:
1351 		return (EINVAL);
1352 	case MMCBR_IVAR_BUS_MODE:
1353 		*result = slot->host.ios.bus_mode;
1354 		break;
1355 	case MMCBR_IVAR_BUS_WIDTH:
1356 		*result = slot->host.ios.bus_width;
1357 		break;
1358 	case MMCBR_IVAR_CHIP_SELECT:
1359 		*result = slot->host.ios.chip_select;
1360 		break;
1361 	case MMCBR_IVAR_CLOCK:
1362 		*result = slot->host.ios.clock;
1363 		break;
1364 	case MMCBR_IVAR_F_MIN:
1365 		*result = slot->host.f_min;
1366 		break;
1367 	case MMCBR_IVAR_F_MAX:
1368 		*result = slot->host.f_max;
1369 		break;
1370 	case MMCBR_IVAR_HOST_OCR:
1371 		*result = slot->host.host_ocr;
1372 		break;
1373 	case MMCBR_IVAR_MODE:
1374 		*result = slot->host.mode;
1375 		break;
1376 	case MMCBR_IVAR_OCR:
1377 		*result = slot->host.ocr;
1378 		break;
1379 	case MMCBR_IVAR_POWER_MODE:
1380 		*result = slot->host.ios.power_mode;
1381 		break;
1382 	case MMCBR_IVAR_VDD:
1383 		*result = slot->host.ios.vdd;
1384 		break;
1385 	case MMCBR_IVAR_CAPS:
1386 		*result = slot->host.caps;
1387 		break;
1388 	case MMCBR_IVAR_TIMING:
1389 		*result = slot->host.ios.timing;
1390 		break;
1391 	case MMCBR_IVAR_MAX_DATA:
1392 		*result = 65535;
1393 		break;
1394 	}
1395 	return (0);
1396 }
1397 
1398 int
1399 sdhci_generic_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1400 {
1401 	struct sdhci_slot *slot = device_get_ivars(child);
1402 
1403 	switch (which) {
1404 	default:
1405 		return (EINVAL);
1406 	case MMCBR_IVAR_BUS_MODE:
1407 		slot->host.ios.bus_mode = value;
1408 		break;
1409 	case MMCBR_IVAR_BUS_WIDTH:
1410 		slot->host.ios.bus_width = value;
1411 		break;
1412 	case MMCBR_IVAR_CHIP_SELECT:
1413 		slot->host.ios.chip_select = value;
1414 		break;
1415 	case MMCBR_IVAR_CLOCK:
1416 		if (value > 0) {
1417 			uint32_t max_clock;
1418 			uint32_t clock;
1419 			int i;
1420 
1421 			max_clock = slot->max_clk;
1422 			clock = max_clock;
1423 
1424 			if (slot->version < SDHCI_SPEC_300) {
1425 				for (i = 0; i < SDHCI_200_MAX_DIVIDER;
1426 				    i <<= 1) {
1427 					if (clock <= value)
1428 						break;
1429 					clock >>= 1;
1430 				}
1431 			}
1432 			else {
1433 				for (i = 0; i < SDHCI_300_MAX_DIVIDER;
1434 				    i += 2) {
1435 					if (clock <= value)
1436 						break;
1437 					clock = max_clock / (i + 2);
1438 				}
1439 			}
1440 
1441 			slot->host.ios.clock = clock;
1442 		} else
1443 			slot->host.ios.clock = 0;
1444 		break;
1445 	case MMCBR_IVAR_MODE:
1446 		slot->host.mode = value;
1447 		break;
1448 	case MMCBR_IVAR_OCR:
1449 		slot->host.ocr = value;
1450 		break;
1451 	case MMCBR_IVAR_POWER_MODE:
1452 		slot->host.ios.power_mode = value;
1453 		break;
1454 	case MMCBR_IVAR_VDD:
1455 		slot->host.ios.vdd = value;
1456 		break;
1457 	case MMCBR_IVAR_TIMING:
1458 		slot->host.ios.timing = value;
1459 		break;
1460 	case MMCBR_IVAR_CAPS:
1461 	case MMCBR_IVAR_HOST_OCR:
1462 	case MMCBR_IVAR_F_MIN:
1463 	case MMCBR_IVAR_F_MAX:
1464 	case MMCBR_IVAR_MAX_DATA:
1465 		return (EINVAL);
1466 	}
1467 	return (0);
1468 }
1469 
1470 MODULE_VERSION(sdhci, 1);
1471