Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2007-2008 coresystems GmbH |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "raminit.h" |
| 21 | |
| 22 | /** |
| 23 | * sample the strobes signal |
| 24 | */ |
| 25 | static u32 sample_strobes(int channel_offset, struct sys_info *sysinfo) |
| 26 | { |
| 27 | u32 reg32, addr; |
| 28 | int i; |
| 29 | |
| 30 | MCHBAR32(C0DRC1 + channel_offset) |= (1 << 6); |
| 31 | |
| 32 | MCHBAR32(C0DRC1 + channel_offset) &= ~(1 << 6); |
| 33 | |
| 34 | addr = 0; |
| 35 | |
| 36 | if (channel_offset != 0) { /* must be dual channel */ |
| 37 | if (sysinfo->interleaved == 1) { |
| 38 | addr |= (1 << 6); |
| 39 | } else { |
| 40 | addr = ((u32)MCHBAR8(C0DRB3)) << 25; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | for (i = 0; i < 28; i++) { |
| 45 | read32(addr); |
| 46 | read32(addr + 0x80); |
| 47 | } |
| 48 | |
| 49 | reg32 = MCHBAR32(RCVENMT); |
| 50 | if (channel_offset == 0) { |
| 51 | reg32 = reg32 << 2; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * [19] = 1: all bits are high |
| 56 | * [18] = 1: all bits are low |
| 57 | * [19:18] = 00: bits are mixed high, low |
| 58 | */ |
| 59 | return reg32; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * This function sets receive enable coarse and medium timing parameters |
| 64 | */ |
| 65 | |
| 66 | static void set_receive_enable(int channel_offset, u8 medium, u8 coarse) |
| 67 | { |
| 68 | u32 reg32; |
| 69 | |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 70 | printk_spew(" set_receive_enable() medium=0x%x, coarse=0x%x\n", medium, coarse); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 71 | |
| 72 | reg32 = MCHBAR32(C0DRT1 + channel_offset); |
| 73 | reg32 &= 0xf0ffffff; |
| 74 | reg32 |= ((u32)coarse & 0x0f) << 24; |
| 75 | MCHBAR32(C0DRT1 + channel_offset) = reg32; |
| 76 | |
| 77 | if (coarse > 0x0f) |
| 78 | printk_debug("set_receive_enable: coarse overflow: 0x%02x.\n", coarse); |
| 79 | |
| 80 | /* medium control |
| 81 | * |
| 82 | * 00 - 1/4 clock |
| 83 | * 01 - 1/2 clock |
| 84 | * 10 - 3/4 clock |
| 85 | * 11 - 1 clock |
| 86 | */ |
| 87 | |
| 88 | reg32 = MCHBAR32(RCVENMT); |
| 89 | if (!channel_offset) { |
| 90 | |
| 91 | reg32 &= ~(3 << 2); |
| 92 | reg32 |= medium << 2; |
| 93 | } else { |
| 94 | |
| 95 | reg32 &= ~(3 << 0); |
| 96 | reg32 |= medium; |
| 97 | } |
| 98 | MCHBAR32(RCVENMT) = reg32; |
| 99 | |
| 100 | } |
| 101 | |
| 102 | static int normalize(int channel_offset, u8 * mediumcoarse, u8 * fine) |
| 103 | { |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 104 | printk_spew(" normalize()\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 105 | |
| 106 | if (*fine < 0x80) |
| 107 | return 0; |
| 108 | |
| 109 | *fine -= 0x80; |
| 110 | *mediumcoarse += 1; |
| 111 | |
| 112 | if (*mediumcoarse >= 0x40) { |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 113 | printk_debug("Normalize Error\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 118 | *mediumcoarse >> 2); |
| 119 | |
| 120 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int find_preamble(int channel_offset, u8 * mediumcoarse, |
| 126 | struct sys_info *sysinfo) |
| 127 | { |
| 128 | /* find start of the data phase */ |
| 129 | u32 reg32; |
| 130 | |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 131 | printk_spew(" find_preamble()\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 132 | |
| 133 | do { |
| 134 | if (*mediumcoarse < 4) { |
| 135 | |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 136 | printk_debug("No Preamble found.\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 137 | return -1; |
| 138 | } |
| 139 | *mediumcoarse -= 4; |
| 140 | |
| 141 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 142 | *mediumcoarse >> 2); |
| 143 | |
| 144 | reg32 = sample_strobes(channel_offset, sysinfo); |
| 145 | |
| 146 | } while (reg32 & (1 << 19)); |
| 147 | |
| 148 | if (!(reg32 & (1 << 18))) { |
| 149 | |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 150 | printk_debug("No Preamble found (neither high nor low).\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * add a quarter clock to the current receive enable settings |
| 159 | */ |
| 160 | |
| 161 | static int add_quarter_clock(int channel_offset, u8 * mediumcoarse, u8 * fine) |
| 162 | { |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 163 | printk_spew(" add_quarter_clock() mediumcoarse=%02x fine=%02x\n", |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 164 | *mediumcoarse, *fine); |
| 165 | if (*fine >= 0x80) { |
| 166 | *fine -= 0x80; |
| 167 | |
| 168 | *mediumcoarse += 2; |
| 169 | if (*mediumcoarse >= 0x40) { |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 170 | printk_debug("clocks at max.\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 175 | *mediumcoarse >> 2); |
| 176 | } else { |
| 177 | *fine += 0x80; |
| 178 | } |
| 179 | |
| 180 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int find_strobes_low(int channel_offset, u8 * mediumcoarse, u8 * fine, |
| 186 | struct sys_info *sysinfo) |
| 187 | { |
| 188 | u32 rcvenmt; |
| 189 | |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 190 | printk_spew(" find_strobes_low()\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 191 | |
| 192 | for (;;) { |
| 193 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 194 | |
| 195 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 196 | *mediumcoarse >> 2); |
| 197 | |
| 198 | rcvenmt = sample_strobes(channel_offset, sysinfo); |
| 199 | |
| 200 | if (((rcvenmt & (1 << 18)) != 0)) |
| 201 | return 0; |
| 202 | |
| 203 | |
| 204 | *fine -= 0x80; |
| 205 | if (*fine == 0) |
| 206 | continue; |
| 207 | |
| 208 | *mediumcoarse -= 2; |
| 209 | if (*mediumcoarse < 0xfe) |
| 210 | continue; |
| 211 | |
| 212 | break; |
| 213 | |
| 214 | } |
| 215 | |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 216 | printk_debug("Could not find low strobe\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int find_strobes_edge(int channel_offset, u8 * mediumcoarse, u8 * fine, |
| 221 | struct sys_info *sysinfo) |
| 222 | { |
| 223 | int counter; |
| 224 | u32 rcvenmt; |
| 225 | |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 226 | printk_spew(" find_strobes_edge()\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 227 | |
| 228 | counter = 8; |
| 229 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 230 | *mediumcoarse >> 2); |
| 231 | |
| 232 | for (;;) { |
| 233 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 234 | rcvenmt = sample_strobes(channel_offset, sysinfo); |
| 235 | |
| 236 | if ((rcvenmt & (1 << 19)) == 0) { |
| 237 | counter = 8; |
| 238 | } else { |
| 239 | counter--; |
| 240 | if (!counter) |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | *fine = *fine + 1; |
| 245 | if (*fine < 0xf8) { |
| 246 | if (*fine & (1 << 3)) { |
| 247 | *fine &= ~(1 << 3); |
| 248 | *fine += 0x10; |
| 249 | } |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | *fine = 0; |
| 254 | *mediumcoarse += 2; |
| 255 | if (*mediumcoarse <= 0x40) { |
| 256 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 257 | *mediumcoarse >> 2); |
| 258 | continue; |
| 259 | } |
| 260 | |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 261 | printk_debug("could not find rising edge.\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | *fine -= 7; |
| 266 | if (*fine >= 0xf9) { |
| 267 | *mediumcoarse -= 2; |
| 268 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 269 | *mediumcoarse >> 2); |
| 270 | } |
| 271 | |
| 272 | *fine &= ~(1 << 3); |
| 273 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Here we use a trick. The RCVEN channel 0 registers are all at an |
| 280 | * offset of 0x80 to the channel 0 registers. We don't want to waste |
| 281 | * a lot of if()s so let's just pass 0 or 0x80 for the channel offset. |
| 282 | */ |
| 283 | |
| 284 | static int receive_enable_autoconfig(int channel_offset, |
| 285 | struct sys_info *sysinfo) |
| 286 | { |
| 287 | u8 mediumcoarse; |
| 288 | u8 fine; |
| 289 | |
Stefan Reinauer | 779b3e3 | 2008-11-10 15:43:37 +0000 | [diff] [blame^] | 290 | printk_spew("receive_enable_autoconfig() for channel %d\n", |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 291 | channel_offset ? 1 : 0); |
| 292 | |
| 293 | /* Set initial values */ |
| 294 | mediumcoarse = (sysinfo->cas << 2) | 3; |
| 295 | fine = 0; |
| 296 | |
| 297 | if (find_strobes_low(channel_offset, &mediumcoarse, &fine, sysinfo)) |
| 298 | return -1; |
| 299 | |
| 300 | if (find_strobes_edge(channel_offset, &mediumcoarse, &fine, sysinfo)) |
| 301 | return -1; |
| 302 | |
| 303 | if (add_quarter_clock(channel_offset, &mediumcoarse, &fine)) |
| 304 | return -1; |
| 305 | |
| 306 | if (find_preamble(channel_offset, &mediumcoarse, sysinfo)) |
| 307 | return -1; |
| 308 | |
| 309 | if (add_quarter_clock(channel_offset, &mediumcoarse, &fine)) |
| 310 | return -1; |
| 311 | |
| 312 | if (normalize(channel_offset, &mediumcoarse, &fine)) |
| 313 | return -1; |
| 314 | |
| 315 | /* This is a debug check to see if the rcven code is fully working. |
| 316 | * It can be removed when the output message is not printed anymore |
| 317 | */ |
| 318 | if (MCHBAR8(C0WL0REOST + channel_offset) == 0) { |
| 319 | printk_debug("Weird. No C%sWL0REOST\n", channel_offset?"1":"0"); |
| 320 | } |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | void receive_enable_adjust(struct sys_info *sysinfo) |
| 326 | { |
| 327 | /* Is channel 0 populated? */ |
| 328 | if (sysinfo->dimm[0] != SYSINFO_DIMM_NOT_POPULATED |
| 329 | || sysinfo->dimm[1] != SYSINFO_DIMM_NOT_POPULATED) |
| 330 | if (receive_enable_autoconfig(0, sysinfo)) |
| 331 | return; |
| 332 | |
| 333 | /* Is channel 1 populated? */ |
| 334 | if (sysinfo->dimm[2] != SYSINFO_DIMM_NOT_POPULATED |
| 335 | || sysinfo->dimm[3] != SYSINFO_DIMM_NOT_POPULATED) |
| 336 | if (receive_enable_autoconfig(0x80, sysinfo)) |
| 337 | return; |
| 338 | } |