-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathflc_me30.c
292 lines (241 loc) · 8.53 KB
/
flc_me30.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/******************************************************************************
*
* Copyright (C) 2024 Analog Devices, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/* **** Includes **** */
#include <string.h>
#include "mxc_device.h"
#include "mxc_assert.h"
#include "mxc_sys.h"
#include "flc.h"
#include "flc_reva.h"
#include "flc_common.h"
#include "mcr_regs.h" // For ECCEN registers.
//******************************************************************************
void MXC_FLC_ME30_Flash_Operation(void)
{
/*
This function should be called after modifying the contents of flash memory.
It flushes the instruction caches and line fill buffer.
It should be called _afterwards_ because after flash is modified the cache
may contain instructions that may no longer be valid. _Before_ the
flash modifications the ICC may contain relevant cached instructions related to
the incoming flash instructions (especially relevant in the case of external memory),
and these instructions will be valid up until the point that the modifications are made.
The line fill buffer is a FLC-related buffer that also may no longer be valid.
It's flushed by reading 2 pages of flash.
*/
/* Flush all instruction caches */
MXC_GCR->sysctrl |= MXC_F_GCR_SYSCTRL_ICC_FLUSH;
/* Wait for flush to complete */
while (MXC_GCR->sysctrl & MXC_F_GCR_SYSCTRL_ICC_FLUSH) {}
// Clear the line fill buffer by reading 2 pages from flash
volatile uint32_t *line_addr;
volatile uint32_t __unused line; // __unused attribute removes warning
line_addr = (uint32_t *)(MXC_FLASH_MEM_BASE);
line = *line_addr;
line_addr = (uint32_t *)(MXC_FLASH_MEM_BASE + MXC_FLASH_PAGE_SIZE);
line = *line_addr;
}
//******************************************************************************
int MXC_FLC_ME30_GetPhysicalAddress(uint32_t addr, uint32_t *result)
{
if ((addr >= MXC_FLASH_MEM_BASE) && (addr < (MXC_FLASH_MEM_BASE + MXC_FLASH_MEM_SIZE))) {
*result = addr & (MXC_FLASH_MEM_SIZE - 1);
#if CONFIG_TRUSTED_EXECUTION_SECURE
} else if ((addr >= MXC_INFO_MEM_BASE) && (addr < (MXC_INFO_MEM_BASE + MXC_INFO_MEM_SIZE))) {
*result = (addr & (MXC_INFO_MEM_SIZE - 1)) + MXC_FLASH_MEM_SIZE;
#endif
} else {
return E_BAD_PARAM;
}
return E_NO_ERROR;
}
//******************************************************************************
int MXC_FLC_Init(void)
{
return E_NO_ERROR;
}
//******************************************************************************
#if IAR_PRAGMAS
#pragma section = ".flashprog"
#else
__attribute__((section(".flashprog")))
#endif
int MXC_FLC_Busy(void)
{
return MXC_FLC_RevA_Busy();
}
//******************************************************************************
#if IAR_PRAGMAS
#pragma section = ".flashprog"
#else
__attribute__((section(".flashprog")))
#endif
int MXC_FLC_PageErase(uint32_t address)
{
int err;
uint32_t addr;
if ((err = MXC_FLC_ME30_GetPhysicalAddress(address, &addr)) < E_NO_ERROR) {
return err;
}
err = MXC_FLC_RevA_PageErase((mxc_flc_reva_regs_t *)MXC_FLC, addr);
// Flush the cache
MXC_FLC_ME30_Flash_Operation();
return err;
}
//******************************************************************************
#if IAR_PRAGMAS
#pragma section = ".flashprog"
#else
__attribute__((section(".flashprog")))
#endif
// make sure to disable ICC with ICC_Disable(); before Running this function
int MXC_FLC_Write128(uint32_t address, uint32_t *data)
{
int err;
uint32_t addr;
// Address checked if it is 128-bit aligned
if (address & 0xF) {
return E_BAD_PARAM;
}
if ((err = MXC_FLC_ME30_GetPhysicalAddress(address, &addr)) < E_NO_ERROR) {
return err;
}
err = MXC_FLC_RevA_Write128((mxc_flc_reva_regs_t *)MXC_FLC, addr, data);
// Flush the cache
MXC_FLC_ME30_Flash_Operation();
return err;
}
//******************************************************************************
int MXC_FLC_Write32(uint32_t address, uint32_t data)
{
uint32_t addr, aligned;
int err;
// Address checked if it is byte addressable
if (address & 0x3) {
return E_BAD_PARAM;
}
// Align address to 128-bit word
aligned = address & 0xfffffff0;
if ((err = MXC_FLC_ME30_GetPhysicalAddress(aligned, &addr)) < E_NO_ERROR) {
return err;
}
err = MXC_FLC_RevA_Write32Using128((mxc_flc_reva_regs_t *)MXC_FLC, address, data, addr);
// Flush the cache
MXC_FLC_ME30_Flash_Operation();
return err;
}
//******************************************************************************
int MXC_FLC_MassErase(void)
{
int err;
err = MXC_FLC_RevA_MassErase((mxc_flc_reva_regs_t *)MXC_FLC);
// Flush the cache
MXC_FLC_ME30_Flash_Operation();
return err;
}
//******************************************************************************
int MXC_FLC_UnlockInfoBlock(uint32_t address)
{
/* Flash Controller only accessible in secure world. */
#if defined(CONFIG_TRUSTED_EXECUTION_SECURE) || (CONFIG_TRUSTED_EXECUTION_SECURE != 0)
if ((address < MXC_INFO_MEM_BASE) ||
(address >= (MXC_INFO_MEM_BASE + (MXC_INFO_MEM_SIZE * 2)))) {
return E_BAD_PARAM;
}
#else
return E_NOT_SUPPORTED;
#endif
/* Make sure the info block is locked */
MXC_FLC->actrl = 0x1234;
/* Write the unlock sequence */
MXC_FLC->actrl = 0x55bcbe69;
MXC_FLC->actrl = 0x7688c189;
MXC_FLC->actrl = 0x82306612;
return E_NO_ERROR;
}
//******************************************************************************
int MXC_FLC_LockInfoBlock(uint32_t address)
{
/* Flash Controller only accessible in secure world. */
#if defined(CONFIG_TRUSTED_EXECUTION_SECURE) || (CONFIG_TRUSTED_EXECUTION_SECURE != 0)
if ((address < MXC_INFO_MEM_BASE) ||
(address >= (MXC_INFO_MEM_BASE + (MXC_INFO_MEM_SIZE * 2)))) {
return E_BAD_PARAM;
}
#else
return E_NOT_SUPPORTED;
#endif
MXC_FLC->actrl = 0xDEADBEEF;
return E_NO_ERROR;
}
//******************************************************************************
int MXC_FLC_Write(uint32_t address, uint32_t length, uint32_t *buffer)
{
return MXC_FLC_Com_Write(address, length, buffer);
}
//******************************************************************************
void MXC_FLC_Read(int address, void *buffer, int len)
{
MXC_FLC_Com_Read(address, buffer, len);
}
//******************************************************************************
int MXC_FLC_EnableInt(uint32_t flags)
{
return MXC_FLC_RevA_EnableInt(flags);
}
//******************************************************************************
int MXC_FLC_DisableInt(uint32_t flags)
{
return MXC_FLC_RevA_DisableInt(flags);
}
//******************************************************************************
int MXC_FLC_GetFlags(void)
{
return MXC_FLC_RevA_GetFlags();
}
//******************************************************************************
int MXC_FLC_ClearFlags(uint32_t flags)
{
return MXC_FLC_RevA_ClearFlags(flags);
}
//******************************************************************************
int MXC_FLC_BlockPageWrite(uint32_t address)
{
if (address < MXC_FLASH_MEM_BASE || address > (MXC_FLASH_MEM_BASE + MXC_FLASH_MEM_SIZE)) {
return E_INVALID;
}
return MXC_FLC_RevA_BlockPageWrite(address, MXC_FLASH_MEM_BASE);
}
//******************************************************************************
int MXC_FLC_BlockPageRead(uint32_t address)
{
if (address < MXC_FLASH_MEM_BASE || address > (MXC_FLASH_MEM_BASE + MXC_FLASH_MEM_SIZE)) {
return E_INVALID;
}
return MXC_FLC_RevA_BlockPageRead(address, MXC_FLASH_MEM_BASE);
}
//******************************************************************************
volatile uint32_t *MXC_FLC_GetWELR(uint32_t address, uint32_t page_num)
{
return NULL;
}
//******************************************************************************
volatile uint32_t *MXC_FLC_GetRLR(uint32_t address, uint32_t page_num)
{
return NULL;
}