xref: /netbsd-src/sys/dev/sdmmc/ld_sdmmc.c (revision d877c4c3c02304002c0642d7f34a58d07138d6a9)
1 /*	$NetBSD: ld_sdmmc.c,v 1.1 2009/04/21 03:00:30 nonaka Exp $	*/
2 
3 /*
4  * Copyright (c) 2008 KIYOHARA Takashi
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: ld_sdmmc.c,v 1.1 2009/04/21 03:00:30 nonaka Exp $");
32 
33 #include "rnd.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/buf.h>
40 #include <sys/bufq.h>
41 #include <sys/bus.h>
42 #include <sys/callout.h>
43 #include <sys/endian.h>
44 #include <sys/dkio.h>
45 #include <sys/disk.h>
46 #if NRND > 0
47 #include <sys/rnd.h>
48 #endif
49 
50 #include <uvm/uvm_extern.h>
51 
52 #include <dev/ldvar.h>
53 
54 #include <dev/sdmmc/sdmmcvar.h>
55 
56 #ifdef SDMMC_DEBUG
57 #define DPRINTF(s)	printf s
58 #else
59 #define DPRINTF(s)	/**/
60 #endif
61 
62 struct ld_sdmmc_softc;
63 
64 struct ld_sdmmc_task {
65 	struct sdmmc_task task;
66 
67 	struct ld_sdmmc_softc *task_sc;
68 	struct buf *task_bp;
69 	callout_t task_callout;
70 };
71 
72 struct ld_sdmmc_softc {
73 	struct ld_softc sc_ld;
74 	int sc_hwunit;
75 
76 	struct sdmmc_function *sc_sf;
77 	struct ld_sdmmc_task sc_task;
78 };
79 
80 static int ld_sdmmc_match(device_t, struct cfdata *, void *);
81 static void ld_sdmmc_attach(device_t, device_t, void *);
82 static int ld_sdmmc_detach(device_t, int);
83 
84 static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
85 static int ld_sdmmc_start(struct ld_softc *, struct buf *);
86 
87 static void ld_sdmmc_dobio(void *);
88 static void ld_sdmmc_timeout(void *);
89 
90 CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
91     ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
92 
93 
94 /* ARGSUSED */
95 static int
96 ld_sdmmc_match(device_t parent, struct cfdata *match, void *aux)
97 {
98 	struct sdmmc_softc *sdmsc = device_private(parent);
99 
100 	if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
101 		return 1;
102 	return 0;
103 }
104 
105 /* ARGSUSED */
106 static void
107 ld_sdmmc_attach(device_t parent, device_t self, void *aux)
108 {
109 	struct ld_sdmmc_softc *sc = device_private(self);
110 	struct sdmmc_attach_args *sa = aux;
111 	struct ld_softc *ld = &sc->sc_ld;
112 
113 	ld->sc_dv = self;
114 
115 	aprint_normal("\n");
116 	aprint_naive("\n");
117 
118 	callout_init(&sc->sc_task.task_callout, CALLOUT_MPSAFE);
119 
120 	sc->sc_hwunit = 0;	/* always 0? */
121 	sc->sc_sf = sa->sf;
122 
123 	ld->sc_flags = LDF_ENABLED;
124 	ld->sc_secperunit = sc->sc_sf->csd.capacity;
125 	ld->sc_secsize = sc->sc_sf->csd.sector_size;
126 	ld->sc_maxxfer = MAXPHYS;
127 	ld->sc_maxqueuecnt = 1;
128 	ld->sc_dump = ld_sdmmc_dump;
129 	ld->sc_start = ld_sdmmc_start;
130 
131 	ldattach(ld);
132 }
133 
134 static int
135 ld_sdmmc_detach(device_t dev, int flags)
136 {
137 	struct ld_sdmmc_softc *sc = device_private(dev);
138 	struct ld_softc *ld = &sc->sc_ld;
139 	int rv;
140 
141 	if ((rv = ldbegindetach(ld, flags)) != 0)
142 		return rv;
143 	ldenddetach(ld);
144 
145 	return 0;
146 }
147 
148 static int
149 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
150 {
151 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
152 	struct ld_sdmmc_task *task = &sc->sc_task;
153 
154 	task->task_sc = sc;
155 	task->task_bp = bp;
156 	sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
157 
158 	callout_reset(&task->task_callout, hz, ld_sdmmc_timeout, task);
159 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
160 
161 	return 0;
162 }
163 
164 static void
165 ld_sdmmc_dobio(void *arg)
166 {
167 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
168 	struct ld_sdmmc_softc *sc = task->task_sc;
169 	struct buf *bp = task->task_bp;
170 	int error, s;
171 
172 	callout_stop(&task->task_callout);
173 
174 	/*
175 	 * I/O operation
176 	 */
177 	DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
178 	    device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
179 	    bp->b_rawblkno, bp->b_bcount));
180 
181 	/* is everything done in terms of blocks? */
182 	if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
183 		/* trying to read or write past end of device */
184 		DPRINTF(("%s: blkno exceeds capacity 0x%x\n",
185 		    device_xname(sc->sc_ld.sc_dv), sc->sc_sf->csd.capacity));
186 		bp->b_error = EIO; /* XXX  */
187 		bp->b_resid = bp->b_bcount;
188 		lddone(&sc->sc_ld, bp);
189 		return;
190 	}
191 
192 	s = splbio();
193 	if (bp->b_flags & B_READ)
194 		error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
195 		    bp->b_data, bp->b_bcount);
196 	else
197 		error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
198 		    bp->b_data, bp->b_bcount);
199 	if (error) {
200 		DPRINTF(("%s: error %d\n", device_xname(sc->sc_ld.sc_dv),
201 		    error));
202 		bp->b_error = EIO;	/* XXXX */
203 		bp->b_resid = bp->b_bcount;
204 	} else {
205 		bp->b_resid = 0;
206 	}
207 	splx(s);
208 
209 	lddone(&sc->sc_ld, bp);
210 }
211 
212 static void
213 ld_sdmmc_timeout(void *arg)
214 {
215 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
216 	struct ld_sdmmc_softc *sc = task->task_sc;
217 	struct buf *bp = task->task_bp;
218 	int s;
219 
220 	s = splbio();
221 	if (!sdmmc_task_pending(&task->task)) {
222 		splx(s);
223 		return;
224 	}
225 	bp->b_error = EIO;	/* XXXX */
226 	bp->b_resid = bp->b_bcount;
227 	sdmmc_del_task(&task->task);
228 	splx(s);
229 
230 	lddone(&sc->sc_ld, bp);
231 }
232 
233 static int
234 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
235 {
236 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
237 
238 	return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
239 	    blkcnt * ld->sc_secsize);
240 }
241