Revert sblk/sblink change, use sblk like the rest of the codebase does.
[coreboot.git] / src / southbridge / amd / rs780 / rs780_gfx.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2010 Advanced Micro Devices, Inc.
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 /*
21  *  for rs780 internal graphics device
22  *  device id of internal grphics:
23  *      RS780:  0x9610
24  *      RS780C: 0x9611
25  *      RS780M: 0x9612
26  *      RS780MC:0x9613
27  *      RS780E: 0x9615
28  *      RS785G: 0x9710 - just works, not much tested
29  */
30 #include <console/console.h>
31 #include <device/device.h>
32 #include <device/pci.h>
33 #include <device/pci_ids.h>
34 #include <device/pci_ops.h>
35 #include <delay.h>
36 #include <cpu/x86/msr.h>
37 #include "rs780.h"
38 extern int is_dev3_present(void);
39 void set_pcie_reset(void);
40 void set_pcie_dereset(void);
41
42 /* Trust the original resource allocation. Don't do it again. */
43 #undef DONT_TRUST_RESOURCE_ALLOCATION
44 //#define DONT_TRUST_RESOURCE_ALLOCATION
45
46 #define CLK_CNTL_INDEX  0x8
47 #define CLK_CNTL_DATA   0xC
48
49 /* The Integrated Info Table. */
50 ATOM_INTEGRATED_SYSTEM_INFO_V2 vgainfo;
51
52 #ifdef UNUSED_CODE
53 static u32 clkind_read(device_t dev, u32 index)
54 {
55         u32     gfx_bar2 = pci_read_config32(dev, 0x18) & ~0xF;
56
57         *(u32*)(gfx_bar2+CLK_CNTL_INDEX) = index & 0x7F;
58         return *(u32*)(gfx_bar2+CLK_CNTL_DATA);
59 }
60 #endif
61
62 static void clkind_write(device_t dev, u32 index, u32 data)
63 {
64         u32     gfx_bar2 = pci_read_config32(dev, 0x18) & ~0xF;
65         /* printk(BIOS_DEBUG, "gfx bar 2 %02x\n", gfx_bar2); */
66
67         *(u32*)(gfx_bar2+CLK_CNTL_INDEX) = index | 1<<7;
68         *(u32*)(gfx_bar2+CLK_CNTL_DATA)  = data;
69 }
70
71 /*
72 * pci_dev_read_resources thinks it is a IO type.
73 * We have to force it to mem type.
74 */
75 static void rs780_gfx_read_resources(device_t dev)
76 {
77         printk(BIOS_DEBUG, "rs780_gfx_read_resources.\n");
78
79         /* The initial value of 0x24 is 0xFFFFFFFF, which is confusing.
80            Even if we write 0xFFFFFFFF into it, it will be 0xFFF00000,
81            which tells us it is a memory address base.
82          */
83         pci_write_config32(dev, 0x24, 0x00000000);
84
85         /* Get the normal pci resources of this device */
86         pci_dev_read_resources(dev);
87         compact_resources(dev);
88 }
89
90 typedef struct _MMIORANGE
91 {
92         u32     Base;
93         u32     Limit;
94         u8      Attribute;
95 } MMIORANGE;
96
97 MMIORANGE MMIO[8], CreativeMMIO[8];
98
99 #define CIM_STATUS u32
100 #define CIM_SUCCESS 0x00000000
101 #define CIM_ERROR       0x80000000
102 #define CIM_UNSUPPORTED 0x80000001
103 #define CIM_DISABLEPORT 0x80000002
104
105 #define MMIO_ATTRIB_NP_ONLY     1
106 #define MMIO_ATTRIB_BOTTOM_TO_TOP 1<<1
107 #define MMIO_ATTRIB_SKIP_ZERO 1<<2
108
109 #ifdef DONT_TRUST_RESOURCE_ALLOCATION
110 static MMIORANGE* AllocMMIO(MMIORANGE* pMMIO)
111 {
112         int i;
113         for (i=0; i<8; i++) {
114                 if (pMMIO[i].Limit == 0)
115                                 return &pMMIO[i];
116         }
117         return 0;
118 }
119
120 static void FreeMMIO(MMIORANGE* pMMIO)
121 {
122         pMMIO->Base = 0;
123         pMMIO->Limit = 0;
124 }
125
126 static u32 SetMMIO(u32 Base, u32 Limit, u8 Attribute, MMIORANGE *pMMIO)
127 {
128         int i;
129         MMIORANGE * TempRange;
130         for(i=0; i<8; i++)
131         {
132                 if(pMMIO[i].Attribute != Attribute && Base >= pMMIO[i].Base && Limit <= pMMIO[i].Limit)
133                 {
134                         TempRange = AllocMMIO(pMMIO);
135                         if(TempRange == 0) return 0x80000000;
136                         TempRange->Base = Limit;
137                         TempRange->Limit = pMMIO[i].Limit;
138                         TempRange->Attribute = pMMIO[i].Attribute;
139                         pMMIO[i].Limit = Base;
140                 }
141         }
142         TempRange = AllocMMIO(pMMIO);
143         if(TempRange == 0) return 0x80000000;
144         TempRange->Base = Base;
145         TempRange->Limit = Limit;
146         TempRange->Attribute = Attribute;
147         return 0;
148 }
149
150 static u8 FinalizeMMIO(MMIORANGE *pMMIO)
151 {
152         int i, j, n = 0;
153         for(i=0; i<8; i++)
154         {
155                 if (pMMIO[i].Base == pMMIO[i].Limit)
156                 {
157                         FreeMMIO(&pMMIO[i]);
158                         continue;
159                 }
160                 for(j=0; j<i; j++)
161                 {
162                         if (i!=j && pMMIO[i].Attribute == pMMIO[j].Attribute)
163                         {
164                                 if (pMMIO[i].Base == pMMIO[j].Limit)
165                                 {
166                                         pMMIO[j].Limit = pMMIO[i].Limit;
167                                          FreeMMIO(&pMMIO[i]);
168                                 }
169                                 if (pMMIO[i].Limit == pMMIO[j].Base)
170                                 {
171                                         pMMIO[j].Base = pMMIO[i].Base;
172                                    FreeMMIO(&pMMIO[i]);
173                                 }
174                         }
175                 }
176         }
177         for (i=0; i<8; i++)
178         {
179                 if (pMMIO[i].Limit != 0) n++;
180         }
181         return n;
182 }
183
184 static CIM_STATUS GetCreativeMMIO(MMIORANGE *pMMIO)
185 {
186         CIM_STATUS Status = CIM_UNSUPPORTED;
187         u8 Bus, Dev, Reg, BusStart, BusEnd;
188         u32     Value;
189         device_t dev0x14 = dev_find_slot(0, PCI_DEVFN(0x14, 4));
190         device_t tempdev;
191         Value = pci_read_config32(dev0x14, 0x18);
192         BusStart = (Value >> 8) & 0xFF;
193         BusEnd = (Value >> 16) & 0xFF;
194         for(Bus = BusStart; Bus <= BusEnd; Bus++)
195         {
196                 for(Dev = 0; Dev <= 0x1f; Dev++)
197                 {
198                         tempdev = dev_find_slot(Bus, Dev << 3);
199                         Value = pci_read_config32(tempdev, 0);
200                         printk(BIOS_DEBUG, "Dev ID %x \n", Value);
201                         if((Value & 0xffff) == 0x1102)
202                         {//Creative
203                                 //Found Creative SB
204                                 u32     MMIOStart = 0xffffffff;
205                                 u32 MMIOLimit = 0;
206                                 for(Reg = 0x10; Reg < 0x20; Reg+=4)
207                                 {
208                                         u32     BaseA, LimitA;
209                                         BaseA = pci_read_config32(tempdev, Reg);
210                                         Value = BaseA;
211                                         if(!(Value & 0x01))
212                                         {
213                                                 Value = Value & 0xffffff00;
214                                                 if(Value !=  0)
215                                                 {
216                                                         if(MMIOStart > Value)
217                                                                 MMIOStart = Value;
218                                                         LimitA = 0xffffffff;
219                                                         //WritePCI(PciAddress,AccWidthUint32,&LimitA);
220                                                         pci_write_config32(tempdev, Reg, LimitA);
221                                                         //ReadPCI(PciAddress,AccWidthUint32,&LimitA);
222                                                         LimitA = pci_read_config32(tempdev, Reg);
223                                                         LimitA = Value + (~LimitA + 1);
224                                                         //WritePCI(PciAddress,AccWidthUint32,&BaseA);
225                                                         pci_write_config32(tempdev, Reg, BaseA);
226                                                         if (LimitA > MMIOLimit)
227                                                                 MMIOLimit = LimitA;
228                                                 }
229                                         }
230                                 }
231                                 printk(BIOS_DEBUG, " MMIOStart %x MMIOLimit %x \n", MMIOStart, MMIOLimit);
232                                 if (MMIOStart < MMIOLimit)
233                                 {
234                                         Status = SetMMIO(MMIOStart>>8, MMIOLimit>>8, 0x80, pMMIO);
235                                         if(Status == CIM_ERROR) return Status;
236                                 }
237                         }
238                 }
239         }
240         if(Status == CIM_SUCCESS)
241         {
242                 //Lets optimize MMIO
243                 if(FinalizeMMIO(pMMIO) > 4)
244                 {
245                         Status = CIM_ERROR;
246                 }
247         }
248
249         return Status;
250 }
251
252 static void ProgramMMIO(MMIORANGE *pMMIO, u8 LinkID, u8 Attribute)
253 {
254         int i, j, n = 7;
255         device_t k8_f1;
256
257         k8_f1 = dev_find_slot(0, PCI_DEVFN(0x18, 1));
258
259         for(i = 0; i < 8; i++)
260         {
261                 int k = 0, MmioReg;
262                 u32 Base = 0;
263                 u32 Limit = 0;
264                 for(j = 0; j < 8; j++)
265                 {
266                         if (Base < pMMIO[j].Base)
267                         {
268                                 Base = pMMIO[j].Base;
269                                 k = j;
270                         }
271                 }
272                 if(pMMIO[k].Limit != 0)
273                 {
274                         if(Attribute & MMIO_ATTRIB_NP_ONLY && pMMIO[k].Attribute == 0 )
275                         {
276                                 Base = 0;
277                         }
278                         else
279                         {
280                                 Base = pMMIO[k].Base | 0x3;
281                                 Limit= ((pMMIO[k].Limit - 1) & 0xffffff00) | pMMIO[k].Attribute | (LinkID << 4);
282                         }
283                         FreeMMIO(&pMMIO[k]);
284                 }
285                 if (Attribute & MMIO_ATTRIB_SKIP_ZERO && Base == 0 && Limit == 0) continue;
286                 MmioReg = (Attribute & MMIO_ATTRIB_BOTTOM_TO_TOP)?n:(7-n);
287                 n--;
288                 //RWPCI(PCI_ADDRESS(0,CPU_DEV,CPU_F1,0x80+MmioReg*8),AccWidthUint32 |S3_SAVE,0x0,0x0);
289                 pci_write_config32(k8_f1, 0x80+MmioReg*8, 0);
290
291                 //WritePCI(PCI_ADDRESS(0,CPU_DEV,CPU_F1,0x84+MmioReg*8),AccWidthUint32 |S3_SAVE,&Limit);
292                 pci_write_config32(k8_f1, 0x84+MmioReg*8, Limit);
293
294                 //WritePCI(PCI_ADDRESS(0,CPU_DEV,CPU_F1,0x80+MmioReg*8),AccWidthUint32 |S3_SAVE,&Base);
295                 pci_write_config32(k8_f1, 0x80+MmioReg*8, Base);
296         }
297 }
298 #endif
299
300 static void internal_gfx_pci_dev_init(struct device *dev)
301 {
302         unsigned char * bpointer;
303         volatile u32 * GpuF0MMReg;
304         volatile u32 * pointer;
305         int i;
306         u16 command;
307         u32 value, sblk;
308         u16 deviceid, vendorid;
309         device_t nb_dev = dev_find_slot(0, 0);
310         device_t k8_f2 = dev_find_slot(0, PCI_DEVFN(0x18, 2));
311         device_t k8_f0 = dev_find_slot(0, PCI_DEVFN(0x18, 0));
312
313         /* We definetely will use this in future. Just leave it here. */
314         /*struct southbridge_amd_rs780_config *cfg =
315            (struct southbridge_amd_rs780_config *)dev->chip_info;*/
316
317         deviceid = pci_read_config16(dev, PCI_DEVICE_ID);
318         vendorid = pci_read_config16(dev, PCI_VENDOR_ID);
319         printk(BIOS_DEBUG, "internal_gfx_pci_dev_init device=%x, vendor=%x.\n",
320              deviceid, vendorid);
321
322         command = pci_read_config16(dev, 0x04);
323         command |= 0x7;
324         pci_write_config16(dev, 0x04, command);
325
326         /* Clear vgainfo. */
327         bpointer = (unsigned char *) &vgainfo;
328         for(i=0; i<sizeof(ATOM_INTEGRATED_SYSTEM_INFO_V2); i++)
329         {
330                 *bpointer = 0;
331                 bpointer++;
332         }
333
334         GpuF0MMReg = (u32 *)pci_read_config32(dev, 0x18);
335
336         /* GFX_InitFBAccess. */
337         value = nbmc_read_index(nb_dev, 0x10);
338         *(GpuF0MMReg + 0x2000/4) = 0x11;
339         *(GpuF0MMReg + 0x2180/4) = ((value&0xff00)>>8)|((value&0xff000000)>>8);
340         *(GpuF0MMReg + 0x2c04/4) = ((value&0xff00)<<8);
341         *(GpuF0MMReg + 0x5428/4) = ((value&0xffff0000)+0x10000)-((value&0xffff)<<16);
342         *(GpuF0MMReg + 0x2000/4) = 0x00000011;
343         *(GpuF0MMReg + 0x200c/4) = 0x00000020;
344         *(GpuF0MMReg + 0x2010/4) = 0x10204810;
345         *(GpuF0MMReg + 0x2010/4) = 0x00204810;
346         *(GpuF0MMReg + 0x2014/4) = 0x10408810;
347         *(GpuF0MMReg + 0x2014/4) = 0x00408810;
348         *(GpuF0MMReg + 0x2414/4) = 0x00000080;
349         *(GpuF0MMReg + 0x2418/4) = 0x84422415;
350         *(GpuF0MMReg + 0x2418/4) = 0x04422415;
351         *(GpuF0MMReg + 0x5490/4) = 0x00000001;
352         *(GpuF0MMReg + 0x7de4/4) |= (1<<3) | (1<<4);
353         /* Force allow LDT_STOP Cool'n'Quiet workaround. */
354         *(GpuF0MMReg + 0x655c/4) |= 1<<4;
355         /* GFX_InitFBAccess finished. */
356
357         /* GFX_StartMC. */
358 #if (CONFIG_GFXUMA == 1) /* for UMA mode. */
359         /* MC_INIT_COMPLETE. */
360         set_nbmc_enable_bits(nb_dev, 0x2, 0, 1<<31);
361         /* MC_STARTUP, MC_POWERED_UP and MC_VMODE.*/
362         set_nbmc_enable_bits(nb_dev, 0x1, 1<<18, 1|1<<2);
363
364         set_nbmc_enable_bits(nb_dev, 0xb1, 0, 1<<6);
365         set_nbmc_enable_bits(nb_dev, 0xc3, 0, 1);
366         nbmc_write_index(nb_dev, 0x07, 0x18);
367         nbmc_write_index(nb_dev, 0x06, 0x00000102);
368         nbmc_write_index(nb_dev, 0x09, 0x40000008);
369         set_nbmc_enable_bits(nb_dev, 0x6, 0, 1<<31);
370         /* GFX_StartMC finished. */
371 #else
372         /* for SP mode. */
373         set_nbmc_enable_bits(nb_dev, 0xaa, 0xf0, 0x30);
374         set_nbmc_enable_bits(nb_dev, 0xce, 0xf0, 0x30);
375         set_nbmc_enable_bits(nb_dev, 0xca, 0xff000000, 0x47000000);
376         set_nbmc_enable_bits(nb_dev, 0xcb, 0x3f000000, 0x01000000);
377         set_nbmc_enable_bits(nb_dev, 0x01, 0, 1<<0);
378         set_nbmc_enable_bits(nb_dev, 0x04, 0, 1<<31);
379         set_nbmc_enable_bits(nb_dev, 0xb4, 0x3f, 0x3f);
380         set_nbmc_enable_bits(nb_dev, 0xb4, 0, 1<<6);
381         set_nbmc_enable_bits(nb_dev, 0xc3, 1<<11, 0);
382         set_nbmc_enable_bits(nb_dev, 0xa0, 1<<29, 0);
383         nbmc_write_index(nb_dev, 0xa4, 0x3484576f);
384         nbmc_write_index(nb_dev, 0xa5, 0x222222df);
385         nbmc_write_index(nb_dev, 0xa6, 0x00000000);
386         nbmc_write_index(nb_dev, 0xa7, 0x00000000);
387         set_nbmc_enable_bits(nb_dev, 0xc3, 1<<8, 0);
388         udelay(10);
389         set_nbmc_enable_bits(nb_dev, 0xc3, 1<<9, 0);
390         udelay(10);
391         set_nbmc_enable_bits(nb_dev, 0x01, 0, 1<<2);
392         udelay(200);
393         set_nbmc_enable_bits(nb_dev, 0x01, 0, 1<<3);
394         set_nbmc_enable_bits(nb_dev, 0xa0, 0, 1<<31);
395         udelay(500);
396         set_nbmc_enable_bits(nb_dev, 0x02, 0, 1<<31);
397         set_nbmc_enable_bits(nb_dev, 0xa0, 0, 1<<30);
398         set_nbmc_enable_bits(nb_dev, 0xa0, 1<<31, 0);
399         set_nbmc_enable_bits(nb_dev, 0xa0, 0, 1<<29);
400         nbmc_write_index(nb_dev, 0xa4, 0x23484576);
401         nbmc_write_index(nb_dev, 0xa5, 0x00000000);
402         nbmc_write_index(nb_dev, 0xa6, 0x00000000);
403         nbmc_write_index(nb_dev, 0xa7, 0x00000000);
404         /* GFX_StartMC finished. */
405
406         /* GFX_SPPowerManagment, don't care for new. */
407         /* Post MC Init table programming. */
408         set_nbmc_enable_bits(nb_dev, 0xac, ~(0xfffffff0), 0x0b);
409
410         /* Do we need Write and Read Calibration? */
411         /* GFX_Init finished. */
412 #endif
413
414         /* GFX_InitIntegratedInfo. */
415         /* fill the Integrated Info Table. */
416         vgainfo.sHeader.usStructureSize = sizeof(ATOM_INTEGRATED_SYSTEM_INFO_V2);
417         vgainfo.sHeader.ucTableFormatRevision = 1;
418         vgainfo.sHeader.ucTableContentRevision = 2;
419
420 #if (CONFIG_GFXUMA == 0) /* SP mode. */
421         vgainfo.ulBootUpSidePortClock = 667*100;
422         vgainfo.ucMemoryType = 3;
423         vgainfo.ulMinSidePortClock = 333*100;
424 #endif
425
426         vgainfo.ulBootUpEngineClock = 500 * 100; /* set boot up GFX engine clock. */
427         vgainfo.ulReserved1[0] = 0;     vgainfo.ulReserved1[1] = 0;
428         value = pci_read_config32(k8_f2, 0x94);
429         printk(BIOS_DEBUG, "MEMCLK = %x\n", value&0x7);
430         vgainfo.ulBootUpUMAClock = 333 * 100; /* set boot up UMA memory clock. */
431         vgainfo.ulBootUpSidePortClock = 0; /* disable SP. */
432         vgainfo.ulMinSidePortClock = 0; /* disable SP. */
433         for(i=0; i<6; i++)
434                 vgainfo.ulReserved2[i] = 0;
435         vgainfo.ulSystemConfig = 0;
436         //vgainfo.ulSystemConfig |= 1<<1 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7 | 1;
437         vgainfo.ulBootUpReqDisplayVector = 0; //?
438         vgainfo.ulOtherDisplayMisc = 0; //?
439         vgainfo.ulDDISlot1Config = 0x000c0011; //0; //VGA
440         //vgainfo.ulDDISlot1Config = 0x000c00FF; //0; //HDMI
441         vgainfo.ulDDISlot2Config = 0x00130022; //0; //?
442         vgainfo.ucMemoryType = 2;
443         /* UMA Channel Number: 1 or 2. */
444         vgainfo.ucUMAChannelNumber = 2;
445         vgainfo.ucDockingPinBit = 0; //?
446         vgainfo.ucDockingPinPolarity = 0; //?
447         vgainfo.ulDockingPinCFGInfo = 0; //?
448         vgainfo.ulCPUCapInfo = 3; /* K8. */
449
450         /* page 5-19 on BDG. */
451         vgainfo.usNumberOfCyclesInPeriod = 0x8019;
452         vgainfo.usMaxNBVoltage = 0x1a;
453         vgainfo.usMinNBVoltage = 0;
454         vgainfo.usBootUpNBVoltage = 0x1a;
455
456         /* Get SBLink value (HyperTransport I/O Hub Link ID). */
457         value = pci_read_config32(k8_f0, 0x64);
458         sblk = (value >> 8) & 0x3;
459         printk(BIOS_DEBUG, "SBLINK = %d.\n", sblk);
460
461         /* HT speed */
462         value = pci_read_config32(nb_dev, 0xd0);
463         printk(BIOS_DEBUG, "NB HT speed = %x.\n", value);
464         value = pci_read_config32(k8_f0, 0x88 + (sblk * 0x20));
465         printk(BIOS_DEBUG, "CPU HT speed = %x.\n", value);
466         vgainfo.ulHTLinkFreq = 100 * 100; /* set HT speed. */
467
468         /* HT width. */
469         value = pci_read_config32(nb_dev, 0xc8);
470         printk(BIOS_DEBUG, "HT width = %x.\n", value);
471         vgainfo.usMinHTLinkWidth = 16;
472         vgainfo.usMaxHTLinkWidth = 16;
473         vgainfo.usUMASyncStartDelay = 322;
474         vgainfo.usUMADataReturnTime = 86;
475         vgainfo.usLinkStatusZeroTime = 0x00c8; //0; //?
476         vgainfo.usReserved = 0;
477         vgainfo.ulHighVoltageHTLinkFreq = 100 * 100;
478         vgainfo.ulLowVoltageHTLinkFreq = 100 * 100;
479         vgainfo.usMaxUpStreamHTLinkWidth = 16;
480         vgainfo.usMaxDownStreamHTLinkWidth = 16;
481         vgainfo.usMinUpStreamHTLinkWidth = 16;
482         vgainfo.usMinDownStreamHTLinkWidth = 16;
483         for(i=0; i<97; i++)
484                 vgainfo.ulReserved3[i] = 0;
485
486         /* Transfer the Table to VBIOS. */
487         pointer = (u32 *)&vgainfo;
488         for(i=0; i<sizeof(ATOM_INTEGRATED_SYSTEM_INFO_V2); i+=4)
489         {
490 #if (CONFIG_GFXUMA == 1)
491                 *GpuF0MMReg = 0x80000000 + 0x10000000 - 512 + i;
492 #else
493                 *GpuF0MMReg = 0x80000000 + 0x8000000 - 512 + i;
494 #endif
495                 *(GpuF0MMReg+1) = *pointer++;
496         }
497
498         /* GFX_InitLate. */
499         {
500                 u8 temp8;
501                 temp8 = pci_read_config8(dev, 0x4);
502                 //temp8 &= ~1; /* CIM clears this bit. Strangely, I can'd. */
503                 temp8 |= 1<<1|1<<2;
504                 pci_write_config8(dev, 0x4, temp8);
505         }
506
507 #ifdef DONT_TRUST_RESOURCE_ALLOCATION
508         /* NB_SetupMGMMIO. */
509
510         /* clear MMIO and CreativeMMIO. */
511         bpointer = (unsigned char *)MMIO;
512         for(i=0; i<sizeof(MMIO); i++)
513         {
514                 *bpointer = 0;
515                 bpointer++;
516         }
517         bpointer = (unsigned char *)CreativeMMIO;
518         for(i=0; i<sizeof(CreativeMMIO); i++)
519         {
520                 *bpointer = 0;
521                 bpointer++;
522         }
523
524         /* Set MMIO ranges in K8. */
525         /* Set MMIO TOM - 4G. */
526         SetMMIO(0x400<<12, 0x1000000, 0x80, &MMIO[0]);
527         /* Set MMIO for VGA Legacy FB. */
528         SetMMIO(0xa00, 0xc00, 0x80, &MMIO[0]);
529
530         /* Set MMIO for non prefetchable P2P. */
531         temp = pci_read_config32(dev0x14, 0x20);
532         Base32 = (temp & 0x0fff0) << 8;
533         Limit32 = ((temp & 0x0fff00000) + 0x100000) >> 8;
534         if(Base32 < Limit32)
535         {
536                 Status = GetCreativeMMIO(&CreativeMMIO[0]);
537                 if(Status != CIM_ERROR)
538                         SetMMIO(Base32, Limit32, 0x0, &MMIO[0]);
539         }
540         /* Set MMIO for prefetchable P2P. */
541         if(Status != CIM_ERROR)
542         {
543                 temp = pci_read_config32(dev0x14, 0x24);
544
545                 Base32 = (temp & 0x0fff0) <<8;
546                 Limit32 = ((temp & 0x0fff00000) + 0x100000) >> 8;
547                 if(Base32 < Limit32)
548                         SetMMIO(Base32, Limit32, 0x0, &MMIO[0]);
549         }
550
551         FinalizeMMIO(&MMIO[0]);
552
553         ProgramMMIO(&CreativeMMIO[0], 0, MMIO_ATTRIB_NP_ONLY);
554         ProgramMMIO(&MMIO[0], 0, MMIO_ATTRIB_NP_ONLY | MMIO_ATTRIB_BOTTOM_TO_TOP | MMIO_ATTRIB_SKIP_ZERO);
555 #endif
556
557         pci_dev_init(dev);
558
559         /* clk ind */
560         clkind_write(dev, 0x08, 0x01);
561         clkind_write(dev, 0x0C, 0x22);
562         clkind_write(dev, 0x0F, 0x0);
563         clkind_write(dev, 0x11, 0x0);
564         clkind_write(dev, 0x12, 0x0);
565         clkind_write(dev, 0x14, 0x0);
566         clkind_write(dev, 0x15, 0x0);
567         clkind_write(dev, 0x16, 0x0);
568         clkind_write(dev, 0x17, 0x0);
569         clkind_write(dev, 0x18, 0x0);
570         clkind_write(dev, 0x19, 0x0);
571         clkind_write(dev, 0x1A, 0x0);
572         clkind_write(dev, 0x1B, 0x0);
573         clkind_write(dev, 0x1C, 0x0);
574         clkind_write(dev, 0x1D, 0x0);
575         clkind_write(dev, 0x1E, 0x0);
576         clkind_write(dev, 0x26, 0x0);
577         clkind_write(dev, 0x27, 0x0);
578         clkind_write(dev, 0x28, 0x0);
579         clkind_write(dev, 0x5C, 0x0);
580 }
581
582
583 /*
584 * Set registers in RS780 and CPU to enable the internal GFX.
585 * Please refer to CIM source code and BKDG.
586 */
587 extern uint64_t uma_memory_base, uma_memory_size;
588
589 static void rs780_internal_gfx_enable(device_t dev)
590 {
591         u32 l_dword;
592         int i;
593         device_t nb_dev = dev_find_slot(0, 0);
594         msr_t sysmem;
595
596 #if (CONFIG_GFXUMA == 0)
597         u32 FB_Start, FB_End;
598 #endif
599
600         printk(BIOS_DEBUG, "rs780_internal_gfx_enable dev = 0x%p, nb_dev = 0x%p.\n", dev, nb_dev);
601
602         sysmem = rdmsr(0xc001001a);
603         printk(BIOS_DEBUG, "sysmem = %x_%x\n", sysmem.hi, sysmem.lo);
604
605         /* The system top memory in 780. */
606         pci_write_config32(nb_dev, 0x90, sysmem.lo);
607         htiu_write_index(nb_dev, 0x30, 0);
608         htiu_write_index(nb_dev, 0x31, 0);
609
610         /* Disable external GFX and enable internal GFX. */
611         l_dword = pci_read_config32(nb_dev, 0x8c);
612         l_dword &= ~(1<<0);
613         l_dword |= 1<<1;
614         pci_write_config32(nb_dev, 0x8c, l_dword);
615
616         /* NB_SetDefaultIndexes */
617         pci_write_config32(nb_dev, 0x94, 0x7f);
618         pci_write_config32(nb_dev, 0x60, 0x7f);
619         pci_write_config32(nb_dev, 0xe0, 0);
620
621         /* NB_InitEarlyNB finished. */
622
623         /* LPC DMA Deadlock workaround? */
624         /* GFX_InitCommon*/
625         device_t k8_f0 = dev_find_slot(0, PCI_DEVFN(0x18, 0));
626         l_dword = pci_read_config32(k8_f0, 0x68);
627         l_dword &= ~(3 << 21);
628         l_dword |= (1 << 21);
629         pci_write_config32(k8_f0, 0x68, l_dword);
630
631         /* GFX_InitCommon. */
632         nbmc_write_index(nb_dev, 0x23, 0x00c00010);
633         set_nbmc_enable_bits(nb_dev, 0x16, 1<<15, 1<<15);
634         set_nbmc_enable_bits(nb_dev, 0x25, 0xffffffff, 0x111f111f);
635         set_htiu_enable_bits(nb_dev, 0x37, 1<<24, 1<<24);
636
637 #if (CONFIG_GFXUMA == 1)
638         /* GFX_InitUMA. */
639         /* Copy CPU DDR Controller to NB MC. */
640         device_t k8_f2 = dev_find_slot(0, PCI_DEVFN(0x18, 2));
641         for (i = 0; i < 12; i++)
642         {
643                 l_dword = pci_read_config32(k8_f2, 0x40 + i * 4);
644                 nbmc_write_index(nb_dev, 0x30 + i, l_dword);
645         }
646
647         l_dword = pci_read_config32(k8_f2, 0x80);
648         nbmc_write_index(nb_dev, 0x3c, l_dword);
649
650         l_dword = pci_read_config32(k8_f2, 0x94);
651         if(l_dword & (1<<22))
652                 set_nbmc_enable_bits(nb_dev, 0x3c, 0, 1<<16);
653         else
654                 set_nbmc_enable_bits(nb_dev, 0x3c, 1<<16, 0);
655
656         if(l_dword & (1<<8))
657                 set_nbmc_enable_bits(nb_dev, 0x3c, 0, 1<<17);
658         else
659                 set_nbmc_enable_bits(nb_dev, 0x3c, 1<<17, 0);
660
661         l_dword = pci_read_config32(k8_f2, 0x90);
662         if(l_dword & (1<<10))
663                 set_nbmc_enable_bits(nb_dev, 0x3c, 0, 1<<18);
664         else
665                 set_nbmc_enable_bits(nb_dev, 0x3c, 1<<18, 0);
666
667         /* Set UMA in the 780 side. */
668         /* UMA start address, size. */
669         /* The UMA starts at 0xC0000000 of internal RS780 address space
670             [31:16] addr of last byte | [31:16] addr of first byte
671         */
672         nbmc_write_index(nb_dev, 0x10, ((uma_memory_size - 1 + 0xC0000000) & (~0xffff)) | 0xc000);
673         nbmc_write_index(nb_dev, 0x11, uma_memory_base);
674         nbmc_write_index(nb_dev, 0x12, 0);
675         nbmc_write_index(nb_dev, 0xf0, 256);
676         /* GFX_InitUMA finished. */
677 #else
678         /* GFX_InitSP. */
679         /* SP memory:Hynix HY5TQ1G631ZNFP. 128MB = 64M * 16. 667MHz. DDR3. */
680
681         /* Enable Async mode. */
682         set_nbmc_enable_bits(nb_dev, 0x06, 7<<8, 1<<8);
683         set_nbmc_enable_bits(nb_dev, 0x08, 1<<10, 0);
684         /* The last item in AsynchMclkTaskFileIndex. Why? */
685         /* MC_MPLL_CONTROL2. */
686         nbmc_write_index(nb_dev, 0x07, 0x40100028);
687         /* MC_MPLL_DIV_CONTROL. */
688         nbmc_write_index(nb_dev, 0x0b, 0x00000028);
689         /* MC_MPLL_FREQ_CONTROL. */
690         set_nbmc_enable_bits(nb_dev, 0x09, 3<<12|15<<16|15<<8, 1<<12|4<<16|0<<8);
691         /* MC_MPLL_CONTROL3. For PM. */
692         set_nbmc_enable_bits(nb_dev, 0x08, 0xff<<13, 1<<13|1<<18);
693         /* MPLL_CAL_TRIGGER. */
694         set_nbmc_enable_bits(nb_dev, 0x06, 0, 1<<0);
695         udelay(200); /* time is long enough? */
696         set_nbmc_enable_bits(nb_dev, 0x06, 0, 1<<1);
697         set_nbmc_enable_bits(nb_dev, 0x06, 1<<0, 0);
698         /* MCLK_SRC_USE_MPLL. */
699         set_nbmc_enable_bits(nb_dev, 0x02, 0, 1<<20);
700
701         /* Pre Init MC. */
702         nbmc_write_index(nb_dev, 0x01, 0x88108280);
703         set_nbmc_enable_bits(nb_dev, 0x02, ~(1<<20), 0x00030200);
704         nbmc_write_index(nb_dev, 0x04, 0x08881018);
705         nbmc_write_index(nb_dev, 0x05, 0x000000bb);
706         nbmc_write_index(nb_dev, 0x0c, 0x0f00001f);
707         nbmc_write_index(nb_dev, 0xa1, 0x01f10000);
708         /* MCA_INIT_DLL_PM. */
709         set_nbmc_enable_bits(nb_dev, 0xc9, 1<<24, 1<<24);
710         nbmc_write_index(nb_dev, 0xa2, 0x74f20000);
711         nbmc_write_index(nb_dev, 0xa3, 0x8af30000);
712         nbmc_write_index(nb_dev, 0xaf, 0x47d0a41c);
713         nbmc_write_index(nb_dev, 0xb0, 0x88800130);
714         nbmc_write_index(nb_dev, 0xb1, 0x00000040);
715         nbmc_write_index(nb_dev, 0xb4, 0x41247000);
716         nbmc_write_index(nb_dev, 0xb5, 0x00066664);
717         nbmc_write_index(nb_dev, 0xb6, 0x00000022);
718         nbmc_write_index(nb_dev, 0xb7, 0x00000044);
719         nbmc_write_index(nb_dev, 0xb8, 0xbbbbbbbb);
720         nbmc_write_index(nb_dev, 0xb9, 0xbbbbbbbb);
721         nbmc_write_index(nb_dev, 0xba, 0x55555555);
722         nbmc_write_index(nb_dev, 0xc1, 0x00000000);
723         nbmc_write_index(nb_dev, 0xc2, 0x00000000);
724         nbmc_write_index(nb_dev, 0xc3, 0x80006b00);
725         nbmc_write_index(nb_dev, 0xc4, 0x00066664);
726         nbmc_write_index(nb_dev, 0xc5, 0x00000000);
727         nbmc_write_index(nb_dev, 0xd2, 0x00000022);
728         nbmc_write_index(nb_dev, 0xd3, 0x00000044);
729         nbmc_write_index(nb_dev, 0xd6, 0x00050005);
730         nbmc_write_index(nb_dev, 0xd7, 0x00000000);
731         nbmc_write_index(nb_dev, 0xd8, 0x00700070);
732         nbmc_write_index(nb_dev, 0xd9, 0x00700070);
733         nbmc_write_index(nb_dev, 0xe0, 0x00200020);
734         nbmc_write_index(nb_dev, 0xe1, 0x00200020);
735         nbmc_write_index(nb_dev, 0xe8, 0x00200020);
736         nbmc_write_index(nb_dev, 0xe9, 0x00200020);
737         nbmc_write_index(nb_dev, 0xe0, 0x00180018);
738         nbmc_write_index(nb_dev, 0xe1, 0x00180018);
739         nbmc_write_index(nb_dev, 0xe8, 0x00180018);
740         nbmc_write_index(nb_dev, 0xe9, 0x00180018);
741
742         /* Misc options. */
743         /* Memory Termination. */
744         set_nbmc_enable_bits(nb_dev, 0xa1, 0x0ff, 0x044);
745         set_nbmc_enable_bits(nb_dev, 0xb4, 0xf00, 0xb00);
746 #if 0
747         /* Controller Termation. */
748         set_nbmc_enable_bits(nb_dev, 0xb1, 0x77770000, 0x77770000);
749 #endif
750
751         /* OEM Init MC. 667MHz. */
752         nbmc_write_index(nb_dev, 0xa8, 0x7a5aaa78);
753         nbmc_write_index(nb_dev, 0xa9, 0x514a2319);
754         nbmc_write_index(nb_dev, 0xaa, 0x54400520);
755         nbmc_write_index(nb_dev, 0xab, 0x441460ff);
756         nbmc_write_index(nb_dev, 0xa0, 0x20f00a48);
757         set_nbmc_enable_bits(nb_dev, 0xa2, ~(0xffffffc7), 0x10);
758         nbmc_write_index(nb_dev, 0xb2, 0x00000303);
759         set_nbmc_enable_bits(nb_dev, 0xb1, ~(0xffffff70), 0x45);
760         /* Do it later. */
761         /* set_nbmc_enable_bits(nb_dev, 0xac, ~(0xfffffff0), 0x0b); */
762
763         /* Init PM timing. */
764         for(i=0; i<4; i++)
765         {
766                 l_dword = nbmc_read_index(nb_dev, 0xa0+i);
767                 nbmc_write_index(nb_dev, 0xc8+i, l_dword);
768         }
769         for(i=0; i<4; i++)
770         {
771                 l_dword = nbmc_read_index(nb_dev, 0xa8+i);
772                 nbmc_write_index(nb_dev, 0xcc+i, l_dword);
773         }
774         l_dword = nbmc_read_index(nb_dev, 0xb1);
775         set_nbmc_enable_bits(nb_dev, 0xc8, 0xff<<24, ((l_dword&0x0f)<<24)|((l_dword&0xf00)<<20));
776
777         /* Init MC FB. */
778         /* FB_Start = ; FB_End = ; iSpSize = 0x0080, 128MB. */
779         nbmc_write_index(nb_dev, 0x11, 0x40000000);
780         FB_Start = 0xc00 + 0x080;
781         FB_End = 0xc00 + 0x080;
782         nbmc_write_index(nb_dev, 0x10, (((FB_End&0xfff)<<20)-0x10000)|(((FB_Start&0xfff)-0x080)<<4));
783         set_nbmc_enable_bits(nb_dev, 0x0d, ~0x000ffff0, (FB_Start&0xfff)<<20);
784         nbmc_write_index(nb_dev, 0x0f, 0);
785         nbmc_write_index(nb_dev, 0x0e, (FB_Start&0xfff)|(0xaaaa<<12));
786 #endif
787
788         /* GFX_InitSP finished. */
789 }
790
791 static struct pci_operations lops_pci = {
792         .set_subsystem = pci_dev_set_subsystem,
793 };
794
795 static struct device_operations pcie_ops = {
796         .read_resources = rs780_gfx_read_resources,
797         .set_resources = pci_dev_set_resources,
798         .enable_resources = pci_dev_enable_resources,
799         .init = internal_gfx_pci_dev_init,      /* The option ROM initializes the device. rs780_gfx_init, */
800         .scan_bus = 0,
801         .enable = rs780_internal_gfx_enable,
802         .ops_pci = &lops_pci,
803 };
804
805 /*
806  * We should list all of them here.
807  * */
808 static const struct pci_driver pcie_driver_780 __pci_driver = {
809         .ops = &pcie_ops,
810         .vendor = PCI_VENDOR_ID_ATI,
811         .device = PCI_DEVICE_ID_ATI_RS780_INT_GFX,
812 };
813
814 static const struct pci_driver pcie_driver_780c __pci_driver = {
815         .ops = &pcie_ops,
816         .vendor = PCI_VENDOR_ID_ATI,
817         .device = PCI_DEVICE_ID_ATI_RS780C_INT_GFX,
818 };
819 static const struct pci_driver pcie_driver_780m __pci_driver = {
820         .ops = &pcie_ops,
821         .vendor = PCI_VENDOR_ID_ATI,
822         .device = PCI_DEVICE_ID_ATI_RS780M_INT_GFX,
823 };
824 static const struct pci_driver pcie_driver_780mc __pci_driver = {
825         .ops = &pcie_ops,
826         .vendor = PCI_VENDOR_ID_ATI,
827         .device = PCI_DEVICE_ID_ATI_RS780MC_INT_GFX,
828 };
829 static const struct pci_driver pcie_driver_780e __pci_driver = {
830         .ops = &pcie_ops,
831         .vendor = PCI_VENDOR_ID_ATI,
832         .device = PCI_DEVICE_ID_ATI_RS780E_INT_GFX,
833 };
834 static const struct pci_driver pcie_driver_785g __pci_driver = {
835         .ops = &pcie_ops,
836         .vendor = PCI_VENDOR_ID_ATI,
837         .device = PCI_DEVICE_ID_ATI_RS785G_INT_GFX,
838 };
839
840 /* step 12 ~ step 14 from rpr */
841 static void single_port_configuration(device_t nb_dev, device_t dev)
842 {
843         u8 result, width;
844         u32 reg32;
845         struct southbridge_amd_rs780_config *cfg =
846             (struct southbridge_amd_rs780_config *)nb_dev->chip_info;
847
848         printk(BIOS_DEBUG, "rs780_gfx_init single_port_configuration.\n");
849
850         /* step 12 training, releases hold training for GFX port 0 (device 2) */
851         PcieReleasePortTraining(nb_dev, dev, 2);
852         result = PcieTrainPort(nb_dev, dev, 2);
853         printk(BIOS_DEBUG, "rs780_gfx_init single_port_configuration step12.\n");
854
855         /* step 13 Power Down Control */
856         /* step 13.1 Enables powering down transmitter and receiver pads along with PLL macros. */
857         set_pcie_enable_bits(nb_dev, 0x40, 1 << 0, 1 << 0);
858
859         /* step 13.a Link Training was NOT successful */
860         if (!result) {
861                 set_nbmisc_enable_bits(nb_dev, 0x8, 0, 0x3 << 4); /* prevent from training. */
862                 set_nbmisc_enable_bits(nb_dev, 0xc, 0, 0x3 << 2); /* hide the GFX bridge. */
863                 if (cfg->gfx_tmds)
864                         nbpcie_ind_write_index(nb_dev, 0x65, 0xccf0f0);
865                 else {
866                         nbpcie_ind_write_index(nb_dev, 0x65, 0xffffffff);
867                         set_nbmisc_enable_bits(nb_dev, 0x7, 1 << 3, 1 << 3);
868                 }
869         } else {                /* step 13.b Link Training was successful */
870                 set_pcie_enable_bits(dev, 0xA2, 0xFF, 0x1);
871                 reg32 = nbpcie_p_read_index(dev, 0x29);
872                 width = reg32 & 0xFF;
873                 printk(BIOS_DEBUG, "GFX Inactive Lanes = 0x%x.\n", width);
874                 switch (width) {
875                 case 1:
876                 case 2:
877                         nbpcie_ind_write_index(nb_dev, 0x65,
878                                                cfg->gfx_lane_reversal ? 0x7f7f : 0xccfefe);
879                         break;
880                 case 4:
881                         nbpcie_ind_write_index(nb_dev, 0x65,
882                                                cfg->gfx_lane_reversal ? 0x3f3f : 0xccfcfc);
883                         break;
884                 case 8:
885                         nbpcie_ind_write_index(nb_dev, 0x65,
886                                                cfg->gfx_lane_reversal ? 0x0f0f : 0xccf0f0);
887                         break;
888                 }
889         }
890         printk(BIOS_DEBUG, "rs780_gfx_init single_port_configuration step13.\n");
891
892         /* step 14 Reset Enumeration Timer, disables the shortening of the enumeration timer */
893         set_pcie_enable_bits(dev, 0x70, 1 << 19, 1 << 19);
894         printk(BIOS_DEBUG, "rs780_gfx_init single_port_configuration step14.\n");
895 }
896
897 static void dual_port_configuration(device_t nb_dev, device_t dev)
898 {
899         u8 result, width;
900         u32 reg32, dev_ind = dev->path.pci.devfn >> 3;
901         struct southbridge_amd_rs780_config *cfg =
902                     (struct southbridge_amd_rs780_config *)nb_dev->chip_info;
903
904         /* 5.4.1.2 Dual Port Configuration */
905         set_nbmisc_enable_bits(nb_dev, 0x36, 1 << 31, 1 << 31);
906         set_nbmisc_enable_bits(nb_dev, 0x08, 0xF << 8, 0x5 << 8);
907         set_nbmisc_enable_bits(nb_dev, 0x36, 1 << 31, 0 << 31);
908
909         /* 5.7. Training for Device 2 */
910         /* 5.7.1. Releases hold training for GFX port 0 (device 2) */
911         PcieReleasePortTraining(nb_dev, dev, dev_ind);
912         /* 5.7.2- 5.7.9. PCIE Link Training Sequence */
913         result = PcieTrainPort(nb_dev, dev, dev_ind);
914
915         /* Power Down Control for Device 2 */
916         /* Link Training was NOT successful */
917         if (!result) {
918                 /* Powers down all lanes for port A */
919                 /* nbpcie_ind_write_index(nb_dev, 0x65, 0x0f0f); */
920                 /* Note: I have to disable the slot where there isnt a device,
921                  * otherwise the system will hang. I dont know why. */
922                 set_nbmisc_enable_bits(nb_dev, 0x0c, 1 << dev_ind, 1 << dev_ind);
923
924         } else {                /* step 16.b Link Training was successful */
925                 reg32 = nbpcie_p_read_index(dev, 0xa2);
926                 width = (reg32 >> 4) & 0x7;
927                 printk(BIOS_DEBUG, "GFX LC_LINK_WIDTH = 0x%x.\n", width);
928                 switch (width) {
929                 case 1:
930                 case 2:
931                         nbpcie_ind_write_index(nb_dev, 0x65,
932                                                cfg->gfx_lane_reversal ? 0x0707 : 0x0e0e);
933                         break;
934                 case 4:
935                         nbpcie_ind_write_index(nb_dev, 0x65,
936                                                cfg->gfx_lane_reversal ? 0x0303 : 0x0c0c);
937                         break;
938                 }
939         }
940 }
941
942 /* For single port GFX configuration Only
943 * width:
944 *       000 = x16
945 *       001 = x1
946 *       010 = x2
947 *       011 = x4
948 *       100 = x8
949 *       101 = x12 (not supported)
950 *       110 = x16
951 */
952 static void dynamic_link_width_control(device_t nb_dev, device_t dev, u8 width)
953 {
954         u32 reg32;
955         device_t sb_dev;
956         struct southbridge_amd_rs780_config *cfg =
957             (struct southbridge_amd_rs780_config *)nb_dev->chip_info;
958
959         /* step 5.9.1.1 */
960         reg32 = nbpcie_p_read_index(dev, 0xa2);
961
962         /* step 5.9.1.2 */
963         set_pcie_enable_bits(nb_dev, 0x40, 1 << 0, 1 << 0);
964         /* step 5.9.1.3 */
965         set_pcie_enable_bits(dev, 0xa2, 3 << 0, width << 0);
966         /* step 5.9.1.4 */
967         set_pcie_enable_bits(dev, 0xa2, 1 << 8, 1 << 8);
968         /* step 5.9.2.4 */
969         if (0 == cfg->gfx_reconfiguration)
970                 set_pcie_enable_bits(dev, 0xa2, 1 << 11, 1 << 11);
971
972         /* step 5.9.1.5 */
973         do {
974                 reg32 = nbpcie_p_read_index(dev, 0xa2);
975         }
976         while (reg32 & 0x100);
977
978         /* step 5.9.1.6 */
979         sb_dev = dev_find_slot(0, PCI_DEVFN(8, 0));
980         do {
981                 reg32 = pci_ext_read_config32(nb_dev, sb_dev,
982                                           PCIE_VC0_RESOURCE_STATUS);
983         } while (reg32 & VC_NEGOTIATION_PENDING);
984
985         /* step 5.9.1.7 */
986         reg32 = nbpcie_p_read_index(dev, 0xa2);
987         if (((reg32 & 0x70) >> 4) != 0x6) {
988                 /* the unused lanes should be powered off. */
989         }
990
991         /* step 5.9.1.8 */
992         set_pcie_enable_bits(nb_dev, 0x40, 1 << 0, 0 << 0);
993 }
994
995 /*
996 * GFX Core initialization, dev2, dev3
997 */
998 void rs780_gfx_init(device_t nb_dev, device_t dev, u32 port)
999 {
1000         u32 reg32;
1001         struct southbridge_amd_rs780_config *cfg =
1002             (struct southbridge_amd_rs780_config *)nb_dev->chip_info;
1003
1004         printk(BIOS_DEBUG, "rs780_gfx_init, nb_dev=0x%p, dev=0x%p, port=0x%x.\n",
1005                     nb_dev, dev, port);
1006
1007         /* GFX Core Initialization */
1008         //if (port == 2) return;
1009
1010         /* step 2, TMDS, (only need if CMOS option is enabled) */
1011         if (cfg->gfx_tmds) {
1012         }
1013
1014 #if 1                           /* external clock mode */
1015         /* table 5-22, 5.9.1. REFCLK */
1016         /* 5.9.1.1. Disables the GFX REFCLK transmitter so that the GFX
1017          * REFCLK PAD can be driven by an external source. */
1018         /* 5.9.1.2. Enables GFX REFCLK receiver to receive the REFCLK from an external source. */
1019         set_nbmisc_enable_bits(nb_dev, 0x38, 1 << 29 | 1 << 28, 0 << 29 | 1 << 28);
1020
1021         /* 5.9.1.3 Selects the GFX REFCLK to be the source for PLL A. */
1022         /* 5.9.1.4 Selects the GFX REFCLK to be the source for PLL B. */
1023         /* 5.9.1.5 Selects the GFX REFCLK to be the source for PLL C. */
1024         set_nbmisc_enable_bits(nb_dev, 0x28, 3 << 6 | 3 << 8 | 3 << 10,
1025                                1 << 6 | 1 << 8 | 1 << 10);
1026         reg32 = nbmisc_read_index(nb_dev, 0x28);
1027         printk(BIOS_DEBUG, "misc 28 = %x\n", reg32);
1028
1029         /* 5.9.1.6.Selects the single ended GFX REFCLK to be the source for core logic. */
1030         set_nbmisc_enable_bits(nb_dev, 0x6C, 1 << 31, 1 << 31);
1031 #else                           /* internal clock mode */
1032         /* table 5-23, 5.9.1. REFCLK */
1033         /* 5.9.1.1. Enables the GFX REFCLK transmitter so that the GFX
1034          * REFCLK PAD can be driven by the SB REFCLK. */
1035         /* 5.9.1.2. Disables GFX REFCLK receiver from receiving the
1036          * REFCLK from an external source.*/
1037         set_nbmisc_enable_bits(nb_dev, 0x38, 1 << 29 | 1 << 28, 1 << 29 | 0 << 28);
1038
1039         /* 5.9.1.3 Selects the GFX REFCLK to be the source for PLL A. */
1040         /* 5.9.1.4 Selects the GFX REFCLK to be the source for PLL B. */
1041         /* 5.9.1.5 Selects the GFX REFCLK to be the source for PLL C. */
1042         set_nbmisc_enable_bits(nb_dev, 0x28, 3 << 6 | 3 << 8 | 3 << 10,
1043                                0);
1044         reg32 = nbmisc_read_index(nb_dev, 0x28);
1045         printk(BIOS_DEBUG, "misc 28 = %x\n", reg32);
1046
1047         /* 5.9.1.6.Selects the single ended GFX REFCLK to be the source for core logic. */
1048         set_nbmisc_enable_bits(nb_dev, 0x6C, 1 << 31, 0 << 31);
1049 #endif
1050
1051         /* step 5.9.3, GFX overclocking, (only need if CMOS option is enabled) */
1052         /* 5.9.3.1. Increases PLL BW for 6G operation.*/
1053         /* set_nbmisc_enable_bits(nb_dev, 0x36, 0x3FF << 4, 0xB5 << 4); */
1054         /* skip */
1055
1056         /* step 5.9.4, reset the GFX link */
1057         /* step 5.9.4.1 asserts both calibration reset and global reset */
1058         set_nbmisc_enable_bits(nb_dev, 0x8, 0x3 << 14, 0x3 << 14);
1059
1060         /* step 5.9.4.2 de-asserts calibration reset */
1061         set_nbmisc_enable_bits(nb_dev, 0x8, 1 << 14, 0 << 14);
1062
1063         /* step 5.9.4.3 wait for at least 200us */
1064         udelay(300);
1065
1066         /* step 5.9.4.4 de-asserts global reset */
1067         set_nbmisc_enable_bits(nb_dev, 0x8, 1 << 15, 0 << 15);
1068
1069         /* 5.9.5 Reset PCIE_GFX Slot */
1070         /* It is done in mainboard.c */
1071         set_pcie_reset();
1072         mdelay(1);
1073         set_pcie_dereset();
1074
1075         /* step 5.9.8 program PCIE memory mapped configuration space */
1076         /* done by enable_pci_bar3() before */
1077
1078         /* step 7 compliance state, (only need if CMOS option is enabled) */
1079         /* the compliance stete is just for test. refer to 4.2.5.2 of PCIe specification */
1080         if (cfg->gfx_compliance) {
1081                 /* force compliance */
1082                 set_nbmisc_enable_bits(nb_dev, 0x32, 1 << 6, 1 << 6);
1083                 /* release hold training for device 2. GFX initialization is done. */
1084                 set_nbmisc_enable_bits(nb_dev, 0x8, 1 << 4, 0 << 4);
1085                 dynamic_link_width_control(nb_dev, dev, cfg->gfx_link_width);
1086                 printk(BIOS_DEBUG, "rs780_gfx_init step7.\n");
1087                 return;
1088         }
1089
1090         /* 5.9.12 Core Initialization. */
1091         /* 5.9.12.1 sets RCB timeout to be 25ms */
1092         /* 5.9.12.2. RCB Cpl timeout on link down. */
1093         set_pcie_enable_bits(dev, 0x70, 7 << 16 | 1 << 19, 4 << 16 | 1 << 19);
1094         printk(BIOS_DEBUG, "rs780_gfx_init step5.9.12.1.\n");
1095
1096         /* step 5.9.12.3 disables slave ordering logic */
1097         set_pcie_enable_bits(nb_dev, 0x20, 1 << 8, 1 << 8);
1098         printk(BIOS_DEBUG, "rs780_gfx_init step5.9.12.3.\n");
1099
1100         /* step 5.9.12.4 sets DMA payload size to 64 bytes */
1101         set_pcie_enable_bits(nb_dev, 0x10, 7 << 10, 4 << 10);
1102         /* 5.9.12.5. Blocks DMA traffic during C3 state. */
1103         set_pcie_enable_bits(dev, 0x10, 1 << 0, 0 << 0);
1104
1105         /* 5.9.12.6. Disables RC ordering logic */
1106         set_pcie_enable_bits(nb_dev, 0x20, 1 << 9, 1 << 9);
1107
1108         /* Enabels TLP flushing. */
1109         /* Note: It is got from RS690. The system will hang without this action. */
1110         set_pcie_enable_bits(dev, 0x20, 1 << 19, 0 << 19);
1111
1112         /* 5.9.12.7. Ignores DLLPs during L1 so that txclk can be turned off */
1113         set_pcie_enable_bits(nb_dev, 0x2, 1 << 0, 1 << 0);
1114
1115         /* 5.9.12.8 Prevents LC to go from L0 to Rcv_L0s if L1 is armed. */
1116         set_pcie_enable_bits(dev, 0xA1, 1 << 11, 1 << 11);
1117
1118         /* 5.9.12.9 CMGOOD_OVERRIDE for end point initiated lane degradation. */
1119         set_nbmisc_enable_bits(nb_dev, 0x6a, 1 << 17, 1 << 17);
1120         printk(BIOS_DEBUG, "rs780_gfx_init step5.9.12.9.\n");
1121
1122         /* 5.9.12.10 Sets the timer in Config state from 20us to */
1123         /* 5.9.12.11 De-asserts RX_EN in L0s. */
1124         /* 5.9.12.12 Enables de-assertion of PG2RX_CR_EN to lock clock
1125          * recovery parameter when lane is in electrical idle in L0s.*/
1126         set_pcie_enable_bits(dev, 0xB1, 1 << 23 | 1 << 19 | 1 << 28, 1 << 23 | 1 << 19 | 1 << 28);
1127
1128         /* 5.9.12.13. Turns off offset calibration. */
1129         /* 5.9.12.14. Enables Rx Clock gating in CDR */
1130         set_nbmisc_enable_bits(nb_dev, 0x34, 1 << 10/* | 1 << 22 */, 1 << 10/* | 1 << 22 */);
1131
1132         /* 5.9.12.15. Sets number of TX Clocks to drain TX Pipe to 3. */
1133         set_pcie_enable_bits(dev, 0xA0, 0xF << 4, 3 << 4);
1134
1135         /* 5.9.12.16. Lets PI use Electrical Idle from PHY when
1136          * turning off PLL in L1 at Gen2 speed instead Inferred Electrical Idle. */
1137         set_pcie_enable_bits(nb_dev, 0x40, 3 << 14, 2 << 14);
1138
1139         /* 5.9.12.17. Prevents the Electrical Idle from causing a transition from Rcv_L0 to Rcv_L0s. */
1140         set_pcie_enable_bits(dev, 0xB1, 1 << 20, 1 << 20);
1141
1142         /* 5.9.12.18. Prevents the LTSSM from going to Rcv_L0s if it has already
1143          * acknowledged a request to go to L1. */
1144         set_pcie_enable_bits(dev, 0xA1, 1 << 11, 1 << 11);
1145
1146         /* 5.9.12.19. LDSK only taking deskew on deskewing error detect */
1147         set_pcie_enable_bits(nb_dev, 0x40, 1 << 28, 0 << 28);
1148
1149         /* 5.9.12.20. Bypasses lane de-skew logic if in x1 */
1150         set_pcie_enable_bits(nb_dev, 0xC2, 1 << 14, 1 << 14);
1151
1152         /* 5.9.12.21. Sets Electrical Idle Threshold. */
1153         set_nbmisc_enable_bits(nb_dev, 0x35, 3 << 21, 2 << 21);
1154
1155         /* 5.9.12.22. Advertises -6 dB de-emphasis value in TS1 Data Rate Identifier
1156          * Only if CMOS Option in section. skip */
1157
1158         /* 5.9.12.23. Disables GEN2 capability of the device. */
1159         set_pcie_enable_bits(dev, 0xA4, 1 << 0, 0 << 0);
1160
1161         /* 5.9.12.24.Disables advertising Upconfigure Support. */
1162         set_pcie_enable_bits(dev, 0xA2, 1 << 13, 1 << 13);
1163
1164         /* 5.9.12.25. No comment in RPR. */
1165         set_nbmisc_enable_bits(nb_dev, 0x39, 1 << 10, 0 << 10);
1166
1167         /* 5.9.12.26. This capacity is required since links wider than x1 and/or multiple link
1168          * speed are supported */
1169         set_pcie_enable_bits(nb_dev, 0xC1, 1 << 0, 1 << 0);
1170
1171         /* 5.9.12.27. Enables NVG86 ECO. A13 above only. */
1172         if (get_nb_rev(nb_dev) == REV_RS780_A12)                        /* A12 */
1173                 set_pcie_enable_bits(dev, 0x02, 1 << 11, 1 << 11);
1174
1175         /* 5.9.12.28 Hides and disables the completion timeout method. */
1176         set_pcie_enable_bits(nb_dev, 0xC1, 1 << 2, 0 << 2);
1177
1178         /* 5.9.12.29. Use the bif_core de-emphasis strength by default. */
1179         /* set_nbmisc_enable_bits(nb_dev, 0x36, 1 << 28, 1 << 28); */
1180
1181         /* 5.9.12.30. Set TX arbitration algorithm to round robin */
1182         set_pcie_enable_bits(nb_dev, 0x1C,
1183                              1 << 0 | 0x1F << 1 | 0x1F << 6,
1184                              1 << 0 | 0x04 << 1 | 0x04 << 6);
1185
1186         /* Single-port/Dual-port configureation. */
1187         switch (cfg->gfx_dual_slot) {
1188         case 0:
1189                 /* step 1, lane reversal (only need if build config option is enabled) */
1190                 if (cfg->gfx_lane_reversal) {
1191                         set_nbmisc_enable_bits(nb_dev, 0x36, 1 << 31, 1 << 31);
1192                         set_nbmisc_enable_bits(nb_dev, 0x33, 1 << 2, 1 << 2);
1193                         set_nbmisc_enable_bits(nb_dev, 0x36, 1 << 31, 0 << 31);
1194                 }
1195                 printk(BIOS_DEBUG, "rs780_gfx_init step1.\n");
1196
1197                 printk(BIOS_DEBUG, "device = %x\n", dev->path.pci.devfn >> 3);
1198                 if((dev->path.pci.devfn >> 3) == 2) {
1199                         single_port_configuration(nb_dev, dev);
1200                 } else {
1201                         set_nbmisc_enable_bits(nb_dev, 0xc, 0, 0x2 << 2); /* hide the GFX bridge. */
1202                         printk(BIOS_INFO, "Single port. Do nothing.\n"); // If dev3
1203                 }
1204
1205                 break;
1206         case 1:
1207                 /* step 1, lane reversal (only need if build config option is enabled) */
1208                 if (cfg->gfx_lane_reversal) {
1209                         set_nbmisc_enable_bits(nb_dev, 0x36, 1 << 31, 1 << 31);
1210                         set_nbmisc_enable_bits(nb_dev, 0x33, 1 << 2, 1 << 2);
1211                         set_nbmisc_enable_bits(nb_dev, 0x33, 1 << 3, 1 << 3);
1212                         set_nbmisc_enable_bits(nb_dev, 0x36, 1 << 31, 0 << 31);
1213                 }
1214                 printk(BIOS_DEBUG, "rs780_gfx_init step1.\n");
1215                 /* step 1.1, dual-slot gfx configuration (only need if CMOS option is enabled) */
1216                 /* AMD calls the configuration CrossFire */
1217                 set_nbmisc_enable_bits(nb_dev, 0x0, 0xf << 8, 5 << 8);
1218                 printk(BIOS_DEBUG, "rs780_gfx_init step2.\n");
1219
1220                 printk(BIOS_DEBUG, "device = %x\n", dev->path.pci.devfn >> 3);
1221                 dual_port_configuration(nb_dev, dev);
1222                 break;
1223
1224         case 2:
1225
1226                 if(is_dev3_present()){
1227                         /* step 1, lane reversal (only need if CMOS option is enabled) */
1228                         if (cfg->gfx_lane_reversal) {
1229                                 set_nbmisc_enable_bits(nb_dev, 0x33, 1 << 2, 1 << 2);
1230                                 set_nbmisc_enable_bits(nb_dev, 0x33, 1 << 3, 1 << 3);
1231                         }
1232                         printk(BIOS_DEBUG, "rs780_gfx_init step1.\n");
1233                         /* step 1.1, dual-slot gfx configuration (only need if CMOS option is enabled) */
1234                         /* AMD calls the configuration CrossFire */
1235                         set_nbmisc_enable_bits(nb_dev, 0x0, 0xf << 8, 5 << 8);
1236                         printk(BIOS_DEBUG, "rs780_gfx_init step2.\n");
1237
1238
1239                         printk(BIOS_DEBUG, "device = %x\n", dev->path.pci.devfn >> 3);
1240                         dual_port_configuration(nb_dev, dev);
1241
1242                 }else{
1243                         if (cfg->gfx_lane_reversal) {
1244                                 set_nbmisc_enable_bits(nb_dev, 0x33, 1 << 2, 1 << 2);
1245                         }
1246                         printk(BIOS_DEBUG, "rs780_gfx_init step1.\n");
1247                         printk(BIOS_DEBUG, "rs780_gfx_init step2.\n");
1248
1249                         if((dev->path.pci.devfn >> 3) == 2)
1250                                 single_port_configuration(nb_dev, dev);
1251                         else{
1252                                 set_nbmisc_enable_bits(nb_dev, 0xc, 0, 0x2 << 2); /* hide the GFX bridge. */
1253                                 printk(BIOS_DEBUG, "If dev3.., single port. Do nothing.\n");
1254                             }
1255                 }
1256
1257         default:
1258                 printk(BIOS_INFO, "Incorrect configuration of external GFX slot.\n");
1259                 break;
1260         }
1261 }