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 	uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR,
15 					  CONFIG_SYS_CACHELINE_SIZE);
16 	uintptr_t flush_end;
17 
18 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
19 	raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a);
20 
21 	flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
22 			    CONFIG_SYS_CACHELINE_SIZE);
23 #else
24 	raw_bootcount_store(reg, a);
25 	raw_bootcount_store(reg + 4, CONFIG_SYS_BOOTCOUNT_MAGIC);
26 
27 	flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8,
28 			    CONFIG_SYS_CACHELINE_SIZE);
29 #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
30 	flush_dcache_range(flush_start, flush_end);
31 }
32 
33 __weak ulong bootcount_load(void)
34 {
35 	void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
36 
37 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
38 	u32 tmp = raw_bootcount_load(reg);
39 
40 	if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
41 		return 0;
42 	else
43 		return (tmp & 0x0000ffff);
44 #else
45 	if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC)
46 		return 0;
47 	else
48 		return raw_bootcount_load(reg);
49 #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
50 }
51