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