summaryrefslogtreecommitdiff
path: root/drivers/bootcount/bootcount-uclass.c
blob: 0689db7a5bcc59c0a527d6f8076738399b0f2d16 (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
86
87
88
89
90
91
92
93
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2018 Theobroma Systems Design und Consulting GmbH
 */

#include <common.h>
#include <dm.h>
#include <errno.h>
#include <bootcount.h>

int dm_bootcount_get(struct udevice *dev, u32 *bootcount)
{
	struct bootcount_ops *ops = bootcount_get_ops(dev);

	assert(ops);
	if (!ops->get)
		return -ENOSYS;
	return ops->get(dev, bootcount);
}

int dm_bootcount_set(struct udevice *dev, const u32 bootcount)
{
	struct bootcount_ops *ops = bootcount_get_ops(dev);

	assert(ops);
	if (!ops->set)
		return -ENOSYS;
	return ops->set(dev, bootcount);
}

/* Now implement the generic default functions */
void bootcount_store(ulong val)
{
	struct udevice *dev = NULL;
	ofnode node;
	const char *propname = "u-boot,bootcount-device";
	int ret = -ENODEV;

	/*
	 * If there's a preferred bootcount device selected by the user (by
	 * setting '/chosen/u-boot,bootcount-device' in the DTS), try to use
	 * it if available.
	 */
	node = ofnode_get_chosen_node(propname);
	if (ofnode_valid(node))
		ret = uclass_get_device_by_ofnode(UCLASS_BOOTCOUNT, node, &dev);

	/* If there was no user-selected device, use the first available one */
	if (ret)
		ret = uclass_get_device(UCLASS_BOOTCOUNT, 0, &dev);

	if (dev)
		ret = dm_bootcount_set(dev, val);

	if (ret)
		pr_debug("%s: failed to store 0x%lx\n", __func__, val);
}

ulong bootcount_load(void)
{
	struct udevice *dev = NULL;
	ofnode node;
	const char *propname = "u-boot,bootcount-device";
	int ret = -ENODEV;
	u32 val;

	/*
	 * If there's a preferred bootcount device selected by the user (by
	 * setting '/chosen/u-boot,bootcount-device' in the DTS), try to use
	 * it if available.
	 */
	node = ofnode_get_chosen_node(propname);
	if (ofnode_valid(node))
		ret = uclass_get_device_by_ofnode(UCLASS_BOOTCOUNT, node, &dev);

	/* If there was no user-selected device, use the first available one */
	if (ret)
		ret = uclass_get_device(UCLASS_BOOTCOUNT, 0, &dev);

	if (dev)
		ret = dm_bootcount_get(dev, &val);

	if (ret)
		pr_debug("%s: failed to load bootcount\n", __func__);

	/* Return the 0, if the call to dm_bootcount_get failed */
	return ret ? 0 : val;
}

UCLASS_DRIVER(bootcount) = {
	.name		= "bootcount",
	.id		= UCLASS_BOOTCOUNT,
};