183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
20044c42eSStefan Roese /*
30044c42eSStefan Roese * (C) Copyright 2012
40044c42eSStefan Roese * Stefan Roese, DENX Software Engineering, sr@denx.de.
50044c42eSStefan Roese */
6c1cd21dbSLukasz Majewski #ifndef _BOOTCOUNT_H__
7c1cd21dbSLukasz Majewski #define _BOOTCOUNT_H__
80044c42eSStefan Roese
90044c42eSStefan Roese #include <common.h>
100044c42eSStefan Roese #include <asm/io.h>
110044c42eSStefan Roese #include <asm/byteorder.h>
120044c42eSStefan Roese
13*ebb73de1SPhilipp Tomsich #ifdef CONFIG_DM_BOOTCOUNT
14*ebb73de1SPhilipp Tomsich
15*ebb73de1SPhilipp Tomsich struct bootcount_ops {
16*ebb73de1SPhilipp Tomsich /**
17*ebb73de1SPhilipp Tomsich * get() - get the current bootcount value
18*ebb73de1SPhilipp Tomsich *
19*ebb73de1SPhilipp Tomsich * Returns the current counter value of the bootcount backing
20*ebb73de1SPhilipp Tomsich * store.
21*ebb73de1SPhilipp Tomsich *
22*ebb73de1SPhilipp Tomsich * @dev: Device to read from
23*ebb73de1SPhilipp Tomsich * @bootcount: Address to put the current bootcount value
24*ebb73de1SPhilipp Tomsich */
25*ebb73de1SPhilipp Tomsich int (*get)(struct udevice *dev, u32 *bootcount);
26*ebb73de1SPhilipp Tomsich
27*ebb73de1SPhilipp Tomsich /**
28*ebb73de1SPhilipp Tomsich * set() - set a bootcount value (e.g. to reset or increment)
29*ebb73de1SPhilipp Tomsich *
30*ebb73de1SPhilipp Tomsich * Sets the value in the bootcount backing store.
31*ebb73de1SPhilipp Tomsich *
32*ebb73de1SPhilipp Tomsich * @dev: Device to read from
33*ebb73de1SPhilipp Tomsich * @bootcount: New bootcount value to store
34*ebb73de1SPhilipp Tomsich */
35*ebb73de1SPhilipp Tomsich int (*set)(struct udevice *dev, const u32 bootcount);
36*ebb73de1SPhilipp Tomsich };
37*ebb73de1SPhilipp Tomsich
38*ebb73de1SPhilipp Tomsich /* Access the operations for a bootcount device */
39*ebb73de1SPhilipp Tomsich #define bootcount_get_ops(dev) ((struct bootcount_ops *)(dev)->driver->ops)
40*ebb73de1SPhilipp Tomsich
41*ebb73de1SPhilipp Tomsich /**
42*ebb73de1SPhilipp Tomsich * dm_bootcount_get() - Read the current value from a bootcount storage
43*ebb73de1SPhilipp Tomsich *
44*ebb73de1SPhilipp Tomsich * @dev: Device to read from
45*ebb73de1SPhilipp Tomsich * @bootcount: Place to put the current bootcount
46*ebb73de1SPhilipp Tomsich * @return 0 if OK, -ve on error
47*ebb73de1SPhilipp Tomsich */
48*ebb73de1SPhilipp Tomsich int dm_bootcount_get(struct udevice *dev, u32 *bootcount);
49*ebb73de1SPhilipp Tomsich
50*ebb73de1SPhilipp Tomsich /**
51*ebb73de1SPhilipp Tomsich * dm_bootcount_set() - Write a value to a bootcount storage
52*ebb73de1SPhilipp Tomsich *
53*ebb73de1SPhilipp Tomsich * @dev: Device to read from
54*ebb73de1SPhilipp Tomsich * @bootcount: Value to be written to the backing storage
55*ebb73de1SPhilipp Tomsich * @return 0 if OK, -ve on error
56*ebb73de1SPhilipp Tomsich */
57*ebb73de1SPhilipp Tomsich int dm_bootcount_set(struct udevice *dev, u32 bootcount);
58*ebb73de1SPhilipp Tomsich
59*ebb73de1SPhilipp Tomsich #endif
60*ebb73de1SPhilipp Tomsich
610da70412SLukasz Majewski #if defined(CONFIG_SPL_BOOTCOUNT_LIMIT) || defined(CONFIG_BOOTCOUNT_LIMIT)
620da70412SLukasz Majewski
630044c42eSStefan Roese #if !defined(CONFIG_SYS_BOOTCOUNT_LE) && !defined(CONFIG_SYS_BOOTCOUNT_BE)
640044c42eSStefan Roese # if __BYTE_ORDER == __LITTLE_ENDIAN
650044c42eSStefan Roese # define CONFIG_SYS_BOOTCOUNT_LE
660044c42eSStefan Roese # else
670044c42eSStefan Roese # define CONFIG_SYS_BOOTCOUNT_BE
680044c42eSStefan Roese # endif
690044c42eSStefan Roese #endif
700044c42eSStefan Roese
710044c42eSStefan Roese #ifdef CONFIG_SYS_BOOTCOUNT_LE
raw_bootcount_store(volatile u32 * addr,u32 data)720044c42eSStefan Roese static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
730044c42eSStefan Roese {
740044c42eSStefan Roese out_le32(addr, data);
750044c42eSStefan Roese }
760044c42eSStefan Roese
raw_bootcount_load(volatile u32 * addr)770044c42eSStefan Roese static inline u32 raw_bootcount_load(volatile u32 *addr)
780044c42eSStefan Roese {
790044c42eSStefan Roese return in_le32(addr);
800044c42eSStefan Roese }
810044c42eSStefan Roese #else
raw_bootcount_store(volatile u32 * addr,u32 data)820044c42eSStefan Roese static inline void raw_bootcount_store(volatile u32 *addr, u32 data)
830044c42eSStefan Roese {
840044c42eSStefan Roese out_be32(addr, data);
850044c42eSStefan Roese }
860044c42eSStefan Roese
raw_bootcount_load(volatile u32 * addr)870044c42eSStefan Roese static inline u32 raw_bootcount_load(volatile u32 *addr)
880044c42eSStefan Roese {
890044c42eSStefan Roese return in_be32(addr);
900044c42eSStefan Roese }
910044c42eSStefan Roese #endif
920da70412SLukasz Majewski
930da70412SLukasz Majewski DECLARE_GLOBAL_DATA_PTR;
bootcount_error(void)940da70412SLukasz Majewski static inline int bootcount_error(void)
950da70412SLukasz Majewski {
960da70412SLukasz Majewski unsigned long bootcount = bootcount_load();
970da70412SLukasz Majewski unsigned long bootlimit = env_get_ulong("bootlimit", 10, 0);
980da70412SLukasz Majewski
990da70412SLukasz Majewski if (bootlimit && bootcount > bootlimit) {
1000da70412SLukasz Majewski printf("Warning: Bootlimit (%lu) exceeded.", bootlimit);
1010da70412SLukasz Majewski if (!(gd->flags & GD_FLG_SPL_INIT))
1020da70412SLukasz Majewski printf(" Using altbootcmd.");
1030da70412SLukasz Majewski printf("\n");
1040da70412SLukasz Majewski
1050da70412SLukasz Majewski return 1;
1060da70412SLukasz Majewski }
1070da70412SLukasz Majewski
1080da70412SLukasz Majewski return 0;
1090da70412SLukasz Majewski }
1100da70412SLukasz Majewski
bootcount_inc(void)1110da70412SLukasz Majewski static inline void bootcount_inc(void)
1120da70412SLukasz Majewski {
1130da70412SLukasz Majewski unsigned long bootcount = bootcount_load();
1140da70412SLukasz Majewski
1150da70412SLukasz Majewski if (gd->flags & GD_FLG_SPL_INIT) {
1160da70412SLukasz Majewski bootcount_store(++bootcount);
1170da70412SLukasz Majewski return;
1180da70412SLukasz Majewski }
1190da70412SLukasz Majewski
1200da70412SLukasz Majewski #ifndef CONFIG_SPL_BUILD
1210da70412SLukasz Majewski /* Only increment bootcount when no bootcount support in SPL */
1220da70412SLukasz Majewski #ifndef CONFIG_SPL_BOOTCOUNT_LIMIT
1230da70412SLukasz Majewski bootcount_store(++bootcount);
1240da70412SLukasz Majewski #endif
1250da70412SLukasz Majewski env_set_ulong("bootcount", bootcount);
1260da70412SLukasz Majewski #endif /* !CONFIG_SPL_BUILD */
1270da70412SLukasz Majewski }
1280da70412SLukasz Majewski
1290da70412SLukasz Majewski #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_BOOTCOUNT_LIMIT)
bootcount_store(ulong a)1300da70412SLukasz Majewski void bootcount_store(ulong a) {};
bootcount_load(void)1310da70412SLukasz Majewski ulong bootcount_load(void) { return 0; }
1320da70412SLukasz Majewski #endif /* CONFIG_SPL_BUILD && !CONFIG_SPL_BOOTCOUNT_LIMIT */
1330da70412SLukasz Majewski #else
bootcount_error(void)1340da70412SLukasz Majewski static inline int bootcount_error(void) { return 0; }
bootcount_inc(void)1350da70412SLukasz Majewski static inline void bootcount_inc(void) {}
1360da70412SLukasz Majewski #endif /* CONFIG_SPL_BOOTCOUNT_LIMIT || CONFIG_BOOTCOUNT_LIMIT */
137c1cd21dbSLukasz Majewski #endif /* _BOOTCOUNT_H__ */
138