[PATCH 2/4] bootcount: add simple register support
Jean-Christophe PLAGNIOL-VILLARD
plagnioj at jcrosoft.com
Sat Sep 21 02:46:05 EDT 2013
use a register to store the boot count
if 2 ressources: one for magic, one for value
otherwise 16 upper bit for magic 16 lower for value
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj at jcrosoft.com>
---
drivers/misc/bootcount/Kconfig | 7 +++
drivers/misc/bootcount/Makefile | 1 +
drivers/misc/bootcount/register.c | 107 ++++++++++++++++++++++++++++++++++++++
3 files changed, 115 insertions(+)
create mode 100644 drivers/misc/bootcount/register.c
diff --git a/drivers/misc/bootcount/Kconfig b/drivers/misc/bootcount/Kconfig
index 77a1140..c028cf9 100644
--- a/drivers/misc/bootcount/Kconfig
+++ b/drivers/misc/bootcount/Kconfig
@@ -9,4 +9,11 @@ menuconfig BOOTCOUNT
if BOOTCOUNT
+config BOOTCOUNT_REGISTER
+ tristate "Register"
+ help
+ use a register to store the boot count
+ if 2 ressources: one for magic, one for value
+ otherwise 16 upper bit for magic 16 lower for value
+
endif # BOOTCOUNT
diff --git a/drivers/misc/bootcount/Makefile b/drivers/misc/bootcount/Makefile
index 3c02ba1..cb6cb27 100644
--- a/drivers/misc/bootcount/Makefile
+++ b/drivers/misc/bootcount/Makefile
@@ -1 +1,2 @@
obj-y += core.o
+obj-$(CONFIG_BOOTCOUNT_REGISTER) += register.o
diff --git a/drivers/misc/bootcount/register.c b/drivers/misc/bootcount/register.c
new file mode 100644
index 0000000..fc57f70
--- /dev/null
+++ b/drivers/misc/bootcount/register.c
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2013 Jean-Christophe PLAGNIOL-VILLARD <plagnioj at jcrosoft.com>
+ *
+ * GPLv2 Only
+ */
+
+#include <common.h>
+#include <driver.h>
+#include <init.h>
+#include <io.h>
+#include <errno.h>
+#include <bootcount.h>
+
+#define BOOTCOUNT_MAGIC 0xb001c041
+
+struct bootcount_register {
+ struct device_d *dev;
+ void __iomem *base;
+ void __iomem *magic;
+ void (*set)(struct bootcount_register *br);
+
+ struct bootcount_driver drv;
+};
+
+static void bootcount_register_set_one(struct bootcount_register *br)
+{
+ u32 val;
+
+ val = BOOTCOUNT_MAGIC | (br->drv.count & 0xffff);
+
+ writel(val, br->base);
+}
+
+static void bootcount_register_set_two(struct bootcount_register *br)
+{
+ writel(BOOTCOUNT_MAGIC, br->magic);
+ writel(br->drv.count, br->base);
+}
+
+static void bootcount_register_parse_one(struct bootcount_register *br)
+{
+ u32 val;
+
+ val = readl(br->base);
+
+ if ((val & 0xffff0000) != (BOOTCOUNT_MAGIC& 0xffff0000))
+ return;
+
+ br->drv.count = val & 0xffff;
+}
+
+static void bootcount_register_parse_two(struct bootcount_register *br)
+{
+ u32 val;
+
+ val = readl(br->magic);
+
+ if (val != BOOTCOUNT_MAGIC)
+ return;
+
+ br->drv.count = readl(br->base);
+}
+
+static void bootcount_register_drv_set(struct bootcount_driver *d)
+{
+ struct bootcount_register *br = d->priv;
+
+ br->set(br);
+}
+
+static int bootcount_register_probe(struct device_d *dev)
+{
+ struct bootcount_register *br;
+ int ret;
+
+ br = xzalloc(sizeof(*br));
+ br->base = dev_request_mem_region(dev, 0);
+ br->magic = dev_request_mem_region(dev, 1);
+ br->dev = dev;
+
+ br->drv.priv = br;
+ br->drv.set = bootcount_register_drv_set;
+ br->drv.parent = dev;
+ if (!br->magic) {
+ bootcount_register_parse_one(br);
+ br->set = bootcount_register_set_one;
+ } else {
+ bootcount_register_parse_two(br);
+ br->set = bootcount_register_set_two;
+ }
+ ret = register_bootcount(&br->drv);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
+static struct driver_d bootcount_register_driver = {
+ .name = "bootcount_register",
+ .probe = bootcount_register_probe,
+};
+
+static int bootcount_register_init(void)
+{
+ return platform_driver_register(&bootcount_register_driver);
+}
+postcore_initcall(bootcount_register_init);
--
1.8.4.rc1
More information about the barebox
mailing list