1114b4164SWarner Losh /*- 2114b4164SWarner Losh * Copyright (c) 2006 Bernd Walter. All rights reserved. 3114b4164SWarner Losh * Copyright (c) 2006 M. Warner Losh. All rights reserved. 4114b4164SWarner Losh * 5114b4164SWarner Losh * Redistribution and use in source and binary forms, with or without 6114b4164SWarner Losh * modification, are permitted provided that the following conditions 7114b4164SWarner Losh * are met: 8114b4164SWarner Losh * 1. Redistributions of source code must retain the above copyright 9114b4164SWarner Losh * notice, this list of conditions and the following disclaimer. 10114b4164SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 11114b4164SWarner Losh * notice, this list of conditions and the following disclaimer in the 12114b4164SWarner Losh * documentation and/or other materials provided with the distribution. 13114b4164SWarner Losh * 14114b4164SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15114b4164SWarner Losh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16114b4164SWarner Losh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17114b4164SWarner Losh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18114b4164SWarner Losh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19114b4164SWarner Losh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20114b4164SWarner Losh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21114b4164SWarner Losh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22114b4164SWarner Losh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23114b4164SWarner Losh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2414eced72SWarner Losh * 2514eced72SWarner Losh * Portions of this software may have been developed with reference to 2614eced72SWarner Losh * the SD Simplified Specification. The following disclaimer may apply: 2714eced72SWarner Losh * 2814eced72SWarner Losh * The following conditions apply to the release of the simplified 2914eced72SWarner Losh * specification ("Simplified Specification") by the SD Card Association and 3014eced72SWarner Losh * the SD Group. The Simplified Specification is a subset of the complete SD 3114eced72SWarner Losh * Specification which is owned by the SD Card Association and the SD 3214eced72SWarner Losh * Group. This Simplified Specification is provided on a non-confidential 3314eced72SWarner Losh * basis subject to the disclaimers below. Any implementation of the 3414eced72SWarner Losh * Simplified Specification may require a license from the SD Card 3514eced72SWarner Losh * Association, SD Group, SD-3C LLC or other third parties. 3614eced72SWarner Losh * 3714eced72SWarner Losh * Disclaimers: 3814eced72SWarner Losh * 3914eced72SWarner Losh * The information contained in the Simplified Specification is presented only 4014eced72SWarner Losh * as a standard specification for SD Cards and SD Host/Ancillary products and 4114eced72SWarner Losh * is provided "AS-IS" without any representations or warranties of any 4214eced72SWarner Losh * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 4314eced72SWarner Losh * Card Association for any damages, any infringements of patents or other 4414eced72SWarner Losh * right of the SD Group, SD-3C LLC, the SD Card Association or any third 4514eced72SWarner Losh * parties, which may result from its use. No license is granted by 4614eced72SWarner Losh * implication, estoppel or otherwise under any patent or other rights of the 4714eced72SWarner Losh * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 4814eced72SWarner Losh * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 4914eced72SWarner Losh * or the SD Card Association to disclose or distribute any technical 5014eced72SWarner Losh * information, know-how or other confidential information to any third party. 51114b4164SWarner Losh */ 52114b4164SWarner Losh 53114b4164SWarner Losh #include <sys/cdefs.h> 54114b4164SWarner Losh __FBSDID("$FreeBSD$"); 55114b4164SWarner Losh 56114b4164SWarner Losh #include <sys/param.h> 57114b4164SWarner Losh #include <sys/systm.h> 58114b4164SWarner Losh #include <sys/kernel.h> 59114b4164SWarner Losh #include <sys/malloc.h> 60114b4164SWarner Losh #include <sys/lock.h> 61114b4164SWarner Losh #include <sys/module.h> 62114b4164SWarner Losh #include <sys/mutex.h> 63114b4164SWarner Losh #include <sys/bus.h> 64114b4164SWarner Losh 65114b4164SWarner Losh #include <dev/mmc/mmcreg.h> 66114b4164SWarner Losh #include <dev/mmc/mmcbrvar.h> 67114b4164SWarner Losh #include <dev/mmc/mmcvar.h> 68114b4164SWarner Losh #include "mmcbr_if.h" 69114b4164SWarner Losh #include "mmcbus_if.h" 70114b4164SWarner Losh 71114b4164SWarner Losh struct mmc_softc { 72114b4164SWarner Losh device_t dev; 73114b4164SWarner Losh struct mtx sc_mtx; 74114b4164SWarner Losh struct intr_config_hook config_intrhook; 75114b4164SWarner Losh device_t owner; 76114b4164SWarner Losh uint32_t last_rca; 77114b4164SWarner Losh }; 78114b4164SWarner Losh 79114b4164SWarner Losh /* 80114b4164SWarner Losh * Per-card data 81114b4164SWarner Losh */ 82114b4164SWarner Losh struct mmc_ivars { 83114b4164SWarner Losh uint32_t raw_cid[4]; /* Raw bits of the CID */ 84114b4164SWarner Losh uint32_t raw_csd[4]; /* Raw bits of the CSD */ 85114b4164SWarner Losh uint16_t rca; 86114b4164SWarner Losh enum mmc_card_mode mode; 87114b4164SWarner Losh struct mmc_cid cid; /* cid decoded */ 88114b4164SWarner Losh struct mmc_csd csd; /* csd decoded */ 8940fab6e7SWarner Losh u_char read_only; /* True when the device is read-only */ 90114b4164SWarner Losh }; 91114b4164SWarner Losh 92114b4164SWarner Losh #define CMD_RETRIES 3 93114b4164SWarner Losh 94114b4164SWarner Losh /* bus entry points */ 95114b4164SWarner Losh static int mmc_probe(device_t dev); 96114b4164SWarner Losh static int mmc_attach(device_t dev); 97114b4164SWarner Losh static int mmc_detach(device_t dev); 98114b4164SWarner Losh 99114b4164SWarner Losh #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 100114b4164SWarner Losh #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 101114b4164SWarner Losh #define MMC_LOCK_INIT(_sc) \ 102114b4164SWarner Losh mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 103114b4164SWarner Losh "mmc", MTX_DEF) 104114b4164SWarner Losh #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 105114b4164SWarner Losh #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 106114b4164SWarner Losh #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 107114b4164SWarner Losh 108114b4164SWarner Losh static void mmc_delayed_attach(void *); 1095d2f6510SWarner Losh static void mmc_power_down(struct mmc_softc *sc); 110114b4164SWarner Losh static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, 111114b4164SWarner Losh int retries); 112114b4164SWarner Losh static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 113114b4164SWarner Losh uint32_t arg, uint32_t flags, uint32_t *resp, int retries); 114114b4164SWarner Losh 115114b4164SWarner Losh static void 116114b4164SWarner Losh mmc_ms_delay(int ms) 117114b4164SWarner Losh { 118114b4164SWarner Losh DELAY(1000 * ms); /* XXX BAD */ 119114b4164SWarner Losh } 120114b4164SWarner Losh 121114b4164SWarner Losh static int 122114b4164SWarner Losh mmc_probe(device_t dev) 123114b4164SWarner Losh { 124114b4164SWarner Losh 1252703cdbbSWarner Losh device_set_desc(dev, "MMC/SD bus"); 126114b4164SWarner Losh return (0); 127114b4164SWarner Losh } 128114b4164SWarner Losh 129114b4164SWarner Losh static int 130114b4164SWarner Losh mmc_attach(device_t dev) 131114b4164SWarner Losh { 132114b4164SWarner Losh struct mmc_softc *sc; 133114b4164SWarner Losh 134114b4164SWarner Losh sc = device_get_softc(dev); 135114b4164SWarner Losh sc->dev = dev; 136114b4164SWarner Losh MMC_LOCK_INIT(sc); 137114b4164SWarner Losh 138114b4164SWarner Losh /* We'll probe and attach our children later, but before / mount */ 139114b4164SWarner Losh sc->config_intrhook.ich_func = mmc_delayed_attach; 140114b4164SWarner Losh sc->config_intrhook.ich_arg = sc; 141114b4164SWarner Losh if (config_intrhook_establish(&sc->config_intrhook) != 0) 142114b4164SWarner Losh device_printf(dev, "config_intrhook_establish failed\n"); 143114b4164SWarner Losh return (0); 144114b4164SWarner Losh } 145114b4164SWarner Losh 146114b4164SWarner Losh static int 147114b4164SWarner Losh mmc_detach(device_t dev) 148114b4164SWarner Losh { 1490e1466acSWarner Losh struct mmc_softc *sc = device_get_softc(dev); 1500e1466acSWarner Losh device_t *kids; 1510e1466acSWarner Losh int i, nkid; 1520e1466acSWarner Losh 1530e1466acSWarner Losh /* kill children [ph33r]. -sorbo */ 1540e1466acSWarner Losh if (device_get_children(sc->dev, &kids, &nkid) != 0) 1550e1466acSWarner Losh return 0; 1560e1466acSWarner Losh for (i = 0; i < nkid; i++) { 1570e1466acSWarner Losh device_t kid = kids[i]; 1580e1466acSWarner Losh void *ivar = device_get_ivars(kid); 1590e1466acSWarner Losh 1600e1466acSWarner Losh device_detach(kid); 1610e1466acSWarner Losh device_delete_child(sc->dev, kid); 1620e1466acSWarner Losh free(ivar, M_DEVBUF); 1630e1466acSWarner Losh } 1640e1466acSWarner Losh free(kids, M_TEMP); 1655d2f6510SWarner Losh mmc_power_down(sc); 1660e1466acSWarner Losh 1670e1466acSWarner Losh MMC_LOCK_DESTROY(sc); 1680e1466acSWarner Losh 1690e1466acSWarner Losh return 0; 170114b4164SWarner Losh } 171114b4164SWarner Losh 172114b4164SWarner Losh static int 173114b4164SWarner Losh mmc_acquire_bus(device_t busdev, device_t dev) 174114b4164SWarner Losh { 175114b4164SWarner Losh struct mmc_softc *sc; 176114b4164SWarner Losh int err; 177114b4164SWarner Losh int rca; 178114b4164SWarner Losh 179e9bb9da3SWarner Losh err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev); 180114b4164SWarner Losh if (err) 181114b4164SWarner Losh return (err); 182114b4164SWarner Losh sc = device_get_softc(busdev); 183114b4164SWarner Losh MMC_LOCK(sc); 184114b4164SWarner Losh if (sc->owner) 185114b4164SWarner Losh panic("mmc: host bridge didn't seralize us."); 186114b4164SWarner Losh sc->owner = dev; 187114b4164SWarner Losh MMC_UNLOCK(sc); 188114b4164SWarner Losh 189114b4164SWarner Losh if (busdev != dev) { 1906fdacf52SWarner Losh /* 1916fdacf52SWarner Losh * Keep track of the last rca that we've selected. If 1926fdacf52SWarner Losh * we're asked to do it again, don't. We never 1936fdacf52SWarner Losh * unselect unless the bus code itself wants the mmc 1946fdacf52SWarner Losh * bus, and constantly reselecting causes problems. 1956fdacf52SWarner Losh */ 196114b4164SWarner Losh rca = mmc_get_rca(dev); 197114b4164SWarner Losh if (sc->last_rca != rca) { 198114b4164SWarner Losh mmc_wait_for_command(sc, MMC_SELECT_CARD, rca << 16, 199114b4164SWarner Losh MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES); 200114b4164SWarner Losh sc->last_rca = rca; 201114b4164SWarner Losh } 2026fdacf52SWarner Losh /* XXX should set bus width here? */ 203114b4164SWarner Losh } else { 2046fdacf52SWarner Losh /* 2056fdacf52SWarner Losh * If there's a card selected, stand down. 2066fdacf52SWarner Losh */ 207114b4164SWarner Losh if (sc->last_rca != 0) { 208114b4164SWarner Losh mmc_wait_for_command(sc, MMC_SELECT_CARD, 0, 209114b4164SWarner Losh MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES); 210114b4164SWarner Losh sc->last_rca = 0; 211114b4164SWarner Losh } 2126fdacf52SWarner Losh /* XXX should set bus width here? */ 213114b4164SWarner Losh } 214114b4164SWarner Losh 215114b4164SWarner Losh return (0); 216114b4164SWarner Losh } 217114b4164SWarner Losh 218114b4164SWarner Losh static int 219114b4164SWarner Losh mmc_release_bus(device_t busdev, device_t dev) 220114b4164SWarner Losh { 221114b4164SWarner Losh struct mmc_softc *sc; 222114b4164SWarner Losh int err; 223114b4164SWarner Losh 224114b4164SWarner Losh sc = device_get_softc(busdev); 225114b4164SWarner Losh 226114b4164SWarner Losh MMC_LOCK(sc); 227114b4164SWarner Losh if (!sc->owner) 228114b4164SWarner Losh panic("mmc: releasing unowned bus."); 229114b4164SWarner Losh if (sc->owner != dev) 230114b4164SWarner Losh panic("mmc: you don't own the bus. game over."); 231114b4164SWarner Losh MMC_UNLOCK(sc); 232e9bb9da3SWarner Losh err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev); 233114b4164SWarner Losh if (err) 234114b4164SWarner Losh return (err); 235114b4164SWarner Losh MMC_LOCK(sc); 236114b4164SWarner Losh sc->owner = NULL; 237114b4164SWarner Losh MMC_UNLOCK(sc); 238114b4164SWarner Losh return (0); 239114b4164SWarner Losh } 240114b4164SWarner Losh 241114b4164SWarner Losh static void 242114b4164SWarner Losh mmc_rescan_cards(struct mmc_softc *sc) 243114b4164SWarner Losh { 244114b4164SWarner Losh /* XXX: Look at the children and see if they respond to status */ 245114b4164SWarner Losh } 246114b4164SWarner Losh 247114b4164SWarner Losh static uint32_t 248114b4164SWarner Losh mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr) 249114b4164SWarner Losh { 2508aaa15e2SWarner Losh 2518aaa15e2SWarner Losh return ocr & MMC_OCR_VOLTAGE; 252114b4164SWarner Losh } 253114b4164SWarner Losh 254114b4164SWarner Losh static int 255114b4164SWarner Losh mmc_highest_voltage(uint32_t ocr) 256114b4164SWarner Losh { 257114b4164SWarner Losh int i; 258114b4164SWarner Losh 259114b4164SWarner Losh for (i = 30; i >= 0; i--) 260114b4164SWarner Losh if (ocr & (1 << i)) 261114b4164SWarner Losh return i; 262114b4164SWarner Losh return (-1); 263114b4164SWarner Losh } 264114b4164SWarner Losh 265114b4164SWarner Losh static void 266114b4164SWarner Losh mmc_wakeup(struct mmc_request *req) 267114b4164SWarner Losh { 268114b4164SWarner Losh struct mmc_softc *sc; 269114b4164SWarner Losh 2706fdacf52SWarner Losh /* printf("Wakeup for req %p done_data %p\n", req, req->done_data); */ 271114b4164SWarner Losh sc = (struct mmc_softc *)req->done_data; 272114b4164SWarner Losh MMC_LOCK(sc); 273114b4164SWarner Losh req->flags |= MMC_REQ_DONE; 274114b4164SWarner Losh wakeup(req); 275114b4164SWarner Losh MMC_UNLOCK(sc); 276114b4164SWarner Losh } 277114b4164SWarner Losh 278114b4164SWarner Losh static int 279114b4164SWarner Losh mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req) 280114b4164SWarner Losh { 281114b4164SWarner Losh int err; 282114b4164SWarner Losh 283114b4164SWarner Losh req->done = mmc_wakeup; 284114b4164SWarner Losh req->done_data = sc; 2856fdacf52SWarner Losh /* printf("Submitting request %p sc %p\n", req, sc); */ 286114b4164SWarner Losh MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req); 287114b4164SWarner Losh MMC_LOCK(sc); 288114b4164SWarner Losh do { 289114b4164SWarner Losh err = msleep(req, &sc->sc_mtx, PZERO | PCATCH, "mmcreq", 290114b4164SWarner Losh hz / 10); 291114b4164SWarner Losh } while (!(req->flags & MMC_REQ_DONE) && err == EAGAIN); 2926fdacf52SWarner Losh /* printf("Request %p done with error %d\n", req, err); */ 293114b4164SWarner Losh MMC_UNLOCK(sc); 294114b4164SWarner Losh return (err); 295114b4164SWarner Losh } 296114b4164SWarner Losh 297114b4164SWarner Losh static int 298114b4164SWarner Losh mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req) 299114b4164SWarner Losh { 300114b4164SWarner Losh struct mmc_softc *sc = device_get_softc(brdev); 301114b4164SWarner Losh 302114b4164SWarner Losh return mmc_wait_for_req(sc, req); 303114b4164SWarner Losh } 304114b4164SWarner Losh 305114b4164SWarner Losh static int 306114b4164SWarner Losh mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries) 307114b4164SWarner Losh { 308114b4164SWarner Losh struct mmc_request mreq; 309114b4164SWarner Losh 310114b4164SWarner Losh memset(&mreq, 0, sizeof(mreq)); 311114b4164SWarner Losh memset(cmd->resp, 0, sizeof(cmd->resp)); 312114b4164SWarner Losh cmd->retries = retries; 313114b4164SWarner Losh cmd->data = NULL; 314114b4164SWarner Losh mreq.cmd = cmd; 3156fdacf52SWarner Losh /* printf("CMD: %x ARG %x\n", cmd->opcode, cmd->arg); */ 316114b4164SWarner Losh mmc_wait_for_req(sc, &mreq); 317114b4164SWarner Losh return (cmd->error); 318114b4164SWarner Losh } 319114b4164SWarner Losh 320114b4164SWarner Losh static int 321114b4164SWarner Losh mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca, 322114b4164SWarner Losh struct mmc_command *cmd, int retries) 323114b4164SWarner Losh { 324114b4164SWarner Losh struct mmc_command appcmd; 325114b4164SWarner Losh int err = MMC_ERR_NONE, i; 326114b4164SWarner Losh 327114b4164SWarner Losh for (i = 0; i <= retries; i++) { 328114b4164SWarner Losh appcmd.opcode = MMC_APP_CMD; 329114b4164SWarner Losh appcmd.arg = rca << 16; 330114b4164SWarner Losh appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 331114b4164SWarner Losh mmc_wait_for_cmd(sc, &appcmd, 0); 332114b4164SWarner Losh err = appcmd.error; 333114b4164SWarner Losh if (err != MMC_ERR_NONE) 334114b4164SWarner Losh continue; 335114b4164SWarner Losh if (!(appcmd.resp[0] & R1_APP_CMD)) 336114b4164SWarner Losh return MMC_ERR_FAILED; 337114b4164SWarner Losh mmc_wait_for_cmd(sc, cmd, 0); 338114b4164SWarner Losh err = cmd->error; 339114b4164SWarner Losh if (err == MMC_ERR_NONE) 340114b4164SWarner Losh break; 341114b4164SWarner Losh } 342114b4164SWarner Losh return (err); 343114b4164SWarner Losh } 344114b4164SWarner Losh 345114b4164SWarner Losh static int 346114b4164SWarner Losh mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 347114b4164SWarner Losh uint32_t arg, uint32_t flags, uint32_t *resp, int retries) 348114b4164SWarner Losh { 349114b4164SWarner Losh struct mmc_command cmd; 350114b4164SWarner Losh int err; 351114b4164SWarner Losh 352114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 353114b4164SWarner Losh cmd.opcode = opcode; 354114b4164SWarner Losh cmd.arg = arg; 355114b4164SWarner Losh cmd.flags = flags; 356114b4164SWarner Losh err = mmc_wait_for_cmd(sc, &cmd, retries); 357114b4164SWarner Losh if (err) 358114b4164SWarner Losh return (err); 359114b4164SWarner Losh if (cmd.error) 360114b4164SWarner Losh return (cmd.error); 361114b4164SWarner Losh if (resp) { 362114b4164SWarner Losh if (flags & MMC_RSP_136) 363114b4164SWarner Losh memcpy(resp, cmd.resp, 4 * sizeof(uint32_t)); 364114b4164SWarner Losh else 365114b4164SWarner Losh *resp = cmd.resp[0]; 366114b4164SWarner Losh } 367114b4164SWarner Losh return (0); 368114b4164SWarner Losh } 369114b4164SWarner Losh 370114b4164SWarner Losh static void 371114b4164SWarner Losh mmc_idle_cards(struct mmc_softc *sc) 372114b4164SWarner Losh { 373114b4164SWarner Losh device_t dev; 374114b4164SWarner Losh struct mmc_command cmd; 375114b4164SWarner Losh 376114b4164SWarner Losh dev = sc->dev; 377114b4164SWarner Losh mmcbr_set_chip_select(dev, cs_high); 378114b4164SWarner Losh mmcbr_update_ios(dev); 379114b4164SWarner Losh mmc_ms_delay(1); 380114b4164SWarner Losh 381114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 382114b4164SWarner Losh cmd.opcode = MMC_GO_IDLE_STATE; 383114b4164SWarner Losh cmd.arg = 0; 384114b4164SWarner Losh cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 385114b4164SWarner Losh mmc_wait_for_cmd(sc, &cmd, 0); 386114b4164SWarner Losh mmc_ms_delay(1); 387114b4164SWarner Losh 388114b4164SWarner Losh mmcbr_set_chip_select(dev, cs_dontcare); 389114b4164SWarner Losh mmcbr_update_ios(dev); 390114b4164SWarner Losh mmc_ms_delay(1); 391114b4164SWarner Losh } 392114b4164SWarner Losh 393114b4164SWarner Losh static int 394114b4164SWarner Losh mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 395114b4164SWarner Losh { 396114b4164SWarner Losh struct mmc_command cmd; 397114b4164SWarner Losh int err = MMC_ERR_NONE, i; 398114b4164SWarner Losh 399114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 400114b4164SWarner Losh cmd.opcode = ACMD_SD_SEND_OP_COND; 401114b4164SWarner Losh cmd.arg = ocr; 402114b4164SWarner Losh cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 403114b4164SWarner Losh 4046634080bSWarner Losh for (i = 0; i < 100; i++) { 405114b4164SWarner Losh err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES); 406114b4164SWarner Losh if (err != MMC_ERR_NONE) 407114b4164SWarner Losh break; 408114b4164SWarner Losh if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || ocr == 0) 409114b4164SWarner Losh break; 410114b4164SWarner Losh err = MMC_ERR_TIMEOUT; 411114b4164SWarner Losh mmc_ms_delay(10); 412114b4164SWarner Losh } 413114b4164SWarner Losh if (rocr && err == MMC_ERR_NONE) 414114b4164SWarner Losh *rocr = cmd.resp[0]; 415114b4164SWarner Losh return err; 416114b4164SWarner Losh } 417114b4164SWarner Losh 418114b4164SWarner Losh static int 419114b4164SWarner Losh mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 420114b4164SWarner Losh { 421114b4164SWarner Losh struct mmc_command cmd; 422114b4164SWarner Losh int err = MMC_ERR_NONE, i; 423114b4164SWarner Losh 424114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 425114b4164SWarner Losh cmd.opcode = MMC_SEND_OP_COND; 426114b4164SWarner Losh cmd.arg = ocr; 427114b4164SWarner Losh cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 428114b4164SWarner Losh 429114b4164SWarner Losh for (i = 0; i < 100; i++) { 430114b4164SWarner Losh err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 431114b4164SWarner Losh if (err != MMC_ERR_NONE) 432114b4164SWarner Losh break; 433114b4164SWarner Losh if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || ocr == 0) 434114b4164SWarner Losh break; 435114b4164SWarner Losh err = MMC_ERR_TIMEOUT; 436114b4164SWarner Losh mmc_ms_delay(10); 437114b4164SWarner Losh } 438114b4164SWarner Losh if (rocr && err == MMC_ERR_NONE) 439114b4164SWarner Losh *rocr = cmd.resp[0]; 440114b4164SWarner Losh return err; 441114b4164SWarner Losh } 442114b4164SWarner Losh 443114b4164SWarner Losh static void 444114b4164SWarner Losh mmc_power_up(struct mmc_softc *sc) 445114b4164SWarner Losh { 446114b4164SWarner Losh device_t dev; 447114b4164SWarner Losh 448114b4164SWarner Losh dev = sc->dev; 449114b4164SWarner Losh mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev))); 450114b4164SWarner Losh mmcbr_set_bus_mode(dev, opendrain); 451114b4164SWarner Losh mmcbr_set_chip_select(dev, cs_dontcare); 452114b4164SWarner Losh mmcbr_set_bus_width(dev, bus_width_1); 453114b4164SWarner Losh mmcbr_set_power_mode(dev, power_up); 454114b4164SWarner Losh mmcbr_set_clock(dev, 0); 455114b4164SWarner Losh mmcbr_update_ios(dev); 456114b4164SWarner Losh mmc_ms_delay(1); 457114b4164SWarner Losh 458114b4164SWarner Losh mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev)); 459114b4164SWarner Losh mmcbr_set_power_mode(dev, power_on); 460114b4164SWarner Losh mmcbr_update_ios(dev); 461114b4164SWarner Losh mmc_ms_delay(2); 462114b4164SWarner Losh } 463114b4164SWarner Losh 4645d2f6510SWarner Losh static void 4655d2f6510SWarner Losh mmc_power_down(struct mmc_softc *sc) 4665d2f6510SWarner Losh { 4675d2f6510SWarner Losh device_t dev = sc->dev; 4685d2f6510SWarner Losh 4695d2f6510SWarner Losh mmcbr_set_bus_mode(dev, opendrain); 4705d2f6510SWarner Losh mmcbr_set_chip_select(dev, cs_dontcare); 4715d2f6510SWarner Losh mmcbr_set_bus_width(dev, bus_width_1); 4725d2f6510SWarner Losh mmcbr_set_power_mode(dev, power_off); 4735d2f6510SWarner Losh mmcbr_set_clock(dev, 0); 4745d2f6510SWarner Losh mmcbr_update_ios(dev); 4755d2f6510SWarner Losh } 4765d2f6510SWarner Losh 477114b4164SWarner Losh static uint32_t 478114b4164SWarner Losh mmc_get_bits(uint32_t *bits, int start, int size) 479114b4164SWarner Losh { 480114b4164SWarner Losh const int i = 3 - (start / 32); 481114b4164SWarner Losh const int shift = start & 31; 482114b4164SWarner Losh uint32_t retval = bits[i] >> shift; 483114b4164SWarner Losh if (size + shift > 32) 484114b4164SWarner Losh retval |= bits[i - 1] << (32 - shift); 485114b4164SWarner Losh return retval & ((1 << size) - 1); 486114b4164SWarner Losh } 487114b4164SWarner Losh 488114b4164SWarner Losh static void 489114b4164SWarner Losh mmc_decode_cid(int is_sd, uint32_t *raw_cid, struct mmc_cid *cid) 490114b4164SWarner Losh { 491114b4164SWarner Losh int i; 492114b4164SWarner Losh 493114b4164SWarner Losh memset(cid, 0, sizeof(*cid)); 494114b4164SWarner Losh if (is_sd) { 495114b4164SWarner Losh /* There's no version info, so we take it on faith */ 496114b4164SWarner Losh cid->mid = mmc_get_bits(raw_cid, 120, 8); 497114b4164SWarner Losh cid->oid = mmc_get_bits(raw_cid, 104, 16); 498114b4164SWarner Losh for (i = 0; i < 5; i++) 499114b4164SWarner Losh cid->pnm[i] = mmc_get_bits(raw_cid, 96 - i * 8, 8); 500114b4164SWarner Losh cid->prv = mmc_get_bits(raw_cid, 56, 8); 501114b4164SWarner Losh cid->psn = mmc_get_bits(raw_cid, 24, 32); 502114b4164SWarner Losh cid->mdt_year = mmc_get_bits(raw_cid, 12, 8) + 2001; 503114b4164SWarner Losh cid->mdt_month = mmc_get_bits(raw_cid, 8, 4); 504114b4164SWarner Losh } else { 5056fdacf52SWarner Losh /* XXX write me */ 506114b4164SWarner Losh panic("write mmc cid decoder"); 507114b4164SWarner Losh } 508114b4164SWarner Losh } 509114b4164SWarner Losh 510114b4164SWarner Losh static const int exp[8] = { 511114b4164SWarner Losh 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 512114b4164SWarner Losh }; 513114b4164SWarner Losh static const int mant[16] = { 514114b4164SWarner Losh 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 515114b4164SWarner Losh }; 516114b4164SWarner Losh static const int cur_min[8] = { 517114b4164SWarner Losh 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000 518114b4164SWarner Losh }; 519114b4164SWarner Losh static const int cur_max[8] = { 520114b4164SWarner Losh 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000 521114b4164SWarner Losh }; 522114b4164SWarner Losh 523114b4164SWarner Losh static void 524114b4164SWarner Losh mmc_decode_csd(int is_sd, uint32_t *raw_csd, struct mmc_csd *csd) 525114b4164SWarner Losh { 526114b4164SWarner Losh int v; 527114b4164SWarner Losh int m; 528114b4164SWarner Losh int e; 529114b4164SWarner Losh 530114b4164SWarner Losh memset(csd, 0, sizeof(*csd)); 531114b4164SWarner Losh if (is_sd) { 532114b4164SWarner Losh csd->csd_structure = v = mmc_get_bits(raw_csd, 126, 2); 533114b4164SWarner Losh if (v == 0) { 534114b4164SWarner Losh m = mmc_get_bits(raw_csd, 115, 4); 535114b4164SWarner Losh e = mmc_get_bits(raw_csd, 112, 3); 536114b4164SWarner Losh csd->tacc = exp[e] * mant[m] + 9 / 10; 537114b4164SWarner Losh csd->nsac = mmc_get_bits(raw_csd, 104, 8) * 100; 538114b4164SWarner Losh m = mmc_get_bits(raw_csd, 99, 4); 539114b4164SWarner Losh e = mmc_get_bits(raw_csd, 96, 3); 540114b4164SWarner Losh csd->tran_speed = exp[e] * 10000 * mant[m]; 541114b4164SWarner Losh csd->ccc = mmc_get_bits(raw_csd, 84, 12); 542114b4164SWarner Losh csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 80, 4); 543114b4164SWarner Losh csd->read_bl_partial = mmc_get_bits(raw_csd, 79, 1); 544114b4164SWarner Losh csd->write_blk_misalign = mmc_get_bits(raw_csd, 78, 1); 545114b4164SWarner Losh csd->read_blk_misalign = mmc_get_bits(raw_csd, 77, 1); 546114b4164SWarner Losh csd->dsr_imp = mmc_get_bits(raw_csd, 76, 1); 547114b4164SWarner Losh csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 59, 3)]; 548114b4164SWarner Losh csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 56, 3)]; 549114b4164SWarner Losh csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 53, 3)]; 550114b4164SWarner Losh csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 50, 3)]; 551114b4164SWarner Losh m = mmc_get_bits(raw_csd, 62, 12); 552114b4164SWarner Losh e = mmc_get_bits(raw_csd, 47, 3); 553114b4164SWarner Losh csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 554114b4164SWarner Losh csd->erase_blk_en = mmc_get_bits(raw_csd, 46, 1); 555114b4164SWarner Losh csd->sector_size = mmc_get_bits(raw_csd, 39, 7); 556114b4164SWarner Losh csd->wp_grp_size = mmc_get_bits(raw_csd, 32, 7); 557114b4164SWarner Losh csd->wp_grp_enable = mmc_get_bits(raw_csd, 31, 1); 558114b4164SWarner Losh csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 26, 3); 559114b4164SWarner Losh csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 22, 4); 560114b4164SWarner Losh csd->write_bl_partial = mmc_get_bits(raw_csd, 21, 1); 561114b4164SWarner Losh } else if (v == 1) { 562114b4164SWarner Losh panic("Write SDHC CSD parser"); 563114b4164SWarner Losh } else 564114b4164SWarner Losh panic("unknown SD CSD version"); 565114b4164SWarner Losh } else { 566114b4164SWarner Losh panic("Write a MMC CSD parser"); 567114b4164SWarner Losh } 568114b4164SWarner Losh } 569114b4164SWarner Losh 570114b4164SWarner Losh static int 571114b4164SWarner Losh mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid) 572114b4164SWarner Losh { 573114b4164SWarner Losh struct mmc_command cmd; 574114b4164SWarner Losh int err; 575114b4164SWarner Losh 576114b4164SWarner Losh cmd.opcode = MMC_ALL_SEND_CID; 577114b4164SWarner Losh cmd.arg = 0; 578114b4164SWarner Losh cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 579114b4164SWarner Losh err = mmc_wait_for_cmd(sc, &cmd, 0); 580114b4164SWarner Losh memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 581114b4164SWarner Losh return (err); 582114b4164SWarner Losh } 583114b4164SWarner Losh 584114b4164SWarner Losh static int 585114b4164SWarner Losh mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcid) 586114b4164SWarner Losh { 587114b4164SWarner Losh struct mmc_command cmd; 588114b4164SWarner Losh int err; 589114b4164SWarner Losh 590114b4164SWarner Losh cmd.opcode = MMC_SEND_CSD; 591114b4164SWarner Losh cmd.arg = rca << 16; 592114b4164SWarner Losh cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 593114b4164SWarner Losh err = mmc_wait_for_cmd(sc, &cmd, 0); 594114b4164SWarner Losh memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 595114b4164SWarner Losh return (err); 596114b4164SWarner Losh } 597114b4164SWarner Losh 598114b4164SWarner Losh static int 599114b4164SWarner Losh mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp) 600114b4164SWarner Losh { 601114b4164SWarner Losh struct mmc_command cmd; 602114b4164SWarner Losh int err; 603114b4164SWarner Losh 604114b4164SWarner Losh cmd.opcode = SD_SEND_RELATIVE_ADDR; 605114b4164SWarner Losh cmd.arg = 0; 606114b4164SWarner Losh cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 607114b4164SWarner Losh err = mmc_wait_for_cmd(sc, &cmd, 0); 608114b4164SWarner Losh *resp = cmd.resp[0]; 609114b4164SWarner Losh return (err); 610114b4164SWarner Losh } 611114b4164SWarner Losh 612114b4164SWarner Losh static void 613114b4164SWarner Losh mmc_discover_cards(struct mmc_softc *sc) 614114b4164SWarner Losh { 615114b4164SWarner Losh struct mmc_ivars *ivar; 616114b4164SWarner Losh int err; 617114b4164SWarner Losh uint32_t resp; 618114b4164SWarner Losh device_t child; 619114b4164SWarner Losh 620114b4164SWarner Losh while (1) { 621114b4164SWarner Losh ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF, M_WAITOK); 6220e1466acSWarner Losh if (!ivar) 6230e1466acSWarner Losh return; 624114b4164SWarner Losh err = mmc_all_send_cid(sc, ivar->raw_cid); 625114b4164SWarner Losh if (err == MMC_ERR_TIMEOUT) 626114b4164SWarner Losh break; 627114b4164SWarner Losh if (err != MMC_ERR_NONE) { 628114b4164SWarner Losh printf("Error reading CID %d\n", err); 629114b4164SWarner Losh break; 630114b4164SWarner Losh } 631114b4164SWarner Losh if (mmcbr_get_mode(sc->dev) == mode_sd) { 632114b4164SWarner Losh ivar->mode = mode_sd; 633114b4164SWarner Losh mmc_decode_cid(1, ivar->raw_cid, &ivar->cid); 634114b4164SWarner Losh mmc_send_relative_addr(sc, &resp); 635114b4164SWarner Losh ivar->rca = resp >> 16; 63640fab6e7SWarner Losh if (mmcbr_get_ro(sc->dev)) 63740fab6e7SWarner Losh ivar->read_only = 1; 638114b4164SWarner Losh mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 639114b4164SWarner Losh mmc_decode_csd(1, ivar->raw_csd, &ivar->csd); 6400e1466acSWarner Losh printf("SD CARD: %lld bytes\n", (long long) 6410e1466acSWarner Losh ivar->csd.capacity); 642114b4164SWarner Losh child = device_add_child(sc->dev, NULL, -1); 643114b4164SWarner Losh device_set_ivars(child, ivar); 6440e1466acSWarner Losh return; 645114b4164SWarner Losh } 646114b4164SWarner Losh panic("Write MMC card code here"); 647114b4164SWarner Losh } 6480e1466acSWarner Losh free(ivar, M_DEVBUF); 649114b4164SWarner Losh } 650114b4164SWarner Losh 651114b4164SWarner Losh static void 652114b4164SWarner Losh mmc_go_discovery(struct mmc_softc *sc) 653114b4164SWarner Losh { 654114b4164SWarner Losh uint32_t ocr; 655114b4164SWarner Losh device_t dev; 656114b4164SWarner Losh 657114b4164SWarner Losh dev = sc->dev; 658114b4164SWarner Losh if (mmcbr_get_power_mode(dev) != power_on) { 6596fdacf52SWarner Losh /* 6606fdacf52SWarner Losh * First, try SD modes 6616fdacf52SWarner Losh */ 662114b4164SWarner Losh mmcbr_set_mode(dev, mode_sd); 663114b4164SWarner Losh mmc_power_up(sc); 664114b4164SWarner Losh mmcbr_set_bus_mode(dev, pushpull); 665114b4164SWarner Losh mmc_idle_cards(sc); 666114b4164SWarner Losh if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 6676fdacf52SWarner Losh /* 6686fdacf52SWarner Losh * Failed, try MMC 6696fdacf52SWarner Losh */ 670114b4164SWarner Losh mmcbr_set_mode(dev, mode_mmc); 671114b4164SWarner Losh if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) 6726fdacf52SWarner Losh return; /* Failed both, punt! XXX powerdown? */ 673114b4164SWarner Losh } 674114b4164SWarner Losh mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr)); 675114b4164SWarner Losh if (mmcbr_get_ocr(dev) != 0) 676114b4164SWarner Losh mmc_idle_cards(sc); 677114b4164SWarner Losh } else { 678114b4164SWarner Losh mmcbr_set_bus_mode(dev, opendrain); 679114b4164SWarner Losh mmcbr_set_clock(dev, mmcbr_get_f_min(dev)); 680114b4164SWarner Losh mmcbr_update_ios(dev); 6816fdacf52SWarner Losh /* XXX recompute vdd based on new cards? */ 682114b4164SWarner Losh } 683114b4164SWarner Losh /* 684114b4164SWarner Losh * Make sure that we have a mutually agreeable voltage to at least 685114b4164SWarner Losh * one card on the bus. 686114b4164SWarner Losh */ 687114b4164SWarner Losh if (mmcbr_get_ocr(dev) == 0) 688114b4164SWarner Losh return; 689114b4164SWarner Losh /* 690114b4164SWarner Losh * Reselect the cards after we've idled them above. 691114b4164SWarner Losh */ 692114b4164SWarner Losh if (mmcbr_get_mode(dev) == mode_sd) 693114b4164SWarner Losh mmc_send_app_op_cond(sc, mmcbr_get_ocr(dev), NULL); 694114b4164SWarner Losh else 695114b4164SWarner Losh mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL); 696114b4164SWarner Losh mmc_discover_cards(sc); 697114b4164SWarner Losh 698114b4164SWarner Losh mmcbr_set_bus_mode(dev, pushpull); 699114b4164SWarner Losh mmcbr_update_ios(dev); 700114b4164SWarner Losh bus_generic_attach(dev); 7016fdacf52SWarner Losh /* mmc_update_children_sysctl(dev);*/ 702114b4164SWarner Losh } 703114b4164SWarner Losh 704114b4164SWarner Losh static int 705114b4164SWarner Losh mmc_calculate_clock(struct mmc_softc *sc) 706114b4164SWarner Losh { 707114b4164SWarner Losh int max_dtr = 0; 708114b4164SWarner Losh int nkid, i, f_min, f_max; 709114b4164SWarner Losh device_t *kids; 710114b4164SWarner Losh 711114b4164SWarner Losh f_min = mmcbr_get_f_min(sc->dev); 712114b4164SWarner Losh f_max = mmcbr_get_f_max(sc->dev); 713114b4164SWarner Losh max_dtr = f_max; 714114b4164SWarner Losh if (device_get_children(sc->dev, &kids, &nkid) != 0) 715114b4164SWarner Losh panic("can't get children"); 716114b4164SWarner Losh for (i = 0; i < nkid; i++) 717114b4164SWarner Losh if (mmc_get_tran_speed(kids[i]) < max_dtr) 718114b4164SWarner Losh max_dtr = mmc_get_tran_speed(kids[i]); 719114b4164SWarner Losh free(kids, M_TEMP); 720114b4164SWarner Losh device_printf(sc->dev, "setting transfer rate to %d.%03dMHz\n", 721114b4164SWarner Losh max_dtr / 1000000, (max_dtr / 1000) % 1000); 722114b4164SWarner Losh return max_dtr; 723114b4164SWarner Losh } 724114b4164SWarner Losh 725114b4164SWarner Losh static void 726114b4164SWarner Losh mmc_scan(struct mmc_softc *sc) 727114b4164SWarner Losh { 728114b4164SWarner Losh device_t dev; 729114b4164SWarner Losh 730114b4164SWarner Losh dev = sc->dev; 731114b4164SWarner Losh mmc_acquire_bus(dev, dev); 732114b4164SWarner Losh 733114b4164SWarner Losh if (mmcbr_get_power_mode(dev) == power_on) 734114b4164SWarner Losh mmc_rescan_cards(sc); 735114b4164SWarner Losh mmc_go_discovery(sc); 736114b4164SWarner Losh mmcbr_set_clock(dev, mmc_calculate_clock(sc)); 737114b4164SWarner Losh mmcbr_update_ios(dev); 738114b4164SWarner Losh 739114b4164SWarner Losh mmc_release_bus(dev, dev); 7406fdacf52SWarner Losh /* XXX probe/attach/detach children? */ 741114b4164SWarner Losh } 742114b4164SWarner Losh 743114b4164SWarner Losh static int 744114b4164SWarner Losh mmc_read_ivar(device_t bus, device_t child, int which, u_char *result) 745114b4164SWarner Losh { 746114b4164SWarner Losh struct mmc_ivars *ivar = device_get_ivars(child); 747114b4164SWarner Losh 748114b4164SWarner Losh switch (which) { 749114b4164SWarner Losh default: 750114b4164SWarner Losh return (EINVAL); 751114b4164SWarner Losh case MMC_IVAR_DSR_IMP: 752114b4164SWarner Losh *(int *)result = ivar->csd.dsr_imp; 753114b4164SWarner Losh break; 754114b4164SWarner Losh case MMC_IVAR_MEDIA_SIZE: 755114b4164SWarner Losh *(int *)result = ivar->csd.capacity; 756114b4164SWarner Losh break; 757114b4164SWarner Losh case MMC_IVAR_RCA: 758114b4164SWarner Losh *(int *)result = ivar->rca; 759114b4164SWarner Losh break; 760114b4164SWarner Losh case MMC_IVAR_SECTOR_SIZE: 761114b4164SWarner Losh *(int *)result = 512; 762114b4164SWarner Losh break; 763114b4164SWarner Losh case MMC_IVAR_TRAN_SPEED: 764114b4164SWarner Losh *(int *)result = ivar->csd.tran_speed; 765114b4164SWarner Losh break; 76640fab6e7SWarner Losh case MMC_IVAR_READ_ONLY: 76740fab6e7SWarner Losh *(int *)result = ivar->read_only; 76840fab6e7SWarner Losh break; 769114b4164SWarner Losh } 770114b4164SWarner Losh return (0); 771114b4164SWarner Losh } 772114b4164SWarner Losh 773114b4164SWarner Losh static int 774114b4164SWarner Losh mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 775114b4164SWarner Losh { 7766fdacf52SWarner Losh /* 7776fdacf52SWarner Losh * None are writable ATM 7786fdacf52SWarner Losh */ 779114b4164SWarner Losh return (EINVAL); 780114b4164SWarner Losh } 781114b4164SWarner Losh 782114b4164SWarner Losh 783114b4164SWarner Losh static void 784114b4164SWarner Losh mmc_delayed_attach(void *xsc) 785114b4164SWarner Losh { 786114b4164SWarner Losh struct mmc_softc *sc = xsc; 787114b4164SWarner Losh 788114b4164SWarner Losh mmc_scan(sc); 789114b4164SWarner Losh config_intrhook_disestablish(&sc->config_intrhook); 790114b4164SWarner Losh } 791114b4164SWarner Losh 792114b4164SWarner Losh static device_method_t mmc_methods[] = { 793114b4164SWarner Losh /* device_if */ 794114b4164SWarner Losh DEVMETHOD(device_probe, mmc_probe), 795114b4164SWarner Losh DEVMETHOD(device_attach, mmc_attach), 796114b4164SWarner Losh DEVMETHOD(device_detach, mmc_detach), 797114b4164SWarner Losh 798114b4164SWarner Losh /* Bus interface */ 799114b4164SWarner Losh DEVMETHOD(bus_read_ivar, mmc_read_ivar), 800114b4164SWarner Losh DEVMETHOD(bus_write_ivar, mmc_write_ivar), 801114b4164SWarner Losh 802114b4164SWarner Losh /* MMC Bus interface */ 803114b4164SWarner Losh DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request), 804114b4164SWarner Losh DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus), 805114b4164SWarner Losh DEVMETHOD(mmcbus_release_bus, mmc_release_bus), 806114b4164SWarner Losh 807114b4164SWarner Losh {0, 0}, 808114b4164SWarner Losh }; 809114b4164SWarner Losh 810114b4164SWarner Losh static driver_t mmc_driver = { 811114b4164SWarner Losh "mmc", 812114b4164SWarner Losh mmc_methods, 813114b4164SWarner Losh sizeof(struct mmc_softc), 814114b4164SWarner Losh }; 815114b4164SWarner Losh static devclass_t mmc_devclass; 816114b4164SWarner Losh 817114b4164SWarner Losh 818114b4164SWarner Losh DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, 0, 0); 8190e1466acSWarner Losh DRIVER_MODULE(mmc, sdh, mmc_driver, mmc_devclass, 0, 0); 820