Merge branch 'u-boot-atmel/master' into 'u-boot-arm/master'
[platform/kernel/u-boot.git] / drivers / mtd / spi / fsl_espi_spl.c
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 /*
16  * The main entry for SPI booting. It's necessary that SDRAM is already
17  * configured and available since this code loads the main U-Boot image
18  * from SPI into SDRAM and starts it from there.
19  */
20 void spi_boot(void)
21 {
22         void (*uboot)(void) __noreturn;
23         u32 offset, code_len;
24         unsigned char *buf = NULL;
25         struct spi_flash *flash;
26
27         flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
28                         CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
29         if (flash == NULL) {
30                 puts("\nspi_flash_probe failed");
31                 hang();
32         }
33
34         /*
35         * Load U-Boot image from SPI flash into RAM
36         */
37         buf = malloc(flash->page_size);
38         if (buf == NULL) {
39                 puts("\nmalloc failed");
40                 hang();
41         }
42         memset(buf, 0, flash->page_size);
43
44         spi_flash_read(flash, CONFIG_CFG_DATA_SECTOR,
45                        flash->page_size, (void *)buf);
46         offset = *(u32 *)(buf + ESPI_BOOT_IMAGE_ADDR);
47         /* Skip spl code */
48         offset += CONFIG_SYS_SPI_FLASH_U_BOOT_OFFS;
49         /* Get the code size from offset 0x48 */
50         code_len = *(u32 *)(buf + ESPI_BOOT_IMAGE_SIZE);
51         /* Skip spl code */
52         code_len = code_len - CONFIG_SPL_MAX_SIZE;
53         /* copy code to DDR */
54         spi_flash_read(flash, offset, code_len,
55                        (void *)CONFIG_SYS_SPI_FLASH_U_BOOT_DST);
56         /*
57         * Jump to U-Boot image
58         */
59         flush_cache(CONFIG_SYS_SPI_FLASH_U_BOOT_DST, code_len);
60         uboot = (void *)CONFIG_SYS_SPI_FLASH_U_BOOT_START;
61         (*uboot)();
62 }