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