blob: 8aa3068df2dd3edc3699b64aad231b74af9dc579 [file] [log] [blame]
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 Damien Zammit <damien@zamaudio.com>
5 * Copyright (C) 2014 Vladimir Serbinenko <phcoder@gmail.com>
6 * Copyright (C) 2016 Patrick Rudolph <siro@das-labor.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#ifndef RAMINIT_COMMON_H
19#define RAMINIT_COMMON_H
20
Felix Held380c6b22020-01-26 05:06:38 +010021#include <stdint.h>
22
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010023#define BASEFREQ 133
24#define tDLLK 512
25
26#define IS_SANDY_CPU(x) ((x & 0xffff0) == 0x206a0)
27#define IS_SANDY_CPU_C(x) ((x & 0xf) == 4)
28#define IS_SANDY_CPU_D0(x) ((x & 0xf) == 5)
29#define IS_SANDY_CPU_D1(x) ((x & 0xf) == 6)
30#define IS_SANDY_CPU_D2(x) ((x & 0xf) == 7)
31
32#define IS_IVY_CPU(x) ((x & 0xffff0) == 0x306a0)
33#define IS_IVY_CPU_C(x) ((x & 0xf) == 4)
34#define IS_IVY_CPU_K(x) ((x & 0xf) == 5)
35#define IS_IVY_CPU_D(x) ((x & 0xf) == 6)
36#define IS_IVY_CPU_E(x) ((x & 0xf) >= 8)
37
38#define NUM_CHANNELS 2
39#define NUM_SLOTRANKS 4
40#define NUM_SLOTS 2
41#define NUM_LANES 8
42
43/* FIXME: Vendor BIOS uses 64 but our algorithms are less
44 performant and even 1 seems to be enough in practice. */
45#define NUM_PATTERNS 4
46
47typedef struct odtmap_st {
48 u16 rttwr;
49 u16 rttnom;
50} odtmap;
51
52typedef struct dimm_info_st {
53 dimm_attr dimm[NUM_CHANNELS][NUM_SLOTS];
54} dimm_info;
55
56struct ram_rank_timings {
Angel Pons88521882020-01-05 20:21:20 +010057 /* ROUNDT_LAT register. One byte per slotrank. */
58 u8 roundtrip_latency;
59
60 /* IO_LATENCY register. One nibble per slotrank. */
Felix Heldef4fe3e2019-12-31 14:15:05 +010061 u8 io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010062
Angel Pons88521882020-01-05 20:21:20 +010063 /* Phase interpolator coding for command and control. */
64 int pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010065
66 struct ram_lane_timings {
67 /* lane register offset 0x10. */
68 u16 timA; /* bits 0 - 5, bits 16 - 18 */
69 u8 rising; /* bits 8 - 14 */
70 u8 falling; /* bits 20 - 26. */
71
72 /* lane register offset 0x20. */
73 int timC; /* bit 0 - 5, 19. */
74 u16 timB; /* bits 8 - 13, 15 - 17. */
75 } lanes[NUM_LANES];
76};
77
78struct ramctr_timing_st;
79
80typedef struct ramctr_timing_st {
81 u16 spd_crc[NUM_CHANNELS][NUM_SLOTS];
Patrick Rudolph305035c2016-11-11 18:38:50 +010082 int sandybridge;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010083
Patrick Rudolph77eaba32016-11-11 18:55:54 +010084 /* DDR base_freq = 100 Mhz / 133 Mhz */
85 u8 base_freq;
86
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010087 u16 cas_supported;
88 /* tLatencies are in units of ns, scaled by x256 */
89 u32 tCK;
90 u32 tAA;
91 u32 tWR;
92 u32 tRCD;
93 u32 tRRD;
94 u32 tRP;
95 u32 tRAS;
96 u32 tRFC;
97 u32 tWTR;
98 u32 tRTP;
99 u32 tFAW;
Dan Elkoubydabebc32018-04-13 18:47:10 +0300100 u32 tCWL;
101 u32 tCMD;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100102 /* Latencies in terms of clock cycles
Angel Pons88521882020-01-05 20:21:20 +0100103 * They are saved separately as they are needed for DRAM MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100104 u8 CAS; /* CAS read latency */
105 u8 CWL; /* CAS write latency */
106
107 u32 tREFI;
108 u32 tMOD;
109 u32 tXSOffset;
110 u32 tWLO;
111 u32 tCKE;
112 u32 tXPDLL;
113 u32 tXP;
114 u32 tAONPD;
115
Angel Pons88521882020-01-05 20:21:20 +0100116 /* Bits [0..11] of PM_DLL_CONFIG: Master DLL wakeup delay timer. */
117 u16 mdll_wake_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100118
119 u8 rankmap[NUM_CHANNELS];
120 int ref_card_offset[NUM_CHANNELS];
121 u32 mad_dimm[NUM_CHANNELS];
122 int channel_size_mb[NUM_CHANNELS];
123 u32 cmd_stretch[NUM_CHANNELS];
124
Angel Pons88521882020-01-05 20:21:20 +0100125 int pi_code_offset;
126 int pi_coding_threshold;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100127
128 int edge_offset[3];
129 int timC_offset[3];
130
131 int extended_temperature_range;
132 int auto_self_refresh;
133
134 int rank_mirror[NUM_CHANNELS][NUM_SLOTRANKS];
135
136 struct ram_rank_timings timings[NUM_CHANNELS][NUM_SLOTRANKS];
137
138 dimm_info info;
139} ramctr_timing;
140
Felix Held87ddea22020-01-26 04:55:27 +0100141#define HOST_BRIDGE PCI_DEV(0, 0, 0)
142#define SOUTHBRIDGE PCI_DEV(0, 0x1f, 0)
143
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100144#define FOR_ALL_LANES for (lane = 0; lane < NUM_LANES; lane++)
145#define FOR_ALL_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++)
146#define FOR_ALL_POPULATED_RANKS for (slotrank = 0; slotrank < NUM_SLOTRANKS; slotrank++) if (ctrl->rankmap[channel] & (1 << slotrank))
147#define FOR_ALL_POPULATED_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++) if (ctrl->rankmap[channel])
148#define MAX_EDGE_TIMING 71
149#define MAX_TIMC 127
150#define MAX_TIMB 511
151#define MAX_TIMA 127
152#define MAX_CAS 18
153#define MIN_CAS 4
154
155#define MAKE_ERR ((channel<<16)|(slotrank<<8)|1)
156#define GET_ERR_CHANNEL(x) (x>>16)
157
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100158u8 get_CWL(u32 tCK);
Angel Pons88521882020-01-05 20:21:20 +0100159void dram_mrscommands(ramctr_timing *ctrl);
160void program_timings(ramctr_timing *ctrl, int channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100161void dram_find_common_params(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100162void dram_xover(ramctr_timing *ctrl);
163void dram_timing_regs(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100164void dram_dimm_mapping(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100165void dram_dimm_set_mapping(ramctr_timing *ctrl);
166void dram_zones(ramctr_timing *ctrl, int training);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100167unsigned int get_mem_min_tck(void);
Angel Pons88521882020-01-05 20:21:20 +0100168void dram_memorymap(ramctr_timing *ctrl, int me_uma_size);
169void dram_jedecreset(ramctr_timing *ctrl);
170int read_training(ramctr_timing *ctrl);
171int write_training(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100172int command_training(ramctr_timing *ctrl);
173int discover_edges(ramctr_timing *ctrl);
174int discover_edges_write(ramctr_timing *ctrl);
175int discover_timC_write(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100176void normalize_training(ramctr_timing *ctrl);
177void write_controller_mr(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100178int channel_test(ramctr_timing *ctrl);
Angel Pons88521882020-01-05 20:21:20 +0100179void set_scrambling_seed(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100180void set_4f8c(void);
Angel Pons88521882020-01-05 20:21:20 +0100181void prepare_training(ramctr_timing *ctrl);
182void set_4008c(ramctr_timing *ctrl);
183void set_normal_operation(ramctr_timing *ctrl);
184void final_registers(ramctr_timing *ctrl);
185void restore_timings(ramctr_timing *ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100186
Patrick Rudolph305035c2016-11-11 18:38:50 +0100187int try_init_dram_ddr3_sandy(ramctr_timing *ctrl, int fast_boot,
188 int s3_resume, int me_uma_size);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100189
Patrick Rudolph305035c2016-11-11 18:38:50 +0100190int try_init_dram_ddr3_ivy(ramctr_timing *ctrl, int fast_boot,
191 int s3_resume, int me_uma_size);
192
193#endif