Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 9e50c40 | 2014-01-25 07:27:13 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 |
| 4 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
Heiko Schocher | 9e50c40 | 2014-01-25 07:27:13 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <bootcount.h> |
| 8 | #include <linux/compiler.h> |
| 9 | #include <i2c.h> |
| 10 | |
| 11 | #define BC_MAGIC 0xbc |
| 12 | |
| 13 | void bootcount_store(ulong a) |
| 14 | { |
| 15 | unsigned char buf[3]; |
| 16 | int ret; |
| 17 | |
| 18 | buf[0] = BC_MAGIC; |
| 19 | buf[1] = (a & 0xff); |
Tom Rini | 65cc0e2 | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 20 | ret = i2c_write(CFG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR, |
Heiko Schocher | 9e50c40 | 2014-01-25 07:27:13 +0100 | [diff] [blame] | 21 | CONFIG_BOOTCOUNT_ALEN, buf, 2); |
| 22 | if (ret != 0) |
| 23 | puts("Error writing bootcount\n"); |
| 24 | } |
| 25 | |
| 26 | ulong bootcount_load(void) |
| 27 | { |
| 28 | unsigned char buf[3]; |
| 29 | int ret; |
| 30 | |
Tom Rini | 65cc0e2 | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 31 | ret = i2c_read(CFG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR, |
Heiko Schocher | 9e50c40 | 2014-01-25 07:27:13 +0100 | [diff] [blame] | 32 | CONFIG_BOOTCOUNT_ALEN, buf, 2); |
| 33 | if (ret != 0) { |
| 34 | puts("Error loading bootcount\n"); |
| 35 | return 0; |
| 36 | } |
| 37 | if (buf[0] == BC_MAGIC) |
| 38 | return buf[1]; |
| 39 | |
| 40 | bootcount_store(0); |
| 41 | |
| 42 | return 0; |
| 43 | } |