1163516Simp/*- 2163516Simp * Copyright (c) 2006 Bernd Walter. All rights reserved. 3163516Simp * Copyright (c) 2006 M. Warner Losh. All rights reserved. 4163516Simp * 5163516Simp * Redistribution and use in source and binary forms, with or without 6163516Simp * modification, are permitted provided that the following conditions 7163516Simp * are met: 8163516Simp * 1. Redistributions of source code must retain the above copyright 9163516Simp * notice, this list of conditions and the following disclaimer. 10163516Simp * 2. Redistributions in binary form must reproduce the above copyright 11163516Simp * notice, this list of conditions and the following disclaimer in the 12163516Simp * documentation and/or other materials provided with the distribution. 13163516Simp * 14163516Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15163516Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16163516Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17163516Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18163516Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19163516Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20163516Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21163516Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22163516Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23163516Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24170002Simp * 25170002Simp * Portions of this software may have been developed with reference to 26170002Simp * the SD Simplified Specification. The following disclaimer may apply: 27170002Simp * 28170002Simp * The following conditions apply to the release of the simplified 29170002Simp * specification ("Simplified Specification") by the SD Card Association and 30170002Simp * the SD Group. The Simplified Specification is a subset of the complete SD 31170002Simp * Specification which is owned by the SD Card Association and the SD 32170002Simp * Group. This Simplified Specification is provided on a non-confidential 33170002Simp * basis subject to the disclaimers below. Any implementation of the 34170002Simp * Simplified Specification may require a license from the SD Card 35170002Simp * Association, SD Group, SD-3C LLC or other third parties. 36170002Simp * 37170002Simp * Disclaimers: 38170002Simp * 39170002Simp * The information contained in the Simplified Specification is presented only 40170002Simp * as a standard specification for SD Cards and SD Host/Ancillary products and 41170002Simp * is provided "AS-IS" without any representations or warranties of any 42170002Simp * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 43170002Simp * Card Association for any damages, any infringements of patents or other 44170002Simp * right of the SD Group, SD-3C LLC, the SD Card Association or any third 45170002Simp * parties, which may result from its use. No license is granted by 46170002Simp * implication, estoppel or otherwise under any patent or other rights of the 47170002Simp * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 48170002Simp * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 49170002Simp * or the SD Card Association to disclose or distribute any technical 50170002Simp * information, know-how or other confidential information to any third party. 51163516Simp */ 52163516Simp 53163516Simp#include <sys/cdefs.h> 54163516Simp__FBSDID("$FreeBSD$"); 55163516Simp 56163516Simp#include <sys/param.h> 57163516Simp#include <sys/systm.h> 58163516Simp#include <sys/kernel.h> 59163516Simp#include <sys/malloc.h> 60163516Simp#include <sys/lock.h> 61163516Simp#include <sys/module.h> 62163516Simp#include <sys/mutex.h> 63163516Simp#include <sys/bus.h> 64183704Smav#include <sys/endian.h> 65187875Smav#include <sys/sysctl.h> 66163516Simp 67163516Simp#include <dev/mmc/mmcreg.h> 68163516Simp#include <dev/mmc/mmcbrvar.h> 69163516Simp#include <dev/mmc/mmcvar.h> 70163516Simp#include "mmcbr_if.h" 71163516Simp#include "mmcbus_if.h" 72163516Simp 73163516Simpstruct mmc_softc { 74163516Simp device_t dev; 75163516Simp struct mtx sc_mtx; 76163516Simp struct intr_config_hook config_intrhook; 77163516Simp device_t owner; 78163516Simp uint32_t last_rca; 79163516Simp}; 80163516Simp 81163516Simp/* 82163516Simp * Per-card data 83163516Simp */ 84163516Simpstruct mmc_ivars { 85163516Simp uint32_t raw_cid[4]; /* Raw bits of the CID */ 86163516Simp uint32_t raw_csd[4]; /* Raw bits of the CSD */ 87183704Smav uint32_t raw_scr[2]; /* Raw bits of the SCR */ 88183704Smav uint8_t raw_ext_csd[512]; /* Raw bits of the EXT_CSD */ 89184033Smav uint32_t raw_sd_status[16]; /* Raw bits of the SD_STATUS */ 90163516Simp uint16_t rca; 91163516Simp enum mmc_card_mode mode; 92163516Simp struct mmc_cid cid; /* cid decoded */ 93163516Simp struct mmc_csd csd; /* csd decoded */ 94183704Smav struct mmc_scr scr; /* scr decoded */ 95184033Smav struct mmc_sd_status sd_status; /* SD_STATUS decoded */ 96183447Simp u_char read_only; /* True when the device is read-only */ 97183704Smav u_char bus_width; /* Bus width to use */ 98183704Smav u_char timing; /* Bus timing support */ 99183731Smav u_char high_cap; /* High Capacity card (block addressed) */ 100183731Smav uint32_t sec_count; /* Card capacity in 512byte blocks */ 101183704Smav uint32_t tran_speed; /* Max speed in normal mode */ 102183704Smav uint32_t hs_tran_speed; /* Max speed in high speed mode */ 103184033Smav uint32_t erase_sector; /* Card native erase sector size */ 104234524Smarius char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */ 105269795Sian char card_sn_string[16];/* Formatted serial # for disk->d_ident */ 106163516Simp}; 107163516Simp 108163516Simp#define CMD_RETRIES 3 109163516Simp 110254716Sian#define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */ 111254427Sian 112227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver"); 113187875Smav 114188044Simpstatic int mmc_debug; 115187875SmavSYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level"); 116187875Smav 117163516Simp/* bus entry points */ 118236491Smariusstatic int mmc_acquire_bus(device_t busdev, device_t dev); 119163516Simpstatic int mmc_attach(device_t dev); 120236491Smariusstatic int mmc_child_location_str(device_t dev, device_t child, char *buf, 121236491Smarius size_t buflen); 122163516Simpstatic int mmc_detach(device_t dev); 123236491Smariusstatic int mmc_probe(device_t dev); 124236491Smariusstatic int mmc_read_ivar(device_t bus, device_t child, int which, 125236491Smarius uintptr_t *result); 126236491Smariusstatic int mmc_release_bus(device_t busdev, device_t dev); 127236491Smariusstatic int mmc_resume(device_t dev); 128185721Smavstatic int mmc_suspend(device_t dev); 129236491Smariusstatic int mmc_wait_for_request(device_t brdev, device_t reqdev, 130236491Smarius struct mmc_request *req); 131236491Smariusstatic int mmc_write_ivar(device_t bus, device_t child, int which, 132236491Smarius uintptr_t value); 133163516Simp 134163516Simp#define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 135163516Simp#define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 136183444Simp#define MMC_LOCK_INIT(_sc) \ 137183444Simp mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 138163516Simp "mmc", MTX_DEF) 139163516Simp#define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 140163516Simp#define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 141163516Simp#define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 142163516Simp 143236491Smariusstatic int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid); 144236491Smariusstatic void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr); 145236491Smariusstatic void mmc_app_decode_sd_status(uint32_t *raw_sd_status, 146236491Smarius struct mmc_sd_status *sd_status); 147236491Smariusstatic int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, 148236491Smarius uint32_t *rawsdstatus); 149236491Smariusstatic int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, 150236491Smarius uint32_t *rawscr); 151183763Smavstatic int mmc_calculate_clock(struct mmc_softc *sc); 152236491Smariusstatic void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid); 153236491Smariusstatic void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid); 154236491Smariusstatic void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd); 155236491Smariusstatic void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd); 156236491Smariusstatic void mmc_delayed_attach(void *xsc); 157236491Smariusstatic int mmc_delete_cards(struct mmc_softc *sc); 158236491Smariusstatic void mmc_discover_cards(struct mmc_softc *sc); 159236491Smariusstatic void mmc_format_card_id_string(struct mmc_ivars *ivar); 160236491Smariusstatic void mmc_go_discovery(struct mmc_softc *sc); 161236491Smariusstatic uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start, 162236491Smarius int size); 163236491Smariusstatic int mmc_highest_voltage(uint32_t ocr); 164236491Smariusstatic void mmc_idle_cards(struct mmc_softc *sc); 165236491Smariusstatic void mmc_ms_delay(int ms); 166236491Smariusstatic void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard); 167183449Simpstatic void mmc_power_down(struct mmc_softc *sc); 168236491Smariusstatic void mmc_power_up(struct mmc_softc *sc); 169236491Smariusstatic void mmc_rescan_cards(struct mmc_softc *sc); 170236491Smariusstatic void mmc_scan(struct mmc_softc *sc); 171236491Smariusstatic int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, 172236491Smarius uint8_t value, uint8_t *res); 173236491Smariusstatic int mmc_select_card(struct mmc_softc *sc, uint16_t rca); 174236491Smariusstatic uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr); 175236491Smariusstatic int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, 176236491Smarius uint32_t *rocr); 177236491Smariusstatic int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd); 178236491Smariusstatic int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd); 179236491Smariusstatic int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs); 180236491Smariusstatic int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, 181236491Smarius uint32_t *rocr); 182236491Smariusstatic int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp); 183236491Smariusstatic int mmc_send_status(struct mmc_softc *sc, uint16_t rca, 184236491Smarius uint32_t *status); 185236491Smariusstatic int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len); 186236491Smariusstatic int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, 187236491Smarius int width); 188236491Smariusstatic int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp); 189236491Smariusstatic int mmc_set_timing(struct mmc_softc *sc, int timing); 190236491Smariusstatic int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, 191236491Smarius uint8_t value); 192236491Smariusstatic int mmc_test_bus_width(struct mmc_softc *sc); 193236491Smariusstatic int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca, 194236491Smarius struct mmc_command *cmd, int retries); 195163516Simpstatic int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, 196163516Simp int retries); 197163516Simpstatic int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 198163516Simp uint32_t arg, uint32_t flags, uint32_t *resp, int retries); 199236491Smariusstatic int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req); 200236491Smariusstatic void mmc_wakeup(struct mmc_request *req); 201163516Simp 202163516Simpstatic void 203163516Simpmmc_ms_delay(int ms) 204163516Simp{ 205236491Smarius 206163516Simp DELAY(1000 * ms); /* XXX BAD */ 207163516Simp} 208163516Simp 209163516Simpstatic int 210163516Simpmmc_probe(device_t dev) 211163516Simp{ 212163516Simp 213183445Simp device_set_desc(dev, "MMC/SD bus"); 214163516Simp return (0); 215163516Simp} 216163516Simp 217163516Simpstatic int 218163516Simpmmc_attach(device_t dev) 219163516Simp{ 220163516Simp struct mmc_softc *sc; 221163516Simp 222163516Simp sc = device_get_softc(dev); 223163516Simp sc->dev = dev; 224163516Simp MMC_LOCK_INIT(sc); 225163516Simp 226163516Simp /* We'll probe and attach our children later, but before / mount */ 227163516Simp sc->config_intrhook.ich_func = mmc_delayed_attach; 228163516Simp sc->config_intrhook.ich_arg = sc; 229163516Simp if (config_intrhook_establish(&sc->config_intrhook) != 0) 230163516Simp device_printf(dev, "config_intrhook_establish failed\n"); 231163516Simp return (0); 232163516Simp} 233163516Simp 234163516Simpstatic int 235163516Simpmmc_detach(device_t dev) 236163516Simp{ 237169567Simp struct mmc_softc *sc = device_get_softc(dev); 238185721Smav int err; 239169567Simp 240185721Smav if ((err = mmc_delete_cards(sc)) != 0) 241185721Smav return (err); 242183449Simp mmc_power_down(sc); 243169567Simp MMC_LOCK_DESTROY(sc); 244169567Simp 245183467Simp return (0); 246163516Simp} 247163516Simp 248163516Simpstatic int 249185721Smavmmc_suspend(device_t dev) 250185721Smav{ 251185721Smav struct mmc_softc *sc = device_get_softc(dev); 252185721Smav int err; 253185721Smav 254185721Smav err = bus_generic_suspend(dev); 255185721Smav if (err) 256185721Smav return (err); 257185721Smav mmc_power_down(sc); 258185721Smav return (0); 259185721Smav} 260185721Smav 261185721Smavstatic int 262185721Smavmmc_resume(device_t dev) 263185721Smav{ 264185721Smav struct mmc_softc *sc = device_get_softc(dev); 265185721Smav 266185721Smav mmc_scan(sc); 267185721Smav return (bus_generic_resume(dev)); 268185721Smav} 269185721Smav 270185721Smavstatic int 271163516Simpmmc_acquire_bus(device_t busdev, device_t dev) 272163516Simp{ 273163516Simp struct mmc_softc *sc; 274183704Smav struct mmc_ivars *ivar; 275163516Simp int err; 276163516Simp int rca; 277163516Simp 278183452Simp err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev); 279163516Simp if (err) 280163516Simp return (err); 281163516Simp sc = device_get_softc(busdev); 282163516Simp MMC_LOCK(sc); 283163516Simp if (sc->owner) 284236156Smarius panic("mmc: host bridge didn't serialize us."); 285163516Simp sc->owner = dev; 286163516Simp MMC_UNLOCK(sc); 287163516Simp 288163516Simp if (busdev != dev) { 289183453Simp /* 290183453Simp * Keep track of the last rca that we've selected. If 291183453Simp * we're asked to do it again, don't. We never 292183453Simp * unselect unless the bus code itself wants the mmc 293183453Simp * bus, and constantly reselecting causes problems. 294183453Simp */ 295163516Simp rca = mmc_get_rca(dev); 296163516Simp if (sc->last_rca != rca) { 297183704Smav mmc_select_card(sc, rca); 298163516Simp sc->last_rca = rca; 299183704Smav /* Prepare bus width for the new card. */ 300183704Smav ivar = device_get_ivars(dev); 301187875Smav if (bootverbose || mmc_debug) { 302183763Smav device_printf(busdev, 303183763Smav "setting bus width to %d bits\n", 304183775Simp (ivar->bus_width == bus_width_4) ? 4 : 305183775Simp (ivar->bus_width == bus_width_8) ? 8 : 1); 306183763Smav } 307183763Smav mmc_set_card_bus_width(sc, rca, ivar->bus_width); 308183704Smav mmcbr_set_bus_width(busdev, ivar->bus_width); 309183704Smav mmcbr_update_ios(busdev); 310163516Simp } 311163516Simp } else { 312183453Simp /* 313183453Simp * If there's a card selected, stand down. 314183453Simp */ 315163516Simp if (sc->last_rca != 0) { 316183704Smav mmc_select_card(sc, 0); 317163516Simp sc->last_rca = 0; 318163516Simp } 319163516Simp } 320163516Simp 321163516Simp return (0); 322163516Simp} 323163516Simp 324163516Simpstatic int 325163516Simpmmc_release_bus(device_t busdev, device_t dev) 326163516Simp{ 327163516Simp struct mmc_softc *sc; 328163516Simp int err; 329163516Simp 330163516Simp sc = device_get_softc(busdev); 331163516Simp 332163516Simp MMC_LOCK(sc); 333163516Simp if (!sc->owner) 334163516Simp panic("mmc: releasing unowned bus."); 335163516Simp if (sc->owner != dev) 336163516Simp panic("mmc: you don't own the bus. game over."); 337163516Simp MMC_UNLOCK(sc); 338183452Simp err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev); 339163516Simp if (err) 340163516Simp return (err); 341163516Simp MMC_LOCK(sc); 342163516Simp sc->owner = NULL; 343163516Simp MMC_UNLOCK(sc); 344163516Simp return (0); 345163516Simp} 346163516Simp 347163516Simpstatic uint32_t 348163516Simpmmc_select_vdd(struct mmc_softc *sc, uint32_t ocr) 349163516Simp{ 350183446Simp 351183467Simp return (ocr & MMC_OCR_VOLTAGE); 352163516Simp} 353163516Simp 354163516Simpstatic int 355163516Simpmmc_highest_voltage(uint32_t ocr) 356163516Simp{ 357163516Simp int i; 358163516Simp 359245755Sgonzo for (i = MMC_OCR_MAX_VOLTAGE_SHIFT; 360245755Sgonzo i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--) 361163516Simp if (ocr & (1 << i)) 362183467Simp return (i); 363163516Simp return (-1); 364163516Simp} 365163516Simp 366163516Simpstatic void 367163516Simpmmc_wakeup(struct mmc_request *req) 368163516Simp{ 369163516Simp struct mmc_softc *sc; 370163516Simp 371163516Simp sc = (struct mmc_softc *)req->done_data; 372163516Simp MMC_LOCK(sc); 373163516Simp req->flags |= MMC_REQ_DONE; 374185721Smav MMC_UNLOCK(sc); 375163516Simp wakeup(req); 376163516Simp} 377163516Simp 378163516Simpstatic int 379163516Simpmmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req) 380163516Simp{ 381163516Simp 382163516Simp req->done = mmc_wakeup; 383163516Simp req->done_data = sc; 384187877Smav if (mmc_debug > 1) { 385187875Smav device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x", 386187875Smav req->cmd->opcode, req->cmd->arg, req->cmd->flags); 387187875Smav if (req->cmd->data) { 388187875Smav printf(" data %d\n", (int)req->cmd->data->len); 389187875Smav } else 390187875Smav printf("\n"); 391187875Smav } 392163516Simp MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req); 393163516Simp MMC_LOCK(sc); 394185721Smav while ((req->flags & MMC_REQ_DONE) == 0) 395185721Smav msleep(req, &sc->sc_mtx, 0, "mmcreq", 0); 396163516Simp MMC_UNLOCK(sc); 397254431Sian if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE)) 398254431Sian device_printf(sc->dev, "CMD%d RESULT: %d\n", 399254431Sian req->cmd->opcode, req->cmd->error); 400185721Smav return (0); 401163516Simp} 402163516Simp 403163516Simpstatic int 404163516Simpmmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req) 405163516Simp{ 406163516Simp struct mmc_softc *sc = device_get_softc(brdev); 407163516Simp 408183467Simp return (mmc_wait_for_req(sc, req)); 409163516Simp} 410163516Simp 411163516Simpstatic int 412163516Simpmmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries) 413163516Simp{ 414163516Simp struct mmc_request mreq; 415254431Sian int err; 416163516Simp 417254431Sian do { 418254431Sian memset(&mreq, 0, sizeof(mreq)); 419254431Sian memset(cmd->resp, 0, sizeof(cmd->resp)); 420254431Sian cmd->retries = 0; /* Retries done here, not in hardware. */ 421254431Sian cmd->mrq = &mreq; 422254431Sian mreq.cmd = cmd; 423254431Sian if (mmc_wait_for_req(sc, &mreq) != 0) 424254431Sian err = MMC_ERR_FAILED; 425254431Sian else 426254431Sian err = cmd->error; 427254431Sian } while (err != MMC_ERR_NONE && retries-- > 0); 428254431Sian 429254431Sian return (err); 430163516Simp} 431163516Simp 432163516Simpstatic int 433163516Simpmmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca, 434163516Simp struct mmc_command *cmd, int retries) 435163516Simp{ 436163516Simp struct mmc_command appcmd; 437254431Sian int err; 438163516Simp 439254431Sian do { 440254432Sian memset(&appcmd, 0, sizeof(appcmd)); 441163516Simp appcmd.opcode = MMC_APP_CMD; 442163516Simp appcmd.arg = rca << 16; 443163516Simp appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 444183470Simp appcmd.data = NULL; 445254431Sian if (mmc_wait_for_cmd(sc, &appcmd, 0) != 0) 446254431Sian err = MMC_ERR_FAILED; 447254431Sian else 448254431Sian err = appcmd.error; 449254431Sian if (err == MMC_ERR_NONE) { 450254431Sian if (!(appcmd.resp[0] & R1_APP_CMD)) 451254431Sian err = MMC_ERR_FAILED; 452254716Sian else if (mmc_wait_for_cmd(sc, cmd, 0) != 0) 453254716Sian err = MMC_ERR_FAILED; 454254431Sian else 455254431Sian err = cmd->error; 456254431Sian } 457254431Sian } while (err != MMC_ERR_NONE && retries-- > 0); 458254431Sian 459163516Simp return (err); 460163516Simp} 461163516Simp 462163516Simpstatic int 463163516Simpmmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 464163516Simp uint32_t arg, uint32_t flags, uint32_t *resp, int retries) 465163516Simp{ 466163516Simp struct mmc_command cmd; 467163516Simp int err; 468163516Simp 469163516Simp memset(&cmd, 0, sizeof(cmd)); 470163516Simp cmd.opcode = opcode; 471163516Simp cmd.arg = arg; 472163516Simp cmd.flags = flags; 473183470Simp cmd.data = NULL; 474163516Simp err = mmc_wait_for_cmd(sc, &cmd, retries); 475163516Simp if (err) 476163516Simp return (err); 477163516Simp if (resp) { 478163516Simp if (flags & MMC_RSP_136) 479163516Simp memcpy(resp, cmd.resp, 4 * sizeof(uint32_t)); 480163516Simp else 481163516Simp *resp = cmd.resp[0]; 482163516Simp } 483163516Simp return (0); 484163516Simp} 485163516Simp 486163516Simpstatic void 487163516Simpmmc_idle_cards(struct mmc_softc *sc) 488163516Simp{ 489163516Simp device_t dev; 490163516Simp struct mmc_command cmd; 491163516Simp 492163516Simp dev = sc->dev; 493163516Simp mmcbr_set_chip_select(dev, cs_high); 494163516Simp mmcbr_update_ios(dev); 495163516Simp mmc_ms_delay(1); 496163516Simp 497163516Simp memset(&cmd, 0, sizeof(cmd)); 498163516Simp cmd.opcode = MMC_GO_IDLE_STATE; 499163516Simp cmd.arg = 0; 500163516Simp cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 501183470Simp cmd.data = NULL; 502254431Sian mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 503163516Simp mmc_ms_delay(1); 504163516Simp 505163516Simp mmcbr_set_chip_select(dev, cs_dontcare); 506163516Simp mmcbr_update_ios(dev); 507163516Simp mmc_ms_delay(1); 508163516Simp} 509163516Simp 510163516Simpstatic int 511163516Simpmmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 512163516Simp{ 513163516Simp struct mmc_command cmd; 514163516Simp int err = MMC_ERR_NONE, i; 515163516Simp 516163516Simp memset(&cmd, 0, sizeof(cmd)); 517163516Simp cmd.opcode = ACMD_SD_SEND_OP_COND; 518163516Simp cmd.arg = ocr; 519163516Simp cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 520183470Simp cmd.data = NULL; 521163516Simp 522216941Spjd for (i = 0; i < 1000; i++) { 523163516Simp err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES); 524163516Simp if (err != MMC_ERR_NONE) 525163516Simp break; 526183709Smav if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 527183709Smav (ocr & MMC_OCR_VOLTAGE) == 0) 528163516Simp break; 529163516Simp err = MMC_ERR_TIMEOUT; 530163516Simp mmc_ms_delay(10); 531163516Simp } 532163516Simp if (rocr && err == MMC_ERR_NONE) 533163516Simp *rocr = cmd.resp[0]; 534183467Simp return (err); 535163516Simp} 536163516Simp 537163516Simpstatic int 538163516Simpmmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 539163516Simp{ 540163516Simp struct mmc_command cmd; 541163516Simp int err = MMC_ERR_NONE, i; 542163516Simp 543163516Simp memset(&cmd, 0, sizeof(cmd)); 544163516Simp cmd.opcode = MMC_SEND_OP_COND; 545163516Simp cmd.arg = ocr; 546163516Simp cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 547183470Simp cmd.data = NULL; 548163516Simp 549216941Spjd for (i = 0; i < 1000; i++) { 550163516Simp err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 551163516Simp if (err != MMC_ERR_NONE) 552163516Simp break; 553183709Smav if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 554183709Smav (ocr & MMC_OCR_VOLTAGE) == 0) 555163516Simp break; 556163516Simp err = MMC_ERR_TIMEOUT; 557163516Simp mmc_ms_delay(10); 558163516Simp } 559163516Simp if (rocr && err == MMC_ERR_NONE) 560163516Simp *rocr = cmd.resp[0]; 561183467Simp return (err); 562163516Simp} 563163516Simp 564183704Smavstatic int 565183704Smavmmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs) 566183704Smav{ 567183704Smav struct mmc_command cmd; 568183704Smav int err; 569183704Smav 570183704Smav memset(&cmd, 0, sizeof(cmd)); 571183704Smav cmd.opcode = SD_SEND_IF_COND; 572183704Smav cmd.arg = (vhs << 8) + 0xAA; 573183704Smav cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR; 574183704Smav cmd.data = NULL; 575183704Smav 576183704Smav err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 577183704Smav return (err); 578183704Smav} 579183704Smav 580163516Simpstatic void 581163516Simpmmc_power_up(struct mmc_softc *sc) 582163516Simp{ 583163516Simp device_t dev; 584163516Simp 585163516Simp dev = sc->dev; 586163516Simp mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev))); 587163516Simp mmcbr_set_bus_mode(dev, opendrain); 588163516Simp mmcbr_set_chip_select(dev, cs_dontcare); 589163516Simp mmcbr_set_bus_width(dev, bus_width_1); 590163516Simp mmcbr_set_power_mode(dev, power_up); 591163516Simp mmcbr_set_clock(dev, 0); 592163516Simp mmcbr_update_ios(dev); 593163516Simp mmc_ms_delay(1); 594163516Simp 595254427Sian mmcbr_set_clock(dev, CARD_ID_FREQUENCY); 596183704Smav mmcbr_set_timing(dev, bus_timing_normal); 597163516Simp mmcbr_set_power_mode(dev, power_on); 598163516Simp mmcbr_update_ios(dev); 599163516Simp mmc_ms_delay(2); 600163516Simp} 601163516Simp 602183449Simpstatic void 603183449Simpmmc_power_down(struct mmc_softc *sc) 604183449Simp{ 605183449Simp device_t dev = sc->dev; 606183449Simp 607183449Simp mmcbr_set_bus_mode(dev, opendrain); 608183449Simp mmcbr_set_chip_select(dev, cs_dontcare); 609183449Simp mmcbr_set_bus_width(dev, bus_width_1); 610183449Simp mmcbr_set_power_mode(dev, power_off); 611183449Simp mmcbr_set_clock(dev, 0); 612183704Smav mmcbr_set_timing(dev, bus_timing_normal); 613183449Simp mmcbr_update_ios(dev); 614183449Simp} 615183449Simp 616183704Smavstatic int 617183704Smavmmc_select_card(struct mmc_softc *sc, uint16_t rca) 618183704Smav{ 619183775Simp int flags; 620183775Simp 621183775Simp flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC; 622183775Simp return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16, 623183775Simp flags, NULL, CMD_RETRIES)); 624183704Smav} 625183704Smav 626183704Smavstatic int 627183704Smavmmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value) 628183704Smav{ 629183704Smav struct mmc_command cmd; 630183704Smav int err; 631183704Smav 632254432Sian memset(&cmd, 0, sizeof(cmd)); 633183704Smav cmd.opcode = MMC_SWITCH_FUNC; 634183704Smav cmd.arg = (MMC_SWITCH_FUNC_WR << 24) | 635183704Smav (index << 16) | 636183704Smav (value << 8) | 637183704Smav set; 638183704Smav cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 639183704Smav cmd.data = NULL; 640254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 641183704Smav return (err); 642183704Smav} 643183704Smav 644183704Smavstatic int 645188044Simpmmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value, 646188044Simp uint8_t *res) 647183704Smav{ 648183704Smav int err; 649183704Smav struct mmc_command cmd; 650183704Smav struct mmc_data data; 651183704Smav 652254432Sian memset(&cmd, 0, sizeof(cmd)); 653254432Sian memset(&data, 0, sizeof(data)); 654188044Simp memset(res, 0, 64); 655183704Smav 656183704Smav cmd.opcode = SD_SWITCH_FUNC; 657183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 658188044Simp cmd.arg = mode << 31; /* 0 - check, 1 - set */ 659183704Smav cmd.arg |= 0x00FFFFFF; 660183705Smav cmd.arg &= ~(0xF << (grp * 4)); 661183705Smav cmd.arg |= value << (grp * 4); 662183704Smav cmd.data = &data; 663183704Smav 664183704Smav data.data = res; 665183704Smav data.len = 64; 666183704Smav data.flags = MMC_DATA_READ; 667183704Smav 668183704Smav err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 669183704Smav return (err); 670183704Smav} 671183704Smav 672183704Smavstatic int 673183763Smavmmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width) 674183704Smav{ 675187546Simp struct mmc_command cmd; 676183704Smav int err; 677187546Simp uint8_t value; 678183704Smav 679183704Smav if (mmcbr_get_mode(sc->dev) == mode_sd) { 680254432Sian memset(&cmd, 0, sizeof(cmd)); 681234524Smarius cmd.opcode = ACMD_SET_CLR_CARD_DETECT; 682234524Smarius cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 683234524Smarius cmd.arg = SD_CLR_CARD_DETECT; 684234524Smarius err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 685234524Smarius if (err != 0) 686234524Smarius return (err); 687254432Sian memset(&cmd, 0, sizeof(cmd)); 688183704Smav cmd.opcode = ACMD_SET_BUS_WIDTH; 689183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 690183704Smav switch (width) { 691183704Smav case bus_width_1: 692183704Smav cmd.arg = SD_BUS_WIDTH_1; 693183704Smav break; 694183704Smav case bus_width_4: 695183704Smav cmd.arg = SD_BUS_WIDTH_4; 696183704Smav break; 697183704Smav default: 698183704Smav return (MMC_ERR_INVALID); 699183704Smav } 700183704Smav err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 701183704Smav } else { 702183704Smav switch (width) { 703183704Smav case bus_width_1: 704183704Smav value = EXT_CSD_BUS_WIDTH_1; 705183704Smav break; 706183704Smav case bus_width_4: 707183704Smav value = EXT_CSD_BUS_WIDTH_4; 708183704Smav break; 709183704Smav case bus_width_8: 710183704Smav value = EXT_CSD_BUS_WIDTH_8; 711183704Smav break; 712183704Smav default: 713183704Smav return (MMC_ERR_INVALID); 714183704Smav } 715187546Simp err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, 716187546Simp value); 717183704Smav } 718183704Smav return (err); 719183704Smav} 720183704Smav 721183704Smavstatic int 722183704Smavmmc_set_timing(struct mmc_softc *sc, int timing) 723183704Smav{ 724183704Smav int err; 725183704Smav uint8_t value; 726187543Simp u_char switch_res[64]; 727183704Smav 728183704Smav switch (timing) { 729183704Smav case bus_timing_normal: 730183704Smav value = 0; 731183704Smav break; 732183704Smav case bus_timing_hs: 733183704Smav value = 1; 734183704Smav break; 735183704Smav default: 736183704Smav return (MMC_ERR_INVALID); 737183704Smav } 738187543Simp if (mmcbr_get_mode(sc->dev) == mode_sd) 739188044Simp err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1, 740188044Simp value, switch_res); 741187543Simp else 742183704Smav err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, 743183704Smav EXT_CSD_HS_TIMING, value); 744183704Smav return (err); 745183704Smav} 746183704Smav 747183704Smavstatic int 748183704Smavmmc_test_bus_width(struct mmc_softc *sc) 749183704Smav{ 750183704Smav struct mmc_command cmd; 751183704Smav struct mmc_data data; 752183704Smav int err; 753183704Smav uint8_t buf[8]; 754183704Smav uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 755183704Smav uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 756183704Smav uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, }; 757183704Smav uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, }; 758183704Smav 759183704Smav if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) { 760183704Smav mmcbr_set_bus_width(sc->dev, bus_width_8); 761183704Smav mmcbr_update_ios(sc->dev); 762183704Smav 763254432Sian memset(&cmd, 0, sizeof(cmd)); 764254432Sian memset(&data, 0, sizeof(data)); 765183704Smav cmd.opcode = MMC_BUSTEST_W; 766183704Smav cmd.arg = 0; 767183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 768183704Smav cmd.data = &data; 769183704Smav 770183704Smav data.data = p8; 771183704Smav data.len = 8; 772183704Smav data.flags = MMC_DATA_WRITE; 773183704Smav mmc_wait_for_cmd(sc, &cmd, 0); 774183704Smav 775254432Sian memset(&cmd, 0, sizeof(cmd)); 776254432Sian memset(&data, 0, sizeof(data)); 777183704Smav cmd.opcode = MMC_BUSTEST_R; 778183704Smav cmd.arg = 0; 779183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 780183704Smav cmd.data = &data; 781183704Smav 782183704Smav data.data = buf; 783183704Smav data.len = 8; 784183704Smav data.flags = MMC_DATA_READ; 785183704Smav err = mmc_wait_for_cmd(sc, &cmd, 0); 786183704Smav 787183704Smav mmcbr_set_bus_width(sc->dev, bus_width_1); 788183704Smav mmcbr_update_ios(sc->dev); 789183704Smav 790183704Smav if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0) 791183704Smav return (bus_width_8); 792183704Smav } 793183704Smav 794183704Smav if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) { 795183704Smav mmcbr_set_bus_width(sc->dev, bus_width_4); 796183704Smav mmcbr_update_ios(sc->dev); 797183704Smav 798254432Sian memset(&cmd, 0, sizeof(cmd)); 799254432Sian memset(&data, 0, sizeof(data)); 800183704Smav cmd.opcode = MMC_BUSTEST_W; 801183704Smav cmd.arg = 0; 802183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 803183704Smav cmd.data = &data; 804183704Smav 805183704Smav data.data = p4; 806183704Smav data.len = 4; 807183704Smav data.flags = MMC_DATA_WRITE; 808183704Smav mmc_wait_for_cmd(sc, &cmd, 0); 809183704Smav 810254432Sian memset(&cmd, 0, sizeof(cmd)); 811254432Sian memset(&data, 0, sizeof(data)); 812183704Smav cmd.opcode = MMC_BUSTEST_R; 813183704Smav cmd.arg = 0; 814183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 815183704Smav cmd.data = &data; 816183704Smav 817183704Smav data.data = buf; 818183704Smav data.len = 4; 819183704Smav data.flags = MMC_DATA_READ; 820183704Smav err = mmc_wait_for_cmd(sc, &cmd, 0); 821183704Smav 822183704Smav mmcbr_set_bus_width(sc->dev, bus_width_1); 823183704Smav mmcbr_update_ios(sc->dev); 824183704Smav 825183704Smav if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0) 826183704Smav return (bus_width_4); 827183704Smav } 828183704Smav return (bus_width_1); 829183704Smav} 830183704Smav 831163516Simpstatic uint32_t 832184033Smavmmc_get_bits(uint32_t *bits, int bit_len, int start, int size) 833163516Simp{ 834183729Simp const int i = (bit_len / 32) - (start / 32) - 1; 835163516Simp const int shift = start & 31; 836163516Simp uint32_t retval = bits[i] >> shift; 837163516Simp if (size + shift > 32) 838163516Simp retval |= bits[i - 1] << (32 - shift); 839217509Smav return (retval & ((1llu << size) - 1)); 840163516Simp} 841163516Simp 842163516Simpstatic void 843183729Simpmmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid) 844163516Simp{ 845163516Simp int i; 846163516Simp 847183729Simp /* There's no version info, so we take it on faith */ 848163516Simp memset(cid, 0, sizeof(*cid)); 849184033Smav cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 850184033Smav cid->oid = mmc_get_bits(raw_cid, 128, 104, 16); 851183729Simp for (i = 0; i < 5; i++) 852184033Smav cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 853187875Smav cid->pnm[5] = 0; 854184033Smav cid->prv = mmc_get_bits(raw_cid, 128, 56, 8); 855184033Smav cid->psn = mmc_get_bits(raw_cid, 128, 24, 32); 856187875Smav cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000; 857184033Smav cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4); 858163516Simp} 859163516Simp 860183729Simpstatic void 861183729Simpmmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid) 862183729Simp{ 863183729Simp int i; 864183729Simp 865183729Simp /* There's no version info, so we take it on faith */ 866183729Simp memset(cid, 0, sizeof(*cid)); 867184033Smav cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 868184033Smav cid->oid = mmc_get_bits(raw_cid, 128, 104, 8); 869183729Simp for (i = 0; i < 6; i++) 870184033Smav cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 871187875Smav cid->pnm[6] = 0; 872184033Smav cid->prv = mmc_get_bits(raw_cid, 128, 48, 8); 873184033Smav cid->psn = mmc_get_bits(raw_cid, 128, 16, 32); 874184033Smav cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4); 875184033Smav cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997; 876183729Simp} 877183729Simp 878234524Smariusstatic void 879234524Smariusmmc_format_card_id_string(struct mmc_ivars *ivar) 880234524Smarius{ 881234524Smarius char oidstr[8]; 882234524Smarius uint8_t c1; 883234524Smarius uint8_t c2; 884234524Smarius 885234524Smarius /* 886234524Smarius * Format a card ID string for use by the mmcsd driver, it's what 887234524Smarius * appears between the <> in the following: 888234524Smarius * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0 889234524Smarius * 22.5MHz/4bit/128-block 890234524Smarius * 891269795Sian * Also format just the card serial number, which the mmcsd driver will 892269795Sian * use as the disk->d_ident string. 893269795Sian * 894234524Smarius * The card_id_string in mmc_ivars is currently allocated as 64 bytes, 895234524Smarius * and our max formatted length is currently 55 bytes if every field 896234524Smarius * contains the largest value. 897234524Smarius * 898234524Smarius * Sometimes the oid is two printable ascii chars; when it's not, 899234524Smarius * format it as 0xnnnn instead. 900234524Smarius */ 901234524Smarius c1 = (ivar->cid.oid >> 8) & 0x0ff; 902234524Smarius c2 = ivar->cid.oid & 0x0ff; 903234524Smarius if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f) 904234524Smarius snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2); 905234524Smarius else 906234524Smarius snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid); 907269795Sian snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string), 908269795Sian "%08X", ivar->cid.psn); 909234524Smarius snprintf(ivar->card_id_string, sizeof(ivar->card_id_string), 910269795Sian "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s", 911234524Smarius ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "", 912234524Smarius ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f, 913234524Smarius ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year, 914234524Smarius ivar->cid.mid, oidstr); 915234524Smarius} 916234524Smarius 917163516Simpstatic const int exp[8] = { 918163516Simp 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 919163516Simp}; 920234524Smarius 921163516Simpstatic const int mant[16] = { 922234524Smarius 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 923163516Simp}; 924234524Smarius 925163516Simpstatic const int cur_min[8] = { 926163516Simp 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000 927163516Simp}; 928234524Smarius 929163516Simpstatic const int cur_max[8] = { 930163516Simp 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000 931163516Simp}; 932163516Simp 933163516Simpstatic void 934183729Simpmmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd) 935163516Simp{ 936163516Simp int v; 937163516Simp int m; 938163516Simp int e; 939163516Simp 940163516Simp memset(csd, 0, sizeof(*csd)); 941184033Smav csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2); 942183729Simp if (v == 0) { 943184033Smav m = mmc_get_bits(raw_csd, 128, 115, 4); 944184033Smav e = mmc_get_bits(raw_csd, 128, 112, 3); 945236156Smarius csd->tacc = (exp[e] * mant[m] + 9) / 10; 946184033Smav csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 947184033Smav m = mmc_get_bits(raw_csd, 128, 99, 4); 948184033Smav e = mmc_get_bits(raw_csd, 128, 96, 3); 949183704Smav csd->tran_speed = exp[e] * 10000 * mant[m]; 950184033Smav csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 951184033Smav csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 952184033Smav csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 953184033Smav csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 954184033Smav csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 955184033Smav csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 956184033Smav csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 957184033Smav csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 958184033Smav csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 959184033Smav csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 960184033Smav m = mmc_get_bits(raw_csd, 128, 62, 12); 961184033Smav e = mmc_get_bits(raw_csd, 128, 47, 3); 962183704Smav csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 963184033Smav csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 964184033Smav csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 965184033Smav csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 966184033Smav csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 967184033Smav csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 968184033Smav csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 969184033Smav csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 970183729Simp } else if (v == 1) { 971184033Smav m = mmc_get_bits(raw_csd, 128, 115, 4); 972184033Smav e = mmc_get_bits(raw_csd, 128, 112, 3); 973236156Smarius csd->tacc = (exp[e] * mant[m] + 9) / 10; 974184033Smav csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 975184033Smav m = mmc_get_bits(raw_csd, 128, 99, 4); 976184033Smav e = mmc_get_bits(raw_csd, 128, 96, 3); 977183729Simp csd->tran_speed = exp[e] * 10000 * mant[m]; 978184033Smav csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 979184033Smav csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 980184033Smav csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 981184033Smav csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 982184033Smav csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 983184033Smav csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 984184033Smav csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) * 985183729Simp 512 * 1024; 986184033Smav csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 987184033Smav csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 988184033Smav csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 989184033Smav csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 990184033Smav csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 991184033Smav csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 992184033Smav csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 993183729Simp } else 994183729Simp panic("unknown SD CSD version"); 995163516Simp} 996163516Simp 997183704Smavstatic void 998183729Simpmmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd) 999183729Simp{ 1000183729Simp int m; 1001183729Simp int e; 1002183729Simp 1003183729Simp memset(csd, 0, sizeof(*csd)); 1004184033Smav csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2); 1005184033Smav csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4); 1006184033Smav m = mmc_get_bits(raw_csd, 128, 115, 4); 1007184033Smav e = mmc_get_bits(raw_csd, 128, 112, 3); 1008183729Simp csd->tacc = exp[e] * mant[m] + 9 / 10; 1009184033Smav csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 1010184033Smav m = mmc_get_bits(raw_csd, 128, 99, 4); 1011184033Smav e = mmc_get_bits(raw_csd, 128, 96, 3); 1012183729Simp csd->tran_speed = exp[e] * 10000 * mant[m]; 1013184033Smav csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 1014184033Smav csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 1015184033Smav csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 1016184033Smav csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 1017184033Smav csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 1018184033Smav csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 1019184033Smav csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 1020184033Smav csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 1021184033Smav csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 1022184033Smav csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 1023184033Smav m = mmc_get_bits(raw_csd, 128, 62, 12); 1024184033Smav e = mmc_get_bits(raw_csd, 128, 47, 3); 1025183729Simp csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 1026184033Smav csd->erase_blk_en = 0; 1027184033Smav csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) * 1028184033Smav (mmc_get_bits(raw_csd, 128, 37, 5) + 1); 1029184033Smav csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5); 1030184033Smav csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 1031184033Smav csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 1032184033Smav csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 1033184033Smav csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 1034183729Simp} 1035183729Simp 1036183729Simpstatic void 1037183704Smavmmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr) 1038183704Smav{ 1039183704Smav unsigned int scr_struct; 1040183704Smav 1041183704Smav memset(scr, 0, sizeof(*scr)); 1042183729Simp 1043184033Smav scr_struct = mmc_get_bits(raw_scr, 64, 60, 4); 1044183704Smav if (scr_struct != 0) { 1045183704Smav printf("Unrecognised SCR structure version %d\n", 1046183704Smav scr_struct); 1047183704Smav return; 1048183704Smav } 1049184033Smav scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4); 1050184033Smav scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4); 1051183704Smav} 1052183704Smav 1053184033Smavstatic void 1054184033Smavmmc_app_decode_sd_status(uint32_t *raw_sd_status, 1055184033Smav struct mmc_sd_status *sd_status) 1056184033Smav{ 1057184033Smav 1058184033Smav memset(sd_status, 0, sizeof(*sd_status)); 1059184033Smav 1060184033Smav sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2); 1061184033Smav sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1); 1062184033Smav sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16); 1063184033Smav sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12); 1064184033Smav sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8); 1065184033Smav sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8); 1066184033Smav sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4); 1067184033Smav sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16); 1068184033Smav sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6); 1069184033Smav sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2); 1070184033Smav} 1071184033Smav 1072163516Simpstatic int 1073163516Simpmmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid) 1074163516Simp{ 1075163516Simp struct mmc_command cmd; 1076163516Simp int err; 1077163516Simp 1078254432Sian memset(&cmd, 0, sizeof(cmd)); 1079163516Simp cmd.opcode = MMC_ALL_SEND_CID; 1080163516Simp cmd.arg = 0; 1081163516Simp cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 1082183470Simp cmd.data = NULL; 1083254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1084163516Simp memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 1085163516Simp return (err); 1086163516Simp} 1087163516Simp 1088163516Simpstatic int 1089236156Smariusmmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd) 1090163516Simp{ 1091163516Simp struct mmc_command cmd; 1092163516Simp int err; 1093163516Simp 1094254432Sian memset(&cmd, 0, sizeof(cmd)); 1095163516Simp cmd.opcode = MMC_SEND_CSD; 1096163516Simp cmd.arg = rca << 16; 1097163516Simp cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 1098183470Simp cmd.data = NULL; 1099254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1100236156Smarius memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t)); 1101163516Simp return (err); 1102163516Simp} 1103163516Simp 1104163516Simpstatic int 1105183704Smavmmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr) 1106183704Smav{ 1107183704Smav int err; 1108183704Smav struct mmc_command cmd; 1109183704Smav struct mmc_data data; 1110183704Smav 1111254432Sian memset(&cmd, 0, sizeof(cmd)); 1112254432Sian memset(&data, 0, sizeof(data)); 1113183704Smav 1114183704Smav memset(rawscr, 0, 8); 1115183704Smav cmd.opcode = ACMD_SEND_SCR; 1116183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1117183704Smav cmd.arg = 0; 1118183704Smav cmd.data = &data; 1119183704Smav 1120183704Smav data.data = rawscr; 1121183704Smav data.len = 8; 1122183704Smav data.flags = MMC_DATA_READ; 1123183704Smav 1124183704Smav err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 1125183704Smav rawscr[0] = be32toh(rawscr[0]); 1126183704Smav rawscr[1] = be32toh(rawscr[1]); 1127183704Smav return (err); 1128183704Smav} 1129183704Smav 1130183704Smavstatic int 1131183704Smavmmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd) 1132183704Smav{ 1133183704Smav int err; 1134183704Smav struct mmc_command cmd; 1135183704Smav struct mmc_data data; 1136183704Smav 1137254432Sian memset(&cmd, 0, sizeof(cmd)); 1138254432Sian memset(&data, 0, sizeof(data)); 1139183704Smav 1140183704Smav memset(rawextcsd, 0, 512); 1141183704Smav cmd.opcode = MMC_SEND_EXT_CSD; 1142183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1143183704Smav cmd.arg = 0; 1144183704Smav cmd.data = &data; 1145183704Smav 1146183704Smav data.data = rawextcsd; 1147183704Smav data.len = 512; 1148183704Smav data.flags = MMC_DATA_READ; 1149183704Smav 1150183704Smav err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1151183704Smav return (err); 1152183704Smav} 1153183704Smav 1154183704Smavstatic int 1155184033Smavmmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus) 1156184033Smav{ 1157184033Smav int err, i; 1158184033Smav struct mmc_command cmd; 1159184033Smav struct mmc_data data; 1160184033Smav 1161254432Sian memset(&cmd, 0, sizeof(cmd)); 1162254432Sian memset(&data, 0, sizeof(data)); 1163184033Smav 1164184033Smav memset(rawsdstatus, 0, 64); 1165184033Smav cmd.opcode = ACMD_SD_STATUS; 1166184033Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1167184033Smav cmd.arg = 0; 1168184033Smav cmd.data = &data; 1169184033Smav 1170184033Smav data.data = rawsdstatus; 1171184033Smav data.len = 64; 1172184033Smav data.flags = MMC_DATA_READ; 1173184033Smav 1174184033Smav err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 1175184033Smav for (i = 0; i < 16; i++) 1176184033Smav rawsdstatus[i] = be32toh(rawsdstatus[i]); 1177184033Smav return (err); 1178184033Smav} 1179184033Smav 1180184033Smavstatic int 1181183704Smavmmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp) 1182183704Smav{ 1183183704Smav struct mmc_command cmd; 1184183704Smav int err; 1185183704Smav 1186254432Sian memset(&cmd, 0, sizeof(cmd)); 1187183704Smav cmd.opcode = MMC_SET_RELATIVE_ADDR; 1188183704Smav cmd.arg = resp << 16; 1189183704Smav cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 1190183704Smav cmd.data = NULL; 1191254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1192183704Smav return (err); 1193183704Smav} 1194183704Smav 1195183704Smavstatic int 1196163516Simpmmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp) 1197163516Simp{ 1198163516Simp struct mmc_command cmd; 1199163516Simp int err; 1200163516Simp 1201254432Sian memset(&cmd, 0, sizeof(cmd)); 1202163516Simp cmd.opcode = SD_SEND_RELATIVE_ADDR; 1203163516Simp cmd.arg = 0; 1204163516Simp cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 1205183470Simp cmd.data = NULL; 1206254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1207163516Simp *resp = cmd.resp[0]; 1208163516Simp return (err); 1209163516Simp} 1210163516Simp 1211236156Smariusstatic int 1212236156Smariusmmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status) 1213236156Smarius{ 1214236156Smarius struct mmc_command cmd; 1215236156Smarius int err; 1216236156Smarius 1217254432Sian memset(&cmd, 0, sizeof(cmd)); 1218236156Smarius cmd.opcode = MMC_SEND_STATUS; 1219236156Smarius cmd.arg = rca << 16; 1220236156Smarius cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1221236156Smarius cmd.data = NULL; 1222254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1223236156Smarius *status = cmd.resp[0]; 1224236156Smarius return (err); 1225236156Smarius} 1226236156Smarius 1227236156Smariusstatic int 1228236156Smariusmmc_set_blocklen(struct mmc_softc *sc, uint32_t len) 1229236156Smarius{ 1230236156Smarius struct mmc_command cmd; 1231236156Smarius int err; 1232236156Smarius 1233254432Sian memset(&cmd, 0, sizeof(cmd)); 1234236156Smarius cmd.opcode = MMC_SET_BLOCKLEN; 1235236156Smarius cmd.arg = len; 1236236156Smarius cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1237236156Smarius cmd.data = NULL; 1238254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1239236156Smarius return (err); 1240236156Smarius} 1241236156Smarius 1242163516Simpstatic void 1243187875Smavmmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard) 1244187875Smav{ 1245254425Sian device_printf(dev, "Card at relative address 0x%04x%s:\n", 1246187875Smav ivar->rca, newcard ? " added" : ""); 1247234524Smarius device_printf(dev, " card: %s\n", ivar->card_id_string); 1248187875Smav device_printf(dev, " bus: %ubit, %uMHz%s\n", 1249187875Smav (ivar->bus_width == bus_width_1 ? 1 : 1250187875Smav (ivar->bus_width == bus_width_4 ? 4 : 8)), 1251187875Smav (ivar->timing == bus_timing_hs ? 1252187875Smav ivar->hs_tran_speed : ivar->tran_speed) / 1000000, 1253187875Smav ivar->timing == bus_timing_hs ? ", high speed timing" : ""); 1254187875Smav device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n", 1255187875Smav ivar->sec_count, ivar->erase_sector, 1256187875Smav ivar->read_only ? ", read-only" : ""); 1257187875Smav} 1258187875Smav 1259187875Smavstatic void 1260163516Simpmmc_discover_cards(struct mmc_softc *sc) 1261163516Simp{ 1262185721Smav struct mmc_ivars *ivar = NULL; 1263185721Smav device_t *devlist; 1264185721Smav int err, i, devcount, newcard; 1265236156Smarius uint32_t raw_cid[4], resp, sec_count, status; 1266163516Simp device_t child; 1267183704Smav uint16_t rca = 2; 1268183704Smav u_char switch_res[64]; 1269163516Simp 1270187875Smav if (bootverbose || mmc_debug) 1271187875Smav device_printf(sc->dev, "Probing cards\n"); 1272163516Simp while (1) { 1273185721Smav err = mmc_all_send_cid(sc, raw_cid); 1274163516Simp if (err == MMC_ERR_TIMEOUT) 1275163516Simp break; 1276163516Simp if (err != MMC_ERR_NONE) { 1277183468Simp device_printf(sc->dev, "Error reading CID %d\n", err); 1278163516Simp break; 1279163516Simp } 1280185721Smav newcard = 1; 1281185721Smav if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 1282185721Smav return; 1283185721Smav for (i = 0; i < devcount; i++) { 1284185721Smav ivar = device_get_ivars(devlist[i]); 1285185721Smav if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) { 1286185721Smav newcard = 0; 1287185721Smav break; 1288185721Smav } 1289185721Smav } 1290185721Smav free(devlist, M_TEMP); 1291187875Smav if (bootverbose || mmc_debug) { 1292187875Smav device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n", 1293187875Smav newcard ? "New c" : "C", 1294187875Smav raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]); 1295187875Smav } 1296185721Smav if (newcard) { 1297185721Smav ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF, 1298185721Smav M_WAITOK | M_ZERO); 1299185721Smav memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid)); 1300185721Smav } 1301183704Smav if (mmcbr_get_ro(sc->dev)) 1302183704Smav ivar->read_only = 1; 1303183704Smav ivar->bus_width = bus_width_1; 1304188044Simp ivar->timing = bus_timing_normal; 1305183704Smav ivar->mode = mmcbr_get_mode(sc->dev); 1306183704Smav if (ivar->mode == mode_sd) { 1307183729Simp mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid); 1308163516Simp mmc_send_relative_addr(sc, &resp); 1309163516Simp ivar->rca = resp >> 16; 1310183704Smav /* Get card CSD. */ 1311163516Simp mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 1312236156Smarius if (bootverbose || mmc_debug) 1313236156Smarius device_printf(sc->dev, 1314236156Smarius "%sard detected (CSD %08x%08x%08x%08x)\n", 1315236156Smarius newcard ? "New c" : "C", ivar->raw_csd[0], 1316236156Smarius ivar->raw_csd[1], ivar->raw_csd[2], 1317236156Smarius ivar->raw_csd[3]); 1318183729Simp mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd); 1319183731Smav ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE; 1320183704Smav if (ivar->csd.csd_structure > 0) 1321183704Smav ivar->high_cap = 1; 1322183704Smav ivar->tran_speed = ivar->csd.tran_speed; 1323184033Smav ivar->erase_sector = ivar->csd.erase_sector * 1324184033Smav ivar->csd.write_bl_len / MMC_SECTOR_SIZE; 1325236156Smarius 1326236156Smarius err = mmc_send_status(sc, ivar->rca, &status); 1327236156Smarius if (err != MMC_ERR_NONE) { 1328236156Smarius device_printf(sc->dev, 1329236156Smarius "Error reading card status %d\n", err); 1330236156Smarius break; 1331236156Smarius } 1332236156Smarius if ((status & R1_CARD_IS_LOCKED) != 0) { 1333236156Smarius device_printf(sc->dev, 1334236156Smarius "Card is password protected, skipping.\n"); 1335236156Smarius break; 1336236156Smarius } 1337236156Smarius 1338183704Smav /* Get card SCR. Card must be selected to fetch it. */ 1339183704Smav mmc_select_card(sc, ivar->rca); 1340183704Smav mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr); 1341183704Smav mmc_app_decode_scr(ivar->raw_scr, &ivar->scr); 1342188044Simp /* Get card switch capabilities (command class 10). */ 1343183704Smav if ((ivar->scr.sda_vsn >= 1) && 1344183704Smav (ivar->csd.ccc & (1<<10))) { 1345188044Simp mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK, 1346188044Simp SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE, 1347188044Simp switch_res); 1348183704Smav if (switch_res[13] & 2) { 1349183704Smav ivar->timing = bus_timing_hs; 1350188044Simp ivar->hs_tran_speed = SD_MAX_HS; 1351183704Smav } 1352183704Smav } 1353184033Smav mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status); 1354184033Smav mmc_app_decode_sd_status(ivar->raw_sd_status, 1355184033Smav &ivar->sd_status); 1356184033Smav if (ivar->sd_status.au_size != 0) { 1357184033Smav ivar->erase_sector = 1358184033Smav 16 << ivar->sd_status.au_size; 1359184033Smav } 1360183704Smav mmc_select_card(sc, 0); 1361183704Smav /* Find max supported bus width. */ 1362183704Smav if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) && 1363183704Smav (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) 1364183704Smav ivar->bus_width = bus_width_4; 1365236156Smarius 1366236156Smarius /* 1367236156Smarius * Some cards that report maximum I/O block sizes 1368236156Smarius * greater than 512 require the block length to be 1369236156Smarius * set to 512, even though that is supposed to be 1370236156Smarius * the default. Example: 1371236156Smarius * 1372236156Smarius * Transcend 2GB SDSC card, CID: 1373236156Smarius * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000 1374236156Smarius */ 1375236156Smarius if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE || 1376236156Smarius ivar->csd.write_bl_len != MMC_SECTOR_SIZE) 1377236156Smarius mmc_set_blocklen(sc, MMC_SECTOR_SIZE); 1378236156Smarius 1379234524Smarius mmc_format_card_id_string(ivar); 1380236156Smarius 1381187875Smav if (bootverbose || mmc_debug) 1382187875Smav mmc_log_card(sc->dev, ivar, newcard); 1383185721Smav if (newcard) { 1384185721Smav /* Add device. */ 1385185721Smav child = device_add_child(sc->dev, NULL, -1); 1386185721Smav device_set_ivars(child, ivar); 1387185721Smav } 1388169567Simp return; 1389163516Simp } 1390183729Simp mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid); 1391183704Smav ivar->rca = rca++; 1392183704Smav mmc_set_relative_addr(sc, ivar->rca); 1393183704Smav /* Get card CSD. */ 1394183704Smav mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 1395236156Smarius if (bootverbose || mmc_debug) 1396236156Smarius device_printf(sc->dev, 1397236156Smarius "%sard detected (CSD %08x%08x%08x%08x)\n", 1398236156Smarius newcard ? "New c" : "C", ivar->raw_csd[0], 1399236156Smarius ivar->raw_csd[1], ivar->raw_csd[2], 1400236156Smarius ivar->raw_csd[3]); 1401236156Smarius 1402183729Simp mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd); 1403183731Smav ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE; 1404183704Smav ivar->tran_speed = ivar->csd.tran_speed; 1405184033Smav ivar->erase_sector = ivar->csd.erase_sector * 1406184033Smav ivar->csd.write_bl_len / MMC_SECTOR_SIZE; 1407236156Smarius 1408236156Smarius err = mmc_send_status(sc, ivar->rca, &status); 1409236156Smarius if (err != MMC_ERR_NONE) { 1410236156Smarius device_printf(sc->dev, 1411236156Smarius "Error reading card status %d\n", err); 1412236156Smarius break; 1413236156Smarius } 1414236156Smarius if ((status & R1_CARD_IS_LOCKED) != 0) { 1415236156Smarius device_printf(sc->dev, 1416236156Smarius "Card is password protected, skipping.\n"); 1417236156Smarius break; 1418236156Smarius } 1419236156Smarius 1420183704Smav /* Only MMC >= 4.x cards support EXT_CSD. */ 1421183704Smav if (ivar->csd.spec_vers >= 4) { 1422183704Smav /* Card must be selected to fetch EXT_CSD. */ 1423183704Smav mmc_select_card(sc, ivar->rca); 1424183704Smav mmc_send_ext_csd(sc, ivar->raw_ext_csd); 1425183731Smav /* Handle extended capacity from EXT_CSD */ 1426183731Smav sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] + 1427183731Smav (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) + 1428183731Smav (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) + 1429183731Smav (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24); 1430183731Smav if (sec_count != 0) { 1431183731Smav ivar->sec_count = sec_count; 1432183731Smav ivar->high_cap = 1; 1433183731Smav } 1434183704Smav /* Get card speed in high speed mode. */ 1435183704Smav ivar->timing = bus_timing_hs; 1436183731Smav if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] 1437183704Smav & EXT_CSD_CARD_TYPE_52) 1438188044Simp ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS; 1439183731Smav else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] 1440183704Smav & EXT_CSD_CARD_TYPE_26) 1441188044Simp ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS; 1442183704Smav else 1443183704Smav ivar->hs_tran_speed = ivar->tran_speed; 1444183704Smav /* Find max supported bus width. */ 1445183704Smav ivar->bus_width = mmc_test_bus_width(sc); 1446183704Smav mmc_select_card(sc, 0); 1447184033Smav /* Handle HC erase sector size. */ 1448184033Smav if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) { 1449184033Smav ivar->erase_sector = 1024 * 1450184033Smav ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE]; 1451184033Smav mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, 1452184033Smav EXT_CSD_ERASE_GRP_DEF, 1); 1453184033Smav } 1454183704Smav } else { 1455183704Smav ivar->bus_width = bus_width_1; 1456183704Smav ivar->timing = bus_timing_normal; 1457183704Smav } 1458236156Smarius 1459236156Smarius /* 1460236156Smarius * Some cards that report maximum I/O block sizes greater 1461236156Smarius * than 512 require the block length to be set to 512, even 1462236156Smarius * though that is supposed to be the default. Example: 1463236156Smarius * 1464236156Smarius * Transcend 2GB SDSC card, CID: 1465236156Smarius * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000 1466236156Smarius */ 1467236156Smarius if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE || 1468236156Smarius ivar->csd.write_bl_len != MMC_SECTOR_SIZE) 1469236156Smarius mmc_set_blocklen(sc, MMC_SECTOR_SIZE); 1470236156Smarius 1471234524Smarius mmc_format_card_id_string(ivar); 1472236156Smarius 1473187875Smav if (bootverbose || mmc_debug) 1474187875Smav mmc_log_card(sc->dev, ivar, newcard); 1475185721Smav if (newcard) { 1476185721Smav /* Add device. */ 1477185721Smav child = device_add_child(sc->dev, NULL, -1); 1478185721Smav device_set_ivars(child, ivar); 1479185721Smav } 1480163516Simp } 1481163516Simp} 1482163516Simp 1483163516Simpstatic void 1484185721Smavmmc_rescan_cards(struct mmc_softc *sc) 1485185721Smav{ 1486185721Smav struct mmc_ivars *ivar = NULL; 1487185721Smav device_t *devlist; 1488185721Smav int err, i, devcount; 1489185721Smav 1490185721Smav if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 1491185721Smav return; 1492185721Smav for (i = 0; i < devcount; i++) { 1493185721Smav ivar = device_get_ivars(devlist[i]); 1494185721Smav if (mmc_select_card(sc, ivar->rca)) { 1495187875Smav if (bootverbose || mmc_debug) 1496187875Smav device_printf(sc->dev, "Card at relative address %d lost.\n", 1497187875Smav ivar->rca); 1498185721Smav device_delete_child(sc->dev, devlist[i]); 1499185721Smav free(ivar, M_DEVBUF); 1500185721Smav } 1501185721Smav } 1502185721Smav free(devlist, M_TEMP); 1503185721Smav mmc_select_card(sc, 0); 1504185721Smav} 1505185721Smav 1506185721Smavstatic int 1507185721Smavmmc_delete_cards(struct mmc_softc *sc) 1508185721Smav{ 1509185721Smav struct mmc_ivars *ivar; 1510185721Smav device_t *devlist; 1511185721Smav int err, i, devcount; 1512185721Smav 1513185721Smav if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 1514185721Smav return (err); 1515185721Smav for (i = 0; i < devcount; i++) { 1516185721Smav ivar = device_get_ivars(devlist[i]); 1517187875Smav if (bootverbose || mmc_debug) 1518187875Smav device_printf(sc->dev, "Card at relative address %d deleted.\n", 1519187875Smav ivar->rca); 1520185721Smav device_delete_child(sc->dev, devlist[i]); 1521185721Smav free(ivar, M_DEVBUF); 1522185721Smav } 1523185721Smav free(devlist, M_TEMP); 1524185721Smav return (0); 1525185721Smav} 1526185721Smav 1527185721Smavstatic void 1528163516Simpmmc_go_discovery(struct mmc_softc *sc) 1529163516Simp{ 1530163516Simp uint32_t ocr; 1531163516Simp device_t dev; 1532183704Smav int err; 1533163516Simp 1534163516Simp dev = sc->dev; 1535163516Simp if (mmcbr_get_power_mode(dev) != power_on) { 1536183453Simp /* 1537183453Simp * First, try SD modes 1538183453Simp */ 1539163516Simp mmcbr_set_mode(dev, mode_sd); 1540163516Simp mmc_power_up(sc); 1541163516Simp mmcbr_set_bus_mode(dev, pushpull); 1542187875Smav if (bootverbose || mmc_debug) 1543187875Smav device_printf(sc->dev, "Probing bus\n"); 1544163516Simp mmc_idle_cards(sc); 1545183704Smav err = mmc_send_if_cond(sc, 1); 1546187875Smav if ((bootverbose || mmc_debug) && err == 0) 1547187875Smav device_printf(sc->dev, "SD 2.0 interface conditions: OK\n"); 1548236156Smarius if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 1549187875Smav if (bootverbose || mmc_debug) 1550187875Smav device_printf(sc->dev, "SD probe: failed\n"); 1551183453Simp /* 1552183453Simp * Failed, try MMC 1553183453Simp */ 1554163516Simp mmcbr_set_mode(dev, mode_mmc); 1555187875Smav if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 1556187875Smav if (bootverbose || mmc_debug) 1557187875Smav device_printf(sc->dev, "MMC probe: failed\n"); 1558185721Smav ocr = 0; /* Failed both, powerdown. */ 1559187875Smav } else if (bootverbose || mmc_debug) 1560187875Smav device_printf(sc->dev, 1561187875Smav "MMC probe: OK (OCR: 0x%08x)\n", ocr); 1562187875Smav } else if (bootverbose || mmc_debug) 1563187875Smav device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr); 1564187875Smav 1565163516Simp mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr)); 1566163516Simp if (mmcbr_get_ocr(dev) != 0) 1567163516Simp mmc_idle_cards(sc); 1568163516Simp } else { 1569163516Simp mmcbr_set_bus_mode(dev, opendrain); 1570254427Sian mmcbr_set_clock(dev, CARD_ID_FREQUENCY); 1571163516Simp mmcbr_update_ios(dev); 1572183453Simp /* XXX recompute vdd based on new cards? */ 1573163516Simp } 1574163516Simp /* 1575163516Simp * Make sure that we have a mutually agreeable voltage to at least 1576163516Simp * one card on the bus. 1577163516Simp */ 1578187875Smav if (bootverbose || mmc_debug) 1579187875Smav device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev)); 1580185721Smav if (mmcbr_get_ocr(dev) == 0) { 1581266200Sian device_printf(sc->dev, "No compatible cards found on bus\n"); 1582185721Smav mmc_delete_cards(sc); 1583185721Smav mmc_power_down(sc); 1584163516Simp return; 1585185721Smav } 1586163516Simp /* 1587163516Simp * Reselect the cards after we've idled them above. 1588163516Simp */ 1589183704Smav if (mmcbr_get_mode(dev) == mode_sd) { 1590183704Smav err = mmc_send_if_cond(sc, 1); 1591183704Smav mmc_send_app_op_cond(sc, 1592183775Simp (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL); 1593183704Smav } else 1594163516Simp mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL); 1595163516Simp mmc_discover_cards(sc); 1596185721Smav mmc_rescan_cards(sc); 1597163516Simp 1598163516Simp mmcbr_set_bus_mode(dev, pushpull); 1599163516Simp mmcbr_update_ios(dev); 1600183763Smav mmc_calculate_clock(sc); 1601163516Simp bus_generic_attach(dev); 1602183453Simp/* mmc_update_children_sysctl(dev);*/ 1603163516Simp} 1604163516Simp 1605163516Simpstatic int 1606163516Simpmmc_calculate_clock(struct mmc_softc *sc) 1607163516Simp{ 1608183704Smav int max_dtr, max_hs_dtr, max_timing; 1609254427Sian int nkid, i, f_max; 1610163516Simp device_t *kids; 1611183704Smav struct mmc_ivars *ivar; 1612163516Simp 1613163516Simp f_max = mmcbr_get_f_max(sc->dev); 1614183704Smav max_dtr = max_hs_dtr = f_max; 1615183704Smav if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED)) 1616183704Smav max_timing = bus_timing_hs; 1617183704Smav else 1618183704Smav max_timing = bus_timing_normal; 1619163516Simp if (device_get_children(sc->dev, &kids, &nkid) != 0) 1620163516Simp panic("can't get children"); 1621183704Smav for (i = 0; i < nkid; i++) { 1622183704Smav ivar = device_get_ivars(kids[i]); 1623183704Smav if (ivar->timing < max_timing) 1624183704Smav max_timing = ivar->timing; 1625183704Smav if (ivar->tran_speed < max_dtr) 1626183704Smav max_dtr = ivar->tran_speed; 1627187525Smav if (ivar->hs_tran_speed < max_hs_dtr) 1628183704Smav max_hs_dtr = ivar->hs_tran_speed; 1629183704Smav } 1630183704Smav for (i = 0; i < nkid; i++) { 1631183704Smav ivar = device_get_ivars(kids[i]); 1632183704Smav if (ivar->timing == bus_timing_normal) 1633183704Smav continue; 1634183704Smav mmc_select_card(sc, ivar->rca); 1635183704Smav mmc_set_timing(sc, max_timing); 1636183704Smav } 1637183704Smav mmc_select_card(sc, 0); 1638163516Simp free(kids, M_TEMP); 1639183704Smav if (max_timing == bus_timing_hs) 1640183704Smav max_dtr = max_hs_dtr; 1641187875Smav if (bootverbose || mmc_debug) { 1642183775Simp device_printf(sc->dev, 1643183775Simp "setting transfer rate to %d.%03dMHz%s\n", 1644183763Smav max_dtr / 1000000, (max_dtr / 1000) % 1000, 1645183775Simp max_timing == bus_timing_hs ? " (high speed timing)" : ""); 1646183763Smav } 1647183704Smav mmcbr_set_timing(sc->dev, max_timing); 1648183704Smav mmcbr_set_clock(sc->dev, max_dtr); 1649183704Smav mmcbr_update_ios(sc->dev); 1650183704Smav return max_dtr; 1651163516Simp} 1652163516Simp 1653163516Simpstatic void 1654163516Simpmmc_scan(struct mmc_softc *sc) 1655163516Simp{ 1656185721Smav device_t dev = sc->dev; 1657163516Simp 1658163516Simp mmc_acquire_bus(dev, dev); 1659163516Simp mmc_go_discovery(sc); 1660163516Simp mmc_release_bus(dev, dev); 1661163516Simp} 1662163516Simp 1663163516Simpstatic int 1664189727Simpmmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result) 1665163516Simp{ 1666163516Simp struct mmc_ivars *ivar = device_get_ivars(child); 1667163516Simp 1668163516Simp switch (which) { 1669163516Simp default: 1670163516Simp return (EINVAL); 1671163516Simp case MMC_IVAR_DSR_IMP: 1672222475Sjchandra *result = ivar->csd.dsr_imp; 1673163516Simp break; 1674163516Simp case MMC_IVAR_MEDIA_SIZE: 1675222475Sjchandra *result = ivar->sec_count; 1676163516Simp break; 1677163516Simp case MMC_IVAR_RCA: 1678222475Sjchandra *result = ivar->rca; 1679163516Simp break; 1680163516Simp case MMC_IVAR_SECTOR_SIZE: 1681222475Sjchandra *result = MMC_SECTOR_SIZE; 1682163516Simp break; 1683163516Simp case MMC_IVAR_TRAN_SPEED: 1684222475Sjchandra *result = mmcbr_get_clock(bus); 1685163516Simp break; 1686183447Simp case MMC_IVAR_READ_ONLY: 1687222475Sjchandra *result = ivar->read_only; 1688183447Simp break; 1689183704Smav case MMC_IVAR_HIGH_CAP: 1690222475Sjchandra *result = ivar->high_cap; 1691183704Smav break; 1692183763Smav case MMC_IVAR_CARD_TYPE: 1693222475Sjchandra *result = ivar->mode; 1694183763Smav break; 1695183763Smav case MMC_IVAR_BUS_WIDTH: 1696222475Sjchandra *result = ivar->bus_width; 1697183763Smav break; 1698184033Smav case MMC_IVAR_ERASE_SECTOR: 1699222475Sjchandra *result = ivar->erase_sector; 1700184033Smav break; 1701184452Smav case MMC_IVAR_MAX_DATA: 1702222475Sjchandra *result = mmcbr_get_max_data(bus); 1703184452Smav break; 1704234524Smarius case MMC_IVAR_CARD_ID_STRING: 1705234524Smarius *(char **)result = ivar->card_id_string; 1706234524Smarius break; 1707269795Sian case MMC_IVAR_CARD_SN_STRING: 1708269795Sian *(char **)result = ivar->card_sn_string; 1709269795Sian break; 1710163516Simp } 1711163516Simp return (0); 1712163516Simp} 1713163516Simp 1714163516Simpstatic int 1715163516Simpmmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 1716163516Simp{ 1717183453Simp /* 1718183453Simp * None are writable ATM 1719183453Simp */ 1720183453Simp return (EINVAL); 1721163516Simp} 1722163516Simp 1723163516Simpstatic void 1724163516Simpmmc_delayed_attach(void *xsc) 1725163516Simp{ 1726163516Simp struct mmc_softc *sc = xsc; 1727163516Simp 1728163516Simp mmc_scan(sc); 1729163516Simp config_intrhook_disestablish(&sc->config_intrhook); 1730163516Simp} 1731163516Simp 1732208441Smavstatic int 1733208441Smavmmc_child_location_str(device_t dev, device_t child, char *buf, 1734208441Smav size_t buflen) 1735208441Smav{ 1736208441Smav 1737208441Smav snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child)); 1738208441Smav return (0); 1739208441Smav} 1740208441Smav 1741163516Simpstatic device_method_t mmc_methods[] = { 1742163516Simp /* device_if */ 1743163516Simp DEVMETHOD(device_probe, mmc_probe), 1744163516Simp DEVMETHOD(device_attach, mmc_attach), 1745163516Simp DEVMETHOD(device_detach, mmc_detach), 1746185721Smav DEVMETHOD(device_suspend, mmc_suspend), 1747185721Smav DEVMETHOD(device_resume, mmc_resume), 1748163516Simp 1749163516Simp /* Bus interface */ 1750163516Simp DEVMETHOD(bus_read_ivar, mmc_read_ivar), 1751163516Simp DEVMETHOD(bus_write_ivar, mmc_write_ivar), 1752208441Smav DEVMETHOD(bus_child_location_str, mmc_child_location_str), 1753163516Simp 1754163516Simp /* MMC Bus interface */ 1755163516Simp DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request), 1756163516Simp DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus), 1757163516Simp DEVMETHOD(mmcbus_release_bus, mmc_release_bus), 1758163516Simp 1759234524Smarius DEVMETHOD_END 1760163516Simp}; 1761163516Simp 1762163516Simpstatic driver_t mmc_driver = { 1763163516Simp "mmc", 1764163516Simp mmc_methods, 1765163516Simp sizeof(struct mmc_softc), 1766163516Simp}; 1767163516Simpstatic devclass_t mmc_devclass; 1768163516Simp 1769188044SimpDRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL); 1770242321SgonzoDRIVER_MODULE(mmc, sdhci_bcm, mmc_driver, mmc_devclass, NULL, NULL); 1771249999SwkoszekDRIVER_MODULE(mmc, sdhci_fdt, mmc_driver, mmc_devclass, NULL, NULL); 1772266160SianDRIVER_MODULE(mmc, sdhci_imx, mmc_driver, mmc_devclass, NULL, NULL); 1773266160SianDRIVER_MODULE(mmc, sdhci_pci, mmc_driver, mmc_devclass, NULL, NULL); 1774254559SianDRIVER_MODULE(mmc, sdhci_ti, mmc_driver, mmc_devclass, NULL, NULL); 1775266160SianDRIVER_MODULE(mmc, ti_mmchs, mmc_driver, mmc_devclass, NULL, NULL); 1776266160Sian 1777