1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2018 Theobroma Systems Design und Consulting GmbH
4  */
5 
6 #define LOG_CATEGORY UCLASS_BOOTCOUNT
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <bootcount.h>
12 #include <log.h>
13 
dm_bootcount_get(struct udevice * dev,u32 * bootcount)14 int dm_bootcount_get(struct udevice *dev, u32 *bootcount)
15 {
16 	struct bootcount_ops *ops = bootcount_get_ops(dev);
17 
18 	assert(ops);
19 	if (!ops->get)
20 		return -ENOSYS;
21 	return ops->get(dev, bootcount);
22 }
23 
dm_bootcount_set(struct udevice * dev,const u32 bootcount)24 int dm_bootcount_set(struct udevice *dev, const u32 bootcount)
25 {
26 	struct bootcount_ops *ops = bootcount_get_ops(dev);
27 
28 	assert(ops);
29 	if (!ops->set)
30 		return -ENOSYS;
31 	return ops->set(dev, bootcount);
32 }
33 
34 /* Now implement the generic default functions */
bootcount_store(ulong val)35 void bootcount_store(ulong val)
36 {
37 	struct udevice *dev = NULL;
38 	ofnode node;
39 	const char *propname = "u-boot,bootcount-device";
40 	int ret = -ENODEV;
41 
42 	/*
43 	 * If there's a preferred bootcount device selected by the user (by
44 	 * setting '/chosen/u-boot,bootcount-device' in the DTS), try to use
45 	 * it if available.
46 	 */
47 	node = ofnode_get_chosen_node(propname);
48 	if (ofnode_valid(node))
49 		ret = uclass_get_device_by_ofnode(UCLASS_BOOTCOUNT, node, &dev);
50 
51 	/* If there was no user-selected device, use the first available one */
52 	if (ret)
53 		ret = uclass_get_device(UCLASS_BOOTCOUNT, 0, &dev);
54 
55 	if (dev)
56 		ret = dm_bootcount_set(dev, val);
57 
58 	if (ret)
59 		pr_debug("%s: failed to store 0x%lx\n", __func__, val);
60 }
61 
bootcount_load(void)62 ulong bootcount_load(void)
63 {
64 	struct udevice *dev = NULL;
65 	ofnode node;
66 	const char *propname = "u-boot,bootcount-device";
67 	int ret = -ENODEV;
68 	u32 val;
69 
70 	/*
71 	 * If there's a preferred bootcount device selected by the user (by
72 	 * setting '/chosen/u-boot,bootcount-device' in the DTS), try to use
73 	 * it if available.
74 	 */
75 	node = ofnode_get_chosen_node(propname);
76 	if (ofnode_valid(node))
77 		ret = uclass_get_device_by_ofnode(UCLASS_BOOTCOUNT, node, &dev);
78 
79 	/* If there was no user-selected device, use the first available one */
80 	if (ret)
81 		ret = uclass_get_device(UCLASS_BOOTCOUNT, 0, &dev);
82 
83 	if (dev)
84 		ret = dm_bootcount_get(dev, &val);
85 
86 	if (ret)
87 		pr_debug("%s: failed to load bootcount\n", __func__);
88 
89 	/* Return the 0, if the call to dm_bootcount_get failed */
90 	return ret ? 0 : val;
91 }
92 
93 UCLASS_DRIVER(bootcount) = {
94 	.name		= "bootcount",
95 	.id		= UCLASS_BOOTCOUNT,
96 };
97