1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2010-2012
4 * Stefan Roese, DENX Software Engineering, sr@denx.de.
9 #include <linux/compiler.h>
11 #if !defined(CONFIG_DM_BOOTCOUNT)
12 /* Now implement the generic default functions */
13 __weak void bootcount_store(ulong a)
15 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
16 uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR,
17 CONFIG_SYS_CACHELINE_SIZE);
20 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
21 raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a);
23 flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
24 CONFIG_SYS_CACHELINE_SIZE);
26 raw_bootcount_store(reg, a);
27 raw_bootcount_store(reg + 4, CONFIG_SYS_BOOTCOUNT_MAGIC);
29 flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8,
30 CONFIG_SYS_CACHELINE_SIZE);
31 #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
32 flush_dcache_range(flush_start, flush_end);
35 __weak ulong bootcount_load(void)
37 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
39 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
40 u32 tmp = raw_bootcount_load(reg);
42 if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
45 return (tmp & 0x0000ffff);
47 if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC)
50 return raw_bootcount_load(reg);
51 #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
57 * struct bootcount_mem_priv - private bootcount mem driver data
59 * @base: base address used for bootcounter
60 * @singleword: if true use only one 32 bit word for bootcounter
62 struct bootcount_mem_priv {
67 static int bootcount_mem_get(struct udevice *dev, u32 *a)
69 struct bootcount_mem_priv *priv = dev_get_priv(dev);
70 void *reg = (void *)priv->base;
71 u32 magic = CONFIG_SYS_BOOTCOUNT_MAGIC;
73 if (priv->singleword) {
74 u32 tmp = raw_bootcount_load(reg);
76 if ((tmp & 0xffff0000) != (magic & 0xffff0000))
79 *a = (tmp & 0x0000ffff);
81 if (raw_bootcount_load(reg + 4) != magic)
84 *a = raw_bootcount_load(reg);
90 static int bootcount_mem_set(struct udevice *dev, const u32 a)
92 struct bootcount_mem_priv *priv = dev_get_priv(dev);
93 void *reg = (void *)priv->base;
94 u32 magic = CONFIG_SYS_BOOTCOUNT_MAGIC;
95 uintptr_t flush_start = rounddown(priv->base,
96 CONFIG_SYS_CACHELINE_SIZE);
99 if (priv->singleword) {
100 raw_bootcount_store(reg, (magic & 0xffff0000) | a);
101 flush_end = roundup(priv->base + 4,
102 CONFIG_SYS_CACHELINE_SIZE);
104 raw_bootcount_store(reg, a);
105 raw_bootcount_store(reg + 4, magic);
106 flush_end = roundup(priv->base + 8,
107 CONFIG_SYS_CACHELINE_SIZE);
109 flush_dcache_range(flush_start, flush_end);
114 static const struct bootcount_ops bootcount_mem_ops = {
115 .get = bootcount_mem_get,
116 .set = bootcount_mem_set,
119 static int bootcount_mem_probe(struct udevice *dev)
121 struct bootcount_mem_priv *priv = dev_get_priv(dev);
123 priv->base = (phys_addr_t)dev_read_addr(dev);
124 if (dev_read_bool(dev, "single-word"))
125 priv->singleword = true;
130 static const struct udevice_id bootcount_mem_ids[] = {
131 { .compatible = "u-boot,bootcount" },
135 U_BOOT_DRIVER(bootcount_mem) = {
136 .name = "bootcount-mem",
137 .id = UCLASS_BOOTCOUNT,
138 .priv_auto_alloc_size = sizeof(struct bootcount_mem_priv),
139 .probe = bootcount_mem_probe,
140 .of_match = bootcount_mem_ids,
141 .ops = &bootcount_mem_ops,