1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <boot/coreboot_tables.h>
4 #include <cbfs.h>
5 #include <cbmem.h>
6 #include <console/console.h>
7 #include <soc/mtc.h>
8 #include <string.h>
9
10 static 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
tegra210_run_mtc(void)16 int tegra210_run_mtc(void)
17 {
18 size_t nread;
19 void *const mtc = (void *)(uintptr_t)CONFIG_MTC_ADDRESS;
20 void *dvfs_table;
21 size_t (*mtc_fw)(void **dvfs_table) = (void *)mtc;
22
23 nread = cbfs_load("tegra_mtc.bin", mtc, 1*GiB);
24 if (!nread) {
25 printk(BIOS_ERR, "MTC file not found: tegra_mtc.bin\n");
26 return -1;
27 }
28
29 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
soc_add_mtc(struct lb_header * header)54 void 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 }
64