sdmmc.c revision 1.36.4.2 1 /* $NetBSD: sdmmc.c,v 1.36.4.2 2020/08/09 14:03:07 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.2 2020/08/09 14:03:07 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 cv_init(&sc->sc_tskq_cv, "mmctaskq");
155
156 evcnt_attach_dynamic(&sc->sc_ev_xfer, EVCNT_TYPE_MISC, NULL,
157 device_xname(self), "xfer");
158 evcnt_attach_dynamic(&sc->sc_ev_xfer_aligned[0], EVCNT_TYPE_MISC,
159 &sc->sc_ev_xfer, device_xname(self), "xfer 512");
160 evcnt_attach_dynamic(&sc->sc_ev_xfer_aligned[1], EVCNT_TYPE_MISC,
161 &sc->sc_ev_xfer, device_xname(self), "xfer 1024");
162 evcnt_attach_dynamic(&sc->sc_ev_xfer_aligned[2], EVCNT_TYPE_MISC,
163 &sc->sc_ev_xfer, device_xname(self), "xfer 2048");
164 evcnt_attach_dynamic(&sc->sc_ev_xfer_aligned[3], EVCNT_TYPE_MISC,
165 &sc->sc_ev_xfer, device_xname(self), "xfer 4096");
166 evcnt_attach_dynamic(&sc->sc_ev_xfer_aligned[4], EVCNT_TYPE_MISC,
167 &sc->sc_ev_xfer, device_xname(self), "xfer 8192");
168 evcnt_attach_dynamic(&sc->sc_ev_xfer_aligned[5], EVCNT_TYPE_MISC,
169 &sc->sc_ev_xfer, device_xname(self), "xfer 16384");
170 evcnt_attach_dynamic(&sc->sc_ev_xfer_aligned[6], EVCNT_TYPE_MISC,
171 &sc->sc_ev_xfer, device_xname(self), "xfer 32768");
172 evcnt_attach_dynamic(&sc->sc_ev_xfer_aligned[7], EVCNT_TYPE_MISC,
173 &sc->sc_ev_xfer, device_xname(self), "xfer 65536");
174 evcnt_attach_dynamic(&sc->sc_ev_xfer_unaligned, EVCNT_TYPE_MISC,
175 &sc->sc_ev_xfer, device_xname(self), "xfer unaligned");
176 evcnt_attach_dynamic(&sc->sc_ev_xfer_error, EVCNT_TYPE_MISC,
177 &sc->sc_ev_xfer, device_xname(self), "xfer error");
178
179 if (ISSET(sc->sc_caps, SMC_CAPS_POLL_CARD_DET)) {
180 callout_init(&sc->sc_card_detect_ch, 0);
181 callout_reset(&sc->sc_card_detect_ch, hz,
182 sdmmc_polling_card, sc);
183 }
184
185 if (!pmf_device_register(self, NULL, NULL)) {
186 aprint_error_dev(self, "couldn't establish power handler\n");
187 }
188
189 SET(sc->sc_flags, SMF_INITED);
190
191 /*
192 * Create the event thread that will attach and detach cards
193 * and perform other lengthy operations.
194 */
195 config_pending_incr(self);
196 config_interrupts(self, sdmmc_doattach);
197 }
198
199 static int
200 sdmmc_detach(device_t self, int flags)
201 {
202 struct sdmmc_softc *sc = device_private(self);
203 int error, i;
204
205 mutex_enter(&sc->sc_tskq_mtx);
206 sc->sc_dying = 1;
207 cv_signal(&sc->sc_tskq_cv);
208 while (sc->sc_tskq_lwp != NULL)
209 cv_wait(&sc->sc_tskq_cv, &sc->sc_tskq_mtx);
210 mutex_exit(&sc->sc_tskq_mtx);
211
212 pmf_device_deregister(self);
213
214 error = config_detach_children(self, flags);
215 if (error)
216 return error;
217
218 if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
219 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
220 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmap);
221 }
222
223 if (ISSET(sc->sc_caps, SMC_CAPS_POLL_CARD_DET)) {
224 callout_halt(&sc->sc_card_detect_ch, NULL);
225 callout_destroy(&sc->sc_card_detect_ch);
226 }
227
228 sdmmc_del_task(sc, &sc->sc_intr_task, NULL);
229 sdmmc_del_task(sc, &sc->sc_discover_task, NULL);
230
231 cv_destroy(&sc->sc_tskq_cv);
232 mutex_destroy(&sc->sc_discover_task_mtx);
233 mutex_destroy(&sc->sc_tskq_mtx);
234 mutex_destroy(&sc->sc_mtx);
235
236 evcnt_detach(&sc->sc_ev_xfer_error);
237 evcnt_detach(&sc->sc_ev_xfer_unaligned);
238 for (i = 0; i < __arraycount(sc->sc_ev_xfer_aligned); i++)
239 evcnt_detach(&sc->sc_ev_xfer_aligned[i]);
240 evcnt_detach(&sc->sc_ev_xfer);
241
242 return 0;
243 }
244
245 static void
246 sdmmc_doattach(device_t dev)
247 {
248 struct sdmmc_softc *sc = device_private(dev);
249
250 if (kthread_create(PRI_SOFTBIO, 0, NULL,
251 sdmmc_task_thread, sc, &sc->sc_tskq_lwp, "%s", device_xname(dev))) {
252 aprint_error_dev(dev, "couldn't create task thread\n");
253 }
254 }
255
256 void
257 sdmmc_add_task(struct sdmmc_softc *sc, struct sdmmc_task *task)
258 {
259
260 mutex_enter(&sc->sc_tskq_mtx);
261 if (task->sc == sc) {
262 KASSERT(task->onqueue);
263 goto out;
264 }
265 KASSERT(task->sc == NULL);
266 KASSERT(!task->onqueue);
267 task->onqueue = 1;
268 task->sc = sc;
269 TAILQ_INSERT_TAIL(&sc->sc_tskq, task, next);
270 cv_broadcast(&sc->sc_tskq_cv);
271 out: mutex_exit(&sc->sc_tskq_mtx);
272 }
273
274 static inline void
275 sdmmc_del_task1(struct sdmmc_softc *sc, struct sdmmc_task *task)
276 {
277
278 KASSERT(mutex_owned(&sc->sc_tskq_mtx));
279
280 TAILQ_REMOVE(&sc->sc_tskq, task, next);
281 task->sc = NULL;
282 task->onqueue = 0;
283 }
284
285 bool
286 sdmmc_del_task(struct sdmmc_softc *sc, struct sdmmc_task *task,
287 kmutex_t *interlock)
288 {
289 bool cancelled;
290
291 KASSERT(interlock == NULL || mutex_owned(interlock));
292
293 mutex_enter(&sc->sc_tskq_mtx);
294 if (task->sc == sc) {
295 KASSERT(task->onqueue);
296 KASSERT(sc->sc_curtask != task);
297 sdmmc_del_task1(sc, task);
298 cancelled = true;
299 } else {
300 KASSERT(task->sc == NULL);
301 KASSERT(!task->onqueue);
302 mutex_exit(interlock);
303 while (sc->sc_curtask == task) {
304 KASSERT(curlwp != sc->sc_tskq_lwp);
305 cv_wait(&sc->sc_tskq_cv, &sc->sc_tskq_mtx);
306 }
307 if (!mutex_tryenter(interlock)) {
308 mutex_exit(&sc->sc_tskq_mtx);
309 mutex_enter(interlock);
310 mutex_enter(&sc->sc_tskq_mtx);
311 }
312 cancelled = false;
313 }
314 mutex_exit(&sc->sc_tskq_mtx);
315
316 KASSERT(interlock == NULL || mutex_owned(interlock));
317
318 return cancelled;
319 }
320
321 static void
322 sdmmc_task_thread(void *arg)
323 {
324 struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
325 struct sdmmc_task *task;
326
327 sdmmc_discover_task(sc);
328 config_pending_decr(sc->sc_dev);
329
330 mutex_enter(&sc->sc_tskq_mtx);
331 for (;;) {
332 task = TAILQ_FIRST(&sc->sc_tskq);
333 if (task != NULL) {
334 sdmmc_del_task1(sc, task);
335 sc->sc_curtask = task;
336 mutex_exit(&sc->sc_tskq_mtx);
337 (*task->func)(task->arg);
338 mutex_enter(&sc->sc_tskq_mtx);
339 sc->sc_curtask = NULL;
340 cv_broadcast(&sc->sc_tskq_cv);
341 } else {
342 /* Check for the exit condition. */
343 if (sc->sc_dying)
344 break;
345 cv_wait(&sc->sc_tskq_cv, &sc->sc_tskq_mtx);
346 }
347 }
348 /* time to die. */
349 sc->sc_dying = 0;
350 if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) {
351 /*
352 * sdmmc_card_detach() may issue commands,
353 * so temporarily drop the interrupt-blocking lock.
354 */
355 mutex_exit(&sc->sc_tskq_mtx);
356 sdmmc_card_detach(sc, DETACH_FORCE);
357 mutex_enter(&sc->sc_tskq_mtx);
358 }
359 sc->sc_tskq_lwp = NULL;
360 cv_broadcast(&sc->sc_tskq_cv);
361 mutex_exit(&sc->sc_tskq_mtx);
362 kthread_exit(0);
363 }
364
365 void
366 sdmmc_needs_discover(device_t dev)
367 {
368 struct sdmmc_softc *sc = device_private(dev);
369
370 if (!ISSET(sc->sc_flags, SMF_INITED))
371 return;
372
373 sdmmc_add_task(sc, &sc->sc_discover_task);
374 }
375
376 static void
377 sdmmc_discover_task(void *arg)
378 {
379 struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
380 int card_detect, card_present;
381
382 mutex_enter(&sc->sc_discover_task_mtx);
383 card_detect = sdmmc_chip_card_detect(sc->sc_sct, sc->sc_sch);
384 card_present = ISSET(sc->sc_flags, SMF_CARD_PRESENT);
385 if (card_detect)
386 SET(sc->sc_flags, SMF_CARD_PRESENT);
387 else
388 CLR(sc->sc_flags, SMF_CARD_PRESENT);
389 mutex_exit(&sc->sc_discover_task_mtx);
390
391 if (card_detect) {
392 if (!card_present) {
393 sdmmc_card_attach(sc);
394 mutex_enter(&sc->sc_discover_task_mtx);
395 if (!ISSET(sc->sc_flags, SMF_CARD_ATTACHED))
396 CLR(sc->sc_flags, SMF_CARD_PRESENT);
397 mutex_exit(&sc->sc_discover_task_mtx);
398 }
399 } else {
400 if (card_present)
401 sdmmc_card_detach(sc, DETACH_FORCE);
402 }
403 }
404
405 static void
406 sdmmc_polling_card(void *arg)
407 {
408 struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
409 int card_detect, card_present;
410
411 mutex_enter(&sc->sc_discover_task_mtx);
412 card_detect = sdmmc_chip_card_detect(sc->sc_sct, sc->sc_sch);
413 card_present = ISSET(sc->sc_flags, SMF_CARD_PRESENT);
414 mutex_exit(&sc->sc_discover_task_mtx);
415
416 if (card_detect != card_present)
417 sdmmc_needs_discover(sc->sc_dev);
418
419 callout_schedule(&sc->sc_card_detect_ch, hz);
420 }
421
422 /*
423 * Called from process context when a card is present.
424 */
425 static void
426 sdmmc_card_attach(struct sdmmc_softc *sc)
427 {
428 struct sdmmc_function *sf;
429 struct sdmmc_attach_args saa;
430 int error;
431
432 DPRINTF(1,("%s: attach card\n", DEVNAME(sc)));
433
434 CLR(sc->sc_flags, SMF_CARD_ATTACHED);
435
436 sdmmc_chip_hw_reset(sc->sc_sct, sc->sc_sch);
437
438 /*
439 * Power up the card (or card stack).
440 */
441 error = sdmmc_enable(sc);
442 if (error) {
443 if (!ISSET(sc->sc_caps, SMC_CAPS_POLL_CARD_DET)) {
444 aprint_error_dev(sc->sc_dev, "couldn't enable card: %d\n", error);
445 }
446 goto err;
447 }
448
449 /*
450 * Scan for I/O functions and memory cards on the bus,
451 * allocating a sdmmc_function structure for each.
452 */
453 error = sdmmc_scan(sc);
454 if (error) {
455 aprint_error_dev(sc->sc_dev, "no functions\n");
456 goto err;
457 }
458
459 /*
460 * Initialize the I/O functions and memory cards.
461 */
462 error = sdmmc_init(sc);
463 if (error) {
464 aprint_error_dev(sc->sc_dev, "init failed\n");
465 goto err;
466 }
467
468 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
469 if (ISSET(sc->sc_flags, SMF_IO_MODE) && sf->number < 1)
470 continue;
471
472 memset(&saa, 0, sizeof saa);
473 saa.manufacturer = sf->cis.manufacturer;
474 saa.product = sf->cis.product;
475 saa.interface = sf->interface;
476 saa.sf = sf;
477
478 sf->child =
479 config_found_ia(sc->sc_dev, "sdmmc", &saa, sdmmc_print);
480 }
481
482 SET(sc->sc_flags, SMF_CARD_ATTACHED);
483 return;
484
485 err:
486 sdmmc_card_detach(sc, DETACH_FORCE);
487 }
488
489 /*
490 * Called from process context with DETACH_* flags from <sys/device.h>
491 * when cards are gone.
492 */
493 static void
494 sdmmc_card_detach(struct sdmmc_softc *sc, int flags)
495 {
496 struct sdmmc_function *sf, *sfnext;
497
498 DPRINTF(1,("%s: detach card\n", DEVNAME(sc)));
499
500 if (ISSET(sc->sc_flags, SMF_CARD_ATTACHED)) {
501 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
502 if (sf->child != NULL) {
503 config_detach(sf->child, DETACH_FORCE);
504 sf->child = NULL;
505 }
506 }
507
508 KASSERT(TAILQ_EMPTY(&sc->sc_intrq));
509
510 CLR(sc->sc_flags, SMF_CARD_ATTACHED);
511 }
512
513 /* Power down. */
514 sdmmc_disable(sc);
515
516 /* Free all sdmmc_function structures. */
517 for (sf = SIMPLEQ_FIRST(&sc->sf_head); sf != NULL; sf = sfnext) {
518 sfnext = SIMPLEQ_NEXT(sf, sf_list);
519 sdmmc_function_free(sf);
520 }
521 SIMPLEQ_INIT(&sc->sf_head);
522 sc->sc_function_count = 0;
523 sc->sc_fn0 = NULL;
524 }
525
526 static int
527 sdmmc_print(void *aux, const char *pnp)
528 {
529 struct sdmmc_attach_args *sa = aux;
530 struct sdmmc_function *sf = sa->sf;
531 struct sdmmc_cis *cis = &sf->sc->sc_fn0->cis;
532 int i, x;
533
534 if (pnp) {
535 if (sf->number == 0)
536 return QUIET;
537
538 for (i = 0; i < 4 && cis->cis1_info[i]; i++)
539 printf("%s%s", i ? ", " : "\"", cis->cis1_info[i]);
540 if (i != 0)
541 printf("\"");
542
543 if ((cis->manufacturer != SDMMC_VENDOR_INVALID &&
544 cis->product != SDMMC_PRODUCT_INVALID) ||
545 sa->interface != SD_IO_SFIC_NO_STANDARD) {
546 x = !!(cis->manufacturer != SDMMC_VENDOR_INVALID);
547 x += !!(cis->product != SDMMC_PRODUCT_INVALID);
548 x += !!(sa->interface != SD_IO_SFIC_NO_STANDARD);
549 printf("%s(", i ? " " : "");
550 if (cis->manufacturer != SDMMC_VENDOR_INVALID)
551 printf("manufacturer 0x%x%s",
552 cis->manufacturer, (--x == 0) ? "" : ", ");
553 if (cis->product != SDMMC_PRODUCT_INVALID)
554 printf("product 0x%x%s",
555 cis->product, (--x == 0) ? "" : ", ");
556 if (sa->interface != SD_IO_SFIC_NO_STANDARD)
557 printf("standard function interface code 0x%x",
558 sf->interface);
559 printf(")");
560 i = 1;
561 }
562 printf("%sat %s", i ? " " : "", pnp);
563 }
564 if (sf->number > 0)
565 printf(" function %d", sf->number);
566
567 if (!pnp) {
568 for (i = 0; i < 3 && cis->cis1_info[i]; i++)
569 printf("%s%s", i ? ", " : " \"", cis->cis1_info[i]);
570 if (i != 0)
571 printf("\"");
572 }
573 return UNCONF;
574 }
575
576 static int
577 sdmmc_enable(struct sdmmc_softc *sc)
578 {
579 int error;
580
581 /*
582 * Calculate the equivalent of the card OCR from the host
583 * capabilities and select the maximum supported bus voltage.
584 */
585 error = sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch,
586 sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch));
587 if (error) {
588 aprint_error_dev(sc->sc_dev, "couldn't supply bus power\n");
589 goto out;
590 }
591
592 /*
593 * Select the minimum clock frequency.
594 */
595 error = sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_400K,
596 false);
597 if (error) {
598 aprint_error_dev(sc->sc_dev, "couldn't supply clock\n");
599 goto out;
600 }
601
602 /* XXX wait for card to power up */
603 sdmmc_pause(100000, NULL);
604
605 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
606 /* Initialize SD I/O card function(s). */
607 error = sdmmc_io_enable(sc);
608 if (error) {
609 DPRINTF(1, ("%s: sdmmc_io_enable failed %d\n", DEVNAME(sc), error));
610 goto out;
611 }
612 }
613
614 /* Initialize SD/MMC memory card(s). */
615 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ||
616 ISSET(sc->sc_flags, SMF_MEM_MODE)) {
617 error = sdmmc_mem_enable(sc);
618 if (error) {
619 DPRINTF(1, ("%s: sdmmc_mem_enable failed %d\n", DEVNAME(sc), error));
620 goto out;
621 }
622 }
623
624 out:
625 if (error)
626 sdmmc_disable(sc);
627 return error;
628 }
629
630 static void
631 sdmmc_disable(struct sdmmc_softc *sc)
632 {
633 /* XXX complete commands if card is still present. */
634
635 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
636 /* Make sure no card is still selected. */
637 (void)sdmmc_select_card(sc, NULL);
638 }
639
640 /* Turn off bus power and clock. */
641 (void)sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch, 1);
642 (void)sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, SDMMC_SDCLK_OFF,
643 false);
644 (void)sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, 0);
645 sc->sc_busclk = sc->sc_clkmax;
646 }
647
648 /*
649 * Set the lowest bus voltage supported by the card and the host.
650 */
651 int
652 sdmmc_set_bus_power(struct sdmmc_softc *sc, uint32_t host_ocr,
653 uint32_t card_ocr)
654 {
655 uint32_t bit;
656
657 /* Mask off unsupported voltage levels and select the lowest. */
658 DPRINTF(1,("%s: host_ocr=%x ", DEVNAME(sc), host_ocr));
659 host_ocr &= card_ocr;
660 for (bit = 4; bit < 23; bit++) {
661 if (ISSET(host_ocr, (1 << bit))) {
662 host_ocr &= (3 << bit);
663 break;
664 }
665 }
666 DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr));
667
668 if (host_ocr == 0 ||
669 sdmmc_chip_bus_power(sc->sc_sct, sc->sc_sch, host_ocr) != 0)
670 return 1;
671 return 0;
672 }
673
674 struct sdmmc_function *
675 sdmmc_function_alloc(struct sdmmc_softc *sc)
676 {
677 struct sdmmc_function *sf;
678
679 sf = malloc(sizeof *sf, M_DEVBUF, M_WAITOK|M_ZERO);
680 if (sf == NULL) {
681 aprint_error_dev(sc->sc_dev,
682 "couldn't alloc memory (sdmmc function)\n");
683 return NULL;
684 }
685
686 sf->sc = sc;
687 sf->number = -1;
688 sf->cis.manufacturer = SDMMC_VENDOR_INVALID;
689 sf->cis.product = SDMMC_PRODUCT_INVALID;
690 sf->cis.function = SDMMC_FUNCTION_INVALID;
691 sf->width = 1;
692 sf->blklen = sdmmc_chip_host_maxblklen(sc->sc_sct, sc->sc_sch);
693
694 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
695 ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
696 !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
697 bus_dma_segment_t ds;
698 int rseg, error;
699
700 error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, 1,
701 MAXPHYS, 0, BUS_DMA_WAITOK, &sf->bbuf_dmap);
702 if (error)
703 goto fail1;
704 error = bus_dmamem_alloc(sc->sc_dmat, MAXPHYS,
705 PAGE_SIZE, 0, &ds, 1, &rseg, BUS_DMA_WAITOK);
706 if (error)
707 goto fail2;
708 error = bus_dmamem_map(sc->sc_dmat, &ds, 1, MAXPHYS,
709 &sf->bbuf, BUS_DMA_WAITOK);
710 if (error)
711 goto fail3;
712 error = bus_dmamap_load(sc->sc_dmat, sf->bbuf_dmap,
713 sf->bbuf, MAXPHYS, NULL,
714 BUS_DMA_WAITOK|BUS_DMA_READ|BUS_DMA_WRITE);
715 if (error)
716 goto fail4;
717 error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, 1,
718 MAXPHYS, 0, BUS_DMA_WAITOK, &sf->sseg_dmap);
719 if (!error)
720 goto out;
721
722 bus_dmamap_unload(sc->sc_dmat, sf->bbuf_dmap);
723 fail4:
724 bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, MAXPHYS);
725 fail3:
726 bus_dmamem_free(sc->sc_dmat, &ds, 1);
727 fail2:
728 bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
729 fail1:
730 free(sf, M_DEVBUF);
731 sf = NULL;
732 }
733 out:
734
735 return sf;
736 }
737
738 void
739 sdmmc_function_free(struct sdmmc_function *sf)
740 {
741 struct sdmmc_softc *sc = sf->sc;
742
743 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
744 ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
745 !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
746 bus_dmamap_destroy(sc->sc_dmat, sf->sseg_dmap);
747 bus_dmamap_unload(sc->sc_dmat, sf->bbuf_dmap);
748 bus_dmamem_unmap(sc->sc_dmat, sf->bbuf, MAXPHYS);
749 bus_dmamem_free(sc->sc_dmat,
750 sf->bbuf_dmap->dm_segs, sf->bbuf_dmap->dm_nsegs);
751 bus_dmamap_destroy(sc->sc_dmat, sf->bbuf_dmap);
752 }
753
754 free(sf, M_DEVBUF);
755 }
756
757 /*
758 * Scan for I/O functions and memory cards on the bus, allocating a
759 * sdmmc_function structure for each.
760 */
761 static int
762 sdmmc_scan(struct sdmmc_softc *sc)
763 {
764
765 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
766 /* Scan for I/O functions. */
767 if (ISSET(sc->sc_flags, SMF_IO_MODE))
768 sdmmc_io_scan(sc);
769 }
770
771 /* Scan for memory cards on the bus. */
772 if (ISSET(sc->sc_flags, SMF_MEM_MODE))
773 sdmmc_mem_scan(sc);
774
775 /* There should be at least one function now. */
776 if (SIMPLEQ_EMPTY(&sc->sf_head)) {
777 aprint_error_dev(sc->sc_dev, "couldn't identify card\n");
778 return 1;
779 }
780 return 0;
781 }
782
783 /*
784 * Initialize all the distinguished functions of the card, be it I/O
785 * or memory functions.
786 */
787 static int
788 sdmmc_init(struct sdmmc_softc *sc)
789 {
790 struct sdmmc_function *sf;
791
792 /* Initialize all identified card functions. */
793 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
794 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
795 if (ISSET(sc->sc_flags, SMF_IO_MODE) &&
796 sdmmc_io_init(sc, sf) != 0) {
797 aprint_error_dev(sc->sc_dev,
798 "i/o init failed\n");
799 }
800 }
801
802 if (ISSET(sc->sc_flags, SMF_MEM_MODE) &&
803 sdmmc_mem_init(sc, sf) != 0) {
804 aprint_error_dev(sc->sc_dev, "mem init failed\n");
805 }
806 }
807
808 /* Any good functions left after initialization? */
809 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
810 if (!ISSET(sf->flags, SFF_ERROR))
811 return 0;
812 }
813
814 /* No, we should probably power down the card. */
815 return 1;
816 }
817
818 void
819 sdmmc_delay(u_int usecs)
820 {
821
822 delay(usecs);
823 }
824
825 void
826 sdmmc_pause(u_int usecs, kmutex_t *lock)
827 {
828 unsigned ticks = mstohz(usecs/1000);
829
830 if (cold || ticks < 1)
831 delay(usecs);
832 else
833 kpause("sdmmcdelay", false, ticks, lock);
834 }
835
836 int
837 sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_function *sf, struct sdmmc_command *cmd)
838 {
839 struct sdmmc_command acmd;
840 int error;
841
842 DPRINTF(1,("sdmmc_app_command: start\n"));
843
844 /* Don't lock */
845
846 memset(&acmd, 0, sizeof(acmd));
847 acmd.c_opcode = MMC_APP_CMD;
848 acmd.c_arg = (sf != NULL) ? (sf->rca << 16) : 0;
849 acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1 | (cmd->c_flags & SCF_TOUT_OK);
850
851 error = sdmmc_mmc_command(sc, &acmd);
852 if (error == 0) {
853 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) &&
854 !ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) {
855 /* Card does not support application commands. */
856 error = ENODEV;
857 } else {
858 error = sdmmc_mmc_command(sc, cmd);
859 }
860 }
861 DPRINTF(1,("sdmmc_app_command: done (error=%d)\n", error));
862 return error;
863 }
864
865 /*
866 * Execute MMC command and data transfers. All interactions with the
867 * host controller to complete the command happen in the context of
868 * the current process.
869 */
870 int
871 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
872 {
873 int error;
874
875 DPRINTF(1,("sdmmc_mmc_command: cmd=%d, arg=%#x, flags=%#x\n",
876 cmd->c_opcode, cmd->c_arg, cmd->c_flags));
877
878 /* Don't lock */
879
880 #if defined(DIAGNOSTIC) || defined(SDMMC_DEBUG)
881 if (cmd->c_data && !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
882 if (sc->sc_card == NULL)
883 panic("%s: deselected card\n", DEVNAME(sc));
884 }
885 #endif
886
887 sdmmc_chip_exec_command(sc->sc_sct, sc->sc_sch, cmd);
888
889 #ifdef SDMMC_DEBUG
890 sdmmc_dump_command(sc, cmd);
891 #endif
892
893 error = cmd->c_error;
894
895 DPRINTF(1,("sdmmc_mmc_command: error=%d\n", error));
896
897 if (error &&
898 (cmd->c_opcode == MMC_READ_BLOCK_MULTIPLE ||
899 cmd->c_opcode == MMC_WRITE_BLOCK_MULTIPLE)) {
900 sdmmc_stop_transmission(sc);
901 }
902
903 return error;
904 }
905
906 /*
907 * Send the "STOP TRANSMISSION" command
908 */
909 void
910 sdmmc_stop_transmission(struct sdmmc_softc *sc)
911 {
912 struct sdmmc_command cmd;
913
914 DPRINTF(1,("sdmmc_stop_transmission\n"));
915
916 /* Don't lock */
917
918 memset(&cmd, 0, sizeof(cmd));
919 cmd.c_opcode = MMC_STOP_TRANSMISSION;
920 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B | SCF_RSP_SPI_R1B;
921
922 (void)sdmmc_mmc_command(sc, &cmd);
923 }
924
925 /*
926 * Send the "GO IDLE STATE" command.
927 */
928 void
929 sdmmc_go_idle_state(struct sdmmc_softc *sc)
930 {
931 struct sdmmc_command cmd;
932
933 DPRINTF(1,("sdmmc_go_idle_state\n"));
934
935 /* Don't lock */
936
937 memset(&cmd, 0, sizeof(cmd));
938 cmd.c_opcode = MMC_GO_IDLE_STATE;
939 cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0 | SCF_RSP_SPI_R1;
940
941 (void)sdmmc_mmc_command(sc, &cmd);
942 }
943
944 /*
945 * Retrieve (SD) or set (MMC) the relative card address (RCA).
946 */
947 int
948 sdmmc_set_relative_addr(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_set_relative_addr: SMC_CAPS_SPI_MODE set");
958 return EIO;
959 }
960
961 memset(&cmd, 0, sizeof(cmd));
962 if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
963 cmd.c_opcode = SD_SEND_RELATIVE_ADDR;
964 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6;
965 } else {
966 cmd.c_opcode = MMC_SET_RELATIVE_ADDR;
967 cmd.c_arg = MMC_ARG_RCA(sf->rca);
968 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
969 }
970 error = sdmmc_mmc_command(sc, &cmd);
971 if (error)
972 return error;
973
974 if (ISSET(sc->sc_flags, SMF_SD_MODE))
975 sf->rca = SD_R6_RCA(cmd.c_resp);
976
977 return 0;
978 }
979
980 int
981 sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf)
982 {
983 struct sdmmc_command cmd;
984 int error;
985
986 /* Don't lock */
987
988 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
989 device_printf(sc->sc_dev,
990 "sdmmc_select_card: SMC_CAPS_SPI_MODE set");
991 return EIO;
992 }
993
994 if (sc->sc_card == sf
995 || (sf && sc->sc_card && sc->sc_card->rca == sf->rca)) {
996 sc->sc_card = sf;
997 return 0;
998 }
999
1000 memset(&cmd, 0, sizeof(cmd));
1001 cmd.c_opcode = MMC_SELECT_CARD;
1002 cmd.c_arg = (sf == NULL) ? 0 : MMC_ARG_RCA(sf->rca);
1003 cmd.c_flags = SCF_CMD_AC | ((sf == NULL) ? SCF_RSP_R0 : SCF_RSP_R1);
1004 error = sdmmc_mmc_command(sc, &cmd);
1005 if (error == 0 || sf == NULL)
1006 sc->sc_card = sf;
1007
1008 if (error) {
1009 device_printf(sc->sc_dev,
1010 "sdmmc_select_card: error %d", error);
1011 }
1012
1013 return error;
1014 }
1015
1016 #ifdef SDMMC_DEBUG
1017 static void
1018 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
1019 {
1020 int i;
1021
1022 DPRINTF(1,("%s: cmd %u arg=%#x data=%p dlen=%d flags=%#x (error %d)\n",
1023 DEVNAME(sc), cmd->c_opcode, cmd->c_arg, cmd->c_data,
1024 cmd->c_datalen, cmd->c_flags, cmd->c_error));
1025
1026 if (cmd->c_error || sdmmcdebug < 1)
1027 return;
1028
1029 aprint_normal_dev(sc->sc_dev, "resp=");
1030 if (ISSET(cmd->c_flags, SCF_RSP_136))
1031 for (i = 0; i < sizeof cmd->c_resp; i++)
1032 aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
1033 else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT))
1034 for (i = 0; i < 4; i++)
1035 aprint_normal("%02x ", ((uint8_t *)cmd->c_resp)[i]);
1036 else
1037 aprint_normal("none");
1038 aprint_normal("\n");
1039 }
1040
1041 void
1042 sdmmc_dump_data(const char *title, void *ptr, size_t size)
1043 {
1044 char buf[16];
1045 uint8_t *p = ptr;
1046 int i, j;
1047
1048 printf("sdmmc_dump_data: %s\n", title ? title : "");
1049 printf("--------+--------------------------------------------------+------------------+\n");
1050 printf("offset | +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f | data |\n");
1051 printf("--------+--------------------------------------------------+------------------+\n");
1052 for (i = 0; i < (int)size; i++) {
1053 if ((i % 16) == 0) {
1054 printf("%08x| ", i);
1055 } else if ((i % 16) == 8) {
1056 printf(" ");
1057 }
1058
1059 printf("%02x ", p[i]);
1060 buf[i % 16] = p[i];
1061
1062 if ((i % 16) == 15) {
1063 printf("| ");
1064 for (j = 0; j < 16; j++) {
1065 if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
1066 printf("%c", buf[j]);
1067 } else {
1068 printf(".");
1069 }
1070 }
1071 printf(" |\n");
1072 }
1073 }
1074 if ((i % 16) != 0) {
1075 j = (i % 16);
1076 for (; j < 16; j++) {
1077 printf(" ");
1078 if ((j % 16) == 8) {
1079 printf(" ");
1080 }
1081 }
1082
1083 printf("| ");
1084 for (j = 0; j < (i % 16); j++) {
1085 if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
1086 printf("%c", buf[j]);
1087 } else {
1088 printf(".");
1089 }
1090 }
1091 for (; j < 16; j++) {
1092 printf(" ");
1093 }
1094 printf(" |\n");
1095 }
1096 printf("--------+--------------------------------------------------+------------------+\n");
1097 }
1098 #endif
1099