blob: 580b1e24b736c2cddf0e549143a215dd62483756 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ying Zhangded0f0f2013-08-16 15:16:13 +08002/*
3 * Copyright 2013 Freescale Semiconductor, Inc.
Ying Zhangded0f0f2013-08-16 15:16:13 +08004 */
5
6#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -07007#include <cpu_func.h>
Ying Zhangded0f0f2013-08-16 15:16:13 +08008#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 Glassdd8e2242016-09-24 18:20:10 -060015void fsl_spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst)
Prabhakar Kushwaha9ea255a2014-04-08 19:13:22 +053016{
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 Zhangded0f0f2013-08-16 15:16:13 +080029/*
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 Glassdd8e2242016-09-24 18:20:10 -060034void fsl_spi_boot(void)
Ying Zhangded0f0f2013-08-16 15:16:13 +080035{
36 void (*uboot)(void) __noreturn;
Prabhakar Kushwaha77aa1322014-04-08 19:13:11 +053037 u32 offset, code_len, copy_len = 0;
38#ifndef CONFIG_FSL_CORENET
Ying Zhangded0f0f2013-08-16 15:16:13 +080039 unsigned char *buf = NULL;
Prabhakar Kushwaha77aa1322014-04-08 19:13:11 +053040#endif
Ying Zhangded0f0f2013-08-16 15:16:13 +080041 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 Jainfed1d312013-11-28 10:08:12 +053050#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 Zhangded0f0f2013-08-16 15:16:13 +080054 /*
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 Jainfed1d312013-11-28 10:08:12 +053073#endif
Ying Zhangded0f0f2013-08-16 15:16:13 +080074 /* copy code to DDR */
Prabhakar Kushwaha77aa1322014-04-08 19:13:11 +053075 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 Zhangded0f0f2013-08-16 15:16:13 +080084 /*
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}