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 <linux/compiler.h>
9 
10 /* Now implement the generic default functions */
11 __weak void bootcount_store(ulong a)
12 {
13 	void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
14 
15 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
16 	raw_bootcount_store(reg, (BOOTCOUNT_MAGIC & 0xffff0000) | a);
17 #else
18 	raw_bootcount_store(reg, a);
19 	raw_bootcount_store(reg + 4, BOOTCOUNT_MAGIC);
20 #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
21 }
22 
23 __weak ulong bootcount_load(void)
24 {
25 	void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
26 
27 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
28 	u32 tmp = raw_bootcount_load(reg);
29 
30 	if ((tmp & 0xffff0000) != (BOOTCOUNT_MAGIC & 0xffff0000))
31 		return 0;
32 	else
33 		return (tmp & 0x0000ffff);
34 #else
35 	if (raw_bootcount_load(reg + 4) != BOOTCOUNT_MAGIC)
36 		return 0;
37 	else
38 		return raw_bootcount_load(reg);
39 #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
40 }
41