xref: /netbsd-src/sys/dev/sdmmc/ld_sdmmc.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: ld_sdmmc.c,v 1.21 2015/08/28 06:04:43 mlelstv 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.21 2015/08/28 06:04:43 mlelstv 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 
49 #include <dev/ldvar.h>
50 
51 #include <dev/sdmmc/sdmmcvar.h>
52 
53 #ifdef LD_SDMMC_DEBUG
54 #define DPRINTF(s)	printf s
55 #else
56 #define DPRINTF(s)	/**/
57 #endif
58 
59 struct ld_sdmmc_softc;
60 
61 struct ld_sdmmc_task {
62 	struct sdmmc_task task;
63 
64 	struct ld_sdmmc_softc *task_sc;
65 	struct buf *task_bp;
66 };
67 
68 struct ld_sdmmc_softc {
69 	struct ld_softc sc_ld;
70 	int sc_hwunit;
71 
72 	struct sdmmc_function *sc_sf;
73 #define LD_SDMMC_MAXQUEUECNT 4
74 	struct ld_sdmmc_task sc_task[LD_SDMMC_MAXQUEUECNT];
75 	int sc_nexttask;
76 };
77 
78 static int ld_sdmmc_match(device_t, cfdata_t, void *);
79 static void ld_sdmmc_attach(device_t, device_t, void *);
80 static int ld_sdmmc_detach(device_t, int);
81 
82 static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
83 static int ld_sdmmc_start(struct ld_softc *, struct buf *);
84 
85 static void ld_sdmmc_doattach(void *);
86 static void ld_sdmmc_dobio(void *);
87 
88 CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
89     ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
90 
91 
92 /* ARGSUSED */
93 static int
94 ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
95 {
96 	struct sdmmc_softc *sdmsc = device_private(parent);
97 
98 	if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
99 		return 1;
100 	return 0;
101 }
102 
103 /* ARGSUSED */
104 static void
105 ld_sdmmc_attach(device_t parent, device_t self, void *aux)
106 {
107 	struct ld_sdmmc_softc *sc = device_private(self);
108 	struct sdmmc_attach_args *sa = aux;
109 	struct ld_softc *ld = &sc->sc_ld;
110 	struct lwp *lwp;
111 
112 	ld->sc_dv = self;
113 
114 	aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
115 	    sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
116 	    sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
117 	aprint_naive("\n");
118 
119 	sc->sc_nexttask = 0;
120 
121 	sc->sc_hwunit = 0;	/* always 0? */
122 	sc->sc_sf = sa->sf;
123 
124 	ld->sc_flags = LDF_ENABLED;
125 	ld->sc_secperunit = sc->sc_sf->csd.capacity;
126 	ld->sc_secsize = SDMMC_SECTOR_SIZE;
127 	ld->sc_maxxfer = MAXPHYS;
128 	ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
129 	ld->sc_dump = ld_sdmmc_dump;
130 	ld->sc_start = ld_sdmmc_start;
131 
132 	/*
133 	 * It is avoided that the error occurs when the card attaches it,
134 	 * when wedge is supported.
135 	 */
136 	config_pending_incr(self);
137 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
138 	    ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
139 		aprint_error_dev(self, "couldn't create thread\n");
140 	}
141 }
142 
143 static void
144 ld_sdmmc_doattach(void *arg)
145 {
146 	struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
147 	struct ld_softc *ld = &sc->sc_ld;
148 	struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
149 
150 	ldattach(ld);
151 	aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
152 	if (ssc->sc_transfer_mode != NULL)
153 		aprint_normal(" %s,", ssc->sc_transfer_mode);
154 	if ((ssc->sc_busclk / 1000) != 0)
155 		aprint_normal(" %u.%03u MHz\n",
156 		    ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
157 	else
158 		aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
159 	config_pending_decr(ld->sc_dv);
160 	kthread_exit(0);
161 }
162 
163 static int
164 ld_sdmmc_detach(device_t dev, int flags)
165 {
166 	struct ld_sdmmc_softc *sc = device_private(dev);
167 	struct ld_softc *ld = &sc->sc_ld;
168 	int rv;
169 
170 	if ((rv = ldbegindetach(ld, flags)) != 0)
171 		return rv;
172 	ldenddetach(ld);
173 
174 	return 0;
175 }
176 
177 static int
178 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
179 {
180 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
181 	struct ld_sdmmc_task *task = &sc->sc_task[sc->sc_nexttask];
182 
183 	sc->sc_nexttask = (sc->sc_nexttask + 1) % LD_SDMMC_MAXQUEUECNT;
184 
185 	task->task_sc = sc;
186 	task->task_bp = bp;
187 	sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
188 
189 	sdmmc_add_task(sc->sc_sf->sc, &task->task);
190 
191 	return 0;
192 }
193 
194 static void
195 ld_sdmmc_dobio(void *arg)
196 {
197 	struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
198 	struct ld_sdmmc_softc *sc = task->task_sc;
199 	struct buf *bp = task->task_bp;
200 	int error;
201 
202 	/*
203 	 * I/O operation
204 	 */
205 	DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
206 	    device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
207 	    bp->b_rawblkno, bp->b_bcount));
208 
209 	/* is everything done in terms of blocks? */
210 	if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
211 		/* trying to read or write past end of device */
212 		aprint_error_dev(sc->sc_ld.sc_dv,
213 		    "blkno 0x%" PRIu64 " exceeds capacity %d\n",
214 		    bp->b_rawblkno, sc->sc_sf->csd.capacity);
215 		bp->b_error = EINVAL;
216 		bp->b_resid = bp->b_bcount;
217 		lddone(&sc->sc_ld, bp);
218 		return;
219 	}
220 
221 	if (bp->b_flags & B_READ)
222 		error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
223 		    bp->b_data, bp->b_bcount);
224 	else
225 		error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
226 		    bp->b_data, bp->b_bcount);
227 	if (error) {
228 		DPRINTF(("%s: error %d\n", device_xname(sc->sc_ld.sc_dv),
229 		    error));
230 		bp->b_error = error;
231 		bp->b_resid = bp->b_bcount;
232 	} else {
233 		bp->b_resid = 0;
234 	}
235 
236 	lddone(&sc->sc_ld, bp);
237 }
238 
239 static int
240 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
241 {
242 	struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
243 
244 	return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
245 	    blkcnt * ld->sc_secsize);
246 }
247