mmcsd.c revision 248689
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: head/sys/dev/mmc/mmcsd.c 248689 2013-03-24 17:23:10Z ian $");
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/mmcbrvar.h>
70#include <dev/mmc/mmcreg.h>
71#include <dev/mmc/mmcvar.h>
72
73#include "mmcbus_if.h"
74
75#if __FreeBSD_version < 800002
76#define	kproc_create	kthread_create
77#define	kproc_exit	kthread_exit
78#endif
79
80struct mmcsd_softc {
81	device_t dev;
82	struct mtx sc_mtx;
83	struct disk *disk;
84	struct proc *p;
85	struct bio_queue_head bio_queue;
86	daddr_t eblock, eend;	/* Range remaining after the last erase. */
87	int running;
88	int suspend;
89};
90
91static const char *errmsg[] =
92{
93	"None",
94	"Timeout",
95	"Bad CRC",
96	"Fifo",
97	"Failed",
98	"Invalid",
99	"NO MEMORY"
100};
101
102/* bus entry points */
103static int mmcsd_attach(device_t dev);
104static int mmcsd_detach(device_t dev);
105static int mmcsd_probe(device_t dev);
106
107/* disk routines */
108static int mmcsd_close(struct disk *dp);
109static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
110	off_t offset, size_t length);
111static int mmcsd_open(struct disk *dp);
112static void mmcsd_strategy(struct bio *bp);
113static void mmcsd_task(void *arg);
114
115static int mmcsd_bus_bit_width(device_t dev);
116static daddr_t mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp);
117static daddr_t mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp);
118
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), \
123	    "mmcsd", MTX_DEF)
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);
127
128static int
129mmcsd_probe(device_t dev)
130{
131
132	device_quiet(dev);
133	device_set_desc(dev, "MMC/SD Memory Card");
134	return (0);
135}
136
137static int
138mmcsd_attach(device_t dev)
139{
140	struct mmcsd_softc *sc;
141	struct disk *d;
142	intmax_t mb;
143	uint32_t speed;
144	uint32_t maxblocks;
145	char unit;
146
147	sc = device_get_softc(dev);
148	sc->dev = dev;
149	MMCSD_LOCK_INIT(sc);
150
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;
156	d->d_name = "mmcsd";
157	d->d_drv1 = sc;
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	/*
166	 * Display in most natural units.  There's no cards < 1MB.
167	 * The SD standard goes to 2GiB, but the data format supports
168	 * up to 4GiB and some card makers push it up to this limit.
169	 * The SDHC standard only goes to 32GiB (the data format in
170	 * SDHC is good to 2TiB however, which isn't too ugly at
171	 * 2048GiBm, so we note it in passing here and don't add the
172	 * code to print TiB).
173	 */
174	mb = d->d_mediasize >> 20;	/* 1MiB == 1 << 20 */
175	unit = 'M';
176	if (mb >= 10240) {		/* 1GiB = 1024 MiB */
177		unit = 'G';
178		mb /= 1024;
179	}
180	/*
181	 * Report the clock speed of the underlying hardware, which might be
182	 * different than what the card reports due to hardware limitations.
183	 * Report how many blocks the hardware transfers at once.
184	 */
185	speed = mmcbr_get_clock(device_get_parent(dev));
186	maxblocks = mmc_get_max_data(dev);
187	device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
188	    mb, unit, mmc_get_card_id_string(dev),
189	    mmc_get_read_only(dev) ? " (read-only)" : "",
190	    device_get_nameunit(device_get_parent(dev)),
191	    speed / 1000000, (speed / 100000) % 10,
192	    mmcsd_bus_bit_width(dev), maxblocks);
193	disk_create(d, DISK_VERSION);
194	bioq_init(&sc->bio_queue);
195
196	sc->running = 1;
197	sc->suspend = 0;
198	sc->eblock = sc->eend = 0;
199	kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
200
201	return (0);
202}
203
204static int
205mmcsd_detach(device_t dev)
206{
207	struct mmcsd_softc *sc = device_get_softc(dev);
208
209	MMCSD_LOCK(sc);
210	sc->suspend = 0;
211	if (sc->running > 0) {
212		/* kill thread */
213		sc->running = 0;
214		wakeup(sc);
215		/* wait for thread to finish. */
216		while (sc->running != -1)
217			msleep(sc, &sc->sc_mtx, 0, "detach", 0);
218	}
219	MMCSD_UNLOCK(sc);
220
221	/* Flush the request queue. */
222	bioq_flush(&sc->bio_queue, NULL, ENXIO);
223	/* kill disk */
224	disk_destroy(sc->disk);
225
226	MMCSD_LOCK_DESTROY(sc);
227
228	return (0);
229}
230
231static int
232mmcsd_suspend(device_t dev)
233{
234	struct mmcsd_softc *sc = device_get_softc(dev);
235
236	MMCSD_LOCK(sc);
237	sc->suspend = 1;
238	if (sc->running > 0) {
239		/* kill thread */
240		sc->running = 0;
241		wakeup(sc);
242		/* wait for thread to finish. */
243		while (sc->running != -1)
244			msleep(sc, &sc->sc_mtx, 0, "detach", 0);
245	}
246	MMCSD_UNLOCK(sc);
247	return (0);
248}
249
250static int
251mmcsd_resume(device_t dev)
252{
253	struct mmcsd_softc *sc = device_get_softc(dev);
254
255	MMCSD_LOCK(sc);
256	sc->suspend = 0;
257	if (sc->running <= 0) {
258		sc->running = 1;
259		MMCSD_UNLOCK(sc);
260		kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
261	} else
262		MMCSD_UNLOCK(sc);
263	return (0);
264}
265
266static int
267mmcsd_open(struct disk *dp)
268{
269
270	return (0);
271}
272
273static int
274mmcsd_close(struct disk *dp)
275{
276
277	return (0);
278}
279
280static void
281mmcsd_strategy(struct bio *bp)
282{
283	struct mmcsd_softc *sc;
284
285	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
286	MMCSD_LOCK(sc);
287	if (sc->running > 0 || sc->suspend > 0) {
288		bioq_disksort(&sc->bio_queue, bp);
289		MMCSD_UNLOCK(sc);
290		wakeup(sc);
291	} else {
292		MMCSD_UNLOCK(sc);
293		biofinish(bp, NULL, ENXIO);
294	}
295}
296
297static const char *
298mmcsd_errmsg(int e)
299{
300	if (e < 0 || e > MMC_ERR_MAX)
301		return "Bad error code";
302	return errmsg[e];
303}
304
305static daddr_t
306mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp)
307{
308	daddr_t block, end;
309	struct mmc_command cmd;
310	struct mmc_command stop;
311	struct mmc_request req;
312	struct mmc_data data;
313	device_t dev = sc->dev;
314	int sz = sc->disk->d_sectorsize;
315
316	block = bp->bio_pblkno;
317	end = bp->bio_pblkno + (bp->bio_bcount / sz);
318	while (block < end) {
319		char *vaddr = bp->bio_data +
320		    (block - bp->bio_pblkno) * sz;
321		int numblocks = min(end - block, mmc_get_max_data(dev));
322		memset(&req, 0, sizeof(req));
323    		memset(&cmd, 0, sizeof(cmd));
324		memset(&stop, 0, sizeof(stop));
325		cmd.mrq = &req;
326		req.cmd = &cmd;
327		cmd.data = &data;
328		if (bp->bio_cmd == BIO_READ) {
329			if (numblocks > 1)
330				cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
331			else
332				cmd.opcode = MMC_READ_SINGLE_BLOCK;
333		} else {
334			if (numblocks > 1)
335				cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
336			else
337				cmd.opcode = MMC_WRITE_BLOCK;
338		}
339		cmd.arg = block;
340		if (!mmc_get_high_cap(dev))
341			cmd.arg <<= 9;
342		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
343		data.data = vaddr;
344		data.mrq = &req;
345		if (bp->bio_cmd == BIO_READ)
346			data.flags = MMC_DATA_READ;
347		else
348			data.flags = MMC_DATA_WRITE;
349		data.len = numblocks * sz;
350		if (numblocks > 1) {
351			data.flags |= MMC_DATA_MULTI;
352			stop.opcode = MMC_STOP_TRANSMISSION;
353			stop.arg = 0;
354			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
355			stop.mrq = &req;
356			req.stop = &stop;
357		}
358		MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
359		    &req);
360		if (req.cmd->error != MMC_ERR_NONE) {
361			device_printf(dev, "Error indicated: %d %s\n",
362			    req.cmd->error, mmcsd_errmsg(req.cmd->error));
363			break;
364		}
365		block += numblocks;
366	}
367	return (block);
368}
369
370static daddr_t
371mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp)
372{
373	daddr_t block, end, start, stop;
374	struct mmc_command cmd;
375	struct mmc_request req;
376	device_t dev = sc->dev;
377	int sz = sc->disk->d_sectorsize;
378	int erase_sector;
379
380	block = bp->bio_pblkno;
381	end = bp->bio_pblkno + (bp->bio_bcount / sz);
382	/* Coalesce with part remaining from previous request. */
383	if (block > sc->eblock && block <= sc->eend)
384		block = sc->eblock;
385	if (end >= sc->eblock && end < sc->eend)
386		end = sc->eend;
387	/* Safe round to the erase sector boundaries. */
388	erase_sector = mmc_get_erase_sector(dev);
389	start = block + erase_sector - 1;	 /* Round up. */
390	start -= start % erase_sector;
391	stop = end;				/* Round down. */
392	stop -= end % erase_sector;
393	/* We can't erase area smaller then sector, store it for later. */
394	if (start >= stop) {
395		sc->eblock = block;
396		sc->eend = end;
397		return (end);
398	}
399
400	/* Set erase start position. */
401	memset(&req, 0, sizeof(req));
402	memset(&cmd, 0, sizeof(cmd));
403	cmd.mrq = &req;
404	req.cmd = &cmd;
405	if (mmc_get_card_type(dev) == mode_sd)
406		cmd.opcode = SD_ERASE_WR_BLK_START;
407	else
408		cmd.opcode = MMC_ERASE_GROUP_START;
409	cmd.arg = start;
410	if (!mmc_get_high_cap(dev))
411		cmd.arg <<= 9;
412	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
413	MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
414	    &req);
415	if (req.cmd->error != MMC_ERR_NONE) {
416	    printf("erase err1: %d\n", req.cmd->error);
417	    return (block);
418	}
419	/* Set erase stop position. */
420	memset(&req, 0, sizeof(req));
421	memset(&cmd, 0, sizeof(cmd));
422	req.cmd = &cmd;
423	if (mmc_get_card_type(dev) == mode_sd)
424		cmd.opcode = SD_ERASE_WR_BLK_END;
425	else
426		cmd.opcode = MMC_ERASE_GROUP_END;
427	cmd.arg = stop;
428	if (!mmc_get_high_cap(dev))
429		cmd.arg <<= 9;
430	cmd.arg--;
431	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
432	MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
433	    &req);
434	if (req.cmd->error != MMC_ERR_NONE) {
435	    printf("erase err2: %d\n", req.cmd->error);
436	    return (block);
437	}
438	/* Erase range. */
439	memset(&req, 0, sizeof(req));
440	memset(&cmd, 0, sizeof(cmd));
441	req.cmd = &cmd;
442	cmd.opcode = MMC_ERASE;
443	cmd.arg = 0;
444	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
445	MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
446	    &req);
447	if (req.cmd->error != MMC_ERR_NONE) {
448	    printf("erase err3 %d\n", req.cmd->error);
449	    return (block);
450	}
451	/* Store one of remaining parts for the next call. */
452	if (bp->bio_pblkno >= sc->eblock || block == start) {
453		sc->eblock = stop;	/* Predict next forward. */
454		sc->eend = end;
455	} else {
456		sc->eblock = block;	/* Predict next backward. */
457		sc->eend = start;
458	}
459	return (end);
460}
461
462static int
463mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
464	off_t offset, size_t length)
465{
466	struct disk *disk = arg;
467	struct mmcsd_softc *sc = (struct mmcsd_softc *)disk->d_drv1;
468	device_t dev = sc->dev;
469	struct bio bp;
470	daddr_t block, end;
471
472	/* length zero is special and really means flush buffers to media */
473	if (!length)
474		return (0);
475
476	bzero(&bp, sizeof(struct bio));
477	bp.bio_disk = disk;
478	bp.bio_pblkno = offset / disk->d_sectorsize;
479	bp.bio_bcount = length;
480	bp.bio_data = virtual;
481	bp.bio_cmd = BIO_WRITE;
482	end = bp.bio_pblkno + bp.bio_bcount / sc->disk->d_sectorsize;
483	MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
484	block = mmcsd_rw(sc, &bp);
485	MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
486	return ((end < block) ? EIO : 0);
487}
488
489static void
490mmcsd_task(void *arg)
491{
492	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
493	struct bio *bp;
494	int sz;
495	daddr_t block, end;
496	device_t dev;
497
498	dev = sc->dev;
499	while (1) {
500		MMCSD_LOCK(sc);
501		do {
502			if (sc->running == 0)
503				goto out;
504			bp = bioq_takefirst(&sc->bio_queue);
505			if (bp == NULL)
506				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
507		} while (bp == NULL);
508		MMCSD_UNLOCK(sc);
509		if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
510			bp->bio_error = EROFS;
511			bp->bio_resid = bp->bio_bcount;
512			bp->bio_flags |= BIO_ERROR;
513			biodone(bp);
514			continue;
515		}
516		MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
517		sz = sc->disk->d_sectorsize;
518		block = bp->bio_pblkno;
519		end = bp->bio_pblkno + (bp->bio_bcount / sz);
520		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
521			/* Access to the remaining erase block obsoletes it. */
522			if (block < sc->eend && end > sc->eblock)
523				sc->eblock = sc->eend = 0;
524			block = mmcsd_rw(sc, bp);
525		} else if (bp->bio_cmd == BIO_DELETE) {
526			block = mmcsd_delete(sc, bp);
527		}
528		MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
529		if (block < end) {
530			bp->bio_error = EIO;
531			bp->bio_resid = (end - block) * sz;
532			bp->bio_flags |= BIO_ERROR;
533		}
534		biodone(bp);
535	}
536out:
537	/* tell parent we're done */
538	sc->running = -1;
539	MMCSD_UNLOCK(sc);
540	wakeup(sc);
541
542	kproc_exit(0);
543}
544
545static int
546mmcsd_bus_bit_width(device_t dev)
547{
548
549	if (mmc_get_bus_width(dev) == bus_width_1)
550		return (1);
551	if (mmc_get_bus_width(dev) == bus_width_4)
552		return (4);
553	return (8);
554}
555
556static device_method_t mmcsd_methods[] = {
557	DEVMETHOD(device_probe, mmcsd_probe),
558	DEVMETHOD(device_attach, mmcsd_attach),
559	DEVMETHOD(device_detach, mmcsd_detach),
560	DEVMETHOD(device_suspend, mmcsd_suspend),
561	DEVMETHOD(device_resume, mmcsd_resume),
562	DEVMETHOD_END
563};
564
565static driver_t mmcsd_driver = {
566	"mmcsd",
567	mmcsd_methods,
568	sizeof(struct mmcsd_softc),
569};
570static devclass_t mmcsd_devclass;
571
572DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL);
573