blob: 646c563f8a4a5f7d400ff1af39a77ea822fbf19e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roeseed7aaf82010-04-28 10:47:36 +02002/*
Stefan Roese033848e2012-08-16 17:55:41 +00003 * (C) Copyright 2010-2012
Stefan Roeseed7aaf82010-04-28 10:47:36 +02004 * Stefan Roese, DENX Software Engineering, sr@denx.de.
Stefan Roeseed7aaf82010-04-28 10:47:36 +02005 */
6
Stefan Roese033848e2012-08-16 17:55:41 +00007#include <bootcount.h>
8#include <linux/compiler.h>
Stefan Roeseed7aaf82010-04-28 10:47:36 +02009
Stefan Roese033848e2012-08-16 17:55:41 +000010/* Now implement the generic default functions */
Stefan Roese033848e2012-08-16 17:55:41 +000011__weak void bootcount_store(ulong a)
Stefan Roeseed7aaf82010-04-28 10:47:36 +020012{
13 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
Alex Kiernandc663572018-07-25 11:45:58 +000014 uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR,
15 CONFIG_SYS_CACHELINE_SIZE);
16 uintptr_t flush_end;
Stefan Roeseed7aaf82010-04-28 10:47:36 +020017
18#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
Stefan Roese033848e2012-08-16 17:55:41 +000019 raw_bootcount_store(reg, (BOOTCOUNT_MAGIC & 0xffff0000) | a);
Alex Kiernandc663572018-07-25 11:45:58 +000020
21 flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
22 CONFIG_SYS_CACHELINE_SIZE);
Stefan Roeseed7aaf82010-04-28 10:47:36 +020023#else
Stefan Roese033848e2012-08-16 17:55:41 +000024 raw_bootcount_store(reg, a);
25 raw_bootcount_store(reg + 4, BOOTCOUNT_MAGIC);
Alex Kiernandc663572018-07-25 11:45:58 +000026
27 flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8,
28 CONFIG_SYS_CACHELINE_SIZE);
Robert P. J. Day26a4e392015-12-22 07:15:14 -050029#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
Alex Kiernandc663572018-07-25 11:45:58 +000030 flush_dcache_range(flush_start, flush_end);
Stefan Roeseed7aaf82010-04-28 10:47:36 +020031}
32
Stefan Roese033848e2012-08-16 17:55:41 +000033__weak ulong bootcount_load(void)
Stefan Roeseed7aaf82010-04-28 10:47:36 +020034{
35 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
36
37#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
Stefan Roese033848e2012-08-16 17:55:41 +000038 u32 tmp = raw_bootcount_load(reg);
Michael Weiss8eed66d2010-05-20 16:09:35 +020039
40 if ((tmp & 0xffff0000) != (BOOTCOUNT_MAGIC & 0xffff0000))
Stefan Roeseed7aaf82010-04-28 10:47:36 +020041 return 0;
42 else
Michael Weiss8eed66d2010-05-20 16:09:35 +020043 return (tmp & 0x0000ffff);
Stefan Roeseed7aaf82010-04-28 10:47:36 +020044#else
Stefan Roese033848e2012-08-16 17:55:41 +000045 if (raw_bootcount_load(reg + 4) != BOOTCOUNT_MAGIC)
Stefan Roeseed7aaf82010-04-28 10:47:36 +020046 return 0;
47 else
Stefan Roese033848e2012-08-16 17:55:41 +000048 return raw_bootcount_load(reg);
Robert P. J. Day26a4e392015-12-22 07:15:14 -050049#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
Stefan Roeseed7aaf82010-04-28 10:47:36 +020050}