Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2010 |
| 4 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 8 | #include <cpu_func.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 9 | #include <asm/cache.h> |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
| 14 | const ulong patterns[] = { 0x00000000, |
| 15 | 0xFFFFFFFF, |
| 16 | 0xFF00FF00, |
| 17 | 0x0F0F0F0F, |
| 18 | 0xF0F0F0F0}; |
| 19 | const ulong NBR_OF_PATTERNS = sizeof(patterns) / sizeof(*patterns); |
| 20 | const ulong OFFS_PATTERN = 3; |
| 21 | const ulong REPEAT_PATTERN = 1000; |
| 22 | |
| 23 | void bootcount_store(ulong a) |
| 24 | { |
| 25 | ulong *save_addr; |
| 26 | ulong size = 0; |
| 27 | int i; |
| 28 | |
| 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); |
| 32 | writel(a, save_addr); |
Marek Vasut | 758694f | 2018-10-11 00:13:54 +0200 | [diff] [blame] | 33 | writel(CONFIG_SYS_BOOTCOUNT_MAGIC, &save_addr[1]); |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 34 | |
| 35 | for (i = 0; i < REPEAT_PATTERN; i++) |
| 36 | writel(patterns[i % NBR_OF_PATTERNS], |
| 37 | &save_addr[i + OFFS_PATTERN]); |
| 38 | |
Stefan Roese | 20adda4 | 2016-04-06 11:02:22 +0200 | [diff] [blame] | 39 | /* Make sure the data is written to RAM */ |
| 40 | flush_dcache_range((ulong)&save_addr[0], |
Stefan Roese | 384b1d5 | 2016-11-18 17:21:52 +0100 | [diff] [blame] | 41 | (((ulong)&save_addr[REPEAT_PATTERN + OFFS_PATTERN] & |
| 42 | ~(ARCH_DMA_MINALIGN - 1)) + ARCH_DMA_MINALIGN)); |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ulong bootcount_load(void) |
| 46 | { |
| 47 | ulong *save_addr; |
| 48 | ulong size = 0; |
| 49 | ulong counter = 0; |
| 50 | int i, tmp; |
| 51 | |
| 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); |
| 55 | |
| 56 | counter = readl(&save_addr[0]); |
| 57 | |
| 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]) |
| 62 | counter = 0; |
| 63 | } |
| 64 | return counter; |
| 65 | } |