bootcount_i2c.c 807 B

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