summaryrefslogtreecommitdiffstats
path: root/common/ddr_spd.c
blob: ea0b529eed86cd5363a3efbe9c3a75c8f2311c38 (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
/*
 * Copyright 2008 Freescale Semiconductor, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * Version 2 as published by the Free Software Foundation.
 */

#include <common.h>
#include <crc.h>
#include <ddr_spd.h>

uint32_t ddr2_spd_checksum_pass(const struct ddr2_spd_eeprom_s *spd)
{
	uint32_t i, cksum = 0;
	const uint8_t *buf = (const uint8_t *)spd;
	uint8_t rev, spd_cksum;

	rev = spd->spd_rev;
	spd_cksum = spd->cksum;

	/* Rev 1.X or less supported by this code */
	if (rev >= 0x20)
		goto error;

	/*
	 * The checksum is calculated on the first 64 bytes
	 * of the SPD as per JEDEC specification.
	 */
	for (i = 0; i < 63; i++)
		cksum += *buf++;
	cksum &= 0xFF;

	if (cksum != spd_cksum)
		goto error;

	return 0;
error:
	return 1;
}

uint32_t ddr3_spd_checksum_pass(const struct ddr3_spd_eeprom_s *spd)
{
	char crc_lsb, crc_msb;
	int csum16, len;

	/*
	 * SPD byte0[7] - CRC coverage
	 * 0 = CRC covers bytes 0~125
	 * 1 = CRC covers bytes 0~116
	 */

	len = !(spd->info_size_crc & 0x80) ? 126 : 117;
	csum16 = cyg_crc16((char *)spd, len);

	crc_lsb = (char) (csum16 & 0xff);
	crc_msb = (char) (csum16 >> 8);

	if (spd->crc[0] != crc_lsb || spd->crc[1] != crc_msb)
		return 1;

	return 0;
}