blob: f37a732a45cc52f7b7de707db2347d2c1f82826f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glass191c0082015-01-19 22:16:14 -07002/*
Bin Mengbfa95c52015-10-11 21:37:38 -07003 * From coreboot src/southbridge/intel/bd82x6x/mrccache.c
Simon Glass191c0082015-01-19 22:16:14 -07004 *
5 * Copyright (C) 2014 Google Inc.
Bin Menged800962015-10-11 21:37:39 -07006 * Copyright (C) 2015 Bin Meng <bmeng.cn@gmail.com>
Simon Glass191c0082015-01-19 22:16:14 -07007 */
8
9#include <common.h>
Bin Menged800962015-10-11 21:37:39 -070010#include <dm.h>
Simon Glass191c0082015-01-19 22:16:14 -070011#include <errno.h>
12#include <fdtdec.h>
13#include <net.h>
14#include <spi.h>
15#include <spi_flash.h>
Bin Mengf6220f12015-10-11 21:37:36 -070016#include <asm/mrccache.h>
Simon Glass191c0082015-01-19 22:16:14 -070017
Bin Menged800962015-10-11 21:37:39 -070018DECLARE_GLOBAL_DATA_PTR;
19
Simon Glass191c0082015-01-19 22:16:14 -070020static struct mrc_data_container *next_mrc_block(
Bin Mengbfa95c52015-10-11 21:37:38 -070021 struct mrc_data_container *cache)
Simon Glass191c0082015-01-19 22:16:14 -070022{
23 /* MRC data blocks are aligned within the region */
Bin Mengbfa95c52015-10-11 21:37:38 -070024 u32 mrc_size = sizeof(*cache) + cache->data_size;
25 u8 *region_ptr = (u8 *)cache;
26
Simon Glass191c0082015-01-19 22:16:14 -070027 if (mrc_size & (MRC_DATA_ALIGN - 1UL)) {
28 mrc_size &= ~(MRC_DATA_ALIGN - 1UL);
29 mrc_size += MRC_DATA_ALIGN;
30 }
31
Simon Glass191c0082015-01-19 22:16:14 -070032 region_ptr += mrc_size;
Bin Mengbfa95c52015-10-11 21:37:38 -070033
Simon Glass191c0082015-01-19 22:16:14 -070034 return (struct mrc_data_container *)region_ptr;
35}
36
37static int is_mrc_cache(struct mrc_data_container *cache)
38{
39 return cache && (cache->signature == MRC_DATA_SIGNATURE);
40}
41
Bin Meng4b9f6a62015-10-11 21:37:41 -070042struct mrc_data_container *mrccache_find_current(struct mrc_region *entry)
Simon Glass191c0082015-01-19 22:16:14 -070043{
44 struct mrc_data_container *cache, *next;
45 ulong base_addr, end_addr;
46 uint id;
47
Bin Meng4b9f6a62015-10-11 21:37:41 -070048 base_addr = entry->base + entry->offset;
Simon Glass191c0082015-01-19 22:16:14 -070049 end_addr = base_addr + entry->length;
50 cache = NULL;
51
52 /* Search for the last filled entry in the region */
53 for (id = 0, next = (struct mrc_data_container *)base_addr;
54 is_mrc_cache(next);
55 id++) {
56 cache = next;
57 next = next_mrc_block(next);
58 if ((ulong)next >= end_addr)
59 break;
60 }
61
62 if (id-- == 0) {
63 debug("%s: No valid MRC cache found.\n", __func__);
64 return NULL;
65 }
66
67 /* Verify checksum */
68 if (cache->checksum != compute_ip_checksum(cache->data,
69 cache->data_size)) {
70 printf("%s: MRC cache checksum mismatch\n", __func__);
71 return NULL;
72 }
73
74 debug("%s: picked entry %u from cache block\n", __func__, id);
75
76 return cache;
77}
78
79/**
80 * find_next_mrc_cache() - get next cache entry
81 *
82 * @entry: MRC cache flash area
83 * @cache: Entry to start from
84 *
85 * @return next cache entry if found, NULL if we got to the end
86 */
Bin Meng4b9f6a62015-10-11 21:37:41 -070087static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry,
Simon Glass191c0082015-01-19 22:16:14 -070088 struct mrc_data_container *cache)
89{
90 ulong base_addr, end_addr;
91
Bin Meng4b9f6a62015-10-11 21:37:41 -070092 base_addr = entry->base + entry->offset;
Simon Glass191c0082015-01-19 22:16:14 -070093 end_addr = base_addr + entry->length;
94
95 cache = next_mrc_block(cache);
96 if ((ulong)cache >= end_addr) {
97 /* Crossed the boundary */
98 cache = NULL;
99 debug("%s: no available entries found\n", __func__);
100 } else {
101 debug("%s: picked next entry from cache block at %p\n",
102 __func__, cache);
103 }
104
105 return cache;
106}
107
Bin Meng4b9f6a62015-10-11 21:37:41 -0700108int mrccache_update(struct udevice *sf, struct mrc_region *entry,
Simon Glass191c0082015-01-19 22:16:14 -0700109 struct mrc_data_container *cur)
110{
111 struct mrc_data_container *cache;
112 ulong offset;
113 ulong base_addr;
114 int ret;
115
Bin Meng2fe66db2015-10-11 21:37:37 -0700116 if (!is_mrc_cache(cur))
117 return -EINVAL;
118
Simon Glass191c0082015-01-19 22:16:14 -0700119 /* Find the last used block */
Bin Meng4b9f6a62015-10-11 21:37:41 -0700120 base_addr = entry->base + entry->offset;
Simon Glass191c0082015-01-19 22:16:14 -0700121 debug("Updating MRC cache data\n");
122 cache = mrccache_find_current(entry);
123 if (cache && (cache->data_size == cur->data_size) &&
124 (!memcmp(cache, cur, cache->data_size + sizeof(*cur)))) {
125 debug("MRC data in flash is up to date. No update\n");
126 return -EEXIST;
127 }
128
129 /* Move to the next block, which will be the first unused block */
130 if (cache)
131 cache = find_next_mrc_cache(entry, cache);
132
133 /*
134 * If we have got to the end, erase the entire mrc-cache area and start
135 * again at block 0.
136 */
137 if (!cache) {
138 debug("Erasing the MRC cache region of %x bytes at %x\n",
139 entry->length, entry->offset);
140
Simon Glassba457562015-03-26 09:29:26 -0600141 ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
Simon Glass191c0082015-01-19 22:16:14 -0700142 if (ret) {
143 debug("Failed to erase flash region\n");
144 return ret;
145 }
146 cache = (struct mrc_data_container *)base_addr;
147 }
148
149 /* Write the data out */
150 offset = (ulong)cache - base_addr + entry->offset;
151 debug("Write MRC cache update to flash at %lx\n", offset);
Simon Glassba457562015-03-26 09:29:26 -0600152 ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
153 cur);
Simon Glass191c0082015-01-19 22:16:14 -0700154 if (ret) {
155 debug("Failed to write to SPI flash\n");
156 return ret;
157 }
158
159 return 0;
160}
Bin Menged800962015-10-11 21:37:39 -0700161
Simon Glass9a679942019-04-25 21:58:57 -0600162static void mrccache_setup(void *data)
Bin Menged800962015-10-11 21:37:39 -0700163{
Simon Glass9a679942019-04-25 21:58:57 -0600164 struct mrc_data_container *cache = data;
Bin Menged800962015-10-11 21:37:39 -0700165 u16 checksum;
166
Bin Menged800962015-10-11 21:37:39 -0700167 cache->signature = MRC_DATA_SIGNATURE;
168 cache->data_size = gd->arch.mrc_output_len;
169 checksum = compute_ip_checksum(gd->arch.mrc_output, cache->data_size);
170 debug("Saving %d bytes for MRC output data, checksum %04x\n",
171 cache->data_size, checksum);
172 cache->checksum = checksum;
173 cache->reserved = 0;
174 memcpy(cache->data, gd->arch.mrc_output, cache->data_size);
175
176 /* gd->arch.mrc_output now points to the container */
177 gd->arch.mrc_output = (char *)cache;
Simon Glass9a679942019-04-25 21:58:57 -0600178}
179
180int mrccache_reserve(void)
181{
182 if (!gd->arch.mrc_output_len)
183 return 0;
184
185 /* adjust stack pointer to store pure cache data plus the header */
186 gd->start_addr_sp -= (gd->arch.mrc_output_len + MRC_DATA_HEADER_SIZE);
187 mrccache_setup((void *)gd->start_addr_sp);
Bin Menged800962015-10-11 21:37:39 -0700188
189 gd->start_addr_sp &= ~0xf;
190
191 return 0;
192}
193
Bin Meng4b9f6a62015-10-11 21:37:41 -0700194int mrccache_get_region(struct udevice **devp, struct mrc_region *entry)
Bin Menged800962015-10-11 21:37:39 -0700195{
196 const void *blob = gd->fdt_blob;
197 int node, mrc_node;
Bin Meng4b9f6a62015-10-11 21:37:41 -0700198 u32 reg[2];
Bin Menged800962015-10-11 21:37:39 -0700199 int ret;
200
201 /* Find the flash chip within the SPI controller node */
202 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
Simon Glass54cd2402016-09-25 21:33:40 -0600203 if (node < 0) {
204 debug("%s: Cannot find SPI flash\n", __func__);
Bin Menged800962015-10-11 21:37:39 -0700205 return -ENOENT;
Simon Glass54cd2402016-09-25 21:33:40 -0600206 }
Bin Menged800962015-10-11 21:37:39 -0700207
Bin Meng4b9f6a62015-10-11 21:37:41 -0700208 if (fdtdec_get_int_array(blob, node, "memory-map", reg, 2))
Simon Glass54cd2402016-09-25 21:33:40 -0600209 return -EINVAL;
Bin Meng4b9f6a62015-10-11 21:37:41 -0700210 entry->base = reg[0];
211
Bin Menged800962015-10-11 21:37:39 -0700212 /* Find the place where we put the MRC cache */
213 mrc_node = fdt_subnode_offset(blob, node, "rw-mrc-cache");
214 if (mrc_node < 0)
215 return -EPERM;
216
Bin Meng4b9f6a62015-10-11 21:37:41 -0700217 if (fdtdec_get_int_array(blob, mrc_node, "reg", reg, 2))
Simon Glass54cd2402016-09-25 21:33:40 -0600218 return -EINVAL;
Bin Meng4b9f6a62015-10-11 21:37:41 -0700219 entry->offset = reg[0];
220 entry->length = reg[1];
Bin Menged800962015-10-11 21:37:39 -0700221
222 if (devp) {
223 ret = uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node,
224 devp);
225 debug("ret = %d\n", ret);
226 if (ret)
227 return ret;
228 }
229
230 return 0;
231}
232
233int mrccache_save(void)
234{
235 struct mrc_data_container *data;
Bin Meng4b9f6a62015-10-11 21:37:41 -0700236 struct mrc_region entry;
Bin Menged800962015-10-11 21:37:39 -0700237 struct udevice *sf;
238 int ret;
239
240 if (!gd->arch.mrc_output_len)
241 return 0;
242 debug("Saving %d bytes of MRC output data to SPI flash\n",
243 gd->arch.mrc_output_len);
244
245 ret = mrccache_get_region(&sf, &entry);
246 if (ret)
247 goto err_entry;
248 data = (struct mrc_data_container *)gd->arch.mrc_output;
249 ret = mrccache_update(sf, &entry, data);
Simon Glass8b674412016-01-17 16:11:29 -0700250 if (!ret) {
Bin Menged800962015-10-11 21:37:39 -0700251 debug("Saved MRC data with checksum %04x\n", data->checksum);
Simon Glass8b674412016-01-17 16:11:29 -0700252 } else if (ret == -EEXIST) {
253 debug("MRC data is the same as last time, skipping save\n");
254 ret = 0;
255 }
Bin Menged800962015-10-11 21:37:39 -0700256
257err_entry:
258 if (ret)
259 debug("%s: Failed: %d\n", __func__, ret);
260 return ret;
261}
Simon Glass9a679942019-04-25 21:58:57 -0600262
263int mrccache_spl_save(void)
264{
265 void *data;
266 int size;
267
268 size = gd->arch.mrc_output_len + MRC_DATA_HEADER_SIZE;
269 data = malloc(size);
270 if (!data)
271 return log_msg_ret("Allocate MRC cache block", -ENOMEM);
272 mrccache_setup(data);
273 gd->arch.mrc_output = data;
274
275 return mrccache_save();
276}