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