blob: daee84316c7f5f7594f6eceace9d06225c4bbd89 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Stefan Roese0044c422012-08-16 17:55:41 +00002/*
3 * (C) Copyright 2012
4 * Stefan Roese, DENX Software Engineering, sr@denx.de.
Stefan Roese0044c422012-08-16 17:55:41 +00005 */
Lukasz Majewskic1cd21d2018-05-02 16:10:51 +02006#ifndef _BOOTCOUNT_H__
7#define _BOOTCOUNT_H__
Stefan Roese0044c422012-08-16 17:55:41 +00008
9#include <common.h>
10#include <asm/io.h>
11#include <asm/byteorder.h>
12
Philipp Tomsichebb73de2018-11-27 23:00:18 +010013#ifdef CONFIG_DM_BOOTCOUNT
14
15struct bootcount_ops {
16 /**
17 * get() - get the current bootcount value
18 *
19 * Returns the current counter value of the bootcount backing
20 * store.
21 *
22 * @dev: Device to read from
23 * @bootcount: Address to put the current bootcount value
24 */
25 int (*get)(struct udevice *dev, u32 *bootcount);
26
27 /**
28 * set() - set a bootcount value (e.g. to reset or increment)
29 *
30 * Sets the value in the bootcount backing store.
31 *
32 * @dev: Device to read from
33 * @bootcount: New bootcount value to store
34 */
35 int (*set)(struct udevice *dev, const u32 bootcount);
36};
37
38/* Access the operations for a bootcount device */
39#define bootcount_get_ops(dev) ((struct bootcount_ops *)(dev)->driver->ops)
40
41/**
42 * dm_bootcount_get() - Read the current value from a bootcount storage
43 *
44 * @dev: Device to read from
45 * @bootcount: Place to put the current bootcount
46 * @return 0 if OK, -ve on error
47 */
48int dm_bootcount_get(struct udevice *dev, u32 *bootcount);
49
50/**
51 * dm_bootcount_set() - Write a value to a bootcount storage
52 *
53 * @dev: Device to read from
54 * @bootcount: Value to be written to the backing storage
55 * @return 0 if OK, -ve on error
56 */
57int dm_bootcount_set(struct udevice *dev, u32 bootcount);
58
59#endif
60
Lukasz Majewski0da70412018-05-02 16:10:52 +020061#if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
62
Stefan Roese0044c422012-08-16 17:55:41 +000063#if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE)
64# if __BYTE_ORDER == __LITTLE_ENDIAN
65# define CONFIG_SYS_BOOTCOUNT_LE
66# else
67# define CONFIG_SYS_BOOTCOUNT_BE
68# endif
69#endif
70
71#ifdef CONFIG_SYS_BOOTCOUNT_LE
72static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
73{
74 out_le32(addr, data);
75}
76
77static inline u32 raw_bootcount_load(volatile u32 *addr)
78{
79 return in_le32(addr);
80}
81#else
82static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
83{
84 out_be32(addr, data);
85}
86
87static inline u32 raw_bootcount_load(volatile u32 *addr)
88{
89 return in_be32(addr);
90}
91#endif
Lukasz Majewski0da70412018-05-02 16:10:52 +020092
93DECLARE_GLOBAL_DATA_PTR;
94static inline int bootcount_error(void)
95{
96 unsigned long bootcount = bootcount_load();
97 unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
98
99 if (bootlimit && bootcount > bootlimit) {
100 printf("Warning: Bootlimit (%lu) exceeded.", bootlimit);
101 if (!(gd->flags & GD_FLG_SPL_INIT))
102 printf(" Using altbootcmd.");
103 printf("\n");
104
105 return 1;
106 }
107
108 return 0;
109}
110
111static inline void bootcount_inc(void)
112{
113 unsigned long bootcount = bootcount_load();
114
115 if (gd->flags & GD_FLG_SPL_INIT) {
116 bootcount_store(++bootcount);
117 return;
118 }
119
120#ifndef CONFIG_SPL_BUILD
121 /* Only increment bootcount when no bootcount support in SPL */
122#ifndef CONFIG_SPL_BOOTCOUNT_LIMIT
123 bootcount_store(++bootcount);
124#endif
125 env_set_ulong("bootcount", bootcount);
126#endif /* !CONFIG_SPL_BUILD */
127}
128
129#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)
130void bootcount_store(ulong a) {};
131ulong bootcount_load(void) { return 0; }
132#endif /* CONFIG_SPL_BUILD && !CONFIG_SPL_BOOTCOUNT_LIMIT */
133#else
134static inline int bootcount_error(void) { return 0; }
135static inline void bootcount_inc(void) {}
136#endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
Lukasz Majewskic1cd21d2018-05-02 16:10:51 +0200137#endif /* _BOOTCOUNT_H__ */