Angel Pons | 4b42983 | 2020-04-02 23:48:50 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 2 | |
Arthur Heymans | 186e9c4 | 2017-01-10 14:26:23 +0100 | [diff] [blame] | 3 | #include <console/console.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 4 | #include <device/mmio.h> |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 5 | #include "raminit.h" |
Arthur Heymans | 186e9c4 | 2017-01-10 14:26:23 +0100 | [diff] [blame] | 6 | #include "i945.h" |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 7 | |
| 8 | /** |
| 9 | * sample the strobes signal |
| 10 | */ |
| 11 | static u32 sample_strobes(int channel_offset, struct sys_info *sysinfo) |
| 12 | { |
| 13 | u32 reg32, addr; |
| 14 | int i; |
| 15 | |
| 16 | MCHBAR32(C0DRC1 + channel_offset) |= (1 << 6); |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 17 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 18 | MCHBAR32(C0DRC1 + channel_offset) &= ~(1 << 6); |
| 19 | |
| 20 | addr = 0; |
| 21 | |
| 22 | if (channel_offset != 0) { /* must be dual channel */ |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 23 | if (sysinfo->interleaved == 1) |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 24 | addr |= (1 << 6); |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 25 | else |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 26 | addr = ((u32)MCHBAR8(C0DRB3)) << 25; |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | for (i = 0; i < 28; i++) { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 30 | read32((void *)addr); |
| 31 | read32((void *)(addr + 0x80)); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | reg32 = MCHBAR32(RCVENMT); |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 35 | if (channel_offset == 0) |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 36 | reg32 = reg32 << 2; |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * [19] = 1: all bits are high |
| 40 | * [18] = 1: all bits are low |
| 41 | * [19:18] = 00: bits are mixed high, low |
| 42 | */ |
| 43 | return reg32; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * This function sets receive enable coarse and medium timing parameters |
| 48 | */ |
| 49 | |
| 50 | static void set_receive_enable(int channel_offset, u8 medium, u8 coarse) |
| 51 | { |
| 52 | u32 reg32; |
| 53 | |
Elyes HAOUAS | 3cd4327 | 2020-03-05 22:01:17 +0100 | [diff] [blame] | 54 | printk(BIOS_SPEW, " %s() medium=0x%x, coarse=0x%x\n", __func__, medium, coarse); |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 55 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 56 | reg32 = MCHBAR32(C0DRT1 + channel_offset); |
| 57 | reg32 &= 0xf0ffffff; |
| 58 | reg32 |= ((u32)coarse & 0x0f) << 24; |
| 59 | MCHBAR32(C0DRT1 + channel_offset) = reg32; |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 60 | |
| 61 | /* This should never happen: */ |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 62 | if (coarse > 0x0f) |
Elyes HAOUAS | 3cd4327 | 2020-03-05 22:01:17 +0100 | [diff] [blame] | 63 | printk(BIOS_DEBUG, "%s: coarse overflow: 0x%02x.\n", __func__, coarse); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 64 | |
| 65 | /* medium control |
| 66 | * |
| 67 | * 00 - 1/4 clock |
| 68 | * 01 - 1/2 clock |
| 69 | * 10 - 3/4 clock |
| 70 | * 11 - 1 clock |
| 71 | */ |
| 72 | |
| 73 | reg32 = MCHBAR32(RCVENMT); |
| 74 | if (!channel_offset) { |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 75 | /* Channel 0 */ |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 76 | reg32 &= ~(3 << 2); |
| 77 | reg32 |= medium << 2; |
| 78 | } else { |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 79 | /* Channel 1 */ |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 80 | reg32 &= ~(3 << 0); |
| 81 | reg32 |= medium; |
| 82 | } |
| 83 | MCHBAR32(RCVENMT) = reg32; |
| 84 | |
| 85 | } |
| 86 | |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 87 | static int normalize(int channel_offset, u8 *mediumcoarse, u8 *fine) |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 88 | { |
Elyes HAOUAS | 3cd4327 | 2020-03-05 22:01:17 +0100 | [diff] [blame] | 89 | printk(BIOS_SPEW, " %s()\n", __func__); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 90 | |
| 91 | if (*fine < 0x80) |
| 92 | return 0; |
| 93 | |
| 94 | *fine -= 0x80; |
| 95 | *mediumcoarse += 1; |
| 96 | |
| 97 | if (*mediumcoarse >= 0x40) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 98 | printk(BIOS_DEBUG, "Normalize Error\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 99 | return -1; |
| 100 | } |
| 101 | |
Elyes HAOUAS | 3dff32c | 2020-03-30 17:16:51 +0200 | [diff] [blame] | 102 | set_receive_enable(channel_offset, *mediumcoarse & 3, *mediumcoarse >> 2); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 103 | |
| 104 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 109 | static int find_preamble(int channel_offset, u8 *mediumcoarse, |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 110 | struct sys_info *sysinfo) |
| 111 | { |
| 112 | /* find start of the data phase */ |
| 113 | u32 reg32; |
| 114 | |
Elyes HAOUAS | 3cd4327 | 2020-03-05 22:01:17 +0100 | [diff] [blame] | 115 | printk(BIOS_SPEW, " %s()\n", __func__); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 116 | |
| 117 | do { |
| 118 | if (*mediumcoarse < 4) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 119 | printk(BIOS_DEBUG, "No Preamble found.\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 120 | return -1; |
| 121 | } |
| 122 | *mediumcoarse -= 4; |
| 123 | |
Elyes HAOUAS | 3dff32c | 2020-03-30 17:16:51 +0200 | [diff] [blame] | 124 | set_receive_enable(channel_offset, *mediumcoarse & 3, *mediumcoarse >> 2); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 125 | |
| 126 | reg32 = sample_strobes(channel_offset, sysinfo); |
| 127 | |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 128 | } while (reg32 & (1 << 19)); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 129 | |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 130 | if (!(reg32 & (1 << 18))) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 131 | printk(BIOS_DEBUG, "No Preamble found (neither high nor low).\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * add a quarter clock to the current receive enable settings |
| 140 | */ |
| 141 | |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 142 | static int add_quarter_clock(int channel_offset, u8 *mediumcoarse, u8 *fine) |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 143 | { |
Elyes HAOUAS | 3cd4327 | 2020-03-05 22:01:17 +0100 | [diff] [blame] | 144 | printk(BIOS_SPEW, " %s() mediumcoarse=%02x fine=%02x\n", __func__, |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 145 | *mediumcoarse, *fine); |
| 146 | if (*fine >= 0x80) { |
| 147 | *fine -= 0x80; |
| 148 | |
| 149 | *mediumcoarse += 2; |
| 150 | if (*mediumcoarse >= 0x40) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 151 | printk(BIOS_DEBUG, "clocks at max.\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 152 | return -1; |
| 153 | } |
| 154 | |
Elyes HAOUAS | 3dff32c | 2020-03-30 17:16:51 +0200 | [diff] [blame] | 155 | set_receive_enable(channel_offset, *mediumcoarse & 3, *mediumcoarse >> 2); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 156 | } else { |
| 157 | *fine += 0x80; |
| 158 | } |
| 159 | |
| 160 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 165 | static int find_strobes_low(int channel_offset, u8 *mediumcoarse, u8 *fine, |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 166 | struct sys_info *sysinfo) |
| 167 | { |
| 168 | u32 rcvenmt; |
| 169 | |
Elyes HAOUAS | 3cd4327 | 2020-03-05 22:01:17 +0100 | [diff] [blame] | 170 | printk(BIOS_SPEW, " %s()\n", __func__); |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 171 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 172 | for (;;) { |
| 173 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 174 | |
Elyes HAOUAS | 3dff32c | 2020-03-30 17:16:51 +0200 | [diff] [blame] | 175 | set_receive_enable(channel_offset, *mediumcoarse & 3, *mediumcoarse >> 2); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 176 | |
| 177 | rcvenmt = sample_strobes(channel_offset, sysinfo); |
| 178 | |
| 179 | if (((rcvenmt & (1 << 18)) != 0)) |
| 180 | return 0; |
| 181 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 182 | *fine -= 0x80; |
| 183 | if (*fine == 0) |
| 184 | continue; |
| 185 | |
| 186 | *mediumcoarse -= 2; |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 187 | if (*mediumcoarse < 0xfe) |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 188 | continue; |
| 189 | |
| 190 | break; |
| 191 | |
| 192 | } |
| 193 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 194 | printk(BIOS_DEBUG, "Could not find low strobe\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 198 | static int find_strobes_edge(int channel_offset, u8 *mediumcoarse, u8 *fine, |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 199 | struct sys_info *sysinfo) |
| 200 | { |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 201 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 202 | int counter; |
| 203 | u32 rcvenmt; |
| 204 | |
Elyes HAOUAS | 3cd4327 | 2020-03-05 22:01:17 +0100 | [diff] [blame] | 205 | printk(BIOS_SPEW, " %s()\n", __func__); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 206 | |
| 207 | counter = 8; |
Elyes HAOUAS | 3dff32c | 2020-03-30 17:16:51 +0200 | [diff] [blame] | 208 | set_receive_enable(channel_offset, *mediumcoarse & 3, *mediumcoarse >> 2); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 209 | |
| 210 | for (;;) { |
| 211 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 212 | rcvenmt = sample_strobes(channel_offset, sysinfo); |
| 213 | |
| 214 | if ((rcvenmt & (1 << 19)) == 0) { |
| 215 | counter = 8; |
| 216 | } else { |
| 217 | counter--; |
| 218 | if (!counter) |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | *fine = *fine + 1; |
| 223 | if (*fine < 0xf8) { |
| 224 | if (*fine & (1 << 3)) { |
| 225 | *fine &= ~(1 << 3); |
| 226 | *fine += 0x10; |
| 227 | } |
| 228 | continue; |
| 229 | } |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 230 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 231 | *fine = 0; |
| 232 | *mediumcoarse += 2; |
| 233 | if (*mediumcoarse <= 0x40) { |
| 234 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 235 | *mediumcoarse >> 2); |
| 236 | continue; |
| 237 | } |
| 238 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 239 | printk(BIOS_DEBUG, "Could not find rising edge.\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 240 | return -1; |
| 241 | } |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 242 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 243 | *fine -= 7; |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 244 | if (*fine >= 0xf9) { |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 245 | *mediumcoarse -= 2; |
Elyes HAOUAS | 3dff32c | 2020-03-30 17:16:51 +0200 | [diff] [blame] | 246 | set_receive_enable(channel_offset, *mediumcoarse & 3, *mediumcoarse >> 2); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 249 | *fine &= ~(1 << 3); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 250 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /** |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 256 | * Here we use a trick. The RCVEN channel 0 registers are all at an |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 257 | * offset of 0x80 to the channel 0 registers. We don't want to waste |
Elyes HAOUAS | 12df950 | 2016-08-23 21:29:48 +0200 | [diff] [blame] | 258 | * a lot of if ()s so let's just pass 0 or 0x80 for the channel offset. |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 259 | */ |
| 260 | |
Elyes HAOUAS | 3dff32c | 2020-03-30 17:16:51 +0200 | [diff] [blame] | 261 | static int receive_enable_autoconfig(int channel_offset, struct sys_info *sysinfo) |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 262 | { |
| 263 | u8 mediumcoarse; |
| 264 | u8 fine; |
| 265 | |
Elyes HAOUAS | 3cd4327 | 2020-03-05 22:01:17 +0100 | [diff] [blame] | 266 | printk(BIOS_SPEW, "%s() for channel %d\n", __func__, channel_offset ? 1 : 0); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 267 | |
| 268 | /* Set initial values */ |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 269 | mediumcoarse = (sysinfo->cas << 2) | 3; |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 270 | fine = 0; |
| 271 | |
| 272 | if (find_strobes_low(channel_offset, &mediumcoarse, &fine, sysinfo)) |
| 273 | return -1; |
| 274 | |
| 275 | if (find_strobes_edge(channel_offset, &mediumcoarse, &fine, sysinfo)) |
| 276 | return -1; |
| 277 | |
| 278 | if (add_quarter_clock(channel_offset, &mediumcoarse, &fine)) |
| 279 | return -1; |
| 280 | |
| 281 | if (find_preamble(channel_offset, &mediumcoarse, sysinfo)) |
| 282 | return -1; |
| 283 | |
| 284 | if (add_quarter_clock(channel_offset, &mediumcoarse, &fine)) |
| 285 | return -1; |
| 286 | |
| 287 | if (normalize(channel_offset, &mediumcoarse, &fine)) |
| 288 | return -1; |
| 289 | |
| 290 | /* This is a debug check to see if the rcven code is fully working. |
| 291 | * It can be removed when the output message is not printed anymore |
| 292 | */ |
Arthur Heymans | 70a8e34 | 2017-03-09 11:30:23 +0100 | [diff] [blame] | 293 | if (MCHBAR8(C0WL0REOST + channel_offset) == 0) |
Angel Pons | 3049257 | 2020-06-11 13:24:54 +0200 | [diff] [blame^] | 294 | printk(BIOS_DEBUG, "Weird. No C%sWL0REOST\n", channel_offset ? "1" : "0"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | void receive_enable_adjust(struct sys_info *sysinfo) |
| 300 | { |
| 301 | /* Is channel 0 populated? */ |
| 302 | if (sysinfo->dimm[0] != SYSINFO_DIMM_NOT_POPULATED |
| 303 | || sysinfo->dimm[1] != SYSINFO_DIMM_NOT_POPULATED) |
| 304 | if (receive_enable_autoconfig(0, sysinfo)) |
| 305 | return; |
| 306 | |
| 307 | /* Is channel 1 populated? */ |
| 308 | if (sysinfo->dimm[2] != SYSINFO_DIMM_NOT_POPULATED |
| 309 | || sysinfo->dimm[3] != SYSINFO_DIMM_NOT_POPULATED) |
| 310 | if (receive_enable_autoconfig(0x80, sysinfo)) |
| 311 | return; |
| 312 | } |