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