dev_sdmmc.c revision 1.4 1 /*-
2 * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Paul Fleischer <paul (at) xpg.dk>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * All SD/MMC code is taken from various files in sys/dev/sdmmc
32 */
33 /*
34 * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
35 *
36 * Permission to use, copy, modify, and distribute this software for any
37 * purpose with or without fee is hereby granted, provided that the above
38 * copyright notice and this permission notice appear in all copies.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
41 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
43 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
45 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
46 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47 */
48
49 /*-
50 * Copyright (c) 2007-2010 NONAKA Kimihiro <nonaka (at) netbsd.org>
51 * All rights reserved.
52 *
53 * Redistribution and use in source and binary forms, with or without
54 * modification, are permitted provided that the following conditions
55 * are met:
56 * 1. Redistributions of source code must retain the above copyright
57 * notice, this list of conditions and the following disclaimer.
58 * 2. Redistributions in binary form must reproduce the above copyright
59 * notice, this list of conditions and the following disclaimer in the
60 * documentation and/or other materials provided with the distribution.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
73 */
74
75 #include <machine/limits.h>
76
77 #include <sys/param.h>
78 #include <sys/types.h>
79 #include <sys/disklabel.h>
80
81 #include <netinet/in.h>
82
83 #include <lib/libsa/stand.h>
84
85 #include <lib/libkern/libkern.h>
86 #include <lib/libsa/stand.h>
87 #include <lib/libsa/iodesc.h>
88
89 #include <dev/sdmmc/sdmmcreg.h>
90 #include "dev_sdmmc.h"
91 #include "s3csdi.h"
92
93 #ifndef SET
94 #define SET(t, f) ((t) |= (f))
95 #endif
96 #ifndef ISSET
97 #define ISSET(t, f) ((t) & (f))
98 #endif
99 #ifndef CLR
100 #define CLR(t, f) ((t) &= ~(f))
101 #endif
102
103
104 //#define SDMMC_DEBUG
105 #ifdef SDMMC_DEBUG
106 #define DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
107 #else
108 #define DPRINTF(s) do {} while (/*CONSTCOND*/0)
109 #endif
110
111 /* SD/MMC device driver structure */
112 struct sdifdv {
113 char* name;
114 int (*match)(unsigned);
115 void* (*init)(unsigned, uint32_t*);
116 int (*host_ocr)(void*);
117 int (*bus_clock)(void*, int);
118 int (*bus_power)(void*, int);
119 int (*bus_width)(void*, int);
120 void (*exec_cmd)(void*, struct sdmmc_command*);
121 int (*get_max_bus_clock)(void*);
122 void* priv;
123 };
124
125 struct sdmmc_softc;
126
127 /* Structure used for of->f_devdata */
128 struct sdmmc_part {
129 struct sdmmc_softc *sc;
130 struct partition *part;
131 };
132
133 /* SD/MMC driver structure */
134 struct sdmmc_softc {
135 uint32_t flags;
136 uint32_t caps;
137 uint16_t rca; /* relative card address */
138 sdmmc_response raw_cid; /* temp. storage for decoding */
139 uint32_t raw_scr[2];
140 struct sdmmc_csd csd; /* decoded CSD value */
141 struct sdmmc_cid cid; /* decoded CID value */
142 struct sdmmc_scr scr;
143 int busclk;
144 struct sdifdv *sdifdv;
145 struct disklabel sc_label;
146 int npartitions;
147 struct sdmmc_part partitions[MAXPARTITIONS];
148 };
149
150 static struct sdifdv vnifdv[] = {
151 {"S3C SD/MMC", s3csd_match, s3csd_init, s3csd_host_ocr,
152 s3csd_bus_clock, s3csd_bus_power, s3csd_bus_width, s3csd_exec_cmd,
153 s3csd_get_max_bus_clock}
154 };
155 static int nnifdv = sizeof(vnifdv)/sizeof(vnifdv[0]);
156
157 static struct sdmmc_softc sdmmc_softc;
158 static uint8_t sdmmc_initialized = FALSE;
159
160 extern time_t getsecs();
161 extern time_t getusecs();
162 extern void usleep(int);
163
164 /* Local functions */
165 static int sdmmc_getdisklabel(struct sdmmc_softc *sc);
166 static int sdmmc_init(unsigned int tag);
167 static int sdmmc_enable(struct sdmmc_softc*);
168
169 static int sdmmc_mem_send_if_cond(struct sdmmc_softc*, uint32_t, uint32_t*);
170 static int sdmmc_mmc_command(struct sdmmc_softc*, struct sdmmc_command*);
171 static void sdmmc_go_idle_state(struct sdmmc_softc*);
172 static int sdmmc_mem_send_op_cond(struct sdmmc_softc*, uint32_t, uint32_t *);
173 static int sdmmc_set_bus_power(struct sdmmc_softc*, uint32_t, uint32_t);
174 static int sdmmc_app_command(struct sdmmc_softc*, uint16_t,
175 struct sdmmc_command*);
176 static int sdmmc_mmc_command(struct sdmmc_softc*, struct sdmmc_command*);
177 static int sdmmc_scan(struct sdmmc_softc*);
178 static void sdmmc_mem_scan(struct sdmmc_softc*);
179 static int sdmmc_set_relative_addr(struct sdmmc_softc*);
180 static int sdmmc_mem_send_cid(struct sdmmc_softc*, sdmmc_response*);
181
182 static int sdmmc_mem_send_csd(struct sdmmc_softc*, sdmmc_response*);
183 static int sdmmc_decode_csd(struct sdmmc_softc*, sdmmc_response);
184 static int sdmmc_decode_cid(struct sdmmc_softc*, sdmmc_response);
185
186 static int sdmmc_mem_read_block(struct sdmmc_softc*, uint32_t, u_char*, size_t);
187 static int sdmmc_select_card(struct sdmmc_softc*);
188 static int sdmmc_mem_set_blocklen(struct sdmmc_softc*);
189
190 static int sdmmc_mem_send_scr(struct sdmmc_softc*, uint32_t[2]);
191 static int sdmmc_mem_decode_scr(struct sdmmc_softc*);
192 static int sdmmc_set_bus_width(struct sdmmc_softc*, int);
193 static int sdmmc_mem_sd_switch(struct sdmmc_softc *, int, int, int, void*);
194
195 #ifdef SDMMC_DEBUG
196 static void sdmmc_dump_data(const char*, void*, size_t);
197 static void sdmmc_print_cid(struct sdmmc_cid*);
198 static void sdmmc_dump_command(struct sdmmc_softc*, struct sdmmc_command*);
199 #endif
200
201 int
202 sdmmc_open(struct open_file *of, ...)
203 {
204 va_list ap;
205 int unit __unused, part;
206
207 va_start(ap, of);
208 unit = va_arg(ap, u_int); /* Not used for now */
209 part = va_arg(ap, u_int);
210 va_end(ap);
211
212 /* Simply try to initialize SD mem sub system. */
213 if( !sdmmc_init(0) ) {
214 return 1;
215 }
216
217 of->f_devdata = (void*)&sdmmc_softc.partitions[part];
218
219 return 0;
220 }
221
222 int
223 sdmmc_close(struct open_file *f)
224 {
225 return (0);
226 }
227
228 int
229 sdmmc_get_fstype(void *p) {
230 struct sdmmc_part *part = (struct sdmmc_part*)p;
231
232 return part->part->p_fstype;
233 }
234
235
236 int
237 sdmmc_strategy(void *d, int f, daddr_t b, size_t s, void *buf, size_t *r)
238 {
239 struct sdmmc_part *part = (struct sdmmc_part*)d;
240 unsigned int offset;
241 switch(f) {
242 case F_READ:
243 offset = part->part->p_offset + b;
244 *r = s;
245 if(sdmmc_mem_read_block(part->sc, offset, buf, s) == 0)
246 return 0;
247 else
248 return EIO;
249 default:
250 printf("Unsupported operation\n");
251 break;
252 }
253 return (EIO);
254 }
255
256 int
257 sdmmc_getdisklabel(struct sdmmc_softc *sc)
258 {
259 char *msg;
260 int sector, i, n;
261 size_t rsize;
262 struct mbr_partition *dp, *bsdp;
263 struct disklabel *lp;
264 /*uint8_t *buf = wd->sc_buf;*/
265 uint8_t buf[DEV_BSIZE];
266
267 lp = &sc->sc_label;
268 memset(lp, 0, sizeof(struct disklabel));
269
270 sector = 0;
271 if (sdmmc_strategy(&sc->partitions[0], F_READ, MBR_BBSECTOR, DEV_BSIZE,
272 buf, &rsize))
273 return EOFFSET;
274
275 dp = (struct mbr_partition *)(buf + MBR_PART_OFFSET);
276 bsdp = NULL;
277 for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
278 if (dp->mbrp_type == MBR_PTYPE_NETBSD) {
279 bsdp = dp;
280 break;
281 }
282 }
283 if (!bsdp) {
284 /* generate fake disklabel */
285 lp->d_secsize = DEV_BSIZE;
286 /*lp->d_ntracks = wd->sc_params.atap_heads;
287 lp->d_nsectors = wd->sc_params.atap_sectors;
288 lp->d_ncylinders = wd->sc_params.atap_cylinders;*/
289 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
290 lp->d_type = DKTYPE_FLASH;
291 /*strncpy(lp->d_typename, (char *)wd->sc_params.atap_model, 16);*/
292 strncpy(lp->d_packname, "fictitious", 16);
293 /*if (wd->sc_capacity > UINT32_MAX)
294 lp->d_secperunit = UINT32_MAX;
295 else
296 lp->d_secperunit = wd->sc_capacity;*/
297 lp->d_rpm = 3600;
298 lp->d_interleave = 1;
299 lp->d_flags = 0;
300 lp->d_partitions[RAW_PART].p_offset = 0;
301 lp->d_partitions[RAW_PART].p_size =
302 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
303 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
304 lp->d_magic = DISKMAGIC;
305 lp->d_magic2 = DISKMAGIC;
306 lp->d_checksum = dkcksum(lp);
307
308 dp = (struct mbr_partition *)(buf + MBR_PART_OFFSET);
309 n = 'e' - 'a';
310 for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
311 if (dp->mbrp_type == MBR_PTYPE_UNUSED)
312 continue;
313 lp->d_partitions[n].p_offset = bswap32(dp->mbrp_start);
314 lp->d_partitions[n].p_size = bswap32(dp->mbrp_size);
315 switch (dp->mbrp_type) {
316 case MBR_PTYPE_FAT12:
317 case MBR_PTYPE_FAT16S:
318 case MBR_PTYPE_FAT16B:
319 case MBR_PTYPE_FAT32:
320 case MBR_PTYPE_FAT32L:
321 case MBR_PTYPE_FAT16L:
322 lp->d_partitions[n].p_fstype = FS_MSDOS;
323 break;
324 case MBR_PTYPE_LNXEXT2:
325 lp->d_partitions[n].p_fstype = FS_EX2FS;
326 break;
327 default:
328 lp->d_partitions[n].p_fstype = FS_OTHER;
329 break;
330 }
331 n += 1;
332 }
333 lp->d_npartitions = n;
334 }
335 else {
336 sector = bsdp->mbrp_start;
337 if (sdmmc_strategy(&sc->partitions[0], F_READ,
338 sector + LABELSECTOR, DEV_BSIZE,
339 buf, &rsize))
340 return EOFFSET;
341 msg = getdisklabel((char *)buf + LABELOFFSET, &sc->sc_label);
342 if (msg != NULL)
343 printf("getdisklabel: %s\n", msg);
344 }
345 /*DPRINTF(("label info: d_secsize %d, d_nsectors %d, d_ncylinders %d,"
346 "d_ntracks %d, d_secpercyl %d\n",
347 wd->sc_label.d_secsize,
348 wd->sc_label.d_nsectors,
349 wd->sc_label.d_ncylinders,
350 wd->sc_label.d_ntracks,
351 wd->sc_label.d_secpercyl));*/
352
353 return 0;
354 }
355
356 void
357 sdmmc_delay(int us) {
358 usleep(us);
359 }
360
361 /* Initialize the SD/MMC subsystem. Return 1 on success, and 0 on error.
362 In case of error, errno will be set to a sane value.
363 */
364 int
365 sdmmc_init(unsigned int tag)
366 {
367 struct sdifdv *dv;
368 int n;
369 int error;
370 struct sdmmc_softc *sc = &sdmmc_softc;
371 char status[64];
372
373 if (sdmmc_initialized) {
374 printf("SD/MMC already initialized\n");
375 return 1;
376 }
377
378 for (n = 0; n < nnifdv; n++) {
379 dv = &vnifdv[n];
380 if ((*dv->match)(tag) > 0)
381 goto found;
382 }
383 errno = ENODEV;
384 return 0;
385 found:
386 sc->caps = 0;
387 /* Init should return NULL if no card is present. */
388 sc->sdifdv->priv = (*dv->init)(tag, &sc->caps);
389 if (sc->sdifdv->priv == NULL) {
390 /* We expect that the device initialization sets
391 errno properly */
392 return 0;
393 }
394
395 sc->flags = 0;
396 sc->sdifdv = dv;
397
398 /* Perfom SD-card initialization. */
399 if( sdmmc_enable(sc) ) {
400 printf("Failed to enable SD interface\n");
401 errno = EIO;
402 return 0;
403 }
404 sc->busclk = sc->sdifdv->get_max_bus_clock(sc->sdifdv->priv);
405
406 if (sdmmc_scan(sc)) {
407 printf("No functions\n");
408 errno = EIO;
409 return 0;
410 }
411
412 if (sdmmc_select_card(sc)) {
413 printf("Failed to select card\n");
414 errno = EIO;
415 return 0;
416 }
417
418 if (!ISSET(sc->flags, SMF_CARD_SDHC)) {
419 sdmmc_mem_set_blocklen(sc);
420 }
421
422 /* change bus width if supported */
423 if (ISSET(sc->flags, SMF_SD_MODE) ) {
424 error = sdmmc_mem_send_scr(sc, sc->raw_scr);
425 if (error) {
426 DPRINTF(("SD_SEND_SCR send failed.\n"));
427 errno = EIO;
428 return 0;
429 }
430 error = sdmmc_mem_decode_scr(sc);
431 if (error) {
432 errno = EIO;
433 return 0;
434 }
435
436 if (ISSET(sc->caps, SMC_CAPS_4BIT_MODE) &&
437 ISSET(sc->scr.bus_width, SCR_SD_BUS_WIDTHS_4BIT)) {
438 error = sdmmc_set_bus_width(sc, 4);
439 if (error) {
440 DPRINTF(("can't change bus width"
441 " (%d bit)\n", 4));
442 errno = EIO;
443 return 0;
444 }
445 }
446
447 #if 1
448 if (sc->scr.sd_spec >= SCR_SD_SPEC_VER_1_10 &&
449 ISSET(sc->csd.ccc, SD_CSD_CCC_SWITCH)) {
450 DPRINTF(("switch func mode 0\n"));
451 error = sdmmc_mem_sd_switch(sc, 0, 1, 0, status);
452 if (error) {
453 printf("switch func mode 0 failed\n");
454 errno = error;
455 return 0;
456 }
457 }
458 #endif
459 sc->sdifdv->bus_clock(sc->sdifdv->priv, sc->busclk);
460 }
461
462 /* Prepare dummy partition[0] entry used by sdmmc_getdisklabel() */
463 sc->partitions[0].sc = sc;
464 sc->partitions[0].part->p_offset = 0;
465
466 if(sdmmc_getdisklabel(sc)) {
467 errno = EOFFSET;
468 return 0;
469 }
470
471 sc->npartitions = sc->sc_label.d_npartitions;
472 for(n=0; n<sc->sc_label.d_npartitions; n++) {
473 sc->partitions[n].part = &sc->sc_label.d_partitions[n];
474 sc->partitions[n].sc = sc;
475 }
476
477 sdmmc_initialized = TRUE;
478
479 return 1;
480 }
481
482 int
483 sdmmc_enable(struct sdmmc_softc *sc)
484 {
485 uint32_t card_ocr;
486 uint32_t ocr = 0;
487 uint32_t host_ocr;
488 int error;
489
490 /* 1. Set the maximum power supported by bus */
491 /* For now, we expect the init function to set the maximum
492 voltage. And if that is not supported by the SD-card we
493 just cannot work with it.
494 */
495
496 sc->busclk = 400;
497 /* 2. Clock bus at minimum frequency */
498 sc->sdifdv->bus_clock(sc->sdifdv->priv, 400);
499
500 /* We expect that the above call has performed any waiting needed.*/
501
502 /* Initialize SD/MMC memory card(s), which is the only thing
503 we support.
504 */
505
506 /* Set host mode to SD "combo" card or SD memory-only. */
507 SET(sc->flags, SMF_SD_MODE|SMF_MEM_MODE);
508
509 sdmmc_go_idle_state(sc);
510
511 error = sdmmc_mem_send_if_cond(sc, 0x1aa, &card_ocr);
512 if (error == 0 && card_ocr == 0x1aa)
513 SET(ocr, MMC_OCR_HCS);
514
515 /*
516 * Read the SD/MMC memory OCR value by issuing CMD55 followed
517 * by ACMD41 to read the OCR value from memory-only SD cards.
518 * MMC cards will not respond to CMD55 or ACMD41 and this is
519 * how we distinguish them from SD cards.
520 */
521 mmc_mode:
522 error = sdmmc_mem_send_op_cond(sc,
523 ISSET(sc->caps, SMC_CAPS_SPI_MODE) ? ocr : 0, &card_ocr);
524 if (error) {
525 if (ISSET(sc->flags, SMF_SD_MODE) &&
526 !ISSET(sc->flags, SMF_IO_MODE)) {
527 /* Not a SD card, switch to MMC mode. */
528 DPRINTF(("Switch to MMC mode\n"));
529 CLR(sc->flags, SMF_SD_MODE);
530 goto mmc_mode;
531 }
532 if (!ISSET(sc->flags, SMF_SD_MODE)) {
533 DPRINTF(("couldn't read memory OCR\n"));
534 goto out;
535 } else {
536 /* Not a "combo" card. */
537 CLR(sc->flags, SMF_MEM_MODE);
538 error = 0;
539 goto out;
540 }
541 }
542 #if 0 /* SPI NOT SUPPORTED */
543 if (ISSET(ssc->caps, SMC_CAPS_SPI_MODE)) {
544 /* get card OCR */
545 error = sdmmc_mem_spi_read_ocr(sc, ocr, &card_ocr);
546 if (error) {
547 DPRINTF(("%s: couldn't read SPI memory OCR\n",
548 SDMMCDEVNAME(sc)));
549 goto out;
550 }
551 }
552 #endif
553
554 /* Set the lowest voltage supported by the card and host. */
555 host_ocr = sc->sdifdv->host_ocr(sc->sdifdv->priv);
556 error = sdmmc_set_bus_power(sc, host_ocr, card_ocr);
557 if (error) {
558 DPRINTF(("Couldn't supply voltage requested by card\n"));
559 goto out;
560 }
561 host_ocr &= card_ocr;
562 host_ocr |= ocr;
563
564 /* Send the new OCR value until all cards are ready. */
565 error = sdmmc_mem_send_op_cond(sc, host_ocr, NULL);
566 if (error) {
567 DPRINTF(("Couldn't send memory OCR\n"));
568 goto out;
569 }
570
571 out:
572 return error;
573 }
574
575 int
576 sdmmc_mem_send_if_cond(struct sdmmc_softc *sc, uint32_t ocr, uint32_t *ocrp)
577 {
578 struct sdmmc_command cmd;
579 int error;
580
581 memset(&cmd, 0, sizeof(cmd));
582 cmd.c_arg = ocr;
583 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R7 | SCF_RSP_SPI_R7;
584 cmd.c_opcode = SD_SEND_IF_COND;
585
586 error = sdmmc_mmc_command(sc, &cmd);
587 if (error == 0 && ocrp != NULL) {
588 *ocrp = MMC_R7(cmd.c_resp);
589 }
590
591 return error;
592 }
593
594 void
595 sdmmc_go_idle_state(struct sdmmc_softc *sc)
596 {
597 struct sdmmc_command cmd;
598
599 memset(&cmd, 0, sizeof(cmd));
600 cmd.c_opcode = MMC_GO_IDLE_STATE;
601 cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0 | SCF_RSP_SPI_R1;
602
603 (void)sdmmc_mmc_command(sc, &cmd);
604 }
605 int
606 sdmmc_mem_send_op_cond(struct sdmmc_softc *sc, uint32_t ocr, uint32_t *ocrp)
607 {
608 struct sdmmc_command cmd;
609 int error;
610 int retry;
611
612
613 /*
614 * If we change the OCR value, retry the command until the OCR
615 * we receive in response has the "CARD BUSY" bit set, meaning
616 * that all cards are ready for identification.
617 */
618 for (retry = 0; retry < 100; retry++) {
619 memset(&cmd, 0, sizeof(cmd));
620 cmd.c_arg = !ISSET(sc->caps, SMC_CAPS_SPI_MODE) ?
621 ocr : (ocr & MMC_OCR_HCS);
622 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R3 | SCF_RSP_SPI_R1;
623
624 if (ISSET(sc->flags, SMF_SD_MODE)) {
625 cmd.c_opcode = SD_APP_OP_COND;
626 error = sdmmc_app_command(sc, 0, &cmd);
627 } else {
628 cmd.c_opcode = MMC_SEND_OP_COND;
629 error = sdmmc_mmc_command(sc, &cmd);
630 }
631 if (error)
632 break;
633
634 if (ISSET(sc->caps, SMC_CAPS_SPI_MODE)) {
635 if (!ISSET(MMC_SPI_R1(cmd.c_resp), R1_SPI_IDLE))
636 break;
637 } else {
638 if (ISSET(MMC_R3(cmd.c_resp), MMC_OCR_MEM_READY) ||
639 ocr == 0)
640 break;
641 }
642
643 error = ETIMEDOUT;
644 sdmmc_delay(10000);
645 }
646 if (error == 0 &&
647 ocrp != NULL &&
648 !ISSET(sc->caps, SMC_CAPS_SPI_MODE))
649 *ocrp = MMC_R3(cmd.c_resp);
650 DPRINTF(("sdmmc_mem_send_op_cond: error=%d, ocr=%x\n",
651 error, MMC_R3(cmd.c_resp)));
652 return error;
653 }
654
655 /*
656 * Set the lowest bus voltage supported by the card and the host.
657 */
658 int
659 sdmmc_set_bus_power(struct sdmmc_softc *sc, uint32_t host_ocr,
660 uint32_t card_ocr)
661 {
662 uint32_t bit;
663
664 /* Mask off unsupported voltage levels and select the lowest. */
665 DPRINTF(("host_ocr=%x ", host_ocr));
666 host_ocr &= card_ocr;
667 for (bit = 4; bit < 23; bit++) {
668 if (ISSET(host_ocr, (1 << bit))) {
669 host_ocr &= (3 << bit);
670 break;
671 }
672 }
673 DPRINTF(("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr));
674
675 if (host_ocr == 0 ||
676 sc->sdifdv->bus_power(sc->sdifdv->priv, host_ocr) != 0)
677 return 1;
678 return 0;
679 }
680
681 int
682 sdmmc_app_command(struct sdmmc_softc *sc, uint16_t rca,
683 struct sdmmc_command *cmd)
684 {
685 struct sdmmc_command acmd;
686 int error;
687
688 memset(&acmd, 0, sizeof(acmd));
689 acmd.c_opcode = MMC_APP_CMD;
690 if (rca != 0) {
691 acmd.c_arg = rca << 16;
692 acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1;
693 } else {
694 acmd.c_arg = 0;
695 acmd.c_flags = SCF_CMD_BCR | SCF_RSP_R1 | SCF_RSP_SPI_R1;
696 }
697
698 error = sdmmc_mmc_command(sc, &acmd);
699 if (error == 0) {
700 if (!ISSET(sc->caps, SMC_CAPS_SPI_MODE) &&
701 !ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) {
702 /* Card does not support application commands. */
703 error = ENODEV;
704 } else {
705 error = sdmmc_mmc_command(sc, cmd);
706 }
707 }
708 DPRINTF(("sdmmc_app_command: done (error=%d)\n", error));
709 return error;
710 }
711
712 void
713 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
714 {
715 int i;
716
717 printf("cmd %u arg=%x data=%p dlen=%d flags=%x (error %d)\n",
718 cmd->c_opcode, cmd->c_arg, cmd->c_data,
719 cmd->c_datalen, cmd->c_flags, cmd->c_error);
720
721 if (cmd->c_error )
722 return;
723
724 printf("resp=");
725 if (ISSET(cmd->c_flags, SCF_RSP_136))
726 for (i = 0; i < sizeof cmd->c_resp; i++)
727 printf("%02x ", ((uint8_t *)cmd->c_resp)[i]);
728 else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT))
729 for (i = 0; i < 4; i++)
730 printf("%02x ", ((uint8_t *)cmd->c_resp)[i]);
731 else
732 printf("none");
733 printf("\n");
734 }
735
736 int
737 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd)
738 {
739 int error;
740
741 DPRINTF(("sdmmc_mmc_command: cmd=%d, arg=%x, flags=%x\n",
742 cmd->c_opcode, cmd->c_arg, cmd->c_flags));
743
744 #if 0
745 #if defined(DIAGNOSTIC) || defined(SDMMC_DEBUG)
746 if (cmd->c_data && !ISSET(sc->caps, SMC_CAPS_SPI_MODE)) {
747 if (sc->sc_card == NULL)
748 panic("%s: deselected card\n", DEVNAME(sc));
749 }
750 #endif
751 #endif
752
753 sc->sdifdv->exec_cmd(sc->sdifdv->priv, cmd);
754
755 #ifdef SDMMC_DEBUG
756
757 sdmmc_dump_command(sc, cmd);
758
759 #endif
760
761 error = cmd->c_error;
762
763 DPRINTF(("sdmmc_mmc_command: error=%d\n", error));
764
765 return error;
766 }
767
768 /*
769 * Scan for I/O functions and memory cards on the bus, allocating a
770 * sdmmc_function structure for each.
771 */
772 int
773 sdmmc_scan(struct sdmmc_softc *sc)
774 {
775
776 #if 0 /* SPI NOT SUPPORTED */
777 if (!ISSET(sc->caps, SMC_CAPS_SPI_MODE)) {
778 /* Scan for I/O functions. */
779 if (ISSET(sc->sc_flags, SMF_IO_MODE))
780 sdmmc_io_scan(sc);
781 }
782 #endif
783
784 /* Scan for memory cards on the bus. */
785 if (ISSET(sc->flags, SMF_MEM_MODE))
786 sdmmc_mem_scan(sc);
787
788 DPRINTF(("Bus clock speed: %d\n", sc->busclk));
789 return sc->sdifdv->bus_clock(sc->sdifdv->priv, sc->busclk);
790 }
791
792 /*
793 * Read the CSD and CID from all cards and assign each card a unique
794 * relative card address (RCA). CMD2 is ignored by SDIO-only cards.
795 */
796 void
797 sdmmc_mem_scan(struct sdmmc_softc *sc)
798 {
799 sdmmc_response resp;
800 //struct sdmmc_function *sf;
801 // uint16_t next_rca;
802 int error;
803 int retry;
804
805 /*
806 * CMD2 is a broadcast command understood by SD cards and MMC
807 * cards. All cards begin to respond to the command, but back
808 * off if another card drives the CMD line to a different level.
809 * Only one card will get its entire response through. That
810 * card remains silent once it has been assigned a RCA.
811 */
812 for (retry = 0; retry < 100; retry++) {
813 error = sdmmc_mem_send_cid(sc, &resp);
814 if (error) {
815 if (!ISSET(sc->caps, SMC_CAPS_SPI_MODE) &&
816 error == ETIMEDOUT) {
817 /* No more cards there. */
818 break;
819 }
820 DPRINTF(("Couldn't read CID\n"));
821 break;
822 }
823
824 /* In MMC mode, find the next available RCA. */
825 /*next_rca = 1;
826 if (!ISSET(dv->flags, SMF_SD_MODE)) {
827 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list)
828 next_rca++;
829 }*/
830
831 /* Allocate a sdmmc_function structure. */
832 /*sf = sdmmc_function_alloc(sc);
833 sf->rca = next_rca;*/
834
835 /*
836 * Remember the CID returned in the CMD2 response for
837 * later decoding.
838 */
839 memcpy(sc->raw_cid, resp, sizeof(sc->raw_cid));
840
841 /*
842 * Silence the card by assigning it a unique RCA, or
843 * querying it for its RCA in the case of SD.
844 */
845 if (!ISSET(sc->caps, SMC_CAPS_SPI_MODE)) {
846 if (sdmmc_set_relative_addr(sc) != 0) {
847 DPRINTF(("couldn't set mem RCA\n"));
848 break;
849 }
850 }
851
852 /*
853 * If this is a memory-only card, the card responding
854 * first becomes an alias for SDIO function 0.
855 */
856 /*if (sc->sc_fn0 == NULL)
857 sc->sc_fn0 = sf;
858
859 SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);*/
860
861 /* only one function in SPI mode */
862 /*if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
863 break;*/
864 }
865
866 /*
867 * All cards are either inactive or awaiting further commands.
868 * Read the CSDs and decode the raw CID for each card.
869 */
870 /* SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {*/
871 error = sdmmc_mem_send_csd(sc, &resp);
872 if (error) {
873 /*SET(sf->flags, SFF_ERROR);
874 continue;*/
875 }
876
877 if (sdmmc_decode_csd(sc, resp) != 0 ||
878 sdmmc_decode_cid(sc, sc->raw_cid) != 0) {
879 /*SET(sf->flags, SFF_ERROR);
880 continue;*/
881 }
882
883 #ifdef SDMMC_DEBUG
884 printf("CID: ");
885 sdmmc_print_cid(&sc->cid);
886 #endif
887 /* }*/
888 }
889
890 /*
891 * Retrieve (SD) or set (MMC) the relative card address (RCA).
892 */
893 int
894 sdmmc_set_relative_addr(struct sdmmc_softc *sc)
895 {
896 struct sdmmc_command cmd;
897 int error;
898
899 /* Don't lock */
900
901 if (ISSET(sc->caps, SMC_CAPS_SPI_MODE))
902 return EIO;
903
904 memset(&cmd, 0, sizeof(cmd));
905 if (ISSET(sc->flags, SMF_SD_MODE)) {
906 cmd.c_opcode = SD_SEND_RELATIVE_ADDR;
907 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6;
908 } else {
909 cmd.c_opcode = MMC_SET_RELATIVE_ADDR;
910 cmd.c_arg = MMC_ARG_RCA(sc->rca);
911 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
912 }
913 error = sdmmc_mmc_command(sc, &cmd);
914 if (error)
915 return error;
916
917 if (ISSET(sc->flags, SMF_SD_MODE))
918 sc->rca = SD_R6_RCA(cmd.c_resp);
919
920 return 0;
921 }
922
923 int
924 sdmmc_mem_send_cid(struct sdmmc_softc *sc, sdmmc_response *resp)
925 {
926 struct sdmmc_command cmd;
927 int error;
928
929
930 memset(&cmd, 0, sizeof cmd);
931 cmd.c_opcode = MMC_ALL_SEND_CID;
932 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R2;
933
934 error = sdmmc_mmc_command(sc, &cmd);
935
936 #ifdef SDMMC_DEBUG
937 sdmmc_dump_data("CID", cmd.c_resp, sizeof(cmd.c_resp));
938 #endif
939 if (error == 0 && resp != NULL)
940 memcpy(resp, &cmd.c_resp, sizeof(*resp));
941 return error;
942 }
943
944 void
945 sdmmc_dump_data(const char *title, void *ptr, size_t size)
946 {
947 char buf[16];
948 uint8_t *p = ptr;
949 int i, j;
950
951 printf("sdmmc_dump_data: %s\n", title ? title : "");
952 printf("--------+--------------------------------------------------+------------------+\n");
953 printf("offset | +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f | data |\n");
954 printf("--------+--------------------------------------------------+------------------+\n");
955 for (i = 0; i < (int)size; i++) {
956 if ((i % 16) == 0) {
957 printf("%08x| ", i);
958 } else if ((i % 16) == 8) {
959 printf(" ");
960 }
961
962 printf("%02x ", p[i]);
963 buf[i % 16] = p[i];
964
965 if ((i % 16) == 15) {
966 printf("| ");
967 for (j = 0; j < 16; j++) {
968 if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
969 printf("%c", buf[j]);
970 } else {
971 printf(".");
972 }
973 }
974 printf(" |\n");
975 }
976 }
977 if ((i % 16) != 0) {
978 j = (i % 16);
979 for (; j < 16; j++) {
980 printf(" ");
981 if ((j % 16) == 8) {
982 printf(" ");
983 }
984 }
985
986 printf("| ");
987 for (j = 0; j < (i % 16); j++) {
988 if (buf[j] >= 0x20 && buf[j] <= 0x7e) {
989 printf("%c", buf[j]);
990 } else {
991 printf(".");
992 }
993 }
994 for (; j < 16; j++) {
995 printf(" ");
996 }
997 printf(" |\n");
998 }
999 printf("--------+--------------------------------------------------+------------------+\n");
1000 }
1001
1002 int
1003 sdmmc_mem_send_csd(struct sdmmc_softc *sc, sdmmc_response *resp)
1004 {
1005 struct sdmmc_command cmd;
1006 int error;
1007
1008 memset(&cmd, 0, sizeof cmd);
1009 cmd.c_opcode = MMC_SEND_CSD;
1010 cmd.c_arg = MMC_ARG_RCA(sc->rca);
1011 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R2;
1012
1013 error = sdmmc_mmc_command(sc, &cmd);
1014
1015 #ifdef SDMMC_DEBUG
1016 sdmmc_dump_data("CSD", cmd.c_resp, sizeof(cmd.c_resp));
1017 #endif
1018 if (error == 0 && resp != NULL)
1019 memcpy(resp, &cmd.c_resp, sizeof(*resp));
1020 return error;
1021 }
1022
1023 int
1024 sdmmc_decode_csd(struct sdmmc_softc *sc, sdmmc_response resp)
1025 {
1026 /* TRAN_SPEED(2:0): transfer rate exponent */
1027 static const int speed_exponent[8] = {
1028 100 * 1, /* 100 Kbits/s */
1029 1 * 1000, /* 1 Mbits/s */
1030 10 * 1000, /* 10 Mbits/s */
1031 100 * 1000, /* 100 Mbits/s */
1032 0,
1033 0,
1034 0,
1035 0,
1036 };
1037 /* TRAN_SPEED(6:3): time mantissa */
1038 static const int speed_mantissa[16] = {
1039 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80,
1040 };
1041 struct sdmmc_csd *csd = &sc->csd;
1042 int e, m;
1043
1044 if (ISSET(sc->flags, SMF_SD_MODE)) {
1045 /*
1046 * CSD version 1.0 corresponds to SD system
1047 * specification version 1.0 - 1.10. (SanDisk, 3.5.3)
1048 */
1049 csd->csdver = SD_CSD_CSDVER(resp);
1050 switch (csd->csdver) {
1051 case SD_CSD_CSDVER_2_0:
1052 DPRINTF(("SD Ver.2.0\n"));
1053 SET(sc->flags, SMF_CARD_SDHC);
1054 csd->capacity = SD_CSD_V2_CAPACITY(resp);
1055 csd->read_bl_len = SD_CSD_V2_BL_LEN;
1056 csd->ccc = SD_CSD_CCC(resp);
1057 break;
1058
1059 case SD_CSD_CSDVER_1_0:
1060 DPRINTF(("SD Ver.1.0\n"));
1061 csd->capacity = SD_CSD_CAPACITY(resp);
1062 csd->read_bl_len = SD_CSD_READ_BL_LEN(resp);
1063 break;
1064
1065 default:
1066 printf("unknown SD CSD structure version 0x%x\n",
1067 csd->csdver);
1068 return 1;
1069 }
1070
1071 csd->mmcver = SD_CSD_MMCVER(resp);
1072 csd->write_bl_len = SD_CSD_WRITE_BL_LEN(resp);
1073 csd->r2w_factor = SD_CSD_R2W_FACTOR(resp);
1074 e = SD_CSD_SPEED_EXP(resp);
1075 m = SD_CSD_SPEED_MANT(resp);
1076 csd->tran_speed = speed_exponent[e] * speed_mantissa[m] / 10;
1077 } else {
1078 csd->csdver = MMC_CSD_CSDVER(resp);
1079 if (csd->csdver == MMC_CSD_CSDVER_1_0 ) {
1080 printf("unknown MMC CSD structure version 0x%x\n",
1081 csd->csdver);
1082 return 1;
1083 }
1084
1085 csd->mmcver = MMC_CSD_MMCVER(resp);
1086 csd->capacity = MMC_CSD_CAPACITY(resp);
1087 csd->read_bl_len = MMC_CSD_READ_BL_LEN(resp);
1088 csd->write_bl_len = MMC_CSD_WRITE_BL_LEN(resp);
1089 csd->r2w_factor = MMC_CSD_R2W_FACTOR(resp);
1090 e = MMC_CSD_TRAN_SPEED_EXP(resp);
1091 m = MMC_CSD_TRAN_SPEED_MANT(resp);
1092 csd->tran_speed = speed_exponent[e] * speed_mantissa[m] / 10;
1093 }
1094 if ((1 << csd->read_bl_len) > SDMMC_SECTOR_SIZE)
1095 csd->capacity *= (1 << csd->read_bl_len) / SDMMC_SECTOR_SIZE;
1096
1097
1098 if (sc->busclk > csd->tran_speed)
1099 sc->busclk = csd->tran_speed;
1100
1101 #ifdef SDMMC_DUMP_CSD
1102 sdmmc_print_csd(resp, csd);
1103 #endif
1104
1105 return 0;
1106 }
1107
1108 int
1109 sdmmc_decode_cid(struct sdmmc_softc *sc, sdmmc_response resp)
1110 {
1111 struct sdmmc_cid *cid = &sc->cid;
1112
1113 if (ISSET(sc->flags, SMF_SD_MODE)) {
1114 cid->mid = SD_CID_MID(resp);
1115 cid->oid = SD_CID_OID(resp);
1116 SD_CID_PNM_CPY(resp, cid->pnm);
1117 cid->rev = SD_CID_REV(resp);
1118 cid->psn = SD_CID_PSN(resp);
1119 cid->mdt = SD_CID_MDT(resp);
1120 } else {
1121 switch(sc->csd.mmcver) {
1122 case MMC_CSD_MMCVER_1_0:
1123 case MMC_CSD_MMCVER_1_4:
1124 cid->mid = MMC_CID_MID_V1(resp);
1125 MMC_CID_PNM_V1_CPY(resp, cid->pnm);
1126 cid->rev = MMC_CID_REV_V1(resp);
1127 cid->psn = MMC_CID_PSN_V1(resp);
1128 cid->mdt = MMC_CID_MDT_V1(resp);
1129 break;
1130 case MMC_CSD_MMCVER_2_0:
1131 case MMC_CSD_MMCVER_3_1:
1132 case MMC_CSD_MMCVER_4_0:
1133 cid->mid = MMC_CID_MID_V2(resp);
1134 cid->oid = MMC_CID_OID_V2(resp);
1135 MMC_CID_PNM_V2_CPY(resp, cid->pnm);
1136 cid->psn = MMC_CID_PSN_V2(resp);
1137 break;
1138 default:
1139 printf("unknown MMC version %d\n",
1140 sc->csd.mmcver);
1141 return 1;
1142 }
1143 }
1144 return 0;
1145 }
1146
1147 void
1148 sdmmc_print_cid(struct sdmmc_cid *cid)
1149 {
1150
1151 printf("mid=0x%02x oid=0x%04x pnm=\"%s\" rev=0x%02x psn=0x%08x"
1152 " mdt=%03x\n", cid->mid, cid->oid, cid->pnm, cid->rev, cid->psn,
1153 cid->mdt);
1154 }
1155
1156 int
1157 sdmmc_mem_read_block(struct sdmmc_softc *sc, uint32_t blkno,
1158 u_char *data, size_t datalen)
1159 {
1160 struct sdmmc_command cmd;
1161 int error;
1162
1163 memset(&cmd, 0, sizeof(cmd));
1164 cmd.c_data = data;
1165 cmd.c_datalen = datalen;
1166 cmd.c_blklen = SDMMC_SECTOR_SIZE;
1167 cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ?
1168 MMC_READ_BLOCK_MULTIPLE : MMC_READ_BLOCK_SINGLE;
1169 cmd.c_arg = blkno;
1170 if (!ISSET(sc->flags, SMF_CARD_SDHC))
1171 cmd.c_arg <<= SDMMC_SECTOR_SIZE_SB;
1172 DPRINTF(("Reading block %d (%d)\n", blkno, cmd.c_arg));
1173 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1;
1174
1175 error = sdmmc_mmc_command(sc, &cmd);
1176 if (error)
1177 goto out;
1178
1179 if (!ISSET(sc->caps, SMC_CAPS_AUTO_STOP)) {
1180 if (cmd.c_opcode == MMC_READ_BLOCK_MULTIPLE) {
1181 memset(&cmd, 0, sizeof cmd);
1182 cmd.c_opcode = MMC_STOP_TRANSMISSION;
1183 cmd.c_arg = MMC_ARG_RCA(sc->rca);
1184 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B | SCF_RSP_SPI_R1B;
1185 error = sdmmc_mmc_command(sc, &cmd);
1186 if (error)
1187 goto out;
1188 }
1189 }
1190
1191 /*if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {*/
1192 do {
1193 memset(&cmd, 0, sizeof(cmd));
1194 cmd.c_opcode = MMC_SEND_STATUS;
1195 cmd.c_arg = MMC_ARG_RCA(sc->rca);
1196 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R2;
1197 error = sdmmc_mmc_command(sc, &cmd);
1198 if (error)
1199 break;
1200 /* XXX time out */
1201 } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
1202 /*}*/
1203
1204 out:
1205 return error;
1206 }
1207
1208 int
1209 sdmmc_select_card(struct sdmmc_softc *sc)
1210 {
1211 struct sdmmc_command cmd;
1212 int error;
1213
1214 /* Don't lock */
1215
1216 /* if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
1217 return EIO;*/
1218
1219 /*if (sc->sc_card == sf
1220 || (sf && sc->sc_card && sc->sc_card->rca == sf->rca)) {
1221 sc->sc_card = sf;
1222 return 0;
1223 }*/
1224
1225 memset(&cmd, 0, sizeof(cmd));
1226 cmd.c_opcode = MMC_SELECT_CARD;
1227 cmd.c_arg = (sc == NULL) ? 0 : MMC_ARG_RCA(sc->rca);
1228 cmd.c_flags = SCF_CMD_AC | ((sc == NULL) ? SCF_RSP_R0 : SCF_RSP_R1);
1229 error = sdmmc_mmc_command(sc, &cmd);
1230 /*if (error == 0 || sf == NULL)
1231 sc->sc_card = sf;*/
1232
1233 return error;
1234 }
1235
1236 /*
1237 * Set the read block length appropriately for this card, according to
1238 * the card CSD register value.
1239 */
1240 int
1241 sdmmc_mem_set_blocklen(struct sdmmc_softc *sc)
1242 {
1243 struct sdmmc_command cmd;
1244 int error;
1245
1246 /* Don't lock */
1247
1248 memset(&cmd, 0, sizeof(cmd));
1249 cmd.c_opcode = MMC_SET_BLOCKLEN;
1250 cmd.c_arg = SDMMC_SECTOR_SIZE;
1251 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1;
1252
1253 error = sdmmc_mmc_command(sc, &cmd);
1254
1255 DPRINTF(("sdmmc_mem_set_blocklen: read_bl_len=%d sector_size=%d\n",
1256 1 << sc->csd.read_bl_len, SDMMC_SECTOR_SIZE));
1257
1258 return error;
1259 }
1260
1261 int
1262 sdmmc_mem_send_scr(struct sdmmc_softc *sc, uint32_t scr[2])
1263 {
1264 struct sdmmc_command cmd;
1265 void *ptr = NULL;
1266 int datalen = 8;
1267 int error = 0;
1268
1269 ptr = alloc(datalen); //malloc(datalen, M_DEVBUF, M_NOWAIT | M_ZERO);
1270 if (ptr == NULL)
1271 goto out;
1272
1273 memset(&cmd, 0, sizeof(cmd));
1274 cmd.c_data = ptr;
1275 cmd.c_datalen = datalen;
1276 cmd.c_blklen = datalen;
1277 cmd.c_arg = 0;
1278 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1;
1279 cmd.c_opcode = SD_APP_SEND_SCR;
1280
1281 error = sdmmc_app_command(sc, sc->rca, &cmd);
1282 if (error == 0) {
1283 memcpy(scr, ptr, datalen);
1284 }
1285
1286 out:
1287 if (ptr != NULL) {
1288 dealloc(ptr, datalen);
1289 }
1290 DPRINTF(("sdmem_mem_send_scr: error = %d\n",
1291 error));
1292 if (error)
1293 return error;
1294 #ifdef SDMMC_DEBUG
1295 sdmmc_dump_data("SCR", scr, 8);
1296 #endif
1297 return error;
1298 }
1299
1300 int
1301 sdmmc_mem_decode_scr(struct sdmmc_softc *sc)
1302 {
1303 sdmmc_response resp;
1304 int ver;
1305
1306 memset(resp, 0, sizeof(resp));
1307 /*resp[0] = sc->raw_scr[1];
1308 resp[1] = sc->raw_scr[0];*/
1309 /*
1310 * Change the raw-scr received from the DMA stream to resp.
1311 */
1312 resp[0] = be32toh(sc->raw_scr[1]) >> 8; // LSW
1313 resp[1] = be32toh(sc->raw_scr[0]); // MSW
1314 resp[0] |= (resp[1] & 0xff) << 24;
1315 resp[1] >>= 8;
1316 resp[0] = htole32(resp[0]);
1317 resp[1] = htole32(resp[1]);
1318
1319 ver = SCR_STRUCTURE(resp);
1320 sc->scr.sd_spec = SCR_SD_SPEC(resp);
1321 sc->scr.bus_width = SCR_SD_BUS_WIDTHS(resp);
1322
1323 DPRINTF(("sdmmc_mem_decode_scr: spec=%d, bus width=%d\n",
1324 sc->scr.sd_spec, sc->scr.bus_width));
1325
1326 if (ver != 0) {
1327 DPRINTF(("unknown structure version: %d\n",
1328 ver));
1329 return EINVAL;
1330 }
1331 return 0;
1332 }
1333
1334 int
1335 sdmmc_set_bus_width(struct sdmmc_softc *sc, int width)
1336 {
1337 struct sdmmc_command cmd;
1338 int error;
1339
1340 if (ISSET(sc->caps, SMC_CAPS_SPI_MODE))
1341 return ENODEV;
1342
1343 memset(&cmd, 0, sizeof(cmd));
1344 cmd.c_opcode = SD_APP_SET_BUS_WIDTH;
1345 cmd.c_flags = SCF_RSP_R1 | SCF_CMD_AC;
1346
1347 switch (width) {
1348 case 1:
1349 cmd.c_arg = SD_ARG_BUS_WIDTH_1;
1350 break;
1351
1352 case 4:
1353 cmd.c_arg = SD_ARG_BUS_WIDTH_4;
1354 break;
1355
1356 default:
1357 return EINVAL;
1358 }
1359
1360 error = sdmmc_app_command(sc, sc->rca, &cmd);
1361 if (error == 0)
1362 error = sc->sdifdv->bus_width(sc->sdifdv->priv, width);
1363 return error;
1364 }
1365
1366 #if 1
1367 static int
1368 sdmmc_mem_sd_switch(struct sdmmc_softc *sc, int mode, int group,
1369 int function, void *status)
1370 {
1371 struct sdmmc_command cmd;
1372 void *ptr = NULL;
1373 int gsft, error = 0;
1374 const int statlen = 64;
1375
1376 if (sc->scr.sd_spec >= SCR_SD_SPEC_VER_1_10 &&
1377 !ISSET(sc->csd.ccc, SD_CSD_CCC_SWITCH))
1378 return EINVAL;
1379
1380 if (group <= 0 || group > 6 ||
1381 function < 0 || function > 16)
1382 return EINVAL;
1383
1384 gsft = (group - 1) << 2;
1385
1386 ptr = alloc(statlen);
1387 if (ptr == NULL)
1388 goto out;
1389
1390 memset(&cmd, 0, sizeof(cmd));
1391 cmd.c_data = ptr;
1392 cmd.c_datalen = statlen;
1393 cmd.c_blklen = statlen;
1394 cmd.c_opcode = SD_SEND_SWITCH_FUNC;
1395 cmd.c_arg =
1396 (!!mode << 31) | (function << gsft) | (0x00ffffff & ~(0xf << gsft));
1397 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1;
1398
1399 error = sdmmc_mmc_command(sc, &cmd);
1400 if (error == 0) {
1401 memcpy(status, ptr, statlen);
1402 }
1403
1404 out:
1405 if (ptr != NULL) {
1406 dealloc(ptr, statlen);
1407 }
1408 return error;
1409 }
1410 #endif
1411