1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 Theobroma Systems Design und Consulting GmbH
12 static const u8 bootcount_magic = 0xbc;
14 struct bootcount_rtc_priv {
19 static int bootcount_rtc_set(struct udevice *dev, const u32 a)
21 struct bootcount_rtc_priv *priv = dev_get_priv(dev);
22 const u16 val = bootcount_magic << 8 | (a & 0xff);
24 if (rtc_write16(priv->rtc, priv->offset, val) < 0) {
25 debug("%s: rtc_write16 failed\n", __func__);
32 static int bootcount_rtc_get(struct udevice *dev, u32 *a)
34 struct bootcount_rtc_priv *priv = dev_get_priv(dev);
37 if (rtc_read16(priv->rtc, priv->offset, &val) < 0) {
38 debug("%s: rtc_write16 failed\n", __func__);
42 if (val >> 8 == bootcount_magic) {
47 debug("%s: bootcount magic does not match on %04x\n", __func__, val);
51 static int bootcount_rtc_probe(struct udevice *dev)
53 struct ofnode_phandle_args phandle_args;
54 struct bootcount_rtc_priv *priv = dev_get_priv(dev);
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);
62 if (uclass_get_device_by_ofnode(UCLASS_RTC, phandle_args.node, &rtc)) {
63 debug("%s: could not get backing device\n", dev->name);
68 priv->offset = dev_read_u32_default(dev, "offset", 0);
73 static const struct bootcount_ops bootcount_rtc_ops = {
74 .get = bootcount_rtc_get,
75 .set = bootcount_rtc_set,
78 static const struct udevice_id bootcount_rtc_ids[] = {
79 { .compatible = "u-boot,bootcount-rtc" },
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,