blob: a080b2b0d2588642ccb24c559145f7437a6700dc [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
Roger Quadros70b26c92023-08-05 11:14:37 +030021#define board_is_am64x_gpevm() (board_ti_k3_is("AM64-GPEVM") || \
22 board_ti_k3_is("AM64-HSEVM"))
Judith Mendezde35c3a2023-04-06 11:49:01 +053023
24#define board_is_am64x_skevm() (board_ti_k3_is("AM64-SKEVM") || \
25 board_ti_k3_is("AM64B-SKEVM"))
Dave Gerlach3dc33f12021-04-23 11:27:42 -050026
27DECLARE_GLOBAL_DATA_PTR;
28
29int board_init(void)
30{
31 return 0;
32}
33
34int dram_init(void)
35{
Dave Gerlach88107fe2022-03-17 12:03:40 -050036 s32 ret;
Dave Gerlach3dc33f12021-04-23 11:27:42 -050037
Dave Gerlach88107fe2022-03-17 12:03:40 -050038 ret = fdtdec_setup_mem_size_base();
39 if (ret)
40 printf("Error setting up mem size and base. %d\n", ret);
41
42 return ret;
Dave Gerlach3dc33f12021-04-23 11:27:42 -050043}
44
45int dram_init_banksize(void)
46{
Dave Gerlach88107fe2022-03-17 12:03:40 -050047 s32 ret;
48
49 ret = fdtdec_setup_memory_banksize();
50 if (ret)
51 printf("Error setting up memory banksize. %d\n", ret);
Dave Gerlach3dc33f12021-04-23 11:27:42 -050052
Dave Gerlach88107fe2022-03-17 12:03:40 -050053 return ret;
Dave Gerlach3dc33f12021-04-23 11:27:42 -050054}
55
56#if defined(CONFIG_SPL_LOAD_FIT)
57int board_fit_config_name_match(const char *name)
58{
Lokesh Vutla8af2b692021-05-06 16:44:51 +053059 bool eeprom_read = board_ti_was_eeprom_read();
60
61 if (!eeprom_read || board_is_am64x_gpevm()) {
62 if (!strcmp(name, "k3-am642-r5-evm") || !strcmp(name, "k3-am642-evm"))
63 return 0;
64 } else if (board_is_am64x_skevm()) {
65 if (!strcmp(name, "k3-am642-r5-sk") || !strcmp(name, "k3-am642-sk"))
66 return 0;
67 }
Dave Gerlach3dc33f12021-04-23 11:27:42 -050068
69 return -1;
70}
71#endif
Lokesh Vutla01032a42021-05-06 16:44:49 +053072
Dave Gerlach10498d12022-03-17 12:03:44 -050073#if defined(CONFIG_SPL_BUILD)
74#if CONFIG_IS_ENABLED(USB_STORAGE)
Aswath Govindrajue910fac2021-08-04 18:42:44 +053075static int fixup_usb_boot(const void *fdt_blob)
76{
77 int ret = 0;
78
79 switch (spl_boot_device()) {
80 case BOOT_DEVICE_USB:
81 /*
82 * If the boot mode is host, fixup the dr_mode to host
83 * before cdns3 bind takes place
84 */
85 ret = fdt_find_and_setprop((void *)fdt_blob,
86 "/bus@f4000/cdns-usb@f900000/usb@f400000",
87 "dr_mode", "host", 5, 0);
88 if (ret)
89 printf("%s: fdt_find_and_setprop() failed:%d\n",
90 __func__, ret);
91 fallthrough;
92 default:
93 break;
94 }
95
96 return ret;
97}
Dave Gerlach10498d12022-03-17 12:03:44 -050098#endif
99
100#if defined(CONFIG_K3_AM64_DDRSS)
101static void fixup_ddr_driver_for_ecc(struct spl_image_info *spl_image)
102{
103 struct udevice *dev;
104 int ret;
105
106 dram_init_banksize();
107
108 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
109 if (ret)
110 panic("Cannot get RAM device for ddr size fixup: %d\n", ret);
111
112 ret = k3_ddrss_ddr_fdt_fixup(dev, spl_image->fdt_addr, gd->bd);
113 if (ret)
114 printf("Error fixing up ddr node for ECC use! %d\n", ret);
115}
116#else
117static void fixup_memory_node(struct spl_image_info *spl_image)
118{
119 u64 start[CONFIG_NR_DRAM_BANKS];
120 u64 size[CONFIG_NR_DRAM_BANKS];
121 int bank;
122 int ret;
123
124 dram_init();
125 dram_init_banksize();
126
127 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
128 start[bank] = gd->bd->bi_dram[bank].start;
129 size[bank] = gd->bd->bi_dram[bank].size;
130 }
131
132 /* dram_init functions use SPL fdt, and we must fixup u-boot fdt */
133 ret = fdt_fixup_memory_banks(spl_image->fdt_addr, start, size, CONFIG_NR_DRAM_BANKS);
134 if (ret)
135 printf("Error fixing up memory node! %d\n", ret);
136}
137#endif
Aswath Govindrajue910fac2021-08-04 18:42:44 +0530138
139void spl_perform_fixups(struct spl_image_info *spl_image)
140{
Dave Gerlach10498d12022-03-17 12:03:44 -0500141#if defined(CONFIG_K3_AM64_DDRSS)
142 fixup_ddr_driver_for_ecc(spl_image);
143#else
144 fixup_memory_node(spl_image);
145#endif
146
147#if CONFIG_IS_ENABLED(USB_STORAGE)
Aswath Govindrajue910fac2021-08-04 18:42:44 +0530148 fixup_usb_boot(spl_image->fdt_addr);
Dave Gerlach10498d12022-03-17 12:03:44 -0500149#endif
Aswath Govindrajue910fac2021-08-04 18:42:44 +0530150}
151#endif
152
Lokesh Vutla01032a42021-05-06 16:44:49 +0530153#ifdef CONFIG_TI_I2C_BOARD_DETECT
154int do_board_detect(void)
155{
156 int ret;
157
158 ret = ti_i2c_eeprom_am6_get_base(CONFIG_EEPROM_BUS_ADDRESS,
159 CONFIG_EEPROM_CHIP_ADDRESS);
160 if (ret) {
161 printf("EEPROM not available at 0x%02x, trying to read at 0x%02x\n",
162 CONFIG_EEPROM_CHIP_ADDRESS, CONFIG_EEPROM_CHIP_ADDRESS + 1);
163 ret = ti_i2c_eeprom_am6_get_base(CONFIG_EEPROM_BUS_ADDRESS,
164 CONFIG_EEPROM_CHIP_ADDRESS + 1);
165 if (ret)
166 pr_err("Reading on-board EEPROM at 0x%02x failed %d\n",
167 CONFIG_EEPROM_CHIP_ADDRESS + 1, ret);
168 }
169
170 return ret;
171}
172
173int checkboard(void)
174{
175 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
176
177 if (!do_board_detect())
178 printf("Board: %s rev %s\n", ep->name, ep->version);
179
180 return 0;
181}
182
183#ifdef CONFIG_BOARD_LATE_INIT
184static void setup_board_eeprom_env(void)
185{
186 char *name = "am64x_gpevm";
187
188 if (do_board_detect())
189 goto invalid_eeprom;
190
191 if (board_is_am64x_gpevm())
192 name = "am64x_gpevm";
193 else if (board_is_am64x_skevm())
194 name = "am64x_skevm";
195 else
196 printf("Unidentified board claims %s in eeprom header\n",
197 board_ti_get_name());
198
199invalid_eeprom:
200 set_board_info_env_am6(name);
201}
202
203static void setup_serial(void)
204{
205 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
206 unsigned long board_serial;
207 char *endp;
208 char serial_string[17] = { 0 };
209
210 if (env_get("serial#"))
211 return;
212
Simon Glass3ff49ec2021-07-24 09:03:29 -0600213 board_serial = hextoul(ep->serial, &endp);
Lokesh Vutla01032a42021-05-06 16:44:49 +0530214 if (*endp != '\0') {
215 pr_err("Error: Can't set serial# to %s\n", ep->serial);
216 return;
217 }
218
219 snprintf(serial_string, sizeof(serial_string), "%016lx", board_serial);
220 env_set("serial#", serial_string);
221}
222#endif
223#endif
224
225#ifdef CONFIG_BOARD_LATE_INIT
226int board_late_init(void)
227{
228 if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT)) {
Vignesh Raghavendra3349e212021-05-10 23:44:22 +0530229 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
230
Lokesh Vutla01032a42021-05-06 16:44:49 +0530231 setup_board_eeprom_env();
232 setup_serial();
Vignesh Raghavendra3349e212021-05-10 23:44:22 +0530233 /*
234 * The first MAC address for ethernet a.k.a. ethernet0 comes from
235 * efuse populated via the am654 gigabit eth switch subsystem driver.
236 * All the other ones are populated via EEPROM, hence continue with
237 * an index of 1.
238 */
239 board_ti_am6_set_ethaddr(1, ep->mac_addr_cnt);
Lokesh Vutla01032a42021-05-06 16:44:49 +0530240 }
241
242 return 0;
243}
244#endif
Aswath Govindrajucb962f92021-06-04 22:00:34 +0530245
246#define CTRLMMR_USB0_PHY_CTRL 0x43004008
247#define CORE_VOLTAGE 0x80000000
248
249#ifdef CONFIG_SPL_BOARD_INIT
250void spl_board_init(void)
251{
252 u32 val;
253 /* Set USB PHY core voltage to 0.85V */
254 val = readl(CTRLMMR_USB0_PHY_CTRL);
255 val &= ~(CORE_VOLTAGE);
256 writel(val, CTRLMMR_USB0_PHY_CTRL);
Vignesh Raghavendradfcda512021-12-24 12:55:31 +0530257
258 /* Init DRAM size for R5/A53 SPL */
259 dram_init_banksize();
Aswath Govindrajucb962f92021-06-04 22:00:34 +0530260}
261#endif