printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / southbridge / amd / sb700 / sb700_sm.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 #include <console/console.h>
21 #include <device/device.h>
22 #include <device/pci.h>
23 #include <device/pci_ids.h>
24 #include <device/pci_ops.h>
25 #include <device/smbus.h>
26 #include <pc80/mc146818rtc.h>
27 #include <bitops.h>
28 #include <arch/io.h>
29 #include <cpu/x86/lapic.h>
30 #include <arch/ioapic.h>
31 #include <stdlib.h>
32 #include "sb700.h"
33 #include "sb700_smbus.c"
34
35 #define NMI_OFF 0
36
37 #define MAINBOARD_POWER_OFF 0
38 #define MAINBOARD_POWER_ON 1
39
40 #ifndef CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL
41 #define CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL MAINBOARD_POWER_ON
42 #endif
43
44 /*
45 * SB700 enables all USB controllers by default in SMBUS Control.
46 * SB700 enables SATA by default in SMBUS Control.
47 */
48 static void sm_init(device_t dev)
49 {
50         u8 byte;
51         u8 byte_old;
52         u32 dword;
53         u32 ioapic_base;
54         u32 on;
55         u32 nmi_option;
56
57         printk(BIOS_INFO, "sm_init().\n");
58
59         ioapic_base = pci_read_config32(dev, 0x74) & (0xffffffe0);      /* some like mem resource, but does not have  enable bit */
60         /* Don't rename APIC ID */
61         /* TODO: We should call setup_ioapic() here. But kernel hangs if cpu is K8.
62          * We need to check out why and change back. */
63         clear_ioapic(ioapic_base);
64
65         /* 2.10 Interrupt Routing/Filtering */
66         dword = pci_read_config8(dev, 0x62);
67         dword |= 3;
68         pci_write_config8(dev, 0x62, dword);
69
70         /* Delay back to back interrupts to the CPU. */
71         dword = pci_read_config16(dev, 0x64);
72         dword |= 1 << 13;
73         pci_write_config16(dev, 0x64, dword);
74
75
76         /* rrg:K8 INTR Enable (BIOS should set this bit after PIC initialization) */
77         /* rpr 2.1 Enabling Legacy Interrupt */
78         dword = pci_read_config8(dev, 0x62);
79         dword |= 1 << 2;
80         pci_write_config8(dev, 0x62, dword);
81
82         dword = pci_read_config32(dev, 0x78);
83         dword |= 1 << 9;
84         pci_write_config32(dev, 0x78, dword);   /* enable 0xCD6 0xCD7 */
85
86         /* bit 10: MultiMediaTimerIrqEn */
87         dword = pci_read_config8(dev, 0x64);
88         dword |= 1 << 10;
89         pci_write_config8(dev, 0x64, dword);
90         /* enable serial irq */
91         byte = pci_read_config8(dev, 0x69);
92         byte |= 1 << 7;         /* enable serial irq function */
93         byte &= ~(0xF << 2);
94         byte |= 4 << 2;         /* set NumSerIrqBits=4 */
95         pci_write_config8(dev, 0x69, byte);
96
97         /* IRQ0From8254 */
98         byte = pci_read_config8(dev, 0x41);
99         byte &= ~(1 << 7);
100         pci_write_config8(dev, 0x41, byte);
101
102         byte = pm_ioread(0x61);
103         byte |= 1 << 1;         /* Set to enable NB/SB handshake during IOAPIC interrupt for AMD K8/K7 */
104         pm_iowrite(0x61, byte);
105
106         /* disable SMI */
107         byte = pm_ioread(0x53);
108         byte |= 1 << 3;
109         pm_iowrite(0x53, byte);
110
111         /* power after power fail */
112         on = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
113         get_option(&on, "power_on_after_fail");
114         byte = pm_ioread(0x74);
115         byte &= ~0x03;
116         if (on) {
117                 byte |= 2;
118         }
119         byte |= 1 << 2;
120         pm_iowrite(0x74, byte);
121         printk(BIOS_INFO, "set power %s after power fail\n", on ? "on" : "off");
122
123         byte = pm_ioread(0x68);
124         byte &= ~(1 << 1);
125         /* 2.7 */
126         byte |= 1 << 2;
127         pm_iowrite(0x68, byte);
128
129         /* 2.7 */
130         byte = pm_ioread(0x65);
131         byte &= ~(1 << 7);
132         pm_iowrite(0x65, byte);
133
134         /* 2.16 */
135         byte = pm_ioread(0x55);
136         byte |= 1 << 5;
137         pm_iowrite(0x55, byte);
138
139         byte = pm_ioread(0xD7);
140         byte |= 1 << 6 | 1 << 1;;
141         pm_iowrite(0xD7, byte);
142
143         /* 2.15 */
144         byte = pm_ioread(0x42);
145         byte &= ~(1 << 2);
146         pm_iowrite(0x42, byte);
147
148         /* Set up NMI on errors */
149         byte = inb(0x70);       /* RTC70 */
150         byte_old = byte;
151         nmi_option = NMI_OFF;
152         get_option(&nmi_option, "nmi");
153         if (nmi_option) {
154                 byte &= ~(1 << 7);      /* set NMI */
155                 printk(BIOS_INFO, "++++++++++set NMI+++++\n");
156         } else {
157                 byte |= (1 << 7);       /* Can not mask NMI from PCI-E and NMI_NOW */
158                 printk(BIOS_INFO, "++++++++++no set NMI+++++\n");
159         }
160         byte &= ~(1 << 7);
161         if (byte != byte_old) {
162                 outb(byte, 0x70);
163         }
164
165         /* 2.11 IO Trap Settings */
166         abcfg_reg(0x10090, 1 << 16, 1 << 16);
167
168         /* ab index */
169         pci_write_config32(dev, 0xF0, AB_INDX);
170         /* Initialize the real time clock */
171         rtc_init(0);
172
173         /* 4.3 Enabling Upstream DMA Access */
174         axcfg_reg(0x04, 1 << 2, 1 << 2);
175         /* 4.4 Enabling IDE/PCIB Prefetch for Performance Enhancement */
176         abcfg_reg(0x10060, 9 << 17, 9 << 17);
177         abcfg_reg(0x10064, 9 << 17, 9 << 17);
178
179         /* 4.5 Enabling OHCI Prefetch for Performance Enhancement, A12 */
180         abcfg_reg(0x80, 1 << 0, 1<< 0);
181
182         /* 4.6 B-Link Client's Credit Variable Settings for the Downstream Arbitration Equation */
183         /* 4.7 Enabling Additional Address Bits Checking in Downstream */
184         /* 4.15 IO write and SMI ordering enhancement*/
185         abcfg_reg(0x9c, 3 << 0 | 1 << 8, 3 << 0 | 1 << 8);
186
187         /* 4.8 Set B-Link Prefetch Mode */
188         abcfg_reg(0x80, 3 << 17, 3 << 17);
189
190         /* 4.9 Enabling Detection of Upstream Interrupts */
191         abcfg_reg(0x94, 1 << 20 | 0x7FFFF, 1 << 20 | 0x00FEE);
192
193         /* 4.10: Enabling Downstream Posted Transactions to Pass Non-Posted
194          *  Transactions for the K8 Platform (for All Revisions) */
195         abcfg_reg(0x10090, 1 << 8, 1 << 8);
196
197         /* 4.11:Programming Cycle Delay for AB and BIF Clock Gating */
198         /* 4.12: Enabling AB and BIF Clock Gating */
199         abcfg_reg(0x10054, 0xFFFF0000, 0x1040000);
200         abcfg_reg(0x54, 0xFF << 16, 4 << 16);
201         abcfg_reg(0x54, 1 << 24, 0 << 24);
202         abcfg_reg(0x98, 0x0000FF00, 0x00004700);
203
204         /* 4.13:Enabling AB Int_Arbiter Enhancement (for All Revisions) */
205         abcfg_reg(0x10054, 0x0000FFFF, 0x07FF);
206
207         /* 4.14:Enabling Requester ID for upstream traffic. */
208         abcfg_reg(0x98, 1 << 16, 1 << 16);
209
210         /* 9.2: Enableing IDE Data Bus DD7 Pull Down Resistor */
211         byte = pm2_ioread(0xE5);
212         byte |= 1 << 2;
213         pm2_iowrite(0xE5, byte);
214
215         /* Enable IDE controller. */
216         byte = pm_ioread(0x59);
217         byte &= ~(1 << 1);
218         pm_iowrite(0x59, byte);
219
220         printk(BIOS_INFO, "sm_init() end\n");
221
222         /* Enable NbSb virtual channel */
223         axcfg_reg(0x114, 0x3f << 1, 0 << 1);
224         axcfg_reg(0x120, 0x7f << 1, 0x7f << 1);
225         axcfg_reg(0x120, 7 << 24, 1 << 24);
226         axcfg_reg(0x120, 1 << 31, 1 << 31);
227         abcfg_reg(0x50, 1 << 3, 1 << 3);
228 }
229
230 static int lsmbus_recv_byte(device_t dev)
231 {
232         u32 device;
233         struct resource *res;
234         struct bus *pbus;
235
236         device = dev->path.i2c.device;
237         pbus = get_pbus_smbus(dev);
238
239         res = find_resource(pbus->dev, 0x90);
240
241         return do_smbus_recv_byte(res->base, device);
242 }
243
244 static int lsmbus_send_byte(device_t dev, u8 val)
245 {
246         u32 device;
247         struct resource *res;
248         struct bus *pbus;
249
250         device = dev->path.i2c.device;
251         pbus = get_pbus_smbus(dev);
252
253         res = find_resource(pbus->dev, 0x90);
254
255         return do_smbus_send_byte(res->base, device, val);
256 }
257
258 static int lsmbus_read_byte(device_t dev, u8 address)
259 {
260         u32 device;
261         struct resource *res;
262         struct bus *pbus;
263
264         device = dev->path.i2c.device;
265         pbus = get_pbus_smbus(dev);
266
267         res = find_resource(pbus->dev, 0x90);
268
269         return do_smbus_read_byte(res->base, device, address);
270 }
271
272 static int lsmbus_write_byte(device_t dev, u8 address, u8 val)
273 {
274         u32 device;
275         struct resource *res;
276         struct bus *pbus;
277
278         device = dev->path.i2c.device;
279         pbus = get_pbus_smbus(dev);
280
281         res = find_resource(pbus->dev, 0x90);
282
283         return do_smbus_write_byte(res->base, device, address, val);
284 }
285 static struct smbus_bus_operations lops_smbus_bus = {
286         .recv_byte = lsmbus_recv_byte,
287         .send_byte = lsmbus_send_byte,
288         .read_byte = lsmbus_read_byte,
289         .write_byte = lsmbus_write_byte,
290 };
291
292 static void sb700_sm_read_resources(device_t dev)
293 {
294         struct resource *res;
295         u8 byte;
296
297         /* rpr2.14: Hides SM bus controller Bar1 where stores HPET MMIO base address */
298         byte = pm_ioread(0x55);
299         byte |= 1 << 7;
300         pm_iowrite(0x55, byte);
301
302         /* Get the normal pci resources of this device */
303         /* pci_dev_read_resources(dev); */
304
305         byte = pm_ioread(0x55);
306         byte &= ~(1 << 7);
307         pm_iowrite(0x55, byte);
308
309         /* apic */
310         res = new_resource(dev, 0x74);
311         res->base  = 0xfec00000;
312         res->size = 256 * 0x10;
313         res->limit = 0xFFFFFFFFUL;      /* res->base + res->size -1; */
314         res->align = 8;
315         res->gran = 8;
316         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED;
317
318         #if 0                          /* Linux ACPI crashes when it is 1. For late debugging. */
319         res = new_resource(dev, 0x14); /* TODO: hpet */
320         res->base  = 0xfed00000;        /* reset hpet to widely accepted address */
321         res->size = 0x400;
322         res->limit = 0xFFFFFFFFUL;      /* res->base + res->size -1; */
323         res->align = 8;
324         res->gran = 8;
325         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED;
326         #endif
327         /* dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER; */
328
329         /* smbus */
330         res = new_resource(dev, 0x90);
331         res->base  = 0xB00;
332         res->size = 0x10;
333         res->limit = 0xFFFFUL;  /* res->base + res->size -1; */
334         res->align = 8;
335         res->gran = 8;
336         res->flags = IORESOURCE_IO | IORESOURCE_FIXED;
337
338
339         compact_resources(dev);
340
341 }
342 static void sb700_sm_set_resources(struct device *dev)
343 {
344         struct resource *res;
345         u8 byte;
346
347         pci_dev_set_resources(dev);
348
349
350         /* rpr2.14: Make HPET MMIO decoding controlled by the memory enable bit in command register of LPC ISA bridage */
351         byte = pm_ioread(0x52);
352         byte |= 1 << 6;
353         pm_iowrite(0x52, byte);
354
355         res = find_resource(dev, 0x74);
356         pci_write_config32(dev, 0x74, res->base | 1 << 3);
357 #if 0                           /* TODO:hpet */
358         res = find_resource(dev, 0x14);
359         pci_write_config32(dev, 0x14, res->base);
360 #endif
361         res = find_resource(dev, 0x90);
362         pci_write_config32(dev, 0x90, res->base | 1);
363 }
364
365 static struct pci_operations lops_pci = {
366         .set_subsystem = pci_dev_set_subsystem,
367 };
368 static struct device_operations smbus_ops = {
369         .read_resources = sb700_sm_read_resources,
370         .set_resources = sb700_sm_set_resources,
371         .enable_resources = pci_dev_enable_resources,
372         .init = sm_init,
373         .scan_bus = scan_static_bus,
374         .ops_pci = &lops_pci,
375         .ops_smbus_bus = &lops_smbus_bus,
376 };
377 static const struct pci_driver smbus_driver __pci_driver = {
378         .ops = &smbus_ops,
379         .vendor = PCI_VENDOR_ID_ATI,
380         .device = PCI_DEVICE_ID_ATI_SB700_SM,
381 };