Debian Codegraph @

src/northbridge/intel/haswell/native_raminit/timings_refresh.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <assert.h>
4 #include <commonlib/bsd/clamp.h>
5 #include <console/console.h>
6 #include <delay.h>
7 #include <device/pci_ops.h>
8 #include <northbridge/intel/haswell/haswell.h>
9
10 #include "raminit_native.h"
11
12 #define BL 8 /* Burst length */
13 #define tCCD 4
14 #define tRPRE 1
15 #define tWPRE 1
16 #define tDLLK 512
17
18 static bool is_sodimm(const enum spd_dimm_type_ddr3 type)
19 {
20 return type == SPD_DDR3_DIMM_TYPE_SO_DIMM || type == SPD_DDR3_DIMM_TYPE_72B_SO_UDIMM;
21 }
22
23 static uint8_t get_odt_stretch(const struct sysinfo *const ctrl)
24 {
25 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
26 /* Only stretch with 2 DIMMs per channel */
27 if (ctrl->dpc[channel] != 2)
28 continue;
29
30 const struct raminit_dimm_info *dimms = ctrl->dimms[channel];
31
32 /* Only stretch when using SO-DIMMs */
33 if (!is_sodimm(dimms[0].data.dimm_type) || !is_sodimm(dimms[1].data.dimm_type))
34 continue;
35
36 /* Only stretch with mismatched card types */
37 if (dimms[0].data.reference_card == dimms[1].data.reference_card)
38 continue;
39
40 /* Stretch if one SO-DIMM is card F */
41 for (uint8_t slot = 0; slot < NUM_SLOTS; slot++) {
42 if (dimms[slot].data.reference_card == 5)
43 return 1;
44 }
45 }
46 return 0;
47 }
48
49 static union tc_bank_reg make_tc_bank(struct sysinfo *const ctrl)
50 {
51 return (union tc_bank_reg) {
52 .tRCD = ctrl->tRCD,
53 .tRP = ctrl->tRP,
54 .tRAS = ctrl->tRAS,
55 .tRDPRE = ctrl->tRTP,
56 .tWRPRE = 4 + ctrl->tCWL + ctrl->tWR,
57 .tRRD = ctrl->tRRD,
58 .tRPab_ext = 0, /** TODO: For LPDDR, this is ctrl->tRPab - ctrl->tRP **/
59 };
60 }
61
62 static union tc_bank_rank_a_reg make_tc_bankrank_a(struct sysinfo *ctrl, uint8_t odt_stretch)
63 {
64 /* Use 3N mode for DDR during training, but always use 1N mode for LPDDR */
65 const uint32_t tCMD = ctrl->lpddr ? 0 : 3;
66 const uint32_t tRDRD_drdd = BL / 2 + 1 + tRPRE + odt_stretch + !!ctrl->lpddr;
67
68 return (union tc_bank_rank_a_reg) {
69 .tCKE = get_tCKE(ctrl->mem_clock_mhz, ctrl->lpddr),
70 .tFAW = ctrl->tFAW,
71 .tRDRD_sr = tCCD,
72 .tRDRD_dr = tRDRD_drdd,
73 .tRDRD_dd = tRDRD_drdd,
74 .tRDPDEN = ctrl->tAA + BL / 2 + 1,
75 .cmd_3st_dis = 1, /* Disable command tri-state before training */
76 .cmd_stretch = tCMD,
77 };
78 }
79
80 static union tc_bank_rank_b_reg make_tc_bankrank_b(struct sysinfo *const ctrl)
81 {
82 const uint8_t tWRRD_drdd = ctrl->tCWL - ctrl->tAA + BL / 2 + 2 + tRPRE;
83 const uint8_t tWRWR_drdd = BL / 2 + 2 + tWPRE;
84
85 return (union tc_bank_rank_b_reg) {
86 .tWRRD_sr = tCCD + ctrl->tCWL + ctrl->tWTR + 2,
87 .tWRRD_dr = ctrl->lpddr ? 8 : tWRRD_drdd,
88 .tWRRD_dd = ctrl->lpddr ? 8 : tWRRD_drdd,
89 .tWRWR_sr = tCCD,
90 .tWRWR_dr = tWRWR_drdd,
91 .tWRWR_dd = tWRWR_drdd,
92 .tWRPDEN = ctrl->tWR + ctrl->tCWL + BL / 2,
93 .dec_wrd = ctrl->tCWL >= 6,
94 };
95 }
96
97 static uint32_t get_tRDWR_sr(const struct sysinfo *ctrl)
98 {
99 if (ctrl->lpddr) {
100 const uint32_t tdqsck_max = DIV_ROUND_UP(5500, ctrl->qclkps * 2);
101 return ctrl->tAA - ctrl->tCWL + tCCD + tWPRE + tdqsck_max + 1;
102 } else {
103 const bool fast_clock = ctrl->mem_clock_mhz > 666;
104 return ctrl->tAA - ctrl->tCWL + tCCD + tWPRE + 2 + fast_clock;
105 }
106 }
107
108 static union tc_bank_rank_c_reg make_tc_bankrank_c(struct sysinfo *ctrl, uint8_t odt_stretch)
109 {
110 const uint32_t tRDWR_sr = get_tRDWR_sr(ctrl);
111 const uint32_t tRDWR_drdd = tRDWR_sr + odt_stretch;
112
113 return (union tc_bank_rank_c_reg) {
114 .tXPDLL = get_tXPDLL(ctrl->mem_clock_mhz),
115 .tXP = MAX(ctrl->tXP, 7), /* Use a higher tXP for training */
116 .tAONPD = get_tAONPD(ctrl->mem_clock_mhz),
117 .tRDWR_sr = tRDWR_sr,
118 .tRDWR_dr = tRDWR_drdd,
119 .tRDWR_dd = tRDWR_drdd,
120 };
121 }
122
123 static union tc_bank_rank_d_reg make_tc_bankrank_d(struct sysinfo *ctrl, uint8_t odt_stretch)
124 {
125 const uint32_t odt_rd_delay = ctrl->tAA - ctrl->tCWL;
126 if (!ctrl->lpddr) {
127 return (union tc_bank_rank_d_reg) {
128 .tAA = ctrl->tAA,
129 .tCWL = ctrl->tCWL,
130 .tCPDED = 1,
131 .tPRPDEN = 1,
132 .odt_read_delay = odt_rd_delay,
133 .odt_read_duration = odt_stretch,
134 };
135 }
136
137 /* tCWL has 1 extra clock because of tDQSS, subtract it here */
138 const uint32_t tCWL_lpddr = ctrl->tCWL - 1;
139 const uint32_t odt_wr_delay = tCWL_lpddr + DIV_ROUND_UP(3500, ctrl->qclkps * 2);
140 const uint32_t odt_wr_duration = DIV_ROUND_UP(3500 - 1750, ctrl->qclkps * 2) + 1;
141
142 return (union tc_bank_rank_d_reg) {
143 .tAA = ctrl->tAA,
144 .tCWL = tCWL_lpddr,
145 .tCPDED = 2, /* Required by JEDEC LPDDR3 spec */
146 .tPRPDEN = 1,
147 .odt_read_delay = odt_rd_delay,
148 .odt_read_duration = odt_stretch,
149 .odt_write_delay = odt_wr_delay,
150 .odt_write_duration = odt_wr_duration,
151 .odt_always_rank_0 = ctrl->lpddr_dram_odt
152 };
153 }
154
155 /* ZQCS period values, in (tREFI * 128) units */
156 #define ZQCS_PERIOD_DDR3 128 /* tREFI * 128 = 7.8 us * 128 = 1ms */
157 #define ZQCS_PERIOD_LPDDR3 256 /* tREFI * 128 = 3.9 us * 128 = 0.5ms */
158
159 static uint32_t make_tc_zqcal(const struct sysinfo *const ctrl)
160 {
161 const uint32_t zqcs_period = ctrl->lpddr ? ZQCS_PERIOD_LPDDR3 : ZQCS_PERIOD_DDR3;
162 const uint32_t tZQCS = get_tZQCS(ctrl->mem_clock_mhz, ctrl->lpddr);
163 return tZQCS << (is_hsw_ult() ? 10 : 8) | zqcs_period;
164 }
165
166 static union tc_rftp_reg make_tc_rftp(const struct sysinfo *const ctrl)
167 {
168 /*
169 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
170 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
171 */
172 return (union tc_rftp_reg) {
173 .tREFI = ctrl->tREFI,
174 .tRFC = ctrl->tRFC,
175 .tREFIx9 = ctrl->tREFI * 89 / 10240,
176 };
177 }
178
179 static union tc_srftp_reg make_tc_srftp(const struct sysinfo *const ctrl)
180 {
181 return (union tc_srftp_reg) {
182 .tXSDLL = tDLLK,
183 .tXS_offset = get_tXS_offset(ctrl->mem_clock_mhz),
184 .tZQOPER = get_tZQOPER(ctrl->mem_clock_mhz, ctrl->lpddr),
185 .tMOD = get_tMOD(ctrl->mem_clock_mhz) - 8,
186 };
187 }
188
189 void configure_timings(struct sysinfo *ctrl)
190 {
191 if (ctrl->lpddr)
192 die("%s: Missing support for LPDDR\n", __func__);
193
194 const uint8_t odt_stretch = get_odt_stretch(ctrl);
195 const union tc_bank_reg tc_bank = make_tc_bank(ctrl);
196 const union tc_bank_rank_a_reg tc_bank_rank_a = make_tc_bankrank_a(ctrl, odt_stretch);
197 const union tc_bank_rank_b_reg tc_bank_rank_b = make_tc_bankrank_b(ctrl);
198 const union tc_bank_rank_c_reg tc_bank_rank_c = make_tc_bankrank_c(ctrl, odt_stretch);
199 const union tc_bank_rank_d_reg tc_bank_rank_d = make_tc_bankrank_d(ctrl, odt_stretch);
200
201 const uint8_t wr_delay = tc_bank_rank_b.dec_wrd + 1;
202 uint8_t sc_wr_add_delay = 0;
203 sc_wr_add_delay |= wr_delay << 0;
204 sc_wr_add_delay |= wr_delay << 2;
205 sc_wr_add_delay |= wr_delay << 4;
206 sc_wr_add_delay |= wr_delay << 6;
207
208 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
209 if (!does_ch_exist(ctrl, channel))
210 continue;
211
212 ctrl->tc_bank[channel] = tc_bank;
213 ctrl->tc_bankrank_a[channel] = tc_bank_rank_a;
214 ctrl->tc_bankrank_b[channel] = tc_bank_rank_b;
215 ctrl->tc_bankrank_c[channel] = tc_bank_rank_c;
216 ctrl->tc_bankrank_d[channel] = tc_bank_rank_d;
217
218 mchbar_write32(TC_BANK_ch(channel), ctrl->tc_bank[channel].raw);
219 mchbar_write32(TC_BANK_RANK_A_ch(channel), ctrl->tc_bankrank_a[channel].raw);
220 mchbar_write32(TC_BANK_RANK_B_ch(channel), ctrl->tc_bankrank_b[channel].raw);
221 mchbar_write32(TC_BANK_RANK_C_ch(channel), ctrl->tc_bankrank_c[channel].raw);
222 mchbar_write32(TC_BANK_RANK_D_ch(channel), ctrl->tc_bankrank_d[channel].raw);
223 mchbar_write8(SC_WR_ADD_DELAY_ch(channel), sc_wr_add_delay);
224 }
225 }
226
227 void configure_refresh(struct sysinfo *ctrl)
228 {
229 const union tc_srftp_reg tc_srftp = make_tc_srftp(ctrl);
230 const union tc_rftp_reg tc_rftp = make_tc_rftp(ctrl);
231 const uint32_t tc_zqcal = make_tc_zqcal(ctrl);
232
233 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
234 if (!does_ch_exist(ctrl, channel))
235 continue;
236
237 mchbar_setbits32(TC_RFP_ch(channel), 0xff);
238 mchbar_write32(TC_RFTP_ch(channel), tc_rftp.raw);
239 mchbar_write32(TC_SRFTP_ch(channel), tc_srftp.raw);
240 mchbar_write32(TC_ZQCAL_ch(channel), tc_zqcal);
241 }
242 }