blob: d1c44f290c467e21ca1adb6f5469ba799f67668f [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 Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glass428dfa42015-01-19 22:16:14 -070014#include <net.h>
15#include <spi.h>
16#include <spi_flash.h>
Bin Meng21666cf2015-10-11 21:37:36 -070017#include <asm/mrccache.h>
Simon Glass9d25f2e2019-12-06 21:42:03 -070018#include <dm/device-internal.h>
19#include <dm/uclass-internal.h>
Simon Glass428dfa42015-01-19 22:16:14 -070020
Bin Meng1f81b592015-10-11 21:37:39 -070021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass040bef12019-09-25 08:57:04 -060023static uint mrc_block_size(uint data_size)
24{
25 uint mrc_size = sizeof(struct mrc_data_container) + data_size;
26
27 return ALIGN(mrc_size, MRC_DATA_ALIGN);
28}
29
Simon Glass428dfa42015-01-19 22:16:14 -070030static struct mrc_data_container *next_mrc_block(
Bin Meng8575ab12015-10-11 21:37:38 -070031 struct mrc_data_container *cache)
Simon Glass428dfa42015-01-19 22:16:14 -070032{
33 /* MRC data blocks are aligned within the region */
Bin Meng8575ab12015-10-11 21:37:38 -070034 u8 *region_ptr = (u8 *)cache;
35
Simon Glass040bef12019-09-25 08:57:04 -060036 region_ptr += mrc_block_size(cache->data_size);
Bin Meng8575ab12015-10-11 21:37:38 -070037
Simon Glass428dfa42015-01-19 22:16:14 -070038 return (struct mrc_data_container *)region_ptr;
39}
40
41static int is_mrc_cache(struct mrc_data_container *cache)
42{
43 return cache && (cache->signature == MRC_DATA_SIGNATURE);
44}
45
Bin Meng2845ead2015-10-11 21:37:41 -070046struct mrc_data_container *mrccache_find_current(struct mrc_region *entry)
Simon Glass428dfa42015-01-19 22:16:14 -070047{
48 struct mrc_data_container *cache, *next;
49 ulong base_addr, end_addr;
50 uint id;
51
Bin Meng2845ead2015-10-11 21:37:41 -070052 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -070053 end_addr = base_addr + entry->length;
54 cache = NULL;
55
56 /* Search for the last filled entry in the region */
57 for (id = 0, next = (struct mrc_data_container *)base_addr;
58 is_mrc_cache(next);
59 id++) {
60 cache = next;
61 next = next_mrc_block(next);
62 if ((ulong)next >= end_addr)
63 break;
64 }
65
66 if (id-- == 0) {
67 debug("%s: No valid MRC cache found.\n", __func__);
68 return NULL;
69 }
70
71 /* Verify checksum */
72 if (cache->checksum != compute_ip_checksum(cache->data,
73 cache->data_size)) {
74 printf("%s: MRC cache checksum mismatch\n", __func__);
75 return NULL;
76 }
77
78 debug("%s: picked entry %u from cache block\n", __func__, id);
79
80 return cache;
81}
82
83/**
84 * find_next_mrc_cache() - get next cache entry
85 *
Simon Glassd553f972019-12-06 21:42:02 -070086 * This moves to the next cache entry in the region, making sure it has enough
87 * space to hold data of size @data_size.
88 *
Simon Glass428dfa42015-01-19 22:16:14 -070089 * @entry: MRC cache flash area
90 * @cache: Entry to start from
Simon Glassd553f972019-12-06 21:42:02 -070091 * @data_size: Required data size of the new entry. Note that we assume that
92 * all cache entries are the same size
Simon Glass428dfa42015-01-19 22:16:14 -070093 *
94 * @return next cache entry if found, NULL if we got to the end
95 */
Bin Meng2845ead2015-10-11 21:37:41 -070096static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry,
Simon Glassd553f972019-12-06 21:42:02 -070097 struct mrc_data_container *prev, int data_size)
Simon Glass428dfa42015-01-19 22:16:14 -070098{
Simon Glassd553f972019-12-06 21:42:02 -070099 struct mrc_data_container *cache;
Simon Glass428dfa42015-01-19 22:16:14 -0700100 ulong base_addr, end_addr;
101
Bin Meng2845ead2015-10-11 21:37:41 -0700102 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -0700103 end_addr = base_addr + entry->length;
104
Simon Glassd553f972019-12-06 21:42:02 -0700105 /*
106 * We assume that all cache entries are the same size, but let's use
107 * data_size here for clarity.
108 */
109 cache = next_mrc_block(prev);
110 if ((ulong)cache + mrc_block_size(data_size) > end_addr) {
Simon Glass428dfa42015-01-19 22:16:14 -0700111 /* Crossed the boundary */
112 cache = NULL;
113 debug("%s: no available entries found\n", __func__);
114 } else {
115 debug("%s: picked next entry from cache block at %p\n",
116 __func__, cache);
117 }
118
119 return cache;
120}
121
Simon Glass6e1b9562019-12-06 21:42:09 -0700122/**
123 * mrccache_update() - update the MRC cache with a new record
124 *
125 * This writes a new record to the end of the MRC cache region. If the new
126 * record is the same as the latest record then the write is skipped
127 *
128 * @sf: SPI flash to write to
129 * @entry: Position and size of MRC cache in SPI flash
130 * @cur: Record to write
131 * @return 0 if updated, -EEXIST if the record is the same as the latest
132 * record, -EINVAL if the record is not valid, other error if SPI write failed
133 */
134static int mrccache_update(struct udevice *sf, struct mrc_region *entry,
135 struct mrc_data_container *cur)
Simon Glass428dfa42015-01-19 22:16:14 -0700136{
137 struct mrc_data_container *cache;
138 ulong offset;
139 ulong base_addr;
140 int ret;
141
Simon Glassfbef25f2019-04-25 21:58:59 -0600142 if (!is_mrc_cache(cur)) {
143 debug("%s: Cache data not valid\n", __func__);
Bin Mengd61a7b42015-10-11 21:37:37 -0700144 return -EINVAL;
Simon Glassfbef25f2019-04-25 21:58:59 -0600145 }
Bin Mengd61a7b42015-10-11 21:37:37 -0700146
Simon Glass428dfa42015-01-19 22:16:14 -0700147 /* Find the last used block */
Bin Meng2845ead2015-10-11 21:37:41 -0700148 base_addr = entry->base + entry->offset;
Simon Glass428dfa42015-01-19 22:16:14 -0700149 debug("Updating MRC cache data\n");
150 cache = mrccache_find_current(entry);
151 if (cache && (cache->data_size == cur->data_size) &&
152 (!memcmp(cache, cur, cache->data_size + sizeof(*cur)))) {
153 debug("MRC data in flash is up to date. No update\n");
154 return -EEXIST;
155 }
156
157 /* Move to the next block, which will be the first unused block */
158 if (cache)
Simon Glassd553f972019-12-06 21:42:02 -0700159 cache = find_next_mrc_cache(entry, cache, cur->data_size);
Simon Glass428dfa42015-01-19 22:16:14 -0700160
161 /*
162 * If we have got to the end, erase the entire mrc-cache area and start
163 * again at block 0.
164 */
165 if (!cache) {
166 debug("Erasing the MRC cache region of %x bytes at %x\n",
167 entry->length, entry->offset);
168
Simon Glass35f15f62015-03-26 09:29:26 -0600169 ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
Simon Glass428dfa42015-01-19 22:16:14 -0700170 if (ret) {
171 debug("Failed to erase flash region\n");
172 return ret;
173 }
174 cache = (struct mrc_data_container *)base_addr;
175 }
176
177 /* Write the data out */
178 offset = (ulong)cache - base_addr + entry->offset;
179 debug("Write MRC cache update to flash at %lx\n", offset);
Simon Glass35f15f62015-03-26 09:29:26 -0600180 ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
181 cur);
Simon Glass428dfa42015-01-19 22:16:14 -0700182 if (ret) {
183 debug("Failed to write to SPI flash\n");
Simon Glass1b9d8152019-12-06 21:42:06 -0700184 return log_msg_ret("Cannot update mrccache", ret);
Simon Glass428dfa42015-01-19 22:16:14 -0700185 }
186
187 return 0;
188}
Bin Meng1f81b592015-10-11 21:37:39 -0700189
Simon Glass91efff52019-12-06 21:42:07 -0700190static void mrccache_setup(struct mrc_output *mrc, void *data)
Bin Meng1f81b592015-10-11 21:37:39 -0700191{
Simon Glass48fd8562019-04-25 21:58:57 -0600192 struct mrc_data_container *cache = data;
Bin Meng1f81b592015-10-11 21:37:39 -0700193 u16 checksum;
194
Bin Meng1f81b592015-10-11 21:37:39 -0700195 cache->signature = MRC_DATA_SIGNATURE;
Simon Glass91efff52019-12-06 21:42:07 -0700196 cache->data_size = mrc->len;
197 checksum = compute_ip_checksum(mrc->buf, cache->data_size);
Bin Meng1f81b592015-10-11 21:37:39 -0700198 debug("Saving %d bytes for MRC output data, checksum %04x\n",
199 cache->data_size, checksum);
200 cache->checksum = checksum;
201 cache->reserved = 0;
Simon Glass91efff52019-12-06 21:42:07 -0700202 memcpy(cache->data, mrc->buf, cache->data_size);
Bin Meng1f81b592015-10-11 21:37:39 -0700203
Simon Glass91efff52019-12-06 21:42:07 -0700204 mrc->cache = cache;
Simon Glass48fd8562019-04-25 21:58:57 -0600205}
206
207int mrccache_reserve(void)
208{
Simon Glass91efff52019-12-06 21:42:07 -0700209 int i;
Simon Glass48fd8562019-04-25 21:58:57 -0600210
Simon Glass91efff52019-12-06 21:42:07 -0700211 for (i = 0; i < MRC_TYPE_COUNT; i++) {
212 struct mrc_output *mrc = &gd->arch.mrc[i];
Bin Meng1f81b592015-10-11 21:37:39 -0700213
Simon Glass91efff52019-12-06 21:42:07 -0700214 if (!mrc->len)
215 continue;
216
217 /* adjust stack pointer to store pure cache data plus header */
218 gd->start_addr_sp -= (mrc->len + MRC_DATA_HEADER_SIZE);
219 mrccache_setup(mrc, (void *)gd->start_addr_sp);
220
221 gd->start_addr_sp &= ~0xf;
222 }
Bin Meng1f81b592015-10-11 21:37:39 -0700223
224 return 0;
225}
226
Simon Glass91efff52019-12-06 21:42:07 -0700227int mrccache_get_region(enum mrc_type_t type, struct udevice **devp,
228 struct mrc_region *entry)
Bin Meng1f81b592015-10-11 21:37:39 -0700229{
Simon Glass9d25f2e2019-12-06 21:42:03 -0700230 struct udevice *dev;
231 ofnode mrc_node;
Simon Glass4e988f92019-12-06 21:42:04 -0700232 ulong map_base;
233 uint map_size;
234 uint offset;
Bin Meng2845ead2015-10-11 21:37:41 -0700235 u32 reg[2];
Bin Meng1f81b592015-10-11 21:37:39 -0700236 int ret;
237
Simon Glass9d25f2e2019-12-06 21:42:03 -0700238 /*
239 * Find the flash chip within the SPI controller node. Avoid probing
240 * the device here since it may put it into a strange state where the
241 * memory map cannot be read.
242 */
243 ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
Simon Glass92f98282020-02-02 13:37:06 -0700244 if (!ret && !dev)
245 ret = -ENODEV;
Simon Glass9d25f2e2019-12-06 21:42:03 -0700246 if (ret)
247 return log_msg_ret("Cannot find SPI flash\n", ret);
Simon Glass4e988f92019-12-06 21:42:04 -0700248 ret = dm_spi_get_mmap(dev, &map_base, &map_size, &offset);
249 if (!ret) {
250 entry->base = map_base;
251 } else {
252 ret = dev_read_u32_array(dev, "memory-map", reg, 2);
253 if (ret)
254 return log_msg_ret("Cannot find memory map\n", ret);
255 entry->base = reg[0];
256 }
Bin Meng2845ead2015-10-11 21:37:41 -0700257
Bin Meng1f81b592015-10-11 21:37:39 -0700258 /* Find the place where we put the MRC cache */
Simon Glassb5b0aff2019-12-06 21:42:08 -0700259 mrc_node = dev_read_subnode(dev, type == MRC_TYPE_NORMAL ?
260 "rw-mrc-cache" : "rw-var-mrc-cache");
Simon Glass9d25f2e2019-12-06 21:42:03 -0700261 if (!ofnode_valid(mrc_node))
262 return log_msg_ret("Cannot find node", -EPERM);
Bin Meng1f81b592015-10-11 21:37:39 -0700263
Simon Glass9d25f2e2019-12-06 21:42:03 -0700264 ret = ofnode_read_u32_array(mrc_node, "reg", reg, 2);
265 if (ret)
266 return log_msg_ret("Cannot find address", ret);
Bin Meng2845ead2015-10-11 21:37:41 -0700267 entry->offset = reg[0];
268 entry->length = reg[1];
Bin Meng1f81b592015-10-11 21:37:39 -0700269
Simon Glass9d25f2e2019-12-06 21:42:03 -0700270 if (devp)
271 *devp = dev;
Simon Glass91efff52019-12-06 21:42:07 -0700272 debug("MRC cache type %d in '%s', offset %x, len %x, base %x\n",
273 type, dev->name, entry->offset, entry->length, entry->base);
Bin Meng1f81b592015-10-11 21:37:39 -0700274
275 return 0;
276}
277
Simon Glass91efff52019-12-06 21:42:07 -0700278static int mrccache_save_type(enum mrc_type_t type)
Bin Meng1f81b592015-10-11 21:37:39 -0700279{
Simon Glassc3d0c232019-12-06 21:42:05 -0700280 struct mrc_data_container *cache;
Simon Glass91efff52019-12-06 21:42:07 -0700281 struct mrc_output *mrc;
Bin Meng2845ead2015-10-11 21:37:41 -0700282 struct mrc_region entry;
Bin Meng1f81b592015-10-11 21:37:39 -0700283 struct udevice *sf;
284 int ret;
285
Simon Glass91efff52019-12-06 21:42:07 -0700286 mrc = &gd->arch.mrc[type];
287 if (!mrc->len)
Bin Meng1f81b592015-10-11 21:37:39 -0700288 return 0;
Simon Glass91efff52019-12-06 21:42:07 -0700289 log_debug("Saving %#x bytes of MRC output data type %d to SPI flash\n",
290 mrc->len, type);
291 ret = mrccache_get_region(type, &sf, &entry);
Bin Meng1f81b592015-10-11 21:37:39 -0700292 if (ret)
Simon Glass1b9d8152019-12-06 21:42:06 -0700293 return log_msg_ret("Cannot get region", ret);
Simon Glass9d25f2e2019-12-06 21:42:03 -0700294 ret = device_probe(sf);
295 if (ret)
Simon Glass1b9d8152019-12-06 21:42:06 -0700296 return log_msg_ret("Cannot probe device", ret);
Simon Glass91efff52019-12-06 21:42:07 -0700297 cache = mrc->cache;
298
Simon Glassc3d0c232019-12-06 21:42:05 -0700299 ret = mrccache_update(sf, &entry, cache);
Simon Glass1b9d8152019-12-06 21:42:06 -0700300 if (!ret)
Simon Glassc3d0c232019-12-06 21:42:05 -0700301 debug("Saved MRC data with checksum %04x\n", cache->checksum);
Simon Glass1b9d8152019-12-06 21:42:06 -0700302 else if (ret == -EEXIST)
Simon Glass9df244f2016-01-17 16:11:29 -0700303 debug("MRC data is the same as last time, skipping save\n");
Bin Meng1f81b592015-10-11 21:37:39 -0700304
Simon Glass1b9d8152019-12-06 21:42:06 -0700305 return 0;
Bin Meng1f81b592015-10-11 21:37:39 -0700306}
Simon Glass48fd8562019-04-25 21:58:57 -0600307
Simon Glass91efff52019-12-06 21:42:07 -0700308int mrccache_save(void)
309{
310 int i;
311
312 for (i = 0; i < MRC_TYPE_COUNT; i++) {
313 int ret;
314
315 ret = mrccache_save_type(i);
316 if (ret)
317 return ret;
318 }
319
320 return 0;
321}
322
Simon Glass48fd8562019-04-25 21:58:57 -0600323int mrccache_spl_save(void)
324{
Simon Glass91efff52019-12-06 21:42:07 -0700325 int i;
Simon Glass48fd8562019-04-25 21:58:57 -0600326
Simon Glass91efff52019-12-06 21:42:07 -0700327 for (i = 0; i < MRC_TYPE_COUNT; i++) {
328 struct mrc_output *mrc = &gd->arch.mrc[i];
329 void *data;
330 int size;
331
332 size = mrc->len + MRC_DATA_HEADER_SIZE;
333 data = malloc(size);
334 if (!data)
335 return log_msg_ret("Allocate MRC cache block", -ENOMEM);
336 mrccache_setup(mrc, data);
337 }
Simon Glass48fd8562019-04-25 21:58:57 -0600338
339 return mrccache_save();
340}