blob: 273ecb8bd6cff2727d2d8288dfba67ac17a2ac21 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roeseb0f80b92015-01-19 11:33:42 +01002/*
Stefan Roesea5f88872016-01-07 14:09:09 +01003 * Copyright (C) 2014-2016 Stefan Roese <sr@denx.de>
Stefan Roeseb0f80b92015-01-19 11:33:42 +01004 */
5
6#include <common.h>
Stefan Roese64512232015-11-25 07:37:00 +01007#include <dm.h>
Stefan Roese64512232015-11-25 07:37:00 +01008#include <fdtdec.h>
Simon Glassdb41d652019-12-28 10:45:07 -07009#include <hang.h>
Pali Rohár2226ca12021-07-23 11:14:29 +020010#include <image.h>
Simon Glass691d7192020-05-10 11:40:02 -060011#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Stefan Roeseb0f80b92015-01-19 11:33:42 +010013#include <spl.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Stefan Roeseb0f80b92015-01-19 11:33:42 +010015#include <asm/io.h>
16#include <asm/arch/cpu.h>
17#include <asm/arch/soc.h>
18
Simon Glass103c5f12021-08-08 12:20:09 -060019#if defined(CONFIG_SPL_SPI_FLASH_SUPPORT) || defined(CONFIG_SPL_MMC) || \
Simon Glassf7560372021-08-08 12:20:17 -060020 defined(CONFIG_SPL_SATA)
Pali Rohár2226ca12021-07-23 11:14:29 +020021
22/*
23 * When loading U-Boot via SPL from SPI NOR, CONFIG_SYS_SPI_U_BOOT_OFFS must
24 * point to the offset of kwbimage main header which is always at offset zero
25 * (defined by BootROM). Therefore other values of CONFIG_SYS_SPI_U_BOOT_OFFS
26 * makes U-Boot non-bootable.
27 */
28#ifdef CONFIG_SPL_SPI_FLASH_SUPPORT
29#if defined(CONFIG_SYS_SPI_U_BOOT_OFFS) && CONFIG_SYS_SPI_U_BOOT_OFFS != 0
30#error CONFIG_SYS_SPI_U_BOOT_OFFS must be set to 0
31#endif
32#endif
33
34/*
35 * When loading U-Boot via SPL from eMMC (in Marvell terminology SDIO), the
36 * kwbimage main header is stored at sector 0. U-Boot SPL needs to parse this
37 * header and figure out at which sector the U-Boot proper binary is stored.
38 * Partition booting is therefore not supported and CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
39 * and CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET need to point to the
40 * kwbimage main header.
41 */
Simon Glass103c5f12021-08-08 12:20:09 -060042#ifdef CONFIG_SPL_MMC
Pali Rohár2226ca12021-07-23 11:14:29 +020043#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
44#error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION is unsupported
45#endif
46#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) && CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR != 0
47#error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR must be set to 0
48#endif
Marek Behún0d582a42022-01-14 14:31:45 +010049#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET) && \
50 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET != 0
Pali Rohár2226ca12021-07-23 11:14:29 +020051#error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET must be set to 0
52#endif
53#endif
54
55/*
56 * When loading U-Boot via SPL from SATA disk, the kwbimage main header is
57 * stored at sector 1. Therefore CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR must be
58 * set to 1. Otherwise U-Boot SPL would not be able to load U-Boot proper.
59 */
Simon Glassf7560372021-08-08 12:20:17 -060060#ifdef CONFIG_SPL_SATA
Marek Behún0d582a42022-01-14 14:31:45 +010061#if !defined(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR) || \
62 !defined(CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR) || CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR != 1
Pali Rohár2226ca12021-07-23 11:14:29 +020063#error CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR must be set to 1
64#endif
65#endif
66
67/* Boot Type - block ID */
68#define IBR_HDR_I2C_ID 0x4D
69#define IBR_HDR_SPI_ID 0x5A
70#define IBR_HDR_NAND_ID 0x8B
71#define IBR_HDR_SATA_ID 0x78
72#define IBR_HDR_PEX_ID 0x9C
73#define IBR_HDR_UART_ID 0x69
74#define IBR_HDR_SDIO_ID 0xAE
75
Pali Rohár7af368f2021-10-22 12:41:10 +020076/* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
Pali Rohár2226ca12021-07-23 11:14:29 +020077struct kwbimage_main_hdr_v1 {
Marek Behúnc8945662022-01-14 14:31:43 +010078 u8 blockid; /* 0x0 */
79 u8 flags; /* 0x1 */
80 u16 nandpagesize; /* 0x2-0x3 */
81 u32 blocksize; /* 0x4-0x7 */
82 u8 version; /* 0x8 */
83 u8 headersz_msb; /* 0x9 */
84 u16 headersz_lsb; /* 0xA-0xB */
85 u32 srcaddr; /* 0xC-0xF */
86 u32 destaddr; /* 0x10-0x13 */
87 u32 execaddr; /* 0x14-0x17 */
88 u8 options; /* 0x18 */
89 u8 nandblocksize; /* 0x19 */
90 u8 nandbadblklocation; /* 0x1A */
91 u8 reserved4; /* 0x1B */
92 u16 reserved5; /* 0x1C-0x1D */
93 u8 ext; /* 0x1E */
94 u8 checksum; /* 0x1F */
Pali Rohár2226ca12021-07-23 11:14:29 +020095} __packed;
96
Simon Glass103c5f12021-08-08 12:20:09 -060097#ifdef CONFIG_SPL_MMC
Pali Rohár2226ca12021-07-23 11:14:29 +020098u32 spl_mmc_boot_mode(const u32 boot_device)
99{
100 return MMCSD_MODE_RAW;
101}
102#endif
103
Pali Rohár402e84e2022-01-14 14:31:41 +0100104static u32 checksum32(void *start, u32 len)
105{
106 u32 csum = 0;
107 u32 *p = start;
108
109 while (len > 0) {
110 csum += *p++;
111 len -= sizeof(u32);
112 };
113
114 return csum;
115}
116
117int spl_check_board_image(struct spl_image_info *spl_image,
118 const struct spl_boot_device *bootdev)
119{
120 u32 csum = *(u32 *)(spl_image->load_addr + spl_image->size - 4);
121
122 if (checksum32((void *)spl_image->load_addr,
123 spl_image->size - 4) != csum) {
124 printf("ERROR: Invalid data checksum in kwbimage\n");
125 return -EINVAL;
126 }
127
128 return 0;
129}
130
Pali Rohár2226ca12021-07-23 11:14:29 +0200131int spl_parse_board_header(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100132 const struct spl_boot_device *bootdev,
Pali Rohár2226ca12021-07-23 11:14:29 +0200133 const void *image_header, size_t size)
134{
135 const struct kwbimage_main_hdr_v1 *mhdr = image_header;
136
137 if (size < sizeof(*mhdr)) {
138 /* This should be compile time assert */
139 printf("FATAL ERROR: Image header size is too small\n");
140 hang();
141 }
142
143 /*
144 * Very basic check for image validity. We cannot check mhdr->checksum
145 * as it is calculated also from variable length extended headers
146 * (including SPL content) which is not included in U-Boot image_header.
147 */
148 if (mhdr->version != 1 ||
Pali Rohárf8307032022-01-14 14:31:39 +0100149 ((mhdr->headersz_msb << 16) | mhdr->headersz_lsb) < sizeof(*mhdr)) {
150 printf("ERROR: Invalid kwbimage v1\n");
Pali Rohár2226ca12021-07-23 11:14:29 +0200151 return -EINVAL;
152 }
153
Marek Behún37241ce2022-01-14 14:31:44 +0100154 if (IS_ENABLED(CONFIG_SPL_SPI_FLASH_SUPPORT) &&
155 bootdev->boot_device == BOOT_DEVICE_SPI &&
Pali Rohárf8307032022-01-14 14:31:39 +0100156 mhdr->blockid != IBR_HDR_SPI_ID) {
157 printf("ERROR: Wrong blockid (0x%x) in SPI kwbimage\n",
158 mhdr->blockid);
159 return -EINVAL;
160 }
Pali Rohárf8307032022-01-14 14:31:39 +0100161
Marek Behún37241ce2022-01-14 14:31:44 +0100162 if (IS_ENABLED(CONFIG_SPL_SATA) &&
163 bootdev->boot_device == BOOT_DEVICE_SATA &&
Pali Rohárf8307032022-01-14 14:31:39 +0100164 mhdr->blockid != IBR_HDR_SATA_ID) {
165 printf("ERROR: Wrong blockid (0x%x) in SATA kwbimage\n",
166 mhdr->blockid);
167 return -EINVAL;
168 }
Pali Rohárf8307032022-01-14 14:31:39 +0100169
Marek Behún37241ce2022-01-14 14:31:44 +0100170 if (IS_ENABLED(CONFIG_SPL_MMC) &&
171 (bootdev->boot_device == BOOT_DEVICE_MMC1 ||
Pali Rohárf8307032022-01-14 14:31:39 +0100172 bootdev->boot_device == BOOT_DEVICE_MMC2 ||
173 bootdev->boot_device == BOOT_DEVICE_MMC2_2) &&
174 mhdr->blockid != IBR_HDR_SDIO_ID) {
175 printf("ERROR: Wrong blockid (0x%x) in SDIO kwbimage\n",
176 mhdr->blockid);
177 return -EINVAL;
178 }
Pali Rohárf8307032022-01-14 14:31:39 +0100179
Pali Rohár2226ca12021-07-23 11:14:29 +0200180 spl_image->offset = mhdr->srcaddr;
181
Pali Rohár2226ca12021-07-23 11:14:29 +0200182 /*
183 * For SATA srcaddr is specified in number of sectors.
184 * The main header is must be stored at sector number 1.
185 * This expects that sector size is 512 bytes and recalculates
186 * data offset to bytes relative to the main header.
187 */
Marek Behún37241ce2022-01-14 14:31:44 +0100188 if (IS_ENABLED(CONFIG_SPL_SATA) && mhdr->blockid == IBR_HDR_SATA_ID) {
Pali Rohár2226ca12021-07-23 11:14:29 +0200189 if (spl_image->offset < 1) {
Marek Behún62ee75a2022-01-14 14:31:42 +0100190 printf("ERROR: Wrong srcaddr (0x%08x) in SATA kwbimage\n",
191 spl_image->offset);
Pali Rohár2226ca12021-07-23 11:14:29 +0200192 return -EINVAL;
193 }
194 spl_image->offset -= 1;
195 spl_image->offset *= 512;
196 }
Pali Rohár2226ca12021-07-23 11:14:29 +0200197
Pali Rohár2226ca12021-07-23 11:14:29 +0200198 /*
199 * For SDIO (eMMC) srcaddr is specified in number of sectors.
200 * This expects that sector size is 512 bytes and recalculates
201 * data offset to bytes.
202 */
Marek Behún37241ce2022-01-14 14:31:44 +0100203 if (IS_ENABLED(CONFIG_SPL_MMC) && mhdr->blockid == IBR_HDR_SDIO_ID)
Pali Rohár2226ca12021-07-23 11:14:29 +0200204 spl_image->offset *= 512;
Pali Rohár2226ca12021-07-23 11:14:29 +0200205
Pali Rohár66f87482022-01-14 14:31:37 +0100206 if (spl_image->offset % 4 != 0) {
207 printf("ERROR: Wrong srcaddr (0x%08x) in kwbimage\n",
208 spl_image->offset);
209 return -EINVAL;
210 }
211
212 if (mhdr->blocksize <= 4 || mhdr->blocksize % 4 != 0) {
213 printf("ERROR: Wrong blocksize (0x%08x) in kwbimage\n",
214 mhdr->blocksize);
215 return -EINVAL;
216 }
217
Pali Rohár2226ca12021-07-23 11:14:29 +0200218 spl_image->size = mhdr->blocksize;
219 spl_image->entry_point = mhdr->execaddr;
220 spl_image->load_addr = mhdr->destaddr;
221 spl_image->os = IH_OS_U_BOOT;
222 spl_image->name = "U-Boot";
223
224 return 0;
225}
226
Stefan Roeseb0f80b92015-01-19 11:33:42 +0100227u32 spl_boot_device(void)
228{
Pali Rohárabbf2172021-07-23 11:14:26 +0200229 u32 boot_device = get_boot_device();
230
Pali Rohár2226ca12021-07-23 11:14:29 +0200231 switch (boot_device) {
Pali Rohárabbf2172021-07-23 11:14:26 +0200232 /*
233 * Return to the BootROM to continue the Marvell xmodem
234 * UART boot protocol. As initiated by the kwboot tool.
235 *
236 * This can only be done by the BootROM since the beginning
237 * of the image is already read and interpreted by the BootROM.
238 * SPL has no chance to receive this information. So we
239 * need to return to the BootROM to enable this xmodem
240 * UART download. Use SPL infrastructure to return to BootROM.
Pali Rohárabbf2172021-07-23 11:14:26 +0200241 */
Pali Rohárabbf2172021-07-23 11:14:26 +0200242 case BOOT_DEVICE_UART:
Pali Rohárabbf2172021-07-23 11:14:26 +0200243 return BOOT_DEVICE_BOOTROM;
Pali Rohár2226ca12021-07-23 11:14:29 +0200244
245 /*
246 * If SPL is compiled with chosen boot_device support
247 * then use SPL driver for loading U-Boot proper.
248 */
Simon Glass103c5f12021-08-08 12:20:09 -0600249#ifdef CONFIG_SPL_MMC
Pali Rohár2226ca12021-07-23 11:14:29 +0200250 case BOOT_DEVICE_MMC1:
251 return BOOT_DEVICE_MMC1;
252#endif
Simon Glassf7560372021-08-08 12:20:17 -0600253#ifdef CONFIG_SPL_SATA
Pali Rohárd73db302021-10-29 14:09:48 +0200254 case BOOT_DEVICE_SATA:
255 return BOOT_DEVICE_SATA;
Pali Rohár2226ca12021-07-23 11:14:29 +0200256#endif
257#ifdef CONFIG_SPL_SPI_FLASH_SUPPORT
258 case BOOT_DEVICE_SPI:
259 return BOOT_DEVICE_SPI;
260#endif
261
262 /*
263 * If SPL is not compiled with chosen boot_device support
264 * then return to the BootROM. BootROM supports loading
265 * U-Boot proper from any valid boot_device present in SAR
266 * register.
267 */
Pali Rohárabbf2172021-07-23 11:14:26 +0200268 default:
Pali Rohár2226ca12021-07-23 11:14:29 +0200269 return BOOT_DEVICE_BOOTROM;
Pali Rohárabbf2172021-07-23 11:14:26 +0200270 }
Stefan Roeseb0f80b92015-01-19 11:33:42 +0100271}
272
Marek Behúndc595e32021-08-16 15:19:37 +0200273#else
274
275u32 spl_boot_device(void)
276{
277 return BOOT_DEVICE_BOOTROM;
278}
279
280#endif
281
Pali Rohár9d0225b2021-07-23 11:14:25 +0200282int board_return_to_bootrom(struct spl_image_info *spl_image,
283 struct spl_boot_device *bootdev)
284{
285 u32 *regs = *(u32 **)CONFIG_SPL_BOOTROM_SAVE;
286
287 printf("Returning to BootROM (return address 0x%08x)...\n", regs[13]);
288 return_to_bootrom();
289
290 /* NOTREACHED - return_to_bootrom() does not return */
291 hang();
292}
293
Stefan Roeseb0f80b92015-01-19 11:33:42 +0100294void board_init_f(ulong dummy)
295{
Stefan Roese64512232015-11-25 07:37:00 +0100296 int ret;
297
Stefan Roesee3cccf92015-04-17 18:13:06 +0200298 /*
299 * Pin muxing needs to be done before UART output, since
300 * on A38x the UART pins need some re-muxing for output
301 * to work.
302 */
303 board_early_init_f();
304
Stefan Roesef2100f62019-04-12 16:42:28 +0200305 /*
306 * Use special translation offset for SPL. This needs to be
307 * configured *before* spl_init() is called as this function
308 * calls dm_init() which calls the bind functions of the
309 * device drivers. Here the base address needs to be configured
310 * (translated) correctly.
311 */
312 gd->translation_offset = 0xd0000000 - 0xf1000000;
313
Stefan Roese64512232015-11-25 07:37:00 +0100314 ret = spl_init();
315 if (ret) {
Pali Rohár2e5d0aa2021-12-17 18:31:14 +0100316 printf("spl_init() failed: %d\n", ret);
Stefan Roese64512232015-11-25 07:37:00 +0100317 hang();
318 }
319
Stefan Roeseb0f80b92015-01-19 11:33:42 +0100320 preloader_console_init();
321
Stefan Roeseade741b2015-07-15 15:36:52 +0200322 timer_init();
323
Stefan Roese09e89ab2016-02-10 07:23:00 +0100324 /* Armada 375 does not support SerDes and DDR3 init yet */
325#if !defined(CONFIG_ARMADA_375)
Stefan Roeseb0f80b92015-01-19 11:33:42 +0100326 /* First init the serdes PHY's */
327 serdes_phy_config();
328
329 /* Setup DDR */
Pali Rohár7e1c0d02021-08-09 17:44:35 +0200330 ret = ddr3_init();
331 if (ret) {
Pali Rohár2e5d0aa2021-12-17 18:31:14 +0100332 printf("ddr3_init() failed: %d\n", ret);
Pali Rohár7e1c0d02021-08-09 17:44:35 +0200333 hang();
334 }
Stefan Roese09e89ab2016-02-10 07:23:00 +0100335#endif
Stefan Roeseb0f80b92015-01-19 11:33:42 +0100336
Baruch Siachcc66ebd2019-07-10 18:23:04 +0300337 /* Initialize Auto Voltage Scaling */
338 mv_avs_init();
339
Chris Packhamad91fdf2020-02-26 19:53:50 +1300340 /* Update read timing control for PCIe */
341 mv_rtc_config();
Stefan Roeseb0f80b92015-01-19 11:33:42 +0100342}