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