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