1 // SPDX-License-Identifier: GPL-2.0
3 * From coreboot src/southbridge/intel/bd82x6x/mrccache.c
5 * Copyright (C) 2014 Google Inc.
9 #define LOG_CATEGORY UCLASS_RAM
19 #include <spi_flash.h>
20 #include <asm/global_data.h>
21 #include <asm/mrccache.h>
22 #include <dm/device-internal.h>
23 #include <dm/uclass-internal.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 static uint mrc_block_size(uint data_size)
29 uint mrc_size = sizeof(struct mrc_data_container) + data_size;
31 return ALIGN(mrc_size, MRC_DATA_ALIGN);
34 static struct mrc_data_container *next_mrc_block(
35 struct mrc_data_container *cache)
37 /* MRC data blocks are aligned within the region */
38 u8 *region_ptr = (u8 *)cache;
40 region_ptr += mrc_block_size(cache->data_size);
42 return (struct mrc_data_container *)region_ptr;
45 static int is_mrc_cache(struct mrc_data_container *cache)
47 return cache && (cache->signature == MRC_DATA_SIGNATURE);
50 struct mrc_data_container *mrccache_find_current(struct mrc_region *entry)
52 struct mrc_data_container *cache, *next;
53 ulong base_addr, end_addr;
56 base_addr = entry->base + entry->offset;
57 end_addr = base_addr + entry->length;
60 /* Search for the last filled entry in the region */
61 for (id = 0, next = (struct mrc_data_container *)base_addr;
65 next = next_mrc_block(next);
66 if ((ulong)next >= end_addr)
71 debug("%s: No valid MRC cache found.\n", __func__);
76 if (cache->checksum != compute_ip_checksum(cache->data,
78 printf("%s: MRC cache checksum mismatch\n", __func__);
82 debug("%s: picked entry %u from cache block\n", __func__, id);
88 * find_next_mrc_cache() - get next cache entry
90 * This moves to the next cache entry in the region, making sure it has enough
91 * space to hold data of size @data_size.
93 * @entry: MRC cache flash area
94 * @cache: Entry to start from
95 * @data_size: Required data size of the new entry. Note that we assume that
96 * all cache entries are the same size
98 * Return: next cache entry if found, NULL if we got to the end
100 static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry,
101 struct mrc_data_container *prev, int data_size)
103 struct mrc_data_container *cache;
104 ulong base_addr, end_addr;
106 base_addr = entry->base + entry->offset;
107 end_addr = base_addr + entry->length;
110 * We assume that all cache entries are the same size, but let's use
111 * data_size here for clarity.
113 cache = next_mrc_block(prev);
114 if ((ulong)cache + mrc_block_size(data_size) > end_addr) {
115 /* Crossed the boundary */
117 debug("%s: no available entries found\n", __func__);
119 debug("%s: picked next entry from cache block at %p\n",
127 * mrccache_update() - update the MRC cache with a new record
129 * This writes a new record to the end of the MRC cache region. If the new
130 * record is the same as the latest record then the write is skipped
132 * @sf: SPI flash to write to
133 * @entry: Position and size of MRC cache in SPI flash
134 * @cur: Record to write
135 * Return: 0 if updated, -EEXIST if the record is the same as the latest
136 * record, -EINVAL if the record is not valid, other error if SPI write failed
138 static int mrccache_update(struct udevice *sf, struct mrc_region *entry,
139 struct mrc_data_container *cur)
141 struct mrc_data_container *cache;
146 if (!is_mrc_cache(cur)) {
147 debug("%s: Cache data not valid\n", __func__);
151 /* Find the last used block */
152 base_addr = entry->base + entry->offset;
153 debug("Updating MRC cache data\n");
154 cache = mrccache_find_current(entry);
155 if (cache && (cache->data_size == cur->data_size) &&
156 (!memcmp(cache, cur, cache->data_size + sizeof(*cur)))) {
157 debug("MRC data in flash is up to date. No update\n");
161 /* Move to the next block, which will be the first unused block */
163 cache = find_next_mrc_cache(entry, cache, cur->data_size);
166 * If we have got to the end, erase the entire mrc-cache area and start
170 debug("Erasing the MRC cache region of %x bytes at %x\n",
171 entry->length, entry->offset);
173 ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
175 debug("Failed to erase flash region\n");
178 cache = (struct mrc_data_container *)base_addr;
181 /* Write the data out */
182 offset = (ulong)cache - base_addr + entry->offset;
183 debug("Write MRC cache update to flash at %lx\n", offset);
184 ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
187 debug("Failed to write to SPI flash\n");
188 return log_msg_ret("Cannot update mrccache", ret);
194 static void mrccache_setup(struct mrc_output *mrc, void *data)
196 struct mrc_data_container *cache = data;
199 cache->signature = MRC_DATA_SIGNATURE;
200 cache->data_size = mrc->len;
201 checksum = compute_ip_checksum(mrc->buf, cache->data_size);
202 log_debug("Saving %d bytes for MRC output data, checksum %04x\n",
203 cache->data_size, checksum);
204 cache->checksum = checksum;
206 memcpy(cache->data, mrc->buf, cache->data_size);
211 int mrccache_reserve(void)
215 for (i = 0; i < MRC_TYPE_COUNT; i++) {
216 struct mrc_output *mrc = &gd->arch.mrc[i];
221 /* adjust stack pointer to store pure cache data plus header */
222 gd->start_addr_sp -= (mrc->len + MRC_DATA_HEADER_SIZE);
223 mrccache_setup(mrc, (void *)gd->start_addr_sp);
225 gd->start_addr_sp &= ~0xf;
231 int mrccache_get_region(enum mrc_type_t type, struct udevice **devp,
232 struct mrc_region *entry)
244 * Find the flash chip within the SPI controller node. Avoid probing
245 * the device here since it may put it into a strange state where the
246 * memory map cannot be read.
248 ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
251 * Fall back to searching the device tree since driver model
252 * may not be ready yet (e.g. with FSPv1)
254 node = ofnode_by_compatible(ofnode_null(), "jedec,spi-nor");
255 if (!ofnode_valid(node))
256 return log_msg_ret("Cannot find SPI flash\n", -ENOENT);
259 ret = dm_spi_get_mmap(dev, &map_base, &map_size, &offset);
261 entry->base = map_base;
262 node = dev_ofnode(dev);
266 * At this point we have entry->base if ret == 0. If not, then we have
267 * the node and can look for memory-map
270 ret = ofnode_read_u32_array(node, "memory-map", reg, 2);
272 return log_msg_ret("Cannot find memory map\n", ret);
273 entry->base = reg[0];
276 /* Find the place where we put the MRC cache */
277 mrc_node = ofnode_find_subnode(node, type == MRC_TYPE_NORMAL ?
278 "rw-mrc-cache" : "rw-var-mrc-cache");
279 if (!ofnode_valid(mrc_node))
280 return log_msg_ret("Cannot find node", -EPERM);
282 ret = ofnode_read_u32_array(mrc_node, "reg", reg, 2);
284 return log_msg_ret("Cannot find address", ret);
285 entry->offset = reg[0];
286 entry->length = reg[1];
290 debug("MRC cache type %d in '%s', offset %x, len %x, base %x\n",
291 type, dev ? dev->name : ofnode_get_name(node), entry->offset,
292 entry->length, entry->base);
297 static int mrccache_save_type(enum mrc_type_t type)
299 struct mrc_data_container *cache;
300 struct mrc_output *mrc;
301 struct mrc_region entry;
305 mrc = &gd->arch.mrc[type];
308 log_debug("Saving %x bytes of MRC output data type %d to SPI flash\n",
310 ret = mrccache_get_region(type, &sf, &entry);
312 return log_msg_ret("Cannot get region", ret);
313 ret = device_probe(sf);
315 return log_msg_ret("Cannot probe device", ret);
318 ret = mrccache_update(sf, &entry, cache);
320 debug("Saved MRC data with checksum %04x\n", cache->checksum);
321 else if (ret == -EEXIST)
322 debug("MRC data is the same as last time, skipping save\n");
327 int mrccache_save(void)
331 for (i = 0; i < MRC_TYPE_COUNT; i++) {
334 ret = mrccache_save_type(i);
342 int mrccache_spl_save(void)
346 for (i = 0; i < MRC_TYPE_COUNT; i++) {
347 struct mrc_output *mrc = &gd->arch.mrc[i];
351 size = mrc->len + MRC_DATA_HEADER_SIZE;
354 return log_msg_ret("Allocate MRC cache block", -ENOMEM);
355 mrccache_setup(mrc, data);
358 return mrccache_save();