xref: /rk3399_rockchip-uboot/drivers/bootcount/bootcount.c (revision 38d67a4e552ac991f21c2d3e442a38fb0098fda6)
10044c42eSStefan Roese /*
20044c42eSStefan Roese  * (C) Copyright 2010-2012
30044c42eSStefan Roese  * Stefan Roese, DENX Software Engineering, sr@denx.de.
40044c42eSStefan Roese  *
51a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
60044c42eSStefan Roese  */
70044c42eSStefan Roese 
80044c42eSStefan Roese #include <bootcount.h>
90044c42eSStefan Roese #include <linux/compiler.h>
100044c42eSStefan Roese 
110044c42eSStefan Roese /*
120044c42eSStefan Roese  * Only override CONFIG_SYS_BOOTCOUNT_ADDR if not already defined. This
130044c42eSStefan Roese  * way, some boards can define it directly in their config header.
140044c42eSStefan Roese  */
150044c42eSStefan Roese #if !defined(CONFIG_SYS_BOOTCOUNT_ADDR)
160044c42eSStefan Roese 
170044c42eSStefan Roese #if defined(CONFIG_MPC5xxx)
180044c42eSStefan Roese #define CONFIG_SYS_BOOTCOUNT_ADDR	(MPC5XXX_CDM_BRDCRMB)
190044c42eSStefan Roese #define CONFIG_SYS_BOOTCOUNT_SINGLEWORD
200044c42eSStefan Roese #endif /* defined(CONFIG_MPC5xxx) */
210044c42eSStefan Roese 
220044c42eSStefan Roese #if defined(CONFIG_MPC512X)
230044c42eSStefan Roese #define CONFIG_SYS_BOOTCOUNT_ADDR	(&((immap_t *)CONFIG_SYS_IMMR)->clk.bcr)
240044c42eSStefan Roese #define CONFIG_SYS_BOOTCOUNT_SINGLEWORD
250044c42eSStefan Roese #endif /* defined(CONFIG_MPC512X) */
260044c42eSStefan Roese 
270044c42eSStefan Roese #if defined(CONFIG_8xx)
280044c42eSStefan Roese #define CONFIG_SYS_BOOTCOUNT_ADDR (((immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_dpmem + \
290044c42eSStefan Roese 				CPM_BOOTCOUNT_ADDR)
300044c42eSStefan Roese #endif /* defined(CONFIG_8xx) */
310044c42eSStefan Roese 
320044c42eSStefan Roese #if defined(CONFIG_MPC8260)
330044c42eSStefan Roese #include <asm/cpm_8260.h>
340044c42eSStefan Roese 
350044c42eSStefan Roese #define CONFIG_SYS_BOOTCOUNT_ADDR	(CONFIG_SYS_IMMR + CPM_BOOTCOUNT_ADDR)
360044c42eSStefan Roese #endif /* defined(CONFIG_MPC8260) */
370044c42eSStefan Roese 
380044c42eSStefan Roese #if defined(CONFIG_QE)
39*38d67a4eSZhao Qiang #include <linux/immap_qe.h>
400044c42eSStefan Roese 
410044c42eSStefan Roese #define CONFIG_SYS_BOOTCOUNT_ADDR	(CONFIG_SYS_IMMR + 0x110000 + \
420044c42eSStefan Roese 					 QE_MURAM_SIZE - 2 * sizeof(u32))
430044c42eSStefan Roese #endif /* defined(CONFIG_MPC8360) */
440044c42eSStefan Roese 
450044c42eSStefan Roese #if defined(CONFIG_4xx)
460044c42eSStefan Roese #define CONFIG_SYS_BOOTCOUNT_ADDR	(CONFIG_SYS_OCM_DATA_ADDR + \
470044c42eSStefan Roese 				CONFIG_SYS_BOOTCOUNT_ADDR)
480044c42eSStefan Roese #endif /* defined(CONFIG_4xx) */
490044c42eSStefan Roese 
500044c42eSStefan Roese #endif /* !defined(CONFIG_SYS_BOOTCOUNT_ADDR) */
510044c42eSStefan Roese 
520044c42eSStefan Roese /* Now implement the generic default functions */
530044c42eSStefan Roese #if defined(CONFIG_SYS_BOOTCOUNT_ADDR)
540044c42eSStefan Roese __weak void bootcount_store(ulong a)
550044c42eSStefan Roese {
560044c42eSStefan Roese 	void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
570044c42eSStefan Roese 
580044c42eSStefan Roese #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
590044c42eSStefan Roese 	raw_bootcount_store(reg, (BOOTCOUNT_MAGIC & 0xffff0000) | a);
600044c42eSStefan Roese #else
610044c42eSStefan Roese 	raw_bootcount_store(reg, a);
620044c42eSStefan Roese 	raw_bootcount_store(reg + 4, BOOTCOUNT_MAGIC);
630044c42eSStefan Roese #endif
640044c42eSStefan Roese }
650044c42eSStefan Roese 
660044c42eSStefan Roese __weak ulong bootcount_load(void)
670044c42eSStefan Roese {
680044c42eSStefan Roese 	void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
690044c42eSStefan Roese 
700044c42eSStefan Roese #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
710044c42eSStefan Roese 	u32 tmp = raw_bootcount_load(reg);
720044c42eSStefan Roese 
730044c42eSStefan Roese 	if ((tmp & 0xffff0000) != (BOOTCOUNT_MAGIC & 0xffff0000))
740044c42eSStefan Roese 		return 0;
750044c42eSStefan Roese 	else
760044c42eSStefan Roese 		return (tmp & 0x0000ffff);
770044c42eSStefan Roese #else
780044c42eSStefan Roese 	if (raw_bootcount_load(reg + 4) != BOOTCOUNT_MAGIC)
790044c42eSStefan Roese 		return 0;
800044c42eSStefan Roese 	else
810044c42eSStefan Roese 		return raw_bootcount_load(reg);
820044c42eSStefan Roese #endif
830044c42eSStefan Roese }
840044c42eSStefan Roese #endif
85