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