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/bio.h> 59114b4164SWarner Losh #include <sys/bus.h> 60114b4164SWarner Losh #include <sys/conf.h> 61114b4164SWarner Losh #include <sys/kernel.h> 62114b4164SWarner Losh #include <sys/kthread.h> 63114b4164SWarner Losh #include <sys/lock.h> 64114b4164SWarner Losh #include <sys/malloc.h> 65114b4164SWarner Losh #include <sys/module.h> 66114b4164SWarner Losh #include <sys/mutex.h> 67114b4164SWarner Losh #include <geom/geom_disk.h> 68114b4164SWarner Losh 69114b4164SWarner Losh #include <dev/mmc/mmcvar.h> 70114b4164SWarner Losh #include <dev/mmc/mmcreg.h> 71114b4164SWarner Losh 72114b4164SWarner Losh #include "mmcbus_if.h" 73114b4164SWarner Losh 74114b4164SWarner Losh struct mmcsd_softc { 75114b4164SWarner Losh device_t dev; 76114b4164SWarner Losh struct mtx sc_mtx; 77114b4164SWarner Losh struct disk *disk; 78114b4164SWarner Losh struct proc *p; 79114b4164SWarner Losh struct bio_queue_head bio_queue; 80a7cf6274SAlexander Motin daddr_t eblock, eend; /* Range remaining after the last erase. */ 810e1466acSWarner Losh int running; 82114b4164SWarner Losh }; 83114b4164SWarner Losh 84114b4164SWarner Losh /* bus entry points */ 85114b4164SWarner Losh static int mmcsd_probe(device_t dev); 86114b4164SWarner Losh static int mmcsd_attach(device_t dev); 87114b4164SWarner Losh static int mmcsd_detach(device_t dev); 88114b4164SWarner Losh 89114b4164SWarner Losh /* disk routines */ 90114b4164SWarner Losh static int mmcsd_open(struct disk *dp); 91114b4164SWarner Losh static int mmcsd_close(struct disk *dp); 92114b4164SWarner Losh static void mmcsd_strategy(struct bio *bp); 93114b4164SWarner Losh static void mmcsd_task(void *arg); 94114b4164SWarner Losh 95a0075e58SWarner Losh static const char *mmcsd_card_name(device_t dev); 96a0075e58SWarner Losh static int mmcsd_bus_bit_width(device_t dev); 97a0075e58SWarner Losh 98114b4164SWarner Losh #define MMCSD_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 99114b4164SWarner Losh #define MMCSD_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 100114b4164SWarner Losh #define MMCSD_LOCK_INIT(_sc) \ 101114b4164SWarner Losh mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 102114b4164SWarner Losh "mmcsd", MTX_DEF) 103114b4164SWarner Losh #define MMCSD_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 104114b4164SWarner Losh #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 105114b4164SWarner Losh #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 106114b4164SWarner Losh 107114b4164SWarner Losh static int 108114b4164SWarner Losh mmcsd_probe(device_t dev) 109114b4164SWarner Losh { 110114b4164SWarner Losh 111c18f1e26SAlexander Motin device_quiet(dev); 1120c0903b1SWarner Losh device_set_desc(dev, "MMC/SD Memory Card"); 113114b4164SWarner Losh return (0); 114114b4164SWarner Losh } 115114b4164SWarner Losh 116114b4164SWarner Losh static int 117114b4164SWarner Losh mmcsd_attach(device_t dev) 118114b4164SWarner Losh { 119114b4164SWarner Losh struct mmcsd_softc *sc; 120a0075e58SWarner Losh struct disk *d; 121a0075e58SWarner Losh intmax_t mb; 122a0075e58SWarner Losh char unit; 123114b4164SWarner Losh 124114b4164SWarner Losh sc = device_get_softc(dev); 125114b4164SWarner Losh sc->dev = dev; 126114b4164SWarner Losh MMCSD_LOCK_INIT(sc); 127114b4164SWarner Losh 128a0075e58SWarner Losh d = sc->disk = disk_alloc(); 129a0075e58SWarner Losh d->d_open = mmcsd_open; 130a0075e58SWarner Losh d->d_close = mmcsd_close; 131a0075e58SWarner Losh d->d_strategy = mmcsd_strategy; 132a0075e58SWarner Losh // d->d_dump = mmcsd_dump; Need polling mmc layer 133a0075e58SWarner Losh d->d_name = "mmcsd"; 134a0075e58SWarner Losh d->d_drv1 = sc; 1353906d42dSAlexander Motin d->d_maxsize = 4*1024*1024; /* Maximum defined SD card AU size. */ 136a0075e58SWarner Losh d->d_sectorsize = mmc_get_sector_size(dev); 137a0075e58SWarner Losh d->d_mediasize = mmc_get_media_size(dev) * d->d_sectorsize; 138a0075e58SWarner Losh d->d_unit = device_get_unit(dev); 1393906d42dSAlexander Motin d->d_flags = DISKFLAG_CANDELETE; 140a0075e58SWarner Losh /* 141a0075e58SWarner Losh * Display in most natural units. There's no cards < 1MB. 142a0075e58SWarner Losh * The SD standard goes to 2GiB, but the data format supports 143a0075e58SWarner Losh * up to 4GiB and some card makers push it up to this limit. 144a0075e58SWarner Losh * The SDHC standard only goes to 32GiB (the data format in 145a0075e58SWarner Losh * SDHC is good to 2TiB however, which isn't too ugly at 146a0075e58SWarner Losh * 2048GiBm, so we note it in passing here and don't add the 147a0075e58SWarner Losh * code to print TiB). 148a0075e58SWarner Losh */ 149a0075e58SWarner Losh mb = d->d_mediasize >> 20; /* 1MiB == 1 << 20 */ 150a0075e58SWarner Losh unit = 'M'; 15158bb6260SAlexander Motin if (mb >= 10240) { /* 1GiB = 1024 MiB */ 152a0075e58SWarner Losh unit = 'G'; 153a0075e58SWarner Losh mb /= 1024; 154a0075e58SWarner Losh } 155a0075e58SWarner Losh device_printf(dev, "%ju%cB <%s Memory Card>%s at %s %dMHz/%dbit\n", 156a0075e58SWarner Losh mb, unit, mmcsd_card_name(dev), 1570c0903b1SWarner Losh mmc_get_read_only(dev) ? " (read-only)" : "", 158a0075e58SWarner Losh device_get_nameunit(device_get_parent(dev)), 159a0075e58SWarner Losh mmc_get_tran_speed(dev) / 1000000, mmcsd_bus_bit_width(dev)); 160a0075e58SWarner Losh disk_create(d, DISK_VERSION); 161114b4164SWarner Losh bioq_init(&sc->bio_queue); 1620e1466acSWarner Losh 1630e1466acSWarner Losh sc->running = 1; 164a7cf6274SAlexander Motin sc->eblock = sc->eend = 0; 1653745c395SJulian Elischer kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card"); 166114b4164SWarner Losh 167114b4164SWarner Losh return (0); 168114b4164SWarner Losh } 169114b4164SWarner Losh 170114b4164SWarner Losh static int 171114b4164SWarner Losh mmcsd_detach(device_t dev) 172114b4164SWarner Losh { 1730e1466acSWarner Losh struct mmcsd_softc *sc = device_get_softc(dev); 1740e1466acSWarner Losh 1750e1466acSWarner Losh /* kill thread */ 1760e1466acSWarner Losh MMCSD_LOCK(sc); 1770e1466acSWarner Losh sc->running = 0; 1780e1466acSWarner Losh wakeup(sc); 1790e1466acSWarner Losh MMCSD_UNLOCK(sc); 1800e1466acSWarner Losh 1810e1466acSWarner Losh /* wait for thread to finish. XXX probably want timeout. -sorbo */ 1820e1466acSWarner Losh MMCSD_LOCK(sc); 1830e1466acSWarner Losh while (sc->running != -1) 1840e1466acSWarner Losh msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0); 1850e1466acSWarner Losh MMCSD_UNLOCK(sc); 1860e1466acSWarner Losh 187249b0e85SAlexander Motin /* Flush the request queue. */ 188249b0e85SAlexander Motin bioq_flush(&sc->bio_queue, NULL, ENXIO); 1890e1466acSWarner Losh /* kill disk */ 1900e1466acSWarner Losh disk_destroy(sc->disk); 1910e1466acSWarner Losh 1920e1466acSWarner Losh MMCSD_LOCK_DESTROY(sc); 1930e1466acSWarner Losh 194128d0ff2SWarner Losh return (0); 195114b4164SWarner Losh } 196114b4164SWarner Losh 197114b4164SWarner Losh static int 198114b4164SWarner Losh mmcsd_open(struct disk *dp) 199114b4164SWarner Losh { 200128d0ff2SWarner Losh return (0); 201114b4164SWarner Losh } 202114b4164SWarner Losh 203114b4164SWarner Losh static int 204114b4164SWarner Losh mmcsd_close(struct disk *dp) 205114b4164SWarner Losh { 206128d0ff2SWarner Losh return (0); 207114b4164SWarner Losh } 208114b4164SWarner Losh 209114b4164SWarner Losh static void 210114b4164SWarner Losh mmcsd_strategy(struct bio *bp) 211114b4164SWarner Losh { 212114b4164SWarner Losh struct mmcsd_softc *sc; 213114b4164SWarner Losh 214114b4164SWarner Losh sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1; 215114b4164SWarner Losh MMCSD_LOCK(sc); 216249b0e85SAlexander Motin if (sc->running > 0) { 217114b4164SWarner Losh bioq_disksort(&sc->bio_queue, bp); 218114b4164SWarner Losh wakeup(sc); 219114b4164SWarner Losh MMCSD_UNLOCK(sc); 220249b0e85SAlexander Motin } else { 221249b0e85SAlexander Motin MMCSD_UNLOCK(sc); 222249b0e85SAlexander Motin biofinish(bp, NULL, ENXIO); 223249b0e85SAlexander Motin } 224114b4164SWarner Losh } 225114b4164SWarner Losh 2263906d42dSAlexander Motin static daddr_t 2273906d42dSAlexander Motin mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp) 228114b4164SWarner Losh { 229114b4164SWarner Losh daddr_t block, end; 230114b4164SWarner Losh struct mmc_command cmd; 231114b4164SWarner Losh struct mmc_command stop; 232114b4164SWarner Losh struct mmc_request req; 233114b4164SWarner Losh struct mmc_data data; 2343906d42dSAlexander Motin device_t dev = sc->dev; 2353906d42dSAlexander Motin int sz = sc->disk->d_sectorsize; 236114b4164SWarner Losh 2373906d42dSAlexander Motin block = bp->bio_pblkno; 238114b4164SWarner Losh end = bp->bio_pblkno + (bp->bio_bcount / sz); 2393906d42dSAlexander Motin while (block < end) { 2403906d42dSAlexander Motin char *vaddr = bp->bio_data + 2413906d42dSAlexander Motin (block - bp->bio_pblkno) * sz; 2423a4a2557SAlexander Motin int numblocks = min(end - block, mmc_get_max_data(dev)); 243114b4164SWarner Losh memset(&req, 0, sizeof(req)); 244114b4164SWarner Losh memset(&cmd, 0, sizeof(cmd)); 245114b4164SWarner Losh memset(&stop, 0, sizeof(stop)); 246114b4164SWarner Losh req.cmd = &cmd; 247114b4164SWarner Losh cmd.data = &data; 248114b4164SWarner Losh if (bp->bio_cmd == BIO_READ) { 2490c0903b1SWarner Losh if (numblocks > 1) 250114b4164SWarner Losh cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 251114b4164SWarner Losh else 252114b4164SWarner Losh cmd.opcode = MMC_READ_SINGLE_BLOCK; 2530c0903b1SWarner Losh } else { 2540c0903b1SWarner Losh if (numblocks > 1) 2550c0903b1SWarner Losh cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; 2560c0903b1SWarner Losh else 257114b4164SWarner Losh cmd.opcode = MMC_WRITE_BLOCK; 2580c0903b1SWarner Losh } 259c18f1e26SAlexander Motin cmd.arg = block; 260c18f1e26SAlexander Motin if (!mmc_get_high_cap(dev)) 261c18f1e26SAlexander Motin cmd.arg <<= 9; 262114b4164SWarner Losh cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 263114b4164SWarner Losh data.data = vaddr; 264114b4164SWarner Losh data.mrq = &req; 2650c0903b1SWarner Losh if (bp->bio_cmd == BIO_READ) 266114b4164SWarner Losh data.flags = MMC_DATA_READ; 2670c0903b1SWarner Losh else 268114b4164SWarner Losh data.flags = MMC_DATA_WRITE; 2690c0903b1SWarner Losh data.len = numblocks * sz; 2700c0903b1SWarner Losh if (numblocks > 1) { 2710c0903b1SWarner Losh data.flags |= MMC_DATA_MULTI; 272114b4164SWarner Losh stop.opcode = MMC_STOP_TRANSMISSION; 273114b4164SWarner Losh stop.arg = 0; 274114b4164SWarner Losh stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 2750c0903b1SWarner Losh req.stop = &stop; 2760c0903b1SWarner Losh } 2770c0903b1SWarner Losh // printf("Len %d %lld-%lld flags %#x sz %d\n", 2780c0903b1SWarner Losh // (int)data.len, (long long)block, (long long)end, data.flags, sz); 279114b4164SWarner Losh MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 280114b4164SWarner Losh &req); 2810c0903b1SWarner Losh if (req.cmd->error != MMC_ERR_NONE) 2820c0903b1SWarner Losh break; 2830c0903b1SWarner Losh block += numblocks; 284114b4164SWarner Losh } 2853906d42dSAlexander Motin return (block); 2863906d42dSAlexander Motin } 2873906d42dSAlexander Motin 2883906d42dSAlexander Motin static daddr_t 2893906d42dSAlexander Motin mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp) 2903906d42dSAlexander Motin { 2913906d42dSAlexander Motin daddr_t block, end, start, stop; 2923906d42dSAlexander Motin struct mmc_command cmd; 2933906d42dSAlexander Motin struct mmc_request req; 2943906d42dSAlexander Motin device_t dev = sc->dev; 2953906d42dSAlexander Motin int sz = sc->disk->d_sectorsize; 2963906d42dSAlexander Motin int erase_sector; 2973906d42dSAlexander Motin 2983906d42dSAlexander Motin block = bp->bio_pblkno; 2993906d42dSAlexander Motin end = bp->bio_pblkno + (bp->bio_bcount / sz); 300a7cf6274SAlexander Motin /* Coalesce with part remaining from previous request. */ 301a7cf6274SAlexander Motin if (block > sc->eblock && block <= sc->eend) 302a7cf6274SAlexander Motin block = sc->eblock; 303a7cf6274SAlexander Motin if (end >= sc->eblock && end < sc->eend) 304a7cf6274SAlexander Motin end = sc->eend; 3053906d42dSAlexander Motin /* Safe round to the erase sector boundaries. */ 3063906d42dSAlexander Motin erase_sector = mmc_get_erase_sector(dev); 3073906d42dSAlexander Motin start = block + erase_sector - 1; /* Round up. */ 3083906d42dSAlexander Motin start -= start % erase_sector; 3093906d42dSAlexander Motin stop = end; /* Round down. */ 3103906d42dSAlexander Motin stop -= end % erase_sector; 311a7cf6274SAlexander Motin /* We can't erase area smaller then sector, store it for later. */ 312a7cf6274SAlexander Motin if (start >= stop) { 313a7cf6274SAlexander Motin sc->eblock = block; 314a7cf6274SAlexander Motin sc->eend = end; 3153906d42dSAlexander Motin return (end); 316a7cf6274SAlexander Motin } 3173906d42dSAlexander Motin 3183906d42dSAlexander Motin /* Set erase start position. */ 3193906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 3203906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 3213906d42dSAlexander Motin req.cmd = &cmd; 3223906d42dSAlexander Motin if (mmc_get_card_type(dev) == mode_sd) 3233906d42dSAlexander Motin cmd.opcode = SD_ERASE_WR_BLK_START; 3243906d42dSAlexander Motin else 3253906d42dSAlexander Motin cmd.opcode = MMC_ERASE_GROUP_START; 3263906d42dSAlexander Motin cmd.arg = start; 3273906d42dSAlexander Motin if (!mmc_get_high_cap(dev)) 3283906d42dSAlexander Motin cmd.arg <<= 9; 3293906d42dSAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 3303906d42dSAlexander Motin MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 3313906d42dSAlexander Motin &req); 3323906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 3333906d42dSAlexander Motin printf("erase err1: %d\n", req.cmd->error); 3343906d42dSAlexander Motin return (block); 3353906d42dSAlexander Motin } 3363906d42dSAlexander Motin /* Set erase stop position. */ 3373906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 3383906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 3393906d42dSAlexander Motin req.cmd = &cmd; 3403906d42dSAlexander Motin if (mmc_get_card_type(dev) == mode_sd) 3413906d42dSAlexander Motin cmd.opcode = SD_ERASE_WR_BLK_END; 3423906d42dSAlexander Motin else 3433906d42dSAlexander Motin cmd.opcode = MMC_ERASE_GROUP_END; 3443906d42dSAlexander Motin cmd.arg = stop; 3453906d42dSAlexander Motin if (!mmc_get_high_cap(dev)) 3463906d42dSAlexander Motin cmd.arg <<= 9; 3473906d42dSAlexander Motin cmd.arg--; 3483906d42dSAlexander Motin cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 3493906d42dSAlexander Motin MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 3503906d42dSAlexander Motin &req); 3513906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 3523906d42dSAlexander Motin printf("erase err2: %d\n", req.cmd->error); 3533906d42dSAlexander Motin return (block); 3543906d42dSAlexander Motin } 3553906d42dSAlexander Motin /* Erase range. */ 3563906d42dSAlexander Motin memset(&req, 0, sizeof(req)); 3573906d42dSAlexander Motin memset(&cmd, 0, sizeof(cmd)); 3583906d42dSAlexander Motin req.cmd = &cmd; 3593906d42dSAlexander Motin cmd.opcode = MMC_ERASE; 3603906d42dSAlexander Motin cmd.arg = 0; 3613906d42dSAlexander Motin cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 3623906d42dSAlexander Motin MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 3633906d42dSAlexander Motin &req); 3643906d42dSAlexander Motin if (req.cmd->error != MMC_ERR_NONE) { 3653906d42dSAlexander Motin printf("erase err3 %d\n", req.cmd->error); 3663906d42dSAlexander Motin return (block); 3673906d42dSAlexander Motin } 368a7cf6274SAlexander Motin /* Store one of remaining parts for the next call. */ 369a7cf6274SAlexander Motin if (bp->bio_pblkno >= sc->eblock || block == start) { 370a7cf6274SAlexander Motin sc->eblock = stop; /* Predict next forward. */ 371a7cf6274SAlexander Motin sc->eend = end; 372a7cf6274SAlexander Motin } else { 373a7cf6274SAlexander Motin sc->eblock = block; /* Predict next backward. */ 374a7cf6274SAlexander Motin sc->eend = start; 375a7cf6274SAlexander Motin } 3763906d42dSAlexander Motin return (end); 3773906d42dSAlexander Motin } 3783906d42dSAlexander Motin 3793906d42dSAlexander Motin static void 3803906d42dSAlexander Motin mmcsd_task(void *arg) 3813906d42dSAlexander Motin { 3823906d42dSAlexander Motin struct mmcsd_softc *sc = (struct mmcsd_softc*)arg; 3833906d42dSAlexander Motin struct bio *bp; 3843906d42dSAlexander Motin int sz; 3853906d42dSAlexander Motin daddr_t block, end; 3863906d42dSAlexander Motin device_t dev; 3873906d42dSAlexander Motin 3883906d42dSAlexander Motin dev = sc->dev; 389249b0e85SAlexander Motin while (1) { 3903906d42dSAlexander Motin MMCSD_LOCK(sc); 3913906d42dSAlexander Motin do { 392249b0e85SAlexander Motin if (sc->running == 0) 393249b0e85SAlexander Motin goto out; 394249b0e85SAlexander Motin bp = bioq_takefirst(&sc->bio_queue); 3953906d42dSAlexander Motin if (bp == NULL) 3963906d42dSAlexander Motin msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0); 397249b0e85SAlexander Motin } while (bp == NULL); 3983906d42dSAlexander Motin MMCSD_UNLOCK(sc); 3993906d42dSAlexander Motin if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) { 4003906d42dSAlexander Motin bp->bio_error = EROFS; 4013906d42dSAlexander Motin bp->bio_resid = bp->bio_bcount; 4023906d42dSAlexander Motin bp->bio_flags |= BIO_ERROR; 4033906d42dSAlexander Motin biodone(bp); 4043906d42dSAlexander Motin continue; 4053906d42dSAlexander Motin } 4063906d42dSAlexander Motin MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev); 4073906d42dSAlexander Motin sz = sc->disk->d_sectorsize; 4083906d42dSAlexander Motin block = bp->bio_pblkno; 4093906d42dSAlexander Motin end = bp->bio_pblkno + (bp->bio_bcount / sz); 4103906d42dSAlexander Motin if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { 411a7cf6274SAlexander Motin /* Access to the remaining erase block obsoletes it. */ 412a7cf6274SAlexander Motin if (block < sc->eend && end > sc->eblock) 413a7cf6274SAlexander Motin sc->eblock = sc->eend = 0; 4143906d42dSAlexander Motin block = mmcsd_rw(sc, bp); 4153906d42dSAlexander Motin } else if (bp->bio_cmd == BIO_DELETE) { 4163906d42dSAlexander Motin block = mmcsd_delete(sc, bp); 4173906d42dSAlexander Motin } 418114b4164SWarner Losh MMCBUS_RELEASE_BUS(device_get_parent(dev), dev); 4190c0903b1SWarner Losh if (block < end) { 4200c0903b1SWarner Losh bp->bio_error = EIO; 4210c0903b1SWarner Losh bp->bio_resid = (end - block) * sz; 4220c0903b1SWarner Losh bp->bio_flags |= BIO_ERROR; 4230c0903b1SWarner Losh } 424114b4164SWarner Losh biodone(bp); 425114b4164SWarner Losh } 426249b0e85SAlexander Motin out: 4270e1466acSWarner Losh /* tell parent we're done */ 4280e1466acSWarner Losh sc->running = -1; 4290e1466acSWarner Losh wakeup(sc); 4300e1466acSWarner Losh MMCSD_UNLOCK(sc); 4310e1466acSWarner Losh 4323745c395SJulian Elischer kproc_exit(0); 433114b4164SWarner Losh } 434114b4164SWarner Losh 435a0075e58SWarner Losh static const char * 436a0075e58SWarner Losh mmcsd_card_name(device_t dev) 437a0075e58SWarner Losh { 438a0075e58SWarner Losh if (mmc_get_card_type(dev) == mode_mmc) 439a0075e58SWarner Losh return ("MMC"); 440a0075e58SWarner Losh if (mmc_get_high_cap(dev)) 441a0075e58SWarner Losh return ("SDHC"); 442a0075e58SWarner Losh return ("SD"); 443a0075e58SWarner Losh } 444a0075e58SWarner Losh 445a0075e58SWarner Losh static int 446a0075e58SWarner Losh mmcsd_bus_bit_width(device_t dev) 447a0075e58SWarner Losh { 448a0075e58SWarner Losh if (mmc_get_bus_width(dev) == bus_width_1) 449a0075e58SWarner Losh return (1); 450a0075e58SWarner Losh if (mmc_get_bus_width(dev) == bus_width_4) 451a0075e58SWarner Losh return (4); 452a0075e58SWarner Losh return (8); 453a0075e58SWarner Losh } 454a0075e58SWarner Losh 455114b4164SWarner Losh static device_method_t mmcsd_methods[] = { 456114b4164SWarner Losh DEVMETHOD(device_probe, mmcsd_probe), 457114b4164SWarner Losh DEVMETHOD(device_attach, mmcsd_attach), 458114b4164SWarner Losh DEVMETHOD(device_detach, mmcsd_detach), 459114b4164SWarner Losh {0, 0}, 460114b4164SWarner Losh }; 461114b4164SWarner Losh 462114b4164SWarner Losh static driver_t mmcsd_driver = { 463114b4164SWarner Losh "mmcsd", 464114b4164SWarner Losh mmcsd_methods, 465114b4164SWarner Losh sizeof(struct mmcsd_softc), 466114b4164SWarner Losh }; 467114b4164SWarner Losh static devclass_t mmcsd_devclass; 468114b4164SWarner Losh 469114b4164SWarner Losh DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0); 470