blob: a3aa0cf5595787b278e2bd68e6605df99bcdc32c [file] [log] [blame]
Robert Becketta8c32b52019-10-28 18:44:06 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2019 Collabora
4 * (C) Copyright 2019 GE
5 */
6
7#include <common.h>
8#include <bootcount.h>
9#include <dm.h>
10#include <i2c_eeprom.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Robert Becketta8c32b52019-10-28 18:44:06 +000012
13static const u8 bootcount_magic = 0xbc;
14
15struct bootcount_i2c_eeprom_priv {
16 struct udevice *i2c_eeprom;
17 u32 offset;
18};
19
20static int bootcount_i2c_eeprom_set(struct udevice *dev, const u32 a)
21{
22 struct bootcount_i2c_eeprom_priv *priv = dev_get_priv(dev);
23 const u16 val = bootcount_magic << 8 | (a & 0xff);
24
25 if (i2c_eeprom_write(priv->i2c_eeprom, priv->offset,
26 (uint8_t *)&val, 2) < 0) {
27 debug("%s: write failed\n", __func__);
28 return -EIO;
29 }
30
31 return 0;
32}
33
34static int bootcount_i2c_eeprom_get(struct udevice *dev, u32 *a)
35{
36 struct bootcount_i2c_eeprom_priv *priv = dev_get_priv(dev);
37 u16 val;
38
39 if (i2c_eeprom_read(priv->i2c_eeprom, priv->offset,
40 (uint8_t *)&val, 2) < 0) {
41 debug("%s: read failed\n", __func__);
42 return -EIO;
43 }
44
45 if (val >> 8 == bootcount_magic) {
46 *a = val & 0xff;
47 return 0;
48 }
49
50 debug("%s: bootcount magic does not match on %04x\n", __func__, val);
51 return -EIO;
52}
53
54static int bootcount_i2c_eeprom_probe(struct udevice *dev)
55{
56 struct ofnode_phandle_args phandle_args;
57 struct bootcount_i2c_eeprom_priv *priv = dev_get_priv(dev);
58 struct udevice *i2c_eeprom;
59
60 if (dev_read_phandle_with_args(dev, "i2c-eeprom", NULL, 0, 0,
61 &phandle_args)) {
62 debug("%s: i2c-eeprom backing device not specified\n",
63 dev->name);
64 return -ENOENT;
65 }
66
67 if (uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, phandle_args.node,
68 &i2c_eeprom)) {
69 debug("%s: could not get backing device\n", dev->name);
70 return -ENODEV;
71 }
72
73 priv->i2c_eeprom = i2c_eeprom;
74 priv->offset = dev_read_u32_default(dev, "offset", 0);
75
76 return 0;
77}
78
79static const struct bootcount_ops bootcount_i2c_eeprom_ops = {
80 .get = bootcount_i2c_eeprom_get,
81 .set = bootcount_i2c_eeprom_set,
82};
83
84static const struct udevice_id bootcount_i2c_eeprom_ids[] = {
85 { .compatible = "u-boot,bootcount-i2c-eeprom" },
86 { }
87};
88
89U_BOOT_DRIVER(bootcount_spi_flash) = {
90 .name = "bootcount-i2c-eeprom",
91 .id = UCLASS_BOOTCOUNT,
92 .priv_auto_alloc_size = sizeof(struct bootcount_i2c_eeprom_priv),
93 .probe = bootcount_i2c_eeprom_probe,
94 .of_match = bootcount_i2c_eeprom_ids,
95 .ops = &bootcount_i2c_eeprom_ops,
96};