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 daddr_t eblock, eend; /* Range remaining after the last erase. */ 81 int running; 82 }; 83 84 /* bus entry points */ 85 static int mmcsd_probe(device_t dev); 86 static int mmcsd_attach(device_t dev); 87 static int mmcsd_detach(device_t dev); 88 89 /* disk routines */ 90 static int mmcsd_open(struct disk *dp); 91 static int mmcsd_close(struct disk *dp); 92 static void mmcsd_strategy(struct bio *bp); 93 static void mmcsd_task(void *arg); 94 95 static const char *mmcsd_card_name(device_t dev); 96 static int mmcsd_bus_bit_width(device_t dev); 97 98 #define MMCSD_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 99 #define MMCSD_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 100 #define MMCSD_LOCK_INIT(_sc) \ 101 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 102 "mmcsd", MTX_DEF) 103 #define MMCSD_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 104 #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 105 #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 106 107 static int 108 mmcsd_probe(device_t dev) 109 { 110 111 device_quiet(dev); 112 device_set_desc(dev, "MMC/SD Memory Card"); 113 return (0); 114 } 115 116 static int 117 mmcsd_attach(device_t dev) 118 { 119 struct mmcsd_softc *sc; 120 struct disk *d; 121 intmax_t mb; 122 char unit; 123 124 sc = device_get_softc(dev); 125 sc->dev = dev; 126 MMCSD_LOCK_INIT(sc); 127 128 d = sc->disk = disk_alloc(); 129 d->d_open = mmcsd_open; 130 d->d_close = mmcsd_close; 131 d->d_strategy = mmcsd_strategy; 132 // d->d_dump = mmcsd_dump; Need polling mmc layer 133 d->d_name = "mmcsd"; 134 d->d_drv1 = sc; 135 d->d_maxsize = 4*1024*1024; /* Maximum defined SD card AU size. */ 136 d->d_sectorsize = mmc_get_sector_size(dev); 137 d->d_mediasize = mmc_get_media_size(dev) * d->d_sectorsize; 138 d->d_unit = device_get_unit(dev); 139 d->d_flags = DISKFLAG_CANDELETE; 140 /* 141 * Display in most natural units. There's no cards < 1MB. 142 * The SD standard goes to 2GiB, but the data format supports 143 * up to 4GiB and some card makers push it up to this limit. 144 * The SDHC standard only goes to 32GiB (the data format in 145 * SDHC is good to 2TiB however, which isn't too ugly at 146 * 2048GiBm, so we note it in passing here and don't add the 147 * code to print TiB). 148 */ 149 mb = d->d_mediasize >> 20; /* 1MiB == 1 << 20 */ 150 unit = 'M'; 151 if (mb >= 10240) { /* 1GiB = 1024 MiB */ 152 unit = 'G'; 153 mb /= 1024; 154 } 155 device_printf(dev, "%ju%cB <%s Memory Card>%s at %s %dMHz/%dbit\n", 156 mb, unit, mmcsd_card_name(dev), 157 mmc_get_read_only(dev) ? " (read-only)" : "", 158 device_get_nameunit(device_get_parent(dev)), 159 mmc_get_tran_speed(dev) / 1000000, mmcsd_bus_bit_width(dev)); 160 disk_create(d, DISK_VERSION); 161 bioq_init(&sc->bio_queue); 162 163 sc->running = 1; 164 sc->eblock = sc->eend = 0; 165 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card"); 166 167 return (0); 168 } 169 170 static int 171 mmcsd_detach(device_t dev) 172 { 173 struct mmcsd_softc *sc = device_get_softc(dev); 174 175 /* kill thread */ 176 MMCSD_LOCK(sc); 177 sc->running = 0; 178 wakeup(sc); 179 MMCSD_UNLOCK(sc); 180 181 /* wait for thread to finish. XXX probably want timeout. -sorbo */ 182 MMCSD_LOCK(sc); 183 while (sc->running != -1) 184 msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0); 185 MMCSD_UNLOCK(sc); 186 187 /* kill disk */ 188 disk_destroy(sc->disk); 189 /* XXX destroy anything in queue */ 190 191 MMCSD_LOCK_DESTROY(sc); 192 193 return (0); 194 } 195 196 static int 197 mmcsd_open(struct disk *dp) 198 { 199 return (0); 200 } 201 202 static int 203 mmcsd_close(struct disk *dp) 204 { 205 return (0); 206 } 207 208 static void 209 mmcsd_strategy(struct bio *bp) 210 { 211 struct mmcsd_softc *sc; 212 213 sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1; 214 MMCSD_LOCK(sc); 215 bioq_disksort(&sc->bio_queue, bp); 216 wakeup(sc); 217 MMCSD_UNLOCK(sc); 218 } 219 220 static daddr_t 221 mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp) 222 { 223 daddr_t block, end; 224 struct mmc_command cmd; 225 struct mmc_command stop; 226 struct mmc_request req; 227 struct mmc_data data; 228 device_t dev = sc->dev; 229 int sz = sc->disk->d_sectorsize; 230 231 block = bp->bio_pblkno; 232 end = bp->bio_pblkno + (bp->bio_bcount / sz); 233 while (block < end) { 234 char *vaddr = bp->bio_data + 235 (block - bp->bio_pblkno) * sz; 236 int numblocks = min(end - block, mmc_get_max_data(dev)); 237 memset(&req, 0, sizeof(req)); 238 memset(&cmd, 0, sizeof(cmd)); 239 memset(&stop, 0, sizeof(stop)); 240 req.cmd = &cmd; 241 cmd.data = &data; 242 if (bp->bio_cmd == BIO_READ) { 243 if (numblocks > 1) 244 cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 245 else 246 cmd.opcode = MMC_READ_SINGLE_BLOCK; 247 } else { 248 if (numblocks > 1) 249 cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; 250 else 251 cmd.opcode = MMC_WRITE_BLOCK; 252 } 253 cmd.arg = block; 254 if (!mmc_get_high_cap(dev)) 255 cmd.arg <<= 9; 256 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 257 data.data = vaddr; 258 data.mrq = &req; 259 if (bp->bio_cmd == BIO_READ) 260 data.flags = MMC_DATA_READ; 261 else 262 data.flags = MMC_DATA_WRITE; 263 data.len = numblocks * sz; 264 if (numblocks > 1) { 265 data.flags |= MMC_DATA_MULTI; 266 stop.opcode = MMC_STOP_TRANSMISSION; 267 stop.arg = 0; 268 stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 269 req.stop = &stop; 270 } 271 // printf("Len %d %lld-%lld flags %#x sz %d\n", 272 // (int)data.len, (long long)block, (long long)end, data.flags, sz); 273 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 274 &req); 275 if (req.cmd->error != MMC_ERR_NONE) 276 break; 277 block += numblocks; 278 } 279 return (block); 280 } 281 282 static daddr_t 283 mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp) 284 { 285 daddr_t block, end, start, stop; 286 struct mmc_command cmd; 287 struct mmc_request req; 288 device_t dev = sc->dev; 289 int sz = sc->disk->d_sectorsize; 290 int erase_sector; 291 292 block = bp->bio_pblkno; 293 end = bp->bio_pblkno + (bp->bio_bcount / sz); 294 /* Coalesce with part remaining from previous request. */ 295 if (block > sc->eblock && block <= sc->eend) 296 block = sc->eblock; 297 if (end >= sc->eblock && end < sc->eend) 298 end = sc->eend; 299 /* Safe round to the erase sector boundaries. */ 300 erase_sector = mmc_get_erase_sector(dev); 301 start = block + erase_sector - 1; /* Round up. */ 302 start -= start % erase_sector; 303 stop = end; /* Round down. */ 304 stop -= end % erase_sector; 305 /* We can't erase area smaller then sector, store it for later. */ 306 if (start >= stop) { 307 sc->eblock = block; 308 sc->eend = end; 309 return (end); 310 } 311 312 /* Set erase start position. */ 313 memset(&req, 0, sizeof(req)); 314 memset(&cmd, 0, sizeof(cmd)); 315 req.cmd = &cmd; 316 if (mmc_get_card_type(dev) == mode_sd) 317 cmd.opcode = SD_ERASE_WR_BLK_START; 318 else 319 cmd.opcode = MMC_ERASE_GROUP_START; 320 cmd.arg = start; 321 if (!mmc_get_high_cap(dev)) 322 cmd.arg <<= 9; 323 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 324 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 325 &req); 326 if (req.cmd->error != MMC_ERR_NONE) { 327 printf("erase err1: %d\n", req.cmd->error); 328 return (block); 329 } 330 /* Set erase stop position. */ 331 memset(&req, 0, sizeof(req)); 332 memset(&cmd, 0, sizeof(cmd)); 333 req.cmd = &cmd; 334 if (mmc_get_card_type(dev) == mode_sd) 335 cmd.opcode = SD_ERASE_WR_BLK_END; 336 else 337 cmd.opcode = MMC_ERASE_GROUP_END; 338 cmd.arg = stop; 339 if (!mmc_get_high_cap(dev)) 340 cmd.arg <<= 9; 341 cmd.arg--; 342 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 343 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 344 &req); 345 if (req.cmd->error != MMC_ERR_NONE) { 346 printf("erase err2: %d\n", req.cmd->error); 347 return (block); 348 } 349 /* Erase range. */ 350 memset(&req, 0, sizeof(req)); 351 memset(&cmd, 0, sizeof(cmd)); 352 req.cmd = &cmd; 353 cmd.opcode = MMC_ERASE; 354 cmd.arg = 0; 355 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 356 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, 357 &req); 358 if (req.cmd->error != MMC_ERR_NONE) { 359 printf("erase err3 %d\n", req.cmd->error); 360 return (block); 361 } 362 /* Store one of remaining parts for the next call. */ 363 if (bp->bio_pblkno >= sc->eblock || block == start) { 364 sc->eblock = stop; /* Predict next forward. */ 365 sc->eend = end; 366 } else { 367 sc->eblock = block; /* Predict next backward. */ 368 sc->eend = start; 369 } 370 return (end); 371 } 372 373 static void 374 mmcsd_task(void *arg) 375 { 376 struct mmcsd_softc *sc = (struct mmcsd_softc*)arg; 377 struct bio *bp; 378 int sz; 379 daddr_t block, end; 380 device_t dev; 381 382 dev = sc->dev; 383 while (sc->running) { 384 MMCSD_LOCK(sc); 385 do { 386 bp = bioq_first(&sc->bio_queue); 387 if (bp == NULL) 388 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0); 389 } while (bp == NULL && sc->running); 390 if (bp) 391 bioq_remove(&sc->bio_queue, bp); 392 MMCSD_UNLOCK(sc); 393 if (!sc->running) 394 break; 395 if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) { 396 bp->bio_error = EROFS; 397 bp->bio_resid = bp->bio_bcount; 398 bp->bio_flags |= BIO_ERROR; 399 biodone(bp); 400 continue; 401 } 402 MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev); 403 sz = sc->disk->d_sectorsize; 404 block = bp->bio_pblkno; 405 end = bp->bio_pblkno + (bp->bio_bcount / sz); 406 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) { 407 /* Access to the remaining erase block obsoletes it. */ 408 if (block < sc->eend && end > sc->eblock) 409 sc->eblock = sc->eend = 0; 410 block = mmcsd_rw(sc, bp); 411 } else if (bp->bio_cmd == BIO_DELETE) { 412 block = mmcsd_delete(sc, bp); 413 } 414 MMCBUS_RELEASE_BUS(device_get_parent(dev), dev); 415 if (block < end) { 416 bp->bio_error = EIO; 417 bp->bio_resid = (end - block) * sz; 418 bp->bio_flags |= BIO_ERROR; 419 } 420 biodone(bp); 421 } 422 423 /* tell parent we're done */ 424 MMCSD_LOCK(sc); 425 sc->running = -1; 426 wakeup(sc); 427 MMCSD_UNLOCK(sc); 428 429 kproc_exit(0); 430 } 431 432 static const char * 433 mmcsd_card_name(device_t dev) 434 { 435 if (mmc_get_card_type(dev) == mode_mmc) 436 return ("MMC"); 437 if (mmc_get_high_cap(dev)) 438 return ("SDHC"); 439 return ("SD"); 440 } 441 442 static int 443 mmcsd_bus_bit_width(device_t dev) 444 { 445 if (mmc_get_bus_width(dev) == bus_width_1) 446 return (1); 447 if (mmc_get_bus_width(dev) == bus_width_4) 448 return (4); 449 return (8); 450 } 451 452 static device_method_t mmcsd_methods[] = { 453 DEVMETHOD(device_probe, mmcsd_probe), 454 DEVMETHOD(device_attach, mmcsd_attach), 455 DEVMETHOD(device_detach, mmcsd_detach), 456 {0, 0}, 457 }; 458 459 static driver_t mmcsd_driver = { 460 "mmcsd", 461 mmcsd_methods, 462 sizeof(struct mmcsd_softc), 463 }; 464 static devclass_t mmcsd_devclass; 465 466 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0); 467