Philip Oberfichtner | 7503e27 | 2022-03-18 12:04:38 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018-2022 Denx Software Engineering GmbH |
| 4 | * Heiko Schocher <hs@denx.de> |
| 5 | * Philip Oberfichtner <pro@denx.de> |
| 6 | * |
| 7 | * A bootcount driver using the registers MEMA - MEMD on the PFUZE100. |
Philip Oberfichtner | 33e48bf | 2022-09-19 10:11:00 +0200 | [diff] [blame] | 8 | * This works only, if the PMIC is not connected to a battery. |
Philip Oberfichtner | 7503e27 | 2022-03-18 12:04:38 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
Philip Oberfichtner | 7503e27 | 2022-03-18 12:04:38 +0100 | [diff] [blame] | 11 | #include <bootcount.h> |
| 12 | #include <dm.h> |
| 13 | #include <power/pmic.h> |
| 14 | #include <power/pfuze100_pmic.h> |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | #define PFUZE_BC_MAGIC 0xdead |
| 19 | |
| 20 | struct bootcount_pmic_priv { |
| 21 | struct udevice *pmic; |
| 22 | }; |
| 23 | |
| 24 | static int pfuze100_get_magic(struct udevice *dev, u32 *magic) |
| 25 | { |
| 26 | int ret; |
| 27 | |
| 28 | ret = pmic_reg_read(dev, PFUZE100_MEMA); |
| 29 | if (ret < 0) |
| 30 | return ret; |
| 31 | *magic = ret; |
| 32 | |
| 33 | ret = pmic_reg_read(dev, PFUZE100_MEMB); |
| 34 | if (ret < 0) |
| 35 | return ret; |
| 36 | *magic += ret << 8; |
| 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static int pfuze100_set_magic(struct udevice *dev) |
| 42 | { |
| 43 | int ret; |
| 44 | |
| 45 | ret = pmic_reg_write(dev, PFUZE100_MEMA, PFUZE_BC_MAGIC & 0xff); |
| 46 | if (ret) |
| 47 | return ret; |
| 48 | |
| 49 | ret = pmic_reg_write(dev, PFUZE100_MEMB, (PFUZE_BC_MAGIC >> 8) & 0xff); |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | static int pfuze100_get_value(struct udevice *dev, u32 *a) |
| 54 | { |
| 55 | int ret; |
| 56 | |
| 57 | ret = pmic_reg_read(dev, PFUZE100_MEMC); |
| 58 | if (ret < 0) |
| 59 | return ret; |
| 60 | *a = ret; |
| 61 | |
| 62 | ret = pmic_reg_read(dev, PFUZE100_MEMD); |
| 63 | if (ret < 0) |
| 64 | return ret; |
| 65 | *a += ret << 8; |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int pfuze100_set_value(struct udevice *dev, u32 val) |
| 71 | { |
| 72 | int ret; |
| 73 | |
| 74 | ret = pmic_reg_write(dev, PFUZE100_MEMC, val & 0xff); |
| 75 | if (ret) |
| 76 | return ret; |
| 77 | |
| 78 | ret = pmic_reg_write(dev, PFUZE100_MEMD, (val >> 8) & 0xff); |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | static int bootcount_pmic_set(struct udevice *dev, const u32 a) |
| 83 | { |
| 84 | struct bootcount_pmic_priv *priv = dev_get_priv(dev); |
| 85 | |
| 86 | if (pfuze100_set_magic(priv->pmic)) { |
| 87 | debug("%s: writing magic failed\n", __func__); |
| 88 | return -EIO; |
| 89 | } |
| 90 | |
| 91 | if (pfuze100_set_value(priv->pmic, a)) { |
| 92 | debug("%s: writing value failed\n", __func__); |
| 93 | return -EIO; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int bootcount_pmic_get(struct udevice *dev, u32 *a) |
| 100 | { |
| 101 | struct bootcount_pmic_priv *priv = dev_get_priv(dev); |
| 102 | u32 magic; |
| 103 | |
| 104 | if (pfuze100_get_magic(priv->pmic, &magic)) { |
| 105 | debug("%s: reading magic failed\n", __func__); |
| 106 | return -EIO; |
| 107 | } |
| 108 | |
| 109 | if (magic != PFUZE_BC_MAGIC) { |
| 110 | *a = 0; |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | if (pfuze100_get_value(priv->pmic, a)) { |
| 115 | debug("%s: reading value failed\n", __func__); |
| 116 | return -EIO; |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int bootcount_pmic_probe(struct udevice *dev) |
| 123 | { |
| 124 | struct ofnode_phandle_args phandle_args; |
| 125 | struct bootcount_pmic_priv *priv = dev_get_priv(dev); |
| 126 | struct udevice *pmic; |
| 127 | |
| 128 | if (dev_read_phandle_with_args(dev, "pmic", NULL, 0, 0, &phandle_args)) { |
| 129 | debug("%s: pmic backing device not specified\n", dev->name); |
| 130 | return -ENOENT; |
| 131 | } |
| 132 | |
| 133 | if (uclass_get_device_by_ofnode(UCLASS_PMIC, phandle_args.node, &pmic)) { |
| 134 | debug("%s: could not get backing device\n", dev->name); |
| 135 | return -ENODEV; |
| 136 | } |
| 137 | |
| 138 | priv->pmic = pmic; |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static const struct bootcount_ops bootcount_pmic_ops = { |
| 144 | .get = bootcount_pmic_get, |
| 145 | .set = bootcount_pmic_set, |
| 146 | }; |
| 147 | |
| 148 | static const struct udevice_id bootcount_pmic_ids[] = { |
| 149 | { .compatible = "u-boot,bootcount-pmic" }, |
| 150 | { } |
| 151 | }; |
| 152 | |
| 153 | U_BOOT_DRIVER(bootcount_pmic) = { |
| 154 | .name = "bootcount-pmic", |
| 155 | .id = UCLASS_BOOTCOUNT, |
| 156 | .priv_auto = sizeof(struct bootcount_pmic_priv), |
| 157 | .probe = bootcount_pmic_probe, |
| 158 | .of_match = bootcount_pmic_ids, |
| 159 | .ops = &bootcount_pmic_ops, |
| 160 | }; |