1114b4164SWarner Losh /*- 2114b4164SWarner Losh * Copyright (c) 2006 Bernd Walter. All rights reserved. 3114b4164SWarner Losh * Copyright (c) 2006 M. Warner Losh. All rights reserved. 40f34084fSMarius Strobl * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org> 5114b4164SWarner Losh * 6114b4164SWarner Losh * Redistribution and use in source and binary forms, with or without 7114b4164SWarner Losh * modification, are permitted provided that the following conditions 8114b4164SWarner Losh * are met: 9114b4164SWarner Losh * 1. Redistributions of source code must retain the above copyright 10114b4164SWarner Losh * notice, this list of conditions and the following disclaimer. 11114b4164SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 12114b4164SWarner Losh * notice, this list of conditions and the following disclaimer in the 13114b4164SWarner Losh * documentation and/or other materials provided with the distribution. 14114b4164SWarner Losh * 15114b4164SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16114b4164SWarner Losh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17114b4164SWarner Losh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18114b4164SWarner Losh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19114b4164SWarner Losh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20114b4164SWarner Losh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21114b4164SWarner Losh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22114b4164SWarner Losh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23114b4164SWarner Losh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24114b4164SWarner Losh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2514eced72SWarner Losh * 2614eced72SWarner Losh * Portions of this software may have been developed with reference to 2714eced72SWarner Losh * the SD Simplified Specification. The following disclaimer may apply: 2814eced72SWarner Losh * 2914eced72SWarner Losh * The following conditions apply to the release of the simplified 3014eced72SWarner Losh * specification ("Simplified Specification") by the SD Card Association and 3114eced72SWarner Losh * the SD Group. The Simplified Specification is a subset of the complete SD 3214eced72SWarner Losh * Specification which is owned by the SD Card Association and the SD 3314eced72SWarner Losh * Group. This Simplified Specification is provided on a non-confidential 3414eced72SWarner Losh * basis subject to the disclaimers below. Any implementation of the 3514eced72SWarner Losh * Simplified Specification may require a license from the SD Card 3614eced72SWarner Losh * Association, SD Group, SD-3C LLC or other third parties. 3714eced72SWarner Losh * 3814eced72SWarner Losh * Disclaimers: 3914eced72SWarner Losh * 4014eced72SWarner Losh * The information contained in the Simplified Specification is presented only 4114eced72SWarner Losh * as a standard specification for SD Cards and SD Host/Ancillary products and 4214eced72SWarner Losh * is provided "AS-IS" without any representations or warranties of any 4314eced72SWarner Losh * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 4414eced72SWarner Losh * Card Association for any damages, any infringements of patents or other 4514eced72SWarner Losh * right of the SD Group, SD-3C LLC, the SD Card Association or any third 4614eced72SWarner Losh * parties, which may result from its use. No license is granted by 4714eced72SWarner Losh * implication, estoppel or otherwise under any patent or other rights of the 4814eced72SWarner Losh * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 4914eced72SWarner Losh * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 5014eced72SWarner Losh * or the SD Card Association to disclose or distribute any technical 5114eced72SWarner Losh * information, know-how or other confidential information to any third party. 52114b4164SWarner Losh */ 53114b4164SWarner Losh 54114b4164SWarner Losh #include <sys/cdefs.h> 55114b4164SWarner Losh __FBSDID("$FreeBSD$"); 56114b4164SWarner Losh 57114b4164SWarner Losh #include <sys/param.h> 58114b4164SWarner Losh #include <sys/systm.h> 59114b4164SWarner Losh #include <sys/kernel.h> 60114b4164SWarner Losh #include <sys/malloc.h> 61114b4164SWarner Losh #include <sys/lock.h> 62114b4164SWarner Losh #include <sys/module.h> 63114b4164SWarner Losh #include <sys/mutex.h> 64114b4164SWarner Losh #include <sys/bus.h> 65c18f1e26SAlexander Motin #include <sys/endian.h> 66ed01be88SAlexander Motin #include <sys/sysctl.h> 67dc47198fSIan Lepore #include <sys/time.h> 68114b4164SWarner Losh 6972dec079SMarius Strobl #include <dev/mmc/bridge.h> 7072dec079SMarius Strobl #include <dev/mmc/mmc_private.h> 7172dec079SMarius Strobl #include <dev/mmc/mmc_subr.h> 72114b4164SWarner Losh #include <dev/mmc/mmcreg.h> 73114b4164SWarner Losh #include <dev/mmc/mmcbrvar.h> 74114b4164SWarner Losh #include <dev/mmc/mmcvar.h> 7572dec079SMarius Strobl 76114b4164SWarner Losh #include "mmcbr_if.h" 77114b4164SWarner Losh #include "mmcbus_if.h" 78114b4164SWarner Losh 790f34084fSMarius Strobl CTASSERT(bus_timing_max <= sizeof(uint32_t) * NBBY); 800f34084fSMarius Strobl 81114b4164SWarner Losh /* 82114b4164SWarner Losh * Per-card data 83114b4164SWarner Losh */ 84114b4164SWarner Losh struct mmc_ivars { 85114b4164SWarner Losh uint32_t raw_cid[4]; /* Raw bits of the CID */ 86114b4164SWarner Losh uint32_t raw_csd[4]; /* Raw bits of the CSD */ 87c18f1e26SAlexander Motin uint32_t raw_scr[2]; /* Raw bits of the SCR */ 8872dec079SMarius Strobl uint8_t raw_ext_csd[MMC_EXTCSD_SIZE]; /* Raw bits of the EXT_CSD */ 893906d42dSAlexander Motin uint32_t raw_sd_status[16]; /* Raw bits of the SD_STATUS */ 90114b4164SWarner Losh uint16_t rca; 91aca38eabSMarius Strobl u_char read_only; /* True when the device is read-only */ 92aca38eabSMarius Strobl u_char high_cap; /* High Capacity device (block addressed) */ 93114b4164SWarner Losh enum mmc_card_mode mode; 94aca38eabSMarius Strobl enum mmc_bus_width bus_width; /* Bus width to use */ 95114b4164SWarner Losh struct mmc_cid cid; /* cid decoded */ 96114b4164SWarner Losh struct mmc_csd csd; /* csd decoded */ 97c18f1e26SAlexander Motin struct mmc_scr scr; /* scr decoded */ 983906d42dSAlexander Motin struct mmc_sd_status sd_status; /* SD_STATUS decoded */ 99fa5f41d3SAlexander Motin uint32_t sec_count; /* Card capacity in 512byte blocks */ 1000f34084fSMarius Strobl uint32_t timings; /* Mask of bus timings supported */ 1010f34084fSMarius Strobl uint32_t vccq_120; /* Mask of bus timings at VCCQ of 1.2 V */ 1020f34084fSMarius Strobl uint32_t vccq_180; /* Mask of bus timings at VCCQ of 1.8 V */ 103c18f1e26SAlexander Motin uint32_t tran_speed; /* Max speed in normal mode */ 104c18f1e26SAlexander Motin uint32_t hs_tran_speed; /* Max speed in high speed mode */ 1053906d42dSAlexander Motin uint32_t erase_sector; /* Card native erase sector size */ 10672dec079SMarius Strobl uint32_t cmd6_time; /* Generic switch timeout [us] */ 107*79f39c6aSMarius Strobl uint32_t quirks; /* Quirks as per mmc_quirk->quirks */ 1087aa65846SMarius Strobl char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */ 109f3a4b7f7SIan Lepore char card_sn_string[16];/* Formatted serial # for disk->d_ident */ 110114b4164SWarner Losh }; 111114b4164SWarner Losh 112114b4164SWarner Losh #define CMD_RETRIES 3 113114b4164SWarner Losh 114*79f39c6aSMarius Strobl static const struct mmc_quirk mmc_quirks[] = { 115*79f39c6aSMarius Strobl /* 116*79f39c6aSMarius Strobl * For some SanDisk iNAND devices, the CMD38 argument needs to be 117*79f39c6aSMarius Strobl * provided in EXT_CSD[113]. 118*79f39c6aSMarius Strobl */ 119*79f39c6aSMarius Strobl { 0x2, 0x100, "SEM02G", MMC_QUIRK_INAND_CMD38 }, 120*79f39c6aSMarius Strobl { 0x2, 0x100, "SEM04G", MMC_QUIRK_INAND_CMD38 }, 121*79f39c6aSMarius Strobl { 0x2, 0x100, "SEM08G", MMC_QUIRK_INAND_CMD38 }, 122*79f39c6aSMarius Strobl { 0x2, 0x100, "SEM16G", MMC_QUIRK_INAND_CMD38 }, 123*79f39c6aSMarius Strobl { 0x2, 0x100, "SEM32G", MMC_QUIRK_INAND_CMD38 }, 124*79f39c6aSMarius Strobl 125*79f39c6aSMarius Strobl /* 126*79f39c6aSMarius Strobl * Disable TRIM for Kingston eMMCs where a firmware bug can lead to 127*79f39c6aSMarius Strobl * unrecoverable data corruption. 128*79f39c6aSMarius Strobl */ 129*79f39c6aSMarius Strobl { 0x70, MMC_QUIRK_OID_ANY, "V10008", MMC_QUIRK_BROKEN_TRIM }, 130*79f39c6aSMarius Strobl { 0x70, MMC_QUIRK_OID_ANY, "V10016", MMC_QUIRK_BROKEN_TRIM }, 131*79f39c6aSMarius Strobl 132*79f39c6aSMarius Strobl { 0x0, 0x0, NULL, 0x0 } 133*79f39c6aSMarius Strobl }; 134*79f39c6aSMarius Strobl 1356472ac3dSEd Schouten static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver"); 136ed01be88SAlexander Motin 137711873d4SWarner Losh static int mmc_debug; 1387e6ccea3SMarius Strobl SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &mmc_debug, 0, 1397e6ccea3SMarius Strobl "Debug level"); 140ed01be88SAlexander Motin 141114b4164SWarner Losh /* bus entry points */ 142ae5d8757SMarius Strobl static int mmc_acquire_bus(device_t busdev, device_t dev); 143114b4164SWarner Losh static int mmc_attach(device_t dev); 144ae5d8757SMarius Strobl static int mmc_child_location_str(device_t dev, device_t child, char *buf, 145ae5d8757SMarius Strobl size_t buflen); 146114b4164SWarner Losh static int mmc_detach(device_t dev); 147ae5d8757SMarius Strobl static int mmc_probe(device_t dev); 148ae5d8757SMarius Strobl static int mmc_read_ivar(device_t bus, device_t child, int which, 149ae5d8757SMarius Strobl uintptr_t *result); 150ae5d8757SMarius Strobl static int mmc_release_bus(device_t busdev, device_t dev); 151eb67f31aSAlexander Motin static int mmc_resume(device_t dev); 152aca38eabSMarius Strobl static void mmc_retune_pause(device_t busdev, device_t dev, bool retune); 153aca38eabSMarius Strobl static void mmc_retune_unpause(device_t busdev, device_t dev); 154ae5d8757SMarius Strobl static int mmc_suspend(device_t dev); 155aca38eabSMarius Strobl static int mmc_wait_for_request(device_t busdev, device_t dev, 156ae5d8757SMarius Strobl struct mmc_request *req); 157ae5d8757SMarius Strobl static int mmc_write_ivar(device_t bus, device_t child, int which, 158ae5d8757SMarius Strobl uintptr_t value); 159114b4164SWarner Losh 160114b4164SWarner Losh #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 161114b4164SWarner Losh #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 162114b4164SWarner Losh #define MMC_LOCK_INIT(_sc) \ 1637e6ccea3SMarius Strobl mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->dev), \ 164114b4164SWarner Losh "mmc", MTX_DEF) 1657e6ccea3SMarius Strobl #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx); 1667e6ccea3SMarius Strobl #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED); 1677e6ccea3SMarius Strobl #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED); 168114b4164SWarner Losh 169ae5d8757SMarius Strobl static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid); 170ae5d8757SMarius Strobl static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr); 171ae5d8757SMarius Strobl static void mmc_app_decode_sd_status(uint32_t *raw_sd_status, 172ae5d8757SMarius Strobl struct mmc_sd_status *sd_status); 173ae5d8757SMarius Strobl static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, 174ae5d8757SMarius Strobl uint32_t *rawsdstatus); 175ae5d8757SMarius Strobl static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, 176ae5d8757SMarius Strobl uint32_t *rawscr); 17767d752c3SAlexander Motin static int mmc_calculate_clock(struct mmc_softc *sc); 17872dec079SMarius Strobl static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid, 17972dec079SMarius Strobl bool is_4_41p); 180ae5d8757SMarius Strobl static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid); 181ae5d8757SMarius Strobl static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd); 182aca38eabSMarius Strobl static int mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd); 183ae5d8757SMarius Strobl static void mmc_delayed_attach(void *xsc); 184aca38eabSMarius Strobl static int mmc_delete_cards(struct mmc_softc *sc, bool final); 185ae5d8757SMarius Strobl static void mmc_discover_cards(struct mmc_softc *sc); 186ae5d8757SMarius Strobl static void mmc_format_card_id_string(struct mmc_ivars *ivar); 187ae5d8757SMarius Strobl static void mmc_go_discovery(struct mmc_softc *sc); 188ae5d8757SMarius Strobl static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start, 189ae5d8757SMarius Strobl int size); 190ae5d8757SMarius Strobl static int mmc_highest_voltage(uint32_t ocr); 191aca38eabSMarius Strobl static bool mmc_host_timing(device_t dev, enum mmc_bus_timing timing); 192ae5d8757SMarius Strobl static void mmc_idle_cards(struct mmc_softc *sc); 193ae5d8757SMarius Strobl static void mmc_ms_delay(int ms); 194ae5d8757SMarius Strobl static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard); 1955d2f6510SWarner Losh static void mmc_power_down(struct mmc_softc *sc); 196ae5d8757SMarius Strobl static void mmc_power_up(struct mmc_softc *sc); 197ae5d8757SMarius Strobl static void mmc_rescan_cards(struct mmc_softc *sc); 198aca38eabSMarius Strobl static int mmc_retune(device_t busdev, device_t dev, bool reset); 199ae5d8757SMarius Strobl static void mmc_scan(struct mmc_softc *sc); 200ae5d8757SMarius Strobl static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, 201ae5d8757SMarius Strobl uint8_t value, uint8_t *res); 202ae5d8757SMarius Strobl static int mmc_select_card(struct mmc_softc *sc, uint16_t rca); 203ae5d8757SMarius Strobl static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr); 204ae5d8757SMarius Strobl static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, 205ae5d8757SMarius Strobl uint32_t *rocr); 206ae5d8757SMarius Strobl static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd); 207ae5d8757SMarius Strobl static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs); 208ae5d8757SMarius Strobl static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, 209ae5d8757SMarius Strobl uint32_t *rocr); 210ae5d8757SMarius Strobl static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp); 211ae5d8757SMarius Strobl static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len); 212aca38eabSMarius Strobl static int mmc_set_card_bus_width(struct mmc_softc *sc, struct mmc_ivars *ivar, 213aca38eabSMarius Strobl enum mmc_bus_timing timing); 2140f34084fSMarius Strobl static int mmc_set_power_class(struct mmc_softc *sc, struct mmc_ivars *ivar); 215ae5d8757SMarius Strobl static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp); 21672dec079SMarius Strobl static int mmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar, 2170f34084fSMarius Strobl enum mmc_bus_timing timing); 218aca38eabSMarius Strobl static int mmc_set_vccq(struct mmc_softc *sc, struct mmc_ivars *ivar, 219aca38eabSMarius Strobl enum mmc_bus_timing timing); 220aca38eabSMarius Strobl static int mmc_switch_to_hs200(struct mmc_softc *sc, struct mmc_ivars *ivar, 221aca38eabSMarius Strobl uint32_t clock); 222aca38eabSMarius Strobl static int mmc_switch_to_hs400(struct mmc_softc *sc, struct mmc_ivars *ivar, 223aca38eabSMarius Strobl uint32_t max_dtr, enum mmc_bus_timing max_timing); 224ae5d8757SMarius Strobl static int mmc_test_bus_width(struct mmc_softc *sc); 2250f34084fSMarius Strobl static uint32_t mmc_timing_to_dtr(struct mmc_ivars *ivar, 2260f34084fSMarius Strobl enum mmc_bus_timing timing); 2270f34084fSMarius Strobl static const char *mmc_timing_to_string(enum mmc_bus_timing timing); 228aca38eabSMarius Strobl static void mmc_update_child_list(struct mmc_softc *sc); 229114b4164SWarner Losh static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 230114b4164SWarner Losh uint32_t arg, uint32_t flags, uint32_t *resp, int retries); 231ae5d8757SMarius Strobl static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req); 232ae5d8757SMarius Strobl static void mmc_wakeup(struct mmc_request *req); 233114b4164SWarner Losh 234114b4164SWarner Losh static void 235114b4164SWarner Losh mmc_ms_delay(int ms) 236114b4164SWarner Losh { 237ae5d8757SMarius Strobl 238114b4164SWarner Losh DELAY(1000 * ms); /* XXX BAD */ 239114b4164SWarner Losh } 240114b4164SWarner Losh 241114b4164SWarner Losh static int 242114b4164SWarner Losh mmc_probe(device_t dev) 243114b4164SWarner Losh { 244114b4164SWarner Losh 2452703cdbbSWarner Losh device_set_desc(dev, "MMC/SD bus"); 246114b4164SWarner Losh return (0); 247114b4164SWarner Losh } 248114b4164SWarner Losh 249114b4164SWarner Losh static int 250114b4164SWarner Losh mmc_attach(device_t dev) 251114b4164SWarner Losh { 252114b4164SWarner Losh struct mmc_softc *sc; 253114b4164SWarner Losh 254114b4164SWarner Losh sc = device_get_softc(dev); 255114b4164SWarner Losh sc->dev = dev; 256114b4164SWarner Losh MMC_LOCK_INIT(sc); 257114b4164SWarner Losh 258114b4164SWarner Losh /* We'll probe and attach our children later, but before / mount */ 259114b4164SWarner Losh sc->config_intrhook.ich_func = mmc_delayed_attach; 260114b4164SWarner Losh sc->config_intrhook.ich_arg = sc; 261114b4164SWarner Losh if (config_intrhook_establish(&sc->config_intrhook) != 0) 262114b4164SWarner Losh device_printf(dev, "config_intrhook_establish failed\n"); 263114b4164SWarner Losh return (0); 264114b4164SWarner Losh } 265114b4164SWarner Losh 266114b4164SWarner Losh static int 267114b4164SWarner Losh mmc_detach(device_t dev) 268114b4164SWarner Losh { 2690e1466acSWarner Losh struct mmc_softc *sc = device_get_softc(dev); 270eb67f31aSAlexander Motin int err; 2710e1466acSWarner Losh 272aca38eabSMarius Strobl err = mmc_delete_cards(sc, true); 273aca38eabSMarius Strobl if (err != 0) 274eb67f31aSAlexander Motin return (err); 2755d2f6510SWarner Losh mmc_power_down(sc); 2760e1466acSWarner Losh MMC_LOCK_DESTROY(sc); 2770e1466acSWarner Losh 278128d0ff2SWarner Losh return (0); 279114b4164SWarner Losh } 280114b4164SWarner Losh 281114b4164SWarner Losh static int 282eb67f31aSAlexander Motin mmc_suspend(device_t dev) 283eb67f31aSAlexander Motin { 284eb67f31aSAlexander Motin struct mmc_softc *sc = device_get_softc(dev); 285eb67f31aSAlexander Motin int err; 286eb67f31aSAlexander Motin 287eb67f31aSAlexander Motin err = bus_generic_suspend(dev); 288aca38eabSMarius Strobl if (err != 0) 289aca38eabSMarius Strobl return (err); 290aca38eabSMarius Strobl /* 291aca38eabSMarius Strobl * We power down with the bus acquired here, mainly so that no device 292aca38eabSMarius Strobl * is selected any longer and sc->last_rca gets set to 0. Otherwise, 293aca38eabSMarius Strobl * the deselect as part of the bus acquisition in mmc_scan() may fail 294aca38eabSMarius Strobl * during resume, as the bus isn't powered up again before later in 295aca38eabSMarius Strobl * mmc_go_discovery(). 296aca38eabSMarius Strobl */ 297aca38eabSMarius Strobl err = mmc_acquire_bus(dev, dev); 298aca38eabSMarius Strobl if (err != 0) 299eb67f31aSAlexander Motin return (err); 300eb67f31aSAlexander Motin mmc_power_down(sc); 301aca38eabSMarius Strobl err = mmc_release_bus(dev, dev); 302aca38eabSMarius Strobl return (err); 303eb67f31aSAlexander Motin } 304eb67f31aSAlexander Motin 305eb67f31aSAlexander Motin static int 306eb67f31aSAlexander Motin mmc_resume(device_t dev) 307eb67f31aSAlexander Motin { 308eb67f31aSAlexander Motin struct mmc_softc *sc = device_get_softc(dev); 309eb67f31aSAlexander Motin 310eb67f31aSAlexander Motin mmc_scan(sc); 311eb67f31aSAlexander Motin return (bus_generic_resume(dev)); 312eb67f31aSAlexander Motin } 313eb67f31aSAlexander Motin 314eb67f31aSAlexander Motin static int 315114b4164SWarner Losh mmc_acquire_bus(device_t busdev, device_t dev) 316114b4164SWarner Losh { 317114b4164SWarner Losh struct mmc_softc *sc; 318c18f1e26SAlexander Motin struct mmc_ivars *ivar; 319aca38eabSMarius Strobl int err; 320aca38eabSMarius Strobl uint16_t rca; 3210f34084fSMarius Strobl enum mmc_bus_timing timing; 322114b4164SWarner Losh 323e9bb9da3SWarner Losh err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev); 324114b4164SWarner Losh if (err) 325114b4164SWarner Losh return (err); 326114b4164SWarner Losh sc = device_get_softc(busdev); 327114b4164SWarner Losh MMC_LOCK(sc); 328114b4164SWarner Losh if (sc->owner) 3290554fd51SMarius Strobl panic("mmc: host bridge didn't serialize us."); 330114b4164SWarner Losh sc->owner = dev; 331114b4164SWarner Losh MMC_UNLOCK(sc); 332114b4164SWarner Losh 333114b4164SWarner Losh if (busdev != dev) { 3346fdacf52SWarner Losh /* 3356fdacf52SWarner Losh * Keep track of the last rca that we've selected. If 3366fdacf52SWarner Losh * we're asked to do it again, don't. We never 3376fdacf52SWarner Losh * unselect unless the bus code itself wants the mmc 3386fdacf52SWarner Losh * bus, and constantly reselecting causes problems. 3396fdacf52SWarner Losh */ 34072dec079SMarius Strobl ivar = device_get_ivars(dev); 34172dec079SMarius Strobl rca = ivar->rca; 342114b4164SWarner Losh if (sc->last_rca != rca) { 3430f34084fSMarius Strobl if (mmc_select_card(sc, rca) != MMC_ERR_NONE) { 344aca38eabSMarius Strobl device_printf(busdev, "Card at relative " 345aca38eabSMarius Strobl "address %d failed to select\n", rca); 3460f34084fSMarius Strobl return (ENXIO); 3470f34084fSMarius Strobl } 348114b4164SWarner Losh sc->last_rca = rca; 3490f34084fSMarius Strobl timing = mmcbr_get_timing(busdev); 350aca38eabSMarius Strobl /* 351aca38eabSMarius Strobl * For eMMC modes, setting/updating bus width and VCCQ 352aca38eabSMarius Strobl * only really is necessary if there actually is more 353aca38eabSMarius Strobl * than one device on the bus as generally that already 354aca38eabSMarius Strobl * had to be done by mmc_calculate_clock() or one of 355aca38eabSMarius Strobl * its calees. Moreover, setting the bus width anew 356aca38eabSMarius Strobl * can trigger re-tuning (via a CRC error on the next 357aca38eabSMarius Strobl * CMD), even if not switching between devices an the 358aca38eabSMarius Strobl * previously selected one is still tuned. Obviously, 359aca38eabSMarius Strobl * we need to re-tune the host controller if devices 360aca38eabSMarius Strobl * are actually switched, though. 361aca38eabSMarius Strobl */ 362aca38eabSMarius Strobl if (timing >= bus_timing_mmc_ddr52 && 363aca38eabSMarius Strobl sc->child_count == 1) 364aca38eabSMarius Strobl return (0); 365c18f1e26SAlexander Motin /* Prepare bus width for the new card. */ 366ed01be88SAlexander Motin if (bootverbose || mmc_debug) { 367c18f1e26SAlexander Motin device_printf(busdev, 3680f34084fSMarius Strobl "setting bus width to %d bits %s timing\n", 369c18f1e26SAlexander Motin (ivar->bus_width == bus_width_4) ? 4 : 3700f34084fSMarius Strobl (ivar->bus_width == bus_width_8) ? 8 : 1, 3710f34084fSMarius Strobl mmc_timing_to_string(timing)); 37267d752c3SAlexander Motin } 373aca38eabSMarius Strobl if (mmc_set_card_bus_width(sc, ivar, timing) != 374aca38eabSMarius Strobl MMC_ERR_NONE) { 375aca38eabSMarius Strobl device_printf(busdev, "Card at relative " 376aca38eabSMarius Strobl "address %d failed to set bus width\n", 3770f34084fSMarius Strobl rca); 3780f34084fSMarius Strobl return (ENXIO); 3790f34084fSMarius Strobl } 380c18f1e26SAlexander Motin mmcbr_set_bus_width(busdev, ivar->bus_width); 381c18f1e26SAlexander Motin mmcbr_update_ios(busdev); 382aca38eabSMarius Strobl if (mmc_set_vccq(sc, ivar, timing) != MMC_ERR_NONE) { 383aca38eabSMarius Strobl device_printf(busdev, "Failed to set VCCQ " 384aca38eabSMarius Strobl "for card at relative address %d\n", rca); 385aca38eabSMarius Strobl return (ENXIO); 386aca38eabSMarius Strobl } 387aca38eabSMarius Strobl if (timing >= bus_timing_mmc_hs200 && 388aca38eabSMarius Strobl mmc_retune(busdev, dev, true) != 0) { 389aca38eabSMarius Strobl device_printf(busdev, "Card at relative " 390aca38eabSMarius Strobl "address %d failed to re-tune\n", rca); 391aca38eabSMarius Strobl return (ENXIO); 392aca38eabSMarius Strobl } 393114b4164SWarner Losh } 394114b4164SWarner Losh } else { 3956fdacf52SWarner Losh /* 3966fdacf52SWarner Losh * If there's a card selected, stand down. 3976fdacf52SWarner Losh */ 398114b4164SWarner Losh if (sc->last_rca != 0) { 399aca38eabSMarius Strobl if (mmc_select_card(sc, 0) != MMC_ERR_NONE) 400aca38eabSMarius Strobl return (ENXIO); 401114b4164SWarner Losh sc->last_rca = 0; 402114b4164SWarner Losh } 403114b4164SWarner Losh } 404114b4164SWarner Losh 405114b4164SWarner Losh return (0); 406114b4164SWarner Losh } 407114b4164SWarner Losh 408114b4164SWarner Losh static int 409114b4164SWarner Losh mmc_release_bus(device_t busdev, device_t dev) 410114b4164SWarner Losh { 411114b4164SWarner Losh struct mmc_softc *sc; 412114b4164SWarner Losh int err; 413114b4164SWarner Losh 414114b4164SWarner Losh sc = device_get_softc(busdev); 415114b4164SWarner Losh 416114b4164SWarner Losh MMC_LOCK(sc); 417114b4164SWarner Losh if (!sc->owner) 418114b4164SWarner Losh panic("mmc: releasing unowned bus."); 419114b4164SWarner Losh if (sc->owner != dev) 420114b4164SWarner Losh panic("mmc: you don't own the bus. game over."); 421114b4164SWarner Losh MMC_UNLOCK(sc); 422e9bb9da3SWarner Losh err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev); 423114b4164SWarner Losh if (err) 424114b4164SWarner Losh return (err); 425114b4164SWarner Losh MMC_LOCK(sc); 426114b4164SWarner Losh sc->owner = NULL; 427114b4164SWarner Losh MMC_UNLOCK(sc); 428114b4164SWarner Losh return (0); 429114b4164SWarner Losh } 430114b4164SWarner Losh 431114b4164SWarner Losh static uint32_t 432114b4164SWarner Losh mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr) 433114b4164SWarner Losh { 4348aaa15e2SWarner Losh 435128d0ff2SWarner Losh return (ocr & MMC_OCR_VOLTAGE); 436114b4164SWarner Losh } 437114b4164SWarner Losh 438114b4164SWarner Losh static int 439114b4164SWarner Losh mmc_highest_voltage(uint32_t ocr) 440114b4164SWarner Losh { 441114b4164SWarner Losh int i; 442114b4164SWarner Losh 44310b7c3bfSOleksandr Tymoshenko for (i = MMC_OCR_MAX_VOLTAGE_SHIFT; 44410b7c3bfSOleksandr Tymoshenko i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--) 445114b4164SWarner Losh if (ocr & (1 << i)) 446128d0ff2SWarner Losh return (i); 447114b4164SWarner Losh return (-1); 448114b4164SWarner Losh } 449114b4164SWarner Losh 450114b4164SWarner Losh static void 451114b4164SWarner Losh mmc_wakeup(struct mmc_request *req) 452114b4164SWarner Losh { 453114b4164SWarner Losh struct mmc_softc *sc; 454114b4164SWarner Losh 455114b4164SWarner Losh sc = (struct mmc_softc *)req->done_data; 456114b4164SWarner Losh MMC_LOCK(sc); 457114b4164SWarner Losh req->flags |= MMC_REQ_DONE; 458114b4164SWarner Losh MMC_UNLOCK(sc); 459eb67f31aSAlexander Motin wakeup(req); 460114b4164SWarner Losh } 461114b4164SWarner Losh 462114b4164SWarner Losh static int 463114b4164SWarner Losh mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req) 464114b4164SWarner Losh { 465114b4164SWarner Losh 466114b4164SWarner Losh req->done = mmc_wakeup; 467114b4164SWarner Losh req->done_data = sc; 468aca38eabSMarius Strobl if (__predict_false(mmc_debug > 1)) { 469ed01be88SAlexander Motin device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x", 470ed01be88SAlexander Motin req->cmd->opcode, req->cmd->arg, req->cmd->flags); 471ed01be88SAlexander Motin if (req->cmd->data) { 472ed01be88SAlexander Motin printf(" data %d\n", (int)req->cmd->data->len); 473ed01be88SAlexander Motin } else 474ed01be88SAlexander Motin printf("\n"); 475ed01be88SAlexander Motin } 476114b4164SWarner Losh MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req); 477114b4164SWarner Losh MMC_LOCK(sc); 478eb67f31aSAlexander Motin while ((req->flags & MMC_REQ_DONE) == 0) 479eb67f31aSAlexander Motin msleep(req, &sc->sc_mtx, 0, "mmcreq", 0); 480114b4164SWarner Losh MMC_UNLOCK(sc); 481aca38eabSMarius Strobl if (__predict_false(mmc_debug > 2 || (mmc_debug > 0 && 482aca38eabSMarius Strobl req->cmd->error != MMC_ERR_NONE))) 483a8328210SIan Lepore device_printf(sc->dev, "CMD%d RESULT: %d\n", 484a8328210SIan Lepore req->cmd->opcode, req->cmd->error); 485eb67f31aSAlexander Motin return (0); 486114b4164SWarner Losh } 487114b4164SWarner Losh 488114b4164SWarner Losh static int 489aca38eabSMarius Strobl mmc_wait_for_request(device_t busdev, device_t dev, struct mmc_request *req) 490114b4164SWarner Losh { 491aca38eabSMarius Strobl struct mmc_softc *sc; 492aca38eabSMarius Strobl struct mmc_ivars *ivar; 493aca38eabSMarius Strobl int err, i; 494aca38eabSMarius Strobl enum mmc_retune_req retune_req; 495114b4164SWarner Losh 496aca38eabSMarius Strobl sc = device_get_softc(busdev); 497aca38eabSMarius Strobl KASSERT(sc->owner != NULL, 498aca38eabSMarius Strobl ("%s: Request from %s without bus being acquired.", __func__, 499aca38eabSMarius Strobl device_get_nameunit(dev))); 500aca38eabSMarius Strobl 501aca38eabSMarius Strobl /* 502aca38eabSMarius Strobl * Unless no device is selected or re-tuning is already ongoing, 503aca38eabSMarius Strobl * execute re-tuning if a) the bridge is requesting to do so and 504aca38eabSMarius Strobl * re-tuning hasn't been otherwise paused, or b) if a child asked 505aca38eabSMarius Strobl * to be re-tuned prior to pausing (see also mmc_retune_pause()). 506aca38eabSMarius Strobl */ 507aca38eabSMarius Strobl if (__predict_false(sc->last_rca != 0 && sc->retune_ongoing == 0 && 508aca38eabSMarius Strobl (((retune_req = mmcbr_get_retune_req(busdev)) != retune_req_none && 509aca38eabSMarius Strobl sc->retune_paused == 0) || sc->retune_needed == 1))) { 510aca38eabSMarius Strobl if (__predict_false(mmc_debug > 1)) { 511aca38eabSMarius Strobl device_printf(busdev, 512aca38eabSMarius Strobl "Re-tuning with%s circuit reset required\n", 513aca38eabSMarius Strobl retune_req == retune_req_reset ? "" : "out"); 514aca38eabSMarius Strobl } 515aca38eabSMarius Strobl if (device_get_parent(dev) == busdev) 516aca38eabSMarius Strobl ivar = device_get_ivars(dev); 517aca38eabSMarius Strobl else { 518aca38eabSMarius Strobl for (i = 0; i < sc->child_count; i++) { 519aca38eabSMarius Strobl ivar = device_get_ivars(sc->child_list[i]); 520aca38eabSMarius Strobl if (ivar->rca == sc->last_rca) 521aca38eabSMarius Strobl break; 522aca38eabSMarius Strobl } 523aca38eabSMarius Strobl if (ivar->rca != sc->last_rca) 524aca38eabSMarius Strobl return (EINVAL); 525aca38eabSMarius Strobl } 526aca38eabSMarius Strobl sc->retune_ongoing = 1; 527aca38eabSMarius Strobl err = mmc_retune(busdev, dev, retune_req == retune_req_reset); 528aca38eabSMarius Strobl sc->retune_ongoing = 0; 529aca38eabSMarius Strobl switch (err) { 530aca38eabSMarius Strobl case MMC_ERR_NONE: 531aca38eabSMarius Strobl case MMC_ERR_FAILED: /* Re-tune error but still might work */ 532aca38eabSMarius Strobl break; 533aca38eabSMarius Strobl case MMC_ERR_BADCRC: /* Switch failure on HS400 recovery */ 534aca38eabSMarius Strobl return (ENXIO); 535aca38eabSMarius Strobl case MMC_ERR_INVALID: /* Driver implementation b0rken */ 536aca38eabSMarius Strobl default: /* Unknown error, should not happen */ 537aca38eabSMarius Strobl return (EINVAL); 538aca38eabSMarius Strobl } 539aca38eabSMarius Strobl sc->retune_needed = 0; 540aca38eabSMarius Strobl } 541128d0ff2SWarner Losh return (mmc_wait_for_req(sc, req)); 542114b4164SWarner Losh } 543114b4164SWarner Losh 544114b4164SWarner Losh static int 545114b4164SWarner Losh mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 546114b4164SWarner Losh uint32_t arg, uint32_t flags, uint32_t *resp, int retries) 547114b4164SWarner Losh { 548114b4164SWarner Losh struct mmc_command cmd; 549114b4164SWarner Losh int err; 550114b4164SWarner Losh 551114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 552114b4164SWarner Losh cmd.opcode = opcode; 553114b4164SWarner Losh cmd.arg = arg; 554114b4164SWarner Losh cmd.flags = flags; 5554e0efe35SWarner Losh cmd.data = NULL; 55672dec079SMarius Strobl err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, retries); 557114b4164SWarner Losh if (err) 558114b4164SWarner Losh return (err); 559114b4164SWarner Losh if (resp) { 560114b4164SWarner Losh if (flags & MMC_RSP_136) 561114b4164SWarner Losh memcpy(resp, cmd.resp, 4 * sizeof(uint32_t)); 562114b4164SWarner Losh else 563114b4164SWarner Losh *resp = cmd.resp[0]; 564114b4164SWarner Losh } 565114b4164SWarner Losh return (0); 566114b4164SWarner Losh } 567114b4164SWarner Losh 568114b4164SWarner Losh static void 569114b4164SWarner Losh mmc_idle_cards(struct mmc_softc *sc) 570114b4164SWarner Losh { 571114b4164SWarner Losh device_t dev; 572114b4164SWarner Losh struct mmc_command cmd; 573114b4164SWarner Losh 574114b4164SWarner Losh dev = sc->dev; 575114b4164SWarner Losh mmcbr_set_chip_select(dev, cs_high); 576114b4164SWarner Losh mmcbr_update_ios(dev); 577114b4164SWarner Losh mmc_ms_delay(1); 578114b4164SWarner Losh 579114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 580114b4164SWarner Losh cmd.opcode = MMC_GO_IDLE_STATE; 581114b4164SWarner Losh cmd.arg = 0; 582114b4164SWarner Losh cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 5834e0efe35SWarner Losh cmd.data = NULL; 58472dec079SMarius Strobl mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 585114b4164SWarner Losh mmc_ms_delay(1); 586114b4164SWarner Losh 587114b4164SWarner Losh mmcbr_set_chip_select(dev, cs_dontcare); 588114b4164SWarner Losh mmcbr_update_ios(dev); 589114b4164SWarner Losh mmc_ms_delay(1); 590114b4164SWarner Losh } 591114b4164SWarner Losh 592114b4164SWarner Losh static int 593114b4164SWarner Losh mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 594114b4164SWarner Losh { 595114b4164SWarner Losh struct mmc_command cmd; 596114b4164SWarner Losh int err = MMC_ERR_NONE, i; 597114b4164SWarner Losh 598114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 599114b4164SWarner Losh cmd.opcode = ACMD_SD_SEND_OP_COND; 600114b4164SWarner Losh cmd.arg = ocr; 601114b4164SWarner Losh cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 6024e0efe35SWarner Losh cmd.data = NULL; 603114b4164SWarner Losh 6044daf3d25SPawel Jakub Dawidek for (i = 0; i < 1000; i++) { 60572dec079SMarius Strobl err = mmc_wait_for_app_cmd(sc->dev, sc->dev, 0, &cmd, 60672dec079SMarius Strobl CMD_RETRIES); 607114b4164SWarner Losh if (err != MMC_ERR_NONE) 608114b4164SWarner Losh break; 60949dd20d1SAlexander Motin if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 61049dd20d1SAlexander Motin (ocr & MMC_OCR_VOLTAGE) == 0) 611114b4164SWarner Losh break; 612114b4164SWarner Losh err = MMC_ERR_TIMEOUT; 613114b4164SWarner Losh mmc_ms_delay(10); 614114b4164SWarner Losh } 615114b4164SWarner Losh if (rocr && err == MMC_ERR_NONE) 616114b4164SWarner Losh *rocr = cmd.resp[0]; 617128d0ff2SWarner Losh return (err); 618114b4164SWarner Losh } 619114b4164SWarner Losh 620114b4164SWarner Losh static int 621114b4164SWarner Losh mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 622114b4164SWarner Losh { 623114b4164SWarner Losh struct mmc_command cmd; 624114b4164SWarner Losh int err = MMC_ERR_NONE, i; 625114b4164SWarner Losh 626114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 627114b4164SWarner Losh cmd.opcode = MMC_SEND_OP_COND; 628114b4164SWarner Losh cmd.arg = ocr; 629114b4164SWarner Losh cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 6304e0efe35SWarner Losh cmd.data = NULL; 631114b4164SWarner Losh 6324daf3d25SPawel Jakub Dawidek for (i = 0; i < 1000; i++) { 63372dec079SMarius Strobl err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 634114b4164SWarner Losh if (err != MMC_ERR_NONE) 635114b4164SWarner Losh break; 63649dd20d1SAlexander Motin if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 63749dd20d1SAlexander Motin (ocr & MMC_OCR_VOLTAGE) == 0) 638114b4164SWarner Losh break; 639114b4164SWarner Losh err = MMC_ERR_TIMEOUT; 640114b4164SWarner Losh mmc_ms_delay(10); 641114b4164SWarner Losh } 642114b4164SWarner Losh if (rocr && err == MMC_ERR_NONE) 643114b4164SWarner Losh *rocr = cmd.resp[0]; 644128d0ff2SWarner Losh return (err); 645114b4164SWarner Losh } 646114b4164SWarner Losh 647c18f1e26SAlexander Motin static int 648c18f1e26SAlexander Motin mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs) 649c18f1e26SAlexander Motin { 650c18f1e26SAlexander Motin struct mmc_command cmd; 651c18f1e26SAlexander Motin int err; 652c18f1e26SAlexander Motin 653c18f1e26SAlexander Motin memset(&cmd, 0, sizeof(cmd)); 654c18f1e26SAlexander Motin cmd.opcode = SD_SEND_IF_COND; 655c18f1e26SAlexander Motin cmd.arg = (vhs << 8) + 0xAA; 656c18f1e26SAlexander Motin cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR; 657c18f1e26SAlexander Motin cmd.data = NULL; 658c18f1e26SAlexander Motin 65972dec079SMarius Strobl err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 660c18f1e26SAlexander Motin return (err); 661c18f1e26SAlexander Motin } 662c18f1e26SAlexander Motin 663114b4164SWarner Losh static void 664114b4164SWarner Losh mmc_power_up(struct mmc_softc *sc) 665114b4164SWarner Losh { 666114b4164SWarner Losh device_t dev; 6670f34084fSMarius Strobl enum mmc_vccq vccq; 668114b4164SWarner Losh 669114b4164SWarner Losh dev = sc->dev; 670114b4164SWarner Losh mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev))); 671114b4164SWarner Losh mmcbr_set_bus_mode(dev, opendrain); 672114b4164SWarner Losh mmcbr_set_chip_select(dev, cs_dontcare); 673114b4164SWarner Losh mmcbr_set_bus_width(dev, bus_width_1); 674114b4164SWarner Losh mmcbr_set_power_mode(dev, power_up); 675114b4164SWarner Losh mmcbr_set_clock(dev, 0); 676114b4164SWarner Losh mmcbr_update_ios(dev); 6770f34084fSMarius Strobl for (vccq = vccq_330; ; vccq--) { 6780f34084fSMarius Strobl mmcbr_set_vccq(dev, vccq); 6790f34084fSMarius Strobl if (mmcbr_switch_vccq(dev) == 0 || vccq == vccq_120) 6800f34084fSMarius Strobl break; 6810f34084fSMarius Strobl } 682114b4164SWarner Losh mmc_ms_delay(1); 683114b4164SWarner Losh 6840f34084fSMarius Strobl mmcbr_set_clock(dev, SD_MMC_CARD_ID_FREQUENCY); 685c18f1e26SAlexander Motin mmcbr_set_timing(dev, bus_timing_normal); 686114b4164SWarner Losh mmcbr_set_power_mode(dev, power_on); 687114b4164SWarner Losh mmcbr_update_ios(dev); 688114b4164SWarner Losh mmc_ms_delay(2); 689114b4164SWarner Losh } 690114b4164SWarner Losh 6915d2f6510SWarner Losh static void 6925d2f6510SWarner Losh mmc_power_down(struct mmc_softc *sc) 6935d2f6510SWarner Losh { 6945d2f6510SWarner Losh device_t dev = sc->dev; 6955d2f6510SWarner Losh 6965d2f6510SWarner Losh mmcbr_set_bus_mode(dev, opendrain); 6975d2f6510SWarner Losh mmcbr_set_chip_select(dev, cs_dontcare); 6985d2f6510SWarner Losh mmcbr_set_bus_width(dev, bus_width_1); 6995d2f6510SWarner Losh mmcbr_set_power_mode(dev, power_off); 7005d2f6510SWarner Losh mmcbr_set_clock(dev, 0); 701c18f1e26SAlexander Motin mmcbr_set_timing(dev, bus_timing_normal); 7025d2f6510SWarner Losh mmcbr_update_ios(dev); 7035d2f6510SWarner Losh } 7045d2f6510SWarner Losh 705c18f1e26SAlexander Motin static int 706c18f1e26SAlexander Motin mmc_select_card(struct mmc_softc *sc, uint16_t rca) 707c18f1e26SAlexander Motin { 708aca38eabSMarius Strobl int err, flags; 709b231f5d9SWarner Losh 710b231f5d9SWarner Losh flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC; 711aca38eabSMarius Strobl sc->retune_paused++; 712aca38eabSMarius Strobl err = mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16, 713aca38eabSMarius Strobl flags, NULL, CMD_RETRIES); 714aca38eabSMarius Strobl sc->retune_paused--; 715aca38eabSMarius Strobl return (err); 716c18f1e26SAlexander Motin } 717c18f1e26SAlexander Motin 718c18f1e26SAlexander Motin static int 719711873d4SWarner Losh mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value, 720711873d4SWarner Losh uint8_t *res) 721c18f1e26SAlexander Motin { 722c18f1e26SAlexander Motin int err; 723c18f1e26SAlexander Motin struct mmc_command cmd; 724c18f1e26SAlexander Motin struct mmc_data data; 725c18f1e26SAlexander Motin 726ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 727ed7142a7SIan Lepore memset(&data, 0, sizeof(data)); 728c18f1e26SAlexander Motin memset(res, 0, 64); 729711873d4SWarner Losh 730c18f1e26SAlexander Motin cmd.opcode = SD_SWITCH_FUNC; 731c18f1e26SAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 732711873d4SWarner Losh cmd.arg = mode << 31; /* 0 - check, 1 - set */ 733c18f1e26SAlexander Motin cmd.arg |= 0x00FFFFFF; 73407492efeSAlexander Motin cmd.arg &= ~(0xF << (grp * 4)); 73507492efeSAlexander Motin cmd.arg |= value << (grp * 4); 736c18f1e26SAlexander Motin cmd.data = &data; 737c18f1e26SAlexander Motin 738c18f1e26SAlexander Motin data.data = res; 739c18f1e26SAlexander Motin data.len = 64; 740c18f1e26SAlexander Motin data.flags = MMC_DATA_READ; 741c18f1e26SAlexander Motin 74272dec079SMarius Strobl err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 743c18f1e26SAlexander Motin return (err); 744c18f1e26SAlexander Motin } 745c18f1e26SAlexander Motin 746c18f1e26SAlexander Motin static int 747aca38eabSMarius Strobl mmc_set_card_bus_width(struct mmc_softc *sc, struct mmc_ivars *ivar, 748aca38eabSMarius Strobl enum mmc_bus_timing timing) 749c18f1e26SAlexander Motin { 75038d1bc23SWarner Losh struct mmc_command cmd; 751c18f1e26SAlexander Motin int err; 75238d1bc23SWarner Losh uint8_t value; 753c18f1e26SAlexander Motin 754c18f1e26SAlexander Motin if (mmcbr_get_mode(sc->dev) == mode_sd) { 755ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 7567aa65846SMarius Strobl cmd.opcode = ACMD_SET_CLR_CARD_DETECT; 7577aa65846SMarius Strobl cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 7587aa65846SMarius Strobl cmd.arg = SD_CLR_CARD_DETECT; 75972dec079SMarius Strobl err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd, 76072dec079SMarius Strobl CMD_RETRIES); 7617aa65846SMarius Strobl if (err != 0) 7627aa65846SMarius Strobl return (err); 763ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 764c18f1e26SAlexander Motin cmd.opcode = ACMD_SET_BUS_WIDTH; 765c18f1e26SAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 76672dec079SMarius Strobl switch (ivar->bus_width) { 767c18f1e26SAlexander Motin case bus_width_1: 768c18f1e26SAlexander Motin cmd.arg = SD_BUS_WIDTH_1; 769c18f1e26SAlexander Motin break; 770c18f1e26SAlexander Motin case bus_width_4: 771c18f1e26SAlexander Motin cmd.arg = SD_BUS_WIDTH_4; 772c18f1e26SAlexander Motin break; 773c18f1e26SAlexander Motin default: 774c18f1e26SAlexander Motin return (MMC_ERR_INVALID); 775c18f1e26SAlexander Motin } 77672dec079SMarius Strobl err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd, 77772dec079SMarius Strobl CMD_RETRIES); 778c18f1e26SAlexander Motin } else { 77972dec079SMarius Strobl switch (ivar->bus_width) { 780c18f1e26SAlexander Motin case bus_width_1: 781aca38eabSMarius Strobl if (timing == bus_timing_mmc_hs400 || 782aca38eabSMarius Strobl timing == bus_timing_mmc_hs400es) 783aca38eabSMarius Strobl return (MMC_ERR_INVALID); 784c18f1e26SAlexander Motin value = EXT_CSD_BUS_WIDTH_1; 785c18f1e26SAlexander Motin break; 786c18f1e26SAlexander Motin case bus_width_4: 787aca38eabSMarius Strobl switch (timing) { 7880f34084fSMarius Strobl case bus_timing_mmc_ddr52: 7890f34084fSMarius Strobl value = EXT_CSD_BUS_WIDTH_4_DDR; 7900f34084fSMarius Strobl break; 791aca38eabSMarius Strobl case bus_timing_mmc_hs400: 792aca38eabSMarius Strobl case bus_timing_mmc_hs400es: 793aca38eabSMarius Strobl return (MMC_ERR_INVALID); 7940f34084fSMarius Strobl default: 795c18f1e26SAlexander Motin value = EXT_CSD_BUS_WIDTH_4; 796c18f1e26SAlexander Motin break; 7970f34084fSMarius Strobl } 7980f34084fSMarius Strobl break; 799c18f1e26SAlexander Motin case bus_width_8: 800aca38eabSMarius Strobl value = 0; 801aca38eabSMarius Strobl switch (timing) { 8020f34084fSMarius Strobl case bus_timing_mmc_hs400es: 803aca38eabSMarius Strobl value = EXT_CSD_BUS_WIDTH_ES; 804aca38eabSMarius Strobl /* FALLTHROUGH */ 805aca38eabSMarius Strobl case bus_timing_mmc_ddr52: 806aca38eabSMarius Strobl case bus_timing_mmc_hs400: 807aca38eabSMarius Strobl value |= EXT_CSD_BUS_WIDTH_8_DDR; 8080f34084fSMarius Strobl break; 8090f34084fSMarius Strobl default: 810c18f1e26SAlexander Motin value = EXT_CSD_BUS_WIDTH_8; 811c18f1e26SAlexander Motin break; 8120f34084fSMarius Strobl } 8130f34084fSMarius Strobl break; 814c18f1e26SAlexander Motin default: 815c18f1e26SAlexander Motin return (MMC_ERR_INVALID); 816c18f1e26SAlexander Motin } 81772dec079SMarius Strobl err = mmc_switch(sc->dev, sc->dev, ivar->rca, 81872dec079SMarius Strobl EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, value, 81972dec079SMarius Strobl ivar->cmd6_time, true); 820c18f1e26SAlexander Motin } 821c18f1e26SAlexander Motin return (err); 822c18f1e26SAlexander Motin } 823c18f1e26SAlexander Motin 824c18f1e26SAlexander Motin static int 8250f34084fSMarius Strobl mmc_set_power_class(struct mmc_softc *sc, struct mmc_ivars *ivar) 8260f34084fSMarius Strobl { 8270f34084fSMarius Strobl device_t dev; 8280f34084fSMarius Strobl const uint8_t *ext_csd; 8290f34084fSMarius Strobl uint32_t clock; 8300f34084fSMarius Strobl uint8_t value; 8310f34084fSMarius Strobl 8320f34084fSMarius Strobl dev = sc->dev; 8330f34084fSMarius Strobl if (mmcbr_get_mode(dev) != mode_mmc || ivar->csd.spec_vers < 4) 8340f34084fSMarius Strobl return (MMC_ERR_NONE); 8350f34084fSMarius Strobl 8360f34084fSMarius Strobl value = 0; 8370f34084fSMarius Strobl ext_csd = ivar->raw_ext_csd; 8380f34084fSMarius Strobl clock = mmcbr_get_clock(dev); 8390f34084fSMarius Strobl switch (1 << mmcbr_get_vdd(dev)) { 8400f34084fSMarius Strobl case MMC_OCR_LOW_VOLTAGE: 8410f34084fSMarius Strobl if (clock <= MMC_TYPE_HS_26_MAX) 8420f34084fSMarius Strobl value = ext_csd[EXT_CSD_PWR_CL_26_195]; 8430f34084fSMarius Strobl else if (clock <= MMC_TYPE_HS_52_MAX) { 8440f34084fSMarius Strobl if (mmcbr_get_timing(dev) >= bus_timing_mmc_ddr52 && 8450f34084fSMarius Strobl ivar->bus_width >= bus_width_4) 8460f34084fSMarius Strobl value = ext_csd[EXT_CSD_PWR_CL_52_195_DDR]; 8470f34084fSMarius Strobl else 8480f34084fSMarius Strobl value = ext_csd[EXT_CSD_PWR_CL_52_195]; 8490f34084fSMarius Strobl } else if (clock <= MMC_TYPE_HS200_HS400ES_MAX) 8500f34084fSMarius Strobl value = ext_csd[EXT_CSD_PWR_CL_200_195]; 8510f34084fSMarius Strobl break; 8520f34084fSMarius Strobl case MMC_OCR_270_280: 8530f34084fSMarius Strobl case MMC_OCR_280_290: 8540f34084fSMarius Strobl case MMC_OCR_290_300: 8550f34084fSMarius Strobl case MMC_OCR_300_310: 8560f34084fSMarius Strobl case MMC_OCR_310_320: 8570f34084fSMarius Strobl case MMC_OCR_320_330: 8580f34084fSMarius Strobl case MMC_OCR_330_340: 8590f34084fSMarius Strobl case MMC_OCR_340_350: 8600f34084fSMarius Strobl case MMC_OCR_350_360: 8610f34084fSMarius Strobl if (clock <= MMC_TYPE_HS_26_MAX) 8620f34084fSMarius Strobl value = ext_csd[EXT_CSD_PWR_CL_26_360]; 8630f34084fSMarius Strobl else if (clock <= MMC_TYPE_HS_52_MAX) { 8640f34084fSMarius Strobl if (mmcbr_get_timing(dev) == bus_timing_mmc_ddr52 && 8650f34084fSMarius Strobl ivar->bus_width >= bus_width_4) 8660f34084fSMarius Strobl value = ext_csd[EXT_CSD_PWR_CL_52_360_DDR]; 8670f34084fSMarius Strobl else 8680f34084fSMarius Strobl value = ext_csd[EXT_CSD_PWR_CL_52_360]; 8690f34084fSMarius Strobl } else if (clock <= MMC_TYPE_HS200_HS400ES_MAX) { 8700f34084fSMarius Strobl if (ivar->bus_width == bus_width_8) 8710f34084fSMarius Strobl value = ext_csd[EXT_CSD_PWR_CL_200_360_DDR]; 8720f34084fSMarius Strobl else 8730f34084fSMarius Strobl value = ext_csd[EXT_CSD_PWR_CL_200_360]; 8740f34084fSMarius Strobl } 8750f34084fSMarius Strobl break; 8760f34084fSMarius Strobl default: 8770f34084fSMarius Strobl device_printf(dev, "No power class support for VDD 0x%x\n", 8780f34084fSMarius Strobl 1 << mmcbr_get_vdd(dev)); 8790f34084fSMarius Strobl return (MMC_ERR_INVALID); 8800f34084fSMarius Strobl } 8810f34084fSMarius Strobl 8820f34084fSMarius Strobl if (ivar->bus_width == bus_width_8) 8830f34084fSMarius Strobl value = (value & EXT_CSD_POWER_CLASS_8BIT_MASK) >> 8840f34084fSMarius Strobl EXT_CSD_POWER_CLASS_8BIT_SHIFT; 8850f34084fSMarius Strobl else 8860f34084fSMarius Strobl value = (value & EXT_CSD_POWER_CLASS_4BIT_MASK) >> 8870f34084fSMarius Strobl EXT_CSD_POWER_CLASS_4BIT_SHIFT; 8880f34084fSMarius Strobl 8890f34084fSMarius Strobl if (value == 0) 8900f34084fSMarius Strobl return (MMC_ERR_NONE); 8910f34084fSMarius Strobl 8920f34084fSMarius Strobl return (mmc_switch(dev, dev, ivar->rca, EXT_CSD_CMD_SET_NORMAL, 8930f34084fSMarius Strobl EXT_CSD_POWER_CLASS, value, ivar->cmd6_time, true)); 8940f34084fSMarius Strobl } 8950f34084fSMarius Strobl 8960f34084fSMarius Strobl static int 8970f34084fSMarius Strobl mmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar, 8980f34084fSMarius Strobl enum mmc_bus_timing timing) 899c18f1e26SAlexander Motin { 9007e6ccea3SMarius Strobl u_char switch_res[64]; 901c18f1e26SAlexander Motin uint8_t value; 90272dec079SMarius Strobl int err; 903c18f1e26SAlexander Motin 9040f34084fSMarius Strobl if (mmcbr_get_mode(sc->dev) == mode_sd) { 905c18f1e26SAlexander Motin switch (timing) { 906c18f1e26SAlexander Motin case bus_timing_normal: 9070f34084fSMarius Strobl value = SD_SWITCH_NORMAL_MODE; 908c18f1e26SAlexander Motin break; 909c18f1e26SAlexander Motin case bus_timing_hs: 9100f34084fSMarius Strobl value = SD_SWITCH_HS_MODE; 911c18f1e26SAlexander Motin break; 912c18f1e26SAlexander Motin default: 913c18f1e26SAlexander Motin return (MMC_ERR_INVALID); 914c18f1e26SAlexander Motin } 915711873d4SWarner Losh err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1, 916711873d4SWarner Losh value, switch_res); 91772dec079SMarius Strobl if (err != MMC_ERR_NONE) 91872dec079SMarius Strobl return (err); 91972dec079SMarius Strobl if ((switch_res[16] & 0xf) != value) 92072dec079SMarius Strobl return (MMC_ERR_FAILED); 92172dec079SMarius Strobl mmcbr_set_timing(sc->dev, timing); 92272dec079SMarius Strobl mmcbr_update_ios(sc->dev); 92372dec079SMarius Strobl } else { 9240f34084fSMarius Strobl switch (timing) { 9250f34084fSMarius Strobl case bus_timing_normal: 9260f34084fSMarius Strobl value = EXT_CSD_HS_TIMING_BC; 9270f34084fSMarius Strobl break; 9280f34084fSMarius Strobl case bus_timing_hs: 9290f34084fSMarius Strobl case bus_timing_mmc_ddr52: 9300f34084fSMarius Strobl value = EXT_CSD_HS_TIMING_HS; 9310f34084fSMarius Strobl break; 932aca38eabSMarius Strobl case bus_timing_mmc_hs200: 933aca38eabSMarius Strobl value = EXT_CSD_HS_TIMING_HS200; 934aca38eabSMarius Strobl break; 935aca38eabSMarius Strobl case bus_timing_mmc_hs400: 936aca38eabSMarius Strobl case bus_timing_mmc_hs400es: 937aca38eabSMarius Strobl value = EXT_CSD_HS_TIMING_HS400; 938aca38eabSMarius Strobl break; 9390f34084fSMarius Strobl default: 9400f34084fSMarius Strobl return (MMC_ERR_INVALID); 9410f34084fSMarius Strobl } 94272dec079SMarius Strobl err = mmc_switch(sc->dev, sc->dev, ivar->rca, 94372dec079SMarius Strobl EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, value, 94472dec079SMarius Strobl ivar->cmd6_time, false); 94572dec079SMarius Strobl if (err != MMC_ERR_NONE) 94672dec079SMarius Strobl return (err); 94772dec079SMarius Strobl mmcbr_set_timing(sc->dev, timing); 94872dec079SMarius Strobl mmcbr_update_ios(sc->dev); 94972dec079SMarius Strobl err = mmc_switch_status(sc->dev, sc->dev, ivar->rca, 95072dec079SMarius Strobl ivar->cmd6_time); 95172dec079SMarius Strobl } 952c18f1e26SAlexander Motin return (err); 953c18f1e26SAlexander Motin } 954c18f1e26SAlexander Motin 955aca38eabSMarius Strobl static int 956aca38eabSMarius Strobl mmc_set_vccq(struct mmc_softc *sc, struct mmc_ivars *ivar, 957aca38eabSMarius Strobl enum mmc_bus_timing timing) 958aca38eabSMarius Strobl { 959aca38eabSMarius Strobl 960aca38eabSMarius Strobl if (isset(&ivar->vccq_120, timing)) 961aca38eabSMarius Strobl mmcbr_set_vccq(sc->dev, vccq_120); 962aca38eabSMarius Strobl else if (isset(&ivar->vccq_180, timing)) 963aca38eabSMarius Strobl mmcbr_set_vccq(sc->dev, vccq_180); 964aca38eabSMarius Strobl else 965aca38eabSMarius Strobl mmcbr_set_vccq(sc->dev, vccq_330); 966aca38eabSMarius Strobl if (mmcbr_switch_vccq(sc->dev) != 0) 967aca38eabSMarius Strobl return (MMC_ERR_INVALID); 968aca38eabSMarius Strobl else 969aca38eabSMarius Strobl return (MMC_ERR_NONE); 970aca38eabSMarius Strobl } 971aca38eabSMarius Strobl 972b440e965SMarius Strobl static const uint8_t p8[8] = { 973b440e965SMarius Strobl 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 974b440e965SMarius Strobl }; 975b440e965SMarius Strobl 976b440e965SMarius Strobl static const uint8_t p8ok[8] = { 977b440e965SMarius Strobl 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 978b440e965SMarius Strobl }; 979b440e965SMarius Strobl 980b440e965SMarius Strobl static const uint8_t p4[4] = { 981b440e965SMarius Strobl 0x5A, 0x00, 0x00, 0x00 982b440e965SMarius Strobl }; 983b440e965SMarius Strobl 984b440e965SMarius Strobl static const uint8_t p4ok[4] = { 985b440e965SMarius Strobl 0xA5, 0x00, 0x00, 0x00 986b440e965SMarius Strobl }; 987b440e965SMarius Strobl 988c18f1e26SAlexander Motin static int 989c18f1e26SAlexander Motin mmc_test_bus_width(struct mmc_softc *sc) 990c18f1e26SAlexander Motin { 991c18f1e26SAlexander Motin struct mmc_command cmd; 992c18f1e26SAlexander Motin struct mmc_data data; 993c18f1e26SAlexander Motin uint8_t buf[8]; 994b440e965SMarius Strobl int err; 995c18f1e26SAlexander Motin 996c18f1e26SAlexander Motin if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) { 997c18f1e26SAlexander Motin mmcbr_set_bus_width(sc->dev, bus_width_8); 998c18f1e26SAlexander Motin mmcbr_update_ios(sc->dev); 999c18f1e26SAlexander Motin 1000dc47198fSIan Lepore sc->squelched++; /* Errors are expected, squelch reporting. */ 1001ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 1002ed7142a7SIan Lepore memset(&data, 0, sizeof(data)); 1003c18f1e26SAlexander Motin cmd.opcode = MMC_BUSTEST_W; 1004c18f1e26SAlexander Motin cmd.arg = 0; 1005c18f1e26SAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1006c18f1e26SAlexander Motin cmd.data = &data; 1007c18f1e26SAlexander Motin 1008b440e965SMarius Strobl data.data = __DECONST(void *, p8); 1009c18f1e26SAlexander Motin data.len = 8; 1010c18f1e26SAlexander Motin data.flags = MMC_DATA_WRITE; 101172dec079SMarius Strobl mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0); 1012c18f1e26SAlexander Motin 1013ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 1014ed7142a7SIan Lepore memset(&data, 0, sizeof(data)); 1015c18f1e26SAlexander Motin cmd.opcode = MMC_BUSTEST_R; 1016c18f1e26SAlexander Motin cmd.arg = 0; 1017c18f1e26SAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1018c18f1e26SAlexander Motin cmd.data = &data; 1019c18f1e26SAlexander Motin 1020c18f1e26SAlexander Motin data.data = buf; 1021c18f1e26SAlexander Motin data.len = 8; 1022c18f1e26SAlexander Motin data.flags = MMC_DATA_READ; 102372dec079SMarius Strobl err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0); 1024dc47198fSIan Lepore sc->squelched--; 1025c18f1e26SAlexander Motin 1026c18f1e26SAlexander Motin mmcbr_set_bus_width(sc->dev, bus_width_1); 1027c18f1e26SAlexander Motin mmcbr_update_ios(sc->dev); 1028c18f1e26SAlexander Motin 1029c18f1e26SAlexander Motin if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0) 1030c18f1e26SAlexander Motin return (bus_width_8); 1031c18f1e26SAlexander Motin } 1032c18f1e26SAlexander Motin 1033c18f1e26SAlexander Motin if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) { 1034c18f1e26SAlexander Motin mmcbr_set_bus_width(sc->dev, bus_width_4); 1035c18f1e26SAlexander Motin mmcbr_update_ios(sc->dev); 1036c18f1e26SAlexander Motin 1037dc47198fSIan Lepore sc->squelched++; /* Errors are expected, squelch reporting. */ 1038ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 1039ed7142a7SIan Lepore memset(&data, 0, sizeof(data)); 1040c18f1e26SAlexander Motin cmd.opcode = MMC_BUSTEST_W; 1041c18f1e26SAlexander Motin cmd.arg = 0; 1042c18f1e26SAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1043c18f1e26SAlexander Motin cmd.data = &data; 1044c18f1e26SAlexander Motin 1045b440e965SMarius Strobl data.data = __DECONST(void *, p4); 1046c18f1e26SAlexander Motin data.len = 4; 1047c18f1e26SAlexander Motin data.flags = MMC_DATA_WRITE; 104872dec079SMarius Strobl mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0); 1049c18f1e26SAlexander Motin 1050ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 1051ed7142a7SIan Lepore memset(&data, 0, sizeof(data)); 1052c18f1e26SAlexander Motin cmd.opcode = MMC_BUSTEST_R; 1053c18f1e26SAlexander Motin cmd.arg = 0; 1054c18f1e26SAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1055c18f1e26SAlexander Motin cmd.data = &data; 1056c18f1e26SAlexander Motin 1057c18f1e26SAlexander Motin data.data = buf; 1058c18f1e26SAlexander Motin data.len = 4; 1059c18f1e26SAlexander Motin data.flags = MMC_DATA_READ; 106072dec079SMarius Strobl err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0); 1061dc47198fSIan Lepore sc->squelched--; 1062c18f1e26SAlexander Motin 1063c18f1e26SAlexander Motin mmcbr_set_bus_width(sc->dev, bus_width_1); 1064c18f1e26SAlexander Motin mmcbr_update_ios(sc->dev); 1065c18f1e26SAlexander Motin 1066c18f1e26SAlexander Motin if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0) 1067c18f1e26SAlexander Motin return (bus_width_4); 1068c18f1e26SAlexander Motin } 1069c18f1e26SAlexander Motin return (bus_width_1); 1070c18f1e26SAlexander Motin } 1071c18f1e26SAlexander Motin 1072114b4164SWarner Losh static uint32_t 10733906d42dSAlexander Motin mmc_get_bits(uint32_t *bits, int bit_len, int start, int size) 1074114b4164SWarner Losh { 10754871891eSWarner Losh const int i = (bit_len / 32) - (start / 32) - 1; 1076114b4164SWarner Losh const int shift = start & 31; 1077114b4164SWarner Losh uint32_t retval = bits[i] >> shift; 1078c11bbc7dSMarius Strobl 1079114b4164SWarner Losh if (size + shift > 32) 1080114b4164SWarner Losh retval |= bits[i - 1] << (32 - shift); 10818b09b5b1SAlexander Motin return (retval & ((1llu << size) - 1)); 1082114b4164SWarner Losh } 1083114b4164SWarner Losh 1084114b4164SWarner Losh static void 10854871891eSWarner Losh mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid) 1086114b4164SWarner Losh { 1087114b4164SWarner Losh int i; 1088114b4164SWarner Losh 1089114b4164SWarner Losh /* There's no version info, so we take it on faith */ 10904871891eSWarner Losh memset(cid, 0, sizeof(*cid)); 10913906d42dSAlexander Motin cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 10923906d42dSAlexander Motin cid->oid = mmc_get_bits(raw_cid, 128, 104, 16); 1093114b4164SWarner Losh for (i = 0; i < 5; i++) 10943906d42dSAlexander Motin cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 1095ed01be88SAlexander Motin cid->pnm[5] = 0; 10963906d42dSAlexander Motin cid->prv = mmc_get_bits(raw_cid, 128, 56, 8); 10973906d42dSAlexander Motin cid->psn = mmc_get_bits(raw_cid, 128, 24, 32); 1098ed01be88SAlexander Motin cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000; 10993906d42dSAlexander Motin cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4); 11004871891eSWarner Losh } 11014871891eSWarner Losh 11024871891eSWarner Losh static void 110372dec079SMarius Strobl mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid, bool is_4_41p) 11044871891eSWarner Losh { 11054871891eSWarner Losh int i; 11064871891eSWarner Losh 11074871891eSWarner Losh /* There's no version info, so we take it on faith */ 11084871891eSWarner Losh memset(cid, 0, sizeof(*cid)); 11093906d42dSAlexander Motin cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 11103906d42dSAlexander Motin cid->oid = mmc_get_bits(raw_cid, 128, 104, 8); 1111c18f1e26SAlexander Motin for (i = 0; i < 6; i++) 11123906d42dSAlexander Motin cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 1113ed01be88SAlexander Motin cid->pnm[6] = 0; 11143906d42dSAlexander Motin cid->prv = mmc_get_bits(raw_cid, 128, 48, 8); 11153906d42dSAlexander Motin cid->psn = mmc_get_bits(raw_cid, 128, 16, 32); 11163906d42dSAlexander Motin cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4); 111772dec079SMarius Strobl cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4); 111872dec079SMarius Strobl if (is_4_41p) 111972dec079SMarius Strobl cid->mdt_year += 2013; 112072dec079SMarius Strobl else 112172dec079SMarius Strobl cid->mdt_year += 1997; 1122114b4164SWarner Losh } 1123114b4164SWarner Losh 11247aa65846SMarius Strobl static void 11257aa65846SMarius Strobl mmc_format_card_id_string(struct mmc_ivars *ivar) 11267aa65846SMarius Strobl { 11277aa65846SMarius Strobl char oidstr[8]; 11287aa65846SMarius Strobl uint8_t c1; 11297aa65846SMarius Strobl uint8_t c2; 11307aa65846SMarius Strobl 11317aa65846SMarius Strobl /* 11327aa65846SMarius Strobl * Format a card ID string for use by the mmcsd driver, it's what 11337aa65846SMarius Strobl * appears between the <> in the following: 1134*79f39c6aSMarius Strobl * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 MFG 08/2008 by 3 TN> at mmc0 11357aa65846SMarius Strobl * 22.5MHz/4bit/128-block 11367aa65846SMarius Strobl * 1137f3a4b7f7SIan Lepore * Also format just the card serial number, which the mmcsd driver will 1138f3a4b7f7SIan Lepore * use as the disk->d_ident string. 1139f3a4b7f7SIan Lepore * 11407aa65846SMarius Strobl * The card_id_string in mmc_ivars is currently allocated as 64 bytes, 11417aa65846SMarius Strobl * and our max formatted length is currently 55 bytes if every field 11427aa65846SMarius Strobl * contains the largest value. 11437aa65846SMarius Strobl * 11447aa65846SMarius Strobl * Sometimes the oid is two printable ascii chars; when it's not, 11457aa65846SMarius Strobl * format it as 0xnnnn instead. 11467aa65846SMarius Strobl */ 11477aa65846SMarius Strobl c1 = (ivar->cid.oid >> 8) & 0x0ff; 11487aa65846SMarius Strobl c2 = ivar->cid.oid & 0x0ff; 11497aa65846SMarius Strobl if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f) 11507aa65846SMarius Strobl snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2); 11517aa65846SMarius Strobl else 11527aa65846SMarius Strobl snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid); 1153f3a4b7f7SIan Lepore snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string), 1154f3a4b7f7SIan Lepore "%08X", ivar->cid.psn); 11557aa65846SMarius Strobl snprintf(ivar->card_id_string, sizeof(ivar->card_id_string), 1156f3a4b7f7SIan Lepore "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s", 11577aa65846SMarius Strobl ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "", 11587aa65846SMarius Strobl ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f, 11597aa65846SMarius Strobl ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year, 11607aa65846SMarius Strobl ivar->cid.mid, oidstr); 11617aa65846SMarius Strobl } 11627aa65846SMarius Strobl 1163114b4164SWarner Losh static const int exp[8] = { 1164114b4164SWarner Losh 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 1165114b4164SWarner Losh }; 11667aa65846SMarius Strobl 1167114b4164SWarner Losh static const int mant[16] = { 11687aa65846SMarius Strobl 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 1169114b4164SWarner Losh }; 11707aa65846SMarius Strobl 1171114b4164SWarner Losh static const int cur_min[8] = { 1172114b4164SWarner Losh 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000 1173114b4164SWarner Losh }; 11747aa65846SMarius Strobl 1175114b4164SWarner Losh static const int cur_max[8] = { 1176114b4164SWarner Losh 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000 1177114b4164SWarner Losh }; 1178114b4164SWarner Losh 1179aca38eabSMarius Strobl static int 11804871891eSWarner Losh mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd) 1181114b4164SWarner Losh { 1182114b4164SWarner Losh int v; 1183114b4164SWarner Losh int m; 1184114b4164SWarner Losh int e; 1185114b4164SWarner Losh 1186114b4164SWarner Losh memset(csd, 0, sizeof(*csd)); 11873906d42dSAlexander Motin csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2); 1188114b4164SWarner Losh if (v == 0) { 11893906d42dSAlexander Motin m = mmc_get_bits(raw_csd, 128, 115, 4); 11903906d42dSAlexander Motin e = mmc_get_bits(raw_csd, 128, 112, 3); 11910554fd51SMarius Strobl csd->tacc = (exp[e] * mant[m] + 9) / 10; 11923906d42dSAlexander Motin csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 11933906d42dSAlexander Motin m = mmc_get_bits(raw_csd, 128, 99, 4); 11943906d42dSAlexander Motin e = mmc_get_bits(raw_csd, 128, 96, 3); 1195114b4164SWarner Losh csd->tran_speed = exp[e] * 10000 * mant[m]; 11963906d42dSAlexander Motin csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 11973906d42dSAlexander Motin csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 11983906d42dSAlexander Motin csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 11993906d42dSAlexander Motin csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 12003906d42dSAlexander Motin csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 12013906d42dSAlexander Motin csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 12021bacf3beSMarius Strobl csd->vdd_r_curr_min = 12031bacf3beSMarius Strobl cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 12041bacf3beSMarius Strobl csd->vdd_r_curr_max = 12051bacf3beSMarius Strobl cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 12061bacf3beSMarius Strobl csd->vdd_w_curr_min = 12071bacf3beSMarius Strobl cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 12081bacf3beSMarius Strobl csd->vdd_w_curr_max = 12091bacf3beSMarius Strobl cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 12103906d42dSAlexander Motin m = mmc_get_bits(raw_csd, 128, 62, 12); 12113906d42dSAlexander Motin e = mmc_get_bits(raw_csd, 128, 47, 3); 1212114b4164SWarner Losh csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 12133906d42dSAlexander Motin csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 12143906d42dSAlexander Motin csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 12153906d42dSAlexander Motin csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 12163906d42dSAlexander Motin csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 12173906d42dSAlexander Motin csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 12183906d42dSAlexander Motin csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 12193906d42dSAlexander Motin csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 1220aca38eabSMarius Strobl return (MMC_ERR_NONE); 1221114b4164SWarner Losh } else if (v == 1) { 12223906d42dSAlexander Motin m = mmc_get_bits(raw_csd, 128, 115, 4); 12233906d42dSAlexander Motin e = mmc_get_bits(raw_csd, 128, 112, 3); 12240554fd51SMarius Strobl csd->tacc = (exp[e] * mant[m] + 9) / 10; 12253906d42dSAlexander Motin csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 12263906d42dSAlexander Motin m = mmc_get_bits(raw_csd, 128, 99, 4); 12273906d42dSAlexander Motin e = mmc_get_bits(raw_csd, 128, 96, 3); 1228c18f1e26SAlexander Motin csd->tran_speed = exp[e] * 10000 * mant[m]; 12293906d42dSAlexander Motin csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 12303906d42dSAlexander Motin csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 12313906d42dSAlexander Motin csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 12323906d42dSAlexander Motin csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 12333906d42dSAlexander Motin csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 12343906d42dSAlexander Motin csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 12351bacf3beSMarius Strobl csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 12361bacf3beSMarius Strobl 1) * 512 * 1024; 12373906d42dSAlexander Motin csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 12383906d42dSAlexander Motin csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 12393906d42dSAlexander Motin csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 12403906d42dSAlexander Motin csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 12413906d42dSAlexander Motin csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 12423906d42dSAlexander Motin csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 12433906d42dSAlexander Motin csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 1244aca38eabSMarius Strobl return (MMC_ERR_NONE); 1245aca38eabSMarius Strobl } 1246aca38eabSMarius Strobl return (MMC_ERR_INVALID); 12474871891eSWarner Losh } 12484871891eSWarner Losh 12494871891eSWarner Losh static void 12504871891eSWarner Losh mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd) 12514871891eSWarner Losh { 12524871891eSWarner Losh int m; 12534871891eSWarner Losh int e; 12544871891eSWarner Losh 12554871891eSWarner Losh memset(csd, 0, sizeof(*csd)); 12563906d42dSAlexander Motin csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2); 12573906d42dSAlexander Motin csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4); 12583906d42dSAlexander Motin m = mmc_get_bits(raw_csd, 128, 115, 4); 12593906d42dSAlexander Motin e = mmc_get_bits(raw_csd, 128, 112, 3); 1260c18f1e26SAlexander Motin csd->tacc = exp[e] * mant[m] + 9 / 10; 12613906d42dSAlexander Motin csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 12623906d42dSAlexander Motin m = mmc_get_bits(raw_csd, 128, 99, 4); 12633906d42dSAlexander Motin e = mmc_get_bits(raw_csd, 128, 96, 3); 1264c18f1e26SAlexander Motin csd->tran_speed = exp[e] * 10000 * mant[m]; 12653906d42dSAlexander Motin csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 12663906d42dSAlexander Motin csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 12673906d42dSAlexander Motin csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 12683906d42dSAlexander Motin csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 12693906d42dSAlexander Motin csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 12703906d42dSAlexander Motin csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 12713906d42dSAlexander Motin csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 12723906d42dSAlexander Motin csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 12733906d42dSAlexander Motin csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 12743906d42dSAlexander Motin csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 12753906d42dSAlexander Motin m = mmc_get_bits(raw_csd, 128, 62, 12); 12763906d42dSAlexander Motin e = mmc_get_bits(raw_csd, 128, 47, 3); 1277c18f1e26SAlexander Motin csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 12783906d42dSAlexander Motin csd->erase_blk_en = 0; 12793906d42dSAlexander Motin csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) * 12803906d42dSAlexander Motin (mmc_get_bits(raw_csd, 128, 37, 5) + 1); 12813906d42dSAlexander Motin csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5); 12823906d42dSAlexander Motin csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 12833906d42dSAlexander Motin csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 12843906d42dSAlexander Motin csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 12853906d42dSAlexander Motin csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 1286114b4164SWarner Losh } 1287114b4164SWarner Losh 1288c18f1e26SAlexander Motin static void 1289c18f1e26SAlexander Motin mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr) 1290c18f1e26SAlexander Motin { 1291c18f1e26SAlexander Motin unsigned int scr_struct; 1292c18f1e26SAlexander Motin 1293c18f1e26SAlexander Motin memset(scr, 0, sizeof(*scr)); 1294c18f1e26SAlexander Motin 12953906d42dSAlexander Motin scr_struct = mmc_get_bits(raw_scr, 64, 60, 4); 1296c18f1e26SAlexander Motin if (scr_struct != 0) { 1297c18f1e26SAlexander Motin printf("Unrecognised SCR structure version %d\n", 1298c18f1e26SAlexander Motin scr_struct); 1299c18f1e26SAlexander Motin return; 1300c18f1e26SAlexander Motin } 13013906d42dSAlexander Motin scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4); 13023906d42dSAlexander Motin scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4); 13033906d42dSAlexander Motin } 13043906d42dSAlexander Motin 13053906d42dSAlexander Motin static void 13063906d42dSAlexander Motin mmc_app_decode_sd_status(uint32_t *raw_sd_status, 13073906d42dSAlexander Motin struct mmc_sd_status *sd_status) 13083906d42dSAlexander Motin { 13093906d42dSAlexander Motin 13103906d42dSAlexander Motin memset(sd_status, 0, sizeof(*sd_status)); 13113906d42dSAlexander Motin 13123906d42dSAlexander Motin sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2); 13133906d42dSAlexander Motin sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1); 13143906d42dSAlexander Motin sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16); 13153906d42dSAlexander Motin sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12); 13163906d42dSAlexander Motin sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8); 13173906d42dSAlexander Motin sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8); 13183906d42dSAlexander Motin sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4); 13193906d42dSAlexander Motin sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16); 13203906d42dSAlexander Motin sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6); 13213906d42dSAlexander Motin sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2); 1322c18f1e26SAlexander Motin } 1323c18f1e26SAlexander Motin 1324114b4164SWarner Losh static int 1325114b4164SWarner Losh mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid) 1326114b4164SWarner Losh { 1327114b4164SWarner Losh struct mmc_command cmd; 1328114b4164SWarner Losh int err; 1329114b4164SWarner Losh 1330ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 1331114b4164SWarner Losh cmd.opcode = MMC_ALL_SEND_CID; 1332114b4164SWarner Losh cmd.arg = 0; 1333114b4164SWarner Losh cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 13344e0efe35SWarner Losh cmd.data = NULL; 133572dec079SMarius Strobl err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 1336114b4164SWarner Losh memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 1337114b4164SWarner Losh return (err); 1338114b4164SWarner Losh } 1339114b4164SWarner Losh 1340114b4164SWarner Losh static int 13410554fd51SMarius Strobl mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd) 1342114b4164SWarner Losh { 1343114b4164SWarner Losh struct mmc_command cmd; 1344114b4164SWarner Losh int err; 1345114b4164SWarner Losh 1346ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 1347114b4164SWarner Losh cmd.opcode = MMC_SEND_CSD; 1348114b4164SWarner Losh cmd.arg = rca << 16; 1349114b4164SWarner Losh cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 13504e0efe35SWarner Losh cmd.data = NULL; 135172dec079SMarius Strobl err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 13520554fd51SMarius Strobl memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t)); 1353114b4164SWarner Losh return (err); 1354114b4164SWarner Losh } 1355114b4164SWarner Losh 1356114b4164SWarner Losh static int 1357c18f1e26SAlexander Motin mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr) 1358c18f1e26SAlexander Motin { 1359c18f1e26SAlexander Motin int err; 1360c18f1e26SAlexander Motin struct mmc_command cmd; 1361c18f1e26SAlexander Motin struct mmc_data data; 1362c18f1e26SAlexander Motin 1363ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 1364ed7142a7SIan Lepore memset(&data, 0, sizeof(data)); 1365c18f1e26SAlexander Motin 1366c18f1e26SAlexander Motin memset(rawscr, 0, 8); 1367c18f1e26SAlexander Motin cmd.opcode = ACMD_SEND_SCR; 1368c18f1e26SAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1369c18f1e26SAlexander Motin cmd.arg = 0; 1370c18f1e26SAlexander Motin cmd.data = &data; 1371c18f1e26SAlexander Motin 1372c18f1e26SAlexander Motin data.data = rawscr; 1373c18f1e26SAlexander Motin data.len = 8; 1374c18f1e26SAlexander Motin data.flags = MMC_DATA_READ; 1375c18f1e26SAlexander Motin 137672dec079SMarius Strobl err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES); 1377c18f1e26SAlexander Motin rawscr[0] = be32toh(rawscr[0]); 1378c18f1e26SAlexander Motin rawscr[1] = be32toh(rawscr[1]); 1379c18f1e26SAlexander Motin return (err); 1380c18f1e26SAlexander Motin } 1381c18f1e26SAlexander Motin 1382c18f1e26SAlexander Motin static int 13833906d42dSAlexander Motin mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus) 13843906d42dSAlexander Motin { 13853906d42dSAlexander Motin struct mmc_command cmd; 13863906d42dSAlexander Motin struct mmc_data data; 13877e6ccea3SMarius Strobl int err, i; 13883906d42dSAlexander Motin 1389ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 1390ed7142a7SIan Lepore memset(&data, 0, sizeof(data)); 13913906d42dSAlexander Motin 13923906d42dSAlexander Motin memset(rawsdstatus, 0, 64); 13933906d42dSAlexander Motin cmd.opcode = ACMD_SD_STATUS; 13943906d42dSAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 13953906d42dSAlexander Motin cmd.arg = 0; 13963906d42dSAlexander Motin cmd.data = &data; 13973906d42dSAlexander Motin 13983906d42dSAlexander Motin data.data = rawsdstatus; 13993906d42dSAlexander Motin data.len = 64; 14003906d42dSAlexander Motin data.flags = MMC_DATA_READ; 14013906d42dSAlexander Motin 140272dec079SMarius Strobl err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES); 14033906d42dSAlexander Motin for (i = 0; i < 16; i++) 14043906d42dSAlexander Motin rawsdstatus[i] = be32toh(rawsdstatus[i]); 14053906d42dSAlexander Motin return (err); 14063906d42dSAlexander Motin } 14073906d42dSAlexander Motin 14083906d42dSAlexander Motin static int 1409c18f1e26SAlexander Motin mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp) 1410c18f1e26SAlexander Motin { 1411c18f1e26SAlexander Motin struct mmc_command cmd; 1412c18f1e26SAlexander Motin int err; 1413c18f1e26SAlexander Motin 1414ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 1415c18f1e26SAlexander Motin cmd.opcode = MMC_SET_RELATIVE_ADDR; 1416c18f1e26SAlexander Motin cmd.arg = resp << 16; 1417c18f1e26SAlexander Motin cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 1418c18f1e26SAlexander Motin cmd.data = NULL; 141972dec079SMarius Strobl err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 1420c18f1e26SAlexander Motin return (err); 1421c18f1e26SAlexander Motin } 1422c18f1e26SAlexander Motin 1423c18f1e26SAlexander Motin static int 1424114b4164SWarner Losh mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp) 1425114b4164SWarner Losh { 1426114b4164SWarner Losh struct mmc_command cmd; 1427114b4164SWarner Losh int err; 1428114b4164SWarner Losh 1429ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 1430114b4164SWarner Losh cmd.opcode = SD_SEND_RELATIVE_ADDR; 1431114b4164SWarner Losh cmd.arg = 0; 1432114b4164SWarner Losh cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 14334e0efe35SWarner Losh cmd.data = NULL; 143472dec079SMarius Strobl err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 1435114b4164SWarner Losh *resp = cmd.resp[0]; 1436114b4164SWarner Losh return (err); 1437114b4164SWarner Losh } 1438114b4164SWarner Losh 14390554fd51SMarius Strobl static int 14400554fd51SMarius Strobl mmc_set_blocklen(struct mmc_softc *sc, uint32_t len) 14410554fd51SMarius Strobl { 14420554fd51SMarius Strobl struct mmc_command cmd; 14430554fd51SMarius Strobl int err; 14440554fd51SMarius Strobl 1445ed7142a7SIan Lepore memset(&cmd, 0, sizeof(cmd)); 14460554fd51SMarius Strobl cmd.opcode = MMC_SET_BLOCKLEN; 14470554fd51SMarius Strobl cmd.arg = len; 14480554fd51SMarius Strobl cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 14490554fd51SMarius Strobl cmd.data = NULL; 145072dec079SMarius Strobl err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES); 14510554fd51SMarius Strobl return (err); 14520554fd51SMarius Strobl } 14530554fd51SMarius Strobl 14540f34084fSMarius Strobl static uint32_t 14550f34084fSMarius Strobl mmc_timing_to_dtr(struct mmc_ivars *ivar, enum mmc_bus_timing timing) 14560f34084fSMarius Strobl { 14570f34084fSMarius Strobl 14580f34084fSMarius Strobl switch (timing) { 14590f34084fSMarius Strobl case bus_timing_normal: 14600f34084fSMarius Strobl return (ivar->tran_speed); 14610f34084fSMarius Strobl case bus_timing_hs: 14620f34084fSMarius Strobl return (ivar->hs_tran_speed); 14630f34084fSMarius Strobl case bus_timing_uhs_sdr12: 14640f34084fSMarius Strobl return (SD_SDR12_MAX); 14650f34084fSMarius Strobl case bus_timing_uhs_sdr25: 14660f34084fSMarius Strobl return (SD_SDR25_MAX); 14670f34084fSMarius Strobl case bus_timing_uhs_ddr50: 14680f34084fSMarius Strobl return (SD_DDR50_MAX); 14690f34084fSMarius Strobl case bus_timing_uhs_sdr50: 14700f34084fSMarius Strobl return (SD_SDR50_MAX); 14710f34084fSMarius Strobl case bus_timing_uhs_sdr104: 14720f34084fSMarius Strobl return (SD_SDR104_MAX); 14730f34084fSMarius Strobl case bus_timing_mmc_ddr52: 14740f34084fSMarius Strobl return (MMC_TYPE_DDR52_MAX); 14750f34084fSMarius Strobl case bus_timing_mmc_hs200: 14760f34084fSMarius Strobl case bus_timing_mmc_hs400: 14770f34084fSMarius Strobl case bus_timing_mmc_hs400es: 14780f34084fSMarius Strobl return (MMC_TYPE_HS200_HS400ES_MAX); 14790f34084fSMarius Strobl } 14800f34084fSMarius Strobl return (0); 14810f34084fSMarius Strobl } 14820f34084fSMarius Strobl 14830f34084fSMarius Strobl static const char * 14840f34084fSMarius Strobl mmc_timing_to_string(enum mmc_bus_timing timing) 14850f34084fSMarius Strobl { 14860f34084fSMarius Strobl 14870f34084fSMarius Strobl switch (timing) { 14880f34084fSMarius Strobl case bus_timing_normal: 14890f34084fSMarius Strobl return ("normal speed"); 14900f34084fSMarius Strobl case bus_timing_hs: 14910f34084fSMarius Strobl return ("high speed"); 14920f34084fSMarius Strobl case bus_timing_uhs_sdr12: 14930f34084fSMarius Strobl case bus_timing_uhs_sdr25: 14940f34084fSMarius Strobl case bus_timing_uhs_sdr50: 14950f34084fSMarius Strobl case bus_timing_uhs_sdr104: 14960f34084fSMarius Strobl return ("single data rate"); 14970f34084fSMarius Strobl case bus_timing_uhs_ddr50: 14980f34084fSMarius Strobl case bus_timing_mmc_ddr52: 14990f34084fSMarius Strobl return ("dual data rate"); 15000f34084fSMarius Strobl case bus_timing_mmc_hs200: 15010f34084fSMarius Strobl return ("HS200"); 15020f34084fSMarius Strobl case bus_timing_mmc_hs400: 15030f34084fSMarius Strobl return ("HS400"); 15040f34084fSMarius Strobl case bus_timing_mmc_hs400es: 15050f34084fSMarius Strobl return ("HS400 with enhanced strobe"); 15060f34084fSMarius Strobl } 15070f34084fSMarius Strobl return (""); 15080f34084fSMarius Strobl } 15090f34084fSMarius Strobl 1510aca38eabSMarius Strobl static bool 1511aca38eabSMarius Strobl mmc_host_timing(device_t dev, enum mmc_bus_timing timing) 1512aca38eabSMarius Strobl { 1513aca38eabSMarius Strobl int host_caps; 1514aca38eabSMarius Strobl 1515aca38eabSMarius Strobl host_caps = mmcbr_get_caps(dev); 1516aca38eabSMarius Strobl 1517aca38eabSMarius Strobl #define HOST_TIMING_CAP(host_caps, cap) ({ \ 1518aca38eabSMarius Strobl bool retval; \ 1519aca38eabSMarius Strobl if (((host_caps) & (cap)) == (cap)) \ 1520aca38eabSMarius Strobl retval = true; \ 1521aca38eabSMarius Strobl else \ 1522aca38eabSMarius Strobl retval = false; \ 1523aca38eabSMarius Strobl retval; \ 1524aca38eabSMarius Strobl }) 1525aca38eabSMarius Strobl 1526aca38eabSMarius Strobl switch (timing) { 1527aca38eabSMarius Strobl case bus_timing_normal: 1528aca38eabSMarius Strobl return (true); 1529aca38eabSMarius Strobl case bus_timing_hs: 1530aca38eabSMarius Strobl return (HOST_TIMING_CAP(host_caps, MMC_CAP_HSPEED)); 1531aca38eabSMarius Strobl case bus_timing_uhs_sdr12: 1532aca38eabSMarius Strobl return (HOST_TIMING_CAP(host_caps, MMC_CAP_UHS_SDR12)); 1533aca38eabSMarius Strobl case bus_timing_uhs_sdr25: 1534aca38eabSMarius Strobl return (HOST_TIMING_CAP(host_caps, MMC_CAP_UHS_SDR25)); 1535aca38eabSMarius Strobl case bus_timing_uhs_ddr50: 1536aca38eabSMarius Strobl return (HOST_TIMING_CAP(host_caps, MMC_CAP_UHS_DDR50)); 1537aca38eabSMarius Strobl case bus_timing_uhs_sdr50: 1538aca38eabSMarius Strobl return (HOST_TIMING_CAP(host_caps, MMC_CAP_UHS_SDR50)); 1539aca38eabSMarius Strobl case bus_timing_uhs_sdr104: 1540aca38eabSMarius Strobl return (HOST_TIMING_CAP(host_caps, MMC_CAP_UHS_SDR104)); 1541aca38eabSMarius Strobl case bus_timing_mmc_ddr52: 1542aca38eabSMarius Strobl return (HOST_TIMING_CAP(host_caps, MMC_CAP_MMC_DDR52)); 1543aca38eabSMarius Strobl case bus_timing_mmc_hs200: 1544aca38eabSMarius Strobl return (HOST_TIMING_CAP(host_caps, MMC_CAP_MMC_HS200)); 1545aca38eabSMarius Strobl case bus_timing_mmc_hs400: 1546aca38eabSMarius Strobl return (HOST_TIMING_CAP(host_caps, MMC_CAP_MMC_HS400)); 1547aca38eabSMarius Strobl case bus_timing_mmc_hs400es: 1548aca38eabSMarius Strobl return (HOST_TIMING_CAP(host_caps, MMC_CAP_MMC_HS400 | 1549aca38eabSMarius Strobl MMC_CAP_MMC_ENH_STROBE)); 1550aca38eabSMarius Strobl } 1551aca38eabSMarius Strobl 1552aca38eabSMarius Strobl #undef HOST_TIMING_CAP 1553aca38eabSMarius Strobl 1554aca38eabSMarius Strobl return (false); 1555aca38eabSMarius Strobl } 1556aca38eabSMarius Strobl 1557114b4164SWarner Losh static void 1558ed01be88SAlexander Motin mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard) 1559ed01be88SAlexander Motin { 15600f34084fSMarius Strobl enum mmc_bus_timing max_timing, timing; 156172dec079SMarius Strobl 156265f63c73SIan Lepore device_printf(dev, "Card at relative address 0x%04x%s:\n", 1563ed01be88SAlexander Motin ivar->rca, newcard ? " added" : ""); 15647aa65846SMarius Strobl device_printf(dev, " card: %s\n", ivar->card_id_string); 15650f34084fSMarius Strobl max_timing = bus_timing_normal; 15660f34084fSMarius Strobl for (timing = bus_timing_max; timing > bus_timing_normal; timing--) { 15670f34084fSMarius Strobl if (isset(&ivar->timings, timing)) { 15680f34084fSMarius Strobl max_timing = timing; 15690f34084fSMarius Strobl break; 15700f34084fSMarius Strobl } 15710f34084fSMarius Strobl } 1572*79f39c6aSMarius Strobl device_printf(dev, " quirks: %b\n", ivar->quirks, MMC_QUIRKS_FMT); 15730f34084fSMarius Strobl device_printf(dev, " bus: %ubit, %uMHz (%s timing)\n", 1574ed01be88SAlexander Motin (ivar->bus_width == bus_width_1 ? 1 : 1575ed01be88SAlexander Motin (ivar->bus_width == bus_width_4 ? 4 : 8)), 15760f34084fSMarius Strobl mmc_timing_to_dtr(ivar, timing) / 1000000, 15770f34084fSMarius Strobl mmc_timing_to_string(timing)); 1578ed01be88SAlexander Motin device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n", 1579ed01be88SAlexander Motin ivar->sec_count, ivar->erase_sector, 1580ed01be88SAlexander Motin ivar->read_only ? ", read-only" : ""); 1581ed01be88SAlexander Motin } 1582ed01be88SAlexander Motin 1583ed01be88SAlexander Motin static void 1584114b4164SWarner Losh mmc_discover_cards(struct mmc_softc *sc) 1585114b4164SWarner Losh { 1586b440e965SMarius Strobl u_char switch_res[64]; 1587b440e965SMarius Strobl uint32_t raw_cid[4]; 1588eb67f31aSAlexander Motin struct mmc_ivars *ivar = NULL; 1589*79f39c6aSMarius Strobl const struct mmc_quirk *quirk; 1590114b4164SWarner Losh device_t child; 1591aca38eabSMarius Strobl int err, host_caps, i, newcard; 1592b440e965SMarius Strobl uint32_t resp, sec_count, status; 1593c18f1e26SAlexander Motin uint16_t rca = 2; 1594114b4164SWarner Losh 15950f34084fSMarius Strobl host_caps = mmcbr_get_caps(sc->dev); 1596ed01be88SAlexander Motin if (bootverbose || mmc_debug) 1597ed01be88SAlexander Motin device_printf(sc->dev, "Probing cards\n"); 1598114b4164SWarner Losh while (1) { 1599aca38eabSMarius Strobl child = NULL; 1600dc47198fSIan Lepore sc->squelched++; /* Errors are expected, squelch reporting. */ 1601eb67f31aSAlexander Motin err = mmc_all_send_cid(sc, raw_cid); 1602dc47198fSIan Lepore sc->squelched--; 1603114b4164SWarner Losh if (err == MMC_ERR_TIMEOUT) 1604114b4164SWarner Losh break; 1605114b4164SWarner Losh if (err != MMC_ERR_NONE) { 1606dece39abSWarner Losh device_printf(sc->dev, "Error reading CID %d\n", err); 1607114b4164SWarner Losh break; 1608114b4164SWarner Losh } 1609eb67f31aSAlexander Motin newcard = 1; 1610aca38eabSMarius Strobl for (i = 0; i < sc->child_count; i++) { 1611aca38eabSMarius Strobl ivar = device_get_ivars(sc->child_list[i]); 16121bacf3beSMarius Strobl if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 16131bacf3beSMarius Strobl 0) { 1614eb67f31aSAlexander Motin newcard = 0; 1615eb67f31aSAlexander Motin break; 1616eb67f31aSAlexander Motin } 1617eb67f31aSAlexander Motin } 1618ed01be88SAlexander Motin if (bootverbose || mmc_debug) { 16191bacf3beSMarius Strobl device_printf(sc->dev, 16201bacf3beSMarius Strobl "%sard detected (CID %08x%08x%08x%08x)\n", 1621ed01be88SAlexander Motin newcard ? "New c" : "C", 1622ed01be88SAlexander Motin raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]); 1623ed01be88SAlexander Motin } 1624eb67f31aSAlexander Motin if (newcard) { 1625eb67f31aSAlexander Motin ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF, 1626eb67f31aSAlexander Motin M_WAITOK | M_ZERO); 1627eb67f31aSAlexander Motin memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid)); 1628eb67f31aSAlexander Motin } 1629c18f1e26SAlexander Motin if (mmcbr_get_ro(sc->dev)) 1630c18f1e26SAlexander Motin ivar->read_only = 1; 1631c18f1e26SAlexander Motin ivar->bus_width = bus_width_1; 16320f34084fSMarius Strobl setbit(&ivar->timings, bus_timing_normal); 1633c18f1e26SAlexander Motin ivar->mode = mmcbr_get_mode(sc->dev); 1634c18f1e26SAlexander Motin if (ivar->mode == mode_sd) { 16354871891eSWarner Losh mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid); 16360f34084fSMarius Strobl err = mmc_send_relative_addr(sc, &resp); 16370f34084fSMarius Strobl if (err != MMC_ERR_NONE) { 16380f34084fSMarius Strobl device_printf(sc->dev, 16390f34084fSMarius Strobl "Error getting RCA %d\n", err); 1640aca38eabSMarius Strobl goto free_ivar; 16410f34084fSMarius Strobl } 1642114b4164SWarner Losh ivar->rca = resp >> 16; 1643c18f1e26SAlexander Motin /* Get card CSD. */ 16440f34084fSMarius Strobl err = mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 16450f34084fSMarius Strobl if (err != MMC_ERR_NONE) { 16460f34084fSMarius Strobl device_printf(sc->dev, 16470f34084fSMarius Strobl "Error getting CSD %d\n", err); 1648aca38eabSMarius Strobl goto free_ivar; 16490f34084fSMarius Strobl } 16500554fd51SMarius Strobl if (bootverbose || mmc_debug) 16510554fd51SMarius Strobl device_printf(sc->dev, 16520554fd51SMarius Strobl "%sard detected (CSD %08x%08x%08x%08x)\n", 16530554fd51SMarius Strobl newcard ? "New c" : "C", ivar->raw_csd[0], 16540554fd51SMarius Strobl ivar->raw_csd[1], ivar->raw_csd[2], 16550554fd51SMarius Strobl ivar->raw_csd[3]); 1656aca38eabSMarius Strobl err = mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd); 1657aca38eabSMarius Strobl if (err != MMC_ERR_NONE) { 1658aca38eabSMarius Strobl device_printf(sc->dev, "Error decoding CSD\n"); 1659aca38eabSMarius Strobl goto free_ivar; 1660aca38eabSMarius Strobl } 1661fa5f41d3SAlexander Motin ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE; 1662c18f1e26SAlexander Motin if (ivar->csd.csd_structure > 0) 1663c18f1e26SAlexander Motin ivar->high_cap = 1; 1664c18f1e26SAlexander Motin ivar->tran_speed = ivar->csd.tran_speed; 16653906d42dSAlexander Motin ivar->erase_sector = ivar->csd.erase_sector * 16663906d42dSAlexander Motin ivar->csd.write_bl_len / MMC_SECTOR_SIZE; 16670554fd51SMarius Strobl 166872dec079SMarius Strobl err = mmc_send_status(sc->dev, sc->dev, ivar->rca, 166972dec079SMarius Strobl &status); 16700554fd51SMarius Strobl if (err != MMC_ERR_NONE) { 16710554fd51SMarius Strobl device_printf(sc->dev, 16720554fd51SMarius Strobl "Error reading card status %d\n", err); 1673aca38eabSMarius Strobl goto free_ivar; 16740554fd51SMarius Strobl } 16750554fd51SMarius Strobl if ((status & R1_CARD_IS_LOCKED) != 0) { 16760554fd51SMarius Strobl device_printf(sc->dev, 1677aca38eabSMarius Strobl "Card is password protected, skipping\n"); 1678aca38eabSMarius Strobl goto free_ivar; 16790554fd51SMarius Strobl } 16800554fd51SMarius Strobl 1681c18f1e26SAlexander Motin /* Get card SCR. Card must be selected to fetch it. */ 16820f34084fSMarius Strobl err = mmc_select_card(sc, ivar->rca); 16830f34084fSMarius Strobl if (err != MMC_ERR_NONE) { 16840f34084fSMarius Strobl device_printf(sc->dev, 16850f34084fSMarius Strobl "Error selecting card %d\n", err); 1686aca38eabSMarius Strobl goto free_ivar; 16870f34084fSMarius Strobl } 16880f34084fSMarius Strobl err = mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr); 16890f34084fSMarius Strobl if (err != MMC_ERR_NONE) { 16900f34084fSMarius Strobl device_printf(sc->dev, 16910f34084fSMarius Strobl "Error reading SCR %d\n", err); 1692aca38eabSMarius Strobl goto free_ivar; 16930f34084fSMarius Strobl } 1694c18f1e26SAlexander Motin mmc_app_decode_scr(ivar->raw_scr, &ivar->scr); 1695711873d4SWarner Losh /* Get card switch capabilities (command class 10). */ 1696c18f1e26SAlexander Motin if ((ivar->scr.sda_vsn >= 1) && 1697c18f1e26SAlexander Motin (ivar->csd.ccc & (1 << 10))) { 16980f34084fSMarius Strobl err = mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK, 1699711873d4SWarner Losh SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE, 1700711873d4SWarner Losh switch_res); 17010f34084fSMarius Strobl if (err == MMC_ERR_NONE && 17020f34084fSMarius Strobl switch_res[13] & (1 << SD_SWITCH_HS_MODE)) { 17030f34084fSMarius Strobl setbit(&ivar->timings, bus_timing_hs); 17040f34084fSMarius Strobl ivar->hs_tran_speed = SD_HS_MAX; 1705c18f1e26SAlexander Motin } 1706c18f1e26SAlexander Motin } 170725b2c4dfSWarner Losh 170825b2c4dfSWarner Losh /* 1709e7b25f91SIan Lepore * We deselect then reselect the card here. Some cards 1710e7b25f91SIan Lepore * become unselected and timeout with the above two 1711e7b25f91SIan Lepore * commands, although the state tables / diagrams in the 1712e7b25f91SIan Lepore * standard suggest they go back to the transfer state. 1713e7b25f91SIan Lepore * Other cards don't become deselected, and if we 17147e6ccea3SMarius Strobl * attempt to blindly re-select them, we get timeout 1715e7b25f91SIan Lepore * errors from some controllers. So we deselect then 1716e7b25f91SIan Lepore * reselect to handle all situations. The only thing we 1717e7b25f91SIan Lepore * use from the sd_status is the erase sector size, but 1718e7b25f91SIan Lepore * it is still nice to get that right. 171925b2c4dfSWarner Losh */ 1720aca38eabSMarius Strobl (void)mmc_select_card(sc, 0); 17210f34084fSMarius Strobl (void)mmc_select_card(sc, ivar->rca); 17220f34084fSMarius Strobl (void)mmc_app_sd_status(sc, ivar->rca, 17230f34084fSMarius Strobl ivar->raw_sd_status); 17243906d42dSAlexander Motin mmc_app_decode_sd_status(ivar->raw_sd_status, 17253906d42dSAlexander Motin &ivar->sd_status); 17263906d42dSAlexander Motin if (ivar->sd_status.au_size != 0) { 17273906d42dSAlexander Motin ivar->erase_sector = 17283906d42dSAlexander Motin 16 << ivar->sd_status.au_size; 17293906d42dSAlexander Motin } 1730aca38eabSMarius Strobl /* Find maximum supported bus width. */ 17310f34084fSMarius Strobl if ((host_caps & MMC_CAP_4_BIT_DATA) && 1732c18f1e26SAlexander Motin (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) 1733c18f1e26SAlexander Motin ivar->bus_width = bus_width_4; 17340554fd51SMarius Strobl 1735aca38eabSMarius Strobl goto child_common; 1736114b4164SWarner Losh } 1737c18f1e26SAlexander Motin ivar->rca = rca++; 17380f34084fSMarius Strobl err = mmc_set_relative_addr(sc, ivar->rca); 17390f34084fSMarius Strobl if (err != MMC_ERR_NONE) { 17400f34084fSMarius Strobl device_printf(sc->dev, "Error setting RCA %d\n", err); 1741aca38eabSMarius Strobl goto free_ivar; 17420f34084fSMarius Strobl } 1743c18f1e26SAlexander Motin /* Get card CSD. */ 17440f34084fSMarius Strobl err = mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 17450f34084fSMarius Strobl if (err != MMC_ERR_NONE) { 17460f34084fSMarius Strobl device_printf(sc->dev, "Error getting CSD %d\n", err); 1747aca38eabSMarius Strobl goto free_ivar; 17480f34084fSMarius Strobl } 17490554fd51SMarius Strobl if (bootverbose || mmc_debug) 17500554fd51SMarius Strobl device_printf(sc->dev, 17510554fd51SMarius Strobl "%sard detected (CSD %08x%08x%08x%08x)\n", 17520554fd51SMarius Strobl newcard ? "New c" : "C", ivar->raw_csd[0], 17530554fd51SMarius Strobl ivar->raw_csd[1], ivar->raw_csd[2], 17540554fd51SMarius Strobl ivar->raw_csd[3]); 17550554fd51SMarius Strobl 17564871891eSWarner Losh mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd); 1757fa5f41d3SAlexander Motin ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE; 1758c18f1e26SAlexander Motin ivar->tran_speed = ivar->csd.tran_speed; 17593906d42dSAlexander Motin ivar->erase_sector = ivar->csd.erase_sector * 17603906d42dSAlexander Motin ivar->csd.write_bl_len / MMC_SECTOR_SIZE; 17610554fd51SMarius Strobl 176272dec079SMarius Strobl err = mmc_send_status(sc->dev, sc->dev, ivar->rca, &status); 17630554fd51SMarius Strobl if (err != MMC_ERR_NONE) { 17640554fd51SMarius Strobl device_printf(sc->dev, 17650554fd51SMarius Strobl "Error reading card status %d\n", err); 1766aca38eabSMarius Strobl goto free_ivar; 17670554fd51SMarius Strobl } 17680554fd51SMarius Strobl if ((status & R1_CARD_IS_LOCKED) != 0) { 17690554fd51SMarius Strobl device_printf(sc->dev, 1770aca38eabSMarius Strobl "Card is password protected, skipping\n"); 1771aca38eabSMarius Strobl goto free_ivar; 17720554fd51SMarius Strobl } 17730554fd51SMarius Strobl 17740f34084fSMarius Strobl err = mmc_select_card(sc, ivar->rca); 17750f34084fSMarius Strobl if (err != MMC_ERR_NONE) { 17760f34084fSMarius Strobl device_printf(sc->dev, "Error selecting card %d\n", 17770f34084fSMarius Strobl err); 1778aca38eabSMarius Strobl goto free_ivar; 17790f34084fSMarius Strobl } 17803e834dd3SWarner Losh 178172dec079SMarius Strobl /* Only MMC >= 4.x devices support EXT_CSD. */ 1782c18f1e26SAlexander Motin if (ivar->csd.spec_vers >= 4) { 178372dec079SMarius Strobl err = mmc_send_ext_csd(sc->dev, sc->dev, 178472dec079SMarius Strobl ivar->raw_ext_csd); 178572dec079SMarius Strobl if (err != MMC_ERR_NONE) { 178672dec079SMarius Strobl device_printf(sc->dev, 178772dec079SMarius Strobl "Error reading EXT_CSD %d\n", err); 1788aca38eabSMarius Strobl goto free_ivar; 178972dec079SMarius Strobl } 1790fa5f41d3SAlexander Motin /* Handle extended capacity from EXT_CSD */ 1791fa5f41d3SAlexander Motin sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] + 1792fa5f41d3SAlexander Motin (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) + 1793fa5f41d3SAlexander Motin (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) + 1794fa5f41d3SAlexander Motin (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24); 1795fa5f41d3SAlexander Motin if (sec_count != 0) { 1796fa5f41d3SAlexander Motin ivar->sec_count = sec_count; 1797fa5f41d3SAlexander Motin ivar->high_cap = 1; 1798fa5f41d3SAlexander Motin } 1799aca38eabSMarius Strobl /* Find maximum supported bus width. */ 1800aca38eabSMarius Strobl ivar->bus_width = mmc_test_bus_width(sc); 18010f34084fSMarius Strobl /* Get device speeds beyond normal mode. */ 18020f34084fSMarius Strobl if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 18030f34084fSMarius Strobl EXT_CSD_CARD_TYPE_HS_52) != 0) { 18040f34084fSMarius Strobl setbit(&ivar->timings, bus_timing_hs); 18050f34084fSMarius Strobl ivar->hs_tran_speed = MMC_TYPE_HS_52_MAX; 18060f34084fSMarius Strobl } else if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 18070f34084fSMarius Strobl EXT_CSD_CARD_TYPE_HS_26) != 0) { 18080f34084fSMarius Strobl setbit(&ivar->timings, bus_timing_hs); 18090f34084fSMarius Strobl ivar->hs_tran_speed = MMC_TYPE_HS_26_MAX; 18100f34084fSMarius Strobl } 18110f34084fSMarius Strobl if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 18120f34084fSMarius Strobl EXT_CSD_CARD_TYPE_DDR_52_1_2V) != 0 && 18130f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_120) != 0) { 18140f34084fSMarius Strobl setbit(&ivar->timings, bus_timing_mmc_ddr52); 18150f34084fSMarius Strobl setbit(&ivar->vccq_120, bus_timing_mmc_ddr52); 18160f34084fSMarius Strobl } 18170f34084fSMarius Strobl if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 18180f34084fSMarius Strobl EXT_CSD_CARD_TYPE_DDR_52_1_8V) != 0 && 18190f34084fSMarius Strobl (host_caps & MMC_CAP_SIGNALING_180) != 0) { 18200f34084fSMarius Strobl setbit(&ivar->timings, bus_timing_mmc_ddr52); 18210f34084fSMarius Strobl setbit(&ivar->vccq_180, bus_timing_mmc_ddr52); 18220f34084fSMarius Strobl } 1823aca38eabSMarius Strobl if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 1824aca38eabSMarius Strobl EXT_CSD_CARD_TYPE_HS200_1_2V) != 0 && 1825aca38eabSMarius Strobl (host_caps & MMC_CAP_SIGNALING_120) != 0) { 1826aca38eabSMarius Strobl setbit(&ivar->timings, bus_timing_mmc_hs200); 1827aca38eabSMarius Strobl setbit(&ivar->vccq_120, bus_timing_mmc_hs200); 1828aca38eabSMarius Strobl } 1829aca38eabSMarius Strobl if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 1830aca38eabSMarius Strobl EXT_CSD_CARD_TYPE_HS200_1_8V) != 0 && 1831aca38eabSMarius Strobl (host_caps & MMC_CAP_SIGNALING_180) != 0) { 1832aca38eabSMarius Strobl setbit(&ivar->timings, bus_timing_mmc_hs200); 1833aca38eabSMarius Strobl setbit(&ivar->vccq_180, bus_timing_mmc_hs200); 1834aca38eabSMarius Strobl } 1835aca38eabSMarius Strobl if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 1836aca38eabSMarius Strobl EXT_CSD_CARD_TYPE_HS400_1_2V) != 0 && 1837aca38eabSMarius Strobl (host_caps & MMC_CAP_SIGNALING_120) != 0 && 1838aca38eabSMarius Strobl ivar->bus_width == bus_width_8) { 1839aca38eabSMarius Strobl setbit(&ivar->timings, bus_timing_mmc_hs400); 1840aca38eabSMarius Strobl setbit(&ivar->vccq_120, bus_timing_mmc_hs400); 1841aca38eabSMarius Strobl } 1842aca38eabSMarius Strobl if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 1843aca38eabSMarius Strobl EXT_CSD_CARD_TYPE_HS400_1_8V) != 0 && 1844aca38eabSMarius Strobl (host_caps & MMC_CAP_SIGNALING_180) != 0 && 1845aca38eabSMarius Strobl ivar->bus_width == bus_width_8) { 1846aca38eabSMarius Strobl setbit(&ivar->timings, bus_timing_mmc_hs400); 1847aca38eabSMarius Strobl setbit(&ivar->vccq_180, bus_timing_mmc_hs400); 1848aca38eabSMarius Strobl } 1849aca38eabSMarius Strobl if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 1850aca38eabSMarius Strobl EXT_CSD_CARD_TYPE_HS400_1_2V) != 0 && 1851aca38eabSMarius Strobl (ivar->raw_ext_csd[EXT_CSD_STROBE_SUPPORT] & 1852aca38eabSMarius Strobl EXT_CSD_STROBE_SUPPORT_EN) != 0 && 1853aca38eabSMarius Strobl (host_caps & MMC_CAP_SIGNALING_120) != 0 && 1854aca38eabSMarius Strobl ivar->bus_width == bus_width_8) { 1855aca38eabSMarius Strobl setbit(&ivar->timings, bus_timing_mmc_hs400es); 1856aca38eabSMarius Strobl setbit(&ivar->vccq_120, bus_timing_mmc_hs400es); 1857aca38eabSMarius Strobl } 1858aca38eabSMarius Strobl if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] & 1859aca38eabSMarius Strobl EXT_CSD_CARD_TYPE_HS400_1_8V) != 0 && 1860aca38eabSMarius Strobl (ivar->raw_ext_csd[EXT_CSD_STROBE_SUPPORT] & 1861aca38eabSMarius Strobl EXT_CSD_STROBE_SUPPORT_EN) != 0 && 1862aca38eabSMarius Strobl (host_caps & MMC_CAP_SIGNALING_180) != 0 && 1863aca38eabSMarius Strobl ivar->bus_width == bus_width_8) { 1864aca38eabSMarius Strobl setbit(&ivar->timings, bus_timing_mmc_hs400es); 1865aca38eabSMarius Strobl setbit(&ivar->vccq_180, bus_timing_mmc_hs400es); 1866aca38eabSMarius Strobl } 186772dec079SMarius Strobl /* 186872dec079SMarius Strobl * Determine generic switch timeout (provided in 186972dec079SMarius Strobl * units of 10 ms), defaulting to 500 ms. 187072dec079SMarius Strobl */ 187172dec079SMarius Strobl ivar->cmd6_time = 500 * 1000; 187272dec079SMarius Strobl if (ivar->csd.spec_vers >= 6) 187372dec079SMarius Strobl ivar->cmd6_time = 10 * 187472dec079SMarius Strobl ivar->raw_ext_csd[EXT_CSD_GEN_CMD6_TIME]; 18753906d42dSAlexander Motin /* Handle HC erase sector size. */ 18763906d42dSAlexander Motin if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) { 18773906d42dSAlexander Motin ivar->erase_sector = 1024 * 18783906d42dSAlexander Motin ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE]; 187972dec079SMarius Strobl err = mmc_switch(sc->dev, sc->dev, ivar->rca, 188072dec079SMarius Strobl EXT_CSD_CMD_SET_NORMAL, 188172dec079SMarius Strobl EXT_CSD_ERASE_GRP_DEF, 188272dec079SMarius Strobl EXT_CSD_ERASE_GRP_DEF_EN, 188372dec079SMarius Strobl ivar->cmd6_time, true); 188472dec079SMarius Strobl if (err != MMC_ERR_NONE) { 188572dec079SMarius Strobl device_printf(sc->dev, 188672dec079SMarius Strobl "Error setting erase group %d\n", 188772dec079SMarius Strobl err); 1888aca38eabSMarius Strobl goto free_ivar; 188972dec079SMarius Strobl } 18903906d42dSAlexander Motin } 1891c18f1e26SAlexander Motin } 18920554fd51SMarius Strobl 1893aca38eabSMarius Strobl mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid, 1894aca38eabSMarius Strobl ivar->raw_ext_csd[EXT_CSD_REV] >= 5); 1895aca38eabSMarius Strobl 1896aca38eabSMarius Strobl child_common: 1897*79f39c6aSMarius Strobl for (quirk = &mmc_quirks[0]; quirk->mid != 0x0; quirk++) { 1898*79f39c6aSMarius Strobl if ((quirk->mid == MMC_QUIRK_MID_ANY || 1899*79f39c6aSMarius Strobl quirk->mid == ivar->cid.mid) && 1900*79f39c6aSMarius Strobl (quirk->oid == MMC_QUIRK_OID_ANY || 1901*79f39c6aSMarius Strobl quirk->oid == ivar->cid.oid) && 1902*79f39c6aSMarius Strobl strncmp(quirk->pnm, ivar->cid.pnm, 1903*79f39c6aSMarius Strobl sizeof(ivar->cid.pnm)) == 0) { 1904*79f39c6aSMarius Strobl ivar->quirks = quirk->quirks; 1905*79f39c6aSMarius Strobl break; 1906*79f39c6aSMarius Strobl } 1907*79f39c6aSMarius Strobl } 1908*79f39c6aSMarius Strobl 19090554fd51SMarius Strobl /* 19100554fd51SMarius Strobl * Some cards that report maximum I/O block sizes greater 19110554fd51SMarius Strobl * than 512 require the block length to be set to 512, even 19120554fd51SMarius Strobl * though that is supposed to be the default. Example: 19130554fd51SMarius Strobl * 19140554fd51SMarius Strobl * Transcend 2GB SDSC card, CID: 19150554fd51SMarius Strobl * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000 19160554fd51SMarius Strobl */ 19170554fd51SMarius Strobl if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE || 19180554fd51SMarius Strobl ivar->csd.write_bl_len != MMC_SECTOR_SIZE) 19190554fd51SMarius Strobl mmc_set_blocklen(sc, MMC_SECTOR_SIZE); 19200554fd51SMarius Strobl 19217aa65846SMarius Strobl mmc_format_card_id_string(ivar); 19220554fd51SMarius Strobl 1923ed01be88SAlexander Motin if (bootverbose || mmc_debug) 1924ed01be88SAlexander Motin mmc_log_card(sc->dev, ivar, newcard); 1925eb67f31aSAlexander Motin if (newcard) { 1926c18f1e26SAlexander Motin /* Add device. */ 1927c18f1e26SAlexander Motin child = device_add_child(sc->dev, NULL, -1); 1928aca38eabSMarius Strobl if (child != NULL) { 1929c18f1e26SAlexander Motin device_set_ivars(child, ivar); 1930aca38eabSMarius Strobl sc->child_list = realloc(sc->child_list, 19317a777659SMarius Strobl sizeof(device_t) * sc->child_count + 1, 1932aca38eabSMarius Strobl M_DEVBUF, M_WAITOK); 1933aca38eabSMarius Strobl sc->child_list[sc->child_count++] = child; 1934aca38eabSMarius Strobl } else 1935aca38eabSMarius Strobl device_printf(sc->dev, "Error adding child\n"); 1936114b4164SWarner Losh } 1937aca38eabSMarius Strobl 1938aca38eabSMarius Strobl free_ivar: 1939aca38eabSMarius Strobl if (newcard && child == NULL) 1940aca38eabSMarius Strobl free(ivar, M_DEVBUF); 1941aca38eabSMarius Strobl (void)mmc_select_card(sc, 0); 1942aca38eabSMarius Strobl /* 1943aca38eabSMarius Strobl * Not returning here when one MMC device could no be added 1944aca38eabSMarius Strobl * potentially would mean looping forever when that device 1945aca38eabSMarius Strobl * is broken (in which case it also may impact the remainder 1946aca38eabSMarius Strobl * of the bus anyway, though). 1947aca38eabSMarius Strobl */ 1948aca38eabSMarius Strobl if ((newcard && child == NULL) || 1949aca38eabSMarius Strobl mmcbr_get_mode(sc->dev) == mode_sd) 1950aca38eabSMarius Strobl return; 1951eb67f31aSAlexander Motin } 1952eb67f31aSAlexander Motin } 1953eb67f31aSAlexander Motin 1954eb67f31aSAlexander Motin static void 1955aca38eabSMarius Strobl mmc_update_child_list(struct mmc_softc *sc) 1956aca38eabSMarius Strobl { 1957aca38eabSMarius Strobl device_t child; 1958aca38eabSMarius Strobl int i, j; 1959aca38eabSMarius Strobl 1960aca38eabSMarius Strobl if (sc->child_count == 0) { 1961aca38eabSMarius Strobl free(sc->child_list, M_DEVBUF); 1962aca38eabSMarius Strobl return; 1963aca38eabSMarius Strobl } 1964aca38eabSMarius Strobl for (i = j = 0; i < sc->child_count; i++) { 1965aca38eabSMarius Strobl for (;;) { 1966aca38eabSMarius Strobl child = sc->child_list[j++]; 1967aca38eabSMarius Strobl if (child != NULL) 1968aca38eabSMarius Strobl break; 1969aca38eabSMarius Strobl } 1970aca38eabSMarius Strobl if (i != j) 1971aca38eabSMarius Strobl sc->child_list[i] = child; 1972aca38eabSMarius Strobl } 19737a777659SMarius Strobl sc->child_list = realloc(sc->child_list, sizeof(device_t) * 1974aca38eabSMarius Strobl sc->child_count, M_DEVBUF, M_WAITOK); 1975aca38eabSMarius Strobl } 1976aca38eabSMarius Strobl 1977aca38eabSMarius Strobl static void 1978eb67f31aSAlexander Motin mmc_rescan_cards(struct mmc_softc *sc) 1979eb67f31aSAlexander Motin { 19807e6ccea3SMarius Strobl struct mmc_ivars *ivar; 1981aca38eabSMarius Strobl int err, i, j; 1982eb67f31aSAlexander Motin 1983aca38eabSMarius Strobl for (i = j = 0; i < sc->child_count; i++) { 1984aca38eabSMarius Strobl ivar = device_get_ivars(sc->child_list[i]); 1985c11bbc7dSMarius Strobl if (mmc_select_card(sc, ivar->rca) != MMC_ERR_NONE) { 1986ed01be88SAlexander Motin if (bootverbose || mmc_debug) 19871bacf3beSMarius Strobl device_printf(sc->dev, 1988aca38eabSMarius Strobl "Card at relative address %d lost\n", 1989ed01be88SAlexander Motin ivar->rca); 1990aca38eabSMarius Strobl err = device_delete_child(sc->dev, sc->child_list[i]); 1991aca38eabSMarius Strobl if (err != 0) { 1992aca38eabSMarius Strobl j++; 1993aca38eabSMarius Strobl continue; 1994aca38eabSMarius Strobl } 19950e1466acSWarner Losh free(ivar, M_DEVBUF); 1996aca38eabSMarius Strobl } else 1997aca38eabSMarius Strobl j++; 1998114b4164SWarner Losh } 1999aca38eabSMarius Strobl if (sc->child_count == j) 2000aca38eabSMarius Strobl goto out; 2001aca38eabSMarius Strobl sc->child_count = j; 2002aca38eabSMarius Strobl mmc_update_child_list(sc); 2003aca38eabSMarius Strobl out: 2004aca38eabSMarius Strobl (void)mmc_select_card(sc, 0); 2005eb67f31aSAlexander Motin } 2006eb67f31aSAlexander Motin 2007eb67f31aSAlexander Motin static int 2008aca38eabSMarius Strobl mmc_delete_cards(struct mmc_softc *sc, bool final) 2009eb67f31aSAlexander Motin { 2010eb67f31aSAlexander Motin struct mmc_ivars *ivar; 2011aca38eabSMarius Strobl int err, i, j; 2012eb67f31aSAlexander Motin 2013aca38eabSMarius Strobl err = 0; 2014aca38eabSMarius Strobl for (i = j = 0; i < sc->child_count; i++) { 2015aca38eabSMarius Strobl ivar = device_get_ivars(sc->child_list[i]); 2016ed01be88SAlexander Motin if (bootverbose || mmc_debug) 20171bacf3beSMarius Strobl device_printf(sc->dev, 2018aca38eabSMarius Strobl "Card at relative address %d deleted\n", 2019ed01be88SAlexander Motin ivar->rca); 2020aca38eabSMarius Strobl err = device_delete_child(sc->dev, sc->child_list[i]); 2021aca38eabSMarius Strobl if (err != 0) { 2022aca38eabSMarius Strobl j++; 2023aca38eabSMarius Strobl if (final == false) 2024aca38eabSMarius Strobl continue; 2025aca38eabSMarius Strobl else 2026aca38eabSMarius Strobl break; 2027aca38eabSMarius Strobl } 2028eb67f31aSAlexander Motin free(ivar, M_DEVBUF); 2029eb67f31aSAlexander Motin } 2030aca38eabSMarius Strobl sc->child_count = j; 2031aca38eabSMarius Strobl mmc_update_child_list(sc); 2032aca38eabSMarius Strobl return (err); 2033eb67f31aSAlexander Motin } 2034114b4164SWarner Losh 2035114b4164SWarner Losh static void 2036114b4164SWarner Losh mmc_go_discovery(struct mmc_softc *sc) 2037114b4164SWarner Losh { 2038114b4164SWarner Losh uint32_t ocr; 2039114b4164SWarner Losh device_t dev; 2040c18f1e26SAlexander Motin int err; 2041114b4164SWarner Losh 2042114b4164SWarner Losh dev = sc->dev; 2043114b4164SWarner Losh if (mmcbr_get_power_mode(dev) != power_on) { 20446fdacf52SWarner Losh /* 20456fdacf52SWarner Losh * First, try SD modes 20466fdacf52SWarner Losh */ 2047dc47198fSIan Lepore sc->squelched++; /* Errors are expected, squelch reporting. */ 2048114b4164SWarner Losh mmcbr_set_mode(dev, mode_sd); 2049114b4164SWarner Losh mmc_power_up(sc); 2050114b4164SWarner Losh mmcbr_set_bus_mode(dev, pushpull); 2051ed01be88SAlexander Motin if (bootverbose || mmc_debug) 2052ed01be88SAlexander Motin device_printf(sc->dev, "Probing bus\n"); 2053114b4164SWarner Losh mmc_idle_cards(sc); 2054c18f1e26SAlexander Motin err = mmc_send_if_cond(sc, 1); 2055ed01be88SAlexander Motin if ((bootverbose || mmc_debug) && err == 0) 20561bacf3beSMarius Strobl device_printf(sc->dev, 20571bacf3beSMarius Strobl "SD 2.0 interface conditions: OK\n"); 20580554fd51SMarius Strobl if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 2059ed01be88SAlexander Motin if (bootverbose || mmc_debug) 2060ed01be88SAlexander Motin device_printf(sc->dev, "SD probe: failed\n"); 20616fdacf52SWarner Losh /* 20626fdacf52SWarner Losh * Failed, try MMC 20636fdacf52SWarner Losh */ 2064114b4164SWarner Losh mmcbr_set_mode(dev, mode_mmc); 2065ed01be88SAlexander Motin if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 2066ed01be88SAlexander Motin if (bootverbose || mmc_debug) 20671bacf3beSMarius Strobl device_printf(sc->dev, 20681bacf3beSMarius Strobl "MMC probe: failed\n"); 2069eb67f31aSAlexander Motin ocr = 0; /* Failed both, powerdown. */ 2070ed01be88SAlexander Motin } else if (bootverbose || mmc_debug) 2071ed01be88SAlexander Motin device_printf(sc->dev, 2072ed01be88SAlexander Motin "MMC probe: OK (OCR: 0x%08x)\n", ocr); 2073ed01be88SAlexander Motin } else if (bootverbose || mmc_debug) 20741bacf3beSMarius Strobl device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", 20751bacf3beSMarius Strobl ocr); 2076dc47198fSIan Lepore sc->squelched--; 2077ed01be88SAlexander Motin 2078114b4164SWarner Losh mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr)); 2079114b4164SWarner Losh if (mmcbr_get_ocr(dev) != 0) 2080114b4164SWarner Losh mmc_idle_cards(sc); 2081114b4164SWarner Losh } else { 2082114b4164SWarner Losh mmcbr_set_bus_mode(dev, opendrain); 20830f34084fSMarius Strobl mmcbr_set_clock(dev, SD_MMC_CARD_ID_FREQUENCY); 2084114b4164SWarner Losh mmcbr_update_ios(dev); 20856fdacf52SWarner Losh /* XXX recompute vdd based on new cards? */ 2086114b4164SWarner Losh } 2087114b4164SWarner Losh /* 2088114b4164SWarner Losh * Make sure that we have a mutually agreeable voltage to at least 2089114b4164SWarner Losh * one card on the bus. 2090114b4164SWarner Losh */ 2091ed01be88SAlexander Motin if (bootverbose || mmc_debug) 20921bacf3beSMarius Strobl device_printf(sc->dev, "Current OCR: 0x%08x\n", 20931bacf3beSMarius Strobl mmcbr_get_ocr(dev)); 2094eb67f31aSAlexander Motin if (mmcbr_get_ocr(dev) == 0) { 2095c4ec9d18SIan Lepore device_printf(sc->dev, "No compatible cards found on bus\n"); 2096aca38eabSMarius Strobl (void)mmc_delete_cards(sc, false); 2097eb67f31aSAlexander Motin mmc_power_down(sc); 2098114b4164SWarner Losh return; 2099eb67f31aSAlexander Motin } 2100114b4164SWarner Losh /* 2101114b4164SWarner Losh * Reselect the cards after we've idled them above. 2102114b4164SWarner Losh */ 2103c18f1e26SAlexander Motin if (mmcbr_get_mode(dev) == mode_sd) { 2104c18f1e26SAlexander Motin err = mmc_send_if_cond(sc, 1); 2105c18f1e26SAlexander Motin mmc_send_app_op_cond(sc, 2106c18f1e26SAlexander Motin (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL); 2107c18f1e26SAlexander Motin } else 210816c41306SIan Lepore mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL); 2109114b4164SWarner Losh mmc_discover_cards(sc); 2110eb67f31aSAlexander Motin mmc_rescan_cards(sc); 2111114b4164SWarner Losh 2112114b4164SWarner Losh mmcbr_set_bus_mode(dev, pushpull); 2113114b4164SWarner Losh mmcbr_update_ios(dev); 211467d752c3SAlexander Motin mmc_calculate_clock(sc); 2115114b4164SWarner Losh } 2116114b4164SWarner Losh 2117114b4164SWarner Losh static int 2118114b4164SWarner Losh mmc_calculate_clock(struct mmc_softc *sc) 2119114b4164SWarner Losh { 2120aca38eabSMarius Strobl device_t dev; 2121c18f1e26SAlexander Motin struct mmc_ivars *ivar; 2122aca38eabSMarius Strobl int i; 21230f34084fSMarius Strobl uint32_t dtr, max_dtr; 2124aca38eabSMarius Strobl uint16_t rca; 21250f34084fSMarius Strobl enum mmc_bus_timing max_timing, timing; 2126aca38eabSMarius Strobl bool changed, hs400; 2127114b4164SWarner Losh 2128aca38eabSMarius Strobl dev = sc->dev; 2129aca38eabSMarius Strobl max_dtr = mmcbr_get_f_max(dev); 2130aca38eabSMarius Strobl max_timing = bus_timing_max; 21310f34084fSMarius Strobl do { 21320f34084fSMarius Strobl changed = false; 2133aca38eabSMarius Strobl for (i = 0; i < sc->child_count; i++) { 2134aca38eabSMarius Strobl ivar = device_get_ivars(sc->child_list[i]); 2135aca38eabSMarius Strobl if (isclr(&ivar->timings, max_timing) || 2136aca38eabSMarius Strobl !mmc_host_timing(dev, max_timing)) { 2137aca38eabSMarius Strobl for (timing = max_timing - 1; timing >= 21380f34084fSMarius Strobl bus_timing_normal; timing--) { 2139aca38eabSMarius Strobl if (isset(&ivar->timings, timing) && 2140aca38eabSMarius Strobl mmc_host_timing(dev, timing)) { 21410f34084fSMarius Strobl max_timing = timing; 21420f34084fSMarius Strobl break; 2143c18f1e26SAlexander Motin } 21440f34084fSMarius Strobl } 21450f34084fSMarius Strobl changed = true; 21460f34084fSMarius Strobl } 21470f34084fSMarius Strobl dtr = mmc_timing_to_dtr(ivar, max_timing); 21480f34084fSMarius Strobl if (dtr < max_dtr) { 21490f34084fSMarius Strobl max_dtr = dtr; 21500f34084fSMarius Strobl changed = true; 21510f34084fSMarius Strobl } 21520f34084fSMarius Strobl } 21530f34084fSMarius Strobl } while (changed == true); 2154aca38eabSMarius Strobl 2155ed01be88SAlexander Motin if (bootverbose || mmc_debug) { 2156aca38eabSMarius Strobl device_printf(dev, 21570f34084fSMarius Strobl "setting transfer rate to %d.%03dMHz (%s timing)\n", 2158c18f1e26SAlexander Motin max_dtr / 1000000, (max_dtr / 1000) % 1000, 21590f34084fSMarius Strobl mmc_timing_to_string(max_timing)); 216067d752c3SAlexander Motin } 2161aca38eabSMarius Strobl 2162aca38eabSMarius Strobl /* 2163aca38eabSMarius Strobl * HS400 must be tuned in HS200 mode, so in case of HS400 we begin 2164aca38eabSMarius Strobl * with HS200 following the sequence as described in "6.6.2.2 HS200 2165aca38eabSMarius Strobl * timing mode selection" of the eMMC specification v5.1, too, and 2166aca38eabSMarius Strobl * switch to max_timing later. HS400ES requires no tuning and, thus, 2167aca38eabSMarius Strobl * can be switch to directly, but requires the same detour via high 2168aca38eabSMarius Strobl * speed mode as does HS400 (see mmc_switch_to_hs400()). 2169aca38eabSMarius Strobl */ 2170aca38eabSMarius Strobl hs400 = max_timing == bus_timing_mmc_hs400; 2171aca38eabSMarius Strobl timing = hs400 == true ? bus_timing_mmc_hs200 : max_timing; 2172aca38eabSMarius Strobl for (i = 0; i < sc->child_count; i++) { 2173aca38eabSMarius Strobl ivar = device_get_ivars(sc->child_list[i]); 21740f34084fSMarius Strobl if ((ivar->timings & ~(1 << bus_timing_normal)) == 0) 217572dec079SMarius Strobl continue; 2176aca38eabSMarius Strobl 2177aca38eabSMarius Strobl rca = ivar->rca; 2178aca38eabSMarius Strobl if (mmc_select_card(sc, rca) != MMC_ERR_NONE) { 2179aca38eabSMarius Strobl device_printf(dev, "Card at relative address %d " 2180aca38eabSMarius Strobl "failed to select\n", rca); 2181aca38eabSMarius Strobl continue; 218272dec079SMarius Strobl } 2183aca38eabSMarius Strobl 2184aca38eabSMarius Strobl if (timing == bus_timing_mmc_hs200 || /* includes HS400 */ 2185aca38eabSMarius Strobl timing == bus_timing_mmc_hs400es) { 2186aca38eabSMarius Strobl if (mmc_set_vccq(sc, ivar, timing) != MMC_ERR_NONE) { 2187aca38eabSMarius Strobl device_printf(dev, "Failed to set VCCQ for " 2188aca38eabSMarius Strobl "card at relative address %d\n", rca); 2189aca38eabSMarius Strobl continue; 2190aca38eabSMarius Strobl } 2191aca38eabSMarius Strobl } 2192aca38eabSMarius Strobl 2193aca38eabSMarius Strobl if (timing == bus_timing_mmc_hs200) { /* includes HS400 */ 2194aca38eabSMarius Strobl /* Set bus width (required for initial tuning). */ 2195aca38eabSMarius Strobl if (mmc_set_card_bus_width(sc, ivar, timing) != 2196aca38eabSMarius Strobl MMC_ERR_NONE) { 2197aca38eabSMarius Strobl device_printf(dev, "Card at relative address " 2198aca38eabSMarius Strobl "%d failed to set bus width\n", rca); 2199aca38eabSMarius Strobl continue; 2200aca38eabSMarius Strobl } 2201aca38eabSMarius Strobl mmcbr_set_bus_width(dev, ivar->bus_width); 2202aca38eabSMarius Strobl mmcbr_update_ios(dev); 2203aca38eabSMarius Strobl } else if (timing == bus_timing_mmc_hs400es) { 2204aca38eabSMarius Strobl if (mmc_switch_to_hs400(sc, ivar, max_dtr, timing) != 2205aca38eabSMarius Strobl MMC_ERR_NONE) { 2206aca38eabSMarius Strobl device_printf(dev, "Card at relative address " 2207aca38eabSMarius Strobl "%d failed to set %s timing\n", rca, 2208aca38eabSMarius Strobl mmc_timing_to_string(timing)); 2209aca38eabSMarius Strobl continue; 2210aca38eabSMarius Strobl } 2211aca38eabSMarius Strobl goto power_class; 2212aca38eabSMarius Strobl } 2213aca38eabSMarius Strobl 2214aca38eabSMarius Strobl if (mmc_set_timing(sc, ivar, timing) != MMC_ERR_NONE) { 2215aca38eabSMarius Strobl device_printf(dev, "Card at relative address %d " 2216aca38eabSMarius Strobl "failed to set %s timing\n", rca, 2217aca38eabSMarius Strobl mmc_timing_to_string(timing)); 2218aca38eabSMarius Strobl continue; 2219aca38eabSMarius Strobl } 2220aca38eabSMarius Strobl 2221aca38eabSMarius Strobl if (timing == bus_timing_mmc_ddr52) { 2222aca38eabSMarius Strobl /* 2223aca38eabSMarius Strobl * Set EXT_CSD_BUS_WIDTH_n_DDR in EXT_CSD_BUS_WIDTH 2224aca38eabSMarius Strobl * (must be done after switching to EXT_CSD_HS_TIMING). 2225aca38eabSMarius Strobl */ 2226aca38eabSMarius Strobl if (mmc_set_card_bus_width(sc, ivar, timing) != 2227aca38eabSMarius Strobl MMC_ERR_NONE) { 2228aca38eabSMarius Strobl device_printf(dev, "Card at relative address " 2229aca38eabSMarius Strobl "%d failed to set bus width\n", rca); 2230aca38eabSMarius Strobl continue; 2231aca38eabSMarius Strobl } 2232aca38eabSMarius Strobl mmcbr_set_bus_width(dev, ivar->bus_width); 2233aca38eabSMarius Strobl mmcbr_update_ios(dev); 2234aca38eabSMarius Strobl if (mmc_set_vccq(sc, ivar, timing) != MMC_ERR_NONE) { 2235aca38eabSMarius Strobl device_printf(dev, "Failed to set VCCQ for " 2236aca38eabSMarius Strobl "card at relative address %d\n", rca); 2237aca38eabSMarius Strobl continue; 2238aca38eabSMarius Strobl } 2239aca38eabSMarius Strobl } 2240aca38eabSMarius Strobl 2241aca38eabSMarius Strobl /* Set clock (must be done before initial tuning). */ 2242aca38eabSMarius Strobl mmcbr_set_clock(dev, max_dtr); 2243aca38eabSMarius Strobl mmcbr_update_ios(dev); 2244aca38eabSMarius Strobl 2245aca38eabSMarius Strobl if (mmcbr_tune(dev, hs400) != 0) { 2246aca38eabSMarius Strobl device_printf(dev, "Card at relative address %d " 2247aca38eabSMarius Strobl "failed to execute initial tuning\n", rca); 2248aca38eabSMarius Strobl continue; 2249aca38eabSMarius Strobl } 2250aca38eabSMarius Strobl 2251aca38eabSMarius Strobl if (hs400 == true && mmc_switch_to_hs400(sc, ivar, max_dtr, 2252aca38eabSMarius Strobl max_timing) != MMC_ERR_NONE) { 2253aca38eabSMarius Strobl device_printf(dev, "Card at relative address %d " 2254aca38eabSMarius Strobl "failed to set %s timing\n", rca, 2255aca38eabSMarius Strobl mmc_timing_to_string(max_timing)); 2256aca38eabSMarius Strobl continue; 2257aca38eabSMarius Strobl } 2258aca38eabSMarius Strobl 2259aca38eabSMarius Strobl power_class: 2260aca38eabSMarius Strobl if (mmc_set_power_class(sc, ivar) != MMC_ERR_NONE) { 2261aca38eabSMarius Strobl device_printf(dev, "Card at relative address %d " 2262aca38eabSMarius Strobl "failed to set power class\n", rca); 2263aca38eabSMarius Strobl } 2264aca38eabSMarius Strobl } 2265aca38eabSMarius Strobl (void)mmc_select_card(sc, 0); 226672dec079SMarius Strobl return (max_dtr); 2267114b4164SWarner Losh } 2268114b4164SWarner Losh 2269aca38eabSMarius Strobl /* 2270aca38eabSMarius Strobl * Switch from HS200 to HS400 (either initially or for re-tuning) or directly 2271aca38eabSMarius Strobl * to HS400ES. This follows the sequences described in "6.6.2.3 HS400 timing 2272aca38eabSMarius Strobl * mode selection" of the eMMC specification v5.1. 2273aca38eabSMarius Strobl */ 2274aca38eabSMarius Strobl static int 2275aca38eabSMarius Strobl mmc_switch_to_hs400(struct mmc_softc *sc, struct mmc_ivars *ivar, 2276aca38eabSMarius Strobl uint32_t clock, enum mmc_bus_timing max_timing) 2277aca38eabSMarius Strobl { 2278aca38eabSMarius Strobl device_t dev; 2279aca38eabSMarius Strobl int err; 2280aca38eabSMarius Strobl uint16_t rca; 2281aca38eabSMarius Strobl 2282aca38eabSMarius Strobl dev = sc->dev; 2283aca38eabSMarius Strobl rca = ivar->rca; 2284aca38eabSMarius Strobl 2285aca38eabSMarius Strobl /* 2286aca38eabSMarius Strobl * Both clock and timing must be set as appropriate for high speed 2287aca38eabSMarius Strobl * before eventually switching to HS400/HS400ES; mmc_set_timing() 2288aca38eabSMarius Strobl * will issue mmcbr_update_ios(). 2289aca38eabSMarius Strobl */ 2290aca38eabSMarius Strobl mmcbr_set_clock(dev, ivar->hs_tran_speed); 2291aca38eabSMarius Strobl err = mmc_set_timing(sc, ivar, bus_timing_hs); 2292aca38eabSMarius Strobl if (err != MMC_ERR_NONE) 2293aca38eabSMarius Strobl return (err); 2294aca38eabSMarius Strobl 2295aca38eabSMarius Strobl /* 2296aca38eabSMarius Strobl * Set EXT_CSD_BUS_WIDTH_8_DDR in EXT_CSD_BUS_WIDTH (and additionally 2297aca38eabSMarius Strobl * EXT_CSD_BUS_WIDTH_ES for HS400ES). 2298aca38eabSMarius Strobl */ 2299aca38eabSMarius Strobl err = mmc_set_card_bus_width(sc, ivar, max_timing); 2300aca38eabSMarius Strobl if (err != MMC_ERR_NONE) 2301aca38eabSMarius Strobl return (err); 2302aca38eabSMarius Strobl mmcbr_set_bus_width(dev, ivar->bus_width); 2303aca38eabSMarius Strobl mmcbr_update_ios(dev); 2304aca38eabSMarius Strobl 2305aca38eabSMarius Strobl /* Finally, switch to HS400/HS400ES mode. */ 2306aca38eabSMarius Strobl err = mmc_set_timing(sc, ivar, max_timing); 2307aca38eabSMarius Strobl if (err != MMC_ERR_NONE) 2308aca38eabSMarius Strobl return (err); 2309aca38eabSMarius Strobl mmcbr_set_clock(dev, clock); 2310aca38eabSMarius Strobl mmcbr_update_ios(dev); 2311aca38eabSMarius Strobl return (MMC_ERR_NONE); 2312aca38eabSMarius Strobl } 2313aca38eabSMarius Strobl 2314aca38eabSMarius Strobl /* 2315aca38eabSMarius Strobl * Switch from HS400 to HS200 (for re-tuning). 2316aca38eabSMarius Strobl */ 2317aca38eabSMarius Strobl static int 2318aca38eabSMarius Strobl mmc_switch_to_hs200(struct mmc_softc *sc, struct mmc_ivars *ivar, 2319aca38eabSMarius Strobl uint32_t clock) 2320aca38eabSMarius Strobl { 2321aca38eabSMarius Strobl device_t dev; 2322aca38eabSMarius Strobl int err; 2323aca38eabSMarius Strobl uint16_t rca; 2324aca38eabSMarius Strobl 2325aca38eabSMarius Strobl dev = sc->dev; 2326aca38eabSMarius Strobl rca = ivar->rca; 2327aca38eabSMarius Strobl 2328aca38eabSMarius Strobl /* 2329aca38eabSMarius Strobl * Both clock and timing must initially be set as appropriate for 2330aca38eabSMarius Strobl * DDR52 before eventually switching to HS200; mmc_set_timing() 2331aca38eabSMarius Strobl * will issue mmcbr_update_ios(). 2332aca38eabSMarius Strobl */ 2333aca38eabSMarius Strobl mmcbr_set_clock(dev, ivar->hs_tran_speed); 2334aca38eabSMarius Strobl err = mmc_set_timing(sc, ivar, bus_timing_mmc_ddr52); 2335aca38eabSMarius Strobl if (err != MMC_ERR_NONE) 2336aca38eabSMarius Strobl return (err); 2337aca38eabSMarius Strobl 2338aca38eabSMarius Strobl /* 2339aca38eabSMarius Strobl * Next, switch to high speed. Thus, clear EXT_CSD_BUS_WIDTH_n_DDR 2340aca38eabSMarius Strobl * in EXT_CSD_BUS_WIDTH and update bus width and timing in ios. 2341aca38eabSMarius Strobl */ 2342aca38eabSMarius Strobl err = mmc_set_card_bus_width(sc, ivar, bus_timing_hs); 2343aca38eabSMarius Strobl if (err != MMC_ERR_NONE) 2344aca38eabSMarius Strobl return (err); 2345aca38eabSMarius Strobl mmcbr_set_bus_width(dev, ivar->bus_width); 2346aca38eabSMarius Strobl mmcbr_set_timing(sc->dev, bus_timing_hs); 2347aca38eabSMarius Strobl mmcbr_update_ios(dev); 2348aca38eabSMarius Strobl 2349aca38eabSMarius Strobl /* Finally, switch to HS200 mode. */ 2350aca38eabSMarius Strobl err = mmc_set_timing(sc, ivar, bus_timing_mmc_hs200); 2351aca38eabSMarius Strobl if (err != MMC_ERR_NONE) 2352aca38eabSMarius Strobl return (err); 2353aca38eabSMarius Strobl mmcbr_set_clock(dev, clock); 2354aca38eabSMarius Strobl mmcbr_update_ios(dev); 2355aca38eabSMarius Strobl return (MMC_ERR_NONE); 2356aca38eabSMarius Strobl } 2357aca38eabSMarius Strobl 2358aca38eabSMarius Strobl static int 2359aca38eabSMarius Strobl mmc_retune(device_t busdev, device_t dev, bool reset) 2360aca38eabSMarius Strobl { 2361aca38eabSMarius Strobl struct mmc_softc *sc; 2362aca38eabSMarius Strobl struct mmc_ivars *ivar; 2363aca38eabSMarius Strobl int err; 2364aca38eabSMarius Strobl uint32_t clock; 2365aca38eabSMarius Strobl enum mmc_bus_timing timing; 2366aca38eabSMarius Strobl 2367aca38eabSMarius Strobl if (device_get_parent(dev) != busdev) 2368aca38eabSMarius Strobl return (MMC_ERR_INVALID); 2369aca38eabSMarius Strobl 2370aca38eabSMarius Strobl sc = device_get_softc(busdev); 2371aca38eabSMarius Strobl if (sc->retune_needed != 1 && sc->retune_paused != 0) 2372aca38eabSMarius Strobl return (MMC_ERR_INVALID); 2373aca38eabSMarius Strobl 2374aca38eabSMarius Strobl timing = mmcbr_get_timing(busdev); 2375aca38eabSMarius Strobl if (timing == bus_timing_mmc_hs400) { 2376aca38eabSMarius Strobl /* 2377aca38eabSMarius Strobl * Controllers use the data strobe line to latch data from 2378aca38eabSMarius Strobl * the devices in HS400 mode so periodic re-tuning isn't 2379aca38eabSMarius Strobl * expected to be required, i. e. only if a CRC or tuning 2380aca38eabSMarius Strobl * error is signaled to the bridge. In these latter cases 2381aca38eabSMarius Strobl * we are asked to reset the tuning circuit and need to do 2382aca38eabSMarius Strobl * the switch timing dance. 2383aca38eabSMarius Strobl */ 2384aca38eabSMarius Strobl if (reset == false) 2385aca38eabSMarius Strobl return (0); 2386aca38eabSMarius Strobl ivar = device_get_ivars(dev); 2387aca38eabSMarius Strobl clock = mmcbr_get_clock(busdev); 2388aca38eabSMarius Strobl if (mmc_switch_to_hs200(sc, ivar, clock) != MMC_ERR_NONE) 2389aca38eabSMarius Strobl return (MMC_ERR_BADCRC); 2390aca38eabSMarius Strobl } 2391aca38eabSMarius Strobl err = mmcbr_retune(busdev, reset); 2392aca38eabSMarius Strobl if (err != 0 && timing == bus_timing_mmc_hs400) 2393aca38eabSMarius Strobl return (MMC_ERR_BADCRC); 2394aca38eabSMarius Strobl switch (err) { 2395aca38eabSMarius Strobl case 0: 2396aca38eabSMarius Strobl break; 2397aca38eabSMarius Strobl case EIO: 2398aca38eabSMarius Strobl return (MMC_ERR_FAILED); 2399aca38eabSMarius Strobl default: 2400aca38eabSMarius Strobl return (MMC_ERR_INVALID); 2401aca38eabSMarius Strobl } 2402aca38eabSMarius Strobl if (timing == bus_timing_mmc_hs400) { 2403aca38eabSMarius Strobl if (mmc_switch_to_hs400(sc, ivar, clock, timing) != 2404aca38eabSMarius Strobl MMC_ERR_NONE) 2405aca38eabSMarius Strobl return (MMC_ERR_BADCRC); 2406aca38eabSMarius Strobl } 2407aca38eabSMarius Strobl return (MMC_ERR_NONE); 2408aca38eabSMarius Strobl } 2409aca38eabSMarius Strobl 2410aca38eabSMarius Strobl static void 2411aca38eabSMarius Strobl mmc_retune_pause(device_t busdev, device_t dev, bool retune) 2412aca38eabSMarius Strobl { 2413aca38eabSMarius Strobl struct mmc_softc *sc; 2414aca38eabSMarius Strobl 2415aca38eabSMarius Strobl sc = device_get_softc(busdev); 2416aca38eabSMarius Strobl KASSERT(device_get_parent(dev) == busdev, 2417aca38eabSMarius Strobl ("%s: %s is not a child of %s", __func__, device_get_nameunit(dev), 2418aca38eabSMarius Strobl device_get_nameunit(busdev))); 2419aca38eabSMarius Strobl KASSERT(sc->owner != NULL, 2420aca38eabSMarius Strobl ("%s: Request from %s without bus being acquired.", __func__, 2421aca38eabSMarius Strobl device_get_nameunit(dev))); 2422aca38eabSMarius Strobl 2423aca38eabSMarius Strobl if (retune == true && sc->retune_paused == 0) 2424aca38eabSMarius Strobl sc->retune_needed = 1; 2425aca38eabSMarius Strobl sc->retune_paused++; 2426aca38eabSMarius Strobl } 2427aca38eabSMarius Strobl 2428aca38eabSMarius Strobl static void 2429aca38eabSMarius Strobl mmc_retune_unpause(device_t busdev, device_t dev) 2430aca38eabSMarius Strobl { 2431aca38eabSMarius Strobl struct mmc_softc *sc; 2432aca38eabSMarius Strobl 2433aca38eabSMarius Strobl sc = device_get_softc(busdev); 2434aca38eabSMarius Strobl KASSERT(device_get_parent(dev) == busdev, 2435aca38eabSMarius Strobl ("%s: %s is not a child of %s", __func__, device_get_nameunit(dev), 2436aca38eabSMarius Strobl device_get_nameunit(busdev))); 2437aca38eabSMarius Strobl KASSERT(sc->owner != NULL, 2438aca38eabSMarius Strobl ("%s: Request from %s without bus being acquired.", __func__, 2439aca38eabSMarius Strobl device_get_nameunit(dev))); 2440aca38eabSMarius Strobl KASSERT(sc->retune_paused != 0, 2441aca38eabSMarius Strobl ("%s: Re-tune pause count already at 0", __func__)); 2442aca38eabSMarius Strobl 2443aca38eabSMarius Strobl sc->retune_paused--; 2444aca38eabSMarius Strobl } 2445aca38eabSMarius Strobl 2446114b4164SWarner Losh static void 2447114b4164SWarner Losh mmc_scan(struct mmc_softc *sc) 2448114b4164SWarner Losh { 2449eb67f31aSAlexander Motin device_t dev = sc->dev; 2450aca38eabSMarius Strobl int err; 2451114b4164SWarner Losh 2452aca38eabSMarius Strobl err = mmc_acquire_bus(dev, dev); 2453aca38eabSMarius Strobl if (err != 0) { 2454aca38eabSMarius Strobl device_printf(dev, "Failed to acquire bus for scanning\n"); 2455aca38eabSMarius Strobl return; 2456aca38eabSMarius Strobl } 2457114b4164SWarner Losh mmc_go_discovery(sc); 2458aca38eabSMarius Strobl err = mmc_release_bus(dev, dev); 2459aca38eabSMarius Strobl if (err != 0) { 2460aca38eabSMarius Strobl device_printf(dev, "Failed to release bus after scanning\n"); 2461aca38eabSMarius Strobl return; 2462aca38eabSMarius Strobl } 2463aca38eabSMarius Strobl (void)bus_generic_attach(dev); 2464114b4164SWarner Losh } 2465114b4164SWarner Losh 2466114b4164SWarner Losh static int 2467125da88aSWarner Losh mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result) 2468114b4164SWarner Losh { 2469114b4164SWarner Losh struct mmc_ivars *ivar = device_get_ivars(child); 2470114b4164SWarner Losh 2471114b4164SWarner Losh switch (which) { 2472114b4164SWarner Losh default: 2473114b4164SWarner Losh return (EINVAL); 247472dec079SMarius Strobl case MMC_IVAR_SPEC_VERS: 247572dec079SMarius Strobl *result = ivar->csd.spec_vers; 247672dec079SMarius Strobl break; 2477114b4164SWarner Losh case MMC_IVAR_DSR_IMP: 2478bcd91d25SJayachandran C. *result = ivar->csd.dsr_imp; 2479114b4164SWarner Losh break; 2480114b4164SWarner Losh case MMC_IVAR_MEDIA_SIZE: 2481bcd91d25SJayachandran C. *result = ivar->sec_count; 2482114b4164SWarner Losh break; 2483114b4164SWarner Losh case MMC_IVAR_RCA: 2484bcd91d25SJayachandran C. *result = ivar->rca; 2485114b4164SWarner Losh break; 2486114b4164SWarner Losh case MMC_IVAR_SECTOR_SIZE: 2487bcd91d25SJayachandran C. *result = MMC_SECTOR_SIZE; 2488114b4164SWarner Losh break; 2489114b4164SWarner Losh case MMC_IVAR_TRAN_SPEED: 2490bcd91d25SJayachandran C. *result = mmcbr_get_clock(bus); 2491114b4164SWarner Losh break; 249240fab6e7SWarner Losh case MMC_IVAR_READ_ONLY: 2493bcd91d25SJayachandran C. *result = ivar->read_only; 249440fab6e7SWarner Losh break; 2495c18f1e26SAlexander Motin case MMC_IVAR_HIGH_CAP: 2496bcd91d25SJayachandran C. *result = ivar->high_cap; 2497c18f1e26SAlexander Motin break; 249867d752c3SAlexander Motin case MMC_IVAR_CARD_TYPE: 2499bcd91d25SJayachandran C. *result = ivar->mode; 250067d752c3SAlexander Motin break; 250167d752c3SAlexander Motin case MMC_IVAR_BUS_WIDTH: 2502bcd91d25SJayachandran C. *result = ivar->bus_width; 250367d752c3SAlexander Motin break; 25043906d42dSAlexander Motin case MMC_IVAR_ERASE_SECTOR: 2505bcd91d25SJayachandran C. *result = ivar->erase_sector; 25063906d42dSAlexander Motin break; 25073a4a2557SAlexander Motin case MMC_IVAR_MAX_DATA: 2508bcd91d25SJayachandran C. *result = mmcbr_get_max_data(bus); 25093a4a2557SAlexander Motin break; 2510*79f39c6aSMarius Strobl case MMC_IVAR_CMD6_TIMEOUT: 2511*79f39c6aSMarius Strobl *result = ivar->cmd6_time; 2512*79f39c6aSMarius Strobl break; 2513*79f39c6aSMarius Strobl case MMC_IVAR_QUIRKS: 2514*79f39c6aSMarius Strobl *result = ivar->quirks; 2515*79f39c6aSMarius Strobl break; 25167aa65846SMarius Strobl case MMC_IVAR_CARD_ID_STRING: 25177aa65846SMarius Strobl *(char **)result = ivar->card_id_string; 25187aa65846SMarius Strobl break; 2519f3a4b7f7SIan Lepore case MMC_IVAR_CARD_SN_STRING: 2520f3a4b7f7SIan Lepore *(char **)result = ivar->card_sn_string; 2521f3a4b7f7SIan Lepore break; 2522114b4164SWarner Losh } 2523114b4164SWarner Losh return (0); 2524114b4164SWarner Losh } 2525114b4164SWarner Losh 2526114b4164SWarner Losh static int 2527114b4164SWarner Losh mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 2528114b4164SWarner Losh { 25291bacf3beSMarius Strobl 25306fdacf52SWarner Losh /* 25316fdacf52SWarner Losh * None are writable ATM 25326fdacf52SWarner Losh */ 2533114b4164SWarner Losh return (EINVAL); 2534114b4164SWarner Losh } 2535114b4164SWarner Losh 2536114b4164SWarner Losh static void 2537114b4164SWarner Losh mmc_delayed_attach(void *xsc) 2538114b4164SWarner Losh { 2539114b4164SWarner Losh struct mmc_softc *sc = xsc; 2540114b4164SWarner Losh 2541114b4164SWarner Losh mmc_scan(sc); 2542114b4164SWarner Losh config_intrhook_disestablish(&sc->config_intrhook); 2543114b4164SWarner Losh } 2544114b4164SWarner Losh 2545b5bc46d8SAlexander Motin static int 2546b5bc46d8SAlexander Motin mmc_child_location_str(device_t dev, device_t child, char *buf, 2547b5bc46d8SAlexander Motin size_t buflen) 2548b5bc46d8SAlexander Motin { 2549b5bc46d8SAlexander Motin 2550b5bc46d8SAlexander Motin snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child)); 2551b5bc46d8SAlexander Motin return (0); 2552b5bc46d8SAlexander Motin } 2553b5bc46d8SAlexander Motin 2554114b4164SWarner Losh static device_method_t mmc_methods[] = { 2555114b4164SWarner Losh /* device_if */ 2556114b4164SWarner Losh DEVMETHOD(device_probe, mmc_probe), 2557114b4164SWarner Losh DEVMETHOD(device_attach, mmc_attach), 2558114b4164SWarner Losh DEVMETHOD(device_detach, mmc_detach), 2559eb67f31aSAlexander Motin DEVMETHOD(device_suspend, mmc_suspend), 2560eb67f31aSAlexander Motin DEVMETHOD(device_resume, mmc_resume), 2561114b4164SWarner Losh 2562114b4164SWarner Losh /* Bus interface */ 2563114b4164SWarner Losh DEVMETHOD(bus_read_ivar, mmc_read_ivar), 2564114b4164SWarner Losh DEVMETHOD(bus_write_ivar, mmc_write_ivar), 2565b5bc46d8SAlexander Motin DEVMETHOD(bus_child_location_str, mmc_child_location_str), 2566114b4164SWarner Losh 2567114b4164SWarner Losh /* MMC Bus interface */ 2568aca38eabSMarius Strobl DEVMETHOD(mmcbus_retune_pause, mmc_retune_pause), 2569aca38eabSMarius Strobl DEVMETHOD(mmcbus_retune_unpause, mmc_retune_unpause), 2570114b4164SWarner Losh DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request), 2571114b4164SWarner Losh DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus), 2572114b4164SWarner Losh DEVMETHOD(mmcbus_release_bus, mmc_release_bus), 2573114b4164SWarner Losh 25747aa65846SMarius Strobl DEVMETHOD_END 2575114b4164SWarner Losh }; 2576114b4164SWarner Losh 25773f627274SIan Lepore driver_t mmc_driver = { 2578114b4164SWarner Losh "mmc", 2579114b4164SWarner Losh mmc_methods, 2580114b4164SWarner Losh sizeof(struct mmc_softc), 2581114b4164SWarner Losh }; 25823f627274SIan Lepore devclass_t mmc_devclass; 2583dec48737SIan Lepore 258472dec079SMarius Strobl MODULE_VERSION(mmc, MMC_VERSION); 2585