sdmmc.c revision 1.1.4.3 1 1.1.4.3 yamt /* $NetBSD: sdmmc.c,v 1.1.4.3 2010/08/11 22:54:11 yamt Exp $ */
2 1.1.4.2 yamt /* $OpenBSD: sdmmc.c,v 1.18 2009/01/09 10:58:38 jsg Exp $ */
3 1.1.4.2 yamt
4 1.1.4.2 yamt /*
5 1.1.4.2 yamt * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
6 1.1.4.2 yamt *
7 1.1.4.2 yamt * Permission to use, copy, modify, and distribute this software for any
8 1.1.4.2 yamt * purpose with or without fee is hereby granted, provided that the above
9 1.1.4.2 yamt * copyright notice and this permission notice appear in all copies.
10 1.1.4.2 yamt *
11 1.1.4.2 yamt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1.4.2 yamt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1.4.2 yamt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1.4.2 yamt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1.4.2 yamt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1.4.2 yamt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1.4.2 yamt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1.4.2 yamt */
19 1.1.4.2 yamt
20 1.1.4.2 yamt /*-
21 1.1.4.2 yamt * Copyright (c) 2007-2009 NONAKA Kimihiro <nonaka (at) netbsd.org>
22 1.1.4.2 yamt * All rights reserved.
23 1.1.4.2 yamt *
24 1.1.4.2 yamt * Redistribution and use in source and binary forms, with or without
25 1.1.4.2 yamt * modification, are permitted provided that the following conditions
26 1.1.4.2 yamt * are met:
27 1.1.4.2 yamt * 1. Redistributions of source code must retain the above copyright
28 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer.
29 1.1.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
30 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer in the
31 1.1.4.2 yamt * documentation and/or other materials provided with the distribution.
32 1.1.4.2 yamt *
33 1.1.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 1.1.4.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 1.1.4.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 1.1.4.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 1.1.4.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 1.1.4.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 1.1.4.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 1.1.4.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 1.1.4.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 1.1.4.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 1.1.4.2 yamt * SUCH DAMAGE.
44 1.1.4.2 yamt */
45 1.1.4.2 yamt
46 1.1.4.2 yamt /*
47 1.1.4.2 yamt * Host controller independent SD/MMC bus driver based on information
48 1.1.4.2 yamt * from SanDisk SD Card Product Manual Revision 2.2 (SanDisk), SDIO
49 1.1.4.2 yamt * Simple Specification Version 1.0 (SDIO) and the Linux "mmc" driver.
50 1.1.4.2 yamt */
51 1.1.4.2 yamt
52 1.1.4.2 yamt #include <sys/cdefs.h>
53 1.1.4.3 yamt __KERNEL_RCSID(0, "$NetBSD: sdmmc.c,v 1.1.4.3 2010/08/11 22:54:11 yamt Exp $");
54 1.1.4.2 yamt
55 1.1.4.2 yamt #include <sys/param.h>
56 1.1.4.2 yamt #include <sys/device.h>
57 1.1.4.2 yamt #include <sys/kernel.h>
58 1.1.4.2 yamt #include <sys/kthread.h>
59 1.1.4.2 yamt #include <sys/malloc.h>
60 1.1.4.2 yamt #include <sys/proc.h>
61 1.1.4.2 yamt #include <sys/systm.h>
62 1.1.4.3 yamt #include <sys/callout.h>
63 1.1.4.2 yamt
64 1.1.4.2 yamt #include <dev/sdmmc/sdmmc_ioreg.h>
65 1.1.4.2 yamt #include <dev/sdmmc/sdmmcchip.h>
66 1.1.4.2 yamt #include <dev/sdmmc/sdmmcreg.h>
67 1.1.4.2 yamt #include <dev/sdmmc/sdmmcvar.h>
68 1.1.4.2 yamt
69 1.1.4.2 yamt #ifdef SDMMC_DEBUG
70 1.1.4.2 yamt int sdmmcdebug = 1;
71 1.1.4.2 yamt static void sdmmc_dump_command(struct sdmmc_softc *, struct sdmmc_command *);
72 1.1.4.2 yamt #define DPRINTF(n,s) do { if ((n) <= sdmmcdebug) printf s; } while (0)
73 1.1.4.2 yamt #else
74 1.1.4.2 yamt #define DPRINTF(n,s) do {} while (0)
75 1.1.4.2 yamt #endif
76 1.1.4.2 yamt
77 1.1.4.2 yamt #define DEVNAME(sc) SDMMCDEVNAME(sc)
78 1.1.4.2 yamt
79 1.1.4.2 yamt static int sdmmc_match(device_t, cfdata_t, void *);
80 1.1.4.2 yamt static void sdmmc_attach(device_t, device_t, void *);
81 1.1.4.2 yamt static int sdmmc_detach(device_t, int);
82 1.1.4.2 yamt
83 1.1.4.2 yamt CFATTACH_DECL_NEW(sdmmc, sizeof(struct sdmmc_softc),
84 1.1.4.2 yamt sdmmc_match, sdmmc_attach, sdmmc_detach, NULL);
85 1.1.4.2 yamt
86 1.1.4.2 yamt static void sdmmc_doattach(device_t);
87 1.1.4.2 yamt static void sdmmc_task_thread(void *);
88 1.1.4.2 yamt static void sdmmc_discover_task(void *);
89 1.1.4.3 yamt static void sdmmc_polling_card(void *);
90 1.1.4.2 yamt static void sdmmc_card_attach(struct sdmmc_softc *);
91 1.1.4.2 yamt static void sdmmc_card_detach(struct sdmmc_softc *, int);
92 1.1.4.2 yamt static int sdmmc_print(void *, const char *);
93 1.1.4.2 yamt static int sdmmc_enable(struct sdmmc_softc *);
94 1.1.4.2 yamt static void sdmmc_disable(struct sdmmc_softc *);
95 1.1.4.2 yamt static int sdmmc_scan(struct sdmmc_softc *);
96 1.1.4.2 yamt static int sdmmc_init(struct sdmmc_softc *);
97 1.1.4.2 yamt
98 1.1.4.2 yamt static int
99 1.1.4.2 yamt sdmmc_match(device_t parent, cfdata_t cf, void *aux)
100 1.1.4.2 yamt {
101 1.1.4.2 yamt struct sdmmcbus_attach_args *saa = (struct sdmmcbus_attach_args *)aux;
102 1.1.4.2 yamt
103 1.1.4.2 yamt if (strcmp(saa->saa_busname, cf->cf_name) == 0)
104 1.1.4.2 yamt return 1;
105 1.1.4.2 yamt return 0;
106 1.1.4.2 yamt }
107 1.1.4.2 yamt
108 1.1.4.2 yamt static void
109 1.1.4.2 yamt sdmmc_attach(device_t parent, device_t self, void *aux)
110 1.1.4.2 yamt {
111 1.1.4.2 yamt struct sdmmc_softc *sc = device_private(self);
112 1.1.4.2 yamt struct sdmmcbus_attach_args *saa = (struct sdmmcbus_attach_args *)aux;
113 1.1.4.2 yamt int error;
114 1.1.4.2 yamt
115 1.1.4.2 yamt aprint_normal("\n");
116 1.1.4.2 yamt aprint_naive("\n");
117 1.1.4.2 yamt
118 1.1.4.2 yamt sc->sc_dev = self;
119 1.1.4.2 yamt sc->sc_sct = saa->saa_sct;
120 1.1.4.3 yamt sc->sc_spi_sct = saa->saa_spi_sct;
121 1.1.4.2 yamt sc->sc_sch = saa->saa_sch;
122 1.1.4.2 yamt sc->sc_dmat = saa->saa_dmat;
123 1.1.4.2 yamt sc->sc_clkmin = saa->saa_clkmin;
124 1.1.4.2 yamt sc->sc_clkmax = saa->saa_clkmax;
125 1.1.4.2 yamt sc->sc_busclk = sc->sc_clkmax;
126 1.1.4.2 yamt sc->sc_buswidth = 1;
127 1.1.4.2 yamt sc->sc_caps = saa->saa_caps;
128 1.1.4.2 yamt
129 1.1.4.2 yamt if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
130 1.1.4.2 yamt error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, SDMMC_MAXNSEGS,
131 1.1.4.2 yamt MAXPHYS, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &sc->sc_dmap);
132 1.1.4.2 yamt if (error) {
133 1.1.4.2 yamt aprint_error_dev(sc->sc_dev,
134 1.1.4.2 yamt "couldn't create dma map. (error=%d)\n", error);
135 1.1.4.2 yamt return;
136 1.1.4.2 yamt }
137 1.1.4.2 yamt }
138 1.1.4.2 yamt
139 1.1.4.3 yamt if (ISSET(sc->sc_caps, SMC_CAPS_POLL_CARD_DET)) {
140 1.1.4.3 yamt callout_init(&sc->sc_card_detect_ch, 0);
141 1.1.4.3 yamt callout_reset(&sc->sc_card_detect_ch, hz,
142 1.1.4.3 yamt sdmmc_polling_card, sc);
143 1.1.4.3 yamt }
144 1.1.4.3 yamt
145 1.1.4.2 yamt SIMPLEQ_INIT(&sc->sf_head);
146 1.1.4.2 yamt TAILQ_INIT(&sc->sc_tskq);
147 1.1.4.2 yamt TAILQ_INIT(&sc->sc_intrq);
148 1.1.4.2 yamt
149 1.1.4.2 yamt sdmmc_init_task(&sc->sc_discover_task, sdmmc_discover_task, sc);
150 1.1.4.2 yamt sdmmc_init_task(&sc->sc_intr_task, sdmmc_intr_task, sc);
151 1.1.4.2 yamt
152 1.1.4.2 yamt mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_SDMMC);
153 1.1.4.2 yamt mutex_init(&sc->sc_tskq_mtx, MUTEX_DEFAULT, IPL_SDMMC);
154 1.1.4.2 yamt mutex_init(&sc->sc_discover_task_mtx, MUTEX_DEFAULT, IPL_SDMMC);
155 1.1.4.2 yamt mutex_init(&sc->sc_intr_task_mtx, MUTEX_DEFAULT, IPL_SDMMC);
156 1.1.4.2 yamt cv_init(&sc->sc_tskq_cv, "mmctaskq");
157 1.1.4.2 yamt
158 1.1.4.2 yamt if (!pmf_device_register(self, NULL, NULL)) {
159 1.1.4.2 yamt aprint_error_dev(self, "couldn't establish power handler\n");
160 1.1.4.2 yamt }
161 1.1.4.2 yamt
162 1.1.4.2 yamt SET(sc->sc_flags, SMF_INITED);
163 1.1.4.2 yamt
164 1.1.4.2 yamt /*
165 1.1.4.2 yamt * Create the event thread that will attach and detach cards
166 1.1.4.2 yamt * and perform other lengthy operations.
167 1.1.4.2 yamt */
168 1.1.4.2 yamt config_pending_incr();
169 1.1.4.2 yamt config_interrupts(self, sdmmc_doattach);
170 1.1.4.2 yamt }
171 1.1.4.2 yamt
172 1.1.4.2 yamt static int
173 1.1.4.2 yamt sdmmc_detach(device_t self, int flags)
174 1.1.4.2 yamt {
175 1.1.4.2 yamt struct sdmmc_softc *sc = device_private(self);
176 1.1.4.2 yamt int error;
177 1.1.4.2 yamt
178 1.1.4.2 yamt mutex_enter(&sc->sc_tskq_mtx);
179 1.1.4.2 yamt sc->sc_dying = 1;
180 1.1.4.2 yamt cv_signal(&sc->sc_tskq_cv);
181 1.1.4.2 yamt while (sc->sc_tskq_lwp != NULL)
182 1.1.4.2 yamt cv_wait(&sc->sc_tskq_cv, &sc->sc_tskq_mtx);
183 1.1.4.2 yamt mutex_exit(&sc->sc_tskq_mtx);
184 1.1.4.2 yamt
185 1.1.4.2 yamt pmf_device_deregister(self);
186 1.1.4.2 yamt
187 1.1.4.2 yamt error = config_detach_children(self, flags);
188 1.1.4.2 yamt if (error)
189 1.1.4.2 yamt return error;
190 1.1.4.2 yamt return 0;
191 1.1.4.2 yamt }
192 1.1.4.2 yamt
193 1.1.4.2 yamt static void
194 1.1.4.2 yamt sdmmc_doattach(device_t dev)
195 1.1.4.2 yamt {
196 1.1.4.2 yamt struct sdmmc_softc *sc = device_private(dev);
197 1.1.4.2 yamt
198 1.1.4.2 yamt if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
199 1.1.4.2 yamt sdmmc_task_thread, sc, &sc->sc_tskq_lwp, "%s", device_xname(dev))) {
200 1.1.4.2 yamt aprint_error_dev(dev, "couldn't create task thread\n");
201 1.1.4.2 yamt }
202 1.1.4.2 yamt }
203 1.1.4.2 yamt
204 1.1.4.2 yamt void
205 1.1.4.2 yamt sdmmc_add_task(struct sdmmc_softc *sc, struct sdmmc_task *task)
206 1.1.4.2 yamt {
207 1.1.4.2 yamt
208 1.1.4.2 yamt mutex_enter(&sc->sc_tskq_mtx);
209 1.1.4.2 yamt task->onqueue = 1;
210 1.1.4.2 yamt task->sc = sc;
211 1.1.4.2 yamt TAILQ_INSERT_TAIL(&sc->sc_tskq, task, next);
212 1.1.4.2 yamt cv_broadcast(&sc->sc_tskq_cv);
213 1.1.4.2 yamt mutex_exit(&sc->sc_tskq_mtx);
214 1.1.4.2 yamt }
215 1.1.4.2 yamt
216 1.1.4.2 yamt static inline void
217 1.1.4.2 yamt sdmmc_del_task1(struct sdmmc_softc *sc, struct sdmmc_task *task)
218 1.1.4.2 yamt {
219 1.1.4.2 yamt
220 1.1.4.2 yamt TAILQ_REMOVE(&sc->sc_tskq, task, next);
221 1.1.4.2 yamt task->sc = NULL;
222 1.1.4.2 yamt task->onqueue = 0;
223 1.1.4.2 yamt }
224 1.1.4.2 yamt
225 1.1.4.2 yamt void
226 1.1.4.2 yamt sdmmc_del_task(struct sdmmc_task *task)
227 1.1.4.2 yamt {
228 1.1.4.2 yamt struct sdmmc_softc *sc = (struct sdmmc_softc *)task->sc;
229 1.1.4.2 yamt
230 1.1.4.2 yamt if (sc != NULL) {
231 1.1.4.2 yamt mutex_enter(&sc->sc_tskq_mtx);
232 1.1.4.2 yamt sdmmc_del_task1(sc, task);
233 1.1.4.2 yamt mutex_exit(&sc->sc_tskq_mtx);
234 1.1.4.2 yamt }
235 1.1.4.2 yamt }
236 1.1.4.2 yamt
237 1.1.4.2 yamt static void
238 1.1.4.2 yamt sdmmc_task_thread(void *arg)
239 1.1.4.2 yamt {
240 1.1.4.2 yamt struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
241 1.1.4.2 yamt struct sdmmc_task *task;
242 1.1.4.2 yamt
243 1.1.4.2 yamt sdmmc_discover_task(sc);
244 1.1.4.2 yamt config_pending_decr();
245 1.1.4.2 yamt
246 1.1.4.2 yamt mutex_enter(&sc->sc_tskq_mtx);
247 1.1.4.2 yamt for (;;) {
248 1.1.4.2 yamt task = TAILQ_FIRST(&sc->sc_tskq);
249 1.1.4.2 yamt if (task != NULL) {
250 1.1.4.2 yamt sdmmc_del_task1(sc, task);
251 1.1.4.2 yamt mutex_exit(&sc->sc_tskq_mtx);
252 1.1.4.2 yamt (*task->func)(task->arg);
253 1.1.4.2 yamt mutex_enter(&sc->sc_tskq_mtx);
254 1.1.4.2 yamt } else {
255 1.1.4.2 yamt /* Check for the exit condition. */
256 1.1.4.2 yamt if (sc->sc_dying)
257 1.1.4.2 yamt break;
258 1.1.4.2 yamt cv_wait(&sc->sc_tskq_cv, &sc->sc_tskq_mtx);
259 1.1.4.2 yamt }
260 1.1.4.2 yamt }
261 1.1.4.2 yamt /* time to die. */
262 1.1.4.2 yamt sc->sc_dying = 0;
263 1.1.4.2 yamt if (ISSET(sc->sc_flags, SMF_CARD_PRESENT))
264 1.1.4.2 yamt sdmmc_card_detach(sc, DETACH_FORCE);
265 1.1.4.2 yamt sc->sc_tskq_lwp = NULL;
266 1.1.4.2 yamt cv_broadcast(&sc->sc_tskq_cv);
267 1.1.4.2 yamt mutex_exit(&sc->sc_tskq_mtx);
268 1.1.4.2 yamt kthread_exit(0);
269 1.1.4.2 yamt }
270 1.1.4.2 yamt
271 1.1.4.2 yamt void
272 1.1.4.2 yamt sdmmc_needs_discover(device_t dev)
273 1.1.4.2 yamt {
274 1.1.4.2 yamt struct sdmmc_softc *sc = device_private(dev);
275 1.1.4.2 yamt
276 1.1.4.2 yamt if (!ISSET(sc->sc_flags, SMF_INITED))
277 1.1.4.2 yamt return;
278 1.1.4.2 yamt
279 1.1.4.2 yamt mutex_enter(&sc->sc_discover_task_mtx);
280 1.1.4.2 yamt if (!sdmmc_task_pending(&sc->sc_discover_task))
281 1.1.4.2 yamt sdmmc_add_task(sc, &sc->sc_discover_task);
282 1.1.4.2 yamt mutex_exit(&sc->sc_discover_task_mtx);
283 1.1.4.2 yamt }
284 1.1.4.2 yamt
285 1.1.4.2 yamt static void
286 1.1.4.2 yamt sdmmc_discover_task(void *arg)
287 1.1.4.2 yamt {
288 1.1.4.2 yamt struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
289 1.1.4.2 yamt
290 1.1.4.2 yamt if (sdmmc_chip_card_detect(sc->sc_sct, sc->sc_sch)) {
291 1.1.4.2 yamt if (!ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
292 1.1.4.2 yamt SET(sc->sc_flags, SMF_CARD_PRESENT);
293 1.1.4.2 yamt sdmmc_card_attach(sc);
294 1.1.4.2 yamt }
295 1.1.4.2 yamt } else {
296 1.1.4.2 yamt if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
297 1.1.4.2 yamt CLR(sc->sc_flags, SMF_CARD_PRESENT);
298 1.1.4.2 yamt sdmmc_card_detach(sc, DETACH_FORCE);
299 1.1.4.2 yamt }
300 1.1.4.2 yamt }
301 1.1.4.2 yamt }
302 1.1.4.2 yamt
303 1.1.4.3 yamt static void
304 1.1.4.3 yamt sdmmc_polling_card(void *arg)
305 1.1.4.3 yamt {
306 1.1.4.3 yamt struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
307 1.1.4.3 yamt int card_detect;
308 1.1.4.3 yamt int s;
309 1.1.4.3 yamt
310 1.1.4.3 yamt s = splsdmmc();
311 1.1.4.3 yamt card_detect = sdmmc_chip_card_detect(sc->sc_sct, sc->sc_sch);
312 1.1.4.3 yamt if (card_detect) {
313 1.1.4.3 yamt if (!ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
314 1.1.4.3 yamt sdmmc_needs_discover(sc->sc_dev);
315 1.1.4.3 yamt }
316 1.1.4.3 yamt } else {
317 1.1.4.3 yamt if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
318 1.1.4.3 yamt sdmmc_needs_discover(sc->sc_dev);
319 1.1.4.3 yamt }
320 1.1.4.3 yamt }
321 1.1.4.3 yamt splx(s);
322 1.1.4.3 yamt
323 1.1.4.3 yamt callout_schedule(&sc->sc_card_detect_ch, hz);
324 1.1.4.3 yamt }
325 1.1.4.3 yamt
326 1.1.4.2 yamt /*
327 1.1.4.2 yamt * Called from process context when a card is present.
328 1.1.4.2 yamt */
329 1.1.4.2 yamt static void
330 1.1.4.2 yamt sdmmc_card_attach(struct sdmmc_softc *sc)
331 1.1.4.2 yamt {
332 1.1.4.2 yamt struct sdmmc_function *sf;
333 1.1.4.2 yamt struct sdmmc_attach_args saa;
334 1.1.4.2 yamt int error;
335 1.1.4.2 yamt
336 1.1.4.2 yamt DPRINTF(1,("%s: attach card\n", DEVNAME(sc)));
337 1.1.4.2 yamt
338 1.1.4.2 yamt CLR(sc->sc_flags, SMF_CARD_ATTACHED);
339 1.1.4.2 yamt
340 1.1.4.2 yamt /*
341 1.1.4.2 yamt * Power up the card (or card stack).
342 1.1.4.2 yamt */
343 1.1.4.2 yamt error = sdmmc_enable(sc);
344 1.1.4.2 yamt if (error) {
345 1.1.4.2 yamt aprint_error_dev(sc->sc_dev, "couldn't enable card\n");
346 1.1.4.2 yamt goto err;
347 1.1.4.2 yamt }
348 1.1.4.2 yamt
349 1.1.4.2 yamt /*
350 1.1.4.2 yamt * Scan for I/O functions and memory cards on the bus,
351 1.1.4.2 yamt * allocating a sdmmc_function structure for each.
352 1.1.4.2 yamt */
353 1.1.4.2 yamt error = sdmmc_scan(sc);
354 1.1.4.2 yamt if (error) {
355 1.1.4.2 yamt aprint_error_dev(sc->sc_dev, "no functions\n");
356 1.1.4.2 yamt goto err;
357 1.1.4.2 yamt }
358 1.1.4.2 yamt
359 1.1.4.2 yamt /*
360 1.1.4.3 yamt * Initialize the I/O functions and memory cards.
361 1.1.4.3 yamt */
362 1.1.4.3 yamt error = sdmmc_init(sc);
363 1.1.4.3 yamt if (error) {
364 1.1.4.3 yamt aprint_error_dev(sc->sc_dev, "init failed\n");
365 1.1.4.3 yamt goto err;
366 1.1.4.3 yamt }
367 1.1.4.3 yamt
368 1.1.4.3 yamt /*
369 1.1.4.2 yamt * Set SD/MMC bus clock.
370 1.1.4.2 yamt */
371 1.1.4.2 yamt #ifdef SDMMC_DEBUG
372 1.1.4.2 yamt if ((sc->sc_busclk / 1000) != 0) {
373 1.1.4.2 yamt DPRINTF(1,("%s: bus clock: %u.%03u MHz\n", DEVNAME(sc),
374 1.1.4.2 yamt sc->sc_busclk / 1000, sc->sc_busclk % 1000));
375 1.1.4.2 yamt } else {
376 1.1.4.2 yamt DPRINTF(1,("%s: bus clock: %u KHz\n", DEVNAME(sc),
377 1.1.4.2 yamt sc->sc_busclk % 1000));
378 1.1.4.2 yamt }
379 1.1.4.2 yamt #endif
380 1.1.4.2 yamt (void)sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, sc->sc_busclk);
381 1.1.4.2 yamt
382 1.1.4.2 yamt SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
383 1.1.4.2 yamt if (ISSET(sc->sc_flags, SMF_IO_MODE) && sf->number < 1)
384 1.1.4.2 yamt continue;
385 1.1.4.2 yamt
386 1.1.4.2 yamt memset(&saa, 0, sizeof saa);
387 1.1.4.2 yamt saa.manufacturer = sf->cis.manufacturer;
388 1.1.4.2 yamt saa.product = sf->cis.product;
389 1.1.4.2 yamt saa.sf = sf;
390 1.1.4.2 yamt
391 1.1.4.2 yamt sf->child =
392 1.1.4.2 yamt config_found_ia(sc->sc_dev, "sdmmc", &saa, sdmmc_print);
393 1.1.4.2 yamt }
394 1.1.4.2 yamt
395 1.1.4.2 yamt SET(sc->sc_flags, SMF_CARD_ATTACHED);
396 1.1.4.2 yamt return;
397 1.1.4.2 yamt
398 1.1.4.2 yamt err:
399 1.1.4.2 yamt sdmmc_card_detach(sc, DETACH_FORCE);
400 1.1.4.2 yamt }
401 1.1.4.2 yamt
402 1.1.4.2 yamt /*
403 1.1.4.2 yamt * Called from process context with DETACH_* flags from <sys/device.h>
404 1.1.4.2 yamt * when cards are gone.
405 1.1.4.2 yamt */
406 1.1.4.2 yamt static void
407 1.1.4.2 yamt sdmmc_card_detach(struct sdmmc_softc *sc, int flags)
408 1.1.4.2 yamt {
409 1.1.4.2 yamt struct sdmmc_function *sf, *sfnext;
410 1.1.4.2 yamt
411 1.1.4.2 yamt DPRINTF(1,("%s: detach card\n", DEVNAME(sc)));
412 1.1.4.2 yamt
413 1.1.4.2 yamt if (ISSET(sc->sc_flags, SMF_CARD_ATTACHED)) {
414 1.1.4.2 yamt SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
415 1.1.4.2 yamt if (sf->child != NULL) {
416 1.1.4.2 yamt config_detach(sf->child, DETACH_FORCE);
417 1.1.4.2 yamt sf->child = NULL;
418 1.1.4.2 yamt }
419 1.1.4.2 yamt }
420 1.1.4.2 yamt
421 1.1.4.2 yamt KASSERT(TAILQ_EMPTY(&sc->sc_intrq));
422 1.1.4.2 yamt
423 1.1.4.2 yamt CLR(sc->sc_flags, SMF_CARD_ATTACHED);
424 1.1.4.2 yamt }
425 1.1.4.2 yamt
426 1.1.4.2 yamt /* Power down. */
427 1.1.4.2 yamt sdmmc_disable(sc);
428 1.1.4.2 yamt
429 1.1.4.2 yamt /* Free all sdmmc_function structures. */
430 1.1.4.2 yamt for (sf = SIMPLEQ_FIRST(&sc->sf_head); sf != NULL; sf = sfnext) {
431 1.1.4.2 yamt sfnext = SIMPLEQ_NEXT(sf, sf_list);
432 1.1.4.2 yamt sdmmc_function_free(sf);
433 1.1.4.2 yamt }
434 1.1.4.2 yamt SIMPLEQ_INIT(&sc->sf_head);
435 1.1.4.2 yamt sc->sc_function_count = 0;
436 1.1.4.2 yamt sc->sc_fn0 = NULL;
437 1.1.4.2 yamt }
438 1.1.4.2 yamt
439 1.1.4.2 yamt static int
440 1.1.4.2 yamt sdmmc_print(void *aux, const char *pnp)
441 1.1.4.2 yamt {
442 1.1.4.2 yamt struct sdmmc_attach_args *sa = aux;
443 1.1.4.2 yamt struct sdmmc_function *sf = sa->sf;
444 1.1.4.2 yamt struct sdmmc_cis *cis = &sf->sc->sc_fn0->cis;
445 1.1.4.2 yamt int i;
446 1.1.4.2 yamt
447 1.1.4.2 yamt if (pnp) {
448 1.1.4.2 yamt if (sf->number == 0)
449 1.1.4.2 yamt return QUIET;
450 1.1.4.2 yamt
451 1.1.4.2 yamt for (i = 0; i < 4 && cis->cis1_info[i]; i++)
452 1.1.4.2 yamt printf("%s%s", i ? ", " : "\"", cis->cis1_info[i]);
453 1.1.4.2 yamt if (i != 0)
454 1.1.4.2 yamt printf("\"");
455 1.1.4.2 yamt
456 1.1.4.2 yamt if (cis->manufacturer != SDMMC_VENDOR_INVALID &&
457 1.1.4.2 yamt cis->product != SDMMC_PRODUCT_INVALID) {
458 1.1.4.2 yamt printf("%s(", i ? " " : "");
459 1.1.4.2 yamt if (cis->manufacturer != SDMMC_VENDOR_INVALID)
460 1.1.4.2 yamt printf("manufacturer 0x%x%s",
461 1.1.4.2 yamt cis->manufacturer,
462 1.1.4.2 yamt cis->product == SDMMC_PRODUCT_INVALID ?
463 1.1.4.2 yamt "" : ", ");
464 1.1.4.2 yamt if (cis->product != SDMMC_PRODUCT_INVALID)
465 1.1.4.2 yamt printf("product 0x%x", cis->product);
466 1.1.4.2 yamt printf(")");
467 1.1.4.2 yamt }
468 1.1.4.2 yamt printf("%sat %s", i ? " " : "", pnp);
469 1.1.4.2 yamt }
470 1.1.4.2 yamt if (sf->number > 0)
471 1.1.4.2 yamt printf(" function %d", sf->number);
472 1.1.4.2 yamt
473 1.1.4.2 yamt if (!pnp) {
474 1.1.4.2 yamt for (i = 0; i < 3 && cis->cis1_info[i]; i++)
475 1.1.4.2 yamt printf("%s%s", i ? ", " : " \"", cis->cis1_info[i]);
476 1.1.4.2 yamt if (i != 0)
477 1.1.4.2 yamt printf("\"");
478 1.1.4.2 yamt }
479 1.1.4.2 yamt return UNCONF;
480 1.1.4.2 yamt }
481 1.1.4.2 yamt
482 1.1.4.2 yamt static int
483 1.1.4.2 yamt sdmmc_enable(struct sdmmc_softc *sc)
484 1.1.4.2 yamt {
485 1.1.4.2 yamt int error;
486 1.1.4.2 yamt
487 1.1.4.2 yamt /*
488 1.1.4.2 yamt * Calculate the equivalent of the card OCR from the host
489 1.1.4.2 yamt * capabilities and select the maximum supported bus voltage.
490 1.1.4.2 yamt */
491 1.1.4.2 yamt error = sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch,
492 1.1.4.2 yamt sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch));
493 1.1.4.2 yamt if (error) {
494 1.1.4.2 yamt aprint_error_dev(sc->sc_dev, "couldn't supply bus power\n");
495 1.1.4.2 yamt goto out;
496 1.1.4.2 yamt }
497 1.1.4.2 yamt
498 1.1.4.2 yamt /*
499 1.1.4.2 yamt * Select the minimum clock frequency.
500 1.1.4.2 yamt */
501 1.1.4.2 yamt error = sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_400K);
502 1.1.4.2 yamt if (error) {
503 1.1.4.2 yamt aprint_error_dev(sc->sc_dev, "couldn't supply clock\n");
504 1.1.4.2 yamt goto out;
505 1.1.4.2 yamt }
506 1.1.4.2 yamt
507 1.1.4.2 yamt /* XXX wait for card to power up */
508 1.1.4.2 yamt sdmmc_delay(100000);
509 1.1.4.2 yamt
510 1.1.4.3 yamt if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
511 1.1.4.3 yamt /* Initialize SD I/O card function(s). */
512 1.1.4.3 yamt error = sdmmc_io_enable(sc);
513 1.1.4.3 yamt if (error)
514 1.1.4.3 yamt goto out;
515 1.1.4.3 yamt }
516 1.1.4.2 yamt
517 1.1.4.3 yamt /* Initialize SD/MMC memory card(s). */
518 1.1.4.3 yamt if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ||
519 1.1.4.3 yamt ISSET(sc->sc_flags, SMF_MEM_MODE))
520 1.1.4.2 yamt error = sdmmc_mem_enable(sc);
521 1.1.4.2 yamt
522 1.1.4.2 yamt out:
523 1.1.4.2 yamt if (error)
524 1.1.4.2 yamt sdmmc_disable(sc);
525 1.1.4.2 yamt return error;
526 1.1.4.2 yamt }
527 1.1.4.2 yamt
528 1.1.4.2 yamt static void
529 1.1.4.2 yamt sdmmc_disable(struct sdmmc_softc *sc)
530 1.1.4.2 yamt {
531 1.1.4.2 yamt /* XXX complete commands if card is still present. */
532 1.1.4.2 yamt
533 1.1.4.3 yamt if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
534 1.1.4.3 yamt /* Make sure no card is still selected. */
535 1.1.4.3 yamt (void)sdmmc_select_card(sc, NULL);
536 1.1.4.3 yamt }
537 1.1.4.2 yamt
538 1.1.4.2 yamt /* Turn off bus power and clock. */
539 1.1.4.2 yamt (void)sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch, 1);
540 1.1.4.2 yamt (void)sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_OFF);
541 1.1.4.2 yamt (void)sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, 0);
542 1.1.4.2 yamt }
543 1.1.4.2 yamt
544 1.1.4.2 yamt /*
545 1.1.4.2 yamt * Set the lowest bus voltage supported by the card and the host.
546 1.1.4.2 yamt */
547 1.1.4.2 yamt int
548 1.1.4.2 yamt sdmmc_set_bus_power(struct sdmmc_softc *sc, uint32_t host_ocr,
549 1.1.4.2 yamt uint32_t card_ocr)
550 1.1.4.2 yamt {
551 1.1.4.2 yamt uint32_t bit;
552 1.1.4.2 yamt
553 1.1.4.2 yamt /* Mask off unsupported voltage levels and select the lowest. */
554 1.1.4.2 yamt DPRINTF(1,("%s: host_ocr=%x ", DEVNAME(sc), host_ocr));
555 1.1.4.2 yamt host_ocr &= card_ocr;
556 1.1.4.2 yamt for (bit = 4; bit < 23; bit++) {
557 1.1.4.2 yamt if (ISSET(host_ocr, (1 << bit))) {
558 1.1.4.2 yamt host_ocr &= (3 << bit);
559 1.1.4.2 yamt break;
560 1.1.4.2 yamt }
561 1.1.4.2 yamt }
562 1.1.4.2 yamt DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr));
563 1.1.4.2 yamt
564 1.1.4.2 yamt if (host_ocr == 0 ||
565 1.1.4.2 yamt sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, host_ocr) != 0)
566 1.1.4.2 yamt return 1;
567 1.1.4.2 yamt return 0;
568 1.1.4.2 yamt }
569 1.1.4.2 yamt
570 1.1.4.2 yamt struct sdmmc_function *
571 1.1.4.2 yamt sdmmc_function_alloc(struct sdmmc_softc *sc)
572 1.1.4.2 yamt {
573 1.1.4.2 yamt struct sdmmc_function *sf;
574 1.1.4.2 yamt
575 1.1.4.2 yamt sf = malloc(sizeof *sf, M_DEVBUF, M_WAITOK|M_ZERO);
576 1.1.4.2 yamt if (sf == NULL) {
577 1.1.4.2 yamt aprint_error_dev(sc->sc_dev,
578 1.1.4.2 yamt "couldn't alloc memory (sdmmc function)\n");
579 1.1.4.2 yamt return NULL;
580 1.1.4.2 yamt }
581 1.1.4.2 yamt
582 1.1.4.2 yamt sf->sc = sc;
583 1.1.4.2 yamt sf->number = -1;
584 1.1.4.2 yamt sf->cis.manufacturer = SDMMC_VENDOR_INVALID;
585 1.1.4.2 yamt sf->cis.product = SDMMC_PRODUCT_INVALID;
586 1.1.4.2 yamt sf->cis.function = SDMMC_FUNCTION_INVALID;
587 1.1.4.2 yamt
588 1.1.4.2 yamt return sf;
589 1.1.4.2 yamt }
590 1.1.4.2 yamt
591 1.1.4.2 yamt void
592 1.1.4.2 yamt sdmmc_function_free(struct sdmmc_function *sf)
593 1.1.4.2 yamt {
594 1.1.4.2 yamt
595 1.1.4.2 yamt free(sf, M_DEVBUF);
596 1.1.4.2 yamt }
597 1.1.4.2 yamt
598 1.1.4.2 yamt /*
599 1.1.4.2 yamt * Scan for I/O functions and memory cards on the bus, allocating a
600 1.1.4.2 yamt * sdmmc_function structure for each.
601 1.1.4.2 yamt */
602 1.1.4.2 yamt static int
603 1.1.4.2 yamt sdmmc_scan(struct sdmmc_softc *sc)
604 1.1.4.2 yamt {
605 1.1.4.2 yamt
606 1.1.4.3 yamt if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
607 1.1.4.3 yamt /* Scan for I/O functions. */
608 1.1.4.3 yamt if (ISSET(sc->sc_flags, SMF_IO_MODE))
609 1.1.4.3 yamt sdmmc_io_scan(sc);
610 1.1.4.3 yamt }
611 1.1.4.2 yamt
612 1.1.4.2 yamt /* Scan for memory cards on the bus. */
613 1.1.4.2 yamt if (ISSET(sc->sc_flags, SMF_MEM_MODE))
614 1.1.4.2 yamt sdmmc_mem_scan(sc);
615 1.1.4.2 yamt
616 1.1.4.2 yamt /* There should be at least one function now. */
617 1.1.4.2 yamt if (SIMPLEQ_EMPTY(&sc->sf_head)) {
618 1.1.4.2 yamt aprint_error_dev(sc->sc_dev, "couldn't identify card\n");
619 1.1.4.2 yamt return 1;
620 1.1.4.2 yamt }
621 1.1.4.2 yamt return 0;
622 1.1.4.2 yamt }
623 1.1.4.2 yamt
624 1.1.4.2 yamt /*
625 1.1.4.2 yamt * Initialize all the distinguished functions of the card, be it I/O
626 1.1.4.2 yamt * or memory functions.
627 1.1.4.2 yamt */
628 1.1.4.2 yamt static int
629 1.1.4.2 yamt sdmmc_init(struct sdmmc_softc *sc)
630 1.1.4.2 yamt {
631 1.1.4.2 yamt struct sdmmc_function *sf;
632 1.1.4.2 yamt
633 1.1.4.2 yamt /* Initialize all identified card functions. */
634 1.1.4.2 yamt SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
635 1.1.4.3 yamt if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
636 1.1.4.3 yamt if (ISSET(sc->sc_flags, SMF_IO_MODE) &&
637 1.1.4.3 yamt sdmmc_io_init(sc, sf) != 0) {
638 1.1.4.3 yamt aprint_error_dev(sc->sc_dev,
639 1.1.4.3 yamt "i/o init failed\n");
640 1.1.4.3 yamt }
641 1.1.4.2 yamt }
642 1.1.4.2 yamt
643 1.1.4.2 yamt if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
644 1.1.4.2 yamt sdmmc_mem_init(sc, sf) != 0) {
645 1.1.4.2 yamt aprint_error_dev(sc->sc_dev, "mem init failed\n");
646 1.1.4.2 yamt }
647 1.1.4.2 yamt }
648 1.1.4.2 yamt
649 1.1.4.2 yamt /* Any good functions left after initialization? */
650 1.1.4.2 yamt SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
651 1.1.4.2 yamt if (!ISSET(sf->flags, SFF_ERROR))
652 1.1.4.2 yamt return 0;
653 1.1.4.2 yamt }
654 1.1.4.2 yamt
655 1.1.4.2 yamt /* No, we should probably power down the card. */
656 1.1.4.2 yamt return 1;
657 1.1.4.2 yamt }
658 1.1.4.2 yamt
659 1.1.4.2 yamt void
660 1.1.4.2 yamt sdmmc_delay(u_int usecs)
661 1.1.4.2 yamt {
662 1.1.4.2 yamt
663 1.1.4.2 yamt delay(usecs);
664 1.1.4.2 yamt }
665 1.1.4.2 yamt
666 1.1.4.2 yamt int
667 1.1.4.3 yamt sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_function *sf, struct sdmmc_command *cmd)
668 1.1.4.2 yamt {
669 1.1.4.2 yamt struct sdmmc_command acmd;
670 1.1.4.2 yamt int error;
671 1.1.4.2 yamt
672 1.1.4.2 yamt DPRINTF(1,("sdmmc_app_command: start\n"));
673 1.1.4.2 yamt
674 1.1.4.2 yamt /* Don't lock */
675 1.1.4.2 yamt
676 1.1.4.2 yamt memset(&acmd, 0, sizeof(acmd));
677 1.1.4.2 yamt acmd.c_opcode = MMC_APP_CMD;
678 1.1.4.3 yamt if (sf != NULL) {
679 1.1.4.3 yamt acmd.c_arg = sf->rca << 16;
680 1.1.4.3 yamt acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1;
681 1.1.4.3 yamt } else {
682 1.1.4.3 yamt acmd.c_arg = 0;
683 1.1.4.3 yamt acmd.c_flags = SCF_CMD_BCR | SCF_RSP_R1 | SCF_RSP_SPI_R1;
684 1.1.4.3 yamt }
685 1.1.4.2 yamt
686 1.1.4.2 yamt error = sdmmc_mmc_command(sc, &acmd);
687 1.1.4.2 yamt if (error == 0) {
688 1.1.4.3 yamt if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) &&
689 1.1.4.3 yamt !ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) {
690 1.1.4.2 yamt /* Card does not support application commands. */
691 1.1.4.2 yamt error = ENODEV;
692 1.1.4.2 yamt } else {
693 1.1.4.2 yamt error = sdmmc_mmc_command(sc, cmd);
694 1.1.4.2 yamt }
695 1.1.4.2 yamt }
696 1.1.4.2 yamt DPRINTF(1,("sdmmc_app_command: done (error=%d)\n", error));
697 1.1.4.2 yamt return error;
698 1.1.4.2 yamt }
699 1.1.4.2 yamt
700 1.1.4.2 yamt /*
701 1.1.4.2 yamt * Execute MMC command and data transfers. All interactions with the
702 1.1.4.2 yamt * host controller to complete the command happen in the context of
703 1.1.4.2 yamt * the current process.
704 1.1.4.2 yamt */
705 1.1.4.2 yamt int
706 1.1.4.2 yamt sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
707 1.1.4.2 yamt {
708 1.1.4.2 yamt int error;
709 1.1.4.2 yamt
710 1.1.4.3 yamt DPRINTF(1,("sdmmc_mmc_command: cmd=%d, arg=%#x, flags=%#x\n",
711 1.1.4.2 yamt cmd->c_opcode, cmd->c_arg, cmd->c_flags));
712 1.1.4.2 yamt
713 1.1.4.2 yamt /* Don't lock */
714 1.1.4.2 yamt
715 1.1.4.2 yamt #if defined(DIAGNOSTIC) || defined(SDMMC_DEBUG)
716 1.1.4.3 yamt if (cmd->c_data && !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
717 1.1.4.2 yamt if (sc->sc_card == NULL)
718 1.1.4.2 yamt panic("%s: deselected card\n", DEVNAME(sc));
719 1.1.4.2 yamt }
720 1.1.4.2 yamt #endif
721 1.1.4.2 yamt
722 1.1.4.2 yamt sdmmc_chip_exec_command(sc->sc_sct, sc->sc_sch, cmd);
723 1.1.4.2 yamt
724 1.1.4.2 yamt #ifdef SDMMC_DEBUG
725 1.1.4.2 yamt sdmmc_dump_command(sc, cmd);
726 1.1.4.2 yamt #endif
727 1.1.4.2 yamt
728 1.1.4.2 yamt error = cmd->c_error;
729 1.1.4.2 yamt
730 1.1.4.2 yamt DPRINTF(1,("sdmmc_mmc_command: error=%d\n", error));
731 1.1.4.2 yamt
732 1.1.4.2 yamt return error;
733 1.1.4.2 yamt }
734 1.1.4.2 yamt
735 1.1.4.2 yamt /*
736 1.1.4.2 yamt * Send the "GO IDLE STATE" command.
737 1.1.4.2 yamt */
738 1.1.4.2 yamt void
739 1.1.4.2 yamt sdmmc_go_idle_state(struct sdmmc_softc *sc)
740 1.1.4.2 yamt {
741 1.1.4.2 yamt struct sdmmc_command cmd;
742 1.1.4.2 yamt
743 1.1.4.2 yamt DPRINTF(1,("sdmmc_go_idle_state\n"));
744 1.1.4.2 yamt
745 1.1.4.2 yamt /* Don't lock */
746 1.1.4.2 yamt
747 1.1.4.2 yamt memset(&cmd, 0, sizeof(cmd));
748 1.1.4.2 yamt cmd.c_opcode = MMC_GO_IDLE_STATE;
749 1.1.4.3 yamt cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0 | SCF_RSP_SPI_R1;
750 1.1.4.2 yamt
751 1.1.4.2 yamt (void)sdmmc_mmc_command(sc, &cmd);
752 1.1.4.2 yamt }
753 1.1.4.2 yamt
754 1.1.4.2 yamt /*
755 1.1.4.2 yamt * Retrieve (SD) or set (MMC) the relative card address (RCA).
756 1.1.4.2 yamt */
757 1.1.4.2 yamt int
758 1.1.4.2 yamt sdmmc_set_relative_addr(struct sdmmc_softc *sc, struct sdmmc_function *sf)
759 1.1.4.2 yamt {
760 1.1.4.2 yamt struct sdmmc_command cmd;
761 1.1.4.2 yamt int error;
762 1.1.4.2 yamt
763 1.1.4.2 yamt /* Don't lock */
764 1.1.4.2 yamt
765 1.1.4.3 yamt if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
766 1.1.4.3 yamt return EIO;
767 1.1.4.3 yamt
768 1.1.4.2 yamt memset(&cmd, 0, sizeof(cmd));
769 1.1.4.2 yamt if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
770 1.1.4.2 yamt cmd.c_opcode = SD_SEND_RELATIVE_ADDR;
771 1.1.4.2 yamt cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6;
772 1.1.4.2 yamt } else {
773 1.1.4.2 yamt cmd.c_opcode = MMC_SET_RELATIVE_ADDR;
774 1.1.4.2 yamt cmd.c_arg = MMC_ARG_RCA(sf->rca);
775 1.1.4.2 yamt cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
776 1.1.4.2 yamt }
777 1.1.4.2 yamt error = sdmmc_mmc_command(sc, &cmd);
778 1.1.4.2 yamt if (error)
779 1.1.4.2 yamt return error;
780 1.1.4.2 yamt
781 1.1.4.2 yamt if (ISSET(sc->sc_flags, SMF_SD_MODE))
782 1.1.4.2 yamt sf->rca = SD_R6_RCA(cmd.c_resp);
783 1.1.4.2 yamt
784 1.1.4.2 yamt return 0;
785 1.1.4.2 yamt }
786 1.1.4.2 yamt
787 1.1.4.2 yamt int
788 1.1.4.2 yamt sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf)
789 1.1.4.2 yamt {
790 1.1.4.2 yamt struct sdmmc_command cmd;
791 1.1.4.2 yamt int error;
792 1.1.4.2 yamt
793 1.1.4.2 yamt /* Don't lock */
794 1.1.4.2 yamt
795 1.1.4.3 yamt if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
796 1.1.4.3 yamt return EIO;
797 1.1.4.3 yamt
798 1.1.4.2 yamt if (sc->sc_card == sf
799 1.1.4.2 yamt || (sf && sc->sc_card && sc->sc_card->rca == sf->rca)) {
800 1.1.4.2 yamt sc->sc_card = sf;
801 1.1.4.2 yamt return 0;
802 1.1.4.2 yamt }
803 1.1.4.2 yamt
804 1.1.4.2 yamt memset(&cmd, 0, sizeof(cmd));
805 1.1.4.2 yamt cmd.c_opcode = MMC_SELECT_CARD;
806 1.1.4.2 yamt cmd.c_arg = (sf == NULL) ? 0 : MMC_ARG_RCA(sf->rca);
807 1.1.4.2 yamt cmd.c_flags = SCF_CMD_AC | ((sf == NULL) ? SCF_RSP_R0 : SCF_RSP_R1);
808 1.1.4.2 yamt error = sdmmc_mmc_command(sc, &cmd);
809 1.1.4.2 yamt if (error == 0 || sf == NULL)
810 1.1.4.2 yamt sc->sc_card = sf;
811 1.1.4.2 yamt
812 1.1.4.2 yamt return error;
813 1.1.4.2 yamt }
814 1.1.4.2 yamt
815 1.1.4.2 yamt #ifdef SDMMC_DEBUG
816 1.1.4.2 yamt static void
817 1.1.4.2 yamt sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
818 1.1.4.2 yamt {
819 1.1.4.2 yamt int i;
820 1.1.4.2 yamt
821 1.1.4.3 yamt DPRINTF(1,("%s: cmd %u arg=%#x data=%p dlen=%d flags=%#x (error %d)\n",
822 1.1.4.2 yamt DEVNAME(sc), cmd->c_opcode, cmd->c_arg, cmd->c_data,
823 1.1.4.3 yamt cmd->c_datalen, cmd->c_flags, cmd->c_error));
824 1.1.4.2 yamt
825 1.1.4.2 yamt if (cmd->c_error || sdmmcdebug < 1)
826 1.1.4.2 yamt return;
827 1.1.4.2 yamt
828 1.1.4.2 yamt aprint_normal_dev(sc->sc_dev, "resp=");
829 1.1.4.2 yamt if (ISSET(cmd->c_flags, SCF_RSP_136))
830 1.1.4.2 yamt for (i = 0; i < sizeof cmd->c_resp; i++)
831 1.1.4.2 yamt aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
832 1.1.4.2 yamt else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT))
833 1.1.4.2 yamt for (i = 0; i < 4; i++)
834 1.1.4.2 yamt aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
835 1.1.4.3 yamt else
836 1.1.4.3 yamt aprint_normal("none");
837 1.1.4.2 yamt aprint_normal("\n");
838 1.1.4.2 yamt }
839 1.1.4.3 yamt
840 1.1.4.3 yamt void
841 1.1.4.3 yamt sdmmc_dump_data(const char *title, void *ptr, size_t size)
842 1.1.4.3 yamt {
843 1.1.4.3 yamt char buf[16];
844 1.1.4.3 yamt uint8_t *p = ptr;
845 1.1.4.3 yamt int i, j;
846 1.1.4.3 yamt
847 1.1.4.3 yamt printf("sdmmc_dump_data: %s\n", title ? title : "");
848 1.1.4.3 yamt printf("--------+--------------------------------------------------+------------------+\n");
849 1.1.4.3 yamt printf("offset | +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f | data |\n");
850 1.1.4.3 yamt printf("--------+--------------------------------------------------+------------------+\n");
851 1.1.4.3 yamt for (i = 0; i < (int)size; i++) {
852 1.1.4.3 yamt if ((i % 16) == 0) {
853 1.1.4.3 yamt printf("%08x| ", i);
854 1.1.4.3 yamt } else if ((i % 16) == 8) {
855 1.1.4.3 yamt printf(" ");
856 1.1.4.3 yamt }
857 1.1.4.3 yamt
858 1.1.4.3 yamt printf("%02x ", p[i]);
859 1.1.4.3 yamt buf[i % 16] = p[i];
860 1.1.4.3 yamt
861 1.1.4.3 yamt if ((i % 16) == 15) {
862 1.1.4.3 yamt printf("| ");
863 1.1.4.3 yamt for (j = 0; j < 16; j++) {
864 1.1.4.3 yamt if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
865 1.1.4.3 yamt printf("%c", buf[j]);
866 1.1.4.3 yamt } else {
867 1.1.4.3 yamt printf(".");
868 1.1.4.3 yamt }
869 1.1.4.3 yamt }
870 1.1.4.3 yamt printf(" |\n");
871 1.1.4.3 yamt }
872 1.1.4.3 yamt }
873 1.1.4.3 yamt if ((i % 16) != 0) {
874 1.1.4.3 yamt j = (i % 16);
875 1.1.4.3 yamt for (; j < 16; j++) {
876 1.1.4.3 yamt printf(" ");
877 1.1.4.3 yamt if ((j % 16) == 8) {
878 1.1.4.3 yamt printf(" ");
879 1.1.4.3 yamt }
880 1.1.4.3 yamt }
881 1.1.4.3 yamt
882 1.1.4.3 yamt printf("| ");
883 1.1.4.3 yamt for (j = 0; j < (i % 16); j++) {
884 1.1.4.3 yamt if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
885 1.1.4.3 yamt printf("%c", buf[j]);
886 1.1.4.3 yamt } else {
887 1.1.4.3 yamt printf(".");
888 1.1.4.3 yamt }
889 1.1.4.3 yamt }
890 1.1.4.3 yamt for (; j < 16; j++) {
891 1.1.4.3 yamt printf(" ");
892 1.1.4.3 yamt }
893 1.1.4.3 yamt printf(" |\n");
894 1.1.4.3 yamt }
895 1.1.4.3 yamt printf("--------+--------------------------------------------------+------------------+\n");
896 1.1.4.3 yamt }
897 1.1.4.2 yamt #endif
898