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, ®);
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 /*
50 * Verify that a register write works. Given the custom nature of SDIO devices,
51 * we just write to the card common I/O area.
52 */
ZTEST(sd_stack,test_write)53 ZTEST(sd_stack, test_write)
54 {
55 int ret;
56 uint8_t data = 0x01;
57 uint8_t reg = 0xFF;
58 uint8_t new_reg_value = 0xFF;
59
60 /* Read from card common I/O area. */
61 ret = sdio_read_byte(&card.func0, SDIO_CCCR_BUS_IF, ®);
62 zassert_equal(ret, 0, "SD card read failed");
63
64 /* Write to card common I/O area. */
65 ret = sdio_write_byte(&card.func0, SDIO_CCCR_BUS_IF, data);
66 zassert_equal(ret, 0, "SD card write failed");
67
68 /* Read after write to verify that data was written */
69 ret = sdio_read_byte(&card.func0, SDIO_CCCR_BUS_IF, &new_reg_value);
70 zassert_equal(ret, 0, "SD card read failed");
71 new_reg_value = new_reg_value & SDIO_CCCR_BUS_IF_WIDTH_MASK;
72 zassert_equal(new_reg_value, data, "read data was not as expected");
73 }
74
75 /* Simply dump the card configuration. */
ZTEST(sd_stack,test_card_config)76 ZTEST(sd_stack, test_card_config)
77 {
78 switch (card.card_voltage) {
79 case SD_VOL_1_2_V:
80 TC_PRINT("Card voltage: 1.2V\n");
81 break;
82 case SD_VOL_1_8_V:
83 TC_PRINT("Card voltage: 1.8V\n");
84 break;
85 case SD_VOL_3_0_V:
86 TC_PRINT("Card voltage: 3.0V\n");
87 break;
88 case SD_VOL_3_3_V:
89 TC_PRINT("Card voltage: 3.3V\n");
90 break;
91 default:
92 zassert_unreachable("Card voltage is not known value");
93 }
94 zassert_equal(card.status, CARD_INITIALIZED, "Card status is not OK");
95 switch (card.card_speed) {
96 case SD_TIMING_SDR12:
97 TC_PRINT("Card timing: SDR12\n");
98 break;
99 case SD_TIMING_SDR25:
100 TC_PRINT("Card timing: SDR25\n");
101 break;
102 case SD_TIMING_SDR50:
103 TC_PRINT("Card timing: SDR50\n");
104 break;
105 case SD_TIMING_SDR104:
106 TC_PRINT("Card timing: SDR104\n");
107 break;
108 case SD_TIMING_DDR50:
109 TC_PRINT("Card timing: DDR50\n");
110 break;
111 default:
112 zassert_unreachable("Card timing is not known value");
113 }
114 switch (card.type) {
115 case CARD_SDIO:
116 TC_PRINT("Card type: SDIO\n");
117 break;
118 case CARD_SDMMC:
119 TC_PRINT("Card type: SDMMC\n");
120 break;
121 case CARD_COMBO:
122 TC_PRINT("Card type: combo card\n");
123 break;
124 default:
125 zassert_unreachable("Card type is not known value");
126 }
127 if (card.sd_version >= SD_SPEC_VER3_0) {
128 TC_PRINT("Card spec: 3.0\n");
129 } else if (card.sd_version >= SD_SPEC_VER2_0) {
130 TC_PRINT("Card spec: 2.0\n");
131 } else if (card.sd_version >= SD_SPEC_VER1_1) {
132 TC_PRINT("Card spec: 1.1\n");
133 } else if (card.sd_version >= SD_SPEC_VER1_0) {
134 TC_PRINT("Card spec: 1.0\n");
135 } else {
136 zassert_unreachable("Card spec is unknown value");
137 }
138 }
139
140 ZTEST_SUITE(sd_stack, NULL, NULL, NULL, NULL, NULL);
141