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