1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2020 Toradex
4 */
5
6 #include <dm.h>
7 #include <i2c_eeprom.h>
8 #include <asm/global_data.h>
9 #include <linux/errno.h>
10
11 DECLARE_GLOBAL_DATA_PTR;
12
get_tdx_eeprom(u32 eeprom_id,struct udevice ** devp)13 static int get_tdx_eeprom(u32 eeprom_id, struct udevice **devp)
14 {
15 int ret = 0;
16 int node;
17 ofnode eeprom;
18 char eeprom_str[16];
19 const char *path;
20
21 if (!gd->fdt_blob) {
22 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
23 return -EFAULT;
24 }
25
26 node = fdt_path_offset(gd->fdt_blob, "/aliases");
27 if (node < 0)
28 return -ENODEV;
29
30 sprintf(eeprom_str, "eeprom%d", eeprom_id);
31
32 path = fdt_getprop(gd->fdt_blob, node, eeprom_str, NULL);
33 if (!path) {
34 printf("%s: no alias for %s\n", __func__, eeprom_str);
35 return -ENODEV;
36 }
37
38 eeprom = ofnode_path(path);
39 if (!ofnode_valid(eeprom)) {
40 printf("%s: invalid hardware path to EEPROM\n", __func__);
41 return -ENODEV;
42 }
43
44 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, devp);
45 if (ret) {
46 printf("%s: cannot find EEPROM by node\n", __func__);
47 return ret;
48 }
49
50 return ret;
51 }
52
read_tdx_eeprom_data(u32 eeprom_id,int offset,u8 * buf,int size)53 int read_tdx_eeprom_data(u32 eeprom_id, int offset, u8 *buf,
54 int size)
55 {
56 struct udevice *dev;
57 int ret;
58
59 ret = get_tdx_eeprom(eeprom_id, &dev);
60 if (ret)
61 return ret;
62
63 ret = i2c_eeprom_read(dev, 0x0, buf, size);
64 if (ret) {
65 printf("%s: error reading data from EEPROM id: %d!, ret = %d\n",
66 __func__, eeprom_id, ret);
67 return ret;
68 }
69
70 return ret;
71 }
72
write_tdx_eeprom_data(u32 eeprom_id,int offset,u8 * buf,int size)73 int write_tdx_eeprom_data(u32 eeprom_id, int offset, u8 *buf,
74 int size)
75 {
76 struct udevice *dev;
77 int ret;
78
79 ret = get_tdx_eeprom(eeprom_id, &dev);
80 if (ret)
81 return ret;
82
83 ret = i2c_eeprom_write(dev, 0x0, buf, size);
84 if (ret) {
85 printf("%s: error writing data to EEPROM id: %d, ret = %d\n",
86 __func__, eeprom_id, ret);
87 return ret;
88 }
89
90 return ret;
91 }
92