846af4f080dbe967782e6292b2c45561193b68e3
[coreboot.git] / src / northbridge / intel / i945 / rcven.c
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
70         printk_spew("    set_receive_enable() medium=0x%x, coarse=0x%x\n", medium, coarse);
71
72         reg32 = MCHBAR32(C0DRT1 + channel_offset);
73         reg32 &= 0xf0ffffff;
74         reg32 |= ((u32)coarse & 0x0f) << 24;
75         MCHBAR32(C0DRT1 + channel_offset) = reg32;
76
77         /* This should never happen: */
78         if (coarse > 0x0f)
79                 printk_debug("set_receive_enable: coarse overflow: 0x%02x.\n", coarse);
80
81         /* medium control
82          *
83          * 00 - 1/4 clock
84          * 01 - 1/2 clock
85          * 10 - 3/4 clock
86          * 11 - 1   clock
87          */
88
89         reg32 = MCHBAR32(RCVENMT);
90         if (!channel_offset) {
91                 /* Channel 0 */
92                 reg32 &= ~(3 << 2);
93                 reg32 |= medium << 2;
94         } else {
95                 /* Channel 1 */
96                 reg32 &= ~(3 << 0);
97                 reg32 |= medium;
98         }
99         MCHBAR32(RCVENMT) = reg32;
100
101 }
102
103 static int normalize(int channel_offset, u8 * mediumcoarse, u8 * fine)
104 {
105         printk_spew("  normalize()\n");
106
107         if (*fine < 0x80)
108                 return 0;
109
110         *fine -= 0x80;
111         *mediumcoarse += 1;
112
113         if (*mediumcoarse >= 0x40) {
114                 printk_debug("Normalize Error\n");
115                 return -1;
116         }
117
118         set_receive_enable(channel_offset, *mediumcoarse & 3,
119                            *mediumcoarse >> 2);
120
121         MCHBAR8(C0WL0REOST + channel_offset) = *fine;
122
123         return 0;
124 }
125
126 static int find_preamble(int channel_offset, u8 * mediumcoarse,
127                          struct sys_info *sysinfo)
128 {
129         /* find start of the data phase */
130         u32 reg32;
131
132         printk_spew("  find_preamble()\n");
133
134         do {
135                 if (*mediumcoarse < 4) {
136                         printk_debug("No Preamble found.\n");
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                 printk_debug("No Preamble found (neither high nor low).\n");
150                 return -1;
151         }
152
153         return 0;
154 }
155
156 /**
157  * add a quarter clock to the current receive enable settings
158  */
159
160 static int add_quarter_clock(int channel_offset, u8 * mediumcoarse, u8 * fine)
161 {
162         printk_spew("  add_quarter_clock() mediumcoarse=%02x fine=%02x\n",
163                         *mediumcoarse, *fine);
164         if (*fine >= 0x80) {
165                 *fine -= 0x80;
166
167                 *mediumcoarse += 2;
168                 if (*mediumcoarse >= 0x40) {
169                         printk_debug("clocks at max.\n");
170                         return -1;
171                 }
172
173                 set_receive_enable(channel_offset, *mediumcoarse & 3,
174                                    *mediumcoarse >> 2);
175         } else {
176                 *fine += 0x80;
177         }
178
179         MCHBAR8(C0WL0REOST + channel_offset) = *fine;
180
181         return 0;
182 }
183
184 static int find_strobes_low(int channel_offset, u8 * mediumcoarse, u8 * fine,
185                             struct sys_info *sysinfo)
186 {
187         u32 rcvenmt;
188
189         printk_spew("  find_strobes_low()\n");
190
191         for (;;) {
192                 MCHBAR8(C0WL0REOST + channel_offset) = *fine;
193
194                 set_receive_enable(channel_offset, *mediumcoarse & 3,
195                                    *mediumcoarse >> 2);
196
197                 rcvenmt = sample_strobes(channel_offset, sysinfo);
198
199                 if (((rcvenmt & (1 << 18)) != 0))
200                         return 0;
201
202                 *fine -= 0x80;
203                 if (*fine == 0)
204                         continue;
205
206                 *mediumcoarse -= 2;
207                 if (*mediumcoarse < 0xfe)
208                         continue;
209
210                 break;
211
212         }
213
214         printk_debug("Could not find low strobe\n");
215         return 0;
216 }
217
218 static int find_strobes_edge(int channel_offset, u8 * mediumcoarse, u8 * fine,
219                              struct sys_info *sysinfo)
220 {
221
222         int counter;
223         u32 rcvenmt;
224
225         printk_spew("  find_strobes_edge()\n");
226
227         counter = 8;
228         set_receive_enable(channel_offset, *mediumcoarse & 3,
229                            *mediumcoarse >> 2);
230
231         for (;;) {
232                 MCHBAR8(C0WL0REOST + channel_offset) = *fine;
233                 rcvenmt = sample_strobes(channel_offset, sysinfo);
234
235                 if ((rcvenmt & (1 << 19)) == 0) {
236                         counter = 8;
237                 } else {
238                         counter--;
239                         if (!counter)
240                                 break;
241                 }
242
243                 *fine = *fine + 1;
244                 if (*fine < 0xf8) {
245                         if (*fine & (1 << 3)) {
246                                 *fine &= ~(1 << 3);
247                                 *fine += 0x10;
248                         }
249                         continue;
250                 }
251
252                 *fine = 0;
253                 *mediumcoarse += 2;
254                 if (*mediumcoarse <= 0x40) {
255                         set_receive_enable(channel_offset, *mediumcoarse & 3,
256                                            *mediumcoarse >> 2);
257                         continue;
258                 }
259
260                 printk_debug("Could not find rising edge.\n");
261                 return -1;
262         }
263
264         *fine -= 7;
265         if (*fine >= 0xf9) {
266                 *mediumcoarse -= 2;
267                 set_receive_enable(channel_offset, *mediumcoarse & 3,
268                                    *mediumcoarse >> 2);
269         }
270
271         *fine &= ~(1 << 3);
272         MCHBAR8(C0WL0REOST + channel_offset) = *fine;
273
274         return 0;
275 }
276
277 /**
278  * Here we use a trick. The RCVEN channel 0 registers are all at an
279  * offset of 0x80 to the channel 0 registers. We don't want to waste
280  * a lot of if()s so let's just pass 0 or 0x80 for the channel offset.
281  */
282
283 static int receive_enable_autoconfig(int channel_offset,
284                                      struct sys_info *sysinfo)
285 {
286         u8 mediumcoarse;
287         u8 fine;
288
289         printk_spew("receive_enable_autoconfig() for channel %d\n",
290                     channel_offset ? 1 : 0);
291
292         /* Set initial values */
293         mediumcoarse = (sysinfo->cas << 2) | 3;
294         fine = 0;
295
296         if (find_strobes_low(channel_offset, &mediumcoarse, &fine, sysinfo))
297                 return -1;
298
299         if (find_strobes_edge(channel_offset, &mediumcoarse, &fine, sysinfo))
300                 return -1;
301
302         if (add_quarter_clock(channel_offset, &mediumcoarse, &fine))
303                 return -1;
304
305         if (find_preamble(channel_offset, &mediumcoarse, sysinfo))
306                 return -1;
307
308         if (add_quarter_clock(channel_offset, &mediumcoarse, &fine))
309                 return -1;
310
311         if (normalize(channel_offset, &mediumcoarse, &fine))
312                 return -1;
313
314         /* This is a debug check to see if the rcven code is fully working.
315          * It can be removed when the output message is not printed anymore
316          */
317         if (MCHBAR8(C0WL0REOST + channel_offset) == 0) {
318                 printk_debug("Weird. No C%sWL0REOST\n", channel_offset?"1":"0");
319         }
320
321         return 0;
322 }
323
324 void receive_enable_adjust(struct sys_info *sysinfo)
325 {
326         /* Is channel 0 populated? */
327         if (sysinfo->dimm[0] != SYSINFO_DIMM_NOT_POPULATED
328             || sysinfo->dimm[1] != SYSINFO_DIMM_NOT_POPULATED)
329                 if (receive_enable_autoconfig(0, sysinfo))
330                         return;
331
332         /* Is channel 1 populated? */
333         if (sysinfo->dimm[2] != SYSINFO_DIMM_NOT_POPULATED
334             || sysinfo->dimm[3] != SYSINFO_DIMM_NOT_POPULATED)
335                 if (receive_enable_autoconfig(0x80, sysinfo))
336                         return;
337 }
338