blob: 79f8877745b35e1d6fdf84c0f6d329889aa8545c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roesee463bf32015-01-19 11:33:42 +01002/*
Stefan Roese44e7ebd2016-01-07 14:09:09 +01003 * Copyright (C) 2014-2016 Stefan Roese <sr@denx.de>
Stefan Roesee463bf32015-01-19 11:33:42 +01004 */
5
6#include <common.h>
Marek Behún90555af2022-02-17 13:54:42 +01007#include <cpu_func.h>
Stefan Roese83097cf2015-11-25 07:37:00 +01008#include <dm.h>
Stefan Roese83097cf2015-11-25 07:37:00 +01009#include <fdtdec.h>
Simon Glassf11478f2019-12-28 10:45:07 -070010#include <hang.h>
Pali Rohárcf97b822021-07-23 11:14:29 +020011#include <image.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Stefan Roesee463bf32015-01-19 11:33:42 +010014#include <spl.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Stefan Roesee463bf32015-01-19 11:33:42 +010016#include <asm/io.h>
17#include <asm/arch/cpu.h>
18#include <asm/arch/soc.h>
19
Simon Glassb58bfe02021-08-08 12:20:09 -060020#if defined(CONFIG_SPL_SPI_FLASH_SUPPORT) || defined(CONFIG_SPL_MMC) || \
Simon Glass081a45a2021-08-08 12:20:17 -060021 defined(CONFIG_SPL_SATA)
Pali Rohárcf97b822021-07-23 11:14:29 +020022
23/*
24 * When loading U-Boot via SPL from SPI NOR, CONFIG_SYS_SPI_U_BOOT_OFFS must
25 * point to the offset of kwbimage main header which is always at offset zero
26 * (defined by BootROM). Therefore other values of CONFIG_SYS_SPI_U_BOOT_OFFS
27 * makes U-Boot non-bootable.
28 */
29#ifdef CONFIG_SPL_SPI_FLASH_SUPPORT
30#if defined(CONFIG_SYS_SPI_U_BOOT_OFFS) && CONFIG_SYS_SPI_U_BOOT_OFFS != 0
31#error CONFIG_SYS_SPI_U_BOOT_OFFS must be set to 0
32#endif
33#endif
34
35/*
Pali Roháraa6244e2023-01-09 00:52:09 +010036 * When loading U-Boot via SPL from eMMC, the kwbimage main header is stored at
37 * sector 0 and either on HW boot partition or on data partition. Choice of HW
38 * partition depends on what is configured in eMMC EXT_CSC register.
39 * When loading U-Boot via SPL from SD card, the kwbimage main header is stored
40 * at sector 1.
41 * Therefore MBR/GPT partition booting, fixed sector number and fixed eMMC HW
42 * partition number are unsupported due to limitation of Marvell BootROM.
43 * Correct sector number must be determined as runtime in mvebu SPL code based
44 * on the detected boot source. Otherwise U-Boot SPL would not be able to load
45 * U-Boot proper.
46 * Runtime mvebu SPL sector calculation code expects:
47 * - CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET=0
48 * - CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR=0
Pali Rohárcf97b822021-07-23 11:14:29 +020049 */
Simon Glassb58bfe02021-08-08 12:20:09 -060050#ifdef CONFIG_SPL_MMC
Pali Roháraa6244e2023-01-09 00:52:09 +010051#ifdef CONFIG_SYS_MMCSD_FS_BOOT
52#error CONFIG_SYS_MMCSD_FS_BOOT is unsupported
53#endif
54#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
55#error CONFIG_SYS_MMCSD_FS_BOOT_PARTITION is unsupported
56#endif
Pali Rohárcefdc032023-01-08 13:31:41 +010057#ifdef CONFIG_SUPPORT_EMMC_BOOT_OVERRIDE_PART_CONFIG
58#error CONFIG_SUPPORT_EMMC_BOOT_OVERRIDE_PART_CONFIG is unsupported
59#endif
60#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
61#error CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION is unsupported
62#endif
Pali Rohárcf97b822021-07-23 11:14:29 +020063#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
64#error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION is unsupported
65#endif
Pali Roháraa6244e2023-01-09 00:52:09 +010066#ifndef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
67#error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR must be enabled for SD/eMMC boot support
68#endif
69#if !defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) || \
70 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR != 0
Pali Rohárcf97b822021-07-23 11:14:29 +020071#error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR must be set to 0
72#endif
Pali Roháraa6244e2023-01-09 00:52:09 +010073#if !defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET) || \
Josua Mayer929b4b82023-10-25 10:22:53 +020074 (CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET != 0 && \
75 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET != 4096)
76#error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET must be set to either 0 or 4096
Pali Rohárcf97b822021-07-23 11:14:29 +020077#endif
78#endif
79
80/*
81 * When loading U-Boot via SPL from SATA disk, the kwbimage main header is
82 * stored at sector 1. Therefore CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR must be
83 * set to 1. Otherwise U-Boot SPL would not be able to load U-Boot proper.
84 */
Simon Glass081a45a2021-08-08 12:20:17 -060085#ifdef CONFIG_SPL_SATA
Marek Behún3a7bbd82022-01-14 14:31:45 +010086#if !defined(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR) || \
87 !defined(CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR) || CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR != 1
Pali Rohárcf97b822021-07-23 11:14:29 +020088#error CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR must be set to 1
89#endif
90#endif
91
92/* Boot Type - block ID */
93#define IBR_HDR_I2C_ID 0x4D
94#define IBR_HDR_SPI_ID 0x5A
95#define IBR_HDR_NAND_ID 0x8B
96#define IBR_HDR_SATA_ID 0x78
97#define IBR_HDR_PEX_ID 0x9C
98#define IBR_HDR_UART_ID 0x69
99#define IBR_HDR_SDIO_ID 0xAE
100
Pali Rohár0f7df222021-10-22 12:41:10 +0200101/* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
Pali Rohárcf97b822021-07-23 11:14:29 +0200102struct kwbimage_main_hdr_v1 {
Marek Behún031d1722022-01-14 14:31:43 +0100103 u8 blockid; /* 0x0 */
104 u8 flags; /* 0x1 */
105 u16 nandpagesize; /* 0x2-0x3 */
106 u32 blocksize; /* 0x4-0x7 */
107 u8 version; /* 0x8 */
108 u8 headersz_msb; /* 0x9 */
109 u16 headersz_lsb; /* 0xA-0xB */
110 u32 srcaddr; /* 0xC-0xF */
111 u32 destaddr; /* 0x10-0x13 */
112 u32 execaddr; /* 0x14-0x17 */
113 u8 options; /* 0x18 */
114 u8 nandblocksize; /* 0x19 */
115 u8 nandbadblklocation; /* 0x1A */
116 u8 reserved4; /* 0x1B */
117 u16 reserved5; /* 0x1C-0x1D */
118 u8 ext; /* 0x1E */
119 u8 checksum; /* 0x1F */
Pali Rohárcf97b822021-07-23 11:14:29 +0200120} __packed;
121
Simon Glassb58bfe02021-08-08 12:20:09 -0600122#ifdef CONFIG_SPL_MMC
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100123u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Pali Rohárcf97b822021-07-23 11:14:29 +0200124{
Pali Roháraa6244e2023-01-09 00:52:09 +0100125 return IS_SD(mmc) ? MMCSD_MODE_RAW : MMCSD_MODE_EMMCBOOT;
126}
Marek Vasutf9a921e2023-10-16 18:16:12 +0200127unsigned long arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
128 unsigned long raw_sect)
Pali Roháraa6244e2023-01-09 00:52:09 +0100129{
130 return IS_SD(mmc) ? 1 : 0;
Pali Rohárcf97b822021-07-23 11:14:29 +0200131}
132#endif
133
Pali Rohár82420562022-01-14 14:31:41 +0100134static u32 checksum32(void *start, u32 len)
135{
136 u32 csum = 0;
137 u32 *p = start;
138
139 while (len > 0) {
140 csum += *p++;
141 len -= sizeof(u32);
142 };
143
144 return csum;
145}
146
147int spl_check_board_image(struct spl_image_info *spl_image,
148 const struct spl_boot_device *bootdev)
149{
150 u32 csum = *(u32 *)(spl_image->load_addr + spl_image->size - 4);
151
152 if (checksum32((void *)spl_image->load_addr,
153 spl_image->size - 4) != csum) {
154 printf("ERROR: Invalid data checksum in kwbimage\n");
155 return -EINVAL;
156 }
157
158 return 0;
159}
160
Pali Rohárcf97b822021-07-23 11:14:29 +0200161int spl_parse_board_header(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100162 const struct spl_boot_device *bootdev,
Pali Rohárcf97b822021-07-23 11:14:29 +0200163 const void *image_header, size_t size)
164{
165 const struct kwbimage_main_hdr_v1 *mhdr = image_header;
166
167 if (size < sizeof(*mhdr)) {
168 /* This should be compile time assert */
169 printf("FATAL ERROR: Image header size is too small\n");
170 hang();
171 }
172
173 /*
174 * Very basic check for image validity. We cannot check mhdr->checksum
175 * as it is calculated also from variable length extended headers
176 * (including SPL content) which is not included in U-Boot image_header.
177 */
178 if (mhdr->version != 1 ||
Pali Rohára157e122022-01-14 14:31:39 +0100179 ((mhdr->headersz_msb << 16) | mhdr->headersz_lsb) < sizeof(*mhdr)) {
180 printf("ERROR: Invalid kwbimage v1\n");
181 return -EINVAL;
182 }
183
Marek Behún556eab62022-01-14 14:31:44 +0100184 if (IS_ENABLED(CONFIG_SPL_SPI_FLASH_SUPPORT) &&
185 bootdev->boot_device == BOOT_DEVICE_SPI &&
Pali Rohára157e122022-01-14 14:31:39 +0100186 mhdr->blockid != IBR_HDR_SPI_ID) {
187 printf("ERROR: Wrong blockid (0x%x) in SPI kwbimage\n",
188 mhdr->blockid);
189 return -EINVAL;
190 }
Pali Rohára157e122022-01-14 14:31:39 +0100191
Marek Behún556eab62022-01-14 14:31:44 +0100192 if (IS_ENABLED(CONFIG_SPL_SATA) &&
193 bootdev->boot_device == BOOT_DEVICE_SATA &&
Pali Rohára157e122022-01-14 14:31:39 +0100194 mhdr->blockid != IBR_HDR_SATA_ID) {
195 printf("ERROR: Wrong blockid (0x%x) in SATA kwbimage\n",
196 mhdr->blockid);
197 return -EINVAL;
198 }
Pali Rohára157e122022-01-14 14:31:39 +0100199
Marek Behún556eab62022-01-14 14:31:44 +0100200 if (IS_ENABLED(CONFIG_SPL_MMC) &&
Pali Rohár78097252023-01-08 13:27:07 +0100201 (bootdev->boot_device == BOOT_DEVICE_MMC1) &&
Pali Rohára157e122022-01-14 14:31:39 +0100202 mhdr->blockid != IBR_HDR_SDIO_ID) {
203 printf("ERROR: Wrong blockid (0x%x) in SDIO kwbimage\n",
204 mhdr->blockid);
Pali Rohárcf97b822021-07-23 11:14:29 +0200205 return -EINVAL;
206 }
207
208 spl_image->offset = mhdr->srcaddr;
209
Pali Rohárcf97b822021-07-23 11:14:29 +0200210 /*
211 * For SATA srcaddr is specified in number of sectors.
Pali Rohárb4c49822023-03-29 21:25:52 +0200212 * Retrieve block size of the first SCSI device (same
213 * code used by the spl_sata_load_image_raw() function)
214 * or fallback to default sector size of 512 bytes.
Pali Rohárcf97b822021-07-23 11:14:29 +0200215 */
Pali Rohárb4c49822023-03-29 21:25:52 +0200216 if (IS_ENABLED(CONFIG_SPL_SATA) && mhdr->blockid == IBR_HDR_SATA_ID) {
217 struct blk_desc *blk_dev = blk_get_devnum_by_uclass_id(UCLASS_SCSI, 0);
218 unsigned long blksz = blk_dev ? blk_dev->blksz : 512;
219 spl_image->offset *= blksz;
220 }
Pali Rohárcf97b822021-07-23 11:14:29 +0200221
Pali Roháreb7e1fc2022-01-14 14:31:37 +0100222 if (spl_image->offset % 4 != 0) {
223 printf("ERROR: Wrong srcaddr (0x%08x) in kwbimage\n",
224 spl_image->offset);
225 return -EINVAL;
226 }
227
228 if (mhdr->blocksize <= 4 || mhdr->blocksize % 4 != 0) {
229 printf("ERROR: Wrong blocksize (0x%08x) in kwbimage\n",
230 mhdr->blocksize);
231 return -EINVAL;
232 }
233
Pali Rohárcf97b822021-07-23 11:14:29 +0200234 spl_image->size = mhdr->blocksize;
235 spl_image->entry_point = mhdr->execaddr;
236 spl_image->load_addr = mhdr->destaddr;
237 spl_image->os = IH_OS_U_BOOT;
238 spl_image->name = "U-Boot";
239
240 return 0;
241}
242
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100243u32 spl_boot_device(void)
244{
Pali Rohárda1be862021-07-23 11:14:26 +0200245 u32 boot_device = get_boot_device();
246
Pali Rohárcf97b822021-07-23 11:14:29 +0200247 switch (boot_device) {
Pali Rohárda1be862021-07-23 11:14:26 +0200248 /*
249 * Return to the BootROM to continue the Marvell xmodem
250 * UART boot protocol. As initiated by the kwboot tool.
251 *
252 * This can only be done by the BootROM since the beginning
253 * of the image is already read and interpreted by the BootROM.
254 * SPL has no chance to receive this information. So we
255 * need to return to the BootROM to enable this xmodem
256 * UART download. Use SPL infrastructure to return to BootROM.
Pali Rohárda1be862021-07-23 11:14:26 +0200257 */
Pali Rohárda1be862021-07-23 11:14:26 +0200258 case BOOT_DEVICE_UART:
Pali Rohárda1be862021-07-23 11:14:26 +0200259 return BOOT_DEVICE_BOOTROM;
Pali Rohárcf97b822021-07-23 11:14:29 +0200260
261 /*
262 * If SPL is compiled with chosen boot_device support
263 * then use SPL driver for loading U-Boot proper.
264 */
Simon Glassb58bfe02021-08-08 12:20:09 -0600265#ifdef CONFIG_SPL_MMC
Pali Rohárcf97b822021-07-23 11:14:29 +0200266 case BOOT_DEVICE_MMC1:
267 return BOOT_DEVICE_MMC1;
268#endif
Simon Glass081a45a2021-08-08 12:20:17 -0600269#ifdef CONFIG_SPL_SATA
Pali Rohár90a88982021-10-29 14:09:48 +0200270 case BOOT_DEVICE_SATA:
271 return BOOT_DEVICE_SATA;
Pali Rohárcf97b822021-07-23 11:14:29 +0200272#endif
273#ifdef CONFIG_SPL_SPI_FLASH_SUPPORT
274 case BOOT_DEVICE_SPI:
275 return BOOT_DEVICE_SPI;
276#endif
277
278 /*
279 * If SPL is not compiled with chosen boot_device support
280 * then return to the BootROM. BootROM supports loading
281 * U-Boot proper from any valid boot_device present in SAR
282 * register.
283 */
Pali Rohárda1be862021-07-23 11:14:26 +0200284 default:
Pali Rohárcf97b822021-07-23 11:14:29 +0200285 return BOOT_DEVICE_BOOTROM;
Pali Rohárda1be862021-07-23 11:14:26 +0200286 }
Stefan Roese63962132015-07-20 11:20:36 +0200287}
288
Pali Rohárd0f064d2022-08-02 11:55:19 +0200289void board_boot_order(u32 *spl_boot_list)
290{
291 spl_boot_list[0] = spl_boot_device();
292 if (spl_boot_list[0] != BOOT_DEVICE_BOOTROM)
293 spl_boot_list[1] = BOOT_DEVICE_BOOTROM;
294}
295
Marek Behúnee76b4a2021-08-16 15:19:37 +0200296#else
297
298u32 spl_boot_device(void)
299{
300 return BOOT_DEVICE_BOOTROM;
301}
302
303#endif
304
Pali Rohára3a38e52021-07-23 11:14:25 +0200305int board_return_to_bootrom(struct spl_image_info *spl_image,
306 struct spl_boot_device *bootdev)
307{
Tom Rinia276bc72022-05-24 09:57:18 -0400308 u32 *regs = *(u32 **)(CONFIG_SPL_STACK + 4);
Pali Rohára3a38e52021-07-23 11:14:25 +0200309
310 printf("Returning to BootROM (return address 0x%08x)...\n", regs[13]);
311 return_to_bootrom();
312
313 /* NOTREACHED - return_to_bootrom() does not return */
314 hang();
315}
316
Stefan Roesee463bf32015-01-19 11:33:42 +0100317void board_init_f(ulong dummy)
318{
Stefan Roese83097cf2015-11-25 07:37:00 +0100319 int ret;
320
Stefan Roesed7f2c122015-04-17 18:13:06 +0200321 /*
322 * Pin muxing needs to be done before UART output, since
323 * on A38x the UART pins need some re-muxing for output
324 * to work.
325 */
326 board_early_init_f();
327
Stefan Roese85bddff2019-04-12 16:42:28 +0200328 /*
329 * Use special translation offset for SPL. This needs to be
330 * configured *before* spl_init() is called as this function
331 * calls dm_init() which calls the bind functions of the
332 * device drivers. Here the base address needs to be configured
333 * (translated) correctly.
334 */
335 gd->translation_offset = 0xd0000000 - 0xf1000000;
336
Stefan Roese83097cf2015-11-25 07:37:00 +0100337 ret = spl_init();
338 if (ret) {
Pali Rohár6e863512021-12-17 18:31:14 +0100339 printf("spl_init() failed: %d\n", ret);
Stefan Roese83097cf2015-11-25 07:37:00 +0100340 hang();
341 }
342
Stefan Roesee463bf32015-01-19 11:33:42 +0100343 preloader_console_init();
344
Stefan Roese479f9af2016-02-10 07:23:00 +0100345 /* Armada 375 does not support SerDes and DDR3 init yet */
346#if !defined(CONFIG_ARMADA_375)
Stefan Roesee463bf32015-01-19 11:33:42 +0100347 /* First init the serdes PHY's */
348 serdes_phy_config();
349
350 /* Setup DDR */
Pali Rohárc87978a2021-08-09 17:44:35 +0200351 ret = ddr3_init();
352 if (ret) {
Pali Rohár6e863512021-12-17 18:31:14 +0100353 printf("ddr3_init() failed: %d\n", ret);
Marek Behún90555af2022-02-17 13:54:42 +0100354 if (IS_ENABLED(CONFIG_DDR_RESET_ON_TRAINING_FAILURE) &&
355 get_boot_device() != BOOT_DEVICE_UART)
356 reset_cpu();
357 else
358 hang();
Pali Rohárc87978a2021-08-09 17:44:35 +0200359 }
Stefan Roese479f9af2016-02-10 07:23:00 +0100360#endif
Stefan Roesee463bf32015-01-19 11:33:42 +0100361
Baruch Siach056e1072019-07-10 18:23:04 +0300362 /* Initialize Auto Voltage Scaling */
363 mv_avs_init();
364
Chris Packham3667bec2020-02-26 19:53:50 +1300365 /* Update read timing control for PCIe */
366 mv_rtc_config();
Stefan Roesee463bf32015-01-19 11:33:42 +0100367}