1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
234e026f9SYork Sun /*
3c0c32af0SYork Sun * Copyright 2014-2016 Freescale Semiconductor, Inc.
4c0c32af0SYork Sun * Copyright 2017-2018 NXP Semiconductor
5c0c32af0SYork Sun *
634e026f9SYork Sun * calculate the organization and timing parameter
734e026f9SYork Sun * from ddr3 spd, please refer to the spec
834e026f9SYork Sun * JEDEC standard No.21-C 4_01_02_12R23A.pdf
934e026f9SYork Sun *
1034e026f9SYork Sun *
1134e026f9SYork Sun */
1234e026f9SYork Sun
1334e026f9SYork Sun #include <common.h>
1434e026f9SYork Sun #include <fsl_ddr_sdram.h>
1534e026f9SYork Sun
1634e026f9SYork Sun #include <fsl_ddr.h>
1734e026f9SYork Sun
1834e026f9SYork Sun /*
1934e026f9SYork Sun * Calculate the Density of each Physical Rank.
2034e026f9SYork Sun * Returned size is in bytes.
2134e026f9SYork Sun *
2234e026f9SYork Sun * Total DIMM size =
2334e026f9SYork Sun * sdram capacity(bit) / 8 * primary bus width / sdram width
2434e026f9SYork Sun * * Logical Ranks per DIMM
2534e026f9SYork Sun *
2634e026f9SYork Sun * where: sdram capacity = spd byte4[3:0]
2734e026f9SYork Sun * primary bus width = spd byte13[2:0]
2834e026f9SYork Sun * sdram width = spd byte12[2:0]
2934e026f9SYork Sun * Logical Ranks per DIMM = spd byte12[5:3] for SDP, DDP, QDP
3034e026f9SYork Sun * spd byte12{5:3] * spd byte6[6:4] for 3DS
3134e026f9SYork Sun *
3234e026f9SYork Sun * To simplify each rank size = total DIMM size / Number of Package Ranks
3334e026f9SYork Sun * where Number of Package Ranks = spd byte12[5:3]
3434e026f9SYork Sun *
3534e026f9SYork Sun * SPD byte4 - sdram density and banks
3634e026f9SYork Sun * bit[3:0] size(bit) size(byte)
3734e026f9SYork Sun * 0000 256Mb 32MB
3834e026f9SYork Sun * 0001 512Mb 64MB
3934e026f9SYork Sun * 0010 1Gb 128MB
4034e026f9SYork Sun * 0011 2Gb 256MB
4134e026f9SYork Sun * 0100 4Gb 512MB
4234e026f9SYork Sun * 0101 8Gb 1GB
4334e026f9SYork Sun * 0110 16Gb 2GB
4434e026f9SYork Sun * 0111 32Gb 4GB
4534e026f9SYork Sun *
4634e026f9SYork Sun * SPD byte13 - module memory bus width
4734e026f9SYork Sun * bit[2:0] primary bus width
4834e026f9SYork Sun * 000 8bits
4934e026f9SYork Sun * 001 16bits
5034e026f9SYork Sun * 010 32bits
5134e026f9SYork Sun * 011 64bits
5234e026f9SYork Sun *
5334e026f9SYork Sun * SPD byte12 - module organization
5434e026f9SYork Sun * bit[2:0] sdram device width
5534e026f9SYork Sun * 000 4bits
5634e026f9SYork Sun * 001 8bits
5734e026f9SYork Sun * 010 16bits
5834e026f9SYork Sun * 011 32bits
5934e026f9SYork Sun *
6034e026f9SYork Sun * SPD byte12 - module organization
6134e026f9SYork Sun * bit[5:3] number of package ranks per DIMM
6234e026f9SYork Sun * 000 1
6334e026f9SYork Sun * 001 2
6434e026f9SYork Sun * 010 3
6534e026f9SYork Sun * 011 4
6634e026f9SYork Sun *
6734e026f9SYork Sun * SPD byte6 - SDRAM package type
6834e026f9SYork Sun * bit[6:4] Die count
6934e026f9SYork Sun * 000 1
7034e026f9SYork Sun * 001 2
7134e026f9SYork Sun * 010 3
7234e026f9SYork Sun * 011 4
7334e026f9SYork Sun * 100 5
7434e026f9SYork Sun * 101 6
7534e026f9SYork Sun * 110 7
7634e026f9SYork Sun * 111 8
7734e026f9SYork Sun *
7834e026f9SYork Sun * SPD byte6 - SRAM package type
7934e026f9SYork Sun * bit[1:0] Signal loading
8034e026f9SYork Sun * 00 Not specified
8134e026f9SYork Sun * 01 Multi load stack
8234e026f9SYork Sun * 10 Sigle load stack (3DS)
8334e026f9SYork Sun * 11 Reserved
8434e026f9SYork Sun */
8534e026f9SYork Sun static unsigned long long
compute_ranksize(const struct ddr4_spd_eeprom_s * spd)8634e026f9SYork Sun compute_ranksize(const struct ddr4_spd_eeprom_s *spd)
8734e026f9SYork Sun {
8834e026f9SYork Sun unsigned long long bsize;
8934e026f9SYork Sun
9034e026f9SYork Sun int nbit_sdram_cap_bsize = 0;
9134e026f9SYork Sun int nbit_primary_bus_width = 0;
9234e026f9SYork Sun int nbit_sdram_width = 0;
9334e026f9SYork Sun int die_count = 0;
9434e026f9SYork Sun bool package_3ds;
9534e026f9SYork Sun
9634e026f9SYork Sun if ((spd->density_banks & 0xf) <= 7)
9734e026f9SYork Sun nbit_sdram_cap_bsize = (spd->density_banks & 0xf) + 28;
9834e026f9SYork Sun if ((spd->bus_width & 0x7) < 4)
9934e026f9SYork Sun nbit_primary_bus_width = (spd->bus_width & 0x7) + 3;
10034e026f9SYork Sun if ((spd->organization & 0x7) < 4)
10134e026f9SYork Sun nbit_sdram_width = (spd->organization & 0x7) + 2;
10234e026f9SYork Sun package_3ds = (spd->package_type & 0x3) == 0x2;
103c0c32af0SYork Sun if ((spd->package_type & 0x80) && !package_3ds) { /* other than 3DS */
104c0c32af0SYork Sun printf("Warning: not supported SDRAM package type\n");
105c0c32af0SYork Sun return 0;
106c0c32af0SYork Sun }
10734e026f9SYork Sun if (package_3ds)
10834e026f9SYork Sun die_count = (spd->package_type >> 4) & 0x7;
10934e026f9SYork Sun
11034e026f9SYork Sun bsize = 1ULL << (nbit_sdram_cap_bsize - 3 +
11134e026f9SYork Sun nbit_primary_bus_width - nbit_sdram_width +
11234e026f9SYork Sun die_count);
11334e026f9SYork Sun
114c0c32af0SYork Sun debug("DDR: DDR rank density = 0x%16llx\n", bsize);
11534e026f9SYork Sun
11634e026f9SYork Sun return bsize;
11734e026f9SYork Sun }
11834e026f9SYork Sun
11934e026f9SYork Sun #define spd_to_ps(mtb, ftb) \
12034e026f9SYork Sun (mtb * pdimm->mtb_ps + (ftb * pdimm->ftb_10th_ps) / 10)
12134e026f9SYork Sun /*
1221d71efbbSYork Sun * ddr_compute_dimm_parameters for DDR4 SPD
12334e026f9SYork Sun *
12434e026f9SYork Sun * Compute DIMM parameters based upon the SPD information in spd.
12534e026f9SYork Sun * Writes the results to the dimm_params_t structure pointed by pdimm.
12634e026f9SYork Sun *
12734e026f9SYork Sun */
ddr_compute_dimm_parameters(const unsigned int ctrl_num,const generic_spd_eeprom_t * spd,dimm_params_t * pdimm,unsigned int dimm_number)12803e664d8SYork Sun unsigned int ddr_compute_dimm_parameters(const unsigned int ctrl_num,
12903e664d8SYork Sun const generic_spd_eeprom_t *spd,
13034e026f9SYork Sun dimm_params_t *pdimm,
13134e026f9SYork Sun unsigned int dimm_number)
13234e026f9SYork Sun {
13334e026f9SYork Sun unsigned int retval;
13434e026f9SYork Sun int i;
135789b3447SYork Sun const u8 udimm_rc_e_dq[18] = {
136789b3447SYork Sun 0x0c, 0x2c, 0x15, 0x35, 0x15, 0x35, 0x0b, 0x2c, 0x15,
137789b3447SYork Sun 0x35, 0x0b, 0x35, 0x0b, 0x2c, 0x0b, 0x35, 0x15, 0x36
138789b3447SYork Sun };
139789b3447SYork Sun int spd_error = 0;
140789b3447SYork Sun u8 *ptr;
141564e9383SYork Sun u8 val;
14234e026f9SYork Sun
14334e026f9SYork Sun if (spd->mem_type) {
14434e026f9SYork Sun if (spd->mem_type != SPD_MEMTYPE_DDR4) {
1456b95be22SYork Sun printf("Ctrl %u DIMM %u: is not a DDR4 SPD.\n",
1466b95be22SYork Sun ctrl_num, dimm_number);
14734e026f9SYork Sun return 1;
14834e026f9SYork Sun }
14934e026f9SYork Sun } else {
15034e026f9SYork Sun memset(pdimm, 0, sizeof(dimm_params_t));
15134e026f9SYork Sun return 1;
15234e026f9SYork Sun }
15334e026f9SYork Sun
15434e026f9SYork Sun retval = ddr4_spd_check(spd);
15534e026f9SYork Sun if (retval) {
15634e026f9SYork Sun printf("DIMM %u: failed checksum\n", dimm_number);
15734e026f9SYork Sun return 2;
15834e026f9SYork Sun }
15934e026f9SYork Sun
16034e026f9SYork Sun /*
16134e026f9SYork Sun * The part name in ASCII in the SPD EEPROM is not null terminated.
16234e026f9SYork Sun * Guarantee null termination here by presetting all bytes to 0
16334e026f9SYork Sun * and copying the part name in ASCII from the SPD onto it
16434e026f9SYork Sun */
16534e026f9SYork Sun memset(pdimm->mpart, 0, sizeof(pdimm->mpart));
16634e026f9SYork Sun if ((spd->info_size_crc & 0xF) > 2)
16734e026f9SYork Sun memcpy(pdimm->mpart, spd->mpart, sizeof(pdimm->mpart) - 1);
16834e026f9SYork Sun
16934e026f9SYork Sun /* DIMM organization parameters */
17034e026f9SYork Sun pdimm->n_ranks = ((spd->organization >> 3) & 0x7) + 1;
17134e026f9SYork Sun pdimm->rank_density = compute_ranksize(spd);
17234e026f9SYork Sun pdimm->capacity = pdimm->n_ranks * pdimm->rank_density;
173c0c32af0SYork Sun pdimm->die_density = spd->density_banks & 0xf;
17434e026f9SYork Sun pdimm->primary_sdram_width = 1 << (3 + (spd->bus_width & 0x7));
17534e026f9SYork Sun if ((spd->bus_width >> 3) & 0x3)
17634e026f9SYork Sun pdimm->ec_sdram_width = 8;
17734e026f9SYork Sun else
17834e026f9SYork Sun pdimm->ec_sdram_width = 0;
17934e026f9SYork Sun pdimm->data_width = pdimm->primary_sdram_width
18034e026f9SYork Sun + pdimm->ec_sdram_width;
18134e026f9SYork Sun pdimm->device_width = 1 << ((spd->organization & 0x7) + 2);
182c0c32af0SYork Sun pdimm->package_3ds = (spd->package_type & 0x3) == 0x2 ?
183c0c32af0SYork Sun (spd->package_type >> 4) & 0x7 : 0;
18434e026f9SYork Sun
1851d71efbbSYork Sun /* These are the types defined by the JEDEC SPD spec */
18634e026f9SYork Sun pdimm->mirrored_dimm = 0;
18734e026f9SYork Sun pdimm->registered_dimm = 0;
1881d71efbbSYork Sun switch (spd->module_type & DDR4_SPD_MODULETYPE_MASK) {
1891d71efbbSYork Sun case DDR4_SPD_MODULETYPE_RDIMM:
19034e026f9SYork Sun /* Registered/buffered DIMMs */
19134e026f9SYork Sun pdimm->registered_dimm = 1;
192426230a6SYork Sun if (spd->mod_section.registered.reg_map & 0x1)
193426230a6SYork Sun pdimm->mirrored_dimm = 1;
194564e9383SYork Sun val = spd->mod_section.registered.ca_stren;
195564e9383SYork Sun pdimm->rcw[3] = val >> 4;
196564e9383SYork Sun pdimm->rcw[4] = ((val & 0x3) << 2) | ((val & 0xc) >> 2);
197564e9383SYork Sun val = spd->mod_section.registered.clk_stren;
198564e9383SYork Sun pdimm->rcw[5] = ((val & 0x3) << 2) | ((val & 0xc) >> 2);
199564e9383SYork Sun /* Not all in SPD. For convience only. Boards may overwrite. */
200564e9383SYork Sun pdimm->rcw[6] = 0xf;
201564e9383SYork Sun /*
202564e9383SYork Sun * A17 only used for 16Gb and above devices.
203564e9383SYork Sun * C[2:0] only used for 3DS.
204564e9383SYork Sun */
205564e9383SYork Sun pdimm->rcw[8] = pdimm->die_density >= 0x6 ? 0x0 : 0x8 |
206564e9383SYork Sun (pdimm->package_3ds > 0x3 ? 0x0 :
207564e9383SYork Sun (pdimm->package_3ds > 0x1 ? 0x1 :
208564e9383SYork Sun (pdimm->package_3ds > 0 ? 0x2 : 0x3)));
209564e9383SYork Sun if (pdimm->package_3ds || pdimm->n_ranks != 4)
210564e9383SYork Sun pdimm->rcw[13] = 0xc;
211564e9383SYork Sun else
212564e9383SYork Sun pdimm->rcw[13] = 0xd; /* Fix encoded by board */
213564e9383SYork Sun
21434e026f9SYork Sun break;
21534e026f9SYork Sun
2161d71efbbSYork Sun case DDR4_SPD_MODULETYPE_UDIMM:
2171d71efbbSYork Sun case DDR4_SPD_MODULETYPE_SO_DIMM:
21834e026f9SYork Sun /* Unbuffered DIMMs */
21934e026f9SYork Sun if (spd->mod_section.unbuffered.addr_mapping & 0x1)
22034e026f9SYork Sun pdimm->mirrored_dimm = 1;
221789b3447SYork Sun if ((spd->mod_section.unbuffered.mod_height & 0xe0) == 0 &&
222789b3447SYork Sun (spd->mod_section.unbuffered.ref_raw_card == 0x04)) {
223789b3447SYork Sun /* Fix SPD error found on DIMMs with raw card E0 */
224789b3447SYork Sun for (i = 0; i < 18; i++) {
225789b3447SYork Sun if (spd->mapping[i] == udimm_rc_e_dq[i])
226789b3447SYork Sun continue;
227789b3447SYork Sun spd_error = 1;
228789b3447SYork Sun debug("SPD byte %d: 0x%x, should be 0x%x\n",
229789b3447SYork Sun 60 + i, spd->mapping[i],
230789b3447SYork Sun udimm_rc_e_dq[i]);
231789b3447SYork Sun ptr = (u8 *)&spd->mapping[i];
232789b3447SYork Sun *ptr = udimm_rc_e_dq[i];
233789b3447SYork Sun }
234789b3447SYork Sun if (spd_error)
235789b3447SYork Sun puts("SPD DQ mapping error fixed\n");
236789b3447SYork Sun }
23734e026f9SYork Sun break;
23834e026f9SYork Sun
23934e026f9SYork Sun default:
24034e026f9SYork Sun printf("unknown module_type 0x%02X\n", spd->module_type);
24134e026f9SYork Sun return 1;
24234e026f9SYork Sun }
24334e026f9SYork Sun
24434e026f9SYork Sun /* SDRAM device parameters */
24534e026f9SYork Sun pdimm->n_row_addr = ((spd->addressing >> 3) & 0x7) + 12;
24634e026f9SYork Sun pdimm->n_col_addr = (spd->addressing & 0x7) + 9;
24734e026f9SYork Sun pdimm->bank_addr_bits = (spd->density_banks >> 4) & 0x3;
24834e026f9SYork Sun pdimm->bank_group_bits = (spd->density_banks >> 6) & 0x3;
24934e026f9SYork Sun
25034e026f9SYork Sun /*
25134e026f9SYork Sun * The SPD spec has not the ECC bit,
25234e026f9SYork Sun * We consider the DIMM as ECC capability
25334e026f9SYork Sun * when the extension bus exist
25434e026f9SYork Sun */
25534e026f9SYork Sun if (pdimm->ec_sdram_width)
25634e026f9SYork Sun pdimm->edc_config = 0x02;
25734e026f9SYork Sun else
25834e026f9SYork Sun pdimm->edc_config = 0x00;
25934e026f9SYork Sun
26034e026f9SYork Sun /*
26134e026f9SYork Sun * The SPD spec has not the burst length byte
26234e026f9SYork Sun * but DDR4 spec has nature BL8 and BC4,
26334e026f9SYork Sun * BL8 -bit3, BC4 -bit2
26434e026f9SYork Sun */
26534e026f9SYork Sun pdimm->burst_lengths_bitmask = 0x0c;
26634e026f9SYork Sun
26734e026f9SYork Sun /* MTB - medium timebase
26834e026f9SYork Sun * The MTB in the SPD spec is 125ps,
26934e026f9SYork Sun *
27034e026f9SYork Sun * FTB - fine timebase
27134e026f9SYork Sun * use 1/10th of ps as our unit to avoid floating point
27234e026f9SYork Sun * eg, 10 for 1ps, 25 for 2.5ps, 50 for 5ps
27334e026f9SYork Sun */
27434e026f9SYork Sun if ((spd->timebases & 0xf) == 0x0) {
27534e026f9SYork Sun pdimm->mtb_ps = 125;
27634e026f9SYork Sun pdimm->ftb_10th_ps = 10;
27734e026f9SYork Sun
27834e026f9SYork Sun } else {
27934e026f9SYork Sun printf("Unknown Timebases\n");
28034e026f9SYork Sun }
28134e026f9SYork Sun
28234e026f9SYork Sun /* sdram minimum cycle time */
28334e026f9SYork Sun pdimm->tckmin_x_ps = spd_to_ps(spd->tck_min, spd->fine_tck_min);
28434e026f9SYork Sun
28534e026f9SYork Sun /* sdram max cycle time */
28634e026f9SYork Sun pdimm->tckmax_ps = spd_to_ps(spd->tck_max, spd->fine_tck_max);
28734e026f9SYork Sun
28834e026f9SYork Sun /*
28934e026f9SYork Sun * CAS latency supported
29034e026f9SYork Sun * bit0 - CL7
29134e026f9SYork Sun * bit4 - CL11
29234e026f9SYork Sun * bit8 - CL15
29334e026f9SYork Sun * bit12- CL19
29434e026f9SYork Sun * bit16- CL23
29534e026f9SYork Sun */
29634e026f9SYork Sun pdimm->caslat_x = (spd->caslat_b1 << 7) |
29734e026f9SYork Sun (spd->caslat_b2 << 15) |
29834e026f9SYork Sun (spd->caslat_b3 << 23);
29934e026f9SYork Sun
30034e026f9SYork Sun BUG_ON(spd->caslat_b4 != 0);
30134e026f9SYork Sun
30234e026f9SYork Sun /*
30334e026f9SYork Sun * min CAS latency time
30434e026f9SYork Sun */
30534e026f9SYork Sun pdimm->taa_ps = spd_to_ps(spd->taa_min, spd->fine_taa_min);
30634e026f9SYork Sun
30734e026f9SYork Sun /*
30834e026f9SYork Sun * min RAS to CAS delay time
30934e026f9SYork Sun */
31034e026f9SYork Sun pdimm->trcd_ps = spd_to_ps(spd->trcd_min, spd->fine_trcd_min);
31134e026f9SYork Sun
31234e026f9SYork Sun /*
31334e026f9SYork Sun * Min Row Precharge Delay Time
31434e026f9SYork Sun */
31534e026f9SYork Sun pdimm->trp_ps = spd_to_ps(spd->trp_min, spd->fine_trp_min);
31634e026f9SYork Sun
31734e026f9SYork Sun /* min active to precharge delay time */
31834e026f9SYork Sun pdimm->tras_ps = (((spd->tras_trc_ext & 0xf) << 8) +
31934e026f9SYork Sun spd->tras_min_lsb) * pdimm->mtb_ps;
32034e026f9SYork Sun
32134e026f9SYork Sun /* min active to actice/refresh delay time */
32234e026f9SYork Sun pdimm->trc_ps = spd_to_ps((((spd->tras_trc_ext & 0xf0) << 4) +
32334e026f9SYork Sun spd->trc_min_lsb), spd->fine_trc_min);
32434e026f9SYork Sun /* Min Refresh Recovery Delay Time */
32534e026f9SYork Sun pdimm->trfc1_ps = ((spd->trfc1_min_msb << 8) | (spd->trfc1_min_lsb)) *
32634e026f9SYork Sun pdimm->mtb_ps;
32734e026f9SYork Sun pdimm->trfc2_ps = ((spd->trfc2_min_msb << 8) | (spd->trfc2_min_lsb)) *
32834e026f9SYork Sun pdimm->mtb_ps;
32934e026f9SYork Sun pdimm->trfc4_ps = ((spd->trfc4_min_msb << 8) | (spd->trfc4_min_lsb)) *
33034e026f9SYork Sun pdimm->mtb_ps;
33134e026f9SYork Sun /* min four active window delay time */
33234e026f9SYork Sun pdimm->tfaw_ps = (((spd->tfaw_msb & 0xf) << 8) | spd->tfaw_min) *
33334e026f9SYork Sun pdimm->mtb_ps;
33434e026f9SYork Sun
33534e026f9SYork Sun /* min row active to row active delay time, different bank group */
33634e026f9SYork Sun pdimm->trrds_ps = spd_to_ps(spd->trrds_min, spd->fine_trrds_min);
33734e026f9SYork Sun /* min row active to row active delay time, same bank group */
33834e026f9SYork Sun pdimm->trrdl_ps = spd_to_ps(spd->trrdl_min, spd->fine_trrdl_min);
33934e026f9SYork Sun /* min CAS to CAS Delay Time (tCCD_Lmin), same bank group */
34034e026f9SYork Sun pdimm->tccdl_ps = spd_to_ps(spd->tccdl_min, spd->fine_tccdl_min);
34134e026f9SYork Sun
342c0c32af0SYork Sun if (pdimm->package_3ds) {
343c0c32af0SYork Sun if (pdimm->die_density <= 0x4) {
344c0c32af0SYork Sun pdimm->trfc_slr_ps = 260000;
345c0c32af0SYork Sun } else if (pdimm->die_density <= 0x5) {
346c0c32af0SYork Sun pdimm->trfc_slr_ps = 350000;
347c0c32af0SYork Sun } else {
348c0c32af0SYork Sun printf("WARN: Unsupported logical rank density 0x%x\n",
349c0c32af0SYork Sun pdimm->die_density);
350c0c32af0SYork Sun }
351c0c32af0SYork Sun }
352c0c32af0SYork Sun
35334e026f9SYork Sun /*
35434e026f9SYork Sun * Average periodic refresh interval
35534e026f9SYork Sun * tREFI = 7.8 us at normal temperature range
35634e026f9SYork Sun */
35734e026f9SYork Sun pdimm->refresh_rate_ps = 7800000;
35834e026f9SYork Sun
35934e026f9SYork Sun for (i = 0; i < 18; i++)
36034e026f9SYork Sun pdimm->dq_mapping[i] = spd->mapping[i];
36134e026f9SYork Sun
36234e026f9SYork Sun pdimm->dq_mapping_ors = ((spd->mapping[0] >> 6) & 0x3) == 0 ? 1 : 0;
36334e026f9SYork Sun
36434e026f9SYork Sun return 0;
36534e026f9SYork Sun }
366