mmcsd.c revision 183467
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 183467 2008-09-29 18:05:26Z imp $");
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
74struct 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_READ_BROKEN
84
85/* bus entry points */
86static int mmcsd_probe(device_t dev);
87static int mmcsd_attach(device_t dev);
88static int mmcsd_detach(device_t dev);
89
90/* disk routines */
91static int mmcsd_open(struct disk *dp);
92static int mmcsd_close(struct disk *dp);
93static void mmcsd_strategy(struct bio *bp);
94static void mmcsd_task(void *arg);
95
96#define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
97#define	MMCSD_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
98#define MMCSD_LOCK_INIT(_sc) \
99	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
100	    "mmcsd", MTX_DEF)
101#define MMCSD_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
102#define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
103#define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
104
105static int
106mmcsd_probe(device_t dev)
107{
108
109	device_set_desc(dev, "mmc or sd flash card");
110	return (0);
111}
112
113static int
114mmcsd_attach(device_t dev)
115{
116	struct mmcsd_softc *sc;
117
118	sc = device_get_softc(dev);
119	sc->dev = dev;
120	MMCSD_LOCK_INIT(sc);
121
122	sc->disk = disk_alloc();
123	sc->disk->d_open = mmcsd_open;
124	sc->disk->d_close = mmcsd_close;
125	sc->disk->d_strategy = mmcsd_strategy;
126	// sc->disk->d_dump = mmcsd_dump;	Need polling mmc layer
127	sc->disk->d_name = "mmcsd";
128	sc->disk->d_drv1 = sc;
129	sc->disk->d_maxsize = DFLTPHYS;
130	sc->disk->d_sectorsize = mmc_get_sector_size(dev);
131	sc->disk->d_mediasize = mmc_get_media_size(dev);
132	sc->disk->d_unit = device_get_unit(dev);
133	disk_create(sc->disk, DISK_VERSION);
134	bioq_init(&sc->bio_queue);
135
136	sc->running = 1;
137	kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
138
139	return (0);
140}
141
142static int
143mmcsd_detach(device_t dev)
144{
145	struct mmcsd_softc *sc = device_get_softc(dev);
146
147	/* kill thread */
148	MMCSD_LOCK(sc);
149	sc->running = 0;
150	wakeup(sc);
151	MMCSD_UNLOCK(sc);
152
153	/* wait for thread to finish.  XXX probably want timeout.  -sorbo */
154	MMCSD_LOCK(sc);
155	while (sc->running != -1)
156		msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0);
157	MMCSD_UNLOCK(sc);
158
159	/* kill disk */
160	disk_destroy(sc->disk);
161	/* XXX destroy anything in queue */
162
163	MMCSD_LOCK_DESTROY(sc);
164
165	return (0);
166}
167
168static int
169mmcsd_open(struct disk *dp)
170{
171	return (0);
172}
173
174static int
175mmcsd_close(struct disk *dp)
176{
177	return (0);
178}
179
180static void
181mmcsd_strategy(struct bio *bp)
182{
183	struct mmcsd_softc *sc;
184
185	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
186	MMCSD_LOCK(sc);
187	bioq_disksort(&sc->bio_queue, bp);
188	wakeup(sc);
189	MMCSD_UNLOCK(sc);
190}
191
192static void
193mmcsd_task(void *arg)
194{
195	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
196	struct bio *bp;
197	int sz;
198	daddr_t block, end;
199	struct mmc_command cmd;
200	struct mmc_command stop;
201	struct mmc_request req;
202	struct mmc_data data;
203	device_t dev;
204
205	dev = sc->dev;
206	while (sc->running) {
207		MMCSD_LOCK(sc);
208		do {
209			bp = bioq_first(&sc->bio_queue);
210			if (bp == NULL)
211				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
212		} while (bp == NULL && sc->running);
213		if (bp)
214			bioq_remove(&sc->bio_queue, bp);
215		MMCSD_UNLOCK(sc);
216		if (!sc->running)
217			break;
218//		printf("mmc_task: request %p for block %ju\n", bp, bp->bio_pblkno);
219		if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
220			bp->bio_error = EROFS;
221			bp->bio_resid = bp->bio_bcount;
222			bp->bio_flags |= BIO_ERROR;
223			biodone(bp);
224			continue;
225		}
226		MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
227		sz = sc->disk->d_sectorsize;
228		end = bp->bio_pblkno + (bp->bio_bcount / sz);
229		// XXX should use read/write_mulit
230		for (block = bp->bio_pblkno; block < end;) {
231			char *vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
232			memset(&req, 0, sizeof(req));
233			memset(&cmd, 0, sizeof(cmd));
234			memset(&stop, 0, sizeof(stop));
235			req.cmd = &cmd;
236			cmd.data = &data;
237			if (bp->bio_cmd == BIO_READ) {
238#ifdef MULTI_BLOCK_READ
239				if (end - block > 1)
240					cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
241				else
242					cmd.opcode = MMC_READ_SINGLE_BLOCK;
243#else
244				cmd.opcode = MMC_READ_SINGLE_BLOCK;
245#endif
246			} else
247				cmd.opcode = MMC_WRITE_BLOCK;
248			cmd.arg = block << 9;
249			cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
250			// data.timeout_ns = ;
251			// data.timeout_clks = ;
252			data.data = vaddr;
253			data.mrq = &req;
254			if (bp->bio_cmd == BIO_READ) {
255				data.flags = MMC_DATA_READ;
256#ifdef MULTI_BLOCK_READ
257				data.len = bp->bio_bcount;
258				if (end - block > 1) {
259					req.stop = &stop;
260					data.flags |= MMC_DATA_MULTI;
261				}
262				printf("Len %d  %lld-%lld flags %#x sz %d\n",
263				    data.len, block, end, data.flags, sz);
264				block = end;
265#else
266				data.len = sz;
267				block++;
268#endif
269			} else {
270				data.flags = MMC_DATA_WRITE;
271				data.len = sz;
272				block++;
273			}
274			stop.opcode = MMC_STOP_TRANSMISSION;
275			stop.arg = 0;
276			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
277			MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
278			    &req);
279			// XXX error handling
280//XXX			while (!(mmc_send_status(dev) & R1_READY_FOR_DATA))
281//				continue;
282			// XXX decode mmc status
283		}
284		MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
285		biodone(bp);
286	}
287
288	/* tell parent we're done */
289	MMCSD_LOCK(sc);
290	sc->running = -1;
291	wakeup(sc);
292	MMCSD_UNLOCK(sc);
293
294	kproc_exit(0);
295}
296
297static device_method_t mmcsd_methods[] = {
298	DEVMETHOD(device_probe, mmcsd_probe),
299	DEVMETHOD(device_attach, mmcsd_attach),
300	DEVMETHOD(device_detach, mmcsd_detach),
301	{0, 0},
302};
303
304static driver_t mmcsd_driver = {
305	"mmcsd",
306	mmcsd_methods,
307	sizeof(struct mmcsd_softc),
308};
309static devclass_t mmcsd_devclass;
310
311
312DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0);
313