1 // SPDX-License-Identifier: GPL-2.0
3 * From coreboot src/southbridge/intel/bd82x6x/mrccache.c
5 * Copyright (C) 2014 Google Inc.
6 * Copyright (C) 2015 Bin Meng <bmeng.cn@gmail.com>
17 #include <spi_flash.h>
18 #include <asm/global_data.h>
19 #include <asm/mrccache.h>
20 #include <dm/device-internal.h>
21 #include <dm/uclass-internal.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 static uint mrc_block_size(uint data_size)
27 uint mrc_size = sizeof(struct mrc_data_container) + data_size;
29 return ALIGN(mrc_size, MRC_DATA_ALIGN);
32 static struct mrc_data_container *next_mrc_block(
33 struct mrc_data_container *cache)
35 /* MRC data blocks are aligned within the region */
36 u8 *region_ptr = (u8 *)cache;
38 region_ptr += mrc_block_size(cache->data_size);
40 return (struct mrc_data_container *)region_ptr;
43 static int is_mrc_cache(struct mrc_data_container *cache)
45 return cache && (cache->signature == MRC_DATA_SIGNATURE);
48 struct mrc_data_container *mrccache_find_current(struct mrc_region *entry)
50 struct mrc_data_container *cache, *next;
51 ulong base_addr, end_addr;
54 base_addr = entry->base + entry->offset;
55 end_addr = base_addr + entry->length;
58 /* Search for the last filled entry in the region */
59 for (id = 0, next = (struct mrc_data_container *)base_addr;
63 next = next_mrc_block(next);
64 if ((ulong)next >= end_addr)
69 debug("%s: No valid MRC cache found.\n", __func__);
74 if (cache->checksum != compute_ip_checksum(cache->data,
76 printf("%s: MRC cache checksum mismatch\n", __func__);
80 debug("%s: picked entry %u from cache block\n", __func__, id);
86 * find_next_mrc_cache() - get next cache entry
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.
91 * @entry: MRC cache flash area
92 * @cache: Entry to start from
93 * @data_size: Required data size of the new entry. Note that we assume that
94 * all cache entries are the same size
96 * @return next cache entry if found, NULL if we got to the end
98 static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry,
99 struct mrc_data_container *prev, int data_size)
101 struct mrc_data_container *cache;
102 ulong base_addr, end_addr;
104 base_addr = entry->base + entry->offset;
105 end_addr = base_addr + entry->length;
108 * We assume that all cache entries are the same size, but let's use
109 * data_size here for clarity.
111 cache = next_mrc_block(prev);
112 if ((ulong)cache + mrc_block_size(data_size) > end_addr) {
113 /* Crossed the boundary */
115 debug("%s: no available entries found\n", __func__);
117 debug("%s: picked next entry from cache block at %p\n",
125 * mrccache_update() - update the MRC cache with a new record
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
130 * @sf: SPI flash to write to
131 * @entry: Position and size of MRC cache in SPI flash
132 * @cur: Record to write
133 * @return 0 if updated, -EEXIST if the record is the same as the latest
134 * record, -EINVAL if the record is not valid, other error if SPI write failed
136 static int mrccache_update(struct udevice *sf, struct mrc_region *entry,
137 struct mrc_data_container *cur)
139 struct mrc_data_container *cache;
144 if (!is_mrc_cache(cur)) {
145 debug("%s: Cache data not valid\n", __func__);
149 /* Find the last used block */
150 base_addr = entry->base + entry->offset;
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");
159 /* Move to the next block, which will be the first unused block */
161 cache = find_next_mrc_cache(entry, cache, cur->data_size);
164 * If we have got to the end, erase the entire mrc-cache area and start
168 debug("Erasing the MRC cache region of %x bytes at %x\n",
169 entry->length, entry->offset);
171 ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
173 debug("Failed to erase flash region\n");
176 cache = (struct mrc_data_container *)base_addr;
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);
182 ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
185 debug("Failed to write to SPI flash\n");
186 return log_msg_ret("Cannot update mrccache", ret);
192 static void mrccache_setup(struct mrc_output *mrc, void *data)
194 struct mrc_data_container *cache = data;
197 cache->signature = MRC_DATA_SIGNATURE;
198 cache->data_size = mrc->len;
199 checksum = compute_ip_checksum(mrc->buf, cache->data_size);
200 debug("Saving %d bytes for MRC output data, checksum %04x\n",
201 cache->data_size, checksum);
202 cache->checksum = checksum;
204 memcpy(cache->data, mrc->buf, cache->data_size);
209 int mrccache_reserve(void)
213 for (i = 0; i < MRC_TYPE_COUNT; i++) {
214 struct mrc_output *mrc = &gd->arch.mrc[i];
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);
223 gd->start_addr_sp &= ~0xf;
229 int mrccache_get_region(enum mrc_type_t type, struct udevice **devp,
230 struct mrc_region *entry)
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.
246 ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
249 * Fall back to searching the device tree since driver model
250 * may not be ready yet (e.g. with FSPv1)
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);
257 ret = dm_spi_get_mmap(dev, &map_base, &map_size, &offset);
259 entry->base = map_base;
260 node = dev_ofnode(dev);
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
268 ret = ofnode_read_u32_array(node, "memory-map", reg, 2);
270 return log_msg_ret("Cannot find memory map\n", ret);
271 entry->base = reg[0];
274 /* Find the place where we put the MRC cache */
275 mrc_node = ofnode_find_subnode(node, type == MRC_TYPE_NORMAL ?
276 "rw-mrc-cache" : "rw-var-mrc-cache");
277 if (!ofnode_valid(mrc_node))
278 return log_msg_ret("Cannot find node", -EPERM);
280 ret = ofnode_read_u32_array(mrc_node, "reg", reg, 2);
282 return log_msg_ret("Cannot find address", ret);
283 entry->offset = reg[0];
284 entry->length = reg[1];
288 debug("MRC cache type %d in '%s', offset %x, len %x, base %x\n",
289 type, dev ? dev->name : ofnode_get_name(node), entry->offset,
290 entry->length, entry->base);
295 static int mrccache_save_type(enum mrc_type_t type)
297 struct mrc_data_container *cache;
298 struct mrc_output *mrc;
299 struct mrc_region entry;
303 mrc = &gd->arch.mrc[type];
306 log_debug("Saving %#x bytes of MRC output data type %d to SPI flash\n",
308 ret = mrccache_get_region(type, &sf, &entry);
310 return log_msg_ret("Cannot get region", ret);
311 ret = device_probe(sf);
313 return log_msg_ret("Cannot probe device", ret);
316 ret = mrccache_update(sf, &entry, cache);
318 debug("Saved MRC data with checksum %04x\n", cache->checksum);
319 else if (ret == -EEXIST)
320 debug("MRC data is the same as last time, skipping save\n");
325 int mrccache_save(void)
329 for (i = 0; i < MRC_TYPE_COUNT; i++) {
332 ret = mrccache_save_type(i);
340 int mrccache_spl_save(void)
344 for (i = 0; i < MRC_TYPE_COUNT; i++) {
345 struct mrc_output *mrc = &gd->arch.mrc[i];
349 size = mrc->len + MRC_DATA_HEADER_SIZE;
352 return log_msg_ret("Allocate MRC cache block", -ENOMEM);
353 mrccache_setup(mrc, data);
356 return mrccache_save();