xref: /rk3399_rockchip-uboot/drivers/bootcount/bootcount_ram.c (revision 2d221489df021393654805536be7effcb9d39702)
10044c42eSStefan Roese /*
20044c42eSStefan Roese  * (C) Copyright 2010
30044c42eSStefan Roese  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
40044c42eSStefan Roese  *
51a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
60044c42eSStefan Roese  */
70044c42eSStefan Roese 
80044c42eSStefan Roese #include <common.h>
90044c42eSStefan Roese #include <asm/io.h>
100044c42eSStefan Roese 
110044c42eSStefan Roese DECLARE_GLOBAL_DATA_PTR;
120044c42eSStefan Roese 
130044c42eSStefan Roese const ulong patterns[]      = {	0x00000000,
140044c42eSStefan Roese 				0xFFFFFFFF,
150044c42eSStefan Roese 				0xFF00FF00,
160044c42eSStefan Roese 				0x0F0F0F0F,
170044c42eSStefan Roese 				0xF0F0F0F0};
180044c42eSStefan Roese const ulong NBR_OF_PATTERNS = sizeof(patterns) / sizeof(*patterns);
190044c42eSStefan Roese const ulong OFFS_PATTERN    = 3;
200044c42eSStefan Roese const ulong REPEAT_PATTERN  = 1000;
210044c42eSStefan Roese 
bootcount_store(ulong a)220044c42eSStefan Roese void bootcount_store(ulong a)
230044c42eSStefan Roese {
240044c42eSStefan Roese 	ulong *save_addr;
250044c42eSStefan Roese 	ulong size = 0;
260044c42eSStefan Roese 	int i;
270044c42eSStefan Roese 
280044c42eSStefan Roese 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
290044c42eSStefan Roese 		size += gd->bd->bi_dram[i].size;
300044c42eSStefan Roese 	save_addr = (ulong *)(size - BOOTCOUNT_ADDR);
310044c42eSStefan Roese 	writel(a, save_addr);
320044c42eSStefan Roese 	writel(BOOTCOUNT_MAGIC, &save_addr[1]);
330044c42eSStefan Roese 
340044c42eSStefan Roese 	for (i = 0; i < REPEAT_PATTERN; i++)
350044c42eSStefan Roese 		writel(patterns[i % NBR_OF_PATTERNS],
360044c42eSStefan Roese 			&save_addr[i + OFFS_PATTERN]);
370044c42eSStefan Roese 
3820adda4cSStefan Roese 	/* Make sure the data is written to RAM */
3920adda4cSStefan Roese 	flush_dcache_range((ulong)&save_addr[0],
40*384b1d50SStefan Roese 			   (((ulong)&save_addr[REPEAT_PATTERN + OFFS_PATTERN] &
41*384b1d50SStefan Roese 			     ~(ARCH_DMA_MINALIGN - 1)) + ARCH_DMA_MINALIGN));
420044c42eSStefan Roese }
430044c42eSStefan Roese 
bootcount_load(void)440044c42eSStefan Roese ulong bootcount_load(void)
450044c42eSStefan Roese {
460044c42eSStefan Roese 	ulong *save_addr;
470044c42eSStefan Roese 	ulong size = 0;
480044c42eSStefan Roese 	ulong counter = 0;
490044c42eSStefan Roese 	int i, tmp;
500044c42eSStefan Roese 
510044c42eSStefan Roese 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
520044c42eSStefan Roese 		size += gd->bd->bi_dram[i].size;
530044c42eSStefan Roese 	save_addr = (ulong *)(size - BOOTCOUNT_ADDR);
540044c42eSStefan Roese 
550044c42eSStefan Roese 	counter = readl(&save_addr[0]);
560044c42eSStefan Roese 
570044c42eSStefan Roese 	/* Is the counter reliable, check in the big pattern for bit errors */
580044c42eSStefan Roese 	for (i = 0; (i < REPEAT_PATTERN) && (counter != 0); i++) {
590044c42eSStefan Roese 		tmp = readl(&save_addr[i + OFFS_PATTERN]);
600044c42eSStefan Roese 		if (tmp != patterns[i % NBR_OF_PATTERNS])
610044c42eSStefan Roese 			counter = 0;
620044c42eSStefan Roese 	}
630044c42eSStefan Roese 	return counter;
640044c42eSStefan Roese }
65