2019-06-30 00:49:33 +00:00
|
|
|
/*
|
2022-01-20 10:43:24 +00:00
|
|
|
* Copyright (c) 2019-2022 CTCaer
|
2019-06-30 00:49:33 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "minerva.h"
|
|
|
|
|
2020-06-14 13:45:45 +00:00
|
|
|
#include <ianos/ianos.h>
|
2022-01-15 23:03:24 +00:00
|
|
|
#include <mem/emc.h>
|
2020-06-14 13:45:45 +00:00
|
|
|
#include <soc/clock.h>
|
|
|
|
#include <soc/fuse.h>
|
2020-07-04 18:02:45 +00:00
|
|
|
#include <soc/hw_init.h>
|
2020-06-14 13:45:45 +00:00
|
|
|
#include <soc/t210.h>
|
|
|
|
#include <utils/util.h>
|
2019-06-30 00:49:33 +00:00
|
|
|
|
|
|
|
extern volatile nyx_storage_t *nyx_str;
|
2020-06-13 15:16:29 +00:00
|
|
|
|
2020-05-08 22:32:44 +00:00
|
|
|
void (*minerva_cfg)(mtc_config_t *mtc_cfg, void *);
|
2019-06-30 00:49:33 +00:00
|
|
|
|
2019-12-09 20:27:01 +00:00
|
|
|
u32 minerva_init()
|
2019-06-30 00:49:33 +00:00
|
|
|
{
|
|
|
|
u32 curr_ram_idx = 0;
|
|
|
|
|
2019-12-16 20:06:13 +00:00
|
|
|
minerva_cfg = NULL;
|
2019-06-30 00:49:33 +00:00
|
|
|
mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg;
|
2020-06-13 15:16:29 +00:00
|
|
|
|
2020-07-04 18:02:45 +00:00
|
|
|
//!TODO: Not supported on T210B01 yet.
|
|
|
|
if (hw_get_chip_id() == GP_HIDREV_MAJOR_T210B01)
|
|
|
|
return 0;
|
|
|
|
|
2022-01-20 11:19:48 +00:00
|
|
|
#ifdef BDK_MINERVA_CFG_FROM_RAM
|
2020-06-13 15:16:29 +00:00
|
|
|
// Set table to nyx storage.
|
|
|
|
mtc_cfg->mtc_table = (emc_table_t *)nyx_str->mtc_table;
|
|
|
|
|
|
|
|
// Check if Minerva is already initialized.
|
|
|
|
if (mtc_cfg->init_done == MTC_INIT_MAGIC)
|
|
|
|
{
|
|
|
|
mtc_cfg->train_mode = OP_PERIODIC_TRAIN; // Retrain if needed.
|
2020-06-14 10:17:26 +00:00
|
|
|
u32 ep_addr = ianos_loader("bootloader/sys/libsys_minerva.bso", DRAM_LIB, (void *)mtc_cfg);
|
2020-06-13 15:16:29 +00:00
|
|
|
minerva_cfg = (void *)ep_addr;
|
|
|
|
|
2020-11-15 11:40:13 +00:00
|
|
|
return !minerva_cfg ? 1 : 0;
|
2020-06-13 15:16:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mtc_config_t mtc_tmp;
|
|
|
|
|
|
|
|
mtc_tmp.mtc_table = mtc_cfg->mtc_table;
|
2020-12-26 14:34:12 +00:00
|
|
|
mtc_tmp.sdram_id = fuse_read_dramid(false);
|
2020-06-13 15:16:29 +00:00
|
|
|
mtc_tmp.init_done = MTC_NEW_MAGIC;
|
|
|
|
|
2020-06-14 10:17:26 +00:00
|
|
|
u32 ep_addr = ianos_loader("bootloader/sys/libsys_minerva.bso", DRAM_LIB, (void *)&mtc_tmp);
|
2020-06-13 15:16:29 +00:00
|
|
|
|
|
|
|
// Ensure that Minerva is new.
|
|
|
|
if (mtc_tmp.init_done == MTC_INIT_MAGIC)
|
|
|
|
minerva_cfg = (void *)ep_addr;
|
|
|
|
else
|
|
|
|
mtc_cfg->init_done = 0;
|
|
|
|
|
|
|
|
// Copy Minerva context to Nyx storage.
|
|
|
|
if (minerva_cfg)
|
|
|
|
memcpy(mtc_cfg, (void *)&mtc_tmp, sizeof(mtc_config_t));
|
|
|
|
}
|
|
|
|
#else
|
2020-03-21 20:10:06 +00:00
|
|
|
memset(mtc_cfg, 0, sizeof(mtc_config_t));
|
2019-06-30 00:49:33 +00:00
|
|
|
|
2019-12-04 19:56:45 +00:00
|
|
|
// Set table to nyx storage.
|
2020-04-30 00:47:23 +00:00
|
|
|
mtc_cfg->mtc_table = (emc_table_t *)nyx_str->mtc_table;
|
2019-12-04 19:56:45 +00:00
|
|
|
|
2020-12-26 14:34:12 +00:00
|
|
|
mtc_cfg->sdram_id = fuse_read_dramid(false);
|
2019-12-09 20:27:01 +00:00
|
|
|
mtc_cfg->init_done = MTC_NEW_MAGIC; // Initialize mtc table.
|
2020-03-21 20:10:06 +00:00
|
|
|
|
2020-06-14 10:17:26 +00:00
|
|
|
u32 ep_addr = ianos_loader("bootloader/sys/libsys_minerva.bso", DRAM_LIB, (void *)mtc_cfg);
|
2019-12-09 20:27:01 +00:00
|
|
|
|
|
|
|
// Ensure that Minerva is new.
|
|
|
|
if (mtc_cfg->init_done == MTC_INIT_MAGIC)
|
|
|
|
minerva_cfg = (void *)ep_addr;
|
2020-03-21 20:10:06 +00:00
|
|
|
else
|
|
|
|
mtc_cfg->init_done = 0;
|
2020-06-13 15:16:29 +00:00
|
|
|
#endif
|
2019-06-30 00:49:33 +00:00
|
|
|
|
|
|
|
if (!minerva_cfg)
|
2019-12-09 20:27:01 +00:00
|
|
|
return 1;
|
2019-06-30 00:49:33 +00:00
|
|
|
|
|
|
|
// Get current frequency
|
2022-01-20 10:43:24 +00:00
|
|
|
u32 current_emc_clk_src = CLOCK(CLK_RST_CONTROLLER_CLK_SOURCE_EMC);
|
2019-06-30 00:49:33 +00:00
|
|
|
for (curr_ram_idx = 0; curr_ram_idx < 10; curr_ram_idx++)
|
|
|
|
{
|
2022-01-20 10:43:24 +00:00
|
|
|
if (current_emc_clk_src == mtc_cfg->mtc_table[curr_ram_idx].clk_src_emc)
|
2019-06-30 00:49:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtc_cfg->rate_from = mtc_cfg->mtc_table[curr_ram_idx].rate_khz;
|
2021-04-09 16:28:04 +00:00
|
|
|
mtc_cfg->rate_to = FREQ_204;
|
2019-06-30 00:49:33 +00:00
|
|
|
mtc_cfg->train_mode = OP_TRAIN;
|
|
|
|
minerva_cfg(mtc_cfg, NULL);
|
2021-04-09 16:28:04 +00:00
|
|
|
mtc_cfg->rate_to = FREQ_800;
|
2019-06-30 00:49:33 +00:00
|
|
|
minerva_cfg(mtc_cfg, NULL);
|
2021-04-09 16:28:04 +00:00
|
|
|
mtc_cfg->rate_to = FREQ_1600;
|
2019-06-30 00:49:33 +00:00
|
|
|
minerva_cfg(mtc_cfg, NULL);
|
2019-12-07 23:15:35 +00:00
|
|
|
|
|
|
|
// FSP WAR.
|
|
|
|
mtc_cfg->train_mode = OP_SWITCH;
|
2021-04-09 16:28:04 +00:00
|
|
|
mtc_cfg->rate_to = FREQ_800;
|
2019-12-07 23:15:35 +00:00
|
|
|
minerva_cfg(mtc_cfg, NULL);
|
|
|
|
|
|
|
|
// Switch to max.
|
2021-04-09 16:28:04 +00:00
|
|
|
mtc_cfg->rate_to = FREQ_1600;
|
2019-12-07 23:15:35 +00:00
|
|
|
minerva_cfg(mtc_cfg, NULL);
|
2019-12-09 20:27:01 +00:00
|
|
|
|
|
|
|
return 0;
|
2019-06-30 00:49:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void minerva_change_freq(minerva_freq_t freq)
|
|
|
|
{
|
|
|
|
if (!minerva_cfg)
|
|
|
|
return;
|
2019-09-12 20:08:38 +00:00
|
|
|
|
2021-01-04 00:45:32 +00:00
|
|
|
// Check if requested frequency is different. Do not allow otherwise because it will hang.
|
2019-06-30 00:49:33 +00:00
|
|
|
mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg;
|
2019-12-04 19:56:45 +00:00
|
|
|
if (mtc_cfg->rate_from != freq)
|
2019-06-30 00:49:33 +00:00
|
|
|
{
|
|
|
|
mtc_cfg->rate_to = freq;
|
|
|
|
mtc_cfg->train_mode = OP_SWITCH;
|
|
|
|
minerva_cfg(mtc_cfg, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 16:28:04 +00:00
|
|
|
void minerva_prep_boot_freq()
|
|
|
|
{
|
|
|
|
if (!minerva_cfg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg;
|
|
|
|
|
|
|
|
// Check if there's RAM OC. If not exit.
|
|
|
|
if (mtc_cfg->mtc_table[mtc_cfg->table_entries - 1].rate_khz == FREQ_1600)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FSP WAR.
|
|
|
|
minerva_change_freq(FREQ_204);
|
|
|
|
// Scale down to 800 MHz boot freq.
|
|
|
|
minerva_change_freq(FREQ_800);
|
|
|
|
}
|
|
|
|
|
2022-03-22 22:58:20 +00:00
|
|
|
void minerva_prep_boot_l4t(int oc_freq)
|
2022-01-20 10:43:24 +00:00
|
|
|
{
|
|
|
|
if (!minerva_cfg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg;
|
|
|
|
|
2022-03-22 22:58:20 +00:00
|
|
|
// Add OC frequency.
|
|
|
|
if (oc_freq && mtc_cfg->mtc_table[mtc_cfg->table_entries - 1].rate_khz == FREQ_1600)
|
|
|
|
{
|
|
|
|
memcpy(&mtc_cfg->mtc_table[mtc_cfg->table_entries],
|
|
|
|
&mtc_cfg->mtc_table[mtc_cfg->table_entries - 1],
|
|
|
|
sizeof(emc_table_t));
|
|
|
|
mtc_cfg->mtc_table[mtc_cfg->table_entries].rate_khz = oc_freq;
|
|
|
|
mtc_cfg->table_entries++;
|
|
|
|
}
|
|
|
|
|
2022-01-20 10:43:24 +00:00
|
|
|
// Set init frequency.
|
|
|
|
minerva_change_freq(FREQ_204);
|
|
|
|
|
|
|
|
// Train the rest of the frequencies.
|
|
|
|
mtc_cfg->train_mode = OP_TRAIN;
|
|
|
|
for (u32 i = 0; i < mtc_cfg->table_entries; i++)
|
|
|
|
{
|
|
|
|
mtc_cfg->rate_to = mtc_cfg->mtc_table[i].rate_khz;
|
|
|
|
// Skip already trained frequencies.
|
|
|
|
if (mtc_cfg->rate_to == FREQ_204 || mtc_cfg->rate_to == FREQ_800 || mtc_cfg->rate_to == FREQ_1600)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Train frequency.
|
|
|
|
minerva_cfg(mtc_cfg, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do FSP WAR and scale to 800 MHz as boot freq.
|
2022-01-28 23:31:28 +00:00
|
|
|
bool fsp_opwr_disabled = !(EMC(EMC_MRW3) & 0xC0);
|
|
|
|
if (fsp_opwr_disabled)
|
2022-01-20 10:43:24 +00:00
|
|
|
minerva_change_freq(FREQ_666);
|
|
|
|
minerva_change_freq(FREQ_800);
|
|
|
|
|
2022-03-22 22:58:20 +00:00
|
|
|
// Trim table.
|
|
|
|
int entries = 0;
|
|
|
|
for (u32 i = 0; i < mtc_cfg->table_entries; i++)
|
|
|
|
{
|
|
|
|
// Copy freqs from 204 MHz to 800 MHz and 1600 MHz and above.
|
|
|
|
int rate = mtc_cfg->mtc_table[i].rate_khz;
|
|
|
|
if ((rate >= FREQ_204 && rate <= FREQ_800) || rate >= FREQ_1600)
|
|
|
|
{
|
|
|
|
memcpy(&mtc_cfg->mtc_table[entries], &mtc_cfg->mtc_table[i], sizeof(emc_table_t));
|
|
|
|
entries++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mtc_cfg->table_entries = entries;
|
|
|
|
|
2022-01-20 10:43:24 +00:00
|
|
|
// Do not let other mtc ops.
|
|
|
|
mtc_cfg->init_done = 0;
|
|
|
|
}
|
|
|
|
|
2019-06-30 00:49:33 +00:00
|
|
|
void minerva_periodic_training()
|
|
|
|
{
|
|
|
|
if (!minerva_cfg)
|
|
|
|
return;
|
2019-09-12 20:08:38 +00:00
|
|
|
|
2019-06-30 00:49:33 +00:00
|
|
|
mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg;
|
2019-12-04 19:56:45 +00:00
|
|
|
if (mtc_cfg->rate_from == FREQ_1600)
|
2019-06-30 00:49:33 +00:00
|
|
|
{
|
|
|
|
mtc_cfg->train_mode = OP_PERIODIC_TRAIN;
|
|
|
|
minerva_cfg(mtc_cfg, NULL);
|
|
|
|
}
|
2022-01-20 10:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emc_table_t *minerva_get_mtc_table()
|
|
|
|
{
|
|
|
|
if (!minerva_cfg)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg;
|
|
|
|
return mtc_cfg->mtc_table;
|
|
|
|
}
|
2022-03-22 22:58:20 +00:00
|
|
|
|
|
|
|
int minerva_get_mtc_table_entries()
|
|
|
|
{
|
|
|
|
if (!minerva_cfg)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
mtc_config_t *mtc_cfg = (mtc_config_t *)&nyx_str->mtc_cfg;
|
|
|
|
return mtc_cfg->table_entries;
|
|
|
|
}
|