1 // SPDX-License-Identifier: GPL-2.0+
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
10 #include <asm/global_data.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 const ulong patterns[] = { 0x00000000,
20 const ulong NBR_OF_PATTERNS = sizeof(patterns) / sizeof(*patterns);
21 const ulong OFFS_PATTERN = 3;
22 const ulong REPEAT_PATTERN = 1000;
24 void bootcount_store(ulong a)
30 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
31 size += gd->bd->bi_dram[i].size;
32 save_addr = (ulong *)(size - BOOTCOUNT_ADDR);
34 writel(CONFIG_SYS_BOOTCOUNT_MAGIC, &save_addr[1]);
36 for (i = 0; i < REPEAT_PATTERN; i++)
37 writel(patterns[i % NBR_OF_PATTERNS],
38 &save_addr[i + OFFS_PATTERN]);
40 /* Make sure the data is written to RAM */
41 flush_dcache_range((ulong)&save_addr[0],
42 (((ulong)&save_addr[REPEAT_PATTERN + OFFS_PATTERN] &
43 ~(ARCH_DMA_MINALIGN - 1)) + ARCH_DMA_MINALIGN));
46 ulong bootcount_load(void)
53 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
54 size += gd->bd->bi_dram[i].size;
55 save_addr = (ulong *)(size - BOOTCOUNT_ADDR);
57 counter = readl(&save_addr[0]);
59 /* Is the counter reliable, check in the big pattern for bit errors */
60 for (i = 0; (i < REPEAT_PATTERN) && (counter != 0); i++) {
61 tmp = readl(&save_addr[i + OFFS_PATTERN]);
62 if (tmp != patterns[i % NBR_OF_PATTERNS])