sdmmc_mem.c revision 1.7 1 /* $NetBSD: sdmmc_mem.c,v 1.7 2010/09/20 09:30:20 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.7 2010/09/20 09:30:20 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 resp[0] = sf->raw_scr[1];
750 resp[1] = sf->raw_scr[0];
751
752 ver = SCR_STRUCTURE(resp);
753 sf->scr.sd_spec = SCR_SD_SPEC(resp);
754 sf->scr.bus_width = SCR_SD_BUS_WIDTHS(resp);
755
756 DPRINTF(("%s: sdmmc_mem_decode_scr: spec=%d, bus width=%d\n",
757 SDMMCDEVNAME(sc), sf->scr.sd_spec, sf->scr.bus_width));
758
759 if (ver != 0) {
760 DPRINTF(("%s: unknown structure version: %d\n",
761 SDMMCDEVNAME(sc), ver));
762 return EINVAL;
763 }
764 return 0;
765 }
766
767 static int
768 sdmmc_mem_send_cxd_data(struct sdmmc_softc *sc, int opcode, void *data,
769 size_t datalen)
770 {
771 struct sdmmc_command cmd;
772 bus_dma_segment_t ds[1];
773 void *ptr = NULL;
774 int rseg;
775 int error = 0;
776
777 if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
778 error = bus_dmamem_alloc(sc->sc_dmat, datalen, PAGE_SIZE, 0, ds,
779 1, &rseg, BUS_DMA_NOWAIT);
780 if (error)
781 goto out;
782 error = bus_dmamem_map(sc->sc_dmat, ds, 1, datalen, &ptr,
783 BUS_DMA_NOWAIT);
784 if (error)
785 goto dmamem_free;
786 error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, ptr, datalen,
787 NULL, BUS_DMA_NOWAIT|BUS_DMA_STREAMING|BUS_DMA_READ);
788 if (error)
789 goto dmamem_unmap;
790
791 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
792 BUS_DMASYNC_PREREAD);
793 } else {
794 ptr = malloc(datalen, M_DEVBUF, M_NOWAIT | M_ZERO);
795 if (ptr == NULL)
796 goto out;
797 }
798
799 memset(&cmd, 0, sizeof(cmd));
800 cmd.c_data = ptr;
801 cmd.c_datalen = datalen;
802 cmd.c_blklen = datalen;
803 cmd.c_opcode = opcode;
804 cmd.c_arg = 0;
805 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_SPI_R1;
806 if (opcode == MMC_SEND_EXT_CSD)
807 SET(cmd.c_flags, SCF_RSP_R1);
808 else
809 SET(cmd.c_flags, SCF_RSP_R2);
810 if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
811 cmd.c_dmamap = sc->sc_dmap;
812
813 error = sdmmc_mmc_command(sc, &cmd);
814 if (error == 0) {
815 if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
816 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
817 BUS_DMASYNC_POSTREAD);
818 }
819 memcpy(data, ptr, datalen);
820 }
821
822 out:
823 if (ptr != NULL) {
824 if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
825 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
826 dmamem_unmap:
827 bus_dmamem_unmap(sc->sc_dmat, ptr, datalen);
828 dmamem_free:
829 bus_dmamem_free(sc->sc_dmat, ds, rseg);
830 } else {
831 free(ptr, M_DEVBUF);
832 }
833 }
834 return error;
835 }
836
837 int
838 sdmmc_mem_send_extcsd(struct sdmmc_softc *sc)
839 {
840 char buf[512];
841 int error;
842
843 error = sdmmc_mem_send_cxd_data(sc, MMC_SEND_EXT_CSD, buf, 512);
844
845 /*XXX*/
846
847 return error;
848 }
849
850 static int
851 sdmmc_set_bus_width(struct sdmmc_function *sf, int width)
852 {
853 struct sdmmc_softc *sc = sf->sc;
854 struct sdmmc_command cmd;
855 int error;
856
857 if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
858 return ENODEV;
859
860 memset(&cmd, 0, sizeof(cmd));
861 cmd.c_opcode = SD_APP_SET_BUS_WIDTH;
862 cmd.c_flags = SCF_RSP_R1 | SCF_CMD_AC;
863
864 switch (width) {
865 case 1:
866 cmd.c_arg = SD_ARG_BUS_WIDTH_1;
867 break;
868
869 case 4:
870 cmd.c_arg = SD_ARG_BUS_WIDTH_4;
871 break;
872
873 default:
874 return EINVAL;
875 }
876
877 error = sdmmc_app_command(sc, sf, &cmd);
878 if (error == 0)
879 error = sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch, width);
880 return error;
881 }
882
883 static int
884 sdmmc_mem_mmc_switch(struct sdmmc_function *sf, uint8_t set, uint8_t index,
885 uint8_t value)
886 {
887 struct sdmmc_softc *sc = sf->sc;
888 struct sdmmc_command cmd;
889
890 memset(&cmd, 0, sizeof(cmd));
891 cmd.c_opcode = MMC_SWITCH;
892 cmd.c_arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
893 (index << 16) | (value << 8) | set;
894 cmd.c_flags = SCF_RSP_SPI_R1B | SCF_RSP_R1B | SCF_CMD_AC;
895
896 return sdmmc_mmc_command(sc, &cmd);
897 }
898
899 /*
900 * SPI mode function
901 */
902 static int
903 sdmmc_mem_spi_read_ocr(struct sdmmc_softc *sc, uint32_t hcs, uint32_t *card_ocr)
904 {
905 struct sdmmc_command cmd;
906 int error;
907
908 memset(&cmd, 0, sizeof(cmd));
909 cmd.c_opcode = MMC_READ_OCR;
910 cmd.c_arg = hcs ? MMC_OCR_HCS : 0;
911 cmd.c_flags = SCF_RSP_SPI_R3;
912
913 error = sdmmc_mmc_command(sc, &cmd);
914 if (error == 0 && card_ocr != NULL)
915 *card_ocr = cmd.c_resp[1];
916 DPRINTF(("%s: sdmmc_mem_spi_read_ocr: error=%d, ocr=%#x\n",
917 SDMMCDEVNAME(sc), error, cmd.c_resp[1]));
918 return error;
919 }
920
921 /*
922 * read/write function
923 */
924 /* read */
925 static int
926 sdmmc_mem_single_read_block(struct sdmmc_function *sf, uint32_t blkno,
927 u_char *data, size_t datalen)
928 {
929 int error = 0;
930 int i;
931
932 KASSERT((datalen % SDMMC_SECTOR_SIZE) == 0);
933
934 for (i = 0; i < datalen / SDMMC_SECTOR_SIZE; i++) {
935 error = sdmmc_mem_read_block_subr(sf, blkno + i,
936 data + i * SDMMC_SECTOR_SIZE, SDMMC_SECTOR_SIZE);
937 if (error)
938 break;
939 }
940 return error;
941 }
942
943 static int
944 sdmmc_mem_read_block_subr(struct sdmmc_function *sf, uint32_t blkno,
945 u_char *data, size_t datalen)
946 {
947 struct sdmmc_softc *sc = sf->sc;
948 struct sdmmc_command cmd;
949 int error;
950
951 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
952 error = sdmmc_select_card(sc, sf);
953 if (error)
954 goto out;
955 }
956
957 memset(&cmd, 0, sizeof(cmd));
958 cmd.c_data = data;
959 cmd.c_datalen = datalen;
960 cmd.c_blklen = SDMMC_SECTOR_SIZE;
961 cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ?
962 MMC_READ_BLOCK_MULTIPLE : MMC_READ_BLOCK_SINGLE;
963 cmd.c_arg = blkno;
964 if (!ISSET(sf->flags, SFF_SDHC))
965 cmd.c_arg <<= SDMMC_SECTOR_SIZE_SB;
966 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1;
967 if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
968 cmd.c_dmamap = sc->sc_dmap;
969
970 error = sdmmc_mmc_command(sc, &cmd);
971 if (error)
972 goto out;
973
974 if (!ISSET(sc->sc_caps, SMC_CAPS_AUTO_STOP)) {
975 if (cmd.c_opcode == MMC_READ_BLOCK_MULTIPLE) {
976 memset(&cmd, 0, sizeof cmd);
977 cmd.c_opcode = MMC_STOP_TRANSMISSION;
978 cmd.c_arg = MMC_ARG_RCA(sf->rca);
979 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B | SCF_RSP_SPI_R1B;
980 error = sdmmc_mmc_command(sc, &cmd);
981 if (error)
982 goto out;
983 }
984 }
985
986 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
987 do {
988 memset(&cmd, 0, sizeof(cmd));
989 cmd.c_opcode = MMC_SEND_STATUS;
990 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
991 cmd.c_arg = MMC_ARG_RCA(sf->rca);
992 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R2;
993 error = sdmmc_mmc_command(sc, &cmd);
994 if (error)
995 break;
996 /* XXX time out */
997 } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
998 }
999
1000 out:
1001 return error;
1002 }
1003
1004 int
1005 sdmmc_mem_read_block(struct sdmmc_function *sf, uint32_t blkno, u_char *data,
1006 size_t datalen)
1007 {
1008 struct sdmmc_softc *sc = sf->sc;
1009 int error;
1010
1011 SDMMC_LOCK(sc);
1012
1013 if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) {
1014 error = sdmmc_mem_single_read_block(sf, blkno, data, datalen);
1015 goto out;
1016 }
1017
1018 if (!ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1019 error = sdmmc_mem_read_block_subr(sf, blkno, data, datalen);
1020 goto out;
1021 }
1022
1023 /* DMA transfer */
1024 error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, data, datalen, NULL,
1025 BUS_DMA_NOWAIT|BUS_DMA_READ);
1026 if (error)
1027 goto out;
1028
1029 #ifdef SDMMC_DEBUG
1030 for (int i = 0; i < sc->sc_dmap->dm_nsegs; i++) {
1031 printf("seg#%d: addr=%#lx, size=%#lx\n", i,
1032 (u_long)sc->sc_dmap->dm_segs[i].ds_addr,
1033 (u_long)sc->sc_dmap->dm_segs[i].ds_len);
1034 }
1035 #endif
1036
1037 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1038 BUS_DMASYNC_PREREAD);
1039
1040 error = sdmmc_mem_read_block_subr(sf, blkno, data, datalen);
1041 if (error)
1042 goto unload;
1043
1044 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1045 BUS_DMASYNC_POSTREAD);
1046 unload:
1047 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
1048
1049 out:
1050 SDMMC_UNLOCK(sc);
1051
1052 return error;
1053 }
1054
1055 /* write */
1056 static int
1057 sdmmc_mem_single_write_block(struct sdmmc_function *sf, uint32_t blkno,
1058 u_char *data, size_t datalen)
1059 {
1060 int error = 0;
1061 int i;
1062
1063 KASSERT((datalen % SDMMC_SECTOR_SIZE) == 0);
1064
1065 for (i = 0; i < datalen / SDMMC_SECTOR_SIZE; i++) {
1066 error = sdmmc_mem_write_block_subr(sf, blkno + i,
1067 data + i * SDMMC_SECTOR_SIZE, SDMMC_SECTOR_SIZE);
1068 if (error)
1069 break;
1070 }
1071 return error;
1072 }
1073
1074 static int
1075 sdmmc_mem_write_block_subr(struct sdmmc_function *sf, uint32_t blkno,
1076 u_char *data, size_t datalen)
1077 {
1078 struct sdmmc_softc *sc = sf->sc;
1079 struct sdmmc_command cmd;
1080 int error;
1081
1082 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
1083 error = sdmmc_select_card(sc, sf);
1084 if (error)
1085 goto out;
1086 }
1087
1088 memset(&cmd, 0, sizeof(cmd));
1089 cmd.c_data = data;
1090 cmd.c_datalen = datalen;
1091 cmd.c_blklen = SDMMC_SECTOR_SIZE;
1092 cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ?
1093 MMC_WRITE_BLOCK_MULTIPLE : MMC_WRITE_BLOCK_SINGLE;
1094 cmd.c_arg = blkno;
1095 if (!ISSET(sf->flags, SFF_SDHC))
1096 cmd.c_arg <<= SDMMC_SECTOR_SIZE_SB;
1097 cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R1;
1098 if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
1099 cmd.c_dmamap = sc->sc_dmap;
1100
1101 error = sdmmc_mmc_command(sc, &cmd);
1102 if (error)
1103 goto out;
1104
1105 if (!ISSET(sc->sc_caps, SMC_CAPS_AUTO_STOP)) {
1106 if (cmd.c_opcode == MMC_WRITE_BLOCK_MULTIPLE) {
1107 memset(&cmd, 0, sizeof(cmd));
1108 cmd.c_opcode = MMC_STOP_TRANSMISSION;
1109 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B | SCF_RSP_SPI_R1B;
1110 error = sdmmc_mmc_command(sc, &cmd);
1111 if (error)
1112 goto out;
1113 }
1114 }
1115
1116 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
1117 do {
1118 memset(&cmd, 0, sizeof(cmd));
1119 cmd.c_opcode = MMC_SEND_STATUS;
1120 if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
1121 cmd.c_arg = MMC_ARG_RCA(sf->rca);
1122 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R2;
1123 error = sdmmc_mmc_command(sc, &cmd);
1124 if (error)
1125 break;
1126 /* XXX time out */
1127 } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
1128 }
1129
1130 out:
1131 return error;
1132 }
1133
1134 int
1135 sdmmc_mem_write_block(struct sdmmc_function *sf, uint32_t blkno, u_char *data,
1136 size_t datalen)
1137 {
1138 struct sdmmc_softc *sc = sf->sc;
1139 int error;
1140
1141 SDMMC_LOCK(sc);
1142
1143 if (sdmmc_chip_write_protect(sc->sc_sct, sc->sc_sch)) {
1144 aprint_normal_dev(sc->sc_dev, "write-protected\n");
1145 error = EIO;
1146 goto out;
1147 }
1148
1149 if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) {
1150 error = sdmmc_mem_single_write_block(sf, blkno, data, datalen);
1151 goto out;
1152 }
1153
1154 if (!ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1155 error = sdmmc_mem_write_block_subr(sf, blkno, data, datalen);
1156 goto out;
1157 }
1158
1159 /* DMA transfer */
1160 error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, data, datalen, NULL,
1161 BUS_DMA_NOWAIT|BUS_DMA_WRITE);
1162 if (error)
1163 goto out;
1164
1165 #ifdef SDMMC_DEBUG
1166 for (int i = 0; i < sc->sc_dmap->dm_nsegs; i++) {
1167 printf("seg#%d: addr=%#lx, size=%#lx\n", i,
1168 (u_long)sc->sc_dmap->dm_segs[i].ds_addr,
1169 (u_long)sc->sc_dmap->dm_segs[i].ds_len);
1170 }
1171 #endif
1172
1173 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1174 BUS_DMASYNC_PREWRITE);
1175
1176 error = sdmmc_mem_write_block_subr(sf, blkno, data, datalen);
1177 if (error)
1178 goto unload;
1179
1180 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1181 BUS_DMASYNC_POSTWRITE);
1182 unload:
1183 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
1184
1185 out:
1186 SDMMC_UNLOCK(sc);
1187
1188 return error;
1189 }
1190