Merge tag 'ti-v2020.07-next' of https://gitlab.denx.de/u-boot/custodians/u-boot-ti...
[platform/kernel/u-boot.git] / drivers / bootcount / bootcount.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2010-2012
4  * Stefan Roese, DENX Software Engineering, sr@denx.de.
5  */
6
7 #include <bootcount.h>
8 #include <cpu_func.h>
9 #include <linux/compiler.h>
10
11 #if !defined(CONFIG_DM_BOOTCOUNT)
12 /* Now implement the generic default functions */
13 __weak void bootcount_store(ulong a)
14 {
15         void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
16         uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR,
17                                           CONFIG_SYS_CACHELINE_SIZE);
18         uintptr_t flush_end;
19
20 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
21         raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a);
22
23         flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
24                             CONFIG_SYS_CACHELINE_SIZE);
25 #else
26         raw_bootcount_store(reg, a);
27         raw_bootcount_store(reg + 4, CONFIG_SYS_BOOTCOUNT_MAGIC);
28
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);
33 }
34
35 __weak ulong bootcount_load(void)
36 {
37         void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
38
39 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
40         u32 tmp = raw_bootcount_load(reg);
41
42         if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
43                 return 0;
44         else
45                 return (tmp & 0x0000ffff);
46 #else
47         if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC)
48                 return 0;
49         else
50                 return raw_bootcount_load(reg);
51 #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
52 }
53 #else
54 #include <dm.h>
55
56 /*
57  * struct bootcount_mem_priv - private bootcount mem driver data
58  *
59  * @base: base address used for bootcounter
60  * @singleword: if true use only one 32 bit word for bootcounter
61  */
62 struct bootcount_mem_priv {
63         phys_addr_t base;
64         bool singleword;
65 };
66
67 static int bootcount_mem_get(struct udevice *dev, u32 *a)
68 {
69         struct bootcount_mem_priv *priv = dev_get_priv(dev);
70         void *reg = (void *)priv->base;
71         u32 magic = CONFIG_SYS_BOOTCOUNT_MAGIC;
72
73         if (priv->singleword) {
74                 u32 tmp = raw_bootcount_load(reg);
75
76                 if ((tmp & 0xffff0000) != (magic & 0xffff0000))
77                         return -ENODEV;
78
79                 *a = (tmp & 0x0000ffff);
80         } else {
81                 if (raw_bootcount_load(reg + 4) != magic)
82                         return -ENODEV;
83
84                 *a = raw_bootcount_load(reg);
85         }
86
87         return 0;
88 };
89
90 static int bootcount_mem_set(struct udevice *dev, const u32 a)
91 {
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);
97         uintptr_t flush_end;
98
99         if (priv->singleword) {
100                 raw_bootcount_store(reg, (magic & 0xffff0000) | a);
101                 flush_end = roundup(priv->base + 4,
102                                     CONFIG_SYS_CACHELINE_SIZE);
103         } else {
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);
108         }
109         flush_dcache_range(flush_start, flush_end);
110
111         return 0;
112 };
113
114 static const struct bootcount_ops bootcount_mem_ops = {
115         .get = bootcount_mem_get,
116         .set = bootcount_mem_set,
117 };
118
119 static int bootcount_mem_probe(struct udevice *dev)
120 {
121         struct bootcount_mem_priv *priv = dev_get_priv(dev);
122
123         priv->base = (phys_addr_t)dev_read_addr(dev);
124         if (dev_read_bool(dev, "single-word"))
125                 priv->singleword = true;
126
127         return 0;
128 }
129
130 static const struct udevice_id bootcount_mem_ids[] = {
131         { .compatible = "u-boot,bootcount" },
132         { }
133 };
134
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,
142 };
143 #endif