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 | --
-- Copyright (C) 2022 Fabien Chouteau <fabien.chouteau@gmail.com>
--
-- SPDX-License-Identifier: BSD-3-Clause
--
with System.Storage_Elements; use System.Storage_Elements;
with System.Machine_Code; use System.Machine_Code;
with Atomic.Critical_Section;
with RP2040_SVD.XIP_SSI;
with RP2040_SVD.IO_QSPI;
with Interfaces.C;
with RP.ROM;
package body RP.Flash is
function In_Flash
(Addr : System.Address)
return Boolean
is
begin
return To_Integer (Addr) in XIP_BASE .. (XIP_BASE + Flash_Size);
end In_Flash;
function To_Flash_Offset
(Addr : System.Address)
return Flash_Offset
is (Flash_Offset (To_Integer (Addr) - XIP_BASE));
function To_Address
(Offset : Flash_Offset)
return System.Address
is (To_Address (Integer_Address (Offset) + XIP_BASE));
procedure Flash_Init_Boot2_Copyout
with No_Inline, Linker_Section => ".time_critical";
-- If not done already, copy the code from bootrom into a RAM buffer
procedure Flash_Enable_XIP_Via_Boot2
with No_Inline, Linker_Section => ".time_critical";
-- Execute the copy of bootrom procedure in RAM
procedure Flash_CS_Force
(High : Boolean)
with No_Inline, Linker_Section => ".time_critical";
procedure Flash_Do_Cmd
(Tx : UInt8_Array;
Rx : out UInt8_Array)
with No_Inline, Linker_Section => ".time_critical";
BOOT_SIZE_WORDS : constant := 64;
Boot2_Copyout : HAL.UInt32_Array (1 .. BOOT_SIZE_WORDS);
Boot2_Copyout_Valid : Boolean := False;
------------------------------
-- Flash_Init_Boot2_Copyout --
------------------------------
procedure Flash_Init_Boot2_Copyout is
Boot2 : HAL.UInt32_Array (Boot2_Copyout'Range)
with Address => System'To_Address (XIP_BASE);
begin
if not Boot2_Copyout_Valid then
Boot2_Copyout := Boot2;
-- __compiler_memory_barrier();
Asm ("", Clobber => "memory", Volatile => True);
Boot2_Copyout_Valid := True;
end if;
end Flash_Init_Boot2_Copyout;
--------------------------------
-- Flash_Enable_XIP_Via_Boot2 --
--------------------------------
procedure Flash_Enable_XIP_Via_Boot2 is
procedure Boot2_Call
with Import,
Convention => C,
Address => To_Address (To_Integer (Boot2_Copyout'Address) + 1);
-- Addr + 1 for ARM thumb mode call
begin
Boot2_Call;
end Flash_Enable_XIP_Via_Boot2;
-----------
-- Erase --
-----------
procedure Erase
(Offset : Flash_Offset;
Length : Natural)
is
State : Atomic.Critical_Section.Interrupt_State;
begin
Flash_Init_Boot2_Copyout;
-- No flash accesses after this point
Atomic.Critical_Section.Enter (State);
-- __compiler_memory_barrier();
Asm ("", Clobber => "memory", Volatile => True);
RP.ROM.connect_internal_flash;
RP.ROM.flash_exit_xip;
RP.ROM.flash_range_erase
(Addr => Interfaces.Unsigned_32 (Offset),
Count => Interfaces.C.size_t (Length),
Block_Size => Interfaces.Unsigned_32 (Block_Size),
Block_Cmd => Block_Erase_Command);
RP.ROM.flash_flush_cache;
Flash_Enable_XIP_Via_Boot2;
Atomic.Critical_Section.Leave (State);
end Erase;
-------------
-- Program --
-------------
procedure Program
(Offset : Flash_Offset;
Source : System.Address;
Length : Natural)
is
State : Atomic.Critical_Section.Interrupt_State;
begin
Flash_Init_Boot2_Copyout;
-- No flash accesses after this point
Atomic.Critical_Section.Enter (State);
-- __compiler_memory_barrier();
Asm ("", Clobber => "memory", Volatile => True);
RP.ROM.connect_internal_flash;
RP.ROM.flash_exit_xip;
RP.ROM.flash_range_program (Addr => Interfaces.Unsigned_32 (Offset),
Data => Source,
Count => Interfaces.C.size_t (Length));
RP.ROM.flash_flush_cache;
Flash_Enable_XIP_Via_Boot2;
Atomic.Critical_Section.Leave (State);
end Program;
procedure Flash_CS_Force
(High : Boolean)
is
begin
RP2040_SVD.IO_QSPI.IO_QSPI_Periph.GPIO_QSPI_SS_CTRL.OUTOVER :=
(if High then RP2040_SVD.IO_QSPI.HIGH else RP2040_SVD.IO_QSPI.LOW);
end Flash_CS_Force;
procedure Flash_Do_Cmd
(Tx : UInt8_Array;
Rx : out UInt8_Array)
is
use RP2040_SVD.XIP_SSI;
State : Atomic.Critical_Section.Interrupt_State;
Flags : SR_Register;
Max_In_Flight : constant := 16 - 2;
Tx_Remaining : Natural := Tx'Length;
Rx_Remaining : Natural := Rx'Length;
Tx_Index : Integer := Tx'First;
Rx_Index : Integer := Rx'First;
begin
Flash_Init_Boot2_Copyout;
-- No flash accesses after this point
Atomic.Critical_Section.Enter (State);
-- __compiler_memory_barrier();
Asm ("", Clobber => "memory", Volatile => True);
RP.ROM.connect_internal_flash;
RP.ROM.flash_exit_xip;
Flash_CS_Force (False);
while Tx_Remaining > 0 or Rx_Remaining > 0 loop
Flags := XIP_SSI_Periph.SR;
if Flags.TFNF and Tx_Remaining > 0 and (Rx_Remaining - Tx_Remaining) < Max_In_Flight then
XIP_SSI_Periph.DR0 := UInt32 (Tx (Tx_Index));
Tx_Index := Tx_Index + 1;
Tx_Remaining := Tx_Remaining - 1;
end if;
if Flags.RFNE and Rx_Remaining > 0 then
Rx (Rx_Index) := UInt8 (XIP_SSI_Periph.DR0);
Rx_Index := Rx_Index + 1;
Rx_Remaining := Rx_Remaining - 1;
end if;
end loop;
Flash_CS_Force (True);
RP.ROM.flash_flush_cache;
Flash_Enable_XIP_Via_Boot2;
Atomic.Critical_Section.Leave (State);
end Flash_Do_Cmd;
function Unique_Id
return UInt64
is
FLASH_RUID_CMD : constant := 16#4B#;
Dummy_Length : constant := 4;
Data_Length : constant := 8;
Length : constant := 1 + Dummy_Length + Data_Length;
Tx, Rx : UInt8_Array (1 .. Length);
Id : UInt64 := 0;
begin
Tx (1) := FLASH_RUID_CMD;
Flash_Do_Cmd (Tx, Rx);
for I in Rx'Last - 7 .. Rx'Last loop
Id := Shift_Left (Id, 8) or UInt64 (Rx (I));
end loop;
return Id;
end Unique_Id;
end RP.Flash;
|