]> git.sur5r.net Git - u-boot/blob - arch/x86/cpu/ivybridge/sdram.c
x86: Create a common header for Intel register access
[u-boot] / arch / x86 / cpu / ivybridge / sdram.c
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * (C) Copyright 2010,2011
4  * Graeme Russ, <graeme.russ@gmail.com>
5  *
6  * Portions from Coreboot mainboard/google/link/romstage.c
7  * Copyright (C) 2007-2010 coresystems GmbH
8  * Copyright (C) 2011 Google Inc.
9  *
10  * SPDX-License-Identifier:     GPL-2.0
11  */
12
13 #include <common.h>
14 #include <errno.h>
15 #include <fdtdec.h>
16 #include <malloc.h>
17 #include <net.h>
18 #include <rtc.h>
19 #include <spi.h>
20 #include <spi_flash.h>
21 #include <syscon.h>
22 #include <asm/cpu.h>
23 #include <asm/processor.h>
24 #include <asm/gpio.h>
25 #include <asm/global_data.h>
26 #include <asm/intel_regs.h>
27 #include <asm/mrccache.h>
28 #include <asm/mtrr.h>
29 #include <asm/pci.h>
30 #include <asm/arch/me.h>
31 #include <asm/arch/pei_data.h>
32 #include <asm/arch/pch.h>
33 #include <asm/post.h>
34 #include <asm/arch/sandybridge.h>
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 #define CMOS_OFFSET_MRC_SEED            152
39 #define CMOS_OFFSET_MRC_SEED_S3         156
40 #define CMOS_OFFSET_MRC_SEED_CHK        160
41
42 /*
43  * This function looks for the highest region of memory lower than 4GB which
44  * has enough space for U-Boot where U-Boot is aligned on a page boundary.
45  * It overrides the default implementation found elsewhere which simply
46  * picks the end of ram, wherever that may be. The location of the stack,
47  * the relocation address, and how far U-Boot is moved by relocation are
48  * set in the global data structure.
49  */
50 ulong board_get_usable_ram_top(ulong total_size)
51 {
52         struct memory_info *info = &gd->arch.meminfo;
53         uintptr_t dest_addr = 0;
54         struct memory_area *largest = NULL;
55         int i;
56
57         /* Find largest area of memory below 4GB */
58
59         for (i = 0; i < info->num_areas; i++) {
60                 struct memory_area *area = &info->area[i];
61
62                 if (area->start >= 1ULL << 32)
63                         continue;
64                 if (!largest || area->size > largest->size)
65                         largest = area;
66         }
67
68         /* If no suitable area was found, return an error. */
69         assert(largest);
70         if (!largest || largest->size < (2 << 20))
71                 panic("No available memory found for relocation");
72
73         dest_addr = largest->start + largest->size;
74
75         return (ulong)dest_addr;
76 }
77
78 void dram_init_banksize(void)
79 {
80         struct memory_info *info = &gd->arch.meminfo;
81         int num_banks;
82         int i;
83
84         for (i = 0, num_banks = 0; i < info->num_areas; i++) {
85                 struct memory_area *area = &info->area[i];
86
87                 if (area->start >= 1ULL << 32)
88                         continue;
89                 gd->bd->bi_dram[num_banks].start = area->start;
90                 gd->bd->bi_dram[num_banks].size = area->size;
91                 num_banks++;
92         }
93 }
94
95 static int read_seed_from_cmos(struct pei_data *pei_data)
96 {
97         u16 c1, c2, checksum, seed_checksum;
98         struct udevice *dev;
99         int ret = 0;
100
101         ret = uclass_get_device(UCLASS_RTC, 0, &dev);
102         if (ret) {
103                 debug("Cannot find RTC: err=%d\n", ret);
104                 return -ENODEV;
105         }
106
107         /*
108          * Read scrambler seeds from CMOS RAM. We don't want to store them in
109          * SPI flash since they change on every boot and that would wear down
110          * the flash too much. So we store these in CMOS and the large MRC
111          * data in SPI flash.
112          */
113         ret = rtc_read32(dev, CMOS_OFFSET_MRC_SEED, &pei_data->scrambler_seed);
114         if (!ret) {
115                 ret = rtc_read32(dev, CMOS_OFFSET_MRC_SEED_S3,
116                                  &pei_data->scrambler_seed_s3);
117         }
118         if (ret) {
119                 debug("Failed to read from RTC %s\n", dev->name);
120                 return ret;
121         }
122
123         debug("Read scrambler seed    0x%08x from CMOS 0x%02x\n",
124               pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
125         debug("Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
126               pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
127
128         /* Compute seed checksum and compare */
129         c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed,
130                                  sizeof(u32));
131         c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3,
132                                  sizeof(u32));
133         checksum = add_ip_checksums(sizeof(u32), c1, c2);
134
135         seed_checksum = rtc_read8(dev, CMOS_OFFSET_MRC_SEED_CHK);
136         seed_checksum |= rtc_read8(dev, CMOS_OFFSET_MRC_SEED_CHK + 1) << 8;
137
138         if (checksum != seed_checksum) {
139                 debug("%s: invalid seed checksum\n", __func__);
140                 pei_data->scrambler_seed = 0;
141                 pei_data->scrambler_seed_s3 = 0;
142                 return -EINVAL;
143         }
144
145         return 0;
146 }
147
148 static int prepare_mrc_cache(struct pei_data *pei_data)
149 {
150         struct mrc_data_container *mrc_cache;
151         struct mrc_region entry;
152         int ret;
153
154         ret = read_seed_from_cmos(pei_data);
155         if (ret)
156                 return ret;
157         ret = mrccache_get_region(NULL, &entry);
158         if (ret)
159                 return ret;
160         mrc_cache = mrccache_find_current(&entry);
161         if (!mrc_cache)
162                 return -ENOENT;
163
164         pei_data->mrc_input = mrc_cache->data;
165         pei_data->mrc_input_len = mrc_cache->data_size;
166         debug("%s: at %p, size %x checksum %04x\n", __func__,
167               pei_data->mrc_input, pei_data->mrc_input_len,
168               mrc_cache->checksum);
169
170         return 0;
171 }
172
173 static int write_seeds_to_cmos(struct pei_data *pei_data)
174 {
175         u16 c1, c2, checksum;
176         struct udevice *dev;
177         int ret = 0;
178
179         ret = uclass_get_device(UCLASS_RTC, 0, &dev);
180         if (ret) {
181                 debug("Cannot find RTC: err=%d\n", ret);
182                 return -ENODEV;
183         }
184
185         /* Save the MRC seed values to CMOS */
186         rtc_write32(dev, CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed);
187         debug("Save scrambler seed    0x%08x to CMOS 0x%02x\n",
188               pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
189
190         rtc_write32(dev, CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3);
191         debug("Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
192               pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
193
194         /* Save a simple checksum of the seed values */
195         c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed,
196                                  sizeof(u32));
197         c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3,
198                                  sizeof(u32));
199         checksum = add_ip_checksums(sizeof(u32), c1, c2);
200
201         rtc_write8(dev, CMOS_OFFSET_MRC_SEED_CHK, checksum & 0xff);
202         rtc_write8(dev, CMOS_OFFSET_MRC_SEED_CHK + 1, (checksum >> 8) & 0xff);
203
204         return 0;
205 }
206
207 /* Use this hook to save our SDRAM parameters */
208 int misc_init_r(void)
209 {
210         int ret;
211
212         ret = mrccache_save();
213         if (ret)
214                 printf("Unable to save MRC data: %d\n", ret);
215
216         return 0;
217 }
218
219 static const char *const ecc_decoder[] = {
220         "inactive",
221         "active on IO",
222         "disabled on IO",
223         "active"
224 };
225
226 /*
227  * Dump in the log memory controller configuration as read from the memory
228  * controller registers.
229  */
230 static void report_memory_config(void)
231 {
232         u32 addr_decoder_common, addr_decode_ch[2];
233         int i;
234
235         addr_decoder_common = readl(MCHBAR_REG(0x5000));
236         addr_decode_ch[0] = readl(MCHBAR_REG(0x5004));
237         addr_decode_ch[1] = readl(MCHBAR_REG(0x5008));
238
239         debug("memcfg DDR3 clock %d MHz\n",
240               (readl(MCHBAR_REG(0x5e04)) * 13333 * 2 + 50) / 100);
241         debug("memcfg channel assignment: A: %d, B % d, C % d\n",
242               addr_decoder_common & 3,
243               (addr_decoder_common >> 2) & 3,
244               (addr_decoder_common >> 4) & 3);
245
246         for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
247                 u32 ch_conf = addr_decode_ch[i];
248                 debug("memcfg channel[%d] config (%8.8x):\n", i, ch_conf);
249                 debug("   ECC %s\n", ecc_decoder[(ch_conf >> 24) & 3]);
250                 debug("   enhanced interleave mode %s\n",
251                       ((ch_conf >> 22) & 1) ? "on" : "off");
252                 debug("   rank interleave %s\n",
253                       ((ch_conf >> 21) & 1) ? "on" : "off");
254                 debug("   DIMMA %d MB width x%d %s rank%s\n",
255                       ((ch_conf >> 0) & 0xff) * 256,
256                       ((ch_conf >> 19) & 1) ? 16 : 8,
257                       ((ch_conf >> 17) & 1) ? "dual" : "single",
258                       ((ch_conf >> 16) & 1) ? "" : ", selected");
259                 debug("   DIMMB %d MB width x%d %s rank%s\n",
260                       ((ch_conf >> 8) & 0xff) * 256,
261                       ((ch_conf >> 20) & 1) ? 16 : 8,
262                       ((ch_conf >> 18) & 1) ? "dual" : "single",
263                       ((ch_conf >> 16) & 1) ? ", selected" : "");
264         }
265 }
266
267 static void post_system_agent_init(struct pei_data *pei_data)
268 {
269         /* If PCIe init is skipped, set the PEG clock gating */
270         if (!pei_data->pcie_init)
271                 setbits_le32(MCHBAR_REG(0x7010), 1);
272 }
273
274 static asmlinkage void console_tx_byte(unsigned char byte)
275 {
276 #ifdef DEBUG
277         putc(byte);
278 #endif
279 }
280
281 static int recovery_mode_enabled(void)
282 {
283         return false;
284 }
285
286 /**
287  * Find the PEI executable in the ROM and execute it.
288  *
289  * @dev: Northbridge device
290  * @pei_data: configuration data for UEFI PEI reference code
291  */
292 int sdram_initialise(struct udevice *dev, struct udevice *me_dev,
293                      struct pei_data *pei_data)
294 {
295         unsigned version;
296         const char *data;
297         uint16_t done;
298         int ret;
299
300         report_platform_info(dev);
301
302         /* Wait for ME to be ready */
303         ret = intel_early_me_init(me_dev);
304         if (ret)
305                 return ret;
306         ret = intel_early_me_uma_size(me_dev);
307         if (ret < 0)
308                 return ret;
309
310         debug("Starting UEFI PEI System Agent\n");
311
312         /*
313          * Do not pass MRC data in for recovery mode boot,
314          * Always pass it in for S3 resume.
315          */
316         if (!recovery_mode_enabled() ||
317             pei_data->boot_mode == PEI_BOOT_RESUME) {
318                 ret = prepare_mrc_cache(pei_data);
319                 if (ret)
320                         debug("prepare_mrc_cache failed: %d\n", ret);
321         }
322
323         /* If MRC data is not found we cannot continue S3 resume. */
324         if (pei_data->boot_mode == PEI_BOOT_RESUME && !pei_data->mrc_input) {
325                 debug("Giving up in sdram_initialize: No MRC data\n");
326                 reset_cpu(0);
327         }
328
329         /* Pass console handler in pei_data */
330         pei_data->tx_byte = console_tx_byte;
331
332         debug("PEI data at %p, size %x:\n", pei_data, sizeof(*pei_data));
333
334         data = (char *)CONFIG_X86_MRC_ADDR;
335         if (data) {
336                 int rv;
337                 int (*func)(struct pei_data *);
338                 ulong start;
339
340                 debug("Calling MRC at %p\n", data);
341                 post_code(POST_PRE_MRC);
342                 start = get_timer(0);
343                 func = (int (*)(struct pei_data *))data;
344                 rv = func(pei_data);
345                 post_code(POST_MRC);
346                 if (rv) {
347                         switch (rv) {
348                         case -1:
349                                 printf("PEI version mismatch.\n");
350                                 break;
351                         case -2:
352                                 printf("Invalid memory frequency.\n");
353                                 break;
354                         default:
355                                 printf("MRC returned %x.\n", rv);
356                         }
357                         printf("Nonzero MRC return value.\n");
358                         return -EFAULT;
359                 }
360                 debug("MRC execution time %lu ms\n", get_timer(start));
361         } else {
362                 printf("UEFI PEI System Agent not found.\n");
363                 return -ENOSYS;
364         }
365
366 #if CONFIG_USBDEBUG
367         /* mrc.bin reconfigures USB, so reinit it to have debug */
368         early_usbdebug_init();
369 #endif
370
371         version = readl(MCHBAR_REG(0x5034));
372         debug("System Agent Version %d.%d.%d Build %d\n",
373               version >> 24 , (version >> 16) & 0xff,
374               (version >> 8) & 0xff, version & 0xff);
375         debug("MRC output data length %#x at %p\n", pei_data->mrc_output_len,
376               pei_data->mrc_output);
377
378         /*
379          * Send ME init done for SandyBridge here.  This is done inside the
380          * SystemAgent binary on IvyBridge
381          */
382         dm_pci_read_config16(dev, PCI_DEVICE_ID, &done);
383         done &= BASE_REV_MASK;
384         if (BASE_REV_SNB == done)
385                 intel_early_me_init_done(dev, me_dev, ME_INIT_STATUS_SUCCESS);
386         else
387                 intel_early_me_status(me_dev);
388
389         post_system_agent_init(pei_data);
390         report_memory_config();
391
392         /* S3 resume: don't save scrambler seed or MRC data */
393         if (pei_data->boot_mode != PEI_BOOT_RESUME) {
394                 /*
395                  * This will be copied to SDRAM in reserve_arch(), then written
396                  * to SPI flash in mrccache_save()
397                  */
398                 gd->arch.mrc_output = (char *)pei_data->mrc_output;
399                 gd->arch.mrc_output_len = pei_data->mrc_output_len;
400                 ret = write_seeds_to_cmos(pei_data);
401                 if (ret)
402                         debug("Failed to write seeds to CMOS: %d\n", ret);
403         }
404
405         return 0;
406 }
407
408 int reserve_arch(void)
409 {
410         return mrccache_reserve();
411 }
412
413 static int copy_spd(struct pei_data *peid)
414 {
415         const int gpio_vector[] = {41, 42, 43, 10, -1};
416         int spd_index;
417         const void *blob = gd->fdt_blob;
418         int node, spd_node;
419         int ret, i;
420
421         for (i = 0; ; i++) {
422                 if (gpio_vector[i] == -1)
423                         break;
424                 ret = gpio_requestf(gpio_vector[i], "spd_id%d", i);
425                 if (ret) {
426                         debug("%s: Could not request gpio %d\n", __func__,
427                               gpio_vector[i]);
428                         return ret;
429                 }
430         }
431         spd_index = gpio_get_values_as_int(gpio_vector);
432         debug("spd index %d\n", spd_index);
433         node = fdtdec_next_compatible(blob, 0, COMPAT_MEMORY_SPD);
434         if (node < 0) {
435                 printf("SPD data not found.\n");
436                 return -ENOENT;
437         }
438
439         for (spd_node = fdt_first_subnode(blob, node);
440              spd_node > 0;
441              spd_node = fdt_next_subnode(blob, spd_node)) {
442                 const char *data;
443                 int len;
444
445                 if (fdtdec_get_int(blob, spd_node, "reg", -1) != spd_index)
446                         continue;
447                 data = fdt_getprop(blob, spd_node, "data", &len);
448                 if (len < sizeof(peid->spd_data[0])) {
449                         printf("Missing SPD data\n");
450                         return -EINVAL;
451                 }
452
453                 debug("Using SDRAM SPD data for '%s'\n",
454                       fdt_get_name(blob, spd_node, NULL));
455                 memcpy(peid->spd_data[0], data, sizeof(peid->spd_data[0]));
456                 break;
457         }
458
459         if (spd_node < 0) {
460                 printf("No SPD data found for index %d\n", spd_index);
461                 return -ENOENT;
462         }
463
464         return 0;
465 }
466
467 /**
468  * add_memory_area() - Add a new usable memory area to our list
469  *
470  * Note: @start and @end must not span the first 4GB boundary
471  *
472  * @info:       Place to store memory info
473  * @start:      Start of this memory area
474  * @end:        End of this memory area + 1
475  */
476 static int add_memory_area(struct memory_info *info,
477                            uint64_t start, uint64_t end)
478 {
479         struct memory_area *ptr;
480
481         if (info->num_areas == CONFIG_NR_DRAM_BANKS)
482                 return -ENOSPC;
483
484         ptr = &info->area[info->num_areas];
485         ptr->start = start;
486         ptr->size = end - start;
487         info->total_memory += ptr->size;
488         if (ptr->start < (1ULL << 32))
489                 info->total_32bit_memory += ptr->size;
490         debug("%d: memory %llx size %llx, total now %llx / %llx\n",
491               info->num_areas, ptr->start, ptr->size,
492               info->total_32bit_memory, info->total_memory);
493         info->num_areas++;
494
495         return 0;
496 }
497
498 /**
499  * sdram_find() - Find available memory
500  *
501  * This is a bit complicated since on x86 there are system memory holes all
502  * over the place. We create a list of available memory blocks
503  *
504  * @dev:        Northbridge device
505  */
506 static int sdram_find(struct udevice *dev)
507 {
508         struct memory_info *info = &gd->arch.meminfo;
509         uint32_t tseg_base, uma_size, tolud;
510         uint64_t tom, me_base, touud;
511         uint64_t uma_memory_base = 0;
512         uint64_t uma_memory_size;
513         unsigned long long tomk;
514         uint16_t ggc;
515         u32 val;
516
517         /* Total Memory 2GB example:
518          *
519          *  00000000  0000MB-1992MB  1992MB  RAM     (writeback)
520          *  7c800000  1992MB-2000MB     8MB  TSEG    (SMRR)
521          *  7d000000  2000MB-2002MB     2MB  GFX GTT (uncached)
522          *  7d200000  2002MB-2034MB    32MB  GFX UMA (uncached)
523          *  7f200000   2034MB TOLUD
524          *  7f800000   2040MB MEBASE
525          *  7f800000  2040MB-2048MB     8MB  ME UMA  (uncached)
526          *  80000000   2048MB TOM
527          * 100000000  4096MB-4102MB     6MB  RAM     (writeback)
528          *
529          * Total Memory 4GB example:
530          *
531          *  00000000  0000MB-2768MB  2768MB  RAM     (writeback)
532          *  ad000000  2768MB-2776MB     8MB  TSEG    (SMRR)
533          *  ad800000  2776MB-2778MB     2MB  GFX GTT (uncached)
534          *  ada00000  2778MB-2810MB    32MB  GFX UMA (uncached)
535          *  afa00000   2810MB TOLUD
536          *  ff800000   4088MB MEBASE
537          *  ff800000  4088MB-4096MB     8MB  ME UMA  (uncached)
538          * 100000000   4096MB TOM
539          * 100000000  4096MB-5374MB  1278MB  RAM     (writeback)
540          * 14fe00000   5368MB TOUUD
541          */
542
543         /* Top of Upper Usable DRAM, including remap */
544         dm_pci_read_config32(dev, TOUUD + 4, &val);
545         touud = (uint64_t)val << 32;
546         dm_pci_read_config32(dev, TOUUD, &val);
547         touud |= val;
548
549         /* Top of Lower Usable DRAM */
550         dm_pci_read_config32(dev, TOLUD, &tolud);
551
552         /* Top of Memory - does not account for any UMA */
553         dm_pci_read_config32(dev, 0xa4, &val);
554         tom = (uint64_t)val << 32;
555         dm_pci_read_config32(dev, 0xa0, &val);
556         tom |= val;
557
558         debug("TOUUD %llx TOLUD %08x TOM %llx\n", touud, tolud, tom);
559
560         /* ME UMA needs excluding if total memory <4GB */
561         dm_pci_read_config32(dev, 0x74, &val);
562         me_base = (uint64_t)val << 32;
563         dm_pci_read_config32(dev, 0x70, &val);
564         me_base |= val;
565
566         debug("MEBASE %llx\n", me_base);
567
568         /* TODO: Get rid of all this shifting by 10 bits */
569         tomk = tolud >> 10;
570         if (me_base == tolud) {
571                 /* ME is from MEBASE-TOM */
572                 uma_size = (tom - me_base) >> 10;
573                 /* Increment TOLUD to account for ME as RAM */
574                 tolud += uma_size << 10;
575                 /* UMA starts at old TOLUD */
576                 uma_memory_base = tomk * 1024ULL;
577                 uma_memory_size = uma_size * 1024ULL;
578                 debug("ME UMA base %llx size %uM\n", me_base, uma_size >> 10);
579         }
580
581         /* Graphics memory comes next */
582         dm_pci_read_config16(dev, GGC, &ggc);
583         if (!(ggc & 2)) {
584                 debug("IGD decoded, subtracting ");
585
586                 /* Graphics memory */
587                 uma_size = ((ggc >> 3) & 0x1f) * 32 * 1024ULL;
588                 debug("%uM UMA", uma_size >> 10);
589                 tomk -= uma_size;
590                 uma_memory_base = tomk * 1024ULL;
591                 uma_memory_size += uma_size * 1024ULL;
592
593                 /* GTT Graphics Stolen Memory Size (GGMS) */
594                 uma_size = ((ggc >> 8) & 0x3) * 1024ULL;
595                 tomk -= uma_size;
596                 uma_memory_base = tomk * 1024ULL;
597                 uma_memory_size += uma_size * 1024ULL;
598                 debug(" and %uM GTT\n", uma_size >> 10);
599         }
600
601         /* Calculate TSEG size from its base which must be below GTT */
602         dm_pci_read_config32(dev, 0xb8, &tseg_base);
603         uma_size = (uma_memory_base - tseg_base) >> 10;
604         tomk -= uma_size;
605         uma_memory_base = tomk * 1024ULL;
606         uma_memory_size += uma_size * 1024ULL;
607         debug("TSEG base 0x%08x size %uM\n", tseg_base, uma_size >> 10);
608
609         debug("Available memory below 4GB: %lluM\n", tomk >> 10);
610
611         /* Report the memory regions */
612         add_memory_area(info, 1 << 20, 2 << 28);
613         add_memory_area(info, (2 << 28) + (2 << 20), 4 << 28);
614         add_memory_area(info, (4 << 28) + (2 << 20), tseg_base);
615         add_memory_area(info, 1ULL << 32, touud);
616
617         /* Add MTRRs for memory */
618         mtrr_add_request(MTRR_TYPE_WRBACK, 0, 2ULL << 30);
619         mtrr_add_request(MTRR_TYPE_WRBACK, 2ULL << 30, 512 << 20);
620         mtrr_add_request(MTRR_TYPE_WRBACK, 0xaULL << 28, 256 << 20);
621         mtrr_add_request(MTRR_TYPE_UNCACHEABLE, tseg_base, 16 << 20);
622         mtrr_add_request(MTRR_TYPE_UNCACHEABLE, tseg_base + (16 << 20),
623                          32 << 20);
624
625         /*
626          * If >= 4GB installed then memory from TOLUD to 4GB
627          * is remapped above TOM, TOUUD will account for both
628          */
629         if (touud > (1ULL << 32ULL)) {
630                 debug("Available memory above 4GB: %lluM\n",
631                       (touud >> 20) - 4096);
632         }
633
634         return 0;
635 }
636
637 static void rcba_config(void)
638 {
639         /*
640          *             GFX    INTA -> PIRQA (MSI)
641          * D28IP_P3IP  WLAN   INTA -> PIRQB
642          * D29IP_E1P   EHCI1  INTA -> PIRQD
643          * D26IP_E2P   EHCI2  INTA -> PIRQF
644          * D31IP_SIP   SATA   INTA -> PIRQF (MSI)
645          * D31IP_SMIP  SMBUS  INTB -> PIRQH
646          * D31IP_TTIP  THRT   INTC -> PIRQA
647          * D27IP_ZIP   HDA    INTA -> PIRQA (MSI)
648          *
649          * TRACKPAD                -> PIRQE (Edge Triggered)
650          * TOUCHSCREEN             -> PIRQG (Edge Triggered)
651          */
652
653         /* Device interrupt pin register (board specific) */
654         writel((INTC << D31IP_TTIP) | (NOINT << D31IP_SIP2) |
655                (INTB << D31IP_SMIP) | (INTA << D31IP_SIP), RCB_REG(D31IP));
656         writel(NOINT << D30IP_PIP, RCB_REG(D30IP));
657         writel(INTA << D29IP_E1P, RCB_REG(D29IP));
658         writel(INTA << D28IP_P3IP, RCB_REG(D28IP));
659         writel(INTA << D27IP_ZIP, RCB_REG(D27IP));
660         writel(INTA << D26IP_E2P, RCB_REG(D26IP));
661         writel(NOINT << D25IP_LIP, RCB_REG(D25IP));
662         writel(NOINT << D22IP_MEI1IP, RCB_REG(D22IP));
663
664         /* Device interrupt route registers */
665         writel(DIR_ROUTE(PIRQB, PIRQH, PIRQA, PIRQC), RCB_REG(D31IR));
666         writel(DIR_ROUTE(PIRQD, PIRQE, PIRQF, PIRQG), RCB_REG(D29IR));
667         writel(DIR_ROUTE(PIRQB, PIRQC, PIRQD, PIRQE), RCB_REG(D28IR));
668         writel(DIR_ROUTE(PIRQA, PIRQH, PIRQA, PIRQB), RCB_REG(D27IR));
669         writel(DIR_ROUTE(PIRQF, PIRQE, PIRQG, PIRQH), RCB_REG(D26IR));
670         writel(DIR_ROUTE(PIRQA, PIRQB, PIRQC, PIRQD), RCB_REG(D25IR));
671         writel(DIR_ROUTE(PIRQA, PIRQB, PIRQC, PIRQD), RCB_REG(D22IR));
672
673         /* Enable IOAPIC (generic) */
674         writew(0x0100, RCB_REG(OIC));
675         /* PCH BWG says to read back the IOAPIC enable register */
676         (void)readw(RCB_REG(OIC));
677
678         /* Disable unused devices (board specific) */
679         setbits_le32(RCB_REG(FD), PCH_DISABLE_ALWAYS);
680 }
681
682 int dram_init(void)
683 {
684         struct pei_data pei_data __aligned(8) = {
685                 .pei_version = PEI_VERSION,
686                 .mchbar = MCH_BASE_ADDRESS,
687                 .dmibar = DEFAULT_DMIBAR,
688                 .epbar = DEFAULT_EPBAR,
689                 .pciexbar = CONFIG_PCIE_ECAM_BASE,
690                 .smbusbar = SMBUS_IO_BASE,
691                 .wdbbar = 0x4000000,
692                 .wdbsize = 0x1000,
693                 .hpet_address = CONFIG_HPET_ADDRESS,
694                 .rcba = DEFAULT_RCBABASE,
695                 .pmbase = DEFAULT_PMBASE,
696                 .gpiobase = DEFAULT_GPIOBASE,
697                 .thermalbase = 0xfed08000,
698                 .system_type = 0, /* 0 Mobile, 1 Desktop/Server */
699                 .tseg_size = CONFIG_SMM_TSEG_SIZE,
700                 .ts_addresses = { 0x00, 0x00, 0x00, 0x00 },
701                 .ec_present = 1,
702                 .ddr3lv_support = 1,
703                 /*
704                  * 0 = leave channel enabled
705                  * 1 = disable dimm 0 on channel
706                  * 2 = disable dimm 1 on channel
707                  * 3 = disable dimm 0+1 on channel
708                  */
709                 .dimm_channel0_disabled = 2,
710                 .dimm_channel1_disabled = 2,
711                 .max_ddr3_freq = 1600,
712                 .usb_port_config = {
713                         /*
714                          * Empty and onboard Ports 0-7, set to un-used pin
715                          * OC3
716                          */
717                         { 0, 3, 0x0000 }, /* P0= Empty */
718                         { 1, 0, 0x0040 }, /* P1= Left USB 1  (OC0) */
719                         { 1, 1, 0x0040 }, /* P2= Left USB 2  (OC1) */
720                         { 1, 3, 0x0040 }, /* P3= SDCARD      (no OC) */
721                         { 0, 3, 0x0000 }, /* P4= Empty */
722                         { 1, 3, 0x0040 }, /* P5= WWAN        (no OC) */
723                         { 0, 3, 0x0000 }, /* P6= Empty */
724                         { 0, 3, 0x0000 }, /* P7= Empty */
725                         /*
726                          * Empty and onboard Ports 8-13, set to un-used pin
727                          * OC4
728                          */
729                         { 1, 4, 0x0040 }, /* P8= Camera      (no OC) */
730                         { 1, 4, 0x0040 }, /* P9= Bluetooth   (no OC) */
731                         { 0, 4, 0x0000 }, /* P10= Empty */
732                         { 0, 4, 0x0000 }, /* P11= Empty */
733                         { 0, 4, 0x0000 }, /* P12= Empty */
734                         { 0, 4, 0x0000 }, /* P13= Empty */
735                 },
736         };
737         struct udevice *dev, *me_dev;
738         int ret;
739
740         ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &dev);
741         if (ret)
742                 return ret;
743         ret = syscon_get_by_driver_data(X86_SYSCON_ME, &me_dev);
744         if (ret)
745                 return ret;
746         debug("Boot mode %d\n", gd->arch.pei_boot_mode);
747         debug("mrc_input %p\n", pei_data.mrc_input);
748         pei_data.boot_mode = gd->arch.pei_boot_mode;
749         ret = copy_spd(&pei_data);
750         if (!ret)
751                 ret = sdram_initialise(dev, me_dev, &pei_data);
752         if (ret)
753                 return ret;
754
755         rcba_config();
756         quick_ram_check();
757
758         writew(0xCAFE, MCHBAR_REG(SSKPD));
759
760         post_code(POST_DRAM);
761
762         ret = sdram_find(dev);
763         if (ret)
764                 return ret;
765
766         gd->ram_size = gd->arch.meminfo.total_32bit_memory;
767
768         return 0;
769 }