blob: 343b8a34414fb2b86b8dcd00ef5d3d009712ec3a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roesee4a95d12010-04-28 10:47:36 +02002/*
Stefan Roese0044c422012-08-16 17:55:41 +00003 * (C) Copyright 2010-2012
Stefan Roesee4a95d12010-04-28 10:47:36 +02004 * Stefan Roese, DENX Software Engineering, sr@denx.de.
Stefan Roesee4a95d12010-04-28 10:47:36 +02005 */
6
Stefan Roese0044c422012-08-16 17:55:41 +00007#include <bootcount.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glass90526e92020-05-10 11:39:56 -06009#include <asm/cache.h>
Stefan Roese0044c422012-08-16 17:55:41 +000010#include <linux/compiler.h>
Stefan Roesee4a95d12010-04-28 10:47:36 +020011
Heiko Schocher80e8b8a2020-03-02 15:43:59 +010012#if !defined(CONFIG_DM_BOOTCOUNT)
Stefan Roese0044c422012-08-16 17:55:41 +000013/* Now implement the generic default functions */
Stefan Roese0044c422012-08-16 17:55:41 +000014__weak void bootcount_store(ulong a)
Stefan Roesee4a95d12010-04-28 10:47:36 +020015{
16 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
Alex Kiernanfe9805f2018-07-25 11:45:58 +000017 uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR,
18 CONFIG_SYS_CACHELINE_SIZE);
19 uintptr_t flush_end;
Stefan Roesee4a95d12010-04-28 10:47:36 +020020
21#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
Marek Vasut758694f2018-10-11 00:13:54 +020022 raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a);
Alex Kiernanfe9805f2018-07-25 11:45:58 +000023
24 flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
25 CONFIG_SYS_CACHELINE_SIZE);
Stefan Roesee4a95d12010-04-28 10:47:36 +020026#else
Stefan Roese0044c422012-08-16 17:55:41 +000027 raw_bootcount_store(reg, a);
Marek Vasut758694f2018-10-11 00:13:54 +020028 raw_bootcount_store(reg + 4, CONFIG_SYS_BOOTCOUNT_MAGIC);
Alex Kiernanfe9805f2018-07-25 11:45:58 +000029
30 flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8,
31 CONFIG_SYS_CACHELINE_SIZE);
Robert P. J. Day76765372015-12-22 07:15:14 -050032#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
Alex Kiernanfe9805f2018-07-25 11:45:58 +000033 flush_dcache_range(flush_start, flush_end);
Stefan Roesee4a95d12010-04-28 10:47:36 +020034}
35
Stefan Roese0044c422012-08-16 17:55:41 +000036__weak ulong bootcount_load(void)
Stefan Roesee4a95d12010-04-28 10:47:36 +020037{
38 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
39
40#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
Stefan Roese0044c422012-08-16 17:55:41 +000041 u32 tmp = raw_bootcount_load(reg);
Michael Weiss59dde442010-05-20 16:09:35 +020042
Marek Vasut758694f2018-10-11 00:13:54 +020043 if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
Stefan Roesee4a95d12010-04-28 10:47:36 +020044 return 0;
45 else
Michael Weiss59dde442010-05-20 16:09:35 +020046 return (tmp & 0x0000ffff);
Stefan Roesee4a95d12010-04-28 10:47:36 +020047#else
Marek Vasut758694f2018-10-11 00:13:54 +020048 if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC)
Stefan Roesee4a95d12010-04-28 10:47:36 +020049 return 0;
50 else
Stefan Roese0044c422012-08-16 17:55:41 +000051 return raw_bootcount_load(reg);
Robert P. J. Day76765372015-12-22 07:15:14 -050052#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
Stefan Roesee4a95d12010-04-28 10:47:36 +020053}
Heiko Schocher80e8b8a2020-03-02 15:43:59 +010054#else
55#include <dm.h>
56
57/*
58 * struct bootcount_mem_priv - private bootcount mem driver data
59 *
60 * @base: base address used for bootcounter
61 * @singleword: if true use only one 32 bit word for bootcounter
62 */
63struct bootcount_mem_priv {
64 phys_addr_t base;
65 bool singleword;
66};
67
68static int bootcount_mem_get(struct udevice *dev, u32 *a)
69{
70 struct bootcount_mem_priv *priv = dev_get_priv(dev);
71 void *reg = (void *)priv->base;
72 u32 magic = CONFIG_SYS_BOOTCOUNT_MAGIC;
73
74 if (priv->singleword) {
75 u32 tmp = raw_bootcount_load(reg);
76
77 if ((tmp & 0xffff0000) != (magic & 0xffff0000))
78 return -ENODEV;
79
80 *a = (tmp & 0x0000ffff);
81 } else {
82 if (raw_bootcount_load(reg + 4) != magic)
83 return -ENODEV;
84
85 *a = raw_bootcount_load(reg);
86 }
87
88 return 0;
89};
90
91static int bootcount_mem_set(struct udevice *dev, const u32 a)
92{
93 struct bootcount_mem_priv *priv = dev_get_priv(dev);
94 void *reg = (void *)priv->base;
95 u32 magic = CONFIG_SYS_BOOTCOUNT_MAGIC;
96 uintptr_t flush_start = rounddown(priv->base,
97 CONFIG_SYS_CACHELINE_SIZE);
98 uintptr_t flush_end;
99
100 if (priv->singleword) {
101 raw_bootcount_store(reg, (magic & 0xffff0000) | a);
102 flush_end = roundup(priv->base + 4,
103 CONFIG_SYS_CACHELINE_SIZE);
104 } else {
105 raw_bootcount_store(reg, a);
106 raw_bootcount_store(reg + 4, magic);
107 flush_end = roundup(priv->base + 8,
108 CONFIG_SYS_CACHELINE_SIZE);
109 }
110 flush_dcache_range(flush_start, flush_end);
111
112 return 0;
113};
114
115static const struct bootcount_ops bootcount_mem_ops = {
116 .get = bootcount_mem_get,
117 .set = bootcount_mem_set,
118};
119
120static int bootcount_mem_probe(struct udevice *dev)
121{
122 struct bootcount_mem_priv *priv = dev_get_priv(dev);
123
124 priv->base = (phys_addr_t)dev_read_addr(dev);
125 if (dev_read_bool(dev, "single-word"))
126 priv->singleword = true;
127
128 return 0;
129}
130
131static const struct udevice_id bootcount_mem_ids[] = {
132 { .compatible = "u-boot,bootcount" },
133 { }
134};
135
136U_BOOT_DRIVER(bootcount_mem) = {
137 .name = "bootcount-mem",
138 .id = UCLASS_BOOTCOUNT,
Simon Glass41575d82020-12-03 16:55:17 -0700139 .priv_auto = sizeof(struct bootcount_mem_priv),
Heiko Schocher80e8b8a2020-03-02 15:43:59 +0100140 .probe = bootcount_mem_probe,
141 .of_match = bootcount_mem_ids,
142 .ops = &bootcount_mem_ops,
143};
144#endif