blob: 3b6bc3897090d140c1c6692ba9ae21f64735050e [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>
Stefan Roese83097cf2015-11-25 07:37:00 +01007#include <dm.h>
8#include <debug_uart.h>
9#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
Pali Rohárcf97b822021-07-23 11:14:29 +020020#if defined(CONFIG_SPL_SPI_FLASH_SUPPORT) || defined(CONFIG_SPL_MMC_SUPPORT) || defined(CONFIG_SPL_SATA_SUPPORT)
21
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 */
42#ifdef CONFIG_SPL_MMC_SUPPORT
43#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
49#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET) && CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET != 0
50#error CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET must be set to 0
51#endif
52#endif
53
54/*
55 * When loading U-Boot via SPL from SATA disk, the kwbimage main header is
56 * stored at sector 1. Therefore CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR must be
57 * set to 1. Otherwise U-Boot SPL would not be able to load U-Boot proper.
58 */
59#ifdef CONFIG_SPL_SATA_SUPPORT
60#if !defined(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR) || !defined(CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR) || CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR != 1
61#error CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR must be set to 1
62#endif
63#endif
64
65/* Boot Type - block ID */
66#define IBR_HDR_I2C_ID 0x4D
67#define IBR_HDR_SPI_ID 0x5A
68#define IBR_HDR_NAND_ID 0x8B
69#define IBR_HDR_SATA_ID 0x78
70#define IBR_HDR_PEX_ID 0x9C
71#define IBR_HDR_UART_ID 0x69
72#define IBR_HDR_SDIO_ID 0xAE
73
74/* Structure of the main header, version 1 (Armada 370/38x/XP) */
75struct kwbimage_main_hdr_v1 {
76 uint8_t blockid; /* 0x0 */
77 uint8_t flags; /* 0x1 */
78 uint16_t reserved2; /* 0x2-0x3 */
79 uint32_t blocksize; /* 0x4-0x7 */
80 uint8_t version; /* 0x8 */
81 uint8_t headersz_msb; /* 0x9 */
82 uint16_t headersz_lsb; /* 0xA-0xB */
83 uint32_t srcaddr; /* 0xC-0xF */
84 uint32_t destaddr; /* 0x10-0x13 */
85 uint32_t execaddr; /* 0x14-0x17 */
86 uint8_t options; /* 0x18 */
87 uint8_t nandblocksize; /* 0x19 */
88 uint8_t nandbadblklocation; /* 0x1A */
89 uint8_t reserved4; /* 0x1B */
90 uint16_t reserved5; /* 0x1C-0x1D */
91 uint8_t ext; /* 0x1E */
92 uint8_t checksum; /* 0x1F */
93} __packed;
94
95#ifdef CONFIG_SPL_MMC_SUPPORT
96u32 spl_mmc_boot_mode(const u32 boot_device)
97{
98 return MMCSD_MODE_RAW;
99}
100#endif
101
102int spl_parse_board_header(struct spl_image_info *spl_image,
103 const void *image_header, size_t size)
104{
105 const struct kwbimage_main_hdr_v1 *mhdr = image_header;
106
107 if (size < sizeof(*mhdr)) {
108 /* This should be compile time assert */
109 printf("FATAL ERROR: Image header size is too small\n");
110 hang();
111 }
112
113 /*
114 * Very basic check for image validity. We cannot check mhdr->checksum
115 * as it is calculated also from variable length extended headers
116 * (including SPL content) which is not included in U-Boot image_header.
117 */
118 if (mhdr->version != 1 ||
119 ((mhdr->headersz_msb << 16) | mhdr->headersz_lsb) < sizeof(*mhdr) ||
120 (
121#ifdef CONFIG_SPL_SPI_FLASH_SUPPORT
122 mhdr->blockid != IBR_HDR_SPI_ID &&
123#endif
124#ifdef CONFIG_SPL_SATA_SUPPORT
125 mhdr->blockid != IBR_HDR_SATA_ID &&
126#endif
127#ifdef CONFIG_SPL_MMC_SUPPORT
128 mhdr->blockid != IBR_HDR_SDIO_ID &&
129#endif
130 1
131 )) {
132 printf("ERROR: Not valid SPI/NAND/SATA/SDIO kwbimage v1\n");
133 return -EINVAL;
134 }
135
136 spl_image->offset = mhdr->srcaddr;
137
138#ifdef CONFIG_SPL_SATA_SUPPORT
139 /*
140 * For SATA srcaddr is specified in number of sectors.
141 * The main header is must be stored at sector number 1.
142 * This expects that sector size is 512 bytes and recalculates
143 * data offset to bytes relative to the main header.
144 */
145 if (mhdr->blockid == IBR_HDR_SATA_ID) {
146 if (spl_image->offset < 1) {
147 printf("ERROR: Wrong SATA srcaddr in kwbimage\n");
148 return -EINVAL;
149 }
150 spl_image->offset -= 1;
151 spl_image->offset *= 512;
152 }
153#endif
154
155#ifdef CONFIG_SPL_MMC_SUPPORT
156 /*
157 * For SDIO (eMMC) srcaddr is specified in number of sectors.
158 * This expects that sector size is 512 bytes and recalculates
159 * data offset to bytes.
160 */
161 if (mhdr->blockid == IBR_HDR_SDIO_ID)
162 spl_image->offset *= 512;
163#endif
164
165 spl_image->size = mhdr->blocksize;
166 spl_image->entry_point = mhdr->execaddr;
167 spl_image->load_addr = mhdr->destaddr;
168 spl_image->os = IH_OS_U_BOOT;
169 spl_image->name = "U-Boot";
170
171 return 0;
172}
173
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100174static u32 get_boot_device(void)
Stefan Roesee463bf32015-01-19 11:33:42 +0100175{
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100176 u32 val;
177 u32 boot_device;
178
Stefan Roese04ec0d32016-01-07 14:12:04 +0100179 /*
180 * First check, if UART boot-mode is active. This can only
181 * be done, via the bootrom error register. Here the
182 * MSB marks if the UART mode is active.
183 */
184 val = readl(CONFIG_BOOTROM_ERR_REG);
185 boot_device = (val & BOOTROM_ERR_MODE_MASK) >> BOOTROM_ERR_MODE_OFFS;
186 debug("BOOTROM_REG=0x%08x boot_device=0x%x\n", val, boot_device);
187 if (boot_device == BOOTROM_ERR_MODE_UART)
188 return BOOT_DEVICE_UART;
189
Chris Packham8e932522018-08-17 20:47:42 +1200190#ifdef CONFIG_ARMADA_38X
191 /*
192 * If the bootrom error code contains any other than zeros it's an
193 * error condition and the bootROM has fallen back to UART boot
194 */
195 boot_device = (val & BOOTROM_ERR_CODE_MASK) >> BOOTROM_ERR_CODE_OFFS;
196 if (boot_device)
197 return BOOT_DEVICE_UART;
198#endif
199
Stefan Roese04ec0d32016-01-07 14:12:04 +0100200 /*
201 * Now check the SAR register for the strapped boot-device
202 */
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100203 val = readl(CONFIG_SAR_REG); /* SAR - Sample At Reset */
204 boot_device = (val & BOOT_DEV_SEL_MASK) >> BOOT_DEV_SEL_OFFS;
Stefan Roese04ec0d32016-01-07 14:12:04 +0100205 debug("SAR_REG=0x%08x boot_device=0x%x\n", val, boot_device);
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100206 switch (boot_device) {
Pali Rohárcf97b822021-07-23 11:14:29 +0200207#ifdef BOOT_FROM_NAND
Sean Nyekjaer11d44662017-11-24 14:01:47 +0100208 case BOOT_FROM_NAND:
209 return BOOT_DEVICE_NAND;
210#endif
Pali Rohárcf97b822021-07-23 11:14:29 +0200211#ifdef BOOT_FROM_MMC
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100212 case BOOT_FROM_MMC:
213 case BOOT_FROM_MMC_ALT:
214 return BOOT_DEVICE_MMC1;
Stefan Roese63962132015-07-20 11:20:36 +0200215#endif
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100216 case BOOT_FROM_UART:
Baruch Siache4c0ad62017-09-24 15:50:17 +0300217#ifdef BOOT_FROM_UART_ALT
218 case BOOT_FROM_UART_ALT:
219#endif
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100220 return BOOT_DEVICE_UART;
Baruch Siachb936a272019-05-16 13:03:58 +0300221#ifdef BOOT_FROM_SATA
222 case BOOT_FROM_SATA:
223 case BOOT_FROM_SATA_ALT:
224 return BOOT_DEVICE_SATA;
225#endif
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100226 case BOOT_FROM_SPI:
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100227 return BOOT_DEVICE_SPI;
Pali Rohárcf97b822021-07-23 11:14:29 +0200228 default:
229 return BOOT_DEVICE_BOOTROM;
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100230 };
231}
232
Pali Rohárcf97b822021-07-23 11:14:29 +0200233#else
234
235static u32 get_boot_device(void)
236{
237 return BOOT_DEVICE_BOOTROM;
238}
239
240#endif
241
Stefan Roese44e7ebd2016-01-07 14:09:09 +0100242u32 spl_boot_device(void)
243{
Pali Rohárda1be862021-07-23 11:14:26 +0200244 u32 boot_device = get_boot_device();
245
Pali Rohárcf97b822021-07-23 11:14:29 +0200246 switch (boot_device) {
Pali Rohárda1be862021-07-23 11:14:26 +0200247 /*
248 * Return to the BootROM to continue the Marvell xmodem
249 * UART boot protocol. As initiated by the kwboot tool.
250 *
251 * This can only be done by the BootROM since the beginning
252 * of the image is already read and interpreted by the BootROM.
253 * SPL has no chance to receive this information. So we
254 * need to return to the BootROM to enable this xmodem
255 * UART download. Use SPL infrastructure to return to BootROM.
Pali Rohárda1be862021-07-23 11:14:26 +0200256 */
Pali Rohárda1be862021-07-23 11:14:26 +0200257 case BOOT_DEVICE_UART:
Pali Rohárda1be862021-07-23 11:14:26 +0200258 return BOOT_DEVICE_BOOTROM;
Pali Rohárcf97b822021-07-23 11:14:29 +0200259
260 /*
261 * If SPL is compiled with chosen boot_device support
262 * then use SPL driver for loading U-Boot proper.
263 */
264#ifdef CONFIG_SPL_MMC_SUPPORT
265 case BOOT_DEVICE_MMC1:
266 return BOOT_DEVICE_MMC1;
267#endif
268#ifdef CONFIG_SPL_SATA_SUPPORT
269 case BOOT_FROM_SATA:
270 return BOOT_FROM_SATA;
271#endif
272#ifdef CONFIG_SPL_SPI_FLASH_SUPPORT
273 case BOOT_DEVICE_SPI:
274 return BOOT_DEVICE_SPI;
275#endif
276
277 /*
278 * If SPL is not compiled with chosen boot_device support
279 * then return to the BootROM. BootROM supports loading
280 * U-Boot proper from any valid boot_device present in SAR
281 * register.
282 */
Pali Rohárda1be862021-07-23 11:14:26 +0200283 default:
Pali Rohárcf97b822021-07-23 11:14:29 +0200284 return BOOT_DEVICE_BOOTROM;
Pali Rohárda1be862021-07-23 11:14:26 +0200285 }
Stefan Roese63962132015-07-20 11:20:36 +0200286}
287
Pali Rohára3a38e52021-07-23 11:14:25 +0200288int board_return_to_bootrom(struct spl_image_info *spl_image,
289 struct spl_boot_device *bootdev)
290{
291 u32 *regs = *(u32 **)CONFIG_SPL_BOOTROM_SAVE;
292
293 printf("Returning to BootROM (return address 0x%08x)...\n", regs[13]);
294 return_to_bootrom();
295
296 /* NOTREACHED - return_to_bootrom() does not return */
297 hang();
298}
299
Stefan Roesee463bf32015-01-19 11:33:42 +0100300void board_init_f(ulong dummy)
301{
Stefan Roese83097cf2015-11-25 07:37:00 +0100302 int ret;
303
Stefan Roesed7f2c122015-04-17 18:13:06 +0200304 /*
305 * Pin muxing needs to be done before UART output, since
306 * on A38x the UART pins need some re-muxing for output
307 * to work.
308 */
309 board_early_init_f();
310
Stefan Roese83097cf2015-11-25 07:37:00 +0100311 /* Example code showing how to enable the debug UART on MVEBU */
312#ifdef EARLY_UART
313 /*
314 * Debug UART can be used from here if required:
315 *
316 * debug_uart_init();
317 * printch('a');
318 * printhex8(0x1234);
319 * printascii("string");
320 */
321#endif
322
Stefan Roese85bddff2019-04-12 16:42:28 +0200323 /*
324 * Use special translation offset for SPL. This needs to be
325 * configured *before* spl_init() is called as this function
326 * calls dm_init() which calls the bind functions of the
327 * device drivers. Here the base address needs to be configured
328 * (translated) correctly.
329 */
330 gd->translation_offset = 0xd0000000 - 0xf1000000;
331
Stefan Roese83097cf2015-11-25 07:37:00 +0100332 ret = spl_init();
333 if (ret) {
334 debug("spl_init() failed: %d\n", ret);
335 hang();
336 }
337
Stefan Roesee463bf32015-01-19 11:33:42 +0100338 preloader_console_init();
339
Stefan Roesed04fe8b2015-07-15 15:36:52 +0200340 timer_init();
341
Stefan Roese479f9af2016-02-10 07:23:00 +0100342 /* Armada 375 does not support SerDes and DDR3 init yet */
343#if !defined(CONFIG_ARMADA_375)
Stefan Roesee463bf32015-01-19 11:33:42 +0100344 /* First init the serdes PHY's */
345 serdes_phy_config();
346
347 /* Setup DDR */
348 ddr3_init();
Stefan Roese479f9af2016-02-10 07:23:00 +0100349#endif
Stefan Roesee463bf32015-01-19 11:33:42 +0100350
Baruch Siach056e1072019-07-10 18:23:04 +0300351 /* Initialize Auto Voltage Scaling */
352 mv_avs_init();
353
Chris Packham3667bec2020-02-26 19:53:50 +1300354 /* Update read timing control for PCIe */
355 mv_rtc_config();
Stefan Roesee463bf32015-01-19 11:33:42 +0100356}