blob: 80693bb30e94412d2561fe698e93e93468d6c6d5 [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002
3#ifndef RAMINIT_COMMON_H
4#define RAMINIT_COMMON_H
5
Felix Held380c6b22020-01-26 05:06:38 +01006#include <stdint.h>
7
Angel Pons7c49cb82020-03-16 23:17:32 +01008#define BASEFREQ 133
9#define tDLLK 512
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010010
Angel Pons7c49cb82020-03-16 23:17:32 +010011#define IS_SANDY_CPU(x) ((x & 0xffff0) == 0x206a0)
12#define IS_SANDY_CPU_C(x) ((x & 0xf) == 4)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010013#define IS_SANDY_CPU_D0(x) ((x & 0xf) == 5)
14#define IS_SANDY_CPU_D1(x) ((x & 0xf) == 6)
15#define IS_SANDY_CPU_D2(x) ((x & 0xf) == 7)
16
Angel Pons7c49cb82020-03-16 23:17:32 +010017#define IS_IVY_CPU(x) ((x & 0xffff0) == 0x306a0)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010018#define IS_IVY_CPU_C(x) ((x & 0xf) == 4)
19#define IS_IVY_CPU_K(x) ((x & 0xf) == 5)
20#define IS_IVY_CPU_D(x) ((x & 0xf) == 6)
21#define IS_IVY_CPU_E(x) ((x & 0xf) >= 8)
22
Angel Pons7c49cb82020-03-16 23:17:32 +010023#define NUM_CHANNELS 2
24#define NUM_SLOTRANKS 4
25#define NUM_SLOTS 2
Patrick Rudolphdd662872017-10-28 18:20:11 +020026#define NUM_LANES 9
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010027
Angel Pons3abd2062020-05-03 00:25:02 +020028/* IOSAV_n_SP_CMD_CTRL DRAM commands */
Angel Pons6aa7cca2020-05-02 19:38:34 +020029#define IOSAV_MRS (0xf000)
30#define IOSAV_PRE (0xf002)
31#define IOSAV_ZQCS (0xf003)
32#define IOSAV_ACT (0xf006)
33#define IOSAV_RD (0xf105)
34#define IOSAV_NOP_ALT (0xf107)
35#define IOSAV_WR (0xf201)
36#define IOSAV_NOP (0xf207)
Angel Pons69e17142020-03-23 12:26:29 +010037
Angel Pons3abd2062020-05-03 00:25:02 +020038/* IOSAV_n_SUBSEQ_CTRL data direction */
39#define SSQ_NA 0 /* Non-data */
40#define SSQ_RD 1 /* Read */
41#define SSQ_WR 2 /* Write */
42#define SSQ_RW 3 /* Read and write */
43
Angel Ponsd5b780c2020-05-02 21:48:46 +020044struct iosav_ssq {
45 /* IOSAV_n_SP_CMD_CTRL */
46 union {
47 struct {
Angel Pons19c5cd22020-08-10 14:27:23 +020048 u32 command : 16; /* [15.. 0] */
49 u32 ranksel_ap : 2; /* [17..16] */
Angel Ponsd5b780c2020-05-02 21:48:46 +020050 u32 : 14;
51 };
52 u32 raw;
53 } sp_cmd_ctrl;
54
55 /* IOSAV_n_SUBSEQ_CTRL */
56 union {
57 struct {
Angel Pons19c5cd22020-08-10 14:27:23 +020058 u32 cmd_executions : 9; /* [ 8.. 0] */
Angel Ponsd5b780c2020-05-02 21:48:46 +020059 u32 : 1;
Angel Pons19c5cd22020-08-10 14:27:23 +020060 u32 cmd_delay_gap : 5; /* [14..10] */
Angel Ponsd5b780c2020-05-02 21:48:46 +020061 u32 : 1;
Angel Pons19c5cd22020-08-10 14:27:23 +020062 u32 post_ssq_wait : 9; /* [24..16] */
Angel Ponsd5b780c2020-05-02 21:48:46 +020063 u32 : 1;
Angel Pons19c5cd22020-08-10 14:27:23 +020064 u32 data_direction : 2; /* [27..26] */
Angel Ponsd5b780c2020-05-02 21:48:46 +020065 u32 : 4;
66 };
67 u32 raw;
68 } subseq_ctrl;
69
70 /* IOSAV_n_SP_CMD_ADDR */
71 union {
72 struct {
Angel Pons19c5cd22020-08-10 14:27:23 +020073 u32 address : 16; /* [15.. 0] */
74 u32 rowbits : 3; /* [18..16] */
Angel Ponsd5b780c2020-05-02 21:48:46 +020075 u32 : 1;
Angel Pons19c5cd22020-08-10 14:27:23 +020076 u32 bank : 3; /* [22..20] */
Angel Ponsd5b780c2020-05-02 21:48:46 +020077 u32 : 1;
Angel Pons19c5cd22020-08-10 14:27:23 +020078 u32 rank : 2; /* [25..24] */
Angel Ponsd5b780c2020-05-02 21:48:46 +020079 u32 : 6;
80 };
81 u32 raw;
82 } sp_cmd_addr;
83
84 /* IOSAV_n_ADDR_UPDATE */
85 union {
86 struct {
Angel Pons19c5cd22020-08-10 14:27:23 +020087 u32 inc_addr_1 : 1; /* [ 0.. 0] */
88 u32 inc_addr_8 : 1; /* [ 1.. 1] */
89 u32 inc_bank : 1; /* [ 2.. 2] */
90 u32 inc_rank : 2; /* [ 4.. 3] */
91 u32 addr_wrap : 5; /* [ 9.. 5] */
92 u32 lfsr_upd : 2; /* [11..10] */
93 u32 upd_rate : 4; /* [15..12] */
94 u32 lfsr_xors : 2; /* [17..16] */
Angel Ponsd5b780c2020-05-02 21:48:46 +020095 u32 : 14;
96 };
97 u32 raw;
98 } addr_update;
99};
100
Angel Ponsffd50152020-11-12 11:03:10 +0100101typedef struct ramctr_timing_st ramctr_timing;
102
Angel Pons8f0757e2020-11-11 23:03:36 +0100103void iosav_write_sequence(const int ch, const struct iosav_ssq *seq, const unsigned int length);
Angel Pons1c505f82020-11-11 20:55:35 +0100104void iosav_run_queue(const int ch, const u8 loops, const u8 as_timer);
105void iosav_run_once(const int ch);
106void wait_for_iosav(int channel);
107
Angel Ponsffd50152020-11-12 11:03:10 +0100108void iosav_write_zqcs_sequence(int channel, int slotrank, u32 gap, u32 post, u32 wrap);
109void iosav_write_prea_sequence(int channel, int slotrank, u32 post, u32 wrap);
110void iosav_write_read_mpr_sequence(
111 int channel, int slotrank, u32 tMOD, u32 loops, u32 gap, u32 loops2, u32 post2);
112void iosav_write_misc_write_sequence(ramctr_timing *ctrl, int channel, int slotrank,
113 u32 gap0, u32 loops0, u32 gap1, u32 loops2, u32 wrap2);
114void iosav_write_command_training_sequence(
115 ramctr_timing *ctrl, int channel, int slotrank, unsigned int address);
116void iosav_write_data_write_sequence(ramctr_timing *ctrl, int channel, int slotrank);
117void iosav_write_aggressive_write_read_sequence(ramctr_timing *ctrl, int channel, int slotrank);
118void iosav_write_memory_test_sequence(ramctr_timing *ctrl, int channel, int slotrank);
119
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100120/* FIXME: Vendor BIOS uses 64 but our algorithms are less
121 performant and even 1 seems to be enough in practice. */
Angel Pons7c49cb82020-03-16 23:17:32 +0100122#define NUM_PATTERNS 4
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100123
Angel Pons5c1baf52020-03-22 12:23:35 +0100124/*
125 * WARNING: Do not forget to increase MRC_CACHE_VERSION when the saved data is changed!
126 */
Patrick Rudolphdd662872017-10-28 18:20:11 +0200127#define MRC_CACHE_VERSION 5
Angel Pons5c1baf52020-03-22 12:23:35 +0100128
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100129typedef struct odtmap_st {
130 u16 rttwr;
131 u16 rttnom;
132} odtmap;
133
Angel Pons5c1baf52020-03-22 12:23:35 +0100134/* WARNING: Do not forget to increase MRC_CACHE_VERSION when this struct is changed! */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100135typedef struct dimm_info_st {
136 dimm_attr dimm[NUM_CHANNELS][NUM_SLOTS];
137} dimm_info;
138
Angel Pons5c1baf52020-03-22 12:23:35 +0100139/* WARNING: Do not forget to increase MRC_CACHE_VERSION when this struct is changed! */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100140struct ram_rank_timings {
Angel Pons7c49cb82020-03-16 23:17:32 +0100141 /* ROUNDT_LAT register: One byte per slotrank */
Angel Pons88521882020-01-05 20:21:20 +0100142 u8 roundtrip_latency;
143
Angel Pons7c49cb82020-03-16 23:17:32 +0100144 /* IO_LATENCY register: One nibble per slotrank */
Felix Heldef4fe3e2019-12-31 14:15:05 +0100145 u8 io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100146
Angel Pons7c49cb82020-03-16 23:17:32 +0100147 /* Phase interpolator coding for command and control */
Angel Pons88521882020-01-05 20:21:20 +0100148 int pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100149
150 struct ram_lane_timings {
Angel Pons7c49cb82020-03-16 23:17:32 +0100151 /* Lane register offset 0x10 */
152 u16 timA; /* bits 0 - 5, bits 16 - 18 */
153 u8 rising; /* bits 8 - 14 */
154 u8 falling; /* bits 20 - 26 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100155
Angel Pons7c49cb82020-03-16 23:17:32 +0100156 /* Lane register offset 0x20 */
157 int timC; /* bits 0 - 5, 19 */
158 u16 timB; /* bits 8 - 13, 15 - 17 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100159 } lanes[NUM_LANES];
160};
161
Angel Pons5c1baf52020-03-22 12:23:35 +0100162/* WARNING: Do not forget to increase MRC_CACHE_VERSION when this struct is changed! */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100163typedef struct ramctr_timing_st {
164 u16 spd_crc[NUM_CHANNELS][NUM_SLOTS];
Angel Pons80037f72020-03-21 13:12:37 +0100165
166 /* CPUID value */
167 u32 cpu;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100168
Patrick Rudolph77eaba32016-11-11 18:55:54 +0100169 /* DDR base_freq = 100 Mhz / 133 Mhz */
170 u8 base_freq;
171
Angel Pons48409b82020-03-23 22:19:29 +0100172 /* Frequency index */
173 u32 FRQ;
174
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100175 u16 cas_supported;
Angel Pons7c49cb82020-03-16 23:17:32 +0100176 /* Latencies are in units of ns, scaled by x256 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100177 u32 tCK;
178 u32 tAA;
179 u32 tWR;
180 u32 tRCD;
181 u32 tRRD;
182 u32 tRP;
183 u32 tRAS;
184 u32 tRFC;
185 u32 tWTR;
186 u32 tRTP;
187 u32 tFAW;
Dan Elkoubydabebc32018-04-13 18:47:10 +0300188 u32 tCWL;
189 u32 tCMD;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100190 /* Latencies in terms of clock cycles
Angel Pons7c49cb82020-03-16 23:17:32 +0100191 They are saved separately as they are needed for DRAM MRS commands */
192 u8 CAS; /* CAS read latency */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100193 u8 CWL; /* CAS write latency */
194
195 u32 tREFI;
196 u32 tMOD;
197 u32 tXSOffset;
198 u32 tWLO;
199 u32 tCKE;
200 u32 tXPDLL;
201 u32 tXP;
202 u32 tAONPD;
203
Angel Pons7c49cb82020-03-16 23:17:32 +0100204 /* Bits [0..11] of PM_DLL_CONFIG: Master DLL wakeup delay timer */
Angel Pons88521882020-01-05 20:21:20 +0100205 u16 mdll_wake_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100206
207 u8 rankmap[NUM_CHANNELS];
208 int ref_card_offset[NUM_CHANNELS];
209 u32 mad_dimm[NUM_CHANNELS];
210 int channel_size_mb[NUM_CHANNELS];
211 u32 cmd_stretch[NUM_CHANNELS];
212
Angel Pons88521882020-01-05 20:21:20 +0100213 int pi_code_offset;
214 int pi_coding_threshold;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100215
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200216 bool ecc_supported;
217 bool ecc_forced;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200218 bool ecc_enabled;
219 int lanes; /* active lanes: 8 or 9 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100220 int edge_offset[3];
221 int timC_offset[3];
222
223 int extended_temperature_range;
224 int auto_self_refresh;
225
226 int rank_mirror[NUM_CHANNELS][NUM_SLOTRANKS];
227
228 struct ram_rank_timings timings[NUM_CHANNELS][NUM_SLOTRANKS];
229
230 dimm_info info;
231} ramctr_timing;
232
Felix Held87ddea22020-01-26 04:55:27 +0100233#define SOUTHBRIDGE PCI_DEV(0, 0x1f, 0)
234
Patrick Rudolphdd662872017-10-28 18:20:11 +0200235#define FOR_ALL_LANES for (lane = 0; lane < ctrl->lanes; lane++)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100236#define FOR_ALL_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++)
237#define FOR_ALL_POPULATED_RANKS for (slotrank = 0; slotrank < NUM_SLOTRANKS; slotrank++) if (ctrl->rankmap[channel] & (1 << slotrank))
238#define FOR_ALL_POPULATED_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++) if (ctrl->rankmap[channel])
239#define MAX_EDGE_TIMING 71
240#define MAX_TIMC 127
241#define MAX_TIMB 511
242#define MAX_TIMA 127
243#define MAX_CAS 18
244#define MIN_CAS 4
245
Angel Pons7c49cb82020-03-16 23:17:32 +0100246#define MAKE_ERR ((channel << 16) | (slotrank << 8) | 1)
247#define GET_ERR_CHANNEL(x) (x >> 16)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100248
Angel Pons88521882020-01-05 20:21:20 +0100249void dram_mrscommands(ramctr_timing *ctrl);
250void program_timings(ramctr_timing *ctrl, int channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100251void dram_find_common_params(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100252void dram_xover(ramctr_timing *ctrl);
253void dram_timing_regs(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100254void dram_dimm_mapping(ramctr_timing *ctrl);
Patrick Rudolphdd662872017-10-28 18:20:11 +0200255void dram_dimm_set_mapping(ramctr_timing *ctrl, int training);
Angel Pons88521882020-01-05 20:21:20 +0100256void dram_zones(ramctr_timing *ctrl, int training);
Angel Pons88521882020-01-05 20:21:20 +0100257void dram_memorymap(ramctr_timing *ctrl, int me_uma_size);
258void dram_jedecreset(ramctr_timing *ctrl);
259int read_training(ramctr_timing *ctrl);
260int write_training(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100261int command_training(ramctr_timing *ctrl);
Angel Pons4c79f932020-11-14 01:26:52 +0100262int read_mpr_training(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100263int discover_edges_write(ramctr_timing *ctrl);
264int discover_timC_write(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100265void normalize_training(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100266int channel_test(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100267void set_scrambling_seed(ramctr_timing *ctrl);
Angel Pons89ae6b82020-03-21 13:23:32 +0100268void set_wmm_behavior(const u32 cpu);
Angel Pons88521882020-01-05 20:21:20 +0100269void prepare_training(ramctr_timing *ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +0100270void set_read_write_timings(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100271void set_normal_operation(ramctr_timing *ctrl);
272void final_registers(ramctr_timing *ctrl);
273void restore_timings(ramctr_timing *ctrl);
Angel Ponsefbed262020-03-23 23:18:03 +0100274int try_init_dram_ddr3(ramctr_timing *ctrl, int fast_boot, int s3resume, int me_uma_size);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100275
Patrick Rudolphdd662872017-10-28 18:20:11 +0200276void channel_scrub(ramctr_timing *ctrl);
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200277bool get_host_ecc_cap(void);
278bool get_host_ecc_forced(void);
279
Patrick Rudolph305035c2016-11-11 18:38:50 +0100280#endif