diff --git a/data/nvm.toml b/data/nvm.toml index a2a0c03c221..3a38be56271 160000 --- a/data/nvm.toml +++ b/data/nvm.toml @@ -1 +1 @@ -Subproject commit a2a0c03c221a9bc37992147b5d4ad785a6e467fe +Subproject commit 3a38be562718fce8f545eebaf8c7a6c0f367b345 diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index e622e4b1ea6..bfacd3a6c9c 100755 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -492,6 +492,11 @@ INC += \ CFLAGS += -DPICO_RP2040=0 -DPICO_RP2350=1 +# Make crt0 (the SDK's startup code) run our stage 2 at boot and copy it to boot +# RAM, so the SDK reruns it after each flash write. Otherwise the boot ROM's +# flash read setup is used. +CFLAGS += -DPICO_EMBED_XIP_SETUP=1 + SRC_SDK_CHIP_VARIANT := \ src/common/pico_util/fixed_bitset.c \ src/rp2_common/hardware_powman/powman.c \ @@ -816,9 +821,17 @@ $(BUILD)/%.o: $(BUILD)/%.S $(STEPECHO) "CC $<" $(Q)$(CC) $(CFLAGS) -c -o $@ $< +# RP2350 stage 2 has no checksum, so it is padded without one. The SDK's RP2350 +# pad script also defines __boot2_entry_point, which crt0 uses to run stage 2 +# and copy it to boot RAM. $(BUILD)/boot2_padded_checksummed.S: $(BUILD)/boot2.bin +ifeq ($(CHIP_VARIANT),RP2350) + $(STEPECHO) "PAD $<" + $(Q)$(PYTHON) sdk/src/rp2350/boot_stage2/pad $< $@ +else $(STEPECHO) "PAD_CHECKSUM $<" $(Q)$(PYTHON) sdk/src/rp2040/boot_stage2/pad_checksum -s 0xffffffff $< $@ +endif $(BUILD)/boot2.bin: $(BUILD)/boot2.elf $(STEPECHO) "OBJCOPY $<" @@ -835,6 +848,7 @@ $(HEADER_BUILD)/flash_info.h: flash_info.h.jinja gen_stage2.py $(Q)$(PYTHON) gen_stage2.py $< $@ $(EXTERNAL_FLASH_DEVICES) $(BUILD)/supervisor/internal_flash.o: $(HEADER_BUILD)/flash_info.h +$(BUILD)/common-hal/microcontroller/Processor.o: $(HEADER_BUILD)/flash_info.h $(BUILD)/boot2.elf: $(BUILD)/stage2.c $(STEPECHO) "BOOT $<" diff --git a/ports/raspberrypi/boards/adafruit_floppsy_rp2040/mpconfigboard.mk b/ports/raspberrypi/boards/adafruit_floppsy_rp2040/mpconfigboard.mk index b2264a0e589..2775e10f1ea 100644 --- a/ports/raspberrypi/boards/adafruit_floppsy_rp2040/mpconfigboard.mk +++ b/ports/raspberrypi/boards/adafruit_floppsy_rp2040/mpconfigboard.mk @@ -6,7 +6,7 @@ USB_MANUFACTURER = "Adafruit" CHIP_VARIANT = RP2040 CHIP_FAMILY = rp2 -EXTERNAL_FLASH_DEVICES = "GD25Q64C,W25Q64JVxQ,W25Q128JV" +EXTERNAL_FLASH_DEVICES = "GD25Q64C,W25Q64JVxQ,W25Q128JVxQ" CIRCUITPY_USB_HOST = 0 CIRCUITPY_PICODVI = 0 diff --git a/ports/raspberrypi/boards/adafruit_metro_rp2040/mpconfigboard.mk b/ports/raspberrypi/boards/adafruit_metro_rp2040/mpconfigboard.mk index 8b3ce22c1bc..980ee394611 100644 --- a/ports/raspberrypi/boards/adafruit_metro_rp2040/mpconfigboard.mk +++ b/ports/raspberrypi/boards/adafruit_metro_rp2040/mpconfigboard.mk @@ -6,6 +6,6 @@ USB_MANUFACTURER = "Adafruit" CHIP_VARIANT = RP2040 CHIP_FAMILY = rp2 -EXTERNAL_FLASH_DEVICES = "GD25Q64C,W25Q64JVxQ,W25Q128JV" +EXTERNAL_FLASH_DEVICES = "GD25Q64C,W25Q64JVxQ,W25Q128JVxQ" CIRCUITPY_SDIOIO = 1 diff --git a/ports/raspberrypi/boot_stage2/RP2350.c.jinja b/ports/raspberrypi/boot_stage2/RP2350.c.jinja index 77969f9fcae..56fc7aabb4b 100644 --- a/ports/raspberrypi/boot_stage2/RP2350.c.jinja +++ b/ports/raspberrypi/boot_stage2/RP2350.c.jinja @@ -62,14 +62,16 @@ void __attribute__((section(".entry._stage2_boot"), used)) _stage2_boot(void) { pads_qspi_hw->io[0] = PADS_QSPI_GPIO_QSPI_SCLK_DRIVE_VALUE_8MA << PADS_QSPI_GPIO_QSPI_SCLK_DRIVE_LSB | PADS_QSPI_GPIO_QSPI_SCLK_SLEWFAST_BITS; - // Data lines - uint32_t data_settings = pads_qspi_hw->io[1]; - data_settings &= ~PADS_QSPI_GPIO_QSPI_SCLK_SCHMITT_BITS; - pads_qspi_hw->io[2] = data_settings; + // Data lines: clear only each pad's Schmitt trigger bit, so SD2/SD3 + // (WP#/HOLD#) keep their pull-ups. + // + // io[1] to io[4] are SD0 to SD3. SD1 carries read data in serial mode as + // well, so it is always set up. The others matter only in quad mode. + pads_qspi_hw->io[2] &= ~PADS_QSPI_GPIO_QSPI_SD1_SCHMITT_BITS; {% if quad_ok %} - pads_qspi_hw->io[1] = data_settings; - pads_qspi_hw->io[3] = data_settings; - pads_qspi_hw->io[4] = data_settings; + pads_qspi_hw->io[1] &= ~PADS_QSPI_GPIO_QSPI_SD0_SCHMITT_BITS; + pads_qspi_hw->io[3] &= ~PADS_QSPI_GPIO_QSPI_SD2_SCHMITT_BITS; + pads_qspi_hw->io[4] &= ~PADS_QSPI_GPIO_QSPI_SD3_SCHMITT_BITS; {% endif %} // QMI config @@ -122,10 +124,12 @@ void __attribute__((section(".entry._stage2_boot"), used)) _stage2_boot(void) { // Disable direct mode qmi_hw->direct_csr &= ~QMI_DIRECT_CSR_EN_BITS; + // A slow clock divider and a sample delay (RXDELAY) of 6, safe at any + // system clock. See rp2350_clock_divider in gen_stage2.py. qmi_hw->m[0].timing = 1 << QMI_M0_TIMING_COOLDOWN_LSB | - 1 << QMI_M0_TIMING_RXDELAY_LSB | - {{ clock_divider }} << QMI_M0_TIMING_CLKDIV_LSB; + 6 << QMI_M0_TIMING_RXDELAY_LSB | + {{ rp2350_clock_divider }} << QMI_M0_TIMING_CLKDIV_LSB; qmi_hw->m[0].rcmd = READ_INSTRUCTION << QMI_M0_RCMD_PREFIX_LSB | MODE_CONTINUOUS_READ << QMI_M0_RCMD_SUFFIX_LSB; @@ -134,8 +138,9 @@ void __attribute__((section(".entry._stage2_boot"), used)) _stage2_boot(void) { {{ wait_cycles }} << QMI_M0_RFMT_DUMMY_LEN_LSB; {% if quad_ok %} - // Dummy transfer to get into continuous mode. - (void) *((uint32_t*) XIP_NOCACHE_NOALLOC_BASE); + // Dummy transfer to get into continuous mode. It must be volatile, or the + // compiler drops the read and the flash never enters continuous mode. + (void) *((volatile uint32_t*) XIP_NOCACHE_NOALLOC_BASE); // Set prefix to 0 to skip the command portion. qmi_hw->m[0].rfmt &= ~QMI_M0_RFMT_PREFIX_LEN_BITS; diff --git a/ports/raspberrypi/common-hal/microcontroller/Processor.c b/ports/raspberrypi/common-hal/microcontroller/Processor.c index 2ff7eda1cd9..1cbd12aeb98 100644 --- a/ports/raspberrypi/common-hal/microcontroller/Processor.c +++ b/ports/raspberrypi/common-hal/microcontroller/Processor.c @@ -24,13 +24,19 @@ #include "hardware/vreg.h" #include "hardware/watchdog.h" -#ifdef CIRCUITPY_PSRAM_CHIP_SELECT +#include "genhdr/flash_info.h" + +#if PICO_RP2350 +#include "hardware/regs/io_qspi.h" #include "hardware/regs/qmi.h" +#include "hardware/structs/io_qspi.h" #include "hardware/structs/qmi.h" #endif #if PICO_RP2040 +#include "hardware/regs/ssi.h" #include "hardware/regs/vreg_and_chip_reset.h" +#include "hardware/structs/ssi.h" #include "hardware/structs/vreg_and_chip_reset.h" #endif #if PICO_RP2350 @@ -100,6 +106,137 @@ void __no_inline_not_in_flash_func(mcu_processor_update_psram_timing)(uint32_t s } #endif +#if PICO_RP2350 +// Saved flash timing for the current system clock: the value of the timing +// register for QMI window 0. QMI is the Quad Memory Interface, which reads the +// flash. A flash erase, program or command leaves stage 2's slow timing in +// place, and mcu_processor_restore_flash_timing() writes this value back. +static uint32_t _flash_timing; + +void __no_inline_not_in_flash_func(mcu_processor_restore_flash_timing)(void) { + // QMI has no busy flag. Instead, wait until the flash chip select pad (SS, + // active low) is deasserted, so the timing doesn't change during a read. + while ((io_qspi_hw->io[1].status & IO_QSPI_GPIO_QSPI_SS_STATUS_OUTTOPAD_BITS) == 0) { + } + qmi_hw->m[0].timing = _flash_timing; + // The new timing applies from the next read. Read once through XIP + // (execute in place) so that it is in use before returning. + (void)*(volatile uint32_t *)XIP_NOCACHE_NOALLOC_BASE; +} + +// Maximum delays inside the RP2350 at VDDIO = 3.3 V, from RP2350 datasheet +// Table 1291 (QMI timing delay). +#define QMI_CLOCK_TO_OUTPUT_PS (2500) +#define QMI_INPUT_TO_SAMPLE_PS (1500) +// Allowance for the board trace between the RP2350 and the flash, each way. +#define FLASH_TRACE_DELAY_PS (500) +// Margin required beyond the flash's output hold time. +#define FLASH_HOLD_MARGIN_PS (500) + +void __no_inline_not_in_flash_func(mcu_processor_update_flash_timing)(uint32_t sys_clk_khz) { + // Choose the flash clock divider (CLKDIV) and sample delay (RXDELAY) from + // the flash's datasheet timing in flash_info.h, assuming worst-case delays. + // + // The flash drives each data bit after a falling clock edge. QMI samples + // the bit (CLKDIV + RXDELAY) half system clocks after that edge. + // Setup: the sample must come after the bit is valid. That is the RP2350 + // output delay, plus the flash's output valid time, plus the + // RP2350 input delay, plus the board trace delay both ways. + // Hold: the sample must come before the flash starts driving the next + // bit. That is one SCK period plus the flash's output hold time, + // less FLASH_HOLD_MARGIN_PS. The RP2350's minimum delays aren't + // specified, so they count as 0. + // + // Use the smallest divider that keeps SCK within the flash's 0xeb rating + // and has a sample delay meeting both checks. Of those sample delays, pick + // the one furthest from failing either check. + const int32_t period_ps = 1000000000 / sys_clk_khz; + const int32_t setup_ps = QMI_CLOCK_TO_OUTPUT_PS + FLASH_MAX_OUTPUT_VALID_PS + + QMI_INPUT_TO_SAMPLE_PS + 2 * FLASH_TRACE_DELAY_PS; + uint32_t max_sck_khz = FLASH_EB_MAX_SCK_KHZ; + #ifdef CIRCUITPY_FLASH_MAX_SCK_HZ + max_sck_khz = MIN(max_sck_khz, CIRCUITPY_FLASH_MAX_SCK_HZ / 1000); + #endif + const uint32_t max_rxdelay = QMI_M0_TIMING_RXDELAY_BITS >> QMI_M0_TIMING_RXDELAY_LSB; + + uint32_t clkdiv = (sys_clk_khz + max_sck_khz - 1) / max_sck_khz; + uint32_t rxdelay = max_rxdelay; + for (; clkdiv <= 255; clkdiv++) { + const int32_t hold_ps = (int32_t)clkdiv * period_ps + FLASH_MIN_OUTPUT_HOLD_PS - FLASH_HOLD_MARGIN_PS; + int32_t best_margin_ps = -1; + for (uint32_t r = 0; r <= max_rxdelay; r++) { + const int32_t sample_ps = (int32_t)(clkdiv + r) * period_ps / 2; + const int32_t margin_ps = MIN(sample_ps - setup_ps, hold_ps - sample_ps); + if (margin_ps > best_margin_ps) { + best_margin_ps = margin_ps; + rxdelay = r; + } + } + if (best_margin_ps >= 0) { + break; + } + } + if (clkdiv > 255) { + // No divider up to 255 passes both checks. That can't happen at a real + // clock speed, but fall back to the slowest timing. + clkdiv = 255; + rxdelay = max_rxdelay; + } + + // COOLDOWN = 1 keeps chip select asserted briefly after a read, so the next + // read from the following address doesn't resend the address. Stage 2 + // sets the same value. + _flash_timing = + 1 << QMI_M0_TIMING_COOLDOWN_LSB | + rxdelay << QMI_M0_TIMING_RXDELAY_LSB | + clkdiv << QMI_M0_TIMING_CLKDIV_LSB; + mcu_processor_restore_flash_timing(); +} +#endif + +#if PICO_RP2040 +// Highest RP2040 flash clock. The RP2040 datasheet gives no QSPI pad delays, +// so this can't be worked out like the RP2350 timing. 62.5 MHz (divider 2 at +// 125 MHz) is the pico-sdk default for the Pico. +// +// On a Feather RP2040, a sample delay of 1 read correctly at every divider and +// every system clock from 48 to 250 MHz. At a 75 MHz flash clock (divider 2 at +// 150 MHz), no other sample delay worked, so this limit keeps some margin. +#define RP2040_FLASH_MAX_SCK_KHZ (62500) + +// SSI clock divider for the current system clock. The SSI (synchronous serial +// interface) reads the flash. A flash erase, program or command reruns stage +// 2, which sets divider 4, and mcu_processor_restore_flash_timing() writes +// this value back. Until the first update, it is stage 2's value. +static uint32_t _flash_clock_divider = 4; + +void __no_inline_not_in_flash_func(mcu_processor_restore_flash_timing)(void) { + // The divider can change only while the SSI is disabled, which also stops + // XIP (execute in place). So this runs from RAM. + while (ssi_hw->sr & SSI_SR_BUSY_BITS) { + } + ssi_hw->ssienr = 0; + ssi_hw->baudr = _flash_clock_divider; + // Sample read data one system clock late, as stage 2 does. + ssi_hw->rx_sample_dly = 1; + ssi_hw->ssienr = 1; +} + +void mcu_processor_update_flash_timing(uint32_t sys_clk_khz) { + // Use the smallest divider that keeps the flash clock within the flash's + // 0xeb rating and RP2040_FLASH_MAX_SCK_KHZ. The SSI supports only even + // dividers, 2 or more. + uint32_t max_sck_khz = MIN(FLASH_EB_MAX_SCK_KHZ, RP2040_FLASH_MAX_SCK_KHZ); + #ifdef CIRCUITPY_FLASH_MAX_SCK_HZ + max_sck_khz = MIN(max_sck_khz, CIRCUITPY_FLASH_MAX_SCK_HZ / 1000); + #endif + uint32_t divider = (sys_clk_khz + max_sck_khz - 1) / max_sck_khz; + divider += divider & 1; + _flash_clock_divider = MAX(divider, 2); + mcu_processor_restore_flash_timing(); +} +#endif + void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t frequency) { uint vco, postdiv1, postdiv2; uint32_t freq_khz = frequency / 1000; @@ -119,16 +256,18 @@ void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t // Wait for a stable voltage common_hal_time_delay_ms(10); - #ifdef CIRCUITPY_PSRAM_CHIP_SELECT - // Prevent interrupt handlers from accessing PSRAM until its timing matches - // the new system clock. + // Prevent interrupt handlers from accessing flash or PSRAM until their + // timing matches the new system clock. common_hal_mcu_disable_interrupts(); - #endif + // While the clock changes, use the flash timing for the faster of the old + // and new clocks. That keeps the flash within its timing limits at both. + mcu_processor_update_flash_timing(MAX(freq_khz, clock_get_hz(clk_sys) / 1000)); set_sys_clock_khz(freq_khz, false); #ifdef CIRCUITPY_PSRAM_CHIP_SELECT mcu_processor_update_psram_timing(freq_khz); - common_hal_mcu_enable_interrupts(); #endif + mcu_processor_update_flash_timing(freq_khz); + common_hal_mcu_enable_interrupts(); } void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) { diff --git a/ports/raspberrypi/common-hal/microcontroller/Processor.h b/ports/raspberrypi/common-hal/microcontroller/Processor.h index 9503eaf21f4..c342a0ec3a0 100644 --- a/ports/raspberrypi/common-hal/microcontroller/Processor.h +++ b/ports/raspberrypi/common-hal/microcontroller/Processor.h @@ -20,3 +20,23 @@ typedef struct { #ifdef CIRCUITPY_PSRAM_CHIP_SELECT void mcu_processor_update_psram_timing(uint32_t sys_clk_khz); #endif + +// Set the flash clock divider and sample delay for a system clock of +// sys_clk_khz, and save them for mcu_processor_restore_flash_timing(). Returns +// nothing. +// +// The flash clock is limited by the flash's datasheet timing (flash_info.h, +// generated from data/nvm.toml). A board can lower it further with +// CIRCUITPY_FLASH_MAX_SCK_HZ. On RP2350, the sample delay is also worked out +// from the datasheet timing. On RP2040, the flash clock is also limited to +// 62.5 MHz, and the sample delay is always 1. +// +// Call with interrupts disabled and no flash operation in progress. +void mcu_processor_update_flash_timing(uint32_t sys_clk_khz); + +// Put back the flash timing last set by mcu_processor_update_flash_timing(). +// Call after each flash erase, program or command (such as reading the flash +// ID). Those leave stage 2's slow timing in place. Returns nothing. +// +// Runs from RAM. Call with interrupts disabled. +void mcu_processor_restore_flash_timing(void); diff --git a/ports/raspberrypi/flash_info.h.jinja b/ports/raspberrypi/flash_info.h.jinja index cfeb4840b9e..aa507aad400 100644 --- a/ports/raspberrypi/flash_info.h.jinja +++ b/ports/raspberrypi/flash_info.h.jinja @@ -3,3 +3,9 @@ #pragma once #define FLASH_DEFAULT_POWER_OF_TWO {{ default_power_of_two }} + +// Worst-case read timing across the board's flash devices, from data/nvm.toml. +// Used to choose the flash clock divider, and on RP2350 the sample delay. +#define FLASH_EB_MAX_SCK_KHZ {{ eb_max_khz }} +#define FLASH_MAX_OUTPUT_VALID_PS {{ max_output_valid_ps }} +#define FLASH_MIN_OUTPUT_HOLD_PS {{ min_output_hold_ps }} diff --git a/ports/raspberrypi/gen_stage2.py b/ports/raspberrypi/gen_stage2.py index 005994dc107..c41c7503112 100644 --- a/ports/raspberrypi/gen_stage2.py +++ b/ports/raspberrypi/gen_stage2.py @@ -7,17 +7,23 @@ def main(input_template: pathlib.Path, output_path: pathlib.Path, skus: str = typer.Argument("")): - if "," in skus: - skus = skus.split(",") - else: - skus = [skus] - skus = ['sku="{}"'.format(f.strip()) for f in skus] - flashes = cascadetoml.filter_toml(pathlib.Path("../../data/nvm.toml"), skus) - - if len(skus) == 0: + skus = [sku.strip() for sku in skus.split(",") if sku.strip()] + if not skus: print("Set EXTERNAL_FLASH_DEVICES in mpconfigboard.mk with all possible flash skus") raise typer.Exit(code=1) + flashes = cascadetoml.filter_toml( + pathlib.Path("../../data/nvm.toml"), ['sku="{}"'.format(sku) for sku in skus] + ) + + # Stop if a listed device is not in data/nvm.toml. Otherwise it would be + # left out silently, and its settings and read timing would not be used. + found = {nvm["sku"] for nvm in flashes["nvm"]} + missing = [sku for sku in skus if sku not in found] + if missing: + print("EXTERNAL_FLASH_DEVICES lists devices not in data/nvm.toml:", ", ".join(missing)) + raise typer.Exit(code=1) + def all_have(nvms, key): for nvm in nvms: if not nvm.get(key, False): @@ -41,7 +47,6 @@ def all_match(nvms, key, default=None): quad_enable_bit_mask = all_match(flashes["nvm"], "quad_enable_bit_mask") continuous_status_write = all_have(flashes["nvm"], "01_continuous_status_write") split_status_write = all_have(flashes["nvm"], "write_status_register_split") - e7_quad_word_read = all_have(flashes["nvm"], "e7_quad_word_read") quad_ok = quad_enable_status_byte is not None and quad_enable_bit_mask is not None @@ -64,15 +69,41 @@ def all_match(nvms, key, default=None): clock_divider = 4 - read_command = 0x03 - wait_cycles = 0 - if quad_ok: - if e7_quad_word_read: - read_command = 0xE7 - wait_cycles = 2 - else: - read_command = 0xEB - wait_cycles = 4 + # RP2350 stage 2 runs after every flash erase or program, at whatever the + # system clock is then. In quad mode it makes one read to enter + # continuous-read mode. Its divider and sample delay must be safe at any + # system clock. Divider 8 with a sample delay of 6 (set in the template) + # meets the setup and hold times of flash with an output valid time of 8 ns + # or less, up to 532 MHz. CircuitPython sets the real timing for the system + # clock afterward. + rp2350_clock_divider = 8 + + # Stage 2 always uses 0xeb for quad reads, because the read timing below is + # rated for 0xeb. Datasheets often give no separate rating for 0xe7. + read_command = 0xEB if quad_ok else 0x03 + wait_cycles = 4 if quad_ok else 0 + + # Read timing for choosing the flash clock divider, and on RP2350 the sample delay + # (mcu_processor_update_flash_timing() in common-hal/microcontroller/Processor.c). + # Use the worst case across all the listed devices, because any of them may + # be fitted. A device without a value uses the defaults passed to worst() + # below: 75 MHz, an output valid time of 8 ns and an output hold time of 0. + # + # A device with eb_quad_read_max_clock_speed_mhz = 0 has no 0xeb read, so + # stage 2 uses the plain 0x03 read. All the devices in data/nvm.toml + # support 0x03 at 33 MHz or more. + def worst(key, default, pick): + values = [] + for nvm in flashes["nvm"]: + value = nvm.get(key, default) + if key == "eb_quad_read_max_clock_speed_mhz" and value == 0: + value = 33 + values.append(value) + return pick(values) + + eb_max_khz = int(worst("eb_quad_read_max_clock_speed_mhz", 75, min) * 1000) + max_output_valid_ps = int(worst("max_output_valid_ns", 8, max) * 1000) + min_output_hold_ps = int(worst("min_output_hold_ns", 0, min) * 1000) flash_settings = { "quad_ok": quad_ok, @@ -80,9 +111,13 @@ def all_match(nvms, key, default=None): "quad_enable_bit_mask": quad_enable_bit_mask, "split_status_write": split_status_write, "clock_divider": clock_divider, + "rp2350_clock_divider": rp2350_clock_divider, "read_command": read_command, "wait_cycles": wait_cycles, "default_power_of_two": default_power_of_two, + "eb_max_khz": eb_max_khz, + "max_output_valid_ps": max_output_valid_ps, + "min_output_hold_ps": min_output_hold_ps, } template = Template(input_template.read_text()) diff --git a/ports/raspberrypi/link-rp2350.ld b/ports/raspberrypi/link-rp2350.ld index 4338b4afc02..ec39f08e806 100644 --- a/ports/raspberrypi/link-rp2350.ld +++ b/ports/raspberrypi/link-rp2350.ld @@ -37,11 +37,6 @@ ENTRY(_entry_point) SECTIONS { - /* Second stage bootloader is prepended to the image. It must be 256 bytes big - and checksummed. It is usually built by the boot_stage2 target - in the Pico SDK - */ - .flash_begin : { __flash_binary_start = .; } > FLASH_FIRMWARE @@ -84,6 +79,22 @@ SECTIONS . = ALIGN(4); } > FLASH_FIRMWARE + /* Stage 2 (the flash read setup function), generated from + boot_stage2/RP2350.c.jinja. On RP2350 the boot ROM does not run it. + The Pico SDK's startup code (crt0) runs it at boot and copies it to boot + RAM, and the SDK reruns it after each flash erase or program. It must + fit in boot RAM's 256 bytes. + */ + .boot2 : { + . = ALIGN(4); + __boot2_start__ = .; + *(.boot2) + __boot2_end__ = .; + } > FLASH_FIRMWARE + + ASSERT(__boot2_end__ - __boot2_start__ <= 256, + "ERROR: RP2350 stage 2 must be no more than 256 bytes in size") + .rodata : { *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .rodata*) . = ALIGN(4); diff --git a/ports/raspberrypi/mpconfigport.h b/ports/raspberrypi/mpconfigport.h index a99050f8892..82c83ce63a4 100644 --- a/ports/raspberrypi/mpconfigport.h +++ b/ports/raspberrypi/mpconfigport.h @@ -66,6 +66,12 @@ #define CIRCUITPY_I2C_ALLOW_INTERNAL_PULL_UP (0) #endif +// CIRCUITPY_FLASH_MAX_SCK_HZ is not defined by default. The flash clock is +// chosen from the flash's datasheet timing in flash_info.h, which is +// generated from data/nvm.toml. A board whose flash or wiring needs a slower +// flash clock can define CIRCUITPY_FLASH_MAX_SCK_HZ in mpconfigboard.h as a +// limit, in Hz. + #if CIRCUITPY_CYW43 #define MICROPY_PY_LWIP_ENTER cyw43_arch_lwip_begin(); #define MICROPY_PY_LWIP_REENTER MICROPY_PY_LWIP_ENTER diff --git a/ports/raspberrypi/supervisor/internal_flash.c b/ports/raspberrypi/supervisor/internal_flash.c index e9da8a39a14..b189422cabd 100644 --- a/ports/raspberrypi/supervisor/internal_flash.c +++ b/ports/raspberrypi/supervisor/internal_flash.c @@ -20,6 +20,7 @@ #include "shared-bindings/microcontroller/__init__.h" #include "audio_dma.h" +#include "common-hal/microcontroller/Processor.h" #include "supervisor/flash.h" #include "supervisor/usb.h" @@ -54,6 +55,9 @@ void supervisor_flash_pre_write(void) { } void supervisor_flash_post_write(void) { + // The flash erase, program or command left stage 2's slow timing in place. + // Restore the timing for the current system clock. + mcu_processor_restore_flash_timing(); #if CIRCUITPY_AUDIOCORE // Unpause audio DMA. audio_dma_unpause_mask(_audio_channel_mask); diff --git a/ports/raspberrypi/supervisor/port.c b/ports/raspberrypi/supervisor/port.c index 46253e9943f..5cceeafc736 100644 --- a/ports/raspberrypi/supervisor/port.c +++ b/ports/raspberrypi/supervisor/port.c @@ -385,6 +385,14 @@ size_t port_heap_get_largest_free_size(void) { safe_mode_t port_init(void) { _binary_info(); + + // Stage 2 left slow flash timing that is safe at any system clock. Switch + // to the fastest flash timing allowed at this clock. Interrupts are off so + // that no interrupt handler reads flash during the change. + common_hal_mcu_disable_interrupts(); + mcu_processor_update_flash_timing(clock_get_hz(clk_sys) / 1000); + common_hal_mcu_enable_interrupts(); + // Set brown out. // Load from the XIP memory space that doesn't cache. That way we don't diff --git a/shared-bindings/microcontroller/Processor.c b/shared-bindings/microcontroller/Processor.c index d8b8424f793..9a6e1938e80 100644 --- a/shared-bindings/microcontroller/Processor.c +++ b/shared-bindings/microcontroller/Processor.c @@ -78,6 +78,10 @@ MP_PROPERTY_GETTER(mcu_processor_architecture_obj, //| the ``frequency`` is possible on RP2040 and RP2350 boards, some ESP32 boards and some //| i.MX boards. //| +//| .. note:: On RP2040, the flash clock is the CPU frequency divided by an +//| even number, and is at most 62.5 MHz. So some frequencies just above +//| 125 MHz, such as 150 MHz, run Python code more slowly than 125 MHz. +//| //| .. warning:: Overclocking likely voids your warranties and may reduce //| the lifetime of the chip. //|