blob: c747c9ab276bb55a16a453a96656f6f6c4a70b3e [file] [log] [blame]
Philipp Tomsichebb73de2018-11-27 23:00:18 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018 Theobroma Systems Design und Consulting GmbH
4 */
5
Patrick Delaunayb953ec22021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_BOOTCOUNT
7
Philipp Tomsichebb73de2018-11-27 23:00:18 +01008#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <bootcount.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Philipp Tomsichebb73de2018-11-27 23:00:18 +010013
14int 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
24int 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 */
35void 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
62ulong 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
93UCLASS_DRIVER(bootcount) = {
94 .name = "bootcount",
95 .id = UCLASS_BOOTCOUNT,
96};