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) */ 105163516Simp}; 106163516Simp 107163516Simp#define CMD_RETRIES 3 108163516Simp 109254716Sian#define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */ 110254427Sian 111227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver"); 112187875Smav 113188044Simpstatic int mmc_debug; 114187875SmavSYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level"); 115187875Smav 116163516Simp/* bus entry points */ 117236491Smariusstatic int mmc_acquire_bus(device_t busdev, device_t dev); 118163516Simpstatic int mmc_attach(device_t dev); 119236491Smariusstatic int mmc_child_location_str(device_t dev, device_t child, char *buf, 120236491Smarius size_t buflen); 121163516Simpstatic int mmc_detach(device_t dev); 122236491Smariusstatic int mmc_probe(device_t dev); 123236491Smariusstatic int mmc_read_ivar(device_t bus, device_t child, int which, 124236491Smarius uintptr_t *result); 125236491Smariusstatic int mmc_release_bus(device_t busdev, device_t dev); 126236491Smariusstatic int mmc_resume(device_t dev); 127185721Smavstatic int mmc_suspend(device_t dev); 128236491Smariusstatic int mmc_wait_for_request(device_t brdev, device_t reqdev, 129236491Smarius struct mmc_request *req); 130236491Smariusstatic int mmc_write_ivar(device_t bus, device_t child, int which, 131236491Smarius uintptr_t value); 132163516Simp 133163516Simp#define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 134163516Simp#define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 135183444Simp#define MMC_LOCK_INIT(_sc) \ 136183444Simp mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 137163516Simp "mmc", MTX_DEF) 138163516Simp#define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 139163516Simp#define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 140163516Simp#define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 141163516Simp 142236491Smariusstatic int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid); 143236491Smariusstatic void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr); 144236491Smariusstatic void mmc_app_decode_sd_status(uint32_t *raw_sd_status, 145236491Smarius struct mmc_sd_status *sd_status); 146236491Smariusstatic int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, 147236491Smarius uint32_t *rawsdstatus); 148236491Smariusstatic int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, 149236491Smarius uint32_t *rawscr); 150183763Smavstatic int mmc_calculate_clock(struct mmc_softc *sc); 151236491Smariusstatic void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid); 152236491Smariusstatic void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid); 153236491Smariusstatic void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd); 154236491Smariusstatic void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd); 155236491Smariusstatic void mmc_delayed_attach(void *xsc); 156236491Smariusstatic int mmc_delete_cards(struct mmc_softc *sc); 157236491Smariusstatic void mmc_discover_cards(struct mmc_softc *sc); 158236491Smariusstatic void mmc_format_card_id_string(struct mmc_ivars *ivar); 159236491Smariusstatic void mmc_go_discovery(struct mmc_softc *sc); 160236491Smariusstatic uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start, 161236491Smarius int size); 162236491Smariusstatic int mmc_highest_voltage(uint32_t ocr); 163236491Smariusstatic void mmc_idle_cards(struct mmc_softc *sc); 164236491Smariusstatic void mmc_ms_delay(int ms); 165236491Smariusstatic void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard); 166183449Simpstatic void mmc_power_down(struct mmc_softc *sc); 167236491Smariusstatic void mmc_power_up(struct mmc_softc *sc); 168236491Smariusstatic void mmc_rescan_cards(struct mmc_softc *sc); 169236491Smariusstatic void mmc_scan(struct mmc_softc *sc); 170236491Smariusstatic int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, 171236491Smarius uint8_t value, uint8_t *res); 172236491Smariusstatic int mmc_select_card(struct mmc_softc *sc, uint16_t rca); 173236491Smariusstatic uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr); 174236491Smariusstatic int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, 175236491Smarius uint32_t *rocr); 176236491Smariusstatic int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd); 177236491Smariusstatic int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd); 178236491Smariusstatic int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs); 179236491Smariusstatic int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, 180236491Smarius uint32_t *rocr); 181236491Smariusstatic int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp); 182236491Smariusstatic int mmc_send_status(struct mmc_softc *sc, uint16_t rca, 183236491Smarius uint32_t *status); 184236491Smariusstatic int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len); 185236491Smariusstatic int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, 186236491Smarius int width); 187236491Smariusstatic int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp); 188236491Smariusstatic int mmc_set_timing(struct mmc_softc *sc, int timing); 189236491Smariusstatic int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, 190236491Smarius uint8_t value); 191236491Smariusstatic int mmc_test_bus_width(struct mmc_softc *sc); 192236491Smariusstatic int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca, 193236491Smarius struct mmc_command *cmd, int retries); 194163516Simpstatic int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, 195163516Simp int retries); 196163516Simpstatic int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 197163516Simp uint32_t arg, uint32_t flags, uint32_t *resp, int retries); 198236491Smariusstatic int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req); 199236491Smariusstatic void mmc_wakeup(struct mmc_request *req); 200163516Simp 201163516Simpstatic void 202163516Simpmmc_ms_delay(int ms) 203163516Simp{ 204236491Smarius 205163516Simp DELAY(1000 * ms); /* XXX BAD */ 206163516Simp} 207163516Simp 208163516Simpstatic int 209163516Simpmmc_probe(device_t dev) 210163516Simp{ 211163516Simp 212183445Simp device_set_desc(dev, "MMC/SD bus"); 213163516Simp return (0); 214163516Simp} 215163516Simp 216163516Simpstatic int 217163516Simpmmc_attach(device_t dev) 218163516Simp{ 219163516Simp struct mmc_softc *sc; 220163516Simp 221163516Simp sc = device_get_softc(dev); 222163516Simp sc->dev = dev; 223163516Simp MMC_LOCK_INIT(sc); 224163516Simp 225163516Simp /* We'll probe and attach our children later, but before / mount */ 226163516Simp sc->config_intrhook.ich_func = mmc_delayed_attach; 227163516Simp sc->config_intrhook.ich_arg = sc; 228163516Simp if (config_intrhook_establish(&sc->config_intrhook) != 0) 229163516Simp device_printf(dev, "config_intrhook_establish failed\n"); 230163516Simp return (0); 231163516Simp} 232163516Simp 233163516Simpstatic int 234163516Simpmmc_detach(device_t dev) 235163516Simp{ 236169567Simp struct mmc_softc *sc = device_get_softc(dev); 237185721Smav int err; 238169567Simp 239185721Smav if ((err = mmc_delete_cards(sc)) != 0) 240185721Smav return (err); 241183449Simp mmc_power_down(sc); 242169567Simp MMC_LOCK_DESTROY(sc); 243169567Simp 244183467Simp return (0); 245163516Simp} 246163516Simp 247163516Simpstatic int 248185721Smavmmc_suspend(device_t dev) 249185721Smav{ 250185721Smav struct mmc_softc *sc = device_get_softc(dev); 251185721Smav int err; 252185721Smav 253185721Smav err = bus_generic_suspend(dev); 254185721Smav if (err) 255185721Smav return (err); 256185721Smav mmc_power_down(sc); 257185721Smav return (0); 258185721Smav} 259185721Smav 260185721Smavstatic int 261185721Smavmmc_resume(device_t dev) 262185721Smav{ 263185721Smav struct mmc_softc *sc = device_get_softc(dev); 264185721Smav 265185721Smav mmc_scan(sc); 266185721Smav return (bus_generic_resume(dev)); 267185721Smav} 268185721Smav 269185721Smavstatic int 270163516Simpmmc_acquire_bus(device_t busdev, device_t dev) 271163516Simp{ 272163516Simp struct mmc_softc *sc; 273183704Smav struct mmc_ivars *ivar; 274163516Simp int err; 275163516Simp int rca; 276163516Simp 277183452Simp err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev); 278163516Simp if (err) 279163516Simp return (err); 280163516Simp sc = device_get_softc(busdev); 281163516Simp MMC_LOCK(sc); 282163516Simp if (sc->owner) 283236156Smarius panic("mmc: host bridge didn't serialize us."); 284163516Simp sc->owner = dev; 285163516Simp MMC_UNLOCK(sc); 286163516Simp 287163516Simp if (busdev != dev) { 288183453Simp /* 289183453Simp * Keep track of the last rca that we've selected. If 290183453Simp * we're asked to do it again, don't. We never 291183453Simp * unselect unless the bus code itself wants the mmc 292183453Simp * bus, and constantly reselecting causes problems. 293183453Simp */ 294163516Simp rca = mmc_get_rca(dev); 295163516Simp if (sc->last_rca != rca) { 296183704Smav mmc_select_card(sc, rca); 297163516Simp sc->last_rca = rca; 298183704Smav /* Prepare bus width for the new card. */ 299183704Smav ivar = device_get_ivars(dev); 300187875Smav if (bootverbose || mmc_debug) { 301183763Smav device_printf(busdev, 302183763Smav "setting bus width to %d bits\n", 303183775Simp (ivar->bus_width == bus_width_4) ? 4 : 304183775Simp (ivar->bus_width == bus_width_8) ? 8 : 1); 305183763Smav } 306183763Smav mmc_set_card_bus_width(sc, rca, ivar->bus_width); 307183704Smav mmcbr_set_bus_width(busdev, ivar->bus_width); 308183704Smav mmcbr_update_ios(busdev); 309163516Simp } 310163516Simp } else { 311183453Simp /* 312183453Simp * If there's a card selected, stand down. 313183453Simp */ 314163516Simp if (sc->last_rca != 0) { 315183704Smav mmc_select_card(sc, 0); 316163516Simp sc->last_rca = 0; 317163516Simp } 318163516Simp } 319163516Simp 320163516Simp return (0); 321163516Simp} 322163516Simp 323163516Simpstatic int 324163516Simpmmc_release_bus(device_t busdev, device_t dev) 325163516Simp{ 326163516Simp struct mmc_softc *sc; 327163516Simp int err; 328163516Simp 329163516Simp sc = device_get_softc(busdev); 330163516Simp 331163516Simp MMC_LOCK(sc); 332163516Simp if (!sc->owner) 333163516Simp panic("mmc: releasing unowned bus."); 334163516Simp if (sc->owner != dev) 335163516Simp panic("mmc: you don't own the bus. game over."); 336163516Simp MMC_UNLOCK(sc); 337183452Simp err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev); 338163516Simp if (err) 339163516Simp return (err); 340163516Simp MMC_LOCK(sc); 341163516Simp sc->owner = NULL; 342163516Simp MMC_UNLOCK(sc); 343163516Simp return (0); 344163516Simp} 345163516Simp 346163516Simpstatic uint32_t 347163516Simpmmc_select_vdd(struct mmc_softc *sc, uint32_t ocr) 348163516Simp{ 349183446Simp 350183467Simp return (ocr & MMC_OCR_VOLTAGE); 351163516Simp} 352163516Simp 353163516Simpstatic int 354163516Simpmmc_highest_voltage(uint32_t ocr) 355163516Simp{ 356163516Simp int i; 357163516Simp 358245755Sgonzo for (i = MMC_OCR_MAX_VOLTAGE_SHIFT; 359245755Sgonzo i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--) 360163516Simp if (ocr & (1 << i)) 361183467Simp return (i); 362163516Simp return (-1); 363163516Simp} 364163516Simp 365163516Simpstatic void 366163516Simpmmc_wakeup(struct mmc_request *req) 367163516Simp{ 368163516Simp struct mmc_softc *sc; 369163516Simp 370163516Simp sc = (struct mmc_softc *)req->done_data; 371163516Simp MMC_LOCK(sc); 372163516Simp req->flags |= MMC_REQ_DONE; 373185721Smav MMC_UNLOCK(sc); 374163516Simp wakeup(req); 375163516Simp} 376163516Simp 377163516Simpstatic int 378163516Simpmmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req) 379163516Simp{ 380163516Simp 381163516Simp req->done = mmc_wakeup; 382163516Simp req->done_data = sc; 383187877Smav if (mmc_debug > 1) { 384187875Smav device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x", 385187875Smav req->cmd->opcode, req->cmd->arg, req->cmd->flags); 386187875Smav if (req->cmd->data) { 387187875Smav printf(" data %d\n", (int)req->cmd->data->len); 388187875Smav } else 389187875Smav printf("\n"); 390187875Smav } 391163516Simp MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req); 392163516Simp MMC_LOCK(sc); 393185721Smav while ((req->flags & MMC_REQ_DONE) == 0) 394185721Smav msleep(req, &sc->sc_mtx, 0, "mmcreq", 0); 395163516Simp MMC_UNLOCK(sc); 396254431Sian if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE)) 397254431Sian device_printf(sc->dev, "CMD%d RESULT: %d\n", 398254431Sian req->cmd->opcode, req->cmd->error); 399185721Smav return (0); 400163516Simp} 401163516Simp 402163516Simpstatic int 403163516Simpmmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req) 404163516Simp{ 405163516Simp struct mmc_softc *sc = device_get_softc(brdev); 406163516Simp 407183467Simp return (mmc_wait_for_req(sc, req)); 408163516Simp} 409163516Simp 410163516Simpstatic int 411163516Simpmmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries) 412163516Simp{ 413163516Simp struct mmc_request mreq; 414254431Sian int err; 415163516Simp 416254431Sian do { 417254431Sian memset(&mreq, 0, sizeof(mreq)); 418254431Sian memset(cmd->resp, 0, sizeof(cmd->resp)); 419254431Sian cmd->retries = 0; /* Retries done here, not in hardware. */ 420254431Sian cmd->mrq = &mreq; 421254431Sian mreq.cmd = cmd; 422254431Sian if (mmc_wait_for_req(sc, &mreq) != 0) 423254431Sian err = MMC_ERR_FAILED; 424254431Sian else 425254431Sian err = cmd->error; 426254431Sian } while (err != MMC_ERR_NONE && retries-- > 0); 427254431Sian 428254431Sian return (err); 429163516Simp} 430163516Simp 431163516Simpstatic int 432163516Simpmmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca, 433163516Simp struct mmc_command *cmd, int retries) 434163516Simp{ 435163516Simp struct mmc_command appcmd; 436254431Sian int err; 437163516Simp 438254431Sian do { 439254432Sian memset(&appcmd, 0, sizeof(appcmd)); 440163516Simp appcmd.opcode = MMC_APP_CMD; 441163516Simp appcmd.arg = rca << 16; 442163516Simp appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 443183470Simp appcmd.data = NULL; 444254431Sian if (mmc_wait_for_cmd(sc, &appcmd, 0) != 0) 445254431Sian err = MMC_ERR_FAILED; 446254431Sian else 447254431Sian err = appcmd.error; 448254431Sian if (err == MMC_ERR_NONE) { 449254431Sian if (!(appcmd.resp[0] & R1_APP_CMD)) 450254431Sian err = MMC_ERR_FAILED; 451254716Sian else if (mmc_wait_for_cmd(sc, cmd, 0) != 0) 452254716Sian err = MMC_ERR_FAILED; 453254431Sian else 454254431Sian err = cmd->error; 455254431Sian } 456254431Sian } while (err != MMC_ERR_NONE && retries-- > 0); 457254431Sian 458163516Simp return (err); 459163516Simp} 460163516Simp 461163516Simpstatic int 462163516Simpmmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 463163516Simp uint32_t arg, uint32_t flags, uint32_t *resp, int retries) 464163516Simp{ 465163516Simp struct mmc_command cmd; 466163516Simp int err; 467163516Simp 468163516Simp memset(&cmd, 0, sizeof(cmd)); 469163516Simp cmd.opcode = opcode; 470163516Simp cmd.arg = arg; 471163516Simp cmd.flags = flags; 472183470Simp cmd.data = NULL; 473163516Simp err = mmc_wait_for_cmd(sc, &cmd, retries); 474163516Simp if (err) 475163516Simp return (err); 476163516Simp if (resp) { 477163516Simp if (flags & MMC_RSP_136) 478163516Simp memcpy(resp, cmd.resp, 4 * sizeof(uint32_t)); 479163516Simp else 480163516Simp *resp = cmd.resp[0]; 481163516Simp } 482163516Simp return (0); 483163516Simp} 484163516Simp 485163516Simpstatic void 486163516Simpmmc_idle_cards(struct mmc_softc *sc) 487163516Simp{ 488163516Simp device_t dev; 489163516Simp struct mmc_command cmd; 490163516Simp 491163516Simp dev = sc->dev; 492163516Simp mmcbr_set_chip_select(dev, cs_high); 493163516Simp mmcbr_update_ios(dev); 494163516Simp mmc_ms_delay(1); 495163516Simp 496163516Simp memset(&cmd, 0, sizeof(cmd)); 497163516Simp cmd.opcode = MMC_GO_IDLE_STATE; 498163516Simp cmd.arg = 0; 499163516Simp cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 500183470Simp cmd.data = NULL; 501254431Sian mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 502163516Simp mmc_ms_delay(1); 503163516Simp 504163516Simp mmcbr_set_chip_select(dev, cs_dontcare); 505163516Simp mmcbr_update_ios(dev); 506163516Simp mmc_ms_delay(1); 507163516Simp} 508163516Simp 509163516Simpstatic int 510163516Simpmmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 511163516Simp{ 512163516Simp struct mmc_command cmd; 513163516Simp int err = MMC_ERR_NONE, i; 514163516Simp 515163516Simp memset(&cmd, 0, sizeof(cmd)); 516163516Simp cmd.opcode = ACMD_SD_SEND_OP_COND; 517163516Simp cmd.arg = ocr; 518163516Simp cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 519183470Simp cmd.data = NULL; 520163516Simp 521216941Spjd for (i = 0; i < 1000; i++) { 522163516Simp err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES); 523163516Simp if (err != MMC_ERR_NONE) 524163516Simp break; 525183709Smav if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 526183709Smav (ocr & MMC_OCR_VOLTAGE) == 0) 527163516Simp break; 528163516Simp err = MMC_ERR_TIMEOUT; 529163516Simp mmc_ms_delay(10); 530163516Simp } 531163516Simp if (rocr && err == MMC_ERR_NONE) 532163516Simp *rocr = cmd.resp[0]; 533183467Simp return (err); 534163516Simp} 535163516Simp 536163516Simpstatic int 537163516Simpmmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 538163516Simp{ 539163516Simp struct mmc_command cmd; 540163516Simp int err = MMC_ERR_NONE, i; 541163516Simp 542163516Simp memset(&cmd, 0, sizeof(cmd)); 543163516Simp cmd.opcode = MMC_SEND_OP_COND; 544163516Simp cmd.arg = ocr; 545163516Simp cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 546183470Simp cmd.data = NULL; 547163516Simp 548216941Spjd for (i = 0; i < 1000; i++) { 549163516Simp err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 550163516Simp if (err != MMC_ERR_NONE) 551163516Simp break; 552183709Smav if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 553183709Smav (ocr & MMC_OCR_VOLTAGE) == 0) 554163516Simp break; 555163516Simp err = MMC_ERR_TIMEOUT; 556163516Simp mmc_ms_delay(10); 557163516Simp } 558163516Simp if (rocr && err == MMC_ERR_NONE) 559163516Simp *rocr = cmd.resp[0]; 560183467Simp return (err); 561163516Simp} 562163516Simp 563183704Smavstatic int 564183704Smavmmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs) 565183704Smav{ 566183704Smav struct mmc_command cmd; 567183704Smav int err; 568183704Smav 569183704Smav memset(&cmd, 0, sizeof(cmd)); 570183704Smav cmd.opcode = SD_SEND_IF_COND; 571183704Smav cmd.arg = (vhs << 8) + 0xAA; 572183704Smav cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR; 573183704Smav cmd.data = NULL; 574183704Smav 575183704Smav err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 576183704Smav return (err); 577183704Smav} 578183704Smav 579163516Simpstatic void 580163516Simpmmc_power_up(struct mmc_softc *sc) 581163516Simp{ 582163516Simp device_t dev; 583163516Simp 584163516Simp dev = sc->dev; 585163516Simp mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev))); 586163516Simp mmcbr_set_bus_mode(dev, opendrain); 587163516Simp mmcbr_set_chip_select(dev, cs_dontcare); 588163516Simp mmcbr_set_bus_width(dev, bus_width_1); 589163516Simp mmcbr_set_power_mode(dev, power_up); 590163516Simp mmcbr_set_clock(dev, 0); 591163516Simp mmcbr_update_ios(dev); 592163516Simp mmc_ms_delay(1); 593163516Simp 594254427Sian mmcbr_set_clock(dev, CARD_ID_FREQUENCY); 595183704Smav mmcbr_set_timing(dev, bus_timing_normal); 596163516Simp mmcbr_set_power_mode(dev, power_on); 597163516Simp mmcbr_update_ios(dev); 598163516Simp mmc_ms_delay(2); 599163516Simp} 600163516Simp 601183449Simpstatic void 602183449Simpmmc_power_down(struct mmc_softc *sc) 603183449Simp{ 604183449Simp device_t dev = sc->dev; 605183449Simp 606183449Simp mmcbr_set_bus_mode(dev, opendrain); 607183449Simp mmcbr_set_chip_select(dev, cs_dontcare); 608183449Simp mmcbr_set_bus_width(dev, bus_width_1); 609183449Simp mmcbr_set_power_mode(dev, power_off); 610183449Simp mmcbr_set_clock(dev, 0); 611183704Smav mmcbr_set_timing(dev, bus_timing_normal); 612183449Simp mmcbr_update_ios(dev); 613183449Simp} 614183449Simp 615183704Smavstatic int 616183704Smavmmc_select_card(struct mmc_softc *sc, uint16_t rca) 617183704Smav{ 618183775Simp int flags; 619183775Simp 620183775Simp flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC; 621183775Simp return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16, 622183775Simp flags, NULL, CMD_RETRIES)); 623183704Smav} 624183704Smav 625183704Smavstatic int 626183704Smavmmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value) 627183704Smav{ 628183704Smav struct mmc_command cmd; 629183704Smav int err; 630183704Smav 631254432Sian memset(&cmd, 0, sizeof(cmd)); 632183704Smav cmd.opcode = MMC_SWITCH_FUNC; 633183704Smav cmd.arg = (MMC_SWITCH_FUNC_WR << 24) | 634183704Smav (index << 16) | 635183704Smav (value << 8) | 636183704Smav set; 637183704Smav cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 638183704Smav cmd.data = NULL; 639254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 640183704Smav return (err); 641183704Smav} 642183704Smav 643183704Smavstatic int 644188044Simpmmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value, 645188044Simp uint8_t *res) 646183704Smav{ 647183704Smav int err; 648183704Smav struct mmc_command cmd; 649183704Smav struct mmc_data data; 650183704Smav 651254432Sian memset(&cmd, 0, sizeof(cmd)); 652254432Sian memset(&data, 0, sizeof(data)); 653188044Simp memset(res, 0, 64); 654183704Smav 655183704Smav cmd.opcode = SD_SWITCH_FUNC; 656183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 657188044Simp cmd.arg = mode << 31; /* 0 - check, 1 - set */ 658183704Smav cmd.arg |= 0x00FFFFFF; 659183705Smav cmd.arg &= ~(0xF << (grp * 4)); 660183705Smav cmd.arg |= value << (grp * 4); 661183704Smav cmd.data = &data; 662183704Smav 663183704Smav data.data = res; 664183704Smav data.len = 64; 665183704Smav data.flags = MMC_DATA_READ; 666183704Smav 667183704Smav err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 668183704Smav return (err); 669183704Smav} 670183704Smav 671183704Smavstatic int 672183763Smavmmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width) 673183704Smav{ 674187546Simp struct mmc_command cmd; 675183704Smav int err; 676187546Simp uint8_t value; 677183704Smav 678183704Smav if (mmcbr_get_mode(sc->dev) == mode_sd) { 679254432Sian memset(&cmd, 0, sizeof(cmd)); 680234524Smarius cmd.opcode = ACMD_SET_CLR_CARD_DETECT; 681234524Smarius cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 682234524Smarius cmd.arg = SD_CLR_CARD_DETECT; 683234524Smarius err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 684234524Smarius if (err != 0) 685234524Smarius return (err); 686254432Sian memset(&cmd, 0, sizeof(cmd)); 687183704Smav cmd.opcode = ACMD_SET_BUS_WIDTH; 688183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 689183704Smav switch (width) { 690183704Smav case bus_width_1: 691183704Smav cmd.arg = SD_BUS_WIDTH_1; 692183704Smav break; 693183704Smav case bus_width_4: 694183704Smav cmd.arg = SD_BUS_WIDTH_4; 695183704Smav break; 696183704Smav default: 697183704Smav return (MMC_ERR_INVALID); 698183704Smav } 699183704Smav err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 700183704Smav } else { 701183704Smav switch (width) { 702183704Smav case bus_width_1: 703183704Smav value = EXT_CSD_BUS_WIDTH_1; 704183704Smav break; 705183704Smav case bus_width_4: 706183704Smav value = EXT_CSD_BUS_WIDTH_4; 707183704Smav break; 708183704Smav case bus_width_8: 709183704Smav value = EXT_CSD_BUS_WIDTH_8; 710183704Smav break; 711183704Smav default: 712183704Smav return (MMC_ERR_INVALID); 713183704Smav } 714187546Simp err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, 715187546Simp value); 716183704Smav } 717183704Smav return (err); 718183704Smav} 719183704Smav 720183704Smavstatic int 721183704Smavmmc_set_timing(struct mmc_softc *sc, int timing) 722183704Smav{ 723183704Smav int err; 724183704Smav uint8_t value; 725187543Simp u_char switch_res[64]; 726183704Smav 727183704Smav switch (timing) { 728183704Smav case bus_timing_normal: 729183704Smav value = 0; 730183704Smav break; 731183704Smav case bus_timing_hs: 732183704Smav value = 1; 733183704Smav break; 734183704Smav default: 735183704Smav return (MMC_ERR_INVALID); 736183704Smav } 737187543Simp if (mmcbr_get_mode(sc->dev) == mode_sd) 738188044Simp err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1, 739188044Simp value, switch_res); 740187543Simp else 741183704Smav err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, 742183704Smav EXT_CSD_HS_TIMING, value); 743183704Smav return (err); 744183704Smav} 745183704Smav 746183704Smavstatic int 747183704Smavmmc_test_bus_width(struct mmc_softc *sc) 748183704Smav{ 749183704Smav struct mmc_command cmd; 750183704Smav struct mmc_data data; 751183704Smav int err; 752183704Smav uint8_t buf[8]; 753183704Smav uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 754183704Smav uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 755183704Smav uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, }; 756183704Smav uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, }; 757183704Smav 758183704Smav if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) { 759183704Smav mmcbr_set_bus_width(sc->dev, bus_width_8); 760183704Smav mmcbr_update_ios(sc->dev); 761183704Smav 762254432Sian memset(&cmd, 0, sizeof(cmd)); 763254432Sian memset(&data, 0, sizeof(data)); 764183704Smav cmd.opcode = MMC_BUSTEST_W; 765183704Smav cmd.arg = 0; 766183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 767183704Smav cmd.data = &data; 768183704Smav 769183704Smav data.data = p8; 770183704Smav data.len = 8; 771183704Smav data.flags = MMC_DATA_WRITE; 772183704Smav mmc_wait_for_cmd(sc, &cmd, 0); 773183704Smav 774254432Sian memset(&cmd, 0, sizeof(cmd)); 775254432Sian memset(&data, 0, sizeof(data)); 776183704Smav cmd.opcode = MMC_BUSTEST_R; 777183704Smav cmd.arg = 0; 778183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 779183704Smav cmd.data = &data; 780183704Smav 781183704Smav data.data = buf; 782183704Smav data.len = 8; 783183704Smav data.flags = MMC_DATA_READ; 784183704Smav err = mmc_wait_for_cmd(sc, &cmd, 0); 785183704Smav 786183704Smav mmcbr_set_bus_width(sc->dev, bus_width_1); 787183704Smav mmcbr_update_ios(sc->dev); 788183704Smav 789183704Smav if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0) 790183704Smav return (bus_width_8); 791183704Smav } 792183704Smav 793183704Smav if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) { 794183704Smav mmcbr_set_bus_width(sc->dev, bus_width_4); 795183704Smav mmcbr_update_ios(sc->dev); 796183704Smav 797254432Sian memset(&cmd, 0, sizeof(cmd)); 798254432Sian memset(&data, 0, sizeof(data)); 799183704Smav cmd.opcode = MMC_BUSTEST_W; 800183704Smav cmd.arg = 0; 801183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 802183704Smav cmd.data = &data; 803183704Smav 804183704Smav data.data = p4; 805183704Smav data.len = 4; 806183704Smav data.flags = MMC_DATA_WRITE; 807183704Smav mmc_wait_for_cmd(sc, &cmd, 0); 808183704Smav 809254432Sian memset(&cmd, 0, sizeof(cmd)); 810254432Sian memset(&data, 0, sizeof(data)); 811183704Smav cmd.opcode = MMC_BUSTEST_R; 812183704Smav cmd.arg = 0; 813183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 814183704Smav cmd.data = &data; 815183704Smav 816183704Smav data.data = buf; 817183704Smav data.len = 4; 818183704Smav data.flags = MMC_DATA_READ; 819183704Smav err = mmc_wait_for_cmd(sc, &cmd, 0); 820183704Smav 821183704Smav mmcbr_set_bus_width(sc->dev, bus_width_1); 822183704Smav mmcbr_update_ios(sc->dev); 823183704Smav 824183704Smav if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0) 825183704Smav return (bus_width_4); 826183704Smav } 827183704Smav return (bus_width_1); 828183704Smav} 829183704Smav 830163516Simpstatic uint32_t 831184033Smavmmc_get_bits(uint32_t *bits, int bit_len, int start, int size) 832163516Simp{ 833183729Simp const int i = (bit_len / 32) - (start / 32) - 1; 834163516Simp const int shift = start & 31; 835163516Simp uint32_t retval = bits[i] >> shift; 836163516Simp if (size + shift > 32) 837163516Simp retval |= bits[i - 1] << (32 - shift); 838217509Smav return (retval & ((1llu << size) - 1)); 839163516Simp} 840163516Simp 841163516Simpstatic void 842183729Simpmmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid) 843163516Simp{ 844163516Simp int i; 845163516Simp 846183729Simp /* There's no version info, so we take it on faith */ 847163516Simp memset(cid, 0, sizeof(*cid)); 848184033Smav cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 849184033Smav cid->oid = mmc_get_bits(raw_cid, 128, 104, 16); 850183729Simp for (i = 0; i < 5; i++) 851184033Smav cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 852187875Smav cid->pnm[5] = 0; 853184033Smav cid->prv = mmc_get_bits(raw_cid, 128, 56, 8); 854184033Smav cid->psn = mmc_get_bits(raw_cid, 128, 24, 32); 855187875Smav cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000; 856184033Smav cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4); 857163516Simp} 858163516Simp 859183729Simpstatic void 860183729Simpmmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid) 861183729Simp{ 862183729Simp int i; 863183729Simp 864183729Simp /* There's no version info, so we take it on faith */ 865183729Simp memset(cid, 0, sizeof(*cid)); 866184033Smav cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 867184033Smav cid->oid = mmc_get_bits(raw_cid, 128, 104, 8); 868183729Simp for (i = 0; i < 6; i++) 869184033Smav cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 870187875Smav cid->pnm[6] = 0; 871184033Smav cid->prv = mmc_get_bits(raw_cid, 128, 48, 8); 872184033Smav cid->psn = mmc_get_bits(raw_cid, 128, 16, 32); 873184033Smav cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4); 874184033Smav cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997; 875183729Simp} 876183729Simp 877234524Smariusstatic void 878234524Smariusmmc_format_card_id_string(struct mmc_ivars *ivar) 879234524Smarius{ 880234524Smarius char oidstr[8]; 881234524Smarius uint8_t c1; 882234524Smarius uint8_t c2; 883234524Smarius 884234524Smarius /* 885234524Smarius * Format a card ID string for use by the mmcsd driver, it's what 886234524Smarius * appears between the <> in the following: 887234524Smarius * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0 888234524Smarius * 22.5MHz/4bit/128-block 889234524Smarius * 890234524Smarius * The card_id_string in mmc_ivars is currently allocated as 64 bytes, 891234524Smarius * and our max formatted length is currently 55 bytes if every field 892234524Smarius * contains the largest value. 893234524Smarius * 894234524Smarius * Sometimes the oid is two printable ascii chars; when it's not, 895234524Smarius * format it as 0xnnnn instead. 896234524Smarius */ 897234524Smarius c1 = (ivar->cid.oid >> 8) & 0x0ff; 898234524Smarius c2 = ivar->cid.oid & 0x0ff; 899234524Smarius if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f) 900234524Smarius snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2); 901234524Smarius else 902234524Smarius snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid); 903234524Smarius snprintf(ivar->card_id_string, sizeof(ivar->card_id_string), 904243387Semaste "%s%s %s %d.%d SN %u MFG %02d/%04d by %d %s", 905234524Smarius ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "", 906234524Smarius ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f, 907234524Smarius ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year, 908234524Smarius ivar->cid.mid, oidstr); 909234524Smarius} 910234524Smarius 911163516Simpstatic const int exp[8] = { 912163516Simp 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 913163516Simp}; 914234524Smarius 915163516Simpstatic const int mant[16] = { 916234524Smarius 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 917163516Simp}; 918234524Smarius 919163516Simpstatic const int cur_min[8] = { 920163516Simp 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000 921163516Simp}; 922234524Smarius 923163516Simpstatic const int cur_max[8] = { 924163516Simp 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000 925163516Simp}; 926163516Simp 927163516Simpstatic void 928183729Simpmmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd) 929163516Simp{ 930163516Simp int v; 931163516Simp int m; 932163516Simp int e; 933163516Simp 934163516Simp memset(csd, 0, sizeof(*csd)); 935184033Smav csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2); 936183729Simp if (v == 0) { 937184033Smav m = mmc_get_bits(raw_csd, 128, 115, 4); 938184033Smav e = mmc_get_bits(raw_csd, 128, 112, 3); 939236156Smarius csd->tacc = (exp[e] * mant[m] + 9) / 10; 940184033Smav csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 941184033Smav m = mmc_get_bits(raw_csd, 128, 99, 4); 942184033Smav e = mmc_get_bits(raw_csd, 128, 96, 3); 943183704Smav csd->tran_speed = exp[e] * 10000 * mant[m]; 944184033Smav csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 945184033Smav csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 946184033Smav csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 947184033Smav csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 948184033Smav csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 949184033Smav csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 950184033Smav csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 951184033Smav csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 952184033Smav csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 953184033Smav csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 954184033Smav m = mmc_get_bits(raw_csd, 128, 62, 12); 955184033Smav e = mmc_get_bits(raw_csd, 128, 47, 3); 956183704Smav csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 957184033Smav csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 958184033Smav csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 959184033Smav csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 960184033Smav csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 961184033Smav csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 962184033Smav csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 963184033Smav csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 964183729Simp } else if (v == 1) { 965184033Smav m = mmc_get_bits(raw_csd, 128, 115, 4); 966184033Smav e = mmc_get_bits(raw_csd, 128, 112, 3); 967236156Smarius csd->tacc = (exp[e] * mant[m] + 9) / 10; 968184033Smav csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 969184033Smav m = mmc_get_bits(raw_csd, 128, 99, 4); 970184033Smav e = mmc_get_bits(raw_csd, 128, 96, 3); 971183729Simp csd->tran_speed = exp[e] * 10000 * mant[m]; 972184033Smav csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 973184033Smav csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 974184033Smav csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 975184033Smav csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 976184033Smav csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 977184033Smav csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 978184033Smav csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) * 979183729Simp 512 * 1024; 980184033Smav csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 981184033Smav csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 982184033Smav csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 983184033Smav csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 984184033Smav csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 985184033Smav csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 986184033Smav csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 987183729Simp } else 988183729Simp panic("unknown SD CSD version"); 989163516Simp} 990163516Simp 991183704Smavstatic void 992183729Simpmmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd) 993183729Simp{ 994183729Simp int m; 995183729Simp int e; 996183729Simp 997183729Simp memset(csd, 0, sizeof(*csd)); 998184033Smav csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2); 999184033Smav csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4); 1000184033Smav m = mmc_get_bits(raw_csd, 128, 115, 4); 1001184033Smav e = mmc_get_bits(raw_csd, 128, 112, 3); 1002183729Simp csd->tacc = exp[e] * mant[m] + 9 / 10; 1003184033Smav csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 1004184033Smav m = mmc_get_bits(raw_csd, 128, 99, 4); 1005184033Smav e = mmc_get_bits(raw_csd, 128, 96, 3); 1006183729Simp csd->tran_speed = exp[e] * 10000 * mant[m]; 1007184033Smav csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 1008184033Smav csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 1009184033Smav csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 1010184033Smav csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 1011184033Smav csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 1012184033Smav csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 1013184033Smav csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 1014184033Smav csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 1015184033Smav csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 1016184033Smav csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 1017184033Smav m = mmc_get_bits(raw_csd, 128, 62, 12); 1018184033Smav e = mmc_get_bits(raw_csd, 128, 47, 3); 1019183729Simp csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 1020184033Smav csd->erase_blk_en = 0; 1021184033Smav csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) * 1022184033Smav (mmc_get_bits(raw_csd, 128, 37, 5) + 1); 1023184033Smav csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5); 1024184033Smav csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 1025184033Smav csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 1026184033Smav csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 1027184033Smav csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 1028183729Simp} 1029183729Simp 1030183729Simpstatic void 1031183704Smavmmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr) 1032183704Smav{ 1033183704Smav unsigned int scr_struct; 1034183704Smav 1035183704Smav memset(scr, 0, sizeof(*scr)); 1036183729Simp 1037184033Smav scr_struct = mmc_get_bits(raw_scr, 64, 60, 4); 1038183704Smav if (scr_struct != 0) { 1039183704Smav printf("Unrecognised SCR structure version %d\n", 1040183704Smav scr_struct); 1041183704Smav return; 1042183704Smav } 1043184033Smav scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4); 1044184033Smav scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4); 1045183704Smav} 1046183704Smav 1047184033Smavstatic void 1048184033Smavmmc_app_decode_sd_status(uint32_t *raw_sd_status, 1049184033Smav struct mmc_sd_status *sd_status) 1050184033Smav{ 1051184033Smav 1052184033Smav memset(sd_status, 0, sizeof(*sd_status)); 1053184033Smav 1054184033Smav sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2); 1055184033Smav sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1); 1056184033Smav sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16); 1057184033Smav sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12); 1058184033Smav sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8); 1059184033Smav sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8); 1060184033Smav sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4); 1061184033Smav sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16); 1062184033Smav sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6); 1063184033Smav sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2); 1064184033Smav} 1065184033Smav 1066163516Simpstatic int 1067163516Simpmmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid) 1068163516Simp{ 1069163516Simp struct mmc_command cmd; 1070163516Simp int err; 1071163516Simp 1072254432Sian memset(&cmd, 0, sizeof(cmd)); 1073163516Simp cmd.opcode = MMC_ALL_SEND_CID; 1074163516Simp cmd.arg = 0; 1075163516Simp cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 1076183470Simp cmd.data = NULL; 1077254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1078163516Simp memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 1079163516Simp return (err); 1080163516Simp} 1081163516Simp 1082163516Simpstatic int 1083236156Smariusmmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd) 1084163516Simp{ 1085163516Simp struct mmc_command cmd; 1086163516Simp int err; 1087163516Simp 1088254432Sian memset(&cmd, 0, sizeof(cmd)); 1089163516Simp cmd.opcode = MMC_SEND_CSD; 1090163516Simp cmd.arg = rca << 16; 1091163516Simp cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 1092183470Simp cmd.data = NULL; 1093254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1094236156Smarius memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t)); 1095163516Simp return (err); 1096163516Simp} 1097163516Simp 1098163516Simpstatic int 1099183704Smavmmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr) 1100183704Smav{ 1101183704Smav int err; 1102183704Smav struct mmc_command cmd; 1103183704Smav struct mmc_data data; 1104183704Smav 1105254432Sian memset(&cmd, 0, sizeof(cmd)); 1106254432Sian memset(&data, 0, sizeof(data)); 1107183704Smav 1108183704Smav memset(rawscr, 0, 8); 1109183704Smav cmd.opcode = ACMD_SEND_SCR; 1110183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1111183704Smav cmd.arg = 0; 1112183704Smav cmd.data = &data; 1113183704Smav 1114183704Smav data.data = rawscr; 1115183704Smav data.len = 8; 1116183704Smav data.flags = MMC_DATA_READ; 1117183704Smav 1118183704Smav err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 1119183704Smav rawscr[0] = be32toh(rawscr[0]); 1120183704Smav rawscr[1] = be32toh(rawscr[1]); 1121183704Smav return (err); 1122183704Smav} 1123183704Smav 1124183704Smavstatic int 1125183704Smavmmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd) 1126183704Smav{ 1127183704Smav int err; 1128183704Smav struct mmc_command cmd; 1129183704Smav struct mmc_data data; 1130183704Smav 1131254432Sian memset(&cmd, 0, sizeof(cmd)); 1132254432Sian memset(&data, 0, sizeof(data)); 1133183704Smav 1134183704Smav memset(rawextcsd, 0, 512); 1135183704Smav cmd.opcode = MMC_SEND_EXT_CSD; 1136183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1137183704Smav cmd.arg = 0; 1138183704Smav cmd.data = &data; 1139183704Smav 1140183704Smav data.data = rawextcsd; 1141183704Smav data.len = 512; 1142183704Smav data.flags = MMC_DATA_READ; 1143183704Smav 1144183704Smav err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1145183704Smav return (err); 1146183704Smav} 1147183704Smav 1148183704Smavstatic int 1149184033Smavmmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus) 1150184033Smav{ 1151184033Smav int err, i; 1152184033Smav struct mmc_command cmd; 1153184033Smav struct mmc_data data; 1154184033Smav 1155254432Sian memset(&cmd, 0, sizeof(cmd)); 1156254432Sian memset(&data, 0, sizeof(data)); 1157184033Smav 1158184033Smav memset(rawsdstatus, 0, 64); 1159184033Smav cmd.opcode = ACMD_SD_STATUS; 1160184033Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1161184033Smav cmd.arg = 0; 1162184033Smav cmd.data = &data; 1163184033Smav 1164184033Smav data.data = rawsdstatus; 1165184033Smav data.len = 64; 1166184033Smav data.flags = MMC_DATA_READ; 1167184033Smav 1168184033Smav err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 1169184033Smav for (i = 0; i < 16; i++) 1170184033Smav rawsdstatus[i] = be32toh(rawsdstatus[i]); 1171184033Smav return (err); 1172184033Smav} 1173184033Smav 1174184033Smavstatic int 1175183704Smavmmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp) 1176183704Smav{ 1177183704Smav struct mmc_command cmd; 1178183704Smav int err; 1179183704Smav 1180254432Sian memset(&cmd, 0, sizeof(cmd)); 1181183704Smav cmd.opcode = MMC_SET_RELATIVE_ADDR; 1182183704Smav cmd.arg = resp << 16; 1183183704Smav cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 1184183704Smav cmd.data = NULL; 1185254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1186183704Smav return (err); 1187183704Smav} 1188183704Smav 1189183704Smavstatic int 1190163516Simpmmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp) 1191163516Simp{ 1192163516Simp struct mmc_command cmd; 1193163516Simp int err; 1194163516Simp 1195254432Sian memset(&cmd, 0, sizeof(cmd)); 1196163516Simp cmd.opcode = SD_SEND_RELATIVE_ADDR; 1197163516Simp cmd.arg = 0; 1198163516Simp cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 1199183470Simp cmd.data = NULL; 1200254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1201163516Simp *resp = cmd.resp[0]; 1202163516Simp return (err); 1203163516Simp} 1204163516Simp 1205236156Smariusstatic int 1206236156Smariusmmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status) 1207236156Smarius{ 1208236156Smarius struct mmc_command cmd; 1209236156Smarius int err; 1210236156Smarius 1211254432Sian memset(&cmd, 0, sizeof(cmd)); 1212236156Smarius cmd.opcode = MMC_SEND_STATUS; 1213236156Smarius cmd.arg = rca << 16; 1214236156Smarius cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1215236156Smarius cmd.data = NULL; 1216254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1217236156Smarius *status = cmd.resp[0]; 1218236156Smarius return (err); 1219236156Smarius} 1220236156Smarius 1221236156Smariusstatic int 1222236156Smariusmmc_set_blocklen(struct mmc_softc *sc, uint32_t len) 1223236156Smarius{ 1224236156Smarius struct mmc_command cmd; 1225236156Smarius int err; 1226236156Smarius 1227254432Sian memset(&cmd, 0, sizeof(cmd)); 1228236156Smarius cmd.opcode = MMC_SET_BLOCKLEN; 1229236156Smarius cmd.arg = len; 1230236156Smarius cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1231236156Smarius cmd.data = NULL; 1232254431Sian err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1233236156Smarius return (err); 1234236156Smarius} 1235236156Smarius 1236163516Simpstatic void 1237187875Smavmmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard) 1238187875Smav{ 1239254425Sian device_printf(dev, "Card at relative address 0x%04x%s:\n", 1240187875Smav ivar->rca, newcard ? " added" : ""); 1241234524Smarius device_printf(dev, " card: %s\n", ivar->card_id_string); 1242187875Smav device_printf(dev, " bus: %ubit, %uMHz%s\n", 1243187875Smav (ivar->bus_width == bus_width_1 ? 1 : 1244187875Smav (ivar->bus_width == bus_width_4 ? 4 : 8)), 1245187875Smav (ivar->timing == bus_timing_hs ? 1246187875Smav ivar->hs_tran_speed : ivar->tran_speed) / 1000000, 1247187875Smav ivar->timing == bus_timing_hs ? ", high speed timing" : ""); 1248187875Smav device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n", 1249187875Smav ivar->sec_count, ivar->erase_sector, 1250187875Smav ivar->read_only ? ", read-only" : ""); 1251187875Smav} 1252187875Smav 1253187875Smavstatic void 1254163516Simpmmc_discover_cards(struct mmc_softc *sc) 1255163516Simp{ 1256185721Smav struct mmc_ivars *ivar = NULL; 1257185721Smav device_t *devlist; 1258185721Smav int err, i, devcount, newcard; 1259236156Smarius uint32_t raw_cid[4], resp, sec_count, status; 1260163516Simp device_t child; 1261183704Smav uint16_t rca = 2; 1262183704Smav u_char switch_res[64]; 1263163516Simp 1264187875Smav if (bootverbose || mmc_debug) 1265187875Smav device_printf(sc->dev, "Probing cards\n"); 1266163516Simp while (1) { 1267185721Smav err = mmc_all_send_cid(sc, raw_cid); 1268163516Simp if (err == MMC_ERR_TIMEOUT) 1269163516Simp break; 1270163516Simp if (err != MMC_ERR_NONE) { 1271183468Simp device_printf(sc->dev, "Error reading CID %d\n", err); 1272163516Simp break; 1273163516Simp } 1274185721Smav newcard = 1; 1275185721Smav if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 1276185721Smav return; 1277185721Smav for (i = 0; i < devcount; i++) { 1278185721Smav ivar = device_get_ivars(devlist[i]); 1279185721Smav if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) { 1280185721Smav newcard = 0; 1281185721Smav break; 1282185721Smav } 1283185721Smav } 1284185721Smav free(devlist, M_TEMP); 1285187875Smav if (bootverbose || mmc_debug) { 1286187875Smav device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n", 1287187875Smav newcard ? "New c" : "C", 1288187875Smav raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]); 1289187875Smav } 1290185721Smav if (newcard) { 1291185721Smav ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF, 1292185721Smav M_WAITOK | M_ZERO); 1293185721Smav memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid)); 1294185721Smav } 1295183704Smav if (mmcbr_get_ro(sc->dev)) 1296183704Smav ivar->read_only = 1; 1297183704Smav ivar->bus_width = bus_width_1; 1298188044Simp ivar->timing = bus_timing_normal; 1299183704Smav ivar->mode = mmcbr_get_mode(sc->dev); 1300183704Smav if (ivar->mode == mode_sd) { 1301183729Simp mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid); 1302163516Simp mmc_send_relative_addr(sc, &resp); 1303163516Simp ivar->rca = resp >> 16; 1304183704Smav /* Get card CSD. */ 1305163516Simp mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 1306236156Smarius if (bootverbose || mmc_debug) 1307236156Smarius device_printf(sc->dev, 1308236156Smarius "%sard detected (CSD %08x%08x%08x%08x)\n", 1309236156Smarius newcard ? "New c" : "C", ivar->raw_csd[0], 1310236156Smarius ivar->raw_csd[1], ivar->raw_csd[2], 1311236156Smarius ivar->raw_csd[3]); 1312183729Simp mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd); 1313183731Smav ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE; 1314183704Smav if (ivar->csd.csd_structure > 0) 1315183704Smav ivar->high_cap = 1; 1316183704Smav ivar->tran_speed = ivar->csd.tran_speed; 1317184033Smav ivar->erase_sector = ivar->csd.erase_sector * 1318184033Smav ivar->csd.write_bl_len / MMC_SECTOR_SIZE; 1319236156Smarius 1320236156Smarius err = mmc_send_status(sc, ivar->rca, &status); 1321236156Smarius if (err != MMC_ERR_NONE) { 1322236156Smarius device_printf(sc->dev, 1323236156Smarius "Error reading card status %d\n", err); 1324236156Smarius break; 1325236156Smarius } 1326236156Smarius if ((status & R1_CARD_IS_LOCKED) != 0) { 1327236156Smarius device_printf(sc->dev, 1328236156Smarius "Card is password protected, skipping.\n"); 1329236156Smarius break; 1330236156Smarius } 1331236156Smarius 1332183704Smav /* Get card SCR. Card must be selected to fetch it. */ 1333183704Smav mmc_select_card(sc, ivar->rca); 1334183704Smav mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr); 1335183704Smav mmc_app_decode_scr(ivar->raw_scr, &ivar->scr); 1336188044Simp /* Get card switch capabilities (command class 10). */ 1337183704Smav if ((ivar->scr.sda_vsn >= 1) && 1338183704Smav (ivar->csd.ccc & (1<<10))) { 1339188044Simp mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK, 1340188044Simp SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE, 1341188044Simp switch_res); 1342183704Smav if (switch_res[13] & 2) { 1343183704Smav ivar->timing = bus_timing_hs; 1344188044Simp ivar->hs_tran_speed = SD_MAX_HS; 1345183704Smav } 1346183704Smav } 1347184033Smav mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status); 1348184033Smav mmc_app_decode_sd_status(ivar->raw_sd_status, 1349184033Smav &ivar->sd_status); 1350184033Smav if (ivar->sd_status.au_size != 0) { 1351184033Smav ivar->erase_sector = 1352184033Smav 16 << ivar->sd_status.au_size; 1353184033Smav } 1354183704Smav mmc_select_card(sc, 0); 1355183704Smav /* Find max supported bus width. */ 1356183704Smav if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) && 1357183704Smav (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) 1358183704Smav ivar->bus_width = bus_width_4; 1359236156Smarius 1360236156Smarius /* 1361236156Smarius * Some cards that report maximum I/O block sizes 1362236156Smarius * greater than 512 require the block length to be 1363236156Smarius * set to 512, even though that is supposed to be 1364236156Smarius * the default. Example: 1365236156Smarius * 1366236156Smarius * Transcend 2GB SDSC card, CID: 1367236156Smarius * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000 1368236156Smarius */ 1369236156Smarius if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE || 1370236156Smarius ivar->csd.write_bl_len != MMC_SECTOR_SIZE) 1371236156Smarius mmc_set_blocklen(sc, MMC_SECTOR_SIZE); 1372236156Smarius 1373234524Smarius mmc_format_card_id_string(ivar); 1374236156Smarius 1375187875Smav if (bootverbose || mmc_debug) 1376187875Smav mmc_log_card(sc->dev, ivar, newcard); 1377185721Smav if (newcard) { 1378185721Smav /* Add device. */ 1379185721Smav child = device_add_child(sc->dev, NULL, -1); 1380185721Smav device_set_ivars(child, ivar); 1381185721Smav } 1382169567Simp return; 1383163516Simp } 1384183729Simp mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid); 1385183704Smav ivar->rca = rca++; 1386183704Smav mmc_set_relative_addr(sc, ivar->rca); 1387183704Smav /* Get card CSD. */ 1388183704Smav mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 1389236156Smarius if (bootverbose || mmc_debug) 1390236156Smarius device_printf(sc->dev, 1391236156Smarius "%sard detected (CSD %08x%08x%08x%08x)\n", 1392236156Smarius newcard ? "New c" : "C", ivar->raw_csd[0], 1393236156Smarius ivar->raw_csd[1], ivar->raw_csd[2], 1394236156Smarius ivar->raw_csd[3]); 1395236156Smarius 1396183729Simp mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd); 1397183731Smav ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE; 1398183704Smav ivar->tran_speed = ivar->csd.tran_speed; 1399184033Smav ivar->erase_sector = ivar->csd.erase_sector * 1400184033Smav ivar->csd.write_bl_len / MMC_SECTOR_SIZE; 1401236156Smarius 1402236156Smarius err = mmc_send_status(sc, ivar->rca, &status); 1403236156Smarius if (err != MMC_ERR_NONE) { 1404236156Smarius device_printf(sc->dev, 1405236156Smarius "Error reading card status %d\n", err); 1406236156Smarius break; 1407236156Smarius } 1408236156Smarius if ((status & R1_CARD_IS_LOCKED) != 0) { 1409236156Smarius device_printf(sc->dev, 1410236156Smarius "Card is password protected, skipping.\n"); 1411236156Smarius break; 1412236156Smarius } 1413236156Smarius 1414183704Smav /* Only MMC >= 4.x cards support EXT_CSD. */ 1415183704Smav if (ivar->csd.spec_vers >= 4) { 1416183704Smav /* Card must be selected to fetch EXT_CSD. */ 1417183704Smav mmc_select_card(sc, ivar->rca); 1418183704Smav mmc_send_ext_csd(sc, ivar->raw_ext_csd); 1419183731Smav /* Handle extended capacity from EXT_CSD */ 1420183731Smav sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] + 1421183731Smav (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) + 1422183731Smav (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) + 1423183731Smav (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24); 1424183731Smav if (sec_count != 0) { 1425183731Smav ivar->sec_count = sec_count; 1426183731Smav ivar->high_cap = 1; 1427183731Smav } 1428183704Smav /* Get card speed in high speed mode. */ 1429183704Smav ivar->timing = bus_timing_hs; 1430183731Smav if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] 1431183704Smav & EXT_CSD_CARD_TYPE_52) 1432188044Simp ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS; 1433183731Smav else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] 1434183704Smav & EXT_CSD_CARD_TYPE_26) 1435188044Simp ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS; 1436183704Smav else 1437183704Smav ivar->hs_tran_speed = ivar->tran_speed; 1438183704Smav /* Find max supported bus width. */ 1439183704Smav ivar->bus_width = mmc_test_bus_width(sc); 1440183704Smav mmc_select_card(sc, 0); 1441184033Smav /* Handle HC erase sector size. */ 1442184033Smav if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) { 1443184033Smav ivar->erase_sector = 1024 * 1444184033Smav ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE]; 1445184033Smav mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, 1446184033Smav EXT_CSD_ERASE_GRP_DEF, 1); 1447184033Smav } 1448183704Smav } else { 1449183704Smav ivar->bus_width = bus_width_1; 1450183704Smav ivar->timing = bus_timing_normal; 1451183704Smav } 1452236156Smarius 1453236156Smarius /* 1454236156Smarius * Some cards that report maximum I/O block sizes greater 1455236156Smarius * than 512 require the block length to be set to 512, even 1456236156Smarius * though that is supposed to be the default. Example: 1457236156Smarius * 1458236156Smarius * Transcend 2GB SDSC card, CID: 1459236156Smarius * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000 1460236156Smarius */ 1461236156Smarius if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE || 1462236156Smarius ivar->csd.write_bl_len != MMC_SECTOR_SIZE) 1463236156Smarius mmc_set_blocklen(sc, MMC_SECTOR_SIZE); 1464236156Smarius 1465234524Smarius mmc_format_card_id_string(ivar); 1466236156Smarius 1467187875Smav if (bootverbose || mmc_debug) 1468187875Smav mmc_log_card(sc->dev, ivar, newcard); 1469185721Smav if (newcard) { 1470185721Smav /* Add device. */ 1471185721Smav child = device_add_child(sc->dev, NULL, -1); 1472185721Smav device_set_ivars(child, ivar); 1473185721Smav } 1474163516Simp } 1475163516Simp} 1476163516Simp 1477163516Simpstatic void 1478185721Smavmmc_rescan_cards(struct mmc_softc *sc) 1479185721Smav{ 1480185721Smav struct mmc_ivars *ivar = NULL; 1481185721Smav device_t *devlist; 1482185721Smav int err, i, devcount; 1483185721Smav 1484185721Smav if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 1485185721Smav return; 1486185721Smav for (i = 0; i < devcount; i++) { 1487185721Smav ivar = device_get_ivars(devlist[i]); 1488185721Smav if (mmc_select_card(sc, ivar->rca)) { 1489187875Smav if (bootverbose || mmc_debug) 1490187875Smav device_printf(sc->dev, "Card at relative address %d lost.\n", 1491187875Smav ivar->rca); 1492185721Smav device_delete_child(sc->dev, devlist[i]); 1493185721Smav free(ivar, M_DEVBUF); 1494185721Smav } 1495185721Smav } 1496185721Smav free(devlist, M_TEMP); 1497185721Smav mmc_select_card(sc, 0); 1498185721Smav} 1499185721Smav 1500185721Smavstatic int 1501185721Smavmmc_delete_cards(struct mmc_softc *sc) 1502185721Smav{ 1503185721Smav struct mmc_ivars *ivar; 1504185721Smav device_t *devlist; 1505185721Smav int err, i, devcount; 1506185721Smav 1507185721Smav if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 1508185721Smav return (err); 1509185721Smav for (i = 0; i < devcount; i++) { 1510185721Smav ivar = device_get_ivars(devlist[i]); 1511187875Smav if (bootverbose || mmc_debug) 1512187875Smav device_printf(sc->dev, "Card at relative address %d deleted.\n", 1513187875Smav ivar->rca); 1514185721Smav device_delete_child(sc->dev, devlist[i]); 1515185721Smav free(ivar, M_DEVBUF); 1516185721Smav } 1517185721Smav free(devlist, M_TEMP); 1518185721Smav return (0); 1519185721Smav} 1520185721Smav 1521185721Smavstatic void 1522163516Simpmmc_go_discovery(struct mmc_softc *sc) 1523163516Simp{ 1524163516Simp uint32_t ocr; 1525163516Simp device_t dev; 1526183704Smav int err; 1527163516Simp 1528163516Simp dev = sc->dev; 1529163516Simp if (mmcbr_get_power_mode(dev) != power_on) { 1530183453Simp /* 1531183453Simp * First, try SD modes 1532183453Simp */ 1533163516Simp mmcbr_set_mode(dev, mode_sd); 1534163516Simp mmc_power_up(sc); 1535163516Simp mmcbr_set_bus_mode(dev, pushpull); 1536187875Smav if (bootverbose || mmc_debug) 1537187875Smav device_printf(sc->dev, "Probing bus\n"); 1538163516Simp mmc_idle_cards(sc); 1539183704Smav err = mmc_send_if_cond(sc, 1); 1540187875Smav if ((bootverbose || mmc_debug) && err == 0) 1541187875Smav device_printf(sc->dev, "SD 2.0 interface conditions: OK\n"); 1542236156Smarius if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 1543187875Smav if (bootverbose || mmc_debug) 1544187875Smav device_printf(sc->dev, "SD probe: failed\n"); 1545183453Simp /* 1546183453Simp * Failed, try MMC 1547183453Simp */ 1548163516Simp mmcbr_set_mode(dev, mode_mmc); 1549187875Smav if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 1550187875Smav if (bootverbose || mmc_debug) 1551187875Smav device_printf(sc->dev, "MMC probe: failed\n"); 1552185721Smav ocr = 0; /* Failed both, powerdown. */ 1553187875Smav } else if (bootverbose || mmc_debug) 1554187875Smav device_printf(sc->dev, 1555187875Smav "MMC probe: OK (OCR: 0x%08x)\n", ocr); 1556187875Smav } else if (bootverbose || mmc_debug) 1557187875Smav device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr); 1558187875Smav 1559163516Simp mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr)); 1560163516Simp if (mmcbr_get_ocr(dev) != 0) 1561163516Simp mmc_idle_cards(sc); 1562163516Simp } else { 1563163516Simp mmcbr_set_bus_mode(dev, opendrain); 1564254427Sian mmcbr_set_clock(dev, CARD_ID_FREQUENCY); 1565163516Simp mmcbr_update_ios(dev); 1566183453Simp /* XXX recompute vdd based on new cards? */ 1567163516Simp } 1568163516Simp /* 1569163516Simp * Make sure that we have a mutually agreeable voltage to at least 1570163516Simp * one card on the bus. 1571163516Simp */ 1572187875Smav if (bootverbose || mmc_debug) 1573187875Smav device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev)); 1574185721Smav if (mmcbr_get_ocr(dev) == 0) { 1575185721Smav mmc_delete_cards(sc); 1576185721Smav mmc_power_down(sc); 1577163516Simp return; 1578185721Smav } 1579163516Simp /* 1580163516Simp * Reselect the cards after we've idled them above. 1581163516Simp */ 1582183704Smav if (mmcbr_get_mode(dev) == mode_sd) { 1583183704Smav err = mmc_send_if_cond(sc, 1); 1584183704Smav mmc_send_app_op_cond(sc, 1585183775Simp (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL); 1586183704Smav } else 1587163516Simp mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL); 1588163516Simp mmc_discover_cards(sc); 1589185721Smav mmc_rescan_cards(sc); 1590163516Simp 1591163516Simp mmcbr_set_bus_mode(dev, pushpull); 1592163516Simp mmcbr_update_ios(dev); 1593183763Smav mmc_calculate_clock(sc); 1594163516Simp bus_generic_attach(dev); 1595183453Simp/* mmc_update_children_sysctl(dev);*/ 1596163516Simp} 1597163516Simp 1598163516Simpstatic int 1599163516Simpmmc_calculate_clock(struct mmc_softc *sc) 1600163516Simp{ 1601183704Smav int max_dtr, max_hs_dtr, max_timing; 1602254427Sian int nkid, i, f_max; 1603163516Simp device_t *kids; 1604183704Smav struct mmc_ivars *ivar; 1605163516Simp 1606163516Simp f_max = mmcbr_get_f_max(sc->dev); 1607183704Smav max_dtr = max_hs_dtr = f_max; 1608183704Smav if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED)) 1609183704Smav max_timing = bus_timing_hs; 1610183704Smav else 1611183704Smav max_timing = bus_timing_normal; 1612163516Simp if (device_get_children(sc->dev, &kids, &nkid) != 0) 1613163516Simp panic("can't get children"); 1614183704Smav for (i = 0; i < nkid; i++) { 1615183704Smav ivar = device_get_ivars(kids[i]); 1616183704Smav if (ivar->timing < max_timing) 1617183704Smav max_timing = ivar->timing; 1618183704Smav if (ivar->tran_speed < max_dtr) 1619183704Smav max_dtr = ivar->tran_speed; 1620187525Smav if (ivar->hs_tran_speed < max_hs_dtr) 1621183704Smav max_hs_dtr = ivar->hs_tran_speed; 1622183704Smav } 1623183704Smav for (i = 0; i < nkid; i++) { 1624183704Smav ivar = device_get_ivars(kids[i]); 1625183704Smav if (ivar->timing == bus_timing_normal) 1626183704Smav continue; 1627183704Smav mmc_select_card(sc, ivar->rca); 1628183704Smav mmc_set_timing(sc, max_timing); 1629183704Smav } 1630183704Smav mmc_select_card(sc, 0); 1631163516Simp free(kids, M_TEMP); 1632183704Smav if (max_timing == bus_timing_hs) 1633183704Smav max_dtr = max_hs_dtr; 1634187875Smav if (bootverbose || mmc_debug) { 1635183775Simp device_printf(sc->dev, 1636183775Simp "setting transfer rate to %d.%03dMHz%s\n", 1637183763Smav max_dtr / 1000000, (max_dtr / 1000) % 1000, 1638183775Simp max_timing == bus_timing_hs ? " (high speed timing)" : ""); 1639183763Smav } 1640183704Smav mmcbr_set_timing(sc->dev, max_timing); 1641183704Smav mmcbr_set_clock(sc->dev, max_dtr); 1642183704Smav mmcbr_update_ios(sc->dev); 1643183704Smav return max_dtr; 1644163516Simp} 1645163516Simp 1646163516Simpstatic void 1647163516Simpmmc_scan(struct mmc_softc *sc) 1648163516Simp{ 1649185721Smav device_t dev = sc->dev; 1650163516Simp 1651163516Simp mmc_acquire_bus(dev, dev); 1652163516Simp mmc_go_discovery(sc); 1653163516Simp mmc_release_bus(dev, dev); 1654163516Simp} 1655163516Simp 1656163516Simpstatic int 1657189727Simpmmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result) 1658163516Simp{ 1659163516Simp struct mmc_ivars *ivar = device_get_ivars(child); 1660163516Simp 1661163516Simp switch (which) { 1662163516Simp default: 1663163516Simp return (EINVAL); 1664163516Simp case MMC_IVAR_DSR_IMP: 1665222475Sjchandra *result = ivar->csd.dsr_imp; 1666163516Simp break; 1667163516Simp case MMC_IVAR_MEDIA_SIZE: 1668222475Sjchandra *result = ivar->sec_count; 1669163516Simp break; 1670163516Simp case MMC_IVAR_RCA: 1671222475Sjchandra *result = ivar->rca; 1672163516Simp break; 1673163516Simp case MMC_IVAR_SECTOR_SIZE: 1674222475Sjchandra *result = MMC_SECTOR_SIZE; 1675163516Simp break; 1676163516Simp case MMC_IVAR_TRAN_SPEED: 1677222475Sjchandra *result = mmcbr_get_clock(bus); 1678163516Simp break; 1679183447Simp case MMC_IVAR_READ_ONLY: 1680222475Sjchandra *result = ivar->read_only; 1681183447Simp break; 1682183704Smav case MMC_IVAR_HIGH_CAP: 1683222475Sjchandra *result = ivar->high_cap; 1684183704Smav break; 1685183763Smav case MMC_IVAR_CARD_TYPE: 1686222475Sjchandra *result = ivar->mode; 1687183763Smav break; 1688183763Smav case MMC_IVAR_BUS_WIDTH: 1689222475Sjchandra *result = ivar->bus_width; 1690183763Smav break; 1691184033Smav case MMC_IVAR_ERASE_SECTOR: 1692222475Sjchandra *result = ivar->erase_sector; 1693184033Smav break; 1694184452Smav case MMC_IVAR_MAX_DATA: 1695222475Sjchandra *result = mmcbr_get_max_data(bus); 1696184452Smav break; 1697234524Smarius case MMC_IVAR_CARD_ID_STRING: 1698234524Smarius *(char **)result = ivar->card_id_string; 1699234524Smarius break; 1700163516Simp } 1701163516Simp return (0); 1702163516Simp} 1703163516Simp 1704163516Simpstatic int 1705163516Simpmmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 1706163516Simp{ 1707183453Simp /* 1708183453Simp * None are writable ATM 1709183453Simp */ 1710183453Simp return (EINVAL); 1711163516Simp} 1712163516Simp 1713163516Simpstatic void 1714163516Simpmmc_delayed_attach(void *xsc) 1715163516Simp{ 1716163516Simp struct mmc_softc *sc = xsc; 1717163516Simp 1718163516Simp mmc_scan(sc); 1719163516Simp config_intrhook_disestablish(&sc->config_intrhook); 1720163516Simp} 1721163516Simp 1722208441Smavstatic int 1723208441Smavmmc_child_location_str(device_t dev, device_t child, char *buf, 1724208441Smav size_t buflen) 1725208441Smav{ 1726208441Smav 1727208441Smav snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child)); 1728208441Smav return (0); 1729208441Smav} 1730208441Smav 1731163516Simpstatic device_method_t mmc_methods[] = { 1732163516Simp /* device_if */ 1733163516Simp DEVMETHOD(device_probe, mmc_probe), 1734163516Simp DEVMETHOD(device_attach, mmc_attach), 1735163516Simp DEVMETHOD(device_detach, mmc_detach), 1736185721Smav DEVMETHOD(device_suspend, mmc_suspend), 1737185721Smav DEVMETHOD(device_resume, mmc_resume), 1738163516Simp 1739163516Simp /* Bus interface */ 1740163516Simp DEVMETHOD(bus_read_ivar, mmc_read_ivar), 1741163516Simp DEVMETHOD(bus_write_ivar, mmc_write_ivar), 1742208441Smav DEVMETHOD(bus_child_location_str, mmc_child_location_str), 1743163516Simp 1744163516Simp /* MMC Bus interface */ 1745163516Simp DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request), 1746163516Simp DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus), 1747163516Simp DEVMETHOD(mmcbus_release_bus, mmc_release_bus), 1748163516Simp 1749234524Smarius DEVMETHOD_END 1750163516Simp}; 1751163516Simp 1752163516Simpstatic driver_t mmc_driver = { 1753163516Simp "mmc", 1754163516Simp mmc_methods, 1755163516Simp sizeof(struct mmc_softc), 1756163516Simp}; 1757163516Simpstatic devclass_t mmc_devclass; 1758163516Simp 1759239281SgonzoDRIVER_MODULE(mmc, ti_mmchs, mmc_driver, mmc_devclass, NULL, NULL); 1760188044SimpDRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL); 1761241600SgonzoDRIVER_MODULE(mmc, sdhci_pci, mmc_driver, mmc_devclass, NULL, NULL); 1762242321SgonzoDRIVER_MODULE(mmc, sdhci_bcm, mmc_driver, mmc_devclass, NULL, NULL); 1763249999SwkoszekDRIVER_MODULE(mmc, sdhci_fdt, mmc_driver, mmc_devclass, NULL, NULL); 1764254559SianDRIVER_MODULE(mmc, sdhci_ti, mmc_driver, mmc_devclass, NULL, NULL); 1765