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