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 | |
| 3 | #ifndef RAMINIT_COMMON_H |
| 4 | #define RAMINIT_COMMON_H |
| 5 | |
Felix Held | 380c6b2 | 2020-01-26 05:06:38 +0100 | [diff] [blame] | 6 | #include <stdint.h> |
| 7 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 8 | #define BASEFREQ 133 |
| 9 | #define tDLLK 512 |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 10 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 11 | #define IS_SANDY_CPU(x) ((x & 0xffff0) == 0x206a0) |
| 12 | #define IS_SANDY_CPU_C(x) ((x & 0xf) == 4) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 13 | #define IS_SANDY_CPU_D0(x) ((x & 0xf) == 5) |
| 14 | #define IS_SANDY_CPU_D1(x) ((x & 0xf) == 6) |
| 15 | #define IS_SANDY_CPU_D2(x) ((x & 0xf) == 7) |
| 16 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 17 | #define IS_IVY_CPU(x) ((x & 0xffff0) == 0x306a0) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 18 | #define IS_IVY_CPU_C(x) ((x & 0xf) == 4) |
| 19 | #define IS_IVY_CPU_K(x) ((x & 0xf) == 5) |
| 20 | #define IS_IVY_CPU_D(x) ((x & 0xf) == 6) |
| 21 | #define IS_IVY_CPU_E(x) ((x & 0xf) >= 8) |
| 22 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 23 | #define NUM_CHANNELS 2 |
| 24 | #define NUM_SLOTRANKS 4 |
| 25 | #define NUM_SLOTS 2 |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 26 | #define NUM_LANES 9 |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 27 | |
Angel Pons | 6aa7cca | 2020-05-02 19:38:34 +0200 | [diff] [blame] | 28 | #define IOSAV_MRS (0xf000) |
| 29 | #define IOSAV_PRE (0xf002) |
| 30 | #define IOSAV_ZQCS (0xf003) |
| 31 | #define IOSAV_ACT (0xf006) |
| 32 | #define IOSAV_RD (0xf105) |
| 33 | #define IOSAV_NOP_ALT (0xf107) |
| 34 | #define IOSAV_WR (0xf201) |
| 35 | #define IOSAV_NOP (0xf207) |
Angel Pons | 69e1714 | 2020-03-23 12:26:29 +0100 | [diff] [blame] | 36 | |
Angel Pons | d5b780c | 2020-05-02 21:48:46 +0200 | [diff] [blame^] | 37 | struct iosav_ssq { |
| 38 | /* IOSAV_n_SP_CMD_CTRL */ |
| 39 | union { |
| 40 | struct { |
| 41 | u32 command : 16; |
| 42 | u32 ranksel_ap : 2; |
| 43 | u32 : 14; |
| 44 | }; |
| 45 | u32 raw; |
| 46 | } sp_cmd_ctrl; |
| 47 | |
| 48 | /* IOSAV_n_SUBSEQ_CTRL */ |
| 49 | union { |
| 50 | struct { |
| 51 | u32 cmd_executions : 9; |
| 52 | u32 : 1; |
| 53 | u32 cmd_delay_gap : 5; |
| 54 | u32 : 1; |
| 55 | u32 post_ssq_wait : 9; |
| 56 | u32 : 1; |
| 57 | u32 data_direction : 2; |
| 58 | u32 : 4; |
| 59 | }; |
| 60 | u32 raw; |
| 61 | } subseq_ctrl; |
| 62 | |
| 63 | /* IOSAV_n_SP_CMD_ADDR */ |
| 64 | union { |
| 65 | struct { |
| 66 | u32 address : 16; |
| 67 | u32 rowbits : 3; |
| 68 | u32 : 1; |
| 69 | u32 bank : 3; |
| 70 | u32 : 1; |
| 71 | u32 rank : 2; |
| 72 | u32 : 6; |
| 73 | }; |
| 74 | u32 raw; |
| 75 | } sp_cmd_addr; |
| 76 | |
| 77 | /* IOSAV_n_ADDR_UPDATE */ |
| 78 | union { |
| 79 | struct { |
| 80 | u32 inc_addr_1 : 1; |
| 81 | u32 inc_addr_8 : 1; |
| 82 | u32 inc_bank : 1; |
| 83 | u32 inc_rank : 2; |
| 84 | u32 addr_wrap : 5; |
| 85 | u32 lfsr_upd : 2; |
| 86 | u32 upd_rate : 4; |
| 87 | u32 lfsr_xors : 2; |
| 88 | u32 : 14; |
| 89 | }; |
| 90 | u32 raw; |
| 91 | } addr_update; |
| 92 | }; |
| 93 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 94 | /* FIXME: Vendor BIOS uses 64 but our algorithms are less |
| 95 | performant and even 1 seems to be enough in practice. */ |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 96 | #define NUM_PATTERNS 4 |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 97 | |
Angel Pons | 5c1baf5 | 2020-03-22 12:23:35 +0100 | [diff] [blame] | 98 | /* |
| 99 | * WARNING: Do not forget to increase MRC_CACHE_VERSION when the saved data is changed! |
| 100 | */ |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 101 | #define MRC_CACHE_VERSION 5 |
Angel Pons | 5c1baf5 | 2020-03-22 12:23:35 +0100 | [diff] [blame] | 102 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 103 | typedef struct odtmap_st { |
| 104 | u16 rttwr; |
| 105 | u16 rttnom; |
| 106 | } odtmap; |
| 107 | |
Angel Pons | 5c1baf5 | 2020-03-22 12:23:35 +0100 | [diff] [blame] | 108 | /* WARNING: Do not forget to increase MRC_CACHE_VERSION when this struct is changed! */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 109 | typedef struct dimm_info_st { |
| 110 | dimm_attr dimm[NUM_CHANNELS][NUM_SLOTS]; |
| 111 | } dimm_info; |
| 112 | |
Angel Pons | 5c1baf5 | 2020-03-22 12:23:35 +0100 | [diff] [blame] | 113 | /* WARNING: Do not forget to increase MRC_CACHE_VERSION when this struct is changed! */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 114 | struct ram_rank_timings { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 115 | /* ROUNDT_LAT register: One byte per slotrank */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 116 | u8 roundtrip_latency; |
| 117 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 118 | /* IO_LATENCY register: One nibble per slotrank */ |
Felix Held | ef4fe3e | 2019-12-31 14:15:05 +0100 | [diff] [blame] | 119 | u8 io_latency; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 120 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 121 | /* Phase interpolator coding for command and control */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 122 | int pi_coding; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 123 | |
| 124 | struct ram_lane_timings { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 125 | /* Lane register offset 0x10 */ |
| 126 | u16 timA; /* bits 0 - 5, bits 16 - 18 */ |
| 127 | u8 rising; /* bits 8 - 14 */ |
| 128 | u8 falling; /* bits 20 - 26 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 129 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 130 | /* Lane register offset 0x20 */ |
| 131 | int timC; /* bits 0 - 5, 19 */ |
| 132 | u16 timB; /* bits 8 - 13, 15 - 17 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 133 | } lanes[NUM_LANES]; |
| 134 | }; |
| 135 | |
Angel Pons | 5c1baf5 | 2020-03-22 12:23:35 +0100 | [diff] [blame] | 136 | /* WARNING: Do not forget to increase MRC_CACHE_VERSION when this struct is changed! */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 137 | typedef struct ramctr_timing_st { |
| 138 | u16 spd_crc[NUM_CHANNELS][NUM_SLOTS]; |
Angel Pons | 80037f7 | 2020-03-21 13:12:37 +0100 | [diff] [blame] | 139 | |
| 140 | /* CPUID value */ |
| 141 | u32 cpu; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 142 | |
Patrick Rudolph | 77eaba3 | 2016-11-11 18:55:54 +0100 | [diff] [blame] | 143 | /* DDR base_freq = 100 Mhz / 133 Mhz */ |
| 144 | u8 base_freq; |
| 145 | |
Angel Pons | 48409b8 | 2020-03-23 22:19:29 +0100 | [diff] [blame] | 146 | /* Frequency index */ |
| 147 | u32 FRQ; |
| 148 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 149 | u16 cas_supported; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 150 | /* Latencies are in units of ns, scaled by x256 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 151 | u32 tCK; |
| 152 | u32 tAA; |
| 153 | u32 tWR; |
| 154 | u32 tRCD; |
| 155 | u32 tRRD; |
| 156 | u32 tRP; |
| 157 | u32 tRAS; |
| 158 | u32 tRFC; |
| 159 | u32 tWTR; |
| 160 | u32 tRTP; |
| 161 | u32 tFAW; |
Dan Elkouby | dabebc3 | 2018-04-13 18:47:10 +0300 | [diff] [blame] | 162 | u32 tCWL; |
| 163 | u32 tCMD; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 164 | /* Latencies in terms of clock cycles |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 165 | They are saved separately as they are needed for DRAM MRS commands */ |
| 166 | u8 CAS; /* CAS read latency */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 167 | u8 CWL; /* CAS write latency */ |
| 168 | |
| 169 | u32 tREFI; |
| 170 | u32 tMOD; |
| 171 | u32 tXSOffset; |
| 172 | u32 tWLO; |
| 173 | u32 tCKE; |
| 174 | u32 tXPDLL; |
| 175 | u32 tXP; |
| 176 | u32 tAONPD; |
| 177 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 178 | /* Bits [0..11] of PM_DLL_CONFIG: Master DLL wakeup delay timer */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 179 | u16 mdll_wake_delay; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 180 | |
| 181 | u8 rankmap[NUM_CHANNELS]; |
| 182 | int ref_card_offset[NUM_CHANNELS]; |
| 183 | u32 mad_dimm[NUM_CHANNELS]; |
| 184 | int channel_size_mb[NUM_CHANNELS]; |
| 185 | u32 cmd_stretch[NUM_CHANNELS]; |
| 186 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 187 | int pi_code_offset; |
| 188 | int pi_coding_threshold; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 189 | |
Patrick Rudolph | 05d4bf7e | 2017-10-28 16:36:09 +0200 | [diff] [blame] | 190 | bool ecc_supported; |
| 191 | bool ecc_forced; |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 192 | bool ecc_enabled; |
| 193 | int lanes; /* active lanes: 8 or 9 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 194 | int edge_offset[3]; |
| 195 | int timC_offset[3]; |
| 196 | |
| 197 | int extended_temperature_range; |
| 198 | int auto_self_refresh; |
| 199 | |
| 200 | int rank_mirror[NUM_CHANNELS][NUM_SLOTRANKS]; |
| 201 | |
| 202 | struct ram_rank_timings timings[NUM_CHANNELS][NUM_SLOTRANKS]; |
| 203 | |
| 204 | dimm_info info; |
| 205 | } ramctr_timing; |
| 206 | |
Felix Held | 87ddea2 | 2020-01-26 04:55:27 +0100 | [diff] [blame] | 207 | #define SOUTHBRIDGE PCI_DEV(0, 0x1f, 0) |
| 208 | |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 209 | #define FOR_ALL_LANES for (lane = 0; lane < ctrl->lanes; lane++) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 210 | #define FOR_ALL_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++) |
| 211 | #define FOR_ALL_POPULATED_RANKS for (slotrank = 0; slotrank < NUM_SLOTRANKS; slotrank++) if (ctrl->rankmap[channel] & (1 << slotrank)) |
| 212 | #define FOR_ALL_POPULATED_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++) if (ctrl->rankmap[channel]) |
| 213 | #define MAX_EDGE_TIMING 71 |
| 214 | #define MAX_TIMC 127 |
| 215 | #define MAX_TIMB 511 |
| 216 | #define MAX_TIMA 127 |
| 217 | #define MAX_CAS 18 |
| 218 | #define MIN_CAS 4 |
| 219 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 220 | #define MAKE_ERR ((channel << 16) | (slotrank << 8) | 1) |
| 221 | #define GET_ERR_CHANNEL(x) (x >> 16) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 222 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 223 | u8 get_CWL(u32 tCK); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 224 | void dram_mrscommands(ramctr_timing *ctrl); |
| 225 | void program_timings(ramctr_timing *ctrl, int channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 226 | void dram_find_common_params(ramctr_timing *ctrl); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 227 | void dram_xover(ramctr_timing *ctrl); |
| 228 | void dram_timing_regs(ramctr_timing *ctrl); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 229 | void dram_dimm_mapping(ramctr_timing *ctrl); |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 230 | void dram_dimm_set_mapping(ramctr_timing *ctrl, int training); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 231 | void dram_zones(ramctr_timing *ctrl, int training); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 232 | void dram_memorymap(ramctr_timing *ctrl, int me_uma_size); |
| 233 | void dram_jedecreset(ramctr_timing *ctrl); |
| 234 | int read_training(ramctr_timing *ctrl); |
| 235 | int write_training(ramctr_timing *ctrl); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 236 | int command_training(ramctr_timing *ctrl); |
| 237 | int discover_edges(ramctr_timing *ctrl); |
| 238 | int discover_edges_write(ramctr_timing *ctrl); |
| 239 | int discover_timC_write(ramctr_timing *ctrl); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 240 | void normalize_training(ramctr_timing *ctrl); |
| 241 | void write_controller_mr(ramctr_timing *ctrl); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 242 | int channel_test(ramctr_timing *ctrl); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 243 | void set_scrambling_seed(ramctr_timing *ctrl); |
Angel Pons | 89ae6b8 | 2020-03-21 13:23:32 +0100 | [diff] [blame] | 244 | void set_wmm_behavior(const u32 cpu); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 245 | void prepare_training(ramctr_timing *ctrl); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 246 | void set_read_write_timings(ramctr_timing *ctrl); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 247 | void set_normal_operation(ramctr_timing *ctrl); |
| 248 | void final_registers(ramctr_timing *ctrl); |
| 249 | void restore_timings(ramctr_timing *ctrl); |
Angel Pons | efbed26 | 2020-03-23 23:18:03 +0100 | [diff] [blame] | 250 | int try_init_dram_ddr3(ramctr_timing *ctrl, int fast_boot, int s3resume, int me_uma_size); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 251 | |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 252 | void channel_scrub(ramctr_timing *ctrl); |
Patrick Rudolph | 05d4bf7e | 2017-10-28 16:36:09 +0200 | [diff] [blame] | 253 | bool get_host_ecc_cap(void); |
| 254 | bool get_host_ecc_forced(void); |
| 255 | |
Patrick Rudolph | 305035c | 2016-11-11 18:38:50 +0100 | [diff] [blame] | 256 | #endif |