xref: /freebsd/sys/dev/mmc/mmcsd.c (revision 0e1466ac)
1114b4164SWarner Losh /*-
2114b4164SWarner Losh  * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3114b4164SWarner Losh  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4114b4164SWarner Losh  *
5114b4164SWarner Losh  * Redistribution and use in source and binary forms, with or without
6114b4164SWarner Losh  * modification, are permitted provided that the following conditions
7114b4164SWarner Losh  * are met:
8114b4164SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9114b4164SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10114b4164SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11114b4164SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12114b4164SWarner Losh  *    documentation and/or other materials provided with the distribution.
13114b4164SWarner Losh  *
14114b4164SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15114b4164SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16114b4164SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17114b4164SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18114b4164SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19114b4164SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20114b4164SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21114b4164SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22114b4164SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23114b4164SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24114b4164SWarner Losh  */
25114b4164SWarner Losh 
26114b4164SWarner Losh #include <sys/cdefs.h>
27114b4164SWarner Losh __FBSDID("$FreeBSD$");
28114b4164SWarner Losh 
29114b4164SWarner Losh #include <sys/param.h>
30114b4164SWarner Losh #include <sys/systm.h>
31114b4164SWarner Losh #include <sys/bio.h>
32114b4164SWarner Losh #include <sys/bus.h>
33114b4164SWarner Losh #include <sys/conf.h>
34114b4164SWarner Losh #include <sys/kernel.h>
35114b4164SWarner Losh #include <sys/kthread.h>
36114b4164SWarner Losh #include <sys/lock.h>
37114b4164SWarner Losh #include <sys/malloc.h>
38114b4164SWarner Losh #include <sys/module.h>
39114b4164SWarner Losh #include <sys/mutex.h>
40114b4164SWarner Losh #include <geom/geom_disk.h>
41114b4164SWarner Losh 
42114b4164SWarner Losh #include <dev/mmc/mmcvar.h>
43114b4164SWarner Losh #include <dev/mmc/mmcreg.h>
44114b4164SWarner Losh 
45114b4164SWarner Losh #include "mmcbus_if.h"
46114b4164SWarner Losh 
47114b4164SWarner Losh struct mmcsd_softc {
48114b4164SWarner Losh 	device_t dev;
49114b4164SWarner Losh 	struct mtx sc_mtx;
50114b4164SWarner Losh 	struct disk *disk;
51114b4164SWarner Losh 	struct proc *p;
52114b4164SWarner Losh 	struct bio_queue_head bio_queue;
530e1466acSWarner Losh 	int running;
54114b4164SWarner Losh };
55114b4164SWarner Losh 
56114b4164SWarner Losh #define	MULTI_BLOCK_READ_BROKEN
57114b4164SWarner Losh 
58114b4164SWarner Losh /* bus entry points */
59114b4164SWarner Losh static int mmcsd_probe(device_t dev);
60114b4164SWarner Losh static int mmcsd_attach(device_t dev);
61114b4164SWarner Losh static int mmcsd_detach(device_t dev);
62114b4164SWarner Losh 
63114b4164SWarner Losh /* disk routines */
64114b4164SWarner Losh static int mmcsd_open(struct disk *dp);
65114b4164SWarner Losh static int mmcsd_close(struct disk *dp);
66114b4164SWarner Losh static void mmcsd_strategy(struct bio *bp);
67114b4164SWarner Losh static void mmcsd_task(void *arg);
68114b4164SWarner Losh 
69114b4164SWarner Losh #define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
70114b4164SWarner Losh #define	MMCSD_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
71114b4164SWarner Losh #define MMCSD_LOCK_INIT(_sc) \
72114b4164SWarner Losh 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
73114b4164SWarner Losh 	    "mmcsd", MTX_DEF)
74114b4164SWarner Losh #define MMCSD_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
75114b4164SWarner Losh #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
76114b4164SWarner Losh #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
77114b4164SWarner Losh 
78114b4164SWarner Losh static int
79114b4164SWarner Losh mmcsd_probe(device_t dev)
80114b4164SWarner Losh {
81114b4164SWarner Losh 
82114b4164SWarner Losh 	device_set_desc(dev, "mmc or sd flash card");
83114b4164SWarner Losh 	return (0);
84114b4164SWarner Losh }
85114b4164SWarner Losh 
86114b4164SWarner Losh static int
87114b4164SWarner Losh mmcsd_attach(device_t dev)
88114b4164SWarner Losh {
89114b4164SWarner Losh 	struct mmcsd_softc *sc;
90114b4164SWarner Losh 
91114b4164SWarner Losh 	sc = device_get_softc(dev);
92114b4164SWarner Losh 	sc->dev = dev;
93114b4164SWarner Losh 	MMCSD_LOCK_INIT(sc);
94114b4164SWarner Losh 
95114b4164SWarner Losh 	sc->disk = disk_alloc();
96114b4164SWarner Losh 	sc->disk->d_open = mmcsd_open;
97114b4164SWarner Losh 	sc->disk->d_close = mmcsd_close;
98114b4164SWarner Losh 	sc->disk->d_strategy = mmcsd_strategy;
99114b4164SWarner Losh 	// sc->disk->d_dump = mmcsd_dump;	Need polling mmc layer
100114b4164SWarner Losh 	sc->disk->d_name = "mmcsd";
101114b4164SWarner Losh 	sc->disk->d_drv1 = sc;
102114b4164SWarner Losh 	sc->disk->d_maxsize = DFLTPHYS;
103114b4164SWarner Losh 	sc->disk->d_sectorsize = mmc_get_sector_size(dev);
104114b4164SWarner Losh 	sc->disk->d_mediasize = mmc_get_media_size(dev);
105114b4164SWarner Losh 	sc->disk->d_unit = device_get_unit(dev);
106114b4164SWarner Losh 	disk_create(sc->disk, DISK_VERSION);
107114b4164SWarner Losh 	bioq_init(&sc->bio_queue);
1080e1466acSWarner Losh 
1090e1466acSWarner Losh 	sc->running = 1;
110114b4164SWarner Losh 	kthread_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
111114b4164SWarner Losh 
112114b4164SWarner Losh 	return (0);
113114b4164SWarner Losh }
114114b4164SWarner Losh 
115114b4164SWarner Losh static int
116114b4164SWarner Losh mmcsd_detach(device_t dev)
117114b4164SWarner Losh {
1180e1466acSWarner Losh 	struct mmcsd_softc *sc = device_get_softc(dev);
1190e1466acSWarner Losh 
1200e1466acSWarner Losh 	/* kill thread */
1210e1466acSWarner Losh 	MMCSD_LOCK(sc);
1220e1466acSWarner Losh 	sc->running = 0;
1230e1466acSWarner Losh 	wakeup(sc);
1240e1466acSWarner Losh 	MMCSD_UNLOCK(sc);
1250e1466acSWarner Losh 
1260e1466acSWarner Losh 	/* wait for thread to finish.  XXX probably want timeout.  -sorbo */
1270e1466acSWarner Losh 	MMCSD_LOCK(sc);
1280e1466acSWarner Losh 	while (sc->running != -1)
1290e1466acSWarner Losh 		msleep(sc, &sc->sc_mtx, PRIBIO, "detach", 0);
1300e1466acSWarner Losh 	MMCSD_UNLOCK(sc);
1310e1466acSWarner Losh 
1320e1466acSWarner Losh 	/* kill disk */
1330e1466acSWarner Losh 	disk_destroy(sc->disk);
1340e1466acSWarner Losh 	/* XXX destroy anything in queue */
1350e1466acSWarner Losh 
1360e1466acSWarner Losh 	MMCSD_LOCK_DESTROY(sc);
1370e1466acSWarner Losh 
1380e1466acSWarner Losh 	return 0;
139114b4164SWarner Losh }
140114b4164SWarner Losh 
141114b4164SWarner Losh static int
142114b4164SWarner Losh mmcsd_open(struct disk *dp)
143114b4164SWarner Losh {
144114b4164SWarner Losh 	return 0;
145114b4164SWarner Losh }
146114b4164SWarner Losh 
147114b4164SWarner Losh static int
148114b4164SWarner Losh mmcsd_close(struct disk *dp)
149114b4164SWarner Losh {
150114b4164SWarner Losh 	return 0;
151114b4164SWarner Losh }
152114b4164SWarner Losh 
153114b4164SWarner Losh static void
154114b4164SWarner Losh mmcsd_strategy(struct bio *bp)
155114b4164SWarner Losh {
156114b4164SWarner Losh 	struct mmcsd_softc *sc;
157114b4164SWarner Losh 
158114b4164SWarner Losh 	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
159114b4164SWarner Losh 	MMCSD_LOCK(sc);
160114b4164SWarner Losh 	bioq_disksort(&sc->bio_queue, bp);
161114b4164SWarner Losh 	wakeup(sc);
162114b4164SWarner Losh 	MMCSD_UNLOCK(sc);
163114b4164SWarner Losh }
164114b4164SWarner Losh 
165114b4164SWarner Losh static void
166114b4164SWarner Losh mmcsd_task(void *arg)
167114b4164SWarner Losh {
168114b4164SWarner Losh 	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
169114b4164SWarner Losh 	struct bio *bp;
170114b4164SWarner Losh 	int sz;
171114b4164SWarner Losh 	daddr_t block, end;
172114b4164SWarner Losh 	struct mmc_command cmd;
173114b4164SWarner Losh 	struct mmc_command stop;
174114b4164SWarner Losh 	struct mmc_request req;
175114b4164SWarner Losh 	struct mmc_data data;
176114b4164SWarner Losh 	device_t dev;
177114b4164SWarner Losh 
178114b4164SWarner Losh 	dev = sc->dev;
1790e1466acSWarner Losh 	while (sc->running) {
180114b4164SWarner Losh 		MMCSD_LOCK(sc);
181114b4164SWarner Losh 		do {
182114b4164SWarner Losh 			bp = bioq_first(&sc->bio_queue);
183114b4164SWarner Losh 			if (bp == NULL)
184114b4164SWarner Losh 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
1850e1466acSWarner Losh 		} while (bp == NULL && sc->running);
1860e1466acSWarner Losh 		if (bp)
187114b4164SWarner Losh 			bioq_remove(&sc->bio_queue, bp);
188114b4164SWarner Losh 		MMCSD_UNLOCK(sc);
1890e1466acSWarner Losh 		if (!sc->running)
1900e1466acSWarner Losh 			break;
191114b4164SWarner Losh 		MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
192114b4164SWarner Losh //		printf("mmc_task: request %p for block %lld\n", bp, bp->bio_pblkno);
193114b4164SWarner Losh 		sz = sc->disk->d_sectorsize;
194114b4164SWarner Losh 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
195114b4164SWarner Losh 		// XXX should use read/write_mulit
196114b4164SWarner Losh 		for (block = bp->bio_pblkno; block < end;) {
197114b4164SWarner Losh 			char *vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
198114b4164SWarner Losh 			memset(&req, 0, sizeof(req));
199114b4164SWarner Losh 			memset(&cmd, 0, sizeof(cmd));
200114b4164SWarner Losh 			memset(&stop, 0, sizeof(stop));
201114b4164SWarner Losh 			req.cmd = &cmd;
202114b4164SWarner Losh 			cmd.data = &data;
203114b4164SWarner Losh 			if (bp->bio_cmd == BIO_READ) {
204114b4164SWarner Losh #ifdef MULTI_BLOCK_READ
205114b4164SWarner Losh 				if (end - block > 1)
206114b4164SWarner Losh 					cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
207114b4164SWarner Losh 				else
208114b4164SWarner Losh 					cmd.opcode = MMC_READ_SINGLE_BLOCK;
209114b4164SWarner Losh #else
210114b4164SWarner Losh 				cmd.opcode = MMC_READ_SINGLE_BLOCK;
211114b4164SWarner Losh #endif
212114b4164SWarner Losh 			} else
213114b4164SWarner Losh 				cmd.opcode = MMC_WRITE_BLOCK;
214114b4164SWarner Losh 			cmd.arg = block << 9;
215114b4164SWarner Losh 			cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
216114b4164SWarner Losh 			// data.timeout_ns = ;
217114b4164SWarner Losh 			// data.timeout_clks = ;
218114b4164SWarner Losh 			data.data = vaddr;
219114b4164SWarner Losh 			data.mrq = &req;
220114b4164SWarner Losh 			if (bp->bio_cmd == BIO_READ) {
221114b4164SWarner Losh 				data.flags = MMC_DATA_READ;
222114b4164SWarner Losh #ifdef MULTI_BLOCK_READ
223114b4164SWarner Losh 				data.len = bp->bio_bcount;
224114b4164SWarner Losh 				if (end - block > 1) {
225114b4164SWarner Losh 					req.stop = &stop;
226114b4164SWarner Losh 					data.flags |= MMC_DATA_MULTI;
227114b4164SWarner Losh 				}
228114b4164SWarner Losh 				printf("Len %d  %lld-%lld flags %#x sz %d\n",
229114b4164SWarner Losh 				    data.len, block, end, data.flags, sz);
230114b4164SWarner Losh 				block = end;
231114b4164SWarner Losh #else
232114b4164SWarner Losh 				data.len = sz;
233114b4164SWarner Losh 				block++;
234114b4164SWarner Losh #endif
235114b4164SWarner Losh 			} else {
236114b4164SWarner Losh 				data.flags = MMC_DATA_WRITE;
237114b4164SWarner Losh 				data.len = sz;
238114b4164SWarner Losh 				block++;
239114b4164SWarner Losh 			}
240114b4164SWarner Losh 			stop.opcode = MMC_STOP_TRANSMISSION;
241114b4164SWarner Losh 			stop.arg = 0;
242114b4164SWarner Losh 			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
243114b4164SWarner Losh 			MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
244114b4164SWarner Losh 			    &req);
245114b4164SWarner Losh 			// XXX error handling
246114b4164SWarner Losh //XXX			while (!(mmc_send_status(dev) & R1_READY_FOR_DATA))
247114b4164SWarner Losh //				continue;
248114b4164SWarner Losh 			// XXX decode mmc status
249114b4164SWarner Losh 		}
250114b4164SWarner Losh 		MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
251114b4164SWarner Losh 		biodone(bp);
252114b4164SWarner Losh 	}
2530e1466acSWarner Losh 
2540e1466acSWarner Losh 	/* tell parent we're done */
2550e1466acSWarner Losh 	MMCSD_LOCK(sc);
2560e1466acSWarner Losh 	sc->running = -1;
2570e1466acSWarner Losh 	wakeup(sc);
2580e1466acSWarner Losh 	MMCSD_UNLOCK(sc);
2590e1466acSWarner Losh 
2600e1466acSWarner Losh 	kthread_exit(0);
261114b4164SWarner Losh }
262114b4164SWarner Losh 
263114b4164SWarner Losh static device_method_t mmcsd_methods[] = {
264114b4164SWarner Losh 	DEVMETHOD(device_probe, mmcsd_probe),
265114b4164SWarner Losh 	DEVMETHOD(device_attach, mmcsd_attach),
266114b4164SWarner Losh 	DEVMETHOD(device_detach, mmcsd_detach),
267114b4164SWarner Losh 	{0, 0},
268114b4164SWarner Losh };
269114b4164SWarner Losh 
270114b4164SWarner Losh static driver_t mmcsd_driver = {
271114b4164SWarner Losh 	"mmcsd",
272114b4164SWarner Losh 	mmcsd_methods,
273114b4164SWarner Losh 	sizeof(struct mmcsd_softc),
274114b4164SWarner Losh };
275114b4164SWarner Losh static devclass_t mmcsd_devclass;
276114b4164SWarner Losh 
277114b4164SWarner Losh 
278114b4164SWarner Losh DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0);
279