blob: 483caaa80df400d11c4796d7ac45d55c96801bb1 [file] [log] [blame]
Philipp Tomsich482734a2018-11-27 23:00:19 +01001// 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 <bootcount.h>
8#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Philipp Tomsich482734a2018-11-27 23:00:19 +010010#include <rtc.h>
11
12static const u8 bootcount_magic = 0xbc;
13
14struct bootcount_rtc_priv {
15 struct udevice *rtc;
16 u32 offset;
17};
18
19static int bootcount_rtc_set(struct udevice *dev, const u32 a)
20{
21 struct bootcount_rtc_priv *priv = dev_get_priv(dev);
22 const u16 val = bootcount_magic << 8 | (a & 0xff);
23
24 if (rtc_write16(priv->rtc, priv->offset, val) < 0) {
25 debug("%s: rtc_write16 failed\n", __func__);
26 return -EIO;
27 }
28
29 return 0;
30}
31
32static int bootcount_rtc_get(struct udevice *dev, u32 *a)
33{
34 struct bootcount_rtc_priv *priv = dev_get_priv(dev);
35 u16 val;
36
37 if (rtc_read16(priv->rtc, priv->offset, &val) < 0) {
38 debug("%s: rtc_write16 failed\n", __func__);
39 return -EIO;
40 }
41
42 if (val >> 8 == bootcount_magic) {
43 *a = val & 0xff;
44 return 0;
45 }
46
47 debug("%s: bootcount magic does not match on %04x\n", __func__, val);
48 return -EIO;
49}
50
51static int bootcount_rtc_probe(struct udevice *dev)
52{
53 struct ofnode_phandle_args phandle_args;
54 struct bootcount_rtc_priv *priv = dev_get_priv(dev);
55 struct udevice *rtc;
56
57 if (dev_read_phandle_with_args(dev, "rtc", NULL, 0, 0, &phandle_args)) {
58 debug("%s: rtc backing device not specified\n", dev->name);
59 return -ENOENT;
60 }
61
62 if (uclass_get_device_by_ofnode(UCLASS_RTC, phandle_args.node, &rtc)) {
63 debug("%s: could not get backing device\n", dev->name);
64 return -ENODEV;
65 }
66
67 priv->rtc = rtc;
68 priv->offset = dev_read_u32_default(dev, "offset", 0);
69
70 return 0;
71}
72
73static const struct bootcount_ops bootcount_rtc_ops = {
74 .get = bootcount_rtc_get,
75 .set = bootcount_rtc_set,
76};
77
78static const struct udevice_id bootcount_rtc_ids[] = {
79 { .compatible = "u-boot,bootcount-rtc" },
80 { }
81};
82
83U_BOOT_DRIVER(bootcount_rtc) = {
84 .name = "bootcount-rtc",
85 .id = UCLASS_BOOTCOUNT,
Simon Glass41575d82020-12-03 16:55:17 -070086 .priv_auto = sizeof(struct bootcount_rtc_priv),
Philipp Tomsich482734a2018-11-27 23:00:19 +010087 .probe = bootcount_rtc_probe,
88 .of_match = bootcount_rtc_ids,
89 .ops = &bootcount_rtc_ops,
90};