Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 2 | /* |
Bin Meng | 8575ab1 | 2015-10-11 21:37:38 -0700 | [diff] [blame] | 3 | * From coreboot src/southbridge/intel/bd82x6x/mrccache.c |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2014 Google Inc. |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 6 | * Copyright (C) 2015 Bin Meng <bmeng.cn@gmail.com> |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 11 | #include <errno.h> |
| 12 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 15 | #include <net.h> |
| 16 | #include <spi.h> |
| 17 | #include <spi_flash.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Bin Meng | 21666cf | 2015-10-11 21:37:36 -0700 | [diff] [blame] | 19 | #include <asm/mrccache.h> |
Simon Glass | 9d25f2e | 2019-12-06 21:42:03 -0700 | [diff] [blame] | 20 | #include <dm/device-internal.h> |
| 21 | #include <dm/uclass-internal.h> |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 22 | |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Simon Glass | 040bef1 | 2019-09-25 08:57:04 -0600 | [diff] [blame] | 25 | static uint mrc_block_size(uint data_size) |
| 26 | { |
| 27 | uint mrc_size = sizeof(struct mrc_data_container) + data_size; |
| 28 | |
| 29 | return ALIGN(mrc_size, MRC_DATA_ALIGN); |
| 30 | } |
| 31 | |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 32 | static struct mrc_data_container *next_mrc_block( |
Bin Meng | 8575ab1 | 2015-10-11 21:37:38 -0700 | [diff] [blame] | 33 | struct mrc_data_container *cache) |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 34 | { |
| 35 | /* MRC data blocks are aligned within the region */ |
Bin Meng | 8575ab1 | 2015-10-11 21:37:38 -0700 | [diff] [blame] | 36 | u8 *region_ptr = (u8 *)cache; |
| 37 | |
Simon Glass | 040bef1 | 2019-09-25 08:57:04 -0600 | [diff] [blame] | 38 | region_ptr += mrc_block_size(cache->data_size); |
Bin Meng | 8575ab1 | 2015-10-11 21:37:38 -0700 | [diff] [blame] | 39 | |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 40 | return (struct mrc_data_container *)region_ptr; |
| 41 | } |
| 42 | |
| 43 | static int is_mrc_cache(struct mrc_data_container *cache) |
| 44 | { |
| 45 | return cache && (cache->signature == MRC_DATA_SIGNATURE); |
| 46 | } |
| 47 | |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 48 | struct mrc_data_container *mrccache_find_current(struct mrc_region *entry) |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 49 | { |
| 50 | struct mrc_data_container *cache, *next; |
| 51 | ulong base_addr, end_addr; |
| 52 | uint id; |
| 53 | |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 54 | base_addr = entry->base + entry->offset; |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 55 | end_addr = base_addr + entry->length; |
| 56 | cache = NULL; |
| 57 | |
| 58 | /* Search for the last filled entry in the region */ |
| 59 | for (id = 0, next = (struct mrc_data_container *)base_addr; |
| 60 | is_mrc_cache(next); |
| 61 | id++) { |
| 62 | cache = next; |
| 63 | next = next_mrc_block(next); |
| 64 | if ((ulong)next >= end_addr) |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | if (id-- == 0) { |
| 69 | debug("%s: No valid MRC cache found.\n", __func__); |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | /* Verify checksum */ |
| 74 | if (cache->checksum != compute_ip_checksum(cache->data, |
| 75 | cache->data_size)) { |
| 76 | printf("%s: MRC cache checksum mismatch\n", __func__); |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | debug("%s: picked entry %u from cache block\n", __func__, id); |
| 81 | |
| 82 | return cache; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * find_next_mrc_cache() - get next cache entry |
| 87 | * |
Simon Glass | d553f97 | 2019-12-06 21:42:02 -0700 | [diff] [blame] | 88 | * This moves to the next cache entry in the region, making sure it has enough |
| 89 | * space to hold data of size @data_size. |
| 90 | * |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 91 | * @entry: MRC cache flash area |
| 92 | * @cache: Entry to start from |
Simon Glass | d553f97 | 2019-12-06 21:42:02 -0700 | [diff] [blame] | 93 | * @data_size: Required data size of the new entry. Note that we assume that |
| 94 | * all cache entries are the same size |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 95 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 96 | * Return: next cache entry if found, NULL if we got to the end |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 97 | */ |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 98 | static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry, |
Simon Glass | d553f97 | 2019-12-06 21:42:02 -0700 | [diff] [blame] | 99 | struct mrc_data_container *prev, int data_size) |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 100 | { |
Simon Glass | d553f97 | 2019-12-06 21:42:02 -0700 | [diff] [blame] | 101 | struct mrc_data_container *cache; |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 102 | ulong base_addr, end_addr; |
| 103 | |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 104 | base_addr = entry->base + entry->offset; |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 105 | end_addr = base_addr + entry->length; |
| 106 | |
Simon Glass | d553f97 | 2019-12-06 21:42:02 -0700 | [diff] [blame] | 107 | /* |
| 108 | * We assume that all cache entries are the same size, but let's use |
| 109 | * data_size here for clarity. |
| 110 | */ |
| 111 | cache = next_mrc_block(prev); |
| 112 | if ((ulong)cache + mrc_block_size(data_size) > end_addr) { |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 113 | /* Crossed the boundary */ |
| 114 | cache = NULL; |
| 115 | debug("%s: no available entries found\n", __func__); |
| 116 | } else { |
| 117 | debug("%s: picked next entry from cache block at %p\n", |
| 118 | __func__, cache); |
| 119 | } |
| 120 | |
| 121 | return cache; |
| 122 | } |
| 123 | |
Simon Glass | 6e1b956 | 2019-12-06 21:42:09 -0700 | [diff] [blame] | 124 | /** |
| 125 | * mrccache_update() - update the MRC cache with a new record |
| 126 | * |
| 127 | * This writes a new record to the end of the MRC cache region. If the new |
| 128 | * record is the same as the latest record then the write is skipped |
| 129 | * |
| 130 | * @sf: SPI flash to write to |
| 131 | * @entry: Position and size of MRC cache in SPI flash |
| 132 | * @cur: Record to write |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 133 | * Return: 0 if updated, -EEXIST if the record is the same as the latest |
Simon Glass | 6e1b956 | 2019-12-06 21:42:09 -0700 | [diff] [blame] | 134 | * record, -EINVAL if the record is not valid, other error if SPI write failed |
| 135 | */ |
| 136 | static int mrccache_update(struct udevice *sf, struct mrc_region *entry, |
| 137 | struct mrc_data_container *cur) |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 138 | { |
| 139 | struct mrc_data_container *cache; |
| 140 | ulong offset; |
| 141 | ulong base_addr; |
| 142 | int ret; |
| 143 | |
Simon Glass | fbef25f | 2019-04-25 21:58:59 -0600 | [diff] [blame] | 144 | if (!is_mrc_cache(cur)) { |
| 145 | debug("%s: Cache data not valid\n", __func__); |
Bin Meng | d61a7b4 | 2015-10-11 21:37:37 -0700 | [diff] [blame] | 146 | return -EINVAL; |
Simon Glass | fbef25f | 2019-04-25 21:58:59 -0600 | [diff] [blame] | 147 | } |
Bin Meng | d61a7b4 | 2015-10-11 21:37:37 -0700 | [diff] [blame] | 148 | |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 149 | /* Find the last used block */ |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 150 | base_addr = entry->base + entry->offset; |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 151 | debug("Updating MRC cache data\n"); |
| 152 | cache = mrccache_find_current(entry); |
| 153 | if (cache && (cache->data_size == cur->data_size) && |
| 154 | (!memcmp(cache, cur, cache->data_size + sizeof(*cur)))) { |
| 155 | debug("MRC data in flash is up to date. No update\n"); |
| 156 | return -EEXIST; |
| 157 | } |
| 158 | |
| 159 | /* Move to the next block, which will be the first unused block */ |
| 160 | if (cache) |
Simon Glass | d553f97 | 2019-12-06 21:42:02 -0700 | [diff] [blame] | 161 | cache = find_next_mrc_cache(entry, cache, cur->data_size); |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * If we have got to the end, erase the entire mrc-cache area and start |
| 165 | * again at block 0. |
| 166 | */ |
| 167 | if (!cache) { |
| 168 | debug("Erasing the MRC cache region of %x bytes at %x\n", |
| 169 | entry->length, entry->offset); |
| 170 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 171 | ret = spi_flash_erase_dm(sf, entry->offset, entry->length); |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 172 | if (ret) { |
| 173 | debug("Failed to erase flash region\n"); |
| 174 | return ret; |
| 175 | } |
| 176 | cache = (struct mrc_data_container *)base_addr; |
| 177 | } |
| 178 | |
| 179 | /* Write the data out */ |
| 180 | offset = (ulong)cache - base_addr + entry->offset; |
| 181 | debug("Write MRC cache update to flash at %lx\n", offset); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 182 | ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur), |
| 183 | cur); |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 184 | if (ret) { |
| 185 | debug("Failed to write to SPI flash\n"); |
Simon Glass | 1b9d815 | 2019-12-06 21:42:06 -0700 | [diff] [blame] | 186 | return log_msg_ret("Cannot update mrccache", ret); |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 191 | |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 192 | static void mrccache_setup(struct mrc_output *mrc, void *data) |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 193 | { |
Simon Glass | 48fd856 | 2019-04-25 21:58:57 -0600 | [diff] [blame] | 194 | struct mrc_data_container *cache = data; |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 195 | u16 checksum; |
| 196 | |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 197 | cache->signature = MRC_DATA_SIGNATURE; |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 198 | cache->data_size = mrc->len; |
| 199 | checksum = compute_ip_checksum(mrc->buf, cache->data_size); |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 200 | debug("Saving %d bytes for MRC output data, checksum %04x\n", |
| 201 | cache->data_size, checksum); |
| 202 | cache->checksum = checksum; |
| 203 | cache->reserved = 0; |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 204 | memcpy(cache->data, mrc->buf, cache->data_size); |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 205 | |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 206 | mrc->cache = cache; |
Simon Glass | 48fd856 | 2019-04-25 21:58:57 -0600 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | int mrccache_reserve(void) |
| 210 | { |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 211 | int i; |
Simon Glass | 48fd856 | 2019-04-25 21:58:57 -0600 | [diff] [blame] | 212 | |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 213 | for (i = 0; i < MRC_TYPE_COUNT; i++) { |
| 214 | struct mrc_output *mrc = &gd->arch.mrc[i]; |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 215 | |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 216 | if (!mrc->len) |
| 217 | continue; |
| 218 | |
| 219 | /* adjust stack pointer to store pure cache data plus header */ |
| 220 | gd->start_addr_sp -= (mrc->len + MRC_DATA_HEADER_SIZE); |
| 221 | mrccache_setup(mrc, (void *)gd->start_addr_sp); |
| 222 | |
| 223 | gd->start_addr_sp &= ~0xf; |
| 224 | } |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 229 | int mrccache_get_region(enum mrc_type_t type, struct udevice **devp, |
| 230 | struct mrc_region *entry) |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 231 | { |
Simon Glass | 9d25f2e | 2019-12-06 21:42:03 -0700 | [diff] [blame] | 232 | struct udevice *dev; |
| 233 | ofnode mrc_node; |
Simon Glass | 4e988f9 | 2019-12-06 21:42:04 -0700 | [diff] [blame] | 234 | ulong map_base; |
| 235 | uint map_size; |
| 236 | uint offset; |
Simon Glass | 00bf279 | 2020-05-27 06:58:49 -0600 | [diff] [blame] | 237 | ofnode node; |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 238 | u32 reg[2]; |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 239 | int ret; |
| 240 | |
Simon Glass | 9d25f2e | 2019-12-06 21:42:03 -0700 | [diff] [blame] | 241 | /* |
| 242 | * Find the flash chip within the SPI controller node. Avoid probing |
| 243 | * the device here since it may put it into a strange state where the |
| 244 | * memory map cannot be read. |
| 245 | */ |
| 246 | ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev); |
Simon Glass | 00bf279 | 2020-05-27 06:58:49 -0600 | [diff] [blame] | 247 | if (ret || !dev) { |
| 248 | /* |
| 249 | * Fall back to searching the device tree since driver model |
| 250 | * may not be ready yet (e.g. with FSPv1) |
| 251 | */ |
| 252 | node = ofnode_by_compatible(ofnode_null(), "jedec,spi-nor"); |
| 253 | if (!ofnode_valid(node)) |
| 254 | return log_msg_ret("Cannot find SPI flash\n", -ENOENT); |
Simon Glass | 92f9828 | 2020-02-02 13:37:06 -0700 | [diff] [blame] | 255 | ret = -ENODEV; |
Simon Glass | 4e988f9 | 2019-12-06 21:42:04 -0700 | [diff] [blame] | 256 | } else { |
Simon Glass | 00bf279 | 2020-05-27 06:58:49 -0600 | [diff] [blame] | 257 | ret = dm_spi_get_mmap(dev, &map_base, &map_size, &offset); |
| 258 | if (!ret) |
| 259 | entry->base = map_base; |
| 260 | node = dev_ofnode(dev); |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * At this point we have entry->base if ret == 0. If not, then we have |
| 265 | * the node and can look for memory-map |
| 266 | */ |
| 267 | if (ret) { |
| 268 | ret = ofnode_read_u32_array(node, "memory-map", reg, 2); |
Simon Glass | 4e988f9 | 2019-12-06 21:42:04 -0700 | [diff] [blame] | 269 | if (ret) |
| 270 | return log_msg_ret("Cannot find memory map\n", ret); |
| 271 | entry->base = reg[0]; |
| 272 | } |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 273 | |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 274 | /* Find the place where we put the MRC cache */ |
Simon Glass | 00bf279 | 2020-05-27 06:58:49 -0600 | [diff] [blame] | 275 | mrc_node = ofnode_find_subnode(node, type == MRC_TYPE_NORMAL ? |
| 276 | "rw-mrc-cache" : "rw-var-mrc-cache"); |
Simon Glass | 9d25f2e | 2019-12-06 21:42:03 -0700 | [diff] [blame] | 277 | if (!ofnode_valid(mrc_node)) |
| 278 | return log_msg_ret("Cannot find node", -EPERM); |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 279 | |
Simon Glass | 9d25f2e | 2019-12-06 21:42:03 -0700 | [diff] [blame] | 280 | ret = ofnode_read_u32_array(mrc_node, "reg", reg, 2); |
| 281 | if (ret) |
| 282 | return log_msg_ret("Cannot find address", ret); |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 283 | entry->offset = reg[0]; |
| 284 | entry->length = reg[1]; |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 285 | |
Simon Glass | 9d25f2e | 2019-12-06 21:42:03 -0700 | [diff] [blame] | 286 | if (devp) |
| 287 | *devp = dev; |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 288 | debug("MRC cache type %d in '%s', offset %x, len %x, base %x\n", |
Simon Glass | 00bf279 | 2020-05-27 06:58:49 -0600 | [diff] [blame] | 289 | type, dev ? dev->name : ofnode_get_name(node), entry->offset, |
| 290 | entry->length, entry->base); |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 295 | static int mrccache_save_type(enum mrc_type_t type) |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 296 | { |
Simon Glass | c3d0c23 | 2019-12-06 21:42:05 -0700 | [diff] [blame] | 297 | struct mrc_data_container *cache; |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 298 | struct mrc_output *mrc; |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 299 | struct mrc_region entry; |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 300 | struct udevice *sf; |
| 301 | int ret; |
| 302 | |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 303 | mrc = &gd->arch.mrc[type]; |
| 304 | if (!mrc->len) |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 305 | return 0; |
Simon Glass | fdbc442 | 2023-05-04 16:50:53 -0600 | [diff] [blame^] | 306 | log_debug("Saving %x bytes of MRC output data type %d to SPI flash\n", |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 307 | mrc->len, type); |
| 308 | ret = mrccache_get_region(type, &sf, &entry); |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 309 | if (ret) |
Simon Glass | 1b9d815 | 2019-12-06 21:42:06 -0700 | [diff] [blame] | 310 | return log_msg_ret("Cannot get region", ret); |
Simon Glass | 9d25f2e | 2019-12-06 21:42:03 -0700 | [diff] [blame] | 311 | ret = device_probe(sf); |
| 312 | if (ret) |
Simon Glass | 1b9d815 | 2019-12-06 21:42:06 -0700 | [diff] [blame] | 313 | return log_msg_ret("Cannot probe device", ret); |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 314 | cache = mrc->cache; |
| 315 | |
Simon Glass | c3d0c23 | 2019-12-06 21:42:05 -0700 | [diff] [blame] | 316 | ret = mrccache_update(sf, &entry, cache); |
Simon Glass | 1b9d815 | 2019-12-06 21:42:06 -0700 | [diff] [blame] | 317 | if (!ret) |
Simon Glass | c3d0c23 | 2019-12-06 21:42:05 -0700 | [diff] [blame] | 318 | debug("Saved MRC data with checksum %04x\n", cache->checksum); |
Simon Glass | 1b9d815 | 2019-12-06 21:42:06 -0700 | [diff] [blame] | 319 | else if (ret == -EEXIST) |
Simon Glass | 9df244f | 2016-01-17 16:11:29 -0700 | [diff] [blame] | 320 | debug("MRC data is the same as last time, skipping save\n"); |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 321 | |
Simon Glass | 1b9d815 | 2019-12-06 21:42:06 -0700 | [diff] [blame] | 322 | return 0; |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 323 | } |
Simon Glass | 48fd856 | 2019-04-25 21:58:57 -0600 | [diff] [blame] | 324 | |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 325 | int mrccache_save(void) |
| 326 | { |
| 327 | int i; |
| 328 | |
| 329 | for (i = 0; i < MRC_TYPE_COUNT; i++) { |
| 330 | int ret; |
| 331 | |
| 332 | ret = mrccache_save_type(i); |
| 333 | if (ret) |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Simon Glass | 48fd856 | 2019-04-25 21:58:57 -0600 | [diff] [blame] | 340 | int mrccache_spl_save(void) |
| 341 | { |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 342 | int i; |
Simon Glass | 48fd856 | 2019-04-25 21:58:57 -0600 | [diff] [blame] | 343 | |
Simon Glass | 91efff5 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 344 | for (i = 0; i < MRC_TYPE_COUNT; i++) { |
| 345 | struct mrc_output *mrc = &gd->arch.mrc[i]; |
| 346 | void *data; |
| 347 | int size; |
| 348 | |
| 349 | size = mrc->len + MRC_DATA_HEADER_SIZE; |
| 350 | data = malloc(size); |
| 351 | if (!data) |
| 352 | return log_msg_ret("Allocate MRC cache block", -ENOMEM); |
| 353 | mrccache_setup(mrc, data); |
| 354 | } |
Simon Glass | 48fd856 | 2019-04-25 21:58:57 -0600 | [diff] [blame] | 355 | |
| 356 | return mrccache_save(); |
| 357 | } |