ld_sdmmc.c revision 1.2.2.2 1 1.2.2.2 jym /* $NetBSD: ld_sdmmc.c,v 1.2.2.2 2009/05/13 17:21:29 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.2 jym __KERNEL_RCSID(0, "$NetBSD: ld_sdmmc.c,v 1.2.2.2 2009/05/13 17:21:29 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.2 jym #if NRND > 0
47 1.2.2.2 jym #include <sys/rnd.h>
48 1.2.2.2 jym #endif
49 1.2.2.2 jym
50 1.2.2.2 jym #include <uvm/uvm_extern.h>
51 1.2.2.2 jym
52 1.2.2.2 jym #include <dev/ldvar.h>
53 1.2.2.2 jym
54 1.2.2.2 jym #include <dev/sdmmc/sdmmcvar.h>
55 1.2.2.2 jym
56 1.2.2.2 jym #ifdef SDMMC_DEBUG
57 1.2.2.2 jym #define DPRINTF(s) printf s
58 1.2.2.2 jym #else
59 1.2.2.2 jym #define DPRINTF(s) /**/
60 1.2.2.2 jym #endif
61 1.2.2.2 jym
62 1.2.2.2 jym struct ld_sdmmc_softc;
63 1.2.2.2 jym
64 1.2.2.2 jym struct ld_sdmmc_task {
65 1.2.2.2 jym struct sdmmc_task task;
66 1.2.2.2 jym
67 1.2.2.2 jym struct ld_sdmmc_softc *task_sc;
68 1.2.2.2 jym struct buf *task_bp;
69 1.2.2.2 jym callout_t task_callout;
70 1.2.2.2 jym };
71 1.2.2.2 jym
72 1.2.2.2 jym struct ld_sdmmc_softc {
73 1.2.2.2 jym struct ld_softc sc_ld;
74 1.2.2.2 jym int sc_hwunit;
75 1.2.2.2 jym
76 1.2.2.2 jym struct sdmmc_function *sc_sf;
77 1.2.2.2 jym struct ld_sdmmc_task sc_task;
78 1.2.2.2 jym };
79 1.2.2.2 jym
80 1.2.2.2 jym static int ld_sdmmc_match(device_t, struct cfdata *, void *);
81 1.2.2.2 jym static void ld_sdmmc_attach(device_t, device_t, void *);
82 1.2.2.2 jym static int ld_sdmmc_detach(device_t, int);
83 1.2.2.2 jym
84 1.2.2.2 jym static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
85 1.2.2.2 jym static int ld_sdmmc_start(struct ld_softc *, struct buf *);
86 1.2.2.2 jym
87 1.2.2.2 jym static void ld_sdmmc_dobio(void *);
88 1.2.2.2 jym static void ld_sdmmc_timeout(void *);
89 1.2.2.2 jym
90 1.2.2.2 jym CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
91 1.2.2.2 jym ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
92 1.2.2.2 jym
93 1.2.2.2 jym
94 1.2.2.2 jym /* ARGSUSED */
95 1.2.2.2 jym static int
96 1.2.2.2 jym ld_sdmmc_match(device_t parent, struct cfdata *match, void *aux)
97 1.2.2.2 jym {
98 1.2.2.2 jym struct sdmmc_softc *sdmsc = device_private(parent);
99 1.2.2.2 jym
100 1.2.2.2 jym if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
101 1.2.2.2 jym return 1;
102 1.2.2.2 jym return 0;
103 1.2.2.2 jym }
104 1.2.2.2 jym
105 1.2.2.2 jym /* ARGSUSED */
106 1.2.2.2 jym static void
107 1.2.2.2 jym ld_sdmmc_attach(device_t parent, device_t self, void *aux)
108 1.2.2.2 jym {
109 1.2.2.2 jym struct ld_sdmmc_softc *sc = device_private(self);
110 1.2.2.2 jym struct sdmmc_attach_args *sa = aux;
111 1.2.2.2 jym struct ld_softc *ld = &sc->sc_ld;
112 1.2.2.2 jym
113 1.2.2.2 jym ld->sc_dv = self;
114 1.2.2.2 jym
115 1.2.2.2 jym aprint_normal("\n");
116 1.2.2.2 jym aprint_naive("\n");
117 1.2.2.2 jym
118 1.2.2.2 jym callout_init(&sc->sc_task.task_callout, CALLOUT_MPSAFE);
119 1.2.2.2 jym
120 1.2.2.2 jym sc->sc_hwunit = 0; /* always 0? */
121 1.2.2.2 jym sc->sc_sf = sa->sf;
122 1.2.2.2 jym
123 1.2.2.2 jym ld->sc_flags = LDF_ENABLED;
124 1.2.2.2 jym ld->sc_secperunit = sc->sc_sf->csd.capacity;
125 1.2.2.2 jym ld->sc_secsize = sc->sc_sf->csd.sector_size;
126 1.2.2.2 jym ld->sc_maxxfer = MAXPHYS;
127 1.2.2.2 jym ld->sc_maxqueuecnt = 1;
128 1.2.2.2 jym ld->sc_dump = ld_sdmmc_dump;
129 1.2.2.2 jym ld->sc_start = ld_sdmmc_start;
130 1.2.2.2 jym
131 1.2.2.2 jym ldattach(ld);
132 1.2.2.2 jym }
133 1.2.2.2 jym
134 1.2.2.2 jym static int
135 1.2.2.2 jym ld_sdmmc_detach(device_t dev, int flags)
136 1.2.2.2 jym {
137 1.2.2.2 jym struct ld_sdmmc_softc *sc = device_private(dev);
138 1.2.2.2 jym struct ld_softc *ld = &sc->sc_ld;
139 1.2.2.2 jym int rv;
140 1.2.2.2 jym
141 1.2.2.2 jym if ((rv = ldbegindetach(ld, flags)) != 0)
142 1.2.2.2 jym return rv;
143 1.2.2.2 jym ldenddetach(ld);
144 1.2.2.2 jym
145 1.2.2.2 jym return 0;
146 1.2.2.2 jym }
147 1.2.2.2 jym
148 1.2.2.2 jym static int
149 1.2.2.2 jym ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
150 1.2.2.2 jym {
151 1.2.2.2 jym struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
152 1.2.2.2 jym struct ld_sdmmc_task *task = &sc->sc_task;
153 1.2.2.2 jym
154 1.2.2.2 jym task->task_sc = sc;
155 1.2.2.2 jym task->task_bp = bp;
156 1.2.2.2 jym sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
157 1.2.2.2 jym
158 1.2.2.2 jym callout_reset(&task->task_callout, hz, ld_sdmmc_timeout, task);
159 1.2.2.2 jym sdmmc_add_task(sc->sc_sf->sc, &task->task);
160 1.2.2.2 jym
161 1.2.2.2 jym return 0;
162 1.2.2.2 jym }
163 1.2.2.2 jym
164 1.2.2.2 jym static void
165 1.2.2.2 jym ld_sdmmc_dobio(void *arg)
166 1.2.2.2 jym {
167 1.2.2.2 jym struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
168 1.2.2.2 jym struct ld_sdmmc_softc *sc = task->task_sc;
169 1.2.2.2 jym struct buf *bp = task->task_bp;
170 1.2.2.2 jym int error, s;
171 1.2.2.2 jym
172 1.2.2.2 jym callout_stop(&task->task_callout);
173 1.2.2.2 jym
174 1.2.2.2 jym /*
175 1.2.2.2 jym * I/O operation
176 1.2.2.2 jym */
177 1.2.2.2 jym DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
178 1.2.2.2 jym device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
179 1.2.2.2 jym bp->b_rawblkno, bp->b_bcount));
180 1.2.2.2 jym
181 1.2.2.2 jym /* is everything done in terms of blocks? */
182 1.2.2.2 jym if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
183 1.2.2.2 jym /* trying to read or write past end of device */
184 1.2.2.2 jym DPRINTF(("%s: blkno exceeds capacity 0x%x\n",
185 1.2.2.2 jym device_xname(sc->sc_ld.sc_dv), sc->sc_sf->csd.capacity));
186 1.2.2.2 jym bp->b_error = EIO; /* XXX */
187 1.2.2.2 jym bp->b_resid = bp->b_bcount;
188 1.2.2.2 jym lddone(&sc->sc_ld, bp);
189 1.2.2.2 jym return;
190 1.2.2.2 jym }
191 1.2.2.2 jym
192 1.2.2.2 jym s = splbio();
193 1.2.2.2 jym if (bp->b_flags & B_READ)
194 1.2.2.2 jym error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
195 1.2.2.2 jym bp->b_data, bp->b_bcount);
196 1.2.2.2 jym else
197 1.2.2.2 jym error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
198 1.2.2.2 jym bp->b_data, bp->b_bcount);
199 1.2.2.2 jym if (error) {
200 1.2.2.2 jym DPRINTF(("%s: error %d\n", device_xname(sc->sc_ld.sc_dv),
201 1.2.2.2 jym error));
202 1.2.2.2 jym bp->b_error = EIO; /* XXXX */
203 1.2.2.2 jym bp->b_resid = bp->b_bcount;
204 1.2.2.2 jym } else {
205 1.2.2.2 jym bp->b_resid = 0;
206 1.2.2.2 jym }
207 1.2.2.2 jym splx(s);
208 1.2.2.2 jym
209 1.2.2.2 jym lddone(&sc->sc_ld, bp);
210 1.2.2.2 jym }
211 1.2.2.2 jym
212 1.2.2.2 jym static void
213 1.2.2.2 jym ld_sdmmc_timeout(void *arg)
214 1.2.2.2 jym {
215 1.2.2.2 jym struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
216 1.2.2.2 jym struct ld_sdmmc_softc *sc = task->task_sc;
217 1.2.2.2 jym struct buf *bp = task->task_bp;
218 1.2.2.2 jym int s;
219 1.2.2.2 jym
220 1.2.2.2 jym s = splbio();
221 1.2.2.2 jym if (!sdmmc_task_pending(&task->task)) {
222 1.2.2.2 jym splx(s);
223 1.2.2.2 jym return;
224 1.2.2.2 jym }
225 1.2.2.2 jym bp->b_error = EIO; /* XXXX */
226 1.2.2.2 jym bp->b_resid = bp->b_bcount;
227 1.2.2.2 jym sdmmc_del_task(&task->task);
228 1.2.2.2 jym splx(s);
229 1.2.2.2 jym
230 1.2.2.2 jym lddone(&sc->sc_ld, bp);
231 1.2.2.2 jym }
232 1.2.2.2 jym
233 1.2.2.2 jym static int
234 1.2.2.2 jym ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
235 1.2.2.2 jym {
236 1.2.2.2 jym struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
237 1.2.2.2 jym
238 1.2.2.2 jym return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
239 1.2.2.2 jym blkcnt * ld->sc_secsize);
240 1.2.2.2 jym }
241