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