1 /*
2  * Copyright (c) 2021 Microchip Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT microchip_xec_bbram
8 
9 #include <zephyr/drivers/bbram.h>
10 #include <errno.h>
11 #include <soc.h>
12 #include <zephyr/sys/util.h>
13 
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(bbram, CONFIG_BBRAM_LOG_LEVEL);
16 
17 /** Device config */
18 struct bbram_xec_config {
19 	/** BBRAM base address */
20 	uint8_t *base;
21 	/** BBRAM size (Unit:bytes) */
22 	int size;
23 };
24 
bbram_xec_check_invalid(const struct device * dev)25 static int bbram_xec_check_invalid(const struct device *dev)
26 {
27 	struct vbatr_regs *const regs = (struct vbatr_regs *)(DT_REG_ADDR_BY_NAME(
28 					DT_NODELABEL(pcr), vbatr));
29 
30 	if (regs->PFRS & BIT(MCHP_VBATR_PFRS_VBAT_RST_POS)) {
31 		regs->PFRS |= BIT(MCHP_VBATR_PFRS_VBAT_RST_POS);
32 		LOG_ERR("VBAT power rail failure");
33 		return -EFAULT;
34 	}
35 
36 	return 0;
37 }
38 
bbram_xec_get_size(const struct device * dev,size_t * size)39 static int bbram_xec_get_size(const struct device *dev, size_t *size)
40 {
41 	const struct bbram_xec_config *dcfg = dev->config;
42 
43 	*size = dcfg->size;
44 	return 0;
45 }
46 
bbram_xec_read(const struct device * dev,size_t offset,size_t size,uint8_t * data)47 static int bbram_xec_read(const struct device *dev, size_t offset, size_t size,
48 			  uint8_t *data)
49 {
50 	const struct bbram_xec_config *dcfg = dev->config;
51 
52 	if (size < 1 || offset + size > dcfg->size) {
53 		LOG_ERR("Invalid params");
54 		return -EFAULT;
55 	}
56 
57 	bytecpy(data, dcfg->base + offset, size);
58 	return 0;
59 }
60 
bbram_xec_write(const struct device * dev,size_t offset,size_t size,const uint8_t * data)61 static int bbram_xec_write(const struct device *dev, size_t offset, size_t size,
62 			       const uint8_t *data)
63 {
64 	const struct bbram_xec_config *dcfg = dev->config;
65 
66 	if (size < 1 || offset + size > dcfg->size) {
67 		LOG_ERR("Invalid params");
68 		return -EFAULT;
69 	}
70 
71 	bytecpy(dcfg->base + offset, data, size);
72 	return 0;
73 }
74 
75 static const struct bbram_driver_api bbram_xec_driver_api = {
76 	.check_invalid = bbram_xec_check_invalid,
77 	.get_size = bbram_xec_get_size,
78 	.read = bbram_xec_read,
79 	.write = bbram_xec_write,
80 };
81 
82 #define BBRAM_INIT(inst)						\
83 	static const struct bbram_xec_config bbram_cfg_##inst = {	\
84 		.base = (uint8_t *)(DT_INST_REG_ADDR(inst)),		\
85 		.size = DT_INST_REG_SIZE(inst),				\
86 	};								\
87 	DEVICE_DT_INST_DEFINE(inst, NULL, NULL, NULL, &bbram_cfg_##inst,\
88 			      PRE_KERNEL_1, CONFIG_BBRAM_INIT_PRIORITY,	\
89 			      &bbram_xec_driver_api);
90 
91 DT_INST_FOREACH_STATUS_OKAY(BBRAM_INIT);
92