Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 2 | /* |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 3 | * (C) Copyright 2010-2012 |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 4 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 7 | #include <bootcount.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 8 | #include <cpu_func.h> |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 9 | #include <linux/compiler.h> |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 10 | |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 11 | /* Now implement the generic default functions */ |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 12 | __weak void bootcount_store(ulong a) |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 13 | { |
| 14 | void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR; |
Alex Kiernan | dc66357 | 2018-07-25 11:45:58 +0000 | [diff] [blame] | 15 | uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR, |
| 16 | CONFIG_SYS_CACHELINE_SIZE); |
| 17 | uintptr_t flush_end; |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 18 | |
| 19 | #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) |
Marek Vasut | 6ae0ded | 2018-10-11 00:13:54 +0200 | [diff] [blame] | 20 | raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a); |
Alex Kiernan | dc66357 | 2018-07-25 11:45:58 +0000 | [diff] [blame] | 21 | |
| 22 | flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4, |
| 23 | CONFIG_SYS_CACHELINE_SIZE); |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 24 | #else |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 25 | raw_bootcount_store(reg, a); |
Marek Vasut | 6ae0ded | 2018-10-11 00:13:54 +0200 | [diff] [blame] | 26 | raw_bootcount_store(reg + 4, CONFIG_SYS_BOOTCOUNT_MAGIC); |
Alex Kiernan | dc66357 | 2018-07-25 11:45:58 +0000 | [diff] [blame] | 27 | |
| 28 | flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8, |
| 29 | CONFIG_SYS_CACHELINE_SIZE); |
Robert P. J. Day | 26a4e39 | 2015-12-22 07:15:14 -0500 | [diff] [blame] | 30 | #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */ |
Alex Kiernan | dc66357 | 2018-07-25 11:45:58 +0000 | [diff] [blame] | 31 | flush_dcache_range(flush_start, flush_end); |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 34 | __weak ulong bootcount_load(void) |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 35 | { |
| 36 | void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR; |
| 37 | |
| 38 | #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 39 | u32 tmp = raw_bootcount_load(reg); |
Michael Weiss | 8eed66d | 2010-05-20 16:09:35 +0200 | [diff] [blame] | 40 | |
Marek Vasut | 6ae0ded | 2018-10-11 00:13:54 +0200 | [diff] [blame] | 41 | if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000)) |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 42 | return 0; |
| 43 | else |
Michael Weiss | 8eed66d | 2010-05-20 16:09:35 +0200 | [diff] [blame] | 44 | return (tmp & 0x0000ffff); |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 45 | #else |
Marek Vasut | 6ae0ded | 2018-10-11 00:13:54 +0200 | [diff] [blame] | 46 | if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC) |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 47 | return 0; |
| 48 | else |
Stefan Roese | 033848e | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 49 | return raw_bootcount_load(reg); |
Robert P. J. Day | 26a4e39 | 2015-12-22 07:15:14 -0500 | [diff] [blame] | 50 | #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */ |
Stefan Roese | ed7aaf8 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 51 | } |