eeprom.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * This work is part of the White Rabbit project
 *
 * Copyright (C) 2012 CERN (www.cern.ch)
 * Author: Grzegorz Daniluk <grzegorz.daniluk@cern.ch>
 *
 * Released according to the GNU GPL, version 2 or any later version.
 */
9 10 11
#include <string.h>
#include <wrc.h>

12 13 14 15 16 17 18
#include "types.h"
#include "i2c.h"
#include "eeprom.h"
#include "board.h"
#include "syscon.h"

/*
19 20
 * The SFP section is placed somewhere inside FMC EEPROM and it really does not
 * matter where (can be a binary data inside the Board Info section but can be
21 22 23
 * placed also outside the FMC standardized EEPROM structure. The only requirement
 * is that it starts with 0xdeadbeef pattern. The structure of SFP section is:
 *
24 25
 * ----------------------------------------------
 * | cal_ph_trans (4B) | SFP count (1B) |
26 27 28 29 30 31 32 33 34
 * --------------------------------------------------------------------------------------------
 * |   SFP(1) part number (16B)       | alpha (4B) | deltaTx (4B) | deltaRx (4B) | chksum(1B) |
 * --------------------------------------------------------------------------------------------
 * |   SFP(2) part number (16B)       | alpha (4B) | deltaTx (4B) | deltaRx (4B) | chksum(1B) |
 * --------------------------------------------------------------------------------------------
 * | (....)                           | (....)     | (....)       | (....)       | (...)      |
 * --------------------------------------------------------------------------------------------
 * |   SFP(count) part number (16B)   | alpha (4B) | deltaTx (4B) | deltaRx (4B) | chksum(1B) |
 * --------------------------------------------------------------------------------------------
35 36
 *
 * Fields description:
37
 * cal_ph_trans       - t2/t4 phase transition value (got from measure_t24p() ), contains
38
 *                      _valid_ bit (MSB) and 31 bits of cal_phase_transition value
39
 * count              - how many SFPs are described in the list (binary)
40
 * SFP(n) part number - SFP PN as read from SFP's EEPROM (e.g. AXGE-1254-0531)
41
 *                      (16 ascii chars)
42
 * checksum           - low order 8 bits of the sum of all bytes for the SFP(PN,alpha,dTx,dRx)
43 44 45
 *
 */

46 47 48 49 50 51 52 53 54 55 56 57 58
/*
 * The init script area consist of 2-byte size field and a set of shell commands
 * separated with '\n' character.
 *
 * -------------------
 * | bytes used (2B) |
 * ------------------------------------------------
 * | shell commands separated with '\n'.....      |
 * |                                              |
 * |                                              |
 * ------------------------------------------------
 */

59 60 61 62
uint8_t has_eeprom = 0;

uint8_t eeprom_present(uint8_t i2cif, uint8_t i2c_addr)
{
63
	has_eeprom = 1;
64 65
	if (!mi2c_devprobe(i2cif, i2c_addr))
		if (!mi2c_devprobe(i2cif, i2c_addr))
66 67 68 69
			has_eeprom = 0;

	return 0;
}
70

71 72
static int eeprom_read(uint8_t i2cif, uint8_t i2c_addr, uint32_t offset,
		       uint8_t * buf, size_t size)
73 74 75 76
{
	int i;
	unsigned char c;

77
	if (!has_eeprom)
78 79
		return -1;

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	mi2c_start(i2cif);
	if (mi2c_put_byte(i2cif, i2c_addr << 1) < 0) {
		mi2c_stop(i2cif);
		return -1;
	}
	mi2c_put_byte(i2cif, (offset >> 8) & 0xff);
	mi2c_put_byte(i2cif, offset & 0xff);
	mi2c_repeat_start(i2cif);
	mi2c_put_byte(i2cif, (i2c_addr << 1) | 1);
	for (i = 0; i < size - 1; ++i) {
		mi2c_get_byte(i2cif, &c, 0);
		*buf++ = c;
	}
	mi2c_get_byte(i2cif, &c, 1);
	*buf++ = c;
	mi2c_stop(i2cif);

	return size;
98 99
}

100
static int eeprom_write(uint8_t i2cif, uint8_t i2c_addr, uint32_t offset,
101
		 uint8_t * buf, size_t size)
102 103 104
{
	int i, busy;

105
	if (!has_eeprom)
106 107
		return -1;

108 109
	for (i = 0; i < size; i++) {
		mi2c_start(i2cif);
110

111 112 113 114
		if (mi2c_put_byte(i2cif, i2c_addr << 1) < 0) {
			mi2c_stop(i2cif);
			return -1;
		}
115 116 117 118 119 120
		mi2c_put_byte(i2cif, (offset >> 8) & 0xff);
		mi2c_put_byte(i2cif, offset & 0xff);
		mi2c_put_byte(i2cif, *buf++);
		offset++;
		mi2c_stop(i2cif);

121 122
		do {		/* wait until the chip becomes ready */
			mi2c_start(i2cif);
123 124
			busy = mi2c_put_byte(i2cif, i2c_addr << 1);
			mi2c_stop(i2cif);
125
		} while (busy);
126 127

	}
128
	return size;
129
}
130

131 132
int32_t eeprom_sfpdb_erase(uint8_t i2cif, uint8_t i2c_addr)
{
133
	uint8_t sfpcount = 0;
134

135 136 137 138 139 140
	//just a dummy function that writes '0' to sfp count field of the SFP DB
	if (eeprom_write(i2cif, i2c_addr, EE_BASE_SFP, &sfpcount,
			 sizeof(sfpcount)) != sizeof(sfpcount))
		return EE_RET_I2CERR;
	else
		return sfpcount;
141 142
}

143 144
int32_t eeprom_get_sfp(uint8_t i2cif, uint8_t i2c_addr, struct s_sfpinfo * sfp,
		       uint8_t add, uint8_t pos)
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
	static uint8_t sfpcount = 0;
	uint8_t i, chksum = 0;
	uint8_t *ptr;

	if (pos >= SFPS_MAX)
		return EE_RET_POSERR;	//position in database outside the range

	//read how many SFPs are in the database, but only in the first call (pos==0)
	if (!pos
	    && eeprom_read(i2cif, i2c_addr, EE_BASE_SFP, &sfpcount,
			   sizeof(sfpcount)) != sizeof(sfpcount))
		return EE_RET_I2CERR;

	if (add && sfpcount == SFPS_MAX)	//no more space in the database to add new SFPs
		return EE_RET_DBFULL;
	else if (!pos && !add && sfpcount == 0)	//there are no SFPs in the database to read
		return sfpcount;

	if (!add) {
		if (eeprom_read(i2cif, i2c_addr,
				EE_BASE_SFP + sizeof(sfpcount)
				+ pos * sizeof(struct s_sfpinfo),
				(uint8_t*)sfp, sizeof(struct s_sfpinfo))
		    != sizeof(struct s_sfpinfo) )
			return EE_RET_I2CERR;

		ptr = (uint8_t *) sfp;
		for (i = 0; i < sizeof(struct s_sfpinfo) - 1; ++i)	//'-1' because we do not include chksum in computation
			chksum =
			    (uint8_t) ((uint16_t) chksum + *(ptr++)) & 0xff;
		if (chksum != sfp->chksum)
177
			return EE_RET_CORRPT;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	} else {
		/*count checksum */
		ptr = (uint8_t *) sfp;
		for (i = 0; i < sizeof(struct s_sfpinfo) - 1; ++i)	//'-1' because we do not include chksum in computation
			chksum =
			    (uint8_t) ((uint16_t) chksum + *(ptr++)) & 0xff;
		sfp->chksum = chksum;
		/*add SFP at the end of DB */
		eeprom_write(i2cif, i2c_addr,
			     EE_BASE_SFP + sizeof(sfpcount)
			     + sfpcount * sizeof(struct s_sfpinfo),
			     (uint8_t *) sfp, sizeof(struct s_sfpinfo));
		sfpcount++;
		eeprom_write(i2cif, i2c_addr, EE_BASE_SFP, &sfpcount,
			     sizeof(sfpcount));
	}

	return sfpcount;
196 197
}

198
int8_t eeprom_match_sfp(uint8_t i2cif, uint8_t i2c_addr, struct s_sfpinfo * sfp)
199
{
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
	uint8_t sfpcount = 1;
	int8_t i, temp;
	struct s_sfpinfo dbsfp;

	for (i = 0; i < sfpcount; ++i) {
		temp = eeprom_get_sfp(WRPC_FMC_I2C, FMC_EEPROM_ADR,
				      &dbsfp, 0, i);
		if (!i) {
			sfpcount = temp;	//only in first round valid sfpcount is returned from eeprom_get_sfp
			if (sfpcount == 0 || sfpcount == 0xFF)
				return 0;
			else if (sfpcount < 0)
				return sfpcount;
		}
		if (!strncmp(dbsfp.pn, sfp->pn, 16)) {
			sfp->dTx = dbsfp.dTx;
			sfp->dRx = dbsfp.dRx;
			sfp->alpha = dbsfp.alpha;
			return 1;
		}
	}

	return 0;
223
}
224

225 226
int8_t eeprom_phtrans(uint8_t i2cif, uint8_t i2c_addr, uint32_t * val,
		      uint8_t write)
227
{
228
	int8_t ret;
229 230 231
	if (write) {
		*val |= (1 << 31);
		if (eeprom_write(i2cif, i2c_addr, EE_BASE_CAL, (uint8_t *) val,
232
		     sizeof(*val)) != sizeof(*val))
233
			ret = EE_RET_I2CERR;
234
		else
235 236 237 238
			ret = 1;

		*val &= 0x7fffffff;	//return ph_trans value without validity bit
		return ret;
239 240
	} else {
		if (eeprom_read(i2cif, i2c_addr, EE_BASE_CAL, (uint8_t *) val,
241
		     sizeof(*val)) != sizeof(*val))
242 243 244 245 246 247 248 249
			return EE_RET_I2CERR;

		if (!(*val & (1 << 31)))
			return 0;

		*val &= 0x7fffffff;	//return ph_trans value without validity bit
		return 1;
	}
250 251
}

252 253
int8_t eeprom_init_erase(uint8_t i2cif, uint8_t i2c_addr)
{
254
	uint16_t used = 0;
255

256 257 258 259 260
	if (eeprom_write(i2cif, i2c_addr, EE_BASE_INIT, (uint8_t *) & used,
	     sizeof(used)) != sizeof(used))
		return EE_RET_I2CERR;
	else
		return used;
261 262
}

263
/*
264 265 266 267
 * Appends a new shell command at the end of boot script
 */
int8_t eeprom_init_add(uint8_t i2cif, uint8_t i2c_addr, const char *args[])
{
268
	uint8_t i = 1;
269
	uint8_t separator = ' ';
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	uint16_t used, readback;

	if (eeprom_read(i2cif, i2c_addr, EE_BASE_INIT, (uint8_t *) & used,
	     sizeof(used)) != sizeof(used))
		return EE_RET_I2CERR;

	if (used == 0xffff)
		used = 0;	//this means the memory is blank

	while (args[i] != '\0') {
		if (eeprom_write(i2cif, i2c_addr, EE_BASE_INIT + sizeof(used)
				 + used, (uint8_t *) args[i], strlen(args[i]))
		    != strlen(args[i]))
			return EE_RET_I2CERR;
		used += strlen(args[i]);
		if (eeprom_write(i2cif, i2c_addr, EE_BASE_INIT + sizeof(used)
				 + used, &separator, sizeof(separator))
		    != sizeof(separator))
			return EE_RET_I2CERR;
		++used;
		++i;
	}
	//the end of the command, replace last separator with '\n'
	separator = '\n';
	if (eeprom_write(i2cif, i2c_addr, EE_BASE_INIT + sizeof(used) + used-1,
	     &separator, sizeof(separator)) != sizeof(separator))
		return EE_RET_I2CERR;
	//and finally update the size of the script
	if (eeprom_write(i2cif, i2c_addr, EE_BASE_INIT, (uint8_t *) & used,
	     sizeof(used)) != sizeof(used))
		return EE_RET_I2CERR;

	if (eeprom_read(i2cif, i2c_addr, EE_BASE_INIT, (uint8_t *) & readback,
	     sizeof(readback)) != sizeof(readback))
		return EE_RET_I2CERR;

	return 0;
307 308 309 310
}

int32_t eeprom_init_show(uint8_t i2cif, uint8_t i2c_addr)
{
311
	uint16_t used, i;
312
	uint8_t byte;
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

	if (eeprom_read(i2cif, i2c_addr, EE_BASE_INIT, (uint8_t *) & used,
	     sizeof(used)) != sizeof(used))
		return EE_RET_I2CERR;

	if (used == 0 || used == 0xffff) {
		used = 0;	//this means the memory is blank
		mprintf("Empty init script...\n");
	}
	//just read and print to the screen char after char
	for (i = 0; i < used; ++i) {
		if (eeprom_read(i2cif, i2c_addr, EE_BASE_INIT + sizeof(used)
				+ i, &byte, sizeof(byte)) != sizeof(byte))
			return EE_RET_I2CERR;
		mprintf("%c", byte);
	}

	return 0;
331 332
}

333
int8_t eeprom_init_readcmd(uint8_t i2cif, uint8_t i2c_addr, uint8_t *buf,
334
			   uint8_t bufsize, uint8_t next)
335
{
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	static uint16_t ptr;
	static uint16_t used = 0;
	uint8_t i = 0;

	if (next == 0) {
		if (eeprom_read(i2cif, i2c_addr, EE_BASE_INIT,
				(uint8_t *) & used, sizeof(used))
		    != sizeof(used))
			return EE_RET_I2CERR;
		ptr = sizeof(used);
	}

	if (ptr - sizeof(used) >= used)
		return 0;

	do {
		if (ptr - sizeof(used) > bufsize)
			return EE_RET_CORRPT;
		if (eeprom_read(i2cif, i2c_addr, EE_BASE_INIT + (ptr++),
				&buf[i], sizeof(char)) != sizeof(char))
			return EE_RET_I2CERR;
	} while (buf[i++] != '\n');

	return i;
360
}
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

#ifdef CONFIG_W1
#include <w1.h>
/*
 * The "persistent mac" thing was part of onewire.c, and it's not something
 * I can understand, I admit.
 *
 * Now, cards with w1 eeprom already use sdb-eeprom.c as far as I
 * know, while this eeprom.c file is still selected for devices that
 * have i2c eeprom and never saved a persistent mac address. But maybe
 * they prefer w1 for temperature (well, sockitowm will go)
 */
int8_t set_persistent_mac(uint8_t portnum, uint8_t * mac)
{
	pp_printf("Can't save persistent MAC address\n");
	return -1;
}

int32_t get_persistent_mac(uint8_t portnum, uint8_t * mac)
{
	int i, class;
	uint64_t rom;

	pp_printf("%s: Using W1 serial number\n", __func__);
	for (i = 0; i < W1_MAX_DEVICES; i++) {
		class = w1_class(wrpc_w1_bus.devs + i);
		if (class != 0x28 && class != 0x42)
			continue;
		rom = wrpc_w1_bus.devs[i].rom;
		mac[3] = rom >> 24;
		mac[4] = rom >> 16;
		mac[5] = rom >> 8;
	}
	return 0;
}

#endif /* CONFIG_W1 */