The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/mmc/mmc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: a8c154525655e6960b8c8db58c24a588


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.