aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/tools/eeprom-init.c
blob: b5997f22294a74728b4d042083c284b3cb561d2f (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
//
// Copyright 2017 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include "eeprom.h"
#include "eeprom-pids.h"
#include <stdio.h>
#include <stdlib.h>

int derive_rev_compat(int rev, long pid)
{
	if (pid == E320_PID) {
		if (rev >= 2)
			return 2;
	} else { /* N3x0 */
		if (rev > 5)
			return 5;
	}
	return rev;
}

int derive_dt_compat(int rev, long pid)
{
	if (pid == E320_PID) {
		return 0;
	} else { /* N3x0 */
		/* up to rev5 they were individual dts */
		if (rev > 5)
			return 5;
	}
	return rev;
}

int derive_mcu_compat(int rev, long pid)
{
	if (pid == E320_PID) {
		return 2;
	} else { /* N3x0 */
		/* up to rev5 they were individual firmware */
		if (rev > 5)
			return 5;
	}

	return rev;
}

void usage(char *argv[])
{
	printf("-- Usage -- \n");
	printf("%s serial# revision eth0 eth1 eth2 [pid] [dt-compat] [mcu-compat] [rev-compat]\n\n", argv[0]);
	printf("Example:\n");
	printf("$ %s 310A850 2 0c:22:cc:1a:25:c1 0c:22:cc:1a:25:c2 0c:22:cc:1a:25:c3 0x4242\n",
		argv[0]);
	printf("or specifying dt-compat, mcu-compat, and rev-compat explicitly:\n");
	printf("$ %s 310A850 2 0c:22:cc:1a:25:c1 0c:22:cc:1a:25:c2 0c:22:cc:1a:25:c3 0x4242 5 5 5\n",
		argv[0]);
	printf("\n");
	printf("Note: 'pid' must be specified when initializing EEPROM for the first time.\n");
	printf("Valid values are: 0x4242 (N310, N320), 0x4240 (N300), 0xe320 (E320).\n");
}

int main(int argc, char *argv[])
{
	struct usrp_sulfur_eeprom *ep, *ep_stored;
	u16 rev;
	u16 mcu_compat;
	u16 dt_compat;
	u16 rev_compat;
	long pid = 0;

	if (argc < 6 || argc > 10) {
		usage(argv);
		return EXIT_FAILURE;
	}
	rev = atoi(argv[2]);

	/* First, we try reading the existing EEPROM contents. This may fail,
	 * and that's fine. But if it works, we can use it to fill in missing
	 * defaults based on the existing data.
	 */
	ep_stored = usrp_sulfur_eeprom_from_file(NVMEM_PATH_MB);
	if (!ep_stored) {
		printf("-- EEPROM is either uninitialized or corrupt. Initializing all fields...\n");
	}

	if (argc >= 7) {
		pid = strtol(argv[6], NULL, 0);
	} else if (ep_stored) {
		pid = ntohs(ep_stored->pid);
	} else {
		printf("-- ERROR: Cannot derive PID from existing EEPROM or command line!\n");
		return EXIT_FAILURE;
	}
	if (pid != N310_PID && pid != N300_PID && pid != E320_PID) {
		printf("Invalid PID: %lX\n", pid);
		return EXIT_FAILURE;
	}

	/* If no MCU or DT compat specified, either use existing values, or
	 * derive based on rules defined at the top of this file.
	 */
	if (argc >= 8) {
		dt_compat = strtol(argv[7], NULL, 0);
		printf("dt_compat=%u\n", dt_compat);
	} else if (ep_stored) {
		dt_compat = ntohs(ep_stored->dt_compat);
		printf("dt_compat=%u\n", dt_compat);
	} else {
		dt_compat = derive_dt_compat(rev, pid);
	}

	if (argc >= 9) {
		mcu_compat = strtol(argv[8], NULL, 0);
		printf("mcu_compat=%u\n", mcu_compat);
	} else if (ep_stored) {
		mcu_compat = ntohs(ep_stored->mcu_compat);
		printf("mcu_compat=%u\n", mcu_compat);
	} else {
		mcu_compat = derive_mcu_compat(rev, pid);
	}

	if (argc >= 10) {
		rev_compat = strtol(argv[9], NULL, 0);
		printf("rev_compat=%u\n", rev_compat);
	} else if (ep_stored && rev == ep_stored->rev) {
		rev_compat = ntohs(ep_stored->rev_compat);
		printf("rev_compat=%u\n", rev_compat);
	} else {
		rev_compat = derive_rev_compat(rev, pid);
	}

	if (ep_stored)
		free(ep_stored);

	ep = usrp_sulfur_eeprom_new(NULL, (u16) pid, rev, argv[1],
			            argv[3], argv[4], argv[5], dt_compat,
				    mcu_compat, rev_compat);

	usrp_sulfur_eeprom_print(ep);
	usrp_sulfur_eeprom_to_i2c(ep, "/dev/i2c-2");
	free(ep);

	printf("-- Reading back \n");
	ep_stored = usrp_sulfur_eeprom_from_file(NVMEM_PATH_MB);
	usrp_sulfur_eeprom_print(ep_stored);
	free(ep_stored);
}