1f46da2228828f9b8f27d944694d9543d5b95dcc
[coreboot.git] / src / southbridge / amd / sb700 / early_setup.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 #ifndef _SB700_EARLY_SETUP_C_
21 #define _SB700_EARLY_SETUP_C_
22
23 #include <stdint.h>
24 #include <arch/cpu.h>
25 #include <arch/io.h>
26 #include <arch/romcc_io.h>
27 #include <console/console.h>
28 #include <cpu/x86/msr.h>
29
30 #include <reset.h>
31 #include <arch/cpu.h>
32 #include <cbmem.h>
33 #include "sb700.h"
34 #include "smbus.h"
35
36
37 static void pmio_write(u8 reg, u8 value)
38 {
39         outb(reg, PM_INDEX);
40         outb(value, PM_INDEX + 1);
41 }
42
43 static u8 pmio_read(u8 reg)
44 {
45         outb(reg, PM_INDEX);
46         return inb(PM_INDEX + 1);
47 }
48
49 static void sb700_acpi_init(void)
50 {
51         u16 word;
52         pmio_write(0x20, ACPI_PM_EVT_BLK & 0xFF);
53         pmio_write(0x21, ACPI_PM_EVT_BLK >> 8);
54         pmio_write(0x22, ACPI_PM1_CNT_BLK & 0xFF);
55         pmio_write(0x23, ACPI_PM1_CNT_BLK >> 8);
56         pmio_write(0x24, ACPI_PM_TMR_BLK & 0xFF);
57         pmio_write(0x25, ACPI_PM_TMR_BLK >> 8);
58         pmio_write(0x28, ACPI_GPE0_BLK & 0xFF);
59         pmio_write(0x29, ACPI_GPE0_BLK >> 8);
60
61         /* CpuControl is in \_PR.CPU0, 6 bytes */
62         pmio_write(0x26, ACPI_CPU_CONTROL & 0xFF);
63         pmio_write(0x27, ACPI_CPU_CONTROL >> 8);
64
65         pmio_write(0x2A, 0);    /* AcpiSmiCmdLo */
66         pmio_write(0x2B, 0);    /* AcpiSmiCmdHi */
67
68         pmio_write(0x2C, ACPI_PMA_CNT_BLK & 0xFF);
69         pmio_write(0x2D, ACPI_PMA_CNT_BLK >> 8);
70
71         pmio_write(0x0E, 1<<3 | 0<<2); /* AcpiDecodeEnable, When set, SB uses
72                                         * the contents of the PM registers at
73                                         * index 20-2B to decode ACPI I/O address.
74                                         * AcpiSmiEn & SmiCmdEn*/
75         pmio_write(0x10, 1<<1 | 1<<3| 1<<5); /* RTC_En_En, TMR_En_En, GBL_EN_EN */
76         word = inl(ACPI_PM1_CNT_BLK);
77         word |= 1;
78         outl(word, ACPI_PM1_CNT_BLK);             /* set SCI_EN */
79 }
80
81 /* RPR 2.28: Get SB ASIC Revision. */
82 static u8 set_sb700_revision(void)
83 {
84         device_t dev;
85         u8 rev_id, enable_14Mhz, byte;
86         u8 rev = 0;
87
88         /* if (rev != 0) return rev; */
89
90         dev = pci_locate_device(PCI_ID(0x1002, 0x4385), 0);
91
92         if (dev == PCI_DEV_INVALID) {
93                 die("SMBUS controller not found\n");
94                 /* NOT REACHED */
95         }
96         rev_id =  pci_read_config8(dev, 0x08);
97
98         if (rev_id == 0x39) {
99                 enable_14Mhz = (pmio_read(0x53) >> 6) & 1;
100                 if (enable_14Mhz == 0x0)
101                         rev = 0x11;     /* A11 */
102                 else if (enable_14Mhz == 0x1) {
103                         /* This happens, if does, only once. So later if we need to get
104                          * the revision ID, we don't have to make such a big function.
105                          * We just get reg 0x8 in smbus dev. 0x39 is A11, 0x3A is A12. */
106                         rev = 0x12;
107                         byte = pci_read_config8(dev, 0x40);
108                         byte |= 1 << 0;
109                         pci_write_config8(dev, 0x40, byte);
110
111                         pci_write_config8(dev, 0x08, 0x3A); /* Change 0x39 to 0x3A. */
112
113                         byte &= ~(1 << 0);
114                         pci_write_config8(dev, 0x40, byte);
115                 }
116         } else if (rev_id == 0x3A) { /* A12 will be 0x3A after BIOS is initialized */
117                 rev = 0x12;
118         } else if (rev_id == 0x3C) {
119                 rev = 0x14;
120         } else if (rev_id == 0x3D) {
121                 rev = 0x15;
122         } else
123                 die("It is not SB700 or SB710\n");
124
125         return rev;
126 }
127
128 /***************************************
129 * Legacy devices are mapped to LPC space.
130 *       Serial port 0
131 *       KBC Port
132 *       ACPI Micro-controller port
133 *       This function does not change port 0x80 decoding.
134 *       Console output through any port besides 0x3f8 is unsupported.
135 *       If you use FWH ROMs, you have to setup IDSEL.
136 ***************************************/
137 void sb7xx_51xx_lpc_init(void)
138 {
139         u8 reg8;
140         u32 reg32;
141         device_t dev;
142
143         dev = pci_locate_device(PCI_ID(0x1002, 0x4385), 0);     /* SMBUS controller */
144         /* NOTE: Set BootTimerDisable, otherwise it would keep rebooting!!
145          * This bit has no meaning if debug strap is not enabled. So if the
146          * board keeps rebooting and the code fails to reach here, we could
147          * disable the debug strap first. */
148         reg32 = pci_read_config32(dev, 0x4C);
149         reg32 |= 1 << 31;
150         pci_write_config32(dev, 0x4C, reg32);
151
152         /* Enable lpc controller */
153         reg32 = pci_read_config32(dev, 0x64);
154         reg32 |= 1 << 20;
155         pci_write_config32(dev, 0x64, reg32);
156
157 #if CONFIG_SOUTHBRIDGE_AMD_SP5100
158         post_code(0x66);
159         dev = pci_locate_device(PCI_ID(0x1002, 0x439d), 0);     /* LPC Controller */
160         reg8 = pci_read_config8(dev, 0xBB);
161         reg8 |= 1 << 2 | 1 << 3 | 1 << 6 | 1 << 7;
162         reg8 &= ~(1 << 1);
163         pci_write_config8(dev, 0xBB, reg8);
164 #endif
165
166         dev = pci_locate_device(PCI_ID(0x1002, 0x439d), 0);     /* LPC Controller */
167         /* Decode port 0x3f8-0x3ff (Serial 0) */
168         // XXX Serial port decode on LPC is hardcoded to 0x3f8
169         reg8 = pci_read_config8(dev, 0x44);
170         reg8 |= 1 << 6;
171 #if CONFIG_SOUTHBRIDGE_AMD_SP5100
172 #if CONFIG_TTYS0_BASE == 0x2f8
173         reg8 |= 1 << 7;
174 #endif
175 #endif
176         pci_write_config8(dev, 0x44, reg8);
177
178         /* Decode port 0x60 & 0x64 (PS/2 keyboard) and port 0x62 & 0x66 (ACPI)*/
179         reg8 = pci_read_config8(dev, 0x47);
180         reg8 |= (1 << 5) | (1 << 6);
181         pci_write_config8(dev, 0x47, reg8);
182
183         /* Enable PrefetchEnSPIFromHost to speed up SPI flash read (does not affect LPC) */
184         reg8 = pci_read_config8(dev, 0xbb);
185         reg8 |= 1 << 0;
186         pci_write_config8(dev, 0xbb, reg8);
187
188         /* Super I/O, RTC */
189         reg8 = pci_read_config8(dev, 0x48);
190         /* Decode ports 0x2e-0x2f, 0x4e-0x4f (SuperI/O configuration) */
191         reg8 |= (1 << 1) | (1 << 0);
192         /* Decode port 0x70-0x73 (RTC) */
193         reg8 |= (1 << 6);
194         pci_write_config8(dev, 0x48, reg8);
195 }
196
197 void sb7xx_51xx_enable_wideio(u8 wio_index, u16 base)
198 {
199         /* TODO: Now assume wio_index=0 */
200         device_t dev;
201         u8 reg8;
202
203         dev = pci_locate_device(PCI_ID(0x1002, 0x439d), 0);     /* LPC Controller */
204         pci_write_config32(dev, 0x64, base);
205         reg8 = pci_read_config8(dev, 0x48);
206         reg8 |= 1 << 2;
207         pci_write_config8(dev, 0x48, reg8);
208 }
209
210 void sb7xx_51xx_disable_wideio(u8 wio_index)
211 {
212         /* TODO: Now assume wio_index=0 */
213         device_t dev;
214         u8 reg8;
215
216         dev = pci_locate_device(PCI_ID(0x1002, 0x439d), 0);     /* LPC Controller */
217         pci_write_config32(dev, 0x64, 0);
218         reg8 = pci_read_config8(dev, 0x48);
219         reg8 &= ~(1 << 2);
220         pci_write_config8(dev, 0x48, reg8);
221 }
222
223 /* what is its usage? */
224 u32 __attribute__ ((weak)) get_sbdn(u32 bus)
225 {
226         device_t dev;
227
228         /* Find the device. */
229         dev = pci_locate_device_on_bus(PCI_ID(0x1002, 0x4385), bus);
230         return (dev >> 15) & 0x1f;
231 }
232
233 static u8 dual_core(void)
234 {
235         return (pci_read_config32(PCI_DEV(0, 0x18, 3), 0xE8) & (0x3<<12)) != 0;
236 }
237
238 /*
239  * RPR 2.4 C-state and VID/FID change for the K8 platform.
240  */
241 void __attribute__((weak)) enable_fid_change_on_sb(u32 sbbusn, u32 sbdn)
242 {
243         u8 byte;
244         byte = pmio_read(0x9a);
245         byte &= ~0x34;
246         if (dual_core())
247                 byte |= 0x34;
248         else
249                 byte |= 0x04;
250         pmio_write(0x9a, byte);
251
252         byte = pmio_read(0x8f);
253         byte &= ~0x30;
254         byte |= 0x20;
255         pmio_write(0x8f, byte);
256
257         pmio_write(0x8b, 0x01); /* TODO: if the HT Link is 200 MHz, it is 0x0A. It doesnt often happen. */
258         pmio_write(0x8a, 0x90);
259
260         pmio_write(0x88, 0x10);
261
262         byte = pmio_read(0x7c);
263         byte |= 0x03;
264         pmio_write(0x7c, byte);
265
266         /* Must be 0 for K8 platform. */
267         byte = pmio_read(0x68);
268         byte &= ~0x01;
269         pmio_write(0x68, byte);
270         /* Must be 0 for K8 platform. */
271         byte = pmio_read(0x8d);
272         byte &= ~(1<<6);
273         pmio_write(0x8d, byte);
274
275         byte = pmio_read(0x61);
276         byte &= ~0x04;
277         pmio_write(0x61, byte);
278
279         byte = pmio_read(0x42);
280         byte &= ~0x04;
281         pmio_write(0x42, byte);
282
283         pmio_write(0x89, 0x10);
284
285         /* Toggle the LDT_STOP# during FID/VID Change, this bit is documented
286            only in SB600!
287            While here, enable C states too
288         */
289         pmio_write(0x67, 0x6);
290 }
291
292 void sb7xx_51xx_pci_port80(void)
293 {
294         u8 byte;
295         device_t dev;
296
297         /* P2P Bridge */
298         dev = pci_locate_device(PCI_ID(0x1002, 0x4384), 0);
299
300         /* Chip Control: Enable subtractive decoding */
301         byte = pci_read_config8(dev, 0x40);
302         byte |= 1 << 5;
303         pci_write_config8(dev, 0x40, byte);
304
305         /* Misc Control: Enable subtractive decoding if 0x40 bit 5 is set */
306         byte = pci_read_config8(dev, 0x4B);
307         byte |= 1 << 7;
308         pci_write_config8(dev, 0x4B, byte);
309
310         /* The same IO Base and IO Limit here is meaningful because we set the
311          * bridge to be subtractive. During early setup stage, we have to make
312          * sure that data can go through port 0x80.
313          */
314         /* IO Base: 0xf000 */
315         byte = pci_read_config8(dev, 0x1C);
316         byte |= 0xF << 4;
317         pci_write_config8(dev, 0x1C, byte);
318
319         /* IO Limit: 0xf000 */
320         byte = pci_read_config8(dev, 0x1D);
321         byte |= 0xF << 4;
322         pci_write_config8(dev, 0x1D, byte);
323
324         /* PCI Command: Enable IO response */
325         byte = pci_read_config8(dev, 0x04);
326         byte |= 1 << 0;
327         pci_write_config8(dev, 0x04, byte);
328
329         /* LPC controller */
330         dev = pci_locate_device(PCI_ID(0x1002, 0x439D), 0);
331
332         byte = pci_read_config8(dev, 0x4A);
333         byte &= ~(1 << 5);      /* disable lpc port 80 */
334         pci_write_config8(dev, 0x4A, byte);
335 }
336
337 void sb7xx_51xx_lpc_port80(void)
338 {
339         u8 byte;
340         device_t dev;
341         u32 reg32;
342
343         /* Enable LPC controller */
344         dev = pci_locate_device(PCI_ID(0x1002, 0x4385), 0);
345         reg32 = pci_read_config32(dev, 0x64);
346         reg32 |= 0x00100000;    /* lpcEnable */
347         pci_write_config32(dev, 0x64, reg32);
348
349         /* Enable port 80 LPC decode in pci function 3 configuration space. */
350         dev = pci_locate_device(PCI_ID(0x1002, 0x439d), 0);
351         byte = pci_read_config8(dev, 0x4a);
352         byte |= 1 << 5;         /* enable port 80 */
353         pci_write_config8(dev, 0x4a, byte);
354 }
355
356 /* sbDevicesPorInitTable */
357 static void sb700_devices_por_init(void)
358 {
359         device_t dev;
360         u8 byte;
361 #if CONFIG_SOUTHBRIDGE_AMD_SP5100
362         u32 dword;
363 #endif
364
365         printk(BIOS_INFO, "sb700_devices_por_init()\n");
366         /* SMBus Device, BDF:0-20-0 */
367         printk(BIOS_INFO, "sb700_devices_por_init(): SMBus Device, BDF:0-20-0\n");
368         dev = pci_locate_device(PCI_ID(0x1002, 0x4385), 0);
369
370         if (dev == PCI_DEV_INVALID) {
371                 die("SMBUS controller not found\n");
372                 /* NOT REACHED */
373         }
374         printk(BIOS_INFO, "SMBus controller enabled, sb revision is A%x\n",
375                     set_sb700_revision());
376
377         /* sbPorAtStartOfTblCfg */
378         /* Set A-Link bridge access address. This address is set at device 14h, function 0, register 0xf0.
379          * This is an I/O address. The I/O address must be on 16-byte boundry.  */
380         pci_write_config32(dev, 0xf0, AB_INDX);
381
382         /* To enable AB/BIF DMA access, a specific register inside the BIF register space needs to be configured first. */
383         /* 4.3:Enables the SB700 to send transactions upstream over A-Link Express interface. */
384         axcfg_reg(0x04, 1 << 2, 1 << 2);
385         axindxc_reg(0x21, 0xff, 0);
386
387         /* 2.5:Enabling Non-Posted Memory Write for the K8 Platform */
388         axindxc_reg(0x10, 1 << 9, 1 << 9);
389         /* END of sbPorAtStartOfTblCfg */
390
391         /* sbDevicesPorInitTables */
392         /* set smbus iobase */
393         pci_write_config32(dev, 0x90, SMBUS_IO_BASE | 1);
394
395         /* enable smbus controller interface */
396         byte = pci_read_config8(dev, 0xd2);
397         byte |= (1 << 0);
398         pci_write_config8(dev, 0xd2, byte);
399
400         /* KB2RstEnable */
401         pci_write_config8(dev, 0x40, 0x44);
402
403         /* Enable ISA Address 0-960K decoding */
404         pci_write_config8(dev, 0x48, 0x0f);
405
406         /* Enable ISA  Address 0xC0000-0xDFFFF decode */
407         pci_write_config8(dev, 0x49, 0xff);
408
409         /* Enable decode cycles to IO C50, C51, C52 GPM controls. */
410         byte = pci_read_config8(dev, 0x41);
411         byte &= 0x80;
412         byte |= 0x33;
413         pci_write_config8(dev, 0x41, byte);
414
415         /* Legacy DMA Prefetch Enhancement, CIM masked it. */
416         /* pci_write_config8(dev, 0x43, 0x1); */
417
418         /* Disabling Legacy USB Fast SMI# */
419         byte = pci_read_config8(dev, 0x62);
420         byte |= 0x24;
421         pci_write_config8(dev, 0x62, byte);
422
423         /* Features Enable */
424         pci_write_config32(dev, 0x64, 0x829E79BF); /* bit10: Enables the HPET interrupt. */
425
426         /* SerialIrq Control */
427         pci_write_config8(dev, 0x69, 0x90);
428
429         /* Test Mode, PCIB_SReset_En Mask is set. */
430         pci_write_config8(dev, 0x6c, 0x20);
431
432         /* IO Address Enable, CIM set 0x78 only and masked 0x79. */
433         /*pci_write_config8(dev, 0x79, 0x4F); */
434         pci_write_config8(dev, 0x78, 0xFF);
435
436         /* Set smbus iospace enable, I don't know why write 0x04 into reg5 that is reserved */
437         pci_write_config16(dev, 0x4, 0x0407);
438
439         /* clear any lingering errors, so the transaction will run */
440         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
441
442         /* IDE Device, BDF:0-20-1 */
443         printk(BIOS_INFO, "sb700_devices_por_init(): IDE Device, BDF:0-20-1\n");
444         dev = pci_locate_device(PCI_ID(0x1002, 0x439C), 0);
445         /* Disable prefetch */
446         byte = pci_read_config8(dev, 0x63);
447         byte |= 0x1;
448         pci_write_config8(dev, 0x63, byte);
449
450         /* LPC Device, BDF:0-20-3 */
451         printk(BIOS_INFO, "sb700_devices_por_init(): LPC Device, BDF:0-20-3\n");
452         dev = pci_locate_device(PCI_ID(0x1002, 0x439D), 0);
453         /* DMA enable */
454         pci_write_config8(dev, 0x40, 0x04);
455
456         /* IO Port Decode Enable */
457         pci_write_config8(dev, 0x44, 0xFF);
458         pci_write_config8(dev, 0x45, 0xFF);
459         pci_write_config8(dev, 0x46, 0xC3);
460         pci_write_config8(dev, 0x47, 0xFF);
461
462         // TODO: This has already been done(?)
463         /* IO/Mem Port Decode Enable, I don't know why CIM disable some ports.
464          *  Disable LPC TimeOut counter, enable SuperIO Configuration Port (2e/2f),
465          * Alternate Super I/O Configuration Port (4e/4f), Wide Generic IO Port (64/65). */
466         byte = pci_read_config8(dev, 0x48);
467         byte |= (1 << 1) | (1 << 0);    /* enable Super IO config port 2e-2h, 4e-4f */
468         byte |= 1 << 6;         /* enable for RTC I/O range */
469         pci_write_config8(dev, 0x48, byte);
470         pci_write_config8(dev, 0x49, 0xFF);
471         /* Enable 0x480-0x4bf, 0x4700-0x470B */
472         byte = pci_read_config8(dev, 0x4A);
473         byte |= ((1 << 1) + (1 << 6));  /*0x42, save the configuraion for port 0x80. */
474         pci_write_config8(dev, 0x4A, byte);
475
476         /* Enable Tpm12_en and Tpm_legacy. I don't know what is its usage and copied from CIM. */
477         pci_write_config8(dev, 0x7C, 0x05);
478
479         /* P2P Bridge, BDF:0-20-4, the configuration of the registers in this dev are copied from CIM,
480          */
481         printk(BIOS_INFO, "sb700_devices_por_init(): P2P Bridge, BDF:0-20-4\n");
482         dev = pci_locate_device(PCI_ID(0x1002, 0x4384), 0);
483
484         /* Arbiter enable. */
485         pci_write_config8(dev, 0x43, 0xff);
486
487         /* Set PCDMA request into hight priority list. */
488         /* pci_write_config8(dev, 0x49, 0x1) */ ;
489
490         pci_write_config8(dev, 0x40, 0x26);
491
492         pci_write_config8(dev, 0x0d, 0x40);
493         pci_write_config8(dev, 0x1b, 0x40);
494         /* Enable PCIB_DUAL_EN_UP will fix potential problem with PCI cards. */
495         pci_write_config8(dev, 0x50, 0x01);
496
497 #if CONFIG_SOUTHBRIDGE_AMD_SP5100
498         /* SP5100 default SATA mode is RAID5 MODE */
499         dev = pci_locate_device(PCI_ID(0x1002, 0x4393), 0);
500         /* Set SATA Operation Mode, Set to IDE mode */
501         byte = pci_read_config8(dev, 0x40);
502         byte |= (1 << 0);
503         pci_write_config8(dev, 0x40, byte);
504
505         dword = 0x01018f00;
506         pci_write_config32(dev, 0x8, dword);
507
508         /* set SATA Device ID writable */
509         dword = pci_read_config32(dev, 0x40);
510         dword &= ~(1 << 24);
511         pci_write_config32(dev, 0x40, dword);
512
513         /* set Device ID accommodate with IDE emulation mode configuration*/
514         pci_write_config32(dev, 0x0, 0x43901002);
515
516         /* rpr v2.13 4.17 Reset CPU on Sync Flood */
517         abcfg_reg(0x10050, 1 << 2, 1 << 2);
518 #endif
519
520         /* SATA Device, BDF:0-17-0, Non-Raid-5 SATA controller */
521         printk(BIOS_INFO, "sb700_devices_por_init(): SATA Device, BDF:0-18-0\n");
522         dev = pci_locate_device(PCI_ID(0x1002, 0x4390), 0);
523
524         /*PHY Global Control*/
525         pci_write_config16(dev, 0x86, 0x2C00);
526 }
527
528 /* sbPmioPorInitTable, Pre-initializing PMIO register space
529 * The power management (PM) block is resident in the PCI/LPC/ISA bridge.
530 * The PM regs are accessed via IO mapped regs 0xcd6 and 0xcd7.
531 * The index address is first programmed into IO reg 0xcd6.
532 * Read or write values are accessed through IO reg 0xcd7.
533 */
534 static void sb700_pmio_por_init(void)
535 {
536         u8 byte;
537
538         printk(BIOS_INFO, "sb700_pmio_por_init()\n");
539         /* K8KbRstEn, KB_RST# control for K8 system. */
540         byte = pmio_read(0x66);
541         byte |= 0x20;
542         pmio_write(0x66, byte);
543
544         /* RPR2.31 PM_TURN_OFF_MSG during ASF Shutdown. */
545         if (get_sb700_revision(pci_locate_device(PCI_ID(0x1002, 0x4385), 0)) <= 0x12) {
546                 byte = pmio_read(0x65);
547                 byte &= ~(1 << 7);
548                 pmio_write(0x65, byte);
549
550                 byte = pmio_read(0x75);
551                 byte &= 0xc0;
552                 byte |= 0x05;
553                 pmio_write(0x75, byte);
554
555                 byte = pmio_read(0x52);
556                 byte &= 0xc0;
557                 byte |= 0x08;
558                 pmio_write(0x52, byte);
559         } else {
560                 byte = pmio_read(0xD7);
561                 byte |= 1 << 0;
562                 pmio_write(0xD7, byte);
563
564                 byte = pmio_read(0x65);
565                 byte |= 1 << 7;
566                 pmio_write(0x65, byte);
567
568                 byte = pmio_read(0x75);
569                 byte &= 0xc0;
570                 byte |= 0x01;
571                 pmio_write(0x75, byte);
572
573                 byte = pmio_read(0x52);
574                 byte &= 0xc0;
575                 byte |= 0x02;
576                 pmio_write(0x52, byte);
577
578         }
579
580         /* Watch Dog Timer Control
581          * Set watchdog time base to 0xfec000f0 to avoid SCSI card boot failure.
582          * But I don't find WDT is enabled in SMBUS 0x41 bit3 in CIM.
583          */
584         pmio_write(0x6c, 0xf0);
585         pmio_write(0x6d, 0x00);
586         pmio_write(0x6e, 0xc0);
587         pmio_write(0x6f, 0xfe);
588
589         /* rpr2.15: Enabling Spread Spectrum */
590         byte = pmio_read(0x42);
591         byte |= 1 << 7;
592         pmio_write(0x42, byte);
593         /* TODO: Check if it is necessary. IDE reset */
594         byte = pmio_read(0xB2);
595         byte |= 1 << 0;
596         pmio_write(0xB2, byte);
597 }
598
599 /*
600 * Add any south bridge setting.
601 */
602 static void sb700_pci_cfg(void)
603 {
604         device_t dev;
605         u8 byte;
606
607         /* SMBus Device, BDF:0-20-0 */
608         dev = pci_locate_device(PCI_ID(0x1002, 0x4385), 0);
609         /* Enable watchdog decode timer */
610         byte = pci_read_config8(dev, 0x41);
611         byte |= (1 << 3);
612         pci_write_config8(dev, 0x41, byte);
613
614         /* Set to 1 to reset USB on the software (such as IO-64 or IO-CF9 cycles)
615          * generated PCIRST#. */
616         byte = pmio_read(0x65);
617         byte |= (1 << 4);
618         pmio_write(0x65, byte);
619
620         /* IDE Device, BDF:0-20-1 */
621         dev = pci_locate_device(PCI_ID(0x1002, 0x439C), 0);
622         /* Enable IDE Explicit prefetch, 0x63[0] clear */
623         byte = pci_read_config8(dev, 0x63);
624         byte &= 0xfe;
625         pci_write_config8(dev, 0x63, byte);
626
627         /* LPC Device, BDF:0-20-3 */
628         /* The code below is ported from old chipset. It is not
629          * mentioned in RPR. But I keep them. The registers and the
630          * comments are compatible. */
631         dev = pci_locate_device(PCI_ID(0x1002, 0x439D), 0);
632         /* Enabling LPC DMA function. */
633         byte = pci_read_config8(dev, 0x40);
634         byte |= (1 << 2);
635         pci_write_config8(dev, 0x40, byte);
636         /* Disabling LPC TimeOut. 0x48[7] clear. */
637         byte = pci_read_config8(dev, 0x48);
638         byte &= 0x7f;
639         pci_write_config8(dev, 0x48, byte);
640         /* Disabling LPC MSI Capability, 0x78[1] clear. */
641         byte = pci_read_config8(dev, 0x78);
642         byte &= 0xfd;
643         pci_write_config8(dev, 0x78, byte);
644
645         /* SATA Device, BDF:0-17-0, Non-Raid-5 SATA controller */
646         dev = pci_locate_device(PCI_ID(0x1002, 0x4390), 0);
647         /* rpr7.12 SATA MSI and D3 Power State Capability. */
648         byte = pci_read_config8(dev, 0x40);
649         byte |= 1 << 0;
650         pci_write_config8(dev, 0x40, byte);
651         if (get_sb700_revision(pci_locate_device(PCI_ID(0x1002, 0x4385), 0)) <= 0x12)
652                 pci_write_config8(dev, 0x34, 0x70); /* set 0x61 to 0x70 if S1 is not supported. */
653         else
654                 pci_write_config8(dev, 0x34, 0x50); /* set 0x61 to 0x50 if S1 is not supported. */
655         byte &= ~(1 << 0);
656         pci_write_config8(dev, 0x40, byte);
657 }
658
659 /*
660 */
661 static void sb700_por_init(void)
662 {
663         /* sbDevicesPorInitTable + sbK8PorInitTable */
664         sb700_devices_por_init();
665
666         /* sbPmioPorInitTable + sbK8PmioPorInitTable */
667         sb700_pmio_por_init();
668 }
669
670 /*
671 * It should be called during early POST after memory detection and BIOS shadowing but before PCI bus enumeration.
672 */
673 void sb7xx_51xx_before_pci_init(void)
674 {
675         sb700_pci_cfg();
676 }
677
678 /*
679 * This function should be called after enable_sb700_smbus().
680 */
681 void sb7xx_51xx_early_setup(void)
682 {
683         printk(BIOS_INFO, "sb700_early_setup()\n");
684         sb700_por_init();
685         sb700_acpi_init();
686 }
687
688 int s3_save_nvram_early(u32 dword, int size, int  nvram_pos)
689 {
690         int i;
691         printk(BIOS_DEBUG, "Writing %x of size %d to nvram pos: %d\n", dword, size, nvram_pos);
692
693         for (i = 0; i<size; i++) {
694                 outb(nvram_pos, BIOSRAM_INDEX);
695                 outb((dword >>(8 * i)) & 0xff , BIOSRAM_DATA);
696                 nvram_pos++;
697         }
698
699         return nvram_pos;
700 }
701
702 int s3_load_nvram_early(int size, u32 *old_dword, int nvram_pos)
703 {
704         u32 data = *old_dword;
705         int i;
706         for (i = 0; i<size; i++) {
707                 outb(nvram_pos, BIOSRAM_INDEX);
708                 data &= ~(0xff << (i * 8));
709                 data |= inb(BIOSRAM_DATA) << (i *8);
710                 nvram_pos++;
711         }
712         *old_dword = data;
713         printk(BIOS_DEBUG, "Loading %x of size %d to nvram pos:%d\n", *old_dword, size,
714                 nvram_pos-size);
715         return nvram_pos;
716 }
717
718 #if CONFIG_HAVE_ACPI_RESUME == 1
719 int acpi_is_wakeup_early(void)
720 {
721         u16 tmp;
722         tmp = inw(ACPI_PM1_CNT_BLK);
723         printk(BIOS_DEBUG, "IN TEST WAKEUP %x\n", tmp);
724         return (((tmp & (7 << 10)) >> 10) == 3);
725 }
726 #endif
727
728 struct cbmem_entry *get_cbmem_toc(void)
729 {
730         uint32_t xdata = 0;
731         int xnvram_pos = 0xfc, xi;
732         for (xi = 0; xi<4; xi++) {
733                 outb(xnvram_pos, BIOSRAM_INDEX);
734                 xdata &= ~(0xff << (xi * 8));
735                 xdata |= inb(BIOSRAM_DATA) << (xi *8);
736                 xnvram_pos++;
737         }
738         return (struct cbmem_entry *) xdata;
739 }
740
741 #endif