1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2019 Collabora
4 * (C) Copyright 2019 GE
10 #include <i2c_eeprom.h>
13 static const u8 bootcount_magic = 0xbc;
15 struct bootcount_i2c_eeprom_priv {
16 struct udevice *i2c_eeprom;
20 static int bootcount_i2c_eeprom_set(struct udevice *dev, const u32 a)
22 struct bootcount_i2c_eeprom_priv *priv = dev_get_priv(dev);
23 const u16 val = bootcount_magic << 8 | (a & 0xff);
25 if (i2c_eeprom_write(priv->i2c_eeprom, priv->offset,
26 (uint8_t *)&val, 2) < 0) {
27 debug("%s: write failed\n", __func__);
34 static int bootcount_i2c_eeprom_get(struct udevice *dev, u32 *a)
36 struct bootcount_i2c_eeprom_priv *priv = dev_get_priv(dev);
39 if (i2c_eeprom_read(priv->i2c_eeprom, priv->offset,
40 (uint8_t *)&val, 2) < 0) {
41 debug("%s: read failed\n", __func__);
45 if (val >> 8 == bootcount_magic) {
50 debug("%s: bootcount magic does not match on %04x\n", __func__, val);
54 static int bootcount_i2c_eeprom_probe(struct udevice *dev)
56 struct ofnode_phandle_args phandle_args;
57 struct bootcount_i2c_eeprom_priv *priv = dev_get_priv(dev);
58 struct udevice *i2c_eeprom;
60 if (dev_read_phandle_with_args(dev, "i2c-eeprom", NULL, 0, 0,
62 debug("%s: i2c-eeprom backing device not specified\n",
67 if (uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, phandle_args.node,
69 debug("%s: could not get backing device\n", dev->name);
73 priv->i2c_eeprom = i2c_eeprom;
74 priv->offset = dev_read_u32_default(dev, "offset", 0);
79 static const struct bootcount_ops bootcount_i2c_eeprom_ops = {
80 .get = bootcount_i2c_eeprom_get,
81 .set = bootcount_i2c_eeprom_set,
84 static const struct udevice_id bootcount_i2c_eeprom_ids[] = {
85 { .compatible = "u-boot,bootcount-i2c-eeprom" },
89 U_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,