Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2013 Freescale Semiconductor, Inc. |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 8 | #include <hang.h> |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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 | |
Simon Glass | dd8e224 | 2016-09-24 18:20:10 -0600 | [diff] [blame] | 16 | void fsl_spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst) |
Prabhakar Kushwaha | 9ea255a | 2014-04-08 19:13:22 +0530 | [diff] [blame] | 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 | |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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 | */ |
Simon Glass | dd8e224 | 2016-09-24 18:20:10 -0600 | [diff] [blame] | 35 | void fsl_spi_boot(void) |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 36 | { |
| 37 | void (*uboot)(void) __noreturn; |
Prabhakar Kushwaha | 77aa132 | 2014-04-08 19:13:11 +0530 | [diff] [blame] | 38 | u32 offset, code_len, copy_len = 0; |
| 39 | #ifndef CONFIG_FSL_CORENET |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 40 | unsigned char *buf = NULL; |
Prabhakar Kushwaha | 77aa132 | 2014-04-08 19:13:11 +0530 | [diff] [blame] | 41 | #endif |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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 | |
Priyanka Jain | fed1d31 | 2013-11-28 10:08:12 +0530 | [diff] [blame] | 51 | #ifdef CONFIG_FSL_CORENET |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 52 | offset = CFG_SYS_SPI_FLASH_U_BOOT_OFFS; |
| 53 | code_len = CFG_SYS_SPI_FLASH_U_BOOT_SIZE; |
Priyanka Jain | fed1d31 | 2013-11-28 10:08:12 +0530 | [diff] [blame] | 54 | #else |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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 */ |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 69 | offset += CFG_SYS_SPI_FLASH_U_BOOT_OFFS; |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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; |
Priyanka Jain | fed1d31 | 2013-11-28 10:08:12 +0530 | [diff] [blame] | 74 | #endif |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 75 | /* copy code to DDR */ |
Prabhakar Kushwaha | 77aa132 | 2014-04-08 19:13:11 +0530 | [diff] [blame] | 76 | printf("Loading second stage boot loader "); |
| 77 | while (copy_len <= code_len) { |
| 78 | spi_flash_read(flash, offset + copy_len, 0x2000, |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 79 | (void *)(CFG_SYS_SPI_FLASH_U_BOOT_DST |
Prabhakar Kushwaha | 77aa132 | 2014-04-08 19:13:11 +0530 | [diff] [blame] | 80 | + copy_len)); |
| 81 | copy_len = copy_len + 0x2000; |
| 82 | putc('.'); |
| 83 | } |
| 84 | |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 85 | /* |
| 86 | * Jump to U-Boot image |
| 87 | */ |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 88 | flush_cache(CFG_SYS_SPI_FLASH_U_BOOT_DST, code_len); |
| 89 | uboot = (void *)CFG_SYS_SPI_FLASH_U_BOOT_START; |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 90 | (*uboot)(); |
| 91 | } |