summaryrefslogtreecommitdiff
path: root/board/toradex/common/tdx-eeprom.c
blob: 176302248db12cad218d7e1289188cf2142cf77c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2020 Toradex
 */

#include <dm.h>
#include <i2c_eeprom.h>

DECLARE_GLOBAL_DATA_PTR;

static int get_tdx_eeprom(u32 eeprom_id, struct udevice **devp)
{
	int ret = 0;
	int node;
	ofnode eeprom;
	char eeprom_str[16];
	const char *path;

	if (gd->fdt_blob == NULL) {
		printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
		return -EFAULT;
	}

	node = fdt_path_offset(gd->fdt_blob, "/aliases");
	if (node < 0)
		return -ENODEV;

	sprintf(eeprom_str, "eeprom%d", eeprom_id);

	path = fdt_getprop(gd->fdt_blob, node, eeprom_str, NULL);
	if (!path) {
		printf("%s: no alias for %s\n", __func__, eeprom_str);
		return -ENODEV;
	}

	eeprom = ofnode_path(path);
	if (!ofnode_valid(eeprom)) {
		printf("%s: invalid hardware path to EEPROM\n", __func__);
		return -ENODEV;
	}

	ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, devp);
	if (ret) {
		printf("%s: cannot find EEPROM by node\n", __func__);
		return ret;
	}

	return ret;
}

#ifdef DEBUG
static void print_tdx_hex(u8 *buf, int size)
{
        unsigned char *p = (unsigned char *)buf;

	printf("Toradex config block: \n");
	for (int i = 0; i < size; ++i) {
		if (! (i % 16) && i)
			printf("\n");

		printf("0x%02x ", p[i]);
	}
        printf("\n\n");
}
#endif

int read_tdx_eeprom_data(u32 eeprom_id, int offset, u8 *buf,
			 int size)
{
	struct udevice *dev;
	int ret;

	ret = get_tdx_eeprom(eeprom_id, &dev);
	if (ret)
		return ret;

	ret = i2c_eeprom_read(dev, 0x0, buf, size);
	if (ret) {
		printf("%s: error reading data from EEPROM id: %d!, ret = %d\n",
		        __func__, eeprom_id, ret);
		return ret;
	}
#ifdef DEBUG
	print_tdx_hex(buf, size);
#endif
	return ret;
}

int write_tdx_eeprom_data(u32 eeprom_id, int offset, u8 *buf,
			  int size)
{
	struct udevice *dev;
	int ret;

	ret = get_tdx_eeprom(eeprom_id, &dev);
	if (ret)
		return ret;

#ifdef DEBUG
	print_tdx_hex(buf, size);
#endif
	ret = i2c_eeprom_write(dev, 0x0, buf, size);
	if (ret) {
		printf("%s: error writing data to EEPROM id: %d, ret = %d\n",
		        __func__, eeprom_id, ret);
		return ret;
	}

	return ret;
}