mmc.c revision 318494
1163516Simp/*- 2163516Simp * Copyright (c) 2006 Bernd Walter. All rights reserved. 3163516Simp * Copyright (c) 2006 M. Warner Losh. All rights reserved. 4318494Smarius * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org> 5163516Simp * 6163516Simp * Redistribution and use in source and binary forms, with or without 7163516Simp * modification, are permitted provided that the following conditions 8163516Simp * are met: 9163516Simp * 1. Redistributions of source code must retain the above copyright 10163516Simp * notice, this list of conditions and the following disclaimer. 11163516Simp * 2. Redistributions in binary form must reproduce the above copyright 12163516Simp * notice, this list of conditions and the following disclaimer in the 13163516Simp * documentation and/or other materials provided with the distribution. 14163516Simp * 15163516Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16163516Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17163516Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18163516Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19163516Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20163516Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21163516Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22163516Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23163516Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24163516Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25170002Simp * 26170002Simp * Portions of this software may have been developed with reference to 27170002Simp * the SD Simplified Specification. The following disclaimer may apply: 28170002Simp * 29170002Simp * The following conditions apply to the release of the simplified 30170002Simp * specification ("Simplified Specification") by the SD Card Association and 31170002Simp * the SD Group. The Simplified Specification is a subset of the complete SD 32170002Simp * Specification which is owned by the SD Card Association and the SD 33170002Simp * Group. This Simplified Specification is provided on a non-confidential 34170002Simp * basis subject to the disclaimers below. Any implementation of the 35170002Simp * Simplified Specification may require a license from the SD Card 36170002Simp * Association, SD Group, SD-3C LLC or other third parties. 37170002Simp * 38170002Simp * Disclaimers: 39170002Simp * 40170002Simp * The information contained in the Simplified Specification is presented only 41170002Simp * as a standard specification for SD Cards and SD Host/Ancillary products and 42170002Simp * is provided "AS-IS" without any representations or warranties of any 43170002Simp * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 44170002Simp * Card Association for any damages, any infringements of patents or other 45170002Simp * right of the SD Group, SD-3C LLC, the SD Card Association or any third 46170002Simp * parties, which may result from its use. No license is granted by 47170002Simp * implication, estoppel or otherwise under any patent or other rights of the 48170002Simp * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 49170002Simp * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 50170002Simp * or the SD Card Association to disclose or distribute any technical 51170002Simp * information, know-how or other confidential information to any third party. 52163516Simp */ 53163516Simp 54163516Simp#include <sys/cdefs.h> 55163516Simp__FBSDID("$FreeBSD: stable/11/sys/dev/mmc/mmc.c 318494 2017-05-18 20:46:20Z marius $"); 56163516Simp 57163516Simp#include <sys/param.h> 58163516Simp#include <sys/systm.h> 59163516Simp#include <sys/kernel.h> 60163516Simp#include <sys/malloc.h> 61163516Simp#include <sys/lock.h> 62163516Simp#include <sys/module.h> 63163516Simp#include <sys/mutex.h> 64163516Simp#include <sys/bus.h> 65183704Smav#include <sys/endian.h> 66187875Smav#include <sys/sysctl.h> 67275951Sian#include <sys/time.h> 68163516Simp 69318197Smarius#include <dev/mmc/bridge.h> 70318197Smarius#include <dev/mmc/mmc_private.h> 71318197Smarius#include <dev/mmc/mmc_subr.h> 72163516Simp#include <dev/mmc/mmcreg.h> 73163516Simp#include <dev/mmc/mmcbrvar.h> 74163516Simp#include <dev/mmc/mmcvar.h> 75318197Smarius 76163516Simp#include "mmcbr_if.h" 77163516Simp#include "mmcbus_if.h" 78163516Simp 79318494SmariusCTASSERT(bus_timing_max <= sizeof(uint32_t) * NBBY); 80318494Smarius 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 */ 88318197Smarius uint8_t raw_ext_csd[MMC_EXTCSD_SIZE]; /* 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 */ 98183731Smav u_char high_cap; /* High Capacity card (block addressed) */ 99183731Smav uint32_t sec_count; /* Card capacity in 512byte blocks */ 100318494Smarius uint32_t timings; /* Mask of bus timings supported */ 101318494Smarius uint32_t vccq_120; /* Mask of bus timings at VCCQ of 1.2 V */ 102318494Smarius uint32_t vccq_180; /* Mask of bus timings at VCCQ of 1.8 V */ 103183704Smav uint32_t tran_speed; /* Max speed in normal mode */ 104183704Smav uint32_t hs_tran_speed; /* Max speed in high speed mode */ 105184033Smav uint32_t erase_sector; /* Card native erase sector size */ 106318197Smarius uint32_t cmd6_time; /* Generic switch timeout [us] */ 107234524Smarius char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */ 108269341Sian char card_sn_string[16];/* Formatted serial # for disk->d_ident */ 109163516Simp}; 110163516Simp 111318197Smarius#define CMD_RETRIES 3 112163516Simp 113227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver"); 114187875Smav 115188044Simpstatic int mmc_debug; 116318197SmariusSYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &mmc_debug, 0, 117318197Smarius "Debug level"); 118187875Smav 119163516Simp/* bus entry points */ 120236491Smariusstatic int mmc_acquire_bus(device_t busdev, device_t dev); 121163516Simpstatic int mmc_attach(device_t dev); 122236491Smariusstatic int mmc_child_location_str(device_t dev, device_t child, char *buf, 123236491Smarius size_t buflen); 124163516Simpstatic int mmc_detach(device_t dev); 125236491Smariusstatic int mmc_probe(device_t dev); 126236491Smariusstatic int mmc_read_ivar(device_t bus, device_t child, int which, 127236491Smarius uintptr_t *result); 128236491Smariusstatic int mmc_release_bus(device_t busdev, device_t dev); 129236491Smariusstatic int mmc_resume(device_t dev); 130185721Smavstatic int mmc_suspend(device_t dev); 131236491Smariusstatic int mmc_wait_for_request(device_t brdev, device_t reqdev, 132236491Smarius struct mmc_request *req); 133236491Smariusstatic int mmc_write_ivar(device_t bus, device_t child, int which, 134236491Smarius uintptr_t value); 135163516Simp 136318197Smarius#define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 137163516Simp#define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 138318197Smarius#define MMC_LOCK_INIT(_sc) \ 139318197Smarius mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->dev), \ 140163516Simp "mmc", MTX_DEF) 141318197Smarius#define MMC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx); 142318197Smarius#define MMC_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED); 143318197Smarius#define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED); 144163516Simp 145236491Smariusstatic int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid); 146236491Smariusstatic void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr); 147236491Smariusstatic void mmc_app_decode_sd_status(uint32_t *raw_sd_status, 148236491Smarius struct mmc_sd_status *sd_status); 149236491Smariusstatic int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, 150236491Smarius uint32_t *rawsdstatus); 151236491Smariusstatic int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, 152236491Smarius uint32_t *rawscr); 153183763Smavstatic int mmc_calculate_clock(struct mmc_softc *sc); 154318197Smariusstatic void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid, 155318197Smarius bool is_4_41p); 156236491Smariusstatic void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid); 157236491Smariusstatic void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd); 158236491Smariusstatic void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd); 159236491Smariusstatic void mmc_delayed_attach(void *xsc); 160236491Smariusstatic int mmc_delete_cards(struct mmc_softc *sc); 161236491Smariusstatic void mmc_discover_cards(struct mmc_softc *sc); 162236491Smariusstatic void mmc_format_card_id_string(struct mmc_ivars *ivar); 163236491Smariusstatic void mmc_go_discovery(struct mmc_softc *sc); 164236491Smariusstatic uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start, 165236491Smarius int size); 166236491Smariusstatic int mmc_highest_voltage(uint32_t ocr); 167236491Smariusstatic void mmc_idle_cards(struct mmc_softc *sc); 168236491Smariusstatic void mmc_ms_delay(int ms); 169236491Smariusstatic void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard); 170183449Simpstatic void mmc_power_down(struct mmc_softc *sc); 171236491Smariusstatic void mmc_power_up(struct mmc_softc *sc); 172236491Smariusstatic void mmc_rescan_cards(struct mmc_softc *sc); 173236491Smariusstatic void mmc_scan(struct mmc_softc *sc); 174236491Smariusstatic int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, 175236491Smarius uint8_t value, uint8_t *res); 176236491Smariusstatic int mmc_select_card(struct mmc_softc *sc, uint16_t rca); 177236491Smariusstatic uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr); 178236491Smariusstatic int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, 179236491Smarius uint32_t *rocr); 180236491Smariusstatic int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd); 181236491Smariusstatic int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs); 182236491Smariusstatic int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, 183236491Smarius uint32_t *rocr); 184236491Smariusstatic int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp); 185236491Smariusstatic int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len); 186318494Smariusstatic int mmc_set_card_bus_width(struct mmc_softc *sc, struct mmc_ivars *ivar); 187318494Smariusstatic int mmc_set_power_class(struct mmc_softc *sc, struct mmc_ivars *ivar); 188236491Smariusstatic int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp); 189318197Smariusstatic int mmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar, 190318494Smarius enum mmc_bus_timing timing); 191236491Smariusstatic int mmc_test_bus_width(struct mmc_softc *sc); 192318494Smariusstatic uint32_t mmc_timing_to_dtr(struct mmc_ivars *ivar, 193318494Smarius enum mmc_bus_timing timing); 194318494Smariusstatic const char *mmc_timing_to_string(enum mmc_bus_timing timing); 195163516Simpstatic int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 196163516Simp uint32_t arg, uint32_t flags, uint32_t *resp, int retries); 197236491Smariusstatic int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req); 198236491Smariusstatic void mmc_wakeup(struct mmc_request *req); 199163516Simp 200163516Simpstatic void 201163516Simpmmc_ms_delay(int ms) 202163516Simp{ 203236491Smarius 204163516Simp DELAY(1000 * ms); /* XXX BAD */ 205163516Simp} 206163516Simp 207163516Simpstatic int 208163516Simpmmc_probe(device_t dev) 209163516Simp{ 210163516Simp 211183445Simp device_set_desc(dev, "MMC/SD bus"); 212163516Simp return (0); 213163516Simp} 214163516Simp 215163516Simpstatic int 216163516Simpmmc_attach(device_t dev) 217163516Simp{ 218163516Simp struct mmc_softc *sc; 219163516Simp 220163516Simp sc = device_get_softc(dev); 221163516Simp sc->dev = dev; 222163516Simp MMC_LOCK_INIT(sc); 223163516Simp 224163516Simp /* We'll probe and attach our children later, but before / mount */ 225163516Simp sc->config_intrhook.ich_func = mmc_delayed_attach; 226163516Simp sc->config_intrhook.ich_arg = sc; 227163516Simp if (config_intrhook_establish(&sc->config_intrhook) != 0) 228163516Simp device_printf(dev, "config_intrhook_establish failed\n"); 229163516Simp return (0); 230163516Simp} 231163516Simp 232163516Simpstatic int 233163516Simpmmc_detach(device_t dev) 234163516Simp{ 235169567Simp struct mmc_softc *sc = device_get_softc(dev); 236185721Smav int err; 237169567Simp 238185721Smav if ((err = mmc_delete_cards(sc)) != 0) 239185721Smav return (err); 240183449Simp mmc_power_down(sc); 241169567Simp MMC_LOCK_DESTROY(sc); 242169567Simp 243183467Simp return (0); 244163516Simp} 245163516Simp 246163516Simpstatic int 247185721Smavmmc_suspend(device_t dev) 248185721Smav{ 249185721Smav struct mmc_softc *sc = device_get_softc(dev); 250185721Smav int err; 251185721Smav 252185721Smav err = bus_generic_suspend(dev); 253185721Smav if (err) 254318197Smarius return (err); 255185721Smav mmc_power_down(sc); 256185721Smav return (0); 257185721Smav} 258185721Smav 259185721Smavstatic int 260185721Smavmmc_resume(device_t dev) 261185721Smav{ 262185721Smav struct mmc_softc *sc = device_get_softc(dev); 263185721Smav 264185721Smav mmc_scan(sc); 265185721Smav return (bus_generic_resume(dev)); 266185721Smav} 267185721Smav 268185721Smavstatic int 269163516Simpmmc_acquire_bus(device_t busdev, device_t dev) 270163516Simp{ 271163516Simp struct mmc_softc *sc; 272183704Smav struct mmc_ivars *ivar; 273318494Smarius int err, rca; 274318494Smarius enum mmc_bus_timing timing; 275163516Simp 276183452Simp err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev); 277163516Simp if (err) 278163516Simp return (err); 279163516Simp sc = device_get_softc(busdev); 280163516Simp MMC_LOCK(sc); 281163516Simp if (sc->owner) 282236156Smarius panic("mmc: host bridge didn't serialize us."); 283163516Simp sc->owner = dev; 284163516Simp MMC_UNLOCK(sc); 285163516Simp 286163516Simp if (busdev != dev) { 287183453Simp /* 288183453Simp * Keep track of the last rca that we've selected. If 289183453Simp * we're asked to do it again, don't. We never 290183453Simp * unselect unless the bus code itself wants the mmc 291183453Simp * bus, and constantly reselecting causes problems. 292183453Simp */ 293318197Smarius ivar = device_get_ivars(dev); 294318197Smarius rca = ivar->rca; 295163516Simp if (sc->last_rca != rca) { 296318494Smarius if (mmc_select_card(sc, rca) != MMC_ERR_NONE) { 297318494Smarius device_printf(sc->dev, "Card at relative " 298318494Smarius "address %d failed to select.\n", rca); 299318494Smarius return (ENXIO); 300318494Smarius } 301163516Simp sc->last_rca = rca; 302318494Smarius timing = mmcbr_get_timing(busdev); 303183704Smav /* Prepare bus width for the new card. */ 304187875Smav if (bootverbose || mmc_debug) { 305183763Smav device_printf(busdev, 306318494Smarius "setting bus width to %d bits %s timing\n", 307183775Simp (ivar->bus_width == bus_width_4) ? 4 : 308318494Smarius (ivar->bus_width == bus_width_8) ? 8 : 1, 309318494Smarius mmc_timing_to_string(timing)); 310183763Smav } 311318494Smarius if (mmc_set_card_bus_width(sc, ivar) != MMC_ERR_NONE) { 312318494Smarius device_printf(sc->dev, "Card at relative " 313318494Smarius "address %d failed to set bus width.\n", 314318494Smarius rca); 315318494Smarius return (ENXIO); 316318494Smarius } 317318494Smarius if (isset(&ivar->vccq_120, timing)) 318318494Smarius mmcbr_set_vccq(busdev, vccq_120); 319318494Smarius else if (isset(&ivar->vccq_180, timing)) 320318494Smarius mmcbr_set_vccq(busdev, vccq_180); 321318494Smarius else 322318494Smarius mmcbr_set_vccq(busdev, vccq_330); 323318494Smarius if (mmcbr_switch_vccq(busdev) != 0) { 324318494Smarius device_printf(sc->dev, "Failed to set VCCQ " 325318494Smarius "for card at relative address %d.\n", rca); 326318494Smarius return (ENXIO); 327318494Smarius } 328318494Smarius if (mmc_set_power_class(sc, ivar) != MMC_ERR_NONE) { 329318494Smarius device_printf(sc->dev, "Card at relative " 330318494Smarius "address %d failed to set power class.\n", 331318494Smarius rca); 332318494Smarius return (ENXIO); 333318494Smarius } 334183704Smav mmcbr_set_bus_width(busdev, ivar->bus_width); 335183704Smav mmcbr_update_ios(busdev); 336163516Simp } 337163516Simp } else { 338183453Simp /* 339183453Simp * If there's a card selected, stand down. 340183453Simp */ 341163516Simp if (sc->last_rca != 0) { 342183704Smav mmc_select_card(sc, 0); 343163516Simp sc->last_rca = 0; 344163516Simp } 345163516Simp } 346163516Simp 347163516Simp return (0); 348163516Simp} 349163516Simp 350163516Simpstatic int 351163516Simpmmc_release_bus(device_t busdev, device_t dev) 352163516Simp{ 353163516Simp struct mmc_softc *sc; 354163516Simp int err; 355163516Simp 356163516Simp sc = device_get_softc(busdev); 357163516Simp 358163516Simp MMC_LOCK(sc); 359163516Simp if (!sc->owner) 360163516Simp panic("mmc: releasing unowned bus."); 361163516Simp if (sc->owner != dev) 362163516Simp panic("mmc: you don't own the bus. game over."); 363163516Simp MMC_UNLOCK(sc); 364183452Simp err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev); 365163516Simp if (err) 366163516Simp return (err); 367163516Simp MMC_LOCK(sc); 368163516Simp sc->owner = NULL; 369163516Simp MMC_UNLOCK(sc); 370163516Simp return (0); 371163516Simp} 372163516Simp 373163516Simpstatic uint32_t 374163516Simpmmc_select_vdd(struct mmc_softc *sc, uint32_t ocr) 375163516Simp{ 376183446Simp 377183467Simp return (ocr & MMC_OCR_VOLTAGE); 378163516Simp} 379163516Simp 380163516Simpstatic int 381163516Simpmmc_highest_voltage(uint32_t ocr) 382163516Simp{ 383163516Simp int i; 384163516Simp 385245755Sgonzo for (i = MMC_OCR_MAX_VOLTAGE_SHIFT; 386245755Sgonzo i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--) 387163516Simp if (ocr & (1 << i)) 388183467Simp return (i); 389163516Simp return (-1); 390163516Simp} 391163516Simp 392163516Simpstatic void 393163516Simpmmc_wakeup(struct mmc_request *req) 394163516Simp{ 395163516Simp struct mmc_softc *sc; 396163516Simp 397163516Simp sc = (struct mmc_softc *)req->done_data; 398163516Simp MMC_LOCK(sc); 399163516Simp req->flags |= MMC_REQ_DONE; 400185721Smav MMC_UNLOCK(sc); 401163516Simp wakeup(req); 402163516Simp} 403163516Simp 404163516Simpstatic int 405163516Simpmmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req) 406163516Simp{ 407163516Simp 408163516Simp req->done = mmc_wakeup; 409163516Simp req->done_data = sc; 410187877Smav if (mmc_debug > 1) { 411187875Smav device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x", 412187875Smav req->cmd->opcode, req->cmd->arg, req->cmd->flags); 413187875Smav if (req->cmd->data) { 414187875Smav printf(" data %d\n", (int)req->cmd->data->len); 415187875Smav } else 416187875Smav printf("\n"); 417187875Smav } 418163516Simp MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req); 419163516Simp MMC_LOCK(sc); 420185721Smav while ((req->flags & MMC_REQ_DONE) == 0) 421185721Smav msleep(req, &sc->sc_mtx, 0, "mmcreq", 0); 422163516Simp MMC_UNLOCK(sc); 423254431Sian if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE)) 424312399Smarius device_printf(sc->dev, "CMD%d RESULT: %d\n", 425254431Sian req->cmd->opcode, req->cmd->error); 426185721Smav return (0); 427163516Simp} 428163516Simp 429163516Simpstatic int 430318197Smariusmmc_wait_for_request(device_t brdev, device_t reqdev __unused, 431318197Smarius struct mmc_request *req) 432163516Simp{ 433163516Simp struct mmc_softc *sc = device_get_softc(brdev); 434163516Simp 435183467Simp return (mmc_wait_for_req(sc, req)); 436163516Simp} 437163516Simp 438163516Simpstatic int 439163516Simpmmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 440163516Simp uint32_t arg, uint32_t flags, uint32_t *resp, int retries) 441163516Simp{ 442163516Simp struct mmc_command cmd; 443163516Simp int err; 444163516Simp 445163516Simp memset(&cmd, 0, sizeof(cmd)); 446163516Simp cmd.opcode = opcode; 447163516Simp cmd.arg = arg; 448163516Simp cmd.flags = flags; 449183470Simp cmd.data = NULL; 450318197Smarius err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, retries); 451163516Simp if (err) 452163516Simp return (err); 453163516Simp if (resp) { 454163516Simp if (flags & MMC_RSP_136) 455163516Simp memcpy(resp, cmd.resp, 4 * sizeof(uint32_t)); 456163516Simp else 457163516Simp *resp = cmd.resp[0]; 458163516Simp } 459163516Simp return (0); 460163516Simp} 461163516Simp 462163516Simpstatic void 463163516Simpmmc_idle_cards(struct mmc_softc *sc) 464163516Simp{ 465163516Simp device_t dev; 466163516Simp struct mmc_command cmd; 467312399Smarius 468163516Simp dev = sc->dev; 469163516Simp mmcbr_set_chip_select(dev, cs_high); 470163516Simp mmcbr_update_ios(dev); 471163516Simp mmc_ms_delay(1); 472163516Simp 473163516Simp memset(&cmd, 0, sizeof(cmd)); 474163516Simp cmd.opcode = MMC_GO_IDLE_STATE; 475163516Simp cmd.arg = 0; 476163516Simp cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 477183470Simp cmd.data = NULL; 478318197Smarius mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 479163516Simp mmc_ms_delay(1); 480163516Simp 481163516Simp mmcbr_set_chip_select(dev, cs_dontcare); 482163516Simp mmcbr_update_ios(dev); 483163516Simp mmc_ms_delay(1); 484163516Simp} 485163516Simp 486163516Simpstatic int 487163516Simpmmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 488163516Simp{ 489163516Simp struct mmc_command cmd; 490163516Simp int err = MMC_ERR_NONE, i; 491163516Simp 492163516Simp memset(&cmd, 0, sizeof(cmd)); 493163516Simp cmd.opcode = ACMD_SD_SEND_OP_COND; 494163516Simp cmd.arg = ocr; 495163516Simp cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 496183470Simp cmd.data = NULL; 497163516Simp 498216941Spjd for (i = 0; i < 1000; i++) { 499318197Smarius err = mmc_wait_for_app_cmd(sc->dev, sc->dev, 0, &cmd, 500318197Smarius CMD_RETRIES); 501163516Simp if (err != MMC_ERR_NONE) 502163516Simp break; 503183709Smav if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 504183709Smav (ocr & MMC_OCR_VOLTAGE) == 0) 505163516Simp break; 506163516Simp err = MMC_ERR_TIMEOUT; 507163516Simp mmc_ms_delay(10); 508163516Simp } 509163516Simp if (rocr && err == MMC_ERR_NONE) 510163516Simp *rocr = cmd.resp[0]; 511183467Simp return (err); 512163516Simp} 513163516Simp 514163516Simpstatic int 515163516Simpmmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 516163516Simp{ 517163516Simp struct mmc_command cmd; 518163516Simp int err = MMC_ERR_NONE, i; 519163516Simp 520163516Simp memset(&cmd, 0, sizeof(cmd)); 521163516Simp cmd.opcode = MMC_SEND_OP_COND; 522163516Simp cmd.arg = ocr; 523163516Simp cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 524183470Simp cmd.data = NULL; 525163516Simp 526216941Spjd for (i = 0; i < 1000; i++) { 527318197Smarius err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 528163516Simp if (err != MMC_ERR_NONE) 529163516Simp break; 530183709Smav if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 531183709Smav (ocr & MMC_OCR_VOLTAGE) == 0) 532163516Simp break; 533163516Simp err = MMC_ERR_TIMEOUT; 534163516Simp mmc_ms_delay(10); 535163516Simp } 536163516Simp if (rocr && err == MMC_ERR_NONE) 537163516Simp *rocr = cmd.resp[0]; 538183467Simp return (err); 539163516Simp} 540163516Simp 541183704Smavstatic int 542183704Smavmmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs) 543183704Smav{ 544183704Smav struct mmc_command cmd; 545183704Smav int err; 546183704Smav 547183704Smav memset(&cmd, 0, sizeof(cmd)); 548183704Smav cmd.opcode = SD_SEND_IF_COND; 549183704Smav cmd.arg = (vhs << 8) + 0xAA; 550183704Smav cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR; 551183704Smav cmd.data = NULL; 552183704Smav 553318197Smarius err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 554183704Smav return (err); 555183704Smav} 556183704Smav 557163516Simpstatic void 558163516Simpmmc_power_up(struct mmc_softc *sc) 559163516Simp{ 560163516Simp device_t dev; 561318494Smarius enum mmc_vccq vccq; 562163516Simp 563163516Simp dev = sc->dev; 564163516Simp mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev))); 565163516Simp mmcbr_set_bus_mode(dev, opendrain); 566163516Simp mmcbr_set_chip_select(dev, cs_dontcare); 567163516Simp mmcbr_set_bus_width(dev, bus_width_1); 568163516Simp mmcbr_set_power_mode(dev, power_up); 569163516Simp mmcbr_set_clock(dev, 0); 570163516Simp mmcbr_update_ios(dev); 571318494Smarius for (vccq = vccq_330; ; vccq--) { 572318494Smarius mmcbr_set_vccq(dev, vccq); 573318494Smarius if (mmcbr_switch_vccq(dev) == 0 || vccq == vccq_120) 574318494Smarius break; 575318494Smarius } 576163516Simp mmc_ms_delay(1); 577163516Simp 578318494Smarius mmcbr_set_clock(dev, SD_MMC_CARD_ID_FREQUENCY); 579183704Smav mmcbr_set_timing(dev, bus_timing_normal); 580163516Simp mmcbr_set_power_mode(dev, power_on); 581163516Simp mmcbr_update_ios(dev); 582163516Simp mmc_ms_delay(2); 583163516Simp} 584163516Simp 585183449Simpstatic void 586183449Simpmmc_power_down(struct mmc_softc *sc) 587183449Simp{ 588183449Simp device_t dev = sc->dev; 589183449Simp 590183449Simp mmcbr_set_bus_mode(dev, opendrain); 591183449Simp mmcbr_set_chip_select(dev, cs_dontcare); 592183449Simp mmcbr_set_bus_width(dev, bus_width_1); 593183449Simp mmcbr_set_power_mode(dev, power_off); 594183449Simp mmcbr_set_clock(dev, 0); 595183704Smav mmcbr_set_timing(dev, bus_timing_normal); 596183449Simp mmcbr_update_ios(dev); 597183449Simp} 598183449Simp 599183704Smavstatic int 600183704Smavmmc_select_card(struct mmc_softc *sc, uint16_t rca) 601183704Smav{ 602183775Simp int flags; 603183775Simp 604183775Simp flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC; 605183775Simp return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16, 606183775Simp flags, NULL, CMD_RETRIES)); 607183704Smav} 608183704Smav 609183704Smavstatic int 610188044Simpmmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value, 611188044Simp uint8_t *res) 612183704Smav{ 613183704Smav int err; 614183704Smav struct mmc_command cmd; 615183704Smav struct mmc_data data; 616183704Smav 617254432Sian memset(&cmd, 0, sizeof(cmd)); 618254432Sian memset(&data, 0, sizeof(data)); 619188044Simp memset(res, 0, 64); 620183704Smav 621183704Smav cmd.opcode = SD_SWITCH_FUNC; 622183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 623188044Simp cmd.arg = mode << 31; /* 0 - check, 1 - set */ 624183704Smav cmd.arg |= 0x00FFFFFF; 625183705Smav cmd.arg &= ~(0xF << (grp * 4)); 626183705Smav cmd.arg |= value << (grp * 4); 627183704Smav cmd.data = &data; 628183704Smav 629183704Smav data.data = res; 630183704Smav data.len = 64; 631183704Smav data.flags = MMC_DATA_READ; 632183704Smav 633318197Smarius err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 634183704Smav return (err); 635183704Smav} 636183704Smav 637183704Smavstatic int 638318197Smariusmmc_set_card_bus_width(struct mmc_softc *sc, struct mmc_ivars *ivar) 639183704Smav{ 640187546Simp struct mmc_command cmd; 641183704Smav int err; 642187546Simp uint8_t value; 643183704Smav 644183704Smav if (mmcbr_get_mode(sc->dev) == mode_sd) { 645254432Sian memset(&cmd, 0, sizeof(cmd)); 646234524Smarius cmd.opcode = ACMD_SET_CLR_CARD_DETECT; 647234524Smarius cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 648234524Smarius cmd.arg = SD_CLR_CARD_DETECT; 649318197Smarius err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd, 650318197Smarius CMD_RETRIES); 651234524Smarius if (err != 0) 652234524Smarius return (err); 653254432Sian memset(&cmd, 0, sizeof(cmd)); 654183704Smav cmd.opcode = ACMD_SET_BUS_WIDTH; 655183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 656318197Smarius switch (ivar->bus_width) { 657183704Smav case bus_width_1: 658183704Smav cmd.arg = SD_BUS_WIDTH_1; 659183704Smav break; 660183704Smav case bus_width_4: 661183704Smav cmd.arg = SD_BUS_WIDTH_4; 662183704Smav break; 663183704Smav default: 664183704Smav return (MMC_ERR_INVALID); 665183704Smav } 666318197Smarius err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd, 667318197Smarius CMD_RETRIES); 668183704Smav } else { 669318197Smarius switch (ivar->bus_width) { 670183704Smav case bus_width_1: 671183704Smav value = EXT_CSD_BUS_WIDTH_1; 672183704Smav break; 673183704Smav case bus_width_4: 674318494Smarius switch (mmcbr_get_timing(sc->dev)) { 675318494Smarius case bus_timing_mmc_ddr52: 676318494Smarius case bus_timing_mmc_hs200: 677318494Smarius case bus_timing_mmc_hs400: 678318494Smarius case bus_timing_mmc_hs400es: 679318494Smarius value = EXT_CSD_BUS_WIDTH_4_DDR; 680318494Smarius break; 681318494Smarius default: 682318494Smarius value = EXT_CSD_BUS_WIDTH_4; 683318494Smarius break; 684318494Smarius } 685183704Smav break; 686183704Smav case bus_width_8: 687318494Smarius switch (mmcbr_get_timing(sc->dev)) { 688318494Smarius case bus_timing_mmc_ddr52: 689318494Smarius case bus_timing_mmc_hs200: 690318494Smarius case bus_timing_mmc_hs400: 691318494Smarius case bus_timing_mmc_hs400es: 692318494Smarius value = EXT_CSD_BUS_WIDTH_8_DDR; 693318494Smarius break; 694318494Smarius default: 695318494Smarius value = EXT_CSD_BUS_WIDTH_8; 696318494Smarius break; 697318494Smarius } 698183704Smav break; 699183704Smav default: 700183704Smav return (MMC_ERR_INVALID); 701183704Smav } 702318197Smarius err = mmc_switch(sc->dev, sc->dev, ivar->rca, 703318197Smarius EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, value, 704318197Smarius ivar->cmd6_time, true); 705183704Smav } 706183704Smav return (err); 707183704Smav} 708183704Smav 709183704Smavstatic int 710318494Smariusmmc_set_power_class(struct mmc_softc *sc, struct mmc_ivars *ivar) 711183704Smav{ 712318494Smarius device_t dev; 713318494Smarius const uint8_t *ext_csd; 714318494Smarius uint32_t clock; 715318494Smarius uint8_t value; 716183704Smav 717318494Smarius dev = sc->dev; 718318494Smarius if (mmcbr_get_mode(dev) != mode_mmc || ivar->csd.spec_vers < 4) 719318494Smarius return (MMC_ERR_NONE); 720318494Smarius 721318494Smarius value = 0; 722318494Smarius ext_csd = ivar->raw_ext_csd; 723318494Smarius clock = mmcbr_get_clock(dev); 724318494Smarius switch (1 << mmcbr_get_vdd(dev)) { 725318494Smarius case MMC_OCR_LOW_VOLTAGE: 726318494Smarius if (clock <= MMC_TYPE_HS_26_MAX) 727318494Smarius value = ext_csd[EXT_CSD_PWR_CL_26_195]; 728318494Smarius else if (clock <= MMC_TYPE_HS_52_MAX) { 729318494Smarius if (mmcbr_get_timing(dev) >= bus_timing_mmc_ddr52 && 730318494Smarius ivar->bus_width >= bus_width_4) 731318494Smarius value = ext_csd[EXT_CSD_PWR_CL_52_195_DDR]; 732318494Smarius else 733318494Smarius value = ext_csd[EXT_CSD_PWR_CL_52_195]; 734318494Smarius } else if (clock <= MMC_TYPE_HS200_HS400ES_MAX) 735318494Smarius value = ext_csd[EXT_CSD_PWR_CL_200_195]; 736183704Smav break; 737318494Smarius case MMC_OCR_270_280: 738318494Smarius case MMC_OCR_280_290: 739318494Smarius case MMC_OCR_290_300: 740318494Smarius case MMC_OCR_300_310: 741318494Smarius case MMC_OCR_310_320: 742318494Smarius case MMC_OCR_320_330: 743318494Smarius case MMC_OCR_330_340: 744318494Smarius case MMC_OCR_340_350: 745318494Smarius case MMC_OCR_350_360: 746318494Smarius if (clock <= MMC_TYPE_HS_26_MAX) 747318494Smarius value = ext_csd[EXT_CSD_PWR_CL_26_360]; 748318494Smarius else if (clock <= MMC_TYPE_HS_52_MAX) { 749318494Smarius if (mmcbr_get_timing(dev) == bus_timing_mmc_ddr52 && 750318494Smarius ivar->bus_width >= bus_width_4) 751318494Smarius value = ext_csd[EXT_CSD_PWR_CL_52_360_DDR]; 752318494Smarius else 753318494Smarius value = ext_csd[EXT_CSD_PWR_CL_52_360]; 754318494Smarius } else if (clock <= MMC_TYPE_HS200_HS400ES_MAX) { 755318494Smarius if (ivar->bus_width == bus_width_8) 756318494Smarius value = ext_csd[EXT_CSD_PWR_CL_200_360_DDR]; 757318494Smarius else 758318494Smarius value = ext_csd[EXT_CSD_PWR_CL_200_360]; 759318494Smarius } 760183704Smav break; 761183704Smav default: 762318494Smarius device_printf(dev, "No power class support for VDD 0x%x\n", 763318494Smarius 1 << mmcbr_get_vdd(dev)); 764183704Smav return (MMC_ERR_INVALID); 765183704Smav } 766318494Smarius 767318494Smarius if (ivar->bus_width == bus_width_8) 768318494Smarius value = (value & EXT_CSD_POWER_CLASS_8BIT_MASK) >> 769318494Smarius EXT_CSD_POWER_CLASS_8BIT_SHIFT; 770318494Smarius else 771318494Smarius value = (value & EXT_CSD_POWER_CLASS_4BIT_MASK) >> 772318494Smarius EXT_CSD_POWER_CLASS_4BIT_SHIFT; 773318494Smarius 774318494Smarius if (value == 0) 775318494Smarius return (MMC_ERR_NONE); 776318494Smarius 777318494Smarius return (mmc_switch(dev, dev, ivar->rca, EXT_CSD_CMD_SET_NORMAL, 778318494Smarius EXT_CSD_POWER_CLASS, value, ivar->cmd6_time, true)); 779318494Smarius} 780318494Smarius 781318494Smariusstatic int 782318494Smariusmmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar, 783318494Smarius enum mmc_bus_timing timing) 784318494Smarius{ 785318494Smarius u_char switch_res[64]; 786318494Smarius uint8_t value; 787318494Smarius int err; 788318494Smarius 789318197Smarius if (mmcbr_get_mode(sc->dev) == mode_sd) { 790318494Smarius switch (timing) { 791318494Smarius case bus_timing_normal: 792318494Smarius value = SD_SWITCH_NORMAL_MODE; 793318494Smarius break; 794318494Smarius case bus_timing_hs: 795318494Smarius value = SD_SWITCH_HS_MODE; 796318494Smarius break; 797318494Smarius default: 798318494Smarius return (MMC_ERR_INVALID); 799318494Smarius } 800188044Simp err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1, 801188044Simp value, switch_res); 802318197Smarius if (err != MMC_ERR_NONE) 803318197Smarius return (err); 804318197Smarius if ((switch_res[16] & 0xf) != value) 805318197Smarius return (MMC_ERR_FAILED); 806318197Smarius mmcbr_set_timing(sc->dev, timing); 807318197Smarius mmcbr_update_ios(sc->dev); 808318197Smarius } else { 809318494Smarius switch (timing) { 810318494Smarius case bus_timing_normal: 811318494Smarius value = EXT_CSD_HS_TIMING_BC; 812318494Smarius break; 813318494Smarius case bus_timing_hs: 814318494Smarius case bus_timing_mmc_ddr52: 815318494Smarius value = EXT_CSD_HS_TIMING_HS; 816318494Smarius break; 817318494Smarius default: 818318494Smarius return (MMC_ERR_INVALID); 819318494Smarius } 820318197Smarius err = mmc_switch(sc->dev, sc->dev, ivar->rca, 821318197Smarius EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, value, 822318197Smarius ivar->cmd6_time, false); 823318197Smarius if (err != MMC_ERR_NONE) 824318197Smarius return (err); 825318197Smarius mmcbr_set_timing(sc->dev, timing); 826318197Smarius mmcbr_update_ios(sc->dev); 827318197Smarius err = mmc_switch_status(sc->dev, sc->dev, ivar->rca, 828318197Smarius ivar->cmd6_time); 829318197Smarius } 830183704Smav return (err); 831183704Smav} 832183704Smav 833318197Smariusstatic const uint8_t p8[8] = { 834318197Smarius 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 835318197Smarius}; 836318197Smarius 837318197Smariusstatic const uint8_t p8ok[8] = { 838318197Smarius 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 839318197Smarius}; 840318197Smarius 841318197Smariusstatic const uint8_t p4[4] = { 842318197Smarius 0x5A, 0x00, 0x00, 0x00 843318197Smarius}; 844318197Smarius 845318197Smariusstatic const uint8_t p4ok[4] = { 846318197Smarius 0xA5, 0x00, 0x00, 0x00 847318197Smarius}; 848318197Smarius 849183704Smavstatic int 850183704Smavmmc_test_bus_width(struct mmc_softc *sc) 851183704Smav{ 852183704Smav struct mmc_command cmd; 853183704Smav struct mmc_data data; 854318197Smarius uint8_t buf[8]; 855183704Smav int err; 856183704Smav 857183704Smav if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) { 858183704Smav mmcbr_set_bus_width(sc->dev, bus_width_8); 859183704Smav mmcbr_update_ios(sc->dev); 860183704Smav 861275951Sian sc->squelched++; /* Errors are expected, squelch reporting. */ 862254432Sian memset(&cmd, 0, sizeof(cmd)); 863254432Sian memset(&data, 0, sizeof(data)); 864183704Smav cmd.opcode = MMC_BUSTEST_W; 865183704Smav cmd.arg = 0; 866183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 867183704Smav cmd.data = &data; 868183704Smav 869318197Smarius data.data = __DECONST(void *, p8); 870183704Smav data.len = 8; 871183704Smav data.flags = MMC_DATA_WRITE; 872318197Smarius mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0); 873312399Smarius 874254432Sian memset(&cmd, 0, sizeof(cmd)); 875254432Sian memset(&data, 0, sizeof(data)); 876183704Smav cmd.opcode = MMC_BUSTEST_R; 877183704Smav cmd.arg = 0; 878183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 879183704Smav cmd.data = &data; 880183704Smav 881183704Smav data.data = buf; 882183704Smav data.len = 8; 883183704Smav data.flags = MMC_DATA_READ; 884318197Smarius err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0); 885275951Sian sc->squelched--; 886312399Smarius 887183704Smav mmcbr_set_bus_width(sc->dev, bus_width_1); 888183704Smav mmcbr_update_ios(sc->dev); 889183704Smav 890183704Smav if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0) 891183704Smav return (bus_width_8); 892183704Smav } 893183704Smav 894183704Smav if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) { 895183704Smav mmcbr_set_bus_width(sc->dev, bus_width_4); 896183704Smav mmcbr_update_ios(sc->dev); 897183704Smav 898275951Sian sc->squelched++; /* Errors are expected, squelch reporting. */ 899254432Sian memset(&cmd, 0, sizeof(cmd)); 900254432Sian memset(&data, 0, sizeof(data)); 901183704Smav cmd.opcode = MMC_BUSTEST_W; 902183704Smav cmd.arg = 0; 903183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 904183704Smav cmd.data = &data; 905183704Smav 906318197Smarius data.data = __DECONST(void *, p4); 907183704Smav data.len = 4; 908183704Smav data.flags = MMC_DATA_WRITE; 909318197Smarius mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0); 910312399Smarius 911254432Sian memset(&cmd, 0, sizeof(cmd)); 912254432Sian memset(&data, 0, sizeof(data)); 913183704Smav cmd.opcode = MMC_BUSTEST_R; 914183704Smav cmd.arg = 0; 915183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 916183704Smav cmd.data = &data; 917183704Smav 918183704Smav data.data = buf; 919183704Smav data.len = 4; 920183704Smav data.flags = MMC_DATA_READ; 921318197Smarius err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0); 922275951Sian sc->squelched--; 923183704Smav 924183704Smav mmcbr_set_bus_width(sc->dev, bus_width_1); 925183704Smav mmcbr_update_ios(sc->dev); 926183704Smav 927183704Smav if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0) 928183704Smav return (bus_width_4); 929183704Smav } 930183704Smav return (bus_width_1); 931183704Smav} 932183704Smav 933163516Simpstatic uint32_t 934184033Smavmmc_get_bits(uint32_t *bits, int bit_len, int start, int size) 935163516Simp{ 936183729Simp const int i = (bit_len / 32) - (start / 32) - 1; 937163516Simp const int shift = start & 31; 938163516Simp uint32_t retval = bits[i] >> shift; 939318197Smarius 940163516Simp if (size + shift > 32) 941163516Simp retval |= bits[i - 1] << (32 - shift); 942217509Smav return (retval & ((1llu << size) - 1)); 943163516Simp} 944163516Simp 945163516Simpstatic void 946183729Simpmmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid) 947163516Simp{ 948163516Simp int i; 949163516Simp 950183729Simp /* There's no version info, so we take it on faith */ 951163516Simp memset(cid, 0, sizeof(*cid)); 952184033Smav cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 953184033Smav cid->oid = mmc_get_bits(raw_cid, 128, 104, 16); 954183729Simp for (i = 0; i < 5; i++) 955184033Smav cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 956187875Smav cid->pnm[5] = 0; 957184033Smav cid->prv = mmc_get_bits(raw_cid, 128, 56, 8); 958184033Smav cid->psn = mmc_get_bits(raw_cid, 128, 24, 32); 959187875Smav cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000; 960184033Smav cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4); 961163516Simp} 962163516Simp 963183729Simpstatic void 964318197Smariusmmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid, bool is_4_41p) 965183729Simp{ 966183729Simp int i; 967183729Simp 968183729Simp /* There's no version info, so we take it on faith */ 969183729Simp memset(cid, 0, sizeof(*cid)); 970184033Smav cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 971184033Smav cid->oid = mmc_get_bits(raw_cid, 128, 104, 8); 972183729Simp for (i = 0; i < 6; i++) 973184033Smav cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 974187875Smav cid->pnm[6] = 0; 975184033Smav cid->prv = mmc_get_bits(raw_cid, 128, 48, 8); 976184033Smav cid->psn = mmc_get_bits(raw_cid, 128, 16, 32); 977184033Smav cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4); 978318197Smarius cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4); 979318197Smarius if (is_4_41p) 980318197Smarius cid->mdt_year += 2013; 981318197Smarius else 982318197Smarius cid->mdt_year += 1997; 983183729Simp} 984183729Simp 985234524Smariusstatic void 986234524Smariusmmc_format_card_id_string(struct mmc_ivars *ivar) 987234524Smarius{ 988234524Smarius char oidstr[8]; 989234524Smarius uint8_t c1; 990234524Smarius uint8_t c2; 991234524Smarius 992234524Smarius /* 993234524Smarius * Format a card ID string for use by the mmcsd driver, it's what 994234524Smarius * appears between the <> in the following: 995234524Smarius * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0 996234524Smarius * 22.5MHz/4bit/128-block 997234524Smarius * 998269341Sian * Also format just the card serial number, which the mmcsd driver will 999269341Sian * use as the disk->d_ident string. 1000269341Sian * 1001234524Smarius * The card_id_string in mmc_ivars is currently allocated as 64 bytes, 1002234524Smarius * and our max formatted length is currently 55 bytes if every field 1003234524Smarius * contains the largest value. 1004234524Smarius * 1005234524Smarius * Sometimes the oid is two printable ascii chars; when it's not, 1006234524Smarius * format it as 0xnnnn instead. 1007234524Smarius */ 1008234524Smarius c1 = (ivar->cid.oid >> 8) & 0x0ff; 1009234524Smarius c2 = ivar->cid.oid & 0x0ff; 1010234524Smarius if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f) 1011234524Smarius snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2); 1012234524Smarius else 1013234524Smarius snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid); 1014269341Sian snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string), 1015269341Sian "%08X", ivar->cid.psn); 1016234524Smarius snprintf(ivar->card_id_string, sizeof(ivar->card_id_string), 1017269341Sian "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s", 1018234524Smarius ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "", 1019234524Smarius ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f, 1020234524Smarius ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year, 1021234524Smarius ivar->cid.mid, oidstr); 1022234524Smarius} 1023234524Smarius 1024163516Simpstatic const int exp[8] = { 1025163516Simp 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 1026163516Simp}; 1027234524Smarius 1028163516Simpstatic const int mant[16] = { 1029234524Smarius 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 1030163516Simp}; 1031234524Smarius 1032163516Simpstatic const int cur_min[8] = { 1033163516Simp 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000 1034163516Simp}; 1035234524Smarius 1036163516Simpstatic const int cur_max[8] = { 1037163516Simp 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000 1038163516Simp}; 1039163516Simp 1040163516Simpstatic void 1041183729Simpmmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd) 1042163516Simp{ 1043163516Simp int v; 1044163516Simp int m; 1045163516Simp int e; 1046163516Simp 1047163516Simp memset(csd, 0, sizeof(*csd)); 1048184033Smav csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2); 1049183729Simp if (v == 0) { 1050184033Smav m = mmc_get_bits(raw_csd, 128, 115, 4); 1051184033Smav e = mmc_get_bits(raw_csd, 128, 112, 3); 1052236156Smarius csd->tacc = (exp[e] * mant[m] + 9) / 10; 1053184033Smav csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 1054184033Smav m = mmc_get_bits(raw_csd, 128, 99, 4); 1055184033Smav e = mmc_get_bits(raw_csd, 128, 96, 3); 1056183704Smav csd->tran_speed = exp[e] * 10000 * mant[m]; 1057184033Smav csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 1058184033Smav csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 1059184033Smav csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 1060184033Smav csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 1061184033Smav csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 1062184033Smav csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 1063318197Smarius csd->vdd_r_curr_min = 1064318197Smarius cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 1065318197Smarius csd->vdd_r_curr_max = 1066318197Smarius cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 1067318197Smarius csd->vdd_w_curr_min = 1068318197Smarius cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 1069318197Smarius csd->vdd_w_curr_max = 1070318197Smarius cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 1071184033Smav m = mmc_get_bits(raw_csd, 128, 62, 12); 1072184033Smav e = mmc_get_bits(raw_csd, 128, 47, 3); 1073183704Smav csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 1074184033Smav csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 1075184033Smav csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 1076184033Smav csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 1077184033Smav csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 1078184033Smav csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 1079184033Smav csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 1080184033Smav csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 1081183729Simp } else if (v == 1) { 1082184033Smav m = mmc_get_bits(raw_csd, 128, 115, 4); 1083184033Smav e = mmc_get_bits(raw_csd, 128, 112, 3); 1084236156Smarius csd->tacc = (exp[e] * mant[m] + 9) / 10; 1085184033Smav csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 1086184033Smav m = mmc_get_bits(raw_csd, 128, 99, 4); 1087184033Smav e = mmc_get_bits(raw_csd, 128, 96, 3); 1088183729Simp csd->tran_speed = exp[e] * 10000 * mant[m]; 1089184033Smav csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 1090184033Smav csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 1091184033Smav csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 1092184033Smav csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 1093184033Smav csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 1094184033Smav csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 1095318197Smarius csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1096318197Smarius 1) * 512 * 1024; 1097184033Smav csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 1098184033Smav csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 1099184033Smav csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 1100184033Smav csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 1101184033Smav csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 1102184033Smav csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 1103184033Smav csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 1104312399Smarius } else 1105183729Simp panic("unknown SD CSD version"); 1106163516Simp} 1107163516Simp 1108183704Smavstatic void 1109183729Simpmmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd) 1110183729Simp{ 1111183729Simp int m; 1112183729Simp int e; 1113183729Simp 1114183729Simp memset(csd, 0, sizeof(*csd)); 1115184033Smav csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2); 1116184033Smav csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4); 1117184033Smav m = mmc_get_bits(raw_csd, 128, 115, 4); 1118184033Smav e = mmc_get_bits(raw_csd, 128, 112, 3); 1119183729Simp csd->tacc = exp[e] * mant[m] + 9 / 10; 1120184033Smav csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 1121184033Smav m = mmc_get_bits(raw_csd, 128, 99, 4); 1122184033Smav e = mmc_get_bits(raw_csd, 128, 96, 3); 1123183729Simp csd->tran_speed = exp[e] * 10000 * mant[m]; 1124184033Smav csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 1125184033Smav csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 1126184033Smav csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 1127184033Smav csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 1128184033Smav csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 1129184033Smav csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 1130184033Smav csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 1131184033Smav csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 1132184033Smav csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 1133184033Smav csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 1134184033Smav m = mmc_get_bits(raw_csd, 128, 62, 12); 1135184033Smav e = mmc_get_bits(raw_csd, 128, 47, 3); 1136183729Simp csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 1137184033Smav csd->erase_blk_en = 0; 1138184033Smav csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) * 1139184033Smav (mmc_get_bits(raw_csd, 128, 37, 5) + 1); 1140184033Smav csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5); 1141184033Smav csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 1142184033Smav csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 1143184033Smav csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 1144184033Smav csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 1145183729Simp} 1146183729Simp 1147183729Simpstatic void 1148183704Smavmmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr) 1149183704Smav{ 1150183704Smav unsigned int scr_struct; 1151183704Smav 1152183704Smav memset(scr, 0, sizeof(*scr)); 1153183729Simp 1154184033Smav scr_struct = mmc_get_bits(raw_scr, 64, 60, 4); 1155183704Smav if (scr_struct != 0) { 1156183704Smav printf("Unrecognised SCR structure version %d\n", 1157183704Smav scr_struct); 1158183704Smav return; 1159183704Smav } 1160184033Smav scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4); 1161184033Smav scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4); 1162183704Smav} 1163183704Smav 1164184033Smavstatic void 1165184033Smavmmc_app_decode_sd_status(uint32_t *raw_sd_status, 1166184033Smav struct mmc_sd_status *sd_status) 1167184033Smav{ 1168184033Smav 1169184033Smav memset(sd_status, 0, sizeof(*sd_status)); 1170184033Smav 1171184033Smav sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2); 1172184033Smav sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1); 1173184033Smav sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16); 1174184033Smav sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12); 1175184033Smav sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8); 1176184033Smav sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8); 1177184033Smav sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4); 1178184033Smav sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16); 1179184033Smav sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6); 1180184033Smav sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2); 1181184033Smav} 1182184033Smav 1183163516Simpstatic int 1184163516Simpmmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid) 1185163516Simp{ 1186163516Simp struct mmc_command cmd; 1187163516Simp int err; 1188163516Simp 1189254432Sian memset(&cmd, 0, sizeof(cmd)); 1190163516Simp cmd.opcode = MMC_ALL_SEND_CID; 1191163516Simp cmd.arg = 0; 1192163516Simp cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 1193183470Simp cmd.data = NULL; 1194318197Smarius err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 1195163516Simp memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 1196163516Simp return (err); 1197163516Simp} 1198163516Simp 1199163516Simpstatic int 1200236156Smariusmmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd) 1201163516Simp{ 1202163516Simp struct mmc_command cmd; 1203163516Simp int err; 1204163516Simp 1205254432Sian memset(&cmd, 0, sizeof(cmd)); 1206163516Simp cmd.opcode = MMC_SEND_CSD; 1207163516Simp cmd.arg = rca << 16; 1208163516Simp cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 1209183470Simp cmd.data = NULL; 1210318197Smarius err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 1211236156Smarius memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t)); 1212163516Simp return (err); 1213163516Simp} 1214163516Simp 1215163516Simpstatic int 1216183704Smavmmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr) 1217183704Smav{ 1218183704Smav int err; 1219183704Smav struct mmc_command cmd; 1220183704Smav struct mmc_data data; 1221183704Smav 1222254432Sian memset(&cmd, 0, sizeof(cmd)); 1223254432Sian memset(&data, 0, sizeof(data)); 1224183704Smav 1225183704Smav memset(rawscr, 0, 8); 1226183704Smav cmd.opcode = ACMD_SEND_SCR; 1227183704Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1228183704Smav cmd.arg = 0; 1229183704Smav cmd.data = &data; 1230183704Smav 1231183704Smav data.data = rawscr; 1232183704Smav data.len = 8; 1233183704Smav data.flags = MMC_DATA_READ; 1234183704Smav 1235318197Smarius err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES); 1236183704Smav rawscr[0] = be32toh(rawscr[0]); 1237183704Smav rawscr[1] = be32toh(rawscr[1]); 1238183704Smav return (err); 1239183704Smav} 1240183704Smav 1241183704Smavstatic int 1242318197Smariusmmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus) 1243183704Smav{ 1244183704Smav struct mmc_command cmd; 1245183704Smav struct mmc_data data; 1246184033Smav int err, i; 1247184033Smav 1248254432Sian memset(&cmd, 0, sizeof(cmd)); 1249254432Sian memset(&data, 0, sizeof(data)); 1250184033Smav 1251184033Smav memset(rawsdstatus, 0, 64); 1252184033Smav cmd.opcode = ACMD_SD_STATUS; 1253184033Smav cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1254184033Smav cmd.arg = 0; 1255184033Smav cmd.data = &data; 1256184033Smav 1257184033Smav data.data = rawsdstatus; 1258184033Smav data.len = 64; 1259184033Smav data.flags = MMC_DATA_READ; 1260184033Smav 1261318197Smarius err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES); 1262184033Smav for (i = 0; i < 16; i++) 1263184033Smav rawsdstatus[i] = be32toh(rawsdstatus[i]); 1264184033Smav return (err); 1265184033Smav} 1266184033Smav 1267184033Smavstatic int 1268183704Smavmmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp) 1269183704Smav{ 1270183704Smav struct mmc_command cmd; 1271183704Smav int err; 1272183704Smav 1273254432Sian memset(&cmd, 0, sizeof(cmd)); 1274183704Smav cmd.opcode = MMC_SET_RELATIVE_ADDR; 1275183704Smav cmd.arg = resp << 16; 1276183704Smav cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 1277183704Smav cmd.data = NULL; 1278318197Smarius err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 1279183704Smav return (err); 1280183704Smav} 1281183704Smav 1282183704Smavstatic int 1283163516Simpmmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp) 1284163516Simp{ 1285163516Simp struct mmc_command cmd; 1286163516Simp int err; 1287163516Simp 1288254432Sian memset(&cmd, 0, sizeof(cmd)); 1289163516Simp cmd.opcode = SD_SEND_RELATIVE_ADDR; 1290163516Simp cmd.arg = 0; 1291163516Simp cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 1292183470Simp cmd.data = NULL; 1293318197Smarius err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 1294163516Simp *resp = cmd.resp[0]; 1295163516Simp return (err); 1296163516Simp} 1297163516Simp 1298236156Smariusstatic int 1299236156Smariusmmc_set_blocklen(struct mmc_softc *sc, uint32_t len) 1300236156Smarius{ 1301236156Smarius struct mmc_command cmd; 1302236156Smarius int err; 1303236156Smarius 1304254432Sian memset(&cmd, 0, sizeof(cmd)); 1305236156Smarius cmd.opcode = MMC_SET_BLOCKLEN; 1306236156Smarius cmd.arg = len; 1307236156Smarius cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1308236156Smarius cmd.data = NULL; 1309318197Smarius err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 1310236156Smarius return (err); 1311236156Smarius} 1312236156Smarius 1313318494Smariusstatic uint32_t 1314318494Smariusmmc_timing_to_dtr(struct mmc_ivars *ivar, enum mmc_bus_timing timing) 1315318494Smarius{ 1316318494Smarius 1317318494Smarius switch (timing) { 1318318494Smarius case bus_timing_normal: 1319318494Smarius return (ivar->tran_speed); 1320318494Smarius case bus_timing_hs: 1321318494Smarius return (ivar->hs_tran_speed); 1322318494Smarius case bus_timing_uhs_sdr12: 1323318494Smarius return (SD_SDR12_MAX); 1324318494Smarius case bus_timing_uhs_sdr25: 1325318494Smarius return (SD_SDR25_MAX); 1326318494Smarius case bus_timing_uhs_ddr50: 1327318494Smarius return (SD_DDR50_MAX); 1328318494Smarius case bus_timing_uhs_sdr50: 1329318494Smarius return (SD_SDR50_MAX); 1330318494Smarius case bus_timing_uhs_sdr104: 1331318494Smarius return (SD_SDR104_MAX); 1332318494Smarius case bus_timing_mmc_ddr52: 1333318494Smarius return (MMC_TYPE_DDR52_MAX); 1334318494Smarius case bus_timing_mmc_hs200: 1335318494Smarius case bus_timing_mmc_hs400: 1336318494Smarius case bus_timing_mmc_hs400es: 1337318494Smarius return (MMC_TYPE_HS200_HS400ES_MAX); 1338318494Smarius } 1339318494Smarius return (0); 1340318494Smarius} 1341318494Smarius 1342318494Smariusstatic const char * 1343318494Smariusmmc_timing_to_string(enum mmc_bus_timing timing) 1344318494Smarius{ 1345318494Smarius 1346318494Smarius switch (timing) { 1347318494Smarius case bus_timing_normal: 1348318494Smarius return ("normal speed"); 1349318494Smarius case bus_timing_hs: 1350318494Smarius return ("high speed"); 1351318494Smarius case bus_timing_uhs_sdr12: 1352318494Smarius case bus_timing_uhs_sdr25: 1353318494Smarius case bus_timing_uhs_sdr50: 1354318494Smarius case bus_timing_uhs_sdr104: 1355318494Smarius return ("single data rate"); 1356318494Smarius case bus_timing_uhs_ddr50: 1357318494Smarius case bus_timing_mmc_ddr52: 1358318494Smarius return ("dual data rate"); 1359318494Smarius case bus_timing_mmc_hs200: 1360318494Smarius return ("HS200"); 1361318494Smarius case bus_timing_mmc_hs400: 1362318494Smarius return ("HS400"); 1363318494Smarius case bus_timing_mmc_hs400es: 1364318494Smarius return ("HS400 with enhanced strobe"); 1365318494Smarius } 1366318494Smarius return (""); 1367318494Smarius} 1368318494Smarius 1369163516Simpstatic void 1370187875Smavmmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard) 1371187875Smav{ 1372318494Smarius enum mmc_bus_timing max_timing, timing; 1373318197Smarius 1374254425Sian device_printf(dev, "Card at relative address 0x%04x%s:\n", 1375187875Smav ivar->rca, newcard ? " added" : ""); 1376234524Smarius device_printf(dev, " card: %s\n", ivar->card_id_string); 1377318494Smarius max_timing = bus_timing_normal; 1378318494Smarius for (timing = bus_timing_max; timing > bus_timing_normal; timing--) { 1379318494Smarius if (isset(&ivar->timings, timing)) { 1380318494Smarius max_timing = timing; 1381318494Smarius break; 1382318494Smarius } 1383318494Smarius } 1384318494Smarius device_printf(dev, " bus: %ubit, %uMHz (%s timing)\n", 1385187875Smav (ivar->bus_width == bus_width_1 ? 1 : 1386187875Smav (ivar->bus_width == bus_width_4 ? 4 : 8)), 1387318494Smarius mmc_timing_to_dtr(ivar, timing) / 1000000, 1388318494Smarius mmc_timing_to_string(timing)); 1389187875Smav device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n", 1390187875Smav ivar->sec_count, ivar->erase_sector, 1391187875Smav ivar->read_only ? ", read-only" : ""); 1392187875Smav} 1393187875Smav 1394187875Smavstatic void 1395163516Simpmmc_discover_cards(struct mmc_softc *sc) 1396163516Simp{ 1397318197Smarius u_char switch_res[64]; 1398318197Smarius uint32_t raw_cid[4]; 1399185721Smav struct mmc_ivars *ivar = NULL; 1400185721Smav device_t *devlist; 1401318197Smarius device_t child; 1402318494Smarius int devcount, err, host_caps, i, newcard; 1403318197Smarius uint32_t resp, sec_count, status; 1404183704Smav uint16_t rca = 2; 1405163516Simp 1406318494Smarius host_caps = mmcbr_get_caps(sc->dev); 1407187875Smav if (bootverbose || mmc_debug) 1408187875Smav device_printf(sc->dev, "Probing cards\n"); 1409163516Simp while (1) { 1410275951Sian sc->squelched++; /* Errors are expected, squelch reporting. */ 1411185721Smav err = mmc_all_send_cid(sc, raw_cid); 1412275951Sian sc->squelched--; 1413163516Simp if (err == MMC_ERR_TIMEOUT) 1414163516Simp break; 1415163516Simp if (err != MMC_ERR_NONE) { 1416183468Simp device_printf(sc->dev, "Error reading CID %d\n", err); 1417163516Simp break; 1418163516Simp } 1419185721Smav newcard = 1; 1420318197Smarius if ((err = device_get_children(sc->dev, &devlist, 1421318197Smarius &devcount)) != 0) 1422185721Smav return; 1423185721Smav for (i = 0; i < devcount; i++) { 1424185721Smav ivar = device_get_ivars(devlist[i]); 1425318197Smarius if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 1426318197Smarius 0) { 1427185721Smav newcard = 0; 1428185721Smav break; 1429185721Smav } 1430185721Smav } 1431185721Smav free(devlist, M_TEMP); 1432187875Smav if (bootverbose || mmc_debug) { 1433318197Smarius device_printf(sc->dev, 1434318197Smarius "%sard detected (CID %08x%08x%08x%08x)\n", 1435187875Smav newcard ? "New c" : "C", 1436187875Smav raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]); 1437187875Smav } 1438185721Smav if (newcard) { 1439185721Smav ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF, 1440185721Smav M_WAITOK | M_ZERO); 1441185721Smav memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid)); 1442185721Smav } 1443183704Smav if (mmcbr_get_ro(sc->dev)) 1444183704Smav ivar->read_only = 1; 1445183704Smav ivar->bus_width = bus_width_1; 1446318494Smarius setbit(&ivar->timings, bus_timing_normal); 1447183704Smav ivar->mode = mmcbr_get_mode(sc->dev); 1448183704Smav if (ivar->mode == mode_sd) { 1449183729Simp mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid); 1450318494Smarius err = mmc_send_relative_addr(sc, &resp); 1451318494Smarius if (err != MMC_ERR_NONE) { 1452318494Smarius device_printf(sc->dev, 1453318494Smarius "Error getting RCA %d\n", err); 1454318494Smarius break; 1455318494Smarius } 1456163516Simp ivar->rca = resp >> 16; 1457183704Smav /* Get card CSD. */ 1458318494Smarius err = mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 1459318494Smarius if (err != MMC_ERR_NONE) { 1460318494Smarius device_printf(sc->dev, 1461318494Smarius "Error getting CSD %d\n", err); 1462318494Smarius break; 1463318494Smarius } 1464236156Smarius if (bootverbose || mmc_debug) 1465236156Smarius device_printf(sc->dev, 1466236156Smarius "%sard detected (CSD %08x%08x%08x%08x)\n", 1467236156Smarius newcard ? "New c" : "C", ivar->raw_csd[0], 1468236156Smarius ivar->raw_csd[1], ivar->raw_csd[2], 1469236156Smarius ivar->raw_csd[3]); 1470183729Simp mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd); 1471183731Smav ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE; 1472183704Smav if (ivar->csd.csd_structure > 0) 1473183704Smav ivar->high_cap = 1; 1474183704Smav ivar->tran_speed = ivar->csd.tran_speed; 1475312399Smarius ivar->erase_sector = ivar->csd.erase_sector * 1476184033Smav ivar->csd.write_bl_len / MMC_SECTOR_SIZE; 1477312399Smarius 1478318197Smarius err = mmc_send_status(sc->dev, sc->dev, ivar->rca, 1479318197Smarius &status); 1480236156Smarius if (err != MMC_ERR_NONE) { 1481236156Smarius device_printf(sc->dev, 1482236156Smarius "Error reading card status %d\n", err); 1483236156Smarius break; 1484236156Smarius } 1485236156Smarius if ((status & R1_CARD_IS_LOCKED) != 0) { 1486236156Smarius device_printf(sc->dev, 1487236156Smarius "Card is password protected, skipping.\n"); 1488236156Smarius break; 1489236156Smarius } 1490236156Smarius 1491318197Smarius /* Get card SCR. Card must be selected to fetch it. */ 1492318494Smarius err = mmc_select_card(sc, ivar->rca); 1493318494Smarius if (err != MMC_ERR_NONE) { 1494318494Smarius device_printf(sc->dev, 1495318494Smarius "Error selecting card %d\n", err); 1496318494Smarius break; 1497318494Smarius } 1498318494Smarius err = mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr); 1499318494Smarius if (err != MMC_ERR_NONE) { 1500318494Smarius device_printf(sc->dev, 1501318494Smarius "Error reading SCR %d\n", err); 1502318494Smarius break; 1503318494Smarius } 1504183704Smav mmc_app_decode_scr(ivar->raw_scr, &ivar->scr); 1505188044Simp /* Get card switch capabilities (command class 10). */ 1506183704Smav if ((ivar->scr.sda_vsn >= 1) && 1507318197Smarius (ivar->csd.ccc & (1 << 10))) { 1508318494Smarius err = mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK, 1509188044Simp SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE, 1510188044Simp switch_res); 1511318494Smarius if (err == MMC_ERR_NONE && 1512318494Smarius switch_res[13] & (1 << SD_SWITCH_HS_MODE)) { 1513318494Smarius setbit(&ivar->timings, bus_timing_hs); 1514318494Smarius ivar->hs_tran_speed = SD_HS_MAX; 1515183704Smav } 1516183704Smav } 1517283128Simp 1518283128Simp /* 1519285678Sian * We deselect then reselect the card here. Some cards 1520285678Sian * become unselected and timeout with the above two 1521285678Sian * commands, although the state tables / diagrams in the 1522285678Sian * standard suggest they go back to the transfer state. 1523285678Sian * Other cards don't become deselected, and if we 1524318197Smarius * attempt to blindly re-select them, we get timeout 1525285678Sian * errors from some controllers. So we deselect then 1526285678Sian * reselect to handle all situations. The only thing we 1527285678Sian * use from the sd_status is the erase sector size, but 1528285678Sian * it is still nice to get that right. 1529283128Simp */ 1530285678Sian mmc_select_card(sc, 0); 1531318494Smarius (void)mmc_select_card(sc, ivar->rca); 1532318494Smarius (void)mmc_app_sd_status(sc, ivar->rca, 1533318494Smarius ivar->raw_sd_status); 1534184033Smav mmc_app_decode_sd_status(ivar->raw_sd_status, 1535184033Smav &ivar->sd_status); 1536184033Smav if (ivar->sd_status.au_size != 0) { 1537184033Smav ivar->erase_sector = 1538184033Smav 16 << ivar->sd_status.au_size; 1539184033Smav } 1540183704Smav /* Find max supported bus width. */ 1541318494Smarius if ((host_caps & MMC_CAP_4_BIT_DATA) && 1542183704Smav (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) 1543183704Smav ivar->bus_width = bus_width_4; 1544236156Smarius 1545236156Smarius /* 1546236156Smarius * Some cards that report maximum I/O block sizes 1547236156Smarius * greater than 512 require the block length to be 1548236156Smarius * set to 512, even though that is supposed to be 1549236156Smarius * the default. Example: 1550236156Smarius * 1551236156Smarius * Transcend 2GB SDSC card, CID: 1552236156Smarius * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000 1553236156Smarius */ 1554236156Smarius if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE || 1555236156Smarius ivar->csd.write_bl_len != MMC_SECTOR_SIZE) 1556236156Smarius mmc_set_blocklen(sc, MMC_SECTOR_SIZE); 1557236156Smarius 1558234524Smarius mmc_format_card_id_string(ivar); 1559236156Smarius 1560187875Smav if (bootverbose || mmc_debug) 1561187875Smav mmc_log_card(sc->dev, ivar, newcard); 1562185721Smav if (newcard) { 1563185721Smav /* Add device. */ 1564185721Smav child = device_add_child(sc->dev, NULL, -1); 1565185721Smav device_set_ivars(child, ivar); 1566185721Smav } 1567275905Simp mmc_select_card(sc, 0); 1568169567Simp return; 1569163516Simp } 1570183704Smav ivar->rca = rca++; 1571318494Smarius err = mmc_set_relative_addr(sc, ivar->rca); 1572318494Smarius if (err != MMC_ERR_NONE) { 1573318494Smarius device_printf(sc->dev, "Error setting RCA %d\n", err); 1574318494Smarius break; 1575318494Smarius } 1576183704Smav /* Get card CSD. */ 1577318494Smarius err = mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 1578318494Smarius if (err != MMC_ERR_NONE) { 1579318494Smarius device_printf(sc->dev, "Error getting CSD %d\n", err); 1580318494Smarius break; 1581318494Smarius } 1582236156Smarius if (bootverbose || mmc_debug) 1583236156Smarius device_printf(sc->dev, 1584236156Smarius "%sard detected (CSD %08x%08x%08x%08x)\n", 1585236156Smarius newcard ? "New c" : "C", ivar->raw_csd[0], 1586236156Smarius ivar->raw_csd[1], ivar->raw_csd[2], 1587236156Smarius ivar->raw_csd[3]); 1588236156Smarius 1589183729Simp mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd); 1590183731Smav ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE; 1591183704Smav ivar->tran_speed = ivar->csd.tran_speed; 1592312399Smarius ivar->erase_sector = ivar->csd.erase_sector * 1593184033Smav ivar->csd.write_bl_len / MMC_SECTOR_SIZE; 1594236156Smarius 1595318197Smarius err = mmc_send_status(sc->dev, sc->dev, ivar->rca, &status); 1596236156Smarius if (err != MMC_ERR_NONE) { 1597236156Smarius device_printf(sc->dev, 1598236156Smarius "Error reading card status %d\n", err); 1599236156Smarius break; 1600236156Smarius } 1601236156Smarius if ((status & R1_CARD_IS_LOCKED) != 0) { 1602236156Smarius device_printf(sc->dev, 1603236156Smarius "Card is password protected, skipping.\n"); 1604236156Smarius break; 1605236156Smarius } 1606236156Smarius 1607318494Smarius err = mmc_select_card(sc, ivar->rca); 1608318494Smarius if (err != MMC_ERR_NONE) { 1609318494Smarius device_printf(sc->dev, "Error selecting card %d\n", 1610318494Smarius err); 1611318494Smarius break; 1612318494Smarius } 1613276106Simp 1614318197Smarius /* Only MMC >= 4.x devices support EXT_CSD. */ 1615183704Smav if (ivar->csd.spec_vers >= 4) { 1616318197Smarius err = mmc_send_ext_csd(sc->dev, sc->dev, 1617318197Smarius ivar->raw_ext_csd); 1618318197Smarius if (err != MMC_ERR_NONE) { 1619318197Smarius device_printf(sc->dev, 1620318197Smarius "Error reading EXT_CSD %d\n", err); 1621318197Smarius break; 1622318197Smarius } 1623183731Smav /* Handle extended capacity from EXT_CSD */ 1624183731Smav sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] + 1625183731Smav (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) + 1626183731Smav (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) + 1627183731Smav (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24); 1628183731Smav if (sec_count != 0) { 1629183731Smav ivar->sec_count = sec_count; 1630183731Smav ivar->high_cap = 1; 1631183731Smav } 1632318494Smarius /* Get device speeds beyond normal mode. */ 1633318494Smarius if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 1634318494Smarius EXT_CSD_CARD_TYPE_HS_52) != 0) { 1635318494Smarius setbit(&ivar->timings, bus_timing_hs); 1636318494Smarius ivar->hs_tran_speed = MMC_TYPE_HS_52_MAX; 1637318494Smarius } else if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 1638318494Smarius EXT_CSD_CARD_TYPE_HS_26) != 0) { 1639318494Smarius setbit(&ivar->timings, bus_timing_hs); 1640318494Smarius ivar->hs_tran_speed = MMC_TYPE_HS_26_MAX; 1641318494Smarius } 1642318494Smarius if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 1643318494Smarius EXT_CSD_CARD_TYPE_DDR_52_1_2V) != 0 && 1644318494Smarius (host_caps & MMC_CAP_SIGNALING_120) != 0) { 1645318494Smarius setbit(&ivar->timings, bus_timing_mmc_ddr52); 1646318494Smarius setbit(&ivar->vccq_120, bus_timing_mmc_ddr52); 1647318494Smarius } 1648318494Smarius if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 1649318494Smarius EXT_CSD_CARD_TYPE_DDR_52_1_8V) != 0 && 1650318494Smarius (host_caps & MMC_CAP_SIGNALING_180) != 0) { 1651318494Smarius setbit(&ivar->timings, bus_timing_mmc_ddr52); 1652318494Smarius setbit(&ivar->vccq_180, bus_timing_mmc_ddr52); 1653318494Smarius } 1654318197Smarius /* 1655318197Smarius * Determine generic switch timeout (provided in 1656318197Smarius * units of 10 ms), defaulting to 500 ms. 1657318197Smarius */ 1658318197Smarius ivar->cmd6_time = 500 * 1000; 1659318197Smarius if (ivar->csd.spec_vers >= 6) 1660318197Smarius ivar->cmd6_time = 10 * 1661318197Smarius ivar->raw_ext_csd[EXT_CSD_GEN_CMD6_TIME]; 1662183704Smav /* Find max supported bus width. */ 1663183704Smav ivar->bus_width = mmc_test_bus_width(sc); 1664184033Smav /* Handle HC erase sector size. */ 1665184033Smav if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) { 1666184033Smav ivar->erase_sector = 1024 * 1667184033Smav ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE]; 1668318197Smarius err = mmc_switch(sc->dev, sc->dev, ivar->rca, 1669318197Smarius EXT_CSD_CMD_SET_NORMAL, 1670318197Smarius EXT_CSD_ERASE_GRP_DEF, 1671318197Smarius EXT_CSD_ERASE_GRP_DEF_EN, 1672318197Smarius ivar->cmd6_time, true); 1673318197Smarius if (err != MMC_ERR_NONE) { 1674318197Smarius device_printf(sc->dev, 1675318197Smarius "Error setting erase group %d\n", 1676318197Smarius err); 1677318197Smarius break; 1678318197Smarius } 1679184033Smav } 1680183704Smav } 1681236156Smarius 1682236156Smarius /* 1683236156Smarius * Some cards that report maximum I/O block sizes greater 1684236156Smarius * than 512 require the block length to be set to 512, even 1685236156Smarius * though that is supposed to be the default. Example: 1686236156Smarius * 1687236156Smarius * Transcend 2GB SDSC card, CID: 1688236156Smarius * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000 1689236156Smarius */ 1690236156Smarius if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE || 1691236156Smarius ivar->csd.write_bl_len != MMC_SECTOR_SIZE) 1692236156Smarius mmc_set_blocklen(sc, MMC_SECTOR_SIZE); 1693236156Smarius 1694318197Smarius mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid, 1695318197Smarius ivar->raw_ext_csd[EXT_CSD_REV] >= 5); 1696234524Smarius mmc_format_card_id_string(ivar); 1697236156Smarius 1698187875Smav if (bootverbose || mmc_debug) 1699187875Smav mmc_log_card(sc->dev, ivar, newcard); 1700185721Smav if (newcard) { 1701185721Smav /* Add device. */ 1702185721Smav child = device_add_child(sc->dev, NULL, -1); 1703185721Smav device_set_ivars(child, ivar); 1704185721Smav } 1705276106Simp mmc_select_card(sc, 0); 1706163516Simp } 1707163516Simp} 1708163516Simp 1709163516Simpstatic void 1710185721Smavmmc_rescan_cards(struct mmc_softc *sc) 1711185721Smav{ 1712318197Smarius struct mmc_ivars *ivar; 1713185721Smav device_t *devlist; 1714185721Smav int err, i, devcount; 1715185721Smav 1716185721Smav if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 1717185721Smav return; 1718185721Smav for (i = 0; i < devcount; i++) { 1719185721Smav ivar = device_get_ivars(devlist[i]); 1720318197Smarius if (mmc_select_card(sc, ivar->rca) != MMC_ERR_NONE) { 1721187875Smav if (bootverbose || mmc_debug) 1722318197Smarius device_printf(sc->dev, 1723318197Smarius "Card at relative address %d lost.\n", 1724187875Smav ivar->rca); 1725185721Smav device_delete_child(sc->dev, devlist[i]); 1726185721Smav free(ivar, M_DEVBUF); 1727185721Smav } 1728185721Smav } 1729185721Smav free(devlist, M_TEMP); 1730185721Smav mmc_select_card(sc, 0); 1731185721Smav} 1732185721Smav 1733185721Smavstatic int 1734185721Smavmmc_delete_cards(struct mmc_softc *sc) 1735185721Smav{ 1736185721Smav struct mmc_ivars *ivar; 1737185721Smav device_t *devlist; 1738185721Smav int err, i, devcount; 1739185721Smav 1740185721Smav if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 1741185721Smav return (err); 1742185721Smav for (i = 0; i < devcount; i++) { 1743185721Smav ivar = device_get_ivars(devlist[i]); 1744187875Smav if (bootverbose || mmc_debug) 1745318197Smarius device_printf(sc->dev, 1746318197Smarius "Card at relative address %d deleted.\n", 1747187875Smav ivar->rca); 1748185721Smav device_delete_child(sc->dev, devlist[i]); 1749185721Smav free(ivar, M_DEVBUF); 1750185721Smav } 1751185721Smav free(devlist, M_TEMP); 1752185721Smav return (0); 1753185721Smav} 1754185721Smav 1755185721Smavstatic void 1756163516Simpmmc_go_discovery(struct mmc_softc *sc) 1757163516Simp{ 1758163516Simp uint32_t ocr; 1759163516Simp device_t dev; 1760183704Smav int err; 1761163516Simp 1762163516Simp dev = sc->dev; 1763163516Simp if (mmcbr_get_power_mode(dev) != power_on) { 1764183453Simp /* 1765183453Simp * First, try SD modes 1766183453Simp */ 1767275951Sian sc->squelched++; /* Errors are expected, squelch reporting. */ 1768163516Simp mmcbr_set_mode(dev, mode_sd); 1769163516Simp mmc_power_up(sc); 1770163516Simp mmcbr_set_bus_mode(dev, pushpull); 1771187875Smav if (bootverbose || mmc_debug) 1772187875Smav device_printf(sc->dev, "Probing bus\n"); 1773163516Simp mmc_idle_cards(sc); 1774183704Smav err = mmc_send_if_cond(sc, 1); 1775187875Smav if ((bootverbose || mmc_debug) && err == 0) 1776318197Smarius device_printf(sc->dev, 1777318197Smarius "SD 2.0 interface conditions: OK\n"); 1778236156Smarius if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 1779187875Smav if (bootverbose || mmc_debug) 1780187875Smav device_printf(sc->dev, "SD probe: failed\n"); 1781183453Simp /* 1782183453Simp * Failed, try MMC 1783183453Simp */ 1784163516Simp mmcbr_set_mode(dev, mode_mmc); 1785187875Smav if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 1786187875Smav if (bootverbose || mmc_debug) 1787318197Smarius device_printf(sc->dev, 1788318197Smarius "MMC probe: failed\n"); 1789185721Smav ocr = 0; /* Failed both, powerdown. */ 1790187875Smav } else if (bootverbose || mmc_debug) 1791187875Smav device_printf(sc->dev, 1792187875Smav "MMC probe: OK (OCR: 0x%08x)\n", ocr); 1793187875Smav } else if (bootverbose || mmc_debug) 1794318197Smarius device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", 1795318197Smarius ocr); 1796275951Sian sc->squelched--; 1797187875Smav 1798163516Simp mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr)); 1799163516Simp if (mmcbr_get_ocr(dev) != 0) 1800163516Simp mmc_idle_cards(sc); 1801163516Simp } else { 1802163516Simp mmcbr_set_bus_mode(dev, opendrain); 1803318494Smarius mmcbr_set_clock(dev, SD_MMC_CARD_ID_FREQUENCY); 1804163516Simp mmcbr_update_ios(dev); 1805183453Simp /* XXX recompute vdd based on new cards? */ 1806163516Simp } 1807163516Simp /* 1808163516Simp * Make sure that we have a mutually agreeable voltage to at least 1809163516Simp * one card on the bus. 1810163516Simp */ 1811187875Smav if (bootverbose || mmc_debug) 1812318197Smarius device_printf(sc->dev, "Current OCR: 0x%08x\n", 1813318197Smarius mmcbr_get_ocr(dev)); 1814185721Smav if (mmcbr_get_ocr(dev) == 0) { 1815261944Sian device_printf(sc->dev, "No compatible cards found on bus\n"); 1816185721Smav mmc_delete_cards(sc); 1817185721Smav mmc_power_down(sc); 1818163516Simp return; 1819185721Smav } 1820163516Simp /* 1821163516Simp * Reselect the cards after we've idled them above. 1822163516Simp */ 1823183704Smav if (mmcbr_get_mode(dev) == mode_sd) { 1824183704Smav err = mmc_send_if_cond(sc, 1); 1825183704Smav mmc_send_app_op_cond(sc, 1826183775Simp (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL); 1827183704Smav } else 1828279359Sian mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL); 1829163516Simp mmc_discover_cards(sc); 1830185721Smav mmc_rescan_cards(sc); 1831163516Simp 1832163516Simp mmcbr_set_bus_mode(dev, pushpull); 1833163516Simp mmcbr_update_ios(dev); 1834183763Smav mmc_calculate_clock(sc); 1835163516Simp} 1836163516Simp 1837163516Simpstatic int 1838163516Simpmmc_calculate_clock(struct mmc_softc *sc) 1839163516Simp{ 1840163516Simp device_t *kids; 1841183704Smav struct mmc_ivars *ivar; 1842318494Smarius int host_caps, i, nkid; 1843318494Smarius uint32_t dtr, max_dtr; 1844318494Smarius enum mmc_bus_timing max_timing, timing; 1845318494Smarius bool changed; 1846312399Smarius 1847318494Smarius max_dtr = mmcbr_get_f_max(sc->dev); 1848318494Smarius host_caps = mmcbr_get_caps(sc->dev); 1849318494Smarius if ((host_caps & MMC_CAP_MMC_DDR52) != 0) 1850318494Smarius max_timing = bus_timing_mmc_ddr52; 1851318494Smarius else if ((host_caps & MMC_CAP_HSPEED) != 0) 1852183704Smav max_timing = bus_timing_hs; 1853183704Smav else 1854183704Smav max_timing = bus_timing_normal; 1855163516Simp if (device_get_children(sc->dev, &kids, &nkid) != 0) 1856163516Simp panic("can't get children"); 1857318494Smarius do { 1858318494Smarius changed = false; 1859318494Smarius for (i = 0; i < nkid; i++) { 1860318494Smarius ivar = device_get_ivars(kids[i]); 1861318494Smarius if (isclr(&ivar->timings, max_timing)) { 1862318494Smarius for (timing = max_timing; timing >= 1863318494Smarius bus_timing_normal; timing--) { 1864318494Smarius if (isset(&ivar->timings, timing)) { 1865318494Smarius max_timing = timing; 1866318494Smarius break; 1867318494Smarius } 1868318494Smarius } 1869318494Smarius changed = true; 1870318494Smarius } 1871318494Smarius dtr = mmc_timing_to_dtr(ivar, max_timing); 1872318494Smarius if (dtr < max_dtr) { 1873318494Smarius max_dtr = dtr; 1874318494Smarius changed = true; 1875318494Smarius } 1876318494Smarius } 1877318494Smarius } while (changed == true); 1878318197Smarius if (bootverbose || mmc_debug) { 1879318197Smarius device_printf(sc->dev, 1880318494Smarius "setting transfer rate to %d.%03dMHz (%s timing)\n", 1881318197Smarius max_dtr / 1000000, (max_dtr / 1000) % 1000, 1882318494Smarius mmc_timing_to_string(max_timing)); 1883318197Smarius } 1884183704Smav for (i = 0; i < nkid; i++) { 1885183704Smav ivar = device_get_ivars(kids[i]); 1886318494Smarius if ((ivar->timings & ~(1 << bus_timing_normal)) == 0) 1887183704Smav continue; 1888318494Smarius if (mmc_select_card(sc, ivar->rca) != MMC_ERR_NONE || 1889318494Smarius mmc_set_timing(sc, ivar, max_timing) != MMC_ERR_NONE) 1890318494Smarius device_printf(sc->dev, "Card at relative address %d " 1891318494Smarius "failed to set timing.\n", ivar->rca); 1892183704Smav } 1893183704Smav mmc_select_card(sc, 0); 1894163516Simp free(kids, M_TEMP); 1895183704Smav mmcbr_set_clock(sc->dev, max_dtr); 1896183704Smav mmcbr_update_ios(sc->dev); 1897318197Smarius return (max_dtr); 1898163516Simp} 1899163516Simp 1900163516Simpstatic void 1901163516Simpmmc_scan(struct mmc_softc *sc) 1902163516Simp{ 1903185721Smav device_t dev = sc->dev; 1904163516Simp 1905163516Simp mmc_acquire_bus(dev, dev); 1906163516Simp mmc_go_discovery(sc); 1907163516Simp mmc_release_bus(dev, dev); 1908318197Smarius 1909318197Smarius bus_generic_attach(dev); 1910163516Simp} 1911163516Simp 1912163516Simpstatic int 1913189727Simpmmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result) 1914163516Simp{ 1915163516Simp struct mmc_ivars *ivar = device_get_ivars(child); 1916163516Simp 1917163516Simp switch (which) { 1918163516Simp default: 1919163516Simp return (EINVAL); 1920318197Smarius case MMC_IVAR_SPEC_VERS: 1921318197Smarius *result = ivar->csd.spec_vers; 1922318197Smarius break; 1923163516Simp case MMC_IVAR_DSR_IMP: 1924222475Sjchandra *result = ivar->csd.dsr_imp; 1925163516Simp break; 1926163516Simp case MMC_IVAR_MEDIA_SIZE: 1927222475Sjchandra *result = ivar->sec_count; 1928163516Simp break; 1929163516Simp case MMC_IVAR_RCA: 1930222475Sjchandra *result = ivar->rca; 1931163516Simp break; 1932163516Simp case MMC_IVAR_SECTOR_SIZE: 1933222475Sjchandra *result = MMC_SECTOR_SIZE; 1934163516Simp break; 1935163516Simp case MMC_IVAR_TRAN_SPEED: 1936222475Sjchandra *result = mmcbr_get_clock(bus); 1937163516Simp break; 1938183447Simp case MMC_IVAR_READ_ONLY: 1939222475Sjchandra *result = ivar->read_only; 1940183447Simp break; 1941183704Smav case MMC_IVAR_HIGH_CAP: 1942222475Sjchandra *result = ivar->high_cap; 1943183704Smav break; 1944183763Smav case MMC_IVAR_CARD_TYPE: 1945222475Sjchandra *result = ivar->mode; 1946183763Smav break; 1947183763Smav case MMC_IVAR_BUS_WIDTH: 1948222475Sjchandra *result = ivar->bus_width; 1949183763Smav break; 1950184033Smav case MMC_IVAR_ERASE_SECTOR: 1951222475Sjchandra *result = ivar->erase_sector; 1952184033Smav break; 1953184452Smav case MMC_IVAR_MAX_DATA: 1954222475Sjchandra *result = mmcbr_get_max_data(bus); 1955184452Smav break; 1956234524Smarius case MMC_IVAR_CARD_ID_STRING: 1957234524Smarius *(char **)result = ivar->card_id_string; 1958234524Smarius break; 1959269341Sian case MMC_IVAR_CARD_SN_STRING: 1960269341Sian *(char **)result = ivar->card_sn_string; 1961269341Sian break; 1962163516Simp } 1963163516Simp return (0); 1964163516Simp} 1965163516Simp 1966163516Simpstatic int 1967163516Simpmmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 1968163516Simp{ 1969318197Smarius 1970183453Simp /* 1971183453Simp * None are writable ATM 1972183453Simp */ 1973183453Simp return (EINVAL); 1974163516Simp} 1975163516Simp 1976163516Simpstatic void 1977163516Simpmmc_delayed_attach(void *xsc) 1978163516Simp{ 1979163516Simp struct mmc_softc *sc = xsc; 1980312399Smarius 1981163516Simp mmc_scan(sc); 1982163516Simp config_intrhook_disestablish(&sc->config_intrhook); 1983163516Simp} 1984163516Simp 1985208441Smavstatic int 1986208441Smavmmc_child_location_str(device_t dev, device_t child, char *buf, 1987208441Smav size_t buflen) 1988208441Smav{ 1989208441Smav 1990208441Smav snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child)); 1991208441Smav return (0); 1992208441Smav} 1993208441Smav 1994163516Simpstatic device_method_t mmc_methods[] = { 1995163516Simp /* device_if */ 1996163516Simp DEVMETHOD(device_probe, mmc_probe), 1997163516Simp DEVMETHOD(device_attach, mmc_attach), 1998163516Simp DEVMETHOD(device_detach, mmc_detach), 1999185721Smav DEVMETHOD(device_suspend, mmc_suspend), 2000185721Smav DEVMETHOD(device_resume, mmc_resume), 2001163516Simp 2002163516Simp /* Bus interface */ 2003163516Simp DEVMETHOD(bus_read_ivar, mmc_read_ivar), 2004163516Simp DEVMETHOD(bus_write_ivar, mmc_write_ivar), 2005208441Smav DEVMETHOD(bus_child_location_str, mmc_child_location_str), 2006163516Simp 2007163516Simp /* MMC Bus interface */ 2008163516Simp DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request), 2009163516Simp DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus), 2010163516Simp DEVMETHOD(mmcbus_release_bus, mmc_release_bus), 2011163516Simp 2012234524Smarius DEVMETHOD_END 2013163516Simp}; 2014163516Simp 2015292180Siandriver_t mmc_driver = { 2016163516Simp "mmc", 2017163516Simp mmc_methods, 2018163516Simp sizeof(struct mmc_softc), 2019163516Simp}; 2020292180Siandevclass_t mmc_devclass; 2021297127Sian 2022318197SmariusMODULE_VERSION(mmc, MMC_VERSION); 2023