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 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 37 | /* FIXME: Vendor BIOS uses 64 but our algorithms are less |
| 38 | performant and even 1 seems to be enough in practice. */ |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 39 | #define NUM_PATTERNS 4 |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 40 | |
Angel Pons | 5c1baf5 | 2020-03-22 12:23:35 +0100 | [diff] [blame] | 41 | /* |
| 42 | * WARNING: Do not forget to increase MRC_CACHE_VERSION when the saved data is changed! |
| 43 | */ |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 44 | #define MRC_CACHE_VERSION 5 |
Angel Pons | 5c1baf5 | 2020-03-22 12:23:35 +0100 | [diff] [blame] | 45 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 46 | typedef struct odtmap_st { |
| 47 | u16 rttwr; |
| 48 | u16 rttnom; |
| 49 | } odtmap; |
| 50 | |
Angel Pons | 5c1baf5 | 2020-03-22 12:23:35 +0100 | [diff] [blame] | 51 | /* 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] | 52 | typedef struct dimm_info_st { |
| 53 | dimm_attr dimm[NUM_CHANNELS][NUM_SLOTS]; |
| 54 | } dimm_info; |
| 55 | |
Angel Pons | 5c1baf5 | 2020-03-22 12:23:35 +0100 | [diff] [blame] | 56 | /* 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] | 57 | struct ram_rank_timings { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 58 | /* ROUNDT_LAT register: One byte per slotrank */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 59 | u8 roundtrip_latency; |
| 60 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 61 | /* IO_LATENCY register: One nibble per slotrank */ |
Felix Held | ef4fe3e | 2019-12-31 14:15:05 +0100 | [diff] [blame] | 62 | u8 io_latency; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 63 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 64 | /* Phase interpolator coding for command and control */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 65 | int pi_coding; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 66 | |
| 67 | struct ram_lane_timings { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 68 | /* Lane register offset 0x10 */ |
| 69 | u16 timA; /* bits 0 - 5, bits 16 - 18 */ |
| 70 | u8 rising; /* bits 8 - 14 */ |
| 71 | u8 falling; /* bits 20 - 26 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 72 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 73 | /* Lane register offset 0x20 */ |
| 74 | int timC; /* bits 0 - 5, 19 */ |
| 75 | u16 timB; /* bits 8 - 13, 15 - 17 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 76 | } lanes[NUM_LANES]; |
| 77 | }; |
| 78 | |
Angel Pons | 5c1baf5 | 2020-03-22 12:23:35 +0100 | [diff] [blame] | 79 | /* 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] | 80 | typedef struct ramctr_timing_st { |
| 81 | u16 spd_crc[NUM_CHANNELS][NUM_SLOTS]; |
Angel Pons | 80037f7 | 2020-03-21 13:12:37 +0100 | [diff] [blame] | 82 | |
| 83 | /* CPUID value */ |
| 84 | u32 cpu; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 85 | |
Patrick Rudolph | 77eaba3 | 2016-11-11 18:55:54 +0100 | [diff] [blame] | 86 | /* DDR base_freq = 100 Mhz / 133 Mhz */ |
| 87 | u8 base_freq; |
| 88 | |
Angel Pons | 48409b8 | 2020-03-23 22:19:29 +0100 | [diff] [blame] | 89 | /* Frequency index */ |
| 90 | u32 FRQ; |
| 91 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 92 | u16 cas_supported; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 93 | /* Latencies are in units of ns, scaled by x256 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 94 | u32 tCK; |
| 95 | u32 tAA; |
| 96 | u32 tWR; |
| 97 | u32 tRCD; |
| 98 | u32 tRRD; |
| 99 | u32 tRP; |
| 100 | u32 tRAS; |
| 101 | u32 tRFC; |
| 102 | u32 tWTR; |
| 103 | u32 tRTP; |
| 104 | u32 tFAW; |
Dan Elkouby | dabebc3 | 2018-04-13 18:47:10 +0300 | [diff] [blame] | 105 | u32 tCWL; |
| 106 | u32 tCMD; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 107 | /* Latencies in terms of clock cycles |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 108 | They are saved separately as they are needed for DRAM MRS commands */ |
| 109 | u8 CAS; /* CAS read latency */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 110 | u8 CWL; /* CAS write latency */ |
| 111 | |
| 112 | u32 tREFI; |
| 113 | u32 tMOD; |
| 114 | u32 tXSOffset; |
| 115 | u32 tWLO; |
| 116 | u32 tCKE; |
| 117 | u32 tXPDLL; |
| 118 | u32 tXP; |
| 119 | u32 tAONPD; |
| 120 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 121 | /* Bits [0..11] of PM_DLL_CONFIG: Master DLL wakeup delay timer */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 122 | u16 mdll_wake_delay; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 123 | |
| 124 | u8 rankmap[NUM_CHANNELS]; |
| 125 | int ref_card_offset[NUM_CHANNELS]; |
| 126 | u32 mad_dimm[NUM_CHANNELS]; |
| 127 | int channel_size_mb[NUM_CHANNELS]; |
| 128 | u32 cmd_stretch[NUM_CHANNELS]; |
| 129 | |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 130 | int pi_code_offset; |
| 131 | int pi_coding_threshold; |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 132 | |
Patrick Rudolph | 05d4bf7e | 2017-10-28 16:36:09 +0200 | [diff] [blame] | 133 | bool ecc_supported; |
| 134 | bool ecc_forced; |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 135 | bool ecc_enabled; |
| 136 | int lanes; /* active lanes: 8 or 9 */ |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 137 | int edge_offset[3]; |
| 138 | int timC_offset[3]; |
| 139 | |
| 140 | int extended_temperature_range; |
| 141 | int auto_self_refresh; |
| 142 | |
| 143 | int rank_mirror[NUM_CHANNELS][NUM_SLOTRANKS]; |
| 144 | |
| 145 | struct ram_rank_timings timings[NUM_CHANNELS][NUM_SLOTRANKS]; |
| 146 | |
| 147 | dimm_info info; |
| 148 | } ramctr_timing; |
| 149 | |
Felix Held | 87ddea2 | 2020-01-26 04:55:27 +0100 | [diff] [blame] | 150 | #define SOUTHBRIDGE PCI_DEV(0, 0x1f, 0) |
| 151 | |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 152 | #define FOR_ALL_LANES for (lane = 0; lane < ctrl->lanes; lane++) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 153 | #define FOR_ALL_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++) |
| 154 | #define FOR_ALL_POPULATED_RANKS for (slotrank = 0; slotrank < NUM_SLOTRANKS; slotrank++) if (ctrl->rankmap[channel] & (1 << slotrank)) |
| 155 | #define FOR_ALL_POPULATED_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++) if (ctrl->rankmap[channel]) |
| 156 | #define MAX_EDGE_TIMING 71 |
| 157 | #define MAX_TIMC 127 |
| 158 | #define MAX_TIMB 511 |
| 159 | #define MAX_TIMA 127 |
| 160 | #define MAX_CAS 18 |
| 161 | #define MIN_CAS 4 |
| 162 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 163 | #define MAKE_ERR ((channel << 16) | (slotrank << 8) | 1) |
| 164 | #define GET_ERR_CHANNEL(x) (x >> 16) |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 165 | |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 166 | u8 get_CWL(u32 tCK); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 167 | void dram_mrscommands(ramctr_timing *ctrl); |
| 168 | void program_timings(ramctr_timing *ctrl, int channel); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 169 | void dram_find_common_params(ramctr_timing *ctrl); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 170 | void dram_xover(ramctr_timing *ctrl); |
| 171 | void dram_timing_regs(ramctr_timing *ctrl); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 172 | void dram_dimm_mapping(ramctr_timing *ctrl); |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 173 | void dram_dimm_set_mapping(ramctr_timing *ctrl, int training); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 174 | void dram_zones(ramctr_timing *ctrl, int training); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 175 | void dram_memorymap(ramctr_timing *ctrl, int me_uma_size); |
| 176 | void dram_jedecreset(ramctr_timing *ctrl); |
| 177 | int read_training(ramctr_timing *ctrl); |
| 178 | int write_training(ramctr_timing *ctrl); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 179 | int command_training(ramctr_timing *ctrl); |
| 180 | int discover_edges(ramctr_timing *ctrl); |
| 181 | int discover_edges_write(ramctr_timing *ctrl); |
| 182 | int discover_timC_write(ramctr_timing *ctrl); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 183 | void normalize_training(ramctr_timing *ctrl); |
| 184 | void write_controller_mr(ramctr_timing *ctrl); |
Patrick Rudolph | fd5fa2a | 2016-11-11 18:22:33 +0100 | [diff] [blame] | 185 | int channel_test(ramctr_timing *ctrl); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 186 | void set_scrambling_seed(ramctr_timing *ctrl); |
Angel Pons | 89ae6b8 | 2020-03-21 13:23:32 +0100 | [diff] [blame] | 187 | void set_wmm_behavior(const u32 cpu); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 188 | void prepare_training(ramctr_timing *ctrl); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 189 | void set_read_write_timings(ramctr_timing *ctrl); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 190 | void set_normal_operation(ramctr_timing *ctrl); |
| 191 | void final_registers(ramctr_timing *ctrl); |
| 192 | void restore_timings(ramctr_timing *ctrl); |
Angel Pons | efbed26 | 2020-03-23 23:18:03 +0100 | [diff] [blame] | 193 | 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] | 194 | |
Patrick Rudolph | dd66287 | 2017-10-28 18:20:11 +0200 | [diff] [blame] | 195 | void channel_scrub(ramctr_timing *ctrl); |
Patrick Rudolph | 05d4bf7e | 2017-10-28 16:36:09 +0200 | [diff] [blame] | 196 | bool get_host_ecc_cap(void); |
| 197 | bool get_host_ecc_forced(void); |
| 198 | |
Patrick Rudolph | 305035c | 2016-11-11 18:38:50 +0100 | [diff] [blame] | 199 | #endif |