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