xref: /openbsd/sys/dev/sdmmc/sdmmc_mem.c (revision 898184e3)
1 /*	$OpenBSD: sdmmc_mem.c,v 1.16 2010/08/24 14:52:23 blambert Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* Routines for SD/MMC memory cards. */
20 
21 #include <sys/param.h>
22 #include <sys/device.h>
23 #include <sys/kernel.h>
24 #include <sys/malloc.h>
25 #include <sys/systm.h>
26 
27 #include <dev/sdmmc/sdmmcchip.h>
28 #include <dev/sdmmc/sdmmcreg.h>
29 #include <dev/sdmmc/sdmmcvar.h>
30 
31 int	sdmmc_decode_csd(struct sdmmc_softc *, sdmmc_response,
32 	    struct sdmmc_function *);
33 int	sdmmc_decode_cid(struct sdmmc_softc *, sdmmc_response,
34 	    struct sdmmc_function *);
35 void	sdmmc_print_cid(struct sdmmc_cid *);
36 
37 int	sdmmc_mem_send_op_cond(struct sdmmc_softc *, u_int32_t, u_int32_t *);
38 int	sdmmc_mem_set_blocklen(struct sdmmc_softc *, struct sdmmc_function *);
39 
40 #ifdef SDMMC_DEBUG
41 #define DPRINTF(s)	printf s
42 #else
43 #define DPRINTF(s)	/**/
44 #endif
45 
46 /*
47  * Initialize SD/MMC memory cards and memory in SDIO "combo" cards.
48  */
49 int
50 sdmmc_mem_enable(struct sdmmc_softc *sc)
51 {
52 	u_int32_t host_ocr;
53 	u_int32_t card_ocr;
54 
55 	rw_assert_wrlock(&sc->sc_lock);
56 
57 	/* Set host mode to SD "combo" card or SD memory-only. */
58 	SET(sc->sc_flags, SMF_SD_MODE|SMF_MEM_MODE);
59 
60 	/* Reset memory (*must* do that before CMD55 or CMD1). */
61 	sdmmc_go_idle_state(sc);
62 
63 	/*
64 	 * Read the SD/MMC memory OCR value by issuing CMD55 followed
65 	 * by ACMD41 to read the OCR value from memory-only SD cards.
66 	 * MMC cards will not respond to CMD55 or ACMD41 and this is
67 	 * how we distinguish them from SD cards.
68 	 */
69  mmc_mode:
70 	if (sdmmc_mem_send_op_cond(sc, 0, &card_ocr) != 0) {
71 		if (ISSET(sc->sc_flags, SMF_SD_MODE) &&
72 		    !ISSET(sc->sc_flags, SMF_IO_MODE)) {
73 			/* Not a SD card, switch to MMC mode. */
74 			CLR(sc->sc_flags, SMF_SD_MODE);
75 			goto mmc_mode;
76 		}
77 		if (!ISSET(sc->sc_flags, SMF_SD_MODE)) {
78 			DPRINTF(("%s: can't read memory OCR\n",
79 			    DEVNAME(sc)));
80 			return 1;
81 		} else {
82 			/* Not a "combo" card. */
83 			CLR(sc->sc_flags, SMF_MEM_MODE);
84 			return 0;
85 		}
86 	}
87 
88 	/* Set the lowest voltage supported by the card and host. */
89 	host_ocr = sdmmc_chip_host_ocr(sc->sct, sc->sch);
90 	if (sdmmc_set_bus_power(sc, host_ocr, card_ocr) != 0) {
91 		DPRINTF(("%s: can't supply voltage requested by card\n",
92 		    DEVNAME(sc)));
93 		return 1;
94 	}
95 
96 	/* Tell the card(s) to enter the idle state (again). */
97 	sdmmc_go_idle_state(sc);
98 
99 	host_ocr &= card_ocr; /* only allow the common voltages */
100 
101 	if (sdmmc_send_if_cond(sc, card_ocr) == 0)
102 		host_ocr |= SD_OCR_SDHC_CAP;
103 
104 	/* Send the new OCR value until all cards are ready. */
105 	if (sdmmc_mem_send_op_cond(sc, host_ocr, NULL) != 0) {
106 		DPRINTF(("%s: can't send memory OCR\n", DEVNAME(sc)));
107 		return 1;
108 	}
109 	return 0;
110 }
111 
112 /*
113  * Read the CSD and CID from all cards and assign each card a unique
114  * relative card address (RCA).  CMD2 is ignored by SDIO-only cards.
115  */
116 void
117 sdmmc_mem_scan(struct sdmmc_softc *sc)
118 {
119 	struct sdmmc_command cmd;
120 	struct sdmmc_function *sf;
121 	u_int16_t next_rca;
122 	int error;
123 	int i;
124 
125 	rw_assert_wrlock(&sc->sc_lock);
126 
127 	/*
128 	 * CMD2 is a broadcast command understood by SD cards and MMC
129 	 * cards.  All cards begin to respond to the command, but back
130 	 * off if another card drives the CMD line to a different level.
131 	 * Only one card will get its entire response through.  That
132 	 * card remains silent once it has been assigned a RCA.
133 	 */
134 	for (i = 0; i < 100; i++) {
135 		bzero(&cmd, sizeof cmd);
136 		cmd.c_opcode = MMC_ALL_SEND_CID;
137 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R2;
138 
139 		error = sdmmc_mmc_command(sc, &cmd);
140 		if (error == ETIMEDOUT) {
141 			/* No more cards there. */
142 			break;
143 		} else if (error != 0) {
144 			DPRINTF(("%s: can't read CID\n", DEVNAME(sc)));
145 			break;
146 		}
147 
148 		/* In MMC mode, find the next available RCA. */
149 		next_rca = 1;
150 		if (!ISSET(sc->sc_flags, SMF_SD_MODE))
151 			SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list)
152 				next_rca++;
153 
154 		/* Allocate a sdmmc_function structure. */
155 		sf = sdmmc_function_alloc(sc);
156 		sf->rca = next_rca;
157 
158 		/*
159 		 * Remember the CID returned in the CMD2 response for
160 		 * later decoding.
161 		 */
162 		bcopy(cmd.c_resp, sf->raw_cid, sizeof sf->raw_cid);
163 
164 		/*
165 		 * Silence the card by assigning it a unique RCA, or
166 		 * querying it for its RCA in the case of SD.
167 		 */
168 		if (sdmmc_set_relative_addr(sc, sf) != 0) {
169 			printf("%s: can't set mem RCA\n", DEVNAME(sc));
170 			sdmmc_function_free(sf);
171 			break;
172 		}
173 
174 #if 0
175 		/* Verify that the RCA has been set by selecting the card. */
176 		if (sdmmc_select_card(sc, sf) != 0) {
177 			printf("%s: can't select mem RCA %d\n",
178 			    DEVNAME(sc), sf->rca);
179 			sdmmc_function_free(sf);
180 			break;
181 		}
182 
183 		/* Deselect. */
184 		(void)sdmmc_select_card(sc, NULL);
185 #endif
186 
187 		/*
188 		 * If this is a memory-only card, the card responding
189 		 * first becomes an alias for SDIO function 0.
190 		 */
191 		if (sc->sc_fn0 == NULL)
192 			sc->sc_fn0 = sf;
193 
194 		SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
195 	}
196 
197 	/*
198 	 * All cards are either inactive or awaiting further commands.
199 	 * Read the CSDs and decode the raw CID for each card.
200 	 */
201 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
202 		bzero(&cmd, sizeof cmd);
203 		cmd.c_opcode = MMC_SEND_CSD;
204 		cmd.c_arg = MMC_ARG_RCA(sf->rca);
205 		cmd.c_flags = SCF_CMD_AC | SCF_RSP_R2;
206 
207 		if (sdmmc_mmc_command(sc, &cmd) != 0) {
208 			SET(sf->flags, SFF_ERROR);
209 			continue;
210 		}
211 
212 		if (sdmmc_decode_csd(sc, cmd.c_resp, sf) != 0 ||
213 		    sdmmc_decode_cid(sc, sf->raw_cid, sf) != 0) {
214 			SET(sf->flags, SFF_ERROR);
215 			continue;
216 		}
217 
218 #ifdef SDMMC_DEBUG
219 		printf("%s: CID: ", DEVNAME(sc));
220 		sdmmc_print_cid(&sf->cid);
221 #endif
222 	}
223 }
224 
225 int
226 sdmmc_decode_csd(struct sdmmc_softc *sc, sdmmc_response resp,
227     struct sdmmc_function *sf)
228 {
229 	struct sdmmc_csd *csd = &sf->csd;
230 
231 	if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
232 		/*
233 		 * CSD version 1.0 corresponds to SD system
234 		 * specification version 1.0 - 1.10. (SanDisk, 3.5.3)
235 		 */
236 		csd->csdver = SD_CSD_CSDVER(resp);
237 		switch (csd->csdver) {
238 		case SD_CSD_CSDVER_2_0:
239 			sf->flags |= SFF_SDHC;
240 			csd->capacity = SD_CSD_V2_CAPACITY(resp);
241 			csd->read_bl_len = SD_CSD_V2_BL_LEN;
242 			break;
243 		case SD_CSD_CSDVER_1_0:
244 			csd->capacity = SD_CSD_CAPACITY(resp);
245 			csd->read_bl_len = SD_CSD_READ_BL_LEN(resp);
246 			break;
247 		default:
248 			printf("%s: unknown SD CSD structure version 0x%x\n",
249 			    DEVNAME(sc), csd->csdver);
250 			return 1;
251 			break;
252 		}
253 
254 	} else {
255 		csd->csdver = MMC_CSD_CSDVER(resp);
256 		if (csd->csdver != MMC_CSD_CSDVER_1_0 &&
257 		    csd->csdver != MMC_CSD_CSDVER_2_0) {
258 			printf("%s: unknown MMC CSD structure version 0x%x\n",
259 			    DEVNAME(sc), csd->csdver);
260 			return 1;
261 		}
262 
263 		csd->mmcver = MMC_CSD_MMCVER(resp);
264 		csd->capacity = MMC_CSD_CAPACITY(resp);
265 		csd->read_bl_len = MMC_CSD_READ_BL_LEN(resp);
266 	}
267 	csd->sector_size = MIN(1 << csd->read_bl_len,
268 	    sdmmc_chip_host_maxblklen(sc->sct, sc->sch));
269 	if (csd->sector_size < (1<<csd->read_bl_len))
270 		csd->capacity *= (1<<csd->read_bl_len) /
271 		    csd->sector_size;
272 
273 	return 0;
274 }
275 
276 int
277 sdmmc_decode_cid(struct sdmmc_softc *sc, sdmmc_response resp,
278     struct sdmmc_function *sf)
279 {
280 	struct sdmmc_cid *cid = &sf->cid;
281 
282 	if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
283 		cid->mid = SD_CID_MID(resp);
284 		cid->oid = SD_CID_OID(resp);
285 		SD_CID_PNM_CPY(resp, cid->pnm);
286 		cid->rev = SD_CID_REV(resp);
287 		cid->psn = SD_CID_PSN(resp);
288 		cid->mdt = SD_CID_MDT(resp);
289 	} else {
290 		switch(sf->csd.mmcver) {
291 		case MMC_CSD_MMCVER_1_0:
292 		case MMC_CSD_MMCVER_1_4:
293 			cid->mid = MMC_CID_MID_V1(resp);
294 			MMC_CID_PNM_V1_CPY(resp, cid->pnm);
295 			cid->rev = MMC_CID_REV_V1(resp);
296 			cid->psn = MMC_CID_PSN_V1(resp);
297 			cid->mdt = MMC_CID_MDT_V1(resp);
298 			break;
299 		case MMC_CSD_MMCVER_2_0:
300 		case MMC_CSD_MMCVER_3_1:
301 		case MMC_CSD_MMCVER_4_0:
302 			cid->mid = MMC_CID_MID_V2(resp);
303 			cid->oid = MMC_CID_OID_V2(resp);
304 			MMC_CID_PNM_V2_CPY(resp, cid->pnm);
305 			cid->psn = MMC_CID_PSN_V2(resp);
306 			break;
307 		default:
308 			printf("%s: unknown MMC version %d\n",
309 			    DEVNAME(sc), sf->csd.mmcver);
310 			return 1;
311 		}
312 	}
313 	return 0;
314 }
315 
316 #ifdef SDMMC_DEBUG
317 void
318 sdmmc_print_cid(struct sdmmc_cid *cid)
319 {
320 	printf("mid=0x%02x oid=0x%04x pnm=\"%s\" rev=0x%02x psn=0x%08x"
321 	    " mdt=%03x\n", cid->mid, cid->oid, cid->pnm, cid->rev, cid->psn,
322 	    cid->mdt);
323 }
324 #endif
325 
326 /*
327  * Initialize a SD/MMC memory card.
328  */
329 int
330 sdmmc_mem_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
331 {
332 	int error = 0;
333 
334 	rw_assert_wrlock(&sc->sc_lock);
335 
336 	if (sdmmc_select_card(sc, sf) != 0 ||
337 	    sdmmc_mem_set_blocklen(sc, sf) != 0)
338 		error = 1;
339 	return error;
340 }
341 
342 /*
343  * Get or set the card's memory OCR value (SD or MMC).
344  */
345 int
346 sdmmc_mem_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr,
347     u_int32_t *ocrp)
348 {
349 	struct sdmmc_command cmd;
350 	int error;
351 	int i;
352 
353 	rw_assert_wrlock(&sc->sc_lock);
354 
355 	/*
356 	 * If we change the OCR value, retry the command until the OCR
357 	 * we receive in response has the "CARD BUSY" bit set, meaning
358 	 * that all cards are ready for identification.
359 	 */
360 	for (i = 0; i < 100; i++) {
361 		bzero(&cmd, sizeof cmd);
362 		cmd.c_arg = ocr;
363 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R3;
364 
365 		if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
366 			cmd.c_opcode = SD_APP_OP_COND;
367 			error = sdmmc_app_command(sc, &cmd);
368 		} else {
369 			cmd.c_opcode = MMC_SEND_OP_COND;
370 			error = sdmmc_mmc_command(sc, &cmd);
371 		}
372 		if (error != 0)
373 			break;
374 		if (ISSET(MMC_R3(cmd.c_resp), MMC_OCR_MEM_READY) ||
375 		    ocr == 0)
376 			break;
377 		error = ETIMEDOUT;
378 		sdmmc_delay(10000);
379 	}
380 	if (error == 0 && ocrp != NULL)
381 		*ocrp = MMC_R3(cmd.c_resp);
382 
383 	return error;
384 }
385 
386 /*
387  * Set the read block length appropriately for this card, according to
388  * the card CSD register value.
389  */
390 int
391 sdmmc_mem_set_blocklen(struct sdmmc_softc *sc, struct sdmmc_function *sf)
392 {
393 	struct sdmmc_command cmd;
394 
395 	rw_assert_wrlock(&sc->sc_lock);
396 
397 	bzero(&cmd, sizeof cmd);
398 	cmd.c_opcode = MMC_SET_BLOCKLEN;
399 	cmd.c_arg = sf->csd.sector_size;
400 	cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
401 	DPRINTF(("%s: read_bl_len=%d sector_size=%d\n", DEVNAME(sc),
402 	    1 << sf->csd.read_bl_len, sf->csd.sector_size));
403 
404 	return sdmmc_mmc_command(sc, &cmd);
405 }
406 
407 int
408 sdmmc_mem_read_block(struct sdmmc_function *sf, int blkno, u_char *data,
409     size_t datalen)
410 {
411 	struct sdmmc_softc *sc = sf->sc;
412 	struct sdmmc_command cmd;
413 	int error;
414 
415 	rw_enter_write(&sc->sc_lock);
416 
417 	if ((error = sdmmc_select_card(sc, sf)) != 0)
418 		goto err;
419 
420 	bzero(&cmd, sizeof cmd);
421 	cmd.c_data = data;
422 	cmd.c_datalen = datalen;
423 	cmd.c_blklen = sf->csd.sector_size;
424 	cmd.c_opcode = (datalen / cmd.c_blklen) > 1 ?
425 	    MMC_READ_BLOCK_MULTIPLE : MMC_READ_BLOCK_SINGLE;
426 	if (sf->flags & SFF_SDHC)
427 		cmd.c_arg = blkno;
428 	else
429 		cmd.c_arg = blkno << 9;
430 	cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1;
431 
432 	error = sdmmc_mmc_command(sc, &cmd);
433 	if (error != 0)
434 		goto err;
435 
436 	if (ISSET(sc->sc_flags, SMF_STOP_AFTER_MULTIPLE) &&
437 	    cmd.c_opcode == MMC_READ_BLOCK_MULTIPLE) {
438 		bzero(&cmd, sizeof cmd);
439 		cmd.c_opcode = MMC_STOP_TRANSMISSION;
440 		cmd.c_arg = MMC_ARG_RCA(sf->rca);
441 		cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B;
442 		error = sdmmc_mmc_command(sc, &cmd);
443 		if (error != 0)
444 			goto err;
445 	}
446 
447 	do {
448 		bzero(&cmd, sizeof cmd);
449 		cmd.c_opcode = MMC_SEND_STATUS;
450 		cmd.c_arg = MMC_ARG_RCA(sf->rca);
451 		cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
452 		error = sdmmc_mmc_command(sc, &cmd);
453 		if (error != 0)
454 			break;
455 		/* XXX time out */
456 	} while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
457 
458 err:
459 	rw_exit(&sc->sc_lock);
460 	return error;
461 }
462 
463 int
464 sdmmc_mem_write_block(struct sdmmc_function *sf, int blkno, u_char *data,
465     size_t datalen)
466 {
467 	struct sdmmc_softc *sc = sf->sc;
468 	struct sdmmc_command cmd;
469 	int error;
470 
471 	rw_enter_write(&sc->sc_lock);
472 
473 	if ((error = sdmmc_select_card(sc, sf)) != 0)
474 		goto err;
475 
476 	bzero(&cmd, sizeof cmd);
477 	cmd.c_data = data;
478 	cmd.c_datalen = datalen;
479 	cmd.c_blklen = sf->csd.sector_size;
480 	cmd.c_opcode = (datalen / cmd.c_blklen) > 1 ?
481 	    MMC_WRITE_BLOCK_MULTIPLE : MMC_WRITE_BLOCK_SINGLE;
482 	if (sf->flags & SFF_SDHC)
483 		cmd.c_arg = blkno;
484 	else
485 		cmd.c_arg = blkno << 9;
486 	cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R1;
487 
488 	error = sdmmc_mmc_command(sc, &cmd);
489 	if (error != 0)
490 		goto err;
491 
492 	if (ISSET(sc->sc_flags, SMF_STOP_AFTER_MULTIPLE) &&
493 	    cmd.c_opcode == MMC_WRITE_BLOCK_MULTIPLE) {
494 		bzero(&cmd, sizeof cmd);
495 		cmd.c_opcode = MMC_STOP_TRANSMISSION;
496 		cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B;
497 		error = sdmmc_mmc_command(sc, &cmd);
498 		if (error != 0)
499 			goto err;
500 	}
501 
502 	do {
503 		bzero(&cmd, sizeof cmd);
504 		cmd.c_opcode = MMC_SEND_STATUS;
505 		cmd.c_arg = MMC_ARG_RCA(sf->rca);
506 		cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1;
507 		error = sdmmc_mmc_command(sc, &cmd);
508 		if (error != 0)
509 			break;
510 		/* XXX time out */
511 	} while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
512 
513 err:
514 	rw_exit(&sc->sc_lock);
515 	return error;
516 }
517