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