1 /*
2 * Copyright 2021 Google LLC
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #define DT_DRV_COMPAT zephyr_bbram_emul
7
8 #include <zephyr/drivers/bbram.h>
9 #include <string.h>
10
11 #include <zephyr/logging/log.h>
12 LOG_MODULE_REGISTER(bbram, CONFIG_BBRAM_LOG_LEVEL);
13
14 /** Device config */
15 struct bbram_emul_config {
16 /** BBRAM size (Unit:bytes) */
17 int size;
18 };
19
20 /** Device data */
21 struct bbram_emul_data {
22 /** Memory */
23 uint8_t *data;
24 /** Status register */
25 struct {
26 /** True if BBRAM is in an invalid state */
27 uint8_t is_invalid : 1;
28 /** True if BBRAM incurred a standby power failure */
29 uint8_t standby_failure : 1;
30 /** True if BBRAM incurred a power failure */
31 uint8_t power_failure : 1;
32 } status;
33 };
34
bbram_emul_set_invalid(const struct device * dev,bool is_invalid)35 int bbram_emul_set_invalid(const struct device *dev, bool is_invalid)
36 {
37 struct bbram_emul_data *data = dev->data;
38
39 data->status.is_invalid = is_invalid;
40 return 0;
41 }
42
bbram_emul_set_standby_power_state(const struct device * dev,bool failure)43 int bbram_emul_set_standby_power_state(const struct device *dev, bool failure)
44 {
45 struct bbram_emul_data *data = dev->data;
46
47 data->status.standby_failure = failure;
48 return 0;
49 }
50
bbram_emul_set_power_state(const struct device * dev,bool failure)51 int bbram_emul_set_power_state(const struct device *dev, bool failure)
52 {
53 struct bbram_emul_data *data = dev->data;
54
55 data->status.power_failure = failure;
56 return 0;
57 }
58
bbram_emul_check_invalid(const struct device * dev)59 static int bbram_emul_check_invalid(const struct device *dev)
60 {
61 struct bbram_emul_data *data = dev->data;
62 bool is_invalid = data->status.is_invalid;
63
64 data->status.is_invalid = false;
65 return is_invalid;
66 }
67
bbram_emul_check_standby_power(const struct device * dev)68 static int bbram_emul_check_standby_power(const struct device *dev)
69 {
70 struct bbram_emul_data *data = dev->data;
71 bool failure = data->status.standby_failure;
72
73 data->status.standby_failure = false;
74 return failure;
75 }
76
bbram_emul_check_power(const struct device * dev)77 static int bbram_emul_check_power(const struct device *dev)
78 {
79 struct bbram_emul_data *data = dev->data;
80 bool failure = data->status.power_failure;
81
82 data->status.power_failure = false;
83 return failure;
84 }
85
bbram_emul_get_size(const struct device * dev,size_t * size)86 static int bbram_emul_get_size(const struct device *dev, size_t *size)
87 {
88 const struct bbram_emul_config *config = dev->config;
89
90 *size = config->size;
91 return 0;
92 }
93
bbram_emul_read(const struct device * dev,size_t offset,size_t size,uint8_t * data)94 static int bbram_emul_read(const struct device *dev, size_t offset, size_t size,
95 uint8_t *data)
96 {
97 const struct bbram_emul_config *config = dev->config;
98 struct bbram_emul_data *dev_data = dev->data;
99
100 if (size < 1 || offset + size > config->size || bbram_emul_check_invalid(dev)) {
101 return -EFAULT;
102 }
103
104 memcpy(data, dev_data->data + offset, size);
105 return 0;
106 }
107
bbram_emul_write(const struct device * dev,size_t offset,size_t size,const uint8_t * data)108 static int bbram_emul_write(const struct device *dev, size_t offset, size_t size,
109 const uint8_t *data)
110 {
111 const struct bbram_emul_config *config = dev->config;
112 struct bbram_emul_data *dev_data = dev->data;
113
114 if (size < 1 || offset + size > config->size || bbram_emul_check_invalid(dev)) {
115 return -EFAULT;
116 }
117
118 memcpy(dev_data->data + offset, data, size);
119 return 0;
120 }
121
122 static const struct bbram_driver_api bbram_emul_driver_api = {
123 .check_invalid = bbram_emul_check_invalid,
124 .check_standby_power = bbram_emul_check_standby_power,
125 .check_power = bbram_emul_check_power,
126 .get_size = bbram_emul_get_size,
127 .read = bbram_emul_read,
128 .write = bbram_emul_write,
129 };
130
131 #define BBRAM_INIT(inst) \
132 static uint8_t bbram_emul_mem_##inst[DT_INST_PROP(inst, size)]; \
133 static struct bbram_emul_data bbram_emul_data_##inst = { \
134 .data = bbram_emul_mem_##inst, \
135 }; \
136 static struct bbram_emul_config bbram_emul_config_##inst = { \
137 .size = DT_INST_PROP(inst, size), \
138 }; \
139 DEVICE_DT_INST_DEFINE(inst, NULL, NULL, &bbram_emul_data_##inst, \
140 &bbram_emul_config_##inst, PRE_KERNEL_1, CONFIG_BBRAM_INIT_PRIORITY, \
141 &bbram_emul_driver_api);
142
143 DT_INST_FOREACH_STATUS_OKAY(BBRAM_INIT);
144