bootcount_i2c.c 804 B

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