1 // SPDX-License-Identifier: GPL-2.0+
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
12 DECLARE_GLOBAL_DATA_PTR;
14 const ulong patterns[] = { 0x00000000,
19 const ulong NBR_OF_PATTERNS = sizeof(patterns) / sizeof(*patterns);
20 const ulong OFFS_PATTERN = 3;
21 const ulong REPEAT_PATTERN = 1000;
23 void bootcount_store(ulong a)
29 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
30 size += gd->bd->bi_dram[i].size;
31 save_addr = (ulong *)(size - BOOTCOUNT_ADDR);
33 writel(CONFIG_SYS_BOOTCOUNT_MAGIC, &save_addr[1]);
35 for (i = 0; i < REPEAT_PATTERN; i++)
36 writel(patterns[i % NBR_OF_PATTERNS],
37 &save_addr[i + OFFS_PATTERN]);
39 /* Make sure the data is written to RAM */
40 flush_dcache_range((ulong)&save_addr[0],
41 (((ulong)&save_addr[REPEAT_PATTERN + OFFS_PATTERN] &
42 ~(ARCH_DMA_MINALIGN - 1)) + ARCH_DMA_MINALIGN));
45 ulong bootcount_load(void)
52 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
53 size += gd->bd->bi_dram[i].size;
54 save_addr = (ulong *)(size - BOOTCOUNT_ADDR);
56 counter = readl(&save_addr[0]);
58 /* Is the counter reliable, check in the big pattern for bit errors */
59 for (i = 0; (i < REPEAT_PATTERN) && (counter != 0); i++) {
60 tmp = readl(&save_addr[i + OFFS_PATTERN]);
61 if (tmp != patterns[i % NBR_OF_PATTERNS])