From 84328aa676f26f7424a56a7549fbd463c648ecc7 Mon Sep 17 00:00:00 2001 From: CTCaer Date: Wed, 4 Dec 2019 21:56:45 +0200 Subject: [PATCH] minerva: Make use of new minerva - Training and switch is now faster - Compatibility checks: New Minerva does not allow old binaries. New binaries do not allow old Minerva - MTC table is now in a safe region - Periodic training period increased to every 250ms --- bootloader/mem/minerva.c | 10 ++++---- bootloader/mem/minerva.h | 6 ++++- bootloader/utils/util.c | 7 ++++++ bootloader/utils/util.h | 1 + modules/hekate_libsys_minerva/mtc.h | 5 +++- modules/hekate_libsys_minerva/sys_sdrammtc.c | 12 +++++----- nyx/nyx_gui/mem/minerva.c | 25 +++++++++++++------- nyx/nyx_gui/mem/minerva.h | 6 ++++- nyx/nyx_gui/utils/util.c | 12 +++++++++- nyx/nyx_gui/utils/util.h | 1 + 10 files changed, 63 insertions(+), 22 deletions(-) diff --git a/bootloader/mem/minerva.c b/bootloader/mem/minerva.c index d74bef3..833b381 100644 --- a/bootloader/mem/minerva.c +++ b/bootloader/mem/minerva.c @@ -34,8 +34,10 @@ void minerva_init() mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg; - // Set table to ram. - mtc_cfg->mtc_table = NULL; + // Set table to nyx storage. + mtc_cfg->mtc_table = (emc_table_t *)&nyx_str->mtc_table; + + mtc_cfg->init_done = MTC_NEW_MAGIC; mtc_cfg->sdram_id = (fuse_read_odm(4) >> 3) & 0x1F; u32 ep_addr = ianos_loader(false, "bootloader/sys/libsys_minerva.bso", DRAM_LIB, (void *)mtc_cfg); minerva_cfg = (void *)ep_addr; @@ -66,7 +68,7 @@ void minerva_change_freq(minerva_freq_t freq) return; mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg; - if (minerva_cfg && (mtc_cfg->rate_from != freq)) + if (mtc_cfg->rate_from != freq) { mtc_cfg->rate_to = freq; mtc_cfg->train_mode = OP_SWITCH; @@ -80,7 +82,7 @@ void minerva_periodic_training() return; mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg; - if (minerva_cfg && mtc_cfg->rate_from == FREQ_1600) + if (mtc_cfg->rate_from == FREQ_1600) { mtc_cfg->train_mode = OP_PERIODIC_TRAIN; minerva_cfg(mtc_cfg, NULL); diff --git a/bootloader/mem/minerva.h b/bootloader/mem/minerva.h index dd71658..f1edbaa 100644 --- a/bootloader/mem/minerva.h +++ b/bootloader/mem/minerva.h @@ -20,7 +20,10 @@ #include "mtc_table.h" #include "../utils/types.h" -#define EMC_PERIODIC_TRAIN_MS 100 +#define MTC_INIT_MAGIC 0x3043544D +#define MTC_NEW_MAGIC 0x5243544D + +#define EMC_PERIODIC_TRAIN_MS 250 typedef struct { @@ -35,6 +38,7 @@ typedef struct bool emc_2X_clk_src_is_pllmb; bool fsp_for_src_freq; bool train_ram_patterns; + bool init_done; } mtc_config_t; enum train_mode_t diff --git a/bootloader/utils/util.c b/bootloader/utils/util.c index 3fb11aa..cae1981 100644 --- a/bootloader/utils/util.c +++ b/bootloader/utils/util.c @@ -17,6 +17,7 @@ #include "util.h" #include "../gfx/di.h" +#include "../mem/minerva.h" #include "../power/max77620.h" #include "../rtc/max77620-rtc.h" #include "../soc/bpmp.h" @@ -26,6 +27,8 @@ #define USE_RTC_TIMER +extern volatile nyx_storage_t *nyx_str; + extern void sd_unmount(); u32 get_tmr_s() @@ -100,6 +103,8 @@ void reboot_normal() sd_unmount(); display_end(); + nyx_str->mtc_cfg.init_done = 0; + panic(0x21); // Bypass fuse programming in package1. } @@ -110,6 +115,8 @@ void reboot_rcm() sd_unmount(); display_end(); + nyx_str->mtc_cfg.init_done = 0; + PMC(APBDEV_PMC_SCRATCH0) = 2; // Reboot into rcm. PMC(APBDEV_PMC_CNTRL) |= PMC_CNTRL_MAIN_RST; diff --git a/bootloader/utils/util.h b/bootloader/utils/util.h index f257b7c..55f5f18 100644 --- a/bootloader/utils/util.h +++ b/bootloader/utils/util.h @@ -41,6 +41,7 @@ typedef struct _nyx_storage_t u8 hekate[0x30000]; u8 rsvd[0x800000]; mtc_config_t mtc_cfg; + emc_table_t mtc_table; } nyx_storage_t; u32 get_tmr_us(); diff --git a/modules/hekate_libsys_minerva/mtc.h b/modules/hekate_libsys_minerva/mtc.h index 8d00701..a673d38 100644 --- a/modules/hekate_libsys_minerva/mtc.h +++ b/modules/hekate_libsys_minerva/mtc.h @@ -30,7 +30,9 @@ #define EMC_BASE 0x7001B000 #define EMC0_BASE 0x7001E000 #define EMC1_BASE 0x7001F000 -#define MTC_TABLE 0x8F000000 + +#define MTC_INIT_MAGIC 0x3043544D +#define MTC_NEW_MAGIC 0x5243544D #define _REG(base, off) *(vu32 *)((base) + (off)) @@ -61,6 +63,7 @@ typedef struct bool emc_2X_clk_src_is_pllmb; bool fsp_for_src_freq; bool train_ram_patterns; + bool init_done; } mtc_config_t; enum train_mode_t diff --git a/modules/hekate_libsys_minerva/sys_sdrammtc.c b/modules/hekate_libsys_minerva/sys_sdrammtc.c index 43e4e01..9b83b27 100644 --- a/modules/hekate_libsys_minerva/sys_sdrammtc.c +++ b/modules/hekate_libsys_minerva/sys_sdrammtc.c @@ -3857,19 +3857,17 @@ static void _minerva_get_table(mtc_config_t *mtc_cfg) switch (mtc_cfg->sdram_id) { case 1: - memcpy((void *)MTC_TABLE, nx_abca2_2_10NoCfgVersion_V9_8_7_V1_6, EMC_TABLE_SIZE_R7); + memcpy(mtc_cfg->mtc_table, nx_abca2_2_10NoCfgVersion_V9_8_7_V1_6, EMC_TABLE_SIZE_R7); break; case 0: case 2: case 3: case 4: default: - memcpy((void *)MTC_TABLE, nx_abca2_0_3_10NoCfgVersion_V9_8_7_V1_6, EMC_TABLE_SIZE_R7); + memcpy(mtc_cfg->mtc_table, nx_abca2_0_3_10NoCfgVersion_V9_8_7_V1_6, EMC_TABLE_SIZE_R7); break; } - mtc_cfg->mtc_table = (emc_table_t *)MTC_TABLE; - mtc_cfg->table_entries = 10; mtc_cfg->rate_to = 0; mtc_cfg->rate_from = 0; @@ -3881,6 +3879,7 @@ static void _minerva_get_table(mtc_config_t *mtc_cfg) mtc_cfg->emc_2X_clk_src_is_pllmb = false; mtc_cfg->fsp_for_src_freq = false; mtc_cfg->train_ram_patterns = true; + mtc_cfg->init_done = MTC_INIT_MAGIC; } void _minerva_init(mtc_config_t *mtc_cfg, void* bp) @@ -3891,9 +3890,10 @@ void _minerva_init(mtc_config_t *mtc_cfg, void* bp) fsp_for_src_freq = mtc_cfg->fsp_for_src_freq; emc_2X_clk_src_is_pllmb = mtc_cfg->emc_2X_clk_src_is_pllmb; - if (!mtc_cfg->mtc_table) + if (mtc_cfg->init_done != MTC_INIT_MAGIC) { - _minerva_get_table(mtc_cfg); + if (mtc_cfg->init_done == MTC_NEW_MAGIC) + _minerva_get_table(mtc_cfg); return; } diff --git a/nyx/nyx_gui/mem/minerva.c b/nyx/nyx_gui/mem/minerva.c index 0f3bed8..2992d7c 100644 --- a/nyx/nyx_gui/mem/minerva.c +++ b/nyx/nyx_gui/mem/minerva.c @@ -34,23 +34,32 @@ void minerva_init() mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg; + // Set table to nyx storage. + mtc_cfg->mtc_table = (emc_table_t *)&nyx_str->mtc_table; + // Set table to ram. - if (!(mtc_cfg->table_entries == 10)) + if (mtc_cfg->init_done == MTC_INIT_MAGIC) { - mtc_cfg->mtc_table = NULL; - mtc_cfg->sdram_id = (fuse_read_odm(4) >> 3) & 0x1F; + mtc_cfg->train_mode = OP_PERIODIC_TRAIN; // Retrain if needed. u32 ep_addr = ianos_loader(false, "bootloader/sys/libsys_minerva.bso", DRAM_LIB, (void *)mtc_cfg); minerva_cfg = (void *)ep_addr; + + return; } else { mtc_config_t mtc_tmp; - mtc_tmp.mtc_table = NULL; + mtc_tmp.mtc_table = mtc_cfg->mtc_table; mtc_tmp.sdram_id = (fuse_read_odm(4) >> 3) & 0x1F; + mtc_tmp.init_done = MTC_NEW_MAGIC; u32 ep_addr = ianos_loader(false, "bootloader/sys/libsys_minerva.bso", DRAM_LIB, (void *)&mtc_tmp); - minerva_cfg = (void *)ep_addr; - - return; + + if (mtc_tmp.init_done == MTC_INIT_MAGIC) + minerva_cfg = (void *)ep_addr; + + // Copy Minerva context to Nyx storage. + if (minerva_cfg) + memcpy(mtc_cfg, (void *)&mtc_tmp, sizeof(mtc_config_t)); } if (!minerva_cfg) @@ -79,7 +88,7 @@ void minerva_change_freq(minerva_freq_t freq) return; mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg; - if (minerva_cfg && (mtc_cfg->rate_from != freq)) + if (mtc_cfg->rate_from != freq) { mtc_cfg->rate_to = freq; mtc_cfg->train_mode = OP_SWITCH; diff --git a/nyx/nyx_gui/mem/minerva.h b/nyx/nyx_gui/mem/minerva.h index dd71658..f1edbaa 100644 --- a/nyx/nyx_gui/mem/minerva.h +++ b/nyx/nyx_gui/mem/minerva.h @@ -20,7 +20,10 @@ #include "mtc_table.h" #include "../utils/types.h" -#define EMC_PERIODIC_TRAIN_MS 100 +#define MTC_INIT_MAGIC 0x3043544D +#define MTC_NEW_MAGIC 0x5243544D + +#define EMC_PERIODIC_TRAIN_MS 250 typedef struct { @@ -35,6 +38,7 @@ typedef struct bool emc_2X_clk_src_is_pllmb; bool fsp_for_src_freq; bool train_ram_patterns; + bool init_done; } mtc_config_t; enum train_mode_t diff --git a/nyx/nyx_gui/utils/util.c b/nyx/nyx_gui/utils/util.c index e99467d..a705325 100644 --- a/nyx/nyx_gui/utils/util.c +++ b/nyx/nyx_gui/utils/util.c @@ -17,6 +17,7 @@ #include "util.h" #include "../gfx/di.h" +#include "../mem/minerva.h" #include "../power/max77620.h" #include "../soc/bpmp.h" #include "../soc/i2c.h" @@ -25,6 +26,8 @@ #define USE_RTC_TIMER +extern volatile nyx_storage_t *nyx_str; + extern void sd_unmount(bool deinit); u32 get_tmr_s() @@ -99,6 +102,8 @@ void reboot_normal() sd_unmount(true); display_end(); + nyx_str->mtc_cfg.init_done = 0; + panic(0x21); // Bypass fuse programming in package1. } @@ -109,6 +114,8 @@ void reboot_rcm() sd_unmount(true); display_end(); + nyx_str->mtc_cfg.init_done = 0; + PMC(APBDEV_PMC_SCRATCH0) = 2; // Reboot into rcm. PMC(APBDEV_PMC_CNTRL) |= PMC_CNTRL_MAIN_RST; @@ -121,8 +128,11 @@ void power_off() sd_unmount(true); display_end(); + // Stop the alarm, in case we injected and powered off too fast. + max77620_rtc_stop_alarm(); + i2c_send_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_ONOFFCNFG1, MAX77620_ONOFFCNFG1_PWR_OFF); - + while (true) bpmp_halt(); } diff --git a/nyx/nyx_gui/utils/util.h b/nyx/nyx_gui/utils/util.h index f257b7c..55f5f18 100644 --- a/nyx/nyx_gui/utils/util.h +++ b/nyx/nyx_gui/utils/util.h @@ -41,6 +41,7 @@ typedef struct _nyx_storage_t u8 hekate[0x30000]; u8 rsvd[0x800000]; mtc_config_t mtc_cfg; + emc_table_t mtc_table; } nyx_storage_t; u32 get_tmr_us();