blob: 6494b8d2634189a98ff2a92d55628ac338cef54e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glass428dfa42015-01-19 22:16:14 -07002/*
Bin Meng8575ab12015-10-11 21:37:38 -07003 * From coreboot src/southbridge/intel/bd82x6x/mrccache.c
Simon Glass428dfa42015-01-19 22:16:14 -07004 *
5 * Copyright (C) 2014 Google Inc.
Bin Meng1f81b592015-10-11 21:37:39 -07006 * Copyright (C) 2015 Bin Meng <bmeng.cn@gmail.com>
Simon Glass428dfa42015-01-19 22:16:14 -07007 */
8
Simon Glass3a1d96f2023-07-15 21:39:11 -06009#define LOG_CATEGORY UCLASS_RAM
10
Simon Glass428dfa42015-01-19 22:16:14 -070011#include <common.h>
Bin Meng1f81b592015-10-11 21:37:39 -070012#include <dm.h>
Simon Glass428dfa42015-01-19 22:16:14 -070013#include <errno.h>
14#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <malloc.h>
Simon Glass428dfa42015-01-19 22:16:14 -070017#include <net.h>
18#include <spi.h>
19#include <spi_flash.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Bin Meng21666cf2015-10-11 21:37:36 -070021#include <asm/mrccache.h>
Simon Glass9d25f2e2019-12-06 21:42:03 -070022#include <dm/device-internal.h>
23#include <dm/uclass-internal.h>
Simon Glass428dfa42015-01-19 22:16:14 -070024
Bin Meng1f81b592015-10-11 21:37:39 -070025DECLARE_GLOBAL_DATA_PTR;
26
Simon Glass040bef12019-09-25 08:57:04 -060027static uint mrc_block_size(uint data_size)
28{
29 uint mrc_size = sizeof(struct mrc_data_container) + data_size;
30
31 return ALIGN(mrc_size, MRC_DATA_ALIGN);
32}
33
Simon Glass428dfa42015-01-19 22:16:14 -070034static struct mrc_data_container *next_mrc_block(
Bin Meng8575ab12015-10-11 21:37:38 -070035 struct mrc_data_container *cache)
Simon Glass428dfa42015-01-19 22:16:14 -070036{
37 /* MRC data blocks are aligned within the region */
Bin Meng8575ab12015-10-11 21:37:38 -070038 u8 *region_ptr = (u8 *)cache;
39
Simon Glass040bef12019-09-25 08:57:04 -060040 region_ptr += mrc_block_size(cache->data_size);
Bin Meng8575ab12015-10-11 21:37:38 -070041
Simon Glass428dfa42015-01-19 22:16:14 -070042 return (struct mrc_data_container *)region_ptr;
43}
44
45static int is_mrc_cache(struct mrc_data_container *cache)
46{
47 return cache && (cache->signature == MRC_DATA_SIGNATURE);
48}
49
Bin Meng2845ead2015-10-11 21:37:41 -070050struct mrc_data_container *mrccache_find_current(struct mrc_region *entry)
Simon Glass428dfa42015-01-19 22:16:14 -070051{
52 struct mrc_data_container *cache, *next;
53 ulong base_addr, end_addr;
54 uint id;
55
Bin Meng2845ead2015-10-11 21:37:41 -070056 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -070057 end_addr = base_addr + entry->length;
58 cache = NULL;
59
60 /* Search for the last filled entry in the region */
61 for (id = 0, next = (struct mrc_data_container *)base_addr;
62 is_mrc_cache(next);
63 id++) {
64 cache = next;
65 next = next_mrc_block(next);
66 if ((ulong)next >= end_addr)
67 break;
68 }
69
70 if (id-- == 0) {
71 debug("%s: No valid MRC cache found.\n", __func__);
72 return NULL;
73 }
74
75 /* Verify checksum */
76 if (cache->checksum != compute_ip_checksum(cache->data,
77 cache->data_size)) {
78 printf("%s: MRC cache checksum mismatch\n", __func__);
79 return NULL;
80 }
81
82 debug("%s: picked entry %u from cache block\n", __func__, id);
83
84 return cache;
85}
86
87/**
88 * find_next_mrc_cache() - get next cache entry
89 *
Simon Glassd553f972019-12-06 21:42:02 -070090 * This moves to the next cache entry in the region, making sure it has enough
91 * space to hold data of size @data_size.
92 *
Simon Glass428dfa42015-01-19 22:16:14 -070093 * @entry: MRC cache flash area
94 * @cache: Entry to start from
Simon Glassd553f972019-12-06 21:42:02 -070095 * @data_size: Required data size of the new entry. Note that we assume that
96 * all cache entries are the same size
Simon Glass428dfa42015-01-19 22:16:14 -070097 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010098 * Return: next cache entry if found, NULL if we got to the end
Simon Glass428dfa42015-01-19 22:16:14 -070099 */
Bin Meng2845ead2015-10-11 21:37:41 -0700100static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry,
Simon Glassd553f972019-12-06 21:42:02 -0700101 struct mrc_data_container *prev, int data_size)
Simon Glass428dfa42015-01-19 22:16:14 -0700102{
Simon Glassd553f972019-12-06 21:42:02 -0700103 struct mrc_data_container *cache;
Simon Glass428dfa42015-01-19 22:16:14 -0700104 ulong base_addr, end_addr;
105
Bin Meng2845ead2015-10-11 21:37:41 -0700106 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -0700107 end_addr = base_addr + entry->length;
108
Simon Glassd553f972019-12-06 21:42:02 -0700109 /*
110 * We assume that all cache entries are the same size, but let's use
111 * data_size here for clarity.
112 */
113 cache = next_mrc_block(prev);
114 if ((ulong)cache + mrc_block_size(data_size) > end_addr) {
Simon Glass428dfa42015-01-19 22:16:14 -0700115 /* Crossed the boundary */
116 cache = NULL;
117 debug("%s: no available entries found\n", __func__);
118 } else {
119 debug("%s: picked next entry from cache block at %p\n",
120 __func__, cache);
121 }
122
123 return cache;
124}
125
Simon Glass6e1b9562019-12-06 21:42:09 -0700126/**
127 * mrccache_update() - update the MRC cache with a new record
128 *
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
131 *
132 * @sf: SPI flash to write to
133 * @entry: Position and size of MRC cache in SPI flash
134 * @cur: Record to write
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100135 * Return: 0 if updated, -EEXIST if the record is the same as the latest
Simon Glass6e1b9562019-12-06 21:42:09 -0700136 * record, -EINVAL if the record is not valid, other error if SPI write failed
137 */
138static int mrccache_update(struct udevice *sf, struct mrc_region *entry,
139 struct mrc_data_container *cur)
Simon Glass428dfa42015-01-19 22:16:14 -0700140{
141 struct mrc_data_container *cache;
142 ulong offset;
143 ulong base_addr;
144 int ret;
145
Simon Glassfbef25f2019-04-25 21:58:59 -0600146 if (!is_mrc_cache(cur)) {
147 debug("%s: Cache data not valid\n", __func__);
Bin Mengd61a7b42015-10-11 21:37:37 -0700148 return -EINVAL;
Simon Glassfbef25f2019-04-25 21:58:59 -0600149 }
Bin Mengd61a7b42015-10-11 21:37:37 -0700150
Simon Glass428dfa42015-01-19 22:16:14 -0700151 /* Find the last used block */
Bin Meng2845ead2015-10-11 21:37:41 -0700152 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -0700153 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");
158 return -EEXIST;
159 }
160
161 /* Move to the next block, which will be the first unused block */
162 if (cache)
Simon Glassd553f972019-12-06 21:42:02 -0700163 cache = find_next_mrc_cache(entry, cache, cur->data_size);
Simon Glass428dfa42015-01-19 22:16:14 -0700164
165 /*
166 * If we have got to the end, erase the entire mrc-cache area and start
167 * again at block 0.
168 */
169 if (!cache) {
170 debug("Erasing the MRC cache region of %x bytes at %x\n",
171 entry->length, entry->offset);
172
Simon Glass35f15f62015-03-26 09:29:26 -0600173 ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
Simon Glass428dfa42015-01-19 22:16:14 -0700174 if (ret) {
175 debug("Failed to erase flash region\n");
176 return ret;
177 }
178 cache = (struct mrc_data_container *)base_addr;
179 }
180
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);
Simon Glass35f15f62015-03-26 09:29:26 -0600184 ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
185 cur);
Simon Glass428dfa42015-01-19 22:16:14 -0700186 if (ret) {
187 debug("Failed to write to SPI flash\n");
Simon Glass1b9d8152019-12-06 21:42:06 -0700188 return log_msg_ret("Cannot update mrccache", ret);
Simon Glass428dfa42015-01-19 22:16:14 -0700189 }
190
191 return 0;
192}
Bin Meng1f81b592015-10-11 21:37:39 -0700193
Simon Glass91efff52019-12-06 21:42:07 -0700194static void mrccache_setup(struct mrc_output *mrc, void *data)
Bin Meng1f81b592015-10-11 21:37:39 -0700195{
Simon Glass48fd8562019-04-25 21:58:57 -0600196 struct mrc_data_container *cache = data;
Bin Meng1f81b592015-10-11 21:37:39 -0700197 u16 checksum;
198
Bin Meng1f81b592015-10-11 21:37:39 -0700199 cache->signature = MRC_DATA_SIGNATURE;
Simon Glass91efff52019-12-06 21:42:07 -0700200 cache->data_size = mrc->len;
201 checksum = compute_ip_checksum(mrc->buf, cache->data_size);
Simon Glass3a1d96f2023-07-15 21:39:11 -0600202 log_debug("Saving %d bytes for MRC output data, checksum %04x\n",
203 cache->data_size, checksum);
Bin Meng1f81b592015-10-11 21:37:39 -0700204 cache->checksum = checksum;
205 cache->reserved = 0;
Simon Glass91efff52019-12-06 21:42:07 -0700206 memcpy(cache->data, mrc->buf, cache->data_size);
Bin Meng1f81b592015-10-11 21:37:39 -0700207
Simon Glass91efff52019-12-06 21:42:07 -0700208 mrc->cache = cache;
Simon Glass48fd8562019-04-25 21:58:57 -0600209}
210
211int mrccache_reserve(void)
212{
Simon Glass91efff52019-12-06 21:42:07 -0700213 int i;
Simon Glass48fd8562019-04-25 21:58:57 -0600214
Simon Glass91efff52019-12-06 21:42:07 -0700215 for (i = 0; i < MRC_TYPE_COUNT; i++) {
216 struct mrc_output *mrc = &gd->arch.mrc[i];
Bin Meng1f81b592015-10-11 21:37:39 -0700217
Simon Glass91efff52019-12-06 21:42:07 -0700218 if (!mrc->len)
219 continue;
220
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);
224
225 gd->start_addr_sp &= ~0xf;
226 }
Bin Meng1f81b592015-10-11 21:37:39 -0700227
228 return 0;
229}
230
Simon Glass91efff52019-12-06 21:42:07 -0700231int mrccache_get_region(enum mrc_type_t type, struct udevice **devp,
232 struct mrc_region *entry)
Bin Meng1f81b592015-10-11 21:37:39 -0700233{
Simon Glass9d25f2e2019-12-06 21:42:03 -0700234 struct udevice *dev;
235 ofnode mrc_node;
Simon Glass4e988f92019-12-06 21:42:04 -0700236 ulong map_base;
237 uint map_size;
238 uint offset;
Simon Glass00bf2792020-05-27 06:58:49 -0600239 ofnode node;
Bin Meng2845ead2015-10-11 21:37:41 -0700240 u32 reg[2];
Bin Meng1f81b592015-10-11 21:37:39 -0700241 int ret;
242
Simon Glass9d25f2e2019-12-06 21:42:03 -0700243 /*
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.
247 */
248 ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
Simon Glass00bf2792020-05-27 06:58:49 -0600249 if (ret || !dev) {
250 /*
251 * Fall back to searching the device tree since driver model
252 * may not be ready yet (e.g. with FSPv1)
253 */
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);
Simon Glass92f98282020-02-02 13:37:06 -0700257 ret = -ENODEV;
Simon Glass4e988f92019-12-06 21:42:04 -0700258 } else {
Simon Glass00bf2792020-05-27 06:58:49 -0600259 ret = dm_spi_get_mmap(dev, &map_base, &map_size, &offset);
260 if (!ret)
261 entry->base = map_base;
262 node = dev_ofnode(dev);
263 }
264
265 /*
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
268 */
269 if (ret) {
270 ret = ofnode_read_u32_array(node, "memory-map", reg, 2);
Simon Glass4e988f92019-12-06 21:42:04 -0700271 if (ret)
272 return log_msg_ret("Cannot find memory map\n", ret);
273 entry->base = reg[0];
274 }
Bin Meng2845ead2015-10-11 21:37:41 -0700275
Bin Meng1f81b592015-10-11 21:37:39 -0700276 /* Find the place where we put the MRC cache */
Simon Glass00bf2792020-05-27 06:58:49 -0600277 mrc_node = ofnode_find_subnode(node, type == MRC_TYPE_NORMAL ?
278 "rw-mrc-cache" : "rw-var-mrc-cache");
Simon Glass9d25f2e2019-12-06 21:42:03 -0700279 if (!ofnode_valid(mrc_node))
280 return log_msg_ret("Cannot find node", -EPERM);
Bin Meng1f81b592015-10-11 21:37:39 -0700281
Simon Glass9d25f2e2019-12-06 21:42:03 -0700282 ret = ofnode_read_u32_array(mrc_node, "reg", reg, 2);
283 if (ret)
284 return log_msg_ret("Cannot find address", ret);
Bin Meng2845ead2015-10-11 21:37:41 -0700285 entry->offset = reg[0];
286 entry->length = reg[1];
Bin Meng1f81b592015-10-11 21:37:39 -0700287
Simon Glass9d25f2e2019-12-06 21:42:03 -0700288 if (devp)
289 *devp = dev;
Simon Glass91efff52019-12-06 21:42:07 -0700290 debug("MRC cache type %d in '%s', offset %x, len %x, base %x\n",
Simon Glass00bf2792020-05-27 06:58:49 -0600291 type, dev ? dev->name : ofnode_get_name(node), entry->offset,
292 entry->length, entry->base);
Bin Meng1f81b592015-10-11 21:37:39 -0700293
294 return 0;
295}
296
Simon Glass91efff52019-12-06 21:42:07 -0700297static int mrccache_save_type(enum mrc_type_t type)
Bin Meng1f81b592015-10-11 21:37:39 -0700298{
Simon Glassc3d0c232019-12-06 21:42:05 -0700299 struct mrc_data_container *cache;
Simon Glass91efff52019-12-06 21:42:07 -0700300 struct mrc_output *mrc;
Bin Meng2845ead2015-10-11 21:37:41 -0700301 struct mrc_region entry;
Bin Meng1f81b592015-10-11 21:37:39 -0700302 struct udevice *sf;
303 int ret;
304
Simon Glass91efff52019-12-06 21:42:07 -0700305 mrc = &gd->arch.mrc[type];
306 if (!mrc->len)
Bin Meng1f81b592015-10-11 21:37:39 -0700307 return 0;
Simon Glassfdbc4422023-05-04 16:50:53 -0600308 log_debug("Saving %x bytes of MRC output data type %d to SPI flash\n",
Simon Glass91efff52019-12-06 21:42:07 -0700309 mrc->len, type);
310 ret = mrccache_get_region(type, &sf, &entry);
Bin Meng1f81b592015-10-11 21:37:39 -0700311 if (ret)
Simon Glass1b9d8152019-12-06 21:42:06 -0700312 return log_msg_ret("Cannot get region", ret);
Simon Glass9d25f2e2019-12-06 21:42:03 -0700313 ret = device_probe(sf);
314 if (ret)
Simon Glass1b9d8152019-12-06 21:42:06 -0700315 return log_msg_ret("Cannot probe device", ret);
Simon Glass91efff52019-12-06 21:42:07 -0700316 cache = mrc->cache;
317
Simon Glassc3d0c232019-12-06 21:42:05 -0700318 ret = mrccache_update(sf, &entry, cache);
Simon Glass1b9d8152019-12-06 21:42:06 -0700319 if (!ret)
Simon Glassc3d0c232019-12-06 21:42:05 -0700320 debug("Saved MRC data with checksum %04x\n", cache->checksum);
Simon Glass1b9d8152019-12-06 21:42:06 -0700321 else if (ret == -EEXIST)
Simon Glass9df244f2016-01-17 16:11:29 -0700322 debug("MRC data is the same as last time, skipping save\n");
Bin Meng1f81b592015-10-11 21:37:39 -0700323
Simon Glass1b9d8152019-12-06 21:42:06 -0700324 return 0;
Bin Meng1f81b592015-10-11 21:37:39 -0700325}
Simon Glass48fd8562019-04-25 21:58:57 -0600326
Simon Glass91efff52019-12-06 21:42:07 -0700327int mrccache_save(void)
328{
329 int i;
330
331 for (i = 0; i < MRC_TYPE_COUNT; i++) {
332 int ret;
333
334 ret = mrccache_save_type(i);
335 if (ret)
336 return ret;
337 }
338
339 return 0;
340}
341
Simon Glass48fd8562019-04-25 21:58:57 -0600342int mrccache_spl_save(void)
343{
Simon Glass91efff52019-12-06 21:42:07 -0700344 int i;
Simon Glass48fd8562019-04-25 21:58:57 -0600345
Simon Glass91efff52019-12-06 21:42:07 -0700346 for (i = 0; i < MRC_TYPE_COUNT; i++) {
347 struct mrc_output *mrc = &gd->arch.mrc[i];
348 void *data;
349 int size;
350
351 size = mrc->len + MRC_DATA_HEADER_SIZE;
352 data = malloc(size);
353 if (!data)
354 return log_msg_ret("Allocate MRC cache block", -ENOMEM);
355 mrccache_setup(mrc, data);
356 }
Simon Glass48fd8562019-04-25 21:58:57 -0600357
358 return mrccache_save();
359}