1 /*
2  * Copyright 2022 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/sd/sdio.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/disk.h>
10 #include <zephyr/ztest.h>
11 
12 
13 static const struct device *sdhc_dev = DEVICE_DT_GET(DT_ALIAS(sdhc0));
14 static struct sd_card card;
15 
16 /*
17  * Verify that SD stack can initialize an SDIO card.
18  * This test must run first, to ensure the card is initialized.
19  */
ZTEST(sd_stack,test_0_init)20 ZTEST(sd_stack, test_0_init)
21 {
22 	int ret;
23 
24 	zassert_true(device_is_ready(sdhc_dev), "SDHC device is not ready");
25 
26 	ret = sd_is_card_present(sdhc_dev);
27 	zassert_equal(ret, 1, "SD card not present in slot");
28 
29 	ret = sd_init(sdhc_dev, &card);
30 	zassert_equal(ret, 0, "Card initialization failed");
31 }
32 
33 /*
34  * Verify that a register read works. Given the custom nature of SDIO devices,
35  * we just read from the card common I/O area.
36  */
ZTEST(sd_stack,test_read)37 ZTEST(sd_stack, test_read)
38 {
39 	int ret;
40 	uint8_t reg = 0xFF;
41 
42 	/* Read from card common I/O area. */
43 	ret = sdio_read_byte(&card.func0, SDIO_CCCR_CCCR, &reg);
44 	zassert_equal(ret, 0, "SD card read failed");
45 	/* Check to make sure CCCR read actually returned valid data */
46 	zassert_not_equal(reg, 0xFF, "CCCR read returned invalid data");
47 }
48 
49 /* Simply dump the card configuration. */
ZTEST(sd_stack,test_card_config)50 ZTEST(sd_stack, test_card_config)
51 {
52 	switch (card.card_voltage) {
53 	case SD_VOL_1_2_V:
54 		TC_PRINT("Card voltage: 1.2V\n");
55 		break;
56 	case SD_VOL_1_8_V:
57 		TC_PRINT("Card voltage: 1.8V\n");
58 		break;
59 	case SD_VOL_3_0_V:
60 		TC_PRINT("Card voltage: 3.0V\n");
61 		break;
62 	case SD_VOL_3_3_V:
63 		TC_PRINT("Card voltage: 3.3V\n");
64 		break;
65 	default:
66 		zassert_unreachable("Card voltage is not known value");
67 	}
68 	zassert_equal(card.status, CARD_INITIALIZED, "Card status is not OK");
69 	switch (card.card_speed) {
70 	case SD_TIMING_SDR12:
71 		TC_PRINT("Card timing: SDR12\n");
72 		break;
73 	case SD_TIMING_SDR25:
74 		TC_PRINT("Card timing: SDR25\n");
75 		break;
76 	case SD_TIMING_SDR50:
77 		TC_PRINT("Card timing: SDR50\n");
78 		break;
79 	case SD_TIMING_SDR104:
80 		TC_PRINT("Card timing: SDR104\n");
81 		break;
82 	case SD_TIMING_DDR50:
83 		TC_PRINT("Card timing: DDR50\n");
84 		break;
85 	default:
86 		zassert_unreachable("Card timing is not known value");
87 	}
88 	switch (card.type) {
89 	case CARD_SDIO:
90 		TC_PRINT("Card type: SDIO\n");
91 		break;
92 	case CARD_SDMMC:
93 		TC_PRINT("Card type: SDMMC\n");
94 		break;
95 	case CARD_COMBO:
96 		TC_PRINT("Card type: combo card\n");
97 		break;
98 	default:
99 		zassert_unreachable("Card type is not known value");
100 	}
101 	if (card.sd_version >= SD_SPEC_VER3_0) {
102 		TC_PRINT("Card spec: 3.0\n");
103 	} else if (card.sd_version >= SD_SPEC_VER2_0) {
104 		TC_PRINT("Card spec: 2.0\n");
105 	} else if (card.sd_version >= SD_SPEC_VER1_1) {
106 		TC_PRINT("Card spec: 1.1\n");
107 	} else if (card.sd_version >= SD_SPEC_VER1_0) {
108 		TC_PRINT("Card spec: 1.0\n");
109 	} else {
110 		zassert_unreachable("Card spec is unknown value");
111 	}
112 }
113 
114 ZTEST_SUITE(sd_stack, NULL, NULL, NULL, NULL, NULL);
115