xref: /freebsd/sys/dev/mmc/mmcsd.c (revision c18f1e262718976ebf0abd2fc10db526fb4523ae)
1114b4164SWarner Losh /*-
2114b4164SWarner Losh  * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3114b4164SWarner Losh  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4114b4164SWarner Losh  *
5114b4164SWarner Losh  * Redistribution and use in source and binary forms, with or without
6114b4164SWarner Losh  * modification, are permitted provided that the following conditions
7114b4164SWarner Losh  * are met:
8114b4164SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9114b4164SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10114b4164SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11114b4164SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12114b4164SWarner Losh  *    documentation and/or other materials provided with the distribution.
13114b4164SWarner Losh  *
14114b4164SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15114b4164SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16114b4164SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17114b4164SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18114b4164SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19114b4164SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20114b4164SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21114b4164SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22114b4164SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23114b4164SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2414eced72SWarner Losh  *
2514eced72SWarner Losh  * Portions of this software may have been developed with reference to
2614eced72SWarner Losh  * the SD Simplified Specification.  The following disclaimer may apply:
2714eced72SWarner Losh  *
2814eced72SWarner Losh  * The following conditions apply to the release of the simplified
2914eced72SWarner Losh  * specification ("Simplified Specification") by the SD Card Association and
3014eced72SWarner Losh  * the SD Group. The Simplified Specification is a subset of the complete SD
3114eced72SWarner Losh  * Specification which is owned by the SD Card Association and the SD
3214eced72SWarner Losh  * Group. This Simplified Specification is provided on a non-confidential
3314eced72SWarner Losh  * basis subject to the disclaimers below. Any implementation of the
3414eced72SWarner Losh  * Simplified Specification may require a license from the SD Card
3514eced72SWarner Losh  * Association, SD Group, SD-3C LLC or other third parties.
3614eced72SWarner Losh  *
3714eced72SWarner Losh  * Disclaimers:
3814eced72SWarner Losh  *
3914eced72SWarner Losh  * The information contained in the Simplified Specification is presented only
4014eced72SWarner Losh  * as a standard specification for SD Cards and SD Host/Ancillary products and
4114eced72SWarner Losh  * is provided "AS-IS" without any representations or warranties of any
4214eced72SWarner Losh  * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
4314eced72SWarner Losh  * Card Association for any damages, any infringements of patents or other
4414eced72SWarner Losh  * right of the SD Group, SD-3C LLC, the SD Card Association or any third
4514eced72SWarner Losh  * parties, which may result from its use. No license is granted by
4614eced72SWarner Losh  * implication, estoppel or otherwise under any patent or other rights of the
4714eced72SWarner Losh  * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
4814eced72SWarner Losh  * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
4914eced72SWarner Losh  * or the SD Card Association to disclose or distribute any technical
5014eced72SWarner Losh  * information, know-how or other confidential information to any third party.
51114b4164SWarner Losh  */
52114b4164SWarner Losh 
53114b4164SWarner Losh #include <sys/cdefs.h>
54114b4164SWarner Losh __FBSDID("$FreeBSD$");
55114b4164SWarner Losh 
56114b4164SWarner Losh #include <sys/param.h>
57114b4164SWarner Losh #include <sys/systm.h>
58114b4164SWarner Losh #include <sys/bio.h>
59114b4164SWarner Losh #include <sys/bus.h>
60114b4164SWarner Losh #include <sys/conf.h>
61114b4164SWarner Losh #include <sys/kernel.h>
62114b4164SWarner Losh #include <sys/kthread.h>
63114b4164SWarner Losh #include <sys/lock.h>
64114b4164SWarner Losh #include <sys/malloc.h>
65114b4164SWarner Losh #include <sys/module.h>
66114b4164SWarner Losh #include <sys/mutex.h>
67114b4164SWarner Losh #include <geom/geom_disk.h>
68114b4164SWarner Losh 
69114b4164SWarner Losh #include <dev/mmc/mmcvar.h>
70114b4164SWarner Losh #include <dev/mmc/mmcreg.h>
71114b4164SWarner Losh 
72114b4164SWarner Losh #include "mmcbus_if.h"
73114b4164SWarner Losh 
74114b4164SWarner Losh struct mmcsd_softc {
75114b4164SWarner Losh 	device_t dev;
76114b4164SWarner Losh 	struct mtx sc_mtx;
77114b4164SWarner Losh 	struct disk *disk;
78114b4164SWarner Losh 	struct proc *p;
79114b4164SWarner Losh 	struct bio_queue_head bio_queue;
800e1466acSWarner Losh 	int running;
81114b4164SWarner Losh };
82114b4164SWarner Losh 
830c0903b1SWarner Losh #define	MULTI_BLOCK_BROKEN
84114b4164SWarner Losh 
85114b4164SWarner Losh /* bus entry points */
86114b4164SWarner Losh static int mmcsd_probe(device_t dev);
87114b4164SWarner Losh static int mmcsd_attach(device_t dev);
88114b4164SWarner Losh static int mmcsd_detach(device_t dev);
89114b4164SWarner Losh 
90114b4164SWarner Losh /* disk routines */
91114b4164SWarner Losh static int mmcsd_open(struct disk *dp);
92114b4164SWarner Losh static int mmcsd_close(struct disk *dp);
93114b4164SWarner Losh static void mmcsd_strategy(struct bio *bp);
94114b4164SWarner Losh static void mmcsd_task(void *arg);
95114b4164SWarner Losh 
96114b4164SWarner Losh #define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
97114b4164SWarner Losh #define	MMCSD_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
98114b4164SWarner Losh #define MMCSD_LOCK_INIT(_sc) \
99114b4164SWarner Losh 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
100114b4164SWarner Losh 	    "mmcsd", MTX_DEF)
101114b4164SWarner Losh #define MMCSD_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
102114b4164SWarner Losh #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
103114b4164SWarner Losh #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
104114b4164SWarner Losh 
105114b4164SWarner Losh static int
106114b4164SWarner Losh mmcsd_probe(device_t dev)
107114b4164SWarner Losh {
108114b4164SWarner Losh 
109c18f1e26SAlexander Motin 	device_quiet(dev);
1100c0903b1SWarner Losh 	device_set_desc(dev, "MMC/SD Memory Card");
111114b4164SWarner Losh 	return (0);
112114b4164SWarner Losh }
113114b4164SWarner Losh 
114114b4164SWarner Losh static int
115114b4164SWarner Losh mmcsd_attach(device_t dev)
116114b4164SWarner Losh {
117114b4164SWarner Losh 	struct mmcsd_softc *sc;
118114b4164SWarner Losh 
119114b4164SWarner Losh 	sc = device_get_softc(dev);
120114b4164SWarner Losh 	sc->dev = dev;
121114b4164SWarner Losh 	MMCSD_LOCK_INIT(sc);
122114b4164SWarner Losh 
123114b4164SWarner Losh 	sc->disk = disk_alloc();
124114b4164SWarner Losh 	sc->disk->d_open = mmcsd_open;
125114b4164SWarner Losh 	sc->disk->d_close = mmcsd_close;
126114b4164SWarner Losh 	sc->disk->d_strategy = mmcsd_strategy;
127114b4164SWarner Losh 	// sc->disk->d_dump = mmcsd_dump;	Need polling mmc layer
128114b4164SWarner Losh 	sc->disk->d_name = "mmcsd";
129114b4164SWarner Losh 	sc->disk->d_drv1 = sc;
130a84f3c7aSWarner Losh 	sc->disk->d_maxsize = MAXPHYS;		/* Maybe ask bridge? */
131114b4164SWarner Losh 	sc->disk->d_sectorsize = mmc_get_sector_size(dev);
13238c51cbeSWarner Losh 	sc->disk->d_mediasize = mmc_get_media_size(dev) *
1330c0903b1SWarner Losh 	    mmc_get_sector_size(dev);
134114b4164SWarner Losh 	sc->disk->d_unit = device_get_unit(dev);
1350c0903b1SWarner Losh 
1360c0903b1SWarner Losh 	device_printf(dev, "%juMB <MMC/SD Memory Card>%s at %s\n",
1370c0903b1SWarner Losh 	    sc->disk->d_mediasize / 1048576,
1380c0903b1SWarner Losh 	    mmc_get_read_only(dev)?" (read-only)":"",
1390c0903b1SWarner Losh 	    device_get_nameunit(device_get_parent(sc->dev)));
140114b4164SWarner Losh 	disk_create(sc->disk, DISK_VERSION);
141114b4164SWarner Losh 	bioq_init(&sc->bio_queue);
1420e1466acSWarner Losh 
1430e1466acSWarner Losh 	sc->running = 1;
1443745c395SJulian Elischer 	kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
145114b4164SWarner Losh 
146114b4164SWarner Losh 	return (0);
147114b4164SWarner Losh }
148114b4164SWarner Losh 
149114b4164SWarner Losh static int
150114b4164SWarner Losh mmcsd_detach(device_t dev)
151114b4164SWarner Losh {
1520e1466acSWarner Losh 	struct mmcsd_softc *sc = device_get_softc(dev);
1530e1466acSWarner Losh 
1540e1466acSWarner Losh 	/* kill thread */
1550e1466acSWarner Losh 	MMCSD_LOCK(sc);
1560e1466acSWarner Losh 	sc->running = 0;
1570e1466acSWarner Losh 	wakeup(sc);
1580e1466acSWarner Losh 	MMCSD_UNLOCK(sc);
1590e1466acSWarner Losh 
1600e1466acSWarner Losh 	/* wait for thread to finish.  XXX probably want timeout.  -sorbo */
1610e1466acSWarner Losh 	MMCSD_LOCK(sc);
1620e1466acSWarner Losh 	while (sc->running != -1)
1630e1466acSWarner Losh 		msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0);
1640e1466acSWarner Losh 	MMCSD_UNLOCK(sc);
1650e1466acSWarner Losh 
1660e1466acSWarner Losh 	/* kill disk */
1670e1466acSWarner Losh 	disk_destroy(sc->disk);
1680e1466acSWarner Losh 	/* XXX destroy anything in queue */
1690e1466acSWarner Losh 
1700e1466acSWarner Losh 	MMCSD_LOCK_DESTROY(sc);
1710e1466acSWarner Losh 
172128d0ff2SWarner Losh 	return (0);
173114b4164SWarner Losh }
174114b4164SWarner Losh 
175114b4164SWarner Losh static int
176114b4164SWarner Losh mmcsd_open(struct disk *dp)
177114b4164SWarner Losh {
178128d0ff2SWarner Losh 	return (0);
179114b4164SWarner Losh }
180114b4164SWarner Losh 
181114b4164SWarner Losh static int
182114b4164SWarner Losh mmcsd_close(struct disk *dp)
183114b4164SWarner Losh {
184128d0ff2SWarner Losh 	return (0);
185114b4164SWarner Losh }
186114b4164SWarner Losh 
187114b4164SWarner Losh static void
188114b4164SWarner Losh mmcsd_strategy(struct bio *bp)
189114b4164SWarner Losh {
190114b4164SWarner Losh 	struct mmcsd_softc *sc;
191114b4164SWarner Losh 
192114b4164SWarner Losh 	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
193114b4164SWarner Losh 	MMCSD_LOCK(sc);
194114b4164SWarner Losh 	bioq_disksort(&sc->bio_queue, bp);
195114b4164SWarner Losh 	wakeup(sc);
196114b4164SWarner Losh 	MMCSD_UNLOCK(sc);
197114b4164SWarner Losh }
198114b4164SWarner Losh 
199114b4164SWarner Losh static void
200114b4164SWarner Losh mmcsd_task(void *arg)
201114b4164SWarner Losh {
202114b4164SWarner Losh 	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
203114b4164SWarner Losh 	struct bio *bp;
204114b4164SWarner Losh 	int sz;
205114b4164SWarner Losh 	daddr_t block, end;
206114b4164SWarner Losh 	struct mmc_command cmd;
207114b4164SWarner Losh 	struct mmc_command stop;
208114b4164SWarner Losh 	struct mmc_request req;
209114b4164SWarner Losh 	struct mmc_data data;
210114b4164SWarner Losh 	device_t dev;
211114b4164SWarner Losh 
212114b4164SWarner Losh 	dev = sc->dev;
2130e1466acSWarner Losh 	while (sc->running) {
214114b4164SWarner Losh 		MMCSD_LOCK(sc);
215114b4164SWarner Losh 		do {
216114b4164SWarner Losh 			bp = bioq_first(&sc->bio_queue);
217114b4164SWarner Losh 			if (bp == NULL)
218114b4164SWarner Losh 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
2190e1466acSWarner Losh 		} while (bp == NULL && sc->running);
2200e1466acSWarner Losh 		if (bp)
221114b4164SWarner Losh 			bioq_remove(&sc->bio_queue, bp);
222114b4164SWarner Losh 		MMCSD_UNLOCK(sc);
2230e1466acSWarner Losh 		if (!sc->running)
2240e1466acSWarner Losh 			break;
22508eb9a9bSWarner Losh //		printf("mmc_task: request %p for block %ju\n", bp, bp->bio_pblkno);
22608eb9a9bSWarner Losh 		if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
22708eb9a9bSWarner Losh 			bp->bio_error = EROFS;
22808eb9a9bSWarner Losh 			bp->bio_resid = bp->bio_bcount;
22908eb9a9bSWarner Losh 			bp->bio_flags |= BIO_ERROR;
23008eb9a9bSWarner Losh 			biodone(bp);
23108eb9a9bSWarner Losh 			continue;
23208eb9a9bSWarner Losh 		}
233114b4164SWarner Losh 		MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
234114b4164SWarner Losh 		sz = sc->disk->d_sectorsize;
235114b4164SWarner Losh 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
236114b4164SWarner Losh 		for (block = bp->bio_pblkno; block < end;) {
237114b4164SWarner Losh 			char *vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
2380c0903b1SWarner Losh 			int numblocks;
2390c0903b1SWarner Losh #ifdef MULTI_BLOCK
2400c0903b1SWarner Losh 			numblocks = end - block;
2410c0903b1SWarner Losh #else
2420c0903b1SWarner Losh 			numblocks = 1;
2430c0903b1SWarner Losh #endif
244114b4164SWarner Losh 			memset(&req, 0, sizeof(req));
245114b4164SWarner Losh 			memset(&cmd, 0, sizeof(cmd));
246114b4164SWarner Losh 			memset(&stop, 0, sizeof(stop));
247114b4164SWarner Losh 			req.cmd = &cmd;
248114b4164SWarner Losh 			cmd.data = &data;
249114b4164SWarner Losh 			if (bp->bio_cmd == BIO_READ) {
2500c0903b1SWarner Losh 				if (numblocks > 1)
251114b4164SWarner Losh 					cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
252114b4164SWarner Losh 				else
253114b4164SWarner Losh 					cmd.opcode = MMC_READ_SINGLE_BLOCK;
2540c0903b1SWarner Losh 			} else {
2550c0903b1SWarner Losh 				if (numblocks > 1)
2560c0903b1SWarner Losh 					cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2570c0903b1SWarner Losh 				else
258114b4164SWarner Losh 					cmd.opcode = MMC_WRITE_BLOCK;
2590c0903b1SWarner Losh 			}
260c18f1e26SAlexander Motin 			cmd.arg = block;
261c18f1e26SAlexander Motin 			if (!mmc_get_high_cap(dev))
262c18f1e26SAlexander Motin 				cmd.arg <<= 9;
263114b4164SWarner Losh 			cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
264114b4164SWarner Losh 			data.data = vaddr;
265114b4164SWarner Losh 			data.mrq = &req;
2660c0903b1SWarner Losh 			if (bp->bio_cmd == BIO_READ)
267114b4164SWarner Losh 				data.flags = MMC_DATA_READ;
2680c0903b1SWarner Losh 			else
269114b4164SWarner Losh 				data.flags = MMC_DATA_WRITE;
2700c0903b1SWarner Losh 			data.len = numblocks * sz;
2710c0903b1SWarner Losh 			if (numblocks > 1) {
2720c0903b1SWarner Losh 				data.flags |= MMC_DATA_MULTI;
273114b4164SWarner Losh 				stop.opcode = MMC_STOP_TRANSMISSION;
274114b4164SWarner Losh 				stop.arg = 0;
275114b4164SWarner Losh 				stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
2760c0903b1SWarner Losh 				req.stop = &stop;
2770c0903b1SWarner Losh 			}
2780c0903b1SWarner Losh //			printf("Len %d  %lld-%lld flags %#x sz %d\n",
2790c0903b1SWarner Losh //			    (int)data.len, (long long)block, (long long)end, data.flags, sz);
280114b4164SWarner Losh 			MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
281114b4164SWarner Losh 			    &req);
2820c0903b1SWarner Losh 			if (req.cmd->error != MMC_ERR_NONE)
2830c0903b1SWarner Losh 				break;
2840c0903b1SWarner Losh 			block += numblocks;
285114b4164SWarner Losh 		}
286114b4164SWarner Losh 		MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
2870c0903b1SWarner Losh 		if (block < end) {
2880c0903b1SWarner Losh 			bp->bio_error = EIO;
2890c0903b1SWarner Losh 			bp->bio_resid = (end - block) * sz;
2900c0903b1SWarner Losh 			bp->bio_flags |= BIO_ERROR;
2910c0903b1SWarner Losh 		}
292114b4164SWarner Losh 		biodone(bp);
293114b4164SWarner Losh 	}
2940e1466acSWarner Losh 
2950e1466acSWarner Losh 	/* tell parent we're done */
2960e1466acSWarner Losh 	MMCSD_LOCK(sc);
2970e1466acSWarner Losh 	sc->running = -1;
2980e1466acSWarner Losh 	wakeup(sc);
2990e1466acSWarner Losh 	MMCSD_UNLOCK(sc);
3000e1466acSWarner Losh 
3013745c395SJulian Elischer 	kproc_exit(0);
302114b4164SWarner Losh }
303114b4164SWarner Losh 
304114b4164SWarner Losh static device_method_t mmcsd_methods[] = {
305114b4164SWarner Losh 	DEVMETHOD(device_probe, mmcsd_probe),
306114b4164SWarner Losh 	DEVMETHOD(device_attach, mmcsd_attach),
307114b4164SWarner Losh 	DEVMETHOD(device_detach, mmcsd_detach),
308114b4164SWarner Losh 	{0, 0},
309114b4164SWarner Losh };
310114b4164SWarner Losh 
311114b4164SWarner Losh static driver_t mmcsd_driver = {
312114b4164SWarner Losh 	"mmcsd",
313114b4164SWarner Losh 	mmcsd_methods,
314114b4164SWarner Losh 	sizeof(struct mmcsd_softc),
315114b4164SWarner Losh };
316114b4164SWarner Losh static devclass_t mmcsd_devclass;
317114b4164SWarner Losh 
318114b4164SWarner Losh 
319114b4164SWarner Losh DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0);
320