]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/sdhci/sdhci.c
MFC: r339007, r340543, r340654
[FreeBSD/stable/10.git] / sys / dev / sdhci / sdhci.c
1 /*-
2  * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
3  * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/callout.h>
34 #include <sys/conf.h>
35 #include <sys/kernel.h>
36 #include <sys/kobj.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/resource.h>
42 #include <sys/rman.h>
43 #include <sys/sysctl.h>
44 #include <sys/taskqueue.h>
45
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <machine/stdarg.h>
49
50 #include <dev/mmc/bridge.h>
51 #include <dev/mmc/mmcreg.h>
52 #include <dev/mmc/mmcbrvar.h>
53
54 #include <dev/sdhci/sdhci.h>
55
56 #include "mmcbr_if.h"
57 #include "sdhci_if.h"
58
59 SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver");
60
61 static int sdhci_debug;
62 TUNABLE_INT("hw.sdhci.debug", &sdhci_debug);
63 SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0,
64     "Debug level");
65 u_int sdhci_quirk_clear = 0;
66 SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear,
67     0, "Mask of quirks to clear");
68 u_int sdhci_quirk_set = 0;
69 SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0,
70     "Mask of quirks to set");
71
72 #define RD1(slot, off)  SDHCI_READ_1((slot)->bus, (slot), (off))
73 #define RD2(slot, off)  SDHCI_READ_2((slot)->bus, (slot), (off))
74 #define RD4(slot, off)  SDHCI_READ_4((slot)->bus, (slot), (off))
75 #define RD_MULTI_4(slot, off, ptr, count)       \
76     SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
77
78 #define WR1(slot, off, val)     SDHCI_WRITE_1((slot)->bus, (slot), (off), (val))
79 #define WR2(slot, off, val)     SDHCI_WRITE_2((slot)->bus, (slot), (off), (val))
80 #define WR4(slot, off, val)     SDHCI_WRITE_4((slot)->bus, (slot), (off), (val))
81 #define WR_MULTI_4(slot, off, ptr, count)       \
82     SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count))
83
84 static void sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err);
85 static void sdhci_card_poll(void *arg);
86 static void sdhci_card_task(void *arg, int pending);
87 static void sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask);
88 static void sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask);
89 static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset);
90 static void sdhci_handle_card_present_locked(struct sdhci_slot *slot,
91     bool is_present);
92 static void sdhci_finish_command(struct sdhci_slot *slot);
93 static void sdhci_init(struct sdhci_slot *slot);
94 static void sdhci_read_block_pio(struct sdhci_slot *slot);
95 static void sdhci_req_done(struct sdhci_slot *slot);
96 static void sdhci_req_wakeup(struct mmc_request *req);
97 static void sdhci_reset(struct sdhci_slot *slot, uint8_t mask);
98 static void sdhci_retune(void *arg);
99 static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock);
100 static void sdhci_set_power(struct sdhci_slot *slot, u_char power);
101 static void sdhci_set_transfer_mode(struct sdhci_slot *slot,
102    struct mmc_data *data);
103 static void sdhci_start(struct sdhci_slot *slot);
104 static void sdhci_timeout(void *arg);
105 static void sdhci_start_command(struct sdhci_slot *slot,
106    struct mmc_command *cmd);
107 static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data);
108 static void sdhci_write_block_pio(struct sdhci_slot *slot);
109 static void sdhci_transfer_pio(struct sdhci_slot *slot);
110
111 /* helper routines */
112 static void sdhci_dumpregs(struct sdhci_slot *slot);
113 static void sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs,
114     int error);
115 static int slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
116     __printflike(2, 3);
117 static uint32_t sdhci_tuning_intmask(struct sdhci_slot *slot);
118
119 #define SDHCI_LOCK(_slot)               mtx_lock(&(_slot)->mtx)
120 #define SDHCI_UNLOCK(_slot)             mtx_unlock(&(_slot)->mtx)
121 #define SDHCI_LOCK_INIT(_slot) \
122         mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF)
123 #define SDHCI_LOCK_DESTROY(_slot)       mtx_destroy(&_slot->mtx);
124 #define SDHCI_ASSERT_LOCKED(_slot)      mtx_assert(&_slot->mtx, MA_OWNED);
125 #define SDHCI_ASSERT_UNLOCKED(_slot)    mtx_assert(&_slot->mtx, MA_NOTOWNED);
126
127 #define SDHCI_DEFAULT_MAX_FREQ  50
128
129 #define SDHCI_200_MAX_DIVIDER   256
130 #define SDHCI_300_MAX_DIVIDER   2046
131
132 #define SDHCI_CARD_PRESENT_TICKS        (hz / 5)
133 #define SDHCI_INSERT_DELAY_TICKS        (hz / 2)
134
135 /*
136  * Broadcom BCM577xx Controller Constants
137  */
138 /* Maximum divider supported by the default clock source. */
139 #define BCM577XX_DEFAULT_MAX_DIVIDER    256
140 /* Alternative clock's base frequency. */
141 #define BCM577XX_ALT_CLOCK_BASE         63000000
142
143 #define BCM577XX_HOST_CONTROL           0x198
144 #define BCM577XX_CTRL_CLKSEL_MASK       0xFFFFCFFF
145 #define BCM577XX_CTRL_CLKSEL_SHIFT      12
146 #define BCM577XX_CTRL_CLKSEL_DEFAULT    0x0
147 #define BCM577XX_CTRL_CLKSEL_64MHZ      0x3
148
149 static void
150 sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
151 {
152
153         if (error != 0) {
154                 printf("getaddr: error %d\n", error);
155                 return;
156         }
157         *(bus_addr_t *)arg = segs[0].ds_addr;
158 }
159
160 static int
161 slot_printf(struct sdhci_slot *slot, const char * fmt, ...)
162 {
163         va_list ap;
164         int retval;
165
166         retval = printf("%s-slot%d: ",
167             device_get_nameunit(slot->bus), slot->num);
168
169         va_start(ap, fmt);
170         retval += vprintf(fmt, ap);
171         va_end(ap);
172         return (retval);
173 }
174
175 static void
176 sdhci_dumpregs(struct sdhci_slot *slot)
177 {
178
179         slot_printf(slot,
180             "============== REGISTER DUMP ==============\n");
181
182         slot_printf(slot, "Sys addr: 0x%08x | Version:  0x%08x\n",
183             RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION));
184         slot_printf(slot, "Blk size: 0x%08x | Blk cnt:  0x%08x\n",
185             RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT));
186         slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n",
187             RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE));
188         slot_printf(slot, "Present:  0x%08x | Host ctl: 0x%08x\n",
189             RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL));
190         slot_printf(slot, "Power:    0x%08x | Blk gap:  0x%08x\n",
191             RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL));
192         slot_printf(slot, "Wake-up:  0x%08x | Clock:    0x%08x\n",
193             RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL));
194         slot_printf(slot, "Timeout:  0x%08x | Int stat: 0x%08x\n",
195             RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS));
196         slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n",
197             RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE));
198         slot_printf(slot, "AC12 err: 0x%08x | Host ctl2:0x%08x\n",
199             RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2));
200         slot_printf(slot, "Caps:     0x%08x | Caps2:    0x%08x\n",
201             RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2));
202         slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n",
203             RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR));
204         slot_printf(slot, "ADMA addr:0x%08x | Slot int: 0x%08x\n",
205             RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS));
206
207         slot_printf(slot,
208             "===========================================\n");
209 }
210
211 static void
212 sdhci_reset(struct sdhci_slot *slot, uint8_t mask)
213 {
214         int timeout;
215         uint32_t clock;
216
217         if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
218                 if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot))
219                         return;
220         }
221
222         /* Some controllers need this kick or reset won't work. */
223         if ((mask & SDHCI_RESET_ALL) == 0 &&
224             (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) {
225                 /* This is to force an update */
226                 clock = slot->clock;
227                 slot->clock = 0;
228                 sdhci_set_clock(slot, clock);
229         }
230
231         if (mask & SDHCI_RESET_ALL) {
232                 slot->clock = 0;
233                 slot->power = 0;
234         }
235
236         WR1(slot, SDHCI_SOFTWARE_RESET, mask);
237
238         if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) {
239                 /*
240                  * Resets on TI OMAPs and AM335x are incompatible with SDHCI
241                  * specification.  The reset bit has internal propagation delay,
242                  * so a fast read after write returns 0 even if reset process is
243                  * in progress.  The workaround is to poll for 1 before polling
244                  * for 0.  In the worst case, if we miss seeing it asserted the
245                  * time we spent waiting is enough to ensure the reset finishes.
246                  */
247                 timeout = 10000;
248                 while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) {
249                         if (timeout <= 0)
250                                 break;
251                         timeout--;
252                         DELAY(1);
253                 }
254         }
255
256         /* Wait max 100 ms */
257         timeout = 10000;
258         /* Controller clears the bits when it's done */
259         while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) {
260                 if (timeout <= 0) {
261                         slot_printf(slot, "Reset 0x%x never completed.\n",
262                             mask);
263                         sdhci_dumpregs(slot);
264                         return;
265                 }
266                 timeout--;
267                 DELAY(10);
268         }
269 }
270
271 static uint32_t
272 sdhci_tuning_intmask(struct sdhci_slot *slot)
273 {
274         uint32_t intmask;
275
276         intmask = 0;
277         if (slot->opt & SDHCI_TUNING_ENABLED) {
278                 intmask |= SDHCI_INT_TUNEERR;
279                 if (slot->retune_mode == SDHCI_RETUNE_MODE_2 ||
280                     slot->retune_mode == SDHCI_RETUNE_MODE_3)
281                         intmask |= SDHCI_INT_RETUNE;
282         }
283         return (intmask);
284 }
285
286 static void
287 sdhci_init(struct sdhci_slot *slot)
288 {
289
290         sdhci_reset(slot, SDHCI_RESET_ALL);
291
292         /* Enable interrupts. */
293         slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
294             SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
295             SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
296             SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
297             SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
298             SDHCI_INT_ACMD12ERR;
299
300         if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
301             !(slot->opt & SDHCI_NON_REMOVABLE)) {
302                 slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
303         }
304
305         WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
306         WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
307 }
308
309 static void
310 sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock)
311 {
312         uint32_t clk_base;
313         uint32_t clk_sel;
314         uint32_t res;
315         uint16_t clk;
316         uint16_t div;
317         int timeout;
318
319         if (clock == slot->clock)
320                 return;
321         slot->clock = clock;
322
323         /* Turn off the clock. */
324         clk = RD2(slot, SDHCI_CLOCK_CONTROL);
325         WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN);
326         /* If no clock requested - leave it so. */
327         if (clock == 0)
328                 return;
329
330         /* Determine the clock base frequency */
331         clk_base = slot->max_clk;
332         if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) {
333                 clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) &
334                     BCM577XX_CTRL_CLKSEL_MASK;
335
336                 /*
337                  * Select clock source appropriate for the requested frequency.
338                  */
339                 if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) {
340                         clk_base = BCM577XX_ALT_CLOCK_BASE;
341                         clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ <<
342                             BCM577XX_CTRL_CLKSEL_SHIFT);
343                 } else {
344                         clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT <<
345                             BCM577XX_CTRL_CLKSEL_SHIFT);
346                 }
347
348                 WR2(slot, BCM577XX_HOST_CONTROL, clk_sel);
349         }
350
351         /* Recalculate timeout clock frequency based on the new sd clock. */
352         if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
353                 slot->timeout_clk = slot->clock / 1000;
354
355         if (slot->version < SDHCI_SPEC_300) {
356                 /* Looking for highest freq <= clock. */
357                 res = clk_base;
358                 for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) {
359                         if (res <= clock)
360                                 break;
361                         res >>= 1;
362                 }
363                 /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */
364                 div >>= 1;
365         } else {
366                 /* Version 3.0 divisors are multiples of two up to 1023 * 2 */
367                 if (clock >= clk_base)
368                         div = 0;
369                 else {
370                         for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) {
371                                 if ((clk_base / div) <= clock)
372                                         break;
373                         }
374                 }
375                 div >>= 1;
376         }
377
378         if (bootverbose || sdhci_debug)
379                 slot_printf(slot, "Divider %d for freq %d (base %d)\n",
380                         div, clock, clk_base);
381
382         /* Now we have got divider, set it. */
383         clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT;
384         clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK)
385                 << SDHCI_DIVIDER_HI_SHIFT;
386
387         WR2(slot, SDHCI_CLOCK_CONTROL, clk);
388         /* Enable clock. */
389         clk |= SDHCI_CLOCK_INT_EN;
390         WR2(slot, SDHCI_CLOCK_CONTROL, clk);
391         /* Wait up to 10 ms until it stabilize. */
392         timeout = 10;
393         while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL))
394                 & SDHCI_CLOCK_INT_STABLE)) {
395                 if (timeout == 0) {
396                         slot_printf(slot,
397                             "Internal clock never stabilised.\n");
398                         sdhci_dumpregs(slot);
399                         return;
400                 }
401                 timeout--;
402                 DELAY(1000);
403         }
404         /* Pass clock signal to the bus. */
405         clk |= SDHCI_CLOCK_CARD_EN;
406         WR2(slot, SDHCI_CLOCK_CONTROL, clk);
407 }
408
409 static void
410 sdhci_set_power(struct sdhci_slot *slot, u_char power)
411 {
412         int i;
413         uint8_t pwr;
414
415         if (slot->power == power)
416                 return;
417
418         slot->power = power;
419
420         /* Turn off the power. */
421         pwr = 0;
422         WR1(slot, SDHCI_POWER_CONTROL, pwr);
423         /* If power down requested - leave it so. */
424         if (power == 0)
425                 return;
426         /* Set voltage. */
427         switch (1 << power) {
428         case MMC_OCR_LOW_VOLTAGE:
429                 pwr |= SDHCI_POWER_180;
430                 break;
431         case MMC_OCR_290_300:
432         case MMC_OCR_300_310:
433                 pwr |= SDHCI_POWER_300;
434                 break;
435         case MMC_OCR_320_330:
436         case MMC_OCR_330_340:
437                 pwr |= SDHCI_POWER_330;
438                 break;
439         }
440         WR1(slot, SDHCI_POWER_CONTROL, pwr);
441         /*
442          * Turn on VDD1 power.  Note that at least some Intel controllers can
443          * fail to enable bus power on the first try after transiting from D3
444          * to D0, so we give them up to 2 ms.
445          */
446         pwr |= SDHCI_POWER_ON;
447         for (i = 0; i < 20; i++) {
448                 WR1(slot, SDHCI_POWER_CONTROL, pwr);
449                 if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)
450                         break;
451                 DELAY(100);
452         }
453         if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON))
454                 slot_printf(slot, "Bus power failed to enable");
455
456         if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) {
457                 WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10);
458                 DELAY(10);
459                 WR1(slot, SDHCI_POWER_CONTROL, pwr);
460                 DELAY(300);
461         }
462 }
463
464 static void
465 sdhci_read_block_pio(struct sdhci_slot *slot)
466 {
467         uint32_t data;
468         char *buffer;
469         size_t left;
470
471         buffer = slot->curcmd->data->data;
472         buffer += slot->offset;
473         /* Transfer one block at a time. */
474         left = min(512, slot->curcmd->data->len - slot->offset);
475         slot->offset += left;
476
477         /* If we are too fast, broken controllers return zeroes. */
478         if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS)
479                 DELAY(10);
480         /* Handle unaligned and aligned buffer cases. */
481         if ((intptr_t)buffer & 3) {
482                 while (left > 3) {
483                         data = RD4(slot, SDHCI_BUFFER);
484                         buffer[0] = data;
485                         buffer[1] = (data >> 8);
486                         buffer[2] = (data >> 16);
487                         buffer[3] = (data >> 24);
488                         buffer += 4;
489                         left -= 4;
490                 }
491         } else {
492                 RD_MULTI_4(slot, SDHCI_BUFFER,
493                     (uint32_t *)buffer, left >> 2);
494                 left &= 3;
495         }
496         /* Handle uneven size case. */
497         if (left > 0) {
498                 data = RD4(slot, SDHCI_BUFFER);
499                 while (left > 0) {
500                         *(buffer++) = data;
501                         data >>= 8;
502                         left--;
503                 }
504         }
505 }
506
507 static void
508 sdhci_write_block_pio(struct sdhci_slot *slot)
509 {
510         uint32_t data = 0;
511         char *buffer;
512         size_t left;
513
514         buffer = slot->curcmd->data->data;
515         buffer += slot->offset;
516         /* Transfer one block at a time. */
517         left = min(512, slot->curcmd->data->len - slot->offset);
518         slot->offset += left;
519
520         /* Handle unaligned and aligned buffer cases. */
521         if ((intptr_t)buffer & 3) {
522                 while (left > 3) {
523                         data = buffer[0] +
524                             (buffer[1] << 8) +
525                             (buffer[2] << 16) +
526                             (buffer[3] << 24);
527                         left -= 4;
528                         buffer += 4;
529                         WR4(slot, SDHCI_BUFFER, data);
530                 }
531         } else {
532                 WR_MULTI_4(slot, SDHCI_BUFFER,
533                     (uint32_t *)buffer, left >> 2);
534                 left &= 3;
535         }
536         /* Handle uneven size case. */
537         if (left > 0) {
538                 while (left > 0) {
539                         data <<= 8;
540                         data += *(buffer++);
541                         left--;
542                 }
543                 WR4(slot, SDHCI_BUFFER, data);
544         }
545 }
546
547 static void
548 sdhci_transfer_pio(struct sdhci_slot *slot)
549 {
550
551         /* Read as many blocks as possible. */
552         if (slot->curcmd->data->flags & MMC_DATA_READ) {
553                 while (RD4(slot, SDHCI_PRESENT_STATE) &
554                     SDHCI_DATA_AVAILABLE) {
555                         sdhci_read_block_pio(slot);
556                         if (slot->offset >= slot->curcmd->data->len)
557                                 break;
558                 }
559         } else {
560                 while (RD4(slot, SDHCI_PRESENT_STATE) &
561                     SDHCI_SPACE_AVAILABLE) {
562                         sdhci_write_block_pio(slot);
563                         if (slot->offset >= slot->curcmd->data->len)
564                                 break;
565                 }
566         }
567 }
568
569 static void
570 sdhci_card_task(void *arg, int pending __unused)
571 {
572         struct sdhci_slot *slot = arg;
573         device_t d;
574
575         SDHCI_LOCK(slot);
576         if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) {
577                 if (slot->dev == NULL) {
578                         /* If card is present - attach mmc bus. */
579                         if (bootverbose || sdhci_debug)
580                                 slot_printf(slot, "Card inserted\n");
581                         d = slot->dev = device_add_child(slot->bus, "mmc", -1);
582                         SDHCI_UNLOCK(slot);
583                         if (d) {
584                                 device_set_ivars(d, slot);
585                                 (void)device_probe_and_attach(d);
586                         }
587                 } else
588                         SDHCI_UNLOCK(slot);
589         } else {
590                 if (slot->dev != NULL) {
591                         /* If no card present - detach mmc bus. */
592                         if (bootverbose || sdhci_debug)
593                                 slot_printf(slot, "Card removed\n");
594                         d = slot->dev;
595                         slot->dev = NULL;
596                         slot->intmask &= ~sdhci_tuning_intmask(slot);
597                         WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
598                         WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
599                         slot->opt &= ~SDHCI_TUNING_ENABLED;
600                         SDHCI_UNLOCK(slot);
601                         callout_drain(&slot->retune_callout);
602                         device_delete_child(slot->bus, d);
603                 } else
604                         SDHCI_UNLOCK(slot);
605         }
606 }
607
608 static void
609 sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present)
610 {
611         bool was_present;
612
613         /*
614          * If there was no card and now there is one, schedule the task to
615          * create the child device after a short delay.  The delay is to
616          * debounce the card insert (sometimes the card detect pin stabilizes
617          * before the other pins have made good contact).
618          *
619          * If there was a card present and now it's gone, immediately schedule
620          * the task to delete the child device.  No debouncing -- gone is gone,
621          * because once power is removed, a full card re-init is needed, and
622          * that happens by deleting and recreating the child device.
623          */
624         was_present = slot->dev != NULL;
625         if (!was_present && is_present) {
626                 taskqueue_enqueue_timeout(taskqueue_swi_giant,
627                     &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS);
628         } else if (was_present && !is_present) {
629                 taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task);
630         }
631 }
632
633 void
634 sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present)
635 {
636
637         SDHCI_LOCK(slot);
638         sdhci_handle_card_present_locked(slot, is_present);
639         SDHCI_UNLOCK(slot);
640 }
641
642 static void
643 sdhci_card_poll(void *arg)
644 {
645         struct sdhci_slot *slot = arg;
646
647         sdhci_handle_card_present(slot,
648             SDHCI_GET_CARD_PRESENT(slot->bus, slot));
649         callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS,
650             sdhci_card_poll, slot);
651 }
652
653 int
654 sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num)
655 {
656         kobjop_desc_t kobj_desc;
657         kobj_method_t *kobj_method;
658         uint32_t caps, caps2, freq, host_caps;
659         int err;
660
661         SDHCI_LOCK_INIT(slot);
662         slot->num = num;
663         slot->bus = dev;
664
665         /* Allocate DMA tag. */
666         err = bus_dma_tag_create(bus_get_dma_tag(dev),
667             DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
668             BUS_SPACE_MAXADDR, NULL, NULL,
669             DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE,
670             BUS_DMA_ALLOCNOW, NULL, NULL,
671             &slot->dmatag);
672         if (err != 0) {
673                 device_printf(dev, "Can't create DMA tag\n");
674                 SDHCI_LOCK_DESTROY(slot);
675                 return (err);
676         }
677         /* Allocate DMA memory. */
678         err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem,
679             BUS_DMA_NOWAIT, &slot->dmamap);
680         if (err != 0) {
681                 device_printf(dev, "Can't alloc DMA memory\n");
682                 bus_dma_tag_destroy(slot->dmatag);
683                 SDHCI_LOCK_DESTROY(slot);
684                 return (err);
685         }
686         /* Map the memory. */
687         err = bus_dmamap_load(slot->dmatag, slot->dmamap,
688             (void *)slot->dmamem, DMA_BLOCK_SIZE,
689             sdhci_getaddr, &slot->paddr, 0);
690         if (err != 0 || slot->paddr == 0) {
691                 device_printf(dev, "Can't load DMA memory\n");
692                 bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
693                 bus_dma_tag_destroy(slot->dmatag);
694                 SDHCI_LOCK_DESTROY(slot);
695                 if (err)
696                         return (err);
697                 else
698                         return (EFAULT);
699         }
700
701         slot->version = (RD2(slot, SDHCI_HOST_VERSION)
702                 >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK;
703         if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) {
704                 caps = slot->caps;
705                 caps2 = slot->caps2;
706         } else {
707                 caps = RD4(slot, SDHCI_CAPABILITIES);
708                 if (slot->version >= SDHCI_SPEC_300)
709                         caps2 = RD4(slot, SDHCI_CAPABILITIES2);
710                 else
711                         caps2 = 0;
712         }
713         if (slot->version >= SDHCI_SPEC_300) {
714                 if ((caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_REMOVABLE &&
715                     (caps & SDHCI_SLOTTYPE_MASK) != SDHCI_SLOTTYPE_EMBEDDED) {
716                         device_printf(dev,
717                             "Driver doesn't support shared bus slots\n");
718                         bus_dmamap_unload(slot->dmatag, slot->dmamap);
719                         bus_dmamem_free(slot->dmatag, slot->dmamem,
720                             slot->dmamap);
721                         bus_dma_tag_destroy(slot->dmatag);
722                         SDHCI_LOCK_DESTROY(slot);
723                         return (ENXIO);
724                 } else if ((caps & SDHCI_SLOTTYPE_MASK) ==
725                     SDHCI_SLOTTYPE_EMBEDDED) {
726                         slot->opt |= SDHCI_SLOT_EMBEDDED | SDHCI_NON_REMOVABLE;
727                 }
728         }
729         /* Calculate base clock frequency. */
730         if (slot->version >= SDHCI_SPEC_300)
731                 freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >>
732                     SDHCI_CLOCK_BASE_SHIFT;
733         else
734                 freq = (caps & SDHCI_CLOCK_BASE_MASK) >>
735                     SDHCI_CLOCK_BASE_SHIFT;
736         if (freq != 0)
737                 slot->max_clk = freq * 1000000;
738         /*
739          * If the frequency wasn't in the capabilities and the hardware driver
740          * hasn't already set max_clk we're probably not going to work right
741          * with an assumption, so complain about it.
742          */
743         if (slot->max_clk == 0) {
744                 slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000;
745                 device_printf(dev, "Hardware doesn't specify base clock "
746                     "frequency, using %dMHz as default.\n",
747                     SDHCI_DEFAULT_MAX_FREQ);
748         }
749         /* Calculate/set timeout clock frequency. */
750         if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) {
751                 slot->timeout_clk = slot->max_clk / 1000;
752         } else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) {
753                 slot->timeout_clk = 1000;
754         } else {
755                 slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >>
756                     SDHCI_TIMEOUT_CLK_SHIFT;
757                 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
758                         slot->timeout_clk *= 1000;
759         }
760         /*
761          * If the frequency wasn't in the capabilities and the hardware driver
762          * hasn't already set timeout_clk we'll probably work okay using the
763          * max timeout, but still mention it.
764          */
765         if (slot->timeout_clk == 0) {
766                 device_printf(dev, "Hardware doesn't specify timeout clock "
767                     "frequency, setting BROKEN_TIMEOUT quirk.\n");
768                 slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL;
769         }
770
771         slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot);
772         slot->host.f_max = slot->max_clk;
773         slot->host.host_ocr = 0;
774         if (caps & SDHCI_CAN_VDD_330)
775             slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340;
776         if (caps & SDHCI_CAN_VDD_300)
777             slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310;
778         /* 1.8V VDD is not supposed to be used for removable cards. */
779         if ((caps & SDHCI_CAN_VDD_180) && (slot->opt & SDHCI_SLOT_EMBEDDED))
780             slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE;
781         if (slot->host.host_ocr == 0) {
782                 device_printf(dev, "Hardware doesn't report any "
783                     "support voltages.\n");
784         }
785
786         host_caps = MMC_CAP_4_BIT_DATA;
787         if (caps & SDHCI_CAN_DO_8BITBUS)
788                 host_caps |= MMC_CAP_8_BIT_DATA;
789         if (caps & SDHCI_CAN_DO_HISPD)
790                 host_caps |= MMC_CAP_HSPEED;
791         if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC)
792                 host_caps |= MMC_CAP_BOOT_NOACC;
793         if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY)
794                 host_caps |= MMC_CAP_WAIT_WHILE_BUSY;
795
796         /* Determine supported UHS-I and eMMC modes. */
797         if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50))
798                 host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
799         if (caps2 & SDHCI_CAN_SDR104) {
800                 host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
801                 if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200))
802                         host_caps |= MMC_CAP_MMC_HS200;
803         } else if (caps2 & SDHCI_CAN_SDR50)
804                 host_caps |= MMC_CAP_UHS_SDR50;
805         if (caps2 & SDHCI_CAN_DDR50 &&
806             !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50))
807                 host_caps |= MMC_CAP_UHS_DDR50;
808         if (slot->quirks & SDHCI_QUIRK_MMC_DDR52)
809                 host_caps |= MMC_CAP_MMC_DDR52;
810         if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 &&
811             caps2 & SDHCI_CAN_MMC_HS400)
812                 host_caps |= MMC_CAP_MMC_HS400;
813         if (slot->quirks & SDHCI_QUIRK_MMC_HS400_IF_CAN_SDR104 &&
814             caps2 & SDHCI_CAN_SDR104)
815                 host_caps |= MMC_CAP_MMC_HS400;
816
817         /*
818          * Disable UHS-I and eMMC modes if the set_uhs_timing method is the
819          * default NULL implementation.
820          */
821         kobj_desc = &sdhci_set_uhs_timing_desc;
822         kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
823             kobj_desc);
824         if (kobj_method == &kobj_desc->deflt)
825                 host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
826                     MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
827                     MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400);
828
829 #define SDHCI_CAP_MODES_TUNING(caps2)                                   \
830     (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) |             \
831     MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 |        \
832     MMC_CAP_MMC_HS400)
833
834         /*
835          * Disable UHS-I and eMMC modes that require (re-)tuning if either
836          * the tune or re-tune method is the default NULL implementation.
837          */
838         kobj_desc = &mmcbr_tune_desc;
839         kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
840             kobj_desc);
841         if (kobj_method == &kobj_desc->deflt)
842                 goto no_tuning;
843         kobj_desc = &mmcbr_retune_desc;
844         kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
845             kobj_desc);
846         if (kobj_method == &kobj_desc->deflt) {
847 no_tuning:
848                 host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2));
849         }
850
851         /* Allocate tuning structures and determine tuning parameters. */
852         if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) {
853                 slot->opt |= SDHCI_TUNING_SUPPORTED;
854                 slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF,
855                     M_WAITOK);
856                 slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF,
857                     M_WAITOK);
858                 slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF,
859                     M_WAITOK);
860                 if (caps2 & SDHCI_TUNE_SDR50)
861                         slot->opt |= SDHCI_SDR50_NEEDS_TUNING;
862                 slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >>
863                     SDHCI_RETUNE_MODES_SHIFT;
864                 if (slot->retune_mode == SDHCI_RETUNE_MODE_1) {
865                         slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >>
866                             SDHCI_RETUNE_CNT_SHIFT;
867                         if (slot->retune_count > 0xb) {
868                                 device_printf(dev, "Unknown re-tuning count "
869                                     "%x, using 1 sec\n", slot->retune_count);
870                                 slot->retune_count = 1;
871                         } else if (slot->retune_count != 0)
872                                 slot->retune_count =
873                                     1 << (slot->retune_count - 1);
874                 }
875         }
876
877 #undef SDHCI_CAP_MODES_TUNING
878
879         /* Determine supported VCCQ signaling levels. */
880         host_caps |= MMC_CAP_SIGNALING_330;
881         if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
882             MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 |
883             MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 |
884             MMC_CAP_MMC_HS400_180))
885                 host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180;
886
887         /*
888          * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the
889          * default NULL implementation.  Disable 1.2 V support if it's the
890          * generic SDHCI implementation.
891          */
892         kobj_desc = &mmcbr_switch_vccq_desc;
893         kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL,
894             kobj_desc);
895         if (kobj_method == &kobj_desc->deflt)
896                 host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180);
897         else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq)
898                 host_caps &= ~MMC_CAP_SIGNALING_120;
899
900         /* Determine supported driver types (type B is always mandatory). */
901         if (caps2 & SDHCI_CAN_DRIVE_TYPE_A)
902                 host_caps |= MMC_CAP_DRIVER_TYPE_A;
903         if (caps2 & SDHCI_CAN_DRIVE_TYPE_C)
904                 host_caps |= MMC_CAP_DRIVER_TYPE_C;
905         if (caps2 & SDHCI_CAN_DRIVE_TYPE_D)
906                 host_caps |= MMC_CAP_DRIVER_TYPE_D;
907         slot->host.caps = host_caps;
908
909         /* Decide if we have usable DMA. */
910         if (caps & SDHCI_CAN_DO_DMA)
911                 slot->opt |= SDHCI_HAVE_DMA;
912
913         if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA)
914                 slot->opt &= ~SDHCI_HAVE_DMA;
915         if (slot->quirks & SDHCI_QUIRK_FORCE_DMA)
916                 slot->opt |= SDHCI_HAVE_DMA;
917         if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE)
918                 slot->opt |= SDHCI_NON_REMOVABLE;
919
920         /*
921          * Use platform-provided transfer backend
922          * with PIO as a fallback mechanism
923          */
924         if (slot->opt & SDHCI_PLATFORM_TRANSFER)
925                 slot->opt &= ~SDHCI_HAVE_DMA;
926
927         if (bootverbose || sdhci_debug) {
928                 slot_printf(slot,
929                     "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s %s\n",
930                     slot->max_clk / 1000000,
931                     (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "",
932                     (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" :
933                         ((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"),
934                     (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "",
935                     (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "",
936                     ((caps & SDHCI_CAN_VDD_180) &&
937                     (slot->opt & SDHCI_SLOT_EMBEDDED)) ? " 1.8V" : "",
938                     (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "",
939                     (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "",
940                     (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "",
941                     (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "",
942                     (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "",
943                     (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO",
944                     (slot->opt & SDHCI_SLOT_EMBEDDED) ? "embedded" :
945                     (slot->opt & SDHCI_NON_REMOVABLE) ? "non-removable" :
946                     "removable");
947                 if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 |
948                     MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE))
949                         slot_printf(slot, "eMMC:%s%s%s%s\n",
950                             (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "",
951                             (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "",
952                             (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "",
953                             ((host_caps &
954                             (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ==
955                             (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ?
956                             " HS400ES" : "");
957                 if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
958                     MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104))
959                         slot_printf(slot, "UHS-I:%s%s%s%s%s\n",
960                             (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "",
961                             (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "",
962                             (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "",
963                             (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "",
964                             (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : "");
965                 if (slot->opt & SDHCI_TUNING_SUPPORTED)
966                         slot_printf(slot, "Re-tuning count %d secs, mode %d\n",
967                             slot->retune_count, slot->retune_mode + 1);
968                 sdhci_dumpregs(slot);
969         }
970
971         slot->timeout = 10;
972         SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus),
973             SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO,
974             "timeout", CTLFLAG_RW, &slot->timeout, 0,
975             "Maximum timeout for SDHCI transfers (in secs)");
976         TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot);
977         TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0,
978                 sdhci_card_task, slot);
979         callout_init(&slot->card_poll_callout, 1);
980         callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0);
981         callout_init_mtx(&slot->retune_callout, &slot->mtx, 0);
982
983         if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) &&
984             !(slot->opt & SDHCI_NON_REMOVABLE)) {
985                 callout_reset(&slot->card_poll_callout,
986                     SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot);
987         }
988
989         sdhci_init(slot);
990
991         return (0);
992 }
993
994 void
995 sdhci_start_slot(struct sdhci_slot *slot)
996 {
997
998         sdhci_card_task(slot, 0);
999 }
1000
1001 int
1002 sdhci_cleanup_slot(struct sdhci_slot *slot)
1003 {
1004         device_t d;
1005
1006         callout_drain(&slot->timeout_callout);
1007         callout_drain(&slot->card_poll_callout);
1008         callout_drain(&slot->retune_callout);
1009         taskqueue_drain(taskqueue_swi_giant, &slot->card_task);
1010         taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task);
1011
1012         SDHCI_LOCK(slot);
1013         d = slot->dev;
1014         slot->dev = NULL;
1015         SDHCI_UNLOCK(slot);
1016         if (d != NULL)
1017                 device_delete_child(slot->bus, d);
1018
1019         SDHCI_LOCK(slot);
1020         sdhci_reset(slot, SDHCI_RESET_ALL);
1021         SDHCI_UNLOCK(slot);
1022         bus_dmamap_unload(slot->dmatag, slot->dmamap);
1023         bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap);
1024         bus_dma_tag_destroy(slot->dmatag);
1025         if (slot->opt & SDHCI_TUNING_SUPPORTED) {
1026                 free(slot->tune_req, M_DEVBUF);
1027                 free(slot->tune_cmd, M_DEVBUF);
1028                 free(slot->tune_data, M_DEVBUF);
1029         }
1030
1031         SDHCI_LOCK_DESTROY(slot);
1032
1033         return (0);
1034 }
1035
1036 int
1037 sdhci_generic_suspend(struct sdhci_slot *slot)
1038 {
1039
1040         /*
1041          * We expect the MMC layer to issue initial tuning after resume.
1042          * Otherwise, we'd need to indicate re-tuning including circuit reset
1043          * being required at least for re-tuning modes 1 and 2 ourselves.
1044          */
1045         callout_drain(&slot->retune_callout);
1046         SDHCI_LOCK(slot);
1047         slot->opt &= ~SDHCI_TUNING_ENABLED;
1048         sdhci_reset(slot, SDHCI_RESET_ALL);
1049         SDHCI_UNLOCK(slot);
1050
1051         return (0);
1052 }
1053
1054 int
1055 sdhci_generic_resume(struct sdhci_slot *slot)
1056 {
1057
1058         SDHCI_LOCK(slot);
1059         sdhci_init(slot);
1060         SDHCI_UNLOCK(slot);
1061
1062         return (0);
1063 }
1064
1065 uint32_t
1066 sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot)
1067 {
1068
1069         if (slot->version >= SDHCI_SPEC_300)
1070                 return (slot->max_clk / SDHCI_300_MAX_DIVIDER);
1071         else
1072                 return (slot->max_clk / SDHCI_200_MAX_DIVIDER);
1073 }
1074
1075 bool
1076 sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot)
1077 {
1078
1079         if (slot->opt & SDHCI_NON_REMOVABLE)
1080                 return true;
1081
1082         return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1083 }
1084
1085 void
1086 sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot)
1087 {
1088         struct mmc_ios *ios;
1089         uint16_t hostctrl2;
1090
1091         if (slot->version < SDHCI_SPEC_300)
1092                 return;
1093
1094         SDHCI_ASSERT_LOCKED(slot);
1095         ios = &slot->host.ios;
1096         sdhci_set_clock(slot, 0);
1097         hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1098         hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK;
1099         if (ios->clock > SD_SDR50_MAX) {
1100                 if (ios->timing == bus_timing_mmc_hs400 ||
1101                     ios->timing == bus_timing_mmc_hs400es)
1102                         hostctrl2 |= SDHCI_CTRL2_MMC_HS400;
1103                 else
1104                         hostctrl2 |= SDHCI_CTRL2_UHS_SDR104;
1105         }
1106         else if (ios->clock > SD_SDR25_MAX)
1107                 hostctrl2 |= SDHCI_CTRL2_UHS_SDR50;
1108         else if (ios->clock > SD_SDR12_MAX) {
1109                 if (ios->timing == bus_timing_uhs_ddr50 ||
1110                     ios->timing == bus_timing_mmc_ddr52)
1111                         hostctrl2 |= SDHCI_CTRL2_UHS_DDR50;
1112                 else
1113                         hostctrl2 |= SDHCI_CTRL2_UHS_SDR25;
1114         } else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY)
1115                 hostctrl2 |= SDHCI_CTRL2_UHS_SDR12;
1116         WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
1117         sdhci_set_clock(slot, ios->clock);
1118 }
1119
1120 int
1121 sdhci_generic_update_ios(device_t brdev, device_t reqdev)
1122 {
1123         struct sdhci_slot *slot = device_get_ivars(reqdev);
1124         struct mmc_ios *ios = &slot->host.ios;
1125
1126         SDHCI_LOCK(slot);
1127         /* Do full reset on bus power down to clear from any state. */
1128         if (ios->power_mode == power_off) {
1129                 WR4(slot, SDHCI_SIGNAL_ENABLE, 0);
1130                 sdhci_init(slot);
1131         }
1132         /* Configure the bus. */
1133         sdhci_set_clock(slot, ios->clock);
1134         sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd);
1135         if (ios->bus_width == bus_width_8) {
1136                 slot->hostctrl |= SDHCI_CTRL_8BITBUS;
1137                 slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
1138         } else if (ios->bus_width == bus_width_4) {
1139                 slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
1140                 slot->hostctrl |= SDHCI_CTRL_4BITBUS;
1141         } else if (ios->bus_width == bus_width_1) {
1142                 slot->hostctrl &= ~SDHCI_CTRL_8BITBUS;
1143                 slot->hostctrl &= ~SDHCI_CTRL_4BITBUS;
1144         } else {
1145                 panic("Invalid bus width: %d", ios->bus_width);
1146         }
1147         if (ios->clock > SD_SDR12_MAX &&
1148             !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT))
1149                 slot->hostctrl |= SDHCI_CTRL_HISPD;
1150         else
1151                 slot->hostctrl &= ~SDHCI_CTRL_HISPD;
1152         WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl);
1153         SDHCI_SET_UHS_TIMING(brdev, slot);
1154         /* Some controllers like reset after bus changes. */
1155         if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS)
1156                 sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1157
1158         SDHCI_UNLOCK(slot);
1159         return (0);
1160 }
1161
1162 int
1163 sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev)
1164 {
1165         struct sdhci_slot *slot = device_get_ivars(reqdev);
1166         enum mmc_vccq vccq;
1167         int err;
1168         uint16_t hostctrl2;
1169
1170         if (slot->version < SDHCI_SPEC_300)
1171                 return (0);
1172
1173         err = 0;
1174         vccq = slot->host.ios.vccq;
1175         SDHCI_LOCK(slot);
1176         sdhci_set_clock(slot, 0);
1177         hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1178         switch (vccq) {
1179         case vccq_330:
1180                 if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
1181                         goto done;
1182                 hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE;
1183                 WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
1184                 DELAY(5000);
1185                 hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1186                 if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE))
1187                         goto done;
1188                 err = EAGAIN;
1189                 break;
1190         case vccq_180:
1191                 if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) {
1192                         err = EINVAL;
1193                         goto done;
1194                 }
1195                 if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
1196                         goto done;
1197                 hostctrl2 |= SDHCI_CTRL2_S18_ENABLE;
1198                 WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2);
1199                 DELAY(5000);
1200                 hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1201                 if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE)
1202                         goto done;
1203                 err = EAGAIN;
1204                 break;
1205         default:
1206                 slot_printf(slot,
1207                     "Attempt to set unsupported signaling voltage\n");
1208                 err = EINVAL;
1209                 break;
1210         }
1211 done:
1212         sdhci_set_clock(slot, slot->host.ios.clock);
1213         SDHCI_UNLOCK(slot);
1214         return (err);
1215 }
1216
1217 int
1218 sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400)
1219 {
1220         struct sdhci_slot *slot = device_get_ivars(reqdev);
1221         struct mmc_ios *ios = &slot->host.ios;
1222         struct mmc_command *tune_cmd;
1223         struct mmc_data *tune_data;
1224         uint32_t opcode;
1225         int err;
1226
1227         if (!(slot->opt & SDHCI_TUNING_SUPPORTED))
1228                 return (0);
1229
1230         slot->retune_ticks = slot->retune_count * hz;
1231         opcode = MMC_SEND_TUNING_BLOCK;
1232         SDHCI_LOCK(slot);
1233         switch (ios->timing) {
1234         case bus_timing_mmc_hs400:
1235                 slot_printf(slot, "HS400 must be tuned in HS200 mode\n");
1236                 SDHCI_UNLOCK(slot);
1237                 return (EINVAL);
1238         case bus_timing_mmc_hs200:
1239                 /*
1240                  * In HS400 mode, controllers use the data strobe line to
1241                  * latch data from the devices so periodic re-tuning isn't
1242                  * expected to be required.
1243                  */
1244                 if (hs400)
1245                         slot->retune_ticks = 0;
1246                 opcode = MMC_SEND_TUNING_BLOCK_HS200;
1247                 break;
1248         case bus_timing_uhs_ddr50:
1249         case bus_timing_uhs_sdr104:
1250                 break;
1251         case bus_timing_uhs_sdr50:
1252                 if (slot->opt & SDHCI_SDR50_NEEDS_TUNING)
1253                         break;
1254                 /* FALLTHROUGH */
1255         default:
1256                 SDHCI_UNLOCK(slot);
1257                 return (0);
1258         }
1259
1260         tune_cmd = slot->tune_cmd;
1261         memset(tune_cmd, 0, sizeof(*tune_cmd));
1262         tune_cmd->opcode = opcode;
1263         tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1264         tune_data = tune_cmd->data = slot->tune_data;
1265         memset(tune_data, 0, sizeof(*tune_data));
1266         tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
1267             ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 :
1268             MMC_TUNING_LEN;
1269         tune_data->flags = MMC_DATA_READ;
1270         tune_data->mrq = tune_cmd->mrq = slot->tune_req;
1271
1272         slot->opt &= ~SDHCI_TUNING_ENABLED;
1273         err = sdhci_exec_tuning(slot, true);
1274         if (err == 0) {
1275                 slot->opt |= SDHCI_TUNING_ENABLED;
1276                 slot->intmask |= sdhci_tuning_intmask(slot);
1277                 WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
1278                 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1279                 if (slot->retune_ticks) {
1280                         callout_reset(&slot->retune_callout, slot->retune_ticks,
1281                             sdhci_retune, slot);
1282                 }
1283         }
1284         SDHCI_UNLOCK(slot);
1285         return (err);
1286 }
1287
1288 int
1289 sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset)
1290 {
1291         struct sdhci_slot *slot = device_get_ivars(reqdev);
1292         int err;
1293
1294         if (!(slot->opt & SDHCI_TUNING_ENABLED))
1295                 return (0);
1296
1297         /* HS400 must be tuned in HS200 mode. */
1298         if (slot->host.ios.timing == bus_timing_mmc_hs400)
1299                 return (EINVAL);
1300
1301         SDHCI_LOCK(slot);
1302         err = sdhci_exec_tuning(slot, reset);
1303         /*
1304          * There are two ways sdhci_exec_tuning() can fail:
1305          * EBUSY should not actually happen when requests are only issued
1306          *       with the host properly acquired, and
1307          * EIO   re-tuning failed (but it did work initially).
1308          *
1309          * In both cases, we should retry at later point if periodic re-tuning
1310          * is enabled.  Note that due to slot->retune_req not being cleared in
1311          * these failure cases, the MMC layer should trigger another attempt at
1312          * re-tuning with the next request anyway, though.
1313          */
1314         if (slot->retune_ticks) {
1315                 callout_reset(&slot->retune_callout, slot->retune_ticks,
1316                     sdhci_retune, slot);
1317         }
1318         SDHCI_UNLOCK(slot);
1319         return (err);
1320 }
1321
1322 static int
1323 sdhci_exec_tuning(struct sdhci_slot *slot, bool reset)
1324 {
1325         struct mmc_request *tune_req;
1326         struct mmc_command *tune_cmd;
1327         int i;
1328         uint32_t intmask;
1329         uint16_t hostctrl2;
1330         u_char opt;
1331
1332         SDHCI_ASSERT_LOCKED(slot);
1333         if (slot->req != NULL)
1334                 return (EBUSY);
1335
1336         /* Tuning doesn't work with DMA enabled. */
1337         opt = slot->opt;
1338         slot->opt = opt & ~SDHCI_HAVE_DMA;
1339
1340         /*
1341          * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only
1342          * kind of interrupt we receive in response to a tuning request.
1343          */
1344         intmask = slot->intmask;
1345         slot->intmask = SDHCI_INT_DATA_AVAIL;
1346         WR4(slot, SDHCI_INT_ENABLE, SDHCI_INT_DATA_AVAIL);
1347         WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL);
1348
1349         hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1350         if (reset)
1351                 hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK;
1352         else
1353                 hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK;
1354         WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING);
1355
1356         tune_req = slot->tune_req;
1357         tune_cmd = slot->tune_cmd;
1358         for (i = 0; i < MMC_TUNING_MAX; i++) {
1359                 memset(tune_req, 0, sizeof(*tune_req));
1360                 tune_req->cmd = tune_cmd;
1361                 tune_req->done = sdhci_req_wakeup;
1362                 tune_req->done_data = slot;
1363                 slot->req = tune_req;
1364                 slot->flags = 0;
1365                 sdhci_start(slot);
1366                 while (!(tune_req->flags & MMC_REQ_DONE))
1367                         msleep(tune_req, &slot->mtx, 0, "sdhciet", 0);
1368                 if (!(tune_req->flags & MMC_TUNE_DONE))
1369                         break;
1370                 hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2);
1371                 if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING))
1372                         break;
1373                 if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK)
1374                         DELAY(1000);
1375         }
1376
1377         /*
1378          * Restore DMA usage and interrupts.
1379          * Note that the interrupt aggregation code might have cleared
1380          * SDHCI_INT_DMA_END and/or SDHCI_INT_RESPONSE in slot->intmask
1381          * and SDHCI_SIGNAL_ENABLE respectively so ensure SDHCI_INT_ENABLE
1382          * doesn't lose these.
1383          */
1384         slot->opt = opt;
1385         slot->intmask = intmask;
1386         WR4(slot, SDHCI_INT_ENABLE, intmask | SDHCI_INT_DMA_END |
1387             SDHCI_INT_RESPONSE);
1388         WR4(slot, SDHCI_SIGNAL_ENABLE, intmask);
1389
1390         if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING |
1391             SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) {
1392                 slot->retune_req = 0;
1393                 return (0);
1394         }
1395
1396         slot_printf(slot, "Tuning failed, using fixed sampling clock\n");
1397         WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING |
1398             SDHCI_CTRL2_SAMPLING_CLOCK));
1399         sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1400         return (EIO);
1401 }
1402
1403 static void
1404 sdhci_retune(void *arg)
1405 {
1406         struct sdhci_slot *slot = arg;
1407
1408         slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
1409 }
1410
1411 static void
1412 sdhci_req_done(struct sdhci_slot *slot)
1413 {
1414         struct mmc_request *req;
1415
1416         if (slot->req != NULL && slot->curcmd != NULL) {
1417                 callout_stop(&slot->timeout_callout);
1418                 req = slot->req;
1419                 slot->req = NULL;
1420                 slot->curcmd = NULL;
1421                 req->done(req);
1422         }
1423 }
1424
1425 static void
1426 sdhci_req_wakeup(struct mmc_request *req)
1427 {
1428         struct sdhci_slot *slot;
1429
1430         slot = req->done_data;
1431         req->flags |= MMC_REQ_DONE;
1432         wakeup(req);
1433 }
1434
1435 static void
1436 sdhci_timeout(void *arg)
1437 {
1438         struct sdhci_slot *slot = arg;
1439
1440         if (slot->curcmd != NULL) {
1441                 slot_printf(slot, "Controller timeout\n");
1442                 sdhci_dumpregs(slot);
1443                 sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1444                 slot->curcmd->error = MMC_ERR_TIMEOUT;
1445                 sdhci_req_done(slot);
1446         } else {
1447                 slot_printf(slot, "Spurious timeout - no active command\n");
1448         }
1449 }
1450
1451 static void
1452 sdhci_set_transfer_mode(struct sdhci_slot *slot, struct mmc_data *data)
1453 {
1454         uint16_t mode;
1455
1456         if (data == NULL)
1457                 return;
1458
1459         mode = SDHCI_TRNS_BLK_CNT_EN;
1460         if (data->len > 512) {
1461                 mode |= SDHCI_TRNS_MULTI;
1462                 if (__predict_true(slot->req->stop != NULL))
1463                         mode |= SDHCI_TRNS_ACMD12;
1464         }
1465         if (data->flags & MMC_DATA_READ)
1466                 mode |= SDHCI_TRNS_READ;
1467         if (slot->flags & SDHCI_USE_DMA)
1468                 mode |= SDHCI_TRNS_DMA;
1469
1470         WR2(slot, SDHCI_TRANSFER_MODE, mode);
1471 }
1472
1473 static void
1474 sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd)
1475 {
1476         int flags, timeout;
1477         uint32_t mask;
1478
1479         slot->curcmd = cmd;
1480         slot->cmd_done = 0;
1481
1482         cmd->error = MMC_ERR_NONE;
1483
1484         /* This flags combination is not supported by controller. */
1485         if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1486                 slot_printf(slot, "Unsupported response type!\n");
1487                 cmd->error = MMC_ERR_FAILED;
1488                 sdhci_req_done(slot);
1489                 return;
1490         }
1491
1492         /*
1493          * Do not issue command if there is no card, clock or power.
1494          * Controller will not detect timeout without clock active.
1495          */
1496         if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) ||
1497             slot->power == 0 ||
1498             slot->clock == 0) {
1499                 cmd->error = MMC_ERR_FAILED;
1500                 sdhci_req_done(slot);
1501                 return;
1502         }
1503         /* Always wait for free CMD bus. */
1504         mask = SDHCI_CMD_INHIBIT;
1505         /* Wait for free DAT if we have data or busy signal. */
1506         if (cmd->data || (cmd->flags & MMC_RSP_BUSY))
1507                 mask |= SDHCI_DAT_INHIBIT;
1508         /*
1509          * We shouldn't wait for DAT for stop commands or CMD19/CMD21.  Note
1510          * that these latter are also special in that SDHCI_CMD_DATA should
1511          * be set below but no actual data is ever read from the controller.
1512         */
1513         if (cmd == slot->req->stop ||
1514             __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1515             cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))
1516                 mask &= ~SDHCI_DAT_INHIBIT;
1517         /*
1518          *  Wait for bus no more then 250 ms.  Typically there will be no wait
1519          *  here at all, but when writing a crash dump we may be bypassing the
1520          *  host platform's interrupt handler, and in some cases that handler
1521          *  may be working around hardware quirks such as not respecting r1b
1522          *  busy indications.  In those cases, this wait-loop serves the purpose
1523          *  of waiting for the prior command and data transfers to be done, and
1524          *  SD cards are allowed to take up to 250ms for write and erase ops.
1525          *  (It's usually more like 20-30ms in the real world.)
1526          */
1527         timeout = 250;
1528         while (mask & RD4(slot, SDHCI_PRESENT_STATE)) {
1529                 if (timeout == 0) {
1530                         slot_printf(slot, "Controller never released "
1531                             "inhibit bit(s).\n");
1532                         sdhci_dumpregs(slot);
1533                         cmd->error = MMC_ERR_FAILED;
1534                         sdhci_req_done(slot);
1535                         return;
1536                 }
1537                 timeout--;
1538                 DELAY(1000);
1539         }
1540
1541         /* Prepare command flags. */
1542         if (!(cmd->flags & MMC_RSP_PRESENT))
1543                 flags = SDHCI_CMD_RESP_NONE;
1544         else if (cmd->flags & MMC_RSP_136)
1545                 flags = SDHCI_CMD_RESP_LONG;
1546         else if (cmd->flags & MMC_RSP_BUSY)
1547                 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1548         else
1549                 flags = SDHCI_CMD_RESP_SHORT;
1550         if (cmd->flags & MMC_RSP_CRC)
1551                 flags |= SDHCI_CMD_CRC;
1552         if (cmd->flags & MMC_RSP_OPCODE)
1553                 flags |= SDHCI_CMD_INDEX;
1554         if (cmd->data)
1555                 flags |= SDHCI_CMD_DATA;
1556         if (cmd->opcode == MMC_STOP_TRANSMISSION)
1557                 flags |= SDHCI_CMD_TYPE_ABORT;
1558         /* Prepare data. */
1559         sdhci_start_data(slot, cmd->data);
1560         /*
1561          * Interrupt aggregation: To reduce total number of interrupts
1562          * group response interrupt with data interrupt when possible.
1563          * If there going to be data interrupt, mask response one.
1564          */
1565         if (slot->data_done == 0) {
1566                 WR4(slot, SDHCI_SIGNAL_ENABLE,
1567                     slot->intmask &= ~SDHCI_INT_RESPONSE);
1568         }
1569         /* Set command argument. */
1570         WR4(slot, SDHCI_ARGUMENT, cmd->arg);
1571         /* Set data transfer mode. */
1572         sdhci_set_transfer_mode(slot, cmd->data);
1573         /* Start command. */
1574         WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff));
1575         /* Start timeout callout. */
1576         callout_reset(&slot->timeout_callout, slot->timeout * hz,
1577             sdhci_timeout, slot);
1578 }
1579
1580 static void
1581 sdhci_finish_command(struct sdhci_slot *slot)
1582 {
1583         int i;
1584         uint32_t val;
1585         uint8_t extra;
1586
1587         slot->cmd_done = 1;
1588         /*
1589          * Interrupt aggregation: Restore command interrupt.
1590          * Main restore point for the case when command interrupt
1591          * happened first.
1592          */
1593         if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK &&
1594             slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200))
1595                 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |=
1596                     SDHCI_INT_RESPONSE);
1597         /* In case of error - reset host and return. */
1598         if (slot->curcmd->error) {
1599                 if (slot->curcmd->error == MMC_ERR_BADCRC)
1600                         slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1601                 sdhci_reset(slot, SDHCI_RESET_CMD);
1602                 sdhci_reset(slot, SDHCI_RESET_DATA);
1603                 sdhci_start(slot);
1604                 return;
1605         }
1606         /* If command has response - fetch it. */
1607         if (slot->curcmd->flags & MMC_RSP_PRESENT) {
1608                 if (slot->curcmd->flags & MMC_RSP_136) {
1609                         /* CRC is stripped so we need one byte shift. */
1610                         extra = 0;
1611                         for (i = 0; i < 4; i++) {
1612                                 val = RD4(slot, SDHCI_RESPONSE + i * 4);
1613                                 if (slot->quirks &
1614                                     SDHCI_QUIRK_DONT_SHIFT_RESPONSE)
1615                                         slot->curcmd->resp[3 - i] = val;
1616                                 else {
1617                                         slot->curcmd->resp[3 - i] =
1618                                             (val << 8) | extra;
1619                                         extra = val >> 24;
1620                                 }
1621                         }
1622                 } else
1623                         slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE);
1624         }
1625         /* If data ready - finish. */
1626         if (slot->data_done)
1627                 sdhci_start(slot);
1628 }
1629
1630 static void
1631 sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data)
1632 {
1633         uint32_t target_timeout, current_timeout;
1634         uint8_t div;
1635
1636         if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1637                 slot->data_done = 1;
1638                 return;
1639         }
1640
1641         slot->data_done = 0;
1642
1643         /* Calculate and set data timeout.*/
1644         /* XXX: We should have this from mmc layer, now assume 1 sec. */
1645         if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) {
1646                 div = 0xE;
1647         } else {
1648                 target_timeout = 1000000;
1649                 div = 0;
1650                 current_timeout = (1 << 13) * 1000 / slot->timeout_clk;
1651                 while (current_timeout < target_timeout && div < 0xE) {
1652                         ++div;
1653                         current_timeout <<= 1;
1654                 }
1655                 /* Compensate for an off-by-one error in the CaFe chip.*/
1656                 if (div < 0xE &&
1657                     (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) {
1658                         ++div;
1659                 }
1660         }
1661         WR1(slot, SDHCI_TIMEOUT_CONTROL, div);
1662
1663         if (data == NULL)
1664                 return;
1665
1666         /* Use DMA if possible. */
1667         if ((slot->opt & SDHCI_HAVE_DMA))
1668                 slot->flags |= SDHCI_USE_DMA;
1669         /* If data is small, broken DMA may return zeroes instead of data, */
1670         if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) &&
1671             (data->len <= 512))
1672                 slot->flags &= ~SDHCI_USE_DMA;
1673         /* Some controllers require even block sizes. */
1674         if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) &&
1675             ((data->len) & 0x3))
1676                 slot->flags &= ~SDHCI_USE_DMA;
1677         /* Load DMA buffer. */
1678         if (slot->flags & SDHCI_USE_DMA) {
1679                 if (data->flags & MMC_DATA_READ)
1680                         bus_dmamap_sync(slot->dmatag, slot->dmamap,
1681                             BUS_DMASYNC_PREREAD);
1682                 else {
1683                         memcpy(slot->dmamem, data->data,
1684                             (data->len < DMA_BLOCK_SIZE) ?
1685                             data->len : DMA_BLOCK_SIZE);
1686                         bus_dmamap_sync(slot->dmatag, slot->dmamap,
1687                             BUS_DMASYNC_PREWRITE);
1688                 }
1689                 WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1690                 /* Interrupt aggregation: Mask border interrupt
1691                  * for the last page and unmask else. */
1692                 if (data->len == DMA_BLOCK_SIZE)
1693                         slot->intmask &= ~SDHCI_INT_DMA_END;
1694                 else
1695                         slot->intmask |= SDHCI_INT_DMA_END;
1696                 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1697         }
1698         /* Current data offset for both PIO and DMA. */
1699         slot->offset = 0;
1700         /* Set block size and request IRQ on 4K border. */
1701         WR2(slot, SDHCI_BLOCK_SIZE, SDHCI_MAKE_BLKSZ(DMA_BOUNDARY,
1702             (data->len < 512) ? data->len : 512));
1703         /* Set block count. */
1704         WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512);
1705 }
1706
1707 void
1708 sdhci_finish_data(struct sdhci_slot *slot)
1709 {
1710         struct mmc_data *data = slot->curcmd->data;
1711         size_t left;
1712
1713         /* Interrupt aggregation: Restore command interrupt.
1714          * Auxiliary restore point for the case when data interrupt
1715          * happened first. */
1716         if (!slot->cmd_done) {
1717                 WR4(slot, SDHCI_SIGNAL_ENABLE,
1718                     slot->intmask |= SDHCI_INT_RESPONSE);
1719         }
1720         /* Unload rest of data from DMA buffer. */
1721         if (!slot->data_done && (slot->flags & SDHCI_USE_DMA)) {
1722                 if (data->flags & MMC_DATA_READ) {
1723                         left = data->len - slot->offset;
1724                         bus_dmamap_sync(slot->dmatag, slot->dmamap,
1725                             BUS_DMASYNC_POSTREAD);
1726                         memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1727                             (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
1728                 } else
1729                         bus_dmamap_sync(slot->dmatag, slot->dmamap,
1730                             BUS_DMASYNC_POSTWRITE);
1731         }
1732         slot->data_done = 1;
1733         /* If there was error - reset the host. */
1734         if (slot->curcmd->error) {
1735                 if (slot->curcmd->error == MMC_ERR_BADCRC)
1736                         slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
1737                 sdhci_reset(slot, SDHCI_RESET_CMD);
1738                 sdhci_reset(slot, SDHCI_RESET_DATA);
1739                 sdhci_start(slot);
1740                 return;
1741         }
1742         /* If we already have command response - finish. */
1743         if (slot->cmd_done)
1744                 sdhci_start(slot);
1745 }
1746
1747 static void
1748 sdhci_start(struct sdhci_slot *slot)
1749 {
1750         struct mmc_request *req;
1751
1752         req = slot->req;
1753         if (req == NULL)
1754                 return;
1755
1756         if (!(slot->flags & CMD_STARTED)) {
1757                 slot->flags |= CMD_STARTED;
1758                 sdhci_start_command(slot, req->cmd);
1759                 return;
1760         }
1761 /*      We don't need this until using Auto-CMD12 feature
1762         if (!(slot->flags & STOP_STARTED) && req->stop) {
1763                 slot->flags |= STOP_STARTED;
1764                 sdhci_start_command(slot, req->stop);
1765                 return;
1766         }
1767 */
1768         if (__predict_false(sdhci_debug > 1))
1769                 slot_printf(slot, "result: %d\n", req->cmd->error);
1770         if (!req->cmd->error &&
1771             (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) {
1772                 sdhci_reset(slot, SDHCI_RESET_CMD);
1773                 sdhci_reset(slot, SDHCI_RESET_DATA);
1774         }
1775
1776         sdhci_req_done(slot);
1777 }
1778
1779 int
1780 sdhci_generic_request(device_t brdev __unused, device_t reqdev,
1781     struct mmc_request *req)
1782 {
1783         struct sdhci_slot *slot = device_get_ivars(reqdev);
1784
1785         SDHCI_LOCK(slot);
1786         if (slot->req != NULL) {
1787                 SDHCI_UNLOCK(slot);
1788                 return (EBUSY);
1789         }
1790         if (__predict_false(sdhci_debug > 1)) {
1791                 slot_printf(slot,
1792                     "CMD%u arg %#x flags %#x dlen %u dflags %#x\n",
1793                     req->cmd->opcode, req->cmd->arg, req->cmd->flags,
1794                     (req->cmd->data)?(u_int)req->cmd->data->len:0,
1795                     (req->cmd->data)?req->cmd->data->flags:0);
1796         }
1797         slot->req = req;
1798         slot->flags = 0;
1799         sdhci_start(slot);
1800         SDHCI_UNLOCK(slot);
1801         if (dumping) {
1802                 while (slot->req != NULL) {
1803                         sdhci_generic_intr(slot);
1804                         DELAY(10);
1805                 }
1806         }
1807         return (0);
1808 }
1809
1810 int
1811 sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev)
1812 {
1813         struct sdhci_slot *slot = device_get_ivars(reqdev);
1814         uint32_t val;
1815
1816         SDHCI_LOCK(slot);
1817         val = RD4(slot, SDHCI_PRESENT_STATE);
1818         SDHCI_UNLOCK(slot);
1819         return (!(val & SDHCI_WRITE_PROTECT));
1820 }
1821
1822 int
1823 sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev)
1824 {
1825         struct sdhci_slot *slot = device_get_ivars(reqdev);
1826         int err = 0;
1827
1828         SDHCI_LOCK(slot);
1829         while (slot->bus_busy)
1830                 msleep(slot, &slot->mtx, 0, "sdhciah", 0);
1831         slot->bus_busy++;
1832         /* Activate led. */
1833         WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED);
1834         SDHCI_UNLOCK(slot);
1835         return (err);
1836 }
1837
1838 int
1839 sdhci_generic_release_host(device_t brdev __unused, device_t reqdev)
1840 {
1841         struct sdhci_slot *slot = device_get_ivars(reqdev);
1842
1843         SDHCI_LOCK(slot);
1844         /* Deactivate led. */
1845         WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED);
1846         slot->bus_busy--;
1847         SDHCI_UNLOCK(slot);
1848         wakeup(slot);
1849         return (0);
1850 }
1851
1852 static void
1853 sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask)
1854 {
1855
1856         if (!slot->curcmd) {
1857                 slot_printf(slot, "Got command interrupt 0x%08x, but "
1858                     "there is no active command.\n", intmask);
1859                 sdhci_dumpregs(slot);
1860                 return;
1861         }
1862         if (intmask & SDHCI_INT_TIMEOUT)
1863                 slot->curcmd->error = MMC_ERR_TIMEOUT;
1864         else if (intmask & SDHCI_INT_CRC)
1865                 slot->curcmd->error = MMC_ERR_BADCRC;
1866         else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
1867                 slot->curcmd->error = MMC_ERR_FIFO;
1868
1869         sdhci_finish_command(slot);
1870 }
1871
1872 static void
1873 sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask)
1874 {
1875         struct mmc_data *data;
1876         size_t left;
1877
1878         if (!slot->curcmd) {
1879                 slot_printf(slot, "Got data interrupt 0x%08x, but "
1880                     "there is no active command.\n", intmask);
1881                 sdhci_dumpregs(slot);
1882                 return;
1883         }
1884         if (slot->curcmd->data == NULL &&
1885             (slot->curcmd->flags & MMC_RSP_BUSY) == 0) {
1886                 slot_printf(slot, "Got data interrupt 0x%08x, but "
1887                     "there is no active data operation.\n",
1888                     intmask);
1889                 sdhci_dumpregs(slot);
1890                 return;
1891         }
1892         if (intmask & SDHCI_INT_DATA_TIMEOUT)
1893                 slot->curcmd->error = MMC_ERR_TIMEOUT;
1894         else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1895                 slot->curcmd->error = MMC_ERR_BADCRC;
1896         if (slot->curcmd->data == NULL &&
1897             (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
1898             SDHCI_INT_DMA_END))) {
1899                 slot_printf(slot, "Got data interrupt 0x%08x, but "
1900                     "there is busy-only command.\n", intmask);
1901                 sdhci_dumpregs(slot);
1902                 slot->curcmd->error = MMC_ERR_INVALID;
1903         }
1904         if (slot->curcmd->error) {
1905                 /* No need to continue after any error. */
1906                 goto done;
1907         }
1908
1909         /* Handle tuning completion interrupt. */
1910         if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) &&
1911             (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK ||
1912             slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) {
1913                 slot->req->flags |= MMC_TUNE_DONE;
1914                 sdhci_finish_command(slot);
1915                 sdhci_finish_data(slot);
1916                 return;
1917         }
1918         /* Handle PIO interrupt. */
1919         if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
1920                 if ((slot->opt & SDHCI_PLATFORM_TRANSFER) &&
1921                     SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) {
1922                         SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot,
1923                             &intmask);
1924                         slot->flags |= PLATFORM_DATA_STARTED;
1925                 } else
1926                         sdhci_transfer_pio(slot);
1927         }
1928         /* Handle DMA border. */
1929         if (intmask & SDHCI_INT_DMA_END) {
1930                 data = slot->curcmd->data;
1931
1932                 /* Unload DMA buffer ... */
1933                 left = data->len - slot->offset;
1934                 if (data->flags & MMC_DATA_READ) {
1935                         bus_dmamap_sync(slot->dmatag, slot->dmamap,
1936                             BUS_DMASYNC_POSTREAD);
1937                         memcpy((u_char*)data->data + slot->offset, slot->dmamem,
1938                             (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE);
1939                 } else {
1940                         bus_dmamap_sync(slot->dmatag, slot->dmamap,
1941                             BUS_DMASYNC_POSTWRITE);
1942                 }
1943                 /* ... and reload it again. */
1944                 slot->offset += DMA_BLOCK_SIZE;
1945                 left = data->len - slot->offset;
1946                 if (data->flags & MMC_DATA_READ) {
1947                         bus_dmamap_sync(slot->dmatag, slot->dmamap,
1948                             BUS_DMASYNC_PREREAD);
1949                 } else {
1950                         memcpy(slot->dmamem, (u_char*)data->data + slot->offset,
1951                             (left < DMA_BLOCK_SIZE)? left : DMA_BLOCK_SIZE);
1952                         bus_dmamap_sync(slot->dmatag, slot->dmamap,
1953                             BUS_DMASYNC_PREWRITE);
1954                 }
1955                 /* Interrupt aggregation: Mask border interrupt
1956                  * for the last page. */
1957                 if (left == DMA_BLOCK_SIZE) {
1958                         slot->intmask &= ~SDHCI_INT_DMA_END;
1959                         WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
1960                 }
1961                 /* Restart DMA. */
1962                 WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr);
1963         }
1964         /* We have got all data. */
1965         if (intmask & SDHCI_INT_DATA_END) {
1966                 if (slot->flags & PLATFORM_DATA_STARTED) {
1967                         slot->flags &= ~PLATFORM_DATA_STARTED;
1968                         SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1969                 } else
1970                         sdhci_finish_data(slot);
1971         }
1972 done:
1973         if (slot->curcmd != NULL && slot->curcmd->error != 0) {
1974                 if (slot->flags & PLATFORM_DATA_STARTED) {
1975                         slot->flags &= ~PLATFORM_DATA_STARTED;
1976                         SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot);
1977                 } else
1978                         sdhci_finish_data(slot);
1979         }
1980 }
1981
1982 static void
1983 sdhci_acmd_irq(struct sdhci_slot *slot, uint16_t acmd_err)
1984 {
1985
1986         if (!slot->curcmd) {
1987                 slot_printf(slot, "Got AutoCMD12 error 0x%04x, but "
1988                     "there is no active command.\n", acmd_err);
1989                 sdhci_dumpregs(slot);
1990                 return;
1991         }
1992         slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", acmd_err);
1993         sdhci_reset(slot, SDHCI_RESET_CMD);
1994 }
1995
1996 void
1997 sdhci_generic_intr(struct sdhci_slot *slot)
1998 {
1999         uint32_t intmask, present;
2000         uint16_t val16;
2001
2002         SDHCI_LOCK(slot);
2003         /* Read slot interrupt status. */
2004         intmask = RD4(slot, SDHCI_INT_STATUS);
2005         if (intmask == 0 || intmask == 0xffffffff) {
2006                 SDHCI_UNLOCK(slot);
2007                 return;
2008         }
2009         if (__predict_false(sdhci_debug > 2))
2010                 slot_printf(slot, "Interrupt %#x\n", intmask);
2011
2012         /* Handle tuning error interrupt. */
2013         if (__predict_false(intmask & SDHCI_INT_TUNEERR)) {
2014                 WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_TUNEERR);
2015                 slot_printf(slot, "Tuning error indicated\n");
2016                 slot->retune_req |= SDHCI_RETUNE_REQ_RESET;
2017                 if (slot->curcmd) {
2018                         slot->curcmd->error = MMC_ERR_BADCRC;
2019                         sdhci_finish_command(slot);
2020                 }
2021         }
2022         /* Handle re-tuning interrupt. */
2023         if (__predict_false(intmask & SDHCI_INT_RETUNE))
2024                 slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED;
2025         /* Handle card presence interrupts. */
2026         if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2027                 present = (intmask & SDHCI_INT_CARD_INSERT) != 0;
2028                 slot->intmask &=
2029                     ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2030                 slot->intmask |= present ? SDHCI_INT_CARD_REMOVE :
2031                     SDHCI_INT_CARD_INSERT;
2032                 WR4(slot, SDHCI_INT_ENABLE, slot->intmask);
2033                 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask);
2034                 WR4(slot, SDHCI_INT_STATUS, intmask &
2035                     (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE));
2036                 sdhci_handle_card_present_locked(slot, present);
2037         }
2038         /* Handle command interrupts. */
2039         if (intmask & SDHCI_INT_CMD_MASK) {
2040                 WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK);
2041                 sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK);
2042         }
2043         /* Handle data interrupts. */
2044         if (intmask & SDHCI_INT_DATA_MASK) {
2045                 WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK);
2046                 /* Don't call data_irq in case of errored command. */
2047                 if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0)
2048                         sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK);
2049         }
2050         /* Handle AutoCMD12 error interrupt. */
2051         if (intmask & SDHCI_INT_ACMD12ERR) {
2052                 /* Clearing SDHCI_INT_ACMD12ERR may clear SDHCI_ACMD12_ERR. */
2053                 val16 = RD2(slot, SDHCI_ACMD12_ERR);
2054                 WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR);
2055                 sdhci_acmd_irq(slot, val16);
2056         }
2057         /* Handle bus power interrupt. */
2058         if (intmask & SDHCI_INT_BUS_POWER) {
2059                 WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER);
2060                 slot_printf(slot, "Card is consuming too much power!\n");
2061         }
2062         intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE |
2063             SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK |
2064             SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER);
2065         /* The rest is unknown. */
2066         if (intmask) {
2067                 WR4(slot, SDHCI_INT_STATUS, intmask);
2068                 slot_printf(slot, "Unexpected interrupt 0x%08x.\n",
2069                     intmask);
2070                 sdhci_dumpregs(slot);
2071         }
2072
2073         SDHCI_UNLOCK(slot);
2074 }
2075
2076 int
2077 sdhci_generic_read_ivar(device_t bus, device_t child, int which,
2078     uintptr_t *result)
2079 {
2080         struct sdhci_slot *slot = device_get_ivars(child);
2081
2082         switch (which) {
2083         default:
2084                 return (EINVAL);
2085         case MMCBR_IVAR_BUS_MODE:
2086                 *result = slot->host.ios.bus_mode;
2087                 break;
2088         case MMCBR_IVAR_BUS_WIDTH:
2089                 *result = slot->host.ios.bus_width;
2090                 break;
2091         case MMCBR_IVAR_CHIP_SELECT:
2092                 *result = slot->host.ios.chip_select;
2093                 break;
2094         case MMCBR_IVAR_CLOCK:
2095                 *result = slot->host.ios.clock;
2096                 break;
2097         case MMCBR_IVAR_F_MIN:
2098                 *result = slot->host.f_min;
2099                 break;
2100         case MMCBR_IVAR_F_MAX:
2101                 *result = slot->host.f_max;
2102                 break;
2103         case MMCBR_IVAR_HOST_OCR:
2104                 *result = slot->host.host_ocr;
2105                 break;
2106         case MMCBR_IVAR_MODE:
2107                 *result = slot->host.mode;
2108                 break;
2109         case MMCBR_IVAR_OCR:
2110                 *result = slot->host.ocr;
2111                 break;
2112         case MMCBR_IVAR_POWER_MODE:
2113                 *result = slot->host.ios.power_mode;
2114                 break;
2115         case MMCBR_IVAR_VDD:
2116                 *result = slot->host.ios.vdd;
2117                 break;
2118         case MMCBR_IVAR_RETUNE_REQ:
2119                 if (slot->opt & SDHCI_TUNING_ENABLED) {
2120                         if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) {
2121                                 *result = retune_req_reset;
2122                                 break;
2123                         }
2124                         if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) {
2125                                 *result = retune_req_normal;
2126                                 break;
2127                         }
2128                 }
2129                 *result = retune_req_none;
2130                 break;
2131         case MMCBR_IVAR_VCCQ:
2132                 *result = slot->host.ios.vccq;
2133                 break;
2134         case MMCBR_IVAR_CAPS:
2135                 *result = slot->host.caps;
2136                 break;
2137         case MMCBR_IVAR_TIMING:
2138                 *result = slot->host.ios.timing;
2139                 break;
2140         case MMCBR_IVAR_MAX_DATA:
2141                 /*
2142                  * Re-tuning modes 1 and 2 restrict the maximum data length
2143                  * per read/write command to 4 MiB.
2144                  */
2145                 if (slot->opt & SDHCI_TUNING_ENABLED &&
2146                     (slot->retune_mode == SDHCI_RETUNE_MODE_1 ||
2147                     slot->retune_mode == SDHCI_RETUNE_MODE_2)) {
2148                         *result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE;
2149                         break;
2150                 }
2151                 *result = 65535;
2152                 break;
2153         case MMCBR_IVAR_MAX_BUSY_TIMEOUT:
2154                 /*
2155                  * Currently, sdhci_start_data() hardcodes 1 s for all CMDs.
2156                  */
2157                 *result = 1000000;
2158                 break;
2159         }
2160         return (0);
2161 }
2162
2163 int
2164 sdhci_generic_write_ivar(device_t bus, device_t child, int which,
2165     uintptr_t value)
2166 {
2167         struct sdhci_slot *slot = device_get_ivars(child);
2168         uint32_t clock, max_clock;
2169         int i;
2170
2171         switch (which) {
2172         default:
2173                 return (EINVAL);
2174         case MMCBR_IVAR_BUS_MODE:
2175                 slot->host.ios.bus_mode = value;
2176                 break;
2177         case MMCBR_IVAR_BUS_WIDTH:
2178                 slot->host.ios.bus_width = value;
2179                 break;
2180         case MMCBR_IVAR_CHIP_SELECT:
2181                 slot->host.ios.chip_select = value;
2182                 break;
2183         case MMCBR_IVAR_CLOCK:
2184                 if (value > 0) {
2185                         max_clock = slot->max_clk;
2186                         clock = max_clock;
2187
2188                         if (slot->version < SDHCI_SPEC_300) {
2189                                 for (i = 0; i < SDHCI_200_MAX_DIVIDER;
2190                                     i <<= 1) {
2191                                         if (clock <= value)
2192                                                 break;
2193                                         clock >>= 1;
2194                                 }
2195                         } else {
2196                                 for (i = 0; i < SDHCI_300_MAX_DIVIDER;
2197                                     i += 2) {
2198                                         if (clock <= value)
2199                                                 break;
2200                                         clock = max_clock / (i + 2);
2201                                 }
2202                         }
2203
2204                         slot->host.ios.clock = clock;
2205                 } else
2206                         slot->host.ios.clock = 0;
2207                 break;
2208         case MMCBR_IVAR_MODE:
2209                 slot->host.mode = value;
2210                 break;
2211         case MMCBR_IVAR_OCR:
2212                 slot->host.ocr = value;
2213                 break;
2214         case MMCBR_IVAR_POWER_MODE:
2215                 slot->host.ios.power_mode = value;
2216                 break;
2217         case MMCBR_IVAR_VDD:
2218                 slot->host.ios.vdd = value;
2219                 break;
2220         case MMCBR_IVAR_VCCQ:
2221                 slot->host.ios.vccq = value;
2222                 break;
2223         case MMCBR_IVAR_TIMING:
2224                 slot->host.ios.timing = value;
2225                 break;
2226         case MMCBR_IVAR_CAPS:
2227         case MMCBR_IVAR_HOST_OCR:
2228         case MMCBR_IVAR_F_MIN:
2229         case MMCBR_IVAR_F_MAX:
2230         case MMCBR_IVAR_MAX_DATA:
2231         case MMCBR_IVAR_RETUNE_REQ:
2232                 return (EINVAL);
2233         }
2234         return (0);
2235 }
2236
2237 MODULE_VERSION(sdhci, 1);