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. 24114b4164SWarner Losh */ 25114b4164SWarner Losh 26114b4164SWarner Losh #include <sys/cdefs.h> 27114b4164SWarner Losh __FBSDID("$FreeBSD$"); 28114b4164SWarner Losh 29114b4164SWarner Losh #include <sys/param.h> 30114b4164SWarner Losh #include <sys/systm.h> 31114b4164SWarner Losh #include <sys/kernel.h> 32114b4164SWarner Losh #include <sys/malloc.h> 33114b4164SWarner Losh #include <sys/lock.h> 34114b4164SWarner Losh #include <sys/module.h> 35114b4164SWarner Losh #include <sys/mutex.h> 36114b4164SWarner Losh #include <sys/bus.h> 37114b4164SWarner Losh 38114b4164SWarner Losh #include <dev/mmc/mmcreg.h> 39114b4164SWarner Losh #include <dev/mmc/mmcbrvar.h> 40114b4164SWarner Losh #include <dev/mmc/mmcvar.h> 41114b4164SWarner Losh #include "mmcbr_if.h" 42114b4164SWarner Losh #include "mmcbus_if.h" 43114b4164SWarner Losh 44114b4164SWarner Losh struct mmc_softc { 45114b4164SWarner Losh device_t dev; 46114b4164SWarner Losh struct mtx sc_mtx; 47114b4164SWarner Losh struct intr_config_hook config_intrhook; 48114b4164SWarner Losh device_t owner; 49114b4164SWarner Losh uint32_t last_rca; 50114b4164SWarner Losh }; 51114b4164SWarner Losh 52114b4164SWarner Losh /* 53114b4164SWarner Losh * Per-card data 54114b4164SWarner Losh */ 55114b4164SWarner Losh struct mmc_ivars { 56114b4164SWarner Losh uint32_t raw_cid[4]; /* Raw bits of the CID */ 57114b4164SWarner Losh uint32_t raw_csd[4]; /* Raw bits of the CSD */ 58114b4164SWarner Losh uint16_t rca; 59114b4164SWarner Losh enum mmc_card_mode mode; 60114b4164SWarner Losh struct mmc_cid cid; /* cid decoded */ 61114b4164SWarner Losh struct mmc_csd csd; /* csd decoded */ 62114b4164SWarner Losh }; 63114b4164SWarner Losh 64114b4164SWarner Losh #define CMD_RETRIES 3 65114b4164SWarner Losh 66114b4164SWarner Losh /* bus entry points */ 67114b4164SWarner Losh static int mmc_probe(device_t dev); 68114b4164SWarner Losh static int mmc_attach(device_t dev); 69114b4164SWarner Losh static int mmc_detach(device_t dev); 70114b4164SWarner Losh 71114b4164SWarner Losh #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 72114b4164SWarner Losh #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 73114b4164SWarner Losh #define MMC_LOCK_INIT(_sc) \ 74114b4164SWarner Losh mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 75114b4164SWarner Losh "mmc", MTX_DEF) 76114b4164SWarner Losh #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 77114b4164SWarner Losh #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 78114b4164SWarner Losh #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 79114b4164SWarner Losh 80114b4164SWarner Losh static void mmc_delayed_attach(void *); 81114b4164SWarner Losh static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, 82114b4164SWarner Losh int retries); 83114b4164SWarner Losh static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 84114b4164SWarner Losh uint32_t arg, uint32_t flags, uint32_t *resp, int retries); 85114b4164SWarner Losh 86114b4164SWarner Losh static void 87114b4164SWarner Losh mmc_ms_delay(int ms) 88114b4164SWarner Losh { 89114b4164SWarner Losh DELAY(1000 * ms); /* XXX BAD */ 90114b4164SWarner Losh } 91114b4164SWarner Losh 92114b4164SWarner Losh static int 93114b4164SWarner Losh mmc_probe(device_t dev) 94114b4164SWarner Losh { 95114b4164SWarner Losh 96114b4164SWarner Losh device_set_desc(dev, "mmc/sd bus"); 97114b4164SWarner Losh return (0); 98114b4164SWarner Losh } 99114b4164SWarner Losh 100114b4164SWarner Losh static int 101114b4164SWarner Losh mmc_attach(device_t dev) 102114b4164SWarner Losh { 103114b4164SWarner Losh struct mmc_softc *sc; 104114b4164SWarner Losh 105114b4164SWarner Losh sc = device_get_softc(dev); 106114b4164SWarner Losh sc->dev = dev; 107114b4164SWarner Losh MMC_LOCK_INIT(sc); 108114b4164SWarner Losh 109114b4164SWarner Losh /* We'll probe and attach our children later, but before / mount */ 110114b4164SWarner Losh sc->config_intrhook.ich_func = mmc_delayed_attach; 111114b4164SWarner Losh sc->config_intrhook.ich_arg = sc; 112114b4164SWarner Losh if (config_intrhook_establish(&sc->config_intrhook) != 0) 113114b4164SWarner Losh device_printf(dev, "config_intrhook_establish failed\n"); 114114b4164SWarner Losh return (0); 115114b4164SWarner Losh } 116114b4164SWarner Losh 117114b4164SWarner Losh static int 118114b4164SWarner Losh mmc_detach(device_t dev) 119114b4164SWarner Losh { 120114b4164SWarner Losh return (EBUSY); /* XXX */ 121114b4164SWarner Losh } 122114b4164SWarner Losh 123114b4164SWarner Losh static int 124114b4164SWarner Losh mmc_acquire_bus(device_t busdev, device_t dev) 125114b4164SWarner Losh { 126114b4164SWarner Losh struct mmc_softc *sc; 127114b4164SWarner Losh int err; 128114b4164SWarner Losh int rca; 129114b4164SWarner Losh 130114b4164SWarner Losh err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), dev); 131114b4164SWarner Losh if (err) 132114b4164SWarner Losh return (err); 133114b4164SWarner Losh sc = device_get_softc(busdev); 134114b4164SWarner Losh MMC_LOCK(sc); 135114b4164SWarner Losh if (sc->owner) 136114b4164SWarner Losh panic("mmc: host bridge didn't seralize us."); 137114b4164SWarner Losh sc->owner = dev; 138114b4164SWarner Losh MMC_UNLOCK(sc); 139114b4164SWarner Losh 140114b4164SWarner Losh if (busdev != dev) { 141114b4164SWarner Losh // Keep track of the last rca that we've selected. If 142114b4164SWarner Losh // we're asked to do it again, don't. We never unselect 143114b4164SWarner Losh // unless the bus code itself wants the mmc bus. 144114b4164SWarner Losh rca = mmc_get_rca(dev); 145114b4164SWarner Losh if (sc->last_rca != rca) { 146114b4164SWarner Losh mmc_wait_for_command(sc, MMC_SELECT_CARD, rca << 16, 147114b4164SWarner Losh MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES); 148114b4164SWarner Losh sc->last_rca = rca; 149114b4164SWarner Losh } 150114b4164SWarner Losh // XXX should set bus width here? 151114b4164SWarner Losh } else { 152114b4164SWarner Losh // If there's a card selected, stand down. 153114b4164SWarner Losh if (sc->last_rca != 0) { 154114b4164SWarner Losh mmc_wait_for_command(sc, MMC_SELECT_CARD, 0, 155114b4164SWarner Losh MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES); 156114b4164SWarner Losh sc->last_rca = 0; 157114b4164SWarner Losh } 158114b4164SWarner Losh // XXX should set bus width here? 159114b4164SWarner Losh } 160114b4164SWarner Losh 161114b4164SWarner Losh return (0); 162114b4164SWarner Losh } 163114b4164SWarner Losh 164114b4164SWarner Losh static int 165114b4164SWarner Losh mmc_release_bus(device_t busdev, device_t dev) 166114b4164SWarner Losh { 167114b4164SWarner Losh struct mmc_softc *sc; 168114b4164SWarner Losh int err; 169114b4164SWarner Losh 170114b4164SWarner Losh sc = device_get_softc(busdev); 171114b4164SWarner Losh 172114b4164SWarner Losh MMC_LOCK(sc); 173114b4164SWarner Losh if (!sc->owner) 174114b4164SWarner Losh panic("mmc: releasing unowned bus."); 175114b4164SWarner Losh if (sc->owner != dev) 176114b4164SWarner Losh panic("mmc: you don't own the bus. game over."); 177114b4164SWarner Losh MMC_UNLOCK(sc); 178114b4164SWarner Losh err = MMCBR_RELEASE_HOST(device_get_parent(busdev), dev); 179114b4164SWarner Losh if (err) 180114b4164SWarner Losh return (err); 181114b4164SWarner Losh MMC_LOCK(sc); 182114b4164SWarner Losh sc->owner = NULL; 183114b4164SWarner Losh MMC_UNLOCK(sc); 184114b4164SWarner Losh return (0); 185114b4164SWarner Losh } 186114b4164SWarner Losh 187114b4164SWarner Losh static void 188114b4164SWarner Losh mmc_rescan_cards(struct mmc_softc *sc) 189114b4164SWarner Losh { 190114b4164SWarner Losh /* XXX: Look at the children and see if they respond to status */ 191114b4164SWarner Losh } 192114b4164SWarner Losh 193114b4164SWarner Losh static uint32_t 194114b4164SWarner Losh mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr) 195114b4164SWarner Losh { 196114b4164SWarner Losh // XXX 197114b4164SWarner Losh return ocr; 198114b4164SWarner Losh } 199114b4164SWarner Losh 200114b4164SWarner Losh static int 201114b4164SWarner Losh mmc_highest_voltage(uint32_t ocr) 202114b4164SWarner Losh { 203114b4164SWarner Losh int i; 204114b4164SWarner Losh 205114b4164SWarner Losh for (i = 30; i >= 0; i--) 206114b4164SWarner Losh if (ocr & (1 << i)) 207114b4164SWarner Losh return i; 208114b4164SWarner Losh return (-1); 209114b4164SWarner Losh } 210114b4164SWarner Losh 211114b4164SWarner Losh static void 212114b4164SWarner Losh mmc_wakeup(struct mmc_request *req) 213114b4164SWarner Losh { 214114b4164SWarner Losh struct mmc_softc *sc; 215114b4164SWarner Losh 216114b4164SWarner Losh // printf("Wakeup for req %p done_data %p\n", req, req->done_data); 217114b4164SWarner Losh sc = (struct mmc_softc *)req->done_data; 218114b4164SWarner Losh MMC_LOCK(sc); 219114b4164SWarner Losh req->flags |= MMC_REQ_DONE; 220114b4164SWarner Losh wakeup(req); 221114b4164SWarner Losh MMC_UNLOCK(sc); 222114b4164SWarner Losh } 223114b4164SWarner Losh 224114b4164SWarner Losh static int 225114b4164SWarner Losh mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req) 226114b4164SWarner Losh { 227114b4164SWarner Losh int err; 228114b4164SWarner Losh 229114b4164SWarner Losh req->done = mmc_wakeup; 230114b4164SWarner Losh req->done_data = sc; 231114b4164SWarner Losh // printf("Submitting request %p sc %p\n", req, sc); 232114b4164SWarner Losh MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req); 233114b4164SWarner Losh MMC_LOCK(sc); 234114b4164SWarner Losh do { 235114b4164SWarner Losh err = msleep(req, &sc->sc_mtx, PZERO | PCATCH, "mmcreq", 236114b4164SWarner Losh hz / 10); 237114b4164SWarner Losh } while (!(req->flags & MMC_REQ_DONE) && err == EAGAIN); 238114b4164SWarner Losh // printf("Request %p done with error %d\n", req, err); 239114b4164SWarner Losh MMC_UNLOCK(sc); 240114b4164SWarner Losh return (err); 241114b4164SWarner Losh } 242114b4164SWarner Losh 243114b4164SWarner Losh static int 244114b4164SWarner Losh mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req) 245114b4164SWarner Losh { 246114b4164SWarner Losh struct mmc_softc *sc = device_get_softc(brdev); 247114b4164SWarner Losh 248114b4164SWarner Losh return mmc_wait_for_req(sc, req); 249114b4164SWarner Losh } 250114b4164SWarner Losh 251114b4164SWarner Losh static int 252114b4164SWarner Losh mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries) 253114b4164SWarner Losh { 254114b4164SWarner Losh struct mmc_request mreq; 255114b4164SWarner Losh 256114b4164SWarner Losh memset(&mreq, 0, sizeof(mreq)); 257114b4164SWarner Losh memset(cmd->resp, 0, sizeof(cmd->resp)); 258114b4164SWarner Losh cmd->retries = retries; 259114b4164SWarner Losh cmd->data = NULL; 260114b4164SWarner Losh mreq.cmd = cmd; 261114b4164SWarner Losh // printf("CMD: %x ARG %x\n", cmd->opcode, cmd->arg); 262114b4164SWarner Losh mmc_wait_for_req(sc, &mreq); 263114b4164SWarner Losh return (cmd->error); 264114b4164SWarner Losh } 265114b4164SWarner Losh 266114b4164SWarner Losh static int 267114b4164SWarner Losh mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca, 268114b4164SWarner Losh struct mmc_command *cmd, int retries) 269114b4164SWarner Losh { 270114b4164SWarner Losh struct mmc_command appcmd; 271114b4164SWarner Losh int err = MMC_ERR_NONE, i; 272114b4164SWarner Losh 273114b4164SWarner Losh for (i = 0; i <= retries; i++) { 274114b4164SWarner Losh appcmd.opcode = MMC_APP_CMD; 275114b4164SWarner Losh appcmd.arg = rca << 16; 276114b4164SWarner Losh appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 277114b4164SWarner Losh mmc_wait_for_cmd(sc, &appcmd, 0); 278114b4164SWarner Losh err = appcmd.error; 279114b4164SWarner Losh if (err != MMC_ERR_NONE) 280114b4164SWarner Losh continue; 281114b4164SWarner Losh if (!(appcmd.resp[0] & R1_APP_CMD)) 282114b4164SWarner Losh return MMC_ERR_FAILED; 283114b4164SWarner Losh mmc_wait_for_cmd(sc, cmd, 0); 284114b4164SWarner Losh err = cmd->error; 285114b4164SWarner Losh if (err == MMC_ERR_NONE) 286114b4164SWarner Losh break; 287114b4164SWarner Losh } 288114b4164SWarner Losh return (err); 289114b4164SWarner Losh } 290114b4164SWarner Losh 291114b4164SWarner Losh static int 292114b4164SWarner Losh mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 293114b4164SWarner Losh uint32_t arg, uint32_t flags, uint32_t *resp, int retries) 294114b4164SWarner Losh { 295114b4164SWarner Losh struct mmc_command cmd; 296114b4164SWarner Losh int err; 297114b4164SWarner Losh 298114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 299114b4164SWarner Losh cmd.opcode = opcode; 300114b4164SWarner Losh cmd.arg = arg; 301114b4164SWarner Losh cmd.flags = flags; 302114b4164SWarner Losh err = mmc_wait_for_cmd(sc, &cmd, retries); 303114b4164SWarner Losh if (err) 304114b4164SWarner Losh return (err); 305114b4164SWarner Losh if (cmd.error) 306114b4164SWarner Losh return (cmd.error); 307114b4164SWarner Losh if (resp) { 308114b4164SWarner Losh if (flags & MMC_RSP_136) 309114b4164SWarner Losh memcpy(resp, cmd.resp, 4 * sizeof(uint32_t)); 310114b4164SWarner Losh else 311114b4164SWarner Losh *resp = cmd.resp[0]; 312114b4164SWarner Losh } 313114b4164SWarner Losh return (0); 314114b4164SWarner Losh } 315114b4164SWarner Losh 316114b4164SWarner Losh static void 317114b4164SWarner Losh mmc_idle_cards(struct mmc_softc *sc) 318114b4164SWarner Losh { 319114b4164SWarner Losh device_t dev; 320114b4164SWarner Losh struct mmc_command cmd; 321114b4164SWarner Losh 322114b4164SWarner Losh dev = sc->dev; 323114b4164SWarner Losh mmcbr_set_chip_select(dev, cs_high); 324114b4164SWarner Losh mmcbr_update_ios(dev); 325114b4164SWarner Losh mmc_ms_delay(1); 326114b4164SWarner Losh 327114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 328114b4164SWarner Losh cmd.opcode = MMC_GO_IDLE_STATE; 329114b4164SWarner Losh cmd.arg = 0; 330114b4164SWarner Losh cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 331114b4164SWarner Losh mmc_wait_for_cmd(sc, &cmd, 0); 332114b4164SWarner Losh mmc_ms_delay(1); 333114b4164SWarner Losh 334114b4164SWarner Losh mmcbr_set_chip_select(dev, cs_dontcare); 335114b4164SWarner Losh mmcbr_update_ios(dev); 336114b4164SWarner Losh mmc_ms_delay(1); 337114b4164SWarner Losh } 338114b4164SWarner Losh 339114b4164SWarner Losh static int 340114b4164SWarner Losh mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 341114b4164SWarner Losh { 342114b4164SWarner Losh struct mmc_command cmd; 343114b4164SWarner Losh int err = MMC_ERR_NONE, i; 344114b4164SWarner Losh 345114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 346114b4164SWarner Losh cmd.opcode = ACMD_SD_SEND_OP_COND; 347114b4164SWarner Losh cmd.arg = ocr; 348114b4164SWarner Losh cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 349114b4164SWarner Losh 350114b4164SWarner Losh for (i = 0; i < 10; i++) { 351114b4164SWarner Losh err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES); 352114b4164SWarner Losh if (err != MMC_ERR_NONE) 353114b4164SWarner Losh break; 354114b4164SWarner Losh if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || ocr == 0) 355114b4164SWarner Losh break; 356114b4164SWarner Losh err = MMC_ERR_TIMEOUT; 357114b4164SWarner Losh mmc_ms_delay(10); 358114b4164SWarner Losh } 359114b4164SWarner Losh if (rocr && err == MMC_ERR_NONE) 360114b4164SWarner Losh *rocr = cmd.resp[0]; 361114b4164SWarner Losh return err; 362114b4164SWarner Losh } 363114b4164SWarner Losh 364114b4164SWarner Losh static int 365114b4164SWarner Losh mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 366114b4164SWarner Losh { 367114b4164SWarner Losh struct mmc_command cmd; 368114b4164SWarner Losh int err = MMC_ERR_NONE, i; 369114b4164SWarner Losh 370114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 371114b4164SWarner Losh cmd.opcode = MMC_SEND_OP_COND; 372114b4164SWarner Losh cmd.arg = ocr; 373114b4164SWarner Losh cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 374114b4164SWarner Losh 375114b4164SWarner Losh for (i = 0; i < 100; i++) { 376114b4164SWarner Losh err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 377114b4164SWarner Losh if (err != MMC_ERR_NONE) 378114b4164SWarner Losh break; 379114b4164SWarner Losh if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || ocr == 0) 380114b4164SWarner Losh break; 381114b4164SWarner Losh err = MMC_ERR_TIMEOUT; 382114b4164SWarner Losh mmc_ms_delay(10); 383114b4164SWarner Losh } 384114b4164SWarner Losh if (rocr && err == MMC_ERR_NONE) 385114b4164SWarner Losh *rocr = cmd.resp[0]; 386114b4164SWarner Losh return err; 387114b4164SWarner Losh } 388114b4164SWarner Losh 389114b4164SWarner Losh static void 390114b4164SWarner Losh mmc_power_up(struct mmc_softc *sc) 391114b4164SWarner Losh { 392114b4164SWarner Losh device_t dev; 393114b4164SWarner Losh 394114b4164SWarner Losh dev = sc->dev; 395114b4164SWarner Losh mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev))); 396114b4164SWarner Losh mmcbr_set_bus_mode(dev, opendrain); 397114b4164SWarner Losh mmcbr_set_chip_select(dev, cs_dontcare); 398114b4164SWarner Losh mmcbr_set_bus_width(dev, bus_width_1); 399114b4164SWarner Losh mmcbr_set_power_mode(dev, power_up); 400114b4164SWarner Losh mmcbr_set_clock(dev, 0); 401114b4164SWarner Losh mmcbr_update_ios(dev); 402114b4164SWarner Losh mmc_ms_delay(1); 403114b4164SWarner Losh 404114b4164SWarner Losh mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev)); 405114b4164SWarner Losh mmcbr_set_power_mode(dev, power_on); 406114b4164SWarner Losh mmcbr_update_ios(dev); 407114b4164SWarner Losh mmc_ms_delay(2); 408114b4164SWarner Losh } 409114b4164SWarner Losh 410114b4164SWarner Losh // I wonder if the following is endian safe. 411114b4164SWarner Losh static uint32_t 412114b4164SWarner Losh mmc_get_bits(uint32_t *bits, int start, int size) 413114b4164SWarner Losh { 414114b4164SWarner Losh const int i = 3 - (start / 32); 415114b4164SWarner Losh const int shift = start & 31; 416114b4164SWarner Losh uint32_t retval = bits[i] >> shift; 417114b4164SWarner Losh if (size + shift > 32) 418114b4164SWarner Losh retval |= bits[i - 1] << (32 - shift); 419114b4164SWarner Losh return retval & ((1 << size) - 1); 420114b4164SWarner Losh } 421114b4164SWarner Losh 422114b4164SWarner Losh static void 423114b4164SWarner Losh mmc_decode_cid(int is_sd, uint32_t *raw_cid, struct mmc_cid *cid) 424114b4164SWarner Losh { 425114b4164SWarner Losh int i; 426114b4164SWarner Losh 427114b4164SWarner Losh memset(cid, 0, sizeof(*cid)); 428114b4164SWarner Losh if (is_sd) { 429114b4164SWarner Losh /* There's no version info, so we take it on faith */ 430114b4164SWarner Losh cid->mid = mmc_get_bits(raw_cid, 120, 8); 431114b4164SWarner Losh cid->oid = mmc_get_bits(raw_cid, 104, 16); 432114b4164SWarner Losh for (i = 0; i < 5; i++) 433114b4164SWarner Losh cid->pnm[i] = mmc_get_bits(raw_cid, 96 - i * 8, 8); 434114b4164SWarner Losh cid->prv = mmc_get_bits(raw_cid, 56, 8); 435114b4164SWarner Losh cid->psn = mmc_get_bits(raw_cid, 24, 32); 436114b4164SWarner Losh cid->mdt_year = mmc_get_bits(raw_cid, 12, 8) + 2001; 437114b4164SWarner Losh cid->mdt_month = mmc_get_bits(raw_cid, 8, 4); 438114b4164SWarner Losh } else { 439114b4164SWarner Losh // XXX write me 440114b4164SWarner Losh panic("write mmc cid decoder"); 441114b4164SWarner Losh } 442114b4164SWarner Losh } 443114b4164SWarner Losh 444114b4164SWarner Losh static const int exp[8] = { 445114b4164SWarner Losh 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 446114b4164SWarner Losh }; 447114b4164SWarner Losh static const int mant[16] = { 448114b4164SWarner Losh 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 449114b4164SWarner Losh }; 450114b4164SWarner Losh static const int cur_min[8] = { 451114b4164SWarner Losh 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000 452114b4164SWarner Losh }; 453114b4164SWarner Losh static const int cur_max[8] = { 454114b4164SWarner Losh 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000 455114b4164SWarner Losh }; 456114b4164SWarner Losh 457114b4164SWarner Losh static void 458114b4164SWarner Losh mmc_decode_csd(int is_sd, uint32_t *raw_csd, struct mmc_csd *csd) 459114b4164SWarner Losh { 460114b4164SWarner Losh int v; 461114b4164SWarner Losh int m; 462114b4164SWarner Losh int e; 463114b4164SWarner Losh 464114b4164SWarner Losh memset(csd, 0, sizeof(*csd)); 465114b4164SWarner Losh if (is_sd) { 466114b4164SWarner Losh csd->csd_structure = v = mmc_get_bits(raw_csd, 126, 2); 467114b4164SWarner Losh if (v == 0) { 468114b4164SWarner Losh m = mmc_get_bits(raw_csd, 115, 4); 469114b4164SWarner Losh e = mmc_get_bits(raw_csd, 112, 3); 470114b4164SWarner Losh csd->tacc = exp[e] * mant[m] + 9 / 10; 471114b4164SWarner Losh csd->nsac = mmc_get_bits(raw_csd, 104, 8) * 100; 472114b4164SWarner Losh m = mmc_get_bits(raw_csd, 99, 4); 473114b4164SWarner Losh e = mmc_get_bits(raw_csd, 96, 3); 474114b4164SWarner Losh csd->tran_speed = exp[e] * 10000 * mant[m]; 475114b4164SWarner Losh csd->ccc = mmc_get_bits(raw_csd, 84, 12); 476114b4164SWarner Losh csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 80, 4); 477114b4164SWarner Losh csd->read_bl_partial = mmc_get_bits(raw_csd, 79, 1); 478114b4164SWarner Losh csd->write_blk_misalign = mmc_get_bits(raw_csd, 78, 1); 479114b4164SWarner Losh csd->read_blk_misalign = mmc_get_bits(raw_csd, 77, 1); 480114b4164SWarner Losh csd->dsr_imp = mmc_get_bits(raw_csd, 76, 1); 481114b4164SWarner Losh csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 59, 3)]; 482114b4164SWarner Losh csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 56, 3)]; 483114b4164SWarner Losh csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 53, 3)]; 484114b4164SWarner Losh csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 50, 3)]; 485114b4164SWarner Losh m = mmc_get_bits(raw_csd, 62, 12); 486114b4164SWarner Losh e = mmc_get_bits(raw_csd, 47, 3); 487114b4164SWarner Losh csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 488114b4164SWarner Losh csd->erase_blk_en = mmc_get_bits(raw_csd, 46, 1); 489114b4164SWarner Losh csd->sector_size = mmc_get_bits(raw_csd, 39, 7); 490114b4164SWarner Losh csd->wp_grp_size = mmc_get_bits(raw_csd, 32, 7); 491114b4164SWarner Losh csd->wp_grp_enable = mmc_get_bits(raw_csd, 31, 1); 492114b4164SWarner Losh csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 26, 3); 493114b4164SWarner Losh csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 22, 4); 494114b4164SWarner Losh csd->write_bl_partial = mmc_get_bits(raw_csd, 21, 1); 495114b4164SWarner Losh } else if (v == 1) { 496114b4164SWarner Losh panic("Write SDHC CSD parser"); 497114b4164SWarner Losh } else 498114b4164SWarner Losh panic("unknown SD CSD version"); 499114b4164SWarner Losh } else { 500114b4164SWarner Losh panic("Write a MMC CSD parser"); 501114b4164SWarner Losh } 502114b4164SWarner Losh } 503114b4164SWarner Losh 504114b4164SWarner Losh static int 505114b4164SWarner Losh mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid) 506114b4164SWarner Losh { 507114b4164SWarner Losh struct mmc_command cmd; 508114b4164SWarner Losh int err; 509114b4164SWarner Losh 510114b4164SWarner Losh cmd.opcode = MMC_ALL_SEND_CID; 511114b4164SWarner Losh cmd.arg = 0; 512114b4164SWarner Losh cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 513114b4164SWarner Losh err = mmc_wait_for_cmd(sc, &cmd, 0); 514114b4164SWarner Losh memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 515114b4164SWarner Losh return (err); 516114b4164SWarner Losh } 517114b4164SWarner Losh 518114b4164SWarner Losh static int 519114b4164SWarner Losh mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcid) 520114b4164SWarner Losh { 521114b4164SWarner Losh struct mmc_command cmd; 522114b4164SWarner Losh int err; 523114b4164SWarner Losh 524114b4164SWarner Losh cmd.opcode = MMC_SEND_CSD; 525114b4164SWarner Losh cmd.arg = rca << 16; 526114b4164SWarner Losh cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 527114b4164SWarner Losh err = mmc_wait_for_cmd(sc, &cmd, 0); 528114b4164SWarner Losh memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 529114b4164SWarner Losh return (err); 530114b4164SWarner Losh } 531114b4164SWarner Losh 532114b4164SWarner Losh static int 533114b4164SWarner Losh mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp) 534114b4164SWarner Losh { 535114b4164SWarner Losh struct mmc_command cmd; 536114b4164SWarner Losh int err; 537114b4164SWarner Losh 538114b4164SWarner Losh cmd.opcode = SD_SEND_RELATIVE_ADDR; 539114b4164SWarner Losh cmd.arg = 0; 540114b4164SWarner Losh cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 541114b4164SWarner Losh err = mmc_wait_for_cmd(sc, &cmd, 0); 542114b4164SWarner Losh *resp = cmd.resp[0]; 543114b4164SWarner Losh return (err); 544114b4164SWarner Losh } 545114b4164SWarner Losh 546114b4164SWarner Losh static void 547114b4164SWarner Losh mmc_discover_cards(struct mmc_softc *sc) 548114b4164SWarner Losh { 549114b4164SWarner Losh struct mmc_ivars *ivar; 550114b4164SWarner Losh int err; 551114b4164SWarner Losh uint32_t resp; 552114b4164SWarner Losh device_t child; 553114b4164SWarner Losh 554114b4164SWarner Losh while (1) { 555114b4164SWarner Losh ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF, M_WAITOK); 556114b4164SWarner Losh err = mmc_all_send_cid(sc, ivar->raw_cid); 557114b4164SWarner Losh if (err == MMC_ERR_TIMEOUT) 558114b4164SWarner Losh break; 559114b4164SWarner Losh if (err != MMC_ERR_NONE) { 560114b4164SWarner Losh printf("Error reading CID %d\n", err); 561114b4164SWarner Losh break; 562114b4164SWarner Losh } 563114b4164SWarner Losh if (mmcbr_get_mode(sc->dev) == mode_sd) { 564114b4164SWarner Losh ivar->mode = mode_sd; 565114b4164SWarner Losh mmc_decode_cid(1, ivar->raw_cid, &ivar->cid); 566114b4164SWarner Losh mmc_send_relative_addr(sc, &resp); 567114b4164SWarner Losh ivar->rca = resp >> 16; 568114b4164SWarner Losh // RO check 569114b4164SWarner Losh mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 570114b4164SWarner Losh mmc_decode_csd(1, ivar->raw_csd, &ivar->csd); 571114b4164SWarner Losh printf("SD CARD: %lld bytes\n", ivar->csd.capacity); 572114b4164SWarner Losh child = device_add_child(sc->dev, NULL, -1); 573114b4164SWarner Losh device_set_ivars(child, ivar); 574114b4164SWarner Losh break; 575114b4164SWarner Losh } 576114b4164SWarner Losh panic("Write MMC card code here"); 577114b4164SWarner Losh } 578114b4164SWarner Losh } 579114b4164SWarner Losh 580114b4164SWarner Losh static void 581114b4164SWarner Losh mmc_go_discovery(struct mmc_softc *sc) 582114b4164SWarner Losh { 583114b4164SWarner Losh uint32_t ocr; 584114b4164SWarner Losh device_t dev; 585114b4164SWarner Losh 586114b4164SWarner Losh dev = sc->dev; 587114b4164SWarner Losh if (mmcbr_get_power_mode(dev) != power_on) { 588114b4164SWarner Losh // First, try SD modes 589114b4164SWarner Losh mmcbr_set_mode(dev, mode_sd); 590114b4164SWarner Losh mmc_power_up(sc); 591114b4164SWarner Losh mmcbr_set_bus_mode(dev, pushpull); 592114b4164SWarner Losh mmc_idle_cards(sc); 593114b4164SWarner Losh if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 594114b4164SWarner Losh // Failed, try MMC 595114b4164SWarner Losh mmcbr_set_mode(dev, mode_mmc); 596114b4164SWarner Losh if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) 597114b4164SWarner Losh return; // Failed both, punt! XXX power down? 598114b4164SWarner Losh } 599114b4164SWarner Losh mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr)); 600114b4164SWarner Losh if (mmcbr_get_ocr(dev) != 0) 601114b4164SWarner Losh mmc_idle_cards(sc); 602114b4164SWarner Losh } else { 603114b4164SWarner Losh mmcbr_set_bus_mode(dev, opendrain); 604114b4164SWarner Losh mmcbr_set_clock(dev, mmcbr_get_f_min(dev)); 605114b4164SWarner Losh mmcbr_update_ios(dev); 606114b4164SWarner Losh // XXX recompute vdd based on new cards? 607114b4164SWarner Losh } 608114b4164SWarner Losh /* 609114b4164SWarner Losh * Make sure that we have a mutually agreeable voltage to at least 610114b4164SWarner Losh * one card on the bus. 611114b4164SWarner Losh */ 612114b4164SWarner Losh if (mmcbr_get_ocr(dev) == 0) 613114b4164SWarner Losh return; 614114b4164SWarner Losh /* 615114b4164SWarner Losh * Reselect the cards after we've idled them above. 616114b4164SWarner Losh */ 617114b4164SWarner Losh if (mmcbr_get_mode(dev) == mode_sd) 618114b4164SWarner Losh mmc_send_app_op_cond(sc, mmcbr_get_ocr(dev), NULL); 619114b4164SWarner Losh else 620114b4164SWarner Losh mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL); 621114b4164SWarner Losh mmc_discover_cards(sc); 622114b4164SWarner Losh 623114b4164SWarner Losh mmcbr_set_bus_mode(dev, pushpull); 624114b4164SWarner Losh mmcbr_update_ios(dev); 625114b4164SWarner Losh bus_generic_attach(dev); 626114b4164SWarner Losh // mmc_update_children_sysctl(dev); 627114b4164SWarner Losh } 628114b4164SWarner Losh 629114b4164SWarner Losh static int 630114b4164SWarner Losh mmc_calculate_clock(struct mmc_softc *sc) 631114b4164SWarner Losh { 632114b4164SWarner Losh int max_dtr = 0; 633114b4164SWarner Losh int nkid, i, f_min, f_max; 634114b4164SWarner Losh device_t *kids; 635114b4164SWarner Losh 636114b4164SWarner Losh f_min = mmcbr_get_f_min(sc->dev); 637114b4164SWarner Losh f_max = mmcbr_get_f_max(sc->dev); 638114b4164SWarner Losh max_dtr = f_max; 639114b4164SWarner Losh if (device_get_children(sc->dev, &kids, &nkid) != 0) 640114b4164SWarner Losh panic("can't get children"); 641114b4164SWarner Losh for (i = 0; i < nkid; i++) 642114b4164SWarner Losh if (mmc_get_tran_speed(kids[i]) < max_dtr) 643114b4164SWarner Losh max_dtr = mmc_get_tran_speed(kids[i]); 644114b4164SWarner Losh free(kids, M_TEMP); 645114b4164SWarner Losh device_printf(sc->dev, "setting transfer rate to %d.%03dMHz\n", 646114b4164SWarner Losh max_dtr / 1000000, (max_dtr / 1000) % 1000); 647114b4164SWarner Losh return max_dtr; 648114b4164SWarner Losh } 649114b4164SWarner Losh 650114b4164SWarner Losh static void 651114b4164SWarner Losh mmc_scan(struct mmc_softc *sc) 652114b4164SWarner Losh { 653114b4164SWarner Losh device_t dev; 654114b4164SWarner Losh 655114b4164SWarner Losh dev = sc->dev; 656114b4164SWarner Losh mmc_acquire_bus(dev, dev); 657114b4164SWarner Losh 658114b4164SWarner Losh if (mmcbr_get_power_mode(dev) == power_on) 659114b4164SWarner Losh mmc_rescan_cards(sc); 660114b4164SWarner Losh mmc_go_discovery(sc); 661114b4164SWarner Losh mmcbr_set_clock(dev, mmc_calculate_clock(sc)); 662114b4164SWarner Losh mmcbr_update_ios(dev); 663114b4164SWarner Losh 664114b4164SWarner Losh mmc_release_bus(dev, dev); 665114b4164SWarner Losh // XXX probe/attach/detach children? 666114b4164SWarner Losh } 667114b4164SWarner Losh 668114b4164SWarner Losh static int 669114b4164SWarner Losh mmc_read_ivar(device_t bus, device_t child, int which, u_char *result) 670114b4164SWarner Losh { 671114b4164SWarner Losh struct mmc_ivars *ivar = device_get_ivars(child); 672114b4164SWarner Losh 673114b4164SWarner Losh switch (which) { 674114b4164SWarner Losh default: 675114b4164SWarner Losh return (EINVAL); 676114b4164SWarner Losh case MMC_IVAR_DSR_IMP: 677114b4164SWarner Losh *(int *)result = ivar->csd.dsr_imp; 678114b4164SWarner Losh break; 679114b4164SWarner Losh case MMC_IVAR_MEDIA_SIZE: 680114b4164SWarner Losh *(int *)result = ivar->csd.capacity; 681114b4164SWarner Losh break; 682114b4164SWarner Losh case MMC_IVAR_MODE: 683114b4164SWarner Losh *(int *)result = ivar->mode; 684114b4164SWarner Losh break; 685114b4164SWarner Losh case MMC_IVAR_RCA: 686114b4164SWarner Losh *(int *)result = ivar->rca; 687114b4164SWarner Losh break; 688114b4164SWarner Losh case MMC_IVAR_SECTOR_SIZE: 689114b4164SWarner Losh *(int *)result = 512; 690114b4164SWarner Losh break; 691114b4164SWarner Losh case MMC_IVAR_TRAN_SPEED: 692114b4164SWarner Losh *(int *)result = ivar->csd.tran_speed; 693114b4164SWarner Losh break; 694114b4164SWarner Losh } 695114b4164SWarner Losh return (0); 696114b4164SWarner Losh } 697114b4164SWarner Losh 698114b4164SWarner Losh static int 699114b4164SWarner Losh mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 700114b4164SWarner Losh { 701114b4164SWarner Losh // None are writable ATM 702114b4164SWarner Losh switch (which) { 703114b4164SWarner Losh default: 704114b4164SWarner Losh return (EINVAL); 705114b4164SWarner Losh } 706114b4164SWarner Losh return (0); 707114b4164SWarner Losh } 708114b4164SWarner Losh 709114b4164SWarner Losh 710114b4164SWarner Losh static void 711114b4164SWarner Losh mmc_delayed_attach(void *xsc) 712114b4164SWarner Losh { 713114b4164SWarner Losh struct mmc_softc *sc = xsc; 714114b4164SWarner Losh 715114b4164SWarner Losh mmc_scan(sc); 716114b4164SWarner Losh config_intrhook_disestablish(&sc->config_intrhook); 717114b4164SWarner Losh } 718114b4164SWarner Losh 719114b4164SWarner Losh static device_method_t mmc_methods[] = { 720114b4164SWarner Losh /* device_if */ 721114b4164SWarner Losh DEVMETHOD(device_probe, mmc_probe), 722114b4164SWarner Losh DEVMETHOD(device_attach, mmc_attach), 723114b4164SWarner Losh DEVMETHOD(device_detach, mmc_detach), 724114b4164SWarner Losh 725114b4164SWarner Losh /* Bus interface */ 726114b4164SWarner Losh DEVMETHOD(bus_read_ivar, mmc_read_ivar), 727114b4164SWarner Losh DEVMETHOD(bus_write_ivar, mmc_write_ivar), 728114b4164SWarner Losh 729114b4164SWarner Losh /* MMC Bus interface */ 730114b4164SWarner Losh DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request), 731114b4164SWarner Losh DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus), 732114b4164SWarner Losh DEVMETHOD(mmcbus_release_bus, mmc_release_bus), 733114b4164SWarner Losh 734114b4164SWarner Losh {0, 0}, 735114b4164SWarner Losh }; 736114b4164SWarner Losh 737114b4164SWarner Losh static driver_t mmc_driver = { 738114b4164SWarner Losh "mmc", 739114b4164SWarner Losh mmc_methods, 740114b4164SWarner Losh sizeof(struct mmc_softc), 741114b4164SWarner Losh }; 742114b4164SWarner Losh static devclass_t mmc_devclass; 743114b4164SWarner Losh 744114b4164SWarner Losh 745114b4164SWarner Losh DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, 0, 0); 746