blob: 47cbd923ff28a4a30ff6475c1a9eacae19ad6251 [file] [log] [blame]
Angel Ponsa2ee7612020-04-04 18:51:15 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgi40a3e322015-06-22 19:41:29 +02002
Elyes HAOUAS0edf6a52019-10-26 18:41:47 +02003#include <boot/coreboot_tables.h>
Patrick Georgi40a3e322015-06-22 19:41:29 +02004#include <cbfs.h>
5#include <cbmem.h>
6#include <console/console.h>
7#include <soc/mtc.h>
8#include <string.h>
9
10static size_t mtc_table_size;
11
12#define MAX_MTC_TABLE_ENTRIES 20
13#define MTC_TABLE_ENTRY_SIZE 4880
14#define MTC_TABLE_MAX_SIZE (MAX_MTC_TABLE_ENTRIES * MTC_TABLE_ENTRY_SIZE)
15
16int tegra210_run_mtc(void)
17{
Julius Werner77639e42021-02-05 16:51:25 -080018 size_t nread;
Elyes HAOUAS39303d52018-07-08 12:40:45 +020019 void *const mtc = (void *)(uintptr_t)CONFIG_MTC_ADDRESS;
Patrick Georgi40a3e322015-06-22 19:41:29 +020020 void *dvfs_table;
21 size_t (*mtc_fw)(void **dvfs_table) = (void *)mtc;
22
Julius Werner77639e42021-02-05 16:51:25 -080023 nread = cbfs_load("tegra_mtc.bin", mtc, 1*GiB);
24 if (!nread) {
Patrick Georgi40a3e322015-06-22 19:41:29 +020025 printk(BIOS_ERR, "MTC file not found: tegra_mtc.bin\n");
26 return -1;
27 }
28
Patrick Georgi40a3e322015-06-22 19:41:29 +020029 printk(BIOS_INFO, "MTC: %zu bytes loaded @ %p\n", nread, mtc);
30
31 mtc_table_size = (*mtc_fw)(&dvfs_table);
32
33 if ((mtc_table_size == 0) || (mtc_table_size > MTC_TABLE_MAX_SIZE)) {
34 printk(BIOS_ERR, "MTC Training table size is invalid.!\n");
35 return -1;
36 }
37
38 printk(BIOS_INFO, "MTC: Done. Entries size 0x%zx located at %p\n",
39 mtc_table_size, dvfs_table);
40
41 void *cbmem_tab = cbmem_add(CBMEM_ID_MTC, mtc_table_size);
42 if (cbmem_tab == NULL) {
43 printk(BIOS_ERR, "MTC table allocation in cbmem failed!\n");
44 return -1;
45 }
46
47 memcpy(cbmem_tab, dvfs_table, mtc_table_size);
48 printk(BIOS_INFO, "MTC: Copied 0x%zx bytes from %p to %p\n",
49 mtc_table_size, dvfs_table, cbmem_tab);
50
51 return 0;
52}
53
54void soc_add_mtc(struct lb_header *header)
55{
56 struct lb_range *mtc;
57 mtc = (struct lb_range *)lb_new_record(header);
58 mtc->tag = LB_TAG_MTC;
59 mtc->size = sizeof(*mtc);
60
61 mtc->range_start = (uintptr_t)cbmem_find(CBMEM_ID_MTC);
62 mtc->range_size = mtc_table_size;
63}