bootcount_nvmem.c 1.4 KB

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