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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2014-2026 TQ-Systems GmbH <[email protected]>,
* D-82229 Seefeld, Germany.
* Author: Nora Schiffer
*/
#include <dm.h>
#include <log.h>
#include <net.h>
#include <dm/device_compat.h>
#include <linux/build_bug.h>
#include <linux/ctype.h>
#include <nvmem.h>
#include <sysinfo/tq_eeprom.h>
#define TQ_EE_RSV1_BYTES 10
#define TQ_EE_SERIAL_BYTES 8
#define TQ_EE_RSV2_BYTES 8
#define TQ_EE_BDID_BYTES 0x40
struct tq_eeprom_data {
u8 mac[ETH_ALEN]; /* 0x20 ... 0x25 */
u8 rsv1[TQ_EE_RSV1_BYTES];
u8 serial[TQ_EE_SERIAL_BYTES]; /* 0x30 ... 0x37 */
u8 rsv2[TQ_EE_RSV2_BYTES];
u8 id[TQ_EE_BDID_BYTES]; /* 0x40 ... 0x7f */
};
static_assert(sizeof(struct tq_eeprom_data) == 0x60,
"struct tq_eeprom_data has incorrect size");
/**
* struct sysinfo_tq_eeprom_priv - sysinfo private data
*/
struct sysinfo_tq_eeprom_priv {
struct nvmem_cell device_info_cell;
/* Reserve extra space for \0 in id and serial */
char id[TQ_EE_BDID_BYTES + 1];
char serial[TQ_EE_SERIAL_BYTES + 1];
u8 mac[ETH_ALEN];
};
static void tq_eeprom_parse_id(struct udevice *dev, const struct tq_eeprom_data *data)
{
struct sysinfo_tq_eeprom_priv *priv = dev_get_priv(dev);
int i;
for (i = 0; i < sizeof(data->id); i++) {
if (!(isprint(data->id[i]) && isascii(data->id[i])))
break;
}
if (i == 0)
dev_warn(dev, "no valid model name in EEPROM\n");
snprintf(priv->id, sizeof(priv->id), "%.*s", i, data->id);
}
static int tq_eeprom_serial_len(const struct tq_eeprom_data *data, bool allow_upper)
{
int i;
for (i = 0; i < sizeof(data->serial); i++) {
if (!(isdigit(data->serial[i]) || (allow_upper && isupper(data->serial[i]))))
break;
}
return i;
}
static void tq_eeprom_parse_serial(struct udevice *dev, const struct tq_eeprom_data *data)
{
struct sysinfo_tq_eeprom_priv *priv = dev_get_priv(dev);
bool use_new_format;
int len;
use_new_format = data->serial[0] == 'T' && data->serial[1] == 'Q';
len = tq_eeprom_serial_len(data, use_new_format);
/* For now, only serial numbers with the exact size of the field are accepted */
if (len != sizeof(data->serial)) {
dev_warn(dev, "no valid serial number in EEPROM\n");
len = 0;
}
snprintf(priv->serial, sizeof(priv->serial), "%.*s", len, data->serial);
}
static int tq_eeprom_dump(const struct sysinfo_tq_eeprom_priv *priv)
{
printf("TQ EEPROM:\n");
printf(" ID: %s\n", priv->id[0] ? priv->id : "<invalid>");
printf(" SN: %s\n", priv->serial[0] ? priv->serial : "<invalid>");
printf(" MAC: ");
if (is_valid_ethaddr(priv->mac))
printf("%pM\n", priv->mac);
else
printf("<invalid>\n");
return 0;
}
static int sysinfo_tq_eeprom_detect(struct udevice *dev)
{
struct sysinfo_tq_eeprom_priv *priv = dev_get_priv(dev);
struct tq_eeprom_data data;
int ret;
ret = nvmem_cell_read(&priv->device_info_cell, (u8 *)&data, sizeof(data));
if (ret < 0) {
dev_err(dev, "EEPROM read failed: %d\n", ret);
return ret;
}
tq_eeprom_parse_id(dev, &data);
tq_eeprom_parse_serial(dev, &data);
memcpy(priv->mac, data.mac, ETH_ALEN);
if (!IS_ENABLED(CONFIG_SPL_BUILD))
tq_eeprom_dump(priv);
return 0;
}
static int sysinfo_tq_eeprom_get_str(struct udevice *dev, int id, size_t size, char *val)
{
struct sysinfo_tq_eeprom_priv *priv = dev_get_priv(dev);
switch (id) {
case SYSID_TQ_MODEL:
if (!priv->id[0])
return -ENODATA;
strlcpy(val, priv->id, size);
return 0;
case SYSID_TQ_SERIAL:
if (!priv->serial[0])
return -ENODATA;
strlcpy(val, priv->serial, size);
return 0;
default:
return -EINVAL;
}
}
static int sysinfo_tq_eeprom_get_data(struct udevice *dev, int id, void **data, size_t *size)
{
struct sysinfo_tq_eeprom_priv *priv = dev_get_priv(dev);
switch (id) {
case SYSID_TQ_MAC_ADDR:
if (!is_valid_ethaddr(priv->mac))
return -ENODATA;
*data = priv->mac;
*size = sizeof(priv->mac);
return 0;
default:
return -EINVAL;
}
}
static const struct sysinfo_ops sysinfo_tq_eeprom_ops = {
.detect = sysinfo_tq_eeprom_detect,
.get_str = sysinfo_tq_eeprom_get_str,
.get_data = sysinfo_tq_eeprom_get_data,
};
static int sysinfo_tq_eeprom_probe(struct udevice *dev)
{
struct sysinfo_tq_eeprom_priv *priv = dev_get_priv(dev);
int ret;
ret = nvmem_cell_get_by_name(dev, "device_info", &priv->device_info_cell);
if (ret) {
dev_err(dev, "device_info not found: %d\n", ret);
return ret;
}
return 0;
}
static const struct udevice_id sysinfo_tq_eeprom_ids[] = {
{ .compatible = "tq,eeprom-sysinfo" },
{ /* sentinel */ }
};
U_BOOT_DRIVER(sysinfo_tq_eeprom) = {
.name = "sysinfo_tq_eeprom",
.id = UCLASS_SYSINFO,
.of_match = sysinfo_tq_eeprom_ids,
.ops = &sysinfo_tq_eeprom_ops,
.priv_auto = sizeof(struct sysinfo_tq_eeprom_priv),
.probe = sysinfo_tq_eeprom_probe,
};
|