dm: Use uclass_first_device_err() where it is useful
[platform/kernel/u-boot.git] / arch / x86 / cpu / ivybridge / gma.c
1 /*
2  * From Coreboot file of the same name
3  *
4  * Copyright (C) 2011 Chromium OS Authors
5  *
6  * SPDX-License-Identifier:     GPL-2.0
7  */
8
9 #include <common.h>
10 #include <bios_emul.h>
11 #include <dm.h>
12 #include <errno.h>
13 #include <fdtdec.h>
14 #include <pci_rom.h>
15 #include <asm/io.h>
16 #include <asm/mtrr.h>
17 #include <asm/pci.h>
18 #include <asm/arch/pch.h>
19 #include <asm/arch/sandybridge.h>
20
21 struct gt_powermeter {
22         u16 reg;
23         u32 value;
24 };
25
26 static const struct gt_powermeter snb_pm_gt1[] = {
27         { 0xa200, 0xcc000000 },
28         { 0xa204, 0x07000040 },
29         { 0xa208, 0x0000fe00 },
30         { 0xa20c, 0x00000000 },
31         { 0xa210, 0x17000000 },
32         { 0xa214, 0x00000021 },
33         { 0xa218, 0x0817fe19 },
34         { 0xa21c, 0x00000000 },
35         { 0xa220, 0x00000000 },
36         { 0xa224, 0xcc000000 },
37         { 0xa228, 0x07000040 },
38         { 0xa22c, 0x0000fe00 },
39         { 0xa230, 0x00000000 },
40         { 0xa234, 0x17000000 },
41         { 0xa238, 0x00000021 },
42         { 0xa23c, 0x0817fe19 },
43         { 0xa240, 0x00000000 },
44         { 0xa244, 0x00000000 },
45         { 0xa248, 0x8000421e },
46         { 0 }
47 };
48
49 static const struct gt_powermeter snb_pm_gt2[] = {
50         { 0xa200, 0x330000a6 },
51         { 0xa204, 0x402d0031 },
52         { 0xa208, 0x00165f83 },
53         { 0xa20c, 0xf1000000 },
54         { 0xa210, 0x00000000 },
55         { 0xa214, 0x00160016 },
56         { 0xa218, 0x002a002b },
57         { 0xa21c, 0x00000000 },
58         { 0xa220, 0x00000000 },
59         { 0xa224, 0x330000a6 },
60         { 0xa228, 0x402d0031 },
61         { 0xa22c, 0x00165f83 },
62         { 0xa230, 0xf1000000 },
63         { 0xa234, 0x00000000 },
64         { 0xa238, 0x00160016 },
65         { 0xa23c, 0x002a002b },
66         { 0xa240, 0x00000000 },
67         { 0xa244, 0x00000000 },
68         { 0xa248, 0x8000421e },
69         { 0 }
70 };
71
72 static const struct gt_powermeter ivb_pm_gt1[] = {
73         { 0xa800, 0x00000000 },
74         { 0xa804, 0x00021c00 },
75         { 0xa808, 0x00000403 },
76         { 0xa80c, 0x02001700 },
77         { 0xa810, 0x05000200 },
78         { 0xa814, 0x00000000 },
79         { 0xa818, 0x00690500 },
80         { 0xa81c, 0x0000007f },
81         { 0xa820, 0x01002501 },
82         { 0xa824, 0x00000300 },
83         { 0xa828, 0x01000331 },
84         { 0xa82c, 0x0000000c },
85         { 0xa830, 0x00010016 },
86         { 0xa834, 0x01100101 },
87         { 0xa838, 0x00010103 },
88         { 0xa83c, 0x00041300 },
89         { 0xa840, 0x00000b30 },
90         { 0xa844, 0x00000000 },
91         { 0xa848, 0x7f000000 },
92         { 0xa84c, 0x05000008 },
93         { 0xa850, 0x00000001 },
94         { 0xa854, 0x00000004 },
95         { 0xa858, 0x00000007 },
96         { 0xa85c, 0x00000000 },
97         { 0xa860, 0x00010000 },
98         { 0xa248, 0x0000221e },
99         { 0xa900, 0x00000000 },
100         { 0xa904, 0x00001c00 },
101         { 0xa908, 0x00000000 },
102         { 0xa90c, 0x06000000 },
103         { 0xa910, 0x09000200 },
104         { 0xa914, 0x00000000 },
105         { 0xa918, 0x00590000 },
106         { 0xa91c, 0x00000000 },
107         { 0xa920, 0x04002501 },
108         { 0xa924, 0x00000100 },
109         { 0xa928, 0x03000410 },
110         { 0xa92c, 0x00000000 },
111         { 0xa930, 0x00020000 },
112         { 0xa934, 0x02070106 },
113         { 0xa938, 0x00010100 },
114         { 0xa93c, 0x00401c00 },
115         { 0xa940, 0x00000000 },
116         { 0xa944, 0x00000000 },
117         { 0xa948, 0x10000e00 },
118         { 0xa94c, 0x02000004 },
119         { 0xa950, 0x00000001 },
120         { 0xa954, 0x00000004 },
121         { 0xa960, 0x00060000 },
122         { 0xaa3c, 0x00001c00 },
123         { 0xaa54, 0x00000004 },
124         { 0xaa60, 0x00060000 },
125         { 0 }
126 };
127
128 static const struct gt_powermeter ivb_pm_gt2[] = {
129         { 0xa800, 0x10000000 },
130         { 0xa804, 0x00033800 },
131         { 0xa808, 0x00000902 },
132         { 0xa80c, 0x0c002f00 },
133         { 0xa810, 0x12000400 },
134         { 0xa814, 0x00000000 },
135         { 0xa818, 0x00d20800 },
136         { 0xa81c, 0x00000002 },
137         { 0xa820, 0x03004b02 },
138         { 0xa824, 0x00000600 },
139         { 0xa828, 0x07000773 },
140         { 0xa82c, 0x00000000 },
141         { 0xa830, 0x00010032 },
142         { 0xa834, 0x1520040d },
143         { 0xa838, 0x00020105 },
144         { 0xa83c, 0x00083700 },
145         { 0xa840, 0x0000151d },
146         { 0xa844, 0x00000000 },
147         { 0xa848, 0x20001b00 },
148         { 0xa84c, 0x0a000010 },
149         { 0xa850, 0x00000000 },
150         { 0xa854, 0x00000008 },
151         { 0xa858, 0x00000008 },
152         { 0xa85c, 0x00000000 },
153         { 0xa860, 0x00020000 },
154         { 0xa248, 0x0000221e },
155         { 0xa900, 0x00000000 },
156         { 0xa904, 0x00003500 },
157         { 0xa908, 0x00000000 },
158         { 0xa90c, 0x0c000000 },
159         { 0xa910, 0x12000500 },
160         { 0xa914, 0x00000000 },
161         { 0xa918, 0x00b20000 },
162         { 0xa91c, 0x00000000 },
163         { 0xa920, 0x08004b02 },
164         { 0xa924, 0x00000200 },
165         { 0xa928, 0x07000820 },
166         { 0xa92c, 0x00000000 },
167         { 0xa930, 0x00030000 },
168         { 0xa934, 0x050f020d },
169         { 0xa938, 0x00020300 },
170         { 0xa93c, 0x00903900 },
171         { 0xa940, 0x00000000 },
172         { 0xa944, 0x00000000 },
173         { 0xa948, 0x20001b00 },
174         { 0xa94c, 0x0a000010 },
175         { 0xa950, 0x00000000 },
176         { 0xa954, 0x00000008 },
177         { 0xa960, 0x00110000 },
178         { 0xaa3c, 0x00003900 },
179         { 0xaa54, 0x00000008 },
180         { 0xaa60, 0x00110000 },
181         { 0 }
182 };
183
184 static const struct gt_powermeter ivb_pm_gt2_17w[] = {
185         { 0xa800, 0x20000000 },
186         { 0xa804, 0x000e3800 },
187         { 0xa808, 0x00000806 },
188         { 0xa80c, 0x0c002f00 },
189         { 0xa810, 0x0c000800 },
190         { 0xa814, 0x00000000 },
191         { 0xa818, 0x00d20d00 },
192         { 0xa81c, 0x000000ff },
193         { 0xa820, 0x03004b02 },
194         { 0xa824, 0x00000600 },
195         { 0xa828, 0x07000773 },
196         { 0xa82c, 0x00000000 },
197         { 0xa830, 0x00020032 },
198         { 0xa834, 0x1520040d },
199         { 0xa838, 0x00020105 },
200         { 0xa83c, 0x00083700 },
201         { 0xa840, 0x000016ff },
202         { 0xa844, 0x00000000 },
203         { 0xa848, 0xff000000 },
204         { 0xa84c, 0x0a000010 },
205         { 0xa850, 0x00000002 },
206         { 0xa854, 0x00000008 },
207         { 0xa858, 0x0000000f },
208         { 0xa85c, 0x00000000 },
209         { 0xa860, 0x00020000 },
210         { 0xa248, 0x0000221e },
211         { 0xa900, 0x00000000 },
212         { 0xa904, 0x00003800 },
213         { 0xa908, 0x00000000 },
214         { 0xa90c, 0x0c000000 },
215         { 0xa910, 0x12000800 },
216         { 0xa914, 0x00000000 },
217         { 0xa918, 0x00b20000 },
218         { 0xa91c, 0x00000000 },
219         { 0xa920, 0x08004b02 },
220         { 0xa924, 0x00000300 },
221         { 0xa928, 0x01000820 },
222         { 0xa92c, 0x00000000 },
223         { 0xa930, 0x00030000 },
224         { 0xa934, 0x15150406 },
225         { 0xa938, 0x00020300 },
226         { 0xa93c, 0x00903900 },
227         { 0xa940, 0x00000000 },
228         { 0xa944, 0x00000000 },
229         { 0xa948, 0x20001b00 },
230         { 0xa94c, 0x0a000010 },
231         { 0xa950, 0x00000000 },
232         { 0xa954, 0x00000008 },
233         { 0xa960, 0x00110000 },
234         { 0xaa3c, 0x00003900 },
235         { 0xaa54, 0x00000008 },
236         { 0xaa60, 0x00110000 },
237         { 0 }
238 };
239
240 static const struct gt_powermeter ivb_pm_gt2_35w[] = {
241         { 0xa800, 0x00000000 },
242         { 0xa804, 0x00030400 },
243         { 0xa808, 0x00000806 },
244         { 0xa80c, 0x0c002f00 },
245         { 0xa810, 0x0c000300 },
246         { 0xa814, 0x00000000 },
247         { 0xa818, 0x00d20d00 },
248         { 0xa81c, 0x000000ff },
249         { 0xa820, 0x03004b02 },
250         { 0xa824, 0x00000600 },
251         { 0xa828, 0x07000773 },
252         { 0xa82c, 0x00000000 },
253         { 0xa830, 0x00020032 },
254         { 0xa834, 0x1520040d },
255         { 0xa838, 0x00020105 },
256         { 0xa83c, 0x00083700 },
257         { 0xa840, 0x000016ff },
258         { 0xa844, 0x00000000 },
259         { 0xa848, 0xff000000 },
260         { 0xa84c, 0x0a000010 },
261         { 0xa850, 0x00000001 },
262         { 0xa854, 0x00000008 },
263         { 0xa858, 0x00000008 },
264         { 0xa85c, 0x00000000 },
265         { 0xa860, 0x00020000 },
266         { 0xa248, 0x0000221e },
267         { 0xa900, 0x00000000 },
268         { 0xa904, 0x00003800 },
269         { 0xa908, 0x00000000 },
270         { 0xa90c, 0x0c000000 },
271         { 0xa910, 0x12000800 },
272         { 0xa914, 0x00000000 },
273         { 0xa918, 0x00b20000 },
274         { 0xa91c, 0x00000000 },
275         { 0xa920, 0x08004b02 },
276         { 0xa924, 0x00000300 },
277         { 0xa928, 0x01000820 },
278         { 0xa92c, 0x00000000 },
279         { 0xa930, 0x00030000 },
280         { 0xa934, 0x15150406 },
281         { 0xa938, 0x00020300 },
282         { 0xa93c, 0x00903900 },
283         { 0xa940, 0x00000000 },
284         { 0xa944, 0x00000000 },
285         { 0xa948, 0x20001b00 },
286         { 0xa94c, 0x0a000010 },
287         { 0xa950, 0x00000000 },
288         { 0xa954, 0x00000008 },
289         { 0xa960, 0x00110000 },
290         { 0xaa3c, 0x00003900 },
291         { 0xaa54, 0x00000008 },
292         { 0xaa60, 0x00110000 },
293         { 0 }
294 };
295
296 /*
297  * Some vga option roms are used for several chipsets but they only have one
298  * PCI ID in their header. If we encounter such an option rom, we need to do
299  * the mapping ourselves.
300  */
301
302 u32 map_oprom_vendev(u32 vendev)
303 {
304         u32 new_vendev = vendev;
305
306         switch (vendev) {
307         case 0x80860102:                /* GT1 Desktop */
308         case 0x8086010a:                /* GT1 Server */
309         case 0x80860112:                /* GT2 Desktop */
310         case 0x80860116:                /* GT2 Mobile */
311         case 0x80860122:                /* GT2 Desktop >=1.3GHz */
312         case 0x80860126:                /* GT2 Mobile >=1.3GHz */
313         case 0x80860156:                /* IVB */
314         case 0x80860166:                /* IVB */
315                 /* Set to GT1 Mobile */
316                 new_vendev = 0x80860106;
317                 break;
318         }
319
320         return new_vendev;
321 }
322
323 static inline u32 gtt_read(void *bar, u32 reg)
324 {
325         return readl(bar + reg);
326 }
327
328 static inline void gtt_write(void *bar, u32 reg, u32 data)
329 {
330         writel(data, bar + reg);
331 }
332
333 static void gtt_write_powermeter(void *bar, const struct gt_powermeter *pm)
334 {
335         for (; pm && pm->reg; pm++)
336                 gtt_write(bar, pm->reg, pm->value);
337 }
338
339 #define GTT_RETRY 1000
340 static int gtt_poll(void *bar, u32 reg, u32 mask, u32 value)
341 {
342         unsigned try = GTT_RETRY;
343         u32 data;
344
345         while (try--) {
346                 data = gtt_read(bar, reg);
347                 if ((data & mask) == value)
348                         return 1;
349                 udelay(10);
350         }
351
352         printf("GT init timeout\n");
353         return 0;
354 }
355
356 static int gma_pm_init_pre_vbios(void *gtt_bar, int rev)
357 {
358         u32 reg32;
359
360         debug("GT Power Management Init, silicon = %#x\n", rev);
361
362         if (rev < IVB_STEP_C0) {
363                 /* 1: Enable force wake */
364                 gtt_write(gtt_bar, 0xa18c, 0x00000001);
365                 gtt_poll(gtt_bar, 0x130090, (1 << 0), (1 << 0));
366         } else {
367                 gtt_write(gtt_bar, 0xa180, 1 << 5);
368                 gtt_write(gtt_bar, 0xa188, 0xffff0001);
369                 gtt_poll(gtt_bar, 0x130040, (1 << 0), (1 << 0));
370         }
371
372         if ((rev & BASE_REV_MASK) == BASE_REV_SNB) {
373                 /* 1d: Set GTT+0x42004 [15:14]=11 (SnB C1+) */
374                 reg32 = gtt_read(gtt_bar, 0x42004);
375                 reg32 |= (1 << 14) | (1 << 15);
376                 gtt_write(gtt_bar, 0x42004, reg32);
377         }
378
379         if (rev >= IVB_STEP_A0) {
380                 /* Display Reset Acknowledge Settings */
381                 reg32 = gtt_read(gtt_bar, 0x45010);
382                 reg32 |= (1 << 1) | (1 << 0);
383                 gtt_write(gtt_bar, 0x45010, reg32);
384         }
385
386         /* 2: Get GT SKU from GTT+0x911c[13] */
387         reg32 = gtt_read(gtt_bar, 0x911c);
388         if ((rev & BASE_REV_MASK) == BASE_REV_SNB) {
389                 if (reg32 & (1 << 13)) {
390                         debug("SNB GT1 Power Meter Weights\n");
391                         gtt_write_powermeter(gtt_bar, snb_pm_gt1);
392                 } else {
393                         debug("SNB GT2 Power Meter Weights\n");
394                         gtt_write_powermeter(gtt_bar, snb_pm_gt2);
395                 }
396         } else {
397                 u32 unit = readl(MCHBAR_REG(0x5938)) & 0xf;
398
399                 if (reg32 & (1 << 13)) {
400                         /* GT1 SKU */
401                         debug("IVB GT1 Power Meter Weights\n");
402                         gtt_write_powermeter(gtt_bar, ivb_pm_gt1);
403                 } else {
404                         /* GT2 SKU */
405                         u32 tdp = readl(MCHBAR_REG(0x5930)) & 0x7fff;
406                         tdp /= (1 << unit);
407
408                         if (tdp <= 17) {
409                                 /* <=17W ULV */
410                                 debug("IVB GT2 17W Power Meter Weights\n");
411                                 gtt_write_powermeter(gtt_bar, ivb_pm_gt2_17w);
412                         } else if ((tdp >= 25) && (tdp <= 35)) {
413                                 /* 25W-35W */
414                                 debug("IVB GT2 25W-35W Power Meter Weights\n");
415                                 gtt_write_powermeter(gtt_bar, ivb_pm_gt2_35w);
416                         } else {
417                                 /* All others */
418                                 debug("IVB GT2 35W Power Meter Weights\n");
419                                 gtt_write_powermeter(gtt_bar, ivb_pm_gt2_35w);
420                         }
421                 }
422         }
423
424         /* 3: Gear ratio map */
425         gtt_write(gtt_bar, 0xa004, 0x00000010);
426
427         /* 4: GFXPAUSE */
428         gtt_write(gtt_bar, 0xa000, 0x00070020);
429
430         /* 5: Dynamic EU trip control */
431         gtt_write(gtt_bar, 0xa080, 0x00000004);
432
433         /* 6: ECO bits */
434         reg32 = gtt_read(gtt_bar, 0xa180);
435         reg32 |= (1 << 26) | (1 << 31);
436         /* (bit 20=1 for SNB step D1+ / IVB A0+) */
437         if (rev >= SNB_STEP_D1)
438                 reg32 |= (1 << 20);
439         gtt_write(gtt_bar, 0xa180, reg32);
440
441         /* 6a: for SnB step D2+ only */
442         if (((rev & BASE_REV_MASK) == BASE_REV_SNB) &&
443             (rev >= SNB_STEP_D2)) {
444                 reg32 = gtt_read(gtt_bar, 0x9400);
445                 reg32 |= (1 << 7);
446                 gtt_write(gtt_bar, 0x9400, reg32);
447
448                 reg32 = gtt_read(gtt_bar, 0x941c);
449                 reg32 &= 0xf;
450                 reg32 |= (1 << 1);
451                 gtt_write(gtt_bar, 0x941c, reg32);
452                 gtt_poll(gtt_bar, 0x941c, (1 << 1), (0 << 1));
453         }
454
455         if ((rev & BASE_REV_MASK) == BASE_REV_IVB) {
456                 reg32 = gtt_read(gtt_bar, 0x907c);
457                 reg32 |= (1 << 16);
458                 gtt_write(gtt_bar, 0x907c, reg32);
459
460                 /* 6b: Clocking reset controls */
461                 gtt_write(gtt_bar, 0x9424, 0x00000001);
462         } else {
463                 /* 6b: Clocking reset controls */
464                 gtt_write(gtt_bar, 0x9424, 0x00000000);
465         }
466
467         /* 7 */
468         if (gtt_poll(gtt_bar, 0x138124, (1 << 31), (0 << 31))) {
469                 gtt_write(gtt_bar, 0x138128, 0x00000029); /* Mailbox Data */
470                 /* Mailbox Cmd for RC6 VID */
471                 gtt_write(gtt_bar, 0x138124, 0x80000004);
472                 if (gtt_poll(gtt_bar, 0x138124, (1 << 31), (0 << 31)))
473                         gtt_write(gtt_bar, 0x138124, 0x8000000a);
474                 gtt_poll(gtt_bar, 0x138124, (1 << 31), (0 << 31));
475         }
476
477         /* 8 */
478         gtt_write(gtt_bar, 0xa090, 0x00000000); /* RC Control */
479         gtt_write(gtt_bar, 0xa098, 0x03e80000); /* RC1e Wake Rate Limit */
480         gtt_write(gtt_bar, 0xa09c, 0x0028001e); /* RC6/6p Wake Rate Limit */
481         gtt_write(gtt_bar, 0xa0a0, 0x0000001e); /* RC6pp Wake Rate Limit */
482         gtt_write(gtt_bar, 0xa0a8, 0x0001e848); /* RC Evaluation Interval */
483         gtt_write(gtt_bar, 0xa0ac, 0x00000019); /* RC Idle Hysteresis */
484
485         /* 9 */
486         gtt_write(gtt_bar, 0x2054, 0x0000000a); /* Render Idle Max Count */
487         gtt_write(gtt_bar, 0x12054, 0x0000000a); /* Video Idle Max Count */
488         gtt_write(gtt_bar, 0x22054, 0x0000000a); /* Blitter Idle Max Count */
489
490         /* 10 */
491         gtt_write(gtt_bar, 0xa0b0, 0x00000000); /* Unblock Ack to Busy */
492         gtt_write(gtt_bar, 0xa0b4, 0x000003e8); /* RC1e Threshold */
493         gtt_write(gtt_bar, 0xa0b8, 0x0000c350); /* RC6 Threshold */
494         gtt_write(gtt_bar, 0xa0bc, 0x000186a0); /* RC6p Threshold */
495         gtt_write(gtt_bar, 0xa0c0, 0x0000fa00); /* RC6pp Threshold */
496
497         /* 11 */
498         gtt_write(gtt_bar, 0xa010, 0x000f4240); /* RP Down Timeout */
499         gtt_write(gtt_bar, 0xa014, 0x12060000); /* RP Interrupt Limits */
500         gtt_write(gtt_bar, 0xa02c, 0x00015f90); /* RP Up Threshold */
501         gtt_write(gtt_bar, 0xa030, 0x000186a0); /* RP Down Threshold */
502         gtt_write(gtt_bar, 0xa068, 0x000186a0); /* RP Up EI */
503         gtt_write(gtt_bar, 0xa06c, 0x000493e0); /* RP Down EI */
504         gtt_write(gtt_bar, 0xa070, 0x0000000a); /* RP Idle Hysteresis */
505
506         /* 11a: Enable Render Standby (RC6) */
507         if ((rev & BASE_REV_MASK) == BASE_REV_IVB) {
508                 /*
509                  * IvyBridge should also support DeepRenderStandby.
510                  *
511                  * Unfortunately it does not work reliably on all SKUs so
512                  * disable it here and it can be enabled by the kernel.
513                  */
514                 gtt_write(gtt_bar, 0xa090, 0x88040000); /* HW RC Control */
515         } else {
516                 gtt_write(gtt_bar, 0xa090, 0x88040000); /* HW RC Control */
517         }
518
519         /* 12: Normal Frequency Request */
520         /* RPNFREQ_VAL comes from MCHBAR 0x5998 23:16 (8 bits!? use 7) */
521         reg32 = readl(MCHBAR_REG(0x5998));
522         reg32 >>= 16;
523         reg32 &= 0xef;
524         reg32 <<= 25;
525         gtt_write(gtt_bar, 0xa008, reg32);
526
527         /* 13: RP Control */
528         gtt_write(gtt_bar, 0xa024, 0x00000592);
529
530         /* 14: Enable PM Interrupts */
531         gtt_write(gtt_bar, 0x4402c, 0x03000076);
532
533         /* Clear 0x6c024 [8:6] */
534         reg32 = gtt_read(gtt_bar, 0x6c024);
535         reg32 &= ~0x000001c0;
536         gtt_write(gtt_bar, 0x6c024, reg32);
537
538         return 0;
539 }
540
541 int gma_pm_init_post_vbios(struct udevice *dev, int rev, void *gtt_bar)
542 {
543         const void *blob = gd->fdt_blob;
544         int node = dev->of_offset;
545         u32 reg32, cycle_delay;
546
547         debug("GT Power Management Init (post VBIOS)\n");
548
549         /* 15: Deassert Force Wake */
550         if (rev < IVB_STEP_C0) {
551                 gtt_write(gtt_bar, 0xa18c, gtt_read(gtt_bar, 0xa18c) & ~1);
552                 gtt_poll(gtt_bar, 0x130090, (1 << 0), (0 << 0));
553         } else {
554                 gtt_write(gtt_bar, 0xa188, 0x1fffe);
555                 if (gtt_poll(gtt_bar, 0x130040, (1 << 0), (0 << 0))) {
556                         gtt_write(gtt_bar, 0xa188,
557                                   gtt_read(gtt_bar, 0xa188) | 1);
558                 }
559         }
560
561         /* 16: SW RC Control */
562         gtt_write(gtt_bar, 0xa094, 0x00060000);
563
564         /* Setup Digital Port Hotplug */
565         reg32 = gtt_read(gtt_bar, 0xc4030);
566         if (!reg32) {
567                 u32 dp_hotplug[3];
568
569                 if (fdtdec_get_int_array(blob, node, "intel,dp_hotplug",
570                                          dp_hotplug, ARRAY_SIZE(dp_hotplug)))
571                         return -EINVAL;
572
573                 reg32 = (dp_hotplug[0] & 0x7) << 2;
574                 reg32 |= (dp_hotplug[0] & 0x7) << 10;
575                 reg32 |= (dp_hotplug[0] & 0x7) << 18;
576                 gtt_write(gtt_bar, 0xc4030, reg32);
577         }
578
579         /* Setup Panel Power On Delays */
580         reg32 = gtt_read(gtt_bar, 0xc7208);
581         if (!reg32) {
582                 reg32 = (unsigned)fdtdec_get_int(blob, node,
583                                                  "panel-port-select", 0) << 30;
584                 reg32 |= fdtdec_get_int(blob, node, "panel-power-up-delay", 0)
585                                 << 16;
586                 reg32 |= fdtdec_get_int(blob, node,
587                                         "panel-power-backlight-on-delay", 0);
588                 gtt_write(gtt_bar, 0xc7208, reg32);
589         }
590
591         /* Setup Panel Power Off Delays */
592         reg32 = gtt_read(gtt_bar, 0xc720c);
593         if (!reg32) {
594                 reg32 = fdtdec_get_int(blob, node, "panel-power-down-delay", 0)
595                                 << 16;
596                 reg32 |= fdtdec_get_int(blob, node,
597                                         "panel-power-backlight-off-delay", 0);
598                 gtt_write(gtt_bar, 0xc720c, reg32);
599         }
600
601         /* Setup Panel Power Cycle Delay */
602         cycle_delay = fdtdec_get_int(blob, node,
603                                      "intel,panel-power-cycle-delay", 0);
604         if (cycle_delay) {
605                 reg32 = gtt_read(gtt_bar, 0xc7210);
606                 reg32 &= ~0xff;
607                 reg32 |= cycle_delay;
608                 gtt_write(gtt_bar, 0xc7210, reg32);
609         }
610
611         /* Enable Backlight if needed */
612         reg32 = fdtdec_get_int(blob, node, "intel,cpu-backlight", 0);
613         if (reg32) {
614                 gtt_write(gtt_bar, 0x48250, (1 << 31));
615                 gtt_write(gtt_bar, 0x48254, reg32);
616         }
617         reg32 = fdtdec_get_int(blob, node, "intel,pch-backlight", 0);
618         if (reg32) {
619                 gtt_write(gtt_bar, 0xc8250, (1 << 31));
620                 gtt_write(gtt_bar, 0xc8254, reg32);
621         }
622
623         return 0;
624 }
625
626 /*
627  * Some vga option roms are used for several chipsets but they only have one
628  * PCI ID in their header. If we encounter such an option rom, we need to do
629  * the mapping ourselves.
630  */
631
632 uint32_t board_map_oprom_vendev(uint32_t vendev)
633 {
634         switch (vendev) {
635         case 0x80860102:                /* GT1 Desktop */
636         case 0x8086010a:                /* GT1 Server */
637         case 0x80860112:                /* GT2 Desktop */
638         case 0x80860116:                /* GT2 Mobile */
639         case 0x80860122:                /* GT2 Desktop >=1.3GHz */
640         case 0x80860126:                /* GT2 Mobile >=1.3GHz */
641         case 0x80860156:                /* IVB */
642         case 0x80860166:                /* IVB */
643                 return 0x80860106;      /* GT1 Mobile */
644         }
645
646         return vendev;
647 }
648
649 static int int15_handler(void)
650 {
651         int res = 0;
652
653         debug("%s: INT15 function %04x!\n", __func__, M.x86.R_AX);
654
655         switch (M.x86.R_AX) {
656         case 0x5f34:
657                 /*
658                  * Set Panel Fitting Hook:
659                  *  bit 2 = Graphics Stretching
660                  *  bit 1 = Text Stretching
661                  *  bit 0 = Centering (do not set with bit1 or bit2)
662                  *  0     = video bios default
663                  */
664                 M.x86.R_AX = 0x005f;
665                 M.x86.R_CL = 0x00; /* Use video bios default */
666                 res = 1;
667                 break;
668         case 0x5f35:
669                 /*
670                  * Boot Display Device Hook:
671                  *  bit 0 = CRT
672                  *  bit 1 = TV (eDP)
673                  *  bit 2 = EFP
674                  *  bit 3 = LFP
675                  *  bit 4 = CRT2
676                  *  bit 5 = TV2 (eDP)
677                  *  bit 6 = EFP2
678                  *  bit 7 = LFP2
679                  */
680                 M.x86.R_AX = 0x005f;
681                 M.x86.R_CX = 0x0000; /* Use video bios default */
682                 res = 1;
683                 break;
684         case 0x5f51:
685                 /*
686                  * Hook to select active LFP configuration:
687                  *  00h = No LVDS, VBIOS does not enable LVDS
688                  *  01h = Int-LVDS, LFP driven by integrated LVDS decoder
689                  *  02h = SVDO-LVDS, LFP driven by SVDO decoder
690                  *  03h = eDP, LFP Driven by Int-DisplayPort encoder
691                  */
692                 M.x86.R_AX = 0x005f;
693                 M.x86.R_CX = 0x0003; /* eDP */
694                 res = 1;
695                 break;
696         case 0x5f70:
697                 switch (M.x86.R_CH) {
698                 case 0:
699                         /* Get Mux */
700                         M.x86.R_AX = 0x005f;
701                         M.x86.R_CX = 0x0000;
702                         res = 1;
703                         break;
704                 case 1:
705                         /* Set Mux */
706                         M.x86.R_AX = 0x005f;
707                         M.x86.R_CX = 0x0000;
708                         res = 1;
709                         break;
710                 case 2:
711                         /* Get SG/Non-SG mode */
712                         M.x86.R_AX = 0x005f;
713                         M.x86.R_CX = 0x0000;
714                         res = 1;
715                         break;
716                 default:
717                         /* Interrupt was not handled */
718                         debug("Unknown INT15 5f70 function: 0x%02x\n",
719                               M.x86.R_CH);
720                         break;
721                 }
722                 break;
723         case 0x5fac:
724                 res = 1;
725                 break;
726         default:
727                 debug("Unknown INT15 function %04x!\n", M.x86.R_AX);
728                 break;
729         }
730         return res;
731 }
732
733 void sandybridge_setup_graphics(struct udevice *dev, struct udevice *video_dev)
734 {
735         u32 reg32;
736         u16 reg16;
737         u8 reg8;
738
739         dm_pci_read_config16(video_dev, PCI_DEVICE_ID, &reg16);
740         switch (reg16) {
741         case 0x0102: /* GT1 Desktop */
742         case 0x0106: /* GT1 Mobile */
743         case 0x010a: /* GT1 Server */
744         case 0x0112: /* GT2 Desktop */
745         case 0x0116: /* GT2 Mobile */
746         case 0x0122: /* GT2 Desktop >=1.3GHz */
747         case 0x0126: /* GT2 Mobile >=1.3GHz */
748         case 0x0156: /* IvyBridge */
749         case 0x0166: /* IvyBridge */
750                 break;
751         default:
752                 debug("Graphics not supported by this CPU/chipset\n");
753                 return;
754         }
755
756         debug("Initialising Graphics\n");
757
758         /* Setup IGD memory by setting GGC[7:3] = 1 for 32MB */
759         dm_pci_read_config16(dev, GGC, &reg16);
760         reg16 &= ~0x00f8;
761         reg16 |= 1 << 3;
762         /* Program GTT memory by setting GGC[9:8] = 2MB */
763         reg16 &= ~0x0300;
764         reg16 |= 2 << 8;
765         /* Enable VGA decode */
766         reg16 &= ~0x0002;
767         dm_pci_write_config16(dev, GGC, reg16);
768
769         /* Enable 256MB aperture */
770         dm_pci_read_config8(video_dev, MSAC, &reg8);
771         reg8 &= ~0x06;
772         reg8 |= 0x02;
773         dm_pci_write_config8(video_dev, MSAC, reg8);
774
775         /* Erratum workarounds */
776         reg32 = readl(MCHBAR_REG(0x5f00));
777         reg32 |= (1 << 9) | (1 << 10);
778         writel(reg32, MCHBAR_REG(0x5f00));
779
780         /* Enable SA Clock Gating */
781         reg32 = readl(MCHBAR_REG(0x5f00));
782         writel(reg32 | 1, MCHBAR_REG(0x5f00));
783
784         /* GPU RC6 workaround for sighting 366252 */
785         reg32 = readl(MCHBAR_REG(0x5d14));
786         reg32 |= (1 << 31);
787         writel(reg32, MCHBAR_REG(0x5d14));
788
789         /* VLW */
790         reg32 = readl(MCHBAR_REG(0x6120));
791         reg32 &= ~(1 << 0);
792         writel(reg32, MCHBAR_REG(0x6120));
793
794         reg32 = readl(MCHBAR_REG(0x5418));
795         reg32 |= (1 << 4) | (1 << 5);
796         writel(reg32, MCHBAR_REG(0x5418));
797 }
798
799 int gma_func0_init(struct udevice *dev)
800 {
801 #ifdef CONFIG_VIDEO
802         ulong start;
803 #endif
804         struct udevice *nbridge;
805         void *gtt_bar;
806         ulong base;
807         u32 reg32;
808         int ret;
809         int rev;
810
811         /* Enable PCH Display Port */
812         writew(0x0010, RCB_REG(DISPBDF));
813         setbits_le32(RCB_REG(FD2), PCH_ENABLE_DBDF);
814
815         ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &nbridge);
816         if (ret)
817                 return ret;
818         rev = bridge_silicon_revision(nbridge);
819         sandybridge_setup_graphics(nbridge, dev);
820
821         /* IGD needs to be Bus Master */
822         dm_pci_read_config32(dev, PCI_COMMAND, &reg32);
823         reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
824         dm_pci_write_config32(dev, PCI_COMMAND, reg32);
825
826         /* Use write-combining for the graphics memory, 256MB */
827         base = dm_pci_read_bar32(dev, 2);
828         mtrr_add_request(MTRR_TYPE_WRCOMB, base, 256 << 20);
829         mtrr_commit(true);
830
831         gtt_bar = (void *)dm_pci_read_bar32(dev, 0);
832         debug("GT bar %p\n", gtt_bar);
833         ret = gma_pm_init_pre_vbios(gtt_bar, rev);
834         if (ret)
835                 return ret;
836
837 #ifdef CONFIG_VIDEO
838         start = get_timer(0);
839         ret = dm_pci_run_vga_bios(dev, int15_handler,
840                                   PCI_ROM_USE_NATIVE | PCI_ROM_ALLOW_FALLBACK);
841         debug("BIOS ran in %lums\n", get_timer(start));
842 #endif
843         /* Post VBIOS init */
844         ret = gma_pm_init_post_vbios(dev, rev, gtt_bar);
845         if (ret)
846                 return ret;
847
848         return 0;
849 }