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 | { |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 26 | int err; |
| 27 | |
Tom Rini | fa1a73f | 2014-04-03 07:52:55 -0400 | [diff] [blame] | 28 | /* Read for a header, parse or error out. */ |
| 29 | spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, 0x40, |
| 30 | (void *)header); |
| 31 | |
| 32 | if (image_get_magic(header) != IH_MAGIC) |
| 33 | return -1; |
| 34 | |
Simon Glass | 71316c1 | 2016-09-24 18:19:53 -0600 | [diff] [blame] | 35 | err = spl_parse_image_header(&spl_image, header); |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 36 | if (err) |
| 37 | return err; |
Tom Rini | fa1a73f | 2014-04-03 07:52:55 -0400 | [diff] [blame] | 38 | |
| 39 | spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, |
| 40 | spl_image.size, (void *)spl_image.load_addr); |
| 41 | |
| 42 | /* Read device tree. */ |
| 43 | spi_flash_read(flash, CONFIG_SYS_SPI_ARGS_OFFS, |
| 44 | CONFIG_SYS_SPI_ARGS_SIZE, |
| 45 | (void *)CONFIG_SYS_SPL_ARGS_ADDR); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | #endif |
| 50 | |
Lokesh Vutla | 00d5595 | 2016-05-24 10:34:40 +0530 | [diff] [blame] | 51 | static ulong spl_spi_fit_read(struct spl_load_info *load, ulong sector, |
| 52 | ulong count, void *buf) |
| 53 | { |
| 54 | struct spi_flash *flash = load->dev; |
| 55 | ulong ret; |
| 56 | |
| 57 | ret = spi_flash_read(flash, sector, count, buf); |
| 58 | if (!ret) |
| 59 | return count; |
| 60 | else |
| 61 | return 0; |
| 62 | } |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 63 | /* |
| 64 | * The main entry for SPI booting. It's necessary that SDRAM is already |
| 65 | * configured and available since this code loads the main U-Boot image |
| 66 | * from SPI into SDRAM and starts it from there. |
| 67 | */ |
Simon Glass | 139db7a | 2016-09-24 18:20:09 -0600 | [diff] [blame^] | 68 | static int spl_spi_load_image(struct spl_boot_device *bootdev) |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 69 | { |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 70 | int err = 0; |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 71 | struct spi_flash *flash; |
Tom Rini | a4cc1c4 | 2012-08-14 14:34:10 -0700 | [diff] [blame] | 72 | struct image_header *header; |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * Load U-Boot image from SPI flash into RAM |
| 76 | */ |
| 77 | |
Nikita Kiryanov | 88e34e5 | 2014-08-20 15:08:48 +0300 | [diff] [blame] | 78 | flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS, |
| 79 | CONFIG_SF_DEFAULT_CS, |
| 80 | CONFIG_SF_DEFAULT_SPEED, |
| 81 | CONFIG_SF_DEFAULT_MODE); |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 82 | if (!flash) { |
Tom Rini | a4cc1c4 | 2012-08-14 14:34:10 -0700 | [diff] [blame] | 83 | puts("SPI probe failed.\n"); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 84 | return -ENODEV; |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Tom Rini | a4cc1c4 | 2012-08-14 14:34:10 -0700 | [diff] [blame] | 87 | /* use CONFIG_SYS_TEXT_BASE as temporary storage area */ |
| 88 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE); |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 89 | |
Tom Rini | fa1a73f | 2014-04-03 07:52:55 -0400 | [diff] [blame] | 90 | #ifdef CONFIG_SPL_OS_BOOT |
| 91 | if (spl_start_uboot() || spi_load_image_os(flash, header)) |
| 92 | #endif |
| 93 | { |
| 94 | /* Load u-boot, mkimage header is 64 bytes. */ |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 95 | err = spi_flash_read(flash, CONFIG_SYS_SPI_U_BOOT_OFFS, 0x40, |
| 96 | (void *)header); |
| 97 | if (err) |
| 98 | return err; |
| 99 | |
Lokesh Vutla | 00d5595 | 2016-05-24 10:34:40 +0530 | [diff] [blame] | 100 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) { |
| 101 | struct spl_load_info load; |
| 102 | |
| 103 | debug("Found FIT\n"); |
| 104 | load.dev = flash; |
| 105 | load.priv = NULL; |
| 106 | load.filename = NULL; |
| 107 | load.bl_len = 1; |
| 108 | load.read = spl_spi_fit_read; |
| 109 | err = spl_load_simple_fit(&load, |
| 110 | CONFIG_SYS_SPI_U_BOOT_OFFS, |
| 111 | header); |
| 112 | } else { |
Simon Glass | 71316c1 | 2016-09-24 18:19:53 -0600 | [diff] [blame] | 113 | err = spl_parse_image_header(&spl_image, header); |
Lokesh Vutla | 00d5595 | 2016-05-24 10:34:40 +0530 | [diff] [blame] | 114 | if (err) |
| 115 | return err; |
| 116 | err = spi_flash_read(flash, CONFIG_SYS_SPI_U_BOOT_OFFS, |
| 117 | spl_image.size, |
| 118 | (void *)spl_image.load_addr); |
| 119 | } |
Tom Rini | fa1a73f | 2014-04-03 07:52:55 -0400 | [diff] [blame] | 120 | } |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 121 | |
| 122 | return err; |
Christian Riesch | 32b1127 | 2011-12-09 09:47:35 +0000 | [diff] [blame] | 123 | } |
Simon Glass | 139db7a | 2016-09-24 18:20:09 -0600 | [diff] [blame^] | 124 | /* Use priorty 1 so that boards can override this */ |
| 125 | SPL_LOAD_IMAGE_METHOD(1, BOOT_DEVICE_SPI, spl_spi_load_image); |