2 * From Coreboot src/southbridge/intel/bd82x6x/mrccache.c
4 * Copyright (C) 2014 Google Inc.
6 * SPDX-License-Identifier: GPL-2.0
14 #include <spi_flash.h>
15 #include <asm/arch/mrccache.h>
16 #include <asm/arch/sandybridge.h>
18 static struct mrc_data_container *next_mrc_block(
19 struct mrc_data_container *mrc_cache)
21 /* MRC data blocks are aligned within the region */
22 u32 mrc_size = sizeof(*mrc_cache) + mrc_cache->data_size;
23 if (mrc_size & (MRC_DATA_ALIGN - 1UL)) {
24 mrc_size &= ~(MRC_DATA_ALIGN - 1UL);
25 mrc_size += MRC_DATA_ALIGN;
28 u8 *region_ptr = (u8 *)mrc_cache;
29 region_ptr += mrc_size;
30 return (struct mrc_data_container *)region_ptr;
33 static int is_mrc_cache(struct mrc_data_container *cache)
35 return cache && (cache->signature == MRC_DATA_SIGNATURE);
39 * Find the largest index block in the MRC cache. Return NULL if none is
42 struct mrc_data_container *mrccache_find_current(struct fmap_entry *entry)
44 struct mrc_data_container *cache, *next;
45 ulong base_addr, end_addr;
48 base_addr = (1ULL << 32) - CONFIG_ROM_SIZE + entry->offset;
49 end_addr = base_addr + entry->length;
52 /* Search for the last filled entry in the region */
53 for (id = 0, next = (struct mrc_data_container *)base_addr;
57 next = next_mrc_block(next);
58 if ((ulong)next >= end_addr)
63 debug("%s: No valid MRC cache found.\n", __func__);
68 if (cache->checksum != compute_ip_checksum(cache->data,
70 printf("%s: MRC cache checksum mismatch\n", __func__);
74 debug("%s: picked entry %u from cache block\n", __func__, id);
80 * find_next_mrc_cache() - get next cache entry
82 * @entry: MRC cache flash area
83 * @cache: Entry to start from
85 * @return next cache entry if found, NULL if we got to the end
87 static struct mrc_data_container *find_next_mrc_cache(struct fmap_entry *entry,
88 struct mrc_data_container *cache)
90 ulong base_addr, end_addr;
92 base_addr = (1ULL << 32) - CONFIG_ROM_SIZE + entry->offset;
93 end_addr = base_addr + entry->length;
95 cache = next_mrc_block(cache);
96 if ((ulong)cache >= end_addr) {
97 /* Crossed the boundary */
99 debug("%s: no available entries found\n", __func__);
101 debug("%s: picked next entry from cache block at %p\n",
108 int mrccache_update(struct udevice *sf, struct fmap_entry *entry,
109 struct mrc_data_container *cur)
111 struct mrc_data_container *cache;
116 /* Find the last used block */
117 base_addr = (1ULL << 32) - CONFIG_ROM_SIZE + entry->offset;
118 debug("Updating MRC cache data\n");
119 cache = mrccache_find_current(entry);
120 if (cache && (cache->data_size == cur->data_size) &&
121 (!memcmp(cache, cur, cache->data_size + sizeof(*cur)))) {
122 debug("MRC data in flash is up to date. No update\n");
126 /* Move to the next block, which will be the first unused block */
128 cache = find_next_mrc_cache(entry, cache);
131 * If we have got to the end, erase the entire mrc-cache area and start
135 debug("Erasing the MRC cache region of %x bytes at %x\n",
136 entry->length, entry->offset);
138 ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
140 debug("Failed to erase flash region\n");
143 cache = (struct mrc_data_container *)base_addr;
146 /* Write the data out */
147 offset = (ulong)cache - base_addr + entry->offset;
148 debug("Write MRC cache update to flash at %lx\n", offset);
149 ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
152 debug("Failed to write to SPI flash\n");