summaryrefslogtreecommitdiff
path: root/zephyr/shim/chip/it8xxx2/system.c
blob: bb68a77b38d93d9b4b035e28776131d0022c901a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Copyright 2021 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <drivers/cros_bbram.h>
#include <logging/log.h>

#include "system.h"

#define GET_BBRAM_OFFSET(node) \
	DT_PROP(DT_PATH(named_bbram_regions, node), offset)
#define GET_BBRAM_SIZE(node) DT_PROP(DT_PATH(named_bbram_regions, node), size)

LOG_MODULE_REGISTER(shim_ite_system, LOG_LEVEL_ERR);

const struct device *bbram_dev;

void chip_save_reset_flags(uint32_t flags)
{
	if (bbram_dev == NULL) {
		LOG_ERR("bbram_dev doesn't have a binding");
		return;
	}

	cros_bbram_write(bbram_dev, GET_BBRAM_OFFSET(saved_reset_flags),
			 GET_BBRAM_SIZE(saved_reset_flags), (uint8_t *)&flags);
}

uint32_t chip_read_reset_flags(void)
{
	uint32_t flags = 0;

	if (bbram_dev == NULL) {
		LOG_ERR("bbram_dev doesn't have a binding");
		return 0;
	}

	cros_bbram_read(bbram_dev, GET_BBRAM_OFFSET(saved_reset_flags),
			GET_BBRAM_SIZE(saved_reset_flags), (uint8_t *)&flags);

	return flags;
}

int system_set_scratchpad(uint32_t value)
{
	if (bbram_dev == NULL) {
		LOG_ERR("bbram_dev doesn't have a binding");
		return 0;
	}

	cros_bbram_write(bbram_dev, GET_BBRAM_OFFSET(scratchpad),
			 GET_BBRAM_SIZE(scratchpad), (uint8_t *)&value);

	return EC_SUCCESS;
}

uint32_t system_get_scratchpad(void)
{
	uint32_t value = 0;

	if (bbram_dev == NULL) {
		LOG_ERR("bbram_dev doesn't have a binding");
		return 0;
	}

	cros_bbram_read(bbram_dev, GET_BBRAM_OFFSET(scratchpad),
			GET_BBRAM_SIZE(scratchpad), (uint8_t *)&value);

	return value;
}

static int chip_system_init(const struct device *unused)
{
	ARG_UNUSED(unused);

	bbram_dev = device_get_binding(DT_LABEL(DT_NODELABEL(bbram)));
	if (!bbram_dev) {
		LOG_ERR("bbram_dev get binding failed");
		return -1;
	}

	return 0;
}
SYS_INIT(chip_system_init, PRE_KERNEL_1, 15);