Home | History | Annotate | Line # | Download | only in ic
spdmem.c revision 1.10.4.4
      1  1.10.4.4     skrll /* $NetBSD: spdmem.c,v 1.10.4.4 2016/03/19 11:30:09 skrll Exp $ */
      2       1.1  pgoyette 
      3       1.1  pgoyette /*
      4       1.1  pgoyette  * Copyright (c) 2007 Nicolas Joly
      5       1.1  pgoyette  * Copyright (c) 2007 Paul Goyette
      6       1.1  pgoyette  * Copyright (c) 2007 Tobias Nygren
      7       1.1  pgoyette  * All rights reserved.
      8       1.1  pgoyette  *
      9       1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     10       1.1  pgoyette  * modification, are permitted provided that the following conditions
     11       1.1  pgoyette  * are met:
     12       1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     13       1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     14       1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     16       1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     17       1.1  pgoyette  * 3. The name of the author may not be used to endorse or promote products
     18       1.1  pgoyette  *    derived from this software without specific prior written permission.
     19       1.1  pgoyette  *
     20       1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
     21       1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1  pgoyette  */
     32       1.1  pgoyette 
     33       1.1  pgoyette /*
     34       1.1  pgoyette  * Serial Presence Detect (SPD) memory identification
     35       1.1  pgoyette  */
     36       1.1  pgoyette 
     37       1.1  pgoyette #include <sys/cdefs.h>
     38  1.10.4.4     skrll __KERNEL_RCSID(0, "$NetBSD: spdmem.c,v 1.10.4.4 2016/03/19 11:30:09 skrll Exp $");
     39       1.1  pgoyette 
     40       1.1  pgoyette #include <sys/param.h>
     41       1.1  pgoyette #include <sys/device.h>
     42       1.1  pgoyette #include <sys/endian.h>
     43       1.1  pgoyette #include <sys/sysctl.h>
     44       1.1  pgoyette #include <machine/bswap.h>
     45       1.1  pgoyette 
     46       1.1  pgoyette #include <dev/i2c/i2cvar.h>
     47       1.1  pgoyette #include <dev/ic/spdmemreg.h>
     48       1.1  pgoyette #include <dev/ic/spdmemvar.h>
     49       1.1  pgoyette 
     50       1.1  pgoyette /* Routines for decoding spd data */
     51       1.1  pgoyette static void decode_edofpm(const struct sysctlnode *, device_t, struct spdmem *);
     52       1.1  pgoyette static void decode_rom(const struct sysctlnode *, device_t, struct spdmem *);
     53       1.1  pgoyette static void decode_sdram(const struct sysctlnode *, device_t, struct spdmem *,
     54       1.1  pgoyette 	int);
     55       1.1  pgoyette static void decode_ddr(const struct sysctlnode *, device_t, struct spdmem *);
     56       1.1  pgoyette static void decode_ddr2(const struct sysctlnode *, device_t, struct spdmem *);
     57       1.1  pgoyette static void decode_ddr3(const struct sysctlnode *, device_t, struct spdmem *);
     58  1.10.4.2     skrll static void decode_ddr4(const struct sysctlnode *, device_t, struct spdmem *);
     59       1.1  pgoyette static void decode_fbdimm(const struct sysctlnode *, device_t, struct spdmem *);
     60       1.1  pgoyette 
     61       1.3  pgoyette static void decode_size_speed(device_t, const struct sysctlnode *,
     62       1.3  pgoyette 			      int, int, int, int, bool, const char *, int);
     63       1.1  pgoyette static void decode_voltage_refresh(device_t, struct spdmem *);
     64       1.1  pgoyette 
     65       1.1  pgoyette #define IS_RAMBUS_TYPE (s->sm_len < 4)
     66       1.1  pgoyette 
     67  1.10.4.1     skrll static const char* const spdmem_basic_types[] = {
     68       1.1  pgoyette 	"unknown",
     69       1.1  pgoyette 	"FPM",
     70       1.1  pgoyette 	"EDO",
     71       1.1  pgoyette 	"Pipelined Nibble",
     72       1.1  pgoyette 	"SDRAM",
     73       1.1  pgoyette 	"ROM",
     74       1.1  pgoyette 	"DDR SGRAM",
     75       1.1  pgoyette 	"DDR SDRAM",
     76       1.1  pgoyette 	"DDR2 SDRAM",
     77       1.1  pgoyette 	"DDR2 SDRAM FB",
     78       1.1  pgoyette 	"DDR2 SDRAM FB Probe",
     79  1.10.4.1     skrll 	"DDR3 SDRAM",
     80  1.10.4.3     skrll 	"DDR4 SDRAM",
     81  1.10.4.3     skrll 	"unknown",
     82  1.10.4.3     skrll 	"DDR4E SDRAM",
     83  1.10.4.3     skrll 	"LPDDR3 SDRAM",
     84  1.10.4.3     skrll 	"LPDDR4 SDRAM"
     85       1.1  pgoyette };
     86       1.1  pgoyette 
     87  1.10.4.2     skrll static const char* const spdmem_ddr4_module_types[] = {
     88  1.10.4.2     skrll 	"DDR4 Extended",
     89  1.10.4.2     skrll 	"DDR4 RDIMM",
     90  1.10.4.2     skrll 	"DDR4 UDIMM",
     91  1.10.4.2     skrll 	"DDR4 SO-DIMM",
     92  1.10.4.2     skrll 	"DDR4 Load-Reduced DIMM",
     93  1.10.4.2     skrll 	"DDR4 Mini-RDIMM",
     94  1.10.4.2     skrll 	"DDR4 Mini-UDIMM",
     95  1.10.4.2     skrll 	"DDR4 Reserved",
     96  1.10.4.2     skrll 	"DDR4 72Bit SO-RDIMM",
     97  1.10.4.2     skrll 	"DDR4 72Bit SO-UDIMM",
     98  1.10.4.2     skrll 	"DDR4 Undefined",
     99  1.10.4.2     skrll 	"DDR4 Reserved",
    100  1.10.4.2     skrll 	"DDR4 16Bit SO-DIMM",
    101  1.10.4.2     skrll 	"DDR4 32Bit SO-DIMM",
    102  1.10.4.2     skrll 	"DDR4 Reserved",
    103  1.10.4.2     skrll 	"DDR4 Undefined"
    104  1.10.4.2     skrll };
    105  1.10.4.2     skrll 
    106  1.10.4.1     skrll static const char* const spdmem_superset_types[] = {
    107       1.1  pgoyette 	"unknown",
    108       1.1  pgoyette 	"ESDRAM",
    109       1.1  pgoyette 	"DDR ESDRAM",
    110       1.1  pgoyette 	"PEM EDO",
    111       1.1  pgoyette 	"PEM SDRAM"
    112       1.1  pgoyette };
    113       1.1  pgoyette 
    114  1.10.4.1     skrll static const char* const spdmem_voltage_types[] = {
    115       1.1  pgoyette 	"TTL (5V tolerant)",
    116       1.1  pgoyette 	"LvTTL (not 5V tolerant)",
    117       1.1  pgoyette 	"HSTL 1.5V",
    118       1.1  pgoyette 	"SSTL 3.3V",
    119       1.1  pgoyette 	"SSTL 2.5V",
    120       1.1  pgoyette 	"SSTL 1.8V"
    121       1.1  pgoyette };
    122       1.1  pgoyette 
    123  1.10.4.1     skrll static const char* const spdmem_refresh_types[] = {
    124       1.1  pgoyette 	"15.625us",
    125       1.1  pgoyette 	"3.9us",
    126       1.1  pgoyette 	"7.8us",
    127       1.1  pgoyette 	"31.3us",
    128       1.1  pgoyette 	"62.5us",
    129       1.1  pgoyette 	"125us"
    130       1.1  pgoyette };
    131       1.1  pgoyette 
    132  1.10.4.1     skrll static const char* const spdmem_parity_types[] = {
    133       1.1  pgoyette 	"no parity or ECC",
    134       1.1  pgoyette 	"data parity",
    135       1.1  pgoyette 	"data ECC",
    136       1.1  pgoyette 	"data parity and ECC",
    137       1.1  pgoyette 	"cmd/addr parity",
    138       1.1  pgoyette 	"cmd/addr/data parity",
    139       1.1  pgoyette 	"cmd/addr parity, data ECC",
    140       1.1  pgoyette 	"cmd/addr/data parity, data ECC"
    141       1.1  pgoyette };
    142       1.1  pgoyette 
    143  1.10.4.2     skrll int spd_rom_sizes[] = { 0, 128, 256, 384, 512 };
    144  1.10.4.2     skrll 
    145  1.10.4.2     skrll 
    146       1.1  pgoyette /* Cycle time fractional values (units of .001 ns) for DDR2 SDRAM */
    147       1.1  pgoyette static const uint16_t spdmem_cycle_frac[] = {
    148       1.1  pgoyette 	0, 100, 200, 300, 400, 500, 600, 700, 800, 900,
    149       1.1  pgoyette 	250, 333, 667, 750, 999, 999
    150       1.1  pgoyette };
    151       1.1  pgoyette 
    152       1.1  pgoyette /* Format string for timing info */
    153       1.5       wiz #define	LATENCY	"tAA-tRCD-tRP-tRAS: %d-%d-%d-%d\n"
    154       1.1  pgoyette 
    155       1.1  pgoyette /* CRC functions used for certain memory types */
    156       1.1  pgoyette 
    157  1.10.4.4     skrll static uint16_t
    158  1.10.4.4     skrll spdcrc16(struct spdmem_softc *sc, int count)
    159       1.1  pgoyette {
    160       1.1  pgoyette 	uint16_t crc;
    161       1.1  pgoyette 	int i, j;
    162       1.1  pgoyette 	uint8_t val;
    163       1.1  pgoyette 	crc = 0;
    164       1.1  pgoyette 	for (j = 0; j <= count; j++) {
    165  1.10.4.4     skrll 		(sc->sc_read)(sc, j, &val);
    166       1.1  pgoyette 		crc = crc ^ val << 8;
    167       1.1  pgoyette 		for (i = 0; i < 8; ++i)
    168       1.1  pgoyette 			if (crc & 0x8000)
    169       1.1  pgoyette 				crc = crc << 1 ^ 0x1021;
    170       1.1  pgoyette 			else
    171       1.1  pgoyette 				crc = crc << 1;
    172       1.1  pgoyette 	}
    173       1.1  pgoyette 	return (crc & 0xFFFF);
    174       1.1  pgoyette }
    175       1.1  pgoyette 
    176       1.1  pgoyette int
    177       1.1  pgoyette spdmem_common_probe(struct spdmem_softc *sc)
    178       1.1  pgoyette {
    179       1.1  pgoyette 	int cksum = 0;
    180       1.1  pgoyette 	uint8_t i, val, spd_type;
    181       1.1  pgoyette 	int spd_len, spd_crc_cover;
    182       1.1  pgoyette 	uint16_t crc_calc, crc_spd;
    183       1.1  pgoyette 
    184  1.10.4.4     skrll 	/* Read failed means a device doesn't exist */
    185  1.10.4.4     skrll 	if ((sc->sc_read)(sc, 2, &spd_type) != 0)
    186  1.10.4.4     skrll 		return 0;
    187       1.1  pgoyette 
    188       1.1  pgoyette 	/* For older memory types, validate the checksum over 1st 63 bytes */
    189       1.1  pgoyette 	if (spd_type <= SPDMEM_MEMTYPE_DDR2SDRAM) {
    190  1.10.4.4     skrll 		for (i = 0; i < 63; i++) {
    191  1.10.4.4     skrll 			(sc->sc_read)(sc, i, &val);
    192  1.10.4.4     skrll 			cksum += val;
    193  1.10.4.4     skrll 		}
    194       1.1  pgoyette 
    195  1.10.4.4     skrll 		(sc->sc_read)(sc, 63, &val);
    196       1.1  pgoyette 
    197  1.10.4.4     skrll 		if ((cksum & 0xff) != val) {
    198       1.1  pgoyette 			aprint_debug("spd checksum failed, calc = 0x%02x, "
    199       1.1  pgoyette 				     "spd = 0x%02x\n", cksum, val);
    200       1.1  pgoyette 			return 0;
    201       1.1  pgoyette 		} else
    202       1.1  pgoyette 			return 1;
    203       1.1  pgoyette 	}
    204       1.1  pgoyette 
    205       1.1  pgoyette 	/* For DDR3 and FBDIMM, verify the CRC */
    206       1.1  pgoyette 	else if (spd_type <= SPDMEM_MEMTYPE_DDR3SDRAM) {
    207  1.10.4.4     skrll 		(sc->sc_read)(sc, 0, &val);
    208  1.10.4.4     skrll 		spd_len = val;
    209       1.2  pgoyette 		if (spd_len & SPDMEM_SPDCRC_116)
    210       1.1  pgoyette 			spd_crc_cover = 116;
    211       1.1  pgoyette 		else
    212       1.1  pgoyette 			spd_crc_cover = 125;
    213       1.1  pgoyette 		switch (spd_len & SPDMEM_SPDLEN_MASK) {
    214       1.1  pgoyette 		case SPDMEM_SPDLEN_128:
    215       1.1  pgoyette 			spd_len = 128;
    216       1.1  pgoyette 			break;
    217       1.1  pgoyette 		case SPDMEM_SPDLEN_176:
    218       1.1  pgoyette 			spd_len = 176;
    219       1.1  pgoyette 			break;
    220       1.1  pgoyette 		case SPDMEM_SPDLEN_256:
    221       1.1  pgoyette 			spd_len = 256;
    222       1.1  pgoyette 			break;
    223       1.1  pgoyette 		default:
    224       1.1  pgoyette 			return 0;
    225       1.1  pgoyette 		}
    226       1.1  pgoyette 		if (spd_crc_cover > spd_len)
    227       1.1  pgoyette 			return 0;
    228       1.1  pgoyette 		crc_calc = spdcrc16(sc, spd_crc_cover);
    229  1.10.4.4     skrll 		(sc->sc_read)(sc, 127, &val);
    230  1.10.4.4     skrll 		crc_spd = val << 8;
    231  1.10.4.4     skrll 		(sc->sc_read)(sc, 126, &val);
    232  1.10.4.4     skrll 		crc_spd |= val;
    233       1.1  pgoyette 		if (crc_calc != crc_spd) {
    234       1.1  pgoyette 			aprint_debug("crc16 failed, covers %d bytes, "
    235       1.1  pgoyette 				     "calc = 0x%04x, spd = 0x%04x\n",
    236       1.1  pgoyette 				     spd_crc_cover, crc_calc, crc_spd);
    237       1.1  pgoyette 			return 0;
    238       1.1  pgoyette 		}
    239       1.1  pgoyette 		return 1;
    240  1.10.4.2     skrll 	} else if (spd_type == SPDMEM_MEMTYPE_DDR4SDRAM) {
    241  1.10.4.4     skrll 		(sc->sc_read)(sc, 0, &val);
    242  1.10.4.4     skrll 		spd_len = val & 0x0f;
    243  1.10.4.2     skrll 		if ((unsigned int)spd_len > __arraycount(spd_rom_sizes))
    244  1.10.4.2     skrll 			return 0;
    245  1.10.4.2     skrll 		spd_len = spd_rom_sizes[spd_len];
    246  1.10.4.3     skrll 		spd_crc_cover = 125; /* For byte 0 to 125 */
    247  1.10.4.2     skrll 		if (spd_crc_cover > spd_len)
    248  1.10.4.2     skrll 			return 0;
    249  1.10.4.2     skrll 		crc_calc = spdcrc16(sc, spd_crc_cover);
    250  1.10.4.4     skrll 		(sc->sc_read)(sc, 127, &val);
    251  1.10.4.4     skrll 		crc_spd = val << 8;
    252  1.10.4.4     skrll 		(sc->sc_read)(sc, 126, &val);
    253  1.10.4.4     skrll 		crc_spd |= val;
    254  1.10.4.2     skrll 		if (crc_calc != crc_spd) {
    255  1.10.4.2     skrll 			aprint_debug("crc16 failed, covers %d bytes, "
    256  1.10.4.2     skrll 				     "calc = 0x%04x, spd = 0x%04x\n",
    257  1.10.4.2     skrll 				     spd_crc_cover, crc_calc, crc_spd);
    258  1.10.4.2     skrll 			return 0;
    259  1.10.4.2     skrll 		}
    260  1.10.4.2     skrll 		/*
    261  1.10.4.2     skrll 		 * We probably could also verify the CRC for the other
    262  1.10.4.2     skrll 		 * "pages" of SPD data in blocks 1 and 2, but we'll do
    263  1.10.4.2     skrll 		 * it some other time.
    264  1.10.4.2     skrll 		 */
    265  1.10.4.2     skrll 		return 1;
    266  1.10.4.2     skrll 	} else
    267  1.10.4.2     skrll 		return 0;
    268       1.1  pgoyette 
    269       1.1  pgoyette 	/* For unrecognized memory types, don't match at all */
    270       1.1  pgoyette 	return 0;
    271       1.1  pgoyette }
    272       1.1  pgoyette 
    273       1.1  pgoyette void
    274       1.1  pgoyette spdmem_common_attach(struct spdmem_softc *sc, device_t self)
    275       1.1  pgoyette {
    276       1.1  pgoyette 	struct spdmem *s = &(sc->sc_spd_data);
    277       1.1  pgoyette 	const char *type;
    278       1.1  pgoyette 	const char *rambus_rev = "Reserved";
    279       1.1  pgoyette 	int dimm_size;
    280       1.3  pgoyette 	unsigned int i, spd_len, spd_size;
    281       1.1  pgoyette 	const struct sysctlnode *node = NULL;
    282       1.1  pgoyette 
    283  1.10.4.4     skrll 	(sc->sc_read)(sc, 0, &s->sm_len);
    284  1.10.4.4     skrll 	(sc->sc_read)(sc, 1, &s->sm_size);
    285  1.10.4.4     skrll 	(sc->sc_read)(sc, 2, &s->sm_type);
    286       1.1  pgoyette 
    287  1.10.4.2     skrll 	if (s->sm_type == SPDMEM_MEMTYPE_DDR4SDRAM) {
    288  1.10.4.2     skrll 		/*
    289  1.10.4.2     skrll 		 * An even newer encoding with one byte holding both
    290  1.10.4.2     skrll 		 * the used-size and capacity values
    291  1.10.4.2     skrll 		 */
    292  1.10.4.2     skrll 		spd_len = s->sm_len & 0x0f;
    293  1.10.4.2     skrll 		spd_size = (s->sm_len >> 4) & 0x07;
    294  1.10.4.2     skrll 
    295  1.10.4.2     skrll 		spd_len = spd_rom_sizes[spd_len];
    296  1.10.4.2     skrll 		spd_size *= 512;
    297  1.10.4.2     skrll 
    298  1.10.4.2     skrll 	} else if (s->sm_type >= SPDMEM_MEMTYPE_FBDIMM) {
    299  1.10.4.2     skrll 		/*
    300  1.10.4.2     skrll 		 * FBDIMM and DDR3 (and probably all newer) have a different
    301  1.10.4.2     skrll 		 * encoding of the SPD EEPROM used/total sizes
    302  1.10.4.2     skrll 		 */
    303       1.1  pgoyette 		spd_size = 64 << (s->sm_len & SPDMEM_SPDSIZE_MASK);
    304       1.1  pgoyette 		switch (s->sm_len & SPDMEM_SPDLEN_MASK) {
    305       1.1  pgoyette 		case SPDMEM_SPDLEN_128:
    306       1.1  pgoyette 			spd_len = 128;
    307       1.1  pgoyette 			break;
    308       1.1  pgoyette 		case SPDMEM_SPDLEN_176:
    309       1.1  pgoyette 			spd_len = 176;
    310       1.1  pgoyette 			break;
    311       1.1  pgoyette 		case SPDMEM_SPDLEN_256:
    312       1.1  pgoyette 			spd_len = 256;
    313       1.1  pgoyette 			break;
    314       1.1  pgoyette 		default:
    315       1.1  pgoyette 			spd_len = 64;
    316       1.1  pgoyette 			break;
    317       1.1  pgoyette 		}
    318       1.1  pgoyette 	} else {
    319       1.1  pgoyette 		spd_size = 1 << s->sm_size;
    320       1.1  pgoyette 		spd_len = s->sm_len;
    321       1.1  pgoyette 		if (spd_len < 64)
    322       1.1  pgoyette 			spd_len = 64;
    323       1.1  pgoyette 	}
    324       1.1  pgoyette 	if (spd_len > spd_size)
    325       1.1  pgoyette 		spd_len = spd_size;
    326       1.1  pgoyette 	if (spd_len > sizeof(struct spdmem))
    327       1.1  pgoyette 		spd_len = sizeof(struct spdmem);
    328       1.1  pgoyette 	for (i = 3; i < spd_len; i++)
    329  1.10.4.4     skrll 		(sc->sc_read)(sc, i, &((uint8_t *)s)[i]);
    330       1.1  pgoyette 
    331       1.1  pgoyette 	/*
    332       1.1  pgoyette 	 * Setup our sysctl subtree, hw.spdmemN
    333       1.1  pgoyette 	 */
    334       1.3  pgoyette 	sc->sc_sysctl_log = NULL;
    335       1.9     pooka 	sysctl_createv(&sc->sc_sysctl_log, 0, NULL, &node,
    336       1.9     pooka 	    0, CTLTYPE_NODE,
    337       1.9     pooka 	    device_xname(self), NULL, NULL, 0, NULL, 0,
    338       1.9     pooka 	    CTL_HW, CTL_CREATE, CTL_EOL);
    339       1.1  pgoyette 	if (node != NULL && spd_len != 0)
    340       1.3  pgoyette                 sysctl_createv(&sc->sc_sysctl_log, 0, NULL, NULL,
    341       1.1  pgoyette                     0,
    342       1.1  pgoyette                     CTLTYPE_STRUCT, "spd_data",
    343       1.1  pgoyette 		    SYSCTL_DESCR("raw spd data"), NULL,
    344       1.1  pgoyette                     0, s, spd_len,
    345       1.1  pgoyette                     CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
    346       1.1  pgoyette 
    347       1.1  pgoyette 	/*
    348       1.1  pgoyette 	 * Decode and print key SPD contents
    349       1.1  pgoyette 	 */
    350       1.1  pgoyette 	if (IS_RAMBUS_TYPE) {
    351       1.1  pgoyette 		if (s->sm_type == SPDMEM_MEMTYPE_RAMBUS)
    352       1.1  pgoyette 			type = "Rambus";
    353       1.1  pgoyette 		else if (s->sm_type == SPDMEM_MEMTYPE_DIRECTRAMBUS)
    354       1.1  pgoyette 			type = "Direct Rambus";
    355       1.1  pgoyette 		else
    356       1.1  pgoyette 			type = "Rambus (unknown)";
    357       1.1  pgoyette 
    358       1.1  pgoyette 		switch (s->sm_len) {
    359       1.1  pgoyette 		case 0:
    360       1.1  pgoyette 			rambus_rev = "Invalid";
    361       1.1  pgoyette 			break;
    362       1.1  pgoyette 		case 1:
    363       1.1  pgoyette 			rambus_rev = "0.7";
    364       1.1  pgoyette 			break;
    365       1.1  pgoyette 		case 2:
    366       1.1  pgoyette 			rambus_rev = "1.0";
    367       1.1  pgoyette 			break;
    368       1.1  pgoyette 		default:
    369       1.1  pgoyette 			rambus_rev = "Reserved";
    370       1.1  pgoyette 			break;
    371       1.1  pgoyette 		}
    372       1.1  pgoyette 	} else {
    373       1.1  pgoyette 		if (s->sm_type < __arraycount(spdmem_basic_types))
    374       1.1  pgoyette 			type = spdmem_basic_types[s->sm_type];
    375       1.1  pgoyette 		else
    376       1.1  pgoyette 			type = "unknown memory type";
    377       1.1  pgoyette 
    378       1.1  pgoyette 		if (s->sm_type == SPDMEM_MEMTYPE_EDO &&
    379       1.1  pgoyette 		    s->sm_fpm.fpm_superset == SPDMEM_SUPERSET_EDO_PEM)
    380       1.1  pgoyette 			type = spdmem_superset_types[SPDMEM_SUPERSET_EDO_PEM];
    381       1.1  pgoyette 		if (s->sm_type == SPDMEM_MEMTYPE_SDRAM &&
    382       1.1  pgoyette 		    s->sm_sdr.sdr_superset == SPDMEM_SUPERSET_SDRAM_PEM)
    383       1.1  pgoyette 			type = spdmem_superset_types[SPDMEM_SUPERSET_SDRAM_PEM];
    384       1.1  pgoyette 		if (s->sm_type == SPDMEM_MEMTYPE_DDRSDRAM &&
    385       1.1  pgoyette 		    s->sm_ddr.ddr_superset == SPDMEM_SUPERSET_DDR_ESDRAM)
    386       1.1  pgoyette 			type =
    387       1.1  pgoyette 			    spdmem_superset_types[SPDMEM_SUPERSET_DDR_ESDRAM];
    388       1.1  pgoyette 		if (s->sm_type == SPDMEM_MEMTYPE_SDRAM &&
    389       1.1  pgoyette 		    s->sm_sdr.sdr_superset == SPDMEM_SUPERSET_ESDRAM) {
    390       1.1  pgoyette 			type = spdmem_superset_types[SPDMEM_SUPERSET_ESDRAM];
    391       1.1  pgoyette 		}
    392  1.10.4.2     skrll 		if (s->sm_type == SPDMEM_MEMTYPE_DDR4SDRAM &&
    393  1.10.4.2     skrll 		    s->sm_ddr4.ddr4_mod_type <
    394  1.10.4.2     skrll 				__arraycount(spdmem_ddr4_module_types)) {
    395  1.10.4.2     skrll 			type = spdmem_ddr4_module_types[s->sm_ddr4.ddr4_mod_type];
    396  1.10.4.2     skrll 		}
    397       1.1  pgoyette 	}
    398       1.1  pgoyette 
    399       1.1  pgoyette 	strlcpy(sc->sc_type, type, SPDMEM_TYPE_MAXLEN);
    400  1.10.4.3     skrll 
    401  1.10.4.3     skrll 	if (s->sm_type == SPDMEM_MEMTYPE_DDR4SDRAM) {
    402  1.10.4.3     skrll 		/*
    403  1.10.4.3     skrll 		 * The latest spec (DDR4 SPD Document Release 3) defines
    404  1.10.4.3     skrll 		 * NVDIMM Hybrid only.
    405  1.10.4.3     skrll 		 */
    406  1.10.4.3     skrll 		if ((s->sm_ddr4.ddr4_hybrid)
    407  1.10.4.3     skrll 		    && (s->sm_ddr4.ddr4_hybrid_media == 1))
    408  1.10.4.3     skrll 			strlcat(sc->sc_type, " NVDIMM hybrid",
    409  1.10.4.3     skrll 			    SPDMEM_TYPE_MAXLEN);
    410  1.10.4.3     skrll 	}
    411  1.10.4.3     skrll 
    412       1.1  pgoyette 	if (node != NULL)
    413       1.3  pgoyette 		sysctl_createv(&sc->sc_sysctl_log, 0, NULL, NULL,
    414       1.1  pgoyette 		    0,
    415       1.1  pgoyette 		    CTLTYPE_STRING, "mem_type",
    416       1.1  pgoyette 		    SYSCTL_DESCR("memory module type"), NULL,
    417       1.1  pgoyette 		    0, sc->sc_type, 0,
    418       1.1  pgoyette 		    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
    419       1.1  pgoyette 
    420       1.1  pgoyette 	if (IS_RAMBUS_TYPE) {
    421       1.8     soren 		aprint_naive("\n");
    422       1.8     soren 		aprint_normal("\n");
    423       1.8     soren 		aprint_normal_dev(self, "%s, SPD Revision %s", type, rambus_rev);
    424       1.1  pgoyette 		dimm_size = 1 << (s->sm_rdr.rdr_rows + s->sm_rdr.rdr_cols - 13);
    425       1.1  pgoyette 		if (dimm_size >= 1024)
    426       1.1  pgoyette 			aprint_normal(", %dGB\n", dimm_size / 1024);
    427       1.1  pgoyette 		else
    428       1.1  pgoyette 			aprint_normal(", %dMB\n", dimm_size);
    429       1.1  pgoyette 
    430       1.1  pgoyette 		/* No further decode for RAMBUS memory */
    431       1.1  pgoyette 		return;
    432       1.1  pgoyette 	}
    433       1.1  pgoyette 	switch (s->sm_type) {
    434       1.1  pgoyette 	case SPDMEM_MEMTYPE_EDO:
    435       1.1  pgoyette 	case SPDMEM_MEMTYPE_FPM:
    436       1.1  pgoyette 		decode_edofpm(node, self, s);
    437       1.1  pgoyette 		break;
    438       1.1  pgoyette 	case SPDMEM_MEMTYPE_ROM:
    439       1.1  pgoyette 		decode_rom(node, self, s);
    440       1.1  pgoyette 		break;
    441       1.1  pgoyette 	case SPDMEM_MEMTYPE_SDRAM:
    442       1.1  pgoyette 		decode_sdram(node, self, s, spd_len);
    443       1.1  pgoyette 		break;
    444       1.1  pgoyette 	case SPDMEM_MEMTYPE_DDRSDRAM:
    445       1.1  pgoyette 		decode_ddr(node, self, s);
    446       1.1  pgoyette 		break;
    447       1.1  pgoyette 	case SPDMEM_MEMTYPE_DDR2SDRAM:
    448       1.1  pgoyette 		decode_ddr2(node, self, s);
    449       1.1  pgoyette 		break;
    450       1.1  pgoyette 	case SPDMEM_MEMTYPE_DDR3SDRAM:
    451       1.1  pgoyette 		decode_ddr3(node, self, s);
    452       1.1  pgoyette 		break;
    453       1.1  pgoyette 	case SPDMEM_MEMTYPE_FBDIMM:
    454       1.1  pgoyette 	case SPDMEM_MEMTYPE_FBDIMM_PROBE:
    455       1.1  pgoyette 		decode_fbdimm(node, self, s);
    456       1.1  pgoyette 		break;
    457  1.10.4.2     skrll 	case SPDMEM_MEMTYPE_DDR4SDRAM:
    458  1.10.4.2     skrll 		decode_ddr4(node, self, s);
    459  1.10.4.2     skrll 		break;
    460       1.1  pgoyette 	}
    461       1.8     soren 
    462       1.8     soren 	/* Dump SPD */
    463       1.8     soren 	for (i = 0; i < spd_len;  i += 16) {
    464       1.8     soren 		unsigned int j, k;
    465       1.8     soren 		aprint_debug_dev(self, "0x%02x:", i);
    466       1.8     soren 		k = (spd_len > (i + 16)) ? i + 16 : spd_len;
    467       1.8     soren 		for (j = i; j < k; j++)
    468       1.8     soren 			aprint_debug(" %02x", ((uint8_t *)s)[j]);
    469       1.8     soren 		aprint_debug("\n");
    470       1.8     soren 	}
    471       1.1  pgoyette }
    472       1.1  pgoyette 
    473       1.3  pgoyette int
    474       1.3  pgoyette spdmem_common_detach(struct spdmem_softc *sc, device_t self)
    475       1.3  pgoyette {
    476       1.3  pgoyette 	sysctl_teardown(&sc->sc_sysctl_log);
    477       1.3  pgoyette 
    478       1.3  pgoyette 	return 0;
    479       1.3  pgoyette }
    480       1.3  pgoyette 
    481       1.1  pgoyette static void
    482       1.3  pgoyette decode_size_speed(device_t self, const struct sysctlnode *node,
    483       1.3  pgoyette 		  int dimm_size, int cycle_time, int d_clk, int bits,
    484       1.3  pgoyette 		  bool round, const char *ddr_type_string, int speed)
    485       1.1  pgoyette {
    486       1.1  pgoyette 	int p_clk;
    487       1.7       chs 	struct spdmem_softc *sc = device_private(self);
    488       1.1  pgoyette 
    489       1.1  pgoyette 	if (dimm_size < 1024)
    490       1.1  pgoyette 		aprint_normal("%dMB", dimm_size);
    491       1.1  pgoyette 	else
    492       1.1  pgoyette 		aprint_normal("%dGB", dimm_size / 1024);
    493       1.1  pgoyette 	if (node != NULL)
    494       1.3  pgoyette 		sysctl_createv(&sc->sc_sysctl_log, 0, NULL, NULL,
    495       1.1  pgoyette 		    CTLFLAG_IMMEDIATE,
    496       1.1  pgoyette 		    CTLTYPE_INT, "size",
    497       1.1  pgoyette 		    SYSCTL_DESCR("module size in MB"), NULL,
    498       1.1  pgoyette 		    dimm_size, NULL, 0,
    499       1.1  pgoyette 		    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
    500       1.1  pgoyette 
    501       1.1  pgoyette 	if (cycle_time == 0) {
    502       1.1  pgoyette 		aprint_normal("\n");
    503       1.1  pgoyette 		return;
    504       1.1  pgoyette 	}
    505       1.1  pgoyette 
    506       1.1  pgoyette 	/*
    507       1.1  pgoyette 	 * Calculate p_clk first, since for DDR3 we need maximum significance.
    508       1.1  pgoyette 	 * DDR3 rating is not rounded to a multiple of 100.  This results in
    509       1.1  pgoyette 	 * cycle_time of 1.5ns displayed as PC3-10666.
    510       1.1  pgoyette 	 *
    511       1.1  pgoyette 	 * For SDRAM, the speed is provided by the caller so we use it.
    512       1.1  pgoyette 	 */
    513       1.1  pgoyette 	d_clk *= 1000 * 1000;
    514       1.1  pgoyette 	if (speed)
    515       1.1  pgoyette 		p_clk = speed;
    516       1.1  pgoyette 	else
    517       1.1  pgoyette 		p_clk = (d_clk * bits) / 8 / cycle_time;
    518       1.1  pgoyette 	d_clk = ((d_clk + cycle_time / 2) ) / cycle_time;
    519       1.1  pgoyette 	if (round) {
    520       1.1  pgoyette 		if ((p_clk % 100) >= 50)
    521       1.1  pgoyette 			p_clk += 50;
    522       1.1  pgoyette 		p_clk -= p_clk % 100;
    523       1.1  pgoyette 	}
    524       1.1  pgoyette 	aprint_normal(", %dMHz (%s-%d)\n",
    525       1.1  pgoyette 		      d_clk, ddr_type_string, p_clk);
    526       1.1  pgoyette 	if (node != NULL)
    527       1.3  pgoyette 		sysctl_createv(&sc->sc_sysctl_log, 0, NULL, NULL,
    528       1.1  pgoyette 			       CTLFLAG_IMMEDIATE,
    529       1.1  pgoyette 			       CTLTYPE_INT, "speed",
    530       1.1  pgoyette 			       SYSCTL_DESCR("memory speed in MHz"),
    531       1.1  pgoyette 			       NULL, d_clk, NULL, 0,
    532       1.1  pgoyette 			       CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
    533       1.1  pgoyette }
    534       1.1  pgoyette 
    535       1.1  pgoyette static void
    536       1.1  pgoyette decode_voltage_refresh(device_t self, struct spdmem *s)
    537       1.1  pgoyette {
    538       1.1  pgoyette 	const char *voltage, *refresh;
    539       1.1  pgoyette 
    540       1.1  pgoyette 	if (s->sm_voltage < __arraycount(spdmem_voltage_types))
    541       1.1  pgoyette 		voltage = spdmem_voltage_types[s->sm_voltage];
    542       1.1  pgoyette 	else
    543       1.1  pgoyette 		voltage = "unknown";
    544       1.1  pgoyette 
    545       1.1  pgoyette 	if (s->sm_refresh < __arraycount(spdmem_refresh_types))
    546       1.1  pgoyette 		refresh = spdmem_refresh_types[s->sm_refresh];
    547       1.1  pgoyette 	else
    548       1.1  pgoyette 		refresh = "unknown";
    549       1.1  pgoyette 
    550       1.1  pgoyette 	aprint_verbose_dev(self, "voltage %s, refresh time %s%s\n",
    551       1.1  pgoyette 			voltage, refresh,
    552       1.1  pgoyette 			s->sm_selfrefresh?" (self-refreshing)":"");
    553       1.1  pgoyette }
    554       1.1  pgoyette 
    555       1.1  pgoyette static void
    556  1.10.4.3     skrll decode_edofpm(const struct sysctlnode *node, device_t self, struct spdmem *s)
    557  1.10.4.3     skrll {
    558  1.10.4.3     skrll 
    559       1.8     soren 	aprint_naive("\n");
    560       1.8     soren 	aprint_normal("\n");
    561       1.8     soren 	aprint_normal_dev(self, "%s", spdmem_basic_types[s->sm_type]);
    562       1.8     soren 
    563       1.1  pgoyette 	aprint_normal("\n");
    564       1.1  pgoyette 	aprint_verbose_dev(self,
    565       1.1  pgoyette 	    "%d rows, %d cols, %d banks, %dns tRAC, %dns tCAC\n",
    566       1.1  pgoyette 	    s->sm_fpm.fpm_rows, s->sm_fpm.fpm_cols, s->sm_fpm.fpm_banks,
    567       1.1  pgoyette 	    s->sm_fpm.fpm_tRAC, s->sm_fpm.fpm_tCAC);
    568       1.1  pgoyette }
    569       1.1  pgoyette 
    570       1.1  pgoyette static void
    571  1.10.4.3     skrll decode_rom(const struct sysctlnode *node, device_t self, struct spdmem *s)
    572  1.10.4.3     skrll {
    573  1.10.4.3     skrll 
    574       1.8     soren 	aprint_naive("\n");
    575       1.8     soren 	aprint_normal("\n");
    576       1.8     soren 	aprint_normal_dev(self, "%s", spdmem_basic_types[s->sm_type]);
    577       1.8     soren 
    578       1.1  pgoyette 	aprint_normal("\n");
    579       1.1  pgoyette 	aprint_verbose_dev(self, "%d rows, %d cols, %d banks\n",
    580       1.1  pgoyette 	    s->sm_rom.rom_rows, s->sm_rom.rom_cols, s->sm_rom.rom_banks);
    581       1.1  pgoyette }
    582       1.1  pgoyette 
    583       1.1  pgoyette static void
    584       1.1  pgoyette decode_sdram(const struct sysctlnode *node, device_t self, struct spdmem *s,
    585  1.10.4.3     skrll 	     int spd_len)
    586  1.10.4.3     skrll {
    587       1.1  pgoyette 	int dimm_size, cycle_time, bits, tAA, i, speed, freq;
    588       1.1  pgoyette 
    589       1.8     soren 	aprint_naive("\n");
    590       1.8     soren 	aprint_normal("\n");
    591       1.8     soren 	aprint_normal_dev(self, "%s", spdmem_basic_types[s->sm_type]);
    592       1.8     soren 
    593       1.1  pgoyette 	aprint_normal("%s, %s, ",
    594       1.1  pgoyette 		(s->sm_sdr.sdr_mod_attrs & SPDMEM_SDR_MASK_REG)?
    595       1.1  pgoyette 			" (registered)":"",
    596       1.1  pgoyette 		(s->sm_config < __arraycount(spdmem_parity_types))?
    597       1.1  pgoyette 			spdmem_parity_types[s->sm_config]:"invalid parity");
    598       1.1  pgoyette 
    599       1.1  pgoyette 	dimm_size = 1 << (s->sm_sdr.sdr_rows + s->sm_sdr.sdr_cols - 17);
    600       1.1  pgoyette 	dimm_size *= s->sm_sdr.sdr_banks * s->sm_sdr.sdr_banks_per_chip;
    601       1.1  pgoyette 
    602       1.1  pgoyette 	cycle_time = s->sm_sdr.sdr_cycle_whole * 1000 +
    603       1.1  pgoyette 		     s->sm_sdr.sdr_cycle_tenths * 100;
    604       1.1  pgoyette 	bits = le16toh(s->sm_sdr.sdr_datawidth);
    605       1.1  pgoyette 	if (s->sm_config == 1 || s->sm_config == 2)
    606       1.1  pgoyette 		bits -= 8;
    607       1.1  pgoyette 
    608       1.1  pgoyette 	/* Calculate speed here - from OpenBSD */
    609       1.1  pgoyette 	if (spd_len >= 128)
    610       1.1  pgoyette 		freq = ((uint8_t *)s)[126];
    611       1.1  pgoyette 	else
    612       1.1  pgoyette 		freq = 0;
    613       1.1  pgoyette 	switch (freq) {
    614       1.1  pgoyette 		/*
    615  1.10.4.2     skrll 		 * Must check cycle time since some PC-133 DIMMs
    616       1.1  pgoyette 		 * actually report PC-100
    617       1.1  pgoyette 		 */
    618       1.1  pgoyette 	    case 100:
    619       1.1  pgoyette 	    case 133:
    620       1.1  pgoyette 		if (cycle_time < 8000)
    621       1.1  pgoyette 			speed = 133;
    622       1.1  pgoyette 		else
    623       1.1  pgoyette 			speed = 100;
    624       1.1  pgoyette 		break;
    625       1.1  pgoyette 	    case 0x66:		/* Legacy DIMMs use _hex_ 66! */
    626       1.1  pgoyette 	    default:
    627       1.1  pgoyette 		speed = 66;
    628       1.1  pgoyette 	}
    629       1.3  pgoyette 	decode_size_speed(self, node, dimm_size, cycle_time, 1, bits, FALSE,
    630       1.3  pgoyette 			  "PC", speed);
    631       1.1  pgoyette 
    632       1.1  pgoyette 	aprint_verbose_dev(self,
    633       1.1  pgoyette 	    "%d rows, %d cols, %d banks, %d banks/chip, %d.%dns cycle time\n",
    634       1.1  pgoyette 	    s->sm_sdr.sdr_rows, s->sm_sdr.sdr_cols, s->sm_sdr.sdr_banks,
    635       1.1  pgoyette 	    s->sm_sdr.sdr_banks_per_chip, cycle_time/1000,
    636       1.1  pgoyette 	    (cycle_time % 1000) / 100);
    637       1.1  pgoyette 
    638       1.1  pgoyette 	tAA  = 0;
    639       1.1  pgoyette 	for (i = 0; i < 8; i++)
    640       1.1  pgoyette 		if (s->sm_sdr.sdr_tCAS & (1 << i))
    641       1.1  pgoyette 			tAA = i;
    642       1.1  pgoyette 	tAA++;
    643       1.4  christos 	aprint_verbose_dev(self, LATENCY, tAA, s->sm_sdr.sdr_tRCD,
    644       1.1  pgoyette 	    s->sm_sdr.sdr_tRP, s->sm_sdr.sdr_tRAS);
    645       1.1  pgoyette 
    646       1.1  pgoyette 	decode_voltage_refresh(self, s);
    647       1.1  pgoyette }
    648       1.1  pgoyette 
    649       1.1  pgoyette static void
    650  1.10.4.3     skrll decode_ddr(const struct sysctlnode *node, device_t self, struct spdmem *s)
    651  1.10.4.3     skrll {
    652       1.1  pgoyette 	int dimm_size, cycle_time, bits, tAA, i;
    653       1.1  pgoyette 
    654       1.8     soren 	aprint_naive("\n");
    655       1.8     soren 	aprint_normal("\n");
    656       1.8     soren 	aprint_normal_dev(self, "%s", spdmem_basic_types[s->sm_type]);
    657       1.8     soren 
    658       1.1  pgoyette 	aprint_normal("%s, %s, ",
    659       1.1  pgoyette 		(s->sm_ddr.ddr_mod_attrs & SPDMEM_DDR_MASK_REG)?
    660       1.1  pgoyette 			" (registered)":"",
    661       1.1  pgoyette 		(s->sm_config < __arraycount(spdmem_parity_types))?
    662       1.1  pgoyette 			spdmem_parity_types[s->sm_config]:"invalid parity");
    663       1.1  pgoyette 
    664       1.1  pgoyette 	dimm_size = 1 << (s->sm_ddr.ddr_rows + s->sm_ddr.ddr_cols - 17);
    665       1.1  pgoyette 	dimm_size *= s->sm_ddr.ddr_ranks * s->sm_ddr.ddr_banks_per_chip;
    666       1.1  pgoyette 
    667       1.1  pgoyette 	cycle_time = s->sm_ddr.ddr_cycle_whole * 1000 +
    668       1.1  pgoyette 		  spdmem_cycle_frac[s->sm_ddr.ddr_cycle_tenths];
    669       1.1  pgoyette 	bits = le16toh(s->sm_ddr.ddr_datawidth);
    670       1.1  pgoyette 	if (s->sm_config == 1 || s->sm_config == 2)
    671       1.1  pgoyette 		bits -= 8;
    672       1.3  pgoyette 	decode_size_speed(self, node, dimm_size, cycle_time, 2, bits, TRUE,
    673       1.3  pgoyette 			  "PC", 0);
    674       1.1  pgoyette 
    675       1.1  pgoyette 	aprint_verbose_dev(self,
    676       1.1  pgoyette 	    "%d rows, %d cols, %d ranks, %d banks/chip, %d.%dns cycle time\n",
    677       1.1  pgoyette 	    s->sm_ddr.ddr_rows, s->sm_ddr.ddr_cols, s->sm_ddr.ddr_ranks,
    678       1.1  pgoyette 	    s->sm_ddr.ddr_banks_per_chip, cycle_time/1000,
    679       1.1  pgoyette 	    (cycle_time % 1000 + 50) / 100);
    680       1.1  pgoyette 
    681       1.1  pgoyette 	tAA  = 0;
    682       1.1  pgoyette 	for (i = 2; i < 8; i++)
    683       1.1  pgoyette 		if (s->sm_ddr.ddr_tCAS & (1 << i))
    684       1.1  pgoyette 			tAA = i;
    685       1.1  pgoyette 	tAA /= 2;
    686       1.1  pgoyette 
    687       1.1  pgoyette #define __DDR_ROUND(scale, field)	\
    688       1.1  pgoyette 		((scale * s->sm_ddr.field + cycle_time - 1) / cycle_time)
    689       1.1  pgoyette 
    690       1.4  christos 	aprint_verbose_dev(self, LATENCY, tAA, __DDR_ROUND(250, ddr_tRCD),
    691       1.1  pgoyette 		__DDR_ROUND(250, ddr_tRP), __DDR_ROUND(1000, ddr_tRAS));
    692       1.1  pgoyette 
    693       1.1  pgoyette #undef	__DDR_ROUND
    694       1.1  pgoyette 
    695       1.1  pgoyette 	decode_voltage_refresh(self, s);
    696       1.1  pgoyette }
    697       1.1  pgoyette 
    698       1.1  pgoyette static void
    699  1.10.4.3     skrll decode_ddr2(const struct sysctlnode *node, device_t self, struct spdmem *s)
    700  1.10.4.3     skrll {
    701       1.1  pgoyette 	int dimm_size, cycle_time, bits, tAA, i;
    702       1.1  pgoyette 
    703       1.8     soren 	aprint_naive("\n");
    704       1.8     soren 	aprint_normal("\n");
    705       1.8     soren 	aprint_normal_dev(self, "%s", spdmem_basic_types[s->sm_type]);
    706       1.8     soren 
    707       1.1  pgoyette 	aprint_normal("%s, %s, ",
    708       1.1  pgoyette 		(s->sm_ddr2.ddr2_mod_attrs & SPDMEM_DDR2_MASK_REG)?
    709       1.1  pgoyette 			" (registered)":"",
    710       1.1  pgoyette 		(s->sm_config < __arraycount(spdmem_parity_types))?
    711       1.1  pgoyette 			spdmem_parity_types[s->sm_config]:"invalid parity");
    712       1.1  pgoyette 
    713       1.1  pgoyette 	dimm_size = 1 << (s->sm_ddr2.ddr2_rows + s->sm_ddr2.ddr2_cols - 17);
    714       1.1  pgoyette 	dimm_size *= (s->sm_ddr2.ddr2_ranks + 1) *
    715       1.1  pgoyette 		     s->sm_ddr2.ddr2_banks_per_chip;
    716       1.1  pgoyette 
    717       1.1  pgoyette 	cycle_time = s->sm_ddr2.ddr2_cycle_whole * 1000 +
    718       1.1  pgoyette 		 spdmem_cycle_frac[s->sm_ddr2.ddr2_cycle_frac];
    719       1.1  pgoyette 	bits = s->sm_ddr2.ddr2_datawidth;
    720       1.1  pgoyette 	if ((s->sm_config & 0x03) != 0)
    721       1.1  pgoyette 		bits -= 8;
    722       1.3  pgoyette 	decode_size_speed(self, node, dimm_size, cycle_time, 2, bits, TRUE,
    723       1.3  pgoyette 			  "PC2", 0);
    724       1.1  pgoyette 
    725       1.1  pgoyette 	aprint_verbose_dev(self,
    726       1.1  pgoyette 	    "%d rows, %d cols, %d ranks, %d banks/chip, %d.%02dns cycle time\n",
    727       1.1  pgoyette 	    s->sm_ddr2.ddr2_rows, s->sm_ddr2.ddr2_cols,
    728       1.1  pgoyette 	    s->sm_ddr2.ddr2_ranks + 1, s->sm_ddr2.ddr2_banks_per_chip,
    729       1.1  pgoyette 	    cycle_time / 1000, (cycle_time % 1000 + 5) /10 );
    730       1.1  pgoyette 
    731       1.1  pgoyette 	tAA  = 0;
    732       1.1  pgoyette 	for (i = 2; i < 8; i++)
    733       1.1  pgoyette 		if (s->sm_ddr2.ddr2_tCAS & (1 << i))
    734       1.1  pgoyette 			tAA = i;
    735       1.1  pgoyette 
    736       1.1  pgoyette #define __DDR2_ROUND(scale, field)	\
    737       1.1  pgoyette 		((scale * s->sm_ddr2.field + cycle_time - 1) / cycle_time)
    738       1.1  pgoyette 
    739       1.4  christos 	aprint_verbose_dev(self, LATENCY, tAA, __DDR2_ROUND(250, ddr2_tRCD),
    740       1.1  pgoyette 		__DDR2_ROUND(250, ddr2_tRP), __DDR2_ROUND(1000, ddr2_tRAS));
    741       1.1  pgoyette 
    742       1.1  pgoyette #undef	__DDR_ROUND
    743       1.1  pgoyette 
    744       1.1  pgoyette 	decode_voltage_refresh(self, s);
    745       1.1  pgoyette }
    746       1.1  pgoyette 
    747       1.1  pgoyette static void
    748  1.10.4.3     skrll decode_ddr3(const struct sysctlnode *node, device_t self, struct spdmem *s)
    749  1.10.4.3     skrll {
    750       1.1  pgoyette 	int dimm_size, cycle_time, bits;
    751       1.1  pgoyette 
    752       1.8     soren 	aprint_naive("\n");
    753       1.8     soren 	aprint_normal(": %18s\n", s->sm_ddr3.ddr3_part);
    754       1.8     soren 	aprint_normal_dev(self, "%s", spdmem_basic_types[s->sm_type]);
    755       1.8     soren 
    756       1.1  pgoyette 	if (s->sm_ddr3.ddr3_mod_type ==
    757       1.1  pgoyette 		SPDMEM_DDR3_TYPE_MINI_RDIMM ||
    758       1.1  pgoyette 	    s->sm_ddr3.ddr3_mod_type == SPDMEM_DDR3_TYPE_RDIMM)
    759       1.1  pgoyette 		aprint_normal(" (registered)");
    760       1.1  pgoyette 	aprint_normal(", %sECC, %stemp-sensor, ",
    761       1.1  pgoyette 		(s->sm_ddr3.ddr3_hasECC)?"":"no ",
    762       1.1  pgoyette 		(s->sm_ddr3.ddr3_has_therm_sensor)?"":"no ");
    763       1.1  pgoyette 
    764       1.1  pgoyette 	/*
    765       1.1  pgoyette 	 * DDR3 size specification is quite different from others
    766       1.1  pgoyette 	 *
    767       1.1  pgoyette 	 * Module capacity is defined as
    768       1.1  pgoyette 	 *	Chip_Capacity_in_bits / 8bits-per-byte *
    769       1.1  pgoyette 	 *	external_bus_width / internal_bus_width
    770       1.1  pgoyette 	 * We further divide by 2**20 to get our answer in MB
    771       1.1  pgoyette 	 */
    772       1.1  pgoyette 	dimm_size = (s->sm_ddr3.ddr3_chipsize + 28 - 20) - 3 +
    773       1.1  pgoyette 		    (s->sm_ddr3.ddr3_datawidth + 3) -
    774       1.1  pgoyette 		    (s->sm_ddr3.ddr3_chipwidth + 2);
    775       1.1  pgoyette 	dimm_size = (1 << dimm_size) * (s->sm_ddr3.ddr3_physbanks + 1);
    776       1.1  pgoyette 
    777  1.10.4.2     skrll 	cycle_time = (1000 * s->sm_ddr3.ddr3_mtb_dividend +
    778       1.1  pgoyette 			    (s->sm_ddr3.ddr3_mtb_divisor / 2)) /
    779       1.1  pgoyette 		     s->sm_ddr3.ddr3_mtb_divisor;
    780       1.1  pgoyette 	cycle_time *= s->sm_ddr3.ddr3_tCKmin;
    781       1.1  pgoyette 	bits = 1 << (s->sm_ddr3.ddr3_datawidth + 3);
    782       1.3  pgoyette 	decode_size_speed(self, node, dimm_size, cycle_time, 2, bits, FALSE,
    783       1.3  pgoyette 			  "PC3", 0);
    784       1.1  pgoyette 
    785       1.1  pgoyette 	aprint_verbose_dev(self,
    786       1.1  pgoyette 	    "%d rows, %d cols, %d log. banks, %d phys. banks, "
    787       1.1  pgoyette 	    "%d.%03dns cycle time\n",
    788       1.1  pgoyette 	    s->sm_ddr3.ddr3_rows + 9, s->sm_ddr3.ddr3_cols + 12,
    789       1.1  pgoyette 	    1 << (s->sm_ddr3.ddr3_logbanks + 3),
    790       1.1  pgoyette 	    s->sm_ddr3.ddr3_physbanks + 1,
    791       1.1  pgoyette 	    cycle_time/1000, cycle_time % 1000);
    792       1.1  pgoyette 
    793       1.1  pgoyette #define	__DDR3_CYCLES(field) (s->sm_ddr3.field / s->sm_ddr3.ddr3_tCKmin)
    794       1.1  pgoyette 
    795       1.4  christos 	aprint_verbose_dev(self, LATENCY, __DDR3_CYCLES(ddr3_tAAmin),
    796  1.10.4.2     skrll 		__DDR3_CYCLES(ddr3_tRCDmin), __DDR3_CYCLES(ddr3_tRPmin),
    797       1.1  pgoyette 		(s->sm_ddr3.ddr3_tRAS_msb * 256 + s->sm_ddr3.ddr3_tRAS_lsb) /
    798       1.1  pgoyette 		    s->sm_ddr3.ddr3_tCKmin);
    799       1.1  pgoyette 
    800       1.1  pgoyette #undef	__DDR3_CYCLES
    801  1.10.4.2     skrll 
    802  1.10.4.2     skrll 	/* For DDR3, Voltage is written in another area */
    803  1.10.4.2     skrll 	if (!s->sm_ddr3.ddr3_NOT15V || s->sm_ddr3.ddr3_135V
    804  1.10.4.2     skrll 	    || s->sm_ddr3.ddr3_125V) {
    805  1.10.4.2     skrll 		aprint_verbose("%s:", device_xname(self));
    806  1.10.4.2     skrll 		if (!s->sm_ddr3.ddr3_NOT15V)
    807  1.10.4.2     skrll 			aprint_verbose(" 1.5V");
    808  1.10.4.2     skrll 		if (s->sm_ddr3.ddr3_135V)
    809  1.10.4.2     skrll 			aprint_verbose(" 1.35V");
    810  1.10.4.2     skrll 		if (s->sm_ddr3.ddr3_125V)
    811  1.10.4.2     skrll 			aprint_verbose(" 1.25V");
    812  1.10.4.2     skrll 		aprint_verbose(" operable\n");
    813  1.10.4.2     skrll 	}
    814       1.1  pgoyette }
    815       1.1  pgoyette 
    816       1.1  pgoyette static void
    817  1.10.4.3     skrll decode_fbdimm(const struct sysctlnode *node, device_t self, struct spdmem *s)
    818  1.10.4.3     skrll {
    819       1.1  pgoyette 	int dimm_size, cycle_time, bits;
    820       1.1  pgoyette 
    821       1.8     soren 	aprint_naive("\n");
    822       1.8     soren 	aprint_normal("\n");
    823       1.8     soren 	aprint_normal_dev(self, "%s", spdmem_basic_types[s->sm_type]);
    824       1.8     soren 
    825       1.1  pgoyette 	/*
    826       1.1  pgoyette 	 * FB-DIMM module size calculation is very much like DDR3
    827       1.1  pgoyette 	 */
    828       1.1  pgoyette 	dimm_size = s->sm_fbd.fbdimm_rows + 12 +
    829       1.1  pgoyette 		    s->sm_fbd.fbdimm_cols +  9 - 20 - 3;
    830       1.1  pgoyette 	dimm_size = (1 << dimm_size) * (1 << (s->sm_fbd.fbdimm_banks + 2));
    831       1.1  pgoyette 
    832       1.1  pgoyette 	cycle_time = (1000 * s->sm_fbd.fbdimm_mtb_dividend +
    833       1.1  pgoyette 			    (s->sm_fbd.fbdimm_mtb_divisor / 2)) /
    834       1.1  pgoyette 		     s->sm_fbd.fbdimm_mtb_divisor;
    835       1.1  pgoyette 	bits = 1 << (s->sm_fbd.fbdimm_dev_width + 2);
    836       1.3  pgoyette 	decode_size_speed(self, node, dimm_size, cycle_time, 2, bits, TRUE,
    837       1.3  pgoyette 			  "PC2", 0);
    838       1.1  pgoyette 
    839       1.1  pgoyette 	aprint_verbose_dev(self,
    840       1.1  pgoyette 	    "%d rows, %d cols, %d banks, %d.%02dns cycle time\n",
    841       1.1  pgoyette 	    s->sm_fbd.fbdimm_rows, s->sm_fbd.fbdimm_cols,
    842       1.1  pgoyette 	    1 << (s->sm_fbd.fbdimm_banks + 2),
    843       1.1  pgoyette 	    cycle_time / 1000, (cycle_time % 1000 + 5) /10 );
    844       1.1  pgoyette 
    845       1.1  pgoyette #define	__FBDIMM_CYCLES(field) (s->sm_fbd.field / s->sm_fbd.fbdimm_tCKmin)
    846       1.1  pgoyette 
    847       1.4  christos 	aprint_verbose_dev(self, LATENCY, __FBDIMM_CYCLES(fbdimm_tAAmin),
    848  1.10.4.3     skrll 		__FBDIMM_CYCLES(fbdimm_tRCDmin), __FBDIMM_CYCLES(fbdimm_tRPmin),
    849  1.10.4.3     skrll 		(s->sm_fbd.fbdimm_tRAS_msb * 256 + s->sm_fbd.fbdimm_tRAS_lsb) /
    850       1.1  pgoyette 		    s->sm_fbd.fbdimm_tCKmin);
    851       1.1  pgoyette 
    852       1.1  pgoyette #undef	__FBDIMM_CYCLES
    853       1.1  pgoyette 
    854       1.1  pgoyette 	decode_voltage_refresh(self, s);
    855       1.1  pgoyette }
    856  1.10.4.2     skrll 
    857  1.10.4.2     skrll static void
    858  1.10.4.3     skrll decode_ddr4(const struct sysctlnode *node, device_t self, struct spdmem *s)
    859  1.10.4.3     skrll {
    860  1.10.4.2     skrll 	int dimm_size, cycle_time;
    861  1.10.4.2     skrll 	int tAA_clocks, tRCD_clocks,tRP_clocks, tRAS_clocks;
    862  1.10.4.2     skrll 
    863  1.10.4.2     skrll 	aprint_naive("\n");
    864  1.10.4.2     skrll 	aprint_normal(": %20s\n", s->sm_ddr4.ddr4_part_number);
    865  1.10.4.2     skrll 	aprint_normal_dev(self, "%s", spdmem_basic_types[s->sm_type]);
    866  1.10.4.2     skrll 	if (s->sm_ddr4.ddr4_mod_type < __arraycount(spdmem_ddr4_module_types))
    867  1.10.4.2     skrll 		aprint_normal(" (%s)",
    868  1.10.4.2     skrll 		    spdmem_ddr4_module_types[s->sm_ddr4.ddr4_mod_type]);
    869  1.10.4.2     skrll 	aprint_normal(", %stemp-sensor, ",
    870  1.10.4.2     skrll 		(s->sm_ddr4.ddr4_has_therm_sensor)?"":"no ");
    871  1.10.4.2     skrll 
    872  1.10.4.2     skrll 	/*
    873  1.10.4.2     skrll 	 * DDR4 size calculation from JEDEC spec
    874  1.10.4.2     skrll 	 *
    875  1.10.4.2     skrll 	 * Module capacity in bytes is defined as
    876  1.10.4.2     skrll 	 *	Chip_Capacity_in_bits / 8bits-per-byte *
    877  1.10.4.2     skrll 	 *	primary_bus_width / DRAM_width *
    878  1.10.4.2     skrll 	 *	logical_ranks_per_DIMM
    879  1.10.4.2     skrll 	 *
    880  1.10.4.2     skrll 	 * logical_ranks_per DIMM equals package_ranks, but multiply
    881  1.10.4.2     skrll 	 * by diecount for 3DS packages
    882  1.10.4.2     skrll 	 *
    883  1.10.4.2     skrll 	 * We further divide by 2**20 to get our answer in MB
    884  1.10.4.2     skrll 	 */
    885  1.10.4.2     skrll 	dimm_size = (s->sm_ddr4.ddr4_capacity + 28)	/* chip_capacity */
    886  1.10.4.2     skrll 		     - 20				/* convert to MB */
    887  1.10.4.2     skrll 		     - 3				/* bits --> bytes */
    888  1.10.4.2     skrll 		     + (s->sm_ddr4.ddr4_primary_bus_width + 3); /* bus width */
    889  1.10.4.2     skrll 	switch (s->sm_ddr4.ddr4_device_width) {		/* DRAM width */
    890  1.10.4.2     skrll 	case 0:	dimm_size -= 2;
    891  1.10.4.2     skrll 		break;
    892  1.10.4.2     skrll 	case 1: dimm_size -= 3;
    893  1.10.4.2     skrll 		break;
    894  1.10.4.2     skrll 	case 2:	dimm_size -= 4;
    895  1.10.4.2     skrll 		break;
    896  1.10.4.2     skrll 	case 4: dimm_size -= 5;
    897  1.10.4.2     skrll 		break;
    898  1.10.4.2     skrll 	default:
    899  1.10.4.2     skrll 		dimm_size = -1;		/* flag invalid value */
    900  1.10.4.2     skrll 	}
    901  1.10.4.3     skrll 	if (dimm_size >= 0) {
    902  1.10.4.2     skrll 		dimm_size = (1 << dimm_size) *
    903  1.10.4.2     skrll 		    (s->sm_ddr4.ddr4_package_ranks + 1); /* log.ranks/DIMM */
    904  1.10.4.2     skrll 		if (s->sm_ddr4.ddr4_signal_loading == 2) {
    905  1.10.4.3     skrll 			dimm_size *= (s->sm_ddr4.ddr4_diecount + 1);
    906  1.10.4.2     skrll 		}
    907  1.10.4.2     skrll 	}
    908  1.10.4.2     skrll 
    909  1.10.4.3     skrll #define	__DDR4_VALUE(field) ((s->sm_ddr4.ddr4_##field##_mtb * 125 +	\
    910  1.10.4.3     skrll 			     s->sm_ddr4.ddr4_##field##_ftb) - 		\
    911  1.10.4.3     skrll 			    ((s->sm_ddr4.ddr4_##field##_ftb > 127)?256:0))
    912  1.10.4.2     skrll 	/*
    913  1.10.4.2     skrll 	 * For now, the only value for mtb is 1 = 125ps, and ftp = 1ps
    914  1.10.4.2     skrll 	 * so we don't need to figure out the time-base units - just
    915  1.10.4.2     skrll 	 * hard-code them for now.
    916  1.10.4.2     skrll 	 */
    917  1.10.4.3     skrll 	cycle_time = __DDR4_VALUE(tCKAVGmin);
    918  1.10.4.2     skrll 	decode_size_speed(self, node, dimm_size, cycle_time, 2,
    919  1.10.4.3     skrll 			  1 << (s->sm_ddr4.ddr4_primary_bus_width + 3),
    920  1.10.4.2     skrll 			  TRUE, "PC4", 0);
    921  1.10.4.2     skrll 
    922  1.10.4.2     skrll 	aprint_verbose_dev(self,
    923  1.10.4.3     skrll 	    "%d rows, %d cols, %d banks, %d bank groups, "
    924  1.10.4.3     skrll 	    "%d.%03dns cycle time\n",
    925  1.10.4.3     skrll 	    s->sm_ddr4.ddr4_rows + 9, s->sm_ddr4.ddr4_cols + 12,
    926  1.10.4.2     skrll 	    1 << (2 + s->sm_ddr4.ddr4_logbanks),
    927  1.10.4.3     skrll 	    1 << s->sm_ddr4.ddr4_bankgroups,
    928  1.10.4.3     skrll 	    cycle_time / 1000, cycle_time % 1000);
    929  1.10.4.2     skrll 
    930  1.10.4.2     skrll /*
    931  1.10.4.2     skrll  * Note that the ddr4_xxx_ftb fields are actually signed offsets from
    932  1.10.4.2     skrll  * the corresponding mtb value, so we might have to subtract 256!
    933  1.10.4.2     skrll  */
    934  1.10.4.2     skrll 
    935  1.10.4.3     skrll 	tAA_clocks =  __DDR4_VALUE(tAAmin)  * 1000 / cycle_time;
    936  1.10.4.3     skrll 	tRCD_clocks = __DDR4_VALUE(tRCDmin) * 1000 / cycle_time;
    937  1.10.4.3     skrll 	tRP_clocks =  __DDR4_VALUE(tRPmin)  * 1000 / cycle_time;
    938  1.10.4.2     skrll 	tRAS_clocks = (s->sm_ddr4.ddr4_tRASmin_msb * 256 +
    939  1.10.4.2     skrll 		       s->sm_ddr4.ddr4_tRASmin_lsb) * 125 * 1000 / cycle_time;
    940  1.10.4.2     skrll 
    941  1.10.4.2     skrll /*
    942  1.10.4.2     skrll  * Per JEDEC spec, rounding is done by taking the time value, dividing
    943  1.10.4.2     skrll  * by the cycle time, subtracting .010 from the result, and then
    944  1.10.4.2     skrll  * rounded up to the nearest integer.  Unfortunately, none of their
    945  1.10.4.2     skrll  * examples say what to do when the result of the subtraction is already
    946  1.10.4.2     skrll  * an integer.  For now, assume that we still round up (so an interval
    947  1.10.4.2     skrll  * of exactly 12.010 clock cycles will be printed as 13).
    948  1.10.4.2     skrll  */
    949  1.10.4.2     skrll #define	__DDR4_ROUND(value) ((value - 10) / 1000 + 1)
    950  1.10.4.2     skrll 
    951  1.10.4.2     skrll 	aprint_verbose_dev(self, LATENCY, __DDR4_ROUND(tAA_clocks),
    952  1.10.4.2     skrll 			   __DDR4_ROUND(tRCD_clocks),
    953  1.10.4.3     skrll 			   __DDR4_ROUND(tRP_clocks),
    954  1.10.4.2     skrll 			   __DDR4_ROUND(tRAS_clocks));
    955  1.10.4.2     skrll 
    956  1.10.4.2     skrll #undef	__DDR4_VALUE
    957  1.10.4.2     skrll #undef	__DDR4_ROUND
    958  1.10.4.2     skrll }
    959