diff options
Diffstat (limited to 'platforms/arm_atsam/eeprom.c')
-rw-r--r-- | platforms/arm_atsam/eeprom.c | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/platforms/arm_atsam/eeprom.c b/platforms/arm_atsam/eeprom.c new file mode 100644 index 000000000..ff1a69262 --- /dev/null +++ b/platforms/arm_atsam/eeprom.c | |||
@@ -0,0 +1,184 @@ | |||
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 "samd51j18a.h" | ||
19 | #include "core_cm4.h" | ||
20 | #include "component/nvmctrl.h" | ||
21 | |||
22 | #ifndef EEPROM_SIZE | ||
23 | # include "eeconfig.h" | ||
24 | # define EEPROM_SIZE (((EECONFIG_SIZE + 3) / 4) * 4) // based off eeconfig's current usage, aligned to 4-byte sizes, to deal with LTO | ||
25 | #endif | ||
26 | |||
27 | #ifndef MAX | ||
28 | # define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) | ||
29 | #endif | ||
30 | |||
31 | #ifndef BUSY_RETRIES | ||
32 | # define BUSY_RETRIES 10000 | ||
33 | #endif | ||
34 | |||
35 | // #define DEBUG_EEPROM_OUTPUT | ||
36 | |||
37 | /* | ||
38 | * Debug print utils | ||
39 | */ | ||
40 | #if defined(DEBUG_EEPROM_OUTPUT) | ||
41 | # define eeprom_printf(fmt, ...) xprintf(fmt, ##__VA_ARGS__); | ||
42 | #else /* NO_DEBUG */ | ||
43 | # define eeprom_printf(fmt, ...) | ||
44 | #endif /* NO_DEBUG */ | ||
45 | |||
46 | __attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE] = {0}; | ||
47 | volatile uint8_t * SmartEEPROM8 = (uint8_t *)SEEPROM_ADDR; | ||
48 | |||
49 | static inline bool eeprom_is_busy(void) { | ||
50 | int timeout = BUSY_RETRIES; | ||
51 | while (NVMCTRL->SEESTAT.bit.BUSY && timeout-- > 0) | ||
52 | ; | ||
53 | |||
54 | return NVMCTRL->SEESTAT.bit.BUSY; | ||
55 | } | ||
56 | |||
57 | static uint32_t get_virtual_eeprom_size(void) { | ||
58 | // clang-format off | ||
59 | static const uint32_t VIRTUAL_EEPROM_MAP[11][8] = { | ||
60 | /* 4 8 16 32 64 128 256 512 */ | ||
61 | /* 0*/ { 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
62 | /* 1*/ { 512, 1024, 2048, 4096, 4096, 4096, 4096, 4096 }, | ||
63 | /* 2*/ { 512, 1024, 2048, 4096, 8192, 8192, 8192, 8192 }, | ||
64 | /* 3*/ { 512, 1024, 2048, 4096, 8192, 16384, 16384, 16384 }, | ||
65 | /* 4*/ { 512, 1024, 2048, 4096, 8192, 16384, 16384, 16384 }, | ||
66 | /* 5*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 }, | ||
67 | /* 6*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 }, | ||
68 | /* 7*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 }, | ||
69 | /* 8*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 32768 }, | ||
70 | /* 9*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 }, | ||
71 | /*10*/ { 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 }, | ||
72 | }; | ||
73 | // clang-format on | ||
74 | |||
75 | static uint32_t virtual_eeprom_size = UINT32_MAX; | ||
76 | if (virtual_eeprom_size == UINT32_MAX) { | ||
77 | virtual_eeprom_size = VIRTUAL_EEPROM_MAP[NVMCTRL->SEESTAT.bit.PSZ][NVMCTRL->SEESTAT.bit.SBLK]; | ||
78 | } | ||
79 | // eeprom_printf("get_virtual_eeprom_size:: %d:%d:%d\n", NVMCTRL->SEESTAT.bit.PSZ, NVMCTRL->SEESTAT.bit.SBLK, virtual_eeprom_size); | ||
80 | return virtual_eeprom_size; | ||
81 | } | ||
82 | |||
83 | uint8_t eeprom_read_byte(const uint8_t *addr) { | ||
84 | uintptr_t offset = (uintptr_t)addr; | ||
85 | if (offset >= MAX(EEPROM_SIZE, get_virtual_eeprom_size())) { | ||
86 | eeprom_printf("eeprom_read_byte:: out of bounds\n"); | ||
87 | return 0x0; | ||
88 | } | ||
89 | |||
90 | if (get_virtual_eeprom_size() == 0) { | ||
91 | return buffer[offset]; | ||
92 | } | ||
93 | |||
94 | if (eeprom_is_busy()) { | ||
95 | eeprom_printf("eeprom_write_byte:: timeout\n"); | ||
96 | return 0x0; | ||
97 | } | ||
98 | |||
99 | return SmartEEPROM8[offset]; | ||
100 | } | ||
101 | |||
102 | void eeprom_write_byte(uint8_t *addr, uint8_t value) { | ||
103 | uintptr_t offset = (uintptr_t)addr; | ||
104 | if (offset >= MAX(EEPROM_SIZE, get_virtual_eeprom_size())) { | ||
105 | eeprom_printf("eeprom_write_byte:: out of bounds\n"); | ||
106 | return; | ||
107 | } | ||
108 | |||
109 | if (get_virtual_eeprom_size() == 0) { | ||
110 | buffer[offset] = value; | ||
111 | return; | ||
112 | } | ||
113 | |||
114 | if (eeprom_is_busy()) { | ||
115 | eeprom_printf("eeprom_write_byte:: timeout\n"); | ||
116 | return; | ||
117 | } | ||
118 | |||
119 | SmartEEPROM8[offset] = value; | ||
120 | } | ||
121 | |||
122 | uint16_t eeprom_read_word(const uint16_t *addr) { | ||
123 | const uint8_t *p = (const uint8_t *)addr; | ||
124 | return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8); | ||
125 | } | ||
126 | |||
127 | uint32_t eeprom_read_dword(const uint32_t *addr) { | ||
128 | const uint8_t *p = (const uint8_t *)addr; | ||
129 | return eeprom_read_byte(p) | (eeprom_read_byte(p + 1) << 8) | (eeprom_read_byte(p + 2) << 16) | (eeprom_read_byte(p + 3) << 24); | ||
130 | } | ||
131 | |||
132 | void eeprom_read_block(void *buf, const void *addr, size_t len) { | ||
133 | const uint8_t *p = (const uint8_t *)addr; | ||
134 | uint8_t * dest = (uint8_t *)buf; | ||
135 | while (len--) { | ||
136 | *dest++ = eeprom_read_byte(p++); | ||
137 | } | ||
138 | } | ||
139 | |||
140 | void eeprom_write_word(uint16_t *addr, uint16_t value) { | ||
141 | uint8_t *p = (uint8_t *)addr; | ||
142 | eeprom_write_byte(p++, value); | ||
143 | eeprom_write_byte(p, value >> 8); | ||
144 | } | ||
145 | |||
146 | void eeprom_write_dword(uint32_t *addr, uint32_t value) { | ||
147 | uint8_t *p = (uint8_t *)addr; | ||
148 | eeprom_write_byte(p++, value); | ||
149 | eeprom_write_byte(p++, value >> 8); | ||
150 | eeprom_write_byte(p++, value >> 16); | ||
151 | eeprom_write_byte(p, value >> 24); | ||
152 | } | ||
153 | |||
154 | void eeprom_write_block(const void *buf, void *addr, size_t len) { | ||
155 | uint8_t * p = (uint8_t *)addr; | ||
156 | const uint8_t *src = (const uint8_t *)buf; | ||
157 | while (len--) { | ||
158 | eeprom_write_byte(p++, *src++); | ||
159 | } | ||
160 | } | ||
161 | |||
162 | void eeprom_update_byte(uint8_t *addr, uint8_t value) { eeprom_write_byte(addr, value); } | ||
163 | |||
164 | void eeprom_update_word(uint16_t *addr, uint16_t value) { | ||
165 | uint8_t *p = (uint8_t *)addr; | ||
166 | eeprom_write_byte(p++, value); | ||
167 | eeprom_write_byte(p, value >> 8); | ||
168 | } | ||
169 | |||
170 | void eeprom_update_dword(uint32_t *addr, uint32_t value) { | ||
171 | uint8_t *p = (uint8_t *)addr; | ||
172 | eeprom_write_byte(p++, value); | ||
173 | eeprom_write_byte(p++, value >> 8); | ||
174 | eeprom_write_byte(p++, value >> 16); | ||
175 | eeprom_write_byte(p, value >> 24); | ||
176 | } | ||
177 | |||
178 | void eeprom_update_block(const void *buf, void *addr, size_t len) { | ||
179 | uint8_t * p = (uint8_t *)addr; | ||
180 | const uint8_t *src = (const uint8_t *)buf; | ||
181 | while (len--) { | ||
182 | eeprom_write_byte(p++, *src++); | ||
183 | } | ||
184 | } | ||