xref: /freebsd/sys/dev/mmc/mmcsd.c (revision 6c6c03be2ddb04c54e455122799923deaefa4114)
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 	daddr_t eblock, eend;	/* Range remaining after the last erase. */
81 	int running;
82 };
83 
84 #define	MULTI_BLOCK_BROKEN
85 
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);
90 
91 /* disk routines */
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);
96 
97 static const char *mmcsd_card_name(device_t dev);
98 static int mmcsd_bus_bit_width(device_t dev);
99 
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), \
104 	    "mmcsd", MTX_DEF)
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);
108 
109 static int
110 mmcsd_probe(device_t dev)
111 {
112 
113 	device_quiet(dev);
114 	device_set_desc(dev, "MMC/SD Memory Card");
115 	return (0);
116 }
117 
118 static int
119 mmcsd_attach(device_t dev)
120 {
121 	struct mmcsd_softc *sc;
122 	struct disk *d;
123 	intmax_t mb;
124 	char unit;
125 
126 	sc = device_get_softc(dev);
127 	sc->dev = dev;
128 	MMCSD_LOCK_INIT(sc);
129 
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
135 	d->d_name = "mmcsd";
136 	d->d_drv1 = sc;
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;
142 	/*
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).
150 	 */
151 	mb = d->d_mediasize >> 20;	/* 1MiB == 1 << 20 */
152 	unit = 'M';
153 	if (mb >= 10240) {		/* 1GiB = 1024 MiB */
154 		unit = 'G';
155 		mb /= 1024;
156 	}
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);
164 
165 	sc->running = 1;
166 	sc->eblock = sc->eend = 0;
167 	kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
168 
169 	return (0);
170 }
171 
172 static int
173 mmcsd_detach(device_t dev)
174 {
175 	struct mmcsd_softc *sc = device_get_softc(dev);
176 
177 	/* kill thread */
178 	MMCSD_LOCK(sc);
179 	sc->running = 0;
180 	wakeup(sc);
181 	MMCSD_UNLOCK(sc);
182 
183 	/* wait for thread to finish.  XXX probably want timeout.  -sorbo */
184 	MMCSD_LOCK(sc);
185 	while (sc->running != -1)
186 		msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0);
187 	MMCSD_UNLOCK(sc);
188 
189 	/* kill disk */
190 	disk_destroy(sc->disk);
191 	/* XXX destroy anything in queue */
192 
193 	MMCSD_LOCK_DESTROY(sc);
194 
195 	return (0);
196 }
197 
198 static int
199 mmcsd_open(struct disk *dp)
200 {
201 	return (0);
202 }
203 
204 static int
205 mmcsd_close(struct disk *dp)
206 {
207 	return (0);
208 }
209 
210 static void
211 mmcsd_strategy(struct bio *bp)
212 {
213 	struct mmcsd_softc *sc;
214 
215 	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
216 	MMCSD_LOCK(sc);
217 	bioq_disksort(&sc->bio_queue, bp);
218 	wakeup(sc);
219 	MMCSD_UNLOCK(sc);
220 }
221 
222 static daddr_t
223 mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp)
224 {
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 = sc->dev;
231 	int sz = sc->disk->d_sectorsize;
232 
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;
238 		int numblocks;
239 #ifdef MULTI_BLOCK
240 		numblocks = end - block;
241 #else
242 		numblocks = 1;
243 #endif
244 		memset(&req, 0, sizeof(req));
245     		memset(&cmd, 0, sizeof(cmd));
246 		memset(&stop, 0, sizeof(stop));
247 		req.cmd = &cmd;
248 		cmd.data = &data;
249 		if (bp->bio_cmd == BIO_READ) {
250 			if (numblocks > 1)
251 				cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
252 			else
253 				cmd.opcode = MMC_READ_SINGLE_BLOCK;
254 		} else {
255 			if (numblocks > 1)
256 				cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
257 			else
258 				cmd.opcode = MMC_WRITE_BLOCK;
259 		}
260 		cmd.arg = block;
261 		if (!mmc_get_high_cap(dev))
262 			cmd.arg <<= 9;
263 		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
264 		data.data = vaddr;
265 		data.mrq = &req;
266 		if (bp->bio_cmd == BIO_READ)
267 			data.flags = MMC_DATA_READ;
268 		else
269 			data.flags = MMC_DATA_WRITE;
270 		data.len = numblocks * sz;
271 		if (numblocks > 1) {
272 			data.flags |= MMC_DATA_MULTI;
273 			stop.opcode = MMC_STOP_TRANSMISSION;
274 			stop.arg = 0;
275 			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
276 			req.stop = &stop;
277 		}
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,
281 		    &req);
282 		if (req.cmd->error != MMC_ERR_NONE)
283 			break;
284 		block += numblocks;
285 	}
286 	return (block);
287 }
288 
289 static daddr_t
290 mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp)
291 {
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;
297 	int erase_sector;
298 
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)
303 		block = sc->eblock;
304 	if (end >= sc->eblock && end < sc->eend)
305 		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. */
313 	if (start >= stop) {
314 		sc->eblock = block;
315 		sc->eend = end;
316 		return (end);
317 	}
318 
319 	/* Set erase start position. */
320 	memset(&req, 0, sizeof(req));
321 	memset(&cmd, 0, sizeof(cmd));
322 	req.cmd = &cmd;
323 	if (mmc_get_card_type(dev) == mode_sd)
324 		cmd.opcode = SD_ERASE_WR_BLK_START;
325 	else
326 		cmd.opcode = MMC_ERASE_GROUP_START;
327 	cmd.arg = start;
328 	if (!mmc_get_high_cap(dev))
329 		cmd.arg <<= 9;
330 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
331 	MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
332 	    &req);
333 	if (req.cmd->error != MMC_ERR_NONE) {
334 	    printf("erase err1: %d\n", req.cmd->error);
335 	    return (block);
336 	}
337 	/* Set erase stop position. */
338 	memset(&req, 0, sizeof(req));
339 	memset(&cmd, 0, sizeof(cmd));
340 	req.cmd = &cmd;
341 	if (mmc_get_card_type(dev) == mode_sd)
342 		cmd.opcode = SD_ERASE_WR_BLK_END;
343 	else
344 		cmd.opcode = MMC_ERASE_GROUP_END;
345 	cmd.arg = stop;
346 	if (!mmc_get_high_cap(dev))
347 		cmd.arg <<= 9;
348 	cmd.arg--;
349 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
350 	MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
351 	    &req);
352 	if (req.cmd->error != MMC_ERR_NONE) {
353 	    printf("erase err2: %d\n", req.cmd->error);
354 	    return (block);
355 	}
356 	/* Erase range. */
357 	memset(&req, 0, sizeof(req));
358 	memset(&cmd, 0, sizeof(cmd));
359 	req.cmd = &cmd;
360 	cmd.opcode = MMC_ERASE;
361 	cmd.arg = 0;
362 	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
363 	MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
364 	    &req);
365 	if (req.cmd->error != MMC_ERR_NONE) {
366 	    printf("erase err3 %d\n", req.cmd->error);
367 	    return (block);
368 	}
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. */
372 		sc->eend = end;
373 	} else {
374 		sc->eblock = block;	/* Predict next backward. */
375 		sc->eend = start;
376 	}
377 	return (end);
378 }
379 
380 static void
381 mmcsd_task(void *arg)
382 {
383 	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
384 	struct bio *bp;
385 	int sz;
386 	daddr_t block, end;
387 	device_t dev;
388 
389 	dev = sc->dev;
390 	while (sc->running) {
391 		MMCSD_LOCK(sc);
392 		do {
393 			bp = bioq_first(&sc->bio_queue);
394 			if (bp == NULL)
395 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
396 		} while (bp == NULL && sc->running);
397 		if (bp)
398 			bioq_remove(&sc->bio_queue, bp);
399 		MMCSD_UNLOCK(sc);
400 		if (!sc->running)
401 			break;
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;
406 			biodone(bp);
407 			continue;
408 		}
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);
420 		}
421 		MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
422 		if (block < end) {
423 			bp->bio_error = EIO;
424 			bp->bio_resid = (end - block) * sz;
425 			bp->bio_flags |= BIO_ERROR;
426 		}
427 		biodone(bp);
428 	}
429 
430 	/* tell parent we're done */
431 	MMCSD_LOCK(sc);
432 	sc->running = -1;
433 	wakeup(sc);
434 	MMCSD_UNLOCK(sc);
435 
436 	kproc_exit(0);
437 }
438 
439 static const char *
440 mmcsd_card_name(device_t dev)
441 {
442 	if (mmc_get_card_type(dev) == mode_mmc)
443 		return ("MMC");
444 	if (mmc_get_high_cap(dev))
445 		return ("SDHC");
446 	return ("SD");
447 }
448 
449 static int
450 mmcsd_bus_bit_width(device_t dev)
451 {
452 	if (mmc_get_bus_width(dev) == bus_width_1)
453 		return (1);
454 	if (mmc_get_bus_width(dev) == bus_width_4)
455 		return (4);
456 	return (8);
457 }
458 
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),
463 	{0, 0},
464 };
465 
466 static driver_t mmcsd_driver = {
467 	"mmcsd",
468 	mmcsd_methods,
469 	sizeof(struct mmcsd_softc),
470 };
471 static devclass_t mmcsd_devclass;
472 
473 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0);
474