1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 Theobroma Systems Design und Consulting GmbH
6 #define LOG_CATEGORY UCLASS_BOOTCOUNT
10 #include <bootcount.h>
12 #include <linux/printk.h>
14 int dm_bootcount_get(struct udevice *dev, u32 *bootcount)
16 struct bootcount_ops *ops = bootcount_get_ops(dev);
21 return ops->get(dev, bootcount);
24 int dm_bootcount_set(struct udevice *dev, const u32 bootcount)
26 struct bootcount_ops *ops = bootcount_get_ops(dev);
31 return ops->set(dev, bootcount);
34 /* Now implement the generic default functions */
35 void bootcount_store(ulong val)
37 struct udevice *dev = NULL;
39 const char *propname = "u-boot,bootcount-device";
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
47 node = ofnode_get_chosen_node(propname);
48 if (ofnode_valid(node))
49 ret = uclass_get_device_by_ofnode(UCLASS_BOOTCOUNT, node, &dev);
51 /* If there was no user-selected device, use the first available one */
53 ret = uclass_get_device(UCLASS_BOOTCOUNT, 0, &dev);
56 ret = dm_bootcount_set(dev, val);
59 pr_debug("%s: failed to store 0x%lx\n", __func__, val);
62 ulong bootcount_load(void)
64 struct udevice *dev = NULL;
66 const char *propname = "u-boot,bootcount-device";
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
75 node = ofnode_get_chosen_node(propname);
76 if (ofnode_valid(node))
77 ret = uclass_get_device_by_ofnode(UCLASS_BOOTCOUNT, node, &dev);
79 /* If there was no user-selected device, use the first available one */
81 ret = uclass_get_device(UCLASS_BOOTCOUNT, 0, &dev);
84 ret = dm_bootcount_get(dev, &val);
87 pr_debug("%s: failed to load bootcount\n", __func__);
89 /* Return the 0, if the call to dm_bootcount_get failed */
93 UCLASS_DRIVER(bootcount) = {
95 .id = UCLASS_BOOTCOUNT,