1 /*
2 * Copyright (c) 2021 Google Inc
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT nuvoton_npcx_bbram
8
9 #include <zephyr/drivers/bbram.h>
10 #include <errno.h>
11 #include <zephyr/sys/util.h>
12
13 #include <zephyr/logging/log.h>
14 LOG_MODULE_REGISTER(npcx_bbram, CONFIG_BBRAM_LOG_LEVEL);
15
16 #include "npcx.h"
17
18 #define NPCX_STATUS_IBBR BIT(7)
19 #define NPCX_STATUS_VSBY BIT(1)
20 #define NPCX_STATUS_VCC1 BIT(0)
21
22 #define DRV_STATUS(dev) \
23 (*((volatile uint8_t *)((const struct bbram_npcx_config *)(dev)->config)->status_reg_addr))
24
get_bit_and_reset(const struct device * dev,int mask)25 static int get_bit_and_reset(const struct device *dev, int mask)
26 {
27 int result = DRV_STATUS(dev) & mask;
28
29 /*
30 * Clear the bit(s):
31 * For emulator, write 0 to clear status bit(s).
32 * For real chip, write 1 to clear status bit(s).
33 */
34 #ifdef CONFIG_BBRAM_NPCX_EMUL
35 DRV_STATUS(dev) &= ~mask;
36 #else
37 DRV_STATUS(dev) = mask;
38 #endif
39
40 return result;
41 }
42
bbram_npcx_check_invalid(const struct device * dev)43 static int bbram_npcx_check_invalid(const struct device *dev)
44 {
45 return get_bit_and_reset(dev, NPCX_STATUS_IBBR);
46 }
47
bbram_npcx_check_standby_power(const struct device * dev)48 static int bbram_npcx_check_standby_power(const struct device *dev)
49 {
50 return get_bit_and_reset(dev, NPCX_STATUS_VSBY);
51 }
52
bbram_npcx_check_power(const struct device * dev)53 static int bbram_npcx_check_power(const struct device *dev)
54 {
55 return get_bit_and_reset(dev, NPCX_STATUS_VCC1);
56 }
57
bbram_npcx_get_size(const struct device * dev,size_t * size)58 static int bbram_npcx_get_size(const struct device *dev, size_t *size)
59 {
60 const struct bbram_npcx_config *config = dev->config;
61
62 *size = config->size;
63 return 0;
64 }
65
bbram_npcx_read(const struct device * dev,size_t offset,size_t size,uint8_t * data)66 static int bbram_npcx_read(const struct device *dev, size_t offset, size_t size,
67 uint8_t *data)
68 {
69 const struct bbram_npcx_config *config = dev->config;
70
71 if (size < 1 || offset + size > config->size || bbram_npcx_check_invalid(dev)) {
72 return -EINVAL;
73 }
74
75
76 bytecpy(data, ((uint8_t *)config->base_addr + offset), size);
77 return 0;
78 }
79
bbram_npcx_write(const struct device * dev,size_t offset,size_t size,const uint8_t * data)80 static int bbram_npcx_write(const struct device *dev, size_t offset, size_t size,
81 const uint8_t *data)
82 {
83 const struct bbram_npcx_config *config = dev->config;
84
85 if (size < 1 || offset + size > config->size || bbram_npcx_check_invalid(dev)) {
86 return -EINVAL;
87 }
88
89 bytecpy(((uint8_t *)config->base_addr + offset), data, size);
90 return 0;
91 }
92
93 static const struct bbram_driver_api bbram_npcx_driver_api = {
94 .check_invalid = bbram_npcx_check_invalid,
95 .check_standby_power = bbram_npcx_check_standby_power,
96 .check_power = bbram_npcx_check_power,
97 .get_size = bbram_npcx_get_size,
98 .read = bbram_npcx_read,
99 .write = bbram_npcx_write,
100 };
101
102 #define BBRAM_INIT(inst) \
103 BBRAM_NPCX_DECL_CONFIG(inst); \
104 DEVICE_DT_INST_DEFINE(inst, NULL, NULL, NULL, &bbram_cfg_##inst, PRE_KERNEL_1, \
105 CONFIG_BBRAM_INIT_PRIORITY, &bbram_npcx_driver_api);
106
107 DT_INST_FOREACH_STATUS_OKAY(BBRAM_INIT);
108