Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 OMICRON electronics GmbH |
| 3 | * |
| 4 | * based on drivers/mtd/nand/nand_spl_load.c |
| 5 | * |
| 6 | * Copyright (C) 2011 |
| 7 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 8 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Simon Glass | ff0960f | 2014-10-13 23:42:04 -0600 | [diff] [blame] | 13 | #include <spi.h> |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 14 | #include <spi_flash.h> |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 15 | #include <errno.h> |
Tom Rini | a4cc1c4 | 2012-08-14 14:34:10 -0700 | [diff] [blame] | 16 | #include <spl.h> |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 17 | |
Tom Rini | fa1a73f | 2014-04-03 07:52:55 -0400 | [diff] [blame] | 18 | #ifdef CONFIG_SPL_OS_BOOT |
| 19 | /* |
| 20 | * Load the kernel, check for a valid header we can parse, and if found load |
| 21 | * the kernel and then device tree. |
| 22 | */ |
| 23 | static int spi_load_image_os(struct spi_flash *flash, |
| 24 | struct image_header *header) |
| 25 | { |
| 26 | /* Read for a header, parse or error out. */ |
| 27 | spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, 0x40, |
| 28 | (void *)header); |
| 29 | |
| 30 | if (image_get_magic(header) != IH_MAGIC) |
| 31 | return -1; |
| 32 | |
| 33 | spl_parse_image_header(header); |
| 34 | |
| 35 | spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, |
| 36 | spl_image.size, (void *)spl_image.load_addr); |
| 37 | |
| 38 | /* Read device tree. */ |
| 39 | spi_flash_read(flash, CONFIG_SYS_SPI_ARGS_OFFS, |
| 40 | CONFIG_SYS_SPI_ARGS_SIZE, |
| 41 | (void *)CONFIG_SYS_SPL_ARGS_ADDR); |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | #endif |
| 46 | |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 47 | /* |
| 48 | * The main entry for SPI booting. It's necessary that SDRAM is already |
| 49 | * configured and available since this code loads the main U-Boot image |
| 50 | * from SPI into SDRAM and starts it from there. |
| 51 | */ |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 52 | int spl_spi_load_image(void) |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 53 | { |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 54 | int err = 0; |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 55 | struct spi_flash *flash; |
Tom Rini | a4cc1c4 | 2012-08-14 14:34:10 -0700 | [diff] [blame] | 56 | struct image_header *header; |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * Load U-Boot image from SPI flash into RAM |
| 60 | */ |
| 61 | |
Nikita Kiryanov | 88e34e5 | 2014-08-20 15:08:48 +0300 | [diff] [blame] | 62 | flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS, |
| 63 | CONFIG_SF_DEFAULT_CS, |
| 64 | CONFIG_SF_DEFAULT_SPEED, |
| 65 | CONFIG_SF_DEFAULT_MODE); |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 66 | if (!flash) { |
Tom Rini | a4cc1c4 | 2012-08-14 14:34:10 -0700 | [diff] [blame] | 67 | puts("SPI probe failed.\n"); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 68 | return -ENODEV; |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Tom Rini | a4cc1c4 | 2012-08-14 14:34:10 -0700 | [diff] [blame] | 71 | /* use CONFIG_SYS_TEXT_BASE as temporary storage area */ |
| 72 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE); |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 73 | |
Tom Rini | fa1a73f | 2014-04-03 07:52:55 -0400 | [diff] [blame] | 74 | #ifdef CONFIG_SPL_OS_BOOT |
| 75 | if (spl_start_uboot() || spi_load_image_os(flash, header)) |
| 76 | #endif |
| 77 | { |
| 78 | /* Load u-boot, mkimage header is 64 bytes. */ |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 79 | err = spi_flash_read(flash, CONFIG_SYS_SPI_U_BOOT_OFFS, 0x40, |
| 80 | (void *)header); |
| 81 | if (err) |
| 82 | return err; |
| 83 | |
Tom Rini | fa1a73f | 2014-04-03 07:52:55 -0400 | [diff] [blame] | 84 | spl_parse_image_header(header); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 85 | err = spi_flash_read(flash, CONFIG_SYS_SPI_U_BOOT_OFFS, |
Tom Rini | fa1a73f | 2014-04-03 07:52:55 -0400 | [diff] [blame] | 86 | spl_image.size, (void *)spl_image.load_addr); |
| 87 | } |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 88 | |
| 89 | return err; |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 90 | } |