/*
* Copyright (c) 2007 Nicolas Joly
* Copyright (c) 2007 Paul Goyette
* Copyright (c) 2007 Tobias Nygren
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Serial Presence Detect (SPD) memory identification
*/
/* Format string for timing info */
#define LATENCY "tAA-tRCD-tRP-tRAS: %d-%d-%d-%d\n"
/* CRC functions used for certain memory types */
static uint16_t
spdcrc16(struct spdmem_softc *sc, int count)
{
uint16_t crc;
int i, j;
uint8_t val;
crc = 0;
for (j = 0; j <= count; j++) {
(sc->sc_read)(sc, j, &val);
crc = crc ^ val << 8;
for (i = 0; i < 8; ++i)
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
}
return (crc & 0xFFFF);
}
int
spdmem_common_probe(struct spdmem_softc *sc)
{
int cksum = 0;
uint8_t i, val, spd_type;
int spd_len, spd_crc_cover;
uint16_t crc_calc, crc_spd;
/* Read failed means a device doesn't exist */
if ((sc->sc_read)(sc, 2, &spd_type) != 0)
return 0;
/* Memory type should not be 0 */
if (spd_type == 0x00)
return 0;
/* For older memory types, validate the checksum over 1st 63 bytes */
if (spd_type <= SPDMEM_MEMTYPE_DDR2SDRAM) {
for (i = 0; i < 63; i++) {
(sc->sc_read)(sc, i, &val);
cksum += val;
}
if (s->sm_type == SPDMEM_MEMTYPE_DDR4SDRAM) {
/*
* An even newer encoding with one byte holding both
* the used-size and capacity values
*/
spd_len = s->sm_len & 0x0f;
spd_size = (s->sm_len >> 4) & 0x07;
/*
* Decode and print key SPD contents
*/
if (IS_RAMBUS_TYPE) {
if (s->sm_type == SPDMEM_MEMTYPE_RAMBUS)
type = "Rambus";
else if (s->sm_type == SPDMEM_MEMTYPE_DIRECTRAMBUS)
type = "Direct Rambus";
else
type = "Rambus (unknown)";
switch (s->sm_len) {
case 0:
rambus_rev = "Invalid";
break;
case 1:
rambus_rev = "0.7";
break;
case 2:
rambus_rev = "1.0";
break;
default:
rambus_rev = "Reserved";
break;
}
} else {
if (s->sm_type < __arraycount(spdmem_basic_types))
type = spdmem_basic_types[s->sm_type];
else
type = "unknown memory type";
if (s->sm_type == SPDMEM_MEMTYPE_EDO &&
s->sm_fpm.fpm_superset == SPDMEM_SUPERSET_EDO_PEM)
type = spdmem_superset_types[SPDMEM_SUPERSET_EDO_PEM];
if (s->sm_type == SPDMEM_MEMTYPE_SDRAM &&
s->sm_sdr.sdr_superset == SPDMEM_SUPERSET_SDRAM_PEM)
type = spdmem_superset_types[SPDMEM_SUPERSET_SDRAM_PEM];
if (s->sm_type == SPDMEM_MEMTYPE_DDRSDRAM &&
s->sm_ddr.ddr_superset == SPDMEM_SUPERSET_DDR_ESDRAM)
type =
spdmem_superset_types[SPDMEM_SUPERSET_DDR_ESDRAM];
if (s->sm_type == SPDMEM_MEMTYPE_SDRAM &&
s->sm_sdr.sdr_superset == SPDMEM_SUPERSET_ESDRAM) {
type = spdmem_superset_types[SPDMEM_SUPERSET_ESDRAM];
}
if (s->sm_type == SPDMEM_MEMTYPE_DDR4SDRAM &&
s->sm_ddr4.ddr4_mod_type <
__arraycount(spdmem_ddr4_module_types)) {
type = spdmem_ddr4_module_types[s->sm_ddr4.ddr4_mod_type];
}
}
strlcpy(sc->sc_type, type, SPDMEM_TYPE_MAXLEN);
if (s->sm_type == SPDMEM_MEMTYPE_DDR4SDRAM) {
/*
* The latest spec (DDR4 SPD Document Release 3) defines
* NVDIMM Hybrid only.
*/
if ((s->sm_ddr4.ddr4_hybrid)
&& (s->sm_ddr4.ddr4_hybrid_media == 1))
strlcat(sc->sc_type, " NVDIMM hybrid",
SPDMEM_TYPE_MAXLEN);
}
/* No further decode for RAMBUS memory */
return;
}
switch (s->sm_type) {
case SPDMEM_MEMTYPE_EDO:
case SPDMEM_MEMTYPE_FPM:
decode_edofpm(node, self, s);
break;
case SPDMEM_MEMTYPE_ROM:
decode_rom(node, self, s);
break;
case SPDMEM_MEMTYPE_SDRAM:
decode_sdram(node, self, s, spd_len);
break;
case SPDMEM_MEMTYPE_DDRSDRAM:
decode_ddr(node, self, s);
break;
case SPDMEM_MEMTYPE_DDR2SDRAM:
decode_ddr2(node, self, s);
break;
case SPDMEM_MEMTYPE_DDR3SDRAM:
decode_ddr3(node, self, s);
break;
case SPDMEM_MEMTYPE_FBDIMM:
case SPDMEM_MEMTYPE_FBDIMM_PROBE:
decode_fbdimm(node, self, s);
break;
case SPDMEM_MEMTYPE_DDR4SDRAM:
decode_ddr4(node, self, s);
break;
}
/* Dump SPD */
for (i = 0; i < spd_len; i += 16) {
unsigned int j, k;
aprint_debug_dev(self, "0x%02x:", i);
k = (spd_len > (i + 16)) ? i + 16 : spd_len;
for (j = i; j < k; j++)
aprint_debug(" %02x", ((uint8_t *)s)[j]);
aprint_debug("\n");
}
}
int
spdmem_common_detach(struct spdmem_softc *sc, device_t self)
{
sysctl_teardown(&sc->sc_sysctl_log);
return 0;
}
static void
decode_size_speed(device_t self, const struct sysctlnode *node,
int dimm_size, int cycle_time, int d_clk, int bits,
bool round, const char *ddr_type_string, int speed)
{
int p_clk;
struct spdmem_softc *sc = device_private(self);
if (cycle_time == 0) {
aprint_normal("\n");
return;
}
/*
* Calculate p_clk first, since for DDR3 we need maximum significance.
* DDR3 rating is not rounded to a multiple of 100. This results in
* cycle_time of 1.5ns displayed as PC3-10666.
*
* For SDRAM, the speed is provided by the caller so we use it.
*/
d_clk *= 1000 * 1000;
if (speed)
p_clk = speed;
else
p_clk = (d_clk * bits) / 8 / cycle_time;
d_clk = ((d_clk + cycle_time / 2) ) / cycle_time;
if (round) {
if ((p_clk % 100) >= 50)
p_clk += 50;
p_clk -= p_clk % 100;
}
aprint_normal(", %dMHz (%s-%d)\n",
d_clk, ddr_type_string, p_clk);
if (node != NULL)
sysctl_createv(&sc->sc_sysctl_log, 0, NULL, NULL,
CTLFLAG_IMMEDIATE,
CTLTYPE_INT, "speed",
SYSCTL_DESCR("memory speed in MHz"),
NULL, d_clk, NULL, 0,
CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
}
/* For DDR3, Voltage is written in another area */
if (!s->sm_ddr3.ddr3_NOT15V || s->sm_ddr3.ddr3_135V
|| s->sm_ddr3.ddr3_125V) {
aprint_verbose("%s:", device_xname(self));
if (!s->sm_ddr3.ddr3_NOT15V)
aprint_verbose(" 1.5V");
if (s->sm_ddr3.ddr3_135V)
aprint_verbose(" 1.35V");
if (s->sm_ddr3.ddr3_125V)
aprint_verbose(" 1.25V");
aprint_verbose(" operable\n");
}
}
aprint_naive("\n");
print_part(s->sm_ddr4.ddr4_part_number,
sizeof(s->sm_ddr4.ddr4_part_number));
aprint_normal_dev(self, "%s", spdmem_basic_types[s->sm_type]);
if (s->sm_ddr4.ddr4_mod_type < __arraycount(spdmem_ddr4_module_types))
aprint_normal(" (%s)",
spdmem_ddr4_module_types[s->sm_ddr4.ddr4_mod_type]);
aprint_normal(", %sECC, %stemp-sensor, ",
(s->sm_ddr4.ddr4_bus_width_extension) ? "" : "no ",
(s->sm_ddr4.ddr4_has_therm_sensor) ? "" : "no ");
/*
* DDR4 size calculation from JEDEC spec
*
* Module capacity in bytes is defined as
* Chip_Capacity_in_bits / 8bits-per-byte *
* primary_bus_width / DRAM_width *
* logical_ranks_per_DIMM
*
* logical_ranks_per DIMM equals package_ranks, but multiply
* by diecount for 3DS packages
*
* We further divide by 2**20 to get our answer in MB
*/
dimm_size = (s->sm_ddr4.ddr4_capacity + 28) /* chip_capacity */
- 20 /* convert to MB */
- 3 /* bits --> bytes */
+ (s->sm_ddr4.ddr4_primary_bus_width + 3); /* bus width */
switch (s->sm_ddr4.ddr4_device_width) { /* DRAM width */
case 0: dimm_size -= 2;
break;
case 1: dimm_size -= 3;
break;
case 2: dimm_size -= 4;
break;
case 4: dimm_size -= 5;
break;
default:
dimm_size = -1; /* flag invalid value */
}
if (dimm_size >= 0) {
dimm_size = (1 << dimm_size) *
(s->sm_ddr4.ddr4_package_ranks + 1); /* log.ranks/DIMM */
if (s->sm_ddr4.ddr4_signal_loading == 2) {
dimm_size *= (s->sm_ddr4.ddr4_diecount + 1);
}
}
/*
* Note that the ddr4_xxx_ftb fields are actually signed offsets from
* the corresponding mtb value, so we might have to subtract 256!
*/
#define __DDR4_VALUE(field) ((s->sm_ddr4.ddr4_##field##_mtb * 125 + \
s->sm_ddr4.ddr4_##field##_ftb) - \
((s->sm_ddr4.ddr4_##field##_ftb > 127)?256:0))
/*
* For now, the only value for mtb is 0 = 125ps, and ftb = 1ps
* so we don't need to figure out the time-base units - just
* hard-code them for now.
*/
cycle_time = __DDR4_VALUE(tCKAVGmin);
decode_size_speed(self, node, dimm_size, cycle_time, 2,
1 << (s->sm_ddr4.ddr4_primary_bus_width + 3),
TRUE, "PC4", 0);
/*
* Per JEDEC spec, rounding is done by taking the time value, dividing
* by the cycle time, subtracting .010 from the result, and then
* rounded up to the nearest integer. Unfortunately, none of their
* examples say what to do when the result of the subtraction is already
* an integer. For now, assume that we still round up (so an interval
* of exactly 12.010 clock cycles will be printed as 13).
*/
#define __DDR4_ROUND(value) ((value - 10) / 1000 + 1)