xref: /openbmc/u-boot/drivers/ddr/fsl/ddr3_dimm_params.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0
25614e71bSYork Sun /*
35614e71bSYork Sun  * Copyright 2008-2012 Freescale Semiconductor, Inc.
45614e71bSYork Sun  *	Dave Liu <daveliu@freescale.com>
55614e71bSYork Sun  *
65614e71bSYork Sun  * calculate the organization and timing parameter
75614e71bSYork Sun  * from ddr3 spd, please refer to the spec
85614e71bSYork Sun  * JEDEC standard No.21-C 4_01_02_11R18.pdf
95614e71bSYork Sun  */
105614e71bSYork Sun 
115614e71bSYork Sun #include <common.h>
125614e71bSYork Sun #include <fsl_ddr_sdram.h>
135614e71bSYork Sun 
145614e71bSYork Sun #include <fsl_ddr.h>
155614e71bSYork Sun 
165614e71bSYork Sun /*
175614e71bSYork Sun  * Calculate the Density of each Physical Rank.
185614e71bSYork Sun  * Returned size is in bytes.
195614e71bSYork Sun  *
205614e71bSYork Sun  * each rank size =
215614e71bSYork Sun  * sdram capacity(bit) / 8 * primary bus width / sdram width
225614e71bSYork Sun  *
235614e71bSYork Sun  * where: sdram capacity  = spd byte4[3:0]
245614e71bSYork Sun  *        primary bus width = spd byte8[2:0]
255614e71bSYork Sun  *        sdram width = spd byte7[2:0]
265614e71bSYork Sun  *
275614e71bSYork Sun  * SPD byte4 - sdram density and banks
285614e71bSYork Sun  *	bit[3:0]	size(bit)	size(byte)
295614e71bSYork Sun  *	0000		256Mb		32MB
305614e71bSYork Sun  *	0001		512Mb		64MB
315614e71bSYork Sun  *	0010		1Gb		128MB
325614e71bSYork Sun  *	0011		2Gb		256MB
335614e71bSYork Sun  *	0100		4Gb		512MB
345614e71bSYork Sun  *	0101		8Gb		1GB
355614e71bSYork Sun  *	0110		16Gb		2GB
365614e71bSYork Sun  *
375614e71bSYork Sun  * SPD byte8 - module memory bus width
385614e71bSYork Sun  * 	bit[2:0]	primary bus width
395614e71bSYork Sun  *	000		8bits
405614e71bSYork Sun  * 	001		16bits
415614e71bSYork Sun  * 	010		32bits
425614e71bSYork Sun  * 	011		64bits
435614e71bSYork Sun  *
445614e71bSYork Sun  * SPD byte7 - module organiztion
455614e71bSYork Sun  * 	bit[2:0]	sdram device width
465614e71bSYork Sun  * 	000		4bits
475614e71bSYork Sun  * 	001		8bits
485614e71bSYork Sun  * 	010		16bits
495614e71bSYork Sun  * 	011		32bits
505614e71bSYork Sun  *
515614e71bSYork Sun  */
525614e71bSYork Sun static unsigned long long
compute_ranksize(const ddr3_spd_eeprom_t * spd)535614e71bSYork Sun compute_ranksize(const ddr3_spd_eeprom_t *spd)
545614e71bSYork Sun {
555614e71bSYork Sun 	unsigned long long bsize;
565614e71bSYork Sun 
575614e71bSYork Sun 	int nbit_sdram_cap_bsize = 0;
585614e71bSYork Sun 	int nbit_primary_bus_width = 0;
595614e71bSYork Sun 	int nbit_sdram_width = 0;
605614e71bSYork Sun 
615614e71bSYork Sun 	if ((spd->density_banks & 0xf) < 7)
625614e71bSYork Sun 		nbit_sdram_cap_bsize = (spd->density_banks & 0xf) + 28;
635614e71bSYork Sun 	if ((spd->bus_width & 0x7) < 4)
645614e71bSYork Sun 		nbit_primary_bus_width = (spd->bus_width & 0x7) + 3;
655614e71bSYork Sun 	if ((spd->organization & 0x7) < 4)
665614e71bSYork Sun 		nbit_sdram_width = (spd->organization & 0x7) + 2;
675614e71bSYork Sun 
685614e71bSYork Sun 	bsize = 1ULL << (nbit_sdram_cap_bsize - 3
695614e71bSYork Sun 		    + nbit_primary_bus_width - nbit_sdram_width);
705614e71bSYork Sun 
715614e71bSYork Sun 	debug("DDR: DDR III rank density = 0x%16llx\n", bsize);
725614e71bSYork Sun 
735614e71bSYork Sun 	return bsize;
745614e71bSYork Sun }
755614e71bSYork Sun 
765614e71bSYork Sun /*
775614e71bSYork Sun  * ddr_compute_dimm_parameters for DDR3 SPD
785614e71bSYork Sun  *
795614e71bSYork Sun  * Compute DIMM parameters based upon the SPD information in spd.
805614e71bSYork Sun  * Writes the results to the dimm_params_t structure pointed by pdimm.
815614e71bSYork Sun  *
825614e71bSYork Sun  */
ddr_compute_dimm_parameters(const unsigned int ctrl_num,const ddr3_spd_eeprom_t * spd,dimm_params_t * pdimm,unsigned int dimm_number)8303e664d8SYork Sun unsigned int ddr_compute_dimm_parameters(const unsigned int ctrl_num,
8403e664d8SYork Sun 					 const ddr3_spd_eeprom_t *spd,
855614e71bSYork Sun 					 dimm_params_t *pdimm,
865614e71bSYork Sun 					 unsigned int dimm_number)
875614e71bSYork Sun {
885614e71bSYork Sun 	unsigned int retval;
895614e71bSYork Sun 	unsigned int mtb_ps;
905614e71bSYork Sun 	int ftb_10th_ps;
915614e71bSYork Sun 	int i;
925614e71bSYork Sun 
935614e71bSYork Sun 	if (spd->mem_type) {
945614e71bSYork Sun 		if (spd->mem_type != SPD_MEMTYPE_DDR3) {
955614e71bSYork Sun 			printf("DIMM %u: is not a DDR3 SPD.\n", dimm_number);
965614e71bSYork Sun 			return 1;
975614e71bSYork Sun 		}
985614e71bSYork Sun 	} else {
995614e71bSYork Sun 		memset(pdimm, 0, sizeof(dimm_params_t));
1005614e71bSYork Sun 		return 1;
1015614e71bSYork Sun 	}
1025614e71bSYork Sun 
1035614e71bSYork Sun 	retval = ddr3_spd_check(spd);
1045614e71bSYork Sun 	if (retval) {
1055614e71bSYork Sun 		printf("DIMM %u: failed checksum\n", dimm_number);
1065614e71bSYork Sun 		return 2;
1075614e71bSYork Sun 	}
1085614e71bSYork Sun 
1095614e71bSYork Sun 	/*
1105614e71bSYork Sun 	 * The part name in ASCII in the SPD EEPROM is not null terminated.
1115614e71bSYork Sun 	 * Guarantee null termination here by presetting all bytes to 0
1125614e71bSYork Sun 	 * and copying the part name in ASCII from the SPD onto it
1135614e71bSYork Sun 	 */
1145614e71bSYork Sun 	memset(pdimm->mpart, 0, sizeof(pdimm->mpart));
1155614e71bSYork Sun 	if ((spd->info_size_crc & 0xF) > 1)
1165614e71bSYork Sun 		memcpy(pdimm->mpart, spd->mpart, sizeof(pdimm->mpart) - 1);
1175614e71bSYork Sun 
1185614e71bSYork Sun 	/* DIMM organization parameters */
1195614e71bSYork Sun 	pdimm->n_ranks = ((spd->organization >> 3) & 0x7) + 1;
1205614e71bSYork Sun 	pdimm->rank_density = compute_ranksize(spd);
1215614e71bSYork Sun 	pdimm->capacity = pdimm->n_ranks * pdimm->rank_density;
1225614e71bSYork Sun 	pdimm->primary_sdram_width = 1 << (3 + (spd->bus_width & 0x7));
1235614e71bSYork Sun 	if ((spd->bus_width >> 3) & 0x3)
1245614e71bSYork Sun 		pdimm->ec_sdram_width = 8;
1255614e71bSYork Sun 	else
1265614e71bSYork Sun 		pdimm->ec_sdram_width = 0;
1275614e71bSYork Sun 	pdimm->data_width = pdimm->primary_sdram_width
1285614e71bSYork Sun 			  + pdimm->ec_sdram_width;
1295614e71bSYork Sun 	pdimm->device_width = 1 << ((spd->organization & 0x7) + 2);
1305614e71bSYork Sun 
1315614e71bSYork Sun 	/* These are the types defined by the JEDEC DDR3 SPD spec */
1325614e71bSYork Sun 	pdimm->mirrored_dimm = 0;
1335614e71bSYork Sun 	pdimm->registered_dimm = 0;
1345614e71bSYork Sun 	switch (spd->module_type & DDR3_SPD_MODULETYPE_MASK) {
1355614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_RDIMM:
1365614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_MINI_RDIMM:
1375614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_72B_SO_RDIMM:
1385614e71bSYork Sun 		/* Registered/buffered DIMMs */
1395614e71bSYork Sun 		pdimm->registered_dimm = 1;
1405614e71bSYork Sun 		for (i = 0; i < 16; i += 2) {
1415614e71bSYork Sun 			u8 rcw = spd->mod_section.registered.rcw[i/2];
1425614e71bSYork Sun 			pdimm->rcw[i]   = (rcw >> 0) & 0x0F;
1435614e71bSYork Sun 			pdimm->rcw[i+1] = (rcw >> 4) & 0x0F;
1445614e71bSYork Sun 		}
1455614e71bSYork Sun 		break;
1465614e71bSYork Sun 
1475614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_UDIMM:
1485614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_SO_DIMM:
1495614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_MICRO_DIMM:
1505614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_MINI_UDIMM:
1515614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_MINI_CDIMM:
1525614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_72B_SO_UDIMM:
1535614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_72B_SO_CDIMM:
1545614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_LRDIMM:
1555614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_16B_SO_DIMM:
1565614e71bSYork Sun 	case DDR3_SPD_MODULETYPE_32B_SO_DIMM:
1575614e71bSYork Sun 		/* Unbuffered DIMMs */
1585614e71bSYork Sun 		if (spd->mod_section.unbuffered.addr_mapping & 0x1)
1595614e71bSYork Sun 			pdimm->mirrored_dimm = 1;
1605614e71bSYork Sun 		break;
1615614e71bSYork Sun 
1625614e71bSYork Sun 	default:
1635614e71bSYork Sun 		printf("unknown module_type 0x%02X\n", spd->module_type);
1645614e71bSYork Sun 		return 1;
1655614e71bSYork Sun 	}
1665614e71bSYork Sun 
1675614e71bSYork Sun 	/* SDRAM device parameters */
1685614e71bSYork Sun 	pdimm->n_row_addr = ((spd->addressing >> 3) & 0x7) + 12;
1695614e71bSYork Sun 	pdimm->n_col_addr = (spd->addressing & 0x7) + 9;
1705614e71bSYork Sun 	pdimm->n_banks_per_sdram_device = 8 << ((spd->density_banks >> 4) & 0x7);
1715614e71bSYork Sun 
1725614e71bSYork Sun 	/*
1735614e71bSYork Sun 	 * The SPD spec has not the ECC bit,
1745614e71bSYork Sun 	 * We consider the DIMM as ECC capability
1755614e71bSYork Sun 	 * when the extension bus exist
1765614e71bSYork Sun 	 */
1775614e71bSYork Sun 	if (pdimm->ec_sdram_width)
1785614e71bSYork Sun 		pdimm->edc_config = 0x02;
1795614e71bSYork Sun 	else
1805614e71bSYork Sun 		pdimm->edc_config = 0x00;
1815614e71bSYork Sun 
1825614e71bSYork Sun 	/*
1835614e71bSYork Sun 	 * The SPD spec has not the burst length byte
1845614e71bSYork Sun 	 * but DDR3 spec has nature BL8 and BC4,
1855614e71bSYork Sun 	 * BL8 -bit3, BC4 -bit2
1865614e71bSYork Sun 	 */
1875614e71bSYork Sun 	pdimm->burst_lengths_bitmask = 0x0c;
1885614e71bSYork Sun 
1895614e71bSYork Sun 	/* MTB - medium timebase
1905614e71bSYork Sun 	 * The unit in the SPD spec is ns,
1915614e71bSYork Sun 	 * We convert it to ps.
1925614e71bSYork Sun 	 * eg: MTB = 0.125ns (125ps)
1935614e71bSYork Sun 	 */
1945614e71bSYork Sun 	mtb_ps = (spd->mtb_dividend * 1000) /spd->mtb_divisor;
1955614e71bSYork Sun 	pdimm->mtb_ps = mtb_ps;
1965614e71bSYork Sun 
1975614e71bSYork Sun 	/*
1985614e71bSYork Sun 	 * FTB - fine timebase
1995614e71bSYork Sun 	 * use 1/10th of ps as our unit to avoid floating point
2005614e71bSYork Sun 	 * eg, 10 for 1ps, 25 for 2.5ps, 50 for 5ps
2015614e71bSYork Sun 	 */
2025614e71bSYork Sun 	ftb_10th_ps =
2035614e71bSYork Sun 		((spd->ftb_div & 0xf0) >> 4) * 10 / (spd->ftb_div & 0x0f);
2045614e71bSYork Sun 	pdimm->ftb_10th_ps = ftb_10th_ps;
2055614e71bSYork Sun 	/*
2065614e71bSYork Sun 	 * sdram minimum cycle time
2075614e71bSYork Sun 	 * we assume the MTB is 0.125ns
2085614e71bSYork Sun 	 * eg:
2095614e71bSYork Sun 	 * tck_min=15 MTB (1.875ns) ->DDR3-1066
2105614e71bSYork Sun 	 *        =12 MTB (1.5ns) ->DDR3-1333
2115614e71bSYork Sun 	 *        =10 MTB (1.25ns) ->DDR3-1600
2125614e71bSYork Sun 	 */
2135614e71bSYork Sun 	pdimm->tckmin_x_ps = spd->tck_min * mtb_ps +
2145614e71bSYork Sun 		(spd->fine_tck_min * ftb_10th_ps) / 10;
2155614e71bSYork Sun 
2165614e71bSYork Sun 	/*
2175614e71bSYork Sun 	 * CAS latency supported
2185614e71bSYork Sun 	 * bit4 - CL4
2195614e71bSYork Sun 	 * bit5 - CL5
2205614e71bSYork Sun 	 * bit18 - CL18
2215614e71bSYork Sun 	 */
2225614e71bSYork Sun 	pdimm->caslat_x  = ((spd->caslat_msb << 8) | spd->caslat_lsb) << 4;
2235614e71bSYork Sun 
2245614e71bSYork Sun 	/*
2255614e71bSYork Sun 	 * min CAS latency time
2265614e71bSYork Sun 	 * eg: taa_min =
2275614e71bSYork Sun 	 * DDR3-800D	100 MTB (12.5ns)
2285614e71bSYork Sun 	 * DDR3-1066F	105 MTB (13.125ns)
2295614e71bSYork Sun 	 * DDR3-1333H	108 MTB (13.5ns)
2305614e71bSYork Sun 	 * DDR3-1600H	90 MTB (11.25ns)
2315614e71bSYork Sun 	 */
2325614e71bSYork Sun 	pdimm->taa_ps = spd->taa_min * mtb_ps +
2335614e71bSYork Sun 		(spd->fine_taa_min * ftb_10th_ps) / 10;
2345614e71bSYork Sun 
2355614e71bSYork Sun 	/*
2365614e71bSYork Sun 	 * min write recovery time
2375614e71bSYork Sun 	 * eg:
2385614e71bSYork Sun 	 * twr_min = 120 MTB (15ns) -> all speed grades.
2395614e71bSYork Sun 	 */
2405614e71bSYork Sun 	pdimm->twr_ps = spd->twr_min * mtb_ps;
2415614e71bSYork Sun 
2425614e71bSYork Sun 	/*
2435614e71bSYork Sun 	 * min RAS to CAS delay time
2445614e71bSYork Sun 	 * eg: trcd_min =
2455614e71bSYork Sun 	 * DDR3-800	100 MTB (12.5ns)
2465614e71bSYork Sun 	 * DDR3-1066F	105 MTB (13.125ns)
2475614e71bSYork Sun 	 * DDR3-1333H	108 MTB (13.5ns)
2485614e71bSYork Sun 	 * DDR3-1600H	90 MTB (11.25)
2495614e71bSYork Sun 	 */
2505614e71bSYork Sun 	pdimm->trcd_ps = spd->trcd_min * mtb_ps +
2515614e71bSYork Sun 		(spd->fine_trcd_min * ftb_10th_ps) / 10;
2525614e71bSYork Sun 
2535614e71bSYork Sun 	/*
2545614e71bSYork Sun 	 * min row active to row active delay time
2555614e71bSYork Sun 	 * eg: trrd_min =
2565614e71bSYork Sun 	 * DDR3-800(1KB page)	80 MTB (10ns)
2575614e71bSYork Sun 	 * DDR3-1333(1KB page)	48 MTB (6ns)
2585614e71bSYork Sun 	 */
2595614e71bSYork Sun 	pdimm->trrd_ps = spd->trrd_min * mtb_ps;
2605614e71bSYork Sun 
2615614e71bSYork Sun 	/*
2625614e71bSYork Sun 	 * min row precharge delay time
2635614e71bSYork Sun 	 * eg: trp_min =
2645614e71bSYork Sun 	 * DDR3-800D	100 MTB (12.5ns)
2655614e71bSYork Sun 	 * DDR3-1066F	105 MTB (13.125ns)
2665614e71bSYork Sun 	 * DDR3-1333H	108 MTB (13.5ns)
2675614e71bSYork Sun 	 * DDR3-1600H	90 MTB (11.25ns)
2685614e71bSYork Sun 	 */
2695614e71bSYork Sun 	pdimm->trp_ps = spd->trp_min * mtb_ps +
2705614e71bSYork Sun 		(spd->fine_trp_min * ftb_10th_ps) / 10;
2715614e71bSYork Sun 
2725614e71bSYork Sun 	/* min active to precharge delay time
2735614e71bSYork Sun 	 * eg: tRAS_min =
2745614e71bSYork Sun 	 * DDR3-800D	300 MTB (37.5ns)
2755614e71bSYork Sun 	 * DDR3-1066F	300 MTB (37.5ns)
2765614e71bSYork Sun 	 * DDR3-1333H	288 MTB (36ns)
2775614e71bSYork Sun 	 * DDR3-1600H	280 MTB (35ns)
2785614e71bSYork Sun 	 */
2795614e71bSYork Sun 	pdimm->tras_ps = (((spd->tras_trc_ext & 0xf) << 8) | spd->tras_min_lsb)
2805614e71bSYork Sun 			* mtb_ps;
2815614e71bSYork Sun 	/*
2825614e71bSYork Sun 	 * min active to actice/refresh delay time
2835614e71bSYork Sun 	 * eg: tRC_min =
2845614e71bSYork Sun 	 * DDR3-800D	400 MTB (50ns)
2855614e71bSYork Sun 	 * DDR3-1066F	405 MTB (50.625ns)
2865614e71bSYork Sun 	 * DDR3-1333H	396 MTB (49.5ns)
2875614e71bSYork Sun 	 * DDR3-1600H	370 MTB (46.25ns)
2885614e71bSYork Sun 	 */
2895614e71bSYork Sun 	pdimm->trc_ps = (((spd->tras_trc_ext & 0xf0) << 4) | spd->trc_min_lsb)
2905614e71bSYork Sun 			* mtb_ps + (spd->fine_trc_min * ftb_10th_ps) / 10;
2915614e71bSYork Sun 	/*
2925614e71bSYork Sun 	 * min refresh recovery delay time
2935614e71bSYork Sun 	 * eg: tRFC_min =
2945614e71bSYork Sun 	 * 512Mb	720 MTB (90ns)
2955614e71bSYork Sun 	 * 1Gb		880 MTB (110ns)
2965614e71bSYork Sun 	 * 2Gb		1280 MTB (160ns)
2975614e71bSYork Sun 	 */
2985614e71bSYork Sun 	pdimm->trfc_ps = ((spd->trfc_min_msb << 8) | spd->trfc_min_lsb)
2995614e71bSYork Sun 			* mtb_ps;
3005614e71bSYork Sun 	/*
3015614e71bSYork Sun 	 * min internal write to read command delay time
3025614e71bSYork Sun 	 * eg: twtr_min = 40 MTB (7.5ns) - all speed bins.
3035614e71bSYork Sun 	 * tWRT is at least 4 mclk independent of operating freq.
3045614e71bSYork Sun 	 */
3055614e71bSYork Sun 	pdimm->twtr_ps = spd->twtr_min * mtb_ps;
3065614e71bSYork Sun 
3075614e71bSYork Sun 	/*
3085614e71bSYork Sun 	 * min internal read to precharge command delay time
3095614e71bSYork Sun 	 * eg: trtp_min = 40 MTB (7.5ns) - all speed bins.
3105614e71bSYork Sun 	 * tRTP is at least 4 mclk independent of operating freq.
3115614e71bSYork Sun 	 */
3125614e71bSYork Sun 	pdimm->trtp_ps = spd->trtp_min * mtb_ps;
3135614e71bSYork Sun 
3145614e71bSYork Sun 	/*
3155614e71bSYork Sun 	 * Average periodic refresh interval
3165614e71bSYork Sun 	 * tREFI = 7.8 us at normal temperature range
3175614e71bSYork Sun 	 *       = 3.9 us at ext temperature range
3185614e71bSYork Sun 	 */
3195614e71bSYork Sun 	pdimm->refresh_rate_ps = 7800000;
3205614e71bSYork Sun 	if ((spd->therm_ref_opt & 0x1) && !(spd->therm_ref_opt & 0x2)) {
3215614e71bSYork Sun 		pdimm->refresh_rate_ps = 3900000;
3225614e71bSYork Sun 		pdimm->extended_op_srt = 1;
3235614e71bSYork Sun 	}
3245614e71bSYork Sun 
3255614e71bSYork Sun 	/*
3265614e71bSYork Sun 	 * min four active window delay time
3275614e71bSYork Sun 	 * eg: tfaw_min =
3285614e71bSYork Sun 	 * DDR3-800(1KB page)	320 MTB (40ns)
3295614e71bSYork Sun 	 * DDR3-1066(1KB page)	300 MTB (37.5ns)
3305614e71bSYork Sun 	 * DDR3-1333(1KB page)	240 MTB (30ns)
3315614e71bSYork Sun 	 * DDR3-1600(1KB page)	240 MTB (30ns)
3325614e71bSYork Sun 	 */
3335614e71bSYork Sun 	pdimm->tfaw_ps = (((spd->tfaw_msb & 0xf) << 8) | spd->tfaw_min)
3345614e71bSYork Sun 			* mtb_ps;
3355614e71bSYork Sun 
3365614e71bSYork Sun 	return 0;
3375614e71bSYork Sun }
338