1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 Theobroma Systems Design und Consulting GmbH
11 static const u8 bootcount_magic = 0xbc;
13 struct bootcount_rtc_priv {
18 static int bootcount_rtc_set(struct udevice *dev, const u32 a)
20 struct bootcount_rtc_priv *priv = dev_get_priv(dev);
21 const u16 val = bootcount_magic << 8 | (a & 0xff);
23 if (rtc_write16(priv->rtc, priv->offset, val) < 0) {
24 debug("%s: rtc_write16 failed\n", __func__);
31 static int bootcount_rtc_get(struct udevice *dev, u32 *a)
33 struct bootcount_rtc_priv *priv = dev_get_priv(dev);
36 if (rtc_read16(priv->rtc, priv->offset, &val) < 0) {
37 debug("%s: rtc_write16 failed\n", __func__);
41 if (val >> 8 == bootcount_magic) {
46 debug("%s: bootcount magic does not match on %04x\n", __func__, val);
50 static int bootcount_rtc_probe(struct udevice *dev)
52 struct ofnode_phandle_args phandle_args;
53 struct bootcount_rtc_priv *priv = dev_get_priv(dev);
56 if (dev_read_phandle_with_args(dev, "rtc", NULL, 0, 0, &phandle_args)) {
57 debug("%s: rtc backing device not specified\n", dev->name);
61 if (uclass_get_device_by_ofnode(UCLASS_RTC, phandle_args.node, &rtc)) {
62 debug("%s: could not get backing device\n", dev->name);
67 priv->offset = dev_read_u32_default(dev, "offset", 0);
72 static const struct bootcount_ops bootcount_rtc_ops = {
73 .get = bootcount_rtc_get,
74 .set = bootcount_rtc_set,
77 static const struct udevice_id bootcount_rtc_ids[] = {
78 { .compatible = "u-boot,bootcount-rtc" },
82 U_BOOT_DRIVER(bootcount_rtc) = {
83 .name = "bootcount-rtc",
84 .id = UCLASS_BOOTCOUNT,
85 .priv_auto_alloc_size = sizeof(struct bootcount_rtc_priv),
86 .probe = bootcount_rtc_probe,
87 .of_match = bootcount_rtc_ids,
88 .ops = &bootcount_rtc_ops,