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