blob: 6e76cbc0bbca5d4a85cffda30f653557b8deb7d5 [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 Pons6aa7cca2020-05-02 19:38:34 +020028#define IOSAV_MRS (0xf000)
29#define IOSAV_PRE (0xf002)
30#define IOSAV_ZQCS (0xf003)
31#define IOSAV_ACT (0xf006)
32#define IOSAV_RD (0xf105)
33#define IOSAV_NOP_ALT (0xf107)
34#define IOSAV_WR (0xf201)
35#define IOSAV_NOP (0xf207)
Angel Pons69e17142020-03-23 12:26:29 +010036
Angel Ponsd5b780c2020-05-02 21:48:46 +020037struct iosav_ssq {
38 /* IOSAV_n_SP_CMD_CTRL */
39 union {
40 struct {
41 u32 command : 16;
42 u32 ranksel_ap : 2;
43 u32 : 14;
44 };
45 u32 raw;
46 } sp_cmd_ctrl;
47
48 /* IOSAV_n_SUBSEQ_CTRL */
49 union {
50 struct {
51 u32 cmd_executions : 9;
52 u32 : 1;
53 u32 cmd_delay_gap : 5;
54 u32 : 1;
55 u32 post_ssq_wait : 9;
56 u32 : 1;
57 u32 data_direction : 2;
58 u32 : 4;
59 };
60 u32 raw;
61 } subseq_ctrl;
62
63 /* IOSAV_n_SP_CMD_ADDR */
64 union {
65 struct {
66 u32 address : 16;
67 u32 rowbits : 3;
68 u32 : 1;
69 u32 bank : 3;
70 u32 : 1;
71 u32 rank : 2;
72 u32 : 6;
73 };
74 u32 raw;
75 } sp_cmd_addr;
76
77 /* IOSAV_n_ADDR_UPDATE */
78 union {
79 struct {
80 u32 inc_addr_1 : 1;
81 u32 inc_addr_8 : 1;
82 u32 inc_bank : 1;
83 u32 inc_rank : 2;
84 u32 addr_wrap : 5;
85 u32 lfsr_upd : 2;
86 u32 upd_rate : 4;
87 u32 lfsr_xors : 2;
88 u32 : 14;
89 };
90 u32 raw;
91 } addr_update;
92};
93
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010094/* FIXME: Vendor BIOS uses 64 but our algorithms are less
95 performant and even 1 seems to be enough in practice. */
Angel Pons7c49cb82020-03-16 23:17:32 +010096#define NUM_PATTERNS 4
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010097
Angel Pons5c1baf52020-03-22 12:23:35 +010098/*
99 * WARNING: Do not forget to increase MRC_CACHE_VERSION when the saved data is changed!
100 */
Patrick Rudolphdd662872017-10-28 18:20:11 +0200101#define MRC_CACHE_VERSION 5
Angel Pons5c1baf52020-03-22 12:23:35 +0100102
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100103typedef struct odtmap_st {
104 u16 rttwr;
105 u16 rttnom;
106} odtmap;
107
Angel Pons5c1baf52020-03-22 12:23:35 +0100108/* WARNING: Do not forget to increase MRC_CACHE_VERSION when this struct is changed! */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100109typedef struct dimm_info_st {
110 dimm_attr dimm[NUM_CHANNELS][NUM_SLOTS];
111} dimm_info;
112
Angel Pons5c1baf52020-03-22 12:23:35 +0100113/* WARNING: Do not forget to increase MRC_CACHE_VERSION when this struct is changed! */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100114struct ram_rank_timings {
Angel Pons7c49cb82020-03-16 23:17:32 +0100115 /* ROUNDT_LAT register: One byte per slotrank */
Angel Pons88521882020-01-05 20:21:20 +0100116 u8 roundtrip_latency;
117
Angel Pons7c49cb82020-03-16 23:17:32 +0100118 /* IO_LATENCY register: One nibble per slotrank */
Felix Heldef4fe3e2019-12-31 14:15:05 +0100119 u8 io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100120
Angel Pons7c49cb82020-03-16 23:17:32 +0100121 /* Phase interpolator coding for command and control */
Angel Pons88521882020-01-05 20:21:20 +0100122 int pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100123
124 struct ram_lane_timings {
Angel Pons7c49cb82020-03-16 23:17:32 +0100125 /* Lane register offset 0x10 */
126 u16 timA; /* bits 0 - 5, bits 16 - 18 */
127 u8 rising; /* bits 8 - 14 */
128 u8 falling; /* bits 20 - 26 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100129
Angel Pons7c49cb82020-03-16 23:17:32 +0100130 /* Lane register offset 0x20 */
131 int timC; /* bits 0 - 5, 19 */
132 u16 timB; /* bits 8 - 13, 15 - 17 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100133 } lanes[NUM_LANES];
134};
135
Angel Pons5c1baf52020-03-22 12:23:35 +0100136/* WARNING: Do not forget to increase MRC_CACHE_VERSION when this struct is changed! */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100137typedef struct ramctr_timing_st {
138 u16 spd_crc[NUM_CHANNELS][NUM_SLOTS];
Angel Pons80037f72020-03-21 13:12:37 +0100139
140 /* CPUID value */
141 u32 cpu;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100142
Patrick Rudolph77eaba32016-11-11 18:55:54 +0100143 /* DDR base_freq = 100 Mhz / 133 Mhz */
144 u8 base_freq;
145
Angel Pons48409b82020-03-23 22:19:29 +0100146 /* Frequency index */
147 u32 FRQ;
148
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100149 u16 cas_supported;
Angel Pons7c49cb82020-03-16 23:17:32 +0100150 /* Latencies are in units of ns, scaled by x256 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100151 u32 tCK;
152 u32 tAA;
153 u32 tWR;
154 u32 tRCD;
155 u32 tRRD;
156 u32 tRP;
157 u32 tRAS;
158 u32 tRFC;
159 u32 tWTR;
160 u32 tRTP;
161 u32 tFAW;
Dan Elkoubydabebc32018-04-13 18:47:10 +0300162 u32 tCWL;
163 u32 tCMD;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100164 /* Latencies in terms of clock cycles
Angel Pons7c49cb82020-03-16 23:17:32 +0100165 They are saved separately as they are needed for DRAM MRS commands */
166 u8 CAS; /* CAS read latency */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100167 u8 CWL; /* CAS write latency */
168
169 u32 tREFI;
170 u32 tMOD;
171 u32 tXSOffset;
172 u32 tWLO;
173 u32 tCKE;
174 u32 tXPDLL;
175 u32 tXP;
176 u32 tAONPD;
177
Angel Pons7c49cb82020-03-16 23:17:32 +0100178 /* Bits [0..11] of PM_DLL_CONFIG: Master DLL wakeup delay timer */
Angel Pons88521882020-01-05 20:21:20 +0100179 u16 mdll_wake_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100180
181 u8 rankmap[NUM_CHANNELS];
182 int ref_card_offset[NUM_CHANNELS];
183 u32 mad_dimm[NUM_CHANNELS];
184 int channel_size_mb[NUM_CHANNELS];
185 u32 cmd_stretch[NUM_CHANNELS];
186
Angel Pons88521882020-01-05 20:21:20 +0100187 int pi_code_offset;
188 int pi_coding_threshold;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100189
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200190 bool ecc_supported;
191 bool ecc_forced;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200192 bool ecc_enabled;
193 int lanes; /* active lanes: 8 or 9 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100194 int edge_offset[3];
195 int timC_offset[3];
196
197 int extended_temperature_range;
198 int auto_self_refresh;
199
200 int rank_mirror[NUM_CHANNELS][NUM_SLOTRANKS];
201
202 struct ram_rank_timings timings[NUM_CHANNELS][NUM_SLOTRANKS];
203
204 dimm_info info;
205} ramctr_timing;
206
Felix Held87ddea22020-01-26 04:55:27 +0100207#define SOUTHBRIDGE PCI_DEV(0, 0x1f, 0)
208
Patrick Rudolphdd662872017-10-28 18:20:11 +0200209#define FOR_ALL_LANES for (lane = 0; lane < ctrl->lanes; lane++)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100210#define FOR_ALL_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++)
211#define FOR_ALL_POPULATED_RANKS for (slotrank = 0; slotrank < NUM_SLOTRANKS; slotrank++) if (ctrl->rankmap[channel] & (1 << slotrank))
212#define FOR_ALL_POPULATED_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++) if (ctrl->rankmap[channel])
213#define MAX_EDGE_TIMING 71
214#define MAX_TIMC 127
215#define MAX_TIMB 511
216#define MAX_TIMA 127
217#define MAX_CAS 18
218#define MIN_CAS 4
219
Angel Pons7c49cb82020-03-16 23:17:32 +0100220#define MAKE_ERR ((channel << 16) | (slotrank << 8) | 1)
221#define GET_ERR_CHANNEL(x) (x >> 16)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100222
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100223u8 get_CWL(u32 tCK);
Angel Pons88521882020-01-05 20:21:20 +0100224void dram_mrscommands(ramctr_timing *ctrl);
225void program_timings(ramctr_timing *ctrl, int channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100226void dram_find_common_params(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100227void dram_xover(ramctr_timing *ctrl);
228void dram_timing_regs(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100229void dram_dimm_mapping(ramctr_timing *ctrl);
Patrick Rudolphdd662872017-10-28 18:20:11 +0200230void dram_dimm_set_mapping(ramctr_timing *ctrl, int training);
Angel Pons88521882020-01-05 20:21:20 +0100231void dram_zones(ramctr_timing *ctrl, int training);
Angel Pons88521882020-01-05 20:21:20 +0100232void dram_memorymap(ramctr_timing *ctrl, int me_uma_size);
233void dram_jedecreset(ramctr_timing *ctrl);
234int read_training(ramctr_timing *ctrl);
235int write_training(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100236int command_training(ramctr_timing *ctrl);
237int discover_edges(ramctr_timing *ctrl);
238int discover_edges_write(ramctr_timing *ctrl);
239int discover_timC_write(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100240void normalize_training(ramctr_timing *ctrl);
241void write_controller_mr(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100242int channel_test(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100243void set_scrambling_seed(ramctr_timing *ctrl);
Angel Pons89ae6b82020-03-21 13:23:32 +0100244void set_wmm_behavior(const u32 cpu);
Angel Pons88521882020-01-05 20:21:20 +0100245void prepare_training(ramctr_timing *ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +0100246void set_read_write_timings(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100247void set_normal_operation(ramctr_timing *ctrl);
248void final_registers(ramctr_timing *ctrl);
249void restore_timings(ramctr_timing *ctrl);
Angel Ponsefbed262020-03-23 23:18:03 +0100250int try_init_dram_ddr3(ramctr_timing *ctrl, int fast_boot, int s3resume, int me_uma_size);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100251
Patrick Rudolphdd662872017-10-28 18:20:11 +0200252void channel_scrub(ramctr_timing *ctrl);
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200253bool get_host_ecc_cap(void);
254bool get_host_ecc_forced(void);
255
Patrick Rudolph305035c2016-11-11 18:38:50 +0100256#endif