blob: 6045eb2baa84f409051eb946e31491c5c83eee17 [file] [log] [blame]
Michal Simek4b066a12018-08-22 14:55:27 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2014 - 2018 Xilinx, Inc.
4 * Michal Simek <michal.simek@xilinx.com>
5 */
6
7#include <common.h>
Simon Glassafb02152019-12-28 10:45:01 -07008#include <cpu_func.h>
Simon Glassed38aef2020-05-10 11:40:03 -06009#include <env.h>
Michal Simek4b066a12018-08-22 14:55:27 +020010#include <fdtdec.h>
Simon Glassa7b51302019-11-14 12:57:46 -070011#include <init.h>
Ashok Reddy Somafb6b3cd2021-02-23 08:07:46 -070012#include <env_internal.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Michal Simek4b066a12018-08-22 14:55:27 +020014#include <malloc.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070015#include <time.h>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Michal Simek4b066a12018-08-22 14:55:27 +020018#include <asm/io.h>
19#include <asm/arch/hardware.h>
Michal Simek21eb5cc2019-04-29 09:39:09 -070020#include <asm/arch/sys_proto.h>
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +053021#include <dm/device.h>
22#include <dm/uclass.h>
Siva Durga Prasad Paladugub7398972019-08-05 15:54:59 +053023#include <versalpl.h>
Michal Simek705d44a2020-03-31 12:39:37 +020024#include "../common/board.h"
Michal Simek4b066a12018-08-22 14:55:27 +020025
26DECLARE_GLOBAL_DATA_PTR;
27
Siva Durga Prasad Paladugub7398972019-08-05 15:54:59 +053028#if defined(CONFIG_FPGA_VERSALPL)
29static xilinx_desc versalpl = XILINX_VERSAL_DESC;
30#endif
31
Michal Simek4b066a12018-08-22 14:55:27 +020032int board_init(void)
33{
34 printf("EL Level:\tEL%d\n", current_el());
35
Siva Durga Prasad Paladugub7398972019-08-05 15:54:59 +053036#if defined(CONFIG_FPGA_VERSALPL)
37 fpga_init();
38 fpga_add(fpga_xilinx, &versalpl);
39#endif
40
Michal Simek394ee242020-08-03 13:01:45 +020041 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM))
42 xilinx_read_eeprom();
43
Michal Simek4b066a12018-08-22 14:55:27 +020044 return 0;
45}
46
47int board_early_init_r(void)
48{
Michal Simek19f6c972019-01-28 11:08:00 +010049 u32 val;
Michal Simek4b066a12018-08-22 14:55:27 +020050
Michal Simek19f6c972019-01-28 11:08:00 +010051 if (current_el() != 3)
52 return 0;
Michal Simek4b066a12018-08-22 14:55:27 +020053
Michal Simekf56f7d12019-01-28 11:12:41 +010054 debug("iou_switch ctrl div0 %x\n",
55 readl(&crlapb_base->iou_switch_ctrl));
56
Michal Simek19f6c972019-01-28 11:08:00 +010057 writel(IOU_SWITCH_CTRL_CLKACT_BIT |
Michal Simekf56f7d12019-01-28 11:12:41 +010058 (CONFIG_IOU_SWITCH_DIVISOR0 << IOU_SWITCH_CTRL_DIVISOR0_SHIFT),
Michal Simek19f6c972019-01-28 11:08:00 +010059 &crlapb_base->iou_switch_ctrl);
Michal Simek4b066a12018-08-22 14:55:27 +020060
Michal Simek19f6c972019-01-28 11:08:00 +010061 /* Global timer init - Program time stamp reference clk */
62 val = readl(&crlapb_base->timestamp_ref_ctrl);
63 val |= CRL_APB_TIMESTAMP_REF_CTRL_CLKACT_BIT;
64 writel(val, &crlapb_base->timestamp_ref_ctrl);
Michal Simek4b066a12018-08-22 14:55:27 +020065
Michal Simek19f6c972019-01-28 11:08:00 +010066 debug("ref ctrl 0x%x\n",
67 readl(&crlapb_base->timestamp_ref_ctrl));
Michal Simek4b066a12018-08-22 14:55:27 +020068
Michal Simek19f6c972019-01-28 11:08:00 +010069 /* Clear reset of timestamp reg */
70 writel(0, &crlapb_base->rst_timestamp);
Michal Simek4b066a12018-08-22 14:55:27 +020071
Michal Simek19f6c972019-01-28 11:08:00 +010072 /*
73 * Program freq register in System counter and
74 * enable system counter.
75 */
76 writel(COUNTER_FREQUENCY,
77 &iou_scntr_secure->base_frequency_id_register);
Michal Simek4b066a12018-08-22 14:55:27 +020078
Michal Simek19f6c972019-01-28 11:08:00 +010079 debug("counter val 0x%x\n",
80 readl(&iou_scntr_secure->base_frequency_id_register));
81
82 writel(IOU_SCNTRS_CONTROL_EN,
83 &iou_scntr_secure->counter_control_register);
Michal Simek4b066a12018-08-22 14:55:27 +020084
Michal Simek19f6c972019-01-28 11:08:00 +010085 debug("scntrs control 0x%x\n",
86 readl(&iou_scntr_secure->counter_control_register));
87 debug("timer 0x%llx\n", get_ticks());
88 debug("timer 0x%llx\n", get_ticks());
Michal Simek4b066a12018-08-22 14:55:27 +020089
90 return 0;
91}
92
Michal Simek9c91e612020-04-08 11:04:41 +020093static u8 versal_get_bootmode(void)
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +053094{
Michal Simek9c91e612020-04-08 11:04:41 +020095 u8 bootmode;
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +053096 u32 reg = 0;
Michal Simek9c91e612020-04-08 11:04:41 +020097
98 reg = readl(&crp_base->boot_mode_usr);
99
100 if (reg >> BOOT_MODE_ALT_SHIFT)
101 reg >>= BOOT_MODE_ALT_SHIFT;
102
103 bootmode = reg & BOOT_MODES_MASK;
104
105 return bootmode;
106}
107
108int board_late_init(void)
109{
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530110 u8 bootmode;
111 struct udevice *dev;
112 int bootseq = -1;
113 int bootseq_len = 0;
114 int env_targets_len = 0;
115 const char *mode;
116 char *new_targets;
117 char *env_targets;
118
119 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
120 debug("Saved variables - Skipping\n");
121 return 0;
122 }
123
Michal Simekbab07b62020-07-28 12:45:47 +0200124 if (!CONFIG_IS_ENABLED(ENV_VARS_UBOOT_RUNTIME_CONFIG))
125 return 0;
126
Michal Simek9c91e612020-04-08 11:04:41 +0200127 bootmode = versal_get_bootmode();
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530128
129 puts("Bootmode: ");
130 switch (bootmode) {
T Karthik Reddyfacca9a2019-07-11 16:07:57 +0530131 case USB_MODE:
132 puts("USB_MODE\n");
T Karthik Reddy1104faf2021-03-30 23:24:57 -0600133 mode = "usb_dfu0 usb_dfu1";
T Karthik Reddyfacca9a2019-07-11 16:07:57 +0530134 break;
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530135 case JTAG_MODE:
136 puts("JTAG_MODE\n");
Siva Durga Prasad Paladugu00784e02019-06-25 17:13:14 +0530137 mode = "jtag pxe dhcp";
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530138 break;
139 case QSPI_MODE_24BIT:
140 puts("QSPI_MODE_24\n");
141 mode = "xspi0";
142 break;
143 case QSPI_MODE_32BIT:
144 puts("QSPI_MODE_32\n");
145 mode = "xspi0";
146 break;
147 case OSPI_MODE:
148 puts("OSPI_MODE\n");
149 mode = "xspi0";
150 break;
151 case EMMC_MODE:
152 puts("EMMC_MODE\n");
T Karthik Reddybc2b9642019-12-16 04:44:26 -0700153 if (uclass_get_device_by_name(UCLASS_MMC,
154 "sdhci@f1050000", &dev)) {
155 puts("Boot from EMMC but without SD1 enabled!\n");
156 return -1;
157 }
Simon Glass75e534b2020-12-16 21:20:07 -0700158 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
T Karthik Reddybc2b9642019-12-16 04:44:26 -0700159 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700160 bootseq = dev_seq(dev);
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530161 break;
162 case SD_MODE:
163 puts("SD_MODE\n");
164 if (uclass_get_device_by_name(UCLASS_MMC,
165 "sdhci@f1040000", &dev)) {
166 puts("Boot from SD0 but without SD0 enabled!\n");
167 return -1;
168 }
Simon Glass75e534b2020-12-16 21:20:07 -0700169 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530170
171 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700172 bootseq = dev_seq(dev);
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530173 break;
174 case SD1_LSHFT_MODE:
175 puts("LVL_SHFT_");
176 /* fall through */
177 case SD_MODE1:
178 puts("SD_MODE1\n");
179 if (uclass_get_device_by_name(UCLASS_MMC,
180 "sdhci@f1050000", &dev)) {
181 puts("Boot from SD1 but without SD1 enabled!\n");
182 return -1;
183 }
Simon Glass75e534b2020-12-16 21:20:07 -0700184 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530185
186 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700187 bootseq = dev_seq(dev);
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530188 break;
189 default:
190 mode = "";
191 printf("Invalid Boot Mode:0x%x\n", bootmode);
192 break;
193 }
194
195 if (bootseq >= 0) {
196 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
197 debug("Bootseq len: %x\n", bootseq_len);
198 }
199
200 /*
201 * One terminating char + one byte for space between mode
202 * and default boot_targets
203 */
204 env_targets = env_get("boot_targets");
205 if (env_targets)
206 env_targets_len = strlen(env_targets);
207
208 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
209 bootseq_len);
210 if (!new_targets)
211 return -ENOMEM;
212
213 if (bootseq >= 0)
214 sprintf(new_targets, "%s%x %s", mode, bootseq,
215 env_targets ? env_targets : "");
216 else
217 sprintf(new_targets, "%s %s", mode,
218 env_targets ? env_targets : "");
219
220 env_set("boot_targets", new_targets);
221
Michal Simek705d44a2020-03-31 12:39:37 +0200222 return board_late_init_xilinx();
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530223}
224
Michal Simek4b066a12018-08-22 14:55:27 +0200225int dram_init_banksize(void)
226{
Michal Simek21eb5cc2019-04-29 09:39:09 -0700227 int ret;
228
229 ret = fdtdec_setup_memory_banksize();
230 if (ret)
231 return ret;
232
233 mem_map_fill();
Michal Simek4b066a12018-08-22 14:55:27 +0200234
235 return 0;
236}
237
238int dram_init(void)
239{
Michal Simek9134d4c2020-07-10 12:42:09 +0200240 if (fdtdec_setup_mem_size_base_lowest() != 0)
Michal Simek4b066a12018-08-22 14:55:27 +0200241 return -EINVAL;
242
243 return 0;
244}
245
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100246void reset_cpu(void)
Michal Simek4b066a12018-08-22 14:55:27 +0200247{
248}
Ashok Reddy Somafb6b3cd2021-02-23 08:07:46 -0700249
250enum env_location env_get_location(enum env_operation op, int prio)
251{
252 u32 bootmode = versal_get_bootmode();
253
254 if (prio)
255 return ENVL_UNKNOWN;
256
257 switch (bootmode) {
258 case EMMC_MODE:
259 case SD_MODE:
260 case SD1_LSHFT_MODE:
261 case SD_MODE1:
262 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
263 return ENVL_FAT;
264 if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
265 return ENVL_EXT4;
266 return ENVL_UNKNOWN;
267 case OSPI_MODE:
268 case QSPI_MODE_24BIT:
269 case QSPI_MODE_32BIT:
270 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
271 return ENVL_SPI_FLASH;
272 return ENVL_UNKNOWN;
273 case JTAG_MODE:
274 default:
275 return ENVL_NOWHERE;
276 }
277}