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