xref: /openbmc/u-boot/drivers/mtd/spi/fsl_espi_spl.c (revision ef64e782)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2013 Freescale Semiconductor, Inc.
4  */
5 
6 #include <common.h>
7 #include <spi_flash.h>
8 #include <malloc.h>
9 
10 #define ESPI_BOOT_IMAGE_SIZE	0x48
11 #define ESPI_BOOT_IMAGE_ADDR	0x50
12 #define CONFIG_CFG_DATA_SECTOR	0
13 
14 void fsl_spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst)
15 {
16 	struct spi_flash *flash;
17 
18 	flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
19 			CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
20 	if (flash == NULL) {
21 		puts("\nspi_flash_probe failed");
22 		hang();
23 	}
24 
25 	spi_flash_read(flash, offs, size, vdst);
26 }
27 
28 /*
29  * The main entry for SPI booting. It's necessary that SDRAM is already
30  * configured and available since this code loads the main U-Boot image
31  * from SPI into SDRAM and starts it from there.
32  */
33 void fsl_spi_boot(void)
34 {
35 	void (*uboot)(void) __noreturn;
36 	u32 offset, code_len, copy_len = 0;
37 #ifndef CONFIG_FSL_CORENET
38 	unsigned char *buf = NULL;
39 #endif
40 	struct spi_flash *flash;
41 
42 	flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
43 			CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
44 	if (flash == NULL) {
45 		puts("\nspi_flash_probe failed");
46 		hang();
47 	}
48 
49 #ifdef CONFIG_FSL_CORENET
50 	offset = CONFIG_SYS_SPI_FLASH_U_BOOT_OFFS;
51 	code_len = CONFIG_SYS_SPI_FLASH_U_BOOT_SIZE;
52 #else
53 	/*
54 	* Load U-Boot image from SPI flash into RAM
55 	*/
56 	buf = malloc(flash->page_size);
57 	if (buf == NULL) {
58 		puts("\nmalloc failed");
59 		hang();
60 	}
61 	memset(buf, 0, flash->page_size);
62 
63 	spi_flash_read(flash, CONFIG_CFG_DATA_SECTOR,
64 		       flash->page_size, (void *)buf);
65 	offset = *(u32 *)(buf + ESPI_BOOT_IMAGE_ADDR);
66 	/* Skip spl code */
67 	offset += CONFIG_SYS_SPI_FLASH_U_BOOT_OFFS;
68 	/* Get the code size from offset 0x48 */
69 	code_len = *(u32 *)(buf + ESPI_BOOT_IMAGE_SIZE);
70 	/* Skip spl code */
71 	code_len = code_len - CONFIG_SPL_MAX_SIZE;
72 #endif
73 	/* copy code to DDR */
74 	printf("Loading second stage boot loader ");
75 	while (copy_len <= code_len) {
76 		spi_flash_read(flash, offset + copy_len, 0x2000,
77 			       (void *)(CONFIG_SYS_SPI_FLASH_U_BOOT_DST
78 			       + copy_len));
79 		copy_len = copy_len + 0x2000;
80 		putc('.');
81 	}
82 
83 	/*
84 	* Jump to U-Boot image
85 	*/
86 	flush_cache(CONFIG_SYS_SPI_FLASH_U_BOOT_DST, code_len);
87 	uboot = (void *)CONFIG_SYS_SPI_FLASH_U_BOOT_START;
88 	(*uboot)();
89 }
90