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