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