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