2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-01-20 05:16:14 +00:00
|
|
|
/*
|
2015-10-12 04:37:38 +00:00
|
|
|
* From coreboot src/southbridge/intel/bd82x6x/mrccache.c
|
2015-01-20 05:16:14 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Google Inc.
|
2015-10-12 04:37:39 +00:00
|
|
|
* Copyright (C) 2015 Bin Meng <bmeng.cn@gmail.com>
|
2015-01-20 05:16:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2015-10-12 04:37:39 +00:00
|
|
|
#include <dm.h>
|
2015-01-20 05:16:14 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fdtdec.h>
|
|
|
|
#include <net.h>
|
|
|
|
#include <spi.h>
|
|
|
|
#include <spi_flash.h>
|
2015-10-12 04:37:36 +00:00
|
|
|
#include <asm/mrccache.h>
|
2019-12-07 04:42:03 +00:00
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/uclass-internal.h>
|
2015-01-20 05:16:14 +00:00
|
|
|
|
2015-10-12 04:37:39 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2019-09-25 14:57:04 +00:00
|
|
|
static uint mrc_block_size(uint data_size)
|
|
|
|
{
|
|
|
|
uint mrc_size = sizeof(struct mrc_data_container) + data_size;
|
|
|
|
|
|
|
|
return ALIGN(mrc_size, MRC_DATA_ALIGN);
|
|
|
|
}
|
|
|
|
|
2015-01-20 05:16:14 +00:00
|
|
|
static struct mrc_data_container *next_mrc_block(
|
2015-10-12 04:37:38 +00:00
|
|
|
struct mrc_data_container *cache)
|
2015-01-20 05:16:14 +00:00
|
|
|
{
|
|
|
|
/* MRC data blocks are aligned within the region */
|
2015-10-12 04:37:38 +00:00
|
|
|
u8 *region_ptr = (u8 *)cache;
|
|
|
|
|
2019-09-25 14:57:04 +00:00
|
|
|
region_ptr += mrc_block_size(cache->data_size);
|
2015-10-12 04:37:38 +00:00
|
|
|
|
2015-01-20 05:16:14 +00:00
|
|
|
return (struct mrc_data_container *)region_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_mrc_cache(struct mrc_data_container *cache)
|
|
|
|
{
|
|
|
|
return cache && (cache->signature == MRC_DATA_SIGNATURE);
|
|
|
|
}
|
|
|
|
|
2015-10-12 04:37:41 +00:00
|
|
|
struct mrc_data_container *mrccache_find_current(struct mrc_region *entry)
|
2015-01-20 05:16:14 +00:00
|
|
|
{
|
|
|
|
struct mrc_data_container *cache, *next;
|
|
|
|
ulong base_addr, end_addr;
|
|
|
|
uint id;
|
|
|
|
|
2015-10-12 04:37:41 +00:00
|
|
|
base_addr = entry->base + entry->offset;
|
2015-01-20 05:16:14 +00:00
|
|
|
end_addr = base_addr + entry->length;
|
|
|
|
cache = NULL;
|
|
|
|
|
|
|
|
/* Search for the last filled entry in the region */
|
|
|
|
for (id = 0, next = (struct mrc_data_container *)base_addr;
|
|
|
|
is_mrc_cache(next);
|
|
|
|
id++) {
|
|
|
|
cache = next;
|
|
|
|
next = next_mrc_block(next);
|
|
|
|
if ((ulong)next >= end_addr)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id-- == 0) {
|
|
|
|
debug("%s: No valid MRC cache found.\n", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify checksum */
|
|
|
|
if (cache->checksum != compute_ip_checksum(cache->data,
|
|
|
|
cache->data_size)) {
|
|
|
|
printf("%s: MRC cache checksum mismatch\n", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("%s: picked entry %u from cache block\n", __func__, id);
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find_next_mrc_cache() - get next cache entry
|
|
|
|
*
|
2019-12-07 04:42:02 +00:00
|
|
|
* This moves to the next cache entry in the region, making sure it has enough
|
|
|
|
* space to hold data of size @data_size.
|
|
|
|
*
|
2015-01-20 05:16:14 +00:00
|
|
|
* @entry: MRC cache flash area
|
|
|
|
* @cache: Entry to start from
|
2019-12-07 04:42:02 +00:00
|
|
|
* @data_size: Required data size of the new entry. Note that we assume that
|
|
|
|
* all cache entries are the same size
|
2015-01-20 05:16:14 +00:00
|
|
|
*
|
|
|
|
* @return next cache entry if found, NULL if we got to the end
|
|
|
|
*/
|
2015-10-12 04:37:41 +00:00
|
|
|
static struct mrc_data_container *find_next_mrc_cache(struct mrc_region *entry,
|
2019-12-07 04:42:02 +00:00
|
|
|
struct mrc_data_container *prev, int data_size)
|
2015-01-20 05:16:14 +00:00
|
|
|
{
|
2019-12-07 04:42:02 +00:00
|
|
|
struct mrc_data_container *cache;
|
2015-01-20 05:16:14 +00:00
|
|
|
ulong base_addr, end_addr;
|
|
|
|
|
2015-10-12 04:37:41 +00:00
|
|
|
base_addr = entry->base + entry->offset;
|
2015-01-20 05:16:14 +00:00
|
|
|
end_addr = base_addr + entry->length;
|
|
|
|
|
2019-12-07 04:42:02 +00:00
|
|
|
/*
|
|
|
|
* We assume that all cache entries are the same size, but let's use
|
|
|
|
* data_size here for clarity.
|
|
|
|
*/
|
|
|
|
cache = next_mrc_block(prev);
|
|
|
|
if ((ulong)cache + mrc_block_size(data_size) > end_addr) {
|
2015-01-20 05:16:14 +00:00
|
|
|
/* Crossed the boundary */
|
|
|
|
cache = NULL;
|
|
|
|
debug("%s: no available entries found\n", __func__);
|
|
|
|
} else {
|
|
|
|
debug("%s: picked next entry from cache block at %p\n",
|
|
|
|
__func__, cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2019-12-07 04:42:09 +00:00
|
|
|
/**
|
|
|
|
* mrccache_update() - update the MRC cache with a new record
|
|
|
|
*
|
|
|
|
* This writes a new record to the end of the MRC cache region. If the new
|
|
|
|
* record is the same as the latest record then the write is skipped
|
|
|
|
*
|
|
|
|
* @sf: SPI flash to write to
|
|
|
|
* @entry: Position and size of MRC cache in SPI flash
|
|
|
|
* @cur: Record to write
|
|
|
|
* @return 0 if updated, -EEXIST if the record is the same as the latest
|
|
|
|
* record, -EINVAL if the record is not valid, other error if SPI write failed
|
|
|
|
*/
|
|
|
|
static int mrccache_update(struct udevice *sf, struct mrc_region *entry,
|
|
|
|
struct mrc_data_container *cur)
|
2015-01-20 05:16:14 +00:00
|
|
|
{
|
|
|
|
struct mrc_data_container *cache;
|
|
|
|
ulong offset;
|
|
|
|
ulong base_addr;
|
|
|
|
int ret;
|
|
|
|
|
2019-04-26 03:58:59 +00:00
|
|
|
if (!is_mrc_cache(cur)) {
|
|
|
|
debug("%s: Cache data not valid\n", __func__);
|
2015-10-12 04:37:37 +00:00
|
|
|
return -EINVAL;
|
2019-04-26 03:58:59 +00:00
|
|
|
}
|
2015-10-12 04:37:37 +00:00
|
|
|
|
2015-01-20 05:16:14 +00:00
|
|
|
/* Find the last used block */
|
2015-10-12 04:37:41 +00:00
|
|
|
base_addr = entry->base + entry->offset;
|
2015-01-20 05:16:14 +00:00
|
|
|
debug("Updating MRC cache data\n");
|
|
|
|
cache = mrccache_find_current(entry);
|
|
|
|
if (cache && (cache->data_size == cur->data_size) &&
|
|
|
|
(!memcmp(cache, cur, cache->data_size + sizeof(*cur)))) {
|
|
|
|
debug("MRC data in flash is up to date. No update\n");
|
|
|
|
return -EEXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move to the next block, which will be the first unused block */
|
|
|
|
if (cache)
|
2019-12-07 04:42:02 +00:00
|
|
|
cache = find_next_mrc_cache(entry, cache, cur->data_size);
|
2015-01-20 05:16:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have got to the end, erase the entire mrc-cache area and start
|
|
|
|
* again at block 0.
|
|
|
|
*/
|
|
|
|
if (!cache) {
|
|
|
|
debug("Erasing the MRC cache region of %x bytes at %x\n",
|
|
|
|
entry->length, entry->offset);
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
ret = spi_flash_erase_dm(sf, entry->offset, entry->length);
|
2015-01-20 05:16:14 +00:00
|
|
|
if (ret) {
|
|
|
|
debug("Failed to erase flash region\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
cache = (struct mrc_data_container *)base_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the data out */
|
|
|
|
offset = (ulong)cache - base_addr + entry->offset;
|
|
|
|
debug("Write MRC cache update to flash at %lx\n", offset);
|
2015-03-26 15:29:26 +00:00
|
|
|
ret = spi_flash_write_dm(sf, offset, cur->data_size + sizeof(*cur),
|
|
|
|
cur);
|
2015-01-20 05:16:14 +00:00
|
|
|
if (ret) {
|
|
|
|
debug("Failed to write to SPI flash\n");
|
2019-12-07 04:42:06 +00:00
|
|
|
return log_msg_ret("Cannot update mrccache", ret);
|
2015-01-20 05:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-12 04:37:39 +00:00
|
|
|
|
2019-12-07 04:42:07 +00:00
|
|
|
static void mrccache_setup(struct mrc_output *mrc, void *data)
|
2015-10-12 04:37:39 +00:00
|
|
|
{
|
2019-04-26 03:58:57 +00:00
|
|
|
struct mrc_data_container *cache = data;
|
2015-10-12 04:37:39 +00:00
|
|
|
u16 checksum;
|
|
|
|
|
|
|
|
cache->signature = MRC_DATA_SIGNATURE;
|
2019-12-07 04:42:07 +00:00
|
|
|
cache->data_size = mrc->len;
|
|
|
|
checksum = compute_ip_checksum(mrc->buf, cache->data_size);
|
2015-10-12 04:37:39 +00:00
|
|
|
debug("Saving %d bytes for MRC output data, checksum %04x\n",
|
|
|
|
cache->data_size, checksum);
|
|
|
|
cache->checksum = checksum;
|
|
|
|
cache->reserved = 0;
|
2019-12-07 04:42:07 +00:00
|
|
|
memcpy(cache->data, mrc->buf, cache->data_size);
|
2015-10-12 04:37:39 +00:00
|
|
|
|
2019-12-07 04:42:07 +00:00
|
|
|
mrc->cache = cache;
|
2019-04-26 03:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int mrccache_reserve(void)
|
|
|
|
{
|
2019-12-07 04:42:07 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MRC_TYPE_COUNT; i++) {
|
|
|
|
struct mrc_output *mrc = &gd->arch.mrc[i];
|
2019-04-26 03:58:57 +00:00
|
|
|
|
2019-12-07 04:42:07 +00:00
|
|
|
if (!mrc->len)
|
|
|
|
continue;
|
2015-10-12 04:37:39 +00:00
|
|
|
|
2019-12-07 04:42:07 +00:00
|
|
|
/* adjust stack pointer to store pure cache data plus header */
|
|
|
|
gd->start_addr_sp -= (mrc->len + MRC_DATA_HEADER_SIZE);
|
|
|
|
mrccache_setup(mrc, (void *)gd->start_addr_sp);
|
|
|
|
|
|
|
|
gd->start_addr_sp &= ~0xf;
|
|
|
|
}
|
2015-10-12 04:37:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-07 04:42:07 +00:00
|
|
|
int mrccache_get_region(enum mrc_type_t type, struct udevice **devp,
|
|
|
|
struct mrc_region *entry)
|
2015-10-12 04:37:39 +00:00
|
|
|
{
|
2019-12-07 04:42:03 +00:00
|
|
|
struct udevice *dev;
|
|
|
|
ofnode mrc_node;
|
2019-12-07 04:42:04 +00:00
|
|
|
ulong map_base;
|
|
|
|
uint map_size;
|
|
|
|
uint offset;
|
2015-10-12 04:37:41 +00:00
|
|
|
u32 reg[2];
|
2015-10-12 04:37:39 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-12-07 04:42:03 +00:00
|
|
|
/*
|
|
|
|
* Find the flash chip within the SPI controller node. Avoid probing
|
|
|
|
* the device here since it may put it into a strange state where the
|
|
|
|
* memory map cannot be read.
|
|
|
|
*/
|
|
|
|
ret = uclass_find_first_device(UCLASS_SPI_FLASH, &dev);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("Cannot find SPI flash\n", ret);
|
2019-12-07 04:42:04 +00:00
|
|
|
ret = dm_spi_get_mmap(dev, &map_base, &map_size, &offset);
|
|
|
|
if (!ret) {
|
|
|
|
entry->base = map_base;
|
|
|
|
} else {
|
|
|
|
ret = dev_read_u32_array(dev, "memory-map", reg, 2);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("Cannot find memory map\n", ret);
|
|
|
|
entry->base = reg[0];
|
|
|
|
}
|
2015-10-12 04:37:41 +00:00
|
|
|
|
2015-10-12 04:37:39 +00:00
|
|
|
/* Find the place where we put the MRC cache */
|
2019-12-07 04:42:08 +00:00
|
|
|
mrc_node = dev_read_subnode(dev, type == MRC_TYPE_NORMAL ?
|
|
|
|
"rw-mrc-cache" : "rw-var-mrc-cache");
|
2019-12-07 04:42:03 +00:00
|
|
|
if (!ofnode_valid(mrc_node))
|
|
|
|
return log_msg_ret("Cannot find node", -EPERM);
|
2015-10-12 04:37:39 +00:00
|
|
|
|
2019-12-07 04:42:03 +00:00
|
|
|
ret = ofnode_read_u32_array(mrc_node, "reg", reg, 2);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("Cannot find address", ret);
|
2015-10-12 04:37:41 +00:00
|
|
|
entry->offset = reg[0];
|
|
|
|
entry->length = reg[1];
|
2015-10-12 04:37:39 +00:00
|
|
|
|
2019-12-07 04:42:03 +00:00
|
|
|
if (devp)
|
|
|
|
*devp = dev;
|
2019-12-07 04:42:07 +00:00
|
|
|
debug("MRC cache type %d in '%s', offset %x, len %x, base %x\n",
|
|
|
|
type, dev->name, entry->offset, entry->length, entry->base);
|
2015-10-12 04:37:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-07 04:42:07 +00:00
|
|
|
static int mrccache_save_type(enum mrc_type_t type)
|
2015-10-12 04:37:39 +00:00
|
|
|
{
|
2019-12-07 04:42:05 +00:00
|
|
|
struct mrc_data_container *cache;
|
2019-12-07 04:42:07 +00:00
|
|
|
struct mrc_output *mrc;
|
2015-10-12 04:37:41 +00:00
|
|
|
struct mrc_region entry;
|
2015-10-12 04:37:39 +00:00
|
|
|
struct udevice *sf;
|
|
|
|
int ret;
|
|
|
|
|
2019-12-07 04:42:07 +00:00
|
|
|
mrc = &gd->arch.mrc[type];
|
|
|
|
if (!mrc->len)
|
2015-10-12 04:37:39 +00:00
|
|
|
return 0;
|
2019-12-07 04:42:07 +00:00
|
|
|
log_debug("Saving %#x bytes of MRC output data type %d to SPI flash\n",
|
|
|
|
mrc->len, type);
|
|
|
|
ret = mrccache_get_region(type, &sf, &entry);
|
2019-12-07 04:42:03 +00:00
|
|
|
if (ret)
|
2019-12-07 04:42:06 +00:00
|
|
|
return log_msg_ret("Cannot get region", ret);
|
2019-12-07 04:42:03 +00:00
|
|
|
ret = device_probe(sf);
|
2015-10-12 04:37:39 +00:00
|
|
|
if (ret)
|
2019-12-07 04:42:06 +00:00
|
|
|
return log_msg_ret("Cannot probe device", ret);
|
2019-12-07 04:42:07 +00:00
|
|
|
cache = mrc->cache;
|
|
|
|
|
2019-12-07 04:42:05 +00:00
|
|
|
ret = mrccache_update(sf, &entry, cache);
|
2019-12-07 04:42:06 +00:00
|
|
|
if (!ret)
|
2019-12-07 04:42:05 +00:00
|
|
|
debug("Saved MRC data with checksum %04x\n", cache->checksum);
|
2019-12-07 04:42:06 +00:00
|
|
|
else if (ret == -EEXIST)
|
2016-01-17 23:11:29 +00:00
|
|
|
debug("MRC data is the same as last time, skipping save\n");
|
2015-10-12 04:37:39 +00:00
|
|
|
|
2019-12-07 04:42:06 +00:00
|
|
|
return 0;
|
2015-10-12 04:37:39 +00:00
|
|
|
}
|
2019-04-26 03:58:57 +00:00
|
|
|
|
2019-12-07 04:42:07 +00:00
|
|
|
int mrccache_save(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MRC_TYPE_COUNT; i++) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = mrccache_save_type(i);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-26 03:58:57 +00:00
|
|
|
int mrccache_spl_save(void)
|
|
|
|
{
|
2019-12-07 04:42:07 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MRC_TYPE_COUNT; i++) {
|
|
|
|
struct mrc_output *mrc = &gd->arch.mrc[i];
|
|
|
|
void *data;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
size = mrc->len + MRC_DATA_HEADER_SIZE;
|
|
|
|
data = malloc(size);
|
|
|
|
if (!data)
|
|
|
|
return log_msg_ret("Allocate MRC cache block", -ENOMEM);
|
|
|
|
mrccache_setup(mrc, data);
|
|
|
|
}
|
2019-04-26 03:58:57 +00:00
|
|
|
|
|
|
|
return mrccache_save();
|
|
|
|
}
|