blob: 496741d63f783a2928031451af36442cb377047e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher9e50c402014-01-25 07:27:13 +01002/*
3 * (C) Copyright 2013
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
Heiko Schocher9e50c402014-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);
20 ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR,
21 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
31 ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR,
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}