2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2010-04-28 08:47:36 +00:00
|
|
|
/*
|
2012-08-16 17:55:41 +00:00
|
|
|
* (C) Copyright 2010-2012
|
2010-04-28 08:47:36 +00:00
|
|
|
* Stefan Roese, DENX Software Engineering, sr@denx.de.
|
|
|
|
*/
|
|
|
|
|
2012-08-16 17:55:41 +00:00
|
|
|
#include <bootcount.h>
|
2019-11-14 19:57:39 +00:00
|
|
|
#include <cpu_func.h>
|
2012-08-16 17:55:41 +00:00
|
|
|
#include <linux/compiler.h>
|
2010-04-28 08:47:36 +00:00
|
|
|
|
2020-03-02 14:43:59 +00:00
|
|
|
#if !defined(CONFIG_DM_BOOTCOUNT)
|
2012-08-16 17:55:41 +00:00
|
|
|
/* Now implement the generic default functions */
|
|
|
|
__weak void bootcount_store(ulong a)
|
2010-04-28 08:47:36 +00:00
|
|
|
{
|
|
|
|
void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
|
2018-07-25 11:45:58 +00:00
|
|
|
uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR,
|
|
|
|
CONFIG_SYS_CACHELINE_SIZE);
|
|
|
|
uintptr_t flush_end;
|
2010-04-28 08:47:36 +00:00
|
|
|
|
|
|
|
#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
|
2018-10-10 22:13:54 +00:00
|
|
|
raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a);
|
2018-07-25 11:45:58 +00:00
|
|
|
|
|
|
|
flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
|
|
|
|
CONFIG_SYS_CACHELINE_SIZE);
|
2010-04-28 08:47:36 +00:00
|
|
|
#else
|
2012-08-16 17:55:41 +00:00
|
|
|
raw_bootcount_store(reg, a);
|
2018-10-10 22:13:54 +00:00
|
|
|
raw_bootcount_store(reg + 4, CONFIG_SYS_BOOTCOUNT_MAGIC);
|
2018-07-25 11:45:58 +00:00
|
|
|
|
|
|
|
flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8,
|
|
|
|
CONFIG_SYS_CACHELINE_SIZE);
|
2015-12-22 12:15:14 +00:00
|
|
|
#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */
|
2018-07-25 11:45:58 +00:00
|
|
|
flush_dcache_range(flush_start, flush_end);
|
2010-04-28 08:47:36 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 17:55:41 +00:00
|
|
|
__weak ulong bootcount_load(void)
|
2010-04-28 08:47:36 +00:00
|
|
|
{
|
|
|
|
void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR;
|
|
|
|
|
|
|
|
#if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
|
2012-08-16 17:55:41 +00:00
|
|
|
u32 tmp = raw_bootcount_load(reg);
|
2010-05-20 14:09:35 +00:00
|
|
|
|
2018-10-10 22:13:54 +00:00
|
|
|
if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
|
2010-04-28 08:47:36 +00:00
|
|
|
return 0;
|
|
|
|
else
|
2010-05-20 14:09:35 +00:00
|
|
|
return (tmp & 0x0000ffff);
|
2010-04-28 08:47:36 +00:00
|
|
|
#else
|
2018-10-10 22:13:54 +00:00
|
|
|
if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC)
|
2010-04-28 08:47:36 +00:00
|
|
|
return 0;
|
|
|
|
else
|
2012-08-16 17:55:41 +00:00
|
|
|
return raw_bootcount_load(reg);
|
2015-12-22 12:15:14 +00:00
|
|
|
#endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */
|
2010-04-28 08:47:36 +00:00
|
|
|
}
|
2020-03-02 14:43:59 +00:00
|
|
|
#else
|
|
|
|
#include <dm.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* struct bootcount_mem_priv - private bootcount mem driver data
|
|
|
|
*
|
|
|
|
* @base: base address used for bootcounter
|
|
|
|
* @singleword: if true use only one 32 bit word for bootcounter
|
|
|
|
*/
|
|
|
|
struct bootcount_mem_priv {
|
|
|
|
phys_addr_t base;
|
|
|
|
bool singleword;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bootcount_mem_get(struct udevice *dev, u32 *a)
|
|
|
|
{
|
|
|
|
struct bootcount_mem_priv *priv = dev_get_priv(dev);
|
|
|
|
void *reg = (void *)priv->base;
|
|
|
|
u32 magic = CONFIG_SYS_BOOTCOUNT_MAGIC;
|
|
|
|
|
|
|
|
if (priv->singleword) {
|
|
|
|
u32 tmp = raw_bootcount_load(reg);
|
|
|
|
|
|
|
|
if ((tmp & 0xffff0000) != (magic & 0xffff0000))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
*a = (tmp & 0x0000ffff);
|
|
|
|
} else {
|
|
|
|
if (raw_bootcount_load(reg + 4) != magic)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
*a = raw_bootcount_load(reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bootcount_mem_set(struct udevice *dev, const u32 a)
|
|
|
|
{
|
|
|
|
struct bootcount_mem_priv *priv = dev_get_priv(dev);
|
|
|
|
void *reg = (void *)priv->base;
|
|
|
|
u32 magic = CONFIG_SYS_BOOTCOUNT_MAGIC;
|
|
|
|
uintptr_t flush_start = rounddown(priv->base,
|
|
|
|
CONFIG_SYS_CACHELINE_SIZE);
|
|
|
|
uintptr_t flush_end;
|
|
|
|
|
|
|
|
if (priv->singleword) {
|
|
|
|
raw_bootcount_store(reg, (magic & 0xffff0000) | a);
|
|
|
|
flush_end = roundup(priv->base + 4,
|
|
|
|
CONFIG_SYS_CACHELINE_SIZE);
|
|
|
|
} else {
|
|
|
|
raw_bootcount_store(reg, a);
|
|
|
|
raw_bootcount_store(reg + 4, magic);
|
|
|
|
flush_end = roundup(priv->base + 8,
|
|
|
|
CONFIG_SYS_CACHELINE_SIZE);
|
|
|
|
}
|
|
|
|
flush_dcache_range(flush_start, flush_end);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct bootcount_ops bootcount_mem_ops = {
|
|
|
|
.get = bootcount_mem_get,
|
|
|
|
.set = bootcount_mem_set,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bootcount_mem_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct bootcount_mem_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
priv->base = (phys_addr_t)dev_read_addr(dev);
|
|
|
|
if (dev_read_bool(dev, "single-word"))
|
|
|
|
priv->singleword = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct udevice_id bootcount_mem_ids[] = {
|
|
|
|
{ .compatible = "u-boot,bootcount" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(bootcount_mem) = {
|
|
|
|
.name = "bootcount-mem",
|
|
|
|
.id = UCLASS_BOOTCOUNT,
|
|
|
|
.priv_auto_alloc_size = sizeof(struct bootcount_mem_priv),
|
|
|
|
.probe = bootcount_mem_probe,
|
|
|
|
.of_match = bootcount_mem_ids,
|
|
|
|
.ops = &bootcount_mem_ops,
|
|
|
|
};
|
|
|
|
#endif
|