blob: 7a6d03dcca3b8268ddf84b70c8da588c96a22486 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roesee4a95d12010-04-28 10:47:36 +02002/*
Stefan Roese0044c422012-08-16 17:55:41 +00003 * (C) Copyright 2010-2012
Stefan Roesee4a95d12010-04-28 10:47:36 +02004 * Stefan Roese, DENX Software Engineering, sr@denx.de.
Stefan Roesee4a95d12010-04-28 10:47:36 +02005 */
6
Stefan Roese0044c422012-08-16 17:55:41 +00007#include <bootcount.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07008#include <cpu_func.h>
Stefan Roese0044c422012-08-16 17:55:41 +00009#include <linux/compiler.h>
Stefan Roesee4a95d12010-04-28 10:47:36 +020010
Stefan Roese0044c422012-08-16 17:55:41 +000011/* Now implement the generic default functions */
Stefan Roese0044c422012-08-16 17:55:41 +000012__weak void bootcount_store(ulong a)
Stefan Roesee4a95d12010-04-28 10:47:36 +020013{
14 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
Alex Kiernanfe9805f2018-07-25 11:45:58 +000015 uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR,
16 CONFIG_SYS_CACHELINE_SIZE);
17 uintptr_t flush_end;
Stefan Roesee4a95d12010-04-28 10:47:36 +020018
19#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
Marek Vasut758694f2018-10-11 00:13:54 +020020 raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a);
Alex Kiernanfe9805f2018-07-25 11:45:58 +000021
22 flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
23 CONFIG_SYS_CACHELINE_SIZE);
Stefan Roesee4a95d12010-04-28 10:47:36 +020024#else
Stefan Roese0044c422012-08-16 17:55:41 +000025 raw_bootcount_store(reg, a);
Marek Vasut758694f2018-10-11 00:13:54 +020026 raw_bootcount_store(reg + 4, CONFIG_SYS_BOOTCOUNT_MAGIC);
Alex Kiernanfe9805f2018-07-25 11:45:58 +000027
28 flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8,
29 CONFIG_SYS_CACHELINE_SIZE);
Robert P. J. Day76765372015-12-22 07:15:14 -050030#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
Alex Kiernanfe9805f2018-07-25 11:45:58 +000031 flush_dcache_range(flush_start, flush_end);
Stefan Roesee4a95d12010-04-28 10:47:36 +020032}
33
Stefan Roese0044c422012-08-16 17:55:41 +000034__weak ulong bootcount_load(void)
Stefan Roesee4a95d12010-04-28 10:47:36 +020035{
36 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
37
38#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
Stefan Roese0044c422012-08-16 17:55:41 +000039 u32 tmp = raw_bootcount_load(reg);
Michael Weiss59dde442010-05-20 16:09:35 +020040
Marek Vasut758694f2018-10-11 00:13:54 +020041 if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
Stefan Roesee4a95d12010-04-28 10:47:36 +020042 return 0;
43 else
Michael Weiss59dde442010-05-20 16:09:35 +020044 return (tmp & 0x0000ffff);
Stefan Roesee4a95d12010-04-28 10:47:36 +020045#else
Marek Vasut758694f2018-10-11 00:13:54 +020046 if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC)
Stefan Roesee4a95d12010-04-28 10:47:36 +020047 return 0;
48 else
Stefan Roese0044c422012-08-16 17:55:41 +000049 return raw_bootcount_load(reg);
Robert P. J. Day76765372015-12-22 07:15:14 -050050#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
Stefan Roesee4a95d12010-04-28 10:47:36 +020051}