mmcsd.c revision 183704
1163516Simp/*-
2163516Simp * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3163516Simp * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4163516Simp *
5163516Simp * Redistribution and use in source and binary forms, with or without
6163516Simp * modification, are permitted provided that the following conditions
7163516Simp * are met:
8163516Simp * 1. Redistributions of source code must retain the above copyright
9163516Simp *    notice, this list of conditions and the following disclaimer.
10163516Simp * 2. Redistributions in binary form must reproduce the above copyright
11163516Simp *    notice, this list of conditions and the following disclaimer in the
12163516Simp *    documentation and/or other materials provided with the distribution.
13163516Simp *
14163516Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15163516Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16163516Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17163516Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18163516Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19163516Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20163516Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21163516Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22163516Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23163516Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24170002Simp *
25170002Simp * Portions of this software may have been developed with reference to
26170002Simp * the SD Simplified Specification.  The following disclaimer may apply:
27170002Simp *
28170002Simp * The following conditions apply to the release of the simplified
29170002Simp * specification ("Simplified Specification") by the SD Card Association and
30170002Simp * the SD Group. The Simplified Specification is a subset of the complete SD
31170002Simp * Specification which is owned by the SD Card Association and the SD
32170002Simp * Group. This Simplified Specification is provided on a non-confidential
33170002Simp * basis subject to the disclaimers below. Any implementation of the
34170002Simp * Simplified Specification may require a license from the SD Card
35170002Simp * Association, SD Group, SD-3C LLC or other third parties.
36170002Simp *
37170002Simp * Disclaimers:
38170002Simp *
39170002Simp * The information contained in the Simplified Specification is presented only
40170002Simp * as a standard specification for SD Cards and SD Host/Ancillary products and
41170002Simp * is provided "AS-IS" without any representations or warranties of any
42170002Simp * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
43170002Simp * Card Association for any damages, any infringements of patents or other
44170002Simp * right of the SD Group, SD-3C LLC, the SD Card Association or any third
45170002Simp * parties, which may result from its use. No license is granted by
46170002Simp * implication, estoppel or otherwise under any patent or other rights of the
47170002Simp * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48170002Simp * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49170002Simp * or the SD Card Association to disclose or distribute any technical
50170002Simp * information, know-how or other confidential information to any third party.
51163516Simp */
52163516Simp
53163516Simp#include <sys/cdefs.h>
54163516Simp__FBSDID("$FreeBSD: head/sys/dev/mmc/mmcsd.c 183704 2008-10-08 17:35:41Z mav $");
55163516Simp
56163516Simp#include <sys/param.h>
57163516Simp#include <sys/systm.h>
58163516Simp#include <sys/bio.h>
59163516Simp#include <sys/bus.h>
60163516Simp#include <sys/conf.h>
61163516Simp#include <sys/kernel.h>
62163516Simp#include <sys/kthread.h>
63163516Simp#include <sys/lock.h>
64163516Simp#include <sys/malloc.h>
65163516Simp#include <sys/module.h>
66163516Simp#include <sys/mutex.h>
67163516Simp#include <geom/geom_disk.h>
68163516Simp
69163516Simp#include <dev/mmc/mmcvar.h>
70163516Simp#include <dev/mmc/mmcreg.h>
71163516Simp
72163516Simp#include "mmcbus_if.h"
73163516Simp
74163516Simpstruct mmcsd_softc {
75163516Simp	device_t dev;
76163516Simp	struct mtx sc_mtx;
77163516Simp	struct disk *disk;
78163516Simp	struct proc *p;
79163516Simp	struct bio_queue_head bio_queue;
80169567Simp	int running;
81163516Simp};
82163516Simp
83183480Simp#define	MULTI_BLOCK_BROKEN
84163516Simp
85163516Simp/* bus entry points */
86163516Simpstatic int mmcsd_probe(device_t dev);
87163516Simpstatic int mmcsd_attach(device_t dev);
88163516Simpstatic int mmcsd_detach(device_t dev);
89163516Simp
90163516Simp/* disk routines */
91163516Simpstatic int mmcsd_open(struct disk *dp);
92163516Simpstatic int mmcsd_close(struct disk *dp);
93163516Simpstatic void mmcsd_strategy(struct bio *bp);
94163516Simpstatic void mmcsd_task(void *arg);
95163516Simp
96163516Simp#define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
97163516Simp#define	MMCSD_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
98163516Simp#define MMCSD_LOCK_INIT(_sc) \
99163516Simp	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
100163516Simp	    "mmcsd", MTX_DEF)
101163516Simp#define MMCSD_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
102163516Simp#define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
103163516Simp#define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
104163516Simp
105163516Simpstatic int
106163516Simpmmcsd_probe(device_t dev)
107163516Simp{
108163516Simp
109183704Smav	device_quiet(dev);
110183480Simp	device_set_desc(dev, "MMC/SD Memory Card");
111163516Simp	return (0);
112163516Simp}
113163516Simp
114163516Simpstatic int
115163516Simpmmcsd_attach(device_t dev)
116163516Simp{
117163516Simp	struct mmcsd_softc *sc;
118163516Simp
119163516Simp	sc = device_get_softc(dev);
120163516Simp	sc->dev = dev;
121163516Simp	MMCSD_LOCK_INIT(sc);
122163516Simp
123163516Simp	sc->disk = disk_alloc();
124163516Simp	sc->disk->d_open = mmcsd_open;
125163516Simp	sc->disk->d_close = mmcsd_close;
126163516Simp	sc->disk->d_strategy = mmcsd_strategy;
127163516Simp	// sc->disk->d_dump = mmcsd_dump;	Need polling mmc layer
128163516Simp	sc->disk->d_name = "mmcsd";
129163516Simp	sc->disk->d_drv1 = sc;
130183541Simp	sc->disk->d_maxsize = MAXPHYS;		/* Maybe ask bridge? */
131163516Simp	sc->disk->d_sectorsize = mmc_get_sector_size(dev);
132183542Simp	sc->disk->d_mediasize = mmc_get_media_size(dev) *
133183480Simp	    mmc_get_sector_size(dev);
134163516Simp	sc->disk->d_unit = device_get_unit(dev);
135183480Simp
136183480Simp	device_printf(dev, "%juMB <MMC/SD Memory Card>%s at %s\n",
137183480Simp	    sc->disk->d_mediasize / 1048576,
138183480Simp	    mmc_get_read_only(dev)?" (read-only)":"",
139183480Simp	    device_get_nameunit(device_get_parent(sc->dev)));
140163516Simp	disk_create(sc->disk, DISK_VERSION);
141163516Simp	bioq_init(&sc->bio_queue);
142169567Simp
143169567Simp	sc->running = 1;
144172836Sjulian	kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
145163516Simp
146163516Simp	return (0);
147163516Simp}
148163516Simp
149163516Simpstatic int
150163516Simpmmcsd_detach(device_t dev)
151163516Simp{
152169567Simp	struct mmcsd_softc *sc = device_get_softc(dev);
153169567Simp
154169567Simp	/* kill thread */
155169567Simp	MMCSD_LOCK(sc);
156169567Simp	sc->running = 0;
157169567Simp	wakeup(sc);
158169567Simp	MMCSD_UNLOCK(sc);
159169567Simp
160169567Simp	/* wait for thread to finish.  XXX probably want timeout.  -sorbo */
161169567Simp	MMCSD_LOCK(sc);
162169567Simp	while (sc->running != -1)
163169567Simp		msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0);
164169567Simp	MMCSD_UNLOCK(sc);
165169567Simp
166169567Simp	/* kill disk */
167169567Simp	disk_destroy(sc->disk);
168169567Simp	/* XXX destroy anything in queue */
169169567Simp
170169567Simp	MMCSD_LOCK_DESTROY(sc);
171169567Simp
172183467Simp	return (0);
173163516Simp}
174163516Simp
175163516Simpstatic int
176163516Simpmmcsd_open(struct disk *dp)
177163516Simp{
178183467Simp	return (0);
179163516Simp}
180163516Simp
181163516Simpstatic int
182163516Simpmmcsd_close(struct disk *dp)
183163516Simp{
184183467Simp	return (0);
185163516Simp}
186163516Simp
187163516Simpstatic void
188163516Simpmmcsd_strategy(struct bio *bp)
189163516Simp{
190163516Simp	struct mmcsd_softc *sc;
191163516Simp
192163516Simp	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
193163516Simp	MMCSD_LOCK(sc);
194163516Simp	bioq_disksort(&sc->bio_queue, bp);
195163516Simp	wakeup(sc);
196163516Simp	MMCSD_UNLOCK(sc);
197163516Simp}
198163516Simp
199163516Simpstatic void
200163516Simpmmcsd_task(void *arg)
201163516Simp{
202163516Simp	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
203163516Simp	struct bio *bp;
204163516Simp	int sz;
205163516Simp	daddr_t block, end;
206163516Simp	struct mmc_command cmd;
207163516Simp	struct mmc_command stop;
208163516Simp	struct mmc_request req;
209163516Simp	struct mmc_data data;
210163516Simp	device_t dev;
211163516Simp
212163516Simp	dev = sc->dev;
213169567Simp	while (sc->running) {
214163516Simp		MMCSD_LOCK(sc);
215163516Simp		do {
216163516Simp			bp = bioq_first(&sc->bio_queue);
217163516Simp			if (bp == NULL)
218163516Simp				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
219169567Simp		} while (bp == NULL && sc->running);
220169567Simp		if (bp)
221169567Simp			bioq_remove(&sc->bio_queue, bp);
222163516Simp		MMCSD_UNLOCK(sc);
223169567Simp		if (!sc->running)
224169567Simp			break;
225183448Simp//		printf("mmc_task: request %p for block %ju\n", bp, bp->bio_pblkno);
226183448Simp		if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
227183448Simp			bp->bio_error = EROFS;
228183448Simp			bp->bio_resid = bp->bio_bcount;
229183448Simp			bp->bio_flags |= BIO_ERROR;
230183448Simp			biodone(bp);
231183448Simp			continue;
232183448Simp		}
233163516Simp		MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
234163516Simp		sz = sc->disk->d_sectorsize;
235163516Simp		end = bp->bio_pblkno + (bp->bio_bcount / sz);
236163516Simp		for (block = bp->bio_pblkno; block < end;) {
237163516Simp			char *vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
238183480Simp			int numblocks;
239183480Simp#ifdef MULTI_BLOCK
240183480Simp			numblocks = end - block;
241183480Simp#else
242183480Simp			numblocks = 1;
243183480Simp#endif
244163516Simp			memset(&req, 0, sizeof(req));
245163516Simp			memset(&cmd, 0, sizeof(cmd));
246163516Simp			memset(&stop, 0, sizeof(stop));
247163516Simp			req.cmd = &cmd;
248163516Simp			cmd.data = &data;
249163516Simp			if (bp->bio_cmd == BIO_READ) {
250183480Simp				if (numblocks > 1)
251163516Simp					cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
252163516Simp				else
253163516Simp					cmd.opcode = MMC_READ_SINGLE_BLOCK;
254183480Simp			} else {
255183480Simp				if (numblocks > 1)
256183480Simp					cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
257183480Simp				else
258183480Simp					cmd.opcode = MMC_WRITE_BLOCK;
259183480Simp			}
260183704Smav			cmd.arg = block;
261183704Smav			if (!mmc_get_high_cap(dev))
262183704Smav				cmd.arg <<= 9;
263163516Simp			cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
264163516Simp			data.data = vaddr;
265163516Simp			data.mrq = &req;
266183480Simp			if (bp->bio_cmd == BIO_READ)
267163516Simp				data.flags = MMC_DATA_READ;
268183480Simp			else
269163516Simp				data.flags = MMC_DATA_WRITE;
270183480Simp			data.len = numblocks * sz;
271183480Simp			if (numblocks > 1) {
272183480Simp				data.flags |= MMC_DATA_MULTI;
273183480Simp				stop.opcode = MMC_STOP_TRANSMISSION;
274183480Simp				stop.arg = 0;
275183480Simp				stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
276183480Simp				req.stop = &stop;
277163516Simp			}
278183480Simp//			printf("Len %d  %lld-%lld flags %#x sz %d\n",
279183480Simp//			    (int)data.len, (long long)block, (long long)end, data.flags, sz);
280163516Simp			MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
281163516Simp			    &req);
282183480Simp			if (req.cmd->error != MMC_ERR_NONE)
283183480Simp				break;
284183480Simp			block += numblocks;
285163516Simp		}
286163516Simp		MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
287183480Simp		if (block < end) {
288183480Simp			bp->bio_error = EIO;
289183480Simp			bp->bio_resid = (end - block) * sz;
290183480Simp			bp->bio_flags |= BIO_ERROR;
291183480Simp		}
292163516Simp		biodone(bp);
293163516Simp	}
294169567Simp
295169567Simp	/* tell parent we're done */
296169567Simp	MMCSD_LOCK(sc);
297169567Simp	sc->running = -1;
298169567Simp	wakeup(sc);
299169567Simp	MMCSD_UNLOCK(sc);
300169567Simp
301172836Sjulian	kproc_exit(0);
302163516Simp}
303163516Simp
304163516Simpstatic device_method_t mmcsd_methods[] = {
305163516Simp	DEVMETHOD(device_probe, mmcsd_probe),
306163516Simp	DEVMETHOD(device_attach, mmcsd_attach),
307163516Simp	DEVMETHOD(device_detach, mmcsd_detach),
308163516Simp	{0, 0},
309163516Simp};
310163516Simp
311163516Simpstatic driver_t mmcsd_driver = {
312163516Simp	"mmcsd",
313163516Simp	mmcsd_methods,
314163516Simp	sizeof(struct mmcsd_softc),
315163516Simp};
316163516Simpstatic devclass_t mmcsd_devclass;
317163516Simp
318163516Simp
319163516SimpDRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0);
320