blob: 299e128f7b9da1eeb224a9f4995e60d679831d57 [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,
T Karthik Reddya12445f2021-11-18 12:57:20 +0100154 "mmc@f1050000", &dev) &&
155 uclass_get_device_by_name(UCLASS_MMC,
T Karthik Reddybc2b9642019-12-16 04:44:26 -0700156 "sdhci@f1050000", &dev)) {
157 puts("Boot from EMMC but without SD1 enabled!\n");
158 return -1;
159 }
Simon Glass75e534b2020-12-16 21:20:07 -0700160 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
T Karthik Reddybc2b9642019-12-16 04:44:26 -0700161 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700162 bootseq = dev_seq(dev);
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530163 break;
164 case SD_MODE:
165 puts("SD_MODE\n");
166 if (uclass_get_device_by_name(UCLASS_MMC,
T Karthik Reddya12445f2021-11-18 12:57:20 +0100167 "mmc@f1040000", &dev) &&
168 uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530169 "sdhci@f1040000", &dev)) {
170 puts("Boot from SD0 but without SD0 enabled!\n");
171 return -1;
172 }
Simon Glass75e534b2020-12-16 21:20:07 -0700173 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530174
175 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700176 bootseq = dev_seq(dev);
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530177 break;
178 case SD1_LSHFT_MODE:
179 puts("LVL_SHFT_");
180 /* fall through */
181 case SD_MODE1:
182 puts("SD_MODE1\n");
183 if (uclass_get_device_by_name(UCLASS_MMC,
T Karthik Reddya12445f2021-11-18 12:57:20 +0100184 "mmc@f1050000", &dev) &&
185 uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530186 "sdhci@f1050000", &dev)) {
187 puts("Boot from SD1 but without SD1 enabled!\n");
188 return -1;
189 }
Simon Glass75e534b2020-12-16 21:20:07 -0700190 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530191
192 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700193 bootseq = dev_seq(dev);
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530194 break;
195 default:
196 mode = "";
197 printf("Invalid Boot Mode:0x%x\n", bootmode);
198 break;
199 }
200
201 if (bootseq >= 0) {
202 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
203 debug("Bootseq len: %x\n", bootseq_len);
204 }
205
206 /*
207 * One terminating char + one byte for space between mode
208 * and default boot_targets
209 */
210 env_targets = env_get("boot_targets");
211 if (env_targets)
212 env_targets_len = strlen(env_targets);
213
214 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
215 bootseq_len);
216 if (!new_targets)
217 return -ENOMEM;
218
219 if (bootseq >= 0)
220 sprintf(new_targets, "%s%x %s", mode, bootseq,
221 env_targets ? env_targets : "");
222 else
223 sprintf(new_targets, "%s %s", mode,
224 env_targets ? env_targets : "");
225
226 env_set("boot_targets", new_targets);
227
Michal Simek705d44a2020-03-31 12:39:37 +0200228 return board_late_init_xilinx();
Siva Durga Prasad Paladugu37c2ff82019-01-31 17:28:14 +0530229}
230
Michal Simek4b066a12018-08-22 14:55:27 +0200231int dram_init_banksize(void)
232{
Michal Simek21eb5cc2019-04-29 09:39:09 -0700233 int ret;
234
235 ret = fdtdec_setup_memory_banksize();
236 if (ret)
237 return ret;
238
239 mem_map_fill();
Michal Simek4b066a12018-08-22 14:55:27 +0200240
241 return 0;
242}
243
244int dram_init(void)
245{
Michal Simek9134d4c2020-07-10 12:42:09 +0200246 if (fdtdec_setup_mem_size_base_lowest() != 0)
Michal Simek4b066a12018-08-22 14:55:27 +0200247 return -EINVAL;
248
249 return 0;
250}
251
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100252void reset_cpu(void)
Michal Simek4b066a12018-08-22 14:55:27 +0200253{
254}
Ashok Reddy Somafb6b3cd2021-02-23 08:07:46 -0700255
256enum env_location env_get_location(enum env_operation op, int prio)
257{
258 u32 bootmode = versal_get_bootmode();
259
260 if (prio)
261 return ENVL_UNKNOWN;
262
263 switch (bootmode) {
264 case EMMC_MODE:
265 case SD_MODE:
266 case SD1_LSHFT_MODE:
267 case SD_MODE1:
268 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
269 return ENVL_FAT;
270 if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
271 return ENVL_EXT4;
T Karthik Reddy6f8b2052021-11-24 12:16:55 +0100272 return ENVL_NOWHERE;
Ashok Reddy Somafb6b3cd2021-02-23 08:07:46 -0700273 case OSPI_MODE:
274 case QSPI_MODE_24BIT:
275 case QSPI_MODE_32BIT:
276 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
277 return ENVL_SPI_FLASH;
T Karthik Reddy6f8b2052021-11-24 12:16:55 +0100278 return ENVL_NOWHERE;
Ashok Reddy Somafb6b3cd2021-02-23 08:07:46 -0700279 case JTAG_MODE:
280 default:
281 return ENVL_NOWHERE;
282 }
283}