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