blob: 655dfaf59c48a3a1259978ed9cac3b2a007ca332 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roeseed7aaf82010-04-28 10:47:36 +02002/*
Stefan Roese033848e2012-08-16 17:55:41 +00003 * (C) Copyright 2010-2012
Stefan Roeseed7aaf82010-04-28 10:47:36 +02004 * Stefan Roese, DENX Software Engineering, sr@denx.de.
Stefan Roeseed7aaf82010-04-28 10:47:36 +02005 */
6
Stefan Roese033848e2012-08-16 17:55:41 +00007#include <bootcount.h>
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Stefan Roese033848e2012-08-16 17:55:41 +00009#include <linux/compiler.h>
Stefan Roeseed7aaf82010-04-28 10:47:36 +020010
Heiko Schochere2981922020-03-02 15:43:59 +010011#if !defined(CONFIG_DM_BOOTCOUNT)
Stefan Roese033848e2012-08-16 17:55:41 +000012/* Now implement the generic default functions */
Stefan Roese033848e2012-08-16 17:55:41 +000013__weak void bootcount_store(ulong a)
Stefan Roeseed7aaf82010-04-28 10:47:36 +020014{
15 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
Alex Kiernandc663572018-07-25 11:45:58 +000016 uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR,
17 CONFIG_SYS_CACHELINE_SIZE);
18 uintptr_t flush_end;
Stefan Roeseed7aaf82010-04-28 10:47:36 +020019
20#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
Marek Vasut6ae0ded2018-10-11 00:13:54 +020021 raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a);
Alex Kiernandc663572018-07-25 11:45:58 +000022
23 flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
24 CONFIG_SYS_CACHELINE_SIZE);
Stefan Roeseed7aaf82010-04-28 10:47:36 +020025#else
Stefan Roese033848e2012-08-16 17:55:41 +000026 raw_bootcount_store(reg, a);
Marek Vasut6ae0ded2018-10-11 00:13:54 +020027 raw_bootcount_store(reg + 4, CONFIG_SYS_BOOTCOUNT_MAGIC);
Alex Kiernandc663572018-07-25 11:45:58 +000028
29 flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8,
30 CONFIG_SYS_CACHELINE_SIZE);
Robert P. J. Day26a4e392015-12-22 07:15:14 -050031#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
Alex Kiernandc663572018-07-25 11:45:58 +000032 flush_dcache_range(flush_start, flush_end);
Stefan Roeseed7aaf82010-04-28 10:47:36 +020033}
34
Stefan Roese033848e2012-08-16 17:55:41 +000035__weak ulong bootcount_load(void)
Stefan Roeseed7aaf82010-04-28 10:47:36 +020036{
37 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
38
39#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
Stefan Roese033848e2012-08-16 17:55:41 +000040 u32 tmp = raw_bootcount_load(reg);
Michael Weiss8eed66d2010-05-20 16:09:35 +020041
Marek Vasut6ae0ded2018-10-11 00:13:54 +020042 if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
Stefan Roeseed7aaf82010-04-28 10:47:36 +020043 return 0;
44 else
Michael Weiss8eed66d2010-05-20 16:09:35 +020045 return (tmp & 0x0000ffff);
Stefan Roeseed7aaf82010-04-28 10:47:36 +020046#else
Marek Vasut6ae0ded2018-10-11 00:13:54 +020047 if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC)
Stefan Roeseed7aaf82010-04-28 10:47:36 +020048 return 0;
49 else
Stefan Roese033848e2012-08-16 17:55:41 +000050 return raw_bootcount_load(reg);
Robert P. J. Day26a4e392015-12-22 07:15:14 -050051#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
Stefan Roeseed7aaf82010-04-28 10:47:36 +020052}
Heiko Schochere2981922020-03-02 15:43:59 +010053#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 */
62struct bootcount_mem_priv {
63 phys_addr_t base;
64 bool singleword;
65};
66
67static 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
90static 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
114static const struct bootcount_ops bootcount_mem_ops = {
115 .get = bootcount_mem_get,
116 .set = bootcount_mem_set,
117};
118
119static 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
130static const struct udevice_id bootcount_mem_ids[] = {
131 { .compatible = "u-boot,bootcount" },
132 { }
133};
134
135U_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