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> |
| 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 | |
Simon Glass | dd8e224 | 2016-09-24 18:20:10 -0600 | [diff] [blame] | 14 | 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] | 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 | |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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 | */ |
Simon Glass | dd8e224 | 2016-09-24 18:20:10 -0600 | [diff] [blame] | 33 | void fsl_spi_boot(void) |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 34 | { |
| 35 | void (*uboot)(void) __noreturn; |
Prabhakar Kushwaha | 77aa132 | 2014-04-08 19:13:11 +0530 | [diff] [blame] | 36 | u32 offset, code_len, copy_len = 0; |
| 37 | #ifndef CONFIG_FSL_CORENET |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 38 | unsigned char *buf = NULL; |
Prabhakar Kushwaha | 77aa132 | 2014-04-08 19:13:11 +0530 | [diff] [blame] | 39 | #endif |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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 | |
Priyanka Jain | fed1d31 | 2013-11-28 10:08:12 +0530 | [diff] [blame] | 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 |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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; |
Priyanka Jain | fed1d31 | 2013-11-28 10:08:12 +0530 | [diff] [blame] | 72 | #endif |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 73 | /* copy code to DDR */ |
Prabhakar Kushwaha | 77aa132 | 2014-04-08 19:13:11 +0530 | [diff] [blame] | 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 | |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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 | } |