Debian Codegraph @

src/northbridge/intel/haswell/native_raminit/reut.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <console/console.h>
4 #include <delay.h>
5 #include <northbridge/intel/haswell/haswell.h>
6 #include <timer.h>
7 #include <types.h>
8
9 #include "raminit_native.h"
10
11 enum {
12 CADB_CMD_MRS = 0,
13 CADB_CMD_REF = 1,
14 CADB_CMD_PRE = 2,
15 CADB_CMD_ACT = 3,
16 CADB_CMD_WR = 4,
17 CADB_CMD_RD = 5,
18 CADB_CMD_ZQ = 6,
19 CADB_CMD_NOP = 7,
20 };
21
22 /*
23 * DDR3 rank mirror swaps the following pins: A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
24 *
25 * Note that the swapped bits are contiguous. We can use some XOR magic to swap the bits.
26 * Address lanes are at bits 0..15 and bank selects are at bits 24..26 on the REUT register.
27 */
28 #define MIRROR_BITS (BIT(24) | BIT(7) | BIT(5) | BIT(3))
29 static uint64_t cadb_prog_rank_mirror(const uint64_t cadb_prog)
30 {
31 /* First XOR: find which pairs of bits are different (need swapping) */
32 const uint64_t tmp64 = (cadb_prog ^ (cadb_prog >> 1)) & MIRROR_BITS;
33
34 /* Second XOR: invert the pairs of bits that have different values */
35 return cadb_prog ^ (tmp64 | tmp64 << 1);
36 }
37
38 static enum raminit_status reut_write_cadb_cmd(
39 struct sysinfo *ctrl,
40 const uint8_t channel,
41 const uint8_t rankmask,
42 const uint8_t cmd,
43 const uint8_t bank,
44 const uint16_t valarr[NUM_SLOTRANKS],
45 const uint8_t delay)
46 {
47 union mcscheds_dft_misc_reg dft_misc = {
48 .raw = mchbar_read32(MCSCHEDS_DFT_MISC),
49 };
50 dft_misc.ddr_qualifier = 0;
51 mchbar_write32(MCSCHEDS_DFT_MISC, dft_misc.raw);
52
53 /* Pointer will be dynamically incremented after a write to CADB_PROG register */
54 mchbar_write8(REUT_ch_PAT_CADB_WRITE_PTR(channel), 0);
55
56 uint8_t count = 0;
57 for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) {
58 if (!(ctrl->rankmap[channel] & BIT(rank) & rankmask))
59 continue;
60
61 union reut_pat_cadb_prog_reg reut_cadb_prog = {
62 .addr = valarr[rank],
63 .bank = bank,
64 .cs = ~BIT(rank), /* CS is active low */
65 .cmd = cmd,
66 .cke = 0xf,
67 };
68 if (ctrl->rank_mirrored[channel] & BIT(rank))
69 reut_cadb_prog.raw = cadb_prog_rank_mirror(reut_cadb_prog.raw);
70
71 mchbar_write64(REUT_ch_PAT_CADB_PROG(channel), reut_cadb_prog.raw);
72 count++;
73 }
74 if (!count) {
75 printk(BIOS_ERR, "%s: rankmask is invalid\n", __func__);
76 return RAMINIT_STATUS_UNSPECIFIED_ERROR; /** FIXME: Is this needed? **/
77 }
78 const union reut_pat_cadb_mrs_reg reut_cadb_mrs = {
79 .delay_gap = delay ? delay : 3,
80 .end_ptr = count - 1,
81 };
82 mchbar_write32(REUT_ch_PAT_CADB_MRS(channel), reut_cadb_mrs.raw);
83
84 const uint32_t reut_seq_cfg_save = mchbar_read32(REUT_ch_SEQ_CFG(channel));
85 union reut_seq_cfg_reg reut_seq_cfg = {
86 .raw = reut_seq_cfg_save,
87 };
88 reut_seq_cfg.global_control = 0;
89 reut_seq_cfg.initialization_mode = REUT_MODE_MRS;
90 mchbar_write32(REUT_ch_SEQ_CFG(channel), reut_seq_cfg.raw);
91 mchbar_write32(REUT_ch_SEQ_CTL(channel), (union reut_seq_ctl_reg) {
92 .start_test = 1,
93 .clear_errors = 1,
94 }.raw);
95 enum raminit_status status = RAMINIT_STATUS_SUCCESS;
96 union reut_global_err_reg reut_global_err;
97 struct stopwatch timer;
98 stopwatch_init_msecs_expire(&timer, 100);
99 do {
100 reut_global_err.raw = mchbar_read32(REUT_GLOBAL_ERR);
101 if (reut_global_err.ch_error & BIT(channel)) {
102 printk(BIOS_ERR, "Unexpected REUT error for channel %u\n", channel);
103 status = RAMINIT_STATUS_REUT_ERROR;
104 break;
105 }
106 if (stopwatch_expired(&timer)) {
107 printk(BIOS_ERR, "%s: REUT timed out!\n", __func__);
108 status = RAMINIT_STATUS_POLL_TIMEOUT;
109 break;
110 }
111 } while (!(reut_global_err.ch_test_done & BIT(channel)));
112 mchbar_write32(REUT_ch_SEQ_CTL(channel), (union reut_seq_ctl_reg) {
113 .clear_errors = 1,
114 }.raw);
115 mchbar_write32(REUT_ch_SEQ_CFG(channel), reut_seq_cfg_save);
116 return status;
117 }
118
119 static enum raminit_status reut_write_cadb_cmd_all(
120 struct sysinfo *ctrl,
121 const uint8_t channel,
122 const uint8_t rankmask,
123 const uint8_t cmd,
124 const uint8_t bank,
125 const uint16_t val,
126 const uint8_t delay)
127 {
128 const uint16_t valarr[NUM_SLOTRANKS] = { val, val, val, val };
129 return reut_write_cadb_cmd(ctrl, channel, rankmask, cmd, bank, valarr, delay);
130 }
131
132 void reut_issue_mrs(
133 struct sysinfo *ctrl,
134 const uint8_t channel,
135 const uint8_t rankmask,
136 const uint8_t mr,
137 const uint16_t val)
138 {
139 reut_write_cadb_cmd_all(ctrl, channel, rankmask, CADB_CMD_MRS, mr, val, 0);
140 }
141
142 void reut_issue_mrs_all(
143 struct sysinfo *ctrl,
144 const uint8_t channel,
145 const uint8_t mr,
146 const uint16_t val[NUM_SLOTS])
147 {
148 const uint16_t valarr[NUM_SLOTRANKS] = { val[0], val[0], val[1], val[1] };
149 reut_write_cadb_cmd(ctrl, channel, 0xf, CADB_CMD_MRS, mr, valarr, 0);
150 }
151
152 enum raminit_status reut_issue_zq(struct sysinfo *ctrl, uint8_t chanmask, uint8_t zq_type)
153 {
154 /** TODO: Issuing ZQ commands differs for LPDDR **/
155 if (ctrl->lpddr)
156 die("%s: LPDDR not yet supported in ZQ calibration\n", __func__);
157
158 __maybe_unused uint8_t opcode; /* NOTE: Only used for LPDDR */
159 uint16_t zq = 0;
160 switch (zq_type) {
161 case ZQ_INIT:
162 zq = BIT(10);
163 opcode = 0xff;
164 break;
165 case ZQ_LONG:
166 zq = BIT(10);
167 opcode = 0xab;
168 break;
169 case ZQ_SHORT:
170 opcode = 0x56;
171 break;
172 case ZQ_RESET:
173 opcode = 0xc3;
174 break;
175 default:
176 die("%s: ZQ type %u is invalid\n", __func__, zq_type);
177 }
178
179 /* ZQCS on single-channel needs a longer delay */
180 const uint8_t delay = zq_type == ZQ_SHORT && (!ctrl->dpc[0] || !ctrl->dpc[1]) ? 7 : 1;
181 enum raminit_status status = RAMINIT_STATUS_SUCCESS;
182 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
183 if (!(BIT(channel) & chanmask) || !does_ch_exist(ctrl, channel))
184 continue;
185
186 status = reut_write_cadb_cmd_all(ctrl, channel, 0xf, CADB_CMD_ZQ, 0, zq, delay);
187 if (status)
188 break;
189 }
190
191 /* Wait a bit after ZQ INIT and ZQCL commands */
192 if (zq)
193 udelay(1);
194
195 return status;
196 }