2 * Copyright (c) 2006 Bernd Walter. All rights reserved.
3 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
25 * Portions of this software may have been developed with reference to
26 * the SD Simplified Specification. The following disclaimer may apply:
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.
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.
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
56 #include <sys/param.h>
57 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/kthread.h>
64 #include <sys/malloc.h>
65 #include <sys/module.h>
66 #include <sys/mutex.h>
67 #include <geom/geom_disk.h>
69 #include <dev/mmc/mmcbrvar.h>
70 #include <dev/mmc/mmcreg.h>
71 #include <dev/mmc/mmcvar.h>
73 #include "mmcbus_if.h"
75 #if __FreeBSD_version < 800002
76 #define kproc_create kthread_create
77 #define kproc_exit kthread_exit
85 struct bio_queue_head bio_queue;
86 daddr_t eblock, eend; /* Range remaining after the last erase. */
91 static const char *errmsg[] =
102 /* bus entry points */
103 static int mmcsd_attach(device_t dev);
104 static int mmcsd_detach(device_t dev);
105 static int mmcsd_probe(device_t dev);
108 static int mmcsd_close(struct disk *dp);
109 static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
110 off_t offset, size_t length);
111 static int mmcsd_open(struct disk *dp);
112 static void mmcsd_strategy(struct bio *bp);
113 static void mmcsd_task(void *arg);
115 static int mmcsd_bus_bit_width(device_t dev);
116 static daddr_t mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp);
117 static daddr_t mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp);
119 #define MMCSD_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
120 #define MMCSD_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
121 #define MMCSD_LOCK_INIT(_sc) \
122 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
124 #define MMCSD_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
125 #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
126 #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
129 mmcsd_probe(device_t dev)
133 device_set_desc(dev, "MMC/SD Memory Card");
138 mmcsd_attach(device_t dev)
140 struct mmcsd_softc *sc;
147 sc = device_get_softc(dev);
151 d = sc->disk = disk_alloc();
152 d->d_open = mmcsd_open;
153 d->d_close = mmcsd_close;
154 d->d_strategy = mmcsd_strategy;
155 d->d_dump = mmcsd_dump;
158 d->d_maxsize = 4*1024*1024; /* Maximum defined SD card AU size. */
159 d->d_sectorsize = mmc_get_sector_size(dev);
160 d->d_mediasize = (off_t)mmc_get_media_size(dev) * d->d_sectorsize;
161 d->d_stripeoffset = 0;
162 d->d_stripesize = mmc_get_erase_sector(dev) * d->d_sectorsize;
163 d->d_unit = device_get_unit(dev);
164 d->d_flags = DISKFLAG_CANDELETE;
165 d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize * 1; /* conservative */
167 * Display in most natural units. There's no cards < 1MB. The SD
168 * standard goes to 2GiB due to its reliance on FAT, but the data
169 * format supports up to 4GiB and some card makers push it up to this
170 * limit. The SDHC standard only goes to 32GiB due to FAT32, but the
171 * data format supports up to 2TiB however. 2048GB isn't too ugly, so
172 * we note it in passing here and don't add the code to print
173 * TB). Since these cards are sold in terms of MB and GB not MiB and
174 * GiB, report them like that. We also round to the nearest unit, since
175 * many cards are a few percent short, even of the power of 10 size.
177 mb = (d->d_mediasize + 1000000 / 2 - 1) / 1000000;
181 mb = (mb + 1000 / 2 - 1) / 1000;
184 * Report the clock speed of the underlying hardware, which might be
185 * different than what the card reports due to hardware limitations.
186 * Report how many blocks the hardware transfers at once.
188 speed = mmcbr_get_clock(device_get_parent(dev));
189 maxblocks = mmc_get_max_data(dev);
190 device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
191 mb, unit, mmc_get_card_id_string(dev),
192 mmc_get_read_only(dev) ? " (read-only)" : "",
193 device_get_nameunit(device_get_parent(dev)),
194 speed / 1000000, (speed / 100000) % 10,
195 mmcsd_bus_bit_width(dev), maxblocks);
196 disk_create(d, DISK_VERSION);
197 bioq_init(&sc->bio_queue);
201 sc->eblock = sc->eend = 0;
202 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card",
203 device_get_nameunit(dev));
209 mmcsd_detach(device_t dev)
211 struct mmcsd_softc *sc = device_get_softc(dev);
215 if (sc->running > 0) {
219 /* wait for thread to finish. */
220 while (sc->running != -1)
221 msleep(sc, &sc->sc_mtx, 0, "detach", 0);
225 /* Flush the request queue. */
226 bioq_flush(&sc->bio_queue, NULL, ENXIO);
228 disk_destroy(sc->disk);
230 MMCSD_LOCK_DESTROY(sc);
236 mmcsd_suspend(device_t dev)
238 struct mmcsd_softc *sc = device_get_softc(dev);
242 if (sc->running > 0) {
246 /* wait for thread to finish. */
247 while (sc->running != -1)
248 msleep(sc, &sc->sc_mtx, 0, "detach", 0);
255 mmcsd_resume(device_t dev)
257 struct mmcsd_softc *sc = device_get_softc(dev);
261 if (sc->running <= 0) {
264 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card",
265 device_get_nameunit(dev));
272 mmcsd_open(struct disk *dp)
279 mmcsd_close(struct disk *dp)
286 mmcsd_strategy(struct bio *bp)
288 struct mmcsd_softc *sc;
290 sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
292 if (sc->running > 0 || sc->suspend > 0) {
293 bioq_disksort(&sc->bio_queue, bp);
298 biofinish(bp, NULL, ENXIO);
305 if (e < 0 || e > MMC_ERR_MAX)
306 return "Bad error code";
311 mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp)
314 struct mmc_command cmd;
315 struct mmc_command stop;
316 struct mmc_request req;
317 struct mmc_data data;
318 device_t dev = sc->dev;
319 int sz = sc->disk->d_sectorsize;
320 device_t mmcbr = device_get_parent(dev);
322 block = bp->bio_pblkno;
323 end = bp->bio_pblkno + (bp->bio_bcount / sz);
324 while (block < end) {
325 char *vaddr = bp->bio_data +
326 (block - bp->bio_pblkno) * sz;
327 int numblocks = min(end - block, mmc_get_max_data(dev));
328 memset(&req, 0, sizeof(req));
329 memset(&cmd, 0, sizeof(cmd));
330 memset(&stop, 0, sizeof(stop));
331 memset(&data, 0, sizeof(data));
335 if (bp->bio_cmd == BIO_READ) {
337 cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
339 cmd.opcode = MMC_READ_SINGLE_BLOCK;
342 cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
344 cmd.opcode = MMC_WRITE_BLOCK;
347 if (!mmc_get_high_cap(dev))
349 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
352 if (bp->bio_cmd == BIO_READ)
353 data.flags = MMC_DATA_READ;
355 data.flags = MMC_DATA_WRITE;
356 data.len = numblocks * sz;
358 data.flags |= MMC_DATA_MULTI;
359 stop.opcode = MMC_STOP_TRANSMISSION;
361 stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
365 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
366 if (req.cmd->error != MMC_ERR_NONE) {
367 device_printf(dev, "Error indicated: %d %s\n",
368 req.cmd->error, mmcsd_errmsg(req.cmd->error));
377 mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp)
379 daddr_t block, end, start, stop;
380 struct mmc_command cmd;
381 struct mmc_request req;
382 device_t dev = sc->dev;
383 int sz = sc->disk->d_sectorsize;
385 device_t mmcbr = device_get_parent(dev);
387 block = bp->bio_pblkno;
388 end = bp->bio_pblkno + (bp->bio_bcount / sz);
389 /* Coalesce with part remaining from previous request. */
390 if (block > sc->eblock && block <= sc->eend)
392 if (end >= sc->eblock && end < sc->eend)
394 /* Safe round to the erase sector boundaries. */
395 erase_sector = mmc_get_erase_sector(dev);
396 start = block + erase_sector - 1; /* Round up. */
397 start -= start % erase_sector;
398 stop = end; /* Round down. */
399 stop -= end % erase_sector;
400 /* We can't erase area smaller then sector, store it for later. */
407 /* Set erase start position. */
408 memset(&req, 0, sizeof(req));
409 memset(&cmd, 0, sizeof(cmd));
412 if (mmc_get_card_type(dev) == mode_sd)
413 cmd.opcode = SD_ERASE_WR_BLK_START;
415 cmd.opcode = MMC_ERASE_GROUP_START;
417 if (!mmc_get_high_cap(dev))
419 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
420 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
421 if (req.cmd->error != MMC_ERR_NONE) {
422 printf("erase err1: %d\n", req.cmd->error);
425 /* Set erase stop position. */
426 memset(&req, 0, sizeof(req));
427 memset(&cmd, 0, sizeof(cmd));
429 if (mmc_get_card_type(dev) == mode_sd)
430 cmd.opcode = SD_ERASE_WR_BLK_END;
432 cmd.opcode = MMC_ERASE_GROUP_END;
434 if (!mmc_get_high_cap(dev))
437 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
438 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
439 if (req.cmd->error != MMC_ERR_NONE) {
440 printf("erase err2: %d\n", req.cmd->error);
444 memset(&req, 0, sizeof(req));
445 memset(&cmd, 0, sizeof(cmd));
447 cmd.opcode = MMC_ERASE;
449 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
450 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
451 if (req.cmd->error != MMC_ERR_NONE) {
452 printf("erase err3 %d\n", req.cmd->error);
455 /* Store one of remaining parts for the next call. */
456 if (bp->bio_pblkno >= sc->eblock || block == start) {
457 sc->eblock = stop; /* Predict next forward. */
460 sc->eblock = block; /* Predict next backward. */
467 mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
468 off_t offset, size_t length)
470 struct disk *disk = arg;
471 struct mmcsd_softc *sc = (struct mmcsd_softc *)disk->d_drv1;
472 device_t dev = sc->dev;
475 device_t mmcbr = device_get_parent(dev);
477 /* length zero is special and really means flush buffers to media */
481 bzero(&bp, sizeof(struct bio));
483 bp.bio_pblkno = offset / 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_sectorsize;
488 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
489 block = mmcsd_rw(sc, &bp);
490 MMCBUS_RELEASE_BUS(mmcbr, dev);
491 return ((end < block) ? EIO : 0);
495 mmcsd_task(void *arg)
497 struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
501 device_t dev = sc->dev;
502 device_t mmcbr = device_get_parent(sc->dev);
507 if (sc->running == 0)
509 bp = bioq_takefirst(&sc->bio_queue);
511 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
512 } while (bp == NULL);
514 if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
515 bp->bio_error = EROFS;
516 bp->bio_resid = bp->bio_bcount;
517 bp->bio_flags |= BIO_ERROR;
521 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
522 sz = sc->disk->d_sectorsize;
523 block = bp->bio_pblkno;
524 end = bp->bio_pblkno + (bp->bio_bcount / sz);
525 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
526 /* Access to the remaining erase block obsoletes it. */
527 if (block < sc->eend && end > sc->eblock)
528 sc->eblock = sc->eend = 0;
529 block = mmcsd_rw(sc, bp);
530 } else if (bp->bio_cmd == BIO_DELETE) {
531 block = mmcsd_delete(sc, bp);
533 MMCBUS_RELEASE_BUS(mmcbr, dev);
536 bp->bio_resid = (end - block) * sz;
537 bp->bio_flags |= BIO_ERROR;
542 /* tell parent we're done */
551 mmcsd_bus_bit_width(device_t dev)
554 if (mmc_get_bus_width(dev) == bus_width_1)
556 if (mmc_get_bus_width(dev) == bus_width_4)
561 static device_method_t mmcsd_methods[] = {
562 DEVMETHOD(device_probe, mmcsd_probe),
563 DEVMETHOD(device_attach, mmcsd_attach),
564 DEVMETHOD(device_detach, mmcsd_detach),
565 DEVMETHOD(device_suspend, mmcsd_suspend),
566 DEVMETHOD(device_resume, mmcsd_resume),
570 static driver_t mmcsd_driver = {
573 sizeof(struct mmcsd_softc),
575 static devclass_t mmcsd_devclass;
577 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL);