blob: a55d741a3702a7b561284cae04c6c49551e01834 [file] [log] [blame]
Ying Zhangded0f0f2013-08-16 15:16:13 +08001/*
2 * Copyright 2013 Freescale Semiconductor, Inc.
3 *
York Sunb27fef52013-08-20 10:15:37 -07004 * SPDX-License-Identifier: GPL-2.0+
Ying Zhangded0f0f2013-08-16 15:16:13 +08005 */
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 */
20void spi_boot(void)
21{
22 void (*uboot)(void) __noreturn;
Prabhakar Kushwaha77aa1322014-04-08 19:13:11 +053023 u32 offset, code_len, copy_len = 0;
24#ifndef CONFIG_FSL_CORENET
Ying Zhangded0f0f2013-08-16 15:16:13 +080025 unsigned char *buf = NULL;
Prabhakar Kushwaha77aa1322014-04-08 19:13:11 +053026#endif
Ying Zhangded0f0f2013-08-16 15:16:13 +080027 struct spi_flash *flash;
28
29 flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
30 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
31 if (flash == NULL) {
32 puts("\nspi_flash_probe failed");
33 hang();
34 }
35
Priyanka Jainfed1d312013-11-28 10:08:12 +053036#ifdef CONFIG_FSL_CORENET
37 offset = CONFIG_SYS_SPI_FLASH_U_BOOT_OFFS;
38 code_len = CONFIG_SYS_SPI_FLASH_U_BOOT_SIZE;
39#else
Ying Zhangded0f0f2013-08-16 15:16:13 +080040 /*
41 * Load U-Boot image from SPI flash into RAM
42 */
43 buf = malloc(flash->page_size);
44 if (buf == NULL) {
45 puts("\nmalloc failed");
46 hang();
47 }
48 memset(buf, 0, flash->page_size);
49
50 spi_flash_read(flash, CONFIG_CFG_DATA_SECTOR,
51 flash->page_size, (void *)buf);
52 offset = *(u32 *)(buf + ESPI_BOOT_IMAGE_ADDR);
53 /* Skip spl code */
54 offset += CONFIG_SYS_SPI_FLASH_U_BOOT_OFFS;
55 /* Get the code size from offset 0x48 */
56 code_len = *(u32 *)(buf + ESPI_BOOT_IMAGE_SIZE);
57 /* Skip spl code */
58 code_len = code_len - CONFIG_SPL_MAX_SIZE;
Priyanka Jainfed1d312013-11-28 10:08:12 +053059#endif
Ying Zhangded0f0f2013-08-16 15:16:13 +080060 /* copy code to DDR */
Prabhakar Kushwaha77aa1322014-04-08 19:13:11 +053061 printf("Loading second stage boot loader ");
62 while (copy_len <= code_len) {
63 spi_flash_read(flash, offset + copy_len, 0x2000,
64 (void *)(CONFIG_SYS_SPI_FLASH_U_BOOT_DST
65 + copy_len));
66 copy_len = copy_len + 0x2000;
67 putc('.');
68 }
69
Ying Zhangded0f0f2013-08-16 15:16:13 +080070 /*
71 * Jump to U-Boot image
72 */
73 flush_cache(CONFIG_SYS_SPI_FLASH_U_BOOT_DST, code_len);
74 uboot = (void *)CONFIG_SYS_SPI_FLASH_U_BOOT_START;
75 (*uboot)();
76}