summaryrefslogtreecommitdiffstats
path: root/drivers/soc/kvx/kvx_socinfo.c
blob: 87c20c327be9b309d7da9c8e342f6e344f04c974 (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
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2020 Kalray Inc., Clément Léger
 */

#define pr_fmt(fmt) "kvx_socinfo: " fmt

#include <init.h>
#include <driver.h>
#include <globalvar.h>
#include <magicvar.h>
#include <command.h>
#include <libfile.h>
#include <getopt.h>
#include <common.h>
#include <fs.h>

#include <asm/sfr.h>

#include <linux/kernel.h>
#include <linux/nvmem-consumer.h>

#define LOT_ID_STR_LEN	8

#define EWS_LOT_ID_MASK		0x1ffffffffffULL
#define EWS_WAFER_ID_SHIFT	42
#define EWS_WAFER_ID_MASK	0x1fULL

#define FT_COM_AP_SHIFT		16
#define FT_COM_AP_MASK		0x3f
#define FT_DEVICE_ID_SHIFT	22
#define FT_DEVICE_ID_MASK	0x1ff

static char *kvx_mppa_id;
static char *kvx_arch_rev;
static char *kvx_board_sn;

BAREBOX_MAGICVAR(kvx.arch_rev, "KVX architecture revision");
BAREBOX_MAGICVAR(kvx.mppa_id, "KVX MPPA chip id");
BAREBOX_MAGICVAR(kvx.board_sn, "KVX board sn");

static void kvx_soc_info_read_revision(void)
{
	u64 pcr = kvx_sfr_get(PCR);
	u8 sv = kvx_sfr_field_val(pcr, PCR, SV);
	u8 car = kvx_sfr_field_val(pcr, PCR, CAR);
	const char *car_str = "", *ver_str = "";

	switch (car) {
	case 0:
		car_str = "kv3";
		break;
	}

	switch (sv) {
	case 0:
		ver_str = "1";
		break;
	case 1:
		ver_str = "2";
		break;
	}

	kvx_arch_rev = basprintf("%s-%s", car_str, ver_str);

	globalvar_add_simple_string("kvx.arch_rev", &kvx_arch_rev);
}

static int base38_decode(char *s, u64 val, int nb_char)
{
	int i;
	const char *alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_?";
	const int base = strlen(alphabet);

	if (s == NULL)
		return -1;

	for (i = 0; i < nb_char; i++) {
		s[i] = alphabet[val % base];
		val /= base;
	}

	return 0;
}

static int kvx_read_mppa_id(struct device_node *socinfo)
{
	char lot_id[LOT_ID_STR_LEN + 1] = "";
	char com_ap;
	u64 *cell_val64;
	u64 ews_val;
	u32 *cell_val32;
	u32 ft_val;
	u8 wafer_id;
	u16 device_id;

	cell_val64 = (u64 *) nvmem_cell_get_and_read(socinfo, "ews_fuse", 8);
	if (IS_ERR(cell_val64)) {
		pr_debug("Fail to read ews_fuse\n");
		return PTR_ERR(cell_val64);
	}

	ews_val = *cell_val64;
	ews_val = (ews_val >> 32) | (ews_val << 32);
	wafer_id = (ews_val >> EWS_WAFER_ID_SHIFT) & EWS_WAFER_ID_MASK;
	base38_decode(lot_id, ews_val & EWS_LOT_ID_MASK, LOT_ID_STR_LEN);
	free(cell_val64);

	cell_val32 = (u32 *) nvmem_cell_get_and_read(socinfo, "ft_fuse", 4);
	if (IS_ERR(cell_val32)) {
		pr_debug("Fail to read ft_fuse\n");
		return PTR_ERR(cell_val32);
	}

	ft_val = *cell_val32;
	device_id = (ft_val >> FT_DEVICE_ID_SHIFT) & FT_DEVICE_ID_MASK;
	base38_decode(&com_ap, (ft_val >> FT_COM_AP_SHIFT) & FT_COM_AP_MASK, 1);
	free(cell_val32);

	kvx_mppa_id = basprintf("%sA-%d%c-%03d", lot_id, wafer_id, com_ap,
			       device_id);

	globalvar_add_simple_string("kvx.mppa_id", &kvx_mppa_id);

	return 0;
}

static int kvx_read_board_sn(struct device_node *socinfo)
{
	struct nvmem_cell *cell;
	size_t len;
	char *sn;

	cell = of_nvmem_cell_get(socinfo, "board_sn");
	if (IS_ERR(cell)) {
		pr_debug("Fail to get board_sn cell\n");
		return PTR_ERR(cell);
	}

	sn = (char *)nvmem_cell_read(cell, &len);
	nvmem_cell_put(cell);
	if (IS_ERR(sn)) {
		pr_debug("Fail to read board_sn\n");
		return PTR_ERR(sn);
	}

	kvx_board_sn = xzalloc(len + 1);
	memcpy(kvx_board_sn, sn, len);
	globalvar_add_simple_string("kvx.board_sn", &kvx_board_sn);
	free(sn);

	return 0;
}

static int kvx_socinfo_probe(struct device *dev)
{
	kvx_soc_info_read_revision();

	kvx_read_board_sn(dev->device_node);

	return kvx_read_mppa_id(dev->device_node);
}

static const struct of_device_id kvx_socinfo_dt_ids[] = {
	{ .compatible = "kalray,kvx-socinfo" },
	{ }
};
MODULE_DEVICE_TABLE(of, kvx_socinfo_dt_ids);

static struct driver kvx_socinfo_driver = {
	.name = "kvx-socinfo",
	.probe = kvx_socinfo_probe,
	.of_compatible = DRV_OF_COMPAT(kvx_socinfo_dt_ids),
};
coredevice_platform_driver(kvx_socinfo_driver);