blob: b3ac67ea35dda5e491c5c1118e1719dde07cadf4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher443ca402014-01-25 07:27:13 +01002/*
3 * (C) Copyright 2013
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
Heiko Schocher443ca402014-01-25 07:27:13 +01005 */
6
7#include <bootcount.h>
8#include <linux/compiler.h>
9#include <i2c.h>
10
11#define BC_MAGIC 0xbc
12
13void 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 Rini6a5dccc2022-11-16 13:10:41 -050020 ret = i2c_write(CFG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR,
Heiko Schocher443ca402014-01-25 07:27:13 +010021 CONFIG_BOOTCOUNT_ALEN, buf, 2);
22 if (ret != 0)
23 puts("Error writing bootcount\n");
24}
25
26ulong bootcount_load(void)
27{
28 unsigned char buf[3];
29 int ret;
30
Tom Rini6a5dccc2022-11-16 13:10:41 -050031 ret = i2c_read(CFG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR,
Heiko Schocher443ca402014-01-25 07:27:13 +010032 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}