Simon Glass | c914f19 | 2019-12-08 17:40:13 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2019 Google LLC |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <binman_sym.h> |
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 8 | #include <bootstage.h> |
Simon Glass | c914f19 | 2019-12-08 17:40:13 -0700 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 10 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | c914f19 | 2019-12-08 17:40:13 -0700 | [diff] [blame] | 13 | #include <spi.h> |
| 14 | #include <spl.h> |
| 15 | #include <spi_flash.h> |
| 16 | #include <asm/fast_spi.h> |
| 17 | #include <asm/spl.h> |
| 18 | #include <asm/arch/cpu.h> |
| 19 | #include <asm/arch/iomap.h> |
| 20 | #include <dm/device-internal.h> |
| 21 | #include <dm/uclass-internal.h> |
| 22 | |
| 23 | /* This reads the next phase from mapped SPI flash */ |
| 24 | static int rom_load_image(struct spl_image_info *spl_image, |
| 25 | struct spl_boot_device *bootdev) |
| 26 | { |
| 27 | ulong spl_pos = spl_get_image_pos(); |
| 28 | ulong spl_size = spl_get_image_size(); |
| 29 | struct udevice *dev; |
| 30 | ulong map_base; |
| 31 | size_t map_size; |
| 32 | uint offset; |
| 33 | int ret; |
| 34 | |
| 35 | spl_image->size = CONFIG_SYS_MONITOR_LEN; /* We don't know SPL size */ |
| 36 | spl_image->entry_point = spl_phase() == PHASE_TPL ? |
| 37 | CONFIG_SPL_TEXT_BASE : CONFIG_SYS_TEXT_BASE; |
| 38 | spl_image->load_addr = spl_image->entry_point; |
| 39 | spl_image->os = IH_OS_U_BOOT; |
| 40 | spl_image->name = "U-Boot"; |
| 41 | debug("Reading from mapped SPI %lx, size %lx", spl_pos, spl_size); |
| 42 | |
| 43 | if (CONFIG_IS_ENABLED(SPI_FLASH_SUPPORT)) { |
| 44 | ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev); |
| 45 | if (ret) |
| 46 | return log_msg_ret("spi_flash", ret); |
| 47 | if (!dev) |
| 48 | return log_msg_ret("spi_flash dev", -ENODEV); |
| 49 | ret = dm_spi_get_mmap(dev, &map_base, &map_size, &offset); |
| 50 | if (ret) |
| 51 | return log_msg_ret("mmap", ret); |
| 52 | } else { |
| 53 | ret = fast_spi_get_bios_mmap(PCH_DEV_SPI, &map_base, &map_size, |
| 54 | &offset); |
| 55 | if (ret) |
| 56 | return ret; |
| 57 | } |
| 58 | spl_pos += map_base & ~0xff000000; |
| 59 | debug(", base %lx, pos %lx\n", map_base, spl_pos); |
| 60 | bootstage_start(BOOTSTAGE_ID_ACCUM_MMAP_SPI, "mmap_spi"); |
| 61 | memcpy((void *)spl_image->load_addr, (void *)spl_pos, spl_size); |
| 62 | cpu_flush_l1d_to_l2(); |
| 63 | bootstage_accum(BOOTSTAGE_ID_ACCUM_MMAP_SPI); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | SPL_LOAD_IMAGE_METHOD("Mapped SPI", 2, BOOT_DEVICE_SPI_MMAP, rom_load_image); |
| 68 | |
| 69 | #if CONFIG_IS_ENABLED(SPI_FLASH_SUPPORT) |
| 70 | |
| 71 | static int apl_flash_std_read(struct udevice *dev, u32 offset, size_t len, |
| 72 | void *buf) |
| 73 | { |
| 74 | struct spi_flash *flash = dev_get_uclass_priv(dev); |
| 75 | struct mtd_info *mtd = &flash->mtd; |
| 76 | size_t retlen; |
| 77 | |
| 78 | return log_ret(mtd->_read(mtd, offset, len, &retlen, buf)); |
| 79 | } |
| 80 | |
| 81 | static int apl_flash_probe(struct udevice *dev) |
| 82 | { |
| 83 | return spi_flash_std_probe(dev); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Manually set the parent of the SPI flash to SPI, since dtoc doesn't. We also |
| 88 | * need to allocate the parent_platdata since by the time this function is |
| 89 | * called device_bind() has already gone past that step. |
| 90 | */ |
| 91 | static int apl_flash_bind(struct udevice *dev) |
| 92 | { |
| 93 | if (CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 94 | struct dm_spi_slave_platdata *plat; |
| 95 | struct udevice *spi; |
| 96 | int ret; |
| 97 | |
| 98 | ret = uclass_first_device_err(UCLASS_SPI, &spi); |
| 99 | if (ret) |
| 100 | return ret; |
| 101 | dev->parent = spi; |
| 102 | |
| 103 | plat = calloc(sizeof(*plat), 1); |
| 104 | if (!plat) |
| 105 | return -ENOMEM; |
| 106 | dev->parent_platdata = plat; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static const struct dm_spi_flash_ops apl_flash_ops = { |
| 113 | .read = apl_flash_std_read, |
| 114 | }; |
| 115 | |
| 116 | static const struct udevice_id apl_flash_ids[] = { |
| 117 | { .compatible = "jedec,spi-nor" }, |
| 118 | { } |
| 119 | }; |
| 120 | |
| 121 | U_BOOT_DRIVER(winbond_w25q128fw) = { |
| 122 | .name = "winbond_w25q128fw", |
| 123 | .id = UCLASS_SPI_FLASH, |
| 124 | .of_match = apl_flash_ids, |
| 125 | .bind = apl_flash_bind, |
| 126 | .probe = apl_flash_probe, |
| 127 | .priv_auto_alloc_size = sizeof(struct spi_flash), |
| 128 | .ops = &apl_flash_ops, |
| 129 | }; |
| 130 | |
| 131 | /* This uses a SPI flash device to read the next phase */ |
| 132 | static int spl_fast_spi_load_image(struct spl_image_info *spl_image, |
| 133 | struct spl_boot_device *bootdev) |
| 134 | { |
| 135 | ulong spl_pos = spl_get_image_pos(); |
| 136 | ulong spl_size = spl_get_image_size(); |
| 137 | struct udevice *dev; |
| 138 | int ret; |
| 139 | |
| 140 | ret = uclass_first_device_err(UCLASS_SPI_FLASH, &dev); |
| 141 | if (ret) |
| 142 | return ret; |
| 143 | |
| 144 | spl_image->size = CONFIG_SYS_MONITOR_LEN; /* We don't know SPL size */ |
| 145 | spl_image->entry_point = spl_phase() == PHASE_TPL ? |
| 146 | CONFIG_SPL_TEXT_BASE : CONFIG_SYS_TEXT_BASE; |
| 147 | spl_image->load_addr = spl_image->entry_point; |
| 148 | spl_image->os = IH_OS_U_BOOT; |
| 149 | spl_image->name = "U-Boot"; |
| 150 | spl_pos &= ~0xff000000; |
| 151 | debug("Reading from flash %lx, size %lx\n", spl_pos, spl_size); |
| 152 | ret = spi_flash_read_dm(dev, spl_pos, spl_size, |
| 153 | (void *)spl_image->load_addr); |
| 154 | cpu_flush_l1d_to_l2(); |
| 155 | if (ret) |
| 156 | return ret; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | SPL_LOAD_IMAGE_METHOD("Fast SPI", 1, BOOT_DEVICE_FAST_SPI, |
| 161 | spl_fast_spi_load_image); |
| 162 | |
| 163 | void board_boot_order(u32 *spl_boot_list) |
| 164 | { |
| 165 | bool use_spi_flash = IS_ENABLED(CONFIG_APL_BOOT_FROM_FAST_SPI_FLASH); |
| 166 | |
| 167 | if (use_spi_flash) { |
| 168 | spl_boot_list[0] = BOOT_DEVICE_FAST_SPI; |
| 169 | spl_boot_list[1] = BOOT_DEVICE_SPI_MMAP; |
| 170 | } else { |
| 171 | spl_boot_list[0] = BOOT_DEVICE_SPI_MMAP; |
| 172 | spl_boot_list[1] = BOOT_DEVICE_FAST_SPI; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | #else |
| 177 | |
| 178 | void board_boot_order(u32 *spl_boot_list) |
| 179 | { |
| 180 | spl_boot_list[0] = BOOT_DEVICE_SPI_MMAP; |
| 181 | } |
| 182 | #endif |