blob: 9c678e25f4ba40ab55bcd01e9538b4cc96e36a13 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese0044c422012-08-16 17:55:41 +00002/*
3 * (C) Copyright 2010
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
Stefan Roese0044c422012-08-16 17:55:41 +00005 */
6
7#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07008#include <cpu_func.h>
Stefan Roese0044c422012-08-16 17:55:41 +00009#include <asm/io.h>
10
11DECLARE_GLOBAL_DATA_PTR;
12
13const ulong patterns[] = { 0x00000000,
14 0xFFFFFFFF,
15 0xFF00FF00,
16 0x0F0F0F0F,
17 0xF0F0F0F0};
18const ulong NBR_OF_PATTERNS = sizeof(patterns) / sizeof(*patterns);
19const ulong OFFS_PATTERN = 3;
20const ulong REPEAT_PATTERN = 1000;
21
22void bootcount_store(ulong a)
23{
24 ulong *save_addr;
25 ulong size = 0;
26 int i;
27
28 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
29 size += gd->bd->bi_dram[i].size;
30 save_addr = (ulong *)(size - BOOTCOUNT_ADDR);
31 writel(a, save_addr);
Marek Vasut758694f2018-10-11 00:13:54 +020032 writel(CONFIG_SYS_BOOTCOUNT_MAGIC, &save_addr[1]);
Stefan Roese0044c422012-08-16 17:55:41 +000033
34 for (i = 0; i < REPEAT_PATTERN; i++)
35 writel(patterns[i % NBR_OF_PATTERNS],
36 &save_addr[i + OFFS_PATTERN]);
37
Stefan Roese20adda42016-04-06 11:02:22 +020038 /* Make sure the data is written to RAM */
39 flush_dcache_range((ulong)&save_addr[0],
Stefan Roese384b1d52016-11-18 17:21:52 +010040 (((ulong)&save_addr[REPEAT_PATTERN + OFFS_PATTERN] &
41 ~(ARCH_DMA_MINALIGN - 1)) + ARCH_DMA_MINALIGN));
Stefan Roese0044c422012-08-16 17:55:41 +000042}
43
44ulong bootcount_load(void)
45{
46 ulong *save_addr;
47 ulong size = 0;
48 ulong counter = 0;
49 int i, tmp;
50
51 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
52 size += gd->bd->bi_dram[i].size;
53 save_addr = (ulong *)(size - BOOTCOUNT_ADDR);
54
55 counter = readl(&save_addr[0]);
56
57 /* Is the counter reliable, check in the big pattern for bit errors */
58 for (i = 0; (i < REPEAT_PATTERN) && (counter != 0); i++) {
59 tmp = readl(&save_addr[i + OFFS_PATTERN]);
60 if (tmp != patterns[i % NBR_OF_PATTERNS])
61 counter = 0;
62 }
63 return counter;
64}