xref: /netbsd-src/sys/dev/sdmmc/ld_sdmmc.c (revision f21b7d7f2cbdd5c14b3882c4e8a3d43580d460a6)
1 /*	$NetBSD: ld_sdmmc.c,v 1.23 2016/09/27 03:33:33 pgoyette 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.23 2016/09/27 03:33:33 pgoyette Exp $");
32 
33 #ifdef _KERNEL_OPT
34 #include "opt_sdmmc.h"
35 #endif
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/buf.h>
42 #include <sys/bufq.h>
43 #include <sys/bus.h>
44 #include <sys/endian.h>
45 #include <sys/dkio.h>
46 #include <sys/disk.h>
47 #include <sys/kthread.h>
48 #include <sys/module.h>
49 
50 #include <dev/ldvar.h>
51 
52 #include <dev/sdmmc/sdmmcvar.h>
53 
54 #include "ioconf.h"
55 
56 #ifdef LD_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 };
70 
71 struct ld_sdmmc_softc {
72 	struct ld_softc sc_ld;
73 	int sc_hwunit;
74 
75 	struct sdmmc_function *sc_sf;
76 #define LD_SDMMC_MAXQUEUECNT 4
77 	struct ld_sdmmc_task sc_task[LD_SDMMC_MAXQUEUECNT];
78 	int sc_nexttask;
79 };
80 
81 static int ld_sdmmc_match(device_t, cfdata_t, void *);
82 static void ld_sdmmc_attach(device_t, device_t, void *);
83 static int ld_sdmmc_detach(device_t, int);
84 
85 static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
86 static int ld_sdmmc_start(struct ld_softc *, struct buf *);
87 
88 static void ld_sdmmc_doattach(void *);
89 static void ld_sdmmc_dobio(void *);
90 
91 CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
92     ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
93 
94 
95 /* ARGSUSED */
96 static int
97 ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
98 {
99 	struct sdmmc_softc *sdmsc = device_private(parent);
100 
101 	if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
102 		return 1;
103 	return 0;
104 }
105 
106 /* ARGSUSED */
107 static void
108 ld_sdmmc_attach(device_t parent, device_t self, void *aux)
109 {
110 	struct ld_sdmmc_softc *sc = device_private(self);
111 	struct sdmmc_attach_args *sa = aux;
112 	struct ld_softc *ld = &sc->sc_ld;
113 	struct lwp *lwp;
114 
115 	ld->sc_dv = self;
116 
117 	aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
118 	    sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
119 	    sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
120 	aprint_naive("\n");
121 
122 	sc->sc_nexttask = 0;
123 
124 	sc->sc_hwunit = 0;	/* always 0? */
125 	sc->sc_sf = sa->sf;
126 
127 	ld->sc_flags = LDF_ENABLED;
128 	ld->sc_secperunit = sc->sc_sf->csd.capacity;
129 	ld->sc_secsize = SDMMC_SECTOR_SIZE;
130 	ld->sc_maxxfer = MAXPHYS;
131 	ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
132 	ld->sc_dump = ld_sdmmc_dump;
133 	ld->sc_start = ld_sdmmc_start;
134 
135 	/*
136 	 * It is avoided that the error occurs when the card attaches it,
137 	 * when wedge is supported.
138 	 */
139 	config_pending_incr(self);
140 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
141 	    ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
142 		aprint_error_dev(self, "couldn't create thread\n");
143 	}
144 }
145 
146 static void
147 ld_sdmmc_doattach(void *arg)
148 {
149 	struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
150 	struct ld_softc *ld = &sc->sc_ld;
151 	struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
152 
153 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
154 	aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
155 	if (ssc->sc_transfer_mode != NULL)
156 		aprint_normal(" %s,", ssc->sc_transfer_mode);
157 	if ((ssc->sc_busclk / 1000) != 0)
158 		aprint_normal(" %u.%03u MHz\n",
159 		    ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
160 	else
161 		aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
162 	config_pending_decr(ld->sc_dv);
163 	kthread_exit(0);
164 }
165 
166 static int
167 ld_sdmmc_detach(device_t dev, int flags)
168 {
169 	struct ld_sdmmc_softc *sc = device_private(dev);
170 	struct ld_softc *ld = &sc->sc_ld;
171 	int rv;
172 
173 	if ((rv = ldbegindetach(ld, flags)) != 0)
174 		return rv;
175 	ldenddetach(ld);
176 
177 	return 0;
178 }
179 
180 static int
181 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
182 {
183 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
184 	struct ld_sdmmc_task *task = &sc->sc_task[sc->sc_nexttask];
185 
186 	sc->sc_nexttask = (sc->sc_nexttask + 1) % LD_SDMMC_MAXQUEUECNT;
187 
188 	task->task_sc = sc;
189 	task->task_bp = bp;
190 	sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
191 
192 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
193 
194 	return 0;
195 }
196 
197 static void
198 ld_sdmmc_dobio(void *arg)
199 {
200 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
201 	struct ld_sdmmc_softc *sc = task->task_sc;
202 	struct buf *bp = task->task_bp;
203 	int error;
204 
205 	/*
206 	 * I/O operation
207 	 */
208 	DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
209 	    device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
210 	    bp->b_rawblkno, bp->b_bcount));
211 
212 	/* is everything done in terms of blocks? */
213 	if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
214 		/* trying to read or write past end of device */
215 		aprint_error_dev(sc->sc_ld.sc_dv,
216 		    "blkno 0x%" PRIu64 " exceeds capacity %d\n",
217 		    bp->b_rawblkno, sc->sc_sf->csd.capacity);
218 		bp->b_error = EINVAL;
219 		bp->b_resid = bp->b_bcount;
220 		lddone(&sc->sc_ld, bp);
221 		return;
222 	}
223 
224 	if (bp->b_flags & B_READ)
225 		error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
226 		    bp->b_data, bp->b_bcount);
227 	else
228 		error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
229 		    bp->b_data, bp->b_bcount);
230 	if (error) {
231 		DPRINTF(("%s: error %d\n", device_xname(sc->sc_ld.sc_dv),
232 		    error));
233 		bp->b_error = error;
234 		bp->b_resid = bp->b_bcount;
235 	} else {
236 		bp->b_resid = 0;
237 	}
238 
239 	lddone(&sc->sc_ld, bp);
240 }
241 
242 static int
243 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
244 {
245 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
246 
247 	return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
248 	    blkcnt * ld->sc_secsize);
249 }
250 
251 MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
252 
253 #ifdef _MODULE
254 /*
255  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
256  * XXX it will be defined in the common-code module
257  */
258 #undef  CFDRIVER_DECL
259 #define CFDRIVER_DECL(name, class, attr)
260 #include "ioconf.c"
261 #endif
262 
263 static int
264 ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
265 {
266 #ifdef _MODULE
267 	/*
268 	 * We ignore the cfdriver_vec[] that ioconf provides, since
269 	 * the cfdrivers are attached already.
270 	 */
271 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
272 #endif
273 	int error = 0;
274 
275 #ifdef _MODULE
276 	switch (cmd) {
277 	case MODULE_CMD_INIT:
278 		error = config_init_component(no_cfdriver_vec,
279 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
280         	break;
281 	case MODULE_CMD_FINI:
282 		error = config_fini_component(no_cfdriver_vec,
283 		    cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
284 		break;
285 	default:
286 		error = ENOTTY;
287 		break;
288 	}
289 #endif
290 
291 	return error;
292 }
293