1 /*
2 * Copyright (c) 2023, Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT nordic_nrf_gpregret
8
9 #include <string.h>
10 #include <zephyr/device.h>
11 #include <zephyr/devicetree.h>
12 #include <zephyr/drivers/retained_mem.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/sys/util.h>
15 #include <zephyr/logging/log.h>
16
17 LOG_MODULE_REGISTER(retained_mem_nrf_gpregret, CONFIG_RETAINED_MEM_LOG_LEVEL);
18
19 #ifdef CONFIG_RETAINED_MEM_MUTEXES
20 struct nrf_gpregret_data {
21 struct k_mutex lock;
22 };
23 #endif
24
25 struct nrf_gpregret_config {
26 uint8_t *addr;
27 size_t size;
28 };
29
nrf_gpregret_lock_take(const struct device * dev)30 static inline void nrf_gpregret_lock_take(const struct device *dev)
31 {
32 #ifdef CONFIG_RETAINED_MEM_MUTEXES
33 struct nrf_gpregret_data *data = dev->data;
34
35 k_mutex_lock(&data->lock, K_FOREVER);
36 #else
37 ARG_UNUSED(dev);
38 #endif
39 }
40
nrf_gpregret_lock_release(const struct device * dev)41 static inline void nrf_gpregret_lock_release(const struct device *dev)
42 {
43 #ifdef CONFIG_RETAINED_MEM_MUTEXES
44 struct nrf_gpregret_data *data = dev->data;
45
46 k_mutex_unlock(&data->lock);
47 #else
48 ARG_UNUSED(dev);
49 #endif
50 }
51
nrf_gpregret_init(const struct device * dev)52 static int nrf_gpregret_init(const struct device *dev)
53 {
54 #ifdef CONFIG_RETAINED_MEM_MUTEXES
55 struct nrf_gpregret_data *data = dev->data;
56
57 k_mutex_init(&data->lock);
58 #endif
59
60 return 0;
61 }
62
nrf_gpregret_size(const struct device * dev)63 static ssize_t nrf_gpregret_size(const struct device *dev)
64 {
65 const struct nrf_gpregret_config *config = dev->config;
66
67 return (ssize_t)config->size;
68 }
69
nrf_gpregret_read(const struct device * dev,off_t offset,uint8_t * buffer,size_t size)70 static int nrf_gpregret_read(const struct device *dev, off_t offset, uint8_t *buffer, size_t size)
71 {
72 const struct nrf_gpregret_config *config = dev->config;
73
74 nrf_gpregret_lock_take(dev);
75
76 memcpy(buffer, (config->addr + offset), size);
77
78 nrf_gpregret_lock_release(dev);
79
80 return 0;
81 }
82
nrf_gpregret_write(const struct device * dev,off_t offset,const uint8_t * buffer,size_t size)83 static int nrf_gpregret_write(const struct device *dev, off_t offset, const uint8_t *buffer,
84 size_t size)
85 {
86 const struct nrf_gpregret_config *config = dev->config;
87
88 nrf_gpregret_lock_take(dev);
89
90 memcpy((config->addr + offset), buffer, size);
91
92 nrf_gpregret_lock_release(dev);
93
94 return 0;
95 }
96
nrf_gpregret_clear(const struct device * dev)97 static int nrf_gpregret_clear(const struct device *dev)
98 {
99 const struct nrf_gpregret_config *config = dev->config;
100
101 nrf_gpregret_lock_take(dev);
102
103 memset(config->addr, 0, config->size);
104
105 nrf_gpregret_lock_release(dev);
106
107 return 0;
108 }
109
110 static const struct retained_mem_driver_api nrf_gpregret_api = {
111 .size = nrf_gpregret_size,
112 .read = nrf_gpregret_read,
113 .write = nrf_gpregret_write,
114 .clear = nrf_gpregret_clear,
115 };
116
117 #define NRF_GPREGRET_DEVICE(inst) \
118 IF_ENABLED(CONFIG_RETAINED_MEM_MUTEXES, \
119 (static struct nrf_gpregret_data nrf_gpregret_data_##inst;) \
120 ) \
121 static const struct nrf_gpregret_config nrf_gpregret_config_##inst = { \
122 .addr = (uint8_t *)DT_INST_REG_ADDR(inst), \
123 .size = DT_INST_REG_SIZE(inst), \
124 }; \
125 DEVICE_DT_INST_DEFINE(inst, \
126 &nrf_gpregret_init, \
127 NULL, \
128 COND_CODE_1(CONFIG_RETAINED_MEM_MUTEXES, \
129 (&nrf_gpregret_data_##inst), (NULL) \
130 ), \
131 &nrf_gpregret_config_##inst, \
132 POST_KERNEL, \
133 CONFIG_RETAINED_MEM_INIT_PRIORITY, \
134 &nrf_gpregret_api);
135
136 DT_INST_FOREACH_STATUS_OKAY(NRF_GPREGRET_DEVICE)
137