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