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/mmcvar.h>
70 #include <dev/mmc/mmcreg.h>
72 #include "mmcbus_if.h"
79 struct bio_queue_head bio_queue;
80 daddr_t eblock, eend; /* Range remaining after the last erase. */
84 #define MULTI_BLOCK_BROKEN
86 /* bus entry points */
87 static int mmcsd_probe(device_t dev);
88 static int mmcsd_attach(device_t dev);
89 static int mmcsd_detach(device_t dev);
92 static int mmcsd_open(struct disk *dp);
93 static int mmcsd_close(struct disk *dp);
94 static void mmcsd_strategy(struct bio *bp);
95 static void mmcsd_task(void *arg);
97 static const char *mmcsd_card_name(device_t dev);
98 static int mmcsd_bus_bit_width(device_t dev);
100 #define MMCSD_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
101 #define MMCSD_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
102 #define MMCSD_LOCK_INIT(_sc) \
103 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
105 #define MMCSD_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
106 #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
107 #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
110 mmcsd_probe(device_t dev)
114 device_set_desc(dev, "MMC/SD Memory Card");
119 mmcsd_attach(device_t dev)
121 struct mmcsd_softc *sc;
126 sc = device_get_softc(dev);
130 d = sc->disk = disk_alloc();
131 d->d_open = mmcsd_open;
132 d->d_close = mmcsd_close;
133 d->d_strategy = mmcsd_strategy;
134 // d->d_dump = mmcsd_dump; Need polling mmc layer
137 d->d_maxsize = 4*1024*1024; /* Maximum defined SD card AU size. */
138 d->d_sectorsize = mmc_get_sector_size(dev);
139 d->d_mediasize = mmc_get_media_size(dev) * d->d_sectorsize;
140 d->d_unit = device_get_unit(dev);
141 d->d_flags = DISKFLAG_CANDELETE;
143 * Display in most natural units. There's no cards < 1MB.
144 * The SD standard goes to 2GiB, but the data format supports
145 * up to 4GiB and some card makers push it up to this limit.
146 * The SDHC standard only goes to 32GiB (the data format in
147 * SDHC is good to 2TiB however, which isn't too ugly at
148 * 2048GiBm, so we note it in passing here and don't add the
149 * code to print TiB).
151 mb = d->d_mediasize >> 20; /* 1MiB == 1 << 20 */
153 if (mb >= 10240) { /* 1GiB = 1024 MiB */
157 device_printf(dev, "%ju%cB <%s Memory Card>%s at %s %dMHz/%dbit\n",
158 mb, unit, mmcsd_card_name(dev),
159 mmc_get_read_only(dev) ? " (read-only)" : "",
160 device_get_nameunit(device_get_parent(dev)),
161 mmc_get_tran_speed(dev) / 1000000, mmcsd_bus_bit_width(dev));
162 disk_create(d, DISK_VERSION);
163 bioq_init(&sc->bio_queue);
166 sc->eblock = sc->eend = 0;
167 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
173 mmcsd_detach(device_t dev)
175 struct mmcsd_softc *sc = device_get_softc(dev);
183 /* wait for thread to finish. XXX probably want timeout. -sorbo */
185 while (sc->running != -1)
186 msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0);
190 disk_destroy(sc->disk);
191 /* XXX destroy anything in queue */
193 MMCSD_LOCK_DESTROY(sc);
199 mmcsd_open(struct disk *dp)
205 mmcsd_close(struct disk *dp)
211 mmcsd_strategy(struct bio *bp)
213 struct mmcsd_softc *sc;
215 sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
217 bioq_disksort(&sc->bio_queue, bp);
223 mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp)
226 struct mmc_command cmd;
227 struct mmc_command stop;
228 struct mmc_request req;
229 struct mmc_data data;
230 device_t dev = sc->dev;
231 int sz = sc->disk->d_sectorsize;
233 block = bp->bio_pblkno;
234 end = bp->bio_pblkno + (bp->bio_bcount / sz);
235 while (block < end) {
236 char *vaddr = bp->bio_data +
237 (block - bp->bio_pblkno) * sz;
240 numblocks = end - block;
244 memset(&req, 0, sizeof(req));
245 memset(&cmd, 0, sizeof(cmd));
246 memset(&stop, 0, sizeof(stop));
249 if (bp->bio_cmd == BIO_READ) {
251 cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
253 cmd.opcode = MMC_READ_SINGLE_BLOCK;
256 cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
258 cmd.opcode = MMC_WRITE_BLOCK;
261 if (!mmc_get_high_cap(dev))
263 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
266 if (bp->bio_cmd == BIO_READ)
267 data.flags = MMC_DATA_READ;
269 data.flags = MMC_DATA_WRITE;
270 data.len = numblocks * sz;
272 data.flags |= MMC_DATA_MULTI;
273 stop.opcode = MMC_STOP_TRANSMISSION;
275 stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
278 // printf("Len %d %lld-%lld flags %#x sz %d\n",
279 // (int)data.len, (long long)block, (long long)end, data.flags, sz);
280 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
282 if (req.cmd->error != MMC_ERR_NONE)
290 mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp)
292 daddr_t block, end, start, stop;
293 struct mmc_command cmd;
294 struct mmc_request req;
295 device_t dev = sc->dev;
296 int sz = sc->disk->d_sectorsize;
299 block = bp->bio_pblkno;
300 end = bp->bio_pblkno + (bp->bio_bcount / sz);
301 /* Coalesce with part remaining from previous request. */
302 if (block > sc->eblock && block <= sc->eend)
304 if (end >= sc->eblock && end < sc->eend)
306 /* Safe round to the erase sector boundaries. */
307 erase_sector = mmc_get_erase_sector(dev);
308 start = block + erase_sector - 1; /* Round up. */
309 start -= start % erase_sector;
310 stop = end; /* Round down. */
311 stop -= end % erase_sector;
312 /* We can't erase area smaller then sector, store it for later. */
319 /* Set erase start position. */
320 memset(&req, 0, sizeof(req));
321 memset(&cmd, 0, sizeof(cmd));
323 if (mmc_get_card_type(dev) == mode_sd)
324 cmd.opcode = SD_ERASE_WR_BLK_START;
326 cmd.opcode = MMC_ERASE_GROUP_START;
328 if (!mmc_get_high_cap(dev))
330 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
331 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
333 if (req.cmd->error != MMC_ERR_NONE) {
334 printf("erase err1: %d\n", req.cmd->error);
337 /* Set erase stop position. */
338 memset(&req, 0, sizeof(req));
339 memset(&cmd, 0, sizeof(cmd));
341 if (mmc_get_card_type(dev) == mode_sd)
342 cmd.opcode = SD_ERASE_WR_BLK_END;
344 cmd.opcode = MMC_ERASE_GROUP_END;
346 if (!mmc_get_high_cap(dev))
349 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
350 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
352 if (req.cmd->error != MMC_ERR_NONE) {
353 printf("erase err2: %d\n", req.cmd->error);
357 memset(&req, 0, sizeof(req));
358 memset(&cmd, 0, sizeof(cmd));
360 cmd.opcode = MMC_ERASE;
362 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
363 MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
365 if (req.cmd->error != MMC_ERR_NONE) {
366 printf("erase err3 %d\n", req.cmd->error);
369 /* Store one of remaining parts for the next call. */
370 if (bp->bio_pblkno >= sc->eblock || block == start) {
371 sc->eblock = stop; /* Predict next forward. */
374 sc->eblock = block; /* Predict next backward. */
381 mmcsd_task(void *arg)
383 struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
390 while (sc->running) {
393 bp = bioq_first(&sc->bio_queue);
395 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
396 } while (bp == NULL && sc->running);
398 bioq_remove(&sc->bio_queue, bp);
402 if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
403 bp->bio_error = EROFS;
404 bp->bio_resid = bp->bio_bcount;
405 bp->bio_flags |= BIO_ERROR;
409 MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
410 sz = sc->disk->d_sectorsize;
411 block = bp->bio_pblkno;
412 end = bp->bio_pblkno + (bp->bio_bcount / sz);
413 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
414 /* Access to the remaining erase block obsoletes it. */
415 if (block < sc->eend && end > sc->eblock)
416 sc->eblock = sc->eend = 0;
417 block = mmcsd_rw(sc, bp);
418 } else if (bp->bio_cmd == BIO_DELETE) {
419 block = mmcsd_delete(sc, bp);
421 MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
424 bp->bio_resid = (end - block) * sz;
425 bp->bio_flags |= BIO_ERROR;
430 /* tell parent we're done */
440 mmcsd_card_name(device_t dev)
442 if (mmc_get_card_type(dev) == mode_mmc)
444 if (mmc_get_high_cap(dev))
450 mmcsd_bus_bit_width(device_t dev)
452 if (mmc_get_bus_width(dev) == bus_width_1)
454 if (mmc_get_bus_width(dev) == bus_width_4)
459 static device_method_t mmcsd_methods[] = {
460 DEVMETHOD(device_probe, mmcsd_probe),
461 DEVMETHOD(device_attach, mmcsd_attach),
462 DEVMETHOD(device_detach, mmcsd_detach),
466 static driver_t mmcsd_driver = {
469 sizeof(struct mmcsd_softc),
471 static devclass_t mmcsd_devclass;
473 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0);