1 /*
2  * (C) Copyright 2010-2012
3  * Stefan Roese, DENX Software Engineering, sr@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <bootcount.h>
9 #include <linux/compiler.h>
10 
11 /*
12  * Only override CONFIG_SYS_BOOTCOUNT_ADDR if not already defined. This
13  * way, some boards can define it directly in their config header.
14  */
15 #if !defined(CONFIG_SYS_BOOTCOUNT_ADDR)
16 
17 #if defined(CONFIG_QE)
18 #include <linux/immap_qe.h>
19 #define CONFIG_SYS_BOOTCOUNT_ADDR	(CONFIG_SYS_IMMR + 0x110000 + \
20 					 QE_MURAM_SIZE - 2 * sizeof(u32))
21 #endif /* defined(CONFIG_QE) */
22 
23 #if defined(CONFIG_4xx)
24 #define CONFIG_SYS_BOOTCOUNT_ADDR	(CONFIG_SYS_OCM_DATA_ADDR + \
25 				CONFIG_SYS_BOOTCOUNT_ADDR)
26 #endif /* defined(CONFIG_4xx) */
27 
28 #endif /* !defined(CONFIG_SYS_BOOTCOUNT_ADDR) */
29 
30 /* Now implement the generic default functions */
31 #if defined(CONFIG_SYS_BOOTCOUNT_ADDR)
32 __weak void bootcount_store(ulong a)
33 {
34 	void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
35 
36 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
37 	raw_bootcount_store(reg, (BOOTCOUNT_MAGIC & 0xffff0000) | a);
38 #else
39 	raw_bootcount_store(reg, a);
40 	raw_bootcount_store(reg + 4, BOOTCOUNT_MAGIC);
41 #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
42 }
43 
44 __weak ulong bootcount_load(void)
45 {
46 	void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
47 
48 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
49 	u32 tmp = raw_bootcount_load(reg);
50 
51 	if ((tmp & 0xffff0000) != (BOOTCOUNT_MAGIC & 0xffff0000))
52 		return 0;
53 	else
54 		return (tmp & 0x0000ffff);
55 #else
56 	if (raw_bootcount_load(reg + 4) != BOOTCOUNT_MAGIC)
57 		return 0;
58 	else
59 		return raw_bootcount_load(reg);
60 #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
61 }
62 #endif /* defined(CONFIG_SYS_BOOTCOUNT_ADDR) */
63