logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

wear_leveling_internal.h (7533B)


  1. // Copyright 2022 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include "compiler_support.h"
  5. #include <stdint.h>
  6. #include <string.h>
  7. #if BACKING_STORE_WRITE_SIZE == 2
  8. typedef uint16_t backing_store_int_t;
  9. #elif BACKING_STORE_WRITE_SIZE == 4
  10. typedef uint32_t backing_store_int_t;
  11. #elif BACKING_STORE_WRITE_SIZE == 8
  12. typedef uint64_t backing_store_int_t;
  13. #else
  14. # error Invalid BACKING_STORE_WRITE_SIZE, needs to be 2/4/8.
  15. #endif
  16. #ifndef WEAR_LEVELING_BACKING_SIZE
  17. # error WEAR_LEVELING_BACKING_SIZE was not set.
  18. #endif
  19. #ifndef WEAR_LEVELING_LOGICAL_SIZE
  20. # error WEAR_LEVELING_LOGICAL_SIZE was not set.
  21. #endif
  22. #ifdef WEAR_LEVELING_DEBUG_OUTPUT
  23. # include <debug.h>
  24. # define bs_dprintf(...) dprintf("Backing store: " __VA_ARGS__)
  25. # define wl_dprintf(...) dprintf("Wear leveling: " __VA_ARGS__)
  26. # define wl_dump(address, value, length) \
  27. do { \
  28. dprintf("[0x%04X]: ", (int)(address)); \
  29. const uint8_t* p = (const uint8_t*)(value); \
  30. for (int i = 0; i < (length); ++i) { \
  31. dprintf(" %02X", (int)p[i]); \
  32. } \
  33. dprintf("\n"); \
  34. } while (0)
  35. #else
  36. # define wl_dprintf(...) \
  37. do { \
  38. } while (0)
  39. # define bs_dprintf(...) \
  40. do { \
  41. } while (0)
  42. # define wl_dump(...) \
  43. do { \
  44. } while (0)
  45. #endif // WEAR_LEVELING_DEBUG_OUTPUT
  46. #ifdef WEAR_LEVELING_ASSERTS
  47. # include <assert.h>
  48. # define wl_assert(...) assert(__VA_ARGS__)
  49. #else
  50. # define wl_assert(...) \
  51. do { \
  52. } while (0)
  53. #endif // WEAR_LEVELING_ASSERTS
  54. // Compile-time validation of configurable options
  55. STATIC_ASSERT(WEAR_LEVELING_BACKING_SIZE >= (WEAR_LEVELING_LOGICAL_SIZE * 2), "Total backing size must be at least twice the size of the logical size");
  56. STATIC_ASSERT(WEAR_LEVELING_LOGICAL_SIZE % BACKING_STORE_WRITE_SIZE == 0, "Logical size must be a multiple of write size");
  57. STATIC_ASSERT(WEAR_LEVELING_BACKING_SIZE % WEAR_LEVELING_LOGICAL_SIZE == 0, "Backing size must be a multiple of logical size");
  58. // Backing Store API, to be implemented elsewhere by flash driver etc.
  59. bool backing_store_init(void);
  60. bool backing_store_unlock(void);
  61. bool backing_store_erase(void);
  62. bool backing_store_write(uint32_t address, backing_store_int_t value);
  63. bool backing_store_write_bulk(uint32_t address, backing_store_int_t* values, size_t item_count); // weak implementation already provided, optimized implementation can be implemented by driver
  64. bool backing_store_lock(void);
  65. bool backing_store_read(uint32_t address, backing_store_int_t* value);
  66. bool backing_store_read_bulk(uint32_t address, backing_store_int_t* values, size_t item_count); // weak implementation already provided, optimized implementation can be implemented by driver
  67. /**
  68. * Helper type used to contain a write log entry.
  69. */
  70. typedef union write_log_entry_t {
  71. uint64_t raw64;
  72. uint32_t raw32[2];
  73. uint16_t raw16[4];
  74. uint8_t raw8[8];
  75. } write_log_entry_t;
  76. STATIC_ASSERT(sizeof(write_log_entry_t) == 8, "Wear leveling write log entry size was not 8");
  77. /**
  78. * Log entry type discriminator.
  79. */
  80. enum {
  81. // 0x00 -- Multi-byte storage type
  82. LOG_ENTRY_TYPE_MULTIBYTE,
  83. // 0x01 -- 2-byte backing store write optimization: address < 64
  84. LOG_ENTRY_TYPE_OPTIMIZED_64,
  85. // 0x02 -- 2-byte backing store write optimization: word-encoded 0/1 values
  86. LOG_ENTRY_TYPE_WORD_01,
  87. LOG_ENTRY_TYPES
  88. };
  89. STATIC_ASSERT(LOG_ENTRY_TYPES <= (1 << 2), "Too many log entry types to fit into 2 bits of storage");
  90. #define BITMASK_FOR_BITCOUNT(n) ((1 << (n)) - 1)
  91. #define LOG_ENTRY_GET_TYPE(entry) (((entry).raw8[0] >> 6) & BITMASK_FOR_BITCOUNT(2))
  92. #define LOG_ENTRY_MULTIBYTE_MAX_BYTES 5
  93. #define LOG_ENTRY_MULTIBYTE_GET_ADDRESS(entry) (((((uint32_t)((entry).raw8[0])) & BITMASK_FOR_BITCOUNT(3)) << 16) | (((uint32_t)((entry).raw8[1])) << 8) | (entry).raw8[2])
  94. #define LOG_ENTRY_MULTIBYTE_GET_LENGTH(entry) ((uint8_t)(((entry).raw8[0] >> 3) & BITMASK_FOR_BITCOUNT(3)))
  95. #define LOG_ENTRY_MAKE_MULTIBYTE(address, length) \
  96. (write_log_entry_t) { \
  97. .raw8 = { \
  98. [0] = (((((uint8_t)LOG_ENTRY_TYPE_MULTIBYTE) & BITMASK_FOR_BITCOUNT(2)) << 6) /* type */ \
  99. | ((((uint8_t)(length)) & BITMASK_FOR_BITCOUNT(3)) << 3) /* length */ \
  100. | ((((uint8_t)((address) >> 16))) & BITMASK_FOR_BITCOUNT(3)) /* address */ \
  101. ), \
  102. [1] = (((uint8_t)((address) >> 8)) & BITMASK_FOR_BITCOUNT(8)), /* address */ \
  103. [2] = (((uint8_t)(address)) & BITMASK_FOR_BITCOUNT(8)), /* address */ \
  104. } \
  105. }
  106. #define LOG_ENTRY_OPTIMIZED_64_GET_ADDRESS(entry) ((uint32_t)((entry).raw8[0] & BITMASK_FOR_BITCOUNT(6)))
  107. #define LOG_ENTRY_OPTIMIZED_64_GET_VALUE(entry) ((entry).raw8[1])
  108. #define LOG_ENTRY_MAKE_OPTIMIZED_64(address, value) \
  109. (write_log_entry_t) { \
  110. .raw8 = { \
  111. [0] = (((((uint8_t)LOG_ENTRY_TYPE_OPTIMIZED_64) & BITMASK_FOR_BITCOUNT(2)) << 6) /* type */ \
  112. | ((((uint8_t)(address))) & BITMASK_FOR_BITCOUNT(6)) /* address */ \
  113. ), \
  114. [1] = ((uint8_t)(value)), /* value */ \
  115. } \
  116. }
  117. #define LOG_ENTRY_WORD_01_GET_ADDRESS(entry) ((((uint32_t)(((entry).raw8[0]) & BITMASK_FOR_BITCOUNT(5))) << 9) | (((uint32_t)((entry).raw8[1])) << 1))
  118. #define LOG_ENTRY_WORD_01_GET_VALUE(entry) ((uint8_t)((entry).raw8[0] >> 5) & BITMASK_FOR_BITCOUNT(1))
  119. #define LOG_ENTRY_MAKE_WORD_01(address, value) \
  120. (write_log_entry_t) { \
  121. .raw8 = { \
  122. [0] = (((((uint8_t)LOG_ENTRY_TYPE_WORD_01) & BITMASK_FOR_BITCOUNT(2)) << 6) /* type */ \
  123. | (((((uint8_t)((value) ? 1 : 0))) & BITMASK_FOR_BITCOUNT(1)) << 5) /* value */ \
  124. | ((((uint8_t)((address) >> 9))) & BITMASK_FOR_BITCOUNT(5)) /* address */ \
  125. ), \
  126. [1] = (uint8_t)((address) >> 1), /* address */ \
  127. } \
  128. }