xref: /openbmc/u-boot/arch/x86/cpu/quark/mrc.c (revision de9ac9a1)
1 /*
2  * Copyright (C) 2013, Intel Corporation
3  * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4  *
5  * Ported from Intel released Quark UEFI BIOS
6  * QuarkSocPkg/QuarkNorthCluster/MemoryInit/Pei
7  *
8  * SPDX-License-Identifier:	Intel
9  */
10 
11 /*
12  * This is the main Quark Memory Reference Code (MRC)
13  *
14  * These functions are generic and should work for any Quark-based board.
15  *
16  * MRC requires two data structures to be passed in which are initialized by
17  * mrc_adjust_params().
18  *
19  * The basic flow is as follows:
20  * 01) Check for supported DDR speed configuration
21  * 02) Set up Memory Manager buffer as pass-through (POR)
22  * 03) Set Channel Interleaving Mode and Channel Stride to the most aggressive
23  *     setting possible
24  * 04) Set up the Memory Controller logic
25  * 05) Set up the DDR_PHY logic
26  * 06) Initialise the DRAMs (JEDEC)
27  * 07) Perform the Receive Enable Calibration algorithm
28  * 08) Perform the Write Leveling algorithm
29  * 09) Perform the Read Training algorithm (includes internal Vref)
30  * 10) Perform the Write Training algorithm
31  * 11) Set Channel Interleaving Mode and Channel Stride to the desired settings
32  *
33  * DRAM unit configuration based on Valleyview MRC.
34  */
35 
36 #include <common.h>
37 #include <version.h>
38 #include <asm/arch/mrc.h>
39 #include <asm/arch/msg_port.h>
40 #include "mrc_util.h"
41 #include "smc.h"
42 
43 static const struct mem_init init[] = {
44 	{ 0x0101, BM_COLD | BM_FAST | BM_WARM | BM_S3, clear_self_refresh       },
45 	{ 0x0200, BM_COLD | BM_FAST | BM_WARM | BM_S3, prog_ddr_timing_control  },
46 	{ 0x0103, BM_COLD | BM_FAST                  , prog_decode_before_jedec },
47 	{ 0x0104, BM_COLD | BM_FAST                  , perform_ddr_reset        },
48 	{ 0x0300, BM_COLD | BM_FAST           | BM_S3, ddrphy_init              },
49 	{ 0x0400, BM_COLD | BM_FAST                  , perform_jedec_init       },
50 	{ 0x0105, BM_COLD | BM_FAST                  , set_ddr_init_complete    },
51 	{ 0x0106,           BM_FAST | BM_WARM | BM_S3, restore_timings          },
52 	{ 0x0106, BM_COLD                            , default_timings          },
53 	{ 0x0500, BM_COLD                            , rcvn_cal                 },
54 	{ 0x0600, BM_COLD                            , wr_level                 },
55 	{ 0x0120, BM_COLD                            , prog_page_ctrl           },
56 	{ 0x0700, BM_COLD                            , rd_train                 },
57 	{ 0x0800, BM_COLD                            , wr_train                 },
58 	{ 0x010b, BM_COLD                            , store_timings            },
59 	{ 0x010c, BM_COLD | BM_FAST | BM_WARM | BM_S3, enable_scrambling        },
60 	{ 0x010d, BM_COLD | BM_FAST | BM_WARM | BM_S3, prog_ddr_control         },
61 	{ 0x010e, BM_COLD | BM_FAST | BM_WARM | BM_S3, prog_dra_drb             },
62 	{ 0x010f,                     BM_WARM | BM_S3, perform_wake             },
63 	{ 0x0110, BM_COLD | BM_FAST | BM_WARM | BM_S3, change_refresh_period    },
64 	{ 0x0111, BM_COLD | BM_FAST | BM_WARM | BM_S3, set_auto_refresh         },
65 	{ 0x0112, BM_COLD | BM_FAST | BM_WARM | BM_S3, ecc_enable               },
66 	{ 0x0113, BM_COLD | BM_FAST                  , memory_test              },
67 	{ 0x0114, BM_COLD | BM_FAST | BM_WARM | BM_S3, lock_registers           }
68 };
69 
70 /* Adjust configuration parameters before initialization sequence */
71 static void mrc_adjust_params(struct mrc_params *mrc_params)
72 {
73 	const struct dram_params *dram_params;
74 	uint8_t dram_width;
75 	uint32_t rank_enables;
76 	uint32_t channel_width;
77 
78 	ENTERFN();
79 
80 	/* initially expect success */
81 	mrc_params->status = MRC_SUCCESS;
82 
83 	dram_width = mrc_params->dram_width;
84 	rank_enables = mrc_params->rank_enables;
85 	channel_width = mrc_params->channel_width;
86 
87 	/*
88 	 * Setup board layout (must be reviewed as is selecting static timings)
89 	 * 0 == R0 (DDR3 x16), 1 == R1 (DDR3 x16),
90 	 * 2 == DV (DDR3 x8), 3 == SV (DDR3 x8).
91 	 */
92 	if (dram_width == X8)
93 		mrc_params->board_id = 2;	/* select x8 layout */
94 	else
95 		mrc_params->board_id = 0;	/* select x16 layout */
96 
97 	/* initially no memory */
98 	mrc_params->mem_size = 0;
99 
100 	/* begin of channel settings */
101 	dram_params = &mrc_params->params;
102 
103 	/*
104 	 * Determine column bits:
105 	 *
106 	 * Column: 11 for 8Gbx8, else 10
107 	 */
108 	mrc_params->column_bits[0] =
109 		(dram_params[0].density == 4) &&
110 		(dram_width == X8) ? 11 : 10;
111 
112 	/*
113 	 * Determine row bits:
114 	 *
115 	 * 512Mbx16=12 512Mbx8=13
116 	 * 1Gbx16=13   1Gbx8=14
117 	 * 2Gbx16=14   2Gbx8=15
118 	 * 4Gbx16=15   4Gbx8=16
119 	 * 8Gbx16=16   8Gbx8=16
120 	 */
121 	mrc_params->row_bits[0] = 12 + dram_params[0].density +
122 		(dram_params[0].density < 4) &&
123 		(dram_width == X8) ? 1 : 0;
124 
125 	/*
126 	 * Determine per-channel memory size:
127 	 *
128 	 * (For 2 RANKs, multiply by 2)
129 	 * (For 16 bit data bus, divide by 2)
130 	 *
131 	 * DENSITY WIDTH MEM_AVAILABLE
132 	 * 512Mb   x16   0x008000000 ( 128MB)
133 	 * 512Mb   x8    0x010000000 ( 256MB)
134 	 * 1Gb     x16   0x010000000 ( 256MB)
135 	 * 1Gb     x8    0x020000000 ( 512MB)
136 	 * 2Gb     x16   0x020000000 ( 512MB)
137 	 * 2Gb     x8    0x040000000 (1024MB)
138 	 * 4Gb     x16   0x040000000 (1024MB)
139 	 * 4Gb     x8    0x080000000 (2048MB)
140 	 */
141 	mrc_params->channel_size[0] = 1 << dram_params[0].density;
142 	mrc_params->channel_size[0] *= (dram_width == X8) ? 2 : 1;
143 	mrc_params->channel_size[0] *= (rank_enables == 0x3) ? 2 : 1;
144 	mrc_params->channel_size[0] *= (channel_width == X16) ? 1 : 2;
145 
146 	/* Determine memory size (convert number of 64MB/512Mb units) */
147 	mrc_params->mem_size += mrc_params->channel_size[0] << 26;
148 
149 	LEAVEFN();
150 }
151 
152 static void mrc_mem_init(struct mrc_params *mrc_params)
153 {
154 	int i;
155 
156 	ENTERFN();
157 
158 	/* MRC started */
159 	mrc_post_code(0x01, 0x00);
160 
161 	if (mrc_params->boot_mode != BM_COLD) {
162 		if (mrc_params->ddr_speed != mrc_params->timings.ddr_speed) {
163 			/* full training required as frequency changed */
164 			mrc_params->boot_mode = BM_COLD;
165 		}
166 	}
167 
168 	for (i = 0; i < ARRAY_SIZE(init); i++) {
169 		uint64_t my_tsc;
170 
171 		if (mrc_params->boot_mode & init[i].boot_path) {
172 			uint8_t major = init[i].post_code >> 8 & 0xff;
173 			uint8_t minor = init[i].post_code >> 0 & 0xff;
174 			mrc_post_code(major, minor);
175 
176 			my_tsc = rdtsc();
177 			init[i].init_fn(mrc_params);
178 			DPF(D_TIME, "Execution time %llx", rdtsc() - my_tsc);
179 		}
180 	}
181 
182 	/* display the timings */
183 	print_timings(mrc_params);
184 
185 	/* MRC complete */
186 	mrc_post_code(0x01, 0xff);
187 
188 	LEAVEFN();
189 }
190 
191 void mrc_init(struct mrc_params *mrc_params)
192 {
193 	ENTERFN();
194 
195 	DPF(D_INFO, "MRC Version %04x %s %s\n", MRC_VERSION,
196 	    U_BOOT_DATE, U_BOOT_TIME);
197 
198 	/* Set up the data structures used by mrc_mem_init() */
199 	mrc_adjust_params(mrc_params);
200 
201 	/* Initialize system memory */
202 	mrc_mem_init(mrc_params);
203 
204 	LEAVEFN();
205 }
206