printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / northbridge / amd / gx2 / chipsetinit.c
1 #include <console/console.h>
2 #include <arch/io.h>
3 #include <stdint.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/pci_ids.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <bitops.h>
10 #include "chip.h"
11 #include "northbridge.h"
12 #include <cpu/amd/gx2def.h>
13 #include <cpu/x86/msr.h>
14 #include <cpu/x86/cache.h>
15 #include "../../../southbridge/amd/cs5536/cs5536.h"
16
17 extern int sizeram(void);
18 /* the structs in this file only set msr.lo. But ... that may not always be true */
19
20 struct msrinit {
21         unsigned long msrnum;
22         msr_t msr;
23 };
24
25 /*  Master Configuration Register for Bus Masters.*/
26 struct msrinit SB_MASTER_CONF_TABLE[] = {
27         {USB1_SB_GLD_MSR_CONF,  {.hi=0,.lo=0x00008f000}},       /*  NOTE: Must be 1st entry in table*/
28         {USB2_SB_GLD_MSR_CONF,  {.hi=0,.lo=0x00008f000}},
29         {ATA_SB_GLD_MSR_CONF,   {.hi=0,.lo=0x00048f000}},
30         {AC97_SB_GLD_MSR_CONF,  {.hi=0,.lo=0x00008f000}},
31         {MDD_SB_GLD_MSR_CONF,   {.hi=0,.lo=0x00000f000}},
32 /* GLPCI_SB_GLD_MSR_CONF,       0x0FFFFFFFF*/
33 /* GLCP_SB_GLD_MSR_CONF,        0x0FFFFFFFF*/
34 /* GLIU_SB_GLD_MSR_CONF,        0x0*/
35         {0,{0,0}}
36 };
37
38 /*  5535_A3 Clock Gating*/
39 struct msrinit CS5535_CLOCK_GATING_TABLE[] = {
40         {      USB1_SB_GLD_MSR_PM,      {.hi=0,.lo=0x000000005}},
41         {      USB2_SB_GLD_MSR_PM,      {.hi=0,.lo=0x000000005}},
42         {      GLIU_SB_GLD_MSR_PM,      {.hi=0,.lo=0x000000004}},
43         {      GLPCI_SB_GLD_MSR_PM,     {.hi=0,.lo=0x000000005}},
44         {      GLCP_SB_GLD_MSR_PM,      {.hi=0,.lo=0x000000004}},
45         {      MDD_SB_GLD_MSR_PM,       {.hi=0,.lo=0x050554111}},
46         {      ATA_SB_GLD_MSR_PM,       {.hi=0,.lo=0x000000005}},
47         {       AC97_SB_GLD_MSR_PM,     {.hi=0,.lo=0x000000005}},
48         {0,{0,0}}
49 };
50
51 /*  5536 Clock Gating*/
52 struct msrinit CS5536_CLOCK_GATING_TABLE[] = {
53 /* MSR            Setting*/
54         {      GLIU_SB_GLD_MSR_PM,      {.hi=0,.lo=0x000000004}},
55         {      GLPCI_SB_GLD_MSR_PM,     {.hi=0,.lo=0x000000005}},
56         {      GLCP_SB_GLD_MSR_PM,      {.hi=0,.lo=0x000000004}},
57         {      MDD_SB_GLD_MSR_PM,       {.hi=0,.lo=0x050554111}}, /*  SMBus clock gating errata (PBZ 2226 & SiBZ 3977)*/
58         {      ATA_SB_GLD_MSR_PM,       {.hi=0,.lo=0x000000005}},
59         {      AC97_SB_GLD_MSR_PM,      {.hi=0,.lo=0x000000005}},
60         {0,{0,0}}
61 };
62
63 struct acpiinit {
64         unsigned short ioreg; 
65         unsigned long regdata;
66         unsigned short iolen;
67 };
68
69 struct acpiinit acpi_init_table[] = {
70         {ACPI_BASE+0x00, 0x01000000, 4},
71         {ACPI_BASE+0x08, 0, 4},
72         {ACPI_BASE+0x0C, 0, 4},
73         {ACPI_BASE+0x1C, 0, 4},
74         {ACPI_BASE+0x18, 0x0FFFFFFFF, 4},
75         {ACPI_BASE+0x00, 0x0000FFFF, 4},
76
77         {PM_SCLK, 0x000000E00, 4},
78         {PM_SED,  0x000004601, 4},
79         {PM_SIDD, 0x000008C02, 4},
80         {PM_WKD,  0x0000000A0, 4},
81         {PM_WKXD, 0x0000000A0, 4},
82         {0,0,0}
83 };
84
85 /* return 1 if we are a 5536-based system */
86 static int is_5536(void){
87         msr_t msr;
88         msr = rdmsr(GLIU_SB_GLD_MSR_CAP);
89         msr.lo >>= 20;
90         printk(BIOS_DEBUG, "is_5536: msr.lo is 0x%x(==5 means 5536)\n", msr.lo&0xf);
91         return ((msr.lo&0xf) == 5);
92 }
93 /* ***************************************************************************/
94 /* **/
95 /* *    pmChipsetInit*/
96 /* **/
97 /* *    Program ACPI LBAR and initialize ACPI registers.*/
98 /* *  */
99 /* **/
100 /* *    Entry:*/
101 /* *            None*/
102 /* **/
103 /* *    Exit:*/
104 /* *            None*/
105 /* **/
106 /* *    Destroys:*/
107 /* *            None*/
108 /* **/
109 /* ***************************************************************************/
110 static void
111 pmChipsetInit(void) {
112         unsigned long val = 0;
113         unsigned short port;
114
115         port =  (PMLogic_BASE + 0x010);
116         val =  0x0E00           ; /*  1ms*/
117         outl(val, port);
118
119         /*  PM_WKXD*/
120         /*  Make sure bits[3:0]=0000b to clear the*/
121         /*  saved Sx state*/
122         port =  (PMLogic_BASE + 0x034);
123         val =  0x0A0            ; /*  5ms*/
124         outl(val, port);
125         
126         /*  PM_WKD*/
127         port =  (PMLogic_BASE + 0x030);
128         outl(val, port);
129                 
130         /*  PM_SED*/
131         port =  (PMLogic_BASE + 0x014);
132 /*      mov             eax, 0x057642   ; 100ms, works*/
133         val =  0x04601          ; /*  5ms*/
134         outl(val, port);
135         
136         /*  PM_SIDD*/
137         port =  (PMLogic_BASE + 0x020);
138 /*      mov             eax, 0x0AEC84   ; 200ms, works*/
139         val =  0x08C02          ; /*  10ms*/
140         outl(val, port);
141         
142         /*  GPIO24 OUT_AUX1 function is the external signal for 5535's vsb_working_aux*/
143         /*  which is de-asserted when 5535 enters Standby(S3 or S5) state.*/
144         /*  On Hawk, GPIO24 controls all voltage rails except Vmem and Vstandby.  This means*/
145         /*  GX2 will be fully de-powered if this control de-asserts in S3/S5.*/
146         /* */
147         /*  GPIO24 is setup in preChipsetInit for two reasons*/
148         /*  1. GPIO24 at reset defaults to disabled, since this signal is vsb_work_aux on*/
149         /*     Hawk it controls the FET's for all voltage rails except Vstanby & Vmem.*/
150         /*     BIOS needs to enable GPIO24 as OUT_AUX1 & OUTPUT_EN early so it is driven*/
151         /*     by 5535.*/
152         /*  2. Non-PM builds will require GPIO24 enabled for instant-off power button*/
153         /* */
154
155         /*  GPIO11 OUT_AUX1 function is the external signal for 5535's slp_clk_n which is asserted*/
156         /*  when 5535 enters Sleep(S1) state.*/
157         /*  On Hawk, GPIO11 is connected to control input of external clock generator*/
158         /*  for 14MHz, PCI, USB & LPC clocks.*/
159         /*  Programming of GPIO11 will be done by VSA PM code.  During VSA Init. BIOS writes*/
160         /*  PM Core Virual Register indicating if S1 Clocks should be On or Off. This is based*/
161         /*  on a Setup item.  We do not want to leave GPIO11 enabled because of a Hawk board*/
162         /*  problem.  With GPIO11 enabled in S3, something is back-driving GPIO11 causing it to*/
163         /*  float to 1.6-1.7V.*/
164
165 }
166
167 struct FLASH_DEVICE {
168         unsigned char fType;            /* Flash type: NOR or NAND */
169         unsigned char fInterface;       /* Flash interface: I/O or Memory */
170         unsigned long fMask;            /* Flash size/mask */
171 };
172
173 struct FLASH_DEVICE FlashInitTable[] = {
174         { FLASH_TYPE_NAND, FLASH_IF_MEM, FLASH_MEM_4K },        /* CS0, or Flash Device 0 */
175         { FLASH_TYPE_NONE, 0, 0 },      /* CS1, or Flash Device 1 */
176         { FLASH_TYPE_NONE, 0, 0 },      /* CS2, or Flash Device 2 */
177         { FLASH_TYPE_NONE, 0, 0 },      /* CS3, or Flash Device 3 */
178 };
179
180 #define FlashInitTableLen (ARRAY_SIZE(FlashInitTable))
181
182 uint32_t FlashPort[] = {
183         MDD_LBAR_FLSH0,
184         MDD_LBAR_FLSH1,
185         MDD_LBAR_FLSH2,
186         MDD_LBAR_FLSH3
187         };
188
189 /***************************************************************************
190  *
191  *      ChipsetFlashSetup
192  *
193  *      Flash LBARs need to be setup before VSA init so the PCI BARs have
194  *      correct size info.  Call this routine only if flash needs to be 
195  *      configured (don't call it if you want IDE).
196  *
197  *      Entry:
198  *      Exit:
199  *      Destroys:
200  *
201  **************************************************************************/
202 static void ChipsetFlashSetup(void)
203 {
204         msr_t msr;
205         int i;
206         int numEnabled = 0;
207
208         printk(BIOS_DEBUG, "ChipsetFlashSetup++\n");
209         for (i = 0; i < FlashInitTableLen; i++) {
210                 if (FlashInitTable[i].fType != FLASH_TYPE_NONE) {
211                         printk(BIOS_DEBUG, "Enable CS%d\n", i);
212                         /* we need to configure the memory/IO mask */
213                         msr = rdmsr(FlashPort[i]);
214                         msr.hi = 0;     /* start with the "enabled" bit clear */
215                         if (FlashInitTable[i].fType == FLASH_TYPE_NAND)
216                                 msr.hi |= 0x00000002;
217                         else
218                                 msr.hi &= ~0x00000002;
219                         if (FlashInitTable[i].fInterface == FLASH_IF_MEM)
220                                 msr.hi |= 0x00000004;
221                         else
222                                 msr.hi &= ~0x00000004;
223                         msr.hi |= FlashInitTable[i].fMask;
224                         printk(BIOS_DEBUG, "WRMSR(0x%08X, %08X_%08X)\n", FlashPort[i], msr.hi, msr.lo);
225                         wrmsr(FlashPort[i], msr);
226
227                         /* now write-enable the device */
228                         msr = rdmsr(MDD_NORF_CNTRL);
229                         msr.lo |= (1 << i);
230                         printk(BIOS_DEBUG, "WRMSR(0x%08X, %08X_%08X)\n", MDD_NORF_CNTRL, msr.hi, msr.lo);
231                         wrmsr(MDD_NORF_CNTRL, msr);
232
233                         /* update the number enabled */
234                         numEnabled++;
235                 }
236         }
237
238         /* enable the flash */
239         if (0 != numEnabled) {
240                 msr = rdmsr(MDD_PIN_OPT);
241                 msr.lo &= ~1; /* PIN_OPT_IDE */
242                 printk(BIOS_DEBUG, "WRMSR(0x%08X, %08X_%08X)\n", MDD_PIN_OPT, msr.hi, msr.lo);
243                 wrmsr(MDD_PIN_OPT, msr);
244         }
245         printk(BIOS_DEBUG, "ChipsetFlashSetup--\n");
246
247 }
248
249
250  
251 /* ***************************************************************************/
252 /* **/
253 /* *    ChipsetGeodeLinkInit*/
254 /* *    Handle chipset specific GeodeLink settings here. */
255 /* *    Called from GeodeLink init code.*/
256 /* **/
257 /* *    Entry:*/
258 /* *    Exit:*/
259 /* *    Destroys: GS*/
260 /* **/
261 /* ***************************************************************************/
262 static void 
263 ChipsetGeodeLinkInit(void){
264         msr_t msr;
265         unsigned long msrnum;
266         unsigned long totalmem;
267
268         if (is_5536())
269                 return;
270         /*  SWASIF for A1 DMA */
271         /*  Set all memory to  "just above systop" PCI so DMA will work*/
272         /*  check A1*/
273         msrnum = MSR_SB_GLCP + 0x17;
274         msr = rdmsr(msrnum);
275         if ((msr.lo&0xff) == 0x11)
276                 return;
277
278         totalmem = sizeram() << 20 - 1;
279         totalmem >>= 12; 
280         totalmem = ~totalmem;
281         totalmem &= 0xfffff;
282         msr.lo = totalmem;
283         msr.hi = 0x20000000;                            /*  Port 1 (PCI)*/
284         msrnum = MSR_SB_GLIU + 0x20;            /*  */;
285         wrmsr(msrnum, msr);
286 }
287
288 void
289 chipsetinit (struct northbridge_amd_gx2_config *nb){
290         msr_t msr;
291         struct msrinit *csi;
292         int i;
293         unsigned long msrnum;
294
295         outb( P80_CHIPSET_INIT, 0x80);
296         ChipsetGeodeLinkInit();
297 #if 0
298         /* we hope NEVER to be in coreboot when S3 resumes 
299         if (! IsS3Resume()) */
300         {
301                 struct acpiinit *aci = acpi_init_table;
302                 while (aci->ioreg){
303                         if (aci->iolen == 2) {
304                                 outw(aci->regdata, aci->ioreg);
305                                 inw(aci->ioreg);
306                         } else {
307                                 outl(aci->regdata, aci->ioreg);
308                                 inl(aci->ioreg);
309                         }
310                 }
311
312                 pmChipsetInit();
313         }
314 #endif
315
316
317         if (!is_5536()) {
318                 /*  Setup USB. Need more details. #118.18*/
319                 msrnum = MSR_SB_USB1 + 8;
320                 msr.lo =  0x00012090;
321                 msr.hi = 0;
322                 wrmsr(msrnum, msr);
323                 msrnum = MSR_SB_USB2 + 8;
324                 wrmsr(msrnum, msr);
325         }
326         
327         /* set hd IRQ */
328         outl    (GPIOL_2_SET, GPIOL_INPUT_ENABLE);
329         outl    (GPIOL_2_SET, GPIOL_IN_AUX1_SELECT);
330
331         /*  Allow IO read and writes during a ATA DMA operation.*/
332         /*   This could be done in the HD rom but do it here for easier debugging.*/
333         
334         msrnum = ATA_SB_GLD_MSR_ERR;
335         msr = rdmsr(msrnum);
336         msr.lo &= ~0x100;
337         wrmsr(msrnum, msr);
338
339         /*  Enable Post Primary IDE.*/
340         msrnum = GLPCI_SB_CTRL;
341         msr = rdmsr(msrnum);
342         msr.lo |=  GLPCI_CRTL_PPIDE_SET;
343         wrmsr(msrnum, msr);
344
345
346         /*  Set up Master Configuration Register*/
347         /*  If 5536, use same master config settings as 5535, except for OHCI MSRs*/
348         if (is_5536()) 
349                 i = 2;
350         else
351                 i = 0;
352
353         csi = &SB_MASTER_CONF_TABLE[i];
354         for(; csi->msrnum; csi++){
355                 msr.lo = csi->msr.lo;
356                 msr.hi = csi->msr.hi;
357                 wrmsr(csi->msrnum, msr); // MSR - see table above
358         }
359
360
361         /*  Flash Setup*/
362         printk(BIOS_ERR, "%sDOING ChipsetFlashSetup()!!!!!!!!!!!!!!!!!!\n", nb->setupflash? " " : "NOT");
363         if (nb->setupflash)
364                 ChipsetFlashSetup();
365
366
367
368         /* */
369         /*  Set up Hardware Clock Gating*/
370         /* */
371         /* if (getnvram(TOKEN_SB_CLK_GATE) != TVALUE_DISABLE) */
372         {
373                 if (is_5536())
374                         csi = CS5536_CLOCK_GATING_TABLE;
375                 else
376                         csi = CS5535_CLOCK_GATING_TABLE;
377
378                 for(; csi->msrnum; csi++){
379                         msr.lo = csi->msr.lo;
380                         msr.hi = csi->msr.hi;
381                         wrmsr(csi->msrnum, msr);        // MSR - see table above
382                 }
383         }
384
385 }