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