35cd9e027c041e5389f545b8963e1d15c4545f00
[platform/kernel/u-boot.git] / board / ti / am64x / evm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Board specific initialization for AM642 EVM
4  *
5  * Copyright (C) 2020-2021 Texas Instruments Incorporated - https://www.ti.com/
6  *      Keerthy <j-keerthy@ti.com>
7  *
8  */
9
10 #include <common.h>
11 #include <asm/io.h>
12 #include <spl.h>
13 #include <asm/arch/hardware.h>
14 #include <asm/arch/sys_proto.h>
15 #include <env.h>
16
17 #include "../common/board_detect.h"
18
19 #define board_is_am64x_gpevm()  board_ti_k3_is("AM64-GPEVM")
20 #define board_is_am64x_skevm()  board_ti_k3_is("AM64-SKEVM")
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 int board_init(void)
25 {
26         return 0;
27 }
28
29 int dram_init(void)
30 {
31         gd->ram_size = 0x80000000;
32
33         return 0;
34 }
35
36 int dram_init_banksize(void)
37 {
38         /* Bank 0 declares the memory available in the DDR low region */
39         gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
40         gd->bd->bi_dram[0].size = 0x80000000;
41         gd->ram_size = 0x80000000;
42
43         return 0;
44 }
45
46 #if defined(CONFIG_SPL_LOAD_FIT)
47 int board_fit_config_name_match(const char *name)
48 {
49         bool eeprom_read = board_ti_was_eeprom_read();
50
51         if (!eeprom_read || board_is_am64x_gpevm()) {
52                 if (!strcmp(name, "k3-am642-r5-evm") || !strcmp(name, "k3-am642-evm"))
53                         return 0;
54         } else if (board_is_am64x_skevm()) {
55                 if (!strcmp(name, "k3-am642-r5-sk") || !strcmp(name, "k3-am642-sk"))
56                         return 0;
57         }
58
59         return -1;
60 }
61 #endif
62
63 #ifdef CONFIG_TI_I2C_BOARD_DETECT
64 int do_board_detect(void)
65 {
66         int ret;
67
68         ret = ti_i2c_eeprom_am6_get_base(CONFIG_EEPROM_BUS_ADDRESS,
69                                          CONFIG_EEPROM_CHIP_ADDRESS);
70         if (ret) {
71                 printf("EEPROM not available at 0x%02x, trying to read at 0x%02x\n",
72                         CONFIG_EEPROM_CHIP_ADDRESS, CONFIG_EEPROM_CHIP_ADDRESS + 1);
73                 ret = ti_i2c_eeprom_am6_get_base(CONFIG_EEPROM_BUS_ADDRESS,
74                                                  CONFIG_EEPROM_CHIP_ADDRESS + 1);
75                 if (ret)
76                         pr_err("Reading on-board EEPROM at 0x%02x failed %d\n",
77                                CONFIG_EEPROM_CHIP_ADDRESS + 1, ret);
78         }
79
80         return ret;
81 }
82
83 int checkboard(void)
84 {
85         struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
86
87         if (!do_board_detect())
88                 printf("Board: %s rev %s\n", ep->name, ep->version);
89
90         return 0;
91 }
92
93 #ifdef CONFIG_BOARD_LATE_INIT
94 static void setup_board_eeprom_env(void)
95 {
96         char *name = "am64x_gpevm";
97
98         if (do_board_detect())
99                 goto invalid_eeprom;
100
101         if (board_is_am64x_gpevm())
102                 name = "am64x_gpevm";
103         else if (board_is_am64x_skevm())
104                 name = "am64x_skevm";
105         else
106                 printf("Unidentified board claims %s in eeprom header\n",
107                        board_ti_get_name());
108
109 invalid_eeprom:
110         set_board_info_env_am6(name);
111 }
112
113 static void setup_serial(void)
114 {
115         struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
116         unsigned long board_serial;
117         char *endp;
118         char serial_string[17] = { 0 };
119
120         if (env_get("serial#"))
121                 return;
122
123         board_serial = simple_strtoul(ep->serial, &endp, 16);
124         if (*endp != '\0') {
125                 pr_err("Error: Can't set serial# to %s\n", ep->serial);
126                 return;
127         }
128
129         snprintf(serial_string, sizeof(serial_string), "%016lx", board_serial);
130         env_set("serial#", serial_string);
131 }
132 #endif
133 #endif
134
135 #ifdef CONFIG_BOARD_LATE_INIT
136 int board_late_init(void)
137 {
138         if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT)) {
139                 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
140
141                 setup_board_eeprom_env();
142                 setup_serial();
143                 /*
144                  * The first MAC address for ethernet a.k.a. ethernet0 comes from
145                  * efuse populated via the am654 gigabit eth switch subsystem driver.
146                  * All the other ones are populated via EEPROM, hence continue with
147                  * an index of 1.
148                  */
149                 board_ti_am6_set_ethaddr(1, ep->mac_addr_cnt);
150         }
151
152         return 0;
153 }
154 #endif