blob: b526915680128d3917a667019e0c276bafac3153 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Bin Meng21666cf2015-10-11 21:37:36 -070019#include <asm/mrccache.h>
Simon Glass9d25f2e2019-12-06 21:42:03 -070020#include <dm/device-internal.h>
21#include <dm/uclass-internal.h>
Simon Glass428dfa42015-01-19 22:16:14 -070022
Bin Meng1f81b592015-10-11 21:37:39 -070023DECLARE_GLOBAL_DATA_PTR;
24
Simon Glass040bef12019-09-25 08:57:04 -060025static uint mrc_block_size(uint data_size)
26{
27 uint mrc_size = sizeof(struct mrc_data_container) + data_size;
28
29 return ALIGN(mrc_size, MRC_DATA_ALIGN);
30}
31
Simon Glass428dfa42015-01-19 22:16:14 -070032static struct mrc_data_container *next_mrc_block(
Bin Meng8575ab12015-10-11 21:37:38 -070033 struct mrc_data_container *cache)
Simon Glass428dfa42015-01-19 22:16:14 -070034{
35 /* MRC data blocks are aligned within the region */
Bin Meng8575ab12015-10-11 21:37:38 -070036 u8 *region_ptr = (u8 *)cache;
37
Simon Glass040bef12019-09-25 08:57:04 -060038 region_ptr += mrc_block_size(cache->data_size);
Bin Meng8575ab12015-10-11 21:37:38 -070039
Simon Glass428dfa42015-01-19 22:16:14 -070040 return (struct mrc_data_container *)region_ptr;
41}
42
43static int is_mrc_cache(struct mrc_data_container *cache)
44{
45 return cache && (cache->signature == MRC_DATA_SIGNATURE);
46}
47
Bin Meng2845ead2015-10-11 21:37:41 -070048struct mrc_data_container *mrccache_find_current(struct mrc_region *entry)
Simon Glass428dfa42015-01-19 22:16:14 -070049{
50 struct mrc_data_container *cache, *next;
51 ulong base_addr, end_addr;
52 uint id;
53
Bin Meng2845ead2015-10-11 21:37:41 -070054 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -070055 end_addr = base_addr + entry->length;
56 cache = NULL;
57
58 /* Search for the last filled entry in the region */
59 for (id = 0, next = (struct mrc_data_container *)base_addr;
60 is_mrc_cache(next);
61 id++) {
62 cache = next;
63 next = next_mrc_block(next);
64 if ((ulong)next >= end_addr)
65 break;
66 }
67
68 if (id-- == 0) {
69 debug("%s: No valid MRC cache found.\n", __func__);
70 return NULL;
71 }
72
73 /* Verify checksum */
74 if (cache->checksum != compute_ip_checksum(cache->data,
75 cache->data_size)) {
76 printf("%s: MRC cache checksum mismatch\n", __func__);
77 return NULL;
78 }
79
80 debug("%s: picked entry %u from cache block\n", __func__, id);
81
82 return cache;
83}
84
85/**
86 * find_next_mrc_cache() - get next cache entry
87 *
Simon Glassd553f972019-12-06 21:42:02 -070088 * This moves to the next cache entry in the region, making sure it has enough
89 * space to hold data of size @data_size.
90 *
Simon Glass428dfa42015-01-19 22:16:14 -070091 * @entry: MRC cache flash area
92 * @cache: Entry to start from
Simon Glassd553f972019-12-06 21:42:02 -070093 * @data_size: Required data size of the new entry. Note that we assume that
94 * all cache entries are the same size
Simon Glass428dfa42015-01-19 22:16:14 -070095 *
96 * @return next cache entry if found, NULL if we got to the end
97 */
Bin Meng2845ead2015-10-11 21:37:41 -070098static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry,
Simon Glassd553f972019-12-06 21:42:02 -070099 struct mrc_data_container *prev, int data_size)
Simon Glass428dfa42015-01-19 22:16:14 -0700100{
Simon Glassd553f972019-12-06 21:42:02 -0700101 struct mrc_data_container *cache;
Simon Glass428dfa42015-01-19 22:16:14 -0700102 ulong base_addr, end_addr;
103
Bin Meng2845ead2015-10-11 21:37:41 -0700104 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -0700105 end_addr = base_addr + entry->length;
106
Simon Glassd553f972019-12-06 21:42:02 -0700107 /*
108 * We assume that all cache entries are the same size, but let's use
109 * data_size here for clarity.
110 */
111 cache = next_mrc_block(prev);
112 if ((ulong)cache + mrc_block_size(data_size) > end_addr) {
Simon Glass428dfa42015-01-19 22:16:14 -0700113 /* Crossed the boundary */
114 cache = NULL;
115 debug("%s: no available entries found\n", __func__);
116 } else {
117 debug("%s: picked next entry from cache block at %p\n",
118 __func__, cache);
119 }
120
121 return cache;
122}
123
Simon Glass6e1b9562019-12-06 21:42:09 -0700124/**
125 * mrccache_update() - update the MRC cache with a new record
126 *
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
129 *
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
135 */
136static int mrccache_update(struct udevice *sf, struct mrc_region *entry,
137 struct mrc_data_container *cur)
Simon Glass428dfa42015-01-19 22:16:14 -0700138{
139 struct mrc_data_container *cache;
140 ulong offset;
141 ulong base_addr;
142 int ret;
143
Simon Glassfbef25f2019-04-25 21:58:59 -0600144 if (!is_mrc_cache(cur)) {
145 debug("%s: Cache data not valid\n", __func__);
Bin Mengd61a7b42015-10-11 21:37:37 -0700146 return -EINVAL;
Simon Glassfbef25f2019-04-25 21:58:59 -0600147 }
Bin Mengd61a7b42015-10-11 21:37:37 -0700148
Simon Glass428dfa42015-01-19 22:16:14 -0700149 /* Find the last used block */
Bin Meng2845ead2015-10-11 21:37:41 -0700150 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -0700151 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");
156 return -EEXIST;
157 }
158
159 /* Move to the next block, which will be the first unused block */
160 if (cache)
Simon Glassd553f972019-12-06 21:42:02 -0700161 cache = find_next_mrc_cache(entry, cache, cur->data_size);
Simon Glass428dfa42015-01-19 22:16:14 -0700162
163 /*
164 * If we have got to the end, erase the entire mrc-cache area and start
165 * again at block 0.
166 */
167 if (!cache) {
168 debug("Erasing the MRC cache region of %x bytes at %x\n",
169 entry->length, entry->offset);
170
Simon Glass35f15f62015-03-26 09:29:26 -0600171 ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
Simon Glass428dfa42015-01-19 22:16:14 -0700172 if (ret) {
173 debug("Failed to erase flash region\n");
174 return ret;
175 }
176 cache = (struct mrc_data_container *)base_addr;
177 }
178
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);
Simon Glass35f15f62015-03-26 09:29:26 -0600182 ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
183 cur);
Simon Glass428dfa42015-01-19 22:16:14 -0700184 if (ret) {
185 debug("Failed to write to SPI flash\n");
Simon Glass1b9d8152019-12-06 21:42:06 -0700186 return log_msg_ret("Cannot update mrccache", ret);
Simon Glass428dfa42015-01-19 22:16:14 -0700187 }
188
189 return 0;
190}
Bin Meng1f81b592015-10-11 21:37:39 -0700191
Simon Glass91efff52019-12-06 21:42:07 -0700192static void mrccache_setup(struct mrc_output *mrc, void *data)
Bin Meng1f81b592015-10-11 21:37:39 -0700193{
Simon Glass48fd8562019-04-25 21:58:57 -0600194 struct mrc_data_container *cache = data;
Bin Meng1f81b592015-10-11 21:37:39 -0700195 u16 checksum;
196
Bin Meng1f81b592015-10-11 21:37:39 -0700197 cache->signature = MRC_DATA_SIGNATURE;
Simon Glass91efff52019-12-06 21:42:07 -0700198 cache->data_size = mrc->len;
199 checksum = compute_ip_checksum(mrc->buf, cache->data_size);
Bin Meng1f81b592015-10-11 21:37:39 -0700200 debug("Saving %d bytes for MRC output data, checksum %04x\n",
201 cache->data_size, checksum);
202 cache->checksum = checksum;
203 cache->reserved = 0;
Simon Glass91efff52019-12-06 21:42:07 -0700204 memcpy(cache->data, mrc->buf, cache->data_size);
Bin Meng1f81b592015-10-11 21:37:39 -0700205
Simon Glass91efff52019-12-06 21:42:07 -0700206 mrc->cache = cache;
Simon Glass48fd8562019-04-25 21:58:57 -0600207}
208
209int mrccache_reserve(void)
210{
Simon Glass91efff52019-12-06 21:42:07 -0700211 int i;
Simon Glass48fd8562019-04-25 21:58:57 -0600212
Simon Glass91efff52019-12-06 21:42:07 -0700213 for (i = 0; i < MRC_TYPE_COUNT; i++) {
214 struct mrc_output *mrc = &gd->arch.mrc[i];
Bin Meng1f81b592015-10-11 21:37:39 -0700215
Simon Glass91efff52019-12-06 21:42:07 -0700216 if (!mrc->len)
217 continue;
218
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);
222
223 gd->start_addr_sp &= ~0xf;
224 }
Bin Meng1f81b592015-10-11 21:37:39 -0700225
226 return 0;
227}
228
Simon Glass91efff52019-12-06 21:42:07 -0700229int mrccache_get_region(enum mrc_type_t type, struct udevice **devp,
230 struct mrc_region *entry)
Bin Meng1f81b592015-10-11 21:37:39 -0700231{
Simon Glass9d25f2e2019-12-06 21:42:03 -0700232 struct udevice *dev;
233 ofnode mrc_node;
Simon Glass4e988f92019-12-06 21:42:04 -0700234 ulong map_base;
235 uint map_size;
236 uint offset;
Simon Glass00bf2792020-05-27 06:58:49 -0600237 ofnode node;
Bin Meng2845ead2015-10-11 21:37:41 -0700238 u32 reg[2];
Bin Meng1f81b592015-10-11 21:37:39 -0700239 int ret;
240
Simon Glass9d25f2e2019-12-06 21:42:03 -0700241 /*
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.
245 */
246 ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
Simon Glass00bf2792020-05-27 06:58:49 -0600247 if (ret || !dev) {
248 /*
249 * Fall back to searching the device tree since driver model
250 * may not be ready yet (e.g. with FSPv1)
251 */
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);
Simon Glass92f98282020-02-02 13:37:06 -0700255 ret = -ENODEV;
Simon Glass4e988f92019-12-06 21:42:04 -0700256 } else {
Simon Glass00bf2792020-05-27 06:58:49 -0600257 ret = dm_spi_get_mmap(dev, &map_base, &map_size, &offset);
258 if (!ret)
259 entry->base = map_base;
260 node = dev_ofnode(dev);
261 }
262
263 /*
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
266 */
267 if (ret) {
268 ret = ofnode_read_u32_array(node, "memory-map", reg, 2);
Simon Glass4e988f92019-12-06 21:42:04 -0700269 if (ret)
270 return log_msg_ret("Cannot find memory map\n", ret);
271 entry->base = reg[0];
272 }
Bin Meng2845ead2015-10-11 21:37:41 -0700273
Bin Meng1f81b592015-10-11 21:37:39 -0700274 /* Find the place where we put the MRC cache */
Simon Glass00bf2792020-05-27 06:58:49 -0600275 mrc_node = ofnode_find_subnode(node, type == MRC_TYPE_NORMAL ?
276 "rw-mrc-cache" : "rw-var-mrc-cache");
Simon Glass9d25f2e2019-12-06 21:42:03 -0700277 if (!ofnode_valid(mrc_node))
278 return log_msg_ret("Cannot find node", -EPERM);
Bin Meng1f81b592015-10-11 21:37:39 -0700279
Simon Glass9d25f2e2019-12-06 21:42:03 -0700280 ret = ofnode_read_u32_array(mrc_node, "reg", reg, 2);
281 if (ret)
282 return log_msg_ret("Cannot find address", ret);
Bin Meng2845ead2015-10-11 21:37:41 -0700283 entry->offset = reg[0];
284 entry->length = reg[1];
Bin Meng1f81b592015-10-11 21:37:39 -0700285
Simon Glass9d25f2e2019-12-06 21:42:03 -0700286 if (devp)
287 *devp = dev;
Simon Glass91efff52019-12-06 21:42:07 -0700288 debug("MRC cache type %d in '%s', offset %x, len %x, base %x\n",
Simon Glass00bf2792020-05-27 06:58:49 -0600289 type, dev ? dev->name : ofnode_get_name(node), entry->offset,
290 entry->length, entry->base);
Bin Meng1f81b592015-10-11 21:37:39 -0700291
292 return 0;
293}
294
Simon Glass91efff52019-12-06 21:42:07 -0700295static int mrccache_save_type(enum mrc_type_t type)
Bin Meng1f81b592015-10-11 21:37:39 -0700296{
Simon Glassc3d0c232019-12-06 21:42:05 -0700297 struct mrc_data_container *cache;
Simon Glass91efff52019-12-06 21:42:07 -0700298 struct mrc_output *mrc;
Bin Meng2845ead2015-10-11 21:37:41 -0700299 struct mrc_region entry;
Bin Meng1f81b592015-10-11 21:37:39 -0700300 struct udevice *sf;
301 int ret;
302
Simon Glass91efff52019-12-06 21:42:07 -0700303 mrc = &gd->arch.mrc[type];
304 if (!mrc->len)
Bin Meng1f81b592015-10-11 21:37:39 -0700305 return 0;
Simon Glass91efff52019-12-06 21:42:07 -0700306 log_debug("Saving %#x bytes of MRC output data type %d to SPI flash\n",
307 mrc->len, type);
308 ret = mrccache_get_region(type, &sf, &entry);
Bin Meng1f81b592015-10-11 21:37:39 -0700309 if (ret)
Simon Glass1b9d8152019-12-06 21:42:06 -0700310 return log_msg_ret("Cannot get region", ret);
Simon Glass9d25f2e2019-12-06 21:42:03 -0700311 ret = device_probe(sf);
312 if (ret)
Simon Glass1b9d8152019-12-06 21:42:06 -0700313 return log_msg_ret("Cannot probe device", ret);
Simon Glass91efff52019-12-06 21:42:07 -0700314 cache = mrc->cache;
315
Simon Glassc3d0c232019-12-06 21:42:05 -0700316 ret = mrccache_update(sf, &entry, cache);
Simon Glass1b9d8152019-12-06 21:42:06 -0700317 if (!ret)
Simon Glassc3d0c232019-12-06 21:42:05 -0700318 debug("Saved MRC data with checksum %04x\n", cache->checksum);
Simon Glass1b9d8152019-12-06 21:42:06 -0700319 else if (ret == -EEXIST)
Simon Glass9df244f2016-01-17 16:11:29 -0700320 debug("MRC data is the same as last time, skipping save\n");
Bin Meng1f81b592015-10-11 21:37:39 -0700321
Simon Glass1b9d8152019-12-06 21:42:06 -0700322 return 0;
Bin Meng1f81b592015-10-11 21:37:39 -0700323}
Simon Glass48fd8562019-04-25 21:58:57 -0600324
Simon Glass91efff52019-12-06 21:42:07 -0700325int mrccache_save(void)
326{
327 int i;
328
329 for (i = 0; i < MRC_TYPE_COUNT; i++) {
330 int ret;
331
332 ret = mrccache_save_type(i);
333 if (ret)
334 return ret;
335 }
336
337 return 0;
338}
339
Simon Glass48fd8562019-04-25 21:58:57 -0600340int mrccache_spl_save(void)
341{
Simon Glass91efff52019-12-06 21:42:07 -0700342 int i;
Simon Glass48fd8562019-04-25 21:58:57 -0600343
Simon Glass91efff52019-12-06 21:42:07 -0700344 for (i = 0; i < MRC_TYPE_COUNT; i++) {
345 struct mrc_output *mrc = &gd->arch.mrc[i];
346 void *data;
347 int size;
348
349 size = mrc->len + MRC_DATA_HEADER_SIZE;
350 data = malloc(size);
351 if (!data)
352 return log_msg_ret("Allocate MRC cache block", -ENOMEM);
353 mrccache_setup(mrc, data);
354 }
Simon Glass48fd8562019-04-25 21:58:57 -0600355
356 return mrccache_save();
357}