1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 #define SWAP(x) \
22     (((x) >> 24) | (((x)&0x00FF0000) >> 8) | (((x)&0x0000FF00) << 8) | (((x)&0x000000FF) << 24))
23 
24 typedef enum {
25     ROM_A1_VERSION = 0x01000000,
26 } enum_rom_version_t;
27 
28 #ifdef __SLA_FWK__
29 #include <stdio.h>
30 #include <stdint.h>
31 
32 extern unsigned int _start_SWAP;
33 extern unsigned int _SLA_Size_SWAP;
34 
35 typedef enum {
36     MagicH = 0x48495357,
37     MagicL = 0x45444744,
38 } enum_magic_t;
39 
40 typedef struct {
41     enum_magic_t MagicHigh; //> SLA Header magic
42     enum_magic_t MagicLow; //> SLA Header magic
43 } magic_t;
44 
45 typedef struct {
46     magic_t Magic;
47     enum_rom_version_t RomVersion; //> ROM version
48     unsigned int LoadAddr; //> Relocation address.
49     unsigned int SLA_CodeSize; //> SLA code size in bytes
50     unsigned int *JumpAddr; //> Rom code will jump at this address
51     unsigned int ArgSize; //> Size of the Argument
52     unsigned int AppVersionNumber; //> Version of this application
53 } flash_app_header_t;
54 
55 __attribute__((section(".sb_sla_header"))) __attribute__((__used__))
56 const flash_app_header_t sb_header = {
57     .Magic =
58         {
59             .MagicHigh = SWAP(MagicH),
60             .MagicLow  = SWAP(MagicL),
61         },
62 
63 #if defined(MAX32690_A1)
64     .RomVersion = SWAP(ROM_A1_VERSION),
65 #else
66     .RomVersion = SWAP(ROM_A1_VERSION),
67 #endif
68     .LoadAddr         = SWAP(0x10000000),
69     .SLA_CodeSize     = (unsigned int)&_SLA_Size_SWAP, // Trick to get constant defined at link time
70     .JumpAddr         = &_start_SWAP,
71     .ArgSize          = 0,
72     .AppVersionNumber = SWAP(0x01000000), // 0xAABBCCCC for version AA.BB.CCCC
73 };
74 
75 #endif //__SLA_FWK__
76 
77 #ifdef __SCPA_FWK__
78 /** Global declarations */
79 
80 #include <string.h>
81 
82 typedef enum {
83     MagicH = 0xDEADBEEF,
84     MagicL = 0xCAFEFADE,
85 } enum_magic_t;
86 
87 typedef struct {
88     enum_magic_t MagicHigh; //> SLA Header magic
89     enum_magic_t MagicLow; //> SLA Header magic
90 } magic_t;
91 
92 typedef int (*__scpa_write_t)(unsigned int dest, unsigned int length, unsigned char *p_src);
93 typedef int (*__scpa_erase_t)(unsigned int dest, unsigned int length);
94 
95 /** Generic Plugin Operations */
96 typedef struct {
97     __scpa_write_t write; //> Write to memory
98     __scpa_write_t compare; //> Compare memory data
99     __scpa_erase_t erase; //> Erase memory
100 } scpa_ops_t;
101 
102 typedef struct {
103     magic_t Magic;
104     enum_rom_version_t RomVersion; //> ROM version
105     unsigned int mem_base_addr; //> Base address of memory targetted by applet
106     unsigned int mem_size; //> Size of this memory
107     scpa_ops_t ops; //> Operations of the SCP Applet
108 } scpa_header_t;
109 
110 int start_scpa_write(unsigned int dest, unsigned int length, unsigned char *p_src);
111 int start_scpa_compare(unsigned int dest, unsigned int length, unsigned char *p_src);
112 int start_scpa_erase(unsigned int dest, unsigned int length);
113 
114 int __attribute__((weak)) scpa_write(unsigned int dest, unsigned int length, unsigned char *p_src);
115 int __attribute__((weak))
116 scpa_compare(unsigned int dest, unsigned int length, unsigned char *p_src);
117 int __attribute__((weak)) scpa_erase(unsigned int dest, unsigned int length);
118 
119 extern unsigned int __bss_start__;
120 extern unsigned int __bss_end__;
121 extern unsigned int __bss_magic__;
122 
123 #ifndef SCPA_MEM_BASE_ADDR
124 #define SCPA_MEM_BASE_ADDR 0xC0000000
125 #warning 'SCPA_MEM_BASE_ADDR not defined using default value 0xC0000000'
126 #endif
127 
128 #ifndef SCPA_MEM_SIZE
129 #define SCPA_MEM_SIZE 1024
130 #warning 'SCPA_MEM_SIZE not defined using default value 1024'
131 #endif
132 
133 unsigned int __attribute__((section(".scpa_init"))) Magic_bss = 0xABADCAFE;
134 
135 __attribute__((section(".scpa_header"))) __attribute__((__used__))
136 const scpa_header_t scpa_header = {
137     .Magic =
138         {
139             .MagicHigh = MagicH,
140             .MagicLow  = MagicL,
141         },
142 #ifdef MAX32690_A1
143     .RomVersion = SWAP(ROM_A1_VERSION),
144 #elif defined(MAX32690_A2)
145     .RomVersion = SWAP(ROM_A2_VERSION),
146 #else
147 #error "Please Select a chip ROM revision"
148 #endif
149     .mem_base_addr = SCPA_MEM_BASE_ADDR,
150     .mem_size      = SCPA_MEM_SIZE,
151     .ops =
152         {
153             .write   = (__scpa_write_t)start_scpa_write,
154             .compare = (__scpa_write_t)start_scpa_compare,
155             .erase   = (__scpa_erase_t)start_scpa_erase,
156         },
157 };
158 
159 int __attribute__((section(".scpa_ops")))
start_scpa_write(unsigned int dest,unsigned int length,unsigned char * p_src)160 start_scpa_write(unsigned int dest, unsigned int length, unsigned char *p_src)
161 {
162     volatile unsigned int bss_size =
163         (volatile unsigned int)&__bss_end__ - (volatile unsigned int)&__bss_start__;
164     volatile unsigned char *p_bss = (volatile unsigned char *)&__bss_start__;
165     volatile unsigned int *p_magic = (volatile unsigned int *)&__bss_magic__;
166 
167     // Automatic Code for bss init
168     if (*p_magic == 0xABADCAFE) {
169         memset((void *)p_bss, 0x00, bss_size);
170         *p_magic = 0x0;
171     }
172     return scpa_write(dest, length, p_src);
173 }
174 
175 int __attribute__((section(".scpa_ops")))
start_scpa_compare(unsigned int dest,unsigned int length,unsigned char * p_src)176 start_scpa_compare(unsigned int dest, unsigned int length, unsigned char *p_src)
177 {
178     volatile unsigned int bss_size =
179         (volatile unsigned int)&__bss_end__ - (volatile unsigned int)&__bss_start__;
180     volatile unsigned char *p_bss = (volatile unsigned char *)&__bss_start__;
181     volatile unsigned int *p_magic = (volatile unsigned int *)&__bss_magic__;
182 
183     // Automatic Code for bss init
184     if (*p_magic == 0xABADCAFE) {
185         memset((void *)p_bss, 0x00, bss_size);
186         *p_magic = 0x0;
187     }
188     return scpa_compare(dest, length, p_src);
189 }
190 
start_scpa_erase(unsigned int dest,unsigned int length)191 int __attribute__((section(".scpa_ops"))) start_scpa_erase(unsigned int dest, unsigned int length)
192 {
193     volatile unsigned int bss_size =
194         (volatile unsigned int)&__bss_end__ - (volatile unsigned int)&__bss_start__;
195     volatile unsigned char *p_bss = (volatile unsigned char *)&__bss_start__;
196     volatile unsigned int *p_magic = (volatile unsigned int *)&__bss_magic__;
197 
198     // Automatic Code for bss init
199     if ((*p_magic == 0xABADCAFE)) {
200         memset((void *)p_bss, 0x00, bss_size);
201         *p_magic = 0x0;
202     }
203     return scpa_erase(dest, length);
204 }
205 
206 int __attribute__((section(".scpa_ops")))
scpa_write(unsigned int dest,unsigned int length,unsigned char * p_src)207 scpa_write(unsigned int dest, unsigned int length, unsigned char *p_src)
208 {
209     (void)dest;
210     (void)length;
211     (void)p_src;
212     return 0;
213 }
214 
215 int __attribute__((section(".scpa_ops")))
scpa_compare(unsigned int dest,unsigned int length,unsigned char * p_src)216 scpa_compare(unsigned int dest, unsigned int length, unsigned char *p_src)
217 {
218     (void)dest;
219     (void)length;
220     (void)p_src;
221     return 0;
222 }
223 
scpa_erase(unsigned int dest,unsigned int length)224 int __attribute__((section(".scpa_ops"))) scpa_erase(unsigned int dest, unsigned int length)
225 {
226     (void)dest;
227     (void)length;
228     return 0;
229 }
230 
Reset_Handler(void)231 void Reset_Handler(void) {}
232 
233 #endif //__SCPA_FWK__
234