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