Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | /* * Copyright (c) 2022 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/ztest.h> #include <zephyr/tc_util.h> #include <zephyr/sys/byteorder.h> #include <zephyr/random/random.h> #include <zephyr/drivers/pcie/pcie.h> #include <zephyr/drivers/smbus.h> #include "emul.h" #define CONFIG_SMBUS_LOG_LEVEL LOG_LEVEL_DBG #define PERIPH_ADDR 0x10 static uint8_t mock_sys_in8(io_port_t port) { return emul_in8(port); } static void mock_sys_out8(uint8_t data, io_port_t port) { emul_out8(data, port); } static uint32_t mock_conf_read(pcie_bdf_t bdf, unsigned int reg) { return emul_pci_read(reg); } #if defined(PCIE_CONF_WRITE) static void mock_conf_write(pcie_bdf_t bdf, unsigned int reg, uint32_t data) { emul_pci_write(bdf, reg, data); } #define pcie_conf_write(bdf, reg, val) mock_conf_write(bdf, reg, val) #endif /* PCIE_CONF_WRITE */ /* Redefine PCIE access */ #define pcie_conf_read(bdf, reg) mock_conf_read(bdf, reg) /* Redefine sys_in function */ #define sys_in8(port) mock_sys_in8(port) #define sys_out8(data, port) mock_sys_out8(data, port) #define CONFIG_SMBUS_INTEL_PCH_ACCESS_IO #define device_map(a, b, c, d) #define pcie_probe(bdf, id) 1 #define pcie_set_cmd(a, b, c) #define SMBUS_EMUL "smbus_emul" #ifdef PERIPHERAL_INT #define CONFIG_SMBUS_INTEL_PCH_SMBALERT 1 #define CONFIG_SMBUS_INTEL_PCH_HOST_NOTIFY 1 #endif #include "intel_pch_smbus.c" void run_isr(enum emul_isr_type type) { const struct device *const dev = device_get_binding(SMBUS_EMUL); switch (type) { case EMUL_SMBUS_INTR: emul_set_io(emul_get_io(PCH_SMBUS_HSTS) | PCH_SMBUS_HSTS_INTERRUPT, PCH_SMBUS_HSTS); break; case EMUL_SMBUS_SMBALERT: emul_set_io(emul_get_io(PCH_SMBUS_HSTS) | PCH_SMBUS_HSTS_SMB_ALERT, PCH_SMBUS_HSTS); break; case EMUL_SMBUS_HOST_NOTIFY: emul_set_io(emul_get_io(PCH_SMBUS_SSTS)| PCH_SMBUS_SSTS_HNS, PCH_SMBUS_SSTS); peripheral_handle_host_notify(); break; default: break; } smbus_isr(dev); } static void config_function(const struct device *dev) { TC_PRINT("Emulator device configuration\n"); } static struct pch_data smbus_data; /* Zero initialized, dummy device does not care about pcie ids */ static struct pcie_dev pcie_params; static struct pch_config pch_config_data = { .config_func = config_function, .pcie = &pcie_params, }; DEVICE_DEFINE(dummy_driver, SMBUS_EMUL, &pch_smbus_init, NULL, &smbus_data, &pch_config_data, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &funcs); ZTEST(test_smbus_emul, test_byte) { const struct device *const dev = device_get_binding(SMBUS_EMUL); uint8_t snd_byte, rcv_byte; int ret; zassert_not_null(dev, "Device not found"); ret = smbus_quick(dev, PERIPH_ADDR, 1); zassert_ok(ret, "SMBus Quick failed"); snd_byte = (uint8_t)sys_rand32_get(); ret = smbus_byte_write(dev, PERIPH_ADDR, snd_byte); zassert_ok(ret, "SMBus Byte Write failed"); ret = smbus_byte_read(dev, PERIPH_ADDR, &rcv_byte); zassert_ok(ret, "SMBus Byte Read failed"); zassert_equal(snd_byte, rcv_byte, "Data mismatch"); ret = smbus_byte_data_write(dev, PERIPH_ADDR, 0, snd_byte); zassert_ok(ret, "SMBus Byte Data Write failed"); ret = smbus_byte_data_read(dev, PERIPH_ADDR, 0, &rcv_byte); zassert_ok(ret, "SMBus Byte Data Read failed"); zassert_equal(snd_byte, rcv_byte, "Data mismatch"); } ZTEST(test_smbus_emul, test_word) { const struct device *const dev = device_get_binding(SMBUS_EMUL); uint16_t snd_word, rcv_word; uint8_t snd_byte; int ret; zassert_not_null(dev, "Device not found"); snd_word = (uint16_t)sys_rand32_get(); ret = smbus_word_data_write(dev, PERIPH_ADDR, 0, snd_word); zassert_ok(ret, "SMBus Word Data Write failed"); ret = smbus_word_data_read(dev, PERIPH_ADDR, 0, &rcv_word); zassert_ok(ret, "SMBus Byte Data Read failed"); zassert_equal(snd_word, rcv_word, "Data mismatch"); /* Test 2 byte writes following word read */ snd_byte = (uint8_t)sys_rand32_get(); ret = smbus_byte_data_write(dev, PERIPH_ADDR, 0, snd_byte); zassert_ok(ret, "SMBus Byte Data Write failed"); ret = smbus_byte_data_write(dev, PERIPH_ADDR, 1, snd_byte); zassert_ok(ret, "SMBus Byte Data Write failed"); ret = smbus_word_data_read(dev, PERIPH_ADDR, 0, &rcv_word); zassert_ok(ret, "SMBus Byte Data Read failed"); zassert_equal(snd_byte << 8 | snd_byte, rcv_word, "Data mismatch"); } ZTEST(test_smbus_emul, test_proc_call) { const struct device *const dev = device_get_binding(SMBUS_EMUL); uint16_t snd_word, rcv_word; int ret; zassert_not_null(dev, "Device not found"); snd_word = (uint16_t)sys_rand32_get(); zassert_not_equal(snd_word, 0, "Random number generator misconfgured"); ret = smbus_pcall(dev, PERIPH_ADDR, 0x0, snd_word, &rcv_word); zassert_ok(ret, "SMBus Proc Call failed"); /* Our emulated Proc Call swaps bytes */ zassert_equal(snd_word, __bswap_16(rcv_word), "Data mismatch"); } ZTEST(test_smbus_emul, test_block) { const struct device *const dev = device_get_binding(SMBUS_EMUL); uint8_t snd_block[SMBUS_BLOCK_BYTES_MAX]; uint8_t rcv_block[SMBUS_BLOCK_BYTES_MAX]; uint8_t snd_count, rcv_count; int ret; zassert_not_null(dev, "Device not found"); for (int i = 0; i < sizeof(snd_block); i++) { snd_block[i] = (uint8_t)sys_rand32_get(); } snd_count = sizeof(snd_block); ret = smbus_block_write(dev, PERIPH_ADDR, 0, snd_count, snd_block); zassert_ok(ret, "SMBUS write block failed, ret %d", ret); ret = smbus_block_read(dev, PERIPH_ADDR, 0, &rcv_count, rcv_block); zassert_ok(ret, "SMBUS read block failed, ret %d", ret); zassert_equal(snd_count, rcv_count, "Block count differs"); zassert_true(!memcmp(snd_block, rcv_block, rcv_count), "Data mismatch"); } ZTEST(test_smbus_emul, test_block_pcall) { const struct device *const dev = device_get_binding(SMBUS_EMUL); uint8_t snd_block[SMBUS_BLOCK_BYTES_MAX]; uint8_t rcv_block[SMBUS_BLOCK_BYTES_MAX]; uint8_t snd_count, rcv_count; int ret; zassert_not_null(dev, "Device not found"); for (int i = 0; i < sizeof(snd_block); i++) { snd_block[i] = (uint8_t)sys_rand32_get(); } snd_count = SMBUS_BLOCK_BYTES_MAX / 2; ret = smbus_block_pcall(dev, PERIPH_ADDR, 0, snd_count, snd_block, &rcv_count, rcv_block); zassert_ok(ret, "SMBUS block pcall failed, ret %d", ret); zassert_equal(snd_count, rcv_count, "Block count differs"); /** * Verify that our emulated peripheral swapped bytes in the block * buffer */ for (int i = 0; i < rcv_count; i++) { zassert_equal(snd_block[i], rcv_block[rcv_count - (i + 1)], "Data mismatch, not swapped"); } } /* SMBALERT handling */ /* False by default */ bool smbalert_handled; static void smbalert_cb(const struct device *dev, struct smbus_callback *cb, uint8_t addr) { LOG_DBG("SMBALERT callback"); smbalert_handled = true; } struct smbus_callback smbalert_callback = { .handler = smbalert_cb, .addr = PERIPH_ADDR, }; /* Host Notify handling */ /* False by default */ bool notify_handled; static void notify_cb(const struct device *dev, struct smbus_callback *cb, uint8_t addr) { LOG_DBG("Notify callback"); notify_handled = true; } struct smbus_callback notify_callback = { .handler = notify_cb, .addr = PERIPH_ADDR, }; /* Setup peripheral SMBus device on a bus */ struct smbus_peripheral peripheral = { .addr = PERIPH_ADDR, .smbalert = true, .host_notify = true, }; ZTEST(test_smbus_emul, test_alert) { const struct device *const dev = device_get_binding(SMBUS_EMUL); int ret; Z_TEST_SKIP_IFNDEF(CONFIG_SMBUS_INTEL_PCH_SMBALERT); zassert_not_null(dev, "Device not found"); /* Try to remove not existing callback */ ret = smbus_smbalert_remove_cb(dev, &smbalert_callback); zassert_equal(ret, -ENOENT, "Callback remove failed"); /* Set callback */ ret = smbus_smbalert_set_cb(dev, &smbalert_callback); zassert_ok(ret, "Callback set failed"); /* Emulate SMBus alert from peripheral device */ peripheral_clear_smbalert(&peripheral); smbalert_handled = false; /* Run without configure smbalert */ run_isr(EMUL_SMBUS_SMBALERT); /* Wait for delayed work handled */ k_sleep(K_MSEC(100)); /* Verify that smbalert is NOT handled */ zassert_false(smbalert_handled, "smbalert is not handled"); /* Now enable smbalert */ ret = smbus_configure(dev, SMBUS_MODE_CONTROLLER | SMBUS_MODE_SMBALERT); zassert_ok(ret, "Configure failed"); /* Emulate SMBus alert again */ run_isr(EMUL_SMBUS_SMBALERT); /* Wait for delayed work handled */ k_sleep(K_MSEC(100)); /* Verify that smbalert is not handled */ zassert_true(smbalert_handled, "smbalert is not handled"); } ZTEST(test_smbus_emul, test_host_notify) { const struct device *const dev = device_get_binding(SMBUS_EMUL); int ret; Z_TEST_SKIP_IFNDEF(CONFIG_SMBUS_INTEL_PCH_HOST_NOTIFY); zassert_not_null(dev, "Device not found"); /* Try to remove not existing callback */ ret = smbus_host_notify_remove_cb(dev, ¬ify_callback); zassert_equal(ret, -ENOENT, "Callback remove failed"); /* Set callback */ ret = smbus_host_notify_set_cb(dev, ¬ify_callback); zassert_ok(ret, "Callback set failed"); /* Emulate SMBus alert from peripheral device */ notify_handled = false; /* Run without configuring Host Notify */ run_isr(EMUL_SMBUS_HOST_NOTIFY); /* Wait for delayed work handled */ k_sleep(K_MSEC(100)); /* Verify that smbalert is NOT handled */ zassert_false(notify_handled, "smbalert is not handled"); /* Now enable smbalert */ ret = smbus_configure(dev, SMBUS_MODE_CONTROLLER | SMBUS_MODE_HOST_NOTIFY); zassert_ok(ret, "Configure failed"); /* Emulate SMBus alert again */ run_isr(EMUL_SMBUS_HOST_NOTIFY); /* Wait for delayed work handled */ k_sleep(K_MSEC(100)); /* Verify that smbalert is handled */ zassert_true(notify_handled, "smbalert is not handled"); } /* Test setup function */ static void *smbus_emul_setup(void) { emul_register_smbus_peripheral(&peripheral); return NULL; } ZTEST_SUITE(test_smbus_emul, NULL, smbus_emul_setup, NULL, NULL, NULL); |