blob: 8b499fe1286143b22e96d1c10532d7548cc75a9b [file] [log] [blame]
Stefan Roeseed7aaf82010-04-28 10:47:36 +02001/*
Stefan Roese033848e2012-08-16 17:55:41 +00002 * (C) Copyright 2010-2012
Stefan Roeseed7aaf82010-04-28 10:47:36 +02003 * Stefan Roese, DENX Software Engineering, sr@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Stefan Roeseed7aaf82010-04-28 10:47:36 +02006 */
7
Stefan Roese033848e2012-08-16 17:55:41 +00008#include <bootcount.h>
9#include <linux/compiler.h>
Stefan Roeseed7aaf82010-04-28 10:47:36 +020010
11/*
12 * Only override CONFIG_SYS_BOOTCOUNT_ADDR if not already defined. This
13 * way, some boards can define it directly in their config header.
14 */
15#if !defined(CONFIG_SYS_BOOTCOUNT_ADDR)
16
Heiko Schocher466924f2010-02-18 08:08:25 +010017#if defined(CONFIG_QE)
Zhao Qiang978679d2014-06-03 16:27:07 +080018#include <linux/immap_qe.h>
Stefan Roeseed7aaf82010-04-28 10:47:36 +020019#define CONFIG_SYS_BOOTCOUNT_ADDR (CONFIG_SYS_IMMR + 0x110000 + \
20 QE_MURAM_SIZE - 2 * sizeof(u32))
Robert P. J. Day26a4e392015-12-22 07:15:14 -050021#endif /* defined(CONFIG_QE) */
Stefan Roeseed7aaf82010-04-28 10:47:36 +020022
Stefan Roeseed7aaf82010-04-28 10:47:36 +020023#endif /* !defined(CONFIG_SYS_BOOTCOUNT_ADDR) */
24
Stefan Roese033848e2012-08-16 17:55:41 +000025/* Now implement the generic default functions */
26#if defined(CONFIG_SYS_BOOTCOUNT_ADDR)
27__weak void bootcount_store(ulong a)
Stefan Roeseed7aaf82010-04-28 10:47:36 +020028{
29 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
30
31#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
Stefan Roese033848e2012-08-16 17:55:41 +000032 raw_bootcount_store(reg, (BOOTCOUNT_MAGIC & 0xffff0000) | a);
Stefan Roeseed7aaf82010-04-28 10:47:36 +020033#else
Stefan Roese033848e2012-08-16 17:55:41 +000034 raw_bootcount_store(reg, a);
35 raw_bootcount_store(reg + 4, BOOTCOUNT_MAGIC);
Robert P. J. Day26a4e392015-12-22 07:15:14 -050036#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
Stefan Roeseed7aaf82010-04-28 10:47:36 +020037}
38
Stefan Roese033848e2012-08-16 17:55:41 +000039__weak ulong bootcount_load(void)
Stefan Roeseed7aaf82010-04-28 10:47:36 +020040{
41 void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
42
43#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
Stefan Roese033848e2012-08-16 17:55:41 +000044 u32 tmp = raw_bootcount_load(reg);
Michael Weiss8eed66d2010-05-20 16:09:35 +020045
46 if ((tmp & 0xffff0000) != (BOOTCOUNT_MAGIC & 0xffff0000))
Stefan Roeseed7aaf82010-04-28 10:47:36 +020047 return 0;
48 else
Michael Weiss8eed66d2010-05-20 16:09:35 +020049 return (tmp & 0x0000ffff);
Stefan Roeseed7aaf82010-04-28 10:47:36 +020050#else
Stefan Roese033848e2012-08-16 17:55:41 +000051 if (raw_bootcount_load(reg + 4) != BOOTCOUNT_MAGIC)
Stefan Roeseed7aaf82010-04-28 10:47:36 +020052 return 0;
53 else
Stefan Roese033848e2012-08-16 17:55:41 +000054 return raw_bootcount_load(reg);
Robert P. J. Day26a4e392015-12-22 07:15:14 -050055#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
Stefan Roeseed7aaf82010-04-28 10:47:36 +020056}
Robert P. J. Day26a4e392015-12-22 07:15:14 -050057#endif /* defined(CONFIG_SYS_BOOTCOUNT_ADDR) */