xref: /freebsd/sys/dev/mmc/mmcsd.c (revision 4a5216a6dc0c3ce4cf5f2d3ee8af0c3ff3402c4f)
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$");
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 
74 struct 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 */
86 static int mmcsd_probe(device_t dev);
87 static int mmcsd_attach(device_t dev);
88 static int mmcsd_detach(device_t dev);
89 
90 /* disk routines */
91 static int mmcsd_open(struct disk *dp);
92 static int mmcsd_close(struct disk *dp);
93 static void mmcsd_strategy(struct bio *bp);
94 static void mmcsd_task(void *arg);
95 
96 static const char *mmcsd_card_name(device_t dev);
97 static 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 
108 static int
109 mmcsd_probe(device_t dev)
110 {
111 
112 	device_quiet(dev);
113 	device_set_desc(dev, "MMC/SD Memory Card");
114 	return (0);
115 }
116 
117 static int
118 mmcsd_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 = MAXPHYS;		/* Maybe ask bridge? */
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 	/*
141 	 * Display in most natural units.  There's no cards < 1MB.
142 	 * The SD standard goes to 2GiB, but the data format supports
143 	 * up to 4GiB and some card makers push it up to this limit.
144 	 * The SDHC standard only goes to 32GiB (the data format in
145 	 * SDHC is good to 2TiB however, which isn't too ugly at
146 	 * 2048GiBm, so we note it in passing here and don't add the
147 	 * code to print TiB).
148 	 */
149 	mb = d->d_mediasize >> 20;	/* 1MiB == 1 << 20 */
150 	unit = 'M';
151 	if (mb >= 10240) {		/* 1GiB = 1024 MiB */
152 		unit = 'G';
153 		mb /= 1024;
154 	}
155 	device_printf(dev, "%ju%cB <%s Memory Card>%s at %s %dMHz/%dbit\n",
156 	    mb, unit, mmcsd_card_name(dev),
157 	    mmc_get_read_only(dev) ? " (read-only)" : "",
158 	    device_get_nameunit(device_get_parent(dev)),
159 	    mmc_get_tran_speed(dev) / 1000000, mmcsd_bus_bit_width(dev));
160 	disk_create(d, DISK_VERSION);
161 	bioq_init(&sc->bio_queue);
162 
163 	sc->running = 1;
164 	kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
165 
166 	return (0);
167 }
168 
169 static int
170 mmcsd_detach(device_t dev)
171 {
172 	struct mmcsd_softc *sc = device_get_softc(dev);
173 
174 	/* kill thread */
175 	MMCSD_LOCK(sc);
176 	sc->running = 0;
177 	wakeup(sc);
178 	MMCSD_UNLOCK(sc);
179 
180 	/* wait for thread to finish.  XXX probably want timeout.  -sorbo */
181 	MMCSD_LOCK(sc);
182 	while (sc->running != -1)
183 		msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0);
184 	MMCSD_UNLOCK(sc);
185 
186 	/* kill disk */
187 	disk_destroy(sc->disk);
188 	/* XXX destroy anything in queue */
189 
190 	MMCSD_LOCK_DESTROY(sc);
191 
192 	return (0);
193 }
194 
195 static int
196 mmcsd_open(struct disk *dp)
197 {
198 	return (0);
199 }
200 
201 static int
202 mmcsd_close(struct disk *dp)
203 {
204 	return (0);
205 }
206 
207 static void
208 mmcsd_strategy(struct bio *bp)
209 {
210 	struct mmcsd_softc *sc;
211 
212 	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
213 	MMCSD_LOCK(sc);
214 	bioq_disksort(&sc->bio_queue, bp);
215 	wakeup(sc);
216 	MMCSD_UNLOCK(sc);
217 }
218 
219 static void
220 mmcsd_task(void *arg)
221 {
222 	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
223 	struct bio *bp;
224 	int sz;
225 	daddr_t block, end;
226 	struct mmc_command cmd;
227 	struct mmc_command stop;
228 	struct mmc_request req;
229 	struct mmc_data data;
230 	device_t dev;
231 
232 	dev = sc->dev;
233 	while (sc->running) {
234 		MMCSD_LOCK(sc);
235 		do {
236 			bp = bioq_first(&sc->bio_queue);
237 			if (bp == NULL)
238 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
239 		} while (bp == NULL && sc->running);
240 		if (bp)
241 			bioq_remove(&sc->bio_queue, bp);
242 		MMCSD_UNLOCK(sc);
243 		if (!sc->running)
244 			break;
245 //		printf("mmc_task: request %p for block %ju\n", bp, bp->bio_pblkno);
246 		if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
247 			bp->bio_error = EROFS;
248 			bp->bio_resid = bp->bio_bcount;
249 			bp->bio_flags |= BIO_ERROR;
250 			biodone(bp);
251 			continue;
252 		}
253 		MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
254 		sz = sc->disk->d_sectorsize;
255 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
256 		for (block = bp->bio_pblkno; block < end;) {
257 			char *vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
258 			int numblocks;
259 #ifdef MULTI_BLOCK
260 			numblocks = end - block;
261 #else
262 			numblocks = 1;
263 #endif
264 			memset(&req, 0, sizeof(req));
265 			memset(&cmd, 0, sizeof(cmd));
266 			memset(&stop, 0, sizeof(stop));
267 			req.cmd = &cmd;
268 			cmd.data = &data;
269 			if (bp->bio_cmd == BIO_READ) {
270 				if (numblocks > 1)
271 					cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
272 				else
273 					cmd.opcode = MMC_READ_SINGLE_BLOCK;
274 			} else {
275 				if (numblocks > 1)
276 					cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
277 				else
278 					cmd.opcode = MMC_WRITE_BLOCK;
279 			}
280 			cmd.arg = block;
281 			if (!mmc_get_high_cap(dev))
282 				cmd.arg <<= 9;
283 			cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
284 			data.data = vaddr;
285 			data.mrq = &req;
286 			if (bp->bio_cmd == BIO_READ)
287 				data.flags = MMC_DATA_READ;
288 			else
289 				data.flags = MMC_DATA_WRITE;
290 			data.len = numblocks * sz;
291 			if (numblocks > 1) {
292 				data.flags |= MMC_DATA_MULTI;
293 				stop.opcode = MMC_STOP_TRANSMISSION;
294 				stop.arg = 0;
295 				stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
296 				req.stop = &stop;
297 			}
298 //			printf("Len %d  %lld-%lld flags %#x sz %d\n",
299 //			    (int)data.len, (long long)block, (long long)end, data.flags, sz);
300 			MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
301 			    &req);
302 			if (req.cmd->error != MMC_ERR_NONE)
303 				break;
304 			block += numblocks;
305 		}
306 		MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
307 		if (block < end) {
308 			bp->bio_error = EIO;
309 			bp->bio_resid = (end - block) * sz;
310 			bp->bio_flags |= BIO_ERROR;
311 		}
312 		biodone(bp);
313 	}
314 
315 	/* tell parent we're done */
316 	MMCSD_LOCK(sc);
317 	sc->running = -1;
318 	wakeup(sc);
319 	MMCSD_UNLOCK(sc);
320 
321 	kproc_exit(0);
322 }
323 
324 static const char *
325 mmcsd_card_name(device_t dev)
326 {
327 	if (mmc_get_card_type(dev) == mode_mmc)
328 		return ("MMC");
329 	if (mmc_get_high_cap(dev))
330 		return ("SDHC");
331 	return ("SD");
332 }
333 
334 static int
335 mmcsd_bus_bit_width(device_t dev)
336 {
337 	if (mmc_get_bus_width(dev) == bus_width_1)
338 		return (1);
339 	if (mmc_get_bus_width(dev) == bus_width_4)
340 		return (4);
341 	return (8);
342 }
343 
344 static device_method_t mmcsd_methods[] = {
345 	DEVMETHOD(device_probe, mmcsd_probe),
346 	DEVMETHOD(device_attach, mmcsd_attach),
347 	DEVMETHOD(device_detach, mmcsd_detach),
348 	{0, 0},
349 };
350 
351 static driver_t mmcsd_driver = {
352 	"mmcsd",
353 	mmcsd_methods,
354 	sizeof(struct mmcsd_softc),
355 };
356 static devclass_t mmcsd_devclass;
357 
358 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0);
359