sdmmc_mem.c revision 1.8 1 /* $NetBSD: sdmmc_mem.c,v 1.8 2010/09/20 09:34:47 kiyohara Exp $ */
2 /* $OpenBSD: sdmmc_mem.c,v 1.10 2009/01/09 10:55:22 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-2010 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 REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 */
45
46 /* Routines for SD/MMC memory cards. */
47
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: sdmmc_mem.c,v 1.8 2010/09/20 09:34:47 kiyohara Exp $");
50
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/systm.h>
55 #include <sys/device.h>
56
57 #include <uvm/uvm_extern.h>
58
59 #include <dev/sdmmc/sdmmcchip.h>
60 #include <dev/sdmmc/sdmmcreg.h>
61 #include <dev/sdmmc/sdmmcvar.h>
62
63 #ifdef SDMMC_DEBUG
64 #define DPRINTF(s) do { printf s; } while (/*CONSTCOND*/0)
65 #else
66 #define DPRINTF(s) do {} while (/*CONSTCOND*/0)
67 #endif
68
69 static int sdmmc_mem_send_cid(struct sdmmc_softc *, sdmmc_response *);
70 static int sdmmc_mem_send_csd(struct sdmmc_softc *, struct sdmmc_function *,
71 sdmmc_response *);
72 static int sdmmc_mem_send_scr(struct sdmmc_softc *, struct sdmmc_function *,
73 uint32_t scr[2]);
74 static int sdmmc_mem_decode_scr(struct sdmmc_softc *, struct sdmmc_function *);
75 static int sdmmc_mem_send_cxd_data(struct sdmmc_softc *, int, void *, size_t);
76 static int sdmmc_set_bus_width(struct sdmmc_function *, int);
77 static int sdmmc_mem_mmc_switch(struct sdmmc_function *, uint8_t, uint8_t,
78 uint8_t);
79 static int sdmmc_mem_spi_read_ocr(struct sdmmc_softc *, uint32_t, uint32_t *);
80 static int sdmmc_mem_single_read_block(struct sdmmc_function *, uint32_t,
81 u_char *, size_t);
82 static int sdmmc_mem_single_write_block(struct sdmmc_function *, uint32_t,
83 u_char *, size_t);
84 static int sdmmc_mem_read_block_subr(struct sdmmc_function *, uint32_t,
85 u_char *, size_t);
86 static int sdmmc_mem_write_block_subr(struct sdmmc_function *, uint32_t,
87 u_char *, size_t);
88
89 /*
90 * Initialize SD/MMC memory cards and memory in SDIO "combo" cards.
91 */
92 int
93 sdmmc_mem_enable(struct sdmmc_softc *sc)
94 {
95 uint32_t host_ocr;
96 uint32_t card_ocr;
97 uint32_t ocr = 0;
98 int error;
99
100 SDMMC_LOCK(sc);
101
102 /* Set host mode to SD "combo" card or SD memory-only. */
103 SET(sc->sc_flags, SMF_SD_MODE|SMF_MEM_MODE);
104
105 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
106 sdmmc_spi_chip_initialize(sc->sc_spi_sct, sc->sc_sch);
107
108 /* Reset memory (*must* do that before CMD55 or CMD1). */
109 sdmmc_go_idle_state(sc);
110
111 /* Check SD Ver.2 */
112 error = sdmmc_mem_send_if_cond(sc, 0x1aa, &card_ocr);
113 if (error == 0 && card_ocr == 0x1aa)
114 SET(ocr, MMC_OCR_HCS);
115
116 /*
117 * Read the SD/MMC memory OCR value by issuing CMD55 followed
118 * by ACMD41 to read the OCR value from memory-only SD cards.
119 * MMC cards will not respond to CMD55 or ACMD41 and this is
120 * how we distinguish them from SD cards.
121 */
122 mmc_mode:
123 error = sdmmc_mem_send_op_cond(sc,
124 ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ? ocr : 0, &card_ocr);
125 if (error) {
126 if (ISSET(sc->sc_flags, SMF_SD_MODE) &&
127 !ISSET(sc->sc_flags, SMF_IO_MODE)) {
128 /* Not a SD card, switch to MMC mode. */
129 DPRINTF(("%s: switch to MMC mode\n", SDMMCDEVNAME(sc)));
130 CLR(sc->sc_flags, SMF_SD_MODE);
131 goto mmc_mode;
132 }
133 if (!ISSET(sc->sc_flags, SMF_SD_MODE)) {
134 DPRINTF(("%s: couldn't read memory OCR\n",
135 SDMMCDEVNAME(sc)));
136 goto out;
137 } else {
138 /* Not a "combo" card. */
139 CLR(sc->sc_flags, SMF_MEM_MODE);
140 error = 0;
141 goto out;
142 }
143 }
144 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
145 /* get card OCR */
146 error = sdmmc_mem_spi_read_ocr(sc, ocr, &card_ocr);
147 if (error) {
148 DPRINTF(("%s: couldn't read SPI memory OCR\n",
149 SDMMCDEVNAME(sc)));
150 goto out;
151 }
152 }
153
154 /* Set the lowest voltage supported by the card and host. */
155 host_ocr = sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch);
156 error = sdmmc_set_bus_power(sc, host_ocr, card_ocr);
157 if (error) {
158 DPRINTF(("%s: couldn't supply voltage requested by card\n",
159 SDMMCDEVNAME(sc)));
160 goto out;
161 }
162 host_ocr &= card_ocr;
163 host_ocr |= ocr;
164
165 /* Send the new OCR value until all cards are ready. */
166 error = sdmmc_mem_send_op_cond(sc, host_ocr, NULL);
167 if (error) {
168 DPRINTF(("%s: couldn't send memory OCR\n", SDMMCDEVNAME(sc)));
169 goto out;
170 }
171
172 out:
173 SDMMC_UNLOCK(sc);
174
175 return error;
176 }
177
178 /*
179 * Read the CSD and CID from all cards and assign each card a unique
180 * relative card address (RCA). CMD2 is ignored by SDIO-only cards.
181 */
182 void
183 sdmmc_mem_scan(struct sdmmc_softc *sc)
184 {
185 sdmmc_response resp;
186 struct sdmmc_function *sf;
187 uint16_t next_rca;
188 int error;
189 int retry;
190
191 SDMMC_LOCK(sc);
192
193 /*
194 * CMD2 is a broadcast command understood by SD cards and MMC
195 * cards. All cards begin to respond to the command, but back
196 * off if another card drives the CMD line to a different level.
197 * Only one card will get its entire response through. That
198 * card remains silent once it has been assigned a RCA.
199 */
200 for (retry = 0; retry < 100; retry++) {
201 error = sdmmc_mem_send_cid(sc, &resp);
202 if (error) {
203 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) &&
204 error == ETIMEDOUT) {
205 /* No more cards there. */
206 break;
207 }
208 DPRINTF(("%s: couldn't read CID\n", SDMMCDEVNAME(sc)));
209 break;
210 }
211
212 /* In MMC mode, find the next available RCA. */
213 next_rca = 1;
214 if (!ISSET(sc->sc_flags, SMF_SD_MODE)) {
215 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list)
216 next_rca++;
217 }
218
219 /* Allocate a sdmmc_function structure. */
220 sf = sdmmc_function_alloc(sc);
221 sf->rca = next_rca;
222
223 /*
224 * Remember the CID returned in the CMD2 response for
225 * later decoding.
226 */
227 memcpy(sf->raw_cid, resp, sizeof(sf->raw_cid));
228
229 /*
230 * Silence the card by assigning it a unique RCA, or
231 * querying it for its RCA in the case of SD.
232 */
233 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
234 if (sdmmc_set_relative_addr(sc, sf) != 0) {
235 aprint_error_dev(sc->sc_dev,
236 "couldn't set mem RCA\n");
237 sdmmc_function_free(sf);
238 break;
239 }
240 }
241
242 /*
243 * If this is a memory-only card, the card responding
244 * first becomes an alias for SDIO function 0.
245 */
246 if (sc->sc_fn0 == NULL)
247 sc->sc_fn0 = sf;
248
249 SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
250
251 /* only one function in SPI mode */
252 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
253 break;
254 }
255
256 /*
257 * All cards are either inactive or awaiting further commands.
258 * Read the CSDs and decode the raw CID for each card.
259 */
260 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
261 error = sdmmc_mem_send_csd(sc, sf, &resp);
262 if (error) {
263 SET(sf->flags, SFF_ERROR);
264 continue;
265 }
266
267 if (sdmmc_decode_csd(sc, resp, sf) != 0 ||
268 sdmmc_decode_cid(sc, sf->raw_cid, sf) != 0) {
269 SET(sf->flags, SFF_ERROR);
270 continue;
271 }
272
273 #ifdef SDMMC_DEBUG
274 printf("%s: CID: ", SDMMCDEVNAME(sc));
275 sdmmc_print_cid(&sf->cid);
276 #endif
277 }
278
279 SDMMC_UNLOCK(sc);
280 }
281
282 int
283 sdmmc_decode_csd(struct sdmmc_softc *sc, sdmmc_response resp,
284 struct sdmmc_function *sf)
285 {
286 /* TRAN_SPEED(2:0): transfer rate exponent */
287 static const int speed_exponent[8] = {
288 100 * 1, /* 100 Kbits/s */
289 1 * 1000, /* 1 Mbits/s */
290 10 * 1000, /* 10 Mbits/s */
291 100 * 1000, /* 100 Mbits/s */
292 0,
293 0,
294 0,
295 0,
296 };
297 /* TRAN_SPEED(6:3): time mantissa */
298 static const int speed_mantissa[16] = {
299 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80,
300 };
301 struct sdmmc_csd *csd = &sf->csd;
302 int e, m;
303
304 if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
305 /*
306 * CSD version 1.0 corresponds to SD system
307 * specification version 1.0 - 1.10. (SanDisk, 3.5.3)
308 */
309 csd->csdver = SD_CSD_CSDVER(resp);
310 switch (csd->csdver) {
311 case SD_CSD_CSDVER_2_0:
312 DPRINTF(("%s: SD Ver.2.0\n", SDMMCDEVNAME(sc)));
313 SET(sf->flags, SFF_SDHC);
314 csd->capacity = SD_CSD_V2_CAPACITY(resp);
315 csd->read_bl_len = SD_CSD_V2_BL_LEN;
316 break;
317
318 case SD_CSD_CSDVER_1_0:
319 DPRINTF(("%s: SD Ver.1.0\n", SDMMCDEVNAME(sc)));
320 csd->capacity = SD_CSD_CAPACITY(resp);
321 csd->read_bl_len = SD_CSD_READ_BL_LEN(resp);
322 break;
323
324 default:
325 aprint_error_dev(sc->sc_dev,
326 "unknown SD CSD structure version 0x%x\n",
327 csd->csdver);
328 return 1;
329 }
330
331 csd->mmcver = SD_CSD_MMCVER(resp);
332 csd->write_bl_len = SD_CSD_WRITE_BL_LEN(resp);
333 csd->r2w_factor = SD_CSD_R2W_FACTOR(resp);
334 e = SD_CSD_SPEED_EXP(resp);
335 m = SD_CSD_SPEED_MANT(resp);
336 csd->tran_speed = speed_exponent[e] * speed_mantissa[m] / 10;
337 } else {
338 csd->csdver = MMC_CSD_CSDVER(resp);
339 if (csd->csdver != MMC_CSD_CSDVER_1_0 &&
340 csd->csdver != MMC_CSD_CSDVER_2_0) {
341 aprint_error_dev(sc->sc_dev,
342 "unknown MMC CSD structure version 0x%x\n",
343 csd->csdver);
344 return 1;
345 }
346
347 csd->mmcver = MMC_CSD_MMCVER(resp);
348 csd->capacity = MMC_CSD_CAPACITY(resp);
349 csd->read_bl_len = MMC_CSD_READ_BL_LEN(resp);
350 csd->write_bl_len = MMC_CSD_WRITE_BL_LEN(resp);
351 csd->r2w_factor = MMC_CSD_R2W_FACTOR(resp);
352 e = MMC_CSD_TRAN_SPEED_EXP(resp);
353 m = MMC_CSD_TRAN_SPEED_MANT(resp);
354 csd->tran_speed = speed_exponent[e] * speed_mantissa[m] / 10;
355 }
356 if ((1 << csd->read_bl_len) > SDMMC_SECTOR_SIZE)
357 csd->capacity *= (1 << csd->read_bl_len) / SDMMC_SECTOR_SIZE;
358
359 if (sc->sc_busclk > csd->tran_speed)
360 sc->sc_busclk = csd->tran_speed;
361
362 #ifdef SDMMC_DUMP_CSD
363 sdmmc_print_csd(resp, csd);
364 #endif
365
366 return 0;
367 }
368
369 int
370 sdmmc_decode_cid(struct sdmmc_softc *sc, sdmmc_response resp,
371 struct sdmmc_function *sf)
372 {
373 struct sdmmc_cid *cid = &sf->cid;
374
375 if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
376 cid->mid = SD_CID_MID(resp);
377 cid->oid = SD_CID_OID(resp);
378 SD_CID_PNM_CPY(resp, cid->pnm);
379 cid->rev = SD_CID_REV(resp);
380 cid->psn = SD_CID_PSN(resp);
381 cid->mdt = SD_CID_MDT(resp);
382 } else {
383 switch(sf->csd.mmcver) {
384 case MMC_CSD_MMCVER_1_0:
385 case MMC_CSD_MMCVER_1_4:
386 cid->mid = MMC_CID_MID_V1(resp);
387 MMC_CID_PNM_V1_CPY(resp, cid->pnm);
388 cid->rev = MMC_CID_REV_V1(resp);
389 cid->psn = MMC_CID_PSN_V1(resp);
390 cid->mdt = MMC_CID_MDT_V1(resp);
391 break;
392 case MMC_CSD_MMCVER_2_0:
393 case MMC_CSD_MMCVER_3_1:
394 case MMC_CSD_MMCVER_4_0:
395 cid->mid = MMC_CID_MID_V2(resp);
396 cid->oid = MMC_CID_OID_V2(resp);
397 MMC_CID_PNM_V2_CPY(resp, cid->pnm);
398 cid->psn = MMC_CID_PSN_V2(resp);
399 break;
400 default:
401 aprint_error_dev(sc->sc_dev, "unknown MMC version %d\n",
402 sf->csd.mmcver);
403 return 1;
404 }
405 }
406 return 0;
407 }
408
409 void
410 sdmmc_print_cid(struct sdmmc_cid *cid)
411 {
412
413 printf("mid=0x%02x oid=0x%04x pnm=\"%s\" rev=0x%02x psn=0x%08x"
414 " mdt=%03x\n", cid->mid, cid->oid, cid->pnm, cid->rev, cid->psn,
415 cid->mdt);
416 }
417
418 #ifdef SDMMC_DUMP_CSD
419 void
420 sdmmc_print_csd(sdmmc_response resp, struct sdmmc_csd *csd)
421 {
422
423 printf("csdver = %d\n", csd->csdver);
424 printf("mmcver = %d\n", csd->mmcver);
425 printf("capacity = %08x\n", csd->capacity);
426 printf("read_bl_len = %d\n", csd->read_bl_len);
427 printf("write_cl_len = %d\n", csd->write_bl_len);
428 printf("r2w_factor = %d\n", csd->r2w_factor);
429 printf("tran_speed = %d\n", csd->tran_speed);
430 }
431 #endif
432
433 /*
434 * Initialize a SD/MMC memory card.
435 */
436 int
437 sdmmc_mem_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
438 {
439 int error = 0;
440
441 SDMMC_LOCK(sc);
442
443 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
444 error = sdmmc_select_card(sc, sf);
445 if (error)
446 goto out;
447 }
448
449 if (!ISSET(sf->flags, SFF_SDHC)) {
450 error = sdmmc_mem_set_blocklen(sc, sf);
451 if (error)
452 goto out;
453 }
454
455 /* change bus width if supported */
456 if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
457 error = sdmmc_mem_send_scr(sc, sf, sf->raw_scr);
458 if (error) {
459 DPRINTF(("%s: SD_SEND_SCR send failed.\n",
460 SDMMCDEVNAME(sc)));
461 goto out;
462 }
463 error = sdmmc_mem_decode_scr(sc, sf);
464 if (error)
465 goto out;
466
467 if (ISSET(sc->sc_caps, SMC_CAPS_4BIT_MODE) &&
468 ISSET(sf->scr.bus_width, SCR_SD_BUS_WIDTHS_4BIT)) {
469 error = sdmmc_set_bus_width(sf, 4);
470 if (error) {
471 DPRINTF(("%s: can't change bus width"
472 " (%d bit)\n", SDMMCDEVNAME(sc), 4));
473 }
474 }
475 } else if (sf->csd.mmcver >= MMC_CSD_MMCVER_4_0) {
476 if (ISSET(sc->sc_caps, SMC_CAPS_8BIT_MODE)) {
477 width = 8;
478 value = EXT_CSD_BUS_WIDTH_8;
479 } else if (ISSET(sc->sc_caps, SMC_CAPS_4BIT_MODE)) {
480 width = 4;
481 value = EXT_CSD_BUS_WIDTH_4;
482 } else {
483 width = 1;
484 value = EXT_CSD_BUS_WIDTH_1;
485 }
486
487 if (width != 1) {
488 error = sdmmc_mem_mmc_switch(sf, EXT_CSD_CMD_SET_NORMAL,
489 EXT_CSD_BUS_WIDTH, value);
490 if (error == 0)
491 error = sdmmc_chip_bus_width(sc->sc_sct,
492 sc->sc_sch, width);
493 else {
494 DPRINTF(("%s: can't change bus width"
495 " (%d bit)\n", SDMMCDEVNAME(sc), width));
496 goto out;
497 }
498
499 /* XXXX: need bus test? (using by CMD14 & CMD19) */
500 }
501 }
502
503 out:
504 SDMMC_UNLOCK(sc);
505
506 return error;
507 }
508
509 /*
510 * Get or set the card's memory OCR value (SD or MMC).
511 */
512 int
513 sdmmc_mem_send_op_cond(struct sdmmc_softc *sc, uint32_t ocr, uint32_t *ocrp)
514 {
515 struct sdmmc_command cmd;
516 int error;
517 int retry;
518
519 /* Don't lock */
520
521 /*
522 * If we change the OCR value, retry the command until the OCR
523 * we receive in response has the "CARD BUSY" bit set, meaning
524 * that all cards are ready for identification.
525 */
526 for (retry = 0; retry < 100; retry++) {
527 memset(&cmd, 0, sizeof(cmd));
528 cmd.c_arg = !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ?
529 ocr : (ocr & MMC_OCR_HCS);
530 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R3 | SCF_RSP_SPI_R1;
531
532 if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
533 cmd.c_opcode = SD_APP_OP_COND;
534 error = sdmmc_app_command(sc, NULL, &cmd);
535 } else {
536 cmd.c_opcode = MMC_SEND_OP_COND;
537 error = sdmmc_mmc_command(sc, &cmd);
538 }
539 if (error)
540 break;
541
542 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
543 if (!ISSET(MMC_SPI_R1(cmd.c_resp), R1_SPI_IDLE))
544 break;
545 } else {
546 if (ISSET(MMC_R3(cmd.c_resp), MMC_OCR_MEM_READY) ||
547 ocr == 0)
548 break;
549 }
550
551 error = ETIMEDOUT;
552 sdmmc_delay(10000);
553 }
554 if (error == 0 &&
555 ocrp != NULL &&
556 !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
557 *ocrp = MMC_R3(cmd.c_resp);
558 DPRINTF(("%s: sdmmc_mem_send_op_cond: error=%d, ocr=%#x\n",
559 SDMMCDEVNAME(sc), error, MMC_R3(cmd.c_resp)));
560 return error;
561 }
562
563 int
564 sdmmc_mem_send_if_cond(struct sdmmc_softc *sc, uint32_t ocr, uint32_t *ocrp)
565 {
566 struct sdmmc_command cmd;
567 int error;
568
569 /* Don't lock */
570
571 memset(&cmd, 0, sizeof(cmd));
572 cmd.c_arg = ocr;
573 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R7 | SCF_RSP_SPI_R7;
574 cmd.c_opcode = SD_SEND_IF_COND;
575
576 error = sdmmc_mmc_command(sc, &cmd);
577 if (error == 0 && ocrp != NULL) {
578 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
579 *ocrp = MMC_SPI_R7(cmd.c_resp);
580 } else {
581 *ocrp = MMC_R7(cmd.c_resp);
582 }
583 DPRINTF(("%s: sdmmc_mem_send_if_cond: error=%d, ocr=%#x\n",
584 SDMMCDEVNAME(sc), error, *ocrp));
585 }
586 return error;
587 }
588
589 /*
590 * Set the read block length appropriately for this card, according to
591 * the card CSD register value.
592 */
593 int
594 sdmmc_mem_set_blocklen(struct sdmmc_softc *sc, struct sdmmc_function *sf)
595 {
596 struct sdmmc_command cmd;
597 int error;
598
599 /* Don't lock */
600
601 memset(&cmd, 0, sizeof(cmd));
602 cmd.c_opcode = MMC_SET_BLOCKLEN;
603 cmd.c_arg = SDMMC_SECTOR_SIZE;
604 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1;
605
606 error = sdmmc_mmc_command(sc, &cmd);
607
608 DPRINTF(("%s: sdmmc_mem_set_blocklen: read_bl_len=%d sector_size=%d\n",
609 SDMMCDEVNAME(sc), 1 << sf->csd.read_bl_len, SDMMC_SECTOR_SIZE));
610
611 return error;
612 }
613
614 static int
615 sdmmc_mem_send_cid(struct sdmmc_softc *sc, sdmmc_response *resp)
616 {
617 struct sdmmc_command cmd;
618 int error;
619
620 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
621 memset(&cmd, 0, sizeof cmd);
622 cmd.c_opcode = MMC_ALL_SEND_CID;
623 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R2;
624
625 error = sdmmc_mmc_command(sc, &cmd);
626 } else {
627 error = sdmmc_mem_send_cxd_data(sc, MMC_SEND_CID, &cmd.c_resp,
628 sizeof(cmd.c_resp));
629 }
630
631 #ifdef SDMMC_DEBUG
632 sdmmc_dump_data("CID", cmd.c_resp, sizeof(cmd.c_resp));
633 #endif
634 if (error == 0 && resp != NULL)
635 memcpy(resp, &cmd.c_resp, sizeof(*resp));
636 return error;
637 }
638
639 static int
640 sdmmc_mem_send_csd(struct sdmmc_softc *sc, struct sdmmc_function *sf,
641 sdmmc_response *resp)
642 {
643 struct sdmmc_command cmd;
644 int error;
645
646 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
647 memset(&cmd, 0, sizeof cmd);
648 cmd.c_opcode = MMC_SEND_CSD;
649 cmd.c_arg = MMC_ARG_RCA(sf->rca);
650 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R2;
651
652 error = sdmmc_mmc_command(sc, &cmd);
653 } else {
654 error = sdmmc_mem_send_cxd_data(sc, MMC_SEND_CSD, &cmd.c_resp,
655 sizeof(cmd.c_resp));
656 }
657
658 #ifdef SDMMC_DEBUG
659 sdmmc_dump_data("CSD", cmd.c_resp, sizeof(cmd.c_resp));
660 #endif
661 if (error == 0 && resp != NULL)
662 memcpy(resp, &cmd.c_resp, sizeof(*resp));
663 return error;
664 }
665
666 static int
667 sdmmc_mem_send_scr(struct sdmmc_softc *sc, struct sdmmc_function *sf,
668 uint32_t scr[2])
669 {
670 struct sdmmc_command cmd;
671 bus_dma_segment_t ds[1];
672 void *ptr = NULL;
673 int datalen = 8;
674 int rseg;
675 int error = 0;
676
677 /* Don't lock */
678
679 if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
680 error = bus_dmamem_alloc(sc->sc_dmat, datalen, PAGE_SIZE, 0,
681 ds, 1, &rseg, BUS_DMA_NOWAIT);
682 if (error)
683 goto out;
684 error = bus_dmamem_map(sc->sc_dmat, ds, 1, datalen, &ptr,
685 BUS_DMA_NOWAIT);
686 if (error)
687 goto dmamem_free;
688 error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, ptr, datalen,
689 NULL, BUS_DMA_NOWAIT|BUS_DMA_STREAMING|BUS_DMA_READ);
690 if (error)
691 goto dmamem_unmap;
692
693 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
694 BUS_DMASYNC_PREREAD);
695 } else {
696 ptr = malloc(datalen, M_DEVBUF, M_NOWAIT | M_ZERO);
697 if (ptr == NULL)
698 goto out;
699 }
700
701 memset(&cmd, 0, sizeof(cmd));
702 cmd.c_data = ptr;
703 cmd.c_datalen = datalen;
704 cmd.c_blklen = datalen;
705 cmd.c_arg = 0;
706 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1;
707 cmd.c_opcode = SD_APP_SEND_SCR;
708 if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
709 cmd.c_dmamap = sc->sc_dmap;
710
711 error = sdmmc_app_command(sc, sf, &cmd);
712 if (error == 0) {
713 if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
714 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
715 BUS_DMASYNC_POSTREAD);
716 }
717 memcpy(scr, ptr, datalen);
718 }
719
720 out:
721 if (ptr != NULL) {
722 if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
723 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
724 dmamem_unmap:
725 bus_dmamem_unmap(sc->sc_dmat, ptr, datalen);
726 dmamem_free:
727 bus_dmamem_free(sc->sc_dmat, ds, rseg);
728 } else {
729 free(ptr, M_DEVBUF);
730 }
731 }
732 DPRINTF(("%s: sdmem_mem_send_scr: error = %d\n", SDMMCDEVNAME(sc),
733 error));
734 if (error)
735 return error;
736 #ifdef SDMMC_DEBUG
737 sdmmc_dump_data("SCR", scr, 8);
738 #endif
739 return error;
740 }
741
742 static int
743 sdmmc_mem_decode_scr(struct sdmmc_softc *sc, struct sdmmc_function *sf)
744 {
745 sdmmc_response resp;
746 int ver;
747
748 memset(resp, 0, sizeof(resp));
749 /*
750 * Change the raw-scr received from the DMA stream to resp.
751 */
752 resp[0] = be32toh(sf->raw_scr[1]);
753 resp[1] = be32toh(sf->raw_scr[0]) >> 8;
754
755 ver = SCR_STRUCTURE(resp);
756 sf->scr.sd_spec = SCR_SD_SPEC(resp);
757 sf->scr.bus_width = SCR_SD_BUS_WIDTHS(resp);
758
759 DPRINTF(("%s: sdmmc_mem_decode_scr: spec=%d, bus width=%d\n",
760 SDMMCDEVNAME(sc), sf->scr.sd_spec, sf->scr.bus_width));
761
762 if (ver != 0) {
763 DPRINTF(("%s: unknown structure version: %d\n",
764 SDMMCDEVNAME(sc), ver));
765 return EINVAL;
766 }
767 return 0;
768 }
769
770 static int
771 sdmmc_mem_send_cxd_data(struct sdmmc_softc *sc, int opcode, void *data,
772 size_t datalen)
773 {
774 struct sdmmc_command cmd;
775 bus_dma_segment_t ds[1];
776 void *ptr = NULL;
777 int rseg;
778 int error = 0;
779
780 if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
781 error = bus_dmamem_alloc(sc->sc_dmat, datalen, PAGE_SIZE, 0, ds,
782 1, &rseg, BUS_DMA_NOWAIT);
783 if (error)
784 goto out;
785 error = bus_dmamem_map(sc->sc_dmat, ds, 1, datalen, &ptr,
786 BUS_DMA_NOWAIT);
787 if (error)
788 goto dmamem_free;
789 error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, ptr, datalen,
790 NULL, BUS_DMA_NOWAIT|BUS_DMA_STREAMING|BUS_DMA_READ);
791 if (error)
792 goto dmamem_unmap;
793
794 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
795 BUS_DMASYNC_PREREAD);
796 } else {
797 ptr = malloc(datalen, M_DEVBUF, M_NOWAIT | M_ZERO);
798 if (ptr == NULL)
799 goto out;
800 }
801
802 memset(&cmd, 0, sizeof(cmd));
803 cmd.c_data = ptr;
804 cmd.c_datalen = datalen;
805 cmd.c_blklen = datalen;
806 cmd.c_opcode = opcode;
807 cmd.c_arg = 0;
808 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_SPI_R1;
809 if (opcode == MMC_SEND_EXT_CSD)
810 SET(cmd.c_flags, SCF_RSP_R1);
811 else
812 SET(cmd.c_flags, SCF_RSP_R2);
813 if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
814 cmd.c_dmamap = sc->sc_dmap;
815
816 error = sdmmc_mmc_command(sc, &cmd);
817 if (error == 0) {
818 if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
819 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
820 BUS_DMASYNC_POSTREAD);
821 }
822 memcpy(data, ptr, datalen);
823 }
824
825 out:
826 if (ptr != NULL) {
827 if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
828 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
829 dmamem_unmap:
830 bus_dmamem_unmap(sc->sc_dmat, ptr, datalen);
831 dmamem_free:
832 bus_dmamem_free(sc->sc_dmat, ds, rseg);
833 } else {
834 free(ptr, M_DEVBUF);
835 }
836 }
837 return error;
838 }
839
840 int
841 sdmmc_mem_send_extcsd(struct sdmmc_softc *sc)
842 {
843 char buf[512];
844 int error;
845
846 error = sdmmc_mem_send_cxd_data(sc, MMC_SEND_EXT_CSD, buf, 512);
847
848 /*XXX*/
849
850 return error;
851 }
852
853 static int
854 sdmmc_set_bus_width(struct sdmmc_function *sf, int width)
855 {
856 struct sdmmc_softc *sc = sf->sc;
857 struct sdmmc_command cmd;
858 int error;
859
860 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
861 return ENODEV;
862
863 memset(&cmd, 0, sizeof(cmd));
864 cmd.c_opcode = SD_APP_SET_BUS_WIDTH;
865 cmd.c_flags = SCF_RSP_R1 | SCF_CMD_AC;
866
867 switch (width) {
868 case 1:
869 cmd.c_arg = SD_ARG_BUS_WIDTH_1;
870 break;
871
872 case 4:
873 cmd.c_arg = SD_ARG_BUS_WIDTH_4;
874 break;
875
876 default:
877 return EINVAL;
878 }
879
880 error = sdmmc_app_command(sc, sf, &cmd);
881 if (error == 0)
882 error = sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch, width);
883 return error;
884 }
885
886 static int
887 sdmmc_mem_mmc_switch(struct sdmmc_function *sf, uint8_t set, uint8_t index,
888 uint8_t value)
889 {
890 struct sdmmc_softc *sc = sf->sc;
891 struct sdmmc_command cmd;
892
893 memset(&cmd, 0, sizeof(cmd));
894 cmd.c_opcode = MMC_SWITCH;
895 cmd.c_arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
896 (index << 16) | (value << 8) | set;
897 cmd.c_flags = SCF_RSP_SPI_R1B | SCF_RSP_R1B | SCF_CMD_AC;
898
899 return sdmmc_mmc_command(sc, &cmd);
900 }
901
902 /*
903 * SPI mode function
904 */
905 static int
906 sdmmc_mem_spi_read_ocr(struct sdmmc_softc *sc, uint32_t hcs, uint32_t *card_ocr)
907 {
908 struct sdmmc_command cmd;
909 int error;
910
911 memset(&cmd, 0, sizeof(cmd));
912 cmd.c_opcode = MMC_READ_OCR;
913 cmd.c_arg = hcs ? MMC_OCR_HCS : 0;
914 cmd.c_flags = SCF_RSP_SPI_R3;
915
916 error = sdmmc_mmc_command(sc, &cmd);
917 if (error == 0 && card_ocr != NULL)
918 *card_ocr = cmd.c_resp[1];
919 DPRINTF(("%s: sdmmc_mem_spi_read_ocr: error=%d, ocr=%#x\n",
920 SDMMCDEVNAME(sc), error, cmd.c_resp[1]));
921 return error;
922 }
923
924 /*
925 * read/write function
926 */
927 /* read */
928 static int
929 sdmmc_mem_single_read_block(struct sdmmc_function *sf, uint32_t blkno,
930 u_char *data, size_t datalen)
931 {
932 int error = 0;
933 int i;
934
935 KASSERT((datalen % SDMMC_SECTOR_SIZE) == 0);
936
937 for (i = 0; i < datalen / SDMMC_SECTOR_SIZE; i++) {
938 error = sdmmc_mem_read_block_subr(sf, blkno + i,
939 data + i * SDMMC_SECTOR_SIZE, SDMMC_SECTOR_SIZE);
940 if (error)
941 break;
942 }
943 return error;
944 }
945
946 static int
947 sdmmc_mem_read_block_subr(struct sdmmc_function *sf, uint32_t blkno,
948 u_char *data, size_t datalen)
949 {
950 struct sdmmc_softc *sc = sf->sc;
951 struct sdmmc_command cmd;
952 int error;
953
954 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
955 error = sdmmc_select_card(sc, sf);
956 if (error)
957 goto out;
958 }
959
960 memset(&cmd, 0, sizeof(cmd));
961 cmd.c_data = data;
962 cmd.c_datalen = datalen;
963 cmd.c_blklen = SDMMC_SECTOR_SIZE;
964 cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ?
965 MMC_READ_BLOCK_MULTIPLE : MMC_READ_BLOCK_SINGLE;
966 cmd.c_arg = blkno;
967 if (!ISSET(sf->flags, SFF_SDHC))
968 cmd.c_arg <<= SDMMC_SECTOR_SIZE_SB;
969 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1;
970 if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
971 cmd.c_dmamap = sc->sc_dmap;
972
973 error = sdmmc_mmc_command(sc, &cmd);
974 if (error)
975 goto out;
976
977 if (!ISSET(sc->sc_caps, SMC_CAPS_AUTO_STOP)) {
978 if (cmd.c_opcode == MMC_READ_BLOCK_MULTIPLE) {
979 memset(&cmd, 0, sizeof cmd);
980 cmd.c_opcode = MMC_STOP_TRANSMISSION;
981 cmd.c_arg = MMC_ARG_RCA(sf->rca);
982 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B | SCF_RSP_SPI_R1B;
983 error = sdmmc_mmc_command(sc, &cmd);
984 if (error)
985 goto out;
986 }
987 }
988
989 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
990 do {
991 memset(&cmd, 0, sizeof(cmd));
992 cmd.c_opcode = MMC_SEND_STATUS;
993 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
994 cmd.c_arg = MMC_ARG_RCA(sf->rca);
995 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R2;
996 error = sdmmc_mmc_command(sc, &cmd);
997 if (error)
998 break;
999 /* XXX time out */
1000 } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
1001 }
1002
1003 out:
1004 return error;
1005 }
1006
1007 int
1008 sdmmc_mem_read_block(struct sdmmc_function *sf, uint32_t blkno, u_char *data,
1009 size_t datalen)
1010 {
1011 struct sdmmc_softc *sc = sf->sc;
1012 int error;
1013
1014 SDMMC_LOCK(sc);
1015
1016 if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) {
1017 error = sdmmc_mem_single_read_block(sf, blkno, data, datalen);
1018 goto out;
1019 }
1020
1021 if (!ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1022 error = sdmmc_mem_read_block_subr(sf, blkno, data, datalen);
1023 goto out;
1024 }
1025
1026 /* DMA transfer */
1027 error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, data, datalen, NULL,
1028 BUS_DMA_NOWAIT|BUS_DMA_READ);
1029 if (error)
1030 goto out;
1031
1032 #ifdef SDMMC_DEBUG
1033 for (int i = 0; i < sc->sc_dmap->dm_nsegs; i++) {
1034 printf("seg#%d: addr=%#lx, size=%#lx\n", i,
1035 (u_long)sc->sc_dmap->dm_segs[i].ds_addr,
1036 (u_long)sc->sc_dmap->dm_segs[i].ds_len);
1037 }
1038 #endif
1039
1040 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1041 BUS_DMASYNC_PREREAD);
1042
1043 error = sdmmc_mem_read_block_subr(sf, blkno, data, datalen);
1044 if (error)
1045 goto unload;
1046
1047 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1048 BUS_DMASYNC_POSTREAD);
1049 unload:
1050 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
1051
1052 out:
1053 SDMMC_UNLOCK(sc);
1054
1055 return error;
1056 }
1057
1058 /* write */
1059 static int
1060 sdmmc_mem_single_write_block(struct sdmmc_function *sf, uint32_t blkno,
1061 u_char *data, size_t datalen)
1062 {
1063 int error = 0;
1064 int i;
1065
1066 KASSERT((datalen % SDMMC_SECTOR_SIZE) == 0);
1067
1068 for (i = 0; i < datalen / SDMMC_SECTOR_SIZE; i++) {
1069 error = sdmmc_mem_write_block_subr(sf, blkno + i,
1070 data + i * SDMMC_SECTOR_SIZE, SDMMC_SECTOR_SIZE);
1071 if (error)
1072 break;
1073 }
1074 return error;
1075 }
1076
1077 static int
1078 sdmmc_mem_write_block_subr(struct sdmmc_function *sf, uint32_t blkno,
1079 u_char *data, size_t datalen)
1080 {
1081 struct sdmmc_softc *sc = sf->sc;
1082 struct sdmmc_command cmd;
1083 int error;
1084
1085 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
1086 error = sdmmc_select_card(sc, sf);
1087 if (error)
1088 goto out;
1089 }
1090
1091 memset(&cmd, 0, sizeof(cmd));
1092 cmd.c_data = data;
1093 cmd.c_datalen = datalen;
1094 cmd.c_blklen = SDMMC_SECTOR_SIZE;
1095 cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ?
1096 MMC_WRITE_BLOCK_MULTIPLE : MMC_WRITE_BLOCK_SINGLE;
1097 cmd.c_arg = blkno;
1098 if (!ISSET(sf->flags, SFF_SDHC))
1099 cmd.c_arg <<= SDMMC_SECTOR_SIZE_SB;
1100 cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R1;
1101 if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
1102 cmd.c_dmamap = sc->sc_dmap;
1103
1104 error = sdmmc_mmc_command(sc, &cmd);
1105 if (error)
1106 goto out;
1107
1108 if (!ISSET(sc->sc_caps, SMC_CAPS_AUTO_STOP)) {
1109 if (cmd.c_opcode == MMC_WRITE_BLOCK_MULTIPLE) {
1110 memset(&cmd, 0, sizeof(cmd));
1111 cmd.c_opcode = MMC_STOP_TRANSMISSION;
1112 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B | SCF_RSP_SPI_R1B;
1113 error = sdmmc_mmc_command(sc, &cmd);
1114 if (error)
1115 goto out;
1116 }
1117 }
1118
1119 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
1120 do {
1121 memset(&cmd, 0, sizeof(cmd));
1122 cmd.c_opcode = MMC_SEND_STATUS;
1123 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
1124 cmd.c_arg = MMC_ARG_RCA(sf->rca);
1125 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R2;
1126 error = sdmmc_mmc_command(sc, &cmd);
1127 if (error)
1128 break;
1129 /* XXX time out */
1130 } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
1131 }
1132
1133 out:
1134 return error;
1135 }
1136
1137 int
1138 sdmmc_mem_write_block(struct sdmmc_function *sf, uint32_t blkno, u_char *data,
1139 size_t datalen)
1140 {
1141 struct sdmmc_softc *sc = sf->sc;
1142 int error;
1143
1144 SDMMC_LOCK(sc);
1145
1146 if (sdmmc_chip_write_protect(sc->sc_sct, sc->sc_sch)) {
1147 aprint_normal_dev(sc->sc_dev, "write-protected\n");
1148 error = EIO;
1149 goto out;
1150 }
1151
1152 if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) {
1153 error = sdmmc_mem_single_write_block(sf, blkno, data, datalen);
1154 goto out;
1155 }
1156
1157 if (!ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1158 error = sdmmc_mem_write_block_subr(sf, blkno, data, datalen);
1159 goto out;
1160 }
1161
1162 /* DMA transfer */
1163 error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, data, datalen, NULL,
1164 BUS_DMA_NOWAIT|BUS_DMA_WRITE);
1165 if (error)
1166 goto out;
1167
1168 #ifdef SDMMC_DEBUG
1169 for (int i = 0; i < sc->sc_dmap->dm_nsegs; i++) {
1170 printf("seg#%d: addr=%#lx, size=%#lx\n", i,
1171 (u_long)sc->sc_dmap->dm_segs[i].ds_addr,
1172 (u_long)sc->sc_dmap->dm_segs[i].ds_len);
1173 }
1174 #endif
1175
1176 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1177 BUS_DMASYNC_PREWRITE);
1178
1179 error = sdmmc_mem_write_block_subr(sf, blkno, data, datalen);
1180 if (error)
1181 goto unload;
1182
1183 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1184 BUS_DMASYNC_POSTWRITE);
1185 unload:
1186 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
1187
1188 out:
1189 SDMMC_UNLOCK(sc);
1190
1191 return error;
1192 }
1193