blob: f181e8100cb77383120ef583d40537b747fcef89 [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
9#include <common.h>
Bin Meng1f81b592015-10-11 21:37:39 -070010#include <dm.h>
Simon Glass428dfa42015-01-19 22:16:14 -070011#include <errno.h>
12#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Simon Glass428dfa42015-01-19 22:16:14 -070015#include <net.h>
16#include <spi.h>
17#include <spi_flash.h>
Bin Meng21666cf2015-10-11 21:37:36 -070018#include <asm/mrccache.h>
Simon Glass9d25f2e2019-12-06 21:42:03 -070019#include <dm/device-internal.h>
20#include <dm/uclass-internal.h>
Simon Glass428dfa42015-01-19 22:16:14 -070021
Bin Meng1f81b592015-10-11 21:37:39 -070022DECLARE_GLOBAL_DATA_PTR;
23
Simon Glass040bef12019-09-25 08:57:04 -060024static uint mrc_block_size(uint data_size)
25{
26 uint mrc_size = sizeof(struct mrc_data_container) + data_size;
27
28 return ALIGN(mrc_size, MRC_DATA_ALIGN);
29}
30
Simon Glass428dfa42015-01-19 22:16:14 -070031static struct mrc_data_container *next_mrc_block(
Bin Meng8575ab12015-10-11 21:37:38 -070032 struct mrc_data_container *cache)
Simon Glass428dfa42015-01-19 22:16:14 -070033{
34 /* MRC data blocks are aligned within the region */
Bin Meng8575ab12015-10-11 21:37:38 -070035 u8 *region_ptr = (u8 *)cache;
36
Simon Glass040bef12019-09-25 08:57:04 -060037 region_ptr += mrc_block_size(cache->data_size);
Bin Meng8575ab12015-10-11 21:37:38 -070038
Simon Glass428dfa42015-01-19 22:16:14 -070039 return (struct mrc_data_container *)region_ptr;
40}
41
42static int is_mrc_cache(struct mrc_data_container *cache)
43{
44 return cache && (cache->signature == MRC_DATA_SIGNATURE);
45}
46
Bin Meng2845ead2015-10-11 21:37:41 -070047struct mrc_data_container *mrccache_find_current(struct mrc_region *entry)
Simon Glass428dfa42015-01-19 22:16:14 -070048{
49 struct mrc_data_container *cache, *next;
50 ulong base_addr, end_addr;
51 uint id;
52
Bin Meng2845ead2015-10-11 21:37:41 -070053 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -070054 end_addr = base_addr + entry->length;
55 cache = NULL;
56
57 /* Search for the last filled entry in the region */
58 for (id = 0, next = (struct mrc_data_container *)base_addr;
59 is_mrc_cache(next);
60 id++) {
61 cache = next;
62 next = next_mrc_block(next);
63 if ((ulong)next >= end_addr)
64 break;
65 }
66
67 if (id-- == 0) {
68 debug("%s: No valid MRC cache found.\n", __func__);
69 return NULL;
70 }
71
72 /* Verify checksum */
73 if (cache->checksum != compute_ip_checksum(cache->data,
74 cache->data_size)) {
75 printf("%s: MRC cache checksum mismatch\n", __func__);
76 return NULL;
77 }
78
79 debug("%s: picked entry %u from cache block\n", __func__, id);
80
81 return cache;
82}
83
84/**
85 * find_next_mrc_cache() - get next cache entry
86 *
Simon Glassd553f972019-12-06 21:42:02 -070087 * This moves to the next cache entry in the region, making sure it has enough
88 * space to hold data of size @data_size.
89 *
Simon Glass428dfa42015-01-19 22:16:14 -070090 * @entry: MRC cache flash area
91 * @cache: Entry to start from
Simon Glassd553f972019-12-06 21:42:02 -070092 * @data_size: Required data size of the new entry. Note that we assume that
93 * all cache entries are the same size
Simon Glass428dfa42015-01-19 22:16:14 -070094 *
95 * @return next cache entry if found, NULL if we got to the end
96 */
Bin Meng2845ead2015-10-11 21:37:41 -070097static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry,
Simon Glassd553f972019-12-06 21:42:02 -070098 struct mrc_data_container *prev, int data_size)
Simon Glass428dfa42015-01-19 22:16:14 -070099{
Simon Glassd553f972019-12-06 21:42:02 -0700100 struct mrc_data_container *cache;
Simon Glass428dfa42015-01-19 22:16:14 -0700101 ulong base_addr, end_addr;
102
Bin Meng2845ead2015-10-11 21:37:41 -0700103 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -0700104 end_addr = base_addr + entry->length;
105
Simon Glassd553f972019-12-06 21:42:02 -0700106 /*
107 * We assume that all cache entries are the same size, but let's use
108 * data_size here for clarity.
109 */
110 cache = next_mrc_block(prev);
111 if ((ulong)cache + mrc_block_size(data_size) > end_addr) {
Simon Glass428dfa42015-01-19 22:16:14 -0700112 /* Crossed the boundary */
113 cache = NULL;
114 debug("%s: no available entries found\n", __func__);
115 } else {
116 debug("%s: picked next entry from cache block at %p\n",
117 __func__, cache);
118 }
119
120 return cache;
121}
122
Simon Glass6e1b9562019-12-06 21:42:09 -0700123/**
124 * mrccache_update() - update the MRC cache with a new record
125 *
126 * This writes a new record to the end of the MRC cache region. If the new
127 * record is the same as the latest record then the write is skipped
128 *
129 * @sf: SPI flash to write to
130 * @entry: Position and size of MRC cache in SPI flash
131 * @cur: Record to write
132 * @return 0 if updated, -EEXIST if the record is the same as the latest
133 * record, -EINVAL if the record is not valid, other error if SPI write failed
134 */
135static int mrccache_update(struct udevice *sf, struct mrc_region *entry,
136 struct mrc_data_container *cur)
Simon Glass428dfa42015-01-19 22:16:14 -0700137{
138 struct mrc_data_container *cache;
139 ulong offset;
140 ulong base_addr;
141 int ret;
142
Simon Glassfbef25f2019-04-25 21:58:59 -0600143 if (!is_mrc_cache(cur)) {
144 debug("%s: Cache data not valid\n", __func__);
Bin Mengd61a7b42015-10-11 21:37:37 -0700145 return -EINVAL;
Simon Glassfbef25f2019-04-25 21:58:59 -0600146 }
Bin Mengd61a7b42015-10-11 21:37:37 -0700147
Simon Glass428dfa42015-01-19 22:16:14 -0700148 /* Find the last used block */
Bin Meng2845ead2015-10-11 21:37:41 -0700149 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -0700150 debug("Updating MRC cache data\n");
151 cache = mrccache_find_current(entry);
152 if (cache && (cache->data_size == cur->data_size) &&
153 (!memcmp(cache, cur, cache->data_size + sizeof(*cur)))) {
154 debug("MRC data in flash is up to date. No update\n");
155 return -EEXIST;
156 }
157
158 /* Move to the next block, which will be the first unused block */
159 if (cache)
Simon Glassd553f972019-12-06 21:42:02 -0700160 cache = find_next_mrc_cache(entry, cache, cur->data_size);
Simon Glass428dfa42015-01-19 22:16:14 -0700161
162 /*
163 * If we have got to the end, erase the entire mrc-cache area and start
164 * again at block 0.
165 */
166 if (!cache) {
167 debug("Erasing the MRC cache region of %x bytes at %x\n",
168 entry->length, entry->offset);
169
Simon Glass35f15f62015-03-26 09:29:26 -0600170 ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
Simon Glass428dfa42015-01-19 22:16:14 -0700171 if (ret) {
172 debug("Failed to erase flash region\n");
173 return ret;
174 }
175 cache = (struct mrc_data_container *)base_addr;
176 }
177
178 /* Write the data out */
179 offset = (ulong)cache - base_addr + entry->offset;
180 debug("Write MRC cache update to flash at %lx\n", offset);
Simon Glass35f15f62015-03-26 09:29:26 -0600181 ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
182 cur);
Simon Glass428dfa42015-01-19 22:16:14 -0700183 if (ret) {
184 debug("Failed to write to SPI flash\n");
Simon Glass1b9d8152019-12-06 21:42:06 -0700185 return log_msg_ret("Cannot update mrccache", ret);
Simon Glass428dfa42015-01-19 22:16:14 -0700186 }
187
188 return 0;
189}
Bin Meng1f81b592015-10-11 21:37:39 -0700190
Simon Glass91efff52019-12-06 21:42:07 -0700191static void mrccache_setup(struct mrc_output *mrc, void *data)
Bin Meng1f81b592015-10-11 21:37:39 -0700192{
Simon Glass48fd8562019-04-25 21:58:57 -0600193 struct mrc_data_container *cache = data;
Bin Meng1f81b592015-10-11 21:37:39 -0700194 u16 checksum;
195
Bin Meng1f81b592015-10-11 21:37:39 -0700196 cache->signature = MRC_DATA_SIGNATURE;
Simon Glass91efff52019-12-06 21:42:07 -0700197 cache->data_size = mrc->len;
198 checksum = compute_ip_checksum(mrc->buf, cache->data_size);
Bin Meng1f81b592015-10-11 21:37:39 -0700199 debug("Saving %d bytes for MRC output data, checksum %04x\n",
200 cache->data_size, checksum);
201 cache->checksum = checksum;
202 cache->reserved = 0;
Simon Glass91efff52019-12-06 21:42:07 -0700203 memcpy(cache->data, mrc->buf, cache->data_size);
Bin Meng1f81b592015-10-11 21:37:39 -0700204
Simon Glass91efff52019-12-06 21:42:07 -0700205 mrc->cache = cache;
Simon Glass48fd8562019-04-25 21:58:57 -0600206}
207
208int mrccache_reserve(void)
209{
Simon Glass91efff52019-12-06 21:42:07 -0700210 int i;
Simon Glass48fd8562019-04-25 21:58:57 -0600211
Simon Glass91efff52019-12-06 21:42:07 -0700212 for (i = 0; i < MRC_TYPE_COUNT; i++) {
213 struct mrc_output *mrc = &gd->arch.mrc[i];
Bin Meng1f81b592015-10-11 21:37:39 -0700214
Simon Glass91efff52019-12-06 21:42:07 -0700215 if (!mrc->len)
216 continue;
217
218 /* adjust stack pointer to store pure cache data plus header */
219 gd->start_addr_sp -= (mrc->len + MRC_DATA_HEADER_SIZE);
220 mrccache_setup(mrc, (void *)gd->start_addr_sp);
221
222 gd->start_addr_sp &= ~0xf;
223 }
Bin Meng1f81b592015-10-11 21:37:39 -0700224
225 return 0;
226}
227
Simon Glass91efff52019-12-06 21:42:07 -0700228int mrccache_get_region(enum mrc_type_t type, struct udevice **devp,
229 struct mrc_region *entry)
Bin Meng1f81b592015-10-11 21:37:39 -0700230{
Simon Glass9d25f2e2019-12-06 21:42:03 -0700231 struct udevice *dev;
232 ofnode mrc_node;
Simon Glass4e988f92019-12-06 21:42:04 -0700233 ulong map_base;
234 uint map_size;
235 uint offset;
Simon Glass00bf2792020-05-27 06:58:49 -0600236 ofnode node;
Bin Meng2845ead2015-10-11 21:37:41 -0700237 u32 reg[2];
Bin Meng1f81b592015-10-11 21:37:39 -0700238 int ret;
239
Simon Glass9d25f2e2019-12-06 21:42:03 -0700240 /*
241 * Find the flash chip within the SPI controller node. Avoid probing
242 * the device here since it may put it into a strange state where the
243 * memory map cannot be read.
244 */
245 ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
Simon Glass00bf2792020-05-27 06:58:49 -0600246 if (ret || !dev) {
247 /*
248 * Fall back to searching the device tree since driver model
249 * may not be ready yet (e.g. with FSPv1)
250 */
251 node = ofnode_by_compatible(ofnode_null(), "jedec,spi-nor");
252 if (!ofnode_valid(node))
253 return log_msg_ret("Cannot find SPI flash\n", -ENOENT);
Simon Glass92f98282020-02-02 13:37:06 -0700254 ret = -ENODEV;
Simon Glass4e988f92019-12-06 21:42:04 -0700255 } else {
Simon Glass00bf2792020-05-27 06:58:49 -0600256 ret = dm_spi_get_mmap(dev, &map_base, &map_size, &offset);
257 if (!ret)
258 entry->base = map_base;
259 node = dev_ofnode(dev);
260 }
261
262 /*
263 * At this point we have entry->base if ret == 0. If not, then we have
264 * the node and can look for memory-map
265 */
266 if (ret) {
267 ret = ofnode_read_u32_array(node, "memory-map", reg, 2);
Simon Glass4e988f92019-12-06 21:42:04 -0700268 if (ret)
269 return log_msg_ret("Cannot find memory map\n", ret);
270 entry->base = reg[0];
271 }
Bin Meng2845ead2015-10-11 21:37:41 -0700272
Bin Meng1f81b592015-10-11 21:37:39 -0700273 /* Find the place where we put the MRC cache */
Simon Glass00bf2792020-05-27 06:58:49 -0600274 mrc_node = ofnode_find_subnode(node, type == MRC_TYPE_NORMAL ?
275 "rw-mrc-cache" : "rw-var-mrc-cache");
Simon Glass9d25f2e2019-12-06 21:42:03 -0700276 if (!ofnode_valid(mrc_node))
277 return log_msg_ret("Cannot find node", -EPERM);
Bin Meng1f81b592015-10-11 21:37:39 -0700278
Simon Glass9d25f2e2019-12-06 21:42:03 -0700279 ret = ofnode_read_u32_array(mrc_node, "reg", reg, 2);
280 if (ret)
281 return log_msg_ret("Cannot find address", ret);
Bin Meng2845ead2015-10-11 21:37:41 -0700282 entry->offset = reg[0];
283 entry->length = reg[1];
Bin Meng1f81b592015-10-11 21:37:39 -0700284
Simon Glass9d25f2e2019-12-06 21:42:03 -0700285 if (devp)
286 *devp = dev;
Simon Glass91efff52019-12-06 21:42:07 -0700287 debug("MRC cache type %d in '%s', offset %x, len %x, base %x\n",
Simon Glass00bf2792020-05-27 06:58:49 -0600288 type, dev ? dev->name : ofnode_get_name(node), entry->offset,
289 entry->length, entry->base);
Bin Meng1f81b592015-10-11 21:37:39 -0700290
291 return 0;
292}
293
Simon Glass91efff52019-12-06 21:42:07 -0700294static int mrccache_save_type(enum mrc_type_t type)
Bin Meng1f81b592015-10-11 21:37:39 -0700295{
Simon Glassc3d0c232019-12-06 21:42:05 -0700296 struct mrc_data_container *cache;
Simon Glass91efff52019-12-06 21:42:07 -0700297 struct mrc_output *mrc;
Bin Meng2845ead2015-10-11 21:37:41 -0700298 struct mrc_region entry;
Bin Meng1f81b592015-10-11 21:37:39 -0700299 struct udevice *sf;
300 int ret;
301
Simon Glass91efff52019-12-06 21:42:07 -0700302 mrc = &gd->arch.mrc[type];
303 if (!mrc->len)
Bin Meng1f81b592015-10-11 21:37:39 -0700304 return 0;
Simon Glass91efff52019-12-06 21:42:07 -0700305 log_debug("Saving %#x bytes of MRC output data type %d to SPI flash\n",
306 mrc->len, type);
307 ret = mrccache_get_region(type, &sf, &entry);
Bin Meng1f81b592015-10-11 21:37:39 -0700308 if (ret)
Simon Glass1b9d8152019-12-06 21:42:06 -0700309 return log_msg_ret("Cannot get region", ret);
Simon Glass9d25f2e2019-12-06 21:42:03 -0700310 ret = device_probe(sf);
311 if (ret)
Simon Glass1b9d8152019-12-06 21:42:06 -0700312 return log_msg_ret("Cannot probe device", ret);
Simon Glass91efff52019-12-06 21:42:07 -0700313 cache = mrc->cache;
314
Simon Glassc3d0c232019-12-06 21:42:05 -0700315 ret = mrccache_update(sf, &entry, cache);
Simon Glass1b9d8152019-12-06 21:42:06 -0700316 if (!ret)
Simon Glassc3d0c232019-12-06 21:42:05 -0700317 debug("Saved MRC data with checksum %04x\n", cache->checksum);
Simon Glass1b9d8152019-12-06 21:42:06 -0700318 else if (ret == -EEXIST)
Simon Glass9df244f2016-01-17 16:11:29 -0700319 debug("MRC data is the same as last time, skipping save\n");
Bin Meng1f81b592015-10-11 21:37:39 -0700320
Simon Glass1b9d8152019-12-06 21:42:06 -0700321 return 0;
Bin Meng1f81b592015-10-11 21:37:39 -0700322}
Simon Glass48fd8562019-04-25 21:58:57 -0600323
Simon Glass91efff52019-12-06 21:42:07 -0700324int mrccache_save(void)
325{
326 int i;
327
328 for (i = 0; i < MRC_TYPE_COUNT; i++) {
329 int ret;
330
331 ret = mrccache_save_type(i);
332 if (ret)
333 return ret;
334 }
335
336 return 0;
337}
338
Simon Glass48fd8562019-04-25 21:58:57 -0600339int mrccache_spl_save(void)
340{
Simon Glass91efff52019-12-06 21:42:07 -0700341 int i;
Simon Glass48fd8562019-04-25 21:58:57 -0600342
Simon Glass91efff52019-12-06 21:42:07 -0700343 for (i = 0; i < MRC_TYPE_COUNT; i++) {
344 struct mrc_output *mrc = &gd->arch.mrc[i];
345 void *data;
346 int size;
347
348 size = mrc->len + MRC_DATA_HEADER_SIZE;
349 data = malloc(size);
350 if (!data)
351 return log_msg_ret("Allocate MRC cache block", -ENOMEM);
352 mrccache_setup(mrc, data);
353 }
Simon Glass48fd8562019-04-25 21:58:57 -0600354
355 return mrccache_save();
356}