bootcount.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2010-2012
  4. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  5. */
  6. #include <bootcount.h>
  7. #include <cpu_func.h>
  8. #include <asm/cache.h>
  9. #include <linux/compiler.h>
  10. #if !defined(CONFIG_DM_BOOTCOUNT)
  11. /* Now implement the generic default functions */
  12. __weak void bootcount_store(ulong a)
  13. {
  14. void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
  15. uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR,
  16. CONFIG_SYS_CACHELINE_SIZE);
  17. uintptr_t flush_end;
  18. #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
  19. raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a);
  20. flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
  21. CONFIG_SYS_CACHELINE_SIZE);
  22. #else
  23. raw_bootcount_store(reg, a);
  24. raw_bootcount_store(reg + 4, CONFIG_SYS_BOOTCOUNT_MAGIC);
  25. flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8,
  26. CONFIG_SYS_CACHELINE_SIZE);
  27. #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
  28. flush_dcache_range(flush_start, flush_end);
  29. }
  30. __weak ulong bootcount_load(void)
  31. {
  32. void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
  33. #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
  34. u32 tmp = raw_bootcount_load(reg);
  35. if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
  36. return 0;
  37. else
  38. return (tmp & 0x0000ffff);
  39. #else
  40. if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC)
  41. return 0;
  42. else
  43. return raw_bootcount_load(reg);
  44. #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
  45. }
  46. #else
  47. #include <dm.h>
  48. /*
  49. * struct bootcount_mem_priv - private bootcount mem driver data
  50. *
  51. * @base: base address used for bootcounter
  52. * @singleword: if true use only one 32 bit word for bootcounter
  53. */
  54. struct bootcount_mem_priv {
  55. phys_addr_t base;
  56. bool singleword;
  57. };
  58. static int bootcount_mem_get(struct udevice *dev, u32 *a)
  59. {
  60. struct bootcount_mem_priv *priv = dev_get_priv(dev);
  61. void *reg = (void *)priv->base;
  62. u32 magic = CONFIG_SYS_BOOTCOUNT_MAGIC;
  63. if (priv->singleword) {
  64. u32 tmp = raw_bootcount_load(reg);
  65. if ((tmp & 0xffff0000) != (magic & 0xffff0000))
  66. return -ENODEV;
  67. *a = (tmp & 0x0000ffff);
  68. } else {
  69. if (raw_bootcount_load(reg + 4) != magic)
  70. return -ENODEV;
  71. *a = raw_bootcount_load(reg);
  72. }
  73. return 0;
  74. };
  75. static int bootcount_mem_set(struct udevice *dev, const u32 a)
  76. {
  77. struct bootcount_mem_priv *priv = dev_get_priv(dev);
  78. void *reg = (void *)priv->base;
  79. u32 magic = CONFIG_SYS_BOOTCOUNT_MAGIC;
  80. uintptr_t flush_start = rounddown(priv->base,
  81. CONFIG_SYS_CACHELINE_SIZE);
  82. uintptr_t flush_end;
  83. if (priv->singleword) {
  84. raw_bootcount_store(reg, (magic & 0xffff0000) | a);
  85. flush_end = roundup(priv->base + 4,
  86. CONFIG_SYS_CACHELINE_SIZE);
  87. } else {
  88. raw_bootcount_store(reg, a);
  89. raw_bootcount_store(reg + 4, magic);
  90. flush_end = roundup(priv->base + 8,
  91. CONFIG_SYS_CACHELINE_SIZE);
  92. }
  93. flush_dcache_range(flush_start, flush_end);
  94. return 0;
  95. };
  96. static const struct bootcount_ops bootcount_mem_ops = {
  97. .get = bootcount_mem_get,
  98. .set = bootcount_mem_set,
  99. };
  100. static int bootcount_mem_probe(struct udevice *dev)
  101. {
  102. struct bootcount_mem_priv *priv = dev_get_priv(dev);
  103. priv->base = (phys_addr_t)dev_read_addr(dev);
  104. if (dev_read_bool(dev, "single-word"))
  105. priv->singleword = true;
  106. return 0;
  107. }
  108. static const struct udevice_id bootcount_mem_ids[] = {
  109. { .compatible = "u-boot,bootcount" },
  110. { }
  111. };
  112. U_BOOT_DRIVER(bootcount_mem) = {
  113. .name = "bootcount-mem",
  114. .id = UCLASS_BOOTCOUNT,
  115. .priv_auto = sizeof(struct bootcount_mem_priv),
  116. .probe = bootcount_mem_probe,
  117. .of_match = bootcount_mem_ids,
  118. .ops = &bootcount_mem_ops,
  119. };
  120. #endif