]>
Commit | Line | Data |
---|---|---|
8640cc11 PMD |
1 | /* |
2 | * QTest testcase for SDHCI controllers | |
3 | * | |
4 | * Written by Philippe Mathieu-Daudé <f4bug@amsat.org> | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
7 | * See the COPYING file in the top-level directory. | |
8 | * SPDX-License-Identifier: GPL-2.0-or-later | |
9 | */ | |
10 | #include "qemu/osdep.h" | |
11 | #include "hw/registerfields.h" | |
12 | #include "libqtest.h" | |
13 | #include "libqos/pci-pc.h" | |
14 | #include "hw/pci/pci.h" | |
15 | ||
16 | #define SDHC_CAPAB 0x40 | |
17 | #define SDHC_HCVER 0xFE | |
18 | ||
19 | static const struct sdhci_t { | |
20 | const char *arch, *machine; | |
21 | struct { | |
22 | uintptr_t addr; | |
23 | uint8_t version; | |
24 | uint8_t baseclock; | |
25 | struct { | |
26 | bool sdma; | |
27 | uint64_t reg; | |
28 | } capab; | |
29 | } sdhci; | |
30 | struct { | |
31 | uint16_t vendor_id, device_id; | |
32 | } pci; | |
33 | } models[] = { | |
34 | /* PC via PCI */ | |
35 | { "x86_64", "pc", | |
36 | {-1, 2, 0, {1, 0x057834b4} }, | |
37 | .pci = { PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_SDHCI } }, | |
38 | ||
39 | /* Exynos4210 */ | |
40 | { "arm", "smdkc210", | |
41 | {0x12510000, 2, 0, {1, 0x5e80080} } }, | |
42 | }; | |
43 | ||
44 | typedef struct QSDHCI { | |
45 | struct { | |
46 | QPCIBus *bus; | |
47 | QPCIDevice *dev; | |
48 | } pci; | |
49 | union { | |
50 | QPCIBar mem_bar; | |
51 | uint64_t addr; | |
52 | }; | |
53 | } QSDHCI; | |
54 | ||
55 | static uint64_t sdhci_readq(QSDHCI *s, uint32_t reg) | |
56 | { | |
57 | uint64_t val; | |
58 | ||
59 | if (s->pci.dev) { | |
60 | val = qpci_io_readq(s->pci.dev, s->mem_bar, reg); | |
61 | } else { | |
62 | val = qtest_readq(global_qtest, s->addr + reg); | |
63 | } | |
64 | ||
65 | return val; | |
66 | } | |
67 | ||
68 | static void check_capab_capareg(QSDHCI *s, uint64_t expec_capab) | |
69 | { | |
70 | uint64_t capab; | |
71 | ||
72 | capab = sdhci_readq(s, SDHC_CAPAB); | |
73 | g_assert_cmphex(capab, ==, expec_capab); | |
74 | } | |
75 | ||
76 | static QSDHCI *machine_start(const struct sdhci_t *test) | |
77 | { | |
78 | QSDHCI *s = g_new0(QSDHCI, 1); | |
79 | ||
80 | if (test->pci.vendor_id) { | |
81 | /* PCI */ | |
82 | uint16_t vendor_id, device_id; | |
83 | uint64_t barsize; | |
84 | ||
85 | global_qtest = qtest_startf("-machine %s -device sdhci-pci", | |
86 | test->machine); | |
87 | ||
88 | s->pci.bus = qpci_init_pc(NULL); | |
89 | ||
90 | /* Find PCI device and verify it's the right one */ | |
91 | s->pci.dev = qpci_device_find(s->pci.bus, QPCI_DEVFN(4, 0)); | |
92 | g_assert_nonnull(s->pci.dev); | |
93 | vendor_id = qpci_config_readw(s->pci.dev, PCI_VENDOR_ID); | |
94 | device_id = qpci_config_readw(s->pci.dev, PCI_DEVICE_ID); | |
95 | g_assert(vendor_id == test->pci.vendor_id); | |
96 | g_assert(device_id == test->pci.device_id); | |
97 | s->mem_bar = qpci_iomap(s->pci.dev, 0, &barsize); | |
98 | qpci_device_enable(s->pci.dev); | |
99 | } else { | |
100 | /* SysBus */ | |
101 | global_qtest = qtest_startf("-machine %s", test->machine); | |
102 | s->addr = test->sdhci.addr; | |
103 | } | |
104 | ||
105 | return s; | |
106 | } | |
107 | ||
108 | static void machine_stop(QSDHCI *s) | |
109 | { | |
110 | g_free(s->pci.dev); | |
111 | qtest_quit(global_qtest); | |
112 | } | |
113 | ||
114 | static void test_machine(const void *data) | |
115 | { | |
116 | const struct sdhci_t *test = data; | |
117 | QSDHCI *s; | |
118 | ||
119 | s = machine_start(test); | |
120 | ||
121 | check_capab_capareg(s, test->sdhci.capab.reg); | |
122 | ||
123 | machine_stop(s); | |
124 | } | |
125 | ||
126 | int main(int argc, char *argv[]) | |
127 | { | |
128 | const char *arch = qtest_get_arch(); | |
129 | char *name; | |
130 | int i; | |
131 | ||
132 | g_test_init(&argc, &argv, NULL); | |
133 | for (i = 0; i < ARRAY_SIZE(models); i++) { | |
134 | if (strcmp(arch, models[i].arch)) { | |
135 | continue; | |
136 | } | |
137 | name = g_strdup_printf("sdhci/%s", models[i].machine); | |
138 | qtest_add_data_func(name, &models[i], test_machine); | |
139 | g_free(name); | |
140 | } | |
141 | ||
142 | return g_test_run(); | |
143 | } |