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