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