Angel Pons | 6e5aabd | 2020-03-23 23:44:42 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2 | |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 3 | #include <assert.h> |
Elyes HAOUAS | f97c1c9 | 2019-12-03 18:22:06 +0100 | [diff] [blame] | 4 | #include <commonlib/helpers.h> |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 5 | #include <console/console.h> |
Angel Pons | 47a80a0 | 2020-12-07 13:15:23 +0100 | [diff] [blame] | 6 | #include <cpu/intel/model_206ax/model_206ax.h> |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 7 | #include <string.h> |
Subrata Banik | 53b08c3 | 2018-12-10 14:11:35 +0530 | [diff] [blame] | 8 | #include <arch/cpu.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 9 | #include <device/mmio.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 10 | #include <device/pci_ops.h> |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 11 | #include <northbridge/intel/sandybridge/chip.h> |
| 12 | #include <device/pci_def.h> |
| 13 | #include <delay.h> |
Elyes HAOUAS | 1d6484a | 2020-07-10 11:18:11 +0200 | [diff] [blame] | 14 | #include <types.h> |
Elyes HAOUAS | 1d3b3c3 | 2019-05-04 08:12:42 +0200 | [diff] [blame] | 15 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 16 | #include "raminit_native.h" |
| 17 | #include "raminit_common.h" |
Angel Pons | 7f6586f | 2020-03-21 12:45:12 +0100 | [diff] [blame] | 18 | #include "raminit_tables.h" |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 19 | #include "sandybridge.h" |
| 20 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 21 | /* FIXME: no support for 3-channel chipsets */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 22 | |
| 23 | static void sfence(void) |
| 24 | { |
| 25 | asm volatile ("sfence"); |
| 26 | } |
| 27 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 28 | /* Toggle IO reset bit */ |
| 29 | static void toggle_io_reset(void) |
| 30 | { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 31 | u32 r32 = MCHBAR32(MC_INIT_STATE_G); |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 32 | MCHBAR32(MC_INIT_STATE_G) = r32 | (1 << 5); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 33 | udelay(1); |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 34 | MCHBAR32(MC_INIT_STATE_G) = r32 & ~(1 << 5); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 35 | udelay(1); |
| 36 | } |
| 37 | |
| 38 | static u32 get_XOVER_CLK(u8 rankmap) |
| 39 | { |
| 40 | return rankmap << 24; |
| 41 | } |
| 42 | |
| 43 | static u32 get_XOVER_CMD(u8 rankmap) |
| 44 | { |
| 45 | u32 reg; |
| 46 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 47 | /* Enable xover cmd */ |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 48 | reg = 1 << 14; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 49 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 50 | /* Enable xover ctl */ |
| 51 | if (rankmap & 0x03) |
| 52 | reg |= (1 << 17); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 53 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 54 | if (rankmap & 0x0c) |
| 55 | reg |= (1 << 26); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 56 | |
| 57 | return reg; |
| 58 | } |
| 59 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 60 | void dram_find_common_params(ramctr_timing *ctrl) |
| 61 | { |
| 62 | size_t valid_dimms; |
| 63 | int channel, slot; |
| 64 | dimm_info *dimms = &ctrl->info; |
| 65 | |
| 66 | ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1; |
| 67 | valid_dimms = 0; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 68 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 69 | FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 70 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 71 | const dimm_attr *dimm = &dimms->dimm[channel][slot]; |
| 72 | if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) |
| 73 | continue; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 74 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 75 | valid_dimms++; |
| 76 | |
| 77 | /* Find all possible CAS combinations */ |
| 78 | ctrl->cas_supported &= dimm->cas_supported; |
| 79 | |
| 80 | /* Find the smallest common latencies supported by all DIMMs */ |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 81 | ctrl->tCK = MAX(ctrl->tCK, dimm->tCK); |
| 82 | ctrl->tAA = MAX(ctrl->tAA, dimm->tAA); |
| 83 | ctrl->tWR = MAX(ctrl->tWR, dimm->tWR); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 84 | ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD); |
| 85 | ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 86 | ctrl->tRP = MAX(ctrl->tRP, dimm->tRP); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 87 | ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS); |
| 88 | ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC); |
| 89 | ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR); |
| 90 | ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP); |
| 91 | ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW); |
Dan Elkouby | dabebc3 | 2018-04-13 18:47:10 +0300 | [diff] [blame] | 92 | ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL); |
| 93 | ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (!ctrl->cas_supported) |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 97 | die("Unsupported DIMM combination. DIMMS do not support common CAS latency"); |
| 98 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 99 | if (!valid_dimms) |
| 100 | die("No valid DIMMs found"); |
| 101 | } |
| 102 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 103 | void dram_xover(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 104 | { |
| 105 | u32 reg; |
| 106 | int channel; |
| 107 | |
| 108 | FOR_ALL_CHANNELS { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 109 | /* Enable xover clk */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 110 | reg = get_XOVER_CLK(ctrl->rankmap[channel]); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 111 | printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg); |
| 112 | MCHBAR32(GDCRCKPICODE_ch(channel)) = reg; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 113 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 114 | /* Enable xover ctl & xover cmd */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 115 | reg = get_XOVER_CMD(ctrl->rankmap[channel]); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 116 | printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg); |
| 117 | MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Patrick Rudolph | 19c3dad | 2016-11-26 11:37:45 +0100 | [diff] [blame] | 121 | static void dram_odt_stretch(ramctr_timing *ctrl, int channel) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 122 | { |
Angel Pons | 89ae6b8 | 2020-03-21 13:23:32 +0100 | [diff] [blame] | 123 | u32 addr, stretch; |
Patrick Rudolph | 19c3dad | 2016-11-26 11:37:45 +0100 | [diff] [blame] | 124 | |
| 125 | stretch = ctrl->ref_card_offset[channel]; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 126 | /* |
| 127 | * ODT stretch: |
| 128 | * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel. |
| 129 | */ |
Angel Pons | 89ae6b8 | 2020-03-21 13:23:32 +0100 | [diff] [blame] | 130 | if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) { |
Patrick Rudolph | 19c3dad | 2016-11-26 11:37:45 +0100 | [diff] [blame] | 131 | if (stretch == 2) |
| 132 | stretch = 3; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 133 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 134 | addr = SCHED_SECOND_CBIT_ch(channel); |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 135 | MCHBAR32_AND_OR(addr, ~(0xf << 10), (stretch << 12) | (stretch << 10)); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 136 | printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, MCHBAR32(addr)); |
Patrick Rudolph | 19c3dad | 2016-11-26 11:37:45 +0100 | [diff] [blame] | 137 | } else { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 138 | addr = TC_OTHP_ch(channel); |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 139 | union tc_othp_reg tc_othp = { |
| 140 | .raw = MCHBAR32(addr), |
| 141 | }; |
| 142 | tc_othp.odt_delay_d0 = stretch; |
| 143 | tc_othp.odt_delay_d1 = stretch; |
| 144 | MCHBAR32(addr) = tc_othp.raw; |
Iru Cai | 89af71c | 2018-08-16 16:46:27 +0800 | [diff] [blame] | 145 | printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, MCHBAR32(addr)); |
Patrick Rudolph | 19c3dad | 2016-11-26 11:37:45 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | void dram_timing_regs(ramctr_timing *ctrl) |
| 150 | { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 151 | int channel; |
| 152 | |
Angel Pons | 8137806 | 2020-11-12 13:46:21 +0100 | [diff] [blame] | 153 | /* BIN parameters */ |
| 154 | const union tc_dbp_reg tc_dbp = { |
| 155 | .tRCD = ctrl->tRCD, |
| 156 | .tRP = ctrl->tRP, |
| 157 | .tAA = ctrl->CAS, |
| 158 | .tCWL = ctrl->CWL, |
| 159 | .tRAS = ctrl->tRAS, |
| 160 | }; |
| 161 | |
| 162 | /* Regular access parameters */ |
| 163 | const union tc_rap_reg tc_rap = { |
| 164 | .tRRD = ctrl->tRRD, |
| 165 | .tRTP = ctrl->tRTP, |
| 166 | .tCKE = ctrl->tCKE, |
| 167 | .tWTR = ctrl->tWTR, |
| 168 | .tFAW = ctrl->tFAW, |
| 169 | .tWR = ctrl->tWR, |
| 170 | .tCMD = 3, |
| 171 | }; |
| 172 | |
| 173 | /* Other parameters */ |
| 174 | const union tc_othp_reg tc_othp = { |
| 175 | .tXPDLL = ctrl->tXPDLL, |
| 176 | .tXP = ctrl->tXP, |
| 177 | .tAONPD = ctrl->tAONPD, |
| 178 | .tCPDED = 2, |
Angel Pons | 2ad03a4 | 2020-11-19 11:07:27 +0100 | [diff] [blame] | 179 | .tPRPDEN = 1, |
Angel Pons | 8137806 | 2020-11-12 13:46:21 +0100 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | /* |
| 183 | * If tXP and tXPDLL are very high, we need to increase them by one. |
| 184 | * This can only happen on Ivy Bridge, and when overclocking the RAM. |
| 185 | */ |
| 186 | const union tc_dtp_reg tc_dtp = { |
| 187 | .overclock_tXP = ctrl->tXP >= 8, |
| 188 | .overclock_tXPDLL = ctrl->tXPDLL >= 32, |
| 189 | }; |
| 190 | |
| 191 | /* |
| 192 | * TC-Refresh timing parameters: |
| 193 | * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow |
| 194 | * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024. |
| 195 | */ |
| 196 | const u32 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK); |
| 197 | |
| 198 | const union tc_rftp_reg tc_rftp = { |
| 199 | .tREFI = ctrl->tREFI, |
| 200 | .tRFC = ctrl->tRFC, |
| 201 | .tREFIx9 = val32 / 1024, |
| 202 | }; |
| 203 | |
| 204 | /* Self-refresh timing parameters */ |
| 205 | const union tc_srftp_reg tc_srftp = { |
| 206 | .tXSDLL = tDLLK, |
| 207 | .tXS_offset = ctrl->tXSOffset, |
| 208 | .tZQOPER = tDLLK - ctrl->tXSOffset, |
| 209 | .tMOD = ctrl->tMOD - 8, |
| 210 | }; |
| 211 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 212 | FOR_ALL_CHANNELS { |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 213 | printram("DBP [%x] = %x\n", TC_DBP_ch(channel), tc_dbp.raw); |
| 214 | MCHBAR32(TC_DBP_ch(channel)) = tc_dbp.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 215 | |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 216 | printram("RAP [%x] = %x\n", TC_RAP_ch(channel), tc_rap.raw); |
| 217 | MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 218 | |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 219 | printram("OTHP [%x] = %x\n", TC_OTHP_ch(channel), tc_othp.raw); |
| 220 | MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 221 | |
Angel Pons | ca2f68a | 2020-03-22 13:15:12 +0100 | [diff] [blame] | 222 | if (IS_IVY_CPU(ctrl->cpu)) { |
Angel Pons | 8137806 | 2020-11-12 13:46:21 +0100 | [diff] [blame] | 223 | /* Debug parameters - only applies to Ivy Bridge */ |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 224 | MCHBAR32(TC_DTP_ch(channel)) = tc_dtp.raw; |
Angel Pons | ca2f68a | 2020-03-22 13:15:12 +0100 | [diff] [blame] | 225 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 226 | |
Patrick Rudolph | 19c3dad | 2016-11-26 11:37:45 +0100 | [diff] [blame] | 227 | dram_odt_stretch(ctrl, channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 228 | |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 229 | printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), tc_rftp.raw); |
| 230 | MCHBAR32(TC_RFTP_ch(channel)) = tc_rftp.raw; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 231 | |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 232 | union tc_rfp_reg tc_rfp = { |
| 233 | .raw = MCHBAR32(TC_RFP_ch(channel)), |
| 234 | }; |
| 235 | tc_rfp.oref_ri = 0xff; |
| 236 | MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 237 | |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 238 | printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), tc_srftp.raw); |
| 239 | MCHBAR32(TC_SRFTP_ch(channel)) = tc_srftp.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | void dram_dimm_mapping(ramctr_timing *ctrl) |
| 244 | { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 245 | int channel; |
| 246 | dimm_info *info = &ctrl->info; |
| 247 | |
| 248 | FOR_ALL_CHANNELS { |
Nico Huber | ac4f216 | 2017-10-01 18:14:43 +0200 | [diff] [blame] | 249 | dimm_attr *dimmA, *dimmB; |
| 250 | u32 reg = 0; |
| 251 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 252 | if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 253 | dimmA = &info->dimm[channel][0]; |
| 254 | dimmB = &info->dimm[channel][1]; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 255 | reg |= (0 << 16); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 256 | } else { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 257 | dimmA = &info->dimm[channel][1]; |
| 258 | dimmB = &info->dimm[channel][0]; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 259 | reg |= (1 << 16); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 260 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 261 | |
Nico Huber | ac4f216 | 2017-10-01 18:14:43 +0200 | [diff] [blame] | 262 | if (dimmA && (dimmA->ranks > 0)) { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 263 | reg |= (dimmA->size_mb / 256) << 0; |
| 264 | reg |= (dimmA->ranks - 1) << 17; |
Nico Huber | ac4f216 | 2017-10-01 18:14:43 +0200 | [diff] [blame] | 265 | reg |= (dimmA->width / 8 - 1) << 19; |
| 266 | } |
| 267 | |
| 268 | if (dimmB && (dimmB->ranks > 0)) { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 269 | reg |= (dimmB->size_mb / 256) << 8; |
| 270 | reg |= (dimmB->ranks - 1) << 18; |
Nico Huber | ac4f216 | 2017-10-01 18:14:43 +0200 | [diff] [blame] | 271 | reg |= (dimmB->width / 8 - 1) << 20; |
| 272 | } |
| 273 | |
Patrick Rudolph | 4e0cd82 | 2020-05-01 18:35:36 +0200 | [diff] [blame] | 274 | /* |
| 275 | * Rank interleave: Bit 16 of the physical address space sets |
| 276 | * the rank to use in a dual single rank DIMM configuration. |
| 277 | * That results in every 64KiB being interleaved between two ranks. |
| 278 | */ |
| 279 | reg |= 1 << 21; |
| 280 | /* Enhanced interleave */ |
| 281 | reg |= 1 << 22; |
Nico Huber | ac4f216 | 2017-10-01 18:14:43 +0200 | [diff] [blame] | 282 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 283 | if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 284 | ctrl->mad_dimm[channel] = reg; |
| 285 | } else { |
| 286 | ctrl->mad_dimm[channel] = 0; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 291 | void dram_dimm_set_mapping(ramctr_timing *ctrl, int training) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 292 | { |
| 293 | int channel; |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 294 | u32 ecc; |
| 295 | |
| 296 | if (ctrl->ecc_enabled) |
| 297 | ecc = training ? (1 << 24) : (3 << 24); |
| 298 | else |
| 299 | ecc = 0; |
| 300 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 301 | FOR_ALL_CHANNELS { |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 302 | MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 303 | } |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 304 | |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 305 | if (ctrl->ecc_enabled) |
| 306 | udelay(10); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 309 | void dram_zones(ramctr_timing *ctrl, int training) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 310 | { |
| 311 | u32 reg, ch0size, ch1size; |
| 312 | u8 val; |
| 313 | reg = 0; |
| 314 | val = 0; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 315 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 316 | if (training) { |
| 317 | ch0size = ctrl->channel_size_mb[0] ? 256 : 0; |
| 318 | ch1size = ctrl->channel_size_mb[1] ? 256 : 0; |
| 319 | } else { |
| 320 | ch0size = ctrl->channel_size_mb[0]; |
| 321 | ch1size = ctrl->channel_size_mb[1]; |
| 322 | } |
| 323 | |
| 324 | if (ch0size >= ch1size) { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 325 | reg = MCHBAR32(MAD_ZR); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 326 | val = ch1size / 256; |
| 327 | reg = (reg & ~0xff000000) | val << 24; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 328 | reg = (reg & ~0x00ff0000) | (2 * val) << 16; |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 329 | MCHBAR32(MAD_ZR) = reg; |
Felix Held | dee167e | 2019-12-30 17:30:16 +0100 | [diff] [blame] | 330 | MCHBAR32(MAD_CHNL) = 0x24; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 331 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 332 | } else { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 333 | reg = MCHBAR32(MAD_ZR); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 334 | val = ch0size / 256; |
| 335 | reg = (reg & ~0xff000000) | val << 24; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 336 | reg = (reg & ~0x00ff0000) | (2 * val) << 16; |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 337 | MCHBAR32(MAD_ZR) = reg; |
Felix Held | dee167e | 2019-12-30 17:30:16 +0100 | [diff] [blame] | 338 | MCHBAR32(MAD_CHNL) = 0x21; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 342 | #define DEFAULT_PCI_MMIO_SIZE 2048 |
| 343 | |
| 344 | static unsigned int get_mmio_size(void) |
| 345 | { |
| 346 | const struct device *dev; |
| 347 | const struct northbridge_intel_sandybridge_config *cfg = NULL; |
| 348 | |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 349 | dev = pcidev_path_on_root(PCI_DEVFN(0, 0)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 350 | if (dev) |
| 351 | cfg = dev->chip_info; |
| 352 | |
| 353 | /* If this is zero, it just means devicetree.cb didn't set it */ |
| 354 | if (!cfg || cfg->pci_mmio_size == 0) |
| 355 | return DEFAULT_PCI_MMIO_SIZE; |
| 356 | else |
| 357 | return cfg->pci_mmio_size; |
| 358 | } |
| 359 | |
Patrick Rudolph | 05d4bf7e | 2017-10-28 16:36:09 +0200 | [diff] [blame] | 360 | /* |
| 361 | * Returns the ECC mode the NB is running at. It takes precedence over ECC capability. |
| 362 | * The ME/PCU/.. has the ability to change this. |
| 363 | * Return 0: ECC is optional |
| 364 | * Return 1: ECC is forced |
| 365 | */ |
| 366 | bool get_host_ecc_forced(void) |
| 367 | { |
| 368 | /* read Capabilities A Register */ |
| 369 | const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A); |
| 370 | return !!(reg32 & (1 << 24)); |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Returns the ECC capability. |
| 375 | * The ME/PCU/.. has the ability to change this. |
| 376 | * Return 0: ECC is disabled |
| 377 | * Return 1: ECC is possible |
| 378 | */ |
| 379 | bool get_host_ecc_cap(void) |
| 380 | { |
| 381 | /* read Capabilities A Register */ |
| 382 | const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A); |
| 383 | return !(reg32 & (1 << 25)); |
| 384 | } |
| 385 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 386 | void dram_memorymap(ramctr_timing *ctrl, int me_uma_size) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 387 | { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 388 | u32 reg, val, reclaim, tom, gfxstolen, gttsize; |
| 389 | size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase; |
| 390 | size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 391 | uint16_t ggc; |
| 392 | |
| 393 | mmiosize = get_mmio_size(); |
| 394 | |
Felix Held | 87ddea2 | 2020-01-26 04:55:27 +0100 | [diff] [blame] | 395 | ggc = pci_read_config16(HOST_BRIDGE, GGC); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 396 | if (!(ggc & 2)) { |
| 397 | gfxstolen = ((ggc >> 3) & 0x1f) * 32; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 398 | gttsize = ((ggc >> 8) & 0x3); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 399 | } else { |
| 400 | gfxstolen = 0; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 401 | gttsize = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | tsegsize = CONFIG_SMM_TSEG_SIZE >> 20; |
| 405 | |
| 406 | tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1]; |
| 407 | |
| 408 | mestolenbase = tom - me_uma_size; |
| 409 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 410 | toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size); |
| 411 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 412 | gfxstolenbase = toludbase - gfxstolen; |
| 413 | gttbase = gfxstolenbase - gttsize; |
| 414 | |
| 415 | tsegbase = gttbase - tsegsize; |
| 416 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 417 | /* Round tsegbase down to nearest address aligned to tsegsize */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 418 | tsegbasedelta = tsegbase & (tsegsize - 1); |
| 419 | tsegbase &= ~(tsegsize - 1); |
| 420 | |
| 421 | gttbase -= tsegbasedelta; |
| 422 | gfxstolenbase -= tsegbasedelta; |
| 423 | toludbase -= tsegbasedelta; |
| 424 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 425 | /* Test if it is possible to reclaim a hole in the RAM addressing */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 426 | if (tom - me_uma_size > toludbase) { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 427 | /* Reclaim is possible */ |
| 428 | reclaim = 1; |
| 429 | remapbase = MAX(4096, tom - me_uma_size); |
| 430 | remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1; |
| 431 | touudbase = remaplimit + 1; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 432 | } else { |
Angel Pons | c728e25 | 2021-01-03 16:47:09 +0100 | [diff] [blame] | 433 | /* Reclaim not possible */ |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 434 | reclaim = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 435 | touudbase = tom - me_uma_size; |
| 436 | } |
| 437 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 438 | /* Update memory map in PCIe configuration space */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 439 | printk(BIOS_DEBUG, "Update PCI-E configuration space:\n"); |
| 440 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 441 | /* TOM (top of memory) */ |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 442 | reg = pci_read_config32(HOST_BRIDGE, TOM); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 443 | val = tom & 0xfff; |
| 444 | reg = (reg & ~0xfff00000) | (val << 20); |
Felix Held | 4902fee | 2019-12-28 18:09:47 +0100 | [diff] [blame] | 445 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 446 | pci_write_config32(HOST_BRIDGE, TOM, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 447 | |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 448 | reg = pci_read_config32(HOST_BRIDGE, TOM + 4); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 449 | val = tom & 0xfffff000; |
| 450 | reg = (reg & ~0x000fffff) | (val >> 12); |
Felix Held | 4902fee | 2019-12-28 18:09:47 +0100 | [diff] [blame] | 451 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 452 | pci_write_config32(HOST_BRIDGE, TOM + 4, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 453 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 454 | /* TOLUD (Top Of Low Usable DRAM) */ |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 455 | reg = pci_read_config32(HOST_BRIDGE, TOLUD); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 456 | val = toludbase & 0xfff; |
| 457 | reg = (reg & ~0xfff00000) | (val << 20); |
Felix Held | 4902fee | 2019-12-28 18:09:47 +0100 | [diff] [blame] | 458 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 459 | pci_write_config32(HOST_BRIDGE, TOLUD, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 460 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 461 | /* TOUUD LSB (Top Of Upper Usable DRAM) */ |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 462 | reg = pci_read_config32(HOST_BRIDGE, TOUUD); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 463 | val = touudbase & 0xfff; |
| 464 | reg = (reg & ~0xfff00000) | (val << 20); |
Felix Held | 4902fee | 2019-12-28 18:09:47 +0100 | [diff] [blame] | 465 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 466 | pci_write_config32(HOST_BRIDGE, TOUUD, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 467 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 468 | /* TOUUD MSB */ |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 469 | reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 470 | val = touudbase & 0xfffff000; |
| 471 | reg = (reg & ~0x000fffff) | (val >> 12); |
Felix Held | 4902fee | 2019-12-28 18:09:47 +0100 | [diff] [blame] | 472 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 473 | pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 474 | |
| 475 | if (reclaim) { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 476 | /* REMAP BASE */ |
| 477 | pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 478 | pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 479 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 480 | /* REMAP LIMIT */ |
| 481 | pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 482 | pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 483 | } |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 484 | /* TSEG */ |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 485 | reg = pci_read_config32(HOST_BRIDGE, TSEGMB); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 486 | val = tsegbase & 0xfff; |
| 487 | reg = (reg & ~0xfff00000) | (val << 20); |
Felix Held | 4902fee | 2019-12-28 18:09:47 +0100 | [diff] [blame] | 488 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 489 | pci_write_config32(HOST_BRIDGE, TSEGMB, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 490 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 491 | /* GFX stolen memory */ |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 492 | reg = pci_read_config32(HOST_BRIDGE, BDSM); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 493 | val = gfxstolenbase & 0xfff; |
| 494 | reg = (reg & ~0xfff00000) | (val << 20); |
Felix Held | 4902fee | 2019-12-28 18:09:47 +0100 | [diff] [blame] | 495 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 496 | pci_write_config32(HOST_BRIDGE, BDSM, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 497 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 498 | /* GTT stolen memory */ |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 499 | reg = pci_read_config32(HOST_BRIDGE, BGSM); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 500 | val = gttbase & 0xfff; |
| 501 | reg = (reg & ~0xfff00000) | (val << 20); |
Felix Held | 4902fee | 2019-12-28 18:09:47 +0100 | [diff] [blame] | 502 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 503 | pci_write_config32(HOST_BRIDGE, BGSM, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 504 | |
| 505 | if (me_uma_size) { |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 506 | reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 507 | val = (0x80000 - me_uma_size) & 0xfffff000; |
| 508 | reg = (reg & ~0x000fffff) | (val >> 12); |
Felix Held | 651f99f | 2019-12-30 16:28:48 +0100 | [diff] [blame] | 509 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 510 | pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 511 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 512 | /* ME base */ |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 513 | reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 514 | val = mestolenbase & 0xfff; |
| 515 | reg = (reg & ~0xfff00000) | (val << 20); |
Felix Held | 651f99f | 2019-12-30 16:28:48 +0100 | [diff] [blame] | 516 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 517 | pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 518 | |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 519 | reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 520 | val = mestolenbase & 0xfffff000; |
| 521 | reg = (reg & ~0x000fffff) | (val >> 12); |
Felix Held | 651f99f | 2019-12-30 16:28:48 +0100 | [diff] [blame] | 522 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 523 | pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 524 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 525 | /* ME mask */ |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 526 | reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 527 | val = (0x80000 - me_uma_size) & 0xfff; |
| 528 | reg = (reg & ~0xfff00000) | (val << 20); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 529 | reg = reg | ME_STLEN_EN; /* Set ME memory enable */ |
| 530 | reg = reg | MELCK; /* Set lock bit on ME mem */ |
Felix Held | 651f99f | 2019-12-30 16:28:48 +0100 | [diff] [blame] | 531 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg); |
Angel Pons | b31d1d7 | 2020-01-10 01:35:09 +0100 | [diff] [blame] | 532 | pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 536 | static void write_reset(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 537 | { |
| 538 | int channel, slotrank; |
| 539 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 540 | /* Choose a populated channel */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 541 | channel = (ctrl->rankmap[0]) ? 0 : 1; |
| 542 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 543 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 544 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 545 | /* Choose a populated rank */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 546 | slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2; |
| 547 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 548 | iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 549 | |
Angel Pons | edd7cb4 | 2020-12-07 12:17:17 +0100 | [diff] [blame] | 550 | /* This is actually using the IOSAV state machine as a timer */ |
Angel Pons | 38d901e | 2020-05-02 23:50:43 +0200 | [diff] [blame] | 551 | iosav_run_queue(channel, 1, 1); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 552 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 553 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 554 | } |
| 555 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 556 | void dram_jedecreset(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 557 | { |
Felix Held | 9fe248f | 2018-07-31 20:59:45 +0200 | [diff] [blame] | 558 | u32 reg; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 559 | int channel; |
| 560 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 561 | while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16))) |
| 562 | ; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 563 | do { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 564 | reg = MCHBAR32(IOSAV_STATUS_ch(0)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 565 | } while ((reg & 0x14) == 0); |
| 566 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 567 | /* Set state of memory controller */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 568 | reg = 0x112; |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 569 | MCHBAR32(MC_INIT_STATE_G) = reg; |
| 570 | MCHBAR32(MC_INIT_STATE) = 0; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 571 | reg |= 2; /* DDR reset */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 572 | MCHBAR32(MC_INIT_STATE_G) = reg; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 573 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 574 | /* Assert DIMM reset signal */ |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 575 | MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 576 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 577 | /* Wait 200us */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 578 | udelay(200); |
| 579 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 580 | /* Deassert DIMM reset signal */ |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 581 | MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 582 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 583 | /* Wait 500us */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 584 | udelay(500); |
| 585 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 586 | /* Enable DCLK */ |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 587 | MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 588 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 589 | /* XXX Wait 20ns */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 590 | udelay(1); |
| 591 | |
| 592 | FOR_ALL_CHANNELS { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 593 | /* Set valid rank CKE */ |
Felix Held | 9fe248f | 2018-07-31 20:59:45 +0200 | [diff] [blame] | 594 | reg = ctrl->rankmap[channel]; |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 595 | MCHBAR32(MC_INIT_STATE_ch(channel)) = reg; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 596 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 597 | /* Wait 10ns for ranks to settle */ |
| 598 | // udelay(0.01); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 599 | |
| 600 | reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 601 | MCHBAR32(MC_INIT_STATE_ch(channel)) = reg; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 602 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 603 | /* Write reset using a NOP */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 604 | write_reset(ctrl); |
| 605 | } |
| 606 | } |
| 607 | |
Angel Pons | 3d3bf48 | 2020-11-14 16:18:15 +0100 | [diff] [blame] | 608 | /* |
| 609 | * DDR3 Rank1 Address mirror swap the following pins: |
| 610 | * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1 |
| 611 | */ |
| 612 | static void ddr3_mirror_mrreg(int *bank, u32 *addr) |
| 613 | { |
| 614 | *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2); |
| 615 | *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1); |
| 616 | } |
| 617 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 618 | static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 619 | { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 620 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 621 | |
Angel Pons | 3d3bf48 | 2020-11-14 16:18:15 +0100 | [diff] [blame] | 622 | if (ctrl->rank_mirror[channel][slotrank]) |
| 623 | ddr3_mirror_mrreg(®, &val); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 624 | |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 625 | const struct iosav_ssq sequence[] = { |
| 626 | /* DRAM command MRS */ |
| 627 | [0] = { |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 628 | .sp_cmd_ctrl = { |
| 629 | .command = IOSAV_MRS, |
| 630 | }, |
| 631 | .subseq_ctrl = { |
| 632 | .cmd_executions = 1, |
| 633 | .cmd_delay_gap = 4, |
| 634 | .post_ssq_wait = 4, |
| 635 | .data_direction = SSQ_NA, |
| 636 | }, |
| 637 | .sp_cmd_addr = { |
| 638 | .address = val, |
| 639 | .rowbits = 6, |
| 640 | .bank = reg, |
| 641 | .rank = slotrank, |
| 642 | }, |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 643 | }, |
| 644 | /* DRAM command MRS */ |
| 645 | [1] = { |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 646 | .sp_cmd_ctrl = { |
| 647 | .command = IOSAV_MRS, |
| 648 | .ranksel_ap = 1, |
| 649 | }, |
| 650 | .subseq_ctrl = { |
| 651 | .cmd_executions = 1, |
| 652 | .cmd_delay_gap = 4, |
| 653 | .post_ssq_wait = 4, |
| 654 | .data_direction = SSQ_NA, |
| 655 | }, |
| 656 | .sp_cmd_addr = { |
| 657 | .address = val, |
| 658 | .rowbits = 6, |
| 659 | .bank = reg, |
| 660 | .rank = slotrank, |
| 661 | }, |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 662 | }, |
| 663 | /* DRAM command MRS */ |
| 664 | [2] = { |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 665 | .sp_cmd_ctrl = { |
Angel Pons | fd9a8b6 | 2020-11-13 13:56:30 +0100 | [diff] [blame] | 666 | .command = IOSAV_MRS, |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 667 | }, |
| 668 | .subseq_ctrl = { |
| 669 | .cmd_executions = 1, |
| 670 | .cmd_delay_gap = 4, |
| 671 | .post_ssq_wait = ctrl->tMOD, |
| 672 | .data_direction = SSQ_NA, |
| 673 | }, |
| 674 | .sp_cmd_addr = { |
| 675 | .address = val, |
| 676 | .rowbits = 6, |
| 677 | .bank = reg, |
| 678 | .rank = slotrank, |
| 679 | }, |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 680 | }, |
| 681 | }; |
| 682 | iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence)); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 683 | |
Angel Pons | 9f4ed3b | 2020-12-07 12:34:36 +0100 | [diff] [blame] | 684 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 685 | } |
| 686 | |
Angel Pons | 09fc4b9 | 2020-11-19 12:02:07 +0100 | [diff] [blame] | 687 | /* Obtain optimal power down mode for current configuration */ |
| 688 | static enum pdwm_mode get_power_down_mode(ramctr_timing *ctrl) |
| 689 | { |
| 690 | if (ctrl->tXP > 8) |
| 691 | return PDM_NONE; |
| 692 | |
| 693 | if (ctrl->tXPDLL > 32) |
| 694 | return PDM_PPD; |
| 695 | |
| 696 | if (CONFIG(RAMINIT_ALWAYS_ALLOW_DLL_OFF) || get_platform_type() == PLATFORM_MOBILE) |
| 697 | return PDM_DLL_OFF; |
| 698 | |
| 699 | return PDM_APD_PPD; |
| 700 | } |
| 701 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 702 | static u32 make_mr0(ramctr_timing *ctrl, u8 rank) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 703 | { |
| 704 | u16 mr0reg, mch_cas, mch_wr; |
| 705 | static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 }; |
Angel Pons | 09fc4b9 | 2020-11-19 12:02:07 +0100 | [diff] [blame] | 706 | |
| 707 | const enum pdwm_mode power_down = get_power_down_mode(ctrl); |
| 708 | |
| 709 | const bool slow_exit = power_down == PDM_DLL_OFF || power_down == PDM_APD_DLL_OFF; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 710 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 711 | /* Convert CAS to MCH register friendly */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 712 | if (ctrl->CAS < 12) { |
| 713 | mch_cas = (u16) ((ctrl->CAS - 4) << 1); |
| 714 | } else { |
| 715 | mch_cas = (u16) (ctrl->CAS - 12); |
| 716 | mch_cas = ((mch_cas << 1) | 0x1); |
| 717 | } |
| 718 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 719 | /* Convert tWR to MCH register friendly */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 720 | mch_wr = mch_wr_t[ctrl->tWR - 5]; |
| 721 | |
Angel Pons | 2bf28ed | 2020-11-12 13:49:59 +0100 | [diff] [blame] | 722 | /* DLL Reset - self clearing - set after CLK frequency has been changed */ |
| 723 | mr0reg = 1 << 8; |
| 724 | |
| 725 | mr0reg |= (mch_cas & 0x1) << 2; |
| 726 | mr0reg |= (mch_cas & 0xe) << 3; |
| 727 | mr0reg |= mch_wr << 9; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 728 | |
Angel Pons | 09fc4b9 | 2020-11-19 12:02:07 +0100 | [diff] [blame] | 729 | /* Precharge PD - Use slow exit when DLL-off is used - mostly power-saving feature */ |
| 730 | mr0reg |= !slow_exit << 12; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 731 | return mr0reg; |
| 732 | } |
| 733 | |
| 734 | static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel) |
| 735 | { |
Felix Held | 2bb3cdf | 2018-07-28 00:23:59 +0200 | [diff] [blame] | 736 | write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 737 | } |
| 738 | |
Angel Pons | f999748 | 2020-11-12 16:02:52 +0100 | [diff] [blame] | 739 | static odtmap get_ODT(ramctr_timing *ctrl, int channel) |
Angel Pons | 1a9b5aa | 2020-11-12 13:51:46 +0100 | [diff] [blame] | 740 | { |
| 741 | /* Get ODT based on rankmap */ |
| 742 | int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1); |
| 743 | |
| 744 | if (dimms_per_ch == 1) { |
| 745 | return (const odtmap){60, 60}; |
| 746 | } else { |
| 747 | return (const odtmap){120, 30}; |
| 748 | } |
| 749 | } |
| 750 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 751 | static u32 encode_odt(u32 odt) |
| 752 | { |
| 753 | switch (odt) { |
| 754 | case 30: |
Angel Pons | c728e25 | 2021-01-03 16:47:09 +0100 | [diff] [blame] | 755 | return (1 << 9) | (1 << 2); /* RZQ/8, RZQ/4 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 756 | case 60: |
Angel Pons | c728e25 | 2021-01-03 16:47:09 +0100 | [diff] [blame] | 757 | return (1 << 2); /* RZQ/4 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 758 | case 120: |
Angel Pons | c728e25 | 2021-01-03 16:47:09 +0100 | [diff] [blame] | 759 | return (1 << 6); /* RZQ/2 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 760 | default: |
| 761 | case 0: |
| 762 | return 0; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel) |
| 767 | { |
| 768 | odtmap odt; |
| 769 | u32 mr1reg; |
| 770 | |
Angel Pons | f999748 | 2020-11-12 16:02:52 +0100 | [diff] [blame] | 771 | odt = get_ODT(ctrl, channel); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 772 | mr1reg = 2; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 773 | |
| 774 | mr1reg |= encode_odt(odt.rttnom); |
| 775 | |
| 776 | return mr1reg; |
| 777 | } |
| 778 | |
| 779 | static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel) |
| 780 | { |
| 781 | u16 mr1reg; |
| 782 | |
| 783 | mr1reg = make_mr1(ctrl, rank, channel); |
| 784 | |
| 785 | write_mrreg(ctrl, channel, rank, 1, mr1reg); |
| 786 | } |
| 787 | |
| 788 | static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel) |
| 789 | { |
Angel Pons | 868bca2 | 2020-11-13 13:38:04 +0100 | [diff] [blame] | 790 | const u16 pasr = 0; |
| 791 | const u16 cwl = ctrl->CWL - 5; |
| 792 | const odtmap odt = get_ODT(ctrl, channel); |
| 793 | |
Angel Pons | dca3cb5 | 2020-11-13 13:42:07 +0100 | [diff] [blame] | 794 | int srt = 0; |
Angel Pons | dca3cb5 | 2020-11-13 13:42:07 +0100 | [diff] [blame] | 795 | if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ) |
| 796 | srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 797 | |
Angel Pons | 868bca2 | 2020-11-13 13:38:04 +0100 | [diff] [blame] | 798 | u16 mr2reg = 0; |
| 799 | mr2reg |= pasr; |
| 800 | mr2reg |= cwl << 3; |
| 801 | mr2reg |= ctrl->auto_self_refresh << 6; |
| 802 | mr2reg |= srt << 7; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 803 | mr2reg |= (odt.rttwr / 60) << 9; |
| 804 | |
| 805 | write_mrreg(ctrl, channel, rank, 2, mr2reg); |
Angel Pons | 7f1363d | 2020-11-13 13:31:58 +0100 | [diff] [blame] | 806 | |
| 807 | /* Program MR2 shadow */ |
| 808 | u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel)); |
| 809 | |
| 810 | reg32 &= 3 << 14 | 3 << 6; |
| 811 | |
| 812 | reg32 |= mr2reg & ~(3 << 6); |
| 813 | |
Angel Pons | 927b1c0 | 2020-12-10 22:11:27 +0100 | [diff] [blame] | 814 | if (srt) |
| 815 | reg32 |= 1 << (rank / 2 + 6); |
| 816 | |
| 817 | if (ctrl->rank_mirror[channel][rank]) |
| 818 | reg32 |= 1 << (rank / 2 + 14); |
| 819 | |
Angel Pons | 7f1363d | 2020-11-13 13:31:58 +0100 | [diff] [blame] | 820 | MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel) |
| 824 | { |
| 825 | write_mrreg(ctrl, channel, rank, 3, 0); |
| 826 | } |
| 827 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 828 | void dram_mrscommands(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 829 | { |
| 830 | u8 slotrank; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 831 | int channel; |
| 832 | |
| 833 | FOR_ALL_POPULATED_CHANNELS { |
| 834 | FOR_ALL_POPULATED_RANKS { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 835 | /* MR2 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 836 | dram_mr2(ctrl, slotrank, channel); |
| 837 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 838 | /* MR3 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 839 | dram_mr3(ctrl, slotrank, channel); |
| 840 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 841 | /* MR1 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 842 | dram_mr1(ctrl, slotrank, channel); |
| 843 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 844 | /* MR0 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 845 | dram_mr0(ctrl, slotrank, channel); |
| 846 | } |
| 847 | } |
| 848 | |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 849 | const struct iosav_ssq zqcl_sequence[] = { |
| 850 | /* DRAM command NOP (without ODT nor chip selects) */ |
| 851 | [0] = { |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 852 | .sp_cmd_ctrl = { |
Angel Pons | fd9a8b6 | 2020-11-13 13:56:30 +0100 | [diff] [blame] | 853 | .command = IOSAV_NOP & ~(0xff << 8), |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 854 | }, |
| 855 | .subseq_ctrl = { |
| 856 | .cmd_executions = 1, |
| 857 | .cmd_delay_gap = 4, |
| 858 | .post_ssq_wait = 15, |
| 859 | .data_direction = SSQ_NA, |
| 860 | }, |
| 861 | .sp_cmd_addr = { |
| 862 | .address = 2, |
| 863 | .rowbits = 6, |
| 864 | .bank = 0, |
| 865 | .rank = 0, |
| 866 | }, |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 867 | }, |
| 868 | /* DRAM command ZQCL */ |
| 869 | [1] = { |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 870 | .sp_cmd_ctrl = { |
| 871 | .command = IOSAV_ZQCS, |
| 872 | .ranksel_ap = 1, |
| 873 | }, |
| 874 | .subseq_ctrl = { |
| 875 | .cmd_executions = 1, |
| 876 | .cmd_delay_gap = 4, |
| 877 | .post_ssq_wait = 400, |
| 878 | .data_direction = SSQ_NA, |
| 879 | }, |
| 880 | .sp_cmd_addr = { |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 881 | .address = 1 << 10, |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 882 | .rowbits = 6, |
| 883 | .bank = 0, |
| 884 | .rank = 0, |
| 885 | }, |
| 886 | .addr_update = { |
Angel Pons | fd9a8b6 | 2020-11-13 13:56:30 +0100 | [diff] [blame] | 887 | .inc_rank = 1, |
| 888 | .addr_wrap = 20, |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 889 | }, |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 890 | }, |
| 891 | }; |
| 892 | iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 893 | |
Angel Pons | 38d901e | 2020-05-02 23:50:43 +0200 | [diff] [blame] | 894 | iosav_run_queue(BROADCAST_CH, 4, 0); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 895 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 896 | FOR_ALL_CHANNELS { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 897 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 898 | } |
| 899 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 900 | /* Refresh enable */ |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 901 | MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 902 | |
| 903 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 904 | MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 905 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 906 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 907 | |
| 908 | slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2; |
| 909 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 910 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 911 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 912 | iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 913 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 914 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | |
Felix Held | 3b90603 | 2020-01-14 17:05:43 +0100 | [diff] [blame] | 918 | static const u32 lane_base[] = { |
| 919 | LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3, |
| 920 | LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7, |
| 921 | LANEBASE_ECC |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 922 | }; |
| 923 | |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 924 | /* Maximum delay for command, control, clock */ |
| 925 | #define CCC_MAX_PI (2 * QCLK_PI - 1) |
| 926 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 927 | void program_timings(ramctr_timing *ctrl, int channel) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 928 | { |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 929 | u32 reg_roundtrip_latency, reg_io_latency; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 930 | int lane; |
| 931 | int slotrank, slot; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 932 | |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 933 | u32 ctl_delay[NUM_SLOTS] = { 0 }; |
| 934 | int cmd_delay = 0; |
| 935 | |
| 936 | /* Enable CLK XOVER */ |
| 937 | u32 clk_pi_coding = get_XOVER_CLK(ctrl->rankmap[channel]); |
| 938 | u32 clk_logic_dly = 0; |
| 939 | |
| 940 | /* |
| 941 | * Apply command delay if desired setting is negative. Find the |
| 942 | * most negative value: 'cmd_delay' will be the absolute value. |
| 943 | */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 944 | FOR_ALL_POPULATED_RANKS { |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 945 | if (cmd_delay < -ctrl->timings[channel][slotrank].pi_coding) |
| 946 | cmd_delay = -ctrl->timings[channel][slotrank].pi_coding; |
| 947 | } |
| 948 | if (cmd_delay < 0) { |
| 949 | printk(BIOS_ERR, "C%d command delay underflow: %d\n", channel, cmd_delay); |
| 950 | cmd_delay = 0; |
| 951 | } |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 952 | if (cmd_delay > CCC_MAX_PI) { |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 953 | printk(BIOS_ERR, "C%d command delay overflow: %d\n", channel, cmd_delay); |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 954 | cmd_delay = CCC_MAX_PI; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 955 | } |
| 956 | |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 957 | /* Apply control and clock delay if desired setting is positive */ |
| 958 | if (cmd_delay == 0) { |
| 959 | for (slot = 0; slot < NUM_SLOTS; slot++) { |
| 960 | const int pi_coding_0 = ctrl->timings[channel][2 * slot + 0].pi_coding; |
| 961 | const int pi_coding_1 = ctrl->timings[channel][2 * slot + 1].pi_coding; |
| 962 | |
| 963 | const u8 slot_map = (ctrl->rankmap[channel] >> (2 * slot)) & 3; |
| 964 | |
| 965 | if (slot_map & 1) |
| 966 | ctl_delay[slot] += pi_coding_0 + cmd_delay; |
| 967 | |
| 968 | if (slot_map & 2) |
| 969 | ctl_delay[slot] += pi_coding_1 + cmd_delay; |
| 970 | |
| 971 | /* If both ranks in a slot are populated, use the average */ |
| 972 | if (slot_map == 3) |
| 973 | ctl_delay[slot] /= 2; |
| 974 | |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 975 | if (ctl_delay[slot] > CCC_MAX_PI) { |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 976 | printk(BIOS_ERR, "C%dS%d control delay overflow: %d\n", |
| 977 | channel, slot, ctl_delay[slot]); |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 978 | ctl_delay[slot] = CCC_MAX_PI; |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 979 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 980 | } |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 981 | FOR_ALL_POPULATED_RANKS { |
| 982 | u32 clk_delay = ctrl->timings[channel][slotrank].pi_coding + cmd_delay; |
| 983 | |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 984 | if (clk_delay > CCC_MAX_PI) { |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 985 | printk(BIOS_ERR, "C%dR%d clock delay overflow: %d\n", |
| 986 | channel, slotrank, clk_delay); |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 987 | clk_delay = CCC_MAX_PI; |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 988 | } |
| 989 | |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 990 | clk_pi_coding |= (clk_delay % QCLK_PI) << (6 * slotrank); |
| 991 | clk_logic_dly |= (clk_delay / QCLK_PI) << slotrank; |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 992 | } |
| 993 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 994 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 995 | /* Enable CMD XOVER */ |
Angel Pons | 737f111 | 2020-11-13 14:07:30 +0100 | [diff] [blame] | 996 | union gdcr_cmd_pi_coding_reg cmd_pi_coding = { |
| 997 | .raw = get_XOVER_CMD(ctrl->rankmap[channel]), |
| 998 | }; |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 999 | cmd_pi_coding.cmd_pi_code = cmd_delay % QCLK_PI; |
| 1000 | cmd_pi_coding.cmd_logic_delay = cmd_delay / QCLK_PI; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1001 | |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1002 | cmd_pi_coding.ctl_pi_code_d0 = ctl_delay[0] % QCLK_PI; |
| 1003 | cmd_pi_coding.ctl_pi_code_d1 = ctl_delay[1] % QCLK_PI; |
| 1004 | cmd_pi_coding.ctl_logic_delay_d0 = ctl_delay[0] / QCLK_PI; |
| 1005 | cmd_pi_coding.ctl_logic_delay_d1 = ctl_delay[1] / QCLK_PI; |
Angel Pons | 737f111 | 2020-11-13 14:07:30 +0100 | [diff] [blame] | 1006 | |
| 1007 | MCHBAR32(GDCRCMDPICODING_ch(channel)) = cmd_pi_coding.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1008 | |
Angel Pons | 7584e55 | 2020-11-19 21:34:32 +0100 | [diff] [blame] | 1009 | MCHBAR32(GDCRCKPICODE_ch(channel)) = clk_pi_coding; |
| 1010 | MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = clk_logic_dly; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1011 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1012 | reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel)); |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 1013 | reg_io_latency &= ~0xffff; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1014 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1015 | reg_roundtrip_latency = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1016 | |
| 1017 | FOR_ALL_POPULATED_RANKS { |
Angel Pons | 075d123 | 2020-11-19 21:50:33 +0100 | [diff] [blame] | 1018 | reg_io_latency |= ctrl->timings[channel][slotrank].io_latency << (4 * slotrank); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1019 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1020 | reg_roundtrip_latency |= |
Angel Pons | 075d123 | 2020-11-19 21:50:33 +0100 | [diff] [blame] | 1021 | ctrl->timings[channel][slotrank].roundtrip_latency << (8 * slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1022 | |
| 1023 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1024 | const u16 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven; |
| 1025 | const u8 dqs_p = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p; |
| 1026 | const u8 dqs_n = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n; |
Angel Pons | 9fcc110 | 2020-11-19 22:23:13 +0100 | [diff] [blame] | 1027 | const union gdcr_rx_reg gdcr_rx = { |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1028 | .rcven_pi_code = rcven % QCLK_PI, |
Angel Pons | 9fcc110 | 2020-11-19 22:23:13 +0100 | [diff] [blame] | 1029 | .rx_dqs_p_pi_code = dqs_p, |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1030 | .rcven_logic_delay = rcven / QCLK_PI, |
Angel Pons | 9fcc110 | 2020-11-19 22:23:13 +0100 | [diff] [blame] | 1031 | .rx_dqs_n_pi_code = dqs_n, |
| 1032 | }; |
| 1033 | MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) = gdcr_rx.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1034 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1035 | const u16 tx_dqs = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs; |
| 1036 | const int tx_dq = ctrl->timings[channel][slotrank].lanes[lane].tx_dq; |
Angel Pons | 9fcc110 | 2020-11-19 22:23:13 +0100 | [diff] [blame] | 1037 | const union gdcr_tx_reg gdcr_tx = { |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1038 | .tx_dq_pi_code = tx_dq % QCLK_PI, |
| 1039 | .tx_dqs_pi_code = tx_dqs % QCLK_PI, |
| 1040 | .tx_dqs_logic_delay = tx_dqs / QCLK_PI, |
| 1041 | .tx_dq_logic_delay = tx_dq / QCLK_PI, |
Angel Pons | 9fcc110 | 2020-11-19 22:23:13 +0100 | [diff] [blame] | 1042 | }; |
| 1043 | MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) = gdcr_tx.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1044 | } |
| 1045 | } |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1046 | MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency; |
| 1047 | MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1048 | } |
| 1049 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1050 | static void test_rcven(ramctr_timing *ctrl, int channel, int slotrank) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1051 | { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1052 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1053 | |
Angel Pons | 3aed6ac | 2020-12-07 02:00:41 +0100 | [diff] [blame] | 1054 | /* Send a burst of 16 back-to-back read commands (4 DCLK apart) */ |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 1055 | iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1056 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 1057 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1058 | } |
| 1059 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1060 | static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1061 | { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1062 | u32 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1063 | |
| 1064 | return (MCHBAR32(lane_base[lane] + |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1065 | GDCRTRAININGRESULT(channel, (rcven / 32) & 1)) >> (rcven % 32)) & 1; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | struct run { |
| 1069 | int middle; |
| 1070 | int end; |
| 1071 | int start; |
| 1072 | int all; |
| 1073 | int length; |
| 1074 | }; |
| 1075 | |
| 1076 | static struct run get_longest_zero_run(int *seq, int sz) |
| 1077 | { |
| 1078 | int i, ls; |
| 1079 | int bl = 0, bs = 0; |
| 1080 | struct run ret; |
| 1081 | |
| 1082 | ls = 0; |
| 1083 | for (i = 0; i < 2 * sz; i++) |
| 1084 | if (seq[i % sz]) { |
| 1085 | if (i - ls > bl) { |
| 1086 | bl = i - ls; |
| 1087 | bs = ls; |
| 1088 | } |
| 1089 | ls = i + 1; |
| 1090 | } |
| 1091 | if (bl == 0) { |
| 1092 | ret.middle = sz / 2; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1093 | ret.start = 0; |
| 1094 | ret.end = sz; |
Jacob Garber | e0c181d | 2019-04-08 22:21:43 -0600 | [diff] [blame] | 1095 | ret.length = sz; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1096 | ret.all = 1; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1097 | return ret; |
| 1098 | } |
| 1099 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1100 | ret.start = bs % sz; |
| 1101 | ret.end = (bs + bl - 1) % sz; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1102 | ret.middle = (bs + (bl - 1) / 2) % sz; |
| 1103 | ret.length = bl; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1104 | ret.all = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1105 | |
| 1106 | return ret; |
| 1107 | } |
| 1108 | |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1109 | #define RCVEN_COARSE_PI_LENGTH (2 * QCLK_PI) |
| 1110 | |
Angel Pons | f305339 | 2020-11-13 23:31:12 +0100 | [diff] [blame] | 1111 | static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1112 | { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1113 | int rcven; |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1114 | int statistics[NUM_LANES][RCVEN_COARSE_PI_LENGTH]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1115 | int lane; |
| 1116 | |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1117 | for (rcven = 0; rcven < RCVEN_COARSE_PI_LENGTH; rcven++) { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1118 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1119 | ctrl->timings[channel][slotrank].lanes[lane].rcven = rcven; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1120 | } |
| 1121 | program_timings(ctrl, channel); |
| 1122 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1123 | test_rcven(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1124 | |
| 1125 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1126 | statistics[lane][rcven] = |
| 1127 | !does_lane_work(ctrl, channel, slotrank, lane); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | FOR_ALL_LANES { |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1131 | struct run rn = get_longest_zero_run(statistics[lane], RCVEN_COARSE_PI_LENGTH); |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1132 | ctrl->timings[channel][slotrank].lanes[lane].rcven = rn.middle; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1133 | upperA[lane] = rn.end; |
| 1134 | if (upperA[lane] < rn.middle) |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1135 | upperA[lane] += 2 * QCLK_PI; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1136 | |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 1137 | printram("rcven: %d, %d, %d: % 4d-% 4d-% 4d\n", |
Felix Held | 2bb3cdf | 2018-07-28 00:23:59 +0200 | [diff] [blame] | 1138 | channel, slotrank, lane, rn.start, rn.middle, rn.end); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
Angel Pons | f305339 | 2020-11-13 23:31:12 +0100 | [diff] [blame] | 1142 | static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1143 | { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1144 | int rcven_delta; |
Angel Pons | 86e3d74 | 2021-01-03 14:55:12 +0100 | [diff] [blame] | 1145 | int statistics[NUM_LANES][51] = {0}; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1146 | int lane, i; |
| 1147 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1148 | for (rcven_delta = -25; rcven_delta <= 25; rcven_delta++) { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1149 | |
| 1150 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1151 | ctrl->timings[channel][slotrank].lanes[lane].rcven |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1152 | = upperA[lane] + rcven_delta + QCLK_PI; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1153 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1154 | program_timings(ctrl, channel); |
| 1155 | |
| 1156 | for (i = 0; i < 100; i++) { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1157 | test_rcven(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1158 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1159 | statistics[lane][rcven_delta + 25] += |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1160 | does_lane_work(ctrl, channel, slotrank, lane); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | FOR_ALL_LANES { |
| 1165 | int last_zero, first_all; |
| 1166 | |
| 1167 | for (last_zero = -25; last_zero <= 25; last_zero++) |
| 1168 | if (statistics[lane][last_zero + 25]) |
| 1169 | break; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1170 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1171 | last_zero--; |
| 1172 | for (first_all = -25; first_all <= 25; first_all++) |
| 1173 | if (statistics[lane][first_all + 25] == 100) |
| 1174 | break; |
| 1175 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1176 | printram("lane %d: %d, %d\n", lane, last_zero, first_all); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1177 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1178 | ctrl->timings[channel][slotrank].lanes[lane].rcven = |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1179 | (last_zero + first_all) / 2 + upperA[lane]; |
| 1180 | |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 1181 | printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank, |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1182 | lane, ctrl->timings[channel][slotrank].lanes[lane].rcven); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | |
Angel Pons | 3aed6ac | 2020-12-07 02:00:41 +0100 | [diff] [blame] | 1186 | /* |
| 1187 | * Once the DQS high phase has been found (for each DRAM) the next stage |
| 1188 | * is to find out the round trip latency, by locating the preamble cycle. |
| 1189 | * This is achieved by trying smaller and smaller roundtrip values until |
| 1190 | * the strobe sampling is done on the preamble cycle. |
| 1191 | */ |
Angel Pons | f305339 | 2020-11-13 23:31:12 +0100 | [diff] [blame] | 1192 | static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1193 | { |
| 1194 | int works[NUM_LANES]; |
| 1195 | int lane; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1196 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1197 | while (1) { |
| 1198 | int all_works = 1, some_works = 0; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1199 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1200 | program_timings(ctrl, channel); |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1201 | test_rcven(ctrl, channel, slotrank); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1202 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1203 | FOR_ALL_LANES { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1204 | works[lane] = !does_lane_work(ctrl, channel, slotrank, lane); |
| 1205 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1206 | if (works[lane]) |
| 1207 | some_works = 1; |
| 1208 | else |
| 1209 | all_works = 0; |
| 1210 | } |
Angel Pons | 3aed6ac | 2020-12-07 02:00:41 +0100 | [diff] [blame] | 1211 | |
| 1212 | /* If every lane is working, exit */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1213 | if (all_works) |
| 1214 | return 0; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1215 | |
Angel Pons | 3aed6ac | 2020-12-07 02:00:41 +0100 | [diff] [blame] | 1216 | /* |
| 1217 | * If all bits are one (everyone is failing), decrement |
| 1218 | * the roundtrip value by two, and do another iteration. |
| 1219 | */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1220 | if (!some_works) { |
Angel Pons | 3aed6ac | 2020-12-07 02:00:41 +0100 | [diff] [blame] | 1221 | /* Guard against roundtrip latency underflow */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1222 | if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) { |
Angel Pons | 3079163 | 2020-12-12 12:28:29 +0100 | [diff] [blame] | 1223 | printk(BIOS_EMERG, "Roundtrip latency underflow: %d, %d\n", |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1224 | channel, slotrank); |
| 1225 | return MAKE_ERR; |
| 1226 | } |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1227 | ctrl->timings[channel][slotrank].roundtrip_latency -= 2; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1228 | printram("4024 -= 2;\n"); |
| 1229 | continue; |
| 1230 | } |
Angel Pons | 3aed6ac | 2020-12-07 02:00:41 +0100 | [diff] [blame] | 1231 | |
| 1232 | /* |
| 1233 | * Else (if some lanes are failing), increase the rank's |
| 1234 | * I/O latency by 2, and increase rcven logic delay by 2 |
| 1235 | * on the working lanes, then perform another iteration. |
| 1236 | */ |
Felix Held | ef4fe3e | 2019-12-31 14:15:05 +0100 | [diff] [blame] | 1237 | ctrl->timings[channel][slotrank].io_latency += 2; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1238 | printram("4028 += 2;\n"); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1239 | |
Angel Pons | 3aed6ac | 2020-12-07 02:00:41 +0100 | [diff] [blame] | 1240 | /* Guard against I/O latency overflow */ |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 1241 | if (ctrl->timings[channel][slotrank].io_latency >= 16) { |
Angel Pons | 3079163 | 2020-12-12 12:28:29 +0100 | [diff] [blame] | 1242 | printk(BIOS_EMERG, "I/O latency overflow: %d, %d\n", |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1243 | channel, slotrank); |
| 1244 | return MAKE_ERR; |
| 1245 | } |
| 1246 | FOR_ALL_LANES if (works[lane]) { |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1247 | ctrl->timings[channel][slotrank].lanes[lane].rcven += 2 * QCLK_PI; |
| 1248 | upperA[lane] += 2 * QCLK_PI; |
Angel Pons | 891f2bc | 2020-01-10 01:27:28 +0100 | [diff] [blame] | 1249 | printram("increment %d, %d, %d\n", channel, slotrank, lane); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1255 | static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1256 | { |
| 1257 | int lane; |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1258 | u16 logic_delay_min = 7; |
| 1259 | u16 logic_delay_max = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1260 | |
| 1261 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1262 | const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6; |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1263 | |
| 1264 | logic_delay_min = MIN(logic_delay_min, logic_delay); |
| 1265 | logic_delay_max = MAX(logic_delay_max, logic_delay); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1266 | } |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1267 | |
| 1268 | if (logic_delay_max < logic_delay_min) { |
| 1269 | printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n", |
| 1270 | logic_delay_max, logic_delay_min, channel, slotrank); |
| 1271 | } |
| 1272 | |
| 1273 | assert(logic_delay_max >= logic_delay_min); |
| 1274 | |
| 1275 | return logic_delay_max - logic_delay_min; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1278 | static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1279 | { |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1280 | int latency_offset = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1281 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1282 | /* Get changed maxima */ |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1283 | const int post = get_logic_delay_delta(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1284 | |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1285 | if (prev < post) |
| 1286 | latency_offset = +1; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1287 | |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1288 | else if (prev > post) |
| 1289 | latency_offset = -1; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1290 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1291 | else |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1292 | latency_offset = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1293 | |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1294 | ctrl->timings[channel][slotrank].io_latency += latency_offset; |
| 1295 | ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset; |
| 1296 | printram("4024 += %d;\n", latency_offset); |
| 1297 | printram("4028 += %d;\n", latency_offset); |
| 1298 | |
| 1299 | return post; |
| 1300 | } |
| 1301 | |
| 1302 | static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank) |
| 1303 | { |
| 1304 | u16 logic_delay_min = 7; |
| 1305 | int lane; |
| 1306 | |
| 1307 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1308 | const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6; |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1309 | |
| 1310 | logic_delay_min = MIN(logic_delay_min, logic_delay); |
| 1311 | } |
| 1312 | |
| 1313 | if (logic_delay_min >= 2) { |
| 1314 | printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n", |
| 1315 | logic_delay_min, channel, slotrank); |
| 1316 | } |
| 1317 | |
| 1318 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1319 | ctrl->timings[channel][slotrank].lanes[lane].rcven -= logic_delay_min << 6; |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1320 | } |
| 1321 | ctrl->timings[channel][slotrank].io_latency -= logic_delay_min; |
| 1322 | printram("4028 -= %d;\n", logic_delay_min); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1323 | } |
| 1324 | |
Angel Pons | 7f5a97c | 2020-11-13 16:58:46 +0100 | [diff] [blame] | 1325 | int receive_enable_calibration(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1326 | { |
| 1327 | int channel, slotrank, lane; |
| 1328 | int err; |
| 1329 | |
| 1330 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
| 1331 | int all_high, some_high; |
| 1332 | int upperA[NUM_LANES]; |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1333 | int prev; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1334 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1335 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1336 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 1337 | iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 1338 | |
Angel Pons | 9f4ed3b | 2020-12-07 12:34:36 +0100 | [diff] [blame] | 1339 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1340 | |
Angel Pons | 58b609b | 2020-11-13 14:35:29 +0100 | [diff] [blame] | 1341 | const union gdcr_training_mod_reg training_mod = { |
| 1342 | .receive_enable_mode = 1, |
| 1343 | .training_rank_sel = slotrank, |
| 1344 | .odt_always_on = 1, |
| 1345 | }; |
| 1346 | MCHBAR32(GDCRTRAININGMOD) = training_mod.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1347 | |
Felix Held | ef4fe3e | 2019-12-31 14:15:05 +0100 | [diff] [blame] | 1348 | ctrl->timings[channel][slotrank].io_latency = 4; |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1349 | ctrl->timings[channel][slotrank].roundtrip_latency = 55; |
Felix Held | 2bb3cdf | 2018-07-28 00:23:59 +0200 | [diff] [blame] | 1350 | program_timings(ctrl, channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1351 | |
Angel Pons | f305339 | 2020-11-13 23:31:12 +0100 | [diff] [blame] | 1352 | find_rcven_pi_coarse(ctrl, channel, slotrank, upperA); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1353 | |
Felix Held | 2bb3cdf | 2018-07-28 00:23:59 +0200 | [diff] [blame] | 1354 | all_high = 1; |
| 1355 | some_high = 0; |
| 1356 | FOR_ALL_LANES { |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1357 | if (ctrl->timings[channel][slotrank].lanes[lane].rcven >= QCLK_PI) |
Felix Held | 2bb3cdf | 2018-07-28 00:23:59 +0200 | [diff] [blame] | 1358 | some_high = 1; |
| 1359 | else |
| 1360 | all_high = 0; |
| 1361 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1362 | |
| 1363 | if (all_high) { |
Felix Held | ef4fe3e | 2019-12-31 14:15:05 +0100 | [diff] [blame] | 1364 | ctrl->timings[channel][slotrank].io_latency--; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1365 | printram("4028--;\n"); |
| 1366 | FOR_ALL_LANES { |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1367 | ctrl->timings[channel][slotrank].lanes[lane].rcven -= QCLK_PI; |
| 1368 | upperA[lane] -= QCLK_PI; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1369 | |
| 1370 | } |
| 1371 | } else if (some_high) { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1372 | ctrl->timings[channel][slotrank].roundtrip_latency++; |
Felix Held | ef4fe3e | 2019-12-31 14:15:05 +0100 | [diff] [blame] | 1373 | ctrl->timings[channel][slotrank].io_latency++; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1374 | printram("4024++;\n"); |
| 1375 | printram("4028++;\n"); |
| 1376 | } |
| 1377 | |
| 1378 | program_timings(ctrl, channel); |
| 1379 | |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1380 | prev = get_logic_delay_delta(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1381 | |
Angel Pons | f305339 | 2020-11-13 23:31:12 +0100 | [diff] [blame] | 1382 | err = find_roundtrip_latency(ctrl, channel, slotrank, upperA); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1383 | if (err) |
| 1384 | return err; |
| 1385 | |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1386 | prev = align_rt_io_latency(ctrl, channel, slotrank, prev); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1387 | |
Angel Pons | f305339 | 2020-11-13 23:31:12 +0100 | [diff] [blame] | 1388 | fine_tune_rcven_pi(ctrl, channel, slotrank, upperA); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1389 | |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1390 | prev = align_rt_io_latency(ctrl, channel, slotrank, prev); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1391 | |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1392 | compute_final_logic_delay(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1393 | |
Angel Pons | 12bd8ab | 2020-11-13 23:10:52 +0100 | [diff] [blame] | 1394 | align_rt_io_latency(ctrl, channel, slotrank, prev); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1395 | |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 1396 | printram("4/8: %d, %d, % 4d, % 4d\n", channel, slotrank, |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1397 | ctrl->timings[channel][slotrank].roundtrip_latency, |
Felix Held | ef4fe3e | 2019-12-31 14:15:05 +0100 | [diff] [blame] | 1398 | ctrl->timings[channel][slotrank].io_latency); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1399 | |
| 1400 | printram("final results:\n"); |
| 1401 | FOR_ALL_LANES |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 1402 | printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank, lane, |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1403 | ctrl->timings[channel][slotrank].lanes[lane].rcven); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1404 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1405 | MCHBAR32(GDCRTRAININGMOD) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1406 | |
| 1407 | toggle_io_reset(); |
| 1408 | } |
| 1409 | |
| 1410 | FOR_ALL_POPULATED_CHANNELS { |
| 1411 | program_timings(ctrl, channel); |
| 1412 | } |
Angel Pons | c674223 | 2020-11-15 13:26:21 +0100 | [diff] [blame] | 1413 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1414 | return 0; |
| 1415 | } |
| 1416 | |
Angel Pons | 011661c | 2020-11-15 18:21:35 +0100 | [diff] [blame] | 1417 | static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1418 | { |
| 1419 | int lane; |
| 1420 | |
| 1421 | FOR_ALL_LANES { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1422 | MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0; |
| 1423 | MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1424 | } |
| 1425 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1426 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1427 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 1428 | iosav_write_misc_write_sequence(ctrl, channel, slotrank, |
| 1429 | MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1430 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 1431 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1432 | |
Angel Pons | 801a5cb | 2020-11-15 15:48:29 +0100 | [diff] [blame] | 1433 | iosav_write_prea_act_read_sequence(ctrl, channel, slotrank); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 1434 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 1435 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1436 | } |
| 1437 | |
Angel Pons | 011661c | 2020-11-15 18:21:35 +0100 | [diff] [blame] | 1438 | static void tx_dq_threshold_process(int *data, const int count) |
Tobias Diedrich | 87c4f11 | 2017-12-07 22:40:20 +0100 | [diff] [blame] | 1439 | { |
| 1440 | int min = data[0]; |
| 1441 | int max = min; |
| 1442 | int i; |
| 1443 | for (i = 1; i < count; i++) { |
| 1444 | if (min > data[i]) |
| 1445 | min = data[i]; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1446 | |
Tobias Diedrich | 87c4f11 | 2017-12-07 22:40:20 +0100 | [diff] [blame] | 1447 | if (max < data[i]) |
| 1448 | max = data[i]; |
| 1449 | } |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1450 | int threshold = min / 2 + max / 2; |
Tobias Diedrich | 87c4f11 | 2017-12-07 22:40:20 +0100 | [diff] [blame] | 1451 | for (i = 0; i < count; i++) |
| 1452 | data[i] = data[i] > threshold; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1453 | |
Angel Pons | 891f2bc | 2020-01-10 01:27:28 +0100 | [diff] [blame] | 1454 | printram("threshold=%d min=%d max=%d\n", threshold, min, max); |
Tobias Diedrich | 87c4f11 | 2017-12-07 22:40:20 +0100 | [diff] [blame] | 1455 | } |
| 1456 | |
Angel Pons | 011661c | 2020-11-15 18:21:35 +0100 | [diff] [blame] | 1457 | static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1458 | { |
Angel Pons | 011661c | 2020-11-15 18:21:35 +0100 | [diff] [blame] | 1459 | int tx_dq; |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1460 | int stats[NUM_LANES][MAX_TX_DQ + 1]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1461 | int lane; |
| 1462 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1463 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1464 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 1465 | iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 1466 | |
Angel Pons | 9f4ed3b | 2020-12-07 12:34:36 +0100 | [diff] [blame] | 1467 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1468 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1469 | for (tx_dq = 0; tx_dq <= MAX_TX_DQ; tx_dq++) { |
| 1470 | FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].tx_dq = tx_dq; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1471 | program_timings(ctrl, channel); |
| 1472 | |
Angel Pons | 011661c | 2020-11-15 18:21:35 +0100 | [diff] [blame] | 1473 | test_tx_dq(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1474 | |
| 1475 | FOR_ALL_LANES { |
Angel Pons | 011661c | 2020-11-15 18:21:35 +0100 | [diff] [blame] | 1476 | stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | FOR_ALL_LANES { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1480 | struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane])); |
| 1481 | |
Tobias Diedrich | 87c4f11 | 2017-12-07 22:40:20 +0100 | [diff] [blame] | 1482 | if (rn.all || rn.length < 8) { |
Angel Pons | 3079163 | 2020-12-12 12:28:29 +0100 | [diff] [blame] | 1483 | printk(BIOS_EMERG, "tx_dq write leveling failed: %d, %d, %d\n", |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1484 | channel, slotrank, lane); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1485 | /* |
| 1486 | * With command training not being done yet, the lane can be erroneous. |
| 1487 | * Take the average as reference and try again to find a run. |
Tobias Diedrich | 87c4f11 | 2017-12-07 22:40:20 +0100 | [diff] [blame] | 1488 | */ |
Angel Pons | 011661c | 2020-11-15 18:21:35 +0100 | [diff] [blame] | 1489 | tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane])); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1490 | rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane])); |
| 1491 | |
Tobias Diedrich | 87c4f11 | 2017-12-07 22:40:20 +0100 | [diff] [blame] | 1492 | if (rn.all || rn.length < 8) { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1493 | printk(BIOS_EMERG, "tx_dq recovery failed\n"); |
Tobias Diedrich | 87c4f11 | 2017-12-07 22:40:20 +0100 | [diff] [blame] | 1494 | return MAKE_ERR; |
| 1495 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1496 | } |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1497 | ctrl->timings[channel][slotrank].lanes[lane].tx_dq = rn.middle; |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 1498 | printram("tx_dq: %d, %d, %d: % 4d-% 4d-% 4d\n", |
Felix Held | 2bb3cdf | 2018-07-28 00:23:59 +0200 | [diff] [blame] | 1499 | channel, slotrank, lane, rn.start, rn.middle, rn.end); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1500 | } |
| 1501 | return 0; |
| 1502 | } |
| 1503 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1504 | static int get_precedening_channels(ramctr_timing *ctrl, int target_channel) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1505 | { |
| 1506 | int channel, ret = 0; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1507 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1508 | FOR_ALL_POPULATED_CHANNELS if (channel < target_channel) |
| 1509 | ret++; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1510 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1511 | return ret; |
| 1512 | } |
| 1513 | |
Angel Pons | 765d465 | 2020-11-11 14:44:35 +0100 | [diff] [blame] | 1514 | /* Each cacheline is 64 bits long */ |
| 1515 | static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines) |
| 1516 | { |
| 1517 | MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1; |
| 1518 | } |
| 1519 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1520 | static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1521 | { |
Subrata Banik | b1434fc | 2019-03-15 22:20:41 +0530 | [diff] [blame] | 1522 | unsigned int j; |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 1523 | unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1524 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1525 | for (j = 0; j < 16; j++) |
| 1526 | write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1527 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1528 | sfence(); |
Angel Pons | 765d465 | 2020-11-11 14:44:35 +0100 | [diff] [blame] | 1529 | |
| 1530 | program_wdb_pattern_length(channel, 8); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1531 | } |
| 1532 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1533 | static int num_of_channels(const ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1534 | { |
| 1535 | int ret = 0; |
| 1536 | int channel; |
| 1537 | FOR_ALL_POPULATED_CHANNELS ret++; |
| 1538 | return ret; |
| 1539 | } |
| 1540 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1541 | static void fill_pattern1(ramctr_timing *ctrl, int channel) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1542 | { |
Subrata Banik | b1434fc | 2019-03-15 22:20:41 +0530 | [diff] [blame] | 1543 | unsigned int j; |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 1544 | unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64; |
| 1545 | unsigned int channel_step = 64 * num_of_channels(ctrl); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1546 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1547 | for (j = 0; j < 16; j++) |
| 1548 | write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1549 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1550 | for (j = 0; j < 16; j++) |
| 1551 | write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1552 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1553 | sfence(); |
Angel Pons | 765d465 | 2020-11-11 14:44:35 +0100 | [diff] [blame] | 1554 | |
| 1555 | program_wdb_pattern_length(channel, 16); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1556 | } |
| 1557 | |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1558 | #define TX_DQS_PI_LENGTH (2 * QCLK_PI) |
| 1559 | |
Angel Pons | 820bce7 | 2020-11-14 17:02:55 +0100 | [diff] [blame] | 1560 | static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1561 | { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1562 | int tx_dqs; |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1563 | int statistics[NUM_LANES][TX_DQS_PI_LENGTH]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1564 | int lane; |
| 1565 | |
Angel Pons | 58b609b | 2020-11-13 14:35:29 +0100 | [diff] [blame] | 1566 | const union gdcr_training_mod_reg training_mod = { |
| 1567 | .write_leveling_mode = 1, |
| 1568 | .training_rank_sel = slotrank, |
| 1569 | .enable_dqs_wl = 5, |
| 1570 | .odt_always_on = 1, |
| 1571 | .force_drive_enable = 1, |
| 1572 | }; |
| 1573 | MCHBAR32(GDCRTRAININGMOD) = training_mod.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1574 | |
Angel Pons | c6d2fea | 2020-11-14 16:52:33 +0100 | [diff] [blame] | 1575 | u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7; |
| 1576 | int bank = 1; |
| 1577 | |
| 1578 | if (ctrl->rank_mirror[channel][slotrank]) |
| 1579 | ddr3_mirror_mrreg(&bank, &mr1reg); |
| 1580 | |
| 1581 | wait_for_iosav(channel); |
| 1582 | |
| 1583 | iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg); |
| 1584 | |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1585 | for (tx_dqs = 0; tx_dqs < TX_DQS_PI_LENGTH; tx_dqs++) { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1586 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1587 | ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = tx_dqs; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1588 | } |
| 1589 | program_timings(ctrl, channel); |
| 1590 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 1591 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1592 | |
| 1593 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1594 | statistics[lane][tx_dqs] = !((MCHBAR32(lane_base[lane] + |
| 1595 | GDCRTRAININGRESULT(channel, (tx_dqs / 32) & 1)) >> |
| 1596 | (tx_dqs % 32)) & 1); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | FOR_ALL_LANES { |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1600 | struct run rn = get_longest_zero_run(statistics[lane], TX_DQS_PI_LENGTH); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1601 | /* |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1602 | * tx_dq is a direct function of tx_dqs's 6 LSBs. Some tests increment the value |
| 1603 | * of tx_dqs by a small value, which might cause the 6-bit value to overflow if |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1604 | * it's close to 0x3f. Increment the value by a small offset if it's likely |
| 1605 | * to overflow, to make sure it won't overflow while running tests and bricks |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1606 | * the system due to a non matching tx_dq. |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1607 | * |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1608 | * TODO: find out why some tests (edge write discovery) increment tx_dqs. |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1609 | */ |
| 1610 | if ((rn.start & 0x3f) == 0x3e) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1611 | rn.start += 2; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1612 | else if ((rn.start & 0x3f) == 0x3f) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1613 | rn.start += 1; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1614 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1615 | ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = rn.start; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1616 | if (rn.all) { |
Angel Pons | 3079163 | 2020-12-12 12:28:29 +0100 | [diff] [blame] | 1617 | printk(BIOS_EMERG, "JEDEC write leveling failed: %d, %d, %d\n", |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1618 | channel, slotrank, lane); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1619 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1620 | return MAKE_ERR; |
| 1621 | } |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 1622 | printram("tx_dqs: %d, %d, %d: % 4d-% 4d-% 4d\n", |
Patrick Rudolph | 368b615 | 2016-11-25 16:36:52 +0100 | [diff] [blame] | 1623 | channel, slotrank, lane, rn.start, rn.middle, rn.end); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1624 | } |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
Angel Pons | 820bce7 | 2020-11-14 17:02:55 +0100 | [diff] [blame] | 1628 | static int get_dqs_flyby_adjust(u64 val) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1629 | { |
| 1630 | int i; |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 1631 | /* DQS is good enough */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1632 | if (val == 0xffffffffffffffffLL) |
| 1633 | return 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1634 | if (val >= 0xf000000000000000LL) { |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 1635 | /* DQS is late, needs negative adjustment */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1636 | for (i = 0; i < 8; i++) |
| 1637 | if (val << (8 * (7 - i) + 4)) |
| 1638 | return -i; |
| 1639 | } else { |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 1640 | /* DQS is early, needs positive adjustment */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1641 | for (i = 0; i < 8; i++) |
| 1642 | if (val >> (8 * (7 - i) + 4)) |
| 1643 | return i; |
| 1644 | } |
| 1645 | return 8; |
| 1646 | } |
| 1647 | |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 1648 | static void train_write_flyby(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1649 | { |
| 1650 | int channel, slotrank, lane, old; |
Angel Pons | 58b609b | 2020-11-13 14:35:29 +0100 | [diff] [blame] | 1651 | |
| 1652 | const union gdcr_training_mod_reg training_mod = { |
| 1653 | .dq_dqs_training_res = 1, |
| 1654 | }; |
| 1655 | MCHBAR32(GDCRTRAININGMOD) = training_mod.raw; |
| 1656 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1657 | FOR_ALL_POPULATED_CHANNELS { |
| 1658 | fill_pattern1(ctrl, channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1659 | } |
| 1660 | FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS { |
| 1661 | |
Angel Pons | 765d465 | 2020-11-11 14:44:35 +0100 | [diff] [blame] | 1662 | /* Reset read and write WDB pointers */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1663 | MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1664 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1665 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1666 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 1667 | iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1668 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 1669 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1670 | |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 1671 | const struct iosav_ssq rd_sequence[] = { |
| 1672 | /* DRAM command PREA */ |
| 1673 | [0] = { |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 1674 | .sp_cmd_ctrl = { |
| 1675 | .command = IOSAV_PRE, |
| 1676 | .ranksel_ap = 1, |
| 1677 | }, |
| 1678 | .subseq_ctrl = { |
| 1679 | .cmd_executions = 1, |
| 1680 | .cmd_delay_gap = 3, |
| 1681 | .post_ssq_wait = ctrl->tRP, |
| 1682 | .data_direction = SSQ_NA, |
| 1683 | }, |
| 1684 | .sp_cmd_addr = { |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 1685 | .address = 1 << 10, |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 1686 | .rowbits = 6, |
| 1687 | .bank = 0, |
| 1688 | .rank = slotrank, |
| 1689 | }, |
| 1690 | .addr_update = { |
| 1691 | .addr_wrap = 18, |
| 1692 | }, |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 1693 | }, |
| 1694 | /* DRAM command ACT */ |
| 1695 | [1] = { |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 1696 | .sp_cmd_ctrl = { |
| 1697 | .command = IOSAV_ACT, |
| 1698 | .ranksel_ap = 1, |
| 1699 | }, |
| 1700 | .subseq_ctrl = { |
| 1701 | .cmd_executions = 1, |
| 1702 | .cmd_delay_gap = 3, |
| 1703 | .post_ssq_wait = ctrl->tRCD, |
| 1704 | .data_direction = SSQ_NA, |
| 1705 | }, |
| 1706 | .sp_cmd_addr = { |
| 1707 | .address = 0, |
| 1708 | .rowbits = 6, |
| 1709 | .bank = 0, |
| 1710 | .rank = slotrank, |
| 1711 | }, |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 1712 | }, |
| 1713 | /* DRAM command RD */ |
| 1714 | [2] = { |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 1715 | .sp_cmd_ctrl = { |
| 1716 | .command = IOSAV_RD, |
| 1717 | .ranksel_ap = 3, |
| 1718 | }, |
| 1719 | .subseq_ctrl = { |
| 1720 | .cmd_executions = 1, |
| 1721 | .cmd_delay_gap = 3, |
| 1722 | .post_ssq_wait = ctrl->tRP + |
Angel Pons | ca00dec | 2020-05-02 15:04:00 +0200 | [diff] [blame] | 1723 | ctrl->timings[channel][slotrank].roundtrip_latency + |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 1724 | ctrl->timings[channel][slotrank].io_latency, |
| 1725 | .data_direction = SSQ_RD, |
| 1726 | }, |
| 1727 | .sp_cmd_addr = { |
| 1728 | .address = 8, |
| 1729 | .rowbits = 6, |
| 1730 | .bank = 0, |
| 1731 | .rank = slotrank, |
| 1732 | }, |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 1733 | }, |
| 1734 | }; |
| 1735 | iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1736 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 1737 | iosav_run_once_and_wait(channel); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 1738 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1739 | FOR_ALL_LANES { |
Felix Held | fb19c8a | 2020-01-14 21:27:59 +0100 | [diff] [blame] | 1740 | u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel)); |
Felix Held | 283b4466 | 2020-01-14 21:14:42 +0100 | [diff] [blame] | 1741 | res |= ((u64) MCHBAR32(lane_base[lane] + |
Felix Held | fb19c8a | 2020-01-14 21:27:59 +0100 | [diff] [blame] | 1742 | GDCRTRAININGRESULT2(channel))) << 32; |
Angel Pons | 820bce7 | 2020-11-14 17:02:55 +0100 | [diff] [blame] | 1743 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1744 | old = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs; |
| 1745 | ctrl->timings[channel][slotrank].lanes[lane].tx_dqs += |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1746 | get_dqs_flyby_adjust(res) * QCLK_PI; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1747 | |
| 1748 | printram("High adjust %d:%016llx\n", lane, res); |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 1749 | printram("Bval+: %d, %d, %d, % 4d -> % 4d\n", channel, slotrank, lane, |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1750 | old, ctrl->timings[channel][slotrank].lanes[lane].tx_dqs); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1751 | } |
| 1752 | } |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1753 | MCHBAR32(GDCRTRAININGMOD) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1754 | } |
| 1755 | |
Angel Pons | 7d11513 | 2020-11-14 01:44:44 +0100 | [diff] [blame] | 1756 | static void disable_refresh_machine(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1757 | { |
Angel Pons | 7d11513 | 2020-11-14 01:44:44 +0100 | [diff] [blame] | 1758 | int channel; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1759 | |
Angel Pons | 7d11513 | 2020-11-14 01:44:44 +0100 | [diff] [blame] | 1760 | FOR_ALL_POPULATED_CHANNELS { |
| 1761 | /* choose an existing rank */ |
| 1762 | const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1763 | |
Angel Pons | 7d11513 | 2020-11-14 01:44:44 +0100 | [diff] [blame] | 1764 | iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1765 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 1766 | iosav_run_once_and_wait(channel); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 1767 | |
Angel Pons | 7d11513 | 2020-11-14 01:44:44 +0100 | [diff] [blame] | 1768 | MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21); |
| 1769 | } |
| 1770 | |
| 1771 | /* Refresh disable */ |
| 1772 | MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3)); |
| 1773 | |
| 1774 | FOR_ALL_POPULATED_CHANNELS { |
| 1775 | /* Execute the same command queue */ |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 1776 | iosav_run_once_and_wait(channel); |
Angel Pons | 7d11513 | 2020-11-14 01:44:44 +0100 | [diff] [blame] | 1777 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1778 | } |
| 1779 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1780 | /* |
| 1781 | * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes. |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1782 | * |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1783 | * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different |
| 1784 | * times with respect to command, address and clock signals. By delaying either all DQ/DQS or |
| 1785 | * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the |
| 1786 | * CLK/ADDR/CMD signals have the same routing delay. |
| 1787 | * |
| 1788 | * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode, |
| 1789 | * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data |
| 1790 | * lanes (DQ). |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1791 | */ |
Angel Pons | 820bce7 | 2020-11-14 17:02:55 +0100 | [diff] [blame] | 1792 | static int jedec_write_leveling(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1793 | { |
Angel Pons | 820bce7 | 2020-11-14 17:02:55 +0100 | [diff] [blame] | 1794 | int channel, slotrank; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1795 | |
Angel Pons | 7d11513 | 2020-11-14 01:44:44 +0100 | [diff] [blame] | 1796 | disable_refresh_machine(ctrl); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1797 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1798 | /* Enable write leveling on all ranks |
| 1799 | Disable all DQ outputs |
| 1800 | Only NOP is allowed in this mode */ |
| 1801 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS |
| 1802 | write_mrreg(ctrl, channel, slotrank, 1, |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 1803 | make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1804 | |
Angel Pons | a1f1714 | 2020-11-15 12:50:03 +0100 | [diff] [blame] | 1805 | /* Needs to be programmed before I/O reset below */ |
Angel Pons | 58b609b | 2020-11-13 14:35:29 +0100 | [diff] [blame] | 1806 | const union gdcr_training_mod_reg training_mod = { |
| 1807 | .write_leveling_mode = 1, |
| 1808 | .enable_dqs_wl = 5, |
| 1809 | .odt_always_on = 1, |
| 1810 | .force_drive_enable = 1, |
| 1811 | }; |
| 1812 | MCHBAR32(GDCRTRAININGMOD) = training_mod.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1813 | |
| 1814 | toggle_io_reset(); |
| 1815 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1816 | /* Set any valid value for tx_dqs, it gets corrected later */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1817 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
Angel Pons | 820bce7 | 2020-11-14 17:02:55 +0100 | [diff] [blame] | 1818 | const int err = write_level_rank(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1819 | if (err) |
| 1820 | return err; |
| 1821 | } |
| 1822 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1823 | /* Disable write leveling on all ranks */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1824 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1825 | write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1826 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1827 | MCHBAR32(GDCRTRAININGMOD) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1828 | |
| 1829 | FOR_ALL_POPULATED_CHANNELS |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1830 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1831 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1832 | /* Refresh enable */ |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 1833 | MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1834 | |
| 1835 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 1836 | MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21)); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1837 | MCHBAR32(IOSAV_STATUS_ch(channel)); |
| 1838 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1839 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 1840 | iosav_write_zqcs_sequence(channel, 0, 4, 101, 31); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1841 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 1842 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | toggle_io_reset(); |
| 1846 | |
Angel Pons | 820bce7 | 2020-11-14 17:02:55 +0100 | [diff] [blame] | 1847 | return 0; |
| 1848 | } |
| 1849 | |
| 1850 | int write_training(ramctr_timing *ctrl) |
| 1851 | { |
Angel Pons | c674223 | 2020-11-15 13:26:21 +0100 | [diff] [blame] | 1852 | int channel, slotrank; |
Angel Pons | 820bce7 | 2020-11-14 17:02:55 +0100 | [diff] [blame] | 1853 | int err; |
| 1854 | |
Angel Pons | 4d19282 | 2020-12-12 13:54:37 +0100 | [diff] [blame] | 1855 | /* |
| 1856 | * Set the DEC_WRD bit, required for the write flyby algorithm. |
| 1857 | * Needs to be done before starting the write training procedure. |
| 1858 | */ |
Angel Pons | 820bce7 | 2020-11-14 17:02:55 +0100 | [diff] [blame] | 1859 | FOR_ALL_POPULATED_CHANNELS |
| 1860 | MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27); |
| 1861 | |
Angel Pons | 4c76d25 | 2020-11-15 13:06:53 +0100 | [diff] [blame] | 1862 | printram("CPE\n"); |
| 1863 | |
Angel Pons | 820bce7 | 2020-11-14 17:02:55 +0100 | [diff] [blame] | 1864 | err = jedec_write_leveling(ctrl); |
| 1865 | if (err) |
| 1866 | return err; |
| 1867 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1868 | printram("CPF\n"); |
| 1869 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1870 | FOR_ALL_POPULATED_CHANNELS { |
| 1871 | fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
Angel Pons | 011661c | 2020-11-15 18:21:35 +0100 | [diff] [blame] | 1875 | err = tx_dq_write_leveling(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1876 | if (err) |
| 1877 | return err; |
| 1878 | } |
| 1879 | |
| 1880 | FOR_ALL_POPULATED_CHANNELS |
| 1881 | program_timings(ctrl, channel); |
| 1882 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1883 | /* measure and adjust tx_dqs timings */ |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 1884 | train_write_flyby(ctrl); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1885 | |
| 1886 | FOR_ALL_POPULATED_CHANNELS |
| 1887 | program_timings(ctrl, channel); |
| 1888 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1889 | return 0; |
| 1890 | } |
| 1891 | |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 1892 | static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1893 | { |
| 1894 | struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank]; |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1895 | int tx_dq_delta; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1896 | int lanes_ok = 0; |
| 1897 | int ctr = 0; |
| 1898 | int lane; |
| 1899 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1900 | for (tx_dq_delta = -5; tx_dq_delta <= 5; tx_dq_delta++) { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1901 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 1902 | ctrl->timings[channel][slotrank].lanes[lane].tx_dq = |
| 1903 | saved_rt.lanes[lane].tx_dq + tx_dq_delta; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1904 | } |
| 1905 | program_timings(ctrl, channel); |
| 1906 | FOR_ALL_LANES { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1907 | MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1908 | } |
| 1909 | |
Angel Pons | 765d465 | 2020-11-11 14:44:35 +0100 | [diff] [blame] | 1910 | /* Reset read WDB pointer */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1911 | MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1912 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1913 | wait_for_iosav(channel); |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 1914 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 1915 | iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr); |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 1916 | |
| 1917 | /* Program LFSR for the RD/WR subsequences */ |
| 1918 | MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd; |
| 1919 | MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1920 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 1921 | iosav_run_once_and_wait(channel); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 1922 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1923 | FOR_ALL_LANES { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1924 | u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1925 | |
| 1926 | if (r32 == 0) |
| 1927 | lanes_ok |= 1 << lane; |
| 1928 | } |
| 1929 | ctr++; |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 1930 | if (lanes_ok == ((1 << ctrl->lanes) - 1)) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1931 | break; |
| 1932 | } |
| 1933 | |
| 1934 | ctrl->timings[channel][slotrank] = saved_rt; |
| 1935 | |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 1936 | return lanes_ok != ((1 << ctrl->lanes) - 1); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1937 | } |
| 1938 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1939 | static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1940 | { |
Subrata Banik | b1434fc | 2019-03-15 22:20:41 +0530 | [diff] [blame] | 1941 | unsigned int i, j; |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 1942 | unsigned int offset = get_precedening_channels(ctrl, channel) * 64; |
| 1943 | unsigned int step = 64 * num_of_channels(ctrl); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1944 | |
| 1945 | if (patno) { |
| 1946 | u8 base8 = 0x80 >> ((patno - 1) % 8); |
| 1947 | u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24); |
| 1948 | for (i = 0; i < 32; i++) { |
| 1949 | for (j = 0; j < 16; j++) { |
| 1950 | u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1951 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1952 | if (invert[patno - 1][i] & (1 << (j / 2))) |
| 1953 | val = ~val; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1954 | |
| 1955 | write32((void *)((1 << 26) + offset + i * step + j * 4), val); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1956 | } |
| 1957 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1958 | } else { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1959 | for (i = 0; i < ARRAY_SIZE(pattern); i++) { |
| 1960 | for (j = 0; j < 16; j++) { |
| 1961 | const u32 val = pattern[i][j]; |
| 1962 | write32((void *)((1 << 26) + offset + i * step + j * 4), val); |
| 1963 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1964 | } |
| 1965 | sfence(); |
| 1966 | } |
Angel Pons | 765d465 | 2020-11-11 14:44:35 +0100 | [diff] [blame] | 1967 | |
| 1968 | program_wdb_pattern_length(channel, 256); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1969 | } |
| 1970 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 1971 | static void reprogram_320c(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1972 | { |
Angel Pons | 7d11513 | 2020-11-14 01:44:44 +0100 | [diff] [blame] | 1973 | disable_refresh_machine(ctrl); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1974 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1975 | /* JEDEC reset */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1976 | dram_jedecreset(ctrl); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 1977 | |
| 1978 | /* MRS commands */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1979 | dram_mrscommands(ctrl); |
| 1980 | |
| 1981 | toggle_io_reset(); |
| 1982 | } |
| 1983 | |
Angel Pons | 42d033a | 2021-01-03 15:26:37 +0100 | [diff] [blame] | 1984 | #define CT_MIN_PI (-CCC_MAX_PI) |
| 1985 | #define CT_MAX_PI (+CCC_MAX_PI + 1) |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 1986 | #define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1) |
| 1987 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1988 | #define MIN_C320C_LEN 13 |
| 1989 | |
| 1990 | static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch) |
| 1991 | { |
| 1992 | struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS]; |
| 1993 | int slotrank; |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 1994 | int command_pi; |
| 1995 | int stat[NUM_SLOTRANKS][CT_PI_LENGTH]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 1996 | int delta = 0; |
| 1997 | |
| 1998 | printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel); |
| 1999 | |
| 2000 | FOR_ALL_POPULATED_RANKS { |
Angel Pons | 891f2bc | 2020-01-10 01:27:28 +0100 | [diff] [blame] | 2001 | saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | ctrl->cmd_stretch[channel] = cmd_stretch; |
| 2005 | |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 2006 | const union tc_rap_reg tc_rap = { |
| 2007 | .tRRD = ctrl->tRRD, |
| 2008 | .tRTP = ctrl->tRTP, |
| 2009 | .tCKE = ctrl->tCKE, |
| 2010 | .tWTR = ctrl->tWTR, |
| 2011 | .tFAW = ctrl->tFAW, |
| 2012 | .tWR = ctrl->tWR, |
| 2013 | .tCMD = ctrl->cmd_stretch[channel], |
| 2014 | }; |
| 2015 | MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2016 | |
| 2017 | if (ctrl->cmd_stretch[channel] == 2) |
| 2018 | delta = 2; |
| 2019 | else if (ctrl->cmd_stretch[channel] == 0) |
| 2020 | delta = 4; |
| 2021 | |
| 2022 | FOR_ALL_POPULATED_RANKS { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2023 | ctrl->timings[channel][slotrank].roundtrip_latency -= delta; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2024 | } |
| 2025 | |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 2026 | for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2027 | FOR_ALL_POPULATED_RANKS { |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 2028 | ctrl->timings[channel][slotrank].pi_coding = command_pi; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2029 | } |
| 2030 | program_timings(ctrl, channel); |
| 2031 | reprogram_320c(ctrl); |
| 2032 | FOR_ALL_POPULATED_RANKS { |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 2033 | stat[slotrank][command_pi - CT_MIN_PI] = |
| 2034 | test_command_training(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | FOR_ALL_POPULATED_RANKS { |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 2038 | struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2039 | |
Angel Pons | bf13ef0 | 2020-11-11 18:40:06 +0100 | [diff] [blame] | 2040 | ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI; |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 2041 | printram("cmd_stretch: %d, %d: % 4d-% 4d-% 4d\n", |
Patrick Rudolph | 368b615 | 2016-11-25 16:36:52 +0100 | [diff] [blame] | 2042 | channel, slotrank, rn.start, rn.middle, rn.end); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2043 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2044 | if (rn.all || rn.length < MIN_C320C_LEN) { |
| 2045 | FOR_ALL_POPULATED_RANKS { |
| 2046 | ctrl->timings[channel][slotrank] = |
| 2047 | saved_timings[channel][slotrank]; |
| 2048 | } |
| 2049 | return MAKE_ERR; |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2056 | /* |
| 2057 | * Adjust CMD phase shift and try multiple command rates. |
| 2058 | * A command rate of 2T doubles the time needed for address and command decode. |
| 2059 | */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2060 | int command_training(ramctr_timing *ctrl) |
| 2061 | { |
| 2062 | int channel; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2063 | |
| 2064 | FOR_ALL_POPULATED_CHANNELS { |
| 2065 | fill_pattern5(ctrl, channel, 0); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | FOR_ALL_POPULATED_CHANNELS { |
Patrick Rudolph | 58d16af | 2017-06-19 19:33:12 +0200 | [diff] [blame] | 2069 | int cmdrate, err; |
| 2070 | |
| 2071 | /* |
| 2072 | * Dual DIMM per channel: |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2073 | * Issue: |
Angel Pons | 3079163 | 2020-12-12 12:28:29 +0100 | [diff] [blame] | 2074 | * While command training seems to succeed, raminit will fail in write training. |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2075 | * |
| 2076 | * Workaround: |
| 2077 | * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs. |
| 2078 | * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode. |
Patrick Rudolph | 58d16af | 2017-06-19 19:33:12 +0200 | [diff] [blame] | 2079 | * |
| 2080 | * Single DIMM per channel: |
| 2081 | * Try command rate 1T and 2T |
| 2082 | */ |
| 2083 | cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5); |
Dan Elkouby | dabebc3 | 2018-04-13 18:47:10 +0300 | [diff] [blame] | 2084 | if (ctrl->tCMD) |
| 2085 | /* XMP gives the CMD rate in clock ticks, not ns */ |
| 2086 | cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1); |
Patrick Rudolph | 58d16af | 2017-06-19 19:33:12 +0200 | [diff] [blame] | 2087 | |
Elyes HAOUAS | adda3f81 | 2018-01-31 23:02:35 +0100 | [diff] [blame] | 2088 | for (; cmdrate < 2; cmdrate++) { |
Patrick Rudolph | 58d16af | 2017-06-19 19:33:12 +0200 | [diff] [blame] | 2089 | err = try_cmd_stretch(ctrl, channel, cmdrate << 1); |
| 2090 | |
| 2091 | if (!err) |
| 2092 | break; |
| 2093 | } |
| 2094 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2095 | if (err) { |
Angel Pons | 3079163 | 2020-12-12 12:28:29 +0100 | [diff] [blame] | 2096 | printk(BIOS_EMERG, "Command training failed: %d\n", channel); |
Patrick Rudolph | 58d16af | 2017-06-19 19:33:12 +0200 | [diff] [blame] | 2097 | return err; |
| 2098 | } |
| 2099 | |
Angel Pons | 891f2bc | 2020-01-10 01:27:28 +0100 | [diff] [blame] | 2100 | printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | FOR_ALL_POPULATED_CHANNELS |
Angel Pons | fd9a8b6 | 2020-11-13 13:56:30 +0100 | [diff] [blame] | 2104 | program_timings(ctrl, channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2105 | |
| 2106 | reprogram_320c(ctrl); |
| 2107 | return 0; |
| 2108 | } |
| 2109 | |
Angel Pons | 4c79f93 | 2020-11-14 01:26:52 +0100 | [diff] [blame] | 2110 | static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2111 | { |
Angel Pons | 96a06dd | 2020-11-14 00:33:18 +0100 | [diff] [blame] | 2112 | int dqs_pi; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2113 | int stats[NUM_LANES][MAX_EDGE_TIMING + 1]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2114 | int lane; |
| 2115 | |
Angel Pons | 96a06dd | 2020-11-14 00:33:18 +0100 | [diff] [blame] | 2116 | for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2117 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2118 | ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = dqs_pi; |
| 2119 | ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = dqs_pi; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2120 | } |
| 2121 | program_timings(ctrl, channel); |
| 2122 | |
| 2123 | FOR_ALL_LANES { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2124 | MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0; |
| 2125 | MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2126 | } |
| 2127 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2128 | wait_for_iosav(channel); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2129 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 2130 | iosav_write_read_mpr_sequence( |
| 2131 | channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2132 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 2133 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2134 | |
| 2135 | FOR_ALL_LANES { |
Angel Pons | 96a06dd | 2020-11-14 00:33:18 +0100 | [diff] [blame] | 2136 | stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2137 | } |
| 2138 | } |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2139 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2140 | FOR_ALL_LANES { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2141 | struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2142 | edges[lane] = rn.middle; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2143 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2144 | if (rn.all) { |
Angel Pons | 3079163 | 2020-12-12 12:28:29 +0100 | [diff] [blame] | 2145 | printk(BIOS_EMERG, "Read MPR training failed: %d, %d, %d\n", channel, |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2146 | slotrank, lane); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2147 | return MAKE_ERR; |
| 2148 | } |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 2149 | printram("eval %d, %d, %d: % 4d\n", channel, slotrank, lane, edges[lane]); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2150 | } |
| 2151 | return 0; |
| 2152 | } |
| 2153 | |
Angel Pons | 60971dc | 2020-11-14 00:49:38 +0100 | [diff] [blame] | 2154 | static void find_predefined_pattern(ramctr_timing *ctrl, const int channel) |
| 2155 | { |
| 2156 | int slotrank, lane; |
| 2157 | |
| 2158 | fill_pattern0(ctrl, channel, 0, 0); |
| 2159 | FOR_ALL_LANES { |
Angel Pons | c674223 | 2020-11-15 13:26:21 +0100 | [diff] [blame] | 2160 | MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0; |
Angel Pons | 60971dc | 2020-11-14 00:49:38 +0100 | [diff] [blame] | 2161 | MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane)); |
| 2162 | } |
| 2163 | |
| 2164 | FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2165 | ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 16; |
| 2166 | ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 16; |
Angel Pons | 60971dc | 2020-11-14 00:49:38 +0100 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | program_timings(ctrl, channel); |
| 2170 | |
| 2171 | FOR_ALL_POPULATED_RANKS { |
| 2172 | wait_for_iosav(channel); |
| 2173 | |
| 2174 | iosav_write_read_mpr_sequence( |
| 2175 | channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8); |
| 2176 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 2177 | iosav_run_once_and_wait(channel); |
Angel Pons | 60971dc | 2020-11-14 00:49:38 +0100 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | /* XXX: check any measured value ? */ |
| 2181 | |
| 2182 | FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2183 | ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 48; |
| 2184 | ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 48; |
Angel Pons | 60971dc | 2020-11-14 00:49:38 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | program_timings(ctrl, channel); |
| 2188 | |
| 2189 | FOR_ALL_POPULATED_RANKS { |
| 2190 | wait_for_iosav(channel); |
| 2191 | |
| 2192 | iosav_write_read_mpr_sequence( |
| 2193 | channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8); |
| 2194 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 2195 | iosav_run_once_and_wait(channel); |
Angel Pons | 60971dc | 2020-11-14 00:49:38 +0100 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | /* XXX: check any measured value ? */ |
| 2199 | |
| 2200 | FOR_ALL_LANES { |
| 2201 | MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = |
| 2202 | ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff; |
| 2203 | } |
| 2204 | } |
| 2205 | |
Angel Pons | 4c79f93 | 2020-11-14 01:26:52 +0100 | [diff] [blame] | 2206 | int read_mpr_training(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2207 | { |
| 2208 | int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
| 2209 | int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
| 2210 | int channel, slotrank, lane; |
| 2211 | int err; |
| 2212 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2213 | MCHBAR32(GDCRTRAININGMOD) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2214 | |
| 2215 | toggle_io_reset(); |
| 2216 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2217 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | 60971dc | 2020-11-14 00:49:38 +0100 | [diff] [blame] | 2218 | find_predefined_pattern(ctrl, channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2219 | |
| 2220 | fill_pattern0(ctrl, channel, 0, 0xffffffff); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2221 | } |
| 2222 | |
Angel Pons | 0c3936e | 2020-03-22 12:49:27 +0100 | [diff] [blame] | 2223 | /* |
| 2224 | * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will |
| 2225 | * also use a single loop. It would seem that it is a debugging configuration. |
| 2226 | */ |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 2227 | MCHBAR32(IOSAV_DC_MASK) = 3 << 8; |
| 2228 | printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2229 | |
| 2230 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
Angel Pons | 4c79f93 | 2020-11-14 01:26:52 +0100 | [diff] [blame] | 2231 | err = find_read_mpr_margin(ctrl, channel, slotrank, |
Felix Held | 2bb3cdf | 2018-07-28 00:23:59 +0200 | [diff] [blame] | 2232 | falling_edges[channel][slotrank]); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2233 | if (err) |
| 2234 | return err; |
| 2235 | } |
| 2236 | |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 2237 | MCHBAR32(IOSAV_DC_MASK) = 2 << 8; |
| 2238 | printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2239 | |
| 2240 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
Angel Pons | 4c79f93 | 2020-11-14 01:26:52 +0100 | [diff] [blame] | 2241 | err = find_read_mpr_margin(ctrl, channel, slotrank, |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2242 | rising_edges[channel][slotrank]); |
| 2243 | if (err) |
| 2244 | return err; |
| 2245 | } |
| 2246 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2247 | MCHBAR32(IOSAV_DC_MASK) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2248 | |
| 2249 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2250 | ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2251 | falling_edges[channel][slotrank][lane]; |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2252 | ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2253 | rising_edges[channel][slotrank][lane]; |
| 2254 | } |
| 2255 | |
| 2256 | FOR_ALL_POPULATED_CHANNELS { |
| 2257 | program_timings(ctrl, channel); |
| 2258 | } |
| 2259 | |
Angel Pons | 50a6fe7 | 2020-11-14 01:18:14 +0100 | [diff] [blame] | 2260 | FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2261 | MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2262 | } |
| 2263 | return 0; |
| 2264 | } |
| 2265 | |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2266 | static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2267 | { |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2268 | const int rd_vref_offsets[] = { 0, 0xc, 0x2c }; |
| 2269 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2270 | u32 raw_stats[MAX_EDGE_TIMING + 1]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2271 | int lower[NUM_LANES]; |
| 2272 | int upper[NUM_LANES]; |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2273 | int lane, i, read_pi, pat; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2274 | |
| 2275 | FOR_ALL_LANES { |
| 2276 | lower[lane] = 0; |
| 2277 | upper[lane] = MAX_EDGE_TIMING; |
| 2278 | } |
| 2279 | |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2280 | for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) { |
Angel Pons | 58b609b | 2020-11-13 14:35:29 +0100 | [diff] [blame] | 2281 | const union gdcr_training_mod_reg training_mod = { |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2282 | .vref_gen_ctl = rd_vref_offsets[i], |
Angel Pons | 58b609b | 2020-11-13 14:35:29 +0100 | [diff] [blame] | 2283 | }; |
| 2284 | MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw; |
| 2285 | printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2286 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2287 | for (pat = 0; pat < NUM_PATTERNS; pat++) { |
| 2288 | fill_pattern5(ctrl, channel, pat); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2289 | printram("using pattern %d\n", pat); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2290 | |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2291 | for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2292 | FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2293 | ctrl->timings[channel][slotrank].lanes[lane] |
| 2294 | .rx_dqs_p = read_pi; |
| 2295 | ctrl->timings[channel][slotrank].lanes[lane] |
| 2296 | .rx_dqs_n = read_pi; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2297 | } |
| 2298 | program_timings(ctrl, channel); |
| 2299 | |
| 2300 | FOR_ALL_LANES { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2301 | MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0; |
| 2302 | MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2303 | } |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2304 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2305 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 2306 | iosav_write_data_write_sequence(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2307 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 2308 | iosav_run_once_and_wait(channel); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 2309 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2310 | FOR_ALL_LANES { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2311 | MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2312 | } |
| 2313 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2314 | /* FIXME: This register only exists on Ivy Bridge */ |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2315 | raw_stats[read_pi] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2316 | } |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2317 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2318 | FOR_ALL_LANES { |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2319 | int stats[MAX_EDGE_TIMING + 1]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2320 | struct run rn; |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2321 | |
| 2322 | for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) |
| 2323 | stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane)); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2324 | |
| 2325 | rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1); |
| 2326 | |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 2327 | printram("edges: %d, %d, %d: % 4d-% 4d-% 4d, " |
| 2328 | "% 4d-% 4d\n", channel, slotrank, i, rn.start, |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2329 | rn.middle, rn.end, rn.start + ctrl->edge_offset[i], |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2330 | rn.end - ctrl->edge_offset[i]); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2331 | |
| 2332 | lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]); |
| 2333 | upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]); |
| 2334 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2335 | edges[lane] = (lower[lane] + upper[lane]) / 2; |
| 2336 | if (rn.all || (lower[lane] > upper[lane])) { |
Angel Pons | 3079163 | 2020-12-12 12:28:29 +0100 | [diff] [blame] | 2337 | printk(BIOS_EMERG, "Aggressive read training failed: " |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2338 | "%d, %d, %d\n", channel, slotrank, lane); |
| 2339 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2340 | return MAKE_ERR; |
| 2341 | } |
| 2342 | } |
| 2343 | } |
| 2344 | } |
| 2345 | |
Angel Pons | a93f46e | 2020-11-17 16:54:01 +0100 | [diff] [blame] | 2346 | /* Restore nominal Vref after training */ |
| 2347 | MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2348 | printram("CPA\n"); |
| 2349 | return 0; |
| 2350 | } |
| 2351 | |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2352 | int aggressive_read_training(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2353 | { |
| 2354 | int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2355 | int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
| 2356 | int channel, slotrank, lane, err; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2357 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2358 | /* |
| 2359 | * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will |
| 2360 | * also use a single loop. It would seem that it is a debugging configuration. |
| 2361 | */ |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 2362 | MCHBAR32(IOSAV_DC_MASK) = 3 << 8; |
| 2363 | printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2364 | |
| 2365 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2366 | err = find_agrsv_read_margin(ctrl, channel, slotrank, |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2367 | falling_edges[channel][slotrank]); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2368 | if (err) |
| 2369 | return err; |
| 2370 | } |
| 2371 | |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 2372 | MCHBAR32(IOSAV_DC_MASK) = 2 << 8; |
| 2373 | printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2374 | |
| 2375 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
Angel Pons | 08f749d | 2020-11-17 16:50:56 +0100 | [diff] [blame] | 2376 | err = find_agrsv_read_margin(ctrl, channel, slotrank, |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2377 | rising_edges[channel][slotrank]); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2378 | if (err) |
| 2379 | return err; |
| 2380 | } |
| 2381 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2382 | MCHBAR32(IOSAV_DC_MASK) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2383 | |
| 2384 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2385 | ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2386 | falling_edges[channel][slotrank][lane]; |
| 2387 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2388 | ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2389 | rising_edges[channel][slotrank][lane]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | FOR_ALL_POPULATED_CHANNELS |
| 2393 | program_timings(ctrl, channel); |
| 2394 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2395 | return 0; |
| 2396 | } |
| 2397 | |
Angel Pons | 2a7d752 | 2020-11-19 12:49:07 +0100 | [diff] [blame] | 2398 | static void test_aggressive_write(ramctr_timing *ctrl, int channel, int slotrank) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2399 | { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2400 | wait_for_iosav(channel); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2401 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 2402 | iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2403 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 2404 | iosav_run_once_and_wait(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2405 | } |
| 2406 | |
Angel Pons | 2a7d752 | 2020-11-19 12:49:07 +0100 | [diff] [blame] | 2407 | static void set_write_vref(const int channel, const u8 wr_vref) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2408 | { |
Angel Pons | 2a7d752 | 2020-11-19 12:49:07 +0100 | [diff] [blame] | 2409 | MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24), wr_vref << 24); |
| 2410 | udelay(2); |
| 2411 | } |
| 2412 | |
| 2413 | int aggressive_write_training(ramctr_timing *ctrl) |
| 2414 | { |
| 2415 | const u8 wr_vref_offsets[3] = { 0, 0x0f, 0x2f }; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2416 | int i, pat; |
| 2417 | |
| 2418 | int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
| 2419 | int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
| 2420 | int channel, slotrank, lane; |
| 2421 | |
Angel Pons | 9fbb1b0 | 2020-11-19 12:53:36 +0100 | [diff] [blame] | 2422 | /* Changing the write Vref is only supported on some Ivy Bridge SKUs */ |
| 2423 | if (!IS_IVY_CPU(ctrl->cpu)) |
| 2424 | return 0; |
| 2425 | |
| 2426 | if (!(pci_read_config32(HOST_BRIDGE, CAPID0_A) & CAPID_WRTVREF)) |
| 2427 | return 0; |
| 2428 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2429 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 2430 | lower[channel][slotrank][lane] = 0; |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2431 | upper[channel][slotrank][lane] = MAX_TX_DQ; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2432 | } |
| 2433 | |
Angel Pons | 2a7d752 | 2020-11-19 12:49:07 +0100 | [diff] [blame] | 2434 | /* Only enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization on later steppings */ |
| 2435 | const bool enable_iosav_opt = IS_IVY_CPU_D(ctrl->cpu) || IS_IVY_CPU_E(ctrl->cpu); |
| 2436 | |
| 2437 | if (enable_iosav_opt) |
| 2438 | MCHBAR32(MCMNTS_SPARE) = 1; |
| 2439 | |
Angel Pons | 3079163 | 2020-12-12 12:28:29 +0100 | [diff] [blame] | 2440 | printram("Aggresive write training:\n"); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2441 | |
Angel Pons | 2a7d752 | 2020-11-19 12:49:07 +0100 | [diff] [blame] | 2442 | for (i = 0; i < ARRAY_SIZE(wr_vref_offsets); i++) { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2443 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | 2a7d752 | 2020-11-19 12:49:07 +0100 | [diff] [blame] | 2444 | set_write_vref(channel, wr_vref_offsets[i]); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2445 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2446 | for (pat = 0; pat < NUM_PATTERNS; pat++) { |
| 2447 | FOR_ALL_POPULATED_RANKS { |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2448 | int tx_dq; |
| 2449 | u32 raw_stats[MAX_TX_DQ + 1]; |
| 2450 | int stats[MAX_TX_DQ + 1]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2451 | |
| 2452 | /* Make sure rn.start < rn.end */ |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2453 | stats[MAX_TX_DQ] = 1; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2454 | |
| 2455 | fill_pattern5(ctrl, channel, pat); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2456 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2457 | for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2458 | FOR_ALL_LANES { |
| 2459 | ctrl->timings[channel][slotrank] |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2460 | .lanes[lane].tx_dq = tx_dq; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2461 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2462 | program_timings(ctrl, channel); |
| 2463 | |
Angel Pons | 2a7d752 | 2020-11-19 12:49:07 +0100 | [diff] [blame] | 2464 | test_aggressive_write(ctrl, channel, slotrank); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2465 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2466 | raw_stats[tx_dq] = MCHBAR32( |
Angel Pons | 098240eb | 2020-03-22 12:55:32 +0100 | [diff] [blame] | 2467 | IOSAV_BYTE_SERROR_C_ch(channel)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2468 | } |
| 2469 | FOR_ALL_LANES { |
| 2470 | struct run rn; |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2471 | for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) { |
| 2472 | stats[tx_dq] = !!(raw_stats[tx_dq] |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2473 | & (1 << lane)); |
| 2474 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2475 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2476 | rn = get_longest_zero_run(stats, MAX_TX_DQ + 1); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2477 | if (rn.all) { |
Angel Pons | 3079163 | 2020-12-12 12:28:29 +0100 | [diff] [blame] | 2478 | printk(BIOS_EMERG, "Aggressive " |
| 2479 | "write training failed: " |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2480 | "%d, %d, %d\n", channel, |
| 2481 | slotrank, lane); |
| 2482 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2483 | return MAKE_ERR; |
| 2484 | } |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2485 | printram("tx_dq: %d, %d, %d: " |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 2486 | "% 4d-% 4d-% 4d, " |
| 2487 | "% 4d-% 4d\n", channel, slotrank, |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2488 | i, rn.start, rn.middle, rn.end, |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2489 | rn.start + ctrl->tx_dq_offset[i], |
| 2490 | rn.end - ctrl->tx_dq_offset[i]); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2491 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2492 | lower[channel][slotrank][lane] = |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2493 | MAX(rn.start + ctrl->tx_dq_offset[i], |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2494 | lower[channel][slotrank][lane]); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2495 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2496 | upper[channel][slotrank][lane] = |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2497 | MIN(rn.end - ctrl->tx_dq_offset[i], |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2498 | upper[channel][slotrank][lane]); |
| 2499 | |
| 2500 | } |
| 2501 | } |
| 2502 | } |
| 2503 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2504 | } |
| 2505 | |
Angel Pons | 2a7d752 | 2020-11-19 12:49:07 +0100 | [diff] [blame] | 2506 | FOR_ALL_CHANNELS { |
| 2507 | /* Restore nominal write Vref after training */ |
| 2508 | set_write_vref(channel, 0); |
| 2509 | } |
| 2510 | |
| 2511 | /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */ |
| 2512 | if (enable_iosav_opt) |
| 2513 | MCHBAR32(MCMNTS_SPARE) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2514 | |
| 2515 | printram("CPB\n"); |
| 2516 | |
| 2517 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
Angel Pons | 7e439c9 | 2020-12-07 11:56:01 +0100 | [diff] [blame] | 2518 | printram("tx_dq %d, %d, %d: % 4d\n", channel, slotrank, lane, |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2519 | (lower[channel][slotrank][lane] + |
| 2520 | upper[channel][slotrank][lane]) / 2); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2521 | |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2522 | ctrl->timings[channel][slotrank].lanes[lane].tx_dq = |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2523 | (lower[channel][slotrank][lane] + |
| 2524 | upper[channel][slotrank][lane]) / 2; |
| 2525 | } |
| 2526 | FOR_ALL_POPULATED_CHANNELS { |
| 2527 | program_timings(ctrl, channel); |
| 2528 | } |
| 2529 | return 0; |
| 2530 | } |
| 2531 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2532 | void normalize_training(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2533 | { |
| 2534 | int channel, slotrank, lane; |
Patrick Rudolph | 3c8cb97 | 2016-11-25 16:00:01 +0100 | [diff] [blame] | 2535 | int mat; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2536 | |
| 2537 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
| 2538 | int delta; |
Patrick Rudolph | 3c8cb97 | 2016-11-25 16:00:01 +0100 | [diff] [blame] | 2539 | mat = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2540 | FOR_ALL_LANES mat = |
Angel Pons | c8ac2cc | 2020-11-19 22:50:54 +0100 | [diff] [blame] | 2541 | MAX(ctrl->timings[channel][slotrank].lanes[lane].rcven, mat); |
Patrick Rudolph | 413edc8 | 2016-11-25 15:40:07 +0100 | [diff] [blame] | 2542 | printram("normalize %d, %d, %d: mat %d\n", |
| 2543 | channel, slotrank, lane, mat); |
| 2544 | |
Felix Held | ef4fe3e | 2019-12-31 14:15:05 +0100 | [diff] [blame] | 2545 | delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency; |
Patrick Rudolph | 413edc8 | 2016-11-25 15:40:07 +0100 | [diff] [blame] | 2546 | printram("normalize %d, %d, %d: delta %d\n", |
| 2547 | channel, slotrank, lane, delta); |
| 2548 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2549 | ctrl->timings[channel][slotrank].roundtrip_latency += delta; |
Felix Held | ef4fe3e | 2019-12-31 14:15:05 +0100 | [diff] [blame] | 2550 | ctrl->timings[channel][slotrank].io_latency += delta; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | FOR_ALL_POPULATED_CHANNELS { |
| 2554 | program_timings(ctrl, channel); |
| 2555 | } |
| 2556 | } |
| 2557 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2558 | int channel_test(ramctr_timing *ctrl) |
| 2559 | { |
| 2560 | int channel, slotrank, lane; |
| 2561 | |
| 2562 | slotrank = 0; |
| 2563 | FOR_ALL_POPULATED_CHANNELS |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2564 | if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) { |
Angel Pons | 891f2bc | 2020-01-10 01:27:28 +0100 | [diff] [blame] | 2565 | printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2566 | return MAKE_ERR; |
| 2567 | } |
| 2568 | FOR_ALL_POPULATED_CHANNELS { |
| 2569 | fill_pattern0(ctrl, channel, 0x12345678, 0x98765432); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | for (slotrank = 0; slotrank < 4; slotrank++) |
| 2573 | FOR_ALL_CHANNELS |
| 2574 | if (ctrl->rankmap[channel] & (1 << slotrank)) { |
| 2575 | FOR_ALL_LANES { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2576 | MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0; |
| 2577 | MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2578 | } |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2579 | wait_for_iosav(channel); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 2580 | |
Angel Pons | ffd5015 | 2020-11-12 11:03:10 +0100 | [diff] [blame] | 2581 | iosav_write_memory_test_sequence(ctrl, channel, slotrank); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 2582 | |
Angel Pons | a853e7a | 2020-12-07 12:28:38 +0100 | [diff] [blame] | 2583 | iosav_run_once_and_wait(channel); |
Felix Held | 9cf1dd2 | 2018-07-31 14:52:40 +0200 | [diff] [blame] | 2584 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2585 | FOR_ALL_LANES |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2586 | if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2587 | printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n", |
| 2588 | channel, slotrank, lane); |
| 2589 | return MAKE_ERR; |
| 2590 | } |
| 2591 | } |
| 2592 | return 0; |
| 2593 | } |
| 2594 | |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 2595 | void channel_scrub(ramctr_timing *ctrl) |
| 2596 | { |
| 2597 | int channel, slotrank, row, rowsize; |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 2598 | u8 bank; |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 2599 | |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 2600 | FOR_ALL_POPULATED_CHANNELS { |
| 2601 | wait_for_iosav(channel); |
| 2602 | fill_pattern0(ctrl, channel, 0, 0); |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 2603 | } |
| 2604 | |
| 2605 | /* |
| 2606 | * During runtime the "scrubber" will periodically scan through the memory in the |
| 2607 | * physical address space, to identify and fix CRC errors. |
| 2608 | * The following loops writes to every DRAM address, setting the ECC bits to the |
| 2609 | * correct value. A read from this location will no longer return a CRC error, |
| 2610 | * except when a bit has toggled due to external events. |
Angel Pons | 3b9d3e9 | 2020-11-11 19:10:39 +0100 | [diff] [blame] | 2611 | * The same could be achieved by writing to the physical memory map, but it's |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 2612 | * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory, |
| 2613 | * and firmware running in x86_32. |
| 2614 | */ |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 2615 | FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS { |
| 2616 | rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits; |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 2617 | for (bank = 0; bank < 8; bank++) { |
| 2618 | for (row = 0; row < rowsize; row += 16) { |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 2619 | |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 2620 | u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD); |
| 2621 | const struct iosav_ssq sequence[] = { |
| 2622 | /* |
| 2623 | * DRAM command ACT |
| 2624 | * Opens the row for writing. |
| 2625 | */ |
| 2626 | [0] = { |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 2627 | .sp_cmd_ctrl = { |
| 2628 | .command = IOSAV_ACT, |
| 2629 | .ranksel_ap = 1, |
| 2630 | }, |
| 2631 | .subseq_ctrl = { |
| 2632 | .cmd_executions = 1, |
| 2633 | .cmd_delay_gap = gap, |
| 2634 | .post_ssq_wait = ctrl->tRCD, |
| 2635 | .data_direction = SSQ_NA, |
| 2636 | }, |
| 2637 | .sp_cmd_addr = { |
| 2638 | .address = row, |
| 2639 | .rowbits = 6, |
| 2640 | .bank = bank, |
| 2641 | .rank = slotrank, |
| 2642 | }, |
| 2643 | .addr_update = { |
| 2644 | .inc_addr_1 = 1, |
| 2645 | .addr_wrap = 18, |
| 2646 | }, |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 2647 | }, |
| 2648 | /* |
| 2649 | * DRAM command WR |
| 2650 | * Writes (128 + 1) * 8 (burst length) * 8 (bus width) |
| 2651 | * bytes. |
| 2652 | */ |
| 2653 | [1] = { |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 2654 | .sp_cmd_ctrl = { |
| 2655 | .command = IOSAV_WR, |
| 2656 | .ranksel_ap = 1, |
| 2657 | }, |
| 2658 | .subseq_ctrl = { |
| 2659 | .cmd_executions = 129, |
| 2660 | .cmd_delay_gap = 4, |
| 2661 | .post_ssq_wait = ctrl->tWTR + |
| 2662 | ctrl->CWL + 8, |
| 2663 | .data_direction = SSQ_WR, |
| 2664 | }, |
| 2665 | .sp_cmd_addr = { |
| 2666 | .address = row, |
| 2667 | .rowbits = 0, |
| 2668 | .bank = bank, |
| 2669 | .rank = slotrank, |
| 2670 | }, |
| 2671 | .addr_update = { |
| 2672 | .inc_addr_8 = 1, |
| 2673 | .addr_wrap = 9, |
| 2674 | }, |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 2675 | }, |
| 2676 | /* |
| 2677 | * DRAM command PRE |
| 2678 | * Closes the row. |
| 2679 | */ |
| 2680 | [2] = { |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 2681 | .sp_cmd_ctrl = { |
| 2682 | .command = IOSAV_PRE, |
| 2683 | .ranksel_ap = 1, |
| 2684 | }, |
| 2685 | .subseq_ctrl = { |
| 2686 | .cmd_executions = 1, |
| 2687 | .cmd_delay_gap = 4, |
| 2688 | .post_ssq_wait = ctrl->tRP, |
| 2689 | .data_direction = SSQ_NA, |
| 2690 | }, |
| 2691 | .sp_cmd_addr = { |
| 2692 | .address = 0, |
| 2693 | .rowbits = 6, |
| 2694 | .bank = bank, |
| 2695 | .rank = slotrank, |
| 2696 | }, |
| 2697 | .addr_update = { |
Angel Pons | fd9a8b6 | 2020-11-13 13:56:30 +0100 | [diff] [blame] | 2698 | .addr_wrap = 18, |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 2699 | }, |
Angel Pons | 8f0757e | 2020-11-11 23:03:36 +0100 | [diff] [blame] | 2700 | }, |
| 2701 | }; |
| 2702 | iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence)); |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 2703 | |
Patrick Rudolph | b5fa9c8 | 2020-05-01 18:35:05 +0200 | [diff] [blame] | 2704 | iosav_run_queue(channel, 16, 0); |
| 2705 | |
| 2706 | wait_for_iosav(channel); |
Angel Pons | 3abd206 | 2020-05-03 00:25:02 +0200 | [diff] [blame] | 2707 | } |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 2708 | } |
| 2709 | } |
| 2710 | } |
| 2711 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2712 | void set_scrambling_seed(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2713 | { |
| 2714 | int channel; |
| 2715 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2716 | /* FIXME: we hardcode seeds. Do we need to use some PRNG for them? I don't think so. */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2717 | static u32 seeds[NUM_CHANNELS][3] = { |
| 2718 | {0x00009a36, 0xbafcfdcf, 0x46d1ab68}, |
| 2719 | {0x00028bfa, 0x53fe4b49, 0x19ed5483} |
| 2720 | }; |
| 2721 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 2722 | MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2723 | MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0]; |
| 2724 | MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1]; |
| 2725 | MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2726 | } |
| 2727 | } |
| 2728 | |
Angel Pons | 89ae6b8 | 2020-03-21 13:23:32 +0100 | [diff] [blame] | 2729 | void set_wmm_behavior(const u32 cpu) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2730 | { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2731 | if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2732 | MCHBAR32(SC_WDBWM) = 0x141d1519; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2733 | } else { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2734 | MCHBAR32(SC_WDBWM) = 0x551d1519; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2735 | } |
| 2736 | } |
| 2737 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2738 | void prepare_training(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2739 | { |
| 2740 | int channel; |
| 2741 | |
| 2742 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2743 | /* Always drive command bus */ |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 2744 | MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | udelay(1); |
| 2748 | |
| 2749 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2750 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2751 | } |
| 2752 | } |
| 2753 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2754 | void set_read_write_timings(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2755 | { |
Angel Pons | 1146332 | 2020-11-19 11:04:28 +0100 | [diff] [blame] | 2756 | /* Use a larger delay when running fast to improve stability */ |
| 2757 | const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2; |
| 2758 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2759 | int channel, slotrank; |
Patrick Rudolph | 19c3dad | 2016-11-26 11:37:45 +0100 | [diff] [blame] | 2760 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2761 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2762 | int min_pi = 10000; |
| 2763 | int max_pi = -10000; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2764 | |
| 2765 | FOR_ALL_POPULATED_RANKS { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2766 | max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi); |
| 2767 | min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2768 | } |
| 2769 | |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 2770 | const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel]; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2771 | |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 2772 | const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2773 | |
Patrick Rudolph | 19c3dad | 2016-11-26 11:37:45 +0100 | [diff] [blame] | 2774 | dram_odt_stretch(ctrl, channel); |
| 2775 | |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 2776 | const union tc_rwp_reg tc_rwp = { |
| 2777 | .tRRDR = 0, |
| 2778 | .tRRDD = val, |
| 2779 | .tWWDR = val, |
| 2780 | .tWWDD = val, |
Angel Pons | 1146332 | 2020-11-19 11:04:28 +0100 | [diff] [blame] | 2781 | .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc, |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 2782 | .tWRDRDD = tWRDRDD, |
| 2783 | .tRWSR = 2, |
| 2784 | .dec_wrd = 1, |
| 2785 | }; |
| 2786 | MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2787 | } |
| 2788 | } |
| 2789 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2790 | void set_normal_operation(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2791 | { |
| 2792 | int channel; |
| 2793 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 2794 | MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel]; |
| 2795 | MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2796 | } |
| 2797 | } |
| 2798 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2799 | /* Encode the watermark latencies in a suitable format for graphics drivers consumption */ |
| 2800 | static int encode_wm(int ns) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2801 | { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2802 | return (ns + 499) / 500; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2803 | } |
| 2804 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2805 | /* FIXME: values in this function should be hardware revision-dependent */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2806 | void final_registers(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2807 | { |
| 2808 | int channel; |
| 2809 | int t1_cycles = 0, t1_ns = 0, t2_ns; |
| 2810 | int t3_ns; |
| 2811 | u32 r32; |
| 2812 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2813 | /* FIXME: This register only exists on Ivy Bridge */ |
| 2814 | MCHBAR32(WMM_READ_CONFIG) = 0x46; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2815 | |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 2816 | FOR_ALL_CHANNELS { |
| 2817 | union tc_othp_reg tc_othp = { |
| 2818 | .raw = MCHBAR32(TC_OTHP_ch(channel)), |
| 2819 | }; |
| 2820 | tc_othp.tCPDED = 1; |
| 2821 | MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw; |
| 2822 | } |
Patrick Rudolph | 652c491 | 2017-10-31 11:36:55 +0100 | [diff] [blame] | 2823 | |
Angel Pons | 09fc4b9 | 2020-11-19 12:02:07 +0100 | [diff] [blame] | 2824 | /* 64 DCLKs until idle, decision per rank */ |
| 2825 | MCHBAR32(PM_PDWN_CONFIG) = get_power_down_mode(ctrl) << 8 | 64; |
Patrick Rudolph | 652c491 | 2017-10-31 11:36:55 +0100 | [diff] [blame] | 2826 | |
Felix Held | f9b826a | 2018-07-30 17:56:52 +0200 | [diff] [blame] | 2827 | FOR_ALL_CHANNELS |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2828 | MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa; |
Felix Held | f9b826a | 2018-07-30 17:56:52 +0200 | [diff] [blame] | 2829 | |
Angel Pons | c728e25 | 2021-01-03 16:47:09 +0100 | [diff] [blame] | 2830 | MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; |
| 2831 | MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2832 | |
| 2833 | FOR_ALL_CHANNELS { |
| 2834 | switch (ctrl->rankmap[channel]) { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2835 | /* Unpopulated channel */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2836 | case 0: |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2837 | MCHBAR32(PM_CMD_PWR_ch(channel)) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2838 | break; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2839 | /* Only single-ranked dimms */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2840 | case 1: |
| 2841 | case 4: |
| 2842 | case 5: |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2843 | MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2844 | break; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2845 | /* Dual-ranked dimms present */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2846 | default: |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2847 | MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2848 | break; |
| 2849 | } |
| 2850 | } |
| 2851 | |
Felix Held | 50b7ed2 | 2019-12-30 20:41:54 +0100 | [diff] [blame] | 2852 | MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2853 | MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0); |
Felix Held | 50b7ed2 | 2019-12-30 20:41:54 +0100 | [diff] [blame] | 2854 | MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f); |
Felix Held | f9b826a | 2018-07-30 17:56:52 +0200 | [diff] [blame] | 2855 | |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 2856 | FOR_ALL_CHANNELS { |
| 2857 | union tc_rfp_reg tc_rfp = { |
| 2858 | .raw = MCHBAR32(TC_RFP_ch(channel)), |
| 2859 | }; |
| 2860 | tc_rfp.refresh_2x_control = 1; |
| 2861 | MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw; |
| 2862 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2863 | |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 2864 | MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0); |
| 2865 | MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2866 | MCHBAR32(BANDTIMERS_SNB) = 0xfa; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2867 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2868 | /* Find a populated channel */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2869 | FOR_ALL_POPULATED_CHANNELS |
| 2870 | break; |
| 2871 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2872 | t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff; |
| 2873 | r32 = MCHBAR32(PM_DLL_CONFIG); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2874 | if (r32 & (1 << 17)) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2875 | t1_cycles += (r32 & 0xfff); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2876 | t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2877 | t1_ns = t1_cycles * ctrl->tCK / 256 + 544; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2878 | if (!(r32 & (1 << 17))) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2879 | t1_ns += 500; |
| 2880 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2881 | t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff); |
Angel Pons | 891f2bc | 2020-01-10 01:27:28 +0100 | [diff] [blame] | 2882 | if (MCHBAR32(SAPMCTL) & 8) { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2883 | t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2884 | t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff); |
Angel Pons | 891f2bc | 2020-01-10 01:27:28 +0100 | [diff] [blame] | 2885 | } else { |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2886 | t3_ns = 500; |
| 2887 | } |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2888 | |
| 2889 | /* The graphics driver will use these watermark values */ |
| 2890 | printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns); |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 2891 | MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f, |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2892 | ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) | |
| 2893 | ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2894 | } |
| 2895 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2896 | void restore_timings(ramctr_timing *ctrl) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2897 | { |
Angel Pons | c674223 | 2020-11-15 13:26:21 +0100 | [diff] [blame] | 2898 | int channel; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2899 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2900 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | 7a61274 | 2020-11-12 13:34:03 +0100 | [diff] [blame] | 2901 | const union tc_rap_reg tc_rap = { |
| 2902 | .tRRD = ctrl->tRRD, |
| 2903 | .tRTP = ctrl->tRTP, |
| 2904 | .tCKE = ctrl->tCKE, |
| 2905 | .tWTR = ctrl->tWTR, |
| 2906 | .tFAW = ctrl->tFAW, |
| 2907 | .tWR = ctrl->tWR, |
| 2908 | .tCMD = ctrl->cmd_stretch[channel], |
| 2909 | }; |
| 2910 | MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2911 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2912 | |
| 2913 | udelay(1); |
| 2914 | |
| 2915 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2916 | wait_for_iosav(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2917 | } |
| 2918 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2919 | FOR_ALL_POPULATED_CHANNELS |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 2920 | MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2921 | |
| 2922 | FOR_ALL_POPULATED_CHANNELS { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2923 | udelay(1); |
Angel Pons | dc5539f | 2020-11-12 12:44:25 +0100 | [diff] [blame] | 2924 | MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | printram("CPE\n"); |
| 2928 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2929 | MCHBAR32(GDCRTRAININGMOD) = 0; |
| 2930 | MCHBAR32(IOSAV_DC_MASK) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2931 | |
| 2932 | printram("CP5b\n"); |
| 2933 | |
| 2934 | FOR_ALL_POPULATED_CHANNELS { |
| 2935 | program_timings(ctrl, channel); |
| 2936 | } |
| 2937 | |
| 2938 | u32 reg, addr; |
| 2939 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2940 | /* Poll for RCOMP */ |
| 2941 | while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16))) |
| 2942 | ; |
| 2943 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2944 | do { |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2945 | reg = MCHBAR32(IOSAV_STATUS_ch(0)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2946 | } while ((reg & 0x14) == 0); |
| 2947 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2948 | /* Set state of memory controller */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2949 | MCHBAR32(MC_INIT_STATE_G) = 0x116; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2950 | MCHBAR32(MC_INIT_STATE) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2951 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2952 | /* Wait 500us */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2953 | udelay(500); |
| 2954 | |
| 2955 | FOR_ALL_CHANNELS { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2956 | /* Set valid rank CKE */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2957 | reg = 0; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2958 | reg = (reg & ~0x0f) | ctrl->rankmap[channel]; |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2959 | addr = MC_INIT_STATE_ch(channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2960 | MCHBAR32(addr) = reg; |
| 2961 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2962 | /* Wait 10ns for ranks to settle */ |
| 2963 | // udelay(0.01); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2964 | |
| 2965 | reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4); |
| 2966 | MCHBAR32(addr) = reg; |
| 2967 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2968 | /* Write reset using a NOP */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2969 | write_reset(ctrl); |
| 2970 | } |
| 2971 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 2972 | /* MRS commands */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2973 | dram_mrscommands(ctrl); |
| 2974 | |
| 2975 | printram("CP5c\n"); |
| 2976 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 2977 | MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2978 | |
| 2979 | FOR_ALL_CHANNELS { |
Angel Pons | 5db1b15 | 2020-12-13 16:37:53 +0100 | [diff] [blame] | 2980 | MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24)); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2981 | udelay(2); |
| 2982 | } |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 2983 | } |