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