Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Freescale Semiconductor, Inc. |
| 3 | * |
York Sun | b27fef5 | 2013-08-20 10:15:37 -0700 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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 | |
Simon Glass | dd8e224 | 2016-09-24 18:20:10 -0600 | [diff] [blame^] | 15 | 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] | 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 | |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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 | */ |
Simon Glass | dd8e224 | 2016-09-24 18:20:10 -0600 | [diff] [blame^] | 34 | void fsl_spi_boot(void) |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 35 | { |
| 36 | void (*uboot)(void) __noreturn; |
Prabhakar Kushwaha | 77aa132 | 2014-04-08 19:13:11 +0530 | [diff] [blame] | 37 | u32 offset, code_len, copy_len = 0; |
| 38 | #ifndef CONFIG_FSL_CORENET |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 39 | unsigned char *buf = NULL; |
Prabhakar Kushwaha | 77aa132 | 2014-04-08 19:13:11 +0530 | [diff] [blame] | 40 | #endif |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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 | |
Priyanka Jain | fed1d31 | 2013-11-28 10:08:12 +0530 | [diff] [blame] | 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 |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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; |
Priyanka Jain | fed1d31 | 2013-11-28 10:08:12 +0530 | [diff] [blame] | 73 | #endif |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 74 | /* copy code to DDR */ |
Prabhakar Kushwaha | 77aa132 | 2014-04-08 19:13:11 +0530 | [diff] [blame] | 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 | |
Ying Zhang | ded0f0f | 2013-08-16 15:16:13 +0800 | [diff] [blame] | 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 | } |