blob: 96f4e3013a3ef85083efaede708603ede183de9d [file] [log] [blame]
Dave Gerlach3dc33f12021-04-23 11:27:42 -05001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Board specific initialization for AM642 EVM
4 *
Dave Gerlach10498d12022-03-17 12:03:44 -05005 * Copyright (C) 2020-2022 Texas Instruments Incorporated - https://www.ti.com/
Dave Gerlach3dc33f12021-04-23 11:27:42 -05006 * Keerthy <j-keerthy@ti.com>
7 *
8 */
9
10#include <common.h>
11#include <asm/io.h>
Dave Gerlach10498d12022-03-17 12:03:44 -050012#include <dm/uclass.h>
13#include <k3-ddrss.h>
Dave Gerlach3dc33f12021-04-23 11:27:42 -050014#include <spl.h>
Aswath Govindrajue910fac2021-08-04 18:42:44 +053015#include <fdt_support.h>
Lokesh Vutla01032a42021-05-06 16:44:49 +053016#include <asm/arch/hardware.h>
Lokesh Vutla01032a42021-05-06 16:44:49 +053017#include <env.h>
18
19#include "../common/board_detect.h"
20
21#define board_is_am64x_gpevm() board_ti_k3_is("AM64-GPEVM")
Judith Mendezde35c3a2023-04-06 11:49:01 +053022
23#define board_is_am64x_skevm() (board_ti_k3_is("AM64-SKEVM") || \
24 board_ti_k3_is("AM64B-SKEVM"))
Dave Gerlach3dc33f12021-04-23 11:27:42 -050025
26DECLARE_GLOBAL_DATA_PTR;
27
28int board_init(void)
29{
30 return 0;
31}
32
33int dram_init(void)
34{
Dave Gerlach88107fe2022-03-17 12:03:40 -050035 s32 ret;
Dave Gerlach3dc33f12021-04-23 11:27:42 -050036
Dave Gerlach88107fe2022-03-17 12:03:40 -050037 ret = fdtdec_setup_mem_size_base();
38 if (ret)
39 printf("Error setting up mem size and base. %d\n", ret);
40
41 return ret;
Dave Gerlach3dc33f12021-04-23 11:27:42 -050042}
43
44int dram_init_banksize(void)
45{
Dave Gerlach88107fe2022-03-17 12:03:40 -050046 s32 ret;
47
48 ret = fdtdec_setup_memory_banksize();
49 if (ret)
50 printf("Error setting up memory banksize. %d\n", ret);
Dave Gerlach3dc33f12021-04-23 11:27:42 -050051
Dave Gerlach88107fe2022-03-17 12:03:40 -050052 return ret;
Dave Gerlach3dc33f12021-04-23 11:27:42 -050053}
54
55#if defined(CONFIG_SPL_LOAD_FIT)
56int board_fit_config_name_match(const char *name)
57{
Lokesh Vutla8af2b692021-05-06 16:44:51 +053058 bool eeprom_read = board_ti_was_eeprom_read();
59
60 if (!eeprom_read || board_is_am64x_gpevm()) {
61 if (!strcmp(name, "k3-am642-r5-evm") || !strcmp(name, "k3-am642-evm"))
62 return 0;
63 } else if (board_is_am64x_skevm()) {
64 if (!strcmp(name, "k3-am642-r5-sk") || !strcmp(name, "k3-am642-sk"))
65 return 0;
66 }
Dave Gerlach3dc33f12021-04-23 11:27:42 -050067
68 return -1;
69}
70#endif
Lokesh Vutla01032a42021-05-06 16:44:49 +053071
Dave Gerlach10498d12022-03-17 12:03:44 -050072#if defined(CONFIG_SPL_BUILD)
73#if CONFIG_IS_ENABLED(USB_STORAGE)
Aswath Govindrajue910fac2021-08-04 18:42:44 +053074static int fixup_usb_boot(const void *fdt_blob)
75{
76 int ret = 0;
77
78 switch (spl_boot_device()) {
79 case BOOT_DEVICE_USB:
80 /*
81 * If the boot mode is host, fixup the dr_mode to host
82 * before cdns3 bind takes place
83 */
84 ret = fdt_find_and_setprop((void *)fdt_blob,
85 "/bus@f4000/cdns-usb@f900000/usb@f400000",
86 "dr_mode", "host", 5, 0);
87 if (ret)
88 printf("%s: fdt_find_and_setprop() failed:%d\n",
89 __func__, ret);
90 fallthrough;
91 default:
92 break;
93 }
94
95 return ret;
96}
Dave Gerlach10498d12022-03-17 12:03:44 -050097#endif
98
99#if defined(CONFIG_K3_AM64_DDRSS)
100static void fixup_ddr_driver_for_ecc(struct spl_image_info *spl_image)
101{
102 struct udevice *dev;
103 int ret;
104
105 dram_init_banksize();
106
107 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
108 if (ret)
109 panic("Cannot get RAM device for ddr size fixup: %d\n", ret);
110
111 ret = k3_ddrss_ddr_fdt_fixup(dev, spl_image->fdt_addr, gd->bd);
112 if (ret)
113 printf("Error fixing up ddr node for ECC use! %d\n", ret);
114}
115#else
116static void fixup_memory_node(struct spl_image_info *spl_image)
117{
118 u64 start[CONFIG_NR_DRAM_BANKS];
119 u64 size[CONFIG_NR_DRAM_BANKS];
120 int bank;
121 int ret;
122
123 dram_init();
124 dram_init_banksize();
125
126 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
127 start[bank] = gd->bd->bi_dram[bank].start;
128 size[bank] = gd->bd->bi_dram[bank].size;
129 }
130
131 /* dram_init functions use SPL fdt, and we must fixup u-boot fdt */
132 ret = fdt_fixup_memory_banks(spl_image->fdt_addr, start, size, CONFIG_NR_DRAM_BANKS);
133 if (ret)
134 printf("Error fixing up memory node! %d\n", ret);
135}
136#endif
Aswath Govindrajue910fac2021-08-04 18:42:44 +0530137
138void spl_perform_fixups(struct spl_image_info *spl_image)
139{
Dave Gerlach10498d12022-03-17 12:03:44 -0500140#if defined(CONFIG_K3_AM64_DDRSS)
141 fixup_ddr_driver_for_ecc(spl_image);
142#else
143 fixup_memory_node(spl_image);
144#endif
145
146#if CONFIG_IS_ENABLED(USB_STORAGE)
Aswath Govindrajue910fac2021-08-04 18:42:44 +0530147 fixup_usb_boot(spl_image->fdt_addr);
Dave Gerlach10498d12022-03-17 12:03:44 -0500148#endif
Aswath Govindrajue910fac2021-08-04 18:42:44 +0530149}
150#endif
151
Lokesh Vutla01032a42021-05-06 16:44:49 +0530152#ifdef CONFIG_TI_I2C_BOARD_DETECT
153int do_board_detect(void)
154{
155 int ret;
156
157 ret = ti_i2c_eeprom_am6_get_base(CONFIG_EEPROM_BUS_ADDRESS,
158 CONFIG_EEPROM_CHIP_ADDRESS);
159 if (ret) {
160 printf("EEPROM not available at 0x%02x, trying to read at 0x%02x\n",
161 CONFIG_EEPROM_CHIP_ADDRESS, CONFIG_EEPROM_CHIP_ADDRESS + 1);
162 ret = ti_i2c_eeprom_am6_get_base(CONFIG_EEPROM_BUS_ADDRESS,
163 CONFIG_EEPROM_CHIP_ADDRESS + 1);
164 if (ret)
165 pr_err("Reading on-board EEPROM at 0x%02x failed %d\n",
166 CONFIG_EEPROM_CHIP_ADDRESS + 1, ret);
167 }
168
169 return ret;
170}
171
172int checkboard(void)
173{
174 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
175
176 if (!do_board_detect())
177 printf("Board: %s rev %s\n", ep->name, ep->version);
178
179 return 0;
180}
181
182#ifdef CONFIG_BOARD_LATE_INIT
183static void setup_board_eeprom_env(void)
184{
185 char *name = "am64x_gpevm";
186
187 if (do_board_detect())
188 goto invalid_eeprom;
189
190 if (board_is_am64x_gpevm())
191 name = "am64x_gpevm";
192 else if (board_is_am64x_skevm())
193 name = "am64x_skevm";
194 else
195 printf("Unidentified board claims %s in eeprom header\n",
196 board_ti_get_name());
197
198invalid_eeprom:
199 set_board_info_env_am6(name);
200}
201
202static void setup_serial(void)
203{
204 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
205 unsigned long board_serial;
206 char *endp;
207 char serial_string[17] = { 0 };
208
209 if (env_get("serial#"))
210 return;
211
Simon Glass3ff49ec2021-07-24 09:03:29 -0600212 board_serial = hextoul(ep->serial, &endp);
Lokesh Vutla01032a42021-05-06 16:44:49 +0530213 if (*endp != '\0') {
214 pr_err("Error: Can't set serial# to %s\n", ep->serial);
215 return;
216 }
217
218 snprintf(serial_string, sizeof(serial_string), "%016lx", board_serial);
219 env_set("serial#", serial_string);
220}
221#endif
222#endif
223
224#ifdef CONFIG_BOARD_LATE_INIT
225int board_late_init(void)
226{
227 if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT)) {
Vignesh Raghavendra3349e212021-05-10 23:44:22 +0530228 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
229
Lokesh Vutla01032a42021-05-06 16:44:49 +0530230 setup_board_eeprom_env();
231 setup_serial();
Vignesh Raghavendra3349e212021-05-10 23:44:22 +0530232 /*
233 * The first MAC address for ethernet a.k.a. ethernet0 comes from
234 * efuse populated via the am654 gigabit eth switch subsystem driver.
235 * All the other ones are populated via EEPROM, hence continue with
236 * an index of 1.
237 */
238 board_ti_am6_set_ethaddr(1, ep->mac_addr_cnt);
Lokesh Vutla01032a42021-05-06 16:44:49 +0530239 }
240
241 return 0;
242}
243#endif
Aswath Govindrajucb962f92021-06-04 22:00:34 +0530244
245#define CTRLMMR_USB0_PHY_CTRL 0x43004008
246#define CORE_VOLTAGE 0x80000000
247
248#ifdef CONFIG_SPL_BOARD_INIT
249void spl_board_init(void)
250{
251 u32 val;
252 /* Set USB PHY core voltage to 0.85V */
253 val = readl(CTRLMMR_USB0_PHY_CTRL);
254 val &= ~(CORE_VOLTAGE);
255 writel(val, CTRLMMR_USB0_PHY_CTRL);
Vignesh Raghavendradfcda512021-12-24 12:55:31 +0530256
257 /* Init DRAM size for R5/A53 SPL */
258 dram_init_banksize();
Aswath Govindrajucb962f92021-06-04 22:00:34 +0530259}
260#endif