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