]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/mmc/mmc.c
MFC: r292420
[FreeBSD/stable/10.git] / sys / dev / mmc / mmc.c
1 /*-
2  * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Portions of this software may have been developed with reference to
26  * the SD Simplified Specification.  The following disclaimer may apply:
27  *
28  * The following conditions apply to the release of the simplified
29  * specification ("Simplified Specification") by the SD Card Association and
30  * the SD Group. The Simplified Specification is a subset of the complete SD
31  * Specification which is owned by the SD Card Association and the SD
32  * Group. This Simplified Specification is provided on a non-confidential
33  * basis subject to the disclaimers below. Any implementation of the
34  * Simplified Specification may require a license from the SD Card
35  * Association, SD Group, SD-3C LLC or other third parties.
36  *
37  * Disclaimers:
38  *
39  * The information contained in the Simplified Specification is presented only
40  * as a standard specification for SD Cards and SD Host/Ancillary products and
41  * is provided "AS-IS" without any representations or warranties of any
42  * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
43  * Card Association for any damages, any infringements of patents or other
44  * right of the SD Group, SD-3C LLC, the SD Card Association or any third
45  * parties, which may result from its use. No license is granted by
46  * implication, estoppel or otherwise under any patent or other rights of the
47  * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48  * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49  * or the SD Card Association to disclose or distribute any technical
50  * information, know-how or other confidential information to any third party.
51  */
52
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/malloc.h>
60 #include <sys/lock.h>
61 #include <sys/module.h>
62 #include <sys/mutex.h>
63 #include <sys/bus.h>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
66 #include <sys/time.h>
67
68 #include <dev/mmc/mmcreg.h>
69 #include <dev/mmc/mmcbrvar.h>
70 #include <dev/mmc/mmcvar.h>
71 #include "mmcbr_if.h"
72 #include "mmcbus_if.h"
73
74 struct mmc_softc {
75         device_t dev;
76         struct mtx sc_mtx;
77         struct intr_config_hook config_intrhook;
78         device_t owner;
79         uint32_t last_rca;
80         int      squelched; /* suppress reporting of (expected) errors */
81         int      log_count;
82         struct timeval log_time;
83 };
84
85 #define LOG_PPS         5 /* Log no more than 5 errors per second. */
86
87 /*
88  * Per-card data
89  */
90 struct mmc_ivars {
91         uint32_t raw_cid[4];    /* Raw bits of the CID */
92         uint32_t raw_csd[4];    /* Raw bits of the CSD */
93         uint32_t raw_scr[2];    /* Raw bits of the SCR */
94         uint8_t raw_ext_csd[512];       /* Raw bits of the EXT_CSD */
95         uint32_t raw_sd_status[16];     /* Raw bits of the SD_STATUS */
96         uint16_t rca;
97         enum mmc_card_mode mode;
98         struct mmc_cid cid;     /* cid decoded */
99         struct mmc_csd csd;     /* csd decoded */
100         struct mmc_scr scr;     /* scr decoded */
101         struct mmc_sd_status sd_status; /* SD_STATUS decoded */
102         u_char read_only;       /* True when the device is read-only */
103         u_char bus_width;       /* Bus width to use */
104         u_char timing;          /* Bus timing support */
105         u_char high_cap;        /* High Capacity card (block addressed) */
106         uint32_t sec_count;     /* Card capacity in 512byte blocks */
107         uint32_t tran_speed;    /* Max speed in normal mode */
108         uint32_t hs_tran_speed; /* Max speed in high speed mode */
109         uint32_t erase_sector;  /* Card native erase sector size */
110         char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
111         char card_sn_string[16];/* Formatted serial # for disk->d_ident */
112 };
113
114 #define CMD_RETRIES     3
115
116 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
117
118 static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
119
120 static int mmc_debug;
121 TUNABLE_INT("hw.mmc.debug", &mmc_debug);
122 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &mmc_debug, 0, "Debug level");
123
124 /* bus entry points */
125 static int mmc_acquire_bus(device_t busdev, device_t dev);
126 static int mmc_attach(device_t dev);
127 static int mmc_child_location_str(device_t dev, device_t child, char *buf,
128     size_t buflen);
129 static int mmc_detach(device_t dev);
130 static int mmc_probe(device_t dev);
131 static int mmc_read_ivar(device_t bus, device_t child, int which,
132     uintptr_t *result);
133 static int mmc_release_bus(device_t busdev, device_t dev);
134 static int mmc_resume(device_t dev);
135 static int mmc_suspend(device_t dev);
136 static int mmc_wait_for_request(device_t brdev, device_t reqdev,
137     struct mmc_request *req);
138 static int mmc_write_ivar(device_t bus, device_t child, int which,
139     uintptr_t value);
140
141 #define MMC_LOCK(_sc)           mtx_lock(&(_sc)->sc_mtx)
142 #define MMC_UNLOCK(_sc)         mtx_unlock(&(_sc)->sc_mtx)
143 #define MMC_LOCK_INIT(_sc)                                      \
144         mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev),   \
145             "mmc", MTX_DEF)
146 #define MMC_LOCK_DESTROY(_sc)   mtx_destroy(&_sc->sc_mtx);
147 #define MMC_ASSERT_LOCKED(_sc)  mtx_assert(&_sc->sc_mtx, MA_OWNED);
148 #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
149
150 static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
151 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
152 static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
153     struct mmc_sd_status *sd_status);
154 static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
155     uint32_t *rawsdstatus);
156 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
157     uint32_t *rawscr);
158 static int mmc_calculate_clock(struct mmc_softc *sc);
159 static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid);
160 static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
161 static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
162 static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
163 static void mmc_delayed_attach(void *xsc);
164 static int mmc_delete_cards(struct mmc_softc *sc);
165 static void mmc_discover_cards(struct mmc_softc *sc);
166 static void mmc_format_card_id_string(struct mmc_ivars *ivar);
167 static void mmc_go_discovery(struct mmc_softc *sc);
168 static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
169     int size);
170 static int mmc_highest_voltage(uint32_t ocr);
171 static void mmc_idle_cards(struct mmc_softc *sc);
172 static void mmc_ms_delay(int ms);
173 static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
174 static void mmc_power_down(struct mmc_softc *sc);
175 static void mmc_power_up(struct mmc_softc *sc);
176 static void mmc_rescan_cards(struct mmc_softc *sc);
177 static void mmc_scan(struct mmc_softc *sc);
178 static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
179     uint8_t value, uint8_t *res);
180 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
181 static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
182 static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
183     uint32_t *rocr);
184 static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
185 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
186 static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
187 static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
188     uint32_t *rocr);
189 static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
190 static int mmc_send_status(struct mmc_softc *sc, uint16_t rca,
191     uint32_t *status);
192 static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
193 static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca,
194     int width);
195 static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
196 static int mmc_set_timing(struct mmc_softc *sc, int timing);
197 static int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index,
198     uint8_t value);
199 static int mmc_test_bus_width(struct mmc_softc *sc);
200 static int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
201     struct mmc_command *cmd, int retries);
202 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
203     int retries);
204 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
205     uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
206 static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
207 static void mmc_wakeup(struct mmc_request *req);
208
209 static void
210 mmc_ms_delay(int ms)
211 {
212
213         DELAY(1000 * ms);       /* XXX BAD */
214 }
215
216 static int
217 mmc_probe(device_t dev)
218 {
219
220         device_set_desc(dev, "MMC/SD bus");
221         return (0);
222 }
223
224 static int
225 mmc_attach(device_t dev)
226 {
227         struct mmc_softc *sc;
228
229         sc = device_get_softc(dev);
230         sc->dev = dev;
231         MMC_LOCK_INIT(sc);
232
233         /* We'll probe and attach our children later, but before / mount */
234         sc->config_intrhook.ich_func = mmc_delayed_attach;
235         sc->config_intrhook.ich_arg = sc;
236         if (config_intrhook_establish(&sc->config_intrhook) != 0)
237                 device_printf(dev, "config_intrhook_establish failed\n");
238         return (0);
239 }
240
241 static int
242 mmc_detach(device_t dev)
243 {
244         struct mmc_softc *sc = device_get_softc(dev);
245         int err;
246
247         if ((err = mmc_delete_cards(sc)) != 0)
248                 return (err);
249         mmc_power_down(sc);
250         MMC_LOCK_DESTROY(sc);
251
252         return (0);
253 }
254
255 static int
256 mmc_suspend(device_t dev)
257 {
258         struct mmc_softc *sc = device_get_softc(dev);
259         int err;
260
261         err = bus_generic_suspend(dev);
262         if (err)
263                 return (err);
264         mmc_power_down(sc);
265         return (0);
266 }
267
268 static int
269 mmc_resume(device_t dev)
270 {
271         struct mmc_softc *sc = device_get_softc(dev);
272
273         mmc_scan(sc);
274         return (bus_generic_resume(dev));
275 }
276
277 static int
278 mmc_acquire_bus(device_t busdev, device_t dev)
279 {
280         struct mmc_softc *sc;
281         struct mmc_ivars *ivar;
282         int err;
283         int rca;
284
285         err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
286         if (err)
287                 return (err);
288         sc = device_get_softc(busdev);
289         MMC_LOCK(sc);
290         if (sc->owner)
291                 panic("mmc: host bridge didn't serialize us.");
292         sc->owner = dev;
293         MMC_UNLOCK(sc);
294
295         if (busdev != dev) {
296                 /*
297                  * Keep track of the last rca that we've selected.  If
298                  * we're asked to do it again, don't.  We never
299                  * unselect unless the bus code itself wants the mmc
300                  * bus, and constantly reselecting causes problems.
301                  */
302                 rca = mmc_get_rca(dev);
303                 if (sc->last_rca != rca) {
304                         mmc_select_card(sc, rca);
305                         sc->last_rca = rca;
306                         /* Prepare bus width for the new card. */
307                         ivar = device_get_ivars(dev);
308                         if (bootverbose || mmc_debug) {
309                                 device_printf(busdev,
310                                     "setting bus width to %d bits\n",
311                                     (ivar->bus_width == bus_width_4) ? 4 :
312                                     (ivar->bus_width == bus_width_8) ? 8 : 1);
313                         }
314                         mmc_set_card_bus_width(sc, rca, ivar->bus_width);
315                         mmcbr_set_bus_width(busdev, ivar->bus_width);
316                         mmcbr_update_ios(busdev);
317                 }
318         } else {
319                 /*
320                  * If there's a card selected, stand down.
321                  */
322                 if (sc->last_rca != 0) {
323                         mmc_select_card(sc, 0);
324                         sc->last_rca = 0;
325                 }
326         }
327
328         return (0);
329 }
330
331 static int
332 mmc_release_bus(device_t busdev, device_t dev)
333 {
334         struct mmc_softc *sc;
335         int err;
336
337         sc = device_get_softc(busdev);
338
339         MMC_LOCK(sc);
340         if (!sc->owner)
341                 panic("mmc: releasing unowned bus.");
342         if (sc->owner != dev)
343                 panic("mmc: you don't own the bus.  game over.");
344         MMC_UNLOCK(sc);
345         err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
346         if (err)
347                 return (err);
348         MMC_LOCK(sc);
349         sc->owner = NULL;
350         MMC_UNLOCK(sc);
351         return (0);
352 }
353
354 static uint32_t
355 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
356 {
357
358         return (ocr & MMC_OCR_VOLTAGE);
359 }
360
361 static int
362 mmc_highest_voltage(uint32_t ocr)
363 {
364         int i;
365
366         for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
367             i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
368                 if (ocr & (1 << i))
369                         return (i);
370         return (-1);
371 }
372
373 static void
374 mmc_wakeup(struct mmc_request *req)
375 {
376         struct mmc_softc *sc;
377
378         sc = (struct mmc_softc *)req->done_data;
379         MMC_LOCK(sc);
380         req->flags |= MMC_REQ_DONE;
381         MMC_UNLOCK(sc);
382         wakeup(req);
383 }
384
385 static int
386 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
387 {
388
389         req->done = mmc_wakeup;
390         req->done_data = sc;
391         if (mmc_debug > 1) {
392                 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
393                     req->cmd->opcode, req->cmd->arg, req->cmd->flags);
394                 if (req->cmd->data) {
395                         printf(" data %d\n", (int)req->cmd->data->len);
396                 } else
397                         printf("\n");
398         }
399         MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
400         MMC_LOCK(sc);
401         while ((req->flags & MMC_REQ_DONE) == 0)
402                 msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
403         MMC_UNLOCK(sc);
404         if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
405                 device_printf(sc->dev, "CMD%d RESULT: %d\n",
406                     req->cmd->opcode, req->cmd->error);
407         return (0);
408 }
409
410 static int
411 mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
412 {
413         struct mmc_softc *sc = device_get_softc(brdev);
414
415         return (mmc_wait_for_req(sc, req));
416 }
417
418 static int
419 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
420 {
421         struct mmc_request mreq;
422         int err;
423
424         do {
425                 memset(&mreq, 0, sizeof(mreq));
426                 memset(cmd->resp, 0, sizeof(cmd->resp));
427                 cmd->retries = 0; /* Retries done here, not in hardware. */
428                 cmd->mrq = &mreq;
429                 mreq.cmd = cmd;
430                 if (mmc_wait_for_req(sc, &mreq) != 0)
431                         err = MMC_ERR_FAILED;
432                 else
433                         err = cmd->error;
434         } while (err != MMC_ERR_NONE && retries-- > 0);
435
436         if (err != MMC_ERR_NONE && sc->squelched == 0) {
437                 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
438                         device_printf(sc->dev, "CMD%d failed, RESULT: %d\n",
439                             cmd->opcode, err);
440                 }
441         }
442
443         return (err);
444 }
445
446 static int
447 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
448     struct mmc_command *cmd, int retries)
449 {
450         struct mmc_command appcmd;
451         int err;
452
453         /* Squelch error reporting at lower levels, we report below. */
454         sc->squelched++;
455         do {
456                 memset(&appcmd, 0, sizeof(appcmd));
457                 appcmd.opcode = MMC_APP_CMD;
458                 appcmd.arg = rca << 16;
459                 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
460                 appcmd.data = NULL;
461                 if (mmc_wait_for_cmd(sc, &appcmd, 0) != 0)
462                         err = MMC_ERR_FAILED;
463                 else
464                         err = appcmd.error;
465                 if (err == MMC_ERR_NONE) {
466                         if (!(appcmd.resp[0] & R1_APP_CMD))
467                                 err = MMC_ERR_FAILED;
468                         else if (mmc_wait_for_cmd(sc, cmd, 0) != 0)
469                                 err = MMC_ERR_FAILED;
470                         else
471                                 err = cmd->error;
472                 }
473         } while (err != MMC_ERR_NONE && retries-- > 0);
474         sc->squelched--;
475
476         if (err != MMC_ERR_NONE && sc->squelched == 0) {
477                 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
478                         device_printf(sc->dev, "ACMD%d failed, RESULT: %d\n",
479                             cmd->opcode, err);
480                 }
481         }
482
483         return (err);
484 }
485
486 static int
487 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
488     uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
489 {
490         struct mmc_command cmd;
491         int err;
492
493         memset(&cmd, 0, sizeof(cmd));
494         cmd.opcode = opcode;
495         cmd.arg = arg;
496         cmd.flags = flags;
497         cmd.data = NULL;
498         err = mmc_wait_for_cmd(sc, &cmd, retries);
499         if (err)
500                 return (err);
501         if (resp) {
502                 if (flags & MMC_RSP_136)
503                         memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
504                 else
505                         *resp = cmd.resp[0];
506         }
507         return (0);
508 }
509
510 static void
511 mmc_idle_cards(struct mmc_softc *sc)
512 {
513         device_t dev;
514         struct mmc_command cmd;
515
516         dev = sc->dev;
517         mmcbr_set_chip_select(dev, cs_high);
518         mmcbr_update_ios(dev);
519         mmc_ms_delay(1);
520
521         memset(&cmd, 0, sizeof(cmd));
522         cmd.opcode = MMC_GO_IDLE_STATE;
523         cmd.arg = 0;
524         cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
525         cmd.data = NULL;
526         mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
527         mmc_ms_delay(1);
528
529         mmcbr_set_chip_select(dev, cs_dontcare);
530         mmcbr_update_ios(dev);
531         mmc_ms_delay(1);
532 }
533
534 static int
535 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
536 {
537         struct mmc_command cmd;
538         int err = MMC_ERR_NONE, i;
539
540         memset(&cmd, 0, sizeof(cmd));
541         cmd.opcode = ACMD_SD_SEND_OP_COND;
542         cmd.arg = ocr;
543         cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
544         cmd.data = NULL;
545
546         for (i = 0; i < 1000; i++) {
547                 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
548                 if (err != MMC_ERR_NONE)
549                         break;
550                 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
551                     (ocr & MMC_OCR_VOLTAGE) == 0)
552                         break;
553                 err = MMC_ERR_TIMEOUT;
554                 mmc_ms_delay(10);
555         }
556         if (rocr && err == MMC_ERR_NONE)
557                 *rocr = cmd.resp[0];
558         return (err);
559 }
560
561 static int
562 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
563 {
564         struct mmc_command cmd;
565         int err = MMC_ERR_NONE, i;
566
567         memset(&cmd, 0, sizeof(cmd));
568         cmd.opcode = MMC_SEND_OP_COND;
569         cmd.arg = ocr;
570         cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
571         cmd.data = NULL;
572
573         for (i = 0; i < 1000; i++) {
574                 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
575                 if (err != MMC_ERR_NONE)
576                         break;
577                 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
578                     (ocr & MMC_OCR_VOLTAGE) == 0)
579                         break;
580                 err = MMC_ERR_TIMEOUT;
581                 mmc_ms_delay(10);
582         }
583         if (rocr && err == MMC_ERR_NONE)
584                 *rocr = cmd.resp[0];
585         return (err);
586 }
587
588 static int
589 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
590 {
591         struct mmc_command cmd;
592         int err;
593
594         memset(&cmd, 0, sizeof(cmd));
595         cmd.opcode = SD_SEND_IF_COND;
596         cmd.arg = (vhs << 8) + 0xAA;
597         cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
598         cmd.data = NULL;
599
600         err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
601         return (err);
602 }
603
604 static void
605 mmc_power_up(struct mmc_softc *sc)
606 {
607         device_t dev;
608
609         dev = sc->dev;
610         mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
611         mmcbr_set_bus_mode(dev, opendrain);
612         mmcbr_set_chip_select(dev, cs_dontcare);
613         mmcbr_set_bus_width(dev, bus_width_1);
614         mmcbr_set_power_mode(dev, power_up);
615         mmcbr_set_clock(dev, 0);
616         mmcbr_update_ios(dev);
617         mmc_ms_delay(1);
618
619         mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
620         mmcbr_set_timing(dev, bus_timing_normal);
621         mmcbr_set_power_mode(dev, power_on);
622         mmcbr_update_ios(dev);
623         mmc_ms_delay(2);
624 }
625
626 static void
627 mmc_power_down(struct mmc_softc *sc)
628 {
629         device_t dev = sc->dev;
630
631         mmcbr_set_bus_mode(dev, opendrain);
632         mmcbr_set_chip_select(dev, cs_dontcare);
633         mmcbr_set_bus_width(dev, bus_width_1);
634         mmcbr_set_power_mode(dev, power_off);
635         mmcbr_set_clock(dev, 0);
636         mmcbr_set_timing(dev, bus_timing_normal);
637         mmcbr_update_ios(dev);
638 }
639
640 static int
641 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
642 {
643         int flags;
644
645         flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
646         return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
647             flags, NULL, CMD_RETRIES));
648 }
649
650 static int
651 mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
652 {
653         struct mmc_command cmd;
654         int err;
655
656         memset(&cmd, 0, sizeof(cmd));
657         cmd.opcode = MMC_SWITCH_FUNC;
658         cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
659             (index << 16) |
660             (value << 8) |
661             set;
662         cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
663         cmd.data = NULL;
664         err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
665         return (err);
666 }
667
668 static int
669 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
670     uint8_t *res)
671 {
672         int err;
673         struct mmc_command cmd;
674         struct mmc_data data;
675
676         memset(&cmd, 0, sizeof(cmd));
677         memset(&data, 0, sizeof(data));
678         memset(res, 0, 64);
679
680         cmd.opcode = SD_SWITCH_FUNC;
681         cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
682         cmd.arg = mode << 31;                   /* 0 - check, 1 - set */
683         cmd.arg |= 0x00FFFFFF;
684         cmd.arg &= ~(0xF << (grp * 4));
685         cmd.arg |= value << (grp * 4);
686         cmd.data = &data;
687
688         data.data = res;
689         data.len = 64;
690         data.flags = MMC_DATA_READ;
691
692         err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
693         return (err);
694 }
695
696 static int
697 mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
698 {
699         struct mmc_command cmd;
700         int err;
701         uint8_t value;
702
703         if (mmcbr_get_mode(sc->dev) == mode_sd) {
704                 memset(&cmd, 0, sizeof(cmd));
705                 cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
706                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
707                 cmd.arg = SD_CLR_CARD_DETECT;
708                 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
709                 if (err != 0)
710                         return (err);
711                 memset(&cmd, 0, sizeof(cmd));
712                 cmd.opcode = ACMD_SET_BUS_WIDTH;
713                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
714                 switch (width) {
715                 case bus_width_1:
716                         cmd.arg = SD_BUS_WIDTH_1;
717                         break;
718                 case bus_width_4:
719                         cmd.arg = SD_BUS_WIDTH_4;
720                         break;
721                 default:
722                         return (MMC_ERR_INVALID);
723                 }
724                 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
725         } else {
726                 switch (width) {
727                 case bus_width_1:
728                         value = EXT_CSD_BUS_WIDTH_1;
729                         break;
730                 case bus_width_4:
731                         value = EXT_CSD_BUS_WIDTH_4;
732                         break;
733                 case bus_width_8:
734                         value = EXT_CSD_BUS_WIDTH_8;
735                         break;
736                 default:
737                         return (MMC_ERR_INVALID);
738                 }
739                 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
740                     value);
741         }
742         return (err);
743 }
744
745 static int
746 mmc_set_timing(struct mmc_softc *sc, int timing)
747 {
748         int err;
749         uint8_t value;
750         u_char switch_res[64];
751
752         switch (timing) {
753         case bus_timing_normal:
754                 value = 0;
755                 break;
756         case bus_timing_hs:
757                 value = 1;
758                 break;
759         default:
760                 return (MMC_ERR_INVALID);
761         }
762         if (mmcbr_get_mode(sc->dev) == mode_sd)
763                 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
764                     value, switch_res);
765         else
766                 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
767                     EXT_CSD_HS_TIMING, value);
768         return (err);
769 }
770
771 static int
772 mmc_test_bus_width(struct mmc_softc *sc)
773 {
774         struct mmc_command cmd;
775         struct mmc_data data;
776         int err;
777         uint8_t buf[8];
778         uint8_t p8[8] =   { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
779         uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
780         uint8_t p4[4] =   { 0x5A, 0x00, 0x00, 0x00, };
781         uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
782
783         if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
784                 mmcbr_set_bus_width(sc->dev, bus_width_8);
785                 mmcbr_update_ios(sc->dev);
786
787                 sc->squelched++; /* Errors are expected, squelch reporting. */
788                 memset(&cmd, 0, sizeof(cmd));
789                 memset(&data, 0, sizeof(data));
790                 cmd.opcode = MMC_BUSTEST_W;
791                 cmd.arg = 0;
792                 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
793                 cmd.data = &data;
794
795                 data.data = p8;
796                 data.len = 8;
797                 data.flags = MMC_DATA_WRITE;
798                 mmc_wait_for_cmd(sc, &cmd, 0);
799
800                 memset(&cmd, 0, sizeof(cmd));
801                 memset(&data, 0, sizeof(data));
802                 cmd.opcode = MMC_BUSTEST_R;
803                 cmd.arg = 0;
804                 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
805                 cmd.data = &data;
806
807                 data.data = buf;
808                 data.len = 8;
809                 data.flags = MMC_DATA_READ;
810                 err = mmc_wait_for_cmd(sc, &cmd, 0);
811                 sc->squelched--;
812
813                 mmcbr_set_bus_width(sc->dev, bus_width_1);
814                 mmcbr_update_ios(sc->dev);
815
816                 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
817                         return (bus_width_8);
818         }
819
820         if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
821                 mmcbr_set_bus_width(sc->dev, bus_width_4);
822                 mmcbr_update_ios(sc->dev);
823
824                 sc->squelched++; /* Errors are expected, squelch reporting. */
825                 memset(&cmd, 0, sizeof(cmd));
826                 memset(&data, 0, sizeof(data));
827                 cmd.opcode = MMC_BUSTEST_W;
828                 cmd.arg = 0;
829                 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
830                 cmd.data = &data;
831
832                 data.data = p4;
833                 data.len = 4;
834                 data.flags = MMC_DATA_WRITE;
835                 mmc_wait_for_cmd(sc, &cmd, 0);
836
837                 memset(&cmd, 0, sizeof(cmd));
838                 memset(&data, 0, sizeof(data));
839                 cmd.opcode = MMC_BUSTEST_R;
840                 cmd.arg = 0;
841                 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
842                 cmd.data = &data;
843
844                 data.data = buf;
845                 data.len = 4;
846                 data.flags = MMC_DATA_READ;
847                 err = mmc_wait_for_cmd(sc, &cmd, 0);
848                 sc->squelched--;
849
850                 mmcbr_set_bus_width(sc->dev, bus_width_1);
851                 mmcbr_update_ios(sc->dev);
852
853                 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
854                         return (bus_width_4);
855         }
856         return (bus_width_1);
857 }
858
859 static uint32_t
860 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
861 {
862         const int i = (bit_len / 32) - (start / 32) - 1;
863         const int shift = start & 31;
864         uint32_t retval = bits[i] >> shift;
865         if (size + shift > 32)
866                 retval |= bits[i - 1] << (32 - shift);
867         return (retval & ((1llu << size) - 1));
868 }
869
870 static void
871 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
872 {
873         int i;
874
875         /* There's no version info, so we take it on faith */
876         memset(cid, 0, sizeof(*cid));
877         cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
878         cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
879         for (i = 0; i < 5; i++)
880                 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
881         cid->pnm[5] = 0;
882         cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
883         cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
884         cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
885         cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
886 }
887
888 static void
889 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
890 {
891         int i;
892
893         /* There's no version info, so we take it on faith */
894         memset(cid, 0, sizeof(*cid));
895         cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
896         cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
897         for (i = 0; i < 6; i++)
898                 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
899         cid->pnm[6] = 0;
900         cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
901         cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
902         cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
903         cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
904 }
905
906 static void
907 mmc_format_card_id_string(struct mmc_ivars *ivar)
908 {
909         char oidstr[8];
910         uint8_t c1;
911         uint8_t c2;
912
913         /*
914          * Format a card ID string for use by the mmcsd driver, it's what
915          * appears between the <> in the following:
916          * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
917          * 22.5MHz/4bit/128-block
918          *
919          * Also format just the card serial number, which the mmcsd driver will
920          * use as the disk->d_ident string.
921          *
922          * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
923          * and our max formatted length is currently 55 bytes if every field
924          * contains the largest value.
925          *
926          * Sometimes the oid is two printable ascii chars; when it's not,
927          * format it as 0xnnnn instead.
928          */
929         c1 = (ivar->cid.oid >> 8) & 0x0ff;
930         c2 = ivar->cid.oid & 0x0ff;
931         if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
932                 snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
933         else
934                 snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
935         snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string),
936             "%08X", ivar->cid.psn);
937         snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
938             "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
939             ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
940             ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
941             ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
942             ivar->cid.mid, oidstr);
943 }
944
945 static const int exp[8] = {
946         1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
947 };
948
949 static const int mant[16] = {
950         0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
951 };
952
953 static const int cur_min[8] = {
954         500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
955 };
956
957 static const int cur_max[8] = {
958         1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
959 };
960
961 static void
962 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
963 {
964         int v;
965         int m;
966         int e;
967
968         memset(csd, 0, sizeof(*csd));
969         csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
970         if (v == 0) {
971                 m = mmc_get_bits(raw_csd, 128, 115, 4);
972                 e = mmc_get_bits(raw_csd, 128, 112, 3);
973                 csd->tacc = (exp[e] * mant[m] + 9) / 10;
974                 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
975                 m = mmc_get_bits(raw_csd, 128, 99, 4);
976                 e = mmc_get_bits(raw_csd, 128, 96, 3);
977                 csd->tran_speed = exp[e] * 10000 * mant[m];
978                 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
979                 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
980                 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
981                 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
982                 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
983                 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
984                 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
985                 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
986                 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
987                 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
988                 m = mmc_get_bits(raw_csd, 128, 62, 12);
989                 e = mmc_get_bits(raw_csd, 128, 47, 3);
990                 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
991                 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
992                 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
993                 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
994                 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
995                 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
996                 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
997                 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
998         } else if (v == 1) {
999                 m = mmc_get_bits(raw_csd, 128, 115, 4);
1000                 e = mmc_get_bits(raw_csd, 128, 112, 3);
1001                 csd->tacc = (exp[e] * mant[m] + 9) / 10;
1002                 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1003                 m = mmc_get_bits(raw_csd, 128, 99, 4);
1004                 e = mmc_get_bits(raw_csd, 128, 96, 3);
1005                 csd->tran_speed = exp[e] * 10000 * mant[m];
1006                 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1007                 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1008                 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1009                 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1010                 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1011                 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1012                 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
1013                     512 * 1024;
1014                 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1015                 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1016                 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1017                 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1018                 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1019                 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1020                 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1021         } else
1022                 panic("unknown SD CSD version");
1023 }
1024
1025 static void
1026 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
1027 {
1028         int m;
1029         int e;
1030
1031         memset(csd, 0, sizeof(*csd));
1032         csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
1033         csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
1034         m = mmc_get_bits(raw_csd, 128, 115, 4);
1035         e = mmc_get_bits(raw_csd, 128, 112, 3);
1036         csd->tacc = exp[e] * mant[m] + 9 / 10;
1037         csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1038         m = mmc_get_bits(raw_csd, 128, 99, 4);
1039         e = mmc_get_bits(raw_csd, 128, 96, 3);
1040         csd->tran_speed = exp[e] * 10000 * mant[m];
1041         csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1042         csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1043         csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1044         csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1045         csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1046         csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1047         csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1048         csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1049         csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1050         csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1051         m = mmc_get_bits(raw_csd, 128, 62, 12);
1052         e = mmc_get_bits(raw_csd, 128, 47, 3);
1053         csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1054         csd->erase_blk_en = 0;
1055         csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
1056             (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1057         csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1058         csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1059         csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1060         csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1061         csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1062 }
1063
1064 static void
1065 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1066 {
1067         unsigned int scr_struct;
1068
1069         memset(scr, 0, sizeof(*scr));
1070
1071         scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1072         if (scr_struct != 0) {
1073                 printf("Unrecognised SCR structure version %d\n",
1074                     scr_struct);
1075                 return;
1076         }
1077         scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1078         scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1079 }
1080
1081 static void
1082 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1083     struct mmc_sd_status *sd_status)
1084 {
1085
1086         memset(sd_status, 0, sizeof(*sd_status));
1087
1088         sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1089         sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1090         sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1091         sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1092         sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1093         sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1094         sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1095         sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1096         sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1097         sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1098 }
1099
1100 static int
1101 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1102 {
1103         struct mmc_command cmd;
1104         int err;
1105
1106         memset(&cmd, 0, sizeof(cmd));
1107         cmd.opcode = MMC_ALL_SEND_CID;
1108         cmd.arg = 0;
1109         cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1110         cmd.data = NULL;
1111         err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1112         memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1113         return (err);
1114 }
1115
1116 static int
1117 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1118 {
1119         struct mmc_command cmd;
1120         int err;
1121
1122         memset(&cmd, 0, sizeof(cmd));
1123         cmd.opcode = MMC_SEND_CSD;
1124         cmd.arg = rca << 16;
1125         cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1126         cmd.data = NULL;
1127         err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1128         memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1129         return (err);
1130 }
1131
1132 static int
1133 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1134 {
1135         int err;
1136         struct mmc_command cmd;
1137         struct mmc_data data;
1138
1139         memset(&cmd, 0, sizeof(cmd));
1140         memset(&data, 0, sizeof(data));
1141
1142         memset(rawscr, 0, 8);
1143         cmd.opcode = ACMD_SEND_SCR;
1144         cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1145         cmd.arg = 0;
1146         cmd.data = &data;
1147
1148         data.data = rawscr;
1149         data.len = 8;
1150         data.flags = MMC_DATA_READ;
1151
1152         err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1153         rawscr[0] = be32toh(rawscr[0]);
1154         rawscr[1] = be32toh(rawscr[1]);
1155         return (err);
1156 }
1157
1158 static int
1159 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1160 {
1161         int err;
1162         struct mmc_command cmd;
1163         struct mmc_data data;
1164
1165         memset(&cmd, 0, sizeof(cmd));
1166         memset(&data, 0, sizeof(data));
1167
1168         memset(rawextcsd, 0, 512);
1169         cmd.opcode = MMC_SEND_EXT_CSD;
1170         cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1171         cmd.arg = 0;
1172         cmd.data = &data;
1173
1174         data.data = rawextcsd;
1175         data.len = 512;
1176         data.flags = MMC_DATA_READ;
1177
1178         err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1179         return (err);
1180 }
1181
1182 static int
1183 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1184 {
1185         int err, i;
1186         struct mmc_command cmd;
1187         struct mmc_data data;
1188
1189         memset(&cmd, 0, sizeof(cmd));
1190         memset(&data, 0, sizeof(data));
1191
1192         memset(rawsdstatus, 0, 64);
1193         cmd.opcode = ACMD_SD_STATUS;
1194         cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1195         cmd.arg = 0;
1196         cmd.data = &data;
1197
1198         data.data = rawsdstatus;
1199         data.len = 64;
1200         data.flags = MMC_DATA_READ;
1201
1202         err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1203         for (i = 0; i < 16; i++)
1204             rawsdstatus[i] = be32toh(rawsdstatus[i]);
1205         return (err);
1206 }
1207
1208 static int
1209 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1210 {
1211         struct mmc_command cmd;
1212         int err;
1213
1214         memset(&cmd, 0, sizeof(cmd));
1215         cmd.opcode = MMC_SET_RELATIVE_ADDR;
1216         cmd.arg = resp << 16;
1217         cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1218         cmd.data = NULL;
1219         err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1220         return (err);
1221 }
1222
1223 static int
1224 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1225 {
1226         struct mmc_command cmd;
1227         int err;
1228
1229         memset(&cmd, 0, sizeof(cmd));
1230         cmd.opcode = SD_SEND_RELATIVE_ADDR;
1231         cmd.arg = 0;
1232         cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1233         cmd.data = NULL;
1234         err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1235         *resp = cmd.resp[0];
1236         return (err);
1237 }
1238
1239 static int
1240 mmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status)
1241 {
1242         struct mmc_command cmd;
1243         int err;
1244
1245         memset(&cmd, 0, sizeof(cmd));
1246         cmd.opcode = MMC_SEND_STATUS;
1247         cmd.arg = rca << 16;
1248         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1249         cmd.data = NULL;
1250         err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1251         *status = cmd.resp[0];
1252         return (err);
1253 }
1254
1255 static int
1256 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1257 {
1258         struct mmc_command cmd;
1259         int err;
1260
1261         memset(&cmd, 0, sizeof(cmd));
1262         cmd.opcode = MMC_SET_BLOCKLEN;
1263         cmd.arg = len;
1264         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1265         cmd.data = NULL;
1266         err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1267         return (err);
1268 }
1269
1270 static void
1271 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1272 {
1273         device_printf(dev, "Card at relative address 0x%04x%s:\n",
1274             ivar->rca, newcard ? " added" : "");
1275         device_printf(dev, " card: %s\n", ivar->card_id_string);
1276         device_printf(dev, " bus: %ubit, %uMHz%s\n",
1277             (ivar->bus_width == bus_width_1 ? 1 :
1278             (ivar->bus_width == bus_width_4 ? 4 : 8)),
1279             (ivar->timing == bus_timing_hs ?
1280                 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1281             ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1282         device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1283             ivar->sec_count, ivar->erase_sector,
1284             ivar->read_only ? ", read-only" : "");
1285 }
1286
1287 static void
1288 mmc_discover_cards(struct mmc_softc *sc)
1289 {
1290         struct mmc_ivars *ivar = NULL;
1291         device_t *devlist;
1292         int err, i, devcount, newcard;
1293         uint32_t raw_cid[4], resp, sec_count, status;
1294         device_t child;
1295         uint16_t rca = 2;
1296         u_char switch_res[64];
1297
1298         if (bootverbose || mmc_debug)
1299                 device_printf(sc->dev, "Probing cards\n");
1300         while (1) {
1301                 sc->squelched++; /* Errors are expected, squelch reporting. */
1302                 err = mmc_all_send_cid(sc, raw_cid);
1303                 sc->squelched--;
1304                 if (err == MMC_ERR_TIMEOUT)
1305                         break;
1306                 if (err != MMC_ERR_NONE) {
1307                         device_printf(sc->dev, "Error reading CID %d\n", err);
1308                         break;
1309                 }
1310                 newcard = 1;
1311                 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1312                         return;
1313                 for (i = 0; i < devcount; i++) {
1314                         ivar = device_get_ivars(devlist[i]);
1315                         if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) {
1316                                 newcard = 0;
1317                                 break;
1318                         }
1319                 }
1320                 free(devlist, M_TEMP);
1321                 if (bootverbose || mmc_debug) {
1322                         device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n",
1323                             newcard ? "New c" : "C",
1324                             raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1325                 }
1326                 if (newcard) {
1327                         ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1328                             M_WAITOK | M_ZERO);
1329                         memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1330                 }
1331                 if (mmcbr_get_ro(sc->dev))
1332                         ivar->read_only = 1;
1333                 ivar->bus_width = bus_width_1;
1334                 ivar->timing = bus_timing_normal;
1335                 ivar->mode = mmcbr_get_mode(sc->dev);
1336                 if (ivar->mode == mode_sd) {
1337                         mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1338                         mmc_send_relative_addr(sc, &resp);
1339                         ivar->rca = resp >> 16;
1340                         /* Get card CSD. */
1341                         mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1342                         if (bootverbose || mmc_debug)
1343                                 device_printf(sc->dev,
1344                                     "%sard detected (CSD %08x%08x%08x%08x)\n",
1345                                     newcard ? "New c" : "C", ivar->raw_csd[0],
1346                                     ivar->raw_csd[1], ivar->raw_csd[2],
1347                                     ivar->raw_csd[3]);
1348                         mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1349                         ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1350                         if (ivar->csd.csd_structure > 0)
1351                                 ivar->high_cap = 1;
1352                         ivar->tran_speed = ivar->csd.tran_speed;
1353                         ivar->erase_sector = ivar->csd.erase_sector *
1354                             ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1355
1356                         err = mmc_send_status(sc, ivar->rca, &status);
1357                         if (err != MMC_ERR_NONE) {
1358                                 device_printf(sc->dev,
1359                                     "Error reading card status %d\n", err);
1360                                 break;
1361                         }
1362                         if ((status & R1_CARD_IS_LOCKED) != 0) {
1363                                 device_printf(sc->dev,
1364                                     "Card is password protected, skipping.\n");
1365                                 break;
1366                         }
1367
1368                         /* Get card SCR. Card must be selected to fetch it. */
1369                         mmc_select_card(sc, ivar->rca);
1370                         mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1371                         mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1372                         /* Get card switch capabilities (command class 10). */
1373                         if ((ivar->scr.sda_vsn >= 1) &&
1374                             (ivar->csd.ccc & (1<<10))) {
1375                                 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1376                                     SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1377                                     switch_res);
1378                                 if (switch_res[13] & 2) {
1379                                         ivar->timing = bus_timing_hs;
1380                                         ivar->hs_tran_speed = SD_MAX_HS;
1381                                 }
1382                         }
1383
1384                         /*
1385                          * We deselect then reselect the card here.  Some cards
1386                          * become unselected and timeout with the above two
1387                          * commands, although the state tables / diagrams in the
1388                          * standard suggest they go back to the transfer state.
1389                          * Other cards don't become deselected, and if we
1390                          * atttempt to blindly re-select them, we get timeout
1391                          * errors from some controllers.  So we deselect then
1392                          * reselect to handle all situations.  The only thing we
1393                          * use from the sd_status is the erase sector size, but
1394                          * it is still nice to get that right.
1395                          */
1396                         mmc_select_card(sc, 0);
1397                         mmc_select_card(sc, ivar->rca);
1398                         mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1399                         mmc_app_decode_sd_status(ivar->raw_sd_status,
1400                             &ivar->sd_status);
1401                         if (ivar->sd_status.au_size != 0) {
1402                                 ivar->erase_sector =
1403                                     16 << ivar->sd_status.au_size;
1404                         }
1405                         /* Find max supported bus width. */
1406                         if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1407                             (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1408                                 ivar->bus_width = bus_width_4;
1409
1410                         /*
1411                          * Some cards that report maximum I/O block sizes
1412                          * greater than 512 require the block length to be
1413                          * set to 512, even though that is supposed to be
1414                          * the default.  Example:
1415                          *
1416                          * Transcend 2GB SDSC card, CID:
1417                          * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1418                          */
1419                         if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1420                             ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1421                                 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1422
1423                         mmc_format_card_id_string(ivar);
1424
1425                         if (bootverbose || mmc_debug)
1426                                 mmc_log_card(sc->dev, ivar, newcard);
1427                         if (newcard) {
1428                                 /* Add device. */
1429                                 child = device_add_child(sc->dev, NULL, -1);
1430                                 device_set_ivars(child, ivar);
1431                         }
1432                         mmc_select_card(sc, 0);
1433                         return;
1434                 }
1435                 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1436                 ivar->rca = rca++;
1437                 mmc_set_relative_addr(sc, ivar->rca);
1438                 /* Get card CSD. */
1439                 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1440                 if (bootverbose || mmc_debug)
1441                         device_printf(sc->dev,
1442                             "%sard detected (CSD %08x%08x%08x%08x)\n",
1443                             newcard ? "New c" : "C", ivar->raw_csd[0],
1444                             ivar->raw_csd[1], ivar->raw_csd[2],
1445                             ivar->raw_csd[3]);
1446
1447                 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1448                 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1449                 ivar->tran_speed = ivar->csd.tran_speed;
1450                 ivar->erase_sector = ivar->csd.erase_sector *
1451                     ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1452
1453                 err = mmc_send_status(sc, ivar->rca, &status);
1454                 if (err != MMC_ERR_NONE) {
1455                         device_printf(sc->dev,
1456                             "Error reading card status %d\n", err);
1457                         break;
1458                 }
1459                 if ((status & R1_CARD_IS_LOCKED) != 0) {
1460                         device_printf(sc->dev,
1461                             "Card is password protected, skipping.\n");
1462                         break;
1463                 }
1464
1465                 mmc_select_card(sc, ivar->rca);
1466
1467                 /* Only MMC >= 4.x cards support EXT_CSD. */
1468                 if (ivar->csd.spec_vers >= 4) {
1469                         mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1470                         /* Handle extended capacity from EXT_CSD */
1471                         sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1472                             (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1473                             (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1474                             (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1475                         if (sec_count != 0) {
1476                                 ivar->sec_count = sec_count;
1477                                 ivar->high_cap = 1;
1478                         }
1479                         /* Get card speed in high speed mode. */
1480                         ivar->timing = bus_timing_hs;
1481                         if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1482                             & EXT_CSD_CARD_TYPE_52)
1483                                 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1484                         else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1485                             & EXT_CSD_CARD_TYPE_26)
1486                                 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1487                         else
1488                                 ivar->hs_tran_speed = ivar->tran_speed;
1489                         /* Find max supported bus width. */
1490                         ivar->bus_width = mmc_test_bus_width(sc);
1491                         /* Handle HC erase sector size. */
1492                         if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1493                                 ivar->erase_sector = 1024 *
1494                                     ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1495                                 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1496                                     EXT_CSD_ERASE_GRP_DEF, 1);
1497                         }
1498                 } else {
1499                         ivar->bus_width = bus_width_1;
1500                         ivar->timing = bus_timing_normal;
1501                 }
1502
1503                 /*
1504                  * Some cards that report maximum I/O block sizes greater
1505                  * than 512 require the block length to be set to 512, even
1506                  * though that is supposed to be the default.  Example:
1507                  *
1508                  * Transcend 2GB SDSC card, CID:
1509                  * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1510                  */
1511                 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1512                     ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1513                         mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1514
1515                 mmc_format_card_id_string(ivar);
1516
1517                 if (bootverbose || mmc_debug)
1518                         mmc_log_card(sc->dev, ivar, newcard);
1519                 if (newcard) {
1520                         /* Add device. */
1521                         child = device_add_child(sc->dev, NULL, -1);
1522                         device_set_ivars(child, ivar);
1523                 }
1524                 mmc_select_card(sc, 0);
1525         }
1526 }
1527
1528 static void
1529 mmc_rescan_cards(struct mmc_softc *sc)
1530 {
1531         struct mmc_ivars *ivar = NULL;
1532         device_t *devlist;
1533         int err, i, devcount;
1534
1535         if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1536                 return;
1537         for (i = 0; i < devcount; i++) {
1538                 ivar = device_get_ivars(devlist[i]);
1539                 if (mmc_select_card(sc, ivar->rca)) {
1540                         if (bootverbose || mmc_debug)
1541                                 device_printf(sc->dev, "Card at relative address %d lost.\n",
1542                                     ivar->rca);
1543                         device_delete_child(sc->dev, devlist[i]);
1544                         free(ivar, M_DEVBUF);
1545                 }
1546         }
1547         free(devlist, M_TEMP);
1548         mmc_select_card(sc, 0);
1549 }
1550
1551 static int
1552 mmc_delete_cards(struct mmc_softc *sc)
1553 {
1554         struct mmc_ivars *ivar;
1555         device_t *devlist;
1556         int err, i, devcount;
1557
1558         if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1559                 return (err);
1560         for (i = 0; i < devcount; i++) {
1561                 ivar = device_get_ivars(devlist[i]);
1562                 if (bootverbose || mmc_debug)
1563                         device_printf(sc->dev, "Card at relative address %d deleted.\n",
1564                             ivar->rca);
1565                 device_delete_child(sc->dev, devlist[i]);
1566                 free(ivar, M_DEVBUF);
1567         }
1568         free(devlist, M_TEMP);
1569         return (0);
1570 }
1571
1572 static void
1573 mmc_go_discovery(struct mmc_softc *sc)
1574 {
1575         uint32_t ocr;
1576         device_t dev;
1577         int err;
1578
1579         dev = sc->dev;
1580         if (mmcbr_get_power_mode(dev) != power_on) {
1581                 /*
1582                  * First, try SD modes
1583                  */
1584                 sc->squelched++; /* Errors are expected, squelch reporting. */
1585                 mmcbr_set_mode(dev, mode_sd);
1586                 mmc_power_up(sc);
1587                 mmcbr_set_bus_mode(dev, pushpull);
1588                 if (bootverbose || mmc_debug)
1589                         device_printf(sc->dev, "Probing bus\n");
1590                 mmc_idle_cards(sc);
1591                 err = mmc_send_if_cond(sc, 1);
1592                 if ((bootverbose || mmc_debug) && err == 0)
1593                         device_printf(sc->dev, "SD 2.0 interface conditions: OK\n");
1594                 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1595                         if (bootverbose || mmc_debug)
1596                                 device_printf(sc->dev, "SD probe: failed\n");
1597                         /*
1598                          * Failed, try MMC
1599                          */
1600                         mmcbr_set_mode(dev, mode_mmc);
1601                         if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1602                                 if (bootverbose || mmc_debug)
1603                                         device_printf(sc->dev, "MMC probe: failed\n");
1604                                 ocr = 0; /* Failed both, powerdown. */
1605                         } else if (bootverbose || mmc_debug)
1606                                 device_printf(sc->dev,
1607                                     "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1608                 } else if (bootverbose || mmc_debug)
1609                         device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr);
1610                 sc->squelched--;
1611
1612                 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1613                 if (mmcbr_get_ocr(dev) != 0)
1614                         mmc_idle_cards(sc);
1615         } else {
1616                 mmcbr_set_bus_mode(dev, opendrain);
1617                 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
1618                 mmcbr_update_ios(dev);
1619                 /* XXX recompute vdd based on new cards? */
1620         }
1621         /*
1622          * Make sure that we have a mutually agreeable voltage to at least
1623          * one card on the bus.
1624          */
1625         if (bootverbose || mmc_debug)
1626                 device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev));
1627         if (mmcbr_get_ocr(dev) == 0) {
1628                 device_printf(sc->dev, "No compatible cards found on bus\n");
1629                 mmc_delete_cards(sc);
1630                 mmc_power_down(sc);
1631                 return;
1632         }
1633         /*
1634          * Reselect the cards after we've idled them above.
1635          */
1636         if (mmcbr_get_mode(dev) == mode_sd) {
1637                 err = mmc_send_if_cond(sc, 1);
1638                 mmc_send_app_op_cond(sc,
1639                     (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1640         } else
1641                 mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL);
1642         mmc_discover_cards(sc);
1643         mmc_rescan_cards(sc);
1644
1645         mmcbr_set_bus_mode(dev, pushpull);
1646         mmcbr_update_ios(dev);
1647         mmc_calculate_clock(sc);
1648         bus_generic_attach(dev);
1649 /*      mmc_update_children_sysctl(dev);*/
1650 }
1651
1652 static int
1653 mmc_calculate_clock(struct mmc_softc *sc)
1654 {
1655         int max_dtr, max_hs_dtr, max_timing;
1656         int nkid, i, f_max;
1657         device_t *kids;
1658         struct mmc_ivars *ivar;
1659
1660         f_max = mmcbr_get_f_max(sc->dev);
1661         max_dtr = max_hs_dtr = f_max;
1662         if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1663                 max_timing = bus_timing_hs;
1664         else
1665                 max_timing = bus_timing_normal;
1666         if (device_get_children(sc->dev, &kids, &nkid) != 0)
1667                 panic("can't get children");
1668         for (i = 0; i < nkid; i++) {
1669                 ivar = device_get_ivars(kids[i]);
1670                 if (ivar->timing < max_timing)
1671                         max_timing = ivar->timing;
1672                 if (ivar->tran_speed < max_dtr)
1673                         max_dtr = ivar->tran_speed;
1674                 if (ivar->hs_tran_speed < max_hs_dtr)
1675                         max_hs_dtr = ivar->hs_tran_speed;
1676         }
1677         for (i = 0; i < nkid; i++) {
1678                 ivar = device_get_ivars(kids[i]);
1679                 if (ivar->timing == bus_timing_normal)
1680                         continue;
1681                 mmc_select_card(sc, ivar->rca);
1682                 mmc_set_timing(sc, max_timing);
1683         }
1684         mmc_select_card(sc, 0);
1685         free(kids, M_TEMP);
1686         if (max_timing == bus_timing_hs)
1687                 max_dtr = max_hs_dtr;
1688         if (bootverbose || mmc_debug) {
1689                 device_printf(sc->dev,
1690                     "setting transfer rate to %d.%03dMHz%s\n",
1691                     max_dtr / 1000000, (max_dtr / 1000) % 1000,
1692                     max_timing == bus_timing_hs ? " (high speed timing)" : "");
1693         }
1694         mmcbr_set_timing(sc->dev, max_timing);
1695         mmcbr_set_clock(sc->dev, max_dtr);
1696         mmcbr_update_ios(sc->dev);
1697         return max_dtr;
1698 }
1699
1700 static void
1701 mmc_scan(struct mmc_softc *sc)
1702 {
1703         device_t dev = sc->dev;
1704
1705         mmc_acquire_bus(dev, dev);
1706         mmc_go_discovery(sc);
1707         mmc_release_bus(dev, dev);
1708 }
1709
1710 static int
1711 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1712 {
1713         struct mmc_ivars *ivar = device_get_ivars(child);
1714
1715         switch (which) {
1716         default:
1717                 return (EINVAL);
1718         case MMC_IVAR_DSR_IMP:
1719                 *result = ivar->csd.dsr_imp;
1720                 break;
1721         case MMC_IVAR_MEDIA_SIZE:
1722                 *result = ivar->sec_count;
1723                 break;
1724         case MMC_IVAR_RCA:
1725                 *result = ivar->rca;
1726                 break;
1727         case MMC_IVAR_SECTOR_SIZE:
1728                 *result = MMC_SECTOR_SIZE;
1729                 break;
1730         case MMC_IVAR_TRAN_SPEED:
1731                 *result = mmcbr_get_clock(bus);
1732                 break;
1733         case MMC_IVAR_READ_ONLY:
1734                 *result = ivar->read_only;
1735                 break;
1736         case MMC_IVAR_HIGH_CAP:
1737                 *result = ivar->high_cap;
1738                 break;
1739         case MMC_IVAR_CARD_TYPE:
1740                 *result = ivar->mode;
1741                 break;
1742         case MMC_IVAR_BUS_WIDTH:
1743                 *result = ivar->bus_width;
1744                 break;
1745         case MMC_IVAR_ERASE_SECTOR:
1746                 *result = ivar->erase_sector;
1747                 break;
1748         case MMC_IVAR_MAX_DATA:
1749                 *result = mmcbr_get_max_data(bus);
1750                 break;
1751         case MMC_IVAR_CARD_ID_STRING:
1752                 *(char **)result = ivar->card_id_string;
1753                 break;
1754         case MMC_IVAR_CARD_SN_STRING:
1755                 *(char **)result = ivar->card_sn_string;
1756                 break;
1757         }
1758         return (0);
1759 }
1760
1761 static int
1762 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1763 {
1764         /*
1765          * None are writable ATM
1766          */
1767         return (EINVAL);
1768 }
1769
1770 static void
1771 mmc_delayed_attach(void *xsc)
1772 {
1773         struct mmc_softc *sc = xsc;
1774
1775         mmc_scan(sc);
1776         config_intrhook_disestablish(&sc->config_intrhook);
1777 }
1778
1779 static int
1780 mmc_child_location_str(device_t dev, device_t child, char *buf,
1781     size_t buflen)
1782 {
1783
1784         snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1785         return (0);
1786 }
1787
1788 static device_method_t mmc_methods[] = {
1789         /* device_if */
1790         DEVMETHOD(device_probe, mmc_probe),
1791         DEVMETHOD(device_attach, mmc_attach),
1792         DEVMETHOD(device_detach, mmc_detach),
1793         DEVMETHOD(device_suspend, mmc_suspend),
1794         DEVMETHOD(device_resume, mmc_resume),
1795
1796         /* Bus interface */
1797         DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1798         DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1799         DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1800
1801         /* MMC Bus interface */
1802         DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1803         DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1804         DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1805
1806         DEVMETHOD_END
1807 };
1808
1809 static driver_t mmc_driver = {
1810         "mmc",
1811         mmc_methods,
1812         sizeof(struct mmc_softc),
1813 };
1814 static devclass_t mmc_devclass;
1815
1816 DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL);
1817 DRIVER_MODULE(mmc, sdhci_bcm, mmc_driver, mmc_devclass, NULL, NULL);
1818 DRIVER_MODULE(mmc, sdhci_fdt, mmc_driver, mmc_devclass, NULL, NULL);
1819 DRIVER_MODULE(mmc, sdhci_imx, mmc_driver, mmc_devclass, NULL, NULL);
1820 DRIVER_MODULE(mmc, sdhci_pci, mmc_driver, mmc_devclass, NULL, NULL);
1821 DRIVER_MODULE(mmc, sdhci_ti, mmc_driver, mmc_devclass, NULL, NULL);
1822 DRIVER_MODULE(mmc, ti_mmchs, mmc_driver, mmc_devclass, NULL, NULL);
1823