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 * $FreeBSD: src/sys/dev/mmc/mmcsd.c,v 1.20 2009/02/17 19:17:25 mav Exp $ 53 */ 54 55 #include <sys/param.h> 56 #include <sys/systm.h> 57 #include <sys/buf.h> 58 #include <sys/bus.h> 59 #include <sys/conf.h> 60 #include <sys/disk.h> 61 #include <sys/device.h> 62 #include <sys/devicestat.h> 63 #include <sys/kernel.h> 64 #include <sys/kthread.h> 65 #include <sys/lock.h> 66 #include <sys/malloc.h> 67 #include <sys/module.h> 68 #include <sys/spinlock.h> 69 70 #include <sys/buf2.h> 71 72 #include <bus/mmc/mmcvar.h> 73 #include <bus/mmc/mmcreg.h> 74 75 #include "mmcbus_if.h" 76 77 struct mmcsd_softc { 78 device_t dev; 79 cdev_t dev_t; 80 struct lock sc_lock; 81 struct disk disk; 82 struct devstat device_stats; 83 struct thread *td; 84 struct bio_queue_head bio_queue; 85 daddr_t eblock, eend; /* Range remaining after the last erase. */ 86 int running; 87 int suspend; 88 }; 89 90 /* bus entry points */ 91 static int mmcsd_probe(device_t dev); 92 static int mmcsd_attach(device_t dev); 93 static int mmcsd_detach(device_t dev); 94 95 /* disk routines */ 96 static d_open_t mmcsd_open; 97 static d_close_t mmcsd_close; 98 static d_strategy_t mmcsd_strategy; 99 static d_dump_t mmcsd_dump; 100 101 static void mmcsd_task(void *arg); 102 103 static const char *mmcsd_card_name(device_t dev); 104 static int mmcsd_bus_bit_width(device_t dev); 105 106 #define MMCSD_LOCK(_sc) lockmgr(&(_sc)->sc_lock, LK_EXCLUSIVE) 107 #define MMCSD_UNLOCK(_sc) lockmgr(&(_sc)->sc_lock, LK_RELEASE) 108 #define MMCSD_LOCK_INIT(_sc) lockinit(&(_sc)->sc_lock, "mmcsd", 0, LK_CANRECURSE) 109 #define MMCSD_LOCK_DESTROY(_sc) lockuninit(&(_sc)->sc_lock); 110 #define MMCSD_ASSERT_LOCKED(_sc) KKASSERT(lockstatus(&(_sc)->sc_lock, curthread) != 0); 111 #define MMCSD_ASSERT_UNLOCKED(_sc) KKASSERT(lockstatus(&(_sc)->sc_lock, curthread) == 0); 112 113 static struct dev_ops mmcsd_ops = { 114 { "mmcsd", 0, D_DISK | D_MPSAFE }, 115 .d_open = mmcsd_open, 116 .d_close = mmcsd_close, 117 .d_strategy = mmcsd_strategy, 118 .d_dump = mmcsd_dump, 119 }; 120 121 static int 122 mmcsd_probe(device_t dev) 123 { 124 125 device_quiet(dev); 126 device_set_desc(dev, "MMC/SD Memory Card"); 127 return (0); 128 } 129 130 static int 131 mmcsd_attach(device_t dev) 132 { 133 struct mmcsd_softc *sc; 134 struct disk_info info; 135 cdev_t dsk; 136 intmax_t mb; 137 char unit; 138 int sector_size; 139 140 sc = device_get_softc(dev); 141 sc->dev = dev; 142 MMCSD_LOCK_INIT(sc); 143 144 sector_size = mmc_get_sector_size(dev); 145 devstat_add_entry(&sc->device_stats, "mmcsd", device_get_unit(dev), 146 sector_size, DEVSTAT_NO_ORDERED_TAGS, 147 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, 148 DEVSTAT_PRIORITY_DISK); 149 150 bioq_init(&sc->bio_queue); 151 152 sc->running = 1; 153 sc->suspend = 0; 154 sc->eblock = sc->eend = 0; 155 kthread_create(mmcsd_task, sc, &sc->td, "mmc/sd card task"); 156 157 /* 158 * Probe capacity 159 */ 160 bzero(&info, sizeof(info)); 161 info.d_media_blksize = sector_size; 162 info.d_media_blocks = mmc_get_media_size(dev); 163 info.d_secpertrack = 1024; 164 info.d_nheads = 1; 165 info.d_secpercyl = info.d_secpertrack * info.d_nheads; 166 info.d_ncylinders = (u_int)(info.d_media_blocks / info.d_secpercyl); 167 168 /* 169 * Display in most natural units. There's no cards < 1MB. 170 * The SD standard goes to 2GiB, but the data format supports 171 * up to 4GiB and some card makers push it up to this limit. 172 * The SDHC standard only goes to 32GiB (the data format in 173 * SDHC is good to 2TiB however, which isn't too ugly at 174 * 2048GiBm, so we note it in passing here and don't add the 175 * code to print TiB). 176 * 177 * 1MiB == 1 << 20 178 */ 179 mb = (info.d_media_blksize * info.d_media_blocks) >> 20; 180 unit = 'M'; 181 if (mb >= 10240) { /* 1GiB = 1024 MiB */ 182 unit = 'G'; 183 mb /= 1024; 184 } 185 186 device_printf(dev, "%ju%cB <%s Memory Card>%s at %s %dMHz/%dbit\n", 187 mb, unit, mmcsd_card_name(dev), 188 mmc_get_read_only(dev) ? " (read-only)" : "", 189 device_get_nameunit(device_get_parent(dev)), 190 mmc_get_tran_speed(dev) / 1000000, mmcsd_bus_bit_width(dev)); 191 192 /* 193 * SC is fully initialized, we can attach the drive now. The 194 * instant we do the kernel will start probing it. 195 */ 196 dsk = disk_create(device_get_unit(dev), &sc->disk, &mmcsd_ops); 197 dsk->si_drv1 = sc; 198 sc->dev_t = dsk; 199 200 /* Maximum defined SD card AU size. */ 201 dsk->si_iosize_max = 4*1024*1024; 202 203 disk_setdiskinfo(&sc->disk, &info); 204 205 return (0); 206 } 207 208 static int 209 mmcsd_detach(device_t dev) 210 { 211 struct mmcsd_softc *sc = device_get_softc(dev); 212 struct buf *q_bp; 213 struct bio *q_bio; 214 215 MMCSD_LOCK(sc); 216 sc->suspend = 0; 217 if (sc->running > 0) { 218 /* kill thread */ 219 sc->running = 0; 220 wakeup(sc); 221 /* wait for thread to finish. */ 222 while (sc->running != -1) 223 lksleep(sc, &sc->sc_lock, 0, "detach", 0); 224 } 225 MMCSD_UNLOCK(sc); 226 227 /* 228 * Flush the request queue. 229 * 230 * XXX: Return all queued I/O with ENXIO. Is this correct? 231 */ 232 while ((q_bio = bioq_takefirst(&sc->bio_queue)) != NULL) { 233 q_bp = q_bio->bio_buf; 234 q_bp->b_resid = q_bp->b_bcount; 235 q_bp->b_error = ENXIO; 236 q_bp->b_flags |= B_ERROR; 237 biodone(q_bio); 238 } 239 240 /* kill disk */ 241 disk_destroy(&sc->disk); 242 devstat_remove_entry(&sc->device_stats); 243 244 MMCSD_LOCK_DESTROY(sc); 245 246 return (0); 247 } 248 249 static int 250 mmcsd_suspend(device_t dev) 251 { 252 struct mmcsd_softc *sc = device_get_softc(dev); 253 254 MMCSD_LOCK(sc); 255 sc->suspend = 1; 256 if (sc->running > 0) { 257 /* kill thread */ 258 sc->running = 0; 259 wakeup(sc); 260 /* wait for thread to finish. */ 261 while (sc->running != -1) 262 lksleep(sc, &sc->sc_lock, 0, "detach", 0); 263 } 264 MMCSD_UNLOCK(sc); 265 return (0); 266 } 267 268 static int 269 mmcsd_resume(device_t dev) 270 { 271 struct mmcsd_softc *sc = device_get_softc(dev); 272 273 MMCSD_LOCK(sc); 274 sc->suspend = 0; 275 if (sc->running <= 0) { 276 sc->running = 1; 277 MMCSD_UNLOCK(sc); 278 kthread_create(mmcsd_task, sc, &sc->td, "mmc/sd card task"); 279 } else 280 MMCSD_UNLOCK(sc); 281 return (0); 282 } 283 284 static int 285 mmcsd_open(struct dev_open_args *ap) 286 { 287 return (0); 288 } 289 290 static int 291 mmcsd_close(struct dev_close_args *ap) 292 { 293 return (0); 294 } 295 296 static int 297 mmcsd_strategy(struct dev_strategy_args *ap) 298 { 299 struct mmcsd_softc *sc; 300 struct bio *bio = ap->a_bio; 301 struct buf *bp = bio->bio_buf; 302 303 sc = (struct mmcsd_softc *)ap->a_head.a_dev->si_drv1; 304 MMCSD_LOCK(sc); 305 if (sc->running > 0 || sc->suspend > 0) { 306 bioqdisksort(&sc->bio_queue, bio); 307 MMCSD_UNLOCK(sc); 308 wakeup(sc); 309 } else { 310 MMCSD_UNLOCK(sc); 311 bp->b_error = ENXIO; 312 bp->b_flags |= B_ERROR; 313 bp->b_resid = bp->b_bcount; 314 biodone(bio); 315 } 316 return (0); 317 } 318 319 static daddr_t 320 mmcsd_rw(struct mmcsd_softc *sc, struct bio *bio) 321 { 322 daddr_t block, end; 323 struct mmc_command cmd; 324 struct mmc_command stop; 325 struct mmc_request req; 326 struct mmc_data data; 327 device_t dev = sc->dev; 328 int sz = sc->disk.d_info.d_media_blksize; 329 struct buf *bp = bio->bio_buf; 330 331 block = bio->bio_offset / sz; 332 end = block + (bp->b_bcount / sz); 333 while (block < end) { 334 char *vaddr = bp->b_data + 335 (block - (bio->bio_offset / sz)) * sz; 336 int numblocks = min(end - block, mmc_get_max_data(dev)); 337 memset(&req, 0, sizeof(req)); 338 memset(&cmd, 0, sizeof(cmd)); 339 memset(&stop, 0, sizeof(stop)); 340 req.cmd = &cmd; 341 cmd.data = &data; 342 if (bp->b_cmd == BUF_CMD_READ) { 343 if (numblocks > 1) 344 cmd.opcode = MMC_READ_MULTIPLE_BLOCK; 345 else 346 cmd.opcode = MMC_READ_SINGLE_BLOCK; 347 } else { 348 if (numblocks > 1) 349 cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; 350 else 351 cmd.opcode = MMC_WRITE_BLOCK; 352 } 353 cmd.arg = block; 354 if (!mmc_get_high_cap(dev)) 355 cmd.arg <<= 9; 356 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 357 data.data = vaddr; 358 data.mrq = &req; 359 if (bp->b_cmd == BUF_CMD_READ) 360 data.flags = MMC_DATA_READ; 361 else 362 data.flags = MMC_DATA_WRITE; 363 data.len = numblocks * sz; 364 if (numblocks > 1) { 365 data.flags |= MMC_DATA_MULTI; 366 stop.opcode = MMC_STOP_TRANSMISSION; 367 stop.arg = 0; 368 stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 369 req.stop = &stop; 370 } 371 // kprintf("Len %d %lld-%lld flags %#x sz %d\n", 372 // (int)data.len, (long long)block, (long long)end, data.flags, sz); 373 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, &req); 374 if (req.cmd->error != MMC_ERR_NONE) 375 break; 376 block += numblocks; 377 } 378 return (block); 379 } 380 381 static daddr_t 382 mmcsd_delete(struct mmcsd_softc *sc, struct bio *bio) 383 { 384 daddr_t block, end, start, stop; 385 struct mmc_command cmd; 386 struct mmc_request req; 387 device_t dev = sc->dev; 388 int sz = sc->disk.d_info.d_media_blksize; 389 int erase_sector; 390 struct buf *bp = bio->bio_buf; 391 392 block = bio->bio_offset / sz; 393 end = block + (bp->b_bcount / sz); 394 /* Coalesce with part remaining from previous request. */ 395 if (block > sc->eblock && block <= sc->eend) 396 block = sc->eblock; 397 if (end >= sc->eblock && end < sc->eend) 398 end = sc->eend; 399 /* Safe round to the erase sector boundaries. */ 400 erase_sector = mmc_get_erase_sector(dev); 401 start = block + erase_sector - 1; /* Round up. */ 402 start -= start % erase_sector; 403 stop = end; /* Round down. */ 404 stop -= end % erase_sector; 405 /* We can't erase area smaller then sector, store it for later. */ 406 if (start >= stop) { 407 sc->eblock = block; 408 sc->eend = end; 409 return (end); 410 } 411 412 /* Set erase start position. */ 413 memset(&req, 0, sizeof(req)); 414 memset(&cmd, 0, sizeof(cmd)); 415 req.cmd = &cmd; 416 if (mmc_get_card_type(dev) == mode_sd) 417 cmd.opcode = SD_ERASE_WR_BLK_START; 418 else 419 cmd.opcode = MMC_ERASE_GROUP_START; 420 cmd.arg = start; 421 if (!mmc_get_high_cap(dev)) 422 cmd.arg <<= 9; 423 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 424 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, &req); 425 if (req.cmd->error != MMC_ERR_NONE) { 426 kprintf("erase err1: %d\n", req.cmd->error); 427 return (block); 428 } 429 /* Set erase stop position. */ 430 memset(&req, 0, sizeof(req)); 431 memset(&cmd, 0, sizeof(cmd)); 432 req.cmd = &cmd; 433 if (mmc_get_card_type(dev) == mode_sd) 434 cmd.opcode = SD_ERASE_WR_BLK_END; 435 else 436 cmd.opcode = MMC_ERASE_GROUP_END; 437 cmd.arg = stop; 438 if (!mmc_get_high_cap(dev)) 439 cmd.arg <<= 9; 440 cmd.arg--; 441 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 442 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, &req); 443 if (req.cmd->error != MMC_ERR_NONE) { 444 kprintf("erase err2: %d\n", req.cmd->error); 445 return (block); 446 } 447 /* Erase range. */ 448 memset(&req, 0, sizeof(req)); 449 memset(&cmd, 0, sizeof(cmd)); 450 req.cmd = &cmd; 451 cmd.opcode = MMC_ERASE; 452 cmd.arg = 0; 453 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 454 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev, &req); 455 if (req.cmd->error != MMC_ERR_NONE) { 456 kprintf("erase err3 %d\n", req.cmd->error); 457 return (block); 458 } 459 /* Store one of remaining parts for the next call. */ 460 if ((bio->bio_offset / sz) >= sc->eblock || block == start) { 461 sc->eblock = stop; /* Predict next forward. */ 462 sc->eend = end; 463 } else { 464 sc->eblock = block; /* Predict next backward. */ 465 sc->eend = start; 466 } 467 return (end); 468 } 469 470 static int 471 mmcsd_dump(struct dev_dump_args *ap) 472 { 473 #if 0 474 cdev_t cdev = ap->a_head.a_dev; 475 struct mmcsd_softc *sc = (struct mmcsd_softc *)cdev->si_drv1; 476 device_t dev = sc->dev; 477 struct bio bp; 478 daddr_t block, end; 479 int length = ap->a_length; 480 481 /* length zero is special and really means flush buffers to media */ 482 if (!length) 483 return (0); 484 485 bzero(&bp, sizeof(struct bio)); 486 bp.bio_driver_info = cdev; 487 bp.bio_pblkno = offset / sc->disk->d_sectorsize; 488 bp.bio_bcount = length; 489 bp.bio_data = virtual; 490 bp.bio_cmd = BIO_WRITE; 491 end = bp.bio_pblkno + bp.bio_bcount / sc->disk.d_info.d_media_blksize; 492 MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev); 493 block = mmcsd_rw(sc, &bp); 494 MMCBUS_RELEASE_BUS(device_get_parent(dev), dev); 495 return ((end < block) ? EIO : 0); 496 #endif 497 return EIO; 498 } 499 500 static void 501 mmcsd_task(void *arg) 502 { 503 struct mmcsd_softc *sc = (struct mmcsd_softc*)arg; 504 struct bio *bio; 505 struct buf *bp; 506 int sz; 507 daddr_t block, end; 508 device_t dev; 509 510 dev = sc->dev; 511 512 while (1) { 513 MMCSD_LOCK(sc); 514 do { 515 if (sc->running == 0) 516 goto out; 517 bio = bioq_takefirst(&sc->bio_queue); 518 if (bio == NULL) 519 lksleep(sc, &sc->sc_lock, 0, "jobqueue", 0); 520 } while (bio == NULL); 521 MMCSD_UNLOCK(sc); 522 bp = bio->bio_buf; 523 devstat_start_transaction(&sc->device_stats); 524 if (bp->b_cmd != BUF_CMD_READ && mmc_get_read_only(dev)) { 525 bp->b_error = EROFS; 526 bp->b_resid = bp->b_bcount; 527 bp->b_flags |= B_ERROR; 528 devstat_end_transaction_buf(&sc->device_stats, bp); 529 biodone(bio); 530 continue; 531 } 532 MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev); 533 sz = sc->disk.d_info.d_media_blksize; 534 block = bio->bio_offset / sz; 535 end = block + (bp->b_bcount / sz); 536 if (bp->b_cmd == BUF_CMD_READ || 537 bp->b_cmd == BUF_CMD_WRITE) { 538 /* Access to the remaining erase block obsoletes it. */ 539 if (block < sc->eend && end > sc->eblock) 540 sc->eblock = sc->eend = 0; 541 block = mmcsd_rw(sc, bio); 542 } else if (bp->b_cmd == BUF_CMD_FREEBLKS) { 543 block = mmcsd_delete(sc, bio); 544 } 545 MMCBUS_RELEASE_BUS(device_get_parent(dev), dev); 546 if (block < end) { 547 bp->b_error = EIO; 548 bp->b_resid = (end - block) * sz; 549 bp->b_flags |= B_ERROR; 550 } else { 551 bp->b_resid = 0; 552 } 553 devstat_end_transaction_buf(&sc->device_stats, bp); 554 biodone(bio); 555 } 556 out: 557 /* tell parent we're done */ 558 sc->running = -1; 559 MMCSD_UNLOCK(sc); 560 wakeup(sc); 561 } 562 563 static const char * 564 mmcsd_card_name(device_t dev) 565 { 566 if (mmc_get_card_type(dev) == mode_mmc) 567 return ("MMC"); 568 if (mmc_get_high_cap(dev)) 569 return ("SDHC"); 570 return ("SD"); 571 } 572 573 static int 574 mmcsd_bus_bit_width(device_t dev) 575 { 576 if (mmc_get_bus_width(dev) == bus_width_1) 577 return (1); 578 if (mmc_get_bus_width(dev) == bus_width_4) 579 return (4); 580 return (8); 581 } 582 583 static device_method_t mmcsd_methods[] = { 584 DEVMETHOD(device_probe, mmcsd_probe), 585 DEVMETHOD(device_attach, mmcsd_attach), 586 DEVMETHOD(device_detach, mmcsd_detach), 587 DEVMETHOD(device_suspend, mmcsd_suspend), 588 DEVMETHOD(device_resume, mmcsd_resume), 589 DEVMETHOD_END 590 }; 591 592 static driver_t mmcsd_driver = { 593 "mmcsd", 594 mmcsd_methods, 595 sizeof(struct mmcsd_softc), 596 }; 597 static devclass_t mmcsd_devclass; 598 599 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL); 600