1 /*
2  * (C) Copyright 2011
3  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
4  *
5  * A bootcount driver for the RTC IP block found on many TI platforms.
6  * This requires the RTC clocks, etc, to be enabled prior to use and
7  * not all boards with this IP block on it will have the RTC in use.
8  *
9  * SPDX-License-Identifier:	GPL-2.0+
10  */
11 
12 #include <bootcount.h>
13 #include <asm/davinci_rtc.h>
14 
15 void bootcount_store(ulong a)
16 {
17 	struct davinci_rtc *reg =
18 		(struct davinci_rtc *)CONFIG_SYS_BOOTCOUNT_ADDR;
19 
20 	/*
21 	 * write RTC kick register to enable write
22 	 * for RTC Scratch registers. Scratch0 and 1 are
23 	 * used for bootcount values.
24 	 */
25 	writel(RTC_KICK0R_WE, &reg->kick0r);
26 	writel(RTC_KICK1R_WE, &reg->kick1r);
27 	raw_bootcount_store(&reg->scratch2,
28 			    (BOOTCOUNT_MAGIC & 0xffff0000) | (a & 0x0000ffff));
29 }
30 
31 ulong bootcount_load(void)
32 {
33 	unsigned long val;
34 	struct davinci_rtc *reg =
35 		(struct davinci_rtc *)CONFIG_SYS_BOOTCOUNT_ADDR;
36 
37 	val = raw_bootcount_load(&reg->scratch2);
38 	if ((val & 0xffff0000) != (BOOTCOUNT_MAGIC & 0xffff0000))
39 		return 0;
40 	else
41 		return val & 0x0000ffff;
42 }
43