summaryrefslogtreecommitdiff
path: root/firmware/src/simtrace/prod_info.c
blob: ef2e8eba9045ca38a481e03c7699e10c0858af7d (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
/* Maintainance of production information in SPI flash
 * (C) 2013 by Harald Welte <laforge@gnumonks.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <AT91SAM7.h>
#include <lib_AT91SAM7.h>
#include <openpcd.h>

#include <os/dbgu.h>

#include "spi_flash.h"
#include "prod_info.h"

#include "../simtrace.h"

#define OTP_REGION_PRODINFO	1

#define PRODINFO_MAGIC	0x51075ACE

struct simtrace_prod_info
{
	/* magic value */
	uint32_t magic;
	/* unix timestamp of production date (0=unknown) */
	uint32_t production_ts;
	/* hardware version */
	uint32_t version;
	/* re-works applied */
	uint32_t reworks;
} __attribute__((packed));


int prod_info_write(uint32_t ts, uint32_t version, uint32_t reworks)
{
	struct simtrace_prod_info pi = {
		.magic = PRODINFO_MAGIC,
		.production_ts = ts,
		.version = version,
		.reworks = reworks,
	};
	uint8_t *pi8 = (uint8_t *) &pi;
	uint32_t addr = OTP_ADDR(OTP_REGION_PRODINFO);
	unsigned int rc;
	int i;

	spiflash_write_protect(0);

	for (i = 0; i < sizeof(pi); i++) {
		DEBUGPCR("0x%02x writing 0x%0x", addr+i, pi8[i]);
		spiflash_write_enable(1);
		rc = spiflash_otp_write(addr+i, pi8[i]);
		if (rc < 0)
			break;

		do { } while (spiflash_read_status() & 1);
	}

	spiflash_otp_set_lock(OTP_REGION_PRODINFO);

	spiflash_write_protect(1);

	return rc;
}

int prod_info_get(uint32_t *ver, uint32_t *reworks)
{
	struct simtrace_prod_info pi;
	uint32_t addr = OTP_ADDR(OTP_REGION_PRODINFO);

	spiflash_otp_read(addr, (uint8_t *) &pi, sizeof(pi));

	if (pi.magic != PRODINFO_MAGIC)
		return -1;

	if (ver)
		*ver = pi.version;

	if (reworks)
		*reworks = pi.reworks;

	return 0;
}
personal git repositories of Harald Welte. Your mileage may vary