xref: /freebsd/sys/dev/mmc/mmcsd.c (revision 114b4164ddcb5c7b6b98fbe329d5544ece25ec9f)
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;
53114b4164SWarner Losh };
54114b4164SWarner Losh 
55114b4164SWarner Losh #define	MULTI_BLOCK_READ_BROKEN
56114b4164SWarner Losh 
57114b4164SWarner Losh /* bus entry points */
58114b4164SWarner Losh static int mmcsd_probe(device_t dev);
59114b4164SWarner Losh static int mmcsd_attach(device_t dev);
60114b4164SWarner Losh static int mmcsd_detach(device_t dev);
61114b4164SWarner Losh 
62114b4164SWarner Losh /* disk routines */
63114b4164SWarner Losh static int mmcsd_open(struct disk *dp);
64114b4164SWarner Losh static int mmcsd_close(struct disk *dp);
65114b4164SWarner Losh static void mmcsd_strategy(struct bio *bp);
66114b4164SWarner Losh static void mmcsd_task(void *arg);
67114b4164SWarner Losh 
68114b4164SWarner Losh #define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
69114b4164SWarner Losh #define	MMCSD_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
70114b4164SWarner Losh #define MMCSD_LOCK_INIT(_sc) \
71114b4164SWarner Losh 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
72114b4164SWarner Losh 	    "mmcsd", MTX_DEF)
73114b4164SWarner Losh #define MMCSD_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
74114b4164SWarner Losh #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
75114b4164SWarner Losh #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
76114b4164SWarner Losh 
77114b4164SWarner Losh static int
78114b4164SWarner Losh mmcsd_probe(device_t dev)
79114b4164SWarner Losh {
80114b4164SWarner Losh 
81114b4164SWarner Losh 	device_set_desc(dev, "mmc or sd flash card");
82114b4164SWarner Losh 	return (0);
83114b4164SWarner Losh }
84114b4164SWarner Losh 
85114b4164SWarner Losh static int
86114b4164SWarner Losh mmcsd_attach(device_t dev)
87114b4164SWarner Losh {
88114b4164SWarner Losh 	struct mmcsd_softc *sc;
89114b4164SWarner Losh 
90114b4164SWarner Losh 	sc = device_get_softc(dev);
91114b4164SWarner Losh 	sc->dev = dev;
92114b4164SWarner Losh 	MMCSD_LOCK_INIT(sc);
93114b4164SWarner Losh 
94114b4164SWarner Losh 	sc->disk = disk_alloc();
95114b4164SWarner Losh 	sc->disk->d_open = mmcsd_open;
96114b4164SWarner Losh 	sc->disk->d_close = mmcsd_close;
97114b4164SWarner Losh 	sc->disk->d_strategy = mmcsd_strategy;
98114b4164SWarner Losh 	// sc->disk->d_dump = mmcsd_dump;	Need polling mmc layer
99114b4164SWarner Losh 	sc->disk->d_name = "mmcsd";
100114b4164SWarner Losh 	sc->disk->d_drv1 = sc;
101114b4164SWarner Losh 	sc->disk->d_maxsize = DFLTPHYS;
102114b4164SWarner Losh 	sc->disk->d_sectorsize = mmc_get_sector_size(dev);
103114b4164SWarner Losh 	sc->disk->d_mediasize = mmc_get_media_size(dev);
104114b4164SWarner Losh 	sc->disk->d_unit = device_get_unit(dev);
105114b4164SWarner Losh 	disk_create(sc->disk, DISK_VERSION);
106114b4164SWarner Losh 	bioq_init(&sc->bio_queue);
107114b4164SWarner Losh 	kthread_create(&mmcsd_task, sc, &sc->p, 0, 0, "task: mmc/sd card");
108114b4164SWarner Losh 
109114b4164SWarner Losh 	return (0);
110114b4164SWarner Losh }
111114b4164SWarner Losh 
112114b4164SWarner Losh static int
113114b4164SWarner Losh mmcsd_detach(device_t dev)
114114b4164SWarner Losh {
115114b4164SWarner Losh 	return (EBUSY);	/* XXX */
116114b4164SWarner Losh }
117114b4164SWarner Losh 
118114b4164SWarner Losh static int
119114b4164SWarner Losh mmcsd_open(struct disk *dp)
120114b4164SWarner Losh {
121114b4164SWarner Losh 	return 0;
122114b4164SWarner Losh }
123114b4164SWarner Losh 
124114b4164SWarner Losh static int
125114b4164SWarner Losh mmcsd_close(struct disk *dp)
126114b4164SWarner Losh {
127114b4164SWarner Losh 	return 0;
128114b4164SWarner Losh }
129114b4164SWarner Losh 
130114b4164SWarner Losh static void
131114b4164SWarner Losh mmcsd_strategy(struct bio *bp)
132114b4164SWarner Losh {
133114b4164SWarner Losh 	struct mmcsd_softc *sc;
134114b4164SWarner Losh 
135114b4164SWarner Losh 	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
136114b4164SWarner Losh 	MMCSD_LOCK(sc);
137114b4164SWarner Losh 	bioq_disksort(&sc->bio_queue, bp);
138114b4164SWarner Losh 	wakeup(sc);
139114b4164SWarner Losh 	MMCSD_UNLOCK(sc);
140114b4164SWarner Losh }
141114b4164SWarner Losh 
142114b4164SWarner Losh static void
143114b4164SWarner Losh mmcsd_task(void *arg)
144114b4164SWarner Losh {
145114b4164SWarner Losh 	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
146114b4164SWarner Losh 	struct bio *bp;
147114b4164SWarner Losh 	int sz;
148114b4164SWarner Losh 	daddr_t block, end;
149114b4164SWarner Losh 	struct mmc_command cmd;
150114b4164SWarner Losh 	struct mmc_command stop;
151114b4164SWarner Losh 	struct mmc_request req;
152114b4164SWarner Losh 	struct mmc_data data;
153114b4164SWarner Losh 	device_t dev;
154114b4164SWarner Losh 
155114b4164SWarner Losh 	dev = sc->dev;
156114b4164SWarner Losh 	for (;;) {
157114b4164SWarner Losh 		MMCSD_LOCK(sc);
158114b4164SWarner Losh 		do {
159114b4164SWarner Losh 			bp = bioq_first(&sc->bio_queue);
160114b4164SWarner Losh 			if (bp == NULL)
161114b4164SWarner Losh 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
162114b4164SWarner Losh 		} while (bp == NULL);
163114b4164SWarner Losh 		bioq_remove(&sc->bio_queue, bp);
164114b4164SWarner Losh 		MMCSD_UNLOCK(sc);
165114b4164SWarner Losh 		MMCBUS_ACQUIRE_BUS(device_get_parent(dev), dev);
166114b4164SWarner Losh //		printf("mmc_task: request %p for block %lld\n", bp, bp->bio_pblkno);
167114b4164SWarner Losh 		sz = sc->disk->d_sectorsize;
168114b4164SWarner Losh 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
169114b4164SWarner Losh 		// XXX should use read/write_mulit
170114b4164SWarner Losh 		for (block = bp->bio_pblkno; block < end;) {
171114b4164SWarner Losh 			char *vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
172114b4164SWarner Losh 			memset(&req, 0, sizeof(req));
173114b4164SWarner Losh 			memset(&cmd, 0, sizeof(cmd));
174114b4164SWarner Losh 			memset(&stop, 0, sizeof(stop));
175114b4164SWarner Losh 			req.cmd = &cmd;
176114b4164SWarner Losh 			cmd.data = &data;
177114b4164SWarner Losh 			if (bp->bio_cmd == BIO_READ) {
178114b4164SWarner Losh #ifdef MULTI_BLOCK_READ
179114b4164SWarner Losh 				if (end - block > 1)
180114b4164SWarner Losh 					cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
181114b4164SWarner Losh 				else
182114b4164SWarner Losh 					cmd.opcode = MMC_READ_SINGLE_BLOCK;
183114b4164SWarner Losh #else
184114b4164SWarner Losh 				cmd.opcode = MMC_READ_SINGLE_BLOCK;
185114b4164SWarner Losh #endif
186114b4164SWarner Losh 			} else
187114b4164SWarner Losh 				cmd.opcode = MMC_WRITE_BLOCK;
188114b4164SWarner Losh 			cmd.arg = block << 9;
189114b4164SWarner Losh 			cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
190114b4164SWarner Losh 			// data.timeout_ns = ;
191114b4164SWarner Losh 			// data.timeout_clks = ;
192114b4164SWarner Losh 			data.data = vaddr;
193114b4164SWarner Losh 			data.mrq = &req;
194114b4164SWarner Losh 			if (bp->bio_cmd == BIO_READ) {
195114b4164SWarner Losh 				data.flags = MMC_DATA_READ;
196114b4164SWarner Losh #ifdef MULTI_BLOCK_READ
197114b4164SWarner Losh 				data.len = bp->bio_bcount;
198114b4164SWarner Losh 				if (end - block > 1) {
199114b4164SWarner Losh 					req.stop = &stop;
200114b4164SWarner Losh 					data.flags |= MMC_DATA_MULTI;
201114b4164SWarner Losh 				}
202114b4164SWarner Losh 				printf("Len %d  %lld-%lld flags %#x sz %d\n",
203114b4164SWarner Losh 				    data.len, block, end, data.flags, sz);
204114b4164SWarner Losh 				block = end;
205114b4164SWarner Losh #else
206114b4164SWarner Losh 				data.len = sz;
207114b4164SWarner Losh 				block++;
208114b4164SWarner Losh #endif
209114b4164SWarner Losh 			} else {
210114b4164SWarner Losh 				data.flags = MMC_DATA_WRITE;
211114b4164SWarner Losh 				data.len = sz;
212114b4164SWarner Losh 				block++;
213114b4164SWarner Losh 			}
214114b4164SWarner Losh 			stop.opcode = MMC_STOP_TRANSMISSION;
215114b4164SWarner Losh 			stop.arg = 0;
216114b4164SWarner Losh 			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
217114b4164SWarner Losh 			MMCBUS_WAIT_FOR_REQUEST(device_get_parent(dev), dev,
218114b4164SWarner Losh 			    &req);
219114b4164SWarner Losh 			// XXX error handling
220114b4164SWarner Losh //XXX			while (!(mmc_send_status(dev) & R1_READY_FOR_DATA))
221114b4164SWarner Losh //				continue;
222114b4164SWarner Losh 			// XXX decode mmc status
223114b4164SWarner Losh 		}
224114b4164SWarner Losh 		MMCBUS_RELEASE_BUS(device_get_parent(dev), dev);
225114b4164SWarner Losh 		biodone(bp);
226114b4164SWarner Losh 	}
227114b4164SWarner Losh }
228114b4164SWarner Losh 
229114b4164SWarner Losh static device_method_t mmcsd_methods[] = {
230114b4164SWarner Losh 	DEVMETHOD(device_probe, mmcsd_probe),
231114b4164SWarner Losh 	DEVMETHOD(device_attach, mmcsd_attach),
232114b4164SWarner Losh 	DEVMETHOD(device_detach, mmcsd_detach),
233114b4164SWarner Losh 	{0, 0},
234114b4164SWarner Losh };
235114b4164SWarner Losh 
236114b4164SWarner Losh static driver_t mmcsd_driver = {
237114b4164SWarner Losh 	"mmcsd",
238114b4164SWarner Losh 	mmcsd_methods,
239114b4164SWarner Losh 	sizeof(struct mmcsd_softc),
240114b4164SWarner Losh };
241114b4164SWarner Losh static devclass_t mmcsd_devclass;
242114b4164SWarner Losh 
243114b4164SWarner Losh 
244114b4164SWarner Losh DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, 0, 0);
245