blob: 6b8c72a71dab6afb66a39faabdb27d47e103d14f [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) || \
Marek Behún3a7bbd82022-01-14 14:31:45 +010074 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET != 0
Pali Rohárcf97b822021-07-23 11:14:29 +020075#error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET must be set to 0
76#endif
77#endif
78
79/*
80 * When loading U-Boot via SPL from SATA disk, the kwbimage main header is
81 * stored at sector 1. Therefore CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR must be
82 * set to 1. Otherwise U-Boot SPL would not be able to load U-Boot proper.
83 */
Simon Glass081a45a2021-08-08 12:20:17 -060084#ifdef CONFIG_SPL_SATA
Marek Behún3a7bbd82022-01-14 14:31:45 +010085#if !defined(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR) || \
86 !defined(CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR) || CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR != 1
Pali Rohárcf97b822021-07-23 11:14:29 +020087#error CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR must be set to 1
88#endif
89#endif
90
91/* Boot Type - block ID */
92#define IBR_HDR_I2C_ID 0x4D
93#define IBR_HDR_SPI_ID 0x5A
94#define IBR_HDR_NAND_ID 0x8B
95#define IBR_HDR_SATA_ID 0x78
96#define IBR_HDR_PEX_ID 0x9C
97#define IBR_HDR_UART_ID 0x69
98#define IBR_HDR_SDIO_ID 0xAE
99
Pali Rohár0f7df222021-10-22 12:41:10 +0200100/* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
Pali Rohárcf97b822021-07-23 11:14:29 +0200101struct kwbimage_main_hdr_v1 {
Marek Behún031d1722022-01-14 14:31:43 +0100102 u8 blockid; /* 0x0 */
103 u8 flags; /* 0x1 */
104 u16 nandpagesize; /* 0x2-0x3 */
105 u32 blocksize; /* 0x4-0x7 */
106 u8 version; /* 0x8 */
107 u8 headersz_msb; /* 0x9 */
108 u16 headersz_lsb; /* 0xA-0xB */
109 u32 srcaddr; /* 0xC-0xF */
110 u32 destaddr; /* 0x10-0x13 */
111 u32 execaddr; /* 0x14-0x17 */
112 u8 options; /* 0x18 */
113 u8 nandblocksize; /* 0x19 */
114 u8 nandbadblklocation; /* 0x1A */
115 u8 reserved4; /* 0x1B */
116 u16 reserved5; /* 0x1C-0x1D */
117 u8 ext; /* 0x1E */
118 u8 checksum; /* 0x1F */
Pali Rohárcf97b822021-07-23 11:14:29 +0200119} __packed;
120
Simon Glassb58bfe02021-08-08 12:20:09 -0600121#ifdef CONFIG_SPL_MMC
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100122u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Pali Rohárcf97b822021-07-23 11:14:29 +0200123{
Pali Roháraa6244e2023-01-09 00:52:09 +0100124 return IS_SD(mmc) ? MMCSD_MODE_RAW : MMCSD_MODE_EMMCBOOT;
125}
126unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
127 unsigned long raw_sect)
128{
129 return IS_SD(mmc) ? 1 : 0;
Pali Rohárcf97b822021-07-23 11:14:29 +0200130}
131#endif
132
Pali Rohár82420562022-01-14 14:31:41 +0100133static u32 checksum32(void *start, u32 len)
134{
135 u32 csum = 0;
136 u32 *p = start;
137
138 while (len > 0) {
139 csum += *p++;
140 len -= sizeof(u32);
141 };
142
143 return csum;
144}
145
146int spl_check_board_image(struct spl_image_info *spl_image,
147 const struct spl_boot_device *bootdev)
148{
149 u32 csum = *(u32 *)(spl_image->load_addr + spl_image->size - 4);
150
151 if (checksum32((void *)spl_image->load_addr,
152 spl_image->size - 4) != csum) {
153 printf("ERROR: Invalid data checksum in kwbimage\n");
154 return -EINVAL;
155 }
156
157 return 0;
158}
159
Pali Rohárcf97b822021-07-23 11:14:29 +0200160int spl_parse_board_header(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100161 const struct spl_boot_device *bootdev,
Pali Rohárcf97b822021-07-23 11:14:29 +0200162 const void *image_header, size_t size)
163{
164 const struct kwbimage_main_hdr_v1 *mhdr = image_header;
165
166 if (size < sizeof(*mhdr)) {
167 /* This should be compile time assert */
168 printf("FATAL ERROR: Image header size is too small\n");
169 hang();
170 }
171
172 /*
173 * Very basic check for image validity. We cannot check mhdr->checksum
174 * as it is calculated also from variable length extended headers
175 * (including SPL content) which is not included in U-Boot image_header.
176 */
177 if (mhdr->version != 1 ||
Pali Rohára157e122022-01-14 14:31:39 +0100178 ((mhdr->headersz_msb << 16) | mhdr->headersz_lsb) < sizeof(*mhdr)) {
179 printf("ERROR: Invalid kwbimage v1\n");
180 return -EINVAL;
181 }
182
Marek Behún556eab62022-01-14 14:31:44 +0100183 if (IS_ENABLED(CONFIG_SPL_SPI_FLASH_SUPPORT) &&
184 bootdev->boot_device == BOOT_DEVICE_SPI &&
Pali Rohára157e122022-01-14 14:31:39 +0100185 mhdr->blockid != IBR_HDR_SPI_ID) {
186 printf("ERROR: Wrong blockid (0x%x) in SPI kwbimage\n",
187 mhdr->blockid);
188 return -EINVAL;
189 }
Pali Rohára157e122022-01-14 14:31:39 +0100190
Marek Behún556eab62022-01-14 14:31:44 +0100191 if (IS_ENABLED(CONFIG_SPL_SATA) &&
192 bootdev->boot_device == BOOT_DEVICE_SATA &&
Pali Rohára157e122022-01-14 14:31:39 +0100193 mhdr->blockid != IBR_HDR_SATA_ID) {
194 printf("ERROR: Wrong blockid (0x%x) in SATA kwbimage\n",
195 mhdr->blockid);
196 return -EINVAL;
197 }
Pali Rohára157e122022-01-14 14:31:39 +0100198
Marek Behún556eab62022-01-14 14:31:44 +0100199 if (IS_ENABLED(CONFIG_SPL_MMC) &&
Pali Rohár78097252023-01-08 13:27:07 +0100200 (bootdev->boot_device == BOOT_DEVICE_MMC1) &&
Pali Rohára157e122022-01-14 14:31:39 +0100201 mhdr->blockid != IBR_HDR_SDIO_ID) {
202 printf("ERROR: Wrong blockid (0x%x) in SDIO kwbimage\n",
203 mhdr->blockid);
Pali Rohárcf97b822021-07-23 11:14:29 +0200204 return -EINVAL;
205 }
206
207 spl_image->offset = mhdr->srcaddr;
208
Pali Rohárcf97b822021-07-23 11:14:29 +0200209 /*
210 * For SATA srcaddr is specified in number of sectors.
Pali Rohárbb46cea2023-01-21 13:47:45 +0100211 * This expects that sector size is 512 bytes.
Pali Rohárcf97b822021-07-23 11:14:29 +0200212 */
Pali Rohárbb46cea2023-01-21 13:47:45 +0100213 if (IS_ENABLED(CONFIG_SPL_SATA) && mhdr->blockid == IBR_HDR_SATA_ID)
Pali Rohárcf97b822021-07-23 11:14:29 +0200214 spl_image->offset *= 512;
Pali Rohárcf97b822021-07-23 11:14:29 +0200215
Pali Roháreb7e1fc2022-01-14 14:31:37 +0100216 if (spl_image->offset % 4 != 0) {
217 printf("ERROR: Wrong srcaddr (0x%08x) in kwbimage\n",
218 spl_image->offset);
219 return -EINVAL;
220 }
221
222 if (mhdr->blocksize <= 4 || mhdr->blocksize % 4 != 0) {
223 printf("ERROR: Wrong blocksize (0x%08x) in kwbimage\n",
224 mhdr->blocksize);
225 return -EINVAL;
226 }
227
Pali Rohárcf97b822021-07-23 11:14:29 +0200228 spl_image->size = mhdr->blocksize;
229 spl_image->entry_point = mhdr->execaddr;
230 spl_image->load_addr = mhdr->destaddr;
231 spl_image->os = IH_OS_U_BOOT;
232 spl_image->name = "U-Boot";
233
234 return 0;
235}
236
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100237u32 spl_boot_device(void)
238{
Pali Rohárda1be862021-07-23 11:14:26 +0200239 u32 boot_device = get_boot_device();
240
Pali Rohárcf97b822021-07-23 11:14:29 +0200241 switch (boot_device) {
Pali Rohárda1be862021-07-23 11:14:26 +0200242 /*
243 * Return to the BootROM to continue the Marvell xmodem
244 * UART boot protocol. As initiated by the kwboot tool.
245 *
246 * This can only be done by the BootROM since the beginning
247 * of the image is already read and interpreted by the BootROM.
248 * SPL has no chance to receive this information. So we
249 * need to return to the BootROM to enable this xmodem
250 * UART download. Use SPL infrastructure to return to BootROM.
Pali Rohárda1be862021-07-23 11:14:26 +0200251 */
Pali Rohárda1be862021-07-23 11:14:26 +0200252 case BOOT_DEVICE_UART:
Pali Rohárda1be862021-07-23 11:14:26 +0200253 return BOOT_DEVICE_BOOTROM;
Pali Rohárcf97b822021-07-23 11:14:29 +0200254
255 /*
256 * If SPL is compiled with chosen boot_device support
257 * then use SPL driver for loading U-Boot proper.
258 */
Simon Glassb58bfe02021-08-08 12:20:09 -0600259#ifdef CONFIG_SPL_MMC
Pali Rohárcf97b822021-07-23 11:14:29 +0200260 case BOOT_DEVICE_MMC1:
261 return BOOT_DEVICE_MMC1;
262#endif
Simon Glass081a45a2021-08-08 12:20:17 -0600263#ifdef CONFIG_SPL_SATA
Pali Rohár90a88982021-10-29 14:09:48 +0200264 case BOOT_DEVICE_SATA:
265 return BOOT_DEVICE_SATA;
Pali Rohárcf97b822021-07-23 11:14:29 +0200266#endif
267#ifdef CONFIG_SPL_SPI_FLASH_SUPPORT
268 case BOOT_DEVICE_SPI:
269 return BOOT_DEVICE_SPI;
270#endif
271
272 /*
273 * If SPL is not compiled with chosen boot_device support
274 * then return to the BootROM. BootROM supports loading
275 * U-Boot proper from any valid boot_device present in SAR
276 * register.
277 */
Pali Rohárda1be862021-07-23 11:14:26 +0200278 default:
Pali Rohárcf97b822021-07-23 11:14:29 +0200279 return BOOT_DEVICE_BOOTROM;
Pali Rohárda1be862021-07-23 11:14:26 +0200280 }
Stefan Roese63962132015-07-20 11:20:36 +0200281}
282
Pali Rohárd0f064d2022-08-02 11:55:19 +0200283void board_boot_order(u32 *spl_boot_list)
284{
285 spl_boot_list[0] = spl_boot_device();
286 if (spl_boot_list[0] != BOOT_DEVICE_BOOTROM)
287 spl_boot_list[1] = BOOT_DEVICE_BOOTROM;
288}
289
Marek Behúnee76b4a2021-08-16 15:19:37 +0200290#else
291
292u32 spl_boot_device(void)
293{
294 return BOOT_DEVICE_BOOTROM;
295}
296
297#endif
298
Pali Rohára3a38e52021-07-23 11:14:25 +0200299int board_return_to_bootrom(struct spl_image_info *spl_image,
300 struct spl_boot_device *bootdev)
301{
Tom Rinia276bc72022-05-24 09:57:18 -0400302 u32 *regs = *(u32 **)(CONFIG_SPL_STACK + 4);
Pali Rohára3a38e52021-07-23 11:14:25 +0200303
304 printf("Returning to BootROM (return address 0x%08x)...\n", regs[13]);
305 return_to_bootrom();
306
307 /* NOTREACHED - return_to_bootrom() does not return */
308 hang();
309}
310
Stefan Roesee463bf32015-01-19 11:33:42 +0100311void board_init_f(ulong dummy)
312{
Stefan Roese83097cf2015-11-25 07:37:00 +0100313 int ret;
314
Stefan Roesed7f2c122015-04-17 18:13:06 +0200315 /*
316 * Pin muxing needs to be done before UART output, since
317 * on A38x the UART pins need some re-muxing for output
318 * to work.
319 */
320 board_early_init_f();
321
Stefan Roese85bddff2019-04-12 16:42:28 +0200322 /*
323 * Use special translation offset for SPL. This needs to be
324 * configured *before* spl_init() is called as this function
325 * calls dm_init() which calls the bind functions of the
326 * device drivers. Here the base address needs to be configured
327 * (translated) correctly.
328 */
329 gd->translation_offset = 0xd0000000 - 0xf1000000;
330
Stefan Roese83097cf2015-11-25 07:37:00 +0100331 ret = spl_init();
332 if (ret) {
Pali Rohár6e863512021-12-17 18:31:14 +0100333 printf("spl_init() failed: %d\n", ret);
Stefan Roese83097cf2015-11-25 07:37:00 +0100334 hang();
335 }
336
Stefan Roesee463bf32015-01-19 11:33:42 +0100337 preloader_console_init();
338
Stefan Roese479f9af2016-02-10 07:23:00 +0100339 /* Armada 375 does not support SerDes and DDR3 init yet */
340#if !defined(CONFIG_ARMADA_375)
Stefan Roesee463bf32015-01-19 11:33:42 +0100341 /* First init the serdes PHY's */
342 serdes_phy_config();
343
344 /* Setup DDR */
Pali Rohárc87978a2021-08-09 17:44:35 +0200345 ret = ddr3_init();
346 if (ret) {
Pali Rohár6e863512021-12-17 18:31:14 +0100347 printf("ddr3_init() failed: %d\n", ret);
Marek Behún90555af2022-02-17 13:54:42 +0100348 if (IS_ENABLED(CONFIG_DDR_RESET_ON_TRAINING_FAILURE) &&
349 get_boot_device() != BOOT_DEVICE_UART)
350 reset_cpu();
351 else
352 hang();
Pali Rohárc87978a2021-08-09 17:44:35 +0200353 }
Stefan Roese479f9af2016-02-10 07:23:00 +0100354#endif
Stefan Roesee463bf32015-01-19 11:33:42 +0100355
Baruch Siach056e1072019-07-10 18:23:04 +0300356 /* Initialize Auto Voltage Scaling */
357 mv_avs_init();
358
Chris Packham3667bec2020-02-26 19:53:50 +1300359 /* Update read timing control for PCIe */
360 mv_rtc_config();
Stefan Roesee463bf32015-01-19 11:33:42 +0100361}