Merge branch '2019-05-03-master-imports'
[platform/kernel/u-boot.git] / drivers / bootcount / bootcount_i2c.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2013
4  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
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);
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
26 ulong 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 }