blob: b90e6a5527aa6e72805341555794dc4ce188ec0d [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>
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 Glassdd8e2242016-09-24 18:20:10 -060014void fsl_spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst)
Prabhakar Kushwaha9ea255a2014-04-08 19:13:22 +053015{
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 Zhangded0f0f2013-08-16 15:16:13 +080028/*
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 Glassdd8e2242016-09-24 18:20:10 -060033void fsl_spi_boot(void)
Ying Zhangded0f0f2013-08-16 15:16:13 +080034{
35 void (*uboot)(void) __noreturn;
Prabhakar Kushwaha77aa1322014-04-08 19:13:11 +053036 u32 offset, code_len, copy_len = 0;
37#ifndef CONFIG_FSL_CORENET
Ying Zhangded0f0f2013-08-16 15:16:13 +080038 unsigned char *buf = NULL;
Prabhakar Kushwaha77aa1322014-04-08 19:13:11 +053039#endif
Ying Zhangded0f0f2013-08-16 15:16:13 +080040 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 Jainfed1d312013-11-28 10:08:12 +053049#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 Zhangded0f0f2013-08-16 15:16:13 +080053 /*
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 Jainfed1d312013-11-28 10:08:12 +053072#endif
Ying Zhangded0f0f2013-08-16 15:16:13 +080073 /* copy code to DDR */
Prabhakar Kushwaha77aa1322014-04-08 19:13:11 +053074 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 Zhangded0f0f2013-08-16 15:16:13 +080083 /*
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}