Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 2 | /* |
Bin Meng | 8575ab1 | 2015-10-11 21:37:38 -0700 | [diff] [blame] | 3 | * From coreboot src/southbridge/intel/bd82x6x/mrccache.c |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2014 Google Inc. |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 6 | * Copyright (C) 2015 Bin Meng <bmeng.cn@gmail.com> |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 11 | #include <errno.h> |
| 12 | #include <fdtdec.h> |
| 13 | #include <net.h> |
| 14 | #include <spi.h> |
| 15 | #include <spi_flash.h> |
Bin Meng | 21666cf | 2015-10-11 21:37:36 -0700 | [diff] [blame] | 16 | #include <asm/mrccache.h> |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 17 | |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 20 | static struct mrc_data_container *next_mrc_block( |
Bin Meng | 8575ab1 | 2015-10-11 21:37:38 -0700 | [diff] [blame] | 21 | struct mrc_data_container *cache) |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 22 | { |
| 23 | /* MRC data blocks are aligned within the region */ |
Bin Meng | 8575ab1 | 2015-10-11 21:37:38 -0700 | [diff] [blame] | 24 | u32 mrc_size = sizeof(*cache) + cache->data_size; |
| 25 | u8 *region_ptr = (u8 *)cache; |
| 26 | |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 27 | if (mrc_size & (MRC_DATA_ALIGN - 1UL)) { |
| 28 | mrc_size &= ~(MRC_DATA_ALIGN - 1UL); |
| 29 | mrc_size += MRC_DATA_ALIGN; |
| 30 | } |
| 31 | |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 32 | region_ptr += mrc_size; |
Bin Meng | 8575ab1 | 2015-10-11 21:37:38 -0700 | [diff] [blame] | 33 | |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 34 | return (struct mrc_data_container *)region_ptr; |
| 35 | } |
| 36 | |
| 37 | static int is_mrc_cache(struct mrc_data_container *cache) |
| 38 | { |
| 39 | return cache && (cache->signature == MRC_DATA_SIGNATURE); |
| 40 | } |
| 41 | |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 42 | struct mrc_data_container *mrccache_find_current(struct mrc_region *entry) |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 43 | { |
| 44 | struct mrc_data_container *cache, *next; |
| 45 | ulong base_addr, end_addr; |
| 46 | uint id; |
| 47 | |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 48 | base_addr = entry->base + entry->offset; |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 49 | 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 Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 87 | static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry, |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 88 | struct mrc_data_container *cache) |
| 89 | { |
| 90 | ulong base_addr, end_addr; |
| 91 | |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 92 | base_addr = entry->base + entry->offset; |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 93 | 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 Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 108 | int mrccache_update(struct udevice *sf, struct mrc_region *entry, |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 109 | struct mrc_data_container *cur) |
| 110 | { |
| 111 | struct mrc_data_container *cache; |
| 112 | ulong offset; |
| 113 | ulong base_addr; |
| 114 | int ret; |
| 115 | |
Simon Glass | fbef25f | 2019-04-25 21:58:59 -0600 | [diff] [blame] | 116 | if (!is_mrc_cache(cur)) { |
| 117 | debug("%s: Cache data not valid\n", __func__); |
Bin Meng | d61a7b4 | 2015-10-11 21:37:37 -0700 | [diff] [blame] | 118 | return -EINVAL; |
Simon Glass | fbef25f | 2019-04-25 21:58:59 -0600 | [diff] [blame] | 119 | } |
Bin Meng | d61a7b4 | 2015-10-11 21:37:37 -0700 | [diff] [blame] | 120 | |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 121 | /* Find the last used block */ |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 122 | base_addr = entry->base + entry->offset; |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 123 | debug("Updating MRC cache data\n"); |
| 124 | cache = mrccache_find_current(entry); |
| 125 | if (cache && (cache->data_size == cur->data_size) && |
| 126 | (!memcmp(cache, cur, cache->data_size + sizeof(*cur)))) { |
| 127 | debug("MRC data in flash is up to date. No update\n"); |
| 128 | return -EEXIST; |
| 129 | } |
| 130 | |
| 131 | /* Move to the next block, which will be the first unused block */ |
| 132 | if (cache) |
| 133 | cache = find_next_mrc_cache(entry, cache); |
| 134 | |
| 135 | /* |
| 136 | * If we have got to the end, erase the entire mrc-cache area and start |
| 137 | * again at block 0. |
| 138 | */ |
| 139 | if (!cache) { |
| 140 | debug("Erasing the MRC cache region of %x bytes at %x\n", |
| 141 | entry->length, entry->offset); |
| 142 | |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 143 | ret = spi_flash_erase_dm(sf, entry->offset, entry->length); |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 144 | if (ret) { |
| 145 | debug("Failed to erase flash region\n"); |
| 146 | return ret; |
| 147 | } |
| 148 | cache = (struct mrc_data_container *)base_addr; |
| 149 | } |
| 150 | |
| 151 | /* Write the data out */ |
| 152 | offset = (ulong)cache - base_addr + entry->offset; |
| 153 | debug("Write MRC cache update to flash at %lx\n", offset); |
Simon Glass | 35f15f6 | 2015-03-26 09:29:26 -0600 | [diff] [blame] | 154 | ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur), |
| 155 | cur); |
Simon Glass | 428dfa4 | 2015-01-19 22:16:14 -0700 | [diff] [blame] | 156 | if (ret) { |
| 157 | debug("Failed to write to SPI flash\n"); |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 163 | |
Simon Glass | 48fd856 | 2019-04-25 21:58:57 -0600 | [diff] [blame] | 164 | static void mrccache_setup(void *data) |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 165 | { |
Simon Glass | 48fd856 | 2019-04-25 21:58:57 -0600 | [diff] [blame] | 166 | struct mrc_data_container *cache = data; |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 167 | u16 checksum; |
| 168 | |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 169 | cache->signature = MRC_DATA_SIGNATURE; |
| 170 | cache->data_size = gd->arch.mrc_output_len; |
| 171 | checksum = compute_ip_checksum(gd->arch.mrc_output, cache->data_size); |
| 172 | debug("Saving %d bytes for MRC output data, checksum %04x\n", |
| 173 | cache->data_size, checksum); |
| 174 | cache->checksum = checksum; |
| 175 | cache->reserved = 0; |
| 176 | memcpy(cache->data, gd->arch.mrc_output, cache->data_size); |
| 177 | |
| 178 | /* gd->arch.mrc_output now points to the container */ |
| 179 | gd->arch.mrc_output = (char *)cache; |
Simon Glass | 48fd856 | 2019-04-25 21:58:57 -0600 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | int mrccache_reserve(void) |
| 183 | { |
| 184 | if (!gd->arch.mrc_output_len) |
| 185 | return 0; |
| 186 | |
| 187 | /* adjust stack pointer to store pure cache data plus the header */ |
| 188 | gd->start_addr_sp -= (gd->arch.mrc_output_len + MRC_DATA_HEADER_SIZE); |
| 189 | mrccache_setup((void *)gd->start_addr_sp); |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 190 | |
| 191 | gd->start_addr_sp &= ~0xf; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 196 | int mrccache_get_region(struct udevice **devp, struct mrc_region *entry) |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 197 | { |
| 198 | const void *blob = gd->fdt_blob; |
| 199 | int node, mrc_node; |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 200 | u32 reg[2]; |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 201 | int ret; |
| 202 | |
| 203 | /* Find the flash chip within the SPI controller node */ |
| 204 | node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH); |
Simon Glass | 25f9578 | 2016-09-25 21:33:40 -0600 | [diff] [blame] | 205 | if (node < 0) { |
| 206 | debug("%s: Cannot find SPI flash\n", __func__); |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 207 | return -ENOENT; |
Simon Glass | 25f9578 | 2016-09-25 21:33:40 -0600 | [diff] [blame] | 208 | } |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 209 | |
Simon Glass | fbef25f | 2019-04-25 21:58:59 -0600 | [diff] [blame] | 210 | if (fdtdec_get_int_array(blob, node, "memory-map", reg, 2)) { |
| 211 | debug("%s: Cannot find memory map\n", __func__); |
Simon Glass | 25f9578 | 2016-09-25 21:33:40 -0600 | [diff] [blame] | 212 | return -EINVAL; |
Simon Glass | fbef25f | 2019-04-25 21:58:59 -0600 | [diff] [blame] | 213 | } |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 214 | entry->base = reg[0]; |
| 215 | |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 216 | /* Find the place where we put the MRC cache */ |
| 217 | mrc_node = fdt_subnode_offset(blob, node, "rw-mrc-cache"); |
Simon Glass | fbef25f | 2019-04-25 21:58:59 -0600 | [diff] [blame] | 218 | if (mrc_node < 0) { |
| 219 | debug("%s: Cannot find node\n", __func__); |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 220 | return -EPERM; |
Simon Glass | fbef25f | 2019-04-25 21:58:59 -0600 | [diff] [blame] | 221 | } |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 222 | |
Simon Glass | fbef25f | 2019-04-25 21:58:59 -0600 | [diff] [blame] | 223 | if (fdtdec_get_int_array(blob, mrc_node, "reg", reg, 2)) { |
| 224 | debug("%s: Cannot find address\n", __func__); |
Simon Glass | 25f9578 | 2016-09-25 21:33:40 -0600 | [diff] [blame] | 225 | return -EINVAL; |
Simon Glass | fbef25f | 2019-04-25 21:58:59 -0600 | [diff] [blame] | 226 | } |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 227 | entry->offset = reg[0]; |
| 228 | entry->length = reg[1]; |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 229 | |
| 230 | if (devp) { |
| 231 | ret = uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node, |
| 232 | devp); |
| 233 | debug("ret = %d\n", ret); |
| 234 | if (ret) |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | int mrccache_save(void) |
| 242 | { |
| 243 | struct mrc_data_container *data; |
Bin Meng | 2845ead | 2015-10-11 21:37:41 -0700 | [diff] [blame] | 244 | struct mrc_region entry; |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 245 | struct udevice *sf; |
| 246 | int ret; |
| 247 | |
| 248 | if (!gd->arch.mrc_output_len) |
| 249 | return 0; |
| 250 | debug("Saving %d bytes of MRC output data to SPI flash\n", |
| 251 | gd->arch.mrc_output_len); |
| 252 | |
| 253 | ret = mrccache_get_region(&sf, &entry); |
| 254 | if (ret) |
| 255 | goto err_entry; |
| 256 | data = (struct mrc_data_container *)gd->arch.mrc_output; |
| 257 | ret = mrccache_update(sf, &entry, data); |
Simon Glass | 9df244f | 2016-01-17 16:11:29 -0700 | [diff] [blame] | 258 | if (!ret) { |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 259 | debug("Saved MRC data with checksum %04x\n", data->checksum); |
Simon Glass | 9df244f | 2016-01-17 16:11:29 -0700 | [diff] [blame] | 260 | } else if (ret == -EEXIST) { |
| 261 | debug("MRC data is the same as last time, skipping save\n"); |
| 262 | ret = 0; |
| 263 | } |
Bin Meng | 1f81b59 | 2015-10-11 21:37:39 -0700 | [diff] [blame] | 264 | |
| 265 | err_entry: |
| 266 | if (ret) |
| 267 | debug("%s: Failed: %d\n", __func__, ret); |
| 268 | return ret; |
| 269 | } |
Simon Glass | 48fd856 | 2019-04-25 21:58:57 -0600 | [diff] [blame] | 270 | |
| 271 | int mrccache_spl_save(void) |
| 272 | { |
| 273 | void *data; |
| 274 | int size; |
| 275 | |
| 276 | size = gd->arch.mrc_output_len + MRC_DATA_HEADER_SIZE; |
| 277 | data = malloc(size); |
| 278 | if (!data) |
| 279 | return log_msg_ret("Allocate MRC cache block", -ENOMEM); |
| 280 | mrccache_setup(data); |
| 281 | gd->arch.mrc_output = data; |
| 282 | |
| 283 | return mrccache_save(); |
| 284 | } |