1+ /// Based on https://github.com/raspberrypi/pico-sdk/blob/a1438dff1d38bd9c65dbd693f0e5db4b9ae91779/src/rp2_common/hardware_flash/flash.c#L41
2+
13const microzig = @import ("microzig" );
24const peripherals = microzig .chip .peripherals ;
35const IO_QSPI = peripherals .IO_QSPI ;
@@ -23,15 +25,34 @@ pub const XIP_BASE = 0x10000000;
2325pub const BOOTRAM_BASE = 0x400e0000 ;
2426
2527/// Infrastructure for reentering XIP mode after exiting for programming.
28+ ///
2629pub const boot2 = if (! microzig .config .ram_image and compatibility .arch == .arm ) struct {
2730 const BOOT2_SIZE_WORDS = 64 ;
2831
2932 var copyout : [BOOT2_SIZE_WORDS ]u32 = undefined ;
3033 var copyout_valid : bool = false ;
3134
3235 /// Copies the XIP setup function into RAM:
36+ ///
3337 /// - On RP2040 this *is* the second stage bootloader
38+ ///
39+ /// The only job of the second stage bootloader is to configure the SSI and
40+ /// the external flash for the best possible execute-in-place (XIP)
41+ /// performance. Until the SSI is correctly configured for the attached
42+ /// flash device, it's not possible to access flash via the XIP address
43+ /// window, i.e., we have to copy the bootloader into sram before calling
44+ /// `rom.flash_exit_xip`. This is required if we want to erase and/or write
45+ /// to flash.
46+ ///
3447 /// - On RP2350 it is found at BOOTRAM_BASE.
48+ ///
49+ /// ** From RP2350 datasheet section 4.3 **
50+ /// It is physically impossible to execute code from boot RAM, regardless
51+ /// of MPU configuration, as it is on the APB peripheral bus segment, which
52+ /// is not wired to the processor instruction fetch ports.
53+ ///
54+ /// Therefore, we must make a copy of it first.
55+ ///
3556 pub export fn flash_init () linksection (".ram_text" ) void {
3657 if (copyout_valid ) return ;
3758 const bootloader = @as ([* ]u32 , @ptrFromInt (switch (compatibility .chip ) {
0 commit comments