1 /*
2 * Copyright (c) 2024 Google Inc
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #include <zephyr/devicetree.h>
7 #include <zephyr/drivers/emul.h>
8 #include <zephyr/drivers/emul_bbram.h>
9
10 #include "it8xxx2.h"
11
12 #define DT_DRV_COMPAT ite_it8xxx2_bbram
13
14 struct bbram_it8xxx2_emul_config {
15 const struct device *dev;
16 };
17
18 #define GET_CONFIG(target) \
19 ((const struct bbram_it8xxx2_config \
20 *)(((const struct bbram_it8xxx2_emul_config *)((target)->cfg))->dev->config))
21
it8xxx2_emul_backend_set_data(const struct emul * target,size_t offset,size_t count,const uint8_t * buffer)22 static int it8xxx2_emul_backend_set_data(const struct emul *target, size_t offset, size_t count,
23 const uint8_t *buffer)
24 {
25 const struct bbram_it8xxx2_config *config = GET_CONFIG(target);
26
27 if (offset + count > config->size) {
28 return -ERANGE;
29 }
30
31 bytecpy(((uint8_t *)config->base_addr + offset), buffer, count);
32 return 0;
33 }
34
it8xxx2_emul_backend_get_data(const struct emul * target,size_t offset,size_t count,uint8_t * buffer)35 static int it8xxx2_emul_backend_get_data(const struct emul *target, size_t offset, size_t count,
36 uint8_t *buffer)
37 {
38 const struct bbram_it8xxx2_config *config = GET_CONFIG(target);
39
40 if (offset + count > config->size) {
41 return -ERANGE;
42 }
43
44 bytecpy(buffer, ((uint8_t *)config->base_addr + offset), count);
45 return 0;
46 }
47
48 static const struct emul_bbram_driver_api it8xxx2_emul_backend_api = {
49 .set_data = it8xxx2_emul_backend_set_data,
50 .get_data = it8xxx2_emul_backend_get_data,
51 };
52
53 #define BBRAM_EMUL_INIT(inst) \
54 static struct bbram_it8xxx2_emul_config bbram_it8xxx2_emul_config_##inst = { \
55 .dev = DEVICE_DT_INST_GET(inst), \
56 }; \
57 EMUL_DT_INST_DEFINE(inst, NULL, NULL, &bbram_it8xxx2_emul_config_##inst, NULL, \
58 &it8xxx2_emul_backend_api)
59
60 DT_INST_FOREACH_STATUS_OKAY(BBRAM_EMUL_INIT);
61