1 // SPDX-License-Identifier: GPL-2.0
3 * From coreboot src/southbridge/intel/bd82x6x/mrccache.c
5 * Copyright (C) 2014 Google Inc.
16 #include <spi_flash.h>
17 #include <asm/mrccache.h>
18 #include <dm/device-internal.h>
19 #include <dm/uclass-internal.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 static uint mrc_block_size(uint data_size)
25 uint mrc_size = sizeof(struct mrc_data_container) + data_size;
27 return ALIGN(mrc_size, MRC_DATA_ALIGN);
30 static struct mrc_data_container *next_mrc_block(
31 struct mrc_data_container *cache)
33 /* MRC data blocks are aligned within the region */
34 u8 *region_ptr = (u8 *)cache;
36 region_ptr += mrc_block_size(cache->data_size);
38 return (struct mrc_data_container *)region_ptr;
41 static int is_mrc_cache(struct mrc_data_container *cache)
43 return cache && (cache->signature == MRC_DATA_SIGNATURE);
46 struct mrc_data_container *mrccache_find_current(struct mrc_region *entry)
48 struct mrc_data_container *cache, *next;
49 ulong base_addr, end_addr;
52 base_addr = entry->base + entry->offset;
53 end_addr = base_addr + entry->length;
56 /* Search for the last filled entry in the region */
57 for (id = 0, next = (struct mrc_data_container *)base_addr;
61 next = next_mrc_block(next);
62 if ((ulong)next >= end_addr)
67 debug("%s: No valid MRC cache found.\n", __func__);
72 if (cache->checksum != compute_ip_checksum(cache->data,
74 printf("%s: MRC cache checksum mismatch\n", __func__);
78 debug("%s: picked entry %u from cache block\n", __func__, id);
84 * find_next_mrc_cache() - get next cache entry
86 * This moves to the next cache entry in the region, making sure it has enough
87 * space to hold data of size @data_size.
89 * @entry: MRC cache flash area
90 * @cache: Entry to start from
91 * @data_size: Required data size of the new entry. Note that we assume that
92 * all cache entries are the same size
94 * @return next cache entry if found, NULL if we got to the end
96 static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry,
97 struct mrc_data_container *prev, int data_size)
99 struct mrc_data_container *cache;
100 ulong base_addr, end_addr;
102 base_addr = entry->base + entry->offset;
103 end_addr = base_addr + entry->length;
106 * We assume that all cache entries are the same size, but let's use
107 * data_size here for clarity.
109 cache = next_mrc_block(prev);
110 if ((ulong)cache + mrc_block_size(data_size) > end_addr) {
111 /* Crossed the boundary */
113 debug("%s: no available entries found\n", __func__);
115 debug("%s: picked next entry from cache block at %p\n",
123 * mrccache_update() - update the MRC cache with a new record
125 * This writes a new record to the end of the MRC cache region. If the new
126 * record is the same as the latest record then the write is skipped
128 * @sf: SPI flash to write to
129 * @entry: Position and size of MRC cache in SPI flash
130 * @cur: Record to write
131 * @return 0 if updated, -EEXIST if the record is the same as the latest
132 * record, -EINVAL if the record is not valid, other error if SPI write failed
134 static int mrccache_update(struct udevice *sf, struct mrc_region *entry,
135 struct mrc_data_container *cur)
137 struct mrc_data_container *cache;
142 if (!is_mrc_cache(cur)) {
143 debug("%s: Cache data not valid\n", __func__);
147 /* Find the last used block */
148 base_addr = entry->base + entry->offset;
149 debug("Updating MRC cache data\n");
150 cache = mrccache_find_current(entry);
151 if (cache && (cache->data_size == cur->data_size) &&
152 (!memcmp(cache, cur, cache->data_size + sizeof(*cur)))) {
153 debug("MRC data in flash is up to date. No update\n");
157 /* Move to the next block, which will be the first unused block */
159 cache = find_next_mrc_cache(entry, cache, cur->data_size);
162 * If we have got to the end, erase the entire mrc-cache area and start
166 debug("Erasing the MRC cache region of %x bytes at %x\n",
167 entry->length, entry->offset);
169 ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
171 debug("Failed to erase flash region\n");
174 cache = (struct mrc_data_container *)base_addr;
177 /* Write the data out */
178 offset = (ulong)cache - base_addr + entry->offset;
179 debug("Write MRC cache update to flash at %lx\n", offset);
180 ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
183 debug("Failed to write to SPI flash\n");
184 return log_msg_ret("Cannot update mrccache", ret);
190 static void mrccache_setup(struct mrc_output *mrc, void *data)
192 struct mrc_data_container *cache = data;
195 cache->signature = MRC_DATA_SIGNATURE;
196 cache->data_size = mrc->len;
197 checksum = compute_ip_checksum(mrc->buf, cache->data_size);
198 debug("Saving %d bytes for MRC output data, checksum %04x\n",
199 cache->data_size, checksum);
200 cache->checksum = checksum;
202 memcpy(cache->data, mrc->buf, cache->data_size);
207 int mrccache_reserve(void)
211 for (i = 0; i < MRC_TYPE_COUNT; i++) {
212 struct mrc_output *mrc = &gd->arch.mrc[i];
217 /* adjust stack pointer to store pure cache data plus header */
218 gd->start_addr_sp -= (mrc->len + MRC_DATA_HEADER_SIZE);
219 mrccache_setup(mrc, (void *)gd->start_addr_sp);
221 gd->start_addr_sp &= ~0xf;
227 int mrccache_get_region(enum mrc_type_t type, struct udevice **devp,
228 struct mrc_region *entry)
239 * Find the flash chip within the SPI controller node. Avoid probing
240 * the device here since it may put it into a strange state where the
241 * memory map cannot be read.
243 ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
245 return log_msg_ret("Cannot find SPI flash\n", ret);
246 ret = dm_spi_get_mmap(dev, &map_base, &map_size, &offset);
248 entry->base = map_base;
250 ret = dev_read_u32_array(dev, "memory-map", reg, 2);
252 return log_msg_ret("Cannot find memory map\n", ret);
253 entry->base = reg[0];
256 /* Find the place where we put the MRC cache */
257 mrc_node = dev_read_subnode(dev, type == MRC_TYPE_NORMAL ?
258 "rw-mrc-cache" : "rw-var-mrc-cache");
259 if (!ofnode_valid(mrc_node))
260 return log_msg_ret("Cannot find node", -EPERM);
262 ret = ofnode_read_u32_array(mrc_node, "reg", reg, 2);
264 return log_msg_ret("Cannot find address", ret);
265 entry->offset = reg[0];
266 entry->length = reg[1];
270 debug("MRC cache type %d in '%s', offset %x, len %x, base %x\n",
271 type, dev->name, entry->offset, entry->length, entry->base);
276 static int mrccache_save_type(enum mrc_type_t type)
278 struct mrc_data_container *cache;
279 struct mrc_output *mrc;
280 struct mrc_region entry;
284 mrc = &gd->arch.mrc[type];
287 log_debug("Saving %#x bytes of MRC output data type %d to SPI flash\n",
289 ret = mrccache_get_region(type, &sf, &entry);
291 return log_msg_ret("Cannot get region", ret);
292 ret = device_probe(sf);
294 return log_msg_ret("Cannot probe device", ret);
297 ret = mrccache_update(sf, &entry, cache);
299 debug("Saved MRC data with checksum %04x\n", cache->checksum);
300 else if (ret == -EEXIST)
301 debug("MRC data is the same as last time, skipping save\n");
306 int mrccache_save(void)
310 for (i = 0; i < MRC_TYPE_COUNT; i++) {
313 ret = mrccache_save_type(i);
321 int mrccache_spl_save(void)
325 for (i = 0; i < MRC_TYPE_COUNT; i++) {
326 struct mrc_output *mrc = &gd->arch.mrc[i];
330 size = mrc->len + MRC_DATA_HEADER_SIZE;
333 return log_msg_ret("Allocate MRC cache block", -ENOMEM);
334 mrccache_setup(mrc, data);
337 return mrccache_save();