sdb-storage.c 23.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * This work is part of the White Rabbit project
 *
 * Copyright (C) 2012, 2013 CERN (www.cern.ch)
 * Author: Grzegorz Daniluk <grzegorz.daniluk@cern.ch>
 * Author: Alessandro Rubini <rubini@gnudd.com>
 *
 * Released according to the GNU GPL, version 2 or any later version.
 */
10
#include <errno.h>
11 12
#include <wrc.h>
#include <w1.h>
13
#include <storage.h>
14

15 16
#include "types.h"
#include "i2c.h"
17
#include "onewire.h"
18
#include "endpoint.h"
19
#include "syscon.h"
20 21 22 23
#include <sdb.h>

#define SDBFS_BIG_ENDIAN
#include <libsdbfs.h>
24
#include <flash.h>
25
#include <fram.h>
26 27 28 29 30 31 32 33 34 35 36

/*
 * This source file is a drop-in replacement of the legacy one: it manages
 * both i2c and w1 devices even if the interface is the old i2c-based one
 */
#define SDB_VENDOR	0x46696c6544617461LL /* "FileData" */
#define SDB_DEV_INIT	0x77722d69 /* wr-i (nit) */
#define SDB_DEV_MAC	0x6d61632d /* mac- (address) */
#define SDB_DEV_SFP	0x7366702d /* sfp- (database) */
#define SDB_DEV_CALIB	0x63616c69 /* cali (bration) */

37 38 39 40
/* constants for scanning I2C EEPROMs */
#define EEPROM_START_ADR 0
#define EEPROM_STOP_ADR  127

41 42
struct storage_config storage_cfg;

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/* Functions for Flash access */
static int sdb_flash_read(struct sdbfs *fs, int offset, void *buf, int count)
{
	return flash_read(offset, buf, count);
}

static int sdb_flash_write(struct sdbfs *fs, int offset, void *buf, int count)
{
	return flash_write(offset, buf, count);
}

static int sdb_flash_erase(struct sdbfs *fs, int offset, int count)
{
	return flash_erase(offset, count);
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/* Functions for FRAM access */
static int sdb_fram_read(struct sdbfs *fs, int offset, void *buf, int count)
{
	return fram_read(offset, buf, count);
}

static int sdb_fram_write(struct sdbfs *fs, int offset, void *buf, int count)
{
	return fram_write(offset, buf, count);
}

static int sdb_fram_erase(struct sdbfs *fs, int offset, int count)
{
	return fram_erase(offset, count);
}

75 76 77 78 79 80 81 82 83 84 85
/* The methods for W1 access */
static int sdb_w1_read(struct sdbfs *fs, int offset, void *buf, int count)
{
	return w1_read_eeprom_bus(fs->drvdata, offset, buf, count);
}

static int sdb_w1_write(struct sdbfs *fs, int offset, void *buf, int count)
{
	return w1_write_eeprom_bus(fs->drvdata, offset, buf, count);
}

86 87 88 89 90
static int sdb_w1_erase(struct sdbfs *fs, int offset, int count)
{
	return w1_erase_eeprom_bus(fs->drvdata, offset, count);
}

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
/*
 * I2C code.
 * The functions in ./eeprom.c (legacy) are replicated here with the sdb
 * calling convention. So we miss the low-level and high-level layer splitting
 */
struct i2c_params {
	int ifnum;
	int addr;
};

static struct i2c_params i2c_params;

static int sdb_i2c_read(struct sdbfs *fs, int offset, void *buf, int count)
{
	int i;
	struct i2c_params *p = fs->drvdata;
	unsigned char *cb = buf;

	mi2c_start(p->ifnum);
	if (mi2c_put_byte(p->ifnum, p->addr << 1) < 0) {
		mi2c_stop(p->ifnum);
		return -1;
	}
	mi2c_put_byte(p->ifnum, (offset >> 8) & 0xff);
	mi2c_put_byte(p->ifnum, offset & 0xff);
	mi2c_repeat_start(p->ifnum);
	mi2c_put_byte(p->ifnum, (p->addr << 1) | 1);
	for (i = 0; i < count - 1; ++i) {
		mi2c_get_byte(p->ifnum, cb, 0);
		cb++;
	}
	mi2c_get_byte(p->ifnum, cb, 1);
	cb++;
	mi2c_stop(p->ifnum);

	return count;
}

static int sdb_i2c_write(struct sdbfs *fs, int offset, void *buf, int count)
{
	int i, busy;
	struct i2c_params *p = fs->drvdata;
	unsigned char *cb = buf;

	for (i = 0; i < count; i++) {
		mi2c_start(p->ifnum);
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		if (mi2c_put_byte(p->ifnum, p->addr << 1) < 0) {
			mi2c_stop(p->ifnum);
			return -1;
		}
		mi2c_put_byte(p->ifnum, (offset >> 8) & 0xff);
		mi2c_put_byte(p->ifnum, offset & 0xff);
		mi2c_put_byte(p->ifnum, *cb++);
		offset++;
		mi2c_stop(p->ifnum);

		do {		/* wait until the chip becomes ready */
			mi2c_start(p->ifnum);
			busy = mi2c_put_byte(p->ifnum, p->addr << 1);
			mi2c_stop(p->ifnum);
		} while (busy);

	}
	return count;
}
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
static int sdb_i2c_erase(struct sdbfs *fs, int offset, int count)
{
	int i, busy;
	struct i2c_params *p = fs->drvdata;

	for (i = 0; i < count; i++) {
		mi2c_start(p->ifnum);

		if (mi2c_put_byte(p->ifnum, p->addr << 1) < 0) {
			mi2c_stop(p->ifnum);
			return -1;
		}
		mi2c_put_byte(p->ifnum, (offset >> 8) & 0xff);
		mi2c_put_byte(p->ifnum, offset & 0xff);
		mi2c_put_byte(p->ifnum, 0xff);
		offset++;
		mi2c_stop(p->ifnum);

		do {		/* wait until the chip becomes ready */
			mi2c_start(p->ifnum);
			busy = mi2c_put_byte(p->ifnum, p->addr << 1);
			mi2c_stop(p->ifnum);
		} while (busy);

	}
	return count;
}

186 187 188
/*
 * A trivial dumper, just to show what's up in there
 */
189
static void storage_sdb_list(struct sdbfs *fs)
190 191 192
{
	struct sdb_device *d;
	int new = 1;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
193

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
194
	while ((d = sdbfs_scan(fs, new)) != NULL) {
195 196 197 198 199 200 201 202
		d->sdb_component.product.record_type = '\0';
		pp_printf("file 0x%08x @ %4i, name %s\n",
			  (int)(d->sdb_component.product.device_id),
			  (int)(d->sdb_component.addr_first),
			  (char *)(d->sdb_component.product.name));
		new = 0;
	}
}
203
/* The sdb filesystem itself, build-time initialized for i2c */
204
static struct sdbfs wrc_sdb = {
205
	.name = "eeprom",
206
	.blocksize = 1, /* Not currently used */
207 208 209
	.drvdata = &i2c_params,
	.read = sdb_i2c_read,
	.write = sdb_i2c_write,
210 211 212 213 214
};

uint8_t has_eeprom = 0; /* modified at init time */

/*
215
 * Init: sets "int has_eeprom" above
216 217 218
 *
 * This is called by wrc_main, after initializing both w1 and i2c
 */
219
void storage_init(int chosen_i2cif, int chosen_i2c_addr)
220 221
{
	uint32_t magic = 0;
222 223
	static unsigned entry_points_eeprom[] = {0, 64, 128, 256, 512, 1024};
	static unsigned entry_points_flash[] = {
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
224 225 226 227 228
				0x000000,	/* flash base */
				0x100,		/* second page in flash */
				0x200,		/* IPMI with MultiRecord */
				0x300,		/* IPMI with larger MultiRecord */
				0x170000,	/* after first FPGA bitstream */
229 230
				0x2e0000,	/* after MultiBoot bitstream */
				0x600000};	/* after SVEC AFPGA bitstream */
231 232 233 234 235
	uint32_t entry_points_fram[] = {
				0x000000,	/* fram base */
				0x6000,
				0x7000
        };
236 237
	int i, ret;

238 239 240 241 242
	/*
	 * 1. Check if there is SDBFS in the Flash.
	 */
	for (i = 0; i < ARRAY_SIZE(entry_points_flash); i++) {
		flash_read(entry_points_flash[i], (void *)&magic, sizeof(magic));
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
243
		if (magic == SDB_MAGIC)
244 245 246 247 248 249
			break;
	}
	if (magic == SDB_MAGIC) {
		pp_printf("sdbfs: found at %i in Flash\n",
				entry_points_flash[i]);
		wrc_sdb.drvdata = NULL;
250
		wrc_sdb.blocksize = storage_cfg.blocksize;
251 252 253 254 255 256 257 258
		wrc_sdb.entrypoint = entry_points_flash[i];
		wrc_sdb.read = sdb_flash_read;
		wrc_sdb.write = sdb_flash_write;
		wrc_sdb.erase = sdb_flash_erase;
		goto found_exit;
	}

	/*
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	 * 2. Check if there is SDBFS in the FRAM.
	 */
	for (i = 0; i < ARRAY_SIZE(entry_points_fram); i++) {
		fram_read(entry_points_fram[i], (void *)&magic, sizeof(magic));
		if (magic == SDB_MAGIC)
			break;
	}
	if (magic == SDB_MAGIC) {
		pp_printf("sdbfs: found at %i in Fram\n",
				entry_points_fram[i]);
		wrc_sdb.drvdata = NULL;
		wrc_sdb.blocksize = 0;
		wrc_sdb.entrypoint = entry_points_fram[i];
		wrc_sdb.read = sdb_fram_read;
		wrc_sdb.write = sdb_fram_write;
		wrc_sdb.erase = sdb_fram_erase;
		goto found_exit;
	}
	/*
	 * 3. Look for w1 first: if there is no eeprom it fails fast
279 280 281
	 */
	for (i = 0; i < ARRAY_SIZE(entry_points_eeprom); i++) {
		ret = w1_read_eeprom_bus(&wrpc_w1_bus, entry_points_eeprom[i],
282 283 284 285 286 287 288
					 (void *)&magic, sizeof(magic));
		if (ret != sizeof(magic))
			break;
		if (magic == SDB_MAGIC)
			break;
	}
	if (magic == SDB_MAGIC) {
289
		pp_printf("sdbfs: found at %i in W1\n", entry_points_eeprom[i]);
290
		/* override default i2c settings with w1 ones */
291
		wrc_sdb.drvdata = &wrpc_w1_bus;
292
		wrc_sdb.blocksize = 1;
293
		wrc_sdb.entrypoint = entry_points_eeprom[i];
294 295
		wrc_sdb.read = sdb_w1_read;
		wrc_sdb.write = sdb_w1_write;
296
		wrc_sdb.erase = sdb_w1_erase;
297
		goto found_exit;
298 299 300
	}

	/*
301
	 * 4. If w1 failed, look for i2c: start from low offsets.
302
	 */
303
	i2c_params.ifnum = chosen_i2cif;
304 305 306
	i2c_params.addr = EEPROM_START_ADR;
	while (i2c_params.addr <= EEPROM_STOP_ADR) {
		/* First, we check if I2C EEPROM is there */
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
307
		if (!mi2c_devprobe(i2c_params.ifnum, i2c_params.addr)) {
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
			i2c_params.addr++;
			continue;
		}
		/* While looking for the magic number, use sdb-based read function */
		for (i = 0; i < ARRAY_SIZE(entry_points_eeprom); i++) {
			sdb_i2c_read(&wrc_sdb, entry_points_eeprom[i], (void *)&magic,
				    sizeof(magic));
			if (magic == SDB_MAGIC)
				break;
		}
		if (magic == SDB_MAGIC) {
			pp_printf("sdbfs: found at %i in I2C(0x%2X)\n",
				entry_points_eeprom[i], i2c_params.addr);
			wrc_sdb.drvdata = &i2c_params;
			wrc_sdb.blocksize = 1;
			wrc_sdb.entrypoint = entry_points_eeprom[i];
			wrc_sdb.read = sdb_i2c_read;
			wrc_sdb.write = sdb_i2c_write;
			wrc_sdb.erase = sdb_i2c_erase;
			goto found_exit;
		}
		i2c_params.addr++;
330
	}
331 332

	if (i2c_params.addr == EEPROM_STOP_ADR) {
333
		pp_printf("No SDB filesystem in i2c eeprom\n");
334
		return;
335 336 337 338 339 340
	}

found_exit:
	/* found: register the filesystem */
	has_eeprom = 1;
	sdbfs_dev_create(&wrc_sdb);
341
	storage_sdb_list(&wrc_sdb);
342
	return;
343 344
}

345 346 347
/*
 * Reading/writing the MAC address used to be part of dev/onewire.c,
 * but is not onewire-specific.  What is w1-specific is the default
348
 * setting if no sdbfs is there, but CONFIG_SDB_STORAGE depends on
349 350
 * CONFIG_W1 anyways.
 */
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
351
int get_persistent_mac(uint8_t portnum, uint8_t *mac)
352
{
353
	int ret = 0;
354
	int i;
355
	struct w1_dev *d;
356

357 358 359 360 361 362
	if (IS_HOST_PROCESS) {
		/* we don't have sdb working, so get the real eth address */
		get_mac_addr(mac);
		return 0;
	}

363
	if (sdbfs_open_id(&wrc_sdb, SDB_VENDOR, SDB_DEV_MAC) < 0)
364 365 366 367 368 369
		ret =-1;
	else
        {
		ret = sdbfs_fread(&wrc_sdb, 0, mac, 6);
		sdbfs_close(&wrc_sdb);
        }
370

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
371
	if (ret < 0)
372 373 374 375 376 377 378 379
		pp_printf("%s: SDB error\n", __func__);
	if (mac[0] == 0xff ||
	    (mac[0] | mac[1] | mac[2] | mac[3] | mac[4] | mac[5]) == 0) {
		pp_printf("%s: SDB file is empty\n", __func__);
		ret = -1;
	}
	if (ret < 0) {
		pp_printf("%s: Using W1 serial number\n", __func__);
380
		w1_scan_bus(&wrpc_w1_bus);
381
		for (i = 0; i < W1_MAX_DEVICES; i++) {
382 383 384 385 386 387 388 389 390 391 392 393
			d = wrpc_w1_bus.devs + i;
			if (d->rom) {
				mac[0] = 0x22;
				mac[1] = 0x33;
				mac[2] = 0xff & (d->rom >> 32);
				mac[3] = 0xff & (d->rom >> 24);
				mac[4] = 0xff & (d->rom >> 16);
				mac[5] = 0xff & (d->rom >> 8);
				ret = 0;
				break;
			}
                }
394 395 396 397 398 399 400 401
	}
	if (ret < 0) {
		pp_printf("%s: failure\n", __func__);
		return -1;
	}
	return 0;
}

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
402
int set_persistent_mac(uint8_t portnum, uint8_t *mac)
403 404 405 406
{
	int ret;

	ret = sdbfs_open_id(&wrc_sdb, SDB_VENDOR, SDB_DEV_MAC);
407 408
	if (ret >= 0) {
		sdbfs_ferase(&wrc_sdb, 0, wrc_sdb.f_len);
409
		ret = sdbfs_fwrite(&wrc_sdb, 0, mac, 6);
410
	}
411 412 413 414 415 416 417 418
	sdbfs_close(&wrc_sdb);

	if (ret < 0) {
		pp_printf("%s: SDB error, can't save\n", __func__);
		return -1;
	}
	return 0;
}
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

/*
 * The SFP section is placed somewhere inside EEPROM (W1 or I2C), using sdbfs.
 *
 * Initially we have a count of SFP records
 *
 * For each sfp we have
 *
 * - part number (16 bytes)
 * - alpha (4 bytes)
 * - deltaTx (4 bytes)
 * - delta Rx (4 bytes)
 * - checksum (1 byte)  (low order 8 bits of the sum of all bytes)
 *
 * the total is 29 bytes for each sfp (ugly, but we are byte-oriented anyways
 */


437
/* Erase SFB database in the memory */
438
int32_t storage_sfpdb_erase(void)
439 440 441 442 443
{
	int ret;

	if (sdbfs_open_id(&wrc_sdb, SDB_VENDOR, SDB_DEV_SFP) < 0)
		return -1;
444
	ret = sdbfs_ferase(&wrc_sdb, 0, wrc_sdb.f_len);
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
445
	if (ret == wrc_sdb.f_len)
446
		ret = 1;
447 448 449 450
	sdbfs_close(&wrc_sdb);
	return ret == 1 ? 0 : -1;
}

451 452
/* Dummy check if sfp information is correct by verifying it doesn't have
 * 0xff bytes */
453
static int sfp_valid(struct s_sfpinfo *sfp)
454 455
{
	int i;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
456 457 458

	for (i = 0; i < SFP_PN_LEN; ++i) {
		if (sfp->pn[i] == 0xff)
459 460 461 462 463
			return 0;
	}
	return 1;
}

464
static int sfp_entry(struct s_sfpinfo *sfp, uint8_t oper, uint8_t pos)
465 466
{
	static uint8_t sfpcount = 0;
467
	struct s_sfpinfo tempsfp;
468 469 470
	int ret = -1;
	uint8_t i, chksum = 0;
	uint8_t *ptr;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
471
	int sdb_offset;
472 473

	if (pos >= SFPS_MAX)
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
474
		return EE_RET_POSERR;	/* position outside the range */
475 476 477 478

	if (sdbfs_open_id(&wrc_sdb, SDB_VENDOR, SDB_DEV_SFP) < 0)
		return -1;

479 480
	/* Read how many SFPs are in the database, but only in the first
	 * call */
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
481
	if (!pos) {
482
		sfpcount = 0;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
483 484 485 486
		sdb_offset = sizeof(sfpcount);
		while (sdbfs_fread(&wrc_sdb, sdb_offset, &tempsfp,
					sizeof(tempsfp)) == sizeof(tempsfp)) {
			if (!sfp_valid(&tempsfp))
487 488
				break;
			sfpcount++;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
489
			sdb_offset = sizeof(sfpcount) + sfpcount * sizeof(tempsfp);
490 491
		}
	}
492

493 494
	if ((oper == SFP_ADD) && (sfpcount == SFPS_MAX)) {
		/* no more space to add new SFPs */
495 496 497 498
		ret = EE_RET_DBFULL;
		goto out;
	}

499 500
	if (!pos && (oper == SFP_GET) && sfpcount == 0) {
		/* no SFPs in the database */
501 502 503
		ret = 0;
		goto out;
	}
504

505
	if (oper == SFP_GET) {
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
506 507 508
		sdb_offset = sizeof(sfpcount) + pos * sizeof(*sfp);
		if (sdbfs_fread(&wrc_sdb, sdb_offset, sfp, sizeof(*sfp))
				!= sizeof(*sfp))
509 510 511 512 513 514 515 516 517 518
			goto out;

		ptr = (uint8_t *)sfp;
		/* read sizeof() - 1 because we don't include checksum */
		for (i = 0; i < sizeof(struct s_sfpinfo) - 1; ++i)
			chksum = chksum + *(ptr++);
		if (chksum != sfp->chksum) {
			pp_printf("sfp: corrupted checksum\n");
			goto out;
		}
519 520
	}
	if (oper == SFP_ADD) {
521
		/* count checksum */
522 523 524 525 526 527
		ptr = (uint8_t *)sfp;
		/* use sizeof() - 1 because we don't include checksum */
		for (i = 0; i < sizeof(struct s_sfpinfo) - 1; ++i)
			chksum = chksum + *(ptr++);
		sfp->chksum = chksum;
		/* add SFP at the end of DB */
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
528 529 530
		sdb_offset = sizeof(sfpcount) + sfpcount * sizeof(*sfp);
		if (sdbfs_fwrite(&wrc_sdb, sdb_offset, sfp, sizeof(*sfp))
				!= sizeof(*sfp)) {
531
			goto out;
532
		}
533 534 535 536 537 538 539 540
		sfpcount++;
	}
	ret = sfpcount;
out:
	sdbfs_close(&wrc_sdb);
	return ret;
}

541 542 543 544 545 546 547 548 549 550 551
static int storage_update_sfp(struct s_sfpinfo *sfp)
{
	int sfpcount = 1;
	int temp;
	int8_t i;
	struct s_sfpinfo sfp_db[SFPS_MAX];
	struct s_sfpinfo *dbsfp;

	/* copy entries from flash to the memory, update entry if matched */
	for (i = 0; i < sfpcount; ++i) {
		dbsfp = &sfp_db[i];
552
		sfpcount = sfp_entry(dbsfp, SFP_GET, i);
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
		if (sfpcount <= 0)
			return sfpcount;
		if (!strncmp(dbsfp->pn, sfp->pn, 16)) {
			/* update matched entry */
			dbsfp->dTx = sfp->dTx;
			dbsfp->dRx = sfp->dRx;
			dbsfp->alpha = sfp->alpha;
		}
	}

	/* erase entire database */
	if (storage_sfpdb_erase() == EE_RET_I2CERR) {
			pp_printf("Could not erase DB\n");
			return -1;
		}

	/* add all SFPs */
	for (i = 0; i < sfpcount; ++i) {
		dbsfp = &sfp_db[i];
572
		temp = sfp_entry(dbsfp, SFP_ADD, 0);
573 574 575 576 577 578 579 580
		if (temp < 0) {
			/* if error, return it */
			return temp;
		}
	}
	return i;
}

581
int storage_get_sfp(struct s_sfpinfo *sfp, uint8_t oper, uint8_t pos)
582 583 584
{
	struct s_sfpinfo tmp_sfp;

585
	if (oper == SFP_GET) {
586
		/* Get SFP entry */
587
		return sfp_entry(sfp, SFP_GET, pos);
588 589 590 591 592 593 594
	}

	/* storage_match_sfp replaces content of parameter, so do the copy
	 * first */
	tmp_sfp = *sfp;
	if (!storage_match_sfp(&tmp_sfp)) { /* add a new sfp entry */
		pp_printf("Adding new SFP entry\n");
595
		return sfp_entry(sfp, SFP_ADD, 0);
596 597 598 599 600 601
	}

	pp_printf("Update existing SFP entry\n");
	return storage_update_sfp(sfp);
}

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
602
int storage_match_sfp(struct s_sfpinfo *sfp)
603 604
{
	uint8_t sfpcount = 1;
605
	int8_t i;
606 607 608
	struct s_sfpinfo dbsfp;

	for (i = 0; i < sfpcount; ++i) {
609
		sfpcount = sfp_entry(&dbsfp, SFP_GET, i);
610 611
		if (sfpcount <= 0)
			return sfpcount;
612 613 614 615 616 617 618 619 620 621 622 623 624 625
		if (!strncmp(dbsfp.pn, sfp->pn, 16)) {
			sfp->dTx = dbsfp.dTx;
			sfp->dRx = dbsfp.dRx;
			sfp->alpha = dbsfp.alpha;
			return 1;
		}
	}

	return 0;
}

/*
 * Phase transition ("calibration" file)
 */
626
#define VALIDITY_BIT 0x80000000
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
627
int storage_phtrans(uint32_t *valp, uint8_t write)
628 629
{
	int ret = -1;
630
	uint32_t value;
631 632 633 634

	if (sdbfs_open_id(&wrc_sdb, SDB_VENDOR, SDB_DEV_CALIB) < 0)
		return -1;
	if (write) {
635
		sdbfs_ferase(&wrc_sdb, 0, wrc_sdb.f_len);
636 637 638
		value = *valp | VALIDITY_BIT;
		if (sdbfs_fwrite(&wrc_sdb, 0, &value, sizeof(value))
		    != sizeof(value))
639 640 641
			goto out;
		ret = 1;
	} else {
642 643
		if (sdbfs_fread(&wrc_sdb, 0, &value, sizeof(value))
		    != sizeof(value))
644
			goto out;
645 646
		*valp = value & ~VALIDITY_BIT;
		ret = (value & VALIDITY_BIT) != 0;
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
	}
out:
	sdbfs_close(&wrc_sdb);
	return ret;
}

/*
 * 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'.....      |
 * |                                              |
 * |                                              |
 * ------------------------------------------------
 */

666
int storage_init_erase(void)
667 668 669 670 671
{
	int ret;

	if (sdbfs_open_id(&wrc_sdb, SDB_VENDOR, SDB_DEV_INIT) < 0)
		return -1;
672
	ret = sdbfs_ferase(&wrc_sdb, 0, wrc_sdb.f_len);
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
673
	if (ret == wrc_sdb.f_len)
674
		ret = 1;
675
	sdbfs_close(&wrc_sdb);
676
	return ret == 1 ? 0 : -1;
677 678 679 680 681
}

/*
 * Appends a new shell command at the end of boot script
 */
682
int storage_init_add(const char *args[])
683
{
684
	int len, i;
685 686 687
	uint8_t separator = ' ';
	uint16_t used, readback;
	int ret = -1;
688
	uint8_t byte;
689 690 691

	if (sdbfs_open_id(&wrc_sdb, SDB_VENDOR, SDB_DEV_INIT) < 0)
		return -1;
692 693 694 695 696 697

	/* check how many bytes we already have there */
	used = 0;
	while (sdbfs_fread(&wrc_sdb, sizeof(used)+used, &byte, 1) == 1) {
		if (byte == 0xff)
			break;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
698
		used++;
699 700
	}

701 702 703
	if (used > 256 /* 0xffff or wrong */)
		used = 0;

704
	i = 1; /* args[0] is "add" */
705 706 707 708 709 710
	while (args[i] != NULL) {
		len = strlen(args[i]);
		if (sdbfs_fwrite(&wrc_sdb, sizeof(used) + used,
				 (void *)args[i], len) != len)
			goto out;
		used += len;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
711
		if (args[i+1] != NULL)	/* next one is another word of the same command */
712 713 714
			separator = ' ';
		else			/* no more words, end command with '\n' */
			separator = '\n';
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
		if (sdbfs_fwrite(&wrc_sdb, sizeof(used) + used,
				&separator, sizeof(separator))
		    != sizeof(separator))
			goto out;
		++used;
		++i;
	}
	/* and finally update the size of the script */
	if (sdbfs_fwrite(&wrc_sdb, 0, &used, sizeof(used)) != sizeof(used))
		goto out;

	if (sdbfs_fread(&wrc_sdb, 0, &readback, sizeof(readback))
	    != sizeof(readback))
		goto out;

	ret = 0;
out:
	sdbfs_close(&wrc_sdb);
	return ret;
}

736
int storage_init_show(void)
737
{
738
	int ret = -1;
739 740 741 742 743
	uint16_t used;
	uint8_t byte;

	if (sdbfs_open_id(&wrc_sdb, SDB_VENDOR, SDB_DEV_INIT) < 0)
		return -1;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
744

745
	pp_printf("-- user-defined script --\n");
746 747 748
	used = 0;
	do {
		if (sdbfs_fread(&wrc_sdb, sizeof(used) + used, &byte, 1) != 1)
749
			goto out;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
750
		if (byte != 0xff) {
751 752 753
			pp_printf("%c", byte);
			used++;
		}
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
754
	} while (byte != 0xff);
755

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
756
	if (used == 0)
757
		pp_printf("(empty)\n");
758 759 760 761 762 763
	ret = 0;
out:
	sdbfs_close(&wrc_sdb);
	return ret;
}

764
int storage_init_readcmd(uint8_t *buf, uint8_t bufsize, uint8_t next)
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
{
	int i = 0, ret = -1;
	uint16_t used;
	static uint16_t ptr;

	if (sdbfs_open_id(&wrc_sdb, SDB_VENDOR, SDB_DEV_INIT) < 0)
		return -1;

	if (next == 0)
		ptr = sizeof(used);
	do {
		if (ptr - sizeof(used) > bufsize)
			goto out;
		if (sdbfs_fread(&wrc_sdb, (ptr++),
				&buf[i], sizeof(char)) != sizeof(char))
			goto out;
781 782
		if (buf[i] == 0xff)
			break;
783 784 785 786 787 788
	} while (buf[i++] != '\n');
	ret = i;
out:
	sdbfs_close(&wrc_sdb);
	return ret;
}
789

790 791
int storage_read_hdl_cfg(void)
{
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
792 793
	get_storage_info(&storage_cfg.memtype, &storage_cfg.baseadr,
			&storage_cfg.blocksize);
794 795 796 797
	if (storage_cfg.memtype == MEM_FLASH && storage_cfg.blocksize == 0) {
		storage_cfg.valid = 0;
		/* keep default blocksize for backwards compatibility */
		storage_cfg.blocksize = FLASH_BLOCKSIZE;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
798
	} else
799 800 801 802
		storage_cfg.valid = 1;
	return 0;
}

803 804 805 806 807
extern uint32_t _binary_tools_sdbfs_default_bin_start[];
extern uint32_t _binary_tools_sdbfs_default_bin_end[];

static inline unsigned long SDB_ALIGN(unsigned long x, int blocksize)
{
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
808
	return (x + (blocksize - 1)) & ~(blocksize - 1);
809 810
}

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
811 812
int storage_sdbfs_erase(int mem_type, uint32_t base_adr, uint32_t blocksize,
		uint8_t i2c_adr)
813
{
814
	if (!HAS_GENSDBFS || (mem_type == MEM_FLASH && blocksize == 0))
815 816
		return -EINVAL;

817 818
	if (mem_type == MEM_FLASH) {
		pp_printf("Erasing Flash(0x%x)...\n", base_adr);
819
		sdb_flash_erase(NULL, base_adr, SDBFS_REC * blocksize);
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
820
	} else if (mem_type == MEM_EEPROM) {
821 822 823 824
		pp_printf("Erasing EEPROM %d (0x%x)...\n", i2c_adr, base_adr);
		i2c_params.ifnum = WRPC_FMC_I2C;
		i2c_params.addr  = i2c_adr;
		wrc_sdb.drvdata = &i2c_params;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
825 826 827
		sdb_i2c_erase(&wrc_sdb, base_adr, SDBFS_REC *
				sizeof(struct sdb_device));
	} else if (mem_type == MEM_1W_EEPROM) {
828 829
		pp_printf("Erasing 1-W EEPROM (0x%x)...\n", base_adr);
		wrc_sdb.drvdata = &wrpc_w1_bus;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
830 831
		sdb_w1_erase(&wrc_sdb, base_adr, SDBFS_REC *
				sizeof(struct sdb_device));
832 833 834 835
	} else 	if (mem_type == MEM_FRAM) {
		pp_printf("Erasing FRAM (0x%x)...\n", base_adr);
		sdb_fram_erase(NULL, base_adr, SDBFS_REC * blocksize);
        }
836 837 838
	return 0;
}

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
839 840
int storage_gensdbfs(int mem_type, uint32_t base_adr, uint32_t blocksize,
		uint8_t i2c_adr)
841
{
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
842 843 844 845 846
	struct sdb_device *sdbfs =
		(struct sdb_device *) _binary_tools_sdbfs_default_bin_start;
	struct sdb_interconnect *sdbfs_dir = (struct sdb_interconnect *)
		_binary_tools_sdbfs_default_bin_start;
	/* struct sdb_device sdbfs_buf[SDBFS_REC]; */
847 848 849 850 851
	int i;
	char buf[19] = {0};
	int cur_adr, size;
	uint32_t val;

852
	if (!HAS_GENSDBFS || (mem_type == MEM_FLASH && base_adr == 0))
853 854
		return -EINVAL;

855 856
	if (mem_type == MEM_FLASH && blocksize == 0)
		return -EINVAL;
857 858

	/* first file starts after the SDBFS description */
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
859 860
	cur_adr = base_adr + SDB_ALIGN(SDBFS_REC*sizeof(struct sdb_device),
			blocksize);
861
	/* scan through files */
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
862 863 864 865
	for (i = 1; i < SDBFS_REC; ++i) {
		/* relocate each file depending on base address and block size*/
		size = sdbfs[i].sdb_component.addr_last -
			sdbfs[i].sdb_component.addr_first;
866 867 868 869 870 871
		sdbfs[i].sdb_component.addr_first = cur_adr;
		sdbfs[i].sdb_component.addr_last  = cur_adr + size;
		cur_adr = SDB_ALIGN(cur_adr + (size + 1), blocksize);
	}
	/* update the directory */
	sdbfs_dir->sdb_component.addr_first = base_adr;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
872 873
	sdbfs_dir->sdb_component.addr_last  =
		sdbfs[SDBFS_REC-1].sdb_component.addr_last;
874

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
875
	for (i = 0; i < SDBFS_REC; ++i) {
876 877 878 879 880 881 882 883 884 885 886
		strncpy(buf, (char *)sdbfs[i].sdb_component.product.name, 18);
		pp_printf("filename: %s; first: %x; last: %x\n", buf,
				(int)sdbfs[i].sdb_component.addr_first,
				(int)sdbfs[i].sdb_component.addr_last);
	}

	size = sizeof(struct sdb_device);
	if (mem_type == MEM_FLASH) {
		pp_printf("Formatting SDBFS in Flash(0x%x)...\n", base_adr);
		/* each file is in a separate block, therefore erase SDBFS_REC
		 * number of blocks */
887
		sdb_flash_erase(NULL, base_adr, SDBFS_REC * blocksize);
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
888 889 890
		for (i = 0; i < SDBFS_REC; ++i) {
			sdb_flash_write(NULL, base_adr + i*size, &sdbfs[i],
					size);
891
		}
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
892 893 894 895 896 897 898 899 900 901 902
		/*
		pp_printf("Verification...");
		sdb_flash_read(NULL, base_adr, sdbfs_buf, SDBFS_REC *
				sizeof(struct sdb_device));
		if(memcmp(sdbfs, sdbfs_buf, SDBFS_REC *
				sizeof(struct sdb_device)))
			pp_printf("Error.\n");
		else
			pp_printf("OK.\n");
		*/
	} else if (mem_type == MEM_EEPROM) {
903
		/* First, check if EEPROM is really there */
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
904
		if (!mi2c_devprobe(WRPC_FMC_I2C, i2c_adr)) {
905 906 907 908 909
			pp_printf("I2C EEPROM not found\n");
			return -EINVAL;
		}
		i2c_params.ifnum = WRPC_FMC_I2C;
		i2c_params.addr  = i2c_adr;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
910 911
		pp_printf("Formatting SDBFS in I2C EEPROM %d (0x%x)...\n",
				i2c_params.addr, base_adr);
912 913
		wrc_sdb.drvdata = &i2c_params;
		sdb_i2c_erase(&wrc_sdb, base_adr, SDBFS_REC * size);
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
914 915 916
		for (i = 0; i < SDBFS_REC; ++i) {
			sdb_i2c_write(&wrc_sdb, base_adr + i*size, &sdbfs[i],
					size);
917
		}
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
918 919 920 921 922 923 924 925 926 927 928
		/*
		pp_printf("Verification...");
		sdb_i2c_read(&wrc_sdb, base_adr, sdbfs_buf, SDBFS_REC *
				sizeof(struct sdb_device));
		if(memcmp(sdbfs, sdbfs_buf, SDBFS_REC *
				sizeof(struct sdb_device)))
			pp_printf("Error.\n");
		else
			pp_printf("OK.\n");
		*/
	} else if (mem_type == MEM_1W_EEPROM) {
929
		wrc_sdb.drvdata = &wrpc_w1_bus;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
930 931
		if (sdb_w1_read(&wrc_sdb, 0, &val, sizeof(val)) !=
				sizeof(val)) {
932 933 934
			pp_printf("1-Wire EEPROM not found\n");
			return -EINVAL;
		}
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
935 936
		pp_printf("Formatting SDBFS in 1-W EEPROM (0x%x)...\n",
				base_adr);
937
		sdb_w1_erase(&wrc_sdb, base_adr, SDBFS_REC * size);
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
938 939 940
		for (i = 0; i < SDBFS_REC; ++i) {
			sdb_w1_write(&wrc_sdb, base_adr + i*size, &sdbfs[i],
					size);
941
		}
942 943 944 945 946 947 948 949
	} else if (mem_type == MEM_FRAM) {
		pp_printf("Formatting SDBFS in FRAM(0x%x)...\n", base_adr);
		sdb_fram_erase(NULL, base_adr, SDBFS_REC * size);
		for (i = 0; i < SDBFS_REC; ++i) {
			sdb_fram_write(NULL, base_adr + i*size, &sdbfs[i],
					size);
		}
        }
950 951 952
	/* re-initialize storage after writing sdbfs image */
	storage_init(WRPC_FMC_I2C, FMC_EEPROM_ADR);

953 954
	return mem_type;
}