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