1 /*- 2 * Copyright (c) 2006 Bernd Walter. All rights reserved. 3 * Copyright (c) 2006 M. Warner Losh. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * Portions of this software may have been developed with reference to 26 * the SD Simplified Specification. The following disclaimer may apply: 27 * 28 * The following conditions apply to the release of the simplified 29 * specification ("Simplified Specification") by the SD Card Association and 30 * the SD Group. The Simplified Specification is a subset of the complete SD 31 * Specification which is owned by the SD Card Association and the SD 32 * Group. This Simplified Specification is provided on a non-confidential 33 * basis subject to the disclaimers below. Any implementation of the 34 * Simplified Specification may require a license from the SD Card 35 * Association, SD Group, SD-3C LLC or other third parties. 36 * 37 * Disclaimers: 38 * 39 * The information contained in the Simplified Specification is presented only 40 * as a standard specification for SD Cards and SD Host/Ancillary products and 41 * is provided "AS-IS" without any representations or warranties of any 42 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 43 * Card Association for any damages, any infringements of patents or other 44 * right of the SD Group, SD-3C LLC, the SD Card Association or any third 45 * parties, which may result from its use. No license is granted by 46 * implication, estoppel or otherwise under any patent or other rights of the 47 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 48 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 49 * or the SD Card Association to disclose or distribute any technical 50 * information, know-how or other confidential information to any third party. 51 */ 52 53 #include <sys/cdefs.h> 54 __FBSDID("$FreeBSD$"); 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/bio.h> 59 #include <sys/bus.h> 60 #include <sys/conf.h> 61 #include <sys/kernel.h> 62 #include <sys/kthread.h> 63 #include <sys/lock.h> 64 #include <sys/malloc.h> 65 #include <sys/module.h> 66 #include <sys/mutex.h> 67 #include <geom/geom_disk.h> 68 69 #include <dev/mmc/mmcvar.h> 70 #include <dev/mmc/mmcreg.h> 71 72 #include "mmcbus_if.h" 73 74 struct mmcsd_softc { 75 device_t dev; 76 struct mtx sc_mtx; 77 struct disk *disk; 78 struct proc *p; 79 struct bio_queue_head bio_queue; 80 int running; 81 }; 82 83 #define MULTI_BLOCK_READ_BROKEN 84 85 /* bus entry points */ 86 static int mmcsd_probe(device_t dev); 87 static int mmcsd_attach(device_t dev); 88 static int mmcsd_detach(device_t dev); 89 90 /* disk routines */ 91 static int mmcsd_open(struct disk *dp); 92 static int mmcsd_close(struct disk *dp); 93 static void mmcsd_strategy(struct bio *bp); 94 static void mmcsd_task(void *arg); 95 96 #define MMCSD_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 97 #define MMCSD_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 98 #define MMCSD_LOCK_INIT(_sc) \ 99 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 100 "mmcsd", MTX_DEF) 101 #define MMCSD_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 102 #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 103 #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 104 105 static int 106 mmcsd_probe(device_t dev) 107 { 108 109 device_set_desc(dev, "mmc or sd flash card"); 110 return (0); 111 } 112 113 static int 114 mmcsd_attach(device_t dev) 115 { 116 struct mmcsd_softc *sc; 117 118 sc = device_get_softc(dev); 119 sc->dev = dev; 120 MMCSD_LOCK_INIT(sc); 121 122 sc->disk = disk_alloc(); 123 sc->disk->d_open = mmcsd_open; 124 sc->disk->d_close = mmcsd_close; 125 sc->disk->d_strategy = mmcsd_strategy; 126 // sc->disk->d_dump = mmcsd_dump; Need polling mmc layer 127 sc->disk->d_name = "mmcsd"; 128 sc->disk->d_drv1 = sc; 129 sc->disk->d_maxsize = DFLTPHYS; 130 sc->disk->d_sectorsize = mmc_get_sector_size(dev); 131 sc->disk->d_mediasize = mmc_get_media_size(dev); 132 sc->disk->d_unit = device_get_unit(dev); 133 disk_create(sc->disk, DISK_VERSION); 134 bioq_init(&sc->bio_queue); 135 136 sc->running = 1; 137 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card"); 138 139 return (0); 140 } 141 142 static int 143 mmcsd_detach(device_t dev) 144 { 145 struct mmcsd_softc *sc = device_get_softc(dev); 146 147 /* kill thread */ 148 MMCSD_LOCK(sc); 149 sc->running = 0; 150 wakeup(sc); 151 MMCSD_UNLOCK(sc); 152 153 /* wait for thread to finish. XXX probably want timeout. -sorbo */ 154 MMCSD_LOCK(sc); 155 while (sc->running != -1) 156 msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0); 157 MMCSD_UNLOCK(sc); 158 159 /* kill disk */ 160 disk_destroy(sc->disk); 161 /* XXX destroy anything in queue */ 162 163 MMCSD_LOCK_DESTROY(sc); 164 165 return 0; 166 } 167 168 static int 169 mmcsd_open(struct disk *dp) 170 { 171 return 0; 172 } 173 174 static int 175 mmcsd_close(struct disk *dp) 176 { 177 return 0; 178 } 179 180 static void 181 mmcsd_strategy(struct bio *bp) 182 { 183 struct mmcsd_softc *sc; 184 185 sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1; 186 MMCSD_LOCK(sc); 187 bioq_disksort(&sc->bio_queue, bp); 188 wakeup(sc); 189 MMCSD_UNLOCK(sc); 190 } 191 192 static void 193 mmcsd_task(void *arg) 194 { 195 struct mmcsd_softc *sc = (struct mmcsd_softc*)arg; 196 struct bio *bp; 197 int sz; 198 daddr_t block, end; 199 struct mmc_command cmd; 200 struct mmc_command stop; 201 struct mmc_request req; 202 struct mmc_data data; 203 device_t dev; 204 205 dev = sc->dev; 206 while (sc->running) { 207 MMCSD_LOCK(sc); 208 do { 209 bp = bioq_first(&sc->bio_queue); 210 if (bp == NULL) 211 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0); 212 } while (bp == NULL && sc->running); 213 if (bp) 214 bioq_remove(&sc->bio_queue, bp); 215 MMCSD_UNLOCK(sc); 216 if (!sc->running) 217 break; 218 MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev); 219 // printf("mmc_task: request %p for block %lld\n", bp, bp->bio_pblkno); 220 sz = sc->disk->d_sectorsize; 221 end = bp->bio_pblkno + (bp->bio_bcount / sz); 222 // XXX should use read/write_mulit 223 for (block = bp->bio_pblkno; block < end;) { 224 char *vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz; 225 memset(&req, 0, sizeof(req)); 226 memset(&cmd, 0, sizeof(cmd)); 227 memset(&stop, 0, sizeof(stop)); 228 req.cmd = &cmd; 229 cmd.data = &data; 230 if (bp->bio_cmd == BIO_READ) { 231 #ifdef MULTI_BLOCK_READ 232 if (end - block > 1) 233 cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 234 else 235 cmd.opcode = MMC_READ_SINGLE_BLOCK; 236 #else 237 cmd.opcode = MMC_READ_SINGLE_BLOCK; 238 #endif 239 } else 240 cmd.opcode = MMC_WRITE_BLOCK; 241 cmd.arg = block << 9; 242 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 243 // data.timeout_ns = ; 244 // data.timeout_clks = ; 245 data.data = vaddr; 246 data.mrq = &req; 247 if (bp->bio_cmd == BIO_READ) { 248 data.flags = MMC_DATA_READ; 249 #ifdef MULTI_BLOCK_READ 250 data.len = bp->bio_bcount; 251 if (end - block > 1) { 252 req.stop = &stop; 253 data.flags |= MMC_DATA_MULTI; 254 } 255 printf("Len %d %lld-%lld flags %#x sz %d\n", 256 data.len, block, end, data.flags, sz); 257 block = end; 258 #else 259 data.len = sz; 260 block++; 261 #endif 262 } else { 263 data.flags = MMC_DATA_WRITE; 264 data.len = sz; 265 block++; 266 } 267 stop.opcode = MMC_STOP_TRANSMISSION; 268 stop.arg = 0; 269 stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 270 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 271 &req); 272 // XXX error handling 273 //XXX while (!(mmc_send_status(dev) & R1_READY_FOR_DATA)) 274 // continue; 275 // XXX decode mmc status 276 } 277 MMCBUS_RELEASE_BUS(device_get_parent(dev), dev); 278 biodone(bp); 279 } 280 281 /* tell parent we're done */ 282 MMCSD_LOCK(sc); 283 sc->running = -1; 284 wakeup(sc); 285 MMCSD_UNLOCK(sc); 286 287 kproc_exit(0); 288 } 289 290 static device_method_t mmcsd_methods[] = { 291 DEVMETHOD(device_probe, mmcsd_probe), 292 DEVMETHOD(device_attach, mmcsd_attach), 293 DEVMETHOD(device_detach, mmcsd_detach), 294 {0, 0}, 295 }; 296 297 static driver_t mmcsd_driver = { 298 "mmcsd", 299 mmcsd_methods, 300 sizeof(struct mmcsd_softc), 301 }; 302 static devclass_t mmcsd_devclass; 303 304 305 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0); 306