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