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