Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4 | * Copyright (C) 2014 Damien Zammit <damien@zamaudio.com> |
| 5 | * Copyright (C) 2014 Vladimir Serbinenko <phcoder@gmail.com> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <console/console.h> |
Kyösti Mälkki | 1d7541f | 2014-02-17 21:34:42 +0200 | [diff] [blame] | 18 | #include <console/usb.h> |
Kyösti Mälkki | 5687fc9 | 2013-11-28 18:11:49 +0200 | [diff] [blame] | 19 | #include <bootmode.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 20 | #include <string.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 21 | #include <arch/io.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 22 | #include <cbmem.h> |
| 23 | #include <arch/cbfs.h> |
| 24 | #include <cbfs.h> |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 25 | #include <halt.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 26 | #include <ip_checksum.h> |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 27 | #include <timestamp.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 28 | #include <pc80/mc146818rtc.h> |
Alexander Couzens | 81c5c76 | 2016-03-09 03:13:45 +0100 | [diff] [blame] | 29 | #include <northbridge/intel/common/mrc_cache.h> |
Duncan Laurie | 7b508dd | 2012-04-09 12:30:43 -0700 | [diff] [blame] | 30 | #include <device/pci_def.h> |
Patrick Rudolph | b97009e | 2016-02-28 15:24:04 +0100 | [diff] [blame] | 31 | #include <memory_info.h> |
| 32 | #include <smbios.h> |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 33 | #include "raminit_native.h" |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 34 | #include "sandybridge.h" |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 35 | #include <delay.h> |
| 36 | #include <lib.h> |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 37 | #include <device/device.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 38 | |
| 39 | /* Management Engine is in the southbridge */ |
| 40 | #include "southbridge/intel/bd82x6x/me.h" |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 41 | /* For SPD. */ |
| 42 | #include "southbridge/intel/bd82x6x/smbus.h" |
| 43 | #include "arch/cpu.h" |
| 44 | #include "cpu/x86/msr.h" |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 45 | #include <northbridge/intel/sandybridge/chip.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 46 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 47 | /* FIXME: no ECC support. */ |
| 48 | /* FIXME: no support for 3-channel chipsets. */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 49 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 50 | /* |
| 51 | * Register description: |
| 52 | * Intel provides a command queue of depth four. |
| 53 | * Every command is configured by using multiple registers. |
| 54 | * On executing the command queue you have to provide the depth used. |
| 55 | * |
| 56 | * Known registers: |
| 57 | * Channel X = [0, 1] |
| 58 | * Command queue index Y = [0, 1, 2, 3] |
| 59 | * |
| 60 | * DEFAULT_MCHBAR + 0x4220 + 0x400 * X + 4 * Y: command io register |
| 61 | * Controls the DRAM command signals |
| 62 | * Bit 0: !RAS |
| 63 | * Bit 1: !CAS |
| 64 | * Bit 2: !WE |
| 65 | * |
| 66 | * DEFAULT_MCHBAR + 0x4200 + 0x400 * X + 4 * Y: addr bankslot io register |
| 67 | * Controls the address, bank address and slotrank signals |
| 68 | * Bit 0-15 : Address |
| 69 | * Bit 20-22: Bank Address |
| 70 | * Bit 24-25: slotrank |
| 71 | * |
| 72 | * DEFAULT_MCHBAR + 0x4230 + 0x400 * X + 4 * Y: idle register |
| 73 | * Controls the idle time after issuing this DRAM command |
| 74 | * Bit 16-32: number of clock-cylces to idle |
| 75 | * |
| 76 | * DEFAULT_MCHBAR + 0x4284 + 0x400 * channel: execute command queue |
| 77 | * Starts to execute all queued commands |
| 78 | * Bit 0 : start DRAM command execution |
| 79 | * Bit 16-20: (number of queued commands - 1) * 4 |
| 80 | */ |
| 81 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 82 | #define BASEFREQ 133 |
| 83 | #define tDLLK 512 |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 84 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 85 | #define IS_SANDY_CPU(x) ((x & 0xffff0) == 0x206a0) |
| 86 | #define IS_SANDY_CPU_C(x) ((x & 0xf) == 4) |
| 87 | #define IS_SANDY_CPU_D0(x) ((x & 0xf) == 5) |
| 88 | #define IS_SANDY_CPU_D1(x) ((x & 0xf) == 6) |
| 89 | #define IS_SANDY_CPU_D2(x) ((x & 0xf) == 7) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 90 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 91 | #define IS_IVY_CPU(x) ((x & 0xffff0) == 0x306a0) |
| 92 | #define IS_IVY_CPU_C(x) ((x & 0xf) == 4) |
| 93 | #define IS_IVY_CPU_K(x) ((x & 0xf) == 5) |
| 94 | #define IS_IVY_CPU_D(x) ((x & 0xf) == 6) |
| 95 | #define IS_IVY_CPU_E(x) ((x & 0xf) >= 8) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 96 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 97 | #define NUM_CHANNELS 2 |
| 98 | #define NUM_SLOTRANKS 4 |
| 99 | #define NUM_SLOTS 2 |
| 100 | #define NUM_LANES 8 |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 101 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 102 | /* FIXME: Vendor BIOS uses 64 but our algorithms are less |
| 103 | performant and even 1 seems to be enough in practice. */ |
| 104 | #define NUM_PATTERNS 4 |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 105 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 106 | typedef struct odtmap_st { |
| 107 | u16 rttwr; |
| 108 | u16 rttnom; |
| 109 | } odtmap; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 110 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 111 | typedef struct dimm_info_st { |
| 112 | dimm_attr dimm[NUM_CHANNELS][NUM_SLOTS]; |
| 113 | } dimm_info; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 114 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 115 | struct ram_rank_timings { |
| 116 | /* Register 4024. One byte per slotrank. */ |
| 117 | u8 val_4024; |
| 118 | /* Register 4028. One nibble per slotrank. */ |
| 119 | u8 val_4028; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 120 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 121 | int val_320c; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 122 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 123 | struct ram_lane_timings { |
| 124 | /* lane register offset 0x10. */ |
| 125 | u16 timA; /* bits 0 - 5, bits 16 - 18 */ |
| 126 | u8 rising; /* bits 8 - 14 */ |
| 127 | u8 falling; /* bits 20 - 26. */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 128 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 129 | /* lane register offset 0x20. */ |
| 130 | int timC; /* bit 0 - 5, 19. */ |
| 131 | u16 timB; /* bits 8 - 13, 15 - 17. */ |
| 132 | } lanes[NUM_LANES]; |
| 133 | }; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 134 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 135 | struct ramctr_timing_st; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 136 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 137 | typedef struct ramctr_timing_st { |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 138 | u16 spd_crc[NUM_CHANNELS][NUM_SLOTS]; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 139 | int mobile; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 140 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 141 | u16 cas_supported; |
| 142 | /* tLatencies are in units of ns, scaled by x256 */ |
| 143 | u32 tCK; |
| 144 | u32 tAA; |
| 145 | u32 tWR; |
| 146 | u32 tRCD; |
| 147 | u32 tRRD; |
| 148 | u32 tRP; |
| 149 | u32 tRAS; |
| 150 | u32 tRFC; |
| 151 | u32 tWTR; |
| 152 | u32 tRTP; |
| 153 | u32 tFAW; |
| 154 | /* Latencies in terms of clock cycles |
| 155 | * They are saved separately as they are needed for DRAM MRS commands*/ |
| 156 | u8 CAS; /* CAS read latency */ |
| 157 | u8 CWL; /* CAS write latency */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 158 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 159 | u32 tREFI; |
| 160 | u32 tMOD; |
| 161 | u32 tXSOffset; |
| 162 | u32 tWLO; |
| 163 | u32 tCKE; |
| 164 | u32 tXPDLL; |
| 165 | u32 tXP; |
| 166 | u32 tAONPD; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 167 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 168 | u16 reg_5064b0; /* bits 0-11. */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 169 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 170 | u8 rankmap[NUM_CHANNELS]; |
| 171 | int ref_card_offset[NUM_CHANNELS]; |
| 172 | u32 mad_dimm[NUM_CHANNELS]; |
| 173 | int channel_size_mb[NUM_CHANNELS]; |
| 174 | u32 cmd_stretch[NUM_CHANNELS]; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 175 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 176 | int reg_c14_offset; |
| 177 | int reg_320c_range_threshold; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 178 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 179 | int edge_offset[3]; |
| 180 | int timC_offset[3]; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 181 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 182 | int extended_temperature_range; |
| 183 | int auto_self_refresh; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 184 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 185 | int rank_mirror[NUM_CHANNELS][NUM_SLOTRANKS]; |
| 186 | |
| 187 | struct ram_rank_timings timings[NUM_CHANNELS][NUM_SLOTRANKS]; |
Patrick Rudolph | 735ecce | 2016-03-26 10:42:27 +0100 | [diff] [blame] | 188 | |
| 189 | dimm_info info; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 190 | } ramctr_timing; |
| 191 | |
| 192 | #define SOUTHBRIDGE PCI_DEV(0, 0x1f, 0) |
| 193 | #define NORTHBRIDGE PCI_DEV(0, 0x0, 0) |
| 194 | #define FOR_ALL_LANES for (lane = 0; lane < NUM_LANES; lane++) |
| 195 | #define FOR_ALL_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++) |
| 196 | #define FOR_ALL_POPULATED_RANKS for (slotrank = 0; slotrank < NUM_SLOTRANKS; slotrank++) if (ctrl->rankmap[channel] & (1 << slotrank)) |
| 197 | #define FOR_ALL_POPULATED_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++) if (ctrl->rankmap[channel]) |
| 198 | #define MAX_EDGE_TIMING 71 |
| 199 | #define MAX_TIMC 127 |
| 200 | #define MAX_TIMB 511 |
| 201 | #define MAX_TIMA 127 |
| 202 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 203 | #define MAKE_ERR ((channel<<16)|(slotrank<<8)|1) |
Patrick Rudolph | 2ccb74b | 2016-03-26 12:16:29 +0100 | [diff] [blame] | 204 | #define GET_ERR_CHANNEL(x) (x>>16) |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 205 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 206 | static void program_timings(ramctr_timing * ctrl, int channel); |
Patrick Rudolph | 266a1f7 | 2016-06-09 18:13:34 +0200 | [diff] [blame] | 207 | static unsigned int get_mmio_size(void); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 208 | |
| 209 | static const char *ecc_decoder[] = { |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 210 | "inactive", |
| 211 | "active on IO", |
| 212 | "disabled on IO", |
| 213 | "active" |
| 214 | }; |
| 215 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 216 | static void wait_txt_clear(void) |
| 217 | { |
| 218 | struct cpuid_result cp; |
| 219 | |
| 220 | cp = cpuid_ext(0x1, 0x0); |
| 221 | /* Check if TXT is supported? */ |
| 222 | if (!(cp.ecx & 0x40)) |
| 223 | return; |
| 224 | /* Some TXT public bit. */ |
| 225 | if (!(read32((void *)0xfed30010) & 1)) |
| 226 | return; |
| 227 | /* Wait for TXT clear. */ |
| 228 | while (!(read8((void *)0xfed40000) & (1 << 7))) ; |
| 229 | } |
| 230 | |
| 231 | static void sfence(void) |
| 232 | { |
| 233 | asm volatile ("sfence"); |
| 234 | } |
| 235 | |
Patrick Rudolph | 9b51568 | 2015-10-09 13:43:51 +0200 | [diff] [blame] | 236 | static void toggle_io_reset(void) { |
| 237 | /* toggle IO reset bit */ |
| 238 | u32 r32 = read32(DEFAULT_MCHBAR + 0x5030); |
| 239 | write32(DEFAULT_MCHBAR + 0x5030, r32 | 0x20); |
| 240 | udelay(1); |
| 241 | write32(DEFAULT_MCHBAR + 0x5030, r32 & ~0x20); |
| 242 | udelay(1); |
| 243 | } |
| 244 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 245 | /* |
Patrick Rudolph | 2ccb74b | 2016-03-26 12:16:29 +0100 | [diff] [blame] | 246 | * Disable a channel in ramctr_timing. |
| 247 | */ |
| 248 | static void disable_channel(ramctr_timing *ctrl, int channel) { |
| 249 | ctrl->rankmap[channel] = 0; |
| 250 | memset(&ctrl->rank_mirror[channel][0], 0, sizeof(ctrl->rank_mirror[0])); |
| 251 | ctrl->channel_size_mb[channel] = 0; |
| 252 | ctrl->cmd_stretch[channel] = 0; |
| 253 | ctrl->mad_dimm[channel] = 0; |
| 254 | memset(&ctrl->timings[channel][0], 0, sizeof(ctrl->timings[0])); |
| 255 | } |
| 256 | |
| 257 | /* |
Patrick Rudolph | b97009e | 2016-02-28 15:24:04 +0100 | [diff] [blame] | 258 | * Fill cbmem with information for SMBIOS type 17. |
| 259 | */ |
Patrick Rudolph | 735ecce | 2016-03-26 10:42:27 +0100 | [diff] [blame] | 260 | static void fill_smbios17(ramctr_timing *ctrl) |
Patrick Rudolph | b97009e | 2016-02-28 15:24:04 +0100 | [diff] [blame] | 261 | { |
| 262 | struct memory_info *mem_info; |
| 263 | int channel, slot; |
| 264 | struct dimm_info *dimm; |
Patrick Rudolph | 735ecce | 2016-03-26 10:42:27 +0100 | [diff] [blame] | 265 | uint16_t ddr_freq; |
| 266 | dimm_info *info = &ctrl->info; |
| 267 | |
| 268 | ddr_freq = (1000 << 8) / ctrl->tCK; |
Patrick Rudolph | b97009e | 2016-02-28 15:24:04 +0100 | [diff] [blame] | 269 | |
| 270 | /* |
| 271 | * Allocate CBMEM area for DIMM information used to populate SMBIOS |
| 272 | * table 17 |
| 273 | */ |
| 274 | mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(*mem_info)); |
| 275 | printk(BIOS_DEBUG, "CBMEM entry for DIMM info: 0x%p\n", mem_info); |
| 276 | if (!mem_info) |
| 277 | return; |
| 278 | |
| 279 | memset(mem_info, 0, sizeof(*mem_info)); |
| 280 | |
| 281 | FOR_ALL_CHANNELS for(slot = 0; slot < NUM_SLOTS; slot++) { |
| 282 | dimm = &mem_info->dimm[mem_info->dimm_cnt]; |
| 283 | if (info->dimm[channel][slot].size_mb) { |
| 284 | dimm->ddr_type = MEMORY_TYPE_DDR3; |
| 285 | dimm->ddr_frequency = ddr_freq; |
| 286 | dimm->dimm_size = info->dimm[channel][slot].size_mb; |
| 287 | dimm->channel_num = channel; |
| 288 | dimm->rank_per_dimm = info->dimm[channel][slot].ranks; |
| 289 | dimm->dimm_num = slot; |
| 290 | memcpy(dimm->module_part_number, |
| 291 | info->dimm[channel][slot].part_number, 16); |
| 292 | dimm->mod_id = info->dimm[channel][slot].manufacturer_id; |
| 293 | dimm->mod_type = info->dimm[channel][slot].dimm_type; |
| 294 | dimm->bus_width = info->dimm[channel][slot].width; |
| 295 | mem_info->dimm_cnt++; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /* |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 301 | * Dump in the log memory controller configuration as read from the memory |
| 302 | * controller registers. |
| 303 | */ |
| 304 | static void report_memory_config(void) |
| 305 | { |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 306 | u32 addr_decoder_common, addr_decode_ch[NUM_CHANNELS]; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 307 | int i; |
| 308 | |
| 309 | addr_decoder_common = MCHBAR32(0x5000); |
| 310 | addr_decode_ch[0] = MCHBAR32(0x5004); |
| 311 | addr_decode_ch[1] = MCHBAR32(0x5008); |
| 312 | |
| 313 | printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n", |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 314 | (MCHBAR32(0x5e04) * 13333 * 2 + 50) / 100); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 315 | printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n", |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 316 | addr_decoder_common & 3, (addr_decoder_common >> 2) & 3, |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 317 | (addr_decoder_common >> 4) & 3); |
| 318 | |
| 319 | for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) { |
| 320 | u32 ch_conf = addr_decode_ch[i]; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 321 | printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i, |
| 322 | ch_conf); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 323 | printk(BIOS_DEBUG, " ECC %s\n", |
| 324 | ecc_decoder[(ch_conf >> 24) & 3]); |
| 325 | printk(BIOS_DEBUG, " enhanced interleave mode %s\n", |
| 326 | ((ch_conf >> 22) & 1) ? "on" : "off"); |
| 327 | printk(BIOS_DEBUG, " rank interleave %s\n", |
| 328 | ((ch_conf >> 21) & 1) ? "on" : "off"); |
| 329 | printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n", |
| 330 | ((ch_conf >> 0) & 0xff) * 256, |
| 331 | ((ch_conf >> 19) & 1) ? 16 : 8, |
| 332 | ((ch_conf >> 17) & 1) ? "dual" : "single", |
| 333 | ((ch_conf >> 16) & 1) ? "" : ", selected"); |
| 334 | printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n", |
| 335 | ((ch_conf >> 8) & 0xff) * 256, |
| 336 | ((ch_conf >> 20) & 1) ? 16 : 8, |
| 337 | ((ch_conf >> 18) & 1) ? "dual" : "single", |
| 338 | ((ch_conf >> 16) & 1) ? ", selected" : ""); |
| 339 | } |
| 340 | } |
| 341 | |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 342 | /* |
| 343 | * Return CRC16 match for all SPDs. |
| 344 | */ |
| 345 | static int verify_crc16_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl) |
| 346 | { |
| 347 | int channel, slot, spd_slot; |
| 348 | int match = 1; |
| 349 | |
| 350 | FOR_ALL_CHANNELS { |
| 351 | for (slot = 0; slot < NUM_SLOTS; slot++) { |
| 352 | spd_slot = 2 * channel + slot; |
| 353 | match &= ctrl->spd_crc[channel][slot] == |
| 354 | spd_ddr3_calc_crc(spd[spd_slot], sizeof(spd_raw_data)); |
| 355 | } |
| 356 | } |
| 357 | return match; |
| 358 | } |
| 359 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 360 | void read_spd(spd_raw_data * spd, u8 addr) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 361 | { |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 362 | int j; |
| 363 | for (j = 0; j < 256; j++) |
| 364 | (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j); |
| 365 | } |
| 366 | |
Patrick Rudolph | 735ecce | 2016-03-26 10:42:27 +0100 | [diff] [blame] | 367 | static void dram_find_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 368 | { |
Patrick Rudolph | bd1fdc6 | 2016-01-26 08:45:21 +0100 | [diff] [blame] | 369 | int dimms = 0, dimms_on_channel; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 370 | int channel, slot, spd_slot; |
Patrick Rudolph | 735ecce | 2016-03-26 10:42:27 +0100 | [diff] [blame] | 371 | dimm_info *dimm = &ctrl->info; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 372 | |
| 373 | memset (ctrl->rankmap, 0, sizeof (ctrl->rankmap)); |
| 374 | |
| 375 | ctrl->extended_temperature_range = 1; |
| 376 | ctrl->auto_self_refresh = 1; |
| 377 | |
| 378 | FOR_ALL_CHANNELS { |
| 379 | ctrl->channel_size_mb[channel] = 0; |
| 380 | |
Patrick Rudolph | bd1fdc6 | 2016-01-26 08:45:21 +0100 | [diff] [blame] | 381 | dimms_on_channel = 0; |
| 382 | /* count dimms on channel */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 383 | for (slot = 0; slot < NUM_SLOTS; slot++) { |
| 384 | spd_slot = 2 * channel + slot; |
| 385 | spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]); |
Patrick Rudolph | bd1fdc6 | 2016-01-26 08:45:21 +0100 | [diff] [blame] | 386 | if (dimm->dimm[channel][slot].dram_type == SPD_MEMORY_TYPE_SDRAM_DDR3) |
| 387 | dimms_on_channel++; |
| 388 | } |
| 389 | |
| 390 | for (slot = 0; slot < NUM_SLOTS; slot++) { |
| 391 | spd_slot = 2 * channel + slot; |
| 392 | /* search for XMP profile */ |
| 393 | spd_xmp_decode_ddr3(&dimm->dimm[channel][slot], |
| 394 | spd[spd_slot], |
| 395 | DDR3_XMP_PROFILE_1); |
| 396 | |
| 397 | if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) { |
| 398 | printram("No valid XMP profile found.\n"); |
| 399 | spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]); |
| 400 | } else if (dimms_on_channel > dimm->dimm[channel][slot].dimms_per_channel) { |
| 401 | printram("XMP profile supports %u DIMMs, but %u DIMMs are installed.\n", |
| 402 | dimm->dimm[channel][slot].dimms_per_channel, |
| 403 | dimms_on_channel); |
| 404 | spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]); |
| 405 | } else if (dimm->dimm[channel][slot].voltage != 1500) { |
| 406 | /* TODO: support other DDR3 voltage than 1500mV */ |
| 407 | printram("XMP profile's requested %u mV is unsupported.\n", |
| 408 | dimm->dimm[channel][slot].voltage); |
| 409 | spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]); |
| 410 | } |
| 411 | |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 412 | /* fill in CRC16 for MRC cache */ |
| 413 | ctrl->spd_crc[channel][slot] = |
| 414 | spd_ddr3_calc_crc(spd[spd_slot], sizeof(spd_raw_data)); |
| 415 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 416 | if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) { |
| 417 | // set dimm invalid |
| 418 | dimm->dimm[channel][slot].ranks = 0; |
| 419 | dimm->dimm[channel][slot].size_mb = 0; |
| 420 | continue; |
| 421 | } |
| 422 | |
| 423 | dram_print_spd_ddr3(&dimm->dimm[channel][slot]); |
| 424 | dimms++; |
| 425 | ctrl->rank_mirror[channel][slot * 2] = 0; |
| 426 | ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->dimm[channel][slot].flags.pins_mirrored; |
| 427 | ctrl->channel_size_mb[channel] += dimm->dimm[channel][slot].size_mb; |
| 428 | |
| 429 | ctrl->auto_self_refresh &= dimm->dimm[channel][slot].flags.asr; |
| 430 | ctrl->extended_temperature_range &= dimm->dimm[channel][slot].flags.ext_temp_refresh; |
| 431 | |
| 432 | ctrl->rankmap[channel] |= ((1 << dimm->dimm[channel][slot].ranks) - 1) << (2 * slot); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 433 | printk(BIOS_DEBUG, "channel[%d] rankmap = 0x%x\n", |
| 434 | channel, ctrl->rankmap[channel]); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 435 | } |
| 436 | if ((ctrl->rankmap[channel] & 3) && (ctrl->rankmap[channel] & 0xc) |
| 437 | && dimm->dimm[channel][0].reference_card <= 5 && dimm->dimm[channel][1].reference_card <= 5) { |
| 438 | const int ref_card_offset_table[6][6] = { |
| 439 | { 0, 0, 0, 0, 2, 2, }, |
| 440 | { 0, 0, 0, 0, 2, 2, }, |
| 441 | { 0, 0, 0, 0, 2, 2, }, |
| 442 | { 0, 0, 0, 0, 1, 1, }, |
| 443 | { 2, 2, 2, 1, 0, 0, }, |
| 444 | { 2, 2, 2, 1, 0, 0, }, |
| 445 | }; |
| 446 | ctrl->ref_card_offset[channel] = ref_card_offset_table[dimm->dimm[channel][0].reference_card] |
| 447 | [dimm->dimm[channel][1].reference_card]; |
| 448 | } else |
| 449 | ctrl->ref_card_offset[channel] = 0; |
| 450 | } |
| 451 | |
| 452 | if (!dimms) |
| 453 | die("No DIMMs were found"); |
| 454 | } |
| 455 | |
Patrick Rudolph | 735ecce | 2016-03-26 10:42:27 +0100 | [diff] [blame] | 456 | static void dram_find_common_params(ramctr_timing *ctrl) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 457 | { |
| 458 | size_t valid_dimms; |
| 459 | int channel, slot; |
Patrick Rudolph | 735ecce | 2016-03-26 10:42:27 +0100 | [diff] [blame] | 460 | dimm_info *dimms = &ctrl->info; |
| 461 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 462 | ctrl->cas_supported = 0xff; |
| 463 | valid_dimms = 0; |
| 464 | FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) { |
| 465 | const dimm_attr *dimm = &dimms->dimm[channel][slot]; |
| 466 | if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) |
| 467 | continue; |
| 468 | valid_dimms++; |
| 469 | |
| 470 | /* Find all possible CAS combinations */ |
| 471 | ctrl->cas_supported &= dimm->cas_supported; |
| 472 | |
| 473 | /* Find the smallest common latencies supported by all DIMMs */ |
| 474 | ctrl->tCK = MAX(ctrl->tCK, dimm->tCK); |
| 475 | ctrl->tAA = MAX(ctrl->tAA, dimm->tAA); |
| 476 | ctrl->tWR = MAX(ctrl->tWR, dimm->tWR); |
| 477 | ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD); |
| 478 | ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD); |
| 479 | ctrl->tRP = MAX(ctrl->tRP, dimm->tRP); |
| 480 | ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS); |
| 481 | ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC); |
| 482 | ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR); |
| 483 | ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP); |
| 484 | ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW); |
| 485 | } |
| 486 | |
| 487 | if (!ctrl->cas_supported) |
| 488 | die("Unsupported DIMM combination. " |
| 489 | "DIMMS do not support common CAS latency"); |
| 490 | if (!valid_dimms) |
| 491 | die("No valid DIMMs found"); |
| 492 | } |
| 493 | |
| 494 | static u8 get_CWL(u8 CAS) |
| 495 | { |
| 496 | /* Get CWL based on CAS using the following rule: |
| 497 | * _________________________________________ |
| 498 | * CAS: | 4T | 5T | 6T | 7T | 8T | 9T | 10T | 11T | |
| 499 | * CWL: | 5T | 5T | 5T | 6T | 6T | 7T | 7T | 8T | |
| 500 | */ |
| 501 | static const u8 cas_cwl_map[] = { 5, 5, 5, 6, 6, 7, 7, 8 }; |
| 502 | if (CAS > 11) |
| 503 | return 8; |
| 504 | return cas_cwl_map[CAS - 4]; |
| 505 | } |
| 506 | |
| 507 | /* Frequency multiplier. */ |
| 508 | static u32 get_FRQ(u32 tCK) |
| 509 | { |
| 510 | u32 FRQ; |
| 511 | FRQ = 256000 / (tCK * BASEFREQ); |
| 512 | if (FRQ > 8) |
| 513 | return 8; |
| 514 | if (FRQ < 3) |
| 515 | return 3; |
| 516 | return FRQ; |
| 517 | } |
| 518 | |
| 519 | static u32 get_REFI(u32 tCK) |
| 520 | { |
| 521 | /* Get REFI based on MCU frequency using the following rule: |
| 522 | * _________________________________________ |
| 523 | * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 | |
| 524 | * REFI: | 3120 | 4160 | 5200 | 6240 | 7280 | 8320 | |
| 525 | */ |
| 526 | static const u32 frq_refi_map[] = |
| 527 | { 3120, 4160, 5200, 6240, 7280, 8320 }; |
| 528 | return frq_refi_map[get_FRQ(tCK) - 3]; |
| 529 | } |
| 530 | |
| 531 | static u8 get_XSOffset(u32 tCK) |
| 532 | { |
| 533 | /* Get XSOffset based on MCU frequency using the following rule: |
| 534 | * _________________________ |
| 535 | * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 | |
| 536 | * XSOffset : | 4 | 6 | 7 | 8 | 10 | 11 | |
| 537 | */ |
| 538 | static const u8 frq_xs_map[] = { 4, 6, 7, 8, 10, 11 }; |
| 539 | return frq_xs_map[get_FRQ(tCK) - 3]; |
| 540 | } |
| 541 | |
| 542 | static u8 get_MOD(u32 tCK) |
| 543 | { |
| 544 | /* Get MOD based on MCU frequency using the following rule: |
| 545 | * _____________________________ |
| 546 | * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 | |
| 547 | * MOD : | 12 | 12 | 12 | 12 | 15 | 16 | |
| 548 | */ |
| 549 | static const u8 frq_mod_map[] = { 12, 12, 12, 12, 15, 16 }; |
| 550 | return frq_mod_map[get_FRQ(tCK) - 3]; |
| 551 | } |
| 552 | |
| 553 | static u8 get_WLO(u32 tCK) |
| 554 | { |
| 555 | /* Get WLO based on MCU frequency using the following rule: |
| 556 | * _______________________ |
| 557 | * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 | |
| 558 | * WLO : | 4 | 5 | 6 | 6 | 8 | 8 | |
| 559 | */ |
| 560 | static const u8 frq_wlo_map[] = { 4, 5, 6, 6, 8, 8 }; |
| 561 | return frq_wlo_map[get_FRQ(tCK) - 3]; |
| 562 | } |
| 563 | |
| 564 | static u8 get_CKE(u32 tCK) |
| 565 | { |
| 566 | /* Get CKE based on MCU frequency using the following rule: |
| 567 | * _______________________ |
| 568 | * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 | |
| 569 | * CKE : | 3 | 3 | 4 | 4 | 5 | 6 | |
| 570 | */ |
| 571 | static const u8 frq_cke_map[] = { 3, 3, 4, 4, 5, 6 }; |
| 572 | return frq_cke_map[get_FRQ(tCK) - 3]; |
| 573 | } |
| 574 | |
| 575 | static u8 get_XPDLL(u32 tCK) |
| 576 | { |
| 577 | /* Get XPDLL based on MCU frequency using the following rule: |
| 578 | * _____________________________ |
| 579 | * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 | |
| 580 | * XPDLL : | 10 | 13 | 16 | 20 | 23 | 26 | |
| 581 | */ |
| 582 | static const u8 frq_xpdll_map[] = { 10, 13, 16, 20, 23, 26 }; |
| 583 | return frq_xpdll_map[get_FRQ(tCK) - 3]; |
| 584 | } |
| 585 | |
| 586 | static u8 get_XP(u32 tCK) |
| 587 | { |
| 588 | /* Get XP based on MCU frequency using the following rule: |
| 589 | * _______________________ |
| 590 | * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 | |
| 591 | * XP : | 3 | 4 | 4 | 5 | 6 | 7 | |
| 592 | */ |
| 593 | static const u8 frq_xp_map[] = { 3, 4, 4, 5, 6, 7 }; |
| 594 | return frq_xp_map[get_FRQ(tCK) - 3]; |
| 595 | } |
| 596 | |
| 597 | static u8 get_AONPD(u32 tCK) |
| 598 | { |
| 599 | /* Get AONPD based on MCU frequency using the following rule: |
| 600 | * ________________________ |
| 601 | * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 | |
| 602 | * AONPD : | 4 | 5 | 6 | 8 | 8 | 10 | |
| 603 | */ |
| 604 | static const u8 frq_aonpd_map[] = { 4, 5, 6, 8, 8, 10 }; |
| 605 | return frq_aonpd_map[get_FRQ(tCK) - 3]; |
| 606 | } |
| 607 | |
| 608 | static u32 get_COMP2(u32 tCK) |
| 609 | { |
| 610 | /* Get COMP2 based on MCU frequency using the following rule: |
| 611 | * ___________________________________________________________ |
| 612 | * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 | |
| 613 | * COMP : | D6BEDCC | CE7C34C | CA57A4C | C6369CC | C42514C | C21410C | |
| 614 | */ |
| 615 | static const u32 frq_comp2_map[] = { 0xD6BEDCC, 0xCE7C34C, 0xCA57A4C, |
| 616 | 0xC6369CC, 0xC42514C, 0xC21410C |
| 617 | }; |
| 618 | return frq_comp2_map[get_FRQ(tCK) - 3]; |
| 619 | } |
| 620 | |
Patrick Rudolph | a1c3bed | 2016-01-24 14:07:15 +0100 | [diff] [blame] | 621 | static u32 get_XOVER_CLK(u8 rankmap) |
| 622 | { |
| 623 | return rankmap << 24; |
| 624 | } |
| 625 | |
| 626 | static u32 get_XOVER_CMD(u8 rankmap) |
| 627 | { |
| 628 | u32 reg; |
| 629 | |
| 630 | // enable xover cmd |
| 631 | reg = 0x4000; |
| 632 | |
| 633 | // enable xover ctl |
| 634 | if (rankmap & 0x3) |
| 635 | reg |= 0x20000; |
| 636 | |
| 637 | if (rankmap & 0xc) |
| 638 | reg |= 0x4000000; |
| 639 | |
| 640 | return reg; |
| 641 | } |
| 642 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 643 | static void dram_timing(ramctr_timing * ctrl) |
| 644 | { |
| 645 | u8 val; |
| 646 | u32 val32; |
| 647 | |
| 648 | /* Maximum supported DDR3 frequency is 1066MHz (DDR3 2133) so make sure |
| 649 | * we cap it if we have faster DIMMs. |
| 650 | * Then, align it to the closest JEDEC standard frequency */ |
| 651 | if (ctrl->tCK <= TCK_1066MHZ) { |
| 652 | ctrl->tCK = TCK_1066MHZ; |
| 653 | ctrl->edge_offset[0] = 16; |
| 654 | ctrl->edge_offset[1] = 7; |
| 655 | ctrl->edge_offset[2] = 7; |
| 656 | ctrl->timC_offset[0] = 18; |
| 657 | ctrl->timC_offset[1] = 7; |
| 658 | ctrl->timC_offset[2] = 7; |
| 659 | ctrl->reg_c14_offset = 16; |
| 660 | ctrl->reg_5064b0 = 0x218; |
| 661 | ctrl->reg_320c_range_threshold = 13; |
| 662 | } else if (ctrl->tCK <= TCK_933MHZ) { |
| 663 | ctrl->tCK = TCK_933MHZ; |
| 664 | ctrl->edge_offset[0] = 14; |
| 665 | ctrl->edge_offset[1] = 6; |
| 666 | ctrl->edge_offset[2] = 6; |
| 667 | ctrl->timC_offset[0] = 15; |
| 668 | ctrl->timC_offset[1] = 6; |
| 669 | ctrl->timC_offset[2] = 6; |
| 670 | ctrl->reg_c14_offset = 14; |
| 671 | ctrl->reg_5064b0 = 0x1d5; |
| 672 | ctrl->reg_320c_range_threshold = 15; |
| 673 | } else if (ctrl->tCK <= TCK_800MHZ) { |
| 674 | ctrl->tCK = TCK_800MHZ; |
| 675 | ctrl->edge_offset[0] = 13; |
| 676 | ctrl->edge_offset[1] = 5; |
| 677 | ctrl->edge_offset[2] = 5; |
| 678 | ctrl->timC_offset[0] = 14; |
| 679 | ctrl->timC_offset[1] = 5; |
| 680 | ctrl->timC_offset[2] = 5; |
| 681 | ctrl->reg_c14_offset = 12; |
| 682 | ctrl->reg_5064b0 = 0x193; |
| 683 | ctrl->reg_320c_range_threshold = 15; |
| 684 | } else if (ctrl->tCK <= TCK_666MHZ) { |
| 685 | ctrl->tCK = TCK_666MHZ; |
| 686 | ctrl->edge_offset[0] = 10; |
| 687 | ctrl->edge_offset[1] = 4; |
| 688 | ctrl->edge_offset[2] = 4; |
| 689 | ctrl->timC_offset[0] = 11; |
| 690 | ctrl->timC_offset[1] = 4; |
| 691 | ctrl->timC_offset[2] = 4; |
| 692 | ctrl->reg_c14_offset = 10; |
| 693 | ctrl->reg_5064b0 = 0x150; |
| 694 | ctrl->reg_320c_range_threshold = 16; |
| 695 | } else if (ctrl->tCK <= TCK_533MHZ) { |
| 696 | ctrl->tCK = TCK_533MHZ; |
| 697 | ctrl->edge_offset[0] = 8; |
| 698 | ctrl->edge_offset[1] = 3; |
| 699 | ctrl->edge_offset[2] = 3; |
| 700 | ctrl->timC_offset[0] = 9; |
| 701 | ctrl->timC_offset[1] = 3; |
| 702 | ctrl->timC_offset[2] = 3; |
| 703 | ctrl->reg_c14_offset = 8; |
| 704 | ctrl->reg_5064b0 = 0x10d; |
| 705 | ctrl->reg_320c_range_threshold = 17; |
| 706 | } else { |
| 707 | ctrl->tCK = TCK_400MHZ; |
| 708 | ctrl->edge_offset[0] = 6; |
| 709 | ctrl->edge_offset[1] = 2; |
| 710 | ctrl->edge_offset[2] = 2; |
| 711 | ctrl->timC_offset[0] = 6; |
| 712 | ctrl->timC_offset[1] = 2; |
| 713 | ctrl->timC_offset[2] = 2; |
| 714 | ctrl->reg_c14_offset = 8; |
| 715 | ctrl->reg_5064b0 = 0xcd; |
| 716 | ctrl->reg_320c_range_threshold = 17; |
| 717 | } |
| 718 | |
| 719 | val32 = (1000 << 8) / ctrl->tCK; |
| 720 | printk(BIOS_DEBUG, "Selected DRAM frequency: %u MHz\n", val32); |
| 721 | |
| 722 | /* Find CAS and CWL latencies */ |
| 723 | val = (ctrl->tAA + ctrl->tCK - 1) / ctrl->tCK; |
| 724 | printk(BIOS_DEBUG, "Minimum CAS latency : %uT\n", val); |
| 725 | /* Find lowest supported CAS latency that satisfies the minimum value */ |
| 726 | while (!((ctrl->cas_supported >> (val - 4)) & 1) |
| 727 | && (ctrl->cas_supported >> (val - 4))) { |
| 728 | val++; |
| 729 | } |
| 730 | /* Is CAS supported */ |
| 731 | if (!(ctrl->cas_supported & (1 << (val - 4)))) |
| 732 | printk(BIOS_DEBUG, "CAS not supported\n"); |
| 733 | printk(BIOS_DEBUG, "Selected CAS latency : %uT\n", val); |
| 734 | ctrl->CAS = val; |
| 735 | ctrl->CWL = get_CWL(ctrl->CAS); |
| 736 | printk(BIOS_DEBUG, "Selected CWL latency : %uT\n", ctrl->CWL); |
| 737 | |
| 738 | /* Find tRCD */ |
| 739 | ctrl->tRCD = (ctrl->tRCD + ctrl->tCK - 1) / ctrl->tCK; |
| 740 | printk(BIOS_DEBUG, "Selected tRCD : %uT\n", ctrl->tRCD); |
| 741 | |
| 742 | ctrl->tRP = (ctrl->tRP + ctrl->tCK - 1) / ctrl->tCK; |
| 743 | printk(BIOS_DEBUG, "Selected tRP : %uT\n", ctrl->tRP); |
| 744 | |
| 745 | /* Find tRAS */ |
| 746 | ctrl->tRAS = (ctrl->tRAS + ctrl->tCK - 1) / ctrl->tCK; |
| 747 | printk(BIOS_DEBUG, "Selected tRAS : %uT\n", ctrl->tRAS); |
| 748 | |
| 749 | /* Find tWR */ |
| 750 | ctrl->tWR = (ctrl->tWR + ctrl->tCK - 1) / ctrl->tCK; |
| 751 | printk(BIOS_DEBUG, "Selected tWR : %uT\n", ctrl->tWR); |
| 752 | |
| 753 | /* Find tFAW */ |
| 754 | ctrl->tFAW = (ctrl->tFAW + ctrl->tCK - 1) / ctrl->tCK; |
| 755 | printk(BIOS_DEBUG, "Selected tFAW : %uT\n", ctrl->tFAW); |
| 756 | |
| 757 | /* Find tRRD */ |
| 758 | ctrl->tRRD = (ctrl->tRRD + ctrl->tCK - 1) / ctrl->tCK; |
| 759 | printk(BIOS_DEBUG, "Selected tRRD : %uT\n", ctrl->tRRD); |
| 760 | |
| 761 | /* Find tRTP */ |
| 762 | ctrl->tRTP = (ctrl->tRTP + ctrl->tCK - 1) / ctrl->tCK; |
| 763 | printk(BIOS_DEBUG, "Selected tRTP : %uT\n", ctrl->tRTP); |
| 764 | |
| 765 | /* Find tWTR */ |
| 766 | ctrl->tWTR = (ctrl->tWTR + ctrl->tCK - 1) / ctrl->tCK; |
| 767 | printk(BIOS_DEBUG, "Selected tWTR : %uT\n", ctrl->tWTR); |
| 768 | |
| 769 | /* Refresh-to-Active or Refresh-to-Refresh (tRFC) */ |
| 770 | ctrl->tRFC = (ctrl->tRFC + ctrl->tCK - 1) / ctrl->tCK; |
| 771 | printk(BIOS_DEBUG, "Selected tRFC : %uT\n", ctrl->tRFC); |
| 772 | |
| 773 | ctrl->tREFI = get_REFI(ctrl->tCK); |
| 774 | ctrl->tMOD = get_MOD(ctrl->tCK); |
| 775 | ctrl->tXSOffset = get_XSOffset(ctrl->tCK); |
| 776 | ctrl->tWLO = get_WLO(ctrl->tCK); |
| 777 | ctrl->tCKE = get_CKE(ctrl->tCK); |
| 778 | ctrl->tXPDLL = get_XPDLL(ctrl->tCK); |
| 779 | ctrl->tXP = get_XP(ctrl->tCK); |
| 780 | ctrl->tAONPD = get_AONPD(ctrl->tCK); |
| 781 | } |
| 782 | |
| 783 | static void dram_freq(ramctr_timing * ctrl) |
| 784 | { |
| 785 | if (ctrl->tCK > TCK_400MHZ) { |
| 786 | printk (BIOS_ERR, "DRAM frequency is under lowest supported frequency (400 MHz). Increasing to 400 MHz as last resort"); |
| 787 | ctrl->tCK = TCK_400MHZ; |
| 788 | } |
| 789 | while (1) { |
| 790 | u8 val2; |
| 791 | u32 reg1 = 0; |
| 792 | |
| 793 | /* Step 1 - Set target PCU frequency */ |
| 794 | |
| 795 | if (ctrl->tCK <= TCK_1066MHZ) { |
| 796 | ctrl->tCK = TCK_1066MHZ; |
| 797 | } else if (ctrl->tCK <= TCK_933MHZ) { |
| 798 | ctrl->tCK = TCK_933MHZ; |
| 799 | } else if (ctrl->tCK <= TCK_800MHZ) { |
| 800 | ctrl->tCK = TCK_800MHZ; |
| 801 | } else if (ctrl->tCK <= TCK_666MHZ) { |
| 802 | ctrl->tCK = TCK_666MHZ; |
| 803 | } else if (ctrl->tCK <= TCK_533MHZ) { |
| 804 | ctrl->tCK = TCK_533MHZ; |
| 805 | } else if (ctrl->tCK <= TCK_400MHZ) { |
| 806 | ctrl->tCK = TCK_400MHZ; |
| 807 | } else { |
| 808 | die ("No lock frequency found"); |
| 809 | } |
| 810 | |
| 811 | /* Frequency mulitplier. */ |
| 812 | u32 FRQ = get_FRQ(ctrl->tCK); |
| 813 | |
Patrick Rudolph | 9c9bde3 | 2016-03-26 17:20:02 +0100 | [diff] [blame] | 814 | /* The PLL will never lock if the required frequency is |
| 815 | * already set. Exit early to prevent a system hang. |
| 816 | */ |
| 817 | reg1 = MCHBAR32(0x5e04); |
| 818 | val2 = (u8) reg1; |
| 819 | if (val2 == FRQ) { |
| 820 | printk(BIOS_DEBUG, "MCU frequency is set at : %d MHz\n", |
| 821 | (1000 << 8) / ctrl->tCK); |
| 822 | return; |
| 823 | } |
| 824 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 825 | /* Step 2 - Select frequency in the MCU */ |
| 826 | reg1 = FRQ; |
| 827 | reg1 |= 0x80000000; // set running bit |
| 828 | MCHBAR32(0x5e00) = reg1; |
| 829 | while (reg1 & 0x80000000) { |
| 830 | printk(BIOS_DEBUG, " PLL busy..."); |
| 831 | reg1 = MCHBAR32(0x5e00); |
| 832 | } |
| 833 | printk(BIOS_DEBUG, "done\n"); |
| 834 | |
| 835 | /* Step 3 - Verify lock frequency */ |
| 836 | reg1 = MCHBAR32(0x5e04); |
| 837 | val2 = (u8) reg1; |
| 838 | if (val2 >= FRQ) { |
| 839 | printk(BIOS_DEBUG, "MCU frequency is set at : %d MHz\n", |
| 840 | (1000 << 8) / ctrl->tCK); |
| 841 | return; |
| 842 | } |
| 843 | printk(BIOS_DEBUG, "PLL didn't lock. Retrying at lower frequency\n"); |
| 844 | ctrl->tCK++; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | static void dram_xover(ramctr_timing * ctrl) |
| 849 | { |
| 850 | u32 reg; |
| 851 | int channel; |
| 852 | |
| 853 | FOR_ALL_CHANNELS { |
| 854 | // enable xover clk |
Patrick Rudolph | a1c3bed | 2016-01-24 14:07:15 +0100 | [diff] [blame] | 855 | reg = get_XOVER_CLK(ctrl->rankmap[channel]); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 856 | printram("XOVER CLK [%x] = %x\n", channel * 0x100 + 0xc14, |
| 857 | reg); |
Patrick Rudolph | a1c3bed | 2016-01-24 14:07:15 +0100 | [diff] [blame] | 858 | MCHBAR32(channel * 0x100 + 0xc14) = reg; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 859 | |
Patrick Rudolph | a1c3bed | 2016-01-24 14:07:15 +0100 | [diff] [blame] | 860 | // enable xover ctl & xover cmd |
| 861 | reg = get_XOVER_CMD(ctrl->rankmap[channel]); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 862 | printram("XOVER CMD [%x] = %x\n", 0x100 * channel + 0x320c, |
| 863 | reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 864 | MCHBAR32(0x100 * channel + 0x320c) = reg; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | static void dram_timing_regs(ramctr_timing * ctrl) |
| 869 | { |
| 870 | u32 reg, addr, val32, cpu, stretch; |
| 871 | struct cpuid_result cpures; |
| 872 | int channel; |
| 873 | |
| 874 | FOR_ALL_CHANNELS { |
| 875 | // DBP |
| 876 | reg = 0; |
| 877 | reg |= ctrl->tRCD; |
| 878 | reg |= (ctrl->tRP << 4); |
| 879 | reg |= (ctrl->CAS << 8); |
| 880 | reg |= (ctrl->CWL << 12); |
| 881 | reg |= (ctrl->tRAS << 16); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 882 | printram("DBP [%x] = %x\n", 0x400 * channel + 0x4000, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 883 | MCHBAR32(0x400 * channel + 0x4000) = reg; |
| 884 | |
| 885 | // RAP |
| 886 | reg = 0; |
| 887 | reg |= ctrl->tRRD; |
| 888 | reg |= (ctrl->tRTP << 4); |
| 889 | reg |= (ctrl->tCKE << 8); |
| 890 | reg |= (ctrl->tWTR << 12); |
| 891 | reg |= (ctrl->tFAW << 16); |
| 892 | reg |= (ctrl->tWR << 24); |
| 893 | reg |= (3 << 30); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 894 | printram("RAP [%x] = %x\n", 0x400 * channel + 0x4004, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 895 | MCHBAR32(0x400 * channel + 0x4004) = reg; |
| 896 | |
| 897 | // OTHP |
| 898 | addr = 0x400 * channel + 0x400c; |
| 899 | reg = 0; |
| 900 | reg |= ctrl->tXPDLL; |
| 901 | reg |= (ctrl->tXP << 5); |
| 902 | reg |= (ctrl->tAONPD << 8); |
| 903 | reg |= 0xa0000; |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 904 | printram("OTHP [%x] = %x\n", addr, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 905 | MCHBAR32(addr) = reg; |
| 906 | |
| 907 | MCHBAR32(0x400 * channel + 0x4014) = 0; |
| 908 | |
| 909 | MCHBAR32(addr) |= 0x00020000; |
| 910 | |
| 911 | // ODT stretch |
| 912 | reg = 0; |
| 913 | |
| 914 | cpures = cpuid(0); |
| 915 | cpu = cpures.eax; |
| 916 | if (IS_IVY_CPU(cpu) |
| 917 | || (IS_SANDY_CPU(cpu) && IS_SANDY_CPU_D2(cpu))) { |
| 918 | stretch = 2; |
| 919 | addr = 0x400 * channel + 0x400c; |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 920 | printram("ODT stretch [%x] = %x\n", |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 921 | 0x400 * channel + 0x400c, reg); |
| 922 | reg = MCHBAR32(addr); |
| 923 | |
| 924 | if (((ctrl->rankmap[channel] & 3) == 0) |
| 925 | || (ctrl->rankmap[channel] & 0xc) == 0) { |
| 926 | |
| 927 | // Rank 0 - operate on rank 2 |
| 928 | reg = (reg & ~0xc0000) | (stretch << 18); |
| 929 | |
| 930 | // Rank 2 - operate on rank 0 |
| 931 | reg = (reg & ~0x30000) | (stretch << 16); |
| 932 | |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 933 | printram("ODT stretch [%x] = %x\n", addr, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 934 | MCHBAR32(addr) = reg; |
| 935 | } |
| 936 | |
| 937 | } else if (IS_SANDY_CPU(cpu) && IS_SANDY_CPU_C(cpu)) { |
| 938 | stretch = 3; |
| 939 | addr = 0x400 * channel + 0x401c; |
| 940 | reg = MCHBAR32(addr); |
| 941 | |
| 942 | if (((ctrl->rankmap[channel] & 3) == 0) |
| 943 | || (ctrl->rankmap[channel] & 0xc) == 0) { |
| 944 | |
| 945 | // Rank 0 - operate on rank 2 |
| 946 | reg = (reg & ~0x3000) | (stretch << 12); |
| 947 | |
| 948 | // Rank 2 - operate on rank 0 |
| 949 | reg = (reg & ~0xc00) | (stretch << 10); |
| 950 | |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 951 | printram("ODT stretch [%x] = %x\n", addr, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 952 | MCHBAR32(addr) = reg; |
| 953 | } |
| 954 | } else { |
| 955 | stretch = 0; |
| 956 | } |
| 957 | |
| 958 | // REFI |
| 959 | reg = 0; |
| 960 | val32 = ctrl->tREFI; |
| 961 | reg = (reg & ~0xffff) | val32; |
| 962 | val32 = ctrl->tRFC; |
| 963 | reg = (reg & ~0x1ff0000) | (val32 << 16); |
| 964 | val32 = (u32) (ctrl->tREFI * 9) / 1024; |
| 965 | reg = (reg & ~0xfe000000) | (val32 << 25); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 966 | printram("REFI [%x] = %x\n", 0x400 * channel + 0x4298, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 967 | reg); |
| 968 | MCHBAR32(0x400 * channel + 0x4298) = reg; |
| 969 | |
| 970 | MCHBAR32(0x400 * channel + 0x4294) |= 0xff; |
| 971 | |
| 972 | // SRFTP |
| 973 | reg = 0; |
| 974 | val32 = tDLLK; |
| 975 | reg = (reg & ~0xfff) | val32; |
| 976 | val32 = ctrl->tXSOffset; |
| 977 | reg = (reg & ~0xf000) | (val32 << 12); |
| 978 | val32 = tDLLK - ctrl->tXSOffset; |
| 979 | reg = (reg & ~0x3ff0000) | (val32 << 16); |
| 980 | val32 = ctrl->tMOD - 8; |
| 981 | reg = (reg & ~0xf0000000) | (val32 << 28); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 982 | printram("SRFTP [%x] = %x\n", 0x400 * channel + 0x42a4, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 983 | reg); |
| 984 | MCHBAR32(0x400 * channel + 0x42a4) = reg; |
| 985 | } |
| 986 | } |
| 987 | |
Patrick Rudolph | 735ecce | 2016-03-26 10:42:27 +0100 | [diff] [blame] | 988 | static void dram_dimm_mapping(ramctr_timing *ctrl) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 989 | { |
| 990 | u32 reg, val32; |
| 991 | int channel; |
Patrick Rudolph | 735ecce | 2016-03-26 10:42:27 +0100 | [diff] [blame] | 992 | dimm_info *info = &ctrl->info; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 993 | |
| 994 | FOR_ALL_CHANNELS { |
| 995 | dimm_attr *dimmA = 0; |
| 996 | dimm_attr *dimmB = 0; |
| 997 | reg = 0; |
| 998 | val32 = 0; |
| 999 | if (info->dimm[channel][0].size_mb >= |
| 1000 | info->dimm[channel][1].size_mb) { |
| 1001 | // dimm 0 is bigger, set it to dimmA |
| 1002 | dimmA = &info->dimm[channel][0]; |
| 1003 | dimmB = &info->dimm[channel][1]; |
| 1004 | reg |= (0 << 16); |
| 1005 | } else { |
| 1006 | // dimm 1 is bigger, set it to dimmA |
| 1007 | dimmA = &info->dimm[channel][1]; |
| 1008 | dimmB = &info->dimm[channel][0]; |
| 1009 | reg |= (1 << 16); |
| 1010 | } |
| 1011 | // dimmA |
| 1012 | if (dimmA && (dimmA->ranks > 0)) { |
| 1013 | val32 = dimmA->size_mb / 256; |
| 1014 | reg = (reg & ~0xff) | val32; |
| 1015 | val32 = dimmA->ranks - 1; |
| 1016 | reg = (reg & ~0x20000) | (val32 << 17); |
| 1017 | val32 = (dimmA->width / 8) - 1; |
| 1018 | reg = (reg & ~0x80000) | (val32 << 19); |
| 1019 | } |
| 1020 | // dimmB |
| 1021 | if (dimmB && (dimmB->ranks > 0)) { |
| 1022 | val32 = dimmB->size_mb / 256; |
| 1023 | reg = (reg & ~0xff00) | (val32 << 8); |
| 1024 | val32 = dimmB->ranks - 1; |
| 1025 | reg = (reg & ~0x40000) | (val32 << 18); |
| 1026 | val32 = (dimmB->width / 8) - 1; |
| 1027 | reg = (reg & ~0x100000) | (val32 << 20); |
| 1028 | } |
| 1029 | reg = (reg & ~0x200000) | (1 << 21); // rank interleave |
| 1030 | reg = (reg & ~0x400000) | (1 << 22); // enhanced interleave |
| 1031 | |
| 1032 | // Save MAD-DIMM register |
| 1033 | if ((dimmA && (dimmA->ranks > 0)) |
| 1034 | || (dimmB && (dimmB->ranks > 0))) { |
| 1035 | ctrl->mad_dimm[channel] = reg; |
| 1036 | } else { |
| 1037 | ctrl->mad_dimm[channel] = 0; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | static void dram_dimm_set_mapping(ramctr_timing * ctrl) |
| 1043 | { |
| 1044 | int channel; |
| 1045 | FOR_ALL_CHANNELS { |
| 1046 | MCHBAR32(0x5004 + channel * 4) = ctrl->mad_dimm[channel]; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | static void dram_zones(ramctr_timing * ctrl, int training) |
| 1051 | { |
| 1052 | u32 reg, ch0size, ch1size; |
| 1053 | u8 val; |
| 1054 | reg = 0; |
| 1055 | val = 0; |
| 1056 | if (training) { |
| 1057 | ch0size = ctrl->channel_size_mb[0] ? 256 : 0; |
| 1058 | ch1size = ctrl->channel_size_mb[1] ? 256 : 0; |
| 1059 | } else { |
| 1060 | ch0size = ctrl->channel_size_mb[0]; |
| 1061 | ch1size = ctrl->channel_size_mb[1]; |
| 1062 | } |
| 1063 | |
| 1064 | if (ch0size >= ch1size) { |
| 1065 | reg = MCHBAR32(0x5014); |
| 1066 | val = ch1size / 256; |
| 1067 | reg = (reg & ~0xff000000) | val << 24; |
| 1068 | reg = (reg & ~0xff0000) | (2 * val) << 16; |
| 1069 | MCHBAR32(0x5014) = reg; |
| 1070 | MCHBAR32(0x5000) = 0x24; |
| 1071 | } else { |
| 1072 | reg = MCHBAR32(0x5014); |
| 1073 | val = ch0size / 256; |
| 1074 | reg = (reg & ~0xff000000) | val << 24; |
| 1075 | reg = (reg & ~0xff0000) | (2 * val) << 16; |
| 1076 | MCHBAR32(0x5014) = reg; |
| 1077 | MCHBAR32(0x5000) = 0x21; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | static void dram_memorymap(ramctr_timing * ctrl, int me_uma_size) |
| 1082 | { |
| 1083 | u32 reg, val, reclaim; |
| 1084 | u32 tom, gfxstolen, gttsize; |
| 1085 | size_t tsegsize, mmiosize, toludbase, touudbase, gfxstolenbase, gttbase, |
| 1086 | tsegbase, mestolenbase; |
| 1087 | size_t tsegbasedelta, remapbase, remaplimit; |
| 1088 | uint16_t ggc; |
| 1089 | |
Patrick Rudolph | 266a1f7 | 2016-06-09 18:13:34 +0200 | [diff] [blame] | 1090 | mmiosize = get_mmio_size(); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1091 | |
| 1092 | ggc = pci_read_config16(NORTHBRIDGE, GGC); |
| 1093 | if (!(ggc & 2)) { |
| 1094 | gfxstolen = ((ggc >> 3) & 0x1f) * 32; |
| 1095 | gttsize = ((ggc >> 8) & 0x3); |
| 1096 | } else { |
| 1097 | gfxstolen = 0; |
| 1098 | gttsize = 0; |
| 1099 | } |
| 1100 | |
| 1101 | tsegsize = CONFIG_SMM_TSEG_SIZE >> 20; |
| 1102 | |
| 1103 | tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1]; |
| 1104 | |
| 1105 | mestolenbase = tom - me_uma_size; |
| 1106 | |
| 1107 | toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, |
| 1108 | tom - me_uma_size); |
| 1109 | gfxstolenbase = toludbase - gfxstolen; |
| 1110 | gttbase = gfxstolenbase - gttsize; |
| 1111 | |
| 1112 | tsegbase = gttbase - tsegsize; |
| 1113 | |
| 1114 | // Round tsegbase down to nearest address aligned to tsegsize |
| 1115 | tsegbasedelta = tsegbase & (tsegsize - 1); |
| 1116 | tsegbase &= ~(tsegsize - 1); |
| 1117 | |
| 1118 | gttbase -= tsegbasedelta; |
| 1119 | gfxstolenbase -= tsegbasedelta; |
| 1120 | toludbase -= tsegbasedelta; |
| 1121 | |
| 1122 | // Test if it is possible to reclaim a hole in the ram addressing |
| 1123 | if (tom - me_uma_size > toludbase) { |
| 1124 | // Reclaim is possible |
| 1125 | reclaim = 1; |
| 1126 | remapbase = MAX(4096, tom - me_uma_size); |
| 1127 | remaplimit = |
| 1128 | remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1; |
| 1129 | touudbase = remaplimit + 1; |
| 1130 | } else { |
| 1131 | // Reclaim not possible |
| 1132 | reclaim = 0; |
| 1133 | touudbase = tom - me_uma_size; |
| 1134 | } |
| 1135 | |
| 1136 | // Update memory map in pci-e configuration space |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1137 | printk(BIOS_DEBUG, "Update PCI-E configuration space:\n"); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1138 | |
| 1139 | // TOM (top of memory) |
| 1140 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa0); |
| 1141 | val = tom & 0xfff; |
| 1142 | reg = (reg & ~0xfff00000) | (val << 20); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1143 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa0, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1144 | pcie_write_config32(PCI_DEV(0, 0, 0), 0xa0, reg); |
| 1145 | |
| 1146 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa4); |
| 1147 | val = tom & 0xfffff000; |
| 1148 | reg = (reg & ~0x000fffff) | (val >> 12); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1149 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa4, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1150 | pcie_write_config32(PCI_DEV(0, 0, 0), 0xa4, reg); |
| 1151 | |
| 1152 | // TOLUD (top of low used dram) |
| 1153 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xbc); |
| 1154 | val = toludbase & 0xfff; |
| 1155 | reg = (reg & ~0xfff00000) | (val << 20); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1156 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xbc, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1157 | pcie_write_config32(PCI_DEV(0, 0, 0), 0xbc, reg); |
| 1158 | |
| 1159 | // TOUUD LSB (top of upper usable dram) |
| 1160 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa8); |
| 1161 | val = touudbase & 0xfff; |
| 1162 | reg = (reg & ~0xfff00000) | (val << 20); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1163 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa8, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1164 | pcie_write_config32(PCI_DEV(0, 0, 0), 0xa8, reg); |
| 1165 | |
| 1166 | // TOUUD MSB |
| 1167 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xac); |
| 1168 | val = touudbase & 0xfffff000; |
| 1169 | reg = (reg & ~0x000fffff) | (val >> 12); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1170 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xac, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1171 | pcie_write_config32(PCI_DEV(0, 0, 0), 0xac, reg); |
| 1172 | |
| 1173 | if (reclaim) { |
| 1174 | // REMAP BASE |
| 1175 | pcie_write_config32(PCI_DEV(0, 0, 0), 0x90, remapbase << 20); |
| 1176 | pcie_write_config32(PCI_DEV(0, 0, 0), 0x94, remapbase >> 12); |
| 1177 | |
| 1178 | // REMAP LIMIT |
| 1179 | pcie_write_config32(PCI_DEV(0, 0, 0), 0x98, remaplimit << 20); |
| 1180 | pcie_write_config32(PCI_DEV(0, 0, 0), 0x9c, remaplimit >> 12); |
| 1181 | } |
| 1182 | // TSEG |
| 1183 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb8); |
| 1184 | val = tsegbase & 0xfff; |
| 1185 | reg = (reg & ~0xfff00000) | (val << 20); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1186 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb8, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1187 | pcie_write_config32(PCI_DEV(0, 0, 0), 0xb8, reg); |
| 1188 | |
| 1189 | // GFX stolen memory |
| 1190 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb0); |
| 1191 | val = gfxstolenbase & 0xfff; |
| 1192 | reg = (reg & ~0xfff00000) | (val << 20); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1193 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb0, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1194 | pcie_write_config32(PCI_DEV(0, 0, 0), 0xb0, reg); |
| 1195 | |
| 1196 | // GTT stolen memory |
| 1197 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb4); |
| 1198 | val = gttbase & 0xfff; |
| 1199 | reg = (reg & ~0xfff00000) | (val << 20); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1200 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb4, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1201 | pcie_write_config32(PCI_DEV(0, 0, 0), 0xb4, reg); |
| 1202 | |
| 1203 | if (me_uma_size) { |
| 1204 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x7c); |
| 1205 | val = (0x80000 - me_uma_size) & 0xfffff000; |
| 1206 | reg = (reg & ~0x000fffff) | (val >> 12); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1207 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x7c, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1208 | pcie_write_config32(PCI_DEV(0, 0, 0), 0x7c, reg); |
| 1209 | |
| 1210 | // ME base |
| 1211 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x70); |
| 1212 | val = mestolenbase & 0xfff; |
| 1213 | reg = (reg & ~0xfff00000) | (val << 20); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1214 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x70, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1215 | pcie_write_config32(PCI_DEV(0, 0, 0), 0x70, reg); |
| 1216 | |
| 1217 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x74); |
| 1218 | val = mestolenbase & 0xfffff000; |
| 1219 | reg = (reg & ~0x000fffff) | (val >> 12); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1220 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x74, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1221 | pcie_write_config32(PCI_DEV(0, 0, 0), 0x74, reg); |
| 1222 | |
| 1223 | // ME mask |
| 1224 | reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x78); |
| 1225 | val = (0x80000 - me_uma_size) & 0xfff; |
| 1226 | reg = (reg & ~0xfff00000) | (val << 20); |
| 1227 | reg = (reg & ~0x400) | (1 << 10); // set lockbit on ME mem |
| 1228 | |
| 1229 | reg = (reg & ~0x800) | (1 << 11); // set ME memory enable |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1230 | printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x78, reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1231 | pcie_write_config32(PCI_DEV(0, 0, 0), 0x78, reg); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | static void dram_ioregs(ramctr_timing * ctrl) |
| 1236 | { |
| 1237 | u32 reg, comp2; |
| 1238 | |
| 1239 | int channel; |
| 1240 | |
| 1241 | // IO clock |
| 1242 | FOR_ALL_CHANNELS { |
| 1243 | MCHBAR32(0xc00 + 0x100 * channel) = ctrl->rankmap[channel]; |
| 1244 | } |
| 1245 | |
| 1246 | // IO command |
| 1247 | FOR_ALL_CHANNELS { |
| 1248 | MCHBAR32(0x3200 + 0x100 * channel) = ctrl->rankmap[channel]; |
| 1249 | } |
| 1250 | |
| 1251 | // IO control |
| 1252 | FOR_ALL_POPULATED_CHANNELS { |
| 1253 | program_timings(ctrl, channel); |
| 1254 | } |
| 1255 | |
| 1256 | // Rcomp |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1257 | printram("RCOMP..."); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1258 | reg = 0; |
| 1259 | while (reg == 0) { |
| 1260 | reg = MCHBAR32(0x5084) & 0x10000; |
| 1261 | } |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1262 | printram("done\n"); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1263 | |
| 1264 | // Set comp2 |
| 1265 | comp2 = get_COMP2(ctrl->tCK); |
| 1266 | MCHBAR32(0x3714) = comp2; |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1267 | printram("COMP2 done\n"); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1268 | |
| 1269 | // Set comp1 |
| 1270 | FOR_ALL_POPULATED_CHANNELS { |
| 1271 | reg = MCHBAR32(0x1810 + channel * 0x100); //ch0 |
| 1272 | reg = (reg & ~0xe00) | (1 << 9); //odt |
| 1273 | reg = (reg & ~0xe00000) | (1 << 21); //clk drive up |
| 1274 | reg = (reg & ~0x38000000) | (1 << 27); //ctl drive up |
| 1275 | MCHBAR32(0x1810 + channel * 0x100) = reg; |
| 1276 | } |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1277 | printram("COMP1 done\n"); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1278 | |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1279 | printram("FORCE RCOMP and wait 20us..."); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1280 | MCHBAR32(0x5f08) |= 0x100; |
| 1281 | udelay(20); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1282 | printram("done\n"); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | static void wait_428c(int channel) |
| 1286 | { |
| 1287 | while (1) { |
| 1288 | if (read32(DEFAULT_MCHBAR + 0x428c + (channel << 10)) & 0x50) |
| 1289 | return; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | static void write_reset(ramctr_timing * ctrl) |
| 1294 | { |
| 1295 | int channel, slotrank; |
| 1296 | |
| 1297 | /* choose a populated channel. */ |
| 1298 | channel = (ctrl->rankmap[0]) ? 0 : 1; |
| 1299 | |
| 1300 | wait_428c(channel); |
| 1301 | |
| 1302 | /* choose a populated rank. */ |
| 1303 | slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2; |
| 1304 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1305 | /* DRAM command ZQCS */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1306 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003); |
| 1307 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x80c01); |
| 1308 | |
| 1309 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 1310 | (slotrank << 24) | 0x60000); |
| 1311 | |
| 1312 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0); |
| 1313 | |
| 1314 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x400001); |
| 1315 | wait_428c(channel); |
| 1316 | } |
| 1317 | |
| 1318 | static void dram_jedecreset(ramctr_timing * ctrl) |
| 1319 | { |
| 1320 | u32 reg, addr; |
| 1321 | int channel; |
| 1322 | |
| 1323 | while (!(MCHBAR32(0x5084) & 0x10000)) ; |
| 1324 | do { |
| 1325 | reg = MCHBAR32(0x428c); |
| 1326 | } while ((reg & 0x14) == 0); |
| 1327 | |
| 1328 | // Set state of memory controller |
| 1329 | reg = 0x112; |
| 1330 | MCHBAR32(0x5030) = reg; |
| 1331 | MCHBAR32(0x4ea0) = 0; |
| 1332 | reg |= 2; //ddr reset |
| 1333 | MCHBAR32(0x5030) = reg; |
| 1334 | |
| 1335 | // Assert dimm reset signal |
| 1336 | reg = MCHBAR32(0x5030); |
| 1337 | reg &= ~0x2; |
| 1338 | MCHBAR32(0x5030) = reg; |
| 1339 | |
| 1340 | // Wait 200us |
| 1341 | udelay(200); |
| 1342 | |
| 1343 | // Deassert dimm reset signal |
| 1344 | MCHBAR32(0x5030) |= 2; |
| 1345 | |
| 1346 | // Wait 500us |
| 1347 | udelay(500); |
| 1348 | |
| 1349 | // Enable DCLK |
| 1350 | MCHBAR32(0x5030) |= 4; |
| 1351 | |
| 1352 | // XXX Wait 20ns |
| 1353 | udelay(1); |
| 1354 | |
| 1355 | FOR_ALL_CHANNELS { |
| 1356 | // Set valid rank CKE |
| 1357 | reg = 0; |
| 1358 | reg = (reg & ~0xf) | ctrl->rankmap[channel]; |
| 1359 | addr = 0x400 * channel + 0x42a0; |
| 1360 | MCHBAR32(addr) = reg; |
| 1361 | |
| 1362 | // Wait 10ns for ranks to settle |
| 1363 | //udelay(0.01); |
| 1364 | |
| 1365 | reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4); |
| 1366 | MCHBAR32(addr) = reg; |
| 1367 | |
| 1368 | // Write reset using a NOP |
| 1369 | write_reset(ctrl); |
| 1370 | } |
| 1371 | } |
| 1372 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1373 | static odtmap get_ODT(ramctr_timing *ctrl, u8 rank, int channel) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1374 | { |
| 1375 | /* Get ODT based on rankmap: */ |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1376 | int dimms_per_ch = (ctrl->rankmap[channel] & 1) |
| 1377 | + ((ctrl->rankmap[channel] >> 2) & 1); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1378 | |
| 1379 | if (dimms_per_ch == 1) { |
| 1380 | return (const odtmap){60, 60}; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1381 | } else { |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1382 | return (const odtmap){120, 30}; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1383 | } |
| 1384 | } |
| 1385 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1386 | static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1387 | int reg, u32 val) |
| 1388 | { |
| 1389 | wait_428c(channel); |
| 1390 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1391 | if (ctrl->rank_mirror[channel][slotrank]) { |
| 1392 | /* DDR3 Rank1 Address mirror |
| 1393 | * swap the following pins: |
| 1394 | * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1 */ |
| 1395 | reg = ((reg >> 1) & 1) | ((reg << 1) & 2); |
| 1396 | val = (val & ~0x1f8) | ((val >> 1) & 0xa8) |
| 1397 | | ((val & 0xa8) << 1); |
| 1398 | } |
| 1399 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1400 | /* DRAM command MRS */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1401 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f000); |
| 1402 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001); |
| 1403 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 1404 | (slotrank << 24) | (reg << 20) | val | 0x60000); |
| 1405 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0); |
| 1406 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1407 | /* DRAM command MRS */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1408 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f000); |
| 1409 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x41001); |
| 1410 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 1411 | (slotrank << 24) | (reg << 20) | val | 0x60000); |
| 1412 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0); |
| 1413 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1414 | /* DRAM command MRS */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1415 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x0f000); |
| 1416 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, |
| 1417 | 0x1001 | (ctrl->tMOD << 16)); |
| 1418 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, |
| 1419 | (slotrank << 24) | (reg << 20) | val | 0x60000); |
| 1420 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0); |
| 1421 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001); |
| 1422 | } |
| 1423 | |
| 1424 | static u32 make_mr0(ramctr_timing * ctrl, u8 rank) |
| 1425 | { |
| 1426 | u16 mr0reg, mch_cas, mch_wr; |
| 1427 | static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 }; |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1428 | |
| 1429 | /* DLL Reset - self clearing - set after CLK frequency has been changed */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1430 | mr0reg = 0x100; |
| 1431 | |
| 1432 | // Convert CAS to MCH register friendly |
| 1433 | if (ctrl->CAS < 12) { |
| 1434 | mch_cas = (u16) ((ctrl->CAS - 4) << 1); |
| 1435 | } else { |
| 1436 | mch_cas = (u16) (ctrl->CAS - 12); |
| 1437 | mch_cas = ((mch_cas << 1) | 0x1); |
| 1438 | } |
| 1439 | |
| 1440 | // Convert tWR to MCH register friendly |
| 1441 | mch_wr = mch_wr_t[ctrl->tWR - 5]; |
| 1442 | |
| 1443 | mr0reg = (mr0reg & ~0x4) | (mch_cas & 0x1); |
| 1444 | mr0reg = (mr0reg & ~0x70) | ((mch_cas & 0xe) << 3); |
| 1445 | mr0reg = (mr0reg & ~0xe00) | (mch_wr << 9); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1446 | |
| 1447 | // Precharge PD - Fast (desktop) 0x1 or slow (mobile) 0x0 - mostly power-saving feature |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1448 | mr0reg = (mr0reg & ~0x1000) | (!ctrl->mobile << 12); |
| 1449 | return mr0reg; |
| 1450 | } |
| 1451 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1452 | static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1453 | { |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1454 | write_mrreg(ctrl, channel, rank, 0, |
| 1455 | make_mr0(ctrl, rank)); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | static u32 encode_odt(u32 odt) |
| 1459 | { |
| 1460 | switch (odt) { |
| 1461 | case 30: |
| 1462 | return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4 |
| 1463 | case 60: |
| 1464 | return (1 << 2); // RZQ/4 |
| 1465 | case 120: |
| 1466 | return (1 << 6); // RZQ/2 |
| 1467 | default: |
| 1468 | case 0: |
| 1469 | return 0; |
| 1470 | } |
| 1471 | } |
| 1472 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1473 | static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1474 | { |
| 1475 | odtmap odt; |
| 1476 | u32 mr1reg; |
| 1477 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1478 | odt = get_ODT(ctrl, rank, channel); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1479 | mr1reg = 0x2; |
| 1480 | |
| 1481 | mr1reg |= encode_odt(odt.rttnom); |
| 1482 | |
| 1483 | return mr1reg; |
| 1484 | } |
| 1485 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1486 | static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1487 | { |
| 1488 | u16 mr1reg; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1489 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1490 | mr1reg = make_mr1(ctrl, rank, channel); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1491 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1492 | write_mrreg(ctrl, channel, rank, 1, mr1reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1495 | static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1496 | { |
| 1497 | u16 pasr, cwl, mr2reg; |
| 1498 | odtmap odt; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1499 | int srt; |
| 1500 | |
| 1501 | pasr = 0; |
| 1502 | cwl = ctrl->CWL - 5; |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1503 | odt = get_ODT(ctrl, rank, channel); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1504 | |
| 1505 | srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh; |
| 1506 | |
| 1507 | mr2reg = 0; |
| 1508 | mr2reg = (mr2reg & ~0x7) | pasr; |
| 1509 | mr2reg = (mr2reg & ~0x38) | (cwl << 3); |
| 1510 | mr2reg = (mr2reg & ~0x40) | (ctrl->auto_self_refresh << 6); |
| 1511 | mr2reg = (mr2reg & ~0x80) | (srt << 7); |
| 1512 | mr2reg |= (odt.rttwr / 60) << 9; |
| 1513 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1514 | write_mrreg(ctrl, channel, rank, 2, mr2reg); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1517 | static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1518 | { |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1519 | write_mrreg(ctrl, channel, rank, 3, 0); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | static void dram_mrscommands(ramctr_timing * ctrl) |
| 1523 | { |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1524 | u8 slotrank; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1525 | u32 reg, addr; |
| 1526 | int channel; |
| 1527 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1528 | FOR_ALL_POPULATED_CHANNELS { |
| 1529 | FOR_ALL_POPULATED_RANKS { |
| 1530 | // MR2 |
| 1531 | dram_mr2(ctrl, slotrank, channel); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1532 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1533 | // MR3 |
| 1534 | dram_mr3(ctrl, slotrank, channel); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1535 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1536 | // MR1 |
| 1537 | dram_mr1(ctrl, slotrank, channel); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1538 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1539 | // MR0 |
| 1540 | dram_mr0(ctrl, slotrank, channel); |
| 1541 | } |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1544 | /* DRAM command NOP */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1545 | write32(DEFAULT_MCHBAR + 0x4e20, 0x7); |
| 1546 | write32(DEFAULT_MCHBAR + 0x4e30, 0xf1001); |
| 1547 | write32(DEFAULT_MCHBAR + 0x4e00, 0x60002); |
| 1548 | write32(DEFAULT_MCHBAR + 0x4e10, 0); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1549 | |
| 1550 | /* DRAM command ZQCL */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1551 | write32(DEFAULT_MCHBAR + 0x4e24, 0x1f003); |
| 1552 | write32(DEFAULT_MCHBAR + 0x4e34, 0x1901001); |
| 1553 | write32(DEFAULT_MCHBAR + 0x4e04, 0x60400); |
| 1554 | write32(DEFAULT_MCHBAR + 0x4e14, 0x288); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1555 | |
| 1556 | /* execute command queue on all channels ? */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1557 | write32(DEFAULT_MCHBAR + 0x4e84, 0x40004); |
| 1558 | |
| 1559 | // Drain |
| 1560 | FOR_ALL_CHANNELS { |
| 1561 | // Wait for ref drained |
| 1562 | wait_428c(channel); |
| 1563 | } |
| 1564 | |
| 1565 | // Refresh enable |
| 1566 | MCHBAR32(0x5030) |= 8; |
| 1567 | |
| 1568 | FOR_ALL_POPULATED_CHANNELS { |
| 1569 | addr = 0x400 * channel + 0x4020; |
| 1570 | reg = MCHBAR32(addr); |
| 1571 | reg &= ~0x200000; |
| 1572 | MCHBAR32(addr) = reg; |
| 1573 | |
| 1574 | wait_428c(channel); |
| 1575 | |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1576 | slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1577 | |
| 1578 | // Drain |
| 1579 | wait_428c(channel); |
| 1580 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1581 | /* DRAM command ZQCS */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1582 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003); |
| 1583 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001); |
| 1584 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 1585 | (slotrank << 24) | 0x60000); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1586 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0); |
| 1587 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x1); |
| 1588 | |
| 1589 | // Drain |
| 1590 | wait_428c(channel); |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | const u32 lane_registers[] = { |
| 1595 | 0x0000, 0x0200, 0x0400, 0x0600, |
| 1596 | 0x1000, 0x1200, 0x1400, 0x1600, |
| 1597 | 0x0800 |
| 1598 | }; |
| 1599 | |
| 1600 | static void program_timings(ramctr_timing * ctrl, int channel) |
| 1601 | { |
| 1602 | u32 reg32, reg_4024, reg_c14, reg_c18, reg_4028; |
| 1603 | int lane; |
| 1604 | int slotrank, slot; |
| 1605 | int full_shift = 0; |
| 1606 | u16 slot320c[NUM_SLOTS]; |
| 1607 | |
| 1608 | FOR_ALL_POPULATED_RANKS { |
| 1609 | if (full_shift < -ctrl->timings[channel][slotrank].val_320c) |
| 1610 | full_shift = -ctrl->timings[channel][slotrank].val_320c; |
| 1611 | } |
| 1612 | |
| 1613 | for (slot = 0; slot < NUM_SLOTS; slot++) |
| 1614 | switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) { |
| 1615 | case 0: |
| 1616 | default: |
| 1617 | slot320c[slot] = 0x7f; |
| 1618 | break; |
| 1619 | case 1: |
| 1620 | slot320c[slot] = |
| 1621 | ctrl->timings[channel][2 * slot + 0].val_320c + |
| 1622 | full_shift; |
| 1623 | break; |
| 1624 | case 2: |
| 1625 | slot320c[slot] = |
| 1626 | ctrl->timings[channel][2 * slot + 1].val_320c + |
| 1627 | full_shift; |
| 1628 | break; |
| 1629 | case 3: |
| 1630 | slot320c[slot] = |
| 1631 | (ctrl->timings[channel][2 * slot].val_320c + |
| 1632 | ctrl->timings[channel][2 * slot + |
| 1633 | 1].val_320c) / 2 + |
| 1634 | full_shift; |
| 1635 | break; |
| 1636 | } |
| 1637 | |
Patrick Rudolph | a1c3bed | 2016-01-24 14:07:15 +0100 | [diff] [blame] | 1638 | /* enable CMD XOVER */ |
| 1639 | reg32 = get_XOVER_CMD(ctrl->rankmap[channel]); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1640 | reg32 |= ((slot320c[0] & 0x3f) << 6) | ((slot320c[0] & 0x40) << 9); |
| 1641 | reg32 |= (slot320c[1] & 0x7f) << 18; |
| 1642 | reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6); |
| 1643 | |
| 1644 | MCHBAR32(0x320c + 0x100 * channel) = reg32; |
| 1645 | |
Patrick Rudolph | a1c3bed | 2016-01-24 14:07:15 +0100 | [diff] [blame] | 1646 | /* enable CLK XOVER */ |
| 1647 | reg_c14 = get_XOVER_CLK(ctrl->rankmap[channel]); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1648 | reg_c18 = 0; |
| 1649 | |
| 1650 | FOR_ALL_POPULATED_RANKS { |
| 1651 | int shift = |
| 1652 | ctrl->timings[channel][slotrank].val_320c + full_shift; |
| 1653 | int offset_val_c14; |
| 1654 | if (shift < 0) |
| 1655 | shift = 0; |
| 1656 | offset_val_c14 = ctrl->reg_c14_offset + shift; |
Patrick Rudolph | a1c3bed | 2016-01-24 14:07:15 +0100 | [diff] [blame] | 1657 | /* set CLK phase shift */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1658 | reg_c14 |= (offset_val_c14 & 0x3f) << (6 * slotrank); |
| 1659 | reg_c18 |= ((offset_val_c14 >> 6) & 1) << slotrank; |
| 1660 | } |
| 1661 | |
| 1662 | MCHBAR32(0xc14 + channel * 0x100) = reg_c14; |
| 1663 | MCHBAR32(0xc18 + channel * 0x100) = reg_c18; |
| 1664 | |
| 1665 | reg_4028 = MCHBAR32(0x4028 + 0x400 * channel); |
| 1666 | reg_4028 &= 0xffff0000; |
| 1667 | |
| 1668 | reg_4024 = 0; |
| 1669 | |
| 1670 | FOR_ALL_POPULATED_RANKS { |
| 1671 | int post_timA_min_high = 7, post_timA_max_high = 0; |
| 1672 | int pre_timA_min_high = 7, pre_timA_max_high = 0; |
| 1673 | int shift_402x = 0; |
| 1674 | int shift = |
| 1675 | ctrl->timings[channel][slotrank].val_320c + full_shift; |
| 1676 | |
| 1677 | if (shift < 0) |
| 1678 | shift = 0; |
| 1679 | |
| 1680 | FOR_ALL_LANES { |
| 1681 | if (post_timA_min_high > |
| 1682 | ((ctrl->timings[channel][slotrank].lanes[lane]. |
| 1683 | timA + shift) >> 6)) |
| 1684 | post_timA_min_high = |
| 1685 | ((ctrl->timings[channel][slotrank]. |
| 1686 | lanes[lane].timA + shift) >> 6); |
| 1687 | if (pre_timA_min_high > |
| 1688 | (ctrl->timings[channel][slotrank].lanes[lane]. |
| 1689 | timA >> 6)) |
| 1690 | pre_timA_min_high = |
| 1691 | (ctrl->timings[channel][slotrank]. |
| 1692 | lanes[lane].timA >> 6); |
| 1693 | if (post_timA_max_high < |
| 1694 | ((ctrl->timings[channel][slotrank].lanes[lane]. |
| 1695 | timA + shift) >> 6)) |
| 1696 | post_timA_max_high = |
| 1697 | ((ctrl->timings[channel][slotrank]. |
| 1698 | lanes[lane].timA + shift) >> 6); |
| 1699 | if (pre_timA_max_high < |
| 1700 | (ctrl->timings[channel][slotrank].lanes[lane]. |
| 1701 | timA >> 6)) |
| 1702 | pre_timA_max_high = |
| 1703 | (ctrl->timings[channel][slotrank]. |
| 1704 | lanes[lane].timA >> 6); |
| 1705 | } |
| 1706 | |
| 1707 | if (pre_timA_max_high - pre_timA_min_high < |
| 1708 | post_timA_max_high - post_timA_min_high) |
| 1709 | shift_402x = +1; |
| 1710 | else if (pre_timA_max_high - pre_timA_min_high > |
| 1711 | post_timA_max_high - post_timA_min_high) |
| 1712 | shift_402x = -1; |
| 1713 | |
| 1714 | reg_4028 |= |
| 1715 | (ctrl->timings[channel][slotrank].val_4028 + shift_402x - |
| 1716 | post_timA_min_high) << (4 * slotrank); |
| 1717 | reg_4024 |= |
| 1718 | (ctrl->timings[channel][slotrank].val_4024 + |
| 1719 | shift_402x) << (8 * slotrank); |
| 1720 | |
| 1721 | FOR_ALL_LANES { |
| 1722 | MCHBAR32(lane_registers[lane] + 0x10 + 0x100 * channel + |
| 1723 | 4 * slotrank) |
| 1724 | = |
| 1725 | (((ctrl->timings[channel][slotrank].lanes[lane]. |
| 1726 | timA + shift) & 0x3f) |
| 1727 | | |
| 1728 | ((ctrl->timings[channel][slotrank].lanes[lane]. |
| 1729 | rising + shift) << 8) |
| 1730 | | |
| 1731 | (((ctrl->timings[channel][slotrank].lanes[lane]. |
| 1732 | timA + shift - |
| 1733 | (post_timA_min_high << 6)) & 0x1c0) << 10) |
Patrick Rudolph | 0188b13 | 2016-02-10 19:16:34 +0100 | [diff] [blame] | 1734 | | ((ctrl->timings[channel][slotrank].lanes[lane]. |
| 1735 | falling + shift) << 20)); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1736 | |
| 1737 | MCHBAR32(lane_registers[lane] + 0x20 + 0x100 * channel + |
| 1738 | 4 * slotrank) |
| 1739 | = |
| 1740 | (((ctrl->timings[channel][slotrank].lanes[lane]. |
| 1741 | timC + shift) & 0x3f) |
| 1742 | | |
| 1743 | (((ctrl->timings[channel][slotrank].lanes[lane]. |
| 1744 | timB + shift) & 0x3f) << 8) |
| 1745 | | |
| 1746 | (((ctrl->timings[channel][slotrank].lanes[lane]. |
| 1747 | timB + shift) & 0x1c0) << 9) |
| 1748 | | |
| 1749 | (((ctrl->timings[channel][slotrank].lanes[lane]. |
| 1750 | timC + shift) & 0x40) << 13)); |
| 1751 | } |
| 1752 | } |
| 1753 | MCHBAR32(0x4024 + 0x400 * channel) = reg_4024; |
| 1754 | MCHBAR32(0x4028 + 0x400 * channel) = reg_4028; |
| 1755 | } |
| 1756 | |
| 1757 | static void test_timA(ramctr_timing * ctrl, int channel, int slotrank) |
| 1758 | { |
| 1759 | wait_428c(channel); |
| 1760 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1761 | /* DRAM command MRS |
| 1762 | * write MR3 MPR enable |
| 1763 | * in this mode only RD and RDA are allowed |
| 1764 | * all reads return a predefined pattern */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1765 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000); |
| 1766 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 1767 | (0xc01 | (ctrl->tMOD << 16))); |
| 1768 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 1769 | (slotrank << 24) | 0x360004); |
| 1770 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0); |
| 1771 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1772 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1773 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105); |
| 1774 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x4040c01); |
| 1775 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, (slotrank << 24)); |
| 1776 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0); |
| 1777 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1778 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1779 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105); |
| 1780 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, |
| 1781 | 0x100f | ((ctrl->CAS + 36) << 16)); |
| 1782 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, |
| 1783 | (slotrank << 24) | 0x60000); |
| 1784 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0); |
| 1785 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 1786 | /* DRAM command MRS |
| 1787 | * write MR3 MPR disable */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1788 | write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000); |
| 1789 | write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, |
| 1790 | (0xc01 | (ctrl->tMOD << 16))); |
| 1791 | write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel, |
| 1792 | (slotrank << 24) | 0x360000); |
| 1793 | write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0); |
| 1794 | |
| 1795 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001); |
| 1796 | |
| 1797 | wait_428c(channel); |
| 1798 | } |
| 1799 | |
| 1800 | static int does_lane_work(ramctr_timing * ctrl, int channel, int slotrank, |
| 1801 | int lane) |
| 1802 | { |
| 1803 | u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA; |
| 1804 | return ((read32 |
| 1805 | (DEFAULT_MCHBAR + lane_registers[lane] + channel * 0x100 + 4 + |
| 1806 | ((timA / 32) & 1) * 4) |
| 1807 | >> (timA % 32)) & 1); |
| 1808 | } |
| 1809 | |
| 1810 | struct run { |
| 1811 | int middle; |
| 1812 | int end; |
| 1813 | int start; |
| 1814 | int all; |
| 1815 | int length; |
| 1816 | }; |
| 1817 | |
| 1818 | static struct run get_longest_zero_run(int *seq, int sz) |
| 1819 | { |
| 1820 | int i, ls; |
| 1821 | int bl = 0, bs = 0; |
| 1822 | struct run ret; |
| 1823 | |
| 1824 | ls = 0; |
| 1825 | for (i = 0; i < 2 * sz; i++) |
| 1826 | if (seq[i % sz]) { |
| 1827 | if (i - ls > bl) { |
| 1828 | bl = i - ls; |
| 1829 | bs = ls; |
| 1830 | } |
| 1831 | ls = i + 1; |
| 1832 | } |
| 1833 | if (bl == 0) { |
| 1834 | ret.middle = sz / 2; |
| 1835 | ret.start = 0; |
| 1836 | ret.end = sz; |
| 1837 | ret.all = 1; |
| 1838 | return ret; |
| 1839 | } |
| 1840 | |
| 1841 | ret.start = bs % sz; |
| 1842 | ret.end = (bs + bl - 1) % sz; |
| 1843 | ret.middle = (bs + (bl - 1) / 2) % sz; |
| 1844 | ret.length = bl; |
| 1845 | ret.all = 0; |
| 1846 | |
| 1847 | return ret; |
| 1848 | } |
| 1849 | |
| 1850 | static void discover_timA_coarse(ramctr_timing * ctrl, int channel, |
| 1851 | int slotrank, int *upperA) |
| 1852 | { |
| 1853 | int timA; |
| 1854 | int statistics[NUM_LANES][128]; |
| 1855 | int lane; |
| 1856 | |
| 1857 | for (timA = 0; timA < 128; timA++) { |
| 1858 | FOR_ALL_LANES { |
| 1859 | ctrl->timings[channel][slotrank].lanes[lane].timA = timA; |
| 1860 | } |
| 1861 | program_timings(ctrl, channel); |
| 1862 | |
| 1863 | test_timA(ctrl, channel, slotrank); |
| 1864 | |
| 1865 | FOR_ALL_LANES { |
| 1866 | statistics[lane][timA] = |
| 1867 | !does_lane_work(ctrl, channel, slotrank, lane); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1868 | printram("Astat: %d, %d, %d: %x, %x\n", |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1869 | channel, slotrank, lane, timA, |
| 1870 | statistics[lane][timA]); |
| 1871 | } |
| 1872 | } |
| 1873 | FOR_ALL_LANES { |
| 1874 | struct run rn = get_longest_zero_run(statistics[lane], 128); |
| 1875 | ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle; |
| 1876 | upperA[lane] = rn.end; |
| 1877 | if (upperA[lane] < rn.middle) |
| 1878 | upperA[lane] += 128; |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1879 | printram("Aval: %d, %d, %d: %x\n", channel, slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1880 | lane, ctrl->timings[channel][slotrank].lanes[lane].timA); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1881 | printram("Aend: %d, %d, %d: %x\n", channel, slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1882 | lane, upperA[lane]); |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | static void discover_timA_fine(ramctr_timing * ctrl, int channel, int slotrank, |
| 1887 | int *upperA) |
| 1888 | { |
| 1889 | int timA_delta; |
| 1890 | int statistics[NUM_LANES][51]; |
| 1891 | int lane, i; |
| 1892 | |
| 1893 | memset(statistics, 0, sizeof(statistics)); |
| 1894 | |
| 1895 | for (timA_delta = -25; timA_delta <= 25; timA_delta++) { |
| 1896 | FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane]. |
| 1897 | timA = upperA[lane] + timA_delta + 0x40; |
| 1898 | program_timings(ctrl, channel); |
| 1899 | |
| 1900 | for (i = 0; i < 100; i++) { |
| 1901 | test_timA(ctrl, channel, slotrank); |
| 1902 | FOR_ALL_LANES { |
| 1903 | statistics[lane][timA_delta + 25] += |
| 1904 | does_lane_work(ctrl, channel, slotrank, |
| 1905 | lane); |
| 1906 | } |
| 1907 | } |
| 1908 | } |
| 1909 | FOR_ALL_LANES { |
| 1910 | int last_zero, first_all; |
| 1911 | |
| 1912 | for (last_zero = -25; last_zero <= 25; last_zero++) |
| 1913 | if (statistics[lane][last_zero + 25]) |
| 1914 | break; |
| 1915 | last_zero--; |
| 1916 | for (first_all = -25; first_all <= 25; first_all++) |
| 1917 | if (statistics[lane][first_all + 25] == 100) |
| 1918 | break; |
| 1919 | |
| 1920 | printram("lane %d: %d, %d\n", lane, last_zero, |
| 1921 | first_all); |
| 1922 | |
| 1923 | ctrl->timings[channel][slotrank].lanes[lane].timA = |
| 1924 | (last_zero + first_all) / 2 + upperA[lane]; |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 1925 | printram("Aval: %d, %d, %d: %x\n", channel, slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1926 | lane, ctrl->timings[channel][slotrank].lanes[lane].timA); |
| 1927 | } |
| 1928 | } |
| 1929 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 1930 | static int discover_402x(ramctr_timing *ctrl, int channel, int slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1931 | int *upperA) |
| 1932 | { |
| 1933 | int works[NUM_LANES]; |
| 1934 | int lane; |
| 1935 | while (1) { |
| 1936 | int all_works = 1, some_works = 0; |
| 1937 | program_timings(ctrl, channel); |
| 1938 | test_timA(ctrl, channel, slotrank); |
| 1939 | FOR_ALL_LANES { |
| 1940 | works[lane] = |
| 1941 | !does_lane_work(ctrl, channel, slotrank, lane); |
| 1942 | if (works[lane]) |
| 1943 | some_works = 1; |
| 1944 | else |
| 1945 | all_works = 0; |
| 1946 | } |
| 1947 | if (all_works) |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 1948 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1949 | if (!some_works) { |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 1950 | if (ctrl->timings[channel][slotrank].val_4024 < 2) { |
| 1951 | printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n", |
| 1952 | channel, slotrank); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 1953 | return MAKE_ERR; |
| 1954 | } |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1955 | ctrl->timings[channel][slotrank].val_4024 -= 2; |
| 1956 | printram("4024 -= 2;\n"); |
| 1957 | continue; |
| 1958 | } |
| 1959 | ctrl->timings[channel][slotrank].val_4028 += 2; |
| 1960 | printram("4028 += 2;\n"); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 1961 | if (ctrl->timings[channel][slotrank].val_4028 >= 0x10) { |
| 1962 | printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n", |
| 1963 | channel, slotrank); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 1964 | return MAKE_ERR; |
| 1965 | } |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1966 | FOR_ALL_LANES if (works[lane]) { |
| 1967 | ctrl->timings[channel][slotrank].lanes[lane].timA += |
| 1968 | 128; |
| 1969 | upperA[lane] += 128; |
| 1970 | printram("increment %d, %d, %d\n", channel, |
| 1971 | slotrank, lane); |
| 1972 | } |
| 1973 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 1974 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | struct timA_minmax { |
| 1978 | int timA_min_high, timA_max_high; |
| 1979 | }; |
| 1980 | |
| 1981 | static void pre_timA_change(ramctr_timing * ctrl, int channel, int slotrank, |
| 1982 | struct timA_minmax *mnmx) |
| 1983 | { |
| 1984 | int lane; |
| 1985 | mnmx->timA_min_high = 7; |
| 1986 | mnmx->timA_max_high = 0; |
| 1987 | |
| 1988 | FOR_ALL_LANES { |
| 1989 | if (mnmx->timA_min_high > |
| 1990 | (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6)) |
| 1991 | mnmx->timA_min_high = |
| 1992 | (ctrl->timings[channel][slotrank].lanes[lane]. |
| 1993 | timA >> 6); |
| 1994 | if (mnmx->timA_max_high < |
| 1995 | (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6)) |
| 1996 | mnmx->timA_max_high = |
| 1997 | (ctrl->timings[channel][slotrank].lanes[lane]. |
| 1998 | timA >> 6); |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | static void post_timA_change(ramctr_timing * ctrl, int channel, int slotrank, |
| 2003 | struct timA_minmax *mnmx) |
| 2004 | { |
| 2005 | struct timA_minmax post; |
| 2006 | int shift_402x = 0; |
| 2007 | |
| 2008 | /* Get changed maxima. */ |
| 2009 | pre_timA_change(ctrl, channel, slotrank, &post); |
| 2010 | |
| 2011 | if (mnmx->timA_max_high - mnmx->timA_min_high < |
| 2012 | post.timA_max_high - post.timA_min_high) |
| 2013 | shift_402x = +1; |
| 2014 | else if (mnmx->timA_max_high - mnmx->timA_min_high > |
| 2015 | post.timA_max_high - post.timA_min_high) |
| 2016 | shift_402x = -1; |
| 2017 | else |
| 2018 | shift_402x = 0; |
| 2019 | |
| 2020 | ctrl->timings[channel][slotrank].val_4028 += shift_402x; |
| 2021 | ctrl->timings[channel][slotrank].val_4024 += shift_402x; |
| 2022 | printram("4024 += %d;\n", shift_402x); |
| 2023 | printram("4028 += %d;\n", shift_402x); |
| 2024 | } |
| 2025 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2026 | /* Compensate the skew between DQS and DQs. |
| 2027 | * To ease PCB design a small skew between Data Strobe signals and |
| 2028 | * Data Signals is allowed. |
| 2029 | * The controller has to measure and compensate this skew for every byte-lane. |
| 2030 | * By delaying either all DQs signals or DQS signal, a full phase |
| 2031 | * shift can be introduced. |
| 2032 | * It is assumed that one byte-lane's DQs signals have the same routing delay. |
| 2033 | * |
| 2034 | * To measure the actual skew, the DRAM is placed in "read leveling" mode. |
| 2035 | * In read leveling mode the DRAM-chip outputs an alternating periodic pattern. |
| 2036 | * The memory controller iterates over all possible values to do a full phase shift |
| 2037 | * and issues read commands. |
| 2038 | * With DQS and DQs in phase the data read is expected to alternate on every byte: |
| 2039 | * 0xFF 0x00 0xFF ... |
| 2040 | * Once the controller has detected this pattern a bit in the result register is |
| 2041 | * set for the current phase shift. |
| 2042 | */ |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2043 | static int read_training(ramctr_timing * ctrl) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2044 | { |
| 2045 | int channel, slotrank, lane; |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2046 | int err; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2047 | |
| 2048 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2049 | int all_high, some_high; |
| 2050 | int upperA[NUM_LANES]; |
| 2051 | struct timA_minmax mnmx; |
| 2052 | |
| 2053 | wait_428c(channel); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2054 | |
| 2055 | /* DRAM command PREA */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2056 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002); |
| 2057 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 2058 | 0xc01 | (ctrl->tRP << 16)); |
| 2059 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2060 | (slotrank << 24) | 0x60400); |
| 2061 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0); |
| 2062 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1); |
| 2063 | |
| 2064 | write32(DEFAULT_MCHBAR + 0x3400, (slotrank << 2) | 0x8001); |
| 2065 | |
| 2066 | ctrl->timings[channel][slotrank].val_4028 = 4; |
| 2067 | ctrl->timings[channel][slotrank].val_4024 = 55; |
| 2068 | program_timings(ctrl, channel); |
| 2069 | |
| 2070 | discover_timA_coarse(ctrl, channel, slotrank, upperA); |
| 2071 | |
| 2072 | all_high = 1; |
| 2073 | some_high = 0; |
| 2074 | FOR_ALL_LANES { |
| 2075 | if (ctrl->timings[channel][slotrank].lanes[lane]. |
| 2076 | timA >= 0x40) |
| 2077 | some_high = 1; |
| 2078 | else |
| 2079 | all_high = 0; |
| 2080 | } |
| 2081 | |
| 2082 | if (all_high) { |
| 2083 | ctrl->timings[channel][slotrank].val_4028--; |
| 2084 | printram("4028--;\n"); |
| 2085 | FOR_ALL_LANES { |
| 2086 | ctrl->timings[channel][slotrank].lanes[lane]. |
| 2087 | timA -= 0x40; |
| 2088 | upperA[lane] -= 0x40; |
| 2089 | |
| 2090 | } |
| 2091 | } else if (some_high) { |
| 2092 | ctrl->timings[channel][slotrank].val_4024++; |
| 2093 | ctrl->timings[channel][slotrank].val_4028++; |
| 2094 | printram("4024++;\n"); |
| 2095 | printram("4028++;\n"); |
| 2096 | } |
| 2097 | |
| 2098 | program_timings(ctrl, channel); |
| 2099 | |
| 2100 | pre_timA_change(ctrl, channel, slotrank, &mnmx); |
| 2101 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2102 | err = discover_402x(ctrl, channel, slotrank, upperA); |
| 2103 | if (err) |
| 2104 | return err; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2105 | |
| 2106 | post_timA_change(ctrl, channel, slotrank, &mnmx); |
| 2107 | pre_timA_change(ctrl, channel, slotrank, &mnmx); |
| 2108 | |
| 2109 | discover_timA_fine(ctrl, channel, slotrank, upperA); |
| 2110 | |
| 2111 | post_timA_change(ctrl, channel, slotrank, &mnmx); |
| 2112 | pre_timA_change(ctrl, channel, slotrank, &mnmx); |
| 2113 | |
| 2114 | FOR_ALL_LANES { |
| 2115 | ctrl->timings[channel][slotrank].lanes[lane].timA -= mnmx.timA_min_high * 0x40; |
| 2116 | } |
| 2117 | ctrl->timings[channel][slotrank].val_4028 -= mnmx.timA_min_high; |
| 2118 | printram("4028 -= %d;\n", mnmx.timA_min_high); |
| 2119 | |
| 2120 | post_timA_change(ctrl, channel, slotrank, &mnmx); |
| 2121 | |
| 2122 | printram("4/8: %d, %d, %x, %x\n", channel, slotrank, |
| 2123 | ctrl->timings[channel][slotrank].val_4024, |
| 2124 | ctrl->timings[channel][slotrank].val_4028); |
| 2125 | |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 2126 | printram("final results:\n"); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2127 | FOR_ALL_LANES |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 2128 | printram("Aval: %d, %d, %d: %x\n", channel, slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2129 | lane, |
| 2130 | ctrl->timings[channel][slotrank].lanes[lane].timA); |
| 2131 | |
| 2132 | write32(DEFAULT_MCHBAR + 0x3400, 0); |
| 2133 | |
Patrick Rudolph | 9b51568 | 2015-10-09 13:43:51 +0200 | [diff] [blame] | 2134 | toggle_io_reset(); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | FOR_ALL_POPULATED_CHANNELS { |
| 2138 | program_timings(ctrl, channel); |
| 2139 | } |
| 2140 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 2141 | write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel |
| 2142 | + 4 * lane, 0); |
| 2143 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2144 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | static void test_timC(ramctr_timing * ctrl, int channel, int slotrank) |
| 2148 | { |
| 2149 | int lane; |
| 2150 | |
| 2151 | FOR_ALL_LANES { |
| 2152 | write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel + 4 * lane, 0); |
| 2153 | read32(DEFAULT_MCHBAR + 0x4140 + 0x400 * channel + 4 * lane); |
| 2154 | } |
| 2155 | |
| 2156 | wait_428c(channel); |
| 2157 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2158 | /* DRAM command ACT */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2159 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006); |
| 2160 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 2161 | (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD) << 10) |
| 2162 | | 4 | (ctrl->tRCD << 16)); |
| 2163 | |
| 2164 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2165 | (slotrank << 24) | (6 << 16)); |
| 2166 | |
| 2167 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244); |
| 2168 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2169 | /* DRAM command NOP */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2170 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207); |
| 2171 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8041001); |
| 2172 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 2173 | (slotrank << 24) | 8); |
| 2174 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0); |
| 2175 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2176 | /* DRAM command WR */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2177 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201); |
| 2178 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x80411f4); |
| 2179 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24)); |
| 2180 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242); |
| 2181 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2182 | /* DRAM command NOP */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2183 | write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207); |
| 2184 | write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, |
| 2185 | 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16)); |
| 2186 | write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel, |
| 2187 | (slotrank << 24) | 8); |
| 2188 | write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0); |
| 2189 | |
| 2190 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001); |
| 2191 | |
| 2192 | wait_428c(channel); |
| 2193 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2194 | /* DRAM command PREA */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2195 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002); |
| 2196 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 2197 | 0xc01 | (ctrl->tRP << 16)); |
| 2198 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2199 | (slotrank << 24) | 0x60400); |
| 2200 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240); |
| 2201 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2202 | /* DRAM command ACT */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2203 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006); |
| 2204 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, |
| 2205 | (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) << 10) |
| 2206 | | 8 | (ctrl->CAS << 16)); |
| 2207 | |
| 2208 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 2209 | (slotrank << 24) | 0x60000); |
| 2210 | |
| 2211 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x244); |
| 2212 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2213 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2214 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105); |
| 2215 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, |
| 2216 | 0x40011f4 | (max(ctrl->tRTP, 8) << 16)); |
| 2217 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24)); |
| 2218 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242); |
| 2219 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2220 | /* DRAM command PREA */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2221 | write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002); |
| 2222 | write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, |
| 2223 | 0xc01 | (ctrl->tRP << 16)); |
| 2224 | write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel, |
| 2225 | (slotrank << 24) | 0x60400); |
| 2226 | write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240); |
| 2227 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001); |
| 2228 | wait_428c(channel); |
| 2229 | } |
| 2230 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2231 | static int discover_timC(ramctr_timing *ctrl, int channel, int slotrank) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2232 | { |
| 2233 | int timC; |
| 2234 | int statistics[NUM_LANES][MAX_TIMC + 1]; |
| 2235 | int lane; |
| 2236 | |
| 2237 | wait_428c(channel); |
| 2238 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2239 | /* DRAM command PREA */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2240 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002); |
| 2241 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 2242 | 0xc01 | (ctrl->tRP << 16)); |
| 2243 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2244 | (slotrank << 24) | 0x60400); |
| 2245 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240); |
| 2246 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1); |
| 2247 | |
| 2248 | for (timC = 0; timC <= MAX_TIMC; timC++) { |
| 2249 | FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane]. |
| 2250 | timC = timC; |
| 2251 | program_timings(ctrl, channel); |
| 2252 | |
| 2253 | test_timC(ctrl, channel, slotrank); |
| 2254 | |
| 2255 | FOR_ALL_LANES { |
| 2256 | statistics[lane][timC] = |
| 2257 | read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane + |
| 2258 | 0x400 * channel); |
| 2259 | printram("Cstat: %d, %d, %d, %x, %x\n", |
| 2260 | channel, slotrank, lane, timC, |
| 2261 | statistics[lane][timC]); |
| 2262 | } |
| 2263 | } |
| 2264 | FOR_ALL_LANES { |
| 2265 | struct run rn = |
| 2266 | get_longest_zero_run(statistics[lane], MAX_TIMC + 1); |
| 2267 | ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle; |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2268 | if (rn.all) { |
| 2269 | printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n", |
| 2270 | channel, slotrank, lane); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2271 | return MAKE_ERR; |
| 2272 | } |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 2273 | printram("Cval: %d, %d, %d: %x\n", channel, slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2274 | lane, ctrl->timings[channel][slotrank].lanes[lane].timC); |
| 2275 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2276 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | static int get_precedening_channels(ramctr_timing * ctrl, int target_channel) |
| 2280 | { |
| 2281 | int channel, ret = 0; |
| 2282 | FOR_ALL_POPULATED_CHANNELS if (channel < target_channel) |
| 2283 | ret++; |
| 2284 | return ret; |
| 2285 | } |
| 2286 | |
| 2287 | static void fill_pattern0(ramctr_timing * ctrl, int channel, u32 a, u32 b) |
| 2288 | { |
| 2289 | unsigned j; |
| 2290 | unsigned channel_offset = |
| 2291 | get_precedening_channels(ctrl, channel) * 0x40; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2292 | for (j = 0; j < 16; j++) |
| 2293 | write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a); |
| 2294 | sfence(); |
| 2295 | } |
| 2296 | |
| 2297 | static int num_of_channels(const ramctr_timing * ctrl) |
| 2298 | { |
| 2299 | int ret = 0; |
| 2300 | int channel; |
| 2301 | FOR_ALL_POPULATED_CHANNELS ret++; |
| 2302 | return ret; |
| 2303 | } |
| 2304 | |
| 2305 | static void fill_pattern1(ramctr_timing * ctrl, int channel) |
| 2306 | { |
| 2307 | unsigned j; |
| 2308 | unsigned channel_offset = |
| 2309 | get_precedening_channels(ctrl, channel) * 0x40; |
| 2310 | unsigned channel_step = 0x40 * num_of_channels(ctrl); |
| 2311 | for (j = 0; j < 16; j++) |
| 2312 | write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff); |
| 2313 | for (j = 0; j < 16; j++) |
| 2314 | write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0); |
| 2315 | sfence(); |
| 2316 | } |
| 2317 | |
| 2318 | static void precharge(ramctr_timing * ctrl) |
| 2319 | { |
| 2320 | int channel, slotrank, lane; |
| 2321 | |
| 2322 | FOR_ALL_POPULATED_CHANNELS { |
| 2323 | FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 2324 | ctrl->timings[channel][slotrank].lanes[lane].falling = |
| 2325 | 16; |
| 2326 | ctrl->timings[channel][slotrank].lanes[lane].rising = |
| 2327 | 16; |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | program_timings(ctrl, channel); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2331 | |
| 2332 | FOR_ALL_POPULATED_RANKS { |
| 2333 | wait_428c(channel); |
| 2334 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2335 | /* DRAM command MRS |
| 2336 | * write MR3 MPR enable |
| 2337 | * in this mode only RD and RDA are allowed |
| 2338 | * all reads return a predefined pattern */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2339 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, |
| 2340 | 0x1f000); |
| 2341 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 2342 | 0xc01 | (ctrl->tMOD << 16)); |
| 2343 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2344 | (slotrank << 24) | 0x360004); |
| 2345 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0); |
| 2346 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2347 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2348 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, |
| 2349 | 0x1f105); |
| 2350 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, |
| 2351 | 0x4041003); |
| 2352 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 2353 | (slotrank << 24) | 0); |
| 2354 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0); |
| 2355 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2356 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2357 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, |
| 2358 | 0x1f105); |
| 2359 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, |
| 2360 | 0x1001 | ((ctrl->CAS + 8) << 16)); |
| 2361 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, |
| 2362 | (slotrank << 24) | 0x60000); |
| 2363 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0); |
| 2364 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2365 | /* DRAM command MRS |
| 2366 | * write MR3 MPR disable */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2367 | write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, |
| 2368 | 0x1f000); |
| 2369 | write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, |
| 2370 | 0xc01 | (ctrl->tMOD << 16)); |
| 2371 | write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel, |
| 2372 | (slotrank << 24) | 0x360000); |
| 2373 | write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0); |
| 2374 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, |
| 2375 | 0xc0001); |
| 2376 | |
| 2377 | wait_428c(channel); |
| 2378 | } |
| 2379 | |
| 2380 | FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 2381 | ctrl->timings[channel][slotrank].lanes[lane].falling = |
| 2382 | 48; |
| 2383 | ctrl->timings[channel][slotrank].lanes[lane].rising = |
| 2384 | 48; |
| 2385 | } |
| 2386 | |
| 2387 | program_timings(ctrl, channel); |
| 2388 | |
| 2389 | FOR_ALL_POPULATED_RANKS { |
| 2390 | wait_428c(channel); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2391 | /* DRAM command MRS |
| 2392 | * write MR3 MPR enable |
| 2393 | * in this mode only RD and RDA are allowed |
| 2394 | * all reads return a predefined pattern */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2395 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, |
| 2396 | 0x1f000); |
| 2397 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 2398 | 0xc01 | (ctrl->tMOD << 16)); |
| 2399 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2400 | (slotrank << 24) | 0x360004); |
| 2401 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0); |
| 2402 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2403 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2404 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, |
| 2405 | 0x1f105); |
| 2406 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, |
| 2407 | 0x4041003); |
| 2408 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 2409 | (slotrank << 24) | 0); |
| 2410 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0); |
| 2411 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2412 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2413 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, |
| 2414 | 0x1f105); |
| 2415 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, |
| 2416 | 0x1001 | ((ctrl->CAS + 8) << 16)); |
| 2417 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, |
| 2418 | (slotrank << 24) | 0x60000); |
| 2419 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0); |
| 2420 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2421 | /* DRAM command MRS |
| 2422 | * write MR3 MPR disable */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2423 | write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, |
| 2424 | 0x1f000); |
| 2425 | write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, |
| 2426 | 0xc01 | (ctrl->tMOD << 16)); |
| 2427 | |
| 2428 | write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel, |
| 2429 | (slotrank << 24) | 0x360000); |
| 2430 | write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0); |
| 2431 | |
| 2432 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, |
| 2433 | 0xc0001); |
| 2434 | wait_428c(channel); |
| 2435 | } |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | static void test_timB(ramctr_timing * ctrl, int channel, int slotrank) |
| 2440 | { |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2441 | /* enable DQs on this slotrank */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2442 | write_mrreg(ctrl, channel, slotrank, 1, |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 2443 | 0x80 | make_mr1(ctrl, slotrank, channel)); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2444 | |
| 2445 | wait_428c(channel); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2446 | /* DRAM command NOP */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2447 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f207); |
| 2448 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 2449 | 0x8000c01 | ((ctrl->CWL + ctrl->tWLO) << 16)); |
| 2450 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2451 | 8 | (slotrank << 24)); |
| 2452 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0); |
| 2453 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2454 | /* DRAM command NOP */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2455 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f107); |
| 2456 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, |
| 2457 | 0x4000c01 | ((ctrl->CAS + 38) << 16)); |
| 2458 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 2459 | (slotrank << 24) | 4); |
| 2460 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0); |
| 2461 | |
| 2462 | write32(DEFAULT_MCHBAR + 0x400 * channel + 0x4284, 0x40001); |
| 2463 | wait_428c(channel); |
| 2464 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2465 | /* disable DQs on this slotrank */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2466 | write_mrreg(ctrl, channel, slotrank, 1, |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 2467 | 0x1080 | make_mr1(ctrl, slotrank, channel)); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2468 | } |
| 2469 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2470 | static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2471 | { |
| 2472 | int timB; |
| 2473 | int statistics[NUM_LANES][128]; |
| 2474 | int lane; |
| 2475 | |
| 2476 | write32(DEFAULT_MCHBAR + 0x3400, 0x108052 | (slotrank << 2)); |
| 2477 | |
| 2478 | for (timB = 0; timB < 128; timB++) { |
| 2479 | FOR_ALL_LANES { |
| 2480 | ctrl->timings[channel][slotrank].lanes[lane].timB = timB; |
| 2481 | } |
| 2482 | program_timings(ctrl, channel); |
| 2483 | |
| 2484 | test_timB(ctrl, channel, slotrank); |
| 2485 | |
| 2486 | FOR_ALL_LANES { |
| 2487 | statistics[lane][timB] = |
| 2488 | !((read32 |
| 2489 | (DEFAULT_MCHBAR + lane_registers[lane] + |
| 2490 | channel * 0x100 + 4 + ((timB / 32) & 1) * 4) |
| 2491 | >> (timB % 32)) & 1); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 2492 | printram("Bstat: %d, %d, %d: %x, %x\n", |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2493 | channel, slotrank, lane, timB, |
| 2494 | statistics[lane][timB]); |
| 2495 | } |
| 2496 | } |
| 2497 | FOR_ALL_LANES { |
| 2498 | struct run rn = get_longest_zero_run(statistics[lane], 128); |
Patrick Rudolph | d912f1d | 2016-02-15 20:07:42 +0100 | [diff] [blame] | 2499 | /* timC is a direct function of timB's 6 LSBs. |
| 2500 | * Some tests increments the value of timB by a small value, |
| 2501 | * which might cause the 6bit value to overflow, if it's close |
| 2502 | * to 0x3F. Increment the value by a small offset if it's likely |
| 2503 | * to overflow, to make sure it won't overflow while running |
| 2504 | * tests and bricks the system due to a non matching timC. |
| 2505 | * |
| 2506 | * TODO: find out why some tests (edge write discovery) |
| 2507 | * increment timB. */ |
| 2508 | if ((rn.start & 0x3F) == 0x3E) |
| 2509 | rn.start += 2; |
| 2510 | else if ((rn.start & 0x3F) == 0x3F) |
| 2511 | rn.start += 1; |
Vladimir Serbinenko | 3141eac | 2016-01-29 19:42:02 +0100 | [diff] [blame] | 2512 | ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start; |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2513 | if (rn.all) { |
| 2514 | printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n", |
| 2515 | channel, slotrank, lane); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2516 | return MAKE_ERR; |
| 2517 | } |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 2518 | printram("Bval: %d, %d, %d: %x\n", channel, slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2519 | lane, ctrl->timings[channel][slotrank].lanes[lane].timB); |
| 2520 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2521 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | static int get_timB_high_adjust(u64 val) |
| 2525 | { |
| 2526 | int i; |
| 2527 | |
| 2528 | /* good */ |
| 2529 | if (val == 0xffffffffffffffffLL) |
| 2530 | return 0; |
| 2531 | |
| 2532 | if (val >= 0xf000000000000000LL) { |
| 2533 | /* needs negative adjustment */ |
| 2534 | for (i = 0; i < 8; i++) |
| 2535 | if (val << (8 * (7 - i) + 4)) |
| 2536 | return -i; |
| 2537 | } else { |
| 2538 | /* needs positive adjustment */ |
| 2539 | for (i = 0; i < 8; i++) |
| 2540 | if (val >> (8 * (7 - i) + 4)) |
| 2541 | return i; |
| 2542 | } |
| 2543 | return 8; |
| 2544 | } |
| 2545 | |
| 2546 | static void adjust_high_timB(ramctr_timing * ctrl) |
| 2547 | { |
| 2548 | int channel, slotrank, lane, old; |
| 2549 | write32(DEFAULT_MCHBAR + 0x3400, 0x200); |
| 2550 | FOR_ALL_POPULATED_CHANNELS { |
| 2551 | fill_pattern1(ctrl, channel); |
| 2552 | write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 1); |
| 2553 | } |
| 2554 | FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS { |
| 2555 | |
| 2556 | write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x10001); |
| 2557 | |
| 2558 | wait_428c(channel); |
| 2559 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2560 | /* DRAM command ACT */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2561 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006); |
| 2562 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 2563 | 0xc01 | (ctrl->tRCD << 16)); |
| 2564 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2565 | (slotrank << 24) | 0x60000); |
| 2566 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0); |
| 2567 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2568 | /* DRAM command NOP */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2569 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207); |
| 2570 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8040c01); |
| 2571 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 2572 | (slotrank << 24) | 0x8); |
| 2573 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0); |
| 2574 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2575 | /* DRAM command WR */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2576 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201); |
| 2577 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x8041003); |
| 2578 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, |
| 2579 | (slotrank << 24)); |
| 2580 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x3e2); |
| 2581 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2582 | /* DRAM command NOP */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2583 | write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207); |
| 2584 | write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, |
| 2585 | 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16)); |
| 2586 | write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel, |
| 2587 | (slotrank << 24) | 0x8); |
| 2588 | write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0); |
| 2589 | |
| 2590 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001); |
| 2591 | |
| 2592 | wait_428c(channel); |
| 2593 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2594 | /* DRAM command PREA */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2595 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002); |
| 2596 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 2597 | 0xc01 | ((ctrl->tRP) << 16)); |
| 2598 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2599 | (slotrank << 24) | 0x60400); |
| 2600 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240); |
| 2601 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2602 | /* DRAM command ACT */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2603 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006); |
| 2604 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, |
| 2605 | 0xc01 | ((ctrl->tRCD) << 16)); |
| 2606 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 2607 | (slotrank << 24) | 0x60000); |
| 2608 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0); |
| 2609 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2610 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2611 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x3f105); |
| 2612 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, |
| 2613 | 0x4000c01 | |
| 2614 | ((ctrl->tRP + |
| 2615 | ctrl->timings[channel][slotrank].val_4024 + |
| 2616 | ctrl->timings[channel][slotrank].val_4028) << 16)); |
| 2617 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, |
| 2618 | (slotrank << 24) | 0x60008); |
| 2619 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0); |
| 2620 | |
| 2621 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001); |
| 2622 | wait_428c(channel); |
| 2623 | FOR_ALL_LANES { |
| 2624 | u64 res = |
| 2625 | read32(DEFAULT_MCHBAR + lane_registers[lane] + |
| 2626 | 0x100 * channel + 4); |
| 2627 | res |= |
| 2628 | ((u64) read32(DEFAULT_MCHBAR + lane_registers[lane] + |
| 2629 | 0x100 * channel + 8)) << 32; |
| 2630 | old = ctrl->timings[channel][slotrank].lanes[lane].timB; |
| 2631 | ctrl->timings[channel][slotrank].lanes[lane].timB += |
| 2632 | get_timB_high_adjust(res) * 64; |
| 2633 | |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 2634 | printram("High adjust %d:%016llx\n", lane, res); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2635 | printram("Bval+: %d, %d, %d, %x -> %x\n", channel, |
| 2636 | slotrank, lane, old, |
| 2637 | ctrl->timings[channel][slotrank].lanes[lane]. |
| 2638 | timB); |
| 2639 | } |
| 2640 | } |
| 2641 | write32(DEFAULT_MCHBAR + 0x3400, 0); |
| 2642 | } |
| 2643 | |
| 2644 | static void write_op(ramctr_timing * ctrl, int channel) |
| 2645 | { |
| 2646 | int slotrank; |
| 2647 | |
| 2648 | wait_428c(channel); |
| 2649 | |
| 2650 | /* choose an existing rank. */ |
| 2651 | slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0; |
| 2652 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2653 | /* DRAM command ACT */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2654 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003); |
| 2655 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001); |
| 2656 | |
| 2657 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2658 | (slotrank << 24) | 0x60000); |
| 2659 | |
| 2660 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0); |
| 2661 | |
| 2662 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1); |
| 2663 | wait_428c(channel); |
| 2664 | } |
| 2665 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2666 | /* Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes. |
| 2667 | * DDR3 adopted the fly-by topology. The data and strobes signals reach |
| 2668 | * the chips at different times with respect to command, address and |
| 2669 | * clock signals. |
| 2670 | * By delaying either all DQ/DQs or all CMD/ADDR/CLK signals, a full phase |
| 2671 | * shift can be introduced. |
| 2672 | * It is assumed that the CLK/ADDR/CMD signals have the same routing delay. |
| 2673 | * |
| 2674 | * To find the required phase shift the DRAM is placed in "write leveling" mode. |
| 2675 | * In this mode the DRAM-chip samples the CLK on every DQS edge and feeds back the |
| 2676 | * sampled value on the data lanes (DQs). |
| 2677 | */ |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2678 | static int write_training(ramctr_timing * ctrl) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2679 | { |
| 2680 | int channel, slotrank, lane; |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2681 | int err; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2682 | |
| 2683 | FOR_ALL_POPULATED_CHANNELS |
| 2684 | write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel, |
| 2685 | read32(DEFAULT_MCHBAR + 0x4008 + |
| 2686 | 0x400 * channel) | 0x8000000); |
| 2687 | |
| 2688 | FOR_ALL_POPULATED_CHANNELS { |
| 2689 | write_op(ctrl, channel); |
| 2690 | write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel, |
| 2691 | read32(DEFAULT_MCHBAR + 0x4020 + |
| 2692 | 0x400 * channel) | 0x200000); |
| 2693 | } |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2694 | |
| 2695 | /* refresh disable */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2696 | write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8); |
| 2697 | FOR_ALL_POPULATED_CHANNELS { |
| 2698 | write_op(ctrl, channel); |
| 2699 | } |
| 2700 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2701 | /* enable write leveling on all ranks |
| 2702 | * disable all DQ outputs |
| 2703 | * only NOP is allowed in this mode */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2704 | FOR_ALL_CHANNELS |
| 2705 | FOR_ALL_POPULATED_RANKS |
| 2706 | write_mrreg(ctrl, channel, slotrank, 1, |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 2707 | make_mr1(ctrl, slotrank, channel) | 0x1080); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2708 | |
| 2709 | write32(DEFAULT_MCHBAR + 0x3400, 0x108052); |
| 2710 | |
Patrick Rudolph | 9b51568 | 2015-10-09 13:43:51 +0200 | [diff] [blame] | 2711 | toggle_io_reset(); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2712 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2713 | /* set any valid value for timB, it gets corrected later */ |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2714 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
| 2715 | err = discover_timB(ctrl, channel, slotrank); |
| 2716 | if (err) |
| 2717 | return err; |
| 2718 | } |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2719 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2720 | /* disable write leveling on all ranks */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2721 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS |
| 2722 | write_mrreg(ctrl, channel, |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 2723 | slotrank, 1, make_mr1(ctrl, slotrank, channel)); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2724 | |
| 2725 | write32(DEFAULT_MCHBAR + 0x3400, 0); |
| 2726 | |
| 2727 | FOR_ALL_POPULATED_CHANNELS |
| 2728 | wait_428c(channel); |
| 2729 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2730 | /* refresh enable */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2731 | write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) | 8); |
| 2732 | |
| 2733 | FOR_ALL_POPULATED_CHANNELS { |
| 2734 | write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel, |
| 2735 | ~0x00200000 & read32(DEFAULT_MCHBAR + 0x4020 + |
| 2736 | 0x400 * channel)); |
| 2737 | read32(DEFAULT_MCHBAR + 0x428c + 0x400 * channel); |
| 2738 | wait_428c(channel); |
| 2739 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2740 | /* DRAM command ZQCS */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2741 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003); |
| 2742 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001); |
| 2743 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, 0x60000); |
| 2744 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0); |
| 2745 | |
| 2746 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1); |
| 2747 | wait_428c(channel); |
| 2748 | } |
| 2749 | |
Patrick Rudolph | 9b51568 | 2015-10-09 13:43:51 +0200 | [diff] [blame] | 2750 | toggle_io_reset(); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2751 | |
| 2752 | printram("CPE\n"); |
| 2753 | precharge(ctrl); |
| 2754 | printram("CPF\n"); |
| 2755 | |
| 2756 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 2757 | read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane); |
| 2758 | write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane, |
| 2759 | 0); |
| 2760 | } |
| 2761 | |
| 2762 | FOR_ALL_POPULATED_CHANNELS { |
| 2763 | fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555); |
| 2764 | write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0); |
| 2765 | } |
| 2766 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2767 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
| 2768 | err = discover_timC(ctrl, channel, slotrank); |
| 2769 | if (err) |
| 2770 | return err; |
| 2771 | } |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2772 | |
| 2773 | FOR_ALL_POPULATED_CHANNELS |
| 2774 | program_timings(ctrl, channel); |
| 2775 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2776 | /* measure and adjust timB timings */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2777 | adjust_high_timB(ctrl); |
| 2778 | |
| 2779 | FOR_ALL_POPULATED_CHANNELS |
| 2780 | program_timings(ctrl, channel); |
| 2781 | |
| 2782 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 2783 | read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane); |
| 2784 | write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane, |
| 2785 | 0); |
| 2786 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2787 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | static int test_320c(ramctr_timing * ctrl, int channel, int slotrank) |
| 2791 | { |
| 2792 | struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank]; |
| 2793 | int timC_delta; |
| 2794 | int lanes_ok = 0; |
| 2795 | int ctr = 0; |
| 2796 | int lane; |
| 2797 | |
| 2798 | for (timC_delta = -5; timC_delta <= 5; timC_delta++) { |
| 2799 | FOR_ALL_LANES { |
| 2800 | ctrl->timings[channel][slotrank].lanes[lane].timC = |
| 2801 | saved_rt.lanes[lane].timC + timC_delta; |
| 2802 | } |
| 2803 | program_timings(ctrl, channel); |
| 2804 | FOR_ALL_LANES { |
| 2805 | write32(DEFAULT_MCHBAR + 4 * lane + 0x4f40, 0); |
| 2806 | } |
| 2807 | |
| 2808 | write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f); |
| 2809 | |
| 2810 | wait_428c(channel); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2811 | /* DRAM command ACT */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2812 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006); |
| 2813 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 2814 | ((max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1)) << 10) |
| 2815 | | 8 | (ctrl->tRCD << 16)); |
| 2816 | |
| 2817 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2818 | (slotrank << 24) | ctr | 0x60000); |
| 2819 | |
| 2820 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2821 | /* DRAM command WR */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2822 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201); |
| 2823 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, |
| 2824 | 0x8001020 | ((ctrl->CWL + ctrl->tWTR + 8) << 16)); |
| 2825 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 2826 | (slotrank << 24)); |
| 2827 | write32(DEFAULT_MCHBAR + 0x4244 + 0x400 * channel, 0x389abcd); |
| 2828 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x20e42); |
| 2829 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2830 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2831 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105); |
| 2832 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, |
| 2833 | 0x4001020 | (max(ctrl->tRTP, 8) << 16)); |
| 2834 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, |
| 2835 | (slotrank << 24)); |
| 2836 | write32(DEFAULT_MCHBAR + 0x4248 + 0x400 * channel, 0x389abcd); |
| 2837 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x20e42); |
| 2838 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2839 | /* DRAM command PRE */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2840 | write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002); |
| 2841 | write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, 0xf1001); |
| 2842 | write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel, |
| 2843 | (slotrank << 24) | 0x60400); |
| 2844 | write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240); |
| 2845 | |
| 2846 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001); |
| 2847 | wait_428c(channel); |
| 2848 | FOR_ALL_LANES { |
| 2849 | u32 r32 = |
| 2850 | read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane + |
| 2851 | 0x400 * channel); |
| 2852 | |
| 2853 | if (r32 == 0) |
| 2854 | lanes_ok |= 1 << lane; |
| 2855 | } |
| 2856 | ctr++; |
| 2857 | if (lanes_ok == ((1 << NUM_LANES) - 1)) |
| 2858 | break; |
| 2859 | } |
| 2860 | |
| 2861 | ctrl->timings[channel][slotrank] = saved_rt; |
| 2862 | |
| 2863 | printram("3lanes: %x\n", lanes_ok); |
| 2864 | return lanes_ok != ((1 << NUM_LANES) - 1); |
| 2865 | } |
| 2866 | |
| 2867 | #include "raminit_patterns.h" |
| 2868 | |
| 2869 | static void fill_pattern5(ramctr_timing * ctrl, int channel, int patno) |
| 2870 | { |
| 2871 | unsigned i, j; |
| 2872 | unsigned channel_offset = |
| 2873 | get_precedening_channels(ctrl, channel) * 0x40; |
| 2874 | unsigned channel_step = 0x40 * num_of_channels(ctrl); |
| 2875 | |
| 2876 | if (patno) { |
| 2877 | u8 base8 = 0x80 >> ((patno - 1) % 8); |
| 2878 | u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24); |
| 2879 | for (i = 0; i < 32; i++) { |
| 2880 | for (j = 0; j < 16; j++) { |
| 2881 | u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0; |
| 2882 | if (invert[patno - 1][i] & (1 << (j / 2))) |
| 2883 | val = ~val; |
| 2884 | write32((void *)(0x04000000 + channel_offset + i * channel_step + |
| 2885 | j * 4), val); |
| 2886 | } |
| 2887 | } |
| 2888 | |
| 2889 | } else { |
| 2890 | for (i = 0; i < sizeof(pattern) / sizeof(pattern[0]); i++) { |
| 2891 | for (j = 0; j < 16; j++) |
| 2892 | write32((void *)(0x04000000 + channel_offset + i * channel_step + |
| 2893 | j * 4), pattern[i][j]); |
| 2894 | } |
| 2895 | sfence(); |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | static void reprogram_320c(ramctr_timing * ctrl) |
| 2900 | { |
| 2901 | int channel, slotrank; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2902 | |
| 2903 | FOR_ALL_POPULATED_CHANNELS { |
| 2904 | wait_428c(channel); |
| 2905 | |
| 2906 | /* choose an existing rank. */ |
| 2907 | slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0; |
| 2908 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2909 | /* DRAM command ZQCS */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2910 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003); |
| 2911 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001); |
| 2912 | |
| 2913 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2914 | (slotrank << 24) | 0x60000); |
| 2915 | |
| 2916 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0); |
| 2917 | |
| 2918 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1); |
| 2919 | wait_428c(channel); |
| 2920 | write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel, |
| 2921 | read32(DEFAULT_MCHBAR + 0x4020 + |
| 2922 | 0x400 * channel) | 0x200000); |
| 2923 | } |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2924 | |
| 2925 | /* refresh disable */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2926 | write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8); |
| 2927 | FOR_ALL_POPULATED_CHANNELS { |
| 2928 | wait_428c(channel); |
| 2929 | |
| 2930 | /* choose an existing rank. */ |
| 2931 | slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0; |
| 2932 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 2933 | /* DRAM command ZQCS */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2934 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003); |
| 2935 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001); |
| 2936 | |
| 2937 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 2938 | (slotrank << 24) | 0x60000); |
| 2939 | |
| 2940 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0); |
| 2941 | |
| 2942 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1); |
| 2943 | wait_428c(channel); |
| 2944 | } |
| 2945 | |
| 2946 | /* jedec reset */ |
| 2947 | dram_jedecreset(ctrl); |
| 2948 | /* mrs commands. */ |
| 2949 | dram_mrscommands(ctrl); |
| 2950 | |
Patrick Rudolph | 9b51568 | 2015-10-09 13:43:51 +0200 | [diff] [blame] | 2951 | toggle_io_reset(); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | #define MIN_C320C_LEN 13 |
| 2955 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 2956 | static int try_cmd_stretch(ramctr_timing *ctrl, int cmd_stretch) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 2957 | { |
| 2958 | struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS]; |
| 2959 | int channel, slotrank; |
| 2960 | int c320c; |
| 2961 | int stat[NUM_SLOTRANKS][256]; |
| 2962 | |
| 2963 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
| 2964 | saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank]; |
| 2965 | } |
| 2966 | |
| 2967 | FOR_ALL_POPULATED_CHANNELS { |
| 2968 | ctrl->cmd_stretch[channel] = cmd_stretch; |
| 2969 | } |
| 2970 | |
| 2971 | FOR_ALL_POPULATED_CHANNELS |
| 2972 | MCHBAR32(0x4004 + 0x400 * channel) = |
| 2973 | ctrl->tRRD |
| 2974 | | (ctrl->tRTP << 4) |
| 2975 | | (ctrl->tCKE << 8) |
| 2976 | | (ctrl->tWTR << 12) |
| 2977 | | (ctrl->tFAW << 16) |
| 2978 | | (ctrl->tWR << 24) |
| 2979 | | (ctrl->cmd_stretch[channel] << 30); |
| 2980 | |
| 2981 | |
| 2982 | FOR_ALL_CHANNELS { |
| 2983 | int delta = 0; |
| 2984 | if (ctrl->cmd_stretch[channel] == 2) |
| 2985 | delta = 2; |
| 2986 | else if (ctrl->cmd_stretch[channel] == 0) |
| 2987 | delta = 4; |
| 2988 | |
| 2989 | FOR_ALL_POPULATED_RANKS { |
| 2990 | ctrl->timings[channel][slotrank].val_4024 -= delta; |
| 2991 | } |
| 2992 | } |
| 2993 | |
| 2994 | FOR_ALL_POPULATED_CHANNELS { |
| 2995 | for (c320c = -127; c320c <= 127; c320c++) { |
| 2996 | FOR_ALL_POPULATED_RANKS { |
| 2997 | ctrl->timings[channel][slotrank].val_320c = c320c; |
| 2998 | } |
| 2999 | program_timings(ctrl, channel); |
| 3000 | reprogram_320c(ctrl); |
| 3001 | FOR_ALL_POPULATED_RANKS { |
| 3002 | stat[slotrank][c320c + 127] = |
| 3003 | test_320c(ctrl, channel, slotrank); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3004 | printram("3stat: %d, %d, %d: %x\n", |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3005 | channel, slotrank, c320c, |
| 3006 | stat[slotrank][c320c + 127]); |
| 3007 | } |
| 3008 | } |
| 3009 | FOR_ALL_POPULATED_RANKS { |
| 3010 | struct run rn = |
| 3011 | get_longest_zero_run(stat[slotrank], 255); |
| 3012 | ctrl->timings[channel][slotrank].val_320c = |
| 3013 | rn.middle - 127; |
| 3014 | printram("3val: %d, %d: %d\n", channel, |
| 3015 | slotrank, |
| 3016 | ctrl->timings[channel][slotrank].val_320c); |
| 3017 | if (rn.all || rn.length < MIN_C320C_LEN) { |
| 3018 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
| 3019 | ctrl->timings[channel][slotrank] = saved_timings[channel][slotrank]; |
| 3020 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3021 | return MAKE_ERR; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3022 | } |
| 3023 | } |
| 3024 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3025 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3026 | } |
| 3027 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3028 | /* Adjust CMD phase shift and try multiple command rates. |
| 3029 | * A command rate of 2T doubles the time needed for address and |
| 3030 | * command decode. */ |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3031 | static int command_training(ramctr_timing *ctrl) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3032 | { |
| 3033 | int channel; |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3034 | int err; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3035 | |
| 3036 | FOR_ALL_POPULATED_CHANNELS { |
| 3037 | fill_pattern5(ctrl, channel, 0); |
| 3038 | write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f); |
| 3039 | } |
| 3040 | |
| 3041 | /* try command rate 1T and 2T */ |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3042 | err = try_cmd_stretch(ctrl, 0); |
| 3043 | if (err) { |
| 3044 | err = try_cmd_stretch(ctrl, 2); |
| 3045 | if (err) { |
| 3046 | printk(BIOS_EMERG, "c320c discovery failed\n"); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3047 | return err; |
| 3048 | } |
| 3049 | } |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3050 | |
| 3051 | FOR_ALL_POPULATED_CHANNELS { |
| 3052 | program_timings(ctrl, channel); |
| 3053 | } |
| 3054 | |
| 3055 | reprogram_320c(ctrl); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3056 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3057 | } |
| 3058 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3059 | static int discover_edges_real(ramctr_timing *ctrl, int channel, int slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3060 | int *edges) |
| 3061 | { |
| 3062 | int edge; |
| 3063 | int statistics[NUM_LANES][MAX_EDGE_TIMING + 1]; |
| 3064 | int lane; |
| 3065 | |
| 3066 | for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) { |
| 3067 | FOR_ALL_LANES { |
| 3068 | ctrl->timings[channel][slotrank].lanes[lane].rising = |
| 3069 | edge; |
| 3070 | ctrl->timings[channel][slotrank].lanes[lane].falling = |
| 3071 | edge; |
| 3072 | } |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3073 | program_timings(ctrl, channel); |
| 3074 | |
| 3075 | FOR_ALL_LANES { |
| 3076 | write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel + |
| 3077 | 4 * lane, 0); |
| 3078 | read32(DEFAULT_MCHBAR + 0x400 * channel + 4 * lane + |
| 3079 | 0x4140); |
| 3080 | } |
| 3081 | |
| 3082 | wait_428c(channel); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3083 | /* DRAM command MRS |
| 3084 | * write MR3 MPR enable |
| 3085 | * in this mode only RD and RDA are allowed |
| 3086 | * all reads return a predefined pattern */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3087 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000); |
| 3088 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 3089 | (0xc01 | (ctrl->tMOD << 16))); |
| 3090 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 3091 | (slotrank << 24) | 0x360004); |
| 3092 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0); |
| 3093 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3094 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3095 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105); |
| 3096 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x40411f4); |
| 3097 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 3098 | (slotrank << 24)); |
| 3099 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0); |
| 3100 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3101 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3102 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105); |
| 3103 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, |
| 3104 | 0x1001 | ((ctrl->CAS + 8) << 16)); |
| 3105 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, |
| 3106 | (slotrank << 24) | 0x60000); |
| 3107 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0); |
| 3108 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3109 | /* DRAM command MRS |
| 3110 | * MR3 disable MPR */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3111 | write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000); |
| 3112 | write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, |
| 3113 | (0xc01 | (ctrl->tMOD << 16))); |
| 3114 | write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel, |
| 3115 | (slotrank << 24) | 0x360000); |
| 3116 | write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0); |
| 3117 | |
| 3118 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001); |
| 3119 | |
| 3120 | wait_428c(channel); |
| 3121 | |
| 3122 | FOR_ALL_LANES { |
| 3123 | statistics[lane][edge] = |
| 3124 | read32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel + |
| 3125 | lane * 4); |
| 3126 | } |
| 3127 | } |
| 3128 | FOR_ALL_LANES { |
| 3129 | struct run rn = |
| 3130 | get_longest_zero_run(statistics[lane], MAX_EDGE_TIMING + 1); |
| 3131 | edges[lane] = rn.middle; |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3132 | if (rn.all) { |
| 3133 | printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", |
| 3134 | channel, slotrank, lane); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3135 | return MAKE_ERR; |
| 3136 | } |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3137 | printram("eval %d, %d, %d: %02x\n", channel, slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3138 | lane, edges[lane]); |
| 3139 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3140 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3141 | } |
| 3142 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3143 | static int discover_edges(ramctr_timing *ctrl) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3144 | { |
| 3145 | int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
| 3146 | int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
| 3147 | int channel, slotrank, lane; |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3148 | int err; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3149 | |
| 3150 | write32(DEFAULT_MCHBAR + 0x3400, 0); |
| 3151 | |
Patrick Rudolph | 9b51568 | 2015-10-09 13:43:51 +0200 | [diff] [blame] | 3152 | toggle_io_reset(); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3153 | |
| 3154 | FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES { |
| 3155 | write32(DEFAULT_MCHBAR + 4 * lane + |
| 3156 | 0x400 * channel + 0x4080, 0); |
| 3157 | } |
| 3158 | |
| 3159 | FOR_ALL_POPULATED_CHANNELS { |
| 3160 | fill_pattern0(ctrl, channel, 0, 0); |
| 3161 | write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0); |
| 3162 | FOR_ALL_LANES { |
| 3163 | read32(DEFAULT_MCHBAR + 0x400 * channel + |
| 3164 | lane * 4 + 0x4140); |
| 3165 | } |
| 3166 | |
| 3167 | FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 3168 | ctrl->timings[channel][slotrank].lanes[lane].falling = |
| 3169 | 16; |
| 3170 | ctrl->timings[channel][slotrank].lanes[lane].rising = |
| 3171 | 16; |
| 3172 | } |
| 3173 | |
| 3174 | program_timings(ctrl, channel); |
| 3175 | |
| 3176 | FOR_ALL_POPULATED_RANKS { |
| 3177 | wait_428c(channel); |
| 3178 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3179 | /* DRAM command MRS |
| 3180 | * MR3 enable MPR |
| 3181 | * write MR3 MPR enable |
| 3182 | * in this mode only RD and RDA are allowed |
| 3183 | * all reads return a predefined pattern */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3184 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, |
| 3185 | 0x1f000); |
| 3186 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 3187 | 0xc01 | (ctrl->tMOD << 16)); |
| 3188 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 3189 | (slotrank << 24) | 0x360004); |
| 3190 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0); |
| 3191 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3192 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3193 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, |
| 3194 | 0x1f105); |
| 3195 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, |
| 3196 | 0x4041003); |
| 3197 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 3198 | (slotrank << 24) | 0); |
| 3199 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0); |
| 3200 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3201 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3202 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, |
| 3203 | 0x1f105); |
| 3204 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, |
| 3205 | 0x1001 | ((ctrl->CAS + 8) << 16)); |
| 3206 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, |
| 3207 | (slotrank << 24) | 0x60000); |
| 3208 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0); |
| 3209 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3210 | /* DRAM command MRS |
| 3211 | * MR3 disable MPR */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3212 | write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, |
| 3213 | 0x1f000); |
| 3214 | write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, |
| 3215 | 0xc01 | (ctrl->tMOD << 16)); |
| 3216 | write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel, |
| 3217 | (slotrank << 24) | 0x360000); |
| 3218 | write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0); |
| 3219 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, |
| 3220 | 0xc0001); |
| 3221 | |
| 3222 | wait_428c(channel); |
| 3223 | } |
| 3224 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3225 | /* XXX: check any measured value ? */ |
| 3226 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3227 | FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 3228 | ctrl->timings[channel][slotrank].lanes[lane].falling = |
| 3229 | 48; |
| 3230 | ctrl->timings[channel][slotrank].lanes[lane].rising = |
| 3231 | 48; |
| 3232 | } |
| 3233 | |
| 3234 | program_timings(ctrl, channel); |
| 3235 | |
| 3236 | FOR_ALL_POPULATED_RANKS { |
| 3237 | wait_428c(channel); |
| 3238 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3239 | /* DRAM command MRS |
| 3240 | * MR3 enable MPR |
| 3241 | * write MR3 MPR enable |
| 3242 | * in this mode only RD and RDA are allowed |
| 3243 | * all reads return a predefined pattern */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3244 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, |
| 3245 | 0x1f000); |
| 3246 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 3247 | 0xc01 | (ctrl->tMOD << 16)); |
| 3248 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 3249 | (slotrank << 24) | 0x360004); |
| 3250 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0); |
| 3251 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3252 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3253 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, |
| 3254 | 0x1f105); |
| 3255 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, |
| 3256 | 0x4041003); |
| 3257 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 3258 | (slotrank << 24) | 0); |
| 3259 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0); |
| 3260 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3261 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3262 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, |
| 3263 | 0x1f105); |
| 3264 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, |
| 3265 | 0x1001 | ((ctrl->CAS + 8) << 16)); |
| 3266 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, |
| 3267 | (slotrank << 24) | 0x60000); |
| 3268 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0); |
| 3269 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3270 | /* DRAM command MRS |
| 3271 | * MR3 disable MPR */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3272 | write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, |
| 3273 | 0x1f000); |
| 3274 | write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, |
| 3275 | 0xc01 | (ctrl->tMOD << 16)); |
| 3276 | write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel, |
| 3277 | (slotrank << 24) | 0x360000); |
| 3278 | write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0); |
| 3279 | |
| 3280 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, |
| 3281 | 0xc0001); |
| 3282 | wait_428c(channel); |
| 3283 | } |
| 3284 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3285 | /* XXX: check any measured value ? */ |
| 3286 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3287 | FOR_ALL_LANES { |
| 3288 | write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + |
| 3289 | lane * 4, |
| 3290 | ~read32(DEFAULT_MCHBAR + 0x4040 + |
| 3291 | 0x400 * channel + lane * 4) & 0xff); |
| 3292 | } |
| 3293 | |
| 3294 | fill_pattern0(ctrl, channel, 0, 0xffffffff); |
| 3295 | write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0); |
| 3296 | } |
| 3297 | |
| 3298 | /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */ |
| 3299 | write32(DEFAULT_MCHBAR + 0x4eb0, 0x300); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3300 | printram("discover falling edges:\n[%x] = %x\n", 0x4eb0, 0x300); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3301 | |
| 3302 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3303 | err = discover_edges_real(ctrl, channel, slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3304 | falling_edges[channel][slotrank]); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3305 | if (err) |
| 3306 | return err; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | write32(DEFAULT_MCHBAR + 0x4eb0, 0x200); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3310 | printram("discover rising edges:\n[%x] = %x\n", 0x4eb0, 0x200); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3311 | |
| 3312 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3313 | err = discover_edges_real(ctrl, channel, slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3314 | rising_edges[channel][slotrank]); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3315 | if (err) |
| 3316 | return err; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | write32(DEFAULT_MCHBAR + 0x4eb0, 0); |
| 3320 | |
| 3321 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 3322 | ctrl->timings[channel][slotrank].lanes[lane].falling = |
| 3323 | falling_edges[channel][slotrank][lane]; |
| 3324 | ctrl->timings[channel][slotrank].lanes[lane].rising = |
| 3325 | rising_edges[channel][slotrank][lane]; |
| 3326 | } |
| 3327 | |
| 3328 | FOR_ALL_POPULATED_CHANNELS { |
| 3329 | program_timings(ctrl, channel); |
| 3330 | } |
| 3331 | |
| 3332 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 3333 | write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane, |
| 3334 | 0); |
| 3335 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3336 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3337 | } |
| 3338 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3339 | static int discover_edges_write_real(ramctr_timing *ctrl, int channel, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3340 | int slotrank, int *edges) |
| 3341 | { |
| 3342 | int edge; |
| 3343 | u32 raw_statistics[MAX_EDGE_TIMING + 1]; |
| 3344 | int statistics[MAX_EDGE_TIMING + 1]; |
| 3345 | const int reg3000b24[] = { 0, 0xc, 0x2c }; |
| 3346 | int lane, i; |
| 3347 | int lower[NUM_LANES]; |
| 3348 | int upper[NUM_LANES]; |
| 3349 | int pat; |
| 3350 | |
| 3351 | FOR_ALL_LANES { |
| 3352 | lower[lane] = 0; |
| 3353 | upper[lane] = MAX_EDGE_TIMING; |
| 3354 | } |
| 3355 | |
| 3356 | for (i = 0; i < 3; i++) { |
| 3357 | write32(DEFAULT_MCHBAR + 0x3000 + 0x100 * channel, |
| 3358 | reg3000b24[i] << 24); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3359 | printram("[%x] = 0x%08x\n", |
| 3360 | 0x3000 + 0x100 * channel, reg3000b24[i] << 24); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3361 | for (pat = 0; pat < NUM_PATTERNS; pat++) { |
| 3362 | fill_pattern5(ctrl, channel, pat); |
| 3363 | write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3364 | printram("using pattern %d\n", pat); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3365 | for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) { |
| 3366 | FOR_ALL_LANES { |
| 3367 | ctrl->timings[channel][slotrank].lanes[lane]. |
| 3368 | rising = edge; |
| 3369 | ctrl->timings[channel][slotrank].lanes[lane]. |
| 3370 | falling = edge; |
| 3371 | } |
| 3372 | program_timings(ctrl, channel); |
| 3373 | |
| 3374 | FOR_ALL_LANES { |
| 3375 | write32(DEFAULT_MCHBAR + 0x4340 + |
| 3376 | 0x400 * channel + 4 * lane, 0); |
| 3377 | read32(DEFAULT_MCHBAR + 0x400 * channel + |
| 3378 | 4 * lane + 0x4140); |
| 3379 | } |
| 3380 | wait_428c(channel); |
| 3381 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3382 | /* DRAM command ACT */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3383 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, |
| 3384 | 0x1f006); |
| 3385 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 3386 | 0x4 | (ctrl->tRCD << 16) |
| 3387 | | (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) << |
| 3388 | 10)); |
| 3389 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 3390 | (slotrank << 24) | 0x60000); |
| 3391 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, |
| 3392 | 0x240); |
| 3393 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3394 | /* DRAM command WR */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3395 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, |
| 3396 | 0x1f201); |
| 3397 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, |
| 3398 | 0x8005020 | ((ctrl->tWTR + ctrl->CWL + 8) << |
| 3399 | 16)); |
| 3400 | write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, |
| 3401 | (slotrank << 24)); |
| 3402 | write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, |
| 3403 | 0x242); |
| 3404 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3405 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3406 | write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, |
| 3407 | 0x1f105); |
| 3408 | write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, |
| 3409 | 0x4005020 | (max(ctrl->tRTP, 8) << 16)); |
| 3410 | write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, |
| 3411 | (slotrank << 24)); |
| 3412 | write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, |
| 3413 | 0x242); |
| 3414 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3415 | /* DRAM command PRE */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3416 | write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, |
| 3417 | 0x1f002); |
| 3418 | write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, |
| 3419 | 0xc01 | (ctrl->tRP << 16)); |
| 3420 | write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel, |
| 3421 | (slotrank << 24) | 0x60400); |
| 3422 | write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0); |
| 3423 | |
| 3424 | write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, |
| 3425 | 0xc0001); |
| 3426 | wait_428c(channel); |
| 3427 | FOR_ALL_LANES { |
| 3428 | read32(DEFAULT_MCHBAR + 0x4340 + |
| 3429 | 0x400 * channel + lane * 4); |
| 3430 | } |
| 3431 | |
| 3432 | raw_statistics[edge] = |
| 3433 | MCHBAR32(0x436c + 0x400 * channel); |
| 3434 | } |
| 3435 | FOR_ALL_LANES { |
| 3436 | struct run rn; |
| 3437 | for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) |
| 3438 | statistics[edge] = |
| 3439 | ! !(raw_statistics[edge] & (1 << lane)); |
| 3440 | rn = get_longest_zero_run(statistics, |
| 3441 | MAX_EDGE_TIMING + 1); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3442 | printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n", |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3443 | channel, slotrank, i, rn.start, rn.middle, |
| 3444 | rn.end, rn.start + ctrl->edge_offset[i], |
| 3445 | rn.end - ctrl->edge_offset[i]); |
| 3446 | lower[lane] = |
| 3447 | max(rn.start + ctrl->edge_offset[i], lower[lane]); |
| 3448 | upper[lane] = |
| 3449 | min(rn.end - ctrl->edge_offset[i], upper[lane]); |
| 3450 | edges[lane] = (lower[lane] + upper[lane]) / 2; |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3451 | if (rn.all || (lower[lane] > upper[lane])) { |
| 3452 | printk(BIOS_EMERG, "edge write discovery failed: %d, %d, %d\n", |
| 3453 | channel, slotrank, lane); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3454 | return MAKE_ERR; |
| 3455 | } |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3456 | } |
| 3457 | } |
| 3458 | } |
| 3459 | |
| 3460 | write32(DEFAULT_MCHBAR + 0x3000, 0); |
| 3461 | printram("CPA\n"); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3462 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3463 | } |
| 3464 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3465 | static int discover_edges_write(ramctr_timing *ctrl) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3466 | { |
| 3467 | int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
| 3468 | int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
| 3469 | int channel, slotrank, lane; |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3470 | int err; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3471 | |
| 3472 | /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */ |
| 3473 | write32(DEFAULT_MCHBAR + 0x4eb0, 0x300); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3474 | printram("discover falling edges write:\n[%x] = %x\n", 0x4eb0, 0x300); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3475 | |
| 3476 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3477 | err = discover_edges_write_real(ctrl, channel, slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3478 | falling_edges[channel][slotrank]); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3479 | if (err) |
| 3480 | return err; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | write32(DEFAULT_MCHBAR + 0x4eb0, 0x200); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3484 | printram("discover rising edges write:\n[%x] = %x\n", 0x4eb0, 0x200); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3485 | |
| 3486 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3487 | err = discover_edges_write_real(ctrl, channel, slotrank, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3488 | rising_edges[channel][slotrank]); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3489 | if (err) |
| 3490 | return err; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | write32(DEFAULT_MCHBAR + 0x4eb0, 0); |
| 3494 | |
| 3495 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 3496 | ctrl->timings[channel][slotrank].lanes[lane].falling = |
| 3497 | falling_edges[channel][slotrank][lane]; |
| 3498 | ctrl->timings[channel][slotrank].lanes[lane].rising = |
| 3499 | rising_edges[channel][slotrank][lane]; |
| 3500 | } |
| 3501 | |
| 3502 | FOR_ALL_POPULATED_CHANNELS |
| 3503 | program_timings(ctrl, channel); |
| 3504 | |
| 3505 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 3506 | write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane, |
| 3507 | 0); |
| 3508 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3509 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank) |
| 3513 | { |
| 3514 | wait_428c(channel); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3515 | /* DRAM command ACT */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3516 | write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006); |
| 3517 | write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, |
| 3518 | (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD) |
| 3519 | << 10) | (ctrl->tRCD << 16) | 4); |
| 3520 | write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, |
| 3521 | (slotrank << 24) | 0x60000); |
| 3522 | write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244); |
| 3523 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3524 | /* DRAM command WR */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3525 | write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201); |
| 3526 | write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, |
| 3527 | 0x80011e0 | |
| 3528 | ((ctrl->tWTR + ctrl->CWL + 8) << 16)); |
| 3529 | write32(DEFAULT_MCHBAR + 0x4204 + |
| 3530 | 0x400 * channel, (slotrank << 24)); |
| 3531 | write32(DEFAULT_MCHBAR + 0x4214 + |
| 3532 | 0x400 * channel, 0x242); |
| 3533 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3534 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3535 | write32(DEFAULT_MCHBAR + 0x4228 + |
| 3536 | 0x400 * channel, 0x1f105); |
| 3537 | write32(DEFAULT_MCHBAR + 0x4238 + |
| 3538 | 0x400 * channel, |
| 3539 | 0x40011e0 | (max(ctrl->tRTP, 8) << 16)); |
| 3540 | write32(DEFAULT_MCHBAR + 0x4208 + |
| 3541 | 0x400 * channel, (slotrank << 24)); |
| 3542 | write32(DEFAULT_MCHBAR + 0x4218 + |
| 3543 | 0x400 * channel, 0x242); |
| 3544 | |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3545 | /* DRAM command PRE */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3546 | write32(DEFAULT_MCHBAR + 0x422c + |
| 3547 | 0x400 * channel, 0x1f002); |
| 3548 | write32(DEFAULT_MCHBAR + 0x423c + |
| 3549 | 0x400 * channel, |
| 3550 | 0x1001 | (ctrl->tRP << 16)); |
| 3551 | write32(DEFAULT_MCHBAR + 0x420c + |
| 3552 | 0x400 * channel, |
| 3553 | (slotrank << 24) | 0x60400); |
| 3554 | write32(DEFAULT_MCHBAR + 0x421c + |
| 3555 | 0x400 * channel, 0); |
| 3556 | |
| 3557 | write32(DEFAULT_MCHBAR + 0x4284 + |
| 3558 | 0x400 * channel, 0xc0001); |
| 3559 | wait_428c(channel); |
| 3560 | } |
| 3561 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3562 | static int discover_timC_write(ramctr_timing *ctrl) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3563 | { |
| 3564 | const u8 rege3c_b24[3] = { 0, 0xf, 0x2f }; |
| 3565 | int i, pat; |
| 3566 | |
| 3567 | int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
| 3568 | int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES]; |
| 3569 | int channel, slotrank, lane; |
| 3570 | |
| 3571 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 3572 | lower[channel][slotrank][lane] = 0; |
| 3573 | upper[channel][slotrank][lane] = MAX_TIMC; |
| 3574 | } |
| 3575 | |
| 3576 | write32(DEFAULT_MCHBAR + 0x4ea8, 1); |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3577 | printram("discover timC write:\n"); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3578 | |
| 3579 | for (i = 0; i < 3; i++) |
| 3580 | FOR_ALL_POPULATED_CHANNELS { |
| 3581 | write32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100), |
| 3582 | (rege3c_b24[i] << 24) |
| 3583 | | (read32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100)) |
| 3584 | & ~0x3f000000)); |
| 3585 | udelay(2); |
| 3586 | for (pat = 0; pat < NUM_PATTERNS; pat++) { |
| 3587 | FOR_ALL_POPULATED_RANKS { |
| 3588 | int timC; |
| 3589 | u32 raw_statistics[MAX_TIMC + 1]; |
| 3590 | int statistics[MAX_TIMC + 1]; |
| 3591 | |
Patrick Rudolph | 77e45d3 | 2016-02-28 16:14:45 +0100 | [diff] [blame] | 3592 | /* Make sure rn.start < rn.end */ |
| 3593 | statistics[MAX_TIMC] = 1; |
| 3594 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3595 | fill_pattern5(ctrl, channel, pat); |
| 3596 | write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f); |
Patrick Rudolph | 77e45d3 | 2016-02-28 16:14:45 +0100 | [diff] [blame] | 3597 | for (timC = 0; timC < MAX_TIMC; timC++) { |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3598 | FOR_ALL_LANES |
| 3599 | ctrl->timings[channel][slotrank].lanes[lane].timC = timC; |
| 3600 | program_timings(ctrl, channel); |
| 3601 | |
| 3602 | test_timC_write (ctrl, channel, slotrank); |
| 3603 | |
| 3604 | raw_statistics[timC] = |
| 3605 | MCHBAR32(0x436c + 0x400 * channel); |
| 3606 | } |
| 3607 | FOR_ALL_LANES { |
| 3608 | struct run rn; |
Patrick Rudolph | 77e45d3 | 2016-02-28 16:14:45 +0100 | [diff] [blame] | 3609 | for (timC = 0; timC < MAX_TIMC; timC++) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3610 | statistics[timC] = |
| 3611 | !!(raw_statistics[timC] & |
| 3612 | (1 << lane)); |
Patrick Rudolph | 77e45d3 | 2016-02-28 16:14:45 +0100 | [diff] [blame] | 3613 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3614 | rn = get_longest_zero_run(statistics, |
| 3615 | MAX_TIMC + 1); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3616 | if (rn.all) { |
| 3617 | printk(BIOS_EMERG, "timC write discovery failed: %d, %d, %d\n", |
| 3618 | channel, slotrank, lane); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3619 | return MAKE_ERR; |
| 3620 | } |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3621 | printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n", |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3622 | channel, slotrank, i, rn.start, |
| 3623 | rn.middle, rn.end, |
| 3624 | rn.start + ctrl->timC_offset[i], |
| 3625 | rn.end - ctrl->timC_offset[i]); |
| 3626 | lower[channel][slotrank][lane] = |
| 3627 | max(rn.start + ctrl->timC_offset[i], |
| 3628 | lower[channel][slotrank][lane]); |
| 3629 | upper[channel][slotrank][lane] = |
| 3630 | min(rn.end - ctrl->timC_offset[i], |
| 3631 | upper[channel][slotrank][lane]); |
| 3632 | |
| 3633 | } |
| 3634 | } |
| 3635 | } |
| 3636 | } |
| 3637 | |
| 3638 | FOR_ALL_CHANNELS { |
| 3639 | write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c, |
| 3640 | 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) & |
| 3641 | ~0x3f000000)); |
| 3642 | udelay(2); |
| 3643 | } |
| 3644 | |
| 3645 | write32(DEFAULT_MCHBAR + 0x4ea8, 0); |
| 3646 | |
| 3647 | printram("CPB\n"); |
| 3648 | |
| 3649 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
Patrick Rudolph | a649a54 | 2016-01-17 18:32:06 +0100 | [diff] [blame] | 3650 | printram("timC %d, %d, %d: %x\n", channel, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3651 | slotrank, lane, |
| 3652 | (lower[channel][slotrank][lane] + |
| 3653 | upper[channel][slotrank][lane]) / 2); |
| 3654 | ctrl->timings[channel][slotrank].lanes[lane].timC = |
| 3655 | (lower[channel][slotrank][lane] + |
| 3656 | upper[channel][slotrank][lane]) / 2; |
| 3657 | } |
| 3658 | FOR_ALL_POPULATED_CHANNELS { |
| 3659 | program_timings(ctrl, channel); |
| 3660 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3661 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | static void normalize_training(ramctr_timing * ctrl) |
| 3665 | { |
| 3666 | int channel, slotrank, lane; |
| 3667 | int mat = 0; |
| 3668 | |
| 3669 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
| 3670 | int delta; |
| 3671 | FOR_ALL_LANES mat = |
| 3672 | max(ctrl->timings[channel][slotrank].lanes[lane].timA, mat); |
| 3673 | delta = (mat >> 6) - ctrl->timings[channel][slotrank].val_4028; |
| 3674 | ctrl->timings[channel][slotrank].val_4024 += delta; |
| 3675 | ctrl->timings[channel][slotrank].val_4028 += delta; |
| 3676 | } |
| 3677 | |
| 3678 | FOR_ALL_POPULATED_CHANNELS { |
| 3679 | program_timings(ctrl, channel); |
| 3680 | } |
| 3681 | } |
| 3682 | |
| 3683 | static void write_controller_mr(ramctr_timing * ctrl) |
| 3684 | { |
| 3685 | int channel, slotrank; |
| 3686 | |
| 3687 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS { |
| 3688 | write32(DEFAULT_MCHBAR + 0x0004 + (channel << 8) + |
| 3689 | lane_registers[slotrank], make_mr0(ctrl, slotrank)); |
| 3690 | write32(DEFAULT_MCHBAR + 0x0008 + (channel << 8) + |
Patrick Rudolph | 7e513d1 | 2016-01-10 14:22:34 +0100 | [diff] [blame] | 3691 | lane_registers[slotrank], |
| 3692 | make_mr1(ctrl, slotrank, channel)); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3693 | } |
| 3694 | } |
| 3695 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3696 | static int channel_test(ramctr_timing *ctrl) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3697 | { |
| 3698 | int channel, slotrank, lane; |
| 3699 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3700 | slotrank = 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3701 | FOR_ALL_POPULATED_CHANNELS |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3702 | if (read32(DEFAULT_MCHBAR + 0x42a0 + (channel << 10)) & 0xa000) { |
| 3703 | printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", |
| 3704 | channel); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3705 | return MAKE_ERR; |
| 3706 | } |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3707 | FOR_ALL_POPULATED_CHANNELS { |
| 3708 | fill_pattern0(ctrl, channel, 0x12345678, 0x98765432); |
| 3709 | |
| 3710 | write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0); |
| 3711 | } |
| 3712 | |
| 3713 | for (slotrank = 0; slotrank < 4; slotrank++) |
| 3714 | FOR_ALL_CHANNELS |
| 3715 | if (ctrl->rankmap[channel] & (1 << slotrank)) { |
| 3716 | FOR_ALL_LANES { |
| 3717 | write32(DEFAULT_MCHBAR + (0x4f40 + 4 * lane), 0); |
| 3718 | write32(DEFAULT_MCHBAR + (0x4d40 + 4 * lane), 0); |
| 3719 | } |
| 3720 | wait_428c(channel); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3721 | /* DRAM command ACT */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3722 | write32(DEFAULT_MCHBAR + 0x4220 + (channel << 10), 0x0001f006); |
| 3723 | write32(DEFAULT_MCHBAR + 0x4230 + (channel << 10), 0x0028a004); |
| 3724 | write32(DEFAULT_MCHBAR + 0x4200 + (channel << 10), |
| 3725 | 0x00060000 | (slotrank << 24)); |
| 3726 | write32(DEFAULT_MCHBAR + 0x4210 + (channel << 10), 0x00000244); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3727 | /* DRAM command WR */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3728 | write32(DEFAULT_MCHBAR + 0x4224 + (channel << 10), 0x0001f201); |
| 3729 | write32(DEFAULT_MCHBAR + 0x4234 + (channel << 10), 0x08281064); |
| 3730 | write32(DEFAULT_MCHBAR + 0x4204 + (channel << 10), |
| 3731 | 0x00000000 | (slotrank << 24)); |
| 3732 | write32(DEFAULT_MCHBAR + 0x4214 + (channel << 10), 0x00000242); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3733 | /* DRAM command RD */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3734 | write32(DEFAULT_MCHBAR + 0x4228 + (channel << 10), 0x0001f105); |
| 3735 | write32(DEFAULT_MCHBAR + 0x4238 + (channel << 10), 0x04281064); |
| 3736 | write32(DEFAULT_MCHBAR + 0x4208 + (channel << 10), |
| 3737 | 0x00000000 | (slotrank << 24)); |
| 3738 | write32(DEFAULT_MCHBAR + 0x4218 + (channel << 10), 0x00000242); |
Patrick Rudolph | 371d291 | 2015-10-09 13:33:25 +0200 | [diff] [blame] | 3739 | /* DRAM command PRE */ |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3740 | write32(DEFAULT_MCHBAR + 0x422c + (channel << 10), 0x0001f002); |
| 3741 | write32(DEFAULT_MCHBAR + 0x423c + (channel << 10), 0x00280c01); |
| 3742 | write32(DEFAULT_MCHBAR + 0x420c + (channel << 10), |
| 3743 | 0x00060400 | (slotrank << 24)); |
| 3744 | write32(DEFAULT_MCHBAR + 0x421c + (channel << 10), 0x00000240); |
| 3745 | write32(DEFAULT_MCHBAR + 0x4284 + (channel << 10), 0x000c0001); |
| 3746 | wait_428c(channel); |
| 3747 | FOR_ALL_LANES |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3748 | if (read32(DEFAULT_MCHBAR + 0x4340 + (channel << 10) + 4 * lane)) { |
| 3749 | printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n", |
| 3750 | channel, slotrank, lane); |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3751 | return MAKE_ERR; |
| 3752 | } |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3753 | } |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 3754 | return 0; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | static void set_scrambling_seed(ramctr_timing * ctrl) |
| 3758 | { |
| 3759 | int channel; |
| 3760 | |
| 3761 | /* FIXME: we hardcode seeds. Do we need to use some PRNG for them? |
| 3762 | I don't think so. */ |
| 3763 | static u32 seeds[NUM_CHANNELS][3] = { |
| 3764 | {0x00009a36, 0xbafcfdcf, 0x46d1ab68}, |
| 3765 | {0x00028bfa, 0x53fe4b49, 0x19ed5483} |
| 3766 | }; |
| 3767 | FOR_ALL_POPULATED_CHANNELS { |
| 3768 | MCHBAR32(0x4020 + 0x400 * channel) &= ~0x10000000; |
| 3769 | write32(DEFAULT_MCHBAR + 0x4034, seeds[channel][0]); |
| 3770 | write32(DEFAULT_MCHBAR + 0x403c, seeds[channel][1]); |
| 3771 | write32(DEFAULT_MCHBAR + 0x4038, seeds[channel][2]); |
| 3772 | } |
| 3773 | } |
| 3774 | |
| 3775 | static void set_4f8c(void) |
| 3776 | { |
| 3777 | struct cpuid_result cpures; |
| 3778 | u32 cpu; |
| 3779 | |
| 3780 | cpures = cpuid(0); |
| 3781 | cpu = (cpures.eax); |
| 3782 | if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) { |
| 3783 | MCHBAR32(0x4f8c) = 0x141D1519; |
| 3784 | } else { |
| 3785 | MCHBAR32(0x4f8c) = 0x551D1519; |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | static void prepare_training(ramctr_timing * ctrl) |
| 3790 | { |
| 3791 | int channel; |
| 3792 | |
| 3793 | FOR_ALL_POPULATED_CHANNELS { |
| 3794 | // Always drive command bus |
| 3795 | MCHBAR32(0x4004 + 0x400 * channel) |= 0x20000000; |
| 3796 | } |
| 3797 | |
| 3798 | udelay(1); |
| 3799 | |
| 3800 | FOR_ALL_POPULATED_CHANNELS { |
| 3801 | wait_428c(channel); |
| 3802 | } |
| 3803 | } |
| 3804 | |
| 3805 | static void set_4008c(ramctr_timing * ctrl) |
| 3806 | { |
| 3807 | int channel, slotrank; |
| 3808 | u32 reg; |
| 3809 | FOR_ALL_POPULATED_CHANNELS { |
| 3810 | u32 b20, b4_8_12; |
| 3811 | int min_320c = 10000; |
| 3812 | int max_320c = -10000; |
| 3813 | |
| 3814 | FOR_ALL_POPULATED_RANKS { |
| 3815 | max_320c = max(ctrl->timings[channel][slotrank].val_320c, max_320c); |
| 3816 | min_320c = min(ctrl->timings[channel][slotrank].val_320c, min_320c); |
| 3817 | } |
| 3818 | |
| 3819 | if (max_320c - min_320c > 51) |
| 3820 | b20 = 0; |
| 3821 | else |
| 3822 | b20 = ctrl->ref_card_offset[channel]; |
| 3823 | |
| 3824 | if (ctrl->reg_320c_range_threshold < max_320c - min_320c) |
| 3825 | b4_8_12 = 0x3330; |
| 3826 | else |
| 3827 | b4_8_12 = 0x2220; |
| 3828 | |
| 3829 | reg = read32(DEFAULT_MCHBAR + 0x400c + (channel << 10)); |
| 3830 | write32(DEFAULT_MCHBAR + 0x400c + (channel << 10), |
| 3831 | (reg & 0xFFF0FFFF) |
| 3832 | | (ctrl->ref_card_offset[channel] << 16) |
| 3833 | | (ctrl->ref_card_offset[channel] << 18)); |
| 3834 | write32(DEFAULT_MCHBAR + 0x4008 + (channel << 10), |
| 3835 | 0x0a000000 |
| 3836 | | (b20 << 20) |
| 3837 | | ((ctrl->ref_card_offset[channel] + 2) << 16) |
| 3838 | | b4_8_12); |
| 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | static void set_42a0(ramctr_timing * ctrl) |
| 3843 | { |
| 3844 | int channel; |
| 3845 | FOR_ALL_POPULATED_CHANNELS { |
| 3846 | write32(DEFAULT_MCHBAR + (0x42a0 + 0x400 * channel), |
| 3847 | 0x00001000 | ctrl->rankmap[channel]); |
| 3848 | MCHBAR32(0x4004 + 0x400 * channel) &= ~0x20000000; // OK |
| 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | static int encode_5d10(int ns) |
| 3853 | { |
| 3854 | return (ns + 499) / 500; |
| 3855 | } |
| 3856 | |
| 3857 | /* FIXME: values in this function should be hardware revision-dependent. */ |
| 3858 | static void final_registers(ramctr_timing * ctrl) |
| 3859 | { |
| 3860 | int channel; |
| 3861 | int t1_cycles = 0, t1_ns = 0, t2_ns; |
| 3862 | int t3_ns; |
| 3863 | u32 r32; |
| 3864 | |
| 3865 | write32(DEFAULT_MCHBAR + 0x4cd4, 0x00000046); |
| 3866 | |
| 3867 | write32(DEFAULT_MCHBAR + 0x400c, (read32(DEFAULT_MCHBAR + 0x400c) & 0xFFFFCFFF) | 0x1000); // OK |
| 3868 | write32(DEFAULT_MCHBAR + 0x440c, (read32(DEFAULT_MCHBAR + 0x440c) & 0xFFFFCFFF) | 0x1000); // OK |
| 3869 | write32(DEFAULT_MCHBAR + 0x4cb0, 0x00000740); |
| 3870 | write32(DEFAULT_MCHBAR + 0x4380, 0x00000aaa); // OK |
| 3871 | write32(DEFAULT_MCHBAR + 0x4780, 0x00000aaa); // OK |
| 3872 | write32(DEFAULT_MCHBAR + 0x4f88, 0x5f7003ff); // OK |
| 3873 | write32(DEFAULT_MCHBAR + 0x5064, 0x00073000 | ctrl->reg_5064b0); // OK |
| 3874 | |
| 3875 | FOR_ALL_CHANNELS { |
| 3876 | switch (ctrl->rankmap[channel]) { |
| 3877 | /* Unpopulated channel. */ |
| 3878 | case 0: |
| 3879 | write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0); |
| 3880 | break; |
| 3881 | /* Only single-ranked dimms. */ |
| 3882 | case 1: |
| 3883 | case 4: |
| 3884 | case 5: |
| 3885 | write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x373131); |
| 3886 | break; |
| 3887 | /* Dual-ranked dimms present. */ |
| 3888 | default: |
| 3889 | write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x9b6ea1); |
| 3890 | break; |
| 3891 | } |
| 3892 | } |
| 3893 | |
| 3894 | write32 (DEFAULT_MCHBAR + 0x5880, 0xca9171e5); |
| 3895 | write32 (DEFAULT_MCHBAR + 0x5888, |
| 3896 | (read32 (DEFAULT_MCHBAR + 0x5888) & ~0xffffff) | 0xe4d5d0); |
| 3897 | write32 (DEFAULT_MCHBAR + 0x58a8, read32 (DEFAULT_MCHBAR + 0x58a8) & ~0x1f); |
| 3898 | write32 (DEFAULT_MCHBAR + 0x4294, |
| 3899 | (read32 (DEFAULT_MCHBAR + 0x4294) & ~0x30000) |
| 3900 | | (1 << 16)); |
| 3901 | write32 (DEFAULT_MCHBAR + 0x4694, |
| 3902 | (read32 (DEFAULT_MCHBAR + 0x4694) & ~0x30000) |
| 3903 | | (1 << 16)); |
| 3904 | |
| 3905 | MCHBAR32(0x5030) |= 1; // OK |
| 3906 | MCHBAR32(0x5030) |= 0x80; // OK |
| 3907 | MCHBAR32(0x5f18) = 0xfa; // OK |
| 3908 | |
| 3909 | /* Find a populated channel. */ |
| 3910 | FOR_ALL_POPULATED_CHANNELS |
| 3911 | break; |
| 3912 | |
| 3913 | t1_cycles = ((read32(DEFAULT_MCHBAR + 0x4290 + channel * 0x400) >> 8) & 0xff); |
| 3914 | r32 = read32(DEFAULT_MCHBAR + 0x5064); |
| 3915 | if (r32 & 0x20000) |
| 3916 | t1_cycles += (r32 & 0xfff); |
| 3917 | t1_cycles += (read32(DEFAULT_MCHBAR + channel * 0x400 + 0x42a4) & 0xfff); |
| 3918 | t1_ns = t1_cycles * ctrl->tCK / 256 + 544; |
| 3919 | if (!(r32 & 0x20000)) |
| 3920 | t1_ns += 500; |
| 3921 | |
| 3922 | t2_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f10) >> 8) & 0xfff); |
| 3923 | if ( read32(DEFAULT_MCHBAR + 0x5f00) & 8 ) |
| 3924 | { |
| 3925 | t3_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f20) >> 8) & 0xfff); |
| 3926 | t3_ns += 10 * (read32(DEFAULT_MCHBAR + 0x5f18) & 0xff); |
| 3927 | } |
| 3928 | else |
| 3929 | { |
| 3930 | t3_ns = 500; |
| 3931 | } |
| 3932 | printk(BIOS_DEBUG, "t123: %d, %d, %d\n", |
| 3933 | t1_ns, t2_ns, t3_ns); |
| 3934 | write32 (DEFAULT_MCHBAR + 0x5d10, |
| 3935 | ((encode_5d10(t1_ns) + encode_5d10(t2_ns)) << 16) |
| 3936 | | (encode_5d10(t1_ns) << 8) |
| 3937 | | ((encode_5d10(t3_ns) + encode_5d10(t2_ns) + encode_5d10(t1_ns)) << 24) |
| 3938 | | (read32(DEFAULT_MCHBAR + 0x5d10) & 0xC0C0C0C0) |
| 3939 | | 0xc); |
| 3940 | } |
| 3941 | |
Patrick Rudolph | bb9c90a | 2016-05-29 17:05:06 +0200 | [diff] [blame] | 3942 | static void save_timings(ramctr_timing *ctrl) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3943 | { |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3944 | /* Save the MRC S3 restore data to cbmem */ |
Patrick Rudolph | bb9c90a | 2016-05-29 17:05:06 +0200 | [diff] [blame] | 3945 | store_current_mrc_cache(ctrl, sizeof(*ctrl)); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | static void restore_timings(ramctr_timing * ctrl) |
| 3949 | { |
| 3950 | int channel, slotrank, lane; |
| 3951 | |
| 3952 | FOR_ALL_POPULATED_CHANNELS |
| 3953 | MCHBAR32(0x4004 + 0x400 * channel) = |
| 3954 | ctrl->tRRD |
| 3955 | | (ctrl->tRTP << 4) |
| 3956 | | (ctrl->tCKE << 8) |
| 3957 | | (ctrl->tWTR << 12) |
| 3958 | | (ctrl->tFAW << 16) |
| 3959 | | (ctrl->tWR << 24) |
| 3960 | | (ctrl->cmd_stretch[channel] << 30); |
| 3961 | |
| 3962 | udelay(1); |
| 3963 | |
| 3964 | FOR_ALL_POPULATED_CHANNELS { |
| 3965 | wait_428c(channel); |
| 3966 | } |
| 3967 | |
| 3968 | FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES { |
| 3969 | write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel |
| 3970 | + 4 * lane, 0); |
| 3971 | } |
| 3972 | |
| 3973 | FOR_ALL_POPULATED_CHANNELS |
| 3974 | write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel, |
| 3975 | read32(DEFAULT_MCHBAR + 0x4008 + |
| 3976 | 0x400 * channel) | 0x8000000); |
| 3977 | |
| 3978 | FOR_ALL_POPULATED_CHANNELS { |
| 3979 | udelay (1); |
| 3980 | write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel, |
| 3981 | read32(DEFAULT_MCHBAR + 0x4020 + |
| 3982 | 0x400 * channel) | 0x200000); |
| 3983 | } |
| 3984 | |
| 3985 | printram("CPE\n"); |
| 3986 | |
| 3987 | write32(DEFAULT_MCHBAR + 0x3400, 0); |
| 3988 | write32(DEFAULT_MCHBAR + 0x4eb0, 0); |
| 3989 | |
| 3990 | printram("CP5b\n"); |
| 3991 | |
| 3992 | FOR_ALL_POPULATED_CHANNELS { |
| 3993 | program_timings(ctrl, channel); |
| 3994 | } |
| 3995 | |
| 3996 | u32 reg, addr; |
| 3997 | |
| 3998 | while (!(MCHBAR32(0x5084) & 0x10000)) ; |
| 3999 | do { |
| 4000 | reg = MCHBAR32(0x428c); |
| 4001 | } while ((reg & 0x14) == 0); |
| 4002 | |
| 4003 | // Set state of memory controller |
| 4004 | MCHBAR32(0x5030) = 0x116; |
| 4005 | MCHBAR32(0x4ea0) = 0; |
| 4006 | |
| 4007 | // Wait 500us |
| 4008 | udelay(500); |
| 4009 | |
| 4010 | FOR_ALL_CHANNELS { |
| 4011 | // Set valid rank CKE |
| 4012 | reg = 0; |
| 4013 | reg = (reg & ~0xf) | ctrl->rankmap[channel]; |
| 4014 | addr = 0x400 * channel + 0x42a0; |
| 4015 | MCHBAR32(addr) = reg; |
| 4016 | |
| 4017 | // Wait 10ns for ranks to settle |
| 4018 | //udelay(0.01); |
| 4019 | |
| 4020 | reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4); |
| 4021 | MCHBAR32(addr) = reg; |
| 4022 | |
| 4023 | // Write reset using a NOP |
| 4024 | write_reset(ctrl); |
| 4025 | } |
| 4026 | |
| 4027 | /* mrs commands. */ |
| 4028 | dram_mrscommands(ctrl); |
| 4029 | |
| 4030 | printram("CP5c\n"); |
| 4031 | |
| 4032 | write32(DEFAULT_MCHBAR + 0x3000, 0); |
| 4033 | |
| 4034 | FOR_ALL_CHANNELS { |
| 4035 | write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c, |
| 4036 | 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) & |
| 4037 | ~0x3f000000)); |
| 4038 | udelay(2); |
| 4039 | } |
| 4040 | |
| 4041 | write32(DEFAULT_MCHBAR + 0x4ea8, 0); |
| 4042 | } |
| 4043 | |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4044 | static int try_init_dram_ddr3(ramctr_timing *ctrl, int fast_boot, |
Patrick Rudolph | 588ccaa | 2016-04-20 18:00:27 +0200 | [diff] [blame] | 4045 | int s3_resume, int me_uma_size) |
Patrick Rudolph | 27e085a | 2016-03-26 10:59:02 +0100 | [diff] [blame] | 4046 | { |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 4047 | int err; |
| 4048 | |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4049 | printk(BIOS_DEBUG, "Starting RAM training (%d).\n", fast_boot); |
| 4050 | |
| 4051 | if (!fast_boot) { |
Patrick Rudolph | 27e085a | 2016-03-26 10:59:02 +0100 | [diff] [blame] | 4052 | /* Find fastest common supported parameters */ |
| 4053 | dram_find_common_params(ctrl); |
| 4054 | |
| 4055 | dram_dimm_mapping(ctrl); |
| 4056 | } |
| 4057 | |
| 4058 | /* Set MCU frequency */ |
| 4059 | dram_freq(ctrl); |
| 4060 | |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4061 | if (!fast_boot) { |
Patrick Rudolph | 27e085a | 2016-03-26 10:59:02 +0100 | [diff] [blame] | 4062 | /* Calculate timings */ |
| 4063 | dram_timing(ctrl); |
| 4064 | } |
| 4065 | |
| 4066 | /* Set version register */ |
| 4067 | MCHBAR32(0x5034) = 0xC04EB002; |
| 4068 | |
| 4069 | /* Enable crossover */ |
| 4070 | dram_xover(ctrl); |
| 4071 | |
| 4072 | /* Set timing and refresh registers */ |
| 4073 | dram_timing_regs(ctrl); |
| 4074 | |
| 4075 | /* Power mode preset */ |
| 4076 | MCHBAR32(0x4e80) = 0x5500; |
| 4077 | |
| 4078 | /* Set scheduler parameters */ |
| 4079 | MCHBAR32(0x4c20) = 0x10100005; |
| 4080 | |
| 4081 | /* Set cpu specific register */ |
| 4082 | set_4f8c(); |
| 4083 | |
| 4084 | /* Clear IO reset bit */ |
| 4085 | MCHBAR32(0x5030) &= ~0x20; |
| 4086 | |
| 4087 | /* Set MAD-DIMM registers */ |
| 4088 | dram_dimm_set_mapping(ctrl); |
| 4089 | printk(BIOS_DEBUG, "Done dimm mapping\n"); |
| 4090 | |
| 4091 | /* Zone config */ |
| 4092 | dram_zones(ctrl, 1); |
| 4093 | |
| 4094 | /* Set memory map */ |
| 4095 | dram_memorymap(ctrl, me_uma_size); |
| 4096 | printk(BIOS_DEBUG, "Done memory map\n"); |
| 4097 | |
| 4098 | /* Set IO registers */ |
| 4099 | dram_ioregs(ctrl); |
| 4100 | printk(BIOS_DEBUG, "Done io registers\n"); |
| 4101 | |
| 4102 | udelay(1); |
| 4103 | |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4104 | if (fast_boot) { |
Patrick Rudolph | 27e085a | 2016-03-26 10:59:02 +0100 | [diff] [blame] | 4105 | restore_timings(ctrl); |
| 4106 | } else { |
| 4107 | /* Do jedec ddr3 reset sequence */ |
| 4108 | dram_jedecreset(ctrl); |
| 4109 | printk(BIOS_DEBUG, "Done jedec reset\n"); |
| 4110 | |
| 4111 | /* MRS commands */ |
| 4112 | dram_mrscommands(ctrl); |
| 4113 | printk(BIOS_DEBUG, "Done MRS commands\n"); |
| 4114 | |
| 4115 | /* Prepare for memory training */ |
| 4116 | prepare_training(ctrl); |
| 4117 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 4118 | err = read_training(ctrl); |
| 4119 | if (err) |
| 4120 | return err; |
| 4121 | |
| 4122 | err = write_training(ctrl); |
| 4123 | if (err) |
| 4124 | return err; |
Patrick Rudolph | 27e085a | 2016-03-26 10:59:02 +0100 | [diff] [blame] | 4125 | |
| 4126 | printram("CP5a\n"); |
| 4127 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 4128 | err = discover_edges(ctrl); |
| 4129 | if (err) |
| 4130 | return err; |
Patrick Rudolph | 27e085a | 2016-03-26 10:59:02 +0100 | [diff] [blame] | 4131 | |
| 4132 | printram("CP5b\n"); |
| 4133 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 4134 | err = command_training(ctrl); |
| 4135 | if (err) |
| 4136 | return err; |
Patrick Rudolph | 27e085a | 2016-03-26 10:59:02 +0100 | [diff] [blame] | 4137 | |
| 4138 | printram("CP5c\n"); |
| 4139 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 4140 | err = discover_edges_write(ctrl); |
| 4141 | if (err) |
| 4142 | return err; |
Patrick Rudolph | 27e085a | 2016-03-26 10:59:02 +0100 | [diff] [blame] | 4143 | |
Patrick Rudolph | 24a845b | 2016-03-25 18:19:47 +0100 | [diff] [blame] | 4144 | err = discover_timC_write(ctrl); |
| 4145 | if (err) |
| 4146 | return err; |
Patrick Rudolph | 27e085a | 2016-03-26 10:59:02 +0100 | [diff] [blame] | 4147 | |
| 4148 | normalize_training(ctrl); |
| 4149 | } |
| 4150 | |
| 4151 | set_4008c(ctrl); |
| 4152 | |
| 4153 | write_controller_mr(ctrl); |
| 4154 | |
Patrick Rudolph | 588ccaa | 2016-04-20 18:00:27 +0200 | [diff] [blame] | 4155 | if (!s3_resume) { |
| 4156 | err = channel_test(ctrl); |
| 4157 | if (err) |
| 4158 | return err; |
| 4159 | } |
Patrick Rudolph | 27e085a | 2016-03-26 10:59:02 +0100 | [diff] [blame] | 4160 | |
| 4161 | return 0; |
| 4162 | } |
| 4163 | |
| 4164 | void init_dram_ddr3(spd_raw_data *spds, int mobile, int min_tck, |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4165 | int s3resume) |
| 4166 | { |
| 4167 | int me_uma_size; |
| 4168 | int cbmem_was_inited; |
Patrick Rudolph | 735ecce | 2016-03-26 10:42:27 +0100 | [diff] [blame] | 4169 | ramctr_timing ctrl; |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4170 | int fast_boot; |
| 4171 | struct mrc_data_container *mrc_cache; |
| 4172 | ramctr_timing *ctrl_cached; |
Patrick Rudolph | 31d1959 | 2016-03-26 12:22:34 +0100 | [diff] [blame] | 4173 | int err; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4174 | |
| 4175 | MCHBAR32(0x5f00) |= 1; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4176 | |
Vadim Bendebury | 7a3f36a | 2012-04-18 15:47:32 -0700 | [diff] [blame] | 4177 | report_platform_info(); |
| 4178 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4179 | /* Wait for ME to be ready */ |
| 4180 | intel_early_me_init(); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4181 | me_uma_size = intel_early_me_uma_size(); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4182 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4183 | printk(BIOS_DEBUG, "Starting native Platform init\n"); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4184 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4185 | u32 reg_5d10; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4186 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4187 | wait_txt_clear(); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4188 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4189 | wrmsr(0x000002e6, (msr_t) { .lo = 0, .hi = 0 }); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4190 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4191 | reg_5d10 = read32(DEFAULT_MCHBAR + 0x5d10); // !!! = 0x00000000 |
| 4192 | if ((pcie_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 /* 0x0004 */ |
| 4193 | && reg_5d10 && !s3resume) { |
| 4194 | write32(DEFAULT_MCHBAR + 0x5d10, 0); |
| 4195 | /* Need reset. */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4196 | outb(0x6, 0xcf9); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4197 | |
Patrick Georgi | 546953c | 2014-11-29 10:38:17 +0100 | [diff] [blame] | 4198 | halt(); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4199 | } |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4200 | |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4201 | memset(&ctrl, 0, sizeof (ctrl)); |
| 4202 | |
| 4203 | early_pch_init_native(); |
| 4204 | early_thermal_init(); |
| 4205 | |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4206 | /* try to find timings in MRC cache */ |
| 4207 | mrc_cache = find_current_mrc_cache(); |
| 4208 | if (!mrc_cache || (mrc_cache->mrc_data_size < sizeof(ctrl))) { |
| 4209 | if (s3resume) { |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4210 | /* Failed S3 resume, reset to come up cleanly */ |
| 4211 | outb(0x6, 0xcf9); |
| 4212 | halt(); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4213 | } |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4214 | ctrl_cached = NULL; |
Patrick Rudolph | 27e085a | 2016-03-26 10:59:02 +0100 | [diff] [blame] | 4215 | } else { |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4216 | ctrl_cached = (ramctr_timing *)mrc_cache->mrc_data; |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4217 | } |
| 4218 | |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4219 | /* verify MRC cache for fast boot */ |
| 4220 | if (ctrl_cached) { |
| 4221 | /* check SPD CRC16 to make sure the DIMMs haven't been replaced */ |
| 4222 | fast_boot = verify_crc16_spds_ddr3(spds, ctrl_cached); |
| 4223 | if (!fast_boot) |
| 4224 | printk(BIOS_DEBUG, "Stored timings CRC16 mismatch.\n"); |
| 4225 | if (!fast_boot && s3resume) { |
| 4226 | /* Failed S3 resume, reset to come up cleanly */ |
| 4227 | outb(0x6, 0xcf9); |
| 4228 | halt(); |
| 4229 | } |
| 4230 | } else |
| 4231 | fast_boot = 0; |
| 4232 | |
| 4233 | if (fast_boot) { |
| 4234 | printk(BIOS_DEBUG, "Trying stored timings.\n"); |
| 4235 | memcpy(&ctrl, ctrl_cached, sizeof(ctrl)); |
| 4236 | |
Patrick Rudolph | 588ccaa | 2016-04-20 18:00:27 +0200 | [diff] [blame] | 4237 | err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size); |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4238 | if (err) { |
Patrick Rudolph | 588ccaa | 2016-04-20 18:00:27 +0200 | [diff] [blame] | 4239 | if (s3resume) { |
| 4240 | /* Failed S3 resume, reset to come up cleanly */ |
| 4241 | outb(0x6, 0xcf9); |
| 4242 | halt(); |
| 4243 | } |
| 4244 | /* no need to erase bad mrc cache here, it gets overwritten on |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4245 | * successful boot. */ |
| 4246 | printk(BIOS_ERR, "Stored timings are invalid !\n"); |
| 4247 | fast_boot = 0; |
| 4248 | } |
| 4249 | } |
| 4250 | if (!fast_boot) { |
| 4251 | ctrl.mobile = mobile; |
| 4252 | ctrl.tCK = min_tck; |
| 4253 | |
| 4254 | /* Get DDR3 SPD data */ |
| 4255 | dram_find_spds_ddr3(spds, &ctrl); |
| 4256 | |
Patrick Rudolph | 588ccaa | 2016-04-20 18:00:27 +0200 | [diff] [blame] | 4257 | err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size); |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4258 | } |
Patrick Rudolph | 2ccb74b | 2016-03-26 12:16:29 +0100 | [diff] [blame] | 4259 | |
| 4260 | if (err && (ctrl.tCK < TCK_400MHZ)) { |
| 4261 | /* fallback: lower clock frequency */ |
| 4262 | printk(BIOS_ERR, "RAM training failed, trying fallback.\n"); |
| 4263 | printram("Decreasing clock frequency.\n"); |
| 4264 | ctrl.tCK++; |
| 4265 | err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size); |
| 4266 | } |
| 4267 | |
| 4268 | if (err && (ctrl.tCK < TCK_400MHZ)) { |
| 4269 | /* fallback: lower clock frequency */ |
| 4270 | printk(BIOS_ERR, "RAM training failed, trying fallback.\n"); |
| 4271 | printram("Decreasing clock frequency.\n"); |
| 4272 | ctrl.tCK++; |
| 4273 | err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size); |
| 4274 | } |
| 4275 | |
| 4276 | if (err) { |
| 4277 | /* fallback: disable failing channel */ |
| 4278 | printk(BIOS_ERR, "RAM training failed, trying fallback.\n"); |
| 4279 | printram("Disable failing channel.\n"); |
| 4280 | |
| 4281 | /* Reset DDR3 frequency */ |
| 4282 | dram_find_spds_ddr3(spds, &ctrl); |
| 4283 | |
| 4284 | /* disable failing channel */ |
| 4285 | disable_channel(&ctrl, GET_ERR_CHANNEL(err)); |
| 4286 | |
| 4287 | err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size); |
| 4288 | } |
| 4289 | |
Patrick Rudolph | 31d1959 | 2016-03-26 12:22:34 +0100 | [diff] [blame] | 4290 | if (err) |
| 4291 | die("raminit failed"); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4292 | |
| 4293 | /* FIXME: should be hardware revision-dependent. */ |
| 4294 | write32(DEFAULT_MCHBAR + 0x5024, 0x00a030ce); |
| 4295 | |
| 4296 | set_scrambling_seed(&ctrl); |
| 4297 | |
| 4298 | set_42a0(&ctrl); |
| 4299 | |
| 4300 | final_registers(&ctrl); |
| 4301 | |
| 4302 | /* Zone config */ |
| 4303 | dram_zones(&ctrl, 0); |
| 4304 | |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4305 | if (!fast_boot) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4306 | quick_ram_check(); |
| 4307 | |
| 4308 | intel_early_me_status(); |
| 4309 | intel_early_me_init_done(ME_INIT_STATUS_SUCCESS); |
| 4310 | intel_early_me_status(); |
| 4311 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4312 | report_memory_config(); |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4313 | |
| 4314 | cbmem_was_inited = !cbmem_recovery(s3resume); |
Patrick Rudolph | 56abd4d | 2016-03-13 11:07:45 +0100 | [diff] [blame] | 4315 | if (!fast_boot) |
Alexandru Gagniuc | ecf2eb4 | 2015-09-28 21:39:12 -0700 | [diff] [blame] | 4316 | save_timings(&ctrl); |
| 4317 | if (s3resume && !cbmem_was_inited) { |
| 4318 | /* Failed S3 resume, reset to come up cleanly */ |
| 4319 | outb(0x6, 0xcf9); |
| 4320 | halt(); |
| 4321 | } |
Patrick Rudolph | b97009e | 2016-02-28 15:24:04 +0100 | [diff] [blame] | 4322 | |
Patrick Rudolph | 735ecce | 2016-03-26 10:42:27 +0100 | [diff] [blame] | 4323 | fill_smbios17(&ctrl); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 4324 | } |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 4325 | |
| 4326 | #define HOST_BRIDGE PCI_DEVFN(0, 0) |
| 4327 | #define DEFAULT_TCK TCK_800MHZ |
| 4328 | |
| 4329 | static unsigned int get_mem_min_tck(void) |
| 4330 | { |
Patrick Rudolph | 9f3f915 | 2016-01-26 20:02:14 +0100 | [diff] [blame] | 4331 | u32 reg32; |
| 4332 | u8 rev; |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 4333 | const struct device *dev; |
Patrick Rudolph | 9f3f915 | 2016-01-26 20:02:14 +0100 | [diff] [blame] | 4334 | const struct northbridge_intel_sandybridge_config *cfg = NULL; |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 4335 | |
| 4336 | dev = dev_find_slot(0, HOST_BRIDGE); |
Patrick Rudolph | 9f3f915 | 2016-01-26 20:02:14 +0100 | [diff] [blame] | 4337 | if (dev) |
| 4338 | cfg = dev->chip_info; |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 4339 | |
| 4340 | /* If this is zero, it just means devicetree.cb didn't set it */ |
Patrick Rudolph | 9f3f915 | 2016-01-26 20:02:14 +0100 | [diff] [blame] | 4341 | if (!cfg || cfg->max_mem_clock_mhz == 0) { |
| 4342 | rev = pci_read_config8(PCI_DEV(0, 0, 0), PCI_DEVICE_ID); |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 4343 | |
Patrick Rudolph | 9f3f915 | 2016-01-26 20:02:14 +0100 | [diff] [blame] | 4344 | if ((rev & BASE_REV_MASK) == BASE_REV_SNB) { |
| 4345 | /* read Capabilities A Register DMFC bits */ |
| 4346 | reg32 = pci_read_config32(PCI_DEV(0, 0, 0), CAPID0_A); |
| 4347 | reg32 &= 0x7; |
| 4348 | |
| 4349 | switch (reg32) { |
| 4350 | case 7: return TCK_533MHZ; |
| 4351 | case 6: return TCK_666MHZ; |
| 4352 | case 5: return TCK_800MHZ; |
| 4353 | /* reserved: */ |
| 4354 | default: |
| 4355 | break; |
| 4356 | } |
| 4357 | } else { |
| 4358 | /* read Capabilities B Register DMFC bits */ |
| 4359 | reg32 = pci_read_config32(PCI_DEV(0, 0, 0), CAPID0_B); |
| 4360 | reg32 = (reg32 >> 4) & 0x7; |
| 4361 | |
| 4362 | switch (reg32) { |
| 4363 | case 7: return TCK_533MHZ; |
| 4364 | case 6: return TCK_666MHZ; |
| 4365 | case 5: return TCK_800MHZ; |
| 4366 | case 4: return TCK_933MHZ; |
| 4367 | case 3: return TCK_1066MHZ; |
| 4368 | case 2: return TCK_1200MHZ; |
| 4369 | case 1: return TCK_1333MHZ; |
| 4370 | /* reserved: */ |
| 4371 | default: |
| 4372 | break; |
| 4373 | } |
| 4374 | } |
| 4375 | return DEFAULT_TCK; |
| 4376 | } else { |
| 4377 | if (cfg->max_mem_clock_mhz >= 800) |
| 4378 | return TCK_800MHZ; |
| 4379 | else if (cfg->max_mem_clock_mhz >= 666) |
| 4380 | return TCK_666MHZ; |
| 4381 | else if (cfg->max_mem_clock_mhz >= 533) |
| 4382 | return TCK_533MHZ; |
| 4383 | else |
| 4384 | return TCK_400MHZ; |
| 4385 | } |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 4386 | } |
| 4387 | |
Patrick Rudolph | 266a1f7 | 2016-06-09 18:13:34 +0200 | [diff] [blame] | 4388 | #define DEFAULT_PCI_MMIO_SIZE 2048 |
| 4389 | |
| 4390 | static unsigned int get_mmio_size(void) |
| 4391 | { |
| 4392 | const struct device *dev; |
| 4393 | const struct northbridge_intel_sandybridge_config *cfg = NULL; |
| 4394 | |
| 4395 | dev = dev_find_slot(0, HOST_BRIDGE); |
| 4396 | if (dev) |
| 4397 | cfg = dev->chip_info; |
| 4398 | |
| 4399 | /* If this is zero, it just means devicetree.cb didn't set it */ |
| 4400 | if (!cfg || cfg->pci_mmio_size == 0) |
| 4401 | return DEFAULT_PCI_MMIO_SIZE; |
| 4402 | else |
| 4403 | return cfg->pci_mmio_size; |
| 4404 | } |
| 4405 | |
Vladimir Serbinenko | ffbb3c0 | 2016-02-10 01:36:25 +0100 | [diff] [blame] | 4406 | void perform_raminit(int s3resume) |
| 4407 | { |
| 4408 | spd_raw_data spd[4]; |
| 4409 | |
| 4410 | post_code(0x3a); |
| 4411 | |
| 4412 | memset (spd, 0, sizeof (spd)); |
| 4413 | mainboard_get_spd(spd); |
| 4414 | |
| 4415 | timestamp_add_now(TS_BEFORE_INITRAM); |
| 4416 | |
| 4417 | init_dram_ddr3(spd, 1, get_mem_min_tck(), s3resume); |
| 4418 | } |