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