Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 2 | /* |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 3 | * (C) Copyright 2010-2012 |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 4 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 7 | #include <bootcount.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 8 | #include <cpu_func.h> |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 9 | #include <linux/compiler.h> |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 10 | |
Heiko Schocher | 80e8b8a | 2020-03-02 15:43:59 +0100 | [diff] [blame^] | 11 | #if !defined(CONFIG_DM_BOOTCOUNT) |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 12 | /* Now implement the generic default functions */ |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 13 | __weak void bootcount_store(ulong a) |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 14 | { |
| 15 | void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR; |
Alex Kiernan | fe9805f | 2018-07-25 11:45:58 +0000 | [diff] [blame] | 16 | uintptr_t flush_start = rounddown(CONFIG_SYS_BOOTCOUNT_ADDR, |
| 17 | CONFIG_SYS_CACHELINE_SIZE); |
| 18 | uintptr_t flush_end; |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 19 | |
| 20 | #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) |
Marek Vasut | 758694f | 2018-10-11 00:13:54 +0200 | [diff] [blame] | 21 | raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a); |
Alex Kiernan | fe9805f | 2018-07-25 11:45:58 +0000 | [diff] [blame] | 22 | |
| 23 | flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4, |
| 24 | CONFIG_SYS_CACHELINE_SIZE); |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 25 | #else |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 26 | raw_bootcount_store(reg, a); |
Marek Vasut | 758694f | 2018-10-11 00:13:54 +0200 | [diff] [blame] | 27 | raw_bootcount_store(reg + 4, CONFIG_SYS_BOOTCOUNT_MAGIC); |
Alex Kiernan | fe9805f | 2018-07-25 11:45:58 +0000 | [diff] [blame] | 28 | |
| 29 | flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 8, |
| 30 | CONFIG_SYS_CACHELINE_SIZE); |
Robert P. J. Day | 7676537 | 2015-12-22 07:15:14 -0500 | [diff] [blame] | 31 | #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD */ |
Alex Kiernan | fe9805f | 2018-07-25 11:45:58 +0000 | [diff] [blame] | 32 | flush_dcache_range(flush_start, flush_end); |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 35 | __weak ulong bootcount_load(void) |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 36 | { |
| 37 | void *reg = (void *)CONFIG_SYS_BOOTCOUNT_ADDR; |
| 38 | |
| 39 | #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 40 | u32 tmp = raw_bootcount_load(reg); |
Michael Weiss | 59dde44 | 2010-05-20 16:09:35 +0200 | [diff] [blame] | 41 | |
Marek Vasut | 758694f | 2018-10-11 00:13:54 +0200 | [diff] [blame] | 42 | if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000)) |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 43 | return 0; |
| 44 | else |
Michael Weiss | 59dde44 | 2010-05-20 16:09:35 +0200 | [diff] [blame] | 45 | return (tmp & 0x0000ffff); |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 46 | #else |
Marek Vasut | 758694f | 2018-10-11 00:13:54 +0200 | [diff] [blame] | 47 | if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC) |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 48 | return 0; |
| 49 | else |
Stefan Roese | 0044c42 | 2012-08-16 17:55:41 +0000 | [diff] [blame] | 50 | return raw_bootcount_load(reg); |
Robert P. J. Day | 7676537 | 2015-12-22 07:15:14 -0500 | [diff] [blame] | 51 | #endif /* defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD) */ |
Stefan Roese | e4a95d1 | 2010-04-28 10:47:36 +0200 | [diff] [blame] | 52 | } |
Heiko Schocher | 80e8b8a | 2020-03-02 15:43:59 +0100 | [diff] [blame^] | 53 | #else |
| 54 | #include <dm.h> |
| 55 | |
| 56 | /* |
| 57 | * struct bootcount_mem_priv - private bootcount mem driver data |
| 58 | * |
| 59 | * @base: base address used for bootcounter |
| 60 | * @singleword: if true use only one 32 bit word for bootcounter |
| 61 | */ |
| 62 | struct bootcount_mem_priv { |
| 63 | phys_addr_t base; |
| 64 | bool singleword; |
| 65 | }; |
| 66 | |
| 67 | static int bootcount_mem_get(struct udevice *dev, u32 *a) |
| 68 | { |
| 69 | struct bootcount_mem_priv *priv = dev_get_priv(dev); |
| 70 | void *reg = (void *)priv->base; |
| 71 | u32 magic = CONFIG_SYS_BOOTCOUNT_MAGIC; |
| 72 | |
| 73 | if (priv->singleword) { |
| 74 | u32 tmp = raw_bootcount_load(reg); |
| 75 | |
| 76 | if ((tmp & 0xffff0000) != (magic & 0xffff0000)) |
| 77 | return -ENODEV; |
| 78 | |
| 79 | *a = (tmp & 0x0000ffff); |
| 80 | } else { |
| 81 | if (raw_bootcount_load(reg + 4) != magic) |
| 82 | return -ENODEV; |
| 83 | |
| 84 | *a = raw_bootcount_load(reg); |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | }; |
| 89 | |
| 90 | static int bootcount_mem_set(struct udevice *dev, const u32 a) |
| 91 | { |
| 92 | struct bootcount_mem_priv *priv = dev_get_priv(dev); |
| 93 | void *reg = (void *)priv->base; |
| 94 | u32 magic = CONFIG_SYS_BOOTCOUNT_MAGIC; |
| 95 | uintptr_t flush_start = rounddown(priv->base, |
| 96 | CONFIG_SYS_CACHELINE_SIZE); |
| 97 | uintptr_t flush_end; |
| 98 | |
| 99 | if (priv->singleword) { |
| 100 | raw_bootcount_store(reg, (magic & 0xffff0000) | a); |
| 101 | flush_end = roundup(priv->base + 4, |
| 102 | CONFIG_SYS_CACHELINE_SIZE); |
| 103 | } else { |
| 104 | raw_bootcount_store(reg, a); |
| 105 | raw_bootcount_store(reg + 4, magic); |
| 106 | flush_end = roundup(priv->base + 8, |
| 107 | CONFIG_SYS_CACHELINE_SIZE); |
| 108 | } |
| 109 | flush_dcache_range(flush_start, flush_end); |
| 110 | |
| 111 | return 0; |
| 112 | }; |
| 113 | |
| 114 | static const struct bootcount_ops bootcount_mem_ops = { |
| 115 | .get = bootcount_mem_get, |
| 116 | .set = bootcount_mem_set, |
| 117 | }; |
| 118 | |
| 119 | static int bootcount_mem_probe(struct udevice *dev) |
| 120 | { |
| 121 | struct bootcount_mem_priv *priv = dev_get_priv(dev); |
| 122 | |
| 123 | priv->base = (phys_addr_t)dev_read_addr(dev); |
| 124 | if (dev_read_bool(dev, "single-word")) |
| 125 | priv->singleword = true; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static const struct udevice_id bootcount_mem_ids[] = { |
| 131 | { .compatible = "u-boot,bootcount" }, |
| 132 | { } |
| 133 | }; |
| 134 | |
| 135 | U_BOOT_DRIVER(bootcount_mem) = { |
| 136 | .name = "bootcount-mem", |
| 137 | .id = UCLASS_BOOTCOUNT, |
| 138 | .priv_auto_alloc_size = sizeof(struct bootcount_mem_priv), |
| 139 | .probe = bootcount_mem_probe, |
| 140 | .of_match = bootcount_mem_ids, |
| 141 | .ops = &bootcount_mem_ops, |
| 142 | }; |
| 143 | #endif |