eeprom_samd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Copyright 2017 Fred Sundvik
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "eeprom.h"
  17. #include "debug.h"
  18. #include "util.h"
  19. #include "samd51j18a.h"
  20. #include "core_cm4.h"
  21. #include "component/nvmctrl.h"
  22. #include "eeprom_samd.h"
  23. #ifndef BUSY_RETRIES
  24. # define BUSY_RETRIES 10000
  25. #endif
  26. // #define DEBUG_EEPROM_OUTPUT
  27. /*
  28. * Debug print utils
  29. */
  30. #if defined(DEBUG_EEPROM_OUTPUT)
  31. # define eeprom_printf(fmt, ...) xprintf(fmt, ##__VA_ARGS__);
  32. #else /* NO_DEBUG */
  33. # define eeprom_printf(fmt, ...)
  34. #endif /* NO_DEBUG */
  35. __attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE] = {0};
  36. volatile uint8_t * SmartEEPROM8 = (uint8_t *)SEEPROM_ADDR;
  37. static inline bool eeprom_is_busy(void) {
  38. int timeout = BUSY_RETRIES;
  39. while (NVMCTRL->SEESTAT.bit.BUSY && timeout-- > 0)
  40. ;
  41. return NVMCTRL->SEESTAT.bit.BUSY;
  42. }
  43. static uint32_t get_virtual_eeprom_size(void) {
  44. // clang-format off
  45. static const uint32_t VIRTUAL_EEPROM_MAP[11][8] = {
  46. /* 4 8 16 32 64 128 256 512 */
  47. /* 0*/ { 0, 0, 0, 0, 0, 0, 0, 0 },
  48. /* 1*/ { 512, 1024, 2048, 4096, 4096, 4096, 4096, 4096 },
  49. /* 2*/ { 512, 1024, 2048, 4096, 8192, 8192, 8192, 8192 },
  50. /* 3*/ { 512, 1024, 2048, 4096, 8192, 16384, 16384, 16384 },
  51. /* 4*/ { 512, 1024, 2048, 4096, 8192, 16384, 16384, 16384 },
  52. /* 5*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
  53. /* 6*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
  54. /* 7*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
  55. /* 8*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 },
  56. /* 9*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 },
  57. /*10*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 },
  58. };
  59. // clang-format on
  60. static uint32_t virtual_eeprom_size = UINT32_MAX;
  61. if (virtual_eeprom_size == UINT32_MAX) {
  62. virtual_eeprom_size = VIRTUAL_EEPROM_MAP[NVMCTRL->SEESTAT.bit.PSZ][NVMCTRL->SEESTAT.bit.SBLK];
  63. }
  64. // eeprom_printf("get_virtual_eeprom_size:: %d:%d:%d\n", NVMCTRL->SEESTAT.bit.PSZ, NVMCTRL->SEESTAT.bit.SBLK, virtual_eeprom_size);
  65. return virtual_eeprom_size;
  66. }
  67. uint8_t eeprom_read_byte(const uint8_t *addr) {
  68. uintptr_t offset = (uintptr_t)addr;
  69. if (offset >= MAX(EEPROM_SIZE, get_virtual_eeprom_size())) {
  70. eeprom_printf("eeprom_read_byte:: out of bounds\n");
  71. return 0x0;
  72. }
  73. if (get_virtual_eeprom_size() == 0) {
  74. return buffer[offset];
  75. }
  76. if (eeprom_is_busy()) {
  77. eeprom_printf("eeprom_write_byte:: timeout\n");
  78. return 0x0;
  79. }
  80. return SmartEEPROM8[offset];
  81. }
  82. void eeprom_write_byte(uint8_t *addr, uint8_t value) {
  83. uintptr_t offset = (uintptr_t)addr;
  84. if (offset >= MAX(EEPROM_SIZE, get_virtual_eeprom_size())) {
  85. eeprom_printf("eeprom_write_byte:: out of bounds\n");
  86. return;
  87. }
  88. if (get_virtual_eeprom_size() == 0) {
  89. buffer[offset] = value;
  90. return;
  91. }
  92. if (eeprom_is_busy()) {
  93. eeprom_printf("eeprom_write_byte:: timeout\n");
  94. return;
  95. }
  96. SmartEEPROM8[offset] = value;
  97. }
  98. uint16_t eeprom_read_word(const uint16_t *addr) {
  99. const uint8_t *p = (const uint8_t *)addr;
  100. return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8);
  101. }
  102. uint32_t eeprom_read_dword(const uint32_t *addr) {
  103. const uint8_t *p = (const uint8_t *)addr;
  104. return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24);
  105. }
  106. void eeprom_read_block(void *buf, const void *addr, size_t len) {
  107. const uint8_t *p = (const uint8_t *)addr;
  108. uint8_t * dest = (uint8_t *)buf;
  109. while (len--) {
  110. *dest++ = eeprom_read_byte(p++);
  111. }
  112. }
  113. void eeprom_write_word(uint16_t *addr, uint16_t value) {
  114. uint8_t *p = (uint8_t *)addr;
  115. eeprom_write_byte(p++, value);
  116. eeprom_write_byte(p, value >> 8);
  117. }
  118. void eeprom_write_dword(uint32_t *addr, uint32_t value) {
  119. uint8_t *p = (uint8_t *)addr;
  120. eeprom_write_byte(p++, value);
  121. eeprom_write_byte(p++, value >> 8);
  122. eeprom_write_byte(p++, value >> 16);
  123. eeprom_write_byte(p, value >> 24);
  124. }
  125. void eeprom_write_block(const void *buf, void *addr, size_t len) {
  126. uint8_t * p = (uint8_t *)addr;
  127. const uint8_t *src = (const uint8_t *)buf;
  128. while (len--) {
  129. eeprom_write_byte(p++, *src++);
  130. }
  131. }
  132. void eeprom_update_byte(uint8_t *addr, uint8_t value) {
  133. eeprom_write_byte(addr, value);
  134. }
  135. void eeprom_update_word(uint16_t *addr, uint16_t value) {
  136. uint8_t *p = (uint8_t *)addr;
  137. eeprom_write_byte(p++, value);
  138. eeprom_write_byte(p, value >> 8);
  139. }
  140. void eeprom_update_dword(uint32_t *addr, uint32_t value) {
  141. uint8_t *p = (uint8_t *)addr;
  142. eeprom_write_byte(p++, value);
  143. eeprom_write_byte(p++, value >> 8);
  144. eeprom_write_byte(p++, value >> 16);
  145. eeprom_write_byte(p, value >> 24);
  146. }
  147. void eeprom_update_block(const void *buf, void *addr, size_t len) {
  148. uint8_t * p = (uint8_t *)addr;
  149. const uint8_t *src = (const uint8_t *)buf;
  150. while (len--) {
  151. eeprom_write_byte(p++, *src++);
  152. }
  153. }