summaryrefslogtreecommitdiff
path: root/drivers/bootcount/bootcount_i2c.c
blob: 496741d63f783a2928031451af36442cb377047e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2013
 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
 */

#include <bootcount.h>
#include <linux/compiler.h>
#include <i2c.h>

#define BC_MAGIC	0xbc

void bootcount_store(ulong a)
{
	unsigned char buf[3];
	int ret;

	buf[0] = BC_MAGIC;
	buf[1] = (a & 0xff);
	ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR,
		  CONFIG_BOOTCOUNT_ALEN, buf, 2);
	if (ret != 0)
		puts("Error writing bootcount\n");
}

ulong bootcount_load(void)
{
	unsigned char buf[3];
	int ret;

	ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR,
		       CONFIG_BOOTCOUNT_ALEN, buf, 2);
	if (ret != 0) {
		puts("Error loading bootcount\n");
		return 0;
	}
	if (buf[0] == BC_MAGIC)
		return buf[1];

	bootcount_store(0);

	return 0;
}