From b8af853b4185c198af3e23920d59578930a94851 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sat, 12 Oct 2024 12:00:55 -0700 Subject: [PATCH 1/2] Preserve PSRAM QMI interface around flash ops The flash ROM routines seem to overwrite the QMI configuration we set for PSRAM, rendering it unreadable after any erase or write or ID command. Wrap the 4 flash control functions to preserve the QMI state on the RP2350. On the RP2040, simply pass through the call. Fixes #2537 --- cores/rp2040/flash_wrapper.cpp | 73 ++++++++++++++++++++++++++++++++++ lib/core_wrap.txt | 5 +++ 2 files changed, 78 insertions(+) create mode 100644 cores/rp2040/flash_wrapper.cpp diff --git a/cores/rp2040/flash_wrapper.cpp b/cores/rp2040/flash_wrapper.cpp new file mode 100644 index 000000000..8388077af --- /dev/null +++ b/cores/rp2040/flash_wrapper.cpp @@ -0,0 +1,73 @@ +/* + Flash wrappers to protect PSRAM access on the RP2350 + + Copyright (c) 2024 Earle F. Philhower, III + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifdef PICO_RP2350 +#include +#endif + +extern "C" { + extern void __real_flash_range_erase(uint32_t flash_offs, size_t count); + void __wrap_flash_range_erase(uint32_t flash_offs, size_t count) { +#ifdef PICO_RP2350 + auto s = qmi_hw->m[1]; +#endif + __real_flash_range_erase(flash_offs, count); +#ifdef PICO_RP2350 + qmi_hw->m[1] = s; + __compiler_memory_barrier(); +#endif + } + + extern void __real_flash_range_program(uint32_t flash_offs, const uint8_t *data, size_t count); + void __wrap_flash_range_program(uint32_t flash_offs, const uint8_t *data, size_t count) { +#ifdef PICO_RP2350 + auto s = qmi_hw->m[1]; +#endif + __real_flash_range_program(flash_offs, data, count); +#ifdef PICO_RP2350 + qmi_hw->m[1] = s; + __compiler_memory_barrier(); +#endif + } + + extern void __real_flash_get_unique_id(uint8_t *id_out); + void __wrap_flash_get_unique_id(uint8_t *id_out) { +#ifdef PICO_RP2350 + auto s = qmi_hw->m[1]; +#endif + __real_flash_get_unique_id(id_out); +#ifdef PICO_RP2350 + qmi_hw->m[1] = s; + __compiler_memory_barrier(); +#endif + } + + extern void __real_flash_do_cmd(const uint8_t *txbuf, uint8_t *rxbuf, size_t count); + void __wrap_flash_do_cmd(const uint8_t *txbuf, uint8_t *rxbuf, size_t count) { +#ifdef PICO_RP2350 + auto s = qmi_hw->m[1]; +#endif + __real_flash_do_cmd(txbuf, rxbuf, count); +#ifdef PICO_RP2350 + qmi_hw->m[1] = s; + __compiler_memory_barrier(); +#endif + } +}; diff --git a/lib/core_wrap.txt b/lib/core_wrap.txt index 0dd4570d9..7532fa301 100644 --- a/lib/core_wrap.txt +++ b/lib/core_wrap.txt @@ -67,3 +67,8 @@ -Wl,--wrap=cyw43_tcpip_link_status -Wl,--wrap=cyw43_cb_tcpip_init -Wl,--wrap=cyw43_cb_tcpip_deinit + +-Wl,--wrap=flash_range_erase +-Wl,--wrap=flash_range_program +-Wl,--wrap=flash_get_unique_id +-Wl,--wrap=flash_do_cmd From e44b9d285080aaaa96cad9188af86627e26dca9f Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sat, 12 Oct 2024 12:16:08 -0700 Subject: [PATCH 2/2] Include needed definitions on RP2040 --- cores/rp2040/flash_wrapper.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cores/rp2040/flash_wrapper.cpp b/cores/rp2040/flash_wrapper.cpp index 8388077af..f447c60b1 100644 --- a/cores/rp2040/flash_wrapper.cpp +++ b/cores/rp2040/flash_wrapper.cpp @@ -18,6 +18,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include + #ifdef PICO_RP2350 #include #endif