Browse Source

RP2040: Use actual flash chip size for ROM drive, set firmware reserved size to 384 kB

Petteri Aimonen 3 years ago
parent
commit
a09c2c6559

+ 23 - 8
lib/ZuluSCSI_platform_RP2040/ZuluSCSI_platform.cpp

@@ -15,9 +15,11 @@ extern "C" {
 // As of 2022-09-13, the platformio RP2040 core is missing cplusplus guard on flash.h
 // For that reason this has to be inside the extern "C" here.
 #include <hardware/flash.h>
+#include "rp2040_flash_do_cmd.h"
 
 const char *g_azplatform_name = PLATFORM_NAME;
 static bool g_scsi_initiator = false;
+static uint32_t g_flash_chip_size = 0;
 
 void mbed_error_hook(const mbed_error_ctx * error_context);
 
@@ -78,6 +80,13 @@ void azplatform_init()
         azlog("NOTE: SCSI termination is disabled");
     }
 
+    // Get flash chip size
+    uint8_t cmd_read_jedec_id[4] = {0x9f, 0, 0, 0};
+    uint8_t response_jedec[4] = {0};
+    flash_do_cmd(cmd_read_jedec_id, response_jedec, 4);
+    g_flash_chip_size = (1 << response_jedec[3]);
+    azlog("Flash chip size: ", (int)(g_flash_chip_size / 1024), " kB");
+
     // SD card pins
     // Card is used in SDIO mode for main program, and in SPI mode for crash handler & bootloader.
     //        pin             function       pup   pdown  out    state fast
@@ -463,15 +472,20 @@ const void * btldr_vectors[2] = {&__StackTop, (void*)&btldr_reset_handler};
 
 #ifdef PLATFORM_HAS_ROM_DRIVE
 
-// Reserve up to 512 kB for firmware.
-#define ROMDRIVE_OFFSET (512 * 1024)
-
-// TODO: Actually read the flash chip size instead of assuming 2 MB.
-static uint32_t g_romdrive_max_size = 2048 * 1024 - ROMDRIVE_OFFSET;
+// Reserve up to 384 kB for firmware.
+#define ROMDRIVE_OFFSET (384 * 1024)
 
 uint32_t azplatform_get_romdrive_maxsize()
 {
-    return g_romdrive_max_size;
+    if (g_flash_chip_size >= ROMDRIVE_OFFSET)
+    {
+        return g_flash_chip_size - ROMDRIVE_OFFSET;
+    }
+    else
+    {
+        // Failed to read flash chip size, default to 2 MB
+        return 2048 * 1024 - ROMDRIVE_OFFSET;
+    }
 }
 
 bool azplatform_read_romdrive(uint8_t *dest, uint32_t start, uint32_t count)
@@ -487,7 +501,7 @@ bool azplatform_read_romdrive(uint8_t *dest, uint32_t start, uint32_t count)
     xip_ctrl_hw->stream_ctr = count / 4;
 
     // Transfer happens in multiples of 4 bytes
-    assert(start < g_romdrive_max_size);
+    assert(start < azplatform_get_romdrive_maxsize());
     assert((count & 3) == 0);
     assert((((uint32_t)dest) & 3) == 0);
 
@@ -507,8 +521,9 @@ bool azplatform_read_romdrive(uint8_t *dest, uint32_t start, uint32_t count)
 
 bool azplatform_write_romdrive(const uint8_t *data, uint32_t start, uint32_t count)
 {
-    assert(start < g_romdrive_max_size);
+    assert(start < azplatform_get_romdrive_maxsize());
     assert((count % AZPLATFORM_ROMDRIVE_PAGE_SIZE) == 0);
+
     __disable_irq();
     flash_range_erase(start + ROMDRIVE_OFFSET, count);
     flash_range_program(start + ROMDRIVE_OFFSET, data, count);

+ 1 - 1
lib/ZuluSCSI_platform_RP2040/rp2040.ld

@@ -1,6 +1,6 @@
 MEMORY
 {
-    FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 512k
+    FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 384k
     RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 240k  /* Leave space for pico-debug */
     SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k
     SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k

+ 78 - 0
lib/ZuluSCSI_platform_RP2040/rp2040_flash_do_cmd.h

@@ -0,0 +1,78 @@
+// As of 2022-11-16, the raspberrypi platformio package ships with old version
+// of pico-sdk. This version lacks the flash_do_cmd() function in flash.h header.
+// This file backports the function from new versions.
+
+#pragma once
+
+#ifndef RP2040_HAS_FLASH_DO_CMD
+
+#include <hardware/flash.h>
+#include "pico/bootrom.h"
+#include "hardware/structs/ssi.h"
+#include "hardware/structs/ioqspi.h"
+
+#define BOOT2_SIZE_WORDS 64
+
+static uint32_t boot2_copyout[BOOT2_SIZE_WORDS];
+static bool boot2_copyout_valid = false;
+
+static void __no_inline_not_in_flash_func(flash_init_boot2_copyout)(void) {
+    if (boot2_copyout_valid)
+        return;
+    for (int i = 0; i < BOOT2_SIZE_WORDS; ++i)
+        boot2_copyout[i] = ((uint32_t *)XIP_BASE)[i];
+    __asm__ volatile ("" : : : "memory");
+    boot2_copyout_valid = true;
+}
+
+static void __no_inline_not_in_flash_func(flash_enable_xip_via_boot2)(void) {
+    ((void (*)(void))((char*)boot2_copyout+1))();
+}
+
+// Bitbanging the chip select using IO overrides, in case RAM-resident IRQs
+// are still running, and the FIFO bottoms out. (the bootrom does the same)
+static void __no_inline_not_in_flash_func(flash_cs_force)(bool high) {
+    uint32_t field_val = high ?
+        IO_QSPI_GPIO_QSPI_SS_CTRL_OUTOVER_VALUE_HIGH :
+        IO_QSPI_GPIO_QSPI_SS_CTRL_OUTOVER_VALUE_LOW;
+    hw_write_masked(&ioqspi_hw->io[1].ctrl,
+        field_val << IO_QSPI_GPIO_QSPI_SS_CTRL_OUTOVER_LSB,
+        IO_QSPI_GPIO_QSPI_SS_CTRL_OUTOVER_BITS
+    );
+}
+
+static void __no_inline_not_in_flash_func(flash_do_cmd)(const uint8_t *txbuf, uint8_t *rxbuf, size_t count) {
+    void (*connect_internal_flash)(void) = (void(*)(void))rom_func_lookup(rom_table_code('I', 'F'));
+    void (*flash_exit_xip)(void) = (void(*)(void))rom_func_lookup(rom_table_code('E', 'X'));
+    void (*flash_flush_cache)(void) = (void(*)(void))rom_func_lookup(rom_table_code('F', 'C'));
+    assert(connect_internal_flash && flash_exit_xip && flash_flush_cache);
+    flash_init_boot2_copyout();
+    __asm__ volatile ("" : : : "memory");
+    connect_internal_flash();
+    flash_exit_xip();
+
+    flash_cs_force(0);
+    size_t tx_remaining = count;
+    size_t rx_remaining = count;
+    // We may be interrupted -- don't want FIFO to overflow if we're distracted.
+    const size_t max_in_flight = 16 - 2;
+    while (tx_remaining || rx_remaining) {
+        uint32_t flags = ssi_hw->sr;
+        bool can_put = !!(flags & SSI_SR_TFNF_BITS);
+        bool can_get = !!(flags & SSI_SR_RFNE_BITS);
+        if (can_put && tx_remaining && rx_remaining - tx_remaining < max_in_flight) {
+            ssi_hw->dr0 = *txbuf++;
+            --tx_remaining;
+        }
+        if (can_get && rx_remaining) {
+            *rxbuf++ = (uint8_t)ssi_hw->dr0;
+            --rx_remaining;
+        }
+    }
+    flash_cs_force(1);
+
+    flash_flush_cache();
+    flash_enable_xip_via_boot2();
+}
+
+#endif