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 <bootcount.h>
8 #include <dm.h>
9 #include <log.h>
10 #include <rtc.h>
11 
12 static const u8 bootcount_magic = 0xbc;
13 
14 struct bootcount_rtc_priv {
15 	struct udevice *rtc;
16 	u32 offset;
17 };
18 
bootcount_rtc_set(struct udevice * dev,const u32 a)19 static 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 
bootcount_rtc_get(struct udevice * dev,u32 * a)32 static 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 
bootcount_rtc_probe(struct udevice * dev)51 static 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 
73 static const struct bootcount_ops bootcount_rtc_ops = {
74 	.get = bootcount_rtc_get,
75 	.set = bootcount_rtc_set,
76 };
77 
78 static const struct udevice_id bootcount_rtc_ids[] = {
79 	{ .compatible = "u-boot,bootcount-rtc" },
80 	{ }
81 };
82 
83 U_BOOT_DRIVER(bootcount_rtc) = {
84 	.name	= "bootcount-rtc",
85 	.id	= UCLASS_BOOTCOUNT,
86 	.priv_auto	= sizeof(struct bootcount_rtc_priv),
87 	.probe	= bootcount_rtc_probe,
88 	.of_match = bootcount_rtc_ids,
89 	.ops	= &bootcount_rtc_ops,
90 };
91