| 1 | /* |
| 2 | * QTest testcase for the Aspeed GPIO Controller. |
| 3 | * |
| 4 | * Copyright (c) Meta Platforms, Inc. and affiliates. (http://www.meta.com) |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "qemu/osdep.h" |
| 26 | #include "qemu/bitops.h" |
| 27 | #include "qemu/timer.h" |
| 28 | #include "qobject/qdict.h" |
| 29 | #include "libqtest-single.h" |
| 30 | |
| 31 | #define AST2600_GPIO_BASE 0x1E780000 |
| 32 | |
| 33 | #define GPIO_ABCD_DATA_VALUE 0x000 |
| 34 | #define GPIO_ABCD_DIRECTION 0x004 |
| 35 | |
| 36 | static uint32_t qtest_qom_get_uint32(QTestState *s, const char *path, |
| 37 | const char *property) |
| 38 | { |
| 39 | QDict *r; |
| 40 | |
| 41 | uint32_t res; |
| 42 | r = qtest_qmp(s, "{ 'execute': 'qom-get', 'arguments': " |
| 43 | "{ 'path': %s, 'property': %s } }", path, property); |
| 44 | res = qdict_get_uint(r, "return"); |
| 45 | qobject_unref(r); |
| 46 | |
| 47 | return res; |
| 48 | } |
| 49 | |
| 50 | static void qtest_qom_set_uint32(QTestState *s, const char *path, |
| 51 | const char *property, uint32_t value) |
| 52 | { |
| 53 | QDict *r; |
| 54 | |
| 55 | r = qtest_qmp(s, "{ 'execute': 'qom-set', 'arguments': " |
| 56 | "{ 'path': %s, 'property': %s, 'value': %" PRIu32 " } }", |
| 57 | path, property, value); |
| 58 | qobject_unref(r); |
| 59 | } |
| 60 | |
| 61 | static const char *resp_get_error(QDict *r, const char* error_key) |
| 62 | { |
| 63 | QDict *qdict; |
| 64 | |
| 65 | g_assert(r); |
| 66 | |
| 67 | qdict = qdict_get_qdict(r, "error"); |
| 68 | if (qdict) { |
| 69 | return qdict_get_str(qdict, error_key); |
| 70 | } |
| 71 | |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | static bool qtest_qom_check_error(QTestState *s, const char *path, |
| 76 | const char *property, const char *error_msg, |
| 77 | const char *error_msg_key) |
| 78 | { |
| 79 | QDict *r; |
| 80 | bool b; |
| 81 | |
| 82 | r = qtest_qmp(s, "{ 'execute': 'qom-get', 'arguments': " |
| 83 | "{ 'path': %s, 'property': %s } }", path, property); |
| 84 | b = g_str_equal(resp_get_error(r, error_msg_key), error_msg); |
| 85 | qobject_unref(r); |
| 86 | |
| 87 | return b; |
| 88 | } |
| 89 | |
| 90 | static void test_set_colocated_pins(const void *data) |
| 91 | { |
| 92 | QTestState *s = (QTestState *)data; |
| 93 | const char path[] = "/machine/soc/gpio"; |
| 94 | /* |
| 95 | * gpioV4-7 occupy bits within a single 32-bit value, so we want to make |
| 96 | * sure that modifying one doesn't affect the other. |
| 97 | */ |
| 98 | qtest_qom_set_bool(s, path, "gpioV4", true); |
| 99 | qtest_qom_set_bool(s, path, "gpioV5", false); |
| 100 | qtest_qom_set_bool(s, path, "gpioV6", true); |
| 101 | qtest_qom_set_bool(s, path, "gpioV7", false); |
| 102 | g_assert(qtest_qom_get_bool(s, path, "gpioV4")); |
| 103 | g_assert(!qtest_qom_get_bool(s, path, "gpioV5")); |
| 104 | g_assert(qtest_qom_get_bool(s, path, "gpioV6")); |
| 105 | g_assert(!qtest_qom_get_bool(s, path, "gpioV7")); |
| 106 | |
| 107 | /* |
| 108 | * Testing the gpio-set[%d] properties, using individual gpio boolean |
| 109 | * properties to do cross check. |
| 110 | * We use gpioR4-7 for test, Setting them to be 0b1010. |
| 111 | */ |
| 112 | qtest_qom_set_uint32(s, path, "gpio-set[4]", 0x0); |
| 113 | g_assert(qtest_qom_get_uint32(s, path, "gpio-set[4]") == 0x0); |
| 114 | qtest_qom_set_uint32(s, path, "gpio-set[4]", 0xa000); |
| 115 | g_assert(qtest_qom_get_uint32(s, path, "gpio-set[4]") == 0xa000); |
| 116 | |
| 117 | g_assert(!qtest_qom_get_bool(s, path, "gpioR4")); |
| 118 | g_assert(qtest_qom_get_bool(s, path, "gpioR5")); |
| 119 | g_assert(!qtest_qom_get_bool(s, path, "gpioR6")); |
| 120 | g_assert(qtest_qom_get_bool(s, path, "gpioR7")); |
| 121 | |
| 122 | /* |
| 123 | * Testing the invalid indexing, the response info should contain following |
| 124 | * info: |
| 125 | * {key: "class", value: "GenericError"} |
| 126 | * |
| 127 | * For pins, it should follow "gpio%2[A-Z]%1d" or "gpio%3[18A-E]%1d" format. |
| 128 | */ |
| 129 | const char error_msg[] = "GenericError"; |
| 130 | const char error_msg_key[] = "class"; |
| 131 | |
| 132 | g_assert(qtest_qom_check_error(s, path, "gpioR+1", error_msg, |
| 133 | error_msg_key)); |
| 134 | g_assert(qtest_qom_check_error(s, path, "gpio-set[99]", error_msg, |
| 135 | error_msg_key)); |
| 136 | g_assert(qtest_qom_check_error(s, path, "gpio-set[-3]", error_msg, |
| 137 | error_msg_key)); |
| 138 | } |
| 139 | |
| 140 | static void test_set_input_pins(const void *data) |
| 141 | { |
| 142 | QTestState *s = (QTestState *)data; |
| 143 | uint32_t value; |
| 144 | |
| 145 | qtest_writel(s, AST2600_GPIO_BASE + GPIO_ABCD_DIRECTION, 0x00000000); |
| 146 | for (char c = 'A'; c <= 'D'; c++) { |
| 147 | for (int i = 0; i < 8; i++) { |
| 148 | g_autofree const char *name = g_strdup_printf("gpio%c%d", c, i); |
| 149 | |
| 150 | qtest_qom_set_bool(s, "/machine/soc/gpio", name, true); |
| 151 | } |
| 152 | } |
| 153 | value = qtest_readl(s, AST2600_GPIO_BASE + GPIO_ABCD_DATA_VALUE); |
| 154 | g_assert_cmphex(value, ==, 0xffffffff); |
| 155 | |
| 156 | qtest_writel(s, AST2600_GPIO_BASE + GPIO_ABCD_DATA_VALUE, 0x00000000); |
| 157 | value = qtest_readl(s, AST2600_GPIO_BASE + GPIO_ABCD_DATA_VALUE); |
| 158 | g_assert_cmphex(value, ==, 0xffffffff); |
| 159 | } |
| 160 | |
| 161 | int main(int argc, char **argv) |
| 162 | { |
| 163 | QTestState *s; |
| 164 | int r; |
| 165 | |
| 166 | g_test_init(&argc, &argv, NULL); |
| 167 | |
| 168 | s = qtest_init("-machine ast2600-evb"); |
| 169 | qtest_add_data_func("/ast2600/gpio/set_colocated_pins", s, |
| 170 | test_set_colocated_pins); |
| 171 | qtest_add_data_func("/ast2600/gpio/set_input_pins", s, test_set_input_pins); |
| 172 | r = g_test_run(); |
| 173 | qtest_quit(s); |
| 174 | |
| 175 | return r; |
| 176 | } |