ld_sdmmc.c revision 1.24 1 1.24 kiyohara /* $NetBSD: ld_sdmmc.c,v 1.24 2017/01/07 14:49:53 kiyohara Exp $ */
2 1.1 nonaka
3 1.1 nonaka /*
4 1.1 nonaka * Copyright (c) 2008 KIYOHARA Takashi
5 1.1 nonaka * All rights reserved.
6 1.1 nonaka *
7 1.1 nonaka * Redistribution and use in source and binary forms, with or without
8 1.1 nonaka * modification, are permitted provided that the following conditions
9 1.1 nonaka * are met:
10 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
11 1.1 nonaka * notice, this list of conditions and the following disclaimer.
12 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
14 1.1 nonaka * documentation and/or other materials provided with the distribution.
15 1.1 nonaka *
16 1.1 nonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 nonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.1 nonaka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 1.1 nonaka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 1.1 nonaka * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1 nonaka * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1 nonaka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 nonaka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 1.1 nonaka * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 1.1 nonaka * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 nonaka * POSSIBILITY OF SUCH DAMAGE.
27 1.1 nonaka *
28 1.1 nonaka */
29 1.1 nonaka
30 1.1 nonaka #include <sys/cdefs.h>
31 1.24 kiyohara __KERNEL_RCSID(0, "$NetBSD: ld_sdmmc.c,v 1.24 2017/01/07 14:49:53 kiyohara Exp $");
32 1.1 nonaka
33 1.9 matt #ifdef _KERNEL_OPT
34 1.9 matt #include "opt_sdmmc.h"
35 1.9 matt #endif
36 1.1 nonaka
37 1.1 nonaka #include <sys/param.h>
38 1.1 nonaka #include <sys/systm.h>
39 1.1 nonaka #include <sys/kernel.h>
40 1.1 nonaka #include <sys/device.h>
41 1.1 nonaka #include <sys/buf.h>
42 1.1 nonaka #include <sys/bufq.h>
43 1.1 nonaka #include <sys/bus.h>
44 1.1 nonaka #include <sys/endian.h>
45 1.1 nonaka #include <sys/dkio.h>
46 1.1 nonaka #include <sys/disk.h>
47 1.3 nonaka #include <sys/kthread.h>
48 1.23 pgoyette #include <sys/module.h>
49 1.1 nonaka
50 1.1 nonaka #include <dev/ldvar.h>
51 1.1 nonaka
52 1.1 nonaka #include <dev/sdmmc/sdmmcvar.h>
53 1.1 nonaka
54 1.23 pgoyette #include "ioconf.h"
55 1.23 pgoyette
56 1.14 jmcneill #ifdef LD_SDMMC_DEBUG
57 1.1 nonaka #define DPRINTF(s) printf s
58 1.1 nonaka #else
59 1.1 nonaka #define DPRINTF(s) /**/
60 1.1 nonaka #endif
61 1.1 nonaka
62 1.24 kiyohara #define LD_SDMMC_IORETRIES 5 /* number of retries before giving up */
63 1.24 kiyohara #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
64 1.24 kiyohara
65 1.1 nonaka struct ld_sdmmc_softc;
66 1.1 nonaka
67 1.1 nonaka struct ld_sdmmc_task {
68 1.1 nonaka struct sdmmc_task task;
69 1.1 nonaka
70 1.1 nonaka struct ld_sdmmc_softc *task_sc;
71 1.1 nonaka struct buf *task_bp;
72 1.24 kiyohara int task_retries; /* number of xfer retry */
73 1.24 kiyohara struct callout task_restart_ch;
74 1.1 nonaka };
75 1.1 nonaka
76 1.1 nonaka struct ld_sdmmc_softc {
77 1.1 nonaka struct ld_softc sc_ld;
78 1.1 nonaka int sc_hwunit;
79 1.1 nonaka
80 1.1 nonaka struct sdmmc_function *sc_sf;
81 1.20 mlelstv #define LD_SDMMC_MAXQUEUECNT 4
82 1.20 mlelstv struct ld_sdmmc_task sc_task[LD_SDMMC_MAXQUEUECNT];
83 1.24 kiyohara TAILQ_HEAD(, sdmmc_task) sc_freeq;
84 1.1 nonaka };
85 1.1 nonaka
86 1.2 cegger static int ld_sdmmc_match(device_t, cfdata_t, void *);
87 1.1 nonaka static void ld_sdmmc_attach(device_t, device_t, void *);
88 1.1 nonaka static int ld_sdmmc_detach(device_t, int);
89 1.1 nonaka
90 1.1 nonaka static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
91 1.1 nonaka static int ld_sdmmc_start(struct ld_softc *, struct buf *);
92 1.24 kiyohara static void ld_sdmmc_restart(void *);
93 1.1 nonaka
94 1.3 nonaka static void ld_sdmmc_doattach(void *);
95 1.1 nonaka static void ld_sdmmc_dobio(void *);
96 1.1 nonaka
97 1.1 nonaka CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
98 1.1 nonaka ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
99 1.1 nonaka
100 1.1 nonaka
101 1.1 nonaka /* ARGSUSED */
102 1.1 nonaka static int
103 1.2 cegger ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
104 1.1 nonaka {
105 1.1 nonaka struct sdmmc_softc *sdmsc = device_private(parent);
106 1.1 nonaka
107 1.1 nonaka if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
108 1.1 nonaka return 1;
109 1.1 nonaka return 0;
110 1.1 nonaka }
111 1.1 nonaka
112 1.1 nonaka /* ARGSUSED */
113 1.1 nonaka static void
114 1.1 nonaka ld_sdmmc_attach(device_t parent, device_t self, void *aux)
115 1.1 nonaka {
116 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(self);
117 1.1 nonaka struct sdmmc_attach_args *sa = aux;
118 1.1 nonaka struct ld_softc *ld = &sc->sc_ld;
119 1.24 kiyohara struct ld_sdmmc_task *task;
120 1.3 nonaka struct lwp *lwp;
121 1.24 kiyohara int i;
122 1.1 nonaka
123 1.1 nonaka ld->sc_dv = self;
124 1.1 nonaka
125 1.11 jakllsch aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
126 1.11 jakllsch sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
127 1.11 jakllsch sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
128 1.1 nonaka aprint_naive("\n");
129 1.1 nonaka
130 1.24 kiyohara TAILQ_INIT(&sc->sc_freeq);
131 1.24 kiyohara for (i = 0; i < __arraycount(sc->sc_task); i++) {
132 1.24 kiyohara task = &sc->sc_task[i];
133 1.24 kiyohara task->task_sc = sc;
134 1.24 kiyohara callout_init(&task->task_restart_ch, 0);
135 1.24 kiyohara TAILQ_INSERT_TAIL(&sc->sc_freeq, &task->task, next);
136 1.24 kiyohara }
137 1.20 mlelstv
138 1.1 nonaka sc->sc_hwunit = 0; /* always 0? */
139 1.1 nonaka sc->sc_sf = sa->sf;
140 1.1 nonaka
141 1.1 nonaka ld->sc_flags = LDF_ENABLED;
142 1.1 nonaka ld->sc_secperunit = sc->sc_sf->csd.capacity;
143 1.4 nonaka ld->sc_secsize = SDMMC_SECTOR_SIZE;
144 1.1 nonaka ld->sc_maxxfer = MAXPHYS;
145 1.20 mlelstv ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
146 1.1 nonaka ld->sc_dump = ld_sdmmc_dump;
147 1.1 nonaka ld->sc_start = ld_sdmmc_start;
148 1.1 nonaka
149 1.3 nonaka /*
150 1.11 jakllsch * It is avoided that the error occurs when the card attaches it,
151 1.3 nonaka * when wedge is supported.
152 1.3 nonaka */
153 1.12 christos config_pending_incr(self);
154 1.3 nonaka if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
155 1.3 nonaka ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
156 1.3 nonaka aprint_error_dev(self, "couldn't create thread\n");
157 1.3 nonaka }
158 1.3 nonaka }
159 1.3 nonaka
160 1.3 nonaka static void
161 1.3 nonaka ld_sdmmc_doattach(void *arg)
162 1.3 nonaka {
163 1.3 nonaka struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
164 1.3 nonaka struct ld_softc *ld = &sc->sc_ld;
165 1.6 kiyohara struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
166 1.3 nonaka
167 1.22 jdolecek ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
168 1.19 jmcneill aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
169 1.19 jmcneill if (ssc->sc_transfer_mode != NULL)
170 1.19 jmcneill aprint_normal(" %s,", ssc->sc_transfer_mode);
171 1.6 kiyohara if ((ssc->sc_busclk / 1000) != 0)
172 1.6 kiyohara aprint_normal(" %u.%03u MHz\n",
173 1.6 kiyohara ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
174 1.6 kiyohara else
175 1.6 kiyohara aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
176 1.12 christos config_pending_decr(ld->sc_dv);
177 1.3 nonaka kthread_exit(0);
178 1.1 nonaka }
179 1.1 nonaka
180 1.1 nonaka static int
181 1.1 nonaka ld_sdmmc_detach(device_t dev, int flags)
182 1.1 nonaka {
183 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(dev);
184 1.1 nonaka struct ld_softc *ld = &sc->sc_ld;
185 1.24 kiyohara int rv, i;
186 1.1 nonaka
187 1.1 nonaka if ((rv = ldbegindetach(ld, flags)) != 0)
188 1.1 nonaka return rv;
189 1.1 nonaka ldenddetach(ld);
190 1.1 nonaka
191 1.24 kiyohara for (i = 0; i < __arraycount(sc->sc_task); i++)
192 1.24 kiyohara callout_destroy(&sc->sc_task[i].task_restart_ch);
193 1.24 kiyohara
194 1.1 nonaka return 0;
195 1.1 nonaka }
196 1.1 nonaka
197 1.1 nonaka static int
198 1.1 nonaka ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
199 1.1 nonaka {
200 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
201 1.24 kiyohara struct ld_sdmmc_task *task = (void *)TAILQ_FIRST(&sc->sc_freeq);
202 1.20 mlelstv
203 1.24 kiyohara TAILQ_REMOVE(&sc->sc_freeq, &task->task, next);
204 1.1 nonaka
205 1.1 nonaka task->task_bp = bp;
206 1.24 kiyohara task->task_retries = 0;
207 1.1 nonaka sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
208 1.1 nonaka
209 1.1 nonaka sdmmc_add_task(sc->sc_sf->sc, &task->task);
210 1.1 nonaka
211 1.1 nonaka return 0;
212 1.1 nonaka }
213 1.1 nonaka
214 1.1 nonaka static void
215 1.24 kiyohara ld_sdmmc_restart(void *arg)
216 1.24 kiyohara {
217 1.24 kiyohara struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
218 1.24 kiyohara struct ld_sdmmc_softc *sc = task->task_sc;
219 1.24 kiyohara struct buf *bp = task->task_bp;
220 1.24 kiyohara
221 1.24 kiyohara bp->b_resid = bp->b_bcount;
222 1.24 kiyohara
223 1.24 kiyohara sdmmc_add_task(sc->sc_sf->sc, &task->task);
224 1.24 kiyohara }
225 1.24 kiyohara
226 1.24 kiyohara static void
227 1.1 nonaka ld_sdmmc_dobio(void *arg)
228 1.1 nonaka {
229 1.1 nonaka struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
230 1.1 nonaka struct ld_sdmmc_softc *sc = task->task_sc;
231 1.1 nonaka struct buf *bp = task->task_bp;
232 1.18 mlelstv int error;
233 1.1 nonaka
234 1.1 nonaka /*
235 1.1 nonaka * I/O operation
236 1.1 nonaka */
237 1.1 nonaka DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
238 1.1 nonaka device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
239 1.1 nonaka bp->b_rawblkno, bp->b_bcount));
240 1.1 nonaka
241 1.1 nonaka /* is everything done in terms of blocks? */
242 1.1 nonaka if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
243 1.1 nonaka /* trying to read or write past end of device */
244 1.13 mlelstv aprint_error_dev(sc->sc_ld.sc_dv,
245 1.13 mlelstv "blkno 0x%" PRIu64 " exceeds capacity %d\n",
246 1.13 mlelstv bp->b_rawblkno, sc->sc_sf->csd.capacity);
247 1.13 mlelstv bp->b_error = EINVAL;
248 1.1 nonaka bp->b_resid = bp->b_bcount;
249 1.1 nonaka lddone(&sc->sc_ld, bp);
250 1.1 nonaka return;
251 1.1 nonaka }
252 1.1 nonaka
253 1.1 nonaka if (bp->b_flags & B_READ)
254 1.1 nonaka error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
255 1.1 nonaka bp->b_data, bp->b_bcount);
256 1.1 nonaka else
257 1.1 nonaka error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
258 1.1 nonaka bp->b_data, bp->b_bcount);
259 1.1 nonaka if (error) {
260 1.24 kiyohara if (task->task_retries < LD_SDMMC_IORETRIES) {
261 1.24 kiyohara struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
262 1.24 kiyohara struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
263 1.24 kiyohara
264 1.24 kiyohara diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
265 1.24 kiyohara dksc->sc_dkdev.dk_label);
266 1.24 kiyohara printf(", retrying\n");
267 1.24 kiyohara task->task_retries++;
268 1.24 kiyohara callout_reset(&task->task_restart_ch, RECOVERYTIME,
269 1.24 kiyohara ld_sdmmc_restart, task);
270 1.24 kiyohara return;
271 1.24 kiyohara }
272 1.13 mlelstv bp->b_error = error;
273 1.1 nonaka bp->b_resid = bp->b_bcount;
274 1.1 nonaka } else {
275 1.1 nonaka bp->b_resid = 0;
276 1.1 nonaka }
277 1.1 nonaka
278 1.24 kiyohara TAILQ_INSERT_TAIL(&sc->sc_freeq, &task->task, next);
279 1.24 kiyohara
280 1.1 nonaka lddone(&sc->sc_ld, bp);
281 1.1 nonaka }
282 1.1 nonaka
283 1.1 nonaka static int
284 1.1 nonaka ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
285 1.1 nonaka {
286 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
287 1.1 nonaka
288 1.1 nonaka return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
289 1.1 nonaka blkcnt * ld->sc_secsize);
290 1.1 nonaka }
291 1.23 pgoyette
292 1.23 pgoyette MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
293 1.23 pgoyette
294 1.23 pgoyette #ifdef _MODULE
295 1.23 pgoyette /*
296 1.23 pgoyette * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
297 1.23 pgoyette * XXX it will be defined in the common-code module
298 1.23 pgoyette */
299 1.23 pgoyette #undef CFDRIVER_DECL
300 1.23 pgoyette #define CFDRIVER_DECL(name, class, attr)
301 1.23 pgoyette #include "ioconf.c"
302 1.23 pgoyette #endif
303 1.23 pgoyette
304 1.23 pgoyette static int
305 1.23 pgoyette ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
306 1.23 pgoyette {
307 1.23 pgoyette #ifdef _MODULE
308 1.23 pgoyette /*
309 1.23 pgoyette * We ignore the cfdriver_vec[] that ioconf provides, since
310 1.23 pgoyette * the cfdrivers are attached already.
311 1.23 pgoyette */
312 1.23 pgoyette static struct cfdriver * const no_cfdriver_vec[] = { NULL };
313 1.23 pgoyette #endif
314 1.23 pgoyette int error = 0;
315 1.23 pgoyette
316 1.23 pgoyette #ifdef _MODULE
317 1.23 pgoyette switch (cmd) {
318 1.23 pgoyette case MODULE_CMD_INIT:
319 1.23 pgoyette error = config_init_component(no_cfdriver_vec,
320 1.23 pgoyette cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
321 1.23 pgoyette break;
322 1.23 pgoyette case MODULE_CMD_FINI:
323 1.23 pgoyette error = config_fini_component(no_cfdriver_vec,
324 1.23 pgoyette cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
325 1.23 pgoyette break;
326 1.23 pgoyette default:
327 1.23 pgoyette error = ENOTTY;
328 1.23 pgoyette break;
329 1.23 pgoyette }
330 1.23 pgoyette #endif
331 1.23 pgoyette
332 1.23 pgoyette return error;
333 1.23 pgoyette }
334