printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / mainboard / amd / pistachio / mainboard.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008 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 #include <console/console.h>
21 #include <device/device.h>
22 #include <device/pci.h>
23 #include <arch/io.h>
24 #include <boot/coreboot_tables.h>
25 #include <cpu/x86/msr.h>
26 #include <cpu/amd/mtrr.h>
27 #include <device/pci_def.h>
28 #include <../southbridge/amd/sb600/sb600.h>
29 #include "chip.h"
30
31 #define ADT7475_ADDRESS 0x2E
32 #define SMBUS_IO_BASE 0x1000
33
34 extern int do_smbus_read_byte(u32 smbus_io_base, u32 device, u32 address);
35 extern int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address,
36                                u8 val);
37 extern void lb_add_memory_range(struct lb_memory *mem, uint32_t type,
38                                 uint64_t start, uint64_t size);
39
40 #define ADT7475_read_byte(address) \
41          do_smbus_read_byte(SMBUS_IO_BASE, ADT7475_ADDRESS, address)
42 #define ADT7475_write_byte(address, val) \
43         do_smbus_write_byte(SMBUS_IO_BASE, ADT7475_ADDRESS, address, val)
44
45 uint64_t uma_memory_base, uma_memory_size;
46
47 /********************************************************
48 * pistachio uses a BCM5787 as on-board NIC.
49 * It has a pin named LOW_POWER to enable it into LOW POWER state.
50 * In order to run NIC, we should let it out of Low power state. This pin is
51 * controlled by GPM8.
52 * RRG4.2.3 GPM as GPIO
53 * GPM pins can be used as GPIO. The GPM I/O functions is controlled by three registers:
54 * I/O C50, C51, C52, PM I/O94, 95, 96.
55 * RRG4.2.3.1 GPM pins as Input
56 * RRG4.2.3.2 GPM pins as Output
57 * The R77 (on BRASS) / R81 (on Bronze) is not load!
58 * So NIC can work whether this function runs.
59 ********************************************************/
60 static void enable_onboard_nic()
61 {
62         u8 byte;
63
64         printk(BIOS_INFO, "%s.\n", __func__);
65
66         /* enable GPM8 output */
67         byte = pm_ioread(0x95);
68         byte &= 0xfe;
69         pm_iowrite(0x95, byte);
70
71         /* GPM8 outputs low. */
72         byte = pm_ioread(0x94);
73         byte &= 0xfe;
74         pm_iowrite(0x94, byte);
75 }
76
77 /*
78  * set thermal config
79  */
80 static void set_thermal_config()
81 {
82         u8 byte, byte2;
83         u16 word;
84         u32 dword;
85         device_t sm_dev;
86
87         /* set adt7475 */
88         ADT7475_write_byte(0x40, 0x04);
89         /* Config Register 6 */
90         ADT7475_write_byte(0x10, 0x00);
91         /* Config Register 7 */
92         ADT7475_write_byte(0x11, 0x00);
93
94         /* set Offset 64 format, enable THERM on Remote 1& Remote 2 */
95         ADT7475_write_byte(0x7c, 0xa0);
96         /* No offset for remote 2 */
97         ADT7475_write_byte(0x72, 0x00);
98         /* PWM 1 configuration register    CPU fan controlled by CPU Thermal Diode */
99         ADT7475_write_byte(0x5c, 0x02);
100         /* PWM 3 configuration register    Case fan controlled by 690 temp */
101         ADT7475_write_byte(0x5e, 0x42);
102
103         /* remote 1 low temp limit */
104         ADT7475_write_byte(0x4e, 0x00);
105         /* remote 1 High temp limit    (90C) */
106         ADT7475_write_byte(0x4f, 0x9a);
107
108         /* remote2 Low Temp Limit */
109         ADT7475_write_byte(0x52, 0x00);
110         /* remote2 High Limit    (90C) */
111         ADT7475_write_byte(0x53, 0x9a);
112
113         /*  remote 1 therm temp limit    (95C) */
114         ADT7475_write_byte(0x6a, 0x9f);
115         /* remote 2 therm temp limit    (95C) */
116         ADT7475_write_byte(0x6c, 0x9f);
117
118         /* PWM 1 minimum duty cycle     (37%) */
119         ADT7475_write_byte(0x64, 0x60);
120         /* PWM 1 Maximum duty cycle    (100%) */
121         ADT7475_write_byte(0x38, 0xff);
122         /* PWM 3 minimum duty cycle     (37%) */
123         ADT7475_write_byte(0x66, 0x60);
124         /* PWM 3 Maximum Duty Cycle    (100%) */
125         ADT7475_write_byte(0x3a, 0xff);
126
127         /*  Remote 1 temperature Tmin     (32C) */
128         ADT7475_write_byte(0x67, 0x60);
129         /* Remote 2 temperature Tmin     (32C) */
130         ADT7475_write_byte(0x69, 0x60);
131         /* remote 1 Trange            (53C ramp range) */
132         ADT7475_write_byte(0x5f, 0xe8);
133         /* remote 2 Trange            (53C ramp range) */
134         ADT7475_write_byte(0x61, 0xe8);
135
136         /* PWM2 Duty cycle */
137         ADT7475_write_byte(0x65, 0x00);
138         /* PWM2 Disabled */
139         ADT7475_write_byte(0x5d, 0x80);
140         /* PWM2 Max Duty Cycle */
141         ADT7475_write_byte(0x39, 0x00);
142
143         /* Config Register 3 - enable smbalert & therm */
144         ADT7475_write_byte(0x78, 0x03);
145         /* Config Register 4 - enable therm output */
146         ADT7475_write_byte(0x7d, 0x09);
147         /* Interrupt Mask Register 2 - Mask SMB alert for Therm Conditions, Fan 2 fault, SmbAlert Fan for Therm Timer event */
148         ADT7475_write_byte(0x75, 0x2a);
149         /* Config Register 1 Set Start bit */
150         ADT7475_write_byte(0x40, 0x05);
151         /* Read status register to clear any old errors */
152         byte2 = ADT7475_read_byte(0x42);
153         byte = ADT7475_read_byte(0x41);
154
155         /* remote 1 temperature offset */
156         ADT7475_write_byte(0x70, 0x00);
157
158         printk(BIOS_INFO, "Init adt7475 end , status 0x42 %02x, status 0x41 %02x\n",
159                     byte2, byte);
160
161         /* sb600 setting for thermal config. Set SB600 GPM5 to trigger ACPI event */
162         /* set GPM5 as GPM5, not DDR3_memory disable */
163         byte = pm_ioread(0x8f);
164         byte |= 1 << 6;         /* enable GPE */
165         pm_iowrite(0x8f, byte);
166
167         /* GPM5 as GPIO not USB OC */
168         sm_dev = dev_find_slot(0, PCI_DEVFN(0x14, 0));
169         dword = pci_read_config32(sm_dev, 0x64);
170         dword |= 1 << 19;
171         pci_write_config32(sm_dev, 0x64, dword);
172
173         /* Enable Client Management Index/Data registers */
174         dword = pci_read_config32(sm_dev, 0x78);
175         dword |= 1 << 11;       /* Cms_enable */
176         pci_write_config32(sm_dev, 0x78, dword);
177
178         /* MiscfuncEnable */
179         byte = pci_read_config8(sm_dev, 0x41);
180         byte |= (1 << 5);
181         pci_write_config8(sm_dev, 0x41, byte);
182
183         /* set GPM5 as input */
184         /* set index register 0C50h to 13h (miscellaneous control) */
185         outb(0x13, 0xC50);      /* CMIndex */
186         /* set CM data register 0C51h bits [7:6] to 01b to set Input/Out control */
187         byte = inb(0xC51);      /* CMData */
188         byte &= 0x3f;
189         byte |= 1 << 6;
190         outb(byte, 0xC51);
191         /* set GPM port 0C52h bit 5 to 1 to tri-state the GPM port */
192         byte = inb(0xc52);      /* GpmPort */
193         byte |= 1 << 5;
194         outb(byte, 0xc52);
195         /* set CM data register 0C51h bits [7:6] to 00b to set GPM port for read */
196         byte = inb(0xc51);
197         byte &= 0x3f;
198         outb(byte, 0xc51);
199
200         /* trigger SCI/SMI */
201         byte = pm_ioread(0x34);
202         byte &= 0xcf;
203         pm_iowrite(0x34, byte);
204
205         /* set GPM5 to not wake from s5 */
206         byte = pm_ioread(0x77);
207         byte &= ~(1 << 5);
208         pm_iowrite(0x77, byte);
209
210         /* trigger on falling edge */
211         byte = pm_ioread(0x38);
212         byte &= ~(1 << 2);
213         pm_iowrite(0x38, byte);
214
215         /* set SB600 GPIO 64 to GPIO with pull-up */
216         byte = pm2_ioread(0x42);
217         byte &= 0x3f;
218         pm2_iowrite(0x42, byte);
219
220         /* set GPIO 64 to input */
221         word = pci_read_config16(sm_dev, 0x56);
222         word |= 1 << 7;
223         pci_write_config16(sm_dev, 0x56, word);
224
225         /* set GPIO 64 internal pull-up */
226         byte = pm2_ioread(0xf0);
227         byte &= 0xee;
228         pm2_iowrite(0xf0, byte);
229
230         /* set Talert to be active low */
231         byte = pm_ioread(0x67);
232         byte &= ~(1 << 5);
233         pm_iowrite(0x67, byte);
234
235         /* set Talert to generate ACPI event */
236         byte = pm_ioread(0x3c);
237         byte &= 0xf3;
238         pm_iowrite(0x3c, byte);
239
240         /* THERMTRIP pin */
241         /* byte = pm_ioread(0x68);
242          * byte |= 1 << 3;
243          * pm_iowrite(0x68, byte);
244          *
245          * byte = pm_ioread(0x55);
246          * byte |= 1 << 0;
247          * pm_iowrite(0x55, byte);
248          *
249          * byte = pm_ioread(0x67);
250          * byte &= ~( 1 << 6);
251          * pm_iowrite(0x67, byte);
252          */
253 }
254
255 /*************************************************
256 * enable the dedicated function in pistachio board.
257 * This function called early than rs690_enable.
258 *************************************************/
259 void pistachio_enable(device_t dev)
260 {
261         struct mainboard_config *mainboard =
262             (struct mainboard_config *)dev->chip_info;
263
264         printk(BIOS_INFO, "Mainboard Pistachio Enable. dev=0x%p\n", dev);
265
266 #if (CONFIG_GFXUMA == 1)
267         msr_t msr, msr2;
268
269         /* TOP_MEM: the top of DRAM below 4G */
270         msr = rdmsr(TOP_MEM);
271         printk(BIOS_INFO, "%s, TOP MEM: msr.lo = 0x%08x, msr.hi = 0x%08x\n",
272                     __func__, msr.lo, msr.hi);
273
274         /* TOP_MEM2: the top of DRAM above 4G */
275         msr2 = rdmsr(TOP_MEM2);
276         printk(BIOS_INFO, "%s, TOP MEM2: msr2.lo = 0x%08x, msr2.hi = 0x%08x\n",
277                     __func__, msr2.lo, msr2.hi);
278
279         switch (msr.lo) {
280         case 0x10000000:        /* 256M system memory */
281                 uma_memory_size = 0x2000000;    /* 32M recommended UMA */
282                 break;
283
284         case 0x18000000:        /* 384M system memory */
285                 uma_memory_size = 0x4000000;    /* 64M recommended UMA */
286                 break;
287
288         case 0x20000000:        /* 512M system memory */
289                 uma_memory_size = 0x4000000;    /* 64M recommended UMA */
290                 break;
291
292         default:                /* 1GB and above system memory */
293                 uma_memory_size = 0x8000000;    /* 128M recommended UMA */
294                 break;
295         }
296
297         uma_memory_base = msr.lo - uma_memory_size;     /* TOP_MEM1 */
298         printk(BIOS_INFO, "%s: uma size 0x%08llx, memory start 0x%08llx\n",
299                     __func__, uma_memory_size, uma_memory_base);
300
301         /* TODO: TOP_MEM2 */
302 #else
303         uma_memory_size = 0x8000000;    /* 128M recommended UMA */
304         uma_memory_base = 0x38000000;   /* 1GB  system memory supposed */
305 #endif
306
307         enable_onboard_nic();
308
309         set_thermal_config();
310 }
311
312 int add_mainboard_resources(struct lb_memory *mem)
313 {
314         /* UMA is removed from system memory in the northbridge code, but
315          * in some circumstances we want the memory mentioned as reserved.
316          */
317 #if (CONFIG_GFXUMA == 1)
318         printk(BIOS_INFO, "uma_memory_base=0x%llx, uma_memory_size=0x%llx \n",
319         uma_memory_base, uma_memory_size);
320         lb_add_memory_range(mem, LB_MEM_RESERVED,
321                 uma_memory_base, uma_memory_size);
322 #endif
323 }
324
325 struct chip_operations mainboard_ops = {
326         CHIP_NAME("AMD Pistachio Mainboard")
327         .enable_dev = pistachio_enable,
328 };