xref: /openbmc/u-boot/drivers/bootcount/bootcount.c (revision 758694ff076255115b6cd936b13108db3773ee57)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
20044c42eSStefan Roese /*
30044c42eSStefan Roese  * (C) Copyright 2010-2012
40044c42eSStefan Roese  * Stefan Roese, DENX Software Engineering, sr@denx.de.
50044c42eSStefan Roese  */
60044c42eSStefan Roese 
70044c42eSStefan Roese #include <bootcount.h>
80044c42eSStefan Roese #include <linux/compiler.h>
90044c42eSStefan Roese 
100044c42eSStefan Roese /* Now implement the generic default functions */
bootcount_store(ulong a)110044c42eSStefan Roese __weak void bootcount_store(ulong a)
120044c42eSStefan Roese {
130044c42eSStefan Roese 	void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
14fe9805fcSAlex Kiernan 	uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR,
15fe9805fcSAlex Kiernan 					  CONFIG_SYS_CACHELINE_SIZE);
16fe9805fcSAlex Kiernan 	uintptr_t flush_end;
170044c42eSStefan Roese 
180044c42eSStefan Roese #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
19*758694ffSMarek Vasut 	raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a);
20fe9805fcSAlex Kiernan 
21fe9805fcSAlex Kiernan 	flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
22fe9805fcSAlex Kiernan 			    CONFIG_SYS_CACHELINE_SIZE);
230044c42eSStefan Roese #else
240044c42eSStefan Roese 	raw_bootcount_store(reg, a);
25*758694ffSMarek Vasut 	raw_bootcount_store(reg + 4, CONFIG_SYS_BOOTCOUNT_MAGIC);
26fe9805fcSAlex Kiernan 
27fe9805fcSAlex Kiernan 	flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8,
281947c2d2SStefano Babic 			    CONFIG_SYS_CACHELINE_SIZE);
29fe9805fcSAlex Kiernan #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
30fe9805fcSAlex Kiernan 	flush_dcache_range(flush_start, flush_end);
310044c42eSStefan Roese }
320044c42eSStefan Roese 
bootcount_load(void)330044c42eSStefan Roese __weak ulong bootcount_load(void)
340044c42eSStefan Roese {
350044c42eSStefan Roese 	void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
360044c42eSStefan Roese 
370044c42eSStefan Roese #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
380044c42eSStefan Roese 	u32 tmp = raw_bootcount_load(reg);
390044c42eSStefan Roese 
40*758694ffSMarek Vasut 	if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
410044c42eSStefan Roese 		return 0;
420044c42eSStefan Roese 	else
430044c42eSStefan Roese 		return (tmp & 0x0000ffff);
440044c42eSStefan Roese #else
45*758694ffSMarek Vasut 	if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC)
460044c42eSStefan Roese 		return 0;
470044c42eSStefan Roese 	else
480044c42eSStefan Roese 		return raw_bootcount_load(reg);
4976765375SRobert P. J. Day #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
500044c42eSStefan Roese }
51