xref: /openbmc/qemu/tests/qtest/sdhci-test.c (revision 1e8a1fae7464ef79c9e50aa0f807d2c511be3c8e)
1*1e8a1faeSThomas Huth /*
2*1e8a1faeSThomas Huth  * QTest testcase for SDHCI controllers
3*1e8a1faeSThomas Huth  *
4*1e8a1faeSThomas Huth  * Written by Philippe Mathieu-Daudé <f4bug@amsat.org>
5*1e8a1faeSThomas Huth  *
6*1e8a1faeSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7*1e8a1faeSThomas Huth  * See the COPYING file in the top-level directory.
8*1e8a1faeSThomas Huth  * SPDX-License-Identifier: GPL-2.0-or-later
9*1e8a1faeSThomas Huth  */
10*1e8a1faeSThomas Huth 
11*1e8a1faeSThomas Huth #include "qemu/osdep.h"
12*1e8a1faeSThomas Huth #include "hw/registerfields.h"
13*1e8a1faeSThomas Huth #include "libqtest.h"
14*1e8a1faeSThomas Huth #include "qemu/module.h"
15*1e8a1faeSThomas Huth #include "libqos/pci-pc.h"
16*1e8a1faeSThomas Huth #include "hw/pci/pci.h"
17*1e8a1faeSThomas Huth #include "libqos/qgraph.h"
18*1e8a1faeSThomas Huth #include "libqos/sdhci.h"
19*1e8a1faeSThomas Huth 
20*1e8a1faeSThomas Huth #define SDHC_CAPAB                      0x40
21*1e8a1faeSThomas Huth FIELD(SDHC_CAPAB, BASECLKFREQ,               8, 8); /* since v2 */
22*1e8a1faeSThomas Huth FIELD(SDHC_CAPAB, SDMA,                     22, 1);
23*1e8a1faeSThomas Huth FIELD(SDHC_CAPAB, SDR,                      32, 3); /* since v3 */
24*1e8a1faeSThomas Huth FIELD(SDHC_CAPAB, DRIVER,                   36, 3); /* since v3 */
25*1e8a1faeSThomas Huth #define SDHC_HCVER                      0xFE
26*1e8a1faeSThomas Huth 
27*1e8a1faeSThomas Huth static void check_specs_version(QSDHCI *s, uint8_t version)
28*1e8a1faeSThomas Huth {
29*1e8a1faeSThomas Huth     uint32_t v;
30*1e8a1faeSThomas Huth 
31*1e8a1faeSThomas Huth     v = s->readw(s, SDHC_HCVER);
32*1e8a1faeSThomas Huth     v &= 0xff;
33*1e8a1faeSThomas Huth     v += 1;
34*1e8a1faeSThomas Huth     g_assert_cmpuint(v, ==, version);
35*1e8a1faeSThomas Huth }
36*1e8a1faeSThomas Huth 
37*1e8a1faeSThomas Huth static void check_capab_capareg(QSDHCI *s, uint64_t expec_capab)
38*1e8a1faeSThomas Huth {
39*1e8a1faeSThomas Huth     uint64_t capab;
40*1e8a1faeSThomas Huth 
41*1e8a1faeSThomas Huth     capab = s->readq(s, SDHC_CAPAB);
42*1e8a1faeSThomas Huth     g_assert_cmphex(capab, ==, expec_capab);
43*1e8a1faeSThomas Huth }
44*1e8a1faeSThomas Huth 
45*1e8a1faeSThomas Huth static void check_capab_readonly(QSDHCI *s)
46*1e8a1faeSThomas Huth {
47*1e8a1faeSThomas Huth     const uint64_t vrand = 0x123456789abcdef;
48*1e8a1faeSThomas Huth     uint64_t capab0, capab1;
49*1e8a1faeSThomas Huth 
50*1e8a1faeSThomas Huth     capab0 = s->readq(s, SDHC_CAPAB);
51*1e8a1faeSThomas Huth     g_assert_cmpuint(capab0, !=, vrand);
52*1e8a1faeSThomas Huth 
53*1e8a1faeSThomas Huth     s->writeq(s, SDHC_CAPAB, vrand);
54*1e8a1faeSThomas Huth     capab1 = s->readq(s, SDHC_CAPAB);
55*1e8a1faeSThomas Huth     g_assert_cmpuint(capab1, !=, vrand);
56*1e8a1faeSThomas Huth     g_assert_cmpuint(capab1, ==, capab0);
57*1e8a1faeSThomas Huth }
58*1e8a1faeSThomas Huth 
59*1e8a1faeSThomas Huth static void check_capab_baseclock(QSDHCI *s, uint8_t expec_freq)
60*1e8a1faeSThomas Huth {
61*1e8a1faeSThomas Huth     uint64_t capab, capab_freq;
62*1e8a1faeSThomas Huth 
63*1e8a1faeSThomas Huth     if (!expec_freq) {
64*1e8a1faeSThomas Huth         return;
65*1e8a1faeSThomas Huth     }
66*1e8a1faeSThomas Huth     capab = s->readq(s, SDHC_CAPAB);
67*1e8a1faeSThomas Huth     capab_freq = FIELD_EX64(capab, SDHC_CAPAB, BASECLKFREQ);
68*1e8a1faeSThomas Huth     g_assert_cmpuint(capab_freq, ==, expec_freq);
69*1e8a1faeSThomas Huth }
70*1e8a1faeSThomas Huth 
71*1e8a1faeSThomas Huth static void check_capab_sdma(QSDHCI *s, bool supported)
72*1e8a1faeSThomas Huth {
73*1e8a1faeSThomas Huth     uint64_t capab, capab_sdma;
74*1e8a1faeSThomas Huth 
75*1e8a1faeSThomas Huth     capab = s->readq(s, SDHC_CAPAB);
76*1e8a1faeSThomas Huth     capab_sdma = FIELD_EX64(capab, SDHC_CAPAB, SDMA);
77*1e8a1faeSThomas Huth     g_assert_cmpuint(capab_sdma, ==, supported);
78*1e8a1faeSThomas Huth }
79*1e8a1faeSThomas Huth 
80*1e8a1faeSThomas Huth static void check_capab_v3(QSDHCI *s, uint8_t version)
81*1e8a1faeSThomas Huth {
82*1e8a1faeSThomas Huth     uint64_t capab, capab_v3;
83*1e8a1faeSThomas Huth 
84*1e8a1faeSThomas Huth     if (version < 3) {
85*1e8a1faeSThomas Huth         /* before v3 those fields are RESERVED */
86*1e8a1faeSThomas Huth         capab = s->readq(s, SDHC_CAPAB);
87*1e8a1faeSThomas Huth         capab_v3 = FIELD_EX64(capab, SDHC_CAPAB, SDR);
88*1e8a1faeSThomas Huth         g_assert_cmpuint(capab_v3, ==, 0);
89*1e8a1faeSThomas Huth         capab_v3 = FIELD_EX64(capab, SDHC_CAPAB, DRIVER);
90*1e8a1faeSThomas Huth         g_assert_cmpuint(capab_v3, ==, 0);
91*1e8a1faeSThomas Huth     }
92*1e8a1faeSThomas Huth }
93*1e8a1faeSThomas Huth 
94*1e8a1faeSThomas Huth static void test_registers(void *obj, void *data, QGuestAllocator *alloc)
95*1e8a1faeSThomas Huth {
96*1e8a1faeSThomas Huth     QSDHCI *s = obj;
97*1e8a1faeSThomas Huth 
98*1e8a1faeSThomas Huth     check_specs_version(s, s->props.version);
99*1e8a1faeSThomas Huth     check_capab_capareg(s, s->props.capab.reg);
100*1e8a1faeSThomas Huth     check_capab_readonly(s);
101*1e8a1faeSThomas Huth     check_capab_v3(s, s->props.version);
102*1e8a1faeSThomas Huth     check_capab_sdma(s, s->props.capab.sdma);
103*1e8a1faeSThomas Huth     check_capab_baseclock(s, s->props.baseclock);
104*1e8a1faeSThomas Huth }
105*1e8a1faeSThomas Huth 
106*1e8a1faeSThomas Huth static void register_sdhci_test(void)
107*1e8a1faeSThomas Huth {
108*1e8a1faeSThomas Huth     qos_add_test("registers", "sdhci", test_registers, NULL);
109*1e8a1faeSThomas Huth }
110*1e8a1faeSThomas Huth 
111*1e8a1faeSThomas Huth libqos_init(register_sdhci_test);
112