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