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