1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2019 Collabora
4 * (C) Copyright 2019 GE
10 #include <i2c_eeprom.h>
12 static const u8 bootcount_magic = 0xbc;
14 struct bootcount_i2c_eeprom_priv {
15 struct udevice *i2c_eeprom;
19 static int bootcount_i2c_eeprom_set(struct udevice *dev, const u32 a)
21 struct bootcount_i2c_eeprom_priv *priv = dev_get_priv(dev);
22 const u16 val = bootcount_magic << 8 | (a & 0xff);
24 if (i2c_eeprom_write(priv->i2c_eeprom, priv->offset,
25 (uint8_t *)&val, 2) < 0) {
26 debug("%s: write failed\n", __func__);
33 static int bootcount_i2c_eeprom_get(struct udevice *dev, u32 *a)
35 struct bootcount_i2c_eeprom_priv *priv = dev_get_priv(dev);
38 if (i2c_eeprom_read(priv->i2c_eeprom, priv->offset,
39 (uint8_t *)&val, 2) < 0) {
40 debug("%s: read failed\n", __func__);
44 if (val >> 8 == bootcount_magic) {
49 debug("%s: bootcount magic does not match on %04x\n", __func__, val);
53 static int bootcount_i2c_eeprom_probe(struct udevice *dev)
55 struct ofnode_phandle_args phandle_args;
56 struct bootcount_i2c_eeprom_priv *priv = dev_get_priv(dev);
57 struct udevice *i2c_eeprom;
59 if (dev_read_phandle_with_args(dev, "i2c-eeprom", NULL, 0, 0,
61 debug("%s: i2c-eeprom backing device not specified\n",
66 if (uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, phandle_args.node,
68 debug("%s: could not get backing device\n", dev->name);
72 priv->i2c_eeprom = i2c_eeprom;
73 priv->offset = dev_read_u32_default(dev, "offset", 0);
78 static const struct bootcount_ops bootcount_i2c_eeprom_ops = {
79 .get = bootcount_i2c_eeprom_get,
80 .set = bootcount_i2c_eeprom_set,
83 static const struct udevice_id bootcount_i2c_eeprom_ids[] = {
84 { .compatible = "u-boot,bootcount-i2c-eeprom" },
88 U_BOOT_DRIVER(bootcount_spi_flash) = {
89 .name = "bootcount-i2c-eeprom",
90 .id = UCLASS_BOOTCOUNT,
91 .priv_auto_alloc_size = sizeof(struct bootcount_i2c_eeprom_priv),
92 .probe = bootcount_i2c_eeprom_probe,
93 .of_match = bootcount_i2c_eeprom_ids,
94 .ops = &bootcount_i2c_eeprom_ops,